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