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.
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.