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