DOC PREVIEW
UA CSC 520 - Lecture Notes

This preview shows page 1-2-3-24-25-26-27-49-50-51 out of 51 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 51 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 51 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 51 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 51 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 51 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 51 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 51 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 51 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 51 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 51 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 51 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Procedures as Control AbstractionsProcedures as Control Abstractionsldots QuestionsCase Study --- PascalPascal ProceduresPascal Proceduresldots Pascal Proceduresldots Pascal Proceduresldots Pascal Proceduresldots Case Study --- AdaAda --- Subprogram DeclarationsAda --- Subprogram DeclarationsAda --- Subprogram BodiesAda --- Procedure CallAda --- Procedure CallAda --- Procedure Callldots Ada --- Overloaded CallsAda --- Userdefined OperatorsMemory OrganizationRun-Time Memory OrganizationRun-Time Memory Organizationldots Storage AllocationStorage Allocationldots Global Variables -- MIPSGlobal Variables -- Allocation by NameGlobal Variables -- Allocation in BlockGlobal Variables -- Allocation on StackStorage Allocationldots Storage Allocationldots RecursionRecursion ExamplesRecursion ExampleRecursion ExampleCalling ConventionsProcedure Call ConventionsExample Call/Return SequenceThe Control LinkThe Control Linkldots The Control Linkldots Procedure Call on the MIPSMIPS Activation RecordMIPS Procedure CallMIPS Procedure Callldots MIPS Procedure Callldots MIPS Procedure Callldots MIPS Procedure ReturnsMIPS Procedure Returnsldots Readings and ReferencesSummarySummaryldots520—Spring 2005—30CSc 520Principles of ProgrammingLanguages30: Procedures — IntroductionChristian [email protected] of Computer ScienceUniversity of ArizonaCopyrightc 2005 Christian Collberg[1]520—Spring 2005—30Procedures as Control AbstractionsA procedure is a collection of computation (expressions,statements, etc).that we can give a name.A call-site is a location where a caller invokes aprocedure, the callee.The caller waits for the callee to finish executing, atwhich time controls to the point after the call-site.Most procedures are parameterized. The valuespassed to the procedure are calledactual parameters.The actual parameters are mapped toformal parameters, which hold the actual values withinthe procedure.[2]520—Spring 2005—30Procedures as Control Abstractions...Some procedures (called functions) return a value. Insome languages, a function can return multiple values.Most languages use a call-stack on which actualparameters and local variables are stored.Different languages have different rules as to howparameters should be passed to a procedure.[3]520—Spring 2005—30QuestionsHow do we deal with recursion? Every new recursivecall should get its own set of local variables.How do we pass parameters to a procedure?Call-by-Value or Call-by-Reference?In registers or on the stack?How do we allocate/access local and global variables?How do we access non-local variables? (A variable isnon-local in a procedure P if it is declared in procedurethat statically encloses P.)How do we pass large structured parameters (arraysand records)?[4]520—Spring 2005—30Case Study — Pascal[5]520—Spring 2005—30Pascal ProceduresPROCEDURE Name (list of formals);CONST (* Constant declarations *)TYPE (* Type declarations *)VAR (* Variable declarations *)(* Procedure and function definitions *)BEGIN(* procedure body *)END;Note the similarity with the program structure.Note that procedures can be nested.Note the semicolon after the end.[6]520—Spring 2005—30Pascal Procedures...Formal parameters look like this:procedure name (formal1:type1; formal2:type2;...);or like thisprocedure name (formal1,formal2...:type1; ...);By default, arguments are passed by value. varindicates that they are passed by reference:procedure name (var formal1:type1; ...);[7]520—Spring 2005—30Pascal Procedures...Functions are similar to procedures but return values:function func1 (formals);beginfunc1 := 99;end;To return a value assign it to the function name.[8]520—Spring 2005—30Pascal Procedures...Procedures can be nested:procedure A ();procedure B();begin...end;begin...end;Names declared in an outer procedure are visible tonested procedures unless the name is redeclared.[9]520—Spring 2005—30Pascal Procedures...Procedures can be recursive. The forward declarationis used to handle mutually recursive procedures:procedure foo (); forward;procedure bar ();beginfoo();end;procedure foo();beginbar();end;[10]520—Spring 2005—30Case Study — Ada[11]520—Spring 2005—30Ada — Subprogram Declarationsprocedure Traverse_Tree;procedure Increment(X : in out Integer);procedure Right_Indent(Margin : out Line_Size);procedure Switch(From, To : in out Link);function Random return Probability;function Min_Cell(X : Link) return Cell;function Next_Frame(K : Positive) return Frame;function Dot_Product(Left, Right : Vector)return Real;[12]520—Spring 2005—30Ada — Subprogram Declarationsfunction "*"(Left, Right : Matrix) return Matrix;Examples of in parameters with default expressions:procedure Print_Header(Pages : in Natural;Header : in Line :=(1 .. Line’Last => ’ ’);Center : in Boolean := True);[13]520—Spring 2005—30Ada — Subprogram Bodies-- Example of procedure body:procedure Push(E : in Element_Type;S : in out Stack) isbeginif S.Index = S.Size thenraise Stack_Overflow;elseS.Index := S.Index + 1;S.Space(S.Index) := E;end if;end Push;[14]520—Spring 2005—30Ada — Procedure CallTraverse_Tree;Print_Header(128, Title, True);Switch(From => X, To => Next);Print_Header(128, Header => Title,Center => True);Print_Header(Header=>Title,Center=>True, Pages=>128);--Examples of function calls:Dot_Product(U, V)Clock[15]520—Spring 2005—30Ada — Procedure Call-- Procedures with default expressions:procedure Activate(Process : in Process_Name;After : in Process_Name:=No_Process;Wait : in Duration := 0.0;Prior : in Boolean := False);procedure Pair(Left, Right :in Person_Name:=new Person);[16]520—Spring 2005—30Ada — Procedure Call...-- Examples of their calls:Activate(X);Activate(X, After => Y);Activate(X, Wait => 60.0, Prior => True);Activate(X, Y, 10.0, False);Pair;Pair(Left => new Person, Right => new Person);[17]520—Spring 2005—30Ada — Overloaded Callsprocedure Put(X : in Integer);procedure Put(X : in String);procedure Set(Tint : in Color);procedure Set(Signal : in Light);-- Examples of their calls:Put(28);Put("no possible ambiguity here");Set(Tint=>Red); -- Set(Red) is ambiguous.Set(Signal=>Red); -- Red can denote eitherSet(Color’(Red)); -- a Color or a Light[18]520—Spring 2005—30Ada — Userdefined Operatorsfunction "+" (Left,Right:Matrix) return Matrix;function "+" (Left,Right:Vector) return Vector;-- assuming that A, B, and C are of-- the type Vector the following two-- statements are


View Full Document

UA CSC 520 - Lecture Notes

Documents in this Course
Handout

Handout

13 pages

Semantics

Semantics

15 pages

Haskell

Haskell

15 pages

Recursion

Recursion

18 pages

Semantics

Semantics

12 pages

Scheme

Scheme

32 pages

Syllabus

Syllabus

40 pages

Haskell

Haskell

17 pages

Scheme

Scheme

27 pages

Scheme

Scheme

9 pages

TypeS

TypeS

13 pages

Scheme

Scheme

27 pages

Syllabus

Syllabus

10 pages

Types

Types

16 pages

FORTRAN

FORTRAN

10 pages

Load more
Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?