csc402-ln006

csc402-ln006 - Intermediate Representation(IR Our simple...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
Intermediate Representation (IR) Our simple, syntax directed interpretation scheme that we worked out for the exp1 language, where we computed values for expressions as soon as we recognized them in the input stream, will fail with more complex languages. Let’s extend exp1 with conditional and unconditional jump instructions and call the language exp1bytecode Please read Chap. 4 in the Patterns Book.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Exp1bytecode Language Design Four new statements: stop ; jumpT exp label ; jumpF exp label ; jump label ; Note : exp is an integer expression and is interpreted as false if its value is zero otherwise it is true The notion of a labeled statement: E.g., lab3: store x (- x 1) ; jumpT x lab3; Two new operators: =, =<, that return 0 when false otherwise they will return 1. Lastly, we also allow for negative integer constants: -2, -12
Background image of page 2
Exp1bytecode prog : (labelDef? stmt)+ ; labelDef : NAME ':' ; stmt : 'print' exp ';' | 'store' lhsvar exp ';' | 'jumpT' exp label ';' | 'jumpF' exp label ';' | 'jump' label ';' | 'stop' ';' ; exp : '+' exp exp | '-' exp exp | '*' exp exp | '/' exp exp | '=' exp exp | '=<' exp exp | '(' exp ')' | rhsvar | NUM ; label : NAME ; lhsvar : NAME ; rhsvar : NAME ; NAME : ('a'. .'z'|'A'. .'Z')('a'. .'z'|'A'. .'Z'|'0'. .'9')* ; NUM : '-'? ('0'. .'9')+ ;
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Exp1bytecode Here is a simple example program in this language: // this program prints out a // list of integers store x 10 ; L1: print x ; store x (- x 1) ; jumpT x L1 ; stop ; Problem: in syntax directed interpretation all info needs to be available at statement execution time; the label definition is not available at jump time. Answer: we will use an IR to do the actual interpretation.
Background image of page 4
Syntax directed interpretation exp returns [int value] : '+' e1=exp e2=exp { $value = $e1.value + $e2.value; } | '-' e1=exp e2=exp { $value = $e1.value - $e2.value; } | '(' e=exp ')' { $value = $e.value; } | rhsvar { $value = $rhsvar.value; } | NUM { $value = Integer.parseInt($NUM.text); } ; lhsvar returns [String name] : NAME { $name = $NAME.text; }; rhsvar returns [int value] : NAME { Integer v = (Integer)memory.get($NAME.text); if (v == null) { $value = 0; } else { $value = v.intValue(); } } ; // lexical rules NAME : ('a'. .'z'|'A'. .'Z')('a'. .'z'|'A'. .'Z'|'0'. .'9')* ; NUM : '0'. .'9'+ ; In our simple expression interpreter we saw that all the info was available at expression execution.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Syntax directed interpretation prog : ( labelDef? stmt)+ ; labelDef : NAME ':' ; stmt : 'print' exp ';' | 'store' lhsvar exp ';' | 'jumpT' exp label ';' | 'jumpF' exp label ';' | 'jump'
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/03/2011 for the course CSC 402 taught by Professor Staff during the Fall '09 term at Rhode Island.

Page1 / 17

csc402-ln006 - Intermediate Representation(IR Our simple...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online