1 //===-- WriteInst.cpp - Functions for writing instructions -------*- C++ -*--=//
3 // This file implements the routines for encoding instruction opcodes to a
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
10 //===----------------------------------------------------------------------===//
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"
21 typedef unsigned char uchar;
23 // outputInstructionFormat0 - Output those wierd instructions that have a large
24 // number of operands or have large operands themselves...
26 // Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>]
28 static 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
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);
39 for (unsigned i = 0; const Value *N = I->getOperand(i); i++) {
40 assert(i < NumArgs && "Count of arguments failed!");
42 int Slot = Table.getValSlot(N);
43 output_vbr((unsigned)Slot, Out);
45 align32(Out); // We must maintain correct alignment!
49 // outputInstructionFormat1 - Output one operand instructions, knowing that no
50 // operand index is >= 2^12.
52 static 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
57 // bits Instruction format:
58 // --------------------------
59 // 31-30: Opcode type, fixed to 1.
61 // 23-12: Resulting type plane
62 // 11- 0: Operand #1 (if set to (2^12-1), then zero operands)
64 unsigned Opcode = (1 << 30) | (IType << 24) | (Type << 12) | Slots[0];
65 // cerr << "1 " << IType << " " << Type << " " << Slots[0] << endl;
70 // outputInstructionFormat2 - Output two operand instructions, knowing that no
71 // operand index is >= 2^8.
73 static 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
78 // bits Instruction format:
79 // --------------------------
80 // 31-30: Opcode type, fixed to 2.
82 // 23-16: Resulting type plane
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;
94 // outputInstructionFormat3 - Output three operand instructions, knowing that no
95 // operand index is >= 2^6.
97 static 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
102 // bits Instruction format:
103 // --------------------------
104 // 31-30: Opcode type, fixed to 3
106 // 23-18: Resulting type plane
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;
118 bool BytecodeWriter::processInstruction(const Instruction *I) {
119 assert(I->getInstType() < 64 && "Opcode too big???");
121 unsigned NumOperands = 0;
123 int Slots[3]; Slots[0] = (1 << 12)-1;
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;
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.
143 Ty = I->getOperand(0)->getType();
148 int Slot = Table.getValSlot(Ty);
149 assert(Slot != -1 && "Type not available!!?!");
150 Type = (unsigned)Slot;
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
158 switch (NumOperands) {
161 if (MaxOpSlot < (1 << 12)-1) { // -1 because we use 4095 to indicate 0 ops
162 outputInstructionFormat1(I, Table, Slots, Type, Out);
168 if (MaxOpSlot < (1 << 8)) {
169 outputInstructionFormat2(I, Table, Slots, Type, Out);
175 if (MaxOpSlot < (1 << 6)) {
176 outputInstructionFormat3(I, Table, Slots, Type, Out);
182 outputInstructionFormat0(I, Table, Type, Out);