blob: d8e17e25187ca067c8a58098169df4e86fb9426c [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 Lattner00950542001-06-06 20:29:01 +000019#include <algorithm>
20
21typedef unsigned char uchar;
22
23// outputInstructionFormat0 - Output those wierd instructions that have a large
24// number of operands or have large operands themselves...
25//
26// Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>]
27//
28static void outputInstructionFormat0(const Instruction *I,
29 const SlotCalculator &Table,
Chris Lattnere8fdde12001-09-07 16:39:41 +000030 unsigned Type, deque<uchar> &Out) {
Chris Lattner00950542001-06-06 20:29:01 +000031 // Opcode must have top two bits clear...
Chris Lattnera41f50d2001-07-07 19:24:15 +000032 output_vbr(I->getOpcode(), Out); // Instruction Opcode ID
Chris Lattner00950542001-06-06 20:29:01 +000033 output_vbr(Type, Out); // Result type
34
Chris Lattnerc8b25d42001-07-07 08:36:50 +000035 unsigned NumArgs = I->getNumOperands();
Chris Lattner00950542001-06-06 20:29:01 +000036 output_vbr(NumArgs, Out);
37
Chris Lattnerc8b25d42001-07-07 08:36:50 +000038 for (unsigned i = 0; i < NumArgs; ++i) {
Chris Lattnere5a57ee2001-07-25 22:47:55 +000039 int Slot = Table.getValSlot(I->getOperand(i));
40 assert(Slot >= 0 && "No slot number for value!?!?");
41 output_vbr((unsigned)Slot, Out);
42 }
43 align32(Out); // We must maintain correct alignment!
44}
45
46
47// outputInstrVarArgsCall - Output the obsurdly annoying varargs method calls.
48// This are more annoying than most because the signature of the call does not
49// tell us anything about the types of the arguments in the varargs portion.
50// Because of this, we encode (as type 0) all of the argument types explicitly
51// before the argument value. This really sucks, but you shouldn't be using
52// varargs functions in your code! *death to printf*!
53//
54// Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>]
55//
56static void outputInstrVarArgsCall(const Instruction *I,
57 const SlotCalculator &Table, unsigned Type,
Chris Lattnere8fdde12001-09-07 16:39:41 +000058 deque<uchar> &Out) {
Chris Lattnere5a57ee2001-07-25 22:47:55 +000059 assert(I->getOpcode() == Instruction::Call /*||
60 I->getOpcode() == Instruction::ICall */);
61 // Opcode must have top two bits clear...
62 output_vbr(I->getOpcode(), Out); // Instruction Opcode ID
63 output_vbr(Type, Out); // Result type (varargs type)
64
65 unsigned NumArgs = I->getNumOperands();
66 output_vbr((NumArgs-2)*2+2, Out); // Don't duplicate method & Arg1 types
67
68 // Output the method type without an extra type argument.
69 int Slot = Table.getValSlot(I->getOperand(0));
70 assert(Slot >= 0 && "No slot number for value!?!?");
71 output_vbr((unsigned)Slot, Out);
72
73 // VarArgs methods must have at least one specified operand
74 Slot = Table.getValSlot(I->getOperand(1));
75 assert(Slot >= 0 && "No slot number for value!?!?");
76 output_vbr((unsigned)Slot, Out);
77
78 for (unsigned i = 2; i < NumArgs; ++i) {
79 // Output Arg Type ID
80 Slot = Table.getValSlot(I->getOperand(i)->getType());
81 assert(Slot >= 0 && "No slot number for value!?!?");
82 output_vbr((unsigned)Slot, Out);
83
84 // Output arg ID itself
85 Slot = Table.getValSlot(I->getOperand(i));
Chris Lattnerc8b25d42001-07-07 08:36:50 +000086 assert(Slot >= 0 && "No slot number for value!?!?");
Chris Lattner00950542001-06-06 20:29:01 +000087 output_vbr((unsigned)Slot, Out);
88 }
89 align32(Out); // We must maintain correct alignment!
90}
91
92
93// outputInstructionFormat1 - Output one operand instructions, knowing that no
94// operand index is >= 2^12.
95//
96static void outputInstructionFormat1(const Instruction *I,
97 const SlotCalculator &Table, int *Slots,
Chris Lattnere8fdde12001-09-07 16:39:41 +000098 unsigned Type, deque<uchar> &Out) {
Chris Lattnera41f50d2001-07-07 19:24:15 +000099 unsigned IType = I->getOpcode(); // Instruction Opcode ID
Chris Lattner00950542001-06-06 20:29:01 +0000100
101 // bits Instruction format:
102 // --------------------------
103 // 31-30: Opcode type, fixed to 1.
104 // 29-24: Opcode
105 // 23-12: Resulting type plane
106 // 11- 0: Operand #1 (if set to (2^12-1), then zero operands)
107 //
108 unsigned Opcode = (1 << 30) | (IType << 24) | (Type << 12) | Slots[0];
109 // cerr << "1 " << IType << " " << Type << " " << Slots[0] << endl;
110 output(Opcode, Out);
111}
112
113
114// outputInstructionFormat2 - Output two operand instructions, knowing that no
115// operand index is >= 2^8.
116//
117static void outputInstructionFormat2(const Instruction *I,
118 const SlotCalculator &Table, int *Slots,
Chris Lattnere8fdde12001-09-07 16:39:41 +0000119 unsigned Type, deque<uchar> &Out) {
Chris Lattnera41f50d2001-07-07 19:24:15 +0000120 unsigned IType = I->getOpcode(); // Instruction Opcode ID
Chris Lattner00950542001-06-06 20:29:01 +0000121
122 // bits Instruction format:
123 // --------------------------
124 // 31-30: Opcode type, fixed to 2.
125 // 29-24: Opcode
126 // 23-16: Resulting type plane
127 // 15- 8: Operand #1
128 // 7- 0: Operand #2
129 //
130 unsigned Opcode = (2 << 30) | (IType << 24) | (Type << 16) |
131 (Slots[0] << 8) | (Slots[1] << 0);
132 // cerr << "2 " << IType << " " << Type << " " << Slots[0] << " "
133 // << Slots[1] << endl;
134 output(Opcode, Out);
135}
136
137
138// outputInstructionFormat3 - Output three operand instructions, knowing that no
139// operand index is >= 2^6.
140//
141static void outputInstructionFormat3(const Instruction *I,
142 const SlotCalculator &Table, int *Slots,
Chris Lattnere8fdde12001-09-07 16:39:41 +0000143 unsigned Type, deque<uchar> &Out) {
Chris Lattnera41f50d2001-07-07 19:24:15 +0000144 unsigned IType = I->getOpcode(); // Instruction Opcode ID
Chris Lattner00950542001-06-06 20:29:01 +0000145
146 // bits Instruction format:
147 // --------------------------
148 // 31-30: Opcode type, fixed to 3
149 // 29-24: Opcode
150 // 23-18: Resulting type plane
151 // 17-12: Operand #1
152 // 11- 6: Operand #2
153 // 5- 0: Operand #3
154 //
155 unsigned Opcode = (3 << 30) | (IType << 24) | (Type << 18) |
156 (Slots[0] << 12) | (Slots[1] << 6) | (Slots[2] << 0);
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000157 //cerr << "3 " << IType << " " << Type << " " << Slots[0] << " "
158 // << Slots[1] << " " << Slots[2] << endl;
Chris Lattner00950542001-06-06 20:29:01 +0000159 output(Opcode, Out);
160}
161
Chris Lattnere8fdde12001-09-07 16:39:41 +0000162#include "llvm/Assembly/Writer.h"
163
164void BytecodeWriter::processInstruction(const Instruction *I) {
Chris Lattnera41f50d2001-07-07 19:24:15 +0000165 assert(I->getOpcode() < 64 && "Opcode too big???");
Chris Lattner00950542001-06-06 20:29:01 +0000166
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000167 unsigned NumOperands = I->getNumOperands();
Chris Lattner00950542001-06-06 20:29:01 +0000168 int MaxOpSlot = 0;
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000169 int Slots[3]; Slots[0] = (1 << 12)-1; // Marker to signify 0 operands
Chris Lattner00950542001-06-06 20:29:01 +0000170
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000171 for (unsigned i = 0; i < NumOperands; ++i) {
172 const Value *Def = I->getOperand(i);
Chris Lattner00950542001-06-06 20:29:01 +0000173 int slot = Table.getValSlot(Def);
174 assert(slot != -1 && "Broken bytecode!");
175 if (slot > MaxOpSlot) MaxOpSlot = slot;
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000176 if (i < 3) Slots[i] = slot;
Chris Lattner00950542001-06-06 20:29:01 +0000177 }
178
179 // Figure out which type to encode with the instruction. Typically we want
180 // the type of the first parameter, as opposed to the type of the instruction
181 // (for example, with setcc, we always know it returns bool, but the type of
182 // the first param is actually interesting). But if we have no arguments
183 // we take the type of the instruction itself.
184 //
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000185 const Type *Ty;
186 switch (I->getOpcode()) {
187 case Instruction::Malloc:
188 case Instruction::Alloca:
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000189 Ty = I->getType(); // Malloc & Alloca ALWAYS want to encode the return type
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000190 break;
191 case Instruction::Store:
192 Ty = I->getOperand(1)->getType(); // Encode the pointer type...
Chris Lattner7c501472001-07-28 17:51:21 +0000193 assert(Ty->isPointerType() && "Store to nonpointer type!?!?");
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000194 break;
195 default: // Otherwise use the default behavior...
196 Ty = NumOperands ? I->getOperand(0)->getType() : I->getType();
197 break;
198 }
Chris Lattner00950542001-06-06 20:29:01 +0000199
200 unsigned Type;
201 int Slot = Table.getValSlot(Ty);
202 assert(Slot != -1 && "Type not available!!?!");
203 Type = (unsigned)Slot;
204
Chris Lattner7c501472001-07-28 17:51:21 +0000205 // Make sure that we take the type number into consideration. We don't want
206 // to overflow the field size for the instruction format we select.
207 //
208 if (Slot > MaxOpSlot) MaxOpSlot = Slot;
209
Chris Lattner09083092001-07-08 04:57:15 +0000210 // Handle the special case for cast...
211 if (I->getOpcode() == Instruction::Cast) {
212 // Cast has to encode the destination type as the second argument in the
213 // packet, or else we won't know what type to cast to!
214 Slots[1] = Table.getValSlot(I->getType());
215 assert(Slots[1] != -1 && "Cast return type unknown?");
216 if (Slots[1] > MaxOpSlot) MaxOpSlot = Slots[1];
217 NumOperands++;
Chris Lattneref9c23f2001-10-03 14:53:21 +0000218 } else if (const CallInst *CI = dyn_cast<CallInst>(I)) {// Handle VarArg calls
219 if (CI->getCalledMethod()->getMethodType()->isVarArg()) {
220 outputInstrVarArgsCall(I, Table, Type, Out);
221 return;
222 }
Chris Lattner09083092001-07-08 04:57:15 +0000223 }
Chris Lattner00950542001-06-06 20:29:01 +0000224
225 // Decide which instruction encoding to use. This is determined primarily by
226 // the number of operands, and secondarily by whether or not the max operand
227 // will fit into the instruction encoding. More operands == fewer bits per
228 // operand.
229 //
230 switch (NumOperands) {
231 case 0:
232 case 1:
233 if (MaxOpSlot < (1 << 12)-1) { // -1 because we use 4095 to indicate 0 ops
234 outputInstructionFormat1(I, Table, Slots, Type, Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000235 return;
Chris Lattner00950542001-06-06 20:29:01 +0000236 }
237 break;
238
239 case 2:
240 if (MaxOpSlot < (1 << 8)) {
241 outputInstructionFormat2(I, Table, Slots, Type, Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000242 return;
Chris Lattner00950542001-06-06 20:29:01 +0000243 }
244 break;
245
246 case 3:
247 if (MaxOpSlot < (1 << 6)) {
248 outputInstructionFormat3(I, Table, Slots, Type, Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000249 return;
Chris Lattner00950542001-06-06 20:29:01 +0000250 }
251 break;
252 }
253
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000254 // If we weren't handled before here, we either have a large number of
255 // operands or a large operand index that we are refering to.
Chris Lattner00950542001-06-06 20:29:01 +0000256 outputInstructionFormat0(I, Table, Type, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000257}