blob: 861c6b0e34c49694e0a28cfea4a5c739db36e59a [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"
Jim Laskeya44f6262006-07-13 21:02:53 +000019#include "llvm/ADT/StringExtras.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000020#include "llvm/Support/Debug.h"
Chris Lattner68478662004-08-01 03:55:39 +000021using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000022
Jim Laskeya44f6262006-07-13 21:02:53 +000023void CodeEmitterGen::reverseBits(std::vector<Record*> &Insts) {
24 for (std::vector<Record*>::iterator I = Insts.begin(), E = Insts.end();
25 I != E; ++I) {
26 Record *R = *I;
27 if (R->getName() == "PHI" || R->getName() == "INLINEASM") continue;
28
29 BitsInit *BI = R->getValueAsBitsInit("Inst");
Misha Brukman8393c152004-10-14 05:53:01 +000030
Jim Laskeya44f6262006-07-13 21:02:53 +000031 unsigned numBits = BI->getNumBits();
32 BitsInit *NewBI = new BitsInit(numBits);
33 for (unsigned bit = 0, end = numBits / 2; bit != end; ++bit) {
34 unsigned bitSwapIdx = numBits - bit - 1;
35 Init *OrigBit = BI->getBit(bit);
36 Init *BitSwap = BI->getBit(bitSwapIdx);
37 NewBI->setBit(bit, BitSwap);
38 NewBI->setBit(bitSwapIdx, OrigBit);
Misha Brukman8393c152004-10-14 05:53:01 +000039 }
Jim Laskeya44f6262006-07-13 21:02:53 +000040 if (numBits % 2) {
41 unsigned middle = (numBits + 1) / 2;
42 NewBI->setBit(middle, BI->getBit(middle));
43 }
44
45 // Update the bits in reversed order so that emitInstrOpBits will get the
46 // correct endianness.
47 R->getValue("Inst")->setValue(NewBI);
Misha Brukman8393c152004-10-14 05:53:01 +000048 }
49}
50
51
Jim Laskey10d4b042006-07-13 22:17:08 +000052// If the VarBitInit at position 'bit' matches the specified variable then
53// return the variable bit position. Otherwise return -1.
54int CodeEmitterGen::getVariableBit(const std::string &VarName,
55 BitsInit *BI, int bit) {
Jim Laskeya44f6262006-07-13 21:02:53 +000056 if (VarBitInit *VBI = dynamic_cast<VarBitInit*>(BI->getBit(bit))) {
57 TypedInit *TI = VBI->getVariable();
58
59 if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
60 if (VI->getName() == VarName) return VBI->getBitNum();
61 }
62 }
63
64 return -1;
65}
66
67
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000068void CodeEmitterGen::run(std::ostream &o) {
Misha Brukman59978332004-08-10 18:31:01 +000069 CodeGenTarget Target;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000070 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
Jim Laskeya44f6262006-07-13 21:02:53 +000071
72 // For little-endian instruction bit encodings, reverse the bit order
73 if (Target.isLittleEndianEncoding()) reverseBits(Insts);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000074
75 EmitSourceFileHeader("Machine Code Emitter", o);
Misha Brukman59978332004-08-10 18:31:01 +000076 std::string Namespace = Insts[0]->getValueAsString("Namespace") + "::";
Jim Laskey23bd4802006-07-12 19:15:43 +000077
78 std::vector<const CodeGenInstruction*> NumberedInstructions;
79 Target.getInstructionsByEnumValue(NumberedInstructions);
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::"
Jim Laskey23bd4802006-07-12 19:15:43 +000083 << "getBinaryCodeForInstr(MachineInstr &MI) {\n";
Misha Brukman422d0fa2004-08-10 20:54:58 +000084
Jim Laskey23bd4802006-07-12 19:15:43 +000085 // Emit instruction base values
86 o << " static const unsigned InstBits[] = {\n";
87 for (std::vector<const CodeGenInstruction*>::iterator
88 IN = NumberedInstructions.begin(),
89 EN = NumberedInstructions.end();
90 IN != EN; ++IN) {
91 const CodeGenInstruction *CGI = *IN;
92 Record *R = CGI->TheDef;
Chris Lattnerec249a32006-01-27 01:39:38 +000093
Jim Laskey23bd4802006-07-12 19:15:43 +000094 if (IN != NumberedInstructions.begin()) o << ",\n";
95
96 if (R->getName() == "PHI" || R->getName() == "INLINEASM") {
97 o << " 0U";
98 continue;
99 }
100
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000101 BitsInit *BI = R->getValueAsBitsInit("Inst");
102
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000103 // Start by filling in fixed values...
Reid Spencer03c60382006-11-03 01:48:30 +0000104 unsigned Value = 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000105 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) {
106 if (BitInit *B = dynamic_cast<BitInit*>(BI->getBit(e-i-1))) {
107 Value |= B->getValue() << (e-i-1);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000108 }
109 }
Jim Laskey23bd4802006-07-12 19:15:43 +0000110 o << " " << Value << "U";
111 }
112 o << "\n };\n";
Jim Laskeya44f6262006-07-13 21:02:53 +0000113
114 // Map to accumulate all the cases.
115 std::map<std::string, std::vector<std::string> > CaseMap;
116
117 // Construct all cases statement for each opcode
118 for (std::vector<Record*>::iterator IC = Insts.begin(), EC = Insts.end();
119 IC != EC; ++IC) {
120 Record *R = *IC;
121 const std::string &InstName = R->getName();
122 std::string Case("");
Jim Laskey23bd4802006-07-12 19:15:43 +0000123
Jim Laskeya44f6262006-07-13 21:02:53 +0000124 if (InstName == "PHI" || InstName == "INLINEASM") continue;
125
Jim Laskey23bd4802006-07-12 19:15:43 +0000126 BitsInit *BI = R->getValueAsBitsInit("Inst");
127 const std::vector<RecordVal> &Vals = R->getValues();
Chris Lattner78a403f2006-11-15 23:23:02 +0000128 CodeGenInstruction &CGI = Target.getInstruction(InstName);
129
Misha Brukman8393c152004-10-14 05:53:01 +0000130 // Loop over all of the fields in the instruction, determining which are the
Misha Brukman650ba8e2005-04-22 00:00:37 +0000131 // operands to the instruction.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000132 unsigned op = 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000133 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
Misha Brukman8393c152004-10-14 05:53:01 +0000134 if (!Vals[i].getPrefix() && !Vals[i].getValue()->isComplete()) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000135 // Is the operand continuous? If so, we can just mask and OR it in
Misha Brukman650ba8e2005-04-22 00:00:37 +0000136 // instead of doing it bit-by-bit, saving a lot in runtime cost.
Jim Laskeya44f6262006-07-13 21:02:53 +0000137 const std::string &VarName = Vals[i].getName();
138 bool gotOp = false;
139
140 for (int bit = BI->getNumBits()-1; bit >= 0; ) {
141 int varBit = getVariableBit(VarName, BI, bit);
142
143 if (varBit == -1) {
144 --bit;
145 } else {
146 int beginInstBit = bit;
147 int beginVarBit = varBit;
148 int N = 1;
149
150 for (--bit; bit >= 0;) {
151 varBit = getVariableBit(VarName, BI, bit);
152 if (varBit == -1 || varBit != (beginVarBit - N)) break;
153 ++N;
154 --bit;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000155 }
Misha Brukman650ba8e2005-04-22 00:00:37 +0000156
Jim Laskeya44f6262006-07-13 21:02:53 +0000157 if (!gotOp) {
Chris Lattner78a403f2006-11-15 23:23:02 +0000158 /// If this operand is not supposed to be emitted by the generated
159 /// emitter, skip it.
160 while (CGI.isFlatOperandNotEmitted(op))
161 ++op;
162
Jim Laskeya44f6262006-07-13 21:02:53 +0000163 Case += " // op: " + VarName + "\n"
164 + " op = getMachineOpValue(MI, MI.getOperand("
Chris Lattner78a403f2006-11-15 23:23:02 +0000165 + utostr(op++) + "));\n";
Jim Laskeya44f6262006-07-13 21:02:53 +0000166 gotOp = true;
167 }
168
169 unsigned opMask = (1 << N) - 1;
170 int opShift = beginVarBit - N + 1;
171 opMask <<= opShift;
172 opShift = beginInstBit - beginVarBit;
173
174 if (opShift > 0) {
175 Case += " Value |= (op & " + utostr(opMask) + "U) << "
176 + itostr(opShift) + ";\n";
177 } else if (opShift < 0) {
178 Case += " Value |= (op & " + utostr(opMask) + "U) >> "
179 + itostr(-opShift) + ";\n";
180 } else {
181 Case += " Value |= op & " + utostr(opMask) + "U;\n";
182 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000183 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000184 }
185 }
186 }
187
Chris Lattner78a403f2006-11-15 23:23:02 +0000188 std::vector<std::string> &InstList = CaseMap[Case];
Jim Laskeya44f6262006-07-13 21:02:53 +0000189 InstList.push_back(InstName);
190 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000191
Jim Laskeya44f6262006-07-13 21:02:53 +0000192
193 // Emit initial function code
194 o << " const unsigned opcode = MI.getOpcode();\n"
195 << " unsigned Value = InstBits[opcode];\n"
196 << " unsigned op;\n"
197 << " switch (opcode) {\n";
198
199 // Emit each case statement
200 std::map<std::string, std::vector<std::string> >::iterator IE, EE;
201 for (IE = CaseMap.begin(), EE = CaseMap.end(); IE != EE; ++IE) {
202 const std::string &Case = IE->first;
203 std::vector<std::string> &InstList = IE->second;
204
205 for (int i = 0, N = InstList.size(); i < N; i++) {
206 if (i) o << "\n";
207 o << " case " << Namespace << InstList[i] << ":";
208 }
209 o << " {\n";
210 o << Case;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000211 o << " break;\n"
212 << " }\n";
213 }
214
Misha Brukman8393c152004-10-14 05:53:01 +0000215 // Default case: unhandled opcode
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000216 o << " default:\n"
Bill Wendling9bfb1e12006-12-07 22:21:48 +0000217 << " cerr << \"Not supported instr: \" << MI << \"\\n\";\n"
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000218 << " abort();\n"
219 << " }\n"
220 << " return Value;\n"
Misha Brukman8393c152004-10-14 05:53:01 +0000221 << "}\n\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000222}