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

Commit

Permalink
better handling of builtin and extern C functions (ticket:5057)
Browse files Browse the repository at this point in the history
- check if the non fullyqualified name of the function is the same as the name of the external function
- if the non fullyqualified name is not the same, generate the function and call the extern definition
- fix definition of builtin integerMax and numBits

Belonging to [master]:
  - #2585
  • Loading branch information
adrpo authored and OpenModelica-Hudson committed Aug 2, 2018
1 parent fc9be10 commit 9d2a05f
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 20 deletions.
4 changes: 2 additions & 2 deletions Compiler/FrontEnd/ModelicaBuiltin.mo
Expand Up @@ -1021,11 +1021,11 @@ package Internal "Contains internal implementations, e.g. overloaded builtin fun
package Architecture
function numBits
output Integer numBit;
external "builtin";
external "builtin" numBit = architecture_numbits() annotation(Include="#define architecture_numbits() (8*sizeof(void*))");
end numBits;
function integerMax
output Integer max;
external "builtin";
external "builtin" max = intMaxLit();
end integerMax;
end Architecture;

Expand Down
4 changes: 2 additions & 2 deletions Compiler/NFFrontEnd/NFModelicaBuiltin.mo
Expand Up @@ -1142,11 +1142,11 @@ package Internal "Contains internal implementations, e.g. overloaded builtin fun
package Architecture
function numBits
output Integer numBit;
external "builtin";
external "builtin" numBit = architecture_numbits() annotation(Include="#define architecture_numbits() (8*sizeof(void*))");
end numBits;
function integerMax
output Integer max;
external "builtin";
external "builtin" max = intMaxLit();
end integerMax;
end Architecture;

Expand Down
5 changes: 4 additions & 1 deletion Compiler/Script/CevalScriptBackend.mo
Expand Up @@ -5800,13 +5800,16 @@ algorithm
Flags.setConfigBool(Flags.CHECK_MODEL, true);
(_,Values.STRING(str)) = checkModel(FCore.emptyCache(), env, className, msg);
Flags.setConfigBool(Flags.CHECK_MODEL, false);
(_,Values.STRING(str)) = checkModel(FCore.emptyCache(), env, className, msg);
t2 = clock();
elapsedTime = t2 - t1;
s = realString(elapsedTime);
print (s + " seconds -> " + failOrSuccess(str) + "\n\t");
print (System.stringReplace(str, "\n", "\n\t"));
print ("\n");
print ("Error String:\n" + Print.getErrorString() + "\n");
print ("Error Buffer:\n" + ErrorExt.printMessagesStr(false) + "\n");
print ("# " + realString(elapsedTime) + " : " + Absyn.pathString(className) + "\n");
print ("-------------------------------------------------------------------------\n");
checkAll(cache, env, rest, msg);
then ();

Expand Down
24 changes: 16 additions & 8 deletions Compiler/SimCode/SimCodeFunctionUtil.mo
Expand Up @@ -545,7 +545,7 @@ algorithm
DAE.Function fel;
list<DAE.Function> rest;
list<SimCodeFunction.RecordDeclaration> decls;
String name;
String name, fname;
list<String> includeDirs;
Absyn.Path path;

Expand All @@ -563,18 +563,26 @@ algorithm
(fns, rt_2, decls, includes, includeDirs, libs,libPaths) = elaborateFunctions2(program, rest, accfns, rt, decls, includes, includeDirs, libs,libPaths);
then
(fns, rt_2, decls, includes, includeDirs, libs,libPaths);
case (_, DAE.FUNCTION(functions = DAE.FUNCTION_EXT(externalDecl = DAE.EXTERNALDECL(language="builtin"))::_)::rest, accfns, rt, decls, includes, includeDirs, libs,libPaths)
case (_, (fel as DAE.FUNCTION(path = path, functions = DAE.FUNCTION_EXT(externalDecl = DAE.EXTERNALDECL(name=name, language="builtin"))::_))::rest, accfns, rt, decls, includes, includeDirs, libs,libPaths)
equation
// skip over builtin functions
(fns, rt_2, decls, includes, includeDirs, libs,libPaths) = elaborateFunctions2(program, rest, accfns, rt, decls, includes, includeDirs, libs,libPaths);
// skip over builtin functions @adrpo: we should skip ONLY IF THE NAME OF THE FUNCTION IS THE SAME AS THE NAME OF THE EXTERNAL FUNCTION!
fname = Absyn.pathString(Absyn.makeNotFullyQualified(path));
b = stringEq(fname, name);
if not b then
(fn,_, decls, includes, includeDirs, libs,libPaths) = elaborateFunction(program, fel, rt, decls, includes, includeDirs, libs,libPaths);
end if;
(fns, rt_2, decls, includes, includeDirs, libs,libPaths) = elaborateFunctions2(program, rest, List.consOnTrue(not b, fn, accfns), rt, decls, includes, includeDirs, libs,libPaths);
then
(fns, rt_2, decls, includes, includeDirs, libs,libPaths);

