Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
28 views
14 pages
Poc Intermediate Code Generation
Principle of Compilers Code generation
Uploaded by
borntoinfo2
AI-enhanced title
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
Download
Save
Save poc intermediate code generation For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
0 ratings
0% found this document useful (0 votes)
28 views
14 pages
Poc Intermediate Code Generation
Principle of Compilers Code generation
Uploaded by
borntoinfo2
AI-enhanced title
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
Carousel Previous
Carousel Next
Download
Save
Save poc intermediate code generation For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
Download now
Download
You are on page 1
/ 14
Search
Fullscreen
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 PHUsia 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 ofy53 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 statementp 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 permissorThe 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
Compiler Design Chapter-6
PDF
No ratings yet
Compiler Design Chapter-6
83 pages
CD Unit3
PDF
No ratings yet
CD Unit3
17 pages
TSR - Class Cd-Unit 3
PDF
No ratings yet
TSR - Class Cd-Unit 3
111 pages
UNIT-3 Odg
PDF
No ratings yet
UNIT-3 Odg
17 pages
Unit-Iii: Intermediate Code Generation
PDF
No ratings yet
Unit-Iii: Intermediate Code Generation
47 pages
CS 346: Intermediate Code Generation: Resource
PDF
No ratings yet
CS 346: Intermediate Code Generation: Resource
60 pages
Intermediate Code Generation
PDF
100% (1)
Intermediate Code Generation
42 pages
FALLSEM2023-24 BCSE307L TH VL2023240100900 2023-06-14 Reference-Material-I
PDF
No ratings yet
FALLSEM2023-24 BCSE307L TH VL2023240100900 2023-06-14 Reference-Material-I
33 pages
Intermediate Code Generation and Code Optimization
PDF
No ratings yet
Intermediate Code Generation and Code Optimization
40 pages
Chapter 5 Intermidate Code Generation
PDF
No ratings yet
Chapter 5 Intermidate Code Generation
30 pages
Module 4 - Intermediate Code Generation
PDF
No ratings yet
Module 4 - Intermediate Code Generation
61 pages
Chapter 5 Intermidate Code Generation
PDF
No ratings yet
Chapter 5 Intermidate Code Generation
11 pages
Chapter 5 - Intermediate Code Generation
PDF
No ratings yet
Chapter 5 - Intermediate Code Generation
27 pages
Unit-4-2
PDF
No ratings yet
Unit-4-2
23 pages
Intermediate Code Generation
PDF
No ratings yet
Intermediate Code Generation
23 pages
Chapter 6 - Intermediate Code Generation
PDF
No ratings yet
Chapter 6 - Intermediate Code Generation
42 pages
4 - Intermediate Code Generation
PDF
No ratings yet
4 - Intermediate Code Generation
51 pages
006chapter 6 - Intermediate Code Generation
PDF
No ratings yet
006chapter 6 - Intermediate Code Generation
23 pages
Chapter 6 - Intermediate Languages
PDF
No ratings yet
Chapter 6 - Intermediate Languages
18 pages
Compiler Engineering
PDF
No ratings yet
Compiler Engineering
24 pages
UNIT-3 Part-A:Semantic Analysis 1. Intermediate Code Forms
PDF
No ratings yet
UNIT-3 Part-A:Semantic Analysis 1. Intermediate Code Forms
26 pages
Unit 4 - Compiler Design - WWW - Rgpvnotes.in
PDF
No ratings yet
Unit 4 - Compiler Design - WWW - Rgpvnotes.in
21 pages
CD Unit-Iii
PDF
No ratings yet
CD Unit-Iii
20 pages
Compiler Design Lec-Six Intermediate Languages
PDF
No ratings yet
Compiler Design Lec-Six Intermediate Languages
21 pages
CD - Ch.6
PDF
No ratings yet
CD - Ch.6
33 pages
Intermediate Code Generator 1
PDF
No ratings yet
Intermediate Code Generator 1
48 pages
Chapter 6 Code Generation and Optimization
PDF
No ratings yet
Chapter 6 Code Generation and Optimization
34 pages
Lec05 Intermediate Code Generation
PDF
No ratings yet
Lec05 Intermediate Code Generation
40 pages
Mod 4
PDF
No ratings yet
Mod 4
39 pages
RkCD-Chapter 6 - Intermediate Code Generation
PDF
No ratings yet
RkCD-Chapter 6 - Intermediate Code Generation
12 pages
Intermediate Code Generation
PDF
No ratings yet
Intermediate Code Generation
42 pages
Unit 4 - Compiler Design - WWW - Rgpvnotes.in
PDF
No ratings yet
Unit 4 - Compiler Design - WWW - Rgpvnotes.in
23 pages
Assignment On Functional Programming
PDF
No ratings yet
Assignment On Functional Programming
6 pages
CSC 453 Intermediate Code Generation: Saumya Debray
PDF
No ratings yet
CSC 453 Intermediate Code Generation: Saumya Debray
42 pages
BCS 324 Topic 5
PDF
No ratings yet
BCS 324 Topic 5
35 pages
Compiler - Three Addr Codes
PDF
No ratings yet
Compiler - Three Addr Codes
33 pages
C Ompiler Theory: (Intermediate C Ode Generation - Abstract S Yntax + 3 Address C Ode)
PDF
No ratings yet
C Ompiler Theory: (Intermediate C Ode Generation - Abstract S Yntax + 3 Address C Ode)
32 pages
Intermediate
PDF
No ratings yet
Intermediate
29 pages
Santanu Jana (28) CSE AI (CA1)
PDF
No ratings yet
Santanu Jana (28) CSE AI (CA1)
7 pages
Unit-5 Compiler Design - Code Generation
PDF
No ratings yet
Unit-5 Compiler Design - Code Generation
42 pages
Unit-Iv: Intermediate Code Generation
PDF
No ratings yet
Unit-Iv: Intermediate Code Generation
19 pages
CCBYPMM
PDF
No ratings yet
CCBYPMM
16 pages
Chapter 6 - Intermediate Code Generation
PDF
No ratings yet
Chapter 6 - Intermediate Code Generation
42 pages
Lecture Notes Compiler Design Chapter-6
PDF
No ratings yet
Lecture Notes Compiler Design Chapter-6
55 pages
CD Unoit 3
PDF
No ratings yet
CD Unoit 3
16 pages
Chapter 6
PDF
No ratings yet
Chapter 6
28 pages
Chapter - 4: Semantic Analysis
PDF
No ratings yet
Chapter - 4: Semantic Analysis
36 pages
Intermediate Representation and Symbol Table
PDF
No ratings yet
Intermediate Representation and Symbol Table
39 pages
Lecture 7 - Intermediate Code Generation
PDF
No ratings yet
Lecture 7 - Intermediate Code Generation
47 pages
CH06
PDF
No ratings yet
CH06
28 pages
Code Generation
PDF
No ratings yet
Code Generation
43 pages
CD - Chp6 Updated
PDF
No ratings yet
CD - Chp6 Updated
34 pages
CD Module 8 Print
PDF
No ratings yet
CD Module 8 Print
58 pages
2024 CD Ch06 Intermidiate & Ch07 Runtime & Ch08 Code Optimization
PDF
No ratings yet
2024 CD Ch06 Intermidiate & Ch07 Runtime & Ch08 Code Optimization
29 pages
CH-6 Intermediate Code Generator
PDF
No ratings yet
CH-6 Intermediate Code Generator
54 pages
Intermediate Code Generation: C.Naga Raju Professor Department of CSE YSR Engineering College of YVU Proddatur
PDF
No ratings yet
Intermediate Code Generation: C.Naga Raju Professor Department of CSE YSR Engineering College of YVU Proddatur
108 pages
Unit 5 - Intermediate Code Generation
PDF
No ratings yet
Unit 5 - Intermediate Code Generation
18 pages
PYQs Unit 3 CD
PDF
No ratings yet
PYQs Unit 3 CD
34 pages
Symmetric Ciphers
PDF
No ratings yet
Symmetric Ciphers
79 pages
Poc Syntax Directed
PDF
No ratings yet
Poc Syntax Directed
26 pages
Gmail - Infosys Springboard Pragati Path To Future - Cohort 4 - Jan'25 - Apr'25 - Registrations Are Now Open!
PDF
No ratings yet
Gmail - Infosys Springboard Pragati Path To Future - Cohort 4 - Jan'25 - Apr'25 - Registrations Are Now Open!
2 pages
Crypto Intro 2006
PDF
No ratings yet
Crypto Intro 2006
14 pages