c09 - CS421 COMPILERS AND INTERPRETERS CS421 Intermediate...

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

View Full Document Right Arrow Icon
CS421 COMPILERS AND INTERPRETERS Copyright 1994 - 2010 Zhong Shao, Yale University Intermediate Code Generation: Page 1 of 25 Intermediate Code Generation • Translating the abstract syntax into the intermediate representation . • What should Intermediate Representation ( IR ) be like ? not too low-level (machine independent) but also not too high-level (so that we can do optimizations) • How to convert abstract syntax into IR ? parser Tiger source program absyn inter. code semantic analysis report all lexical report all semantic errors inter. codegen correct absyn compiler backend CS421 COMPILERS AND INTERPRETERS Copyright 1994 - 2010 Zhong Shao, Yale University Intermediate Code Generation: Page 2 of 25 Intermediate Representations (IR) What makes a good IR ? --- easy to convert from the absyn; easy to convert into the machine code; must be clear and simple; must support various machine-independent optimizing transformations; • Some modern compilers use several IRs ( e.g., k=3 in SML/NJ ) --- each IR in later phase is a little closer (to the machine code) than the previous phase. Absyn ==> IR 1 ==> IR 2 ... ==> IR k ==> machine code pros : make the compiler cleaner, simpler, and easier to maintain cons : multiple passes of code-traversal --- compilation may be slow •T h e Tiger compiler uses one IR only --- the Intermediate Tree (itree) Absyn => itree frags => assembly => machine code How to design itree ? stay in the middle of absyn and assembly! CS421 COMPILERS AND INTERPRETERS Copyright 1994 - 2010 Zhong Shao, Yale University Intermediate Code Generation: Page 3 of 25 Case Study : itree • Here is one example, defined using ML datatype definition: structure Tree : TREE = struct type label = string type size = int type temp = int datatype stm = SEQ of stm * stm | . ..... and exp = BINOP of binop * exp * exp | . ..... and test = TEST of relop * exp * exp and binop = FPLUS | FMINUS | FDIV | FMUL | PLUS | MINUS | MUL | DIV | AND | OR | LSHIFT | RSHIFT | ARSHIFT | XOR and relop = EQ | NE | LT | GT | LE | GE | ULT | ULE | UGT | UGE | FEQ | FNE | FLT | FLE | FGT | FGE and cvtop = CVTSU | CVTSS | CVTSF | CVTUU | CVTUS | CVTFS | CVTFF end CS421 COMPILERS AND INTERPRETERS Copyright 1994 - 2010 Zhong Shao, Yale University Intermediate Code Generation: Page 4 of 25 itree Statements and Expressions • Here is the detail of itree statements stm and itree expressions exp datatype stm = SEQ of stm * stm | LABEL of label | JUMP of exp | CJUMP of test * label * label | MOVE of exp * exp | EXP of exp and exp = BINOP of binop * exp * exp | CVTOP of cvtop * exp * size * size | MEM of exp * size | TEMP of temp | ESEQ of stm * exp | NAME of label | CONST of int | CONSTF of real |
Background image of page 1

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

View Full Document Right Arrow Icon
CS421 COMPILERS AND INTERPRETERS Copyright 1994 - 2010 Zhong Shao, Yale University Intermediate Code Generation: Page 5 of 25
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 7

c09 - CS421 COMPILERS AND INTERPRETERS CS421 Intermediate...

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

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