1 / 38

Code generation and data types

Code generation and data types. Translation and Address Calculation. Outline. Intermediate codes 3-address code P-code Code generation, using attribute grammar Data types Primitive data types Arrays and Strings Records Address calculation. Intermediate Codes. 3-address code P-code.

hicks
Download Presentation

Code generation and data types

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Code generation and data types Translation and Address Calculation

  2. Outline • Intermediate codes • 3-address code • P-code • Code generation, using attribute grammar • Data types • Primitive data types • Arrays and Strings • Records • Address calculation 2301380 Chapter 6 Code Generation and Data Types

  3. Intermediate Codes 3-address code P-code 2301380 Chapter 6 Code Generation and Data Types

  4. 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 2301380 Chapter 6 Code Generation and Data Types

  5. 3-address code • 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. • Labels can be assigned to a location. • Operators can be: • Arithmetic operators (3-address) • Relational operators (3-address) • Conditional operators (2-address) • If_false … goto … • Jump (1-address) • Input/Output • Halt 2301380 Chapter 6 Code Generation and Data Types

  6. Example: 3-address code 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 2301380 Chapter 6 Code Generation and Data Types

  7. 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 2301380 Chapter 6 Code Generation and Data Types

  8. P-code Instructions • 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 2301380 Chapter 6 Code Generation and Data Types

  9. Example: P-code read (x); loada x read if x>0 then loadv x loadc 0 greater jumpONfalse L1 { fact:=1; loada fact loadc 1 store repeat Label L2 { fact:=fact*x; loada fact loadv fact loadv x mult store x:=x-1; loada x loadv x loadc 1 sub store } until x==0; loadv x loadc 0 equ jumpF L2 write(fact); loadv fact write } Label L1 stop 2301380 Chapter 6 Code Generation and Data Types

  10. Code Generation Using Attribute Grammar 2301380 Chapter 6 Code Generation and Data Types

  11. Code Generation Using Synthesized 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 up to their parent node to construct a larger chunk of code. 2301380 Chapter 6 Code Generation and Data Types

  12. Attribute Grammar: P-code 2301380 Chapter 6 Code Generation and Data Types

  13. Generating P-code: Example exp loada x loadv x loadc 3 adi stn loadc 4 adi aexp loada x loadv x loadc 3 adi stn loadc 4 factor aexp + 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 2301380 Chapter 6 Code Generation and Data Types

  14. Attribute Grammar: 3-address code 2301380 Chapter 6 Code Generation and Data Types

  15. Generating 3-address Code: Example t2 t1=x+3 x=t1 t2=t1+4 aexp t1 t1=x+3 x=t1 4 aexp factor + 4 t1 t1=x+3 x=t1 factor num t1 t1=x+3 x=t1 exp ) ( t1 t1=x+3 exp id = t1 t1=x+3 aexp x 3 + aexp factor x factor num id 2301380 Chapter 6 Code Generation and Data Types

  16. Code Generation: Tree Traversal proceduregenCode (T:node) { if (T != null) then { generate code to prepare for code of left child; genCode(T.leftChild); generate code to prepare for code of right child; genCode(T.rightChild); generate code to implement the action of T; } } 2301380 Chapter 6 Code Generation and Data Types

  17. Code Generation: P-code 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”); } } } 2301380 Chapter 6 Code Generation and Data Types

  18. Control Statements: code generation If statements While loops Logical expressions 2301380 Chapter 6 Code Generation and Data Types

  19. Code Generation for If Statements 3-address code P-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 <code evaluating E> jumpF L1 <code for S1> jump L2 label L1 <code for S2> label L2 IF ( E ) S1 ELSE S2 2301380 Chapter 6 Code Generation and Data Types

  20. Code Generation for While Loops 3-address code P-code label L1 <code evaluating E and assigning to t1> if_false t1 goto L2 <code for S> goto L1 label L2 label L1 <code evaluating E> jumpF L2 <code for S> jump L1 label L2 WHILE ( E ) S 2301380 Chapter 6 Code Generation and Data Types

  21. Generating Labels label L1 <code for E> jumpFL2 <code for S> jump L1 labelL2 • 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. 2301380 Chapter 6 Code Generation and Data Types

  22. Code Generation for Logical Expressions • Data types and operators • Use Boolean data type and operators if included in the target/intermediate language • Use integer 0/1 and bitwise operators • 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_falseagoto L1 <code for b> if_falseb goto L1 <code for S> Label L1 • Intermediate code for IF a OR b THEN S <code for a> if_falseagoto L1 goto L2 Label L1 <code for b> if_falseb goto L3 Label L2 <code for S> Label L3 2301380 Chapter 6 Code Generation and Data Types

  23. Data Types Primitive data types Strings Arrays Records 2301380 Chapter 6 Code Generation and Data Types

  24. Primitive Data Types • Integer • Short, long, signed, unsigned • Floating Point • IEEE floating point format • Decimal • Boolean • Character • ASCII, Unicode 2301380 Chapter 6 Code Generation and Data Types

  25. Ordinal Types • Integers • User-defined ordinal types: range of possible values can be associated with I+ • Enumeration types • user-defined set of possible values. • Subrange types • Ordered contiguous subsequence of an ordinal type. 2301380 Chapter 6 Code Generation and Data Types

  26. Strings • Can be either • Primitive type: Ada, FORTRAN, Basic • Array: Pascal, C, C++ • Class: Java (String) • Operations • Assignment • Comparison • Catenation • Substring reference • Pattern Matching 2301380 Chapter 6 Code Generation and Data Types

  27. Descriptor for Strings • Descriptor is the information stored in a compiler. • Static string • Type: static string • Length • Address • Limited dynamic string • Type: dynamic string • Length: maximum and current length • Address 2301380 Chapter 6 Code Generation and Data Types

  28. Arrays • Types of subscripts • Implementation 2301380 Chapter 6 Code Generation and Data Types

  29. Subscripts Types of subscripts Range of subscripts • Integers • FORTRAN, C, Java • Ordinal types • Enumeration types • Pascal, Ada • Static • C, FORTRAN • Java Arrays • Dynamic • Ada • FORTRAN ALLOCATBLE • Perl, JavaScript • Java ArrayList 2301380 Chapter 6 Code Generation and Data Types

  30. Implementation • Number of subscripts • Most languages have no limit, but some versions of FORTRAN have. • Descriptor • Type: Array • Element type • Index type • Number of dimensions • Index ranges, for each dimension • Address 2301380 Chapter 6 Code Generation and Data Types

  31. Array: Address calculation s Address of A[i, j] = s + ((i-1)n + j)*size(elementType) 2301380 Chapter 6 Code Generation and Data Types

  32. Records • heterogeneous aggregate of data elements in which the individual elements are identified by names. • Example: record student { int id; char firstname[40]; char lastname[40]; float grade; } 2301380 Chapter 6 Code Generation and Data Types

  33. Implementation • Descriptor • Type: record • For each field: • Name, type, offset • Address • Address Calculation • S + field offset Field 1 Field n 2301380 Chapter 6 Code Generation and Data Types

  34. Address Calculation Addressing operations Array reference Record reference 2301380 Chapter 6 Code Generation and Data Types

  35. Addressing Operations 3-address code P-code • Address of x • &x • Indirect address • *x • Address of x • loada x • Indirect load • ind x • (load *(top+x)) • Indexed address • ixa x • (load top*x+(top-1)) 2301380 Chapter 6 Code Generation and Data Types

  36. Array References 3-address code P-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 • x=a[i] loada x loada a loadvi ixaelesize(a) ind 0 sto • a[i]=x loada a loadvi ixaelesize(a) loadv x sto Find offset Find address Find address Find content Find offset Find address Find address 2301380 Chapter 6 Code Generation and Data Types

  37. More Complex Array References 3-address code P- 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 • a[i+1]=a[j+2]+3 loada a loadvi loadc 1 adi ixaelesize(a) loada a loadv j loadc 2 adi ixaelesize(a) ind 0 loadc 3 adi sto push &a[i+1] t4=a[t1] push a[j+2] a[t6]=t5 2301380 Chapter 6 Code Generation and Data Types

  38. Record References 3-address code P-code • Address of a field t1=&x+offset(x,j) • Content of a field t1=&x+offset(x,j) t2=*t1 • 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 2301380 Chapter 6 Code Generation and Data Types

More Related