1 / 20

Compiler/fortolker struktur

Lexikalsk- analyse. Syntax- analyse. Semantik- analyse. Mellemk.- optimering. Kode- generering. Kode- optimering. Compiler/fortolker struktur. - - - - - - - Front end - - - - - - - - -. Mellem- kode. - - - - - - - Back end - - - - - - - - -. Semantik analyse.

ismael
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. Semantik analyse • Check om brug af variable svare til typereglerne for sproget. • Check procedure-kald svare til proceduren. • Check om navne bliver brug i rette sammenhæng float p1(int x) { p1= x(); x= p1(2.4,3); }

  3. Semantik analyse Brug symboltabellen: Typisk indhold: • Navn • Adresse • Simpeltype: Funktion, char, int, float, .... • Komplextype: Pointer til symboltabellen • Hvis funktion så Returtype • Hvis funktion så Parameterliste • Brugt • Tilskrevet

  4. Semantikanalyse: fremgangsmåde varlinelist: varline varlinelist | e varline: type varlist ”;” type: FLOAT | INT | CHAR {statement 1} varlist: IDENT ”,” varlist | IDENT {statement 2} Problem: Statement 2 afvikles før statement 1. Løsning LEX/FLEX sætter CurrentType . Statement 2 sætter typen i symboltabellen.

  5. Eks. på Flex/Bison metode. float {CurrentType=FLOATT; return FLOATT;} int {CurrentType=INTT; return INTT;} char {CurrentType=CHART; return CHART;} [a-zA-Z][a-zA-Z0-9]* {if ((yylval.symptr= lookup_sym(yytext)) == NULL){ yylval.symptr= insert_sym(yytext); yylval.symptr->type= NULL; } return IDENTT;} ----------------------------------------------------- varlinelist: varline varlinelist | ; varline: type varlist ”;” ; type: FLOAT {$$=FLOATT}| INT {$$=INTT}| CHAR {$$=CHART}; varlist: IDENT ”,” varlist {$1->type= CurrentType;} | IDENT {$1->type= CurrentType;};

  6. Eks. på Flex/Bison metode. statement: ident ”=” exp {if ($1->type !=NULL){ if ($1->type || $3) error; } else error; }; exp: exp '+' exp {if ($1 != $3) error; else $$ = $1;} | .... | IDENT {$$ = $1->type;}; funktion: type IDENT ”(” parmlist ”)” funkblok {if ($2->type == NULL){ $2->type=FUNKTIONT; $2->returtype= $1; $2->parmtype= $4; } else if ($2->type != FUNKTIONT) error;};

  7. 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--; } }

  8. 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.

  9. 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:

  10. 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.

  11. 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

  12. 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

  13. 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

  14. 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

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

  16. Kodegenerering. Globale og static variable: Når type og contekst kendes: så symnote->adr= Nextstatic; Nextstatic=+ type.VarSize; Lokal variable: Hvis første: NextLokal= 0; Ellers: symnote->adr= NextLokal; NextLokal=+ type.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 symboltabellen, hvor den bruges. Oprettelse: symnote->adr= LC. Anvendelser: sæt referencer til symnote->adr.

  17. HomeControler DIGIINPUT DoreOpen : 1; DIGIOUTPUT StartFan : 1; RumLight : 2; ANAINPUT _Temperature: 1,TEMP; _Humidity : 4,HUMI; _TempSetTurnButton: 3,[10.0,30.0]; ANAOUTPUT _FanVelosity: 1,[0,100]; EXPORT _Temp_Stue; TempAlarm; IMPORT _OutsideTemp; _OutsideFugt; LightB;

  18. HomeControler SEQUENCE: Ventilation RESET= Total_stop OR Ventilation_Stop; STATE: INIT FanMotor= OFF; GO TO Ventilate WHEN LightButton; GO TO HumidityTooHigh WHEN (_Humidity > _NormalHumidity+10.0); END STATE: Ventilate FanMotor= ON; GO TO INIT WHEN DELAY(NOT light, 60.0); END STATE: HumidityTooHigh FanMotor= ON; GO TO INIT WHEN _Humidity < _NormalHumidity; GO TO Pause WHEN DELAY(HumidityTooHigh, 300.0); END STATE: Pause Start_blæser= OFF; GO TO Ventilate WHEN DELAY(Pause,300.0); GO TO INIT WHEN _Humidity < _NormalHumidity; END END

  19. HomeControler: kodegenerering SEQUENCE: Ventilation # SEK RESET= Ventilation_Stop; # LOD 13 # RES STATE: INIT # STAI 11 FanMotor= OFF; # LODF # OUT 14 GO TO Ventilate WHEN ON; # LODT # GO 15 END STATE: Ventilate # STA 15 FanMotor= ON; # LODT # OUT 14 END END # ESK

  20. HomeControler: kodegenerering LStatement:= Ident "=" Lexp ";" (Gen OUT Ident.address) Lexp:= Lexp "OR" Lexp (Gen OR) | Lexp "AND" Lexp (Gen AND) | "NOT"Lexp (Gen NOT) | "(" Lexp ")" | Ident (Gen LOD Ident.address) | "ON" (Gen LODT) | "OFF" (Gen LODF) LStatement LOD ’d’ NOT LOD ’c’ OR LOD ’b’ AND OUT ’a’ Ident = Lexp ; Lexp AND Lexp Ident ( Lexp ) Lexp OR Lexp a= b AND (c OR NOT d); Ident NOT Lexp Ident

More Related