Skip to content
Snippets Groups Projects
Commit 01fa9ca2 authored by Jesper's avatar Jesper
Browse files

Always cache abstract grammar declared NTAs

* Renamed the AttrDecl.isLazy() attribute to AttrDecl.isMemoized().
* AttrDecl.hasCache() was removed since it was equivalent to AttrDecl.isMemoized().
* Inlined AttrDecl.shouldCache(CacheMode) into AttrDecl.isMemoized().
* Simplified boolean conditionals (isMemoized() || isCircular()) using the
  fact that isCircular() is implied by isMemoized().

fixes #72 (bitbucket)
parent 4fd5ee9c
No related branches found
No related tags found
No related merge requests found
......@@ -4,6 +4,8 @@
written in CSV format to the specified file. The number of synthesized,
inherited, and collection attribute declarations and equations written
for each AST class.
* NTAs declared in the abstract grammar will now always be cached,
just like attribute-only NTAs.
2016-10-24 Jesper Öqvist <jesper.oqvist@cs.lth.se>
......
......@@ -52,7 +52,7 @@ aspect Flush {
syn String AttrDecl.resetVisit() = templateContext().expand("AttrDecl.resetAttrVisit");
public void AttrDecl.emitResetMethod(PrintStream out) {
if (isLazy() || isCircular()) {
if (isMemoized()) {
templateContext().expand("AttrDecl.resetMethod", out);
}
}
......
......@@ -86,13 +86,17 @@ aspect JragCodeGen {
syn String Rewrite.sourceLocation() = ASTNode.sourceLocation(getFileName(), getStartLine());
/**
* Decide if an attribute should be cached based on the cache mode and
* current global configuration.
* Decides if this attribute should be memoized.
*/
syn boolean AttrDecl.shouldCache(CacheMode mode) {
switch (mode) {
syn boolean AttrDecl.isMemoized() {
if (declaredNTA() || isCircular()) {
// NTAs and circular attributes are always cached.
return true;
}
// Check the cache mode:
switch (getCacheMode()) {
case DEFAULT:
return config().cacheAll();
return config().cacheAll() || isNTA();
case LAZY:
return !config().cacheNone() || isNTA();
case CACHED:
......@@ -100,16 +104,10 @@ aspect JragCodeGen {
case UNCACHED:
return isNTA();
}
// Only if there is a missing switch case:
throw new Error("unhandled cache mode");
}
syn boolean AttrDecl.isLazy() = declaredNTA() || isCircular() || shouldCache(getCacheMode());
eq SynDecl.isLazy() = declaredNTA() || isCircular() || shouldCache(getCacheMode());
eq InhDecl.isLazy() = declaredNTA() || isCircular() || shouldCache(getCacheMode());
eq CollDecl.isLazy() = declaredNTA() || isCircular() || shouldCache(getCacheMode());
public String Grammar.genImportsList() {
Set imports = new LinkedHashSet();
......@@ -226,12 +224,12 @@ aspect JragCodeGen {
sb.append(String.format("if (%s_visited == null) %s_visited = %s;\n",
signature(), signature(), config().createDefaultSet()));
}
if (getNumParameter() != 0 && isLazy() && !simpleCacheCheck()) {
if (getNumParameter() != 0 && isMemoized() && !simpleCacheCheck()) {
sb.append(String.format("if (%s_computed == null) %s_computed = %s;\n",
signature(), signature(), config().createDefaultMap()));
}
}
if (getNumParameter() != 0 && (isLazy() || isCircular())) {
if (getNumParameter() != 0 && (isMemoized() || isCircular())) {
sb.append(String.format("if (%s_values == null) %s_values = %s;\n",
signature(), signature(), config().createDefaultMap()));
}
......@@ -304,7 +302,7 @@ aspect JragCodeGen {
syn boolean TokenComponent.isPrimitive() = AttrDecl.isPrimitiveType(getTokenId().getTYPE());
public String AttrDecl.parameterStructure() {
if (!isParameterized() || (!isLazy() && !isCircular() && !config().visitCheckEnabled())) {
if (!isParameterized() || (!isMemoized() && !config().visitCheckEnabled())) {
return "";
} else if (getNumParameter() == 1) {
return "Object _parameters = " + getParameter(0).getName() + ";\n";
......@@ -323,7 +321,7 @@ aspect JragCodeGen {
* Generate the state variable if needed.
*/
public String AttrDecl.lazyState() {
if (isLazy()) {
if (isMemoized()) {
return config().stateClassName() + " state = state();";
} else {
return "";
......@@ -381,7 +379,7 @@ aspect JragCodeGen {
public boolean TypeDecl.hasLazySynEqFor(AttrDecl attr) {
if (attr instanceof SynDecl) {
SynEq synEq = lookupSynEq(attr.signature());
return synEq != null && (synEq.decl().isLazy() || synEq.decl().isCircular());
return synEq != null && synEq.decl().isMemoized();
}
return false;
}
......@@ -432,7 +430,7 @@ aspect JragCodeGen {
syn boolean AttrEq.canInlineBlock(AttrDecl decl) = false;
eq SynEq.canInlineBlock(AttrDecl decl) =
hasComputeBlock() && !decl.isLazy() && !decl.isNTA() && !decl.isCircular();
hasComputeBlock() && !decl.isMemoized();
/**
* Generates the method to compute an attribute with a specific equation
......@@ -520,7 +518,7 @@ aspect JragCodeGen {
attr.emitVisitedDeclarations(out);
if (!attr.isCircular()) {
attr.emitResetMethod(out);
if (attr.isLazy()) {
if (attr.isMemoized()) {
attr.emitCacheDeclarations(out);
}
} else if (attr.getNumParameter() == 0) {
......@@ -770,8 +768,6 @@ aspect JragCodeGen {
}
}
syn boolean AttrDecl.hasCache() = isLazy() || isCircular();
/**
* @return {@code true} if the attribute declaration corresponds to a
* non-token NTA component
......@@ -786,7 +782,7 @@ aspect Compute {
"getParent().Define_" + name() + "(this, null" + inhParametersTail() + ")";
syn String AttrDecl.computeLhs() {
if (isLazy() && getNumParameter() == 0) {
if (isMemoized() && getNumParameter() == 0) {
return signature() + "_value";
} else {
return getType() + " " + signature() + "_value";
......
......@@ -50,7 +50,7 @@ aspect Trace {
} else if (this instanceof CollDecl) {
b.append("coll ");
}
if (isLazy()) {
if (isMemoized()) {
b.append("lazy ");
}
if (isCircular()) {
......
......@@ -64,13 +64,13 @@ aspect IncrementalDDG {
ArrayList list = new ArrayList();
for (int k = 0; k < getNumSynDecl(); k++) {
AttrDecl attr = getSynDecl(k);
if (attr.isLazy() || attr.isCircular()) {
if (attr.isMemoized()) {
list.add(attr);
}
}
for (int k = 0; k < getNumInhDecl(); k++) {
AttrDecl attr = getInhDecl(k);
if (attr.isLazy() || attr.isCircular()) {
if (attr.isMemoized()) {
list.add(attr);
}
}
......@@ -145,13 +145,13 @@ aspect IncrementalDDG {
ArrayList list = new ArrayList();
for (int k = 0; k < getNumSynDecl(); k++) {
AttrDecl attr = getSynDecl(k);
if (attr != null && (attr.isLazy() || attr.isCircular())) {
if (attr != null && attr.isMemoized()) {
list.add(attr);
}
}
for (int k = 0; k < getNumInhDecl(); k++) {
AttrDecl attr = getInhDecl(k);
if (attr != null && (attr.isLazy() || attr.isCircular())) {
if (attr != null && attr.isMemoized()) {
list.add(attr);
}
}
......
......@@ -61,13 +61,13 @@ aspect IncrementalEval {
}
for (int k = 0; k < getNumSynEq(); k++) {
AttrDecl attr = getSynEq(k).decl();
if (attr != null && (attr.isLazy() || attr.isCircular())) {
if (attr != null && attr.isMemoized()) {
list.add(attr);
}
}
for (int k = 0; k < getNumInhDecl(); k++) {
InhDecl attr = getInhDecl(k);
if (attr.isLazy() || attr.isCircular()) {
if (attr.isMemoized()) {
list.add(attr);
}
}
......
......@@ -306,7 +306,7 @@ AttrDecl.emitEquation [[
]]
AttrDecl.cacheCheck [[
$if(#hasCache)
$if(#isMemoized)
$include(AttrDecl.incHookAttrRead)
$if(!#isParameterized)
$if(#simpleCacheCheck)
......@@ -342,7 +342,7 @@ $endif
# Update the cache value for this attribute if caching is enabled.
AttrDecl.cacheUpdate [[
$if(#isLazy)
$if(#isMemoized)
$if(LegacyRewrite)
if (#cacheStoreCondition) {
$endif
......@@ -392,7 +392,7 @@ $endif
AttrDecl.cacheInit [[
$if(LegacyRewrite)
$if(#isLazy)
$if(#isMemoized)
$include(AttrDecl.cacheInitRewrite)
$endif
$endif
......@@ -416,7 +416,7 @@ state().assertSameCircle(#(signature)_circle, "#hostClassName.#signatureJavaStyl
#(signature)_circle = state().currentCircle();
state().enterLazyAttribute();
$else
$if(#isLazy)
$if(#isMemoized)
$if(#simpleCacheCheck)
state().enterLazyAttribute();
$endif
......@@ -428,7 +428,7 @@ AttrDecl.leaveLazyAttribute [[
$if(ComponentCheck)
state().leaveLazyAttribute();
$else
$if(#isLazy)
$if(#isMemoized)
$if(#simpleCacheCheck)
state().leaveLazyAttribute();
$endif
......
......@@ -126,7 +126,7 @@ $endif
]]
AttrDecl.resetAttrCache [[
$if(#isLazy)
$if(#isMemoized)
$if(#isParameterized)
$if(!#simpleCacheCheck)
#(signature)_computed = $CreateDefaultMap;
......
......@@ -420,7 +420,7 @@ $endif
AttrDecl.incHookAttrCompStart = [[
$if(IncrementalEnabled)
$if(#hasCache)
$if(#isMemoized)
$if(IncrementalLevelParam)
$DDGNodeName tmpHandler = new $DDGNodeName(this, "", null);
......@@ -455,7 +455,7 @@ $endif
AttrDecl.incHookAttrCompStartCircular = [[
$if(IncrementalEnabled)
$if(#hasCache)
$if(#isMemoized)
$if(IncrementalLevelParam)
$if(!#isParameterized)
......@@ -491,7 +491,7 @@ $endif
AttrDecl.incHookAttrCompEnd = [[
$if(IncrementalEnabled)
$if(#hasCache)
$if(#isMemoized)
$if(IncrementalLevelParam)
state().exitAttrStoreEval(tmpHandler);
......@@ -529,7 +529,7 @@ $endif
AttrDecl.incHookAttrCompEndCircular = [[
$if(IncrementalEnabled)
$if(#hasCache)
$if(#isMemoized)
$if(IncrementalLevelParam)
$if(!#isParameterized)
......@@ -571,7 +571,7 @@ $endif
AttrDecl.incHookAttrCompBeforeStore = [[
$if(IncrementalEnabled)
$if(#hasCache)
$if(#isMemoized)
$if(IncrementalLevelParam)
$if(!#isParameterized)
......@@ -597,7 +597,7 @@ $endif
AttrDecl.incHookAttrCompAfterStore = [[
$if(IncrementalEnabled)
$if(#hasCache)
$if(#isMemoized)
$if(IncrementalLevelParam)
tmpHandler.clearDependencies();
......
......@@ -39,7 +39,7 @@ $if (TraceCompute)
$if(#isCircular)
state().trace().computeBegin(this, "#hostClassName.#signatureJavaStyle", _parameters, "");
$else
$if(!#isLazy)
$if(!#isMemoized)
$if(!VisitCheckEnabled)
$if(#hasOneParameter)
Object _parameters = #parameters;
......@@ -82,7 +82,7 @@ $endif
AttrDecl.traceCacheRead [[
$if (TraceCache)
$if(#isLazy)
$if(#isMemoized)
$if(#isParameterized)
$if(#isCircular)
state().trace().cacheRead(this, "#hostClassName.#signatureJavaStyle", _parameters, _cache);
......@@ -98,7 +98,7 @@ $endif
AttrDecl.traceCacheStore [[
$if (TraceCache)
$if(#isLazy)
$if(#isMemoized)
$if(#isParameterized)
$if(#isCircular)
state().trace().cacheWrite(this, "#hostClassName.#signatureJavaStyle", _parameters, _value.value);
......@@ -114,7 +114,7 @@ $endif
AttrDecl.traceCacheAbort [[
$if (TraceCache)
$if(#isLazy)
$if(#isMemoized)
$if(#isParameterized)
$if(#isCircular)
state().trace().cacheAbort(this, "#hostClassName.#signatureJavaStyle", _parameters, _value.value);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment