Teknik Kompilasi - 10 - Intermediate Code Generator 1

download Teknik Kompilasi - 10 - Intermediate Code Generator 1

of 48

Transcript of Teknik Kompilasi - 10 - Intermediate Code Generator 1

11

Teknik Kompilasi

4/11/12

Pertemuan 10 Click to edit Master subtitle Intermediate Code Generator (Ekspresi)

style

Aries Yoshan

Sekolah Tinggi Teknik Surabaya

Bacaan

Compilers: Principles, Techniques, and Tools Aho, Sethi, Ullman Bab 7: Run-Time Environments Bab 8: Intermediate Code Generation Bab 2: A Simple One-Pass Compiler

4/11/12

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

4/11/12

3 3 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

4/11/12

4 4 Sekolah Tinggi Teknik Surabaya

Syntax Treea = b * -c + b * -cassign a * b4/11/12

+ * uminus b c uminus c5 5

Sekolah Tinggi Teknik Surabaya

Postfix Notationa = b * -c + b * -cassign a * b4/11/12

+ * uminus b c uminus c6 6

a b c u m i u s * b c u m i u s * + assi n n n g Sekolah Tinggi Teknik Surabaya

Three-Address Codea = b * -c + b * -c mov t1, t1 = b t2 = assign mov t2, t2 = c t2 a + uminus t1 = t2 * t2 * * mul t1, t3 = t2 t4 = b uminus b uminus mov t3, t4 = b t4 c c mov t4, t3 = c * t4 uminus t1 = a b c u m i u s * b c u m i u s * + assi n n n t4 g + t3 Sekolah Tinggi Teknik Surabaya

b c t1 b c t3 t17 7

4/11/12

Variabel Temporermov t1, b mov t2, c uminus t2 mul t1, t2 mov t3, b mov t4, c uminus t4 t1 = t2 = t2 = t2 t1 = * t2 t3 = t4 = t4 = t4 t3 = * t4 t1 = + t3 b c t1 b c t3 t18 8

t1, t2, t3, t4 adalah contoh variabel temporer. Diperlukan sebagai penampung sementara.

4/11/12

Sekolah Tinggi Teknik Surabaya

Triplet

Merupakan implementasi threeaddress code yang paling umum. Tersusun atas tiga bagian:

Kode operator (opcode = operator code) Operand pertama Operand kedua

4/11/12

Serupa dengan bahasa assembly.9 9

opcode operand 1 operand 2

Sekolah Tinggi Teknik Surabaya

Struktur OperandSeperti halnya bahasa assembly, operand 1 dan operand 2 terdiri dari dua bagian:

Mode pengalamatan Nilai

4/11/12

10 10 Sekolah Tinggi Teknik Surabaya

Struktur Operand dan Tripletopcode operand 1mode nilai

operand 2mode nilai

p ub l c cl i ass O perand { p ub l c i m ode; i nt p ub l c i ni ai i nt l ; .. . } p ub l c cl i ass Tri l { p et p ub l c i op C ode; i nt p ub l c O p erand op erand1, op erand2; i .. . } Sekolah Tinggi Teknik Surabaya 4/11/12

11 11

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.

4/11/12

Mode string

nilai menyatakan indeks yang merujuk ke sebuah tabel string.

12 12

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.

4/11/12

13 13 Sekolah Tinggi Teknik Surabaya

Tabel Real dan Tabel String pada struktur operand Field nilai

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

4/11/12

Sekolah Tinggi Teknik Surabaya

Contoh Tripletmov t1, 10 = mov temporer 1 integer 10 mov t2, 10.5Tabel Real = mov temporer 2 real 0Indek Nilai s indeks 0 berisi 10.5 0 10.5 Tabel String mov t3, "stts" = mov temporer 3 string 0 Indek Nilai s indeks 0 berisi "stts" 0 "stts"15 15 Sekolah Tinggi Teknik Surabaya

4/11/12

Indeks Variabel Temporer pertama akan Variabel temporer

mendapatkan indeks = 1.

tempIndex diinisialisasi 0.

%% stati Tabel i bol tabel i bol = c S m S m new Tabel i bol S m (); stati i tem pI c nt ndex = 0;.. .4/11/12

16 16 Sekolah Tinggi Teknik Surabaya

Tabel Real dan Tabel String Diimplementasikan dengan hash

table.p ub l c cl i ass Tab el E> { < p ri vate H ashM ap < E, nteger> tab el = I new H ashM ap < E, nteger> (); I .. . }

