Chris Lattner | 1d1adea | 2003-08-01 04:39:05 +0000 | [diff] [blame] | 1 | //===- TableGen.cpp - Top-Level TableGen implementation -------------------===// |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 01d4582 | 2003-10-20 20:20:30 +0000 | [diff] [blame] | 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. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 01d4582 | 2003-10-20 20:20:30 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 1d1adea | 2003-08-01 04:39:05 +0000 | [diff] [blame] | 9 | // |
| 10 | // TableGen is a tool which can be used to build up a description of something, |
| 11 | // then invoke one or more "tablegen backends" to emit information about the |
| 12 | // description in some predefined format. In practice, this is used by the LLVM |
| 13 | // code generators to automate generation of a code generator through a |
| 14 | // high-level description of the target. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 18 | #include "Record.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 19 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | bed85ff | 2004-05-27 05:41:36 +0000 | [diff] [blame] | 20 | #include "llvm/System/Signals.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 21 | #include "llvm/Support/FileUtilities.h" |
Misha Brukman | f00ce8b | 2003-05-24 00:17:12 +0000 | [diff] [blame] | 22 | #include "CodeEmitterGen.h" |
Chris Lattner | 1d1adea | 2003-08-01 04:39:05 +0000 | [diff] [blame] | 23 | #include "RegisterInfoEmitter.h" |
Chris Lattner | 169e66b | 2003-08-03 17:24:20 +0000 | [diff] [blame] | 24 | #include "InstrInfoEmitter.h" |
Chris Lattner | 2e1f51b | 2004-08-01 05:59:33 +0000 | [diff] [blame] | 25 | #include "AsmWriterEmitter.h" |
Chris Lattner | 4a24c64 | 2005-09-03 01:14:03 +0000 | [diff] [blame] | 26 | #include "DAGISelEmitter.h" |
Jim Laskey | f5fc2cb | 2005-10-21 19:05:19 +0000 | [diff] [blame] | 27 | #include "SubtargetEmitter.h" |
Chris Lattner | 9e493cf | 2006-03-03 02:32:46 +0000 | [diff] [blame^] | 28 | #include "IntrinsicEmitter.h" |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 29 | #include <algorithm> |
Misha Brukman | c3fe45b | 2003-08-14 16:05:35 +0000 | [diff] [blame] | 30 | #include <cstdio> |
Chris Lattner | 9a88638 | 2003-06-03 05:04:42 +0000 | [diff] [blame] | 31 | #include <fstream> |
Duraid Madina | 6fb9a84 | 2005-12-26 05:08:55 +0000 | [diff] [blame] | 32 | #include <ios> |
Chris Lattner | 2082ebe | 2004-08-01 03:55:39 +0000 | [diff] [blame] | 33 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 34 | |
Chris Lattner | bc52013 | 2003-06-03 04:56:29 +0000 | [diff] [blame] | 35 | enum ActionType { |
| 36 | PrintRecords, |
| 37 | GenEmitter, |
Chris Lattner | 54d156d | 2003-08-01 05:59:20 +0000 | [diff] [blame] | 38 | GenRegisterEnums, GenRegister, GenRegisterHeader, |
Chris Lattner | 2dc74dd | 2005-10-23 05:47:52 +0000 | [diff] [blame] | 39 | GenInstrEnums, GenInstrs, GenAsmWriter, |
Chris Lattner | 4a24c64 | 2005-09-03 01:14:03 +0000 | [diff] [blame] | 40 | GenDAGISel, |
Jim Laskey | f5fc2cb | 2005-10-21 19:05:19 +0000 | [diff] [blame] | 41 | GenSubtarget, |
Chris Lattner | 9e493cf | 2006-03-03 02:32:46 +0000 | [diff] [blame^] | 42 | GenIntrinsic, |
Chris Lattner | bc52013 | 2003-06-03 04:56:29 +0000 | [diff] [blame] | 43 | PrintEnums, |
Chris Lattner | 9879aa9 | 2004-07-22 21:32:38 +0000 | [diff] [blame] | 44 | Parse |
Chris Lattner | bc52013 | 2003-06-03 04:56:29 +0000 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | namespace { |
| 48 | cl::opt<ActionType> |
| 49 | Action(cl::desc("Action to perform:"), |
| 50 | cl::values(clEnumValN(PrintRecords, "print-records", |
Chris Lattner | 85df225 | 2003-06-03 05:07:28 +0000 | [diff] [blame] | 51 | "Print all records to stdout (default)"), |
Chris Lattner | bc52013 | 2003-06-03 04:56:29 +0000 | [diff] [blame] | 52 | clEnumValN(GenEmitter, "gen-emitter", |
| 53 | "Generate machine code emitter"), |
Chris Lattner | 54d156d | 2003-08-01 05:59:20 +0000 | [diff] [blame] | 54 | clEnumValN(GenRegisterEnums, "gen-register-enums", |
| 55 | "Generate enum values for registers"), |
Chris Lattner | 1d1adea | 2003-08-01 04:39:05 +0000 | [diff] [blame] | 56 | clEnumValN(GenRegister, "gen-register-desc", |
| 57 | "Generate a register info description"), |
| 58 | clEnumValN(GenRegisterHeader, "gen-register-desc-header", |
| 59 | "Generate a register info description header"), |
Chris Lattner | 169e66b | 2003-08-03 17:24:20 +0000 | [diff] [blame] | 60 | clEnumValN(GenInstrEnums, "gen-instr-enums", |
| 61 | "Generate enum values for instructions"), |
Chris Lattner | 15de32d | 2003-08-03 21:58:28 +0000 | [diff] [blame] | 62 | clEnumValN(GenInstrs, "gen-instr-desc", |
| 63 | "Generate instruction descriptions"), |
Chris Lattner | 2e1f51b | 2004-08-01 05:59:33 +0000 | [diff] [blame] | 64 | clEnumValN(GenAsmWriter, "gen-asm-writer", |
| 65 | "Generate assembly writer"), |
Chris Lattner | 4a24c64 | 2005-09-03 01:14:03 +0000 | [diff] [blame] | 66 | clEnumValN(GenDAGISel, "gen-dag-isel", |
| 67 | "Generate a DAG instruction selector"), |
Jim Laskey | f5fc2cb | 2005-10-21 19:05:19 +0000 | [diff] [blame] | 68 | clEnumValN(GenSubtarget, "gen-subtarget", |
| 69 | "Generate subtarget enumerations"), |
Chris Lattner | 9e493cf | 2006-03-03 02:32:46 +0000 | [diff] [blame^] | 70 | clEnumValN(GenIntrinsic, "gen-intrinsic", |
| 71 | "Generate intrinsic information"), |
Chris Lattner | bc52013 | 2003-06-03 04:56:29 +0000 | [diff] [blame] | 72 | clEnumValN(PrintEnums, "print-enums", |
| 73 | "Print enum values for a class"), |
| 74 | clEnumValN(Parse, "parse", |
| 75 | "Interpret machine code (testing only)"), |
Chris Lattner | bd93533 | 2004-07-16 00:02:21 +0000 | [diff] [blame] | 76 | clEnumValEnd)); |
Chris Lattner | bc52013 | 2003-06-03 04:56:29 +0000 | [diff] [blame] | 77 | |
| 78 | cl::opt<std::string> |
Chris Lattner | 85df225 | 2003-06-03 05:07:28 +0000 | [diff] [blame] | 79 | Class("class", cl::desc("Print Enum list for this class"), |
| 80 | cl::value_desc("class name")); |
Chris Lattner | 9a88638 | 2003-06-03 05:04:42 +0000 | [diff] [blame] | 81 | |
Chris Lattner | 9052390 | 2003-07-30 19:48:02 +0000 | [diff] [blame] | 82 | cl::opt<std::string> |
| 83 | OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"), |
| 84 | cl::init("-")); |
| 85 | |
| 86 | cl::opt<std::string> |
| 87 | InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-")); |
John Criswell | 96b4bed | 2003-08-27 13:41:57 +0000 | [diff] [blame] | 88 | |
Chris Lattner | d9f5d90 | 2006-03-03 01:47:14 +0000 | [diff] [blame] | 89 | cl::list<std::string> |
| 90 | IncludeDirs("I", cl::desc("Directory of include files"), |
Chris Lattner | ed54241 | 2006-03-03 01:53:40 +0000 | [diff] [blame] | 91 | cl::value_desc("directory"), cl::Prefix); |
Chris Lattner | bc52013 | 2003-06-03 04:56:29 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Chris Lattner | 2082ebe | 2004-08-01 03:55:39 +0000 | [diff] [blame] | 94 | namespace llvm { |
| 95 | void ParseFile(const std::string &Filename, |
Chris Lattner | d9f5d90 | 2006-03-03 01:47:14 +0000 | [diff] [blame] | 96 | const std::vector<std::string> &IncludeDirs); |
Chris Lattner | 2082ebe | 2004-08-01 03:55:39 +0000 | [diff] [blame] | 97 | } |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 98 | |
Chris Lattner | 2082ebe | 2004-08-01 03:55:39 +0000 | [diff] [blame] | 99 | RecordKeeper llvm::Records; |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 100 | |
| 101 | static Init *getBit(Record *R, unsigned BitNo) { |
| 102 | const std::vector<RecordVal> &V = R->getValues(); |
| 103 | for (unsigned i = 0, e = V.size(); i != e; ++i) |
| 104 | if (V[i].getPrefix()) { |
| 105 | assert(dynamic_cast<BitsInit*>(V[i].getValue()) && |
Jeff Cohen | ea3e5e5 | 2005-04-22 04:13:13 +0000 | [diff] [blame] | 106 | "Can only handle fields of bits<> type!"); |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 107 | BitsInit *I = (BitsInit*)V[i].getValue(); |
| 108 | if (BitNo < I->getNumBits()) |
Jeff Cohen | ea3e5e5 | 2005-04-22 04:13:13 +0000 | [diff] [blame] | 109 | return I->getBit(BitNo); |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 110 | BitNo -= I->getNumBits(); |
| 111 | } |
| 112 | |
| 113 | std::cerr << "Cannot find requested bit!\n"; |
Chris Lattner | f5761a5 | 2004-02-13 16:37:43 +0000 | [diff] [blame] | 114 | exit(1); |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | static unsigned getNumBits(Record *R) { |
| 119 | const std::vector<RecordVal> &V = R->getValues(); |
| 120 | unsigned Num = 0; |
| 121 | for (unsigned i = 0, e = V.size(); i != e; ++i) |
| 122 | if (V[i].getPrefix()) { |
| 123 | assert(dynamic_cast<BitsInit*>(V[i].getValue()) && |
Jeff Cohen | ea3e5e5 | 2005-04-22 04:13:13 +0000 | [diff] [blame] | 124 | "Can only handle fields of bits<> type!"); |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 125 | Num += ((BitsInit*)V[i].getValue())->getNumBits(); |
| 126 | } |
| 127 | return Num; |
| 128 | } |
| 129 | |
| 130 | static bool BitsAreFixed(Record *I1, Record *I2, unsigned BitNo) { |
| 131 | return dynamic_cast<BitInit*>(getBit(I1, BitNo)) && |
| 132 | dynamic_cast<BitInit*>(getBit(I2, BitNo)); |
| 133 | } |
| 134 | |
| 135 | static bool BitsAreEqual(Record *I1, Record *I2, unsigned BitNo) { |
| 136 | BitInit *Bit1 = dynamic_cast<BitInit*>(getBit(I1, BitNo)); |
| 137 | BitInit *Bit2 = dynamic_cast<BitInit*>(getBit(I2, BitNo)); |
| 138 | |
| 139 | return Bit1 && Bit2 && Bit1->getValue() == Bit2->getValue(); |
| 140 | } |
| 141 | |
| 142 | static bool BitRangesEqual(Record *I1, Record *I2, |
Jeff Cohen | ea3e5e5 | 2005-04-22 04:13:13 +0000 | [diff] [blame] | 143 | unsigned Start, unsigned End) { |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 144 | for (unsigned i = Start; i != End; ++i) |
| 145 | if (!BitsAreEqual(I1, I2, i)) |
| 146 | return false; |
| 147 | return true; |
| 148 | } |
| 149 | |
| 150 | static unsigned getFirstFixedBit(Record *R, unsigned FirstFixedBit) { |
| 151 | // Look for the first bit of the pair that are required to be 0 or 1. |
| 152 | while (!dynamic_cast<BitInit*>(getBit(R, FirstFixedBit))) |
| 153 | ++FirstFixedBit; |
| 154 | return FirstFixedBit; |
| 155 | } |
| 156 | |
| 157 | static void FindInstDifferences(Record *I1, Record *I2, |
Jeff Cohen | ea3e5e5 | 2005-04-22 04:13:13 +0000 | [diff] [blame] | 158 | unsigned FirstFixedBit, unsigned MaxBits, |
| 159 | unsigned &FirstVaryingBitOverall, |
| 160 | unsigned &LastFixedBitOverall) { |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 161 | // Compare the first instruction to the rest of the instructions, looking for |
| 162 | // fields that differ. |
| 163 | // |
| 164 | unsigned FirstVaryingBit = FirstFixedBit; |
| 165 | while (FirstVaryingBit < MaxBits && BitsAreEqual(I1, I2, FirstVaryingBit)) |
| 166 | ++FirstVaryingBit; |
| 167 | |
| 168 | unsigned LastFixedBit = FirstVaryingBit; |
| 169 | while (LastFixedBit < MaxBits && BitsAreFixed(I1, I2, LastFixedBit)) |
| 170 | ++LastFixedBit; |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 171 | |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 172 | if (FirstVaryingBit < FirstVaryingBitOverall) |
| 173 | FirstVaryingBitOverall = FirstVaryingBit; |
| 174 | if (LastFixedBit < LastFixedBitOverall) |
| 175 | LastFixedBitOverall = LastFixedBit; |
| 176 | } |
| 177 | |
| 178 | static bool getBitValue(Record *R, unsigned BitNo) { |
| 179 | Init *I = getBit(R, BitNo); |
| 180 | assert(dynamic_cast<BitInit*>(I) && "Bit should be fixed!"); |
| 181 | return ((BitInit*)I)->getValue(); |
| 182 | } |
| 183 | |
| 184 | struct BitComparator { |
| 185 | unsigned BitBegin, BitEnd; |
| 186 | BitComparator(unsigned B, unsigned E) : BitBegin(B), BitEnd(E) {} |
| 187 | |
| 188 | bool operator()(Record *R1, Record *R2) { // Return true if R1 is less than R2 |
| 189 | for (unsigned i = BitBegin; i != BitEnd; ++i) { |
| 190 | bool V1 = getBitValue(R1, i), V2 = getBitValue(R2, i); |
| 191 | if (V1 < V2) |
Jeff Cohen | ea3e5e5 | 2005-04-22 04:13:13 +0000 | [diff] [blame] | 192 | return true; |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 193 | else if (V2 < V1) |
Jeff Cohen | ea3e5e5 | 2005-04-22 04:13:13 +0000 | [diff] [blame] | 194 | return false; |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 195 | } |
| 196 | return false; |
| 197 | } |
| 198 | }; |
| 199 | |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 200 | static void PrintRange(std::vector<Record*>::iterator I, |
Jeff Cohen | ea3e5e5 | 2005-04-22 04:13:13 +0000 | [diff] [blame] | 201 | std::vector<Record*>::iterator E) { |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 202 | while (I != E) std::cerr << **I++; |
| 203 | } |
| 204 | |
| 205 | static bool getMemoryBit(unsigned char *M, unsigned i) { |
| 206 | return (M[i/8] & (1 << (i&7))) != 0; |
| 207 | } |
| 208 | |
| 209 | static unsigned getFirstFixedBitInSequence(std::vector<Record*>::iterator IB, |
Jeff Cohen | ea3e5e5 | 2005-04-22 04:13:13 +0000 | [diff] [blame] | 210 | std::vector<Record*>::iterator IE, |
| 211 | unsigned StartBit) { |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 212 | unsigned FirstFixedBit = 0; |
| 213 | for (std::vector<Record*>::iterator I = IB; I != IE; ++I) |
| 214 | FirstFixedBit = std::max(FirstFixedBit, getFirstFixedBit(*I, StartBit)); |
| 215 | return FirstFixedBit; |
| 216 | } |
| 217 | |
| 218 | // ParseMachineCode - Try to split the vector of instructions (which is |
Misha Brukman | 737af82 | 2003-09-07 20:12:52 +0000 | [diff] [blame] | 219 | // intentionally taken by-copy) in half, narrowing down the possible |
| 220 | // instructions that we may have found. Eventually, this list will get pared |
| 221 | // down to zero or one instruction, in which case we have a match or failure. |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 222 | // |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 223 | static Record *ParseMachineCode(std::vector<Record*>::iterator InstsB, |
Jeff Cohen | ea3e5e5 | 2005-04-22 04:13:13 +0000 | [diff] [blame] | 224 | std::vector<Record*>::iterator InstsE, |
| 225 | unsigned char *M) { |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 226 | assert(InstsB != InstsE && "Empty range?"); |
| 227 | if (InstsB+1 == InstsE) { |
| 228 | // Only a single instruction, see if we match it... |
| 229 | Record *Inst = *InstsB; |
| 230 | for (unsigned i = 0, e = getNumBits(Inst); i != e; ++i) |
| 231 | if (BitInit *BI = dynamic_cast<BitInit*>(getBit(Inst, i))) |
Jeff Cohen | ea3e5e5 | 2005-04-22 04:13:13 +0000 | [diff] [blame] | 232 | if (getMemoryBit(M, i) != BI->getValue()) |
| 233 | throw std::string("Parse failed!\n"); |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 234 | return Inst; |
| 235 | } |
| 236 | |
| 237 | unsigned MaxBits = ~0; |
| 238 | for (std::vector<Record*>::iterator I = InstsB; I != InstsE; ++I) |
| 239 | MaxBits = std::min(MaxBits, getNumBits(*I)); |
| 240 | |
| 241 | unsigned FirstFixedBit = getFirstFixedBitInSequence(InstsB, InstsE, 0); |
| 242 | unsigned FirstVaryingBit, LastFixedBit; |
| 243 | do { |
| 244 | FirstVaryingBit = ~0; |
| 245 | LastFixedBit = ~0; |
| 246 | for (std::vector<Record*>::iterator I = InstsB+1; I != InstsE; ++I) |
| 247 | FindInstDifferences(*InstsB, *I, FirstFixedBit, MaxBits, |
Jeff Cohen | ea3e5e5 | 2005-04-22 04:13:13 +0000 | [diff] [blame] | 248 | FirstVaryingBit, LastFixedBit); |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 249 | if (FirstVaryingBit == MaxBits) { |
| 250 | std::cerr << "ERROR: Could not find bit to distinguish between " |
Jeff Cohen | ea3e5e5 | 2005-04-22 04:13:13 +0000 | [diff] [blame] | 251 | << "the following entries!\n"; |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 252 | PrintRange(InstsB, InstsE); |
| 253 | } |
| 254 | |
| 255 | #if 0 |
| 256 | std::cerr << "FVB: " << FirstVaryingBit << " - " << LastFixedBit |
Jeff Cohen | ea3e5e5 | 2005-04-22 04:13:13 +0000 | [diff] [blame] | 257 | << ": " << InstsE-InstsB << "\n"; |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 258 | #endif |
| 259 | |
| 260 | FirstFixedBit = getFirstFixedBitInSequence(InstsB, InstsE, FirstVaryingBit); |
| 261 | } while (FirstVaryingBit != FirstFixedBit); |
| 262 | |
| 263 | //std::cerr << "\n\nXXXXXXXXXXXXXXXXX\n\n"; |
| 264 | //PrintRange(InstsB, InstsE); |
| 265 | |
| 266 | // Sort the Insts list so that the entries have all of the bits in the range |
| 267 | // [FirstVaryingBit,LastFixedBit) sorted. These bits are all guaranteed to be |
| 268 | // set to either 0 or 1 (BitInit values), which simplifies things. |
| 269 | // |
| 270 | std::sort(InstsB, InstsE, BitComparator(FirstVaryingBit, LastFixedBit)); |
| 271 | |
| 272 | // Once the list is sorted by these bits, split the bit list into smaller |
| 273 | // lists, and recurse on each one. |
| 274 | // |
| 275 | std::vector<Record*>::iterator RangeBegin = InstsB; |
| 276 | Record *Match = 0; |
| 277 | while (RangeBegin != InstsE) { |
| 278 | std::vector<Record*>::iterator RangeEnd = RangeBegin+1; |
| 279 | while (RangeEnd != InstsE && |
| 280 | BitRangesEqual(*RangeBegin, *RangeEnd, FirstVaryingBit, LastFixedBit)) |
| 281 | ++RangeEnd; |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 282 | |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 283 | // We just identified a range of equal instructions. If this range is the |
| 284 | // input range, we were not able to distinguish between the instructions in |
| 285 | // the set. Print an error and exit! |
| 286 | // |
| 287 | if (RangeBegin == InstsB && RangeEnd == InstsE) { |
| 288 | std::cerr << "Error: Could not distinguish among the following insts!:\n"; |
| 289 | PrintRange(InstsB, InstsE); |
Chris Lattner | f5761a5 | 2004-02-13 16:37:43 +0000 | [diff] [blame] | 290 | exit(1); |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 291 | } |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 292 | |
Chris Lattner | 7b1d49b | 2002-12-03 20:01:04 +0000 | [diff] [blame] | 293 | #if 0 |
| 294 | std::cerr << "FVB: " << FirstVaryingBit << " - " << LastFixedBit |
Jeff Cohen | ea3e5e5 | 2005-04-22 04:13:13 +0000 | [diff] [blame] | 295 | << ": [" << RangeEnd-RangeBegin << "] - "; |
Chris Lattner | 7b1d49b | 2002-12-03 20:01:04 +0000 | [diff] [blame] | 296 | for (int i = LastFixedBit-1; i >= (int)FirstVaryingBit; --i) |
| 297 | std::cerr << (int)((BitInit*)getBit(*RangeBegin, i))->getValue() << " "; |
| 298 | std::cerr << "\n"; |
| 299 | #endif |
| 300 | |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 301 | if (Record *R = ParseMachineCode(RangeBegin, RangeEnd, M)) { |
| 302 | if (Match) { |
Jeff Cohen | ea3e5e5 | 2005-04-22 04:13:13 +0000 | [diff] [blame] | 303 | std::cerr << "Error: Multiple matches found:\n"; |
| 304 | PrintRange(InstsB, InstsE); |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | assert(Match == 0 && "Multiple matches??"); |
| 308 | Match = R; |
| 309 | } |
| 310 | RangeBegin = RangeEnd; |
| 311 | } |
| 312 | |
| 313 | return Match; |
| 314 | } |
| 315 | |
| 316 | static void PrintValue(Record *I, unsigned char *Ptr, const RecordVal &Val) { |
| 317 | assert(dynamic_cast<BitsInit*>(Val.getValue()) && |
Jeff Cohen | ea3e5e5 | 2005-04-22 04:13:13 +0000 | [diff] [blame] | 318 | "Can only handle undefined bits<> types!"); |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 319 | BitsInit *BI = (BitsInit*)Val.getValue(); |
| 320 | assert(BI->getNumBits() <= 32 && "Can only handle fields up to 32 bits!"); |
| 321 | |
| 322 | unsigned Value = 0; |
| 323 | const std::vector<RecordVal> &Vals = I->getValues(); |
| 324 | |
| 325 | // Start by filling in fixed values... |
| 326 | for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) |
| 327 | if (BitInit *B = dynamic_cast<BitInit*>(BI->getBit(i))) |
| 328 | Value |= B->getValue() << i; |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 329 | |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 330 | // Loop over all of the fields in the instruction adding in any |
| 331 | // contributions to this value (due to bit references). |
| 332 | // |
| 333 | unsigned Offset = 0; |
| 334 | for (unsigned f = 0, e = Vals.size(); f != e; ++f) |
| 335 | if (Vals[f].getPrefix()) { |
Chris Lattner | 9833493 | 2002-12-02 17:43:43 +0000 | [diff] [blame] | 336 | BitsInit *FieldInitializer = (BitsInit*)Vals[f].getValue(); |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 337 | if (&Vals[f] == &Val) { |
Jeff Cohen | ea3e5e5 | 2005-04-22 04:13:13 +0000 | [diff] [blame] | 338 | // Read the bits directly now... |
| 339 | for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) |
| 340 | Value |= getMemoryBit(Ptr, Offset+i) << i; |
| 341 | break; |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 342 | } |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 343 | |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 344 | // Scan through the field looking for bit initializers of the current |
| 345 | // variable... |
Chris Lattner | 9833493 | 2002-12-02 17:43:43 +0000 | [diff] [blame] | 346 | for (unsigned i = 0, e = FieldInitializer->getNumBits(); i != e; ++i) |
Jeff Cohen | ea3e5e5 | 2005-04-22 04:13:13 +0000 | [diff] [blame] | 347 | if (VarBitInit *VBI = |
| 348 | dynamic_cast<VarBitInit*>(FieldInitializer->getBit(i))) { |
Chris Lattner | 9833493 | 2002-12-02 17:43:43 +0000 | [diff] [blame] | 349 | TypedInit *TI = VBI->getVariable(); |
| 350 | if (VarInit *VI = dynamic_cast<VarInit*>(TI)) { |
| 351 | if (VI->getName() == Val.getName()) |
| 352 | Value |= getMemoryBit(Ptr, Offset+i) << VBI->getBitNum(); |
| 353 | } else if (FieldInit *FI = dynamic_cast<FieldInit*>(TI)) { |
| 354 | // FIXME: implement this! |
| 355 | std::cerr << "FIELD INIT not implemented yet!\n"; |
| 356 | } |
Jeff Cohen | ea3e5e5 | 2005-04-22 04:13:13 +0000 | [diff] [blame] | 357 | } |
Chris Lattner | 9833493 | 2002-12-02 17:43:43 +0000 | [diff] [blame] | 358 | Offset += FieldInitializer->getNumBits(); |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | std::cout << "0x" << std::hex << Value << std::dec; |
| 362 | } |
| 363 | |
| 364 | static void PrintInstruction(Record *I, unsigned char *Ptr) { |
| 365 | std::cout << "Inst " << getNumBits(I)/8 << " bytes: " |
Jeff Cohen | ea3e5e5 | 2005-04-22 04:13:13 +0000 | [diff] [blame] | 366 | << "\t" << I->getName() << "\t" << *I->getValue("Name")->getValue() |
| 367 | << "\t"; |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 368 | |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 369 | const std::vector<RecordVal> &Vals = I->getValues(); |
| 370 | for (unsigned i = 0, e = Vals.size(); i != e; ++i) |
| 371 | if (!Vals[i].getValue()->isComplete()) { |
| 372 | std::cout << Vals[i].getName() << "="; |
| 373 | PrintValue(I, Ptr, Vals[i]); |
| 374 | std::cout << "\t"; |
| 375 | } |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 376 | |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 377 | std::cout << "\n";// << *I; |
| 378 | } |
| 379 | |
| 380 | static void ParseMachineCode() { |
Misha Brukman | f00ce8b | 2003-05-24 00:17:12 +0000 | [diff] [blame] | 381 | // X86 code |
Chris Lattner | 7b1d49b | 2002-12-03 20:01:04 +0000 | [diff] [blame] | 382 | unsigned char Buffer[] = { |
| 383 | 0x55, // push EBP |
Jeff Cohen | ea3e5e5 | 2005-04-22 04:13:13 +0000 | [diff] [blame] | 384 | 0x89, 0xE5, // mov EBP, ESP |
| 385 | //0x83, 0xEC, 0x08, // sub ESP, 0x8 |
| 386 | 0xE8, 1, 2, 3, 4, // call +0x04030201 |
| 387 | 0x89, 0xEC, // mov ESP, EBP |
| 388 | 0x5D, // pop EBP |
| 389 | 0xC3, // ret |
| 390 | 0x90, // nop |
| 391 | 0xC9, // leave |
| 392 | 0x89, 0xF6, // mov ESI, ESI |
| 393 | 0x68, 1, 2, 3, 4, // push 0x04030201 |
| 394 | 0x5e, // pop ESI |
| 395 | 0xFF, 0xD0, // call EAX |
| 396 | 0xB8, 1, 2, 3, 4, // mov EAX, 0x04030201 |
| 397 | 0x85, 0xC0, // test EAX, EAX |
| 398 | 0xF4, // hlt |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 399 | }; |
Misha Brukman | f00ce8b | 2003-05-24 00:17:12 +0000 | [diff] [blame] | 400 | |
| 401 | #if 0 |
| 402 | // SparcV9 code |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 403 | unsigned char Buffer[] = { 0xbf, 0xe0, 0x20, 0x1f, 0x1, 0x0, 0x0, 0x1, |
Misha Brukman | f00ce8b | 2003-05-24 00:17:12 +0000 | [diff] [blame] | 404 | 0x0, 0x0, 0x0, 0x0, 0xc1, 0x0, 0x20, 0x1, 0x1, |
| 405 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 406 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x40, 0x0, 0x0, 0x0, 0x1, |
| 407 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 408 | 0x0, 0x0, 0xaf, 0xe8, 0x20, 0x17 |
| 409 | }; |
| 410 | #endif |
| 411 | |
Chris Lattner | 1d1adea | 2003-08-01 04:39:05 +0000 | [diff] [blame] | 412 | std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction"); |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 413 | |
| 414 | unsigned char *BuffPtr = Buffer; |
| 415 | while (1) { |
| 416 | Record *R = ParseMachineCode(Insts.begin(), Insts.end(), BuffPtr); |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 417 | PrintInstruction(R, BuffPtr); |
| 418 | |
| 419 | unsigned Bits = getNumBits(R); |
| 420 | assert((Bits & 7) == 0 && "Instruction is not an even number of bytes!"); |
| 421 | BuffPtr += Bits/8; |
| 422 | } |
| 423 | } |
| 424 | |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 425 | int main(int argc, char **argv) { |
| 426 | cl::ParseCommandLineOptions(argc, argv); |
Chris Lattner | d9f5d90 | 2006-03-03 01:47:14 +0000 | [diff] [blame] | 427 | ParseFile(InputFilename, IncludeDirs); |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 428 | |
Chris Lattner | 9a88638 | 2003-06-03 05:04:42 +0000 | [diff] [blame] | 429 | std::ostream *Out = &std::cout; |
| 430 | if (OutputFilename != "-") { |
Chris Lattner | 42df6d1 | 2004-07-13 06:11:46 +0000 | [diff] [blame] | 431 | Out = new std::ofstream(OutputFilename.c_str()); |
Chris Lattner | 9a88638 | 2003-06-03 05:04:42 +0000 | [diff] [blame] | 432 | |
| 433 | if (!Out->good()) { |
Chris Lattner | 42df6d1 | 2004-07-13 06:11:46 +0000 | [diff] [blame] | 434 | std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n"; |
Chris Lattner | 9a88638 | 2003-06-03 05:04:42 +0000 | [diff] [blame] | 435 | return 1; |
| 436 | } |
| 437 | |
| 438 | // Make sure the file gets removed if *gasp* tablegen crashes... |
Reid Spencer | 227b6d0 | 2004-11-14 22:30:54 +0000 | [diff] [blame] | 439 | sys::RemoveFileOnSignal(sys::Path(OutputFilename)); |
Chris Lattner | 9a88638 | 2003-06-03 05:04:42 +0000 | [diff] [blame] | 440 | } |
| 441 | |
Chris Lattner | 1d1adea | 2003-08-01 04:39:05 +0000 | [diff] [blame] | 442 | try { |
| 443 | switch (Action) { |
Chris Lattner | accd8ab | 2003-08-01 04:47:20 +0000 | [diff] [blame] | 444 | case PrintRecords: |
| 445 | *Out << Records; // No argument, dump all contents |
| 446 | break; |
Chris Lattner | 1d1adea | 2003-08-01 04:39:05 +0000 | [diff] [blame] | 447 | case Parse: |
| 448 | ParseMachineCode(); |
| 449 | break; |
| 450 | case GenEmitter: |
| 451 | CodeEmitterGen(Records).run(*Out); |
| 452 | break; |
Chris Lattner | 169e66b | 2003-08-03 17:24:20 +0000 | [diff] [blame] | 453 | |
Chris Lattner | 54d156d | 2003-08-01 05:59:20 +0000 | [diff] [blame] | 454 | case GenRegisterEnums: |
| 455 | RegisterInfoEmitter(Records).runEnums(*Out); |
| 456 | break; |
Chris Lattner | 1d1adea | 2003-08-01 04:39:05 +0000 | [diff] [blame] | 457 | case GenRegister: |
| 458 | RegisterInfoEmitter(Records).run(*Out); |
| 459 | break; |
| 460 | case GenRegisterHeader: |
| 461 | RegisterInfoEmitter(Records).runHeader(*Out); |
| 462 | break; |
Chris Lattner | 169e66b | 2003-08-03 17:24:20 +0000 | [diff] [blame] | 463 | |
| 464 | case GenInstrEnums: |
| 465 | InstrInfoEmitter(Records).runEnums(*Out); |
| 466 | break; |
Chris Lattner | 15de32d | 2003-08-03 21:58:28 +0000 | [diff] [blame] | 467 | case GenInstrs: |
| 468 | InstrInfoEmitter(Records).run(*Out); |
| 469 | break; |
Chris Lattner | 2e1f51b | 2004-08-01 05:59:33 +0000 | [diff] [blame] | 470 | |
| 471 | case GenAsmWriter: |
| 472 | AsmWriterEmitter(Records).run(*Out); |
| 473 | break; |
| 474 | |
Chris Lattner | 4a24c64 | 2005-09-03 01:14:03 +0000 | [diff] [blame] | 475 | case GenDAGISel: |
| 476 | DAGISelEmitter(Records).run(*Out); |
| 477 | break; |
Jim Laskey | f5fc2cb | 2005-10-21 19:05:19 +0000 | [diff] [blame] | 478 | case GenSubtarget: |
| 479 | SubtargetEmitter(Records).run(*Out); |
| 480 | break; |
Chris Lattner | 9e493cf | 2006-03-03 02:32:46 +0000 | [diff] [blame^] | 481 | case GenIntrinsic: |
| 482 | IntrinsicEmitter(Records).run(*Out); |
| 483 | break; |
Chris Lattner | 1d1adea | 2003-08-01 04:39:05 +0000 | [diff] [blame] | 484 | case PrintEnums: |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 485 | { |
Chris Lattner | 1d1adea | 2003-08-01 04:39:05 +0000 | [diff] [blame] | 486 | std::vector<Record*> Recs = Records.getAllDerivedDefinitions(Class); |
Chris Lattner | 1d1adea | 2003-08-01 04:39:05 +0000 | [diff] [blame] | 487 | for (unsigned i = 0, e = Recs.size(); i != e; ++i) |
Chris Lattner | 7b9ee51 | 2004-02-06 03:19:17 +0000 | [diff] [blame] | 488 | *Out << Recs[i]->getName() << ", "; |
Chris Lattner | 1d1adea | 2003-08-01 04:39:05 +0000 | [diff] [blame] | 489 | *Out << "\n"; |
| 490 | break; |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 491 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 492 | default: |
| 493 | assert(1 && "Invalid Action"); |
| 494 | return 1; |
| 495 | } |
Chris Lattner | 1d1adea | 2003-08-01 04:39:05 +0000 | [diff] [blame] | 496 | } catch (const std::string &Error) { |
Reid Spencer | 23f7d51 | 2004-09-03 23:17:54 +0000 | [diff] [blame] | 497 | std::cerr << argv[0] << ": " << Error << "\n"; |
Chris Lattner | f1e366a | 2003-08-01 19:21:43 +0000 | [diff] [blame] | 498 | if (Out != &std::cout) { |
| 499 | delete Out; // Close the file |
| 500 | std::remove(OutputFilename.c_str()); // Remove the file, it's broken |
| 501 | } |
Chris Lattner | 1d1adea | 2003-08-01 04:39:05 +0000 | [diff] [blame] | 502 | return 1; |
Reid Spencer | 23f7d51 | 2004-09-03 23:17:54 +0000 | [diff] [blame] | 503 | } catch (...) { |
| 504 | std::cerr << argv[0] << ": Unknown unexpected exception occurred.\n"; |
| 505 | if (Out != &std::cout) { |
| 506 | delete Out; // Close the file |
| 507 | std::remove(OutputFilename.c_str()); // Remove the file, it's broken |
| 508 | } |
| 509 | return 2; |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 510 | } |
Chris Lattner | 9a88638 | 2003-06-03 05:04:42 +0000 | [diff] [blame] | 511 | |
Chris Lattner | e79c72d | 2003-08-01 20:35:01 +0000 | [diff] [blame] | 512 | if (Out != &std::cout) { |
| 513 | delete Out; // Close the file |
Chris Lattner | e79c72d | 2003-08-01 20:35:01 +0000 | [diff] [blame] | 514 | } |
Chris Lattner | 1d1adea | 2003-08-01 04:39:05 +0000 | [diff] [blame] | 515 | return 0; |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 516 | } |