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