2011-02-18 22:51:29 +01: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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "decoder-emitter"
|
|
|
|
|
|
|
|
#include "FixedLenDecoderEmitter.h"
|
|
|
|
#include "CodeGenTarget.h"
|
2011-10-01 18:41:13 +02:00
|
|
|
#include "llvm/TableGen/Record.h"
|
2012-02-09 11:56:31 +01:00
|
|
|
#include "llvm/ADT/APInt.h"
|
2011-02-18 22:51:29 +01:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
2011-07-30 00:43:06 +02:00
|
|
|
static bit_value_t bitFromBits(BitsInit &bits, unsigned index) {
|
|
|
|
if (BitInit *bit = dynamic_cast<BitInit*>(bits.getBit(index)))
|
2011-02-18 22:51:29 +01:00
|
|
|
return bit->getValue() ? BIT_TRUE : BIT_FALSE;
|
|
|
|
|
|
|
|
// The bit is uninitialized.
|
|
|
|
return BIT_UNSET;
|
|
|
|
}
|
|
|
|
// Prints the bit value for each position.
|
2011-07-30 00:43:06 +02:00
|
|
|
static void dumpBits(raw_ostream &o, BitsInit &bits) {
|
2011-02-18 22:51:29 +01:00
|
|
|
unsigned index;
|
|
|
|
|
|
|
|
for (index = bits.getNumBits(); index > 0; index--) {
|
|
|
|
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 08:21:30 +01:00
|
|
|
llvm_unreachable("unexpected return value from bitFromBits");
|
2011-02-18 22:51:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-30 00:43:06 +02:00
|
|
|
static BitsInit &getBitsField(const Record &def, const char *str) {
|
|
|
|
BitsInit *bits = def.getValueAsBitsInit(str);
|
2011-02-18 22:51:29 +01:00
|
|
|
return *bits;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Forward declaration.
|
|
|
|
class FilterChooser;
|
|
|
|
|
|
|
|
// Representation of the instruction to work on.
|
2011-07-19 23:06:00 +02:00
|
|
|
typedef std::vector<bit_value_t> insn_t;
|
2011-02-18 22:51:29 +01: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.
|
|
|
|
class Filter {
|
|
|
|
protected:
|
|
|
|
FilterChooser *Owner; // points to the FilterChooser who owns this filter
|
|
|
|
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.
|
|
|
|
std::map<unsigned, FilterChooser*> FilterChooserMap;
|
|
|
|
|
|
|
|
// Number of instructions which fall under FilteredInstructions category.
|
|
|
|
unsigned NumFiltered;
|
|
|
|
|
|
|
|
// Keeps track of the last opcode in the filtered bucket.
|
|
|
|
unsigned LastOpcFiltered;
|
|
|
|
|
|
|
|
// Number of instructions which fall under VariableInstructions category.
|
|
|
|
unsigned NumVariable;
|
|
|
|
|
|
|
|
public:
|
|
|
|
unsigned getNumFiltered() { return NumFiltered; }
|
|
|
|
unsigned getNumVariable() { return NumVariable; }
|
|
|
|
unsigned getSingletonOpc() {
|
|
|
|
assert(NumFiltered == 1);
|
|
|
|
return LastOpcFiltered;
|
|
|
|
}
|
|
|
|
// Return the filter chooser for the group of instructions without constant
|
|
|
|
// segment values.
|
|
|
|
FilterChooser &getVariableFC() {
|
|
|
|
assert(NumFiltered == 1);
|
|
|
|
assert(FilterChooserMap.size() == 1);
|
|
|
|
return *(FilterChooserMap.find((unsigned)-1)->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
Filter(const Filter &f);
|
|
|
|
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();
|
|
|
|
|
|
|
|
// Emit code to decode instructions given a segment or segments of bits.
|
|
|
|
void emit(raw_ostream &o, unsigned &Indentation);
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
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.
|
|
|
|
const std::vector<unsigned> Opcodes;
|
|
|
|
|
|
|
|
// Lookup table for the operand decoding of instructions.
|
|
|
|
std::map<unsigned, std::vector<OperandInfo> > &Operands;
|
|
|
|
|
|
|
|
// 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 23:06:00 +02:00
|
|
|
std::vector<bit_value_t> FilterBitValues;
|
2011-02-18 22:51:29 +01:00
|
|
|
|
|
|
|
// Links to the FilterChooser above us in the decoding tree.
|
|
|
|
FilterChooser *Parent;
|
|
|
|
|
|
|
|
// Index of the best filter from Filters.
|
|
|
|
int BestIndex;
|
|
|
|
|
2011-07-19 23:06:00 +02:00
|
|
|
// Width of instructions
|
|
|
|
unsigned BitWidth;
|
|
|
|
|
2011-08-17 19:44:15 +02:00
|
|
|
// Parent emitter
|
|
|
|
const FixedLenDecoderEmitter *Emitter;
|
|
|
|
|
2011-02-18 22:51:29 +01:00
|
|
|
public:
|
|
|
|
FilterChooser(const FilterChooser &FC) :
|
|
|
|
AllInstructions(FC.AllInstructions), Opcodes(FC.Opcodes),
|
2011-07-19 23:06:00 +02:00
|
|
|
Operands(FC.Operands), Filters(FC.Filters),
|
|
|
|
FilterBitValues(FC.FilterBitValues), Parent(FC.Parent),
|
2011-08-17 19:44:15 +02:00
|
|
|
BestIndex(FC.BestIndex), BitWidth(FC.BitWidth),
|
|
|
|
Emitter(FC.Emitter) { }
|
2011-02-18 22:51:29 +01:00
|
|
|
|
|
|
|
FilterChooser(const std::vector<const CodeGenInstruction*> &Insts,
|
|
|
|
const std::vector<unsigned> &IDs,
|
2011-07-19 23:06:00 +02:00
|
|
|
std::map<unsigned, std::vector<OperandInfo> > &Ops,
|
2011-08-17 19:44:15 +02:00
|
|
|
unsigned BW,
|
|
|
|
const FixedLenDecoderEmitter *E) :
|
2011-02-18 22:51:29 +01:00
|
|
|
AllInstructions(Insts), Opcodes(IDs), Operands(Ops), Filters(),
|
2011-08-17 19:44:15 +02:00
|
|
|
Parent(NULL), BestIndex(-1), BitWidth(BW), Emitter(E) {
|
2011-07-19 23:06:00 +02:00
|
|
|
for (unsigned i = 0; i < BitWidth; ++i)
|
|
|
|
FilterBitValues.push_back(BIT_UNFILTERED);
|
2011-02-18 22:51:29 +01:00
|
|
|
|
|
|
|
doFilter();
|
|
|
|
}
|
|
|
|
|
|
|
|
FilterChooser(const std::vector<const CodeGenInstruction*> &Insts,
|
|
|
|
const std::vector<unsigned> &IDs,
|
|
|
|
std::map<unsigned, std::vector<OperandInfo> > &Ops,
|
2011-07-19 23:06:00 +02:00
|
|
|
std::vector<bit_value_t> &ParentFilterBitValues,
|
2011-02-18 22:51:29 +01:00
|
|
|
FilterChooser &parent) :
|
|
|
|
AllInstructions(Insts), Opcodes(IDs), Operands(Ops),
|
2011-07-19 23:06:00 +02:00
|
|
|
Filters(), FilterBitValues(ParentFilterBitValues),
|
2011-08-17 19:44:15 +02:00
|
|
|
Parent(&parent), BestIndex(-1), BitWidth(parent.BitWidth),
|
|
|
|
Emitter(parent.Emitter) {
|
2011-02-18 22:51:29 +01:00
|
|
|
doFilter();
|
|
|
|
}
|
|
|
|
|
|
|
|
// The top level filter chooser has NULL as its parent.
|
|
|
|
bool isTopLevel() { return Parent == NULL; }
|
|
|
|
|
|
|
|
// Emit the top level typedef and decodeInstruction() function.
|
2011-07-19 23:06:00 +02:00
|
|
|
void emitTop(raw_ostream &o, unsigned Indentation, std::string Namespace);
|
2011-02-18 22:51:29 +01:00
|
|
|
|
|
|
|
protected:
|
|
|
|
// Populates the insn given the uid.
|
|
|
|
void insnWithID(insn_t &Insn, unsigned Opcode) const {
|
2011-07-30 00:43:06 +02:00
|
|
|
BitsInit &Bits = getBitsField(*AllInstructions[Opcode]->TheDef, "Inst");
|
2011-02-18 22:51:29 +01:00
|
|
|
|
2012-02-09 11:56:31 +01: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.
|
|
|
|
BitsInit *SFBits = AllInstructions[Opcode]->TheDef->getValueAsBitsInit("SoftFail");
|
|
|
|
|
|
|
|
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 22:51:29 +01: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,
|
|
|
|
unsigned NumBits) const;
|
|
|
|
|
|
|
|
/// dumpFilterArray - dumpFilterArray prints out debugging info for the given
|
|
|
|
/// filter array as a series of chars.
|
2011-07-19 23:06:00 +02:00
|
|
|
void dumpFilterArray(raw_ostream &o, std::vector<bit_value_t> & filter);
|
2011-02-18 22:51:29 +01:00
|
|
|
|
|
|
|
/// dumpStack - dumpStack traverses the filter chooser chain and calls
|
|
|
|
/// dumpFilterArray on each filter chooser up to the top level one.
|
|
|
|
void dumpStack(raw_ostream &o, const char *prefix);
|
|
|
|
|
|
|
|
Filter &bestFilter() {
|
|
|
|
assert(BestIndex != -1 && "BestIndex not set");
|
|
|
|
return Filters[BestIndex];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called from Filter::recurse() when singleton exists. For debug purpose.
|
|
|
|
void SingletonExists(unsigned Opc);
|
|
|
|
|
|
|
|
bool PositionFiltered(unsigned i) {
|
|
|
|
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,
|
|
|
|
std::vector<unsigned> &EndBits, std::vector<uint64_t> &FieldVals,
|
|
|
|
insn_t &Insn);
|
|
|
|
|
2011-09-07 21:42:28 +02:00
|
|
|
// Emits code to check the Predicates member of an instruction are true.
|
|
|
|
// Returns true if predicate matches were emitted, false otherwise.
|
|
|
|
bool emitPredicateMatch(raw_ostream &o, unsigned &Indentation,unsigned Opc);
|
|
|
|
|
2012-02-09 11:56:31 +01:00
|
|
|
void emitSoftFailCheck(raw_ostream &o, unsigned Indentation, unsigned Opc);
|
|
|
|
|
2011-02-18 22:51:29 +01:00
|
|
|
// Emits code to decode the singleton. Return true if we have matched all the
|
|
|
|
// well-known bits.
|
|
|
|
bool emitSingletonDecoder(raw_ostream &o, unsigned &Indentation,unsigned Opc);
|
|
|
|
|
|
|
|
// Emits code to decode the singleton, and then to decode the rest.
|
|
|
|
void emitSingletonDecoder(raw_ostream &o, unsigned &Indentation,Filter &Best);
|
|
|
|
|
2011-07-28 23:54:31 +02:00
|
|
|
void emitBinaryParser(raw_ostream &o , unsigned &Indentation,
|
|
|
|
OperandInfo &OpInfo);
|
|
|
|
|
2011-02-18 22:51:29 +01:00
|
|
|
// Assign a single filter and run with it.
|
|
|
|
void runSingleFilter(FilterChooser &owner, unsigned startBit, unsigned numBit,
|
|
|
|
bool mixed);
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
bool AllowMixed);
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
|
|
|
// Emits code to decode our share of instructions. Returns true if the
|
|
|
|
// emitted code causes a return, which occurs if we know how to decode
|
|
|
|
// the instruction at this level or the instruction is not decodeable.
|
|
|
|
bool emit(raw_ostream &o, unsigned &Indentation);
|
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////
|
|
|
|
// //
|
|
|
|
// Filter Implmenetation //
|
|
|
|
// //
|
|
|
|
///////////////////////////
|
|
|
|
|
|
|
|
Filter::Filter(const Filter &f) :
|
|
|
|
Owner(f.Owner), StartBit(f.StartBit), NumBits(f.NumBits), Mixed(f.Mixed),
|
|
|
|
FilteredInstructions(f.FilteredInstructions),
|
|
|
|
VariableInstructions(f.VariableInstructions),
|
|
|
|
FilterChooserMap(f.FilterChooserMap), NumFiltered(f.NumFiltered),
|
|
|
|
LastOpcFiltered(f.LastOpcFiltered), NumVariable(f.NumVariable) {
|
|
|
|
}
|
|
|
|
|
|
|
|
Filter::Filter(FilterChooser &owner, unsigned startBit, unsigned numBits,
|
|
|
|
bool mixed) : Owner(&owner), StartBit(startBit), NumBits(numBits),
|
|
|
|
Mixed(mixed) {
|
2011-07-19 23:06:00 +02:00
|
|
|
assert(StartBit + NumBits - 1 < Owner->BitWidth);
|
2011-02-18 22:51:29 +01:00
|
|
|
|
|
|
|
NumFiltered = 0;
|
|
|
|
LastOpcFiltered = 0;
|
|
|
|
NumVariable = 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 {
|
|
|
|
// Some of the encoding bit(s) are unspecfied. This contributes to
|
|
|
|
// one additional member of "Variable" instructions.
|
|
|
|
VariableInstructions.push_back(Owner->Opcodes[i]);
|
|
|
|
++NumVariable;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert((FilteredInstructions.size() + VariableInstructions.size() > 0)
|
|
|
|
&& "Filter returns no instruction categories");
|
|
|
|
}
|
|
|
|
|
|
|
|
Filter::~Filter() {
|
|
|
|
std::map<unsigned, FilterChooser*>::iterator filterIterator;
|
|
|
|
for (filterIterator = FilterChooserMap.begin();
|
|
|
|
filterIterator != FilterChooserMap.end();
|
|
|
|
filterIterator++) {
|
|
|
|
delete filterIterator->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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() {
|
|
|
|
std::map<uint64_t, std::vector<unsigned> >::const_iterator mapIterator;
|
|
|
|
|
|
|
|
// Starts by inheriting our parent filter chooser's filter bit values.
|
2011-07-19 23:06:00 +02:00
|
|
|
std::vector<bit_value_t> BitValueArray(Owner->FilterBitValues);
|
2011-02-18 22:51:29 +01:00
|
|
|
|
|
|
|
unsigned bitIndex;
|
|
|
|
|
|
|
|
if (VariableInstructions.size()) {
|
|
|
|
// Conservatively marks each segment position as BIT_UNSET.
|
|
|
|
for (bitIndex = 0; bitIndex < NumBits; bitIndex++)
|
|
|
|
BitValueArray[StartBit + bitIndex] = BIT_UNSET;
|
|
|
|
|
2011-04-15 07:18:47 +02:00
|
|
|
// Delegates to an inferior filter chooser for further processing on this
|
2011-02-18 22:51:29 +01:00
|
|
|
// group of instructions whose segment values are variable.
|
|
|
|
FilterChooserMap.insert(std::pair<unsigned, FilterChooser*>(
|
|
|
|
(unsigned)-1,
|
|
|
|
new FilterChooser(Owner->AllInstructions,
|
|
|
|
VariableInstructions,
|
|
|
|
Owner->Operands,
|
|
|
|
BitValueArray,
|
|
|
|
*Owner)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
// No need to recurse for a singleton filtered instruction.
|
|
|
|
// See also Filter::emit().
|
|
|
|
if (getNumFiltered() == 1) {
|
|
|
|
//Owner->SingletonExists(LastOpcFiltered);
|
|
|
|
assert(FilterChooserMap.size() == 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, create sub choosers.
|
|
|
|
for (mapIterator = FilteredInstructions.begin();
|
|
|
|
mapIterator != FilteredInstructions.end();
|
|
|
|
mapIterator++) {
|
|
|
|
|
|
|
|
// Marks all the segment positions with either BIT_TRUE or BIT_FALSE.
|
|
|
|
for (bitIndex = 0; bitIndex < NumBits; bitIndex++) {
|
|
|
|
if (mapIterator->first & (1ULL << bitIndex))
|
|
|
|
BitValueArray[StartBit + bitIndex] = BIT_TRUE;
|
|
|
|
else
|
|
|
|
BitValueArray[StartBit + bitIndex] = BIT_FALSE;
|
|
|
|
}
|
|
|
|
|
2011-04-15 07:18:47 +02:00
|
|
|
// Delegates to an inferior filter chooser for further processing on this
|
2011-02-18 22:51:29 +01:00
|
|
|
// category of instructions.
|
|
|
|
FilterChooserMap.insert(std::pair<unsigned, FilterChooser*>(
|
|
|
|
mapIterator->first,
|
|
|
|
new FilterChooser(Owner->AllInstructions,
|
|
|
|
mapIterator->second,
|
|
|
|
Owner->Operands,
|
|
|
|
BitValueArray,
|
|
|
|
*Owner)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit code to decode instructions given a segment or segments of bits.
|
|
|
|
void Filter::emit(raw_ostream &o, unsigned &Indentation) {
|
|
|
|
o.indent(Indentation) << "// Check Inst{";
|
|
|
|
|
|
|
|
if (NumBits > 1)
|
|
|
|
o << (StartBit + NumBits - 1) << '-';
|
|
|
|
|
|
|
|
o << StartBit << "} ...\n";
|
|
|
|
|
2011-07-19 23:06:00 +02:00
|
|
|
o.indent(Indentation) << "switch (fieldFromInstruction" << Owner->BitWidth
|
|
|
|
<< "(insn, " << StartBit << ", "
|
|
|
|
<< NumBits << ")) {\n";
|
2011-02-18 22:51:29 +01:00
|
|
|
|
|
|
|
std::map<unsigned, FilterChooser*>::iterator filterIterator;
|
|
|
|
|
|
|
|
bool DefaultCase = false;
|
|
|
|
for (filterIterator = FilterChooserMap.begin();
|
|
|
|
filterIterator != FilterChooserMap.end();
|
|
|
|
filterIterator++) {
|
|
|
|
|
|
|
|
// Field value -1 implies a non-empty set of variable instructions.
|
|
|
|
// See also recurse().
|
|
|
|
if (filterIterator->first == (unsigned)-1) {
|
|
|
|
DefaultCase = true;
|
|
|
|
|
|
|
|
o.indent(Indentation) << "default:\n";
|
|
|
|
o.indent(Indentation) << " break; // fallthrough\n";
|
|
|
|
|
|
|
|
// Closing curly brace for the switch statement.
|
|
|
|
// This is unconventional because we want the default processing to be
|
|
|
|
// performed for the fallthrough cases as well, i.e., when the "cases"
|
|
|
|
// did not prove a decoded instruction.
|
|
|
|
o.indent(Indentation) << "}\n";
|
|
|
|
|
|
|
|
} else
|
|
|
|
o.indent(Indentation) << "case " << filterIterator->first << ":\n";
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
if (!DefaultCase) { ++Indentation; ++Indentation; }
|
|
|
|
|
|
|
|
bool finished = filterIterator->second->emit(o, Indentation);
|
|
|
|
// For top level default case, there's no need for a break statement.
|
|
|
|
if (Owner->isTopLevel() && DefaultCase)
|
|
|
|
break;
|
|
|
|
if (!finished)
|
|
|
|
o.indent(Indentation) << "break;\n";
|
|
|
|
|
|
|
|
if (!DefaultCase) { --Indentation; --Indentation; }
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is no default case, we still need to supply a closing brace.
|
|
|
|
if (!DefaultCase) {
|
|
|
|
// Closing curly brace for the switch statement.
|
|
|
|
o.indent(Indentation) << "}\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the number of fanout produced by the filter. More fanout implies
|
|
|
|
// the filter distinguishes more categories of instructions.
|
|
|
|
unsigned Filter::usefulness() const {
|
|
|
|
if (VariableInstructions.size())
|
|
|
|
return FilteredInstructions.size();
|
|
|
|
else
|
|
|
|
return FilteredInstructions.size() + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////
|
|
|
|
// //
|
|
|
|
// Filterchooser Implementation //
|
|
|
|
// //
|
|
|
|
//////////////////////////////////
|
|
|
|
|
|
|
|
// Emit the top level typedef and decodeInstruction() function.
|
2011-07-19 23:06:00 +02:00
|
|
|
void FilterChooser::emitTop(raw_ostream &o, unsigned Indentation,
|
|
|
|
std::string Namespace) {
|
2011-02-18 22:51:29 +01:00
|
|
|
o.indent(Indentation) <<
|
2011-08-17 19:44:15 +02:00
|
|
|
"static MCDisassembler::DecodeStatus decode" << Namespace << "Instruction" << BitWidth
|
2011-07-19 23:06:00 +02:00
|
|
|
<< "(MCInst &MI, uint" << BitWidth << "_t insn, uint64_t Address, "
|
2011-09-07 21:42:28 +02:00
|
|
|
<< "const void *Decoder, const MCSubtargetInfo &STI) {\n";
|
2011-10-17 18:56:47 +02:00
|
|
|
o.indent(Indentation) << " unsigned tmp = 0;\n";
|
|
|
|
o.indent(Indentation) << " (void)tmp;\n";
|
|
|
|
o.indent(Indentation) << Emitter->Locals << "\n";
|
2011-10-01 04:47:54 +02:00
|
|
|
o.indent(Indentation) << " uint64_t Bits = STI.getFeatureBits();\n";
|
2011-10-17 18:56:47 +02:00
|
|
|
o.indent(Indentation) << " (void)Bits;\n";
|
2011-02-18 22:51:29 +01:00
|
|
|
|
|
|
|
++Indentation; ++Indentation;
|
|
|
|
// Emits code to decode the instructions.
|
|
|
|
emit(o, Indentation);
|
|
|
|
|
|
|
|
o << '\n';
|
2011-08-17 19:44:15 +02:00
|
|
|
o.indent(Indentation) << "return " << Emitter->ReturnFail << ";\n";
|
2011-02-18 22:51:29 +01:00
|
|
|
--Indentation; --Indentation;
|
|
|
|
|
|
|
|
o.indent(Indentation) << "}\n";
|
|
|
|
|
|
|
|
o << '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
unsigned StartBit, unsigned NumBits) const {
|
|
|
|
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,
|
2011-07-19 23:06:00 +02:00
|
|
|
std::vector<bit_value_t> &filter) {
|
2011-02-18 22:51:29 +01:00
|
|
|
unsigned bitIndex;
|
|
|
|
|
2011-07-19 23:06:00 +02:00
|
|
|
for (bitIndex = BitWidth; bitIndex > 0; bitIndex--) {
|
2011-02-18 22:51:29 +01: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.
|
|
|
|
void FilterChooser::dumpStack(raw_ostream &o, const char *prefix) {
|
|
|
|
FilterChooser *current = this;
|
|
|
|
|
|
|
|
while (current) {
|
|
|
|
o << prefix;
|
|
|
|
dumpFilterArray(o, current->FilterBitValues);
|
|
|
|
o << '\n';
|
|
|
|
current = current->Parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called from Filter::recurse() when singleton exists. For debug purpose.
|
|
|
|
void FilterChooser::SingletonExists(unsigned Opc) {
|
|
|
|
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");
|
|
|
|
for (unsigned i = 0; i < Opcodes.size(); i++) {
|
|
|
|
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,
|
|
|
|
std::vector<unsigned> &EndBits, std::vector<uint64_t> &FieldVals,
|
|
|
|
insn_t &Insn) {
|
|
|
|
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 23:06:00 +02:00
|
|
|
for (unsigned i = 0; i < BitWidth; ++i) {
|
2011-02-18 22:51:29 +01:00
|
|
|
Val = Value(Insn[i]);
|
|
|
|
bool Filtered = PositionFiltered(i);
|
|
|
|
switch (State) {
|
2012-02-05 08:21:30 +01:00
|
|
|
default: llvm_unreachable("Unreachable code!");
|
2011-02-18 22:51:29 +01: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 23:06:00 +02:00
|
|
|
EndBits.push_back(BitWidth - 1);
|
2011-02-18 22:51:29 +01:00
|
|
|
FieldVals.push_back(FieldVal);
|
|
|
|
++Num;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(StartBits.size() == Num && EndBits.size() == Num &&
|
|
|
|
FieldVals.size() == Num);
|
|
|
|
return Num;
|
|
|
|
}
|
|
|
|
|
2011-07-28 23:54:31 +02:00
|
|
|
void FilterChooser::emitBinaryParser(raw_ostream &o, unsigned &Indentation,
|
|
|
|
OperandInfo &OpInfo) {
|
|
|
|
std::string &Decoder = OpInfo.Decoder;
|
|
|
|
|
|
|
|
if (OpInfo.numFields() == 1) {
|
|
|
|
OperandInfo::iterator OI = OpInfo.begin();
|
|
|
|
o.indent(Indentation) << " tmp = fieldFromInstruction" << BitWidth
|
|
|
|
<< "(insn, " << OI->Base << ", " << OI->Width
|
|
|
|
<< ");\n";
|
|
|
|
} else {
|
|
|
|
o.indent(Indentation) << " tmp = 0;\n";
|
|
|
|
for (OperandInfo::iterator OI = OpInfo.begin(), OE = OpInfo.end();
|
|
|
|
OI != OE; ++OI) {
|
|
|
|
o.indent(Indentation) << " tmp |= (fieldFromInstruction" << BitWidth
|
2011-09-08 07:23:14 +02:00
|
|
|
<< "(insn, " << OI->Base << ", " << OI->Width
|
2011-07-28 23:54:31 +02:00
|
|
|
<< ") << " << OI->Offset << ");\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Decoder != "")
|
2011-08-17 19:44:15 +02:00
|
|
|
o.indent(Indentation) << " " << Emitter->GuardPrefix << Decoder
|
|
|
|
<< "(MI, tmp, Address, Decoder)" << Emitter->GuardPostfix << "\n";
|
2011-07-28 23:54:31 +02:00
|
|
|
else
|
|
|
|
o.indent(Indentation) << " MI.addOperand(MCOperand::CreateImm(tmp));\n";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-09-07 21:42:28 +02:00
|
|
|
static void emitSinglePredicateMatch(raw_ostream &o, StringRef str,
|
|
|
|
std::string PredicateNamespace) {
|
2011-09-08 07:25:49 +02:00
|
|
|
if (str[0] == '!')
|
|
|
|
o << "!(Bits & " << PredicateNamespace << "::"
|
|
|
|
<< str.slice(1,str.size()) << ")";
|
2011-09-07 21:42:28 +02:00
|
|
|
else
|
2011-09-08 07:25:49 +02:00
|
|
|
o << "(Bits & " << PredicateNamespace << "::" << str << ")";
|
2011-09-07 21:42:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FilterChooser::emitPredicateMatch(raw_ostream &o, unsigned &Indentation,
|
|
|
|
unsigned Opc) {
|
|
|
|
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;
|
|
|
|
|
|
|
|
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 07:23:14 +02:00
|
|
|
}
|
2011-09-07 21:42:28 +02:00
|
|
|
|
2012-02-09 11:56:31 +01:00
|
|
|
void FilterChooser::emitSoftFailCheck(raw_ostream &o, unsigned Indentation, unsigned Opc) {
|
|
|
|
BitsInit *SFBits = AllInstructions[Opc]->TheDef->getValueAsBitsInit("SoftFail");
|
|
|
|
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();
|
|
|
|
errs() << "SoftFail Conflict: bit SoftFail{" << i << "} in "
|
|
|
|
<< Name
|
|
|
|
<< " is set but Inst{" << i <<"} is unset!\n"
|
|
|
|
<< " - You can only mark a bit as SoftFail if it is fully defined"
|
|
|
|
<< " (1/0 - not '?') in Inst\n";
|
|
|
|
o << "#error SoftFail Conflict, " << Name << "::SoftFail{" << i
|
|
|
|
<< "} set but Inst{" << i << "} undefined!\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NeedPositiveMask = PositiveMask.getBoolValue();
|
|
|
|
bool NeedNegativeMask = NegativeMask.getBoolValue();
|
|
|
|
|
|
|
|
if (!NeedPositiveMask && !NeedNegativeMask)
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::string PositiveMaskStr = PositiveMask.toString(16, /*signed=*/false);
|
|
|
|
std::string NegativeMaskStr = NegativeMask.toString(16, /*signed=*/false);
|
|
|
|
StringRef BitExt = "";
|
|
|
|
if (BitWidth > 32)
|
|
|
|
BitExt = "ULL";
|
|
|
|
|
|
|
|
o.indent(Indentation) << "if (";
|
|
|
|
if (NeedPositiveMask)
|
|
|
|
o << "insn & 0x" << PositiveMaskStr << BitExt;
|
|
|
|
if (NeedPositiveMask && NeedNegativeMask)
|
|
|
|
o << " || ";
|
|
|
|
if (NeedNegativeMask)
|
|
|
|
o << "~insn & 0x" << NegativeMaskStr << BitExt;
|
|
|
|
o << ")\n";
|
|
|
|
o.indent(Indentation+2) << "S = MCDisassembler::SoftFail;\n";
|
|
|
|
}
|
|
|
|
|
2011-02-18 22:51:29 +01:00
|
|
|
// Emits code to decode the singleton. Return true if we have matched all the
|
|
|
|
// well-known bits.
|
|
|
|
bool FilterChooser::emitSingletonDecoder(raw_ostream &o, unsigned &Indentation,
|
|
|
|
unsigned Opc) {
|
|
|
|
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();
|
|
|
|
unsigned I, NumBits;
|
|
|
|
|
|
|
|
// If we have matched all the well-known bits, just issue a return.
|
|
|
|
if (Size == 0) {
|
2011-09-07 21:42:28 +02:00
|
|
|
o.indent(Indentation) << "if (";
|
2011-09-08 23:00:31 +02:00
|
|
|
if (!emitPredicateMatch(o, Indentation, Opc))
|
|
|
|
o << "1";
|
2011-09-07 21:42:28 +02:00
|
|
|
o << ") {\n";
|
2012-02-09 11:56:31 +01:00
|
|
|
emitSoftFailCheck(o, Indentation+2, Opc);
|
2011-02-18 22:51:29 +01:00
|
|
|
o.indent(Indentation) << " MI.setOpcode(" << Opc << ");\n";
|
|
|
|
std::vector<OperandInfo>& InsnOperands = Operands[Opc];
|
|
|
|
for (std::vector<OperandInfo>::iterator
|
|
|
|
I = InsnOperands.begin(), E = InsnOperands.end(); I != E; ++I) {
|
|
|
|
// If a custom instruction decoder was specified, use that.
|
2011-07-28 23:54:31 +02:00
|
|
|
if (I->numFields() == 0 && I->Decoder.size()) {
|
2011-08-17 19:44:15 +02:00
|
|
|
o.indent(Indentation) << " " << Emitter->GuardPrefix << I->Decoder
|
|
|
|
<< "(MI, insn, Address, Decoder)" << Emitter->GuardPostfix << "\n";
|
2011-02-18 22:51:29 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-07-28 23:54:31 +02:00
|
|
|
emitBinaryParser(o, Indentation, *I);
|
2011-02-18 22:51:29 +01:00
|
|
|
}
|
|
|
|
|
2011-08-17 19:44:15 +02:00
|
|
|
o.indent(Indentation) << " return " << Emitter->ReturnOK << "; // " << nameWithID(Opc)
|
2011-02-18 22:51:29 +01:00
|
|
|
<< '\n';
|
2011-09-07 21:42:28 +02:00
|
|
|
o.indent(Indentation) << "}\n"; // Closing predicate block.
|
2011-02-18 22:51:29 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, there are more decodings to be done!
|
|
|
|
|
|
|
|
// Emit code to match the island(s) for the singleton.
|
|
|
|
o.indent(Indentation) << "// Check ";
|
|
|
|
|
|
|
|
for (I = Size; I != 0; --I) {
|
|
|
|
o << "Inst{" << EndBits[I-1] << '-' << StartBits[I-1] << "} ";
|
|
|
|
if (I > 1)
|
2011-09-07 21:42:28 +02:00
|
|
|
o << " && ";
|
2011-02-18 22:51:29 +01:00
|
|
|
else
|
|
|
|
o << "for singleton decoding...\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
o.indent(Indentation) << "if (";
|
2011-09-08 10:12:01 +02:00
|
|
|
if (emitPredicateMatch(o, Indentation, Opc)) {
|
2011-09-07 21:42:28 +02:00
|
|
|
o << " &&\n";
|
|
|
|
o.indent(Indentation+4);
|
|
|
|
}
|
2011-02-18 22:51:29 +01:00
|
|
|
|
|
|
|
for (I = Size; I != 0; --I) {
|
|
|
|
NumBits = EndBits[I-1] - StartBits[I-1] + 1;
|
2011-07-19 23:06:00 +02:00
|
|
|
o << "fieldFromInstruction" << BitWidth << "(insn, "
|
|
|
|
<< StartBits[I-1] << ", " << NumBits
|
2011-02-18 22:51:29 +01:00
|
|
|
<< ") == " << FieldVals[I-1];
|
|
|
|
if (I > 1)
|
|
|
|
o << " && ";
|
|
|
|
else
|
|
|
|
o << ") {\n";
|
|
|
|
}
|
2012-02-09 11:56:31 +01:00
|
|
|
emitSoftFailCheck(o, Indentation+2, Opc);
|
2011-02-18 22:51:29 +01:00
|
|
|
o.indent(Indentation) << " MI.setOpcode(" << Opc << ");\n";
|
|
|
|
std::vector<OperandInfo>& InsnOperands = Operands[Opc];
|
|
|
|
for (std::vector<OperandInfo>::iterator
|
|
|
|
I = InsnOperands.begin(), E = InsnOperands.end(); I != E; ++I) {
|
|
|
|
// If a custom instruction decoder was specified, use that.
|
2011-07-28 23:54:31 +02:00
|
|
|
if (I->numFields() == 0 && I->Decoder.size()) {
|
2011-08-17 19:44:15 +02:00
|
|
|
o.indent(Indentation) << " " << Emitter->GuardPrefix << I->Decoder
|
|
|
|
<< "(MI, insn, Address, Decoder)" << Emitter->GuardPostfix << "\n";
|
2011-02-18 22:51:29 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-07-28 23:54:31 +02:00
|
|
|
emitBinaryParser(o, Indentation, *I);
|
2011-02-18 22:51:29 +01:00
|
|
|
}
|
2011-08-17 19:44:15 +02:00
|
|
|
o.indent(Indentation) << " return " << Emitter->ReturnOK << "; // " << nameWithID(Opc)
|
2011-02-18 22:51:29 +01:00
|
|
|
<< '\n';
|
|
|
|
o.indent(Indentation) << "}\n";
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emits code to decode the singleton, and then to decode the rest.
|
|
|
|
void FilterChooser::emitSingletonDecoder(raw_ostream &o, unsigned &Indentation,
|
|
|
|
Filter &Best) {
|
|
|
|
|
|
|
|
unsigned Opc = Best.getSingletonOpc();
|
|
|
|
|
|
|
|
emitSingletonDecoder(o, Indentation, Opc);
|
|
|
|
|
|
|
|
// Emit code for the rest.
|
|
|
|
o.indent(Indentation) << "else\n";
|
|
|
|
|
|
|
|
Indentation += 2;
|
|
|
|
Best.getVariableFC().emit(o, Indentation);
|
|
|
|
Indentation -= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assign a single filter and run with it. Top level API client can initialize
|
|
|
|
// with a single filter to start the filtering process.
|
|
|
|
void FilterChooser::runSingleFilter(FilterChooser &owner, unsigned startBit,
|
|
|
|
unsigned numBit, bool mixed) {
|
|
|
|
Filters.clear();
|
|
|
|
Filter F(*this, startBit, numBit, true);
|
|
|
|
Filters.push_back(F);
|
|
|
|
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,
|
|
|
|
unsigned BitIndex, bool AllowMixed) {
|
|
|
|
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.
|
|
|
|
runSingleFilter(*this, StartBits[0], EndBits[0] - StartBits[0] + 1,
|
|
|
|
true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned BitIndex, InsnIndex;
|
|
|
|
|
|
|
|
// 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 23:06:00 +02:00
|
|
|
std::vector<bitAttr_t> bitAttrs;
|
2011-02-18 22:51:29 +01: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 23:06:00 +02:00
|
|
|
for (BitIndex = 0; BitIndex < BitWidth; ++BitIndex)
|
2011-02-18 22:51:29 +01:00
|
|
|
if (FilterBitValues[BitIndex] == BIT_TRUE ||
|
|
|
|
FilterBitValues[BitIndex] == BIT_FALSE)
|
2011-07-19 23:06:00 +02:00
|
|
|
bitAttrs.push_back(ATTR_FILTERED);
|
2011-02-18 22:51:29 +01:00
|
|
|
else
|
2011-07-19 23:06:00 +02:00
|
|
|
bitAttrs.push_back(ATTR_NONE);
|
2011-02-18 22:51:29 +01:00
|
|
|
|
|
|
|
for (InsnIndex = 0; InsnIndex < numInstructions; ++InsnIndex) {
|
|
|
|
insn_t insn;
|
|
|
|
|
|
|
|
insnWithID(insn, Opcodes[InsnIndex]);
|
|
|
|
|
2011-07-19 23:06:00 +02:00
|
|
|
for (BitIndex = 0; BitIndex < BitWidth; ++BitIndex) {
|
2011-02-18 22:51:29 +01: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;
|
|
|
|
|
2011-07-19 23:06:00 +02:00
|
|
|
for (BitIndex = 0; BitIndex < BitWidth; BitIndex++) {
|
2011-02-18 22:51:29 +01: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 08:21:30 +01:00
|
|
|
llvm_unreachable("Unexpected bitAttr!");
|
2011-02-18 22:51:29 +01: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 08:21:30 +01:00
|
|
|
llvm_unreachable("Unexpected bitAttr!");
|
2011-02-18 22:51:29 +01: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 08:21:30 +01:00
|
|
|
llvm_unreachable("Unexpected bitAttr!");
|
2011-02-18 22:51:29 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ATTR_ALL_UNSET:
|
2012-02-05 08:21:30 +01:00
|
|
|
llvm_unreachable("regionAttr state machine has no ATTR_UNSET state");
|
2011-02-18 22:51:29 +01:00
|
|
|
case ATTR_FILTERED:
|
2012-02-05 08:21:30 +01:00
|
|
|
llvm_unreachable("regionAttr state machine has no ATTR_FILTERED state");
|
2011-02-18 22:51:29 +01: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emits code to decode our share of instructions. Returns true if the
|
|
|
|
// emitted code causes a return, which occurs if we know how to decode
|
|
|
|
// the instruction at this level or the instruction is not decodeable.
|
|
|
|
bool FilterChooser::emit(raw_ostream &o, unsigned &Indentation) {
|
|
|
|
if (Opcodes.size() == 1)
|
|
|
|
// There is only one instruction in the set, which is great!
|
|
|
|
// Call emitSingletonDecoder() to see whether there are any remaining
|
|
|
|
// encodings bits.
|
|
|
|
return emitSingletonDecoder(o, Indentation, Opcodes[0]);
|
|
|
|
|
|
|
|
// Choose the best filter to do the decodings!
|
|
|
|
if (BestIndex != -1) {
|
|
|
|
Filter &Best = bestFilter();
|
|
|
|
if (Best.getNumFiltered() == 1)
|
|
|
|
emitSingletonDecoder(o, Indentation, Best);
|
|
|
|
else
|
|
|
|
bestFilter().emit(o, Indentation);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't know how to decode these instructions! Return 0 and dump the
|
|
|
|
// conflict set!
|
|
|
|
o.indent(Indentation) << "return 0;" << " // Conflict set: ";
|
|
|
|
for (int i = 0, N = Opcodes.size(); i < N; ++i) {
|
|
|
|
o << nameWithID(Opcodes[i]);
|
|
|
|
if (i < (N - 1))
|
|
|
|
o << ", ";
|
|
|
|
else
|
|
|
|
o << '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print out useful conflict information for postmortem analysis.
|
|
|
|
errs() << "Decoding Conflict:\n";
|
|
|
|
|
|
|
|
dumpStack(errs(), "\t\t");
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < Opcodes.size(); i++) {
|
|
|
|
const std::string &Name = nameWithID(Opcodes[i]);
|
|
|
|
|
|
|
|
errs() << '\t' << Name << " ";
|
|
|
|
dumpBits(errs(),
|
|
|
|
getBitsField(*AllInstructions[Opcodes[i]]->TheDef, "Inst"));
|
|
|
|
errs() << '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-07-19 23:06:00 +02:00
|
|
|
static bool populateInstruction(const CodeGenInstruction &CGI,
|
|
|
|
unsigned Opc,
|
|
|
|
std::map<unsigned, std::vector<OperandInfo> >& Operands){
|
2011-02-18 22:51:29 +01: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.
|
|
|
|
//
|
|
|
|
// This also removes pseudo instructions from considerations of disassembly,
|
|
|
|
// which is a better design and less fragile than the name matchings.
|
|
|
|
// Ignore "asm parser only" instructions.
|
2011-03-14 21:58:49 +01:00
|
|
|
if (Def.getValueAsBit("isAsmParserOnly") ||
|
|
|
|
Def.getValueAsBit("isCodeGenOnly"))
|
2011-02-18 22:51:29 +01:00
|
|
|
return false;
|
|
|
|
|
2011-07-30 00:43:06 +02:00
|
|
|
BitsInit &Bits = getBitsField(Def, "Inst");
|
2011-07-06 23:33:38 +02:00
|
|
|
if (Bits.allInComplete()) return false;
|
|
|
|
|
2011-02-18 22:51:29 +01: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 23:54:31 +02:00
|
|
|
InsnOperands.push_back(OperandInfo(InstDecoder));
|
2011-02-18 22:51:29 +01: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-30 00:43:06 +02:00
|
|
|
std::vector<std::pair<Init*, std::string> > InOutOperands;
|
|
|
|
DagInit *Out = Def.getValueAsDag("OutOperandList");
|
|
|
|
DagInit *In = Def.getValueAsDag("InOperandList");
|
2011-02-18 22:51:29 +01: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-29 01:56:20 +02:00
|
|
|
// Search for tied operands, so that we can correctly instantiate
|
|
|
|
// operands that are not explicitly represented in the encoding.
|
2011-07-29 20:28:52 +02:00
|
|
|
std::map<std::string, std::string> TiedNames;
|
2011-07-29 01:56:20 +02:00
|
|
|
for (unsigned i = 0; i < CGI.Operands.size(); ++i) {
|
|
|
|
int tiedTo = CGI.Operands[i].getTiedRegister();
|
2011-07-29 20:28:52 +02:00
|
|
|
if (tiedTo != -1) {
|
|
|
|
TiedNames[InOutOperands[i].second] = InOutOperands[tiedTo].second;
|
|
|
|
TiedNames[InOutOperands[tiedTo].second] = InOutOperands[i].second;
|
|
|
|
}
|
2011-07-29 01:56:20 +02:00
|
|
|
}
|
|
|
|
|
2011-02-18 22:51:29 +01:00
|
|
|
// For each operand, see if we can figure out where it is encoded.
|
2011-07-30 00:43:06 +02:00
|
|
|
for (std::vector<std::pair<Init*, std::string> >::iterator
|
2011-02-18 22:51:29 +01:00
|
|
|
NI = InOutOperands.begin(), NE = InOutOperands.end(); NI != NE; ++NI) {
|
|
|
|
std::string Decoder = "";
|
|
|
|
|
2011-07-28 23:54:31 +02: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.
|
2011-07-30 00:43:06 +02:00
|
|
|
TypedInit *TI = dynamic_cast<TypedInit*>(NI->first);
|
2011-07-28 23:54:31 +02:00
|
|
|
RecordRecTy *Type = dynamic_cast<RecordRecTy*>(TI->getType());
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
RecordVal *DecoderString = TypeRecord->getValue("DecoderMethod");
|
2011-07-30 00:43:06 +02:00
|
|
|
StringInit *String = DecoderString ?
|
|
|
|
dynamic_cast<StringInit*>(DecoderString->getValue()) : 0;
|
2011-07-28 23:54:31 +02:00
|
|
|
if (!isReg && String && String->getValue() != "")
|
|
|
|
Decoder = String->getValue();
|
|
|
|
|
|
|
|
OperandInfo OpInfo(Decoder);
|
|
|
|
unsigned Base = ~0U;
|
|
|
|
unsigned Width = 0;
|
|
|
|
unsigned Offset = 0;
|
|
|
|
|
2011-02-18 22:51:29 +01:00
|
|
|
for (unsigned bi = 0; bi < Bits.getNumBits(); ++bi) {
|
2011-08-02 00:45:43 +02:00
|
|
|
VarInit *Var = 0;
|
2011-07-30 00:43:06 +02:00
|
|
|
VarBitInit *BI = dynamic_cast<VarBitInit*>(Bits.getBit(bi));
|
2011-08-02 00:45:43 +02:00
|
|
|
if (BI)
|
|
|
|
Var = dynamic_cast<VarInit*>(BI->getVariable());
|
|
|
|
else
|
|
|
|
Var = dynamic_cast<VarInit*>(Bits.getBit(bi));
|
|
|
|
|
|
|
|
if (!Var) {
|
2011-07-28 23:54:31 +02:00
|
|
|
if (Base != ~0U) {
|
|
|
|
OpInfo.addField(Base, Width, Offset);
|
|
|
|
Base = ~0U;
|
|
|
|
Width = 0;
|
|
|
|
Offset = 0;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2011-02-18 22:51:29 +01:00
|
|
|
|
2011-07-29 01:56:20 +02:00
|
|
|
if (Var->getName() != NI->second &&
|
2011-07-29 20:28:52 +02:00
|
|
|
Var->getName() != TiedNames[NI->second]) {
|
2011-07-28 23:54:31 +02:00
|
|
|
if (Base != ~0U) {
|
|
|
|
OpInfo.addField(Base, Width, Offset);
|
|
|
|
Base = ~0U;
|
|
|
|
Width = 0;
|
|
|
|
Offset = 0;
|
|
|
|
}
|
|
|
|
continue;
|
2011-02-18 22:51:29 +01:00
|
|
|
}
|
|
|
|
|
2011-07-28 23:54:31 +02:00
|
|
|
if (Base == ~0U) {
|
|
|
|
Base = bi;
|
|
|
|
Width = 1;
|
2011-08-02 00:45:43 +02:00
|
|
|
Offset = BI ? BI->getBitNum() : 0;
|
|
|
|
} else if (BI && BI->getBitNum() != Offset + Width) {
|
2011-07-30 01:01:18 +02:00
|
|
|
OpInfo.addField(Base, Width, Offset);
|
|
|
|
Base = bi;
|
|
|
|
Width = 1;
|
|
|
|
Offset = BI->getBitNum();
|
2011-07-28 23:54:31 +02:00
|
|
|
} else {
|
|
|
|
++Width;
|
2011-02-18 22:51:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-28 23:54:31 +02:00
|
|
|
if (Base != ~0U)
|
|
|
|
OpInfo.addField(Base, Width, Offset);
|
|
|
|
|
|
|
|
if (OpInfo.numFields() > 0)
|
|
|
|
InsnOperands.push_back(OpInfo);
|
2011-02-18 22:51:29 +01: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;
|
|
|
|
}
|
|
|
|
|
2011-07-19 23:06:00 +02:00
|
|
|
static void emitHelper(llvm::raw_ostream &o, unsigned BitWidth) {
|
|
|
|
unsigned Indentation = 0;
|
|
|
|
std::string WidthStr = "uint" + utostr(BitWidth) + "_t";
|
2011-02-18 22:51:29 +01:00
|
|
|
|
2011-07-19 23:06:00 +02:00
|
|
|
o << '\n';
|
|
|
|
|
|
|
|
o.indent(Indentation) << "static " << WidthStr <<
|
|
|
|
" fieldFromInstruction" << BitWidth <<
|
|
|
|
"(" << WidthStr <<" insn, unsigned startBit, unsigned numBits)\n";
|
|
|
|
|
|
|
|
o.indent(Indentation) << "{\n";
|
|
|
|
|
|
|
|
++Indentation; ++Indentation;
|
|
|
|
o.indent(Indentation) << "assert(startBit + numBits <= " << BitWidth
|
|
|
|
<< " && \"Instruction field out of bounds!\");\n";
|
|
|
|
o << '\n';
|
|
|
|
o.indent(Indentation) << WidthStr << " fieldMask;\n";
|
|
|
|
o << '\n';
|
|
|
|
o.indent(Indentation) << "if (numBits == " << BitWidth << ")\n";
|
|
|
|
|
|
|
|
++Indentation; ++Indentation;
|
|
|
|
o.indent(Indentation) << "fieldMask = (" << WidthStr << ")-1;\n";
|
|
|
|
--Indentation; --Indentation;
|
|
|
|
|
|
|
|
o.indent(Indentation) << "else\n";
|
|
|
|
|
|
|
|
++Indentation; ++Indentation;
|
|
|
|
o.indent(Indentation) << "fieldMask = ((1 << numBits) - 1) << startBit;\n";
|
|
|
|
--Indentation; --Indentation;
|
|
|
|
|
|
|
|
o << '\n';
|
|
|
|
o.indent(Indentation) << "return (insn & fieldMask) >> startBit;\n";
|
|
|
|
--Indentation; --Indentation;
|
|
|
|
|
|
|
|
o.indent(Indentation) << "}\n";
|
|
|
|
|
|
|
|
o << '\n';
|
2011-02-18 22:51:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Emits disassembler code for instruction decoding.
|
|
|
|
void FixedLenDecoderEmitter::run(raw_ostream &o)
|
|
|
|
{
|
|
|
|
o << "#include \"llvm/MC/MCInst.h\"\n";
|
|
|
|
o << "#include \"llvm/Support/DataTypes.h\"\n";
|
|
|
|
o << "#include <assert.h>\n";
|
|
|
|
o << '\n';
|
|
|
|
o << "namespace llvm {\n\n";
|
|
|
|
|
2011-07-19 23:06:00 +02:00
|
|
|
// Parameterize the decoders based on namespace and instruction width.
|
2011-02-18 22:51:29 +01:00
|
|
|
NumberedInstructions = Target.getInstructionsByEnumValue();
|
2011-07-19 23:06:00 +02:00
|
|
|
std::map<std::pair<std::string, unsigned>,
|
|
|
|
std::vector<unsigned> > OpcMap;
|
|
|
|
std::map<unsigned, std::vector<OperandInfo> > Operands;
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < NumberedInstructions.size(); ++i) {
|
|
|
|
const CodeGenInstruction *Inst = NumberedInstructions[i];
|
|
|
|
Record *Def = Inst->TheDef;
|
|
|
|
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) {
|
|
|
|
if (populateInstruction(*Inst, i, Operands)) {
|
|
|
|
OpcMap[std::make_pair(DecoderNamespace, Size)].push_back(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::set<unsigned> Sizes;
|
|
|
|
for (std::map<std::pair<std::string, unsigned>,
|
|
|
|
std::vector<unsigned> >::iterator
|
|
|
|
I = OpcMap.begin(), E = OpcMap.end(); I != E; ++I) {
|
|
|
|
// If we haven't visited this instruction width before, emit the
|
|
|
|
// helper method to extract fields.
|
|
|
|
if (!Sizes.count(I->first.second)) {
|
|
|
|
emitHelper(o, 8*I->first.second);
|
|
|
|
Sizes.insert(I->first.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit the decoder for this namespace+width combination.
|
|
|
|
FilterChooser FC(NumberedInstructions, I->second, Operands,
|
2011-08-17 19:44:15 +02:00
|
|
|
8*I->first.second, this);
|
2011-07-19 23:06:00 +02:00
|
|
|
FC.emitTop(o, 0, I->first.first);
|
|
|
|
}
|
2011-02-18 22:51:29 +01:00
|
|
|
|
|
|
|
o << "\n} // End llvm namespace \n";
|
|
|
|
}
|