blob: d5a2abf3fb3c679ebb3c89dc06164e6fc8453a4a [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- InstructionWriter.cpp - Functions for writing instructions --------===//
Chris Lattner00950542001-06-06 20:29:01 +00002//
3// This file implements the routines for encoding instruction opcodes to a
4// bytecode stream.
5//
Chris Lattner00950542001-06-06 20:29:01 +00006//===----------------------------------------------------------------------===//
7
8#include "WriterInternals.h"
9#include "llvm/Module.h"
Chris Lattner00950542001-06-06 20:29:01 +000010#include "llvm/DerivedTypes.h"
Chris Lattner0fe56f42003-09-08 17:58:37 +000011#include "llvm/Instructions.h"
Chris Lattnera92f6962002-10-01 22:38:41 +000012#include "Support/Statistic.h"
Chris Lattner00950542001-06-06 20:29:01 +000013#include <algorithm>
14
Chris Lattnerce6ef112002-07-26 18:40:14 +000015static Statistic<>
Chris Lattnera407ba12003-01-21 20:13:49 +000016NumInstrs("bytecodewriter", "Number of instructions");
Chris Lattnerce6ef112002-07-26 18:40:14 +000017
Chris Lattner00950542001-06-06 20:29:01 +000018typedef unsigned char uchar;
19
20// outputInstructionFormat0 - Output those wierd instructions that have a large
21// number of operands or have large operands themselves...
22//
23// Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>]
24//
Chris Lattner0fe56f42003-09-08 17:58:37 +000025static void outputInstructionFormat0(const Instruction *I, unsigned Opcode,
Chris Lattner00950542001-06-06 20:29:01 +000026 const SlotCalculator &Table,
Chris Lattner697954c2002-01-20 22:54:45 +000027 unsigned Type, std::deque<uchar> &Out) {
Chris Lattner00950542001-06-06 20:29:01 +000028 // Opcode must have top two bits clear...
Chris Lattner0fe56f42003-09-08 17:58:37 +000029 output_vbr(Opcode << 2, Out); // Instruction Opcode ID
Chris Lattner00950542001-06-06 20:29:01 +000030 output_vbr(Type, Out); // Result type
31
Chris Lattnerc8b25d42001-07-07 08:36:50 +000032 unsigned NumArgs = I->getNumOperands();
Chris Lattner8f77dae2003-05-08 02:44:12 +000033 output_vbr(NumArgs + (isa<CastInst>(I) || isa<VarArgInst>(I)), Out);
Chris Lattner00950542001-06-06 20:29:01 +000034
Chris Lattnerc8b25d42001-07-07 08:36:50 +000035 for (unsigned i = 0; i < NumArgs; ++i) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +000036 int Slot = Table.getSlot(I->getOperand(i));
Chris Lattnere5a57ee2001-07-25 22:47:55 +000037 assert(Slot >= 0 && "No slot number for value!?!?");
38 output_vbr((unsigned)Slot, Out);
39 }
Chris Lattner5ab1f872001-10-21 00:14:44 +000040
Chris Lattner8f77dae2003-05-08 02:44:12 +000041 if (isa<CastInst>(I) || isa<VarArgInst>(I)) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +000042 int Slot = Table.getSlot(I->getType());
Chris Lattner8f77dae2003-05-08 02:44:12 +000043 assert(Slot != -1 && "Cast/VarArg return type unknown?");
Chris Lattner5ab1f872001-10-21 00:14:44 +000044 output_vbr((unsigned)Slot, Out);
45 }
46
Chris Lattnere5a57ee2001-07-25 22:47:55 +000047 align32(Out); // We must maintain correct alignment!
48}
49
50
Misha Brukman37f92e22003-09-11 22:34:13 +000051// outputInstrVarArgsCall - Output the absurdly annoying varargs function calls.
Chris Lattnere5a57ee2001-07-25 22:47:55 +000052// This are more annoying than most because the signature of the call does not
53// tell us anything about the types of the arguments in the varargs portion.
54// Because of this, we encode (as type 0) all of the argument types explicitly
55// before the argument value. This really sucks, but you shouldn't be using
56// varargs functions in your code! *death to printf*!
57//
58// Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>]
59//
Chris Lattner0fe56f42003-09-08 17:58:37 +000060static void outputInstrVarArgsCall(const Instruction *I, unsigned Opcode,
Chris Lattnere5a57ee2001-07-25 22:47:55 +000061 const SlotCalculator &Table, unsigned Type,
Chris Lattner697954c2002-01-20 22:54:45 +000062 std::deque<uchar> &Out) {
Chris Lattner1b98c5c2001-10-13 06:48:38 +000063 assert(isa<CallInst>(I) || isa<InvokeInst>(I));
Chris Lattnere5a57ee2001-07-25 22:47:55 +000064 // Opcode must have top two bits clear...
Chris Lattner0fe56f42003-09-08 17:58:37 +000065 output_vbr(Opcode << 2, Out); // Instruction Opcode ID
Chris Lattnere5a57ee2001-07-25 22:47:55 +000066 output_vbr(Type, Out); // Result type (varargs type)
67
68 unsigned NumArgs = I->getNumOperands();
Chris Lattner1b98c5c2001-10-13 06:48:38 +000069 output_vbr(NumArgs*2, Out);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000070 // TODO: Don't need to emit types for the fixed types of the varargs function
Chris Lattner1b98c5c2001-10-13 06:48:38 +000071 // prototype...
Chris Lattnere5a57ee2001-07-25 22:47:55 +000072
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000073 // The type for the function has already been emitted in the type field of the
Chris Lattner1b98c5c2001-10-13 06:48:38 +000074 // instruction. Just emit the slot # now.
Alkis Evlogimenos60596382003-10-17 02:02:40 +000075 int Slot = Table.getSlot(I->getOperand(0));
Chris Lattnere5a57ee2001-07-25 22:47:55 +000076 assert(Slot >= 0 && "No slot number for value!?!?");
77 output_vbr((unsigned)Slot, Out);
78
Chris Lattner1b98c5c2001-10-13 06:48:38 +000079 // Output a dummy field to fill Arg#2 in the reader that is currently unused
80 // for varargs calls. This is a gross hack to make the code simpler, but we
81 // aren't really doing very small bytecode for varargs calls anyways.
82 // FIXME in the future: Smaller bytecode for varargs calls
83 output_vbr(0, Out);
Chris Lattnere5a57ee2001-07-25 22:47:55 +000084
Chris Lattner1b98c5c2001-10-13 06:48:38 +000085 for (unsigned i = 1; i < NumArgs; ++i) {
Chris Lattnere5a57ee2001-07-25 22:47:55 +000086 // Output Arg Type ID
Alkis Evlogimenos60596382003-10-17 02:02:40 +000087 Slot = Table.getSlot(I->getOperand(i)->getType());
Chris Lattnere5a57ee2001-07-25 22:47:55 +000088 assert(Slot >= 0 && "No slot number for value!?!?");
89 output_vbr((unsigned)Slot, Out);
90
91 // Output arg ID itself
Alkis Evlogimenos60596382003-10-17 02:02:40 +000092 Slot = Table.getSlot(I->getOperand(i));
Chris Lattnerc8b25d42001-07-07 08:36:50 +000093 assert(Slot >= 0 && "No slot number for value!?!?");
Chris Lattner00950542001-06-06 20:29:01 +000094 output_vbr((unsigned)Slot, Out);
95 }
96 align32(Out); // We must maintain correct alignment!
97}
98
99
100// outputInstructionFormat1 - Output one operand instructions, knowing that no
101// operand index is >= 2^12.
102//
Chris Lattner0fe56f42003-09-08 17:58:37 +0000103static void outputInstructionFormat1(const Instruction *I, unsigned Opcode,
Chris Lattner00950542001-06-06 20:29:01 +0000104 const SlotCalculator &Table, int *Slots,
Chris Lattner697954c2002-01-20 22:54:45 +0000105 unsigned Type, std::deque<uchar> &Out) {
Chris Lattner00950542001-06-06 20:29:01 +0000106 // bits Instruction format:
107 // --------------------------
Chris Lattner2b9f6002001-10-23 03:21:10 +0000108 // 01-00: Opcode type, fixed to 1.
109 // 07-02: Opcode
110 // 19-08: Resulting type plane
111 // 31-20: Operand #1 (if set to (2^12-1), then zero operands)
Chris Lattner00950542001-06-06 20:29:01 +0000112 //
Chris Lattner2b9f6002001-10-23 03:21:10 +0000113 unsigned Bits = 1 | (Opcode << 2) | (Type << 8) | (Slots[0] << 20);
Chris Lattner00950542001-06-06 20:29:01 +0000114 // cerr << "1 " << IType << " " << Type << " " << Slots[0] << endl;
Chris Lattner2b9f6002001-10-23 03:21:10 +0000115 output(Bits, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000116}
117
118
119// outputInstructionFormat2 - Output two operand instructions, knowing that no
120// operand index is >= 2^8.
121//
Chris Lattner0fe56f42003-09-08 17:58:37 +0000122static void outputInstructionFormat2(const Instruction *I, unsigned Opcode,
Chris Lattner00950542001-06-06 20:29:01 +0000123 const SlotCalculator &Table, int *Slots,
Chris Lattner697954c2002-01-20 22:54:45 +0000124 unsigned Type, std::deque<uchar> &Out) {
Chris Lattner00950542001-06-06 20:29:01 +0000125 // bits Instruction format:
126 // --------------------------
Chris Lattner2b9f6002001-10-23 03:21:10 +0000127 // 01-00: Opcode type, fixed to 2.
128 // 07-02: Opcode
129 // 15-08: Resulting type plane
130 // 23-16: Operand #1
131 // 31-24: Operand #2
Chris Lattner00950542001-06-06 20:29:01 +0000132 //
Chris Lattner2b9f6002001-10-23 03:21:10 +0000133 unsigned Bits = 2 | (Opcode << 2) | (Type << 8) |
134 (Slots[0] << 16) | (Slots[1] << 24);
Chris Lattner00950542001-06-06 20:29:01 +0000135 // cerr << "2 " << IType << " " << Type << " " << Slots[0] << " "
136 // << Slots[1] << endl;
Chris Lattner2b9f6002001-10-23 03:21:10 +0000137 output(Bits, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000138}
139
140
141// outputInstructionFormat3 - Output three operand instructions, knowing that no
142// operand index is >= 2^6.
143//
Chris Lattner0fe56f42003-09-08 17:58:37 +0000144static void outputInstructionFormat3(const Instruction *I, unsigned Opcode,
Chris Lattner00950542001-06-06 20:29:01 +0000145 const SlotCalculator &Table, int *Slots,
Chris Lattner697954c2002-01-20 22:54:45 +0000146 unsigned Type, std::deque<uchar> &Out) {
Chris Lattner00950542001-06-06 20:29:01 +0000147 // bits Instruction format:
148 // --------------------------
Chris Lattner2b9f6002001-10-23 03:21:10 +0000149 // 01-00: Opcode type, fixed to 3.
150 // 07-02: Opcode
151 // 13-08: Resulting type plane
152 // 19-14: Operand #1
153 // 25-20: Operand #2
154 // 31-26: Operand #3
Chris Lattner00950542001-06-06 20:29:01 +0000155 //
Chris Lattner2b9f6002001-10-23 03:21:10 +0000156 unsigned Bits = 3 | (Opcode << 2) | (Type << 8) |
157 (Slots[0] << 14) | (Slots[1] << 20) | (Slots[2] << 26);
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000158 //cerr << "3 " << IType << " " << Type << " " << Slots[0] << " "
159 // << Slots[1] << " " << Slots[2] << endl;
Chris Lattner2b9f6002001-10-23 03:21:10 +0000160 output(Bits, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000161}
162
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000163void BytecodeWriter::processInstruction(const Instruction &I) {
Chris Lattner0fe56f42003-09-08 17:58:37 +0000164 assert(I.getOpcode() < 62 && "Opcode too big???");
165 unsigned Opcode = I.getOpcode();
166
167 // Encode 'volatile load' as 62 and 'volatile store' as 63.
168 if (isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile())
169 Opcode = 62;
170 if (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile())
171 Opcode = 63;
Chris Lattner00950542001-06-06 20:29:01 +0000172
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000173 unsigned NumOperands = I.getNumOperands();
Chris Lattner00950542001-06-06 20:29:01 +0000174 int MaxOpSlot = 0;
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000175 int Slots[3]; Slots[0] = (1 << 12)-1; // Marker to signify 0 operands
Chris Lattner00950542001-06-06 20:29:01 +0000176
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000177 for (unsigned i = 0; i < NumOperands; ++i) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000178 const Value *Def = I.getOperand(i);
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000179 int slot = Table.getSlot(Def);
Chris Lattner00950542001-06-06 20:29:01 +0000180 assert(slot != -1 && "Broken bytecode!");
181 if (slot > MaxOpSlot) MaxOpSlot = slot;
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000182 if (i < 3) Slots[i] = slot;
Chris Lattner00950542001-06-06 20:29:01 +0000183 }
184
185 // Figure out which type to encode with the instruction. Typically we want
186 // the type of the first parameter, as opposed to the type of the instruction
187 // (for example, with setcc, we always know it returns bool, but the type of
188 // the first param is actually interesting). But if we have no arguments
189 // we take the type of the instruction itself.
190 //
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000191 const Type *Ty;
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000192 switch (I.getOpcode()) {
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000193 case Instruction::Malloc:
194 case Instruction::Alloca:
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000195 Ty = I.getType(); // Malloc & Alloca ALWAYS want to encode the return type
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000196 break;
197 case Instruction::Store:
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000198 Ty = I.getOperand(1)->getType(); // Encode the pointer type...
Chris Lattner9b625032002-05-06 16:15:30 +0000199 assert(isa<PointerType>(Ty) && "Store to nonpointer type!?!?");
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000200 break;
201 default: // Otherwise use the default behavior...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000202 Ty = NumOperands ? I.getOperand(0)->getType() : I.getType();
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000203 break;
204 }
Chris Lattner00950542001-06-06 20:29:01 +0000205
206 unsigned Type;
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000207 int Slot = Table.getSlot(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000208 assert(Slot != -1 && "Type not available!!?!");
209 Type = (unsigned)Slot;
210
Chris Lattner7c501472001-07-28 17:51:21 +0000211 // Make sure that we take the type number into consideration. We don't want
212 // to overflow the field size for the instruction format we select.
213 //
214 if (Slot > MaxOpSlot) MaxOpSlot = Slot;
215
Chris Lattner09083092001-07-08 04:57:15 +0000216 // Handle the special case for cast...
Chris Lattner8f77dae2003-05-08 02:44:12 +0000217 if (isa<CastInst>(I) || isa<VarArgInst>(I)) {
Chris Lattner09083092001-07-08 04:57:15 +0000218 // Cast has to encode the destination type as the second argument in the
219 // packet, or else we won't know what type to cast to!
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000220 Slots[1] = Table.getSlot(I.getType());
Chris Lattner09083092001-07-08 04:57:15 +0000221 assert(Slots[1] != -1 && "Cast return type unknown?");
222 if (Slots[1] > MaxOpSlot) MaxOpSlot = Slots[1];
223 NumOperands++;
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000224 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)){// Handle VarArg calls
Chris Lattner9fcccb02002-06-05 17:49:40 +0000225 const PointerType *Ty = cast<PointerType>(CI->getCalledValue()->getType());
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000226 if (cast<FunctionType>(Ty->getElementType())->isVarArg()) {
Chris Lattner0fe56f42003-09-08 17:58:37 +0000227 outputInstrVarArgsCall(CI, Opcode, Table, Type, Out);
Chris Lattner1b98c5c2001-10-13 06:48:38 +0000228 return;
229 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000230 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {// ... & Invokes
Chris Lattner9fcccb02002-06-05 17:49:40 +0000231 const PointerType *Ty = cast<PointerType>(II->getCalledValue()->getType());
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000232 if (cast<FunctionType>(Ty->getElementType())->isVarArg()) {
Chris Lattner0fe56f42003-09-08 17:58:37 +0000233 outputInstrVarArgsCall(II, Opcode, Table, Type, Out);
Chris Lattneref9c23f2001-10-03 14:53:21 +0000234 return;
235 }
Chris Lattner09083092001-07-08 04:57:15 +0000236 }
Chris Lattner00950542001-06-06 20:29:01 +0000237
Chris Lattnera407ba12003-01-21 20:13:49 +0000238 ++NumInstrs;
239
Chris Lattner00950542001-06-06 20:29:01 +0000240 // 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
Chris Lattner0fe56f42003-09-08 17:58:37 +0000249 outputInstructionFormat1(&I, Opcode, Table, Slots, Type, Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000250 return;
Chris Lattner00950542001-06-06 20:29:01 +0000251 }
252 break;
253
254 case 2:
255 if (MaxOpSlot < (1 << 8)) {
Chris Lattner0fe56f42003-09-08 17:58:37 +0000256 outputInstructionFormat2(&I, Opcode, Table, Slots, Type, Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000257 return;
Chris Lattner00950542001-06-06 20:29:01 +0000258 }
259 break;
260
261 case 3:
262 if (MaxOpSlot < (1 << 6)) {
Chris Lattner0fe56f42003-09-08 17:58:37 +0000263 outputInstructionFormat3(&I, Opcode, Table, Slots, Type, Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000264 return;
Chris Lattner00950542001-06-06 20:29:01 +0000265 }
266 break;
267 }
268
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000269 // If we weren't handled before here, we either have a large number of
Misha Brukman37f92e22003-09-11 22:34:13 +0000270 // operands or a large operand index that we are referring to.
Chris Lattner0fe56f42003-09-08 17:58:37 +0000271 outputInstructionFormat0(&I, Opcode, Table, Type, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000272}