Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

201506 CSE340 Lecture 20

403 views

Published on

Intermediate Code (part 1/4)

Published in: Software
  • Be the first to comment

  • Be the first to like this

201506 CSE340 Lecture 20

  1. 1. CSE340 - Principles of Programming Languages Lecture 20: Intermediate Code I Javier Gonzalez-Sanchez BYENG M1-38 Office Hours: By appointment
  2. 2. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 2 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
  3. 3. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 3 Source Code { int a; int b; int c; int d; if (a != 5) { b = c + d; } print (a); }
  4. 4. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 4 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
  5. 5. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 5 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. 6. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 6 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
  7. 7. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 7 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
  8. 8. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 8 A Simple Virtual Machine CPU ALU Register Memory
  9. 9. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 9 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. 10. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 10 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. 11. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 11 Instructions instruction first second parameter parameter
  12. 12. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 12 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
  13. 13. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 13 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
  14. 14. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 14 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
  15. 15. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 15 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
  16. 16. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 16 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
  17. 17. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 17 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)
  18. 18. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 18 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
  19. 19. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 19 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
  20. 20. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 20 to be continued...
  21. 21. CSE340 - Principles of Programming Languages Javier Gonzalez-Sanchez javiergs@asu.edu Summer 2015 Disclaimer. These slides can only be used as study material for the class CSE340 at ASU. They cannot be distributed or used for another purpose.

×