From a40153357464fe67107f1c08ec5b1ddb628ead26 Mon Sep 17 00:00:00 2001 From: Francis Russell Date: Tue, 1 May 2012 06:27:54 +0100 Subject: [PATCH] Add array allocation/deallocation statements. --- src/ofc/codegen/AllocateStatement.scala | 15 +++++++++++++++ src/ofc/codegen/DeallocateStatement.scala | 11 +++++++++++ src/ofc/codegen/FortranGenerator.scala | 10 ++++++++++ 3 files changed, 36 insertions(+) create mode 100644 src/ofc/codegen/AllocateStatement.scala create mode 100644 src/ofc/codegen/DeallocateStatement.scala diff --git a/src/ofc/codegen/AllocateStatement.scala b/src/ofc/codegen/AllocateStatement.scala new file mode 100644 index 0000000..b99840a --- /dev/null +++ b/src/ofc/codegen/AllocateStatement.scala @@ -0,0 +1,15 @@ +package ofc.codegen +import ofc.LogicError + +class AllocateStatement(array: Expression[_], size: Seq[Expression[IntType]]) extends Statement { + val arrayType = array.getType match { + case (at: ArrayType[_]) => at + case _ => throw new LogicError("Can only allocate an array expression.") + } + + if (size.length != arrayType.getRank) + throw new LogicError("Incorrect rank for array allocation.") + + def getArray : Expression[_] = array + def getSize : Seq[Expression[IntType]] = size +} diff --git a/src/ofc/codegen/DeallocateStatement.scala b/src/ofc/codegen/DeallocateStatement.scala new file mode 100644 index 0000000..41efd47 --- /dev/null +++ b/src/ofc/codegen/DeallocateStatement.scala @@ -0,0 +1,11 @@ +package ofc.codegen +import ofc.LogicError + +class DeallocateStatement(array: Expression[_]) extends Statement { + array.getType match { + case (_: ArrayType[_]) => () + case _ => throw new LogicError("Can only deallocate an array expression.") + } + + def getArray : Expression[_] = array +} diff --git a/src/ofc/codegen/FortranGenerator.scala b/src/ofc/codegen/FortranGenerator.scala index f424243..080171d 100644 --- a/src/ofc/codegen/FortranGenerator.scala +++ b/src/ofc/codegen/FortranGenerator.scala @@ -143,6 +143,8 @@ class FortranGenerator { case (x : ForLoop) => processForLoop(x) case (a : Assignment) => processAssignment(a) case (i : IfStatement) => processIf(i) + case (a : AllocateStatement) => processAllocate(a) + case (d : DeallocateStatement) => processDeallocate(d) case x => throw new UnimplementedException("Unknown statement type in FORTRAN generator: " + x.toString) } } @@ -254,6 +256,14 @@ class FortranGenerator { addLine("endif") } + private def processAllocate(allocate: AllocateStatement) { + addLine("allocate(%s(%s))".format(buildExpression(allocate.getArray), allocate.getSize.map(buildExpression(_)).mkString(","))) + } + + private def processDeallocate(deallocate: DeallocateStatement) { + addLine("deallocate(%s)".format(buildExpression(deallocate.getArray))) + } + private def addLine(line: String) { buffer += " "*indentLevel + line } -- 2.47.3