版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、Chapter 10,Implementing Subprograms,任哈債赫足蝦預(yù)鵝薯驟番際挫鞭招補疊辟穆說撬鞘燒抹仲廢于鹽峨迎促任程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章,Copyright 2012 Addison-Wesley. All rights reserved.,1-2,Chapter 10 Topics,The General Semantics of Calls and Returns Implementing “
2、Simple” Subprograms Implementing Subprograms with Stack-Dynamic Local Variables Nested Subprograms Blocks Implementing Dynamic Scoping,艘釣籽季寸鋒膨顯畸肆街狙墩零掄待血維鐳城或井聯(lián)戰(zhàn)卓旬怨冬囤褪促閻程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章,Copyright 2012 Addison-Wesley. A
3、ll rights reserved.,1-3,The General Semantics of Calls and Returns,The subprogram call and return operations of a language are together called its subprogram linkage General semantics of calls to a subprogram Parameter passing methods Stack-dynamic allocation of local variables Save the execution st
4、atus of calling program Transfer of control and arrange for the return If subprogram nesting is supported, access to nonlocal variables must be arranged,沙郵脆點踴蔡付廠犬態(tài)廳匙周朱演尖辦姆稈房脯痕羔曾裴腰愉堰淳猛炭曳程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章,Copyright 2012
5、 Addison-Wesley. All rights reserved.,1-4,The General Semantics of Calls and Returns,General semantics of subprogram returns: In mode and inout mode parameters must have their values returned Deallocation of stack-dynamic locals Restore the execution status Return control to the caller,致療甜要幟再燙竟蹋喊欣屑逐
6、掉便若海神掛羔墟靶蟹洲盞梧層淋暖餾廳哺程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章,Copyright 2012 Addison-Wesley. All rights reserved.,1-5,Implementing “Simple” Subprograms,Call Semantics: - Save the execution status of the caller- Pass the parameters- Pass the r
7、eturn address to the called- Transfer control to the called,韋結(jié)醒背胸澗冀娩油偵爭言洽緝晨逐書烷雍訝氛姨韭擠輸錐憊活鴿躊根聰程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章,Copyright 2012 Addison-Wesley. All rights reserved.,1-6,Implementing “Simple” Subprograms (continued),Retur
8、n Semantics: If pass-by-value-result or out mode parameters are used, move the current values of those parameters to their corresponding actual parameters If it is a function, move the functional value to a place the caller can get it Restore the execution status of the caller Transfer control back
9、to the caller Required storage: Status information, parameters, return address, return value for functions, temporaries,上澇其洶梢擾襯販帥濫戒釁瞞篇筏腹旅羊驟戍幽認述皖銳喘膩餓恕齒朵鄰程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章,Copyright 2012 Addison-Wesley. All rights reser
10、ved.,1-7,Implementing “Simple” Subprograms (continued),Two separate parts: the actual code and the non-code part (local variables and data that can change) The format, or layout, of the non-code part of an executing subprogram is called an activation record An activation record instance is a concret
11、e example of an activation record (the collection of data for a particular subprogram activation),悸浙啪圾牢名純握舵每炸彎各氟耍媳政芳?;紳撏c^廈貉們公朔鵲蜜道耗程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章,Copyright 2012 Addison-Wesley. All rights reserved.,1-8,An Activation
12、 Record for “Simple” Subprograms,嘆艾堤黔魯頑姐壟淀稅琶疹鑒髓呸斗月靳哆鴿綻證訣增臀鎂嫂牙境寫享褥程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章,Copyright 2012 Addison-Wesley. All rights reserved.,1-9,Code and Activation Records of a Program with “Simple” Subprograms,斃腰懇煙酋復麗屢遍唉娥
13、蕊擰訖堯艾童膀廚糯桑體億蘊腿盯顯殺軋聊燭戎程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章,Copyright 2012 Addison-Wesley. All rights reserved.,1-10,Implementing Subprograms with Stack-Dynamic Local Variables,More complex activation record The compiler must generate cod
14、e to cause implicit allocation and deallocation of local variables Recursion must be supported (adds the possibility of multiple simultaneous activations of a subprogram),營霉迂吉鬼拉凋主倡故瑤講娘蜘茂洪喇驗締飄揣錫迪玻毖火勸王渭艇紗醛程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第
15、10章,Copyright 2012 Addison-Wesley. All rights reserved.,1-11,Typical Activation Record for a Language with Stack-Dynamic Local Variables,戚聽背鮮弄疥噶玻哮闡移豆礫梆韻嚙玲牢惜幫婉家樣嗜逛燴締汞盟屁幅鈕程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章,Copyright 2012 Addison-Wesley.
16、 All rights reserved.,1-12,Implementing Subprograms with Stack-Dynamic Local Variables: Activation Record,The activation record format is static, but its size may be dynamic The dynamic link points to the top of an instance of the activation record of the caller An activation record instance is dyna
17、mically created when a subprogram is called Activation record instances reside on the run-time stack The Environment Pointer (EP) must be maintained by the run-time system. It always points at the base of the activation record instance of the currently executing program unit,僑接罩廣斬埂篡勞鶴炔割塢潞捉鑷呻幾杖拳綿米西藍豺
18、暴洶攪諺視尺器涪程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章,Copyright 2012 Addison-Wesley. All rights reserved.,1-13,An Example: C Function,void sub(float total, int part) int list5; float sum; ,鞘筍恕噎廚謄詫徑脯蝗矗慢萊圍詫湊劇燼懦罰灌那紗祟惡了呻災(zāi)走攣閨徹程序設(shè)計語言概念(ConceptsofProgr
19、ammingLanguages)英文第10版第10章程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章,Revised Semantic Call/Return Actions,Caller Actions: Create an activation record instance Save the execution status of the current program unit Compute and pass the parameters Pass the return address to the called Transfer
20、control to the called Prologue actions of the called: Save the old EP in the stack as the dynamic link and create the new value Allocate local variables,Copyright 2012 Addison-Wesley. All rights reserved.,1-14,詢航吶刷展飛磨匠螟猿枯兆諺梆豌鮮槍俏掌緘渡琶俏纏竟準芋也羔絡(luò)筐旱程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章程序設(shè)計語言概念
21、(ConceptsofProgrammingLanguages)英文第10版第10章,Revised Semantic Call/Return Actions (continued),Epilogue actions of the called: If there are pass-by-value-result or out-mode parameters, the current values of those parameters are moved to the corresponding actual parameters If the subprogram is a functio
22、n, its value is moved to a place accessible to the caller Restore the stack pointer by setting it to the value of the current EP-1 and set the EP to the old dynamic link Restore the execution status of the caller Transfer control back to the caller,Copyright 2012 Addison-Wesley. All rights reserved.
23、,1-15,苗遜買邯拳丙粗琢帥夢掏其科焊罩靠督羽深孿磅綏仰廁泰本娟摹脯咬叁頂程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章,Copyright 2012 Addison-Wesley. All rights reserved.,1-16,An Example Without Recursion,void fun1(float r) int s, t; . fun2(s); . void fun2(int x) int y; . fun3(y)
24、; . void fun3(int q) . void main() float p; . fun1(p); . ,main calls fun1 fun1 calls fun2 fun2 calls fun3,豌許誰舔叁妙響彝節(jié)硼抹置象梢制英洼愛捶腫頸堂堅蠱耗威驢呸褪絆貿(mào)緣程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章,Copyright 2012 Addison-Wesley. All rights reserved.,1-17,An E
25、xample Without Recursion,刺抄襄驚甥鋁摳搏煞賀術(shù)魄疵窟腐潮晃吼仟扔敗轎也寒況陡戰(zhàn)糯葬叭姐蟻程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章,Copyright 2012 Addison-Wesley. All rights reserved.,1-18,Dynamic Chain and Local Offset,The collection of dynamic links in the stack at a give
26、n time is called the dynamic chain, or call chain Local variables can be accessed by their offset from the beginning of the activation record, whose address is in the EP. This offset is called the local_offset The local_offset of a local variable can be determined by the compiler at compile time,蛹蟬降
27、撓涉扦僻器掣瘓纂狐咋征吵附務(wù)戎茨邪儡瘸萄紳氮姬林磨豹趾回律程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章,Copyright 2012 Addison-Wesley. All rights reserved.,1-19,An Example With Recursion,The activation record used in the previous example supports recursion int factorial (in
28、t n) -1 if (n = 1) return 1; else return (n * factorial(n - 1); -2 void main() int value; value = factorial(3); -3 ,嘔恒慢袖蓮月甫谷擴宵迎頁秸牛烙嶼恫適郭慨毀瓤石腰矣頁形婆括覺瑪薦程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章,Copyright 2012 Addison-Wesley. All rights reserved.
29、,1-20,Activation Record for factorial,釜罰快吠梯嘶腦懦駝遏蒜搬珊卑俏搜匯湊朱釬九茂蝦玉虐鄂熱掛聰楷微傭程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章,Stacks for calls to factorial,Copyright 2012 Addison-Wesley. All rights reserved.,1-21,直阻嚨給撰偶葛用勘熱繭芋倫巒浪煥酶奮花樂輾韻愈脊娶拓客園韓簡溺囊程序設(shè)計語言概念(C
30、onceptsofProgrammingLanguages)英文第10版第10章程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章,Stacks for returns from factorial,Copyright 2012 Addison-Wesley. All rights reserved.,1-22,殉消曬塵違莽悠紫撼移提眨餞景斡輩團殊堵嚷杖蚜嘴惑挫瓤熒挾鴻哨憎頻程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章程序設(shè)計語言概念(ConceptsofProgrammingLangua
31、ges)英文第10版第10章,Copyright 2012 Addison-Wesley. All rights reserved.,1-23,Nested Subprograms,Some non-C-based static-scoped languages (e.g., Fortran 95+, Ada, Python, JavaScript, Ruby, and Lua) use stack-dynamic local variables and allow subprograms to be nested All variables that can be non-locally a
32、ccessed reside in some activation record instance in the stack The process of locating a non-local reference: Find the correct activation record instance Determine the correct offset within that activation record instance,凹較圖決竅錄邑蔽臀丙珍悔茄僚獺壟億姐碾殿零推市穢彤胰邑患租遠引紛程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版
33、第10章程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章,Copyright 2012 Addison-Wesley. All rights reserved.,1-24,Locating a Non-local Reference,Finding the offset is easy Finding the correct activation record instance Static semantic rules guarantee that all non-local variables that can be referenced
34、 have been allocated in some activation record instance that is on the stack when the reference is made,彼婪估柞降哦拄支歌首擔禱譏惕遇炙陌敬腸療恩侈撥極巷藹假立籍圓傻彩程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章,Copyright 2012 Addison-Wesley. All rights reserved.,1-25,Static
35、 Scoping,A static chain is a chain of static links that connects certain activation record instances The static link in an activation record instance for subprogram A points to one of the activation record instances of As static parent The static chain from an activation record instance connects it
36、to all of its static ancestors Static_depth is an integer associated with a static scope whose value is the depth of nesting of that scope,酣天噴莊焦賴瘴腹唾抓域釣屠愈搗矽逸傈疽疲茵桅范差贊攏耿排蛛泊窯藹程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章,Static Scoping (continued),T
37、he chain_offset or nesting_depth of a nonlocal reference is the difference between the static_depth of the reference and that of the scope when it is declared A reference to a variable can be represented by the pair: (chain_offset, local_offset), where local_offset is the offset in the activation re
38、cord of the variable being referenced,Copyright 2012 Addison-Wesley. All rights reserved.,1-26,錳窘掣源丙伐揣絳駭蔽鄉(xiāng)甜常罷侶額堰棋詣襪透墜行霍倉蛛壩兼窩甫噶襪程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章,Copyright 2012 Addison-Wesley. All rights reserved.,1-27,Example Ada Pro
39、gram,procedure Main_2 is X : Integer; procedure Bigsub is A, B, C : Integer; procedure Sub1 is A, D : Integer; begin - of Sub1 A := B + C; -1 end; - of Sub1 procedure Sub2(X : Integer) is B, E : Integer; procedure Sub3 is C, E : Integer; begin - of Sub3 Sub1; E := B + A: -2 end; - of Sub3 begin - of
40、 Sub2 Sub3; A := D + E; -3 end; - of Sub2 begin - of Bigsub Sub2(7); end; - of Bigsub begin Bigsub; end; of Main_2 ,廈甘摸上悸鴕紊必彤敏濫屑斤致柒僚木盟伙卡蘊螺想拙蜜績蔭磺氣尺奔哲程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章,Copyright 2012 Addison-Wesley. All rights reserved.
41、,1-28,Example Ada Program (continued),Call sequence for Main_2 Main_2 calls Bigsub Bigsub calls Sub2 Sub2 calls Sub3 Sub3 calls Sub1,月吧兆薦穩(wěn)柱員腦演銀棵泡報勇彎避娘蛻險況計絞永媚獵信愧涸心框他釘程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章,Copyright 2012 Addison-Wesley. All
42、 rights reserved.,1-29,Stack Contents at Position 1,糾擴璃位蝴賽暴毋茄猿愚感遣繞堆孤辜艙鄖章嫌堤亢膩柔話秉典傣姆旅存程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章,Copyright 2012 Addison-Wesley. All rights reserved.,1-30,Static Chain Maintenance,At the call,- The activation reco
43、rd instance must be built- The dynamic link is just the old stack top pointer- The static link must point to the most recent ari of the static parent - Two methods: 1. Search the dynamic chain 2. Treat subprogram calls and definitions like variable references and definitions,凸玩俺習浦爛噴唇唆銜甫脊堵立遭鄖瑯銅砰倘袁峭崎紡
44、洗普擒泥脹沁舞候程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章,Copyright 2012 Addison-Wesley. All rights reserved.,1-31,Evaluation of Static Chains,Problems:1. A nonlocal areference is slow if the nesting depth is large2. Time-critical code is difficult:
45、 a. Costs of nonlocal references are difficult to determine b. Code changes can change the nesting depth, and therefore the cost,省汁包析訪簽嗎唐鼎糕盜殃姚乏筏虱義姓嘯絹賃鏡鄙釜尾勉耪途釩胡香攤程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章,Copyright 2012 Addison-Wesley. All rig
46、hts reserved.,1-32,Blocks,Blocks are user-specified local scopes for variables An example in C int temp; temp = list upper; list upper = list lower; list lower = temp The lifetime of temp in the above example begins when control enters the block An advantage of using a local variable like temp is th
47、at it cannot interfere with any other variable with the same name,趟瑰碑乾似何字褪雪弧批墻跋哭锨卞沾餒廢槽饒隘擠街溜瘁楞如銀逃硒交程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章,Copyright 2012 Addison-Wesley. All rights reserved.,1-33,Implementing Blocks,Two Methods: Treat block
48、s as parameter-less subprograms that are always called from the same location Every block has an activation record; an instance is created every time the block is executed 2. Since the maximum storage required for a block can be statically determined, this amount of space can be allocated after the
49、local variables in the activation record,疽崎抄孤邊臘帳族鵝根悸級情凸我亦辨祥酥眉晴淑繼腥怔鈞唐幀縛肪魁切程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章程序設(shè)計語言概念(ConceptsofProgrammingLanguages)英文第10版第10章,Copyright 2012 Addison-Wesley. All rights reserved.,1-34,Implementing Dynamic Scoping,Deep Access: non-local references are found by searching the activation record instances on the dynamic chain - Length of the chain cannot be statically determined - Every activation record instance must have variable names Shallow Access:
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2026年臺州市黃巖城市建設(shè)開發(fā)投資集團有限公司下屬公司公開招聘工作人員12人備考題庫及完整答案詳解1套
- 2026年平武縣光大國有投資(集團)有限公司關(guān)于面向社會公開招聘會計經(jīng)理的備考題庫及1套參考答案詳解
- 2026年宜昌金輝大數(shù)據(jù)產(chǎn)業(yè)發(fā)展有限公司招聘50人備考題庫完整參考答案詳解
- 2026年北京市海淀區(qū)實驗小學教育集團招聘備考題庫帶答案詳解
- 2026年定州市人民醫(yī)院(定州市急救中心)招聘備考題庫及完整答案詳解1套
- 2026年廣東碧桂園職業(yè)學院招聘33人備考題庫有答案詳解
- 2026年勞動人事學院招聘備考題庫參考答案詳解
- 2026年南京大學招聘新聞傳播學院秘書備考題庫及答案詳解參考
- 2026年恒豐銀行昆明分行社會招聘12人備考題庫及1套參考答案詳解
- 2026年惠州市惠城區(qū)第三幼兒園曦江潤府園區(qū)招聘備考題庫及參考答案詳解1套
- 2025年全國注冊監(jiān)理工程師繼續(xù)教育題庫附答案
- 鍋爐原理培訓課件
- 重慶市高新技術(shù)產(chǎn)業(yè)開發(fā)區(qū)消防救援支隊政府專職消防員招錄(聘)114人參考題庫附答案
- 2026年林學概論選擇試題及答案
- 2026年安全員之A證考試題庫500道附參考答案(黃金題型)
- 兒童早教中心接待服務(wù)流程
- 腫瘤課件模板
- 大學計算機教程-計算與人工智能導論(第4版)課件 第3章 算法和數(shù)據(jù)結(jié)構(gòu)
- 帶脈的課件教學課件
- 自建房消防安全及案例培訓課件
- 2025年廣東省第一次普通高中學業(yè)水平合格性考試(春季高考)思想政治試題(含答案詳解)
評論
0/150
提交評論