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