130 likes | 137 Views
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.
E N D
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 5 2 10 15 13
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
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
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
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’);}
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
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);}
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
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
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
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