blob: a9e8261e25c098bf41a597b652445a301cc8e0cd [file] [log] [blame]
Chris Lattnerc9670ef2003-07-31 04:43:49 +00001//===- CodeEmitterGen.cpp - Code Emitter Generator ------------------------===//
John Criswell01d45822003-10-20 20:20:30 +00002//
3// 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.
7//
8//===----------------------------------------------------------------------===//
Chris Lattnerc9670ef2003-07-31 04:43:49 +00009//
Misha Brukman4e4f8632004-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 Lattnerc9670ef2003-07-31 04:43:49 +000013//
14//===----------------------------------------------------------------------===//
15
Misha Brukman9fff7e12003-05-24 00:15:53 +000016#include "CodeEmitterGen.h"
Misha Brukmand7a5b282004-08-09 19:10:43 +000017#include "CodeGenTarget.h"
Chris Lattnerc9670ef2003-07-31 04:43:49 +000018#include "Record.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000019#include "llvm/Support/Debug.h"
Chris Lattner2082ebe2004-08-01 03:55:39 +000020using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000021
Misha Brukman28eefa52004-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 Lattner048c00d2003-08-01 04:38:18 +000074void CodeEmitterGen::run(std::ostream &o) {
Misha Brukmane2ba7782004-08-10 18:31:01 +000075 CodeGenTarget Target;
Chris Lattner048c00d2003-08-01 04:38:18 +000076 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
Misha Brukman9fff7e12003-05-24 00:15:53 +000077
Chris Lattner0e5e49e2003-08-06 04:36:35 +000078 EmitSourceFileHeader("Machine Code Emitter", o);
Chris Lattner2c384132004-08-17 03:08:28 +000079 o << "namespace llvm {\n\n";
Misha Brukmane2ba7782004-08-10 18:31:01 +000080 std::string Namespace = Insts[0]->getValueAsString("Namespace") + "::";
Misha Brukman9fff7e12003-05-24 00:15:53 +000081
Misha Brukmanad346ad2004-08-10 20:54:58 +000082 // Emit function declaration
Misha Brukmane2ba7782004-08-10 18:31:01 +000083 o << "unsigned " << Target.getName() << "CodeEmitter::"
Misha Brukman9fff7e12003-05-24 00:15:53 +000084 << "getBinaryCodeForInstr(MachineInstr &MI) {\n"
85 << " unsigned Value = 0;\n"
Misha Brukmanf4ef4c82003-06-06 00:27:02 +000086 << " DEBUG(std::cerr << MI);\n"
Misha Brukman9fff7e12003-05-24 00:15:53 +000087 << " switch (MI.getOpcode()) {\n";
Misha Brukmanad346ad2004-08-10 20:54:58 +000088
89 // Emit a case statement for each opcode
Misha Brukman9fff7e12003-05-24 00:15:53 +000090 for (std::vector<Record*>::iterator I = Insts.begin(), E = Insts.end();
Chris Lattnercf1b5852003-08-01 04:15:25 +000091 I != E; ++I) {
Misha Brukman9fff7e12003-05-24 00:15:53 +000092 Record *R = *I;
Misha Brukmanecc7fd32003-05-28 18:29:10 +000093 o << " case " << Namespace << R->getName() << ": {\n"
Misha Brukmanf4ef4c82003-06-06 00:27:02 +000094 << " DEBUG(std::cerr << \"Emitting " << R->getName() << "\\n\");\n";
Misha Brukman9fff7e12003-05-24 00:15:53 +000095
Chris Lattner6f334ad2003-08-01 04:46:24 +000096 BitsInit *BI = R->getValueAsBitsInit("Inst");
Misha Brukman9fff7e12003-05-24 00:15:53 +000097
Misha Brukman28eefa52004-10-14 05:53:01 +000098 // For little-endian instruction bit encodings, reverse the bit order
99 if (Target.isLittleEndianEncoding()) {
100 unsigned numBits = BI->getNumBits();
101 BitsInit *NewBI = new BitsInit(numBits);
102 for (unsigned bit = 0, end = numBits / 2; bit != end; ++bit) {
103 unsigned bitSwapIdx = numBits - bit - 1;
104 Init *OrigBit = BI->getBit(bit);
105 Init *BitSwap = BI->getBit(bitSwapIdx);
106 NewBI->setBit(bit, BitSwap);
107 NewBI->setBit(bitSwapIdx, OrigBit);
108 }
109 if (numBits % 2) {
110 unsigned middle = (numBits + 1) / 2;
111 NewBI->setBit(middle, BI->getBit(middle));
112 }
113 BI = NewBI;
114 }
115
Misha Brukman9fff7e12003-05-24 00:15:53 +0000116 unsigned Value = 0;
117 const std::vector<RecordVal> &Vals = R->getValues();
118
Misha Brukmanf6e52172003-07-15 21:26:09 +0000119 DEBUG(o << " // prefilling: ");
Misha Brukman9fff7e12003-05-24 00:15:53 +0000120 // Start by filling in fixed values...
Misha Brukmancbfde0a2003-05-27 22:19:58 +0000121 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) {
122 if (BitInit *B = dynamic_cast<BitInit*>(BI->getBit(e-i-1))) {
123 Value |= B->getValue() << (e-i-1);
Misha Brukmanf6e52172003-07-15 21:26:09 +0000124 DEBUG(o << B->getValue());
Misha Brukmancbfde0a2003-05-27 22:19:58 +0000125 } else {
Misha Brukmanf6e52172003-07-15 21:26:09 +0000126 DEBUG(o << "0");
Misha Brukmancbfde0a2003-05-27 22:19:58 +0000127 }
128 }
Misha Brukmanf6e52172003-07-15 21:26:09 +0000129 DEBUG(o << "\n");
Misha Brukman9fff7e12003-05-24 00:15:53 +0000130
Chris Lattner6f334ad2003-08-01 04:46:24 +0000131 DEBUG(o << " // " << *R->getValue("Inst") << "\n");
Misha Brukmancbfde0a2003-05-27 22:19:58 +0000132 o << " Value = " << Value << "U;\n\n";
Misha Brukman9fff7e12003-05-24 00:15:53 +0000133
Misha Brukman28eefa52004-10-14 05:53:01 +0000134 // Loop over all of the fields in the instruction, determining which are the
Chris Lattnerd7efef92003-08-05 03:53:04 +0000135 // operands to the instruction.
Misha Brukmancbfde0a2003-05-27 22:19:58 +0000136 unsigned op = 0;
Chris Lattnerd7efef92003-08-05 03:53:04 +0000137 std::map<std::string, unsigned> OpOrder;
138 std::map<std::string, bool> OpContinuous;
Misha Brukman9fff7e12003-05-24 00:15:53 +0000139 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
Misha Brukman28eefa52004-10-14 05:53:01 +0000140 if (!Vals[i].getPrefix() && !Vals[i].getValue()->isComplete()) {
Misha Brukman7eac4762003-07-15 21:00:32 +0000141 // Is the operand continuous? If so, we can just mask and OR it in
142 // instead of doing it bit-by-bit, saving a lot in runtime cost.
Misha Brukman28eefa52004-10-14 05:53:01 +0000143 BitsInit *InstInit = BI;
Chris Lattnerd7efef92003-08-05 03:53:04 +0000144 int beginBitInVar = -1, endBitInVar = -1;
145 int beginBitInInst = -1, endBitInInst = -1;
Misha Brukman7eac4762003-07-15 21:00:32 +0000146 bool continuous = true;
147
148 for (int bit = InstInit->getNumBits()-1; bit >= 0; --bit) {
149 if (VarBitInit *VBI =
150 dynamic_cast<VarBitInit*>(InstInit->getBit(bit))) {
151 TypedInit *TI = VBI->getVariable();
152 if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
153 // only process the current variable
154 if (VI->getName() != Vals[i].getName())
155 continue;
156
157 if (beginBitInVar == -1)
158 beginBitInVar = VBI->getBitNum();
159
160 if (endBitInVar == -1)
161 endBitInVar = VBI->getBitNum();
162 else {
163 if (endBitInVar == (int)VBI->getBitNum() + 1)
164 endBitInVar = VBI->getBitNum();
165 else {
166 continuous = false;
167 break;
168 }
169 }
170
171 if (beginBitInInst == -1)
172 beginBitInInst = bit;
173 if (endBitInInst == -1)
174 endBitInInst = bit;
175 else {
176 if (endBitInInst == bit + 1)
177 endBitInInst = bit;
178 else {
179 continuous = false;
180 break;
181 }
182 }
183
184 // maintain same distance between bits in field and bits in
185 // instruction. if the relative distances stay the same
186 // throughout,
Chris Lattnerd7efef92003-08-05 03:53:04 +0000187 if (beginBitInVar - (int)VBI->getBitNum() !=
188 beginBitInInst - bit) {
Misha Brukman7eac4762003-07-15 21:00:32 +0000189 continuous = false;
190 break;
191 }
192 }
193 }
194 }
195
Chris Lattnerffaee372003-08-05 03:59:01 +0000196 // If we have found no bit in "Inst" which comes from this field, then
197 // this is not an operand!!
Chris Lattnerd7efef92003-08-05 03:53:04 +0000198 if (beginBitInInst != -1) {
199 o << " // op" << op << ": " << Vals[i].getName() << "\n"
200 << " int64_t op" << op
201 <<" = getMachineOpValue(MI, MI.getOperand("<<op<<"));\n";
202 //<< " MachineOperand &op" << op <<" = MI.getOperand("<<op<<");\n";
203 OpOrder[Vals[i].getName()] = op++;
Misha Brukman7eac4762003-07-15 21:00:32 +0000204
Chris Lattnerd7efef92003-08-05 03:53:04 +0000205 DEBUG(o << " // Var: begin = " << beginBitInVar
206 << ", end = " << endBitInVar
207 << "; Inst: begin = " << beginBitInInst
208 << ", end = " << endBitInInst << "\n");
209
210 if (continuous) {
211 DEBUG(o << " // continuous: op" << OpOrder[Vals[i].getName()]
212 << "\n");
213
214 // Mask off the right bits
215 // Low mask (ie. shift, if necessary)
Misha Brukmandfd414a2003-08-06 16:28:49 +0000216 assert(endBitInVar >= 0 && "Negative shift amount in masking!");
Chris Lattnerd7efef92003-08-05 03:53:04 +0000217 if (endBitInVar != 0) {
218 o << " op" << OpOrder[Vals[i].getName()]
219 << " >>= " << endBitInVar << ";\n";
220 beginBitInVar -= endBitInVar;
221 endBitInVar = 0;
222 }
223
224 // High mask
Misha Brukman7eac4762003-07-15 21:00:32 +0000225 o << " op" << OpOrder[Vals[i].getName()]
Chris Lattnerd7efef92003-08-05 03:53:04 +0000226 << " &= (1<<" << beginBitInVar+1 << ") - 1;\n";
227
228 // Shift the value to the correct place (according to place in inst)
Misha Brukman4e4f8632004-08-04 22:07:54 +0000229 assert(endBitInInst >= 0 && "Negative shift amount!");
Chris Lattnerd7efef92003-08-05 03:53:04 +0000230 if (endBitInInst != 0)
231 o << " op" << OpOrder[Vals[i].getName()]
Misha Brukman7eac4762003-07-15 21:00:32 +0000232 << " <<= " << endBitInInst << ";\n";
Chris Lattnerd7efef92003-08-05 03:53:04 +0000233
234 // Just OR in the result
235 o << " Value |= op" << OpOrder[Vals[i].getName()] << ";\n";
236 }
237
238 // otherwise, will be taken care of in the loop below using this
239 // value:
240 OpContinuous[Vals[i].getName()] = continuous;
Misha Brukman7eac4762003-07-15 21:00:32 +0000241 }
Misha Brukman9fff7e12003-05-24 00:15:53 +0000242 }
243 }
244
Misha Brukman28eefa52004-10-14 05:53:01 +0000245 emitInstrOpBits(o, Vals, OpOrder, OpContinuous);
Misha Brukman9fff7e12003-05-24 00:15:53 +0000246
247 o << " break;\n"
248 << " }\n";
249 }
Misha Brukman7eac4762003-07-15 21:00:32 +0000250
Misha Brukman28eefa52004-10-14 05:53:01 +0000251 // Default case: unhandled opcode
Misha Brukmancbfde0a2003-05-27 22:19:58 +0000252 o << " default:\n"
Misha Brukman0bb806b2003-09-17 18:21:48 +0000253 << " std::cerr << \"Not supported instr: \" << MI << \"\\n\";\n"
Misha Brukmancbfde0a2003-05-27 22:19:58 +0000254 << " abort();\n"
255 << " }\n"
Misha Brukman9fff7e12003-05-24 00:15:53 +0000256 << " return Value;\n"
Misha Brukman28eefa52004-10-14 05:53:01 +0000257 << "}\n\n";
Brian Gaeked0fde302003-11-11 22:41:34 +0000258
Chris Lattner2c384132004-08-17 03:08:28 +0000259 o << "} // End llvm namespace \n";
Misha Brukman9fff7e12003-05-24 00:15:53 +0000260}