chapter 7 object code generation. chapter 7 -- object code generation2 statements in 3ac are simple...

19
Chapter 7 Object Code Generation Object Code Generation

Upload: lauren-johns

Post on 17-Jan-2016

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 7 Object Code Generation. Chapter 7 -- Object Code Generation2  Statements in 3AC are simple enough that it is usually no great problem to map

Chapter 7

Object Code GenerationObject Code Generation

Page 2: Chapter 7 Object Code Generation. Chapter 7 -- Object Code Generation2  Statements in 3AC are simple enough that it is usually no great problem to map

Chapter 7 -- Object Code Generation

2

Statements in 3AC are simple enough that it is Statements in 3AC are simple enough that it is usually no great problem to map them to usually no great problem to map them to corresponding sequences of machine-language corresponding sequences of machine-language instructions, if some care is taken.instructions, if some care is taken.

This is, of course, one of the great attractions This is, of course, one of the great attractions of 3ACof 3AC

The main issues in Object Code Generation: The main issues in Object Code Generation: How to avoid redundant Operations. How to avoid redundant Operations. Which Machine Instructions to use. Which Machine Instructions to use. How to manage Registers. How to manage Registers.

Page 3: Chapter 7 Object Code Generation. Chapter 7 -- Object Code Generation2  Statements in 3AC are simple enough that it is usually no great problem to map

Chapter 7 -- Object Code Generation

3

1. Generating Machine Language from 3AC This can be done blindly, instruction by This can be done blindly, instruction by

instruction, or it can be done with some instruction, or it can be done with some thought to the way successive instructions thought to the way successive instructions interact and especially to the intelligent use of interact and especially to the intelligent use of registers. registers. We will consider the blind method first, since it We will consider the blind method first, since it

is the simplest.is the simplest.

Page 4: Chapter 7 Object Code Generation. Chapter 7 -- Object Code Generation2  Statements in 3AC are simple enough that it is usually no great problem to map

Chapter 7 -- Object Code Generation

4

In machines where the number of registers is In machines where the number of registers is small or their uses are severely restricted, it small or their uses are severely restricted, it may not make a lot of sense to devote a lot of may not make a lot of sense to devote a lot of effort to optimizing register use.effort to optimizing register use.

Optimization implies a wide range of options Optimization implies a wide range of options to choose from, and if the optimizations are to choose from, and if the optimizations are limited, so is the amount of optimizing we can limited, so is the amount of optimizing we can do.do.

Page 5: Chapter 7 Object Code Generation. Chapter 7 -- Object Code Generation2  Statements in 3AC are simple enough that it is usually no great problem to map

Chapter 7 -- Object Code Generation

5

1.1 Blind Generation

You must take into account You must take into account The addressing modes of the operands. The addressing modes of the operands. result and register restrictions (pairs,…)result and register restrictions (pairs,…)

We could write a procedure, one for each type We could write a procedure, one for each type of 3AC, that handles the blind translation. of 3AC, that handles the blind translation.

These would ignore register allocation, and just These would ignore register allocation, and just do loads to get stuff in, and stores to put stuff do loads to get stuff in, and stores to put stuff back. back.

Page 6: Chapter 7 Object Code Generation. Chapter 7 -- Object Code Generation2  Statements in 3AC are simple enough that it is usually no great problem to map

Chapter 7 -- Object Code Generation

6

1.2 Special Considerations

The main issues are: The main issues are: taking advantage of special instructions, taking advantage of special instructions, deciding when to deviate from straight deciding when to deviate from straight

translation. translation. The use of special instructions arises because The use of special instructions arises because

there is often more than one way to do things. there is often more than one way to do things. this is where peephole optimization is good.this is where peephole optimization is good. procedures for repeated things. (calculating procedures for repeated things. (calculating

array subscripts) array subscripts)

Page 7: Chapter 7 Object Code Generation. Chapter 7 -- Object Code Generation2  Statements in 3AC are simple enough that it is usually no great problem to map

