Code Generation - Computer Science and...
Transcript of Code Generation - Computer Science and...
![Page 1: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/1.jpg)
Code Generation
22 November 2019 OSU CSE 1
![Page 2: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/2.jpg)
BL Compiler Structure
22 November 2019 OSU CSE 2
CodeGeneratorParserTokenizer
string ofcharacters
(source code)
string oftokens
(“words”)
abstractprogram
string ofintegers
(object code)
The code generator is the last step.
![Page 3: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/3.jpg)
Executing a BL Program
• There are two qualitatively different ways one might execute a BL program, given a value of type Program that has been constructed from BL source code:– Interpret the Program directly– Compile the Program into object code
(“byte code”) that is executed by a virtual machine
22 November 2019 OSU CSE 3
![Page 4: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/4.jpg)
Executing a BL Program
• There are two qualitatively different ways one might execute a BL program, given a value of type Program that has been constructed from its source code:– Interpret the Program directly– Compile the Program into object code
(“byte code”) that is executed by a virtual machine
22 November 2019 OSU CSE 4
This is what the BL compiler will actually do; and this is how Java itself
works (recall the JVM and its “byte codes”).
![Page 5: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/5.jpg)
Executing a BL Program
• There are two qualitatively different ways one might execute a BL program, given a value of type Program that has been constructed from its source code:– Interpret the Program directly– Compile the Program into object code
(“byte code”) that is executed by a virtual machine
22 November 2019 OSU CSE 5
Let’s first see how this might be done ...
![Page 6: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/6.jpg)
Time Lines of Execution
• Directly interpreting a Program:
• Compiling and then executing a Program:
22 November 2019 OSU CSE 6
Tokenize Parse Execute by interpreting the Program directly
Tokenize Parse Generate code
Execute by interpretinggenerated code on VM
![Page 7: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/7.jpg)
Time Lines of Execution
• Directly interpreting a Program:
• Compiling and then executing a Program:
22 November 2019 OSU CSE 7
Tokenize Parse Execute by interpreting the Program directly
Tokenize Parse Generate code
Execute by interpretinggenerated code on VM
At this point, you have a Program value to use.
![Page 8: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/8.jpg)
Time Lines of Execution
• Directly interpreting a Program:
• Compiling and then executing a Program:
22 November 2019 OSU CSE 8
Tokenize Parse Execute by interpreting the Program directly
Tokenize Parse Generate code
Execute by interpretinggenerated code on VM
“Execution-time” or“run-time” means here.
“Execution-time” or“run-time” means here.
![Page 9: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/9.jpg)
Interpreting a Program
• The structure of a Program and, within it, the recursive structure of a Statement, directly dictate how to execute a Programby interpretation
• Without contracts and other details, the following few slides indicate the structure of such code
22 November 2019 OSU CSE 9
![Page 10: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/10.jpg)
executeProgram
public static void executeProgram(Program p) {Statement body = p.newBody();Map<String, Statement> context = p.newContext();p.swapBody(body);p.swapContext(context);executeStatement(body, context);p.swapBody(body);p.swapContext(context);
}
22 November 2019 OSU CSE 10
![Page 11: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/11.jpg)
22 November 2019 OSU CSE 11
BLOCK
IFcondition
WHILEcondition
CALLinstruction
s =
IF_ELSEcondition
![Page 12: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/12.jpg)
executeStatement
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: {...}...
}
22 November 2019 OSU CSE 12
It’s recursive just like everything else to do with Statement; context is needed for case CALL.
![Page 13: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/13.jpg)
executeStatement
22 November 2019 OSU CSE 13
• Non-BLOCK cases are different in kind:– For IF, IF_ELSE, and WHILE, you need to
decide whether the condition being tested as the BL program executes is (now) true or false
• This requires a call to some method that knows the state of BugsWorld, i.e., what the bug “sees”
– For CALL, you need to execute a primitive instruction, e.g., MOVE, INFECT, etc.
• This requires a call to some method that updates the state of BugsWorld
![Page 14: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/14.jpg)
The State of BugsWorld
22 November 2019 OSU CSE 14
![Page 15: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/15.jpg)
The State of BugsWorld
22 November 2019 OSU CSE 15
For example, when executing this bug’s Program in this state, next-is-empty is true.
![Page 16: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/16.jpg)
Where Is The State of BugsWorld?
22 November 2019 OSU CSE 16
The server knows about all the bugs, and can report to a client what a particular bug “sees”.
A client executes a particular bug’s
program, and tells the server to execute primitive
instructions.
![Page 17: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/17.jpg)
executeStatement
22 November 2019 OSU CSE 17
• Surprisingly, perhaps, executing a call to a user-defined instruction is straightforward:– You simply make a recursive call to executeStatement and pass it the body of that user-defined instruction, which is obtained from the context
![Page 18: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/18.jpg)
Compiling a Program
• As noted earlier, we are instead going to compile a Program, and the last step for a BL compiler is to generate code
• The structure of a program to do this is similar to that of an interpreter of a Program, except that it processes each Statement once rather than once every time it happens to be executed at run-time
22 November 2019 OSU CSE 18
![Page 19: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/19.jpg)
Code Generation
• Code generation is translating a Program to a linear (non-nested) structure, i.e., to a string of low-level instructions or “byte codes” of a BL virtual machine that can do the following:– Update the state of BugsWorld– “Jump around” in the string to execute the
right “byte codes” under the right conditions, depending on the state of BugsWorld
22 November 2019 OSU CSE 19
![Page 20: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/20.jpg)
Code Generation
• Code generation is translating a Program to a linear (non-nested) structure, i.e., to a string of low-level instructions or “byte codes” of a BL virtual machine that can do the following:– Update the state of BugsWorld– “Jump around” in the string to execute the
right “byte codes” under the right conditions, depending on the state of BugsWorld
22 November 2019 OSU CSE 20
Primitive BL instructions are translated into these “byte codes”.
![Page 21: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/21.jpg)
Code Generation
• Code generation is translating a Program to a linear (non-nested) structure, i.e., to a string of low-level instructions or “byte codes” of a BL virtual machine that can do the following:– Update the state of BugsWorld– “Jump around” in the string to execute the
right “byte codes” under the right conditions, depending on the state of BugsWorld
22 November 2019 OSU CSE 21
BL control constructs that check conditions
are translated into these “byte codes”.
![Page 22: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/22.jpg)
Example StatementIF next-is-wall THEN
turnleftELSE
moveEND IF
22 November 2019 OSU CSE 22
IF_ELSENEXT_IS_WALL
BLOCK
CALLturnleft
BLOCK
CALLmove
Loc Instruction (symbolic name)0 JUMP_IF_NOT_NEXT_IS_WALL
1 5
2 TURNLEFT
3 JUMP
4 6
5 MOVE
6 ...
![Page 23: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/23.jpg)
Example StatementIF next-is-wall THEN
turnleftELSE
moveEND IF
22 November 2019 OSU CSE 23
IF_ELSENEXT_IS_WALL
BLOCK
CALLturnleft
BLOCK
CALLmove
Loc Instruction (“byte code”)0 9
1 5
2 1
3 6
4 6
5 0
6 ...
![Page 24: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/24.jpg)
BugsWorld Virtual Machine
• The virtual machine for BugsWorld has three main features:– Memory– Instruction set– Program counter
22 November 2019 OSU CSE 24
![Page 25: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/25.jpg)
BugsWorld Virtual Machine
• The virtual machine for BugsWorld has three main features:– Memory– Instruction set– Program counter
22 November 2019 OSU CSE 25
A string of integers that contains the “byte codes”
generated from a Program.
![Page 26: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/26.jpg)
BugsWorld Virtual Machine
• The virtual machine for BugsWorld has three main features:– Memory– Instruction set– Program counter
22 November 2019 OSU CSE 26
A finite set of integers that are the “byte codes” for the primitive instructions of the
BugsWorld VM.
![Page 27: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/27.jpg)
BugsWorld Virtual Machine
• The virtual machine for BugsWorld has three main features:– Memory– Instruction set– Program counter
22 November 2019 OSU CSE 27
Each instruction is given a symbolic name here, for ease of reading, but the VM knows
only about integer “byte codes”.
![Page 28: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/28.jpg)
BugsWorld Virtual Machine
• The virtual machine for BugsWorld has three main features:– Memory– Instruction set– Program counter
22 November 2019 OSU CSE 28
An integer that designates the location/position/address in memory of the “byte code” to
be executed next.
![Page 29: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/29.jpg)
BugsWorld Virtual Machine
• The virtual machine for BugsWorld has three main features:– Memory– Instruction set– Program counter
22 November 2019 OSU CSE 29
Normal execution increments the program counter by 1 or 2
after each instruction, so execution proceeds sequentially.
![Page 30: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/30.jpg)
Instruction Set
• The instruction set, or target language, for code generation has two types of instructions:– Primitive instructions– Jump instructions
22 November 2019 OSU CSE 30
![Page 31: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/31.jpg)
Instruction Set
• The instruction set, or target language, for code generation has two types of instructions:– Primitive instructions– Jump instructions
22 November 2019 OSU CSE 31
Each of these occupies one memory location.
![Page 32: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/32.jpg)
Instruction Set
• The instruction set, or target language, for code generation has two types of instructions:– Primitive instructions– Jump instructions
22 November 2019 OSU CSE 32
Each of these occupies two memory locations: the second one is the
location to jump to.
![Page 33: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/33.jpg)
Primitive Instructions• MOVE (0)• TURNLEFT (1)• TURNRIGHT (2)• INFECT (3)• SKIP (4)• HALT (5)
22 November 2019 OSU CSE 33
![Page 34: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/34.jpg)
Primitive Instructions• MOVE (0)• TURNLEFT (1)• TURNRIGHT (2)• INFECT (3)• SKIP (4)• HALT (5)
22 November 2019 OSU CSE 34
This is the “byte code” corresponding to the
symbolic name for each instruction code.
![Page 35: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/35.jpg)
Primitive Instructions• MOVE (0)• TURNLEFT (1)• TURNRIGHT (2)• INFECT (3)• SKIP (4)• HALT (5)
22 November 2019 OSU CSE 35
This instruction halts program execution, and is the last instruction to be
executed.
![Page 36: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/36.jpg)
Jump Instructions• JUMP (6)• JUMP_IF_NOT_NEXT_IS_EMPTY (7)• JUMP_IF_NOT_NEXT_IS_NOT_EMPTY (8)• JUMP_IF_NOT_NEXT_IS_WALL (9)• JUMP_IF_NOT_NEXT_IS_NOT_WALL (10)• JUMP_IF_NOT_NEXT_IS_FRIEND (11)• JUMP_IF_NOT_NEXT_IS_NOT_FRIEND (12)• JUMP_IF_NOT_NEXT_IS_ENEMY (13)• JUMP_IF_NOT_NEXT_IS_NOT_ENEMY (14)• JUMP_IF_NOT_RANDOM (15)• JUMP_IF_NOT_TRUE (16)
22 November 2019 OSU CSE 36
![Page 37: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/37.jpg)
Jump Instructions• JUMP (6)• JUMP_IF_NOT_NEXT_IS_EMPTY (7)• JUMP_IF_NOT_NEXT_IS_NOT_EMPTY (8)• JUMP_IF_NOT_NEXT_IS_WALL (9)• JUMP_IF_NOT_NEXT_IS_NOT_WALL (10)• JUMP_IF_NOT_NEXT_IS_FRIEND (11)• JUMP_IF_NOT_NEXT_IS_NOT_FRIEND (12)• JUMP_IF_NOT_NEXT_IS_ENEMY (13)• JUMP_IF_NOT_NEXT_IS_NOT_ENEMY (14)• JUMP_IF_NOT_RANDOM (15)• JUMP_IF_NOT_TRUE (16)
22 November 2019 OSU CSE 37
This unconditional jump instruction causes the program counter to be set to the value in the memory location following
the JUMP code.
![Page 38: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/38.jpg)
Jump Instructions• JUMP (6)• JUMP_IF_NOT_NEXT_IS_EMPTY (7)• JUMP_IF_NOT_NEXT_IS_NOT_EMPTY (8)• JUMP_IF_NOT_NEXT_IS_WALL (9)• JUMP_IF_NOT_NEXT_IS_NOT_WALL (10)• JUMP_IF_NOT_NEXT_IS_FRIEND (11)• JUMP_IF_NOT_NEXT_IS_NOT_FRIEND (12)• JUMP_IF_NOT_NEXT_IS_ENEMY (13)• JUMP_IF_NOT_NEXT_IS_NOT_ENEMY (14)• JUMP_IF_NOT_RANDOM (15)• JUMP_IF_NOT_TRUE (16)
22 November 2019 OSU CSE 38
This conditional jump instruction causes the program counter to be
set to the value in the memory location following the instruction code iff it is not the case that the cell in front of the bug is a wall.
![Page 39: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/39.jpg)
Handling an IF Statement
IF condition THENblock
END IF
22 November 2019 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 ...
![Page 40: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/40.jpg)
Handling an IF_ELSE Statement
IF condition THENblock1
ELSEblock2
END IF
22 November 2019 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 ...
![Page 41: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/41.jpg)
Handling a WHILE Statement
WHILE condition DOblock
END WHILE
22 November 2019 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 ...
![Page 42: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/42.jpg)
Handling a CALL Statement
move
turnleft
(etc.)
22 November 2019 OSU CSE 42
Loc Instruction (symbolic name)k MOVE
Loc Instruction (symbolic name)k TURNLEFT
![Page 43: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/43.jpg)
Handling a CALL Statement
INSTRUCTIONmy-instruction ISblock
END my-instruction
my-instruction
22 November 2019 OSU CSE 43
Loc Instruction (symbolic name)k block (of n instructions)
...
k+n-1
k+n ...
![Page 44: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/44.jpg)
Handling a CALL Statement
INSTRUCTIONmy-instruction ISblock
END my-instruction
my-instruction
22 November 2019 OSU CSE 44
Loc Instruction (symbolic name)k block (of n instructions)
...
k+n-1
k+n ...
A call to my-instruction generates a block of “byte codes” for the
body of my-instruction.
![Page 45: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/45.jpg)
Handling a CALL Statement
INSTRUCTIONmy-instruction ISblock
END my-instruction
my-instruction
22 November 2019 OSU CSE 45
Loc Instruction (symbolic name)k block (of n instructions)
...
k+n-1
k+n ...
This way of generating code for a call to a user-defined
instruction is called in-lining.
![Page 46: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/46.jpg)
Handling a CALL Statement
INSTRUCTIONmy-instruction ISblock
END my-instruction
my-instruction
22 November 2019 OSU CSE 46
Loc Instruction (symbolic name)k block (of n instructions)
...
k+n-1
k+n ...
What would happen with in-lining if BL allowed recursion?
How else might you handle calls to user-defined instructions?
![Page 47: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/47.jpg)
Handling a BLOCK Statement
• The “byte codes” generated for individual Statements in a block (a sequence of Statements) are placed sequentially, one after the other, in memory
• Remember: at the end of the body block of the Program, there must be a HALTinstruction
22 November 2019 OSU CSE 47
![Page 48: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/48.jpg)
Aside: More On Java enum
• Recall: the Java enum construct allows you to give meaningful symbolic names to values for which you might instead have used arbitrary int constants
• This construct has some other valuable features that allow you to associate symbolic names (e.g., for VM instructions) with specific int constants (e.g., their “byte codes”)
22 November 2019 OSU CSE 48
![Page 49: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/49.jpg)
The Instruction Enum
• The interface Program contains this code:/*** BugsWorld VM instructions and "byte codes".*/
enum Instruction {MOVE(0), TURNLEFT(1), ... ;
...
}
22 November 2019 OSU CSE 49
plus 15 more
instructions
An instance variable,a constructor, and
an accessor method ...
![Page 50: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/50.jpg)
The Instruction Enum
• The interface Program contains this code:enum Instruction {
MOVE(0), TURNLEFT(1), ... ;
private int blByteCode;
private Instruction(int code) {this.blByteCode = code;
}
public int byteCode() {return this.blByteCode;
}}
22 November 2019 OSU CSE 50
![Page 51: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/51.jpg)
The Instruction Enum
• The interface Program contains this code:enum Instruction {
MOVE(0), TURNLEFT(1), ... ;
private int blByteCode;
private Instruction(int code) {this.blByteCode = code;
}
public int byteCode() {return this.blByteCode;
}}
22 November 2019 OSU CSE 51
Every Instruction(e.g., MOVE) has an intinstance variable called
blByteCode.
![Page 52: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/52.jpg)
The Instruction Enum
• The interface Program contains this code:enum Instruction {
MOVE(0), TURNLEFT(1), ... ;
private int blByteCode;
private Instruction(int code) {this.blByteCode = code;
}
public int byteCode() {return this.blByteCode;
}}
22 November 2019 OSU CSE 52
This constructor makes each Instruction’s
“argument” (in parens) the value of its associated
blByteCode.
![Page 53: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/53.jpg)
The Instruction Enum
• The interface Program contains this code:enum Instruction {
MOVE(0), TURNLEFT(1), ... ;
private int blByteCode;
private Instruction(int code) {this.blByteCode = code;
}
public int byteCode() {return this.blByteCode;
}}
22 November 2019 OSU CSE 53
This accessor method(an instance method)
allows a client to access an Instruction’s
associated blByteCode.
![Page 54: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/54.jpg)
Using This Feature
• In client code using Instruction, one might write something like this:
Instruction i = Instruction.TURNLEFT;...int code = i.byteCode();
or even:... Instruction.TURNLEFT.byteCode() ...
• The “byte code” thus obtained is what needs to be put into the generated code
22 November 2019 OSU CSE 54
![Page 55: Code Generation - Computer Science and Engineeringweb.cse.ohio-state.edu/.../slides/28.Code-Generation.pdfCode Generation • Code generation is translating a Programto a linear (non-nested)](https://reader030.fdocuments.us/reader030/viewer/2022040410/5ecaa0c93d077d7ca90ecd51/html5/thumbnails/55.jpg)
Resources
• OSU CSE Components API: Program,Program.Instruction– http://cse.osu.edu/software/common/doc/
• Java Tutorials: Enum Types– http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html
22 November 2019 OSU CSE 55