1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 33: Code Generation and Linking COMP 144 Programming Language Concepts Spring 2002.

Download 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 33: Code Generation and Linking COMP 144 Programming Language Concepts Spring 2002.

Post on 19-Dec-2015

214 views

Category:

Documents

0 download

TRANSCRIPT

  • Slide 1
  • 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 33: Code Generation and Linking COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos April 15 The University of North Carolina at Chapel Hill
  • Slide 2
  • 2 COMP 144 Programming Language Concepts Felix Hernandez-Campos Phases of Compilation The first three phases are language- dependentThe first three phases are language- dependent The last two are machine- dependentThe last two are machine- dependent The middle two dependent on neither the language nor the machineThe middle two dependent on neither the language nor the machine
  • Slide 3
  • 3 COMP 144 Programming Language Concepts Felix Hernandez-Campos Example program gcd(input, output); var i, j: integer; begin read(i, j); while i j do if i > j then i := i j; else j := j i; writeln(i)end. program gcd(input, output); var i, j: integer; begin read(i, j); while i j do if i > j then i := i j; else j := j i; writeln(i)end.
  • Slide 4
  • 4 COMP 144 Programming Language Concepts Felix Hernandez-Campos Example Control Flow Graph Basic blocks are maximal-length set of sequential operationsBasic blocks are maximal-length set of sequential operations Operations on a set of virtual registers Unlimited A new one for each computed value Arcs represent interblock control flowArcs represent interblock control flow
  • Slide 5
  • 5 COMP 144 Programming Language Concepts Felix Hernandez-Campos Code Generation We will illustrate the back-end with a simple compilerWe will illustrate the back-end with a simple compiler There is not control flow graphThere is not control flow graph No optimizations Two main tasks:Two main tasks: Register allocation Instruction Scheduling
  • Slide 6
  • 6 COMP 144 Programming Language Concepts Felix Hernandez-Campos Code Generation Example program gcd(input, output); var i, j: integer; begin read(i, j); while i j do if i > j then i := i j; else j := j i; writeln(i)end. program gcd(input, output); var i, j: integer; begin read(i, j); while i j do if i > j then i := i j; else j := j i; writeln(i)end. Compilation
  • Slide 7
  • 7 COMP 144 Programming Language Concepts Felix Hernandez-Campos Reserved registers: a1, a2, sp and rvReserved registers: a1, a2, sp and rv General purpose: r1..rkGeneral purpose: r1..rk Expression code generation: (a+b) (c (d/e)Expression code generation: (a+b) (c (d/e) Ri used as expression evaluation stack
  • Slide 8
  • 8 COMP 144 Programming Language Concepts Felix Hernandez-Campos Code Generation Example Syntax Tree and Symbol Table
  • Slide 9
  • 9 COMP 144 Programming Language Concepts Felix Hernandez-Campos Code Generation Example Attribute Grammar stmt nodes Code generation
  • Slide 10
  • 10 COMP 144 Programming Language Concepts Felix Hernandez-Campos Code Generation Example
  • Slide 11
  • 11 COMP 144 Programming Language Concepts Felix Hernandez-Campos Code Generation Example Allocation of next register
  • Slide 12
  • 12 COMP 144 Programming Language Concepts Felix Hernandez-Campos Code Generation Example Register Spill
  • Slide 13
  • 13 COMP 144 Programming Language Concepts Felix Hernandez-Campos Code Generation Example
  • Slide 14
  • 14 COMP 144 Programming Language Concepts Felix Hernandez-Campos Code Generation Example
  • Slide 15
  • 15 COMP 144 Programming Language Concepts Felix Hernandez-Campos Code Generation Example
  • Slide 16
  • 16 COMP 144 Programming Language Concepts Felix Hernandez-Campos Address Space Organization Assemblers, linker and loader typically operate on a pair of related file formats:Assemblers, linker and loader typically operate on a pair of related file formats: Relocatable object codeRelocatable object code Input to linker Multiple file are combined to create an executable program It includes the following information: Import table (named locations with unknown addresses) Relocation table (instruction that refer to current file) Export table Imported and exported names are known as external symbols
  • Slide 17
  • 17 COMP 144 Programming Language Concepts Felix Hernandez-Campos Address Space Organization Executable object codeExecutable object code Input to loader Loaded in memory and run A running program is divided into segmentsA running program is divided into segments Code Constants Initialized data Uninitialized data Stack Heap Files (mapped to memory)
  • Slide 18
  • 18 COMP 144 Programming Language Concepts Felix Hernandez-Campos Linking Large program must be divided into separate compilation unitsLarge program must be divided into separate compilation units E.g. main program and libraries The linker is in charge of joining together those compilation unitsThe linker is in charge of joining together those compilation units Each compilation unit must a relocatable object file Linking involves two subtasksLinking involves two subtasks Relocation Resolution of external references
  • Slide 19
  • 19 COMP 144 Programming Language Concepts Felix Hernandez-Campos Relocation Two phasesTwo phases In the first phase:In the first phase: Gather all the of the compilation units Choose an order in memory and note addresses In the second phase:In the second phase: Resolve external references on each unit Modify instruction set as required Linking also involved type checking using module headersLinking also involved type checking using module headers
  • Slide 20
  • 20 COMP 144 Programming Language Concepts Felix Hernandez-Campos Linking Example External Reference Two relocatable object files are linked in this example, A and BTwo relocatable object files are linked in this example, A and B M and X are external references L and Y are internal refereces Internal Reference
  • Slide 21
  • 21 COMP 144 Programming Language Concepts Felix Hernandez-Campos Linking Example Data Segment Code Segment
  • Slide 22
  • 22 COMP 144 Programming Language Concepts Felix Hernandez-Campos Dynamic Linking In many modern systems, many programs share the same librariesIn many modern systems, many programs share the same libraries It is a waste to create a copy of each library at run-time Most operating system support dynamically linked libraries (DLLs)Most operating system support dynamically linked libraries (DLLs) Each library resides in its own code and data segment Program instances that uses the library has private copy of the data segment Program instances share a system-wide read-only copy of the the librarys code segment DLLs support backward-compatible without recompilationDLLs support backward-compatible without recompilation
  • Slide 23
  • 23 COMP 144 Programming Language Concepts Felix Hernandez-Campos Reading Assignment Read ScottRead Scott Sect. 9.3 Sect. 9.4 Sect. 9.6 Sect. 9.7 intro

Recommended

View more >