From 74489a68095f5acbe3f8df3cee5c8832eb868a0b Mon Sep 17 00:00:00 2001 From: Francis Russell Date: Thu, 3 May 2012 17:31:51 +0100 Subject: [PATCH] Add min and max intrinsics to code generator. --- src/ofc/codegen/FortranGenerator.scala | 8 +++++++- src/ofc/codegen/Intrinsic.scala | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 src/ofc/codegen/Intrinsic.scala diff --git a/src/ofc/codegen/FortranGenerator.scala b/src/ofc/codegen/FortranGenerator.scala index 9f0ecd4..059e668 100644 --- a/src/ofc/codegen/FortranGenerator.scala +++ b/src/ofc/codegen/FortranGenerator.scala @@ -176,6 +176,7 @@ class FortranGenerator { case (c: NumericComparison[_]) => buildNumericComparison(c) case (c: NumericOperator[_]) => buildNumericOperator(c) case (c: Conversion[_,_]) => buildConversion(c) + case (i: Intrinsic[_]) => buildIntrinsic(i) case x => throw new UnimplementedException("Unknown expression type in FORTRAN generator: " + x.toString) } } @@ -222,7 +223,12 @@ class FortranGenerator { case (_: ComplexType) => ExpHolder(maxPrec, "cmplx(%s)".format(buildExpression(c.getExpression))) case _ => throw new UnimplementedException("Fortran generator cannot handle conversion.") } - + + private def buildIntrinsic(i: Intrinsic[_]) : ExpHolder = i match { + case (m: Min[_]) => ExpHolder(maxPrec, "min(%s, %s)".format(buildExpression(m.getLeft), buildExpression(m.getRight))) + case (m: Max[_]) => ExpHolder(maxPrec, "max(%s, %s)".format(buildExpression(m.getLeft), buildExpression(m.getRight))) + case _ => throw new UnimplementedException("Unknown intrinsic in Fortran generator: "+i) + } private def buildNumericComparison(c: NumericComparison[_]) : ExpHolder = buildBinaryOperation(getBinaryOpInfo(c.getOperation), buildExpression(c.getLeft), buildExpression(c.getRight)) diff --git a/src/ofc/codegen/Intrinsic.scala b/src/ofc/codegen/Intrinsic.scala new file mode 100644 index 0000000..a73c7ea --- /dev/null +++ b/src/ofc/codegen/Intrinsic.scala @@ -0,0 +1,24 @@ +package ofc.codegen +import ofc.LogicError + +abstract class Intrinsic[T <: Type] extends Expression[T] + +class Min[T <: Type](a: Expression[T], b: Expression[T])(implicit isNumeric: HasProperty[T, Numeric]) extends Intrinsic[T] { + if (a.getType != b.getType) + throw new LogicError("Parameters to min must have matching types.") + + def getType = a.getType + def foreach[U](f: Expression[_] => U) = List(a, b).foreach(f) + def getLeft = a + def getRight = b +} + +class Max[T <: Type](a: Expression[T], b: Expression[T])(implicit isNumeric: HasProperty[T, Numeric]) extends Intrinsic[T] { + if (a.getType != b.getType) + throw new LogicError("Parameters to max must have matching types.") + + def getType = a.getType + def foreach[U](f: Expression[_] => U) = List(a, b).foreach(f) + def getLeft = a + def getRight = b +} -- 2.47.3