Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lesson 12 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.

Similar presentations


Presentation on theme: "Lesson 12 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg."— Presentation transcript:

1 Lesson 12 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

2 Outline Type systems –Checking for type errors –Type equivalence 2

3 TYPE SYSTEMS 3

4 Type systems Specifies how to assign types to computed values Can be expressed with an SDD Example based on this grammar on the next slides → P→ D ; E D→ D ; D | id : T T→ char | integer | array [ num ] of T | ^T | T -> T E→ literal | num | id | E mod E | E [ E ] | E^ | E(E) P→ D ; E D→ D ; D | id : T T→ char | integer | array [ num ] of T | ^T | T -> T E→ literal | num | id | E mod E | E [ E ] | E^ | E(E) 4

5 SDT for collecting type information from declarations ProductionSemantic action P→ D ; E D→ D ; D | id : T{ addtype(id.name, T.type) } T→ char{ T.type = char } | integer { T.type = integer } | array [ num ] of T 1 { T.type = array(num.val, T 1.type) } | ^T 1 { T.type = pointer(T 1.type) } | T 1 -> T 2 { T.type = T 1.type → T 2.type } 5

6 SDT for type checking of expressions ProductionSemantic action E→ literal{ E.type = char } | num{ E.type = integer } | id{ E.type = lookup(id.name) } | E 1 mod E 2 { E.type = if E 1.type == E 2.type == integer then integer else type_error } | E 1 [ E 2 ]{ E.type = if E 1.type == array(n, t) and E 2.type == integer then t else type_error } | E 1 ^{ E.type = if E 1.type == pointer(t) then t else type_error } | E 1 (E 2 ){ E.type = if E 1.type == s → t and E 2.type == s then t else type_error } 6

7 Exercise (1) Assume that lookup(”a”) = array(78, pointer(int)) and lookup(”i”) = integer. Draw the parse tree of the following expression and assign types to all nodes: a[i]^ mod 38 Hint 1: The same expression would look like this in C: *a[i] % 38 Hint 2: Work bottom-up! 7

8 Type checking of statements Types on statements: –void: No type errors in embedded expressions –type_error: Type errors in expressions We add statements to the grammar: S→ id := E | if E then S | while E do S | S ; S 8

9 SDT for type checking of statements ProductionSemantic action S→ id := E{ S.type = if lookup(id.name) == E.type then void else type_error } | if E then S 1 { S.type = if E.type == integer then S 1.type else type_error } | while E do S 1 { S.type = if E.type == integer then S 1.type else type_error } | S 1 ; S 2 { S.type = if S 1.type == S 2.type == void then void else type_error } 9

10 Equivalence of type expressions When are two types equal? Example: Is the following legal? int* arr[72]; … int x = *arr[19]; Two views: Structural equivalence – compare the type expressions recursively Name equivalence – types with different names are always considered non-equivalent 10

11 Structural type equivalence bool equiv(s, t) if s and t are the same basic type then return true else if s == array(n 1, s') and t == array(n 2, t') then return n 1 == n 2 and equiv(s', t’) else if s == s' × s'' and t == t' × t'' then return equiv(s', t') and equiv(s'', t'') else if s == pointer(s') and t == pointer(t') then return equiv(s', t’) else if s == s' → s'' and t == t' → t'' then return equiv(s', t') and equiv(s'', t'') else return false 11

12 Name equivalence More strict than structural equivalence User-defined type names are viewed the same way as basic types Example: Here the types of A and B are structurally equivalent, but not name- equivalent: typedef int IntAlias; typedef struct AStruct { IntAlias x; } A; typedef struct BStruct { int y; } B; 12

13 Type equivalence example: the language C User-defined type names in C: Structs and unions: name equivalence is used Typedefs and enums: structural equivalence is used struct A { int x; }; struct B { int x; }; typedef struct A C; typedef struct A D; typedef D E; void some_func(void) { struct A a; struct B b; C c; D d; E e; a = b;// Error c = d;// OK c = e;// OK } 13

14 Recursively defined types What is the type of Link in the following C code? struct Link { void* data; struct Link* next; }; 14

15 Recursively defined types With name equivalenceWith structural equivalence 15 void pointer nextdata × ×× void pointer nextdata × ×× Link Link = record

16 Implicit type conversions ProductionSemantic action E → E 1 + E 2 { E.type = if E 1.type == E 2.type == int then int else if E 1.type == E 2.type = real then real else if E 1.type == int and E 2.type = real then real else if E 1.type == real and E 2.type == int then real else type_error } 16

17 Explicit type conversions Appear as operators to the type checker Syntax tree for x + (int) y: 17 + id toint id

18 Conclusion A type system is a set of rules for assigning types to parts of a program –Can be specified as an SDD A type checker checks that these rules are adhered to –Can be specified as an SDT With name equivalence, named types are considered equivalent only if they have the same name With structural type equivalence, types with different names but equivalent structures are considered equivalent as well 18

19 Next time More stack machine code –Functions and activation records 19


Download ppt "Lesson 12 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg."

Similar presentations


Ads by Google