Commit e92ac77a authored by dbarbera's avatar dbarbera
Browse files

Added built-in abs generation

parent ae05c28b
...@@ -76,6 +76,12 @@ class GlobalState(): ...@@ -76,6 +76,12 @@ class GlobalState():
[self.double] [self.double]
) )
self.funcs['fabs'] = core.Function.intrinsic(
self.module,
core.INTR_FABS,
[self.double]
)
class StructType(): class StructType():
def __init__(self, name, field_names, field_types): def __init__(self, name, field_names, field_types):
...@@ -548,7 +554,14 @@ def generate_present(params): ...@@ -548,7 +554,14 @@ def generate_present(params):
def generate_abs(params): def generate_abs(params):
''' Generate the code for the built-in abs operation''' ''' Generate the code for the built-in abs operation'''
raise NotImplementedError left_val = expression(params[0])
if left_val.type.kind == core.TYPE_INTEGER:
left_conv = g.builder.sitofp(left_val, g.double)
res_val = g.builder.call(g.funcs['fabs'], [left_conv])
return g.builder.fptosi(res_val, g.i32)
else:
return g.builder.call(g.funcs['fabs'], [left_val])
def generate_fix(params): def generate_fix(params):
......
...@@ -144,7 +144,23 @@ COMMENT '27'; ...@@ -144,7 +144,23 @@ COMMENT '27';
CALL writeln(power(f, 3)) CALL writeln(power(f, 3))
/* CIF COMMENT (70, 1457), (70, 35) */ /* CIF COMMENT (70, 1457), (70, 35) */
COMMENT '27.0'; COMMENT '27.0';
/* CIF NEXTSTATE (-85, 1507), (100, 50) */ /* CIF PROCEDURECALL (-100, 1507), (130, 35) */
CALL writeln(abs(1))
/* CIF COMMENT (50, 1507), (70, 35) */
COMMENT '1';
/* CIF PROCEDURECALL (-102, 1557), (134, 35) */
CALL writeln(abs(-1))
/* CIF COMMENT (52, 1557), (70, 35) */
COMMENT '1';
/* CIF PROCEDURECALL (-105, 1607), (140, 35) */
CALL writeln(abs(1.0))
/* CIF COMMENT (55, 1607), (70, 35) */
COMMENT '1.0';
/* CIF PROCEDURECALL (-107, 1657), (144, 35) */
CALL writeln(abs(-1.0))
/* CIF COMMENT (57, 1657), (70, 35) */
COMMENT '1.0';
/* CIF NEXTSTATE (-85, 1707), (100, 50) */
NEXTSTATE Wait; NEXTSTATE Wait;
ENDSTATE; ENDSTATE;
ENDPROCESS basic; ENDPROCESS basic;
\ No newline at end of file
...@@ -29,3 +29,7 @@ TRUE ...@@ -29,3 +29,7 @@ TRUE
TRUE TRUE
27 27
2.70000000000000E+01 2.70000000000000E+01
1
1
1.00000000000000E+00
1.00000000000000E+00
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