%% stati Tabel i b ol tab el i b ol = c S m S m new Tab el i b ol S m (); stati Tabel D oubl c < e> tabel eal = R new Tab el D oub l (); < e> stati Tabel S tri Surabaya S tri = c < ng> tab el ng Sekolah Tinggi Teknik

4/11/12

17 17

Tabel Real dan Tabel StringTab el< E> { p ub l c cl i assp ri vate H ashM ap < E, nteger> tab el = I new H ashM ap < E, nteger> (); I p ri vate i count = 0; nt p ub l c i cari ni ai { i nt (E l ) I nteger i ndex = tab el get(ni ai . l ); return(i ndex = = nul ? -1 : i l ndex); } .. .

}4/11/12

18 18 Sekolah Tinggi Teknik Surabaya

Tabel Real dan Tabel StringTab el< E> { p ub l c cl i assp ri vate H ashM ap < E, nteger> tab el = I new H ashM ap < E, nteger> (); I p ri vate i count = 0; nt .. . p ub l c i tam bah(E ni ai { i nt l ) I nteger i ndex = tab el get(ni ai . l ); i (i f ndex = = nul ) { l i ndex = count+ + ; tabel put(ni ai i . l , ndex); } return i ndex; } } Sekolah Tinggi Teknik Surabaya 19 19

4/11/12

Run-Time MemoryStruktur memori yang dialokasi saat sebuah program dijalankan:

Code Segment

berisi kode program

Data Segment

Code (Code Segment) Static Data (Data Segment) Stack (Stack Segment)

berisi variabel global, konstanta real dan string

4/11/12

Stack Segment

stack untuk urusan pemanggilan subrutin dan variabel lokal

20 20

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.

4/11/12

21 21 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.22 22

4/11/12

Sekolah Tinggi Teknik Surabaya

Perubahan Tabel Simbolp ub l c cl i ass S i b ol m i p l ents C om parab l S i b ol { m em e< m > S tri nam a; ng i j s; nt eni i ti eD ata; nt p i dataO ffset; nt .. . } p ub l c cl i ass Tab el m b ol { Si p ri vate A rrayLi st< S i b ol m > tab el i b ol = new A rrayLi S m st< S i b ol (); m > .. . }23 23 Sekolah Tinggi Teknik Surabaya

4/11/12

Inisialisasi dataOffset%% stati Tabel i b ol tab el i b ol = c S m S m new Tab el i b ol S m (); stati Tabel D oubl c < e> tabel eal = R new Tab el D oub l (); < e> stati Tabel S tri c < ng> tab el tri = S ng new Tab el S tri < ng> (); stati i dataO ffset = -1; c nt stati i tem p I c nt ndex = 0; .. .4/11/12

dataOffset diinisialisasi -1 agar variabel global pertama memperoleh dataOffset = 0 Sekolah Tinggi Teknik Surabaya

24 24

Menetapkan dataOffsetdi _stm t : D I i i A S nam a_ti e m M d_l st p i i d_l st : i d_uni que | i i ' 'i d_l st , d_uni que i d_uni que : I D { Si b ol o = new S i b ol m m ( $1, S i bol VA R I B EL, m . A S i bol N O N E); m . i (!tab el i b ol tam b ah(o)) f S m . yyerror("D up l cate I "); i D4/11/12

Asumsi: variabel yang dideklarasikan di sini adalah variabel global Sekolah Tinggi Teknik Surabaya}

o. dataO ffset = + + dataO ffset;

25 25

Struktur Tabel Kode

Tabel Kode merupakan array yang elemennya adalah triplet. Di Java: ArrayList triplet

opcode

operand 1mode nilai

operand 2mode nilai

4/11/12

26 26 Sekolah Tinggi Teknik Surabaya

Inisialisasi Tabel Kode%% stati Tabel i b ol tab el i b ol = c S m S m new Tab el i b ol S m (); stati Tabel D oubl c < e> tabel eal = R new Tab el D oub l (); < e> stati Tabel S tri c < ng> tab el tri = S ng new Tab el S tri < ng> (); stati A rrayLi c st< Tri l p et> tab el ode = K new A rrayLi st< Tri et> (); pl stati i dataO ffset = -1; c nt stati i tem p I c nt ndex = 0;4/11/12

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

4/11/12

28 28 Sekolah Tinggi Teknik Surabaya

