w8Color - Code Generation for Subroutine Calls Parameter...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Code Generation for Subroutine Calls Parameter Types Activation Records Parameter Passing Code Examples
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Nomenclature Void Foo( ) { int x, y, z; call bar(x,y) } Void bar(int u,v) { } foo is the caller bar is the callee or called procedure/method/function x and y are the arguments (or actual parameters) for bar u and v are the ( formal ) parameters for bar I will usually say arguments for actual parameters and parameters for formal parameters .
Background image of page 2
3 Parameter Types • Reference (var) parameters: – the address is passed in to the subroutine. – Fortran does this – When passing expressions, the expression is evaluated, its result is placed in memory, and the address of that memory location is passed • Value Parameters : – copy at subroutine call. For large objects this can be done by either the caller or the callee. – an expression value is passed
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Parameter Types (2) • Result Parameters: – are copied at the end of the subroutine to return values to the caller • Value-Result Parameters: – “copy-in-copy-out”. Enhances locality and can be useful for parallelization. – Results often appear similar to passing by reference, but not always.
Background image of page 4
5 Parameter Types • Read-Only parameters: – small objects are passed by value, large parameters are passed by reference. – Compiler need to check that read-only parameter is used as such. – Aliasing can make this more interesting Void foo(readonly int x, int y) { int * p; if (…) p = & x else p = &y; *p = 4 Is this legal? Probably not, but hard to check. Aliasing can also let readonly parameters change values between reads!
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 Call by reference X = 1 Call f(x,x) Print(x) /* prints 4 */ void f(ref int z; ref int y ) { z = z + 1; Print(x); /* prints 2*/ y = y + 2; Print(x); /* prints 4*/ } For y and z, the address of x is passed. Accesses to z and y access the storage of x. This is (effectively, but not actually) the way C passes arrays, and the way Fortran passes everything (except constants)
Background image of page 6
7 Call by reference – why Fortran copies constants when passing by reference Call f(1) Print(1) Procedure f(int z) z = z + 1; Print(1); } Some early Fortran compilers passed constants by reference The address passed was to the address of the constants entry in a constant pool . The entry in the constant pool could be overwritten. In these compilers, all the print statements to the left would print “2”
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 Call by value X = 1 Call f(x,x) Void f(int z; int y) { z = z + 1; y = y + 1; print(z+y); } Print statement prints (1+1)+(1+1) = 4 Call by reference would print 3+3 = 6 When f is called, a 2 copies of x are made (either by caller or callee ). These copies are then manipulated within the routine. C and Java use call by value for scalars. To get call by ref semantics in C, pass in address of the arg (i.e. &x). Note that the address is still passed by reference.
Background image of page 8
9 Call by result X = 1 Call f(x,x) Print(x) /* prints 2 */ Void f(result int z; int y) { z =2; Print(x); /* prints 1*/ y = y + 2; } For z, the address of x is passed in.
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/19/2012 for the course ECE 468 taught by Professor Test during the Fall '08 term at Purdue University-West Lafayette.

Page1 / 71

w8Color - Code Generation for Subroutine Calls Parameter...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online