| 1 |
"""IRBuilder AST transform helpers shared between expressions and statements. |
| 2 |
|
| 3 |
Shared code that is tightly coupled to mypy ASTs can be put here instead of |
| 4 |
making mypyc.irbuild.builder larger. |
| 5 |
""" |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
from mypy.nodes import ( |
| 10 |
LDEF, |
| 11 |
BytesExpr, |
| 12 |
ComparisonExpr, |
| 13 |
Expression, |
| 14 |
FloatExpr, |
| 15 |
IntExpr, |
| 16 |
MemberExpr, |
| 17 |
NameExpr, |
| 18 |
OpExpr, |
| 19 |
StrExpr, |
| 20 |
UnaryExpr, |
| 21 |
Var, |
| 22 |
) |
| 23 |
from mypyc.ir.ops import BasicBlock |
| 24 |
from mypyc.ir.rtypes import is_fixed_width_rtype, is_tagged |
| 25 |
from mypyc.irbuild.builder import IRBuilder |
| 26 |
from mypyc.irbuild.constant_fold import constant_fold_expr |
| 27 |
|
| 28 |
|
| 29 |
def process_conditional( |
| 30 |
self: IRBuilder, e: Expression, true: BasicBlock, false: BasicBlock |
| 31 |
) -> None: |
| 32 |
if isinstance(e, OpExpr) and e.op in ["and", "or"]: |
| 33 |
if e.op == "and": |
| 34 |
# Short circuit 'and' in a conditional context. |
| 35 |
new = BasicBlock() |
| 36 |
process_conditional(self, e.left, new, false) |
| 37 |
self.activate_block(new) |
| 38 |
process_conditional(self, e.right, true, false) |
| 39 |
else: |
| 40 |
# Short circuit 'or' in a conditional context. |
| 41 |
new = BasicBlock() |
| 42 |
process_conditional(self, e.left, true, new) |
| 43 |
self.activate_block(new) |
| 44 |
process_conditional(self, e.right, true, false) |
| 45 |
elif isinstance(e, UnaryExpr) and e.op == "not": |
| 46 |
process_conditional(self, e.expr, false, true) |
| 47 |
else: |
| 48 |
res = maybe_process_conditional_comparison(self, e, true, false) |
| 49 |
if res: |
| 50 |
return |
| 51 |
# Catch-all for arbitrary expressions. |
| 52 |
reg = self.accept(e) |
| 53 |
self.add_bool_branch(reg, true, false) |
| 54 |
|
| 55 |
|
| 56 |
def maybe_process_conditional_comparison( |
| 57 |
self: IRBuilder, e: Expression, true: BasicBlock, false: BasicBlock |
| 58 |
) -> bool: |
| 59 |
"""Transform simple tagged integer comparisons in a conditional context. |
| 60 |
|
| 61 |
Return True if the operation is supported (and was transformed). Otherwise, |
| 62 |
do nothing and return False. |
| 63 |
|
| 64 |
Args: |
| 65 |
self: IR form Builder |
| 66 |
e: Arbitrary expression |
| 67 |
true: Branch target if comparison is true |
| 68 |
false: Branch target if comparison is false |
| 69 |
""" |
| 70 |
if not isinstance(e, ComparisonExpr) or len(e.operands) != 2: |
| 71 |
return False |
| 72 |
ltype = self.node_type(e.operands[0]) |
| 73 |
rtype = self.node_type(e.operands[1]) |
| 74 |
if not ( |
| 75 |
(is_tagged(ltype) or is_fixed_width_rtype(ltype)) |
| 76 |
and (is_tagged(rtype) or is_fixed_width_rtype(rtype)) |
| 77 |
): |
| 78 |
return False |
| 79 |
op = e.operators[0] |
| 80 |
if op not in ("==", "!=", "<", "<=", ">", ">="): |
| 81 |
return False |
| 82 |
left_expr = e.operands[0] |
| 83 |
right_expr = e.operands[1] |
| 84 |
borrow_left = is_borrow_friendly_expr(self, right_expr) |
| 85 |
left = self.accept(left_expr, can_borrow=borrow_left) |
| 86 |
right = self.accept(right_expr, can_borrow=True) |
| 87 |
if is_fixed_width_rtype(ltype) or is_fixed_width_rtype(rtype): |
| 88 |
if not is_fixed_width_rtype(ltype): |
| 89 |
left = self.coerce(left, rtype, e.line) |
| 90 |
elif not is_fixed_width_rtype(rtype): |
| 91 |
right = self.coerce(right, ltype, e.line) |
| 92 |
reg = self.binary_op(left, right, op, e.line) |
| 93 |
self.builder.flush_keep_alives(e.line) |
| 94 |
self.add_bool_branch(reg, true, false) |
| 95 |
else: |
| 96 |
# "left op right" for two tagged integers |
| 97 |
reg = self.builder.binary_op(left, right, op, e.line) |
| 98 |
self.flush_keep_alives(e.line) |
| 99 |
self.add_bool_branch(reg, true, false) |
| 100 |
return True |
| 101 |
|
| 102 |
|
| 103 |
def is_borrow_friendly_expr(self: IRBuilder, expr: Expression) -> bool: |
| 104 |
"""Can the result of the expression borrowed temporarily? |
| 105 |
|
| 106 |
Borrowing means keeping a reference without incrementing the reference count. |
| 107 |
""" |
| 108 |
if isinstance(expr, (IntExpr, FloatExpr, StrExpr, BytesExpr)): |
| 109 |
# Literals are immortal and can always be borrowed |
| 110 |
return True |
| 111 |
if ( |
| 112 |
isinstance(expr, (UnaryExpr, OpExpr, NameExpr, MemberExpr)) |
| 113 |
and constant_fold_expr(self, expr) is not None |
| 114 |
): |
| 115 |
# Literal expressions are similar to literals |
| 116 |
return True |
| 117 |
if isinstance(expr, NameExpr): |
| 118 |
if isinstance(expr.node, Var) and expr.kind == LDEF: |
| 119 |
# Local variable reference can be borrowed |
| 120 |
return True |
| 121 |
if isinstance(expr, MemberExpr) and self.is_native_attr_ref(expr): |
| 122 |
return True |
| 123 |
return False |