Macam Tripletp ub l c cl i ass Tri l { p et p ub l c i op C ode; i nt p ub l c O p erand op erand1, op erand2; i Tri l p et(i op C ode, O p erand op ) { nt thi op C ode = op C ode; s. Triplet thi op erand1 = op ; s. satu thi op erand2 = nul ; s. l opera } Tri l p et(i op C ode, nt O p erand op 1, O perand op 2) { thi op C ode = op C ode; s. Triplet thi op erand1 = op 1; s. dua thi op erand2 = op2; s. opera } } Sekolah Tinggi Teknik Surabaya

nd

4/11/12

nd

29 29

Contoh Triplet pada Ekspresi-c a = b * -c + b *m ov tem porer 1 dataoffset 1 (b) m ov tem porer 2 dataoffset 2 (c) um i nus tem porer 2 m ul tem porer 1 tem porer 2 m ov tem porer 3 dataoffset 1 (b) m ov tem porer 4 dataoffset 2 (c) um i nus tem porer 4 m ul tem porer 3 tem porer 4 add tem porer 1 tem porer 3 m ov dataoffset 0 (a) tem porer 1

4/11/12

Sekolah Tinggi Teknik Surabaya

Asumsi: urutan deklarasi variabel adalah a, b, c

30 30

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 31 31

4/11/12

Sekolah Tinggi Teknik Surabaya

Atribut expr, term, factor, ...ttr { p ub l c cl i ass Exp rAp ub l c i tem p I i nt ndex; p ub l c i ti eD ata; i nt p .. . } % typ e < obj expr term factor >

4/11/12

32 32 Sekolah Tinggi Teknik Surabaya

Konstanta Integerfactor : . . . | I TC O N ST N { tab el ode. K add( new Tri l p et(Tri l M O V, p et. new O p erand( O p erand. TEM P O R ER , + + tem p I ndex), new O p erand( O p erand. N TEG ER , I $1))); $$ = new Exp rA ttr(tem p I ndex, S i b ol I TEG ER ); m .N } |. . .33 33 Sekolah Tinggi Teknik Surabaya

4/11/12

Konstanta Realfactor : . . . | R EA LC O N ST { tab el ode. K add( new Tri l p et(Tri l M O V, p et. new O p erand( O p erand. TEM P O R ER , + + tem p I ndex), new O p erand( O p erand. EA L, R tab el eal tam bah($1)))); R . $$ = new Exp rA ttr(tem p I ndex, S i b ol R EA L); m . } |. . . Perlakuan konstanta

4/11/12

Sekolah Tinggi Teknik Surabaya

string mirip dengan perlakuan konstanta real

34 34

Variabel (Asumsi: Global) factor : . . .

4/11/12

|I D { S i bol o = tab el m b ol cari m Si . ($1); . . / decl . / arati checker on tabel ode. K add( new Tri l p et(Tri l M O V, p et. new O p erand( O p erand. TEM P O R ER , + + tem p I ndex), new O p erand( O p erand. ATA O FFS ET, D o. dataO ffset))); $$ = new Exp rA ttr(tem p I ndex, o. peD ata); ti }

35 35

Sekolah Tinggi Teknik Surabaya

Operasi Perkalianterm : term ' factor *' { . . / typ e checker . / tabel ode. K add( new Tri l p et(Tri et. U L, pl M new O p erand( O perand. TEM P O R ER , ((Tem p A ttr)$1). tem p I ndex), new O p erand( O perand. TEM P O R ER , ((Tem p A ttr)$3). tem p I ndex))); $$ = new ExprA ttr( ((Tem pA ttr)$1). tem p I ndex, .. . ); } |. . . Operasi lainnya

4/11/12

mirip

36 36

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.

4/11/12

37 37

Sekolah Tinggi Teknik Surabaya

Reuse Indeks Var. Temporer a = b * -c + b * -cm ov tem porer 1 dataoffset 1 (b) m ov tem porer 2 dataoffset 2 (c) um i nus tem porer 2 m ul tem porer 1 tem porer 2 m ov tem porer 2 dataoffset 1 (b) m ov tem porer 3 dataoffset 2 (c) um i nus tem porer 3 m ul tem porer 2 tem p orer 3 add tem porer 1 tem porer 2 m ov dataoffset 0 (a) tem porer 1

4/11/12

Sekolah Tinggi Teknik Surabaya

Asumsi: urutan deklarasi variabel adalah a, b, c

38 38

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.

4/11/12

39 39 Sekolah Tinggi Teknik Surabaya

