I am reading about code generation from the dragon book. It gives a naive method to associate a cost with each target language association.
We shall assume each target-language instruction has an associated cost. For simplicity, we take the cost of an instruction to be one plus the costs associated with the addressing modes of the operands. This cost corresponds to the length in words of the instruction. Addressing modes involving registers have zero additional cost, while those involving a memory location or constant in them have an additional cost of one, because such operands have to be stored in the words following the instruction.
Some examples:
I understood the cost calculation for the first 2 examples. I don't get the third one. How is the cost 3? Also I don't understand the bold part in the quoted text above.
Of what I partially understood I supposed the cost of BLTZ *R3,R0 to be 3 as it is so for the similar third example above. But the cost of this is 1. How?
Note BLTZ r, L causes a jump to label L if the value in register r is less than zero, and allows control to pass to the next machine instruction if not.
The instruction LD R1,*100(R2) loads into register R1 the value given by contents(contents(100+contents(R2))). The cost is three because the constant 100 is stored in the word following the instruction.
An instruction is a statement that is executed at runtime. An x86 instruction statement can consist of four parts: Label (optional) Instruction (required) Operands (instruction specific)
Each source statement may include up to four fields: a label, an operation (instruction mnemonic or assembler directive), an operand, and a comment. The following are examples of an assembly directive and a regular machine instruction.
Registers are the fastest computational unit on the target machine, but we usually do not have enough of them to hold all values. Values not held in registers need to reside in memory.
I don't get the third one. How is the cost 3?
cost of BLTZ *R3,R0 to be 3 as it is so for the similar third example above. But the cost of this is 1. How?
Because: Addressing modes involving registers have zero additional cost.
I don't understand the bold part in the quoted text above.
Apparently the machine code for this architecture uses a word to specify the opcode along with the register operands, and an additional word if needed for a constant.
For example LD R1,*100(R2)
takes 2 words. The first specifies the operation (LD
with a register-relative offset) along with the destination register R1
and the base register R2
. These are bitfields in the word. The second word then contains the 100
which the cpu knows to fetch because of the opcode in the first word.
Some fixed-length architectures pack constants into the first word, but then obviously they can only have limited range. Using a separate word allows for full range.
I am sure others could explain this better. But however, here are some inspirations:
The costs are NOT real, they are assumed by definition of the Dragon book. To get informed about the real costs, you have to check the tables of the target architecture. See the SO wiki for the corresponding links.
So coming to the three examples by relating to your quote above:
we take the cost of an instruction to be one
Therefore every instruction is assumed to have a cost of one!
those involving a memory location or constant in them have an additional cost of one
So the examples 1 to 3 have
The memory addresses and the constants are encoded in the instruction OpCode separately in the additional words of the instruction prolonging the, to be fetched, OpCode bytes affecting the CPU Op/microOp processing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With