DecoderEmitter.cpp 99 KB

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