Skip to content

Commit 1ea3276

Browse files
Minor details
1 parent 848cfe3 commit 1ea3276

File tree

3 files changed

+31
-21
lines changed

3 files changed

+31
-21
lines changed

registervm/src/main/java/com/compilerprogramming/ezlang/bytecode/BasicBlock.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
public class BasicBlock {
88
public final int bid;
99
public final boolean loopHead;
10-
public List<BasicBlock> successors = new ArrayList<>(); // successors
11-
List<BasicBlock> predecessors = new ArrayList<>();
10+
public final List<BasicBlock> successors = new ArrayList<>(); // successors
11+
public final List<BasicBlock> predecessors = new ArrayList<>();
1212
public final List<Instruction> instructions = new ArrayList<>();
1313

1414
public BasicBlock(int bid, boolean loopHead) {
@@ -25,7 +25,6 @@ public void addSuccessor(BasicBlock successor) {
2525
successors.add(successor);
2626
successor.predecessors.add(this);
2727
}
28-
2928
public static StringBuilder toStr(StringBuilder sb, BasicBlock bb, BitSet visited)
3029
{
3130
if (visited.get(bb.bid))
@@ -41,5 +40,4 @@ public static StringBuilder toStr(StringBuilder sb, BasicBlock bb, BitSet visite
4140
}
4241
return sb;
4342
}
44-
4543
}

registervm/src/main/java/com/compilerprogramming/ezlang/bytecode/FunctionBuilder.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,22 @@
1111

1212
public class FunctionBuilder {
1313

14-
BasicBlock entry;
15-
BasicBlock exit;
16-
int bid = 0;
17-
BasicBlock currentBlock;
18-
BasicBlock currentBreakTarget;
19-
BasicBlock currentContinueTarget;
20-
21-
List<Operand> virtualStack = new ArrayList<>();
14+
public BasicBlock entry;
15+
public BasicBlock exit;
16+
private int bid = 0;
17+
private BasicBlock currentBlock;
18+
private BasicBlock currentBreakTarget;
19+
private BasicBlock currentContinueTarget;
20+
21+
/**
22+
* We essentially do a form of abstract interpretation as we generate
23+
* the bytecode instructions. For this purpose we use a virtual operand stack.
24+
*
25+
* This is similar to the technique described in
26+
* Dynamic Optimization through the use of Automatic Runtime Specialization
27+
* by John Whaley
28+
*/
29+
private List<Operand> virtualStack = new ArrayList<>();
2230

2331
public FunctionBuilder(Symbol.FunctionTypeSymbol functionSymbol) {
2432
AST.FuncDecl funcDecl = (AST.FuncDecl) functionSymbol.functionDecl;
@@ -66,7 +74,7 @@ private void compileReturn(AST.ReturnStmt returnStmt) {
6674
if (indexed)
6775
codeIndexedLoad();
6876
if (virtualStack.size() == 1)
69-
code(new Instruction.Move(pop(), new Operand.ReturnRegisterOperand(0)));
77+
code(new Instruction.Move(pop(), new Operand.ReturnRegisterOperand()));
7078
else if (virtualStack.size() > 1)
7179
throw new CompilerException("Virtual stack has more than one item at return");
7280
}

registervm/src/main/java/com/compilerprogramming/ezlang/bytecode/Operand.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,22 @@ public String toString() {
3737
}
3838
}
3939

40+
/**
41+
* Represents the return register, which is the location where
42+
* the caller will expect to see any return value. The VM must map
43+
* this to appropriate location.
44+
*/
4045
public static class ReturnRegisterOperand extends Operand {
41-
public final int regnum;
42-
public ReturnRegisterOperand(int regnum) {
43-
this.regnum = regnum;
44-
}
46+
public ReturnRegisterOperand() {}
4547
@Override
46-
public String toString() {
47-
return "R" + regnum;
48-
}
48+
public String toString() { return "RET"; }
4949
}
5050

51+
/**
52+
* Represents a temp register, maps to a location on the
53+
* virtual stack. Temps start at offset 0, but this is a relative
54+
* register number from start of temp area.
55+
*/
5156
public static class TempRegisterOperand extends Operand {
5257
public final int regnum;
5358
public TempRegisterOperand(int regnum) {
@@ -66,7 +71,6 @@ public LoadIndexedOperand(Operand arrayOperand, Operand indexOperand) {
6671
this.arrayOperand = arrayOperand;
6772
this.indexOperand = indexOperand;
6873
}
69-
7074
@Override
7175
public String toString() {
7276
return arrayOperand + "[" + indexOperand + "]";

0 commit comments

Comments
 (0)