Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1 | //===-- 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 Lattner | ef9c23f | 2001-10-03 14:53:21 +0000 | [diff] [blame] | 18 | #include "llvm/iOther.h" |
Chris Lattner | 1b98c5c | 2001-10-13 06:48:38 +0000 | [diff] [blame] | 19 | #include "llvm/iTerminators.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 20 | #include <algorithm> |
| 21 | |
| 22 | typedef unsigned char uchar; |
| 23 | |
| 24 | // outputInstructionFormat0 - Output those wierd instructions that have a large |
| 25 | // number of operands or have large operands themselves... |
| 26 | // |
| 27 | // Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>] |
| 28 | // |
| 29 | static void outputInstructionFormat0(const Instruction *I, |
| 30 | const SlotCalculator &Table, |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame^] | 31 | unsigned Type, std::deque<uchar> &Out) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 32 | // Opcode must have top two bits clear... |
Chris Lattner | 2b9f600 | 2001-10-23 03:21:10 +0000 | [diff] [blame] | 33 | output_vbr(I->getOpcode() << 2, Out); // Instruction Opcode ID |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 34 | output_vbr(Type, Out); // Result type |
| 35 | |
Chris Lattner | c8b25d4 | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 36 | unsigned NumArgs = I->getNumOperands(); |
Chris Lattner | 5ab1f87 | 2001-10-21 00:14:44 +0000 | [diff] [blame] | 37 | output_vbr(NumArgs + isa<CastInst>(I), Out); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 38 | |
Chris Lattner | c8b25d4 | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 39 | for (unsigned i = 0; i < NumArgs; ++i) { |
Chris Lattner | e5a57ee | 2001-07-25 22:47:55 +0000 | [diff] [blame] | 40 | int Slot = Table.getValSlot(I->getOperand(i)); |
| 41 | assert(Slot >= 0 && "No slot number for value!?!?"); |
| 42 | output_vbr((unsigned)Slot, Out); |
| 43 | } |
Chris Lattner | 5ab1f87 | 2001-10-21 00:14:44 +0000 | [diff] [blame] | 44 | |
| 45 | if (isa<CastInst>(I)) { |
| 46 | int Slot = Table.getValSlot(I->getType()); |
| 47 | assert(Slot != -1 && "Cast return type unknown?"); |
| 48 | output_vbr((unsigned)Slot, Out); |
| 49 | } |
| 50 | |
Chris Lattner | e5a57ee | 2001-07-25 22:47:55 +0000 | [diff] [blame] | 51 | align32(Out); // We must maintain correct alignment! |
| 52 | } |
| 53 | |
| 54 | |
| 55 | // outputInstrVarArgsCall - Output the obsurdly annoying varargs method calls. |
| 56 | // This are more annoying than most because the signature of the call does not |
| 57 | // tell us anything about the types of the arguments in the varargs portion. |
| 58 | // Because of this, we encode (as type 0) all of the argument types explicitly |
| 59 | // before the argument value. This really sucks, but you shouldn't be using |
| 60 | // varargs functions in your code! *death to printf*! |
| 61 | // |
| 62 | // Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>] |
| 63 | // |
| 64 | static void outputInstrVarArgsCall(const Instruction *I, |
| 65 | const SlotCalculator &Table, unsigned Type, |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame^] | 66 | std::deque<uchar> &Out) { |
Chris Lattner | 1b98c5c | 2001-10-13 06:48:38 +0000 | [diff] [blame] | 67 | assert(isa<CallInst>(I) || isa<InvokeInst>(I)); |
Chris Lattner | e5a57ee | 2001-07-25 22:47:55 +0000 | [diff] [blame] | 68 | // Opcode must have top two bits clear... |
Chris Lattner | 2b9f600 | 2001-10-23 03:21:10 +0000 | [diff] [blame] | 69 | output_vbr(I->getOpcode() << 2, Out); // Instruction Opcode ID |
Chris Lattner | e5a57ee | 2001-07-25 22:47:55 +0000 | [diff] [blame] | 70 | output_vbr(Type, Out); // Result type (varargs type) |
| 71 | |
| 72 | unsigned NumArgs = I->getNumOperands(); |
Chris Lattner | 1b98c5c | 2001-10-13 06:48:38 +0000 | [diff] [blame] | 73 | output_vbr(NumArgs*2, Out); |
| 74 | // TODO: Don't need to emit types for the fixed types of the varargs method |
| 75 | // prototype... |
Chris Lattner | e5a57ee | 2001-07-25 22:47:55 +0000 | [diff] [blame] | 76 | |
Chris Lattner | 1b98c5c | 2001-10-13 06:48:38 +0000 | [diff] [blame] | 77 | // The type for the method has already been emitted in the type field of the |
| 78 | // instruction. Just emit the slot # now. |
Chris Lattner | e5a57ee | 2001-07-25 22:47:55 +0000 | [diff] [blame] | 79 | int Slot = Table.getValSlot(I->getOperand(0)); |
| 80 | assert(Slot >= 0 && "No slot number for value!?!?"); |
| 81 | output_vbr((unsigned)Slot, Out); |
| 82 | |
Chris Lattner | 1b98c5c | 2001-10-13 06:48:38 +0000 | [diff] [blame] | 83 | // Output a dummy field to fill Arg#2 in the reader that is currently unused |
| 84 | // for varargs calls. This is a gross hack to make the code simpler, but we |
| 85 | // aren't really doing very small bytecode for varargs calls anyways. |
| 86 | // FIXME in the future: Smaller bytecode for varargs calls |
| 87 | output_vbr(0, Out); |
Chris Lattner | e5a57ee | 2001-07-25 22:47:55 +0000 | [diff] [blame] | 88 | |
Chris Lattner | 1b98c5c | 2001-10-13 06:48:38 +0000 | [diff] [blame] | 89 | for (unsigned i = 1; i < NumArgs; ++i) { |
Chris Lattner | e5a57ee | 2001-07-25 22:47:55 +0000 | [diff] [blame] | 90 | // Output Arg Type ID |
| 91 | Slot = Table.getValSlot(I->getOperand(i)->getType()); |
| 92 | assert(Slot >= 0 && "No slot number for value!?!?"); |
| 93 | output_vbr((unsigned)Slot, Out); |
| 94 | |
| 95 | // Output arg ID itself |
| 96 | Slot = Table.getValSlot(I->getOperand(i)); |
Chris Lattner | c8b25d4 | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 97 | assert(Slot >= 0 && "No slot number for value!?!?"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 98 | output_vbr((unsigned)Slot, Out); |
| 99 | } |
| 100 | align32(Out); // We must maintain correct alignment! |
| 101 | } |
| 102 | |
| 103 | |
| 104 | // outputInstructionFormat1 - Output one operand instructions, knowing that no |
| 105 | // operand index is >= 2^12. |
| 106 | // |
| 107 | static void outputInstructionFormat1(const Instruction *I, |
| 108 | const SlotCalculator &Table, int *Slots, |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame^] | 109 | unsigned Type, std::deque<uchar> &Out) { |
Chris Lattner | 2b9f600 | 2001-10-23 03:21:10 +0000 | [diff] [blame] | 110 | unsigned Opcode = I->getOpcode(); // Instruction Opcode ID |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 111 | |
| 112 | // bits Instruction format: |
| 113 | // -------------------------- |
Chris Lattner | 2b9f600 | 2001-10-23 03:21:10 +0000 | [diff] [blame] | 114 | // 01-00: Opcode type, fixed to 1. |
| 115 | // 07-02: Opcode |
| 116 | // 19-08: Resulting type plane |
| 117 | // 31-20: Operand #1 (if set to (2^12-1), then zero operands) |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 118 | // |
Chris Lattner | 2b9f600 | 2001-10-23 03:21:10 +0000 | [diff] [blame] | 119 | unsigned Bits = 1 | (Opcode << 2) | (Type << 8) | (Slots[0] << 20); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 120 | // cerr << "1 " << IType << " " << Type << " " << Slots[0] << endl; |
Chris Lattner | 2b9f600 | 2001-10-23 03:21:10 +0000 | [diff] [blame] | 121 | output(Bits, Out); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | |
| 125 | // outputInstructionFormat2 - Output two operand instructions, knowing that no |
| 126 | // operand index is >= 2^8. |
| 127 | // |
| 128 | static void outputInstructionFormat2(const Instruction *I, |
| 129 | const SlotCalculator &Table, int *Slots, |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame^] | 130 | unsigned Type, std::deque<uchar> &Out) { |
Chris Lattner | 2b9f600 | 2001-10-23 03:21:10 +0000 | [diff] [blame] | 131 | unsigned Opcode = I->getOpcode(); // Instruction Opcode ID |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 132 | |
| 133 | // bits Instruction format: |
| 134 | // -------------------------- |
Chris Lattner | 2b9f600 | 2001-10-23 03:21:10 +0000 | [diff] [blame] | 135 | // 01-00: Opcode type, fixed to 2. |
| 136 | // 07-02: Opcode |
| 137 | // 15-08: Resulting type plane |
| 138 | // 23-16: Operand #1 |
| 139 | // 31-24: Operand #2 |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 140 | // |
Chris Lattner | 2b9f600 | 2001-10-23 03:21:10 +0000 | [diff] [blame] | 141 | unsigned Bits = 2 | (Opcode << 2) | (Type << 8) | |
| 142 | (Slots[0] << 16) | (Slots[1] << 24); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 143 | // cerr << "2 " << IType << " " << Type << " " << Slots[0] << " " |
| 144 | // << Slots[1] << endl; |
Chris Lattner | 2b9f600 | 2001-10-23 03:21:10 +0000 | [diff] [blame] | 145 | output(Bits, Out); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | |
| 149 | // outputInstructionFormat3 - Output three operand instructions, knowing that no |
| 150 | // operand index is >= 2^6. |
| 151 | // |
| 152 | static void outputInstructionFormat3(const Instruction *I, |
| 153 | const SlotCalculator &Table, int *Slots, |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame^] | 154 | unsigned Type, std::deque<uchar> &Out) { |
Chris Lattner | 2b9f600 | 2001-10-23 03:21:10 +0000 | [diff] [blame] | 155 | unsigned Opcode = I->getOpcode(); // Instruction Opcode ID |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 156 | |
| 157 | // bits Instruction format: |
| 158 | // -------------------------- |
Chris Lattner | 2b9f600 | 2001-10-23 03:21:10 +0000 | [diff] [blame] | 159 | // 01-00: Opcode type, fixed to 3. |
| 160 | // 07-02: Opcode |
| 161 | // 13-08: Resulting type plane |
| 162 | // 19-14: Operand #1 |
| 163 | // 25-20: Operand #2 |
| 164 | // 31-26: Operand #3 |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 165 | // |
Chris Lattner | 2b9f600 | 2001-10-23 03:21:10 +0000 | [diff] [blame] | 166 | unsigned Bits = 3 | (Opcode << 2) | (Type << 8) | |
| 167 | (Slots[0] << 14) | (Slots[1] << 20) | (Slots[2] << 26); |
Chris Lattner | c8b25d4 | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 168 | //cerr << "3 " << IType << " " << Type << " " << Slots[0] << " " |
| 169 | // << Slots[1] << " " << Slots[2] << endl; |
Chris Lattner | 2b9f600 | 2001-10-23 03:21:10 +0000 | [diff] [blame] | 170 | output(Bits, Out); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 173 | void BytecodeWriter::processInstruction(const Instruction *I) { |
Chris Lattner | a41f50d | 2001-07-07 19:24:15 +0000 | [diff] [blame] | 174 | assert(I->getOpcode() < 64 && "Opcode too big???"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 175 | |
Chris Lattner | c8b25d4 | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 176 | unsigned NumOperands = I->getNumOperands(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 177 | int MaxOpSlot = 0; |
Chris Lattner | c8b25d4 | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 178 | int Slots[3]; Slots[0] = (1 << 12)-1; // Marker to signify 0 operands |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 179 | |
Chris Lattner | c8b25d4 | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 180 | for (unsigned i = 0; i < NumOperands; ++i) { |
| 181 | const Value *Def = I->getOperand(i); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 182 | int slot = Table.getValSlot(Def); |
| 183 | assert(slot != -1 && "Broken bytecode!"); |
| 184 | if (slot > MaxOpSlot) MaxOpSlot = slot; |
Chris Lattner | c8b25d4 | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 185 | if (i < 3) Slots[i] = slot; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | // Figure out which type to encode with the instruction. Typically we want |
| 189 | // the type of the first parameter, as opposed to the type of the instruction |
| 190 | // (for example, with setcc, we always know it returns bool, but the type of |
| 191 | // the first param is actually interesting). But if we have no arguments |
| 192 | // we take the type of the instruction itself. |
| 193 | // |
Chris Lattner | ab5ac6b | 2001-07-08 23:22:50 +0000 | [diff] [blame] | 194 | const Type *Ty; |
| 195 | switch (I->getOpcode()) { |
| 196 | case Instruction::Malloc: |
| 197 | case Instruction::Alloca: |
Chris Lattner | c8b25d4 | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 198 | Ty = I->getType(); // Malloc & Alloca ALWAYS want to encode the return type |
Chris Lattner | ab5ac6b | 2001-07-08 23:22:50 +0000 | [diff] [blame] | 199 | break; |
| 200 | case Instruction::Store: |
| 201 | Ty = I->getOperand(1)->getType(); // Encode the pointer type... |
Chris Lattner | 7c50147 | 2001-07-28 17:51:21 +0000 | [diff] [blame] | 202 | assert(Ty->isPointerType() && "Store to nonpointer type!?!?"); |
Chris Lattner | ab5ac6b | 2001-07-08 23:22:50 +0000 | [diff] [blame] | 203 | break; |
| 204 | default: // Otherwise use the default behavior... |
| 205 | Ty = NumOperands ? I->getOperand(0)->getType() : I->getType(); |
| 206 | break; |
| 207 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 208 | |
| 209 | unsigned Type; |
| 210 | int Slot = Table.getValSlot(Ty); |
| 211 | assert(Slot != -1 && "Type not available!!?!"); |
| 212 | Type = (unsigned)Slot; |
| 213 | |
Chris Lattner | 7c50147 | 2001-07-28 17:51:21 +0000 | [diff] [blame] | 214 | // Make sure that we take the type number into consideration. We don't want |
| 215 | // to overflow the field size for the instruction format we select. |
| 216 | // |
| 217 | if (Slot > MaxOpSlot) MaxOpSlot = Slot; |
| 218 | |
Chris Lattner | 0908309 | 2001-07-08 04:57:15 +0000 | [diff] [blame] | 219 | // Handle the special case for cast... |
Chris Lattner | 5ab1f87 | 2001-10-21 00:14:44 +0000 | [diff] [blame] | 220 | if (isa<CastInst>(I)) { |
Chris Lattner | 0908309 | 2001-07-08 04:57:15 +0000 | [diff] [blame] | 221 | // Cast has to encode the destination type as the second argument in the |
| 222 | // packet, or else we won't know what type to cast to! |
| 223 | Slots[1] = Table.getValSlot(I->getType()); |
| 224 | assert(Slots[1] != -1 && "Cast return type unknown?"); |
| 225 | if (Slots[1] > MaxOpSlot) MaxOpSlot = Slots[1]; |
| 226 | NumOperands++; |
Chris Lattner | ef9c23f | 2001-10-03 14:53:21 +0000 | [diff] [blame] | 227 | } else if (const CallInst *CI = dyn_cast<CallInst>(I)) {// Handle VarArg calls |
Chris Lattner | 1b98c5c | 2001-10-13 06:48:38 +0000 | [diff] [blame] | 228 | PointerType *Ty = cast<PointerType>(CI->getCalledValue()->getType()); |
Chris Lattner | 7a17675 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 229 | if (cast<MethodType>(Ty->getElementType())->isVarArg()) { |
Chris Lattner | 1b98c5c | 2001-10-13 06:48:38 +0000 | [diff] [blame] | 230 | outputInstrVarArgsCall(I, Table, Type, Out); |
| 231 | return; |
| 232 | } |
| 233 | } else if (const InvokeInst *II = dyn_cast<InvokeInst>(I)) { // ... & Invokes |
| 234 | PointerType *Ty = cast<PointerType>(II->getCalledValue()->getType()); |
Chris Lattner | 7a17675 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 235 | if (cast<MethodType>(Ty->getElementType())->isVarArg()) { |
Chris Lattner | ef9c23f | 2001-10-03 14:53:21 +0000 | [diff] [blame] | 236 | outputInstrVarArgsCall(I, Table, Type, Out); |
| 237 | return; |
| 238 | } |
Chris Lattner | 0908309 | 2001-07-08 04:57:15 +0000 | [diff] [blame] | 239 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 240 | |
| 241 | // Decide which instruction encoding to use. This is determined primarily by |
| 242 | // the number of operands, and secondarily by whether or not the max operand |
| 243 | // will fit into the instruction encoding. More operands == fewer bits per |
| 244 | // operand. |
| 245 | // |
| 246 | switch (NumOperands) { |
| 247 | case 0: |
| 248 | case 1: |
| 249 | if (MaxOpSlot < (1 << 12)-1) { // -1 because we use 4095 to indicate 0 ops |
| 250 | outputInstructionFormat1(I, Table, Slots, Type, Out); |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 251 | return; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 252 | } |
| 253 | break; |
| 254 | |
| 255 | case 2: |
| 256 | if (MaxOpSlot < (1 << 8)) { |
| 257 | outputInstructionFormat2(I, Table, Slots, Type, Out); |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 258 | return; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 259 | } |
| 260 | break; |
| 261 | |
| 262 | case 3: |
| 263 | if (MaxOpSlot < (1 << 6)) { |
| 264 | outputInstructionFormat3(I, Table, Slots, Type, Out); |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 265 | return; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 266 | } |
| 267 | break; |
| 268 | } |
| 269 | |
Chris Lattner | ab5ac6b | 2001-07-08 23:22:50 +0000 | [diff] [blame] | 270 | // If we weren't handled before here, we either have a large number of |
| 271 | // operands or a large operand index that we are refering to. |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 272 | outputInstructionFormat0(I, Table, Type, Out); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 273 | } |