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/Function.h"
15 #include "llvm/BasicBlock.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/iOther.h"
18 #include "llvm/iTerminators.h"
21 typedef unsigned char uchar;
23 // outputInstructionFormat0 - Output those wierd instructions that have a large
24 // number of operands or have large operands themselves...
26 // Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>]
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
35 unsigned NumArgs = I->getNumOperands();
36 output_vbr(NumArgs + isa<CastInst>(I), Out);
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);
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);
50 align32(Out); // We must maintain correct alignment!
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*!
61 // Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>]
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)
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
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);
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
88 for (unsigned i = 1; i < NumArgs; ++i) {
90 Slot = Table.getValSlot(I->getOperand(i)->getType());
91 assert(Slot >= 0 && "No slot number for value!?!?");
92 output_vbr((unsigned)Slot, Out);
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);
99 align32(Out); // We must maintain correct alignment!
103 // outputInstructionFormat1 - Output one operand instructions, knowing that no
104 // operand index is >= 2^12.
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
111 // bits Instruction format:
112 // --------------------------
113 // 01-00: Opcode type, fixed to 1.
115 // 19-08: Resulting type plane
116 // 31-20: Operand #1 (if set to (2^12-1), then zero operands)
118 unsigned Bits = 1 | (Opcode << 2) | (Type << 8) | (Slots[0] << 20);
119 // cerr << "1 " << IType << " " << Type << " " << Slots[0] << endl;
124 // outputInstructionFormat2 - Output two operand instructions, knowing that no
125 // operand index is >= 2^8.
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
132 // bits Instruction format:
133 // --------------------------
134 // 01-00: Opcode type, fixed to 2.
136 // 15-08: Resulting type plane
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;
148 // outputInstructionFormat3 - Output three operand instructions, knowing that no
149 // operand index is >= 2^6.
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
156 // bits Instruction format:
157 // --------------------------
158 // 01-00: Opcode type, fixed to 3.
160 // 13-08: Resulting type plane
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;
172 void BytecodeWriter::processInstruction(const Instruction &I) {
173 assert(I.getOpcode() < 64 && "Opcode too big???");
175 unsigned NumOperands = I.getNumOperands();
177 int Slots[3]; Slots[0] = (1 << 12)-1; // Marker to signify 0 operands
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;
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.
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
199 case Instruction::Store:
200 Ty = I.getOperand(1)->getType(); // Encode the pointer type...
201 assert(isa<PointerType>(Ty) && "Store to nonpointer type!?!?");
203 default: // Otherwise use the default behavior...
204 Ty = NumOperands ? I.getOperand(0)->getType() : I.getType();
209 int Slot = Table.getValSlot(Ty);
210 assert(Slot != -1 && "Type not available!!?!");
211 Type = (unsigned)Slot;
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.
216 if (Slot > MaxOpSlot) MaxOpSlot = Slot;
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];
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);
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);
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
245 switch (NumOperands) {
248 if (MaxOpSlot < (1 << 12)-1) { // -1 because we use 4095 to indicate 0 ops
249 outputInstructionFormat1(&I, Table, Slots, Type, Out);
255 if (MaxOpSlot < (1 << 8)) {
256 outputInstructionFormat2(&I, Table, Slots, Type, Out);
262 if (MaxOpSlot < (1 << 6)) {
263 outputInstructionFormat3(&I, Table, Slots, Type, Out);
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);