2011-02-18 21:51:29 +00:00
|
|
|
//===------------ FixedLenDecoderEmitter.cpp - Decoder Generator ----------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// It contains the tablegen backend that emits the decoder functions for
|
|
|
|
// targets with fixed length instruction set.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CodeGenTarget.h"
|
2012-02-09 10:56:31 +00:00
|
|
|
#include "llvm/ADT/APInt.h"
|
2012-08-14 19:06:05 +00:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2011-02-18 21:51:29 +00:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2012-08-14 19:06:05 +00:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
|
|
|
#include "llvm/MC/MCFixedLenDisassembler.h"
|
2012-06-11 15:37:55 +00:00
|
|
|
#include "llvm/Support/DataTypes.h"
|
2011-02-18 21:51:29 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2012-08-14 19:06:05 +00:00
|
|
|
#include "llvm/Support/FormattedStream.h"
|
|
|
|
#include "llvm/Support/LEB128.h"
|
2011-02-18 21:51:29 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2012-12-04 10:37:14 +00:00
|
|
|
#include "llvm/TableGen/Error.h"
|
|
|
|
#include "llvm/TableGen/Record.h"
|
2011-02-18 21:51:29 +00:00
|
|
|
#include <map>
|
|
|
|
#include <string>
|
2012-12-04 10:37:14 +00:00
|
|
|
#include <vector>
|
2011-02-18 21:51:29 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 03:06:00 +00:00
|
|
|
#define DEBUG_TYPE "decoder-emitter"
|
|
|
|
|
2012-06-11 15:37:55 +00:00
|
|
|
namespace {
|
|
|
|
struct EncodingField {
|
|
|
|
unsigned Base, Width, Offset;
|
|
|
|
EncodingField(unsigned B, unsigned W, unsigned O)
|
|
|
|
: Base(B), Width(W), Offset(O) { }
|
|
|
|
};
|
|
|
|
|
|
|
|
struct OperandInfo {
|
|
|
|
std::vector<EncodingField> Fields;
|
|
|
|
std::string Decoder;
|
|
|
|
|
|
|
|
OperandInfo(std::string D)
|
|
|
|
: Decoder(D) { }
|
|
|
|
|
|
|
|
void addField(unsigned Base, unsigned Width, unsigned Offset) {
|
|
|
|
Fields.push_back(EncodingField(Base, Width, Offset));
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned numFields() const { return Fields.size(); }
|
|
|
|
|
|
|
|
typedef std::vector<EncodingField>::const_iterator const_iterator;
|
|
|
|
|
|
|
|
const_iterator begin() const { return Fields.begin(); }
|
|
|
|
const_iterator end() const { return Fields.end(); }
|
|
|
|
};
|
2012-08-14 19:06:05 +00:00
|
|
|
|
|
|
|
typedef std::vector<uint8_t> DecoderTable;
|
|
|
|
typedef uint32_t DecoderFixup;
|
|
|
|
typedef std::vector<DecoderFixup> FixupList;
|
|
|
|
typedef std::vector<FixupList> FixupScopeList;
|
|
|
|
typedef SetVector<std::string> PredicateSet;
|
|
|
|
typedef SetVector<std::string> DecoderSet;
|
|
|
|
struct DecoderTableInfo {
|
|
|
|
DecoderTable Table;
|
|
|
|
FixupScopeList FixupStack;
|
|
|
|
PredicateSet Predicates;
|
|
|
|
DecoderSet Decoders;
|
|
|
|
};
|
|
|
|
|
2012-06-11 15:37:55 +00:00
|
|
|
} // End anonymous namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class FixedLenDecoderEmitter {
|
2012-08-14 19:06:05 +00:00
|
|
|
const std::vector<const CodeGenInstruction*> *NumberedInstructions;
|
2012-06-11 15:37:55 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
// Defaults preserved here for documentation, even though they aren't
|
|
|
|
// strictly necessary given the way that this is currently being called.
|
|
|
|
FixedLenDecoderEmitter(RecordKeeper &R,
|
|
|
|
std::string PredicateNamespace,
|
|
|
|
std::string GPrefix = "if (",
|
|
|
|
std::string GPostfix = " == MCDisassembler::Fail)"
|
|
|
|
" return MCDisassembler::Fail;",
|
|
|
|
std::string ROK = "MCDisassembler::Success",
|
|
|
|
std::string RFail = "MCDisassembler::Fail",
|
|
|
|
std::string L = "") :
|
|
|
|
Target(R),
|
|
|
|
PredicateNamespace(PredicateNamespace),
|
|
|
|
GuardPrefix(GPrefix), GuardPostfix(GPostfix),
|
|
|
|
ReturnOK(ROK), ReturnFail(RFail), Locals(L) {}
|
|
|
|
|
2012-08-14 19:06:05 +00:00
|
|
|
// Emit the decoder state machine table.
|
|
|
|
void emitTable(formatted_raw_ostream &o, DecoderTable &Table,
|
|
|
|
unsigned Indentation, unsigned BitWidth,
|
|
|
|
StringRef Namespace) const;
|
|
|
|
void emitPredicateFunction(formatted_raw_ostream &OS,
|
|
|
|
PredicateSet &Predicates,
|
|
|
|
unsigned Indentation) const;
|
|
|
|
void emitDecoderFunction(formatted_raw_ostream &OS,
|
|
|
|
DecoderSet &Decoders,
|
|
|
|
unsigned Indentation) const;
|
|
|
|
|
2012-06-11 15:37:55 +00:00
|
|
|
// run - Output the code emitter
|
|
|
|
void run(raw_ostream &o);
|
|
|
|
|
|
|
|
private:
|
|
|
|
CodeGenTarget Target;
|
|
|
|
public:
|
|
|
|
std::string PredicateNamespace;
|
|
|
|
std::string GuardPrefix, GuardPostfix;
|
|
|
|
std::string ReturnOK, ReturnFail;
|
|
|
|
std::string Locals;
|
|
|
|
};
|
|
|
|
} // End anonymous namespace
|
|
|
|
|
2011-02-18 21:51:29 +00:00
|
|
|
// The set (BIT_TRUE, BIT_FALSE, BIT_UNSET) represents a ternary logic system
|
|
|
|
// for a bit value.
|
|
|
|
//
|
|
|
|
// BIT_UNFILTERED is used as the init value for a filter position. It is used
|
|
|
|
// only for filter processings.
|
|
|
|
typedef enum {
|
|
|
|
BIT_TRUE, // '1'
|
|
|
|
BIT_FALSE, // '0'
|
|
|
|
BIT_UNSET, // '?'
|
|
|
|
BIT_UNFILTERED // unfiltered
|
|
|
|
} bit_value_t;
|
|
|
|
|
|
|
|
static bool ValueSet(bit_value_t V) {
|
|
|
|
return (V == BIT_TRUE || V == BIT_FALSE);
|
|
|
|
}
|
|
|
|
static bool ValueNotSet(bit_value_t V) {
|
|
|
|
return (V == BIT_UNSET);
|
|
|
|
}
|
|
|
|
static int Value(bit_value_t V) {
|
|
|
|
return ValueNotSet(V) ? -1 : (V == BIT_FALSE ? 0 : 1);
|
|
|
|
}
|
2012-03-16 05:58:09 +00:00
|
|
|
static bit_value_t bitFromBits(const BitsInit &bits, unsigned index) {
|
2012-10-10 20:24:43 +00:00
|
|
|
if (BitInit *bit = dyn_cast<BitInit>(bits.getBit(index)))
|
2011-02-18 21:51:29 +00:00
|
|
|
return bit->getValue() ? BIT_TRUE : BIT_FALSE;
|
|
|
|
|
|
|
|
// The bit is uninitialized.
|
|
|
|
return BIT_UNSET;
|
|
|
|
}
|
|
|
|
// Prints the bit value for each position.
|
2012-03-16 05:58:09 +00:00
|
|
|
static void dumpBits(raw_ostream &o, const BitsInit &bits) {
|
2012-08-17 05:42:16 +00:00
|
|
|
for (unsigned index = bits.getNumBits(); index > 0; --index) {
|
2011-02-18 21:51:29 +00:00
|
|
|
switch (bitFromBits(bits, index - 1)) {
|
|
|
|
case BIT_TRUE:
|
|
|
|
o << "1";
|
|
|
|
break;
|
|
|
|
case BIT_FALSE:
|
|
|
|
o << "0";
|
|
|
|
break;
|
|
|
|
case BIT_UNSET:
|
|
|
|
o << "_";
|
|
|
|
break;
|
|
|
|
default:
|
2012-02-05 07:21:30 +00:00
|
|
|
llvm_unreachable("unexpected return value from bitFromBits");
|
2011-02-18 21:51:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-29 22:43:06 +00:00
|
|
|
static BitsInit &getBitsField(const Record &def, const char *str) {
|
|
|
|
BitsInit *bits = def.getValueAsBitsInit(str);
|
2011-02-18 21:51:29 +00:00
|
|
|
return *bits;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Forward declaration.
|
2012-06-11 15:37:55 +00:00
|
|
|
namespace {
|
2011-02-18 21:51:29 +00:00
|
|
|
class FilterChooser;
|
2012-06-11 15:37:55 +00:00
|
|
|
} // End anonymous namespace
|
2011-02-18 21:51:29 +00:00
|
|
|
|
|
|
|
// Representation of the instruction to work on.
|
2011-07-19 21:06:00 +00:00
|
|
|
typedef std::vector<bit_value_t> insn_t;
|
2011-02-18 21:51:29 +00:00
|
|
|
|
|
|
|
/// Filter - Filter works with FilterChooser to produce the decoding tree for
|
|
|
|
/// the ISA.
|
|
|
|
///
|
|
|
|
/// It is useful to think of a Filter as governing the switch stmts of the
|
|
|
|
/// decoding tree in a certain level. Each case stmt delegates to an inferior
|
|
|
|
/// FilterChooser to decide what further decoding logic to employ, or in another
|
|
|
|
/// words, what other remaining bits to look at. The FilterChooser eventually
|
|
|
|
/// chooses a best Filter to do its job.
|
|
|
|
///
|
|
|
|
/// This recursive scheme ends when the number of Opcodes assigned to the
|
|
|
|
/// FilterChooser becomes 1 or if there is a conflict. A conflict happens when
|
|
|
|
/// the Filter/FilterChooser combo does not know how to distinguish among the
|
|
|
|
/// Opcodes assigned.
|
|
|
|
///
|
|
|
|
/// An example of a conflict is
|
|
|
|
///
|
|
|
|
/// Conflict:
|
|
|
|
/// 111101000.00........00010000....
|
|
|
|
/// 111101000.00........0001........
|
|
|
|
/// 1111010...00........0001........
|
|
|
|
/// 1111010...00....................
|
|
|
|
/// 1111010.........................
|
|
|
|
/// 1111............................
|
|
|
|
/// ................................
|
|
|
|
/// VST4q8a 111101000_00________00010000____
|
|
|
|
/// VST4q8b 111101000_00________00010000____
|
|
|
|
///
|
|
|
|
/// The Debug output shows the path that the decoding tree follows to reach the
|
|
|
|
/// the conclusion that there is a conflict. VST4q8a is a vst4 to double-spaced
|
|
|
|
/// even registers, while VST4q8b is a vst4 to double-spaced odd regsisters.
|
|
|
|
///
|
|
|
|
/// The encoding info in the .td files does not specify this meta information,
|
|
|
|
/// which could have been used by the decoder to resolve the conflict. The
|
|
|
|
/// decoder could try to decode the even/odd register numbering and assign to
|
|
|
|
/// VST4q8a or VST4q8b, but for the time being, the decoder chooses the "a"
|
|
|
|
/// version and return the Opcode since the two have the same Asm format string.
|
2012-06-11 15:37:55 +00:00
|
|
|
namespace {
|
2011-02-18 21:51:29 +00:00
|
|
|
class Filter {
|
|
|
|
protected:
|
2012-03-16 06:52:56 +00:00
|
|
|
const FilterChooser *Owner;// points to the FilterChooser who owns this filter
|
2011-02-18 21:51:29 +00:00
|
|
|
unsigned StartBit; // the starting bit position
|
|
|
|
unsigned NumBits; // number of bits to filter
|
|
|
|
bool Mixed; // a mixed region contains both set and unset bits
|
|
|
|
|
|
|
|
// Map of well-known segment value to the set of uid's with that value.
|
|
|
|
std::map<uint64_t, std::vector<unsigned> > FilteredInstructions;
|
|
|
|
|
|
|
|
// Set of uid's with non-constant segment values.
|
|
|
|
std::vector<unsigned> VariableInstructions;
|
|
|
|
|
|
|
|
// Map of well-known segment value to its delegate.
|
2014-09-03 06:07:54 +00:00
|
|
|
std::map<unsigned, std::unique_ptr<const FilterChooser>> FilterChooserMap;
|
2011-02-18 21:51:29 +00:00
|
|
|
|
|
|
|
// Number of instructions which fall under FilteredInstructions category.
|
|
|
|
unsigned NumFiltered;
|
|
|
|
|
|
|
|
// Keeps track of the last opcode in the filtered bucket.
|
|
|
|
unsigned LastOpcFiltered;
|
|
|
|
|
|
|
|
public:
|
2012-03-16 05:58:09 +00:00
|
|
|
unsigned getNumFiltered() const { return NumFiltered; }
|
|
|
|
unsigned getSingletonOpc() const {
|
2011-02-18 21:51:29 +00:00
|
|
|
assert(NumFiltered == 1);
|
|
|
|
return LastOpcFiltered;
|
|
|
|
}
|
|
|
|
// Return the filter chooser for the group of instructions without constant
|
|
|
|
// segment values.
|
2012-03-16 05:58:09 +00:00
|
|
|
const FilterChooser &getVariableFC() const {
|
2011-02-18 21:51:29 +00:00
|
|
|
assert(NumFiltered == 1);
|
|
|
|
assert(FilterChooserMap.size() == 1);
|
|
|
|
return *(FilterChooserMap.find((unsigned)-1)->second);
|
|
|
|
}
|
|
|
|
|
2014-09-03 05:49:07 +00:00
|
|
|
Filter(Filter &&f);
|
2011-02-18 21:51:29 +00:00
|
|
|
Filter(FilterChooser &owner, unsigned startBit, unsigned numBits, bool mixed);
|
|
|
|
|
|
|
|
~Filter();
|
|
|
|
|
|
|
|
// Divides the decoding task into sub tasks and delegates them to the
|
|
|
|
// inferior FilterChooser's.
|
|
|
|
//
|
|
|
|
// A special case arises when there's only one entry in the filtered
|
|
|
|
// instructions. In order to unambiguously decode the singleton, we need to
|
|
|
|
// match the remaining undecoded encoding bits against the singleton.
|
|
|
|
void recurse();
|
|
|
|
|
2012-08-14 19:06:05 +00:00
|
|
|
// Emit table entries to decode instructions given a segment or segments of
|
|
|
|
// bits.
|
|
|
|
void emitTableEntry(DecoderTableInfo &TableInfo) const;
|
2011-02-18 21:51:29 +00:00
|
|
|
|
|
|
|
// Returns the number of fanout produced by the filter. More fanout implies
|
|
|
|
// the filter distinguishes more categories of instructions.
|
|
|
|
unsigned usefulness() const;
|
|
|
|
}; // End of class Filter
|
2012-06-11 15:37:55 +00:00
|
|
|
} // End anonymous namespace
|
2011-02-18 21:51:29 +00:00
|
|
|
|
|
|
|
// These are states of our finite state machines used in FilterChooser's
|
|
|
|
// filterProcessor() which produces the filter candidates to use.
|
|
|
|
typedef enum {
|
|
|
|
ATTR_NONE,
|
|
|
|
ATTR_FILTERED,
|
|
|
|
ATTR_ALL_SET,
|
|
|
|
ATTR_ALL_UNSET,
|
|
|
|
ATTR_MIXED
|
|
|
|
} bitAttr_t;
|
|
|
|
|
|
|
|
/// FilterChooser - FilterChooser chooses the best filter among a set of Filters
|
|
|
|
/// in order to perform the decoding of instructions at the current level.
|
|
|
|
///
|
|
|
|
/// Decoding proceeds from the top down. Based on the well-known encoding bits
|
|
|
|
/// of instructions available, FilterChooser builds up the possible Filters that
|
|
|
|
/// can further the task of decoding by distinguishing among the remaining
|
|
|
|
/// candidate instructions.
|
|
|
|
///
|
|
|
|
/// Once a filter has been chosen, it is called upon to divide the decoding task
|
|
|
|
/// into sub-tasks and delegates them to its inferior FilterChoosers for further
|
|
|
|
/// processings.
|
|
|
|
///
|
|
|
|
/// It is useful to think of a Filter as governing the switch stmts of the
|
|
|
|
/// decoding tree. And each case is delegated to an inferior FilterChooser to
|
|
|
|
/// decide what further remaining bits to look at.
|
2012-06-11 15:37:55 +00:00
|
|
|
namespace {
|
2011-02-18 21:51:29 +00:00
|
|
|
class FilterChooser {
|
|
|
|
protected:
|
|
|
|
friend class Filter;
|
|
|
|
|
|
|
|
// Vector of codegen instructions to choose our filter.
|
|
|
|
const std::vector<const CodeGenInstruction*> &AllInstructions;
|
|
|
|
|
|
|
|
// Vector of uid's for this filter chooser to work on.
|
2012-03-16 06:52:56 +00:00
|
|
|
const std::vector<unsigned> &Opcodes;
|
2011-02-18 21:51:29 +00:00
|
|
|
|
|
|
|
// Lookup table for the operand decoding of instructions.
|
2012-03-16 06:52:56 +00:00
|
|
|
const std::map<unsigned, std::vector<OperandInfo> > &Operands;
|
2011-02-18 21:51:29 +00:00
|
|
|
|
|
|
|
// Vector of candidate filters.
|
|
|
|
std::vector<Filter> Filters;
|
|
|
|
|
|
|
|
// Array of bit values passed down from our parent.
|
|
|
|
// Set to all BIT_UNFILTERED's for Parent == NULL.
|
2011-07-19 21:06:00 +00:00
|
|
|
std::vector<bit_value_t> FilterBitValues;
|
2011-02-18 21:51:29 +00:00
|
|
|
|
|
|
|
// Links to the FilterChooser above us in the decoding tree.
|
2012-03-16 06:52:56 +00:00
|
|
|
const FilterChooser *Parent;
|
2011-02-18 21:51:29 +00:00
|
|
|
|
|
|
|
// Index of the best filter from Filters.
|
|
|
|
int BestIndex;
|
|
|
|
|
2011-07-19 21:06:00 +00:00
|
|
|
// Width of instructions
|
|
|
|
unsigned BitWidth;
|
|
|
|
|
2011-08-17 17:44:15 +00:00
|
|
|
// Parent emitter
|
|
|
|
const FixedLenDecoderEmitter *Emitter;
|
|
|
|
|
2015-02-15 22:54:22 +00:00
|
|
|
FilterChooser(const FilterChooser &) = delete;
|
|
|
|
void operator=(const FilterChooser &) = delete;
|
2011-02-18 21:51:29 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
FilterChooser(const std::vector<const CodeGenInstruction*> &Insts,
|
|
|
|
const std::vector<unsigned> &IDs,
|
2012-03-16 06:52:56 +00:00
|
|
|
const std::map<unsigned, std::vector<OperandInfo> > &Ops,
|
2011-08-17 17:44:15 +00:00
|
|
|
unsigned BW,
|
2012-03-16 01:19:24 +00:00
|
|
|
const FixedLenDecoderEmitter *E)
|
|
|
|
: AllInstructions(Insts), Opcodes(IDs), Operands(Ops), Filters(),
|
2014-09-04 04:49:03 +00:00
|
|
|
FilterBitValues(BW, BIT_UNFILTERED), Parent(nullptr), BestIndex(-1),
|
|
|
|
BitWidth(BW), Emitter(E) {
|
2011-02-18 21:51:29 +00:00
|
|
|
doFilter();
|
|
|
|
}
|
|
|
|
|
|
|
|
FilterChooser(const std::vector<const CodeGenInstruction*> &Insts,
|
|
|
|
const std::vector<unsigned> &IDs,
|
2012-03-16 06:52:56 +00:00
|
|
|
const std::map<unsigned, std::vector<OperandInfo> > &Ops,
|
|
|
|
const std::vector<bit_value_t> &ParentFilterBitValues,
|
|
|
|
const FilterChooser &parent)
|
2012-03-16 01:19:24 +00:00
|
|
|
: AllInstructions(Insts), Opcodes(IDs), Operands(Ops),
|
2011-07-19 21:06:00 +00:00
|
|
|
Filters(), FilterBitValues(ParentFilterBitValues),
|
2011-08-17 17:44:15 +00:00
|
|
|
Parent(&parent), BestIndex(-1), BitWidth(parent.BitWidth),
|
|
|
|
Emitter(parent.Emitter) {
|
2011-02-18 21:51:29 +00:00
|
|
|
doFilter();
|
|
|
|
}
|
|
|
|
|
2012-08-14 19:06:05 +00:00
|
|
|
unsigned getBitWidth() const { return BitWidth; }
|
2011-02-18 21:51:29 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
// Populates the insn given the uid.
|
|
|
|
void insnWithID(insn_t &Insn, unsigned Opcode) const {
|
2011-07-29 22:43:06 +00:00
|
|
|
BitsInit &Bits = getBitsField(*AllInstructions[Opcode]->TheDef, "Inst");
|
2011-02-18 21:51:29 +00:00
|
|
|
|
2012-02-09 10:56:31 +00:00
|
|
|
// We may have a SoftFail bitmask, which specifies a mask where an encoding
|
|
|
|
// may differ from the value in "Inst" and yet still be valid, but the
|
|
|
|
// disassembler should return SoftFail instead of Success.
|
|
|
|
//
|
|
|
|
// This is used for marking UNPREDICTABLE instructions in the ARM world.
|
2012-02-29 22:07:56 +00:00
|
|
|
BitsInit *SFBits =
|
|
|
|
AllInstructions[Opcode]->TheDef->getValueAsBitsInit("SoftFail");
|
2012-02-09 10:56:31 +00:00
|
|
|
|
|
|
|
for (unsigned i = 0; i < BitWidth; ++i) {
|
|
|
|
if (SFBits && bitFromBits(*SFBits, i) == BIT_TRUE)
|
|
|
|
Insn.push_back(BIT_UNSET);
|
|
|
|
else
|
|
|
|
Insn.push_back(bitFromBits(Bits, i));
|
|
|
|
}
|
2011-02-18 21:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the record name.
|
|
|
|
const std::string &nameWithID(unsigned Opcode) const {
|
|
|
|
return AllInstructions[Opcode]->TheDef->getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Populates the field of the insn given the start position and the number of
|
|
|
|
// consecutive bits to scan for.
|
|
|
|
//
|
|
|
|
// Returns false if there exists any uninitialized bit value in the range.
|
|
|
|
// Returns true, otherwise.
|
|
|
|
bool fieldFromInsn(uint64_t &Field, insn_t &Insn, unsigned StartBit,
|
2012-03-16 01:19:24 +00:00
|
|
|
unsigned NumBits) const;
|
2011-02-18 21:51:29 +00:00
|
|
|
|
|
|
|
/// dumpFilterArray - dumpFilterArray prints out debugging info for the given
|
|
|
|
/// filter array as a series of chars.
|
2012-03-16 05:58:09 +00:00
|
|
|
void dumpFilterArray(raw_ostream &o,
|
|
|
|
const std::vector<bit_value_t> & filter) const;
|
2011-02-18 21:51:29 +00:00
|
|
|
|
|
|
|
/// dumpStack - dumpStack traverses the filter chooser chain and calls
|
|
|
|
/// dumpFilterArray on each filter chooser up to the top level one.
|
2012-03-16 05:58:09 +00:00
|
|
|
void dumpStack(raw_ostream &o, const char *prefix) const;
|
2011-02-18 21:51:29 +00:00
|
|
|
|
|
|
|
Filter &bestFilter() {
|
|
|
|
assert(BestIndex != -1 && "BestIndex not set");
|
|
|
|
return Filters[BestIndex];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called from Filter::recurse() when singleton exists. For debug purpose.
|
2012-03-16 05:58:09 +00:00
|
|
|
void SingletonExists(unsigned Opc) const;
|
2011-02-18 21:51:29 +00:00
|
|
|
|
2012-03-16 05:58:09 +00:00
|
|
|
bool PositionFiltered(unsigned i) const {
|
2011-02-18 21:51:29 +00:00
|
|
|
return ValueSet(FilterBitValues[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculates the island(s) needed to decode the instruction.
|
|
|
|
// This returns a lit of undecoded bits of an instructions, for example,
|
|
|
|
// Inst{20} = 1 && Inst{3-0} == 0b1111 represents two islands of yet-to-be
|
|
|
|
// decoded bits in order to verify that the instruction matches the Opcode.
|
|
|
|
unsigned getIslands(std::vector<unsigned> &StartBits,
|
2012-03-16 01:19:24 +00:00
|
|
|
std::vector<unsigned> &EndBits,
|
2012-03-16 05:58:09 +00:00
|
|
|
std::vector<uint64_t> &FieldVals,
|
|
|
|
const insn_t &Insn) const;
|
2011-02-18 21:51:29 +00:00
|
|
|
|
2011-09-07 19:42:28 +00:00
|
|
|
// Emits code to check the Predicates member of an instruction are true.
|
|
|
|
// Returns true if predicate matches were emitted, false otherwise.
|
2012-03-16 05:58:09 +00:00
|
|
|
bool emitPredicateMatch(raw_ostream &o, unsigned &Indentation,
|
|
|
|
unsigned Opc) const;
|
2011-09-07 19:42:28 +00:00
|
|
|
|
2012-08-14 19:06:05 +00:00
|
|
|
bool doesOpcodeNeedPredicate(unsigned Opc) const;
|
|
|
|
unsigned getPredicateIndex(DecoderTableInfo &TableInfo, StringRef P) const;
|
|
|
|
void emitPredicateTableEntry(DecoderTableInfo &TableInfo,
|
|
|
|
unsigned Opc) const;
|
|
|
|
|
|
|
|
void emitSoftFailTableEntry(DecoderTableInfo &TableInfo,
|
|
|
|
unsigned Opc) const;
|
2012-02-09 10:56:31 +00:00
|
|
|
|
2012-08-14 19:06:05 +00:00
|
|
|
// Emits table entries to decode the singleton.
|
|
|
|
void emitSingletonTableEntry(DecoderTableInfo &TableInfo,
|
|
|
|
unsigned Opc) const;
|
2011-02-18 21:51:29 +00:00
|
|
|
|
|
|
|
// Emits code to decode the singleton, and then to decode the rest.
|
2012-08-14 19:06:05 +00:00
|
|
|
void emitSingletonTableEntry(DecoderTableInfo &TableInfo,
|
|
|
|
const Filter &Best) const;
|
2011-02-18 21:51:29 +00:00
|
|
|
|
2012-08-14 19:06:05 +00:00
|
|
|
void emitBinaryParser(raw_ostream &o, unsigned &Indentation,
|
2012-03-16 05:58:09 +00:00
|
|
|
const OperandInfo &OpInfo) const;
|
2011-07-28 21:54:31 +00:00
|
|
|
|
2012-08-14 19:06:05 +00:00
|
|
|
void emitDecoder(raw_ostream &OS, unsigned Indentation, unsigned Opc) const;
|
|
|
|
unsigned getDecoderIndex(DecoderSet &Decoders, unsigned Opc) const;
|
|
|
|
|
2011-02-18 21:51:29 +00:00
|
|
|
// Assign a single filter and run with it.
|
2012-03-16 05:58:09 +00:00
|
|
|
void runSingleFilter(unsigned startBit, unsigned numBit, bool mixed);
|
2011-02-18 21:51:29 +00:00
|
|
|
|
|
|
|
// reportRegion is a helper function for filterProcessor to mark a region as
|
|
|
|
// eligible for use as a filter region.
|
|
|
|
void reportRegion(bitAttr_t RA, unsigned StartBit, unsigned BitIndex,
|
2012-03-16 01:19:24 +00:00
|
|
|
bool AllowMixed);
|
2011-02-18 21:51:29 +00:00
|
|
|
|
|
|
|
// FilterProcessor scans the well-known encoding bits of the instructions and
|
|
|
|
// builds up a list of candidate filters. It chooses the best filter and
|
|
|
|
// recursively descends down the decoding tree.
|
|
|
|
bool filterProcessor(bool AllowMixed, bool Greedy = true);
|
|
|
|
|
|
|
|
// Decides on the best configuration of filter(s) to use in order to decode
|
|
|
|
// the instructions. A conflict of instructions may occur, in which case we
|
|
|
|
// dump the conflict set to the standard error.
|
|
|
|
void doFilter();
|
|
|
|
|
2012-08-14 19:06:05 +00:00
|
|
|
public:
|
|
|
|
// emitTableEntries - Emit state machine entries to decode our share of
|
|
|
|
// instructions.
|
|
|
|
void emitTableEntries(DecoderTableInfo &TableInfo) const;
|
2011-02-18 21:51:29 +00:00
|
|
|
};
|
2012-06-11 15:37:55 +00:00
|
|
|
} // End anonymous namespace
|
2011-02-18 21:51:29 +00:00
|
|
|
|
|
|
|
///////////////////////////
|
|
|
|
// //
|
2012-03-16 00:56:01 +00:00
|
|
|
// Filter Implementation //
|
2011-02-18 21:51:29 +00:00
|
|
|
// //
|
|
|
|
///////////////////////////
|
|
|
|
|
2014-09-03 05:49:07 +00:00
|
|
|
Filter::Filter(Filter &&f)
|
2012-03-16 01:19:24 +00:00
|
|
|
: Owner(f.Owner), StartBit(f.StartBit), NumBits(f.NumBits), Mixed(f.Mixed),
|
2014-09-03 05:49:07 +00:00
|
|
|
FilteredInstructions(std::move(f.FilteredInstructions)),
|
|
|
|
VariableInstructions(std::move(f.VariableInstructions)),
|
|
|
|
FilterChooserMap(std::move(f.FilterChooserMap)), NumFiltered(f.NumFiltered),
|
2012-03-16 01:19:24 +00:00
|
|
|
LastOpcFiltered(f.LastOpcFiltered) {
|
2011-02-18 21:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Filter::Filter(FilterChooser &owner, unsigned startBit, unsigned numBits,
|
2012-03-16 01:19:24 +00:00
|
|
|
bool mixed)
|
|
|
|
: Owner(&owner), StartBit(startBit), NumBits(numBits), Mixed(mixed) {
|
2011-07-19 21:06:00 +00:00
|
|
|
assert(StartBit + NumBits - 1 < Owner->BitWidth);
|
2011-02-18 21:51:29 +00:00
|
|
|
|
|
|
|
NumFiltered = 0;
|
|
|
|
LastOpcFiltered = 0;
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = Owner->Opcodes.size(); i != e; ++i) {
|
|
|
|
insn_t Insn;
|
|
|
|
|
|
|
|
// Populates the insn given the uid.
|
|
|
|
Owner->insnWithID(Insn, Owner->Opcodes[i]);
|
|
|
|
|
|
|
|
uint64_t Field;
|
|
|
|
// Scans the segment for possibly well-specified encoding bits.
|
|
|
|
bool ok = Owner->fieldFromInsn(Field, Insn, StartBit, NumBits);
|
|
|
|
|
|
|
|
if (ok) {
|
|
|
|
// The encoding bits are well-known. Lets add the uid of the
|
|
|
|
// instruction into the bucket keyed off the constant field value.
|
|
|
|
LastOpcFiltered = Owner->Opcodes[i];
|
|
|
|
FilteredInstructions[Field].push_back(LastOpcFiltered);
|
|
|
|
++NumFiltered;
|
|
|
|
} else {
|
2012-03-16 00:56:01 +00:00
|
|
|
// Some of the encoding bit(s) are unspecified. This contributes to
|
2011-02-18 21:51:29 +00:00
|
|
|
// one additional member of "Variable" instructions.
|
|
|
|
VariableInstructions.push_back(Owner->Opcodes[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert((FilteredInstructions.size() + VariableInstructions.size() > 0)
|
|
|
|
&& "Filter returns no instruction categories");
|
|
|
|
}
|
|
|
|
|
|
|
|
Filter::~Filter() {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Divides the decoding task into sub tasks and delegates them to the
|
|
|
|
// inferior FilterChooser's.
|
|
|
|
//
|
|
|
|
// A special case arises when there's only one entry in the filtered
|
|
|
|
// instructions. In order to unambiguously decode the singleton, we need to
|
|
|
|
// match the remaining undecoded encoding bits against the singleton.
|
|
|
|
void Filter::recurse() {
|
|
|
|
// Starts by inheriting our parent filter chooser's filter bit values.
|
2011-07-19 21:06:00 +00:00
|
|
|
std::vector<bit_value_t> BitValueArray(Owner->FilterBitValues);
|
2011-02-18 21:51:29 +00:00
|
|
|
|
2015-01-15 11:41:30 +00:00
|
|
|
if (!VariableInstructions.empty()) {
|
2011-02-18 21:51:29 +00:00
|
|
|
// Conservatively marks each segment position as BIT_UNSET.
|
2012-08-17 05:42:16 +00:00
|
|
|
for (unsigned bitIndex = 0; bitIndex < NumBits; ++bitIndex)
|
2011-02-18 21:51:29 +00:00
|
|
|
BitValueArray[StartBit + bitIndex] = BIT_UNSET;
|
|
|
|
|
2011-04-15 05:18:47 +00:00
|
|
|
// Delegates to an inferior filter chooser for further processing on this
|
2011-02-18 21:51:29 +00:00
|
|
|
// group of instructions whose segment values are variable.
|
2014-09-03 08:22:30 +00:00
|
|
|
FilterChooserMap.insert(
|
|
|
|
std::make_pair(-1U, llvm::make_unique<FilterChooser>(
|
|
|
|
Owner->AllInstructions, VariableInstructions,
|
|
|
|
Owner->Operands, BitValueArray, *Owner)));
|
2011-02-18 21:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// No need to recurse for a singleton filtered instruction.
|
2012-08-14 19:06:05 +00:00
|
|
|
// See also Filter::emit*().
|
2011-02-18 21:51:29 +00:00
|
|
|
if (getNumFiltered() == 1) {
|
|
|
|
//Owner->SingletonExists(LastOpcFiltered);
|
|
|
|
assert(FilterChooserMap.size() == 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, create sub choosers.
|
2014-12-13 05:12:19 +00:00
|
|
|
for (const auto &Inst : FilteredInstructions) {
|
2011-02-18 21:51:29 +00:00
|
|
|
|
|
|
|
// Marks all the segment positions with either BIT_TRUE or BIT_FALSE.
|
2012-08-17 05:42:16 +00:00
|
|
|
for (unsigned bitIndex = 0; bitIndex < NumBits; ++bitIndex) {
|
2014-12-13 05:12:19 +00:00
|
|
|
if (Inst.first & (1ULL << bitIndex))
|
2011-02-18 21:51:29 +00:00
|
|
|
BitValueArray[StartBit + bitIndex] = BIT_TRUE;
|
|
|
|
else
|
|
|
|
BitValueArray[StartBit + bitIndex] = BIT_FALSE;
|
|
|
|
}
|
|
|
|
|
2011-04-15 05:18:47 +00:00
|
|
|
// Delegates to an inferior filter chooser for further processing on this
|
2011-02-18 21:51:29 +00:00
|
|
|
// category of instructions.
|
2014-09-03 06:07:54 +00:00
|
|
|
FilterChooserMap.insert(std::make_pair(
|
2014-12-13 05:12:19 +00:00
|
|
|
Inst.first, llvm::make_unique<FilterChooser>(
|
|
|
|
Owner->AllInstructions, Inst.second,
|
2014-09-03 08:22:30 +00:00
|
|
|
Owner->Operands, BitValueArray, *Owner)));
|
2011-02-18 21:51:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-14 19:06:05 +00:00
|
|
|
static void resolveTableFixups(DecoderTable &Table, const FixupList &Fixups,
|
|
|
|
uint32_t DestIdx) {
|
|
|
|
// Any NumToSkip fixups in the current scope can resolve to the
|
|
|
|
// current location.
|
|
|
|
for (FixupList::const_reverse_iterator I = Fixups.rbegin(),
|
|
|
|
E = Fixups.rend();
|
|
|
|
I != E; ++I) {
|
|
|
|
// Calculate the distance from the byte following the fixup entry byte
|
|
|
|
// to the destination. The Target is calculated from after the 16-bit
|
|
|
|
// NumToSkip entry itself, so subtract two from the displacement here
|
|
|
|
// to account for that.
|
|
|
|
uint32_t FixupIdx = *I;
|
|
|
|
uint32_t Delta = DestIdx - FixupIdx - 2;
|
|
|
|
// Our NumToSkip entries are 16-bits. Make sure our table isn't too
|
|
|
|
// big.
|
|
|
|
assert(Delta < 65536U && "disassembler decoding table too large!");
|
|
|
|
Table[FixupIdx] = (uint8_t)Delta;
|
|
|
|
Table[FixupIdx + 1] = (uint8_t)(Delta >> 8);
|
|
|
|
}
|
|
|
|
}
|
2011-02-18 21:51:29 +00:00
|
|
|
|
2012-08-14 19:06:05 +00:00
|
|
|
// Emit table entries to decode instructions given a segment or segments
|
|
|
|
// of bits.
|
|
|
|
void Filter::emitTableEntry(DecoderTableInfo &TableInfo) const {
|
|
|
|
TableInfo.Table.push_back(MCD::OPC_ExtractField);
|
|
|
|
TableInfo.Table.push_back(StartBit);
|
|
|
|
TableInfo.Table.push_back(NumBits);
|
2011-02-18 21:51:29 +00:00
|
|
|
|
2012-08-14 19:06:05 +00:00
|
|
|
// A new filter entry begins a new scope for fixup resolution.
|
|
|
|
TableInfo.FixupStack.push_back(FixupList());
|
2011-02-18 21:51:29 +00:00
|
|
|
|
2012-08-14 19:06:05 +00:00
|
|
|
DecoderTable &Table = TableInfo.Table;
|
|
|
|
|
|
|
|
size_t PrevFilter = 0;
|
|
|
|
bool HasFallthrough = false;
|
2014-12-13 05:12:19 +00:00
|
|
|
for (auto &Filter : FilterChooserMap) {
|
2011-02-18 21:51:29 +00:00
|
|
|
// Field value -1 implies a non-empty set of variable instructions.
|
|
|
|
// See also recurse().
|
2014-12-13 05:12:19 +00:00
|
|
|
if (Filter.first == (unsigned)-1) {
|
2012-08-14 19:06:05 +00:00
|
|
|
HasFallthrough = true;
|
|
|
|
|
|
|
|
// Each scope should always have at least one filter value to check
|
|
|
|
// for.
|
|
|
|
assert(PrevFilter != 0 && "empty filter set!");
|
|
|
|
FixupList &CurScope = TableInfo.FixupStack.back();
|
|
|
|
// Resolve any NumToSkip fixups in the current scope.
|
|
|
|
resolveTableFixups(Table, CurScope, Table.size());
|
|
|
|
CurScope.clear();
|
|
|
|
PrevFilter = 0; // Don't re-process the filter's fallthrough.
|
|
|
|
} else {
|
|
|
|
Table.push_back(MCD::OPC_FilterValue);
|
|
|
|
// Encode and emit the value to filter against.
|
|
|
|
uint8_t Buffer[8];
|
2014-12-13 05:12:19 +00:00
|
|
|
unsigned Len = encodeULEB128(Filter.first, Buffer);
|
2012-08-14 19:06:05 +00:00
|
|
|
Table.insert(Table.end(), Buffer, Buffer + Len);
|
|
|
|
// Reserve space for the NumToSkip entry. We'll backpatch the value
|
|
|
|
// later.
|
|
|
|
PrevFilter = Table.size();
|
|
|
|
Table.push_back(0);
|
|
|
|
Table.push_back(0);
|
|
|
|
}
|
2011-02-18 21:51:29 +00:00
|
|
|
|
|
|
|
// We arrive at a category of instructions with the same segment value.
|
|
|
|
// Now delegate to the sub filter chooser for further decodings.
|
|
|
|
// The case may fallthrough, which happens if the remaining well-known
|
|
|
|
// encoding bits do not match exactly.
|
2014-12-13 05:12:19 +00:00
|
|
|
Filter.second->emitTableEntries(TableInfo);
|
2012-08-14 19:06:05 +00:00
|
|
|
|
|
|
|
// Now that we've emitted the body of the handler, update the NumToSkip
|
|
|
|
// of the filter itself to be able to skip forward when false. Subtract
|
|
|
|
// two as to account for the width of the NumToSkip field itself.
|
|
|
|
if (PrevFilter) {
|
|
|
|
uint32_t NumToSkip = Table.size() - PrevFilter - 2;
|
|
|
|
assert(NumToSkip < 65536U && "disassembler decoding table too large!");
|
|
|
|
Table[PrevFilter] = (uint8_t)NumToSkip;
|
|
|
|
Table[PrevFilter + 1] = (uint8_t)(NumToSkip >> 8);
|
|
|
|
}
|
2011-02-18 21:51:29 +00:00
|
|
|
}
|
|
|
|
|
2012-08-14 19:06:05 +00:00
|
|
|
// Any remaining unresolved fixups bubble up to the parent fixup scope.
|
|
|
|
assert(TableInfo.FixupStack.size() > 1 && "fixup stack underflow!");
|
|
|
|
FixupScopeList::iterator Source = TableInfo.FixupStack.end() - 1;
|
|
|
|
FixupScopeList::iterator Dest = Source - 1;
|
|
|
|
Dest->insert(Dest->end(), Source->begin(), Source->end());
|
|
|
|
TableInfo.FixupStack.pop_back();
|
|
|
|
|
|
|
|
// If there is no fallthrough, then the final filter should get fixed
|
|
|
|
// up according to the enclosing scope rather than the current position.
|
|
|
|
if (!HasFallthrough)
|
|
|
|
TableInfo.FixupStack.back().push_back(PrevFilter);
|
2011-02-18 21:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the number of fanout produced by the filter. More fanout implies
|
|
|
|
// the filter distinguishes more categories of instructions.
|
|
|
|
unsigned Filter::usefulness() const {
|
2015-01-15 11:41:30 +00:00
|
|
|
if (!VariableInstructions.empty())
|
2011-02-18 21:51:29 +00:00
|
|
|
return FilteredInstructions.size();
|
|
|
|
else
|
|
|
|
return FilteredInstructions.size() + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////
|
|
|
|
// //
|
|
|
|
// Filterchooser Implementation //
|
|
|
|
// //
|
|
|
|
//////////////////////////////////
|
|
|
|
|
2012-08-14 19:06:05 +00:00
|
|
|
// Emit the decoder state machine table.
|
|
|
|
void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
|
|
|
|
DecoderTable &Table,
|
|
|
|
unsigned Indentation,
|
|
|
|
unsigned BitWidth,
|
|
|
|
StringRef Namespace) const {
|
|
|
|
OS.indent(Indentation) << "static const uint8_t DecoderTable" << Namespace
|
|
|
|
<< BitWidth << "[] = {\n";
|
|
|
|
|
|
|
|
Indentation += 2;
|
|
|
|
|
|
|
|
// FIXME: We may be able to use the NumToSkip values to recover
|
|
|
|
// appropriate indentation levels.
|
|
|
|
DecoderTable::const_iterator I = Table.begin();
|
|
|
|
DecoderTable::const_iterator E = Table.end();
|
|
|
|
while (I != E) {
|
|
|
|
assert (I < E && "incomplete decode table entry!");
|
|
|
|
|
|
|
|
uint64_t Pos = I - Table.begin();
|
|
|
|
OS << "/* " << Pos << " */";
|
|
|
|
OS.PadToColumn(12);
|
|
|
|
|
|
|
|
switch (*I) {
|
|
|
|
default:
|
2012-10-25 20:33:17 +00:00
|
|
|
PrintFatalError("invalid decode table opcode");
|
2012-08-14 19:06:05 +00:00
|
|
|
case MCD::OPC_ExtractField: {
|
|
|
|
++I;
|
|
|
|
unsigned Start = *I++;
|
|
|
|
unsigned Len = *I++;
|
|
|
|
OS.indent(Indentation) << "MCD::OPC_ExtractField, " << Start << ", "
|
|
|
|
<< Len << ", // Inst{";
|
|
|
|
if (Len > 1)
|
|
|
|
OS << (Start + Len - 1) << "-";
|
|
|
|
OS << Start << "} ...\n";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MCD::OPC_FilterValue: {
|
|
|
|
++I;
|
|
|
|
OS.indent(Indentation) << "MCD::OPC_FilterValue, ";
|
|
|
|
// The filter value is ULEB128 encoded.
|
|
|
|
while (*I >= 128)
|
|
|
|
OS << utostr(*I++) << ", ";
|
|
|
|
OS << utostr(*I++) << ", ";
|
|
|
|
|
|
|
|
// 16-bit numtoskip value.
|
|
|
|
uint8_t Byte = *I++;
|
|
|
|
uint32_t NumToSkip = Byte;
|
|
|
|
OS << utostr(Byte) << ", ";
|
|
|
|
Byte = *I++;
|
|
|
|
OS << utostr(Byte) << ", ";
|
|
|
|
NumToSkip |= Byte << 8;
|
|
|
|
OS << "// Skip to: " << ((I - Table.begin()) + NumToSkip) << "\n";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MCD::OPC_CheckField: {
|
|
|
|
++I;
|
|
|
|
unsigned Start = *I++;
|
|
|
|
unsigned Len = *I++;
|
|
|
|
OS.indent(Indentation) << "MCD::OPC_CheckField, " << Start << ", "
|
|
|
|
<< Len << ", ";// << Val << ", " << NumToSkip << ",\n";
|
|
|
|
// ULEB128 encoded field value.
|
|
|
|
for (; *I >= 128; ++I)
|
|
|
|
OS << utostr(*I) << ", ";
|
|
|
|
OS << utostr(*I++) << ", ";
|
|
|
|
// 16-bit numtoskip value.
|
|
|
|
uint8_t Byte = *I++;
|
|
|
|
uint32_t NumToSkip = Byte;
|
|
|
|
OS << utostr(Byte) << ", ";
|
|
|
|
Byte = *I++;
|
|
|
|
OS << utostr(Byte) << ", ";
|
|
|
|
NumToSkip |= Byte << 8;
|
|
|
|
OS << "// Skip to: " << ((I - Table.begin()) + NumToSkip) << "\n";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MCD::OPC_CheckPredicate: {
|
|
|
|
++I;
|
|
|
|
OS.indent(Indentation) << "MCD::OPC_CheckPredicate, ";
|
|
|
|
for (; *I >= 128; ++I)
|
|
|
|
OS << utostr(*I) << ", ";
|
|
|
|
OS << utostr(*I++) << ", ";
|
|
|
|
|
|
|
|
// 16-bit numtoskip value.
|
|
|
|
uint8_t Byte = *I++;
|
|
|
|
uint32_t NumToSkip = Byte;
|
|
|
|
OS << utostr(Byte) << ", ";
|
|
|
|
Byte = *I++;
|
|
|
|
OS << utostr(Byte) << ", ";
|
|
|
|
NumToSkip |= Byte << 8;
|
|
|
|
OS << "// Skip to: " << ((I - Table.begin()) + NumToSkip) << "\n";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MCD::OPC_Decode: {
|
|
|
|
++I;
|
|
|
|
// Extract the ULEB128 encoded Opcode to a buffer.
|
|
|
|
uint8_t Buffer[8], *p = Buffer;
|
|
|
|
while ((*p++ = *I++) >= 128)
|
|
|
|
assert((p - Buffer) <= (ptrdiff_t)sizeof(Buffer)
|
|
|
|
&& "ULEB128 value too large!");
|
|
|
|
// Decode the Opcode value.
|
|
|
|
unsigned Opc = decodeULEB128(Buffer);
|
|
|
|
OS.indent(Indentation) << "MCD::OPC_Decode, ";
|
|
|
|
for (p = Buffer; *p >= 128; ++p)
|
|
|
|
OS << utostr(*p) << ", ";
|
|
|
|
OS << utostr(*p) << ", ";
|
|
|
|
|
|
|
|
// Decoder index.
|
|
|
|
for (; *I >= 128; ++I)
|
|
|
|
OS << utostr(*I) << ", ";
|
|
|
|
OS << utostr(*I++) << ", ";
|
|
|
|
|
|
|
|
OS << "// Opcode: "
|
|
|
|
<< NumberedInstructions->at(Opc)->TheDef->getName() << "\n";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MCD::OPC_SoftFail: {
|
|
|
|
++I;
|
|
|
|
OS.indent(Indentation) << "MCD::OPC_SoftFail";
|
|
|
|
// Positive mask
|
|
|
|
uint64_t Value = 0;
|
|
|
|
unsigned Shift = 0;
|
|
|
|
do {
|
|
|
|
OS << ", " << utostr(*I);
|
|
|
|
Value += (*I & 0x7f) << Shift;
|
|
|
|
Shift += 7;
|
|
|
|
} while (*I++ >= 128);
|
|
|
|
if (Value > 127)
|
|
|
|
OS << " /* 0x" << utohexstr(Value) << " */";
|
|
|
|
// Negative mask
|
|
|
|
Value = 0;
|
|
|
|
Shift = 0;
|
|
|
|
do {
|
|
|
|
OS << ", " << utostr(*I);
|
|
|
|
Value += (*I & 0x7f) << Shift;
|
|
|
|
Shift += 7;
|
|
|
|
} while (*I++ >= 128);
|
|
|
|
if (Value > 127)
|
|
|
|
OS << " /* 0x" << utohexstr(Value) << " */";
|
|
|
|
OS << ",\n";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MCD::OPC_Fail: {
|
|
|
|
++I;
|
|
|
|
OS.indent(Indentation) << "MCD::OPC_Fail,\n";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
OS.indent(Indentation) << "0\n";
|
|
|
|
|
|
|
|
Indentation -= 2;
|
|
|
|
|
|
|
|
OS.indent(Indentation) << "};\n\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void FixedLenDecoderEmitter::
|
|
|
|
emitPredicateFunction(formatted_raw_ostream &OS, PredicateSet &Predicates,
|
|
|
|
unsigned Indentation) const {
|
|
|
|
// The predicate function is just a big switch statement based on the
|
|
|
|
// input predicate index.
|
|
|
|
OS.indent(Indentation) << "static bool checkDecoderPredicate(unsigned Idx, "
|
2015-02-19 11:38:11 +00:00
|
|
|
<< "uint64_t Bits) {\n";
|
2012-08-14 19:06:05 +00:00
|
|
|
Indentation += 2;
|
2013-07-15 16:53:32 +00:00
|
|
|
if (!Predicates.empty()) {
|
|
|
|
OS.indent(Indentation) << "switch (Idx) {\n";
|
|
|
|
OS.indent(Indentation) << "default: llvm_unreachable(\"Invalid index!\");\n";
|
|
|
|
unsigned Index = 0;
|
2014-12-13 05:12:19 +00:00
|
|
|
for (const auto &Predicate : Predicates) {
|
|
|
|
OS.indent(Indentation) << "case " << Index++ << ":\n";
|
|
|
|
OS.indent(Indentation+2) << "return (" << Predicate << ");\n";
|
2013-07-15 16:53:32 +00:00
|
|
|
}
|
|
|
|
OS.indent(Indentation) << "}\n";
|
|
|
|
} else {
|
|
|
|
// No case statement to emit
|
|
|
|
OS.indent(Indentation) << "llvm_unreachable(\"Invalid index!\");\n";
|
2012-08-14 19:06:05 +00:00
|
|
|
}
|
|
|
|
Indentation -= 2;
|
|
|
|
OS.indent(Indentation) << "}\n\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void FixedLenDecoderEmitter::
|
|
|
|
emitDecoderFunction(formatted_raw_ostream &OS, DecoderSet &Decoders,
|
|
|
|
unsigned Indentation) const {
|
|
|
|
// The decoder function is just a big switch statement based on the
|
|
|
|
// input decoder index.
|
|
|
|
OS.indent(Indentation) << "template<typename InsnType>\n";
|
|
|
|
OS.indent(Indentation) << "static DecodeStatus decodeToMCInst(DecodeStatus S,"
|
|
|
|
<< " unsigned Idx, InsnType insn, MCInst &MI,\n";
|
|
|
|
OS.indent(Indentation) << " uint64_t "
|
2012-08-15 10:26:44 +00:00
|
|
|
<< "Address, const void *Decoder) {\n";
|
2012-08-14 19:06:05 +00:00
|
|
|
Indentation += 2;
|
|
|
|
OS.indent(Indentation) << "InsnType tmp;\n";
|
|
|
|
OS.indent(Indentation) << "switch (Idx) {\n";
|
|
|
|
OS.indent(Indentation) << "default: llvm_unreachable(\"Invalid index!\");\n";
|
|
|
|
unsigned Index = 0;
|
2014-12-13 05:12:19 +00:00
|
|
|
for (const auto &Decoder : Decoders) {
|
|
|
|
OS.indent(Indentation) << "case " << Index++ << ":\n";
|
|
|
|
OS << Decoder;
|
2012-08-14 19:06:05 +00:00
|
|
|
OS.indent(Indentation+2) << "return S;\n";
|
|
|
|
}
|
|
|
|
OS.indent(Indentation) << "}\n";
|
|
|
|
Indentation -= 2;
|
|
|
|
OS.indent(Indentation) << "}\n\n";
|
2011-02-18 21:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Populates the field of the insn given the start position and the number of
|
|
|
|
// consecutive bits to scan for.
|
|
|
|
//
|
|
|
|
// Returns false if and on the first uninitialized bit value encountered.
|
|
|
|
// Returns true, otherwise.
|
|
|
|
bool FilterChooser::fieldFromInsn(uint64_t &Field, insn_t &Insn,
|
2012-03-16 05:58:09 +00:00
|
|
|
unsigned StartBit, unsigned NumBits) const {
|
2011-02-18 21:51:29 +00:00
|
|
|
Field = 0;
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < NumBits; ++i) {
|
|
|
|
if (Insn[StartBit + i] == BIT_UNSET)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (Insn[StartBit + i] == BIT_TRUE)
|
|
|
|
Field = Field | (1ULL << i);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// dumpFilterArray - dumpFilterArray prints out debugging info for the given
|
|
|
|
/// filter array as a series of chars.
|
|
|
|
void FilterChooser::dumpFilterArray(raw_ostream &o,
|
2012-03-16 05:58:09 +00:00
|
|
|
const std::vector<bit_value_t> &filter) const {
|
2012-08-17 05:42:16 +00:00
|
|
|
for (unsigned bitIndex = BitWidth; bitIndex > 0; bitIndex--) {
|
2011-02-18 21:51:29 +00:00
|
|
|
switch (filter[bitIndex - 1]) {
|
|
|
|
case BIT_UNFILTERED:
|
|
|
|
o << ".";
|
|
|
|
break;
|
|
|
|
case BIT_UNSET:
|
|
|
|
o << "_";
|
|
|
|
break;
|
|
|
|
case BIT_TRUE:
|
|
|
|
o << "1";
|
|
|
|
break;
|
|
|
|
case BIT_FALSE:
|
|
|
|
o << "0";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// dumpStack - dumpStack traverses the filter chooser chain and calls
|
|
|
|
/// dumpFilterArray on each filter chooser up to the top level one.
|
2012-03-16 05:58:09 +00:00
|
|
|
void FilterChooser::dumpStack(raw_ostream &o, const char *prefix) const {
|
|
|
|
const FilterChooser *current = this;
|
2011-02-18 21:51:29 +00:00
|
|
|
|
|
|
|
while (current) {
|
|
|
|
o << prefix;
|
|
|
|
dumpFilterArray(o, current->FilterBitValues);
|
|
|
|
o << '\n';
|
|
|
|
current = current->Parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called from Filter::recurse() when singleton exists. For debug purpose.
|
2012-03-16 05:58:09 +00:00
|
|
|
void FilterChooser::SingletonExists(unsigned Opc) const {
|
2011-02-18 21:51:29 +00:00
|
|
|
insn_t Insn0;
|
|
|
|
insnWithID(Insn0, Opc);
|
|
|
|
|
|
|
|
errs() << "Singleton exists: " << nameWithID(Opc)
|
|
|
|
<< " with its decoding dominating ";
|
|
|
|
for (unsigned i = 0; i < Opcodes.size(); ++i) {
|
|
|
|
if (Opcodes[i] == Opc) continue;
|
|
|
|
errs() << nameWithID(Opcodes[i]) << ' ';
|
|
|
|
}
|
|
|
|
errs() << '\n';
|
|
|
|
|
|
|
|
dumpStack(errs(), "\t\t");
|
2012-03-16 01:19:24 +00:00
|
|
|
for (unsigned i = 0; i < Opcodes.size(); ++i) {
|
2011-02-18 21:51:29 +00:00
|
|
|
const std::string &Name = nameWithID(Opcodes[i]);
|
|
|
|
|
|
|
|
errs() << '\t' << Name << " ";
|
|
|
|
dumpBits(errs(),
|
|
|
|
getBitsField(*AllInstructions[Opcodes[i]]->TheDef, "Inst"));
|
|
|
|
errs() << '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculates the island(s) needed to decode the instruction.
|
|
|
|
// This returns a list of undecoded bits of an instructions, for example,
|
|
|
|
// Inst{20} = 1 && Inst{3-0} == 0b1111 represents two islands of yet-to-be
|
|
|
|
// decoded bits in order to verify that the instruction matches the Opcode.
|
|
|
|
unsigned FilterChooser::getIslands(std::vector<unsigned> &StartBits,
|
2012-03-16 01:19:24 +00:00
|
|
|
std::vector<unsigned> &EndBits,
|
|
|
|
std::vector<uint64_t> &FieldVals,
|
2012-03-16 05:58:09 +00:00
|
|
|
const insn_t &Insn) const {
|
2011-02-18 21:51:29 +00:00
|
|
|
unsigned Num, BitNo;
|
|
|
|
Num = BitNo = 0;
|
|
|
|
|
|
|
|
uint64_t FieldVal = 0;
|
|
|
|
|
|
|
|
// 0: Init
|
|
|
|
// 1: Water (the bit value does not affect decoding)
|
|
|
|
// 2: Island (well-known bit value needed for decoding)
|
|
|
|
int State = 0;
|
|
|
|
int Val = -1;
|
|
|
|
|
2011-07-19 21:06:00 +00:00
|
|
|
for (unsigned i = 0; i < BitWidth; ++i) {
|
2011-02-18 21:51:29 +00:00
|
|
|
Val = Value(Insn[i]);
|
|
|
|
bool Filtered = PositionFiltered(i);
|
|
|
|
switch (State) {
|
2012-02-05 07:21:30 +00:00
|
|
|
default: llvm_unreachable("Unreachable code!");
|
2011-02-18 21:51:29 +00:00
|
|
|
case 0:
|
|
|
|
case 1:
|
|
|
|
if (Filtered || Val == -1)
|
|
|
|
State = 1; // Still in Water
|
|
|
|
else {
|
|
|
|
State = 2; // Into the Island
|
|
|
|
BitNo = 0;
|
|
|
|
StartBits.push_back(i);
|
|
|
|
FieldVal = Val;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
if (Filtered || Val == -1) {
|
|
|
|
State = 1; // Into the Water
|
|
|
|
EndBits.push_back(i - 1);
|
|
|
|
FieldVals.push_back(FieldVal);
|
|
|
|
++Num;
|
|
|
|
} else {
|
|
|
|
State = 2; // Still in Island
|
|
|
|
++BitNo;
|
|
|
|
FieldVal = FieldVal | Val << BitNo;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If we are still in Island after the loop, do some housekeeping.
|
|
|
|
if (State == 2) {
|
2011-07-19 21:06:00 +00:00
|
|
|
EndBits.push_back(BitWidth - 1);
|
2011-02-18 21:51:29 +00:00
|
|
|
FieldVals.push_back(FieldVal);
|
|
|
|
++Num;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(StartBits.size() == Num && EndBits.size() == Num &&
|
|
|
|
FieldVals.size() == Num);
|
|
|
|
return Num;
|
|
|
|
}
|
|
|
|
|
2011-07-28 21:54:31 +00:00
|
|
|
void FilterChooser::emitBinaryParser(raw_ostream &o, unsigned &Indentation,
|
2012-03-16 05:58:09 +00:00
|
|
|
const OperandInfo &OpInfo) const {
|
|
|
|
const std::string &Decoder = OpInfo.Decoder;
|
2011-07-28 21:54:31 +00:00
|
|
|
|
2014-09-27 05:26:42 +00:00
|
|
|
if (OpInfo.numFields() != 1)
|
2012-08-17 05:16:15 +00:00
|
|
|
o.indent(Indentation) << "tmp = 0;\n";
|
2014-09-27 05:26:42 +00:00
|
|
|
|
|
|
|
for (const EncodingField &EF : OpInfo) {
|
|
|
|
o.indent(Indentation) << "tmp ";
|
|
|
|
if (OpInfo.numFields() != 1) o << '|';
|
|
|
|
o << "= fieldFromInstruction"
|
|
|
|
<< "(insn, " << EF.Base << ", " << EF.Width << ')';
|
|
|
|
if (OpInfo.numFields() != 1 || EF.Offset != 0)
|
|
|
|
o << " << " << EF.Offset;
|
|
|
|
o << ";\n";
|
2011-07-28 21:54:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Decoder != "")
|
2012-08-17 05:16:15 +00:00
|
|
|
o.indent(Indentation) << Emitter->GuardPrefix << Decoder
|
2012-02-29 22:07:56 +00:00
|
|
|
<< "(MI, tmp, Address, Decoder)"
|
|
|
|
<< Emitter->GuardPostfix << "\n";
|
2011-07-28 21:54:31 +00:00
|
|
|
else
|
2012-08-17 05:16:15 +00:00
|
|
|
o.indent(Indentation) << "MI.addOperand(MCOperand::CreateImm(tmp));\n";
|
2011-07-28 21:54:31 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-08-14 19:06:05 +00:00
|
|
|
void FilterChooser::emitDecoder(raw_ostream &OS, unsigned Indentation,
|
|
|
|
unsigned Opc) const {
|
2014-12-13 05:12:19 +00:00
|
|
|
for (const auto &Op : Operands.find(Opc)->second) {
|
2012-08-14 19:06:05 +00:00
|
|
|
// If a custom instruction decoder was specified, use that.
|
2014-12-13 05:12:19 +00:00
|
|
|
if (Op.numFields() == 0 && Op.Decoder.size()) {
|
|
|
|
OS.indent(Indentation) << Emitter->GuardPrefix << Op.Decoder
|
2012-08-14 19:06:05 +00:00
|
|
|
<< "(MI, insn, Address, Decoder)"
|
|
|
|
<< Emitter->GuardPostfix << "\n";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-12-13 05:12:19 +00:00
|
|
|
emitBinaryParser(OS, Indentation, Op);
|
2012-08-14 19:06:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned FilterChooser::getDecoderIndex(DecoderSet &Decoders,
|
|
|
|
unsigned Opc) const {
|
|
|
|
// Build up the predicate string.
|
|
|
|
SmallString<256> Decoder;
|
|
|
|
// FIXME: emitDecoder() function can take a buffer directly rather than
|
|
|
|
// a stream.
|
|
|
|
raw_svector_ostream S(Decoder);
|
2012-08-17 05:16:15 +00:00
|
|
|
unsigned I = 4;
|
2012-08-14 19:06:05 +00:00
|
|
|
emitDecoder(S, I, Opc);
|
|
|
|
S.flush();
|
|
|
|
|
|
|
|
// Using the full decoder string as the key value here is a bit
|
|
|
|
// heavyweight, but is effective. If the string comparisons become a
|
|
|
|
// performance concern, we can implement a mangling of the predicate
|
|
|
|
// data easilly enough with a map back to the actual string. That's
|
|
|
|
// overkill for now, though.
|
|
|
|
|
|
|
|
// Make sure the predicate is in the table.
|
|
|
|
Decoders.insert(Decoder.str());
|
|
|
|
// Now figure out the index for when we write out the table.
|
|
|
|
DecoderSet::const_iterator P = std::find(Decoders.begin(),
|
|
|
|
Decoders.end(),
|
|
|
|
Decoder.str());
|
|
|
|
return (unsigned)(P - Decoders.begin());
|
|
|
|
}
|
|
|
|
|
2011-09-07 19:42:28 +00:00
|
|
|
static void emitSinglePredicateMatch(raw_ostream &o, StringRef str,
|
2012-03-16 05:58:09 +00:00
|
|
|
const std::string &PredicateNamespace) {
|
2011-09-08 05:25:49 +00:00
|
|
|
if (str[0] == '!')
|
2015-02-19 11:38:11 +00:00
|
|
|
o << "!(Bits & " << PredicateNamespace << "::"
|
|
|
|
<< str.slice(1,str.size()) << ")";
|
2011-09-07 19:42:28 +00:00
|
|
|
else
|
2015-02-19 11:38:11 +00:00
|
|
|
o << "(Bits & " << PredicateNamespace << "::" << str << ")";
|
2011-09-07 19:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FilterChooser::emitPredicateMatch(raw_ostream &o, unsigned &Indentation,
|
2012-03-16 05:58:09 +00:00
|
|
|
unsigned Opc) const {
|
2012-02-29 22:07:56 +00:00
|
|
|
ListInit *Predicates =
|
|
|
|
AllInstructions[Opc]->TheDef->getValueAsListInit("Predicates");
|
2011-09-07 19:42:28 +00:00
|
|
|
for (unsigned i = 0; i < Predicates->getSize(); ++i) {
|
|
|
|
Record *Pred = Predicates->getElementAsRecord(i);
|
|
|
|
if (!Pred->getValue("AssemblerMatcherPredicate"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
std::string P = Pred->getValueAsString("AssemblerCondString");
|
|
|
|
|
|
|
|
if (!P.length())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (i != 0)
|
|
|
|
o << " && ";
|
|
|
|
|
|
|
|
StringRef SR(P);
|
|
|
|
std::pair<StringRef, StringRef> pairs = SR.split(',');
|
|
|
|
while (pairs.second.size()) {
|
|
|
|
emitSinglePredicateMatch(o, pairs.first, Emitter->PredicateNamespace);
|
|
|
|
o << " && ";
|
|
|
|
pairs = pairs.second.split(',');
|
|
|
|
}
|
|
|
|
emitSinglePredicateMatch(o, pairs.first, Emitter->PredicateNamespace);
|
|
|
|
}
|
|
|
|
return Predicates->getSize() > 0;
|
2011-09-08 05:23:14 +00:00
|
|
|
}
|
2011-09-07 19:42:28 +00:00
|
|
|
|
2012-08-14 19:06:05 +00:00
|
|
|
bool FilterChooser::doesOpcodeNeedPredicate(unsigned Opc) const {
|
|
|
|
ListInit *Predicates =
|
|
|
|
AllInstructions[Opc]->TheDef->getValueAsListInit("Predicates");
|
|
|
|
for (unsigned i = 0; i < Predicates->getSize(); ++i) {
|
|
|
|
Record *Pred = Predicates->getElementAsRecord(i);
|
|
|
|
if (!Pred->getValue("AssemblerMatcherPredicate"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
std::string P = Pred->getValueAsString("AssemblerCondString");
|
|
|
|
|
|
|
|
if (!P.length())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned FilterChooser::getPredicateIndex(DecoderTableInfo &TableInfo,
|
|
|
|
StringRef Predicate) const {
|
|
|
|
// Using the full predicate string as the key value here is a bit
|
|
|
|
// heavyweight, but is effective. If the string comparisons become a
|
|
|
|
// performance concern, we can implement a mangling of the predicate
|
|
|
|
// data easilly enough with a map back to the actual string. That's
|
|
|
|
// overkill for now, though.
|
|
|
|
|
|
|
|
// Make sure the predicate is in the table.
|
|
|
|
TableInfo.Predicates.insert(Predicate.str());
|
|
|
|
// Now figure out the index for when we write out the table.
|
|
|
|
PredicateSet::const_iterator P = std::find(TableInfo.Predicates.begin(),
|
|
|
|
TableInfo.Predicates.end(),
|
|
|
|
Predicate.str());
|
|
|
|
return (unsigned)(P - TableInfo.Predicates.begin());
|
|
|
|
}
|
|
|
|
|
|
|
|
void FilterChooser::emitPredicateTableEntry(DecoderTableInfo &TableInfo,
|
|
|
|
unsigned Opc) const {
|
|
|
|
if (!doesOpcodeNeedPredicate(Opc))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Build up the predicate string.
|
|
|
|
SmallString<256> Predicate;
|
|
|
|
// FIXME: emitPredicateMatch() functions can take a buffer directly rather
|
|
|
|
// than a stream.
|
|
|
|
raw_svector_ostream PS(Predicate);
|
|
|
|
unsigned I = 0;
|
|
|
|
emitPredicateMatch(PS, I, Opc);
|
|
|
|
|
|
|
|
// Figure out the index into the predicate table for the predicate just
|
|
|
|
// computed.
|
|
|
|
unsigned PIdx = getPredicateIndex(TableInfo, PS.str());
|
|
|
|
SmallString<16> PBytes;
|
|
|
|
raw_svector_ostream S(PBytes);
|
|
|
|
encodeULEB128(PIdx, S);
|
|
|
|
S.flush();
|
|
|
|
|
|
|
|
TableInfo.Table.push_back(MCD::OPC_CheckPredicate);
|
|
|
|
// Predicate index
|
2012-08-17 05:42:16 +00:00
|
|
|
for (unsigned i = 0, e = PBytes.size(); i != e; ++i)
|
2012-08-14 19:06:05 +00:00
|
|
|
TableInfo.Table.push_back(PBytes[i]);
|
|
|
|
// Push location for NumToSkip backpatching.
|
|
|
|
TableInfo.FixupStack.back().push_back(TableInfo.Table.size());
|
|
|
|
TableInfo.Table.push_back(0);
|
|
|
|
TableInfo.Table.push_back(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FilterChooser::emitSoftFailTableEntry(DecoderTableInfo &TableInfo,
|
|
|
|
unsigned Opc) const {
|
2012-02-29 22:07:56 +00:00
|
|
|
BitsInit *SFBits =
|
|
|
|
AllInstructions[Opc]->TheDef->getValueAsBitsInit("SoftFail");
|
2012-02-09 10:56:31 +00:00
|
|
|
if (!SFBits) return;
|
|
|
|
BitsInit *InstBits = AllInstructions[Opc]->TheDef->getValueAsBitsInit("Inst");
|
|
|
|
|
|
|
|
APInt PositiveMask(BitWidth, 0ULL);
|
|
|
|
APInt NegativeMask(BitWidth, 0ULL);
|
|
|
|
for (unsigned i = 0; i < BitWidth; ++i) {
|
|
|
|
bit_value_t B = bitFromBits(*SFBits, i);
|
|
|
|
bit_value_t IB = bitFromBits(*InstBits, i);
|
|
|
|
|
|
|
|
if (B != BIT_TRUE) continue;
|
|
|
|
|
|
|
|
switch (IB) {
|
|
|
|
case BIT_FALSE:
|
|
|
|
// The bit is meant to be false, so emit a check to see if it is true.
|
|
|
|
PositiveMask.setBit(i);
|
|
|
|
break;
|
|
|
|
case BIT_TRUE:
|
|
|
|
// The bit is meant to be true, so emit a check to see if it is false.
|
|
|
|
NegativeMask.setBit(i);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// The bit is not set; this must be an error!
|
|
|
|
StringRef Name = AllInstructions[Opc]->TheDef->getName();
|
2012-08-14 19:06:05 +00:00
|
|
|
errs() << "SoftFail Conflict: bit SoftFail{" << i << "} in " << Name
|
|
|
|
<< " is set but Inst{" << i << "} is unset!\n"
|
2012-02-09 10:56:31 +00:00
|
|
|
<< " - You can only mark a bit as SoftFail if it is fully defined"
|
|
|
|
<< " (1/0 - not '?') in Inst\n";
|
2012-08-14 19:06:05 +00:00
|
|
|
return;
|
2012-02-09 10:56:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NeedPositiveMask = PositiveMask.getBoolValue();
|
|
|
|
bool NeedNegativeMask = NegativeMask.getBoolValue();
|
|
|
|
|
|
|
|
if (!NeedPositiveMask && !NeedNegativeMask)
|
|
|
|
return;
|
|
|
|
|
2012-08-14 19:06:05 +00:00
|
|
|
TableInfo.Table.push_back(MCD::OPC_SoftFail);
|
|
|
|
|
|
|
|
SmallString<16> MaskBytes;
|
|
|
|
raw_svector_ostream S(MaskBytes);
|
|
|
|
if (NeedPositiveMask) {
|
|
|
|
encodeULEB128(PositiveMask.getZExtValue(), S);
|
|
|
|
S.flush();
|
2012-08-17 05:42:16 +00:00
|
|
|
for (unsigned i = 0, e = MaskBytes.size(); i != e; ++i)
|
2012-08-14 19:06:05 +00:00
|
|
|
TableInfo.Table.push_back(MaskBytes[i]);
|
|
|
|
} else
|
|
|
|
TableInfo.Table.push_back(0);
|
|
|
|
if (NeedNegativeMask) {
|
|
|
|
MaskBytes.clear();
|
|
|
|
S.resync();
|
|
|
|
encodeULEB128(NegativeMask.getZExtValue(), S);
|
|
|
|
S.flush();
|
2012-08-17 05:42:16 +00:00
|
|
|
for (unsigned i = 0, e = MaskBytes.size(); i != e; ++i)
|
2012-08-14 19:06:05 +00:00
|
|
|
TableInfo.Table.push_back(MaskBytes[i]);
|
|
|
|
} else
|
|
|
|
TableInfo.Table.push_back(0);
|
2012-02-09 10:56:31 +00:00
|
|
|
}
|
|
|
|
|
2012-08-14 19:06:05 +00:00
|
|
|
// Emits table entries to decode the singleton.
|
|
|
|
void FilterChooser::emitSingletonTableEntry(DecoderTableInfo &TableInfo,
|
|
|
|
unsigned Opc) const {
|
2011-02-18 21:51:29 +00:00
|
|
|
std::vector<unsigned> StartBits;
|
|
|
|
std::vector<unsigned> EndBits;
|
|
|
|
std::vector<uint64_t> FieldVals;
|
|
|
|
insn_t Insn;
|
|
|
|
insnWithID(Insn, Opc);
|
|
|
|
|
|
|
|
// Look for islands of undecoded bits of the singleton.
|
|
|
|
getIslands(StartBits, EndBits, FieldVals, Insn);
|
|
|
|
|
|
|
|
unsigned Size = StartBits.size();
|
|
|
|
|
2012-08-14 19:06:05 +00:00
|
|
|
// Emit the predicate table entry if one is needed.
|
|
|
|
emitPredicateTableEntry(TableInfo, Opc);
|
2011-02-18 21:51:29 +00:00
|
|
|
|
2012-08-14 19:06:05 +00:00
|
|
|
// Check any additional encoding fields needed.
|
2012-08-17 05:42:16 +00:00
|
|
|
for (unsigned I = Size; I != 0; --I) {
|
|
|
|
unsigned NumBits = EndBits[I-1] - StartBits[I-1] + 1;
|
2012-08-14 19:06:05 +00:00
|
|
|
TableInfo.Table.push_back(MCD::OPC_CheckField);
|
|
|
|
TableInfo.Table.push_back(StartBits[I-1]);
|
|
|
|
TableInfo.Table.push_back(NumBits);
|
|
|
|
uint8_t Buffer[8], *p;
|
|
|
|
encodeULEB128(FieldVals[I-1], Buffer);
|
|
|
|
for (p = Buffer; *p >= 128 ; ++p)
|
|
|
|
TableInfo.Table.push_back(*p);
|
|
|
|
TableInfo.Table.push_back(*p);
|
|
|
|
// Push location for NumToSkip backpatching.
|
|
|
|
TableInfo.FixupStack.back().push_back(TableInfo.Table.size());
|
|
|
|
// The fixup is always 16-bits, so go ahead and allocate the space
|
|
|
|
// in the table so all our relative position calculations work OK even
|
|
|
|
// before we fully resolve the real value here.
|
|
|
|
TableInfo.Table.push_back(0);
|
|
|
|
TableInfo.Table.push_back(0);
|
2011-02-18 21:51:29 +00:00
|
|
|
}
|
|
|
|
|
2012-08-14 19:06:05 +00:00
|
|
|
// Check for soft failure of the match.
|
|
|
|
emitSoftFailTableEntry(TableInfo, Opc);
|
|
|
|
|
|
|
|
TableInfo.Table.push_back(MCD::OPC_Decode);
|
|
|
|
uint8_t Buffer[8], *p;
|
|
|
|
encodeULEB128(Opc, Buffer);
|
|
|
|
for (p = Buffer; *p >= 128 ; ++p)
|
|
|
|
TableInfo.Table.push_back(*p);
|
|
|
|
TableInfo.Table.push_back(*p);
|
|
|
|
|
|
|
|
unsigned DIdx = getDecoderIndex(TableInfo.Decoders, Opc);
|
|
|
|
SmallString<16> Bytes;
|
|
|
|
raw_svector_ostream S(Bytes);
|
|
|
|
encodeULEB128(DIdx, S);
|
|
|
|
S.flush();
|
|
|
|
|
|
|
|
// Decoder index
|
2012-08-17 05:42:16 +00:00
|
|
|
for (unsigned i = 0, e = Bytes.size(); i != e; ++i)
|
2012-08-14 19:06:05 +00:00
|
|
|
TableInfo.Table.push_back(Bytes[i]);
|
2011-02-18 21:51:29 +00:00
|
|
|
}
|
|
|
|
|
2012-08-14 19:06:05 +00:00
|
|
|
// Emits table entries to decode the singleton, and then to decode the rest.
|
|
|
|
void FilterChooser::emitSingletonTableEntry(DecoderTableInfo &TableInfo,
|
|
|
|
const Filter &Best) const {
|
2011-02-18 21:51:29 +00:00
|
|
|
unsigned Opc = Best.getSingletonOpc();
|
|
|
|
|
2012-08-14 19:06:05 +00:00
|
|
|
// complex singletons need predicate checks from the first singleton
|
|
|
|
// to refer forward to the variable filterchooser that follows.
|
|
|
|
TableInfo.FixupStack.push_back(FixupList());
|
2011-02-18 21:51:29 +00:00
|
|
|
|
2012-08-14 19:06:05 +00:00
|
|
|
emitSingletonTableEntry(TableInfo, Opc);
|
2011-02-18 21:51:29 +00:00
|
|
|
|
2012-08-14 19:06:05 +00:00
|
|
|
resolveTableFixups(TableInfo.Table, TableInfo.FixupStack.back(),
|
|
|
|
TableInfo.Table.size());
|
|
|
|
TableInfo.FixupStack.pop_back();
|
|
|
|
|
|
|
|
Best.getVariableFC().emitTableEntries(TableInfo);
|
2011-02-18 21:51:29 +00:00
|
|
|
}
|
|
|
|
|
2012-08-14 19:06:05 +00:00
|
|
|
|
2011-02-18 21:51:29 +00:00
|
|
|
// Assign a single filter and run with it. Top level API client can initialize
|
|
|
|
// with a single filter to start the filtering process.
|
2012-03-16 05:58:09 +00:00
|
|
|
void FilterChooser::runSingleFilter(unsigned startBit, unsigned numBit,
|
|
|
|
bool mixed) {
|
2011-02-18 21:51:29 +00:00
|
|
|
Filters.clear();
|
2014-09-03 05:49:07 +00:00
|
|
|
Filters.push_back(Filter(*this, startBit, numBit, true));
|
2011-02-18 21:51:29 +00:00
|
|
|
BestIndex = 0; // Sole Filter instance to choose from.
|
|
|
|
bestFilter().recurse();
|
|
|
|
}
|
|
|
|
|
|
|
|
// reportRegion is a helper function for filterProcessor to mark a region as
|
|
|
|
// eligible for use as a filter region.
|
|
|
|
void FilterChooser::reportRegion(bitAttr_t RA, unsigned StartBit,
|
2012-03-16 01:19:24 +00:00
|
|
|
unsigned BitIndex, bool AllowMixed) {
|
2011-02-18 21:51:29 +00:00
|
|
|
if (RA == ATTR_MIXED && AllowMixed)
|
|
|
|
Filters.push_back(Filter(*this, StartBit, BitIndex - StartBit, true));
|
|
|
|
else if (RA == ATTR_ALL_SET && !AllowMixed)
|
|
|
|
Filters.push_back(Filter(*this, StartBit, BitIndex - StartBit, false));
|
|
|
|
}
|
|
|
|
|
|
|
|
// FilterProcessor scans the well-known encoding bits of the instructions and
|
|
|
|
// builds up a list of candidate filters. It chooses the best filter and
|
|
|
|
// recursively descends down the decoding tree.
|
|
|
|
bool FilterChooser::filterProcessor(bool AllowMixed, bool Greedy) {
|
|
|
|
Filters.clear();
|
|
|
|
BestIndex = -1;
|
|
|
|
unsigned numInstructions = Opcodes.size();
|
|
|
|
|
|
|
|
assert(numInstructions && "Filter created with no instructions");
|
|
|
|
|
|
|
|
// No further filtering is necessary.
|
|
|
|
if (numInstructions == 1)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Heuristics. See also doFilter()'s "Heuristics" comment when num of
|
|
|
|
// instructions is 3.
|
|
|
|
if (AllowMixed && !Greedy) {
|
|
|
|
assert(numInstructions == 3);
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < Opcodes.size(); ++i) {
|
|
|
|
std::vector<unsigned> StartBits;
|
|
|
|
std::vector<unsigned> EndBits;
|
|
|
|
std::vector<uint64_t> FieldVals;
|
|
|
|
insn_t Insn;
|
|
|
|
|
|
|
|
insnWithID(Insn, Opcodes[i]);
|
|
|
|
|
|
|
|
// Look for islands of undecoded bits of any instruction.
|
|
|
|
if (getIslands(StartBits, EndBits, FieldVals, Insn) > 0) {
|
|
|
|
// Found an instruction with island(s). Now just assign a filter.
|
2012-03-16 05:58:09 +00:00
|
|
|
runSingleFilter(StartBits[0], EndBits[0] - StartBits[0] + 1, true);
|
2011-02-18 21:51:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-17 05:42:16 +00:00
|
|
|
unsigned BitIndex;
|
2011-02-18 21:51:29 +00:00
|
|
|
|
|
|
|
// We maintain BIT_WIDTH copies of the bitAttrs automaton.
|
|
|
|
// The automaton consumes the corresponding bit from each
|
|
|
|
// instruction.
|
|
|
|
//
|
|
|
|
// Input symbols: 0, 1, and _ (unset).
|
|
|
|
// States: NONE, FILTERED, ALL_SET, ALL_UNSET, and MIXED.
|
|
|
|
// Initial state: NONE.
|
|
|
|
//
|
|
|
|
// (NONE) ------- [01] -> (ALL_SET)
|
|
|
|
// (NONE) ------- _ ----> (ALL_UNSET)
|
|
|
|
// (ALL_SET) ---- [01] -> (ALL_SET)
|
|
|
|
// (ALL_SET) ---- _ ----> (MIXED)
|
|
|
|
// (ALL_UNSET) -- [01] -> (MIXED)
|
|
|
|
// (ALL_UNSET) -- _ ----> (ALL_UNSET)
|
|
|
|
// (MIXED) ------ . ----> (MIXED)
|
|
|
|
// (FILTERED)---- . ----> (FILTERED)
|
|
|
|
|
2011-07-19 21:06:00 +00:00
|
|
|
std::vector<bitAttr_t> bitAttrs;
|
2011-02-18 21:51:29 +00:00
|
|
|
|
|
|
|
// FILTERED bit positions provide no entropy and are not worthy of pursuing.
|
|
|
|
// Filter::recurse() set either BIT_TRUE or BIT_FALSE for each position.
|
2011-07-19 21:06:00 +00:00
|
|
|
for (BitIndex = 0; BitIndex < BitWidth; ++BitIndex)
|
2011-02-18 21:51:29 +00:00
|
|
|
if (FilterBitValues[BitIndex] == BIT_TRUE ||
|
|
|
|
FilterBitValues[BitIndex] == BIT_FALSE)
|
2011-07-19 21:06:00 +00:00
|
|
|
bitAttrs.push_back(ATTR_FILTERED);
|
2011-02-18 21:51:29 +00:00
|
|
|
else
|
2011-07-19 21:06:00 +00:00
|
|
|
bitAttrs.push_back(ATTR_NONE);
|
2011-02-18 21:51:29 +00:00
|
|
|
|
2012-08-17 05:42:16 +00:00
|
|
|
for (unsigned InsnIndex = 0; InsnIndex < numInstructions; ++InsnIndex) {
|
2011-02-18 21:51:29 +00:00
|
|
|
insn_t insn;
|
|
|
|
|
|
|
|
insnWithID(insn, Opcodes[InsnIndex]);
|
|
|
|
|
2011-07-19 21:06:00 +00:00
|
|
|
for (BitIndex = 0; BitIndex < BitWidth; ++BitIndex) {
|
2011-02-18 21:51:29 +00:00
|
|
|
switch (bitAttrs[BitIndex]) {
|
|
|
|
case ATTR_NONE:
|
|
|
|
if (insn[BitIndex] == BIT_UNSET)
|
|
|
|
bitAttrs[BitIndex] = ATTR_ALL_UNSET;
|
|
|
|
else
|
|
|
|
bitAttrs[BitIndex] = ATTR_ALL_SET;
|
|
|
|
break;
|
|
|
|
case ATTR_ALL_SET:
|
|
|
|
if (insn[BitIndex] == BIT_UNSET)
|
|
|
|
bitAttrs[BitIndex] = ATTR_MIXED;
|
|
|
|
break;
|
|
|
|
case ATTR_ALL_UNSET:
|
|
|
|
if (insn[BitIndex] != BIT_UNSET)
|
|
|
|
bitAttrs[BitIndex] = ATTR_MIXED;
|
|
|
|
break;
|
|
|
|
case ATTR_MIXED:
|
|
|
|
case ATTR_FILTERED:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The regionAttr automaton consumes the bitAttrs automatons' state,
|
|
|
|
// lowest-to-highest.
|
|
|
|
//
|
|
|
|
// Input symbols: F(iltered), (all_)S(et), (all_)U(nset), M(ixed)
|
|
|
|
// States: NONE, ALL_SET, MIXED
|
|
|
|
// Initial state: NONE
|
|
|
|
//
|
|
|
|
// (NONE) ----- F --> (NONE)
|
|
|
|
// (NONE) ----- S --> (ALL_SET) ; and set region start
|
|
|
|
// (NONE) ----- U --> (NONE)
|
|
|
|
// (NONE) ----- M --> (MIXED) ; and set region start
|
|
|
|
// (ALL_SET) -- F --> (NONE) ; and report an ALL_SET region
|
|
|
|
// (ALL_SET) -- S --> (ALL_SET)
|
|
|
|
// (ALL_SET) -- U --> (NONE) ; and report an ALL_SET region
|
|
|
|
// (ALL_SET) -- M --> (MIXED) ; and report an ALL_SET region
|
|
|
|
// (MIXED) ---- F --> (NONE) ; and report a MIXED region
|
|
|
|
// (MIXED) ---- S --> (ALL_SET) ; and report a MIXED region
|
|
|
|
// (MIXED) ---- U --> (NONE) ; and report a MIXED region
|
|
|
|
// (MIXED) ---- M --> (MIXED)
|
|
|
|
|
|
|
|
bitAttr_t RA = ATTR_NONE;
|
|
|
|
unsigned StartBit = 0;
|
|
|
|
|
2012-08-17 05:42:16 +00:00
|
|
|
for (BitIndex = 0; BitIndex < BitWidth; ++BitIndex) {
|
2011-02-18 21:51:29 +00:00
|
|
|
bitAttr_t bitAttr = bitAttrs[BitIndex];
|
|
|
|
|
|
|
|
assert(bitAttr != ATTR_NONE && "Bit without attributes");
|
|
|
|
|
|
|
|
switch (RA) {
|
|
|
|
case ATTR_NONE:
|
|
|
|
switch (bitAttr) {
|
|
|
|
case ATTR_FILTERED:
|
|
|
|
break;
|
|
|
|
case ATTR_ALL_SET:
|
|
|
|
StartBit = BitIndex;
|
|
|
|
RA = ATTR_ALL_SET;
|
|
|
|
break;
|
|
|
|
case ATTR_ALL_UNSET:
|
|
|
|
break;
|
|
|
|
case ATTR_MIXED:
|
|
|
|
StartBit = BitIndex;
|
|
|
|
RA = ATTR_MIXED;
|
|
|
|
break;
|
|
|
|
default:
|
2012-02-05 07:21:30 +00:00
|
|
|
llvm_unreachable("Unexpected bitAttr!");
|
2011-02-18 21:51:29 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ATTR_ALL_SET:
|
|
|
|
switch (bitAttr) {
|
|
|
|
case ATTR_FILTERED:
|
|
|
|
reportRegion(RA, StartBit, BitIndex, AllowMixed);
|
|
|
|
RA = ATTR_NONE;
|
|
|
|
break;
|
|
|
|
case ATTR_ALL_SET:
|
|
|
|
break;
|
|
|
|
case ATTR_ALL_UNSET:
|
|
|
|
reportRegion(RA, StartBit, BitIndex, AllowMixed);
|
|
|
|
RA = ATTR_NONE;
|
|
|
|
break;
|
|
|
|
case ATTR_MIXED:
|
|
|
|
reportRegion(RA, StartBit, BitIndex, AllowMixed);
|
|
|
|
StartBit = BitIndex;
|
|
|
|
RA = ATTR_MIXED;
|
|
|
|
break;
|
|
|
|
default:
|
2012-02-05 07:21:30 +00:00
|
|
|
llvm_unreachable("Unexpected bitAttr!");
|
2011-02-18 21:51:29 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ATTR_MIXED:
|
|
|
|
switch (bitAttr) {
|
|
|
|
case ATTR_FILTERED:
|
|
|
|
reportRegion(RA, StartBit, BitIndex, AllowMixed);
|
|
|
|
StartBit = BitIndex;
|
|
|
|
RA = ATTR_NONE;
|
|
|
|
break;
|
|
|
|
case ATTR_ALL_SET:
|
|
|
|
reportRegion(RA, StartBit, BitIndex, AllowMixed);
|
|
|
|
StartBit = BitIndex;
|
|
|
|
RA = ATTR_ALL_SET;
|
|
|
|
break;
|
|
|
|
case ATTR_ALL_UNSET:
|
|
|
|
reportRegion(RA, StartBit, BitIndex, AllowMixed);
|
|
|
|
RA = ATTR_NONE;
|
|
|
|
break;
|
|
|
|
case ATTR_MIXED:
|
|
|
|
break;
|
|
|
|
default:
|
2012-02-05 07:21:30 +00:00
|
|
|
llvm_unreachable("Unexpected bitAttr!");
|
2011-02-18 21:51:29 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ATTR_ALL_UNSET:
|
2012-02-05 07:21:30 +00:00
|
|
|
llvm_unreachable("regionAttr state machine has no ATTR_UNSET state");
|
2011-02-18 21:51:29 +00:00
|
|
|
case ATTR_FILTERED:
|
2012-02-05 07:21:30 +00:00
|
|
|
llvm_unreachable("regionAttr state machine has no ATTR_FILTERED state");
|
2011-02-18 21:51:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// At the end, if we're still in ALL_SET or MIXED states, report a region
|
|
|
|
switch (RA) {
|
|
|
|
case ATTR_NONE:
|
|
|
|
break;
|
|
|
|
case ATTR_FILTERED:
|
|
|
|
break;
|
|
|
|
case ATTR_ALL_SET:
|
|
|
|
reportRegion(RA, StartBit, BitIndex, AllowMixed);
|
|
|
|
break;
|
|
|
|
case ATTR_ALL_UNSET:
|
|
|
|
break;
|
|
|
|
case ATTR_MIXED:
|
|
|
|
reportRegion(RA, StartBit, BitIndex, AllowMixed);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have finished with the filter processings. Now it's time to choose
|
|
|
|
// the best performing filter.
|
|
|
|
BestIndex = 0;
|
|
|
|
bool AllUseless = true;
|
|
|
|
unsigned BestScore = 0;
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = Filters.size(); i != e; ++i) {
|
|
|
|
unsigned Usefulness = Filters[i].usefulness();
|
|
|
|
|
|
|
|
if (Usefulness)
|
|
|
|
AllUseless = false;
|
|
|
|
|
|
|
|
if (Usefulness > BestScore) {
|
|
|
|
BestIndex = i;
|
|
|
|
BestScore = Usefulness;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!AllUseless)
|
|
|
|
bestFilter().recurse();
|
|
|
|
|
|
|
|
return !AllUseless;
|
|
|
|
} // end of FilterChooser::filterProcessor(bool)
|
|
|
|
|
|
|
|
// Decides on the best configuration of filter(s) to use in order to decode
|
|
|
|
// the instructions. A conflict of instructions may occur, in which case we
|
|
|
|
// dump the conflict set to the standard error.
|
|
|
|
void FilterChooser::doFilter() {
|
|
|
|
unsigned Num = Opcodes.size();
|
|
|
|
assert(Num && "FilterChooser created with no instructions");
|
|
|
|
|
|
|
|
// Try regions of consecutive known bit values first.
|
|
|
|
if (filterProcessor(false))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Then regions of mixed bits (both known and unitialized bit values allowed).
|
|
|
|
if (filterProcessor(true))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Heuristics to cope with conflict set {t2CMPrs, t2SUBSrr, t2SUBSrs} where
|
|
|
|
// no single instruction for the maximum ATTR_MIXED region Inst{14-4} has a
|
|
|
|
// well-known encoding pattern. In such case, we backtrack and scan for the
|
|
|
|
// the very first consecutive ATTR_ALL_SET region and assign a filter to it.
|
|
|
|
if (Num == 3 && filterProcessor(true, false))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// If we come to here, the instruction decoding has failed.
|
|
|
|
// Set the BestIndex to -1 to indicate so.
|
|
|
|
BestIndex = -1;
|
|
|
|
}
|
|
|
|
|
2012-08-14 19:06:05 +00:00
|
|
|
// emitTableEntries - Emit state machine entries to decode our share of
|
|
|
|
// instructions.
|
|
|
|
void FilterChooser::emitTableEntries(DecoderTableInfo &TableInfo) const {
|
|
|
|
if (Opcodes.size() == 1) {
|
2011-02-18 21:51:29 +00:00
|
|
|
// There is only one instruction in the set, which is great!
|
|
|
|
// Call emitSingletonDecoder() to see whether there are any remaining
|
|
|
|
// encodings bits.
|
2012-08-14 19:06:05 +00:00
|
|
|
emitSingletonTableEntry(TableInfo, Opcodes[0]);
|
|
|
|
return;
|
|
|
|
}
|
2011-02-18 21:51:29 +00:00
|
|
|
|
|
|
|
// Choose the best filter to do the decodings!
|
|
|
|
if (BestIndex != -1) {
|
2012-03-16 05:58:09 +00:00
|
|
|
const Filter &Best = Filters[BestIndex];
|
2011-02-18 21:51:29 +00:00
|
|
|
if (Best.getNumFiltered() == 1)
|
2012-08-14 19:06:05 +00:00
|
|
|
emitSingletonTableEntry(TableInfo, Best);
|
2011-02-18 21:51:29 +00:00
|
|
|
else
|
2012-08-14 19:06:05 +00:00
|
|
|
Best.emitTableEntry(TableInfo);
|
|
|
|
return;
|
2011-02-18 21:51:29 +00:00
|
|
|
}
|
|
|
|
|
2012-08-14 19:06:05 +00:00
|
|
|
// We don't know how to decode these instructions! Dump the
|
|
|
|
// conflict set and bail.
|
2011-02-18 21:51:29 +00:00
|
|
|
|
|
|
|
// Print out useful conflict information for postmortem analysis.
|
|
|
|
errs() << "Decoding Conflict:\n";
|
|
|
|
|
|
|
|
dumpStack(errs(), "\t\t");
|
|
|
|
|
2012-03-16 01:19:24 +00:00
|
|
|
for (unsigned i = 0; i < Opcodes.size(); ++i) {
|
2011-02-18 21:51:29 +00:00
|
|
|
const std::string &Name = nameWithID(Opcodes[i]);
|
|
|
|
|
|
|
|
errs() << '\t' << Name << " ";
|
|
|
|
dumpBits(errs(),
|
|
|
|
getBitsField(*AllInstructions[Opcodes[i]]->TheDef, "Inst"));
|
|
|
|
errs() << '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Add support for positionally-encoded operands to FixedLenDecoderEmitter
Unfortunately, the PowerPC instruction definitions make heavy use of the
positional operand encoding heuristic to map operands onto bitfield variables
in the instruction definitions. Changing this to use name-based mapping is not
trivial, however, because additional infrastructure needs to be designed to
handle mapping of complex operands (with multiple suboperands) onto multiple
bitfield variables.
In the mean time, this adds support for positionally encoded operands to
FixedLenDecoderEmitter, so that we can generate a disassembler for the PowerPC
backend. To prevent an accidental reliance on this feature, and to prevent an
undesirable interaction with existing disassemblers, a backend must opt-in to
this support by setting the new decodePositionallyEncodedOperands
instruction-set bit to true.
When enabled, this iterates the variables that contribute to the instruction
encoding, just as the encoder does, and emulates the procedure the encoder uses
to map "numbered" operands to variables. The bit range for each variable is
also determined as the encoder determines them. This map is then consulted
during the decoder-generator's loop over operands to decode, allowing the
decoder to understand both position-based and name-based operand-to-variable
mappings.
As noted in the comment on the decodePositionallyEncodedOperands definition,
this support should be removed once it is no longer needed. There should be no
change to existing disassemblers.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@197691 91177308-0d34-0410-b5e6-96231b3b80d8
2013-12-19 16:12:53 +00:00
|
|
|
static bool populateInstruction(CodeGenTarget &Target,
|
|
|
|
const CodeGenInstruction &CGI, unsigned Opc,
|
2012-03-16 01:19:24 +00:00
|
|
|
std::map<unsigned, std::vector<OperandInfo> > &Operands){
|
2011-02-18 21:51:29 +00:00
|
|
|
const Record &Def = *CGI.TheDef;
|
|
|
|
// If all the bit positions are not specified; do not decode this instruction.
|
|
|
|
// We are bound to fail! For proper disassembly, the well-known encoding bits
|
|
|
|
// of the instruction must be fully specified.
|
|
|
|
|
2011-07-29 22:43:06 +00:00
|
|
|
BitsInit &Bits = getBitsField(Def, "Inst");
|
2011-07-06 21:33:38 +00:00
|
|
|
if (Bits.allInComplete()) return false;
|
|
|
|
|
2011-02-18 21:51:29 +00:00
|
|
|
std::vector<OperandInfo> InsnOperands;
|
|
|
|
|
|
|
|
// If the instruction has specified a custom decoding hook, use that instead
|
|
|
|
// of trying to auto-generate the decoder.
|
|
|
|
std::string InstDecoder = Def.getValueAsString("DecoderMethod");
|
|
|
|
if (InstDecoder != "") {
|
2011-07-28 21:54:31 +00:00
|
|
|
InsnOperands.push_back(OperandInfo(InstDecoder));
|
2011-02-18 21:51:29 +00:00
|
|
|
Operands[Opc] = InsnOperands;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate a description of the operand of the instruction that we know
|
|
|
|
// how to decode automatically.
|
|
|
|
// FIXME: We'll need to have a way to manually override this as needed.
|
|
|
|
|
|
|
|
// Gather the outputs/inputs of the instruction, so we can find their
|
|
|
|
// positions in the encoding. This assumes for now that they appear in the
|
|
|
|
// MCInst in the order that they're listed.
|
2011-07-29 22:43:06 +00:00
|
|
|
std::vector<std::pair<Init*, std::string> > InOutOperands;
|
|
|
|
DagInit *Out = Def.getValueAsDag("OutOperandList");
|
|
|
|
DagInit *In = Def.getValueAsDag("InOperandList");
|
2011-02-18 21:51:29 +00:00
|
|
|
for (unsigned i = 0; i < Out->getNumArgs(); ++i)
|
|
|
|
InOutOperands.push_back(std::make_pair(Out->getArg(i), Out->getArgName(i)));
|
|
|
|
for (unsigned i = 0; i < In->getNumArgs(); ++i)
|
|
|
|
InOutOperands.push_back(std::make_pair(In->getArg(i), In->getArgName(i)));
|
|
|
|
|
2011-07-28 23:56:20 +00:00
|
|
|
// Search for tied operands, so that we can correctly instantiate
|
|
|
|
// operands that are not explicitly represented in the encoding.
|
2011-07-29 18:28:52 +00:00
|
|
|
std::map<std::string, std::string> TiedNames;
|
2011-07-28 23:56:20 +00:00
|
|
|
for (unsigned i = 0; i < CGI.Operands.size(); ++i) {
|
|
|
|
int tiedTo = CGI.Operands[i].getTiedRegister();
|
2011-07-29 18:28:52 +00:00
|
|
|
if (tiedTo != -1) {
|
Add support for positionally-encoded operands to FixedLenDecoderEmitter
Unfortunately, the PowerPC instruction definitions make heavy use of the
positional operand encoding heuristic to map operands onto bitfield variables
in the instruction definitions. Changing this to use name-based mapping is not
trivial, however, because additional infrastructure needs to be designed to
handle mapping of complex operands (with multiple suboperands) onto multiple
bitfield variables.
In the mean time, this adds support for positionally encoded operands to
FixedLenDecoderEmitter, so that we can generate a disassembler for the PowerPC
backend. To prevent an accidental reliance on this feature, and to prevent an
undesirable interaction with existing disassemblers, a backend must opt-in to
this support by setting the new decodePositionallyEncodedOperands
instruction-set bit to true.
When enabled, this iterates the variables that contribute to the instruction
encoding, just as the encoder does, and emulates the procedure the encoder uses
to map "numbered" operands to variables. The bit range for each variable is
also determined as the encoder determines them. This map is then consulted
during the decoder-generator's loop over operands to decode, allowing the
decoder to understand both position-based and name-based operand-to-variable
mappings.
As noted in the comment on the decodePositionallyEncodedOperands definition,
this support should be removed once it is no longer needed. There should be no
change to existing disassemblers.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@197691 91177308-0d34-0410-b5e6-96231b3b80d8
2013-12-19 16:12:53 +00:00
|
|
|
std::pair<unsigned, unsigned> SO =
|
|
|
|
CGI.Operands.getSubOperandNumber(tiedTo);
|
|
|
|
TiedNames[InOutOperands[i].second] = InOutOperands[SO.first].second;
|
|
|
|
TiedNames[InOutOperands[SO.first].second] = InOutOperands[i].second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::map<std::string, std::vector<OperandInfo> > NumberedInsnOperands;
|
|
|
|
std::set<std::string> NumberedInsnOperandsNoTie;
|
|
|
|
if (Target.getInstructionSet()->
|
|
|
|
getValueAsBit("decodePositionallyEncodedOperands")) {
|
|
|
|
const std::vector<RecordVal> &Vals = Def.getValues();
|
|
|
|
unsigned NumberedOp = 0;
|
|
|
|
|
[TableGen] Optionally forbid overlap between named and positional operands
There are currently two schemes for mapping instruction operands to
instruction-format variables for generating the instruction encoders and
decoders for the assembler and disassembler respectively: a) to map by name and
b) to map by position.
In the long run, we'd like to remove the position-based scheme and use only
name-based mapping. Unfortunately, the name-based scheme currently cannot deal
with complex operands (those with suboperands), and so we currently must use
the position-based scheme for those. On the other hand, the position-based
scheme cannot deal with (register) variables that are split into multiple
ranges. An upcoming commit to the PowerPC backend (adding VSX support) will
require this capability. While we could teach the position-based scheme to
handle that, since we'd like to move away from the position-based mapping
generally, it seems silly to teach it new tricks now. What makes more sense is
to allow for partial transitioning: use the name-based mapping when possible,
and only use the position-based scheme when necessary.
Now the problem is that mixing the two sensibly was not possible: the
position-based mapping would map based on position, but would not skip those
variables that were mapped by name. Instead, the two sets of assignments would
overlap. However, I cannot currently change the current behavior, because there
are some backends that rely on it [I think mistakenly, but I'll send a message
to llvmdev about that]. So I've added a new TableGen bit variable:
noNamedPositionallyEncodedOperands, that can be used to cause the
position-based mapping to skip variables mapped by name.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@203767 91177308-0d34-0410-b5e6-96231b3b80d8
2014-03-13 07:57:54 +00:00
|
|
|
std::set<unsigned> NamedOpIndices;
|
|
|
|
if (Target.getInstructionSet()->
|
|
|
|
getValueAsBit("noNamedPositionallyEncodedOperands"))
|
|
|
|
// Collect the set of operand indices that might correspond to named
|
|
|
|
// operand, and skip these when assigning operands based on position.
|
|
|
|
for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
|
|
|
|
unsigned OpIdx;
|
|
|
|
if (!CGI.Operands.hasOperandNamed(Vals[i].getName(), OpIdx))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
NamedOpIndices.insert(OpIdx);
|
|
|
|
}
|
|
|
|
|
Add support for positionally-encoded operands to FixedLenDecoderEmitter
Unfortunately, the PowerPC instruction definitions make heavy use of the
positional operand encoding heuristic to map operands onto bitfield variables
in the instruction definitions. Changing this to use name-based mapping is not
trivial, however, because additional infrastructure needs to be designed to
handle mapping of complex operands (with multiple suboperands) onto multiple
bitfield variables.
In the mean time, this adds support for positionally encoded operands to
FixedLenDecoderEmitter, so that we can generate a disassembler for the PowerPC
backend. To prevent an accidental reliance on this feature, and to prevent an
undesirable interaction with existing disassemblers, a backend must opt-in to
this support by setting the new decodePositionallyEncodedOperands
instruction-set bit to true.
When enabled, this iterates the variables that contribute to the instruction
encoding, just as the encoder does, and emulates the procedure the encoder uses
to map "numbered" operands to variables. The bit range for each variable is
also determined as the encoder determines them. This map is then consulted
during the decoder-generator's loop over operands to decode, allowing the
decoder to understand both position-based and name-based operand-to-variable
mappings.
As noted in the comment on the decodePositionallyEncodedOperands definition,
this support should be removed once it is no longer needed. There should be no
change to existing disassemblers.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@197691 91177308-0d34-0410-b5e6-96231b3b80d8
2013-12-19 16:12:53 +00:00
|
|
|
for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
|
|
|
|
// Ignore fixed fields in the record, we're looking for values like:
|
|
|
|
// bits<5> RST = { ?, ?, ?, ?, ? };
|
|
|
|
if (Vals[i].getPrefix() || Vals[i].getValue()->isComplete())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Determine if Vals[i] actually contributes to the Inst encoding.
|
|
|
|
unsigned bi = 0;
|
|
|
|
for (; bi < Bits.getNumBits(); ++bi) {
|
2014-04-15 07:20:03 +00:00
|
|
|
VarInit *Var = nullptr;
|
Add support for positionally-encoded operands to FixedLenDecoderEmitter
Unfortunately, the PowerPC instruction definitions make heavy use of the
positional operand encoding heuristic to map operands onto bitfield variables
in the instruction definitions. Changing this to use name-based mapping is not
trivial, however, because additional infrastructure needs to be designed to
handle mapping of complex operands (with multiple suboperands) onto multiple
bitfield variables.
In the mean time, this adds support for positionally encoded operands to
FixedLenDecoderEmitter, so that we can generate a disassembler for the PowerPC
backend. To prevent an accidental reliance on this feature, and to prevent an
undesirable interaction with existing disassemblers, a backend must opt-in to
this support by setting the new decodePositionallyEncodedOperands
instruction-set bit to true.
When enabled, this iterates the variables that contribute to the instruction
encoding, just as the encoder does, and emulates the procedure the encoder uses
to map "numbered" operands to variables. The bit range for each variable is
also determined as the encoder determines them. This map is then consulted
during the decoder-generator's loop over operands to decode, allowing the
decoder to understand both position-based and name-based operand-to-variable
mappings.
As noted in the comment on the decodePositionallyEncodedOperands definition,
this support should be removed once it is no longer needed. There should be no
change to existing disassemblers.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@197691 91177308-0d34-0410-b5e6-96231b3b80d8
2013-12-19 16:12:53 +00:00
|
|
|
VarBitInit *BI = dyn_cast<VarBitInit>(Bits.getBit(bi));
|
|
|
|
if (BI)
|
|
|
|
Var = dyn_cast<VarInit>(BI->getBitVar());
|
|
|
|
else
|
|
|
|
Var = dyn_cast<VarInit>(Bits.getBit(bi));
|
|
|
|
|
|
|
|
if (Var && Var->getName() == Vals[i].getName())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bi == Bits.getNumBits())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Skip variables that correspond to explicitly-named operands.
|
|
|
|
unsigned OpIdx;
|
|
|
|
if (CGI.Operands.hasOperandNamed(Vals[i].getName(), OpIdx))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Get the bit range for this operand:
|
|
|
|
unsigned bitStart = bi++, bitWidth = 1;
|
|
|
|
for (; bi < Bits.getNumBits(); ++bi) {
|
2014-04-15 07:20:03 +00:00
|
|
|
VarInit *Var = nullptr;
|
Add support for positionally-encoded operands to FixedLenDecoderEmitter
Unfortunately, the PowerPC instruction definitions make heavy use of the
positional operand encoding heuristic to map operands onto bitfield variables
in the instruction definitions. Changing this to use name-based mapping is not
trivial, however, because additional infrastructure needs to be designed to
handle mapping of complex operands (with multiple suboperands) onto multiple
bitfield variables.
In the mean time, this adds support for positionally encoded operands to
FixedLenDecoderEmitter, so that we can generate a disassembler for the PowerPC
backend. To prevent an accidental reliance on this feature, and to prevent an
undesirable interaction with existing disassemblers, a backend must opt-in to
this support by setting the new decodePositionallyEncodedOperands
instruction-set bit to true.
When enabled, this iterates the variables that contribute to the instruction
encoding, just as the encoder does, and emulates the procedure the encoder uses
to map "numbered" operands to variables. The bit range for each variable is
also determined as the encoder determines them. This map is then consulted
during the decoder-generator's loop over operands to decode, allowing the
decoder to understand both position-based and name-based operand-to-variable
mappings.
As noted in the comment on the decodePositionallyEncodedOperands definition,
this support should be removed once it is no longer needed. There should be no
change to existing disassemblers.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@197691 91177308-0d34-0410-b5e6-96231b3b80d8
2013-12-19 16:12:53 +00:00
|
|
|
VarBitInit *BI = dyn_cast<VarBitInit>(Bits.getBit(bi));
|
|
|
|
if (BI)
|
|
|
|
Var = dyn_cast<VarInit>(BI->getBitVar());
|
|
|
|
else
|
|
|
|
Var = dyn_cast<VarInit>(Bits.getBit(bi));
|
|
|
|
|
|
|
|
if (!Var)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (Var->getName() != Vals[i].getName())
|
|
|
|
break;
|
|
|
|
|
|
|
|
++bitWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned NumberOps = CGI.Operands.size();
|
|
|
|
while (NumberedOp < NumberOps &&
|
[TableGen] Optionally forbid overlap between named and positional operands
There are currently two schemes for mapping instruction operands to
instruction-format variables for generating the instruction encoders and
decoders for the assembler and disassembler respectively: a) to map by name and
b) to map by position.
In the long run, we'd like to remove the position-based scheme and use only
name-based mapping. Unfortunately, the name-based scheme currently cannot deal
with complex operands (those with suboperands), and so we currently must use
the position-based scheme for those. On the other hand, the position-based
scheme cannot deal with (register) variables that are split into multiple
ranges. An upcoming commit to the PowerPC backend (adding VSX support) will
require this capability. While we could teach the position-based scheme to
handle that, since we'd like to move away from the position-based mapping
generally, it seems silly to teach it new tricks now. What makes more sense is
to allow for partial transitioning: use the name-based mapping when possible,
and only use the position-based scheme when necessary.
Now the problem is that mixing the two sensibly was not possible: the
position-based mapping would map based on position, but would not skip those
variables that were mapped by name. Instead, the two sets of assignments would
overlap. However, I cannot currently change the current behavior, because there
are some backends that rely on it [I think mistakenly, but I'll send a message
to llvmdev about that]. So I've added a new TableGen bit variable:
noNamedPositionallyEncodedOperands, that can be used to cause the
position-based mapping to skip variables mapped by name.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@203767 91177308-0d34-0410-b5e6-96231b3b80d8
2014-03-13 07:57:54 +00:00
|
|
|
(CGI.Operands.isFlatOperandNotEmitted(NumberedOp) ||
|
2015-01-15 11:41:30 +00:00
|
|
|
(!NamedOpIndices.empty() && NamedOpIndices.count(
|
[TableGen] Optionally forbid overlap between named and positional operands
There are currently two schemes for mapping instruction operands to
instruction-format variables for generating the instruction encoders and
decoders for the assembler and disassembler respectively: a) to map by name and
b) to map by position.
In the long run, we'd like to remove the position-based scheme and use only
name-based mapping. Unfortunately, the name-based scheme currently cannot deal
with complex operands (those with suboperands), and so we currently must use
the position-based scheme for those. On the other hand, the position-based
scheme cannot deal with (register) variables that are split into multiple
ranges. An upcoming commit to the PowerPC backend (adding VSX support) will
require this capability. While we could teach the position-based scheme to
handle that, since we'd like to move away from the position-based mapping
generally, it seems silly to teach it new tricks now. What makes more sense is
to allow for partial transitioning: use the name-based mapping when possible,
and only use the position-based scheme when necessary.
Now the problem is that mixing the two sensibly was not possible: the
position-based mapping would map based on position, but would not skip those
variables that were mapped by name. Instead, the two sets of assignments would
overlap. However, I cannot currently change the current behavior, because there
are some backends that rely on it [I think mistakenly, but I'll send a message
to llvmdev about that]. So I've added a new TableGen bit variable:
noNamedPositionallyEncodedOperands, that can be used to cause the
position-based mapping to skip variables mapped by name.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@203767 91177308-0d34-0410-b5e6-96231b3b80d8
2014-03-13 07:57:54 +00:00
|
|
|
CGI.Operands.getSubOperandNumber(NumberedOp).first))))
|
Add support for positionally-encoded operands to FixedLenDecoderEmitter
Unfortunately, the PowerPC instruction definitions make heavy use of the
positional operand encoding heuristic to map operands onto bitfield variables
in the instruction definitions. Changing this to use name-based mapping is not
trivial, however, because additional infrastructure needs to be designed to
handle mapping of complex operands (with multiple suboperands) onto multiple
bitfield variables.
In the mean time, this adds support for positionally encoded operands to
FixedLenDecoderEmitter, so that we can generate a disassembler for the PowerPC
backend. To prevent an accidental reliance on this feature, and to prevent an
undesirable interaction with existing disassemblers, a backend must opt-in to
this support by setting the new decodePositionallyEncodedOperands
instruction-set bit to true.
When enabled, this iterates the variables that contribute to the instruction
encoding, just as the encoder does, and emulates the procedure the encoder uses
to map "numbered" operands to variables. The bit range for each variable is
also determined as the encoder determines them. This map is then consulted
during the decoder-generator's loop over operands to decode, allowing the
decoder to understand both position-based and name-based operand-to-variable
mappings.
As noted in the comment on the decodePositionallyEncodedOperands definition,
this support should be removed once it is no longer needed. There should be no
change to existing disassemblers.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@197691 91177308-0d34-0410-b5e6-96231b3b80d8
2013-12-19 16:12:53 +00:00
|
|
|
++NumberedOp;
|
|
|
|
|
|
|
|
OpIdx = NumberedOp++;
|
|
|
|
|
|
|
|
// OpIdx now holds the ordered operand number of Vals[i].
|
|
|
|
std::pair<unsigned, unsigned> SO =
|
|
|
|
CGI.Operands.getSubOperandNumber(OpIdx);
|
|
|
|
const std::string &Name = CGI.Operands[SO.first].Name;
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "Numbered operand mapping for " << Def.getName() << ": " <<
|
|
|
|
Name << "(" << SO.first << ", " << SO.second << ") => " <<
|
|
|
|
Vals[i].getName() << "\n");
|
|
|
|
|
|
|
|
std::string Decoder = "";
|
|
|
|
Record *TypeRecord = CGI.Operands[SO.first].Rec;
|
|
|
|
|
|
|
|
RecordVal *DecoderString = TypeRecord->getValue("DecoderMethod");
|
|
|
|
StringInit *String = DecoderString ?
|
2014-04-15 07:20:03 +00:00
|
|
|
dyn_cast<StringInit>(DecoderString->getValue()) : nullptr;
|
Add support for positionally-encoded operands to FixedLenDecoderEmitter
Unfortunately, the PowerPC instruction definitions make heavy use of the
positional operand encoding heuristic to map operands onto bitfield variables
in the instruction definitions. Changing this to use name-based mapping is not
trivial, however, because additional infrastructure needs to be designed to
handle mapping of complex operands (with multiple suboperands) onto multiple
bitfield variables.
In the mean time, this adds support for positionally encoded operands to
FixedLenDecoderEmitter, so that we can generate a disassembler for the PowerPC
backend. To prevent an accidental reliance on this feature, and to prevent an
undesirable interaction with existing disassemblers, a backend must opt-in to
this support by setting the new decodePositionallyEncodedOperands
instruction-set bit to true.
When enabled, this iterates the variables that contribute to the instruction
encoding, just as the encoder does, and emulates the procedure the encoder uses
to map "numbered" operands to variables. The bit range for each variable is
also determined as the encoder determines them. This map is then consulted
during the decoder-generator's loop over operands to decode, allowing the
decoder to understand both position-based and name-based operand-to-variable
mappings.
As noted in the comment on the decodePositionallyEncodedOperands definition,
this support should be removed once it is no longer needed. There should be no
change to existing disassemblers.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@197691 91177308-0d34-0410-b5e6-96231b3b80d8
2013-12-19 16:12:53 +00:00
|
|
|
if (String && String->getValue() != "")
|
|
|
|
Decoder = String->getValue();
|
|
|
|
|
|
|
|
if (Decoder == "" &&
|
|
|
|
CGI.Operands[SO.first].MIOperandInfo &&
|
|
|
|
CGI.Operands[SO.first].MIOperandInfo->getNumArgs()) {
|
|
|
|
Init *Arg = CGI.Operands[SO.first].MIOperandInfo->
|
|
|
|
getArg(SO.second);
|
|
|
|
if (TypedInit *TI = cast<TypedInit>(Arg)) {
|
|
|
|
RecordRecTy *Type = cast<RecordRecTy>(TI->getType());
|
|
|
|
TypeRecord = Type->getRecord();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isReg = false;
|
|
|
|
if (TypeRecord->isSubClassOf("RegisterOperand"))
|
|
|
|
TypeRecord = TypeRecord->getValueAsDef("RegClass");
|
|
|
|
if (TypeRecord->isSubClassOf("RegisterClass")) {
|
|
|
|
Decoder = "Decode" + TypeRecord->getName() + "RegisterClass";
|
|
|
|
isReg = true;
|
|
|
|
} else if (TypeRecord->isSubClassOf("PointerLikeRegClass")) {
|
|
|
|
Decoder = "DecodePointerLikeRegClass" +
|
|
|
|
utostr(TypeRecord->getValueAsInt("RegClassKind"));
|
|
|
|
isReg = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
DecoderString = TypeRecord->getValue("DecoderMethod");
|
|
|
|
String = DecoderString ?
|
2014-04-15 07:20:03 +00:00
|
|
|
dyn_cast<StringInit>(DecoderString->getValue()) : nullptr;
|
Add support for positionally-encoded operands to FixedLenDecoderEmitter
Unfortunately, the PowerPC instruction definitions make heavy use of the
positional operand encoding heuristic to map operands onto bitfield variables
in the instruction definitions. Changing this to use name-based mapping is not
trivial, however, because additional infrastructure needs to be designed to
handle mapping of complex operands (with multiple suboperands) onto multiple
bitfield variables.
In the mean time, this adds support for positionally encoded operands to
FixedLenDecoderEmitter, so that we can generate a disassembler for the PowerPC
backend. To prevent an accidental reliance on this feature, and to prevent an
undesirable interaction with existing disassemblers, a backend must opt-in to
this support by setting the new decodePositionallyEncodedOperands
instruction-set bit to true.
When enabled, this iterates the variables that contribute to the instruction
encoding, just as the encoder does, and emulates the procedure the encoder uses
to map "numbered" operands to variables. The bit range for each variable is
also determined as the encoder determines them. This map is then consulted
during the decoder-generator's loop over operands to decode, allowing the
decoder to understand both position-based and name-based operand-to-variable
mappings.
As noted in the comment on the decodePositionallyEncodedOperands definition,
this support should be removed once it is no longer needed. There should be no
change to existing disassemblers.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@197691 91177308-0d34-0410-b5e6-96231b3b80d8
2013-12-19 16:12:53 +00:00
|
|
|
if (!isReg && String && String->getValue() != "")
|
|
|
|
Decoder = String->getValue();
|
|
|
|
|
|
|
|
OperandInfo OpInfo(Decoder);
|
|
|
|
OpInfo.addField(bitStart, bitWidth, 0);
|
|
|
|
|
|
|
|
NumberedInsnOperands[Name].push_back(OpInfo);
|
|
|
|
|
|
|
|
// FIXME: For complex operands with custom decoders we can't handle tied
|
|
|
|
// sub-operands automatically. Skip those here and assume that this is
|
|
|
|
// fixed up elsewhere.
|
|
|
|
if (CGI.Operands[SO.first].MIOperandInfo &&
|
|
|
|
CGI.Operands[SO.first].MIOperandInfo->getNumArgs() > 1 &&
|
|
|
|
String && String->getValue() != "")
|
|
|
|
NumberedInsnOperandsNoTie.insert(Name);
|
2011-07-29 18:28:52 +00:00
|
|
|
}
|
2011-07-28 23:56:20 +00:00
|
|
|
}
|
|
|
|
|
2011-02-18 21:51:29 +00:00
|
|
|
// For each operand, see if we can figure out where it is encoded.
|
2014-12-13 05:12:19 +00:00
|
|
|
for (const auto &Op : InOutOperands) {
|
|
|
|
if (!NumberedInsnOperands[Op.second].empty()) {
|
Add support for positionally-encoded operands to FixedLenDecoderEmitter
Unfortunately, the PowerPC instruction definitions make heavy use of the
positional operand encoding heuristic to map operands onto bitfield variables
in the instruction definitions. Changing this to use name-based mapping is not
trivial, however, because additional infrastructure needs to be designed to
handle mapping of complex operands (with multiple suboperands) onto multiple
bitfield variables.
In the mean time, this adds support for positionally encoded operands to
FixedLenDecoderEmitter, so that we can generate a disassembler for the PowerPC
backend. To prevent an accidental reliance on this feature, and to prevent an
undesirable interaction with existing disassemblers, a backend must opt-in to
this support by setting the new decodePositionallyEncodedOperands
instruction-set bit to true.
When enabled, this iterates the variables that contribute to the instruction
encoding, just as the encoder does, and emulates the procedure the encoder uses
to map "numbered" operands to variables. The bit range for each variable is
also determined as the encoder determines them. This map is then consulted
during the decoder-generator's loop over operands to decode, allowing the
decoder to understand both position-based and name-based operand-to-variable
mappings.
As noted in the comment on the decodePositionallyEncodedOperands definition,
this support should be removed once it is no longer needed. There should be no
change to existing disassemblers.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@197691 91177308-0d34-0410-b5e6-96231b3b80d8
2013-12-19 16:12:53 +00:00
|
|
|
InsnOperands.insert(InsnOperands.end(),
|
2014-12-13 05:12:19 +00:00
|
|
|
NumberedInsnOperands[Op.second].begin(),
|
|
|
|
NumberedInsnOperands[Op.second].end());
|
Add support for positionally-encoded operands to FixedLenDecoderEmitter
Unfortunately, the PowerPC instruction definitions make heavy use of the
positional operand encoding heuristic to map operands onto bitfield variables
in the instruction definitions. Changing this to use name-based mapping is not
trivial, however, because additional infrastructure needs to be designed to
handle mapping of complex operands (with multiple suboperands) onto multiple
bitfield variables.
In the mean time, this adds support for positionally encoded operands to
FixedLenDecoderEmitter, so that we can generate a disassembler for the PowerPC
backend. To prevent an accidental reliance on this feature, and to prevent an
undesirable interaction with existing disassemblers, a backend must opt-in to
this support by setting the new decodePositionallyEncodedOperands
instruction-set bit to true.
When enabled, this iterates the variables that contribute to the instruction
encoding, just as the encoder does, and emulates the procedure the encoder uses
to map "numbered" operands to variables. The bit range for each variable is
also determined as the encoder determines them. This map is then consulted
during the decoder-generator's loop over operands to decode, allowing the
decoder to understand both position-based and name-based operand-to-variable
mappings.
As noted in the comment on the decodePositionallyEncodedOperands definition,
this support should be removed once it is no longer needed. There should be no
change to existing disassemblers.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@197691 91177308-0d34-0410-b5e6-96231b3b80d8
2013-12-19 16:12:53 +00:00
|
|
|
continue;
|
2014-12-13 05:12:19 +00:00
|
|
|
}
|
|
|
|
if (!NumberedInsnOperands[TiedNames[Op.second]].empty()) {
|
|
|
|
if (!NumberedInsnOperandsNoTie.count(TiedNames[Op.second])) {
|
Add support for positionally-encoded operands to FixedLenDecoderEmitter
Unfortunately, the PowerPC instruction definitions make heavy use of the
positional operand encoding heuristic to map operands onto bitfield variables
in the instruction definitions. Changing this to use name-based mapping is not
trivial, however, because additional infrastructure needs to be designed to
handle mapping of complex operands (with multiple suboperands) onto multiple
bitfield variables.
In the mean time, this adds support for positionally encoded operands to
FixedLenDecoderEmitter, so that we can generate a disassembler for the PowerPC
backend. To prevent an accidental reliance on this feature, and to prevent an
undesirable interaction with existing disassemblers, a backend must opt-in to
this support by setting the new decodePositionallyEncodedOperands
instruction-set bit to true.
When enabled, this iterates the variables that contribute to the instruction
encoding, just as the encoder does, and emulates the procedure the encoder uses
to map "numbered" operands to variables. The bit range for each variable is
also determined as the encoder determines them. This map is then consulted
during the decoder-generator's loop over operands to decode, allowing the
decoder to understand both position-based and name-based operand-to-variable
mappings.
As noted in the comment on the decodePositionallyEncodedOperands definition,
this support should be removed once it is no longer needed. There should be no
change to existing disassemblers.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@197691 91177308-0d34-0410-b5e6-96231b3b80d8
2013-12-19 16:12:53 +00:00
|
|
|
// Figure out to which (sub)operand we're tied.
|
2014-12-13 05:12:19 +00:00
|
|
|
unsigned i = CGI.Operands.getOperandNamed(TiedNames[Op.second]);
|
Add support for positionally-encoded operands to FixedLenDecoderEmitter
Unfortunately, the PowerPC instruction definitions make heavy use of the
positional operand encoding heuristic to map operands onto bitfield variables
in the instruction definitions. Changing this to use name-based mapping is not
trivial, however, because additional infrastructure needs to be designed to
handle mapping of complex operands (with multiple suboperands) onto multiple
bitfield variables.
In the mean time, this adds support for positionally encoded operands to
FixedLenDecoderEmitter, so that we can generate a disassembler for the PowerPC
backend. To prevent an accidental reliance on this feature, and to prevent an
undesirable interaction with existing disassemblers, a backend must opt-in to
this support by setting the new decodePositionallyEncodedOperands
instruction-set bit to true.
When enabled, this iterates the variables that contribute to the instruction
encoding, just as the encoder does, and emulates the procedure the encoder uses
to map "numbered" operands to variables. The bit range for each variable is
also determined as the encoder determines them. This map is then consulted
during the decoder-generator's loop over operands to decode, allowing the
decoder to understand both position-based and name-based operand-to-variable
mappings.
As noted in the comment on the decodePositionallyEncodedOperands definition,
this support should be removed once it is no longer needed. There should be no
change to existing disassemblers.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@197691 91177308-0d34-0410-b5e6-96231b3b80d8
2013-12-19 16:12:53 +00:00
|
|
|
int tiedTo = CGI.Operands[i].getTiedRegister();
|
|
|
|
if (tiedTo == -1) {
|
2014-12-13 05:12:19 +00:00
|
|
|
i = CGI.Operands.getOperandNamed(Op.second);
|
Add support for positionally-encoded operands to FixedLenDecoderEmitter
Unfortunately, the PowerPC instruction definitions make heavy use of the
positional operand encoding heuristic to map operands onto bitfield variables
in the instruction definitions. Changing this to use name-based mapping is not
trivial, however, because additional infrastructure needs to be designed to
handle mapping of complex operands (with multiple suboperands) onto multiple
bitfield variables.
In the mean time, this adds support for positionally encoded operands to
FixedLenDecoderEmitter, so that we can generate a disassembler for the PowerPC
backend. To prevent an accidental reliance on this feature, and to prevent an
undesirable interaction with existing disassemblers, a backend must opt-in to
this support by setting the new decodePositionallyEncodedOperands
instruction-set bit to true.
When enabled, this iterates the variables that contribute to the instruction
encoding, just as the encoder does, and emulates the procedure the encoder uses
to map "numbered" operands to variables. The bit range for each variable is
also determined as the encoder determines them. This map is then consulted
during the decoder-generator's loop over operands to decode, allowing the
decoder to understand both position-based and name-based operand-to-variable
mappings.
As noted in the comment on the decodePositionallyEncodedOperands definition,
this support should be removed once it is no longer needed. There should be no
change to existing disassemblers.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@197691 91177308-0d34-0410-b5e6-96231b3b80d8
2013-12-19 16:12:53 +00:00
|
|
|
tiedTo = CGI.Operands[i].getTiedRegister();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tiedTo != -1) {
|
|
|
|
std::pair<unsigned, unsigned> SO =
|
|
|
|
CGI.Operands.getSubOperandNumber(tiedTo);
|
|
|
|
|
2014-12-13 05:12:19 +00:00
|
|
|
InsnOperands.push_back(NumberedInsnOperands[TiedNames[Op.second]]
|
Add support for positionally-encoded operands to FixedLenDecoderEmitter
Unfortunately, the PowerPC instruction definitions make heavy use of the
positional operand encoding heuristic to map operands onto bitfield variables
in the instruction definitions. Changing this to use name-based mapping is not
trivial, however, because additional infrastructure needs to be designed to
handle mapping of complex operands (with multiple suboperands) onto multiple
bitfield variables.
In the mean time, this adds support for positionally encoded operands to
FixedLenDecoderEmitter, so that we can generate a disassembler for the PowerPC
backend. To prevent an accidental reliance on this feature, and to prevent an
undesirable interaction with existing disassemblers, a backend must opt-in to
this support by setting the new decodePositionallyEncodedOperands
instruction-set bit to true.
When enabled, this iterates the variables that contribute to the instruction
encoding, just as the encoder does, and emulates the procedure the encoder uses
to map "numbered" operands to variables. The bit range for each variable is
also determined as the encoder determines them. This map is then consulted
during the decoder-generator's loop over operands to decode, allowing the
decoder to understand both position-based and name-based operand-to-variable
mappings.
As noted in the comment on the decodePositionallyEncodedOperands definition,
this support should be removed once it is no longer needed. There should be no
change to existing disassemblers.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@197691 91177308-0d34-0410-b5e6-96231b3b80d8
2013-12-19 16:12:53 +00:00
|
|
|
[SO.second]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-02-18 21:51:29 +00:00
|
|
|
std::string Decoder = "";
|
|
|
|
|
2011-07-28 21:54:31 +00:00
|
|
|
// At this point, we can locate the field, but we need to know how to
|
|
|
|
// interpret it. As a first step, require the target to provide callbacks
|
|
|
|
// for decoding register classes.
|
|
|
|
// FIXME: This need to be extended to handle instructions with custom
|
|
|
|
// decoder methods, and operands with (simple) MIOperandInfo's.
|
2014-12-13 05:12:19 +00:00
|
|
|
TypedInit *TI = cast<TypedInit>(Op.first);
|
2012-10-10 20:24:47 +00:00
|
|
|
RecordRecTy *Type = cast<RecordRecTy>(TI->getType());
|
2011-07-28 21:54:31 +00:00
|
|
|
Record *TypeRecord = Type->getRecord();
|
|
|
|
bool isReg = false;
|
|
|
|
if (TypeRecord->isSubClassOf("RegisterOperand"))
|
|
|
|
TypeRecord = TypeRecord->getValueAsDef("RegClass");
|
|
|
|
if (TypeRecord->isSubClassOf("RegisterClass")) {
|
|
|
|
Decoder = "Decode" + TypeRecord->getName() + "RegisterClass";
|
|
|
|
isReg = true;
|
2013-12-19 14:58:22 +00:00
|
|
|
} else if (TypeRecord->isSubClassOf("PointerLikeRegClass")) {
|
|
|
|
Decoder = "DecodePointerLikeRegClass" +
|
|
|
|
utostr(TypeRecord->getValueAsInt("RegClassKind"));
|
|
|
|
isReg = true;
|
2011-07-28 21:54:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RecordVal *DecoderString = TypeRecord->getValue("DecoderMethod");
|
2011-07-29 22:43:06 +00:00
|
|
|
StringInit *String = DecoderString ?
|
2014-04-15 07:20:03 +00:00
|
|
|
dyn_cast<StringInit>(DecoderString->getValue()) : nullptr;
|
2011-07-28 21:54:31 +00:00
|
|
|
if (!isReg && String && String->getValue() != "")
|
|
|
|
Decoder = String->getValue();
|
|
|
|
|
|
|
|
OperandInfo OpInfo(Decoder);
|
|
|
|
unsigned Base = ~0U;
|
|
|
|
unsigned Width = 0;
|
|
|
|
unsigned Offset = 0;
|
|
|
|
|
2011-02-18 21:51:29 +00:00
|
|
|
for (unsigned bi = 0; bi < Bits.getNumBits(); ++bi) {
|
2014-04-15 07:20:03 +00:00
|
|
|
VarInit *Var = nullptr;
|
2012-10-10 20:24:43 +00:00
|
|
|
VarBitInit *BI = dyn_cast<VarBitInit>(Bits.getBit(bi));
|
2011-08-01 22:45:43 +00:00
|
|
|
if (BI)
|
2012-10-10 20:24:43 +00:00
|
|
|
Var = dyn_cast<VarInit>(BI->getBitVar());
|
2011-08-01 22:45:43 +00:00
|
|
|
else
|
2012-10-10 20:24:43 +00:00
|
|
|
Var = dyn_cast<VarInit>(Bits.getBit(bi));
|
2011-08-01 22:45:43 +00:00
|
|
|
|
|
|
|
if (!Var) {
|
2011-07-28 21:54:31 +00:00
|
|
|
if (Base != ~0U) {
|
|
|
|
OpInfo.addField(Base, Width, Offset);
|
|
|
|
Base = ~0U;
|
|
|
|
Width = 0;
|
|
|
|
Offset = 0;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2011-02-18 21:51:29 +00:00
|
|
|
|
2014-12-13 05:12:19 +00:00
|
|
|
if (Var->getName() != Op.second &&
|
|
|
|
Var->getName() != TiedNames[Op.second]) {
|
2011-07-28 21:54:31 +00:00
|
|
|
if (Base != ~0U) {
|
|
|
|
OpInfo.addField(Base, Width, Offset);
|
|
|
|
Base = ~0U;
|
|
|
|
Width = 0;
|
|
|
|
Offset = 0;
|
|
|
|
}
|
|
|
|
continue;
|
2011-02-18 21:51:29 +00:00
|
|
|
}
|
|
|
|
|
2011-07-28 21:54:31 +00:00
|
|
|
if (Base == ~0U) {
|
|
|
|
Base = bi;
|
|
|
|
Width = 1;
|
2011-08-01 22:45:43 +00:00
|
|
|
Offset = BI ? BI->getBitNum() : 0;
|
|
|
|
} else if (BI && BI->getBitNum() != Offset + Width) {
|
2011-07-29 23:01:18 +00:00
|
|
|
OpInfo.addField(Base, Width, Offset);
|
|
|
|
Base = bi;
|
|
|
|
Width = 1;
|
|
|
|
Offset = BI->getBitNum();
|
2011-07-28 21:54:31 +00:00
|
|
|
} else {
|
|
|
|
++Width;
|
2011-02-18 21:51:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-28 21:54:31 +00:00
|
|
|
if (Base != ~0U)
|
|
|
|
OpInfo.addField(Base, Width, Offset);
|
|
|
|
|
|
|
|
if (OpInfo.numFields() > 0)
|
|
|
|
InsnOperands.push_back(OpInfo);
|
2011-02-18 21:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Operands[Opc] = InsnOperands;
|
|
|
|
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
DEBUG({
|
|
|
|
// Dumps the instruction encoding bits.
|
|
|
|
dumpBits(errs(), Bits);
|
|
|
|
|
|
|
|
errs() << '\n';
|
|
|
|
|
|
|
|
// Dumps the list of operand info.
|
|
|
|
for (unsigned i = 0, e = CGI.Operands.size(); i != e; ++i) {
|
|
|
|
const CGIOperandList::OperandInfo &Info = CGI.Operands[i];
|
|
|
|
const std::string &OperandName = Info.Name;
|
|
|
|
const Record &OperandDef = *Info.Rec;
|
|
|
|
|
|
|
|
errs() << "\t" << OperandName << " (" << OperandDef.getName() << ")\n";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-08-14 19:06:05 +00:00
|
|
|
// emitFieldFromInstruction - Emit the templated helper function
|
|
|
|
// fieldFromInstruction().
|
|
|
|
static void emitFieldFromInstruction(formatted_raw_ostream &OS) {
|
|
|
|
OS << "// Helper function for extracting fields from encoded instructions.\n"
|
|
|
|
<< "template<typename InsnType>\n"
|
|
|
|
<< "static InsnType fieldFromInstruction(InsnType insn, unsigned startBit,\n"
|
|
|
|
<< " unsigned numBits) {\n"
|
|
|
|
<< " assert(startBit + numBits <= (sizeof(InsnType)*8) &&\n"
|
|
|
|
<< " \"Instruction field out of bounds!\");\n"
|
|
|
|
<< " InsnType fieldMask;\n"
|
|
|
|
<< " if (numBits == sizeof(InsnType)*8)\n"
|
|
|
|
<< " fieldMask = (InsnType)(-1LL);\n"
|
|
|
|
<< " else\n"
|
2012-12-26 06:43:14 +00:00
|
|
|
<< " fieldMask = (((InsnType)1 << numBits) - 1) << startBit;\n"
|
2012-08-14 19:06:05 +00:00
|
|
|
<< " return (insn & fieldMask) >> startBit;\n"
|
|
|
|
<< "}\n\n";
|
|
|
|
}
|
2011-07-19 21:06:00 +00:00
|
|
|
|
2012-08-14 19:06:05 +00:00
|
|
|
// emitDecodeInstruction - Emit the templated helper function
|
|
|
|
// decodeInstruction().
|
|
|
|
static void emitDecodeInstruction(formatted_raw_ostream &OS) {
|
|
|
|
OS << "template<typename InsnType>\n"
|
|
|
|
<< "static DecodeStatus decodeInstruction(const uint8_t DecodeTable[], MCInst &MI,\n"
|
|
|
|
<< " InsnType insn, uint64_t Address,\n"
|
|
|
|
<< " const void *DisAsm,\n"
|
|
|
|
<< " const MCSubtargetInfo &STI) {\n"
|
2015-02-19 11:38:11 +00:00
|
|
|
<< " uint64_t Bits = STI.getFeatureBits();\n"
|
2012-08-14 19:06:05 +00:00
|
|
|
<< "\n"
|
|
|
|
<< " const uint8_t *Ptr = DecodeTable;\n"
|
2012-09-17 18:00:53 +00:00
|
|
|
<< " uint32_t CurFieldValue = 0;\n"
|
2012-08-14 19:06:05 +00:00
|
|
|
<< " DecodeStatus S = MCDisassembler::Success;\n"
|
|
|
|
<< " for (;;) {\n"
|
|
|
|
<< " ptrdiff_t Loc = Ptr - DecodeTable;\n"
|
|
|
|
<< " switch (*Ptr) {\n"
|
|
|
|
<< " default:\n"
|
|
|
|
<< " errs() << Loc << \": Unexpected decode table opcode!\\n\";\n"
|
|
|
|
<< " return MCDisassembler::Fail;\n"
|
|
|
|
<< " case MCD::OPC_ExtractField: {\n"
|
|
|
|
<< " unsigned Start = *++Ptr;\n"
|
|
|
|
<< " unsigned Len = *++Ptr;\n"
|
|
|
|
<< " ++Ptr;\n"
|
|
|
|
<< " CurFieldValue = fieldFromInstruction(insn, Start, Len);\n"
|
|
|
|
<< " DEBUG(dbgs() << Loc << \": OPC_ExtractField(\" << Start << \", \"\n"
|
|
|
|
<< " << Len << \"): \" << CurFieldValue << \"\\n\");\n"
|
|
|
|
<< " break;\n"
|
|
|
|
<< " }\n"
|
|
|
|
<< " case MCD::OPC_FilterValue: {\n"
|
|
|
|
<< " // Decode the field value.\n"
|
|
|
|
<< " unsigned Len;\n"
|
|
|
|
<< " InsnType Val = decodeULEB128(++Ptr, &Len);\n"
|
|
|
|
<< " Ptr += Len;\n"
|
|
|
|
<< " // NumToSkip is a plain 16-bit integer.\n"
|
|
|
|
<< " unsigned NumToSkip = *Ptr++;\n"
|
|
|
|
<< " NumToSkip |= (*Ptr++) << 8;\n"
|
|
|
|
<< "\n"
|
|
|
|
<< " // Perform the filter operation.\n"
|
|
|
|
<< " if (Val != CurFieldValue)\n"
|
|
|
|
<< " Ptr += NumToSkip;\n"
|
|
|
|
<< " DEBUG(dbgs() << Loc << \": OPC_FilterValue(\" << Val << \", \" << NumToSkip\n"
|
|
|
|
<< " << \"): \" << ((Val != CurFieldValue) ? \"FAIL:\" : \"PASS:\")\n"
|
|
|
|
<< " << \" continuing at \" << (Ptr - DecodeTable) << \"\\n\");\n"
|
|
|
|
<< "\n"
|
|
|
|
<< " break;\n"
|
|
|
|
<< " }\n"
|
|
|
|
<< " case MCD::OPC_CheckField: {\n"
|
|
|
|
<< " unsigned Start = *++Ptr;\n"
|
|
|
|
<< " unsigned Len = *++Ptr;\n"
|
|
|
|
<< " InsnType FieldValue = fieldFromInstruction(insn, Start, Len);\n"
|
|
|
|
<< " // Decode the field value.\n"
|
|
|
|
<< " uint32_t ExpectedValue = decodeULEB128(++Ptr, &Len);\n"
|
|
|
|
<< " Ptr += Len;\n"
|
|
|
|
<< " // NumToSkip is a plain 16-bit integer.\n"
|
|
|
|
<< " unsigned NumToSkip = *Ptr++;\n"
|
|
|
|
<< " NumToSkip |= (*Ptr++) << 8;\n"
|
|
|
|
<< "\n"
|
|
|
|
<< " // If the actual and expected values don't match, skip.\n"
|
|
|
|
<< " if (ExpectedValue != FieldValue)\n"
|
|
|
|
<< " Ptr += NumToSkip;\n"
|
|
|
|
<< " DEBUG(dbgs() << Loc << \": OPC_CheckField(\" << Start << \", \"\n"
|
|
|
|
<< " << Len << \", \" << ExpectedValue << \", \" << NumToSkip\n"
|
|
|
|
<< " << \"): FieldValue = \" << FieldValue << \", ExpectedValue = \"\n"
|
|
|
|
<< " << ExpectedValue << \": \"\n"
|
|
|
|
<< " << ((ExpectedValue == FieldValue) ? \"PASS\\n\" : \"FAIL\\n\"));\n"
|
|
|
|
<< " break;\n"
|
|
|
|
<< " }\n"
|
|
|
|
<< " case MCD::OPC_CheckPredicate: {\n"
|
|
|
|
<< " unsigned Len;\n"
|
|
|
|
<< " // Decode the Predicate Index value.\n"
|
|
|
|
<< " unsigned PIdx = decodeULEB128(++Ptr, &Len);\n"
|
|
|
|
<< " Ptr += Len;\n"
|
|
|
|
<< " // NumToSkip is a plain 16-bit integer.\n"
|
|
|
|
<< " unsigned NumToSkip = *Ptr++;\n"
|
|
|
|
<< " NumToSkip |= (*Ptr++) << 8;\n"
|
|
|
|
<< " // Check the predicate.\n"
|
|
|
|
<< " bool Pred;\n"
|
|
|
|
<< " if (!(Pred = checkDecoderPredicate(PIdx, Bits)))\n"
|
|
|
|
<< " Ptr += NumToSkip;\n"
|
|
|
|
<< " (void)Pred;\n"
|
|
|
|
<< " DEBUG(dbgs() << Loc << \": OPC_CheckPredicate(\" << PIdx << \"): \"\n"
|
|
|
|
<< " << (Pred ? \"PASS\\n\" : \"FAIL\\n\"));\n"
|
|
|
|
<< "\n"
|
|
|
|
<< " break;\n"
|
|
|
|
<< " }\n"
|
|
|
|
<< " case MCD::OPC_Decode: {\n"
|
|
|
|
<< " unsigned Len;\n"
|
|
|
|
<< " // Decode the Opcode value.\n"
|
|
|
|
<< " unsigned Opc = decodeULEB128(++Ptr, &Len);\n"
|
|
|
|
<< " Ptr += Len;\n"
|
|
|
|
<< " unsigned DecodeIdx = decodeULEB128(Ptr, &Len);\n"
|
|
|
|
<< " Ptr += Len;\n"
|
|
|
|
<< " DEBUG(dbgs() << Loc << \": OPC_Decode: opcode \" << Opc\n"
|
|
|
|
<< " << \", using decoder \" << DecodeIdx << \"\\n\" );\n"
|
|
|
|
<< " DEBUG(dbgs() << \"----- DECODE SUCCESSFUL -----\\n\");\n"
|
|
|
|
<< "\n"
|
|
|
|
<< " MI.setOpcode(Opc);\n"
|
2012-08-15 10:26:44 +00:00
|
|
|
<< " return decodeToMCInst(S, DecodeIdx, insn, MI, Address, DisAsm);\n"
|
2012-08-14 19:06:05 +00:00
|
|
|
<< " }\n"
|
|
|
|
<< " case MCD::OPC_SoftFail: {\n"
|
|
|
|
<< " // Decode the mask values.\n"
|
|
|
|
<< " unsigned Len;\n"
|
|
|
|
<< " InsnType PositiveMask = decodeULEB128(++Ptr, &Len);\n"
|
|
|
|
<< " Ptr += Len;\n"
|
|
|
|
<< " InsnType NegativeMask = decodeULEB128(Ptr, &Len);\n"
|
|
|
|
<< " Ptr += Len;\n"
|
|
|
|
<< " bool Fail = (insn & PositiveMask) || (~insn & NegativeMask);\n"
|
|
|
|
<< " if (Fail)\n"
|
|
|
|
<< " S = MCDisassembler::SoftFail;\n"
|
|
|
|
<< " DEBUG(dbgs() << Loc << \": OPC_SoftFail: \" << (Fail ? \"FAIL\\n\":\"PASS\\n\"));\n"
|
|
|
|
<< " break;\n"
|
|
|
|
<< " }\n"
|
|
|
|
<< " case MCD::OPC_Fail: {\n"
|
|
|
|
<< " DEBUG(dbgs() << Loc << \": OPC_Fail\\n\");\n"
|
|
|
|
<< " return MCDisassembler::Fail;\n"
|
|
|
|
<< " }\n"
|
|
|
|
<< " }\n"
|
|
|
|
<< " }\n"
|
|
|
|
<< " llvm_unreachable(\"bogosity detected in disassembler state machine!\");\n"
|
|
|
|
<< "}\n\n";
|
2011-02-18 21:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Emits disassembler code for instruction decoding.
|
2012-03-16 01:19:24 +00:00
|
|
|
void FixedLenDecoderEmitter::run(raw_ostream &o) {
|
2012-08-14 19:06:05 +00:00
|
|
|
formatted_raw_ostream OS(o);
|
|
|
|
OS << "#include \"llvm/MC/MCInst.h\"\n";
|
|
|
|
OS << "#include \"llvm/Support/Debug.h\"\n";
|
|
|
|
OS << "#include \"llvm/Support/DataTypes.h\"\n";
|
|
|
|
OS << "#include \"llvm/Support/LEB128.h\"\n";
|
|
|
|
OS << "#include \"llvm/Support/raw_ostream.h\"\n";
|
|
|
|
OS << "#include <assert.h>\n";
|
|
|
|
OS << '\n';
|
|
|
|
OS << "namespace llvm {\n\n";
|
|
|
|
|
|
|
|
emitFieldFromInstruction(OS);
|
2011-02-18 21:51:29 +00:00
|
|
|
|
2013-12-17 22:37:50 +00:00
|
|
|
Target.reverseBitsForLittleEndianEncoding();
|
|
|
|
|
2011-07-19 21:06:00 +00:00
|
|
|
// Parameterize the decoders based on namespace and instruction width.
|
2012-08-14 19:06:05 +00:00
|
|
|
NumberedInstructions = &Target.getInstructionsByEnumValue();
|
2011-07-19 21:06:00 +00:00
|
|
|
std::map<std::pair<std::string, unsigned>,
|
|
|
|
std::vector<unsigned> > OpcMap;
|
|
|
|
std::map<unsigned, std::vector<OperandInfo> > Operands;
|
|
|
|
|
2012-08-14 19:06:05 +00:00
|
|
|
for (unsigned i = 0; i < NumberedInstructions->size(); ++i) {
|
|
|
|
const CodeGenInstruction *Inst = NumberedInstructions->at(i);
|
2012-03-16 05:58:09 +00:00
|
|
|
const Record *Def = Inst->TheDef;
|
2011-07-19 21:06:00 +00:00
|
|
|
unsigned Size = Def->getValueAsInt("Size");
|
|
|
|
if (Def->getValueAsString("Namespace") == "TargetOpcode" ||
|
|
|
|
Def->getValueAsBit("isPseudo") ||
|
|
|
|
Def->getValueAsBit("isAsmParserOnly") ||
|
|
|
|
Def->getValueAsBit("isCodeGenOnly"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
std::string DecoderNamespace = Def->getValueAsString("DecoderNamespace");
|
|
|
|
|
|
|
|
if (Size) {
|
Add support for positionally-encoded operands to FixedLenDecoderEmitter
Unfortunately, the PowerPC instruction definitions make heavy use of the
positional operand encoding heuristic to map operands onto bitfield variables
in the instruction definitions. Changing this to use name-based mapping is not
trivial, however, because additional infrastructure needs to be designed to
handle mapping of complex operands (with multiple suboperands) onto multiple
bitfield variables.
In the mean time, this adds support for positionally encoded operands to
FixedLenDecoderEmitter, so that we can generate a disassembler for the PowerPC
backend. To prevent an accidental reliance on this feature, and to prevent an
undesirable interaction with existing disassemblers, a backend must opt-in to
this support by setting the new decodePositionallyEncodedOperands
instruction-set bit to true.
When enabled, this iterates the variables that contribute to the instruction
encoding, just as the encoder does, and emulates the procedure the encoder uses
to map "numbered" operands to variables. The bit range for each variable is
also determined as the encoder determines them. This map is then consulted
during the decoder-generator's loop over operands to decode, allowing the
decoder to understand both position-based and name-based operand-to-variable
mappings.
As noted in the comment on the decodePositionallyEncodedOperands definition,
this support should be removed once it is no longer needed. There should be no
change to existing disassemblers.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@197691 91177308-0d34-0410-b5e6-96231b3b80d8
2013-12-19 16:12:53 +00:00
|
|
|
if (populateInstruction(Target, *Inst, i, Operands)) {
|
2011-07-19 21:06:00 +00:00
|
|
|
OpcMap[std::make_pair(DecoderNamespace, Size)].push_back(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-14 19:06:05 +00:00
|
|
|
DecoderTableInfo TableInfo;
|
2014-12-13 05:12:19 +00:00
|
|
|
for (const auto &Opc : OpcMap) {
|
2011-07-19 21:06:00 +00:00
|
|
|
// Emit the decoder for this namespace+width combination.
|
2014-12-13 05:12:19 +00:00
|
|
|
FilterChooser FC(*NumberedInstructions, Opc.second, Operands,
|
|
|
|
8*Opc.first.second, this);
|
2012-08-14 19:06:05 +00:00
|
|
|
|
|
|
|
// The decode table is cleared for each top level decoder function. The
|
|
|
|
// predicates and decoders themselves, however, are shared across all
|
|
|
|
// decoders to give more opportunities for uniqueing.
|
|
|
|
TableInfo.Table.clear();
|
|
|
|
TableInfo.FixupStack.clear();
|
|
|
|
TableInfo.Table.reserve(16384);
|
|
|
|
TableInfo.FixupStack.push_back(FixupList());
|
|
|
|
FC.emitTableEntries(TableInfo);
|
|
|
|
// Any NumToSkip fixups in the top level scope can resolve to the
|
|
|
|
// OPC_Fail at the end of the table.
|
|
|
|
assert(TableInfo.FixupStack.size() == 1 && "fixup stack phasing error!");
|
|
|
|
// Resolve any NumToSkip fixups in the current scope.
|
|
|
|
resolveTableFixups(TableInfo.Table, TableInfo.FixupStack.back(),
|
|
|
|
TableInfo.Table.size());
|
|
|
|
TableInfo.FixupStack.clear();
|
|
|
|
|
|
|
|
TableInfo.Table.push_back(MCD::OPC_Fail);
|
|
|
|
|
|
|
|
// Print the table to the output stream.
|
2014-12-13 05:12:19 +00:00
|
|
|
emitTable(OS, TableInfo.Table, 0, FC.getBitWidth(), Opc.first.first);
|
2012-08-14 19:06:05 +00:00
|
|
|
OS.flush();
|
2011-07-19 21:06:00 +00:00
|
|
|
}
|
2011-02-18 21:51:29 +00:00
|
|
|
|
2012-08-14 19:06:05 +00:00
|
|
|
// Emit the predicate function.
|
|
|
|
emitPredicateFunction(OS, TableInfo.Predicates, 0);
|
|
|
|
|
|
|
|
// Emit the decoder function.
|
|
|
|
emitDecoderFunction(OS, TableInfo.Decoders, 0);
|
|
|
|
|
|
|
|
// Emit the main entry point for the decoder, decodeInstruction().
|
|
|
|
emitDecodeInstruction(OS);
|
|
|
|
|
|
|
|
OS << "\n} // End llvm namespace\n";
|
2011-02-18 21:51:29 +00:00
|
|
|
}
|
2012-06-11 15:37:55 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
void EmitFixedLenDecoder(RecordKeeper &RK, raw_ostream &OS,
|
|
|
|
std::string PredicateNamespace,
|
|
|
|
std::string GPrefix,
|
|
|
|
std::string GPostfix,
|
|
|
|
std::string ROK,
|
|
|
|
std::string RFail,
|
|
|
|
std::string L) {
|
|
|
|
FixedLenDecoderEmitter(RK, PredicateNamespace, GPrefix, GPostfix,
|
|
|
|
ROK, RFail, L).run(OS);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // End llvm namespace
|