1 //===-- WriteInst.cpp - Functions for writing instructions -------*- C++ -*--=//
3 // This file implements the routines for encoding instruction opcodes to a
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
10 //===----------------------------------------------------------------------===//
12 #include "WriterInternals.h"
13 #include "llvm/Module.h"
14 #include "llvm/DerivedTypes.h"
15 #include "llvm/iOther.h"
16 #include "llvm/iTerminators.h"
17 #include "Support/Statistic.h"
21 NumInstrs("bytecodewriter", "Number of instructions");
23 typedef unsigned char uchar;
25 // outputInstructionFormat0 - Output those wierd instructions that have a large
26 // number of operands or have large operands themselves...
28 // Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>]
30 static void outputInstructionFormat0(const Instruction *I,
31 const SlotCalculator &Table,
32 unsigned Type, std::deque<uchar> &Out) {
33 // Opcode must have top two bits clear...
34 output_vbr(I->getOpcode() << 2, Out); // Instruction Opcode ID
35 output_vbr(Type, Out); // Result type
37 unsigned NumArgs = I->getNumOperands();
38 output_vbr(NumArgs + isa<CastInst>(I), Out);
40 for (unsigned i = 0; i < NumArgs; ++i) {
41 int Slot = Table.getValSlot(I->getOperand(i));
42 assert(Slot >= 0 && "No slot number for value!?!?");
43 output_vbr((unsigned)Slot, Out);
46 if (isa<CastInst>(I)) {
47 int Slot = Table.getValSlot(I->getType());
48 assert(Slot != -1 && "Cast return type unknown?");
49 output_vbr((unsigned)Slot, Out);
52 align32(Out); // We must maintain correct alignment!
56 // outputInstrVarArgsCall - Output the obsurdly annoying varargs function calls.
57 // This are more annoying than most because the signature of the call does not
58 // tell us anything about the types of the arguments in the varargs portion.
59 // Because of this, we encode (as type 0) all of the argument types explicitly
60 // before the argument value. This really sucks, but you shouldn't be using
61 // varargs functions in your code! *death to printf*!
63 // Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>]
65 static void outputInstrVarArgsCall(const Instruction *I,
66 const SlotCalculator &Table, unsigned Type,
67 std::deque<uchar> &Out) {
68 assert(isa<CallInst>(I) || isa<InvokeInst>(I));
69 // Opcode must have top two bits clear...
70 output_vbr(I->getOpcode() << 2, Out); // Instruction Opcode ID
71 output_vbr(Type, Out); // Result type (varargs type)
73 unsigned NumArgs = I->getNumOperands();
74 output_vbr(NumArgs*2, Out);
75 // TODO: Don't need to emit types for the fixed types of the varargs function
78 // The type for the function has already been emitted in the type field of the
79 // instruction. Just emit the slot # now.
80 int Slot = Table.getValSlot(I->getOperand(0));
81 assert(Slot >= 0 && "No slot number for value!?!?");
82 output_vbr((unsigned)Slot, Out);
84 // Output a dummy field to fill Arg#2 in the reader that is currently unused
85 // for varargs calls. This is a gross hack to make the code simpler, but we
86 // aren't really doing very small bytecode for varargs calls anyways.
87 // FIXME in the future: Smaller bytecode for varargs calls
90 for (unsigned i = 1; i < NumArgs; ++i) {
92 Slot = Table.getValSlot(I->getOperand(i)->getType());
93 assert(Slot >= 0 && "No slot number for value!?!?");
94 output_vbr((unsigned)Slot, Out);
96 // Output arg ID itself
97 Slot = Table.getValSlot(I->getOperand(i));
98 assert(Slot >= 0 && "No slot number for value!?!?");
99 output_vbr((unsigned)Slot, Out);
101 align32(Out); // We must maintain correct alignment!
105 // outputInstructionFormat1 - Output one operand instructions, knowing that no
106 // operand index is >= 2^12.
108 static void outputInstructionFormat1(const Instruction *I,
109 const SlotCalculator &Table, int *Slots,
110 unsigned Type, std::deque<uchar> &Out) {
111 unsigned Opcode = I->getOpcode(); // Instruction Opcode ID
113 // bits Instruction format:
114 // --------------------------
115 // 01-00: Opcode type, fixed to 1.
117 // 19-08: Resulting type plane
118 // 31-20: Operand #1 (if set to (2^12-1), then zero operands)
120 unsigned Bits = 1 | (Opcode << 2) | (Type << 8) | (Slots[0] << 20);
121 // cerr << "1 " << IType << " " << Type << " " << Slots[0] << endl;
126 // outputInstructionFormat2 - Output two operand instructions, knowing that no
127 // operand index is >= 2^8.
129 static void outputInstructionFormat2(const Instruction *I,
130 const SlotCalculator &Table, int *Slots,
131 unsigned Type, std::deque<uchar> &Out) {
132 unsigned Opcode = I->getOpcode(); // Instruction Opcode ID
134 // bits Instruction format:
135 // --------------------------
136 // 01-00: Opcode type, fixed to 2.
138 // 15-08: Resulting type plane
142 unsigned Bits = 2 | (Opcode << 2) | (Type << 8) |
143 (Slots[0] << 16) | (Slots[1] << 24);
144 // cerr << "2 " << IType << " " << Type << " " << Slots[0] << " "
145 // << Slots[1] << endl;
150 // outputInstructionFormat3 - Output three operand instructions, knowing that no
151 // operand index is >= 2^6.
153 static void outputInstructionFormat3(const Instruction *I,
154 const SlotCalculator &Table, int *Slots,
155 unsigned Type, std::deque<uchar> &Out) {
156 unsigned Opcode = I->getOpcode(); // Instruction Opcode ID
158 // bits Instruction format:
159 // --------------------------
160 // 01-00: Opcode type, fixed to 3.
162 // 13-08: Resulting type plane
167 unsigned Bits = 3 | (Opcode << 2) | (Type << 8) |
168 (Slots[0] << 14) | (Slots[1] << 20) | (Slots[2] << 26);
169 //cerr << "3 " << IType << " " << Type << " " << Slots[0] << " "
170 // << Slots[1] << " " << Slots[2] << endl;
174 void BytecodeWriter::processInstruction(const Instruction &I) {
175 assert(I.getOpcode() < 64 && "Opcode too big???");
177 unsigned NumOperands = I.getNumOperands();
179 int Slots[3]; Slots[0] = (1 << 12)-1; // Marker to signify 0 operands
181 for (unsigned i = 0; i < NumOperands; ++i) {
182 const Value *Def = I.getOperand(i);
183 int slot = Table.getValSlot(Def);
184 assert(slot != -1 && "Broken bytecode!");
185 if (slot > MaxOpSlot) MaxOpSlot = slot;
186 if (i < 3) Slots[i] = slot;
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.
196 switch (I.getOpcode()) {
197 case Instruction::Malloc:
198 case Instruction::Alloca:
199 Ty = I.getType(); // Malloc & Alloca ALWAYS want to encode the return type
201 case Instruction::Store:
202 Ty = I.getOperand(1)->getType(); // Encode the pointer type...
203 assert(isa<PointerType>(Ty) && "Store to nonpointer type!?!?");
205 default: // Otherwise use the default behavior...
206 Ty = NumOperands ? I.getOperand(0)->getType() : I.getType();
211 int Slot = Table.getValSlot(Ty);
212 assert(Slot != -1 && "Type not available!!?!");
213 Type = (unsigned)Slot;
215 // 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.
218 if (Slot > MaxOpSlot) MaxOpSlot = Slot;
220 // Handle the special case for cast...
221 if (isa<CastInst>(I)) {
222 // 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!
224 Slots[1] = Table.getValSlot(I.getType());
225 assert(Slots[1] != -1 && "Cast return type unknown?");
226 if (Slots[1] > MaxOpSlot) MaxOpSlot = Slots[1];
228 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)){// Handle VarArg calls
229 const PointerType *Ty = cast<PointerType>(CI->getCalledValue()->getType());
230 if (cast<FunctionType>(Ty->getElementType())->isVarArg()) {
231 outputInstrVarArgsCall(CI, Table, Type, Out);
234 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {// ... & Invokes
235 const PointerType *Ty = cast<PointerType>(II->getCalledValue()->getType());
236 if (cast<FunctionType>(Ty->getElementType())->isVarArg()) {
237 outputInstrVarArgsCall(II, Table, Type, Out);
244 // 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
249 switch (NumOperands) {
252 if (MaxOpSlot < (1 << 12)-1) { // -1 because we use 4095 to indicate 0 ops
253 outputInstructionFormat1(&I, Table, Slots, Type, Out);
259 if (MaxOpSlot < (1 << 8)) {
260 outputInstructionFormat2(&I, Table, Slots, Type, Out);
266 if (MaxOpSlot < (1 << 6)) {
267 outputInstructionFormat3(&I, Table, Slots, Type, Out);
273 // 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.
275 outputInstructionFormat0(&I, Table, Type, Out);