CS 342 Lecture -*- Outline -*- * run-time implementation of Block Structured Languages (Pascal, Algol 60) esp. procedure call/return, variable addressing Reference: Aho, Sethi, Ullman: Compilers ** what happens at run-time *** What kinds of things happen in a Pascal program? *procedure calls (including start of program) *evaluation of expressions (function calls, ops, variable dereference) *assignment (store into variables) -control flow (if, while, etc.) -I/O (read, print) *** smallest actions of implementation (these characterize an imperative language) **** variable access (fetch, store), (already studied array addressing) (access to records, etc. similar) **** procedure call and return (recursion, procedure parameters add new wrinkles) ** General Storage layout ----------------- | (heap) | \\\\\\\\\\\\\\\\| |\\\\\\\\\\\\\\\| | ^ | | | | | | | run-time | | stack of ARs | |---------------| | Static Data | | (constants) | |---------------| | Code | ----------------- Conventions: low address (0) at bottom, high at top of drawing stacks grow up (to top) ** Stacks *** Reasons for using a stack: procedures can be active more than once (with recursion) semidynamic arrays (etc) cannot be allocated statically procedures and blocks are activated and deactived LIFO stack reuses space automatically convenient for evaluation of expressions (nested function calls) *** Operations on stacks How many are familiar with operations on stacks? push, pop, top overflow, usually assume does not happen, but no guarantee (hardware checks help) *show: stack based evaluation of x + y*z as postfix traversal of abstract syntax tree