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

Commit 81ba868

Browse files
perostOpenModelica-Hudson
authored andcommitted
[NF] Various fixes.
- Fix ExpressionSimplify.evalCat so that it doesn't reverse the arguments when dim == 1. - Disabled constant evaluation of function call arguments during typing. - Added constant evaluation of ranges. Belonging to [master]: - #2400
1 parent 3aae2a0 commit 81ba868

File tree

6 files changed

+111
-22
lines changed

6 files changed

+111
-22
lines changed

Compiler/FrontEnd/ExpressionSimplify.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1636,7 +1636,7 @@ algorithm
16361636
true := dim >= 1;
16371637
false := listEmpty(exps);
16381638
if 1 == dim then
1639-
outExps := listAppend(getArrayContents(e) for e in exps);
1639+
outExps := listAppend(getArrayContents(e) for e in listReverse(exps));
16401640
outDims := {listLength(outExps)};
16411641
return;
16421642
end if;

Compiler/NFFrontEnd/NFCall.mo

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -589,26 +589,31 @@ uniontype Call
589589
protected
590590
Function func;
591591
list<Expression> args;
592-
list<Type> arg_ty;
593-
list<Variability> arg_var;
594592
CallAttributes ca;
595593
list<TypedArg> typed_args;
596594
MatchedFunction matchedFunc;
597595
InstNode scope;
598-
Variability var;
596+
Variability var, arg_var;
599597
Type ty;
598+
Expression arg_exp;
600599
algorithm
601600
ARG_TYPED_CALL(call_scope = scope) := call;
602601
matchedFunc := checkMatchingFunctions(call,info);
603602

604603
func := matchedFunc.func;
604+
// Don't evaluate structural parameters for external functions, the code generation can't handle it in
605+
// some cases (see bug #4904). For constants we'll get issues no matter if we evaluate them or not,
606+
// but evaluating them will probably cause the last amount of issues.
605607
typed_args := matchedFunc.args;
606-
args := list(Util.tuple31(a) for a in typed_args);
607608

609+
args := {};
608610
var := Variability.CONSTANT;
609611
for a in typed_args loop
610-
var := Prefixes.variabilityMax(var, Util.tuple33(a));
612+
(arg_exp, _, arg_var) := a;
613+
args := arg_exp :: args;
614+
var := Prefixes.variabilityMax(var, arg_var);
611615
end for;
616+
args := listReverseInPlace(args);
612617

613618
ty := Function.returnType(func);
614619

@@ -845,7 +850,7 @@ uniontype Call
845850
algorithm
846851
typedArgs := {};
847852
for arg in call.arguments loop
848-
(arg, arg_ty, arg_var) := Typing.typeExp(arg, origin, info);
853+
(arg, arg_ty, arg_var) := Typing.typeExp(arg, origin, info, replaceConstants = false);
849854
typedArgs := (arg, arg_ty, arg_var) :: typedArgs;
850855
end for;
851856

@@ -854,7 +859,7 @@ uniontype Call
854859
typedNamedArgs := {};
855860
for narg in call.named_args loop
856861
(name,arg) := narg;
857-
(arg, arg_ty, arg_var) := Typing.typeExp(arg, origin, info);
862+
(arg, arg_ty, arg_var) := Typing.typeExp(arg, origin, info, replaceConstants = false);
858863
typedNamedArgs := (name, arg, arg_ty, arg_var) :: typedNamedArgs;
859864
end for;
860865

@@ -1521,8 +1526,7 @@ protected
15211526
list<TypedArg> args;
15221527
TypedArg start,interval;
15231528
algorithm
1524-
argtycall := typeNormalCall(call, origin, info);
1525-
argtycall := matchTypedNormalCall(argtycall, origin, info);
1529+
argtycall := typeMatchNormalCall(call, origin, info);
15261530
ty := getType(argtycall);
15271531
callExp := Expression.CALL(unboxArgs(argtycall));
15281532
end typeDiscreteCall;
@@ -1762,8 +1766,7 @@ protected
17621766
protected
17631767
Call argtycall;
17641768
algorithm
1765-
argtycall := typeNormalCall(call, origin, info);
1766-
argtycall := matchTypedNormalCall(argtycall, origin, info);
1769+
argtycall := typeMatchNormalCall(call, origin, info);
17671770
argtycall := unboxArgs(argtycall);
17681771
ty := getType(argtycall);
17691772
var := variability(argtycall);
@@ -1784,8 +1787,7 @@ protected
17841787
Call argtycall;
17851788
algorithm
17861789
// TODO: Rewrite this whole thing.
1787-
argtycall := typeNormalCall(call, origin, info);
1788-
argtycall := matchTypedNormalCall(argtycall, origin, info);
1790+
argtycall := typeMatchNormalCall(call, origin, info);
17891791
argtycall := unboxArgs(argtycall);
17901792
ty := getType(argtycall);
17911793
var := variability(argtycall);
@@ -2289,8 +2291,7 @@ protected
22892291
protected
22902292
Call argtycall;
22912293
algorithm
2292-
argtycall := typeNormalCall(call, origin, info);
2293-
argtycall := matchTypedNormalCall(argtycall, origin, info);
2294+
argtycall := typeMatchNormalCall(call, origin, info);
22942295

22952296
ty := getType(argtycall);
22962297
var := variability(argtycall);

Compiler/NFFrontEnd/NFCeval.mo

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,14 @@ algorithm
149149
then
150150
exp;
151151

152-
// Ranges could be evaluated into arrays, but that's less efficient in some
153-
// cases. So here we just evaluate the range's expressions, and let the
154-
// caller worry about vectorization.
155152
case Expression.RANGE()
156153
algorithm
157154
exp1 := evalExp(exp.start, target);
158155
oexp := evalExpOpt(exp.step, target);
159156
exp3 := evalExp(exp.stop, target);
160157
then
161-
Expression.RANGE(exp.ty, exp1, oexp, exp3);
158+
if EvalTarget.isRange(target) then
159+
Expression.RANGE(exp.ty, exp1, oexp, exp3) else evalRange(exp1, oexp, exp3);
162160

163161
case Expression.RECORD()
164162
algorithm
@@ -295,6 +293,78 @@ algorithm
295293
end if;
296294
end evalTypename;
297295

296+
function evalRange
297+
input Expression start;
298+
input Option<Expression> optStep;
299+
input Expression stop;
300+
output Expression exp;
301+
protected
302+
Expression step;
303+
list<Expression> expl;
304+
Type ty;
305+
list<String> literals;
306+
Integer istep;
307+
algorithm
308+
if isSome(optStep) then
309+
SOME(step) := optStep;
310+
311+
(ty, expl) := match (start, step, stop)
312+
case (Expression.INTEGER(), Expression.INTEGER(istep), Expression.INTEGER())
313+
algorithm
314+
// The compiler decided to randomly dislike using step.value here, hence istep.
315+
expl := list(Expression.INTEGER(i) for i in start.value:istep:stop.value);
316+
then
317+
(Type.INTEGER(), expl);
318+
319+
case (Expression.REAL(), Expression.REAL(), Expression.REAL())
320+
algorithm
321+
expl := list(Expression.REAL(r) for r in start.value:step.value:stop.value);
322+
then
323+
(Type.REAL(), expl);
324+
325+
else
326+
algorithm
327+
printWrongArgsError(getInstanceName(), {start, step, stop}, sourceInfo());
328+
then
329+
fail();
330+
end match;
331+
else
332+
(ty, expl) := match (start, stop)
333+
case (Expression.INTEGER(), Expression.INTEGER())
334+
algorithm
335+
expl := list(Expression.INTEGER(i) for i in start.value:stop.value);
336+
then
337+
(Type.INTEGER(), expl);
338+
339+
case (Expression.REAL(), Expression.REAL())
340+
algorithm
341+
expl := list(Expression.REAL(r) for r in start.value:stop.value);
342+
then
343+
(Type.REAL(), expl);
344+
345+
case (Expression.BOOLEAN(), Expression.BOOLEAN())
346+
algorithm
347+
expl := list(Expression.BOOLEAN(b) for b in start.value:stop.value);
348+
then
349+
(Type.BOOLEAN(), expl);
350+
351+
case (Expression.ENUM_LITERAL(ty = ty as Type.ENUMERATION()), Expression.ENUM_LITERAL())
352+
algorithm
353+
expl := list(Expression.ENUM_LITERAL(ty, listGet(ty.literals, i), i) for i in start.index:stop.index);
354+
then
355+
(ty, expl);
356+
357+
else
358+
algorithm
359+
printWrongArgsError(getInstanceName(), {start, stop}, sourceInfo());
360+
then
361+
fail();
362+
end match;
363+
end if;
364+
365+
exp := Expression.ARRAY(Type.ARRAY(ty, {Dimension.fromInteger(listLength(expl))}), expl);
366+
end evalRange;
367+
298368
function printFailedEvalError
299369
input String name;
300370
input Expression exp;

