blob: 3ab882fa7b96f22a1b4d8e985f265b5d8e56b3fd [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 Lattner00950542001-06-06 20:29:01 +000018#include <algorithm>
19
20typedef unsigned char uchar;
21
22// outputInstructionFormat0 - Output those wierd instructions that have a large
23// number of operands or have large operands themselves...
24//
25// Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>]
26//
27static void outputInstructionFormat0(const Instruction *I,
28 const SlotCalculator &Table,
29 unsigned Type, vector<uchar> &Out) {
30 // Opcode must have top two bits clear...
Chris Lattnera41f50d2001-07-07 19:24:15 +000031 output_vbr(I->getOpcode(), Out); // Instruction Opcode ID
Chris Lattner00950542001-06-06 20:29:01 +000032 output_vbr(Type, Out); // Result type
33
Chris Lattnerc8b25d42001-07-07 08:36:50 +000034 unsigned NumArgs = I->getNumOperands();
Chris Lattner00950542001-06-06 20:29:01 +000035 output_vbr(NumArgs, Out);
36
Chris Lattnerc8b25d42001-07-07 08:36:50 +000037 for (unsigned i = 0; i < NumArgs; ++i) {
Chris Lattnere5a57ee2001-07-25 22:47:55 +000038 int Slot = Table.getValSlot(I->getOperand(i));
39 assert(Slot >= 0 && "No slot number for value!?!?");
40 output_vbr((unsigned)Slot, Out);
41 }
42 align32(Out); // We must maintain correct alignment!
43}
44
45
46// outputInstrVarArgsCall - Output the obsurdly annoying varargs method calls.
47// This are more annoying than most because the signature of the call does not
48// tell us anything about the types of the arguments in the varargs portion.
49// Because of this, we encode (as type 0) all of the argument types explicitly
50// before the argument value. This really sucks, but you shouldn't be using
51// varargs functions in your code! *death to printf*!
52//
53// Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>]
54//
55static void outputInstrVarArgsCall(const Instruction *I,
56 const SlotCalculator &Table, unsigned Type,
57 vector<uchar> &Out) {
58 assert(I->getOpcode() == Instruction::Call /*||
59 I->getOpcode() == Instruction::ICall */);
60 // Opcode must have top two bits clear...
61 output_vbr(I->getOpcode(), Out); // Instruction Opcode ID
62 output_vbr(Type, Out); // Result type (varargs type)
63
64 unsigned NumArgs = I->getNumOperands();
65 output_vbr((NumArgs-2)*2+2, Out); // Don't duplicate method & Arg1 types
66
67 // Output the method type without an extra type argument.
68 int Slot = Table.getValSlot(I->getOperand(0));
69 assert(Slot >= 0 && "No slot number for value!?!?");
70 output_vbr((unsigned)Slot, Out);
71
72 // VarArgs methods must have at least one specified operand
73 Slot = Table.getValSlot(I->getOperand(1));
74 assert(Slot >= 0 && "No slot number for value!?!?");
75 output_vbr((unsigned)Slot, Out);
76
77 for (unsigned i = 2; i < NumArgs; ++i) {
78 // Output Arg Type ID
79 Slot = Table.getValSlot(I->getOperand(i)->getType());
80 assert(Slot >= 0 && "No slot number for value!?!?");
81 output_vbr((unsigned)Slot, Out);
82
83 // Output arg ID itself
84 Slot = Table.getValSlot(I->getOperand(i));
Chris Lattnerc8b25d42001-07-07 08:36:50 +000085 assert(Slot >= 0 && "No slot number for value!?!?");
Chris Lattner00950542001-06-06 20:29:01 +000086 output_vbr((unsigned)Slot, Out);
87 }
88 align32(Out); // We must maintain correct alignment!
89}
90
91
92// outputInstructionFormat1 - Output one operand instructions, knowing that no
93// operand index is >= 2^12.
94//
95static void outputInstructionFormat1(const Instruction *I,
96 const SlotCalculator &Table, int *Slots,
97 unsigned Type, vector<uchar> &Out) {
Chris Lattnera41f50d2001-07-07 19:24:15 +000098 unsigned IType = I->getOpcode(); // Instruction Opcode ID
Chris Lattner00950542001-06-06 20:29:01 +000099
100 // bits Instruction format:
101 // --------------------------
102 // 31-30: Opcode type, fixed to 1.
103 // 29-24: Opcode
104 // 23-12: Resulting type plane
105 // 11- 0: Operand #1 (if set to (2^12-1), then zero operands)
106 //
107 unsigned Opcode = (1 << 30) | (IType << 24) | (Type << 12) | Slots[0];
108 // cerr << "1 " << IType << " " << Type << " " << Slots[0] << endl;
109 output(Opcode, Out);
110}
111
112
113// outputInstructionFormat2 - Output two operand instructions, knowing that no
114// operand index is >= 2^8.
115//
116static void outputInstructionFormat2(const Instruction *I,
117 const SlotCalculator &Table, int *Slots,
118 unsigned Type, vector<uchar> &Out) {
Chris Lattnera41f50d2001-07-07 19:24:15 +0000119 unsigned IType = I->getOpcode(); // Instruction Opcode ID
Chris Lattner00950542001-06-06 20:29:01 +0000120
121 // bits Instruction format:
122 // --------------------------
123 // 31-30: Opcode type, fixed to 2.
124 // 29-24: Opcode
125 // 23-16: Resulting type plane
126 // 15- 8: Operand #1
127 // 7- 0: Operand #2
128 //
129 unsigned Opcode = (2 << 30) | (IType << 24) | (Type << 16) |
130 (Slots[0] << 8) | (Slots[1] << 0);
131 // cerr << "2 " << IType << " " << Type << " " << Slots[0] << " "
132 // << Slots[1] << endl;
133 output(Opcode, Out);
134}
135
136
137// outputInstructionFormat3 - Output three operand instructions, knowing that no
138// operand index is >= 2^6.
139//
140static void outputInstructionFormat3(const Instruction *I,
141 const SlotCalculator &Table, int *Slots,
142 unsigned Type, vector<uchar> &Out) {
Chris Lattnera41f50d2001-07-07 19:24:15 +0000143 unsigned IType = I->getOpcode(); // Instruction Opcode ID
Chris Lattner00950542001-06-06 20:29:01 +0000144
145 // bits Instruction format:
146 // --------------------------
147 // 31-30: Opcode type, fixed to 3
148 // 29-24: Opcode
149 // 23-18: Resulting type plane
150 // 17-12: Operand #1
151 // 11- 6: Operand #2
152 // 5- 0: Operand #3
153 //
154 unsigned Opcode = (3 << 30) | (IType << 24) | (Type << 18) |
155 (Slots[0] << 12) | (Slots[1] << 6) | (Slots[2] << 0);
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000156 //cerr << "3 " << IType << " " << Type << " " << Slots[0] << " "
157 // << Slots[1] << " " << Slots[2] << endl;
Chris Lattner00950542001-06-06 20:29:01 +0000158 output(Opcode, Out);
159}
160
161bool BytecodeWriter::processInstruction(const Instruction *I) {
Chris Lattnera41f50d2001-07-07 19:24:15 +0000162 assert(I->getOpcode() < 64 && "Opcode too big???");
Chris Lattner00950542001-06-06 20:29:01 +0000163
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000164 unsigned NumOperands = I->getNumOperands();
Chris Lattner00950542001-06-06 20:29:01 +0000165 int MaxOpSlot = 0;
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000166 int Slots[3]; Slots[0] = (1 << 12)-1; // Marker to signify 0 operands
Chris Lattner00950542001-06-06 20:29:01 +0000167
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000168 for (unsigned i = 0; i < NumOperands; ++i) {
169 const Value *Def = I->getOperand(i);
Chris Lattner00950542001-06-06 20:29:01 +0000170 int slot = Table.getValSlot(Def);
171 assert(slot != -1 && "Broken bytecode!");
172 if (slot > MaxOpSlot) MaxOpSlot = slot;
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000173 if (i < 3) Slots[i] = slot;
Chris Lattner00950542001-06-06 20:29:01 +0000174 }
175
176 // Figure out which type to encode with the instruction. Typically we want
177 // the type of the first parameter, as opposed to the type of the instruction
178 // (for example, with setcc, we always know it returns bool, but the type of
179 // the first param is actually interesting). But if we have no arguments
180 // we take the type of the instruction itself.
181 //
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000182 const Type *Ty;
183 switch (I->getOpcode()) {
184 case Instruction::Malloc:
185 case Instruction::Alloca:
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000186 Ty = I->getType(); // Malloc & Alloca ALWAYS want to encode the return type
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000187 break;
188 case Instruction::Store:
189 Ty = I->getOperand(1)->getType(); // Encode the pointer type...
Chris Lattner7c501472001-07-28 17:51:21 +0000190 assert(Ty->isPointerType() && "Store to nonpointer type!?!?");
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000191 break;
192 default: // Otherwise use the default behavior...
193 Ty = NumOperands ? I->getOperand(0)->getType() : I->getType();
194 break;
195 }
Chris Lattner00950542001-06-06 20:29:01 +0000196
197 unsigned Type;
198 int Slot = Table.getValSlot(Ty);
199 assert(Slot != -1 && "Type not available!!?!");
200 Type = (unsigned)Slot;
201
Chris Lattner7c501472001-07-28 17:51:21 +0000202 // Make sure that we take the type number into consideration. We don't want
203 // to overflow the field size for the instruction format we select.
204 //
205 if (Slot > MaxOpSlot) MaxOpSlot = Slot;
206
Chris Lattner09083092001-07-08 04:57:15 +0000207 // Handle the special case for cast...
208 if (I->getOpcode() == Instruction::Cast) {
209 // Cast has to encode the destination type as the second argument in the
210 // packet, or else we won't know what type to cast to!
211 Slots[1] = Table.getValSlot(I->getType());
212 assert(Slots[1] != -1 && "Cast return type unknown?");
213 if (Slots[1] > MaxOpSlot) MaxOpSlot = Slots[1];
214 NumOperands++;
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000215 } else if (I->getOpcode() == Instruction::Call && // Handle VarArg calls
216 I->getOperand(0)->getType()->isMethodType()->isVarArg()) {
217 outputInstrVarArgsCall(I, Table, Type, Out);
218 return false;
Chris Lattner09083092001-07-08 04:57:15 +0000219 }
Chris Lattner00950542001-06-06 20:29:01 +0000220
221 // Decide which instruction encoding to use. This is determined primarily by
222 // the number of operands, and secondarily by whether or not the max operand
223 // will fit into the instruction encoding. More operands == fewer bits per
224 // operand.
225 //
226 switch (NumOperands) {
227 case 0:
228 case 1:
229 if (MaxOpSlot < (1 << 12)-1) { // -1 because we use 4095 to indicate 0 ops
230 outputInstructionFormat1(I, Table, Slots, Type, Out);
231 return false;
232 }
233 break;
234
235 case 2:
236 if (MaxOpSlot < (1 << 8)) {
237 outputInstructionFormat2(I, Table, Slots, Type, Out);
238 return false;
239 }
240 break;
241
242 case 3:
243 if (MaxOpSlot < (1 << 6)) {
244 outputInstructionFormat3(I, Table, Slots, Type, Out);
245 return false;
246 }
247 break;
248 }
249
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000250 // If we weren't handled before here, we either have a large number of
251 // operands or a large operand index that we are refering to.
Chris Lattner00950542001-06-06 20:29:01 +0000252 outputInstructionFormat0(I, Table, Type, Out);
253 return false;
254}