Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Carry allow_eq flag for Bools through various passes #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion teg/ir/instr/binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ def __init__(self, output, input1, input2):
super(IR_CompareLT, self).__init__(output=output, input1=input1, input2=input2)


class IR_CompareGT(IR_Binary):
class IR_CompareLTE(IR_Binary):
def __init__(self, output, input1, input2):
super(IR_CompareLTE, self).__init__(output=output, input1=input1, input2=input2)


class IR_CompareGT(IR_Binary): # NOTE: This class is never instantiated?
def __init__(self, output, input1, input2):
super(IR_CompareGT, self).__init__(output=output, input1=input1, input2=input2)
7 changes: 7 additions & 0 deletions teg/ir/passes/to_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

IR_Binary,
IR_CompareLT,
IR_CompareLTE,
IR_CompareGT,
IR_LAnd,
IR_LOr,
Expand Down Expand Up @@ -301,6 +302,12 @@ def __tegpass_c__(self, name_ctx, **kwargs):
return self.__tegpass_c_binary__(name_ctx, op='<', **kwargs)


@overloads(IR_CompareLTE)
class CPass_CompareLTE:
def __tegpass_c__(self, name_ctx, **kwargs):
return self.__tegpass_c_binary__(name_ctx, op='<=', **kwargs)


@overloads(IR_Add)
class CPass_Add:
def __tegpass_c__(self, name_ctx, **kwargs):
Expand Down
15 changes: 15 additions & 0 deletions teg/ir/passes/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
IR_Binary,
IR_IfElse,
IR_CompareLT,
IR_CompareLTE,
IR_CompareGT,
IR_Variable,
IR_Literal,
Expand Down Expand Up @@ -183,6 +184,20 @@ def __tegpass_typing__(self):
self.output.set_irtype(IR_Type(ctype=Types.BOOL, size=left_type.size))


@overloads(IR_CompareLTE)
class TypingPass_CompareLTE:
def __tegpass_typing__(self):
left_symbol, right_symbol = self.inputs
left_type = left_symbol.irtype()
right_type = right_symbol.irtype()

assert left_type.ctype == right_type.ctype, 'Binary operands have incompatible types'
assert ((left_type.size == right_type.size) or
(left_type.size == 1) or (right_type.size == 1)), 'Binary operands have incompatible sizes'

self.output.set_irtype(IR_Type(ctype=Types.BOOL, size=left_type.size))


@overloads(IR_UnaryMath)
class TypingPass_UnaryMath:
def __tegpass_typing__(self):
Expand Down
4 changes: 2 additions & 2 deletions teg/lang/operator_overloads.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,10 +391,10 @@ def __eq__(self, other):
class BoolOverloads:

def __str__(self):
return f'{self.left_expr} < {self.right_expr}'
return f'{self.left_expr} <{"=" if self.allow_eq else ""} {self.right_expr}'

def __repr__(self):
return f'Bool({repr(self.left_expr)}, {repr(self.right_expr)})'
return f'Bool({repr(self.left_expr)}, {repr(self.right_expr)}, {repr(self.allow_eq)})'


@overloads(And)
Expand Down
4 changes: 3 additions & 1 deletion teg/passes/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
IR_Integrate,
IR_Pack,
IR_CompareLT,
IR_CompareLTE,
IR_LAnd,
IR_LOr,
IR_Assign
Expand Down Expand Up @@ -226,7 +227,8 @@ def _to_ir(expr: ITeg, symbols: Dict[str, IR_Symbol]) -> (List[IR_Instruction],
elif isinstance(expr, Or):
code = [*ir_left, *ir_right, IR_LOr(out_var, left_var, right_var)]
elif isinstance(expr, Bool):
code = [*ir_left, *ir_right, IR_CompareLT(out_var, left_var, right_var)]
ir_class = IR_CompareLTE if expr.allow_eq else IR_CompareLT
code = [*ir_left, *ir_right, ir_class(out_var, left_var, right_var)]

return code, out_var, {**left_symbols, **right_symbols}

Expand Down
8 changes: 5 additions & 3 deletions teg/passes/simplify.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,10 @@ def simplify(expr: ITeg) -> ITeg:
elif isinstance(expr, Bool):
left_expr, right_expr = simplify(expr.left_expr), simplify(expr.right_expr)
if isinstance(left_expr, Const) and isinstance(right_expr, Const):
return false if evaluate(Bool(left_expr, right_expr)) == 0.0 else true
return false if evaluate(Bool(left_expr, right_expr, allow_eq=expr.allow_eq)) == 0.0 else true
return Bool(left_expr, right_expr)
# NOTE: having the allow_eq flag be carried through appears to break evals of certain complicated expressions
# return Bool(left_expr, right_expr, allow_eq=expr.allow_eq)

elif isinstance(expr, And):
left_expr, right_expr = simplify(expr.left_expr), simplify(expr.right_expr)
Expand All @@ -275,7 +277,7 @@ def simplify(expr: ITeg) -> ITeg:
if left_expr == false or right_expr == false:
return false
if isinstance(left_expr, Const) and isinstance(right_expr, Const):
return Const(evaluate(And(simple1, simple2)))
return Const(evaluate(And(simple1, simple2))) # TODO: These variables are never defined; is this tested?
return And(left_expr, right_expr)

elif isinstance(expr, Or):
Expand All @@ -287,7 +289,7 @@ def simplify(expr: ITeg) -> ITeg:
if left_expr == true or right_expr == true:
return true
if isinstance(left_expr, Const) and isinstance(right_expr, Const):
return Const(evaluate(Or(simple1, simple2)))
return Const(evaluate(Or(simple1, simple2))) # TODO: These variables are never defined; is this tested?
return Or(left_expr, right_expr)

else:
Expand Down