blob: 15b35d5d5dbc8a7d875e3b9a7c84709b6f902666 [file] [log] [blame]
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001//===- CodeEmitterGen.cpp - Code Emitter Generator ------------------------===//
Misha Brukman650ba8e2005-04-22 00:00:37 +00002//
John Criswelld3032032003-10-20 20:20:30 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman650ba8e2005-04-22 00:00:37 +00007//
John Criswelld3032032003-10-20 20:20:30 +00008//===----------------------------------------------------------------------===//
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00009//
Misha Brukman8e5492e2004-08-04 22:07:54 +000010// CodeEmitterGen uses the descriptions of instructions and their fields to
11// construct an automated code emitter: a function that, given a MachineInstr,
12// returns the (currently, 32-bit unsigned) value of the instruction.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000013//
14//===----------------------------------------------------------------------===//
15
16#include "CodeEmitterGen.h"
Misha Brukman920ae952004-08-09 19:10:43 +000017#include "CodeGenTarget.h"
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000018#include "Record.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000019#include "llvm/Support/Debug.h"
Chris Lattner68478662004-08-01 03:55:39 +000020using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000021
Misha Brukman8393c152004-10-14 05:53:01 +000022void CodeEmitterGen::emitInstrOpBits(std::ostream &o,
23 const std::vector<RecordVal> &Vals,
24 std::map<std::string, unsigned> &OpOrder,
25 std::map<std::string, bool> &OpContinuous)
26{
27 for (unsigned f = 0, e = Vals.size(); f != e; ++f) {
28 if (Vals[f].getPrefix()) {
29 BitsInit *FieldInitializer = (BitsInit*)Vals[f].getValue();
30
31 // Scan through the field looking for bit initializers of the current
32 // variable...
33 for (int i = FieldInitializer->getNumBits()-1; i >= 0; --i) {
34 Init *I = FieldInitializer->getBit(i);
35 if (BitInit *BI = dynamic_cast<BitInit*>(I)) {
36 DEBUG(o << " // bit init: f: " << f << ", i: " << i << "\n");
37 } else if (UnsetInit *UI = dynamic_cast<UnsetInit*>(I)) {
38 DEBUG(o << " // unset init: f: " << f << ", i: " << i << "\n");
39 } else if (VarBitInit *VBI = dynamic_cast<VarBitInit*>(I)) {
40 TypedInit *TI = VBI->getVariable();
41 if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
42 // If the bits of the field are laid out consecutively in the
43 // instruction, then instead of separately ORing in bits, just
44 // mask and shift the entire field for efficiency.
45 if (OpContinuous[VI->getName()]) {
46 // already taken care of in the loop above, thus there is no
47 // need to individually OR in the bits
48
49 // for debugging, output the regular version anyway, commented
50 DEBUG(o << " // Value |= getValueBit(op"
51 << OpOrder[VI->getName()] << ", " << VBI->getBitNum()
52 << ")" << " << " << i << ";\n");
53 } else {
54 o << " Value |= getValueBit(op" << OpOrder[VI->getName()]
55 << ", " << VBI->getBitNum()
56 << ")" << " << " << i << ";\n";
57 }
58 } else if (FieldInit *FI = dynamic_cast<FieldInit*>(TI)) {
59 // FIXME: implement this!
60 std::cerr << "Error: FieldInit not implemented!\n";
61 abort();
62 } else {
63 std::cerr << "Error: unimplemented case in "
64 << "CodeEmitterGen::emitInstrOpBits()\n";
65 abort();
66 }
67 }
68 }
69 }
70 }
71}
72
73
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000074void CodeEmitterGen::run(std::ostream &o) {
Misha Brukman59978332004-08-10 18:31:01 +000075 CodeGenTarget Target;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000076 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
77
78 EmitSourceFileHeader("Machine Code Emitter", o);
Misha Brukman59978332004-08-10 18:31:01 +000079 std::string Namespace = Insts[0]->getValueAsString("Namespace") + "::";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000080
Misha Brukman422d0fa2004-08-10 20:54:58 +000081 // Emit function declaration
Misha Brukman59978332004-08-10 18:31:01 +000082 o << "unsigned " << Target.getName() << "CodeEmitter::"
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000083 << "getBinaryCodeForInstr(MachineInstr &MI) {\n"
84 << " unsigned Value = 0;\n"
85 << " DEBUG(std::cerr << MI);\n"
86 << " switch (MI.getOpcode()) {\n";
Misha Brukman422d0fa2004-08-10 20:54:58 +000087
88 // Emit a case statement for each opcode
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000089 for (std::vector<Record*>::iterator I = Insts.begin(), E = Insts.end();
90 I != E; ++I) {
91 Record *R = *I;
Chris Lattnerec249a32006-01-27 01:39:38 +000092 if (R->getName() == "PHI" || R->getName() == "INLINEASM") continue;
93
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000094 o << " case " << Namespace << R->getName() << ": {\n"
95 << " DEBUG(std::cerr << \"Emitting " << R->getName() << "\\n\");\n";
96
97 BitsInit *BI = R->getValueAsBitsInit("Inst");
98
Misha Brukman8393c152004-10-14 05:53:01 +000099 // For little-endian instruction bit encodings, reverse the bit order
100 if (Target.isLittleEndianEncoding()) {
101 unsigned numBits = BI->getNumBits();
102 BitsInit *NewBI = new BitsInit(numBits);
103 for (unsigned bit = 0, end = numBits / 2; bit != end; ++bit) {
104 unsigned bitSwapIdx = numBits - bit - 1;
105 Init *OrigBit = BI->getBit(bit);
106 Init *BitSwap = BI->getBit(bitSwapIdx);
107 NewBI->setBit(bit, BitSwap);
108 NewBI->setBit(bitSwapIdx, OrigBit);
109 }
110 if (numBits % 2) {
111 unsigned middle = (numBits + 1) / 2;
112 NewBI->setBit(middle, BI->getBit(middle));
113 }
114 BI = NewBI;
Chris Lattnerf878f6a2006-03-18 00:40:36 +0000115
116 // Update the bits in reversed order so that emitInstrOpBits will get the
117 // correct endianness.
118 R->getValue("Inst")->setValue(NewBI);
Misha Brukman8393c152004-10-14 05:53:01 +0000119 }
Misha Brukman650ba8e2005-04-22 00:00:37 +0000120
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000121 unsigned Value = 0;
122 const std::vector<RecordVal> &Vals = R->getValues();
123
124 DEBUG(o << " // prefilling: ");
125 // Start by filling in fixed values...
126 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) {
127 if (BitInit *B = dynamic_cast<BitInit*>(BI->getBit(e-i-1))) {
128 Value |= B->getValue() << (e-i-1);
129 DEBUG(o << B->getValue());
130 } else {
131 DEBUG(o << "0");
132 }
133 }
134 DEBUG(o << "\n");
135
136 DEBUG(o << " // " << *R->getValue("Inst") << "\n");
137 o << " Value = " << Value << "U;\n\n";
Misha Brukman650ba8e2005-04-22 00:00:37 +0000138
Misha Brukman8393c152004-10-14 05:53:01 +0000139 // Loop over all of the fields in the instruction, determining which are the
Misha Brukman650ba8e2005-04-22 00:00:37 +0000140 // operands to the instruction.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000141 unsigned op = 0;
142 std::map<std::string, unsigned> OpOrder;
143 std::map<std::string, bool> OpContinuous;
144 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
Misha Brukman8393c152004-10-14 05:53:01 +0000145 if (!Vals[i].getPrefix() && !Vals[i].getValue()->isComplete()) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000146 // Is the operand continuous? If so, we can just mask and OR it in
Misha Brukman650ba8e2005-04-22 00:00:37 +0000147 // instead of doing it bit-by-bit, saving a lot in runtime cost.
Misha Brukman8393c152004-10-14 05:53:01 +0000148 BitsInit *InstInit = BI;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000149 int beginBitInVar = -1, endBitInVar = -1;
150 int beginBitInInst = -1, endBitInInst = -1;
151 bool continuous = true;
152
153 for (int bit = InstInit->getNumBits()-1; bit >= 0; --bit) {
154 if (VarBitInit *VBI =
155 dynamic_cast<VarBitInit*>(InstInit->getBit(bit))) {
156 TypedInit *TI = VBI->getVariable();
157 if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
158 // only process the current variable
159 if (VI->getName() != Vals[i].getName())
160 continue;
161
162 if (beginBitInVar == -1)
163 beginBitInVar = VBI->getBitNum();
164
165 if (endBitInVar == -1)
166 endBitInVar = VBI->getBitNum();
167 else {
168 if (endBitInVar == (int)VBI->getBitNum() + 1)
169 endBitInVar = VBI->getBitNum();
170 else {
171 continuous = false;
172 break;
173 }
174 }
175
176 if (beginBitInInst == -1)
177 beginBitInInst = bit;
178 if (endBitInInst == -1)
179 endBitInInst = bit;
180 else {
181 if (endBitInInst == bit + 1)
182 endBitInInst = bit;
183 else {
184 continuous = false;
185 break;
186 }
187 }
188
189 // maintain same distance between bits in field and bits in
190 // instruction. if the relative distances stay the same
191 // throughout,
192 if (beginBitInVar - (int)VBI->getBitNum() !=
193 beginBitInInst - bit) {
194 continuous = false;
195 break;
196 }
197 }
198 }
199 }
200
201 // If we have found no bit in "Inst" which comes from this field, then
202 // this is not an operand!!
203 if (beginBitInInst != -1) {
204 o << " // op" << op << ": " << Vals[i].getName() << "\n"
Chris Lattner04a5ae32005-08-19 01:04:33 +0000205 << " int op" << op
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000206 <<" = getMachineOpValue(MI, MI.getOperand("<<op<<"));\n";
207 //<< " MachineOperand &op" << op <<" = MI.getOperand("<<op<<");\n";
208 OpOrder[Vals[i].getName()] = op++;
Misha Brukman650ba8e2005-04-22 00:00:37 +0000209
210 DEBUG(o << " // Var: begin = " << beginBitInVar
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000211 << ", end = " << endBitInVar
212 << "; Inst: begin = " << beginBitInInst
213 << ", end = " << endBitInInst << "\n");
Misha Brukman650ba8e2005-04-22 00:00:37 +0000214
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000215 if (continuous) {
216 DEBUG(o << " // continuous: op" << OpOrder[Vals[i].getName()]
217 << "\n");
Misha Brukman650ba8e2005-04-22 00:00:37 +0000218
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000219 // Mask off the right bits
220 // Low mask (ie. shift, if necessary)
221 assert(endBitInVar >= 0 && "Negative shift amount in masking!");
222 if (endBitInVar != 0) {
223 o << " op" << OpOrder[Vals[i].getName()]
224 << " >>= " << endBitInVar << ";\n";
225 beginBitInVar -= endBitInVar;
226 endBitInVar = 0;
227 }
Misha Brukman650ba8e2005-04-22 00:00:37 +0000228
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000229 // High mask
230 o << " op" << OpOrder[Vals[i].getName()]
231 << " &= (1<<" << beginBitInVar+1 << ") - 1;\n";
Misha Brukman650ba8e2005-04-22 00:00:37 +0000232
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000233 // Shift the value to the correct place (according to place in inst)
Misha Brukman8e5492e2004-08-04 22:07:54 +0000234 assert(endBitInInst >= 0 && "Negative shift amount!");
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000235 if (endBitInInst != 0)
236 o << " op" << OpOrder[Vals[i].getName()]
237 << " <<= " << endBitInInst << ";\n";
Misha Brukman650ba8e2005-04-22 00:00:37 +0000238
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000239 // Just OR in the result
240 o << " Value |= op" << OpOrder[Vals[i].getName()] << ";\n";
241 }
Misha Brukman650ba8e2005-04-22 00:00:37 +0000242
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000243 // otherwise, will be taken care of in the loop below using this
244 // value:
245 OpContinuous[Vals[i].getName()] = continuous;
246 }
247 }
248 }
249
Misha Brukman8393c152004-10-14 05:53:01 +0000250 emitInstrOpBits(o, Vals, OpOrder, OpContinuous);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000251
252 o << " break;\n"
253 << " }\n";
254 }
255
Misha Brukman8393c152004-10-14 05:53:01 +0000256 // Default case: unhandled opcode
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000257 o << " default:\n"
258 << " std::cerr << \"Not supported instr: \" << MI << \"\\n\";\n"
259 << " abort();\n"
260 << " }\n"
261 << " return Value;\n"
Misha Brukman8393c152004-10-14 05:53:01 +0000262 << "}\n\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000263}