1 //===-- InstructionWriter.cpp - Functions for writing instructions --------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the routines for encoding instruction opcodes to a
13 //===----------------------------------------------------------------------===//
15 #include "WriterInternals.h"
16 #include "llvm/Module.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/Instructions.h"
19 #include "Support/Statistic.h"
23 NumInstrs("bytecodewriter", "Number of instructions");
25 typedef unsigned char uchar;
27 // outputInstructionFormat0 - Output those wierd instructions that have a large
28 // number of operands or have large operands themselves...
30 // Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>]
32 static void outputInstructionFormat0(const Instruction *I, unsigned Opcode,
33 const SlotCalculator &Table,
34 unsigned Type, std::deque<uchar> &Out) {
35 // Opcode must have top two bits clear...
36 output_vbr(Opcode << 2, Out); // Instruction Opcode ID
37 output_vbr(Type, Out); // Result type
39 unsigned NumArgs = I->getNumOperands();
40 output_vbr(NumArgs + (isa<CastInst>(I) || isa<VANextInst>(I) ||
41 isa<VAArgInst>(I)), Out);
43 for (unsigned i = 0; i < NumArgs; ++i) {
44 int Slot = Table.getSlot(I->getOperand(i));
45 assert(Slot >= 0 && "No slot number for value!?!?");
46 output_vbr((unsigned)Slot, Out);
49 if (isa<CastInst>(I) || isa<VAArgInst>(I)) {
50 int Slot = Table.getSlot(I->getType());
51 assert(Slot != -1 && "Cast return type unknown?");
52 output_vbr((unsigned)Slot, Out);
53 } else if (const VANextInst *VAI = dyn_cast<VANextInst>(I)) {
54 int Slot = Table.getSlot(VAI->getArgType());
55 assert(Slot != -1 && "VarArg argument type unknown?");
56 output_vbr((unsigned)Slot, Out);
59 align32(Out); // We must maintain correct alignment!
63 // outputInstrVarArgsCall - Output the absurdly annoying varargs function calls.
64 // This are more annoying than most because the signature of the call does not
65 // tell us anything about the types of the arguments in the varargs portion.
66 // Because of this, we encode (as type 0) all of the argument types explicitly
67 // before the argument value. This really sucks, but you shouldn't be using
68 // varargs functions in your code! *death to printf*!
70 // Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>]
72 static void outputInstrVarArgsCall(const Instruction *I, unsigned Opcode,
73 const SlotCalculator &Table, unsigned Type,
74 std::deque<uchar> &Out) {
75 assert(isa<CallInst>(I) || isa<InvokeInst>(I));
76 // Opcode must have top two bits clear...
77 output_vbr(Opcode << 2, Out); // Instruction Opcode ID
78 output_vbr(Type, Out); // Result type (varargs type)
80 const PointerType *PTy = cast<PointerType>(I->getOperand(0)->getType());
81 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
82 unsigned NumParams = FTy->getNumParams();
84 unsigned NumFixedOperands;
85 if (isa<CallInst>(I)) {
86 // Output an operand for the callee and each fixed argument, then two for
87 // each variable argument.
88 NumFixedOperands = 1+NumParams;
90 assert(isa<InvokeInst>(I) && "Not call or invoke??");
91 // Output an operand for the callee and destinations, then two for each
93 NumFixedOperands = 3+NumParams;
95 output_vbr(2 * I->getNumOperands()-NumFixedOperands, Out);
97 // The type for the function has already been emitted in the type field of the
98 // instruction. Just emit the slot # now.
99 for (unsigned i = 0; i != NumFixedOperands; ++i) {
100 int Slot = Table.getSlot(I->getOperand(i));
101 assert(Slot >= 0 && "No slot number for value!?!?");
102 output_vbr((unsigned)Slot, Out);
105 for (unsigned i = NumFixedOperands, e = I->getNumOperands(); i != e; ++i) {
106 // Output Arg Type ID
107 int Slot = Table.getSlot(I->getOperand(i)->getType());
108 assert(Slot >= 0 && "No slot number for value!?!?");
109 output_vbr((unsigned)Slot, Out);
111 // Output arg ID itself
112 Slot = Table.getSlot(I->getOperand(i));
113 assert(Slot >= 0 && "No slot number for value!?!?");
114 output_vbr((unsigned)Slot, Out);
116 align32(Out); // We must maintain correct alignment!
120 // outputInstructionFormat1 - Output one operand instructions, knowing that no
121 // operand index is >= 2^12.
123 static void outputInstructionFormat1(const Instruction *I, unsigned Opcode,
124 const SlotCalculator &Table, int *Slots,
125 unsigned Type, std::deque<uchar> &Out) {
126 // bits Instruction format:
127 // --------------------------
128 // 01-00: Opcode type, fixed to 1.
130 // 19-08: Resulting type plane
131 // 31-20: Operand #1 (if set to (2^12-1), then zero operands)
133 unsigned Bits = 1 | (Opcode << 2) | (Type << 8) | (Slots[0] << 20);
134 // cerr << "1 " << IType << " " << Type << " " << Slots[0] << endl;
139 // outputInstructionFormat2 - Output two operand instructions, knowing that no
140 // operand index is >= 2^8.
142 static void outputInstructionFormat2(const Instruction *I, unsigned Opcode,
143 const SlotCalculator &Table, int *Slots,
144 unsigned Type, std::deque<uchar> &Out) {
145 // bits Instruction format:
146 // --------------------------
147 // 01-00: Opcode type, fixed to 2.
149 // 15-08: Resulting type plane
153 unsigned Bits = 2 | (Opcode << 2) | (Type << 8) |
154 (Slots[0] << 16) | (Slots[1] << 24);
155 // cerr << "2 " << IType << " " << Type << " " << Slots[0] << " "
156 // << Slots[1] << endl;
161 // outputInstructionFormat3 - Output three operand instructions, knowing that no
162 // operand index is >= 2^6.
164 static void outputInstructionFormat3(const Instruction *I, unsigned Opcode,
165 const SlotCalculator &Table, int *Slots,
166 unsigned Type, std::deque<uchar> &Out) {
167 // bits Instruction format:
168 // --------------------------
169 // 01-00: Opcode type, fixed to 3.
171 // 13-08: Resulting type plane
176 unsigned Bits = 3 | (Opcode << 2) | (Type << 8) |
177 (Slots[0] << 14) | (Slots[1] << 20) | (Slots[2] << 26);
178 //cerr << "3 " << IType << " " << Type << " " << Slots[0] << " "
179 // << Slots[1] << " " << Slots[2] << endl;
183 void BytecodeWriter::processInstruction(const Instruction &I) {
184 assert(I.getOpcode() < 62 && "Opcode too big???");
185 unsigned Opcode = I.getOpcode();
187 // Encode 'volatile load' as 62 and 'volatile store' as 63.
188 if (isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile())
190 if (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile())
193 unsigned NumOperands = I.getNumOperands();
195 int Slots[3]; Slots[0] = (1 << 12)-1; // Marker to signify 0 operands
197 for (unsigned i = 0; i < NumOperands; ++i) {
198 const Value *Def = I.getOperand(i);
199 int slot = Table.getSlot(Def);
200 assert(slot != -1 && "Broken bytecode!");
201 if (slot > MaxOpSlot) MaxOpSlot = slot;
202 if (i < 3) Slots[i] = slot;
205 // Figure out which type to encode with the instruction. Typically we want
206 // the type of the first parameter, as opposed to the type of the instruction
207 // (for example, with setcc, we always know it returns bool, but the type of
208 // the first param is actually interesting). But if we have no arguments
209 // we take the type of the instruction itself.
212 switch (I.getOpcode()) {
213 case Instruction::Malloc:
214 case Instruction::Alloca:
215 Ty = I.getType(); // Malloc & Alloca ALWAYS want to encode the return type
217 case Instruction::Store:
218 Ty = I.getOperand(1)->getType(); // Encode the pointer type...
219 assert(isa<PointerType>(Ty) && "Store to nonpointer type!?!?");
221 default: // Otherwise use the default behavior...
222 Ty = NumOperands ? I.getOperand(0)->getType() : I.getType();
227 int Slot = Table.getSlot(Ty);
228 assert(Slot != -1 && "Type not available!!?!");
229 Type = (unsigned)Slot;
231 // Make sure that we take the type number into consideration. We don't want
232 // to overflow the field size for the instruction format we select.
234 if (Slot > MaxOpSlot) MaxOpSlot = Slot;
236 // Handle the special case for cast...
237 if (isa<CastInst>(I) || isa<VAArgInst>(I)) {
238 // Cast has to encode the destination type as the second argument in the
239 // packet, or else we won't know what type to cast to!
240 Slots[1] = Table.getSlot(I.getType());
241 assert(Slots[1] != -1 && "Cast return type unknown?");
242 if (Slots[1] > MaxOpSlot) MaxOpSlot = Slots[1];
244 } else if (const VANextInst *VANI = dyn_cast<VANextInst>(&I)) {
245 Slots[1] = Table.getSlot(VANI->getArgType());
246 assert(Slots[1] != -1 && "va_next return type unknown?");
247 if (Slots[1] > MaxOpSlot) MaxOpSlot = Slots[1];
249 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)){// Handle VarArg calls
250 const PointerType *Ty = cast<PointerType>(CI->getCalledValue()->getType());
251 if (cast<FunctionType>(Ty->getElementType())->isVarArg()) {
252 outputInstrVarArgsCall(CI, Opcode, Table, Type, Out);
255 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {// ... & Invokes
256 const PointerType *Ty = cast<PointerType>(II->getCalledValue()->getType());
257 if (cast<FunctionType>(Ty->getElementType())->isVarArg()) {
258 outputInstrVarArgsCall(II, Opcode, Table, Type, Out);
265 // Decide which instruction encoding to use. This is determined primarily by
266 // the number of operands, and secondarily by whether or not the max operand
267 // will fit into the instruction encoding. More operands == fewer bits per
270 switch (NumOperands) {
273 if (MaxOpSlot < (1 << 12)-1) { // -1 because we use 4095 to indicate 0 ops
274 outputInstructionFormat1(&I, Opcode, Table, Slots, Type, Out);
280 if (MaxOpSlot < (1 << 8)) {
281 outputInstructionFormat2(&I, Opcode, Table, Slots, Type, Out);
287 if (MaxOpSlot < (1 << 6)) {
288 outputInstructionFormat3(&I, Opcode, Table, Slots, Type, Out);
294 // If we weren't handled before here, we either have a large number of
295 // operands or a large operand index that we are referring to.
296 outputInstructionFormat0(&I, Opcode, Table, Type, Out);