Skip to content

Commit 2c010b1

Browse files
author
whitequark
committedJul 19, 2015
Remove UnaryOp ARTIQ IR instruction; rename BinaryOp to Arith.
Everything it can express can also be expressed via Arith.
1 parent ac491fa commit 2c010b1

File tree

2 files changed

+55
-74
lines changed

2 files changed

+55
-74
lines changed
 

Diff for: ‎artiq/compiler/ir.py

+4-27
Original file line numberDiff line numberDiff line change
@@ -658,29 +658,6 @@ def index(self):
658658
def value(self):
659659
return self.operands[2]
660660

661-
class UnaryOp(Instruction):
662-
"""
663-
An unary operation on numbers.
664-
665-
:ivar op: (:class:`pythonparser.ast.unaryop`) operation
666-
"""
667-
668-
"""
669-
:param op: (:class:`pythonparser.ast.unaryop`) operation
670-
:param operand: (:class:`Value`) operand
671-
"""
672-
def __init__(self, op, operand, name=""):
673-
assert isinstance(op, ast.unaryop)
674-
assert isinstance(operand, Value)
675-
super().__init__([operand], operand.type, name)
676-
self.op = op
677-
678-
def opcode(self):
679-
return "unaryop({})".format(type(self.op).__name__)
680-
681-
def operand(self):
682-
return self.operands[0]
683-
684661
class Coerce(Instruction):
685662
"""
686663
A coercion operation for numbers.
@@ -697,11 +674,11 @@ def opcode(self):
697674
def value(self):
698675
return self.operands[0]
699676

700-
class BinaryOp(Instruction):
677+
class Arith(Instruction):
701678
"""
702-
A binary operation on numbers.
679+
An arithmetic operation on numbers.
703680
704-
:ivar op: (:class:`pythonparser.ast.unaryop`) operation
681+
:ivar op: (:class:`pythonparser.ast.operator`) operation
705682
"""
706683

707684
"""
@@ -718,7 +695,7 @@ def __init__(self, op, lhs, rhs, name=""):
718695
self.op = op
719696

720697
def opcode(self):
721-
return "binaryop({})".format(type(self.op).__name__)
698+
return "arith({})".format(type(self.op).__name__)
722699

723700
def lhs(self):
724701
return self.operands[0]

Diff for: ‎artiq/compiler/transforms/ir_generator.py

+51-47
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,8 @@ def visit_Expr(self, node):
255255
def visit_Pass(self, node):
256256
# Insert a dummy instruction so that analyses which extract
257257
# locations from CFG have something to use.
258-
self.append(ir.BinaryOp(ast.Add(loc=None),
259-
ir.Constant(0, self._size_type), ir.Constant(0, self._size_type)))
258+
self.append(ir.Arith(ast.Add(loc=None),
259+
ir.Constant(0, self._size_type), ir.Constant(0, self._size_type)))
260260

261261
def visit_Assign(self, node):
262262
try:
@@ -270,7 +270,7 @@ def visit_Assign(self, node):
270270
def visit_AugAssign(self, node):
271271
lhs = self.visit(target)
272272
rhs = self.visit(node.value)
273-
value = self.append(ir.BinaryOp(node.op, lhs, rhs))
273+
value = self.append(ir.Arith(node.op, lhs, rhs))
274274
try:
275275
self.current_assign = value
276276
self.visit(node.target)
@@ -346,8 +346,8 @@ def _iterable_len(self, value, typ=builtins.TInt(types.TValue(32))):
346346
start = self.append(ir.GetAttr(value, "start"))
347347
stop = self.append(ir.GetAttr(value, "stop"))
348348
step = self.append(ir.GetAttr(value, "step"))
349-
spread = self.append(ir.BinaryOp(ast.Sub(loc=None), stop, start))
350-
return self.append(ir.BinaryOp(ast.FloorDiv(loc=None), spread, step))
349+
spread = self.append(ir.Arith(ast.Sub(loc=None), stop, start))
350+
return self.append(ir.Arith(ast.FloorDiv(loc=None), spread, step))
351351
else:
352352
assert False
353353

