Languages and Compilers (SProg og Oversættere)

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms
Advertisements

CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
Chapter 9 Subprograms Specification: name, signature, actions Signature: number and types of input arguments, number and types of output results –Book.
Subprogram Control - Data sharing Mechanisms to exchange data Arguments - data objects sent to a subprogram to be processed. Obtained through  parameters.
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison- Wesley. All rights reserved. 1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
1 Languages and Compilers (SProg og Oversættere) Sequence control and Subprogram Control.
ISBN Chapter 10 Implementing Subprograms.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 10 Implementing Subprograms.
Run time vs. Compile time
Chapter 8 . Sequence Control
Semantics of Calls and Returns
Chapter 9: Subprogram Control
ISBN Chapter 10 Implementing Subprograms –Semantics of Calls and Returns –Implementing “Simple” Subprograms –Implementing Subprograms with.
1 CSCI 360 Survey Of Programming Languages 9 – Implementing Subprograms Spring, 2008 Doug L Hoffman, PhD.
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
Chapter 10 Implementing Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Semantics of Call and Return The subprogram call and return.
ISBN Chapter 10 Implementing Subprograms.
Compiler Construction
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 9 Def: The subprogram call and return operations of a language are together called its subprogram.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
Programming Languages and Paradigms Imperative Programming.
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Angela Guercio.
PZ07A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ07A - Expressions Programming Language Design and Implementation.
ISBN Chapter 10 Implementing Subprograms.
CSC 8505 Compiler Construction Runtime Environments.
1 Chapter 10 © 2002 by Addison Wesley Longman, Inc The General Semantics of Calls and Returns - Def: The subprogram call and return operations of.
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
10-1 Chapter 10: Implementing Subprograms The General Semantics of Calls and Returns Implementing “Simple” Subprograms Implementing Subprograms with Stack-Dynamic.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms
ISBN Chapter 10 Implementing Subprograms.
Run-Time Environments Presented By: Seema Gupta 09MCA102.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
Implementing Subprograms
Definition of the Programming Language CPRL
Chapter 10 : Implementing Subprograms
Run-Time Environments Chapter 7
Implementing Subprograms Chapter 10
Functions.
Implementing Subprograms
Implementing Subprograms
Implementing Subprograms
Run-time organization
Run-Time Storage Organization
Run-Time Storage Organization
Precedence and Associativity
Implementing Subprograms
Chapter 10: Implementing Subprograms Sangho Ha
Chapter 9 :: Subroutines and Control Abstraction
Implementing Subprograms
Chap. 8 :: Subroutines and Control Abstraction
Implementing Subprograms
Midterm Review In Text: Chapters 1-3, 5-10, 15.
Implementing Subprograms
Final Review In Text: Chapters 1-3, 5-12,
UNIT V Run Time Environments.
CSE 3302 Programming Languages
Final Review In Text: Chapters 1-3, 5-16.
Final Review In Text: Chapters 1-3, 5-16.
PZ07A - Expressions Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section PZ07A.
Implementing Subprograms
Procedure Linkages Standard procedure linkage Procedure has
Implementing Subprograms
Chapter 10 Def: The subprogram call and return operations of
Presentation transcript:

Languages and Compilers (SProg og Oversættere) Sequence control and Subprogram Control

Sequence control and Subprogram Control Evaluation of expressions Explicit sequence control vs. structured sequence control Subprogram implementation Parameter mechanisms

Sequence control Implicit and explicit sequence control Expressions Precedence rules Associativity Statements Sequence Conditionals Iterations Subprograms Declarative programming Functional Logic programming

Expression Evaluation Determined by operator evaluation order operand evaluation order Operators: Most operators are either infix or prefix (some languages have postfix) Order of evaluation determined by operator precedence and associativity

… but it depends on the precedence of operators Example What is the result for: 3 + 4 * 5 + 6 Possible answers: 41 = ((3 + 4) * 5) + 6 47 = 3 + (4 * (5 + 6)) 29 = (3 + (4 * 5)) + 6 = 3 + ((4 * 5) + 6) 77 = (3 + 4) * (5 + 6) In most language, 3 + 4 * 5 + 6 = 29 … but it depends on the precedence of operators

