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

UP Lecture 25

UP Lecture 25

Compilers
Intermediate Code II
(202505)

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. Dr. Javier Gonzalez-Sanchez | Compilers | 2 jgs jgs GitHub

    Classroom ▪ Final Project https://classroom.github.com/a/XJmZgSI2 ▪ TheCompiler ▪ Your final project ▪ Include Lexer ▪ Include Parser with error handling ▪ Include Semantic Analysis ▪ Include Code Generation ▪ Bonus: your generated code work with the provided VM
  2. Dr. Javier Gonzalez-Sanchez | Compilers | 3 jgs jgs Grading

    Midterm Exam(s) 40% 10% + 10% 20% Final Exam (Comprehensive) 40% 10% + 10% + 10% + 10% Programming Assignments (4) 100% A+ 97 A 93 A- 89 B+ 86 B 82 B- 78 C+ 74 C 70 Labs & Homework 20% 20%
  3. Dr. Javier Gonzalez-Sanchez | Compilers | 4 jgs jgs Grading

    Midterm Exam(s) 40% 10% + 10% 20% Final Exam (Comprehensive) 40% 10% Programming Assignments (4) Labs & Homework 20% 20% 10% + 10% + 10% Labs & Homework [100, 100, 95, 70] = 91.25 => 18.25/20 Exams [70, 89] = 79.5 => 15.9/20 Total Today: 34.15 / 40
  4. Dr. Javier Gonzalez-Sanchez | Compilers | 6 jgs 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
  5. Dr. Javier Gonzalez-Sanchez | Compilers | 7 jgs 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
  6. Dr. Javier Gonzalez-Sanchez | Compilers | 8 jgs 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
  7. Dr. Javier Gonzalez-Sanchez | Compilers | 9 jgs 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)
  8. Dr. Javier Gonzalez-Sanchez | Compilers | 10 jgs 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
  9. Dr. Javier Gonzalez-Sanchez | Compilers | 12 jgs 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
  10. Dr. Javier Gonzalez-Sanchez | Compilers | 13 jgs 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
  11. Dr. Javier Gonzalez-Sanchez | Compilers | 14 jgs 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
  12. Dr. Javier Gonzalez-Sanchez | Compilers | 15 jgs jgs Review

    Virtual Machine (interpreter) 100 true total: 50
  13. Dr. Javier Gonzalez-Sanchez | Compilers | 21 jgs jgs Assignment

    #4 | VM Use it to test your compiler. No changes required
  14. Dr. Javier Gonzalez-Sanchez | Compilers | 23 jgs jgs Review

    Programming Assignment 4 Level 1 LIT, LOD, STO, OPR
  15. Dr. Javier Gonzalez-Sanchez | Compilers | 43 jgs jgs Parser

    R if (twice) { OPR <operatorNumber>, 0 }
  16. Dr. Javier Gonzalez-Sanchez | Compilers | 45 jgs jgs Parser

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

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

    B if (operatorUsed) { OPR 3, 0 } LIT 0, 0
  19. Dr. Javier Gonzalez-Sanchez | Compilers | 51 jgs jgs Assignment

    2 | Code C LIT <value>, 0 LOD identifier, 0
  20. jgs 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.