Docsity

Assignment 5 - Variables, Control Flow and Iterative Type Analysis | ECEN 4553, Assignments of Electrical and Electronics Engineering

Toggle sidebar
bg1
Assignment 5: variables, control flow, and
iterative type analysis
ECEN 4553 & 5013, CSCI 4555 & 5525
Prof. Jeremy G. Siek
October 1, 2008
The main concepts of this assignment are
•Variable binding.
•Extending type analysis to deal with variables and control flow using
iterative type analysis.
1 The P2subset of Python
In this assignment we grow the language of our compiler to P2, which in-
cludes variables, assignment, and structured control flow (if and while
statements).
1.1 Syntax of P2
Figure 1 shows the concrete syntax for P2. Note that in Python, assignment
is a statement and not an expression as it is in C.
1.2 Semantics of P2and compiling to C
Compiling the variables and assignment is the more challenging aspect be-
cause in Python, there are no variable declarations, as there are in C. For
example, the following program assigns the value 3to the variable xand
then prints out its contents.
>>> x = 3
>>> print x
3
1
pf3
pf4
pf5
pf8

Partial preview of the text

Download Assignment 5 - Variables, Control Flow and Iterative Type Analysis | ECEN 4553 and more Assignments Electrical and Electronics Engineering in PDF only on Docsity!

Assignment 5: variables, control flow, and

iterative type analysis

ECEN 4553 & 5013, CSCI 4555 & 5525

Prof. Jeremy G. Siek

October 1, 2008

The main concepts of this assignment are

  • Variable binding.
  • Extending type analysis to deal with variables and control flow using iterative type analysis.

1 The P 2 subset of Python

In this assignment we grow the language of our compiler to P 2 , which in- cludes variables, assignment, and structured control flow (if and while statements).

1.1 Syntax of P 2

Figure 1 shows the concrete syntax for P 2. Note that in Python, assignment is a statement and not an expression as it is in C.

1.2 Semantics of P 2 and compiling to C

Compiling the variables and assignment is the more challenging aspect be- cause in Python, there are no variable declarations, as there are in C. For example, the following program assigns the value 3 to the variable x and then prints out its contents.

x = 3 print x 3

identifier ::= # see the Python Reference Manual expression ::= identifier simple_statement ::= "pass" | identifier "=" expression | expression compound_statement ::= "if" expression ":" suite "else" ":" suite | "while" expression ":" suite stmt_list ::= simple_statement (";" simple_statement)* statement ::= stmt_list NEWLINE | compound_stmt suite ::= stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT module ::= (NEWLINE | statement)*

Figure 1: Concrete syntax for the P 2 subset of Python. (In addition to the syntax of P 1 .)

The analogous program in C would need to first declare the variable x and then assign to it as follows.

int x; x = 3; printf("%d\n", x); The compiler needs to analyze the Python program and figure out where to insert the variable declarations in the output C program.

Exercise 1.1. Read the sections of the Python Reference Manual that apply to P 2 : 4.1 Naming and binding 6.1 expression statements, 6.3 assignment, 6.4 pass, 6.10 break, 6.11 continue, 7.1 if, and 7.2 while.

Exercise 1.2. Write five programs in the P 2 subset of Python that help you understand the language. Look for corner cases or unusual aspects of the language to test in your programs. Run the programs using the standard python interpreter. Add your test programs to the test directory.

1.3 New Python classes for P 2

Figure 2 shows the Python classes for the P 2 AST nodes.

Discard This statement contains an expression. The semantics of this state- ment it to evaluate the expression and discard the result.

If The If class has two attributes, tests and else. The tests attribute that is a sequence of pairs where the first item in each pair is an ex- pression (the condition) and the second item is a statement. The else attribute is a statement.

variable declarations. The variable declarations should appear at the be- ginning of the program, and once we add functions, at the beginning of functions.

class VarDecl(Node): def init(self, name, type): self.name = name self.type = type

Figure 3 shows the architecture of the compiler for this assignment.

Generate C

Lex & Parse Python AST

Python text

C text

Simplify Comparisons IR 0 and Logic Operations

IR (^0) Specialization^ Type IR 0 Type Analysis

IR 0 with VarDecl

Insert variable declarations

Figure 3: Diagram of passes in the compiler for P 2.

Exercise 1.4. Extend your Python to C compiler to handle P 2. Test your compiler against the new test programs for P 2 in addition to the tests for P 0 and P 1. To keep things simple with respect to type analysis, just use the pyobj union type for all variables. In Section 2 of this assignment you implement a more sophisticated type analysis to improve on this.

Exercise 1.5. (Extra credit) Extend your compiler to handle while loops with else clauses and to handle if statements without else clauses.

2 Iterative type analysis for P 2

