ops.py 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579
  1. """Low-level opcodes for compiler intermediate representation (IR).
  2. Opcodes operate on abstract values (Value) in a register machine. Each
  3. value has a type (RType). A value can hold various things, such as:
  4. - local variables (Register)
  5. - intermediate values of expressions (RegisterOp subclasses)
  6. - condition flags (true/false)
  7. - literals (integer literals, True, False, etc.)
  8. """
  9. from __future__ import annotations
  10. from abc import abstractmethod
  11. from typing import TYPE_CHECKING, Dict, Generic, List, NamedTuple, Sequence, TypeVar, Union
  12. from typing_extensions import Final
  13. from mypy_extensions import trait
  14. from mypyc.ir.rtypes import (
  15. RArray,
  16. RInstance,
  17. RTuple,
  18. RType,
  19. RVoid,
  20. bit_rprimitive,
  21. bool_rprimitive,
  22. float_rprimitive,
  23. int_rprimitive,
  24. is_bit_rprimitive,
  25. is_bool_rprimitive,
  26. is_int_rprimitive,
  27. is_none_rprimitive,
  28. is_pointer_rprimitive,
  29. is_short_int_rprimitive,
  30. object_rprimitive,
  31. pointer_rprimitive,
  32. short_int_rprimitive,
  33. void_rtype,
  34. )
  35. if TYPE_CHECKING:
  36. from mypyc.codegen.literals import LiteralValue
  37. from mypyc.ir.class_ir import ClassIR
  38. from mypyc.ir.func_ir import FuncDecl, FuncIR
  39. T = TypeVar("T")
  40. class BasicBlock:
  41. """IR basic block.
  42. Contains a sequence of Ops and ends with a ControlOp (Goto,
  43. Branch, Return or Unreachable). Only the last op can be a
  44. ControlOp.
  45. All generated Ops live in basic blocks. Basic blocks determine the
  46. order of evaluation and control flow within a function. A basic
  47. block is always associated with a single function/method (FuncIR).
  48. When building the IR, ops that raise exceptions can be included in
  49. the middle of a basic block, but the exceptions aren't checked.
  50. Afterwards we perform a transform that inserts explicit checks for
  51. all error conditions and splits basic blocks accordingly to preserve
  52. the invariant that a jump, branch or return can only ever appear
  53. as the final op in a block. Manually inserting error checking ops
  54. would be boring and error-prone.
  55. BasicBlocks have an error_handler attribute that determines where
  56. to jump if an error occurs. If none is specified, an error will
  57. propagate up out of the function. This is compiled away by the
  58. `exceptions` module.
  59. Block labels are used for pretty printing and emitting C code, and
  60. get filled in by those passes.
  61. Ops that may terminate the program aren't treated as exits.
  62. """
  63. def __init__(self, label: int = -1) -> None:
  64. self.label = label
  65. self.ops: list[Op] = []
  66. self.error_handler: BasicBlock | None = None
  67. self.referenced = False
  68. @property
  69. def terminated(self) -> bool:
  70. """Does the block end with a jump, branch or return?
  71. This should always be true after the basic block has been fully built, but
  72. this is false during construction.
  73. """
  74. return bool(self.ops) and isinstance(self.ops[-1], ControlOp)
  75. @property
  76. def terminator(self) -> ControlOp:
  77. """The terminator operation of the block."""
  78. assert bool(self.ops) and isinstance(self.ops[-1], ControlOp)
  79. return self.ops[-1]
  80. # Never generates an exception
  81. ERR_NEVER: Final = 0
  82. # Generates magic value (c_error_value) based on target RType on exception
  83. ERR_MAGIC: Final = 1
  84. # Generates false (bool) on exception
  85. ERR_FALSE: Final = 2
  86. # Always fails
  87. ERR_ALWAYS: Final = 3
  88. # Like ERR_MAGIC, but the magic return overlaps with a possible return value, and
  89. # an extra PyErr_Occurred() check is also required
  90. ERR_MAGIC_OVERLAPPING: Final = 4
  91. # Hack: using this line number for an op will suppress it in tracebacks
  92. NO_TRACEBACK_LINE_NO = -10000
  93. class Value:
  94. """Abstract base class for all IR values.
  95. These include references to registers, literals, and all
  96. operations (Ops), such as assignments, calls and branches.
  97. Values are often used as inputs of Ops. Register can be used as an
  98. assignment target.
  99. A Value is part of the IR being compiled if it's included in a BasicBlock
  100. that is reachable from a FuncIR (i.e., is part of a function).
  101. See also: Op is a subclass of Value that is the base class of all
  102. operations.
  103. """
  104. # Source line number (-1 for no/unknown line)
  105. line = -1
  106. # Type of the value or the result of the operation
  107. type: RType = void_rtype
  108. is_borrowed = False
  109. @property
  110. def is_void(self) -> bool:
  111. return isinstance(self.type, RVoid)
  112. class Register(Value):
  113. """A Register holds a value of a specific type, and it can be read and mutated.
  114. A Register is always local to a function. Each local variable maps
  115. to a Register, and they are also used for some (but not all)
  116. temporary values.
  117. Note that the term 'register' is overloaded and is sometimes used
  118. to refer to arbitrary Values (for example, in RegisterOp).
  119. """
  120. def __init__(self, type: RType, name: str = "", is_arg: bool = False, line: int = -1) -> None:
  121. self.type = type
  122. self.name = name
  123. self.is_arg = is_arg
  124. self.is_borrowed = is_arg
  125. self.line = line
  126. @property
  127. def is_void(self) -> bool:
  128. return False
  129. def __repr__(self) -> str:
  130. return f"<Register {self.name!r} at {hex(id(self))}>"
  131. class Integer(Value):
  132. """Short integer literal.
  133. Integer literals are treated as constant values and are generally
  134. not included in data flow analyses and such, unlike Register and
  135. Op subclasses.
  136. Integer can represent multiple types:
  137. * Short tagged integers (short_int_primitive type; the tag bit is clear)
  138. * Ordinary fixed-width integers (e.g., int32_rprimitive)
  139. * Values of other unboxed primitive types that are represented as integers
  140. (none_rprimitive, bool_rprimitive)
  141. * Null pointers (value 0) of various types, including object_rprimitive
  142. """
  143. def __init__(self, value: int, rtype: RType = short_int_rprimitive, line: int = -1) -> None:
  144. if is_short_int_rprimitive(rtype) or is_int_rprimitive(rtype):
  145. self.value = value * 2
  146. else:
  147. self.value = value
  148. self.type = rtype
  149. self.line = line
  150. def numeric_value(self) -> int:
  151. if is_short_int_rprimitive(self.type) or is_int_rprimitive(self.type):
  152. return self.value // 2
  153. return self.value
  154. class Float(Value):
  155. """Float literal.
  156. Floating point literals are treated as constant values and are generally
  157. not included in data flow analyses and such, unlike Register and
  158. Op subclasses.
  159. """
  160. def __init__(self, value: float, line: int = -1) -> None:
  161. self.value = value
  162. self.type = float_rprimitive
  163. self.line = line
  164. class Op(Value):
  165. """Abstract base class for all IR operations.
  166. Each operation must be stored in a BasicBlock (in 'ops') to be
  167. active in the IR. This is different from non-Op values, including
  168. Register and Integer, where a reference from an active Op is
  169. sufficient to be considered active.
  170. In well-formed IR an active Op has no references to inactive ops
  171. or ops used in another function.
  172. """
  173. def __init__(self, line: int) -> None:
  174. self.line = line
  175. def can_raise(self) -> bool:
  176. # Override this is if Op may raise an exception. Note that currently the fact that
  177. # only RegisterOps may raise an exception in hard coded in some places.
  178. return False
  179. @abstractmethod
  180. def sources(self) -> list[Value]:
  181. """All the values the op may read."""
  182. def stolen(self) -> list[Value]:
  183. """Return arguments that have a reference count stolen by this op"""
  184. return []
  185. def unique_sources(self) -> list[Value]:
  186. result: list[Value] = []
  187. for reg in self.sources():
  188. if reg not in result:
  189. result.append(reg)
  190. return result
  191. @abstractmethod
  192. def accept(self, visitor: OpVisitor[T]) -> T:
  193. pass
  194. class BaseAssign(Op):
  195. """Base class for ops that assign to a register."""
  196. def __init__(self, dest: Register, line: int = -1) -> None:
  197. super().__init__(line)
  198. self.dest = dest
  199. class Assign(BaseAssign):
  200. """Assign a value to a Register (dest = src)."""
  201. error_kind = ERR_NEVER
  202. def __init__(self, dest: Register, src: Value, line: int = -1) -> None:
  203. super().__init__(dest, line)
  204. self.src = src
  205. def sources(self) -> list[Value]:
  206. return [self.src]
  207. def stolen(self) -> list[Value]:
  208. return [self.src]
  209. def accept(self, visitor: OpVisitor[T]) -> T:
  210. return visitor.visit_assign(self)
  211. class AssignMulti(BaseAssign):
  212. """Assign multiple values to a Register (dest = src1, src2, ...).
  213. This is used to initialize RArray values. It's provided to avoid
  214. very verbose IR for common vectorcall operations.
  215. Note that this interacts atypically with reference counting. We
  216. assume that each RArray register is initialized exactly once
  217. with this op.
  218. """
  219. error_kind = ERR_NEVER
  220. def __init__(self, dest: Register, src: list[Value], line: int = -1) -> None:
  221. super().__init__(dest, line)
  222. assert src
  223. assert isinstance(dest.type, RArray)
  224. assert dest.type.length == len(src)
  225. self.src = src
  226. def sources(self) -> list[Value]:
  227. return self.src.copy()
  228. def stolen(self) -> list[Value]:
  229. return []
  230. def accept(self, visitor: OpVisitor[T]) -> T:
  231. return visitor.visit_assign_multi(self)
  232. class ControlOp(Op):
  233. """Control flow operation."""
  234. def targets(self) -> Sequence[BasicBlock]:
  235. """Get all basic block targets of the control operation."""
  236. return ()
  237. def set_target(self, i: int, new: BasicBlock) -> None:
  238. """Update a basic block target."""
  239. raise AssertionError(f"Invalid set_target({self}, {i})")
  240. class Goto(ControlOp):
  241. """Unconditional jump."""
  242. error_kind = ERR_NEVER
  243. def __init__(self, label: BasicBlock, line: int = -1) -> None:
  244. super().__init__(line)
  245. self.label = label
  246. def targets(self) -> Sequence[BasicBlock]:
  247. return (self.label,)
  248. def set_target(self, i: int, new: BasicBlock) -> None:
  249. assert i == 0
  250. self.label = new
  251. def __repr__(self) -> str:
  252. return "<Goto %s>" % self.label.label
  253. def sources(self) -> list[Value]:
  254. return []
  255. def accept(self, visitor: OpVisitor[T]) -> T:
  256. return visitor.visit_goto(self)
  257. class Branch(ControlOp):
  258. """Branch based on a value.
  259. If op is BOOL, branch based on a bit/bool value:
  260. if [not] r1 goto L1 else goto L2
  261. If op is IS_ERROR, branch based on whether there is an error value:
  262. if [not] is_error(r1) goto L1 else goto L2
  263. """
  264. # Branch ops never raise an exception.
  265. error_kind = ERR_NEVER
  266. BOOL: Final = 100
  267. IS_ERROR: Final = 101
  268. def __init__(
  269. self,
  270. value: Value,
  271. true_label: BasicBlock,
  272. false_label: BasicBlock,
  273. op: int,
  274. line: int = -1,
  275. *,
  276. rare: bool = False,
  277. ) -> None:
  278. super().__init__(line)
  279. # Target value being checked
  280. self.value = value
  281. # Branch here if the condition is true
  282. self.true = true_label
  283. # Branch here if the condition is false
  284. self.false = false_label
  285. # Branch.BOOL (boolean check) or Branch.IS_ERROR (error value check)
  286. self.op = op
  287. # If True, the condition is negated
  288. self.negated = False
  289. # If not None, the true label should generate a traceback entry (func name, line number)
  290. self.traceback_entry: tuple[str, int] | None = None
  291. # If True, we expect to usually take the false branch (for optimization purposes);
  292. # this is implicitly treated as true if there is a traceback entry
  293. self.rare = rare
  294. def targets(self) -> Sequence[BasicBlock]:
  295. return (self.true, self.false)
  296. def set_target(self, i: int, new: BasicBlock) -> None:
  297. assert i == 0 or i == 1
  298. if i == 0:
  299. self.true = new
  300. else:
  301. self.false = new
  302. def sources(self) -> list[Value]:
  303. return [self.value]
  304. def invert(self) -> None:
  305. self.negated = not self.negated
  306. def accept(self, visitor: OpVisitor[T]) -> T:
  307. return visitor.visit_branch(self)
  308. class Return(ControlOp):
  309. """Return a value from a function."""
  310. error_kind = ERR_NEVER
  311. def __init__(self, value: Value, line: int = -1) -> None:
  312. super().__init__(line)
  313. self.value = value
  314. def sources(self) -> list[Value]:
  315. return [self.value]
  316. def stolen(self) -> list[Value]:
  317. return [self.value]
  318. def accept(self, visitor: OpVisitor[T]) -> T:
  319. return visitor.visit_return(self)
  320. class Unreachable(ControlOp):
  321. """Mark the end of basic block as unreachable.
  322. This is sometimes necessary when the end of a basic block is never
  323. reached. This can also be explicitly added to the end of non-None
  324. returning functions (in None-returning function we can just return
  325. None).
  326. Mypy statically guarantees that the end of the function is not
  327. unreachable if there is not a return statement.
  328. This prevents the block formatter from being confused due to lack
  329. of a leave and also leaves a nifty note in the IR. It is not
  330. generally processed by visitors.
  331. """
  332. error_kind = ERR_NEVER
  333. def __init__(self, line: int = -1) -> None:
  334. super().__init__(line)
  335. def sources(self) -> list[Value]:
  336. return []
  337. def accept(self, visitor: OpVisitor[T]) -> T:
  338. return visitor.visit_unreachable(self)
  339. class RegisterOp(Op):
  340. """Abstract base class for operations that can be written as r1 = f(r2, ..., rn).
  341. Takes some values, performs an operation, and generates an output
  342. (unless the 'type' attribute is void_rtype, which is the default).
  343. Other ops can refer to the result of the Op by referring to the Op
  344. instance. This doesn't do any explicit control flow, but can raise an
  345. error.
  346. Note that the operands can be arbitrary Values, not just Register
  347. instances, even though the naming may suggest otherwise.
  348. """
  349. error_kind = -1 # Can this raise exception and how is it signalled; one of ERR_*
  350. _type: RType | None = None
  351. def __init__(self, line: int) -> None:
  352. super().__init__(line)
  353. assert self.error_kind != -1, "error_kind not defined"
  354. def can_raise(self) -> bool:
  355. return self.error_kind != ERR_NEVER
  356. class IncRef(RegisterOp):
  357. """Increase reference count (inc_ref src)."""
  358. error_kind = ERR_NEVER
  359. def __init__(self, src: Value, line: int = -1) -> None:
  360. assert src.type.is_refcounted
  361. super().__init__(line)
  362. self.src = src
  363. def sources(self) -> list[Value]:
  364. return [self.src]
  365. def accept(self, visitor: OpVisitor[T]) -> T:
  366. return visitor.visit_inc_ref(self)
  367. class DecRef(RegisterOp):
  368. """Decrease reference count and free object if zero (dec_ref src).
  369. The is_xdec flag says to use an XDECREF, which checks if the
  370. pointer is NULL first.
  371. """
  372. error_kind = ERR_NEVER
  373. def __init__(self, src: Value, is_xdec: bool = False, line: int = -1) -> None:
  374. assert src.type.is_refcounted
  375. super().__init__(line)
  376. self.src = src
  377. self.is_xdec = is_xdec
  378. def __repr__(self) -> str:
  379. return "<{}DecRef {!r}>".format("X" if self.is_xdec else "", self.src)
  380. def sources(self) -> list[Value]:
  381. return [self.src]
  382. def accept(self, visitor: OpVisitor[T]) -> T:
  383. return visitor.visit_dec_ref(self)
  384. class Call(RegisterOp):
  385. """Native call f(arg, ...).
  386. The call target can be a module-level function or a class.
  387. """
  388. def __init__(self, fn: FuncDecl, args: Sequence[Value], line: int) -> None:
  389. self.fn = fn
  390. self.args = list(args)
  391. assert len(self.args) == len(fn.sig.args)
  392. self.type = fn.sig.ret_type
  393. ret_type = fn.sig.ret_type
  394. if not ret_type.error_overlap:
  395. self.error_kind = ERR_MAGIC
  396. else:
  397. self.error_kind = ERR_MAGIC_OVERLAPPING
  398. super().__init__(line)
  399. def sources(self) -> list[Value]:
  400. return list(self.args.copy())
  401. def accept(self, visitor: OpVisitor[T]) -> T:
  402. return visitor.visit_call(self)
  403. class MethodCall(RegisterOp):
  404. """Native method call obj.method(arg, ...)"""
  405. def __init__(self, obj: Value, method: str, args: list[Value], line: int = -1) -> None:
  406. self.obj = obj
  407. self.method = method
  408. self.args = args
  409. assert isinstance(obj.type, RInstance), "Methods can only be called on instances"
  410. self.receiver_type = obj.type
  411. method_ir = self.receiver_type.class_ir.method_sig(method)
  412. assert method_ir is not None, "{} doesn't have method {}".format(
  413. self.receiver_type.name, method
  414. )
  415. ret_type = method_ir.ret_type
  416. self.type = ret_type
  417. if not ret_type.error_overlap:
  418. self.error_kind = ERR_MAGIC
  419. else:
  420. self.error_kind = ERR_MAGIC_OVERLAPPING
  421. super().__init__(line)
  422. def sources(self) -> list[Value]:
  423. return self.args.copy() + [self.obj]
  424. def accept(self, visitor: OpVisitor[T]) -> T:
  425. return visitor.visit_method_call(self)
  426. class LoadErrorValue(RegisterOp):
  427. """Load an error value.
  428. Each type has one reserved value that signals an error (exception). This
  429. loads the error value for a specific type.
  430. """
  431. error_kind = ERR_NEVER
  432. def __init__(
  433. self, rtype: RType, line: int = -1, is_borrowed: bool = False, undefines: bool = False
  434. ) -> None:
  435. super().__init__(line)
  436. self.type = rtype
  437. self.is_borrowed = is_borrowed
  438. # Undefines is true if this should viewed by the definedness
  439. # analysis pass as making the register it is assigned to
  440. # undefined (and thus checks should be added on uses).
  441. self.undefines = undefines
  442. def sources(self) -> list[Value]:
  443. return []
  444. def accept(self, visitor: OpVisitor[T]) -> T:
  445. return visitor.visit_load_error_value(self)
  446. class LoadLiteral(RegisterOp):
  447. """Load a Python literal object (dest = 'foo' / b'foo' / ...).
  448. This is used to load a static PyObject * value corresponding to
  449. a literal of one of the supported types.
  450. Tuple / frozenset literals must contain only valid literal values as items.
  451. NOTE: You can use this to load boxed (Python) int objects. Use
  452. Integer to load unboxed, tagged integers or fixed-width,
  453. low-level integers.
  454. For int literals, both int_rprimitive (CPyTagged) and
  455. object_primitive (PyObject *) are supported as rtype. However,
  456. when using int_rprimitive, the value must *not* be small enough
  457. to fit in an unboxed integer.
  458. """
  459. error_kind = ERR_NEVER
  460. is_borrowed = True
  461. def __init__(self, value: LiteralValue, rtype: RType) -> None:
  462. self.value = value
  463. self.type = rtype
  464. def sources(self) -> list[Value]:
  465. return []
  466. def accept(self, visitor: OpVisitor[T]) -> T:
  467. return visitor.visit_load_literal(self)
  468. class GetAttr(RegisterOp):
  469. """obj.attr (for a native object)"""
  470. error_kind = ERR_MAGIC
  471. def __init__(self, obj: Value, attr: str, line: int, *, borrow: bool = False) -> None:
  472. super().__init__(line)
  473. self.obj = obj
  474. self.attr = attr
  475. assert isinstance(obj.type, RInstance), "Attribute access not supported: %s" % obj.type
  476. self.class_type = obj.type
  477. attr_type = obj.type.attr_type(attr)
  478. self.type = attr_type
  479. if attr_type.error_overlap:
  480. self.error_kind = ERR_MAGIC_OVERLAPPING
  481. self.is_borrowed = borrow and attr_type.is_refcounted
  482. def sources(self) -> list[Value]:
  483. return [self.obj]
  484. def accept(self, visitor: OpVisitor[T]) -> T:
  485. return visitor.visit_get_attr(self)
  486. class SetAttr(RegisterOp):
  487. """obj.attr = src (for a native object)
  488. Steals the reference to src.
  489. """
  490. error_kind = ERR_FALSE
  491. def __init__(self, obj: Value, attr: str, src: Value, line: int) -> None:
  492. super().__init__(line)
  493. self.obj = obj
  494. self.attr = attr
  495. self.src = src
  496. assert isinstance(obj.type, RInstance), "Attribute access not supported: %s" % obj.type
  497. self.class_type = obj.type
  498. self.type = bool_rprimitive
  499. # If True, we can safely assume that the attribute is previously undefined
  500. # and we don't use a setter
  501. self.is_init = False
  502. def mark_as_initializer(self) -> None:
  503. self.is_init = True
  504. self.error_kind = ERR_NEVER
  505. self.type = void_rtype
  506. def sources(self) -> list[Value]:
  507. return [self.obj, self.src]
  508. def stolen(self) -> list[Value]:
  509. return [self.src]
  510. def accept(self, visitor: OpVisitor[T]) -> T:
  511. return visitor.visit_set_attr(self)
  512. # Default name space for statics, variables
  513. NAMESPACE_STATIC: Final = "static"
  514. # Static namespace for pointers to native type objects
  515. NAMESPACE_TYPE: Final = "type"
  516. # Namespace for modules
  517. NAMESPACE_MODULE: Final = "module"
  518. class LoadStatic(RegisterOp):
  519. """Load a static name (name :: static).
  520. Load a C static variable/pointer. The namespace for statics is shared
  521. for the entire compilation group. You can optionally provide a module
  522. name and a sub-namespace identifier for additional namespacing to avoid
  523. name conflicts. The static namespace does not overlap with other C names,
  524. since the final C name will get a prefix, so conflicts only must be
  525. avoided with other statics.
  526. """
  527. error_kind = ERR_NEVER
  528. is_borrowed = True
  529. def __init__(
  530. self,
  531. type: RType,
  532. identifier: str,
  533. module_name: str | None = None,
  534. namespace: str = NAMESPACE_STATIC,
  535. line: int = -1,
  536. ann: object = None,
  537. ) -> None:
  538. super().__init__(line)
  539. self.identifier = identifier
  540. self.module_name = module_name
  541. self.namespace = namespace
  542. self.type = type
  543. self.ann = ann # An object to pretty print with the load
  544. def sources(self) -> list[Value]:
  545. return []
  546. def accept(self, visitor: OpVisitor[T]) -> T:
  547. return visitor.visit_load_static(self)
  548. class InitStatic(RegisterOp):
  549. """static = value :: static
  550. Initialize a C static variable/pointer. See everything in LoadStatic.
  551. """
  552. error_kind = ERR_NEVER
  553. def __init__(
  554. self,
  555. value: Value,
  556. identifier: str,
  557. module_name: str | None = None,
  558. namespace: str = NAMESPACE_STATIC,
  559. line: int = -1,
  560. ) -> None:
  561. super().__init__(line)
  562. self.identifier = identifier
  563. self.module_name = module_name
  564. self.namespace = namespace
  565. self.value = value
  566. def sources(self) -> list[Value]:
  567. return [self.value]
  568. def accept(self, visitor: OpVisitor[T]) -> T:
  569. return visitor.visit_init_static(self)
  570. class TupleSet(RegisterOp):
  571. """dest = (reg, ...) (for fixed-length tuple)"""
  572. error_kind = ERR_NEVER
  573. def __init__(self, items: list[Value], line: int) -> None:
  574. super().__init__(line)
  575. self.items = items
  576. # Don't keep track of the fact that an int is short after it
  577. # is put into a tuple, since we don't properly implement
  578. # runtime subtyping for tuples.
  579. self.tuple_type = RTuple(
  580. [
  581. arg.type if not is_short_int_rprimitive(arg.type) else int_rprimitive
  582. for arg in items
  583. ]
  584. )
  585. self.type = self.tuple_type
  586. def sources(self) -> list[Value]:
  587. return self.items.copy()
  588. def accept(self, visitor: OpVisitor[T]) -> T:
  589. return visitor.visit_tuple_set(self)
  590. class TupleGet(RegisterOp):
  591. """Get item of a fixed-length tuple (src[index])."""
  592. error_kind = ERR_NEVER
  593. def __init__(self, src: Value, index: int, line: int = -1) -> None:
  594. super().__init__(line)
  595. self.src = src
  596. self.index = index
  597. assert isinstance(src.type, RTuple), "TupleGet only operates on tuples"
  598. assert index >= 0
  599. self.type = src.type.types[index]
  600. def sources(self) -> list[Value]:
  601. return [self.src]
  602. def accept(self, visitor: OpVisitor[T]) -> T:
  603. return visitor.visit_tuple_get(self)
  604. class Cast(RegisterOp):
  605. """cast(type, src)
  606. Perform a runtime type check (no representation or value conversion).
  607. DO NOT increment reference counts.
  608. """
  609. error_kind = ERR_MAGIC
  610. def __init__(self, src: Value, typ: RType, line: int, *, borrow: bool = False) -> None:
  611. super().__init__(line)
  612. self.src = src
  613. self.type = typ
  614. self.is_borrowed = borrow
  615. def sources(self) -> list[Value]:
  616. return [self.src]
  617. def stolen(self) -> list[Value]:
  618. if self.is_borrowed:
  619. return []
  620. return [self.src]
  621. def accept(self, visitor: OpVisitor[T]) -> T:
  622. return visitor.visit_cast(self)
  623. class Box(RegisterOp):
  624. """box(type, src)
  625. This converts from a potentially unboxed representation to a straight Python object.
  626. Only supported for types with an unboxed representation.
  627. """
  628. error_kind = ERR_NEVER
  629. def __init__(self, src: Value, line: int = -1) -> None:
  630. super().__init__(line)
  631. self.src = src
  632. self.type = object_rprimitive
  633. # When we box None and bool values, we produce a borrowed result
  634. if (
  635. is_none_rprimitive(self.src.type)
  636. or is_bool_rprimitive(self.src.type)
  637. or is_bit_rprimitive(self.src.type)
  638. ):
  639. self.is_borrowed = True
  640. def sources(self) -> list[Value]:
  641. return [self.src]
  642. def stolen(self) -> list[Value]:
  643. return [self.src]
  644. def accept(self, visitor: OpVisitor[T]) -> T:
  645. return visitor.visit_box(self)
  646. class Unbox(RegisterOp):
  647. """unbox(type, src)
  648. This is similar to a cast, but it also changes to a (potentially) unboxed runtime
  649. representation. Only supported for types with an unboxed representation.
  650. """
  651. def __init__(self, src: Value, typ: RType, line: int) -> None:
  652. self.src = src
  653. self.type = typ
  654. if not typ.error_overlap:
  655. self.error_kind = ERR_MAGIC
  656. else:
  657. self.error_kind = ERR_MAGIC_OVERLAPPING
  658. super().__init__(line)
  659. def sources(self) -> list[Value]:
  660. return [self.src]
  661. def accept(self, visitor: OpVisitor[T]) -> T:
  662. return visitor.visit_unbox(self)
  663. class RaiseStandardError(RegisterOp):
  664. """Raise built-in exception with an optional error string.
  665. We have a separate opcode for this for convenience and to
  666. generate smaller, more idiomatic C code.
  667. """
  668. # TODO: Make it more explicit at IR level that this always raises
  669. error_kind = ERR_FALSE
  670. VALUE_ERROR: Final = "ValueError"
  671. ASSERTION_ERROR: Final = "AssertionError"
  672. STOP_ITERATION: Final = "StopIteration"
  673. UNBOUND_LOCAL_ERROR: Final = "UnboundLocalError"
  674. RUNTIME_ERROR: Final = "RuntimeError"
  675. NAME_ERROR: Final = "NameError"
  676. ZERO_DIVISION_ERROR: Final = "ZeroDivisionError"
  677. def __init__(self, class_name: str, value: str | Value | None, line: int) -> None:
  678. super().__init__(line)
  679. self.class_name = class_name
  680. self.value = value
  681. self.type = bool_rprimitive
  682. def sources(self) -> list[Value]:
  683. return []
  684. def accept(self, visitor: OpVisitor[T]) -> T:
  685. return visitor.visit_raise_standard_error(self)
  686. # True steals all arguments, False steals none, a list steals those in matching positions
  687. StealsDescription = Union[bool, List[bool]]
  688. class CallC(RegisterOp):
  689. """result = function(arg0, arg1, ...)
  690. Call a C function that is not a compiled/native function (for
  691. example, a Python C API function). Use Call to call native
  692. functions.
  693. """
  694. def __init__(
  695. self,
  696. function_name: str,
  697. args: list[Value],
  698. ret_type: RType,
  699. steals: StealsDescription,
  700. is_borrowed: bool,
  701. error_kind: int,
  702. line: int,
  703. var_arg_idx: int = -1,
  704. ) -> None:
  705. self.error_kind = error_kind
  706. super().__init__(line)
  707. self.function_name = function_name
  708. self.args = args
  709. self.type = ret_type
  710. self.steals = steals
  711. self.is_borrowed = is_borrowed
  712. # The position of the first variable argument in args (if >= 0)
  713. self.var_arg_idx = var_arg_idx
  714. def sources(self) -> list[Value]:
  715. return self.args
  716. def stolen(self) -> list[Value]:
  717. if isinstance(self.steals, list):
  718. assert len(self.steals) == len(self.args)
  719. return [arg for arg, steal in zip(self.args, self.steals) if steal]
  720. else:
  721. return [] if not self.steals else self.sources()
  722. def accept(self, visitor: OpVisitor[T]) -> T:
  723. return visitor.visit_call_c(self)
  724. class Truncate(RegisterOp):
  725. """result = truncate src from src_type to dst_type
  726. Truncate a value from type with more bits to type with less bits.
  727. dst_type and src_type can be native integer types, bools or tagged
  728. integers. Tagged integers should have the tag bit unset.
  729. """
  730. error_kind = ERR_NEVER
  731. def __init__(self, src: Value, dst_type: RType, line: int = -1) -> None:
  732. super().__init__(line)
  733. self.src = src
  734. self.type = dst_type
  735. self.src_type = src.type
  736. def sources(self) -> list[Value]:
  737. return [self.src]
  738. def stolen(self) -> list[Value]:
  739. return []
  740. def accept(self, visitor: OpVisitor[T]) -> T:
  741. return visitor.visit_truncate(self)
  742. class Extend(RegisterOp):
  743. """result = extend src from src_type to dst_type
  744. Extend a value from a type with fewer bits to a type with more bits.
  745. dst_type and src_type can be native integer types, bools or tagged
  746. integers. Tagged integers should have the tag bit unset.
  747. If 'signed' is true, perform sign extension. Otherwise, the result will be
  748. zero extended.
  749. """
  750. error_kind = ERR_NEVER
  751. def __init__(self, src: Value, dst_type: RType, signed: bool, line: int = -1) -> None:
  752. super().__init__(line)
  753. self.src = src
  754. self.type = dst_type
  755. self.src_type = src.type
  756. self.signed = signed
  757. def sources(self) -> list[Value]:
  758. return [self.src]
  759. def stolen(self) -> list[Value]:
  760. return []
  761. def accept(self, visitor: OpVisitor[T]) -> T:
  762. return visitor.visit_extend(self)
  763. class LoadGlobal(RegisterOp):
  764. """Load a low-level global variable/pointer.
  765. Note that can't be used to directly load Python module-level
  766. global variable, since they are stored in a globals dictionary
  767. and accessed using dictionary operations.
  768. """
  769. error_kind = ERR_NEVER
  770. is_borrowed = True
  771. def __init__(self, type: RType, identifier: str, line: int = -1, ann: object = None) -> None:
  772. super().__init__(line)
  773. self.identifier = identifier
  774. self.type = type
  775. self.ann = ann # An object to pretty print with the load
  776. def sources(self) -> list[Value]:
  777. return []
  778. def accept(self, visitor: OpVisitor[T]) -> T:
  779. return visitor.visit_load_global(self)
  780. class IntOp(RegisterOp):
  781. """Binary arithmetic or bitwise op on integer operands (e.g., r1 = r2 + r3).
  782. These ops are low-level and are similar to the corresponding C
  783. operations.
  784. The left and right values must have low-level integer types with
  785. compatible representations. Fixed-width integers, short_int_rprimitive,
  786. bool_rprimitive and bit_rprimitive are supported.
  787. For tagged (arbitrary-precision) integer ops look at mypyc.primitives.int_ops.
  788. """
  789. error_kind = ERR_NEVER
  790. # Arithmetic ops
  791. ADD: Final = 0
  792. SUB: Final = 1
  793. MUL: Final = 2
  794. DIV: Final = 3
  795. MOD: Final = 4
  796. # Bitwise ops
  797. AND: Final = 200
  798. OR: Final = 201
  799. XOR: Final = 202
  800. LEFT_SHIFT: Final = 203
  801. RIGHT_SHIFT: Final = 204
  802. op_str: Final = {
  803. ADD: "+",
  804. SUB: "-",
  805. MUL: "*",
  806. DIV: "/",
  807. MOD: "%",
  808. AND: "&",
  809. OR: "|",
  810. XOR: "^",
  811. LEFT_SHIFT: "<<",
  812. RIGHT_SHIFT: ">>",
  813. }
  814. def __init__(self, type: RType, lhs: Value, rhs: Value, op: int, line: int = -1) -> None:
  815. super().__init__(line)
  816. self.type = type
  817. self.lhs = lhs
  818. self.rhs = rhs
  819. self.op = op
  820. def sources(self) -> list[Value]:
  821. return [self.lhs, self.rhs]
  822. def accept(self, visitor: OpVisitor[T]) -> T:
  823. return visitor.visit_int_op(self)
  824. # We can't have this in the IntOp class body, because of
  825. # https://github.com/mypyc/mypyc/issues/932.
  826. int_op_to_id: Final = {op: op_id for op_id, op in IntOp.op_str.items()}
  827. class ComparisonOp(RegisterOp):
  828. """Low-level comparison op for integers and pointers.
  829. Both unsigned and signed comparisons are supported. Supports
  830. comparisons between fixed-width integer types and pointer types.
  831. The operands should have matching sizes.
  832. The result is always a bit (representing a boolean).
  833. Python semantics, such as calling __eq__, are not supported.
  834. """
  835. # Must be ERR_NEVER or ERR_FALSE. ERR_FALSE means that a false result
  836. # indicates that an exception has been raised and should be propagated.
  837. error_kind = ERR_NEVER
  838. # S for signed and U for unsigned
  839. EQ: Final = 100
  840. NEQ: Final = 101
  841. SLT: Final = 102
  842. SGT: Final = 103
  843. SLE: Final = 104
  844. SGE: Final = 105
  845. ULT: Final = 106
  846. UGT: Final = 107
  847. ULE: Final = 108
  848. UGE: Final = 109
  849. op_str: Final = {
  850. EQ: "==",
  851. NEQ: "!=",
  852. SLT: "<",
  853. SGT: ">",
  854. SLE: "<=",
  855. SGE: ">=",
  856. ULT: "<",
  857. UGT: ">",
  858. ULE: "<=",
  859. UGE: ">=",
  860. }
  861. signed_ops: Final = {"==": EQ, "!=": NEQ, "<": SLT, ">": SGT, "<=": SLE, ">=": SGE}
  862. def __init__(self, lhs: Value, rhs: Value, op: int, line: int = -1) -> None:
  863. super().__init__(line)
  864. self.type = bit_rprimitive
  865. self.lhs = lhs
  866. self.rhs = rhs
  867. self.op = op
  868. def sources(self) -> list[Value]:
  869. return [self.lhs, self.rhs]
  870. def accept(self, visitor: OpVisitor[T]) -> T:
  871. return visitor.visit_comparison_op(self)
  872. class FloatOp(RegisterOp):
  873. """Binary float arithmetic op (e.g., r1 = r2 + r3).
  874. These ops are low-level and are similar to the corresponding C
  875. operations (and somewhat different from Python operations).
  876. The left and right values must be floats.
  877. """
  878. error_kind = ERR_NEVER
  879. ADD: Final = 0
  880. SUB: Final = 1
  881. MUL: Final = 2
  882. DIV: Final = 3
  883. MOD: Final = 4
  884. op_str: Final = {ADD: "+", SUB: "-", MUL: "*", DIV: "/", MOD: "%"}
  885. def __init__(self, lhs: Value, rhs: Value, op: int, line: int = -1) -> None:
  886. super().__init__(line)
  887. self.type = float_rprimitive
  888. self.lhs = lhs
  889. self.rhs = rhs
  890. self.op = op
  891. def sources(self) -> List[Value]:
  892. return [self.lhs, self.rhs]
  893. def accept(self, visitor: "OpVisitor[T]") -> T:
  894. return visitor.visit_float_op(self)
  895. # We can't have this in the FloatOp class body, because of
  896. # https://github.com/mypyc/mypyc/issues/932.
  897. float_op_to_id: Final = {op: op_id for op_id, op in FloatOp.op_str.items()}
  898. class FloatNeg(RegisterOp):
  899. """Float negation op (r1 = -r2)."""
  900. error_kind = ERR_NEVER
  901. def __init__(self, src: Value, line: int = -1) -> None:
  902. super().__init__(line)
  903. self.type = float_rprimitive
  904. self.src = src
  905. def sources(self) -> List[Value]:
  906. return [self.src]
  907. def accept(self, visitor: "OpVisitor[T]") -> T:
  908. return visitor.visit_float_neg(self)
  909. class FloatComparisonOp(RegisterOp):
  910. """Low-level comparison op for floats."""
  911. error_kind = ERR_NEVER
  912. EQ: Final = 200
  913. NEQ: Final = 201
  914. LT: Final = 202
  915. GT: Final = 203
  916. LE: Final = 204
  917. GE: Final = 205
  918. op_str: Final = {EQ: "==", NEQ: "!=", LT: "<", GT: ">", LE: "<=", GE: ">="}
  919. def __init__(self, lhs: Value, rhs: Value, op: int, line: int = -1) -> None:
  920. super().__init__(line)
  921. self.type = bit_rprimitive
  922. self.lhs = lhs
  923. self.rhs = rhs
  924. self.op = op
  925. def sources(self) -> List[Value]:
  926. return [self.lhs, self.rhs]
  927. def accept(self, visitor: "OpVisitor[T]") -> T:
  928. return visitor.visit_float_comparison_op(self)
  929. # We can't have this in the FloatOp class body, because of
  930. # https://github.com/mypyc/mypyc/issues/932.
  931. float_comparison_op_to_id: Final = {op: op_id for op_id, op in FloatComparisonOp.op_str.items()}
  932. class LoadMem(RegisterOp):
  933. """Read a memory location: result = *(type *)src.
  934. Attributes:
  935. type: Type of the read value
  936. src: Pointer to memory to read
  937. """
  938. error_kind = ERR_NEVER
  939. def __init__(self, type: RType, src: Value, line: int = -1) -> None:
  940. super().__init__(line)
  941. self.type = type
  942. # TODO: for now we enforce that the src memory address should be Py_ssize_t
  943. # later we should also support same width unsigned int
  944. assert is_pointer_rprimitive(src.type)
  945. self.src = src
  946. self.is_borrowed = True
  947. def sources(self) -> list[Value]:
  948. return [self.src]
  949. def accept(self, visitor: OpVisitor[T]) -> T:
  950. return visitor.visit_load_mem(self)
  951. class SetMem(Op):
  952. """Write to a memory location: *(type *)dest = src
  953. Attributes:
  954. type: Type of the written value
  955. dest: Pointer to memory to write
  956. src: Source value
  957. """
  958. error_kind = ERR_NEVER
  959. def __init__(self, type: RType, dest: Value, src: Value, line: int = -1) -> None:
  960. super().__init__(line)
  961. self.type = void_rtype
  962. self.dest_type = type
  963. self.src = src
  964. self.dest = dest
  965. def sources(self) -> list[Value]:
  966. return [self.src, self.dest]
  967. def stolen(self) -> list[Value]:
  968. return [self.src]
  969. def accept(self, visitor: OpVisitor[T]) -> T:
  970. return visitor.visit_set_mem(self)
  971. class GetElementPtr(RegisterOp):
  972. """Get the address of a struct element.
  973. Note that you may need to use KeepAlive to avoid the struct
  974. being freed, if it's reference counted, such as PyObject *.
  975. """
  976. error_kind = ERR_NEVER
  977. def __init__(self, src: Value, src_type: RType, field: str, line: int = -1) -> None:
  978. super().__init__(line)
  979. self.type = pointer_rprimitive
  980. self.src = src
  981. self.src_type = src_type
  982. self.field = field
  983. def sources(self) -> list[Value]:
  984. return [self.src]
  985. def accept(self, visitor: OpVisitor[T]) -> T:
  986. return visitor.visit_get_element_ptr(self)
  987. class LoadAddress(RegisterOp):
  988. """Get the address of a value: result = (type)&src
  989. Attributes:
  990. type: Type of the loaded address(e.g. ptr/object_ptr)
  991. src: Source value (str for globals like 'PyList_Type',
  992. Register for temporary values or locals, LoadStatic
  993. for statics.)
  994. """
  995. error_kind = ERR_NEVER
  996. is_borrowed = True
  997. def __init__(self, type: RType, src: str | Register | LoadStatic, line: int = -1) -> None:
  998. super().__init__(line)
  999. self.type = type
  1000. self.src = src
  1001. def sources(self) -> list[Value]:
  1002. if isinstance(self.src, Register):
  1003. return [self.src]
  1004. else:
  1005. return []
  1006. def accept(self, visitor: OpVisitor[T]) -> T:
  1007. return visitor.visit_load_address(self)
  1008. class KeepAlive(RegisterOp):
  1009. """A no-op operation that ensures source values aren't freed.
  1010. This is sometimes useful to avoid decref when a reference is still
  1011. being held but not seen by the compiler.
  1012. A typical use case is like this (C-like pseudocode):
  1013. ptr = &x.item
  1014. r = *ptr
  1015. keep_alive x # x must not be freed here
  1016. # x may be freed here
  1017. If we didn't have "keep_alive x", x could be freed immediately
  1018. after taking the address of 'item', resulting in a read after free
  1019. on the second line.
  1020. """
  1021. error_kind = ERR_NEVER
  1022. def __init__(self, src: list[Value]) -> None:
  1023. assert src
  1024. self.src = src
  1025. def sources(self) -> list[Value]:
  1026. return self.src.copy()
  1027. def accept(self, visitor: OpVisitor[T]) -> T:
  1028. return visitor.visit_keep_alive(self)
  1029. @trait
  1030. class OpVisitor(Generic[T]):
  1031. """Generic visitor over ops (uses the visitor design pattern)."""
  1032. @abstractmethod
  1033. def visit_goto(self, op: Goto) -> T:
  1034. raise NotImplementedError
  1035. @abstractmethod
  1036. def visit_branch(self, op: Branch) -> T:
  1037. raise NotImplementedError
  1038. @abstractmethod
  1039. def visit_return(self, op: Return) -> T:
  1040. raise NotImplementedError
  1041. @abstractmethod
  1042. def visit_unreachable(self, op: Unreachable) -> T:
  1043. raise NotImplementedError
  1044. @abstractmethod
  1045. def visit_assign(self, op: Assign) -> T:
  1046. raise NotImplementedError
  1047. @abstractmethod
  1048. def visit_assign_multi(self, op: AssignMulti) -> T:
  1049. raise NotImplementedError
  1050. @abstractmethod
  1051. def visit_load_error_value(self, op: LoadErrorValue) -> T:
  1052. raise NotImplementedError
  1053. @abstractmethod
  1054. def visit_load_literal(self, op: LoadLiteral) -> T:
  1055. raise NotImplementedError
  1056. @abstractmethod
  1057. def visit_get_attr(self, op: GetAttr) -> T:
  1058. raise NotImplementedError
  1059. @abstractmethod
  1060. def visit_set_attr(self, op: SetAttr) -> T:
  1061. raise NotImplementedError
  1062. @abstractmethod
  1063. def visit_load_static(self, op: LoadStatic) -> T:
  1064. raise NotImplementedError
  1065. @abstractmethod
  1066. def visit_init_static(self, op: InitStatic) -> T:
  1067. raise NotImplementedError
  1068. @abstractmethod
  1069. def visit_tuple_get(self, op: TupleGet) -> T:
  1070. raise NotImplementedError
  1071. @abstractmethod
  1072. def visit_tuple_set(self, op: TupleSet) -> T:
  1073. raise NotImplementedError
  1074. def visit_inc_ref(self, op: IncRef) -> T:
  1075. raise NotImplementedError
  1076. def visit_dec_ref(self, op: DecRef) -> T:
  1077. raise NotImplementedError
  1078. @abstractmethod
  1079. def visit_call(self, op: Call) -> T:
  1080. raise NotImplementedError
  1081. @abstractmethod
  1082. def visit_method_call(self, op: MethodCall) -> T:
  1083. raise NotImplementedError
  1084. @abstractmethod
  1085. def visit_cast(self, op: Cast) -> T:
  1086. raise NotImplementedError
  1087. @abstractmethod
  1088. def visit_box(self, op: Box) -> T:
  1089. raise NotImplementedError
  1090. @abstractmethod
  1091. def visit_unbox(self, op: Unbox) -> T:
  1092. raise NotImplementedError
  1093. @abstractmethod
  1094. def visit_raise_standard_error(self, op: RaiseStandardError) -> T:
  1095. raise NotImplementedError
  1096. @abstractmethod
  1097. def visit_call_c(self, op: CallC) -> T:
  1098. raise NotImplementedError
  1099. @abstractmethod
  1100. def visit_truncate(self, op: Truncate) -> T:
  1101. raise NotImplementedError
  1102. @abstractmethod
  1103. def visit_extend(self, op: Extend) -> T:
  1104. raise NotImplementedError
  1105. @abstractmethod
  1106. def visit_load_global(self, op: LoadGlobal) -> T:
  1107. raise NotImplementedError
  1108. @abstractmethod
  1109. def visit_int_op(self, op: IntOp) -> T:
  1110. raise NotImplementedError
  1111. @abstractmethod
  1112. def visit_comparison_op(self, op: ComparisonOp) -> T:
  1113. raise NotImplementedError
  1114. @abstractmethod
  1115. def visit_float_op(self, op: FloatOp) -> T:
  1116. raise NotImplementedError
  1117. @abstractmethod
  1118. def visit_float_neg(self, op: FloatNeg) -> T:
  1119. raise NotImplementedError
  1120. @abstractmethod
  1121. def visit_float_comparison_op(self, op: FloatComparisonOp) -> T:
  1122. raise NotImplementedError
  1123. @abstractmethod
  1124. def visit_load_mem(self, op: LoadMem) -> T:
  1125. raise NotImplementedError
  1126. @abstractmethod
  1127. def visit_set_mem(self, op: SetMem) -> T:
  1128. raise NotImplementedError
  1129. @abstractmethod
  1130. def visit_get_element_ptr(self, op: GetElementPtr) -> T:
  1131. raise NotImplementedError
  1132. @abstractmethod
  1133. def visit_load_address(self, op: LoadAddress) -> T:
  1134. raise NotImplementedError
  1135. @abstractmethod
  1136. def visit_keep_alive(self, op: KeepAlive) -> T:
  1137. raise NotImplementedError
  1138. # TODO: Should the following definition live somewhere else?
  1139. # We do a three-pass deserialization scheme in order to resolve name
  1140. # references.
  1141. # 1. Create an empty ClassIR for each class in an SCC.
  1142. # 2. Deserialize all of the functions, which can contain references
  1143. # to ClassIRs in their types
  1144. # 3. Deserialize all of the classes, which contain lots of references
  1145. # to the functions they contain. (And to other classes.)
  1146. #
  1147. # Note that this approach differs from how we deserialize ASTs in mypy itself,
  1148. # where everything is deserialized in one pass then a second pass cleans up
  1149. # 'cross_refs'. We don't follow that approach here because it seems to be more
  1150. # code for not a lot of gain since it is easy in mypyc to identify all the objects
  1151. # we might need to reference.
  1152. #
  1153. # Because of these references, we need to maintain maps from class
  1154. # names to ClassIRs and func IDs to FuncIRs.
  1155. #
  1156. # These are tracked in a DeserMaps which is passed to every
  1157. # deserialization function.
  1158. #
  1159. # (Serialization and deserialization *will* be used for incremental
  1160. # compilation but so far it is not hooked up to anything.)
  1161. class DeserMaps(NamedTuple):
  1162. classes: Dict[str, "ClassIR"]
  1163. functions: Dict[str, "FuncIR"]