Type Cheking Rodmp (Where re we?) Lst leture Contet-sensitie nlysis Motition Attriute grmmrs Ad ho Synt-direted trnsltion This leture Type heking Type systems Using synt direted trnsltion Symol tles Leil soping Implementtion Stk Threded stk 2 1
Types Type: A set of lues nd meningful opertions on them Types proide semnti snity heks (onsisteny heks) nd determine effiient implementtions for dt ojets Types help identify errors, if n opertor is pplied to n inomptile opernd dereferening of non-pointer dding funtion to something inorret numer of prmeters to proedure whih opertion to use for oerloded nmes nd opertors, or wht type oerion to use (e.g.: 3.0 + 1) identifition of polymorphi funtions 3 Type Systems Type system: Eh lnguge onstrut (opertor, epression, sttement, ) is ssoited with type epression. The type system is olletion of rules for ssigning type epressions to these onstruts. Type epressions for si types integer, hr, rel, oolen, typeerror onstruted types // T is type epression rry(lu, T) // rry of T pointer(t) // pointer to T T1 X T2 // tuple of T1, T2 T1 T2 // funtion w/ rg T1 returning T2 4 2
Type System Properties Progress Gien epression e, either e is lue with some type T e -> e (e n e eluted to produe epression e ) I.e., preent epressions suh s (2 3) tht nnot e eluted Presertion Gien epression e with type T If e -> e then e must lso he type T I.e., preents type of epression e from hnging t run time Soundness If type system supports progress & presertion, then Gien epression e, e ->* (for some lue ) or e dierges I.e., well-typed progrms don t go wrong 5 Type Cheker A type heker implements type system. It omputes or onstruts type epressions for eh lnguge onstrut Stti type heking Detets type errors t ompile time No run time oerhed Not lwys possile (e.g., A[i]) Dynmi type heking Performed t run time More fleile, llows prototyping Run-time oerhed to mintin & hek tgs 6 3
Type Inferene (for Epressions) Speifies the type of n epression Emple If opernds of ddition re of type integer, result is of type integer Result of unry & opertor is pointer to type of opernd Denottionl semntis of type inferene rule E e 1 : integer E e 2 : integer E (e 1 + e 2 ): integer where E is type enironment tht mps onstnts nd riles to their type epressions. Question How to speify rules tht llow type oerion (type widening) from integers to rels in rithmeti epressions? 3.0 + 1 or 1 + 3.0 7 Type Inferene (in Generl) Gol Gien epression with no type nnottions, reonstrut lid type for the epression, or determine there is no lid typing Approh Use type riles for unknown types Generte equlity onstrints mong types nd type riles Sole onstrints to determine lid typing (unifition) My require generl Constrint Logi Progrmming (CLP) 8 4
Type Equilene Struturl - type equilene: type nmes re epnded Nme - type equilene: type nmes re not epnded Emple: type A is rry(1..10) of integer; type B is rry(1..10) of integer; : A; : B;, d: rry(1..10) of integer; e: rry(1..10) of integer; Answer: struturl equilene: nme equilene: (,,, d, e) (), (), (, d, e) 9 Synt Direted Trnsltion Sheme (in CUP) Reisit our type inferene rule for +. ep ::= ep:e1 PLUS ep:e2 {: if (e1 == sym.int && e2 == sym.int ) RESULT = sym.int ; else { RESULT = typeerror; System.out.println( Error: illegl opernd types ); : The definition of type epression s J types (stti finl int fields in lss sym) should e done in my.up. The ssignment of type epression J types to terminls nd nonterminls of the grmmr is done in my.up. 10 5
Synt Direted Trnsltion Sheme (in Y) Reisit our type inferene rule for +. ep : ep + ep { if ($1 == integer && $3 == integer) $$ = integer; else { $$ = typeerror; printf( Error: illegl opernd types\n ); The definition of type epression s C types (struts) should e done in ttr.h. ttr. my ontin helper funtions. The ssignment of type epression C types to terminls nd nonterminls of the grmmr is done in prse.y. 11 Type Cheker Emple 12 6
Type Cheker Emple (ont.) Hndling delrtions 13 Type Cheker Emple (ont.) Hndling epressions 14 7
Type Cheker Emple (ont.) Hndling sttements 15 Type Cheker Emple (ont.) Hndling funtions 16 8
Symol Tles Symol tle Compile-time strutures for resoling referenes to nmes Will look t run-time strutures lter Cn lso ssoite ttriutes with nme Attriutes possily ssoited with nme Type Delring proedure Leil leel If rry, numer nd size of dimensions If funtion, numer nd type of prmeters 17 Leilly-soped Symol Tles 5.7 in EC The prolem The ompiler needs distint reord for eh delrtion Nested leil sopes dmit duplite delrtions The interfe insert(nme, leel ) retes reord for nme t leel lookup(nme, leel ) returns pointer or inde delete(leel ) remoes ll nmes delred t leel Mny implementtion shemes he een proposed We ll sty t the oneptul leel Hsh tle implementtion is triky, detiled, & fun (see B.4) 18 9
Emple proedure p { int,, proedure q { int,,, w proedure r { int, y, z. proedure s { int,, r s q B0: { int,, B1: { int,,, w B2: { int, y, z. B3: { int,, 19 Emple proedure p { int,, proedure q { int,,, w proedure r { int, y, z., w,, q proedure s { int,, r s B0: { int 0, 1, 2 B1: { int 3, 4, 5, w 6 B2: { int 7, y 8, z 9. B3: { int 10, 11, 12 11, 4, 2, 12, w 6,, 10, no y or z Pituring it s series of Algol-like proedures 20 10
Leilly-soped Symol Tles High-leel ide Crete new tle for eh sope Chin them together for lookup s q w... p... Chin of tles implementtion insert() my need to rete tle it lwys inserts t urrent leel lookup() wlks hin of tles & returns first ourrene of nme delete() throws wy tle for leel p, if it is top tle in the hin Indiidul tles n e hsh tles. 21 Leilly-soped Symol Tles High-leel ide Crete new tle for eh sope Chin them together for lookup s q w... p... Rememer 11, 4, 2, 12, w 6,, 10, no y or z the nmes isile in s If we dd the susripts, the reltionship etween the ode nd the tle eomes ler 22 11
Implementing Leilly Soped Symol Tles Stk orgniztion netfree s (leel 2) q (leel 1) p (leel 0) w growth Implementtion insert () retes new leel pointer if needed nd inserts t netfree lookup () serhes linerly from netfree 1 forwrd delete () sets netfree to the equl the strt lotion of the leel deleted. Adntge Uses muh less spe Disdntge Lookups n e epensie 23 Implementing Leilly Soped Symol Tles Stk orgniztion 11, 4, 2, 12, w 6,, 10, no y or z netfree s (leel 2) q (leel 1) p (leel 0) w growth 12 11 10 6 5 4 3 2 1 0 Implementtion insert () retes new leel pointer if needed nd inserts t netfree lookup () serhes linerly from netfree 1 forwrd delete () sets netfree to the equl the strt lotion of the leel deleted. Adntge Uses muh less spe Disdntge Lookups n e epensie 24 12
Implementing Leilly Soped Symol Tles Threded stk orgniztion h() w growth s q p Implementtion insert () puts new entry t the hed of the list for the nme lookup () goes diret to lotion delete () proesses eh element in leel eing deleted to remoe from hed of list Adntge lookup is fst Disdntge delete tkes time proportionl to numer of delred riles in leel 25 Implementing Leilly Soped Symol Tles Threded stk orgniztion 11, 4, 2, 12, w 6,, 10, no y or z h() w growth 12 11 10 6 5 4 3 2 1 0 s q p Implementtion insert () puts new entry t the hed of the list for the nme lookup () goes diret to lotion delete () proesses eh element in leel eing deleted to remoe from hed of list Adntge lookup is fst Disdntge delete tkes time proportionl to numer of delred riles in leel 26 13