Skip to content
This repository has been archived by the owner on May 18, 2019. It is now read-only.

Commit

Permalink
[NF] Various fixes.
Browse files Browse the repository at this point in the history
- Remove the type in Call.CallAttributes, the call itself already
  contains the same type.
- Improve subscripting of calls such as pre(), where the argument can
  be subscripted instead of the whole call expression.
- Fix Type.copyDims so that it doesn't create an array with no
  dimensions if the source type is a scalar type.
- Add simplification 'if ... then x else x' => 'x'.

Belonging to [master]:
  - #2819
  - OpenModelica/OpenModelica-testsuite#1086
  • Loading branch information
perost authored and OpenModelica-Hudson committed Dec 7, 2018
1 parent c7c9c71 commit cb2f4d4
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 11 deletions.
7 changes: 3 additions & 4 deletions Compiler/NFFrontEnd/NFCall.mo
Expand Up @@ -70,7 +70,6 @@ import Operator = NFOperator;
public
uniontype CallAttributes
record CALL_ATTR
Type ty "The type of the return value, if several return values this is undefined";
Boolean tuple_ "tuple" ;
Boolean builtin "builtin Function call" ;
Boolean isImpure "if the function has prefix *impure* is true, else false";
Expand All @@ -81,9 +80,10 @@ public

function toDAE
input CallAttributes attr;
input Type returnType;
output DAE.CallAttributes fattr;
algorithm
fattr := DAE.CALL_ATTR(Type.toDAE(attr.ty), attr.tuple_, attr.builtin,
fattr := DAE.CALL_ATTR(Type.toDAE(returnType), attr.tuple_, attr.builtin,
attr.isImpure, attr.isFunctionPointerCall, attr.inlineType, attr.tailCall);
end toDAE;
end CallAttributes;
Expand Down Expand Up @@ -297,7 +297,6 @@ uniontype Call
CallAttributes ca;
algorithm
ca := CallAttributes.CALL_ATTR(
returnType,
Type.isTuple(returnType),
Function.isBuiltin(fn),
Function.isImpure(fn),
Expand Down Expand Up @@ -685,7 +684,7 @@ uniontype Call
then DAE.CALL(
Function.nameConsiderBuiltin(call.fn),
list(Expression.toDAE(e) for e in call.arguments),
CallAttributes.toDAE(call.attributes));
CallAttributes.toDAE(call.attributes, call.ty));

case TYPED_ARRAY_CONSTRUCTOR()
algorithm
Expand Down
1 change: 0 additions & 1 deletion Compiler/NFFrontEnd/NFExpandExp.mo
Expand Up @@ -353,7 +353,6 @@ public
algorithm
Call.TYPED_CALL(fn, ty, var, {arg}, attr) := call;
ty := Type.arrayElementType(ty);
attr.ty := ty;

(arg, true) := expand(arg);
outExp := expandBuiltinGeneric2(arg, fn, ty, var, attr);
Expand Down
33 changes: 33 additions & 0 deletions Compiler/NFFrontEnd/NFExpression.mo
Expand Up @@ -932,6 +932,9 @@ public
case CALL(call = Call.TYPED_ARRAY_CONSTRUCTOR())
then applySubscriptArrayConstructor(subscript, exp.call, restSubscripts);

case CALL()
then applySubscriptCall(subscript, exp, restSubscripts);

case IF() then applySubscriptIf(subscript, exp, restSubscripts);

else makeSubscriptedExp(subscript :: restSubscripts, exp);
Expand Down Expand Up @@ -1163,6 +1166,36 @@ public
end match;
end applyIndexSubscriptRange2;

function applySubscriptCall
input Subscript subscript;
input Expression exp;
input list<Subscript> restSubscripts;
output Expression outExp;
protected
Call call;
algorithm
CALL(call = call) := exp;

outExp := match call
local
Expression arg;
Type ty;

case Call.TYPED_CALL(arguments = {arg})
guard Function.Function.isSubscriptableBuiltin(call.fn)
algorithm
arg := applySubscript(subscript, arg, restSubscripts);
ty := Type.copyDims(typeOf(arg), call.ty);
then
CALL(Call.TYPED_CALL(call.fn, ty, call.var, {arg}, call.attributes));

case Call.TYPED_ARRAY_CONSTRUCTOR()
then applySubscriptArrayConstructor(subscript, call, restSubscripts);

else makeSubscriptedExp(subscript :: restSubscripts, exp);
end match;
end applySubscriptCall;

function applySubscriptArrayConstructor
input Subscript subscript;
input Call call;
Expand Down
17 changes: 17 additions & 0 deletions Compiler/NFFrontEnd/NFFunction.mo
Expand Up @@ -1475,6 +1475,23 @@ uniontype Function
end if;
end isSpecialBuiltin;

function isSubscriptableBuiltin
input Function fn;
output Boolean scalarBuiltin;
protected
algorithm
if not isBuiltin(fn) then
scalarBuiltin := false;
else
scalarBuiltin := match Absyn.pathFirstIdent(Function.nameConsiderBuiltin(fn))
case "change" then true;
case "der" then true;
case "pre" then true;
else false;
end match;
end if;
end isSubscriptableBuiltin;

function isImpure
input Function fn;
output Boolean isImpure = fn.attributes.isImpure;
Expand Down
8 changes: 7 additions & 1 deletion Compiler/NFFrontEnd/NFSimplifyExp.mo
Expand Up @@ -588,7 +588,13 @@ algorithm
case Expression.BOOLEAN()
then simplify(if cond.value then tb else fb);

else Expression.IF(cond, simplify(tb), simplify(fb));
else
algorithm
tb := simplify(tb);
fb := simplify(fb);
then
if Expression.isEqual(tb, fb) then tb else Expression.IF(cond, tb, fb);

end match;
end simplifyIf;

Expand Down
14 changes: 9 additions & 5 deletions Compiler/NFFrontEnd/NFType.mo
Expand Up @@ -515,12 +515,16 @@ public
input Type dstType;
output Type ty;
algorithm
ty := match dstType
case ARRAY()
then ARRAY(dstType.elementType, arrayDims(srcType));
if listEmpty(arrayDims(srcType)) then
ty := arrayElementType(dstType);
else
ty := match dstType
case ARRAY()
then ARRAY(dstType.elementType, arrayDims(srcType));

else ARRAY(dstType, arrayDims(srcType));
end match;
else ARRAY(dstType, arrayDims(srcType));
end match;
end if;
end copyDims;

function nthDimension
Expand Down

0 comments on commit cb2f4d4

Please sign in to comment.