




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、8. Code Generation Overview of Code GenerationGenerate executable code for a target machine that is a faithful representation of the semantics of the source codeDepends on the characteristics of the source language detailed information about the target architecturethe structure of the runtime enviro
2、nmentand the operating system running on the target machineCode generation is typically broken into several stepsIntermediate code generationGenerate some form of assembly code instead of actual executable codeOptimization: to improve the speed and reduce the size of the target codeOverview of Code
3、Generationlexical analysissyntax analysissemantic analysisintermediate code generationtarget code generationintermediate code optimizationtarget code optimizationAlthough a source program can be translated directly into the target language, some benefits of using a machine-independent intermediate c
4、ode are:Retargeting is facilitated: a compiler for a different machine can be created by attaching a back end for the new machineA machine-independent code optimizer can be applied to the intermediate codeOverview of Code GenerationOverview of Code GenerationWe will talk about general techniques of
5、code generation rather than present a detailed description for a particular target machineContents8.1 Intermediate Code and Data Structure for code GenerationMore8.2 Basic Code Generation TechniquesMore8.4 Code Generation of Control Statements and Logical ExpressionMore8.1 Intermediate Code and Data
6、 Structures for Code GenerationIntermediate CodeIntermediate Representation(or IR)A data structure that represents the source program during translationFor example: abstract syntax treeSuch an intermediate representation that resembles target code is called intermediate codeIntermediate CodeThe need
7、 for intermediate codeAbstract syntax tree does not resemble target codeparticularly in its representation of control flow constructsIntermediate code is the representation of the syntax tree in sequential form that more closely resembles target codeIntermediate CodeAdvantage of intermediate codeIt
8、is particularly useful when the goal of the compiler is to produce extremely efficient code;It can also be useful in making a compiler more easily retargetableTwo popular forms of intermediate code: Three -Address code P-code8.1.1 Three-Address CodeThe most basic instruction of three-address code de
9、signed to represent the evaluation of arithmetic expressions has the general form: x=y op zx,y,z are names, constants or compiler-generated temporary namesop stands for any arithmetic or logical operator, such as + ,and“Three-address code” comes from this form of instruction, in general each of x,y
10、and z represents an address in memoryExampleComputation of an expression is represented in three-address code2*a+(b-3) the correspondingthree-address code ist1 = 2*at2 = b-3t3 = t1+t2where t1,t2,t3 are names for temporaries, they correspond to the interior nodes of the syntax tree and represent thei
11、r computed values+*-2ab3Other instructions of three-address codeThree-address code for each construction of a standard programming languageAssignment statement has the form“x=y op z”, where op is a binary operationAssignment statement has the form “x=op y”, where op is a unary operationCopy statemen
12、t has the form “x=y” where the value of y is assigned to xOther instructions of three-address codeThe unconditional jump “goto L”Conditional jumps, such as “if B goto L” , “if_false B goto L” and “if A rop B goto L”Statement “Label L” represents the position of the jump address“read x” “write x”Stat
13、ement “halt” serves to mark the end of the codeExampleSample TINY programread x;If 0 x then fact:=1;repeat fact:=fact*x; x:=x-1;until x=0;write factendThree-address code for itread xt1=0 id==exp1.tacode=exp2.tacode+ id.strval| “=”| exp - = exp.taco
14、de=aexp.tacodeaexp1 - aexp2+ =newtemp()aexp1.tacode=aexp2.code +factor.tacode+ | ”=” ||”+”|aexp - = aexp.tacode=factor.tacodefactor - (exp)= factor.tacode=exp.tacodefactor - =num.strval factor.t
15、acode=“ ”factor - =id.strval factor.tacode=“ ”tacode attribute of expression (x=x+3)+4name=xname=xname=3name=t1;tacode=“t1=x+3”name=t1;tacode=“t1=x+3 x=t1”name=t1;tacode=“t1=x+3 x=t1”name=4name=t2;tacode=“t1=x+3 x=t1 t2=t1+4”expaexpfactor( exp )id = expaexp + factoraexp + factorfactorid
16、 (x)num (3)num (4)aexp(x)name=t1;tacode=“t1=x+3”Back8.4 Code Generation of Control Statements and Logical ExpressionsA program consists of declarations and statementsDeclarations do not generate intermediate codes, for each declared name, we create a symbol-table entryCode generation for assignment
17、and simple arithmetic expressions (8.2)Code generation for control statements and logical expressions (8.4)8.4.1 Code Generation for If and While StatementsForms of the if- and while-statements:if-stmt if ( e x p ) stmt | if ( exp ) stmt else stmtwhile-stmt while ( e x p ) stmtThe chief problem is t
18、o translate the structured control features into an “unstructured” equivalent involving jumpsCompilers arrange to generate code for such statements in a standard order that allows the efficient use of a subset of the possible jumps that target architecture might permit.The typical code arrangement f
19、or an if-statement is shown as follows: if-stmt-if (exp) stmt | if (exp) stmt else stmtThe typical code arrangement for a while-statementwhile-stmt- while (exp) stmtThree-Address Code for Control StatementFor the statement:if ( E ) S1 e l s e S2The following code pattern is generated:if_false t1 got
20、o L1goto L2label L1label L2Three-Address Code for Control Statementwhile-statement of the formwhile ( E ) Sthree-address code pattern to be generated:label L1if_false t1 goto L2goto L1label L2NoteThere needs only two kinds of jumpsUnconditional jumps (goto L)Jumps when the condition is false (if_fal
21、segoto L)The true case is always a “fall-through” case that needs no jump.This reduces the number of jumps that the compiler needs to generate8.4.3 Code Generation of Logical ExpressionsCode Generation of Logical ExpressionsLogical expressions (or Boolean Expressions) have two primary purposeThey ar
22、e used to compute logical valuesThey are used as test in control statements, such as if-then or while-doComponent of Logical expressionsThey are composed of the Boolean operators (and,or,not) applied to elements that are Boolean variables or relational expressionsRelational expressions are of the fo
23、rm “E1 relop E2”, where E1 and E2 are arithmetic expressions,relop is a comparison operator such as , , =Component of Logical expressionsBoolean expressions generated by the following grammarE-E or E | E and E | not E | (E) | id relop id | true | false or and and are left-associative, and or has the
24、 lowest precedence, then and , then notCode generation for Boolean ExpressionsCode for Boolean Expressions can be generated in a manner similar to arithmetic expressionsFor example, the translation for “a or b and not c” is the three-address sequencet1= not ct2=b and t1t3=a or t2Code generation for
25、Boolean ExpressionsShort circuitA logic operation is short circuit if it may fail to evaluate its second argument.For exampleA and B: if A is computed to be false then A and B can be determined to be false without evaluating BA or B: if A is known to be true, then A or B can be determined to be true without evaluating BShort circuitShort-circuit Boolean operators are similar to if-statements, except that they return values, and often they are defined using if-exp
溫馨提示
- 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)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 吉林工業(yè)職業(yè)技術(shù)學(xué)院《文化與翻譯》2023-2024學(xué)年第二學(xué)期期末試卷
- 上海農(nóng)林職業(yè)技術(shù)學(xué)院《大數(shù)據(jù)技術(shù)概論》2023-2024學(xué)年第二學(xué)期期末試卷
- 常州工學(xué)院《中小學(xué)管理學(xué)》2023-2024學(xué)年第二學(xué)期期末試卷
- 泰州2025年江蘇泰州市第二人民醫(yī)院招聘衛(wèi)生專業(yè)技術(shù)人員21人筆試歷年參考題庫附帶答案詳解-1
- 2025年熱壓硫化鋅(ZNS)晶體合作協(xié)議書
- 溫州大學(xué)《結(jié)構(gòu)力學(xué)上》2023-2024學(xué)年第二學(xué)期期末試卷
- 泉州輕工職業(yè)學(xué)院《微生物資源開發(fā)與利用》2023-2024學(xué)年第二學(xué)期期末試卷
- 清遠(yuǎn)職業(yè)技術(shù)學(xué)院《學(xué)校心理學(xué)》2023-2024學(xué)年第二學(xué)期期末試卷
- 重慶商務(wù)職業(yè)學(xué)院《數(shù)據(jù)新聞與數(shù)據(jù)可視化》2023-2024學(xué)年第二學(xué)期期末試卷
- 福建信息職業(yè)技術(shù)學(xué)院《海商法學(xué)》2023-2024學(xué)年第二學(xué)期期末試卷
- 四川家庭農(nóng)場補(bǔ)貼標(biāo)準(zhǔn)2023年
- 智能人體秤市場需求分析報告
- 2023新北師大版高中英語選擇性必修三全冊課文翻譯(英漢對照)
- 設(shè)備采購供貨安裝實(shí)施方案
- 初中生物《病毒》說課課件
- 國網(wǎng)考試企業(yè)文化能源與戰(zhàn)略題庫
- 智聯(lián)招聘行測題庫2023
- 小工考勤表記工模板
- 編輯學(xué)概論-課件
- 理發(fā)店個人門面轉(zhuǎn)讓合同
- 03J111-1 輕鋼龍骨內(nèi)隔墻
評論
0/150
提交評論