1 / 13

Simple One-Pass Compiler part II

Simple One-Pass Compiler part II. Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University. Outline. Abstract Stack Machine. Simple Code Generation. Abstract Stack Machine. Basic machine with stack only for computation. Ex: push 10 push 5 add push 2 minus.

mcorina
Download Presentation

Simple One-Pass Compiler part II

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Simple One-Pass Compiler part II Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University

  2. Outline • Abstract Stack Machine. • Simple Code Generation.

  3. Abstract Stack Machine • Basic machine with stack only for computation. • Ex: push 10 push 5 add push 2 minus 5 2 10 15 13

  4. Abstract Stack Machine • Variables • Use value from variable • r-value. • Store value to variable • l-value. k := 5 2 + k • Ex: lvalue k push 5 := push 2 rvalue k add 5 k := 5 (addr) k 5 7 2

  5. Stack Manipulation • push v push v on top of stack • pop v pop v from top of stack • rvalue l push content of data l • lvalue l push address of data l • := put r-value on top to l-value below and pop both from top • copy push copy of top

  6. Translation of Expression day := (1461 * y) div 4 + (153 * m + 2) div 5 + d lvalue day push 153 div push 1461 rvalue m add rvalue y mul rvalue d mul push 2 add push 4 add := div push 5

  7. Translation Scheme for Simple Expression expr ::= expr + term {print(‘add’);} expr ::= expr – term {print(‘minus’);} expr ::= term term ::= 0{print(‘push 0’);} term ::= 1{print(‘push 1’);} ... term ::= 9{print(‘push 9’);}

  8. Example expr + { print(‘add’) } expr term - { print(‘minus’) } 2 { print(‘push 2’) } expr term term 5 { print(‘push 5’) } 9 { print(‘push 9’) } • Input:9 – 5 + 2 • Output: • push 9 • push 5 • minus • push 2 • add

  9. Translation Scheme for Simple Expression with Tokens expr ::= expr + term {print(‘add’);} expr ::= expr – term {print(‘minus’);} expr ::= term term ::= number{print(‘push ’); print(number.lexeme);} term ::= id{print(‘lvalue ); print(id.lexeme);}

  10. expr ::= expr + term {print(‘add’);} expr ::= expr – term {print(‘minus’);} expr ::= term term ::= number{print(‘push ’); print(number.lexeme);} term ::= id{print(‘rvalue ’); print(id.lexeme);} Input: 2 + k – m Output: push 2 rvalue k add rvalue m minus Example

  11. Translation Scheme for Assignment Statement stmt ::= id:={print(‘lvalue’); print(id.lexeme);} expr{print(‘:=‘);} • Input: k := k + 1 • Output: • lvalue k • rvalue k • push 1 • add • := expr stmt

  12. Control Flow • eq pop top two values and compare, if equal push 1 on top of the stack, otherwise push 0. • lt pop top two values and compare, if bottom less than first push 1 on top of the stack, otherwise push 0. • gt similar to “lt”. • label l target for jump • goto l jump to lable l • gofalse l pop top; jump if zero • gotrue l pop top; jump if nonzero

  13. Translation of if Statement stmt ::= ifexpr {out:=newlabel; print(‘gofalse’); print(out);} thenstmt1 {print(‘label ’); print(out);} • if k+1 < 0 then m := 3; • rvalue k • rush 1 • add • push 0 • lt • gofalse lb01 • lvalue m • push 3 • := • label lb01 stmt Code for expr gofalse lb01 Code for stmt1 label lb01

More Related