blob: f047ab5e1c566b0c29ec08e1f222401dd206cb43 [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"
14#include "llvm/Method.h"
15#include "llvm/BasicBlock.h"
16#include "llvm/Instruction.h"
17#include "llvm/DerivedTypes.h"
Chris Lattneref9c23f2001-10-03 14:53:21 +000018#include "llvm/iOther.h"
Chris Lattner1b98c5c2001-10-13 06:48:38 +000019#include "llvm/iTerminators.h"
Chris Lattner00950542001-06-06 20:29:01 +000020#include <algorithm>
21
22typedef 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//
29static void outputInstructionFormat0(const Instruction *I,
30 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 Lattner2b9f6002001-10-23 03:21:10 +000033 output_vbr(I->getOpcode() << 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 Lattner5ab1f872001-10-21 00:14:44 +000037 output_vbr(NumArgs + isa<CastInst>(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
45 if (isa<CastInst>(I)) {
46 int Slot = Table.getValSlot(I->getType());
47 assert(Slot != -1 && "Cast return type unknown?");
48 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
55// outputInstrVarArgsCall - Output the obsurdly annoying varargs method calls.
56// 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//
64static void outputInstrVarArgsCall(const Instruction *I,
65 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 Lattner2b9f6002001-10-23 03:21:10 +000069 output_vbr(I->getOpcode() << 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);
74 // TODO: Don't need to emit types for the fixed types of the varargs method
75 // prototype...
Chris Lattnere5a57ee2001-07-25 22:47:55 +000076
Chris Lattner1b98c5c2001-10-13 06:48:38 +000077 // The type for the method has already been emitted in the type field of the
78 // 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//
107static void outputInstructionFormat1(const Instruction *I,
108 const SlotCalculator &Table, int *Slots,
Chris Lattner697954c2002-01-20 22:54:45 +0000109 unsigned Type, std::deque<uchar> &Out) {
Chris Lattner2b9f6002001-10-23 03:21:10 +0000110 unsigned Opcode = I->getOpcode(); // Instruction Opcode ID
Chris Lattner00950542001-06-06 20:29:01 +0000111
112 // bits Instruction format:
113 // --------------------------
Chris Lattner2b9f6002001-10-23 03:21:10 +0000114 // 01-00: Opcode type, fixed to 1.
115 // 07-02: Opcode
116 // 19-08: Resulting type plane
117 // 31-20: Operand #1 (if set to (2^12-1), then zero operands)
Chris Lattner00950542001-06-06 20:29:01 +0000118 //
Chris Lattner2b9f6002001-10-23 03:21:10 +0000119 unsigned Bits = 1 | (Opcode << 2) | (Type << 8) | (Slots[0] << 20);
Chris Lattner00950542001-06-06 20:29:01 +0000120 // cerr << "1 " << IType << " " << Type << " " << Slots[0] << endl;
Chris Lattner2b9f6002001-10-23 03:21:10 +0000121 output(Bits, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000122}
123
124
125// outputInstructionFormat2 - Output two operand instructions, knowing that no
126// operand index is >= 2^8.
127//
128static void outputInstructionFormat2(const Instruction *I,
129 const SlotCalculator &Table, int *Slots,
Chris Lattner697954c2002-01-20 22:54:45 +0000130 unsigned Type, std::deque<uchar> &Out) {
Chris Lattner2b9f6002001-10-23 03:21:10 +0000131 unsigned Opcode = I->getOpcode(); // Instruction Opcode ID
Chris Lattner00950542001-06-06 20:29:01 +0000132
133 // bits Instruction format:
134 // --------------------------
Chris Lattner2b9f6002001-10-23 03:21:10 +0000135 // 01-00: Opcode type, fixed to 2.
136 // 07-02: Opcode
137 // 15-08: Resulting type plane
138 // 23-16: Operand #1
139 // 31-24: Operand #2
Chris Lattner00950542001-06-06 20:29:01 +0000140 //
Chris Lattner2b9f6002001-10-23 03:21:10 +0000141 unsigned Bits = 2 | (Opcode << 2) | (Type << 8) |
142 (Slots[0] << 16) | (Slots[1] << 24);
Chris Lattner00950542001-06-06 20:29:01 +0000143 // cerr << "2 " << IType << " " << Type << " " << Slots[0] << " "
144 // << Slots[1] << endl;
Chris Lattner2b9f6002001-10-23 03:21:10 +0000145 output(Bits, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000146}
147
148
149// outputInstructionFormat3 - Output three operand instructions, knowing that no
150// operand index is >= 2^6.
151//
152static void outputInstructionFormat3(const Instruction *I,
153 const SlotCalculator &Table, int *Slots,
Chris Lattner697954c2002-01-20 22:54:45 +0000154 unsigned Type, std::deque<uchar> &Out) {
Chris Lattner2b9f6002001-10-23 03:21:10 +0000155 unsigned Opcode = I->getOpcode(); // Instruction Opcode ID
Chris Lattner00950542001-06-06 20:29:01 +0000156
157 // bits Instruction format:
158 // --------------------------
Chris Lattner2b9f6002001-10-23 03:21:10 +0000159 // 01-00: Opcode type, fixed to 3.
160 // 07-02: Opcode
161 // 13-08: Resulting type plane
162 // 19-14: Operand #1
163 // 25-20: Operand #2
164 // 31-26: Operand #3
Chris Lattner00950542001-06-06 20:29:01 +0000165 //
Chris Lattner2b9f6002001-10-23 03:21:10 +0000166 unsigned Bits = 3 | (Opcode << 2) | (Type << 8) |
167 (Slots[0] << 14) | (Slots[1] << 20) | (Slots[2] << 26);
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000168 //cerr << "3 " << IType << " " << Type << " " << Slots[0] << " "
169 // << Slots[1] << " " << Slots[2] << endl;
Chris Lattner2b9f6002001-10-23 03:21:10 +0000170 output(Bits, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000171}
172
Chris Lattnere8fdde12001-09-07 16:39:41 +0000173void BytecodeWriter::processInstruction(const Instruction *I) {
Chris Lattnera41f50d2001-07-07 19:24:15 +0000174 assert(I->getOpcode() < 64 && "Opcode too big???");
Chris Lattner00950542001-06-06 20:29:01 +0000175
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000176 unsigned NumOperands = I->getNumOperands();
Chris Lattner00950542001-06-06 20:29:01 +0000177 int MaxOpSlot = 0;
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000178 int Slots[3]; Slots[0] = (1 << 12)-1; // Marker to signify 0 operands
Chris Lattner00950542001-06-06 20:29:01 +0000179
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000180 for (unsigned i = 0; i < NumOperands; ++i) {
181 const Value *Def = I->getOperand(i);
Chris Lattner00950542001-06-06 20:29:01 +0000182 int slot = Table.getValSlot(Def);
183 assert(slot != -1 && "Broken bytecode!");
184 if (slot > MaxOpSlot) MaxOpSlot = slot;
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000185 if (i < 3) Slots[i] = slot;
Chris Lattner00950542001-06-06 20:29:01 +0000186 }
187
188 // Figure out which type to encode with the instruction. Typically we want
189 // the type of the first parameter, as opposed to the type of the instruction
190 // (for example, with setcc, we always know it returns bool, but the type of
191 // the first param is actually interesting). But if we have no arguments
192 // we take the type of the instruction itself.
193 //
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000194 const Type *Ty;
195 switch (I->getOpcode()) {
196 case Instruction::Malloc:
197 case Instruction::Alloca:
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000198 Ty = I->getType(); // Malloc & Alloca ALWAYS want to encode the return type
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000199 break;
200 case Instruction::Store:
201 Ty = I->getOperand(1)->getType(); // Encode the pointer type...
Chris Lattner7c501472001-07-28 17:51:21 +0000202 assert(Ty->isPointerType() && "Store to nonpointer type!?!?");
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000203 break;
204 default: // Otherwise use the default behavior...
205 Ty = NumOperands ? I->getOperand(0)->getType() : I->getType();
206 break;
207 }
Chris Lattner00950542001-06-06 20:29:01 +0000208
209 unsigned Type;
210 int Slot = Table.getValSlot(Ty);
211 assert(Slot != -1 && "Type not available!!?!");
212 Type = (unsigned)Slot;
213
Chris Lattner7c501472001-07-28 17:51:21 +0000214 // Make sure that we take the type number into consideration. We don't want
215 // to overflow the field size for the instruction format we select.
216 //
217 if (Slot > MaxOpSlot) MaxOpSlot = Slot;
218
Chris Lattner09083092001-07-08 04:57:15 +0000219 // Handle the special case for cast...
Chris Lattner5ab1f872001-10-21 00:14:44 +0000220 if (isa<CastInst>(I)) {
Chris Lattner09083092001-07-08 04:57:15 +0000221 // Cast has to encode the destination type as the second argument in the
222 // packet, or else we won't know what type to cast to!
223 Slots[1] = Table.getValSlot(I->getType());
224 assert(Slots[1] != -1 && "Cast return type unknown?");
225 if (Slots[1] > MaxOpSlot) MaxOpSlot = Slots[1];
226 NumOperands++;
Chris Lattneref9c23f2001-10-03 14:53:21 +0000227 } else if (const CallInst *CI = dyn_cast<CallInst>(I)) {// Handle VarArg calls
Chris Lattner1b98c5c2001-10-13 06:48:38 +0000228 PointerType *Ty = cast<PointerType>(CI->getCalledValue()->getType());
Chris Lattner7a176752001-12-04 00:03:30 +0000229 if (cast<MethodType>(Ty->getElementType())->isVarArg()) {
Chris Lattner1b98c5c2001-10-13 06:48:38 +0000230 outputInstrVarArgsCall(I, Table, Type, Out);
231 return;
232 }
233 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(I)) { // ... & Invokes
234 PointerType *Ty = cast<PointerType>(II->getCalledValue()->getType());
Chris Lattner7a176752001-12-04 00:03:30 +0000235 if (cast<MethodType>(Ty->getElementType())->isVarArg()) {
Chris Lattneref9c23f2001-10-03 14:53:21 +0000236 outputInstrVarArgsCall(I, Table, Type, Out);
237 return;
238 }
Chris Lattner09083092001-07-08 04:57:15 +0000239 }
Chris Lattner00950542001-06-06 20:29:01 +0000240
241 // Decide which instruction encoding to use. This is determined primarily by
242 // the number of operands, and secondarily by whether or not the max operand
243 // will fit into the instruction encoding. More operands == fewer bits per
244 // operand.
245 //
246 switch (NumOperands) {
247 case 0:
248 case 1:
249 if (MaxOpSlot < (1 << 12)-1) { // -1 because we use 4095 to indicate 0 ops
250 outputInstructionFormat1(I, Table, Slots, Type, Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000251 return;
Chris Lattner00950542001-06-06 20:29:01 +0000252 }
253 break;
254
255 case 2:
256 if (MaxOpSlot < (1 << 8)) {
257 outputInstructionFormat2(I, Table, Slots, Type, Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000258 return;
Chris Lattner00950542001-06-06 20:29:01 +0000259 }
260 break;
261
262 case 3:
263 if (MaxOpSlot < (1 << 6)) {
264 outputInstructionFormat3(I, Table, Slots, Type, Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000265 return;
Chris Lattner00950542001-06-06 20:29:01 +0000266 }
267 break;
268 }
269
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000270 // If we weren't handled before here, we either have a large number of
271 // operands or a large operand index that we are refering to.
Chris Lattner00950542001-06-06 20:29:01 +0000272 outputInstructionFormat0(I, Table, Type, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000273}