ACTIVATION RECORDS What are ARs used for on the runtime stack? for procedure calls, holds storage for local constants and variables for that AR's scope Why not have all calls of a procedure share the same storage? this would always replace storage for each call so recursion would not work right In C, how many levels of static scopes can there be in a program? can be an arbitrary number In C, what causes that nesting of scopes? there's a global scope (for a file), each function is a scope (formal parameters), curly braces { and } PROCEDURE CALLS What does the CAL instruction do? calls a procedure by: create a new AR by: pushes stack[BP] on stack (static link) pushes BP on stack (dynamic link) pushes PC on the stack (return address) sets the BP to the start of the AR (SP - 3) sets the PC at the procedure's start address Does the code for a procedure body need to reserve space for the links? No, CAL does that already In C, when the function main returns, what is it returning to? the OS (process running your program) PROCEDURE CALLS AND RETURNS How is the starting address of a procedure obtained (for use in a CAL instruction)? from the label, which is set by generation of the code for the procedure being called, when it is "registered" (laid out in memory) then know the starting address make a second pass over the code sequence to put the addresses of procedures in the CAL instructions, when know they are set Why does the stack need to be trimmed before a procedure returns? because memory was allocated in the AR for constants and variables need to restore the runtime stack to the state after the call (so that the top of stack to contain the old PC, and 2nd from top to contain the old BP) so that the RTN instruction finds the stack in the configuration it expects With static scoping, what does the BP register point to on the stack? its address is the beginning of the AR, and in that location is the static link (the address of the AR for the surrounding scope) SCOPING AND ADDRESSING If some variables were dynamically scoped, how would they be addressed? follow the dynamic links but need to be search for variable names GENERATING CODE FOR DECLARATIONS How would the C declaration const double e = 2.718281828459; be compiled for a stack machine? Use LIT instruction (equivalent) push the right value on the stack How would the C declaration int i; be compiled for a stack machine? Use equivalent of INC 1 (to allocated space for it) How would the C declaration void incI() { i += 1; } be compiled? [code for i += 1, PBP, PSI..., LIT 1, ADD, STO, RTN] CODE GENERATION FOR EXPRESSIONS Where does the compiled code put the result of an expression (in a stack machine)? On top of the stack Why are identifier uses important? To get the lexical address of them to load and store them CODE GENERATION FOR EXPRESSIONS 2 How is a constant's lexical address used to load the constant's value? Use LIT for the constant's value (obtained from the attributes) How is a variable's lexical address used in an assignment statement? to get the value of the variable: follow the specified number of static links (PBP followed by that number of PSIs) then LOD instruction to load the value of that address + the offset to assign to the variable: follow the specified number of static links (PBP followed by that number of PSIs) then STO instruction to stoe the value (on top of the stack) at the AR's base address + the offset ASSEMBLY LANGUAGE What is an assembly language? A programming language which gives mnemonics for opcodes of instructions: statements are instructions provides label What features does assembly language provide to help programmers? mnemonics for opcodes of instructions: symbolic labels for addresses and variables translates numeric literals and others into binary How does assembly language differ from C? each statement in C generates several instructions (instead of one) C implicitly manages temporary storage (and registers and/or the stack) C manages storage layout C has function abstractions ASSEMBLERS How does an assembler translate forward jumps to a label? uses multiple passes 1. determine addresses of all labels put them in a symbol table 2. translate all instructions into binary (look up labels to find their addresses) How would you organize an assembler as a program? ASSEMBLER OUTPUT FORMAT What would be the output format for an assembler on Unix? What are the main sections of an object file? What addresses would need to be relocated and would need to be marked as such? LINKERS What does a linker do? How many passes would a linker need? Does a linker treat user program code differently than library code? LOADERS What does a loader do? What is a boot loader? How is the boot loader loaded? Does a boot loader do relocation? OS What are the goals of an OS? What is the basic technique for running programs efficiently used by an OS? Would it be better if an OS were just a collection of libraries that worked with the hardware? PROCESSES What is a process? How is a process different from a program? How does a program become a process? Why is the process concept useful for programmers? PROCESS TRANSITIONS What states can a process be in? What causes transitions between states? RESOURCE SHARING How does an OS facilitate sharing of resources? Why are interrupts needed? What are the different kinds of interrupts? INTERRUPT PROCESSING What is an interrupt handler? What mode does an interrupt execute in? What does an interrupt handler do to save the state of the running process? TRAP TABLES (INTERRUPT VECTORS) What is a trap table? What software controls the trap table? LIMITED DIRECT EXECUTION What is limited direct execution? How does it work? What mode does your program run in? What kinds of instructions are privileged? SYSTEM CALLS What is a system call? Why are system calls needed? THREADS What is a thread? Does each thread have its own stack? Why are threads useful? SYNCHRONIZATION What is a race condition? Why are race conditions a problem? What is a critical section? CORRECTNESS FOR THREADS When is an execution serializable? When is execution atomic?