compiler construction
play

Compiler Construction Lecture 19: Code Generation V (Compiler - PowerPoint PPT Presentation

Compiler Construction Lecture 19: Code Generation V (Compiler Backend) Winter Semester 2018/19 Thomas Noll Software Modeling and Verification Group RWTH Aachen University https://moves.rwth-aachen.de/teaching/ws-1819/cc/ The Compiler Backend


  1. Compiler Construction Lecture 19: Code Generation V (Compiler Backend) Winter Semester 2018/19 Thomas Noll Software Modeling and Verification Group RWTH Aachen University https://moves.rwth-aachen.de/teaching/ws-1819/cc/

  2. The Compiler Backend Outline of Lecture 19 The Compiler Backend Register Allocation Outlook Course Evaluation 2 of 28 Compiler Construction Winter Semester 2018/19 Lecture 19: Code Generation V (Compiler Backend)

  3. The Compiler Backend Conceptual Structure of a Compiler Source code Lexical analysis (Scanner) Syntax analysis (Parser) Semantic analysis Generation of intermediate code Code optimisation Generation of target code Target code 3 of 28 Compiler Construction Winter Semester 2018/19 Lecture 19: Code Generation V (Compiler Backend)

  4. The Compiler Backend The Compiler Backend Final step: translation of (optimised) abstract machine code into “real” machine code (possibly followed by assembling phase) 4 of 28 Compiler Construction Winter Semester 2018/19 Lecture 19: Code Generation V (Compiler Backend)

  5. The Compiler Backend The Compiler Backend Final step: translation of (optimised) abstract machine code into “real” machine code (possibly followed by assembling phase) Goal: runtime and storage efficiency • fast backend • fast and compact code • low memory requirements for and efficient access to data 4 of 28 Compiler Construction Winter Semester 2018/19 Lecture 19: Code Generation V (Compiler Backend)

  6. The Compiler Backend The Compiler Backend Final step: translation of (optimised) abstract machine code into “real” machine code (possibly followed by assembling phase) Goal: runtime and storage efficiency • fast backend • fast and compact code • low memory requirements for and efficient access to data Memory hierarchy: decreasing speed & costs • registers (program counter, data [universal/floating point/address], frame pointer, index register, condition code, ...) • cache (“fast” RAM) • main memory (“slow” RAM) • background storage (disks, sticks, ...) 4 of 28 Compiler Construction Winter Semester 2018/19 Lecture 19: Code Generation V (Compiler Backend)

  7. The Compiler Backend The Compiler Backend Final step: translation of (optimised) abstract machine code into “real” machine code (possibly followed by assembling phase) Goal: runtime and storage efficiency • fast backend • fast and compact code • low memory requirements for and efficient access to data Memory hierarchy: decreasing speed & costs • registers (program counter, data [universal/floating point/address], frame pointer, index register, condition code, ...) • cache (“fast” RAM) • main memory (“slow” RAM) • background storage (disks, sticks, ...) Principle: use fast memory whenever possible • evaluation of expressions in registers (instead of data/runtime stack) • code/procedure stack/heap in main memory 4 of 28 Compiler Construction Winter Semester 2018/19 Lecture 19: Code Generation V (Compiler Backend)

  8. The Compiler Backend The Compiler Backend Final step: translation of (optimised) abstract machine code into “real” machine code (possibly followed by assembling phase) Goal: runtime and storage efficiency • fast backend • fast and compact code • low memory requirements for and efficient access to data Memory hierarchy: decreasing speed & costs • registers (program counter, data [universal/floating point/address], frame pointer, index register, condition code, ...) • cache (“fast” RAM) • main memory (“slow” RAM) • background storage (disks, sticks, ...) Principle: use fast memory whenever possible • evaluation of expressions in registers (instead of data/runtime stack) • code/procedure stack/heap in main memory Instructions: select adequately (number/type of operands, addressing modes, ...) 4 of 28 Compiler Construction Winter Semester 2018/19 Lecture 19: Code Generation V (Compiler Backend)

  9. The Compiler Backend Code Generation Phases 1. Register allocation: registers used for – values of (frequently used) variables and intermediate results – computing memory addresses (array indexing, ...) – passing parameters to procedures/functions 2. Instruction selection: – translation of abstract instructions into (sequences of) real instructions – employ special instructions for efficiency (e.g., INC(x) rather than ADD(x,1) ) 3. Instruction scheduling (placement): increase level of parallelism and/or pipelining by smart ordering of instructions 5 of 28 Compiler Construction Winter Semester 2018/19 Lecture 19: Code Generation V (Compiler Backend)

  10. The Compiler Backend Code Generation Phases 1. Register allocation: registers used for – values of (frequently used) variables and intermediate results – computing memory addresses (array indexing, ...) – passing parameters to procedures/functions 2. Instruction selection: – translation of abstract instructions into (sequences of) real instructions – employ special instructions for efficiency (e.g., INC(x) rather than ADD(x,1) ) 3. Instruction scheduling (placement): increase level of parallelism and/or pipelining by smart ordering of instructions 5 of 28 Compiler Construction Winter Semester 2018/19 Lecture 19: Code Generation V (Compiler Backend)

  11. Register Allocation Outline of Lecture 19 The Compiler Backend Register Allocation Outlook Course Evaluation 6 of 28 Compiler Construction Winter Semester 2018/19 Lecture 19: Code Generation V (Compiler Backend)

  12. Register Allocation Register Allocation Example 19.1 Assignment: z := (u+v)-(w-(x+y)) 7 of 28 Compiler Construction Winter Semester 2018/19 Lecture 19: Code Generation V (Compiler Backend)

  13. Register Allocation Register Allocation Example 19.1 Assignment: z := (u+v)-(w-(x+y)) Target machine with r registers R 0 , R 1 , ..., R r − 1 and main memory M 7 of 28 Compiler Construction Winter Semester 2018/19 Lecture 19: Code Generation V (Compiler Backend)

  14. Register Allocation Register Allocation Example 19.1 Assignment: z := (u+v)-(w-(x+y)) Target machine with r registers R 0 , R 1 , ..., R r − 1 and main memory M Instruction types: R i := M[ a ] M[ a ] := R i R i := R i op M[ a ] R i := R i op R j (with address a ) 7 of 28 Compiler Construction Winter Semester 2018/19 Lecture 19: Code Generation V (Compiler Backend)

  15. Register Allocation Register Allocation Example 19.1 Instruction sequence ( r = 2): Assignment: R 0 := M[u] z := (u+v)-(w-(x+y)) R 0 := R 0 +M[v] Target machine with R 1 := M[x] r registers R 0 , R 1 , ..., R r − 1 R 1 := R 1 +M[y] and main memory M M[t] := R 1 R 1 := M[w] Instruction types: R i := M[ a ] R 1 := R 1 -M[t] M[ a ] := R i R 0 := R 0 -R 1 R i := R i op M[ a ] M[z] := R 0 R i := R i op R j (with address a ) 7 of 28 Compiler Construction Winter Semester 2018/19 Lecture 19: Code Generation V (Compiler Backend)

  16. Register Allocation Register Allocation Example 19.1 Instruction sequence ( r = 2): Shorter sequence: Assignment: R 0 := M[u] R 0 := M[w] z := (u+v)-(w-(x+y)) R 0 := R 0 +M[v] R 1 := M[x] Target machine with R 1 := M[x] R 1 := R 1 +M[y] r registers R 0 , R 1 , ..., R r − 1 R 1 := R 1 +M[y] R 0 := R 0 -R 1 and main memory M M[t] := R 1 R 1 := M[u] R 1 := M[w] R 1 := R 1 +M[v] Instruction types: R i := M[ a ] R 1 := R 1 -M[t] R 1 := R 1 -R 0 M[ a ] := R i R 0 := R 0 -R 1 M[z] := R 1 R i := R i op M[ a ] M[z] := R 0 R i := R i op R j (with address a ) 7 of 28 Compiler Construction Winter Semester 2018/19 Lecture 19: Code Generation V (Compiler Backend)

  17. Register Allocation Register Allocation Example 19.1 Instruction sequence ( r = 2): Shorter sequence: Assignment: R 0 := M[u] R 0 := M[w] z := (u+v)-(w-(x+y)) R 0 := R 0 +M[v] R 1 := M[x] Target machine with R 1 := M[x] R 1 := R 1 +M[y] r registers R 0 , R 1 , ..., R r − 1 R 1 := R 1 +M[y] R 0 := R 0 -R 1 and main memory M M[t] := R 1 R 1 := M[u] R 1 := M[w] R 1 := R 1 +M[v] Instruction types: R i := M[ a ] R 1 := R 1 -M[t] R 1 := R 1 -R 0 M[ a ] := R i R 0 := R 0 -R 1 M[z] := R 1 R i := R i op M[ a ] M[z] := R 0 R i := R i op R j • Reason: 2nd variant avoids intermediate storage t for x+y (with address a ) 7 of 28 Compiler Construction Winter Semester 2018/19 Lecture 19: Code Generation V (Compiler Backend)

  18. Register Allocation Register Allocation Example 19.1 Instruction sequence ( r = 2): Shorter sequence: Assignment: R 0 := M[u] R 0 := M[w] z := (u+v)-(w-(x+y)) R 0 := R 0 +M[v] R 1 := M[x] Target machine with R 1 := M[x] R 1 := R 1 +M[y] r registers R 0 , R 1 , ..., R r − 1 R 1 := R 1 +M[y] R 0 := R 0 -R 1 and main memory M M[t] := R 1 R 1 := M[u] R 1 := M[w] R 1 := R 1 +M[v] Instruction types: R i := M[ a ] R 1 := R 1 -M[t] R 1 := R 1 -R 0 M[ a ] := R i R 0 := R 0 -R 1 M[z] := R 1 R i := R i op M[ a ] M[z] := R 0 R i := R i op R j • Reason: 2nd variant avoids intermediate storage t for x+y • How to compute systematically? (with address a ) 7 of 28 Compiler Construction Winter Semester 2018/19 Lecture 19: Code Generation V (Compiler Backend)

Recommend


More recommend