Compiler/NFFrontEnd/NFClass.mo

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,18 @@ uniontype Class
467467
output Boolean isFunction = Restriction.isFunction(restriction(cls));
468468
end isFunction;
469469

470+
function isExternalFunction
471+
input Class cls;
472+
output Boolean isExtFunc;
473+
algorithm
474+
isExtFunc := match cls
475+
case EXPANDED_DERIVED() then isExternalFunction(InstNode.getClass(cls.baseClass));
476+
case INSTANCED_CLASS(sections = Sections.EXTERNAL()) then true;
477+
case TYPED_DERIVED() then isExternalFunction(InstNode.getClass(cls.baseClass));
478+
else false;
479+
end match;
480+
end isExternalFunction;
481+
470482
function getPrefixes
471483
input Class cls;
472484
output Prefixes prefs;

Compiler/NFFrontEnd/NFFunction.mo

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,11 @@ uniontype Function
10791079
output Boolean isPointer = fn.attributes.isFunctionPointer;
10801080
end isFunctionPointer;
10811081

1082+
function isExternal
1083+
input Function fn;
1084+
output Boolean isExternal = Class.isExternalFunction(InstNode.getClass(fn.node));
1085+
end isExternal;
1086+
10821087
function inlineBuiltin
10831088
input Function fn;
10841089
output DAE.InlineType inlineType;

Compiler/NFFrontEnd/NFTyping.mo

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ algorithm
409409
() := match c
410410
case Component.ITERATOR(binding = Binding.UNTYPED_BINDING(), info = info)
411411
algorithm
412-
binding := typeBinding(c.binding, intBitOr(origin, ExpOrigin.ITERATION_RANGE));
412+
binding := typeBinding(c.binding, intBitOr(origin, ExpOrigin.ITERATION_RANGE), replaceConstants = false);
413413

414414
// If the iteration range is structural, it must be a parameter expression.
415415
if structural then
@@ -761,6 +761,7 @@ end typeComponentBinding;
761761
function typeBinding
762762
input output Binding binding;
763763
input ExpOrigin.Type origin;
764+
input Boolean replaceConstants = true;
764765
algorithm
765766
binding := match binding
766767
local
@@ -772,7 +773,7 @@ algorithm
772773
case Binding.UNTYPED_BINDING(bindingExp = exp)
773774
algorithm
774775
info := Binding.getInfo(binding);
775-
(exp, ty, var) := typeExp(exp, origin, info);
776+
(exp, ty, var) := typeExp(exp, origin, info, replaceConstants);
776777
then
777778
Binding.TYPED_BINDING(exp, ty, var, binding.origin, binding.isEach);
778779

0 commit comments

Comments
 (0)