@@ -358,8 +358,8 @@ def _iterable_get(self, value, index):
358358
elif builtins.is_range(value.type):
359359
start = self.append(ir.GetAttr(value, "start"))
360360
step = self.append(ir.GetAttr(value, "step"))
361-
offset = self.append(ir.BinaryOp(ast.Mult(loc=None), step, index))
362-
return self.append(ir.BinaryOp(ast.Add(loc=None), start, offset))
361+
offset = self.append(ir.Arith(ast.Mult(loc=None), step, index))
362+
return self.append(ir.Arith(ast.Add(loc=None), start, offset))
363363
else:
364364
assert False
365365

@@ -382,8 +382,7 @@ def visit_For(self, node):
382382
old_continue, self.continue_target = self.continue_target, continue_block
383383
self.current_block = continue_block
384384

385-
updated_index = self.append(ir.BinaryOp(ast.Add(loc=None), phi,
386-
ir.Constant(1, phi.type)))
385+
updated_index = self.append(ir.Arith(ast.Add(loc=None), phi, ir.Constant(1, phi.type)))
387386
phi.add_incoming(updated_index, continue_block)
388387
self.append(ir.Branch(head))
389388

@@ -604,7 +603,7 @@ def visit_AttributeT(self, node):
604603
def _map_index(self, length, index):
605604
lt_0 = self.append(ir.Compare(ast.Lt(loc=None),
606605
index, ir.Constant(0, index.type)))
607-
from_end = self.append(ir.BinaryOp(ast.Add(loc=None), length, index))
606+
from_end = self.append(ir.Arith(ast.Add(loc=None), length, index))
608607
mapped_index = self.append(ir.Select(lt_0, from_end, index))
609608
mapped_ge_0 = self.append(ir.Compare(ast.GtE(loc=None),
610609
mapped_index, ir.Constant(0, mapped_index.type)))
@@ -696,9 +695,9 @@ def visit_SubscriptT(self, node):
696695
else:
697696
step = ir.Constant(1, node.slice.type)
698697

699-
unstepped_size = self.append(ir.BinaryOp(ast.Sub(loc=None),
700-
mapped_max_index, mapped_min_index))
701-
slice_size = self.append(ir.BinaryOp(ast.FloorDiv(loc=None), unstepped_size, step))
698+
unstepped_size = self.append(ir.Arith(ast.Sub(loc=None),
699+
mapped_max_index, mapped_min_index))
700+
slice_size = self.append(ir.Arith(ast.FloorDiv(loc=None), unstepped_size, step))
702701

703702
self._make_check(self.append(ir.Compare(ast.Eq(loc=None), slice_size, length)),
704703
lambda: self.append(ir.Alloc([], builtins.TValueError())))
@@ -709,8 +708,8 @@ def visit_SubscriptT(self, node):
709708
other_value = self.current_assign
710709

711710
def body_gen(other_index):
712-
offset = self.append(ir.BinaryOp(ast.Mult(loc=None), step, other_index))
713-
index = self.append(ir.BinaryOp(ast.Add(loc=None), min_index, offset))
711+
offset = self.append(ir.Arith(ast.Mult(loc=None), step, other_index))
712+
index = self.append(ir.Arith(ast.Add(loc=None), min_index, offset))
714713

715714
if self.current_assign is None:
716715
elem = self._iterable_get(value, index)
@@ -719,8 +718,8 @@ def body_gen(other_index):
719718
elem = self.append(ir.GetElem(self.current_assign, other_index))
720719
self.append(ir.SetElem(value, index, elem))
721720

722-
return self.append(ir.BinaryOp(ast.Add(loc=None), other_index,
723-
ir.Constant(1, node.slice.type)))
721+
return self.append(ir.Arith(ast.Add(loc=None), other_index,
722+
ir.Constant(1, node.slice.type)))
724723
self._make_loop(ir.Constant(0, node.slice.type),
725724
lambda index: self.append(ir.Compare(ast.Lt(loc=None), index, slice_size)),
726725
body_gen)
@@ -790,8 +789,8 @@ def body_gen(index):
790789

