2003-08-01 04:39:05 +00:00
|
|
|
//===- TableGen.cpp - Top-Level TableGen implementation -------------------===//
|
2005-04-22 00:00:37 +00:00
|
|
|
//
|
2003-10-20 20:20:30 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
2005-04-22 00:00:37 +00:00
|
|
|
//
|
2003-10-20 20:20:30 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2003-08-01 04:39:05 +00:00
|
|
|
//
|
|
|
|
// TableGen is a tool which can be used to build up a description of something,
|
|
|
|
// then invoke one or more "tablegen backends" to emit information about the
|
|
|
|
// description in some predefined format. In practice, this is used by the LLVM
|
|
|
|
// code generators to automate generation of a code generator through a
|
|
|
|
// high-level description of the target.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2002-12-02 01:23:04 +00:00
|
|
|
#include "Record.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2004-05-27 05:41:36 +00:00
|
|
|
#include "llvm/System/Signals.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/FileUtilities.h"
|
2003-05-24 00:17:12 +00:00
|
|
|
#include "CodeEmitterGen.h"
|
2003-08-01 04:39:05 +00:00
|
|
|
#include "RegisterInfoEmitter.h"
|
2003-08-03 17:24:20 +00:00
|
|
|
#include "InstrInfoEmitter.h"
|
2004-08-01 05:59:33 +00:00
|
|
|
#include "AsmWriterEmitter.h"
|
2003-08-06 04:47:56 +00:00
|
|
|
#include "InstrSelectorEmitter.h"
|
2002-12-02 01:23:04 +00:00
|
|
|
#include <algorithm>
|
2003-08-14 16:05:35 +00:00
|
|
|
#include <cstdio>
|
2003-06-03 05:04:42 +00:00
|
|
|
#include <fstream>
|
2004-08-01 03:55:39 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2003-06-03 04:56:29 +00:00
|
|
|
enum ActionType {
|
|
|
|
PrintRecords,
|
|
|
|
GenEmitter,
|
2003-08-01 05:59:20 +00:00
|
|
|
GenRegisterEnums, GenRegister, GenRegisterHeader,
|
2004-08-01 05:59:33 +00:00
|
|
|
GenInstrEnums, GenInstrs, GenAsmWriter, GenInstrSelector,
|
2003-06-03 04:56:29 +00:00
|
|
|
PrintEnums,
|
2004-07-22 21:32:38 +00:00
|
|
|
Parse
|
2003-06-03 04:56:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
cl::opt<ActionType>
|
|
|
|
Action(cl::desc("Action to perform:"),
|
|
|
|
cl::values(clEnumValN(PrintRecords, "print-records",
|
2003-06-03 05:07:28 +00:00
|
|
|
"Print all records to stdout (default)"),
|
2003-06-03 04:56:29 +00:00
|
|
|
clEnumValN(GenEmitter, "gen-emitter",
|
|
|
|
"Generate machine code emitter"),
|
2003-08-01 05:59:20 +00:00
|
|
|
clEnumValN(GenRegisterEnums, "gen-register-enums",
|
|
|
|
"Generate enum values for registers"),
|
2003-08-01 04:39:05 +00:00
|
|
|
clEnumValN(GenRegister, "gen-register-desc",
|
|
|
|
"Generate a register info description"),
|
|
|
|
clEnumValN(GenRegisterHeader, "gen-register-desc-header",
|
|
|
|
"Generate a register info description header"),
|
2003-08-03 17:24:20 +00:00
|
|
|
clEnumValN(GenInstrEnums, "gen-instr-enums",
|
|
|
|
"Generate enum values for instructions"),
|
2003-08-03 21:58:28 +00:00
|
|
|
clEnumValN(GenInstrs, "gen-instr-desc",
|
|
|
|
"Generate instruction descriptions"),
|
2004-08-01 05:59:33 +00:00
|
|
|
clEnumValN(GenAsmWriter, "gen-asm-writer",
|
|
|
|
"Generate assembly writer"),
|
2003-08-06 04:47:56 +00:00
|
|
|
clEnumValN(GenInstrSelector, "gen-instr-selector",
|
|
|
|
"Generate an instruction selector"),
|
2003-06-03 04:56:29 +00:00
|
|
|
clEnumValN(PrintEnums, "print-enums",
|
|
|
|
"Print enum values for a class"),
|
|
|
|
clEnumValN(Parse, "parse",
|
|
|
|
"Interpret machine code (testing only)"),
|
2004-07-16 00:02:21 +00:00
|
|
|
clEnumValEnd));
|
2003-06-03 04:56:29 +00:00
|
|
|
|
|
|
|
cl::opt<std::string>
|
2003-06-03 05:07:28 +00:00
|
|
|
Class("class", cl::desc("Print Enum list for this class"),
|
|
|
|
cl::value_desc("class name"));
|
2003-06-03 05:04:42 +00:00
|
|
|
|
2003-07-30 19:48:02 +00:00
|
|
|
cl::opt<std::string>
|
|
|
|
OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"),
|
|
|
|
cl::init("-"));
|
|
|
|
|
|
|
|
cl::opt<std::string>
|
|
|
|
InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
|
2003-08-27 13:41:57 +00:00
|
|
|
|
|
|
|
cl::opt<std::string>
|
|
|
|
IncludeDir("I", cl::desc("Directory of include files"),
|
|
|
|
cl::value_desc("directory"), cl::init(""));
|
2003-06-03 04:56:29 +00:00
|
|
|
}
|
|
|
|
|
2004-08-01 03:55:39 +00:00
|
|
|
namespace llvm {
|
|
|
|
void ParseFile(const std::string &Filename,
|
|
|
|
const std::string &IncludeDir);
|
|
|
|
}
|
2002-12-02 01:23:04 +00:00
|
|
|
|
2004-08-01 03:55:39 +00:00
|
|
|
RecordKeeper llvm::Records;
|
2002-12-02 01:23:04 +00:00
|
|
|
|
|
|
|
static Init *getBit(Record *R, unsigned BitNo) {
|
|
|
|
const std::vector<RecordVal> &V = R->getValues();
|
|
|
|
for (unsigned i = 0, e = V.size(); i != e; ++i)
|
|
|
|
if (V[i].getPrefix()) {
|
|
|
|
assert(dynamic_cast<BitsInit*>(V[i].getValue()) &&
|
2005-04-22 04:13:13 +00:00
|
|
|
"Can only handle fields of bits<> type!");
|
2002-12-02 01:23:04 +00:00
|
|
|
BitsInit *I = (BitsInit*)V[i].getValue();
|
|
|
|
if (BitNo < I->getNumBits())
|
2005-04-22 04:13:13 +00:00
|
|
|
return I->getBit(BitNo);
|
2002-12-02 01:23:04 +00:00
|
|
|
BitNo -= I->getNumBits();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cerr << "Cannot find requested bit!\n";
|
2004-02-13 16:37:43 +00:00
|
|
|
exit(1);
|
2002-12-02 01:23:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned getNumBits(Record *R) {
|
|
|
|
const std::vector<RecordVal> &V = R->getValues();
|
|
|
|
unsigned Num = 0;
|
|
|
|
for (unsigned i = 0, e = V.size(); i != e; ++i)
|
|
|
|
if (V[i].getPrefix()) {
|
|
|
|
assert(dynamic_cast<BitsInit*>(V[i].getValue()) &&
|
2005-04-22 04:13:13 +00:00
|
|
|
"Can only handle fields of bits<> type!");
|
2002-12-02 01:23:04 +00:00
|
|
|
Num += ((BitsInit*)V[i].getValue())->getNumBits();
|
|
|
|
}
|
|
|
|
return Num;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool BitsAreFixed(Record *I1, Record *I2, unsigned BitNo) {
|
|
|
|
return dynamic_cast<BitInit*>(getBit(I1, BitNo)) &&
|
|
|
|
dynamic_cast<BitInit*>(getBit(I2, BitNo));
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool BitsAreEqual(Record *I1, Record *I2, unsigned BitNo) {
|
|
|
|
BitInit *Bit1 = dynamic_cast<BitInit*>(getBit(I1, BitNo));
|
|
|
|
BitInit *Bit2 = dynamic_cast<BitInit*>(getBit(I2, BitNo));
|
|
|
|
|
|
|
|
return Bit1 && Bit2 && Bit1->getValue() == Bit2->getValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool BitRangesEqual(Record *I1, Record *I2,
|
2005-04-22 04:13:13 +00:00
|
|
|
unsigned Start, unsigned End) {
|
2002-12-02 01:23:04 +00:00
|
|
|
for (unsigned i = Start; i != End; ++i)
|
|
|
|
if (!BitsAreEqual(I1, I2, i))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned getFirstFixedBit(Record *R, unsigned FirstFixedBit) {
|
|
|
|
// Look for the first bit of the pair that are required to be 0 or 1.
|
|
|
|
while (!dynamic_cast<BitInit*>(getBit(R, FirstFixedBit)))
|
|
|
|
++FirstFixedBit;
|
|
|
|
return FirstFixedBit;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void FindInstDifferences(Record *I1, Record *I2,
|
2005-04-22 04:13:13 +00:00
|
|
|
unsigned FirstFixedBit, unsigned MaxBits,
|
|
|
|
unsigned &FirstVaryingBitOverall,
|
|
|
|
unsigned &LastFixedBitOverall) {
|
2002-12-02 01:23:04 +00:00
|
|
|
// Compare the first instruction to the rest of the instructions, looking for
|
|
|
|
// fields that differ.
|
|
|
|
//
|
|
|
|
unsigned FirstVaryingBit = FirstFixedBit;
|
|
|
|
while (FirstVaryingBit < MaxBits && BitsAreEqual(I1, I2, FirstVaryingBit))
|
|
|
|
++FirstVaryingBit;
|
|
|
|
|
|
|
|
unsigned LastFixedBit = FirstVaryingBit;
|
|
|
|
while (LastFixedBit < MaxBits && BitsAreFixed(I1, I2, LastFixedBit))
|
|
|
|
++LastFixedBit;
|
2005-04-22 00:00:37 +00:00
|
|
|
|
2002-12-02 01:23:04 +00:00
|
|
|
if (FirstVaryingBit < FirstVaryingBitOverall)
|
|
|
|
FirstVaryingBitOverall = FirstVaryingBit;
|
|
|
|
if (LastFixedBit < LastFixedBitOverall)
|
|
|
|
LastFixedBitOverall = LastFixedBit;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool getBitValue(Record *R, unsigned BitNo) {
|
|
|
|
Init *I = getBit(R, BitNo);
|
|
|
|
assert(dynamic_cast<BitInit*>(I) && "Bit should be fixed!");
|
|
|
|
return ((BitInit*)I)->getValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct BitComparator {
|
|
|
|
unsigned BitBegin, BitEnd;
|
|
|
|
BitComparator(unsigned B, unsigned E) : BitBegin(B), BitEnd(E) {}
|
|
|
|
|
|
|
|
bool operator()(Record *R1, Record *R2) { // Return true if R1 is less than R2
|
|
|
|
for (unsigned i = BitBegin; i != BitEnd; ++i) {
|
|
|
|
bool V1 = getBitValue(R1, i), V2 = getBitValue(R2, i);
|
|
|
|
if (V1 < V2)
|
2005-04-22 04:13:13 +00:00
|
|
|
return true;
|
2002-12-02 01:23:04 +00:00
|
|
|
else if (V2 < V1)
|
2005-04-22 04:13:13 +00:00
|
|
|
return false;
|
2002-12-02 01:23:04 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2005-04-22 00:00:37 +00:00
|
|
|
static void PrintRange(std::vector<Record*>::iterator I,
|
2005-04-22 04:13:13 +00:00
|
|
|
std::vector<Record*>::iterator E) {
|
2002-12-02 01:23:04 +00:00
|
|
|
while (I != E) std::cerr << **I++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool getMemoryBit(unsigned char *M, unsigned i) {
|
|
|
|
return (M[i/8] & (1 << (i&7))) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned getFirstFixedBitInSequence(std::vector<Record*>::iterator IB,
|
2005-04-22 04:13:13 +00:00
|
|
|
std::vector<Record*>::iterator IE,
|
|
|
|
unsigned StartBit) {
|
2002-12-02 01:23:04 +00:00
|
|
|
unsigned FirstFixedBit = 0;
|
|
|
|
for (std::vector<Record*>::iterator I = IB; I != IE; ++I)
|
|
|
|
FirstFixedBit = std::max(FirstFixedBit, getFirstFixedBit(*I, StartBit));
|
|
|
|
return FirstFixedBit;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseMachineCode - Try to split the vector of instructions (which is
|
2003-09-07 20:12:52 +00:00
|
|
|
// intentionally taken by-copy) in half, narrowing down the possible
|
|
|
|
// instructions that we may have found. Eventually, this list will get pared
|
|
|
|
// down to zero or one instruction, in which case we have a match or failure.
|
2002-12-02 01:23:04 +00:00
|
|
|
//
|
2005-04-22 00:00:37 +00:00
|
|
|
static Record *ParseMachineCode(std::vector<Record*>::iterator InstsB,
|
2005-04-22 04:13:13 +00:00
|
|
|
std::vector<Record*>::iterator InstsE,
|
|
|
|
unsigned char *M) {
|
2002-12-02 01:23:04 +00:00
|
|
|
assert(InstsB != InstsE && "Empty range?");
|
|
|
|
if (InstsB+1 == InstsE) {
|
|
|
|
// Only a single instruction, see if we match it...
|
|
|
|
Record *Inst = *InstsB;
|
|
|
|
for (unsigned i = 0, e = getNumBits(Inst); i != e; ++i)
|
|
|
|
if (BitInit *BI = dynamic_cast<BitInit*>(getBit(Inst, i)))
|
2005-04-22 04:13:13 +00:00
|
|
|
if (getMemoryBit(M, i) != BI->getValue())
|
|
|
|
throw std::string("Parse failed!\n");
|
2002-12-02 01:23:04 +00:00
|
|
|
return Inst;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned MaxBits = ~0;
|
|
|
|
for (std::vector<Record*>::iterator I = InstsB; I != InstsE; ++I)
|
|
|
|
MaxBits = std::min(MaxBits, getNumBits(*I));
|
|
|
|
|
|
|
|
unsigned FirstFixedBit = getFirstFixedBitInSequence(InstsB, InstsE, 0);
|
|
|
|
unsigned FirstVaryingBit, LastFixedBit;
|
|
|
|
do {
|
|
|
|
FirstVaryingBit = ~0;
|
|
|
|
LastFixedBit = ~0;
|
|
|
|
for (std::vector<Record*>::iterator I = InstsB+1; I != InstsE; ++I)
|
|
|
|
FindInstDifferences(*InstsB, *I, FirstFixedBit, MaxBits,
|
2005-04-22 04:13:13 +00:00
|
|
|
FirstVaryingBit, LastFixedBit);
|
2002-12-02 01:23:04 +00:00
|
|
|
if (FirstVaryingBit == MaxBits) {
|
|
|
|
std::cerr << "ERROR: Could not find bit to distinguish between "
|
2005-04-22 04:13:13 +00:00
|
|
|
<< "the following entries!\n";
|
2002-12-02 01:23:04 +00:00
|
|
|
PrintRange(InstsB, InstsE);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
std::cerr << "FVB: " << FirstVaryingBit << " - " << LastFixedBit
|
2005-04-22 04:13:13 +00:00
|
|
|
<< ": " << InstsE-InstsB << "\n";
|
2002-12-02 01:23:04 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
FirstFixedBit = getFirstFixedBitInSequence(InstsB, InstsE, FirstVaryingBit);
|
|
|
|
} while (FirstVaryingBit != FirstFixedBit);
|
|
|
|
|
|
|
|
//std::cerr << "\n\nXXXXXXXXXXXXXXXXX\n\n";
|
|
|
|
//PrintRange(InstsB, InstsE);
|
|
|
|
|
|
|
|
// Sort the Insts list so that the entries have all of the bits in the range
|
|
|
|
// [FirstVaryingBit,LastFixedBit) sorted. These bits are all guaranteed to be
|
|
|
|
// set to either 0 or 1 (BitInit values), which simplifies things.
|
|
|
|
//
|
|
|
|
std::sort(InstsB, InstsE, BitComparator(FirstVaryingBit, LastFixedBit));
|
|
|
|
|
|
|
|
// Once the list is sorted by these bits, split the bit list into smaller
|
|
|
|
// lists, and recurse on each one.
|
|
|
|
//
|
|
|
|
std::vector<Record*>::iterator RangeBegin = InstsB;
|
|
|
|
Record *Match = 0;
|
|
|
|
while (RangeBegin != InstsE) {
|
|
|
|
std::vector<Record*>::iterator RangeEnd = RangeBegin+1;
|
|
|
|
while (RangeEnd != InstsE &&
|
|
|
|
BitRangesEqual(*RangeBegin, *RangeEnd, FirstVaryingBit, LastFixedBit))
|
|
|
|
++RangeEnd;
|
2005-04-22 00:00:37 +00:00
|
|
|
|
2002-12-02 01:23:04 +00:00
|
|
|
// We just identified a range of equal instructions. If this range is the
|
|
|
|
// input range, we were not able to distinguish between the instructions in
|
|
|
|
// the set. Print an error and exit!
|
|
|
|
//
|
|
|
|
if (RangeBegin == InstsB && RangeEnd == InstsE) {
|
|
|
|
std::cerr << "Error: Could not distinguish among the following insts!:\n";
|
|
|
|
PrintRange(InstsB, InstsE);
|
2004-02-13 16:37:43 +00:00
|
|
|
exit(1);
|
2002-12-02 01:23:04 +00:00
|
|
|
}
|
2005-04-22 00:00:37 +00:00
|
|
|
|
2002-12-03 20:01:04 +00:00
|
|
|
#if 0
|
|
|
|
std::cerr << "FVB: " << FirstVaryingBit << " - " << LastFixedBit
|
2005-04-22 04:13:13 +00:00
|
|
|
<< ": [" << RangeEnd-RangeBegin << "] - ";
|
2002-12-03 20:01:04 +00:00
|
|
|
for (int i = LastFixedBit-1; i >= (int)FirstVaryingBit; --i)
|
|
|
|
std::cerr << (int)((BitInit*)getBit(*RangeBegin, i))->getValue() << " ";
|
|
|
|
std::cerr << "\n";
|
|
|
|
#endif
|
|
|
|
|
2002-12-02 01:23:04 +00:00
|
|
|
if (Record *R = ParseMachineCode(RangeBegin, RangeEnd, M)) {
|
|
|
|
if (Match) {
|
2005-04-22 04:13:13 +00:00
|
|
|
std::cerr << "Error: Multiple matches found:\n";
|
|
|
|
PrintRange(InstsB, InstsE);
|
2002-12-02 01:23:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(Match == 0 && "Multiple matches??");
|
|
|
|
Match = R;
|
|
|
|
}
|
|
|
|
RangeBegin = RangeEnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Match;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void PrintValue(Record *I, unsigned char *Ptr, const RecordVal &Val) {
|
|
|
|
assert(dynamic_cast<BitsInit*>(Val.getValue()) &&
|
2005-04-22 04:13:13 +00:00
|
|
|
"Can only handle undefined bits<> types!");
|
2002-12-02 01:23:04 +00:00
|
|
|
BitsInit *BI = (BitsInit*)Val.getValue();
|
|
|
|
assert(BI->getNumBits() <= 32 && "Can only handle fields up to 32 bits!");
|
|
|
|
|
|
|
|
unsigned Value = 0;
|
|
|
|
const std::vector<RecordVal> &Vals = I->getValues();
|
|
|
|
|
|
|
|
// Start by filling in fixed values...
|
|
|
|
for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
|
|
|
|
if (BitInit *B = dynamic_cast<BitInit*>(BI->getBit(i)))
|
|
|
|
Value |= B->getValue() << i;
|
2005-04-22 00:00:37 +00:00
|
|
|
|
2002-12-02 01:23:04 +00:00
|
|
|
// Loop over all of the fields in the instruction adding in any
|
|
|
|
// contributions to this value (due to bit references).
|
|
|
|
//
|
|
|
|
unsigned Offset = 0;
|
|
|
|
for (unsigned f = 0, e = Vals.size(); f != e; ++f)
|
|
|
|
if (Vals[f].getPrefix()) {
|
2002-12-02 17:43:43 +00:00
|
|
|
BitsInit *FieldInitializer = (BitsInit*)Vals[f].getValue();
|
2002-12-02 01:23:04 +00:00
|
|
|
if (&Vals[f] == &Val) {
|
2005-04-22 04:13:13 +00:00
|
|
|
// Read the bits directly now...
|
|
|
|
for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
|
|
|
|
Value |= getMemoryBit(Ptr, Offset+i) << i;
|
|
|
|
break;
|
2002-12-02 01:23:04 +00:00
|
|
|
}
|
2005-04-22 00:00:37 +00:00
|
|
|
|
2002-12-02 01:23:04 +00:00
|
|
|
// Scan through the field looking for bit initializers of the current
|
|
|
|
// variable...
|
2002-12-02 17:43:43 +00:00
|
|
|
for (unsigned i = 0, e = FieldInitializer->getNumBits(); i != e; ++i)
|
2005-04-22 04:13:13 +00:00
|
|
|
if (VarBitInit *VBI =
|
|
|
|
dynamic_cast<VarBitInit*>(FieldInitializer->getBit(i))) {
|
2002-12-02 17:43:43 +00:00
|
|
|
TypedInit *TI = VBI->getVariable();
|
|
|
|
if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
|
|
|
|
if (VI->getName() == Val.getName())
|
|
|
|
Value |= getMemoryBit(Ptr, Offset+i) << VBI->getBitNum();
|
|
|
|
} else if (FieldInit *FI = dynamic_cast<FieldInit*>(TI)) {
|
|
|
|
// FIXME: implement this!
|
|
|
|
std::cerr << "FIELD INIT not implemented yet!\n";
|
|
|
|
}
|
2005-04-22 04:13:13 +00:00
|
|
|
}
|
2002-12-02 17:43:43 +00:00
|
|
|
Offset += FieldInitializer->getNumBits();
|
2002-12-02 01:23:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << "0x" << std::hex << Value << std::dec;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void PrintInstruction(Record *I, unsigned char *Ptr) {
|
|
|
|
std::cout << "Inst " << getNumBits(I)/8 << " bytes: "
|
2005-04-22 04:13:13 +00:00
|
|
|
<< "\t" << I->getName() << "\t" << *I->getValue("Name")->getValue()
|
|
|
|
<< "\t";
|
2005-04-22 00:00:37 +00:00
|
|
|
|
2002-12-02 01:23:04 +00:00
|
|
|
const std::vector<RecordVal> &Vals = I->getValues();
|
|
|
|
for (unsigned i = 0, e = Vals.size(); i != e; ++i)
|
|
|
|
if (!Vals[i].getValue()->isComplete()) {
|
|
|
|
std::cout << Vals[i].getName() << "=";
|
|
|
|
PrintValue(I, Ptr, Vals[i]);
|
|
|
|
std::cout << "\t";
|
|
|
|
}
|
2005-04-22 00:00:37 +00:00
|
|
|
|
2002-12-02 01:23:04 +00:00
|
|
|
std::cout << "\n";// << *I;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ParseMachineCode() {
|
2003-05-24 00:17:12 +00:00
|
|
|
// X86 code
|
2002-12-03 20:01:04 +00:00
|
|
|
unsigned char Buffer[] = {
|
|
|
|
0x55, // push EBP
|
2005-04-22 04:13:13 +00:00
|
|
|
0x89, 0xE5, // mov EBP, ESP
|
|
|
|
//0x83, 0xEC, 0x08, // sub ESP, 0x8
|
|
|
|
0xE8, 1, 2, 3, 4, // call +0x04030201
|
|
|
|
0x89, 0xEC, // mov ESP, EBP
|
|
|
|
0x5D, // pop EBP
|
|
|
|
0xC3, // ret
|
|
|
|
0x90, // nop
|
|
|
|
0xC9, // leave
|
|
|
|
0x89, 0xF6, // mov ESI, ESI
|
|
|
|
0x68, 1, 2, 3, 4, // push 0x04030201
|
|
|
|
0x5e, // pop ESI
|
|
|
|
0xFF, 0xD0, // call EAX
|
|
|
|
0xB8, 1, 2, 3, 4, // mov EAX, 0x04030201
|
|
|
|
0x85, 0xC0, // test EAX, EAX
|
|
|
|
0xF4, // hlt
|
2002-12-02 01:23:04 +00:00
|
|
|
};
|
2003-05-24 00:17:12 +00:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
// SparcV9 code
|
2005-04-22 00:00:37 +00:00
|
|
|
unsigned char Buffer[] = { 0xbf, 0xe0, 0x20, 0x1f, 0x1, 0x0, 0x0, 0x1,
|
2003-05-24 00:17:12 +00:00
|
|
|
0x0, 0x0, 0x0, 0x0, 0xc1, 0x0, 0x20, 0x1, 0x1,
|
|
|
|
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
|
|
|
|
0x0, 0x0, 0x0, 0x0, 0x0, 0x40, 0x0, 0x0, 0x0, 0x1,
|
|
|
|
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
|
|
|
|
0x0, 0x0, 0xaf, 0xe8, 0x20, 0x17
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2003-08-01 04:39:05 +00:00
|
|
|
std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
|
2002-12-02 01:23:04 +00:00
|
|
|
|
|
|
|
unsigned char *BuffPtr = Buffer;
|
|
|
|
while (1) {
|
|
|
|
Record *R = ParseMachineCode(Insts.begin(), Insts.end(), BuffPtr);
|
|
|
|
PrintInstruction(R, BuffPtr);
|
|
|
|
|
|
|
|
unsigned Bits = getNumBits(R);
|
|
|
|
assert((Bits & 7) == 0 && "Instruction is not an even number of bytes!");
|
|
|
|
BuffPtr += Bits/8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
cl::ParseCommandLineOptions(argc, argv);
|
2003-08-27 13:41:57 +00:00
|
|
|
ParseFile(InputFilename, IncludeDir);
|
2002-12-02 01:23:04 +00:00
|
|
|
|
2003-06-03 05:04:42 +00:00
|
|
|
std::ostream *Out = &std::cout;
|
|
|
|
if (OutputFilename != "-") {
|
2004-07-13 06:11:46 +00:00
|
|
|
Out = new std::ofstream(OutputFilename.c_str());
|
2003-06-03 05:04:42 +00:00
|
|
|
|
|
|
|
if (!Out->good()) {
|
2004-07-13 06:11:46 +00:00
|
|
|
std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
|
2003-06-03 05:04:42 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the file gets removed if *gasp* tablegen crashes...
|
2004-11-14 22:30:54 +00:00
|
|
|
sys::RemoveFileOnSignal(sys::Path(OutputFilename));
|
2003-06-03 05:04:42 +00:00
|
|
|
}
|
|
|
|
|
2003-08-01 04:39:05 +00:00
|
|
|
try {
|
|
|
|
switch (Action) {
|
2003-08-01 04:47:20 +00:00
|
|
|
case PrintRecords:
|
|
|
|
*Out << Records; // No argument, dump all contents
|
|
|
|
break;
|
2003-08-01 04:39:05 +00:00
|
|
|
case Parse:
|
|
|
|
ParseMachineCode();
|
|
|
|
break;
|
|
|
|
case GenEmitter:
|
|
|
|
CodeEmitterGen(Records).run(*Out);
|
|
|
|
break;
|
2003-08-03 17:24:20 +00:00
|
|
|
|
2003-08-01 05:59:20 +00:00
|
|
|
case GenRegisterEnums:
|
|
|
|
RegisterInfoEmitter(Records).runEnums(*Out);
|
|
|
|
break;
|
2003-08-01 04:39:05 +00:00
|
|
|
case GenRegister:
|
|
|
|
RegisterInfoEmitter(Records).run(*Out);
|
|
|
|
break;
|
|
|
|
case GenRegisterHeader:
|
|
|
|
RegisterInfoEmitter(Records).runHeader(*Out);
|
|
|
|
break;
|
2003-08-03 17:24:20 +00:00
|
|
|
|
|
|
|
case GenInstrEnums:
|
|
|
|
InstrInfoEmitter(Records).runEnums(*Out);
|
|
|
|
break;
|
2003-08-03 21:58:28 +00:00
|
|
|
case GenInstrs:
|
|
|
|
InstrInfoEmitter(Records).run(*Out);
|
|
|
|
break;
|
2004-08-01 05:59:33 +00:00
|
|
|
|
|
|
|
case GenAsmWriter:
|
|
|
|
AsmWriterEmitter(Records).run(*Out);
|
|
|
|
break;
|
|
|
|
|
2003-08-06 04:47:56 +00:00
|
|
|
case GenInstrSelector:
|
|
|
|
InstrSelectorEmitter(Records).run(*Out);
|
|
|
|
break;
|
2003-08-01 04:39:05 +00:00
|
|
|
case PrintEnums:
|
2003-11-11 22:41:34 +00:00
|
|
|
{
|
2003-08-01 04:39:05 +00:00
|
|
|
std::vector<Record*> Recs = Records.getAllDerivedDefinitions(Class);
|
|
|
|
for (unsigned i = 0, e = Recs.size(); i != e; ++i)
|
2004-02-06 03:19:17 +00:00
|
|
|
*Out << Recs[i]->getName() << ", ";
|
2003-08-01 04:39:05 +00:00
|
|
|
*Out << "\n";
|
|
|
|
break;
|
2002-12-02 01:23:04 +00:00
|
|
|
}
|
2003-11-11 22:41:34 +00:00
|
|
|
default:
|
|
|
|
assert(1 && "Invalid Action");
|
|
|
|
return 1;
|
|
|
|
}
|
2003-08-01 04:39:05 +00:00
|
|
|
} catch (const std::string &Error) {
|
2004-09-03 23:17:54 +00:00
|
|
|
std::cerr << argv[0] << ": " << Error << "\n";
|
2003-08-01 19:21:43 +00:00
|
|
|
if (Out != &std::cout) {
|
|
|
|
delete Out; // Close the file
|
|
|
|
std::remove(OutputFilename.c_str()); // Remove the file, it's broken
|
|
|
|
}
|
2003-08-01 04:39:05 +00:00
|
|
|
return 1;
|
2004-09-03 23:17:54 +00:00
|
|
|
} catch (...) {
|
|
|
|
std::cerr << argv[0] << ": Unknown unexpected exception occurred.\n";
|
|
|
|
if (Out != &std::cout) {
|
|
|
|
delete Out; // Close the file
|
|
|
|
std::remove(OutputFilename.c_str()); // Remove the file, it's broken
|
|
|
|
}
|
|
|
|
return 2;
|
2002-12-02 01:23:04 +00:00
|
|
|
}
|
2003-06-03 05:04:42 +00:00
|
|
|
|
2003-08-01 20:35:01 +00:00
|
|
|
if (Out != &std::cout) {
|
|
|
|
delete Out; // Close the file
|
|
|
|
}
|
2003-08-01 04:39:05 +00:00
|
|
|
return 0;
|
2002-12-02 01:23:04 +00:00
|
|
|
}
|