AsmMatcherEmitter.cpp 113 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064
  1. //===- AsmMatcherEmitter.cpp - Generate an assembly matcher ---------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This tablegen backend emits a target specifier matcher for converting parsed
  11. // assembly operands in the MCInst structures. It also emits a matcher for
  12. // custom operand parsing.
  13. //
  14. // Converting assembly operands into MCInst structures
  15. // ---------------------------------------------------
  16. //
  17. // The input to the target specific matcher is a list of literal tokens and
  18. // operands. The target specific parser should generally eliminate any syntax
  19. // which is not relevant for matching; for example, comma tokens should have
  20. // already been consumed and eliminated by the parser. Most instructions will
  21. // end up with a single literal token (the instruction name) and some number of
  22. // operands.
  23. //
  24. // Some example inputs, for X86:
  25. // 'addl' (immediate ...) (register ...)
  26. // 'add' (immediate ...) (memory ...)
  27. // 'call' '*' %epc
  28. //
  29. // The assembly matcher is responsible for converting this input into a precise
  30. // machine instruction (i.e., an instruction with a well defined encoding). This
  31. // mapping has several properties which complicate matching:
  32. //
  33. // - It may be ambiguous; many architectures can legally encode particular
  34. // variants of an instruction in different ways (for example, using a smaller
  35. // encoding for small immediates). Such ambiguities should never be
  36. // arbitrarily resolved by the assembler, the assembler is always responsible
  37. // for choosing the "best" available instruction.
  38. //
  39. // - It may depend on the subtarget or the assembler context. Instructions
  40. // which are invalid for the current mode, but otherwise unambiguous (e.g.,
  41. // an SSE instruction in a file being assembled for i486) should be accepted
  42. // and rejected by the assembler front end. However, if the proper encoding
  43. // for an instruction is dependent on the assembler context then the matcher
  44. // is responsible for selecting the correct machine instruction for the
  45. // current mode.
  46. //
  47. // The core matching algorithm attempts to exploit the regularity in most
  48. // instruction sets to quickly determine the set of possibly matching
  49. // instructions, and the simplify the generated code. Additionally, this helps
  50. // to ensure that the ambiguities are intentionally resolved by the user.
  51. //
  52. // The matching is divided into two distinct phases:
  53. //
  54. // 1. Classification: Each operand is mapped to the unique set which (a)
  55. // contains it, and (b) is the largest such subset for which a single
  56. // instruction could match all members.
  57. //
  58. // For register classes, we can generate these subgroups automatically. For
  59. // arbitrary operands, we expect the user to define the classes and their
  60. // relations to one another (for example, 8-bit signed immediates as a
  61. // subset of 32-bit immediates).
  62. //
  63. // By partitioning the operands in this way, we guarantee that for any
  64. // tuple of classes, any single instruction must match either all or none
  65. // of the sets of operands which could classify to that tuple.
  66. //
  67. // In addition, the subset relation amongst classes induces a partial order
  68. // on such tuples, which we use to resolve ambiguities.
  69. //
  70. // 2. The input can now be treated as a tuple of classes (static tokens are
  71. // simple singleton sets). Each such tuple should generally map to a single
  72. // instruction (we currently ignore cases where this isn't true, whee!!!),
  73. // which we can emit a simple matcher for.
  74. //
  75. // Custom Operand Parsing
  76. // ----------------------
  77. //
  78. // Some targets need a custom way to parse operands, some specific instructions
  79. // can contain arguments that can represent processor flags and other kinds of
  80. // identifiers that need to be mapped to specific values in the final encoded
  81. // instructions. The target specific custom operand parsing works in the
  82. // following way:
  83. //
  84. // 1. A operand match table is built, each entry contains a mnemonic, an
  85. // operand class, a mask for all operand positions for that same
  86. // class/mnemonic and target features to be checked while trying to match.
  87. //
  88. // 2. The operand matcher will try every possible entry with the same
  89. // mnemonic and will check if the target feature for this mnemonic also
  90. // matches. After that, if the operand to be matched has its index
  91. // present in the mask, a successful match occurs. Otherwise, fallback
  92. // to the regular operand parsing.
  93. //
  94. // 3. For a match success, each operand class that has a 'ParserMethod'
  95. // becomes part of a switch from where the custom method is called.
  96. //
  97. //===----------------------------------------------------------------------===//
  98. #include "CodeGenTarget.h"
  99. #include "llvm/ADT/PointerUnion.h"
  100. #include "llvm/ADT/STLExtras.h"
  101. #include "llvm/ADT/SmallPtrSet.h"
  102. #include "llvm/ADT/SmallVector.h"
  103. #include "llvm/ADT/StringExtras.h"
  104. #include "llvm/Support/CommandLine.h"
  105. #include "llvm/Support/Debug.h"
  106. #include "llvm/Support/ErrorHandling.h"
  107. #include "llvm/TableGen/Error.h"
  108. #include "llvm/TableGen/Record.h"
  109. #include "llvm/TableGen/StringMatcher.h"
  110. #include "llvm/TableGen/StringToOffsetTable.h"
  111. #include "llvm/TableGen/TableGenBackend.h"
  112. #include <cassert>
  113. #include <cctype>
  114. #include <map>
  115. #include <set>
  116. #include <sstream>
  117. #include <forward_list>
  118. using namespace llvm;
  119. #define DEBUG_TYPE "asm-matcher-emitter"
  120. static cl::opt<std::string>
  121. MatchPrefix("match-prefix", cl::init(""),
  122. cl::desc("Only match instructions with the given prefix"));
  123. namespace {
  124. class AsmMatcherInfo;
  125. struct SubtargetFeatureInfo;
  126. // Register sets are used as keys in some second-order sets TableGen creates
  127. // when generating its data structures. This means that the order of two
  128. // RegisterSets can be seen in the outputted AsmMatcher tables occasionally, and
  129. // can even affect compiler output (at least seen in diagnostics produced when
  130. // all matches fail). So we use a type that sorts them consistently.
  131. typedef std::set<Record*, LessRecordByID> RegisterSet;
  132. class AsmMatcherEmitter {
  133. RecordKeeper &Records;
  134. public:
  135. AsmMatcherEmitter(RecordKeeper &R) : Records(R) {}
  136. void run(raw_ostream &o);
  137. };
  138. /// ClassInfo - Helper class for storing the information about a particular
  139. /// class of operands which can be matched.
  140. struct ClassInfo {
  141. enum ClassInfoKind {
  142. /// Invalid kind, for use as a sentinel value.
  143. Invalid = 0,
  144. /// The class for a particular token.
  145. Token,
  146. /// The (first) register class, subsequent register classes are
  147. /// RegisterClass0+1, and so on.
  148. RegisterClass0,
  149. /// The (first) user defined class, subsequent user defined classes are
  150. /// UserClass0+1, and so on.
  151. UserClass0 = 1<<16
  152. };
  153. /// Kind - The class kind, which is either a predefined kind, or (UserClass0 +
  154. /// N) for the Nth user defined class.
  155. unsigned Kind;
  156. /// SuperClasses - The super classes of this class. Note that for simplicities
  157. /// sake user operands only record their immediate super class, while register
  158. /// operands include all superclasses.
  159. std::vector<ClassInfo*> SuperClasses;
  160. /// Name - The full class name, suitable for use in an enum.
  161. std::string Name;
  162. /// ClassName - The unadorned generic name for this class (e.g., Token).
  163. std::string ClassName;
  164. /// ValueName - The name of the value this class represents; for a token this
  165. /// is the literal token string, for an operand it is the TableGen class (or
  166. /// empty if this is a derived class).
  167. std::string ValueName;
  168. /// PredicateMethod - The name of the operand method to test whether the
  169. /// operand matches this class; this is not valid for Token or register kinds.
  170. std::string PredicateMethod;
  171. /// RenderMethod - The name of the operand method to add this operand to an
  172. /// MCInst; this is not valid for Token or register kinds.
  173. std::string RenderMethod;
  174. /// ParserMethod - The name of the operand method to do a target specific
  175. /// parsing on the operand.
  176. std::string ParserMethod;
  177. /// For register classes: the records for all the registers in this class.
  178. RegisterSet Registers;
  179. /// For custom match classes: the diagnostic kind for when the predicate fails.
  180. std::string DiagnosticType;
  181. public:
  182. /// isRegisterClass() - Check if this is a register class.
  183. bool isRegisterClass() const {
  184. return Kind >= RegisterClass0 && Kind < UserClass0;
  185. }
  186. /// isUserClass() - Check if this is a user defined class.
  187. bool isUserClass() const {
  188. return Kind >= UserClass0;
  189. }
  190. /// isRelatedTo - Check whether this class is "related" to \p RHS. Classes
  191. /// are related if they are in the same class hierarchy.
  192. bool isRelatedTo(const ClassInfo &RHS) const {
  193. // Tokens are only related to tokens.
  194. if (Kind == Token || RHS.Kind == Token)
  195. return Kind == Token && RHS.Kind == Token;
  196. // Registers classes are only related to registers classes, and only if
  197. // their intersection is non-empty.
  198. if (isRegisterClass() || RHS.isRegisterClass()) {
  199. if (!isRegisterClass() || !RHS.isRegisterClass())
  200. return false;
  201. RegisterSet Tmp;
  202. std::insert_iterator<RegisterSet> II(Tmp, Tmp.begin());
  203. std::set_intersection(Registers.begin(), Registers.end(),
  204. RHS.Registers.begin(), RHS.Registers.end(),
  205. II, LessRecordByID());
  206. return !Tmp.empty();
  207. }
  208. // Otherwise we have two users operands; they are related if they are in the
  209. // same class hierarchy.
  210. //
  211. // FIXME: This is an oversimplification, they should only be related if they
  212. // intersect, however we don't have that information.
  213. assert(isUserClass() && RHS.isUserClass() && "Unexpected class!");
  214. const ClassInfo *Root = this;
  215. while (!Root->SuperClasses.empty())
  216. Root = Root->SuperClasses.front();
  217. const ClassInfo *RHSRoot = &RHS;
  218. while (!RHSRoot->SuperClasses.empty())
  219. RHSRoot = RHSRoot->SuperClasses.front();
  220. return Root == RHSRoot;
  221. }
  222. /// isSubsetOf - Test whether this class is a subset of \p RHS.
  223. bool isSubsetOf(const ClassInfo &RHS) const {
  224. // This is a subset of RHS if it is the same class...
  225. if (this == &RHS)
  226. return true;
  227. // ... or if any of its super classes are a subset of RHS.
  228. for (const ClassInfo *CI : SuperClasses)
  229. if (CI->isSubsetOf(RHS))
  230. return true;
  231. return false;
  232. }
  233. /// operator< - Compare two classes.
  234. // FIXME: This ordering seems to be broken. For example:
  235. // u64 < i64, i64 < s8, s8 < u64, forming a cycle
  236. // u64 is a subset of i64
  237. // i64 and s8 are not subsets of each other, so are ordered by name
  238. // s8 and u64 are not subsets of each other, so are ordered by name
  239. bool operator<(const ClassInfo &RHS) const {
  240. if (this == &RHS)
  241. return false;
  242. // Unrelated classes can be ordered by kind.
  243. if (!isRelatedTo(RHS))
  244. return Kind < RHS.Kind;
  245. switch (Kind) {
  246. case Invalid:
  247. llvm_unreachable("Invalid kind!");
  248. default:
  249. // This class precedes the RHS if it is a proper subset of the RHS.
  250. if (isSubsetOf(RHS))
  251. return true;
  252. if (RHS.isSubsetOf(*this))
  253. return false;
  254. // Otherwise, order by name to ensure we have a total ordering.
  255. return ValueName < RHS.ValueName;
  256. }
  257. }
  258. };
  259. /// MatchableInfo - Helper class for storing the necessary information for an
  260. /// instruction or alias which is capable of being matched.
  261. struct MatchableInfo {
  262. struct AsmOperand {
  263. /// Token - This is the token that the operand came from.
  264. StringRef Token;
  265. /// The unique class instance this operand should match.
  266. ClassInfo *Class;
  267. /// The operand name this is, if anything.
  268. StringRef SrcOpName;
  269. /// The suboperand index within SrcOpName, or -1 for the entire operand.
  270. int SubOpIdx;
  271. /// Whether the token is "isolated", i.e., it is preceded and followed
  272. /// by separators.
  273. bool IsIsolatedToken;
  274. /// Register record if this token is singleton register.
  275. Record *SingletonReg;
  276. explicit AsmOperand(bool IsIsolatedToken, StringRef T)
  277. : Token(T), Class(nullptr), SubOpIdx(-1),
  278. IsIsolatedToken(IsIsolatedToken), SingletonReg(nullptr) {}
  279. };
  280. /// ResOperand - This represents a single operand in the result instruction
  281. /// generated by the match. In cases (like addressing modes) where a single
  282. /// assembler operand expands to multiple MCOperands, this represents the
  283. /// single assembler operand, not the MCOperand.
  284. struct ResOperand {
  285. enum {
  286. /// RenderAsmOperand - This represents an operand result that is
  287. /// generated by calling the render method on the assembly operand. The
  288. /// corresponding AsmOperand is specified by AsmOperandNum.
  289. RenderAsmOperand,
  290. /// TiedOperand - This represents a result operand that is a duplicate of
  291. /// a previous result operand.
  292. TiedOperand,
  293. /// ImmOperand - This represents an immediate value that is dumped into
  294. /// the operand.
  295. ImmOperand,
  296. /// RegOperand - This represents a fixed register that is dumped in.
  297. RegOperand
  298. } Kind;
  299. union {
  300. /// This is the operand # in the AsmOperands list that this should be
  301. /// copied from.
  302. unsigned AsmOperandNum;
  303. /// TiedOperandNum - This is the (earlier) result operand that should be
  304. /// copied from.
  305. unsigned TiedOperandNum;
  306. /// ImmVal - This is the immediate value added to the instruction.
  307. int64_t ImmVal;
  308. /// Register - This is the register record.
  309. Record *Register;
  310. };
  311. /// MINumOperands - The number of MCInst operands populated by this
  312. /// operand.
  313. unsigned MINumOperands;
  314. static ResOperand getRenderedOp(unsigned AsmOpNum, unsigned NumOperands) {
  315. ResOperand X;
  316. X.Kind = RenderAsmOperand;
  317. X.AsmOperandNum = AsmOpNum;
  318. X.MINumOperands = NumOperands;
  319. return X;
  320. }
  321. static ResOperand getTiedOp(unsigned TiedOperandNum) {
  322. ResOperand X;
  323. X.Kind = TiedOperand;
  324. X.TiedOperandNum = TiedOperandNum;
  325. X.MINumOperands = 1;
  326. return X;
  327. }
  328. static ResOperand getImmOp(int64_t Val) {
  329. ResOperand X;
  330. X.Kind = ImmOperand;
  331. X.ImmVal = Val;
  332. X.MINumOperands = 1;
  333. return X;
  334. }
  335. static ResOperand getRegOp(Record *Reg) {
  336. ResOperand X;
  337. X.Kind = RegOperand;
  338. X.Register = Reg;
  339. X.MINumOperands = 1;
  340. return X;
  341. }
  342. };
  343. /// AsmVariantID - Target's assembly syntax variant no.
  344. int AsmVariantID;
  345. /// AsmString - The assembly string for this instruction (with variants
  346. /// removed), e.g. "movsx $src, $dst".
  347. std::string AsmString;
  348. /// TheDef - This is the definition of the instruction or InstAlias that this
  349. /// matchable came from.
  350. Record *const TheDef;
  351. /// DefRec - This is the definition that it came from.
  352. PointerUnion<const CodeGenInstruction*, const CodeGenInstAlias*> DefRec;
  353. const CodeGenInstruction *getResultInst() const {
  354. if (DefRec.is<const CodeGenInstruction*>())
  355. return DefRec.get<const CodeGenInstruction*>();
  356. return DefRec.get<const CodeGenInstAlias*>()->ResultInst;
  357. }
  358. /// ResOperands - This is the operand list that should be built for the result
  359. /// MCInst.
  360. SmallVector<ResOperand, 8> ResOperands;
  361. /// Mnemonic - This is the first token of the matched instruction, its
  362. /// mnemonic.
  363. StringRef Mnemonic;
  364. /// AsmOperands - The textual operands that this instruction matches,
  365. /// annotated with a class and where in the OperandList they were defined.
  366. /// This directly corresponds to the tokenized AsmString after the mnemonic is
  367. /// removed.
  368. SmallVector<AsmOperand, 8> AsmOperands;
  369. /// Predicates - The required subtarget features to match this instruction.
  370. SmallVector<const SubtargetFeatureInfo *, 4> RequiredFeatures;
  371. /// ConversionFnKind - The enum value which is passed to the generated
  372. /// convertToMCInst to convert parsed operands into an MCInst for this
  373. /// function.
  374. std::string ConversionFnKind;
  375. /// If this instruction is deprecated in some form.
  376. bool HasDeprecation;
  377. /// If this is an alias, this is use to determine whether or not to using
  378. /// the conversion function defined by the instruction's AsmMatchConverter
  379. /// or to use the function generated by the alias.
  380. bool UseInstAsmMatchConverter;
  381. MatchableInfo(const CodeGenInstruction &CGI)
  382. : AsmVariantID(0), AsmString(CGI.AsmString), TheDef(CGI.TheDef), DefRec(&CGI),
  383. UseInstAsmMatchConverter(true) {
  384. }
  385. MatchableInfo(std::unique_ptr<const CodeGenInstAlias> Alias)
  386. : AsmVariantID(0), AsmString(Alias->AsmString), TheDef(Alias->TheDef),
  387. DefRec(Alias.release()),
  388. UseInstAsmMatchConverter(
  389. TheDef->getValueAsBit("UseInstAsmMatchConverter")) {
  390. }
  391. ~MatchableInfo() {
  392. delete DefRec.dyn_cast<const CodeGenInstAlias*>();
  393. }
  394. // Two-operand aliases clone from the main matchable, but mark the second
  395. // operand as a tied operand of the first for purposes of the assembler.
  396. void formTwoOperandAlias(StringRef Constraint);
  397. void initialize(const AsmMatcherInfo &Info,
  398. SmallPtrSetImpl<Record*> &SingletonRegisters,
  399. int AsmVariantNo, std::string &RegisterPrefix);
  400. /// validate - Return true if this matchable is a valid thing to match against
  401. /// and perform a bunch of validity checking.
  402. bool validate(StringRef CommentDelimiter, bool Hack) const;
  403. /// extractSingletonRegisterForAsmOperand - Extract singleton register,
  404. /// if present, from specified token.
  405. void
  406. extractSingletonRegisterForAsmOperand(unsigned i, const AsmMatcherInfo &Info,
  407. std::string &RegisterPrefix);
  408. /// findAsmOperand - Find the AsmOperand with the specified name and
  409. /// suboperand index.
  410. int findAsmOperand(StringRef N, int SubOpIdx) const {
  411. for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i)
  412. if (N == AsmOperands[i].SrcOpName &&
  413. SubOpIdx == AsmOperands[i].SubOpIdx)
  414. return i;
  415. return -1;
  416. }
  417. /// findAsmOperandNamed - Find the first AsmOperand with the specified name.
  418. /// This does not check the suboperand index.
  419. int findAsmOperandNamed(StringRef N) const {
  420. for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i)
  421. if (N == AsmOperands[i].SrcOpName)
  422. return i;
  423. return -1;
  424. }
  425. void buildInstructionResultOperands();
  426. void buildAliasResultOperands();
  427. /// operator< - Compare two matchables.
  428. bool operator<(const MatchableInfo &RHS) const {
  429. // The primary comparator is the instruction mnemonic.
  430. if (Mnemonic != RHS.Mnemonic)
  431. return Mnemonic < RHS.Mnemonic;
  432. if (AsmOperands.size() != RHS.AsmOperands.size())
  433. return AsmOperands.size() < RHS.AsmOperands.size();
  434. // Compare lexicographically by operand. The matcher validates that other
  435. // orderings wouldn't be ambiguous using \see couldMatchAmbiguouslyWith().
  436. for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
  437. if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class)
  438. return true;
  439. if (*RHS.AsmOperands[i].Class < *AsmOperands[i].Class)
  440. return false;
  441. }
  442. // Give matches that require more features higher precedence. This is useful
  443. // because we cannot define AssemblerPredicates with the negation of
  444. // processor features. For example, ARM v6 "nop" may be either a HINT or
  445. // MOV. With v6, we want to match HINT. The assembler has no way to
  446. // predicate MOV under "NoV6", but HINT will always match first because it
  447. // requires V6 while MOV does not.
  448. if (RequiredFeatures.size() != RHS.RequiredFeatures.size())
  449. return RequiredFeatures.size() > RHS.RequiredFeatures.size();
  450. return false;
  451. }
  452. /// couldMatchAmbiguouslyWith - Check whether this matchable could
  453. /// ambiguously match the same set of operands as \p RHS (without being a
  454. /// strictly superior match).
  455. bool couldMatchAmbiguouslyWith(const MatchableInfo &RHS) const {
  456. // The primary comparator is the instruction mnemonic.
  457. if (Mnemonic != RHS.Mnemonic)
  458. return false;
  459. // The number of operands is unambiguous.
  460. if (AsmOperands.size() != RHS.AsmOperands.size())
  461. return false;
  462. // Otherwise, make sure the ordering of the two instructions is unambiguous
  463. // by checking that either (a) a token or operand kind discriminates them,
  464. // or (b) the ordering among equivalent kinds is consistent.
  465. // Tokens and operand kinds are unambiguous (assuming a correct target
  466. // specific parser).
  467. for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i)
  468. if (AsmOperands[i].Class->Kind != RHS.AsmOperands[i].Class->Kind ||
  469. AsmOperands[i].Class->Kind == ClassInfo::Token)
  470. if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class ||
  471. *RHS.AsmOperands[i].Class < *AsmOperands[i].Class)
  472. return false;
  473. // Otherwise, this operand could commute if all operands are equivalent, or
  474. // there is a pair of operands that compare less than and a pair that
  475. // compare greater than.
  476. bool HasLT = false, HasGT = false;
  477. for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
  478. if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class)
  479. HasLT = true;
  480. if (*RHS.AsmOperands[i].Class < *AsmOperands[i].Class)
  481. HasGT = true;
  482. }
  483. return !(HasLT ^ HasGT);
  484. }
  485. void dump() const;
  486. private:
  487. void tokenizeAsmString(const AsmMatcherInfo &Info);
  488. void addAsmOperand(size_t Start, size_t End);
  489. };
  490. /// SubtargetFeatureInfo - Helper class for storing information on a subtarget
  491. /// feature which participates in instruction matching.
  492. struct SubtargetFeatureInfo {
  493. /// \brief The predicate record for this feature.
  494. Record *TheDef;
  495. /// \brief An unique index assigned to represent this feature.
  496. uint64_t Index;
  497. SubtargetFeatureInfo(Record *D, uint64_t Idx) : TheDef(D), Index(Idx) {}
  498. /// \brief The name of the enumerated constant identifying this feature.
  499. std::string getEnumName() const {
  500. return "Feature_" + TheDef->getName();
  501. }
  502. void dump() const {
  503. errs() << getEnumName() << " " << Index << "\n";
  504. TheDef->dump();
  505. }
  506. };
  507. struct OperandMatchEntry {
  508. unsigned OperandMask;
  509. const MatchableInfo* MI;
  510. ClassInfo *CI;
  511. static OperandMatchEntry create(const MatchableInfo *mi, ClassInfo *ci,
  512. unsigned opMask) {
  513. OperandMatchEntry X;
  514. X.OperandMask = opMask;
  515. X.CI = ci;
  516. X.MI = mi;
  517. return X;
  518. }
  519. };
  520. class AsmMatcherInfo {
  521. public:
  522. /// Tracked Records
  523. RecordKeeper &Records;
  524. /// The tablegen AsmParser record.
  525. Record *AsmParser;
  526. /// Target - The target information.
  527. CodeGenTarget &Target;
  528. /// The classes which are needed for matching.
  529. std::forward_list<ClassInfo> Classes;
  530. /// The information on the matchables to match.
  531. std::vector<std::unique_ptr<MatchableInfo>> Matchables;
  532. /// Info for custom matching operands by user defined methods.
  533. std::vector<OperandMatchEntry> OperandMatchInfo;
  534. /// Map of Register records to their class information.
  535. typedef std::map<Record*, ClassInfo*, LessRecordByID> RegisterClassesTy;
  536. RegisterClassesTy RegisterClasses;
  537. /// Map of Predicate records to their subtarget information.
  538. std::map<Record *, SubtargetFeatureInfo, LessRecordByID> SubtargetFeatures;
  539. /// Map of AsmOperandClass records to their class information.
  540. std::map<Record*, ClassInfo*> AsmOperandClasses;
  541. private:
  542. /// Map of token to class information which has already been constructed.
  543. std::map<std::string, ClassInfo*> TokenClasses;
  544. /// Map of RegisterClass records to their class information.
  545. std::map<Record*, ClassInfo*> RegisterClassClasses;
  546. private:
  547. /// getTokenClass - Lookup or create the class for the given token.
  548. ClassInfo *getTokenClass(StringRef Token);
  549. /// getOperandClass - Lookup or create the class for the given operand.
  550. ClassInfo *getOperandClass(const CGIOperandList::OperandInfo &OI,
  551. int SubOpIdx);
  552. ClassInfo *getOperandClass(Record *Rec, int SubOpIdx);
  553. /// buildRegisterClasses - Build the ClassInfo* instances for register
  554. /// classes.
  555. void buildRegisterClasses(SmallPtrSetImpl<Record*> &SingletonRegisters);
  556. /// buildOperandClasses - Build the ClassInfo* instances for user defined
  557. /// operand classes.
  558. void buildOperandClasses();
  559. void buildInstructionOperandReference(MatchableInfo *II, StringRef OpName,
  560. unsigned AsmOpIdx);
  561. void buildAliasOperandReference(MatchableInfo *II, StringRef OpName,
  562. MatchableInfo::AsmOperand &Op);
  563. public:
  564. AsmMatcherInfo(Record *AsmParser,
  565. CodeGenTarget &Target,
  566. RecordKeeper &Records);
  567. /// buildInfo - Construct the various tables used during matching.
  568. void buildInfo();
  569. /// buildOperandMatchInfo - Build the necessary information to handle user
  570. /// defined operand parsing methods.
  571. void buildOperandMatchInfo();
  572. /// getSubtargetFeature - Lookup or create the subtarget feature info for the
  573. /// given operand.
  574. const SubtargetFeatureInfo *getSubtargetFeature(Record *Def) const {
  575. assert(Def->isSubClassOf("Predicate") && "Invalid predicate type!");
  576. const auto &I = SubtargetFeatures.find(Def);
  577. return I == SubtargetFeatures.end() ? nullptr : &I->second;
  578. }
  579. RecordKeeper &getRecords() const {
  580. return Records;
  581. }
  582. };
  583. } // End anonymous namespace
  584. void MatchableInfo::dump() const {
  585. errs() << TheDef->getName() << " -- " << "flattened:\"" << AsmString <<"\"\n";
  586. for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
  587. const AsmOperand &Op = AsmOperands[i];
  588. errs() << " op[" << i << "] = " << Op.Class->ClassName << " - ";
  589. errs() << '\"' << Op.Token << "\"\n";
  590. }
  591. }
  592. static std::pair<StringRef, StringRef>
  593. parseTwoOperandConstraint(StringRef S, ArrayRef<SMLoc> Loc) {
  594. // Split via the '='.
  595. std::pair<StringRef, StringRef> Ops = S.split('=');
  596. if (Ops.second == "")
  597. PrintFatalError(Loc, "missing '=' in two-operand alias constraint");
  598. // Trim whitespace and the leading '$' on the operand names.
  599. size_t start = Ops.first.find_first_of('$');
  600. if (start == std::string::npos)
  601. PrintFatalError(Loc, "expected '$' prefix on asm operand name");
  602. Ops.first = Ops.first.slice(start + 1, std::string::npos);
  603. size_t end = Ops.first.find_last_of(" \t");
  604. Ops.first = Ops.first.slice(0, end);
  605. // Now the second operand.
  606. start = Ops.second.find_first_of('$');
  607. if (start == std::string::npos)
  608. PrintFatalError(Loc, "expected '$' prefix on asm operand name");
  609. Ops.second = Ops.second.slice(start + 1, std::string::npos);
  610. end = Ops.second.find_last_of(" \t");
  611. Ops.first = Ops.first.slice(0, end);
  612. return Ops;
  613. }
  614. void MatchableInfo::formTwoOperandAlias(StringRef Constraint) {
  615. // Figure out which operands are aliased and mark them as tied.
  616. std::pair<StringRef, StringRef> Ops =
  617. parseTwoOperandConstraint(Constraint, TheDef->getLoc());
  618. // Find the AsmOperands that refer to the operands we're aliasing.
  619. int SrcAsmOperand = findAsmOperandNamed(Ops.first);
  620. int DstAsmOperand = findAsmOperandNamed(Ops.second);
  621. if (SrcAsmOperand == -1)
  622. PrintFatalError(TheDef->getLoc(),
  623. "unknown source two-operand alias operand '" + Ops.first +
  624. "'.");
  625. if (DstAsmOperand == -1)
  626. PrintFatalError(TheDef->getLoc(),
  627. "unknown destination two-operand alias operand '" +
  628. Ops.second + "'.");
  629. // Find the ResOperand that refers to the operand we're aliasing away
  630. // and update it to refer to the combined operand instead.
  631. for (unsigned i = 0, e = ResOperands.size(); i != e; ++i) {
  632. ResOperand &Op = ResOperands[i];
  633. if (Op.Kind == ResOperand::RenderAsmOperand &&
  634. Op.AsmOperandNum == (unsigned)SrcAsmOperand) {
  635. Op.AsmOperandNum = DstAsmOperand;
  636. break;
  637. }
  638. }
  639. // Remove the AsmOperand for the alias operand.
  640. AsmOperands.erase(AsmOperands.begin() + SrcAsmOperand);
  641. // Adjust the ResOperand references to any AsmOperands that followed
  642. // the one we just deleted.
  643. for (unsigned i = 0, e = ResOperands.size(); i != e; ++i) {
  644. ResOperand &Op = ResOperands[i];
  645. switch(Op.Kind) {
  646. default:
  647. // Nothing to do for operands that don't reference AsmOperands.
  648. break;
  649. case ResOperand::RenderAsmOperand:
  650. if (Op.AsmOperandNum > (unsigned)SrcAsmOperand)
  651. --Op.AsmOperandNum;
  652. break;
  653. case ResOperand::TiedOperand:
  654. if (Op.TiedOperandNum > (unsigned)SrcAsmOperand)
  655. --Op.TiedOperandNum;
  656. break;
  657. }
  658. }
  659. }
  660. void MatchableInfo::initialize(const AsmMatcherInfo &Info,
  661. SmallPtrSetImpl<Record*> &SingletonRegisters,
  662. int AsmVariantNo, std::string &RegisterPrefix) {
  663. AsmVariantID = AsmVariantNo;
  664. AsmString =
  665. CodeGenInstruction::FlattenAsmStringVariants(AsmString, AsmVariantNo);
  666. tokenizeAsmString(Info);
  667. // Compute the require features.
  668. std::vector<Record*> Predicates =TheDef->getValueAsListOfDefs("Predicates");
  669. for (unsigned i = 0, e = Predicates.size(); i != e; ++i)
  670. if (const SubtargetFeatureInfo *Feature =
  671. Info.getSubtargetFeature(Predicates[i]))
  672. RequiredFeatures.push_back(Feature);
  673. // Collect singleton registers, if used.
  674. for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
  675. extractSingletonRegisterForAsmOperand(i, Info, RegisterPrefix);
  676. if (Record *Reg = AsmOperands[i].SingletonReg)
  677. SingletonRegisters.insert(Reg);
  678. }
  679. const RecordVal *DepMask = TheDef->getValue("DeprecatedFeatureMask");
  680. if (!DepMask)
  681. DepMask = TheDef->getValue("ComplexDeprecationPredicate");
  682. HasDeprecation =
  683. DepMask ? !DepMask->getValue()->getAsUnquotedString().empty() : false;
  684. }
  685. /// Append an AsmOperand for the given substring of AsmString.
  686. void MatchableInfo::addAsmOperand(size_t Start, size_t End) {
  687. StringRef String = AsmString;
  688. StringRef Separators = "[]*! \t,";
  689. // Look for separators before and after to figure out is this token is
  690. // isolated. Accept '$$' as that's how we escape '$'.
  691. bool IsIsolatedToken =
  692. (!Start || Separators.find(String[Start - 1]) != StringRef::npos ||
  693. String.substr(Start - 1, 2) == "$$") &&
  694. (End >= String.size() || Separators.find(String[End]) != StringRef::npos);
  695. AsmOperands.push_back(AsmOperand(IsIsolatedToken, String.slice(Start, End)));
  696. }
  697. /// tokenizeAsmString - Tokenize a simplified assembly string.
  698. void MatchableInfo::tokenizeAsmString(const AsmMatcherInfo &Info) {
  699. StringRef String = AsmString;
  700. unsigned Prev = 0;
  701. bool InTok = true;
  702. for (unsigned i = 0, e = String.size(); i != e; ++i) {
  703. switch (String[i]) {
  704. case '[':
  705. case ']':
  706. case '*':
  707. case '!':
  708. case ' ':
  709. case '\t':
  710. case ',':
  711. if (InTok) {
  712. addAsmOperand(Prev, i);
  713. InTok = false;
  714. }
  715. if (!isspace(String[i]) && String[i] != ',')
  716. addAsmOperand(i, i + 1);
  717. Prev = i + 1;
  718. break;
  719. case '\\':
  720. if (InTok) {
  721. addAsmOperand(Prev, i);
  722. InTok = false;
  723. }
  724. ++i;
  725. assert(i != String.size() && "Invalid quoted character");
  726. addAsmOperand(i, i + 1);
  727. Prev = i + 1;
  728. break;
  729. case '$': {
  730. if (InTok) {
  731. addAsmOperand(Prev, i);
  732. InTok = false;
  733. }
  734. // If this isn't "${", treat like a normal token.
  735. if (i + 1 == String.size() || String[i + 1] != '{') {
  736. Prev = i;
  737. break;
  738. }
  739. StringRef::iterator End = std::find(String.begin() + i, String.end(),'}');
  740. assert(End != String.end() && "Missing brace in operand reference!");
  741. size_t EndPos = End - String.begin();
  742. addAsmOperand(i, EndPos+1);
  743. Prev = EndPos + 1;
  744. i = EndPos;
  745. break;
  746. }
  747. case '.':
  748. if (!Info.AsmParser->getValueAsBit("MnemonicContainsDot")) {
  749. if (InTok)
  750. addAsmOperand(Prev, i);
  751. Prev = i;
  752. }
  753. InTok = true;
  754. break;
  755. default:
  756. InTok = true;
  757. }
  758. }
  759. if (InTok && Prev != String.size())
  760. addAsmOperand(Prev, StringRef::npos);
  761. // The first token of the instruction is the mnemonic, which must be a
  762. // simple string, not a $foo variable or a singleton register.
  763. if (AsmOperands.empty())
  764. PrintFatalError(TheDef->getLoc(),
  765. "Instruction '" + TheDef->getName() + "' has no tokens");
  766. Mnemonic = AsmOperands[0].Token;
  767. if (Mnemonic.empty())
  768. PrintFatalError(TheDef->getLoc(),
  769. "Missing instruction mnemonic");
  770. // FIXME : Check and raise an error if it is a register.
  771. if (Mnemonic[0] == '$')
  772. PrintFatalError(TheDef->getLoc(),
  773. "Invalid instruction mnemonic '" + Mnemonic + "'!");
  774. // Remove the first operand, it is tracked in the mnemonic field.
  775. AsmOperands.erase(AsmOperands.begin());
  776. }
  777. bool MatchableInfo::validate(StringRef CommentDelimiter, bool Hack) const {
  778. // Reject matchables with no .s string.
  779. if (AsmString.empty())
  780. PrintFatalError(TheDef->getLoc(), "instruction with empty asm string");
  781. // Reject any matchables with a newline in them, they should be marked
  782. // isCodeGenOnly if they are pseudo instructions.
  783. if (AsmString.find('\n') != std::string::npos)
  784. PrintFatalError(TheDef->getLoc(),
  785. "multiline instruction is not valid for the asmparser, "
  786. "mark it isCodeGenOnly");
  787. // Remove comments from the asm string. We know that the asmstring only
  788. // has one line.
  789. if (!CommentDelimiter.empty() &&
  790. StringRef(AsmString).find(CommentDelimiter) != StringRef::npos)
  791. PrintFatalError(TheDef->getLoc(),
  792. "asmstring for instruction has comment character in it, "
  793. "mark it isCodeGenOnly");
  794. // Reject matchables with operand modifiers, these aren't something we can
  795. // handle, the target should be refactored to use operands instead of
  796. // modifiers.
  797. //
  798. // Also, check for instructions which reference the operand multiple times;
  799. // this implies a constraint we would not honor.
  800. std::set<std::string> OperandNames;
  801. for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
  802. StringRef Tok = AsmOperands[i].Token;
  803. if (Tok[0] == '$' && Tok.find(':') != StringRef::npos)
  804. PrintFatalError(TheDef->getLoc(),
  805. "matchable with operand modifier '" + Tok +
  806. "' not supported by asm matcher. Mark isCodeGenOnly!");
  807. // Verify that any operand is only mentioned once.
  808. // We reject aliases and ignore instructions for now.
  809. if (Tok[0] == '$' && !OperandNames.insert(Tok).second) {
  810. if (!Hack)
  811. PrintFatalError(TheDef->getLoc(),
  812. "ERROR: matchable with tied operand '" + Tok +
  813. "' can never be matched!");
  814. // FIXME: Should reject these. The ARM backend hits this with $lane in a
  815. // bunch of instructions. It is unclear what the right answer is.
  816. DEBUG({
  817. errs() << "warning: '" << TheDef->getName() << "': "
  818. << "ignoring instruction with tied operand '"
  819. << Tok << "'\n";
  820. });
  821. return false;
  822. }
  823. }
  824. return true;
  825. }
  826. /// extractSingletonRegisterForAsmOperand - Extract singleton register,
  827. /// if present, from specified token.
  828. void MatchableInfo::
  829. extractSingletonRegisterForAsmOperand(unsigned OperandNo,
  830. const AsmMatcherInfo &Info,
  831. std::string &RegisterPrefix) {
  832. StringRef Tok = AsmOperands[OperandNo].Token;
  833. // If this token is not an isolated token, i.e., it isn't separated from
  834. // other tokens (e.g. with whitespace), don't interpret it as a register name.
  835. if (!AsmOperands[OperandNo].IsIsolatedToken)
  836. return;
  837. if (RegisterPrefix.empty()) {
  838. std::string LoweredTok = Tok.lower();
  839. if (const CodeGenRegister *Reg = Info.Target.getRegisterByName(LoweredTok))
  840. AsmOperands[OperandNo].SingletonReg = Reg->TheDef;
  841. return;
  842. }
  843. if (!Tok.startswith(RegisterPrefix))
  844. return;
  845. StringRef RegName = Tok.substr(RegisterPrefix.size());
  846. if (const CodeGenRegister *Reg = Info.Target.getRegisterByName(RegName))
  847. AsmOperands[OperandNo].SingletonReg = Reg->TheDef;
  848. // If there is no register prefix (i.e. "%" in "%eax"), then this may
  849. // be some random non-register token, just ignore it.
  850. return;
  851. }
  852. static std::string getEnumNameForToken(StringRef Str) {
  853. std::string Res;
  854. for (StringRef::iterator it = Str.begin(), ie = Str.end(); it != ie; ++it) {
  855. switch (*it) {
  856. case '*': Res += "_STAR_"; break;
  857. case '%': Res += "_PCT_"; break;
  858. case ':': Res += "_COLON_"; break;
  859. case '!': Res += "_EXCLAIM_"; break;
  860. case '.': Res += "_DOT_"; break;
  861. case '<': Res += "_LT_"; break;
  862. case '>': Res += "_GT_"; break;
  863. case '-': Res += "_MINUS_"; break;
  864. default:
  865. if ((*it >= 'A' && *it <= 'Z') ||
  866. (*it >= 'a' && *it <= 'z') ||
  867. (*it >= '0' && *it <= '9'))
  868. Res += *it;
  869. else
  870. Res += "_" + utostr((unsigned) *it) + "_";
  871. }
  872. }
  873. return Res;
  874. }
  875. ClassInfo *AsmMatcherInfo::getTokenClass(StringRef Token) {
  876. ClassInfo *&Entry = TokenClasses[Token];
  877. if (!Entry) {
  878. Classes.emplace_front();
  879. Entry = &Classes.front();
  880. Entry->Kind = ClassInfo::Token;
  881. Entry->ClassName = "Token";
  882. Entry->Name = "MCK_" + getEnumNameForToken(Token);
  883. Entry->ValueName = Token;
  884. Entry->PredicateMethod = "<invalid>";
  885. Entry->RenderMethod = "<invalid>";
  886. Entry->ParserMethod = "";
  887. Entry->DiagnosticType = "";
  888. }
  889. return Entry;
  890. }
  891. ClassInfo *
  892. AsmMatcherInfo::getOperandClass(const CGIOperandList::OperandInfo &OI,
  893. int SubOpIdx) {
  894. Record *Rec = OI.Rec;
  895. if (SubOpIdx != -1)
  896. Rec = cast<DefInit>(OI.MIOperandInfo->getArg(SubOpIdx))->getDef();
  897. return getOperandClass(Rec, SubOpIdx);
  898. }
  899. ClassInfo *
  900. AsmMatcherInfo::getOperandClass(Record *Rec, int SubOpIdx) {
  901. if (Rec->isSubClassOf("RegisterOperand")) {
  902. // RegisterOperand may have an associated ParserMatchClass. If it does,
  903. // use it, else just fall back to the underlying register class.
  904. const RecordVal *R = Rec->getValue("ParserMatchClass");
  905. if (!R || !R->getValue())
  906. PrintFatalError("Record `" + Rec->getName() +
  907. "' does not have a ParserMatchClass!\n");
  908. if (DefInit *DI= dyn_cast<DefInit>(R->getValue())) {
  909. Record *MatchClass = DI->getDef();
  910. if (ClassInfo *CI = AsmOperandClasses[MatchClass])
  911. return CI;
  912. }
  913. // No custom match class. Just use the register class.
  914. Record *ClassRec = Rec->getValueAsDef("RegClass");
  915. if (!ClassRec)
  916. PrintFatalError(Rec->getLoc(), "RegisterOperand `" + Rec->getName() +
  917. "' has no associated register class!\n");
  918. if (ClassInfo *CI = RegisterClassClasses[ClassRec])
  919. return CI;
  920. PrintFatalError(Rec->getLoc(), "register class has no class info!");
  921. }
  922. if (Rec->isSubClassOf("RegisterClass")) {
  923. if (ClassInfo *CI = RegisterClassClasses[Rec])
  924. return CI;
  925. PrintFatalError(Rec->getLoc(), "register class has no class info!");
  926. }
  927. if (!Rec->isSubClassOf("Operand"))
  928. PrintFatalError(Rec->getLoc(), "Operand `" + Rec->getName() +
  929. "' does not derive from class Operand!\n");
  930. Record *MatchClass = Rec->getValueAsDef("ParserMatchClass");
  931. if (ClassInfo *CI = AsmOperandClasses[MatchClass])
  932. return CI;
  933. PrintFatalError(Rec->getLoc(), "operand has no match class!");
  934. }
  935. struct LessRegisterSet {
  936. bool operator() (const RegisterSet &LHS, const RegisterSet & RHS) const {
  937. // std::set<T> defines its own compariso "operator<", but it
  938. // performs a lexicographical comparison by T's innate comparison
  939. // for some reason. We don't want non-deterministic pointer
  940. // comparisons so use this instead.
  941. return std::lexicographical_compare(LHS.begin(), LHS.end(),
  942. RHS.begin(), RHS.end(),
  943. LessRecordByID());
  944. }
  945. };
  946. void AsmMatcherInfo::
  947. buildRegisterClasses(SmallPtrSetImpl<Record*> &SingletonRegisters) {
  948. const auto &Registers = Target.getRegBank().getRegisters();
  949. auto &RegClassList = Target.getRegBank().getRegClasses();
  950. typedef std::set<RegisterSet, LessRegisterSet> RegisterSetSet;
  951. // The register sets used for matching.
  952. RegisterSetSet RegisterSets;
  953. // Gather the defined sets.
  954. for (const CodeGenRegisterClass &RC : RegClassList)
  955. RegisterSets.insert(
  956. RegisterSet(RC.getOrder().begin(), RC.getOrder().end()));
  957. // Add any required singleton sets.
  958. for (Record *Rec : SingletonRegisters) {
  959. RegisterSets.insert(RegisterSet(&Rec, &Rec + 1));
  960. }
  961. // Introduce derived sets where necessary (when a register does not determine
  962. // a unique register set class), and build the mapping of registers to the set
  963. // they should classify to.
  964. std::map<Record*, RegisterSet> RegisterMap;
  965. for (const CodeGenRegister &CGR : Registers) {
  966. // Compute the intersection of all sets containing this register.
  967. RegisterSet ContainingSet;
  968. for (const RegisterSet &RS : RegisterSets) {
  969. if (!RS.count(CGR.TheDef))
  970. continue;
  971. if (ContainingSet.empty()) {
  972. ContainingSet = RS;
  973. continue;
  974. }
  975. RegisterSet Tmp;
  976. std::swap(Tmp, ContainingSet);
  977. std::insert_iterator<RegisterSet> II(ContainingSet,
  978. ContainingSet.begin());
  979. std::set_intersection(Tmp.begin(), Tmp.end(), RS.begin(), RS.end(), II,
  980. LessRecordByID());
  981. }
  982. if (!ContainingSet.empty()) {
  983. RegisterSets.insert(ContainingSet);
  984. RegisterMap.insert(std::make_pair(CGR.TheDef, ContainingSet));
  985. }
  986. }
  987. // Construct the register classes.
  988. std::map<RegisterSet, ClassInfo*, LessRegisterSet> RegisterSetClasses;
  989. unsigned Index = 0;
  990. for (const RegisterSet &RS : RegisterSets) {
  991. Classes.emplace_front();
  992. ClassInfo *CI = &Classes.front();
  993. CI->Kind = ClassInfo::RegisterClass0 + Index;
  994. CI->ClassName = "Reg" + utostr(Index);
  995. CI->Name = "MCK_Reg" + utostr(Index);
  996. CI->ValueName = "";
  997. CI->PredicateMethod = ""; // unused
  998. CI->RenderMethod = "addRegOperands";
  999. CI->Registers = RS;
  1000. // FIXME: diagnostic type.
  1001. CI->DiagnosticType = "";
  1002. RegisterSetClasses.insert(std::make_pair(RS, CI));
  1003. ++Index;
  1004. }
  1005. // Find the superclasses; we could compute only the subgroup lattice edges,
  1006. // but there isn't really a point.
  1007. for (const RegisterSet &RS : RegisterSets) {
  1008. ClassInfo *CI = RegisterSetClasses[RS];
  1009. for (const RegisterSet &RS2 : RegisterSets)
  1010. if (RS != RS2 &&
  1011. std::includes(RS2.begin(), RS2.end(), RS.begin(), RS.end(),
  1012. LessRecordByID()))
  1013. CI->SuperClasses.push_back(RegisterSetClasses[RS2]);
  1014. }
  1015. // Name the register classes which correspond to a user defined RegisterClass.
  1016. for (const CodeGenRegisterClass &RC : RegClassList) {
  1017. // Def will be NULL for non-user defined register classes.
  1018. Record *Def = RC.getDef();
  1019. if (!Def)
  1020. continue;
  1021. ClassInfo *CI = RegisterSetClasses[RegisterSet(RC.getOrder().begin(),
  1022. RC.getOrder().end())];
  1023. if (CI->ValueName.empty()) {
  1024. CI->ClassName = RC.getName();
  1025. CI->Name = "MCK_" + RC.getName();
  1026. CI->ValueName = RC.getName();
  1027. } else
  1028. CI->ValueName = CI->ValueName + "," + RC.getName();
  1029. RegisterClassClasses.insert(std::make_pair(Def, CI));
  1030. }
  1031. // Populate the map for individual registers.
  1032. for (std::map<Record*, RegisterSet>::iterator it = RegisterMap.begin(),
  1033. ie = RegisterMap.end(); it != ie; ++it)
  1034. RegisterClasses[it->first] = RegisterSetClasses[it->second];
  1035. // Name the register classes which correspond to singleton registers.
  1036. for (Record *Rec : SingletonRegisters) {
  1037. ClassInfo *CI = RegisterClasses[Rec];
  1038. assert(CI && "Missing singleton register class info!");
  1039. if (CI->ValueName.empty()) {
  1040. CI->ClassName = Rec->getName();
  1041. CI->Name = "MCK_" + Rec->getName();
  1042. CI->ValueName = Rec->getName();
  1043. } else
  1044. CI->ValueName = CI->ValueName + "," + Rec->getName();
  1045. }
  1046. }
  1047. void AsmMatcherInfo::buildOperandClasses() {
  1048. std::vector<Record*> AsmOperands =
  1049. Records.getAllDerivedDefinitions("AsmOperandClass");
  1050. // Pre-populate AsmOperandClasses map.
  1051. for (Record *Rec : AsmOperands) {
  1052. Classes.emplace_front();
  1053. AsmOperandClasses[Rec] = &Classes.front();
  1054. }
  1055. unsigned Index = 0;
  1056. for (Record *Rec : AsmOperands) {
  1057. ClassInfo *CI = AsmOperandClasses[Rec];
  1058. CI->Kind = ClassInfo::UserClass0 + Index;
  1059. ListInit *Supers = Rec->getValueAsListInit("SuperClasses");
  1060. for (Init *I : Supers->getValues()) {
  1061. DefInit *DI = dyn_cast<DefInit>(I);
  1062. if (!DI) {
  1063. PrintError(Rec->getLoc(), "Invalid super class reference!");
  1064. continue;
  1065. }
  1066. ClassInfo *SC = AsmOperandClasses[DI->getDef()];
  1067. if (!SC)
  1068. PrintError(Rec->getLoc(), "Invalid super class reference!");
  1069. else
  1070. CI->SuperClasses.push_back(SC);
  1071. }
  1072. CI->ClassName = Rec->getValueAsString("Name");
  1073. CI->Name = "MCK_" + CI->ClassName;
  1074. CI->ValueName = Rec->getName();
  1075. // Get or construct the predicate method name.
  1076. Init *PMName = Rec->getValueInit("PredicateMethod");
  1077. if (StringInit *SI = dyn_cast<StringInit>(PMName)) {
  1078. CI->PredicateMethod = SI->getValue();
  1079. } else {
  1080. assert(isa<UnsetInit>(PMName) && "Unexpected PredicateMethod field!");
  1081. CI->PredicateMethod = "is" + CI->ClassName;
  1082. }
  1083. // Get or construct the render method name.
  1084. Init *RMName = Rec->getValueInit("RenderMethod");
  1085. if (StringInit *SI = dyn_cast<StringInit>(RMName)) {
  1086. CI->RenderMethod = SI->getValue();
  1087. } else {
  1088. assert(isa<UnsetInit>(RMName) && "Unexpected RenderMethod field!");
  1089. CI->RenderMethod = "add" + CI->ClassName + "Operands";
  1090. }
  1091. // Get the parse method name or leave it as empty.
  1092. Init *PRMName = Rec->getValueInit("ParserMethod");
  1093. if (StringInit *SI = dyn_cast<StringInit>(PRMName))
  1094. CI->ParserMethod = SI->getValue();
  1095. // Get the diagnostic type or leave it as empty.
  1096. // Get the parse method name or leave it as empty.
  1097. Init *DiagnosticType = Rec->getValueInit("DiagnosticType");
  1098. if (StringInit *SI = dyn_cast<StringInit>(DiagnosticType))
  1099. CI->DiagnosticType = SI->getValue();
  1100. ++Index;
  1101. }
  1102. }
  1103. AsmMatcherInfo::AsmMatcherInfo(Record *asmParser,
  1104. CodeGenTarget &target,
  1105. RecordKeeper &records)
  1106. : Records(records), AsmParser(asmParser), Target(target) {
  1107. }
  1108. /// buildOperandMatchInfo - Build the necessary information to handle user
  1109. /// defined operand parsing methods.
  1110. void AsmMatcherInfo::buildOperandMatchInfo() {
  1111. /// Map containing a mask with all operands indices that can be found for
  1112. /// that class inside a instruction.
  1113. typedef std::map<ClassInfo *, unsigned, less_ptr<ClassInfo>> OpClassMaskTy;
  1114. OpClassMaskTy OpClassMask;
  1115. for (const auto &MI : Matchables) {
  1116. OpClassMask.clear();
  1117. // Keep track of all operands of this instructions which belong to the
  1118. // same class.
  1119. for (unsigned i = 0, e = MI->AsmOperands.size(); i != e; ++i) {
  1120. const MatchableInfo::AsmOperand &Op = MI->AsmOperands[i];
  1121. if (Op.Class->ParserMethod.empty())
  1122. continue;
  1123. unsigned &OperandMask = OpClassMask[Op.Class];
  1124. OperandMask |= (1 << i);
  1125. }
  1126. // Generate operand match info for each mnemonic/operand class pair.
  1127. for (const auto &OCM : OpClassMask) {
  1128. unsigned OpMask = OCM.second;
  1129. ClassInfo *CI = OCM.first;
  1130. OperandMatchInfo.push_back(OperandMatchEntry::create(MI.get(), CI,
  1131. OpMask));
  1132. }
  1133. }
  1134. }
  1135. void AsmMatcherInfo::buildInfo() {
  1136. // Build information about all of the AssemblerPredicates.
  1137. std::vector<Record*> AllPredicates =
  1138. Records.getAllDerivedDefinitions("Predicate");
  1139. for (unsigned i = 0, e = AllPredicates.size(); i != e; ++i) {
  1140. Record *Pred = AllPredicates[i];
  1141. // Ignore predicates that are not intended for the assembler.
  1142. if (!Pred->getValueAsBit("AssemblerMatcherPredicate"))
  1143. continue;
  1144. if (Pred->getName().empty())
  1145. PrintFatalError(Pred->getLoc(), "Predicate has no name!");
  1146. SubtargetFeatures.insert(std::make_pair(
  1147. Pred, SubtargetFeatureInfo(Pred, SubtargetFeatures.size())));
  1148. DEBUG(SubtargetFeatures.find(Pred)->second.dump());
  1149. assert(SubtargetFeatures.size() <= 64 && "Too many subtarget features!");
  1150. }
  1151. // Parse the instructions; we need to do this first so that we can gather the
  1152. // singleton register classes.
  1153. SmallPtrSet<Record*, 16> SingletonRegisters;
  1154. unsigned VariantCount = Target.getAsmParserVariantCount();
  1155. for (unsigned VC = 0; VC != VariantCount; ++VC) {
  1156. Record *AsmVariant = Target.getAsmParserVariant(VC);
  1157. std::string CommentDelimiter =
  1158. AsmVariant->getValueAsString("CommentDelimiter");
  1159. std::string RegisterPrefix = AsmVariant->getValueAsString("RegisterPrefix");
  1160. int AsmVariantNo = AsmVariant->getValueAsInt("Variant");
  1161. for (const CodeGenInstruction *CGI : Target.instructions()) {
  1162. // If the tblgen -match-prefix option is specified (for tblgen hackers),
  1163. // filter the set of instructions we consider.
  1164. if (!StringRef(CGI->TheDef->getName()).startswith(MatchPrefix))
  1165. continue;
  1166. // Ignore "codegen only" instructions.
  1167. if (CGI->TheDef->getValueAsBit("isCodeGenOnly"))
  1168. continue;
  1169. std::unique_ptr<MatchableInfo> II(new MatchableInfo(*CGI));
  1170. II->initialize(*this, SingletonRegisters, AsmVariantNo, RegisterPrefix);
  1171. // Ignore instructions which shouldn't be matched and diagnose invalid
  1172. // instruction definitions with an error.
  1173. if (!II->validate(CommentDelimiter, true))
  1174. continue;
  1175. Matchables.push_back(std::move(II));
  1176. }
  1177. // Parse all of the InstAlias definitions and stick them in the list of
  1178. // matchables.
  1179. std::vector<Record*> AllInstAliases =
  1180. Records.getAllDerivedDefinitions("InstAlias");
  1181. for (unsigned i = 0, e = AllInstAliases.size(); i != e; ++i) {
  1182. auto Alias = llvm::make_unique<CodeGenInstAlias>(AllInstAliases[i],
  1183. AsmVariantNo, Target);
  1184. // If the tblgen -match-prefix option is specified (for tblgen hackers),
  1185. // filter the set of instruction aliases we consider, based on the target
  1186. // instruction.
  1187. if (!StringRef(Alias->ResultInst->TheDef->getName())
  1188. .startswith( MatchPrefix))
  1189. continue;
  1190. std::unique_ptr<MatchableInfo> II(new MatchableInfo(std::move(Alias)));
  1191. II->initialize(*this, SingletonRegisters, AsmVariantNo, RegisterPrefix);
  1192. // Validate the alias definitions.
  1193. II->validate(CommentDelimiter, false);
  1194. Matchables.push_back(std::move(II));
  1195. }
  1196. }
  1197. // Build info for the register classes.
  1198. buildRegisterClasses(SingletonRegisters);
  1199. // Build info for the user defined assembly operand classes.
  1200. buildOperandClasses();
  1201. // Build the information about matchables, now that we have fully formed
  1202. // classes.
  1203. std::vector<std::unique_ptr<MatchableInfo>> NewMatchables;
  1204. for (auto &II : Matchables) {
  1205. // Parse the tokens after the mnemonic.
  1206. // Note: buildInstructionOperandReference may insert new AsmOperands, so
  1207. // don't precompute the loop bound.
  1208. for (unsigned i = 0; i != II->AsmOperands.size(); ++i) {
  1209. MatchableInfo::AsmOperand &Op = II->AsmOperands[i];
  1210. StringRef Token = Op.Token;
  1211. // Check for singleton registers.
  1212. if (Record *RegRecord = II->AsmOperands[i].SingletonReg) {
  1213. Op.Class = RegisterClasses[RegRecord];
  1214. assert(Op.Class && Op.Class->Registers.size() == 1 &&
  1215. "Unexpected class for singleton register");
  1216. continue;
  1217. }
  1218. // Check for simple tokens.
  1219. if (Token[0] != '$') {
  1220. Op.Class = getTokenClass(Token);
  1221. continue;
  1222. }
  1223. if (Token.size() > 1 && isdigit(Token[1])) {
  1224. Op.Class = getTokenClass(Token);
  1225. continue;
  1226. }
  1227. // Otherwise this is an operand reference.
  1228. StringRef OperandName;
  1229. if (Token[1] == '{')
  1230. OperandName = Token.substr(2, Token.size() - 3);
  1231. else
  1232. OperandName = Token.substr(1);
  1233. if (II->DefRec.is<const CodeGenInstruction*>())
  1234. buildInstructionOperandReference(II.get(), OperandName, i);
  1235. else
  1236. buildAliasOperandReference(II.get(), OperandName, Op);
  1237. }
  1238. if (II->DefRec.is<const CodeGenInstruction*>()) {
  1239. II->buildInstructionResultOperands();
  1240. // If the instruction has a two-operand alias, build up the
  1241. // matchable here. We'll add them in bulk at the end to avoid
  1242. // confusing this loop.
  1243. std::string Constraint =
  1244. II->TheDef->getValueAsString("TwoOperandAliasConstraint");
  1245. if (Constraint != "") {
  1246. // Start by making a copy of the original matchable.
  1247. std::unique_ptr<MatchableInfo> AliasII(new MatchableInfo(*II));
  1248. // Adjust it to be a two-operand alias.
  1249. AliasII->formTwoOperandAlias(Constraint);
  1250. // Add the alias to the matchables list.
  1251. NewMatchables.push_back(std::move(AliasII));
  1252. }
  1253. } else
  1254. II->buildAliasResultOperands();
  1255. }
  1256. if (!NewMatchables.empty())
  1257. Matchables.insert(Matchables.end(),
  1258. std::make_move_iterator(NewMatchables.begin()),
  1259. std::make_move_iterator(NewMatchables.end()));
  1260. // Process token alias definitions and set up the associated superclass
  1261. // information.
  1262. std::vector<Record*> AllTokenAliases =
  1263. Records.getAllDerivedDefinitions("TokenAlias");
  1264. for (unsigned i = 0, e = AllTokenAliases.size(); i != e; ++i) {
  1265. Record *Rec = AllTokenAliases[i];
  1266. ClassInfo *FromClass = getTokenClass(Rec->getValueAsString("FromToken"));
  1267. ClassInfo *ToClass = getTokenClass(Rec->getValueAsString("ToToken"));
  1268. if (FromClass == ToClass)
  1269. PrintFatalError(Rec->getLoc(),
  1270. "error: Destination value identical to source value.");
  1271. FromClass->SuperClasses.push_back(ToClass);
  1272. }
  1273. // Reorder classes so that classes precede super classes.
  1274. Classes.sort();
  1275. }
  1276. /// buildInstructionOperandReference - The specified operand is a reference to a
  1277. /// named operand such as $src. Resolve the Class and OperandInfo pointers.
  1278. void AsmMatcherInfo::
  1279. buildInstructionOperandReference(MatchableInfo *II,
  1280. StringRef OperandName,
  1281. unsigned AsmOpIdx) {
  1282. const CodeGenInstruction &CGI = *II->DefRec.get<const CodeGenInstruction*>();
  1283. const CGIOperandList &Operands = CGI.Operands;
  1284. MatchableInfo::AsmOperand *Op = &II->AsmOperands[AsmOpIdx];
  1285. // Map this token to an operand.
  1286. unsigned Idx;
  1287. if (!Operands.hasOperandNamed(OperandName, Idx))
  1288. PrintFatalError(II->TheDef->getLoc(),
  1289. "error: unable to find operand: '" + OperandName + "'");
  1290. // If the instruction operand has multiple suboperands, but the parser
  1291. // match class for the asm operand is still the default "ImmAsmOperand",
  1292. // then handle each suboperand separately.
  1293. if (Op->SubOpIdx == -1 && Operands[Idx].MINumOperands > 1) {
  1294. Record *Rec = Operands[Idx].Rec;
  1295. assert(Rec->isSubClassOf("Operand") && "Unexpected operand!");
  1296. Record *MatchClass = Rec->getValueAsDef("ParserMatchClass");
  1297. if (MatchClass && MatchClass->getValueAsString("Name") == "Imm") {
  1298. // Insert remaining suboperands after AsmOpIdx in II->AsmOperands.
  1299. StringRef Token = Op->Token; // save this in case Op gets moved
  1300. for (unsigned SI = 1, SE = Operands[Idx].MINumOperands; SI != SE; ++SI) {
  1301. MatchableInfo::AsmOperand NewAsmOp(/*IsIsolatedToken=*/true, Token);
  1302. NewAsmOp.SubOpIdx = SI;
  1303. II->AsmOperands.insert(II->AsmOperands.begin()+AsmOpIdx+SI, NewAsmOp);
  1304. }
  1305. // Replace Op with first suboperand.
  1306. Op = &II->AsmOperands[AsmOpIdx]; // update the pointer in case it moved
  1307. Op->SubOpIdx = 0;
  1308. }
  1309. }
  1310. // Set up the operand class.
  1311. Op->Class = getOperandClass(Operands[Idx], Op->SubOpIdx);
  1312. // If the named operand is tied, canonicalize it to the untied operand.
  1313. // For example, something like:
  1314. // (outs GPR:$dst), (ins GPR:$src)
  1315. // with an asmstring of
  1316. // "inc $src"
  1317. // we want to canonicalize to:
  1318. // "inc $dst"
  1319. // so that we know how to provide the $dst operand when filling in the result.
  1320. int OITied = -1;
  1321. if (Operands[Idx].MINumOperands == 1)
  1322. OITied = Operands[Idx].getTiedRegister();
  1323. if (OITied != -1) {
  1324. // The tied operand index is an MIOperand index, find the operand that
  1325. // contains it.
  1326. std::pair<unsigned, unsigned> Idx = Operands.getSubOperandNumber(OITied);
  1327. OperandName = Operands[Idx.first].Name;
  1328. Op->SubOpIdx = Idx.second;
  1329. }
  1330. Op->SrcOpName = OperandName;
  1331. }
  1332. /// buildAliasOperandReference - When parsing an operand reference out of the
  1333. /// matching string (e.g. "movsx $src, $dst"), determine what the class of the
  1334. /// operand reference is by looking it up in the result pattern definition.
  1335. void AsmMatcherInfo::buildAliasOperandReference(MatchableInfo *II,
  1336. StringRef OperandName,
  1337. MatchableInfo::AsmOperand &Op) {
  1338. const CodeGenInstAlias &CGA = *II->DefRec.get<const CodeGenInstAlias*>();
  1339. // Set up the operand class.
  1340. for (unsigned i = 0, e = CGA.ResultOperands.size(); i != e; ++i)
  1341. if (CGA.ResultOperands[i].isRecord() &&
  1342. CGA.ResultOperands[i].getName() == OperandName) {
  1343. // It's safe to go with the first one we find, because CodeGenInstAlias
  1344. // validates that all operands with the same name have the same record.
  1345. Op.SubOpIdx = CGA.ResultInstOperandIndex[i].second;
  1346. // Use the match class from the Alias definition, not the
  1347. // destination instruction, as we may have an immediate that's
  1348. // being munged by the match class.
  1349. Op.Class = getOperandClass(CGA.ResultOperands[i].getRecord(),
  1350. Op.SubOpIdx);
  1351. Op.SrcOpName = OperandName;
  1352. return;
  1353. }
  1354. PrintFatalError(II->TheDef->getLoc(),
  1355. "error: unable to find operand: '" + OperandName + "'");
  1356. }
  1357. void MatchableInfo::buildInstructionResultOperands() {
  1358. const CodeGenInstruction *ResultInst = getResultInst();
  1359. // Loop over all operands of the result instruction, determining how to
  1360. // populate them.
  1361. for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) {
  1362. const CGIOperandList::OperandInfo &OpInfo = ResultInst->Operands[i];
  1363. // If this is a tied operand, just copy from the previously handled operand.
  1364. int TiedOp = -1;
  1365. if (OpInfo.MINumOperands == 1)
  1366. TiedOp = OpInfo.getTiedRegister();
  1367. if (TiedOp != -1) {
  1368. ResOperands.push_back(ResOperand::getTiedOp(TiedOp));
  1369. continue;
  1370. }
  1371. // Find out what operand from the asmparser this MCInst operand comes from.
  1372. int SrcOperand = findAsmOperandNamed(OpInfo.Name);
  1373. if (OpInfo.Name.empty() || SrcOperand == -1) {
  1374. // This may happen for operands that are tied to a suboperand of a
  1375. // complex operand. Simply use a dummy value here; nobody should
  1376. // use this operand slot.
  1377. // FIXME: The long term goal is for the MCOperand list to not contain
  1378. // tied operands at all.
  1379. ResOperands.push_back(ResOperand::getImmOp(0));
  1380. continue;
  1381. }
  1382. // Check if the one AsmOperand populates the entire operand.
  1383. unsigned NumOperands = OpInfo.MINumOperands;
  1384. if (AsmOperands[SrcOperand].SubOpIdx == -1) {
  1385. ResOperands.push_back(ResOperand::getRenderedOp(SrcOperand, NumOperands));
  1386. continue;
  1387. }
  1388. // Add a separate ResOperand for each suboperand.
  1389. for (unsigned AI = 0; AI < NumOperands; ++AI) {
  1390. assert(AsmOperands[SrcOperand+AI].SubOpIdx == (int)AI &&
  1391. AsmOperands[SrcOperand+AI].SrcOpName == OpInfo.Name &&
  1392. "unexpected AsmOperands for suboperands");
  1393. ResOperands.push_back(ResOperand::getRenderedOp(SrcOperand + AI, 1));
  1394. }
  1395. }
  1396. }
  1397. void MatchableInfo::buildAliasResultOperands() {
  1398. const CodeGenInstAlias &CGA = *DefRec.get<const CodeGenInstAlias*>();
  1399. const CodeGenInstruction *ResultInst = getResultInst();
  1400. // Loop over all operands of the result instruction, determining how to
  1401. // populate them.
  1402. unsigned AliasOpNo = 0;
  1403. unsigned LastOpNo = CGA.ResultInstOperandIndex.size();
  1404. for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) {
  1405. const CGIOperandList::OperandInfo *OpInfo = &ResultInst->Operands[i];
  1406. // If this is a tied operand, just copy from the previously handled operand.
  1407. int TiedOp = -1;
  1408. if (OpInfo->MINumOperands == 1)
  1409. TiedOp = OpInfo->getTiedRegister();
  1410. if (TiedOp != -1) {
  1411. ResOperands.push_back(ResOperand::getTiedOp(TiedOp));
  1412. continue;
  1413. }
  1414. // Handle all the suboperands for this operand.
  1415. const std::string &OpName = OpInfo->Name;
  1416. for ( ; AliasOpNo < LastOpNo &&
  1417. CGA.ResultInstOperandIndex[AliasOpNo].first == i; ++AliasOpNo) {
  1418. int SubIdx = CGA.ResultInstOperandIndex[AliasOpNo].second;
  1419. // Find out what operand from the asmparser that this MCInst operand
  1420. // comes from.
  1421. switch (CGA.ResultOperands[AliasOpNo].Kind) {
  1422. case CodeGenInstAlias::ResultOperand::K_Record: {
  1423. StringRef Name = CGA.ResultOperands[AliasOpNo].getName();
  1424. int SrcOperand = findAsmOperand(Name, SubIdx);
  1425. if (SrcOperand == -1)
  1426. PrintFatalError(TheDef->getLoc(), "Instruction '" +
  1427. TheDef->getName() + "' has operand '" + OpName +
  1428. "' that doesn't appear in asm string!");
  1429. unsigned NumOperands = (SubIdx == -1 ? OpInfo->MINumOperands : 1);
  1430. ResOperands.push_back(ResOperand::getRenderedOp(SrcOperand,
  1431. NumOperands));
  1432. break;
  1433. }
  1434. case CodeGenInstAlias::ResultOperand::K_Imm: {
  1435. int64_t ImmVal = CGA.ResultOperands[AliasOpNo].getImm();
  1436. ResOperands.push_back(ResOperand::getImmOp(ImmVal));
  1437. break;
  1438. }
  1439. case CodeGenInstAlias::ResultOperand::K_Reg: {
  1440. Record *Reg = CGA.ResultOperands[AliasOpNo].getRegister();
  1441. ResOperands.push_back(ResOperand::getRegOp(Reg));
  1442. break;
  1443. }
  1444. }
  1445. }
  1446. }
  1447. }
  1448. static unsigned getConverterOperandID(const std::string &Name,
  1449. SetVector<std::string> &Table,
  1450. bool &IsNew) {
  1451. IsNew = Table.insert(Name);
  1452. unsigned ID = IsNew ? Table.size() - 1 :
  1453. std::find(Table.begin(), Table.end(), Name) - Table.begin();
  1454. assert(ID < Table.size());
  1455. return ID;
  1456. }
  1457. static void emitConvertFuncs(CodeGenTarget &Target, StringRef ClassName,
  1458. std::vector<std::unique_ptr<MatchableInfo>> &Infos,
  1459. raw_ostream &OS) {
  1460. SetVector<std::string> OperandConversionKinds;
  1461. SetVector<std::string> InstructionConversionKinds;
  1462. std::vector<std::vector<uint8_t> > ConversionTable;
  1463. size_t MaxRowLength = 2; // minimum is custom converter plus terminator.
  1464. // TargetOperandClass - This is the target's operand class, like X86Operand.
  1465. std::string TargetOperandClass = Target.getName() + "Operand";
  1466. // Write the convert function to a separate stream, so we can drop it after
  1467. // the enum. We'll build up the conversion handlers for the individual
  1468. // operand types opportunistically as we encounter them.
  1469. std::string ConvertFnBody;
  1470. raw_string_ostream CvtOS(ConvertFnBody);
  1471. // Start the unified conversion function.
  1472. CvtOS << "void " << Target.getName() << ClassName << "::\n"
  1473. << "convertToMCInst(unsigned Kind, MCInst &Inst, "
  1474. << "unsigned Opcode,\n"
  1475. << " const OperandVector"
  1476. << " &Operands) {\n"
  1477. << " assert(Kind < CVT_NUM_SIGNATURES && \"Invalid signature!\");\n"
  1478. << " const uint8_t *Converter = ConversionTable[Kind];\n"
  1479. << " Inst.setOpcode(Opcode);\n"
  1480. << " for (const uint8_t *p = Converter; *p; p+= 2) {\n"
  1481. << " switch (*p) {\n"
  1482. << " default: llvm_unreachable(\"invalid conversion entry!\");\n"
  1483. << " case CVT_Reg:\n"
  1484. << " static_cast<" << TargetOperandClass
  1485. << "&>(*Operands[*(p + 1)]).addRegOperands(Inst, 1);\n"
  1486. << " break;\n"
  1487. << " case CVT_Tied:\n"
  1488. << " Inst.addOperand(Inst.getOperand(*(p + 1)));\n"
  1489. << " break;\n";
  1490. std::string OperandFnBody;
  1491. raw_string_ostream OpOS(OperandFnBody);
  1492. // Start the operand number lookup function.
  1493. OpOS << "void " << Target.getName() << ClassName << "::\n"
  1494. << "convertToMapAndConstraints(unsigned Kind,\n";
  1495. OpOS.indent(27);
  1496. OpOS << "const OperandVector &Operands) {\n"
  1497. << " assert(Kind < CVT_NUM_SIGNATURES && \"Invalid signature!\");\n"
  1498. << " unsigned NumMCOperands = 0;\n"
  1499. << " const uint8_t *Converter = ConversionTable[Kind];\n"
  1500. << " for (const uint8_t *p = Converter; *p; p+= 2) {\n"
  1501. << " switch (*p) {\n"
  1502. << " default: llvm_unreachable(\"invalid conversion entry!\");\n"
  1503. << " case CVT_Reg:\n"
  1504. << " Operands[*(p + 1)]->setMCOperandNum(NumMCOperands);\n"
  1505. << " Operands[*(p + 1)]->setConstraint(\"r\");\n"
  1506. << " ++NumMCOperands;\n"
  1507. << " break;\n"
  1508. << " case CVT_Tied:\n"
  1509. << " ++NumMCOperands;\n"
  1510. << " break;\n";
  1511. // Pre-populate the operand conversion kinds with the standard always
  1512. // available entries.
  1513. OperandConversionKinds.insert("CVT_Done");
  1514. OperandConversionKinds.insert("CVT_Reg");
  1515. OperandConversionKinds.insert("CVT_Tied");
  1516. enum { CVT_Done, CVT_Reg, CVT_Tied };
  1517. for (auto &II : Infos) {
  1518. // Check if we have a custom match function.
  1519. std::string AsmMatchConverter =
  1520. II->getResultInst()->TheDef->getValueAsString("AsmMatchConverter");
  1521. if (!AsmMatchConverter.empty() && II->UseInstAsmMatchConverter) {
  1522. std::string Signature = "ConvertCustom_" + AsmMatchConverter;
  1523. II->ConversionFnKind = Signature;
  1524. // Check if we have already generated this signature.
  1525. if (!InstructionConversionKinds.insert(Signature))
  1526. continue;
  1527. // Remember this converter for the kind enum.
  1528. unsigned KindID = OperandConversionKinds.size();
  1529. OperandConversionKinds.insert("CVT_" +
  1530. getEnumNameForToken(AsmMatchConverter));
  1531. // Add the converter row for this instruction.
  1532. ConversionTable.emplace_back();
  1533. ConversionTable.back().push_back(KindID);
  1534. ConversionTable.back().push_back(CVT_Done);
  1535. // Add the handler to the conversion driver function.
  1536. CvtOS << " case CVT_"
  1537. << getEnumNameForToken(AsmMatchConverter) << ":\n"
  1538. << " " << AsmMatchConverter << "(Inst, Operands);\n"
  1539. << " break;\n";
  1540. // FIXME: Handle the operand number lookup for custom match functions.
  1541. continue;
  1542. }
  1543. // Build the conversion function signature.
  1544. std::string Signature = "Convert";
  1545. std::vector<uint8_t> ConversionRow;
  1546. // Compute the convert enum and the case body.
  1547. MaxRowLength = std::max(MaxRowLength, II->ResOperands.size()*2 + 1 );
  1548. for (unsigned i = 0, e = II->ResOperands.size(); i != e; ++i) {
  1549. const MatchableInfo::ResOperand &OpInfo = II->ResOperands[i];
  1550. // Generate code to populate each result operand.
  1551. switch (OpInfo.Kind) {
  1552. case MatchableInfo::ResOperand::RenderAsmOperand: {
  1553. // This comes from something we parsed.
  1554. const MatchableInfo::AsmOperand &Op =
  1555. II->AsmOperands[OpInfo.AsmOperandNum];
  1556. // Registers are always converted the same, don't duplicate the
  1557. // conversion function based on them.
  1558. Signature += "__";
  1559. std::string Class;
  1560. Class = Op.Class->isRegisterClass() ? "Reg" : Op.Class->ClassName;
  1561. Signature += Class;
  1562. Signature += utostr(OpInfo.MINumOperands);
  1563. Signature += "_" + itostr(OpInfo.AsmOperandNum);
  1564. // Add the conversion kind, if necessary, and get the associated ID
  1565. // the index of its entry in the vector).
  1566. std::string Name = "CVT_" + (Op.Class->isRegisterClass() ? "Reg" :
  1567. Op.Class->RenderMethod);
  1568. Name = getEnumNameForToken(Name);
  1569. bool IsNewConverter = false;
  1570. unsigned ID = getConverterOperandID(Name, OperandConversionKinds,
  1571. IsNewConverter);
  1572. // Add the operand entry to the instruction kind conversion row.
  1573. ConversionRow.push_back(ID);
  1574. ConversionRow.push_back(OpInfo.AsmOperandNum + 1);
  1575. if (!IsNewConverter)
  1576. break;
  1577. // This is a new operand kind. Add a handler for it to the
  1578. // converter driver.
  1579. CvtOS << " case " << Name << ":\n"
  1580. << " static_cast<" << TargetOperandClass
  1581. << "&>(*Operands[*(p + 1)])." << Op.Class->RenderMethod
  1582. << "(Inst, " << OpInfo.MINumOperands << ");\n"
  1583. << " break;\n";
  1584. // Add a handler for the operand number lookup.
  1585. OpOS << " case " << Name << ":\n"
  1586. << " Operands[*(p + 1)]->setMCOperandNum(NumMCOperands);\n";
  1587. if (Op.Class->isRegisterClass())
  1588. OpOS << " Operands[*(p + 1)]->setConstraint(\"r\");\n";
  1589. else
  1590. OpOS << " Operands[*(p + 1)]->setConstraint(\"m\");\n";
  1591. OpOS << " NumMCOperands += " << OpInfo.MINumOperands << ";\n"
  1592. << " break;\n";
  1593. break;
  1594. }
  1595. case MatchableInfo::ResOperand::TiedOperand: {
  1596. // If this operand is tied to a previous one, just copy the MCInst
  1597. // operand from the earlier one.We can only tie single MCOperand values.
  1598. assert(OpInfo.MINumOperands == 1 && "Not a singular MCOperand");
  1599. unsigned TiedOp = OpInfo.TiedOperandNum;
  1600. assert(i > TiedOp && "Tied operand precedes its target!");
  1601. Signature += "__Tie" + utostr(TiedOp);
  1602. ConversionRow.push_back(CVT_Tied);
  1603. ConversionRow.push_back(TiedOp);
  1604. break;
  1605. }
  1606. case MatchableInfo::ResOperand::ImmOperand: {
  1607. int64_t Val = OpInfo.ImmVal;
  1608. std::string Ty = "imm_" + itostr(Val);
  1609. Ty = getEnumNameForToken(Ty);
  1610. Signature += "__" + Ty;
  1611. std::string Name = "CVT_" + Ty;
  1612. bool IsNewConverter = false;
  1613. unsigned ID = getConverterOperandID(Name, OperandConversionKinds,
  1614. IsNewConverter);
  1615. // Add the operand entry to the instruction kind conversion row.
  1616. ConversionRow.push_back(ID);
  1617. ConversionRow.push_back(0);
  1618. if (!IsNewConverter)
  1619. break;
  1620. CvtOS << " case " << Name << ":\n"
  1621. << " Inst.addOperand(MCOperand::createImm(" << Val << "));\n"
  1622. << " break;\n";
  1623. OpOS << " case " << Name << ":\n"
  1624. << " Operands[*(p + 1)]->setMCOperandNum(NumMCOperands);\n"
  1625. << " Operands[*(p + 1)]->setConstraint(\"\");\n"
  1626. << " ++NumMCOperands;\n"
  1627. << " break;\n";
  1628. break;
  1629. }
  1630. case MatchableInfo::ResOperand::RegOperand: {
  1631. std::string Reg, Name;
  1632. if (!OpInfo.Register) {
  1633. Name = "reg0";
  1634. Reg = "0";
  1635. } else {
  1636. Reg = getQualifiedName(OpInfo.Register);
  1637. Name = "reg" + OpInfo.Register->getName();
  1638. }
  1639. Signature += "__" + Name;
  1640. Name = "CVT_" + Name;
  1641. bool IsNewConverter = false;
  1642. unsigned ID = getConverterOperandID(Name, OperandConversionKinds,
  1643. IsNewConverter);
  1644. // Add the operand entry to the instruction kind conversion row.
  1645. ConversionRow.push_back(ID);
  1646. ConversionRow.push_back(0);
  1647. if (!IsNewConverter)
  1648. break;
  1649. CvtOS << " case " << Name << ":\n"
  1650. << " Inst.addOperand(MCOperand::createReg(" << Reg << "));\n"
  1651. << " break;\n";
  1652. OpOS << " case " << Name << ":\n"
  1653. << " Operands[*(p + 1)]->setMCOperandNum(NumMCOperands);\n"
  1654. << " Operands[*(p + 1)]->setConstraint(\"m\");\n"
  1655. << " ++NumMCOperands;\n"
  1656. << " break;\n";
  1657. }
  1658. }
  1659. }
  1660. // If there were no operands, add to the signature to that effect
  1661. if (Signature == "Convert")
  1662. Signature += "_NoOperands";
  1663. II->ConversionFnKind = Signature;
  1664. // Save the signature. If we already have it, don't add a new row
  1665. // to the table.
  1666. if (!InstructionConversionKinds.insert(Signature))
  1667. continue;
  1668. // Add the row to the table.
  1669. ConversionTable.push_back(ConversionRow);
  1670. }
  1671. // Finish up the converter driver function.
  1672. CvtOS << " }\n }\n}\n\n";
  1673. // Finish up the operand number lookup function.
  1674. OpOS << " }\n }\n}\n\n";
  1675. OS << "namespace {\n";
  1676. // Output the operand conversion kind enum.
  1677. OS << "enum OperatorConversionKind {\n";
  1678. for (unsigned i = 0, e = OperandConversionKinds.size(); i != e; ++i)
  1679. OS << " " << OperandConversionKinds[i] << ",\n";
  1680. OS << " CVT_NUM_CONVERTERS\n";
  1681. OS << "};\n\n";
  1682. // Output the instruction conversion kind enum.
  1683. OS << "enum InstructionConversionKind {\n";
  1684. for (SetVector<std::string>::const_iterator
  1685. i = InstructionConversionKinds.begin(),
  1686. e = InstructionConversionKinds.end(); i != e; ++i)
  1687. OS << " " << *i << ",\n";
  1688. OS << " CVT_NUM_SIGNATURES\n";
  1689. OS << "};\n\n";
  1690. OS << "} // end anonymous namespace\n\n";
  1691. // Output the conversion table.
  1692. OS << "static const uint8_t ConversionTable[CVT_NUM_SIGNATURES]["
  1693. << MaxRowLength << "] = {\n";
  1694. for (unsigned Row = 0, ERow = ConversionTable.size(); Row != ERow; ++Row) {
  1695. assert(ConversionTable[Row].size() % 2 == 0 && "bad conversion row!");
  1696. OS << " // " << InstructionConversionKinds[Row] << "\n";
  1697. OS << " { ";
  1698. for (unsigned i = 0, e = ConversionTable[Row].size(); i != e; i += 2)
  1699. OS << OperandConversionKinds[ConversionTable[Row][i]] << ", "
  1700. << (unsigned)(ConversionTable[Row][i + 1]) << ", ";
  1701. OS << "CVT_Done },\n";
  1702. }
  1703. OS << "};\n\n";
  1704. // Spit out the conversion driver function.
  1705. OS << CvtOS.str();
  1706. // Spit out the operand number lookup function.
  1707. OS << OpOS.str();
  1708. }
  1709. /// emitMatchClassEnumeration - Emit the enumeration for match class kinds.
  1710. static void emitMatchClassEnumeration(CodeGenTarget &Target,
  1711. std::forward_list<ClassInfo> &Infos,
  1712. raw_ostream &OS) {
  1713. OS << "namespace {\n\n";
  1714. OS << "/// MatchClassKind - The kinds of classes which participate in\n"
  1715. << "/// instruction matching.\n";
  1716. OS << "enum MatchClassKind {\n";
  1717. OS << " InvalidMatchClass = 0,\n";
  1718. for (const auto &CI : Infos) {
  1719. OS << " " << CI.Name << ", // ";
  1720. if (CI.Kind == ClassInfo::Token) {
  1721. OS << "'" << CI.ValueName << "'\n";
  1722. } else if (CI.isRegisterClass()) {
  1723. if (!CI.ValueName.empty())
  1724. OS << "register class '" << CI.ValueName << "'\n";
  1725. else
  1726. OS << "derived register class\n";
  1727. } else {
  1728. OS << "user defined class '" << CI.ValueName << "'\n";
  1729. }
  1730. }
  1731. OS << " NumMatchClassKinds\n";
  1732. OS << "};\n\n";
  1733. OS << "}\n\n";
  1734. }
  1735. /// emitValidateOperandClass - Emit the function to validate an operand class.
  1736. static void emitValidateOperandClass(AsmMatcherInfo &Info,
  1737. raw_ostream &OS) {
  1738. OS << "static unsigned validateOperandClass(MCParsedAsmOperand &GOp, "
  1739. << "MatchClassKind Kind) {\n";
  1740. OS << " " << Info.Target.getName() << "Operand &Operand = ("
  1741. << Info.Target.getName() << "Operand&)GOp;\n";
  1742. // The InvalidMatchClass is not to match any operand.
  1743. OS << " if (Kind == InvalidMatchClass)\n";
  1744. OS << " return MCTargetAsmParser::Match_InvalidOperand;\n\n";
  1745. // Check for Token operands first.
  1746. // FIXME: Use a more specific diagnostic type.
  1747. OS << " if (Operand.isToken())\n";
  1748. OS << " return isSubclass(matchTokenString(Operand.getToken()), Kind) ?\n"
  1749. << " MCTargetAsmParser::Match_Success :\n"
  1750. << " MCTargetAsmParser::Match_InvalidOperand;\n\n";
  1751. // Check the user classes. We don't care what order since we're only
  1752. // actually matching against one of them.
  1753. for (const auto &CI : Info.Classes) {
  1754. if (!CI.isUserClass())
  1755. continue;
  1756. OS << " // '" << CI.ClassName << "' class\n";
  1757. OS << " if (Kind == " << CI.Name << ") {\n";
  1758. OS << " if (Operand." << CI.PredicateMethod << "())\n";
  1759. OS << " return MCTargetAsmParser::Match_Success;\n";
  1760. if (!CI.DiagnosticType.empty())
  1761. OS << " return " << Info.Target.getName() << "AsmParser::Match_"
  1762. << CI.DiagnosticType << ";\n";
  1763. OS << " }\n\n";
  1764. }
  1765. // Check for register operands, including sub-classes.
  1766. OS << " if (Operand.isReg()) {\n";
  1767. OS << " MatchClassKind OpKind;\n";
  1768. OS << " switch (Operand.getReg()) {\n";
  1769. OS << " default: OpKind = InvalidMatchClass; break;\n";
  1770. for (const auto &RC : Info.RegisterClasses)
  1771. OS << " case " << Info.Target.getName() << "::"
  1772. << RC.first->getName() << ": OpKind = " << RC.second->Name
  1773. << "; break;\n";
  1774. OS << " }\n";
  1775. OS << " return isSubclass(OpKind, Kind) ? "
  1776. << "MCTargetAsmParser::Match_Success :\n "
  1777. << " MCTargetAsmParser::Match_InvalidOperand;\n }\n\n";
  1778. // Generic fallthrough match failure case for operands that don't have
  1779. // specialized diagnostic types.
  1780. OS << " return MCTargetAsmParser::Match_InvalidOperand;\n";
  1781. OS << "}\n\n";
  1782. }
  1783. /// emitIsSubclass - Emit the subclass predicate function.
  1784. static void emitIsSubclass(CodeGenTarget &Target,
  1785. std::forward_list<ClassInfo> &Infos,
  1786. raw_ostream &OS) {
  1787. OS << "/// isSubclass - Compute whether \\p A is a subclass of \\p B.\n";
  1788. OS << "static bool isSubclass(MatchClassKind A, MatchClassKind B) {\n";
  1789. OS << " if (A == B)\n";
  1790. OS << " return true;\n\n";
  1791. std::string OStr;
  1792. raw_string_ostream SS(OStr);
  1793. unsigned Count = 0;
  1794. SS << " switch (A) {\n";
  1795. SS << " default:\n";
  1796. SS << " return false;\n";
  1797. for (const auto &A : Infos) {
  1798. std::vector<StringRef> SuperClasses;
  1799. for (const auto &B : Infos) {
  1800. if (&A != &B && A.isSubsetOf(B))
  1801. SuperClasses.push_back(B.Name);
  1802. }
  1803. if (SuperClasses.empty())
  1804. continue;
  1805. ++Count;
  1806. SS << "\n case " << A.Name << ":\n";
  1807. if (SuperClasses.size() == 1) {
  1808. SS << " return B == " << SuperClasses.back().str() << ";\n";
  1809. continue;
  1810. }
  1811. if (!SuperClasses.empty()) {
  1812. SS << " switch (B) {\n";
  1813. SS << " default: return false;\n";
  1814. for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
  1815. SS << " case " << SuperClasses[i].str() << ": return true;\n";
  1816. SS << " }\n";
  1817. } else {
  1818. // No case statement to emit
  1819. SS << " return false;\n";
  1820. }
  1821. }
  1822. SS << " }\n";
  1823. // If there were case statements emitted into the string stream, write them
  1824. // to the output stream, otherwise write the default.
  1825. if (Count)
  1826. OS << SS.str();
  1827. else
  1828. OS << " return false;\n";
  1829. OS << "}\n\n";
  1830. }
  1831. /// emitMatchTokenString - Emit the function to match a token string to the
  1832. /// appropriate match class value.
  1833. static void emitMatchTokenString(CodeGenTarget &Target,
  1834. std::forward_list<ClassInfo> &Infos,
  1835. raw_ostream &OS) {
  1836. // Construct the match list.
  1837. std::vector<StringMatcher::StringPair> Matches;
  1838. for (const auto &CI : Infos) {
  1839. if (CI.Kind == ClassInfo::Token)
  1840. Matches.emplace_back(CI.ValueName, "return " + CI.Name + ";");
  1841. }
  1842. OS << "static MatchClassKind matchTokenString(StringRef Name) {\n";
  1843. StringMatcher("Name", Matches, OS).Emit();
  1844. OS << " return InvalidMatchClass;\n";
  1845. OS << "}\n\n";
  1846. }
  1847. /// emitMatchRegisterName - Emit the function to match a string to the target
  1848. /// specific register enum.
  1849. static void emitMatchRegisterName(CodeGenTarget &Target, Record *AsmParser,
  1850. raw_ostream &OS) {
  1851. // Construct the match list.
  1852. std::vector<StringMatcher::StringPair> Matches;
  1853. const auto &Regs = Target.getRegBank().getRegisters();
  1854. for (const CodeGenRegister &Reg : Regs) {
  1855. if (Reg.TheDef->getValueAsString("AsmName").empty())
  1856. continue;
  1857. Matches.emplace_back(Reg.TheDef->getValueAsString("AsmName"),
  1858. "return " + utostr(Reg.EnumValue) + ";");
  1859. }
  1860. OS << "static unsigned MatchRegisterName(StringRef Name) {\n";
  1861. StringMatcher("Name", Matches, OS).Emit();
  1862. OS << " return 0;\n";
  1863. OS << "}\n\n";
  1864. }
  1865. static const char *getMinimalTypeForRange(uint64_t Range) {
  1866. assert(Range <= 0xFFFFFFFFFFFFFFFFULL && "Enum too large");
  1867. if (Range > 0xFFFFFFFFULL)
  1868. return "uint64_t";
  1869. if (Range > 0xFFFF)
  1870. return "uint32_t";
  1871. if (Range > 0xFF)
  1872. return "uint16_t";
  1873. return "uint8_t";
  1874. }
  1875. static const char *getMinimalRequiredFeaturesType(const AsmMatcherInfo &Info) {
  1876. uint64_t MaxIndex = Info.SubtargetFeatures.size();
  1877. if (MaxIndex > 0)
  1878. MaxIndex--;
  1879. return getMinimalTypeForRange(1ULL << MaxIndex);
  1880. }
  1881. /// emitSubtargetFeatureFlagEnumeration - Emit the subtarget feature flag
  1882. /// definitions.
  1883. static void emitSubtargetFeatureFlagEnumeration(AsmMatcherInfo &Info,
  1884. raw_ostream &OS) {
  1885. OS << "// Flags for subtarget features that participate in "
  1886. << "instruction matching.\n";
  1887. OS << "enum SubtargetFeatureFlag : " << getMinimalRequiredFeaturesType(Info)
  1888. << " {\n";
  1889. for (const auto &SF : Info.SubtargetFeatures) {
  1890. const SubtargetFeatureInfo &SFI = SF.second;
  1891. OS << " " << SFI.getEnumName() << " = (1ULL << " << SFI.Index << "),\n";
  1892. }
  1893. OS << " Feature_None = 0\n";
  1894. OS << "};\n\n";
  1895. }
  1896. /// emitOperandDiagnosticTypes - Emit the operand matching diagnostic types.
  1897. static void emitOperandDiagnosticTypes(AsmMatcherInfo &Info, raw_ostream &OS) {
  1898. // Get the set of diagnostic types from all of the operand classes.
  1899. std::set<StringRef> Types;
  1900. for (std::map<Record*, ClassInfo*>::const_iterator
  1901. I = Info.AsmOperandClasses.begin(),
  1902. E = Info.AsmOperandClasses.end(); I != E; ++I) {
  1903. if (!I->second->DiagnosticType.empty())
  1904. Types.insert(I->second->DiagnosticType);
  1905. }
  1906. if (Types.empty()) return;
  1907. // Now emit the enum entries.
  1908. for (std::set<StringRef>::const_iterator I = Types.begin(), E = Types.end();
  1909. I != E; ++I)
  1910. OS << " Match_" << *I << ",\n";
  1911. OS << " END_OPERAND_DIAGNOSTIC_TYPES\n";
  1912. }
  1913. /// emitGetSubtargetFeatureName - Emit the helper function to get the
  1914. /// user-level name for a subtarget feature.
  1915. static void emitGetSubtargetFeatureName(AsmMatcherInfo &Info, raw_ostream &OS) {
  1916. OS << "// User-level names for subtarget features that participate in\n"
  1917. << "// instruction matching.\n"
  1918. << "static const char *getSubtargetFeatureName(uint64_t Val) {\n";
  1919. if (!Info.SubtargetFeatures.empty()) {
  1920. OS << " switch(Val) {\n";
  1921. for (const auto &SF : Info.SubtargetFeatures) {
  1922. const SubtargetFeatureInfo &SFI = SF.second;
  1923. // FIXME: Totally just a placeholder name to get the algorithm working.
  1924. OS << " case " << SFI.getEnumName() << ": return \""
  1925. << SFI.TheDef->getValueAsString("PredicateName") << "\";\n";
  1926. }
  1927. OS << " default: return \"(unknown)\";\n";
  1928. OS << " }\n";
  1929. } else {
  1930. // Nothing to emit, so skip the switch
  1931. OS << " return \"(unknown)\";\n";
  1932. }
  1933. OS << "}\n\n";
  1934. }
  1935. /// emitComputeAvailableFeatures - Emit the function to compute the list of
  1936. /// available features given a subtarget.
  1937. static void emitComputeAvailableFeatures(AsmMatcherInfo &Info,
  1938. raw_ostream &OS) {
  1939. std::string ClassName =
  1940. Info.AsmParser->getValueAsString("AsmParserClassName");
  1941. OS << "uint64_t " << Info.Target.getName() << ClassName << "::\n"
  1942. << "ComputeAvailableFeatures(const FeatureBitset& FB) const {\n";
  1943. OS << " uint64_t Features = 0;\n";
  1944. for (const auto &SF : Info.SubtargetFeatures) {
  1945. const SubtargetFeatureInfo &SFI = SF.second;
  1946. OS << " if (";
  1947. std::string CondStorage =
  1948. SFI.TheDef->getValueAsString("AssemblerCondString");
  1949. StringRef Conds = CondStorage;
  1950. std::pair<StringRef,StringRef> Comma = Conds.split(',');
  1951. bool First = true;
  1952. do {
  1953. if (!First)
  1954. OS << " && ";
  1955. bool Neg = false;
  1956. StringRef Cond = Comma.first;
  1957. if (Cond[0] == '!') {
  1958. Neg = true;
  1959. Cond = Cond.substr(1);
  1960. }
  1961. OS << "(";
  1962. if (Neg)
  1963. OS << "!";
  1964. OS << "FB[" << Info.Target.getName() << "::" << Cond << "])";
  1965. if (Comma.second.empty())
  1966. break;
  1967. First = false;
  1968. Comma = Comma.second.split(',');
  1969. } while (true);
  1970. OS << ")\n";
  1971. OS << " Features |= " << SFI.getEnumName() << ";\n";
  1972. }
  1973. OS << " return Features;\n";
  1974. OS << "}\n\n";
  1975. }
  1976. static std::string GetAliasRequiredFeatures(Record *R,
  1977. const AsmMatcherInfo &Info) {
  1978. std::vector<Record*> ReqFeatures = R->getValueAsListOfDefs("Predicates");
  1979. std::string Result;
  1980. unsigned NumFeatures = 0;
  1981. for (unsigned i = 0, e = ReqFeatures.size(); i != e; ++i) {
  1982. const SubtargetFeatureInfo *F = Info.getSubtargetFeature(ReqFeatures[i]);
  1983. if (!F)
  1984. PrintFatalError(R->getLoc(), "Predicate '" + ReqFeatures[i]->getName() +
  1985. "' is not marked as an AssemblerPredicate!");
  1986. if (NumFeatures)
  1987. Result += '|';
  1988. Result += F->getEnumName();
  1989. ++NumFeatures;
  1990. }
  1991. if (NumFeatures > 1)
  1992. Result = '(' + Result + ')';
  1993. return Result;
  1994. }
  1995. static void emitMnemonicAliasVariant(raw_ostream &OS,const AsmMatcherInfo &Info,
  1996. std::vector<Record*> &Aliases,
  1997. unsigned Indent = 0,
  1998. StringRef AsmParserVariantName = StringRef()){
  1999. // Keep track of all the aliases from a mnemonic. Use an std::map so that the
  2000. // iteration order of the map is stable.
  2001. std::map<std::string, std::vector<Record*> > AliasesFromMnemonic;
  2002. for (unsigned i = 0, e = Aliases.size(); i != e; ++i) {
  2003. Record *R = Aliases[i];
  2004. // FIXME: Allow AssemblerVariantName to be a comma separated list.
  2005. std::string AsmVariantName = R->getValueAsString("AsmVariantName");
  2006. if (AsmVariantName != AsmParserVariantName)
  2007. continue;
  2008. AliasesFromMnemonic[R->getValueAsString("FromMnemonic")].push_back(R);
  2009. }
  2010. if (AliasesFromMnemonic.empty())
  2011. return;
  2012. // Process each alias a "from" mnemonic at a time, building the code executed
  2013. // by the string remapper.
  2014. std::vector<StringMatcher::StringPair> Cases;
  2015. for (std::map<std::string, std::vector<Record*> >::iterator
  2016. I = AliasesFromMnemonic.begin(), E = AliasesFromMnemonic.end();
  2017. I != E; ++I) {
  2018. const std::vector<Record*> &ToVec = I->second;
  2019. // Loop through each alias and emit code that handles each case. If there
  2020. // are two instructions without predicates, emit an error. If there is one,
  2021. // emit it last.
  2022. std::string MatchCode;
  2023. int AliasWithNoPredicate = -1;
  2024. for (unsigned i = 0, e = ToVec.size(); i != e; ++i) {
  2025. Record *R = ToVec[i];
  2026. std::string FeatureMask = GetAliasRequiredFeatures(R, Info);
  2027. // If this unconditionally matches, remember it for later and diagnose
  2028. // duplicates.
  2029. if (FeatureMask.empty()) {
  2030. if (AliasWithNoPredicate != -1) {
  2031. // We can't have two aliases from the same mnemonic with no predicate.
  2032. PrintError(ToVec[AliasWithNoPredicate]->getLoc(),
  2033. "two MnemonicAliases with the same 'from' mnemonic!");
  2034. PrintFatalError(R->getLoc(), "this is the other MnemonicAlias.");
  2035. }
  2036. AliasWithNoPredicate = i;
  2037. continue;
  2038. }
  2039. if (R->getValueAsString("ToMnemonic") == I->first)
  2040. PrintFatalError(R->getLoc(), "MnemonicAlias to the same string");
  2041. if (!MatchCode.empty())
  2042. MatchCode += "else ";
  2043. MatchCode += "if ((Features & " + FeatureMask + ") == "+FeatureMask+")\n";
  2044. MatchCode += " Mnemonic = \"" +R->getValueAsString("ToMnemonic")+"\";\n";
  2045. }
  2046. if (AliasWithNoPredicate != -1) {
  2047. Record *R = ToVec[AliasWithNoPredicate];
  2048. if (!MatchCode.empty())
  2049. MatchCode += "else\n ";
  2050. MatchCode += "Mnemonic = \"" + R->getValueAsString("ToMnemonic")+"\";\n";
  2051. }
  2052. MatchCode += "return;";
  2053. Cases.push_back(std::make_pair(I->first, MatchCode));
  2054. }
  2055. StringMatcher("Mnemonic", Cases, OS).Emit(Indent);
  2056. }
  2057. /// emitMnemonicAliases - If the target has any MnemonicAlias<> definitions,
  2058. /// emit a function for them and return true, otherwise return false.
  2059. static bool emitMnemonicAliases(raw_ostream &OS, const AsmMatcherInfo &Info,
  2060. CodeGenTarget &Target) {
  2061. // Ignore aliases when match-prefix is set.
  2062. if (!MatchPrefix.empty())
  2063. return false;
  2064. std::vector<Record*> Aliases =
  2065. Info.getRecords().getAllDerivedDefinitions("MnemonicAlias");
  2066. if (Aliases.empty()) return false;
  2067. OS << "static void applyMnemonicAliases(StringRef &Mnemonic, "
  2068. "uint64_t Features, unsigned VariantID) {\n";
  2069. OS << " switch (VariantID) {\n";
  2070. unsigned VariantCount = Target.getAsmParserVariantCount();
  2071. for (unsigned VC = 0; VC != VariantCount; ++VC) {
  2072. Record *AsmVariant = Target.getAsmParserVariant(VC);
  2073. int AsmParserVariantNo = AsmVariant->getValueAsInt("Variant");
  2074. std::string AsmParserVariantName = AsmVariant->getValueAsString("Name");
  2075. OS << " case " << AsmParserVariantNo << ":\n";
  2076. emitMnemonicAliasVariant(OS, Info, Aliases, /*Indent=*/2,
  2077. AsmParserVariantName);
  2078. OS << " break;\n";
  2079. }
  2080. OS << " }\n";
  2081. // Emit aliases that apply to all variants.
  2082. emitMnemonicAliasVariant(OS, Info, Aliases);
  2083. OS << "}\n\n";
  2084. return true;
  2085. }
  2086. static void emitCustomOperandParsing(raw_ostream &OS, CodeGenTarget &Target,
  2087. const AsmMatcherInfo &Info, StringRef ClassName,
  2088. StringToOffsetTable &StringTable,
  2089. unsigned MaxMnemonicIndex) {
  2090. unsigned MaxMask = 0;
  2091. for (std::vector<OperandMatchEntry>::const_iterator it =
  2092. Info.OperandMatchInfo.begin(), ie = Info.OperandMatchInfo.end();
  2093. it != ie; ++it) {
  2094. MaxMask |= it->OperandMask;
  2095. }
  2096. // Emit the static custom operand parsing table;
  2097. OS << "namespace {\n";
  2098. OS << " struct OperandMatchEntry {\n";
  2099. OS << " " << getMinimalRequiredFeaturesType(Info)
  2100. << " RequiredFeatures;\n";
  2101. OS << " " << getMinimalTypeForRange(MaxMnemonicIndex)
  2102. << " Mnemonic;\n";
  2103. OS << " " << getMinimalTypeForRange(std::distance(
  2104. Info.Classes.begin(), Info.Classes.end())) << " Class;\n";
  2105. OS << " " << getMinimalTypeForRange(MaxMask)
  2106. << " OperandMask;\n\n";
  2107. OS << " StringRef getMnemonic() const {\n";
  2108. OS << " return StringRef(MnemonicTable + Mnemonic + 1,\n";
  2109. OS << " MnemonicTable[Mnemonic]);\n";
  2110. OS << " }\n";
  2111. OS << " };\n\n";
  2112. OS << " // Predicate for searching for an opcode.\n";
  2113. OS << " struct LessOpcodeOperand {\n";
  2114. OS << " bool operator()(const OperandMatchEntry &LHS, StringRef RHS) {\n";
  2115. OS << " return LHS.getMnemonic() < RHS;\n";
  2116. OS << " }\n";
  2117. OS << " bool operator()(StringRef LHS, const OperandMatchEntry &RHS) {\n";
  2118. OS << " return LHS < RHS.getMnemonic();\n";
  2119. OS << " }\n";
  2120. OS << " bool operator()(const OperandMatchEntry &LHS,";
  2121. OS << " const OperandMatchEntry &RHS) {\n";
  2122. OS << " return LHS.getMnemonic() < RHS.getMnemonic();\n";
  2123. OS << " }\n";
  2124. OS << " };\n";
  2125. OS << "} // end anonymous namespace.\n\n";
  2126. OS << "static const OperandMatchEntry OperandMatchTable["
  2127. << Info.OperandMatchInfo.size() << "] = {\n";
  2128. OS << " /* Operand List Mask, Mnemonic, Operand Class, Features */\n";
  2129. for (std::vector<OperandMatchEntry>::const_iterator it =
  2130. Info.OperandMatchInfo.begin(), ie = Info.OperandMatchInfo.end();
  2131. it != ie; ++it) {
  2132. const OperandMatchEntry &OMI = *it;
  2133. const MatchableInfo &II = *OMI.MI;
  2134. OS << " { ";
  2135. // Write the required features mask.
  2136. if (!II.RequiredFeatures.empty()) {
  2137. for (unsigned i = 0, e = II.RequiredFeatures.size(); i != e; ++i) {
  2138. if (i) OS << "|";
  2139. OS << II.RequiredFeatures[i]->getEnumName();
  2140. }
  2141. } else
  2142. OS << "0";
  2143. // Store a pascal-style length byte in the mnemonic.
  2144. std::string LenMnemonic = char(II.Mnemonic.size()) + II.Mnemonic.str();
  2145. OS << ", " << StringTable.GetOrAddStringOffset(LenMnemonic, false)
  2146. << " /* " << II.Mnemonic << " */, ";
  2147. OS << OMI.CI->Name;
  2148. OS << ", " << OMI.OperandMask;
  2149. OS << " /* ";
  2150. bool printComma = false;
  2151. for (int i = 0, e = 31; i !=e; ++i)
  2152. if (OMI.OperandMask & (1 << i)) {
  2153. if (printComma)
  2154. OS << ", ";
  2155. OS << i;
  2156. printComma = true;
  2157. }
  2158. OS << " */";
  2159. OS << " },\n";
  2160. }
  2161. OS << "};\n\n";
  2162. // Emit the operand class switch to call the correct custom parser for
  2163. // the found operand class.
  2164. OS << Target.getName() << ClassName << "::OperandMatchResultTy "
  2165. << Target.getName() << ClassName << "::\n"
  2166. << "tryCustomParseOperand(OperandVector"
  2167. << " &Operands,\n unsigned MCK) {\n\n"
  2168. << " switch(MCK) {\n";
  2169. for (const auto &CI : Info.Classes) {
  2170. if (CI.ParserMethod.empty())
  2171. continue;
  2172. OS << " case " << CI.Name << ":\n"
  2173. << " return " << CI.ParserMethod << "(Operands);\n";
  2174. }
  2175. OS << " default:\n";
  2176. OS << " return MatchOperand_NoMatch;\n";
  2177. OS << " }\n";
  2178. OS << " return MatchOperand_NoMatch;\n";
  2179. OS << "}\n\n";
  2180. // Emit the static custom operand parser. This code is very similar with
  2181. // the other matcher. Also use MatchResultTy here just in case we go for
  2182. // a better error handling.
  2183. OS << Target.getName() << ClassName << "::OperandMatchResultTy "
  2184. << Target.getName() << ClassName << "::\n"
  2185. << "MatchOperandParserImpl(OperandVector"
  2186. << " &Operands,\n StringRef Mnemonic) {\n";
  2187. // Emit code to get the available features.
  2188. OS << " // Get the current feature set.\n";
  2189. OS << " uint64_t AvailableFeatures = getAvailableFeatures();\n\n";
  2190. OS << " // Get the next operand index.\n";
  2191. OS << " unsigned NextOpNum = Operands.size()-1;\n";
  2192. // Emit code to search the table.
  2193. OS << " // Search the table.\n";
  2194. OS << " std::pair<const OperandMatchEntry*, const OperandMatchEntry*>";
  2195. OS << " MnemonicRange =\n";
  2196. OS << " std::equal_range(OperandMatchTable, OperandMatchTable+"
  2197. << Info.OperandMatchInfo.size() << ", Mnemonic,\n"
  2198. << " LessOpcodeOperand());\n\n";
  2199. OS << " if (MnemonicRange.first == MnemonicRange.second)\n";
  2200. OS << " return MatchOperand_NoMatch;\n\n";
  2201. OS << " for (const OperandMatchEntry *it = MnemonicRange.first,\n"
  2202. << " *ie = MnemonicRange.second; it != ie; ++it) {\n";
  2203. OS << " // equal_range guarantees that instruction mnemonic matches.\n";
  2204. OS << " assert(Mnemonic == it->getMnemonic());\n\n";
  2205. // Emit check that the required features are available.
  2206. OS << " // check if the available features match\n";
  2207. OS << " if ((AvailableFeatures & it->RequiredFeatures) "
  2208. << "!= it->RequiredFeatures) {\n";
  2209. OS << " continue;\n";
  2210. OS << " }\n\n";
  2211. // Emit check to ensure the operand number matches.
  2212. OS << " // check if the operand in question has a custom parser.\n";
  2213. OS << " if (!(it->OperandMask & (1 << NextOpNum)))\n";
  2214. OS << " continue;\n\n";
  2215. // Emit call to the custom parser method
  2216. OS << " // call custom parse method to handle the operand\n";
  2217. OS << " OperandMatchResultTy Result = ";
  2218. OS << "tryCustomParseOperand(Operands, it->Class);\n";
  2219. OS << " if (Result != MatchOperand_NoMatch)\n";
  2220. OS << " return Result;\n";
  2221. OS << " }\n\n";
  2222. OS << " // Okay, we had no match.\n";
  2223. OS << " return MatchOperand_NoMatch;\n";
  2224. OS << "}\n\n";
  2225. }
  2226. void AsmMatcherEmitter::run(raw_ostream &OS) {
  2227. CodeGenTarget Target(Records);
  2228. Record *AsmParser = Target.getAsmParser();
  2229. std::string ClassName = AsmParser->getValueAsString("AsmParserClassName");
  2230. // Compute the information on the instructions to match.
  2231. AsmMatcherInfo Info(AsmParser, Target, Records);
  2232. Info.buildInfo();
  2233. // Sort the instruction table using the partial order on classes. We use
  2234. // stable_sort to ensure that ambiguous instructions are still
  2235. // deterministically ordered.
  2236. std::stable_sort(Info.Matchables.begin(), Info.Matchables.end(),
  2237. [](const std::unique_ptr<MatchableInfo> &a,
  2238. const std::unique_ptr<MatchableInfo> &b){
  2239. return *a < *b;});
  2240. DEBUG_WITH_TYPE("instruction_info", {
  2241. for (const auto &MI : Info.Matchables)
  2242. MI->dump();
  2243. });
  2244. // Check for ambiguous matchables.
  2245. DEBUG_WITH_TYPE("ambiguous_instrs", {
  2246. unsigned NumAmbiguous = 0;
  2247. for (auto I = Info.Matchables.begin(), E = Info.Matchables.end(); I != E;
  2248. ++I) {
  2249. for (auto J = std::next(I); J != E; ++J) {
  2250. const MatchableInfo &A = **I;
  2251. const MatchableInfo &B = **J;
  2252. if (A.couldMatchAmbiguouslyWith(B)) {
  2253. errs() << "warning: ambiguous matchables:\n";
  2254. A.dump();
  2255. errs() << "\nis incomparable with:\n";
  2256. B.dump();
  2257. errs() << "\n\n";
  2258. ++NumAmbiguous;
  2259. }
  2260. }
  2261. }
  2262. if (NumAmbiguous)
  2263. errs() << "warning: " << NumAmbiguous
  2264. << " ambiguous matchables!\n";
  2265. });
  2266. // Compute the information on the custom operand parsing.
  2267. Info.buildOperandMatchInfo();
  2268. // Write the output.
  2269. // Information for the class declaration.
  2270. OS << "\n#ifdef GET_ASSEMBLER_HEADER\n";
  2271. OS << "#undef GET_ASSEMBLER_HEADER\n";
  2272. OS << " // This should be included into the middle of the declaration of\n";
  2273. OS << " // your subclasses implementation of MCTargetAsmParser.\n";
  2274. OS << " uint64_t ComputeAvailableFeatures(const FeatureBitset& FB) const;\n";
  2275. OS << " void convertToMCInst(unsigned Kind, MCInst &Inst, "
  2276. << "unsigned Opcode,\n"
  2277. << " const OperandVector "
  2278. << "&Operands);\n";
  2279. OS << " void convertToMapAndConstraints(unsigned Kind,\n ";
  2280. OS << " const OperandVector &Operands) override;\n";
  2281. OS << " bool mnemonicIsValid(StringRef Mnemonic, unsigned VariantID) override;\n";
  2282. OS << " unsigned MatchInstructionImpl(const OperandVector &Operands,\n"
  2283. << " MCInst &Inst,\n"
  2284. << " uint64_t &ErrorInfo,"
  2285. << " bool matchingInlineAsm,\n"
  2286. << " unsigned VariantID = 0);\n";
  2287. if (!Info.OperandMatchInfo.empty()) {
  2288. OS << "\n enum OperandMatchResultTy {\n";
  2289. OS << " MatchOperand_Success, // operand matched successfully\n";
  2290. OS << " MatchOperand_NoMatch, // operand did not match\n";
  2291. OS << " MatchOperand_ParseFail // operand matched but had errors\n";
  2292. OS << " };\n";
  2293. OS << " OperandMatchResultTy MatchOperandParserImpl(\n";
  2294. OS << " OperandVector &Operands,\n";
  2295. OS << " StringRef Mnemonic);\n";
  2296. OS << " OperandMatchResultTy tryCustomParseOperand(\n";
  2297. OS << " OperandVector &Operands,\n";
  2298. OS << " unsigned MCK);\n\n";
  2299. }
  2300. OS << "#endif // GET_ASSEMBLER_HEADER_INFO\n\n";
  2301. // Emit the operand match diagnostic enum names.
  2302. OS << "\n#ifdef GET_OPERAND_DIAGNOSTIC_TYPES\n";
  2303. OS << "#undef GET_OPERAND_DIAGNOSTIC_TYPES\n\n";
  2304. emitOperandDiagnosticTypes(Info, OS);
  2305. OS << "#endif // GET_OPERAND_DIAGNOSTIC_TYPES\n\n";
  2306. OS << "\n#ifdef GET_REGISTER_MATCHER\n";
  2307. OS << "#undef GET_REGISTER_MATCHER\n\n";
  2308. // Emit the subtarget feature enumeration.
  2309. emitSubtargetFeatureFlagEnumeration(Info, OS);
  2310. // Emit the function to match a register name to number.
  2311. // This should be omitted for Mips target
  2312. if (AsmParser->getValueAsBit("ShouldEmitMatchRegisterName"))
  2313. emitMatchRegisterName(Target, AsmParser, OS);
  2314. OS << "#endif // GET_REGISTER_MATCHER\n\n";
  2315. OS << "\n#ifdef GET_SUBTARGET_FEATURE_NAME\n";
  2316. OS << "#undef GET_SUBTARGET_FEATURE_NAME\n\n";
  2317. // Generate the helper function to get the names for subtarget features.
  2318. emitGetSubtargetFeatureName(Info, OS);
  2319. OS << "#endif // GET_SUBTARGET_FEATURE_NAME\n\n";
  2320. OS << "\n#ifdef GET_MATCHER_IMPLEMENTATION\n";
  2321. OS << "#undef GET_MATCHER_IMPLEMENTATION\n\n";
  2322. // Generate the function that remaps for mnemonic aliases.
  2323. bool HasMnemonicAliases = emitMnemonicAliases(OS, Info, Target);
  2324. // Generate the convertToMCInst function to convert operands into an MCInst.
  2325. // Also, generate the convertToMapAndConstraints function for MS-style inline
  2326. // assembly. The latter doesn't actually generate a MCInst.
  2327. emitConvertFuncs(Target, ClassName, Info.Matchables, OS);
  2328. // Emit the enumeration for classes which participate in matching.
  2329. emitMatchClassEnumeration(Target, Info.Classes, OS);
  2330. // Emit the routine to match token strings to their match class.
  2331. emitMatchTokenString(Target, Info.Classes, OS);
  2332. // Emit the subclass predicate routine.
  2333. emitIsSubclass(Target, Info.Classes, OS);
  2334. // Emit the routine to validate an operand against a match class.
  2335. emitValidateOperandClass(Info, OS);
  2336. // Emit the available features compute function.
  2337. emitComputeAvailableFeatures(Info, OS);
  2338. StringToOffsetTable StringTable;
  2339. size_t MaxNumOperands = 0;
  2340. unsigned MaxMnemonicIndex = 0;
  2341. bool HasDeprecation = false;
  2342. for (const auto &MI : Info.Matchables) {
  2343. MaxNumOperands = std::max(MaxNumOperands, MI->AsmOperands.size());
  2344. HasDeprecation |= MI->HasDeprecation;
  2345. // Store a pascal-style length byte in the mnemonic.
  2346. std::string LenMnemonic = char(MI->Mnemonic.size()) + MI->Mnemonic.str();
  2347. MaxMnemonicIndex = std::max(MaxMnemonicIndex,
  2348. StringTable.GetOrAddStringOffset(LenMnemonic, false));
  2349. }
  2350. OS << "static const char *const MnemonicTable =\n";
  2351. StringTable.EmitString(OS);
  2352. OS << ";\n\n";
  2353. // Emit the static match table; unused classes get initalized to 0 which is
  2354. // guaranteed to be InvalidMatchClass.
  2355. //
  2356. // FIXME: We can reduce the size of this table very easily. First, we change
  2357. // it so that store the kinds in separate bit-fields for each index, which
  2358. // only needs to be the max width used for classes at that index (we also need
  2359. // to reject based on this during classification). If we then make sure to
  2360. // order the match kinds appropriately (putting mnemonics last), then we
  2361. // should only end up using a few bits for each class, especially the ones
  2362. // following the mnemonic.
  2363. OS << "namespace {\n";
  2364. OS << " struct MatchEntry {\n";
  2365. OS << " " << getMinimalTypeForRange(MaxMnemonicIndex)
  2366. << " Mnemonic;\n";
  2367. OS << " uint16_t Opcode;\n";
  2368. OS << " " << getMinimalTypeForRange(Info.Matchables.size())
  2369. << " ConvertFn;\n";
  2370. OS << " " << getMinimalRequiredFeaturesType(Info)
  2371. << " RequiredFeatures;\n";
  2372. OS << " " << getMinimalTypeForRange(
  2373. std::distance(Info.Classes.begin(), Info.Classes.end()))
  2374. << " Classes[" << MaxNumOperands << "];\n";
  2375. OS << " StringRef getMnemonic() const {\n";
  2376. OS << " return StringRef(MnemonicTable + Mnemonic + 1,\n";
  2377. OS << " MnemonicTable[Mnemonic]);\n";
  2378. OS << " }\n";
  2379. OS << " };\n\n";
  2380. OS << " // Predicate for searching for an opcode.\n";
  2381. OS << " struct LessOpcode {\n";
  2382. OS << " bool operator()(const MatchEntry &LHS, StringRef RHS) {\n";
  2383. OS << " return LHS.getMnemonic() < RHS;\n";
  2384. OS << " }\n";
  2385. OS << " bool operator()(StringRef LHS, const MatchEntry &RHS) {\n";
  2386. OS << " return LHS < RHS.getMnemonic();\n";
  2387. OS << " }\n";
  2388. OS << " bool operator()(const MatchEntry &LHS, const MatchEntry &RHS) {\n";
  2389. OS << " return LHS.getMnemonic() < RHS.getMnemonic();\n";
  2390. OS << " }\n";
  2391. OS << " };\n";
  2392. OS << "} // end anonymous namespace.\n\n";
  2393. unsigned VariantCount = Target.getAsmParserVariantCount();
  2394. for (unsigned VC = 0; VC != VariantCount; ++VC) {
  2395. Record *AsmVariant = Target.getAsmParserVariant(VC);
  2396. int AsmVariantNo = AsmVariant->getValueAsInt("Variant");
  2397. OS << "static const MatchEntry MatchTable" << VC << "[] = {\n";
  2398. for (const auto &MI : Info.Matchables) {
  2399. if (MI->AsmVariantID != AsmVariantNo)
  2400. continue;
  2401. // Store a pascal-style length byte in the mnemonic.
  2402. std::string LenMnemonic = char(MI->Mnemonic.size()) + MI->Mnemonic.str();
  2403. OS << " { " << StringTable.GetOrAddStringOffset(LenMnemonic, false)
  2404. << " /* " << MI->Mnemonic << " */, "
  2405. << Target.getName() << "::"
  2406. << MI->getResultInst()->TheDef->getName() << ", "
  2407. << MI->ConversionFnKind << ", ";
  2408. // Write the required features mask.
  2409. if (!MI->RequiredFeatures.empty()) {
  2410. for (unsigned i = 0, e = MI->RequiredFeatures.size(); i != e; ++i) {
  2411. if (i) OS << "|";
  2412. OS << MI->RequiredFeatures[i]->getEnumName();
  2413. }
  2414. } else
  2415. OS << "0";
  2416. OS << ", { ";
  2417. for (unsigned i = 0, e = MI->AsmOperands.size(); i != e; ++i) {
  2418. const MatchableInfo::AsmOperand &Op = MI->AsmOperands[i];
  2419. if (i) OS << ", ";
  2420. OS << Op.Class->Name;
  2421. }
  2422. OS << " }, },\n";
  2423. }
  2424. OS << "};\n\n";
  2425. }
  2426. // A method to determine if a mnemonic is in the list.
  2427. OS << "bool " << Target.getName() << ClassName << "::\n"
  2428. << "mnemonicIsValid(StringRef Mnemonic, unsigned VariantID) {\n";
  2429. OS << " // Find the appropriate table for this asm variant.\n";
  2430. OS << " const MatchEntry *Start, *End;\n";
  2431. OS << " switch (VariantID) {\n";
  2432. OS << " default: llvm_unreachable(\"invalid variant!\");\n";
  2433. for (unsigned VC = 0; VC != VariantCount; ++VC) {
  2434. Record *AsmVariant = Target.getAsmParserVariant(VC);
  2435. int AsmVariantNo = AsmVariant->getValueAsInt("Variant");
  2436. OS << " case " << AsmVariantNo << ": Start = std::begin(MatchTable" << VC
  2437. << "); End = std::end(MatchTable" << VC << "); break;\n";
  2438. }
  2439. OS << " }\n";
  2440. OS << " // Search the table.\n";
  2441. OS << " std::pair<const MatchEntry*, const MatchEntry*> MnemonicRange =\n";
  2442. OS << " std::equal_range(Start, End, Mnemonic, LessOpcode());\n";
  2443. OS << " return MnemonicRange.first != MnemonicRange.second;\n";
  2444. OS << "}\n\n";
  2445. // Finally, build the match function.
  2446. OS << "unsigned " << Target.getName() << ClassName << "::\n"
  2447. << "MatchInstructionImpl(const OperandVector &Operands,\n";
  2448. OS << " MCInst &Inst, uint64_t &ErrorInfo,\n"
  2449. << " bool matchingInlineAsm, unsigned VariantID) {\n";
  2450. OS << " // Eliminate obvious mismatches.\n";
  2451. OS << " if (Operands.size() > " << (MaxNumOperands+1) << ") {\n";
  2452. OS << " ErrorInfo = " << (MaxNumOperands+1) << ";\n";
  2453. OS << " return Match_InvalidOperand;\n";
  2454. OS << " }\n\n";
  2455. // Emit code to get the available features.
  2456. OS << " // Get the current feature set.\n";
  2457. OS << " uint64_t AvailableFeatures = getAvailableFeatures();\n\n";
  2458. OS << " // Get the instruction mnemonic, which is the first token.\n";
  2459. OS << " StringRef Mnemonic = ((" << Target.getName()
  2460. << "Operand&)*Operands[0]).getToken();\n\n";
  2461. if (HasMnemonicAliases) {
  2462. OS << " // Process all MnemonicAliases to remap the mnemonic.\n";
  2463. OS << " applyMnemonicAliases(Mnemonic, AvailableFeatures, VariantID);\n\n";
  2464. }
  2465. // Emit code to compute the class list for this operand vector.
  2466. OS << " // Some state to try to produce better error messages.\n";
  2467. OS << " bool HadMatchOtherThanFeatures = false;\n";
  2468. OS << " bool HadMatchOtherThanPredicate = false;\n";
  2469. OS << " unsigned RetCode = Match_InvalidOperand;\n";
  2470. OS << " uint64_t MissingFeatures = ~0ULL;\n";
  2471. OS << " // Set ErrorInfo to the operand that mismatches if it is\n";
  2472. OS << " // wrong for all instances of the instruction.\n";
  2473. OS << " ErrorInfo = ~0ULL;\n";
  2474. // Emit code to search the table.
  2475. OS << " // Find the appropriate table for this asm variant.\n";
  2476. OS << " const MatchEntry *Start, *End;\n";
  2477. OS << " switch (VariantID) {\n";
  2478. OS << " default: llvm_unreachable(\"invalid variant!\");\n";
  2479. for (unsigned VC = 0; VC != VariantCount; ++VC) {
  2480. Record *AsmVariant = Target.getAsmParserVariant(VC);
  2481. int AsmVariantNo = AsmVariant->getValueAsInt("Variant");
  2482. OS << " case " << AsmVariantNo << ": Start = std::begin(MatchTable" << VC
  2483. << "); End = std::end(MatchTable" << VC << "); break;\n";
  2484. }
  2485. OS << " }\n";
  2486. OS << " // Search the table.\n";
  2487. OS << " std::pair<const MatchEntry*, const MatchEntry*> MnemonicRange =\n";
  2488. OS << " std::equal_range(Start, End, Mnemonic, LessOpcode());\n\n";
  2489. OS << " // Return a more specific error code if no mnemonics match.\n";
  2490. OS << " if (MnemonicRange.first == MnemonicRange.second)\n";
  2491. OS << " return Match_MnemonicFail;\n\n";
  2492. OS << " for (const MatchEntry *it = MnemonicRange.first, "
  2493. << "*ie = MnemonicRange.second;\n";
  2494. OS << " it != ie; ++it) {\n";
  2495. OS << " // equal_range guarantees that instruction mnemonic matches.\n";
  2496. OS << " assert(Mnemonic == it->getMnemonic());\n";
  2497. // Emit check that the subclasses match.
  2498. OS << " bool OperandsValid = true;\n";
  2499. OS << " for (unsigned i = 0; i != " << MaxNumOperands << "; ++i) {\n";
  2500. OS << " if (i + 1 >= Operands.size()) {\n";
  2501. OS << " OperandsValid = (it->Classes[i] == " <<"InvalidMatchClass);\n";
  2502. OS << " if (!OperandsValid) ErrorInfo = i + 1;\n";
  2503. OS << " break;\n";
  2504. OS << " }\n";
  2505. OS << " unsigned Diag = validateOperandClass(*Operands[i+1],\n";
  2506. OS.indent(43);
  2507. OS << "(MatchClassKind)it->Classes[i]);\n";
  2508. OS << " if (Diag == Match_Success)\n";
  2509. OS << " continue;\n";
  2510. OS << " // If the generic handler indicates an invalid operand\n";
  2511. OS << " // failure, check for a special case.\n";
  2512. OS << " if (Diag == Match_InvalidOperand) {\n";
  2513. OS << " Diag = validateTargetOperandClass(*Operands[i+1],\n";
  2514. OS.indent(43);
  2515. OS << "(MatchClassKind)it->Classes[i]);\n";
  2516. OS << " if (Diag == Match_Success)\n";
  2517. OS << " continue;\n";
  2518. OS << " }\n";
  2519. OS << " // If this operand is broken for all of the instances of this\n";
  2520. OS << " // mnemonic, keep track of it so we can report loc info.\n";
  2521. OS << " // If we already had a match that only failed due to a\n";
  2522. OS << " // target predicate, that diagnostic is preferred.\n";
  2523. OS << " if (!HadMatchOtherThanPredicate &&\n";
  2524. OS << " (it == MnemonicRange.first || ErrorInfo <= i+1)) {\n";
  2525. OS << " ErrorInfo = i+1;\n";
  2526. OS << " // InvalidOperand is the default. Prefer specificity.\n";
  2527. OS << " if (Diag != Match_InvalidOperand)\n";
  2528. OS << " RetCode = Diag;\n";
  2529. OS << " }\n";
  2530. OS << " // Otherwise, just reject this instance of the mnemonic.\n";
  2531. OS << " OperandsValid = false;\n";
  2532. OS << " break;\n";
  2533. OS << " }\n\n";
  2534. OS << " if (!OperandsValid) continue;\n";
  2535. // Emit check that the required features are available.
  2536. OS << " if ((AvailableFeatures & it->RequiredFeatures) "
  2537. << "!= it->RequiredFeatures) {\n";
  2538. OS << " HadMatchOtherThanFeatures = true;\n";
  2539. OS << " uint64_t NewMissingFeatures = it->RequiredFeatures & "
  2540. "~AvailableFeatures;\n";
  2541. OS << " if (countPopulation(NewMissingFeatures) <=\n"
  2542. " countPopulation(MissingFeatures))\n";
  2543. OS << " MissingFeatures = NewMissingFeatures;\n";
  2544. OS << " continue;\n";
  2545. OS << " }\n";
  2546. OS << "\n";
  2547. OS << " Inst.clear();\n\n";
  2548. OS << " if (matchingInlineAsm) {\n";
  2549. OS << " Inst.setOpcode(it->Opcode);\n";
  2550. OS << " convertToMapAndConstraints(it->ConvertFn, Operands);\n";
  2551. OS << " return Match_Success;\n";
  2552. OS << " }\n\n";
  2553. OS << " // We have selected a definite instruction, convert the parsed\n"
  2554. << " // operands into the appropriate MCInst.\n";
  2555. OS << " convertToMCInst(it->ConvertFn, Inst, it->Opcode, Operands);\n";
  2556. OS << "\n";
  2557. // Verify the instruction with the target-specific match predicate function.
  2558. OS << " // We have a potential match. Check the target predicate to\n"
  2559. << " // handle any context sensitive constraints.\n"
  2560. << " unsigned MatchResult;\n"
  2561. << " if ((MatchResult = checkTargetMatchPredicate(Inst)) !="
  2562. << " Match_Success) {\n"
  2563. << " Inst.clear();\n"
  2564. << " RetCode = MatchResult;\n"
  2565. << " HadMatchOtherThanPredicate = true;\n"
  2566. << " continue;\n"
  2567. << " }\n\n";
  2568. // Call the post-processing function, if used.
  2569. std::string InsnCleanupFn =
  2570. AsmParser->getValueAsString("AsmParserInstCleanup");
  2571. if (!InsnCleanupFn.empty())
  2572. OS << " " << InsnCleanupFn << "(Inst);\n";
  2573. if (HasDeprecation) {
  2574. OS << " std::string Info;\n";
  2575. OS << " if (MII.get(Inst.getOpcode()).getDeprecatedInfo(Inst, STI, Info)) {\n";
  2576. OS << " SMLoc Loc = ((" << Target.getName()
  2577. << "Operand&)*Operands[0]).getStartLoc();\n";
  2578. OS << " getParser().Warning(Loc, Info, None);\n";
  2579. OS << " }\n";
  2580. }
  2581. OS << " return Match_Success;\n";
  2582. OS << " }\n\n";
  2583. OS << " // Okay, we had no match. Try to return a useful error code.\n";
  2584. OS << " if (HadMatchOtherThanPredicate || !HadMatchOtherThanFeatures)\n";
  2585. OS << " return RetCode;\n\n";
  2586. OS << " // Missing feature matches return which features were missing\n";
  2587. OS << " ErrorInfo = MissingFeatures;\n";
  2588. OS << " return Match_MissingFeature;\n";
  2589. OS << "}\n\n";
  2590. if (!Info.OperandMatchInfo.empty())
  2591. emitCustomOperandParsing(OS, Target, Info, ClassName, StringTable,
  2592. MaxMnemonicIndex);
  2593. OS << "#endif // GET_MATCHER_IMPLEMENTATION\n\n";
  2594. }
  2595. namespace llvm {
  2596. void EmitAsmMatcher(RecordKeeper &RK, raw_ostream &OS) {
  2597. emitSourceFileHeader("Assembly Matcher Source Fragment", OS);
  2598. AsmMatcherEmitter(RK).run(OS);
  2599. }
  2600. } // End llvm namespace