791790
mapped_elt = self.visit(node.elt)
792791
self.append(ir.SetElem(result, index, mapped_elt))
793-
return self.append(ir.BinaryOp(ast.Add(loc=None), index,
794-
ir.Constant(1, length.type)))
792+
return self.append(ir.Arith(ast.Add(loc=None), index,
793+
ir.Constant(1, length.type)))
795794
self._make_loop(ir.Constant(0, length.type),
796795
lambda index: self.append(ir.Compare(ast.Lt(loc=None), index, length)),
797796
body_gen)
@@ -822,8 +821,15 @@ def visit_UnaryOpT(self, node):
822821
return self.append(ir.Select(node.operand,
823822
ir.Constant(False, builtins.TBool()),
824823
ir.Constant(True, builtins.TBool())))
825-
else: # Numeric operators
826-
return self.append(ir.UnaryOp(node.op, self.visit(node.operand)))
824+
elif isinstance(node.op, ast.USub):
825+
operand = self.visit(node.operand)
826+
return self.append(ir.Arith(ast.Sub(loc=None),
827+
ir.Constant(0, operand.type), operand))
828+
elif isinstance(node.op, ast.UAdd):
829+
# No-op.
830+
return self.visit(node.operand)
831+
else:
832+
assert False
827833

828834
def visit_CoerceT(self, node):
829835
value = self.visit(node.value)
@@ -836,9 +842,7 @@ def visit_CoerceT(self, node):
836842

837843
def visit_BinOpT(self, node):
838844
if builtins.is_numeric(node.type):
839-
return self.append(ir.BinaryOp(node.op,
840-
self.visit(node.left),
841-
self.visit(node.right)))
845+
return self.append(ir.Arith(node.op, self.visit(node.left), self.visit(node.right)))
842846
elif isinstance(node.op, ast.Add): # list + list, tuple + tuple
843847
lhs, rhs = self.visit(node.left), self.visit(node.right)
844848
if types.is_tuple(node.left.type) and builtins.is_tuple(node.right.type):
@@ -852,26 +856,26 @@ def visit_BinOpT(self, node):
852856
lhs_length = self.append(ir.Builtin("len", [lhs], self._size_type))
853857
rhs_length = self.append(ir.Builtin("len", [rhs], self._size_type))
854858

855-
result_length = self.append(ir.BinaryOp(ast.Add(loc=None), lhs_length, rhs_length))
859+
result_length = self.append(ir.Arith(ast.Add(loc=None), lhs_length, rhs_length))
856860
result = self.append(ir.Alloc([result_length], node.type))
857861

858862
# Copy lhs
859863
def body_gen(index):
860864
elt = self.append(ir.GetElem(lhs, index))
861865
self.append(ir.SetElem(result, index, elt))
862-
return self.append(ir.BinaryOp(ast.Add(loc=None), index,
863-
ir.Constant(1, self._size_type)))
866+
return self.append(ir.Arith(ast.Add(loc=None), index,
867+
ir.Constant(1, self._size_type)))
864868
self._make_loop(ir.Constant(0, self._size_type),
865869
lambda index: self.append(ir.Compare(ast.Lt(loc=None), index, lhs_length)),
866870
body_gen)
867871

868872
# Copy rhs
869873
def body_gen(index):
870874
elt = self.append(ir.GetElem(rhs, index))
871-
result_index = self.append(ir.BinaryOp(ast.Add(loc=None), index, lhs_length))
875+
result_index = self.append(ir.Arith(ast.Add(loc=None), index, lhs_length))
872876
self.append(ir.SetElem(result, result_index, elt))
873-
return self.append(ir.BinaryOp(ast.Add(loc=None), index,
874-
ir.Constant(1, self._size_type)))
877+
return self.append(ir.Arith(ast.Add(loc=None), index,
878+
ir.Constant(1, self._size_type)))
875879
self._make_loop(ir.Constant(0, self._size_type),
876880
lambda index: self.append(ir.Compare(ast.Lt(loc=None), index, rhs_length)),
877881
body_gen)
@@ -890,27 +894,27 @@ def body_gen(index):
890894

891895
lst_length = self.append(ir.Builtin("len", [lst], self._size_type))
892896

893-
result_length = self.append(ir.BinaryOp(ast.Mult(loc=None), lst_length, num))
897+
result_length = self.append(ir.Arith(ast.Mult(loc=None), lst_length, num))
894898
result = self.append(ir.Alloc([result_length], node.type))
895899

