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