chapter 8 . sequence control -...

45
UNIT 3 www.getmyuni.com

Upload: nguyenkien

Post on 30-Jul-2018

226 views

Category:

Documents


0 download

TRANSCRIPT

UNIT 3

www.getmyuni.com

Presentation Outline

• Sequence control with expressions

• Conditional Statements, Loops

• Exception Handling

• Subprogram definition and activation

• Simple and Recursive Subprogram

• Subprogram Environment

www.getmyuni.com

Sequence control

Control of the order of execution of the operations

both primitive and user defined.

Implicit : determined by the order of the statements

in the source program or by the built-in execution

model

Explicit : the programmer uses statements to change

the order of execution (e.g. uses If statement)

www.getmyuni.com

Levels of sequence control

Expressions: How data are manipulated using

precedence rules and parentheses.

Statements: conditional and iteration statements change

the sequential execution.

Declarative programming: an execution

model that does not depend on the order of the

statements in the source program.

Subprograms: transfer control from one program to

another.

www.getmyuni.com

Sequencing with expressions

What is the sequence of performing the operations?

How is the sequence defined, and how is it represented?

Functional composition : Basic sequence-control

mechanism:

Given an operation with its operands, the operands may

be:

· Constants

· Data objects

· Other operations

www.getmyuni.com

Example

Example 1: 3 * (var1 + 5)

operation - multiplication, operator: *, arity - 2

operand 1: constant (3)

operand 2: operation addition

operand1: data object (var1)

operand 2: constant (5)

www.getmyuni.com

More examples and questions

Example 2: 3* var1 +5

Question: is the example equivalent to the above one?

Example 3: 3 + var1 +5

Question: is this equivalent to (3 + var1) + 5,

or to 3 + (var1 + 5) ?

www.getmyuni.com

Precedence and associativity

Precedence concerns the order of applying

operations

Associativity deals with the order of operations of

same precedence.

Precedence and associativity are defined when the

language is defined - within the semantic rules for

expressions.

www.getmyuni.com

Arithmetic operations / expressions

Linear representation of the expression tree:

Prefix notation

· Postfix notation

· Infix notation

Prefix and postfix notations are parentheses-free.

www.getmyuni.com

Execution-time representation of expressions

Machine code sequence

Tree structures - software simulation

Prefix or postfix form - requires stack, executed by an

interpreter.

www.getmyuni.com

Evaluation of tree representation

Eager evaluation - evaluate all operands before

applying operators.

Lazy evaluation

www.getmyuni.com

Problems

Side effects - some operations may change operands of

other operations.

Error conditions - may depend on the evaluation

strategy (eager or lazy evaluation)

Boolean expressions - results may differ depending on

the evaluation strategy.

www.getmyuni.com

Conditional statements

if expression then statement1 else

statement2

if expression then statement1

a choice among many alternatives

nested if statements

case statements

Implementation: jump and branch machine

instructions, jump table implementation for case

statements

www.getmyuni.com

Iteration statements

Simple repetition (for loop)

Specifies a count of the number

of times to execute a loop:

perform statement K times;

for loop -

Examples:

for I=1 to 10 do statement;

for(I=0;I<10; I++) statement;

www.getmyuni.com

Repetition while condition holds

while expression do statement;

Evaluate expression and if true execute statement, then

repeat process.

repeat statement until expression;

Execute statement and then evaluate expression.

Repeat if expression is not true.

C++ for loop functionally is equivalent to repetition

while condition holds

www.getmyuni.com

www.getmyuni.com

www.getmyuni.com

Problems with structured sequence control

Multiple exit loops

Exceptional conditions

Do-while-do structure

Solutions vary with languages, e.g. in C++ - break

statement, assert for exceptions.

www.getmyuni.com

Exceptions and exception handlers

Exception Handlers are subprograms that are not

invoked by explicit calls

Special situations, called exceptions:

Error conditions

Unpredictable conditions

Tracing and monitoring

www.getmyuni.com

Exceptions and exception handlers

Exception handlers typically contain only:

A set of declarations of local variables

A sequence of executable statements

Exception Handlers can be

- predefined in the language

- programmer defined

www.getmyuni.com

Raising and catching an exception

Languages provide methods for raising (throwing) and

testing for exceptions.

try {

statement1;

statement2;

if badCondition throw ExceptionName;

}

catch ExceptionName

