FixedLenDecoderEmitter.cpp 91 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560
  1. //===------------ FixedLenDecoderEmitter.cpp - Decoder Generator ----------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // It contains the tablegen backend that emits the decoder functions for
  10. // targets with fixed length instruction set.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "CodeGenInstruction.h"
  14. #include "CodeGenTarget.h"
  15. #include "InfoByHwMode.h"
  16. #include "llvm/ADT/APInt.h"
  17. #include "llvm/ADT/ArrayRef.h"
  18. #include "llvm/ADT/CachedHashString.h"
  19. #include "llvm/ADT/STLExtras.h"
  20. #include "llvm/ADT/SetVector.h"
  21. #include "llvm/ADT/SmallString.h"
  22. #include "llvm/ADT/Statistic.h"
  23. #include "llvm/ADT/StringExtras.h"
  24. #include "llvm/ADT/StringRef.h"
  25. #include "llvm/MC/MCFixedLenDisassembler.h"
  26. #include "llvm/Support/Casting.h"
  27. #include "llvm/Support/Debug.h"
  28. #include "llvm/Support/ErrorHandling.h"
  29. #include "llvm/Support/FormattedStream.h"
  30. #include "llvm/Support/LEB128.h"
  31. #include "llvm/Support/raw_ostream.h"
  32. #include "llvm/TableGen/Error.h"
  33. #include "llvm/TableGen/Record.h"
  34. #include <algorithm>
  35. #include <cassert>
  36. #include <cstddef>
  37. #include <cstdint>
  38. #include <map>
  39. #include <memory>
  40. #include <set>
  41. #include <string>
  42. #include <utility>
  43. #include <vector>
  44. using namespace llvm;
  45. #define DEBUG_TYPE "decoder-emitter"
  46. namespace {
  47. STATISTIC(NumEncodings, "Number of encodings considered");
  48. STATISTIC(NumEncodingsLackingDisasm, "Number of encodings without disassembler info");
  49. STATISTIC(NumInstructions, "Number of instructions considered");
  50. STATISTIC(NumEncodingsSupported, "Number of encodings supported");
  51. STATISTIC(NumEncodingsOmitted, "Number of encodings omitted");
  52. struct EncodingField {
  53. unsigned Base, Width, Offset;
  54. EncodingField(unsigned B, unsigned W, unsigned O)
  55. : Base(B), Width(W), Offset(O) { }
  56. };
  57. struct OperandInfo {
  58. std::vector<EncodingField> Fields;
  59. std::string Decoder;
  60. bool HasCompleteDecoder;
  61. uint64_t InitValue;
  62. OperandInfo(std::string D, bool HCD)
  63. : Decoder(std::move(D)), HasCompleteDecoder(HCD), InitValue(0) {}
  64. void addField(unsigned Base, unsigned Width, unsigned Offset) {
  65. Fields.push_back(EncodingField(Base, Width, Offset));
  66. }
  67. unsigned numFields() const { return Fields.size(); }
  68. typedef std::vector<EncodingField>::const_iterator const_iterator;
  69. const_iterator begin() const { return Fields.begin(); }
  70. const_iterator end() const { return Fields.end(); }
  71. };
  72. typedef std::vector<uint8_t> DecoderTable;
  73. typedef uint32_t DecoderFixup;
  74. typedef std::vector<DecoderFixup> FixupList;
  75. typedef std::vector<FixupList> FixupScopeList;
  76. typedef SmallSetVector<CachedHashString, 16> PredicateSet;
  77. typedef SmallSetVector<CachedHashString, 16> DecoderSet;
  78. struct DecoderTableInfo {
  79. DecoderTable Table;
  80. FixupScopeList FixupStack;
  81. PredicateSet Predicates;
  82. DecoderSet Decoders;
  83. };
  84. struct EncodingAndInst {
  85. const Record *EncodingDef;
  86. const CodeGenInstruction *Inst;
  87. StringRef HwModeName;
  88. EncodingAndInst(const Record *EncodingDef, const CodeGenInstruction *Inst,
  89. StringRef HwModeName = "")
  90. : EncodingDef(EncodingDef), Inst(Inst), HwModeName(HwModeName) {}
  91. };
  92. struct EncodingIDAndOpcode {
  93. unsigned EncodingID;
  94. unsigned Opcode;
  95. EncodingIDAndOpcode() : EncodingID(0), Opcode(0) {}
  96. EncodingIDAndOpcode(unsigned EncodingID, unsigned Opcode)
  97. : EncodingID(EncodingID), Opcode(Opcode) {}
  98. };
  99. raw_ostream &operator<<(raw_ostream &OS, const EncodingAndInst &Value) {
  100. if (Value.EncodingDef != Value.Inst->TheDef)
  101. OS << Value.EncodingDef->getName() << ":";
  102. OS << Value.Inst->TheDef->getName();
  103. return OS;
  104. }
  105. class FixedLenDecoderEmitter {
  106. RecordKeeper &RK;
  107. std::vector<EncodingAndInst> NumberedEncodings;
  108. public:
  109. // Defaults preserved here for documentation, even though they aren't
  110. // strictly necessary given the way that this is currently being called.
  111. FixedLenDecoderEmitter(RecordKeeper &R, std::string PredicateNamespace,
  112. std::string GPrefix = "if (",
  113. std::string GPostfix = " == MCDisassembler::Fail)",
  114. std::string ROK = "MCDisassembler::Success",
  115. std::string RFail = "MCDisassembler::Fail",
  116. std::string L = "")
  117. : RK(R), Target(R), PredicateNamespace(std::move(PredicateNamespace)),
  118. GuardPrefix(std::move(GPrefix)), GuardPostfix(std::move(GPostfix)),
  119. ReturnOK(std::move(ROK)), ReturnFail(std::move(RFail)),
  120. Locals(std::move(L)) {}
  121. // Emit the decoder state machine table.
  122. void emitTable(formatted_raw_ostream &o, DecoderTable &Table,
  123. unsigned Indentation, unsigned BitWidth,
  124. StringRef Namespace) const;
  125. void emitPredicateFunction(formatted_raw_ostream &OS,
  126. PredicateSet &Predicates,
  127. unsigned Indentation) const;
  128. void emitDecoderFunction(formatted_raw_ostream &OS,
  129. DecoderSet &Decoders,
  130. unsigned Indentation) const;
  131. // run - Output the code emitter
  132. void run(raw_ostream &o);
  133. private:
  134. CodeGenTarget Target;
  135. public:
  136. std::string PredicateNamespace;
  137. std::string GuardPrefix, GuardPostfix;
  138. std::string ReturnOK, ReturnFail;
  139. std::string Locals;
  140. };
  141. } // end anonymous namespace
  142. // The set (BIT_TRUE, BIT_FALSE, BIT_UNSET) represents a ternary logic system
  143. // for a bit value.
  144. //
  145. // BIT_UNFILTERED is used as the init value for a filter position. It is used
  146. // only for filter processings.
  147. typedef enum {
  148. BIT_TRUE, // '1'
  149. BIT_FALSE, // '0'
  150. BIT_UNSET, // '?'
  151. BIT_UNFILTERED // unfiltered
  152. } bit_value_t;
  153. static bool ValueSet(bit_value_t V) {
  154. return (V == BIT_TRUE || V == BIT_FALSE);
  155. }
  156. static bool ValueNotSet(bit_value_t V) {
  157. return (V == BIT_UNSET);
  158. }
  159. static int Value(bit_value_t V) {
  160. return ValueNotSet(V) ? -1 : (V == BIT_FALSE ? 0 : 1);
  161. }
  162. static bit_value_t bitFromBits(const BitsInit &bits, unsigned index) {
  163. if (BitInit *bit = dyn_cast<BitInit>(bits.getBit(index)))
  164. return bit->getValue() ? BIT_TRUE : BIT_FALSE;
  165. // The bit is uninitialized.
  166. return BIT_UNSET;
  167. }
  168. // Prints the bit value for each position.
  169. static void dumpBits(raw_ostream &o, const BitsInit &bits) {
  170. for (unsigned index = bits.getNumBits(); index > 0; --index) {
  171. switch (bitFromBits(bits, index - 1)) {
  172. case BIT_TRUE:
  173. o << "1";
  174. break;
  175. case BIT_FALSE:
  176. o << "0";
  177. break;
  178. case BIT_UNSET:
  179. o << "_";
  180. break;
  181. default:
  182. llvm_unreachable("unexpected return value from bitFromBits");
  183. }
  184. }
  185. }
  186. static BitsInit &getBitsField(const Record &def, StringRef str) {
  187. BitsInit *bits = def.getValueAsBitsInit(str);
  188. return *bits;
  189. }
  190. // Representation of the instruction to work on.
  191. typedef std::vector<bit_value_t> insn_t;
  192. namespace {
  193. static const uint64_t NO_FIXED_SEGMENTS_SENTINEL = -1ULL;
  194. class FilterChooser;
  195. /// Filter - Filter works with FilterChooser to produce the decoding tree for
  196. /// the ISA.
  197. ///
  198. /// It is useful to think of a Filter as governing the switch stmts of the
  199. /// decoding tree in a certain level. Each case stmt delegates to an inferior
  200. /// FilterChooser to decide what further decoding logic to employ, or in another
  201. /// words, what other remaining bits to look at. The FilterChooser eventually
  202. /// chooses a best Filter to do its job.
  203. ///
  204. /// This recursive scheme ends when the number of Opcodes assigned to the
  205. /// FilterChooser becomes 1 or if there is a conflict. A conflict happens when
  206. /// the Filter/FilterChooser combo does not know how to distinguish among the
  207. /// Opcodes assigned.
  208. ///
  209. /// An example of a conflict is
  210. ///
  211. /// Conflict:
  212. /// 111101000.00........00010000....
  213. /// 111101000.00........0001........
  214. /// 1111010...00........0001........
  215. /// 1111010...00....................
  216. /// 1111010.........................
  217. /// 1111............................
  218. /// ................................
  219. /// VST4q8a 111101000_00________00010000____
  220. /// VST4q8b 111101000_00________00010000____
  221. ///
  222. /// The Debug output shows the path that the decoding tree follows to reach the
  223. /// the conclusion that there is a conflict. VST4q8a is a vst4 to double-spaced
  224. /// even registers, while VST4q8b is a vst4 to double-spaced odd registers.
  225. ///
  226. /// The encoding info in the .td files does not specify this meta information,
  227. /// which could have been used by the decoder to resolve the conflict. The
  228. /// decoder could try to decode the even/odd register numbering and assign to
  229. /// VST4q8a or VST4q8b, but for the time being, the decoder chooses the "a"
  230. /// version and return the Opcode since the two have the same Asm format string.
  231. class Filter {
  232. protected:
  233. const FilterChooser *Owner;// points to the FilterChooser who owns this filter
  234. unsigned StartBit; // the starting bit position
  235. unsigned NumBits; // number of bits to filter
  236. bool Mixed; // a mixed region contains both set and unset bits
  237. // Map of well-known segment value to the set of uid's with that value.
  238. std::map<uint64_t, std::vector<EncodingIDAndOpcode>>
  239. FilteredInstructions;
  240. // Set of uid's with non-constant segment values.
  241. std::vector<EncodingIDAndOpcode> VariableInstructions;
  242. // Map of well-known segment value to its delegate.
  243. std::map<uint64_t, std::unique_ptr<const FilterChooser>> FilterChooserMap;
  244. // Number of instructions which fall under FilteredInstructions category.
  245. unsigned NumFiltered;
  246. // Keeps track of the last opcode in the filtered bucket.
  247. EncodingIDAndOpcode LastOpcFiltered;
  248. public:
  249. Filter(Filter &&f);
  250. Filter(FilterChooser &owner, unsigned startBit, unsigned numBits, bool mixed);
  251. ~Filter() = default;
  252. unsigned getNumFiltered() const { return NumFiltered; }
  253. EncodingIDAndOpcode getSingletonOpc() const {
  254. assert(NumFiltered == 1);
  255. return LastOpcFiltered;
  256. }
  257. // Return the filter chooser for the group of instructions without constant
  258. // segment values.
  259. const FilterChooser &getVariableFC() const {
  260. assert(NumFiltered == 1);
  261. assert(FilterChooserMap.size() == 1);
  262. return *(FilterChooserMap.find(NO_FIXED_SEGMENTS_SENTINEL)->second);
  263. }
  264. // Divides the decoding task into sub tasks and delegates them to the
  265. // inferior FilterChooser's.
  266. //
  267. // A special case arises when there's only one entry in the filtered
  268. // instructions. In order to unambiguously decode the singleton, we need to
  269. // match the remaining undecoded encoding bits against the singleton.
  270. void recurse();
  271. // Emit table entries to decode instructions given a segment or segments of
  272. // bits.
  273. void emitTableEntry(DecoderTableInfo &TableInfo) const;
  274. // Returns the number of fanout produced by the filter. More fanout implies
  275. // the filter distinguishes more categories of instructions.
  276. unsigned usefulness() const;
  277. }; // end class Filter
  278. } // end anonymous namespace
  279. // These are states of our finite state machines used in FilterChooser's
  280. // filterProcessor() which produces the filter candidates to use.
  281. typedef enum {
  282. ATTR_NONE,
  283. ATTR_FILTERED,
  284. ATTR_ALL_SET,
  285. ATTR_ALL_UNSET,
  286. ATTR_MIXED
  287. } bitAttr_t;
  288. /// FilterChooser - FilterChooser chooses the best filter among a set of Filters
  289. /// in order to perform the decoding of instructions at the current level.
  290. ///
  291. /// Decoding proceeds from the top down. Based on the well-known encoding bits
  292. /// of instructions available, FilterChooser builds up the possible Filters that
  293. /// can further the task of decoding by distinguishing among the remaining
  294. /// candidate instructions.
  295. ///
  296. /// Once a filter has been chosen, it is called upon to divide the decoding task
  297. /// into sub-tasks and delegates them to its inferior FilterChoosers for further
  298. /// processings.
  299. ///
  300. /// It is useful to think of a Filter as governing the switch stmts of the
  301. /// decoding tree. And each case is delegated to an inferior FilterChooser to
  302. /// decide what further remaining bits to look at.
  303. namespace {
  304. class FilterChooser {
  305. protected:
  306. friend class Filter;
  307. // Vector of codegen instructions to choose our filter.
  308. ArrayRef<EncodingAndInst> AllInstructions;
  309. // Vector of uid's for this filter chooser to work on.
  310. // The first member of the pair is the opcode id being decoded, the second is
  311. // the opcode id that should be emitted.
  312. const std::vector<EncodingIDAndOpcode> &Opcodes;
  313. // Lookup table for the operand decoding of instructions.
  314. const std::map<unsigned, std::vector<OperandInfo>> &Operands;
  315. // Vector of candidate filters.
  316. std::vector<Filter> Filters;
  317. // Array of bit values passed down from our parent.
  318. // Set to all BIT_UNFILTERED's for Parent == NULL.
  319. std::vector<bit_value_t> FilterBitValues;
  320. // Links to the FilterChooser above us in the decoding tree.
  321. const FilterChooser *Parent;
  322. // Index of the best filter from Filters.
  323. int BestIndex;
  324. // Width of instructions
  325. unsigned BitWidth;
  326. // Parent emitter
  327. const FixedLenDecoderEmitter *Emitter;
  328. public:
  329. FilterChooser(ArrayRef<EncodingAndInst> Insts,
  330. const std::vector<EncodingIDAndOpcode> &IDs,
  331. const std::map<unsigned, std::vector<OperandInfo>> &Ops,
  332. unsigned BW, const FixedLenDecoderEmitter *E)
  333. : AllInstructions(Insts), Opcodes(IDs), Operands(Ops),
  334. FilterBitValues(BW, BIT_UNFILTERED), Parent(nullptr), BestIndex(-1),
  335. BitWidth(BW), Emitter(E) {
  336. doFilter();
  337. }
  338. FilterChooser(ArrayRef<EncodingAndInst> Insts,
  339. const std::vector<EncodingIDAndOpcode> &IDs,
  340. const std::map<unsigned, std::vector<OperandInfo>> &Ops,
  341. const std::vector<bit_value_t> &ParentFilterBitValues,
  342. const FilterChooser &parent)
  343. : AllInstructions(Insts), Opcodes(IDs), Operands(Ops),
  344. FilterBitValues(ParentFilterBitValues), Parent(&parent), BestIndex(-1),
  345. BitWidth(parent.BitWidth), Emitter(parent.Emitter) {
  346. doFilter();
  347. }
  348. FilterChooser(const FilterChooser &) = delete;
  349. void operator=(const FilterChooser &) = delete;
  350. unsigned getBitWidth() const { return BitWidth; }
  351. protected:
  352. // Populates the insn given the uid.
  353. void insnWithID(insn_t &Insn, unsigned Opcode) const {
  354. BitsInit &Bits = getBitsField(*AllInstructions[Opcode].EncodingDef, "Inst");
  355. // We may have a SoftFail bitmask, which specifies a mask where an encoding
  356. // may differ from the value in "Inst" and yet still be valid, but the
  357. // disassembler should return SoftFail instead of Success.
  358. //
  359. // This is used for marking UNPREDICTABLE instructions in the ARM world.
  360. BitsInit *SFBits =
  361. AllInstructions[Opcode].EncodingDef->getValueAsBitsInit("SoftFail");
  362. for (unsigned i = 0; i < BitWidth; ++i) {
  363. if (SFBits && bitFromBits(*SFBits, i) == BIT_TRUE)
  364. Insn.push_back(BIT_UNSET);
  365. else
  366. Insn.push_back(bitFromBits(Bits, i));
  367. }
  368. }
  369. // Emit the name of the encoding/instruction pair.
  370. void emitNameWithID(raw_ostream &OS, unsigned Opcode) const {
  371. const Record *EncodingDef = AllInstructions[Opcode].EncodingDef;
  372. const Record *InstDef = AllInstructions[Opcode].Inst->TheDef;
  373. if (EncodingDef != InstDef)
  374. OS << EncodingDef->getName() << ":";
  375. OS << InstDef->getName();
  376. }
  377. // Populates the field of the insn given the start position and the number of
  378. // consecutive bits to scan for.
  379. //
  380. // Returns false if there exists any uninitialized bit value in the range.
  381. // Returns true, otherwise.
  382. bool fieldFromInsn(uint64_t &Field, insn_t &Insn, unsigned StartBit,
  383. unsigned NumBits) const;
  384. /// dumpFilterArray - dumpFilterArray prints out debugging info for the given
  385. /// filter array as a series of chars.
  386. void dumpFilterArray(raw_ostream &o,
  387. const std::vector<bit_value_t> & filter) const;
  388. /// dumpStack - dumpStack traverses the filter chooser chain and calls
  389. /// dumpFilterArray on each filter chooser up to the top level one.
  390. void dumpStack(raw_ostream &o, const char *prefix) const;
  391. Filter &bestFilter() {
  392. assert(BestIndex != -1 && "BestIndex not set");
  393. return Filters[BestIndex];
  394. }
  395. bool PositionFiltered(unsigned i) const {
  396. return ValueSet(FilterBitValues[i]);
  397. }
  398. // Calculates the island(s) needed to decode the instruction.
  399. // This returns a lit of undecoded bits of an instructions, for example,
  400. // Inst{20} = 1 && Inst{3-0} == 0b1111 represents two islands of yet-to-be
  401. // decoded bits in order to verify that the instruction matches the Opcode.
  402. unsigned getIslands(std::vector<unsigned> &StartBits,
  403. std::vector<unsigned> &EndBits,
  404. std::vector<uint64_t> &FieldVals,
  405. const insn_t &Insn) const;
  406. // Emits code to check the Predicates member of an instruction are true.
  407. // Returns true if predicate matches were emitted, false otherwise.
  408. bool emitPredicateMatch(raw_ostream &o, unsigned &Indentation,
  409. unsigned Opc) const;
  410. bool doesOpcodeNeedPredicate(unsigned Opc) const;
  411. unsigned getPredicateIndex(DecoderTableInfo &TableInfo, StringRef P) const;
  412. void emitPredicateTableEntry(DecoderTableInfo &TableInfo,
  413. unsigned Opc) const;
  414. void emitSoftFailTableEntry(DecoderTableInfo &TableInfo,
  415. unsigned Opc) const;
  416. // Emits table entries to decode the singleton.
  417. void emitSingletonTableEntry(DecoderTableInfo &TableInfo,
  418. EncodingIDAndOpcode Opc) const;
  419. // Emits code to decode the singleton, and then to decode the rest.
  420. void emitSingletonTableEntry(DecoderTableInfo &TableInfo,
  421. const Filter &Best) const;
  422. void emitBinaryParser(raw_ostream &o, unsigned &Indentation,
  423. const OperandInfo &OpInfo,
  424. bool &OpHasCompleteDecoder) const;
  425. void emitDecoder(raw_ostream &OS, unsigned Indentation, unsigned Opc,
  426. bool &HasCompleteDecoder) const;
  427. unsigned getDecoderIndex(DecoderSet &Decoders, unsigned Opc,
  428. bool &HasCompleteDecoder) const;
  429. // Assign a single filter and run with it.
  430. void runSingleFilter(unsigned startBit, unsigned numBit, bool mixed);
  431. // reportRegion is a helper function for filterProcessor to mark a region as
  432. // eligible for use as a filter region.
  433. void reportRegion(bitAttr_t RA, unsigned StartBit, unsigned BitIndex,
  434. bool AllowMixed);
  435. // FilterProcessor scans the well-known encoding bits of the instructions and
  436. // builds up a list of candidate filters. It chooses the best filter and
  437. // recursively descends down the decoding tree.
  438. bool filterProcessor(bool AllowMixed, bool Greedy = true);
  439. // Decides on the best configuration of filter(s) to use in order to decode
  440. // the instructions. A conflict of instructions may occur, in which case we
  441. // dump the conflict set to the standard error.
  442. void doFilter();
  443. public:
  444. // emitTableEntries - Emit state machine entries to decode our share of
  445. // instructions.
  446. void emitTableEntries(DecoderTableInfo &TableInfo) const;
  447. };
  448. } // end anonymous namespace
  449. ///////////////////////////
  450. // //
  451. // Filter Implementation //
  452. // //
  453. ///////////////////////////
  454. Filter::Filter(Filter &&f)
  455. : Owner(f.Owner), StartBit(f.StartBit), NumBits(f.NumBits), Mixed(f.Mixed),
  456. FilteredInstructions(std::move(f.FilteredInstructions)),
  457. VariableInstructions(std::move(f.VariableInstructions)),
  458. FilterChooserMap(std::move(f.FilterChooserMap)), NumFiltered(f.NumFiltered),
  459. LastOpcFiltered(f.LastOpcFiltered) {
  460. }
  461. Filter::Filter(FilterChooser &owner, unsigned startBit, unsigned numBits,
  462. bool mixed)
  463. : Owner(&owner), StartBit(startBit), NumBits(numBits), Mixed(mixed) {
  464. assert(StartBit + NumBits - 1 < Owner->BitWidth);
  465. NumFiltered = 0;
  466. LastOpcFiltered = {0, 0};
  467. for (unsigned i = 0, e = Owner->Opcodes.size(); i != e; ++i) {
  468. insn_t Insn;
  469. // Populates the insn given the uid.
  470. Owner->insnWithID(Insn, Owner->Opcodes[i].EncodingID);
  471. uint64_t Field;
  472. // Scans the segment for possibly well-specified encoding bits.
  473. bool ok = Owner->fieldFromInsn(Field, Insn, StartBit, NumBits);
  474. if (ok) {
  475. // The encoding bits are well-known. Lets add the uid of the
  476. // instruction into the bucket keyed off the constant field value.
  477. LastOpcFiltered = Owner->Opcodes[i];
  478. FilteredInstructions[Field].push_back(LastOpcFiltered);
  479. ++NumFiltered;
  480. } else {
  481. // Some of the encoding bit(s) are unspecified. This contributes to
  482. // one additional member of "Variable" instructions.
  483. VariableInstructions.push_back(Owner->Opcodes[i]);
  484. }
  485. }
  486. assert((FilteredInstructions.size() + VariableInstructions.size() > 0)
  487. && "Filter returns no instruction categories");
  488. }
  489. // Divides the decoding task into sub tasks and delegates them to the
  490. // inferior FilterChooser's.
  491. //
  492. // A special case arises when there's only one entry in the filtered
  493. // instructions. In order to unambiguously decode the singleton, we need to
  494. // match the remaining undecoded encoding bits against the singleton.
  495. void Filter::recurse() {
  496. // Starts by inheriting our parent filter chooser's filter bit values.
  497. std::vector<bit_value_t> BitValueArray(Owner->FilterBitValues);
  498. if (!VariableInstructions.empty()) {
  499. // Conservatively marks each segment position as BIT_UNSET.
  500. for (unsigned bitIndex = 0; bitIndex < NumBits; ++bitIndex)
  501. BitValueArray[StartBit + bitIndex] = BIT_UNSET;
  502. // Delegates to an inferior filter chooser for further processing on this
  503. // group of instructions whose segment values are variable.
  504. FilterChooserMap.insert(std::make_pair(NO_FIXED_SEGMENTS_SENTINEL,
  505. std::make_unique<FilterChooser>(Owner->AllInstructions,
  506. VariableInstructions, Owner->Operands, BitValueArray, *Owner)));
  507. }
  508. // No need to recurse for a singleton filtered instruction.
  509. // See also Filter::emit*().
  510. if (getNumFiltered() == 1) {
  511. assert(FilterChooserMap.size() == 1);
  512. return;
  513. }
  514. // Otherwise, create sub choosers.
  515. for (const auto &Inst : FilteredInstructions) {
  516. // Marks all the segment positions with either BIT_TRUE or BIT_FALSE.
  517. for (unsigned bitIndex = 0; bitIndex < NumBits; ++bitIndex) {
  518. if (Inst.first & (1ULL << bitIndex))
  519. BitValueArray[StartBit + bitIndex] = BIT_TRUE;
  520. else
  521. BitValueArray[StartBit + bitIndex] = BIT_FALSE;
  522. }
  523. // Delegates to an inferior filter chooser for further processing on this
  524. // category of instructions.
  525. FilterChooserMap.insert(std::make_pair(
  526. Inst.first, std::make_unique<FilterChooser>(
  527. Owner->AllInstructions, Inst.second,
  528. Owner->Operands, BitValueArray, *Owner)));
  529. }
  530. }
  531. static void resolveTableFixups(DecoderTable &Table, const FixupList &Fixups,
  532. uint32_t DestIdx) {
  533. // Any NumToSkip fixups in the current scope can resolve to the
  534. // current location.
  535. for (FixupList::const_reverse_iterator I = Fixups.rbegin(),
  536. E = Fixups.rend();
  537. I != E; ++I) {
  538. // Calculate the distance from the byte following the fixup entry byte
  539. // to the destination. The Target is calculated from after the 16-bit
  540. // NumToSkip entry itself, so subtract two from the displacement here
  541. // to account for that.
  542. uint32_t FixupIdx = *I;
  543. uint32_t Delta = DestIdx - FixupIdx - 3;
  544. // Our NumToSkip entries are 24-bits. Make sure our table isn't too
  545. // big.
  546. assert(Delta < (1u << 24));
  547. Table[FixupIdx] = (uint8_t)Delta;
  548. Table[FixupIdx + 1] = (uint8_t)(Delta >> 8);
  549. Table[FixupIdx + 2] = (uint8_t)(Delta >> 16);
  550. }
  551. }
  552. // Emit table entries to decode instructions given a segment or segments
  553. // of bits.
  554. void Filter::emitTableEntry(DecoderTableInfo &TableInfo) const {
  555. TableInfo.Table.push_back(MCD::OPC_ExtractField);
  556. TableInfo.Table.push_back(StartBit);
  557. TableInfo.Table.push_back(NumBits);
  558. // A new filter entry begins a new scope for fixup resolution.
  559. TableInfo.FixupStack.emplace_back();
  560. DecoderTable &Table = TableInfo.Table;
  561. size_t PrevFilter = 0;
  562. bool HasFallthrough = false;
  563. for (auto &Filter : FilterChooserMap) {
  564. // Field value -1 implies a non-empty set of variable instructions.
  565. // See also recurse().
  566. if (Filter.first == NO_FIXED_SEGMENTS_SENTINEL) {
  567. HasFallthrough = true;
  568. // Each scope should always have at least one filter value to check
  569. // for.
  570. assert(PrevFilter != 0 && "empty filter set!");
  571. FixupList &CurScope = TableInfo.FixupStack.back();
  572. // Resolve any NumToSkip fixups in the current scope.
  573. resolveTableFixups(Table, CurScope, Table.size());
  574. CurScope.clear();
  575. PrevFilter = 0; // Don't re-process the filter's fallthrough.
  576. } else {
  577. Table.push_back(MCD::OPC_FilterValue);
  578. // Encode and emit the value to filter against.
  579. uint8_t Buffer[16];
  580. unsigned Len = encodeULEB128(Filter.first, Buffer);
  581. Table.insert(Table.end(), Buffer, Buffer + Len);
  582. // Reserve space for the NumToSkip entry. We'll backpatch the value
  583. // later.
  584. PrevFilter = Table.size();
  585. Table.push_back(0);
  586. Table.push_back(0);
  587. Table.push_back(0);
  588. }
  589. // We arrive at a category of instructions with the same segment value.
  590. // Now delegate to the sub filter chooser for further decodings.
  591. // The case may fallthrough, which happens if the remaining well-known
  592. // encoding bits do not match exactly.
  593. Filter.second->emitTableEntries(TableInfo);
  594. // Now that we've emitted the body of the handler, update the NumToSkip
  595. // of the filter itself to be able to skip forward when false. Subtract
  596. // two as to account for the width of the NumToSkip field itself.
  597. if (PrevFilter) {
  598. uint32_t NumToSkip = Table.size() - PrevFilter - 3;
  599. assert(NumToSkip < (1u << 24) && "disassembler decoding table too large!");
  600. Table[PrevFilter] = (uint8_t)NumToSkip;
  601. Table[PrevFilter + 1] = (uint8_t)(NumToSkip >> 8);
  602. Table[PrevFilter + 2] = (uint8_t)(NumToSkip >> 16);
  603. }
  604. }
  605. // Any remaining unresolved fixups bubble up to the parent fixup scope.
  606. assert(TableInfo.FixupStack.size() > 1 && "fixup stack underflow!");
  607. FixupScopeList::iterator Source = TableInfo.FixupStack.end() - 1;
  608. FixupScopeList::iterator Dest = Source - 1;
  609. llvm::append_range(*Dest, *Source);
  610. TableInfo.FixupStack.pop_back();
  611. // If there is no fallthrough, then the final filter should get fixed
  612. // up according to the enclosing scope rather than the current position.
  613. if (!HasFallthrough)
  614. TableInfo.FixupStack.back().push_back(PrevFilter);
  615. }
  616. // Returns the number of fanout produced by the filter. More fanout implies
  617. // the filter distinguishes more categories of instructions.
  618. unsigned Filter::usefulness() const {
  619. if (!VariableInstructions.empty())
  620. return FilteredInstructions.size();
  621. else
  622. return FilteredInstructions.size() + 1;
  623. }
  624. //////////////////////////////////
  625. // //
  626. // Filterchooser Implementation //
  627. // //
  628. //////////////////////////////////
  629. // Emit the decoder state machine table.
  630. void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
  631. DecoderTable &Table,
  632. unsigned Indentation,
  633. unsigned BitWidth,
  634. StringRef Namespace) const {
  635. OS.indent(Indentation) << "static const uint8_t DecoderTable" << Namespace
  636. << BitWidth << "[] = {\n";
  637. Indentation += 2;
  638. // FIXME: We may be able to use the NumToSkip values to recover
  639. // appropriate indentation levels.
  640. DecoderTable::const_iterator I = Table.begin();
  641. DecoderTable::const_iterator E = Table.end();
  642. while (I != E) {
  643. assert (I < E && "incomplete decode table entry!");
  644. uint64_t Pos = I - Table.begin();
  645. OS << "/* " << Pos << " */";
  646. OS.PadToColumn(12);
  647. switch (*I) {
  648. default:
  649. PrintFatalError("invalid decode table opcode");
  650. case MCD::OPC_ExtractField: {
  651. ++I;
  652. unsigned Start = *I++;
  653. unsigned Len = *I++;
  654. OS.indent(Indentation) << "MCD::OPC_ExtractField, " << Start << ", "
  655. << Len << ", // Inst{";
  656. if (Len > 1)
  657. OS << (Start + Len - 1) << "-";
  658. OS << Start << "} ...\n";
  659. break;
  660. }
  661. case MCD::OPC_FilterValue: {
  662. ++I;
  663. OS.indent(Indentation) << "MCD::OPC_FilterValue, ";
  664. // The filter value is ULEB128 encoded.
  665. while (*I >= 128)
  666. OS << (unsigned)*I++ << ", ";
  667. OS << (unsigned)*I++ << ", ";
  668. // 24-bit numtoskip value.
  669. uint8_t Byte = *I++;
  670. uint32_t NumToSkip = Byte;
  671. OS << (unsigned)Byte << ", ";
  672. Byte = *I++;
  673. OS << (unsigned)Byte << ", ";
  674. NumToSkip |= Byte << 8;
  675. Byte = *I++;
  676. OS << utostr(Byte) << ", ";
  677. NumToSkip |= Byte << 16;
  678. OS << "// Skip to: " << ((I - Table.begin()) + NumToSkip) << "\n";
  679. break;
  680. }
  681. case MCD::OPC_CheckField: {
  682. ++I;
  683. unsigned Start = *I++;
  684. unsigned Len = *I++;
  685. OS.indent(Indentation) << "MCD::OPC_CheckField, " << Start << ", "
  686. << Len << ", ";// << Val << ", " << NumToSkip << ",\n";
  687. // ULEB128 encoded field value.
  688. for (; *I >= 128; ++I)
  689. OS << (unsigned)*I << ", ";
  690. OS << (unsigned)*I++ << ", ";
  691. // 24-bit numtoskip value.
  692. uint8_t Byte = *I++;
  693. uint32_t NumToSkip = Byte;
  694. OS << (unsigned)Byte << ", ";
  695. Byte = *I++;
  696. OS << (unsigned)Byte << ", ";
  697. NumToSkip |= Byte << 8;
  698. Byte = *I++;
  699. OS << utostr(Byte) << ", ";
  700. NumToSkip |= Byte << 16;
  701. OS << "// Skip to: " << ((I - Table.begin()) + NumToSkip) << "\n";
  702. break;
  703. }
  704. case MCD::OPC_CheckPredicate: {
  705. ++I;
  706. OS.indent(Indentation) << "MCD::OPC_CheckPredicate, ";
  707. for (; *I >= 128; ++I)
  708. OS << (unsigned)*I << ", ";
  709. OS << (unsigned)*I++ << ", ";
  710. // 24-bit numtoskip value.
  711. uint8_t Byte = *I++;
  712. uint32_t NumToSkip = Byte;
  713. OS << (unsigned)Byte << ", ";
  714. Byte = *I++;
  715. OS << (unsigned)Byte << ", ";
  716. NumToSkip |= Byte << 8;
  717. Byte = *I++;
  718. OS << utostr(Byte) << ", ";
  719. NumToSkip |= Byte << 16;
  720. OS << "// Skip to: " << ((I - Table.begin()) + NumToSkip) << "\n";
  721. break;
  722. }
  723. case MCD::OPC_Decode:
  724. case MCD::OPC_TryDecode: {
  725. bool IsTry = *I == MCD::OPC_TryDecode;
  726. ++I;
  727. // Extract the ULEB128 encoded Opcode to a buffer.
  728. uint8_t Buffer[16], *p = Buffer;
  729. while ((*p++ = *I++) >= 128)
  730. assert((p - Buffer) <= (ptrdiff_t)sizeof(Buffer)
  731. && "ULEB128 value too large!");
  732. // Decode the Opcode value.
  733. unsigned Opc = decodeULEB128(Buffer);
  734. OS.indent(Indentation) << "MCD::OPC_" << (IsTry ? "Try" : "")
  735. << "Decode, ";
  736. for (p = Buffer; *p >= 128; ++p)
  737. OS << (unsigned)*p << ", ";
  738. OS << (unsigned)*p << ", ";
  739. // Decoder index.
  740. for (; *I >= 128; ++I)
  741. OS << (unsigned)*I << ", ";
  742. OS << (unsigned)*I++ << ", ";
  743. if (!IsTry) {
  744. OS << "// Opcode: " << NumberedEncodings[Opc] << "\n";
  745. break;
  746. }
  747. // Fallthrough for OPC_TryDecode.
  748. // 24-bit numtoskip value.
  749. uint8_t Byte = *I++;
  750. uint32_t NumToSkip = Byte;
  751. OS << (unsigned)Byte << ", ";
  752. Byte = *I++;
  753. OS << (unsigned)Byte << ", ";
  754. NumToSkip |= Byte << 8;
  755. Byte = *I++;
  756. OS << utostr(Byte) << ", ";
  757. NumToSkip |= Byte << 16;
  758. OS << "// Opcode: " << NumberedEncodings[Opc]
  759. << ", skip to: " << ((I - Table.begin()) + NumToSkip) << "\n";
  760. break;
  761. }
  762. case MCD::OPC_SoftFail: {
  763. ++I;
  764. OS.indent(Indentation) << "MCD::OPC_SoftFail";
  765. // Positive mask
  766. uint64_t Value = 0;
  767. unsigned Shift = 0;
  768. do {
  769. OS << ", " << (unsigned)*I;
  770. Value += (*I & 0x7f) << Shift;
  771. Shift += 7;
  772. } while (*I++ >= 128);
  773. if (Value > 127) {
  774. OS << " /* 0x";
  775. OS.write_hex(Value);
  776. OS << " */";
  777. }
  778. // Negative mask
  779. Value = 0;
  780. Shift = 0;
  781. do {
  782. OS << ", " << (unsigned)*I;
  783. Value += (*I & 0x7f) << Shift;
  784. Shift += 7;
  785. } while (*I++ >= 128);
  786. if (Value > 127) {
  787. OS << " /* 0x";
  788. OS.write_hex(Value);
  789. OS << " */";
  790. }
  791. OS << ",\n";
  792. break;
  793. }
  794. case MCD::OPC_Fail: {
  795. ++I;
  796. OS.indent(Indentation) << "MCD::OPC_Fail,\n";
  797. break;
  798. }
  799. }
  800. }
  801. OS.indent(Indentation) << "0\n";
  802. Indentation -= 2;
  803. OS.indent(Indentation) << "};\n\n";
  804. }
  805. void FixedLenDecoderEmitter::
  806. emitPredicateFunction(formatted_raw_ostream &OS, PredicateSet &Predicates,
  807. unsigned Indentation) const {
  808. // The predicate function is just a big switch statement based on the
  809. // input predicate index.
  810. OS.indent(Indentation) << "static bool checkDecoderPredicate(unsigned Idx, "
  811. << "const FeatureBitset &Bits) {\n";
  812. Indentation += 2;
  813. if (!Predicates.empty()) {
  814. OS.indent(Indentation) << "switch (Idx) {\n";
  815. OS.indent(Indentation) << "default: llvm_unreachable(\"Invalid index!\");\n";
  816. unsigned Index = 0;
  817. for (const auto &Predicate : Predicates) {
  818. OS.indent(Indentation) << "case " << Index++ << ":\n";
  819. OS.indent(Indentation+2) << "return (" << Predicate << ");\n";
  820. }
  821. OS.indent(Indentation) << "}\n";
  822. } else {
  823. // No case statement to emit
  824. OS.indent(Indentation) << "llvm_unreachable(\"Invalid index!\");\n";
  825. }
  826. Indentation -= 2;
  827. OS.indent(Indentation) << "}\n\n";
  828. }
  829. void FixedLenDecoderEmitter::
  830. emitDecoderFunction(formatted_raw_ostream &OS, DecoderSet &Decoders,
  831. unsigned Indentation) const {
  832. // The decoder function is just a big switch statement based on the
  833. // input decoder index.
  834. OS.indent(Indentation) << "template <typename InsnType>\n";
  835. OS.indent(Indentation) << "static DecodeStatus decodeToMCInst(DecodeStatus S,"
  836. << " unsigned Idx, InsnType insn, MCInst &MI,\n";
  837. OS.indent(Indentation) << " uint64_t "
  838. << "Address, const void *Decoder, bool &DecodeComplete) {\n";
  839. Indentation += 2;
  840. OS.indent(Indentation) << "DecodeComplete = true;\n";
  841. // TODO: When InsnType is large, using uint64_t limits all fields to 64 bits
  842. // It would be better for emitBinaryParser to use a 64-bit tmp whenever
  843. // possible but fall back to an InsnType-sized tmp for truly large fields.
  844. OS.indent(Indentation) << "using TmpType = "
  845. "std::conditional_t<std::is_integral<InsnType>::"
  846. "value, InsnType, uint64_t>;\n";
  847. OS.indent(Indentation) << "TmpType tmp;\n";
  848. OS.indent(Indentation) << "switch (Idx) {\n";
  849. OS.indent(Indentation) << "default: llvm_unreachable(\"Invalid index!\");\n";
  850. unsigned Index = 0;
  851. for (const auto &Decoder : Decoders) {
  852. OS.indent(Indentation) << "case " << Index++ << ":\n";
  853. OS << Decoder;
  854. OS.indent(Indentation+2) << "return S;\n";
  855. }
  856. OS.indent(Indentation) << "}\n";
  857. Indentation -= 2;
  858. OS.indent(Indentation) << "}\n\n";
  859. }
  860. // Populates the field of the insn given the start position and the number of
  861. // consecutive bits to scan for.
  862. //
  863. // Returns false if and on the first uninitialized bit value encountered.
  864. // Returns true, otherwise.
  865. bool FilterChooser::fieldFromInsn(uint64_t &Field, insn_t &Insn,
  866. unsigned StartBit, unsigned NumBits) const {
  867. Field = 0;
  868. for (unsigned i = 0; i < NumBits; ++i) {
  869. if (Insn[StartBit + i] == BIT_UNSET)
  870. return false;
  871. if (Insn[StartBit + i] == BIT_TRUE)
  872. Field = Field | (1ULL << i);
  873. }
  874. return true;
  875. }
  876. /// dumpFilterArray - dumpFilterArray prints out debugging info for the given
  877. /// filter array as a series of chars.
  878. void FilterChooser::dumpFilterArray(raw_ostream &o,
  879. const std::vector<bit_value_t> &filter) const {
  880. for (unsigned bitIndex = BitWidth; bitIndex > 0; bitIndex--) {
  881. switch (filter[bitIndex - 1]) {
  882. case BIT_UNFILTERED:
  883. o << ".";
  884. break;
  885. case BIT_UNSET:
  886. o << "_";
  887. break;
  888. case BIT_TRUE:
  889. o << "1";
  890. break;
  891. case BIT_FALSE:
  892. o << "0";
  893. break;
  894. }
  895. }
  896. }
  897. /// dumpStack - dumpStack traverses the filter chooser chain and calls
  898. /// dumpFilterArray on each filter chooser up to the top level one.
  899. void FilterChooser::dumpStack(raw_ostream &o, const char *prefix) const {
  900. const FilterChooser *current = this;
  901. while (current) {
  902. o << prefix;
  903. dumpFilterArray(o, current->FilterBitValues);
  904. o << '\n';
  905. current = current->Parent;
  906. }
  907. }
  908. // Calculates the island(s) needed to decode the instruction.
  909. // This returns a list of undecoded bits of an instructions, for example,
  910. // Inst{20} = 1 && Inst{3-0} == 0b1111 represents two islands of yet-to-be
  911. // decoded bits in order to verify that the instruction matches the Opcode.
  912. unsigned FilterChooser::getIslands(std::vector<unsigned> &StartBits,
  913. std::vector<unsigned> &EndBits,
  914. std::vector<uint64_t> &FieldVals,
  915. const insn_t &Insn) const {
  916. unsigned Num, BitNo;
  917. Num = BitNo = 0;
  918. uint64_t FieldVal = 0;
  919. // 0: Init
  920. // 1: Water (the bit value does not affect decoding)
  921. // 2: Island (well-known bit value needed for decoding)
  922. int State = 0;
  923. for (unsigned i = 0; i < BitWidth; ++i) {
  924. int64_t Val = Value(Insn[i]);
  925. bool Filtered = PositionFiltered(i);
  926. switch (State) {
  927. default: llvm_unreachable("Unreachable code!");
  928. case 0:
  929. case 1:
  930. if (Filtered || Val == -1)
  931. State = 1; // Still in Water
  932. else {
  933. State = 2; // Into the Island
  934. BitNo = 0;
  935. StartBits.push_back(i);
  936. FieldVal = Val;
  937. }
  938. break;
  939. case 2:
  940. if (Filtered || Val == -1) {
  941. State = 1; // Into the Water
  942. EndBits.push_back(i - 1);
  943. FieldVals.push_back(FieldVal);
  944. ++Num;
  945. } else {
  946. State = 2; // Still in Island
  947. ++BitNo;
  948. FieldVal = FieldVal | Val << BitNo;
  949. }
  950. break;
  951. }
  952. }
  953. // If we are still in Island after the loop, do some housekeeping.
  954. if (State == 2) {
  955. EndBits.push_back(BitWidth - 1);
  956. FieldVals.push_back(FieldVal);
  957. ++Num;
  958. }
  959. assert(StartBits.size() == Num && EndBits.size() == Num &&
  960. FieldVals.size() == Num);
  961. return Num;
  962. }
  963. void FilterChooser::emitBinaryParser(raw_ostream &o, unsigned &Indentation,
  964. const OperandInfo &OpInfo,
  965. bool &OpHasCompleteDecoder) const {
  966. const std::string &Decoder = OpInfo.Decoder;
  967. bool UseInsertBits = OpInfo.numFields() != 1 || OpInfo.InitValue != 0;
  968. if (UseInsertBits) {
  969. o.indent(Indentation) << "tmp = 0x";
  970. o.write_hex(OpInfo.InitValue);
  971. o << ";\n";
  972. }
  973. for (const EncodingField &EF : OpInfo) {
  974. o.indent(Indentation);
  975. if (UseInsertBits)
  976. o << "insertBits(tmp, ";
  977. else
  978. o << "tmp = ";
  979. o << "fieldFromInstruction(insn, " << EF.Base << ", " << EF.Width << ')';
  980. if (UseInsertBits)
  981. o << ", " << EF.Offset << ", " << EF.Width << ')';
  982. else if (EF.Offset != 0)
  983. o << " << " << EF.Offset;
  984. o << ";\n";
  985. }
  986. if (Decoder != "") {
  987. OpHasCompleteDecoder = OpInfo.HasCompleteDecoder;
  988. o.indent(Indentation) << Emitter->GuardPrefix << Decoder
  989. << "(MI, tmp, Address, Decoder)"
  990. << Emitter->GuardPostfix
  991. << " { " << (OpHasCompleteDecoder ? "" : "DecodeComplete = false; ")
  992. << "return MCDisassembler::Fail; }\n";
  993. } else {
  994. OpHasCompleteDecoder = true;
  995. o.indent(Indentation) << "MI.addOperand(MCOperand::createImm(tmp));\n";
  996. }
  997. }
  998. void FilterChooser::emitDecoder(raw_ostream &OS, unsigned Indentation,
  999. unsigned Opc, bool &HasCompleteDecoder) const {
  1000. HasCompleteDecoder = true;
  1001. for (const auto &Op : Operands.find(Opc)->second) {
  1002. // If a custom instruction decoder was specified, use that.
  1003. if (Op.numFields() == 0 && !Op.Decoder.empty()) {
  1004. HasCompleteDecoder = Op.HasCompleteDecoder;
  1005. OS.indent(Indentation) << Emitter->GuardPrefix << Op.Decoder
  1006. << "(MI, insn, Address, Decoder)"
  1007. << Emitter->GuardPostfix
  1008. << " { " << (HasCompleteDecoder ? "" : "DecodeComplete = false; ")
  1009. << "return MCDisassembler::Fail; }\n";
  1010. break;
  1011. }
  1012. bool OpHasCompleteDecoder;
  1013. emitBinaryParser(OS, Indentation, Op, OpHasCompleteDecoder);
  1014. if (!OpHasCompleteDecoder)
  1015. HasCompleteDecoder = false;
  1016. }
  1017. }
  1018. unsigned FilterChooser::getDecoderIndex(DecoderSet &Decoders,
  1019. unsigned Opc,
  1020. bool &HasCompleteDecoder) const {
  1021. // Build up the predicate string.
  1022. SmallString<256> Decoder;
  1023. // FIXME: emitDecoder() function can take a buffer directly rather than
  1024. // a stream.
  1025. raw_svector_ostream S(Decoder);
  1026. unsigned I = 4;
  1027. emitDecoder(S, I, Opc, HasCompleteDecoder);
  1028. // Using the full decoder string as the key value here is a bit
  1029. // heavyweight, but is effective. If the string comparisons become a
  1030. // performance concern, we can implement a mangling of the predicate
  1031. // data easily enough with a map back to the actual string. That's
  1032. // overkill for now, though.
  1033. // Make sure the predicate is in the table.
  1034. Decoders.insert(CachedHashString(Decoder));
  1035. // Now figure out the index for when we write out the table.
  1036. DecoderSet::const_iterator P = find(Decoders, Decoder.str());
  1037. return (unsigned)(P - Decoders.begin());
  1038. }
  1039. bool FilterChooser::emitPredicateMatch(raw_ostream &o, unsigned &Indentation,
  1040. unsigned Opc) const {
  1041. ListInit *Predicates =
  1042. AllInstructions[Opc].EncodingDef->getValueAsListInit("Predicates");
  1043. bool IsFirstEmission = true;
  1044. for (unsigned i = 0; i < Predicates->size(); ++i) {
  1045. Record *Pred = Predicates->getElementAsRecord(i);
  1046. if (!Pred->getValue("AssemblerMatcherPredicate"))
  1047. continue;
  1048. if (!isa<DagInit>(Pred->getValue("AssemblerCondDag")->getValue()))
  1049. continue;
  1050. const DagInit *D = Pred->getValueAsDag("AssemblerCondDag");
  1051. std::string CombineType = D->getOperator()->getAsString();
  1052. if (CombineType != "any_of" && CombineType != "all_of")
  1053. PrintFatalError(Pred->getLoc(), "Invalid AssemblerCondDag!");
  1054. if (D->getNumArgs() == 0)
  1055. PrintFatalError(Pred->getLoc(), "Invalid AssemblerCondDag!");
  1056. bool IsOr = CombineType == "any_of";
  1057. if (!IsFirstEmission)
  1058. o << " && ";
  1059. if (IsOr)
  1060. o << "(";
  1061. ListSeparator LS(IsOr ? " || " : " && ");
  1062. for (auto *Arg : D->getArgs()) {
  1063. o << LS;
  1064. if (auto *NotArg = dyn_cast<DagInit>(Arg)) {
  1065. if (NotArg->getOperator()->getAsString() != "not" ||
  1066. NotArg->getNumArgs() != 1)
  1067. PrintFatalError(Pred->getLoc(), "Invalid AssemblerCondDag!");
  1068. Arg = NotArg->getArg(0);
  1069. o << "!";
  1070. }
  1071. if (!isa<DefInit>(Arg) ||
  1072. !cast<DefInit>(Arg)->getDef()->isSubClassOf("SubtargetFeature"))
  1073. PrintFatalError(Pred->getLoc(), "Invalid AssemblerCondDag!");
  1074. o << "Bits[" << Emitter->PredicateNamespace << "::" << Arg->getAsString()
  1075. << "]";
  1076. }
  1077. if (IsOr)
  1078. o << ")";
  1079. IsFirstEmission = false;
  1080. }
  1081. return !Predicates->empty();
  1082. }
  1083. bool FilterChooser::doesOpcodeNeedPredicate(unsigned Opc) const {
  1084. ListInit *Predicates =
  1085. AllInstructions[Opc].EncodingDef->getValueAsListInit("Predicates");
  1086. for (unsigned i = 0; i < Predicates->size(); ++i) {
  1087. Record *Pred = Predicates->getElementAsRecord(i);
  1088. if (!Pred->getValue("AssemblerMatcherPredicate"))
  1089. continue;
  1090. if (isa<DagInit>(Pred->getValue("AssemblerCondDag")->getValue()))
  1091. return true;
  1092. }
  1093. return false;
  1094. }
  1095. unsigned FilterChooser::getPredicateIndex(DecoderTableInfo &TableInfo,
  1096. StringRef Predicate) const {
  1097. // Using the full predicate string as the key value here is a bit
  1098. // heavyweight, but is effective. If the string comparisons become a
  1099. // performance concern, we can implement a mangling of the predicate
  1100. // data easily enough with a map back to the actual string. That's
  1101. // overkill for now, though.
  1102. // Make sure the predicate is in the table.
  1103. TableInfo.Predicates.insert(CachedHashString(Predicate));
  1104. // Now figure out the index for when we write out the table.
  1105. PredicateSet::const_iterator P = find(TableInfo.Predicates, Predicate);
  1106. return (unsigned)(P - TableInfo.Predicates.begin());
  1107. }
  1108. void FilterChooser::emitPredicateTableEntry(DecoderTableInfo &TableInfo,
  1109. unsigned Opc) const {
  1110. if (!doesOpcodeNeedPredicate(Opc))
  1111. return;
  1112. // Build up the predicate string.
  1113. SmallString<256> Predicate;
  1114. // FIXME: emitPredicateMatch() functions can take a buffer directly rather
  1115. // than a stream.
  1116. raw_svector_ostream PS(Predicate);
  1117. unsigned I = 0;
  1118. emitPredicateMatch(PS, I, Opc);
  1119. // Figure out the index into the predicate table for the predicate just
  1120. // computed.
  1121. unsigned PIdx = getPredicateIndex(TableInfo, PS.str());
  1122. SmallString<16> PBytes;
  1123. raw_svector_ostream S(PBytes);
  1124. encodeULEB128(PIdx, S);
  1125. TableInfo.Table.push_back(MCD::OPC_CheckPredicate);
  1126. // Predicate index
  1127. for (unsigned i = 0, e = PBytes.size(); i != e; ++i)
  1128. TableInfo.Table.push_back(PBytes[i]);
  1129. // Push location for NumToSkip backpatching.
  1130. TableInfo.FixupStack.back().push_back(TableInfo.Table.size());
  1131. TableInfo.Table.push_back(0);
  1132. TableInfo.Table.push_back(0);
  1133. TableInfo.Table.push_back(0);
  1134. }
  1135. void FilterChooser::emitSoftFailTableEntry(DecoderTableInfo &TableInfo,
  1136. unsigned Opc) const {
  1137. BitsInit *SFBits =
  1138. AllInstructions[Opc].EncodingDef->getValueAsBitsInit("SoftFail");
  1139. if (!SFBits) return;
  1140. BitsInit *InstBits =
  1141. AllInstructions[Opc].EncodingDef->getValueAsBitsInit("Inst");
  1142. APInt PositiveMask(BitWidth, 0ULL);
  1143. APInt NegativeMask(BitWidth, 0ULL);
  1144. for (unsigned i = 0; i < BitWidth; ++i) {
  1145. bit_value_t B = bitFromBits(*SFBits, i);
  1146. bit_value_t IB = bitFromBits(*InstBits, i);
  1147. if (B != BIT_TRUE) continue;
  1148. switch (IB) {
  1149. case BIT_FALSE:
  1150. // The bit is meant to be false, so emit a check to see if it is true.
  1151. PositiveMask.setBit(i);
  1152. break;
  1153. case BIT_TRUE:
  1154. // The bit is meant to be true, so emit a check to see if it is false.
  1155. NegativeMask.setBit(i);
  1156. break;
  1157. default:
  1158. // The bit is not set; this must be an error!
  1159. errs() << "SoftFail Conflict: bit SoftFail{" << i << "} in "
  1160. << AllInstructions[Opc] << " is set but Inst{" << i
  1161. << "} is unset!\n"
  1162. << " - You can only mark a bit as SoftFail if it is fully defined"
  1163. << " (1/0 - not '?') in Inst\n";
  1164. return;
  1165. }
  1166. }
  1167. bool NeedPositiveMask = PositiveMask.getBoolValue();
  1168. bool NeedNegativeMask = NegativeMask.getBoolValue();
  1169. if (!NeedPositiveMask && !NeedNegativeMask)
  1170. return;
  1171. TableInfo.Table.push_back(MCD::OPC_SoftFail);
  1172. SmallString<16> MaskBytes;
  1173. raw_svector_ostream S(MaskBytes);
  1174. if (NeedPositiveMask) {
  1175. encodeULEB128(PositiveMask.getZExtValue(), S);
  1176. for (unsigned i = 0, e = MaskBytes.size(); i != e; ++i)
  1177. TableInfo.Table.push_back(MaskBytes[i]);
  1178. } else
  1179. TableInfo.Table.push_back(0);
  1180. if (NeedNegativeMask) {
  1181. MaskBytes.clear();
  1182. encodeULEB128(NegativeMask.getZExtValue(), S);
  1183. for (unsigned i = 0, e = MaskBytes.size(); i != e; ++i)
  1184. TableInfo.Table.push_back(MaskBytes[i]);
  1185. } else
  1186. TableInfo.Table.push_back(0);
  1187. }
  1188. // Emits table entries to decode the singleton.
  1189. void FilterChooser::emitSingletonTableEntry(DecoderTableInfo &TableInfo,
  1190. EncodingIDAndOpcode Opc) const {
  1191. std::vector<unsigned> StartBits;
  1192. std::vector<unsigned> EndBits;
  1193. std::vector<uint64_t> FieldVals;
  1194. insn_t Insn;
  1195. insnWithID(Insn, Opc.EncodingID);
  1196. // Look for islands of undecoded bits of the singleton.
  1197. getIslands(StartBits, EndBits, FieldVals, Insn);
  1198. unsigned Size = StartBits.size();
  1199. // Emit the predicate table entry if one is needed.
  1200. emitPredicateTableEntry(TableInfo, Opc.EncodingID);
  1201. // Check any additional encoding fields needed.
  1202. for (unsigned I = Size; I != 0; --I) {
  1203. unsigned NumBits = EndBits[I-1] - StartBits[I-1] + 1;
  1204. TableInfo.Table.push_back(MCD::OPC_CheckField);
  1205. TableInfo.Table.push_back(StartBits[I-1]);
  1206. TableInfo.Table.push_back(NumBits);
  1207. uint8_t Buffer[16], *p;
  1208. encodeULEB128(FieldVals[I-1], Buffer);
  1209. for (p = Buffer; *p >= 128 ; ++p)
  1210. TableInfo.Table.push_back(*p);
  1211. TableInfo.Table.push_back(*p);
  1212. // Push location for NumToSkip backpatching.
  1213. TableInfo.FixupStack.back().push_back(TableInfo.Table.size());
  1214. // The fixup is always 24-bits, so go ahead and allocate the space
  1215. // in the table so all our relative position calculations work OK even
  1216. // before we fully resolve the real value here.
  1217. TableInfo.Table.push_back(0);
  1218. TableInfo.Table.push_back(0);
  1219. TableInfo.Table.push_back(0);
  1220. }
  1221. // Check for soft failure of the match.
  1222. emitSoftFailTableEntry(TableInfo, Opc.EncodingID);
  1223. bool HasCompleteDecoder;
  1224. unsigned DIdx =
  1225. getDecoderIndex(TableInfo.Decoders, Opc.EncodingID, HasCompleteDecoder);
  1226. // Produce OPC_Decode or OPC_TryDecode opcode based on the information
  1227. // whether the instruction decoder is complete or not. If it is complete
  1228. // then it handles all possible values of remaining variable/unfiltered bits
  1229. // and for any value can determine if the bitpattern is a valid instruction
  1230. // or not. This means OPC_Decode will be the final step in the decoding
  1231. // process. If it is not complete, then the Fail return code from the
  1232. // decoder method indicates that additional processing should be done to see
  1233. // if there is any other instruction that also matches the bitpattern and
  1234. // can decode it.
  1235. TableInfo.Table.push_back(HasCompleteDecoder ? MCD::OPC_Decode :
  1236. MCD::OPC_TryDecode);
  1237. NumEncodingsSupported++;
  1238. uint8_t Buffer[16], *p;
  1239. encodeULEB128(Opc.Opcode, Buffer);
  1240. for (p = Buffer; *p >= 128 ; ++p)
  1241. TableInfo.Table.push_back(*p);
  1242. TableInfo.Table.push_back(*p);
  1243. SmallString<16> Bytes;
  1244. raw_svector_ostream S(Bytes);
  1245. encodeULEB128(DIdx, S);
  1246. // Decoder index
  1247. for (unsigned i = 0, e = Bytes.size(); i != e; ++i)
  1248. TableInfo.Table.push_back(Bytes[i]);
  1249. if (!HasCompleteDecoder) {
  1250. // Push location for NumToSkip backpatching.
  1251. TableInfo.FixupStack.back().push_back(TableInfo.Table.size());
  1252. // Allocate the space for the fixup.
  1253. TableInfo.Table.push_back(0);
  1254. TableInfo.Table.push_back(0);
  1255. TableInfo.Table.push_back(0);
  1256. }
  1257. }
  1258. // Emits table entries to decode the singleton, and then to decode the rest.
  1259. void FilterChooser::emitSingletonTableEntry(DecoderTableInfo &TableInfo,
  1260. const Filter &Best) const {
  1261. EncodingIDAndOpcode Opc = Best.getSingletonOpc();
  1262. // complex singletons need predicate checks from the first singleton
  1263. // to refer forward to the variable filterchooser that follows.
  1264. TableInfo.FixupStack.emplace_back();
  1265. emitSingletonTableEntry(TableInfo, Opc);
  1266. resolveTableFixups(TableInfo.Table, TableInfo.FixupStack.back(),
  1267. TableInfo.Table.size());
  1268. TableInfo.FixupStack.pop_back();
  1269. Best.getVariableFC().emitTableEntries(TableInfo);
  1270. }
  1271. // Assign a single filter and run with it. Top level API client can initialize
  1272. // with a single filter to start the filtering process.
  1273. void FilterChooser::runSingleFilter(unsigned startBit, unsigned numBit,
  1274. bool mixed) {
  1275. Filters.clear();
  1276. Filters.emplace_back(*this, startBit, numBit, true);
  1277. BestIndex = 0; // Sole Filter instance to choose from.
  1278. bestFilter().recurse();
  1279. }
  1280. // reportRegion is a helper function for filterProcessor to mark a region as
  1281. // eligible for use as a filter region.
  1282. void FilterChooser::reportRegion(bitAttr_t RA, unsigned StartBit,
  1283. unsigned BitIndex, bool AllowMixed) {
  1284. if (RA == ATTR_MIXED && AllowMixed)
  1285. Filters.emplace_back(*this, StartBit, BitIndex - StartBit, true);
  1286. else if (RA == ATTR_ALL_SET && !AllowMixed)
  1287. Filters.emplace_back(*this, StartBit, BitIndex - StartBit, false);
  1288. }
  1289. // FilterProcessor scans the well-known encoding bits of the instructions and
  1290. // builds up a list of candidate filters. It chooses the best filter and
  1291. // recursively descends down the decoding tree.
  1292. bool FilterChooser::filterProcessor(bool AllowMixed, bool Greedy) {
  1293. Filters.clear();
  1294. BestIndex = -1;
  1295. unsigned numInstructions = Opcodes.size();
  1296. assert(numInstructions && "Filter created with no instructions");
  1297. // No further filtering is necessary.
  1298. if (numInstructions == 1)
  1299. return true;
  1300. // Heuristics. See also doFilter()'s "Heuristics" comment when num of
  1301. // instructions is 3.
  1302. if (AllowMixed && !Greedy) {
  1303. assert(numInstructions == 3);
  1304. for (auto Opcode : Opcodes) {
  1305. std::vector<unsigned> StartBits;
  1306. std::vector<unsigned> EndBits;
  1307. std::vector<uint64_t> FieldVals;
  1308. insn_t Insn;
  1309. insnWithID(Insn, Opcode.EncodingID);
  1310. // Look for islands of undecoded bits of any instruction.
  1311. if (getIslands(StartBits, EndBits, FieldVals, Insn) > 0) {
  1312. // Found an instruction with island(s). Now just assign a filter.
  1313. runSingleFilter(StartBits[0], EndBits[0] - StartBits[0] + 1, true);
  1314. return true;
  1315. }
  1316. }
  1317. }
  1318. unsigned BitIndex;
  1319. // We maintain BIT_WIDTH copies of the bitAttrs automaton.
  1320. // The automaton consumes the corresponding bit from each
  1321. // instruction.
  1322. //
  1323. // Input symbols: 0, 1, and _ (unset).
  1324. // States: NONE, FILTERED, ALL_SET, ALL_UNSET, and MIXED.
  1325. // Initial state: NONE.
  1326. //
  1327. // (NONE) ------- [01] -> (ALL_SET)
  1328. // (NONE) ------- _ ----> (ALL_UNSET)
  1329. // (ALL_SET) ---- [01] -> (ALL_SET)
  1330. // (ALL_SET) ---- _ ----> (MIXED)
  1331. // (ALL_UNSET) -- [01] -> (MIXED)
  1332. // (ALL_UNSET) -- _ ----> (ALL_UNSET)
  1333. // (MIXED) ------ . ----> (MIXED)
  1334. // (FILTERED)---- . ----> (FILTERED)
  1335. std::vector<bitAttr_t> bitAttrs;
  1336. // FILTERED bit positions provide no entropy and are not worthy of pursuing.
  1337. // Filter::recurse() set either BIT_TRUE or BIT_FALSE for each position.
  1338. for (BitIndex = 0; BitIndex < BitWidth; ++BitIndex)
  1339. if (FilterBitValues[BitIndex] == BIT_TRUE ||
  1340. FilterBitValues[BitIndex] == BIT_FALSE)
  1341. bitAttrs.push_back(ATTR_FILTERED);
  1342. else
  1343. bitAttrs.push_back(ATTR_NONE);
  1344. for (unsigned InsnIndex = 0; InsnIndex < numInstructions; ++InsnIndex) {
  1345. insn_t insn;
  1346. insnWithID(insn, Opcodes[InsnIndex].EncodingID);
  1347. for (BitIndex = 0; BitIndex < BitWidth; ++BitIndex) {
  1348. switch (bitAttrs[BitIndex]) {
  1349. case ATTR_NONE:
  1350. if (insn[BitIndex] == BIT_UNSET)
  1351. bitAttrs[BitIndex] = ATTR_ALL_UNSET;
  1352. else
  1353. bitAttrs[BitIndex] = ATTR_ALL_SET;
  1354. break;
  1355. case ATTR_ALL_SET:
  1356. if (insn[BitIndex] == BIT_UNSET)
  1357. bitAttrs[BitIndex] = ATTR_MIXED;
  1358. break;
  1359. case ATTR_ALL_UNSET:
  1360. if (insn[BitIndex] != BIT_UNSET)
  1361. bitAttrs[BitIndex] = ATTR_MIXED;
  1362. break;
  1363. case ATTR_MIXED:
  1364. case ATTR_FILTERED:
  1365. break;
  1366. }
  1367. }
  1368. }
  1369. // The regionAttr automaton consumes the bitAttrs automatons' state,
  1370. // lowest-to-highest.
  1371. //
  1372. // Input symbols: F(iltered), (all_)S(et), (all_)U(nset), M(ixed)
  1373. // States: NONE, ALL_SET, MIXED
  1374. // Initial state: NONE
  1375. //
  1376. // (NONE) ----- F --> (NONE)
  1377. // (NONE) ----- S --> (ALL_SET) ; and set region start
  1378. // (NONE) ----- U --> (NONE)
  1379. // (NONE) ----- M --> (MIXED) ; and set region start
  1380. // (ALL_SET) -- F --> (NONE) ; and report an ALL_SET region
  1381. // (ALL_SET) -- S --> (ALL_SET)
  1382. // (ALL_SET) -- U --> (NONE) ; and report an ALL_SET region
  1383. // (ALL_SET) -- M --> (MIXED) ; and report an ALL_SET region
  1384. // (MIXED) ---- F --> (NONE) ; and report a MIXED region
  1385. // (MIXED) ---- S --> (ALL_SET) ; and report a MIXED region
  1386. // (MIXED) ---- U --> (NONE) ; and report a MIXED region
  1387. // (MIXED) ---- M --> (MIXED)
  1388. bitAttr_t RA = ATTR_NONE;
  1389. unsigned StartBit = 0;
  1390. for (BitIndex = 0; BitIndex < BitWidth; ++BitIndex) {
  1391. bitAttr_t bitAttr = bitAttrs[BitIndex];
  1392. assert(bitAttr != ATTR_NONE && "Bit without attributes");
  1393. switch (RA) {
  1394. case ATTR_NONE:
  1395. switch (bitAttr) {
  1396. case ATTR_FILTERED:
  1397. break;
  1398. case ATTR_ALL_SET:
  1399. StartBit = BitIndex;
  1400. RA = ATTR_ALL_SET;
  1401. break;
  1402. case ATTR_ALL_UNSET:
  1403. break;
  1404. case ATTR_MIXED:
  1405. StartBit = BitIndex;
  1406. RA = ATTR_MIXED;
  1407. break;
  1408. default:
  1409. llvm_unreachable("Unexpected bitAttr!");
  1410. }
  1411. break;
  1412. case ATTR_ALL_SET:
  1413. switch (bitAttr) {
  1414. case ATTR_FILTERED:
  1415. reportRegion(RA, StartBit, BitIndex, AllowMixed);
  1416. RA = ATTR_NONE;
  1417. break;
  1418. case ATTR_ALL_SET:
  1419. break;
  1420. case ATTR_ALL_UNSET:
  1421. reportRegion(RA, StartBit, BitIndex, AllowMixed);
  1422. RA = ATTR_NONE;
  1423. break;
  1424. case ATTR_MIXED:
  1425. reportRegion(RA, StartBit, BitIndex, AllowMixed);
  1426. StartBit = BitIndex;
  1427. RA = ATTR_MIXED;
  1428. break;
  1429. default:
  1430. llvm_unreachable("Unexpected bitAttr!");
  1431. }
  1432. break;
  1433. case ATTR_MIXED:
  1434. switch (bitAttr) {
  1435. case ATTR_FILTERED:
  1436. reportRegion(RA, StartBit, BitIndex, AllowMixed);
  1437. StartBit = BitIndex;
  1438. RA = ATTR_NONE;
  1439. break;
  1440. case ATTR_ALL_SET:
  1441. reportRegion(RA, StartBit, BitIndex, AllowMixed);
  1442. StartBit = BitIndex;
  1443. RA = ATTR_ALL_SET;
  1444. break;
  1445. case ATTR_ALL_UNSET:
  1446. reportRegion(RA, StartBit, BitIndex, AllowMixed);
  1447. RA = ATTR_NONE;
  1448. break;
  1449. case ATTR_MIXED:
  1450. break;
  1451. default:
  1452. llvm_unreachable("Unexpected bitAttr!");
  1453. }
  1454. break;
  1455. case ATTR_ALL_UNSET:
  1456. llvm_unreachable("regionAttr state machine has no ATTR_UNSET state");
  1457. case ATTR_FILTERED:
  1458. llvm_unreachable("regionAttr state machine has no ATTR_FILTERED state");
  1459. }
  1460. }
  1461. // At the end, if we're still in ALL_SET or MIXED states, report a region
  1462. switch (RA) {
  1463. case ATTR_NONE:
  1464. break;
  1465. case ATTR_FILTERED:
  1466. break;
  1467. case ATTR_ALL_SET:
  1468. reportRegion(RA, StartBit, BitIndex, AllowMixed);
  1469. break;
  1470. case ATTR_ALL_UNSET:
  1471. break;
  1472. case ATTR_MIXED:
  1473. reportRegion(RA, StartBit, BitIndex, AllowMixed);
  1474. break;
  1475. }
  1476. // We have finished with the filter processings. Now it's time to choose
  1477. // the best performing filter.
  1478. BestIndex = 0;
  1479. bool AllUseless = true;
  1480. unsigned BestScore = 0;
  1481. for (unsigned i = 0, e = Filters.size(); i != e; ++i) {
  1482. unsigned Usefulness = Filters[i].usefulness();
  1483. if (Usefulness)
  1484. AllUseless = false;
  1485. if (Usefulness > BestScore) {
  1486. BestIndex = i;
  1487. BestScore = Usefulness;
  1488. }
  1489. }
  1490. if (!AllUseless)
  1491. bestFilter().recurse();
  1492. return !AllUseless;
  1493. } // end of FilterChooser::filterProcessor(bool)
  1494. // Decides on the best configuration of filter(s) to use in order to decode
  1495. // the instructions. A conflict of instructions may occur, in which case we
  1496. // dump the conflict set to the standard error.
  1497. void FilterChooser::doFilter() {
  1498. unsigned Num = Opcodes.size();
  1499. assert(Num && "FilterChooser created with no instructions");
  1500. // Try regions of consecutive known bit values first.
  1501. if (filterProcessor(false))
  1502. return;
  1503. // Then regions of mixed bits (both known and unitialized bit values allowed).
  1504. if (filterProcessor(true))
  1505. return;
  1506. // Heuristics to cope with conflict set {t2CMPrs, t2SUBSrr, t2SUBSrs} where
  1507. // no single instruction for the maximum ATTR_MIXED region Inst{14-4} has a
  1508. // well-known encoding pattern. In such case, we backtrack and scan for the
  1509. // the very first consecutive ATTR_ALL_SET region and assign a filter to it.
  1510. if (Num == 3 && filterProcessor(true, false))
  1511. return;
  1512. // If we come to here, the instruction decoding has failed.
  1513. // Set the BestIndex to -1 to indicate so.
  1514. BestIndex = -1;
  1515. }
  1516. // emitTableEntries - Emit state machine entries to decode our share of
  1517. // instructions.
  1518. void FilterChooser::emitTableEntries(DecoderTableInfo &TableInfo) const {
  1519. if (Opcodes.size() == 1) {
  1520. // There is only one instruction in the set, which is great!
  1521. // Call emitSingletonDecoder() to see whether there are any remaining
  1522. // encodings bits.
  1523. emitSingletonTableEntry(TableInfo, Opcodes[0]);
  1524. return;
  1525. }
  1526. // Choose the best filter to do the decodings!
  1527. if (BestIndex != -1) {
  1528. const Filter &Best = Filters[BestIndex];
  1529. if (Best.getNumFiltered() == 1)
  1530. emitSingletonTableEntry(TableInfo, Best);
  1531. else
  1532. Best.emitTableEntry(TableInfo);
  1533. return;
  1534. }
  1535. // We don't know how to decode these instructions! Dump the
  1536. // conflict set and bail.
  1537. // Print out useful conflict information for postmortem analysis.
  1538. errs() << "Decoding Conflict:\n";
  1539. dumpStack(errs(), "\t\t");
  1540. for (auto Opcode : Opcodes) {
  1541. errs() << '\t';
  1542. emitNameWithID(errs(), Opcode.EncodingID);
  1543. errs() << " ";
  1544. dumpBits(
  1545. errs(),
  1546. getBitsField(*AllInstructions[Opcode.EncodingID].EncodingDef, "Inst"));
  1547. errs() << '\n';
  1548. }
  1549. }
  1550. static std::string findOperandDecoderMethod(TypedInit *TI) {
  1551. std::string Decoder;
  1552. Record *Record = cast<DefInit>(TI)->getDef();
  1553. RecordVal *DecoderString = Record->getValue("DecoderMethod");
  1554. StringInit *String = DecoderString ?
  1555. dyn_cast<StringInit>(DecoderString->getValue()) : nullptr;
  1556. if (String) {
  1557. Decoder = std::string(String->getValue());
  1558. if (!Decoder.empty())
  1559. return Decoder;
  1560. }
  1561. if (Record->isSubClassOf("RegisterOperand"))
  1562. Record = Record->getValueAsDef("RegClass");
  1563. if (Record->isSubClassOf("RegisterClass")) {
  1564. Decoder = "Decode" + Record->getName().str() + "RegisterClass";
  1565. } else if (Record->isSubClassOf("PointerLikeRegClass")) {
  1566. Decoder = "DecodePointerLikeRegClass" +
  1567. utostr(Record->getValueAsInt("RegClassKind"));
  1568. }
  1569. return Decoder;
  1570. }
  1571. static bool
  1572. populateInstruction(CodeGenTarget &Target, const Record &EncodingDef,
  1573. const CodeGenInstruction &CGI, unsigned Opc,
  1574. std::map<unsigned, std::vector<OperandInfo>> &Operands) {
  1575. const Record &Def = *CGI.TheDef;
  1576. // If all the bit positions are not specified; do not decode this instruction.
  1577. // We are bound to fail! For proper disassembly, the well-known encoding bits
  1578. // of the instruction must be fully specified.
  1579. BitsInit &Bits = getBitsField(EncodingDef, "Inst");
  1580. if (Bits.allInComplete()) return false;
  1581. std::vector<OperandInfo> InsnOperands;
  1582. // If the instruction has specified a custom decoding hook, use that instead
  1583. // of trying to auto-generate the decoder.
  1584. StringRef InstDecoder = EncodingDef.getValueAsString("DecoderMethod");
  1585. if (InstDecoder != "") {
  1586. bool HasCompleteInstDecoder = EncodingDef.getValueAsBit("hasCompleteDecoder");
  1587. InsnOperands.push_back(
  1588. OperandInfo(std::string(InstDecoder), HasCompleteInstDecoder));
  1589. Operands[Opc] = InsnOperands;
  1590. return true;
  1591. }
  1592. // Generate a description of the operand of the instruction that we know
  1593. // how to decode automatically.
  1594. // FIXME: We'll need to have a way to manually override this as needed.
  1595. // Gather the outputs/inputs of the instruction, so we can find their
  1596. // positions in the encoding. This assumes for now that they appear in the
  1597. // MCInst in the order that they're listed.
  1598. std::vector<std::pair<Init*, StringRef>> InOutOperands;
  1599. DagInit *Out = Def.getValueAsDag("OutOperandList");
  1600. DagInit *In = Def.getValueAsDag("InOperandList");
  1601. for (unsigned i = 0; i < Out->getNumArgs(); ++i)
  1602. InOutOperands.push_back(std::make_pair(Out->getArg(i),
  1603. Out->getArgNameStr(i)));
  1604. for (unsigned i = 0; i < In->getNumArgs(); ++i)
  1605. InOutOperands.push_back(std::make_pair(In->getArg(i),
  1606. In->getArgNameStr(i)));
  1607. // Search for tied operands, so that we can correctly instantiate
  1608. // operands that are not explicitly represented in the encoding.
  1609. std::map<std::string, std::string> TiedNames;
  1610. for (unsigned i = 0; i < CGI.Operands.size(); ++i) {
  1611. int tiedTo = CGI.Operands[i].getTiedRegister();
  1612. if (tiedTo != -1) {
  1613. std::pair<unsigned, unsigned> SO =
  1614. CGI.Operands.getSubOperandNumber(tiedTo);
  1615. TiedNames[std::string(InOutOperands[i].second)] =
  1616. std::string(InOutOperands[SO.first].second);
  1617. TiedNames[std::string(InOutOperands[SO.first].second)] =
  1618. std::string(InOutOperands[i].second);
  1619. }
  1620. }
  1621. std::map<std::string, std::vector<OperandInfo>> NumberedInsnOperands;
  1622. std::set<std::string> NumberedInsnOperandsNoTie;
  1623. if (Target.getInstructionSet()->
  1624. getValueAsBit("decodePositionallyEncodedOperands")) {
  1625. const std::vector<RecordVal> &Vals = Def.getValues();
  1626. unsigned NumberedOp = 0;
  1627. std::set<unsigned> NamedOpIndices;
  1628. if (Target.getInstructionSet()->
  1629. getValueAsBit("noNamedPositionallyEncodedOperands"))
  1630. // Collect the set of operand indices that might correspond to named
  1631. // operand, and skip these when assigning operands based on position.
  1632. for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
  1633. unsigned OpIdx;
  1634. if (!CGI.Operands.hasOperandNamed(Vals[i].getName(), OpIdx))
  1635. continue;
  1636. NamedOpIndices.insert(OpIdx);
  1637. }
  1638. for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
  1639. // Ignore fixed fields in the record, we're looking for values like:
  1640. // bits<5> RST = { ?, ?, ?, ?, ? };
  1641. if (Vals[i].isNonconcreteOK() || Vals[i].getValue()->isComplete())
  1642. continue;
  1643. // Determine if Vals[i] actually contributes to the Inst encoding.
  1644. unsigned bi = 0;
  1645. for (; bi < Bits.getNumBits(); ++bi) {
  1646. VarInit *Var = nullptr;
  1647. VarBitInit *BI = dyn_cast<VarBitInit>(Bits.getBit(bi));
  1648. if (BI)
  1649. Var = dyn_cast<VarInit>(BI->getBitVar());
  1650. else
  1651. Var = dyn_cast<VarInit>(Bits.getBit(bi));
  1652. if (Var && Var->getName() == Vals[i].getName())
  1653. break;
  1654. }
  1655. if (bi == Bits.getNumBits())
  1656. continue;
  1657. // Skip variables that correspond to explicitly-named operands.
  1658. unsigned OpIdx;
  1659. if (CGI.Operands.hasOperandNamed(Vals[i].getName(), OpIdx))
  1660. continue;
  1661. // Get the bit range for this operand:
  1662. unsigned bitStart = bi++, bitWidth = 1;
  1663. for (; bi < Bits.getNumBits(); ++bi) {
  1664. VarInit *Var = nullptr;
  1665. VarBitInit *BI = dyn_cast<VarBitInit>(Bits.getBit(bi));
  1666. if (BI)
  1667. Var = dyn_cast<VarInit>(BI->getBitVar());
  1668. else
  1669. Var = dyn_cast<VarInit>(Bits.getBit(bi));
  1670. if (!Var)
  1671. break;
  1672. if (Var->getName() != Vals[i].getName())
  1673. break;
  1674. ++bitWidth;
  1675. }
  1676. unsigned NumberOps = CGI.Operands.size();
  1677. while (NumberedOp < NumberOps &&
  1678. (CGI.Operands.isFlatOperandNotEmitted(NumberedOp) ||
  1679. (!NamedOpIndices.empty() && NamedOpIndices.count(
  1680. CGI.Operands.getSubOperandNumber(NumberedOp).first))))
  1681. ++NumberedOp;
  1682. OpIdx = NumberedOp++;
  1683. // OpIdx now holds the ordered operand number of Vals[i].
  1684. std::pair<unsigned, unsigned> SO =
  1685. CGI.Operands.getSubOperandNumber(OpIdx);
  1686. const std::string &Name = CGI.Operands[SO.first].Name;
  1687. LLVM_DEBUG(dbgs() << "Numbered operand mapping for " << Def.getName()
  1688. << ": " << Name << "(" << SO.first << ", " << SO.second
  1689. << ") => " << Vals[i].getName() << "\n");
  1690. std::string Decoder;
  1691. Record *TypeRecord = CGI.Operands[SO.first].Rec;
  1692. RecordVal *DecoderString = TypeRecord->getValue("DecoderMethod");
  1693. StringInit *String = DecoderString ?
  1694. dyn_cast<StringInit>(DecoderString->getValue()) : nullptr;
  1695. if (String && String->getValue() != "")
  1696. Decoder = std::string(String->getValue());
  1697. if (Decoder == "" &&
  1698. CGI.Operands[SO.first].MIOperandInfo &&
  1699. CGI.Operands[SO.first].MIOperandInfo->getNumArgs()) {
  1700. Init *Arg = CGI.Operands[SO.first].MIOperandInfo->
  1701. getArg(SO.second);
  1702. if (DefInit *DI = cast<DefInit>(Arg))
  1703. TypeRecord = DI->getDef();
  1704. }
  1705. bool isReg = false;
  1706. if (TypeRecord->isSubClassOf("RegisterOperand"))
  1707. TypeRecord = TypeRecord->getValueAsDef("RegClass");
  1708. if (TypeRecord->isSubClassOf("RegisterClass")) {
  1709. Decoder = "Decode" + TypeRecord->getName().str() + "RegisterClass";
  1710. isReg = true;
  1711. } else if (TypeRecord->isSubClassOf("PointerLikeRegClass")) {
  1712. Decoder = "DecodePointerLikeRegClass" +
  1713. utostr(TypeRecord->getValueAsInt("RegClassKind"));
  1714. isReg = true;
  1715. }
  1716. DecoderString = TypeRecord->getValue("DecoderMethod");
  1717. String = DecoderString ?
  1718. dyn_cast<StringInit>(DecoderString->getValue()) : nullptr;
  1719. if (!isReg && String && String->getValue() != "")
  1720. Decoder = std::string(String->getValue());
  1721. RecordVal *HasCompleteDecoderVal =
  1722. TypeRecord->getValue("hasCompleteDecoder");
  1723. BitInit *HasCompleteDecoderBit = HasCompleteDecoderVal ?
  1724. dyn_cast<BitInit>(HasCompleteDecoderVal->getValue()) : nullptr;
  1725. bool HasCompleteDecoder = HasCompleteDecoderBit ?
  1726. HasCompleteDecoderBit->getValue() : true;
  1727. OperandInfo OpInfo(Decoder, HasCompleteDecoder);
  1728. OpInfo.addField(bitStart, bitWidth, 0);
  1729. NumberedInsnOperands[Name].push_back(OpInfo);
  1730. // FIXME: For complex operands with custom decoders we can't handle tied
  1731. // sub-operands automatically. Skip those here and assume that this is
  1732. // fixed up elsewhere.
  1733. if (CGI.Operands[SO.first].MIOperandInfo &&
  1734. CGI.Operands[SO.first].MIOperandInfo->getNumArgs() > 1 &&
  1735. String && String->getValue() != "")
  1736. NumberedInsnOperandsNoTie.insert(Name);
  1737. }
  1738. }
  1739. // For each operand, see if we can figure out where it is encoded.
  1740. for (const auto &Op : InOutOperands) {
  1741. if (!NumberedInsnOperands[std::string(Op.second)].empty()) {
  1742. llvm::append_range(InsnOperands,
  1743. NumberedInsnOperands[std::string(Op.second)]);
  1744. continue;
  1745. }
  1746. if (!NumberedInsnOperands[TiedNames[std::string(Op.second)]].empty()) {
  1747. if (!NumberedInsnOperandsNoTie.count(TiedNames[std::string(Op.second)])) {
  1748. // Figure out to which (sub)operand we're tied.
  1749. unsigned i =
  1750. CGI.Operands.getOperandNamed(TiedNames[std::string(Op.second)]);
  1751. int tiedTo = CGI.Operands[i].getTiedRegister();
  1752. if (tiedTo == -1) {
  1753. i = CGI.Operands.getOperandNamed(Op.second);
  1754. tiedTo = CGI.Operands[i].getTiedRegister();
  1755. }
  1756. if (tiedTo != -1) {
  1757. std::pair<unsigned, unsigned> SO =
  1758. CGI.Operands.getSubOperandNumber(tiedTo);
  1759. InsnOperands.push_back(
  1760. NumberedInsnOperands[TiedNames[std::string(Op.second)]]
  1761. [SO.second]);
  1762. }
  1763. }
  1764. continue;
  1765. }
  1766. TypedInit *TI = cast<TypedInit>(Op.first);
  1767. // At this point, we can locate the decoder field, but we need to know how
  1768. // to interpret it. As a first step, require the target to provide
  1769. // callbacks for decoding register classes.
  1770. std::string Decoder = findOperandDecoderMethod(TI);
  1771. Record *TypeRecord = cast<DefInit>(TI)->getDef();
  1772. RecordVal *HasCompleteDecoderVal =
  1773. TypeRecord->getValue("hasCompleteDecoder");
  1774. BitInit *HasCompleteDecoderBit = HasCompleteDecoderVal ?
  1775. dyn_cast<BitInit>(HasCompleteDecoderVal->getValue()) : nullptr;
  1776. bool HasCompleteDecoder = HasCompleteDecoderBit ?
  1777. HasCompleteDecoderBit->getValue() : true;
  1778. OperandInfo OpInfo(Decoder, HasCompleteDecoder);
  1779. // Some bits of the operand may be required to be 1 depending on the
  1780. // instruction's encoding. Collect those bits.
  1781. if (const RecordVal *EncodedValue = EncodingDef.getValue(Op.second))
  1782. if (const BitsInit *OpBits = dyn_cast<BitsInit>(EncodedValue->getValue()))
  1783. for (unsigned I = 0; I < OpBits->getNumBits(); ++I)
  1784. if (const BitInit *OpBit = dyn_cast<BitInit>(OpBits->getBit(I)))
  1785. if (OpBit->getValue())
  1786. OpInfo.InitValue |= 1ULL << I;
  1787. unsigned Base = ~0U;
  1788. unsigned Width = 0;
  1789. unsigned Offset = 0;
  1790. for (unsigned bi = 0; bi < Bits.getNumBits(); ++bi) {
  1791. VarInit *Var = nullptr;
  1792. VarBitInit *BI = dyn_cast<VarBitInit>(Bits.getBit(bi));
  1793. if (BI)
  1794. Var = dyn_cast<VarInit>(BI->getBitVar());
  1795. else
  1796. Var = dyn_cast<VarInit>(Bits.getBit(bi));
  1797. if (!Var) {
  1798. if (Base != ~0U) {
  1799. OpInfo.addField(Base, Width, Offset);
  1800. Base = ~0U;
  1801. Width = 0;
  1802. Offset = 0;
  1803. }
  1804. continue;
  1805. }
  1806. if (Var->getName() != Op.second &&
  1807. Var->getName() != TiedNames[std::string(Op.second)]) {
  1808. if (Base != ~0U) {
  1809. OpInfo.addField(Base, Width, Offset);
  1810. Base = ~0U;
  1811. Width = 0;
  1812. Offset = 0;
  1813. }
  1814. continue;
  1815. }
  1816. if (Base == ~0U) {
  1817. Base = bi;
  1818. Width = 1;
  1819. Offset = BI ? BI->getBitNum() : 0;
  1820. } else if (BI && BI->getBitNum() != Offset + Width) {
  1821. OpInfo.addField(Base, Width, Offset);
  1822. Base = bi;
  1823. Width = 1;
  1824. Offset = BI->getBitNum();
  1825. } else {
  1826. ++Width;
  1827. }
  1828. }
  1829. if (Base != ~0U)
  1830. OpInfo.addField(Base, Width, Offset);
  1831. if (OpInfo.numFields() > 0)
  1832. InsnOperands.push_back(OpInfo);
  1833. }
  1834. Operands[Opc] = InsnOperands;
  1835. #if 0
  1836. LLVM_DEBUG({
  1837. // Dumps the instruction encoding bits.
  1838. dumpBits(errs(), Bits);
  1839. errs() << '\n';
  1840. // Dumps the list of operand info.
  1841. for (unsigned i = 0, e = CGI.Operands.size(); i != e; ++i) {
  1842. const CGIOperandList::OperandInfo &Info = CGI.Operands[i];
  1843. const std::string &OperandName = Info.Name;
  1844. const Record &OperandDef = *Info.Rec;
  1845. errs() << "\t" << OperandName << " (" << OperandDef.getName() << ")\n";
  1846. }
  1847. });
  1848. #endif
  1849. return true;
  1850. }
  1851. // emitFieldFromInstruction - Emit the templated helper function
  1852. // fieldFromInstruction().
  1853. // On Windows we make sure that this function is not inlined when
  1854. // using the VS compiler. It has a bug which causes the function
  1855. // to be optimized out in some circustances. See llvm.org/pr38292
  1856. static void emitFieldFromInstruction(formatted_raw_ostream &OS) {
  1857. OS << "// Helper functions for extracting fields from encoded instructions.\n"
  1858. << "// InsnType must either be integral or an APInt-like object that "
  1859. "must:\n"
  1860. << "// * be default-constructible and copy-constructible\n"
  1861. << "// * be constructible from a uint64_t\n"
  1862. << "// * be constructible from an APInt (this can be private)\n"
  1863. << "// * Support insertBits(bits, startBit, numBits)\n"
  1864. << "// * Support extractBitsAsZExtValue(numBits, startBit)\n"
  1865. << "// * be convertible to bool\n"
  1866. << "// * Support the ~, &, ==, and != operators with other objects of "
  1867. "the same type\n"
  1868. << "// * Support put (<<) to raw_ostream&\n"
  1869. << "template <typename InsnType>\n"
  1870. << "#if defined(_MSC_VER) && !defined(__clang__)\n"
  1871. << "__declspec(noinline)\n"
  1872. << "#endif\n"
  1873. << "static std::enable_if_t<std::is_integral<InsnType>::value, InsnType>\n"
  1874. << "fieldFromInstruction(const InsnType &insn, unsigned startBit,\n"
  1875. << " unsigned numBits) {\n"
  1876. << " assert(startBit + numBits <= 64 && \"Cannot support >64-bit "
  1877. "extractions!\");\n"
  1878. << " assert(startBit + numBits <= (sizeof(InsnType) * 8) &&\n"
  1879. << " \"Instruction field out of bounds!\");\n"
  1880. << " InsnType fieldMask;\n"
  1881. << " if (numBits == sizeof(InsnType) * 8)\n"
  1882. << " fieldMask = (InsnType)(-1LL);\n"
  1883. << " else\n"
  1884. << " fieldMask = (((InsnType)1 << numBits) - 1) << startBit;\n"
  1885. << " return (insn & fieldMask) >> startBit;\n"
  1886. << "}\n"
  1887. << "\n"
  1888. << "template <typename InsnType>\n"
  1889. << "static std::enable_if_t<!std::is_integral<InsnType>::value, "
  1890. "uint64_t>\n"
  1891. << "fieldFromInstruction(const InsnType &insn, unsigned startBit,\n"
  1892. << " unsigned numBits) {\n"
  1893. << " return insn.extractBitsAsZExtValue(numBits, startBit);\n"
  1894. << "}\n\n";
  1895. }
  1896. // emitInsertBits - Emit the templated helper function insertBits().
  1897. static void emitInsertBits(formatted_raw_ostream &OS) {
  1898. OS << "// Helper function for inserting bits extracted from an encoded "
  1899. "instruction into\n"
  1900. << "// a field.\n"
  1901. << "template <typename InsnType>\n"
  1902. << "static std::enable_if_t<std::is_integral<InsnType>::value>\n"
  1903. << "insertBits(InsnType &field, InsnType bits, unsigned startBit, "
  1904. "unsigned numBits) {\n"
  1905. << " assert(startBit + numBits <= sizeof field * 8);\n"
  1906. << " field |= (InsnType)bits << startBit;\n"
  1907. << "}\n"
  1908. << "\n"
  1909. << "template <typename InsnType>\n"
  1910. << "static std::enable_if_t<!std::is_integral<InsnType>::value>\n"
  1911. << "insertBits(InsnType &field, uint64_t bits, unsigned startBit, "
  1912. "unsigned numBits) {\n"
  1913. << " field.insertBits(bits, startBit, numBits);\n"
  1914. << "}\n\n";
  1915. }
  1916. // emitDecodeInstruction - Emit the templated helper function
  1917. // decodeInstruction().
  1918. static void emitDecodeInstruction(formatted_raw_ostream &OS) {
  1919. OS << "template <typename InsnType>\n"
  1920. << "static DecodeStatus decodeInstruction(const uint8_t DecodeTable[], "
  1921. "MCInst &MI,\n"
  1922. << " InsnType insn, uint64_t "
  1923. "Address,\n"
  1924. << " const void *DisAsm,\n"
  1925. << " const MCSubtargetInfo &STI) {\n"
  1926. << " const FeatureBitset &Bits = STI.getFeatureBits();\n"
  1927. << "\n"
  1928. << " const uint8_t *Ptr = DecodeTable;\n"
  1929. << " InsnType CurFieldValue = 0;\n"
  1930. << " DecodeStatus S = MCDisassembler::Success;\n"
  1931. << " while (true) {\n"
  1932. << " ptrdiff_t Loc = Ptr - DecodeTable;\n"
  1933. << " switch (*Ptr) {\n"
  1934. << " default:\n"
  1935. << " errs() << Loc << \": Unexpected decode table opcode!\\n\";\n"
  1936. << " return MCDisassembler::Fail;\n"
  1937. << " case MCD::OPC_ExtractField: {\n"
  1938. << " unsigned Start = *++Ptr;\n"
  1939. << " unsigned Len = *++Ptr;\n"
  1940. << " ++Ptr;\n"
  1941. << " CurFieldValue = fieldFromInstruction(insn, Start, Len);\n"
  1942. << " LLVM_DEBUG(dbgs() << Loc << \": OPC_ExtractField(\" << Start << "
  1943. "\", \"\n"
  1944. << " << Len << \"): \" << CurFieldValue << \"\\n\");\n"
  1945. << " break;\n"
  1946. << " }\n"
  1947. << " case MCD::OPC_FilterValue: {\n"
  1948. << " // Decode the field value.\n"
  1949. << " unsigned Len;\n"
  1950. << " InsnType Val = decodeULEB128(++Ptr, &Len);\n"
  1951. << " Ptr += Len;\n"
  1952. << " // NumToSkip is a plain 24-bit integer.\n"
  1953. << " unsigned NumToSkip = *Ptr++;\n"
  1954. << " NumToSkip |= (*Ptr++) << 8;\n"
  1955. << " NumToSkip |= (*Ptr++) << 16;\n"
  1956. << "\n"
  1957. << " // Perform the filter operation.\n"
  1958. << " if (Val != CurFieldValue)\n"
  1959. << " Ptr += NumToSkip;\n"
  1960. << " LLVM_DEBUG(dbgs() << Loc << \": OPC_FilterValue(\" << Val << "
  1961. "\", \" << NumToSkip\n"
  1962. << " << \"): \" << ((Val != CurFieldValue) ? \"FAIL:\" "
  1963. ": \"PASS:\")\n"
  1964. << " << \" continuing at \" << (Ptr - DecodeTable) << "
  1965. "\"\\n\");\n"
  1966. << "\n"
  1967. << " break;\n"
  1968. << " }\n"
  1969. << " case MCD::OPC_CheckField: {\n"
  1970. << " unsigned Start = *++Ptr;\n"
  1971. << " unsigned Len = *++Ptr;\n"
  1972. << " InsnType FieldValue = fieldFromInstruction(insn, Start, Len);\n"
  1973. << " // Decode the field value.\n"
  1974. << " InsnType ExpectedValue = decodeULEB128(++Ptr, &Len);\n"
  1975. << " Ptr += Len;\n"
  1976. << " // NumToSkip is a plain 24-bit integer.\n"
  1977. << " unsigned NumToSkip = *Ptr++;\n"
  1978. << " NumToSkip |= (*Ptr++) << 8;\n"
  1979. << " NumToSkip |= (*Ptr++) << 16;\n"
  1980. << "\n"
  1981. << " // If the actual and expected values don't match, skip.\n"
  1982. << " if (ExpectedValue != FieldValue)\n"
  1983. << " Ptr += NumToSkip;\n"
  1984. << " LLVM_DEBUG(dbgs() << Loc << \": OPC_CheckField(\" << Start << "
  1985. "\", \"\n"
  1986. << " << Len << \", \" << ExpectedValue << \", \" << "
  1987. "NumToSkip\n"
  1988. << " << \"): FieldValue = \" << FieldValue << \", "
  1989. "ExpectedValue = \"\n"
  1990. << " << ExpectedValue << \": \"\n"
  1991. << " << ((ExpectedValue == FieldValue) ? \"PASS\\n\" : "
  1992. "\"FAIL\\n\"));\n"
  1993. << " break;\n"
  1994. << " }\n"
  1995. << " case MCD::OPC_CheckPredicate: {\n"
  1996. << " unsigned Len;\n"
  1997. << " // Decode the Predicate Index value.\n"
  1998. << " unsigned PIdx = decodeULEB128(++Ptr, &Len);\n"
  1999. << " Ptr += Len;\n"
  2000. << " // NumToSkip is a plain 24-bit integer.\n"
  2001. << " unsigned NumToSkip = *Ptr++;\n"
  2002. << " NumToSkip |= (*Ptr++) << 8;\n"
  2003. << " NumToSkip |= (*Ptr++) << 16;\n"
  2004. << " // Check the predicate.\n"
  2005. << " bool Pred;\n"
  2006. << " if (!(Pred = checkDecoderPredicate(PIdx, Bits)))\n"
  2007. << " Ptr += NumToSkip;\n"
  2008. << " (void)Pred;\n"
  2009. << " LLVM_DEBUG(dbgs() << Loc << \": OPC_CheckPredicate(\" << PIdx "
  2010. "<< \"): \"\n"
  2011. << " << (Pred ? \"PASS\\n\" : \"FAIL\\n\"));\n"
  2012. << "\n"
  2013. << " break;\n"
  2014. << " }\n"
  2015. << " case MCD::OPC_Decode: {\n"
  2016. << " unsigned Len;\n"
  2017. << " // Decode the Opcode value.\n"
  2018. << " unsigned Opc = decodeULEB128(++Ptr, &Len);\n"
  2019. << " Ptr += Len;\n"
  2020. << " unsigned DecodeIdx = decodeULEB128(Ptr, &Len);\n"
  2021. << " Ptr += Len;\n"
  2022. << "\n"
  2023. << " MI.clear();\n"
  2024. << " MI.setOpcode(Opc);\n"
  2025. << " bool DecodeComplete;\n"
  2026. << " S = decodeToMCInst(S, DecodeIdx, insn, MI, Address, DisAsm, "
  2027. "DecodeComplete);\n"
  2028. << " assert(DecodeComplete);\n"
  2029. << "\n"
  2030. << " LLVM_DEBUG(dbgs() << Loc << \": OPC_Decode: opcode \" << Opc\n"
  2031. << " << \", using decoder \" << DecodeIdx << \": \"\n"
  2032. << " << (S != MCDisassembler::Fail ? \"PASS\" : "
  2033. "\"FAIL\") << \"\\n\");\n"
  2034. << " return S;\n"
  2035. << " }\n"
  2036. << " case MCD::OPC_TryDecode: {\n"
  2037. << " unsigned Len;\n"
  2038. << " // Decode the Opcode value.\n"
  2039. << " unsigned Opc = decodeULEB128(++Ptr, &Len);\n"
  2040. << " Ptr += Len;\n"
  2041. << " unsigned DecodeIdx = decodeULEB128(Ptr, &Len);\n"
  2042. << " Ptr += Len;\n"
  2043. << " // NumToSkip is a plain 24-bit integer.\n"
  2044. << " unsigned NumToSkip = *Ptr++;\n"
  2045. << " NumToSkip |= (*Ptr++) << 8;\n"
  2046. << " NumToSkip |= (*Ptr++) << 16;\n"
  2047. << "\n"
  2048. << " // Perform the decode operation.\n"
  2049. << " MCInst TmpMI;\n"
  2050. << " TmpMI.setOpcode(Opc);\n"
  2051. << " bool DecodeComplete;\n"
  2052. << " S = decodeToMCInst(S, DecodeIdx, insn, TmpMI, Address, DisAsm, "
  2053. "DecodeComplete);\n"
  2054. << " LLVM_DEBUG(dbgs() << Loc << \": OPC_TryDecode: opcode \" << "
  2055. "Opc\n"
  2056. << " << \", using decoder \" << DecodeIdx << \": \");\n"
  2057. << "\n"
  2058. << " if (DecodeComplete) {\n"
  2059. << " // Decoding complete.\n"
  2060. << " LLVM_DEBUG(dbgs() << (S != MCDisassembler::Fail ? \"PASS\" : "
  2061. "\"FAIL\") << \"\\n\");\n"
  2062. << " MI = TmpMI;\n"
  2063. << " return S;\n"
  2064. << " } else {\n"
  2065. << " assert(S == MCDisassembler::Fail);\n"
  2066. << " // If the decoding was incomplete, skip.\n"
  2067. << " Ptr += NumToSkip;\n"
  2068. << " LLVM_DEBUG(dbgs() << \"FAIL: continuing at \" << (Ptr - "
  2069. "DecodeTable) << \"\\n\");\n"
  2070. << " // Reset decode status. This also drops a SoftFail status "
  2071. "that could be\n"
  2072. << " // set before the decode attempt.\n"
  2073. << " S = MCDisassembler::Success;\n"
  2074. << " }\n"
  2075. << " break;\n"
  2076. << " }\n"
  2077. << " case MCD::OPC_SoftFail: {\n"
  2078. << " // Decode the mask values.\n"
  2079. << " unsigned Len;\n"
  2080. << " InsnType PositiveMask = decodeULEB128(++Ptr, &Len);\n"
  2081. << " Ptr += Len;\n"
  2082. << " InsnType NegativeMask = decodeULEB128(Ptr, &Len);\n"
  2083. << " Ptr += Len;\n"
  2084. << " bool Fail = (insn & PositiveMask) || (~insn & NegativeMask);\n"
  2085. << " if (Fail)\n"
  2086. << " S = MCDisassembler::SoftFail;\n"
  2087. << " LLVM_DEBUG(dbgs() << Loc << \": OPC_SoftFail: \" << (Fail ? "
  2088. "\"FAIL\\n\" : \"PASS\\n\"));\n"
  2089. << " break;\n"
  2090. << " }\n"
  2091. << " case MCD::OPC_Fail: {\n"
  2092. << " LLVM_DEBUG(dbgs() << Loc << \": OPC_Fail\\n\");\n"
  2093. << " return MCDisassembler::Fail;\n"
  2094. << " }\n"
  2095. << " }\n"
  2096. << " }\n"
  2097. << " llvm_unreachable(\"bogosity detected in disassembler state "
  2098. "machine!\");\n"
  2099. << "}\n\n";
  2100. }
  2101. // Emits disassembler code for instruction decoding.
  2102. void FixedLenDecoderEmitter::run(raw_ostream &o) {
  2103. formatted_raw_ostream OS(o);
  2104. OS << "#include \"llvm/MC/MCInst.h\"\n";
  2105. OS << "#include \"llvm/Support/DataTypes.h\"\n";
  2106. OS << "#include \"llvm/Support/Debug.h\"\n";
  2107. OS << "#include \"llvm/Support/LEB128.h\"\n";
  2108. OS << "#include \"llvm/Support/raw_ostream.h\"\n";
  2109. OS << "#include <assert.h>\n";
  2110. OS << '\n';
  2111. OS << "namespace llvm {\n\n";
  2112. emitFieldFromInstruction(OS);
  2113. emitInsertBits(OS);
  2114. Target.reverseBitsForLittleEndianEncoding();
  2115. // Parameterize the decoders based on namespace and instruction width.
  2116. std::set<StringRef> HwModeNames;
  2117. const auto &NumberedInstructions = Target.getInstructionsByEnumValue();
  2118. NumberedEncodings.reserve(NumberedInstructions.size());
  2119. DenseMap<Record *, unsigned> IndexOfInstruction;
  2120. // First, collect all HwModes referenced by the target.
  2121. for (const auto &NumberedInstruction : NumberedInstructions) {
  2122. IndexOfInstruction[NumberedInstruction->TheDef] = NumberedEncodings.size();
  2123. if (const RecordVal *RV =
  2124. NumberedInstruction->TheDef->getValue("EncodingInfos")) {
  2125. if (auto *DI = dyn_cast_or_null<DefInit>(RV->getValue())) {
  2126. const CodeGenHwModes &HWM = Target.getHwModes();
  2127. EncodingInfoByHwMode EBM(DI->getDef(), HWM);
  2128. for (auto &KV : EBM)
  2129. HwModeNames.insert(HWM.getMode(KV.first).Name);
  2130. }
  2131. }
  2132. }
  2133. // If HwModeNames is empty, add the empty string so we always have one HwMode.
  2134. if (HwModeNames.empty())
  2135. HwModeNames.insert("");
  2136. for (const auto &NumberedInstruction : NumberedInstructions) {
  2137. IndexOfInstruction[NumberedInstruction->TheDef] = NumberedEncodings.size();
  2138. if (const RecordVal *RV =
  2139. NumberedInstruction->TheDef->getValue("EncodingInfos")) {
  2140. if (DefInit *DI = dyn_cast_or_null<DefInit>(RV->getValue())) {
  2141. const CodeGenHwModes &HWM = Target.getHwModes();
  2142. EncodingInfoByHwMode EBM(DI->getDef(), HWM);
  2143. for (auto &KV : EBM) {
  2144. NumberedEncodings.emplace_back(KV.second, NumberedInstruction,
  2145. HWM.getMode(KV.first).Name);
  2146. HwModeNames.insert(HWM.getMode(KV.first).Name);
  2147. }
  2148. continue;
  2149. }
  2150. }
  2151. // This instruction is encoded the same on all HwModes. Emit it for all
  2152. // HwModes.
  2153. for (StringRef HwModeName : HwModeNames)
  2154. NumberedEncodings.emplace_back(NumberedInstruction->TheDef,
  2155. NumberedInstruction, HwModeName);
  2156. }
  2157. for (const auto &NumberedAlias : RK.getAllDerivedDefinitions("AdditionalEncoding"))
  2158. NumberedEncodings.emplace_back(
  2159. NumberedAlias,
  2160. &Target.getInstruction(NumberedAlias->getValueAsDef("AliasOf")));
  2161. std::map<std::pair<std::string, unsigned>, std::vector<EncodingIDAndOpcode>>
  2162. OpcMap;
  2163. std::map<unsigned, std::vector<OperandInfo>> Operands;
  2164. for (unsigned i = 0; i < NumberedEncodings.size(); ++i) {
  2165. const Record *EncodingDef = NumberedEncodings[i].EncodingDef;
  2166. const CodeGenInstruction *Inst = NumberedEncodings[i].Inst;
  2167. const Record *Def = Inst->TheDef;
  2168. unsigned Size = EncodingDef->getValueAsInt("Size");
  2169. if (Def->getValueAsString("Namespace") == "TargetOpcode" ||
  2170. Def->getValueAsBit("isPseudo") ||
  2171. Def->getValueAsBit("isAsmParserOnly") ||
  2172. Def->getValueAsBit("isCodeGenOnly")) {
  2173. NumEncodingsLackingDisasm++;
  2174. continue;
  2175. }
  2176. if (i < NumberedInstructions.size())
  2177. NumInstructions++;
  2178. NumEncodings++;
  2179. if (!Size)
  2180. continue;
  2181. if (populateInstruction(Target, *EncodingDef, *Inst, i, Operands)) {
  2182. std::string DecoderNamespace =
  2183. std::string(EncodingDef->getValueAsString("DecoderNamespace"));
  2184. if (!NumberedEncodings[i].HwModeName.empty())
  2185. DecoderNamespace +=
  2186. std::string("_") + NumberedEncodings[i].HwModeName.str();
  2187. OpcMap[std::make_pair(DecoderNamespace, Size)].emplace_back(
  2188. i, IndexOfInstruction.find(Def)->second);
  2189. } else {
  2190. NumEncodingsOmitted++;
  2191. }
  2192. }
  2193. DecoderTableInfo TableInfo;
  2194. for (const auto &Opc : OpcMap) {
  2195. // Emit the decoder for this namespace+width combination.
  2196. ArrayRef<EncodingAndInst> NumberedEncodingsRef(
  2197. NumberedEncodings.data(), NumberedEncodings.size());
  2198. FilterChooser FC(NumberedEncodingsRef, Opc.second, Operands,
  2199. 8 * Opc.first.second, this);
  2200. // The decode table is cleared for each top level decoder function. The
  2201. // predicates and decoders themselves, however, are shared across all
  2202. // decoders to give more opportunities for uniqueing.
  2203. TableInfo.Table.clear();
  2204. TableInfo.FixupStack.clear();
  2205. TableInfo.Table.reserve(16384);
  2206. TableInfo.FixupStack.emplace_back();
  2207. FC.emitTableEntries(TableInfo);
  2208. // Any NumToSkip fixups in the top level scope can resolve to the
  2209. // OPC_Fail at the end of the table.
  2210. assert(TableInfo.FixupStack.size() == 1 && "fixup stack phasing error!");
  2211. // Resolve any NumToSkip fixups in the current scope.
  2212. resolveTableFixups(TableInfo.Table, TableInfo.FixupStack.back(),
  2213. TableInfo.Table.size());
  2214. TableInfo.FixupStack.clear();
  2215. TableInfo.Table.push_back(MCD::OPC_Fail);
  2216. // Print the table to the output stream.
  2217. emitTable(OS, TableInfo.Table, 0, FC.getBitWidth(), Opc.first.first);
  2218. OS.flush();
  2219. }
  2220. // Emit the predicate function.
  2221. emitPredicateFunction(OS, TableInfo.Predicates, 0);
  2222. // Emit the decoder function.
  2223. emitDecoderFunction(OS, TableInfo.Decoders, 0);
  2224. // Emit the main entry point for the decoder, decodeInstruction().
  2225. emitDecodeInstruction(OS);
  2226. OS << "\n} // end namespace llvm\n";
  2227. }
  2228. namespace llvm {
  2229. void EmitFixedLenDecoder(RecordKeeper &RK, raw_ostream &OS,
  2230. const std::string &PredicateNamespace,
  2231. const std::string &GPrefix,
  2232. const std::string &GPostfix, const std::string &ROK,
  2233. const std::string &RFail, const std::string &L) {
  2234. FixedLenDecoderEmitter(RK, PredicateNamespace, GPrefix, GPostfix,
  2235. ROK, RFail, L).run(OS);
  2236. }
  2237. } // end namespace llvm