896900
# num times...
897901
def body_gen(num_index):
898902
# ... copy the list
899903
def body_gen(lst_index):
900904
elt = self.append(ir.GetElem(lst, lst_index))
901-
base_index = self.append(ir.BinaryOp(ast.Mult(loc=None),
902-
num_index, lst_length))
903-
result_index = self.append(ir.BinaryOp(ast.Add(loc=None),
904-
base_index, lst_index))
905+
base_index = self.append(ir.Arith(ast.Mult(loc=None),
906+
num_index, lst_length))
907+
result_index = self.append(ir.Arith(ast.Add(loc=None),
908+
base_index, lst_index))
905909
self.append(ir.SetElem(result, base_index, elt))
906-
return self.append(ir.BinaryOp(ast.Add(loc=None), lst_index,
907-
ir.Constant(1, self._size_type)))
910+
return self.append(ir.Arith(ast.Add(loc=None), lst_index,
911+
ir.Constant(1, self._size_type)))
908912
self._make_loop(ir.Constant(0, self._size_type),
909913
lambda index: self.append(ir.Compare(ast.Lt(loc=None), index, lst_length)),
910914
body_gen)
911915

912-
return self.append(ir.BinaryOp(ast.Add(loc=None), lst_length,
913-
ir.Constant(1, self._size_type)))
916+
return self.append(ir.Arith(ast.Add(loc=None), lst_length,
917+
ir.Constant(1, self._size_type)))
914918
self._make_loop(ir.Constant(0, self._size_type),
915919
lambda index: self.append(ir.Compare(ast.Lt(loc=None), index, num)),
916920
body_gen)
@@ -955,8 +959,8 @@ def _compare_pair_order(self, op, lhs, rhs):
955959

956960
loop_body2 = self.add_block()
957961
self.current_block = loop_body2
958-
index_next = self.append(ir.BinaryOp(ast.Add(loc=None), index_phi,
959-
ir.Constant(1, self._size_type)))
962+
index_next = self.append(ir.Arith(ast.Add(loc=None), index_phi,
963+
ir.Constant(1, self._size_type)))
960964
self.append(ir.Branch(loop_head))
961965
index_phi.add_incoming(index_next, loop_body2)
962966

@@ -988,8 +992,8 @@ def _compare_pair_inclusion(self, op, needle, haystack):
988992
step = self.append(ir.GetAttr(haystack, "step"))
989993
after_start = self.append(ir.Compare(ast.GtE(loc=None), needle, start))
990994
after_stop = self.append(ir.Compare(ast.Lt(loc=None), needle, stop))
991-
from_start = self.append(ir.BinaryOp(ast.Sub(loc=None), needle, start))
992-
mod_step = self.append(ir.BinaryOp(ast.Mod(loc=None), from_start, step))
995+
from_start = self.append(ir.Arith(ast.Sub(loc=None), needle, start))
996+
mod_step = self.append(ir.Arith(ast.Mod(loc=None), from_start, step))
993997
on_step = self.append(ir.Compare(ast.Eq(loc=None), mod_step,
994998
ir.Constant(0, mod_step.type)))
995999
result = self.append(ir.Select(after_start, after_stop,
@@ -1008,8 +1012,8 @@ def body_gen(index):
10081012

10091013
loop_body2 = self.add_block()
10101014
self.current_block = loop_body2
1011-
return self.append(ir.BinaryOp(ast.Add(loc=None), index,
1012-
ir.Constant(1, length.type)))
1015+
return self.append(ir.Arith(ast.Add(loc=None), index,
1016+
ir.Constant(1, length.type)))
10131017
loop_head, loop_body, loop_tail = \
10141018
self._make_loop(ir.Constant(0, length.type),
10151019
lambda index: self.append(ir.Compare(ast.Lt(loc=None), index, length)),
@@ -1117,8 +1121,8 @@ def visit_builtin_call(self, node):
11171121
def body_gen(index):
11181122
elt = self._iterable_get(arg, index)
11191123
self.append(ir.SetElem(result, index, elt))
1120-
return self.append(ir.BinaryOp(ast.Add(loc=None), index,
1121-
ir.Constant(1, length.type)))
1124+
return self.append(ir.Arith(ast.Add(loc=None), index,
1125+
ir.Constant(1, length.type)))
11221126
self._make_loop(ir.Constant(0, length.type),
11231127
lambda index: self.append(ir.Compare(ast.Lt(loc=None), index, length)),
11241128
body_gen)

0 commit comments

Comments
 (0)
Please sign in to comment.