Upgrade to Pro — share decks privately, control downloads, hide ads and more …

UP Lecture 22

UP Lecture 22

Compilers
Code Generation II
(202405)

Javier Gonzalez-Sanchez

December 27, 2023
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. Dr. Javier Gonzalez-Sanchez | Compilers | 3 jgs High-Level Languages

    X,E,G,O,O #e1,I,I,0,7 @ OPR 19, AX STO x, AX LIT 5, AX OPR 21, AX LOD #e1,AX CAL 1, AX OPR 0, AX 5 Virtual Machine (interpreter) // sorce code int x; int foo () { read (x); print (5); } main () { foo (); } Lexer Parser Semantic Analyzer Code Generation 01001010101000010 01010100101010010 10100100000011011 11010010110101111 00010010101010010 10101001010101011 Assembler compilation execution
  2. Dr. Javier Gonzalez-Sanchez | Compilers | 4 jgs Translate Source

    to Object { int a; int b; int c; int d; if (a != 5) { b = c + d; } print (a); } a,int,global 0 b,int,global 0 c,int,global 0 d,int,global 0 #E1,int,label,9 #P,int,label,1 @ lod , 0 lit 5, 0 opr 14, 0 jmc #e1, false lod c, 0 lod d, 0 opr 2, 0 sto b, 0 lod a, 0 opr 21, 0 opr 1, 0
  3. Dr. Javier Gonzalez-Sanchez | Compilers | 5 jgs A Simple

    Virtual Machine CPU ALU Register PC Memory program Code sto 0, s sto 0, d sto 0, c sto 0, d lod s, 0 lit “s”, 0 opr 14, 0 jmc #a1, false lod b, 0 Symbol Table
  4. Dr. Javier Gonzalez-Sanchez | Compilers | 7 jgs Code Generation

    * 1 2 * 4 5 + 3 > 1 * 2 > 3 + 4 * 5 LIT 1, 0 LIT 2, 0 OPR 4, 0 LIT 3, 0 LIT 4, 0 LIT 5, 0 OPR 4, 0 OPR 2, 0 OPR 11, 0
  5. Dr. Javier Gonzalez-Sanchez | Compilers | 8 jgs Review a,

    int, global, 0 b, int, global, 0 @ LIT 5, 0 STO a, 0 LIT 9, 0 STO b, 0 LOD a, 0 LOD b, 0 LIT 3, 0 OPR 4, 0 OPR 2, 0 STO a, 0 LOD a, 0 OPR 21,0 LIT "hello", 0 OPR 21,0 OPR 1, 0 OPR 0,0 Virtual Machine (interpreter) { int a; int b; a = 5; b = 9; a = a + b / 3; print (a); print ("hello"); } Lexer Parser Semantic Analyzer Code Generation 8 hello
  6. Dr. Javier Gonzalez-Sanchez | Compilers | 9 jgs Review {

    int a; int b; boolean foo; a = 10 + 20 + 30 + 40; print (a); foo = 340 > 126; print (foo); a = a / 2; print ("total:" + a); return; } Lexer Parser Semantic Analyzer Code Generation
  7. Dr. Javier Gonzalez-Sanchez | Compilers | 10 jgs Review Virtual

    Machine (interpreter) 100 true total: 50
  8. Dr. Javier Gonzalez-Sanchez | Compilers | 16 jgs Assignment #4

    | VM Use it to test your compiler. No changes required
  9. Dr. Javier Gonzalez-Sanchez | Compilers | 38 jgs Parser R

    if (twice) { OPR <operatorNumber>, 0 }
  10. Dr. Javier Gonzalez-Sanchez | Compilers | 40 jgs Parser E

    if (twice) { OPR <operatorNumber>, 0 }
  11. Dr. Javier Gonzalez-Sanchez | Compilers | 42 jgs Parser A

    if (twice) { OPR <operatorNumber>, 0 }
  12. Dr. Javier Gonzalez-Sanchez | Compilers | 44 jgs Parser B

    if (operatorUsed) { OPR 3, 0 } LIT 0, 0
  13. Dr. Javier Gonzalez-Sanchez | Compilers | 46 jgs Assignment 2

    | Code C LIT <value>, 0 LOD identifier, 0
  14. jgs Compilers Javier Gonzalez-Sanchez, Ph.D. [email protected] Spring 2024 Copyright. These

    slides can only be used as study material for the Compilers course at Universidad Panamericana. They cannot be distributed or used for another purpose.