1 / 35

Modern Compilers

Explore the structure and process of modern compilers, from lexical analysis to code generation and optimization. Learn about front-end and back-end components translating source code to target language efficiently.

sacker
Télécharger la présentation

Modern Compilers

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. Modern Compilers

  2. Modern Compilers • Compilers have not changed a great deal since the days of Backus. They still consist of two main components: • The front-end reads in the program in the source languages, makes sense of it, and stores it in an internal representation…

  3. And the back-end, which converts the internal representation into the target language, perhaps with optimizations. The target language used is typically an assembly language, but it is often easier to use a more established, higher-level language.

  4. Source Language Structure of a Compiler ? Target Language

  5. Source Language Structure of a Compiler Front End Intermediate Code Back End Target Language

  6. Source Language Structure of a Compiler Lexical Analyzer Front End Syntax Analyzer Semantic Analyzer Int. Code Generator Intermediate Code Back End Target Language

  7. Source Language Structure of a Compiler Lexical Analyzer Front End Syntax Analyzer Semantic Analyzer Int. Code Generator Intermediate Code Code Optimizer Back End Target Code Generator Target Language

  8. Lexical Analysis In a compiler linear analysis is called lexical analysis or scanning . for example in lexical analysis the characters in the assignment statement. Position =initial+Rate*60

  9. Lexical Analysis (cont’d) Would be grouped into the following tokens. • The identification position • The assignment symbol:= • The identifier initial • The plus sign • The identifier rate • The multiplication sign • The number 60

  10. Lexical Analysis (cont’d) The blanks separating the characters of these tokens would normally be eliminated during lexical analysis.

  11. Syntax Analysis Hierarchical analysis is called parsing or syntax analysis. It involves grouping the token of the source program into grammatical phrases that are used by the compiler to synthesize the output. Usually the grammatical phrases of the source program are represented by a parse tree.

  12. Semantic Analysis The semantic analysis phase checks the source programme for semantic errors and gathers type information for the subsequent code generation phase. it uses the hierarchical structure determined by the syntax analysis phase to identify the operators and operands of expressions and statements.

  13. Semantic Analysis (cont’d) An important component of semantic analysis is type checking hence the compiler checks that each operator has operands that are permitted by the source language specification. For example many programming language definitions require a compiler to report an error every time a real number is used to index an array

  14. Intermediate Code Generation After syntax and semantic analysis some compilers generate an explicit intermediate representation of the source programme this intermediate representation should have two important properties it should be easy to produce and easy to translate into the target programme .

  15. Intermediate Code Generation (cont’d) We consider an intermediate form called Three address code which is like the assembly language for a machine in which every memory location can act like a register. “These address code consists of a sequence of instructions each of which has at most three operands. The source programme in three address code is:

  16. Intermediate Code Generation (cont’d) • Temp1 = Into real (60) • Temp2 =Id3 * Temp1 • Temp3 = Id2+ Temp2 • Id1 = Temp3

  17. Intermediate Code Generation (cont’d) This intermediate form has several properties ; Each three address instruction has at most one operator in addition to the assignment thus when generating these instructions the compiler has to decide on the order in which operations are to be done; the multiplication precedes the addition in source programme

  18. Intermediate Code Generation (cont’d) The compiler must generate a temporary name to hold the value computed by each instruction. Some “Three address” instruction have fewer than three operands e.g. the first and last instruction.

  19. Code Optimization The code optimization function phase attempts to improve the intermediate code so that faster running machine code will result. Some optimizations are trivial (of very little importance) for example a natural algorithm generates the intermediate code using an instruction for each operator in the tree representation after semantic analysis even though there is a better way to perform the same calculation using two instructions

  20. Code Optimization (cont’d) Temp 1 = id3 *60.0 Id1 = id2 + temp 1

  21. Code Generation The final phase of the compiler is the generation of target code constructing normally of relocatable m/c code or assembly code memory locations are selected for each variables used by the program. A crucial aspect is the assignment of variables to registers.

  22. Code Generation (cont’d) For example using registers 1 and 2 the translation of the code might become MOVF id 3, R2 MULF # 60.0,R2 MOVF id2,R1 ADDF R2,R1 MOVF R1, ID 1

  23. Code Generation (cont’d) The first and the 2nd operands of each instruction specify a source and destination respectively . The F in each instruction tells us that instruction deal with floating point numbers.

  24. Code Optimizer Target Code Generator Example Compilation Source Language Lexical Analyzer Source Code: Position = Initial + Rate * 60 Syntax Analyzer Semantic Analyzer Int. Code Generator Intermediate Code Target Language

  25. Code Optimizer Target Code Generator Example Compilation Source Language Lexical Analyzer Syntax Analyzer Semantic Analyzer Source Code: Position = Initial + Rate * 60 Lexical Analysis: ID(1)ASSIGN ID(2)ADD ID(3)MULT INT(60) Int. Code Generator Intermediate Code Target Language

  26. Code Optimizer Target Code Generator Example Compilation Source Language Lexical Analyzer Syntax Analyzer Semantic Analyzer Source Code: Position = Initial + Rate * 60 Lexical Analysis: ID(1)ASSIGN ID(2)ADD ID(3)MULT INT(60) Syntax Analysis: ASSIGN ID(1) ADD ID(2) MULT ID(3) INT(60) Int. Code Generator Intermediate Code Target Language

  27. Syntax Analysis: ASSIGN ID(1) ADD ID(2) MULT ID(3) INT(60) Sematic Analysis: ASSIGN ID(1) ADD ID(2) MULT ID(3) int2real INT(60) Code Optimizer Target Code Generator Example Compilation Source Language Lexical Analyzer Syntax Analyzer Semantic Analyzer Int. Code Generator Intermediate Code Target Language

  28. Sematic Analysis: ASSIGN ID(1) ADD ID(2) MULT ID(3) int2real INT(60) Intermediate Code: temp1 = int2real(60) temp2 = id3 * temp1 temp3 = id2 + temp2 id1 = temp3 Code Optimizer Target Code Generator Example Compilation Source Language Lexical Analyzer Syntax Analyzer Semantic Analyzer Int. Code Generator Intermediate Code Target Language

  29. Code Optimizer Target Code Generator Example Compilation Source Language Lexical Analyzer Syntax Analyzer Semantic Analyzer Intermediate Code: temp1 = int2real(60) temp2 = id3 * temp1 temp3 = id2 + temp2 id1 = temp3 Optimized Code (step 0): temp1 = int2real(60) temp2 = id3 * temp1 temp3 = id2 + temp2 id1 = temp3 Int. Code Generator Intermediate Code Target Language

  30. Code Optimizer Target Code Generator Example Compilation Source Language Lexical Analyzer Syntax Analyzer Semantic Analyzer Intermediate Code: temp1 = int2real(60) temp2 = id3 * temp1 temp3 = id2 + temp2 id1 = temp3 Optimized Code (step 1): temp1 = 60.0 temp2 = id3 * temp1 temp3 = id2 + temp2 id1 = temp3 Int. Code Generator Intermediate Code Target Language

  31. Code Optimizer Target Code Generator Example Compilation Source Language Lexical Analyzer Syntax Analyzer Semantic Analyzer Intermediate Code: temp1 = int2real(60) temp2 = id3 * temp1 temp3 = id2 + temp2 id1 = temp3 Optimized Code (step 2): temp2 = id3 * 60.0 temp3 = id2 + temp2 id1 = temp3 Int. Code Generator Intermediate Code Target Language

  32. Code Optimizer Target Code Generator Example Compilation Source Language Lexical Analyzer Syntax Analyzer Semantic Analyzer Intermediate Code: temp1 = int2real(60) temp2 = id3 * temp1 temp3 = id2 + temp2 id1 = temp3 Optimized Code (step 3): temp2 = id3 * 60.0 id1 = id2 + temp2 Int. Code Generator Intermediate Code Target Language

  33. Code Optimizer Target Code Generator Example Compilation Source Language Lexical Analyzer Syntax Analyzer Semantic Analyzer Intermediate Code: temp1 = int2real(60) temp2 = id3 * temp1 temp3 = id2 + temp2 id1 = temp3 Optimized Code: temp1 = id3 * 60.0 id1 = id2 + temp1 Int. Code Generator Intermediate Code Target Language

  34. Code Optimizer Target Code Generator Example Compilation Source Language Lexical Analyzer Syntax Analyzer Intermediate Code: temp1 = int2real(60) temp2 = id3 * temp1 temp3 = id2 + temp2 id1 = temp3 Optimized Code: temp1 = id3 * 60.0 id1 = id2 + temp1 Target Code: MOVF id3, R2 MULF #60.0, R2 MOVF id2, R1 ADDF R2, R1 MOVF R1, id1 Semantic Analyzer Int. Code Generator Intermediate Code Target Language

  35. THANKS

More Related