Programming Language Pragmatics - PowerPoint PPT Presentation

1 / 33
About This Presentation
Title:

Programming Language Pragmatics

Description:

Vars as values: implicit at elaboration time. Execution. Base class constructors first ... Objects created implicitly at elaboration time. Dynamic method binding ... – PowerPoint PPT presentation

Number of Views:492
Avg rating:3.0/5.0
Slides: 34
Provided by: csVir
Category:

less

Transcript and Presenter's Notes

Title: Programming Language Pragmatics


1
  • Programming Language Pragmatics
  • Chapters 8 10
  • Tamim Sookoor
  • 11/26/2007

2
Chapter 8
  • Subroutines and Control Abstraction

3
Details
  • Stack frame or activation record
  • Args, return values, local vars, return address,
    saved registers, temps
  • When subroutine returns, its frame is popped
  • Important pointers
  • Stack pointer register points to first unused
    location at the top of the stack
  • Frame pointer register points to a reference
    address within the frame
  • Objects in frame addressed via offset wrt frame
    pointer

4
Details (II)
  • Variable-size area at top of frame to store
    objects whose size not known at compile time
  • If no such object, frame pointer is not needed

5
Free references
  • Finding objects that are neither local nor global
    in a language with nested subroutines
  • Static chain each stack frame has a reference to
    the frame of the surrounding subroutines
    (requires multiple dereferencing)
  • Display embedding of a static chain into an
    array (requires two memory accesses only)

6
Cost of static chains vs. displays
  • Nesting of subroutines more than 2-3 levels is
    rare, hence static chains are short
  • Cost of displays is slightly higher than static
    chains
  • Static chains simplify representation of closures
    (just store code address and static link)
  • Display imposes a limit on max nexting

7
Calling sequences
  • Maintenance of stack frames in done by code
    executed
  • By caller just before and just after subroutine
    call (calling sequence)
  • By callee (subroutine)
  • At the beginning (prologue)
  • At the end (epilogue)

8
Making a subroutine call
  • On the way into a subroutine call, the following
    things need to be done
  • Passing parameters
  • Saving return address
  • Changing program counter
  • Changing stack pointer
  • Saving registers (incl. Frame pointer)
  • Changing frame pointer.
  • Executing init code for objects in new frame

9
Making a subroutine call
  • On the way out of a subroutine call, the
    following things need to be done
  • Passing return parameters of function values
  • Executing finalization code for local objects
  • Deallocating stack frame (restoring stack
    pointer)
  • Restoring saved registers (incl. Frame pointer)
  • Restoring program counter

10
Division of labor
  • Most tasks can be performed either by the caller
    or the called
  • Saving registers
  • Only need to save the registers used both in the
    callee and the caller
  • Callee saves all registers it will overwrite
  • Divide registers into two sets, caller-saves and
    callee-saves

11
Callers duties
  • Passing parameters
  • Static chain maintenance (part of). Caller
    computes the callees static link and passes it
    as an extra hidden parameter
  • Callee nested directly inside caller caller
    passes its own frame pointer
  • Callee nested k scopes outward caller
    dereferences its own static link k times and
    passes result
  • Display maintenance is more complicated

12
In-line expansion
  • Expands subroutines in-line at the point of the
    call
  • Advantages
  • Avoids overhead associated with subroutine calls
    gt faster calls
  • Programmer free to use many short subroutines
  • Compiler chooses which subs to in-line
  • Programmer can make suggestions
  • Disadvantages
  • Code bloating
  • Does not apply to recursive calls

13
Parameter passing
  • Formal parameters names that appear in the
    declaration of a subroutine
  • Actual parameters or arguments variables and
    expressions passed to subroutine in a call

14
Parameter modes
  • Call by value a copy of the arguments value is
    passed
  • Call by reference the address of the argument is
    passed
  • Formal parameter is alias of actual parameter
  • Changes in the formal parameter affect the actual
    parameter
  • Actual parameter must be an l-value (e.g. not an
    arithmetic expression)

15
Chapter 9
  • Building a Runnable Program

16
Intermediate Forms
  • High-level based on trees or DAGs (e.g. syntax
    trees) or stacks
  • Medium-level control flow graphs containing
    pseudo assembly language
  • Three-address instructions
  • Two operands
  • An operator
  • A destination
  • Low-level assembly language of target machine

