2003-10-06 03:27:59 +08:00
|
|
|
//===- CodeEmitterGen.cpp - Code Emitter Generator ------------------------===//
|
2005-04-22 08:00:37 +08:00
|
|
|
//
|
2003-10-21 04:20:30 +08: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 08:00:37 +08:00
|
|
|
//
|
2003-10-21 04:20:30 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2003-10-06 03:27:59 +08:00
|
|
|
//
|
2004-08-05 06:07:54 +08: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-10-06 03:27:59 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CodeEmitterGen.h"
|
2004-08-10 03:10:43 +08:00
|
|
|
#include "CodeGenTarget.h"
|
2003-10-06 03:27:59 +08:00
|
|
|
#include "Record.h"
|
2006-07-14 05:02:53 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2004-09-02 06:55:40 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2004-08-01 11:55:39 +08:00
|
|
|
using namespace llvm;
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2006-07-14 05:02:53 +08: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;
|
|
|
|
if (R->getName() == "PHI" || R->getName() == "INLINEASM") continue;
|
|
|
|
|
|
|
|
BitsInit *BI = R->getValueAsBitsInit("Inst");
|
2004-10-14 13:53:01 +08:00
|
|
|
|
2006-07-14 05:02:53 +08: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 13:53:01 +08:00
|
|
|
}
|
2006-07-14 05:02:53 +08:00
|
|
|
|
|
|
|
// Update the bits in reversed order so that emitInstrOpBits will get the
|
|
|
|
// correct endianness.
|
|
|
|
R->getValue("Inst")->setValue(NewBI);
|
2004-10-14 13:53:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-07-14 06:17:08 +08: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-14 05:02:53 +08: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-10-06 03:27:59 +08:00
|
|
|
void CodeEmitterGen::run(std::ostream &o) {
|
2004-08-11 02:31:01 +08:00
|
|
|
CodeGenTarget Target;
|
2003-10-06 03:27:59 +08:00
|
|
|
std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
|
2006-07-14 05:02:53 +08:00
|
|
|
|
|
|
|
// For little-endian instruction bit encodings, reverse the bit order
|
|
|
|
if (Target.isLittleEndianEncoding()) reverseBits(Insts);
|
2003-10-06 03:27:59 +08:00
|
|
|
|
|
|
|
EmitSourceFileHeader("Machine Code Emitter", o);
|
2004-08-11 02:31:01 +08:00
|
|
|
std::string Namespace = Insts[0]->getValueAsString("Namespace") + "::";
|
2006-07-13 03:15:43 +08:00
|
|
|
|
|
|
|
std::vector<const CodeGenInstruction*> NumberedInstructions;
|
|
|
|
Target.getInstructionsByEnumValue(NumberedInstructions);
|
2003-10-06 03:27:59 +08:00
|
|
|
|
2004-08-11 04:54:58 +08:00
|
|
|
// Emit function declaration
|
2004-08-11 02:31:01 +08:00
|
|
|
o << "unsigned " << Target.getName() << "CodeEmitter::"
|
2006-07-13 03:15:43 +08:00
|
|
|
<< "getBinaryCodeForInstr(MachineInstr &MI) {\n";
|
2004-08-11 04:54:58 +08:00
|
|
|
|
2006-07-13 03:15:43 +08: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";
|
|
|
|
|
|
|
|
if (R->getName() == "PHI" || R->getName() == "INLINEASM") {
|
|
|
|
o << " 0U";
|
|
|
|
continue;
|
|
|
|
}
|
2006-01-27 09:39:38 +08:00
|
|
|
|
2003-10-06 03:27:59 +08:00
|
|
|
BitsInit *BI = R->getValueAsBitsInit("Inst");
|
|
|
|
|
|
|
|
// Start by filling in fixed values...
|
2006-11-03 09:48:30 +08:00
|
|
|
unsigned Value = 0;
|
2003-10-06 03:27:59 +08: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-13 03:15:43 +08:00
|
|
|
o << " " << Value << "U";
|
|
|
|
}
|
|
|
|
o << "\n };\n";
|
2006-07-14 05:02:53 +08: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("");
|
|
|
|
|
|
|
|
if (InstName == "PHI" || InstName == "INLINEASM") continue;
|
2006-07-13 03:15:43 +08:00
|
|
|
|
|
|
|
BitsInit *BI = R->getValueAsBitsInit("Inst");
|
|
|
|
const std::vector<RecordVal> &Vals = R->getValues();
|
2006-11-16 07:23:02 +08:00
|
|
|
CodeGenInstruction &CGI = Target.getInstruction(InstName);
|
|
|
|
|
2004-10-14 13:53:01 +08:00
|
|
|
// Loop over all of the fields in the instruction, determining which are the
|
2005-04-22 08:00:37 +08:00
|
|
|
// operands to the instruction.
|
2003-10-06 03:27:59 +08:00
|
|
|
unsigned op = 0;
|
|
|
|
for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
|
2004-10-14 13:53:01 +08:00
|
|
|
if (!Vals[i].getPrefix() && !Vals[i].getValue()->isComplete()) {
|
2003-10-06 03:27:59 +08:00
|
|
|
// Is the operand continuous? If so, we can just mask and OR it in
|
2005-04-22 08:00:37 +08:00
|
|
|
// instead of doing it bit-by-bit, saving a lot in runtime cost.
|
2006-07-14 05:02:53 +08: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-10-06 03:27:59 +08:00
|
|
|
|
2006-07-14 05:02:53 +08:00
|
|
|
if (!gotOp) {
|
2006-11-16 07:23:02 +08:00
|
|
|
/// If this operand is not supposed to be emitted by the generated
|
|
|
|
/// emitter, skip it.
|
|
|
|
while (CGI.isFlatOperandNotEmitted(op))
|
|
|
|
++op;
|
|
|
|
|
2006-07-14 05:02:53 +08:00
|
|
|
Case += " // op: " + VarName + "\n"
|
|
|
|
+ " op = getMachineOpValue(MI, MI.getOperand("
|
2006-11-16 07:23:02 +08:00
|
|
|
+ utostr(op++) + "));\n";
|
2006-07-14 05:02:53 +08: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-10-06 03:27:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-07-14 05:02:53 +08:00
|
|
|
}
|
|
|
|
}
|
2003-10-06 03:27:59 +08:00
|
|
|
|
2006-11-16 07:23:02 +08:00
|
|
|
std::vector<std::string> &InstList = CaseMap[Case];
|
2006-07-14 05:02:53 +08:00
|
|
|
InstList.push_back(InstName);
|
|
|
|
}
|
2005-04-22 08:00:37 +08:00
|
|
|
|
|
|
|
|
2006-07-14 05:02:53 +08: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 08:00:37 +08:00
|
|
|
|
2006-07-14 05:02:53 +08: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 08:00:37 +08:00
|
|
|
|
2006-07-14 05:02:53 +08:00
|
|
|
for (int i = 0, N = InstList.size(); i < N; i++) {
|
|
|
|
if (i) o << "\n";
|
|
|
|
o << " case " << Namespace << InstList[i] << ":";
|
2003-10-06 03:27:59 +08:00
|
|
|
}
|
2006-07-14 05:02:53 +08:00
|
|
|
o << " {\n";
|
|
|
|
o << Case;
|
2003-10-06 03:27:59 +08:00
|
|
|
o << " break;\n"
|
|
|
|
<< " }\n";
|
|
|
|
}
|
|
|
|
|
2004-10-14 13:53:01 +08:00
|
|
|
// Default case: unhandled opcode
|
2003-10-06 03:27:59 +08:00
|
|
|
o << " default:\n"
|
2006-12-08 06:21:48 +08:00
|
|
|
<< " cerr << \"Not supported instr: \" << MI << \"\\n\";\n"
|
2003-10-06 03:27:59 +08:00
|
|
|
<< " abort();\n"
|
|
|
|
<< " }\n"
|
|
|
|
<< " return Value;\n"
|
2004-10-14 13:53:01 +08:00
|
|
|
<< "}\n\n";
|
2003-10-06 03:27:59 +08:00
|
|
|
}
|