Slide 1

Slide 1 text

jgs Compilers Lecture 21: Intermediate Code Dr. Javier Gonzalez-Sanchez [email protected]

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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); }

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

Dr. Javier Gonzalez-Sanchez | Compilers | 12 jgs Instructions instruction first second parameter parameter

Slide 13

Slide 13 text

Dr. Javier Gonzalez-Sanchez | Compilers | 13 jgs Instructions § LIT , 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

Slide 14

Slide 14 text

Dr. Javier Gonzalez-Sanchez | Compilers | 14 jgs Instructions § LOD , Search for in the symbol table Read its value Put the value of in the CPU register Examples: LOD a, 0 LOD hello, 0 LOD cse340, 0

Slide 15

Slide 15 text

Dr. Javier Gonzalez-Sanchez | Compilers | 15 jgs Instructions § STO , Read a value from the CPU register Search for in the symbol table Store the value into the variable Examples: STO a, 0 STO hello, 0 STO cse340, 0

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

Dr. Javier Gonzalez-Sanchez | Compilers | 17 jgs Instructions § OPR , Read one or two values from the CPU register Do the operation Put the result into the CPU register Examples: OPR 1, 0 ; return OPR 2, 0 ; add OPR 3, 0 ; subtract

Slide 18

Slide 18 text

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)

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

Dr. Javier Gonzalez-Sanchez | Compilers | 24 jgs Review Virtual Machine (interpreter) 100 true total: 50

Slide 25

Slide 25 text

Dr. Javier Gonzalez-Sanchez | Compilers | 25 jgs to be continued...

Slide 26

Slide 26 text

Dr. Javier Gonzalez-Sanchez | Compilers | 26 jgs Questions

Slide 27

Slide 27 text

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.