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 | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 18 | #include <algorithm> |
| 19 | |
| 20 | typedef unsigned char uchar; |
| 21 | |
| 22 | // outputInstructionFormat0 - Output those wierd instructions that have a large |
| 23 | // number of operands or have large operands themselves... |
| 24 | // |
| 25 | // Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>] |
| 26 | // |
| 27 | static void outputInstructionFormat0(const Instruction *I, |
| 28 | const SlotCalculator &Table, |
| 29 | unsigned Type, vector<uchar> &Out) { |
| 30 | // Opcode must have top two bits clear... |
Chris Lattner | a41f50d | 2001-07-07 19:24:15 +0000 | [diff] [blame] | 31 | output_vbr(I->getOpcode(), Out); // Instruction Opcode ID |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 32 | output_vbr(Type, Out); // Result type |
| 33 | |
Chris Lattner | c8b25d4 | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 34 | unsigned NumArgs = I->getNumOperands(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 35 | output_vbr(NumArgs, Out); |
| 36 | |
Chris Lattner | c8b25d4 | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 37 | for (unsigned i = 0; i < NumArgs; ++i) { |
| 38 | const Value *N = I->getOperand(i); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 39 | int Slot = Table.getValSlot(N); |
Chris Lattner | c8b25d4 | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 40 | assert(Slot >= 0 && "No slot number for value!?!?"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 41 | output_vbr((unsigned)Slot, Out); |
| 42 | } |
| 43 | align32(Out); // We must maintain correct alignment! |
| 44 | } |
| 45 | |
| 46 | |
| 47 | // outputInstructionFormat1 - Output one operand instructions, knowing that no |
| 48 | // operand index is >= 2^12. |
| 49 | // |
| 50 | static void outputInstructionFormat1(const Instruction *I, |
| 51 | const SlotCalculator &Table, int *Slots, |
| 52 | unsigned Type, vector<uchar> &Out) { |
Chris Lattner | a41f50d | 2001-07-07 19:24:15 +0000 | [diff] [blame] | 53 | unsigned IType = I->getOpcode(); // Instruction Opcode ID |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 54 | |
| 55 | // bits Instruction format: |
| 56 | // -------------------------- |
| 57 | // 31-30: Opcode type, fixed to 1. |
| 58 | // 29-24: Opcode |
| 59 | // 23-12: Resulting type plane |
| 60 | // 11- 0: Operand #1 (if set to (2^12-1), then zero operands) |
| 61 | // |
| 62 | unsigned Opcode = (1 << 30) | (IType << 24) | (Type << 12) | Slots[0]; |
| 63 | // cerr << "1 " << IType << " " << Type << " " << Slots[0] << endl; |
| 64 | output(Opcode, Out); |
| 65 | } |
| 66 | |
| 67 | |
| 68 | // outputInstructionFormat2 - Output two operand instructions, knowing that no |
| 69 | // operand index is >= 2^8. |
| 70 | // |
| 71 | static void outputInstructionFormat2(const Instruction *I, |
| 72 | const SlotCalculator &Table, int *Slots, |
| 73 | unsigned Type, vector<uchar> &Out) { |
Chris Lattner | a41f50d | 2001-07-07 19:24:15 +0000 | [diff] [blame] | 74 | unsigned IType = I->getOpcode(); // Instruction Opcode ID |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 75 | |
| 76 | // bits Instruction format: |
| 77 | // -------------------------- |
| 78 | // 31-30: Opcode type, fixed to 2. |
| 79 | // 29-24: Opcode |
| 80 | // 23-16: Resulting type plane |
| 81 | // 15- 8: Operand #1 |
| 82 | // 7- 0: Operand #2 |
| 83 | // |
| 84 | unsigned Opcode = (2 << 30) | (IType << 24) | (Type << 16) | |
| 85 | (Slots[0] << 8) | (Slots[1] << 0); |
| 86 | // cerr << "2 " << IType << " " << Type << " " << Slots[0] << " " |
| 87 | // << Slots[1] << endl; |
| 88 | output(Opcode, Out); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | // outputInstructionFormat3 - Output three operand instructions, knowing that no |
| 93 | // operand index is >= 2^6. |
| 94 | // |
| 95 | static void outputInstructionFormat3(const Instruction *I, |
| 96 | const SlotCalculator &Table, int *Slots, |
| 97 | unsigned Type, vector<uchar> &Out) { |
Chris Lattner | a41f50d | 2001-07-07 19:24:15 +0000 | [diff] [blame] | 98 | unsigned IType = I->getOpcode(); // Instruction Opcode ID |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 99 | |
| 100 | // bits Instruction format: |
| 101 | // -------------------------- |
| 102 | // 31-30: Opcode type, fixed to 3 |
| 103 | // 29-24: Opcode |
| 104 | // 23-18: Resulting type plane |
| 105 | // 17-12: Operand #1 |
| 106 | // 11- 6: Operand #2 |
| 107 | // 5- 0: Operand #3 |
| 108 | // |
| 109 | unsigned Opcode = (3 << 30) | (IType << 24) | (Type << 18) | |
| 110 | (Slots[0] << 12) | (Slots[1] << 6) | (Slots[2] << 0); |
Chris Lattner | c8b25d4 | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 111 | //cerr << "3 " << IType << " " << Type << " " << Slots[0] << " " |
| 112 | // << Slots[1] << " " << Slots[2] << endl; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 113 | output(Opcode, Out); |
| 114 | } |
| 115 | |
| 116 | bool BytecodeWriter::processInstruction(const Instruction *I) { |
Chris Lattner | a41f50d | 2001-07-07 19:24:15 +0000 | [diff] [blame] | 117 | assert(I->getOpcode() < 64 && "Opcode too big???"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 118 | |
Chris Lattner | c8b25d4 | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 119 | unsigned NumOperands = I->getNumOperands(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 120 | int MaxOpSlot = 0; |
Chris Lattner | c8b25d4 | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 121 | 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] | 122 | |
Chris Lattner | c8b25d4 | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 123 | for (unsigned i = 0; i < NumOperands; ++i) { |
| 124 | const Value *Def = I->getOperand(i); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 125 | int slot = Table.getValSlot(Def); |
| 126 | assert(slot != -1 && "Broken bytecode!"); |
| 127 | if (slot > MaxOpSlot) MaxOpSlot = slot; |
Chris Lattner | c8b25d4 | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 128 | if (i < 3) Slots[i] = slot; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | // Figure out which type to encode with the instruction. Typically we want |
| 132 | // the type of the first parameter, as opposed to the type of the instruction |
| 133 | // (for example, with setcc, we always know it returns bool, but the type of |
| 134 | // the first param is actually interesting). But if we have no arguments |
| 135 | // we take the type of the instruction itself. |
| 136 | // |
Chris Lattner | ab5ac6b | 2001-07-08 23:22:50 +0000 | [diff] [blame] | 137 | const Type *Ty; |
| 138 | switch (I->getOpcode()) { |
| 139 | case Instruction::Malloc: |
| 140 | case Instruction::Alloca: |
Chris Lattner | c8b25d4 | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 141 | Ty = I->getType(); // Malloc & Alloca ALWAYS want to encode the return type |
Chris Lattner | ab5ac6b | 2001-07-08 23:22:50 +0000 | [diff] [blame] | 142 | break; |
| 143 | case Instruction::Store: |
| 144 | Ty = I->getOperand(1)->getType(); // Encode the pointer type... |
| 145 | break; |
| 146 | default: // Otherwise use the default behavior... |
| 147 | Ty = NumOperands ? I->getOperand(0)->getType() : I->getType(); |
| 148 | break; |
| 149 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 150 | |
| 151 | unsigned Type; |
| 152 | int Slot = Table.getValSlot(Ty); |
| 153 | assert(Slot != -1 && "Type not available!!?!"); |
| 154 | Type = (unsigned)Slot; |
| 155 | |
Chris Lattner | 0908309 | 2001-07-08 04:57:15 +0000 | [diff] [blame] | 156 | // Handle the special case for cast... |
| 157 | if (I->getOpcode() == Instruction::Cast) { |
| 158 | // Cast has to encode the destination type as the second argument in the |
| 159 | // packet, or else we won't know what type to cast to! |
| 160 | Slots[1] = Table.getValSlot(I->getType()); |
| 161 | assert(Slots[1] != -1 && "Cast return type unknown?"); |
| 162 | if (Slots[1] > MaxOpSlot) MaxOpSlot = Slots[1]; |
| 163 | NumOperands++; |
| 164 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 165 | |
| 166 | // Decide which instruction encoding to use. This is determined primarily by |
| 167 | // the number of operands, and secondarily by whether or not the max operand |
| 168 | // will fit into the instruction encoding. More operands == fewer bits per |
| 169 | // operand. |
| 170 | // |
| 171 | switch (NumOperands) { |
| 172 | case 0: |
| 173 | case 1: |
| 174 | if (MaxOpSlot < (1 << 12)-1) { // -1 because we use 4095 to indicate 0 ops |
| 175 | outputInstructionFormat1(I, Table, Slots, Type, Out); |
| 176 | return false; |
| 177 | } |
| 178 | break; |
| 179 | |
| 180 | case 2: |
| 181 | if (MaxOpSlot < (1 << 8)) { |
| 182 | outputInstructionFormat2(I, Table, Slots, Type, Out); |
| 183 | return false; |
| 184 | } |
| 185 | break; |
| 186 | |
| 187 | case 3: |
| 188 | if (MaxOpSlot < (1 << 6)) { |
| 189 | outputInstructionFormat3(I, Table, Slots, Type, Out); |
| 190 | return false; |
| 191 | } |
| 192 | break; |
| 193 | } |
| 194 | |
Chris Lattner | ab5ac6b | 2001-07-08 23:22:50 +0000 | [diff] [blame] | 195 | // If we weren't handled before here, we either have a large number of |
| 196 | // operands or a large operand index that we are refering to. |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 197 | outputInstructionFormat0(I, Table, Type, Out); |
| 198 | return false; |
| 199 | } |