Dr. D.M. Akbar Hussain - Aalborg...

34
Dr. D.M. Akbar Hussain Compiler Construction F6S 1 Dr. D. M. Akbar Hussain Department of Electronic Systems 1 Dr. D. M. Akbar Hussain Department of Electronic Systems 2

Transcript of Dr. D.M. Akbar Hussain - Aalborg...

  • Dr. D.M. Akbar Hussain

    Compiler Construction F6S 1

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems1

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems2

  • Dr. D.M. Akbar Hussain

    Compiler Construction F6S 2

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems3

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems4

  • Dr. D.M. Akbar Hussain

    Compiler Construction F6S 3

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems5

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems6

  • Dr. D.M. Akbar Hussain

    Compiler Construction F6S 4

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems7

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems8

  • Dr. D.M. Akbar Hussain

    Compiler Construction F6S 5

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems9

    aa == bb **-- cc ++ bb **-- cc

    aa == bb **-- cc ++ bb **-- cc

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems10

  • Dr. D.M. Akbar Hussain

    Compiler Construction F6S 6

    aa == bb **-- cc ++ bb **-- cc

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems11

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems12

  • Dr. D.M. Akbar Hussain

    Compiler Construction F6S 7

    Grammar RulesGrammar Rules Semantic RulesSemantic Rulesexp1 → id = exp2 exp1.pcode = “lda” || id.strval

    ++exp2.pcode ++ “stn”exp → aexp exp.pcode = aexp.pcodeaexp1 → aexp2 + exp1.pcode = aexp2.pcode

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems13

    p pfactor

    p p p p++factor.pcode ++ “adi”

    aexp → factor aexp.pcode = factor.pcodefactor → (exp) factor.pcode = exp.pcodefactor → num factor.pcode = “ldc” || num.strvalfactor → id factor.pcode = “lod” || id.strval

    Synthesized 3AC

    Grammar Rules Semantic Rulesexp1 → id = exp2 exp1.name = exp2.name

    exp1 tacode = exp2 tacode ++exp1.tacode = exp2.tacode ++id.strval || “=“ || exp2.name

    exp → aexp exp.name = aexp.nameexp.tacode = aexp.tacode

    aexp1 → aexp2 + factor

    exp1.name = newtemp ()aexp1.tacode = aexp2.tacode ++ factor.tacode

    ++ aexp1.name || “=“ || aexp2.name|| “+” || factor.name

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems14

    aexp → factor aexp.name = factor.name aexp.tacode = factor.tacode

    factor → (exp) factor.name = exp.name factor.tacode = exp.tacode

    factor → num factor.name = num.strval factor.tacode = “ ”factor → id factor.name = id.strval factor.tacode = “ ”

  • Dr. D.M. Akbar Hussain

    Compiler Construction F6S 8

    Practical Code GenerationPractical Code Generation

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems15

    Practical Code GenerationPractical Code Generation

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems16

  • Dr. D.M. Akbar Hussain

    Compiler Construction F6S 9

    Generating Target Code from ICGenerating Target Code from IC

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems17

    3x

    Address of x

    3 + xAddress of x

    x + 3

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems18

    4x + 3

    x + 3 + 4

  • Dr. D.M. Akbar Hussain

    Compiler Construction F6S 10

    3x

    Add fAddress of x

    T1Address of x

    T1

    4

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems19

    4T1

    T2

    T2 +

    x, T1 + 4

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems20

    x, T1 4

    x 3

  • Dr. D.M. Akbar Hussain

    Compiler Construction F6S 11

    x

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems21

    100

    a *(a + i)

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems22

    a + s * iia

  • Dr. D.M. Akbar Hussain

    Compiler Construction F6S 12

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems23

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems24

  • Dr. D.M. Akbar Hussain

    Compiler Construction F6S 13

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems25

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems26

  • Dr. D.M. Akbar Hussain

    Compiler Construction F6S 14

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems27

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems28

  • Dr. D.M. Akbar Hussain

    Compiler Construction F6S 15

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems29

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems30

  • Dr. D.M. Akbar Hussain

    Compiler Construction F6S 16

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems31

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems32

  • Dr. D.M. Akbar Hussain

    Compiler Construction F6S 17

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems33

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems34

  • Dr. D.M. Akbar Hussain

    Compiler Construction F6S 18

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems35

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems36

  • Dr. D.M. Akbar Hussain

    Compiler Construction F6S 19

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems37

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems38

  • Dr. D.M. Akbar Hussain

    Compiler Construction F6S 20

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems39

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems40

  • Dr. D.M. Akbar Hussain

    Compiler Construction F6S 21

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems41

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems42

  • Dr. D.M. Akbar Hussain

    Compiler Construction F6S 22

    (x = x+3 ) + 4Variable x in this expression is stored locally on the stack frame.Assembly code for this expression:

    d t [b 2]mov ax, word ptr [bp-2]add ax, 3mov word ptr [bp-2] , axadd ax, 4Register ax is used as the main temporary location for the computation.

    Location of the local variable x is bp-2.

    bp base pointer register as the frame pointer and integer variables occupy two bytes on thismachine.

    The first instruction moves the value of the x to ax (the brackets in the addresses [bp-2] indicatean indirect rather than an immediate load).

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems43

    The second instruction adds the constant 3 to this register.

    The third instruction then moves this value to the location of x.

    Finally, the forth instruction adds 4 to ax, so that the final vale of the expression is left in thisregister, where it may be used for further computations.

    Note the address of x for this assignment in the third instruction is not pre-computed (as an ldaP-code instruction would suggest). A static simulation of the intermediate code, together withknowledge of available addressing modes, can delay the computation of the address of x until thispoint.

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems44

  • Dr. D.M. Akbar Hussain

    Compiler Construction F6S 23

    if and whileif and while

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems45

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems46

  • Dr. D.M. Akbar Hussain

    Compiler Construction F6S 24

    Consider Definition now for f:_f proc near

    push bpmov bp,spadd ax,word ptr [bp+4]inc axjmp short @1@58

    @1@58:pop bpret

    _f endp

    The return address is on the stack between the control link (the old bp) and the argument as a result

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems47

    The return address is on the stack between the control link (the old bp) and the argument as a resultof the caller’s execution of a call instruction. This, the old bp is at the top of the stack, the returnaddress is at location bp+2 (addresses are two bytes in this example), the parameter x is at locationbp+4, and the parameter y is at location bp+6. The body of f then corresponds to the code that comesnext:

    Mov ax,word ptr [bp+4]Add ax,word ptr [bp+6]Inc ax

    which loads x into ax, adds y to it, and then increment it by one.

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems48

  • Dr. D.M. Akbar Hussain

    Compiler Construction F6S 25

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems49

    The code generated for this statement:x.j = x.i;

    mov ax, word ptr [bp-6] (loads x.i into ax)mov word ptr [bp-3], ax (stores this value to x.j)

    The offset computation for j(−6 + 3 = −3) is performed statically by thecompiler.

    The code generated for the statement:p -> lchild = p;mov word ptr [si+2], siN t h th i di ti d th ff t t ti bi d i t

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems50

    Note how the indirection and the offset computation are combined intoa single instruction.

    Finally, the code generated for the statement:p = p -> rchild;mov si, word ptr [si+4]

  • Dr. D.M. Akbar Hussain

    Compiler Construction F6S 26

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems51

    cmp bx,dxp ,jle short @1@86inc dxjmp short @1@114

    @1@86:dec bx

    @1@114:This code uses the same sequential organization shown earlier but note this code does not computethe actual logical value of the expression x > y but simply uses the condition code directly.The code generated by the Borland compiler for the while-statement is as follows:

    jmp short @1@170@1@142:

    sub dx bx

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems52

    sub dx,bx@1@170:

    cmp dx,bxjl short @1@142

    This uses a slightly different sequential organization given earlier, here test is placed at the end, andan initial unconditional jump is made to this test.

  • Dr. D.M. Akbar Hussain

    Compiler Construction F6S 27

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems53

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems54

  • Dr. D.M. Akbar Hussain

    Compiler Construction F6S 28

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems55

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems56

  • Dr. D.M. Akbar Hussain

    Compiler Construction F6S 29

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems57

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems58

  • Dr. D.M. Akbar Hussain

    Compiler Construction F6S 30

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems59

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems60

  • Dr. D.M. Akbar Hussain

    Compiler Construction F6S 31

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems61

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems62

  • Dr. D.M. Akbar Hussain

    Compiler Construction F6S 32

    x = 1;…..y = 0;……if ( ) 10if (y) x = 10;…….if (x) y = 100;

    Constant Propagationx = 1;…y = 0;….if (0) x = 10;….

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems63

    if (x) y = 100;

    Un-reachablex = 1;…y = 0;….if (x) y = 100;

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems64

  • Dr. D.M. Akbar Hussain

    Compiler Construction F6S 33

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems65

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems66

  • Dr. D.M. Akbar Hussain

    Compiler Construction F6S 34

    Example Flow GraphExample Flow Graph

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems67

    Dr. D. M. Akbar Hussain

    Department of Electronic Systems68