280 likes | 378 Views
Code Generation. How to produce intermediate or target code. Outline. Intermediate code Three-address code P-code Code generation techniques Using attribute grammar Tree traversal Macro expansion Address Calculation Code generation for control statements
E N D
Code Generation How to produce intermediate or target code
Outline • Intermediate code • Three-address code • P-code • Code generation techniques • Using attribute grammar • Tree traversal • Macro expansion • Address Calculation • Code generation for control statements • Code generation for logical expressions Code Generation
Intermediate Code • Intermediate representation for programs • Why intermediate code • Reduce amount of work if optimization is done for intermediate code • Easy for retargeting compilers • Forms of intermediate code • Abstract syntax tree • Linearization of abstract syntax tree Code Generation
Three-address code: Form of Instructions • x = y op z • x, y, and z are addresses of • Variables (perhaps temporaries) • Locations in programs • y and z must be differed from x • Operators can be: • Arithmetic operators (3-address) • Relational operators (3-address) • Conditional operators (2-address) • If_false … goto … • Jump (1-address) • Input/Output • Halt • Labels can be assigned to a location Code Generation
read (x); if x>0 then { fact:=1; repeat { fact:=fact*x; x:=x-1; } until x==0; write(fact); } read x t1=x>0 if_false t1 goto L1 fact=1 label L2 t2=fact*x fact=t2 t3=x-1 x=t3 t4=x==0 if_false t4 goto L2 write fact label L1 halt Example of 3-address Code Code Generation
P-Code • Code for a hypothetical stack machine • For Pascal compilers • No variable name is required • Instructions • Load stack • Arithmetic and relational operators • Jumps • Operations are performed on topmost values on stack • 0-address or 1-address instructions Code Generation
Load: push stack Load value Load address Load constant Store: save top of stack in memory Destructive store Nondestructive store Arithmetic operations Add Subtract Multiply Compare Greater Less equal Label Jump Unconditional jump Conditional jump I/O Read write Stop P-code Instruction Code Generation
read (x); if x>0 then { fact:=1; repeat { fact:=fact*x; x:=x-1; } until x==0; write(fact); } loada x read loadv x loadc 0 greater jumpONfalse L1 loada fact loadc 1 store label L2 loada fact loadv fact loadv x mult store loada x loadv x loadc 1 sub store loadv x loadc 0 equ jumpF L2 loadv fact write label L1 stop Example of P-code Code Generation
Code Generation Using Synthesizes Attributes • An attribute is created for the sequence of characters representing generated code. • An attribute grammar is written to generate the intermediate/target code. • The value of the attribute is passed from child nodes upto their parent node to construct a larger chunk of code Code Generation
Grammar Rules exp1 -> id = exp2 exp -> aexp aexp1 ->aexp2 + factor aexp -> factor factor -> ( exp ) factor -> num factor -> id Semantic Rules exp1.code =“loada”|| id.strval ++ exp2.code ++ ”stn” exp.code = aexp.code aexp1 .code = aexp2 .code ++ factor.code ++ “add” aexp.code = factor.code factor.code = exp.code factor.code = “loadc ” || num.strval factor.code = “loadv ” || id.strval Attribute Grammar for P-code Code Generation
Generating P-Code:Example loada x loadv x loadc 3 adi stn loadc 4 adi exp loada x loadv x loadc 3 adi stn loadc 4 adi loada x loadv x loadc 3 adi stn aexp loadc 4 aexp factor + loada x loadv x loadc 3 adi stn factor num loada x loadv x loadc 3 adi stn exp loadv x loadc 3 adi ( ) exp id = loadv x loadc 3 adi aexp loadc 3 loadv x factor aexp + loadv x num factor id Code Generation
Grammar Rules exp1-> id = exp2 exp -> aexp aexp1->aexp2+factor aexp -> factor factor -> ( exp ) factor -> num factor -> id Semantic Rules exp1.name = exp2.name; exp1.code=exp2.code++id.strval||”=“||exp2.name exp.name = aexp.name; exp.code = aexp.code exp1.name = newtemp(); aexp1.code=aexp2.code++factor.code++ aexp1.name||”=“||aexp2 .name||”+“||factor.name aexp.name=factor.name;aexp.code=factor.code factor.name =exp.name; factor.code = exp.code factor.name = num.strval; factor.code = “” factor.name = id.strval; factor.code = “” Attribute Grammar for 3-address Code Code Generation
Generating 3-address Code:Example exp t2 t1=x+3 x=t1 t2=t1+4 t2 t1=x+3 x=t1 t2=t1+4 t1 t1=x+3 x=t1 aexp 4 aexp factor + t1 t1=x+3 x=t1 4 factor num t1 t1=x+3 x=t1 exp ( ) t1 t1=x+3 exp id = t1 t1=x+3 aexp 3 x factor aexp + x factor num id Code Generation
Practical Code Generation: Tree Traversal proceduregenCode (t:node) { if (t is not null) then { generate code to prepare for code of left child; genCode(left child of t); generate code to prepare for code of right child; genCode(right child of t); generate code to implement the action of t; } } Code Generation
gencode(T:Tree) { if (T is not null) { switch (T.type) caseplusnode: { gencode(T.lchild); gencode(T.rchild); emitCode(“add”); } caseasgnnode: { emitcode(“loada”,T.strval); gencode(T.rchild); emitcode(“stn”); } caseconstnode: { emitcode(“loadc”,t.strval); } caseidnode: { emitcode(“loadv”,t.strval); } default: { emitcode(“error”); } } } Practical Code Generation for P-code Code Generation
From intermediate code to target code Example: 3-address code: x=y+n P code: loada x loadv y loadc n adi sto Macaro Expansion Code Generation
Address Calculation • Addressing operations • Array references • Record structure references Code Generation
3-address code address of x &x indirect address (content pointed by x) *x P-code address of x loada x indirect load ind x (load *(top+ x)) indexed address ixa x (load top*x+(top-1)) Addressing Operations Code Generation
3-address code x=a[i] t1=i*elesize(a) t2=&a+t1 x=*t2 a[i]=x t1=i*elesize(a) t2=&a+t1 *t2=x P-code x=a[i] loada x loada a loadv i ixa elesize(a) ind 0 sto a[i]=x loada a loadv i ixa elesize(a) loadv x sto Array References Find offset Find address Find address Find content Find offset Find address Find address Code Generation
3-address code a[i+1]=a[j+2]+3 t1=j+2 t2=t1*elesize(a) t3=&a+t2 t4=*t3 t5=t4+3 t6=i+1 t7=t6*elesize(a) t8=&a+t7 *t8=t5 P code a[i+1]=a[j+2]+3 loada a loadv i loadc 1 adi ixa elesize(a) loada a loadv j loadc 2 adi ixa elesize(a) ind 0 loadc 3 adi sto More Complex Array References t2=&a[i+1] t2=&a[i+1] t4=a[t1] t4=a[t1] a[t6]=t5 t1=a[j+2] t1=a[j+2] a[t6]=t5 Code Generation
typedef struct rec { int i; char c; int j; } Entry; Entry x; 3-address code Address of a field t1=&x+offset(x,j) Content of a field t1=&x+offset(x,j) t2=*t1 P code Address of a field loada x loadc offset(x.j) ixa 1 Content of a field loada x ind offset(x.j) x.j x.c Offset of x.j x.i base address of x Offset of x.c Structure References Code Generation
Code Generation for Control Statements • If Statements • While Statements • Logical Expressions Code Generation
IF ( E ) S1 ELSE S2 3-address code <code evaluating E and assigning to t1> if_false t1 goto L1 <code for S1> goto L2 label L1 <code for S2> label L2 P code <code evaluating E> jumpF L1 <code for S1> jump L2 label L1 <code for S2> label L2 Code Generation for If-Statements Code Generation
WHILE ( E ) S 3-address code label L1 <code evaluating E and assigning to t1> if_false t1 goto L2 <code for S> goto L1 label L2 P code label L1 <code evaluating E> jumpF L2 <code for S> jump L1 label L2 Code Generation for While-Statements Code Generation
Forward jump Label must be generated before defining the label For intermediate code Generate label at the jump instruction Store the label until the actual location is found For target code (assembly) Leave the destination address in the jump instruction When the destination address is found, go back and fill the address (backpatching) Short jump or long jump? Leave space enough for long jump If only short jump is required, the extra space is filled with nop. label L1 <code for E> jumpFL2 <code for S> jump L1 labelL2 Generating Labels Code Generation
Data types and operators Use Boolean data type and operators if included in the target/intermediate language Use integer 0/1 and bitwise opeartors Short-circuit evaluation IF a AND b THEN S If a is false, the whole exp is false there is no need to evaluate b IF a OR b THEN S If a is true, the whole exp is true there is no need to evaluate b Intermediate code for IF a AND b THEN S <code for a> if_false a goto L1 <code for b> if_false b goto L1 <code for S> Label L1 Intermediate code for IF a OR b THEN S <code for a> if_false a goto L1 goto L2 Label L1 <code for b> if_false b goto L3 Label L2 <code for S> Label L3 Code Generation for Logical Expressions Code Generation