blob: 906f4d6c7244b40236cc0d9f1627573d4aff20a9 [file] [log] [blame]
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001//===- CodeEmitterGen.cpp - Code Emitter Generator ------------------------===//
John Criswelld3032032003-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 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"
19#include "Support/Debug.h"
Chris Lattner68478662004-08-01 03:55:39 +000020using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000021
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000022void CodeEmitterGen::run(std::ostream &o) {
Misha Brukman59978332004-08-10 18:31:01 +000023 CodeGenTarget Target;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000024 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
25
26 EmitSourceFileHeader("Machine Code Emitter", o);
Chris Lattnerc9d99ef2004-08-17 03:08:28 +000027 o << "namespace llvm {\n\n";
Misha Brukman59978332004-08-10 18:31:01 +000028 std::string Namespace = Insts[0]->getValueAsString("Namespace") + "::";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000029
Misha Brukman422d0fa2004-08-10 20:54:58 +000030 // Emit function declaration
Misha Brukman59978332004-08-10 18:31:01 +000031 o << "unsigned " << Target.getName() << "CodeEmitter::"
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000032 << "getBinaryCodeForInstr(MachineInstr &MI) {\n"
33 << " unsigned Value = 0;\n"
34 << " DEBUG(std::cerr << MI);\n"
35 << " switch (MI.getOpcode()) {\n";
Misha Brukman422d0fa2004-08-10 20:54:58 +000036
37 // Emit a case statement for each opcode
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000038 for (std::vector<Record*>::iterator I = Insts.begin(), E = Insts.end();
39 I != E; ++I) {
40 Record *R = *I;
41 o << " case " << Namespace << R->getName() << ": {\n"
42 << " DEBUG(std::cerr << \"Emitting " << R->getName() << "\\n\");\n";
43
44 BitsInit *BI = R->getValueAsBitsInit("Inst");
45
46 unsigned Value = 0;
47 const std::vector<RecordVal> &Vals = R->getValues();
48
49 DEBUG(o << " // prefilling: ");
50 // Start by filling in fixed values...
51 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) {
52 if (BitInit *B = dynamic_cast<BitInit*>(BI->getBit(e-i-1))) {
53 Value |= B->getValue() << (e-i-1);
54 DEBUG(o << B->getValue());
55 } else {
56 DEBUG(o << "0");
57 }
58 }
59 DEBUG(o << "\n");
60
61 DEBUG(o << " // " << *R->getValue("Inst") << "\n");
62 o << " Value = " << Value << "U;\n\n";
63
64 // Loop over all of the fields in the instruction determining which are the
65 // operands to the instruction.
66 //
67 unsigned op = 0;
68 std::map<std::string, unsigned> OpOrder;
69 std::map<std::string, bool> OpContinuous;
70 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
71 if (!Vals[i].getPrefix() && !Vals[i].getValue()->isComplete()) {
72 // Is the operand continuous? If so, we can just mask and OR it in
73 // instead of doing it bit-by-bit, saving a lot in runtime cost.
74 const BitsInit *InstInit = BI;
75 int beginBitInVar = -1, endBitInVar = -1;
76 int beginBitInInst = -1, endBitInInst = -1;
77 bool continuous = true;
78
79 for (int bit = InstInit->getNumBits()-1; bit >= 0; --bit) {
80 if (VarBitInit *VBI =
81 dynamic_cast<VarBitInit*>(InstInit->getBit(bit))) {
82 TypedInit *TI = VBI->getVariable();
83 if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
84 // only process the current variable
85 if (VI->getName() != Vals[i].getName())
86 continue;
87
88 if (beginBitInVar == -1)
89 beginBitInVar = VBI->getBitNum();
90
91 if (endBitInVar == -1)
92 endBitInVar = VBI->getBitNum();
93 else {
94 if (endBitInVar == (int)VBI->getBitNum() + 1)
95 endBitInVar = VBI->getBitNum();
96 else {
97 continuous = false;
98 break;
99 }
100 }
101
102 if (beginBitInInst == -1)
103 beginBitInInst = bit;
104 if (endBitInInst == -1)
105 endBitInInst = bit;
106 else {
107 if (endBitInInst == bit + 1)
108 endBitInInst = bit;
109 else {
110 continuous = false;
111 break;
112 }
113 }
114
115 // maintain same distance between bits in field and bits in
116 // instruction. if the relative distances stay the same
117 // throughout,
118 if (beginBitInVar - (int)VBI->getBitNum() !=
119 beginBitInInst - bit) {
120 continuous = false;
121 break;
122 }
123 }
124 }
125 }
126
127 // If we have found no bit in "Inst" which comes from this field, then
128 // this is not an operand!!
129 if (beginBitInInst != -1) {
130 o << " // op" << op << ": " << Vals[i].getName() << "\n"
131 << " int64_t op" << op
132 <<" = getMachineOpValue(MI, MI.getOperand("<<op<<"));\n";
133 //<< " MachineOperand &op" << op <<" = MI.getOperand("<<op<<");\n";
134 OpOrder[Vals[i].getName()] = op++;
135
136 DEBUG(o << " // Var: begin = " << beginBitInVar
137 << ", end = " << endBitInVar
138 << "; Inst: begin = " << beginBitInInst
139 << ", end = " << endBitInInst << "\n");
140
141 if (continuous) {
142 DEBUG(o << " // continuous: op" << OpOrder[Vals[i].getName()]
143 << "\n");
144
145 // Mask off the right bits
146 // Low mask (ie. shift, if necessary)
147 assert(endBitInVar >= 0 && "Negative shift amount in masking!");
148 if (endBitInVar != 0) {
149 o << " op" << OpOrder[Vals[i].getName()]
150 << " >>= " << endBitInVar << ";\n";
151 beginBitInVar -= endBitInVar;
152 endBitInVar = 0;
153 }
154
155 // High mask
156 o << " op" << OpOrder[Vals[i].getName()]
157 << " &= (1<<" << beginBitInVar+1 << ") - 1;\n";
158
159 // Shift the value to the correct place (according to place in inst)
Misha Brukman8e5492e2004-08-04 22:07:54 +0000160 assert(endBitInInst >= 0 && "Negative shift amount!");
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000161 if (endBitInInst != 0)
162 o << " op" << OpOrder[Vals[i].getName()]
163 << " <<= " << endBitInInst << ";\n";
164
165 // Just OR in the result
166 o << " Value |= op" << OpOrder[Vals[i].getName()] << ";\n";
167 }
168
169 // otherwise, will be taken care of in the loop below using this
170 // value:
171 OpContinuous[Vals[i].getName()] = continuous;
172 }
173 }
174 }
175
176 for (unsigned f = 0, e = Vals.size(); f != e; ++f) {
177 if (Vals[f].getPrefix()) {
178 BitsInit *FieldInitializer = (BitsInit*)Vals[f].getValue();
179
180 // Scan through the field looking for bit initializers of the current
181 // variable...
182 for (int i = FieldInitializer->getNumBits()-1; i >= 0; --i) {
Misha Brukman18d27dc2004-08-09 17:47:45 +0000183 Init *I = FieldInitializer->getBit(i);
184 if (BitInit *BI = dynamic_cast<BitInit*>(I)) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000185 DEBUG(o << " // bit init: f: " << f << ", i: " << i << "\n");
Misha Brukman18d27dc2004-08-09 17:47:45 +0000186 } else if (UnsetInit *UI = dynamic_cast<UnsetInit*>(I)) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000187 DEBUG(o << " // unset init: f: " << f << ", i: " << i << "\n");
Misha Brukman18d27dc2004-08-09 17:47:45 +0000188 } else if (VarBitInit *VBI = dynamic_cast<VarBitInit*>(I)) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000189 TypedInit *TI = VBI->getVariable();
190 if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
191 // If the bits of the field are laid out consecutively in the
192 // instruction, then instead of separately ORing in bits, just
193 // mask and shift the entire field for efficiency.
194 if (OpContinuous[VI->getName()]) {
195 // already taken care of in the loop above, thus there is no
196 // need to individually OR in the bits
197
198 // for debugging, output the regular version anyway, commented
199 DEBUG(o << " // Value |= getValueBit(op"
200 << OpOrder[VI->getName()] << ", " << VBI->getBitNum()
201 << ")" << " << " << i << ";\n");
202 } else {
203 o << " Value |= getValueBit(op" << OpOrder[VI->getName()]
204 << ", " << VBI->getBitNum()
205 << ")" << " << " << i << ";\n";
206 }
207 } else if (FieldInit *FI = dynamic_cast<FieldInit*>(TI)) {
208 // FIXME: implement this!
209 o << "FIELD INIT not implemented yet!\n";
210 } else {
211 o << "Error: UNIMPLEMENTED\n";
212 }
213 }
214 }
215 }
216 }
217
218 o << " break;\n"
219 << " }\n";
220 }
221
222 o << " default:\n"
223 << " std::cerr << \"Not supported instr: \" << MI << \"\\n\";\n"
224 << " abort();\n"
225 << " }\n"
226 << " return Value;\n"
227 << "}\n";
Brian Gaeke960707c2003-11-11 22:41:34 +0000228
Chris Lattnerc9d99ef2004-08-17 03:08:28 +0000229 o << "} // End llvm namespace \n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000230}