Presentation is loading. Please wait.

Presentation is loading. Please wait.

Narges S. Bathaeian طراحي و پیاده سازی زبانها کنترل زیربرنامه.

Similar presentations


Presentation on theme: "Narges S. Bathaeian طراحي و پیاده سازی زبانها کنترل زیربرنامه."— Presentation transcript:

1 Narges S. Bathaeian طراحي و پیاده سازی زبانها کنترل زیربرنامه

2 Narges S. Bathaeian شكل كلي حافظه بعد از load برنامه... Code segment Data segment حافظه تخصيص داده شده به يك برنامه كد تابع main …… كد تابع 1........ كد تابع 2........ متغيرهاي global …… پارامترهاي تابع x آدرس برگشت متغيرهاي محلي تابع x متغيرهاي موقت تابع x........

3 Narges S. Bathaeian Activation record متغيرهاي global …… پارامترهاي تابع x آدرس برگشت متغيرهاي محلي تابع x متغيرهاي موقت تابع x........ پارامترهاي تابع y آدرس برگشت متغيرهاي محلي تابع y متغيرهاي موقت تابع y …….. Activation record تابع x Activation record تابع y

4 4 activation record Remember 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.

5 Narges S. Bathaeian انواع محيطهاي اجرا كاملا ايستا (fully static): simple call-return FORTRAN77 مبتني بر پشته (stack based) C, PASCAL كاملا پويا (fully dynamic) LISP

6 Narges S. Bathaeian Fully static قبل از اجراي برنامه, يك Activation record براي هر تابع, در نظر گرفته مي شود. همه متغيرها (global, local) از يك آدرس ثابت قابل دستيابي هستند. پارامترهاي توابع تنها اشاره گر به مكانهايي از تابع صدا زننده هستند. توابع بازگشتي نمي توانيم داشته باشيم. پوينتر نمي توانيم داشته باشيم.

7 Narges S. Bathaeian شكل كلي حافظه در محيط كاملا ايستا ( الگوی اول ) كد تابع main كد تابع 1 ….…. كد تابع n متغيرهاي global Activation record تابع main Activation record تابع 1 ….…. Activation record تابع n

8 Narges S. Bathaeian شكل كلي حافظه در محيط كاملا ايستا ( الگوی دوم ) متغيرهاي global (common) آدرس دستور كد تابع main پارامترها و متغیرهای محلی آدرس دستور كد تابع 1 پارامترها و متغیرهای محلی آدرس دستور كد تابع 2 پارامترها و متغیرهای محلی ….….

9 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

10 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. rec تابع TEST Act. rec تابع QUADMEAN Return addr. (IP = PC)

11 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

12 PZ09A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 Activation record structure

13 Narges S. Bathaeian مثال از يك برنامه C #include 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 : 15 v : 10 EP IP u : 10 v : 5 EP IP u : 5 v : 0 EP IP CEP sp Control link = EP: Environment Pointer

14 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) می دهند.

15 Narges S. Bathaeian هنگام فراخواني يك تابع (prologue): آرگومانها محاسبه شده و در stack, push مي شوند. مقدار CEP در stack, push مي شود.(EP) مقدار جديد CEP برابر sp جاري مي شود. آدرس بازگشت (PC) در stack, push مي شود. (IP) يك jump به ابتداي كد تابع فراخواني شده انجام مي شود.

16 Narges S. Bathaeian هنگام بازگشت از يك تابع (Epilogue): مقدار CEP در sp ريخته مي شود. مقدار EP به داخل CEP ريخته مي شود. يك پرش با توجه به آدرس موجود در return address به كد انجام مي شود. آرگومانها از stack, pop مي شوند تا مقدار sp تصحيح شود.

17 Narges S. Bathaeian مثال از يك برنامه C #include 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 CEP= XX00 sp = XX04 pc = X0X0

18 Narges S. Bathaeian مثال از يك برنامه C #include 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 : 15 v : 10 XX00 X0X0 t : CEP=XX0B sp=XX11 pc = X0XA

19 Narges S. Bathaeian مثال از يك برنامه C #include 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 : 15 v : 10 XX00 X0X0 t : CEP=XX0B sp=XX0F pc = X0XD

20 Narges S. Bathaeian مثال از يك برنامه C #include 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 : 15 v : 10 XX00 X0X0 t : u : 10 v : 5 XX0B X0XD t : CEP sp

21 Narges S. Bathaeian محاسبه offset متغيرها در زمان كامپايل آدرس EP در هر activation record مبنا در نظر گرفته مي شود. آدرس متغير هايي كه در بالاي EP هستند ( پارامترهاي تابع ) با مقدار مثبت و بقيه با مقدار منفي نمايش داده مي شوند.

22 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 : 15 v : 10 XX00 X0X0 t : u : 10 v : 5 XX0B X0XD t : CEP

23 Narges S. Bathaeian Procedure هاي تودر تو در هر حوزه (scope) به اسامی تعریف شده در آن حوزه میتوان دسترسی داشت. پس : هر procedure به procedure هاي فرزند و برادر خود دسترسي دارد و مي تواند آنها را فراخواني كند. حوزه متغيرهاي تعريف شده در يك حوزه, آن حوزه و حوزه های فرزند آن حوزه است. پياده سازي : SCP (static chain pointer) یا Access link : آدرس Act. Rec پدر ( دسترسي به procedure پدر ) فراخواني شده از پدر فراخواني شده از برادر