case (_, (fel as DAE.FUNCTION(functions = DAE.FUNCTION_EXT(externalDecl = DAE.EXTERNALDECL(name=name, language="C"))::_))::rest, accfns, rt, decls, includes, includeDirs, libs,libPaths)
case (_, (fel as DAE.FUNCTION(path = path, functions = DAE.FUNCTION_EXT(externalDecl = DAE.EXTERNALDECL(name=name, language="C"))::_))::rest, accfns, rt, decls, includes, includeDirs, libs,libPaths)
equation
// skip over builtin functions
b = listMember(name, SCode.knownExternalCFunctions);
(fn,_, decls, includes, includeDirs, libs,libPaths) = elaborateFunction(program, fel, rt, decls, includes, includeDirs, libs,libPaths);
// skip over known external C functions @adrpo: we should skip ONLY IF THE NAME OF THE FUNCTION IS THE SAME AS THE NAME OF THE EXTERNAL FUNCTION!
fname = Absyn.pathString(Absyn.makeNotFullyQualified(path));
b = listMember(name, SCode.knownExternalCFunctions) and stringEq(fname, name);
if not b then
(fn,_, decls, includes, includeDirs, libs,libPaths) = elaborateFunction(program, fel, rt, decls, includes, includeDirs, libs,libPaths);
end if;
(fns, rt_2, decls, includes, includeDirs, libs,libPaths) = elaborateFunctions2(program, rest, List.consOnTrue(not b, fn, accfns), rt, decls, includes, includeDirs, libs,libPaths);
then
(fns, rt_2, decls, includes, includeDirs, libs,libPaths);
Expand Down
17 changes: 10 additions & 7 deletions Compiler/Template/CodegenCFunctions.tpl
Expand Up @@ -784,17 +784,16 @@ case func as EXTERNAL_FUNCTION(__) then
let fn_name = extFunctionName(extName, language)
let fargsStr = extFunDefArgs(extArgs, language)
let fargsStrEscaped = '<%escapeCComments(fargsStr)%>'
let includesStr = includes |> i => i ;separator=", "
let isBuiltin = match language case "BUILTIN" then true end match
/*
* adrpo:
* only declare the external function definition IF THERE WERE NO INCLUDES!
* i did not put includesStr string in the comment below as it might include
* entire files
* or if the function is not builtin
*/
if includes then
if boolOr(boolNot(listEmpty(includes)), boolNot(stringEq(isBuiltin, ""))) then
<<
/*
* The function has annotation(Include=...>)
* The function has annotation(Include=...>) or is builtin
* the external function definition should be present
* in one of these files and have this prototype:
* extern <%extReturnType(extReturn)%> <%fn_name%>(<%fargsStrEscaped%>);
Expand Down Expand Up @@ -823,17 +822,19 @@ end extFunDefDynamic;
/* public */ template extFunctionName(String name, String language) "used in Compiler/Template/CodegenFMU.tpl"
::=
match language
case "BUILTIN"
case "C" then '<%name%>'
case "FORTRAN 77" then '<%name%>_'
else error(sourceInfo(), 'Unsupport external language: <%language%>')
else error(sourceInfo(), 'Unsupported external language: <%language%>')
end extFunctionName;

template extFunDefArgs(list<SimExtArg> args, String language)
::=
match language
case "BUILTIN"
case "C" then (args |> arg => extFunDefArg(arg) ;separator=", ")
case "FORTRAN 77" then (args |> arg => extFunDefArgF77(arg) ;separator=", ")
else error(sourceInfo(), 'Unsupport external language: <%language%>')
else error(sourceInfo(), 'Unsupported external language: <%language%>')
end extFunDefArgs;

template extReturnType(SimExtArg extArg)
Expand Down Expand Up @@ -2111,8 +2112,10 @@ template extFunCall(Function fun, Text &preExp, Text &varDecls, Text &varInit, T
match fun
case EXTERNAL_FUNCTION(__) then
match language
case "BUILTIN"
case "C" then extFunCallC(fun, &preExp, &varDecls, &auxFunction)
case "FORTRAN 77" then extFunCallF77(fun, &preExp, &varDecls, &varInit, &auxFunction)
else error(sourceInfo(), 'Unsupported external language: <%language%>')
end extFunCall;

template extFunCallC(Function fun, Text &preExp, Text &varDecls, Text &auxFunction)
Expand Down

0 comments on commit 9d2a05f

Please sign in to comment.