Chris Lattner | cf3056d | 2003-10-13 03:32:08 +0000 | [diff] [blame] | 1 | //===-- InstructionWriter.cpp - Functions for writing instructions --------===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 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. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements the routines for encoding instruction opcodes to a |
| 11 | // bytecode stream. |
| 12 | // |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "WriterInternals.h" |
| 16 | #include "llvm/Module.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 17 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 0fe56f4 | 2003-09-08 17:58:37 +0000 | [diff] [blame] | 18 | #include "llvm/Instructions.h" |
Chris Lattner | 5fa428f | 2004-04-05 01:27:26 +0000 | [diff] [blame^] | 19 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 20 | #include "Support/Statistic.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 21 | #include <algorithm> |
Chris Lattner | 44f549b | 2004-01-10 18:49:43 +0000 | [diff] [blame] | 22 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 23 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 24 | typedef unsigned char uchar; |
| 25 | |
| 26 | // outputInstructionFormat0 - Output those wierd instructions that have a large |
| 27 | // number of operands or have large operands themselves... |
| 28 | // |
| 29 | // Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>] |
| 30 | // |
Chris Lattner | 0fe56f4 | 2003-09-08 17:58:37 +0000 | [diff] [blame] | 31 | static void outputInstructionFormat0(const Instruction *I, unsigned Opcode, |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 32 | const SlotCalculator &Table, |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 33 | unsigned Type, std::deque<uchar> &Out) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 34 | // Opcode must have top two bits clear... |
Chris Lattner | 0fe56f4 | 2003-09-08 17:58:37 +0000 | [diff] [blame] | 35 | output_vbr(Opcode << 2, Out); // Instruction Opcode ID |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 36 | output_vbr(Type, Out); // Result type |
| 37 | |
Chris Lattner | c8b25d4 | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 38 | unsigned NumArgs = I->getNumOperands(); |
Chris Lattner | eff112c | 2003-10-18 05:54:48 +0000 | [diff] [blame] | 39 | output_vbr(NumArgs + (isa<CastInst>(I) || isa<VANextInst>(I) || |
| 40 | isa<VAArgInst>(I)), Out); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 41 | |
Chris Lattner | 5fa428f | 2004-04-05 01:27:26 +0000 | [diff] [blame^] | 42 | if (!isa<GetElementPtrInst>(&I)) { |
| 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); |
| 47 | } |
Chris Lattner | 5ab1f87 | 2001-10-21 00:14:44 +0000 | [diff] [blame] | 48 | |
Chris Lattner | 5fa428f | 2004-04-05 01:27:26 +0000 | [diff] [blame^] | 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); |
| 57 | } |
| 58 | |
| 59 | } else { |
| 60 | int Slot = Table.getSlot(I->getOperand(0)); |
| 61 | assert(Slot >= 0 && "No slot number for value!?!?"); |
| 62 | output_vbr(unsigned(Slot), Out); |
| 63 | |
| 64 | // We need to encode the type of sequential type indices into their slot # |
| 65 | unsigned Idx = 1; |
| 66 | for (gep_type_iterator TI = gep_type_begin(I), E = gep_type_end(I); |
| 67 | Idx != NumArgs; ++TI, ++Idx) { |
| 68 | Slot = Table.getSlot(I->getOperand(Idx)); |
| 69 | assert(Slot >= 0 && "No slot number for value!?!?"); |
| 70 | |
| 71 | if (isa<SequentialType>(*TI)) { |
| 72 | unsigned IdxId; |
| 73 | switch (I->getOperand(Idx)->getType()->getPrimitiveID()) { |
| 74 | default: assert(0 && "Unknown index type!"); |
| 75 | case Type::UIntTyID: IdxId = 0; break; |
| 76 | case Type::IntTyID: IdxId = 1; break; |
| 77 | case Type::ULongTyID: IdxId = 2; break; |
| 78 | case Type::LongTyID: IdxId = 3; break; |
| 79 | } |
| 80 | Slot = (Slot << 2) | IdxId; |
| 81 | } |
| 82 | output_vbr(unsigned(Slot), Out); |
| 83 | } |
Chris Lattner | 5ab1f87 | 2001-10-21 00:14:44 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Chris Lattner | e5a57ee | 2001-07-25 22:47:55 +0000 | [diff] [blame] | 86 | align32(Out); // We must maintain correct alignment! |
| 87 | } |
| 88 | |
| 89 | |
Misha Brukman | 37f92e2 | 2003-09-11 22:34:13 +0000 | [diff] [blame] | 90 | // outputInstrVarArgsCall - Output the absurdly annoying varargs function calls. |
Chris Lattner | e5a57ee | 2001-07-25 22:47:55 +0000 | [diff] [blame] | 91 | // This are more annoying than most because the signature of the call does not |
| 92 | // tell us anything about the types of the arguments in the varargs portion. |
| 93 | // Because of this, we encode (as type 0) all of the argument types explicitly |
| 94 | // before the argument value. This really sucks, but you shouldn't be using |
| 95 | // varargs functions in your code! *death to printf*! |
| 96 | // |
| 97 | // Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>] |
| 98 | // |
Chris Lattner | 0fe56f4 | 2003-09-08 17:58:37 +0000 | [diff] [blame] | 99 | static void outputInstrVarArgsCall(const Instruction *I, unsigned Opcode, |
Chris Lattner | e5a57ee | 2001-07-25 22:47:55 +0000 | [diff] [blame] | 100 | const SlotCalculator &Table, unsigned Type, |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 101 | std::deque<uchar> &Out) { |
Chris Lattner | 1b98c5c | 2001-10-13 06:48:38 +0000 | [diff] [blame] | 102 | assert(isa<CallInst>(I) || isa<InvokeInst>(I)); |
Chris Lattner | e5a57ee | 2001-07-25 22:47:55 +0000 | [diff] [blame] | 103 | // Opcode must have top two bits clear... |
Chris Lattner | 0fe56f4 | 2003-09-08 17:58:37 +0000 | [diff] [blame] | 104 | output_vbr(Opcode << 2, Out); // Instruction Opcode ID |
Chris Lattner | e5a57ee | 2001-07-25 22:47:55 +0000 | [diff] [blame] | 105 | output_vbr(Type, Out); // Result type (varargs type) |
| 106 | |
Chris Lattner | eff112c | 2003-10-18 05:54:48 +0000 | [diff] [blame] | 107 | const PointerType *PTy = cast<PointerType>(I->getOperand(0)->getType()); |
| 108 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
| 109 | unsigned NumParams = FTy->getNumParams(); |
| 110 | |
| 111 | unsigned NumFixedOperands; |
| 112 | if (isa<CallInst>(I)) { |
| 113 | // Output an operand for the callee and each fixed argument, then two for |
| 114 | // each variable argument. |
| 115 | NumFixedOperands = 1+NumParams; |
| 116 | } else { |
| 117 | assert(isa<InvokeInst>(I) && "Not call or invoke??"); |
| 118 | // Output an operand for the callee and destinations, then two for each |
| 119 | // variable argument. |
| 120 | NumFixedOperands = 3+NumParams; |
| 121 | } |
| 122 | output_vbr(2 * I->getNumOperands()-NumFixedOperands, Out); |
Chris Lattner | e5a57ee | 2001-07-25 22:47:55 +0000 | [diff] [blame] | 123 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 124 | // The type for the function has already been emitted in the type field of the |
Chris Lattner | 1b98c5c | 2001-10-13 06:48:38 +0000 | [diff] [blame] | 125 | // instruction. Just emit the slot # now. |
Chris Lattner | eff112c | 2003-10-18 05:54:48 +0000 | [diff] [blame] | 126 | for (unsigned i = 0; i != NumFixedOperands; ++i) { |
| 127 | int Slot = Table.getSlot(I->getOperand(i)); |
Chris Lattner | e5a57ee | 2001-07-25 22:47:55 +0000 | [diff] [blame] | 128 | assert(Slot >= 0 && "No slot number for value!?!?"); |
| 129 | output_vbr((unsigned)Slot, Out); |
Chris Lattner | eff112c | 2003-10-18 05:54:48 +0000 | [diff] [blame] | 130 | } |
Chris Lattner | e5a57ee | 2001-07-25 22:47:55 +0000 | [diff] [blame] | 131 | |
Chris Lattner | eff112c | 2003-10-18 05:54:48 +0000 | [diff] [blame] | 132 | for (unsigned i = NumFixedOperands, e = I->getNumOperands(); i != e; ++i) { |
| 133 | // Output Arg Type ID |
| 134 | int Slot = Table.getSlot(I->getOperand(i)->getType()); |
| 135 | assert(Slot >= 0 && "No slot number for value!?!?"); |
| 136 | output_vbr((unsigned)Slot, Out); |
| 137 | |
Chris Lattner | e5a57ee | 2001-07-25 22:47:55 +0000 | [diff] [blame] | 138 | // Output arg ID itself |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 139 | Slot = Table.getSlot(I->getOperand(i)); |
Chris Lattner | c8b25d4 | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 140 | assert(Slot >= 0 && "No slot number for value!?!?"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 141 | output_vbr((unsigned)Slot, Out); |
| 142 | } |
| 143 | align32(Out); // We must maintain correct alignment! |
| 144 | } |
| 145 | |
| 146 | |
| 147 | // outputInstructionFormat1 - Output one operand instructions, knowing that no |
| 148 | // operand index is >= 2^12. |
| 149 | // |
Chris Lattner | 0fe56f4 | 2003-09-08 17:58:37 +0000 | [diff] [blame] | 150 | static void outputInstructionFormat1(const Instruction *I, unsigned Opcode, |
Chris Lattner | 5fa428f | 2004-04-05 01:27:26 +0000 | [diff] [blame^] | 151 | const SlotCalculator &Table, |
| 152 | unsigned *Slots, unsigned Type, |
| 153 | std::deque<uchar> &Out) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 154 | // bits Instruction format: |
| 155 | // -------------------------- |
Chris Lattner | 2b9f600 | 2001-10-23 03:21:10 +0000 | [diff] [blame] | 156 | // 01-00: Opcode type, fixed to 1. |
| 157 | // 07-02: Opcode |
| 158 | // 19-08: Resulting type plane |
| 159 | // 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] | 160 | // |
Chris Lattner | 2b9f600 | 2001-10-23 03:21:10 +0000 | [diff] [blame] | 161 | unsigned Bits = 1 | (Opcode << 2) | (Type << 8) | (Slots[0] << 20); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 162 | // cerr << "1 " << IType << " " << Type << " " << Slots[0] << endl; |
Chris Lattner | 2b9f600 | 2001-10-23 03:21:10 +0000 | [diff] [blame] | 163 | output(Bits, Out); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | |
| 167 | // outputInstructionFormat2 - Output two operand instructions, knowing that no |
| 168 | // operand index is >= 2^8. |
| 169 | // |
Chris Lattner | 0fe56f4 | 2003-09-08 17:58:37 +0000 | [diff] [blame] | 170 | static void outputInstructionFormat2(const Instruction *I, unsigned Opcode, |
Chris Lattner | 5fa428f | 2004-04-05 01:27:26 +0000 | [diff] [blame^] | 171 | const SlotCalculator &Table, |
| 172 | unsigned *Slots, unsigned Type, |
| 173 | std::deque<uchar> &Out) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 174 | // bits Instruction format: |
| 175 | // -------------------------- |
Chris Lattner | 2b9f600 | 2001-10-23 03:21:10 +0000 | [diff] [blame] | 176 | // 01-00: Opcode type, fixed to 2. |
| 177 | // 07-02: Opcode |
| 178 | // 15-08: Resulting type plane |
| 179 | // 23-16: Operand #1 |
| 180 | // 31-24: Operand #2 |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 181 | // |
Chris Lattner | 2b9f600 | 2001-10-23 03:21:10 +0000 | [diff] [blame] | 182 | unsigned Bits = 2 | (Opcode << 2) | (Type << 8) | |
| 183 | (Slots[0] << 16) | (Slots[1] << 24); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 184 | // cerr << "2 " << IType << " " << Type << " " << Slots[0] << " " |
| 185 | // << Slots[1] << endl; |
Chris Lattner | 2b9f600 | 2001-10-23 03:21:10 +0000 | [diff] [blame] | 186 | output(Bits, Out); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | |
| 190 | // outputInstructionFormat3 - Output three operand instructions, knowing that no |
| 191 | // operand index is >= 2^6. |
| 192 | // |
Chris Lattner | 0fe56f4 | 2003-09-08 17:58:37 +0000 | [diff] [blame] | 193 | static void outputInstructionFormat3(const Instruction *I, unsigned Opcode, |
Chris Lattner | 5fa428f | 2004-04-05 01:27:26 +0000 | [diff] [blame^] | 194 | const SlotCalculator &Table, |
| 195 | unsigned *Slots, unsigned Type, |
| 196 | std::deque<uchar> &Out) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 197 | // bits Instruction format: |
| 198 | // -------------------------- |
Chris Lattner | 2b9f600 | 2001-10-23 03:21:10 +0000 | [diff] [blame] | 199 | // 01-00: Opcode type, fixed to 3. |
| 200 | // 07-02: Opcode |
| 201 | // 13-08: Resulting type plane |
| 202 | // 19-14: Operand #1 |
| 203 | // 25-20: Operand #2 |
| 204 | // 31-26: Operand #3 |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 205 | // |
Chris Lattner | 2b9f600 | 2001-10-23 03:21:10 +0000 | [diff] [blame] | 206 | unsigned Bits = 3 | (Opcode << 2) | (Type << 8) | |
| 207 | (Slots[0] << 14) | (Slots[1] << 20) | (Slots[2] << 26); |
Chris Lattner | c8b25d4 | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 208 | //cerr << "3 " << IType << " " << Type << " " << Slots[0] << " " |
| 209 | // << Slots[1] << " " << Slots[2] << endl; |
Chris Lattner | 2b9f600 | 2001-10-23 03:21:10 +0000 | [diff] [blame] | 210 | output(Bits, Out); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Chris Lattner | cf3e67f | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 213 | void BytecodeWriter::outputInstruction(const Instruction &I) { |
Chris Lattner | 0fe56f4 | 2003-09-08 17:58:37 +0000 | [diff] [blame] | 214 | assert(I.getOpcode() < 62 && "Opcode too big???"); |
| 215 | unsigned Opcode = I.getOpcode(); |
Chris Lattner | 5fa428f | 2004-04-05 01:27:26 +0000 | [diff] [blame^] | 216 | unsigned NumOperands = I.getNumOperands(); |
Chris Lattner | 0fe56f4 | 2003-09-08 17:58:37 +0000 | [diff] [blame] | 217 | |
| 218 | // Encode 'volatile load' as 62 and 'volatile store' as 63. |
| 219 | if (isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) |
| 220 | Opcode = 62; |
| 221 | if (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()) |
| 222 | Opcode = 63; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 223 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 224 | // Figure out which type to encode with the instruction. Typically we want |
| 225 | // the type of the first parameter, as opposed to the type of the instruction |
| 226 | // (for example, with setcc, we always know it returns bool, but the type of |
| 227 | // the first param is actually interesting). But if we have no arguments |
| 228 | // we take the type of the instruction itself. |
| 229 | // |
Chris Lattner | ab5ac6b | 2001-07-08 23:22:50 +0000 | [diff] [blame] | 230 | const Type *Ty; |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 231 | switch (I.getOpcode()) { |
Chris Lattner | 46b787e | 2004-03-12 05:52:01 +0000 | [diff] [blame] | 232 | case Instruction::Select: |
Chris Lattner | ab5ac6b | 2001-07-08 23:22:50 +0000 | [diff] [blame] | 233 | case Instruction::Malloc: |
| 234 | case Instruction::Alloca: |
Chris Lattner | 46b787e | 2004-03-12 05:52:01 +0000 | [diff] [blame] | 235 | Ty = I.getType(); // These ALWAYS want to encode the return type |
Chris Lattner | ab5ac6b | 2001-07-08 23:22:50 +0000 | [diff] [blame] | 236 | break; |
| 237 | case Instruction::Store: |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 238 | Ty = I.getOperand(1)->getType(); // Encode the pointer type... |
Chris Lattner | 9b62503 | 2002-05-06 16:15:30 +0000 | [diff] [blame] | 239 | assert(isa<PointerType>(Ty) && "Store to nonpointer type!?!?"); |
Chris Lattner | ab5ac6b | 2001-07-08 23:22:50 +0000 | [diff] [blame] | 240 | break; |
| 241 | default: // Otherwise use the default behavior... |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 242 | Ty = NumOperands ? I.getOperand(0)->getType() : I.getType(); |
Chris Lattner | ab5ac6b | 2001-07-08 23:22:50 +0000 | [diff] [blame] | 243 | break; |
| 244 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 245 | |
| 246 | unsigned Type; |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 247 | int Slot = Table.getSlot(Ty); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 248 | assert(Slot != -1 && "Type not available!!?!"); |
| 249 | Type = (unsigned)Slot; |
| 250 | |
Chris Lattner | 5fa428f | 2004-04-05 01:27:26 +0000 | [diff] [blame^] | 251 | // Varargs calls and invokes are encoded entirely different from any other |
| 252 | // instructions. |
| 253 | if (const CallInst *CI = dyn_cast<CallInst>(&I)){ |
| 254 | const PointerType *Ty =cast<PointerType>(CI->getCalledValue()->getType()); |
Chris Lattner | 2aac6bf | 2002-04-04 22:19:18 +0000 | [diff] [blame] | 255 | if (cast<FunctionType>(Ty->getElementType())->isVarArg()) { |
Chris Lattner | 0fe56f4 | 2003-09-08 17:58:37 +0000 | [diff] [blame] | 256 | outputInstrVarArgsCall(CI, Opcode, Table, Type, Out); |
Chris Lattner | 1b98c5c | 2001-10-13 06:48:38 +0000 | [diff] [blame] | 257 | return; |
| 258 | } |
Chris Lattner | 5fa428f | 2004-04-05 01:27:26 +0000 | [diff] [blame^] | 259 | } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) { |
| 260 | const PointerType *Ty =cast<PointerType>(II->getCalledValue()->getType()); |
Chris Lattner | 2aac6bf | 2002-04-04 22:19:18 +0000 | [diff] [blame] | 261 | if (cast<FunctionType>(Ty->getElementType())->isVarArg()) { |
Chris Lattner | 0fe56f4 | 2003-09-08 17:58:37 +0000 | [diff] [blame] | 262 | outputInstrVarArgsCall(II, Opcode, Table, Type, Out); |
Chris Lattner | ef9c23f | 2001-10-03 14:53:21 +0000 | [diff] [blame] | 263 | return; |
| 264 | } |
Chris Lattner | 0908309 | 2001-07-08 04:57:15 +0000 | [diff] [blame] | 265 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 266 | |
Chris Lattner | 5fa428f | 2004-04-05 01:27:26 +0000 | [diff] [blame^] | 267 | if (NumOperands <= 3) { |
| 268 | // Make sure that we take the type number into consideration. We don't want |
| 269 | // to overflow the field size for the instruction format we select. |
| 270 | // |
| 271 | unsigned MaxOpSlot = Type; |
| 272 | unsigned Slots[3]; Slots[0] = (1 << 12)-1; // Marker to signify 0 operands |
| 273 | |
| 274 | for (unsigned i = 0; i != NumOperands; ++i) { |
| 275 | int slot = Table.getSlot(I.getOperand(i)); |
| 276 | assert(slot != -1 && "Broken bytecode!"); |
| 277 | if (unsigned(slot) > MaxOpSlot) MaxOpSlot = unsigned(slot); |
| 278 | Slots[i] = unsigned(slot); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 279 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 280 | |
Chris Lattner | 5fa428f | 2004-04-05 01:27:26 +0000 | [diff] [blame^] | 281 | // Handle the special cases for various instructions... |
| 282 | if (isa<CastInst>(I) || isa<VAArgInst>(I)) { |
| 283 | // Cast has to encode the destination type as the second argument in the |
| 284 | // packet, or else we won't know what type to cast to! |
| 285 | Slots[1] = Table.getSlot(I.getType()); |
| 286 | assert(Slots[1] != ~0U && "Cast return type unknown?"); |
| 287 | if (Slots[1] > MaxOpSlot) MaxOpSlot = Slots[1]; |
| 288 | NumOperands++; |
| 289 | } else if (const VANextInst *VANI = dyn_cast<VANextInst>(&I)) { |
| 290 | Slots[1] = Table.getSlot(VANI->getArgType()); |
| 291 | assert(Slots[1] != ~0U && "va_next return type unknown?"); |
| 292 | if (Slots[1] > MaxOpSlot) MaxOpSlot = Slots[1]; |
| 293 | NumOperands++; |
| 294 | } else if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&I)) { |
| 295 | // We need to encode the type of sequential type indices into their slot # |
| 296 | unsigned Idx = 1; |
| 297 | for (gep_type_iterator I = gep_type_begin(GEP), E = gep_type_end(GEP); |
| 298 | I != E; ++I, ++Idx) |
| 299 | if (isa<SequentialType>(*I)) { |
| 300 | unsigned IdxId; |
| 301 | switch (GEP->getOperand(Idx)->getType()->getPrimitiveID()) { |
| 302 | default: assert(0 && "Unknown index type!"); |
| 303 | case Type::UIntTyID: IdxId = 0; break; |
| 304 | case Type::IntTyID: IdxId = 1; break; |
| 305 | case Type::ULongTyID: IdxId = 2; break; |
| 306 | case Type::LongTyID: IdxId = 3; break; |
| 307 | } |
| 308 | Slots[Idx] = (Slots[Idx] << 2) | IdxId; |
| 309 | if (Slots[Idx] > MaxOpSlot) MaxOpSlot = Slots[Idx]; |
| 310 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 311 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 312 | |
Chris Lattner | 5fa428f | 2004-04-05 01:27:26 +0000 | [diff] [blame^] | 313 | // Decide which instruction encoding to use. This is determined primarily |
| 314 | // by the number of operands, and secondarily by whether or not the max |
| 315 | // operand will fit into the instruction encoding. More operands == fewer |
| 316 | // bits per operand. |
| 317 | // |
| 318 | switch (NumOperands) { |
| 319 | case 0: |
| 320 | case 1: |
| 321 | if (MaxOpSlot < (1 << 12)-1) { // -1 because we use 4095 to indicate 0 ops |
| 322 | outputInstructionFormat1(&I, Opcode, Table, Slots, Type, Out); |
| 323 | return; |
| 324 | } |
| 325 | break; |
| 326 | |
| 327 | case 2: |
| 328 | if (MaxOpSlot < (1 << 8)) { |
| 329 | outputInstructionFormat2(&I, Opcode, Table, Slots, Type, Out); |
| 330 | return; |
| 331 | } |
| 332 | break; |
| 333 | |
| 334 | case 3: |
| 335 | if (MaxOpSlot < (1 << 6)) { |
| 336 | outputInstructionFormat3(&I, Opcode, Table, Slots, Type, Out); |
| 337 | return; |
| 338 | } |
| 339 | break; |
| 340 | default: |
| 341 | break; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 342 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Chris Lattner | ab5ac6b | 2001-07-08 23:22:50 +0000 | [diff] [blame] | 345 | // If we weren't handled before here, we either have a large number of |
Misha Brukman | 37f92e2 | 2003-09-11 22:34:13 +0000 | [diff] [blame] | 346 | // operands or a large operand index that we are referring to. |
Chris Lattner | 0fe56f4 | 2003-09-08 17:58:37 +0000 | [diff] [blame] | 347 | outputInstructionFormat0(&I, Opcode, Table, Type, Out); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 348 | } |