View
50
Download
0
Category
Tags:
Preview:
Citation preview
Click to edit Master subtitle style
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Teknik Kompilasi
Pertemuan 10Intermediate Code Generator (Ekspresi)Aries Yoshan
© Sekolah Tinggi Teknik Surabaya
11
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Bacaan» Compilers:
Principles, Techniques, and ToolsAho, Sethi, UllmanBab 7: Run-Time EnvironmentsBab 8: Intermediate Code GenerationBab 2: A Simple One-Pass Compiler
© Sekolah Tinggi Teknik Surabaya
22
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Intermediate Code Generator» Bertugas membangkitkan
representasi intermediate yang mewakili program asal.
» Representasi intermediate dapat dipandang sebagai sebuah program untuk mesin abstrak.
33
© Sekolah Tinggi Teknik Surabaya
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Representasi Intermediate» Syntax tree
˃ Representasi grafis berbentuk tree yang menggambarkan struktur hirarki program asal.
» Postfix notation˃ Notasi yang merupakan hasil penelusuran syntax tree
secara postorder.
» Three-address code˃ Bentuk yang sangat mirip dengan kode assembly.
˃ Contoh kode assembly: mov ax, 10
44
© Sekolah Tinggi Teknik Surabaya
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Syntax Treea = b * -c + b * -c
55
© Sekolah Tinggi Teknik Surabaya
+
c
* *
b uminus b uminus
c
assign
a
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Postfix Notationa = b * -c + b * -c
a b c um inus * b c um inus * + assign
66
© Sekolah Tinggi Teknik Surabaya
+
c
* *
b uminus b uminus
c
assign
a
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Three-Address Codea = b * -c + b * -c
a b c um inus * b c um inus * + assign
77
© Sekolah Tinggi Teknik Surabaya
mov t1, bmov t2, cuminus t2mul t1, t2mov t3, bmov t4, cuminus t4mul t3, t4add t1, t3mov a, t3
+
c
* *
b uminus b uminus
c
assign
a
t1 = bt2 = ct2 = - t2t1 = t1 * t2t3 = bt4 = ct4 = - t4t3 = t3 * t4t1 = t1 + t3a = t3
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Variabel Temporer» t1, t2, t3, t4
adalah contoh variabel temporer.
» Diperlukan sebagai penampung sementara.
88
© Sekolah Tinggi Teknik Surabaya
mov t1, bmov t2, cuminus t2mul t1, t2mov t3, bmov t4, cuminus t4mul t3, t4add t1, t3mov a, t3
t1 = bt2 = ct2 = - t2t1 = t1 * t2t3 = bt4 = ct4 = - t4t3 = t3 * t4t1 = t1 + t3a = t3
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Triplet» Merupakan implementasi three-
address code yang paling umum.
» Tersusun atas tiga bagian:˃ Kode operator (opcode = operator code)
˃ Operand pertama
˃ Operand kedua
» Serupa dengan bahasa assembly.
99
© Sekolah Tinggi Teknik Surabaya
opcode operand 1 operand 2
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Struktur OperandSeperti halnya bahasa assembly, operand 1 dan operand 2 terdiri dari dua bagian:
» Mode pengalamatan
» Nilai
1010
© Sekolah Tinggi Teknik Surabaya
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Struktur Operand dan Triplet
public class O perand { public int m ode; public int nilai; ...}
public class Triplet { public int opCode; public O perand operand1, operand2; ...}
1111
© Sekolah Tinggi Teknik Surabaya
opcode operand 1 operand 2mode nilai mode nilai
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Mode Pengalamatan» Mode temporer
˃ nilai menyatakan indeks variabel temporer.
˃ nilai = 1 untuk menyatakan t1.
» Mode integer˃ nilai menyatakan bilangan integer.
» Mode real˃ nilai menyatakan indeks yang merujuk ke sebuah
tabel real.
» Mode string˃ nilai menyatakan indeks yang merujuk ke sebuah
tabel string. 1212
© Sekolah Tinggi Teknik Surabaya
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Tipe Boolean» Operand dengan nilai bertipe
Boolean dapat direpresentasikan dengan mode integer.
» Boolean true diwakili oleh nilai = 1.
» Boolean false diwakili oleh nilai = 0.
1313
© Sekolah Tinggi Teknik Surabaya
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Tabel Real dan Tabel String» Field nilai pada struktur operand
bertipe int, sehingga untuk menyimpan nilai operand yang bertipe real dan string, harus menggunakan tabel real dan tabel string.
» Untuk operand bertipe real, harus menggunakan mode real, dan nilai = indeks pada tabel real.
» Demikian juga untuk tipe string.1414
© Sekolah Tinggi Teknik Surabaya
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Contoh Tripletmov t1, 10
= mov temporer 1 integer 10
mov t2, 10.5
= mov temporer 2 real 0
indeks 0 berisi 10.5
mov t3, "stts"= mov temporer 3 string 0
indeks 0 berisi "stts"
1515
© Sekolah Tinggi Teknik Surabaya
Tabel RealIndek
sNilai
0 10.5Tabel String
Indeks
Nilai
0 "stts"
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Indeks Variabel Temporer» Variabel temporer pertama akan
mendapatkan indeks = 1.
» tempIndex diinisialisasi 0.
% %static TabelSim bol tabelSim bol = new TabelSim bol();static int tem pIndex = 0;...
1616
© Sekolah Tinggi Teknik Surabaya
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Tabel Real dan Tabel String» Diimplementasikan dengan hash
table.
public class Tabel< E> { private H ashM ap< E,Integer> tabel = new H ashM ap< E,Integer> (); ... }
% %static TabelSim bol tabelSim bol = new TabelSim bol();static Tabel< D ouble> tabelReal = new Tabel< D ouble> ();static Tabel< String> tabelString = new Tabel< String> ();
1717
© Sekolah Tinggi Teknik Surabaya
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Tabel Real dan Tabel Stringpublic class Tabel< E> { private H ashM ap< E,Integer> tabel = new H ashM ap< E,Integer> (); private int count = 0; public int cari(E nilai) { Integer index = tabel.get(nilai); return(index = = null ? -1 : index); } ...}
1818
© Sekolah Tinggi Teknik Surabaya
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Tabel Real dan Tabel Stringpublic class Tabel< E> { private H ashM ap< E,Integer> tabel = new H ashM ap< E,Integer> (); private int count = 0; ... public int tam bah(E nilai) { Integer index = tabel.get(nilai); if (index = = null) { index = count+ + ; tabel.put(nilai, index); } return index; }} 19
19
© Sekolah Tinggi Teknik Surabaya
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Run-Time MemoryStruktur memori yang dialokasi saat sebuah program dijalankan:
» Code Segment˃ berisi kode program
» Data Segment˃ berisi variabel global,
konstanta real dan string
» Stack Segment˃ stack untuk urusan
pemanggilan subrutin dan variabel lokal
2020
© Sekolah Tinggi Teknik Surabaya
Code(Code
Segment)Static Data
(Data Segment)
Stack(Stack
Segment)
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Mode PengalamatanTerkait pengalamatan memori:
» Mode dataoffset˃ nilai menyatakan offset pada data segment.
» Mode codeoffset˃ nilai menyatakan offset pada code segment.
» Mode stackoffset˃ nilai menyatakan offset pada stack segment.
2121
© Sekolah Tinggi Teknik Surabaya
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Variabel Global» Untuk mengakses variabel global,
gunakan mode dataoffset.
» Pada Tabel Simbol, perlu ditambahkan sebuah field/kolom untuk menyimpan offset pada data segment, yang mana offset ini diawali dari nilai 0.
» Field baru ini diberi nama dataOffset.
2222
© Sekolah Tinggi Teknik Surabaya
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Perubahan Tabel Simbolpublic class Sim bol im plem ents Com parable< Sim bol> { String nam a; int jenis; int tipeD ata; int dataO ffset; ...}
public class TabelSim bol { private ArrayList< Sim bol> tabelSim bol = new ArrayList< Sim bol> (); ...
}2323
© Sekolah Tinggi Teknik Surabaya
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Inisialisasi dataOffset% %static TabelSim bol tabelSim bol = new TabelSim bol();static Tabel< D ouble> tabelReal = new Tabel< D ouble> ();static Tabel< String> tabelString = new Tabel< String> ();static int dataO ffset = -1;static int tem pIndex = 0;
...
dataOffset diinisialisasi -1 agar variabel global pertama memperoleh dataOffset = 0
2424
© Sekolah Tinggi Teknik Surabaya
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Menetapkan dataOffsetdim _stm t : D IM id_list AS nam a_tipe
id_list : id_unique | id_list ',' id_unique
id_unique : ID { Sim bol o = new Sim bol( $1, Sim bol.VARIABEL, Sim bol.N O N E); if (!tabelSim bol.tam bah(o)) yyerror("D uplicate ID ");
o.dataO ffset = + + dataO ffset; }
2525
© Sekolah Tinggi Teknik Surabaya
Asumsi: variabel yang dideklarasikan di sini adalah variabel global
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Struktur Tabel Kode» Tabel Kode merupakan array yang
elemennya adalah triplet.
» Di Java: ArrayList<Triplet>
triplet
2626
© Sekolah Tinggi Teknik Surabaya
opcode operand 1 operand 2mode nilai mode nilai
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Inisialisasi Tabel Kode% %static TabelSim bol tabelSim bol = new TabelSim bol();static Tabel< D ouble> tabelReal = new Tabel< D ouble> ();static Tabel< String> tabelString = new Tabel< String> ();static ArrayList< Triplet> tabelKode = new ArrayList< Triplet> ();static int dataO ffset = -1;static int tem pIndex = 0;
...
2727
© Sekolah Tinggi Teknik Surabaya
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Macam Triplet» Triplet dua operand
˃ Umumnya triplet yang dipakai berjenis triplet dua operand.
˃ Contoh: mov temporer 1 integer 10
» Triplet satu operand˃ Contoh: inc temporer 1
+ Triplet ini nantinya dipakai untuk membentuk konstruksi for.
˃ Contoh lain: uminus temporer 2
2828
© Sekolah Tinggi Teknik Surabaya
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Macam Tripletpublic class Triplet { public int opCode; public O perand operand1, operand2;
Triplet(int opCode, O perand op) { this.opCode = opCode; this.operand1 = op; this.operand2 = null; }
Triplet(int opCode, O perand op1, O perand op2) { this.opCode = opCode; this.operand1 = op1; this.operand2 = op2; }}
2929
© Sekolah Tinggi Teknik Surabaya
Triplet satu operand
Triplet dua operand
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Contoh Triplet pada Ekspresia = b * -c + b * -c
m ov tem porer 1 dataoffset 1 (b)m ov tem porer 2 dataoffset 2 (c)um inus tem porer 2m ul tem porer 1 tem porer 2m ov tem porer 3 dataoffset 1 (b)m ov tem porer 4 dataoffset 2 (c)um inus tem porer 4m ul tem porer 3 tem porer 4add tem porer 1 tem porer 3m ov dataoffset 0 (a) tem porer 1
3030
© Sekolah Tinggi Teknik Surabaya
Asumsi: urutan deklarasi variabel adalah a, b, c
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Atribut expr, term, factor, ...» Agar bisa menghasilkan urutan
triplet sesuai dengan ekspresi yang tertera pada source code, setiap LHS dari production yang terkait ekspresi
(expr, term, factor, dst.) mempunyai atribut berupa dua hal:˃ Indeks variabel temporer
˃ Tipe data hasil ekspresi dipertahankan untuk semantic analysis˃
3131
© Sekolah Tinggi Teknik Surabaya
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Atribut expr, term, factor, ...public class ExprAttr { public int tem pIndex; public int tipeD ata;
...}
% type < obj> expr term factor
3232
© Sekolah Tinggi Teknik Surabaya
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Konstanta Integerfactor : ... | IN TCO N ST { tabelKode.add( new Triplet(Triplet.M O V, new O perand( O perand.TEM PO RER, + + tem pIndex), new O perand( O perand.IN TEG ER, $1))); $$ = new ExprAttr(tem pIndex, Sim bol.IN TEG ER); } | ...
3333
© Sekolah Tinggi Teknik Surabaya
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Konstanta Realfactor : ... | REALCO N ST { tabelKode.add( new Triplet(Triplet.M O V, new O perand( O perand.TEM PO RER, + + tem pIndex), new O perand( O perand.REAL, tabelReal.tam bah($1)))); $$ = new ExprAttr(tem pIndex, Sim bol.REAL); } | ...
3434
© Sekolah Tinggi Teknik Surabaya
Perlakuan konstanta string mirip dengan perlakuan konstanta real
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Variabel (Asumsi: Global)factor : ... | ID { Sim bol o = tabelSim bol.cari($1); ... // declaration checker tabelKode.add( new Triplet(Triplet.M O V, new O perand( O perand.TEM PO RER, + + tem pIndex), new O perand( O perand.D ATAO FFSET, o.dataO ffset))); $$ = new ExprAttr(tem pIndex, o.tipeD ata); } 35
35
© Sekolah Tinggi Teknik Surabaya
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Operasi Perkalianterm : term '*' factor { ... // type checker tabelKode.add( new Triplet(Triplet.M U L, new O perand( O perand.TEM PO RER, ((Tem pAttr)$1).tem pIndex), new O perand( O perand.TEM PO RER, ((Tem pAttr)$3).tem pIndex))); $$ = new ExprAttr( ((Tem pAttr)$1).tem pIndex, ...); } | ... 36
36
© Sekolah Tinggi Teknik Surabaya
Operasi lainnya mirip
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Reuse Indeks Var. Temporer» Indeks variabel temporer selalu
bertambah.
» Variabel temporer nantinya akan menjadi variabel lokal yang tersimpan di stack.
» Jumlah variabel temporer yang terlalu banyak memboroskan memori.
» Harus ada efisiensi penggunaan indeks variabel temporer. Caranya: reuse. 37
37
© Sekolah Tinggi Teknik Surabaya
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Reuse Indeks Var. Temporera = b * -c + b * -c
m ov tem porer 1 dataoffset 1 (b)m ov tem porer 2 dataoffset 2 (c)um inus tem porer 2m ul tem porer 1 tem porer 2m ov tem porer 2 dataoffset 1 (b)m ov tem porer 3 dataoffset 2 (c)um inus tem porer 3m ul tem porer 2 tem porer 3add tem porer 1 tem porer 2m ov dataoffset 0 (a) tem porer 1
3838
© Sekolah Tinggi Teknik Surabaya
Asumsi: urutan deklarasi variabel adalah a, b, c
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Reuse Indeks Var. Temporer» Agar indeks variabel temporer bisa
digunakan ulang (reuse), yang berubah adalah semantic rule bagian operasi: penjumlahan, pengurangan, perkalian, pembagian, pangkat (bila ada), dst.
3939
© Sekolah Tinggi Teknik Surabaya
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Operasi Perkalian Versi 2term : term '*' factor { ... // type checker tabelKode.add( new Triplet(Triplet.M O V, new O perand( O perand.TEM PO RER, ((Tem pAttr)$1).tem pIndex), new O perand( O perand.TEM PO RER, ((Tem pAttr)$3).tem pIndex))); tem pIndex = ((Tem pAttr)$1).tem pIndex; $$ = new ExprAttr( ((Tem pAttr)$1).tem pIndex, ...); } | ...
4040
© Sekolah Tinggi Teknik Surabaya
Operasi lainnya mirip
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Tiga Golongan OperasiBerdasarkan tipe data operandnya, operasi dibagi menjadi tiga golongan:
» Operasi bilangan integer/bulat˃ mode integer
» Operasi bilangan real/pecahan˃ mode real
» Operasi string˃ mode string
Operasi Boolean menggunakan operasi bilangan integer; operand tipe data Boolean menggunakan mode integer.
4141
© Sekolah Tinggi Teknik Surabaya
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Tiga Golongan Operasi» Pembagian operasi menjadi tiga
golongan dikarenakan ada pembedaan penanganan tiga golongan tersebut dalam kode mesin.
» Operasi bilangan integer diproses oleh CPU.
» Operasi bilangan real diproses oleh floating-point co-processor atau mathematic co-processor (kini terintegrasi dalam sebuah CPU).
» Operasi string diproses oleh CPU. Perintah assembly untuk operasi string menggunakan awalan repe.˃ Contoh: repe cmpsb
4242
© Sekolah Tinggi Teknik Surabaya
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Tiga Golongan Operasi» Setiap operand pada intermedate
code mempunyai sebuah opcode untuk setiap golongan operasi.˃ Contoh:
mov – untuk operasi bilangan integerfmov – untuk operasi bilangan realsmov – untuk operasi string
» Juga ada opcode untuk konversi tipe data dari integer ke real, dan sebaliknya.˃ Contoh:
itof – untuk konversi dari integer ke realftoi – untuk konversi dari real ke integer 43
43
© Sekolah Tinggi Teknik Surabaya
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Triplet Stl. Penggolongana = b * -c + b * -c
fm ov tem porer 1 dataoffset 1 (b)m ov tem porer 2 dataoffset 2 (c)um inus tem porer 2itof tem porer 2fm ul tem porer 1 tem porer 2fm ov tem porer 2 dataoffset 1 (b)m ov tem porer 3 dataoffset 2 (c)um inus tem porer 3itof tem porer 3fm ul tem porer 2 tem porer 3fadd tem porer 1 tem porer 2fm ov dataoffset 0 (a) tem porer 1 44
44
© Sekolah Tinggi Teknik Surabaya
a dan b = real, c = integer
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Menentukan OpCode» Untuk menentukan opcode yang
sesuai dengan golongan operasi, diperlukan type checker.
4545
© Sekolah Tinggi Teknik Surabaya
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Konstanta Real Versi 2factor : ... | REALCO N ST { tabelKode.add( new Triplet(Triplet.FM O V, new O perand( O perand.TEM PO RER, + + tem pIndex), new O perand( O perand.REAL, tabelReal.tam bah($1)))); $$ = new ExprAttr(tem pIndex, Sim bol.REAL); } | ...
4646
© Sekolah Tinggi Teknik Surabaya
Perlakuan konstanta string mirip dengan perlakuan konstanta real
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Operasi Perkalian Versi 3term : term '*' factor { int tipe1 = ((Tem pAttr)$1).tipeD ata; int tipe2 = ((Tem pAttr)$3).tipeD ata; if (tipe1 = = tipe2) { if (tipe1 = = Sim bol.IN TEG ER || tipe1 = = Sim bol.REAL) { tabelKode.add( new Triplet( (tipe1 = = Sim bol.IN TEG ER) ? Triplet.M O V : Triplet.FM O V, new O perand(O perand.TEM PO RER, ((Tem pAttr)$1).tem pIndex), new O perand(O perand.TEM PO RER, ((Tem pAttr)$3).tem pIndex))); $$ = new ExprAttr( ((Tem pAttr)$1).tem pIndex, tipe1); } ...
4747
© Sekolah Tinggi Teknik Surabaya
Operasi lainnya mirip
4/1
1/1
2
© Sekolah Tinggi Teknik Surabaya
Operasi Perkalian Versi 3term : term '*' factor { ... if (tipe1 != tipe2) { if (tipe1 = = Sim bol.REAL & & tipe2 = = Sim bol.IN TEG ER) { tabelKode.add( new Triplet(Triplet.ITO F, new O perand(O perand.TEM PO RER, ((Tem pAttr)$1).tem pIndex) ); tabelKode.add( new Triplet(Triplet.FM O V, new O perand(O perand.TEM PO RER, ((Tem pAttr)$1).tem pIndex), new O perand(O perand.TEM PO RER, ((Tem pAttr)$3).tem pIndex))); $$ = new ExprAttr( ((Tem pAttr)$1).tem pIndex, Sim bol.REAL); } ...
4848
© Sekolah Tinggi Teknik Surabaya
Operasi lainnya mirip
Recommended