Remove an unused variable.
[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" || R->getName() == "INLINEASM") continue;
28     
29     BitsInit *BI = R->getValueAsBitsInit("Inst");
30
31     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);
39     }
40     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);
48   }
49 }
50
51
52 // If the VarBitInit at position 'bit' matches the specified variable then
53 // return the variable bit position.  Otherwise return -1.
54 int CodeEmitterGen::getVariableBit(const std::string &VarName,
55             BitsInit *BI, int bit) {
56   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
68 void CodeEmitterGen::run(std::ostream &o) {
69   CodeGenTarget Target;
70   std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
71   
72   // For little-endian instruction bit encodings, reverse the bit order
73   if (Target.isLittleEndianEncoding()) reverseBits(Insts);
74
75   EmitSourceFileHeader("Machine Code Emitter", o);
76   std::string Namespace = Insts[0]->getValueAsString("Namespace") + "::";
77   
78   std::vector<const CodeGenInstruction*> NumberedInstructions;
79   Target.getInstructionsByEnumValue(NumberedInstructions);
80
81   // Emit function declaration
82   o << "unsigned " << Target.getName() << "CodeEmitter::"
83     << "getBinaryCodeForInstr(MachineInstr &MI) {\n";
84
85   // 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;
93     
94     if (IN != NumberedInstructions.begin()) o << ",\n";
95     
96     if (R->getName() == "PHI" || R->getName() == "INLINEASM") {
97       o << "    0U";
98       continue;
99     }
100     
101     BitsInit *BI = R->getValueAsBitsInit("Inst");
102
103     // Start by filling in fixed values...
104     unsigned Value = 0;
105     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);
108       }
109     }
110     o << "    " << Value << "U";
111   }
112   o << "\n  };\n";
113   
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("");
123     
124     if (InstName == "PHI" || InstName == "INLINEASM") continue;
125     
126     BitsInit *BI = R->getValueAsBitsInit("Inst");
127     const std::vector<RecordVal> &Vals = R->getValues();
128
129     // Loop over all of the fields in the instruction, determining which are the
130     // operands to the instruction.
131     unsigned op = 0;
132     for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
133       if (!Vals[i].getPrefix() && !Vals[i].getValue()->isComplete()) {
134         // Is the operand continuous? If so, we can just mask and OR it in
135         // instead of doing it bit-by-bit, saving a lot in runtime cost.
136         const std::string &VarName = Vals[i].getName();
137         bool gotOp = false;
138         
139         for (int bit = BI->getNumBits()-1; bit >= 0; ) {
140           int varBit = getVariableBit(VarName, BI, bit);
141           
142           if (varBit == -1) {
143             --bit;
144           } else {
145             int beginInstBit = bit;
146             int beginVarBit = varBit;
147             int N = 1;
148             
149             for (--bit; bit >= 0;) {
150               varBit = getVariableBit(VarName, BI, bit);
151               if (varBit == -1 || varBit != (beginVarBit - N)) break;
152               ++N;
153               --bit;
154             }
155
156             if (!gotOp) {
157               Case += "      // op: " + VarName + "\n"
158                    +  "      op = getMachineOpValue(MI, MI.getOperand("
159                    +  utostr(op++)
160                    +  "));\n";
161               gotOp = true;
162               
163               // If this is a two-address instruction and we just got the dest
164               // op, skip the src op.
165               if (op == 1 && Target.getInstruction(InstName).isTwoAddress)
166                 ++op;
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             }
183           }
184         }
185       }
186     }
187
188     std::vector<std::string> &InstList =  CaseMap[Case];
189     InstList.push_back(InstName);
190   }
191
192
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;
211     o << "      break;\n"
212       << "    }\n";
213   }
214
215   // Default case: unhandled opcode
216   o << "  default:\n"
217     << "    std::cerr << \"Not supported instr: \" << MI << \"\\n\";\n"
218     << "    abort();\n"
219     << "  }\n"
220     << "  return Value;\n"
221     << "}\n\n";
222 }