Opcodes.td 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. //===--- Opcodes.td - Opcode defitions for the constexpr VM -----*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Helper file used to generate opcodes, the interpreter and the disassembler.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. //===----------------------------------------------------------------------===//
  13. // Types evaluated by the interpreter.
  14. //===----------------------------------------------------------------------===//
  15. class Type;
  16. def Bool : Type;
  17. def Sint8 : Type;
  18. def Uint8 : Type;
  19. def Sint16 : Type;
  20. def Uint16 : Type;
  21. def Sint32 : Type;
  22. def Uint32 : Type;
  23. def Sint64 : Type;
  24. def Uint64 : Type;
  25. def Ptr : Type;
  26. //===----------------------------------------------------------------------===//
  27. // Types transferred to the interpreter.
  28. //===----------------------------------------------------------------------===//
  29. class ArgType { string Name = ?; }
  30. def ArgSint8 : ArgType { let Name = "int8_t"; }
  31. def ArgUint8 : ArgType { let Name = "uint8_t"; }
  32. def ArgSint16 : ArgType { let Name = "int16_t"; }
  33. def ArgUint16 : ArgType { let Name = "uint16_t"; }
  34. def ArgSint32 : ArgType { let Name = "int32_t"; }
  35. def ArgUint32 : ArgType { let Name = "uint32_t"; }
  36. def ArgSint64 : ArgType { let Name = "int64_t"; }
  37. def ArgUint64 : ArgType { let Name = "uint64_t"; }
  38. def ArgBool : ArgType { let Name = "bool"; }
  39. def ArgFunction : ArgType { let Name = "Function *"; }
  40. def ArgRecord : ArgType { let Name = "Record *"; }
  41. def ArgSema : ArgType { let Name = "const fltSemantics *"; }
  42. def ArgExpr : ArgType { let Name = "const Expr *"; }
  43. def ArgFloatingLiteral : ArgType { let Name = "const FloatingLiteral *"; }
  44. def ArgCXXMethodDecl : ArgType { let Name = "const CXXMethodDecl *"; }
  45. def ArgFunctionDecl : ArgType { let Name = "const FunctionDecl *"; }
  46. def ArgRecordDecl : ArgType { let Name = "const RecordDecl *"; }
  47. def ArgCXXRecordDecl : ArgType { let Name = "const CXXRecordDecl *"; }
  48. def ArgValueDecl : ArgType { let Name = "const ValueDecl *"; }
  49. def ArgRecordField : ArgType { let Name = "const Record::Field *"; }
  50. //===----------------------------------------------------------------------===//
  51. // Classes of types instructions operate on.
  52. //===----------------------------------------------------------------------===//
  53. class TypeClass {
  54. list<Type> Types;
  55. }
  56. def AluTypeClass : TypeClass {
  57. let Types = [Sint8, Uint8, Sint16, Uint16, Sint32,
  58. Uint32, Sint64, Uint64, Bool];
  59. }
  60. def PtrTypeClass : TypeClass {
  61. let Types = [Ptr];
  62. }
  63. def AllTypeClass : TypeClass {
  64. let Types = !listconcat(AluTypeClass.Types, PtrTypeClass.Types);
  65. }
  66. def ComparableTypeClass : TypeClass {
  67. let Types = !listconcat(AluTypeClass.Types, [Ptr]);
  68. }
  69. class SingletonTypeClass<Type Ty> : TypeClass {
  70. let Types = [Ty];
  71. }
  72. //===----------------------------------------------------------------------===//
  73. // Record describing all opcodes.
  74. //===----------------------------------------------------------------------===//
  75. class Opcode {
  76. list<TypeClass> Types = [];
  77. list<ArgType> Args = [];
  78. string Name = "";
  79. bit CanReturn = 0;
  80. bit ChangesPC = 0;
  81. bit HasCustomLink = 0;
  82. bit HasCustomEval = 0;
  83. bit HasGroup = 0;
  84. }
  85. class AluOpcode : Opcode {
  86. let Types = [AluTypeClass];
  87. let HasGroup = 1;
  88. }
  89. //===----------------------------------------------------------------------===//
  90. // Jump opcodes
  91. //===----------------------------------------------------------------------===//
  92. class JumpOpcode : Opcode {
  93. let Args = [ArgSint32];
  94. let ChangesPC = 1;
  95. let HasCustomEval = 1;
  96. }
  97. // [] -> []
  98. def Jmp : JumpOpcode;
  99. // [Bool] -> [], jumps if true.
  100. def Jt : JumpOpcode;
  101. // [Bool] -> [], jumps if false.
  102. def Jf : JumpOpcode;
  103. //===----------------------------------------------------------------------===//
  104. // Returns
  105. //===----------------------------------------------------------------------===//
  106. // [Value] -> []
  107. def Ret : Opcode {
  108. let Types = [AllTypeClass];
  109. let ChangesPC = 1;
  110. let CanReturn = 1;
  111. let HasGroup = 1;
  112. let HasCustomEval = 1;
  113. }
  114. // [] -> []
  115. def RetVoid : Opcode {
  116. let CanReturn = 1;
  117. let ChangesPC = 1;
  118. let HasCustomEval = 1;
  119. }
  120. // [Value] -> []
  121. def RetValue : Opcode {
  122. let CanReturn = 1;
  123. let ChangesPC = 1;
  124. let HasCustomEval = 1;
  125. }
  126. // [] -> EXIT
  127. def NoRet : Opcode {}
  128. //===----------------------------------------------------------------------===//
  129. // Frame management
  130. //===----------------------------------------------------------------------===//
  131. // [] -> []
  132. def Destroy : Opcode {
  133. let Args = [ArgUint32];
  134. let HasCustomEval = 1;
  135. }
  136. //===----------------------------------------------------------------------===//
  137. // Constants
  138. //===----------------------------------------------------------------------===//
  139. class ConstOpcode<Type Ty, ArgType ArgTy> : Opcode {
  140. let Types = [SingletonTypeClass<Ty>];
  141. let Args = [ArgTy];
  142. let Name = "Const";
  143. }
  144. // [] -> [Integer]
  145. def ConstSint8 : ConstOpcode<Sint8, ArgSint8>;
  146. def ConstUint8 : ConstOpcode<Uint8, ArgUint8>;
  147. def ConstSint16 : ConstOpcode<Sint16, ArgSint16>;
  148. def ConstUint16 : ConstOpcode<Uint16, ArgUint16>;
  149. def ConstSint32 : ConstOpcode<Sint32, ArgSint32>;
  150. def ConstUint32 : ConstOpcode<Uint32, ArgUint32>;
  151. def ConstSint64 : ConstOpcode<Sint64, ArgSint64>;
  152. def ConstUint64 : ConstOpcode<Uint64, ArgUint64>;
  153. def ConstBool : ConstOpcode<Bool, ArgBool>;
  154. // [] -> [Integer]
  155. def Zero : Opcode {
  156. let Types = [AluTypeClass];
  157. }
  158. // [] -> [Pointer]
  159. def Null : Opcode {
  160. let Types = [PtrTypeClass];
  161. }
  162. //===----------------------------------------------------------------------===//
  163. // Pointer generation
  164. //===----------------------------------------------------------------------===//
  165. // [] -> [Pointer]
  166. def GetPtrLocal : Opcode {
  167. // Offset of local.
  168. let Args = [ArgUint32];
  169. bit HasCustomEval = 1;
  170. }
  171. // [] -> [Pointer]
  172. def GetPtrParam : Opcode {
  173. // Offset of parameter.
  174. let Args = [ArgUint32];
  175. }
  176. // [] -> [Pointer]
  177. def GetPtrGlobal : Opcode {
  178. // Index of global.
  179. let Args = [ArgUint32];
  180. }
  181. // [Pointer] -> [Pointer]
  182. def GetPtrField : Opcode {
  183. // Offset of field.
  184. let Args = [ArgUint32];
  185. }
  186. // [Pointer] -> [Pointer]
  187. def GetPtrActiveField : Opcode {
  188. // Offset of field.
  189. let Args = [ArgUint32];
  190. }
  191. // [] -> [Pointer]
  192. def GetPtrActiveThisField : Opcode {
  193. // Offset of field.
  194. let Args = [ArgUint32];
  195. }
  196. // [] -> [Pointer]
  197. def GetPtrThisField : Opcode {
  198. // Offset of field.
  199. let Args = [ArgUint32];
  200. }
  201. // [Pointer] -> [Pointer]
  202. def GetPtrBase : Opcode {
  203. // Offset of field, which is a base.
  204. let Args = [ArgUint32];
  205. }
  206. // [Pointer] -> [Pointer]
  207. def GetPtrVirtBase : Opcode {
  208. // RecordDecl of base class.
  209. let Args = [ArgRecordDecl];
  210. }
  211. // [] -> [Pointer]
  212. def GetPtrThisBase : Opcode {
  213. // Offset of field, which is a base.
  214. let Args = [ArgUint32];
  215. }
  216. // [] -> [Pointer]
  217. def GetPtrThisVirtBase : Opcode {
  218. // RecordDecl of base class.
  219. let Args = [ArgRecordDecl];
  220. }
  221. // [] -> [Pointer]
  222. def This : Opcode;
  223. // [Pointer] -> [Pointer]
  224. def NarrowPtr : Opcode;
  225. // [Pointer] -> [Pointer]
  226. def ExpandPtr : Opcode;
  227. //===----------------------------------------------------------------------===//
  228. // Direct field accessors
  229. //===----------------------------------------------------------------------===//
  230. class AccessOpcode : Opcode {
  231. let Types = [AllTypeClass];
  232. let Args = [ArgUint32];
  233. let HasGroup = 1;
  234. }
  235. class BitFieldOpcode : Opcode {
  236. let Types = [AluTypeClass];
  237. let Args = [ArgRecordField];
  238. let HasGroup = 1;
  239. }
  240. // [] -> [Pointer]
  241. def GetLocal : AccessOpcode { let HasCustomEval = 1; }
  242. // [] -> [Pointer]
  243. def SetLocal : AccessOpcode { let HasCustomEval = 1; }
  244. // [] -> [Value]
  245. def GetGlobal : AccessOpcode;
  246. // [Value] -> []
  247. def InitGlobal : AccessOpcode;
  248. // [Value] -> []
  249. def SetGlobal : AccessOpcode;
  250. // [] -> [Value]
  251. def GetParam : AccessOpcode;
  252. // [Value] -> []
  253. def SetParam : AccessOpcode;
  254. // [Pointer] -> [Pointer, Value]
  255. def GetField : AccessOpcode;
  256. // [Pointer] -> [Value]
  257. def GetFieldPop : AccessOpcode;
  258. // [] -> [Value]
  259. def GetThisField : AccessOpcode;
  260. // [Pointer, Value] -> [Pointer]
  261. def SetField : AccessOpcode;
  262. // [Value] -> []
  263. def SetThisField : AccessOpcode;
  264. // [Value] -> []
  265. def InitThisField : AccessOpcode;
  266. // [Value] -> []
  267. def InitThisFieldActive : AccessOpcode;
  268. // [Value] -> []
  269. def InitThisBitField : BitFieldOpcode;
  270. // [Pointer, Value] -> []
  271. def InitField : AccessOpcode;
  272. // [Pointer, Value] -> []
  273. def InitBitField : BitFieldOpcode;
  274. // [Pointer, Value] -> []
  275. def InitFieldActive : AccessOpcode;
  276. //===----------------------------------------------------------------------===//
  277. // Pointer access
  278. //===----------------------------------------------------------------------===//
  279. class LoadOpcode : Opcode {
  280. let Types = [AllTypeClass];
  281. let HasGroup = 1;
  282. }
  283. // [Pointer] -> [Pointer, Value]
  284. def Load : LoadOpcode {}
  285. // [Pointer] -> [Value]
  286. def LoadPop : LoadOpcode {}
  287. class StoreOpcode : Opcode {
  288. let Types = [AllTypeClass];
  289. let HasGroup = 1;
  290. }
  291. class StoreBitFieldOpcode : Opcode {
  292. let Types = [AluTypeClass];
  293. let HasGroup = 1;
  294. }
  295. // [Pointer, Value] -> [Pointer]
  296. def Store : StoreOpcode {}
  297. // [Pointer, Value] -> []
  298. def StorePop : StoreOpcode {}
  299. // [Pointer, Value] -> [Pointer]
  300. def StoreBitField : StoreBitFieldOpcode {}
  301. // [Pointer, Value] -> []
  302. def StoreBitFieldPop : StoreBitFieldOpcode {}
  303. // [Pointer, Value] -> []
  304. def InitPop : StoreOpcode {}
  305. // [Pointer, Value] -> [Pointer]
  306. def InitElem : Opcode {
  307. let Types = [AllTypeClass];
  308. let Args = [ArgUint32];
  309. let HasGroup = 1;
  310. }
  311. // [Pointer, Value] -> []
  312. def InitElemPop : Opcode {
  313. let Types = [AllTypeClass];
  314. let Args = [ArgUint32];
  315. let HasGroup = 1;
  316. }
  317. //===----------------------------------------------------------------------===//
  318. // Pointer arithmetic.
  319. //===----------------------------------------------------------------------===//
  320. // [Pointer, Integral] -> [Pointer]
  321. def AddOffset : AluOpcode;
  322. // [Pointer, Integral] -> [Pointer]
  323. def SubOffset : AluOpcode;
  324. //===----------------------------------------------------------------------===//
  325. // Binary operators.
  326. //===----------------------------------------------------------------------===//
  327. // [Real, Real] -> [Real]
  328. def Sub : AluOpcode;
  329. def Add : AluOpcode;
  330. def Mul : AluOpcode;
  331. //===----------------------------------------------------------------------===//
  332. // Comparison opcodes.
  333. //===----------------------------------------------------------------------===//
  334. class EqualityOpcode : Opcode {
  335. let Types = [AllTypeClass];
  336. let HasGroup = 1;
  337. }
  338. def EQ : EqualityOpcode;
  339. def NE : EqualityOpcode;
  340. class ComparisonOpcode : Opcode {
  341. let Types = [ComparableTypeClass];
  342. let HasGroup = 1;
  343. }
  344. def LT : ComparisonOpcode;
  345. def LE : ComparisonOpcode;
  346. def GT : ComparisonOpcode;
  347. def GE : ComparisonOpcode;
  348. //===----------------------------------------------------------------------===//
  349. // Stack management.
  350. //===----------------------------------------------------------------------===//
  351. // [Value] -> []
  352. def Pop : Opcode {
  353. let Types = [AllTypeClass];
  354. let HasGroup = 1;
  355. }
  356. // [Value] -> [Value, Value]
  357. def Dup : Opcode {
  358. let Types = [AllTypeClass];
  359. let HasGroup = 1;
  360. }