17
Compilers and IFs
  • Why would a family of compilers be based on a
    single IF?
  • This would enable reuse of front-ends and
    back-ends for different language-machine
    combinations
  • Why might a compiler employ more than one IF?
  • Compilers do as much work as possible on a high
    or medium-level IF to increase machine
    independence, but perform most code improvement
    on a low-level IF, closely modeled after the
    assembly language of the target machine

18
Back-End Compiler
  • What is a basic block?
  • Maximal-length set of operators that should be
    executed sequentially at run time, with no
    branches in or out
  • What is the difference between local and global
    code improvement?
  • Local modification of instruction sequence
    within each basic block to eliminate redundant
    loads, stores, and arithmetic computation
  • Global removal of redundancies across basic
    block boundaries

19
Address space
  • Two kinds of object code
  • Relocatable
  • Input to linker
  • Several files linked together into an executable
    program
  • Executable
  • Input to loader
  • Can be loaded into memory and run

20
A relocatable object file
  • Includes the following descriptive info
  • Import table identifies instructions that refer
    to named locations presumed to lie in other files
  • Relocation table identifies instructions that
    refer to locations in current file, which must be
    modified at link time to reflect the position of
    current file in the executable program
  • Export table lists names and addresses of
    locations in current file that can be referred to
    in other files

21
Assembly
  • Translates assembly code into executable code
  • Replace opcodes and operands with machine
    language encodings
  • Replace symbolic names with actual addresses
  • Modern assemblers may perform
  • Instruction scheduling
  • Register allocation
  • Peephole optimization

22
Constructing instructions
  • Many assemblers
  • Extend the instruction set in minor ways to make
    assembly language easier for humans to read
  • Have directives
  • Segment switching, e.g. .text in MIPS
  • Data generation, e.g. .byte, .half, .word, .float
  • Symbol identification, e.g. .globl
  • Alignment, e.g. .align

23
Linker
  • Joins together compilation units
  • Static linker prior to program execution
  • Dynamic linker during execution
  • Two subtasks
  • Relocation
  • Resolution of external references

24
Chapter 10
  • Data Abstraction and Object Orientation

25
Advantages of OOP
  • Reduces conceptual load
  • Minimizes amount of detail programmer must think
    about at any one time
  • Provides fault and change containment
  • Limits the portion of a program that needs to be
    looked at when debugging
  • Limits the portion of a program that needs to be
    changed if the interface is the same
  • Provides independence among program components
  • Facilitates code reuse

26
Containers
  • Abstraction that holds a collection of objects of
    some element class
  • Alternatives
  • Based on container element base class
  • Objects derived from the element base class
  • List node as separate object containing a pointer
  • List node member of listed object
  • Designing consistent, intuitive and useful class
    hierarchies is difficult

27
Inheritance and Visibility in C
  • Inheritance
  • Hiding methods of base class in C
  • private base class (to hide)
  • using declarations (to make visible)
  • Protected members visible to methods of own
    class or of derived classes or friends
  • Private members visible to methods of own class
    or friends

28
Constructors
  • Constructor does not allocate space
  • It initializes already allocated space
  • Issues
  • Choosing one out of several constructors
  • References versus values
  • Execution order of constructors
  • Destructors vs. garbage collection

29
Constructors
  • More than one constructor often possible
  • Different names (Eiffel)
  • Different number and type of args (Java/C)
  • Creation
  • Vars as references explicit
  • Vars as values implicit at elaboration time
  • Execution
  • Base class constructors first
  • Constructors of members first

30
References and Values
  • Reference model
  • More elegant
  • Objects must be allocated from the heap
  • Extra level of indirection (less efficient)
  • Objects created explicitly
  • Value model
  • More efficient
  • Difficult to control initialization
  • Objects created implicitly at elaboration time

31
Dynamic method binding
  • Possible to use a derived class where the base
    class is expected
  • Derived class redefines a method of the base
    class

32
Virtual methods
  • Methods specified as virtual in C and Simula
    are bound dynamically at run time based on the
    class of the object
  • Can/must an implementation of a virtual method be
    defined in the base class?
  • Possible to omit implementation (body) by
    following subroutine declaration with 0
  • virtual void foo () 0

33
Abstract classes
  • A bodyless virtual method is an abstract method
    or pure virtual method
  • A class is abstract if it has at least one
    abstract method
  • An abstract class
  • Cannot be instantiated!!!
  • Serves as a base for other concrete classes
Write a Comment
User Comments (0)
About PowerShow.com