{ ……….// do something for exception…….}

www.getmyuni.com

Implementation

Operating system exceptions - raised directly

by hardware interrupts.

Programmer defined -

the translator inserts code to handle the

exceptions.

www.getmyuni.com

Subprogram Control

Subprogram Control :

interaction among subprograms

how subprograms pass data among themselves

www.getmyuni.com

Subprogram Sequence Control

Simple subprogram call return

Copy rule view of subprograms:

the effect of a call statement is the same as if the

subprogram were copied and inserted into the

main program.

www.getmyuni.com

Assumptions

Subprograms cannot be recursive

Explicit call statements are required

Subprograms must execute completely at each call

Immediate transfer of control at point of call

Single execution sequence

www.getmyuni.com

Simple flow of execution

CALL

RETURN

www.getmyuni.com

Simple call-return subprograms

Execution of subprograms

Subprogram definition.

Subprogram activation.

www.getmyuni.com

Subprogram definition

The definition is translated into a template, used

to create an activation each time a subprogram is

called.

www.getmyuni.com

Subprogram activation

a code segment (the invariant part) -

executable code and constants,

an activation record (the dynamic part) -

local data, parameters.

created a new each time the subprogram is called,

destroyed when the subprogram returns.

www.getmyuni.com

System-defined pointers

Current-instruction pointer – CIP

address of the next statement to be executed

Current-environment pointer – CEP

pointer to the activation record.

www.getmyuni.com

www.getmyuni.com

On call instruction

An activation record is created

Current CIP and CEP are saved in the createdactivation record as return point

CEP is assigned the address of the activationrecord.

CIP gets the address of the first instruction in thecode segment

The execution continues from the address in CIP

www.getmyuni.com

On return

•The old values of CIP and CEP are retrieved.

•The execution continues from the address in CIP

Restrictions of the model:

at most one activation of any subprogram

www.getmyuni.com

The simplest implementation

Allocate storage for a single activation record statically

as an extension of the code segment.

Used in FORTRAN and COBOL.

The activation record is not destroyed - only reinitialized

for each subprogram execution.

Hardware support - CIP is the program counter,

CEP is not used, simple jump executed on return.

www.getmyuni.com

Stack-based implementation

The simplest run-time storage management technique

call statements : push CIP and CEP

return statements : pop CIP and CEP off of the stack.

Used in most C implementations

LISP: uses the stack as an environment.

www.getmyuni.com

Recursive Subprograms

Specification

Syntactically - no difference

Semantically - multiple activations of the

same subprogram exist simultaneously at

some point in the execution.

E.G. the first recursive call creates a second

activation within the lifetime of the first activation.

www.getmyuni.com

Implementation

Stack-based -

CIP and CEP are stored in stack, forming a

dynamic chain of links.

A new activation record is created for each call

and destroyed on return.

The lifetimes of the activation records cannot

overlap - they are nested.

www.getmyuni.com

Attributes of Data Control

Data control features determine the accessibility of data at

different points during program execution.

Central problem:

the meaning of variable names, i.e. the correspondence

between names and memory locations.

www.getmyuni.com

Names and Referencing Environments

Two ways to make a data object available as an operand

for an operation

Direct transmission

Referencing through a named data object

www.getmyuni.com

Direct transmission

A data object computed at one point as the result of

an operation may be directly transmitted to another

operation as an operand

Example: x = y + 2*z;

The result of multiplication is transmitted directly as

an operand of the addition operation

www.getmyuni.com

Referencing through a named data object

A data object may be given a name when it is

created, the name may then be used to designate it

as an operand of an operation.

www.getmyuni.com

Program elements that may be named

Variables

Formal parameters

Subprograms

Defined types

Defined constants

Labels

Exception names

Primitive operations

Literal constants

www.getmyuni.com

Associations and Referencing Environments

Association: binding identifiers to particular data

objects and subprograms

Referencing environment: the set of identifier

associations for a given subprogram.

Referencing operations during program execution:

determine the particular data object or subprogram

associated with an identifier

www.getmyuni.com

Local referencing environment

The set of associations created on entry to a subprogram formal

parameters, local variables, and subprograms defined only within that

subprogram.

Subprogram Environment

Non-local referencing environment

The set of associations for identifiers

• used within a subprogram

• not created on entry to it

Global referencing environment: associations created at the start of execution of the main program, available to

be used in a subprogram.

Predefined referencing environments:

predefined associations in the language definition.

www.getmyuni.com

THANK YOU.. !!

www.getmyuni.com