blob: 64dc65e9d6930143960d412e5d8995d2e586a693 [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===-- 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"
Chris Lattner00950542001-06-06 20:29:01 +000014#include "llvm/DerivedTypes.h"
Chris Lattner0fe56f42003-09-08 17:58:37 +000015#include "llvm/Instructions.h"
Chris Lattnera92f6962002-10-01 22:38:41 +000016#include "Support/Statistic.h"
Chris Lattner00950542001-06-06 20:29:01 +000017#include <algorithm>
18
Chris Lattnerce6ef112002-07-26 18:40:14 +000019static Statistic<>
Chris Lattnera407ba12003-01-21 20:13:49 +000020NumInstrs("bytecodewriter", "Number of instructions");
Chris Lattnerce6ef112002-07-26 18:40:14 +000021
Chris Lattner00950542001-06-06 20:29:01 +000022typedef unsigned char uchar;
23
24// outputInstructionFormat0 - Output those wierd instructions that have a large
25// number of operands or have large operands themselves...
26//
27// Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>]
28//
Chris Lattner0fe56f42003-09-08 17:58:37 +000029static void outputInstructionFormat0(const Instruction *I, unsigned Opcode,
Chris Lattner00950542001-06-06 20:29:01 +000030 const SlotCalculator &Table,
Chris Lattner697954c2002-01-20 22:54:45 +000031 unsigned Type, std::deque<uchar> &Out) {
Chris Lattner00950542001-06-06 20:29:01 +000032 // Opcode must have top two bits clear...
Chris Lattner0fe56f42003-09-08 17:58:37 +000033 output_vbr(Opcode << 2, Out); // Instruction Opcode ID
Chris Lattner00950542001-06-06 20:29:01 +000034 output_vbr(Type, Out); // Result type
35
Chris Lattnerc8b25d42001-07-07 08:36:50 +000036 unsigned NumArgs = I->getNumOperands();
Chris Lattner8f77dae2003-05-08 02:44:12 +000037 output_vbr(NumArgs + (isa<CastInst>(I) || isa<VarArgInst>(I)), Out);
Chris Lattner00950542001-06-06 20:29:01 +000038
Chris Lattnerc8b25d42001-07-07 08:36:50 +000039 for (unsigned i = 0; i < NumArgs; ++i) {
Chris Lattnere5a57ee2001-07-25 22:47:55 +000040 int Slot = Table.getValSlot(I->getOperand(i));
41 assert(Slot >= 0 && "No slot number for value!?!?");
42 output_vbr((unsigned)Slot, Out);
43 }
Chris Lattner5ab1f872001-10-21 00:14:44 +000044
Chris Lattner8f77dae2003-05-08 02:44:12 +000045 if (isa<CastInst>(I) || isa<VarArgInst>(I)) {
Chris Lattner5ab1f872001-10-21 00:14:44 +000046 int Slot = Table.getValSlot(I->getType());
Chris Lattner8f77dae2003-05-08 02:44:12 +000047 assert(Slot != -1 && "Cast/VarArg return type unknown?");
Chris Lattner5ab1f872001-10-21 00:14:44 +000048 output_vbr((unsigned)Slot, Out);
49 }
50
Chris Lattnere5a57ee2001-07-25 22:47:55 +000051 align32(Out); // We must maintain correct alignment!
52}
53
54
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000055// outputInstrVarArgsCall - Output the obsurdly annoying varargs function calls.
Chris Lattnere5a57ee2001-07-25 22:47:55 +000056// This are more annoying than most because the signature of the call does not
57// tell us anything about the types of the arguments in the varargs portion.
58// Because of this, we encode (as type 0) all of the argument types explicitly
59// before the argument value. This really sucks, but you shouldn't be using
60// varargs functions in your code! *death to printf*!
61//
62// Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>]
63//
Chris Lattner0fe56f42003-09-08 17:58:37 +000064static void outputInstrVarArgsCall(const Instruction *I, unsigned Opcode,
Chris Lattnere5a57ee2001-07-25 22:47:55 +000065 const SlotCalculator &Table, unsigned Type,
Chris Lattner697954c2002-01-20 22:54:45 +000066 std::deque<uchar> &Out) {
Chris Lattner1b98c5c2001-10-13 06:48:38 +000067 assert(isa<CallInst>(I) || isa<InvokeInst>(I));
Chris Lattnere5a57ee2001-07-25 22:47:55 +000068 // Opcode must have top two bits clear...
Chris Lattner0fe56f42003-09-08 17:58:37 +000069 output_vbr(Opcode << 2, Out); // Instruction Opcode ID
Chris Lattnere5a57ee2001-07-25 22:47:55 +000070 output_vbr(Type, Out); // Result type (varargs type)
71
72 unsigned NumArgs = I->getNumOperands();
Chris Lattner1b98c5c2001-10-13 06:48:38 +000073 output_vbr(NumArgs*2, Out);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000074 // TODO: Don't need to emit types for the fixed types of the varargs function
Chris Lattner1b98c5c2001-10-13 06:48:38 +000075 // prototype...
Chris Lattnere5a57ee2001-07-25 22:47:55 +000076
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000077 // The type for the function has already been emitted in the type field of the
Chris Lattner1b98c5c2001-10-13 06:48:38 +000078 // instruction. Just emit the slot # now.
Chris Lattnere5a57ee2001-07-25 22:47:55 +000079 int Slot = Table.getValSlot(I->getOperand(0));
80 assert(Slot >= 0 && "No slot number for value!?!?");
81 output_vbr((unsigned)Slot, Out);
82
Chris Lattner1b98c5c2001-10-13 06:48:38 +000083 // Output a dummy field to fill Arg#2 in the reader that is currently unused
84 // for varargs calls. This is a gross hack to make the code simpler, but we
85 // aren't really doing very small bytecode for varargs calls anyways.
86 // FIXME in the future: Smaller bytecode for varargs calls
87 output_vbr(0, Out);
Chris Lattnere5a57ee2001-07-25 22:47:55 +000088
Chris Lattner1b98c5c2001-10-13 06:48:38 +000089 for (unsigned i = 1; i < NumArgs; ++i) {
Chris Lattnere5a57ee2001-07-25 22:47:55 +000090 // Output Arg Type ID
91 Slot = Table.getValSlot(I->getOperand(i)->getType());
92 assert(Slot >= 0 && "No slot number for value!?!?");
93 output_vbr((unsigned)Slot, Out);
94
95 // Output arg ID itself
96 Slot = Table.getValSlot(I->getOperand(i));
Chris Lattnerc8b25d42001-07-07 08:36:50 +000097 assert(Slot >= 0 && "No slot number for value!?!?");
Chris Lattner00950542001-06-06 20:29:01 +000098 output_vbr((unsigned)Slot, Out);
99 }
100 align32(Out); // We must maintain correct alignment!
101}
102
103
104// outputInstructionFormat1 - Output one operand instructions, knowing that no
105// operand index is >= 2^12.
106//
Chris Lattner0fe56f42003-09-08 17:58:37 +0000107static void outputInstructionFormat1(const Instruction *I, unsigned Opcode,
Chris Lattner00950542001-06-06 20:29:01 +0000108 const SlotCalculator &Table, int *Slots,
Chris Lattner697954c2002-01-20 22:54:45 +0000109 unsigned Type, std::deque<uchar> &Out) {
Chris Lattner00950542001-06-06 20:29:01 +0000110 // bits Instruction format:
111 // --------------------------
Chris Lattner2b9f6002001-10-23 03:21:10 +0000112 // 01-00: Opcode type, fixed to 1.
113 // 07-02: Opcode
114 // 19-08: Resulting type plane
115 // 31-20: Operand #1 (if set to (2^12-1), then zero operands)
Chris Lattner00950542001-06-06 20:29:01 +0000116 //
Chris Lattner2b9f6002001-10-23 03:21:10 +0000117 unsigned Bits = 1 | (Opcode << 2) | (Type << 8) | (Slots[0] << 20);
Chris Lattner00950542001-06-06 20:29:01 +0000118 // cerr << "1 " << IType << " " << Type << " " << Slots[0] << endl;
Chris Lattner2b9f6002001-10-23 03:21:10 +0000119 output(Bits, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000120}
121
122
123// outputInstructionFormat2 - Output two operand instructions, knowing that no
124// operand index is >= 2^8.
125//
Chris Lattner0fe56f42003-09-08 17:58:37 +0000126static void outputInstructionFormat2(const Instruction *I, unsigned Opcode,
Chris Lattner00950542001-06-06 20:29:01 +0000127 const SlotCalculator &Table, int *Slots,
Chris Lattner697954c2002-01-20 22:54:45 +0000128 unsigned Type, std::deque<uchar> &Out) {
Chris Lattner00950542001-06-06 20:29:01 +0000129 // bits Instruction format:
130 // --------------------------
Chris Lattner2b9f6002001-10-23 03:21:10 +0000131 // 01-00: Opcode type, fixed to 2.
132 // 07-02: Opcode
133 // 15-08: Resulting type plane
134 // 23-16: Operand #1
135 // 31-24: Operand #2
Chris Lattner00950542001-06-06 20:29:01 +0000136 //
Chris Lattner2b9f6002001-10-23 03:21:10 +0000137 unsigned Bits = 2 | (Opcode << 2) | (Type << 8) |
138 (Slots[0] << 16) | (Slots[1] << 24);
Chris Lattner00950542001-06-06 20:29:01 +0000139 // cerr << "2 " << IType << " " << Type << " " << Slots[0] << " "
140 // << Slots[1] << endl;
Chris Lattner2b9f6002001-10-23 03:21:10 +0000141 output(Bits, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000142}
143
144
145// outputInstructionFormat3 - Output three operand instructions, knowing that no
146// operand index is >= 2^6.
147//
Chris Lattner0fe56f42003-09-08 17:58:37 +0000148static void outputInstructionFormat3(const Instruction *I, unsigned Opcode,
Chris Lattner00950542001-06-06 20:29:01 +0000149 const SlotCalculator &Table, int *Slots,
Chris Lattner697954c2002-01-20 22:54:45 +0000150 unsigned Type, std::deque<uchar> &Out) {
Chris Lattner00950542001-06-06 20:29:01 +0000151 // bits Instruction format:
152 // --------------------------
Chris Lattner2b9f6002001-10-23 03:21:10 +0000153 // 01-00: Opcode type, fixed to 3.
154 // 07-02: Opcode
155 // 13-08: Resulting type plane
156 // 19-14: Operand #1
157 // 25-20: Operand #2
158 // 31-26: Operand #3
Chris Lattner00950542001-06-06 20:29:01 +0000159 //
Chris Lattner2b9f6002001-10-23 03:21:10 +0000160 unsigned Bits = 3 | (Opcode << 2) | (Type << 8) |
161 (Slots[0] << 14) | (Slots[1] << 20) | (Slots[2] << 26);
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000162 //cerr << "3 " << IType << " " << Type << " " << Slots[0] << " "
163 // << Slots[1] << " " << Slots[2] << endl;
Chris Lattner2b9f6002001-10-23 03:21:10 +0000164 output(Bits, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000165}
166
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000167void BytecodeWriter::processInstruction(const Instruction &I) {
Chris Lattner0fe56f42003-09-08 17:58:37 +0000168 assert(I.getOpcode() < 62 && "Opcode too big???");
169 unsigned Opcode = I.getOpcode();
170
171 // Encode 'volatile load' as 62 and 'volatile store' as 63.
172 if (isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile())
173 Opcode = 62;
174 if (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile())
175 Opcode = 63;
Chris Lattner00950542001-06-06 20:29:01 +0000176
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000177 unsigned NumOperands = I.getNumOperands();
Chris Lattner00950542001-06-06 20:29:01 +0000178 int MaxOpSlot = 0;
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000179 int Slots[3]; Slots[0] = (1 << 12)-1; // Marker to signify 0 operands
Chris Lattner00950542001-06-06 20:29:01 +0000180
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000181 for (unsigned i = 0; i < NumOperands; ++i) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000182 const Value *Def = I.getOperand(i);
Chris Lattner00950542001-06-06 20:29:01 +0000183 int slot = Table.getValSlot(Def);
184 assert(slot != -1 && "Broken bytecode!");
185 if (slot > MaxOpSlot) MaxOpSlot = slot;
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000186 if (i < 3) Slots[i] = slot;
Chris Lattner00950542001-06-06 20:29:01 +0000187 }
188
189 // Figure out which type to encode with the instruction. Typically we want
190 // the type of the first parameter, as opposed to the type of the instruction
191 // (for example, with setcc, we always know it returns bool, but the type of
192 // the first param is actually interesting). But if we have no arguments
193 // we take the type of the instruction itself.
194 //
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000195 const Type *Ty;
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000196 switch (I.getOpcode()) {
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000197 case Instruction::Malloc:
198 case Instruction::Alloca:
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000199 Ty = I.getType(); // Malloc & Alloca ALWAYS want to encode the return type
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000200 break;
201 case Instruction::Store:
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000202 Ty = I.getOperand(1)->getType(); // Encode the pointer type...
Chris Lattner9b625032002-05-06 16:15:30 +0000203 assert(isa<PointerType>(Ty) && "Store to nonpointer type!?!?");
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000204 break;
205 default: // Otherwise use the default behavior...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000206 Ty = NumOperands ? I.getOperand(0)->getType() : I.getType();
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000207 break;
208 }
Chris Lattner00950542001-06-06 20:29:01 +0000209
210 unsigned Type;
211 int Slot = Table.getValSlot(Ty);
212 assert(Slot != -1 && "Type not available!!?!");
213 Type = (unsigned)Slot;
214
Chris Lattner7c501472001-07-28 17:51:21 +0000215 // Make sure that we take the type number into consideration. We don't want
216 // to overflow the field size for the instruction format we select.
217 //
218 if (Slot > MaxOpSlot) MaxOpSlot = Slot;
219
Chris Lattner09083092001-07-08 04:57:15 +0000220 // Handle the special case for cast...
Chris Lattner8f77dae2003-05-08 02:44:12 +0000221 if (isa<CastInst>(I) || isa<VarArgInst>(I)) {
Chris Lattner09083092001-07-08 04:57:15 +0000222 // Cast has to encode the destination type as the second argument in the
223 // packet, or else we won't know what type to cast to!
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000224 Slots[1] = Table.getValSlot(I.getType());
Chris Lattner09083092001-07-08 04:57:15 +0000225 assert(Slots[1] != -1 && "Cast return type unknown?");
226 if (Slots[1] > MaxOpSlot) MaxOpSlot = Slots[1];
227 NumOperands++;
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000228 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)){// Handle VarArg calls
Chris Lattner9fcccb02002-06-05 17:49:40 +0000229 const PointerType *Ty = cast<PointerType>(CI->getCalledValue()->getType());
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000230 if (cast<FunctionType>(Ty->getElementType())->isVarArg()) {
Chris Lattner0fe56f42003-09-08 17:58:37 +0000231 outputInstrVarArgsCall(CI, Opcode, Table, Type, Out);
Chris Lattner1b98c5c2001-10-13 06:48:38 +0000232 return;
233 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000234 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {// ... & Invokes
Chris Lattner9fcccb02002-06-05 17:49:40 +0000235 const PointerType *Ty = cast<PointerType>(II->getCalledValue()->getType());
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000236 if (cast<FunctionType>(Ty->getElementType())->isVarArg()) {
Chris Lattner0fe56f42003-09-08 17:58:37 +0000237 outputInstrVarArgsCall(II, Opcode, Table, Type, Out);
Chris Lattneref9c23f2001-10-03 14:53:21 +0000238 return;
239 }
Chris Lattner09083092001-07-08 04:57:15 +0000240 }
Chris Lattner00950542001-06-06 20:29:01 +0000241
Chris Lattnera407ba12003-01-21 20:13:49 +0000242 ++NumInstrs;
243
Chris Lattner00950542001-06-06 20:29:01 +0000244 // Decide which instruction encoding to use. This is determined primarily by
245 // the number of operands, and secondarily by whether or not the max operand
246 // will fit into the instruction encoding. More operands == fewer bits per
247 // operand.
248 //
249 switch (NumOperands) {
250 case 0:
251 case 1:
252 if (MaxOpSlot < (1 << 12)-1) { // -1 because we use 4095 to indicate 0 ops
Chris Lattner0fe56f42003-09-08 17:58:37 +0000253 outputInstructionFormat1(&I, Opcode, Table, Slots, Type, Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000254 return;
Chris Lattner00950542001-06-06 20:29:01 +0000255 }
256 break;
257
258 case 2:
259 if (MaxOpSlot < (1 << 8)) {
Chris Lattner0fe56f42003-09-08 17:58:37 +0000260 outputInstructionFormat2(&I, Opcode, Table, Slots, Type, Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000261 return;
Chris Lattner00950542001-06-06 20:29:01 +0000262 }
263 break;
264
265 case 3:
266 if (MaxOpSlot < (1 << 6)) {
Chris Lattner0fe56f42003-09-08 17:58:37 +0000267 outputInstructionFormat3(&I, Opcode, Table, Slots, Type, Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000268 return;
Chris Lattner00950542001-06-06 20:29:01 +0000269 }
270 break;
271 }
272
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000273 // If we weren't handled before here, we either have a large number of
274 // operands or a large operand index that we are refering to.
Chris Lattner0fe56f42003-09-08 17:58:37 +0000275 outputInstructionFormat0(&I, Opcode, Table, Type, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000276}