Skip to content

Commit

Permalink
Implement sum()
Browse files Browse the repository at this point in the history
  • Loading branch information
certik committed Nov 29, 2017
1 parent 33ee790 commit 2c9438e
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 3 deletions.
5 changes: 5 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ python grammar/asdl_py.py
# Generate a parse tree from fortran.g4
antlr4="java org.antlr.v4.Tool"
(cd grammar; $antlr4 -Dlanguage=Python3 -no-listener -visitor fortran.g4 -o ../liblfort/parser)

# Compile LFort runtime library
CFLAGS="-Wall -g"
gcc $CFLAGS -o lfort_intrinsics.o -c lfort_intrinsics.c
ar rcs liblfort.a lfort_intrinsics.o
9 changes: 9 additions & 0 deletions lfort_intrinsics.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
double _lfort_sum(int n, double *v)
{
int i, r;
r = 0;
for (i=0; i < n; i++) {
r += v[i];
}
return r;
}
17 changes: 16 additions & 1 deletion liblfort/codegen/gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ def visit_Program(self, node):
self.builder = ir.IRBuilder(block)

for ssym in self.symbol_table:
if ssym in ["abs", "sqrt", "log", "sum"]: continue
sym = self.symbol_table[ssym]
type_f = sym["type"]
if isinstance(type_f, Array):
Expand All @@ -155,6 +156,10 @@ def visit_Program(self, node):
'llvm.sqrt', [ir.DoubleType()])
self.symbol_table["log"]["fn"] = self.module.declare_intrinsic(
'llvm.log', [ir.DoubleType()])
fn_type = ir.FunctionType(ir.DoubleType(),
[ir.IntType(64), ir.DoubleType().as_pointer()])
fn_sum = ir.Function(self.module, fn_type, name="_lfort_sum")
self.symbol_table["sum"]["fn"] = fn_sum

self.visit_sequence(node.contains)
self.visit_sequence(node.body)
Expand Down Expand Up @@ -276,7 +281,17 @@ def visit_FuncCallOrArray(self, node):
sym = self.symbol_table[node.func]
fn = sym["fn"]
arg = self.visit(node.args[0])
return self.builder.call(fn, [arg])
if sym["name"] == "sum":
# FIXME: for now we assume an array was passed in:
arg = self.symbol_table[node.args[0].id]
addr = self.builder.gep(arg["ptr"],
[ir.Constant(ir.IntType(64), 0),
ir.Constant(ir.IntType(64), 0)])
# FIXME: pass in the length of the array
return self.builder.call(fn, [ir.Constant(ir.IntType(64), 3),
addr])
else:
return self.builder.call(fn, [arg])

def visit_If(self, node):
cond = self.visit(node.test)
Expand Down
2 changes: 1 addition & 1 deletion liblfort/liblfort.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,6 @@ def main():
os.system("ld -o %s %s %s" % (outfile, objfile, LDFLAGS))
else:
# Invoke a C compiler to do the linking
os.system("cc -o %s %s -lm" % (outfile, objfile))
os.system("cc -o %s %s -L. -llfort -lm" % (outfile, objfile))
if objfile == "tmp_object_file.o":
os.system("rm %s" % objfile)
1 change: 1 addition & 0 deletions liblfort/semantic/analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def __init__(self):
"abs": {"name": "abs", "type": Real()},
"sqrt": {"name": "sqrt", "type": Real()},
"log": {"name": "log", "type": Real()},
"sum": {"name": "sum", "type": Real()},
}

def visit_Declaration(self, node):
Expand Down
4 changes: 3 additions & 1 deletion tests/arrays_02.f90
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
program arrays_02
implicit none
integer, parameter :: dp
real(dp) :: a(3)
real(dp) :: a(3), b
a(1) = 3._dp
a(2) = 2._dp
a(3) = 1._dp
b = sum(a)
if (abs(b-6._dp) > 1e-12_dp) error stop
end

0 comments on commit 2c9438e

Please sign in to comment.