1 / 11

Compiler/fortolker struktur

Lexikalsk- analyse. Syntax- analyse. Semantik- analyse. Mellemk.- optimering. Kode- generering. Kode- optimering. Compiler/fortolker struktur. - - - - - - - Front end - - - - - - - - -. Mellem- kode. - - - - - - - Back end - - - - - - - - -. Mellemkode. Tree-based repræsentation.

joie
Download Presentation

Compiler/fortolker struktur

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. Lexikalsk- analyse Syntax- analyse Semantik- analyse Mellemk.- optimering Kode- generering Kode- optimering Compiler/fortolker struktur - - - - - - - Front end - - - - - - - - - Mellem- kode - - - - - - - Back end - - - - - - - - -

  2. Mellemkode. Tree-based repræsentation. Mellemkode som et parsertræ. Three-address koder: A:= b + c a= 0 b= 2 l1: ifz b goto l2 a= a + b b= b – 1 goto l1 l2: int main () { int a=0,b=2; while (b) { a= a+b; b--; } }

  3. Three-address kodeoptimering. • Find basic blokke. • Optimer hver basic blok: • Commom sub-expression • Copy propagation • Dead-code elimination • Arithmetic transform • Constant folding • Algebraic transform • Reduction in strength • Packing af mellemvariable. • Loop optimering.

  4. Basic blokke • Blok start: • Første statement • Label, der hoppes til • Statement efter et branch a= 0 b= 2 l1: ifz b goto l2 a= a + b b= b – 1 goto l1 l2:

  5. Basic blok optimering. • Commom sub-expression Samme beregning flere gange. • Copy propagation Samme værdi i flere variable. Erstat alle med en. • Dead-code elimination Variable, der ikke bruges, slettes. • Arithmetic transform • Constant folding Beregn konstanter • Algebraic transform + 0, - 0, * 1 og /1 • Reduction in strength Fx: 2*x -> x+x • Packing af mellemvariable Frigiv overflødig plads til mellemvariable.

  6. Commom sub-expression b := 4 - 2 t1 := b / 2 t2 := a * t1 t3 := t2 * b t4 := t3 + c t5 := t2 * b t6 := t5 + c d := t4 * t6 b := 4 - 2 t1 := b / 2 t2 := a * t1 t3 := t2 * b t4 := t3 + c t5 := t3 t6 := t5 + c d := t4 * t6

  7. Copy propagation b := 4 - 2 t1 := b / 2 t2 := a * t1 t3 := t2 * b t4 := t3 + c t5 := t3 t6 := t5 + c d := t4 * t6 b := 4 - 2 t1 := b / 2 t2 := a * t1 t3 := t2 * b t4 := t3 + c t5 := t3 t6 := t3 + c d := t4 * t6 b := 4 - 2 t1 := b / 2 t2 := a * t1 t3 := t2 * b t4 := t3 + c t5 := t3 t6 := t4 d := t4 * t4

  8. Dead-code elimination b := 4 - 2 t1 := b / 2 t2 := a * t1 t3 := t2 * b t4 := t3 + c t5 := t3 t6 := t4 d := t4 * t4 b := 4 - 2 t1 := b / 2 t2 := a * t1 t3 := t2 * b t4 := t3 + c d := t4 * t4

  9. Arithmetic transform b := 4 - 2 t1 := b / 2 t2 := a * t1 t3 := t2 * b t4 := t3 + c d := t4 * t4 b := 2 t1 := b / 2 t2 := a * t1 t3 := t2 * b t4 := t3 + c d := t4 * t4 t1 := 1 t2 := a * t1 t3 := t2 * 2 t4 := t3 + c d := t4 * t4 t2 := a t3 := t2 * 2 t4 := t3 + c d := t4 * t4

  10. 1 2a 2b 3 4 5 6 Loop optimering. 1 2 3 4 5 6

  11. Kodegenerering. Globale og static variable: Når navn indsættes i symboltabellen så SymAdr= Nextstatic; Nextstatic=+ VarSize; Lokal variable: Hvis første: NextLokal= 0; Ellers: SymAdr= NextLokal; NextLokal=+ VarSize; a= 0 b= 2 l1: ifz b goto l2 a= a + b b= b – 1 goto l1 l2: For hver linie i mellemkode: Generer koden vha informationer fra symboltabellen. Labels: Anvendelse: Kik i symboltabellen om den er defineret ellers gem i tabellen, hvor den bruges. Oprettelse: Labeladr= LC. Hvis gemte anvendelser, så sæt referencer til LabelAdr.

More Related