blob: a9b5e8d3041201b27910fb38a7b686469540dfc0 [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===-- 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 Lattner00950542001-06-06 20:29:01 +000018#include <algorithm>
19
20typedef 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//
27static void outputInstructionFormat0(const Instruction *I,
28 const SlotCalculator &Table,
29 unsigned Type, vector<uchar> &Out) {
30 // Opcode must have top two bits clear...
Chris Lattnera41f50d2001-07-07 19:24:15 +000031 output_vbr(I->getOpcode(), Out); // Instruction Opcode ID
Chris Lattner00950542001-06-06 20:29:01 +000032 output_vbr(Type, Out); // Result type
33
Chris Lattnerc8b25d42001-07-07 08:36:50 +000034 unsigned NumArgs = I->getNumOperands();
Chris Lattner00950542001-06-06 20:29:01 +000035 output_vbr(NumArgs, Out);
36
Chris Lattnerc8b25d42001-07-07 08:36:50 +000037 for (unsigned i = 0; i < NumArgs; ++i) {
38 const Value *N = I->getOperand(i);
Chris Lattner00950542001-06-06 20:29:01 +000039 int Slot = Table.getValSlot(N);
Chris Lattnerc8b25d42001-07-07 08:36:50 +000040 assert(Slot >= 0 && "No slot number for value!?!?");
Chris Lattner00950542001-06-06 20:29:01 +000041 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//
50static void outputInstructionFormat1(const Instruction *I,
51 const SlotCalculator &Table, int *Slots,
52 unsigned Type, vector<uchar> &Out) {
Chris Lattnera41f50d2001-07-07 19:24:15 +000053 unsigned IType = I->getOpcode(); // Instruction Opcode ID
Chris Lattner00950542001-06-06 20:29:01 +000054
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//
71static void outputInstructionFormat2(const Instruction *I,
72 const SlotCalculator &Table, int *Slots,
73 unsigned Type, vector<uchar> &Out) {
Chris Lattnera41f50d2001-07-07 19:24:15 +000074 unsigned IType = I->getOpcode(); // Instruction Opcode ID
Chris Lattner00950542001-06-06 20:29:01 +000075
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//
95static void outputInstructionFormat3(const Instruction *I,
96 const SlotCalculator &Table, int *Slots,
97 unsigned Type, vector<uchar> &Out) {
Chris Lattnera41f50d2001-07-07 19:24:15 +000098 unsigned IType = I->getOpcode(); // Instruction Opcode ID
Chris Lattner00950542001-06-06 20:29:01 +000099
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 Lattnerc8b25d42001-07-07 08:36:50 +0000111 //cerr << "3 " << IType << " " << Type << " " << Slots[0] << " "
112 // << Slots[1] << " " << Slots[2] << endl;
Chris Lattner00950542001-06-06 20:29:01 +0000113 output(Opcode, Out);
114}
115
116bool BytecodeWriter::processInstruction(const Instruction *I) {
Chris Lattnera41f50d2001-07-07 19:24:15 +0000117 assert(I->getOpcode() < 64 && "Opcode too big???");
Chris Lattner00950542001-06-06 20:29:01 +0000118
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000119 unsigned NumOperands = I->getNumOperands();
Chris Lattner00950542001-06-06 20:29:01 +0000120 int MaxOpSlot = 0;
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000121 int Slots[3]; Slots[0] = (1 << 12)-1; // Marker to signify 0 operands
Chris Lattner00950542001-06-06 20:29:01 +0000122
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000123 for (unsigned i = 0; i < NumOperands; ++i) {
124 const Value *Def = I->getOperand(i);
Chris Lattner00950542001-06-06 20:29:01 +0000125 int slot = Table.getValSlot(Def);
126 assert(slot != -1 && "Broken bytecode!");
127 if (slot > MaxOpSlot) MaxOpSlot = slot;
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000128 if (i < 3) Slots[i] = slot;
Chris Lattner00950542001-06-06 20:29:01 +0000129 }
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 Lattnerab5ac6b2001-07-08 23:22:50 +0000137 const Type *Ty;
138 switch (I->getOpcode()) {
139 case Instruction::Malloc:
140 case Instruction::Alloca:
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000141 Ty = I->getType(); // Malloc & Alloca ALWAYS want to encode the return type
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000142 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 Lattner00950542001-06-06 20:29:01 +0000150
151 unsigned Type;
152 int Slot = Table.getValSlot(Ty);
153 assert(Slot != -1 && "Type not available!!?!");
154 Type = (unsigned)Slot;
155
Chris Lattner09083092001-07-08 04:57:15 +0000156 // 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 Lattner00950542001-06-06 20:29:01 +0000165
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 Lattnerab5ac6b2001-07-08 23:22:50 +0000195 // 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 Lattner00950542001-06-06 20:29:01 +0000197 outputInstructionFormat0(I, Table, Type, Out);
198 return false;
199}