Skip to content

Commit

Permalink
- Some minor optimizations.
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@24411 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
perost committed Feb 4, 2015
1 parent abdc521 commit 11df4e4
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 127 deletions.
52 changes: 18 additions & 34 deletions Compiler/FFrontEnd/FNode.mo
Expand Up @@ -2794,42 +2794,26 @@ public function avlTreeGet
input AvlTree inAvlTree;
input AvlKey inKey;
output AvlValue outValue;
algorithm
outValue := match (inAvlTree,inKey)
local
AvlKey rkey,key;
case (FCore.CAVLTREENODE(value = SOME(FCore.CAVLTREEVALUE(key=rkey))),key)
then avlTreeGet2(inAvlTree,stringCompare(key,rkey),key);
end match;
protected
AvlKey key;
AvlTree branch;
Integer res;
algorithm
FCore.CAVLTREENODE(value = SOME(FCore.CAVLTREEVALUE(key = key))) := inAvlTree;
res := stringCompare(inKey, key);

if res == 0 then
FCore.CAVLTREENODE(value = SOME(FCore.CAVLTREEVALUE(value = outValue))) := inAvlTree;
return;
elseif res == 1 then
FCore.CAVLTREENODE(right = SOME(branch)) := inAvlTree;
else
FCore.CAVLTREENODE(left = SOME(branch)) := inAvlTree;
end if;

outValue := avlTreeGet(branch, inKey);
end avlTreeGet;

protected function avlTreeGet2
"Get a value from the binary tree given a key."
input AvlTree inAvlTree;
input Integer keyComp "0=get value from current node, 1=search right subtree, -1=search left subtree";
input AvlKey inKey;
output AvlValue outValue;
algorithm
outValue := match (inAvlTree,keyComp,inKey)
local
AvlKey key;
AvlValue rval;
AvlTree left,right;

// hash func Search to the right
case (FCore.CAVLTREENODE(value = SOME(FCore.CAVLTREEVALUE(value=rval))),0,_)
then rval;

// search to the right
case (FCore.CAVLTREENODE(right = SOME(right)),1,key)
then avlTreeGet(right, key);

// search to the left
case (FCore.CAVLTREENODE(left = SOME(left)),-1,key)
then avlTreeGet(left, key);
end match;
end avlTreeGet2;

protected function getOptionStr "Retrieve the string from a string option.
If NONE() return empty string."
input Option<Type_a> inTypeAOption;
Expand Down
6 changes: 4 additions & 2 deletions Compiler/FrontEnd/InstExtends.mo
Expand Up @@ -167,7 +167,8 @@ algorithm
els = updateElementListVisibility(els, vis);

// build a ht with the constant elements from the extends scope
ht = getLocalIdentList(InstUtil.constantAndParameterEls(elsExtendsScope),HashTableStringToPath.emptyHashTable(),getLocalIdentElement);
ht = HashTableStringToPath.emptyHashTableSized(BaseHashTable.lowBucketSize);
ht = getLocalIdentList(InstUtil.constantAndParameterEls(elsExtendsScope),ht,getLocalIdentElement);
ht = getLocalIdentList(InstUtil.constantAndParameterEls(els),ht,getLocalIdentElement);
// fully qualify modifiers in extends in the extends environment!
(cache, emod) = fixModifications(cache, env, emod, ht);
Expand Down Expand Up @@ -197,7 +198,8 @@ algorithm

// print("Extended Elements Extends:\n" + InstUtil.printElementAndModList(List.map(compelts1, Util.tuple312)));

ht = getLocalIdentList(compelts1,HashTableStringToPath.emptyHashTable(),getLocalIdentElementTpl);
ht = HashTableStringToPath.emptyHashTableSized(BaseHashTable.lowBucketSize);
ht = getLocalIdentList(compelts1,ht,getLocalIdentElementTpl);
ht = getLocalIdentList(cdefelts,ht,getLocalIdentElement);
ht = getLocalIdentList(importelts,ht,getLocalIdentElement);