Chapter 7 -- Object Code Generation

7

2. Context-Sensitive Translation and Register Use The vast majority of computer instructions use The vast majority of computer instructions use

a working register to hold one of its operands. a working register to hold one of its operands. Since it takes time to copy from registers to Since it takes time to copy from registers to

memory, keep things in registers. memory, keep things in registers. The general rule for register usage is: If a value The general rule for register usage is: If a value

is in a register, and it is going to be used again is in a register, and it is going to be used again soon, keep it in a register. soon, keep it in a register.

The problem is when you run out of registers. The problem is when you run out of registers.

Page 8: Chapter 7 Object Code Generation. Chapter 7 -- Object Code Generation2  Statements in 3AC are simple enough that it is usually no great problem to map

Chapter 7 -- Object Code Generation

8

Therefore, we must keep track of: Therefore, we must keep track of: 1. Which registers are used, and what do they 1. Which registers are used, and what do they

hold. hold. 2. Where the current value of the variable can 2. Where the current value of the variable can

be found. be found. 3. Which variables will be needed later in the 3. Which variables will be needed later in the

block, and where. block, and where. 4. Which variables whose current values are in 4. Which variables whose current values are in

registers must be stored upon exiting registers must be stored upon exiting the block (these are live variables) the block (these are live variables)

Page 9: Chapter 7 Object Code Generation. Chapter 7 -- Object Code Generation2  Statements in 3AC are simple enough that it is usually no great problem to map

Chapter 7 -- Object Code Generation

9

Since programs may have hundreds of Since programs may have hundreds of variables (including temporaries created in variables (including temporaries created in code generation) this job can easily get out of code generation) this job can easily get out of hand.hand.

Page 10: Chapter 7 Object Code Generation. Chapter 7 -- Object Code Generation2  Statements in 3AC are simple enough that it is usually no great problem to map

Chapter 7 -- Object Code Generation

10

2.1 Livens and Next Use

a variable is live if it is going to be used again a variable is live if it is going to be used again in the program. in the program.

programmer defined variables can be assumed programmer defined variables can be assumed to be live at the end of the block. to be live at the end of the block.

temporaries are assumed to not be live at the temporaries are assumed to not be live at the end of a block. end of a block.

Page 11: Chapter 7 Object Code Generation. Chapter 7 -- Object Code Generation2  Statements in 3AC are simple enough that it is usually no great problem to map

Chapter 7 -- Object Code Generation

11

2.2 Descriptor Tables

register allocation tableregister allocation table -- current contents of -- current contents of each register (every use of a register updates each register (every use of a register updates this table).this table).

address tableaddress table -- where the current value of -- where the current value of each variable may be found (memory, register, each variable may be found (memory, register, both) both)

Page 12: Chapter 7 Object Code Generation. Chapter 7 -- Object Code Generation2  Statements in 3AC are simple enough that it is usually no great problem to map

Chapter 7 -- Object Code Generation

12

2.3 Assigning Registers

int Get_Register(char *, int & new);int Get_Register(char *, int & new);

Pass it the operand Pass it the operand 1. Find out if the parameter is already in a 1. Find out if the parameter is already in a

register. If so return it. register. If so return it. 2. If not, find an empty register and fill it in the 2. If not, find an empty register and fill it in the

register table and set new to true. register table and set new to true. 3. If there are no empty registers, spill. 3. If there are no empty registers, spill.

Page 13: Chapter 7 Object Code Generation. Chapter 7 -- Object Code Generation2  Statements in 3AC are simple enough that it is usually no great problem to map

Chapter 7 -- Object Code Generation

13

2.4 Generating Code

a := b op ca := b op c 1. R = Get_Register (B, new); 1. R = Get_Register (B, new); 2. if(new) 2. if(new)

L R, B // Load register R with BL R, B // Load register R with B 3. Check address table for C 3. Check address table for C

