1 / 74

Intermediate Code Generation

Intermediate Code Generation. Chapter 6. Bac k -end and Fron t -end of A Com piler. Bac k -end and Fron t -end of A Com piler. Cl ose t o sou r c e lan g uage (e . g. s y n t a x t r e e ). Cl ose t o machine l an g uage.

rahim-moses
Download Presentation

Intermediate Code Generation

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. Intermediate Code Generation Chapter 6

  2. Back-endandFront-end ofACompiler

  3. Back-endandFront-end ofACompiler Closetosource language (e.g.syntaxtree) Closetomachinelanguage mxncompilerscanbebuiltby writingjust mfrontends andnback ends

  4. Back-endandFront-end ofACompiler • Includes: • Typechecking • Any syntactic checksthat remain afterparsing • (e.g. ensurebreak statementisenclosedwithin while-,for-,orswitchstatements).

  5. Component-BasedApproachtoBuildingCompilers Sourceprogram inLanguage-1 Sourceprogram inLanguage-2 Non-optimizedIntermediateCode OptimizedIntermediate Code Target-2 machinecode Target-1 machinecode

  6. IntermediateRepresentation(IR) Akind ofabstractmachinelanguagethat canexpress thetargetmachine operations withoutcommitting totoo much machine details. :MWhyIR?

  7. WithoutIR C Pascal FORTRAN C++ SPARC HPPA x86 IBM PPC

  8. WithIR C SPARC Pascal HPPA IR FORTRAN x86 IBM PPC C++

  9. WithIR C ? Pascal CommonBackend IR FORTRAN C++

  10. Advantagesof UsinganIntermediateLanguage Retargeting-Builda compilerforanewmachine by attaching anew codegeneratorto an existing front-end. Optimization-reuse intermediate code optimizers in compilers fordifferentlanguages and different machines. Note:the terms “intermediate code”,“intermediate language”, and “intermediate representation”are all used interchangeably.

  11. IssuesinDesigninganIR • Whether touseanexisting IR • if targetmachine architecture is similar • if the new language is similar • Whether theIRis appropriate for the kind ofoptimizations tobeperformed • e.g. speculationandpredication • sometransformations maytake muchlongerthanthey wouldona different IR

  12. IssuesinDesigninganIR • Designing anewIRneeds to consider • Level(how machine dependent it is) • Structure • Expressiveness • Appropriateness for general andspecial optimizations • Appropriateness forcodegeneration • Whethermultiple IRs should be used

  13. Multiple-Level IR Target code High-level IR Low-level IR SourceProgram … Semantic Check High-level Optimization Low-level Optimization

  14. UsingMultiple-levelIR • Translating from onelevelto another inthe compilationprocess • Preserving anexisting technology investment • Some representationsmaybe more appropriate foraparticular task.

  15. Commonly UsedIR • :MPossibleIR forms • Graphical representations:such as syntaxtrees,AST (Abstract SyntaxTrees),DAG • Postfixnotation • Threeaddresscode • SSA(StaticSingle Assignment) form • :MIR should haveindividual components thatdescribe simplethings

  16. SyntaxTree

  17. SyntaxTree + + * a * - b c d a - b c

  18. SyntaxTree + + * a * - b c d a - b c

  19. SyntaxTree + + * a * - b c d a - b c Nodecan have morethan one parent • DirectedAcyclicGraph (DAG): • Morecompactrepresentation • Givescluesregardinggenerationofefficient code

  20. Example Construct the DAG for:

  21. HowtoGenerateDAGfrom Syntax-DirectedDefinition? Allwhat is neededis that functionssuchasNode andLeaf abovecheck whethera nodealready exists.Ifsucha nodeexists, a pointer isreturnedtothatnode.

  22. HowtoGenerateDAGfrom Syntax-DirectedDefinition?

  23. DataStructure:Array

  24. DataStructure:Array Leaves Leftandrightchildren ofanintermediatenode Operationcode Scanningthearray eachtimea newnodeis needed,is not anefficientthingtodo.

  25. DataStructure:HashTable Hashfunction=h(op,L,R)

  26. Three-AddressCode • Anotheroptionforintermediatepresentation • Builtfromtwoconcepts: • –addressesandinstructions • Atmostoneoperator

  27. Address • Canbeone ofthe following: • Aname: source program name • Aconstant • Compiler-generatedtemporary

  28. Instructions Procedure callsuch as p(x1, x2,…,xn)is implementedas:

  29. Example OR

  30. Choiceof OperatorSet • Rich enoughto implementthe • operationsofthe sourcelanguage • Closeenough to machine instructionsto • simplifycodegeneration

  31. DataStructure • Howto present these instructionsin a • datastructure? • Quadruples • Triples • Indirecttriples

  32. DataStructure: Quadruples • Hasfourfields:op, arg1, arg2,result • Exceptions: • Unaryoperators:noarg2 • Operatorslikeparam:noarg2,noresult • (Un)conditionaljumps: targetlabelis the • result

  33. op arg1 arg2 result tl= tz= b *tt t3= minusct4= b *t3 t6= tz+ t4 a = t5 minusc 0 1 2 3 4 5 = I tsI I

  34. DataStructure: Triples • Onlythree fields:noresultfield • Results referredtoby itsposition

  35. DataStructure: IndirectTriples • Wheninstructions are movingaround duringoptimizations: quadruples are betterthantriples. • Indirecttriplessolvethisproblem Optimizingcompliercanreorderinstruction list, insteadofaffectingthetriplesthemselves List ofpointerstotriples

  36. Single-Static-Assignment(SSA) • Isan intermediatepresentation • Facilitatescertaincode optimizations • Allassignmentsare tovariableswith distinct names

  37. Single-Static-Assignment(SSA) Example: Ifwe usedifferent names for Xintruepartand false part,thenwhichname shall weuseinthe assignmentof y = x* a? Theanswer is:Ø-function Returnsthevalueofits argument that correspondstothe control-flow paththat was takentoget totheassignment statementcontainingtheØ-function

  38. Example

  39. TypesandDeclarations • Typechecking:to ensurethat typesof operands matchthe type expectedby operator • Determinethe storage needed • Calculate the address ofanarray reference • Insertexplicittypeconversion • Choose the writeversionofanoperator • …

  40. StorageLayout • From thetype,we candetermine • amount of storage at runtime. • Atcompiletime, we will use thisamount • to assign its namea relativeaddress. • Typeandrelativeaddressare saved in • thesymboltable entry of thename. • Datawith lengthdeterminedonly at run timesavesa pointer inthe symboltable.

  41. StorageLayout • Multibyteobjectsarestoredin consecutivebytesandgiven the address of thefirstbyte • Storageforaggregates(e.g. arrays and classes)isallocatedinonecontiguous block ofbytes.

  42. B

  43. offset = offset+T.width; } D€

  44. Tokeeptrack ofthenext availablerelative address Create asymbol tableentry

  45. TranslationsofStatements andExpressions Syntax-DirectedDefinition (SDD) Syntax-DirectedTranslation (SDT)

  46. Addressholdingvalue of E (e.g.tmp variable,name,constant) Three-address codeofE Buildan instruction Getatemporary variable Current symbol table

  47. a=b+-c • t1 = minusc t2;b+tl a = t2

  48. Generating three-address codeincrementally toavoidlongstrings manipulations • gen() does twothings: • generate threeaddressinstruction • •appenditto the sequenceof • instructionsgeneratedsofar

  49. Arrays • Elementsof thesametime • Storedconsecutively inmemory • Inlanguageslike C or Java elements are:0, 1, …, n-1 • Insomeotherlanguages: • low, low+1, …, high

More Related