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

UP Lecture 24

UP Lecture 24

Compilers
Intermediate Code
(202505)

Javier Gonzalez-Sanchez PRO

December 29, 2023

Transcript

28 slides
  1. Dr. Javier Gonzalez-Sanchez | Compilers | 2 jgs A Compiler

    in Action Lexer Parser Semantic Analyzer Code Generation Words Tokens Sentences Symbol table Uniqueness Type matching Translation Source Code ➔ Intermediate Code Intermediate Code ➔ Machine or Binary Code Analysis Compilation Assembly
  2. 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
  3. Dr. Javier Gonzalez-Sanchez | Compilers | 4 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
  4. Dr. Javier Gonzalez-Sanchez | Compilers | 5 jgs Assignment #4

    Lexer Parser Semantic Analyzer Code Generation Report Errors JTable with Tokens Report Errors Draw a Tree Report Errors Create Symbol Table Following lectures
  5. Dr. Javier Gonzalez-Sanchez | Compilers | 6 jgs Source Code

    { int a; int b; int c; int d; if (a != 5) { b = c + d; } print (a); }
  6. Dr. Javier Gonzalez-Sanchez | Compilers | 7 jgs Intermediate (Object)

    Code 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 a, 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 Symbol table Instructions
  7. Dr. Javier Gonzalez-Sanchez | Compilers | 8 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
  8. Dr. Javier Gonzalez-Sanchez | Compilers | 9 jgs A Simple

    Virtual Machine CPU ALU Register Memory
  9. Dr. Javier Gonzalez-Sanchez | Compilers | 10 jgs Memory program

    A Simple Virtual Machine CPU ALU Register 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
  10. Dr. Javier Gonzalez-Sanchez | Compilers | 11 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
  11. Dr. Javier Gonzalez-Sanchez | Compilers | 13 jgs Instructions ▪

    LIT <value>, <register_id> Put a constant value in a CPU register Examples: LIT 5, 0 LIT 5.5, 0 LIT 'a', 0 LIT ”hello”, 0 LIT true, 0
  12. Dr. Javier Gonzalez-Sanchez | Compilers | 14 jgs Instructions ▪

    LOD <variable>, <register_id> Search for <variable> in the symbol table Read its value Put the value of <variable> in the CPU register Examples: LOD a, 0 LOD hello, 0 LOD cse340, 0
  13. Dr. Javier Gonzalez-Sanchez | Compilers | 15 jgs Instructions ▪

    STO <variable>, <register_id> Read a value from the CPU register Search for <variable> in the symbol table Store the value into the variable Examples: STO a, 0 STO hello, 0 STO cse340, 0
  14. Dr. Javier Gonzalez-Sanchez | Compilers | 16 jgs Example Source

    Code: { int a; int b; a = 10; b = a; Symbol Table: Type Name Scope Value int a global 0 int b global 0 Intermediate (Object) Code: LIT 10,0 STO a,0 LOD a,0 STO b,0
  15. Dr. Javier Gonzalez-Sanchez | Compilers | 17 jgs Instructions ▪

    OPR <operation>, <register_id> Read one or two values from the CPU register Do the operation <operation> Put the result into the CPU register Examples: OPR 1, 0 ; return OPR 2, 0 ; add OPR 3, 0 ; subtract
  16. Dr. Javier Gonzalez-Sanchez | Compilers | 18 jgs Operations Number

    Action 0 Exit program 1 Return 2 ADD: POP A, POP B, PUSH B+A 3 SUBTRACT: POP A, POP B, PUSH B-A 4 MULTIPLY: POP A, POP B, PUSH B*A 5 DIVIDE: POP A, POP B, PUSH B/A 6 MOD: POP A, POP B, PUSH (B mod A) 7 POW: POP A, POP B, PUSH (A to the power B) 8 OR: POP A, POP B, PUSH (B or A) 9 AND: POP A, POP B, PUSH (B and A) 10 NOT: POP A, PUSH (not A) 11 TEST GREATER THAN: POP A, POP B, PUSH (B>A) 12 TEST LESS THAN: POP A, POP B, PUSH (B<A) 13 TEST GREATER THAN OR EQUAL: POP A, POP B, PUSH (B>=A)
  17. Dr. Javier Gonzalez-Sanchez | Compilers | 19 jgs Operations Number

    Action 14 TEST LESS THAN OR EQUAL: POP A, POP B, PUSH (B<=A) 15 TEST EQUAL: POP A, POP B, PUSH (B=A) 16 TEST NOT EQUAL: POP A, POP B, PUSH (B<>A) 17 18 clear screen 19 read a value from the standard input 20 print a value to the standard output 21 print a value to the standard output and a newline character
  18. Dr. Javier Gonzalez-Sanchez | Compilers | 20 jgs Example {

    int a; int b; a = 10; b = a; a = a * 10; b = 2 + 3 + 4; } Type Name Scope Value int a global 0 int b global 0 LIT 10, 0 STO a, 0 LOD a, 0 STO b, 0 LOD a, 0 LIT 10, 0 OPR 4, 0 ; multiply STO a, 0 LIT 2, 0 LIT 3, 0 OPR 2, 0 ; add LIT 4, 0 OPR 2, 0 ; add STO b, 0 OPR 1,0 OPR 0,0
  19. Dr. Javier Gonzalez-Sanchez | Compilers | 21 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
  20. Dr. Javier Gonzalez-Sanchez | Compilers | 22 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
  21. Dr. Javier Gonzalez-Sanchez | Compilers | 23 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
  22. Dr. Javier Gonzalez-Sanchez | Compilers | 24 jgs Review Virtual

    Machine (interpreter) 100 true total: 50
  23. Dr. Javier Gonzalez-Sanchez | Compilers | 26 jgs We need

    a VM Use it to test your compiler. No changes required
  24. jgs Compilers Javier Gonzalez-Sanchez, Ph.D. [email protected] Spring 2025 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.

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming