code generation
play

Code Generation 22 November 2019 OSU CSE 1 BL Compiler Structure - PowerPoint PPT Presentation

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


  1. Code Generation 22 November 2019 OSU CSE 1

  2. 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

  3. 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

  4. Executing a BL Program This is what the BL compiler will actually do; • There are two qualitatively different ways and this is how Java itself one might execute a BL program, given a works (recall the JVM value of type Program that has been and its “byte codes”). 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

  5. 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

  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 22 November 2019 OSU CSE 6

  7. 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

  8. “Execution-time” or Time Lines of Execution “run-time” 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

  9. 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

  10. 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

  11. BLOCK s = IF condition CALL IF_ELSE instruction WHILE condition condition 22 November 2019 OSU CSE 11

  12. 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); } It’s recursive just like break ; everything else to do with } Statement ; context is case IF: {...} ... needed for case CALL . } 22 November 2019 OSU CSE 12

  13. 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

  14. The State of BugsWorld 22 November 2019 OSU CSE 14

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. Code Generation Primitive BL instructions are translated into • Code generation is translating a these “byte 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

  21. Code Generation BL control constructs that check conditions are translated into • Code generation is translating a these “byte 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 21

  22. Example Statement Loc Instruction (symbolic name) IF next-is-wall THEN JUMP_IF_NOT_NEXT_IS_WALL turnleft 0 1 5 ELSE 2 TURNLEFT move 3 JUMP END IF IF_ELSE NEXT_IS_WALL 6 4 5 MOVE BLOCK BLOCK 6 ... CALL CALL turnleft move 22 November 2019 OSU CSE 22

  23. Example Statement Loc Instruction (“byte code”) IF next-is-wall THEN 9 turnleft 0 1 5 ELSE 2 1 move 3 6 END IF IF_ELSE NEXT_IS_WALL 6 4 5 0 BLOCK BLOCK 6 ... CALL CALL turnleft move 22 November 2019 OSU CSE 23

  24. BugsWorld Virtual Machine • The virtual machine for BugsWorld has three main features: – Memory – Instruction set – Program counter 22 November 2019 OSU CSE 24

  25. BugsWorld Virtual Machine A string of integer s that contains the “byte codes” • The virtual machine for BugsWorld has generated from a Program . three main features: – Memory – Instruction set – Program counter 22 November 2019 OSU CSE 25

  26. BugsWorld Virtual Machine A finite set of integer s that are the “byte codes” for the primitive instructions of the • The virtual machine for BugsWorld has BugsWorld VM. three main features: – Memory – Instruction set – Program counter 22 November 2019 OSU CSE 26

  27. BugsWorld Virtual Machine Each instruction is given a symbolic name here, for ease of reading, but the VM knows • The virtual machine for BugsWorld has only about integer “byte codes”. three main features: – Memory – Instruction set – Program counter 22 November 2019 OSU CSE 27

  28. BugsWorld Virtual Machine An integer that designates the location/position/address in memory of the “byte code” to • The virtual machine for BugsWorld has be executed next. three main features: – Memory – Instruction set – Program counter 22 November 2019 OSU CSE 28

  29. BugsWorld Virtual Machine Normal execution increments the program counter by 1 or 2 after each instruction, so • The virtual machine for BugsWorld has execution proceeds sequentially. three main features: – Memory – Instruction set – Program counter 22 November 2019 OSU CSE 29

  30. 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

  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 one memory location. 22 November 2019 OSU CSE 31

Recommend


More recommend