Expand Down
23 changes: 6 additions & 17 deletions Compiler/FrontEnd/InstUtil.mo
Expand Up @@ -2070,25 +2070,14 @@ protected function isElementNamed
input String inName;
output Boolean isNamed;
algorithm
isNamed := matchcontinue(inElement, inName)
isNamed := match inElement
local
String name;

case ((SCode.COMPONENT(name = name), _), _)
equation
true = stringEqual(name, inName);
then
true;

// we can also have packages!
case ((SCode.CLASS(name = name), _), _)
equation
true = stringEqual(name, inName);
then
true;

case (SCode.COMPONENT(name = name), _) then name == inName;
case (SCode.CLASS(name = name), _) then name == inName;
else false;
end matchcontinue;
end match;
end isElementNamed;

protected function isElementEqual
Expand All @@ -2097,7 +2086,7 @@ protected function isElementEqual
input tuple<SCode.Element, DAE.Mod> inElement2;
output Boolean isEqual;
algorithm
isEqual := matchcontinue(inElement1, inElement2)
isEqual := match(inElement1, inElement2)
local
String id1, id2;

Expand All @@ -2112,7 +2101,7 @@ algorithm

else stringEq(elementName(inElement1), elementName(inElement2));

end matchcontinue;
end match;
end isElementEqual;

protected function checkCyclicalComponents
Expand Down
101 changes: 43 additions & 58 deletions Compiler/FrontEnd/Lookup.mo
Expand Up @@ -1444,7 +1444,7 @@ algorithm
case (cache, FCore.G(scope = r::_), id)
equation
ht = FNode.children(FNode.fromRef(r));
(cache,fv,c,m,i,componentEnv) = lookupVar2(cache, ht, id, inEnv);
(fv,c,m,i,componentEnv) = lookupVar2(ht, id, inEnv);
then
(cache,fv,c,m,i,componentEnv);

Expand Down Expand Up @@ -1515,7 +1515,7 @@ algorithm
case (cache,FCore.G(scope = r::_),id)
equation
ht = FNode.children(FNode.fromRef(r));
(cache,fv,c,m,i,_) = lookupVar2(cache, ht, id, inEnv);
(fv,c,m,i,_) = lookupVar2(ht, id, inEnv);
then
(cache,fv,c,m,i,inEnv);

Expand Down Expand Up @@ -2530,55 +2530,36 @@ end reportSeveralNamesError;

protected function lookupVar2
"Helper function to lookupVarF and lookupIdent."
input FCore.Cache inCache;
input FCore.Children inBinTree;
input SCode.Ident inIdent;
input FCore.Graph inGraph;
output FCore.Cache outCache;
output DAE.Var outVar;
output SCode.Element outElement;
output DAE.Mod outMod;
output FCore.Status instStatus;
output FCore.Graph outEnv;
protected
FCore.Ref r;
FCore.Scope s;
FCore.Node n;
String name;
algorithm
(outCache,outVar,outElement,outMod,instStatus,outEnv):=
matchcontinue (inCache,inBinTree,inIdent,inGraph)
local
DAE.Var fv;
SCode.Element c;
DAE.Mod m;
FCore.Status i;
FCore.Kind k;
FCore.Graph env;
FCore.Children ht;
String id, name;
FCore.Cache cache;
SCode.Restriction restr;
FCore.Ref r;
FCore.Scope s;

case (cache, ht, id, _)
equation
r = FNode.avlTreeGet(ht, id);
fv = FNode.refInstVar(r);
s = FNode.refRefTargetScope(r);
FCore.N(data = FCore.CO(c,m,_,i)) = FNode.fromRef(r);
env = FGraph.setScope(inGraph, s);
then
(cache,fv,c,m,i,env);

