0% found this document useful (0 votes)
28 views14 pages

Poc Intermediate Code Generation

Principle of Compilers Code generation

Uploaded by

borntoinfo2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
28 views14 pages

Poc Intermediate Code Generation

Principle of Compilers Code generation

Uploaded by

borntoinfo2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 14
ITRODUCT ION RUCTION Intermediate Code ig Benerated using the p input langu: © code has the fo lated to “apture the compli age. Intermediat Simple enough to be trans} Complex enough to ¢, arse rule producing a | owing property mbly code, ication of high level | language from the language. Static Checker Intermediate Code Generator Intermediate Code Generator -—» Code’ front end backend source Highlevel Low Level backend eee ot —_ Intermediate = Program Representation Target ~> Intermediate ——» Code Representation Figure 5.1: Inermediate Code Generation An Intermediate Representation (IR) is a | language that can be easily ev; Tt should not include too muc language for an aluated by an abstract machine) h machine specific detail, It provides a Separation between front and b; Portability. Code generation and assignment of t Separated from semantic analysi: abstract machine (or a ack ends which helps compil emporaries to registers are clear'y It allows optimization independent of the target machine Intermediate Tepresentations are by design more abstract and unifor M, So Optimization routines are simpler, Properties of good intermediate representations Convenient to produce in the semantic analysis phase. Convenient to translate into code for the desired target architecture, Each construct must have a clear and simple meaning such that optimizing transformations that rewrite the IR can be easily specified and impl lemented, ~ Na jaced or vilaed «Postfix notation Code «Three address ise for generating three-address code from com r ramming Sa Fs a inilar to those for constructing symlax (ees of fur constructs are § Language construc’ scenerating postfix notation ad Intermediate Representations: ay to present the information gi ‘ representation can be handled using a struct for ned from lexical and syntax Tree-Ba The most obvious W e ax tree, In C, the analysis 18. Syn each node: struct node { int nodetype: struct node *field 1; struct node *field2; The nodetype field contains a small integer to identify the nonterminal corresponding to the node. The other fields point to subtrees. Unused fields may have null pointers Here's a C routine for allocating storage for the nodes. truct node *mknode (int type, struct node *f1, struct node *f2, struct node *£3) ‘ruct node *t = (struct node *) malloc(sizeof(struct node)); {> nodetype = type; t> field = f1; > field = 12; > field3 = 1; return { i We can exter efi f ges cnd our definition of the struct node from earlier to include name and struct node { int nodetype; struct node *field1; struct node *field2; struct node *field3; = 1s char *name; int value; pxample: x-directed definition to produce syntax trees for assignment statements could ‘Semantic Rule S.ptr E ptr: = make_node(’ +’, El ptr, make node assign’, make leafl id, identry), E.pt) pi) eines E,ptr: = make_node(’ *, El ptr, E2.ptr) | E.ptr: = make_node(’ *’, E es E pir: = make_node(’ unimus', El.ptt) Eptr: = EL ptr ne pir: = make _leaf(id, id.entry) a Postfix Notation is a linearized representation of a syntax tree Iris a list of the nodes of the tree in which every node appears immediately after its children. ‘The edges of a syntax tree do not appear explicitly in postfix notation However they can be recovered from the order in which the nodes appear and the number of operands that the operator at a node expects. ‘The recovery of edges is similar to the evaluation, using @ postfix notation stack, of an expression in Three-Address Code ‘Three-Address Codes are a form of IR simil ch three-address code instruction has the form jar to assembler for an imaginary machine. x=yopz Xy,z are names (identifiers), constants, compiler) op is an operator, Observe that, Complicated ari address statements, using temporaries for intermc expression x + y + z becomes th:sytz or temporaries (names generated by the from a limited set defined by the IR. Jhmetic expressions are diate values. For instance the represented as a sequence of 3- Q:=x+tl n of a syntax tree, where the names Three address codes is a linearized representatio of the temporaries correspond to the nodes for intermediate values all mization. lows three-address code to be easily The use of names Postfix notation does not have this rearranged which is convenient for opt feature. Ta Conran Stone PHU sia aed eae — les, that each statement usually contain three-address code is : ‘The reason for the term a for the result. three addresses, two for the operands and one Three Address Statements are similar to assembly code. We will consider the ree 3 § following statements. Assignments Two possible forms x= y op z where op is x:= op y where op is a unary a binary arithmetic or logical operation, operation (minus, negation, conversion operator) Copy statements They have the form x: Unconditional jumps They have the form goto L where L is a symbolic label of a statement. Unconditional jumps They have the form ifx relop y goto L where statement L is executed if x and y are in relation relop. Procedure calls They have the form param x1 param x2 param xn call p.m corresponding to the procdure call p(x!, x2, ... xn) Return statement ‘They have the form return y where ¥ Tepresenting a returned value is optional Indexed assignments They have the form x: = y(i] or x[i) Address assignments They have the form x: = &y which sets x to the location of y. Pointer assignments They have the form 'y where y is a pointer and which sets x to the value pointed to by y ¥ which changes the value pointed by x to the value ofy 53 One popular form of interme machine, fe representation is code for an abstract stack This machine has three memory zones, ‘The instruction memory Which holds the instruction of the program being executed ‘The data memory Which store values. Each value can be essed by its address or by the identifier of a variable associated with this address. if any. Astack Which is used to perform arithmetic operations. ‘The instruction is quite limited and fall into three classes: + Arithmetic operations: + Stack manipul + Control flow. ( Instruction Stack [Data value 1 push 5 | | 0 | 1 2 | rvalue2 top> | 7 1 | 2 3 + é | Ts =3 i 4 rvalue3 | ara | ae 4 3 * | EI*E2 Evplace = newtemp | “ code = generate(E.place, "=", El place, "*', E2.place) E>-El am = code == generate(E place, "=," unimus’, El place) ae E.code = El.code || code al ES (El) gin | f xpressions and Con ol Flow yolean Expressions are ¢ Boolean Expresstons are constructed using boolean operators. We will constder here the following rules ___ E> Eand E> not F Eid relop id F > true E > false + Boolean expressions are used as conditions for statements changing the flow of control + Evaluation of boolean expressions ean be optimized if itis sufficient to evaluate apart of the expression that determines its value + When translating Boolean expressions into three-address code, we can use two different methods> Numerical method Assign numerical values to true and false and evaluate the expression analogously to nt for boolean expressions which are not an arithmetic expression. This is conveni involved in flow of control constructs, J Evaluate a boolean expression Fas a sequel jumps to location E.true (if E is true) or to E.false. To be detailed shortly. mp method nce of conditional and unconditional nerical Method ation generates code for a while statement. While Statements with the Nu The following syntax-directed transl "Production Semantic Rule n= newlabel S$ while E repeat SI | S-beg' after = newlabel generate(S. begin ':) || E.code || codel + afier)| SI-code code2 = generate(‘if' E.place '= 0" 'goto’ S code} = generate(‘goto’ S.begin) |] generate(S.after) S.code = codel || code? || code3 With respect to the previous syntax-directed translation = We have added two new synthesized attributes S.begin and S.after * When the value of E becomes zero, control leaves the while statement p Method Flow of Control Starements with the we will consider here the following rules: s iff them S! s|2. s [sale ; the boolean expression 10 BS translated. (that are inherited roductions, Eis hp two Jabels In each of these Pt pression F gsociated with is a The boolean © ntic rules) 1 the following semai if E is true: Ise. attributes in ¢ the label to whic Jabel to W hich con! S isa flow h control flows ! trol flows if Bi of control state! + Btu + Efalse the ese productions, ment associated with two Ineach of th ment to be attributes sn is a label that 1S attached to the first 3-address state rite § , S.next is am inhi g attribute, is a synthesized « S.next whicl executed after the code for attribute. le is the translation code for S, a8 usual it + S.cod «Senet ea] | Production | ‘Semantic Rule [Emme macwabel ____-— [S> if then SI newlabel oll next re cpext := S.next ode |r enerate(E.true '") I Sl.code a E.false newlabel S1.next ‘S2.next := S.next en | | S2.nent a generate(arue =) SL code | del = Ecode | generate(E.true ") | Sicode code2 := generate(’ ‘goto’ S.next) Il coded = generate(E false) || S2.code | | Zcode = codel || code? || eode3 [pawns fT . (Jo if E then S1 else S2 eran > while E repeat SI Sbegin code] = generate(S begin) || E.code coded <= generate(E.true '?) || St.code ol code3 generate(‘goto’ S.begin) codel || code? || code3 S.code = ——— ervey Thinkcal Learning ei Sehutons Pet Lid No pa i “i Ns par of hs bookiel may be reproduced or ulze Santon perms produced or utlized cea many form without the writen permissor The above is a syntax-dire oe ee eae definition: It provides formulas for the computation of © computations of the other attributes). Since several attributes 'S are inherited and si . s eincerene| above appears after its associated production, this is ae ae igus : not a translation scheme. However it is an L-attributed definition, Then its conversion into a translation scheme is obvious. From now o1 ay preser 7 v on, we may present a translation scheme as a syntax-directed definition if the latter is an L-attributed definition. The reason is to make large translation schemes easier to read. Translation Of Boolean Expressions with the Jump Method: We will consider here the following rules. E + EI or £2 ElandE2 «| not El (ED) idl relop id2_ LJ dfs] s]d true ml mom mo] [> ~ | false © Here again each symbol E is associated with two inherited attributes © E.true the label to which control flows if E is true: + E:false the label to which control flows if E is false. © The attributes E.true and E.fulse of E will be defined when the flow of control (where E appears) is translated. Production Semantic Rule EEl or E2 Fl true = E.true EI false := newlabel E2.true *= E.true false E2.false ° E- Eland E2 El.true = newlabel Cg eeeeee| OO E.code = El.code | | generate(E1 false") | | E2.code | EL false == E.false E2.true := E.true E2.false := E.false ie ny ba reproduced OF Ics| code _[Ebane > Petals = ee —T keke SEtcode — Fire = Batre eS - | Eatrue) Toded = generate(’goto’ Sc ce | Excode = codel || code? Eeode = generate(‘goto' E.true) Etre [E> false [ Escode = gene’ rate( goto’ E. false) Example: Let us consider the expression a

You might also like