OMCompiler/Compiler/Template/CodegenCppCommon.tpl
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * This file is part of OpenModelica. | ||
| 3 | * | ||
| 4 | * Copyright (c) 1998-2026, Open Source Modelica Consortium (OSMC), | ||
| 5 | * c/o Linköpings universitet, Department of Computer and Information Science, | ||
| 6 | * SE-58183 Linköping, Sweden. | ||
| 7 | * | ||
| 8 | * All rights reserved. | ||
| 9 | * | ||
| 10 | * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF AGPL VERSION 3 LICENSE OR | ||
| 11 | * THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.8. | ||
| 12 | * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES | ||
| 13 | * RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GNU AGPL | ||
| 14 | * VERSION 3, ACCORDING TO RECIPIENTS CHOICE. | ||
| 15 | * | ||
| 16 | * The OpenModelica software and the OSMC (Open Source Modelica Consortium) | ||
| 17 | * Public License (OSMC-PL) are obtained from OSMC, either from the above | ||
| 18 | * address, from the URLs: | ||
| 19 | * http://www.openmodelica.org or | ||
| 20 | * https://github.com/OpenModelica/ or | ||
| 21 | * http://www.ida.liu.se/projects/OpenModelica, | ||
| 22 | * and in the OpenModelica distribution. | ||
| 23 | * | ||
| 24 | * GNU AGPL version 3 is obtained from: | ||
| 25 | * https://www.gnu.org/licenses/licenses.html#GPL | ||
| 26 | * | ||
| 27 | * This program is distributed WITHOUT ANY WARRANTY; without | ||
| 28 | * even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 29 | * FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH | ||
| 30 | * IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL. | ||
| 31 | * | ||
| 32 | * See the full OSMC Public License conditions for more details. | ||
| 33 | * | ||
| 34 | */ | ||
| 35 | |||
| 36 | package CodegenCppCommon | ||
| 37 | import interface SimCodeTV; | ||
| 38 | import CodegenUtil.*; | ||
| 39 | import ExpressionDumpTpl; | ||
| 40 | |||
| 41 | /** | ||
| 42 | * Basic template functions for cpp template | ||
| 43 | * -cref to string template functions | ||
| 44 | * -type to string template functions | ||
| 45 | * -string for temp var template functions | ||
| 46 | * -exp to string template functions | ||
| 47 | * -accessors to SimCode attributes | ||
| 48 | */ | ||
| 49 | |||
| 50 | /******************************************************************************************************************************************************* | ||
| 51 | * cref to string template functions | ||
| 52 | *******************************************************************************************************************************************************/ | ||
| 53 | |||
| 54 | 401946 | template cref(ComponentRef cr, Boolean useFlatArrayNotation) | |
| 55 | "Generates C equivalent name for component reference." | ||
| 56 | ::= | ||
| 57 | match cr | ||
| 58 | case CREF_IDENT(ident = "time") then "_simTime" | ||
| 59 | case WILD(__) then '' | ||
| 60 | else "_"+crefToCStr(cr, useFlatArrayNotation) | ||
| 61 | end cref; | ||
| 62 | |||
| 63 | 70 | template localCref(ComponentRef cr, Boolean useFlatArrayNotation) | |
| 64 | "Generates C equivalent name for a local component reference." | ||
| 65 | ::= | ||
| 66 | match cr | ||
| 67 | case CREF_IDENT(ident = "time") then "_simTime" | ||
| 68 | case CREF_IDENT(ident = "__HOM_LAMBDA") then "1.0" | ||
| 69 | case WILD(__) then '' | ||
| 70 | else crefToCStr(cr,useFlatArrayNotation) | ||
| 71 | end localCref; | ||
| 72 | |||
| 73 | 16809 | template subscriptsToCStr(list<DAE.Subscript> subscripts, Boolean useFlatArrayNotation) | |
| 74 | ::= | ||
| 75 | if subscripts then | ||
| 76 | |||
| 77 | if useFlatArrayNotation then | ||
| 78 | '_<%subscripts |> s => subscriptToCStr(s) ;separator="_"%>' | ||
| 79 | else | ||
| 80 | '(<%subscripts |> s => subscriptToCStr(s) ;separator=","%>)' | ||
| 81 | end subscriptsToCStr; | ||
| 82 | |||
| 83 | 257276 | template subscriptToCStr(DAE.Subscript subscript) | |
| 84 | ::= | ||
| 85 | match subscript | ||
| 86 | case SLICE(exp=ICONST(integer=i)) then i | ||
| 87 | case WHOLEDIM(__) then "WHOLEDIM" | ||
| 88 | case INDEX(__) then | ||
| 89 | match exp | ||
| 90 | case ICONST(integer=i) then i | ||
| 91 | case ENUM_LITERAL(index=i) then i | ||
| 92 | case CREF(componentRef=cr) then crefToCStr(cr, false) | ||
| 93 | end match | ||
| 94 | else "UNKNOWN_SUBSCRIPT" | ||
| 95 | end subscriptToCStr; | ||
| 96 | |||
| 97 | 879950 | template crefToCStrForArray(ComponentRef cr, Text& dims) | |
| 98 | "Convert array cref to cstr. Skip subscripts if not NF_SCALARIZE." | ||
| 99 | ::= | ||
| 100 | match cr | ||
| 101 | case CREF_IDENT(__) then | ||
| 102 | let &dims+=listLength(subscriptLst) | ||
| 103 | '<%System.unquoteIdentifier(ident)%>_' | ||
| 104 | case CREF_QUAL(__) then | ||
| 105 | let subs = if Flags.isSet(Flags.NF_SCALARIZE) then subscriptsToCStrForArray(subscriptLst) | ||
| 106 | '<%System.unquoteIdentifier(ident)%><%subs%>_P_<%crefToCStrForArray(componentRef,dims)%>' | ||
| 107 | case WILD(__) then ' ' | ||
| 108 | else "CREF_NOT_IDENT_OR_QUAL" | ||
| 109 | end crefToCStrForArray; | ||
| 110 | |||
| 111 | 215334 | template crefToCStr1(ComponentRef cr, Boolean useFlatArrayNotation) | |
| 112 | ::= | ||
| 113 | match cr | ||
| 114 | case CREF_IDENT(__) then '<%System.unquoteIdentifier(ident)%>_' | ||
| 115 | case CREF_QUAL(__) then | ||
| 116 | '<%System.unquoteIdentifier(ident)%><%subscriptsToCStrForArray(subscriptLst)%>_P_<%crefToCStr1(componentRef,useFlatArrayNotation)%>' | ||
| 117 | case WILD(__) then ' ' | ||
| 118 | else "CREF_NOT_IDENT_OR_QUAL" | ||
| 119 | end crefToCStr1; | ||
| 120 | |||
| 121 | 238020 | template subscriptsToCStrForArray(list<DAE.Subscript> subscripts) | |
| 122 | ::= | ||
| 123 | if subscripts then | ||
| 124 | '<%subscripts |> s => subscriptToCStr(s) ;separator="$c"%>' | ||
| 125 | end subscriptsToCStrForArray; | ||
| 126 | |||
| 127 | 715 | template crefStrForWriteOutput(ComponentRef cr) | |
| 128 | "template for writing output variable names in mat or csv files" | ||
| 129 | ::= | ||
| 130 | match cr | ||
| 131 | case CREF_IDENT(ident = "xloc") then '__xd<%subscriptsStrForWriteOutput(subscriptLst)%>' | ||
| 132 | case CREF_IDENT(ident = "time") then "_simTime" | ||
| 133 | case CREF_IDENT(__) then '<%System.unquoteIdentifier(ident)%><%subscriptsStrForWriteOutput(subscriptLst)%>' | ||
| 134 | case CREF_QUAL(ident = "$DER") then 'der(<%crefStrForWriteOutput(componentRef)%>)' | ||
| 135 | case CREF_QUAL(ident = "$CLKPRE") then 'previous(<%crefStrForWriteOutput(componentRef)%>)' | ||
| 136 | case CREF_QUAL(__) then '<%System.unquoteIdentifier(ident)%><%subscriptsStrForWriteOutput(subscriptLst)%>.<%crefStrForWriteOutput(componentRef)%>' | ||
| 137 | else "CREF_NOT_IDENT_OR_QUAL" | ||
| 138 | end crefStrForWriteOutput; | ||
| 139 | |||
| 140 | ✗ | template crefStrForSetVariables(ComponentRef cr, Boolean useFlatArrayNotation) | |
| 141 | "template for Set Variables for labeling reduction" | ||
| 142 | ::= | ||
| 143 | match cr | ||
| 144 | case CREF_QUAL(ident = "$DER") then "" | ||
| 145 | else cref(cr,useFlatArrayNotation) | ||
| 146 | end crefStrForSetVariables; | ||
| 147 | |||
| 148 | 201 | template subscriptsStrForWriteOutput(list<DAE.Subscript> subscripts) | |
| 149 | "Generares subscript part of the name." | ||
| 150 | ::= | ||
| 151 | if subscripts then | ||
| 152 | '[<%subscripts |> s => subscriptStr(s) ;separator=","%>]' | ||
| 153 | end subscriptsStrForWriteOutput; | ||
| 154 | |||
| 155 | 40864 | template crefStr(ComponentRef cr) | |
| 156 | ::= | ||
| 157 | match cr | ||
| 158 | case CREF_IDENT(ident = "xloc") then '__xd<%subscriptsStr(subscriptLst)%>' | ||
| 159 | case CREF_IDENT(ident = "time") then "_simTime" | ||
| 160 | case CREF_IDENT(__) then '<%System.unquoteIdentifier(ident)%>_<%subscriptsStr(subscriptLst)%>' | ||
| 161 | case CREF_QUAL(__) then '<%System.unquoteIdentifier(ident)%>_<%subscriptsStr(subscriptLst)%>.<%crefStr(componentRef)%>' | ||
| 162 | else "CREF_NOT_IDENT_OR_QUAL" | ||
| 163 | end crefStr; | ||
| 164 | |||
| 165 | ✗ | template subscriptsStr(list<DAE.Subscript> subscripts) | |
| 166 | "Generares subscript part of the name." | ||
| 167 | ::= | ||
| 168 | if subscripts then | ||
| 169 | '(<%subscripts |> s => subscriptStr(s) ;separator=","%>)' | ||
| 170 | end subscriptsStr; | ||
| 171 | |||
| 172 | 201 | template subscriptStr(DAE.Subscript subscript) | |
| 173 | "Generates a single subscript." | ||
| 174 | ::= | ||
| 175 | match subscript | ||
| 176 | case SLICE(exp=ICONST(integer=i)) then i | ||
| 177 | case WHOLEDIM(__) then "WHOLEDIM" | ||
| 178 | case INDEX(__) then | ||
| 179 | match exp | ||
| 180 | case ICONST(integer=i) then i | ||
| 181 | case ENUM_LITERAL(index=i) then i | ||
| 182 | case CREF(componentRef=cr) then crefStr(cr) | ||
| 183 | end match | ||
| 184 | else "UNKNOWN_SUBSCRIPT" | ||
| 185 | end subscriptStr; | ||
| 186 | |||
| 187 | 61262 | template contextCref(ComponentRef cr, Context context, SimCode simCode, Text& extraFuncs, Text& extraFuncsDecl, Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) | |
| 188 | "Generates code for a component reference depending on which context we're in." | ||
| 189 | ::= | ||
| 190 | let &varDeclsCref = buffer "" /*BUFD*/ | ||
| 191 | match context | ||
| 192 | case FUNCTION_CONTEXT(__) then crefStr(cr) | ||
| 193 | else '<%cref1(cr, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, context, varDeclsCref, stateDerVectorName, useFlatArrayNotation)%>' | ||
| 194 | end contextCref; | ||
| 195 | |||
| 196 | 736 | template contextCref2(ComponentRef cr, Context context) | |
| 197 | "Generates code for a component reference depending on which context we're in." | ||
| 198 | ::= | ||
| 199 | match context | ||
| 200 | case FUNCTION_CONTEXT(__) then crefStr(cr) | ||
| 201 | else "" | ||
| 202 | end contextCref2; | ||
| 203 | |||
| 204 | 2951 | template contextFunName(String funName, Context context) | |
| 205 | "Generates a name in the Functions object depending on the context we're in." | ||
| 206 | ::= | ||
| 207 | match context | ||
| 208 | case FUNCTION_CONTEXT(__) then '<%funName%>' | ||
| 209 | else '_functions-><%funName%>' | ||
| 210 | end contextFunName; | ||
| 211 | |||
| 212 | 29 | template contextIteratorName(Ident name, Context context) | |
| 213 | "Generates code for an iterator variable." | ||
| 214 | ::= | ||
| 215 | System.unquoteIdentifier(name) + "_" | ||
| 216 | end contextIteratorName; | ||
| 217 | |||
| 218 | ✗ | template crefWithIndex(ComponentRef cr, Context context, Text &varDecls, SimCode simCode, Text& extraFuncs, Text& extraFuncsDecl, | |
| 219 | Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) | ||
| 220 | "Return cref with index for the lhs of a for loop, i.e., _resistori_P_i." | ||
| 221 | ::= | ||
| 222 | match cr | ||
| 223 | case CREF_QUAL(__) then | ||
| 224 | "_" + crefToCStrWithIndex(cr, context, varDecls, simCode, extraFuncs, extraFuncsDecl, extraFuncsNamespace, stateDerVectorName /*=__zDot*/, useFlatArrayNotation) | ||
| 225 | end match | ||
| 226 | end crefWithIndex; | ||
| 227 | |||
| 228 | ✗ | template crefToCStrWithIndex(ComponentRef cr, Context context, Text &varDecls, SimCode simCode, Text& extraFuncs, Text& extraFuncsDecl, | |
| 229 | Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) | ||
| 230 | "Helper function to crefWithIndex." | ||
| 231 | ::= | ||
| 232 | let &preExp = buffer "" | ||
| 233 | let tmp = "" | ||
| 234 | match cr | ||
| 235 | case CREF_QUAL(__) then | ||
| 236 | let identTmp = '<%System.unquoteIdentifier(ident)%>' | ||
| 237 | match listHead(subscriptLst) | ||
| 238 | case INDEX(__) then | ||
| 239 | match exp case e as CREF(__) then | ||
| 240 | let tmp = daeExpCrefRhs(e, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 241 | '<%identTmp%><%tmp%><%subscriptsToCStrForArray(subscriptLst)%>_P_<%crefToCStr(componentRef,useFlatArrayNotation)%>' | ||
| 242 | end match | ||
| 243 | end match | ||
| 244 | else "CREF_NOT_IDENT_OR_QUAL" | ||
| 245 | end crefToCStrWithIndex; | ||
| 246 | |||
| 247 | |||
| 248 | 41065 | template cref1(ComponentRef cr, SimCode simCode ,Text& extraFuncs,Text& extraFuncsDecl,Text extraFuncsNamespace, Context context, Text &varDecls, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) ::= | |
| 249 | match cr | ||
| 250 | case CREF_QUAL(ident = "$PRE") then | ||
| 251 | '_discrete_events->pre(<%cref1(componentRef, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, context, varDecls, stateDerVectorName, useFlatArrayNotation)%>)' | ||
| 252 | case CREF_IDENT(ident = "xloc") then | ||
| 253 | '<%representationCref(cr, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, context, varDecls, stateDerVectorName, useFlatArrayNotation)%>' | ||
| 254 | case CREF_IDENT(ident = "time") then | ||
| 255 | match context | ||
| 256 | case ALGLOOP_CONTEXT(genInitialisation=false) | ||
| 257 | then "_system->_simTime" | ||
| 258 | else "_simTime" | ||
| 259 | end match | ||
| 260 | case CREF_QUAL(ident = "$START") then '<%representationCref(componentRef, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, context, varDecls, stateDerVectorName, useFlatArrayNotation)%>' | ||
| 261 | else '<%representationCref(cr, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, context, varDecls, stateDerVectorName, useFlatArrayNotation)%>' | ||
| 262 | end cref1; | ||
| 263 | |||
| 264 | 42725 | template representationCref(ComponentRef inCref, SimCode simCode, Text& extraFuncs, Text& extraFuncsDecl, Text extraFuncsNamespace, Context context, Text &varDecls, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) ::= | |
| 265 | match context | ||
| 266 | case FUNCTION_CONTEXT() then | ||
| 267 | '<%crefStr(inCref)%>' | ||
| 268 | else | ||
| 269 | let representation = cref2simvar(inCref, simCode) |> var as SIMVAR(varKind=varKind, index=i, matrixName=matrixName) => | ||
| 270 | match varKind | ||
| 271 | /* don't use __zDot due to SIMVAR indexing of new backend | ||
| 272 | case STATE() then | ||
| 273 | '__z[<%i%>]' | ||
| 274 | case STATE_DER() then | ||
| 275 | '__zDot[<%i%>]' | ||
| 276 | */ | ||
| 277 | case DAE_RESIDUAL_VAR() then | ||
| 278 | '__daeResidual[<%i%>]' | ||
| 279 | case JAC_VAR() then | ||
| 280 | '<%contextSystem(context)%>_<%getOption(matrixName)%>jac_y(<%i%>)' | ||
| 281 | case JAC_TMP_VAR() then | ||
| 282 | '<%contextSystem(context)%>_<%getOption(matrixName)%>jac_tmp(<%i%>)' | ||
| 283 | case SEED_VAR() then | ||
| 284 | '<%contextSystem(context)%>_<%getOption(matrixName)%>jac_x(<%i%>)' | ||
| 285 | case VARIABLE() then | ||
| 286 | match var | ||
| 287 | case SIMVAR(index=-2) then | ||
| 288 | // unknown in cref2simvar, e.g. local in a function, iterator or time | ||
| 289 | '<%localCref(inCref, useFlatArrayNotation)%>' | ||
| 290 | else | ||
| 291 | match context | ||
| 292 | case ALGLOOP_CONTEXT(genInitialisation = false, genJacobian=false) then | ||
| 293 | '_system-><%cref(inCref, useFlatArrayNotation)%>' | ||
| 294 | case ALGLOOP_CONTEXT(genInitialisation = false, genJacobian=true) then | ||
| 295 | '_system->_<%crefToCStr(inCref,false)%>' | ||
| 296 | case JACOBIAN_CONTEXT() then | ||
| 297 | '_<%crefToCStr(inCref, false)%>' | ||
| 298 | else | ||
| 299 | '<%cref(inCref, useFlatArrayNotation)%>' | ||
| 300 | else | ||
| 301 | '<%contextSystem(context)%><%cref(inCref, useFlatArrayNotation)%>' | ||
| 302 | '<%representation%>' | ||
| 303 | end representationCref; | ||
| 304 | |||
| 305 | |||
| 306 | 108139 | template arraycref(ComponentRef cr, Boolean useFlatArrayNotation) | |
| 307 | ::= | ||
| 308 | match cr | ||
| 309 | case CREF_IDENT(ident = "xloc") then crefStr(cr) | ||
| 310 | case CREF_IDENT(ident = "time") then "_simTime" | ||
| 311 | case WILD(__) then '' | ||
| 312 | else "_"+crefToCStr1(cr, useFlatArrayNotation) | ||
| 313 | end arraycref; | ||
| 314 | |||
| 315 | |||
| 316 | 427976 | template arraycref2(ComponentRef cr, Text& dims) | |
| 317 | ::= | ||
| 318 | match cr | ||
| 319 | case CREF_IDENT(ident = "xloc") then crefStr(cr) | ||
| 320 | case CREF_IDENT(ident = "time") then "_simTime" | ||
| 321 | case WILD(__) then '' | ||
| 322 | else "_"+crefToCStrForArray(cr,dims) | ||
| 323 | end arraycref2; | ||
| 324 | |||
| 325 | ✗ | template cref2(ComponentRef cr, Boolean useFlatArrayNotation) | |
| 326 | "Generates C equivalent name for component reference." | ||
| 327 | ::= | ||
| 328 | match cr | ||
| 329 | case CREF_IDENT(ident = "xloc") then '<%crefStr(cr)%>' | ||
| 330 | case CREF_IDENT(ident = "time") then "_simTime" | ||
| 331 | case WILD(__) then '' | ||
| 332 | else "_"+crefToCStr(cr,useFlatArrayNotation) | ||
| 333 | end cref2; | ||
| 334 | |||
| 335 | 703570 | template crefToCStr(ComponentRef cr, Boolean useFlatArrayNotation) | |
| 336 | "Helper function to cref." | ||
| 337 | ::= | ||
| 338 | match cr | ||
| 339 | case CREF_IDENT(__) then | ||
| 340 | let subs = if Flags.isSet(Flags.NF_SCALARIZE) then subscriptsToCStr(subscriptLst, useFlatArrayNotation) | ||
| 341 | '<%System.unquoteIdentifier(ident)%>_<%subs%>' | ||
| 342 | case CREF_QUAL(__) then | ||
| 343 | let subs = if Flags.isSet(Flags.NF_SCALARIZE) then subscriptsToCStrForArray(subscriptLst) | ||
| 344 | '<%System.unquoteIdentifier(ident)%><%subs%>_P_<%crefToCStr(componentRef, useFlatArrayNotation)%>' | ||
| 345 | case WILD(__) then '' | ||
| 346 | else "CREF_NOT_IDENT_OR_QUAL" | ||
| 347 | end crefToCStr; | ||
| 348 | |||
| 349 | 7065 | template contextSystem(Context context) | |
| 350 | "Dereference _system in algloop context" | ||
| 351 | ::= | ||
| 352 | match context | ||
| 353 | case ALGLOOP_CONTEXT(genInitialisation = false) then | ||
| 354 | '_system->' | ||
| 355 | else | ||
| 356 | '' | ||
| 357 | end contextSystem; | ||
| 358 | |||
| 359 | 54256 | template daeExpCrefRhs(Exp exp, Context context, Text &preExp, Text &varDecls, SimCode simCode, Text& extraFuncs, | |
| 360 | Text& extraFuncsDecl, Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) | ||
| 361 | "Generates code for a component reference on the right hand side of an | ||
| 362 | expression." | ||
| 363 | ::= | ||
| 364 | match exp | ||
| 365 | // A record cref without subscripts (i.e. a record instance) is handled | ||
| 366 | // by daeExpRecordCrefRhs only in a simulation context, not in a function. | ||
| 367 | case CREF(componentRef = cr, ty = t as T_COMPLEX(complexClassType = RECORD(path = _))) then | ||
| 368 | match context case FUNCTION_CONTEXT(__) then | ||
| 369 | '<%daeExpCref(false, exp, context, &preExp, &varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)%>' | ||
| 370 | else | ||
| 371 | daeExpRecordCrefRhs(t, cr, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 372 | case CREF(ty = T_FUNCTION_REFERENCE_FUNC(functionType=t)) then | ||
| 373 | functionClosure(underscorePath(crefToPathIgnoreSubs(componentRef)), "", t, t, context, &extraFuncsDecl) | ||
| 374 | case CREF(componentRef = CREF_IDENT(ident=ident), ty = T_FUNCTION_REFERENCE_VAR(__)) then | ||
| 375 | contextFunName(ident, context) | ||
| 376 | else | ||
| 377 | daeExpCref(false, exp, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 378 | end daeExpCrefRhs; | ||
| 379 | |||
| 380 | |||
| 381 | 57891 | template daeExpCref(Boolean isLhs, Exp ecr, Context context, Text &preExp, Text &varDecls, SimCode simCode, Text& extraFuncs, | |
| 382 | Text& extraFuncsDecl, Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) | ||
| 383 | "Generates code for a component reference." | ||
| 384 | ::= | ||
| 385 | match ecr | ||
| 386 | case component as CREF(componentRef=cr, ty=T_FUNCTION(path=name)) then | ||
| 387 | underscorePath(name) | ||
| 388 | case component as CREF(componentRef=cr, ty=ty) then | ||
| 389 | let box = daeExpCrefRhsArrayBox(cr, ty, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 390 | if box then | ||
| 391 | box | ||
| 392 | else if crefIsScalar(cr, context) then | ||
| 393 | contextCref(cr, context, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 394 | else | ||
| 395 | if boolAnd(intEq(listLength(crefSubs(cr)), listLength(crefDims(cr))), crefSubIsScalar(cr)) then | ||
| 396 | // The array subscript results in a scalar | ||
| 397 | let arrName = contextCref(crefStripLastSubs(cr), context,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 398 | let arrayType = expTypeShort(ty) | ||
| 399 | let dimsValuesStr = (crefSubs(cr) |> INDEX(__) => | ||
| 400 | daeExp(exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 401 | ;separator=",") | ||
| 402 | match arrayType | ||
| 403 | case "metatype_array" then | ||
| 404 | 'arrayGet(<%arrName%>,<%dimsValuesStr%>) /* DAE.CREF */' | ||
| 405 | else | ||
| 406 | << | ||
| 407 | <%arrName%>(<%dimsValuesStr%>) | ||
| 408 | >> | ||
| 409 | else | ||
| 410 | // The array subscript denotes a slice | ||
| 411 | let arrName = contextArrayCref(cr, context) | ||
| 412 | let arrTypeStr = if isLhs then 'ArraySlice' else 'ArraySliceConst' | ||
| 413 | let elTypeStr = expTypeShort(ty) | ||
| 414 | let slice = daeExpCrefIndexSpec(crefSubs(cr), context, &preExp, | ||
| 415 | &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, | ||
| 416 | stateDerVectorName, useFlatArrayNotation) | ||
| 417 | let &preExp += '<%arrTypeStr%><<%elTypeStr%>> <%slice%>_as(<%arrName%>, <%slice%>);<%\n%>' | ||
| 418 | '<%slice%>_as' | ||
| 419 | end daeExpCref; | ||
| 420 | |||
| 421 | |||
| 422 | 27 | template daeExpCrefIndexSpec(list<DAE.Subscript> subs, Context context, | |
| 423 | Text &preExp, Text &varDecls, SimCode simCode, | ||
| 424 | Text& extraFuncs, Text& extraFuncsDecl, Text extraFuncsNamespace, | ||
| 425 | Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) | ||
| 426 | "Generates index spec of an array as temporary vector<Slice>." | ||
| 427 | ::= | ||
| 428 | let tmp_slice = tempDecl("vector<Slice>", &varDecls /*BUFD*/) | ||
| 429 | let &preExp += '<%tmp_slice%>.clear();<%\n%>' | ||
| 430 | let _ = (subs |> sub hasindex i1 => | ||
| 431 | match sub | ||
| 432 | case INDEX(__) then | ||
| 433 | let expPart = daeExp(exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 434 | let &preExp += '<%tmp_slice%>.push_back(Slice(<%expPart%>));<%\n%>' | ||
| 435 | '' | ||
| 436 | case WHOLEDIM(__) then | ||
| 437 | let &preExp += '<%tmp_slice%>.push_back(Slice());<%\n%>' | ||
| 438 | '' | ||
| 439 | case SLICE(__) then | ||
| 440 | match exp | ||
| 441 | case RANGE(__) then | ||
| 442 | let start_exp = daeExp(start, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 443 | let stop_exp = daeExp(stop, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 444 | let step_exp = match step case SOME(stepExp) then daeExp(stepExp, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) else "1" | ||
| 445 | let &preExp += '<%tmp_slice%>.push_back(Slice(<%start_exp%>, <%step_exp%>, <%stop_exp%>));<%\n%>' | ||
| 446 | '' | ||
| 447 | else | ||
| 448 | // default branch if exp is no range | ||
| 449 | let expPart = daeExp(exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 450 | let &preExp += '<%tmp_slice%>.push_back(Slice(<%expPart%>));<%\n%>' | ||
| 451 | '' | ||
| 452 | end match | ||
| 453 | ;separator="\n ") | ||
| 454 | '<%tmp_slice%>' | ||
| 455 | end daeExpCrefIndexSpec; | ||
| 456 | |||
| 457 | 57891 | template daeExpCrefRhsArrayBox(ComponentRef cr, DAE.Type ty, Context context, Text &preExp, | |
| 458 | Text &varDecls, SimCode simCode, Text& extraFuncs, Text& extraFuncsDecl, | ||
| 459 | Text extraFuncsNamespace, Text stateDerVectorName, Boolean useFlatArrayNotation) | ||
| 460 | "Helper to daeExpCrefRhs." | ||
| 461 | ::= | ||
| 462 | match context | ||
| 463 | case FUNCTION_CONTEXT(__) then | ||
| 464 | '' | ||
| 465 | else | ||
| 466 | let box = cref2simvar(cr, simCode) |> var as SIMVAR(index=i, matrixName=matrixName) => | ||
| 467 | match varKind | ||
| 468 | /* don't use __zDot due to SIMVAR indexing of new backend | ||
| 469 | case STATE() | ||
| 470 | case STATE_DER() | ||
| 471 | */ | ||
| 472 | case DAE_RESIDUAL_VAR() then | ||
| 473 | let arrdata = representationCref(cr, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, context, varDecls, stateDerVectorName, useFlatArrayNotation) | ||
| 474 | daeExpCrefRhsArrayBox2(arrdata, ty, false, context, preExp, varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace) | ||
| 475 | case JAC_VAR() | ||
| 476 | case JAC_TMP_VAR() | ||
| 477 | case SEED_VAR() then | ||
| 478 | let arrdata = representationCref(cr, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, context, varDecls, stateDerVectorName, useFlatArrayNotation) | ||
| 479 | daeExpCrefRhsArrayBox2(arrdata, ty, true, context, preExp, varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace) | ||
| 480 | else | ||
| 481 | match ty | ||
| 482 | case t as T_ARRAY(ty=aty, dims=dims) then | ||
| 483 | match cr | ||
| 484 | case CREF_QUAL(ident = "$PRE") then | ||
| 485 | let arr = arrayCrefCStr(componentRef, context) | ||
| 486 | let ndims = listLength(dims) | ||
| 487 | let dimstr = checkDimension(dims) | ||
| 488 | let T = expTypeShort(aty) | ||
| 489 | let &preExp += | ||
| 490 | << | ||
| 491 | StatArrayDim<%ndims%><<%T%>, <%dimstr%>> <%arr%>_pre; | ||
| 492 | std::transform(<%arr%>.getData(), | ||
| 493 | <%arr%>.getData() + <%arr%>.getNumElems(), | ||
| 494 | <%arr%>_pre.getData(), | ||
| 495 | PreArray2CArray<<%T%>>(_discrete_events)); | ||
| 496 | >> | ||
| 497 | '<%arr%>_pre' | ||
| 498 | else | ||
| 499 | '' | ||
| 500 | else '' | ||
| 501 | '<%box%>' | ||
| 502 | end daeExpCrefRhsArrayBox; | ||
| 503 | |||
| 504 | |||
| 505 | 1473 | template daeExpCrefRhsArrayBox2(Text arrayData, DAE.Type ty, Boolean isRowMajorData, Context context, Text &preExp /*BUFP*/, | |
| 506 | Text &varDecls /*BUFP*/, SimCode simCode, Text& extraFuncs, Text& extraFuncsDecl, Text extraFuncsNamespace) | ||
| 507 | ::= | ||
| 508 | match ty | ||
| 509 | case t as T_ARRAY(ty=aty,dims=dims) then | ||
| 510 | let dimstr = checkDimension(dims) | ||
| 511 | let arrayType = match dimstr | ||
| 512 | case "" then 'DynArrayDim<%listLength(dims)%><<%expTypeShort(ty)%>>' | ||
| 513 | else 'StatArrayDim<%listLength(dims)%><<%expTypeShort(ty)%>,<%dimstr%>>' | ||
| 514 | end match | ||
| 515 | let &tmpdecl = buffer "" /*BUFD*/ | ||
| 516 | let arrayVar = tempDecl(arrayType, &tmpdecl /*BUFD*/) | ||
| 517 | let arrayAssign = if isRowMajorData then | ||
| 518 | 'assignRowMajorData(&<%arrayData%>, <%arrayVar%>)' else | ||
| 519 | '<%arrayVar%>.assign(&<%arrayData%>)' | ||
| 520 | let &preExp += | ||
| 521 | << | ||
| 522 | <%arrayType%> <%arrayVar%>; | ||
| 523 | <%arrayAssign%>;<%\n%> | ||
| 524 | >> | ||
| 525 | arrayVar | ||
| 526 | else | ||
| 527 | arrayData | ||
| 528 | end daeExpCrefRhsArrayBox2; | ||
| 529 | |||
| 530 | 3 | template daeExpRecordCrefRhs(DAE.Type ty, ComponentRef cr, Context context, Text &preExp, Text &varDecls, SimCode simCode, Text& extraFuncs, | |
| 531 | Text& extraFuncsDecl, Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) | ||
| 532 | ::= | ||
| 533 | match ty | ||
| 534 | case T_COMPLEX(complexClassType = record_state, varLst = var_lst) then | ||
| 535 | let vars = var_lst |> v => daeExp(makeCrefRecordExp(cr,v), context, &preExp, &varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 536 | ;separator=", " | ||
| 537 | let record_type_name = underscorePath(ClassInfUtil.getStateName(record_state)) | ||
| 538 | let ret_type = '<%record_type_name%>RetType' | ||
| 539 | let ret_var = tempDecl(ret_type, &varDecls) | ||
| 540 | let &preExp += '<%contextFunName(record_type_name, context)%>(<%vars%>,<%ret_var%>);<%\n%>' | ||
| 541 | '<%ret_var%>' | ||
| 542 | end daeExpRecordCrefRhs; | ||
| 543 | |||
| 544 | ✗ | template crefST(ComponentRef cr, Boolean useFlatArrayNotation) | |
| 545 | "Generates C equivalent name for component reference." | ||
| 546 | ::= | ||
| 547 | match cr | ||
| 548 | case CREF_IDENT(ident = "time") then "_simTime" | ||
| 549 | case WILD(__) then '' | ||
| 550 | else crefToCStr(cr, useFlatArrayNotation) | ||
| 551 | end crefST; | ||
| 552 | |||
| 553 | 28 | template contextArrayCref(ComponentRef cr, Context context) | |
| 554 | "Generates code for an array component reference depending on the context." | ||
| 555 | ::= | ||
| 556 | match cr | ||
| 557 | // No separate variable is generated for a start value, so refer to the variable itself, | ||
| 558 | // the same way cref1 resolves a scalar $START cref. | ||
| 559 | case CREF_QUAL(ident = "$START") then contextArrayCref(componentRef, context) | ||
| 560 | else | ||
| 561 | match context | ||
| 562 | case FUNCTION_CONTEXT(__) then arrayCrefStr(cr) | ||
| 563 | else arrayCrefCStr(cr,context) | ||
| 564 | end contextArrayCref; | ||
| 565 | |||
| 566 | 14 | template arrayCrefStr(ComponentRef cr) | |
| 567 | ::= | ||
| 568 | match cr | ||
| 569 | case CREF_IDENT(ident = "time") then "_simTime" | ||
| 570 | case CREF_IDENT(__) then '<%System.unquoteIdentifier(ident)%>_' | ||
| 571 | case CREF_QUAL(__) then '<%System.unquoteIdentifier(ident)%>_.<%arrayCrefStr(componentRef)%>' | ||
| 572 | else "CREF_NOT_IDENT_OR_QUAL" | ||
| 573 | end arrayCrefStr; | ||
| 574 | |||
| 575 | 16 | template arrayCrefCStr(ComponentRef cr,Context context) | |
| 576 | ::= | ||
| 577 | match context | ||
| 578 | case ALGLOOP_CONTEXT(genInitialisation = false) then | ||
| 579 | let& dims = buffer "" /*BUFD*/ | ||
| 580 | << _system->_<%crefToCStrForArray(cr,dims)%> >> | ||
| 581 | else | ||
| 582 | let& dims = buffer "" /*BUFD*/ | ||
| 583 | '_<%crefToCStrForArray(cr,dims)%>' | ||
| 584 | end arrayCrefCStr; | ||
| 585 | |||
| 586 | ✗ | template arrayCrefCStr2(ComponentRef cr) | |
| 587 | ::= | ||
| 588 | match cr | ||
| 589 | case CREF_IDENT(__) then '<%unquoteIdentifier(ident)%>' | ||
| 590 | case CREF_QUAL(__) then '<%unquoteIdentifier(ident)%>_P_<%arrayCrefCStr2(componentRef)%>' | ||
| 591 | else "CREF_NOT_IDENT_OR_QUAL" | ||
| 592 | end arrayCrefCStr2; | ||
| 593 | |||
| 594 | ✗ | template crefTypeST(ComponentRef cr) "template crefType | |
| 595 | Like cref but with cast if type is integer." | ||
| 596 | ::= | ||
| 597 | match cr | ||
| 598 | case CREF_IDENT(__) then '<%expTypeShortSPS(identType)%>' | ||
| 599 | case CREF_QUAL(__) then '<%crefTypeST(componentRef)%>' | ||
| 600 | else "crefType:ERROR" | ||
| 601 | end match | ||
| 602 | end crefTypeST; | ||
| 603 | |||
| 604 | ✗ | template crefTypeMLPI(ComponentRef cr) "template crefType | |
| 605 | Like cref but with cast if type is integer." | ||
| 606 | ::= | ||
| 607 | match cr | ||
| 608 | case CREF_IDENT(__) then '<%expTypeShortMLPI(identType)%>' | ||
| 609 | case CREF_QUAL(__) then '<%crefTypeMLPI(componentRef)%>' | ||
| 610 | else "crefType:ERROR" | ||
| 611 | end match | ||
| 612 | end crefTypeMLPI; | ||
| 613 | |||
| 614 | |||
| 615 | 23088 | template crefStartValueType(ComponentRef cr) | |
| 616 | "Returns type string for get/set<type>StartValue methods." | ||
| 617 | ::= | ||
| 618 | match cr | ||
| 619 | case CREF_IDENT(__) then | ||
| 620 | let typeShort = expTypeShort(identType) | ||
| 621 | let typeString = if stringEq(typeShort, "double") then "Real" | ||
| 622 | else if stringEq(typeShort, "int") then "Int" | ||
| 623 | else if stringEq(typeShort, "bool") then "Bool" | ||
| 624 | else if stringEq(typeShort, "string") then "String" | ||
| 625 | else if stringEq(typeShort, "void*") then "ExternalObject" | ||
| 626 | else 'ERROR:crefStartValueType <%typeShort%> ' | ||
| 627 | '<%typeString%>' | ||
| 628 | case CREF_QUAL(__) then | ||
| 629 | '<%crefStartValueType(componentRef)%>' | ||
| 630 | else | ||
| 631 | 'crefStartValueType:ERROR' | ||
| 632 | end match | ||
| 633 | end crefStartValueType; | ||
| 634 | |||
| 635 | /******************************************************************************************************************************************************* | ||
| 636 | * end of cref to string template functions | ||
| 637 | *******************************************************************************************************************************************************/ | ||
| 638 | |||
| 639 | |||
| 640 | /******************************************************************************************************************************************************* | ||
| 641 | * type to string template functions | ||
| 642 | *******************************************************************************************************************************************************/ | ||
| 643 | |||
| 644 | 246 | template dimension(Dimension d,Context context) | |
| 645 | ::= | ||
| 646 | match d | ||
| 647 | case DAE.DIM_BOOLEAN(__) then '2' | ||
| 648 | case DAE.DIM_INTEGER(__) then integer | ||
| 649 | case DAE.DIM_ENUM(__) then size | ||
| 650 | case DAE.DIM_EXP(exp=e) then dimensionExp(e,context, false) | ||
| 651 | case DAE.DIM_UNKNOWN(__) then '-1'//error(sourceInfo(),"Unknown dimensions may not be part of generated code. This is most likely an error on the part of OpenModelica. Please submit a detailed bug-report.") | ||
| 652 | else error(sourceInfo(), 'dimension: INVALID_DIMENSION') | ||
| 653 | end dimension; | ||
| 654 | |||
| 655 | 1788 | template checkDimension(Dimensions dims) | |
| 656 | ::= | ||
| 657 | let dimstr = dimensionsList(dims) |> dim as Integer => '<%dim%>';separator="," | ||
| 658 | // check for unknown dimension that is treated as -1 | ||
| 659 | if intEq(-1, stringFind(dimstr, "-")) then | ||
| 660 | dimstr | ||
| 661 | else | ||
| 662 | "" | ||
| 663 | end checkDimension; | ||
| 664 | |||
| 665 | ✗ | template checkExpDimension(list<DAE.Exp> dims) | |
| 666 | ::= | ||
| 667 | expDimensionsList(dims) |> dim as Integer => '<%dim%>';separator="," | ||
| 668 | end checkExpDimension; | ||
| 669 | |||
| 670 | 454 | template listDimsFlat(Dimensions dims, Type elty) | |
| 671 | "return list of dimensions of form 'd1, d2, ..., dn', flattening subarrays" | ||
| 672 | ::= | ||
| 673 | let dimstr = checkDimension(dims) | ||
| 674 | match dimstr | ||
| 675 | case "" then | ||
| 676 | '' | ||
| 677 | else | ||
| 678 | match elty | ||
| 679 | case T_ARRAY(dims=subdims, ty=subty) then | ||
| 680 | let subdimstr = listDimsFlat(subdims, subty) | ||
| 681 | match subdimstr | ||
| 682 | case "" then | ||
| 683 | '' | ||
| 684 | else | ||
| 685 | '<%dimstr%>, <%subdimstr%>' | ||
| 686 | end match | ||
| 687 | else | ||
| 688 | dimstr | ||
| 689 | end listDimsFlat; | ||
| 690 | |||
| 691 | 454 | template nDimsFlat(Dimensions dims, Type elty, Integer offset) | |
| 692 | "return number of dimensions, flattening subarrays" | ||
| 693 | ::= | ||
| 694 | match elty | ||
| 695 | case T_ARRAY(dims=subdims, ty=subty) then | ||
| 696 | nDimsFlat(subdims, subty, intAdd(listLength(dims), offset)) | ||
| 697 | else | ||
| 698 | intAdd(listLength(dims), offset) | ||
| 699 | end nDimsFlat; | ||
| 700 | |||
| 701 | 30717 | template expTypeShort(DAE.Type type) | |
| 702 | "Returns the base type name for declarations" | ||
| 703 | ::= | ||
| 704 | match type | ||
| 705 | case T_INTEGER(__) then "int" | ||
| 706 | case T_REAL(__) then "double" | ||
| 707 | case T_STRING(__) then if acceptMetaModelicaGrammar() then "metatype" else "string" | ||
| 708 | case T_BOOL(__) then "bool" | ||
| 709 | case T_ENUMERATION(__) then "int" | ||
| 710 | case T_UNKNOWN(__) then "double /*W1*/" // assumming real for unknown type | ||
| 711 | case T_ANYTYPE(__) then "complex2" | ||
| 712 | case T_SUBTYPE_BASIC(__) then expTypeShort(complexType) | ||
| 713 | case T_ARRAY(__) then expTypeShort(ty) | ||
| 714 | case T_COMPLEX(complexClassType=EXTERNAL_OBJ(__)) then "void*" | ||
| 715 | case T_COMPLEX(__) then '<%underscorePath(ClassInfUtil.getStateName(complexClassType))%>Type' | ||
| 716 | case T_METATYPE(__) | ||
| 717 | case T_METABOXED(__) then "metatype" | ||
| 718 | case T_FUNCTION(__) | ||
| 719 | case T_FUNCTION_REFERENCE_FUNC(__) | ||
| 720 | case T_FUNCTION_REFERENCE_VAR(__) then "fnptr" | ||
| 721 | else 'expTypeShort:ERROR <%unparseType(type)%> ' | ||
| 722 | end expTypeShort; | ||
| 723 | |||
| 724 | 13286 | template expTypeFlag(DAE.Type ty, Integer flag) | |
| 725 | "Returns code for a type, depending on flag" | ||
| 726 | ::= | ||
| 727 | match flag | ||
| 728 | case 1 then | ||
| 729 | // we want the short type | ||
| 730 | expTypeShort(ty) | ||
| 731 | case 2 then | ||
| 732 | // we want the "modelica type" | ||
| 733 | match ty case T_COMPLEX(complexClassType=EXTERNAL_OBJ(__)) then | ||
| 734 | '<%expTypeShort(ty)%>' | ||
| 735 | else match ty case T_COMPLEX(complexClassType=RECORD(path=rname)) then | ||
| 736 | '<%underscorePath(rname)%>Type' | ||
| 737 | else match ty case T_COMPLEX(__) then | ||
| 738 | '<%underscorePath(ClassInfUtil.getStateName(complexClassType))%>' | ||
| 739 | else | ||
| 740 | '<%expTypeShort(ty)%>' | ||
| 741 | case 3 then | ||
| 742 | // we want the "array type", static if dims are known, dynamic otherwise | ||
| 743 | match ty | ||
| 744 | case T_ARRAY(ty=elty, dims=dims) then | ||
| 745 | expTypeArrayDims(elty, dims) | ||
| 746 | else | ||
| 747 | 'ERROR:expTypeFlag3 no array' | ||
| 748 | end match | ||
| 749 | case 4 then | ||
| 750 | // we want the "array type" only if type is array, otherwise "modelica type" | ||
| 751 | match ty | ||
| 752 | case T_ARRAY(ty=elty, dims=dims) then | ||
| 753 | expTypeArrayDims(elty, dims) | ||
| 754 | else | ||
| 755 | expTypeFlag(ty, 2) | ||
| 756 | end match | ||
| 757 | case 5 then | ||
| 758 | match ty | ||
| 759 | case T_ARRAY(dims=dims) then | ||
| 760 | //let testbasearray = dims |> dim => '<%testdimension(dim)%>' ;separator='' | ||
| 761 | let dimstr = checkDimension(dims) | ||
| 762 | match dimstr | ||
| 763 | case "" then 'const DynArrayDim<%listLength(dims)%><<%expTypeShort(ty)%>>&' | ||
| 764 | else 'const StatArrayDim<%listLength(dims)%><<%expTypeShort(ty)%>,<%dimstr%>>&' | ||
| 765 | else expTypeFlag(ty, 2) | ||
| 766 | end match | ||
| 767 | |||
| 768 | case 6 then | ||
| 769 | expTypeFlag(ty, 4) | ||
| 770 | |||
| 771 | case 7 then | ||
| 772 | match ty | ||
| 773 | case T_ARRAY(dims=dims) | ||
| 774 | then | ||
| 775 | 'multi_array<<%expTypeShort(ty)%>,<%listLength(dims)%>>' | ||
| 776 | end match | ||
| 777 | |||
| 778 | case 8 then | ||
| 779 | match ty | ||
| 780 | case T_ARRAY(dims=dims) then 'const BaseArray<<%expTypeShort(ty)%>>&' | ||
| 781 | else expTypeFlag(ty, 9) | ||
| 782 | end match | ||
| 783 | |||
| 784 | case 9 then | ||
| 785 | // we want the "modelica type" | ||
| 786 | match ty case T_COMPLEX(complexClassType=EXTERNAL_OBJ(__)) then | ||
| 787 | '<%expTypeShort(ty)%>' | ||
| 788 | else match ty case T_COMPLEX(complexClassType=RECORD(path=rname)) then | ||
| 789 | 'const <%underscorePath(rname)%>Type&' | ||
| 790 | else match ty case T_COMPLEX(__) then | ||
| 791 | 'const <%underscorePath(ClassInfUtil.getStateName(complexClassType))%>&' | ||
| 792 | else | ||
| 793 | '<%expTypeShort(ty)%>' | ||
| 794 | |||
| 795 | end expTypeFlag; | ||
| 796 | |||
| 797 | ✗ | template crefType(ComponentRef cr) "template crefType | |
| 798 | Like cref but with cast if type is integer." | ||
| 799 | ::= | ||
| 800 | match cr | ||
| 801 | case CREF_IDENT(__) then expTypeArrayIf(identType) | ||
| 802 | case CREF_QUAL(__) then crefType(componentRef) | ||
| 803 | else "crefType:ERROR" | ||
| 804 | end match | ||
| 805 | end crefType; | ||
| 806 | |||
| 807 | 258 | template expTypeFromExpShort(Exp exp) | |
| 808 | |||
| 809 | ::= | ||
| 810 | expTypeFromExpFlag(exp, 1) | ||
| 811 | end expTypeFromExpShort; | ||
| 812 | |||
| 813 | 1012 | template expTypeFromExpModelica(Exp exp) | |
| 814 | |||
| 815 | ::= | ||
| 816 | expTypeFromExpFlag(exp, 2) | ||
| 817 | end expTypeFromExpModelica; | ||
| 818 | |||
| 819 | 9 | template expTypeFromExpArray(Exp exp) | |
| 820 | |||
| 821 | ::= | ||
| 822 | expTypeFromExpFlag(exp, 4) | ||
| 823 | end expTypeFromExpArray; | ||
| 824 | |||
| 825 | ✗ | template expTypeShortSPS(DAE.Type type) | |
| 826 | ::= | ||
| 827 | match type | ||
| 828 | case T_INTEGER(__) then "INT" | ||
| 829 | case T_REAL(__) then "LREAL" | ||
| 830 | case T_STRING(__) then if acceptMetaModelicaGrammar() then "metatype" else "string" | ||
| 831 | case T_BOOL(__) then "BOOL" | ||
| 832 | case T_ENUMERATION(__) then "INT" | ||
| 833 | /* assumming real for uknown type! */ | ||
| 834 | case T_UNKNOWN(__) then "LREAL" | ||
| 835 | case T_ANYTYPE(__) then "type not supported" | ||
| 836 | case T_ARRAY(__) then expTypeShortSPS(ty) | ||
| 837 | case T_COMPLEX(complexClassType=EXTERNAL_OBJ(__)) | ||
| 838 | then "type not supported" | ||
| 839 | case T_COMPLEX(__) then '<%underscorePath(ClassInfUtil.getStateName(complexClassType))%>Type' | ||
| 840 | case T_METATYPE(__) case T_METABOXED(__) then "type not supported" | ||
| 841 | case T_FUNCTION(__) | ||
| 842 | case T_FUNCTION_REFERENCE_FUNC(__) | ||
| 843 | case T_FUNCTION_REFERENCE_VAR(__) then "type not supported" | ||
| 844 | else "expTypeShort:ERROR" | ||
| 845 | end expTypeShortSPS; | ||
| 846 | |||
| 847 | ✗ | template expTypeShortMLPI(DAE.Type type) | |
| 848 | ::= | ||
| 849 | match type | ||
| 850 | case T_INTEGER(__) then "MLPI_IEC_INT" | ||
| 851 | case T_REAL(__) then "MLPI_IEC_LREAL" | ||
| 852 | case T_STRING(__) then if acceptMetaModelicaGrammar() then "metatype" else "string" | ||
| 853 | case T_BOOL(__) then "MLPI_IEC_BOOL" | ||
| 854 | case T_ENUMERATION(__) then "MLPI_IEC_INT" | ||
| 855 | /* assumming real for uknown type! */ | ||
| 856 | case T_UNKNOWN(__) then "MLPI_IEC_LREAL" | ||
| 857 | case T_ANYTYPE(__) then "type not supported" | ||
| 858 | case T_ARRAY(__) then expTypeShortSPS(ty) | ||
| 859 | case T_COMPLEX(complexClassType=EXTERNAL_OBJ(__)) | ||
| 860 | then "type not supported" | ||
| 861 | case T_COMPLEX(__) then '<%underscorePath(ClassInfUtil.getStateName(complexClassType))%>Type' | ||
| 862 | case T_METATYPE(__) case T_METABOXED(__) then "type not supported" | ||
| 863 | case T_FUNCTION(__) | ||
| 864 | case T_FUNCTION_REFERENCE_FUNC(__) | ||
| 865 | case T_FUNCTION_REFERENCE_VAR(__) then "type not supported" | ||
| 866 | else "expTypeShort:ERROR" | ||
| 867 | end expTypeShortMLPI; | ||
| 868 | |||
| 869 | 20 | template expType(DAE.Type ty, Boolean isArray) | |
| 870 | "Generate type helper." | ||
| 871 | ::= | ||
| 872 | if isArray | ||
| 873 | then 'expType_<%expTypeArray1(ty,0)%>_NOT_YET' | ||
| 874 | else expTypeShort(ty) | ||
| 875 | end expType; | ||
| 876 | |||
| 877 | ✗ | template expTypeModelica(DAE.Type ty) | |
| 878 | "Generate type helper." | ||
| 879 | ::= | ||
| 880 | expTypeFlag(ty, 2) | ||
| 881 | end expTypeModelica; | ||
| 882 | |||
| 883 | ✗ | template expTypeArray(DAE.Type ty) | |
| 884 | "Returns the array type" | ||
| 885 | ::= | ||
| 886 | expTypeFlag(ty, 3) | ||
| 887 | end expTypeArray; | ||
| 888 | |||
| 889 | 2096 | template expTypeArrayIf(DAE.Type ty) | |
| 890 | "Generate type helper." | ||
| 891 | ::= | ||
| 892 | expTypeFlag(ty, 4) | ||
| 893 | end expTypeArrayIf; | ||
| 894 | |||
| 895 | ✗ | template expTypeArray1(DAE.Type ty, Integer dims) ::= | |
| 896 | << | ||
| 897 | SimArray<%dims%><<%expTypeShort(ty)%>> | ||
| 898 | >> | ||
| 899 | end expTypeArray1; | ||
| 900 | |||
| 901 | 454 | template expTypeArrayDims(DAE.Type elty, DAE.Dimensions dims) | |
| 902 | "Generate type string for static or dynamic array, depending on dims" | ||
| 903 | ::= | ||
| 904 | let typeShort = expTypeShort(elty) | ||
| 905 | let dimstr = listDimsFlat(dims, elty) | ||
| 906 | match dimstr | ||
| 907 | case "" then | ||
| 908 | 'DynArrayDim<%nDimsFlat(dims, elty, 0)%><<%typeShort%>>' | ||
| 909 | else | ||
| 910 | 'StatArrayDim<%nDimsFlat(dims, elty, 0)%><<%typeShort%>, <%dimstr%>>' | ||
| 911 | end match | ||
| 912 | end expTypeArrayDims; | ||
| 913 | |||
| 914 | ✗ | template allocateDimensions(DAE.Type ty,Context context) | |
| 915 | ::= | ||
| 916 | match ty | ||
| 917 | case T_ARRAY(dims=dims) then | ||
| 918 | let dimstr = dims |> dim => '<%dimension(dim,context)%>' ;separator=',' | ||
| 919 | << | ||
| 920 | <%dimstr%> | ||
| 921 | >> | ||
| 922 | |||
| 923 | end allocateDimensions; | ||
| 924 | |||
| 925 | |||
| 926 | |||
| 927 | /******************************************************************************************************************************************************* | ||
| 928 | * end of type to string template functions | ||
| 929 | ********************************************************************************************************************************************************/ | ||
| 930 | |||
| 931 | |||
| 932 | /******************************************************************************************************************************************************* | ||
| 933 | * string for temp var template functions | ||
| 934 | ********************************************************************************************************************************************************/ | ||
| 935 | |||
| 936 | 5268 | template tempDecl(String ty, Text &varDecls /*BUFP*/) | |
| 937 | "Declares a temporary variable in varDecls and returns the name." | ||
| 938 | ::= | ||
| 939 | let newVar = 'tmp<%System.tmpTick()%>' | ||
| 940 | let &varDecls += '<%ty%> <%newVar%>;<%\n%>' | ||
| 941 | newVar | ||
| 942 | end tempDecl; | ||
| 943 | |||
| 944 | ✗ | template tempDeclAssign(String ty, Text &varDecls /*BUFP*/,String assign) | |
| 945 | "Declares a temporary variable in varDecls and returns the name." | ||
| 946 | ::= | ||
| 947 | let newVar = 'tmp<%System.tmpTick()%>' | ||
| 948 | let &varDecls += '<%ty%> <%newVar%> = <%assign%>;<%\n%>' | ||
| 949 | newVar | ||
| 950 | end tempDeclAssign; | ||
| 951 | |||
| 952 | ✗ | template tempDecl1(String ty, String exp, Text &varDecls /*BUFP*/) | |
| 953 | "Declares a temporary variable in varDecls and returns the name." | ||
| 954 | ::= | ||
| 955 | let newVar = 'tmp<%System.tmpTick()%>' | ||
| 956 | let newVar1 = '<%newVar%>(<%exp%>)' | ||
| 957 | let &varDecls += '<%ty%> <%newVar1%>;<%\n%>' | ||
| 958 | newVar | ||
| 959 | end tempDecl1; | ||
| 960 | |||
| 961 | /******************************************************************************************************************************************************* | ||
| 962 | end string for temp var template functions | ||
| 963 | ********************************************************************************************************************************************************/ | ||
| 964 | |||
| 965 | |||
| 966 | |||
| 967 | /******************************************************************************************************************************************************* | ||
| 968 | exp to string template functions | ||
| 969 | ********************************************************************************************************************************************************/ | ||
| 970 | |||
| 971 | ✗ | template dimensionExp(DAE.Exp dimExp,Context context,Boolean useFlatArrayNotation) | |
| 972 | ::= | ||
| 973 | match dimExp | ||
| 974 | case DAE.CREF(componentRef = cr) then | ||
| 975 | match context | ||
| 976 | case FUNCTION_CONTEXT(__) then System.unquoteIdentifier(crefStr(cr)) | ||
| 977 | else '<%cref(cr, useFlatArrayNotation)%>' | ||
| 978 | else '/* fehler dimensionExp: INVALID_DIMENSION <%ExpressionDumpTpl.dumpExp(dimExp,"\"")%>*/' //error(sourceInfo(), 'dimensionExp: INVALID_DIMENSION <%ExpressionDumpTpl.dumpExp(dimExp,"\"")%>') | ||
| 979 | end dimensionExp; | ||
| 980 | |||
| 981 | ✗ | template daeDimensionExp(Exp exp) | |
| 982 | "Generates code for an expression." | ||
| 983 | ::= | ||
| 984 | match exp | ||
| 985 | case e as ICONST(__) then '<%integer%>' | ||
| 986 | else '-1' | ||
| 987 | end daeDimensionExp; | ||
| 988 | |||
| 989 | |||
| 990 | 54297 | template daeExp(Exp exp, Context context, Text &preExp /*BUFP*/, Text &varDecls /*BUFP*/,SimCode simCode ,Text& extraFuncs,Text& extraFuncsDecl,Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) | |
| 991 | "Generates code for an expression." | ||
| 992 | ::= | ||
| 993 | match exp | ||
| 994 | case e as ICONST(__) then integer | ||
| 995 | case e as RCONST(__) then real | ||
| 996 | case e as BCONST(__) then if bool then "true" else "false" | ||
| 997 | case e as SCONST(__) then daeExpSconst(string, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 998 | case e as CLKCONST(__) then error(sourceInfo(), 'Unsupported CLKCONST exp:<%ExpressionDumpTpl.dumpExp(e,"\"")%>') | ||
| 999 | case e as ENUM_LITERAL(__) then index | ||
| 1000 | case e as CREF(__) then daeExpCrefRhs(e, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1001 | case e as CAST(__) then daeExpCast(e, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1002 | case e as CONS(__) then error(sourceInfo(), 'Unsupported CONS exp:<%ExpressionDumpTpl.dumpExp(e,"\"")%>') | ||
| 1003 | case e as UNARY(__) then daeExpUnary(e, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1004 | case e as LBINARY(__) then daeExpLbinary(e, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1005 | case e as LUNARY(__) then daeExpLunary(e, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1006 | case e as BINARY(__) then daeExpBinary(operator, exp1, exp2, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1007 | case e as IFEXP(__) then daeExpIf(expCond, expThen, expElse, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1008 | case e as RELATION(__) then daeExpRelation(e, context, &preExp, &varDecls,simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1009 | case e as CALL(__) then daeExpCall(e, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1010 | case e as RECORD(__) then daeExpRecord(e, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1011 | case e as TUPLE(__) then error(sourceInfo(), 'Unsupported TUPLE exp:<%ExpressionDumpTpl.dumpExp(e,"\"")%>') | ||
| 1012 | case e as ASUB(__) then '<%daeExpAsub(e, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)%>' | ||
| 1013 | case e as MATRIX(__) then daeExpMatrix(e, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1014 | case e as RANGE(__) then '<%daeExpRange(e, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)%>' | ||
| 1015 | case e as TSUB(__) then '<%daeExpTsub(e, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation )%>' | ||
| 1016 | case e as REDUCTION(__) then daeExpReduction(e, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1017 | case e as ARRAY(__) then '<%daeExpArray(e, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)%>' | ||
| 1018 | case e as SIZE(__) then daeExpSize(e, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1019 | case e as SHARED_LITERAL(__) then daeExpSharedLiteral(e, context, &preExp, &varDecls, useFlatArrayNotation) | ||
| 1020 | case e as PARTEVALFUNCTION(__)then daeExpPartEvalFunction(e, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1021 | case e as BOX(__) then daeExpBox(e, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1022 | case e as UNBOX(__) then daeExpUnbox(e, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1023 | case e as RSUB(__) then daeExpRSub(e, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1024 | case e as METARECORDCALL(__) then daeExpRecordCall(e, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1025 | case e as META_TUPLE(__) then error(sourceInfo(), 'Unsupported META_TUPLE exp:<%ExpressionDumpTpl.dumpExp(e,"\"")%>') | ||
| 1026 | case e as META_OPTION(__) then error(sourceInfo(), 'Unsupported META_OPTION exp:<%ExpressionDumpTpl.dumpExp(e,"\"")%>') | ||
| 1027 | case e as MATCHEXPRESSION(__) then error(sourceInfo(), 'Unsupported MATCHEXPRESSION exp:<%ExpressionDumpTpl.dumpExp(e,"\"")%>') | ||
| 1028 | else error(sourceInfo(), 'Unknown exp:<%ExpressionDumpTpl.dumpExp(exp,"\"")%>') | ||
| 1029 | end daeExp; | ||
| 1030 | |||
| 1031 | 79 | template daeExpRSub(Exp exp, Context context, Text &preExp, Text &varDecls, SimCode simCode, Text& extraFuncs, Text& extraFuncsDecl, | |
| 1032 | Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) | ||
| 1033 | "Generates code for an tsub expression." | ||
| 1034 | ::= | ||
| 1035 | match exp | ||
| 1036 | case RSUB(ix=-1) then | ||
| 1037 | let res = daeExp(exp, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1038 | '<%res%>.<%fieldName%>_' | ||
| 1039 | case RSUB(__) then | ||
| 1040 | error(sourceInfo(), '<%ExpressionDumpTpl.dumpExp(exp,"\"")%>: failed') | ||
| 1041 | end daeExpRSub; | ||
| 1042 | |||
| 1043 | 19 | template daeExpRange(Exp exp, Context context, Text &preExp, Text &varDecls, SimCode simCode, Text& extraFuncs, Text& extraFuncsDecl, | |
| 1044 | Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) | ||
| 1045 | "Generates code for a range expression." | ||
| 1046 | ::= | ||
| 1047 | match exp | ||
| 1048 | case RANGE(__) then | ||
| 1049 | let ty_str = expTypeShort(ty) | ||
| 1050 | let start_exp = daeExp(start, context, &preExp, &varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1051 | let stop_exp = daeExp(stop, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1052 | let tmp = tempDecl('DynArrayDim1<<%ty_str%>>', &varDecls /*BUFD*/) | ||
| 1053 | let step_exp = match step case SOME(stepExp) then daeExp(stepExp, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) else "1" | ||
| 1054 | let &preExp += | ||
| 1055 | << | ||
| 1056 | int <%tmp%>_num_elems =(<%stop_exp%>-<%start_exp%>)/<%step_exp%>+1; | ||
| 1057 | <%tmp%>.setDims(<%tmp%>_num_elems) /*daeExpRange*/; | ||
| 1058 | for (int <%tmp%>_i = 1; <%tmp%>_i <= <%tmp%>_num_elems; <%tmp%>_i++) | ||
| 1059 | <%tmp%>(<%tmp%>_i) = <%start_exp%>+(<%tmp%>_i-1)*<%step_exp%>;<%\n%> | ||
| 1060 | >> | ||
| 1061 | '<%tmp%>' | ||
| 1062 | end daeExpRange; | ||
| 1063 | |||
| 1064 | 9 | template daeExpReduction(Exp exp, Context context, Text &preExp, | |
| 1065 | Text &varDecls,SimCode simCode, Text& extraFuncs,Text& extraFuncsDecl,Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) | ||
| 1066 | "Generates code for a reduction expression. The code is quite messy because it handles all | ||
| 1067 | special reduction functions (list, listReverse, array) and handles both list and array as input" | ||
| 1068 | ::= | ||
| 1069 | match exp | ||
| 1070 | case r as REDUCTION(reductionInfo=ri as REDUCTIONINFO(iterType=THREAD()),iterators=iterators) | ||
| 1071 | case r as REDUCTION(reductionInfo=ri as REDUCTIONINFO(iterType=COMBINE()),iterators=iterators as {_}) then | ||
| 1072 | ( | ||
| 1073 | let &tmpVarDecls = buffer "" | ||
| 1074 | let &bodyExpPre = buffer "" | ||
| 1075 | let &rangeExpPre = buffer "" | ||
| 1076 | let arrayTypeResult = expTypeFromExpArray(r) | ||
| 1077 | let arrIndex = match ri.path case IDENT(name="array") then tempDecl("int",&tmpVarDecls) | ||
| 1078 | let foundFirst = match ri.path case IDENT(name="array") then "" else (if not ri.defaultValue then tempDecl("int",&tmpVarDecls)) | ||
| 1079 | let resType = expTypeArrayIf(typeof(exp)) | ||
| 1080 | let res = contextCref(makeUntypedCrefIdent(ri.resultName), context,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1081 | let &tmpVarDecls += '<%resType%> <%res%>;<%\n%>' | ||
| 1082 | let resTmp = tempDecl(resType,&varDecls) | ||
| 1083 | let &preDefault = buffer "" | ||
| 1084 | let resTail = (match ri.path case IDENT(name="list") then tempDecl("modelica_metatype*",&tmpVarDecls)) | ||
| 1085 | let defaultValue = (match ri.path | ||
| 1086 | case IDENT(name="array") then "" | ||
| 1087 | else (match ri.defaultValue | ||
| 1088 | case SOME(v) then daeExp(ValuesUtil.valueExpNoOriginal(v), context, &preDefault, &tmpVarDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation))) | ||
| 1089 | let reductionBodyExpr = contextCref(makeUntypedCrefIdent(ri.foldName), context,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1090 | let bodyExprType = expTypeArrayIf(typeof(r.expr)) | ||
| 1091 | let reductionBodyExprWork = daeExp(r.expr, context, &bodyExpPre, &tmpVarDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1092 | let &tmpVarDecls += '<%bodyExprType%> <%reductionBodyExpr%>;<%\n%>' | ||
| 1093 | let &bodyExpPre += '<%reductionBodyExpr%> = <%reductionBodyExprWork%>;<%\n%>' | ||
| 1094 | let foldExp = (match ri.path | ||
| 1095 | case IDENT(name="list") then | ||
| 1096 | << | ||
| 1097 | *<%resTail%> = mmc_mk_cons(<%reductionBodyExpr%>,0); | ||
| 1098 | <%resTail%> = &MMC_CDR(*<%resTail%>); | ||
| 1099 | >> | ||
| 1100 | case IDENT(name="listReverse") then // This is too easy; the damn list is already in the correct order | ||
| 1101 | '<%res%> = mmc_mk_cons(<%reductionBodyExpr%>,<%res%>);' | ||
| 1102 | case IDENT(name="array") then | ||
| 1103 | match typeof(r.expr) | ||
| 1104 | case T_ARRAY(__) then | ||
| 1105 | let tmp_slice = tempDecl("vector<Slice>", &varDecls) | ||
| 1106 | << | ||
| 1107 | <%tmp_slice%>.clear(); | ||
| 1108 | <%tmp_slice%>.push_back(Slice(<%arrIndex%>++)); | ||
| 1109 | ArraySlice<<%expTypeShort(ty)%>>(<%res%>, <%tmp_slice%>).assign(<%reductionBodyExpr%>); | ||
| 1110 | >> | ||
| 1111 | else | ||
| 1112 | '<%res%>(<%arrIndex%>++) = <%reductionBodyExpr%>;' | ||
| 1113 | else match ri.foldExp case SOME(fExp) then | ||
| 1114 | let &foldExpPre = buffer "" | ||
| 1115 | let fExpStr = daeExp(fExp, context, &foldExpPre, &tmpVarDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1116 | if foundFirst then | ||
| 1117 | << | ||
| 1118 | if (<%foundFirst%>) | ||
| 1119 | { | ||
| 1120 | <%foldExpPre%> | ||
| 1121 | <%res%> = <%fExpStr%>; | ||
| 1122 | } | ||
| 1123 | else | ||
| 1124 | { | ||
| 1125 | <%res%> = <%reductionBodyExpr%>; | ||
| 1126 | <%foundFirst%> = 1; | ||
| 1127 | } | ||
| 1128 | >> | ||
| 1129 | else '<%res%> = <%fExpStr%>;') | ||
| 1130 | let endLoop = tempDecl("int",&tmpVarDecls) | ||
| 1131 | let loopHeadIter = (iterators |> iter as REDUCTIONITER(__) => | ||
| 1132 | let identType = expTypeFromExpModelica(iter.exp) | ||
| 1133 | let ty_str = expTypeShort(ty) | ||
| 1134 | let arrayType = 'DynArrayDim1<<%identType%>>'//expTypeFromExpArray(iter.exp) | ||
| 1135 | let loopVar = '<%iter.id%>_loopVar' | ||
| 1136 | let &guardExpPre = buffer "" | ||
| 1137 | let &tmpVarDecls += '<%arrayType%> <%loopVar%>;/*testloopvar*/<%\n%>' | ||
| 1138 | let firstIndex = tempDecl("int",&tmpVarDecls) | ||
| 1139 | let rangeExp = daeExp(iter.exp, context, &rangeExpPre, &tmpVarDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1140 | let &rangeExpPre += '<%loopVar%> = <%rangeExp%>/*testloopvar2*/;<%\n%>' | ||
| 1141 | let &rangeExpPre += if firstIndex then '<%firstIndex%> = 1;<%\n%>' | ||
| 1142 | let guardCond = (match iter.guardExp case SOME(grd) then daeExp(grd, context, &guardExpPre, &tmpVarDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) else "1") | ||
| 1143 | let empty = '0 == (<%loopVar%>.getDim(2))' | ||
| 1144 | let iteratorName = contextIteratorName(iter.id, context) | ||
| 1145 | let &tmpVarDecls += '<%identType%> <%iteratorName%>;<%\n%>' | ||
| 1146 | let guardExp = | ||
| 1147 | << | ||
| 1148 | <%&guardExpPre%> | ||
| 1149 | if(<%guardCond%>) { /* found non-guarded */ | ||
| 1150 | <%endLoop%>--; | ||
| 1151 | break; | ||
| 1152 | } | ||
| 1153 | >> | ||
| 1154 | let addr = match iter.ty | ||
| 1155 | case T_ARRAY(ty=T_COMPLEX(complexClassType = record_state)) then | ||
| 1156 | let rec_name = '<%underscorePath(ClassInfUtil.getStateName(record_state))%>' | ||
| 1157 | '<%rec_name%>_array_get(<%loopVar%>, 1, <%firstIndex%>++)' | ||
| 1158 | else | ||
| 1159 | '<%loopVar%>( <%firstIndex%>++)' | ||
| 1160 | << | ||
| 1161 | while(<%firstIndex%> <= <%loopVar%>.getDim(1)) { | ||
| 1162 | <%iteratorName%> = <%addr%>; | ||
| 1163 | <%guardExp%> | ||
| 1164 | } | ||
| 1165 | >>) | ||
| 1166 | let firstValue = (match ri.path | ||
| 1167 | case IDENT(name="array") then | ||
| 1168 | let length = tempDecl("int", &tmpVarDecls) | ||
| 1169 | let &rangeExpPre += '<%length%> = 0;<%\n%>' | ||
| 1170 | let _ = (iterators |> iter as REDUCTIONITER(__) => | ||
| 1171 | let loopVar = '<%iter.id%>_loopVar' | ||
| 1172 | let &rangeExpPre += '<%length%> = max(<%length%>, <%loopVar%>.getDim(1));<%\n%>' | ||
| 1173 | "") | ||
| 1174 | << | ||
| 1175 | <%arrIndex%> = 1; | ||
| 1176 | <% match typeof(r.expr) | ||
| 1177 | case T_ARRAY(__) then | ||
| 1178 | let dim_vec = tempDecl("std::vector<size_t>",&tmpVarDecls) | ||
| 1179 | let dimSizes = dims |> dim => match dim | ||
| 1180 | case DIM_INTEGER(__) then '<%dim_vec%>.push_back(<%integer%>)' | ||
| 1181 | case DIM_BOOLEAN(__) then '<%dim_vec%>.push_back(2)' | ||
| 1182 | case DIM_ENUM(__) then '<%dim_vec%>.push_back(<%size%>)' | ||
| 1183 | case DIM_EXP(exp = exp) then | ||
| 1184 | let val = daeExp(exp, context, &rangeExpPre, &tmpVarDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1185 | '<%dim_vec%>.push_back(<%val%>)' | ||
| 1186 | else error(sourceInfo(), 'array reduction unable to generate code for element of unknown dimension sizes; type <%unparseType(typeof(r.expr))%>: <%ExpressionDumpTpl.dumpExp(r.expr,"\"")%>') | ||
| 1187 | ; separator = "; " | ||
| 1188 | << | ||
| 1189 | <%dim_vec%>.push_back(<%length%>); | ||
| 1190 | <%dimSizes%>; | ||
| 1191 | <%res%>.setDims(<%dim_vec%>); | ||
| 1192 | >> | ||
| 1193 | else | ||
| 1194 | '<%res%>.setDims(<%length%>);'%> | ||
| 1195 | >> | ||
| 1196 | else | ||
| 1197 | (if foundFirst then | ||
| 1198 | << | ||
| 1199 | <%foundFirst%> = 0; /* <%dotPath(ri.path)%> lacks default-value */ | ||
| 1200 | >> | ||
| 1201 | else | ||
| 1202 | << | ||
| 1203 | <%&preDefault%> | ||
| 1204 | <%res%> = <%defaultValue%>; /* defaultValue */ | ||
| 1205 | >>) | ||
| 1206 | ) | ||
| 1207 | let loop = | ||
| 1208 | << | ||
| 1209 | while(1) { | ||
| 1210 | <%endLoop%> = <%listLength(iterators)%>; | ||
| 1211 | <%loopHeadIter%> | ||
| 1212 | if (<%endLoop%> == 0) { | ||
| 1213 | <%&bodyExpPre%> | ||
| 1214 | <%foldExp%> | ||
| 1215 | } <% match iterators case _::_ then | ||
| 1216 | << | ||
| 1217 | else if (<%endLoop%> == <%listLength(iterators)%>) { | ||
| 1218 | break; | ||
| 1219 | } else { | ||
| 1220 | throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION,"Internal error"); | ||
| 1221 | } | ||
| 1222 | >> %> | ||
| 1223 | } | ||
| 1224 | >> | ||
| 1225 | let &preExp += << | ||
| 1226 | { | ||
| 1227 | <%&tmpVarDecls%> | ||
| 1228 | <%&rangeExpPre%> | ||
| 1229 | <%firstValue%> | ||
| 1230 | <% if resTail then '<%resTail%> = &<%res%>;' %> | ||
| 1231 | <%loop%> | ||
| 1232 | <% if foundFirst then 'if (!<%foundFirst%>) throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION,"Internal error");' %> | ||
| 1233 | <% if resTail then '*<%resTail%> = NULL;' %> | ||
| 1234 | <% resTmp %> = <% res %>; | ||
| 1235 | }<%\n%> | ||
| 1236 | >> | ||
| 1237 | resTmp) | ||
| 1238 | else error(sourceInfo(), 'Code generation does not support multiple iterators: <%ExpressionDumpTpl.dumpExp(exp,"\"")%>') | ||
| 1239 | end daeExpReduction; | ||
| 1240 | |||
| 1241 | |||
| 1242 | 131 | template daeExpSize(Exp exp, Context context, Text &preExp, Text &varDecls, SimCode simCode, Text& extraFuncs, Text& extraFuncsDecl, | |
| 1243 | Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) | ||
| 1244 | "Generates code for a size expression." | ||
| 1245 | ::= | ||
| 1246 | match exp | ||
| 1247 | case SIZE(sz=SOME(dim)) then | ||
| 1248 | let expPart = daeExp(exp, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1249 | let dimPart = daeExp(dim, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1250 | '<%expPart%>.getDim(<%dimPart%>)' | ||
| 1251 | case SIZE(sz=NONE()) then | ||
| 1252 | let expPart = daeExp(exp, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1253 | let tmp = tempDecl("vector<size_t>", &varDecls) | ||
| 1254 | let &preExp += | ||
| 1255 | << | ||
| 1256 | <%tmp%> = <%expPart%>.getDims(); | ||
| 1257 | DynArrayDim1<int> <%tmp%>_size(<%tmp%>.size()); | ||
| 1258 | for (size_t <%tmp%>_i = 1; <%tmp%>_i <= <%tmp%>.size(); <%tmp%>_i++) | ||
| 1259 | <%tmp%>_size(<%tmp%>_i) = (int)<%tmp%>[<%tmp%>_i-1];<%\n%> | ||
| 1260 | >> | ||
| 1261 | '<%tmp%>_size' | ||
| 1262 | else error(sourceInfo(), ExpressionDumpTpl.dumpExp(exp,"\"") + " not implemented") | ||
| 1263 | end daeExpSize; | ||
| 1264 | |||
| 1265 | |||
| 1266 | 8 | template daeExpMatrix(Exp exp, Context context, Text &preExp, Text &varDecls, SimCode simCode, Text& extraFuncs, | |
| 1267 | Text& extraFuncsDecl,Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) | ||
| 1268 | "Generates code for a matrix expression." | ||
| 1269 | ::= | ||
| 1270 | match exp | ||
| 1271 | case MATRIX(matrix={{}}) // special case for empty matrix: create dimensional array Real[0,1] | ||
| 1272 | case MATRIX(matrix={}) // special case for empty array: create dimensional array Real[0,1] | ||
| 1273 | then | ||
| 1274 | let typestr = expTypeShort(ty) | ||
| 1275 | let arrayTypeStr = 'DynArrayDim2<<%typestr%>>' | ||
| 1276 | let tmp = tempDecl(arrayTypeStr, &varDecls /*BUFD*/) | ||
| 1277 | // let &preExp += 'alloc_<%arrayTypeStr%>(&<%tmp%>, test2, 0, 1);<%\n%>' | ||
| 1278 | tmp | ||
| 1279 | case m as MATRIX(matrix=(row1::_)) then | ||
| 1280 | let arrayTypeStr = expTypeShort(ty) | ||
| 1281 | let StatArrayDim = expTypeArrayIf(ty) | ||
| 1282 | let &tmp = buffer "" /*BUFD*/ | ||
| 1283 | let arrayVar = tempDecl(arrayTypeStr, &tmp /*BUFD*/) | ||
| 1284 | let &vals = buffer "" /*BUFD*/ | ||
| 1285 | let dim_cols = listLength(row1) | ||
| 1286 | |||
| 1287 | /* | ||
| 1288 | /////////////////////////////////////////////////NonCED | ||
| 1289 | let params = (m.matrix |> row => | ||
| 1290 | let vars = daeExpMatrixRow(row, context, &varDecls,&preExp,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace,useFlatArrayNotation) | ||
| 1291 | '<%vars%>' | ||
| 1292 | ;separator=",") | ||
| 1293 | let &preExp += ' | ||
| 1294 | <%StatArrayDim%><%arrayVar%>; | ||
| 1295 | <%arrayTypeStr%> <%arrayVar%>_data[]={<%params%>}; | ||
| 1296 | <%arrayVar%>.assign( <%arrayVar%>_data );<%\n%>' | ||
| 1297 | arrayVar | ||
| 1298 | /////////////////////////////////////////////////NonCED | ||
| 1299 | */ | ||
| 1300 | |||
| 1301 | ///////////////////////////////////////////////CED | ||
| 1302 | let matrixassign = match m.matrix | ||
| 1303 | case row::_ then | ||
| 1304 | let vars = "NO_ASSIGN" //daeExpMatrixRow(m.matrix,context,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName) | ||
| 1305 | match vars | ||
| 1306 | case "NO_ASSIGN" | ||
| 1307 | then | ||
| 1308 | let params = (m.matrix |> row => | ||
| 1309 | let vars = daeExpMatrixRow2(row, context, &varDecls, &preExp, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1310 | '<%vars%>' | ||
| 1311 | ;separator=",") | ||
| 1312 | let &preExp += | ||
| 1313 | << | ||
| 1314 | //default matrix assign | ||
| 1315 | <%StatArrayDim%> <%arrayVar%>; | ||
| 1316 | <%arrayTypeStr%> <%arrayVar%>_data[] = {<%params%>}; | ||
| 1317 | assignRowMajorData(<%arrayVar%>_data, <%arrayVar%>);<%\n%> | ||
| 1318 | >> | ||
| 1319 | '' | ||
| 1320 | else | ||
| 1321 | let &preExp += | ||
| 1322 | << | ||
| 1323 | //optimized matrix assign | ||
| 1324 | <%StatArrayDim%> <%arrayVar%>; | ||
| 1325 | <%arrayVar%>.assign( <%vars%> );<%\n%> | ||
| 1326 | >> | ||
| 1327 | '' | ||
| 1328 | end match | ||
| 1329 | |||
| 1330 | |||
| 1331 | //let &preExp += ' | ||
| 1332 | // <%StatArrayDim%><%arrayVar%>; | ||
| 1333 | // <%arrayVar%>.assign( <%matrixassign%> );<%\n%>' | ||
| 1334 | |||
| 1335 | |||
| 1336 | arrayVar | ||
| 1337 | end daeExpMatrix; | ||
| 1338 | |||
| 1339 | |||
| 1340 | 20 | template daeExpMatrixRow2(list<Exp> row, Context context, Text &varDecls, Text &preExp, SimCode simCode, Text& extraFuncs, Text& extraFuncsDecl, | |
| 1341 | Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) | ||
| 1342 | "Helper to daeExpMatrix." | ||
| 1343 | ::= | ||
| 1344 | let varLstStr = (row |> e => | ||
| 1345 | let expVar = daeExp(e, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1346 | '<%expVar%>' | ||
| 1347 | ;separator=",") | ||
| 1348 | varLstStr | ||
| 1349 | end daeExpMatrixRow2; | ||
| 1350 | /////////////////////////////////////////////////CED | ||
| 1351 | |||
| 1352 | /* | ||
| 1353 | /////////////////////////////////////////////////NonCED functions | ||
| 1354 | template daeExpMatrixRow(list<Exp> row, | ||
| 1355 | Context context, | ||
| 1356 | Text &varDecls ,Text &preExp ,SimCode simCode, Text& extraFuncs,Text& extraFuncsDecl,Text extraFuncsNamespace,Boolean useFlatArrayNotation) | ||
| 1357 | "Helper to daeExpMatrix." | ||
| 1358 | ::= | ||
| 1359 | |||
| 1360 | let varLstStr = (row |> e => | ||
| 1361 | |||
| 1362 | let expVar = daeExp(e, context, &preExp , &varDecls ,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace,useFlatArrayNotation) | ||
| 1363 | '<%expVar%>' | ||
| 1364 | ;separator=",") | ||
| 1365 | varLstStr | ||
| 1366 | end daeExpMatrixRow; | ||
| 1367 | /////////////////////////////////////////////////NonCED functions | ||
| 1368 | */ | ||
| 1369 | |||
| 1370 | ////////////////////////////////////////////////////////////////////////CED Functions | ||
| 1371 | ✗ | template daeExpMatrixRow(list<list<Exp>> matrix,Context context,SimCode simCode ,Text& extraFuncs,Text& extraFuncsDecl,Text extraFuncsNamespace,Text stateDerVectorName /*=__zDot*/) | |
| 1372 | "Helper to daeExpMatrix." | ||
| 1373 | ::= | ||
| 1374 | if isCrefListWithEqualIdents(List.flatten(matrix)) then | ||
| 1375 | match matrix | ||
| 1376 | case row::_ then | ||
| 1377 | daeExpMatrixName(row,context,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName) | ||
| 1378 | else | ||
| 1379 | "NO_ASSIGN" | ||
| 1380 | end match | ||
| 1381 | else | ||
| 1382 | "NO_ASSIGN" | ||
| 1383 | end daeExpMatrixRow; | ||
| 1384 | |||
| 1385 | ✗ | template daeExpMatrixName(list<Exp> row,Context context,SimCode simCode ,Text& extraFuncs,Text& extraFuncsDecl, Text stateDerVectorName /*=__zDot*/, Text extraFuncsNamespace) | |
| 1386 | ::= | ||
| 1387 | let &varDecls = buffer "" /*BUFD*/ | ||
| 1388 | let &preExp = buffer "" /*BUFD*/ | ||
| 1389 | match row | ||
| 1390 | case CREF(componentRef = cr)::_ then | ||
| 1391 | contextCref(crefStripLastSubs(cr),context,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, false) | ||
| 1392 | /* | ||
| 1393 | match context | ||
| 1394 | case FUNCTION_CONTEXT(__) then | ||
| 1395 | cref2(cr,false) //daeExpMatrixName2(cr) //assign array complete to the function therefore false as second argument | ||
| 1396 | else | ||
| 1397 | "_"+cref2(cr,false)//daeExpMatrixName2(cr) //assign array complete to function therefore false as second argument | ||
| 1398 | else | ||
| 1399 | "NO_ASSIGN" | ||
| 1400 | */ | ||
| 1401 | end daeExpMatrixName; | ||
| 1402 | |||
| 1403 | |||
| 1404 | ✗ | template daeExpMatrixName2(ComponentRef cr) | |
| 1405 | ::= | ||
| 1406 | |||
| 1407 | match cr | ||
| 1408 | case CREF_IDENT(__) then | ||
| 1409 | '<%System.unquoteIdentifier(ident)%>' | ||
| 1410 | case CREF_QUAL(__) then | ||
| 1411 | '<%System.unquoteIdentifier(ident)%><%subscriptsToCStrForArray(subscriptLst)%>_P_<%daeExpMatrixName2(componentRef)%>' | ||
| 1412 | case WILD(__) then ' ' | ||
| 1413 | else "CREF_NOT_IDENT_OR_QUAL" | ||
| 1414 | end daeExpMatrixName2; | ||
| 1415 | |||
| 1416 | |||
| 1417 | 409 | template daeExpArray(Exp exp, Context context, Text &preExp, Text &varDecls, SimCode simCode, Text& extraFuncs, Text& extraFuncsDecl, | |
| 1418 | Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) | ||
| 1419 | "Generates code for an array expression." | ||
| 1420 | ::= | ||
| 1421 | match exp | ||
| 1422 | case ARRAY(array=_::_, ty = arraytype) then | ||
| 1423 | let arrayTypeStr = expTypeShort(ty) | ||
| 1424 | let ArrayType = expTypeArrayIf(ty) | ||
| 1425 | let &tmpVar = buffer "" | ||
| 1426 | let arrayVar = tempDecl(arrayTypeStr, &tmpVar /*BUFD*/) | ||
| 1427 | let arrayassign = if scalar then | ||
| 1428 | let params = daeExpArray2(array,arrayVar,ArrayType,arrayTypeStr,context,preExp,varDecls,simCode, &extraFuncs,&extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1429 | "" | ||
| 1430 | else | ||
| 1431 | let &varDecls += '<%ArrayType%> <%arrayVar%>;<%\n%>' | ||
| 1432 | daeExpArray3(array, arrayVar, ArrayType, context, preExp, varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1433 | arrayVar | ||
| 1434 | case ARRAY(__) then | ||
| 1435 | let arrayTypeStr = expTypeShort(ty) | ||
| 1436 | let arrayDef = expTypeArrayIf(ty) | ||
| 1437 | let &tmpdecl = buffer "" | ||
| 1438 | let arrayVar = tempDecl(arrayTypeStr, &tmpdecl ) | ||
| 1439 | let &tmpVar = buffer "" | ||
| 1440 | let &preExp += ' | ||
| 1441 | //tmp array | ||
| 1442 | <%arrayDef%><%arrayVar%>;<%\n%>' | ||
| 1443 | arrayVar | ||
| 1444 | end daeExpArray; | ||
| 1445 | |||
| 1446 | |||
| 1447 | |||
| 1448 | 2 | template daeExpArray3(list<Exp> array, String arrayVar, String ArrayType, Context context, Text &preExp, | |
| 1449 | Text &varDecls, SimCode simCode, Text& extraFuncs, Text& extraFuncsDecl, Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) | ||
| 1450 | "Generates code for an array expression." | ||
| 1451 | ::= | ||
| 1452 | let arraycreate = (array |> e hasindex i0 fromindex 1 => | ||
| 1453 | let subArraycall = daeExp(e, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1454 | << | ||
| 1455 | <%arrayVar%>.append(<%i0%>, <%subArraycall%>,<%listLength(array)%>); | ||
| 1456 | >> ;separator="\n") | ||
| 1457 | let &preExp += | ||
| 1458 | << | ||
| 1459 | <%arraycreate%> | ||
| 1460 | <%\n%> | ||
| 1461 | >> | ||
| 1462 | arraycreate | ||
| 1463 | end daeExpArray3; | ||
| 1464 | |||
| 1465 | |||
| 1466 | |||
| 1467 | |||
| 1468 | |||
| 1469 | /* | ||
| 1470 | Array creation template functions, which splits the array creation code in separate methods | ||
| 1471 | */ | ||
| 1472 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 1473 | //template daeExpArray(Exp exp, Context context, Text &preExp, Text &varDecls, SimCode simCode, Text& extraFuncs, Text& extraFuncsDecl, | ||
| 1474 | // Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) | ||
| 1475 | // "Generates code for an array expression." | ||
| 1476 | //::= | ||
| 1477 | //match exp | ||
| 1478 | //case ARRAY(array=_::_, ty = arraytype) then | ||
| 1479 | // let arrayTypeStr = expTypeShort(ty) | ||
| 1480 | // let ArrayType = expTypeArrayIf(ty) | ||
| 1481 | // let &tmpVar = buffer "" | ||
| 1482 | // let arrayVar = tempDecl(arrayTypeStr, &tmpVar /*BUFD*/) | ||
| 1483 | // let arrayassign = if scalar then | ||
| 1484 | // let params = daeExpArray2(array,arrayVar,ArrayType,arrayTypeStr,context,preExp,varDecls,simCode, &extraFuncs,&extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1485 | // "" | ||
| 1486 | // else | ||
| 1487 | // let funcCalls = daeExpSubArray(array, arrayVar, ArrayType, context, preExp, varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1488 | // let &extraFuncsDecl += 'void createArray_<%arrayVar%>(<%ArrayType%>& <%arrayVar%>);<%\n%>' | ||
| 1489 | // let &extraFuncs += | ||
| 1490 | // << | ||
| 1491 | // void <%extraFuncsNamespace%>::createArray_<%arrayVar%>(<%ArrayType%>& <%arrayVar%>) | ||
| 1492 | // { | ||
| 1493 | // <%arrayVar%>.setDims(<%allocateDimensions(arraytype,context)%>); | ||
| 1494 | // <%funcCalls%> | ||
| 1495 | // }<%\n%> | ||
| 1496 | // >> | ||
| 1497 | // << | ||
| 1498 | // <%ArrayType%> <%arrayVar%>; | ||
| 1499 | // createArray_<%arrayVar%>(<%arrayVar%>);<%\n%> | ||
| 1500 | // >> | ||
| 1501 | // | ||
| 1502 | // let &preExp += '<%arrayassign%>' | ||
| 1503 | // arrayVar | ||
| 1504 | //case ARRAY(__) then | ||
| 1505 | // let arrayTypeStr = expTypeShort(ty) | ||
| 1506 | // let arrayDef = expTypeArrayIf(ty) | ||
| 1507 | // let &tmpdecl = buffer "" | ||
| 1508 | // let arrayVar = tempDecl(arrayTypeStr, &tmpdecl ) | ||
| 1509 | // let &tmpVar = buffer "" | ||
| 1510 | // let &preExp += ' | ||
| 1511 | // //tmp array | ||
| 1512 | // <%arrayDef%><%arrayVar%>;<%\n%>' | ||
| 1513 | // arrayVar | ||
| 1514 | //end daeExpArray; | ||
| 1515 | // | ||
| 1516 | //template daeExpSubArray(list<Exp> array, String arrayVar, String ArrayType, Context context, Text &preExp, Text &varDecls, SimCode simCode, | ||
| 1517 | // Text& extraFuncs, Text& extraFuncsDecl, Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) | ||
| 1518 | // "Generates code for an array expression." | ||
| 1519 | //::= | ||
| 1520 | //(List.partition(array,50) |> subarray hasindex i0 fromindex 0 => | ||
| 1521 | // daeExpSubArray2(subarray,i0,50,arrayVar,ArrayType,context,preExp,varDecls,simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1522 | // ;separator ="\n") | ||
| 1523 | //end daeExpSubArray; | ||
| 1524 | // | ||
| 1525 | //template daeExpSubArray2(list<Exp> array, Integer idx, Integer multiplicator, String arrayVar, String ArrayType, Context context, Text &preExp, | ||
| 1526 | // Text &varDecls, SimCode simCode, Text& extraFuncs, Text& extraFuncsDecl, Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) | ||
| 1527 | // "Generates code for an array expression." | ||
| 1528 | //::= | ||
| 1529 | //let func = 'void createArray_<%arrayVar%>_<%idx%>(<%ArrayType%>& <%arrayVar%>);' | ||
| 1530 | //let &extraFuncsDecl += '<%func%><%\n%>' | ||
| 1531 | //let funcCall = 'createArray_<%arrayVar%>_<%idx%>(<%arrayVar%>);' | ||
| 1532 | //let &funcVarDecls = buffer "" | ||
| 1533 | //let &preExpSubArrays = buffer "" | ||
| 1534 | //let funcs = (array |> e hasindex i0 fromindex intAdd(intMul(idx, multiplicator),1) => | ||
| 1535 | // let subArraycall = daeExp(e, context, &preExpSubArrays, &funcVarDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1536 | // << | ||
| 1537 | // <%arrayVar%>.append(<%i0%>, <%subArraycall%>); | ||
| 1538 | // >> ;separator="\n") | ||
| 1539 | // let &extraFuncs += | ||
| 1540 | // << | ||
| 1541 | // void <%extraFuncsNamespace%>::createArray_<%arrayVar%>_<%idx%>(<%ArrayType%>& <%arrayVar%>) | ||
| 1542 | // { | ||
| 1543 | // <%funcVarDecls%> | ||
| 1544 | // <%preExpSubArrays%> | ||
| 1545 | // <%funcs%> | ||
| 1546 | // }<%\n%> | ||
| 1547 | // >> | ||
| 1548 | //funcCall | ||
| 1549 | //end daeExpSubArray2; | ||
| 1550 | |||
| 1551 | 407 | template daeExpArray2(list<Exp> array,String arrayVar,String ArrayType,String arrayTypeStr, Context context, Text &preExp, | |
| 1552 | Text &varDecls, SimCode simCode, Text& extraFuncs, Text& extraFuncsDecl, Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) | ||
| 1553 | "Generates code for an array expression." | ||
| 1554 | ::= | ||
| 1555 | let params = (array |> e => '<%daeExp(e, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)%>' ;separator=", ") | ||
| 1556 | let &preExp += | ||
| 1557 | << | ||
| 1558 | <%arrayTypeStr%> <%arrayVar%>_data[]={<%params%>}; | ||
| 1559 | <%ArrayType%> <%arrayVar%>(<%arrayVar%>_data);<%\n%> | ||
| 1560 | >> | ||
| 1561 | |||
| 1562 | params | ||
| 1563 | end daeExpArray2; | ||
| 1564 | |||
| 1565 | ✗ | template daeSubscript(DAE.Subscript sub, Context context, Text &preExp, Text &varDecls, SimCode simCode, Text& extraFuncs, Text& extraFuncsDecl, | |
| 1566 | Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) | ||
| 1567 | ::= | ||
| 1568 | match sub | ||
| 1569 | case sub as INDEX() then daeExp(exp, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1570 | else error(sourceInfo(), 'non INDEX(_) (i.e., slice) subscripts probably should not reach here. Check indexedAssign template.') | ||
| 1571 | end match | ||
| 1572 | end daeSubscript; | ||
| 1573 | |||
| 1574 | 140 | template daeExpAsub(Exp inExp, Context context, Text &preExp, Text &varDecls, SimCode simCode, Text& extraFuncs, Text& extraFuncsDecl, | |
| 1575 | Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) | ||
| 1576 | "Generates code for an asub expression." | ||
| 1577 | ::= | ||
| 1578 | match expTypeFromExpShort(inExp) | ||
| 1579 | case "metatype" then | ||
| 1580 | // MetaModelica Array | ||
| 1581 | (match inExp case ASUB(exp=e, sub={idx}) then | ||
| 1582 | let e1 = daeExp(e, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1583 | let idx1 = daeSubscript(idx, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1584 | 'arrayGet(<%e1%>,<%idx1%>) /* DAE.ASUB */') | ||
| 1585 | // Modelica Array | ||
| 1586 | else | ||
| 1587 | match inExp | ||
| 1588 | |||
| 1589 | case ASUB(exp=ASUB(__)) then | ||
| 1590 | error(sourceInfo(),'Nested array subscripting *should* have been handled by the routine creating the asub, but for some reason it was not: <%ExpressionDumpTpl.dumpExp(exp,"\"")%>') | ||
| 1591 | |||
| 1592 | // Faster asub: Do not construct a whole new array just to access one subscript | ||
| 1593 | case ASUB(exp=exp as ARRAY(scalar=true), sub={idx}) then | ||
| 1594 | let res = tempDecl(expTypeFromExpModelica(exp),&varDecls) | ||
| 1595 | let idx1 = daeSubscript(idx, context, &preExp, &varDecls,simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1596 | let expl = (exp.array |> e hasindex i1 fromindex 1 => | ||
| 1597 | let &caseVarDecls = buffer "" | ||
| 1598 | let &casePreExp = buffer "" | ||
| 1599 | let v =daeExp(e, context, &casePreExp, &caseVarDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1600 | << | ||
| 1601 | case <%i1%>: { | ||
| 1602 | <%&caseVarDecls%> | ||
| 1603 | <%&casePreExp%> | ||
| 1604 | <%res%> = <%v%>; | ||
| 1605 | break; | ||
| 1606 | } | ||
| 1607 | >> ; separator = "\n") | ||
| 1608 | let &preExp += | ||
| 1609 | << | ||
| 1610 | switch (<%idx1%>) { /* ASUB */ | ||
| 1611 | <%expl%> | ||
| 1612 | default: | ||
| 1613 | assert(NULL == "index out of bounds"); | ||
| 1614 | } | ||
| 1615 | >> | ||
| 1616 | '<%res%>' | ||
| 1617 | |||
| 1618 | case ASUB(exp=ecr as CREF(__), sub=subs) then | ||
| 1619 | let arrName = daeExpCrefRhs(buildCrefExpFromSubs(ecr, subs), context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1620 | match context case FUNCTION_CONTEXT(__) then | ||
| 1621 | arrName | ||
| 1622 | else | ||
| 1623 | '<%arrayScalarRhs(ecr.ty, subs, arrName, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)%>' | ||
| 1624 | |||
| 1625 | case ASUB(exp=e, sub=indexes) then | ||
| 1626 | let exp = daeExp(e, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1627 | // let typeShort = expTypeFromExpShort(e) | ||
| 1628 | let expIndexes = (indexes |> index => '<%daeSubscriptASubIndex(index, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)%>' ;separator=",") | ||
| 1629 | //'<%typeShort%>_get<%match listLength(indexes) case 1 then "" case i then '_<%i%>D'%>(&<%exp%>, <%expIndexes%>)' | ||
| 1630 | '(<%exp%>)(<%expIndexes%>)' | ||
| 1631 | |||
| 1632 | case exp then | ||
| 1633 | error(sourceInfo(),'OTHER_ASUB <%ExpressionDumpTpl.dumpExp(exp,"\"")%>') | ||
| 1634 | end daeExpAsub; | ||
| 1635 | |||
| 1636 | 220 | template daeSubscriptASubIndex(DAE.Subscript sub, Context context, Text &preExp, Text &varDecls, SimCode simCode, Text& extraFuncs, Text& extraFuncsDecl, | |
| 1637 | Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) | ||
| 1638 | ::= | ||
| 1639 | match sub | ||
| 1640 | case INDEX(__) then daeExpASubIndex(exp, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1641 | else daeSubscript(sub, context, &preExp, &varDecls, simCode ,&extraFuncs ,&extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1642 | end daeSubscriptASubIndex; | ||
| 1643 | |||
| 1644 | 216 | template daeExpASubIndex(Exp exp, Context context, Text &preExp, Text &varDecls, SimCode simCode, Text& extraFuncs, Text& extraFuncsDecl, | |
| 1645 | Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) | ||
| 1646 | ::= | ||
| 1647 | match exp | ||
| 1648 | case ICONST(__) then integer | ||
| 1649 | case ENUM_LITERAL(__) then index | ||
| 1650 | else daeExp(exp, context, &preExp, &varDecls, simCode ,&extraFuncs ,&extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1651 | end daeExpASubIndex; | ||
| 1652 | |||
| 1653 | |||
| 1654 | ✗ | template arrayScalarRhs(Type ty, list<DAE.Subscript> subs, String arrName, Context context, | |
| 1655 | Text &preExp, Text &varDecls, SimCode simCode, Text& extraFuncs, Text& extraFuncsDecl, Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) | ||
| 1656 | "Helper to daeExpAsub." | ||
| 1657 | ::= | ||
| 1658 | /* match exp | ||
| 1659 | case ASUB(exp=ecr as CREF(__)) then*/ | ||
| 1660 | let arrayType = expTypeShort(ty) | ||
| 1661 | let dimsLenStr = listLength(subs) | ||
| 1662 | let dimsValuesStr = (subs |> sub => | ||
| 1663 | daeSubscript(sub, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) ;separator=",") | ||
| 1664 | //previous multi_array ;separator="][") | ||
| 1665 | |||
| 1666 | |||
| 1667 | match arrayType | ||
| 1668 | case "metatype_array" then | ||
| 1669 | 'arrayGet(<%arrName%>,<%dimsValuesStr%>) /*arrayScalarRhs*/' | ||
| 1670 | else | ||
| 1671 | //ToDo before used <%arrayCrefCStr(ecr.componentRef)%>[<%dimsValuesStr%>] | ||
| 1672 | << <%arrName%>(<%dimsValuesStr%>) >> | ||
| 1673 | end arrayScalarRhs; | ||
| 1674 | |||
| 1675 | 165 | template daeExpCast(Exp exp, Context context, Text &preExp /*BUFP*/, | |
| 1676 | Text &varDecls /*BUFP*/,SimCode simCode, Text& extraFuncs,Text& extraFuncsDecl,Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) | ||
| 1677 | "Generates code for a cast expression." | ||
| 1678 | ::= | ||
| 1679 | match exp | ||
| 1680 | case CAST(__) then | ||
| 1681 | let expVar = daeExp(exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1682 | match ty | ||
| 1683 | case T_INTEGER(__) then '((int)<%expVar%>)' | ||
| 1684 | case T_REAL(__) then '((double)<%expVar%>)' | ||
| 1685 | case T_ENUMERATION(__) then '((int)<%expVar%>)' | ||
| 1686 | case T_BOOL(__) then '((bool)<%expVar%>)' | ||
| 1687 | case T_ARRAY(dims=dims) then | ||
| 1688 | let from = expTypeFromExpShort(exp) | ||
| 1689 | let to = expTypeShort(ty) | ||
| 1690 | let tvar = tempDecl(expTypeArrayDims(ty, dims), &varDecls /*BUFD*/) | ||
| 1691 | let &preExp += 'cast_array<<%from%>, <%to%>>(<%expVar%>, <%tvar%>);<%\n%>' | ||
| 1692 | '<%tvar%>' | ||
| 1693 | case T_COMPLEX(varLst=vl,complexClassType=rec as RECORD(__)) then | ||
| 1694 | let tvar = tempDecl(underscorePath(rec.path)+"Type", &varDecls /*BUFD*/) | ||
| 1695 | let &preExp += '<%structParams(expVar,tvar,vl)%><%\n%>' | ||
| 1696 | '<%tvar%>' | ||
| 1697 | else | ||
| 1698 | '(<%expVar%>) /* could not cast, using the variable as it is */' | ||
| 1699 | end daeExpCast; | ||
| 1700 | |||
| 1701 | |||
| 1702 | ✗ | template structParams(String structName,String varName,list<DAE.Var> exps) | |
| 1703 | ::= | ||
| 1704 | let params = (exps |> e => match e | ||
| 1705 | case TYPES_VAR(__) then | ||
| 1706 | '<%varName%>.<%name%>_ = <%structName%>.<%name%>_;' | ||
| 1707 | ;separator="\n" ) | ||
| 1708 | params | ||
| 1709 | end structParams; | ||
| 1710 | |||
| 1711 | 260 | template daeExpRecord(Exp rec, Context context, Text &preExp, Text &varDecls, SimCode simCode, Text& extraFuncs, Text& extraFuncsDecl, | |
| 1712 | Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) | ||
| 1713 | "Generates code for the creation and initialization of a record instance." | ||
| 1714 | ::= | ||
| 1715 | match rec | ||
| 1716 | case RECORD(__) then | ||
| 1717 | let name = tempDecl(underscorePath(path) + "Type", &varDecls) | ||
| 1718 | let ass = List.zip(exps,comp) |> (exp,compn) => | ||
| 1719 | let compnStr = crefStr(makeUntypedCrefIdent(compn)) | ||
| 1720 | '<%name%>.<%compnStr%> = <%daeExp(exp, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)%>;<%\n%>' | ||
| 1721 | let &preExp += ass | ||
| 1722 | name | ||
| 1723 | end daeExpRecord; | ||
| 1724 | |||
| 1725 | ✗ | template daeExpRecordCall(Exp rec, Context context, Text &preExp, Text &varDecls, SimCode simCode, Text& extraFuncs, Text& extraFuncsDecl, | |
| 1726 | Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) | ||
| 1727 | "Same as daeExpRecord." | ||
| 1728 | ::= | ||
| 1729 | match rec | ||
| 1730 | case METARECORDCALL(path=path, args=exps, fieldNames=comp) then | ||
| 1731 | let name = tempDecl(underscorePath(path) + "Type", &varDecls) | ||
| 1732 | let ass = List.zip(exps,comp) |> (exp,compn) => | ||
| 1733 | let compnStr = crefStr(makeUntypedCrefIdent(compn)) | ||
| 1734 | '<%name%>.<%compnStr%> = <%daeExp(exp, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)%>;<%\n%>' | ||
| 1735 | let &preExp += ass | ||
| 1736 | name | ||
| 1737 | end daeExpRecordCall; | ||
| 1738 | |||
| 1739 | 1362 | template daeExpCall(Exp call, Context context, Text &preExp /*BUFP*/, Text &varDecls /*BUFP*/,SimCode simCode, Text& extraFuncs,Text& extraFuncsDecl, | |
| 1740 | Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) | ||
| 1741 | "Generates code for a function call." | ||
| 1742 | ::= | ||
| 1743 | //<%name%> | ||
| 1744 | match call | ||
| 1745 | // special builtins | ||
| 1746 | |||
| 1747 | case CALL(path=IDENT(name="edge"), | ||
| 1748 | expLst={e1}) then | ||
| 1749 | let var1 = daeExp(e1, context, &preExp, &varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1750 | '_discrete_events.edge(<%var1%>)' | ||
| 1751 | |||
| 1752 | case CALL(path=IDENT(name="pre"), | ||
| 1753 | expLst={arg as CREF(__)}) then | ||
| 1754 | let var1 = daeExp(arg, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1755 | '_discrete_events->pre(<%var1%>)' | ||
| 1756 | |||
| 1757 | case CALL(path=IDENT(name="previous"), expLst={arg as CREF(__)}) then | ||
| 1758 | '<%daeExp(crefExp(crefPrefixPrevious(arg.componentRef)), context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)%>' | ||
| 1759 | |||
| 1760 | case CALL(path=IDENT(name="firstTick")) then | ||
| 1761 | match context | ||
| 1762 | case SIMULATION_CONTEXT() then | ||
| 1763 | // clockSubactive is checked separately for state equations -- see CodegenCpp.equationSimpleAssign | ||
| 1764 | '(<%contextSystem(context)%>_clockStart[clockIndex - 1])' | ||
| 1765 | else | ||
| 1766 | '(<%contextSystem(context)%>_clockStart[clockIndex - 1] || <%contextSystem(context)%>_clockSubactive[clockIndex - 1])' | ||
| 1767 | |||
| 1768 | case CALL(path=IDENT(name="interval")) then | ||
| 1769 | '<%contextSystem(context)%>_clockInterval[clockIndex - 1]' | ||
| 1770 | |||
| 1771 | case CALL(path=IDENT(name="$_clkfire"), expLst={arg as ICONST(__)}) then | ||
| 1772 | let absClockIdx = absoluteClockIdxForBaseClock(arg.integer, getClockedPartitions(simCode)) | ||
| 1773 | '_time_conditions[<%absClockIdx%> - 1 + <%timeEventLength(simCode)%>] = (_simTime > _clockTime[<%absClockIdx%> - 1])' | ||
| 1774 | |||
| 1775 | case CALL(path=IDENT(name="$getPart"), expLst={e1}) then | ||
| 1776 | daeExp(e1, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1777 | |||
| 1778 | case CALL(path=IDENT(name="sample"), expLst={ICONST(integer=index), start, interval}) then | ||
| 1779 | let &preExp = buffer "" /*BUFD*/ | ||
| 1780 | let eStart = daeExp(start, contextOther, &preExp, &varDecls, simCode, &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1781 | let eInterval = daeExp(interval, contextOther, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1782 | match context | ||
| 1783 | case ALGLOOP_CONTEXT(genInitialisation=false) then | ||
| 1784 | '_system->_time_conditions[<%intSub(index, 1)%>]' | ||
| 1785 | else | ||
| 1786 | '_time_conditions[<%intSub(index, 1)%>]' | ||
| 1787 | case CALL(path=IDENT(name="initial") ) then | ||
| 1788 | match context | ||
| 1789 | |||
| 1790 | case ALGLOOP_CONTEXT(genInitialisation = false) | ||
| 1791 | |||
| 1792 | then '_system->_initial' | ||
| 1793 | else | ||
| 1794 | '_initial' | ||
| 1795 | case CALL(path=IDENT(name="terminal") ) then | ||
| 1796 | match context | ||
| 1797 | |||
| 1798 | case ALGLOOP_CONTEXT(genInitialisation = false) | ||
| 1799 | |||
| 1800 | then '_system->_terminal' | ||
| 1801 | else | ||
| 1802 | '_terminal' | ||
| 1803 | |||
| 1804 | case CALL(path=IDENT(name="DIVISION"), expLst={e1, e2}) then | ||
| 1805 | let var1 = daeExp(e1, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1806 | let var2 = daeExp(e2, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1807 | let var3 = Util.escapeModelicaStringToCString(ExpressionDumpTpl.dumpExp(e2,"\"")) | ||
| 1808 | match context | ||
| 1809 | case ALGLOOP_CONTEXT(genInitialisation = false) | ||
| 1810 | then | ||
| 1811 | << | ||
| 1812 | division(<%var1%>,<%var2%>,!_system->_initial,"<%var3%>") | ||
| 1813 | >> | ||
| 1814 | else | ||
| 1815 | << | ||
| 1816 | division(<%var1%>,<%var2%>,!_initial,"<%var3%>") | ||
| 1817 | >> | ||
| 1818 | end match | ||
| 1819 | |||
| 1820 | |||
| 1821 | case CALL(path=IDENT(name="sign"), | ||
| 1822 | expLst={e1}) then | ||
| 1823 | let var1 = daeExp(e1, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1824 | 'sgn(<%var1%>)' | ||
| 1825 | |||
| 1826 | case CALL(attr=CALL_ATTR(ty=ty as T_ARRAY(dims=dims)), | ||
| 1827 | path=IDENT(name="DIVISION_ARRAY_SCALAR"), | ||
| 1828 | expLst={e1, e2}) then | ||
| 1829 | let type = match ty case T_ARRAY(ty=T_INTEGER(__)) then "int" | ||
| 1830 | case T_ARRAY(ty=T_ENUMERATION(__)) then "int" | ||
| 1831 | else "double" | ||
| 1832 | |||
| 1833 | let var = tempDecl('multi_array<<%type%>,<%listLength(dims)%>>', &varDecls /*BUFD*/) | ||
| 1834 | let var1 = daeExp(e1, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1835 | let var2 = daeExp(e2, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1836 | let var3 = Util.escapeModelicaStringToCString(ExpressionDumpTpl.dumpExp(e2,"\"")) | ||
| 1837 | let &preExp += 'assign_array(<%var%>,divide_array<<%type%>,<%listLength(dims)%>>(<%var1%>, <%var2%>));<%\n%>' | ||
| 1838 | //let &preExp += 'division_alloc_<%type%>_scalar(&<%var1%>, <%var2%>, &<%var%>, "<%var3%>");<%\n%>' | ||
| 1839 | '<%var%>' | ||
| 1840 | |||
| 1841 | case CALL(path=IDENT(name="der"), expLst={arg as CREF(__)}) then | ||
| 1842 | let var = cref2simvar(arg.componentRef, simCode) |> SIMVAR(index=i) => '__zDot[<%i%>]' | ||
| 1843 | '<%var%>' | ||
| 1844 | |||
| 1845 | case CALL(path=IDENT(name="print"), expLst={e1}) then | ||
| 1846 | let var1 = daeExp(e1, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1847 | if acceptMetaModelicaGrammar() then 'print(<%var1%>)' else 'puts(<%var1%>)' | ||
| 1848 | |||
| 1849 | |||
| 1850 | case CALL(path=IDENT(name="integer"), expLst={inExp,index}) then | ||
| 1851 | let exp = daeExp(inExp, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1852 | // let constIndex = daeExp(index, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace,useFlatArrayNotation) | ||
| 1853 | 'integer(<%exp%>)' | ||
| 1854 | |||
| 1855 | |||
| 1856 | case CALL(path=IDENT(name="floor"), expLst={inExp,index}, attr=CALL_ATTR(ty = ty)) then | ||
| 1857 | let exp = daeExp(inExp, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1858 | //let constIndex = daeExp(index, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1859 | 'std::floor(<%exp%>)' | ||
| 1860 | case CALL(path=IDENT(name="floor"), expLst={inExp}, attr=CALL_ATTR(ty = ty)) then | ||
| 1861 | let exp = daeExp(inExp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1862 | //let constIndex = daeExp(index, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1863 | 'std::floor(<%exp%>)' | ||
| 1864 | case CALL(path=IDENT(name="ceil"), expLst={inExp,index}, attr=CALL_ATTR(ty = ty)) then | ||
| 1865 | let exp = daeExp(inExp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1866 | //let constIndex = daeExp(index, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1867 | 'std::ceil(<%exp%>)' | ||
| 1868 | case CALL(path=IDENT(name="ceil"), expLst={inExp}, attr=CALL_ATTR(ty = ty)) then | ||
| 1869 | let exp = daeExp(inExp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl , extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1870 | //let constIndex = daeExp(index, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1871 | 'std::ceil(<%exp%>)' | ||
| 1872 | |||
| 1873 | case CALL(path=IDENT(name="integer"), expLst={inExp}) then | ||
| 1874 | let exp = daeExp(inExp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1875 | 'integer(<%exp%>)' | ||
| 1876 | |||
| 1877 | case CALL(path=IDENT(name="max"), attr=CALL_ATTR(ty = T_REAL(__)), expLst={e1,e2}) then | ||
| 1878 | let var1 = daeExp(e1, context, &preExp, &varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1879 | let var2 = daeExp(e2, context, &preExp, &varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1880 | 'max(<%var1%>,<%var2%>)' | ||
| 1881 | |||
| 1882 | case CALL(path=IDENT(name="max"), expLst={e1,e2}) then | ||
| 1883 | let var1 = daeExp(e1, context, &preExp, &varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1884 | let var2 = daeExp(e2, context, &preExp, &varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1885 | 'max(<%var1%>,<%var2%>)' | ||
| 1886 | |||
| 1887 | case CALL(attr=CALL_ATTR(ty = T_REAL(__)), | ||
| 1888 | path=IDENT(name="min"), expLst={e1,e2}) then | ||
| 1889 | let var1 = daeExp(e1, context, &preExp, &varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1890 | let var2 = daeExp(e2, context, &preExp, &varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1891 | 'min(<%var1%>,<%var2%>)' | ||
| 1892 | |||
| 1893 | case CALL(path=IDENT(name="min"), expLst={e1,e2}) then | ||
| 1894 | let var1 = daeExp(e1, context, &preExp, &varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1895 | let var2 = daeExp(e2, context, &preExp, &varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1896 | 'min(<%var1%>,<%var2%>)' | ||
| 1897 | |||
| 1898 | case CALL(path=IDENT(name="abs"), expLst={e1}, attr=CALL_ATTR(ty = T_INTEGER(__))) then | ||
| 1899 | let var1 = daeExp(e1, context, &preExp, &varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1900 | 'std::labs(<%var1%>)' | ||
| 1901 | |||
| 1902 | case CALL(path=IDENT(name="abs"), expLst={e1}) then | ||
| 1903 | let var1 = daeExp(e1, context, &preExp, &varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1904 | 'std::abs(<%var1%>)' | ||
| 1905 | |||
| 1906 | case CALL(path=IDENT(name="sqrt"), | ||
| 1907 | expLst={e1},attr=attr as CALL_ATTR(__)) then | ||
| 1908 | let argStr = (expLst |> exp => '<%daeExp(exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)%>' ;separator=", ") | ||
| 1909 | (if isPositiveOrZero(e1) then | ||
| 1910 | let typeStr = expTypeShort(attr.ty ) | ||
| 1911 | let retVar = tempDecl(typeStr, &varDecls /*BUFD*/) | ||
| 1912 | let &preExp += '<%retVar%> = std::sqrt(<%argStr%>);<%\n%>' | ||
| 1913 | |||
| 1914 | '<%retVar%>' | ||
| 1915 | else | ||
| 1916 | let tmp = tempDecl(expTypeFromExpModelica(e1), &varDecls) | ||
| 1917 | let cstr = ExpressionDumpTpl.dumpExp(e1,"\"") | ||
| 1918 | let &preExp += | ||
| 1919 | << | ||
| 1920 | <%tmp%> = <%argStr%>; | ||
| 1921 | <%assertCommonVar('<%tmp%> >= 0.0', '"Model error: Argument of sqrt(<%Util.escapeModelicaStringToCString(cstr)%>) should be >= 0"', context, &varDecls, Absyn.dummyInfo)%> | ||
| 1922 | >> | ||
| 1923 | 'sqrt(<%tmp%>)') | ||
| 1924 | |||
| 1925 | // built-in mathematical functions | ||
| 1926 | case CALL(path=IDENT(name="sin"), expLst={e1}) | ||
| 1927 | case CALL(path=IDENT(name="cos"), expLst={e1}) | ||
| 1928 | case CALL(path=IDENT(name="tan"), expLst={e1}) | ||
| 1929 | case CALL(path=IDENT(name="asin"), expLst={e1}) | ||
| 1930 | case CALL(path=IDENT(name="acos"), expLst={e1}) | ||
| 1931 | case CALL(path=IDENT(name="atan"), expLst={e1}) | ||
| 1932 | case CALL(path=IDENT(name="sinh"), expLst={e1}) | ||
| 1933 | case CALL(path=IDENT(name="cosh"), expLst={e1}) | ||
| 1934 | case CALL(path=IDENT(name="tanh"), expLst={e1}) | ||
| 1935 | case CALL(path=IDENT(name="exp"), expLst={e1}) | ||
| 1936 | case CALL(path=IDENT(name="log"), expLst={e1}) | ||
| 1937 | case CALL(path=IDENT(name="log10"), expLst={e1}) then | ||
| 1938 | let argStr = (expLst |> exp => '<%daeExp(exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)%>' ;separator=", ") | ||
| 1939 | let funName = '<%underscorePath(path)%>' | ||
| 1940 | 'std::<%funName%>(<%argStr%>)' | ||
| 1941 | |||
| 1942 | case CALL(path=IDENT(name="atan2"), | ||
| 1943 | expLst={e1,e2},attr=attr as CALL_ATTR(__)) then | ||
| 1944 | let argStr = (expLst |> exp => '<%daeExp(exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)%>' ;separator=", ") | ||
| 1945 | |||
| 1946 | let retType = 'double' | ||
| 1947 | let retVar = tempDecl(retType, &varDecls /*BUFD*/) | ||
| 1948 | let &preExp += '<%retVar%> = std::atan2(<%argStr%>);<%\n%>' | ||
| 1949 | '<%retVar%>' | ||
| 1950 | |||
| 1951 | case CALL(path=IDENT(name="smooth"), | ||
| 1952 | expLst={e1,e2},attr=attr as CALL_ATTR(__)) then | ||
| 1953 | let var1 = daeExp(e1, context, &preExp, &varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1954 | let var2 = daeExp(e2, context, &preExp, &varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1955 | '<%var2%>' | ||
| 1956 | |||
| 1957 | case CALL(path=IDENT(name="homotopy"), | ||
| 1958 | expLst={e1,e2},attr=attr as CALL_ATTR(__)) then | ||
| 1959 | let var1 = daeExp(e1, context, &preExp, &varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1960 | let var2 = daeExp(e2, context, &preExp, &varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1961 | '<%var1%>' | ||
| 1962 | |||
| 1963 | case CALL(path=IDENT(name="div"), expLst={e1,e2}, attr=CALL_ATTR(ty = T_INTEGER(__))) then | ||
| 1964 | let var1 = daeExp(e1, context, &preExp, &varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1965 | let var2 = daeExp(e2, context, &preExp, &varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1966 | 'ldiv(<%var1%>,<%var2%>).quot' | ||
| 1967 | |||
| 1968 | case CALL(path=IDENT(name="div"), expLst={e1,e2}) then | ||
| 1969 | let var1 = daeExp(e1, context, &preExp, &varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1970 | let var2 = daeExp(e2, context, &preExp, &varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1971 | 'omcpp::trunc(<%var1%>/<%var2%>)' | ||
| 1972 | |||
| 1973 | case CALL(path=IDENT(name="div"), expLst={e1,e2,index}) then | ||
| 1974 | // TODO: should trigger event if result changes discontinuously | ||
| 1975 | let var1 = daeExp(e1, context, &preExp, &varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1976 | let var2 = daeExp(e2, context, &preExp, &varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1977 | 'omcpp::trunc(<%var1%>/<%var2%>)' | ||
| 1978 | |||
| 1979 | case CALL(path=IDENT(name="mod"), expLst={e1,e2,index}, attr=attr as CALL_ATTR(__)) | ||
| 1980 | case CALL(path=IDENT(name="mod"), expLst={e1,e2}, attr=attr as CALL_ATTR(__)) then | ||
| 1981 | let var1 = daeExp(e1, context, &preExp, &varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1982 | let var2 = daeExp(e2, context, &preExp, &varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1983 | 'modelica_mod_<%expTypeShort(attr.ty)%>(<%var1%>,<%var2%>)' | ||
| 1984 | |||
| 1985 | case CALL(path=IDENT(name="semiLinear"), expLst={e1,e2,e3}, attr=attr as CALL_ATTR(__)) then | ||
| 1986 | let var1 = daeExp(e1, context, &preExp, &varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1987 | let var2 = daeExp(e2, context, &preExp, &varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1988 | let var3 = daeExp(e3, context, &preExp, &varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1989 | 'semiLinear(<%var1%>,<%var2%>,<%var3%>)' | ||
| 1990 | |||
| 1991 | case CALL(path=IDENT(name="max"), attr=CALL_ATTR(ty = ty), expLst={array}) then | ||
| 1992 | let expVar = daeExp(array, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 1993 | let arr_tp_str = expTypeShort(ty) | ||
| 1994 | let tvar = tempDecl(arr_tp_str, &varDecls /*BUFD*/) | ||
| 1995 | let &preExp += '<%tvar%> = min_max<<%arr_tp_str%>>(<%expVar%>).second;<%\n%>' | ||
| 1996 | '<%tvar%>' | ||
| 1997 | |||
| 1998 | case CALL(path=IDENT(name="min"), attr=CALL_ATTR(ty = ty), expLst={array}) then | ||
| 1999 | let expVar = daeExp(array, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2000 | let arr_tp_str = expTypeShort(ty) | ||
| 2001 | let tvar = tempDecl(arr_tp_str, &varDecls /*BUFD*/) | ||
| 2002 | let &preExp += '<%tvar%> = min_max<<%arr_tp_str%>>(<%expVar%>).first;<%\n%>' | ||
| 2003 | '<%tvar%>' | ||
| 2004 | |||
| 2005 | case CALL(path=IDENT(name="sum"), attr=CALL_ATTR(ty = ty), expLst={array}) then | ||
| 2006 | let expVar = daeExp(array, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2007 | let arr_tp_str = expTypeShort(ty) | ||
| 2008 | 'sum_array<<%arr_tp_str%>>(<%expVar%>)' | ||
| 2009 | |||
| 2010 | case CALL(path=IDENT(name="product"), attr=CALL_ATTR(ty = ty), expLst={array}) then | ||
| 2011 | let expVar = daeExp(array, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2012 | let arr_tp_str = expTypeShort(ty) | ||
| 2013 | 'product_array<<%arr_tp_str%>>(<%expVar%>)' | ||
| 2014 | |||
| 2015 | case call as CALL(path=IDENT(name="vector"), expLst={exp}, attr=CALL_ATTR(ty=ty)) then | ||
| 2016 | let expVar = daeExp(exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2017 | let tvar = match ty | ||
| 2018 | case T_ARRAY(ty=elty) then | ||
| 2019 | // use dynamic array as static arrays are treated during translation | ||
| 2020 | 'DynArrayDim1<<%expTypeShort(elty)%>>(<%expVar%>.getNumElems(), ConstArray(<%expVar%>).getData())' | ||
| 2021 | else | ||
| 2022 | // this should never happen because it is eliminated during translation | ||
| 2023 | 'StatArrayDim1<<%expTypeShort(ty)%>, 1, true>(&<%expVar%>)' | ||
| 2024 | end match | ||
| 2025 | '<%tvar%>' | ||
| 2026 | |||
| 2027 | case CALL(path=IDENT(name="fill"), expLst=val::dims, attr=attr as CALL_ATTR(__)) then | ||
| 2028 | let valExp = daeExp(val, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2029 | let dimsExp = (dims |> dim => daeExp(dim, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) ;separator=",") | ||
| 2030 | |||
| 2031 | let ty_str = '<%expTypeShort(attr.ty)%>' | ||
| 2032 | //previous multi_array | ||
| 2033 | // let tmp_type_str = 'multi_array<<%ty_str%>,<%listLength(dims)%>>' | ||
| 2034 | let tmp_type_str = 'DynArrayDim<%listLength(dims)%><<%ty_str%>>' | ||
| 2035 | |||
| 2036 | let tvar = tempDecl(tmp_type_str, &varDecls /*BUFD*/) | ||
| 2037 | |||
| 2038 | let &preExp += '<%tvar%>.setDims(<%dimsExp%>);<%\n%>' | ||
| 2039 | |||
| 2040 | let &preExp += 'fill_array<<%ty_str%>>(<%tvar%>, <%valExp%>);<%\n%>' | ||
| 2041 | '<%tvar%>' | ||
| 2042 | |||
| 2043 | case CALL(path=IDENT(name="cat"), expLst=dim::a0::arrays, attr=attr as CALL_ATTR(__)) then | ||
| 2044 | let dim_exp = daeExp(dim, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2045 | let& dimstr = buffer "" | ||
| 2046 | let tmp_type_str = match typeof(a0) | ||
| 2047 | case ty as T_ARRAY(dims=dims) then | ||
| 2048 | let &dimstr += listLength(dims) | ||
| 2049 | 'DynArrayDim<%listLength(dims)%><<%expTypeShort(ty)%>>' | ||
| 2050 | else | ||
| 2051 | let &dimstr += 'error array dims' | ||
| 2052 | 'array error' | ||
| 2053 | let ty_str = '<%expTypeShort(attr.ty)%>' | ||
| 2054 | let tvar = tempDecl(tmp_type_str, &varDecls /*BUFD*/) | ||
| 2055 | let a0str = daeExp(a0, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2056 | let arrays_exp = (arrays |> array => | ||
| 2057 | '<%tvar%>_list.push_back(&<%daeExp(array, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)%>);' ;separator="\n") | ||
| 2058 | let &preExp += | ||
| 2059 | << | ||
| 2060 | vector<const BaseArray<<%ty_str%>>*> <%tvar%>_list; | ||
| 2061 | <%tvar%>_list.push_back(&<%a0str%>); | ||
| 2062 | <%arrays_exp%> | ||
| 2063 | cat_array<<%ty_str%>>(<%dim_exp%>, <%tvar%>_list, <%tvar%>);<%\n%> | ||
| 2064 | >> | ||
| 2065 | '<%tvar%>' | ||
| 2066 | |||
| 2067 | case CALL(path=IDENT(name="promote"), expLst={A, n}, attr=attr as CALL_ATTR(ty=ty)) then | ||
| 2068 | //match A | ||
| 2069 | //case component as CREF(componentRef=cr, ty=ty) then | ||
| 2070 | let var1 = daeExp(A, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2071 | let var2 = daeExp(n, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2072 | //let temp = tempDeclAssign('const size_t*', &varDecls /*BUFD*/,'<%var1%>.shape()') | ||
| 2073 | //let temp_ex = tempDecl('std::vector<size_t>', &varDecls /*BUFD*/) | ||
| 2074 | let arrayType = expTypeArrayIf(ty) | ||
| 2075 | //let dimstr = listLength(crefSubs(cr)) | ||
| 2076 | let tmp = tempDecl('<%arrayType%>', &varDecls /*BUFD*/) | ||
| 2077 | |||
| 2078 | // let arr_tp_str = '<%expTypeFromExpArray(A)%>' | ||
| 2079 | //let tvar = tempDecl(arr_tp_str, &varDecls /*BUFD*/) | ||
| 2080 | let &preExp += 'promote_array(<%var2%>, <%var1%>, <%tmp%>);<%\n%>' | ||
| 2081 | |||
| 2082 | '<%tmp%> ' | ||
| 2083 | //else | ||
| 2084 | //'promote array error' | ||
| 2085 | case CALL(path=IDENT(name="transpose"), expLst={A}) then | ||
| 2086 | let var1 = daeExp(A, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2087 | let type_str = expTypeFromExpShort(A) | ||
| 2088 | let arr_tp_str = '<%expTypeFromExpArray(A)%>' | ||
| 2089 | let tvar = tempDecl(arr_tp_str, &varDecls /*BUFD*/) | ||
| 2090 | let &preExp += 'transpose_array<<%type_str%>>(<%var1%>, <%tvar%>);<%\n%>' | ||
| 2091 | '<%tvar%>' | ||
| 2092 | |||
| 2093 | case CALL(path=IDENT(name="cross"), expLst={v1, v2},attr=CALL_ATTR(ty=ty as T_ARRAY(dims=dims))) then | ||
| 2094 | let var1 = daeExp(v1, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2095 | let var2 = daeExp(v2, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2096 | let type = match ty case T_ARRAY(ty=T_INTEGER(__)) then 'int' | ||
| 2097 | case T_ARRAY(ty=T_ENUMERATION(__)) then 'int' | ||
| 2098 | else 'double' | ||
| 2099 | let tvar = tempDecl('multi_array<<%type%>,<%listLength(dims)%>>', &varDecls /*BUFD*/) | ||
| 2100 | let &preExp += 'assign_array(<%tvar%>,cross_array<<%type%>>(<%var1%>,<%var2%>));<%\n%>' | ||
| 2101 | '<%tvar%>' | ||
| 2102 | |||
| 2103 | case CALL(path=IDENT(name="identity"), expLst={A}) then | ||
| 2104 | let var1 = daeExp(A, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2105 | let tvar = tempDecl('DynArrayDim2<int>', &varDecls) | ||
| 2106 | let &preExp += 'identity_alloc(<%var1%>, <%tvar%>);<%\n%>' | ||
| 2107 | '<%tvar%>' | ||
| 2108 | |||
| 2109 | case CALL(path=IDENT(name="diagonal"), expLst={v}, attr=CALL_ATTR(ty=ty as T_ARRAY(ty=elty))) then | ||
| 2110 | let var1 = daeExp(v, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2111 | let tvar = tempDecl('DynArrayDim2<<%expTypeShort(elty)%>>', &varDecls) | ||
| 2112 | let &preExp += 'diagonal_alloc(<%var1%>, <%tvar%>);<%\n%>' | ||
| 2113 | '<%tvar%>' | ||
| 2114 | |||
| 2115 | case CALL(path=IDENT(name="rem"), expLst={e1, e2}) then | ||
| 2116 | let var1 = daeExp(e1, context, &preExp, &varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2117 | let var2 = daeExp(e2, context, &preExp, &varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2118 | let typeStr = expTypeFromExpShort(e1) | ||
| 2119 | 'modelica_rem_<%typeStr%>(<%var1%>,<%var2%>)' | ||
| 2120 | |||
| 2121 | |||
| 2122 | case CALL(path=IDENT(name="String"), | ||
| 2123 | expLst={s, format}) then | ||
| 2124 | let emptybuf = "" | ||
| 2125 | let tvar = tempDecl("string", &varDecls /*BUFD*/) | ||
| 2126 | let sExp = daeExp(s, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2127 | let formatExp = daeExp(format, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2128 | let typeStr = expTypeFromExpModelica(s) | ||
| 2129 | let &preExp += 'string <%tvar%> = omcpp::to_string(<%sExp%>);<%\n%>' | ||
| 2130 | '<%tvar%>' | ||
| 2131 | |||
| 2132 | case CALL(path=IDENT(name="String"), | ||
| 2133 | expLst={s, minlen, leftjust}) then | ||
| 2134 | let emptybuf = "" | ||
| 2135 | let tvar = tempDecl("string", &emptybuf /*BUFD*/) | ||
| 2136 | let sExp = daeExp(s, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2137 | let minlenExp = daeExp(minlen, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2138 | let leftjustExp = daeExp(leftjust, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2139 | let typeStr = expTypeFromExpModelica(s) | ||
| 2140 | let &preExp += 'string <%tvar%> = omcpp::to_string(<%sExp%>);<%\n%>' | ||
| 2141 | '<%tvar%>' | ||
| 2142 | |||
| 2143 | |||
| 2144 | //hierhier todo | ||
| 2145 | case CALL(path=IDENT(name="String"), | ||
| 2146 | expLst={s, minlen, leftjust, signdig}) then | ||
| 2147 | let emptybuf = "" | ||
| 2148 | let tvar = tempDecl("string", &emptybuf /*BUFD*/) | ||
| 2149 | let sExp = daeExp(s, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2150 | let minlenExp = daeExp(minlen, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2151 | let leftjustExp = daeExp(leftjust, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2152 | let signdigExp = daeExp(signdig, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2153 | let &preExp += 'string <%tvar%> = omcpp::to_string(<%sExp%>);<%\n%>' | ||
| 2154 | '<%tvar%>' | ||
| 2155 | |||
| 2156 | case CALL(path=IDENT(name="delay"), | ||
| 2157 | expLst={ICONST(integer=index), e, d, delayMax}) then | ||
| 2158 | let tvar = tempDecl("double", &varDecls /*BUFD*/) | ||
| 2159 | let var1 = daeExp(e, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2160 | let var2 = daeExp(d, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2161 | let var3 = daeExp(delayMax, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2162 | let &preExp += '<%tvar%> = delay(<%index%>, <%var1%>, <%var2%>, <%var3%>);<%\n%>' | ||
| 2163 | '<%tvar%>' | ||
| 2164 | |||
| 2165 | |||
| 2166 | case CALL(path=IDENT(name="integer"), | ||
| 2167 | expLst={toBeCasted}) then | ||
| 2168 | let castedVar = daeExp(toBeCasted, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2169 | '((int)<%castedVar%>)' | ||
| 2170 | |||
| 2171 | case CALL(path=IDENT(name="Integer"), | ||
| 2172 | expLst={toBeCasted}) then | ||
| 2173 | let castedVar = daeExp(toBeCasted, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2174 | '((int)<%castedVar%>)' | ||
| 2175 | |||
| 2176 | case CALL(path=IDENT(name="clock"), expLst={}) then | ||
| 2177 | 'mmc_clock()' | ||
| 2178 | |||
| 2179 | case CALL(path=IDENT(name="noEvent"), | ||
| 2180 | expLst={e1}) then | ||
| 2181 | daeExp(e1, context, &preExp, &varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2182 | |||
| 2183 | case CALL(path=IDENT(name="anyString"), | ||
| 2184 | expLst={e1}) then | ||
| 2185 | 'mmc_anyString(<%daeExp(e1, context, &preExp, &varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)%>)' | ||
| 2186 | |||
| 2187 | case CALL(path=IDENT(name="mmc_get_field"), | ||
| 2188 | expLst={s1, ICONST(integer=i)}) then | ||
| 2189 | let tvar = tempDecl("modelica_metatype", &varDecls /*BUFD*/) | ||
| 2190 | let expPart = daeExp(s1, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2191 | let &preExp += '<%tvar%> = MMC_FETCH(MMC_OFFSET(MMC_UNTAGPTR(<%expPart%>), <%i%>));<%\n%>' | ||
| 2192 | '<%tvar%>' | ||
| 2193 | |||
| 2194 | case exp as CALL(attr=attr as CALL_ATTR(ty=T_NORETCALL(__))) then | ||
| 2195 | let argStr = (expLst |> exp => '<%daeExp(exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)%>' ;separator=", ") | ||
| 2196 | let funName = '<%underscorePath(path)%>' | ||
| 2197 | let &preExp += '<%contextFunName(funName, context)%>(<%argStr%>);<%\n%>' | ||
| 2198 | "" | ||
| 2199 | /*Function calls with array return type*/ | ||
| 2200 | case exp as CALL(attr=attr as CALL_ATTR(ty=T_ARRAY(ty=ty,dims=dims))) then | ||
| 2201 | |||
| 2202 | let argStr = (expLst |> exp => '<%daeExp(exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)%>' ;separator=",") | ||
| 2203 | let funName = '<%underscorePath(path)%>' | ||
| 2204 | let retType = '<%funName%>RetType' | ||
| 2205 | let retVar = tempDecl(retType, &varDecls) | ||
| 2206 | let &preExp += '<%contextFunName(funName, context)%>(<%argStr%><%if expLst then if retVar then "," %><%retVar%>);<%\n%>' | ||
| 2207 | '<%retVar%>' | ||
| 2208 | /*Function calls with tuple return type | ||
| 2209 | case exp as CALL(attr=attr as CALL_ATTR(ty=T_TUPLE(__))) then | ||
| 2210 | then "Tuple not supported yet" | ||
| 2211 | */ | ||
| 2212 | /*Function calls with default type*/ | ||
| 2213 | case exp as CALL(expLst = explist,attr=attr as CALL_ATTR(ty =ty)) then | ||
| 2214 | |||
| 2215 | let funName = '<%underscorePath(path)%>' | ||
| 2216 | /*workaround until we support this*/ | ||
| 2217 | match funName | ||
| 2218 | case "Modelica_Utilities_Files_loadResource" | ||
| 2219 | then | ||
| 2220 | '"noName"' | ||
| 2221 | else | ||
| 2222 | /*end workaround*/ | ||
| 2223 | let argStr = (explist |> exp => '<%daeExp(exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)%>' ;separator=", ") | ||
| 2224 | let retType = '<%funName%>RetType' | ||
| 2225 | let retVar = tempDecl(retType, &varDecls) | ||
| 2226 | let &preExp += match context case FUNCTION_CONTEXT(__) then'<%funName%>(<%argStr%><%if explist then if retVar then "," %><%if retVar then '<%retVar%>'%>);<%\n%>' | ||
| 2227 | else '<%contextFunName(funName, context)%>(<%argStr%><%if explist then if retVar then "," %> <%if retVar then '<%retVar%>'%>);<%\n%>' | ||
| 2228 | '<%retVar%>' | ||
| 2229 | |||
| 2230 | else | ||
| 2231 | error(sourceInfo(), 'Code generation does not support daeExpCall(<%ExpressionDumpTpl.dumpExp(call,"\"")%>)') | ||
| 2232 | end daeExpCall; | ||
| 2233 | |||
| 2234 | 570 | template daeExpLunary(Exp exp, Context context, Text &preExp, Text &varDecls, SimCode simCode, Text& extraFuncs, Text& extraFuncsDecl, | |
| 2235 | Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) | ||
| 2236 | "Generates code for a logical unary expression." | ||
| 2237 | ::= | ||
| 2238 | match exp | ||
| 2239 | case LUNARY(__) then | ||
| 2240 | let e = daeExp(exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2241 | match operator | ||
| 2242 | case NOT(__) then '(!<%e%>)' | ||
| 2243 | end daeExpLunary; | ||
| 2244 | |||
| 2245 | 553 | template daeExpLbinary(Exp exp, Context context, Text &preExp, Text &varDecls, SimCode simCode, Text& extraFuncs,Text& extraFuncsDecl, | |
| 2246 | Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) | ||
| 2247 | "Generates code for a logical binary expression." | ||
| 2248 | ::= | ||
| 2249 | match exp | ||
| 2250 | case LBINARY(__) then | ||
| 2251 | let e1 = daeExp(exp1, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2252 | let e2 = daeExp(exp2, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2253 | match operator | ||
| 2254 | case AND(__) then '(<%e1%> && <%e2%>)' | ||
| 2255 | case OR(__) then '(<%e1%> || <%e2%>)' | ||
| 2256 | else "daeExpLbinary:ERR" | ||
| 2257 | end daeExpLbinary; | ||
| 2258 | |||
| 2259 | 982 | template expTypeFromExpFlag(Exp exp, Integer flag) | |
| 2260 | "Generate type helper." | ||
| 2261 | ::= | ||
| 2262 | match exp | ||
| 2263 | case ICONST(__) then match flag case 8 then "int" case 1 then "int" else "int" | ||
| 2264 | case RCONST(__) then match flag case 1 then "double" else "double" | ||
| 2265 | case SCONST(__) then match flag case 1 then "string" else "modelica_string" | ||
| 2266 | case BCONST(__) then match flag case 1 then "bool" else "modelica_boolean" | ||
| 2267 | case ENUM_LITERAL(__) then match flag case 8 then "int" case 1 then "int" else "int" | ||
| 2268 | case e as BINARY(__) | ||
| 2269 | case e as UNARY(__) | ||
| 2270 | case e as LBINARY(__) | ||
| 2271 | case e as LUNARY(__) | ||
| 2272 | case e as RELATION(__) then expTypeFromOpFlag(e.operator, flag) | ||
| 2273 | case IFEXP(__) then expTypeFromExpFlag(expThen, flag) | ||
| 2274 | case CALL(attr=CALL_ATTR(__)) then expTypeFlag(attr.ty, flag) | ||
| 2275 | case c as RECORD(__) then expTypeFlag(c.ty, flag) | ||
| 2276 | case c as ARRAY(__) | ||
| 2277 | case c as MATRIX(__) | ||
| 2278 | case c as RANGE(__) | ||
| 2279 | case c as CAST(__) | ||
| 2280 | case c as TSUB(__) | ||
| 2281 | case c as CREF(__) | ||
| 2282 | case c as CODE(__) then expTypeFlag(c.ty, flag) | ||
| 2283 | case c as ASUB(__) then expTypeFlag(typeof(c), flag) | ||
| 2284 | case REDUCTION(__) then expTypeFlag(typeof(exp), flag) | ||
| 2285 | case e as CONS(__) | ||
| 2286 | case e as LIST(__) | ||
| 2287 | case e as SIZE(__) then expTypeFlag(typeof(e), flag) | ||
| 2288 | case c as RSUB(ix=-1) then expTypeFlag(c.ty, flag) | ||
| 2289 | |||
| 2290 | case META_TUPLE(__) | ||
| 2291 | case META_OPTION(__) | ||
| 2292 | case MATCHEXPRESSION(__) | ||
| 2293 | case METARECORDCALL(__) | ||
| 2294 | case RSUB(__) | ||
| 2295 | case BOX(__) then match flag case 1 then "metatype" else "modelica_metatype" | ||
| 2296 | case c as UNBOX(__) then expTypeFlag(c.ty, flag) | ||
| 2297 | case c as SHARED_LITERAL(__) then expTypeFromExpFlag(c.exp, flag) | ||
| 2298 | else error(sourceInfo(), 'expTypeFromExpFlag(flag=<%flag%>):<%ExpressionDumpTpl.dumpExp(exp,"\"")%>') | ||
| 2299 | end expTypeFromExpFlag; | ||
| 2300 | |||
| 2301 | 26 | template expTypeFromOpFlag(Operator op, Integer flag) | |
| 2302 | "Generate type helper." | ||
| 2303 | ::= | ||
| 2304 | match op | ||
| 2305 | case o as ADD(__) | ||
| 2306 | case o as SUB(__) | ||
| 2307 | case o as MUL(__) | ||
| 2308 | case o as DIV(__) | ||
| 2309 | case o as POW(__) | ||
| 2310 | |||
| 2311 | case o as UMINUS(__) | ||
| 2312 | case o as UMINUS_ARR(__) | ||
| 2313 | case o as ADD_ARR(__) | ||
| 2314 | case o as SUB_ARR(__) | ||
| 2315 | case o as MUL_ARR(__) | ||
| 2316 | case o as DIV_ARR(__) | ||
| 2317 | case o as MUL_ARRAY_SCALAR(__) | ||
| 2318 | case o as ADD_ARRAY_SCALAR(__) | ||
| 2319 | case o as SUB_SCALAR_ARRAY(__) | ||
| 2320 | case o as MUL_SCALAR_PRODUCT(__) | ||
| 2321 | case o as MUL_MATRIX_PRODUCT(__) | ||
| 2322 | case o as DIV_ARRAY_SCALAR(__) | ||
| 2323 | case o as DIV_SCALAR_ARRAY(__) | ||
| 2324 | case o as POW_ARRAY_SCALAR(__) | ||
| 2325 | case o as POW_SCALAR_ARRAY(__) | ||
| 2326 | case o as POW_ARR(__) | ||
| 2327 | case o as POW_ARR2(__) | ||
| 2328 | case o as LESS(__) | ||
| 2329 | case o as LESSEQ(__) | ||
| 2330 | case o as GREATER(__) | ||
| 2331 | case o as GREATEREQ(__) | ||
| 2332 | case o as EQUAL(__) | ||
| 2333 | case o as NEQUAL(__) then | ||
| 2334 | expTypeFlag(o.ty, flag) | ||
| 2335 | case o as AND(__) | ||
| 2336 | case o as OR(__) | ||
| 2337 | case o as NOT(__) then | ||
| 2338 | match flag case 1 then "bool" else "modelica_boolean" | ||
| 2339 | else "expTypeFromOpFlag:ERROR" | ||
| 2340 | end expTypeFromOpFlag; | ||
| 2341 | |||
| 2342 | |||
| 2343 | 52155 | template daeExpBinary(Operator it, Exp exp1, Exp exp2, Context context, Text &preExp, Text &varDecls, SimCode simCode, Text& extraFuncs, Text& extraFuncsDecl, | |
| 2344 | Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) | ||
| 2345 | ::= | ||
| 2346 | let e1 = daeExp(exp1, context, &preExp, &varDecls, simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2347 | let e2 = daeExp(exp2, context, &preExp, &varDecls, simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2348 | match it | ||
| 2349 | case ADD(__) then '(<%e1%> + <%e2%>)' | ||
| 2350 | case SUB(__) then '(<%e1%> - <%e2%>)' | ||
| 2351 | case MUL(__) then '(<%e1%> * <%e2%>)' | ||
| 2352 | case DIV(__) then | ||
| 2353 | let e2str = Util.escapeModelicaStringToCString(ExpressionDumpTpl.dumpExp(exp2,"\"")) | ||
| 2354 | match context | ||
| 2355 | case ALGLOOP_CONTEXT(genInitialisation = false) | ||
| 2356 | then | ||
| 2357 | << | ||
| 2358 | division(<%e1%>,<%e2%>,!_system->_initial,"<%e2str%>") | ||
| 2359 | >> | ||
| 2360 | else | ||
| 2361 | << | ||
| 2362 | division(<%e1%>,<%e2%>,!_initial,"<%e2str%>") | ||
| 2363 | >> | ||
| 2364 | end match | ||
| 2365 | case POW(__) then 'std::pow(<%e1%>, <%e2%>)' | ||
| 2366 | case AND(__) then '(<%e1%> && <%e2%>)' | ||
| 2367 | case OR(__) then '(<%e1%> || <%e2%>)' | ||
| 2368 | case MUL_ARRAY_SCALAR(ty=T_ARRAY(dims=dims)) then | ||
| 2369 | let type = expTypeShort(ty.ty) | ||
| 2370 | let tvar = tempDecl(expTypeArrayDims(ty.ty, dims), &varDecls /*BUFD*/) | ||
| 2371 | let &preExp += 'multiply_array<<%type%>>(<%e1%>, <%e2%>, <%tvar%>);<%\n%>' | ||
| 2372 | '<%tvar%>' | ||
| 2373 | case MUL_MATRIX_PRODUCT(ty=T_ARRAY(dims=dims)) then | ||
| 2374 | let type = expTypeShort(ty.ty) | ||
| 2375 | let tvar = tempDecl(expTypeArrayDims(ty.ty, dims), &varDecls /*BUFD*/) | ||
| 2376 | let &preExp += 'multiply_array<<%type%>>(<%e1%>, <%e2%>, <%tvar%>);<%\n%>' | ||
| 2377 | '<%tvar%>' | ||
| 2378 | case DIV_ARRAY_SCALAR(ty=T_ARRAY(dims=dims)) then | ||
| 2379 | let type = expTypeShort(ty.ty) | ||
| 2380 | let tvar = tempDecl(expTypeArrayDims(ty.ty, dims), &varDecls /*BUFD*/) | ||
| 2381 | let &preExp += 'divide_array<<%type%>>(<%e1%>, <%e2%>, <%tvar%>);<%\n%>' | ||
| 2382 | '<%tvar%>' | ||
| 2383 | case DIV_SCALAR_ARRAY(ty=T_ARRAY(dims=dims)) then | ||
| 2384 | let type = expTypeShort(ty.ty) | ||
| 2385 | let tvar = tempDecl(expTypeArrayDims(ty.ty, dims), &varDecls /*BUFD*/) | ||
| 2386 | let &preExp += 'divide_array<<%type%>>(<%e1%>, <%e2%>, <%tvar%>);<%\n%>' | ||
| 2387 | '<%tvar%>' | ||
| 2388 | case UMINUS(__) then "daeExpBinary:ERR UMINUS not supported" | ||
| 2389 | case UMINUS_ARR(__) then "daeExpBinary:ERR UMINUS_ARR not supported" | ||
| 2390 | case ADD_ARR(ty=T_ARRAY(dims=dims)) then | ||
| 2391 | let type = expTypeShort(ty.ty) | ||
| 2392 | let tvar = tempDecl(expTypeArrayDims(ty.ty, dims), &varDecls /*BUFD*/) | ||
| 2393 | let &preExp += 'add_array<<%type%>>(<%e1%>, <%e2%>, <%tvar%>);<%\n%>' | ||
| 2394 | '<%tvar%>' | ||
| 2395 | case SUB_ARR(ty=T_ARRAY(dims=dims)) then | ||
| 2396 | let type = expTypeShort(ty.ty) | ||
| 2397 | let tvar = tempDecl(expTypeArrayDims(ty.ty, dims), &varDecls /*BUFD*/) | ||
| 2398 | let &preExp += 'subtract_array<<%type%>>(<%e1%>, <%e2%>, <%tvar%>);<%\n%>' | ||
| 2399 | '<%tvar%>' | ||
| 2400 | case MUL_ARR(ty=T_ARRAY(dims=dims)) then | ||
| 2401 | let type = expTypeShort(ty.ty) | ||
| 2402 | let tvar = tempDecl(expTypeArrayDims(ty.ty, dims), &varDecls /*BUFD*/) | ||
| 2403 | let &preExp += 'multiply_array_elem_wise<<%type%>>(<%e1%>, <%e2%>, <%tvar%>);<%\n%>' | ||
| 2404 | '<%tvar%>' | ||
| 2405 | case DIV_ARR(ty=T_ARRAY(dims=dims)) then | ||
| 2406 | let type = expTypeShort(ty.ty) | ||
| 2407 | let tvar = tempDecl(expTypeArrayDims(ty.ty, dims), &varDecls /*BUFD*/) | ||
| 2408 | let &preExp += 'divide_array_elem_wise<<%type%>>(<%e1%>, <%e2%>, <%tvar%>);<%\n%>' | ||
| 2409 | '<%tvar%>' | ||
| 2410 | case ADD_ARRAY_SCALAR(ty=T_ARRAY(dims=dims)) then | ||
| 2411 | let type = expTypeShort(ty.ty) | ||
| 2412 | let tvar = tempDecl(expTypeArrayDims(ty.ty, dims), &varDecls /*BUFD*/) | ||
| 2413 | let &preExp += if isArrayType(typeof(exp1)) then | ||
| 2414 | 'add_array_scalar<<%type%>>(<%e1%>, <%e2%>, <%tvar%>);<%\n%>' else | ||
| 2415 | 'add_array_scalar<<%type%>>(<%e2%>, <%e1%>, <%tvar%>);<%\n%>' | ||
| 2416 | '<%tvar%>' | ||
| 2417 | case SUB_SCALAR_ARRAY(ty=T_ARRAY(dims=dims)) then | ||
| 2418 | let type = expTypeShort(ty.ty) | ||
| 2419 | let tvar = tempDecl(expTypeArrayDims(ty.ty, dims), &varDecls /*BUFD*/) | ||
| 2420 | let &preExp += 'subtract_array_scalar<<%type%>>(<%e2%>, <%e1%>, <%tvar%>);<%\n%>' | ||
| 2421 | '<%tvar%>' | ||
| 2422 | case MUL_SCALAR_PRODUCT(__) then | ||
| 2423 | let type = expTypeShort(ty) | ||
| 2424 | 'dot_array<<%type%>>(<%e1%>, <%e2%>)' | ||
| 2425 | case POW_ARRAY_SCALAR(ty=T_ARRAY(dims=dims)) then | ||
| 2426 | let tvar = tempDecl(expTypeArrayDims(ty.ty, dims), &varDecls /*BUFD*/) | ||
| 2427 | let &preExp += 'pow_array_scalar(<%e1%>, <%e2%>, <%tvar%>);<%\n%>' | ||
| 2428 | '<%tvar%>' | ||
| 2429 | case POW_SCALAR_ARRAY(__) then "daeExpBinary:ERR POW_SCALAR_ARRAY not supported" | ||
| 2430 | case POW_ARR(__) then "daeExpBinary:ERR POW_ARR not supported" | ||
| 2431 | case POW_ARR2(__) then "daeExpBinary:ERR POW_ARR2 not supported" | ||
| 2432 | case NOT(__) then "daeExpBinary:ERR NOT not supported" | ||
| 2433 | case LESS(__) then "daeExpBinary:ERR LESS not supported" | ||
| 2434 | case LESSEQ(__) then "daeExpBinary:ERR LESSEQ not supported" | ||
| 2435 | case GREATER(__) then "daeExpBinary:ERR GREATER not supported" | ||
| 2436 | case GREATEREQ(__) then "daeExpBinary:ERR GREATEREQ not supported" | ||
| 2437 | case EQUAL(__) then "daeExpBinary:ERR EQUAL not supported" | ||
| 2438 | case NEQUAL(__) then "daeExpBinary:ERR NEQUAL not supported" | ||
| 2439 | case USERDEFINED(__) then "daeExpBinary:ERR POW_ARR not supported" | ||
| 2440 | case _ then 'daeExpBinary:ERR <%ExpressionDumpTpl.dumpExp(exp1,"\"")%> <%ExpressionDump.binopSymbol(it)%> <%ExpressionDumpTpl.dumpExp(exp2,"\"")%>' | ||
| 2441 | end daeExpBinary; | ||
| 2442 | |||
| 2443 | |||
| 2444 | 1028 | template daeExpSconst(String string, Context context, Text &preExp, Text &varDecls, SimCode simCode, Text& extraFuncs, Text& extraFuncsDecl, | |
| 2445 | Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) | ||
| 2446 | "Generates code for a string constant." | ||
| 2447 | ::= | ||
| 2448 | '"<%Util.escapeModelicaStringToCString(string)%>"' | ||
| 2449 | end daeExpSconst; | ||
| 2450 | |||
| 2451 | 1314 | template daeExpUnary(Exp exp, Context context, Text &preExp, Text &varDecls, SimCode simCode, Text& extraFuncs, | |
| 2452 | Text& extraFuncsDecl, Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) | ||
| 2453 | "Generates code for a unary expression." | ||
| 2454 | ::= | ||
| 2455 | match exp | ||
| 2456 | case UNARY(__) then | ||
| 2457 | let e = daeExp(exp, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2458 | match operator | ||
| 2459 | case UMINUS(__) then '(-<%e%>)' | ||
| 2460 | case UMINUS_ARR(ty=T_ARRAY(ty=T_REAL(__))) then | ||
| 2461 | |||
| 2462 | let dimensions = (ty.dims |> dim as DIM_INTEGER(integer=i) => '<%i%>';separator=",") | ||
| 2463 | let listlength = listLength(ty.dims) | ||
| 2464 | let tmp_type_str = match dimensions | ||
| 2465 | case "" then 'DynArrayDim<%listlength%><double>' | ||
| 2466 | else 'StatArrayDim<%listlength%><double, <%dimensions%>>' | ||
| 2467 | |||
| 2468 | |||
| 2469 | //previous multi_array let tmp_type_str = 'multi_array<double,<%listLength(ty.dims)%>>' | ||
| 2470 | |||
| 2471 | let tvar = tempDecl(tmp_type_str, &varDecls /*BUFD*/) | ||
| 2472 | let &preExp += 'usub_array<double>(<%e%>,<%tvar%>);<%\n%>' | ||
| 2473 | '<%tvar%>' | ||
| 2474 | case UMINUS_ARR(ty=T_ARRAY(ty=T_INTEGER(__))) then | ||
| 2475 | let tmp_type_str = 'multi_array<int,<%listLength(ty.dims)%>>/*multi3*/' | ||
| 2476 | let tvar = tempDecl(tmp_type_str, &varDecls /*BUFD*/) | ||
| 2477 | let &preExp += 'usub_array<int>(<%e%>,<%tvar%>);<%\n%>' | ||
| 2478 | '<%tvar%>' | ||
| 2479 | case UMINUS_ARR(__) then 'unary minus for non-real arrays not implemented' | ||
| 2480 | else "daeExpUnary:ERR" | ||
| 2481 | end daeExpUnary; | ||
| 2482 | |||
| 2483 | 2730 | template daeExpRelation(Exp exp, Context context, Text &preExp,Text &varDecls,SimCode simCode ,Text& extraFuncs,Text& extraFuncsDecl,Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) | |
| 2484 | ::= | ||
| 2485 | match exp | ||
| 2486 | case rel as RELATION(__) then | ||
| 2487 | match rel.optionExpisASUB | ||
| 2488 | case NONE() then | ||
| 2489 | daeExpRelation2(rel.operator,rel.index,rel.exp1,rel.exp2, context, preExp,varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2490 | case SOME((exp,i,j)) then | ||
| 2491 | let e1 = daeExp(rel.exp1, context, &preExp /*BUFC*/, &varDecls /*BUFC*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2492 | let e2 = daeExp(rel.exp2, context, &preExp /*BUFC*/, &varDecls /*BUFC*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2493 | match context | ||
| 2494 | case ALGLOOP_CONTEXT(genInitialisation = false) then | ||
| 2495 | match rel.operator | ||
| 2496 | case LESS(__) then | ||
| 2497 | '_system->getCondition(<%rel.index%>) && (<%e1%> < <%e2%>)' | ||
| 2498 | |||
| 2499 | case LESSEQ(__) then | ||
| 2500 | '_system->getCondition(<%rel.index%>) && (<%e1%> <= <%e2%>)' | ||
| 2501 | |||
| 2502 | case GREATER(__) then | ||
| 2503 | '_system->getCondition(<%rel.index%>) && (<%e1%> > <%e2%>)' | ||
| 2504 | |||
| 2505 | case GREATEREQ(__) then | ||
| 2506 | '_system->getCondition(<%rel.index%>) && (<%e1%> >= <%e2%>)' | ||
| 2507 | end match | ||
| 2508 | else | ||
| 2509 | match rel.operator | ||
| 2510 | case LESS(__) then | ||
| 2511 | '(<%e1%> < <%e2%>)' | ||
| 2512 | |||
| 2513 | case LESSEQ(__) then | ||
| 2514 | '(<%e1%> <= <%e2%>)' | ||
| 2515 | |||
| 2516 | case GREATER(__) then | ||
| 2517 | '(<%e1%> > <%e2%>)' | ||
| 2518 | |||
| 2519 | case GREATEREQ(__) then | ||
| 2520 | '(<%e1%> >= <%e2%>)' | ||
| 2521 | end match | ||
| 2522 | end daeExpRelation; | ||
| 2523 | |||
| 2524 | |||
| 2525 | 113 | template daeExpRelation3(Context context,Integer index) ::= | |
| 2526 | match context | ||
| 2527 | case ALGLOOP_CONTEXT(genInitialisation = false) | ||
| 2528 | then <<_system->getCondition(<%index%>)>> | ||
| 2529 | else | ||
| 2530 | <<getCondition(<%index%>)>> | ||
| 2531 | end daeExpRelation3; | ||
| 2532 | |||
| 2533 | |||
| 2534 | 2730 | template daeExpRelation2(Operator op, Integer index,Exp exp1, Exp exp2, Context context, Text &preExp,Text &varDecls,SimCode simCode ,Text& extraFuncs,Text& extraFuncsDecl,Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) ::= | |
| 2535 | let e1 = daeExp(exp1, context, &preExp, &varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2536 | let e2 = daeExp(exp2, context, &preExp, &varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2537 | match index | ||
| 2538 | case -1 then | ||
| 2539 | match op | ||
| 2540 | case LESS(ty = T_BOOL(__)) then '(!<%e1%> && <%e2%>)' | ||
| 2541 | case LESS(ty = T_STRING(__)) then "# string comparison not supported\n" | ||
| 2542 | case LESS(ty = T_INTEGER(__)) | ||
| 2543 | case LESS(ty = T_REAL(__)) then '(<%e1%> < <%e2%>)' | ||
| 2544 | case LESS(ty = T_ENUMERATION(__)) then '(<%e1%> < <%e2%>)' | ||
| 2545 | |||
| 2546 | case GREATER(ty = T_BOOL(__)) then '(<%e1%> && !<%e2%>)' | ||
| 2547 | case GREATER(ty = T_STRING(__)) then "# string comparison not supported\n" | ||
| 2548 | case GREATER(ty = T_INTEGER(__)) | ||
| 2549 | case GREATER(ty = T_REAL(__)) then '(<%e1%> > <%e2%>)' | ||
| 2550 | case GREATER(ty = T_ENUMERATION(__)) then '(<%e1%> > <%e2%>)' | ||
| 2551 | |||
| 2552 | case LESSEQ(ty = T_BOOL(__)) then '(!<%e1%> || <%e2%>)' | ||
| 2553 | case LESSEQ(ty = T_STRING(__)) then "# string comparison not supported\n" | ||
| 2554 | case LESSEQ(ty = T_INTEGER(__)) | ||
| 2555 | case LESSEQ(ty = T_REAL(__)) then '(<%e1%> <= <%e2%>)' | ||
| 2556 | case LESSEQ(ty = T_ENUMERATION(__)) then '(<%e1%> <= <%e2%>)' | ||
| 2557 | |||
| 2558 | case GREATEREQ(ty = T_BOOL(__)) then '(<%e1%> || !<%e2%>)' | ||
| 2559 | case GREATEREQ(ty = T_STRING(__)) then "# string comparison not supported\n" | ||
| 2560 | case GREATEREQ(ty = T_INTEGER(__)) | ||
| 2561 | case GREATEREQ(ty = T_REAL(__)) then '(<%e1%> >= <%e2%>)' | ||
| 2562 | case GREATEREQ(ty = T_ENUMERATION(__)) then '(<%e1%> >= <%e2%>)' | ||
| 2563 | |||
| 2564 | case EQUAL(ty = T_BOOL(__)) then '((!<%e1%> && !<%e2%>) || (<%e1%> && <%e2%>))' | ||
| 2565 | case EQUAL(ty = T_STRING(__)) | ||
| 2566 | case EQUAL(ty = T_INTEGER(__)) | ||
| 2567 | case EQUAL(ty = T_REAL(__)) then '(<%e1%> == <%e2%>)' | ||
| 2568 | case EQUAL(ty = T_ENUMERATION(__)) then '(<%e1%> == <%e2%>)' | ||
| 2569 | |||
| 2570 | case NEQUAL(ty = T_BOOL(__)) then '((!<%e1%> && <%e2%>) || (<%e1%> && !<%e2%>))' | ||
| 2571 | case NEQUAL(ty = T_STRING(__)) | ||
| 2572 | case NEQUAL(ty = T_INTEGER(__)) | ||
| 2573 | case NEQUAL(ty = T_REAL(__)) then '(<%e1%> != <%e2%>)' | ||
| 2574 | case NEQUAL(ty = T_ENUMERATION(__)) then '(<%e1%> != <%e2%>)' | ||
| 2575 | |||
| 2576 | case _ then "daeExpRelation:ERR" | ||
| 2577 | end match | ||
| 2578 | case _ then | ||
| 2579 | match op | ||
| 2580 | case LESS(ty = T_BOOL(__)) then daeExpRelation3(context,index) | ||
| 2581 | case LESS(ty = T_STRING(__)) then "# string comparison not supported\n" | ||
| 2582 | case LESS(ty = T_INTEGER(__)) | ||
| 2583 | case LESS(ty = T_REAL(__)) then daeExpRelation3(context,index) | ||
| 2584 | |||
| 2585 | case GREATER(ty = T_BOOL(__)) then daeExpRelation3(context,index) | ||
| 2586 | case GREATER(ty = T_STRING(__)) then "# string comparison not supported\n" | ||
| 2587 | case GREATER(ty = T_INTEGER(__)) | ||
| 2588 | case GREATER(ty = T_REAL(__)) then daeExpRelation3(context,index) | ||
| 2589 | |||
| 2590 | case LESSEQ(ty = T_BOOL(__)) then daeExpRelation3(context,index) | ||
| 2591 | case LESSEQ(ty = T_STRING(__)) then "# string comparison not supported\n" | ||
| 2592 | case LESSEQ(ty = T_INTEGER(__)) | ||
| 2593 | case LESSEQ(ty = T_REAL(__)) then daeExpRelation3(context,index) | ||
| 2594 | |||
| 2595 | case GREATEREQ(ty = T_BOOL(__)) then daeExpRelation3(context,index) | ||
| 2596 | case GREATEREQ(ty = T_STRING(__)) then "# string comparison not supported\n" | ||
| 2597 | case GREATEREQ(ty = T_INTEGER(__)) | ||
| 2598 | case GREATEREQ(ty = T_REAL(__)) then daeExpRelation3(context,index) | ||
| 2599 | |||
| 2600 | case EQUAL(ty = T_BOOL(__)) then daeExpRelation3(context,index) | ||
| 2601 | case EQUAL(ty = T_STRING(__)) | ||
| 2602 | case EQUAL(ty = T_INTEGER(__)) | ||
| 2603 | case EQUAL(ty = T_REAL(__)) then daeExpRelation3(context,index) | ||
| 2604 | |||
| 2605 | case NEQUAL(ty = T_BOOL(__)) then daeExpRelation3(context,index) | ||
| 2606 | case NEQUAL(ty = T_STRING(__)) | ||
| 2607 | case NEQUAL(ty = T_INTEGER(__)) | ||
| 2608 | case NEQUAL(ty = T_REAL(__)) then daeExpRelation3(context,index) | ||
| 2609 | case _ then "daeExpRelationCondition:ERR" | ||
| 2610 | end match | ||
| 2611 | end daeExpRelation2; | ||
| 2612 | |||
| 2613 | |||
| 2614 | 1585 | template daeExpIf(Exp cond, Exp then_, Exp else_, Context context, Text &preExp, Text &varDecls,SimCode simCode ,Text& extraFuncs,Text& extraFuncsDecl,Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) ::= | |
| 2615 | let condExp = daeExp(cond, context, &preExp, &varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2616 | let &preExpThen = buffer "" | ||
| 2617 | let eThen = daeExp(then_, context, &preExpThen, &varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2618 | let &preExpElse = buffer "" | ||
| 2619 | let eElse = daeExp(else_, context, &preExpElse /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2620 | //let resVarType = expTypeFromExpArrayIf(else_,context,preExp,varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace) | ||
| 2621 | let resVar = expTypeFromExpArrayIf(else_,context,preExp,varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2622 | /*previous multi_array instead of .assign: | ||
| 2623 | 'assign_array(<%resVar%>,<%eThen%>);' | ||
| 2624 | */ | ||
| 2625 | let &preExp += | ||
| 2626 | << | ||
| 2627 | if <%encloseInParantheses(condExp)%> { | ||
| 2628 | <%preExpThen%> | ||
| 2629 | <% match typeof(then_) | ||
| 2630 | case T_ARRAY(dims=dims) then | ||
| 2631 | '<%resVar%>.assign(<%eThen%>);' | ||
| 2632 | else | ||
| 2633 | '<%resVar%> = <%eThen%>;' | ||
| 2634 | %> | ||
| 2635 | } else { | ||
| 2636 | <%preExpElse%> | ||
| 2637 | <%match typeof(else_) | ||
| 2638 | case T_ARRAY(dims=dims) then | ||
| 2639 | '<%resVar%>.assign(<%eElse%>);' | ||
| 2640 | else | ||
| 2641 | '<%resVar%> = <%eElse%>;' | ||
| 2642 | %> | ||
| 2643 | }<%\n%> | ||
| 2644 | >> | ||
| 2645 | resVar | ||
| 2646 | end daeExpIf; | ||
| 2647 | 1585 | template expTypeFromExpArrayIf(Exp exp, Context context, Text &preExp, Text &varDecls,SimCode simCode, | |
| 2648 | Text& extraFuncs,Text& extraFuncsDecl,Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) | ||
| 2649 | "Generates code for an array expression." | ||
| 2650 | ::= | ||
| 2651 | match exp | ||
| 2652 | case ARRAY(__) then | ||
| 2653 | let arrayTypeStr = expTypeShort(ty) | ||
| 2654 | let StatArrayDim = expTypeArrayIf(ty) | ||
| 2655 | let &tmpdecl = buffer "" /*BUFD*/ | ||
| 2656 | let arrayVar = tempDecl(arrayTypeStr, &tmpdecl /*BUFD*/) | ||
| 2657 | // let scalarPrefix = if scalar then "scalar_" else "" | ||
| 2658 | //let scalarRef = if scalar then "&" else "" | ||
| 2659 | let &tmpVar = buffer "" | ||
| 2660 | let params = (array |> e => | ||
| 2661 | '<%daeExp(e, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)%>' | ||
| 2662 | ;separator=", ") | ||
| 2663 | /* previous multi_array | ||
| 2664 | //tmp array | ||
| 2665 | <%StatArrayDim%><%arrayVar%>(boost::extents[<%listLength(array)%>]); | ||
| 2666 | <%arrayVar%>.reindex(1);' | ||
| 2667 | */ | ||
| 2668 | let &preExp += ' | ||
| 2669 | //tmp array | ||
| 2670 | <%StatArrayDim%><%arrayVar%>;/*testarray6*/<%\n%>' | ||
| 2671 | arrayVar | ||
| 2672 | else | ||
| 2673 | match typeof(exp) | ||
| 2674 | case ty as T_ARRAY(dims=dims) then | ||
| 2675 | // previous multi_array let resVarType = 'multi_array<<%expTypeShort(ty)%>,<%listLength(dims)%>>' | ||
| 2676 | let resVarType = 'DynArrayDim<%listLength(dims)%><<%expTypeShort(ty)%>>'//TODO evtl statarray | ||
| 2677 | let resVar = tempDecl(resVarType, &varDecls /*BUFD*/) | ||
| 2678 | |||
| 2679 | resVar | ||
| 2680 | else | ||
| 2681 | let resVarType = expTypeFlag(typeof(exp), 2) | ||
| 2682 | let resVar = tempDecl(resVarType, &varDecls /*BUFD*/) | ||
| 2683 | resVar | ||
| 2684 | end match | ||
| 2685 | end match | ||
| 2686 | end expTypeFromExpArrayIf; | ||
| 2687 | |||
| 2688 | |||
| 2689 | |||
| 2690 | ✗ | template expTypeFromExp(Exp it) ::= | |
| 2691 | match it | ||
| 2692 | case ICONST(__) then "int" | ||
| 2693 | case ENUM_LITERAL(__) then "int" | ||
| 2694 | case RCONST(__) then "double" | ||
| 2695 | case SCONST(__) then "string" | ||
| 2696 | case BCONST(__) then "bool" | ||
| 2697 | case BINARY(__) | ||
| 2698 | case UNARY(__) | ||
| 2699 | case LBINARY(__) | ||
| 2700 | case LUNARY(__) then expTypeFromOp(operator) | ||
| 2701 | case RELATION(__) then "bool" //TODO: a HACK, it was expTypeFromOp(operator) | ||
| 2702 | case IFEXP(__) then expTypeFromExp(expThen) | ||
| 2703 | case CALL(attr=CALL_ATTR(__)) then expTypeShort(attr.ty) | ||
| 2704 | case ARRAY(__) | ||
| 2705 | case MATRIX(__) | ||
| 2706 | case RANGE(__) | ||
| 2707 | case CAST(__) | ||
| 2708 | case CREF(__) | ||
| 2709 | case CODE(__) then expTypeShort(ty) | ||
| 2710 | case ASUB(__) then expTypeFromExp(exp) | ||
| 2711 | case REDUCTION(__) then expTypeFromExp(expr) | ||
| 2712 | |||
| 2713 | case TUPLE(__) then "expTypeFromExp:ERROR TUPLE unsupported" | ||
| 2714 | case TSUB(__) then "expTypeFromExp:ERROR TSUB unsupported" | ||
| 2715 | case SIZE(__) then "expTypeFromExp:ERROR SIZE unsupported" | ||
| 2716 | |||
| 2717 | /* Part of MetaModelica extension. KS */ | ||
| 2718 | case LIST(__) then "expTypeFromExp:ERROR LIST unsupported" | ||
| 2719 | case CONS(__) then "expTypeFromExp:ERROR CONS unsupported" | ||
| 2720 | case META_TUPLE(__) then "expTypeFromExp:ERROR META_TUPLE unsupported" | ||
| 2721 | case META_OPTION(__) then "expTypeFromExp:ERROR META_OPTION unsupported" | ||
| 2722 | case METARECORDCALL(__) then "expTypeFromExp:ERROR METARECORDCALL unsupported" | ||
| 2723 | case MATCHEXPRESSION(__) then "expTypeFromExp:ERROR MATCHEXPRESSION unsupported" | ||
| 2724 | case BOX(__) then "expTypeFromExp:ERROR BOX unsupported" | ||
| 2725 | case UNBOX(__) then "expTypeFromExp:ERROR UNBOX unsupported" | ||
| 2726 | case SHARED_LITERAL(__) then expTypeFromExp(exp) | ||
| 2727 | case PATTERN(__) then "expTypeFromExp:ERROR PATTERN unsupported" | ||
| 2728 | |||
| 2729 | case _ then "expTypeFromExp:ERROR" | ||
| 2730 | end expTypeFromExp; | ||
| 2731 | |||
| 2732 | |||
| 2733 | ✗ | template expTypeFromOp(Operator it) ::= | |
| 2734 | match it | ||
| 2735 | case ADD(__) | ||
| 2736 | case SUB(__) | ||
| 2737 | case MUL(__) | ||
| 2738 | case DIV(__) | ||
| 2739 | case POW(__) | ||
| 2740 | case UMINUS(__) | ||
| 2741 | case UMINUS_ARR(__) | ||
| 2742 | case ADD_ARR(__) | ||
| 2743 | case SUB_ARR(__) | ||
| 2744 | case MUL_ARR(__) | ||
| 2745 | case DIV_ARR(__) | ||
| 2746 | case MUL_ARRAY_SCALAR(__) | ||
| 2747 | case ADD_ARRAY_SCALAR(__) | ||
| 2748 | case SUB_SCALAR_ARRAY(__) | ||
| 2749 | case MUL_SCALAR_PRODUCT(__) | ||
| 2750 | case MUL_MATRIX_PRODUCT(__) | ||
| 2751 | case DIV_ARRAY_SCALAR(__) | ||
| 2752 | case DIV_SCALAR_ARRAY(__) | ||
| 2753 | case POW_ARRAY_SCALAR(__) | ||
| 2754 | case POW_SCALAR_ARRAY(__) | ||
| 2755 | case POW_ARR(__) | ||
| 2756 | case POW_ARR2(__) | ||
| 2757 | case LESS(__) | ||
| 2758 | case LESSEQ(__) | ||
| 2759 | case GREATER(__) | ||
| 2760 | case GREATEREQ(__) | ||
| 2761 | case EQUAL(__) | ||
| 2762 | case NEQUAL(__) then expTypeShort(ty) | ||
| 2763 | case AND(__) | ||
| 2764 | case OR(__) | ||
| 2765 | case NOT(__) then "bool" | ||
| 2766 | case _ then "expTypeFromOp:ERROR" | ||
| 2767 | end expTypeFromOp; | ||
| 2768 | |||
| 2769 | |||
| 2770 | |||
| 2771 | |||
| 2772 | 54 | template algStmtTupleAssign(DAE.Statement stmt, Context context, Text &varDecls, SimCode simCode, Text& extraFuncs, Text& extraFuncsDecl, Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) | |
| 2773 | "Generates a tuple assigment algorithm statement." | ||
| 2774 | ::= | ||
| 2775 | match stmt | ||
| 2776 | case STMT_TUPLE_ASSIGN(exp=CALL(__)) then | ||
| 2777 | let &preExp = buffer "" /*BUFD*/ | ||
| 2778 | let crefs = (expExpLst |> e => ExpressionDumpTpl.dumpExp(e,"\"") ;separator=", ") | ||
| 2779 | let marker = '(<%crefs%>) = <%ExpressionDumpTpl.dumpExp(exp,"\"")%>' | ||
| 2780 | let retStruct = daeExp(exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/, simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2781 | //previous multi_array let rhsStr = 'get<<%i1%>>(<%retStruct%>.data)' | ||
| 2782 | |||
| 2783 | let lhsCrefs = (expExpLst |> cr hasindex i1 fromindex 0 => | ||
| 2784 | let rhsStr = 'get<<%i1%>>(<%retStruct%>.data)' | ||
| 2785 | writeLhsCref(cr, rhsStr, context, &preExp, &varDecls, simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2786 | ;separator="\n";empty) | ||
| 2787 | << | ||
| 2788 | // algStmtTupleAssign: preExp <%marker%> | ||
| 2789 | <%preExp%> | ||
| 2790 | // algStmtTupleAssign: writeLhsCref | ||
| 2791 | <%lhsCrefs%> | ||
| 2792 | >> | ||
| 2793 | |||
| 2794 | else error(sourceInfo(), 'algStmtTupleAssign failed') | ||
| 2795 | end algStmtTupleAssign; | ||
| 2796 | |||
| 2797 | |||
| 2798 | |||
| 2799 | ✗ | template error(builtin.SourceInfo srcInfo, String errMessage) | |
| 2800 | "Example source template error reporting template to be used together with the sourceInfo() magic function. | ||
| 2801 | Usage: error(sourceInfo(), <<message>>) " | ||
| 2802 | ::= | ||
| 2803 | let() = Tpl.addSourceTemplateError(errMessage, srcInfo) | ||
| 2804 | << | ||
| 2805 | #error "<% Error.infoStr(srcInfo) %> <% errMessage %>"<%\n%> | ||
| 2806 | >> | ||
| 2807 | end error; | ||
| 2808 | |||
| 2809 | //for completeness; although the error() template above is preferable | ||
| 2810 | ✗ | template errorMsg(String errMessage) | |
| 2811 | "Example template error reporting template | ||
| 2812 | that is reporting only the error message without the usage of source infotmation." | ||
| 2813 | ::= | ||
| 2814 | let() = Tpl.addTemplateError(errMessage) | ||
| 2815 | << | ||
| 2816 | #error "<% errMessage %>"<%\n%> | ||
| 2817 | >> | ||
| 2818 | end errorMsg; | ||
| 2819 | |||
| 2820 | 1883 | template encloseInParantheses(String expStr) | |
| 2821 | "Encloses expression in paranthesis if not yet given" | ||
| 2822 | ::= | ||
| 2823 | if intEq(stringGet(expStr, 1), stringGet("(", 1)) then '<%expStr%>' else '(<%expStr%>)' | ||
| 2824 | end encloseInParantheses; | ||
| 2825 | |||
| 2826 | ✗ | template assignJacArray(String lhsStr, String rhsStr, DAE.Type ty) | |
| 2827 | "Assign array to JAC/DIFF/SEED vars that are flat vectors with row major odering" | ||
| 2828 | ::= | ||
| 2829 | match ty | ||
| 2830 | case DAE.T_ARRAY(ty=elty, dims=dims) then | ||
| 2831 | let dimstr = listDimsFlat(dims, elty) | ||
| 2832 | let arrayWrapper = 'tmp<%System.tmpTick()%>' | ||
| 2833 | << | ||
| 2834 | /*assign through wrapper array*/ | ||
| 2835 | StatArrayDim<%nDimsFlat(dims, elty, 0)%><<%expTypeShort(elty)%>, <%dimstr%>, true> <%arrayWrapper%>(&<%lhsStr%>); | ||
| 2836 | assignRowMajorData(<%rhsStr%>.getData(), <%arrayWrapper%>); | ||
| 2837 | >> | ||
| 2838 | end assignJacArray; | ||
| 2839 | |||
| 2840 | 101 | template writeLhsCref(Exp exp, String rhsStr, Context context, Text &preExp, Text &varDecls, SimCode simCode, | |
| 2841 | Text& extraFuncs,Text& extraFuncsDecl,Text extraFuncsNamespace, Text stateDerVectorName, Boolean useFlatArrayNotation) | ||
| 2842 | "Generates code for writing a returnStructur to var." | ||
| 2843 | ::= | ||
| 2844 | match exp | ||
| 2845 | case ecr as CREF(componentRef=WILD(__)) then | ||
| 2846 | '' | ||
| 2847 | case ecr as CREF(componentRef=cr, ty=ty as DAE.T_ARRAY()) then | ||
| 2848 | let lhsStr = daeExpCref(true, exp, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2849 | match cref2simvar(cr, simCode) | ||
| 2850 | case SIMVAR(varKind=varKind) then | ||
| 2851 | match varKind | ||
| 2852 | case JAC_VAR() | ||
| 2853 | case JAC_TMP_VAR() | ||
| 2854 | case SEED_VAR() then | ||
| 2855 | << | ||
| 2856 | <%assignJacArray(lhsStr, rhsStr, ty)%> | ||
| 2857 | >> | ||
| 2858 | else | ||
| 2859 | << | ||
| 2860 | <%lhsStr%>.assign(<%rhsStr%>); | ||
| 2861 | >> | ||
| 2862 | end match | ||
| 2863 | end match | ||
| 2864 | case UNARY(exp = e as CREF(ty=t as DAE.T_ARRAY(__))) then | ||
| 2865 | let lhsStr = daeExpCref(true, e, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2866 | match context | ||
| 2867 | case SIMULATION_CONTEXT(__) then | ||
| 2868 | << | ||
| 2869 | usub_<%expTypeShort(t)%>_array(&<%rhsStr%>);<%\n%> | ||
| 2870 | copy_<%expTypeShort(t)%>_array_data_mem(&<%rhsStr%>, &<%lhsStr%>); | ||
| 2871 | >> | ||
| 2872 | else | ||
| 2873 | '<%lhsStr%> = -<%rhsStr%>;' | ||
| 2874 | case CREF(componentRef = cr, ty=DAE.T_COMPLEX(varLst = varLst, complexClassType=RECORD(__))) then | ||
| 2875 | match context | ||
| 2876 | case FUNCTION_CONTEXT(__) then | ||
| 2877 | let lhsStr = daeExpCref(true, exp, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2878 | << | ||
| 2879 | <%lhsStr%> = <%rhsStr%>; | ||
| 2880 | >> | ||
| 2881 | else | ||
| 2882 | let lhsStr = contextCref(crefStripSubs(cr), context, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2883 | << | ||
| 2884 | <% varLst |> var as TYPES_VAR(__) hasindex i1 fromindex 0 => | ||
| 2885 | '_<%lhsStr%>P_<%var.name%>_ = <%rhsStr%>.<%var.name%>_;' | ||
| 2886 | ; separator="\n" | ||
| 2887 | %> | ||
| 2888 | >> | ||
| 2889 | end match | ||
| 2890 | case CREF(__) then | ||
| 2891 | let lhsStr = daeExpCref(true, exp, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2892 | << | ||
| 2893 | <%lhsStr%> = <%rhsStr%>; | ||
| 2894 | >> | ||
| 2895 | case UNARY(exp = e as CREF(__)) then | ||
| 2896 | let lhsStr = daeExpCref(true, e, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2897 | << | ||
| 2898 | <%lhsStr%> = -<%rhsStr%>; | ||
| 2899 | >> | ||
| 2900 | case ARRAY(array = {}) then | ||
| 2901 | << | ||
| 2902 | |||
| 2903 | >> | ||
| 2904 | case ARRAY(ty=T_ARRAY(ty=ty, dims=dims), array=expl) then | ||
| 2905 | //let typeShort = expTypeFromExpShort(exp) | ||
| 2906 | //let fcallsuf = match listLength(dims) case 1 then "" case i then '_<%i%>D' | ||
| 2907 | let body = (List.zip(expl,dimsToAllIndexes(dims)) |> (lhs,indxs) => | ||
| 2908 | let lhsStr = daeExpCref(true, lhs, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2909 | let indexStr = (indxs |> i => '<%i%>' ;separator=",") | ||
| 2910 | //'<%lhsStr%> = <%typeShort%>_get<%fcallsuf%>(&<%rhsStr%>, <%indexStr%>);' | ||
| 2911 | '<%lhsStr%> = (<%rhsStr%>)(<%indexStr%>);' | ||
| 2912 | ;separator="\n") | ||
| 2913 | << | ||
| 2914 | <%body%> | ||
| 2915 | >> | ||
| 2916 | case RECORD(exps=exps, comp=comp) then | ||
| 2917 | // assign fields of rhs record to lhs exps, one by one | ||
| 2918 | let body = (List.zip(exps, comp) |> (exp, compn) => | ||
| 2919 | let lhsStr = daeExpCref(true, exp, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2920 | let compnStr = crefStr(makeUntypedCrefIdent(compn)) | ||
| 2921 | '<%lhsStr%> = <%rhsStr%>.<%compnStr%>;' | ||
| 2922 | ;separator="\n") | ||
| 2923 | << | ||
| 2924 | <%body%> | ||
| 2925 | >> | ||
| 2926 | case ASUB(__) then | ||
| 2927 | error(sourceInfo(), 'writeLhsCref UNHANDLED ASUB (should never be part of a lhs expression): <%ExpressionDumpTpl.dumpExp(exp,"\"")%> = <%rhsStr%>') | ||
| 2928 | else | ||
| 2929 | error(sourceInfo(), 'writeLhsCref UNHANDLED: <%ExpressionDumpTpl.dumpExp(exp,"\"")%> = <%rhsStr%>') | ||
| 2930 | |||
| 2931 | end writeLhsCref; | ||
| 2932 | |||
| 2933 | 13 | template daeExpTsub(Exp inExp, Context context, Text &preExp, | |
| 2934 | Text &varDecls,SimCode simCode, Text& extraFuncs,Text& extraFuncsDecl,Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) | ||
| 2935 | "Generates code for an tsub expression." | ||
| 2936 | ::= | ||
| 2937 | match inExp | ||
| 2938 | case TSUB(ix=1) then | ||
| 2939 | let tuple_val = daeExp(exp, context, &preExp, &varDecls, simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2940 | 'get<0>(<%tuple_val%>.data)' | ||
| 2941 | //case TSUB(exp=CALL(attr=CALL_ATTR(ty=T_TUPLE(types=tys)))) then | ||
| 2942 | case TSUB(exp=CALL(path=p,attr=CALL_ATTR(ty=tys as T_TUPLE(__)))) then | ||
| 2943 | //let v = tempDecl(expTypeArrayIf(listGet(tys,ix)), &varDecls) | ||
| 2944 | //let additionalOutputs = List.restOrEmpty(tys) |> ty hasindex i1 fromindex 2 => if intEq(i1,ix) then ', &<%v%>' else ", NULL" | ||
| 2945 | let retType = '<%underscorePath(p)%>RetType' | ||
| 2946 | let retVar = tempDecl(retType, &varDecls) | ||
| 2947 | let res = daeExpCallTuple(exp, retVar, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2948 | let &preExp += '<%res%>;<%\n%>' | ||
| 2949 | 'get<<%intAdd(-1,ix)%>>(<%retVar%>.data)' | ||
| 2950 | |||
| 2951 | case TSUB(__) then | ||
| 2952 | error(sourceInfo(), '<%ExpressionDumpTpl.dumpExp(inExp,"\"")%>: TSUB only makes sense if the subscripted expression is a function call of tuple type') | ||
| 2953 | end daeExpTsub; | ||
| 2954 | |||
| 2955 | 8 | template daeExpCallTuple(Exp call , Text additionalOutputs/* arguments 2..N */, Context context, Text &preExp, Text &varDecls,SimCode simCode ,Text& extraFuncs,Text& extraFuncsDecl,Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) | |
| 2956 | ::= | ||
| 2957 | match call | ||
| 2958 | case exp as CALL(attr=attr as CALL_ATTR(__)) then | ||
| 2959 | |||
| 2960 | |||
| 2961 | let argStr = if boolOr(attr.builtin,isParallelFunctionContext(context)) | ||
| 2962 | then (expLst |> exp => '<%daeExp(exp, context, &preExp, &varDecls, simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)%>' ;separator=", ") | ||
| 2963 | else ((expLst |> exp => (daeExp(exp, context, preExp, &varDecls, simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation));separator=", ")) | ||
| 2964 | if attr.isFunctionPointerCall | ||
| 2965 | then | ||
| 2966 | let typeCast1 = generateTypeCast(attr.ty, expLst, true,preExp, varDecls,context, simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2967 | let typeCast2 = generateTypeCast(attr.ty, expLst, false, preExp, varDecls,context,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 2968 | let name = '_<%underscorePath(path)%>' | ||
| 2969 | let func = '(MMC_FETCH(MMC_OFFSET(MMC_UNTAGPTR(<%name%>), 1)))' | ||
| 2970 | let closure = '(MMC_FETCH(MMC_OFFSET(MMC_UNTAGPTR(<%name%>), 2)))' | ||
| 2971 | let argStrPointer = ('threadData, <%closure%>' + (expLst |> exp => (", " + daeExp(exp, context, &preExp, &varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)))) | ||
| 2972 | //'<%name%>(<%argStr%><%additionalOutputs%>)' | ||
| 2973 | '/*Closure?*/<%closure%> ? (<%typeCast1%> <%func%>) (<%argStrPointer%><%additionalOutputs%>) : (<%typeCast2%> <%func%>) (<%argStr%><%additionalOutputs%>)' | ||
| 2974 | else | ||
| 2975 | let name = underscorePath(path) | ||
| 2976 | '<%contextFunName(name, context)%>(<%argStr%>,<%additionalOutputs%>)' | ||
| 2977 | end daeExpCallTuple; | ||
| 2978 | |||
| 2979 | ✗ | template generateTypeCast(Type ty, list<DAE.Exp> es, Boolean isClosure, Text &preExp /*BUFP*/, | |
| 2980 | Text &varDecls, Context context,SimCode simCode, Text& extraFuncs,Text& extraFuncsDecl,Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) | ||
| 2981 | ::= | ||
| 2982 | let ret = (match ty | ||
| 2983 | case T_NORETCALL(__) then "void" | ||
| 2984 | else "modelica_metatype") | ||
| 2985 | let inputs = es |> e => ', <%expTypeFromExpArrayIf(e,context, &preExp ,&varDecls ,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)%>' | ||
| 2986 | let outputs = match ty | ||
| 2987 | case T_TUPLE(types=_::tys) then (tys |> t => ', <%expTypeArrayIf(t)%>') | ||
| 2988 | '(<%ret%>(*)(threadData_t*<%if isClosure then ", modelica_metatype"%><%inputs%><%outputs%>))' | ||
| 2989 | end generateTypeCast; | ||
| 2990 | |||
| 2991 | 2092 | template daeExpSharedLiteral(Exp exp, Context context, Text &preExp /*BUFP*/, Text &varDecls /*BUFP*/, Boolean useFlatArrayNotation) | |
| 2992 | "Generates code for a match expression." | ||
| 2993 | ::= | ||
| 2994 | match exp case exp as SHARED_LITERAL(__) then | ||
| 2995 | let lit = '_OMC_LIT<%exp.index%>' | ||
| 2996 | '<%contextFunName(lit, context)%>' | ||
| 2997 | end daeExpSharedLiteral; | ||
| 2998 | |||
| 2999 | 16 | template daeExpPartEvalFunction(Exp exp, Context context, Text &preExp, Text &varDecls, SimCode simCode, Text& extraFuncs, Text& extraFuncsDecl, Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) | |
| 3000 | "Generates code for a function reference and a closure." | ||
| 3001 | ::= | ||
| 3002 | match exp | ||
| 3003 | case PARTEVALFUNCTION(ty=T_FUNCTION_REFERENCE_VAR(functionType = t), origType=T_FUNCTION_REFERENCE_VAR(functionType=t_orig as T_FUNCTION(path=name))) then | ||
| 3004 | let funcName = '<%underscorePath(name)%>' | ||
| 3005 | let closureArgs = (expList |> e => ', <%daeExp(e, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)%>') | ||
| 3006 | functionClosure(funcName, closureArgs, t, t_orig, context, &extraFuncsDecl) | ||
| 3007 | case PARTEVALFUNCTION(__) then | ||
| 3008 | error(sourceInfo(), 'PARTEVALFUNCTION: <%ExpressionDumpTpl.dumpExp(exp,"\"")%>, ty=<%unparseType(ty)%>, origType=<%unparseType(origType)%>') | ||
| 3009 | end daeExpPartEvalFunction; | ||
| 3010 | |||
| 3011 | 16 | template functionClosure(String funcName, String closureArgs, Type t, Type t_orig, Context context, Text& extraFuncsDecl) | |
| 3012 | "Generates a closure for calling a function." | ||
| 3013 | ::= | ||
| 3014 | match t | ||
| 3015 | case T_FUNCTION(funcArg=funcArgs) then | ||
| 3016 | match t_orig | ||
| 3017 | case T_FUNCTION(funcArg=funcArgsOrig) then | ||
| 3018 | let closureName = '_Closure<%System.tmpTickIndex(2/*auxFunction*/)%>_<%funcName%>' | ||
| 3019 | let functionsObject = match context case FUNCTION_CONTEXT(__) then 'this' else '_functions' | ||
| 3020 | let closureArgsDecl = (setDifference(funcArgsOrig, funcArgs) |> a as FUNCARG(__) hasindex i1 fromindex 1 => ', <%expTypeUnboxed(a.ty)%> <%a.name%>') | ||
| 3021 | let callArgsDecl = (funcArgs |> a as FUNCARG(__) hasindex i1 fromindex 1 => '<%expTypeUnboxed(a.ty)%> <%a.name%>, ') | ||
| 3022 | let callArgsOrig = (funcArgsOrig |> a as FUNCARG(__) hasindex i1 fromindex 1 => '<%a.name%>, ') | ||
| 3023 | let &extraFuncsDecl += | ||
| 3024 | << | ||
| 3025 | |||
| 3026 | class <%closureName%> | ||
| 3027 | { | ||
| 3028 | Functions* _functions; | ||
| 3029 | <%setDifference(funcArgsOrig, funcArgs) |> a as FUNCARG(__) hasindex i1 fromindex 1 => '<%expTypeUnboxed(a.ty)%> <%a.name%>;<%\n%>'%> | ||
| 3030 | public: | ||
| 3031 | <%closureName%>(Functions* functions<%closureArgsDecl%>) | ||
| 3032 | : _functions(functions) | ||
| 3033 | <%setDifference(funcArgsOrig, funcArgs) |> a as FUNCARG(__) hasindex i1 fromindex 1 => ', <%a.name%>(<%a.name%>)<%\n%>'%> | ||
| 3034 | {} | ||
| 3035 | void operator()(<%callArgsDecl%><%funcName%>RetType &output) { | ||
| 3036 | _functions-><%funcName%>(<%callArgsOrig%>output); | ||
| 3037 | } | ||
| 3038 | };<%\n%> | ||
| 3039 | >> | ||
| 3040 | '<%closureName%>(<%functionsObject%><%closureArgs%>)' | ||
| 3041 | end functionClosure; | ||
| 3042 | |||
| 3043 | |||
| 3044 | 76 | template assertCommonVar(Text condVar, Text msgVar, Context context, Text &varDecls, builtin.SourceInfo info) | |
| 3045 | ::= | ||
| 3046 | match context | ||
| 3047 | // OpenCL doesn't have support for variadic args. So message should be just a single string. | ||
| 3048 | case FUNCTION_CONTEXT(is_parallel=true) then | ||
| 3049 | << | ||
| 3050 | if(!(<%condVar%>)) | ||
| 3051 | { | ||
| 3052 | |||
| 3053 | throw ModelicaSimulationError(MODEL_EQ_SYSTEM, "Common assertion failed"); | ||
| 3054 | |||
| 3055 | } | ||
| 3056 | >> | ||
| 3057 | case FUNCTION_CONTEXT(__) then | ||
| 3058 | << | ||
| 3059 | if(!(<%condVar%>)) | ||
| 3060 | { | ||
| 3061 | |||
| 3062 | throw ModelicaSimulationError(MODEL_EQ_SYSTEM, <%msgVar%>); | ||
| 3063 | } | ||
| 3064 | >> | ||
| 3065 | else | ||
| 3066 | << | ||
| 3067 | if(!(<%condVar%>)) | ||
| 3068 | { | ||
| 3069 | //string error_msg = "The following assertion has been violated %sat time %f", initial() ? "during initialization " : "", data->localData[0]->timeValue); | ||
| 3070 | throw ModelicaSimulationError(MODEL_EQ_SYSTEM,<%msgVar%>); | ||
| 3071 | } | ||
| 3072 | >> | ||
| 3073 | end assertCommonVar; | ||
| 3074 | |||
| 3075 | |||
| 3076 | 112 | template expTypeUnboxed(Type t) | |
| 3077 | "Returns the actual type in the box" | ||
| 3078 | ::= | ||
| 3079 | match t | ||
| 3080 | case T_METABOXED(__) then | ||
| 3081 | expTypeFlag(Types.unboxedType(t), 8) | ||
| 3082 | else | ||
| 3083 | expTypeFlag(t, 8) | ||
| 3084 | end expTypeUnboxed; | ||
| 3085 | |||
| 3086 | 51 | template daeExpBox(Exp exp, Context context, Text &preExp, Text &varDecls, SimCode simCode, Text& extraFuncs, Text& extraFuncsDecl, Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) | |
| 3087 | "Not needed; just returns exp" | ||
| 3088 | ::= | ||
| 3089 | match exp | ||
| 3090 | case BOX(__) then | ||
| 3091 | let elty = expTypeFromExpShort(exp) | ||
| 3092 | let ty = if isArrayType(typeof(exp)) then 'BaseArray<'+'<%elty%>'+'>' else '<%elty%>' | ||
| 3093 | let res = daeExp(exp, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 3094 | '/*box <%ty%>*/<%res%>' | ||
| 3095 | end daeExpBox; | ||
| 3096 | |||
| 3097 | 3 | template daeExpUnbox(Exp exp, Context context, Text &preExp, Text &varDecls, SimCode simCode, Text& extraFuncs, Text& extraFuncsDecl, Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) | |
| 3098 | "Not needed; just returns exp.exp" | ||
| 3099 | ::= | ||
| 3100 | match exp | ||
| 3101 | case exp as UNBOX(__) then | ||
| 3102 | let ty = expTypeShort(exp.ty) | ||
| 3103 | let res = daeExp(exp.exp, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) | ||
| 3104 | '/*unbox <%ty%>*/<%res%>' | ||
| 3105 | end daeExpUnbox; | ||
| 3106 | |||
| 3107 | /******************************************************************************************************************************************************* | ||
| 3108 | * end of exp to string template functions | ||
| 3109 | ********************************************************************************************************************************************************/ | ||
| 3110 | |||
| 3111 | 219 | template zeroCrossLength(SimCode simCode) | |
| 3112 | ::= | ||
| 3113 | match simCode | ||
| 3114 | case SIMCODE(modelInfo = MODELINFO(varInfo = vi as VARINFO(__))) then | ||
| 3115 | let size = listLength(zeroCrossings) | ||
| 3116 | << | ||
| 3117 | <%intSub(listLength(zeroCrossings), vi.numTimeEvents)%> | ||
| 3118 | >> | ||
| 3119 | end zeroCrossLength; | ||
| 3120 | |||
| 3121 | 153 | template timeEventLength(SimCode simCode) | |
| 3122 | ::= | ||
| 3123 | match simCode | ||
| 3124 | case SIMCODE(modelInfo = MODELINFO(varInfo = vi as VARINFO(__))) then | ||
| 3125 | << | ||
| 3126 | <%vi.numTimeEvents%> | ||
| 3127 | >> | ||
| 3128 | end timeEventLength; | ||
| 3129 | |||
| 3130 | annotation(__OpenModelica_Interface="codegen_cpp_common"); | ||
| 3131 | end CodegenCppCommon; | ||
| 3132 |