Commit 722f1b37 authored by Maxime Perrotin's avatar Maxime Perrotin
Browse files

Refactor binary operator in ada backend

parent 0c503cdc
...@@ -1374,8 +1374,7 @@ def _task_forloop(task, **kwargs): ...@@ -1374,8 +1374,7 @@ def _task_forloop(task, **kwargs):
start_stmt, start_str, start_local = \ start_stmt, start_str, start_local = \
expression(loop['range']['start']) expression(loop['range']['start'])
#if basic.kind == "Integer32Type": if not is_numeric(start_str):
# start_str = u"AsN1InT({})".format(start_str)
start_str = u"Integer ({})".format(start_str) start_str = u"Integer ({})".format(start_str)
local_decl.extend(start_local) local_decl.extend(start_local)
...@@ -1387,8 +1386,7 @@ def _task_forloop(task, **kwargs): ...@@ -1387,8 +1386,7 @@ def _task_forloop(task, **kwargs):
basic = find_basic_type(loop['range']['stop'].exprType) basic = find_basic_type(loop['range']['stop'].exprType)
stop_stmt, stop_str, stop_local = expression(loop['range']['stop']) stop_stmt, stop_str, stop_local = expression(loop['range']['stop'])
#if basic.kind == "Integer32Type": if not is_numeric(stop_str):
# stop_str = u"Asn1INt({})".format(stop_str)
stop_str = u"Integer ({})".format(stop_str) stop_str = u"Integer ({})".format(stop_str)
local_decl.extend(stop_local) local_decl.extend(stop_local)
...@@ -1769,9 +1767,6 @@ def _basic_operators(expr): ...@@ -1769,9 +1767,6 @@ def _basic_operators(expr):
left_stmts, left_str, left_local = expression(expr.left) left_stmts, left_str, left_local = expression(expr.left)
right_stmts, right_str, right_local = expression(expr.right) right_stmts, right_str, right_local = expression(expr.right)
if isinstance (expr, ogAST.ExprMod):
bt = find_basic_type(expr.exprType)
# Check if either side is a literal number # Check if either side is a literal number
right_is_numeric = is_numeric(right_str) right_is_numeric = is_numeric(right_str)
left_is_numeric = is_numeric(left_str) left_is_numeric = is_numeric(left_str)
...@@ -1779,22 +1774,32 @@ def _basic_operators(expr): ...@@ -1779,22 +1774,32 @@ def _basic_operators(expr):
lbty = find_basic_type(expr.left.exprType) lbty = find_basic_type(expr.left.exprType)
rbty = find_basic_type(expr.right.exprType) rbty = find_basic_type(expr.right.exprType)
if rbty.kind != lbty.kind and 'Integer32Type' in (lbty.kind, rbty.kind): if left_is_numeric != right_is_numeric or rbty.kind == lbty.kind:
# One of the sides is an int32 (eg a for loop iterator), must cast # No cast is needed if:
# it to the left type which is either signed or unsigned 64 bits # - one of the two sides only is a literal : no cast is needed
if lbty.kind == 'IntegerType' and not right_is_numeric: # - or if the the basic types are identical
right_str = u'{cast}({val})'.format(cast=type_name ada_string = u'({left} {op} {right})'.format(left=left_str,
(expr.left.exprType), op=expr.operand,
val=right_str) right=right_str)
elif not left_is_numeric:
left_str = u'Asn1INT({})'.format(left_str)
if left_is_numeric == right_is_numeric == True: elif left_is_numeric == right_is_numeric == True:
# Both sides are literals : compute the result on the fly
ada_string = u"{}".format(eval(u"{left} {op} {right}" ada_string = u"{}".format(eval(u"{left} {op} {right}"
.format(left=left_str, .format(left=left_str,
op=expr.operand, op=expr.operand,
right=right_str))) right=right_str)))
elif rbty.kind != lbty.kind:
# Basic types are different (one is an Integer32, eg. loop iterator)
# => We must cast it to the type of the other side
if lbty.kind == 'Integer32Type':
left_str = u'{cast}({val})'.format(cast=type_name
(expr.right.exprType),
val=left_str)
else: else:
right_str = u'{cast}({val})'.format(cast=type_name
(expr.left.exprType),
val=right_str)
ada_string = u'({left} {op} {right})'.format(left=left_str, ada_string = u'({left} {op} {right})'.format(left=left_str,
op=expr.operand, op=expr.operand,
right=right_str) right=right_str)
...@@ -1910,14 +1915,6 @@ def _assign_expression(expr): ...@@ -1910,14 +1915,6 @@ def _assign_expression(expr):
else: else:
res = right_str res = right_str
# if float(basic_right.Min) >= 0 and float (basic_left.Min) < 0:
# res = "Asn1Int({})".format(right_str)
# # Modulo expressions: if left min range is >= 0, cast right to uint
# elif isinstance(expr.right,
# ogAST.ExprMod) and float(basic_left.Min) >= 0:
# res = u'Asn1UInt({})'.format(right_str)
# else:
# res = right_str
strings.append(u"{} := {};".format(left_str, res)) strings.append(u"{} := {};".format(left_str, res))
else: else:
strings.append(u"{} := {};".format(left_str, right_str)) strings.append(u"{} := {};".format(left_str, right_str))
...@@ -2866,7 +2863,7 @@ def type_name(a_type, use_prefix=True): ...@@ -2866,7 +2863,7 @@ def type_name(a_type, use_prefix=True):
elif a_type.kind.endswith('StringType'): elif a_type.kind.endswith('StringType'):
return u'String' return u'String'
elif a_type.kind == 'ChoiceEnumeratedType': elif a_type.kind == 'ChoiceEnumeratedType':
return u'Asn1INT' return u'Asn1InT'
elif a_type.kind == 'StateEnumeratedType': elif a_type.kind == 'StateEnumeratedType':
return u'' return u''
elif a_type.kind == 'EnumeratedType': elif a_type.kind == 'EnumeratedType':
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment