blob: bd3d2ffea57c7555a68d30567720923765b4793c [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//
10// FIXME: Document.
11//
12//===----------------------------------------------------------------------===//
13
Misha Brukman9fff7e12003-05-24 00:15:53 +000014#include "CodeEmitterGen.h"
Chris Lattnerc9670ef2003-07-31 04:43:49 +000015#include "Record.h"
Chris Lattnerc648dab2003-08-01 22:13:59 +000016#include "Support/Debug.h"
Misha Brukman9fff7e12003-05-24 00:15:53 +000017
Chris Lattner048c00d2003-08-01 04:38:18 +000018void CodeEmitterGen::run(std::ostream &o) {
19 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
Misha Brukman9fff7e12003-05-24 00:15:53 +000020
Chris Lattner0e5e49e2003-08-06 04:36:35 +000021 EmitSourceFileHeader("Machine Code Emitter", o);
22
Misha Brukman9fff7e12003-05-24 00:15:53 +000023 std::string Namespace = "V9::";
24 std::string ClassName = "SparcV9CodeEmitter::";
25
26 //const std::string &Namespace = Inst->getValue("Namespace")->getName();
Misha Brukmanb9dd8152003-05-27 22:29:02 +000027 o << "unsigned " << ClassName
Misha Brukman9fff7e12003-05-24 00:15:53 +000028 << "getBinaryCodeForInstr(MachineInstr &MI) {\n"
29 << " unsigned Value = 0;\n"
Misha Brukmanf4ef4c82003-06-06 00:27:02 +000030 << " DEBUG(std::cerr << MI);\n"
Misha Brukman9fff7e12003-05-24 00:15:53 +000031 << " switch (MI.getOpcode()) {\n";
32 for (std::vector<Record*>::iterator I = Insts.begin(), E = Insts.end();
Chris Lattnercf1b5852003-08-01 04:15:25 +000033 I != E; ++I) {
Misha Brukman9fff7e12003-05-24 00:15:53 +000034 Record *R = *I;
Misha Brukmanecc7fd32003-05-28 18:29:10 +000035 o << " case " << Namespace << R->getName() << ": {\n"
Misha Brukmanf4ef4c82003-06-06 00:27:02 +000036 << " DEBUG(std::cerr << \"Emitting " << R->getName() << "\\n\");\n";
Misha Brukman9fff7e12003-05-24 00:15:53 +000037
Chris Lattner6f334ad2003-08-01 04:46:24 +000038 BitsInit *BI = R->getValueAsBitsInit("Inst");
Misha Brukman9fff7e12003-05-24 00:15:53 +000039
40 unsigned Value = 0;
41 const std::vector<RecordVal> &Vals = R->getValues();
42
Misha Brukmanf6e52172003-07-15 21:26:09 +000043 DEBUG(o << " // prefilling: ");
Misha Brukman9fff7e12003-05-24 00:15:53 +000044 // Start by filling in fixed values...
Misha Brukmancbfde0a2003-05-27 22:19:58 +000045 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) {
46 if (BitInit *B = dynamic_cast<BitInit*>(BI->getBit(e-i-1))) {
47 Value |= B->getValue() << (e-i-1);
Misha Brukmanf6e52172003-07-15 21:26:09 +000048 DEBUG(o << B->getValue());
Misha Brukmancbfde0a2003-05-27 22:19:58 +000049 } else {
Misha Brukmanf6e52172003-07-15 21:26:09 +000050 DEBUG(o << "0");
Misha Brukmancbfde0a2003-05-27 22:19:58 +000051 }
52 }
Misha Brukmanf6e52172003-07-15 21:26:09 +000053 DEBUG(o << "\n");
Misha Brukman9fff7e12003-05-24 00:15:53 +000054
Chris Lattner6f334ad2003-08-01 04:46:24 +000055 DEBUG(o << " // " << *R->getValue("Inst") << "\n");
Misha Brukmancbfde0a2003-05-27 22:19:58 +000056 o << " Value = " << Value << "U;\n\n";
Misha Brukman9fff7e12003-05-24 00:15:53 +000057
Chris Lattnerd7efef92003-08-05 03:53:04 +000058 // Loop over all of the fields in the instruction determining which are the
59 // operands to the instruction.
Misha Brukman9fff7e12003-05-24 00:15:53 +000060 //
Misha Brukmancbfde0a2003-05-27 22:19:58 +000061 unsigned op = 0;
Chris Lattnerd7efef92003-08-05 03:53:04 +000062 std::map<std::string, unsigned> OpOrder;
63 std::map<std::string, bool> OpContinuous;
Misha Brukman9fff7e12003-05-24 00:15:53 +000064 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
Misha Brukmand88ba5a2003-08-05 14:35:35 +000065 if (!Vals[i].getPrefix() && !Vals[i].getValue()->isComplete()) {
Misha Brukman7eac4762003-07-15 21:00:32 +000066 // Is the operand continuous? If so, we can just mask and OR it in
67 // instead of doing it bit-by-bit, saving a lot in runtime cost.
68 const BitsInit *InstInit = BI;
Chris Lattnerd7efef92003-08-05 03:53:04 +000069 int beginBitInVar = -1, endBitInVar = -1;
70 int beginBitInInst = -1, endBitInInst = -1;
Misha Brukman7eac4762003-07-15 21:00:32 +000071 bool continuous = true;
72
73 for (int bit = InstInit->getNumBits()-1; bit >= 0; --bit) {
74 if (VarBitInit *VBI =
75 dynamic_cast<VarBitInit*>(InstInit->getBit(bit))) {
76 TypedInit *TI = VBI->getVariable();
77 if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
78 // only process the current variable
79 if (VI->getName() != Vals[i].getName())
80 continue;
81
82 if (beginBitInVar == -1)
83 beginBitInVar = VBI->getBitNum();
84
85 if (endBitInVar == -1)
86 endBitInVar = VBI->getBitNum();
87 else {
88 if (endBitInVar == (int)VBI->getBitNum() + 1)
89 endBitInVar = VBI->getBitNum();
90 else {
91 continuous = false;
92 break;
93 }
94 }
95
96 if (beginBitInInst == -1)
97 beginBitInInst = bit;
98 if (endBitInInst == -1)
99 endBitInInst = bit;
100 else {
101 if (endBitInInst == bit + 1)
102 endBitInInst = bit;
103 else {
104 continuous = false;
105 break;
106 }
107 }
108
109 // maintain same distance between bits in field and bits in
110 // instruction. if the relative distances stay the same
111 // throughout,
Chris Lattnerd7efef92003-08-05 03:53:04 +0000112 if (beginBitInVar - (int)VBI->getBitNum() !=
113 beginBitInInst - bit) {
Misha Brukman7eac4762003-07-15 21:00:32 +0000114 continuous = false;
115 break;
116 }
117 }
118 }
119 }
120
Chris Lattnerffaee372003-08-05 03:59:01 +0000121 // If we have found no bit in "Inst" which comes from this field, then
122 // this is not an operand!!
Chris Lattnerd7efef92003-08-05 03:53:04 +0000123 if (beginBitInInst != -1) {
124 o << " // op" << op << ": " << Vals[i].getName() << "\n"
125 << " int64_t op" << op
126 <<" = getMachineOpValue(MI, MI.getOperand("<<op<<"));\n";
127 //<< " MachineOperand &op" << op <<" = MI.getOperand("<<op<<");\n";
128 OpOrder[Vals[i].getName()] = op++;
Misha Brukman7eac4762003-07-15 21:00:32 +0000129
Chris Lattnerd7efef92003-08-05 03:53:04 +0000130 DEBUG(o << " // Var: begin = " << beginBitInVar
131 << ", end = " << endBitInVar
132 << "; Inst: begin = " << beginBitInInst
133 << ", end = " << endBitInInst << "\n");
134
135 if (continuous) {
136 DEBUG(o << " // continuous: op" << OpOrder[Vals[i].getName()]
137 << "\n");
138
139 // Mask off the right bits
140 // Low mask (ie. shift, if necessary)
Misha Brukmandfd414a2003-08-06 16:28:49 +0000141 assert(endBitInVar >= 0 && "Negative shift amount in masking!");
Chris Lattnerd7efef92003-08-05 03:53:04 +0000142 if (endBitInVar != 0) {
143 o << " op" << OpOrder[Vals[i].getName()]
144 << " >>= " << endBitInVar << ";\n";
145 beginBitInVar -= endBitInVar;
146 endBitInVar = 0;
147 }
148
149 // High mask
Misha Brukman7eac4762003-07-15 21:00:32 +0000150 o << " op" << OpOrder[Vals[i].getName()]
Chris Lattnerd7efef92003-08-05 03:53:04 +0000151 << " &= (1<<" << beginBitInVar+1 << ") - 1;\n";
152
153 // Shift the value to the correct place (according to place in inst)
Misha Brukmandfd414a2003-08-06 16:28:49 +0000154 assert(endBitInInst >= 0 && "Negative shift amount in inst position!");
Chris Lattnerd7efef92003-08-05 03:53:04 +0000155 if (endBitInInst != 0)
156 o << " op" << OpOrder[Vals[i].getName()]
Misha Brukman7eac4762003-07-15 21:00:32 +0000157 << " <<= " << endBitInInst << ";\n";
Chris Lattnerd7efef92003-08-05 03:53:04 +0000158
159 // Just OR in the result
160 o << " Value |= op" << OpOrder[Vals[i].getName()] << ";\n";
161 }
162
163 // otherwise, will be taken care of in the loop below using this
164 // value:
165 OpContinuous[Vals[i].getName()] = continuous;
Misha Brukman7eac4762003-07-15 21:00:32 +0000166 }
Misha Brukman9fff7e12003-05-24 00:15:53 +0000167 }
168 }
169
Misha Brukman48aa8242003-07-07 22:30:44 +0000170 for (unsigned f = 0, e = Vals.size(); f != e; ++f) {
Misha Brukman9fff7e12003-05-24 00:15:53 +0000171 if (Vals[f].getPrefix()) {
172 BitsInit *FieldInitializer = (BitsInit*)Vals[f].getValue();
173
174 // Scan through the field looking for bit initializers of the current
175 // variable...
176 for (int i = FieldInitializer->getNumBits()-1; i >= 0; --i) {
Chris Lattnerffaee372003-08-05 03:59:01 +0000177 if (BitInit *BI = dynamic_cast<BitInit*>(FieldInitializer->getBit(i)))
Misha Brukman7eac4762003-07-15 21:00:32 +0000178 {
Misha Brukmanf6e52172003-07-15 21:26:09 +0000179 DEBUG(o << " // bit init: f: " << f << ", i: " << i << "\n");
Misha Brukman7eac4762003-07-15 21:00:32 +0000180 } else if (UnsetInit *UI =
Misha Brukman9fff7e12003-05-24 00:15:53 +0000181 dynamic_cast<UnsetInit*>(FieldInitializer->getBit(i))) {
Misha Brukmanf6e52172003-07-15 21:26:09 +0000182 DEBUG(o << " // unset init: f: " << f << ", i: " << i << "\n");
Misha Brukman9fff7e12003-05-24 00:15:53 +0000183 } else if (VarBitInit *VBI =
184 dynamic_cast<VarBitInit*>(FieldInitializer->getBit(i))) {
185 TypedInit *TI = VBI->getVariable();
186 if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
Misha Brukman7eac4762003-07-15 21:00:32 +0000187 // If the bits of the field are laid out consecutively in the
188 // instruction, then instead of separately ORing in bits, just
189 // mask and shift the entire field for efficiency.
190 if (OpContinuous[VI->getName()]) {
191 // already taken care of in the loop above, thus there is no
192 // need to individually OR in the bits
193
194 // for debugging, output the regular version anyway, commented
Misha Brukmanf6e52172003-07-15 21:26:09 +0000195 DEBUG(o << " // Value |= getValueBit(op"
196 << OpOrder[VI->getName()] << ", " << VBI->getBitNum()
197 << ")" << " << " << i << ";\n");
Misha Brukman7eac4762003-07-15 21:00:32 +0000198 } else {
199 o << " Value |= getValueBit(op" << OpOrder[VI->getName()]
200 << ", " << VBI->getBitNum()
201 << ")" << " << " << i << ";\n";
202 }
Misha Brukman9fff7e12003-05-24 00:15:53 +0000203 } else if (FieldInit *FI = dynamic_cast<FieldInit*>(TI)) {
204 // FIXME: implement this!
205 o << "FIELD INIT not implemented yet!\n";
206 } else {
Misha Brukmancbfde0a2003-05-27 22:19:58 +0000207 o << "Error: UNIMPLEMENTED\n";
Misha Brukman9fff7e12003-05-24 00:15:53 +0000208 }
209 }
Misha Brukman7eac4762003-07-15 21:00:32 +0000210 }
Misha Brukman9fff7e12003-05-24 00:15:53 +0000211 }
212 }
213
214 o << " break;\n"
215 << " }\n";
216 }
Misha Brukman7eac4762003-07-15 21:00:32 +0000217
Misha Brukmancbfde0a2003-05-27 22:19:58 +0000218 o << " default:\n"
Misha Brukman0bb806b2003-09-17 18:21:48 +0000219 << " std::cerr << \"Not supported instr: \" << MI << \"\\n\";\n"
Misha Brukmancbfde0a2003-05-27 22:19:58 +0000220 << " abort();\n"
221 << " }\n"
Misha Brukman9fff7e12003-05-24 00:15:53 +0000222 << " return Value;\n"
223 << "}\n";
Misha Brukman9fff7e12003-05-24 00:15:53 +0000224}