Store number of locals as a field in ScriptOrFnNode and not as int node property.

This commit is contained in:
igor%mir2.org 2003-02-17 17:41:04 +00:00
parent 7c4d921fe0
commit 511c2e945f
7 changed files with 74 additions and 58 deletions

View File

@ -58,10 +58,10 @@ public class IRFactory {
{
ScriptOrFnNode result = new ScriptOrFnNode(TokenStream.SCRIPT);
result.variableTable = vars;
result.encodedSource = source;
result.sourceName = sourceName;
result.baseLineno = baseLineno;
result.endLineno = endLineno;
result.setEncodedSource(source);
result.setSourceName(sourceName);
result.setBaseLineno(baseLineno);
result.setEndLineno(endLineno);
Node children = ((Node) body).getFirstChild();
if (children != null) { result.addChildrenToBack(children); }
@ -213,10 +213,10 @@ public class IRFactory {
}
FunctionNode f = compiler.createFunctionNode(this, name);
f.variableTable = vars;
f.encodedSource = source;
f.sourceName = sourceName;
f.baseLineno = baseLineno;
f.endLineno = endLineno;
f.setEncodedSource(source);
f.setSourceName(sourceName);
f.setBaseLineno(baseLineno);
f.setEndLineno(endLineno);
f.setFunctionType(functionType);
f.addChildToBack((Node)statements);
Node result = Node.newString(TokenStream.FUNCTION, name);

View File

@ -423,7 +423,7 @@ public class Interpreter {
child = child.getNext();
childCount++;
}
if (node.getProp(Node.SPECIALCALL_PROP) != null) {
if (node.getIntProp(Node.SPECIALCALL_PROP, 0) != 0) {
// embed line number and source filename
iCodeTop = addByte(TokenStream.CALLSPECIAL, iCodeTop);
iCodeTop = addShort(itsLineNumber, iCodeTop);

View File

@ -286,7 +286,6 @@ public class Node implements Cloneable {
SPECIAL_PROP_PROP = 16,
LABEL_PROP = 17,
FINALLY_PROP = 18,
LOCALCOUNT_PROP = 19,
/*
the following properties are defined and manipulated by the
optimizer -
@ -301,13 +300,13 @@ public class Node implements Cloneable {
matches.
*/
TARGETBLOCK_PROP = 20,
VARIABLE_PROP = 21,
LASTUSE_PROP = 22,
ISNUMBER_PROP = 23,
DIRECTCALL_PROP = 24,
TARGETBLOCK_PROP = 19,
VARIABLE_PROP = 20,
LASTUSE_PROP = 21,
ISNUMBER_PROP = 22,
DIRECTCALL_PROP = 23,
SPECIALCALL_PROP = 25;
SPECIALCALL_PROP = 24;
public static final int // this value of the ISNUMBER_PROP specifies
BOTH = 0, // which of the children are Number types
@ -337,7 +336,6 @@ public class Node implements Cloneable {
case SPECIAL_PROP_PROP: return "special_prop";
case LABEL_PROP: return "label";
case FINALLY_PROP: return "finally";
case LOCALCOUNT_PROP: return "localcount";
case TARGETBLOCK_PROP: return "targetblock";
case VARIABLE_PROP: return "variable";

View File

@ -63,8 +63,9 @@ public class NodeTransformer {
loops = new ObjArray();
loopEnds = new ObjArray();
inFunction = tree.getType() == TokenStream.FUNCTION;
VariableTable vars = getVariableTable(tree);
VariableTable vars = tree.getVariableTable();
if (inFunction) {
Node stmts = tree.getLastChild();
FunctionNode fn = (FunctionNode)tree;
if (fn.getFunctionType() == FunctionNode.FUNCTION_EXPRESSION) {
String name = fn.getFunctionName();
@ -77,15 +78,21 @@ public class NodeTransformer {
// function to initialize a local variable of the
// function's name to the function value.
vars.addLocal(name);
Node block = tree.getLastChild();
Node setFn = new Node(TokenStream.POP,
new Node(TokenStream.SETVAR,
Node.newString(name),
new Node(TokenStream.PRIMARY,
TokenStream.THISFN)));
block.addChildrenToFront(setFn);
stmts.addChildrenToFront(setFn);
}
}
// Add return to end if needed.
Node lastStmt = stmts.getLastChild();
if (lastStmt == null ||
lastStmt.getType() != TokenStream.RETURN)
{
stmts.addChildToBack(new Node(TokenStream.RETURN));
}
fn.markVariableTableReady();
}
@ -101,17 +108,7 @@ public class NodeTransformer {
switch (type) {
case TokenStream.FUNCTION:
if (node == tree) {
// Add return to end if needed.
Node stmts = node.getLastChild();
Node lastStmt = stmts.getLastChild();
if (lastStmt == null ||
lastStmt.getType() != TokenStream.RETURN)
{
stmts.addChildToBack(new Node(TokenStream.RETURN));
}
} else {
if (node != tree) {
FunctionNode
fnNode = (FunctionNode)node.getProp(Node.FUNCTION_PROP);
if (inFunction) {
@ -217,10 +214,8 @@ public class NodeTransformer {
break;
}
case TokenStream.NEWLOCAL : {
int localCount = tree.getIntProp(Node.LOCALCOUNT_PROP, 0);
tree.putIntProp(Node.LOCALCOUNT_PROP, localCount + 1);
}
case TokenStream.NEWLOCAL :
tree.incrementLocalCount();
break;
case TokenStream.LOOP:
@ -251,8 +246,7 @@ public class NodeTransformer {
loops.push(node);
loopEnds.push(finallytarget);
}
int localCount = tree.getIntProp(Node.LOCALCOUNT_PROP, 0);
tree.putIntProp(Node.LOCALCOUNT_PROP, localCount + 1);
tree.incrementLocalCount();
break;
}
@ -368,13 +362,13 @@ public class NodeTransformer {
case TokenStream.CALL:
if (isSpecialCallName(tree, node))
node.putProp(Node.SPECIALCALL_PROP, Boolean.TRUE);
node.putIntProp(Node.SPECIALCALL_PROP, 1);
visitCall(node, tree);
break;
case TokenStream.NEW:
if (isSpecialCallName(tree, node))
node.putProp(Node.SPECIALCALL_PROP, Boolean.TRUE);
node.putIntProp(Node.SPECIALCALL_PROP, 1);
visitNew(node, tree);
break;
@ -395,7 +389,7 @@ public class NodeTransformer {
Node right = node.getLastChild();
String string = left.getString();
String flags = (left != right) ? right.getString() : null;
int index = ((ScriptOrFnNode)tree).addRegexp(string, flags);
int index = tree.addRegexp(string, flags);
Node n = new Node(TokenStream.REGEXP);
iter.replaceCurrent(n);
n.putIntProp(Node.REGEXP_PROP, index);
@ -519,7 +513,7 @@ public class NodeTransformer {
}
boolean addGetThis = false;
if (left.getType() == TokenStream.NAME) {
VariableTable vars = getVariableTable(tree);
VariableTable vars = tree.getVariableTable();
String name = left.getString();
if (inFunction && vars.hasVariable(name) && !inWithStatement()) {
// call to a var. Transform to Call(GetVar("a"), b, c)
@ -606,10 +600,6 @@ public class NodeTransformer {
return false;
}
protected VariableTable getVariableTable(Node tree) {
return ((ScriptOrFnNode)tree).getVariableTable();
}
private void
reportError(String messageId, Object[] messageArgs, Node stmt,
ScriptOrFnNode tree)

View File

@ -43,8 +43,24 @@ public class ScriptOrFnNode extends Node {
public final VariableTable getVariableTable() { return variableTable; }
public final String getSourceName() { return sourceName; }
public final void setSourceName(String sourceName) {
// One time action
if (sourceName == null || this.sourceName != null)
Context.codeBug();
this.sourceName = sourceName;
}
public final String getEncodedSource() { return encodedSource; }
public final void setEncodedSource(String encodedSource) {
// One time action
if (encodedSource == null || this.encodedSource != null)
Context.codeBug();
this.encodedSource = encodedSource;
}
public final String getOriginalSource() { return originalSource; }
public final void setOriginalSource(String originalSource) {
@ -54,12 +70,22 @@ public class ScriptOrFnNode extends Node {
this.originalSource = originalSource;
}
public final String getSourceName() { return sourceName; }
public final int getBaseLineno() { return baseLineno; }
public final void setBaseLineno(int lineno) {
// One time action
if (lineno < 0 || baseLineno >= 0) Context.codeBug();
baseLineno = lineno;
}
public final int getEndLineno() { return baseLineno; }
public final void setEndLineno(int lineno) {
// One time action
if (lineno < 0 || endLineno >= 0) Context.codeBug();
endLineno = lineno;
}
public final int getFunctionCount() {
if (functions == null) { return 0; }
return functions.size();
@ -102,13 +128,20 @@ public class ScriptOrFnNode extends Node {
return (regexps.size() - 2) / 2;
}
public final int getLocalCount() { return localCount; }
public final void incrementLocalCount() {
++localCount;
}
VariableTable variableTable;
String encodedSource;
private String encodedSource;
private String originalSource;
String sourceName;
int baseLineno = -1;
int endLineno = -1;
private String sourceName;
private int baseLineno = -1;
private int endLineno = -1;
private ObjArray functions;
private ObjArray regexps;
private int localCount;
}

View File

@ -1358,7 +1358,7 @@ public class Codegen extends Interpreter {
// These locals are to be pre-allocated since they need function scope.
// They are primarily used by the exception handling mechanism
int localCount = tree.getIntProp(Node.LOCALCOUNT_PROP, 0);
int localCount = tree.getLocalCount();
if (localCount != 0) {
itsLocalAllocationBase = (short)(argsLocal + 1);
for (int i = 0; i < localCount; i++) {
@ -1926,7 +1926,7 @@ public class Codegen extends Interpreter {
if (firstArgDone && (type == TokenStream.NEW))
constructArgArray(childCount - argSkipCount);
boolean isSpecialCall = node.getProp(Node.SPECIALCALL_PROP) != null;
boolean isSpecialCall = node.getIntProp(Node.SPECIALCALL_PROP, 0) != 0;
boolean isSimpleCall = false;
String simpleCallName = null;
if (!firstArgDone && type != TokenStream.NEW) {

View File

@ -121,11 +121,6 @@ class OptFunctionNode extends FunctionNode {
itsContainsCalls = true;
}
void incrementLocalCount() {
int localCount = getIntProp(Node.LOCALCOUNT_PROP, 0);
putIntProp(Node.LOCALCOUNT_PROP, localCount + 1);
}
int getVarCount() {
return optVars.length;
}