Operasi Perkalian Versi 2 : term '*'factor term{ . . / typ e checker . / tabel ode. K add( new Tri l p et(Tri et. O V, pl M new O p erand( O perand. TEM P O R ER , ((Tem p A ttr)$1). tem p I ndex), new O p erand( O perand. TEM P O R ER , ((Tem p A ttr)$3). tem p I ndex))); tem p I ndex = ((Tem p A ttr)$1). tem pI ndex; $$ = new ExprA ttr( ((Tem pA ttr)$1). tem p I ndex, .. . ); Operasi lainnya } |. . . mirip

4/11/12

40 40

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

4/11/12

Operasi Boolean menggunakan operasi bilangan integer; operand tipe data Boolean menggunakan mode Sekolah Tinggi Teknik Surabaya

41 41

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 operasi42 42

4/11/12

Sekolah Tinggi Teknik Surabaya

Tiga Golongan Operasi

Setiap operand pada intermedate code mempunyai sebuah opcode untuk setiap golongan operasi.

Contoh: mov untuk operasi bilangan integer fmov untuk operasi bilangan real smov untuk operasi string

Juga ada opcode untuk konversi tipe data dari integer ke real, dan sebaliknya.

4/11/12

Contoh: itof untuk konversi dari integer ke real ftoi untuk konversi dari real ke integer

43 43

Sekolah Tinggi Teknik Surabaya

Triplet Stl. Penggolongan a = b * -c + b * -c

fm ov tem porer 1 dataoffset 1 (b) m ov tem porer 2 dataoffset 2 (c) um i nus tem porer 2 i tof tem porer 2 fm ul tem porer 1 tem porer 2 fm ov tem porer 2 dataoffset 1 (b) m ov tem porer 3 dataoffset 2 (c) um i nus tem porer 3 i tof tem porer 3 fm ul tem porer 2 tem porer 3 fadd tem porer 1 tem porer 2 fm ov dataoffset 0 (a) tem porer 1 Sekolah Tinggi Teknik Surabaya

a dan b = real, c = integer

4/11/12

44 44

Menentukan OpCode

Untuk menentukan opcode yang sesuai dengan golongan operasi, diperlukan type checker.

4/11/12

45 45 Sekolah Tinggi Teknik Surabaya

Konstanta Real Versi 2factor : . . . | R EA LC O N ST { tab el ode. K add( new Tri l p et(Tri l FM O V , p et. new O p erand( O p erand. TEM P O R ER , + + tem p I ndex), new O p erand( O p erand. EA L, R tab el eal tam bah($1)))); R . $$ = new Exp rA ttr(tem p I ndex, S i b ol R EA L); m . } |. . . Perlakuan konstanta

4/11/12

Sekolah Tinggi Teknik Surabaya

string mirip dengan perlakuan konstanta real

46 46

Operasi Perkalian Versi 3 : term '*'factor term{ i ti e1 = ((Tem p A ttr)$1). peD ata; nt p ti i ti e2 = ((Tem p A ttr)$3). p eD ata; nt p ti i (ti e1 = = ti e2) { f p p i (ti e1 = = S i b ol I TEG ER | f p m .N | ti e1 = = S i b ol R EA L) { p m . tab el ode. K add( new Tri l p et( (ti e1 = = S i b ol I TEG ER ) ? p m .N Tri l M O V : Tri l FM O V , p et. p et. new O p erand(O p erand. TEM P O R ER , ((Tem p A ttr)$1). tem p I ndex), new O p erand(O p erand. TEM P O R ER , ((Tem p A ttr)$3). tem p I ndex))); $$ = new Exp rA ttr( ((Tem p A ttr)$1). tem p I ndex, Operasi lainnya ti e1); p

4/11/12

47 47

Sekolah Tinggi Teknik Surabaya

mirip

Operasi Perkalian Versi 3 : term '*'factor term{ .. . i (ti e1 != ti e2) { f p p i (ti e1 = = S i b ol R EA L & & f p m . ti e2 = = S i b ol I TEG ER ) { p m .N tab el ode. K add( new Tri l p et(Tri l I F, p et. TO new O p erand(O p erand. TEM P O R ER , ((Tem p A ttr)$1). tem p I ndex) ); tab el ode. K add( new Tri l p et(Tri l FM O V , p et. new O p erand(O p erand. TEM P O R ER , ((Tem p A ttr)$1). tem p I ndex), new O p erand(O p erand. TEM P O R ER , ((Tem p A ttr)$3). tem p I ndex))); $$ = new Exp rA ttr( Operasi lainnya ((Tem p A ttr)$1). tem p I ndex,

4/11/12

48 48

Sekolah Tinggi Teknik Surabaya

mirip