Files missing from LABEL check in.
[oota-llvm.git] / utils / TableGen / CodeEmitterGen.cpp
1 //===- CodeEmitterGen.cpp - Code Emitter Generator ------------------------===//
2 //
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 //===----------------------------------------------------------------------===//
9 //
10 // 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.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "CodeEmitterGen.h"
17 #include "CodeGenTarget.h"
18 #include "Record.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/Support/Debug.h"
21 using namespace llvm;
22
23 void 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" ||
28         R->getName() == "INLINEASM" ||
29         R->getName() == "LABEL") continue;
30     
31     BitsInit *BI = R->getValueAsBitsInit("Inst");
32
33     unsigned numBits = BI->getNumBits();
34     BitsInit *NewBI = new BitsInit(numBits);
35     for (unsigned bit = 0, end = numBits / 2; bit != end; ++bit) {
36       unsigned bitSwapIdx = numBits - bit - 1;
37       Init *OrigBit = BI->getBit(bit);
38       Init *BitSwap = BI->getBit(bitSwapIdx);
39       NewBI->setBit(bit, BitSwap);
40       NewBI->setBit(bitSwapIdx, OrigBit);
41     }
42     if (numBits % 2) {
43       unsigned middle = (numBits + 1) / 2;
44       NewBI->setBit(middle, BI->getBit(middle));
45     }
46     
47     // Update the bits in reversed order so that emitInstrOpBits will get the
48     // correct endianness.
49     R->getValue("Inst")->setValue(NewBI);
50   }
51 }
52
53
54 // If the VarBitInit at position 'bit' matches the specified variable then
55 // return the variable bit position.  Otherwise return -1.
56 int CodeEmitterGen::getVariableBit(const std::string &VarName,
57             BitsInit *BI, int bit) {
58   if (VarBitInit *VBI = dynamic_cast<VarBitInit*>(BI->getBit(bit))) {
59     TypedInit *TI = VBI->getVariable();
60     
61     if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
62       if (VI->getName() == VarName) return VBI->getBitNum();
63     }
64   }
65   
66   return -1;
67
68
69
70 void CodeEmitterGen::run(std::ostream &o) {
71   CodeGenTarget Target;
72   std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
73   
74   // For little-endian instruction bit encodings, reverse the bit order
75   if (Target.isLittleEndianEncoding()) reverseBits(Insts);
76
77   EmitSourceFileHeader("Machine Code Emitter", o);
78   std::string Namespace = Insts[0]->getValueAsString("Namespace") + "::";
79   
80   std::vector<const CodeGenInstruction*> NumberedInstructions;
81   Target.getInstructionsByEnumValue(NumberedInstructions);
82
83   // Emit function declaration
84   o << "unsigned " << Target.getName() << "CodeEmitter::"
85     << "getBinaryCodeForInstr(MachineInstr &MI) {\n";
86
87   // Emit instruction base values
88   o << "  static const unsigned InstBits[] = {\n";
89   for (std::vector<const CodeGenInstruction*>::iterator
90           IN = NumberedInstructions.begin(),
91           EN = NumberedInstructions.end();
92        IN != EN; ++IN) {
93     const CodeGenInstruction *CGI = *IN;
94     Record *R = CGI->TheDef;
95     
96     if (IN != NumberedInstructions.begin()) o << ",\n";
97     
98     if (R->getName() == "PHI" ||
99         R->getName() == "INLINEASM" ||
100         R->getName() == "LABEL") {
101       o << "    0U";
102       continue;
103     }
104     
105     BitsInit *BI = R->getValueAsBitsInit("Inst");
106
107     // Start by filling in fixed values...
108     unsigned Value = 0;
109     for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) {
110       if (BitInit *B = dynamic_cast<BitInit*>(BI->getBit(e-i-1))) {
111         Value |= B->getValue() << (e-i-1);
112       }
113     }
114     o << "    " << Value << "U";
115   }
116   o << "\n  };\n";
117   
118   // Map to accumulate all the cases.
119   std::map<std::string, std::vector<std::string> > CaseMap;
120   
121   // Construct all cases statement for each opcode
122   for (std::vector<Record*>::iterator IC = Insts.begin(), EC = Insts.end();
123         IC != EC; ++IC) {
124     Record *R = *IC;
125     const std::string &InstName = R->getName();
126     std::string Case("");
127     
128     if (InstName == "PHI" ||
129         InstName == "INLINEASM" ||
130         InstName == "LABEL") continue;
131     
132     BitsInit *BI = R->getValueAsBitsInit("Inst");
133     const std::vector<RecordVal> &Vals = R->getValues();
134     CodeGenInstruction &CGI = Target.getInstruction(InstName);
135     
136     // Loop over all of the fields in the instruction, determining which are the
137     // operands to the instruction.
138     unsigned op = 0;
139     for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
140       if (!Vals[i].getPrefix() && !Vals[i].getValue()->isComplete()) {
141         // 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.
143         const std::string &VarName = Vals[i].getName();
144         bool gotOp = false;
145         
146         for (int bit = BI->getNumBits()-1; bit >= 0; ) {
147           int varBit = getVariableBit(VarName, BI, bit);
148           
149           if (varBit == -1) {
150             --bit;
151           } else {
152             int beginInstBit = bit;
153             int beginVarBit = varBit;
154             int N = 1;
155             
156             for (--bit; bit >= 0;) {
157               varBit = getVariableBit(VarName, BI, bit);
158               if (varBit == -1 || varBit != (beginVarBit - N)) break;
159               ++N;
160               --bit;
161             }
162
163             if (!gotOp) {
164               /// If this operand is not supposed to be emitted by the generated
165               /// emitter, skip it.
166               while (CGI.isFlatOperandNotEmitted(op))
167                 ++op;
168               
169               Case += "      // op: " + VarName + "\n"
170                    +  "      op = getMachineOpValue(MI, MI.getOperand("
171                    +  utostr(op++) + "));\n";
172               gotOp = true;
173             }
174             
175             unsigned opMask = (1 << N) - 1;
176             int opShift = beginVarBit - N + 1;
177             opMask <<= opShift;
178             opShift = beginInstBit - beginVarBit;
179             
180             if (opShift > 0) {
181               Case += "      Value |= (op & " + utostr(opMask) + "U) << "
182                    +  itostr(opShift) + ";\n";
183             } else if (opShift < 0) {
184               Case += "      Value |= (op & " + utostr(opMask) + "U) >> "
185                    +  itostr(-opShift) + ";\n";
186             } else {
187               Case += "      Value |= op & " + utostr(opMask) + "U;\n";
188             }
189           }
190         }
191       }
192     }
193
194     std::vector<std::string> &InstList = CaseMap[Case];
195     InstList.push_back(InstName);
196   }
197
198
199   // Emit initial function code
200   o << "  const unsigned opcode = MI.getOpcode();\n"
201     << "  unsigned Value = InstBits[opcode];\n"
202     << "  unsigned op;\n"
203     << "  switch (opcode) {\n";
204
205   // Emit each case statement
206   std::map<std::string, std::vector<std::string> >::iterator IE, EE;
207   for (IE = CaseMap.begin(), EE = CaseMap.end(); IE != EE; ++IE) {
208     const std::string &Case = IE->first;
209     std::vector<std::string> &InstList = IE->second;
210
211     for (int i = 0, N = InstList.size(); i < N; i++) {
212       if (i) o << "\n";
213       o << "    case " << Namespace << InstList[i]  << ":";
214     }
215     o << " {\n";
216     o << Case;
217     o << "      break;\n"
218       << "    }\n";
219   }
220
221   // Default case: unhandled opcode
222   o << "  default:\n"
223     << "    cerr << \"Not supported instr: \" << MI << \"\\n\";\n"
224     << "    abort();\n"
225     << "  }\n"
226     << "  return Value;\n"
227     << "}\n\n";
228 }