Precedence table for ADA Operator Precedence Operators of highest precedence evaluated first (bind more tightly). Precedence for operators usually given in a table, e.g.: In APL, all infix operators have same precedence Level Operator Operation Highest ** abs not Exp, abs, negation * / mod rem + - Unary + - & Binary = <= < > => Relations Lowest And or xor Boolean Precedence table for ADA

C precedence levels Precedence Operators Operator names 17 tokens, a[k], f() Literals, subscripting, function call .,-> Selection 16 ++, -- Postfix increment/decrement 15* ++, -- Prefix inc/dec , -, sizeof Unary operators, storage !,&,* Logical negation, indirection 14 typename Casts 13 *, /, % Multiplicative operators 12 +,- Additive operators 11 <<, >> Shift 10 <,>,<=, >= Relational 9 ==, != Equality 8 & Bitwise and 7  Bitwise xor 6 | Bitwise or 5 && Logical and 4 || Logical or 3 ?: Conditional 2 =, +=, -=, *=, Assignment /=, %=, <<=, >>=, &=, =, |= 1 , Sequential evaluation Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000

Control of Statement Execution Sequential Conditional Selection Looping Construct Must have all three to provide full power of a Computing Machine

Loops Main types: Counter-controlled iteraters (For-loops) Logical-test iterators Recursion

Gotos Requires notion of program point Transfers execution to given program point Basic construct in machine language Implements loops

Advance in Computer Science Standard constructs that structure jumps if … then … else … end while … do … end for … { … } case … Modern style Group code in logical blocks Avoid explicit jumps except for function return Cannot jump into middle of block or function body But there may be situations when “jumping” is the right thing to do!

Exceptions: Structured Exit Terminate part of computation Jump out of construct Pass data as part of jump Return to most recent site set up to handle exception Unnecessary activation records may be deallocated May need to free heap space, other resources Two main language constructs Declaration to establish exception handler Statement or expression to raise or throw exception Often used for unusual or exceptional condition, but not necessarily.

Subprograms A subprogram has a single entry point The caller is suspended during execution of the called subprogram Control always returns to the caller when the called subprogram’s execution terminates Functions or Procedures? Procedures provide user-defined statements Functions provide user-defined operators

Executing Subprograms The subprogram call and return operations of a language are together called its subprogram linkage Call Semantics: 1. Save the execution status of the caller 2. Carry out the parameter-passing process 3. Pass the return address 4. Transfer control to the callee Return Semantics: 1. If pass-by-value-result parameters are used, move the current values of those parameters to their corresponding actual parameters 2. If it is a function, move the functional value to a place the caller can get it 3. Restore the execution status of the caller 4. Transfer control back to the caller Required Storage: - Status information of the caller, parameters, returnaddress, and functional value (if it is a function)

Code segment generated once at compiled time Activation Records Code segment generated once at compiled time Activation record instances created at run time Fresh activation record instance created for each call of subprogram Usually put in a stack (top inserted first)

Subprogram Implementation Code segment Activation Record Code to create activation record inst Program code Code to delete activation record inst const 1 const n Return point Static link Dynamic link Result data Input parameters Local parameters

Activation Records Static link – pointer to bottom of activation record instance of static parent Used to find non-local variables Dynamic link – pointer to top of activation record instance of the caller Used to delete subprogram activation record instance at completion

Subprogram Parameters Formal parameters: names (and types) of arguments to the subprogram used in defining the subprogram body Actual parameters: arguments supplied for formal parameters when subprogram is called Actual/Formal Parameter Correspondence: attributes of variables are used to exchange information Name – Call-by-name Memory Location – Call-by reference Value Call-by-value (one way from actual to formal parameter) Call-by-value-result (two ways between actual and formal parameter) Call-by-result (one way from formal to actual parameter)