Code Generation
2 April 2015 OSU CSE 1
Code Generation OSU CSE 2 April 2015 BL Compiler Structure Code - - PDF document
1 Code Generation OSU CSE 2 April 2015 BL Compiler Structure Code Tokenizer Parser Generator string of string of abstract string of characters tokens integers program (source code) (words) (object code) The code generator
2 April 2015 OSU CSE 1
2 April 2015 OSU CSE 2
2 April 2015 OSU CSE 3
2 April 2015 OSU CSE 4
2 April 2015 OSU CSE 5
2 April 2015 OSU CSE 6
2 April 2015 OSU CSE 7
2 April 2015 OSU CSE 8
2 April 2015 OSU CSE 9
public static void executeProgram(Program p) { Statement tmpBody = p.newBody(); Map<String, Statement> tmpContext = p.newContext(); Statement body = p.replaceBody(tmpBody); Map<String, Statement> context = p.replaceContext(tmpContext); executeStatement(body, context); p.replaceBody(body); p.replaceContext(context); }
2 April 2015 OSU CSE 10
2 April 2015 OSU CSE 11
BLOCK IF condition WHILE condition CALL instruction
IF_ELSE condition
public static void executeStatement(Statement s, Map<String, Statement> context) { switch (s.kind()) { case BLOCK: { for (int i = 0; i < s.lengthOfBlock(); i++) { Statement ns = s.removeFromBlock(i); executeStatement(ns, context); s.addToBlock(i, ns); } break; } case IF: {...} ... }
2 April 2015 OSU CSE 12
2 April 2015 OSU CSE 13
2 April 2015 OSU CSE 14
2 April 2015 OSU CSE 15
2 April 2015 OSU CSE 16
2 April 2015 OSU CSE 17
2 April 2015 OSU CSE 18
2 April 2015 OSU CSE 19
2 April 2015 OSU CSE 20
2 April 2015 OSU CSE 21
2 April 2015 OSU CSE 22
IF_ELSE NEXT_IS_WALL BLOCK CALL turnleft BLOCK CALL move
Loc Instruction (symbolic name)
JUMP_IF_NOT_NEXT_IS_WALL 1 5 2 TURNLEFT 3 JUMP 4 6 5 MOVE 6 ...
2 April 2015 OSU CSE 23
IF_ELSE NEXT_IS_WALL BLOCK CALL turnleft BLOCK CALL move
Loc Instruction (“byte code”)
10 1 5 2 1 3 6 4 6 5 6 ...
2 April 2015 OSU CSE 24
2 April 2015 OSU CSE 25
2 April 2015 OSU CSE 26
2 April 2015 OSU CSE 27
2 April 2015 OSU CSE 28
2 April 2015 OSU CSE 29
2 April 2015 OSU CSE 30
2 April 2015 OSU CSE 31
2 April 2015 OSU CSE 32
2 April 2015 OSU CSE 33
2 April 2015 OSU CSE 34
2 April 2015 OSU CSE 35
2 April 2015 OSU CSE 36
2 April 2015 OSU CSE 37
2 April 2015 OSU CSE 38
2 April 2015 OSU CSE 39
Loc Instruction (symbolic name)
k JUMP_IF_NOT_condition k+1 k+n+2 k+2 block (n instructions) ... k+n+1 k+n+2 ...
2 April 2015 OSU CSE 40
Loc Instruction (symbolic name)
k JUMP_IF_NOT_condition k+1 k+n1+4 k+2 block1 (n1 instructions) ... k+n1+2 JUMP k+n1+3 k+n1+n2+4 k+n1+4 block2 (n2 instructions) ... k+n1+n2+4 ...
2 April 2015 OSU CSE 41
Loc Instruction (symbolic name)
k JUMP_IF_NOT_condition k+1 k+n+4 k+2 block (n instructions) ... k+n+2 JUMP k+n+3 k k+n+4 ...
2 April 2015 OSU CSE 42
Loc Instruction (symbolic name)
k MOVE
Loc Instruction (symbolic name)
k TURNLEFT
2 April 2015 OSU CSE 43
Loc Instruction (symbolic name)
k block (of n instructions) ... k+n-1 k+n ...
2 April 2015 OSU CSE 44
Loc Instruction (symbolic name)
k block (of n instructions) ... k+n-1 k+n ...
2 April 2015 OSU CSE 45
Loc Instruction (symbolic name)
k block (of n instructions) ... k+n-1 k+n ...
2 April 2015 OSU CSE 46
Loc Instruction (symbolic name)
k block (of n instructions) ... k+n-1 k+n ...
2 April 2015 OSU CSE 47
2 April 2015 OSU CSE 48
/** * BugsWorld VM instructions and "byte codes". */ enum Instruction { MOVE(0), TURNLEFT(1), ... ; ... }
2 April 2015 OSU CSE 49
enum Instruction { MOVE(0), TURNLEFT(1), ... ; private int blByteCode; private Instruction(int code) { this.blByteCode = code; } public int byteCode() { return this.blByteCode; } }
2 April 2015 OSU CSE 50
enum Instruction { MOVE(0), TURNLEFT(1), ... ; private int blByteCode; private Instruction(int code) { this.blByteCode = code; } public int byteCode() { return this.blByteCode; } }
2 April 2015 OSU CSE 51
enum Instruction { MOVE(0), TURNLEFT(1), ... ; private int blByteCode; private Instruction(int code) { this.blByteCode = code; } public int byteCode() { return this.blByteCode; } }
2 April 2015 OSU CSE 52
enum Instruction { MOVE(0), TURNLEFT(1), ... ; private int blByteCode; private Instruction(int code) { this.blByteCode = code; } public int byteCode() { return this.blByteCode; } }
2 April 2015 OSU CSE 53
2 April 2015 OSU CSE 54
– http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html
2 April 2015 OSU CSE 55