Activation Tree (cont.) main p S q S CS308 Compiler Theory 6
Activation Tree (cont.) main p s q s CS308 Compiler Theory 6
Control Stack The flow of the control in a program corresponds to a depth-first traversal of the activation tree that: starts at the root. visits a node before its children,and recursively visits children at each node an a left-to-right order A stack (called control stack)can be used to keep track of live procedure activations. An activation record is pushed onto the control stack as the activation starts. That activation record is popped when that activation ends. When node n is at the top of the control stack,the stack contains the nodes along the path from n to the root. CS308 Compiler Theory 7
Control Stack • The flow of the control in a program corresponds to a depth-first traversal fh i i h l of the activation tree that: – starts at the root, – visits a node before its children, and – recursively visits children at each node an a left-to-right order. • A stack (called control stack) can be used to keep track of live procedure activations. – A i i di h d h l k h An activation record is pushed onto the control stack as the activation starts. – Th t ti ti d i d h th t ti ti d That activation record is popped when that activation ends. • When node n is at the top of the control stack, the stack contains the nodes along the path from n to the root CS308 Compiler Theory 7 nodes along the path from n to the root
Variable scopes The same variable name can be used in the different parts of the program. The scope rules of the language determine which declaration of a name applies when the name appears in the program. An occurrence of a variable (a name)is: local:If that occurrence is in the same procedure in which that name is declared. non-local:Otherwise (ie.it is declared outside of that procedure) procedure p; var b:real; procedure p; var a:integer; a is local begin a :1;b 2;end; b is non-local begin ..end; CS308 Compiler Theory 8
Variable Scopes • The same variable name can be used in the different parts of the program. • The scope rules of the language determine which declaration of a name applies when the name appears in the program. applies when the name appears in the program. • An occurrence of a variable (a name) is: – local: If that occurrence is in the same procedure in which that name is declared. – non-local: Otherwise (ie. it is declared outside of that procedure) procedure p; procedure p; var b:real; procedure p; var a: integer; var a: integer; a is local a is local begin a := 1; b := 2; end; b is non-local begin ... end; CS308 Compiler Theory 8
Run-Time Storage Organization Code Memory locations for code are determined at compile time. Static Data Locations of static data can also be determined at compile time. Stack Data objects allocated at run-time. (Activation Records) Other dynamically allocated data Heap objects at run-time.(For example, malloc area in C). CS308 Compiler Theory 9
Run-Time Storage Organization Code Memory locations for code are determined at compile time. Static Data determined at compile time. Locations of static data can also be Stack determined at compile time. Data objects allocated at r n Data objects allocated at ru n -time. (Activation Records) Other dynamically allocated data Heap objects at run-time. (For example, malloc area in C). CS308 Compiler Theory 9