opcode_310graal.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. # (C) 2024 by Rocky Bernstein
  2. """
  3. Python Graal 3.10 bytecode opcodes
  4. See com.oracle.graal.python/src/com/oracle/graal/python/compiler/OpCodes.java
  5. """
  6. from xdis.opcodes.base import init_opdata
  7. opc = locals()
  8. init_opdata(opc, None, None)
  9. def def_graal_op(
  10. loc: dict,
  11. op_name: str,
  12. opcode: int,
  13. pop: int = -2,
  14. push: int = -2,
  15. unknown: int = 0,
  16. fallthrough: bool = True,
  17. ) -> None:
  18. loc["opname"][opcode] = op_name
  19. loc["opmap"][op_name] = opcode
  20. loc["oppush"][opcode] = push
  21. loc["oppop"][opcode] = pop
  22. if not fallthrough:
  23. loc["nofollow"].append(opcode)
  24. # Instruction opcodes for compiled code
  25. # Blank lines correspond to available opcodes
  26. # If the POP field is -1 and the opcode is a var args operation
  27. # then the operand holds the size.
  28. #
  29. # If the POP field is negative and the opcode is a nargs operation
  30. # then pop the operand amount plus the negative of the POP amount.
  31. # Pop a single item from the stack.
  32. def_graal_op(opc, "POP_TOP", 0, 0, 1, 0)
  33. # Exchange two top stack items.
  34. def_graal_op(opc, "ROT_TWO", 1, 0, 2, 2)
  35. # Exchange three top stack items. [a, b, c] (a is top) becomes [b, c, a]
  36. def_graal_op(opc, "ROT_THREE", 2, 0, 3, 3)
  37. # Exchange N top stack items. [a, b, c, ..., N] (a is top) becomes [b, c, ..., N, a].
  38. def_graal_op(
  39. opc, "ROT_N", 3, -1, -1
  40. ) # (oparg, followingArgs, withJump) -> oparg, (oparg, followingArgs, withJump) -> oparg)
  41. # Duplicates the top stack item.
  42. def_graal_op(opc, "DUP_TOP", 4, 0, 1, 2)
  43. # Does nothing. Might still be useful to maintain a line number.
  44. def_graal_op(opc, "NOP", 5, 0, 0, 0)
  45. # Performs a unary operation specified by the immediate operand. It
  46. # has to be the ordinal of one of {@link UnaryOps} constants.
  47. # Pops: operand
  48. # Pushes: result
  49. def_graal_op(opc, "UNARY_OP", 6, 1, 1, 1)
  50. # Performs a binary operation specified by the immediate operand. It has to be the ordinal of
  51. # one of {@link BinaryOps} constants.
  52. # Pops: right operand, then left operand
  53. # Pushes: result
  54. def_graal_op(opc, "BINARY_OP", 7, 1, 2, 1)
  55. # Performs subscript get operation - {@code a[b]}.
  56. # Pops: {@code b}, then {@code a}
  57. # Pushes: result
  58. def_graal_op(opc, "BINARY_SUBSCR", 8, 0, 2, 1)
  59. # Performs subscript set operation - {@code a[b] = c}.
  60. # Pops: {@code b}, then {@code a}, then {@code c}
  61. def_graal_op(opc, "STORE_SUBSCR", 9, 0, 3, 0)
  62. # Performs subscript delete operation - {@code del a[b]}.
  63. # Pops: {@code b}, then {@code a}
  64. #
  65. def_graal_op(opc, "DELETE_SUBSCR", 10, 0, 2, 0)
  66. # Gets an iterator of an object.
  67. # Pops: object
  68. # Pushes: iterator
  69. def_graal_op(opc, "GET_ITER", 11, 0, 1, 1)
  70. # Gets an iterator of an object, does nothing for a generator iterator or a coroutine.
  71. # Pops: object
  72. # Pushes: iterator
  73. def_graal_op(opc, "GET_YIELD_FROM_ITER", 12, 0, 1, 1)
  74. # Gets an awaitable of an object.
  75. # Pops: object
  76. # Pushes: awaitable
  77. def_graal_op(opc, "GET_AWAITABLE", 13, 0, 1, 1)
  78. # Gets the async iterator of an object - error if a coroutine is returned.
  79. # Pops: object
  80. # Pushes: async iterator
  81. def_graal_op(opc, "GET_AITER", 14, 0, 1, 1)
  82. # Get the awaitable that will return the next element of an async iterator.
  83. # Pops: object
  84. # Pushes: awaitable
  85. def_graal_op(opc, "GET_ANEXT", 15, 0, 1, 1)
  86. # Pushes: {@code __build_class__} builtin
  87. def_graal_op(opc, "LOAD_BUILD_CLASS", 16, 0, 0, 1)
  88. # Pushes: {@code AssertionError} builtin exception type
  89. def_graal_op(opc, "LOAD_ASSERTION_ERROR", 17, 0, 0, 1)
  90. # Returns the value to the caller. In generators, performs generator return.
  91. # Pops: return value
  92. def_graal_op(opc, "RETURN_VALUE", 18, 0, 1, 0)
  93. #
  94. # Reads a name from locals dict, globals or builtins determined by the
  95. # immediate operand which indexes the names array ({@code co_names}).
  96. # Pushes: read object
  97. def_graal_op(opc, "LOAD_NAME", 19, 1, 0, 1)
  98. opc["nullaryloadop"].add(19)
  99. # Writes the stack top into a name in locals dict or globals
  100. # determined by the immediate operand which indexes the names array
  101. # ({@code co_names}).
  102. # Pops: object to be written
  103. def_graal_op(opc, "STORE_NAME", 20, 1, 1, 0)
  104. # Deletes the name in locals dict or globals determined by the
  105. # immediate operand which indexes the names array ({@code co_names}).
  106. def_graal_op(opc, "DELETE_NAME", 21, 1, 0, 0)
  107. # Reads an attribute - {@code a.b}. {@code b} is determined by the immediate operand which
  108. # indexes the names array ({@code co_names}).
  109. # Pops: {@code a}
  110. # Pushes: read attribute
  111. def_graal_op(opc, "LOAD_ATTR", 22, 1, 1, 1)
  112. #
  113. # Reads method on an object. The method name is determined by the
  114. # first immediate operand which indexes the names array ({@code
  115. # co_names}).
  116. # Pushes: read method
  117. def_graal_op(opc, "LOAD_METHOD", 23, 1, 1, 2)
  118. # Writes an attribute - {@code a.b = c}. {@code b} is determined by
  119. # the immediate operand which indexes the names array ({@code
  120. # co_names}).
  121. # Pops: {@code c}, then {@code a}
  122. def_graal_op(opc, "STORE_ATTR", 24, 1, 2, 0)
  123. # Deletes an attribute - {@code del a.b}. {@code b} is determined by
  124. # the immediate operand which indexes the names array ({@code
  125. # co_names}).
  126. # Pops: {@code a}
  127. def_graal_op(opc, "DELETE_ATTR", 25, 1, 1, 0)
  128. # Reads a global variable. The name is determined by the immediate
  129. # operand which indexes the names array ({@code co_names}).
  130. # Pushes: read object
  131. def_graal_op(opc, "LOAD_GLOBAL", 26, 1, 0, 1)
  132. opc["nullaryloadop"].add(26)
  133. # Writes a global variable. The name is determined by the immediate
  134. # operand which indexes the names array ({@code co_names}).
  135. # Pops: value to be written
  136. def_graal_op(opc, "STORE_GLOBAL", 27, 1, 1, 0)
  137. # Deletes a global variable. The name is determined by the immediate operand which indexes the
  138. # names array ({@code co_names}).
  139. def_graal_op(opc, "DELETE_GLOBAL", 28, 1, 0, 0)
  140. # Reads a constant object from constants array ({@code co_consts}). Performs no conversion.
  141. # Pushes: read constant
  142. def_graal_op(opc, "LOAD_CONST", 29, 1, 0, 1)
  143. opc["nullaryloadop"].add(29)
  144. # Reads a local variable determined by the immediate operand which indexes a stack slot and a
  145. # variable name in varnames array ({@code co_varnames}).
  146. # Pushes: read value
  147. def_graal_op(opc, "LOAD_FAST", 30, 1, 0, 1)
  148. opc["nullaryloadop"].add(30)
  149. # Writes a local variable determined by the immediate operand which indexes a stack slot and a
  150. # variable name in varnames array ({@code co_varnames}).
  151. # Pops: value to be written
  152. def_graal_op(opc, "STORE_FAST", 31, 1, 1, 0)
  153. # Deletes a local variable determined by the immediate operand which indexes a stack slot and a
  154. # variable name in varnames array ({@code co_varnames}).
  155. def_graal_op(opc, "DELETE_FAST", 32, 1, 0, 0)
  156. # Reads a local cell variable determined by the immediate operand
  157. # which indexes a stack slot after celloffset and a variable name in
  158. # cellvars or freevars array ({@code co_cellvars}, {@code
  159. # co_freevars}).
  160. # Pushes: cell contents
  161. def_graal_op(opc, "LOAD_DEREF", 33, 1, 0, 1)
  162. # Writes a local cell variable determined by the immediate operand
  163. # which indexes a stack slot after celloffset and a variable name in
  164. # cellvars or freevars array ({@code co_cellvars}, {@code
  165. # co_freevars}).
  166. # Pops: value to be written into the cell contents
  167. # FIXME: this should be tagged as both a "free" and as "store" op.
  168. def_graal_op(opc, "STORE_DEREF", 34, 1, 1, 0)
  169. # Deletes a local cell variable determined by the immediate operand
  170. # which indexes a stack slot after celloffset and a variable name in
  171. # cellvars or freevars array ({@code co_cellvars}, {@code
  172. # co_freevars}). Note that it doesn't delete the cell, just its
  173. # contents.
  174. def_graal_op(opc, "DELETE_DEREF", 35, 1, 0, 0)
  175. # TODO not implemented
  176. def_graal_op(opc, "LOAD_CLASSDEREF", 36, 1, 0, 1)
  177. # Raises an exception. If the immediate operand is 0, it pops nothing
  178. # and is equivalent to {@code raise} without arguments. If the
  179. # immediate operand is 1, it is equivalent to {@code raise e} and it
  180. # pops {@code e}. If the immediate operand is 2, it is equivalent to
  181. # {@code raise e from c} and it pops {@code c}, then {@code e}. Other
  182. # immediate operand values are illegal.
  183. def_graal_op(
  184. opc, "RAISE_VARARGS", 37, 1
  185. ) # , (oparg, followingArgs, withJump) -> oparg, 0)
  186. # Creates a slice object. If the immediate argument is 2, it is equivalent to a slice
  187. # {@code a:b}. It pops {@code b}, then {@code a}. If the immediate argument is 3, it is
  188. # equivalent to a slice {@code a:b:c}. It pops {@code c}, then {@code b}, then {@code a}. Other
  189. # immediate operand values are illegal.
  190. # Pushes: the created slice object
  191. def_graal_op(opc, "BUILD_SLICE", 38, 1) # (oparg, followingArgs, withJump) -> oparg, 1)
  192. # Formats a value. If the immediate argument contains flag {@link FormatOptions#FVS_HAVE_SPEC},
  193. # it is equivalent to {@code format(conv(v), spec)}. It pops {@code spec}, then {@code v}.
  194. # Otherwise, it is equivalent to {@code format(conv(v), None)}. It pops {@code v}. {@code conv}
  195. # is determined by the immediate operand which contains one of the {@code FVC} options in
  196. # {@link FormatOptions}.
  197. # Pushes: the formatted value
  198. def_graal_op(
  199. opc, "FORMAT_VALUE", 39, 1
  200. ) # , (oparg, followingArgs, withJump) -> (oparg & FormatOptions.FVS_MASK) == FormatOptions.FVS_HAVE_SPEC ? 2 : 1, 1)
  201. # Extends the immediate operand of the following instruction by its own operand shifted left by
  202. # a byte.
  203. def_graal_op(opc, "EXTENDED_ARG", 40, 1, 0, 0)
  204. # Imports a module by name determined by the immediate operand which
  205. # indexes the names array ({@code co_names}).
  206. # Pops: fromlist (must be a constant {@code TruffleString[]}), then level (must be {@code int})
  207. # Pushes: imported module
  208. def_graal_op(opc, "IMPORT_NAME", 41, 1, 2, 1)
  209. opc["nullaryloadop"].add(41)
  210. # Imports a name from a module. The name determined by the immediate operand which indexes the
  211. # names array ({@code co_names}).
  212. # Pops: module object
  213. # Pushes: module object, imported object
  214. def_graal_op(opc, "IMPORT_FROM", 42, 1, 1, 2)
  215. # Imports all names from a module of name determined by the immediate operand which indexes the
  216. # names array ({@code co_names}). The imported names are written to locals dict (can only be
  217. # invoked on module level).
  218. # Pops: level (must be {@code int})
  219. def_graal_op(opc, "IMPORT_STAR", 43, 1, 1, 0)
  220. # Prints the top of the stack. Used by "single" parsing mode to echo
  221. # expressions.
  222. # Pops: the value to print
  223. def_graal_op(opc, "PRINT_EXPR", 44, 0, 1, 0)
  224. # Creates annotations dict in locals
  225. def_graal_op(opc, "SETUP_ANNOTATIONS", 45, 0, 0, 0)
  226. # Determines if a python object is a sequence.
  227. def_graal_op(opc, "MATCH_SEQUENCE", 46, 0, 0, 1)
  228. # Determines if a Python object is a mapping.
  229. def_graal_op(opc, "MATCH_MAPPING", 47, 0, 0, 1)
  230. # Determines if a Python object is of a particular type.
  231. def_graal_op(opc, "MATCH_CLASS", 48, 1, 3, 2)
  232. # Matches the keys (stack top) in a dict (stack second). On successful
  233. # match pushes the values and True, otherwise None and False.
  234. def_graal_op(opc, "MATCH_KEYS", 49, 0, 2, 4)
  235. # Creates a copy of a dict (stack second) without elements matching a
  236. # tuple of keys (stack top).
  237. def_graal_op(opc, "COPY_DICT_WITHOUT_KEYS", 50, 0, 1, 1)
  238. # Retrieves the length of a Python object and stores it on top.
  239. def_graal_op(opc, "GET_LEN", 51, 0, 0, 1)
  240. # -------------------------------------
  241. # load bytecodes for special constants
  242. # -------------------------------------
  243. def_graal_op(opc, "LOAD_NONE", 52, 0, 0, 1)
  244. opc["nullaryloadop"].add(52)
  245. def_graal_op(opc, "LOAD_ELLIPSIS", 53, 0, 0, 1)
  246. def_graal_op(opc, "LOAD_TRUE", 54, 0, 0, 1)
  247. opc["nullaryloadop"].add(54)
  248. def_graal_op(opc, "LOAD_FALSE", 55, 0, 0, 1)
  249. opc["nullaryloadop"].add(55)
  250. #### Continue adding opcode numbers here...
  251. # Loads signed byte from immediate operand.
  252. #
  253. def_graal_op(opc, "LOAD_BYTE", 1, 0, 1)
  254. #
  255. # Loads {@code int} from primitiveConstants array indexed by the immediate operand.
  256. #
  257. def_graal_op(opc, "LOAD_INT", 1, 0, 1)
  258. #
  259. # Loads {@code long} from primitiveConstants array indexed by the immediate operand.
  260. #
  261. def_graal_op(opc, "LOAD_LONG", 1, 0, 1)
  262. #
  263. # Loads {@code double} from primitiveConstants array indexed by the immediate operand
  264. # (converted from long).
  265. #
  266. def_graal_op(opc, "LOAD_DOUBLE", 1, 0, 1)
  267. #
  268. # Creates a {@link PInt} from a {@link BigInteger} in constants array indexed by the immediate
  269. # operand.
  270. #
  271. def_graal_op(opc, "LOAD_BIGINT", 1, 0, 1)
  272. #
  273. # Currently the same as {@link #LOAD_CONST}.
  274. #
  275. def_graal_op(opc, "LOAD_STRING", 1, 0, 1)
  276. #
  277. # Creates python {@code bytes} from a {@code byte[]} array in constants array indexed by the
  278. # immediate operand.
  279. #
  280. def_graal_op(opc, "LOAD_BYTES", 1, 0, 1)
  281. #
  282. # Creates python {@code complex} from a {@code double[]} array of size 2 in constants array
  283. # indexed by the immediate operand.
  284. #
  285. def_graal_op(opc, "LOAD_COMPLEX", 1, 0, 1)
  286. # Creates a collection out of a Java array in constants array indexed by the immediate operand.
  287. # The second immediate operand determines the array type and kind, using values from {@link
  288. # CollectionBits}. The only allowed kinds are list and tuple.
  289. #
  290. def_graal_op(opc, "LOAD_CONST_COLLECTION", 2, 0, 1)
  291. # -------
  292. # calling
  293. # -------
  294. #
  295. # Calls method on an object using an array as args. The receiver is taken from the first
  296. # element of the array. The method name is determined by the immediate operand which indexes
  297. # the names array ({@code co_names}).
  298. #
  299. # Pops: args ({@code Object[]} of size >= 1)
  300. #
  301. # Pushes: call result
  302. #
  303. def_graal_op(opc, "CALL_METHOD_VARARGS", 1, 1, 1)
  304. #
  305. # Calls method on an object using a number of stack args determined by the first immediate
  306. # operand.
  307. #
  308. # Pops: multiple arguments depending on the first immediate operand, then the method and the
  309. # receiver
  310. #
  311. # Pushes: call result
  312. #
  313. def_graal_op(
  314. opc, "CALL_METHOD", 1
  315. ) # , (oparg, followingArgs, withJump) -> oparg + 2, 1)
  316. #
  317. # Calls a callable using a number of stack args determined by the immediate operand.
  318. #
  319. # Pops: multiple arguments depending on the immediate operand (0 - 4), then the callable
  320. #
  321. # Pushes: call result
  322. #
  323. def_graal_op(
  324. opc, "CALL_FUNCTION", 1
  325. ) # , (oparg, followingArgs, withJump) -> oparg + 1, 1)
  326. #
  327. # Calls a comprehension function with a single iterator argument. Comprehension functions have
  328. # to always have the same call target at given calls site. The instruction makes use of this
  329. # fact and bypasses function object inline caching which would otherwise slow down warmup since
  330. # comprehensions functions are always created anew and thus the cache would always miss.
  331. #
  332. # Pops: iterator, then the function
  333. #
  334. # Pushes: call result
  335. #
  336. def_graal_op(opc, "CALL_COMPREHENSION", 0, 2, 1)
  337. #
  338. # Calls a callable using an arguments array and keywords array.
  339. #
  340. # Pops: keyword args ({@code PKeyword[]}), then args ({@code Object[]}), then callable
  341. #
  342. # Pushes: call result
  343. #
  344. def_graal_op(opc, "CALL_FUNCTION_KW", 0, 3, 1)
  345. #
  346. # Calls a callable using an arguments array. No keywords are passed.
  347. #
  348. # Pops: args ({@code Object[]}), then callable
  349. #
  350. # Pushes: call result
  351. #
  352. def_graal_op(opc, "CALL_FUNCTION_VARARGS", 0, 2, 1)
  353. # ----------------------
  354. # destructuring bytecodes
  355. # ----------------------
  356. # Unpacks an iterable into multiple stack items.
  357. #
  358. # Pops: iterable
  359. #
  360. # Pushed: unpacked items, the count is determined by the immediate operand
  361. #
  362. def_graal_op(
  363. opc, "UNPACK_SEQUENCE", 1, 1
  364. ) # , (oparg, followingArgs, withJump) -> oparg)
  365. # Unpacks an iterable into multiple stack items with a star item that gets the rest. The first
  366. # immediate operand determines the count before the star item, the second determines the count
  367. # after.
  368. #
  369. # Pops: iterable
  370. #
  371. # Pushed: unpacked items (count = first operand), star item, unpacked items (count = second
  372. # operand)
  373. #
  374. def_graal_op(
  375. opc, "UNPACK_EX", 2, 1
  376. ) # (oparg, followingArgs, withJump) -> oparg + 1 + Byte.toUnsignedInt(followingArgs[0]))
  377. # jumps
  378. #
  379. # Get next value from an iterator. If the iterable is exhausted, jump forward by the offset in
  380. # the immediate argument.
  381. #
  382. # Pops: iterator
  383. #
  384. # Pushes (only if not jumping): the iterator, then the next value
  385. #
  386. def_graal_op(
  387. opc, "FOR_ITER", 1, 1
  388. ) # (, (oparg, followingArgs, withJump) -> withJump ? 0 : 2)
  389. #
  390. # Jump forward by the offset in the immediate operand.
  391. #
  392. def_graal_op(opc, "JUMP_FORWARD", 1, 0, 0)
  393. # Jump backward by the offset in the immediate operand. May trigger OSR compilation.
  394. #
  395. def_graal_op(opc, "JUMP_BACKWARD", 1, 0, 0)
  396. # Jump forward by the offset in the immediate operand if the top of the stack is false (in
  397. # Python sense).
  398. #
  399. # Pops (if not jumping): top of the stack
  400. def_graal_op(
  401. opc, "JUMP_IF_FALSE_OR_POP", 3
  402. ) # , (oparg, followingArgs, withJump) -> withJump ? 0 : 1, 0)
  403. # Jump forward by the offset in the immediate operand if the top of the stack is true (in
  404. # Python sense).
  405. #
  406. # Pops (if not jumping): top of the stack
  407. #
  408. def_graal_op(
  409. opc, "JUMP_IF_TRUE_OR_POP", 3
  410. ) # , (oparg, followingArgs, withJump) -> withJump ? 0 : 1, 0)
  411. #
  412. # Jump forward by the offset in the immediate operand if the top of the stack is false (in
  413. # Python sense).
  414. #
  415. # Pops: top of the stack
  416. #
  417. def_graal_op(opc, "POP_AND_JUMP_IF_FALSE", 3, 1, 0)
  418. #
  419. # Jump forward by the offset in the immediate operand if the top of the stack is true (in
  420. # Python sense).
  421. #
  422. # Pops: top of the stack
  423. #
  424. def_graal_op(opc, "POP_AND_JUMP_IF_TRUE", 3, 1, 0)
  425. # ----------------
  426. # making callables
  427. # ----------------
  428. #
  429. # Like {@link #LOAD_DEREF}, but loads the cell itself, not the contents.
  430. #
  431. # Pushes: the cell object
  432. #
  433. def_graal_op(opc, "LOAD_CLOSURE", 1, 0, 1)
  434. #
  435. # Reduces multiple stack items into an array of cell objects.
  436. #
  437. # Pops: multiple cells (count = immediate argument)
  438. #
  439. # Pushes: cell object array ({@code PCell[]})
  440. #
  441. def_graal_op(
  442. opc, "CLOSURE_FROM_STACK", 1
  443. ) # , (oparg, followingArgs, withJump) -> oparg, 1)
  444. #
  445. # Creates a function object. The first immediate argument is an index to the constants array
  446. # that determines the {@link CodeUnit} object that will provide the function's code.
  447. #
  448. # Pops: The second immediate arguments contains flags (defined in {@link CodeUnit}) that
  449. # determine whether it will need to pop (in this order): closure, annotations, keyword only
  450. # defaults, defaults.
  451. #
  452. # Pushes: created function
  453. #
  454. def_graal_op(
  455. opc, "MAKE_FUNCTION", 2
  456. ) # , (oparg, followingArgs, withJump) -> Integer.bitCount(followingArgs[0]), 1)
  457. # -------------------
  458. # collection literals
  459. # -------------------
  460. #
  461. # Creates a collection from multiple elements from the stack. Collection type is determined by
  462. # {@link CollectionBits} in immediate operand.
  463. #
  464. # Pops: items for the collection (count = immediate argument)
  465. #
  466. # Pushes: new collection
  467. #
  468. def_graal_op(
  469. opc, "COLLECTION_FROM_STACK", 1
  470. ) # , (oparg, followingArgs, withJump) -> CollectionBits.elementCount(oparg), 1)
  471. #
  472. # Add multiple elements from the stack to the collection below them. Collection type is
  473. # determined by {@link CollectionBits} in immediate operand. Tuple is not supported.
  474. #
  475. # Pops: items to be added (count = immediate argument)
  476. #
  477. def_graal_op(
  478. opc, "COLLECTION_ADD_STACK", 1
  479. ) # , (oparg, followingArgs, withJump) -> CollectionBits.elementCount(oparg) + 1, 1)
  480. #
  481. # Concatenates two collection of the same type. Collection type is determined by
  482. # {@link CollectionBits} in immediate operand. Tuple is not supported.
  483. #
  484. # Pops: second collection, first collection
  485. #
  486. # Pushes: concatenated collection
  487. #
  488. def_graal_op(opc, "COLLECTION_ADD_COLLECTION", 1, 2, 1)
  489. #
  490. # Converts collection to another type determined by {@link CollectionBits} in immediate
  491. # operand. The converted collection is expected to be an independent copy (they don't share
  492. # storage).
  493. #
  494. # Pops: original collection
  495. #
  496. # Pushes: converted collection
  497. #
  498. def_graal_op(opc, "COLLECTION_FROM_COLLECTION", 1, 1, 1)
  499. #
  500. # Converts list to tuple by reusing the underlying storage.
  501. #
  502. # Pops: list
  503. #
  504. # Pushes: tuple
  505. #
  506. def_graal_op(opc, "TUPLE_FROM_LIST", 0, 1, 1)
  507. #
  508. # Converts list to frozenset.
  509. #
  510. # Pops: list
  511. #
  512. # Pushes: frozenset
  513. #
  514. def_graal_op(opc, "FROZENSET_FROM_LIST", 0, 1, 1)
  515. #
  516. # Adds an item to a collection that is multiple items deep under the top of the stack,
  517. # determined by the immediate argument.
  518. #
  519. # Pops: item to be added
  520. #
  521. def_graal_op(
  522. opc, "ADD_TO_COLLECTION", 1
  523. ) # (oparg, followingArgs, withJump) -> CollectionBits.collectionKind(oparg) == CollectionBits.KIND_DICT ? 2 : 1, 0)
  524. #
  525. # Like {@link #COLLECTION_ADD_COLLECTION} for dicts, but with checks for duplicate keys
  526. # necessary for keyword arguments merge. Note it works with dicts. Keyword arrays need to be
  527. # converted to dicts first.
  528. #
  529. def_graal_op(opc, "KWARGS_DICT_MERGE", 0, 2, 1)
  530. #
  531. # Create a single {@link PKeyword} object. The name is determined by the immediate operand
  532. # which indexes the names array ({@code co_names})
  533. #
  534. # Pops: keyword value
  535. #
  536. # Pushes: keyword object
  537. #
  538. def_graal_op(opc, "MAKE_KEYWORD", 1, 1, 1)
  539. # -----------
  540. # exceptions
  541. # -----------
  542. #
  543. # Jump forward by the offset in the immediate argument if the exception doesn't match the
  544. # expected type. The exception object is {@link PException}, not a python exception.
  545. #
  546. # Pops: expected type, then exception
  547. #
  548. # Pushes (if jumping): the exception
  549. #
  550. def_graal_op(opc, "MATCH_EXC_OR_JUMP", 3, 2, 1)
  551. #
  552. # Save the current exception state on the stack and set it to the exception on the stack. The
  553. # exception object is {@link PException}, not a python exception. The exception is pushed back
  554. # to the top.
  555. #
  556. # Pops: the exception
  557. #
  558. # Pushes: the saved exception state, the exception
  559. #
  560. def_graal_op(opc, "PUSH_EXC_INFO", 0, 0, 1)
  561. # Sets the current exception state to the saved state (by {@link #PUSH_EXC_INFO}) on the stack
  562. # and pop it.
  563. #
  564. # Pops: save exception state
  565. def_graal_op(opc, "POP_EXCEPT", 0, 1, 0)
  566. # Restore exception state and reraise exception.
  567. #
  568. # Pops: exception to reraise, then saved exception state
  569. def_graal_op(opc, "END_EXC_HANDLER", 0, 2, 0)
  570. # Gets the python-level exception object from a {@link PException}.
  571. #
  572. # Pops: a {@link PException} Pushes: python exception
  573. #
  574. def_graal_op(opc, "UNWRAP_EXC", 0, 1, 1)
  575. # ----------
  576. # generators
  577. # ----------
  578. #
  579. # Yield value from the stack to the caller. Saves execution state. The generator will resume at
  580. # the next instruction.
  581. #
  582. # Pops: yielded value
  583. #
  584. def_graal_op(opc, "YIELD_VALUE", 0, 1, 0)
  585. #
  586. # Wrap value from the stack in a {@link PAsyncGenWrappedValue}. CPython 3.11 opcode, used here
  587. # to avoid a runtime check
  588. #
  589. # Pops: an object Pushes: async_generator_wrapped_value
  590. #
  591. def_graal_op(opc, "ASYNCGEN_WRAP", 0, 1, 1)
  592. #
  593. # Resume after yield. Will raise exception passed by {@code throw} if any.
  594. #
  595. # Pushes: value received from {@code send} or {@code None}.
  596. #
  597. def_graal_op(opc, "RESUME_YIELD", 0, 0, 1)
  598. #
  599. # Send value into a generator. Jumps forward by the offset in the immediate argument if the
  600. # generator is exhausted. Used to implement {@code yield from}.
  601. #
  602. # Pops: value to be sent, then generator
  603. #
  604. # Pushes (if not jumping): the generator, then the yielded value
  605. #
  606. # Pushes (if jumping): the generator return value
  607. #
  608. def_graal_op(
  609. opc, "SEND", 1, 2
  610. ) # , (oparg, followingArgs, withJump) -> withJump ? 1 : 2)
  611. # Exception handler for forwarding {@code throw} calls into {@code yield from}.
  612. #
  613. # Pops: exception, then the generator
  614. #
  615. # Pushes (if not jumping): the generator, then the yielded value
  616. #
  617. # Pushes (if jumping): the generator return value
  618. def_graal_op(
  619. opc, "THROW", 1, 2
  620. ) # , (oparg, followingArgs, withJump) -> withJump ? 1 : 2)
  621. # Exception handler for async for loops. If the current exception is StopAsyncIteration, handle
  622. # it, otherwise, reraise.
  623. #
  624. # Pops: exception, then the anext coroutine, then the async iterator
  625. def_graal_op(opc, "END_ASYNC_FOR", 0, 3, 0)
  626. # with statements
  627. #
  628. # Enter a context manager and save data for its exit.
  629. #
  630. # Pops: the context manager
  631. #
  632. # Pushes: the context manager, then maybe-bound {@code __exit__}, then the result of
  633. # {@code __enter__}
  634. def_graal_op(opc, "SETUP_WITH", 0, 1, 3)
  635. # Run the exit handler of a context manager and reraise if necessary.
  636. #
  637. # Pops: exception or {@code None}, then maybe-bound {@code __exit__}, then the context manager
  638. def_graal_op(opc, "EXIT_WITH", 0, 3, 0)
  639. # Enter a context manager and save data for its exit
  640. #
  641. # Pops: the context manager
  642. #
  643. # Pushes: the context manager, then the maybe-bound async function {@code __aexit__}, then the
  644. # awaitable returned by {@code __aenter__}
  645. #
  646. def_graal_op(opc, "SETUP_AWITH", 0, 1, 3)
  647. # Run the exit handler of a context manager
  648. #
  649. # Pops: exception or {@code None}, then maybe-bound {@code __aexit__}, then the context manager
  650. #
  651. # Pushes: the exception or {@code None}, then the awaitable returned by {@code __aexit__}
  652. def_graal_op(opc, "GET_AEXIT_CORO", 0, 3, 2)
  653. # Reraise the exception passed to {@code __aexit__} if appropriate
  654. #
  655. #!Pops: The result of awaiting {@code __aexit__}, then the exception
  656. def_graal_op(opc, "EXIT_AWITH", 0, 2, 0)