if memoryif memory op R, Cop R, C

if register Sif register S opR R,SopR R,S

Page 14: Chapter 7 Object Code Generation. Chapter 7 -- Object Code Generation2  Statements in 3AC are simple enough that it is usually no great problem to map

Chapter 7 -- Object Code Generation

14

Don't forget to free temporaries from registers Don't forget to free temporaries from registers after they are used. after they are used. This may require a second function to free the This may require a second function to free the

register of some variable.register of some variable.free_register (char *name);free_register (char *name);

Note: Get_Register may also have to worry Note: Get_Register may also have to worry about even/odd register pairs in some about even/odd register pairs in some architectures. architectures.

Page 15: Chapter 7 Object Code Generation. Chapter 7 -- Object Code Generation2  Statements in 3AC are simple enough that it is usually no great problem to map

Chapter 7 -- Object Code Generation

15

2.5 Instruction Sequencing

Aho, Sethi, and Ullman [1970] devised a Aho, Sethi, and Ullman [1970] devised a method of re-ordering the instructions to method of re-ordering the instructions to evaluate the basic blocks that require the most evaluate the basic blocks that require the most registers first.registers first.

This can save on your register use, thereby This can save on your register use, thereby minimizing spilling of registers.minimizing spilling of registers.

Page 16: Chapter 7 Object Code Generation. Chapter 7 -- Object Code Generation2  Statements in 3AC are simple enough that it is usually no great problem to map

Chapter 7 -- Object Code Generation

16

3. Special Architectural Features

The code generator should be able to take The code generator should be able to take advantage of any special capabilities provided advantage of any special capabilities provided by the target machine. by the target machine. The PD.-11 provided auto-increment and auto-The PD.-11 provided auto-increment and auto-

decrement registers. You could set them to decrement registers. You could set them to auto-increment before or after they gave you auto-increment before or after they gave you the value.the value.

++i or i++++i or i++

Page 17: Chapter 7 Object Code Generation. Chapter 7 -- Object Code Generation2  Statements in 3AC are simple enough that it is usually no great problem to map

Chapter 7 -- Object Code Generation

17

There is little to say about exotic instructions, There is little to say about exotic instructions, since it depends on what the instruction is, and since it depends on what the instruction is, and what you consider exotic.what you consider exotic.

One can generally say, the more exotic the One can generally say, the more exotic the instruction, the less use it is likely to be. instruction, the less use it is likely to be.

It may be more trouble than it is worth to It may be more trouble than it is worth to detect the opportunity to use some unusual detect the opportunity to use some unusual instruction unless it saves a truly huge number instruction unless it saves a truly huge number of conventional instructions.of conventional instructions.

Page 18: Chapter 7 Object Code Generation. Chapter 7 -- Object Code Generation2  Statements in 3AC are simple enough that it is usually no great problem to map

Chapter 7 -- Object Code Generation

18

4. Summary

Object code generation takes as many forms as Object code generation takes as many forms as there are target machines.there are target machines.

Some programming languages have been Some programming languages have been influenced by the instruction sets of the influenced by the instruction sets of the machines on which they were first developedmachines on which they were first developed C and i++C and i++ FORTRAN and if(x) 10, 20, 30FORTRAN and if(x) 10, 20, 30

reflects the comparison and conditional-jump reflects the comparison and conditional-jump operations of the original target machine.operations of the original target machine.

Page 19: Chapter 7 Object Code Generation. Chapter 7 -- Object Code Generation2  Statements in 3AC are simple enough that it is usually no great problem to map

Chapter 7 -- Object Code Generation

19

You can learn a great deal by using an You can learn a great deal by using an interactive debugger to analyze and study the interactive debugger to analyze and study the object code generated by other compilers for object code generated by other compilers for the same machine.the same machine. You may spot some things the compiler does You may spot some things the compiler does

that are stupid.that are stupid. It may also alert you to problems you can It may also alert you to problems you can

avoid.avoid.