Upload
adela-simmons
View
219
Download
0
Embed Size (px)
Citation preview
Narges S. Bathaeian
و پیاده سازی طراحي زبانها
کنترل زیربرنامه
Narges S. Bathaeian
loadشكل كلي حافظه بعد از برنامه
...
Code segment
Data segment
ك شده به ي
ص داده صي
خظه ت
حافبرنامه كد تابع
main……
1كد تابع ........
2كد تابع ........
globalمتغيرهاي ……
xپارامترهاي تابع آدرس برگشت
متغيرهاي محلي xتابع
متغيرهاي موقت xتابع
........
Narges S. Bathaeian
Activation recordglobalمتغيرهاي
……
تابع xپارامترهايبرگشت آدرس
محلي متغيرهايxتابع
موقت متغيرهايxتابع
........
تابع yپارامترهايبرگشت آدرس
محلي متغيرهايyتابع
موقت متغيرهايyتابع
……..
Activation record تابعx
Activation record تابعy
4
activation recordRemember that data storage for subprograms is in an activation record.
var X: integer;
X is of type integer.
L-value of X is some specific offset in an activation record.
Goal is to look at locating activation record for P.
Given an expression: X = Y + Z
1. Locate activation record containing Y.
2. Get L-value of Y from fixed location in activation record.
3. Repeat process for Z and then X.
Narges S. Bathaeian
انواع محيطهاي اجرا( كامال ايستاfully static) :simple call-
returnFORTRAN77
( مبتني بر پشتهstack based)C, PASCAL
( كامال پوياfully dynamic)LISP
Narges S. Bathaeian
Fully static قبل از اجراي برنامه, يكActivation
record براي هر تابع , در نظر گرفته مي شود.
( همه متغيرهاglobal, local از يك آدرس )ثابت قابل دستيابي هستند.
پارامترهاي توابع تنها اشاره گر به مكانهايي اززننده هستند. تابع صدا
.توابع بازگشتي نمي توانيم داشته باشيم.پوينتر نمي توانيم داشته باشيم
Narges S. Bathaeian
شكل كلي حافظه در محيط كامال )الگوی اول(ايستا
تابع mainكد
تابع 1 كد
….
تابع n كد
globalمتغيرهاي
Activation record تابعmain
Activation record 1تابع
….
Activation record تابعn
Narges S. Bathaeian
شكل كلي حافظه در محيط كامال globalمتغيرهاي )الگوی دوم(ايستا
)common(
دستور آدرس
تابع mainكد
محلی متغیرهای و پارامترها
دستور آدرس
تابع 1كد
محلی متغیرهای و پارامترها
دستور آدرس
تابع 2كد
محلی متغیرهای و پارامترها
….
Narges S. Bathaeian
مثال از يك برنامه
FORTRAN
PROGRAM TESTCOMMON MAXSIZEINTEGER MAXSIZEREAL TABLE(10), TEMPMAXSIZE = 10READ *, TABLE(1), TABLE(2), TABLE(3)CALL QUADMEAN(TABLE,3,TEMP)PRINT *, TEMPENDSUBROUTIN QUADMEAN(A, SIZE, QMEAN)COMMON MAXSIZEINTEGER MAXSIZE, SIZEREAL A(SIZE), QMEAN, TEMPINTEGER KTEMP = 0.0IF(( SIZE.GT.MAXSIZE).OR.(SIZE.LT.1)) GOTO 99
DO 10 K=1, SIZETEMP = TEMP+ A(K) * A(K)
10 CONTINUE99 QMEAN = SQRT(TEMP/SIZE)
RETURNEND
Narges S. Bathaeian
مثال از يك برنامه
FORTRAN
PROGRAM TEST
COMMON MAXSIZE
INTEGER MAXSIZE
REAL TABLE(10), TEMP
MAXSIZE = 10
READ *, TABLE(1), TABLE(2), TABLE(3)
CALL QUADMEAN(TABLE,3,TEMP)
PRINT *, TEMP
END
SUBROUTIN QUADMEAN(A, SIZE, QMEAN)
COMMON MAXSIZE
INTEGER MAXSIZE, SIZE
REAL A(SIZE), QMEAN, TEMP
INTEGER K
TEMP = 0.0
IF(( SIZE.GT.MAXSIZE).OR.(SIZE.LT.1)) GOTO 99
DO 10 K=1, SIZE
TEMP = TEMP+ A(K) * A(K)
10 CONTINUE
99 QMEAN = SQRT(TEMP/SIZE)
RETURN
END
MAXSIZE
TABLE)1(…
TABLE)10(
TEMP
3
A
SIZE
QMEAN
RETURN ADDRESS
TEMP
K
Act. re
cع
تابTES
TA
ct. re
cع
تابQ
UA
DM
EA
N
Return addr. )IP = PC(
Narges S. Bathaeian
Stack based با فراخواني تابعactivation
record مربوط به آن در stack , push مي شود و هنگام بازگشت به
مي stack , popتابع صدازننده, از شود.
دو نوعStatic scope
بدونlocal procedure مانند زبان : C باlocal procedure مانند زبان : Pascal
Dynamic Scope
...
Code segment
Stack
Heap
PZ09A Programming Language design and Implementation -4th EditionCopyright©Prentice Hall, 2000
Activation record structure
Narges S. Bathaeian
مثال از يك برنامه
C #include <stdio.h>int x , y ;int gcd ( int u , int v ){
if (v==0) return u;else return gcd(v , u % v);
}
main(){
scanf(“%d%d”, &x , &y );printf(“%d\n”,gcd(x,y));return 0;
}
x: 15
y: 10
Act. rec تابع main
u : 15v : 10EPIP
u : 10v : 5EPIP
u : 5v : 0EPIP
CEPsp
Control link = EP: Environment Pointer
Narges S. Bathaeian
توضيحاتCEP )Current Environment Pointer( = FP
)frame pointer( رجيستري كه در آن آدرس :activation record.جاري در آن است
sp : stack pointer رجيستري كه در آن آدرس سر :stack.در آن است
EP آدرس :activation record.قبلي را دارد آدرس متغيرهايpush شده مي تواند با توجه به CEP و
اندازه بايت الزم تعيين شوند. متغيرهاي محلي تابع و موقت بعد ازreturn address
مي شوند.pushدر تابع CEP ها تشکیل یک زنجیره بنام DCP )Dynamic
chain pointer(.می دهند
Narges S. Bathaeian
هنگام فراخواني يك تابع(prologue): آرگومانها محاسبه شده و درstack , push مي
شوند. مقدارCEP درstack , push(.مي شود EP) مقدار جديدCEP برابرsp .جاري مي شود ( آدرس بازگشتPC) در stack , push مي
(IPشود. ) يكjump به ابتداي كد تابع فراخواني شده انجام
مي شود.
Narges S. Bathaeian
هنگام بازگشت از يك تابع(Epilogue): مقدارCEP درsp.ريخته مي شود مقدارEP به داخلCEP.ريخته مي شود يك پرش با توجه به آدرس موجود درreturn
address.به كد انجام مي شود آرگومانها ازstack , pop مي شوند تا مقدار
sp.تصحيح شود
Narges S. Bathaeian
مثال از يك برنامه
C #include <stdio.h>
int x , y ;
int gcd ( int u , int v )
{ int t;
if (v==0) return u;
else { t= gcd(v , u % v);
return t;}
}
main()
{
scanf(“%d%d”, &x , &y );
printf(“%d\n”,gcd(x,y));
return 0;
}
x: 15
y: 10
Act. rec تابع mainCEP= XX00
sp = XX04
pc = X0X0
Narges S. Bathaeian
مثال از يك برنامه
C #include <stdio.h>
int x , y ;
int gcd ( int u , int v )
{ int t;
if (v==0) return u;
else { t= gcd(v , u % v);
return t;}
}
main()
{
scanf(“%d%d”, &x , &y );
printf(“%d\n”,gcd(x,y));
return 0;
}
x: 15
y: 10
Act. rec تابع main
u : 15v : 10XX00X0X0t :
CEP=XX0B
sp=XX11
pc = X0XA
Narges S. Bathaeian
مثال از يك برنامه
C #include <stdio.h>
int x , y ;
int gcd ( int u , int v )
{ int t;
if (v==0) return u;
else { t= gcd(v , u % v);
return t;}
}
main()
{
scanf(“%d%d”, &x , &y );
printf(“%d\n”,gcd(x,y));
return 0;
}
x: 15
y: 10
Act. rec تابع main
u : 15v : 10XX00X0X0t :
CEP=XX0B
sp=XX0Fpc = X0XD
Narges S. Bathaeian
مثال از يك برنامه
C #include <stdio.h>
int x , y ;
int gcd ( int u , int v )
{ int t;
if (v==0) return u;
else { t= gcd(v , u % v);
return t;}
}
main()
{
scanf(“%d%d”, &x , &y );
printf(“%d\n”,gcd(x,y));
return 0;
}
x: 15
y: 10
Act. rec تابع main
u : 15v : 10XX00X0X0t :
u : 10v : 5XX0B X0XDt :
CEP
sp
Narges S. Bathaeian
متغيرها در زمان offsetمحاسبه كامپايل
آدرسEP در هرactivation record مبنا در نظر گرفته مي شود.
آدرس متغير هايي كه در بااليEP هستند)پارامترهاي تابع( با مقدار مثبت و بقيه با مقدار
منفي نمايش داده مي شوند.
Narges S. Bathaeian
مثال
v: +4 u: +6 t: -6
:فرض Int : 2 byte Float : 4 byte Address : 4 byte Char : 1 byte Double : 8 byte
x: 15
y: 10
Act. rec تابع main
u : 15v : 10XX00X0X0t :
u : 10v : 5XX0B X0XDt :
CEP
Narges S. Bathaeian
Procedureهاي تودر تو ( در هر حوزهscope به اسامی تعریف شده در )
آن حوزه میتوان دسترسی داشت. پس: هرprocedure به procedure هاي فرزند و برادر
خود دسترسي دارد و مي تواند آنها را فراخواني كند. و حوزه , آن حوزهمتغيرهاي تعريف شده در يك حوزه
است. حوزه فرزند آن حوزه های :پياده سازي
SCP )static chain pointer( یا Access link آدرس : Act. Rec دسترسي به( پدر procedure)پدر
فراخواني شده از پدرفراخواني شده از برادر
Narges S. Bathaeian
از يك 1مثال برنامه
Pascalprogram test; Var x,y:integer; Procedure p;
…procedure q;
begin…r;…
end;procedure r;
… begin
… end;Begin
…p;…
End;
x: 15
y: 10
Act. rec تابع main
test
P
q r
اجازه دسترسيها در زمان كامپايل
نگهداري مي .شود
Narges S. Bathaeian
از يك 1مثال برنامه
Pascal program test;Var x,y:integer;Procedure p;
…procedure q;
begin…r;…
end;procedure r;
…begin
…end;
Begin…p;…
End;
x: 15
y: 10Act. rec تابع main
…No SCPDCP…
Act
. R
ec
p
Narges S. Bathaeian
از يك 1مثال برنامه
Pascal program test;Var x,y:integer;Procedure p;
…procedure q;
begin…r;…
end;procedure r;
…begin
…q;…
end;Begin
…p;…
End;
x: 15
y: 10Act. rec تابع main
…No SCPDCP…
…
SCPDCP…
Act
. R
ec
pA
ct.
Rec
q
ني از طرف پدر:افراخوقرار داده مي شود. SCPدر CEPمقدار
Narges S. Bathaeian
از يك 1مثال برنامه
Pascal program test;Var x,y:integer;Procedure p;
…procedure q;
begin…r;…
end;procedure r;
…begin
…q;…
end;Begin
…p;…
End;
x: 15
y: 10Act. rec تابع main
…No SCPDCP…
…
SCPDCP…
…
SCPDCP…
Act
. R
ec
pA
ct.
Rec
qA
ct.
Rec
r
ني از طرف برادر:افراخوقرار داده مي شود. SCPبرادر در SCPمقدار
Narges S. Bathaeian
از يك برنامه2مثال Pascal
program test;Var x,y:integer;Procedure p;
…procedure q;
procedure r;…
begin…r;…
end;begin
…q;…
end;Begin
…p;…
End;
x: 15
y: 10Act. rec تابع main
…No SCPDCP…
…
SCPDCP…
…
SCPDCP…
Act
. R
ec
pA
ct.
Rec
qA
ct.
Rec
r
SCPزنجيره
Displays
The idea: Put the static links in a separate stack
called a display. The entries in the display are
pointers to the Act_recs that have the variables in the referencing environment.
Represent references as
(display_offset, local_offset)
where display_offset is the same as chain_offset
Narges S. Bathaeian
Displays
For a call to procedure P with a static_depth of k: Save, in the new Act_rec, a copy of the display
pointer at position k Put the link to the Act_rec for P at position k in
the display
Narges S. Bathaeian
Displays (Example)
Narges S. Bathaeian
program MAIN_3; procedure BIGSUB;
procedure SUB1; end; { SUB1 } procedure SUB2; procedure SUB3; end; { SUB3 } end; { SUB2 } end; { BIGSUB } end. { MAIN_3 }
Displays(Example)
Narges S. Bathaeian
program MAIN_3; procedure BIGSUB; procedure SUB1; end; { SUB1 } procedure SUB2; procedure SUB3; end; { SUB3 } end; { SUB2 } end; { BIGSUB }end. { MAIN_3 }
Case 1: SUB2 calls SUB1
Before the call, we have:
A.R. for SUB2
A.R. for BIGSUB 21
A.R. for MAIN_3 0
Stack Display
After the call, we have:
A.R. for SUB1
A.R. for SUB2
A.R. for BIGSUB 21
A.R. for MAIN_3 0
Stack Display
Displays(Example)
Narges S. Bathaeian
program MAIN_3; procedure BIGSUB; procedure SUB1; end; { SUB1 } procedure SUB2; procedure SUB3; end; { SUB3 } end; { SUB2 } end; { BIGSUB }end. { MAIN_3 }
Case 2: SUB2 calls SUB3
Before the call, we have:
AR. for SUB2
AR. for BIGSUB 21
AR. for MAIN_3 0
Stack Display
After the call, we have:
AR. for SUB3
AR. for SUB2 3 AR. for BIGSUB 2
1 AR. for MAIN_3 0
Stack Display
Displays(Example)
Narges S. Bathaeian
program MAIN_3; procedure BIGSUB; procedure SUB1; end; { SUB1 } procedure SUB2; procedure SUB3; end; { SUB3 } end; { SUB2 } end; { BIGSUB }end. { MAIN_3 }
Case 3: SUB3 calls SUB1 Before the call, we have:
AR. for SUB3
AR. for SUB23
AR. for BIGSUB 21
AR. for MAIN_3 0
Stack Display
After the call, we have:
AR. for SUB1
AR. for SUB3
AR. for SUB23
AR. for BIGSUB 21
AR. for MAIN_3 0 Stack Display
Dynamic scope rule refers to most recent activation
record )DCP( Not fully stack based
Stack: function calls Eamples
Lisp …
Narges S. Bathaeian
slide 36
Static vs. Dynamic Scope Example
var x=1;function g)z( { return
x+z; }function f)y( { var x = y+1; return g)y*x(;}f)3(;
x 1
x 4
y 3
z 12
outer block
f)3(
g)12(
Which x is used for expression x+z ?static scope
dynamic scope
Retention vs. Deletion Retention
Fortran Static and global
variables in C
Deletion Local variables in
C, Pascal
Narges S. Bathaeian
38
Parameter passing Parameter: A variable in a procedure that represents
some other data from the procedure that invoked the given procedure.
Parameter transmission: How that information is passed to the procedure.
• The parameter is also called the formal argument.The data from the invoking procedure is called the actual argument or sometimes just the argument.
Usual syntax: Actual arguments: call P(A, B+2, 27+3) Parameters: Procedure P(X, Y, Z) What is connection between the parameters and the
arguments?• Call by name• Call by reference• Call by value• Call by result (or value-result)
39
Language dependent
Difference languages have different mechanisms:
• ALGOL - name, value• Pascal - value, reference• C - value (BUT pointers give us reference
Constant tension between desire for efficiency and semantic correctness in defining parameter transmission.
40
Call by name Substitute argument for parameter at each occurrence of parameter:
Invocation: P(A, B+2, 27+3) Definition: procedure P(X,Y,Z)
{int I; I=7; X = I + (7/Y)*Z;} Meaning: P(X,Y,Z) {int I; I=7; A=I+(7/(B+2))*(27+3);}
This is a true macro expansion. Simple semantics, BUT:1. Implementation. How to do it?
2. Aliases. What if statement of P were: I = A?
3. Expressions versus statements: If we had D=P(1,2,3) and a return(42) in P, what does semantics mean?
4. Error conditions: P(A+B, B+2, 27+3)
41
Implementation of call by name A thunk is the code which computes the L-value and R-
value of an argument. For each argument, pass code address that computes both
L-values and R-values of arguments. P(A, B+2, 27+3) generates:
jump to subroutine P
address of thunk to return L-value(A)
address of thunk to return R-value(A)
address of thunk to return L-value(B+2)
address of thunk to return R-value(B+2)
address of thunk to return L-value(27+3)
address of thunk to return R-value(27+3) To assign to X, call thunk 1, To access X, call thunk 2To assign to Y, call thunk 3, To access Y, call thunk 4To assign to Z, call thunk 5, To access Z, call thunk 6
Issue: Assignment to (B+2): How? Call by name is conceptually convenient, but
inefficient.
42
Examples of Call by Name1. P(x) {x = x + x;}
Seems simple enough …Y = 2; P(Y); write(Y) means Y = Y+Y
write(Y) prints 42. int A[10];
for(I=0; I<10; I++) {A[I]=I;};I=1; P(A[I]) A[1] = A[1] + A[1] A[1] set to 2
3. But: F {I = I + 1; return I;}What is: P(A[F])?P(A[F]) A[F] = A[F]+A[F] A[I++] = A[I++]+A[I++]
A[2] = A[3]+A[4]
4. Write a program to exchange values of X and Y: (swap(X,Y))Usual way: swap(x,y) {t=x; x=y; y=t;}Cannot do it with call by name. Cannot handle both of following: swap(I, A[I]) swap(A[I],I)
One of these must fail.
43
Call by reference Pass the L-value of the argument for the parameter.
Invocation: P(A, B+2, 27+3)
Implementation:Temp1 = B+2Temp2 = 27+3jump to subroutine PL-value of AL-value of Temp1L-value of Temp2
This is the most common parameter transmission mechanism. In the procedure activation record, parameter X is a local variable whose R-value is the L-value of the argument.
44
Call by value Pass the R-value of the argument for the parameter.
Invocation: P(A, B+2, 27+3)
Implementation: Temp1 = B+2
Temp2 = 27+3
jump to subroutine P
R-value of A
R-value of Temp1
R-value of Temp2 In procedure activation record, parameter X is a local
variable whose R-value is the R-value of the argument.
45
Call by reference in C C only has call by value, BUT pointer variables allow for simulating call by reference:P(i, j) passes i and j by value.P(&i, &j) passes L-values of i and j.
P(*x, *y) {*x = *y + 1;} arguments are addresses (pointers)
Call by result (or value-result)
Call by value, AND pass back the final value to argument upon return.
Parameter is a local value in procedure.
Similar to call by reference, except for aliasing.Narges S. Bathaeian
Narges S. Bathaeian
Pass by value
void inc2)int x({ ++x; ++x; }
void inc2)int *x({ ++)*x(; ++)*x(; }
مقدار يك پارامتر قبل ازفراخواني تابع محاسبه
شده و نتيجه فرستاده مي شود.
تغيير پارامتر در تابعفراخواني شده, تاثيري در تابع فراخواني كننده ندارد.
مگر اينكه اشاره گر به متغير فرستاده شده باشد.
C , Pascal , ADA
بي تاثير
موثر
Narges S. Bathaeian
Pass by reference
void inc2)int &x({ ++x; ++x; }
مقدار آدرس پارامترفرستاده مي شود.
عنوان پارامتر در تابعفراخواني شده,
مجازي است.C& : ++Fortran77Pascal : var
در تابع فراخواني شده, آدرس
پارامتر كه بطور نسبي فرستاده
شده بايد با توجه دوباره SCPبه
محاسبه شود.
Narges S. Bathaeian
Pass by value-resultvoid p)int x, int y({ ++x; ++y; }
main)({ int a=1; p)a,a(; printf)“%d”, a(; return 0;}
مقدار يك پارامتر قبل ازفراخواني تابع محاسبه شده و نتيجه فرستاده
سپس مقدار مي شود.پارامتر پس از تغيير در
تابع فراخواني شده, دوباره در مكان اول خود
كپي مي شود.ADA : in out پياده سازي؟a=2
Narges S. Bathaeian
Pass by nameint a[10];int i;void p)int x({ ++i; ++x; }
main)({ i=a[1]=a[2]=1; p)a[i](; printf)“%d %d”, a[1],a[2](; return 0;}
مقدار يك پارامتر قبل از نشدهفراخواني تابع محاسبه
و در تابع فراخواني شده محاسبه مي شود.
هر پارامتر, خود يكprocedure.است
Algol60نتايج غير منتظرهپياده سازي مشكلLazy evaluating
a[1]=1a[2]=2
i=2;
Narges S. Bathaeian
Block برخي زبانها مانندC .بلوك بندي مي شوند Scope متغيرهاي تعريف شده در يك بلوك, آن بلوك و
بلوكهاي فرزند آن بلوك است....main(){
int i,j;{
int k=0;i=j=1;…
}k=… //undefined variable k…
}
Narges S. Bathaeian
پياده سازي بلوك با وارد شدن به يك بلوك متغيرها درstack,
push.مي شوند ازبا خارج شدن از يك بلوك متغيرها stack, pop
مي شوند. در محاسبهoffset متغيرها در زمان كامپايل بايد
يكساني offsetدقت شود. بلوكهاي برادر از شروع مي شوند.
Narges S. Bathaeian
Fully Dynamic در Modula2 مي توان يك procedure را به عنوان
ديگر داشت.procedureآرگومان برگشتي كامال درheap.پياده سازي مي شود :مثال صوري
typedef int (*proc)(void);proc g(int x){ int f(void) {return x; }
return f;}main(){ proc c;
c = g(2);printf(“%d”,c());return 0;
}
Narges S. Bathaeian
تخصيص حافظهدر خواست تخصيص حافظه توسط برنامه
malloc , newپيدا كردن فضاي مناسب خالي در حافظهدر خواست آزادسازي حافظه توسط برنامه
free آزادسازي حافظه و اضافه كردن آن به ليست
فضاهاي خالي
Narges S. Bathaeian
Dynamic environment براي مواقعي كه طول عمر يك متغير
مربوطه است.procedureبيشتر از بخاطرpop شدن procedure با stack
سازي نيست. قابل پياده:مثال
int *dangle)void({int x;
return &x;} است. پسآرگومان برگشتي یک xآدرس
تعريف stackبايد در قسمتي جدا از شده باشد.
...
Code segment
Stack
Heap