case (_, ht, id, _)
equation
true = Flags.isSet(Flags.LOOKUP);
false = Config.acceptMetaModelicaGrammar(); // MetaModelica function references generate too much failtrace...
r = FNode.avlTreeGet(ht, id);
FCore.N(data = FCore.CL(e = SCode.CLASS(name = name))) = FNode.fromRef(r);
name = id + " = " + FGraph.printGraphPathStr(inGraph) + "." + name;
Debug.traceln("- Lookup.lookupVar2 failed because we find a class instead of a variable: " + name);
then
fail();

end matchcontinue;
r := FNode.avlTreeGet(inBinTree, inIdent);
outVar := FNode.refInstVar(r);
s := FNode.refRefTargetScope(r);
n := FNode.fromRef(r);

if not FNode.isComponent(n) and Flags.isSet(Flags.LOOKUP) then
// MetaModelica function references generate too much failtrace...
false := Config.acceptMetaModelicaGrammar();
FCore.N(data = FCore.CL(e = SCode.CLASS(name = name))) := n;
name := inIdent + " = " + FGraph.printGraphPathStr(inGraph) + "." + name;
Debug.traceln("- Lookup.lookupVar2 failed because we found a class instead of a variable: " + name);
fail();
end if;

FCore.N(data = FCore.CO(outElement, outMod, _, instStatus)) := n;
outEnv := FGraph.setScope(inGraph, s);
end lookupVar2;

protected function checkSubscripts "This function checks a list of subscripts agains type, and removes
Expand Down Expand Up @@ -2731,7 +2712,7 @@ algorithm
// Simple identifier
case (cache,ht,DAE.CREF_IDENT(ident = id,subscriptLst = ss),_)
equation
(cache,DAE.TYPES_VAR(name,attr,ty,bind,cnstForRange),_,_,_,componentEnv) = lookupVar2(cache, ht, id, inEnv);
(DAE.TYPES_VAR(name,attr,ty,bind,cnstForRange),_,_,_,componentEnv) = lookupVar2(ht, id, inEnv);
ty_1 = checkSubscripts(ty, ss);
tty = Types.simplifyType(ty);
ss = addArrayDimensions(tty,ss);
Expand All @@ -2744,7 +2725,7 @@ algorithm
// Qualified variables looked up through component environment with or without spliced exp
case (cache,ht,DAE.CREF_QUAL(ident = id,subscriptLst = ss,componentRef = ids), _)
equation
(cache,DAE.TYPES_VAR(_,DAE.ATTR(variability = vt2),tyParent,parentBinding,cnstForRange),_,_,_,componentEnv) = lookupVar2(cache, ht, id, inEnv);
(DAE.TYPES_VAR(_,DAE.ATTR(variability = vt2),tyParent,parentBinding,cnstForRange),_,_,_,componentEnv) = lookupVar2(ht, id, inEnv);

// leave just the last scope from component env as it SHOULD BE ONLY THERE, i.e. don't go on searching the parents!
componentEnv = FGraph.setScope(componentEnv, List.create(FGraph.lastScopeRef(componentEnv)));
Expand Down Expand Up @@ -2781,7 +2762,7 @@ algorithm
case (cache,ht,(DAE.CREF_QUAL(ident = id,subscriptLst = {},componentRef = DAE.CREF_IDENT(ident=id2,subscriptLst={}))), _)
equation
true = Config.acceptMetaModelicaGrammar();
(cache,DAE.TYPES_VAR(ty=DAE.T_METARECORD(fields=fields)),_,_,_,componentEnv) = lookupVar2(cache, ht, id, inEnv);
(DAE.TYPES_VAR(ty=DAE.T_METARECORD(fields=fields)),_,_,_,componentEnv) = lookupVar2(ht, id, inEnv);
DAE.TYPES_VAR(name,attr,ty,binding,cnstForRange) = listGet(fields,Types.findVarIndex(id2,fields)+1);
then
(cache,attr,ty,binding,cnstForRange,InstTypes.SPLICEDEXPDATA(NONE(),ty),componentEnv,name);
Expand Down Expand Up @@ -2921,34 +2902,39 @@ protected function makeDimensionSubscript
input DAE.Dimension inDim;
output DAE.Subscript outSub;
algorithm
outSub := matchcontinue(inDim)
outSub := match(inDim)
local
Integer sz;
list<DAE.Exp> expl;
Absyn.Path enum_name;
list<String> l;

