blob: c7c04efb731bd5b6fd791ab4e22e3d592a3e2c15 [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"
18#include "llvm/Tools/DataTypes.h"
19#include <algorithm>
20
21typedef 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//
28static void outputInstructionFormat0(const Instruction *I,
29 const SlotCalculator &Table,
30 unsigned Type, vector<uchar> &Out) {
31 // Opcode must have top two bits clear...
32 output_vbr(I->getInstType(), Out); // Instruction Opcode ID
33 output_vbr(Type, Out); // Result type
34
35 unsigned NumArgs; // Count the number of arguments to the instruction
36 for (NumArgs = 0; I->getOperand(NumArgs); NumArgs++) /*empty*/;
37 output_vbr(NumArgs, Out);
38
39 for (unsigned i = 0; const Value *N = I->getOperand(i); i++) {
40 assert(i < NumArgs && "Count of arguments failed!");
41
42 int Slot = Table.getValSlot(N);
43 output_vbr((unsigned)Slot, Out);
44 }
45 align32(Out); // We must maintain correct alignment!
46}
47
48
49// outputInstructionFormat1 - Output one operand instructions, knowing that no
50// operand index is >= 2^12.
51//
52static void outputInstructionFormat1(const Instruction *I,
53 const SlotCalculator &Table, int *Slots,
54 unsigned Type, vector<uchar> &Out) {
55 unsigned IType = I->getInstType(); // Instruction Opcode ID
56
57 // bits Instruction format:
58 // --------------------------
59 // 31-30: Opcode type, fixed to 1.
60 // 29-24: Opcode
61 // 23-12: Resulting type plane
62 // 11- 0: Operand #1 (if set to (2^12-1), then zero operands)
63 //
64 unsigned Opcode = (1 << 30) | (IType << 24) | (Type << 12) | Slots[0];
65 // cerr << "1 " << IType << " " << Type << " " << Slots[0] << endl;
66 output(Opcode, Out);
67}
68
69
70// outputInstructionFormat2 - Output two operand instructions, knowing that no
71// operand index is >= 2^8.
72//
73static void outputInstructionFormat2(const Instruction *I,
74 const SlotCalculator &Table, int *Slots,
75 unsigned Type, vector<uchar> &Out) {
76 unsigned IType = I->getInstType(); // Instruction Opcode ID
77
78 // bits Instruction format:
79 // --------------------------
80 // 31-30: Opcode type, fixed to 2.
81 // 29-24: Opcode
82 // 23-16: Resulting type plane
83 // 15- 8: Operand #1
84 // 7- 0: Operand #2
85 //
86 unsigned Opcode = (2 << 30) | (IType << 24) | (Type << 16) |
87 (Slots[0] << 8) | (Slots[1] << 0);
88 // cerr << "2 " << IType << " " << Type << " " << Slots[0] << " "
89 // << Slots[1] << endl;
90 output(Opcode, Out);
91}
92
93
94// outputInstructionFormat3 - Output three operand instructions, knowing that no
95// operand index is >= 2^6.
96//
97static void outputInstructionFormat3(const Instruction *I,
98 const SlotCalculator &Table, int *Slots,
99 unsigned Type, vector<uchar> &Out) {
100 unsigned IType = I->getInstType(); // Instruction Opcode ID
101
102 // bits Instruction format:
103 // --------------------------
104 // 31-30: Opcode type, fixed to 3
105 // 29-24: Opcode
106 // 23-18: Resulting type plane
107 // 17-12: Operand #1
108 // 11- 6: Operand #2
109 // 5- 0: Operand #3
110 //
111 unsigned Opcode = (3 << 30) | (IType << 24) | (Type << 18) |
112 (Slots[0] << 12) | (Slots[1] << 6) | (Slots[2] << 0);
113 // cerr << "3 " << IType << " " << Type << " " << Slots[0] << " "
114 // << Slots[1] << " " << Slots[2] << endl;
115 output(Opcode, Out);
116}
117
118bool BytecodeWriter::processInstruction(const Instruction *I) {
119 assert(I->getInstType() < 64 && "Opcode too big???");
120
121 unsigned NumOperands = 0;
122 int MaxOpSlot = 0;
123 int Slots[3]; Slots[0] = (1 << 12)-1;
124
125 const Value *Def;
126 while ((Def = I->getOperand(NumOperands))) {
127 int slot = Table.getValSlot(Def);
128 assert(slot != -1 && "Broken bytecode!");
129 if (slot > MaxOpSlot) MaxOpSlot = slot;
130 if (NumOperands < 3) Slots[NumOperands] = slot;
131 NumOperands++;
132 }
133
134 // Figure out which type to encode with the instruction. Typically we want
135 // the type of the first parameter, as opposed to the type of the instruction
136 // (for example, with setcc, we always know it returns bool, but the type of
137 // the first param is actually interesting). But if we have no arguments
138 // we take the type of the instruction itself.
139 //
140
141 const Type *Ty;
142 if (NumOperands)
143 Ty = I->getOperand(0)->getType();
144 else
145 Ty = I->getType();
146
147 unsigned Type;
148 int Slot = Table.getValSlot(Ty);
149 assert(Slot != -1 && "Type not available!!?!");
150 Type = (unsigned)Slot;
151
152
153 // Decide which instruction encoding to use. This is determined primarily by
154 // the number of operands, and secondarily by whether or not the max operand
155 // will fit into the instruction encoding. More operands == fewer bits per
156 // operand.
157 //
158 switch (NumOperands) {
159 case 0:
160 case 1:
161 if (MaxOpSlot < (1 << 12)-1) { // -1 because we use 4095 to indicate 0 ops
162 outputInstructionFormat1(I, Table, Slots, Type, Out);
163 return false;
164 }
165 break;
166
167 case 2:
168 if (MaxOpSlot < (1 << 8)) {
169 outputInstructionFormat2(I, Table, Slots, Type, Out);
170 return false;
171 }
172 break;
173
174 case 3:
175 if (MaxOpSlot < (1 << 6)) {
176 outputInstructionFormat3(I, Table, Slots, Type, Out);
177 return false;
178 }
179 break;
180 }
181
182 outputInstructionFormat0(I, Table, Type, Out);
183 return false;
184}