⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 porttour2

📁 unix v7是最后一个广泛发布的研究型UNIX版本
💻
📖 第 1 页 / 共 4 页
字号:
it is essential that the allocator keep track of those registers thatare free and busy, in order to detect such conditions..PPAllocation of registers takes place as the result of a templatematch; the routine.I allois called with a word describing the number of A registers,B registers, and temporary locations needed.The allocation of temporary locations on the stack is relativelystraightforward, and will not be further covered; thebookkeeping is a bit tricky, but conceptually trivial, and requestsfor temporary space on the stack will never fail..PPRegister allocation is less straightforward.The two major complications are.I pairingand.I sharing .In many machines, some operations (such as multiplicationand division), and/or some types (such as longs or double precision)require even/odd pairs of registers.Operations of the first type are exceptionally difficult todeal with in the compiler; in fact, their theoreticalproperties are rather bad as well..[Aho Johnson Ullman Multiregister.]The second issue is dealt with rather more successfully;a machine dependent function called.I szty(t)is called that returns 1 or 2, depending on thenumber of A registers required to hold an object of type.I t .If.I sztyreturns 2, an even/odd pair of A registers is allocatedfor each request..PPThe other issue, sharing, is more subtle, butimportant for good code quality.When registers are allocated, itis possible to reuse registers that hold addressinformation, and use them to contain the valuescomputed or accessed.For example, on the IBM 360, if register 2 hasa pointer to an integer in it, we may load theinteger into register 2 itself by saying:.DSL	2,0(2).DEIf register 2 had a byte pointer, however, the sequence forloading a character involves clearing the targetregister first, and then inserting the desired character:.DSSR	3,3IC	3,0(2).DEIn the first case, if register 3 were used as the target,it would lead to a larger number of registersused for the expression than were required; the compiler wouldgenerate inefficient code.On the other hand, if register 2 were used as the target in the secondcase, the code would simply be wrong.In the first case, register 2 can be .I sharedwhile in the second, it cannot..PPIn the specification of the register needs in the templates,it is possible to indicate whether required scratch registersmay be shared with possible registers on the left or the right of the input tree.In order that a register be shared, it must be scratch, and it mustbe used only once, on the appropriate side of the tree being compiled..PPThe.I alloroutine thus has a bit more to do than meets the eye;it calls.I freeregto obtain a free register for each A and B register request..I Freeregmakes multiple calls on the routine.I usableto decide if a given register can be used to satisfya given need..I Usablecalls.I shareitif the register is busy, but might be shared.Finally,.I shareitcalls.I ushareto decide if the desired register is actually in the appropriatesubtree, and can be shared..PPJust to add additional complexity, on some machines (such as the IBM 370) itis possible to have ``double indexing'' forms ofaddressing; these are represented by OREGS'swith the base and index registers encoded into the register field.While the register allocation and deallocation.I "per se"is not made more difficult by this phenomenon, the code itselfis somewhat more complex..PPHaving allocated the registers and expanded the assembly language,it is time to reclaim the resources; the routine.I reclaimdoes this.Many operations produce more than one result.For example, many arithmetic operations may producea value in a register, and also set the conditioncodes.Assignment operations may leave results both in a register and in memory..I Reclaimis passed three parameters; the tree and cookiethat were matched, and the rewriting field of the template.The rewriting field allows the specification of possible results;the tree is rewritten to reflect the results of the operation.If the tree was computed for side effects only (FOREFF),the tree is freed, and all resources in it reclaimed.If the tree was computed for condition codes, the resourcesare also freed, and the tree replaced by a specialnode type, FORCC.Otherwise, the value may be found in the leftargument of the root, the right argument of the root,or one of the temporary resources allocated.In these cases, first the resources of the tree,and the newly allocated resources,arefreed; then the resources needed by the resultare made busy again.The final result must always match the shape of the input cookie;otherwise, the compiler error``cannot reclaim''is generated.There are some machine dependent ways ofpreferring results in registers or memory whenthere are multiple results matching multiple goals in the cookie..SHThe Machine Dependent Interface.PPThe files.I order.c ,.I local2.c ,and.I table.c ,as well as the header file.I mac2defs ,represent the machine dependent portion of the second pass.The machine dependent portion can be roughly divided intotwo: the easy portion and the hard portion.The easy portiontells the compiler the names of the registers, and arranges thatthe compiler generate the proper assembler formats, opcode names, location counters, etc.The hard portion involves the Sethi\-Ullman computation, therewriting rules, and, to some extent, the templates.It is hard because there are no real algorithms that apply;most of this portion is based on heuristics.This section discusses the easy portion; the next severalsections will discuss the hard portion..PPIf the compiler is adapted from a compiler for a machineof similar architecture, the easy part is indeed easy.In.I mac2defs ,the register numbers are defined, as well as various parameters forthe stack frame, and various macros that describe the machine architecture.If double indexing is to be permitted, for example, the symbolR2REGS is defined.Also, a number of macros that are involved in function call processing,especially for unusual function call mechanisms, are defined here..PPIn.I local2.c ,a large number of simple functions are defined.These do things such as write out opcodes, register names,and address forms for the assembler.Part of the function call code is defined here; that is nontrivialto design, but typically rather straightforward to implement.Among the easy routines in.I order.care routines for generating a created label,defining a label, and generating the argumentsof a function call..PPThese routines tend to have a local effect, and depend on a fairly straightforward wayon the target assembler and the design decisions already made aboutthe compiler.Thus they will not be further treated here..SHThe Rewriting Rules.PPWhen a tree fails to match any template, it becomesa candidate for rewriting.Before the tree is rewritten,the machine dependent routine.I nextcookis called with the tree and the cookie; it suggestsanother cookie that might be a better candidate for thematching of the tree.If all else fails, the templates are searched with the cookieFORREW, to look for a rewriting rule.The rewriting rules are of two kinds;for most of the common operators, there aremachine dependent rewriting rules that may be applied;these are handled by machine dependent functionsthat are called and given the tree to be computed.These routines may recursively call.I orderor.I codgento cause certain subgoals to be achieved;if they actually call for some alteration of the tree,they return 1, and thecode generation algorithm recanonicalizes and tries again.If these routines choose not to deal with the tree, thedefault rewriting rules are applied..PPThe assignment ops, when rewritten, call the routine.I setasg .This is assumed to rewrite the tree at least to the point where there areno side effects in the left hand side.If there is still no template match,a default rewriting is done that causesan expression such as.DS.I "a += b".DEto be rewritten as.DS.I "a = a + b".DEThis is a useful default for certain mixtures of strange types(for example, when.I ais a bit field and.I ban character) thatotherwise might need separate table entries..PPSimple assignment, structure assignment, and all forms of callsare handled completely by the machine dependent routines.For historical reasons, the routines generating the calls return1 on failure, 0 on success, unlike the other routines..PPThe machine dependent routine.I setbinhandles binary operators; it too must do most of the job.In particular, when it returns 0, it must do so withthe left hand side in a temporary register.The default rewriting rule in this case is to convert thebinary operator into the associated assignment operator;since the left hand side is assumed to be a temporary register,this preserves the semantics and often allows a considerablesaving in the template table..PPThe increment and decrement operators may be dealt with withthe machine dependent routine.I setincr .If this routine chooses not to deal with the tree, the rewriting rule replaces.DS.I "x ++".DEby.DS.I "( (x += 1) \- 1)".DEwhich preserves the semantics.Once again, this is not too attractive for the most commoncases, but can generate close to optimal code when thetype of x is unusual..PPFinally, the indirection (UNARY MUL) operator is also handledin a special way.The machine dependent routine.I offstaris extremely important for the efficient generation of code..I Offstaris called with a tree that is the direct descendant of a UNARY MUL node;its job is to transform this tree so that the combination ofUNARY MUL with the transformed tree becomes addressable.On most machines,.I offstarcan simply compute the tree into an A or B register,depending on the architecture, and then.I canonwill make the resulting tree into an OREG.On many machines,.I offstarcan profitably choose to do less work than computingits entire argument into a register.For example, if the target machine supports OREGSwith a constant offset from a register, and.I offstaris calledwith a tree of the form.DS.I "expr + const".DEwhere.I constis a constant, then.I offstarneed only compute.I exprinto the appropriate form of register.On machines that support double indexing,.I offstarmay have even more choice as to how to proceed.The proper tuning of.I offstar ,which is not typically too difficult, should be one of thefirst tries at optimization attempted by thecompiler writer.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -