Skip to content

Commit

Permalink
- YOU WILL NEED A NEW RML FOR THIS: Windows (svn up on OMDev), Linux …
Browse files Browse the repository at this point in the history
…(svn up and compile or wait to update to a new one).

- do NOT die if there is a stack overflow in Inst.instantiateClass, continue on.
  try this script with the Buildings library:
  loadModel(Buildings); getErrorString();
  instantiateModel(Buildings.Airflow.Multizone.Examples.NaturalVentilation); getErrorString();
  2+3;
  5+6;
  ------------ you will get something like this -----------
  SCodeLookup.lookupSimpleName
  SCodeLookup.lookupSimpleName2
  SCodeLookup.lookupInLocalScope
  Stack overflow! Failing the current function stack chain until the stack overflow signal is caught!
  ""
  "Error: Error occurred while flattening model Buildings.Airflow.Multizone.Examples.NaturalVentilation. The compiler got into Stack Overflow!"
  5
  11
- OMEdit will no longer die (but will not display parts of the model).
  


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@10310 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
adrpo committed Nov 7, 2011
1 parent 7b22ec1 commit 8c56b89
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 18 deletions.
77 changes: 60 additions & 17 deletions Compiler/FrontEnd/Inst.mo
Expand Up @@ -271,15 +271,10 @@ algorithm
end matchcontinue;
end instantiateImplicit;

public function instantiateClass
"function: instantiateClass
To enable interactive instantiation, an arbitrary class in the program
needs to be possible to instantiate. This function performs the same
action as instProgram, but given a specific class to instantiate.

First, all the class definitions are added to the environment without
modifications, and then the specified class is instantiated in the
function instClassInProgram"
protected function instantiateClass_dispatch
"function: instantiateClass_dispatch
instantiate a class.
if this function fails with stack overflow, it will be caught in the caller"
input Env.Cache inCache;
input InstanceHierarchy inIH;
input SCode.Program inProgram;
Expand All @@ -302,12 +297,7 @@ algorithm
ConnectionGraph.ConnectionGraph graph;
DAE.ElementSource source "the origin of the element";
list<DAE.Element> daeElts;

case (cache,ih,{},cr)
equation
Error.addMessage(Error.NO_CLASSES_LOADED, {});
then
fail();
Boolean stackOverflow;

case (cache,ih,(cdecls as (_ :: _)),(path as Absyn.IDENT(name = name2))) /* top level class */
equation
Expand Down Expand Up @@ -394,9 +384,62 @@ algorithm
then
(cache, env_2, ih, dae);

case (cache,ih,cdecls,path) /* error instantiating */
end matchcontinue;
end instantiateClass_dispatch;

public function instantiateClass
"function: instantiateClass
To enable interactive instantiation, an arbitrary class in the program
needs to be possible to instantiate. This function performs the same
action as instProgram, but given a specific class to instantiate.

First, all the class definitions are added to the environment without
modifications, and then the specified class is instantiated in the
function instClassInProgram"
input Env.Cache inCache;
input InstanceHierarchy inIH;
input SCode.Program inProgram;
input SCode.Path inPath;
output Env.Cache outCache;
output Env.Env outEnv;
output InstanceHierarchy outIH;
output DAE.DAElist outDAElist;
algorithm
(outCache,outEnv,outIH,outDAElist) := matchcontinue (inCache,inIH,inProgram,inPath)
local
Absyn.Path cr,path;
list<Env.Frame> env,env_1,env_2;
DAE.DAElist dae1,dae,dae2;
list<SCode.Element> cdecls;
String name2,n,pathstr,name,cname_str;
SCode.Element cdef;
Env.Cache cache;
InstanceHierarchy ih;
ConnectionGraph.ConnectionGraph graph;
DAE.ElementSource source "the origin of the element";
list<DAE.Element> daeElts;
Boolean stackOverflow;

case (cache,ih,{},cr)
equation
cname_str = Absyn.pathString(path);
Error.addMessage(Error.NO_CLASSES_LOADED, {});
then
fail();

// instantiate a class
case (cache,ih,cdecls as _::_,path)
equation
(outCache,outEnv,outIH,outDAElist) = instantiateClass_dispatch(cache,ih,cdecls,path);
then
(outCache,outEnv,outIH,outDAElist);

// error instantiating
case (cache,ih,cdecls as _::_,path)
equation
// if we got a stack overflow remove the stack-overflow flag
// adrpo: NOTE THAT THE NEXT FUNCTION CALL MUST BE THE FIRST IN THIS CASE, otherwise the stack overflow will not be caught!
stackOverflow = System.setStackOverflowSignal(false);
cname_str = Absyn.pathString(path) +& Util.if_(stackOverflow, ". The compiler got into Stack Overflow!", "");
Error.addMessage(Error.ERROR_FLATTENING, {cname_str});

// let the GC collect these as they are used only by Inst!
Expand Down
2 changes: 1 addition & 1 deletion Compiler/Util/Error.mo
Expand Up @@ -300,7 +300,7 @@ public constant Message INHERIT_BASIC_WITH_COMPS = MESSAGE(87, TRANSLATION(), ER
public constant Message MODIFIER_TYPE_MISMATCH_ERROR = MESSAGE(88, TRANSLATION(), ERROR(),
"Type mismatch in modifier of component %s, expected type %s, got modifier %s of type %s");
public constant Message ERROR_FLATTENING = MESSAGE(89, TRANSLATION(), ERROR(),
"Error occured while flattening model %s");
"Error occurred while flattening model %s");
public constant Message DUPLICATE_ELEMENTS_NOT_IDENTICAL = MESSAGE(90, TRANSLATION(), ERROR(),
"Duplicate elements (due to inherited elements) not identical:
first element is: %s
Expand Down
8 changes: 8 additions & 0 deletions Compiler/Util/System.mo
Expand Up @@ -890,4 +890,12 @@ public function reopenStandardStream
external "C" success=SystemImpl__reopenStandardStream(_stream,filename) annotation(Library = {"omcruntime"});
end reopenStandardStream;

public function setStackOverflowSignal
"@author: adrpo
sets the stack overflow signal and returns the previous one"
input Boolean inStackOverflow;
output Boolean outStackOverflow;
external "C" outStackOverflow = SystemImpl__setStackOverflowSignal(inStackOverflow) annotation(Library = {"omcruntime"});
end setStackOverflowSignal;

end System;
14 changes: 14 additions & 0 deletions Compiler/runtime/System_rml.c
Expand Up @@ -2110,3 +2110,17 @@ RML_BEGIN_LABEL(System__reopenStandardStream)
RML_TAILCALLK(rmlSC);
}
RML_END_LABEL

RML_BEGIN_LABEL(System__setStackOverflowSignal)
{
char tmp = rml_stack_overflow;

rml_stack_overflow = (rmlA0==RML_FALSE)?0:1;

// fprintf(stderr, "-> System__setStackOverflowSignal rml_stack_overflow = %d! before was: %d\n", (int)rml_stack_overflow, (int)tmp); fflush(NULL);

rmlA0 = tmp?RML_TRUE:RML_FALSE;

RML_TAILCALLK(rmlSC);
}
RML_END_LABEL
6 changes: 6 additions & 0 deletions Compiler/runtime/systemimpl.c
Expand Up @@ -1603,6 +1603,12 @@ extern int SystemImpl__reopenStandardStream(int id,const char *filename)
return 1;
}

extern int SystemImpl_setStackOverflowSignal(int stackOverflow)
{
return stackOverflow;
}


#ifdef __cplusplus
}
#endif
Expand Down

0 comments on commit 8c56b89

Please sign in to comment.