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

UP Lecture 23

UP Lecture 23

Compilers
Code Generation III
(202405)

Javier Gonzalez-Sanchez

December 28, 2023
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. 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
  2. Dr. Javier Gonzalez-Sanchez | Compilers | 18 jgs Parser R

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

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

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

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

    | Code C LIT <value>, 0 LOD identifier, 0
  7. Dr. Javier Gonzalez-Sanchez | Compilers | 24 jgs Instructions §

    JMP <line>, 0 Put the value <line> in the program counter; thus, the next line to be executed will be <line> Examples: JMP 1, 0 JMP 14, 0 JMP 75, 0
  8. Dr. Javier Gonzalez-Sanchez | Compilers | 25 jgs Instructions §

    JMC <line>, <condition> Read one value from the CPU register If the value is equal to <condition>, put the value <line> in the program counter; thus, the next line to be executed will be <line> Examples: JMC 1, true JMC 14, false JMC 75, true
  9. Dr. Javier Gonzalez-Sanchez | Compilers | 26 jgs Example {

    int a; int b; a = 10; while (a>1){ print (a); } return; } int a global 0 int b global 0 @ LIT 10, 0 STO a, 0 LOD a, 0 LIT 1, 0 OPR 11, 0 ; > JMC #e1, false LOD a, 0 OPR 20, 0 ; print JMP #e2, 0 OPR 1, 0 OPR 0, 0
  10. Dr. Javier Gonzalez-Sanchez | Compilers | 27 jgs Labels §

    The compiler creates variables and adds them to the symbol table to remember positions in the code. § This is useful for loops and conditions.
  11. Dr. Javier Gonzalez-Sanchez | Compilers | 28 jgs Labels |

    how while works while (a < b) { //code } label#1 if (a < b) { //code } else goto label#2 } goto label#1 label #2
  12. Dr. Javier Gonzalez-Sanchez | Compilers | 29 jgs Labels |

    how if works if (a < b) { //code1 } else { // code2 } if (a < b) { if (a>b) goto label #1 //code1 } else goto label#2 label #1 // code2 } label #2
  13. Dr. Javier Gonzalez-Sanchez | Compilers | 30 jgs Example |

    while { int a; int b; a = 10; while (a>1){ print (a); } return; } int a global 0 int b global 0 int #e1 global 10 int #e2 global 3 LIT 10, 0 STO a, 0 LOD a, 0 LIT 1, 0 OPR 11, 0 ; > JMC #e1, false LOD a, 0 OPR 20, 0 ;print JMP #e2, 0 OPR 1, 0 OPR 0, 0
  14. Dr. Javier Gonzalez-Sanchez | Compilers | 31 jgs Example |

    if { int a; int b; a = 10; if (a>1) { print (a); } else { print (b); } return; } int a global 0 int b global 0 int #e1 global 10 int #e2 global 12 LIT 10, 0 STO a, 0 LOD a, 0 LIT 1, 0 OPR 11, 0 ; > JMC #e1, false LOD a, 0 OPR 20, 0 ;print JMP #e2, 0 LOD b, 0 OPR 20, 0 ;print OPR 1, 0 OPR 0, 0
  15. Dr. Javier Gonzalez-Sanchez | Compilers | 32 jgs Exercise {

    int a; int b; a = 10; while (a>1) { if (a != 0) { print (a); } else { print (b); } a = a -1; } }
  16. Dr. Javier Gonzalez-Sanchez | Compilers | 39 jgs Homework Translate

    this source code to intermediate code: int a; switch (a) { case 1: { print("hi”); break;} case 2: { print("world"); break;} default: { print(”end"); break;} }
  17. Dr. Javier Gonzalez-Sanchez | Compilers | 42 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
  18. 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.