From 5de59c3df0363011d9207d402b726a5af90eb2a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Sj=C3=B6lund?= Date: Tue, 2 Apr 2019 13:17:00 +0200 Subject: [PATCH] Fix runtime CFLAGS detection The old code only worked for some combinations of CFLAGS. It did not actually find -fno-dollars-in-identifiers. Belonging to [master]: - OpenModelica/OMCompiler#2371 --- Compiler/Template/CodegenC.tpl | 14 ++++---- Compiler/Template/CodegenCFunctions.tpl | 46 ++++++++++++------------- Compiler/Template/CodegenCppCommon.tpl | 6 ++-- Compiler/Template/SimCodeTV.mo | 10 ++++++ Compiler/Util/System.mo | 9 ++++- Compiler/runtime/systemimpl.c | 21 ++++++++--- configure.ac | 12 +++---- 7 files changed, 73 insertions(+), 45 deletions(-) diff --git a/Compiler/Template/CodegenC.tpl b/Compiler/Template/CodegenC.tpl index fbf1e9eeb4..b4d32922d0 100644 --- a/Compiler/Template/CodegenC.tpl +++ b/Compiler/Template/CodegenC.tpl @@ -1506,12 +1506,12 @@ template globalDataVarDefine(SimVar simVar, String arrayName) "template globalDa /* <%crefStrNoUnderscore(c)%> */ #define _<%cref(c)%>(i) data->localData[i]-><%arrayName%>[<%index%>] #define <%cref(c)%> _<%cref(c)%>(0) - #define $P$PRE<%cref(c)%> data->simulationInfo-><%arrayName%>Pre[<%index%>] + #define <%cref(crefPrefixPre(c))%> data->simulationInfo-><%arrayName%>Pre[<%index%>] /* <%crefStrNoUnderscore(name)%> */ #define _<%cref(name)%>(i) data->localData[i]-><%arrayName%>[<%index%>] #define <%cref(name)%> _<%cref(name)%>(0) - #define $P$PRE<%cref(name)%> data->simulationInfo-><%arrayName%>Pre[<%index%>] + #define <%cref(crefPrefixPre(name))%> data->simulationInfo-><%arrayName%>Pre[<%index%>] >> case SIMVAR(aliasvar=NOALIAS()) then @@ -1519,8 +1519,8 @@ template globalDataVarDefine(SimVar simVar, String arrayName) "template globalDa /* <%crefStrNoUnderscore(name)%> */ #define _<%cref(name)%>(i) data->localData[i]-><%arrayName%>[<%index%>] #define <%cref(name)%> _<%cref(name)%>(0) - #define $P$PRE<%cref(name)%> data->simulationInfo-><%arrayName%>Pre[<%index%>] - #define _$P$PRE<%cref(name)%>(i) $P$PRE<%cref(name)%> + #define <%cref(crefPrefixPre(name))%> data->simulationInfo-><%arrayName%>Pre[<%index%>] + #define _<%cref(crefPrefixPre(name))%>(i) <%cref(crefPrefixPre(name))%> >> end match @@ -4025,7 +4025,7 @@ template functionODE(list> derivativEquations, Text method, Op TRACE_PUSH #if !defined(OMC_MINIMAL_RUNTIME) <% if profileFunctions() then "" else "if (measure_time_flag) " %>rt_tick(SIM_TIMER_FUNCTION_ODE); - #endif !defined(OMC_MINIMAL_RUNTIME) + #endif <%varDecls%> @@ -4239,7 +4239,7 @@ template functionDAE(list allEquationsPlusWhen, String modelNamePre <%addRootsTempArray()%> #if !defined(OMC_MINIMAL_RUNTIME) <% if profileFunctions() then "" else "if (measure_time_flag) " %>rt_tick(SIM_TIMER_DAE); - #endif !defined(OMC_MINIMAL_RUNTIME) + #endif data->simulationInfo->needToIterate = 0; data->simulationInfo->discreteCall = 1; @@ -4250,7 +4250,7 @@ template functionDAE(list allEquationsPlusWhen, String modelNamePre #if !defined(OMC_MINIMAL_RUNTIME) <% if profileFunctions() then "" else "if (measure_time_flag) " %>rt_accumulate(SIM_TIMER_DAE); - #endif !defined(OMC_MINIMAL_RUNTIME) + #endif TRACE_POP return 0; } diff --git a/Compiler/Template/CodegenCFunctions.tpl b/Compiler/Template/CodegenCFunctions.tpl index 6e725004b2..8728bb2349 100644 --- a/Compiler/Template/CodegenCFunctions.tpl +++ b/Compiler/Template/CodegenCFunctions.tpl @@ -607,7 +607,7 @@ template funArgName(Variable var) let &auxFunction = buffer "" match var case VARIABLE(__) then contextCref(name,contextFunction,&auxFunction) - case FUNCTION_PTR(__) then '_' + name + case FUNCTION_PTR(__) then '_' + System.unquoteIdentifier(name) end funArgName; template funArgDefinition(Variable var) @@ -615,7 +615,7 @@ template funArgDefinition(Variable var) let &auxFunction = buffer "" match var case VARIABLE(__) then ('<%varType(var)%> <%contextCref(name,contextFunction,&auxFunction)%>' + (if var.instDims then " = {0}")) - case FUNCTION_PTR(__) then 'modelica_fnptr _<%name%>' + case FUNCTION_PTR(__) then 'modelica_fnptr _<%System.unquoteIdentifier(name)%>' end funArgDefinition; template funArgDefinitionKernelFunctionInterface(Variable var) @@ -777,7 +777,7 @@ template funArgBoxedDefinition(Variable var) let &auxFunction = buffer "" match var case VARIABLE(__) then 'modelica_metatype <%contextCref(name,contextFunction,&auxFunction)%>' - case FUNCTION_PTR(__) then 'modelica_fnptr _<%name%>' + case FUNCTION_PTR(__) then 'modelica_fnptr _<%System.unquoteIdentifier(name)%>' end funArgBoxedDefinition; template extFunDef(Function fn) @@ -4205,9 +4205,9 @@ template contextIteratorName(Ident name, Context context) "Generates code for an iterator variable." ::= match context - case FUNCTION_CONTEXT(__) then "_" + name - case PARALLEL_FUNCTION_CONTEXT(__) then "_" + name - else "$P" + name + case FUNCTION_CONTEXT(__) + case PARALLEL_FUNCTION_CONTEXT(__) then "_" + System.unquoteIdentifier(name) + else System.forceQuotedIdentifier(name) end contextIteratorName; /* public */ template cref(ComponentRef cr) @@ -4253,7 +4253,7 @@ end crefPre; case CREF_IDENT(ident = "xloc") then crefStr(cr) case CREF_IDENT(ident = "time") then "data->localData[0]->timeValue" case WILD(__) then '' - else "$P" + crefToCStrDefine(cr) + else System.forceQuotedIdentifier(crefStrNoUnderscore(cr)) end crefDefine; template crefToCStr(ComponentRef cr, Integer ix, Boolean isPre, Boolean isStart) @@ -4279,8 +4279,9 @@ template crefToCStr(ComponentRef cr, Integer ix, Boolean isPre, Boolean isStart) case SIMVAR(varKind=DAE_AUX_VAR()) case SIMVAR(index=-2) then - if intEq(ix,0) then (if isPre then "$P$PRE")+crefDefine(cr) - else '_<%if isPre then "$P$PRE"%><%crefDefine(cr)%>(<%ix%>)' + (let s = (if isPre then crefDefine(crefPrefixPre(cr)) else crefDefine(cr)) + if intEq(ix,0) then s + else '_<%s%>(<%ix%>)') case var as SIMVAR(index=-1) then error(sourceInfo(), 'crefToCStr got index=-1 for <%variabilityString(varKind)%> <%crefStrNoUnderscore(name)%>') case var as SIMVAR(__) then '<%varArrayNameValues(var, ix, isPre, isStart)%>' else "CREF_NOT_IDENT_OR_QUAL" @@ -4374,8 +4375,8 @@ end contextArrayCref; template arrayCrefStr(ComponentRef cr) ::= match cr - case CREF_IDENT(__) then '<%ident%>' - case CREF_QUAL(__) then '<%ident%>._<%arrayCrefStr(componentRef)%>' + case CREF_IDENT(__) then System.unquoteIdentifier(ident) + case CREF_QUAL(__) then '<%System.unquoteIdentifier(ident)%>._<%arrayCrefStr(componentRef)%>' else "CREF_NOT_IDENT_OR_QUAL" end arrayCrefStr; @@ -5656,11 +5657,7 @@ end daeExpIteratedCref; template iteratedCrefStr(ComponentRef cref) ::= -match cref -case (CREF_IDENT(__)) then - <<$P<%ident%>>> -case (CREF_QUAL(__)) then - <<$P<%ident%><%iteratedCrefStr(componentRef)%>>> + System.forceQuotedIdentifier(crefStrNoUnderscore(cref)) end iteratedCrefStr; template resultVarAssignment(DAE.Type ty, Text lhs, Text rhs) "Tuple need to be considered" @@ -6295,16 +6292,16 @@ template daeExpTailCall(list es, list vs, Context context, Text // adrpo: ignore _x = _x! if stringEq(v, crefStr(cr)) then '<%daeExpTailCall(erest, vrest, context, &preExp, &postExp, &varDecls, &auxFunction)%>' - else '_<%v%> = <%exp%>;<%\n%><%daeExpTailCall(erest, vrest, context, &preExp, &postExp, &varDecls, &auxFunction)%>' + else '_<%System.unquoteIdentifier(v)%> = <%exp%>;<%\n%><%daeExpTailCall(erest, vrest, context, &preExp, &postExp, &varDecls, &auxFunction)%>' case _ then (if anyExpHasCrefName(erest, v) then /* We might overwrite a value with something else, so make an extra copy of it */ let tmp = tempDecl(expTypeFromExpModelica(e),&varDecls) - let &postExp += '_<%v%> = <%tmp%>;<%\n%>' + let &postExp += '_<%System.unquoteIdentifier(v)%> = <%tmp%>;<%\n%>' '<%tmp%> = <%exp%>;<%\n%><%daeExpTailCall(erest, vrest, context, &preExp, &postExp, &varDecls, &auxFunction)%>' else let restText = daeExpTailCall(erest, vrest, context, &preExp, &postExp, &varDecls, &auxFunction) - let v2 = '_<%v%>' + let v2 = '_<%System.unquoteIdentifier(v)%>' if stringEq(v2, exp) then restText else '<%v2%> = <%exp%>;<%\n%><%restText%>') @@ -6703,7 +6700,7 @@ template daeExpReduction(Exp exp, Context context, Text &preExp, let identType = expTypeFromExpModelica(iter.exp) let arrayType = expTypeFromExpArray(iter.exp) let iteratorName = contextIteratorName(iter.id, context) - let loopVar = match iter.exp case RANGE(__) then iteratorName else '<%iter.id%>_loopVar' + let loopVar = match iter.exp case RANGE(__) then iteratorName else '<%iteratorName%>_loopVar' let stepVar = match iter.exp case RANGE(__) then tempDecl(identType,&tmpVarDecls) let stopVar = match iter.exp case RANGE(__) then tempDecl(identType,&tmpVarDecls) let &guardExpPre = buffer "" @@ -6731,12 +6728,12 @@ template daeExpReduction(Exp exp, Context context, Text &preExp, case RANGE(step=SOME(DAE.ICONST(__))) then "" else check) let isArrayWithLength = if rangeExpStop then (match ri.path case IDENT(name="array") then "1" else "") else "" - let &tmpVarDecls += if isArrayWithLength then 'modelica_integer <%iter.id%>_length;<%\n%>' + let &tmpVarDecls += if isArrayWithLength then 'modelica_integer <%iteratorName%>_length;<%\n%>' let &rangeExpPre += match iter.exp case RANGE(__) then "" else (if firstIndex then '<%firstIndex%> = 1;<%\n%>') let guardCond = (match iter.guardExp case SOME(grd) then daeExp(grd, context, &guardExpPre, &tmpVarDecls, &auxFunction) else "") let &tmpVarDecls += '<%identType%> <%iteratorName%>;<%\n%>' let &rangeExpPre += if isArrayWithLength then - '<%iter.id%>_length = ((<%stopVar%>-<%if firstIndex then firstIndex else iteratorName %>)/<%stepVar%>)+1;<%\n%>' + '<%iteratorName%>_length = ((<%stopVar%>-<%if firstIndex then firstIndex else iteratorName %>)/<%stepVar%>)+1;<%\n%>' let &rangeExpPre += match iter.exp case RANGE(__) then '<%iteratorName%> = (<%rangeExp%>)-<%stepVar%>;<%\n%>' /* We pre-increment the counter, so subtract the step for the first variable for ranges */ let guardExp = << @@ -6821,9 +6818,10 @@ template daeExpReduction(Exp exp, Context context, Text &preExp, let length = tempDecl("int",&tmpVarDecls) let &rangeExpPre += '<%length%> = 0;<%\n%>' let _ = (iterators |> iter as REDUCTIONITER(__) => - let loopVar = '<%iter.id%>_loopVar' + let iteratorName = contextIteratorName(iter.id, context) + let loopVar = '<%iteratorName%>_loopVar' let identType = expTypeFromExpModelica(iter.exp) - let &rangeExpPre += '<%length%> = modelica_integer_max(<%length%>,<%match identType case "modelica_metatype" then (if isMetaArray(iter.exp) then 'arrayLength(<%loopVar%>)' else 'listLength(<%loopVar%>)') else match iter.exp case RANGE(__) then '<%iter.id%>_length' else 'size_of_dimension_base_array(<%loopVar%>, 1)'%>);<%\n%>' + let &rangeExpPre += '<%length%> = modelica_integer_max(<%length%>,<%match identType case "modelica_metatype" then (if isMetaArray(iter.exp) then 'arrayLength(<%loopVar%>)' else 'listLength(<%loopVar%>)') else match iter.exp case RANGE(__) then '<%iteratorName%>_length' else 'size_of_dimension_base_array(<%loopVar%>, 1)'%>);<%\n%>' "") << <%arrIndex%> = 1; diff --git a/Compiler/Template/CodegenCppCommon.tpl b/Compiler/Template/CodegenCppCommon.tpl index ddd7e1018b..f027b8d9ee 100644 --- a/Compiler/Template/CodegenCppCommon.tpl +++ b/Compiler/Template/CodegenCppCommon.tpl @@ -121,8 +121,8 @@ template crefStr(ComponentRef cr) match cr case CREF_IDENT(ident = "xloc") then '__xd<%subscriptsStr(subscriptLst)%>' case CREF_IDENT(ident = "time") then "_simTime" - case CREF_IDENT(__) then '<%ident%>_<%subscriptsStr(subscriptLst)%>' - case CREF_QUAL(__) then '<%ident%>_<%subscriptsStr(subscriptLst)%>.<%crefStr(componentRef)%>' + case CREF_IDENT(__) then '<%System.unquoteIdentifier(ident)%>_<%subscriptsStr(subscriptLst)%>' + case CREF_QUAL(__) then '<%System.unquoteIdentifier(ident)%>_<%subscriptsStr(subscriptLst)%>.<%crefStr(componentRef)%>' else "CREF_NOT_IDENT_OR_QUAL" end crefStr; @@ -153,7 +153,7 @@ case CREF_QUAL(ident = "$PRE") then else let &varDeclsCref = buffer "" /*BUFD*/ match context - case FUNCTION_CONTEXT(__) then System.unquoteIdentifier(crefStr(cr)) + case FUNCTION_CONTEXT(__) then crefStr(cr) else '<%cref1(cr,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace,context,varDeclsCref,stateDerVectorName,useFlatArrayNotation)%>' end contextCref; diff --git a/Compiler/Template/SimCodeTV.mo b/Compiler/Template/SimCodeTV.mo index 0277cf36ef..8506664022 100644 --- a/Compiler/Template/SimCodeTV.mo +++ b/Compiler/Template/SimCodeTV.mo @@ -1592,6 +1592,11 @@ package System output String outStr; end unquoteIdentifier; + function forceQuotedIdentifier + input String str; + output String outStr; + end forceQuotedIdentifier; + function dirname input String str; output String outStr; @@ -3439,6 +3444,11 @@ package ComponentReference output DAE.ComponentRef outCref; end crefPrefixDer; + function crefPrefixPre + input DAE.ComponentRef inCref; + output DAE.ComponentRef outCref; + end crefPrefixPre; + function makeUntypedCrefIdent input DAE.Ident ident; output DAE.ComponentRef outCrefIdent; diff --git a/Compiler/Util/System.mo b/Compiler/Util/System.mo index ee7be29c98..052a0214da 100644 --- a/Compiler/Util/System.mo +++ b/Compiler/Util/System.mo @@ -864,12 +864,19 @@ literal in C. For example unescapedStringLength('\"')=1, unescapedStringLength(' end unescapedStringLength; public function unquoteIdentifier - "Quoted identifiers, for example 'xyz' need to be translated into canonical form; for example _omcQuot_0x78797A" + "Quoted identifiers, for example 'xyz' need to be translated into canonical form; for example _omcQuot_0x2778797A27" input String str; output String outStr; external "C" outStr=System_unquoteIdentifier(str) annotation(Library = "omcruntime"); end unquoteIdentifier; +public function forceQuotedIdentifier + "Forced quoted identifiers, for example xyz is translated into canonical form; for example _omcQuot_0x78797A" + input String str; + output String outStr; + external "C" outStr=System_forceQuotedIdentifier(str) annotation(Library = "omcruntime"); +end forceQuotedIdentifier; + public function intMaxLit "Returns the maximum integer that can be represent using this version of the compiler" output Integer outInt; external "builtin" outInt=intMaxLit(); diff --git a/Compiler/runtime/systemimpl.c b/Compiler/runtime/systemimpl.c index 8ee9397fa4..5980e66419 100644 --- a/Compiler/runtime/systemimpl.c +++ b/Compiler/runtime/systemimpl.c @@ -1670,20 +1670,19 @@ extern char* SystemImpl__unescapedString(const char* str) return res; } -extern char* SystemImpl__unquoteIdentifier(const char* str) +extern char* System_forceQuotedIdentifier(const char* str) { const char lookupTbl[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; char *res,*cur; int len,i; const int offset = 10; const char _omcQuot[]="_omcQuot_"; - if (*str != '\'') return NULL; - len = strlen(str)-2; + len = strlen(str); res = (char*) omc_alloc_interface.malloc_atomic(2*len+offset+64); cur = res; cur += sprintf(cur,"%s",_omcQuot); for (i=0; i