24 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: 10 Act. rec تابع main test P q r اجازه دسترسيها در زمان كامپايل نگهداري مي شود.

25 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: 10 Act. rec تابع main … No SCP DCP … Act. Rec p

26 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: 10 Act. rec تابع main … No SCP DCP … SCP DCP … Act. Rec p Act. Rec q فراخواني از طرف پدر : مقدار CEP در SCP قرار داده مي شود.

27 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: 10 Act. rec تابع main … No SCP DCP … SCP DCP … SCP DCP … Act. Rec p Act. Rec q Act. Rec r فراخواني از طرف برادر : مقدار SCP برادر در SCP قرار داده مي شود.

28 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: 10 Act. rec تابع main … No SCP DCP … SCP DCP … SCP DCP … Act. Rec p Act. Rec q Act. Rec r زنجيره SCP

29 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

30 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

31 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 }

32 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 2 1 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 2 1 A.R. for MAIN_3 0 Stack Display

33 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 2 1 AR. for MAIN_30 StackDisplay After the call, we have: AR. for SUB3 AR. for SUB2 3 AR. for BIGSUB 2 1 AR. for MAIN_30 StackDisplay

34 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 SUB2 3 AR. for BIGSUB 2 1 AR. for MAIN_30 StackDisplay After the call, we have: AR. for SUB1 AR. for SUB3 AR. for SUB2 3 AR. for BIGSUB 2 1 AR. for MAIN_30 StackDisplay

35 Dynamic scope rule refers to most recent activation record (DCP) Not fully stack based Stack: function calls Eamples Lisp … Narges S. Bathaeian

36 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); x1 x4 y3 z12 outer block f(3) g(12) Which x is used for expression x+z ? static scope dynamic scope

37 Retention vs. Deletion Retention Fortran Static and global variables in C Deletion Local variables in C, Pascal Narges S. Bathaeian

38 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 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 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 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 2 To assign to Y, call thunk 3, To access Y, call thunk 4 To 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 42 Examples of Call by Name 1. P(x) {x = x + x;} Seems simple enough … Y = 2;P(Y); write(Y)  means Y = Y+Y write(Y)  prints 4 2. 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 43 Call by reference Pass the L-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 L-value of A L-value of Temp1 L-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 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 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)

46 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

47 Pass by value void inc2(int x) {++x; ++x; } void inc2(int *x) {++(*x); ++(*x); } مقدار يك پارامتر قبل از فراخواني تابع محاسبه شده و نتيجه فرستاده مي شود. تغيير پارامتر در تابع فراخواني شده, تاثيري در تابع فراخواني كننده ندارد. مگر اينكه اشاره گر به متغير فرستاده شده باشد. C, Pascal, ADA بي تاثير موثر

48 Narges S. Bathaeian Pass by reference void inc2(int &x) {++x; ++x; } مقدار آدرس پارامتر فرستاده مي شود. عنوان پارامتر در تابع فراخواني شده, مجازي است. C++ : & Fortran77 Pascal : var در تابع فراخواني شده, آدرس پارامتر كه بطور نسبي فرستاده شده بايد با توجه به SCP دوباره محاسبه شود.

49 Narges S. Bathaeian Pass by value-result void p(int x, int y) {++x; ++y; } main() { int a=1; p(a,a); printf( “ %d ”, a); return 0; } مقدار يك پارامتر قبل از فراخواني تابع محاسبه شده و نتيجه فرستاده مي شود. سپس مقدار پارامتر پس از تغيير در تابع فراخواني شده, دوباره در مكان اول خود كپي مي شود. ADA : in out پياده سازي؟ a=2

50 Narges S. Bathaeian Pass by name int 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]=1 a[2]=2 i=2;

51 Narges S. Bathaeian Block برخي زبانها مانند C بلوك بندي مي شوند. Scope متغيرهاي تعريف شده در يك بلوك, آن بلوك و بلوكهاي فرزند آن بلوك است.... main() { int i,j; { int k=0; i=j=1; … } k= … //undefined variable k … }

52 Narges S. Bathaeian پياده سازي بلوك با وارد شدن به يك بلوك متغيرها در stack, push مي شوند. با خارج شدن از يك بلوك متغيرها از stack, pop مي شوند. در محاسبه offset متغيرها در زمان كامپايل بايد دقت شود. بلوكهاي برادر از offset يكساني شروع مي شوند.

53 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; }

54 Narges S. Bathaeian تخصيص حافظه در خواست تخصيص حافظه توسط برنامه malloc, new پيدا كردن فضاي مناسب خالي در حافظه در خواست آزادسازي حافظه توسط برنامه free آزادسازي حافظه و اضافه كردن آن به ليست فضاهاي خالي

55 Narges S. Bathaeian Dynamic environment براي مواقعي كه طول عمر يك متغير بيشتر از procedure مربوطه است. بخاطر pop شدن procedure با stack قابل پياده سازي نيست. مثال : int *dangle(void) {int x; return &x;} آدرس x یک آرگومان برگشتي است. پس بايد در قسمتي جدا از stack تعريف شده باشد.... Code segment Stack Heap


Download ppt "Narges S. Bathaeian طراحي و پیاده سازی زبانها کنترل زیربرنامه."

Similar presentations


Ads by Google