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;
|
2007-01-26 17:29:20 +00:00
|
|
|
if (R->getName() == "PHI" ||
|
|
|
|
R->getName() == "INLINEASM" ||
|
2007-07-26 07:48:21 +00:00
|
|
|
R->getName() == "LABEL" ||
|
2008-02-02 04:07:54 +00:00
|
|
|
R->getName() == "DECLARE" ||
|
2007-07-26 07:48:21 +00:00
|
|
|
R->getName() == "EXTRACT_SUBREG" ||
|
|
|
|
R->getName() == "INSERT_SUBREG") continue;
|
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
|
|
|
}
|
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.
|
|
|
|
int CodeEmitterGen::getVariableBit(const std::string &VarName,
|
|
|
|
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();
|
|
|
|
|
|
|
|
if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
|
|
|
|
if (VI->getName() == VarName) return VBI->getBitNum();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-08-01 04:38:18 +00:00
|
|
|
void CodeEmitterGen::run(std::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");
|
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") + "::";
|
2006-07-12 19:15:43 +00:00
|
|
|
|
|
|
|
std::vector<const CodeGenInstruction*> NumberedInstructions;
|
|
|
|
Target.getInstructionsByEnumValue(NumberedInstructions);
|
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::"
|
2006-07-12 19:15:43 +00:00
|
|
|
<< "getBinaryCodeForInstr(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";
|
|
|
|
for (std::vector<const CodeGenInstruction*>::iterator
|
|
|
|
IN = NumberedInstructions.begin(),
|
|
|
|
EN = NumberedInstructions.end();
|
|
|
|
IN != EN; ++IN) {
|
|
|
|
const CodeGenInstruction *CGI = *IN;
|
|
|
|
Record *R = CGI->TheDef;
|
|
|
|
|
|
|
|
if (IN != NumberedInstructions.begin()) o << ",\n";
|
|
|
|
|
2007-01-26 17:29:20 +00:00
|
|
|
if (R->getName() == "PHI" ||
|
|
|
|
R->getName() == "INLINEASM" ||
|
2007-07-26 07:48:21 +00:00
|
|
|
R->getName() == "LABEL" ||
|
2008-02-02 04:07:54 +00:00
|
|
|
R->getName() == "DECLARE" ||
|
2007-07-26 07:48:21 +00:00
|
|
|
R->getName() == "EXTRACT_SUBREG" ||
|
|
|
|
R->getName() == "INSERT_SUBREG") {
|
2006-07-12 19:15:43 +00:00
|
|
|
o << " 0U";
|
|
|
|
continue;
|
|
|
|
}
|
2006-01-27 01:39:38 +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);
|
|
|
|
}
|
|
|
|
}
|
2006-07-12 19:15:43 +00:00
|
|
|
o << " " << Value << "U";
|
|
|
|
}
|
|
|
|
o << "\n };\n";
|
2006-07-13 21:02:53 +00:00
|
|
|
|
|
|
|
// Map to accumulate all the cases.
|
|
|
|
std::map<std::string, std::vector<std::string> > CaseMap;
|
|
|
|
|
|
|
|
// Construct all cases statement for each opcode
|
|
|
|
for (std::vector<Record*>::iterator IC = Insts.begin(), EC = Insts.end();
|
|
|
|
IC != EC; ++IC) {
|
|
|
|
Record *R = *IC;
|
|
|
|
const std::string &InstName = R->getName();
|
|
|
|
std::string Case("");
|
|
|
|
|
2007-01-26 17:29:20 +00:00
|
|
|
if (InstName == "PHI" ||
|
|
|
|
InstName == "INLINEASM" ||
|
2007-07-26 07:48:21 +00:00
|
|
|
InstName == "LABEL"||
|
2008-02-02 04:07:54 +00:00
|
|
|
InstName == "DECLARE"||
|
2007-07-26 07:48:21 +00:00
|
|
|
InstName == "EXTRACT_SUBREG" ||
|
|
|
|
InstName == "INSERT_SUBREG") continue;
|
2006-07-12 19:15:43 +00:00
|
|
|
|
|
|
|
BitsInit *BI = R->getValueAsBitsInit("Inst");
|
|
|
|
const std::vector<RecordVal> &Vals = R->getValues();
|
2006-11-15 23:23:02 +00:00
|
|
|
CodeGenInstruction &CGI = Target.getInstruction(InstName);
|
|
|
|
|
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.
|
2006-07-13 21:02:53 +00:00
|
|
|
const std::string &VarName = Vals[i].getName();
|
|
|
|
bool gotOp = false;
|
|
|
|
|
|
|
|
for (int bit = BI->getNumBits()-1; bit >= 0; ) {
|
|
|
|
int varBit = getVariableBit(VarName, BI, bit);
|
|
|
|
|
|
|
|
if (varBit == -1) {
|
|
|
|
--bit;
|
|
|
|
} else {
|
|
|
|
int beginInstBit = bit;
|
|
|
|
int beginVarBit = varBit;
|
|
|
|
int N = 1;
|
|
|
|
|
|
|
|
for (--bit; bit >= 0;) {
|
|
|
|
varBit = getVariableBit(VarName, BI, bit);
|
|
|
|
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;
|
|
|
|
|
2006-07-13 21:02:53 +00:00
|
|
|
Case += " // op: " + VarName + "\n"
|
|
|
|
+ " 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned opMask = (1 << N) - 1;
|
|
|
|
int opShift = beginVarBit - N + 1;
|
|
|
|
opMask <<= opShift;
|
|
|
|
opShift = beginInstBit - beginVarBit;
|
|
|
|
|
|
|
|
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"
|
|
|
|
<< " unsigned op;\n"
|
|
|
|
<< " 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"
|
2006-12-07 22:21:48 +00:00
|
|
|
<< " cerr << \"Not supported instr: \" << MI << \"\\n\";\n"
|
2003-05-27 22:19:58 +00:00
|
|
|
<< " abort();\n"
|
|
|
|
<< " }\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
|
|
|
}
|