lab manual spcc(1)

Upload: enggeng7

Post on 23-Feb-2018

421 views

Category:

Documents


15 download

TRANSCRIPT

  • 7/24/2019 Lab Manual Spcc(1)

    1/62

    SYSTEM PROGRAMMING & COMPILER CONSTRUCTION

    Mahatma Education Societys

    PILLAI HOC COLLEGE OF ENGINEERING AND TECHNOLOGY, RASAYANI

    La Manua!

    System P"o#"ammin# & Com$i!e"Const"uction

    P"o#"am % TE

    Semeste" % 'I

    Su(ect Name %System P"o#"ammin# & Com$i!e"

    Const"uction

    P"actica! In)Cha"#e I*C +O,

    Name & Si#natu"e P"o- Sandee$ Ras.a"

    Department of Computer Engineering

  • 7/24/2019 Lab Manual Spcc(1)

    2/62

    SYSTEM PROGRAMMING & COMPILER CONSTRUCTION

    Mahatma Education Societys

    PILLAI HOC COLLEGE OF ENGINEERING AND TECHNOLOGY

    ESAY 2014-2015

    Department Com$ute" En#inee"in#

    C!a"" TE

    S#$%e&t System P"o#"ammin# & Com$i!e" Const"uction

    E'per(ment L("t

    Se"ia! No Name o- e/$e"iment

    0Study o- 123*143 $"ocesso" 5ith inst"uctions

    6 Assem!e"s% 6 $ass Assem!e"

    1 Mac"o $"ocesso"% 6 $ass Mac"o $"ocesso"

    7 Le/ica! ana!y8e" too! % -!e/

    9Pa"se" #ene"ato" too!% Yacc

    2 :ind -i"st;< = -o!!o5;< set o- #i>en #"amma"

    4 C"eate you" !i"a"y in Linu/ en>i"onment and usin# it

    ? Code Gene"ation a!#o"ithm

    @ Code O$timi8ation techniues

    03 Study !d command in Uni/*Linu/

    E'per(ment N)* 01

    St#+ ) ./0.0 pr)&e"")r (t3 (n"tr#&t()n"

    Department of Computer Engineering

  • 7/24/2019 Lab Manual Spcc(1)

    3/62

    SYSTEM PROGRAMMING & COMPILER CONSTRUCTION

    A(mStudy o- 123*143 $"ocesso" 5ith inst"uctions

    T3e)r

    C)n&!#"()n

    Department of Computer Engineering

  • 7/24/2019 Lab Manual Spcc(1)

    4/62

    SYSTEM PROGRAMMING & COMPILER CONSTRUCTION

    Experiment No. 2

    A""em$!er" 2 pa"" A""em$!er*

    Title:Design and implementation of 2 pass assemblers for X86 machine

    Theory:

    Assembly Language Syntax:

    Each line of a program is one of the following:

    an instruction

    an assembler directive (or pseudo-op)

    a comment

    hitespace (between s!mbols) and case are ignored" #omments (beginning with $%&)

    are also ignored"

    'n instruction has the following format:

    'E *+#*DE *+E,'D. % #*//E0.

    E1ample:

    'DD,3,3,4

    'DD,3,354

    D,63/E,

    ,7**+

    Schemes of translation:

    Department of Computer Engineering

  • 7/24/2019 Lab Manual Spcc(1)

    5/62

    SYSTEM PROGRAMMING & COMPILER CONSTRUCTION

    I. Two pass translation:

    t can handles forward reference easil!"

    ocation counter(#) processing is done in pass and the s!mbols defined in the

    program are entered into s!mbol table"

    0he second pass uses this address information to generate target program"

    II. Single pass translation:

    9orward reference is handled b! bac patching"

    ata structure of assembler

    .!mbol 0able(.0)

    " /nemonics table or /achine-*peration table (/*0)"

    2" +seudo-opcode table (+*0)

    4" iteral table (0)

    ;" ocation counter"

    .!mbol table:

    /nemonic opcode table:

    +seudo-opcode table (+*0)

    iteral table(0)

    #:

    *pcode fields:

    i .tatement class

    Department of Computer Engineering

    'ddress *pcode *perand

  • 7/24/2019 Lab Manual Spcc(1)

    6/62

    SYSTEM PROGRAMMING & COMPILER CONSTRUCTION

    i" code

    *perand-2 fields

    i" *perand class:

    .:s!mbol

    :literals

    ii" code

    2-+ass 'ssembler:

    !ass"I

    .eparate s!mbol3 mnemonic and operand fields"

    " uild the s!mbol table"

    " #onstruct intermediate code"

    !ass"II

    Evaluate fields and generate code"

    " +rocess pseudo-opcodes"

    Algorithm:

    !ass"I

    .tep: nitiali7e all variables:

    oc%

    ittab%

    .tep2: ,ead line()%

    rea-words()%

    .tep4: hile(opcode?=end)

    ,ead line()%

    rea-words()%

    case: if opcode=start or opcode=small model

    Department of Computer Engineering

  • 7/24/2019 Lab Manual Spcc(1)

    7/62

    SYSTEM PROGRAMMING & COMPILER CONSTRUCTION

    oc

  • 7/24/2019 Lab Manual Spcc(1)

    8/62

    SYSTEM PROGRAMMING & COMPILER CONSTRUCTION

    ii" oc

  • 7/24/2019 Lab Manual Spcc(1)

    9/62

    SYSTEM PROGRAMMING & COMPILER CONSTRUCTION

    :!E 8input8inter;

    inputopen(/input.tt//r/);

    interopen(/inter.tt//=/);

    print(/?A4!" A9E@t2ERA"D@t2?DE@n/);

    print(/_____________________________________/);

    gets(line5&input);

    READ_!"E();

    i(strc+p(opcode/34AR4/))

    startaddratoi(operand);

    locctrstartaddr;

    strcp,(progra+na+elabel);

    print(inter/Bs/line);

    gets(line5&input);

    *

    else

    progra+na+e$&'C@&C;

    startaddr&;

    locctr&;

    *

    print(/@n Bd@t Bs@tBs@t Bs/locctrlabelopcodeoperand);

    =hile(strc+p(line/E"D/)&)

    READ_!"E();

    print(/@n Bd@t Bs @tBs@t Bs/locctrlabelopcodeoperand);

    Department of Computer Engineering

  • 7/24/2019 Lab Manual Spcc(1)

    10/62

    SYSTEM PROGRAMMING & COMPILER CONSTRUCTION

    i(label$&'C@&C)chk_label();

    chk_opcode();

    print(inter/Bs Bs Bs@n/labelopcodeoperand);

    gets(line5&input);

    *

    print(/@n Bd@t@tBs/locctrline);

    print(inter/Bs/line);

    close(inter);

    close(input);

    *

    78Asses+bler pass 5 87

    void 2A335()

    :!E 8inter8output;

    char record$-&'part$6'value$';782art arra, =as deined as part$'previousl,87

    int currttlen&oundopcodeoundoperandchkoperandaddrrecaddr&;

    interopen(/inter.tt//r/);

    outputopen(/output.tt//=/);

    gets(line5&inter);

    READ_!"E();

    i(strc+p(opcode/34AR4/)) gets(line5&inter);

    print(/@n@n?orresponding bect code is..@n/);

    print(/@nFG Bs G Bd G Bd /progra+na+estartaddrlength);

    print(output/@nFG Bs G Bd G Bd /progra+na+estartaddrlength);

    recaddrstartaddr; record$&'C@&C;

    =hile(strc+p(line/E"D/)&)

    Department of Computer Engineering

  • 7/24/2019 Lab Manual Spcc(1)

    11/62

    SYSTEM PROGRAMMING & COMPILER CONSTRUCTION

    operandaddroundoperandoundopcode&;

    value$&'part$&' C@&C;

    READ_!"E();

    or(chk&;chk

  • 7/24/2019 Lab Manual Spcc(1)

    12/62

    SYSTEM PROGRAMMING & COMPILER CONSTRUCTION

    i((currttlenHstrlen(part))

  • 7/24/2019 Lab Manual Spcc(1)

    13/62

    SYSTEM PROGRAMMING & COMPILER CONSTRUCTION

    int i&count&;

    label$&'opcode$&'operand$&'=ord%$&'=ord5$&'=ord-$&'C@&C;

    or(i&;line$i'C@&C;iHH)

    i(line$i'C C)

    bu$HH'line$i';

    else

    bu$'C@&C;

    strcp,(=ord-=ord5);

    strcp,(=ord5=ord%);

    strcp,(=ord%bu);

    &;

    countHH;

    *

    *

    bu$L%'C@&C;

    strcp,(=ord-=ord5);

    strcp,(=ord5=ord%);

    strcp,(=ord%bu);

    s=itch(count)

    case &Mstrcp,(opcode=ord%);

    break;

    case %Mstrcp,(opcode=ord5);strcp,(operand=ord%);*

    break;

    case 5M

    strcp,(label=ord-);strcp,(opcode=ord5);strcp,(operand=ord%);*

    break;

    *

    *

    Department of Computer Engineering

  • 7/24/2019 Lab Manual Spcc(1)

    14/62

    SYSTEM PROGRAMMING & COMPILER CONSTRUCTION

    void chk_label()

    int kdups,+&;

    or(k&;k

  • 7/24/2019 Lab Manual Spcc(1)

    15/62

    SYSTEM PROGRAMMING & COMPILER CONSTRUCTION

    i(strc+p( opcode/KRD/)) locctrH-;

    else i (strc+p(opcode/RE3K/))locctrH(-8atoi(operand));

    else i(strc+p(opcode/RE39/))locctrHatoi(operand);

    *

    *

    int +ain()

    2A33%();

    lengthlocctrLstartaddr;

    2A335();

    getch();

    F

    #onclusion:0hus we have implemented 2 pass assemblers for X86 machine"

    Experiment No.$

    8a&r) pr)&e"")r 2 pa"" 8a&r) pr)&e"")r*

    Title:Design and mplementation of /acro +rocessor"

    Theory:' macro instruction is a notational convenience for the programmer" t allowsthe programmer to write shorthand version of a program (module programming)" 0he

    macro processor replaces each macro invocation with the corresponding seGuence of

    Department of Computer Engineering

  • 7/24/2019 Lab Manual Spcc(1)

    16/62

    Source Code(with macro) Macro ProcessorExpanded CodeCompiler or Assemblerobj

    SYSTEM PROGRAMMING & COMPILER CONSTRUCTION

    statements (expanding)"/acro processor recogni7es macro definitions3 save the macrodefinition3 recogni7e macro calls3 e1pand macro calls"

    %acro !rocessor

    ,ecogni7e macro definitions

    .ave the macro definition

    ,ecogni7e macro calls

    E1pand macro calls

    Examples:

    /acro .tart of the definition

    ncr /acro name

    /ov a13> .eGuence to be abbreviated

    'dd a13>

    /end End of definition

    !arameteri&e' %acro

    macro

    Hlable incr Harg3 Harg2

    Department of Computer Engineering

  • 7/24/2019 Lab Manual Spcc(1)

    17/62

    SYSTEM PROGRAMMING & COMPILER CONSTRUCTION

    mov a13Harg

    Hlable add a13 Harg2

    sub arg23

    In7 Hlable

    /end

    #alll to this macro definition will be:

    loop incr var3var2

    #on'itional %acro

    macro

    @ar! Hcount3 Harg3Harg23Harg4

    'dd a13 Harg

    '9 (Hcount EJ )"9 0est if Hcount=

    'dd a13Harg2

    '9 (Hcount EJ 2)"9 0est if Hcount=

    'dd a13Harg4

    "9 mend

    abel start with (") and do not appear in the output"

    'B* is unconditional branch

    '9 is conditional branch

    f #all to this macro is:

    @ar! 43var3var23var4

    E1pansion will be:

    Department of Computer Engineering

  • 7/24/2019 Lab Manual Spcc(1)

    18/62

    SYSTEM PROGRAMMING & COMPILER CONSTRUCTION

    'dd a13var

    'dd a13var2

    'dd a13var4

    f call is:

    @ar! 23 var3var2

    E1pansion will be:

    'dd a13var

    'd a13var2

    Neste' %acro

    /acro calls within macro

    /acro

    'dd Harg

    /ov a13Harg2

    'dd a13

    /end

    /acro

    'dds Harg3Harg2

    'dd Harg

    'dd Harg2

    " /acro nstruction defining macros:

    macro

    Department of Computer Engineering

  • 7/24/2019 Lab Manual Spcc(1)

    19/62

    SYSTEM PROGRAMMING & COMPILER CONSTRUCTION

    define Hsub

    macro

    Hsub H!

    mov a13H!

    sub a13>

    mend

    mend

    (ecursi)e %acro

    macro

    ncr Harg

    /ov a13 Harg

    'dd a13

    '9 (Harg EJ >) "

    ncr arg

    " mend

    esign of simple macro processor

    "small model

    %acro

    '# Ha3 Ha2

    /ov a13 Ha

    'dd a13Ha2

    %en'

    "data

    Department of Computer Engineering

  • 7/24/2019 Lab Manual Spcc(1)

    20/62

    SYSTEM PROGRAMMING & COMPILER CONSTRUCTION

    @ar db >

    "code

    A*#>32>

    end

    Expan'e' co'e will be:

    "small model

    "data

    @ar db >

    "code

    /ov a13>

    'dd a132>

    end

    Algorithm

    !ass +:

    oth counters for /D0 and /0 i"e" /D0# and /0# are initiali7ed

    first"

    " /acro process in first pass reads each and ever! line of source code"

    2" f it is macro pseudo opcode3 the entire macro definition that follows is

    saved in /D0

    4" 0he macro name is entered in /0"

    ;" hen /ED pseudo opcode is encountered that means all macro definition

    have been processed3 so transfer control to pass2"

    Department of Computer Engineering

  • 7/24/2019 Lab Manual Spcc(1)

    21/62

    SYSTEM PROGRAMMING & COMPILER CONSTRUCTION

    !ass 2:

    " ,ead source code supplied b! passto see opcode mnemonic of each line is

    present in /0 or not"

    2" hen call is found3 the /D0+ is set to corresponding macro definition

    stored in /D0"

    4" 0he positional vs actual parameter list is prepared" Each line from /D0 is

    read and positional parameter is replaced b! actual parameter"

    ;" .canning of input file continues"

    C" hen /ED pseudo output is countered3 /D0 terminates e1pansion of

    6" macro"

    K" hen ED pseudo output is encountered3 e1pansion code is transferred to

    assembler for further processing"

    Pr)ram

    Binc!udestdiohD

    Binc!udeconiohD

    Binc!udest"in#hD

    Binc!udestd!ihD

    >oid main;T)Mfor(t=>%tUproRT"n%tAA)Mif( proRT"rhsRtTR>TU6C PP proRT"rhsRtTR>TVY> )proRiT"ftRstrlen(proRiT"ft)T=proRT"rhsRtTR>T%else if( proRT"rhsRtTR>TV=6C HH proRT"rhsRtTR>TU=Y> )Mtemp=proRT"rhsRtTR>T%if(temp==Z.Z)proRiT"ftRstrlen(proRiT"ft)T=Z5Z%findter()%FF

    brea%

    FF

    Department of Computer Engineering

  • 7/24/2019 Lab Manual Spcc(1)

    34/62

    SYSTEM PROGRAMMING & COMPILER CONSTRUCTION

    void findfol()Mint 3t3p3o3ch%char Nptr%for(=>%Un%AA)Mch=>%for(t=>%tUproRT"n%tAA)

    Mptr=strchr(proRT"rhsRtT3temp)%if( ptr )Mp=ptr-proRT"rhsRtT%

    if(proRT"rhsRtTRpATV=6C HH proRT"rhsRtTRpATU=Y>)M

    for(o=>%oUn%oAA)if(proRoT"lhsR>T==proRT"rhsRtTRpAT)Mstrcat(proRiT"fol3proRoT"ft)%chAA%FF

    else if(proRT"rhsRtTRpAT==ZQ>Z)Mtemp=proRT"lhsR>T%if(proRlT"rhsRITRpT==temp)

    continue%if(temp==Z.Z)strcat(proRiT"fol3OO)%findfol()%chAA%FelseMproRiT"folRstrlen(proRiT"fol)T=proRT"rhsRtTRpAT%chAA%F

    FFif(chV>)brea%F

    F

    void main()M9E Nf%clrscr()%

    for(i=>%iU>%iAA)

    Department of Computer Engineering

  • 7/24/2019 Lab Manual Spcc(1)

    35/62

    SYSTEM PROGRAMMING & COMPILER CONSTRUCTION

    proRiT"n=>%f=fopen(OtabC"t1tO3OrO)%while(?feof(f))Mfscanf(f3OLsO3proRnT"lhs)%if(nV>)Mif( strcmp(proRnT"lhs3proRn-T"lhs) == > )MproRnT"lhsR>T=ZQ>Z%fscanf(f3OLsO3proRn-T"rhsRproRn-T"nT)%proRn-T"nAA%continue%FFfscanf(f3OLsO3proRnT"rhsRproRnT"nT)%

    proRnT"nAA%nAA%F

    printf(OQnQn0[E B,'//', . '. 9**.QnQnO)%for(i=>%iUn%iAA)for(I=>%IUproRiT"n%IAA)printf(OLs -V LsQnO3proRiT"lhs3proRiT"rhsRIT)%

    proR>T"ftR>T=Z5Z%for(i=>%iUn%iAA)

    Mfor(I=>%IUproRiT"n%IAA)Mif( proRiT"rhsRITR>TU6C PP proRiT"rhsRITR>TVY> )MproRiT"ftRstrlen(proRiT"ft)T=proRiT"rhsRITR>T%Felse if( proRiT"rhsRITR>TV=6C HH proRiT"rhsRITR>TU=Y> )Mtemp=proRiT"rhsRITR>T%if(temp==Z.Z)

    proRiT"ftRstrlen(proRiT"ft)T=Z5Z%findter()%FFF

    printf(OQnQn9,.0QnO)%for(i=>%iUn%iAA)Mprintf(OQnLs -V O3proRiT"lhs)%

    for(I=>%IUstrlen(proRiT"ft)%IAA)

    Mfor(l=I-%lV=>%l--)

    Department of Computer Engineering

  • 7/24/2019 Lab Manual Spcc(1)

    36/62

    SYSTEM PROGRAMMING & COMPILER CONSTRUCTION

    if(proRiT"ftRlT==proRiT"ftRIT)brea%if(l==-)printf(OLcO3proRiT"ftRIT)%FF

    for(i=>%iUn%iAA)temp2RiT=proRiT"lhsR>T%proR>T"folR>T=ZZ%for(i=>%iUn%iAA)Mfor(l=>%lUn%lAA)Mfor(I=>%IUproRiT"n%IAA)M

    ptr=strchr(proRlT"rhsRIT3temp2RiT)%if( ptr )M

    p=ptr-proRlT"rhsRIT%if(proRlT"rhsRITRpATV=6C HH proRlT"rhsRITRpATU=Y>)Mfor(o=>%oUn%oAA)if(proRoT"lhsR>T==proRlT"rhsRITRpAT)strcat(proRiT"fol3proRoT"ft)%Felse if(proRlT"rhsRITRpAT==ZQ>Z)

    Mtemp=proRlT"lhsR>T%

    if(proRlT"rhsRITRpT==temp)continue%if(temp==Z.Z)strcat(proRiT"fol3OO)%findfol()%F

    elseproRiT"folRstrlen(proRiT"fol)T=proRlT"rhsRITRpAT%F

    FFF

    printf(OQnQn9**QnO)%for(i=>%iUn%iAA)Mprintf(OQnLs -V O3proRiT"lhs)%for(I=>%IUstrlen(proRiT"fol)%IAA)

    M

    for(l=I-%lV=>%l--)if(proRiT"folRlT==proRiT"folRIT)

    Department of Computer Engineering

  • 7/24/2019 Lab Manual Spcc(1)

    37/62

    SYSTEM PROGRAMMING & COMPILER CONSTRUCTION

    brea%if(l==-)printf(OLcO3proRiT"folRIT)%F

    F

    getch()%F

    Input -ile -or -irst an' -ollow !rogram:

    . 'E

    . a' p' t

    'G. f' w

    #onclusion: 0hus we haveimplemented 9ind 9irst and 9ollow in a given Brammar

    E9PERI8ENT NO*

    Code Gene"ation a!#o"ithm

    T(t!eSt#+ ) &)+e enrat()n a!)r(t3m*

    T3e)r

    In computing= code #ene"ation is the $"ocess y 5hich a com$i!e"Ws code #ene"ato"con>e"ts some inte"mediate "e$"esentation o- sou"ce code into a -o"m ;e#= machine codee studied code #ene"ation a!#o"ithm

    E9PERI8ENT NO*

    C)+e Opt(m(:at()n te&3n(#e"*

    T(t!eImplementation of co'e optimi&ation techni0ues*

    T3e)r

    PEEPHOLE OPTI8IATION

    A statement)y)statement code)#ene"ations st"ate#y o-ten $"oduce ta"#et code that

    contains "edundant inst"uctions and suo$tima! const"ucts The ua!ity o- such ta"#et code

    can e im$"o>ed y a$$!yin# o$timi8in#V t"ans-o"mations to the ta"#et $"o#"am A sim$!e

    ut e--ecti>e techniue -o" im$"o>in# the ta"#et code is peephole optimization= a method

    -o" t"yin# to im$"o>in# the $e"-o"mance o- the ta"#et $"o#"am y e/aminin# a sho"t

    seuence o- ta"#et inst"uctions ;ca!!ed the $ee$ho!e< and "e$!acin# these inst"uctions y a

    sho"te" o" -aste" seuence= 5hene>e" $ossi!e The $ee$ho!e is a sma!!= mo>in# 5indo5 on

    the ta"#et $"o#"am The code in the $ee$ho!e need not conti#uous= a!thou#h some

    im$!ementations do "eui"e this Qe sha!! #i>e the -o!!o5in# e/am$!es o- $"o#"am

    t"ans-o"mations that a"e cha"acte"istic o- $ee$ho!e o$timi8ations%

    Redundant)inst"uctions e!imination

    :!o5)o-)cont"o! o$timi8ations

    A!#e"aic sim$!i-ications

    Use o- machine idioms

    RE,UNTANT LOA,S AN, STORES

    Department of Computer Engineering

  • 7/24/2019 Lab Manual Spcc(1)

    41/62

    SYSTEM PROGRAMMING & COMPILER CONSTRUCTION

    I- 5e see the inst"uctions seuence

    ;0< MO' R3=a

    ;6< ;6

  • 7/24/2019 Lab Manual Spcc(1)

    42/62

    SYSTEM PROGRAMMING & COMPILER CONSTRUCTION

    L2?$@

    As the a"#ument o- the statement o- ;< e>a!uates to a constant tr#e it can e "e$!aced

    y

    I +e$# J0 )t) L2

    Pr(nt +e$#(n (n)rmat()n

    L2 ?&@

    As the a"#ument o- the -i"st statement o- ;c< e>a!uates to a constant t"ue= it can e "e$!aced

    y #oto 2" 0hena!! the statement that $"int deu##in# aids a"e mani-est!y un"eacha!e

    and can e e!iminated one at a time

    C)n&!#"()nThus 5e ha>e im$!emented code o$timi8ation

    Pr)ram

    &!a"" CO

    p#$!(& "tat(& =)(+ ma(n?Str(nK ar"@

    (nt (,%,$,;0M

    Str(n )pKK=)(+,ma(n?@, , , , , , ,

    , , , , , , , ,

    p(,,.*14,,,r,,5,M,

    A,,p(,

  • 7/24/2019 Lab Manual Spcc(1)

    43/62

    SYSTEM PROGRAMMING & COMPILER CONSTRUCTION

    )r?%0M%6M%@

    S"tem*)#t*pr(nt?)pK(K% @M

    S"tem*)#t*pr(nt!n?@M

    )r?(0M(65M(@

    )r?%0M%6M%@

    =)pK(K%M

    (?=*&)mpareT)?@0@

    a)pK(K%1*&3arAt?0@M

    (?a74 a65@

    r"K;K0)pK(K%-1M

    r"K;K1)pK(K%1M

    S"tem*)#t*pr(nt!n?I+ent((erQta!#e@M

    )r?(0M(6;M(@

    )r?%0M%62M%@

    Department of Computer Engineering

  • 7/24/2019 Lab Manual Spcc(1)

    44/62

    SYSTEM PROGRAMMING & COMPILER CONSTRUCTION

    S"tem*)#t*pr(nt?r"K(K%Qt@M

    S"tem*)#t*pr(nt!n?@M

    )r?(0M(65M(@

    )r?%0M%6M%@

    )r?$0M$6;M$@

    (?r"K$

    K0*&)mpareT)?)pK(K%@0)pK(K%1*&)mpareT)?@0@

    )pK(K%r"K$K1M

    S"tem*)#t*pr(nt!n?pr)ram &)+e ater &)+e )pt(m(:at()n@M

    )r?(0M(65M(@

    )r?%0M%6M%@

    S"tem*)#t*pr(nt?)pK(K% @M

    S"tem*)#t*pr(nt!n?@M

    Department of Computer Engineering

  • 7/24/2019 Lab Manual Spcc(1)

    45/62

    SYSTEM PROGRAMMING & COMPILER CONSTRUCTION

    OBTPBT

    pr)ram &)+e ("

    =)(+ ma(n?@

    p( .*14 , r 5 M

    A p( < r < r M

    I+ent((er a!#e

    p( .*14

    r 5

    pr)ram &)+e ater &)+e )pt(m(:at()n

    =)(+ ma(n?@

    p( .*14 , r 5 M

    A .*14 < 5 < 5 M

    C)n&!#"()nThus 5e ha>e im$!emented o- code o$timi8ation techniues

    Department of Computer Engineering

  • 7/24/2019 Lab Manual Spcc(1)

    46/62

    SYSTEM PROGRAMMING & COMPILER CONSTRUCTION

    Department of Computer Engineering

  • 7/24/2019 Lab Manual Spcc(1)

    47/62

    SYSTEM PROGRAMMING & COMPILER CONSTRUCTION

    E9PERI8ENT NO* 10

    St#+ ) !+ &)mman+ (n Bn('L(n#'*

    T(t!eSt#+ ) !+ &)mman+ (n Bn('L(n#'*

    T3e)r

    l'combines a number of obIect and archive files3 relocates their data and ties up s!mbolreferences" suall! the last step in compiling a program is to run l'"

    l'accepts iner #ommand anguage files written in a superset of '0H0Zs in Editor#ommand anguage s!nta13 to provide e1plicit and total control over the lining process"

    0his man page does not describe the command language% see the l'entr! in /ino/3 orthe manual ld: the Bliner3 for full details on the command language and on otheraspects of the Bliner"

    0his version of l'uses the general purpose 9Dlibraries to operate on obIect files" 0hisallows l'to read3 combine3 and write obIect files in man! different formats---for e1ample3#*99or /a.out/" Different formats ma! be lined together to produce an! availableind of obIect file"

    'side from its fle1ibilit!3 the Bliner is more helpful than other liners in providingdiagnostic information" /an! liners abandon e1ecution immediatel! upon encounteringan error% whenever possible3 l'continues e1ecuting3 allowing !ou to identif! other errors(or3 in some cases3 to get an output file in spite of the error)"

    Department of Computer Engineering

  • 7/24/2019 Lab Manual Spcc(1)

    48/62

    SYSTEM PROGRAMMING & COMPILER CONSTRUCTION

    0he Bliner l'is meant to cover a broad range of situations3 and to be as compatible aspossible with other liners" 's a result3 !ou have man! choices to control its behavior"

    OPTIONS

    0he liner supports a plethora of command-line options3 but in actual practice few of themare used in an! particular conte1t" 9or instance3 a freGuent use of l'is to lin standard

    ni1 obIect files on a standard3 supported ni1 s!stem" *n such a s!stem3 to lin a file/hello.o/:

    ld Lo 7lib7crt&.o hello.o Llc

    0his tells l'to produce a file called outputas the result of lining the file/7lib7crt&.o/with /hello.o/and the librar! /libc.a/3 which will come fromthe standard search directories" (.ee the discussion of the "loption below")

    .ome of the command-line options to l'ma! be specified at an! point in the command

    line" [owever3 options which refer to files3 such as "lor "T3 cause the file to be read at thepoint at which the option appears in the command line3 relative to the obIect files and otherfile options" ,epeating non-file options with a different argument will either have nofurther effect3 or override prior occurrences (those further to the left on the command line)of that option" *ptions which ma! be meaningfull! specified more than once are noted inthe descriptions below"

    on-option arguments are obIect files or archives which are to be lined together" 0he!ma! follow3 precede3 or be mi1ed in with command-line options3 e1cept that an obIect fileargument ma! not be placed between an option and its argument"

    suall! the liner is invoed with at least one obIect file3 but !ou can specif! other forms

    of binar! input files using "l3 "(3 and the script command language" f nobinar! input filesat all are specified3 the liner does not produce an! output3 and issues the message Noinput files"

    f the liner can not recogni7e the format of an obIect file3 it will assume that it is a linerscript" ' script specified in this wa! augments the main liner script used for the lin(either the default liner script or the one specified b! using "T)" 0his feature permits theliner to lin against a file which appears to be an obIect or an archive3 but actuall! merel!defines some s!mbol values3 or uses /!"2O4/or /QRO2/to load other obIects" otethat specif!ing a script in this wa! merel! augments the main liner script% use the "Toption to replace the default liner script entirel!"

    9or options whose names are a single letter3 option arguments must either follow theoption letter without intervening whitespace3 or be given as separate argumentsimmediatel! following the option that reGuires them"

    9or options whose names are multiple letters3 either one dash or two can precede theoption name% for e1ample3 "trace"symboland ""trace"symbolare eGuivalent" ote - thereis one e1ception to this rule" /ultiple letter options that start with a lower case ZoZ can onl!be preceeded b! two dashes" 0his is to reduce confusion with the "ooption" .o fore1ample "omagicsets the output file name to magicwhereas ""omagicsets the /'B#flag on the output"

    'rguments to multiple-letter options must either be separated from the option name b! aneGuals sign3 or be given as separate arguments immediatel! following the option thatreGuires them" 9or e1ample3 ""trace"symbol fooand ""trace"symbol1foo are eGuivalent"

    Department of Computer Engineering

  • 7/24/2019 Lab Manual Spcc(1)

    49/62

    SYSTEM PROGRAMMING & COMPILER CONSTRUCTION

    niGue abbreviations of the names of multiple-letter options are accepted"

    ote - if the liner is being invoed indirectl!3 via a compiler driver (eg gcc) then all theliner command line options should be prefi1ed b! "l3(or whatever is appropriate forthe particular compiler driver) lie this:

    gcc LKlLLstartgroup oo.o bar.o LKlLLendgroup

    0his is important3 because otherwise the compiler driver program ma! silentl! drop theliner options3 resulting in a bad lin"

    [ere is a table of the generic command line switches accepted b! the Bliner:

    "akeyword

    0his option is supported for [+Xcompatibilit!" 0he keywordargument must be

    one of the strings archi)e3 share'3 or 'efault" "aarchi)eis functionall! eGuivalent

    to "*static3 and the other two e!words are functionall! eGuivalent to "*'ynamic"0his option ma! be used an! number of times"

    "Aarchitecture

    ""architecture1architecture

    n the current release of l'3 this option is useful onl! for the ntel Y6> famil! of

    architectures" n that l'configuration3 the architectureargument identifies the

    particular architecture in the Y6> famil!3 enabling some safeguards and modif!ing

    the archive-librar! search path"

    9uture releases of l'ma! support similar functionalit! for other architecture

    families"

    "binput-format

    ""format1input-format

    l'ma! be configured to support more than one ind of obIect file" f !our l'is

    configured this wa!3 !ou can use the "boption to specif! the binar! format for input

    obIect files that follow this option on the command line" Even when l'is configured

    to support alternative obIect formats3 !ou donZt usuall! need to specif! this3 as l'

    should be configured to e1pect as a default input format the most usual format on

    each machine" input-formatis a te1t string3 the name of a particular format supported

    b! the 9Dlibraries" (ou can list the available binar! formats with ob4'ump "i")

    ou ma! want to use this option if !ou are lining files with an unusual binar!

    format" ou can also use "bto switch formats e1plicitl! (when lining obIect files of

    different formats)3 b! including "binput-formatbefore each group of obIect files in a

    particular format"

    Department of Computer Engineering

  • 7/24/2019 Lab Manual Spcc(1)

    50/62

    SYSTEM PROGRAMMING & COMPILER CONSTRUCTION

    0he default format is taen from the environment variable /Q"O4ARQE4/"

    ou can also define the input format from a script3 using the command /4ARQE4/%

    "cMRI-commandfile

    ""mri"script1MRI-commandfile

    9or compatibilit! with liners produced b! /,3 l'accepts script files written in an

    alternate3 restricted command language3 described in the /,#ompatible .cript

    9iles section of Bld documentation" ntroduce /,script files with the option "c%

    use the "Toption to run liner scripts written in the general-purpose l'scripting

    language" fMRI-cmdfiledoes not e1ist3 l'loos for it in the directories specified b!

    an! "Loptions"

    "'

    "'c

    "'p

    0hese three options are eGuivalent% multiple forms are supported for compatibilit!

    with other liners" 0he! assign space to common s!mbols even if a relocatable

    output file is specified (with "r)" 0he script command

    /:R?E_?11"_A?A4!"/has the same effect"

    "eentry

    ""entry1entry

    se entryas the e1plicit s!mbol for beginning e1ecution of !our program3 rather

    than the default entr! point" f there is no s!mbol named entry3 the liner will tr! to

    parse entryas a number3 and use that as the entr! address (the number will be

    interpreted in base >% !ou ma! use a leading 5xfor base 63 or a leading 5for base

    8)"

    "E

    ""export"'ynamic

    hen creating a d!namicall! lined e1ecutable3 add all s!mbols to the d!namic

    s!mbol table" 0he d!namic s!mbol table is the set of s!mbols which are visible from

    d!namic obIects at run time"

    f !ou do not use this option3 the d!namic s!mbol table will normall! contain onl!

    those s!mbols which are referenced b! some d!namic obIect mentioned in the lin"

    f !ou use /dlopen/to load a d!namic obIect which needs to refer bac to thes!mbols defined b! the program3 rather than some other d!namic obIect3 then !ou

    Department of Computer Engineering

  • 7/24/2019 Lab Manual Spcc(1)

    51/62

    SYSTEM PROGRAMMING & COMPILER CONSTRUCTION

    will probabl! need to use this option when lining the program itself"

    ou can also use the version script to control what s!mbols should be added to the

    d!namic s!mbol table if the output format supports it" .ee the description of

    "")ersion"script in reM@E,.*F"

    "E*

    in big-endian obIects" 0his affects the default output format"

    "EL

    in little-endian obIects" 0his affects the default output format"

    "f

    ""auxiliaryname

    hen creating an E9shared obIect3 set the internal D0

  • 7/24/2019 Lab Manual Spcc(1)

    52/62

    SYSTEM PROGRAMMING & COMPILER CONSTRUCTION

    .ome older liners used the "-option throughout a compilation toolchain for

    specif!ing obIect-file format for both input and output obIect files" 0he Bliner

    uses other mechanisms for this purpose: the "b3 ""format3 ""oformatoptions3 the

    /4ARQE4/command in liner scripts3 and the /Q"O4ARQE4/environment

    variable" 0he Bliner will ignore the "-option when not creating an E9shared

    obIect"

    "fininame

    hen creating an E9e1ecutable or shared obIect3 call '/Ewhen the e1ecutable

    or shared obIect is unloaded3 b! setting D0

  • 7/24/2019 Lab Manual Spcc(1)

    53/62

    SYSTEM PROGRAMMING & COMPILER CONSTRUCTION

    'dd archive file archiveto the list of files to lin" 0his option ma! be used an!

    number of times" l'will search its path-list for occurrences of /libarchive.a/for

    ever! archivespecified"

    *n s!stems which support shared libraries3 l'ma! also search for libraries with

    e1tensions other than /.a/" .pecificall!3 on E9and .un*. s!stems3 l'will searcha director! for a librar! with an e1tension of /.so/before searching for one with

    an e1tension of /.a/" ! convention3 a /.so/e1tension indicates a shared librar!"

    0he liner will search an archive onl! once3 at the location where it is specified on

    the command line" f the archive defines a s!mbol which was undefined in some

    obIect which appeared before the archive on the command line3 the liner will

    include the appropriate file(s) from the archive" [owever3 an undefined s!mbol in an

    obIect appearing later on the command line will not cause the liner to search the

    archive again"

    .ee the "7option for a wa! to force the liner to search archives multiple times"

    ou ma! list the same archive multiple times on the command line"

    0his t!pe of archive searching is standard for ni1 liners" [owever3 if !ou are

    using l'on 'X3 note that it is different from the behaviour of the 'Xliner"

    "Lsearchdir

    ""library"path1searchdir

    'dd path searchdirto the list of paths that l'will search for archive libraries and l'

    control scripts" ou ma! use this option an! number of times" 0he directories are

    searched in the order in which the! are specified on the command line" Directories

    specified on the command line are searched before the default directories" 'll "L

    options appl! to all "loptions3 regardless of the order in which the options appear"

    f searchdirbegins with //3 then the //will be replaced b! the sysroot prefix3 a

    path specified when the liner is configured"

    0he default set of paths searched (without being specified with "L) depends on

    which emulation mode l'is using3 and in some cases also on how it was configured"

    0he paths can also be specified in a lin script with the /3EAR?F_D!R/command"

    Directories specified this wa! are searched at the point in which the liner script

    appears in the command line"

    "memulation

    Emulate the emulationliner" ou can list the available emulations with the

    Department of Computer Engineering

  • 7/24/2019 Lab Manual Spcc(1)

    54/62

    SYSTEM PROGRAMMING & COMPILER CONSTRUCTION

    "")erboseor "8options"

    f the "moption is not used3 the emulation is taen from the /DE1OA4!"/

    environment variable3 if that is defined"

    *therwise3 the default emulation depends upon how the liner was configured"

    "%

    ""print"map

    +rint a lin map to the standard output" ' lin map provides information about the

    lin3 including the following:

    N

    here obIect files and s!mbols are mapped into memor!"

    N

    [ow common s!mbols are allocated"

    N

    'll archive members included in the lin3 with a mention of the s!mbol

    which caused the archive member to be brought in"

    "n

    ""nmagic

    0urn off page alignment of sections3 and mar the output as /"1AQ!?/if possible"

    "N

    ""omagic

    .et the te1t and data sections to be readable and writable" 'lso3 do not page-align the

    data segment3 and disable lining against shared libraries" f the output format

    supports ni1 st!le magic numbers3 mar the output as /1AQ!?/"

    ""no"omagic

    0his option negates most of the effects of the "Noption" t sets the te1t section to be

    read-onl!3 and forces the data segment to be page-aligned" ote - this option does

    not enable lining against shared libraries" se "*'ynamicfor this"

    "ooutput

    ""output1output

    se outputas the name for the program produced b! l'% if this option is not

    Department of Computer Engineering

  • 7/24/2019 Lab Manual Spcc(1)

    55/62

    SYSTEM PROGRAMMING & COMPILER CONSTRUCTION

    specified3 the name a.outis used b! default" 0he script command /O42O4/can

    also specif! the output file name"

    "level

    f levelis a numeric values greater than 7ero l'optimi7es the output" 0his might tae

    significantl! longer and therefore probabl! should onl! be enabled for the final

    binar!"

    "0

    ""emit"relocs

    eave relocation sections and contents in full! lined e1ececutables" +ost lin

    anal!sis and optimi7ation tools ma! need this information in order to perform

    correct modifications of e1ecutables" 0his results in larger e1ecutables"

    0his option is currentl! onl! supported on E9platforms"

    "r

    ""relocateable

    Benerate relocatable output---i"e"3 generate an output file that can in turn serve as

    input to l'" 0his is often calledpartial linking" 's a side effect3 in environments that

    support standard ni1 magic numbers3 this option also sets the output fileZs magic

    number to /1AQ!?/" f this option is not specified3 an absolute file is produced"

    hen lining #AAprograms3 this option will notresolve references to constructors%

    to do that3 use "9r"

    hen an input file does not have the same format as the output file3 partial lining is

    onl! supported if that input file does not contain an! relocations" Different output

    formats can have further restrictions% for e1ample some /a.out/-based formats do

    not support partial lining with input files in other formats at all"

    0his option does the same thing as "i"

    "(filename

    ""4ust"symbols1filename

    ,ead s!mbol names and their addresses from filename3 but do not relocate it or

    include it in the output" 0his allows !our output file to refer s!mbolicall! to absolute

    locations of memor! defined in other programs" ou ma! use this option more than

    once"

    9or compatibilit! with other E9liners3 if the "(option is followed b! a director!name3 rather than a file name3 it is treated as the "rpathoption"

    Department of Computer Engineering

  • 7/24/2019 Lab Manual Spcc(1)

    56/62

    SYSTEM PROGRAMMING & COMPILER CONSTRUCTION

    "s

    ""strip"all

    *mit all s!mbol information from the output file"

    "S

    ""strip"'ebug

    *mit debugger s!mbol information (but not all s!mbols) from the output file"

    "t

    ""trace

    +rint the names of the input files as l'processes them"

    "Tscriptfile

    ""script1scriptfile

    se scriptfileas the liner script" 0his script replaces l'Zs default liner script (rather

    than adding to it)3 so commandfilemust specif! ever!thing necessar! to describe the

    output file" f scriptfiledoes not e1ist in the current director!3 /ld/loos for it in

    the directories specified b! an! preceding "Loptions" /ultiple "Toptions

    accumulate"

    "usymbol

    ""un'efine'1symbol

    9orce symbolto be entered in the output file as an undefined s!mbol" Doing this

    ma!3 for e1ample3 trigger lining of additional modules from standard libraries" "u

    ma! be repeated with different option arguments to enter additional undefined

    s!mbols" 0his option is eGuivalent to the /EN4ER"/liner script command"

    "9r

    9or an!thing other than #AAprograms3 this option is eGuivalent to "r: it generatesrelocatable output---i"e"3 an output file that can in turn serve as input to l'" hen

    lining #AAprograms3 "9rdoesresolve references to constructors3 unlie "r" t does

    not wor to use "9ron files that were themselves lined with "9r% once the

    constructor table has been built3 it cannot be added to" se "9ronl! for the last

    partial lin3 and "rfor the others"

    ""uni0ue1S!"I#$;

    #reates a separate output section for ever! input section matching S!"I#$3 or if the

    optional wildcard S!"I#$argument is missing3 for ever! orphan input section" 'norphan section is one not specificall! mentioned in a liner script" ou ma! use this

    Department of Computer Engineering

  • 7/24/2019 Lab Manual Spcc(1)

    57/62

    SYSTEM PROGRAMMING & COMPILER CONSTRUCTION

    option multiple times on the command line% t prevents the normal merging of input

    sections with the same name3 overriding output section assignments in a liner

    script"

    ")

    "")ersion

    "8

    Displa! the version number for l'" 0he "8option also lists the supported emulations"

    "x

    ""'iscar'"all

    Delete all local s!mbols"

    "