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" ||
|
2008-07-01 00:05:16 +00:00
|
|
|
R->getName() == "DBG_LABEL" ||
|
|
|
|
R->getName() == "EH_LABEL" ||
|
|
|
|
R->getName() == "GC_LABEL" ||
|
2009-09-28 20:32:26 +00:00
|
|
|
R->getName() == "KILL" ||
|
2007-07-26 07:48:21 +00:00
|
|
|
R->getName() == "EXTRACT_SUBREG" ||
|
2008-03-15 00:03:38 +00:00
|
|
|
R->getName() == "INSERT_SUBREG" ||
|
2008-03-16 03:12:01 +00:00
|
|
|
R->getName() == "IMPLICIT_DEF" ||
|
2009-04-13 15:38:05 +00:00
|
|
|
R->getName() == "SUBREG_TO_REG" ||
|
2010-01-08 23:51:25 +00:00
|
|
|
R->getName() == "COPY_TO_REGCLASS" ||
|
Add a pseudo instruction REG_SEQUENCE that takes a list of registers and
sub-register indices and outputs a single super register which is formed from
a consecutive sequence of registers.
This is used as register allocation / coalescing aid and it is useful to
represent instructions that output register pairs / quads. For example,
v1024, v1025 = vload <address>
where v1024 and v1025 forms a register pair.
This really should be modelled as
v1024<3>, v1025<4> = vload <address>
but it would violate SSA property before register allocation is done.
Currently we use insert_subreg to form the super register:
v1026 = implicit_def
v1027 - insert_subreg v1026, v1024, 3
v1028 = insert_subreg v1027, v1025, 4
...
= use v1024
= use v1028
But this adds pseudo live interval overlap between v1024 and v1025.
We can now modeled it as
v1024, v1025 = vload <address>
v1026 = REG_SEQUENCE v1024, 3, v1025, 4
...
= use v1024
= use v1026
After coalescing, it will be
v1026<3>, v1025<4> = vload <address>
...
= use v1026<3>
= use v1026
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@102815 91177308-0d34-0410-b5e6-96231b3b80d8
2010-05-01 00:28:44 +00:00
|
|
|
R->getName() == "DBG_VALUE" ||
|
|
|
|
R->getName() == "REG_SEQUENCE") 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
|
|
|
}
|
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();
|
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
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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");
|
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
|
|
|
|
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;
|
|
|
|
|
2007-01-26 17:29:20 +00:00
|
|
|
if (R->getName() == "PHI" ||
|
|
|
|
R->getName() == "INLINEASM" ||
|
2008-07-01 00:05:16 +00:00
|
|
|
R->getName() == "DBG_LABEL" ||
|
|
|
|
R->getName() == "EH_LABEL" ||
|
|
|
|
R->getName() == "GC_LABEL" ||
|
2009-09-28 20:32:26 +00:00
|
|
|
R->getName() == "KILL" ||
|
2007-07-26 07:48:21 +00:00
|
|
|
R->getName() == "EXTRACT_SUBREG" ||
|
2008-03-15 00:03:38 +00:00
|
|
|
R->getName() == "INSERT_SUBREG" ||
|
2008-03-16 03:12:01 +00:00
|
|
|
R->getName() == "IMPLICIT_DEF" ||
|
2009-04-13 15:38:05 +00:00
|
|
|
R->getName() == "SUBREG_TO_REG" ||
|
2010-01-08 23:51:25 +00:00
|
|
|
R->getName() == "COPY_TO_REGCLASS" ||
|
Add a pseudo instruction REG_SEQUENCE that takes a list of registers and
sub-register indices and outputs a single super register which is formed from
a consecutive sequence of registers.
This is used as register allocation / coalescing aid and it is useful to
represent instructions that output register pairs / quads. For example,
v1024, v1025 = vload <address>
where v1024 and v1025 forms a register pair.
This really should be modelled as
v1024<3>, v1025<4> = vload <address>
but it would violate SSA property before register allocation is done.
Currently we use insert_subreg to form the super register:
v1026 = implicit_def
v1027 - insert_subreg v1026, v1024, 3
v1028 = insert_subreg v1027, v1025, 4
...
= use v1024
= use v1028
But this adds pseudo live interval overlap between v1024 and v1025.
We can now modeled it as
v1024, v1025 = vload <address>
v1026 = REG_SEQUENCE v1024, 3, v1025, 4
...
= use v1024
= use v1026
After coalescing, it will be
v1026<3>, v1025<4> = vload <address>
...
= use v1026<3>
= use v1026
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@102815 91177308-0d34-0410-b5e6-96231b3b80d8
2010-05-01 00:28:44 +00:00
|
|
|
R->getName() == "DBG_VALUE" ||
|
|
|
|
R->getName() == "REG_SEQUENCE") {
|
2008-09-17 06:29:52 +00:00
|
|
|
o << " 0U,\n";
|
2006-07-12 19:15:43 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
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";
|
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" ||
|
2008-07-01 00:05:16 +00:00
|
|
|
InstName == "DBG_LABEL"||
|
|
|
|
InstName == "EH_LABEL"||
|
|
|
|
InstName == "GC_LABEL"||
|
2009-09-28 20:32:26 +00:00
|
|
|
InstName == "KILL"||
|
2007-07-26 07:48:21 +00:00
|
|
|
InstName == "EXTRACT_SUBREG" ||
|
2008-03-15 00:03:38 +00:00
|
|
|
InstName == "INSERT_SUBREG" ||
|
2008-03-16 03:12:01 +00:00
|
|
|
InstName == "IMPLICIT_DEF" ||
|
2009-04-13 15:38:05 +00:00
|
|
|
InstName == "SUBREG_TO_REG" ||
|
2010-01-08 23:51:25 +00:00
|
|
|
InstName == "COPY_TO_REGCLASS" ||
|
Add a pseudo instruction REG_SEQUENCE that takes a list of registers and
sub-register indices and outputs a single super register which is formed from
a consecutive sequence of registers.
This is used as register allocation / coalescing aid and it is useful to
represent instructions that output register pairs / quads. For example,
v1024, v1025 = vload <address>
where v1024 and v1025 forms a register pair.
This really should be modelled as
v1024<3>, v1025<4> = vload <address>
but it would violate SSA property before register allocation is done.
Currently we use insert_subreg to form the super register:
v1026 = implicit_def
v1027 - insert_subreg v1026, v1024, 3
v1028 = insert_subreg v1027, v1025, 4
...
= use v1024
= use v1028
But this adds pseudo live interval overlap between v1024 and v1025.
We can now modeled it as
v1024, v1025 = vload <address>
v1026 = REG_SEQUENCE v1024, 3, v1025, 4
...
= use v1024
= use v1026
After coalescing, it will be
v1026<3>, v1025<4> = vload <address>
...
= use v1026<3>
= use v1026
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@102815 91177308-0d34-0410-b5e6-96231b3b80d8
2010-05-01 00:28:44 +00:00
|
|
|
InstName == "DBG_VALUE" ||
|
|
|
|
InstName == "REG_SEQUENCE") continue;
|
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);
|
2006-11-15 23:23:02 +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;
|
|
|
|
|
|
|
|
for (int bit = BI->getNumBits()-1; bit >= 0; ) {
|
2009-12-15 20:21:44 +00:00
|
|
|
int varBit = getVariableBit(VarName, BI, bit);
|
2006-07-13 21:02:53 +00:00
|
|
|
|
|
|
|
if (varBit == -1) {
|
|
|
|
--bit;
|
|
|
|
} else {
|
|
|
|
int beginInstBit = bit;
|
|
|
|
int beginVarBit = varBit;
|
|
|
|
int N = 1;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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
|
|
|
}
|