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