• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Declarative Semantics Definition - Code Generation
 

Declarative Semantics Definition - Code Generation

on

  • 2,994 views

Presentation slides for lecture 5 of course IN4303 on Compiler Construction at TU Delft.

Presentation slides for lecture 5 of course IN4303 on Compiler Construction at TU Delft.

Statistics

Views

Total Views
2,994
Views on SlideShare
2,275
Embed Views
719

Actions

Likes
1
Downloads
25
Comments
0

2 Embeds 719

https://blackboard.tudelft.nl 587
http://blackboard.tudelft.nl 132

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

CC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • feedback loop\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • console interaction\n
  • console interaction\n
  • console interaction\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • 1. ldc 0 if_icmpeq label0 → ifeq label0\n2. ldc 0 goto label1 ifeq else0 → goto else0\n3. ifeq label0 goto else0 → ifne else0\n4. ldc 1 ifeq else0 → nop\n5. goto end0 → ireturn\n6. iload 1 → iload_1\n7. ldc 1 → iconst_1\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • round-up on every lecture\n\nwhat to take with you\n\ncheck yourself, pre- and post-paration\n
  • \n
  • \n

Declarative Semantics Definition - Code Generation Declarative Semantics Definition - Code Generation Presentation Transcript

  • Declarative Semantics DefinitionCode GenerationGuido Wachsmuth Delft Course IN4303, 2012/13 University of Technology Compiler Construction Challenge the future
  • Assessmentlast lecture Code Generation 2
  • Assessmentlast lectureDiscuss the need for static analysis in the definition of softwarelanguages. Code Generation 2
  • Assessmentlast lectureDiscuss the need for static analysis in the definition of softwarelanguages. • decidability & efficiency of the word problem • context-free languages • software languages typically not context-free • liberal context-free grammar for superset of the language • static analysis to restrict superset • semantic editor services Code Generation 2
  • Assessmentlast lectureDiscuss the need for static analysis in the definition of softwarelanguages. • decidability & efficiency of the word problem • context-free languages • software languages typically not context-free • liberal context-free grammar for superset of the language • static analysis to restrict superset • semantic editor servicesHow does static analysis contribute to a compiler w.r.t. itsarchitecture? Code Generation 2
  • Recap: Modern Compilers in IDEsarchitecture parse Machine Editor generate analyse AST Code Code Generation 3
  • Overviewtoday’s lectureJava Virtual Machine • name analysis • Java Bytecode instructions • executioncode generation • text vs. code generation • Tiger to Java Bytecodepretty-printing • string interpolation • generic pretty-printing Code Generation 4
  • IJava Virtual Machine Term Rewriting 5
  • Java Virtual Machinebytecode instructions method areapc: 0000 A7 goto01 0002 04 0403 00 nop04 A7 goto05 FF06 FF 03 Code Generation 6
  • Java Virtual Machinebytecode instructions method areapc: 0400 A7 goto01 0002 04 0403 00 nop04 A7 goto05 FF06 FF 03 Code Generation 7
  • Java Virtual Machinebytecode instructions method areapc: 0300 A7 goto01 0002 04 0403 00 nop04 A7 goto05 FF06 FF 03 Code Generation 8
  • Java Virtual Machinebytecode instructions method areapc: 0400 A7 goto01 0002 04 0403 00 nop04 A7 goto05 FF06 FF 03 Code Generation 9
  • the operand stack Code Generation 10
  • Java Virtual Machinethe operand stack method area stackpc: 00 optop: 0000 04 iconst_1 0001 05 iconst_2 0102 10 bipush 0203 2A 0304 11 sipush 0405 43 0506 03 06 Code Generation 11
  • Java Virtual Machinethe operand stack method area stackpc: 01 optop: 0100 04 iconst_1 00 0000 000101 05 iconst_2 0102 10 bipush 0203 2A 0304 11 sipush 0405 43 0506 03 06 Code Generation 12
  • Java Virtual Machinethe operand stack method area stackpc: 02 optop: 0200 04 iconst_1 00 0000 000101 05 iconst_2 01 0000 000202 10 bipush 0203 2A 0304 11 sipush 0405 43 0506 03 06 Code Generation 13
  • Java Virtual Machinethe operand stack method area stackpc: 04 optop: 0300 04 iconst_1 00 0000 000101 05 iconst_2 01 0000 000202 10 bipush 02 0000 002A03 2A 0304 11 sipush 0405 43 0506 03 06 Code Generation 14
  • Java Virtual Machinethe operand stack method area stackpc: 07 optop: 0400 04 iconst_1 00 0000 000101 05 iconst_2 01 0000 000202 10 bipush 02 0000 002A03 2A 03 0000 430304 11 sipush 0405 43 0506 03 06 Code Generation 15
  • Java Virtual Machinethe operand stack method area stackpc: 07 optop: 0407 60 iadd 00 0000 000108 68 imul 01 0000 000209 5F swap 02 0000 002A0A 64 isub 03 0000 43030B 9A ifne 040C FF 050D F5 00 06 Code Generation 16
  • Java Virtual Machinethe operand stack method area stackpc: 08 optop: 0307 60 iadd 00 0000 000108 68 imul 01 0000 000209 5F swap 02 0000 432D0A 64 isub 030B 9A ifne 040C FF 050D F5 00 06 Code Generation 17
  • Java Virtual Machinethe operand stack method area stackpc: 09 optop: 0207 60 iadd 00 0000 000108 68 imul 01 0000 865A09 5F swap 020A 64 isub 030B 9A ifne 040C FF 050D F5 00 06 Code Generation 18
  • Java Virtual Machinethe operand stack method area stackpc: 0A optop: 0207 60 iadd 00 0000 865A08 68 imul 01 0000 000109 5F swap 020A 64 isub 030B 9A ifne 040C FF 050D F5 00 06 Code Generation 19
  • Java Virtual Machinethe operand stack method area stackpc: 0B optop: 0107 60 iadd 00 0000 865908 68 imul 0109 5F swap 020A 64 isub 030B 9A ifne 040C FF 050D F5 00 06 Code Generation 20
  • Java Virtual Machinethe operand stack method area stackpc: 00 optop: 0000 04 iconst_1 0001 05 iconst_2 0102 10 bipush 0203 2A 0304 11 sipush 0405 43 0506 03 06 Code Generation 21
  • the constant pool Code Generation 22
  • Java Virtual Machinethe constant pool method area stackpc: 00 constant pool optop: 0000 12 ldc 00 0000 002A 0001 00 00 01 0000 4303 0102 12 ldc 02 0000 0000 0203 01 01 03 0000 002A 0304 14 ldc2_w 04 0405 00 05 0506 02 02 06 06 Code Generation 23
  • Java Virtual Machinethe constant pool method area stackpc: 02 constant pool optop: 0100 12 ldc 00 0000 002A 00 0000 002A01 00 00 01 0000 4303 0102 12 ldc 02 0000 0000 0203 01 01 03 0000 002A 0304 14 ldc2_w 04 0405 00 05 0506 02 02 06 06 Code Generation 24
  • Java Virtual Machinethe constant pool method area stackpc: 04 constant pool optop: 0200 12 ldc 00 0000 002A 00 0000 002A01 00 00 01 0000 4303 01 0000 430302 12 ldc 02 0000 0000 0203 01 01 03 0000 002A 0304 14 ldc2_w 04 0405 00 05 0506 02 02 06 06 Code Generation 25
  • Java Virtual Machinethe constant pool method area stackpc: 07 constant pool optop: 0400 12 ldc 00 0000 002A 00 0000 002A01 00 00 01 0000 4303 01 0000 430302 12 ldc 02 0000 0000 02 0000 000003 01 01 03 0000 002A 03 0000 002A04 14 ldc2_w 04 0405 00 05 0506 02 02 06 06 Code Generation 26
  • local variables Code Generation 27
  • Java Virtual Machinelocal variables method area stackpc: 00 optop: 00 local variables00 04 iconst_1 00 0001 3B istore_0 01 0102 1A iload_0 02 0203 3C istore_1 03 0304 84 iinc 04 0405 01 01 05 0506 01 01 06 06 Code Generation 28
  • Java Virtual Machinelocal variables method area stackpc: 01 optop: 01 local variables00 04 iconst_1 00 0000 0001 0001 3B istore_0 01 0102 1A iload_0 02 0203 3C istore_1 03 0304 84 iinc 04 0405 01 01 05 0506 01 01 06 06 Code Generation 29
  • Java Virtual Machinelocal variables method area stackpc: 02 optop: 00 local variables00 04 iconst_1 00 00 0000 000101 3B istore_0 01 0102 1A iload_0 02 0203 3C istore_1 03 0304 84 iinc 04 0405 01 01 05 0506 01 01 06 06 Code Generation 30
  • Java Virtual Machinelocal variables method area stackpc: 03 optop: 01 local variables00 04 iconst_1 00 0000 0001 00 0000 000101 3B istore_0 01 0102 1A iload_0 02 0203 3C istore_1 03 0304 84 iinc 04 0405 01 01 05 0506 01 01 06 06 Code Generation 31
  • Java Virtual Machinelocal variables method area stackpc: 04 optop: 00 local variables00 04 iconst_1 00 00 0000 000101 3B istore_0 01 01 0000 000102 1A iload_0 02 0203 3C istore_1 03 0304 84 iinc 04 0405 01 01 05 0506 01 01 06 06 Code Generation 32
  • Java Virtual Machinelocal variables method area stackpc: 07 optop: 00 local variables00 04 iconst_1 00 00 0000 000101 3B istore_0 01 01 0000 000202 1A iload_0 02 0203 3C istore_1 03 0304 84 iinc 04 0405 01 01 05 0506 01 01 06 06 Code Generation 33
  • the heap Code Generation 34
  • Java Virtual Machinethe heap method area stackpc: 00 constant pool optop: 00 local variables00 12 ldc 00 4303 4303 00 00 002A 002A01 00 00 01 0000 0004 01 0102 19 aload 02 02 0203 00 00 03 03 0304 12 ldc 04 04 0405 01 01 05 05 0506 2E iaload 06 06 06 heap4303 4303 "Compilers" 002A 002A [20,01,40,02,42] Code Generation 35
  • Java Virtual Machinethe heap method area stackpc: 02 constant pool optop: 01 local variables00 12 ldc 00 4303 4303 00 4303 4303 00 002A 002A01 00 00 01 0000 0004 01 0102 19 aload 02 02 0203 00 00 03 03 0304 12 ldc 04 04 0405 01 01 05 05 0506 2E iaload 06 06 06 heap4303 4303 "Compilers" 002A 002A [20,01,40,02,42] Code Generation 36
  • Java Virtual Machinethe heap method area stackpc: 04 constant pool optop: 02 local variables00 12 ldc 00 4303 4303 00 4303 4303 00 002A 002A01 00 00 01 0000 0004 01 002A 002A 0102 19 aload 02 02 0203 00 00 03 03 0304 12 ldc 04 04 0405 01 01 05 05 0506 2E iaload 06 06 06 heap4303 4303 "Compilers" 002A 002A [20,01,40,02,42] Code Generation 37
  • Java Virtual Machinethe heap method area stackpc: 06 constant pool optop: 03 local variables00 12 ldc 00 4303 4303 00 4303 4303 00 002A 002A01 00 00 01 0000 0004 01 002A 002A 0102 19 aload 02 02 0000 0004 0203 00 00 03 03 0304 12 ldc 04 04 0405 01 01 05 05 0506 2E iaload 06 06 06 heap4303 4303 "Compilers" 002A 002A [20,01,40,02,42] Code Generation 38
  • Java Virtual Machinethe heap method area stackpc: 07 constant pool optop: 02 local variables00 12 ldc 00 4303 4303 00 4303 4303 00 002A 002A01 00 00 01 0000 0004 01 0000 0042 0102 19 aload 02 02 0203 00 00 03 03 0304 12 ldc 04 04 0405 01 01 05 05 0506 2E iaload 06 06 06 heap4303 4303 "Compilers" 002A 002A [20,01,40,02,42] Code Generation 39
  • stack frames Code Generation 40
  • Java Virtual Machinestack frames method area stackpc: 03 optop: 02 local variables00 2A aload_0 00 4303 4303 00 4303 430301 10 bipush 01 0000 0040 0102 40 02 0203 B6 invokevirtual 03 0304 00 04 0405 01 01 05 0506 AC ireturn 06 06 heap Code Generation 41
  • Java Virtual Machinestack frames method area stack stackpc: 80 optop: 01 optop: 00 local variables local variables80 2B iload_1 00 4303 4303 00 00 4303 4303 00 4303 430381 59 dup 01 0000 0040 01 01 0000 0040 0182 68 imul 02 02 02 0283 AC ireturn 03 03 03 0384 00 04 04 04 0485 00 05 05 05 0586 00 06 06 06 06 heap Code Generation 42
  • Java Virtual Machinestack frames method area stack stackpc: 81 optop: 01 optop: 01 local variables local variables80 2B iload_1 00 4303 4303 00 0000 0040 00 4303 4303 00 4303 430381 59 dup 01 0000 0040 01 01 0000 0040 0182 68 imul 02 02 02 0283 AC ireturn 03 03 03 0384 00 04 04 04 0485 00 05 05 05 0586 00 06 06 06 06 heap Code Generation 43
  • Java Virtual Machinestack frames method area stack stackpc: 81 optop: 01 optop: 02 local variables local variables80 2B iload_1 00 4303 4303 00 0000 0040 00 4303 4303 00 4303 430381 59 dup 01 0000 0040 01 0000 0040 01 0000 0040 0182 68 imul 02 02 02 0283 AC ireturn 03 03 03 0384 00 04 04 04 0485 00 05 05 05 0586 00 06 06 06 06 heap Code Generation 44
  • Java Virtual Machinestack frames method area stack stackpc: 82 optop: 01 optop: 01 local variables local variables80 2B iload_1 00 4303 4303 00 0000 1000 00 4303 4303 00 4303 430381 59 dup 01 0000 0040 01 01 0000 0040 0182 68 imul 02 02 02 0283 AC ireturn 03 03 03 0384 00 04 04 04 0485 00 05 05 05 0586 00 06 06 06 06 heap Code Generation 45
  • Java Virtual Machinestack frames method area stackpc: 06 optop: 01 local variables00 2A aload_0 00 0000 1000 00 4303 430301 10 bipush 01 0102 40 02 0203 B6 invokevirtual 03 0304 00 04 0405 01 01 05 0506 AC ireturn 06 06 heap Code Generation 46
  • IIclass files Term Rewriting 47
  • Recap: Traditional Java Compilerexample Code Generation 48
  • Recap: Traditional Java Compilerexamplels Course.java Code Generation 48
  • Recap: Traditional Java Compilerexamplels Course.javajavac -verbose Course.java [parsing started Course.java] [parsing completed 8ms] [loading java/lang/Object.class(java/lang:Object.class)] [checking university.Course] [wrote Course.class] [total 411ms] Code Generation 48
  • Recap: Traditional Java Compilerexamplels Course.javajavac -verbose Course.java [parsing started Course.java] [parsing completed 8ms] [loading java/lang/Object.class(java/lang:Object.class)] [checking university.Course] [wrote Course.class] [total 411ms]ls Course.class Course.java Code Generation 48
  • Class Filesformatmagic number CAFEBABEclass file version (minor, major)constant pool count + constant poolaccess flagsthis classsuper classinterfaces count + interfacesfields count + fieldsmethods count + methodsattribute count + attributes Code Generation 49
  • Intermediate Language.class public Exp .method public static fac(I)I iload 1 ifne else iconst_1 ireturn else: iload 1 dup iconst_1 isub invokestatic Exp/fac(I)I imul ireturn .end method Code Generation 50
  • IIIcode generation Term Rewriting 51
  • Printing Codeto-jbc = ?Nil() ; <printstring> "aconst_nulln"to-jbc = ?NoVal() ; <printstring> "nopn"to-jbc = ?Seq(es) ; <list-loop(to-jbc)> es to-jbc = ?Int(i); <printstring> "ldc "; <printstring> i; <printstring> "n" to-jbc = ?Bop(op, e1, e2) ; <to-jbc> e1 ; <to-jbc> e2 ; <to-jbc> op to-jbc = ?PLUS() ; <printstring> "iaddn"to-jbc = ?MINUS() ; <printstring> "isubn"to-jbc = ?MUL() ; <printstring> "imuln"to-jbc = ?DIV() ; <printstring> "idivn" Code Generation 52
  • Composing Stringsto-jbc: Nil() -> "aconst_nulln"to-jbc: NoVal() -> "nopn"to-jbc: Seq(es) -> <concat-strings> <map(to-jbc)> es to-jbc: Int(i) -> <concat-strings> ["ldc ", i, "n"] to-jbc: Bop(op, e1, e2) -> <concat-strings> [ <to-jbc> e1, <to-jbc> e2, <to-jbc> op ] to-jbc: PLUS() -> "iaddn"to-jbc: MINUS() -> "isubn"to-jbc: MUL() -> "imuln"to-jbc: DIV() -> "idivn" Code Generation 53
  • String Interpolationto-jbc: Nil() -> $[aconst_null]to-jbc: NoVal() -> $[nop]to-jbc: Seq(es) -> <map-to-jbc> esmap-to-jbc: [] -> $[]map-to-jbc: [h|t] -> $[[<to-jbc> h] [<map-to-jbc> t]] to-jbc: Int(i) -> $[ldc [i]] to-jbc: Bop(op, e1, e2) -> $[[<to-jbc> e1] [<to-jbc> e2] [<to-jbc> op]] to-jbc: PLUS() -> $[iadd]to-jbc: MINUS() -> $[isub]to-jbc: MUL() -> $[imul]to-jbc: DIV() -> $[idiv] Code Generation 54
  • coffee break Code Generation 55
  • IVgeneration by transformation Term Rewriting 56
  • Staged Compilationtransformation stepsAST Code Generation 57
  • Staged Compilationtransformation steps desugarAST AST Code Generation 57
  • Staged Compilationtransformation steps desugar analyseAST AST AST Code Generation 57
  • Staged Compilationtransformation steps desugar analyse normaliseAST AST AST AST Code Generation 57
  • Staged Compilationtransformation steps desugar analyse normaliseAST AST AST ASTAST Code Generation 57
  • Staged Compilationtransformation steps desugar analyse normaliseAST AST AST AST generateAST AST Code Generation 57
  • Staged Compilationtransformation steps desugar analyse normaliseAST AST AST AST generate optimiseAST AST AST Code Generation 57
  • Staged Compilationtransformation steps desugar analyse normaliseAST AST AST AST generate optimise print TargetAST AST AST Code Code Generation 57
  • Pretty-Printingpp-jbc: ACONST_NULL() -> $[aconst_null]pp-jbc: NOP() -> $[nop]pp-jbc: LDC(Int(i)) -> $[ldc [i]]pp-jbc: LDC(String(s)) -> $[ldc [s]]pp-jbc: IADD() -> $[iadd]pp-jbc: ISUB() -> $[isub]pp-jbc: IMUL() -> $[imul]pp-jbc: IDIV() -> $[idiv] Code Generation 58
  • Recap: Desugaringif x then if x then printint(x) printint(x) else ()IfThen( IfThenElse( Var("x") Var("x"), Call( , Call( "printint" "printint" , [Var("x")] , [Var("x")] ) )) , NoVal() )desugar: IfThen(e1, e2) -> IfThenElse(e1, e2, NoVal()) Code Generation 59
  • Recap: Desugaringdesugar: Uminus(e) -> Bop(MINUS(), Int("0"), e) signature constructorsdesugar: Plus(e1, e2) -> Bop(PLUS(), e1, e2) PLUS: BinOpdesugar: Minus(e1, e2) -> Bop(MINUS(), e1, e2) MINUS: BinOpdesugar: Times(e1, e2) -> Bop(MUL(), e1, e2) MUL: BinOpdesugar: Divide(e1, e2) -> Bop(DIV(), e1, e2) DIV: BinOp EQ: RelOpdesugar: Eq(e1, e2) -> Rop(EQ(), e1, e2) NE: RelOpdesugar: Neq(e1, e2) -> Rop(NE(), e1, e2) LE: RelOpdesugar: Leq(e1, e2) -> Rop(LE(), e1, e2) LT: RelOpdesugar: Lt(e1, e2) -> Rop(LT(), e1, e2) desugar: Geq(e1, e2) -> Rop(LE(), e2, e1) Bop: BinOp * Expr * Expr -> Exprdesugar: Gt(e1, e2) -> Rop(LT(), e2, e1) Rop: RelOp * Expr * Expr -> Exprdesugar: And(e1, e2) -> IfThenElse(e1, e2, Int("0"))desugar: Or(e1, e2) -> IfThenElse(e1, Int("1"), e2) Code Generation 60
  • Recap: Analysislet let type t = u type t0 = u0 type u = int type u0 = int var x: u := 0 var x0: u0 := 0in in x := 42 ; x0 := 42 ; let let type u = t type u1 = t0 var y: u := 0 var y0: u1 := 0 in in y := 42 y0 := 42 end endend endrename-all = alltd(rename) Code Generation 61
  • Normalisationnormalise : VarDec(v, e) -> VarDec(v, <type-of> e, e)normalise : VarDec(v, t, e) -> VarDec(v, <type-of> t, e)normalise : FunDec(f, as, e) -> FunDec(f, as, NO_VAL(), e)normalise : FunDec(f, as, t, e) -> FunDec(f, as, <type-of> t, e)normalise : For(v, e1, e2, e3) -> Let([decl], [while]) where !VarDec(v, INT(), e1) => decl ; !Rop(LE(), Var(v), e2) => check ; !Assign(Var(v), Bop(PLUS(), Var(v), Int("1"))) => inc ; !While(check, Seq([e3, inc])) => whilenormalise : Seq([]) -> NoVal()normalise : Seq([e]) -> enormalise : Seq([NoVal()|es]) -> Seq(es)normalise : Seq([e1,e2,e3|es]) -> Seq([e1, Seq([e2, e3|es])])normalise : Seq([Seq(es1)|es2]) -> Seq(<conc> (es1, es2)) Code Generation 62
  • Normalisationlet let type t0 = u0 var x0: int := 0 type u0 = int var y0: int := 0 var x0: u0 := 21in function fac0(n0: int): int= x0 := 42 ; if n0 = 0 then let 1 function fac0(n0: int): int= else if n0 = 0 then n0 * fac0(n0 - 1) 1 in else x0 := 21 ; n0 * fac0(n0 - 1) x0 := 42 ; y0 := fac0(x0) ; type u1 = t0 y0 := 42 var y0: u1 := fac0(x0) end in y0 := 42 endend Code Generation 63
  • Code Generationto-jbc: Nil() -> [ ACONST_NULL() ]to-jbc: NoVal() -> [ NOP() ]to-jbc: Seq(es) -> <mapconcat(to-jbc)> es to-jbc: Int(i) -> [ LDC(Int(i)) ]to-jbc: String(s) -> [ LDC(String(s)) ] to-jbc: Bop(op, e1, e2) -> <mapconcat(to-jbc)> [ e1, e2, op ] to-jbc: PLUS() -> [ IADD() ]to-jbc: MINUS() -> [ ISUB() ]to-jbc: MUL() -> [ IMUL() ]to-jbc: DIV() -> [ IDIV() ]to-jbc: Assign(lhs, e) -> <concat> [ <to-jbc> e, <lhs-to-jbc> lhs ] to-jbc: Var(x) -> [ ILOAD(x) ] where <type-of> Var(x) => INT()to-jbc: Var(x) -> [ ALOAD(x) ] where <type-of> Var(x) => STRING()lhs-to-jbc: Var(x) -> [ ISTORE(x) ] where <type-of> Var(x) => INT()lhs-to-jbc: Var(x) -> [ ASTORE(x) ] where <type-of> Var(x) => STRING() Code Generation 64
  • Code Generationto-jbc: IfThenElse(e1, e2, e3) -> <concat> [ <to-jbc> e1, [ IFEQ(LabelRef(else)) ], <to-jbc> e2, [ GOTO(LabelRef(end)), Label(else) ], <to-jbc> e3, [ Label(end) ] ] where <newname> "else" => else where <newname> "end" => end to-jbc: While(e1, e2) -> <concat> [ [ GOTO(LabelRef(check)), Label(body) ], <to-jbc> e2, [ Label(check) ], <to-jbc> e1, [ IFNE(LabelRef(body)) ] ] where <newname> "test" => check where <newname> "body" => body Code Generation 65
  • Code Generationfunction fac0(n0: int): int= .method public static fac0(I)I if n0 = 0 iload 1 then ldc 0 1 if_icmpeq label0 else ldc 0 n0 * fac0(n0 - 1) goto label1 label0: ldc 1 label1: ifeq else0 ldc 1 goto end0 else0: iload 1 iload 1 ldc 1 isub invokestatic Exp/fac0(I)I imul end0: ireturn .end method Code Generation 66
  • Optimisation.method public static fac0(I)I .method public static fac0(I)I iload 1 iload_1 ldc 0 ifne else0 if_icmpeq label0 ldc 0 iconst_1 goto label1 ireturn label0: ldc 1 label1: ifeq else0 else0: iload_1 ldc 1 dup goto end0 iconst_1 else0: iload 1 isub iload 1 invokestatic ldc 1 Exp/fac0(I)I isub imul invokestatic ireturn Exp/fac0(I)I .end method imul end0: ireturn.end method Code Generation 67
  • Vsummary Term Rewriting 68
  • Summarylessons learned Code Generation 69
  • Summarylessons learnedWhat are Java Bytecode and the Java Virtual Machine? • bytecode instructions • architecture • class file format • intermediate language Code Generation 69
  • Summarylessons learnedWhat are Java Bytecode and the Java Virtual Machine? • bytecode instructions • architecture • class file format • intermediate languageHow do you generate code by transformation? • desugaring, decoration, normalisation • transforming generated code Code Generation 69
  • Summarylessons learnedWhat are Java Bytecode and the Java Virtual Machine? • bytecode instructions • architecture • class file format • intermediate languageHow do you generate code by transformation? • desugaring, decoration, normalisation • transforming generated codeHow do you transform abstract syntax trees into text again? • string interpolation • pretty-printing Code Generation 69
  • Literaturelearn more Code Generation 70
  • Literaturelearn moreJava Virtual Machine Tim Lindholm, Frank Yellin: The Java Virtual Machine Specification, 2nd edition. Addison-Wesley, 1999. Bill Venners: Inside the Java 2 Virtual Machine. McGraw-Hill, 2000. Code Generation 70
  • Literaturelearn moreJava Virtual Machine Tim Lindholm, Frank Yellin: The Java Virtual Machine Specification, 2nd edition. Addison-Wesley, 1999. Bill Venners: Inside the Java 2 Virtual Machine. McGraw-Hill, 2000.Generation by Transformation Zef Hemel, Lennart C. L. Kats, Danny M. Groenewegen, Eelco Visser: Code generation by model transformation. A case study in transformation modularity. SoSym 9(3), 2010 Code Generation 70
  • Outlookcoming nextimperative and object-oriented languages • Activation Frames • Liveness Analysis • Register Allocation • Garbage CollectionLab Oct 18 • type projection & type checking Code Generation 71
  • copyrights & credits Code Generation 72
  • Code Generation 73
  • PicturescopyrightsSlide 1: Matrix by Gamaliel Espinoza Macedo, some rights reservedSlide 7: L is for Lego by Don Solo, some rights reservedSlide 9: Billiard Balls by Darren Hester, some rights reservedSlide 11: Autoturm by m.prinke, some rights reservedSlide 13: Spy Hill Landfill by DArcy Norman, some rights reservedSlide 15: Framed by LexnGer, some rights reservedSlide 25: Benders by Dominica Williamson, some rights reservedSlide 41: Leuchtturm by herr.g, some rights reserved Code Generation 74