Compilers ECS 142
Popular in Course
Popular in Engineering Computer Science
This 40 page Class Notes was uploaded by Ashleigh Dare on Tuesday September 8, 2015. The Class Notes belongs to ECS 142 at University of California - Davis taught by Sean Peisert in Fall. Since its upload, it has received 52 views. For similar materials see /class/187736/ecs-142-university-of-california-davis in Engineering Computer Science at University of California - Davis.
Reviews for Compilers
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 09/08/15
Language Features and Runtime Environments Lecture I9 Dr Sean Peisert ECS I42 Spring 2009 Status 0 lt 2 weeks to go on project 3 0 Read Sec 7 through 74 by next Monday May I8 How would we extend Cool to implement 0 Multiple inheritance 0 Keep a reference to the base class and a list of pointers to base class When accessing elds in the class rst look in own eldsthen look through the list 0 With SELFTYPE and static dispatch this could be a nightmare Overloading In Cool we don t allow function overloading C amp Java do How would we allow the equivalent of O myfunctionint c and myfunctionchar p int d 0 Keep a parameter list and check for duplicate parameter lists 0 myFunca int Bool false 0 myFuncb bool c string Bool false OK I myFuncd int Error Constants 0 We have 0 Red Int 0 0 What would we have to do for 0 Red Const Int 0 Enumerated Types 0 Color Enum Red Green Blue 0 is the same as saying 0 Red Const Int 0 0 Green Const Int I 0 Blue Const Int 2 0 to use 0 c Color i int i Red OK c Green OK c 52 Not OK Status 0 We have covered the frontend phases 0 Lexical Analysis 0 SyntaXAnalysis Parsing 0 Semantic Analysis 0 Now are the back end phases 0 Code Generation we ll do this first 0 Optimization Runtime Environments 0 Before discussing code generationwe need to understand what we are trying to generate 0 There are a number of standard techniques for structuring executable code that are widely used Outline 0 Management of runtime resources 0 Correspondence between static compile time and dynamic runtime structures 0 Storage organization RunTime Resources Execution of a program is initially under the control of an operating system When a program is invoked O The OS allocates space for the program 0 The code is loaded into part of the space 0 The OS jumps to the entry point ie main Memory Memory Layout Code Other Space Low Address High Address Notes 0 Our pictures of machine organization have 0 low address at the top 0 high address at the bottom 0 lines delimiting areas for different kinds of data 0 These pictures are simpli cations O Eg not all memory need be contiguous 0 In some textbooks lower addresses are at the bottom What is other space 0 Holds all data for the program 0 other space data space 0 Compiler is responsible for 0 Generating code 0 Orchestrating use of the data area Code Generation Goals 0 Two goals 0 Correctness 0 Speed 0 Most complications in code generation come from trying to be fast as well as correct Assumptions about Execution 0 Execution is sequential control moves from one point in a program to another in a wellde ned order 0 When a procedure is called control eventually returns to the point immediately after the call 0 Do these assumptions always hold Activations 0 An invocation of procedure P is an activation of P 0 The lifetime of an activation of P is 0 All the steps to execute P 0 Including all the steps in procedures that P cas Lifetimes ofVariabIes 0 The lifetime of a variable x is the portion of execution in which x is de ned 0 Note that 0 Lifetime is a dynamic runtime concept 0 Scope is a static concept Activation Trees 0 Assumption 2 execution returns to the point immediately after the call when a procedure returns requires that when P calls Q then Q returns before P does 0 Lifetimes of procedure activations are properly nested 0 Activation lifetimes can be depicted as a tree Example Class Main g0 Int I f0 i39nt g0 mainO i39nt g0 f0 main Example 2 Class Main g0 Int I f0 i39nt g0 mainO i39nt g0 f0 What is the activation tree for this example Example Class Main g0 Int I f0 i39nt g0 mainO i39nt g0 f0 Main Stack Example Class Main g Int f Int g mainO i39nt 0 f0 Main Stack g f Main f Example Class Main g0 Int I f0 i39nt g0 mainO i39nt g0 f0 Main Stack gf Main gf Notes 0 The activation tree depends on runtime behavior 0 The activation tree may be different for every program input 0 Since activations are properly nesteda stack can track currently active procedures Memory Memory Layout Code Low Address High Address Activation Records 0 On many machinesthe stack starts at high addresses and grows toward lower addresses 0 The information needed to manage one procedure activation is called an activation record AR or frame 0 If procedure F cas Gthen G s activation record contains a mix of info about F and G What is in G s AR when F calls G 0 F is suspended until G completes at which point F resumes G s AR contains information needed to resume execution of F 0 GS AR may also contain 0 Actual parameters to G supplied by F 0 GS return value needed by F 0 Space for G s local variables 0 Note globals are handled separately Contents of aTypicaI AR for G 0 Space for G s return value 0 Actual parameters 0 Pointer to the previous activation record 0 The control link points to AR of caller of G 0 Or frame pointer 0 Machine status prior to calling G 0 Contents of registers amp program counter 0 Local variables 0 Other temporary values Example 2 Revisited Class Main g Int l fxnt Int if x0 then g else fx l main Int f3 return address AR for f3 control link argument result 29 I l Stack After Two Calls to f ret 2 result ret Stack control link I control link 3 result Notes 0 In our case main has no argument or local variables and its result is never used its AR is uninteresting O and are return addresses of the invocations of f O The return address is where the execution resumes after a procedure call nishes O This is only one of many possible AR designs 0 Would also work for C Pascal FORTRAN etc The Main Point 0 The compiler must determineat compile timethe layout of activation records and generate code that correctly assesses locations in the activation record 0 Thusthe AR and code generator must be designed together Discussion 0 The advantage of placing the return value first in a frame is that the caller can find it at a fixed offset from its own frame 0 There is nothing magic about this organization 0 Can rearrange order of frame elements 0 Can divide callercallee responsibilities differently 0 An organization is better if it improves execution speed or simplifies code generation Memory Layout with Static Data Low Address Code Memory Static Data High Address Heap Storage 0 A value that outlives the procedure that creates it cannot be kept in the AR memory x new y 0 TheY value must survive deallocation of x s AR 0 Languages with dynamically allocated data use a heap to store dynamic data Notes The code area contains object code 0 For most languages xed size and read only The static area contains data not code with xed addresses egglobal data 0 Fixed size may be readable or writable The stack contains an AR for each currently active procedure 0 Each AR usually xed size contains locals Heap contains all other data 0 In C heap is managed by malloc and free 0 What aboutJava and Cool Notes 0 Both the heap and the stack grow 0 Must take care that they don t grow into each other 0 Solution start heap and stack at opposite ends of memory and let them grow toward each other Memory Layout with Static Data Low Address Code Memory Static Data High Address Data Layout 0 Lowlevel details of machine architecture are important in laying out data for correct code and maximum performance 0 Chief among these concerns is alignment Alignment 0 Many machines in use are still 32 bit this is changing quickly 0 8 bits in a byte 0 4 bytes in a word dword actually 64 bits or 8 bytes per word qword possible but often not used on 64 bit chips eg 64bit X86 0 Machines are either byte or wordaddressable 0 Data is word aligned if it begins at a word boundary 0 Or performance penalties for poor alignment
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'