In Exercise 1.4 we simply used pyobj as the type for every variable. How- ever, this approach produces poor code. For example, the variable x in the

following code is assigned an integer, so we should generate C code with x declared as an integer.

x = 4 print x + 5

The following is the C code generated from Exercise 1.4 compared to what we would like to produce.

// Generated from the compiler for Section 1 pyobj x; x = make_int(4); print_any(add(x, make_int(5)));

// Goal for Section 2 int x; x = 4; printf("%d\n", x + 3); As a first try, we could give each variable the same type as the type of the expression on the right-hand side of the assignment. However, in Python a variable may be assigned values of different types. For exam- ple, in the following code, variable x is first assigned an integer and later assigned a floating point number.

x = 3 y = x + 1 x = x + 0. z = x / 2

To handle the above code, we would need look at all of the assignments to the same variable and choose a type that is legal for all of the assignments. In the above example we would choose the type pyobj for x. However, even this approach is too simplistic when we consider loops. In the follow- ing program x is assigned an integer, and then inside the loop x is assigned to y. At this point you might think that we should choose integer as the type for y, but that would be incorrect.

x = 0 while i < n: y = x x = x * 0.

The later assignment of x * 0.5 to x turns x into a floating point number. In the next iteration of the loop, y will also turn into a floating point number. The basic problem here is that there is a mutual recursion between the type of the variables and the types of the expressions on the right-hand sides of the assignments.

than τ 2 , or if τ 1 and τ 2 are the same type. So, for example, undefined v int, int v pyobj, and int v int. We can extend the informative relation to dictionaries by saying that some dictionary d 1 is less or equally informative than dictionary d 2 , written d 1 v d 2 , if for every variable x in d 1 , d 1 [x] v d 2 [x]. Now, to ensure that the approximation improves with every iteration, we will require that whenever the input to F increases (or stays the same), then the output also increases (or stays the same). That is, if for some d and d′, d v d′, then F (d) v F (d′). This property is called monotonicity. Now, in the initial dictionary d 0 , every variable is assigned the undefined type, which is the bottom of the type lattice. Thus, no matter what, the first iteration will result in a dictionary d 1 that is at least as informative as d 0 , i.e. d 0 v d 1. If d 0 = d 1 then we have already found the fixed point of F and we are done. Otherwise, we apply F again. Because d 0 v d 1 then F (d 0 ) v F (d 1 ) by the monotonicity of F , so we have d 1 v d 2. We can continue this process until we cannot go higher in the lattice (the lattice does not have infinitely ascending chains) and have reached the fixed point.

d 0

d 1 =F(d 0 )

d 2 =F(d 1 )

d 0 ⊑ d 1 because anything is as good or better than undefined

Because d 0 ⊑ d 1 , we have F(d 0 ) ⊑ F(d 1 ) by the monotonicity of F

d 3 =F(d 2 ) Because d 1 ⊑ d 2 , we have F(d 1 ) ⊑ F(d 2 ) by the monotonicity of F

di=F(di) (^) Fixed point

Figure 5: The monotonicity of F ensures progress towards the fixed point.

The function F is determined by the program and how we propagate types through the various operations. For each operation, such as addition, we have corresponding rules for what the return type should be given the

types of the arguments. Figure 6 shows the rules for addition.

  • undefined int bool float pyobj undefined undefined undefined undefined undefined undefined int undefined int int float pyobj bool undefined int int float pyobj float undefined float float float pyobj pyobj undefined pyobj pyobj pyobj pyobj

Figure 6: Type rule for addition.

To ensure that F is monotone, we need each of the typing rules to also be monotone. Let us check that the typing rule for addition is monotonic on one example. Suppose that the inputs to an addition go from being (int,int) to (int,pyobj). Then the output goes from int to pyobj.

Exercise 2.1. Check two more combinations of inputs to addition to make sure the output does not decrease. Look at two more operations, such as less than comparison and if expressions.

Exercise 2.2. Change the type analysis pass of your compiler to perform it- erative type analysis. The type analysis should update the .type attributes of the variable declarations.

References

[1] C. Chambers and D. Ungar. Iterative type analysis and extended message splitting; optimizing dynamically-typed object-oriented pro- grams. In PLDI ’90: Proceedings of the ACM SIGPLAN 1990 conference on Programming language design and implementation, pages 150–164. ACM Press, 1990.

[2] B. Davey and H. Priestley. Introduction to Lattices and Order. Cambridge University Press, 2nd edition, 2002.

[3] G. A. Kildall. A unified approach to global program optimization. In POPL ’73: Proceedings of the 1st annual ACM SIGACT-SIGPLAN sympo- sium on Principles of programming languages, pages 194–206. ACM Press,