2003-07-31 04:43:49 +00:00
|
|
|
//===- CodeEmitterGen.cpp - Code Emitter Generator ------------------------===//
|
2005-04-22 00:00:37 +00:00
|
|
|
//
|
2003-10-20 20:20:30 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:37:13 +00:00
|
|
|
// This file 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-07-31 04:43:49 +00:00
|
|
|
//
|
2004-08-04 22:07:54 +00:00
|
|
|
// CodeEmitterGen uses the descriptions of instructions and their fields to
|
|
|
|
// construct an automated code emitter: a function that, given a MachineInstr,
|
|
|
|
// returns the (currently, 32-bit unsigned) value of the instruction.
|
2003-07-31 04:43:49 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2003-05-24 00:15:53 +00:00
|
|
|
#include "CodeEmitterGen.h"
|
2004-08-09 19:10:43 +00:00
|
|
|
#include "CodeGenTarget.h"
|
2003-07-31 04:43:49 +00:00
|
|
|
#include "Record.h"
|
2006-07-13 21:02:53 +00:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2004-08-01 03:55:39 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2006-07-13 21:02:53 +00:00
|
|
|
void CodeEmitterGen::reverseBits(std::vector<Record*> &Insts) {
|
|
|
|
for (std::vector<Record*>::iterator I = Insts.begin(), E = Insts.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
Record *R = *I;
|
2010-07-02 21:44:22 +00:00
|
|
|
if (R->getValueAsString("Namespace") == "TargetOpcode")
|
|
|
|
continue;
|
2009-04-13 15:38:05 +00:00
|
|
|
|
2006-07-13 21:02:53 +00:00
|
|
|
BitsInit *BI = R->getValueAsBitsInit("Inst");
|
2004-10-14 05:53:01 +00:00
|
|
|
|
2006-07-13 21:02:53 +00:00
|
|
|
unsigned numBits = BI->getNumBits();
|
|
|
|
BitsInit *NewBI = new BitsInit(numBits);
|
|
|
|
for (unsigned bit = 0, end = numBits / 2; bit != end; ++bit) {
|
|
|
|
unsigned bitSwapIdx = numBits - bit - 1;
|
|
|
|
Init *OrigBit = BI->getBit(bit);
|
|
|
|
Init *BitSwap = BI->getBit(bitSwapIdx);
|
|
|
|
NewBI->setBit(bit, BitSwap);
|
|
|
|
NewBI->setBit(bitSwapIdx, OrigBit);
|
|
|
|
}
|
|
|
|
if (numBits % 2) {
|
|
|
|
unsigned middle = (numBits + 1) / 2;
|
|
|
|
NewBI->setBit(middle, BI->getBit(middle));
|
2004-10-14 05:53:01 +00:00
|
|
|
}
|
2010-10-07 16:56:28 +00:00
|
|
|
|
2006-07-13 21:02:53 +00:00
|
|
|
// Update the bits in reversed order so that emitInstrOpBits will get the
|
|
|
|
// correct endianness.
|
|
|
|
R->getValue("Inst")->setValue(NewBI);
|
2004-10-14 05:53:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-07-13 22:17:08 +00:00
|
|
|
// If the VarBitInit at position 'bit' matches the specified variable then
|
|
|
|
// return the variable bit position. Otherwise return -1.
|
2009-12-15 20:21:44 +00:00
|
|
|
int CodeEmitterGen::getVariableBit(const std::string &VarName,
|
2006-07-13 22:17:08 +00:00
|
|
|
BitsInit *BI, int bit) {
|
2006-07-13 21:02:53 +00:00
|
|
|
if (VarBitInit *VBI = dynamic_cast<VarBitInit*>(BI->getBit(bit))) {
|
|
|
|
TypedInit *TI = VBI->getVariable();
|
2010-10-07 16:56:28 +00:00
|
|
|
|
2009-12-15 20:21:44 +00:00
|
|
|
if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
|
|
|
|
if (VI->getName() == VarName) return VBI->getBitNum();
|
|
|
|
}
|
2006-07-13 21:02:53 +00:00
|
|
|
}
|
2010-10-07 16:56:28 +00:00
|
|
|
|
2006-07-13 21:02:53 +00:00
|
|
|
return -1;
|
2010-10-07 16:56:28 +00:00
|
|
|
}
|
2006-07-13 21:02:53 +00:00
|
|
|
|
|
|
|
|
2009-07-03 00:10:29 +00:00
|
|
|
void CodeEmitterGen::run(raw_ostream &o) {
|
2004-08-10 18:31:01 +00:00
|
|
|
CodeGenTarget Target;
|
2003-08-01 04:38:18 +00:00
|
|
|
std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
|
2010-10-07 16:56:28 +00:00
|
|
|
|
2006-07-13 21:02:53 +00:00
|
|
|
// For little-endian instruction bit encodings, reverse the bit order
|
|
|
|
if (Target.isLittleEndianEncoding()) reverseBits(Insts);
|
2003-05-24 00:15:53 +00:00
|
|
|
|
2003-08-06 04:36:35 +00:00
|
|
|
EmitSourceFileHeader("Machine Code Emitter", o);
|
2004-08-10 18:31:01 +00:00
|
|
|
std::string Namespace = Insts[0]->getValueAsString("Namespace") + "::";
|
2010-10-07 16:56:28 +00:00
|
|
|
|
2010-03-19 00:34:35 +00:00
|
|
|
const std::vector<const CodeGenInstruction*> &NumberedInstructions =
|
|
|
|
Target.getInstructionsByEnumValue();
|
2003-05-24 00:15:53 +00:00
|
|
|
|
2004-08-10 20:54:58 +00:00
|
|
|
// Emit function declaration
|
2004-08-10 18:31:01 +00:00
|
|
|
o << "unsigned " << Target.getName() << "CodeEmitter::"
|
2008-09-02 06:51:36 +00:00
|
|
|
<< "getBinaryCodeForInstr(const MachineInstr &MI) {\n";
|
2004-08-10 20:54:58 +00:00
|
|
|
|
2006-07-12 19:15:43 +00:00
|
|
|
// Emit instruction base values
|
|
|
|
o << " static const unsigned InstBits[] = {\n";
|
2010-03-19 00:34:35 +00:00
|
|
|
for (std::vector<const CodeGenInstruction*>::const_iterator
|
2006-07-12 19:15:43 +00:00
|
|
|
IN = NumberedInstructions.begin(),
|
|
|
|
EN = NumberedInstructions.end();
|
|
|
|
IN != EN; ++IN) {
|
|
|
|
const CodeGenInstruction *CGI = *IN;
|
|
|
|
Record *R = CGI->TheDef;
|
2010-10-07 16:56:28 +00:00
|
|
|
|
2010-07-02 21:44:22 +00:00
|
|
|
if (R->getValueAsString("Namespace") == "TargetOpcode") {
|
2008-09-17 06:29:52 +00:00
|
|
|
o << " 0U,\n";
|
2006-07-12 19:15:43 +00:00
|
|
|
continue;
|
|
|
|
}
|
2010-10-07 16:56:28 +00:00
|
|
|
|
2003-08-01 04:46:24 +00:00
|
|
|
BitsInit *BI = R->getValueAsBitsInit("Inst");
|
2003-05-24 00:15:53 +00:00
|
|
|
|
|
|
|
// Start by filling in fixed values...
|
2006-11-03 01:48:30 +00:00
|
|
|
unsigned Value = 0;
|
2003-05-27 22:19:58 +00:00
|
|
|
for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) {
|
|
|
|
if (BitInit *B = dynamic_cast<BitInit*>(BI->getBit(e-i-1))) {
|
|
|
|
Value |= B->getValue() << (e-i-1);
|
|
|
|
}
|
|
|
|
}
|
2008-09-17 06:29:52 +00:00
|
|
|
o << " " << Value << "U," << '\t' << "// " << R->getName() << "\n";
|
2006-07-12 19:15:43 +00:00
|
|
|
}
|
2008-09-17 06:29:52 +00:00
|
|
|
o << " 0U\n };\n";
|
2010-10-07 16:56:28 +00:00
|
|
|
|
2006-07-13 21:02:53 +00:00
|
|
|
// Map to accumulate all the cases.
|
|
|
|
std::map<std::string, std::vector<std::string> > CaseMap;
|
2010-10-07 16:56:28 +00:00
|
|
|
|
2006-07-13 21:02:53 +00:00
|
|
|
// Construct all cases statement for each opcode
|
|
|
|
for (std::vector<Record*>::iterator IC = Insts.begin(), EC = Insts.end();
|
|
|
|
IC != EC; ++IC) {
|
|
|
|
Record *R = *IC;
|
2010-07-02 21:44:22 +00:00
|
|
|
if (R->getValueAsString("Namespace") == "TargetOpcode")
|
|
|
|
continue;
|
2006-07-13 21:02:53 +00:00
|
|
|
const std::string &InstName = R->getName();
|
|
|
|
std::string Case("");
|
2009-04-13 15:38:05 +00:00
|
|
|
|
2006-07-12 19:15:43 +00:00
|
|
|
BitsInit *BI = R->getValueAsBitsInit("Inst");
|
|
|
|
const std::vector<RecordVal> &Vals = R->getValues();
|
2010-03-19 00:07:20 +00:00
|
|
|
CodeGenInstruction &CGI = Target.getInstruction(R);
|
2010-10-07 16:56:28 +00:00
|
|
|
|
2004-10-14 05:53:01 +00:00
|
|
|
// Loop over all of the fields in the instruction, determining which are the
|
2005-04-22 00:00:37 +00:00
|
|
|
// operands to the instruction.
|
2003-05-27 22:19:58 +00:00
|
|
|
unsigned op = 0;
|
2003-05-24 00:15:53 +00:00
|
|
|
for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
|
2004-10-14 05:53:01 +00:00
|
|
|
if (!Vals[i].getPrefix() && !Vals[i].getValue()->isComplete()) {
|
2003-07-15 21:00:32 +00:00
|
|
|
// Is the operand continuous? If so, we can just mask and OR it in
|
2005-04-22 00:00:37 +00:00
|
|
|
// instead of doing it bit-by-bit, saving a lot in runtime cost.
|
2009-12-15 20:21:44 +00:00
|
|
|
const std::string &VarName = Vals[i].getName();
|
2006-07-13 21:02:53 +00:00
|
|
|
bool gotOp = false;
|
2010-10-07 16:56:28 +00:00
|
|
|
|
2006-07-13 21:02:53 +00:00
|
|
|
for (int bit = BI->getNumBits()-1; bit >= 0; ) {
|
2009-12-15 20:21:44 +00:00
|
|
|
int varBit = getVariableBit(VarName, BI, bit);
|
2010-10-07 16:56:28 +00:00
|
|
|
|
2006-07-13 21:02:53 +00:00
|
|
|
if (varBit == -1) {
|
|
|
|
--bit;
|
|
|
|
} else {
|
|
|
|
int beginInstBit = bit;
|
|
|
|
int beginVarBit = varBit;
|
|
|
|
int N = 1;
|
2010-10-07 16:56:28 +00:00
|
|
|
|
2006-07-13 21:02:53 +00:00
|
|
|
for (--bit; bit >= 0;) {
|
2009-12-15 20:21:44 +00:00
|
|
|
varBit = getVariableBit(VarName, BI, bit);
|
2006-07-13 21:02:53 +00:00
|
|
|
if (varBit == -1 || varBit != (beginVarBit - N)) break;
|
|
|
|
++N;
|
|
|
|
--bit;
|
|
|
|
}
|
2003-07-15 21:00:32 +00:00
|
|
|
|
2006-07-13 21:02:53 +00:00
|
|
|
if (!gotOp) {
|
2006-11-15 23:23:02 +00:00
|
|
|
/// If this operand is not supposed to be emitted by the generated
|
|
|
|
/// emitter, skip it.
|
|
|
|
while (CGI.isFlatOperandNotEmitted(op))
|
|
|
|
++op;
|
2010-10-07 16:56:28 +00:00
|
|
|
|
2009-12-15 20:21:44 +00:00
|
|
|
Case += " // op: " + VarName + "\n"
|
2006-07-13 21:02:53 +00:00
|
|
|
+ " op = getMachineOpValue(MI, MI.getOperand("
|
2006-11-15 23:23:02 +00:00
|
|
|
+ utostr(op++) + "));\n";
|
2006-07-13 21:02:53 +00:00
|
|
|
gotOp = true;
|
|
|
|
}
|
2010-10-07 16:56:28 +00:00
|
|
|
|
2008-10-05 18:31:58 +00:00
|
|
|
unsigned opMask = ~0U >> (32-N);
|
2006-07-13 21:02:53 +00:00
|
|
|
int opShift = beginVarBit - N + 1;
|
|
|
|
opMask <<= opShift;
|
|
|
|
opShift = beginInstBit - beginVarBit;
|
2010-10-07 16:56:28 +00:00
|
|
|
|
2006-07-13 21:02:53 +00:00
|
|
|
if (opShift > 0) {
|
|
|
|
Case += " Value |= (op & " + utostr(opMask) + "U) << "
|
|
|
|
+ itostr(opShift) + ";\n";
|
|
|
|
} else if (opShift < 0) {
|
|
|
|
Case += " Value |= (op & " + utostr(opMask) + "U) >> "
|
|
|
|
+ itostr(-opShift) + ";\n";
|
|
|
|
} else {
|
|
|
|
Case += " Value |= op & " + utostr(opMask) + "U;\n";
|
2003-07-15 21:00:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-07-13 21:02:53 +00:00
|
|
|
}
|
|
|
|
}
|
2003-07-15 21:00:32 +00:00
|
|
|
|
2006-11-15 23:23:02 +00:00
|
|
|
std::vector<std::string> &InstList = CaseMap[Case];
|
2006-07-13 21:02:53 +00:00
|
|
|
InstList.push_back(InstName);
|
|
|
|
}
|
2005-04-22 00:00:37 +00:00
|
|
|
|
|
|
|
|
2006-07-13 21:02:53 +00:00
|
|
|
// Emit initial function code
|
|
|
|
o << " const unsigned opcode = MI.getOpcode();\n"
|
|
|
|
<< " unsigned Value = InstBits[opcode];\n"
|
2008-09-07 09:00:57 +00:00
|
|
|
<< " unsigned op = 0;\n"
|
2008-09-02 06:51:36 +00:00
|
|
|
<< " op = op; // suppress warning\n"
|
2006-07-13 21:02:53 +00:00
|
|
|
<< " switch (opcode) {\n";
|
2005-04-22 00:00:37 +00:00
|
|
|
|
2006-07-13 21:02:53 +00:00
|
|
|
// Emit each case statement
|
|
|
|
std::map<std::string, std::vector<std::string> >::iterator IE, EE;
|
|
|
|
for (IE = CaseMap.begin(), EE = CaseMap.end(); IE != EE; ++IE) {
|
|
|
|
const std::string &Case = IE->first;
|
|
|
|
std::vector<std::string> &InstList = IE->second;
|
2005-04-22 00:00:37 +00:00
|
|
|
|
2006-07-13 21:02:53 +00:00
|
|
|
for (int i = 0, N = InstList.size(); i < N; i++) {
|
|
|
|
if (i) o << "\n";
|
|
|
|
o << " case " << Namespace << InstList[i] << ":";
|
2003-05-24 00:15:53 +00:00
|
|
|
}
|
2006-07-13 21:02:53 +00:00
|
|
|
o << " {\n";
|
|
|
|
o << Case;
|
2003-05-24 00:15:53 +00:00
|
|
|
o << " break;\n"
|
|
|
|
<< " }\n";
|
|
|
|
}
|
2003-07-15 21:00:32 +00:00
|
|
|
|
2004-10-14 05:53:01 +00:00
|
|
|
// Default case: unhandled opcode
|
2003-05-27 22:19:58 +00:00
|
|
|
o << " default:\n"
|
2009-07-08 19:04:27 +00:00
|
|
|
<< " std::string msg;\n"
|
|
|
|
<< " raw_string_ostream Msg(msg);\n"
|
|
|
|
<< " Msg << \"Not supported instr: \" << MI;\n"
|
2010-04-07 22:58:41 +00:00
|
|
|
<< " report_fatal_error(Msg.str());\n"
|
2003-05-27 22:19:58 +00:00
|
|
|
<< " }\n"
|
2003-05-24 00:15:53 +00:00
|
|
|
<< " return Value;\n"
|
2004-10-14 05:53:01 +00:00
|
|
|
<< "}\n\n";
|
2003-05-24 00:15:53 +00:00
|
|
|
}
|