OMCompiler/Compiler/SimCode/SimCodeFunction.mo
| 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 | encapsulated package SimCodeFunction | ||
| 37 | "The entry points to this module is the translateFunctions function." | ||
| 38 | |||
| 39 | // public imports | ||
| 40 | public import Absyn; | ||
| 41 | public import AbsynUtil; | ||
| 42 | public import DAE; | ||
| 43 | public import HashTableCrefSimVar; | ||
| 44 | |||
| 45 | // private imports | ||
| 46 | protected import ComponentReferenceBasics; | ||
| 47 | protected import ExpressionBasics; | ||
| 48 | protected import Error; | ||
| 49 | protected import List; | ||
| 50 | protected import SCode; | ||
| 51 | protected import TypesDump; | ||
| 52 | |||
| 53 | public uniontype FunctionCode | ||
| 54 | "Root data structure containing information required for templates to | ||
| 55 | generate C functions for Modelica/MetaModelica functions." | ||
| 56 | record FUNCTIONCODE | ||
| 57 | String name; | ||
| 58 | Option<Function> mainFunction "This function is special; the 'in'-function should be generated for it"; | ||
| 59 | list<Function> functions; | ||
| 60 | list<DAE.Exp> literals "shared literals"; | ||
| 61 | list<String> externalFunctionIncludes; | ||
| 62 | MakefileParams makefileParams; | ||
| 63 | list<RecordDeclaration> extraRecordDecls; | ||
| 64 | end FUNCTIONCODE; | ||
| 65 | end FunctionCode; | ||
| 66 | |||
| 67 | // TODO: I believe some of these fields can be removed. Check to see what is | ||
| 68 | // used in templates. | ||
| 69 | public uniontype Function | ||
| 70 | "Represents a Modelica, MetaModelica or external function." | ||
| 71 | record FUNCTION | ||
| 72 | Absyn.Path name; | ||
| 73 | list<Variable> outVars; | ||
| 74 | list<Variable> functionArguments; | ||
| 75 | list<Variable> variableDeclarations; | ||
| 76 | list<DAE.Statement> body; | ||
| 77 | SCode.Visibility visibility; | ||
| 78 | SourceInfo info; | ||
| 79 | end FUNCTION; | ||
| 80 | |||
| 81 | record PARALLEL_FUNCTION | ||
| 82 | Absyn.Path name; | ||
| 83 | list<Variable> outVars; | ||
| 84 | list<Variable> functionArguments; | ||
| 85 | list<Variable> variableDeclarations; | ||
| 86 | list<DAE.Statement> body; | ||
| 87 | SourceInfo info; | ||
| 88 | end PARALLEL_FUNCTION; | ||
| 89 | |||
| 90 | record KERNEL_FUNCTION | ||
| 91 | Absyn.Path name; | ||
| 92 | list<Variable> outVars; | ||
| 93 | list<Variable> functionArguments; | ||
| 94 | list<Variable> variableDeclarations; | ||
| 95 | list<DAE.Statement> body; | ||
| 96 | SourceInfo info; | ||
| 97 | end KERNEL_FUNCTION; | ||
| 98 | |||
| 99 | record EXTERNAL_FUNCTION | ||
| 100 | Absyn.Path name; | ||
| 101 | String extName; | ||
| 102 | list<Variable> funArgs; | ||
| 103 | list<SimExtArg> extArgs; | ||
| 104 | SimExtArg extReturn; | ||
| 105 | list<Variable> inVars; | ||
| 106 | list<Variable> outVars; | ||
| 107 | list<Variable> biVars; | ||
| 108 | list<String> includes "this one is needed so that we know if we should generate the external function prototype or not"; | ||
| 109 | list<String> libs "need this one for C#"; | ||
| 110 | String language "C or Fortran"; | ||
| 111 | SCode.Visibility visibility; | ||
| 112 | SourceInfo info; | ||
| 113 | Boolean dynamicLoad; | ||
| 114 | end EXTERNAL_FUNCTION; | ||
| 115 | |||
| 116 | record RECORD_CONSTRUCTOR | ||
| 117 | Absyn.Path name; | ||
| 118 | list<Variable> funArgs; | ||
| 119 | list<Variable> locals; | ||
| 120 | SCode.Visibility visibility; | ||
| 121 | SourceInfo info; | ||
| 122 | end RECORD_CONSTRUCTOR; | ||
| 123 | |||
| 124 | function toString | ||
| 125 | "Print for debugging purpose" | ||
| 126 | input Function func; | ||
| 127 | output String str = ""; | ||
| 128 | algorithm | ||
| 129 | str := match func | ||
| 130 | local | ||
| 131 | String tmp = ""; | ||
| 132 | list<String> ls; | ||
| 133 | case FUNCTION() algorithm | ||
| 134 | ✗ | tmp := tmp + "name: " + AbsynUtil.pathString(func.name); | |
| 135 | ✗ | then "FUNCTION(" + tmp + ")"; | |
| 136 | case PARALLEL_FUNCTION() algorithm | ||
| 137 | ✗ | tmp := tmp + "name: " + AbsynUtil.pathString(func.name); | |
| 138 | ✗ | then "PARALLEL_FUNCTION(" + tmp + ")"; | |
| 139 | case KERNEL_FUNCTION() algorithm | ||
| 140 | ✗ | tmp := tmp + "name: " + AbsynUtil.pathString(func.name); | |
| 141 | ✗ | then "KERNEL_FUNCTION(" + tmp + ")"; | |
| 142 | case EXTERNAL_FUNCTION() algorithm | ||
| 143 | tmp := "\n"; | ||
| 144 | ✗ | tmp := tmp + " name: " + AbsynUtil.pathString(func.name) + ",\n"; | |
| 145 | ✗ | tmp := tmp + " extName: " + func.extName + ",\n"; | |
| 146 | ✗ | ls := List.map(func.funArgs, Variable.toString); | |
| 147 | ✗ | tmp := tmp + " funArgs: {" + stringDelimitList(ls, ", ") + "},\n"; | |
| 148 | ✗ | ls := List.map(func.extArgs, SimExtArg.toString); | |
| 149 | ✗ | tmp := tmp + " extArgs: {" + stringDelimitList(ls, ", ") + "},\n"; | |
| 150 | ✗ | tmp := tmp + " extReturn: " + SimExtArg.toString(func.extReturn) + ",\n"; | |
| 151 | ✗ | ls := List.map(func.inVars, Variable.toString); | |
| 152 | ✗ | tmp := tmp + " inVars: {" + stringDelimitList(ls, ", ") + "},\n"; | |
| 153 | ✗ | ls := List.map(func.outVars, Variable.toString); | |
| 154 | ✗ | tmp := tmp + " outVars: {" + stringDelimitList(ls, ", ") + "},\n"; | |
| 155 | ✗ | ls := List.map(func.biVars, Variable.toString); | |
| 156 | ✗ | tmp := tmp + " biVars: {" + stringDelimitList(ls, ", ") + "},\n"; | |
| 157 | ✗ | tmp := tmp + " includes: {" + stringDelimitList(func.includes, ", ") + "},\n"; | |
| 158 | ✗ | tmp := tmp + " libs: {" + stringDelimitList(func.libs, ", ") + "},\n"; | |
| 159 | ✗ | tmp := tmp + " language: " + func.language + "\n"; | |
| 160 | ✗ | then "EXTERNAL_FUNCTION(" + tmp + ")"; | |
| 161 | case RECORD_CONSTRUCTOR() algorithm | ||
| 162 | ✗ | tmp := tmp + "name: " + AbsynUtil.pathString(func.name); | |
| 163 | ✗ | then "RECORD_CONSTRUCTOR(" + tmp + ")"; | |
| 164 | else algorithm | ||
| 165 | ✗ | Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed for an unknown reason."}); | |
| 166 | ✗ | then fail(); | |
| 167 | end match; | ||
| 168 | end toString; | ||
| 169 | end Function; | ||
| 170 | |||
| 171 | public uniontype RecordDeclaration | ||
| 172 | |||
| 173 | record RECORD_DECL_FULL | ||
| 174 | String name "struct (record) name ? encoded"; | ||
| 175 | Option<String> aliasName "alias of struct (record) name ? encoded. Code generators can generate an aliasing typedef using this, and avoid problems when casting a record from one type to another (*(othertype*)(&var)), which only works if you have a lhs value."; | ||
| 176 | Absyn.Path defPath "definition path"; | ||
| 177 | list<Variable> variables "only name and type"; | ||
| 178 | Boolean usedExternally "If the record is passed to an external function at any point, we need to generate conversion functions for it (for instance to convert 'modelica_integer' to 'int')"; | ||
| 179 | end RECORD_DECL_FULL; | ||
| 180 | |||
| 181 | record RECORD_DECL_ADD_CONSTRCTOR | ||
| 182 | String ctor_name "A unique name for the new constor. e.g. R_1_3() if it needs the 1st an 3rd members as inputs"; | ||
| 183 | String name "The record's name"; | ||
| 184 | list<Variable> variables "The members with the ones that need outisde binding marked. e.g 1st and 3rd elements will have bind_from_outside=true "; | ||
| 185 | Absyn.Path defPath "definition path"; | ||
| 186 | Boolean usedExternally; | ||
| 187 | end RECORD_DECL_ADD_CONSTRCTOR; | ||
| 188 | |||
| 189 | record RECORD_DECL_DEF | ||
| 190 | Absyn.Path path "definition path .. encoded?"; | ||
| 191 | list<String> fieldNames; | ||
| 192 | end RECORD_DECL_DEF; | ||
| 193 | |||
| 194 | end RecordDeclaration; | ||
| 195 | |||
| 196 | public uniontype MakefileParams | ||
| 197 | "Platform specific parameters used when generating makefiles." | ||
| 198 | record MAKEFILE_PARAMS | ||
| 199 | String ccompiler; | ||
| 200 | String cxxcompiler; | ||
| 201 | String linker; | ||
| 202 | String exeext; | ||
| 203 | String dllext; | ||
| 204 | String omhome; | ||
| 205 | String cflags; | ||
| 206 | String ldflags; | ||
| 207 | String runtimelibs "Libraries that are required by the runtime library"; | ||
| 208 | list<String> includes; | ||
| 209 | list<String> libs; | ||
| 210 | list<String> libPaths; | ||
| 211 | String platform; | ||
| 212 | String compileDir; | ||
| 213 | end MAKEFILE_PARAMS; | ||
| 214 | end MakefileParams; | ||
| 215 | |||
| 216 | public uniontype SimExtArg | ||
| 217 | "Information about an argument to an external function." | ||
| 218 | record SIMEXTARG | ||
| 219 | DAE.ComponentRef cref; | ||
| 220 | Boolean isInput; | ||
| 221 | Integer outputIndex "> 0 if output"; | ||
| 222 | Boolean isArray; | ||
| 223 | Boolean hasBinding "avoid double allocation"; | ||
| 224 | DAE.Type type_; | ||
| 225 | end SIMEXTARG; | ||
| 226 | |||
| 227 | record SIMEXTARGEXP | ||
| 228 | DAE.Exp exp; | ||
| 229 | DAE.Type type_; | ||
| 230 | end SIMEXTARGEXP; | ||
| 231 | |||
| 232 | record SIMEXTARGSIZE | ||
| 233 | DAE.ComponentRef cref; | ||
| 234 | Boolean isInput; | ||
| 235 | Integer outputIndex "> 0 if output"; | ||
| 236 | DAE.Type type_; | ||
| 237 | DAE.Exp exp; | ||
| 238 | end SIMEXTARGSIZE; | ||
| 239 | |||
| 240 | record SIMNOEXTARG end SIMNOEXTARG; | ||
| 241 | |||
| 242 | function toString | ||
| 243 | input SimExtArg simExtArg; | ||
| 244 | output String str = ""; | ||
| 245 | algorithm | ||
| 246 | str := match simExtArg | ||
| 247 | local | ||
| 248 | String tmp = ""; | ||
| 249 | case SIMEXTARG() algorithm | ||
| 250 | ✗ | tmp := tmp + "cref: " + ComponentReferenceBasics.printComponentRefStr(simExtArg.cref); | |
| 251 | ✗ | tmp := if simExtArg.isInput then tmp + ", isInput: true" else tmp + ", isInput: false"; | |
| 252 | ✗ | tmp := tmp + ", outputIndex: " + intString(simExtArg.outputIndex); | |
| 253 | ✗ | tmp := if simExtArg.isArray then tmp + ", isArray: true" else tmp + ", isArray: false"; | |
| 254 | ✗ | tmp := if simExtArg.hasBinding then tmp + ", hasBinding: true" else tmp + ", hasBinding: false"; | |
| 255 | ✗ | tmp := tmp + ", type: " + TypesDump.unparseType(simExtArg.type_); | |
| 256 | ✗ | then "SIMEXTARG(" + tmp + ")"; | |
| 257 | |||
| 258 | case SIMEXTARGEXP() algorithm | ||
| 259 | ✗ | tmp := tmp + "exp: " + ExpressionBasics.printExpStr(simExtArg.exp); | |
| 260 | ✗ | tmp := tmp + ", type: " + TypesDump.unparseType(simExtArg.type_); | |
| 261 | ✗ | then "SIMEXTARGEXP(" + tmp + ")"; | |
| 262 | |||
| 263 | case SIMEXTARGSIZE() algorithm | ||
| 264 | ✗ | tmp := tmp + "cref: " + ComponentReferenceBasics.printComponentRefStr(simExtArg.cref); | |
| 265 | ✗ | tmp := if simExtArg.isInput then tmp + ", isInput: true" else tmp + ", isInput: false"; | |
| 266 | ✗ | tmp := tmp + ", outputIndex: " + intString(simExtArg.outputIndex); | |
| 267 | ✗ | tmp := tmp + ", type: " + TypesDump.unparseType(simExtArg.type_); | |
| 268 | ✗ | tmp := tmp + ", exp: " + ExpressionBasics.printExpStr(simExtArg.exp); | |
| 269 | ✗ | then "SIMEXTARGSIZE(" + tmp + ")"; | |
| 270 | |||
| 271 | case SIMNOEXTARG() | ||
| 272 | then "SIMNOEXTARG()"; | ||
| 273 | |||
| 274 | else algorithm | ||
| 275 | ✗ | Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed for an unknown reason."}); | |
| 276 | ✗ | then fail(); | |
| 277 | end match; | ||
| 278 | end toString; | ||
| 279 | end SimExtArg; | ||
| 280 | |||
| 281 | public uniontype Variable | ||
| 282 | "A variable represents a name, a type and a possible default value" | ||
| 283 | record VARIABLE | ||
| 284 | DAE.ComponentRef name; | ||
| 285 | DAE.Type ty; | ||
| 286 | Option<DAE.Exp> value "default value"; | ||
| 287 | list<DAE.Dimension> instDims; | ||
| 288 | DAE.VarParallelism parallelism; | ||
| 289 | DAE.VarKind kind; | ||
| 290 | Boolean bind_from_outside; | ||
| 291 | end VARIABLE; | ||
| 292 | |||
| 293 | record FUNCTION_PTR | ||
| 294 | String name; | ||
| 295 | list<DAE.Type> tys; | ||
| 296 | list<Variable> args; | ||
| 297 | Option<DAE.Exp> defaultValue "default value"; | ||
| 298 | end FUNCTION_PTR; | ||
| 299 | |||
| 300 | function toString | ||
| 301 | input Variable variable; | ||
| 302 | output String str = ""; | ||
| 303 | algorithm | ||
| 304 | str := match variable | ||
| 305 | local | ||
| 306 | String tmp = ""; | ||
| 307 | case VARIABLE() algorithm | ||
| 308 | ✗ | tmp := tmp + "name: " + ComponentReferenceBasics.printComponentRefStr(variable.name); | |
| 309 | ✗ | tmp := tmp + ", type: " + TypesDump.unparseType(variable.ty); | |
| 310 | ✗ | then "VARIABLE(" + tmp + ")"; | |
| 311 | case FUNCTION_PTR() algorithm | ||
| 312 | ✗ | tmp := tmp + "name: " + variable.name; | |
| 313 | ✗ | then "FUNCTION_PTR(" + tmp + ")"; | |
| 314 | else algorithm | ||
| 315 | ✗ | Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed for an unknown reason."}); | |
| 316 | ✗ | then fail(); | |
| 317 | end match; | ||
| 318 | end toString; | ||
| 319 | end Variable; | ||
| 320 | |||
| 321 | public uniontype Context | ||
| 322 | "Constants of this type defined below are used by templates to be able to | ||
| 323 | generate different code depending on the context it is generated in." | ||
| 324 | record SIMULATION_CONTEXT | ||
| 325 | Boolean genDiscrete; | ||
| 326 | end SIMULATION_CONTEXT; | ||
| 327 | |||
| 328 | record FUNCTION_CONTEXT | ||
| 329 | String cref_prefix; | ||
| 330 | Boolean is_parallel; | ||
| 331 | end FUNCTION_CONTEXT; | ||
| 332 | |||
| 333 | record ALGLOOP_CONTEXT | ||
| 334 | Boolean genInitialisation; | ||
| 335 | Boolean genJacobian; | ||
| 336 | end ALGLOOP_CONTEXT; | ||
| 337 | |||
| 338 | record JACOBIAN_CONTEXT | ||
| 339 | String name; | ||
| 340 | Option<HashTableCrefSimVar.HashTable> jacHT; | ||
| 341 | end JACOBIAN_CONTEXT; | ||
| 342 | |||
| 343 | record OTHER_CONTEXT | ||
| 344 | end OTHER_CONTEXT; | ||
| 345 | |||
| 346 | record ZEROCROSSINGS_CONTEXT | ||
| 347 | end ZEROCROSSINGS_CONTEXT; | ||
| 348 | |||
| 349 | record OPTIMIZATION_CONTEXT | ||
| 350 | end OPTIMIZATION_CONTEXT; | ||
| 351 | |||
| 352 | record FMI_CONTEXT | ||
| 353 | end FMI_CONTEXT; | ||
| 354 | |||
| 355 | record DAE_MODE_CONTEXT | ||
| 356 | end DAE_MODE_CONTEXT; | ||
| 357 | end Context; | ||
| 358 | |||
| 359 | public constant Context contextSimulationNonDiscrete = SIMULATION_CONTEXT(false); | ||
| 360 | public constant Context contextSimulationDiscrete = SIMULATION_CONTEXT(true); | ||
| 361 | public constant Context contextFunction = FUNCTION_CONTEXT("", false); | ||
| 362 | public constant Context contextJacobian = JACOBIAN_CONTEXT("", NONE()); | ||
| 363 | public constant Context contextAlgloopJacobian = ALGLOOP_CONTEXT(false,true); | ||
| 364 | public constant Context contextAlgloopInitialisation = ALGLOOP_CONTEXT(true,false); | ||
| 365 | public constant Context contextAlgloop = ALGLOOP_CONTEXT(false,false); | ||
| 366 | public constant Context contextOther = OTHER_CONTEXT(); | ||
| 367 | public constant Context contextParallelFunction = FUNCTION_CONTEXT("", true); | ||
| 368 | public constant Context contextZeroCross = ZEROCROSSINGS_CONTEXT(); | ||
| 369 | public constant Context contextOptimization = OPTIMIZATION_CONTEXT(); | ||
| 370 | public constant Context contextFMI = FMI_CONTEXT(); | ||
| 371 | public constant Context contextDAEmode = DAE_MODE_CONTEXT(); | ||
| 372 | |||
| 373 | constant list<DAE.Exp> listExpLength1 = {DAE.ICONST(0)} "For CodegenC.tpl"; | ||
| 374 | constant list<Variable> boxedRecordOutVars = VARIABLE(DAE.CREF_IDENT("",DAE.T_COMPLEX_DEFAULT_RECORD,{}),DAE.T_COMPLEX_DEFAULT_RECORD,NONE(),{},DAE.NON_PARALLEL(),DAE.VARIABLE(), false)::{} "For CodegenC.tpl"; | ||
| 375 | |||
| 376 | annotation(__OpenModelica_Interface="simcode_types"); | ||
| 377 | end SimCodeFunction; | ||
| 378 |