📄 porttour2
字号:
Suppose we are dealing with the Interdata 8/32 for the moment.It is recognized that the left hand and right hand sides of the += operatorare addressable, and in particular the left hand side has noside effects, so it is permissible to rewrite this as.DS\fIa\fR = \fIa\fR + \fIb\fR.DEand this is done.No match is found on this tree either, so a machine dependent rewrite is done; it is recognizedthat the left hand side of the assignment is addressable, but the right hand side is notin a register, so.I orderis called recursively, being asked to put the righthand side of the assignment into a register.This invocation of.I ordersearches the tree for a match, and fails.The machine dependent rule for +notices that the right hand operand is addressable;it decides to put the left operand into a scratch register.Another recursive call to.I orderis made, with the treeconsisting solely of the leaf.I a ,and the cookie asking that the value be placed into a scratch register.This now matches a template, and a load instruction is emitted.The node consisting of.I ais rewritten in place to represent the register into which.I ais loaded,and this third call to.I orderreturns.The second call to.I ordernow finds that it has the tree.DS\fBreg\fR + \fIb\fR.DEto consider.Once again, there is no match, but the default rewriting rule rewritesthe + as a += operator, since the left operand is a scratch register.When this is done, there is a match: in fact,.DS\fBreg\fR += \fIb\fR.DEsimply describes the effect of the add instructionon a typical machine.After the add is emitted, the tree is rewrittento consist merely of the register node, since the result of the addis now in the register.This agrees with the cookie passed to the second invocation of.I order ,so this invocationterminates, returning to the first level.The original tree has nowbecome.DS\fIa\fR = \fBreg\fR.DEwhich matches a template for the store instruction.The store is output, and the tree rewritten to becomejust a single register node.At this point, since the top level call to.I orderwasinterested only in side effects, the call to.I orderreturns, and the code generation is completed;we have generated a load, add, and store, as might have been expected..PPThe effect of machine architecture on this is considerable.For example, on the Honeywell 6000, the machine dependent heuristics recognize that there is an ``add to storage''instruction, so the strategy is quite different;.I bis loaded in toa register, and then an add to storage instruction generatedto add this register in to.I a .The transformations, involving as they do the semantics of C,are largely machine independent.The decisions as to when to use them, however, arealmost totally machine dependent..PPHaving given a broad outline ofthe code generation process, we shall next consider theheart of it: the templates.This leads naturally into discussions of template matching and register allocation,and finally a discussion of the machine dependent interfaces and strategies..SHThe Templates.PPThe templates describe the effect of the target machine instructionson the model of computation around which the compiler is organized.In effect, each template has five logical sections, and represents an assertionof the form:.IP.B Ifwe have a subtree of a given shape (1), and we have a goal (cookie) or goals toachieve (2), and we have sufficient free resources (3),.B thenwe may emit an instruction or instructions (4), andrewrite the subtree in a particular manner (5),and the rewritten tree will achieve the desired goals..PPThese five sections will be discussed in moredetail later. First, we give an example of atemplate:.DS.ta 1i 2i 3i 4i 5iASG PLUS, INAREG, SAREG, TINT, SNAME, TINT, 0, RLEFT, " add AL,AR\en",.DEThe top line specifies the operator (+=) and the cookie (compute thevalue of the subtree into an AREG).The second and third lines specify the left and right descendants,respectively,of the += operator.The left descendant must be a REG node, representing anA register, and have integer type, while the right side must be a NAME node,and also have integer type.The fourth line contains the resource requirements (no scratch registersor temporaries needed), and the rewriting rule (replace the subtree by the left descendant).Finally, the quoted string on the last line represents the output to the assembler:lower case letters, tabs, spaces, etc. are copied.I verbatim .to the output; upper case letters trigger various macro-like expansions.Thus,.B ALwould expand into the \fBA\fRddress form of the \fBL\fReft operand \(empresumably the register number.Similarly,.B ARwould expand into the name of the right operand.The.I addinstruction of the last section might well beemitted by this template..PPIn principle, it would be possible to make separate templatesfor all legal combinations of operators, cookies, types, and shapes.In practice, the number of combinations is very large.Thus, a considerable amount of mechanism is present topermit a large number of subtrees to be matchedby a single template.Most of the shape and type specifiers are individual bits, and canbe logicallyor'edtogether.There are a number of special descriptors for matching classes ofoperators.The cookies can also be combined.As an example of the kind of templatethat really arises in practice, theactual template for the Interdata 8/32that subsumes the above example is:.DS.ta 1i 2i 3i 4i 5iASG OPSIMP, INAREG\(orFORCC, SAREG, TINT\(orTUNSIGNED\(orTPOINT, SAREG\(orSNAME\(orSOREG\(orSCON, TINT\(orTUNSIGNED\(orTPOINT, 0, RLEFT\(orRESCC, " OI AL,AR\en",.DEHere, OPSIMP represents the operators+, \-, \(or, &, and ^.The.B OImacro in the output string expands into theappropriate \fBI\fRnteger \fBO\fRpcode for the operator.The left and right sides can be integers, unsigned, or pointer types.The right side can be, in addition to a name, a register,a memory location whose address is given by a register and displacement (OREG),or a constant.Finally, these instructions set the condition codes,and so can be used in condition contexts:the cookie and rewriting rules reflect this..SHThe Template Matching Algorithm..PPThe heart of the second pass is the template matchingalgorithm, in the routine.I match ..I Matchis called with a tree and a cookie; it attempts to matchthe given tree against some template that will transform itaccording to one of the goals given in the cookie.If a match is successful, the transformation isapplied;.I expandis called to generate the assembly code, and then.I reclaimrewrites the tree, and reclaims the resources, suchas registers, that might have become free as a resultof the generated code..PPThis part of the compiler is among the most time critical.There is a spectrum of implementation techniques availablefor doing this matching.The most naive algorithm simply looks at the templates one by one.This can be considerably improved upon by restricting the searchfor an acceptable template.It would be possible to do better than this if the templates were givento a separate program that ate them and generated a templatematching subroutine.This would make maintenance of the compiler much morecomplicated, however, so this has not been done..PPThe matching algorithm is actually carried out by restrictingthe range in the table that must be searched for each opcode.This introduces a number of complications, however, and needs abit of sympathetic help by the person constructing thecompiler in order to obtain best results.The exact tuning of this algorithm continues; itis best to consult the code and comments in.I matchfor the latest version..PPIn order to match a template to a tree,it is necessary to match not only the cookie and theop of the root, but also the types and shapes of theleft and right descendants (if any) of the tree.A convention is established here that is carried out throughoutthe second pass of the compiler.If a node represents a unary operator, the single descendantis always the ``left'' descendant.If a node represents a unary operator or a leaf node (no descendants)the ``right'' descendant is taken by convention to be the node itself.This enables templates to easily match leaves and conversion operators, for example,without any additional mechanism in the matching program..PPThe type matching is straightforward; it is possible to specify any combinationof basic types, general pointers, and pointers to one or more ofthe basic types.The shape matching is somewhat more complicated, but still pretty simple.Templates have a collection of possible operand shapeson which the opcode might match.In the simplest case, an.I addoperation might be able to add to either a register variableor a scratch register, and might be able (with appropriatehelp from the assembler) to add an integer constant (ICON), a staticmemory cell (NAME), or a stack location (OREG)..PPIt is usually attractive to specify a number of such shapes,and distinguish between them when the assembler output is produced.It is possible to describe the union of many elementaryshapes such as ICON, NAME, OREG,AREG or BREG(both scratch and register forms), etc.To handle at least the simple forms of indirection, one can alsomatch some more complicated forms of trees; STARNM and STARREGcan match more complicated trees headed by an indirection operator,and SFLD can match certain trees headed by a FLD operator: thesepatterns call machine dependent routines that match thepatterns of interest on a given machine.The shape SWADD may be used to recognize NAME or OREGnodes that lie on word boundaries: this may be of some importanceon word\-addressed machines.Finally, there are some special shapes: these may notbe used in conjunction with the other shapes, but may bedefined and extended in machine dependent ways.The special shapes SZERO, SONE, and SMONE are predefined and matchconstants 0, 1, and \-1, respectively; others are easy to addand match by using the machine dependent routine.I special ..PPWhen a template has been found that matches the root of the tree,the cookie, and the shapes and types of the descendants,there is still one bar to a total match: the template maycall for some resources (for example, a scratch register).The routine.I allois called, and it attempts to allocate the resources.If it cannot, the match fails; no resources areallocated.If successful, the allocated resources are given numbers1, 2, etc. for later reference when the assembly code isgenerated.The routines.I expandand.I reclaimare then called.The.I matchroutine then returns a special value, MDONE.If no match was found, the value MNOPE is returned;this is a signal to the caller to try more cookievalues, or attempt a rewriting rule..I Matchis also used to select rewriting rules, althoughthe way of doing this is pretty straightforward.A special cookie, FORREW, is used to ask.I matchto search for a rewriting rule.The rewriting rules are keyed to various opcodes; mostare carried out in.I order .Since the question of when to rewrite is one of the key issues incode generation, it will be taken up again later..SHRegister Allocation..PPThe register allocation routines, and the allocation strategy,play a central role in the correctness of the code generation algorithm.If there are bugs in the Sethi-Ullman computation that cause thenumber of needed registers to be underestimated,the compiler may run out of scratch registers;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -