MEGAPATCH checkin.
[oota-llvm.git] / lib / Bytecode / Writer / InstructionWriter.cpp
1 //===-- WriteInst.cpp - Functions for writing instructions -------*- C++ -*--=//
2 //
3 // This file implements the routines for encoding instruction opcodes to a 
4 // bytecode stream.
5 //
6 // Note that the performance of this library is not terribly important, because
7 // it shouldn't be used by JIT type applications... so it is not a huge focus
8 // at least.  :)
9 //
10 //===----------------------------------------------------------------------===//
11
12 #include "WriterInternals.h"
13 #include "llvm/Module.h"
14 #include "llvm/Function.h"
15 #include "llvm/BasicBlock.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/iOther.h"
18 #include "llvm/iTerminators.h"
19 #include <algorithm>
20
21 typedef unsigned char uchar;
22
23 // outputInstructionFormat0 - Output those wierd instructions that have a large
24 // number of operands or have large operands themselves...
25 //
26 // Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>]
27 //
28 static void outputInstructionFormat0(const Instruction *I,
29                                      const SlotCalculator &Table,
30                                      unsigned Type, std::deque<uchar> &Out) {
31   // Opcode must have top two bits clear...
32   output_vbr(I->getOpcode() << 2, Out);          // Instruction Opcode ID
33   output_vbr(Type, Out);                         // Result type
34
35   unsigned NumArgs = I->getNumOperands();
36   output_vbr(NumArgs + isa<CastInst>(I), Out);
37
38   for (unsigned i = 0; i < NumArgs; ++i) {
39     int Slot = Table.getValSlot(I->getOperand(i));
40     assert(Slot >= 0 && "No slot number for value!?!?");      
41     output_vbr((unsigned)Slot, Out);
42   }
43
44   if (isa<CastInst>(I)) {
45     int Slot = Table.getValSlot(I->getType());
46     assert(Slot != -1 && "Cast return type unknown?");
47     output_vbr((unsigned)Slot, Out);
48   }
49
50   align32(Out);    // We must maintain correct alignment!
51 }
52
53
54 // outputInstrVarArgsCall - Output the obsurdly annoying varargs function calls.
55 // This are more annoying than most because the signature of the call does not
56 // tell us anything about the types of the arguments in the varargs portion.
57 // Because of this, we encode (as type 0) all of the argument types explicitly
58 // before the argument value.  This really sucks, but you shouldn't be using
59 // varargs functions in your code! *death to printf*!
60 //
61 // Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>]
62 //
63 static void outputInstrVarArgsCall(const Instruction *I,
64                                    const SlotCalculator &Table, unsigned Type,
65                                    std::deque<uchar> &Out) {
66   assert(isa<CallInst>(I) || isa<InvokeInst>(I));
67   // Opcode must have top two bits clear...
68   output_vbr(I->getOpcode() << 2, Out);          // Instruction Opcode ID
69   output_vbr(Type, Out);                         // Result type (varargs type)
70
71   unsigned NumArgs = I->getNumOperands();
72   output_vbr(NumArgs*2, Out);
73   // TODO: Don't need to emit types for the fixed types of the varargs function
74   // prototype...
75
76   // The type for the function has already been emitted in the type field of the
77   // instruction.  Just emit the slot # now.
78   int Slot = Table.getValSlot(I->getOperand(0));
79   assert(Slot >= 0 && "No slot number for value!?!?");      
80   output_vbr((unsigned)Slot, Out);
81
82   // Output a dummy field to fill Arg#2 in the reader that is currently unused
83   // for varargs calls.  This is a gross hack to make the code simpler, but we
84   // aren't really doing very small bytecode for varargs calls anyways.
85   // FIXME in the future: Smaller bytecode for varargs calls
86   output_vbr(0, Out);
87
88   for (unsigned i = 1; i < NumArgs; ++i) {
89     // Output Arg Type ID
90     Slot = Table.getValSlot(I->getOperand(i)->getType());
91     assert(Slot >= 0 && "No slot number for value!?!?");      
92     output_vbr((unsigned)Slot, Out);
93
94     // Output arg ID itself
95     Slot = Table.getValSlot(I->getOperand(i));
96     assert(Slot >= 0 && "No slot number for value!?!?");      
97     output_vbr((unsigned)Slot, Out);
98   }
99   align32(Out);    // We must maintain correct alignment!
100 }
101
102
103 // outputInstructionFormat1 - Output one operand instructions, knowing that no
104 // operand index is >= 2^12.
105 //
106 static void outputInstructionFormat1(const Instruction *I, 
107                                      const SlotCalculator &Table, int *Slots,
108                                      unsigned Type, std::deque<uchar> &Out) {
109   unsigned Opcode = I->getOpcode();      // Instruction Opcode ID
110   
111   // bits   Instruction format:
112   // --------------------------
113   // 01-00: Opcode type, fixed to 1.
114   // 07-02: Opcode
115   // 19-08: Resulting type plane
116   // 31-20: Operand #1 (if set to (2^12-1), then zero operands)
117   //
118   unsigned Bits = 1 | (Opcode << 2) | (Type << 8) | (Slots[0] << 20);
119   //  cerr << "1 " << IType << " " << Type << " " << Slots[0] << endl;
120   output(Bits, Out);
121 }
122
123
124 // outputInstructionFormat2 - Output two operand instructions, knowing that no
125 // operand index is >= 2^8.
126 //
127 static void outputInstructionFormat2(const Instruction *I, 
128                                      const SlotCalculator &Table, int *Slots,
129                                      unsigned Type, std::deque<uchar> &Out) {
130   unsigned Opcode = I->getOpcode();      // Instruction Opcode ID
131
132   // bits   Instruction format:
133   // --------------------------
134   // 01-00: Opcode type, fixed to 2.
135   // 07-02: Opcode
136   // 15-08: Resulting type plane
137   // 23-16: Operand #1
138   // 31-24: Operand #2  
139   //
140   unsigned Bits = 2 | (Opcode << 2) | (Type << 8) |
141                     (Slots[0] << 16) | (Slots[1] << 24);
142   //  cerr << "2 " << IType << " " << Type << " " << Slots[0] << " " 
143   //       << Slots[1] << endl;
144   output(Bits, Out);
145 }
146
147
148 // outputInstructionFormat3 - Output three operand instructions, knowing that no
149 // operand index is >= 2^6.
150 //
151 static void outputInstructionFormat3(const Instruction *I, 
152                                      const SlotCalculator &Table, int *Slots,
153                                      unsigned Type, std::deque<uchar> &Out) {
154   unsigned Opcode = I->getOpcode();      // Instruction Opcode ID
155
156   // bits   Instruction format:
157   // --------------------------
158   // 01-00: Opcode type, fixed to 3.
159   // 07-02: Opcode
160   // 13-08: Resulting type plane
161   // 19-14: Operand #1
162   // 25-20: Operand #2
163   // 31-26: Operand #3
164   //
165   unsigned Bits = 3 | (Opcode << 2) | (Type << 8) |
166           (Slots[0] << 14) | (Slots[1] << 20) | (Slots[2] << 26);
167   //cerr << "3 " << IType << " " << Type << " " << Slots[0] << " " 
168   //     << Slots[1] << " " << Slots[2] << endl;
169   output(Bits, Out);
170 }
171
172 void BytecodeWriter::processInstruction(const Instruction &I) {
173   assert(I.getOpcode() < 64 && "Opcode too big???");
174
175   unsigned NumOperands = I.getNumOperands();
176   int MaxOpSlot = 0;
177   int Slots[3]; Slots[0] = (1 << 12)-1;   // Marker to signify 0 operands
178
179   for (unsigned i = 0; i < NumOperands; ++i) {
180     const Value *Def = I.getOperand(i);
181     int slot = Table.getValSlot(Def);
182     assert(slot != -1 && "Broken bytecode!");
183     if (slot > MaxOpSlot) MaxOpSlot = slot;
184     if (i < 3) Slots[i] = slot;
185   }
186
187   // Figure out which type to encode with the instruction.  Typically we want
188   // the type of the first parameter, as opposed to the type of the instruction
189   // (for example, with setcc, we always know it returns bool, but the type of
190   // the first param is actually interesting).  But if we have no arguments
191   // we take the type of the instruction itself.  
192   //
193   const Type *Ty;
194   switch (I.getOpcode()) {
195   case Instruction::Malloc:
196   case Instruction::Alloca:
197     Ty = I.getType();  // Malloc & Alloca ALWAYS want to encode the return type
198     break;
199   case Instruction::Store:
200     Ty = I.getOperand(1)->getType();  // Encode the pointer type...
201     assert(isa<PointerType>(Ty) && "Store to nonpointer type!?!?");
202     break;
203   default:              // Otherwise use the default behavior...
204     Ty = NumOperands ? I.getOperand(0)->getType() : I.getType();
205     break;
206   }
207
208   unsigned Type;
209   int Slot = Table.getValSlot(Ty);
210   assert(Slot != -1 && "Type not available!!?!");
211   Type = (unsigned)Slot;
212
213   // Make sure that we take the type number into consideration.  We don't want
214   // to overflow the field size for the instruction format we select.
215   //
216   if (Slot > MaxOpSlot) MaxOpSlot = Slot;
217
218   // Handle the special case for cast...
219   if (isa<CastInst>(I)) {
220     // Cast has to encode the destination type as the second argument in the
221     // packet, or else we won't know what type to cast to!
222     Slots[1] = Table.getValSlot(I.getType());
223     assert(Slots[1] != -1 && "Cast return type unknown?");
224     if (Slots[1] > MaxOpSlot) MaxOpSlot = Slots[1];
225     NumOperands++;
226   } else if (const CallInst *CI = dyn_cast<CallInst>(&I)){// Handle VarArg calls
227     const PointerType *Ty = cast<PointerType>(CI->getCalledValue()->getType());
228     if (cast<FunctionType>(Ty->getElementType())->isVarArg()) {
229       outputInstrVarArgsCall(CI, Table, Type, Out);
230       return;
231     }
232   } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {// ...  & Invokes
233     const PointerType *Ty = cast<PointerType>(II->getCalledValue()->getType());
234     if (cast<FunctionType>(Ty->getElementType())->isVarArg()) {
235       outputInstrVarArgsCall(II, Table, Type, Out);
236       return;
237     }
238   }
239
240   // Decide which instruction encoding to use.  This is determined primarily by
241   // the number of operands, and secondarily by whether or not the max operand
242   // will fit into the instruction encoding.  More operands == fewer bits per
243   // operand.
244   //
245   switch (NumOperands) {
246   case 0:
247   case 1:
248     if (MaxOpSlot < (1 << 12)-1) { // -1 because we use 4095 to indicate 0 ops
249       outputInstructionFormat1(&I, Table, Slots, Type, Out);
250       return;
251     }
252     break;
253
254   case 2:
255     if (MaxOpSlot < (1 << 8)) {
256       outputInstructionFormat2(&I, Table, Slots, Type, Out);
257       return;
258     }
259     break;
260
261   case 3:
262     if (MaxOpSlot < (1 << 6)) {
263       outputInstructionFormat3(&I, Table, Slots, Type, Out);
264       return;
265     }
266     break;
267   }
268
269   // If we weren't handled before here, we either have a large number of
270   // operands or a large operand index that we are refering to.
271   outputInstructionFormat0(&I, Table, Type, Out);
272 }