1 / 55

温故知新:第 4 章 要 点

温故知新:第 4 章 要 点. 语义规则的两种描述方法:语法制导的定义和翻译方案。 设计简单问题的语法制导定义和翻译方案,这是本章的重点和难点。 S 属性的自下而上计算(边分析边计算) 。 L 属性的自上而下计算(边分析边计算) 。 L 属性的自下而上计算(边分析边计算) 。. 课 堂 练 习. 1. S 属性定义是指只包含 ________ 属性的语法制导定义。 2. L 属性定义是指包含了综合属性和那些 ________________________________ 的继承属性。

jbuehler
Download Presentation

温故知新:第 4 章 要 点

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 温故知新:第 4 章 要 点 • 语义规则的两种描述方法:语法制导的定义和翻译方案。 • 设计简单问题的语法制导定义和翻译方案,这是本章的重点和难点。 • S属性的自下而上计算(边分析边计算)。 • L属性的自上而下计算(边分析边计算)。 • L属性的自下而上计算(边分析边计算)。

  2. 课 堂 练 习 1. S属性定义是指只包含________属性的语法制导定义。 2. L属性定义是指包含了综合属性和那些________________________________的继承属性。 3. 语法制导定义的自上而下分析是将非终结符映射为分析过程,而继承属性映射为分析过程的________________,综合属性映射为分析过程的____________________。 4. 翻译方案是将语义规则插入到产生式__________的某个位置,反映出该语义规则的执行时间。 5.如果文法G是无二义的,则它的任何句子α ,满足 a. 最左推导和最右推导对应的语法树必定相同 b. 最左推导和最右推导对应的语法树可能不同 c. 最左推导和最右推导必定相同 d. 可能存在两个不同的最左推导,但它们对应的语法树相同 6. 文法G:S→xSx| y所识别的语言是: xyx b. (xyx)* c. xnyxn(n≥0) d. x*yx* 7. 给定文法: SaSb SbAc AaSA Ac 写出翻译方案,打印出句子中共有多少个c。

  3. 课 堂 练 习(答案) 1. S属性定义是指只包含_综合_______属性的语法制导定义。 2. L属性定义是指包含了综合属性和那些__只依赖于左边兄弟节点和父节点的继承属性。 3. 语法制导定义的自上而下分析是将非终结符映射为分析过程,而继承属性映射为分析过程的_参数_____,综合属性映射为分析过程的___返回值__。 4. 翻译方案是将语义规则插入到产生式_右部__的某个位置,反映出该语义规则的执行时间。 5.如果文法G是无二义的,则它的任何句子α ,满足 a a. 最左推导和最右推导对应的语法树必定相同 b. 最左推导和最右推导对应的语法树可能不同 c. 最左推导和最右推导必定相同 d. 可能存在两个不同的最左推导,但它们对应的语法树相同 6. 文法G:S→xSx| y所识别的语言是: c xyx b. (xyx)* c. xnyxn(n≥0) d. x*yx* 7. 给定文法: SaSb SbAc AaSA Ac 写出翻译方案,打印出句子中共有多少个c。 注意:要增加一个新的开始符号! S’S {print(S.num);} SaS1b {S.num = S1.num;} SbAc {S.num = A.num + 1;} AaSA1 {A.num = S.num + A1.num;} Ac {A.num = 1;}

  4. 6 Run-Time Environments Activation of procedure An execution of procedure call is called an activation of the procedure. Activation record an activation record is used to store information about the status of the machine, such as the value of the program counter and machine registers, when a procedure call occurs Topics in this chapter • How the data are organized in activation records • How activation records are organized during the procedure execution 1/46

  5. 6 Run-Time Environments • Support recursion or not • Whether local variables are reserved after return • Whether nonlocal variables could be accessed • Parameter passing • Whether procedures could be passed as parameters • Whether procedures could be returned as return values • Whether storage could be dynamically allocated, under procedural control • Whether storage must be released explicitly

  6. 6.1Local Storage Strategy (Inside Procedures) 6.1.1 Procedure Procedure definition, procedure call, formal parameter, actual parameter, lifetime of activation Procedure definition Procedure call Procedure f(a:int, b:bool) { int c; … } void maini() { f(3,true); … }

  7. 6.1Local Storage Strategy 6.1.2 Scope and binding of names Scope rule • The scope of a declaration of x is the context in which uses of x refer to this declaration. int a; Function f(int b) { int c; … } void main() { a = 0; c = 0; f(3); } correct error

  8. 6.1Local Storage Strategy 6.1.2 Scope and binding of names Scope rule • Declared once, and the name could be used to represent different data objects during the execution of the program. int a; Function f(int b) { int c; … } void main() { a = 0; f(3); f(5); } C has different values in f(int) 5/46

  9. Environment State Name Storage unit value 6.1Local Storage Strategy A = b 6.1.2 Scope and binding of names A 5 b 4 • Bind name to storage • The environment maps names to left value (storage unit), and the state maps left value to right value (value) • Assignment changes the state, not the environment • If the environment maps name x to storage unit s, we say x is bound to s A 5

  10. 6.1Local Storage Strategy Storage unit that stores the information used by procedure activations 6.1.3 Activation record (frame) • Each time the program is invoked, a trunk of memory, which contains • Compiled binary code • Data object (static and dynamic variables) • Control stack used to control the execution of the procedures (activation record)

  11. Code Static data Stack Heap 6.1Local Storage Strategy 6.1.3 Activation record • When the compiled program is executed, a block of memory assigned from OS • A.k.a frame Target code(.exe) Static variables and extern variables Activation records Activation records and dynamic variables

  12. Code Static data Frame of main stack Frame of p Heap 6.1Local Storage Strategy 6.1.3 Activation record • Ex void p() {…} void main() { …; p(); ….; }

  13. Actual parameter Return value Control link Access link Saved machine status Local data Temporaries 6.1Local Storage Strategy 6.1.3 Activation Record Layout of activation record Parameter passed to the procedure return value of the called function Pointer pointed to the procedure call Point to nonlocal data stored in other frames Save the status before procedure call Local variables of current procedure Temporary variables 10/46

  14. 6.1Local Storage Strategy 6.1.4 Local Data Arrangement • a byte is the smallest unit of addressable memory • we assume the run-time storage comes in blocks of contiguous bytes • The local variables are located in the frame, according to the sequence in which they are declared • The addresses of local variables are represented by offsets (with respect to the start address of the current procedure’s frame) • Storage strategy influenced by the addressing mode of the target machine. For example, the alignment problem will lead to padding (Space left un used due to alignment considerations) of the data.

  15. Example On a X86/Linux workstation, the size of the following structs are 24 and 16, respectively, why? typedef struct _a{ typedef struct _b{ char c1; char c1; long i; char c2; char c2; long i; double f; double f; }a; }b;

  16. padding padding c1 i c2 f 0 1 4 8 9 16 24 Answer typedef struct _a{ char c1; long i; char c2; double f; }a;

  17. 衬垫空白区 f c1 c2 i 0 1 8 2 4 16 Answer typedef struct _b{ char c1; char c2; long i; double f; }b;

  18. 6.1Local Storage Strategy 6.1.5Blocks • Syntax like {declaration statement} • Contain local variable declaration. Can be nested • The closest nested scope rule • Scope of declarations in B contains B • Let B1; B2; …; Bk be all the blocks that surround this use of x, with Bk the smallest, nested within Bk-1, which is nested within Bk-2, and so on. Search for the largest i such that there is a declaration of x belonging to Bi. This use of x refers to the declaration in Bi. Alternatively , this use of x is within the scope of the declaration in Bi • Blocks are not activated at the same time • Variable storage in blocks may overlap { …int a; {….int b; a = 3; } {….int a } } B B1 B2 Treat blocks as procedures without parameter and return value 15/46

  19. 6.1Local Storage Strategy main() { / begin of B0/ int a = 0; int b = 0; { / begin of B1/ int b = 1; {/ begin of B2/ int a = 2; }/ end of B2/ {/ begin of B3 / int b = 3; }/ end of B3/ }/ end of B1/ }/ end of B0/

  20. 6.1Local Storage Strategy main() { / begin of B0/ int a = 0; int b = 0; { / begin of B1/ int b = 1; {/ begin of B2/ int a = 2; }/ end of B2/ {/ begin of B3 / int b = 3; }/ end of B3/ }/ end of B1/ }/ end of B0/

  21. a0 b0 b1 a2, b3 Overlapped storage unit 6.1Local Storage Strategy main() { / begin of B0/ int a = 0; int b = 0; { / begin of B1/ int b = 1; {/ begin of B2/ int a = 2; }/ end of B2/ {/ begin of B3 / int b = 3; }/ end of B3/ }/ end of B1/ }/ end of B0/

  22. 代 码 静 态 数 据 栈 堆 6.2 Global Storage Strategy • 介绍程序运行时所需的各个活动记录在存储空间的分配策略 • 描述过程的目标代码怎样访问绑定到局部名字的存储单元 • 介绍三种分配策略 • 静态分配策略 • 栈式分配策略 • 堆式分配策略

  23. Code Static Data Stack Heap 6.2 Global Storage Strategy 6.2.1Memory Layout 20/46

  24. 6.2Global Storage Strategy 6.2.2Static Allocation • Name is bound to storage unit, no need of run-time support • The bound scope is the lifetime of the program • When re-enter the procedure, the value and access control of the local variables are the same as the previous execution • The size and layout of activation records are determined by the code generator via the information about names stored in the symbol table

  25. 6.2Global Storage Strategy Limitations of static allocation • Recursive procedure not supported • The size and offset of data object have to be known at compile time • Dynamic data structure not supported

  26. 6.2 Global Storage Strategy Application of static allocation • Fortransupport static allocation • Extern and constant values in C could be statically allocated • Declared outside procedures • Extern • Declared inside procedures • Static local variables • Constant

  27. 6.2 Global Storage Strategy 6.2.3 Stack Allocation • Used to manage the activation records of procedures • The scope of local variables are the lifetime of the procedure • When entering the procedure, local variables are bound to storage unit. When finishing, local variables are released

  28. s q(2,3) r q(2,1) q(3,3) p(2,3) 6.2 Global Storage Strategy 6.2.3Stack Allocation • Activation tree: represent the activations of procedures during the running of an entire program by a tree, called an activation tree Properties • Each node corresponds to one activation • the root is the activation of the “main" procedure • At a node for an activation of procedure p, the children correspond to activations of the procedures called by this activation of • one child must finish before the activation to its right can begin 25/46

  29. s q(2,3) r q(2,1) q(3,3) p(2,3) 6.2 Global Storage Strategy The current activated frame could be stored in the stack Control stack:s, q (2, 3), p(2, 3) Control Stack. Procedure calls and returns are usually managed by a runtime stack called the control stack.

  30. s a : array 6.2 Global Storage Strategy Run-time stack:Extend the information of control stack, to include the local information (frame) of the activation s

  31. s s a : array r r i: integer 6.2 Global Storage Strategy Run-time stack:Extend the information of control stack, to include the local information (frame) of the activation

  32. s s a : array q(2,3) r q (2, 3) k: integer 6.2 Global Storage Strategy Run-time stack:Extend the information of control stack, to include the local information (frame) of the activation

  33. s s a : array q(2,3) r q (2, 3) p(2,3) k: integer p(2, 3) k: integer 6.2 Global Storage Strategy Run-time stack:Extend the information of control stack, to include the local information (frame) of the activation 30/46

  34. 6.2 Global Storage Strategy Management of run-time stack: • Procedure call sequence and return are maintained by run-time stack, to store and recover machine status • Calling sequence: consists of code that allocates an activation record on the stack and enters information into its fields • Return sequence: is similar code to restore the state of the machine so the calling procedure can continue its execution after the call. Calling sequence and Return sequence are located in the calling procedures and the callee procedures, respectively

  35. 6.2 Global Storage Strategy • Calling sequences and the layout of activation records may differ greatly, even among implementations of the same language. • Design principles • Values communicated between caller and callee are generally placed at the beginning of the callee's activation record, so they are as close as possible to the caller's activation record. • Fixed-length items are generally placed in the middle • Items whose size may not be known early enough are placed at the end of the activation record • locate the top-of-stack pointer to the end of the fixed-length fields in the activation record.

  36.  Caller’s frame Return value and parameter Caller’s task Stack Control link Access link, machine status Local and temp data Callee’s task Callee’s frame base_sp Control link Access link, machine status Local and temp data top_sp 6.2 Global Storage Strategy Tasks of caller and callee p calls q P q Return value and parameter

  37.  Return value and parameter Stack Control link Access link, machine status Local and temp data base_sp Control link Access link, machine status Local and temp data top_sp 6.2 Global Storage Strategy p invokes the calling sequence of q • The caller evaluates the actual parameters • The caller stores a return address and the old value of top_sp into the callee's activation record. The caller then increments top sp to the position • The callee saves the register values and other status information. • The callee initializes its local data and begins execution P q Return value and parameter

  38.  Return value and parameter Stack Control link Access link, machine status Local and temp data base_sp Control link Access link, machine status Local and temp data top_sp 6.2 Global Storage Strategy corresponding return sequence q put the return value to the neighbor of p’s activation record • The callee places the return value next to p’s frame • q decrement the value of top_sp • q restores the other registers, and returns to p • p therefore may use the return values, relative to the current value of top_sp P q Return value and parameter

  39.  Return value and parameter Stack Control link Access link, machine status Local and temp data base_sp Control link Access link, machine status Local and temp data top_sp 6.2 Global Storage Strategy • Case: variable-length of parameters • Use registers to pass parameters • Compiler generate codes that push these parameter in reverse order into the stack • The callee knows the address of the first parameter • Calculate the addresses of the subsequent parameters based on the first parameter P q Return value and parameter

  40. Control link A’s pointer P’s frame B’s pointer Array A P’s array Array B base_sp Control link Q’s frame Q’s array top_sp 6.2 Global Storage Strategy • Case: size of activation record could not be determined at compile time • Size of local array could be determined when the procedure is activated • In the activation frame, store the pointers to arrays • During run-time, these pointers point to the arrays located at the top of the stack

  41. 6.2 Global Storage Strategy Dynamically releasing the stack allocated memories may lead to dangling-reference problem: referring to storage unit that has been released main() | int  dangle ( ) { | { int q; | int j = 20; q = dangle ( ); | return &j; } | }

  42. 6.2 Global Storage Strategy 6.2.4Heap Allocation Stack allocation not suitable for the following situations: • Local variables have to survive after the procedure activation • Callee’s life time longer than the caller. Hence activation tree could not describe the control flow of the program. • Dynamic variables in Pascal and C do not follow stack allocation rules • In Java variables could not be manually released

  43. 6.2 Global Storage Strategy 6.2.4Heap Allocation • The allocation and release in arbitrary order • In Heap there exist memories in use and those that have been released 40/46

  44. Control link Control link Control link Control link Control link s q (1,9) s q(1,9) r r q (1, 9) s 6.2 Global Storage Strategy 6.2.4Heap Allocation Heap • Difference between stack allocation and heap allocation Stack Activation tree

  45. Comparison

  46. What are the storage allocation strategy for each variable? int a; void p(int b){ int c; int *d = malloc(sizeof(int)*b); } void main(){p(3);} Example

  47. Answer int a; void p(int b){ int c; int *d = malloc(sizeof(int)*b); } void main(){p(3);} Extern, static Formal, stack Local, stack Dynamic, heap

  48. Exercise • 6.2, 6.3, 6.4 45/46

More Related