Skip to content

Commit

Permalink
Reduce number of Symbols created and remove blocks from literal array…
Browse files Browse the repository at this point in the history
… after inlining (#211)
  • Loading branch information
smarr authored Aug 12, 2024
2 parents 04e0d8e + f634f99 commit bbb226d
Show file tree
Hide file tree
Showing 23 changed files with 221 additions and 195 deletions.
43 changes: 23 additions & 20 deletions src/trufflesom/src/trufflesom/compiler/MethodGenerationContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@

package trufflesom.compiler;

import static trufflesom.vm.SymbolTable.strBlockSelf;
import static trufflesom.vm.SymbolTable.strFrameOnStack;
import static trufflesom.vm.SymbolTable.strSelf;
import static trufflesom.vm.SymbolTable.symBlockSelf;
import static trufflesom.vm.SymbolTable.symFrameOnStack;
import static trufflesom.vm.SymbolTable.symSelf;
Expand Down Expand Up @@ -81,8 +84,8 @@ public class MethodGenerationContext
protected boolean accessesVariablesOfOuterScope;
protected boolean accessesLocalsOfOuterScope;

protected final LinkedHashMap<SSymbol, Argument> arguments;
protected final LinkedHashMap<SSymbol, Local> locals;
protected final LinkedHashMap<String, Argument> arguments;
protected final LinkedHashMap<String, Local> locals;

private Internal frameOnStack;

Expand Down Expand Up @@ -169,11 +172,11 @@ public Internal getFrameOnStackMarker(final long coord) {

if (frameOnStack == null) {
assert needsToCatchNonLocalReturn;
assert !locals.containsKey(symFrameOnStack);
assert !locals.containsKey(strFrameOnStack);

int index = locals.size();
frameOnStack = new Internal(symFrameOnStack, coord, index);
locals.put(symFrameOnStack, frameOnStack);
frameOnStack = new Internal(strFrameOnStack, coord, index);
locals.put(strFrameOnStack, frameOnStack);
currentScope.addVariable(frameOnStack);
}
return frameOnStack;
Expand Down Expand Up @@ -278,8 +281,8 @@ public void setSignature(final SSymbol sig) {
signature = sig;
}

private Argument addArgument(final SSymbol arg, final long coord) {
if ((symSelf == arg || symBlockSelf == arg) && arguments.size() > 0) {
private Argument addArgument(final String arg, final long coord) {
if ((strSelf.equals(arg) || strBlockSelf.equals(arg)) && arguments.size() > 0) {
throw new IllegalStateException(
"The self argument always has to be the first argument of a method");
}
Expand All @@ -293,23 +296,23 @@ private Argument addArgument(final SSymbol arg, final long coord) {
return argument;
}

public Argument addArgumentIfAbsent(final SSymbol arg, final long coord) {
public Argument addArgumentIfAbsent(final String arg, final long coord) {
if (arguments.containsKey(arg)) {
return arguments.get(arg);
}

return addArgument(arg, coord);
}

public boolean hasLocal(final SSymbol local) {
public boolean hasLocal(final String local) {
return locals.containsKey(local);
}

public int getNumberOfLocals() {
return locals.size();
}

public Local addLocal(final SSymbol local, final long coord) {
public Local addLocal(final String local, final long coord) {
int index = locals.size();
Local l = new Local(local, coord, index);
assert !locals.containsKey(local);
Expand All @@ -321,7 +324,7 @@ public Local addLocal(final SSymbol local, final long coord) {
return l;
}

private Local addLocalAndUpdateScope(final SSymbol name, final long coord) {
private Local addLocalAndUpdateScope(final String name, final long coord) {
Local l = addLocal(name, coord);
currentScope.addVariable(l);
return l;
Expand All @@ -345,7 +348,7 @@ private int getOuterSelfContextLevel() {
return level;
}

public int getContextLevel(final SSymbol varName) {
public int getContextLevel(final String varName) {
if (locals.containsKey(varName) || arguments.containsKey(varName)) {
return 0;
}
Expand Down Expand Up @@ -373,7 +376,7 @@ public Local getEmbeddedLocal(final SSymbol embeddedName) {
return locals.get(embeddedName);
}

protected Variable getVariable(final SSymbol varName) {
protected Variable getVariable(final String varName) {
if (locals.containsKey(varName)) {
return locals.get(varName);
}
Expand Down Expand Up @@ -404,7 +407,7 @@ public ExpressionNode getLocalWriteNode(final Variable variable,
return variable.getWriteNode(getContextLevel(variable), valExpr, coord);
}

protected Local getLocal(final SSymbol varName) {
protected Local getLocal(final String varName) {
if (locals.containsKey(varName)) {
return locals.get(varName);
}
Expand All @@ -428,7 +431,7 @@ public ReturnNonLocalNode getNonLocalReturn(final ExpressionNode expr,
}

private ExpressionNode getSelfRead(final long coord) {
return getVariable(symSelf).getReadNode(getContextLevel(symSelf), coord);
return getVariable(strSelf).getReadNode(getContextLevel(strSelf), coord);
}

public FieldReadNode getObjectFieldRead(final SSymbol fieldName,
Expand Down Expand Up @@ -457,7 +460,7 @@ public FieldNode getObjectFieldWrite(final SSymbol fieldName, final ExpressionNo
return FieldWriteNodeGen.create(fieldIndex, self, exp).initialize(coord);
}

protected void addLocal(final Local l, final SSymbol name) {
protected void addLocal(final Local l, final String name) {
assert !locals.containsKey(name);
locals.put(name, l);
currentScope.addVariable(l);
Expand All @@ -468,7 +471,7 @@ public void mergeIntoScope(final LexicalScope scope, final SMethod toBeInlined)
int slotIndex = locals.size();
Local l = v.splitToMergeIntoOuterScope(slotIndex);
if (l != null) { // can happen for instance for the block self, which we omit
SSymbol name = l.getQualifiedName(holderGenc.getSource());
String name = l.makeQualifiedName(holderGenc.getSource());
addLocal(l, name);
}
}
Expand Down Expand Up @@ -503,13 +506,13 @@ public Variable introduceTempForInlinedVersion(
if (blockOrVal instanceof BlockNode) {
Argument[] args = ((BlockNode) blockOrVal).getArguments();
assert args.length == 2;
loopIdx = getLocal(args[1].getQualifiedName(holderGenc.getSource()));
loopIdx = getLocal(args[1].makeQualifiedName(holderGenc.getSource()));
} else {
// if it is a literal, we still need a memory location for counting, so,
// add a synthetic local
loopIdx = addLocalAndUpdateScope(symbolFor(
loopIdx = addLocalAndUpdateScope(
"!i" + SourceCoordinate.getLocationQualifier(
holderGenc.getSource(), coord)),
holderGenc.getSource(), coord),
coord);
}
return loopIdx;
Expand Down
33 changes: 18 additions & 15 deletions src/trufflesom/src/trufflesom/compiler/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
import static trufflesom.compiler.Symbol.STString;
import static trufflesom.compiler.Symbol.Separator;
import static trufflesom.compiler.Symbol.Star;
import static trufflesom.vm.SymbolTable.strBlockSelf;
import static trufflesom.vm.SymbolTable.strSelf;
import static trufflesom.vm.SymbolTable.symBlockSelf;
import static trufflesom.vm.SymbolTable.symNil;
import static trufflesom.vm.SymbolTable.symObject;
Expand Down Expand Up @@ -360,7 +362,7 @@ private boolean expectOneOf(final List<Symbol> ss) throws ParseError {
}

protected SSymbol field() throws ParseError {
return identifier();
return symbolFor(identifier());
}

private void instanceFields(final ClassGenerationContext cgenc)
Expand Down Expand Up @@ -437,7 +439,7 @@ public ExpressionNode nestedBlock(final MGenC mgenc) throws ProgramDefinitionErr
expect(NewBlock);
int coord = getStartIndex();

mgenc.addArgumentIfAbsent(symBlockSelf, getEmptyCoord());
mgenc.addArgumentIfAbsent(strBlockSelf, getEmptyCoord());

if (sym == Colon) {
blockPattern(mgenc);
Expand All @@ -456,7 +458,7 @@ public ExpressionNode nestedBlock(final MGenC mgenc) throws ProgramDefinitionErr

private void pattern(final MGenC mgenc) throws ProgramDefinitionError {
assert Universe.selfSource != null;
mgenc.addArgumentIfAbsent(symSelf, Universe.selfCoord);
mgenc.addArgumentIfAbsent(strSelf, Universe.selfCoord);
switch (sym) {
case Identifier:
case Primitive:
Expand Down Expand Up @@ -491,11 +493,11 @@ protected void keywordPattern(final MGenC mgenc) throws ProgramDefinitionError {
}

protected SSymbol unarySelector() throws ParseError {
return identifier();
return symbolFor(identifier());
}

protected SSymbol unarySendSelector() throws ParseError {
return identifier();
return symbolFor(identifier());
}

private SSymbol binarySelectorImpl() throws ParseError {
Expand All @@ -518,13 +520,13 @@ protected SSymbol binarySendSelector() throws ParseError {
return binarySelectorImpl();
}

private SSymbol identifier() throws ParseError {
private String identifier() throws ParseError {
String s = new String(text);
boolean isPrimitive = accept(Primitive);
if (!isPrimitive) {
expect(Identifier);
}
return symbolFor(s);
return s;
}

protected String keyword() throws ParseError {
Expand All @@ -545,9 +547,9 @@ protected Argument argument(final MGenC mgenc) throws ProgramDefinitionError {

protected Local local(final MGenC mgenc) throws ProgramDefinitionError {
int coord = getStartIndex();
SSymbol var = variable();
String var = variable();
if (mgenc.hasLocal(var)) {
throw new ParseError("Declared the variable " + var.getString() + " multiple times.",
throw new ParseError("Declared the variable " + var + " multiple times.",
null, this);
}
return mgenc.addLocal(var, getCoordWithLength(coord));
Expand Down Expand Up @@ -589,13 +591,13 @@ protected ExpressionNode expression(final MGenC mgenc)

protected abstract ExpressionNode evaluation(MGenC mgenc) throws ProgramDefinitionError;

protected SSymbol assignment() throws ParseError {
SSymbol v = variable();
protected String assignment() throws ParseError {
String v = variable();
expect(Assign);
return v;
}

protected SSymbol variable() throws ParseError {
protected String variable() throws ParseError {
return identifier();
}

Expand Down Expand Up @@ -703,23 +705,24 @@ private void blockArguments(final MGenC mgenc) throws ProgramDefinitionError {
} while (sym == Colon);
}

protected ExpressionNode variableRead(final MGenC mgenc, final SSymbol variableName,
protected ExpressionNode variableRead(final MGenC mgenc, final String variableName,
final long coord) {
// now look up first local variables, or method arguments
Variable variable = mgenc.getVariable(variableName);
if (variable != null) {
return mgenc.getLocalReadNode(variable, coord);
}

SSymbol varSym = symbolFor(variableName);
// then object fields
FieldReadNode fieldRead = mgenc.getObjectFieldRead(variableName, coord);
FieldReadNode fieldRead = mgenc.getObjectFieldRead(varSym, coord);

if (fieldRead != null) {
return fieldRead;
}

// and finally assume it is a global
return GlobalNode.create(variableName, mgenc).initialize(coord);
return GlobalNode.create(varSym, mgenc).initialize(coord);
}

private void getSymbolFromLexer() {
Expand Down
16 changes: 9 additions & 7 deletions src/trufflesom/src/trufflesom/compiler/ParserAst.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import static trufflesom.compiler.Symbol.OperatorSequence;
import static trufflesom.compiler.Symbol.Period;
import static trufflesom.compiler.Symbol.Pound;
import static trufflesom.vm.SymbolTable.strSelf;
import static trufflesom.vm.SymbolTable.strSuper;
import static trufflesom.vm.SymbolTable.symNil;
import static trufflesom.vm.SymbolTable.symSelf;
import static trufflesom.vm.SymbolTable.symSuper;
Expand Down Expand Up @@ -92,7 +94,7 @@ protected ExpressionNode blockBody(final MethodGenerationContext mgenc,
} else if (sym == EndTerm) {
// the end of the method has been found (EndTerm) - make it implicitly return "self"
ExpressionNode self =
variableRead(mgenc, symSelf, getCoordWithLength(getStartIndex()));
variableRead(mgenc, strSelf, getCoordWithLength(getStartIndex()));
expressions.add(self);
return createSequenceNode(coord, expressions);
}
Expand Down Expand Up @@ -148,7 +150,7 @@ private ExpressionNode assignments(final MethodGenerationContext mgenc)
" fields, but found instead a %(found)s",
Symbol.Identifier, this);
}
SSymbol variable = assignment();
String variable = assignment();

peekForNextSymbolFromLexer();

Expand All @@ -163,14 +165,14 @@ private ExpressionNode assignments(final MethodGenerationContext mgenc)
}

protected ExpressionNode variableWrite(final MethodGenerationContext mgenc,
final SSymbol variableName, final ExpressionNode exp, final long coord)
final String variableName, final ExpressionNode exp, final long coord)
throws ParseError {
Variable variable = mgenc.getVariable(variableName);
if (variable != null) {
return mgenc.getLocalWriteNode(variable, exp, coord);
}

FieldNode fieldWrite = mgenc.getObjectFieldWrite(variableName, exp, coord);
FieldNode fieldWrite = mgenc.getObjectFieldWrite(symbolFor(variableName), exp, coord);

if (fieldWrite != null) {
return fieldWrite;
Expand Down Expand Up @@ -452,13 +454,13 @@ private ExpressionNode primary(final MethodGenerationContext mgenc)
case Identifier:
case Primitive: {
int coord = getStartIndex();
SSymbol v = variable();
String v = variable();

if (v == symSuper) {
if (v.equals(strSuper)) {
assert !superSend : "Since super is consumed directly, it should never be true here";
superSend = true;
// sends to super push self as the receiver
v = symSelf;
v = strSelf;
}

return variableRead(mgenc, v, getCoordWithLength(coord));
Expand Down
Loading

0 comments on commit bbb226d

Please sign in to comment.