// Special case when addressing array[0].
case DAE.DIM_INTEGER(integer = 0)
then
DAE.SLICE(DAE.ARRAY(DAE.T_INTEGER_DEFAULT, true, {DAE.ICONST(0)}));

// Array with integer dimension.
case DAE.DIM_INTEGER(integer = sz)
equation
expl = List.map(List.intRange(sz), Expression.makeIntegerExp);
then
DAE.SLICE(DAE.ARRAY(DAE.T_INTEGER_DEFAULT, true, expl));

// Array with boolean dimension.
case DAE.DIM_BOOLEAN()
equation
expl = DAE.BCONST(false)::DAE.BCONST(true)::{};
expl = {DAE.BCONST(false), DAE.BCONST(true)};
then
DAE.SLICE(DAE.ARRAY(DAE.T_BOOL_DEFAULT, true, expl));

// Array with enumeration dimension.
case DAE.DIM_ENUM(enumTypeName = enum_name, literals = l)
equation
expl = makeEnumLiteralIndices(enum_name, l, 1);
then
DAE.SLICE(DAE.ARRAY(DAE.T_ENUMERATION(NONE(), enum_name, l, {}, {}, DAE.emptyTypeSource), true, expl));
end matchcontinue;
end match;
end makeDimensionSubscript;

protected function makeEnumLiteralIndices
Expand Down Expand Up @@ -3095,23 +3081,22 @@ algorithm

// Look in the current scope.
case (cache, FCore.G(scope = ref::_), _)
equation
ht = FNode.children(FNode.fromRef(ref));
algorithm
ht := FNode.children(FNode.fromRef(ref));
// Only look up the first part of the cref, we're only interested in if
// it exists and if it's an iterator or not.
id = ComponentReference.crefFirstIdent(inCref);
(cache, DAE.TYPES_VAR(constOfForIteratorRange = ic), _, _, _, _) =
lookupVar2(cache, ht, id, inEnv);
b = Util.isSome(ic);
id := ComponentReference.crefFirstIdent(inCref);
(DAE.TYPES_VAR(constOfForIteratorRange = ic),_,_,_,_) := lookupVar2(ht, id, inEnv);
b := Util.isSome(ic);
then
(SOME(b), cache);

// If not found, look in the next scope only if the current scope is implicit.
case (cache, FCore.G(scope = ref::_), _)
equation
true = frameIsImplAddedScope(FNode.fromRef(ref));
(env, _) = FGraph.stripLastScopeRef(inEnv);
(res, cache) = isIterator(cache, env, inCref);
algorithm
true := frameIsImplAddedScope(FNode.fromRef(ref));
(env, _) := FGraph.stripLastScopeRef(inEnv);
(res, cache) := isIterator(cache, env, inCref);
then
(res, cache);

Expand Down
26 changes: 10 additions & 16 deletions Compiler/FrontEnd/Types.mo
Expand Up @@ -1645,25 +1645,19 @@ public function varlistLookup "Given a list of Var and a name, this function fin
input list<DAE.Var> inVarLst;
input String inIdent;
output DAE.Var outVar;
protected
String name;
algorithm
outVar := matchcontinue (inVarLst,inIdent)
local
DAE.Var v;
String n,name;
list<DAE.Var> vs;
for var in inVarLst loop
DAE.TYPES_VAR(name = name) := var;

case (((v as DAE.TYPES_VAR(name = n)) :: _),name)
equation
true = stringEq(n, name);
then
v;
if name == inIdent then
outVar := var;
return;
end if;
end for;

case ((_ :: vs),name)
equation
v = varlistLookup(vs, name);
then
v;
end matchcontinue;
fail();
end varlistLookup;

public function lookupComponent "This function finds a subcomponent by name."
Expand Down

0 comments on commit 11df4e4

Please sign in to comment.