Code Generation 22 November 2019 OSU CSE 1 BL Compiler Structure Code Tokenizer Parser Generator string of string of abstract string of characters tokens program integers (source code) (“words”) (object code) The code generator is the last step. 22 November 2019 OSU CSE 2 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 Executing a BLThis Program is what the BL compiler will actually do; • There are two qualitativelyand this isdifferent how Java ways itself one might execute a worksBL program, (recall the given JVM a value of type Programandthat its “bytehas beencodes”). 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 Executing a BL Program Let’s first see how this might be done ... • 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 Time Lines of Execution • Directly interpreting a Program: Execute by interpreting the Tokenize Parse Program directly • Compiling and then executing a Program: Generate Execute by interpreting Tokenize Parse code generated code on VM 22 November 2019 OSU CSE 6 Time Lines of Execution • Directly interpreting a Program: Execute by interpreting the Tokenize Parse Program directly • Compiling and then executing a Program: Generate Execute by interpreting Tokenize Parse code generated code on VM At this point, you have a Program value to use. 22 November 2019 OSU CSE 7 “Execution-time” or Time Lines of“run-time” Execution means here. • Directly interpreting a Program: Execute by interpreting the Tokenize Parse Program directly • Compiling and then executing a Program: Generate Execute by interpreting Tokenize Parse code generated code on VM “Execution-time” or “run-time” means here. 22 November 2019 OSU CSE 8 Interpreting a Program • The structure of a Program and, within it, the recursive structure of a Statement, directly dictate how to execute a Program by interpretation • Without contracts and other details, the following few slides indicate the structure of such code 22 November 2019 OSU CSE 9 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 BLOCK s = IF condition CALL instruction WHILE IF_ELSE condition condition 22 November 2019 OSU CSE 11 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; It’s recursive just like } everything else to do with case IF: {...} Statement; context is ... needed for case CALL. } 22 November 2019 OSU CSE 12 executeStatement • 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 22 November 2019 OSU CSE 13 The State of BugsWorld 22 November 2019 OSU CSE 14 The State of BugsWorld For example, when executing this bug’s Program in this state, next-is-empty is true. 22 November 2019 OSU CSE 15 Where Is The State of BugsWorld? A client executes a particular bug’s program, and tells the server to execute primitive instructions. The server knows about all the bugs, and can report to a client what a particular bug “sees”. 22 November 2019 OSU CSE 16 executeStatement • 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 22 November 2019 OSU CSE 17 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 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 Code Generation Primitive BL instructions are translated into • Code generation is translatingthese “byte a codes”. 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 Code GenerationBL control constructs that check conditions • Code generation is translatingare translated a into Program to a linear (non-nested)these “byte codes”. 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 Example Statement IF next-is-wall THEN Loc Instruction (symbolic name) turnleft 0 JUMP_IF_NOT_NEXT_IS_WALL ELSE 1 5 move 2 TURNLEFT 3 JUMP END IF IF_ELSE NEXT_IS_WALL 4 6 5 MOVE BLOCK BLOCK 6 ... CALL CALL turnleft move 22 November 2019 OSU CSE 22 Example Statement IF next-is-wall THEN Loc Instruction (“byte code”) turnleft 0 9 ELSE 1 5 move 2 1 3 6 END IF IF_ELSE NEXT_IS_WALL 4 6 5 0 BLOCK BLOCK 6 ... CALL CALL turnleft move 22 November 2019 OSU CSE 23 BugsWorld Virtual Machine • The virtual machine for BugsWorld has three main features: – Memory – Instruction set – Program counter 22 November 2019 OSU CSE 24 BugsWorld Virtual Machine A string of integers that contains the “byte codes” • The virtual machinegenerated for BugsWorld from a Program has. three main features: – Memory – Instruction set – Program counter 22 November 2019 OSU CSE 25 BugsWorldA finite Virtual set Machine of integers that are the “byte codes” for • The virtual machinethe primitive for BugsWorld instructions hasof the three main features: BugsWorld VM. – Memory – Instruction set – Program counter 22 November 2019 OSU CSE 26 BugsWorldEach Virtual instruction Machine is given a symbolic name here, for ease • The virtual machineof reading, for BugsWorldbut the VM knows has three main features:only about integer “byte codes”. – Memory – Instruction set – Program counter 22 November 2019 OSU CSE 27 BugsWorldAn Virtual integer Machinethat designates the location/position/address in • The virtual machinememory for of BugsWorld the “byte code” has to three main features: be executed next. – Memory – Instruction set – Program counter 22 November 2019 OSU CSE 28 BugsWorldNormal Virtual execution Machine increments the program counter by 1 or 2 • The virtual machineafter eachfor BugsWorld instruction, so has three main features:execution proceeds sequentially. – Memory – Instruction set – Program counter 22 November 2019 OSU CSE 29 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 Instruction Set • The instruction set, or target language, for code generation has two types of instructions: – Primitive instructions – Jump instructions Each of these occupies one memory location. 22 November 2019 OSU CSE 31 Instruction Set • The instruction set, or target language, for code generation has two types of instructions: – Primitive instructions – Jump instructions Each of these occupies two memory locations: the second one is the location to jump to. 22 November 2019 OSU CSE 32 Primitive Instructions • MOVE (0) • TURNLEFT (1) • TURNRIGHT (2) • INFECT (3) • SKIP (4) • HALT (5) 22 November 2019 OSU CSE 33 Primitive Instructions • MOVE (0) This is the “byte code” • TURNLEFT (1) corresponding to the symbolic name for each • TURNRIGHT (2) instruction code. • INFECT (3) • SKIP (4) • HALT (5) 22 November 2019 OSU CSE 34 Primitive Instructions • MOVE (0) This instruction halts • TURNLEFT (1) program execution, and is the last instruction to be • TURNRIGHT (2) executed. • INFECT (3) • SKIP (4) • HALT (5) 22 November 2019 OSU CSE 35 Jump Instructions • JUMP (6) • JUMP_IF_NOT_NEXT_IS_EMPTY (7) • JUMP_IF_NOT_NEXT_IS_NOT_EMPTY (8)
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages55 Page
-
File Size-