OMCompiler/Compiler/Template/CodegenWasmJit.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 CodegenWasmJit | ||
| 37 | " Code generator target `wasm-jit`, the simulation half of the WebAssembly | ||
| 38 | pipeline (counterpart of CodegenC for the C target). | ||
| 39 | |||
| 40 | Lowers the SimCode equation systems to a single WebAssembly model module (the | ||
| 41 | numerical right-hand sides) that is JIT-compiled and run in-process with | ||
| 42 | wasmtime, integrating with a forward-Euler solver and writing the MATLAB v4 | ||
| 43 | result file. Unlike the C target, no metadata is serialized to XML/JSON: the | ||
| 44 | host holds the SimCode-derived data in memory. | ||
| 45 | |||
| 46 | The implementation is hand-written in Rust (openmodelica_codegen_wasm_jit); | ||
| 47 | the declarations below exist only so the calls type-check in the MetaModelica | ||
| 48 | sources. " | ||
| 49 | |||
| 50 | import SimCode; | ||
| 51 | import Error; | ||
| 52 | |||
| 53 | function translateModel | ||
| 54 | " Lower the model in `simCode` to a WebAssembly module written to | ||
| 55 | <simCode.fileNamePrefix>.wasm and stash the prepared model in-process for the | ||
| 56 | later runSimulation. Counterpart of CodegenC.translateModel for the C target. | ||
| 57 | Implemented in Rust. " | ||
| 58 | input SimCode.SimCode simCode; | ||
| 59 | algorithm | ||
| 60 | ✗ | Error.addInternalError("CodegenWasmJit.translateModel: the wasm-jit target is only implemented in the Rust omc build", sourceInfo()); | |
| 61 | ✗ | fail(); | |
| 62 | end translateModel; | ||
| 63 | |||
| 64 | function runSimulation | ||
| 65 | " Run the prepared model (built by translateModel) in-process with a | ||
| 66 | forward-Euler solver and write the result file. Returns 0 on success, 1 on | ||
| 67 | failure (matching the exit code of the C target's executable). Implemented in | ||
| 68 | Rust. " | ||
| 69 | input String fileNamePrefix; | ||
| 70 | input String resultFile; | ||
| 71 | input String simflags; | ||
| 72 | output Integer status; | ||
| 73 | algorithm | ||
| 74 | status := 0; | ||
| 75 | end runSimulation; | ||
| 76 | |||
| 77 | function finishCompile | ||
| 78 | " Force the model's wasm modules to finish JIT-compiling and resolve its | ||
| 79 | external \"C\" implementations, building the Include C sources. Called from | ||
| 80 | buildModel's compile phase (the wasm-jit counterpart of compiling the C | ||
| 81 | executable) so the compile cost is attributed to timeCompile rather than | ||
| 82 | timeSimulation. Implemented in Rust; fails if an external \"C\" | ||
| 83 | implementation is unavailable. " | ||
| 84 | input String fileNamePrefix; | ||
| 85 | algorithm | ||
| 86 | ✗ | Error.addInternalError("CodegenWasmJit.finishCompile: the wasm-jit target is only implemented in the Rust omc build", sourceInfo()); | |
| 87 | ✗ | fail(); | |
| 88 | end finishCompile; | ||
| 89 | |||
| 90 | function emitStandalone | ||
| 91 | " The `wasm` simCodeTarget (vs in-process `wasm-jit`): lower the model and | ||
| 92 | merge it with the wasip1 runtime into a self-contained WASI command module | ||
| 93 | written to <simCode.fileNamePrefix>.wasm, runnable with | ||
| 94 | `wasmtime run <prefix>.wasm --dir .::.`. Implemented in Rust. " | ||
| 95 | input SimCode.SimCode simCode; | ||
| 96 | algorithm | ||
| 97 | ✗ | Error.addInternalError("CodegenWasmJit.emitStandalone: the wasm target is only implemented in the Rust omc build", sourceInfo()); | |
| 98 | ✗ | fail(); | |
| 99 | end emitStandalone; | ||
| 100 | |||
| 101 | function translateFmu | ||
| 102 | " Lower the model in `simCode` to the WebAssembly kernel a wasm FMU is built | ||
| 103 | around and keep it in-process, without writing an FMU: the wasm counterpart of | ||
| 104 | the C target generating the FMU sources without building them | ||
| 105 | (translateModelFMU). The buildModelFMU that follows links an adapter onto this | ||
| 106 | kernel rather than translating the model a second time, and unless the model | ||
| 107 | has external \"C\" the kernel is also registered as the prepared simulation | ||
| 108 | model, so `simulate` runs the very module the FMU will carry. Implemented in | ||
| 109 | Rust. " | ||
| 110 | input SimCode.SimCode simCode; | ||
| 111 | input String fmuType "me, cs or me_cs: a pure Co-Simulation kernel embeds a different driver"; | ||
| 112 | input String simulationFlagsJson "--fmiFlags as CodegenFMU renders it, empty when there are none"; | ||
| 113 | algorithm | ||
| 114 | ✗ | Error.addInternalError("CodegenWasmJit.translateFmu: the wasm FMU target is only implemented in the Rust omc build", sourceInfo()); | |
| 115 | ✗ | fail(); | |
| 116 | end translateFmu; | ||
| 117 | |||
| 118 | function emitMeFmu | ||
| 119 | " wasm Model-Exchange export: lower the model, link it with the | ||
| 120 | model-agnostic ME adapter into an fmi-ls-wasm component | ||
| 121 | (wit_component::Linker, pure Rust — no external wasm-merge), and write the | ||
| 122 | self-contained `.fmu` ZIP (`modelDescription` + binaries/wasm32-wasip2/<id>.wasm) | ||
| 123 | to `fmuPath`. Host-free, so it also works in the browser omc. | ||
| 124 | `modelDescription` is CodegenFMU2's or CodegenFMU3's XML; its value references | ||
| 125 | are resolved at run time through the vr table the emitter puts in the model's | ||
| 126 | metadata blob. Implemented in Rust. " | ||
| 127 | input SimCode.SimCode simCode; | ||
| 128 | input String fmuPath; | ||
| 129 | input String guid; | ||
| 130 | input String modelDescription; | ||
| 131 | input String lsDaeManifest "fmi-ls-dae's manifest for a --daeMode model, empty for none"; | ||
| 132 | input String documentationDir "directory shipped as documentation/ (index.html and the images it references); empty for none"; | ||
| 133 | input String terminalsDir "directory shipped as terminalsAndIcons/ (the XML and the rendered icons); empty for none"; | ||
| 134 | input String simulationFlagsJson "--fmiFlags as CodegenFMU renders it, empty when there are none"; | ||
| 135 | algorithm | ||
| 136 | ✗ | Error.addInternalError("CodegenWasmJit.emitMeFmu: the wasm FMU target is only implemented in the Rust omc build", sourceInfo()); | |
| 137 | ✗ | fail(); | |
| 138 | end emitMeFmu; | ||
| 139 | |||
| 140 | function emitCsFmu | ||
| 141 | " wasm Co-Simulation export: as emitMeFmu, but the component embeds the | ||
| 142 | simulation driver (the FMU integrates itself between the importer's | ||
| 143 | communication points). Implemented in Rust. " | ||
| 144 | input SimCode.SimCode simCode; | ||
| 145 | input String fmuPath; | ||
| 146 | input String guid; | ||
| 147 | input String modelDescription; | ||
| 148 | input String lsDaeManifest "fmi-ls-dae's manifest for a --daeMode model, empty for none"; | ||
| 149 | input String documentationDir "directory shipped as documentation/ (index.html and the images it references); empty for none"; | ||
| 150 | input String terminalsDir "directory shipped as terminalsAndIcons/ (the XML and the rendered icons); empty for none"; | ||
| 151 | input String simulationFlagsJson "--fmiFlags as CodegenFMU renders it, empty when there are none"; | ||
| 152 | algorithm | ||
| 153 | ✗ | Error.addInternalError("CodegenWasmJit.emitCsFmu: the wasm FMU target is only implemented in the Rust omc build", sourceInfo()); | |
| 154 | ✗ | fail(); | |
| 155 | end emitCsFmu; | ||
| 156 | |||
| 157 | function emitMeCsFmu | ||
| 158 | " wasm me_cs export: one component exporting both the Model-Exchange and | ||
| 159 | Co-Simulation interfaces (a single binary and modelIdentifier). Implemented in | ||
| 160 | Rust. " | ||
| 161 | input SimCode.SimCode simCode; | ||
| 162 | input String fmuPath; | ||
| 163 | input String guid; | ||
| 164 | input String modelDescription; | ||
| 165 | input String lsDaeManifest "fmi-ls-dae's manifest for a --daeMode model, empty for none"; | ||
| 166 | input String documentationDir "directory shipped as documentation/ (index.html and the images it references); empty for none"; | ||
| 167 | input String terminalsDir "directory shipped as terminalsAndIcons/ (the XML and the rendered icons); empty for none"; | ||
| 168 | input String simulationFlagsJson "--fmiFlags as CodegenFMU renders it, empty when there are none"; | ||
| 169 | algorithm | ||
| 170 | ✗ | Error.addInternalError("CodegenWasmJit.emitMeCsFmu: the wasm FMU target is only implemented in the Rust omc build", sourceInfo()); | |
| 171 | ✗ | fail(); | |
| 172 | end emitMeCsFmu; | ||
| 173 | |||
| 174 | function runSimulationWasmtime | ||
| 175 | " Run the standalone module (built by emitStandalone) in a wasmtime subprocess; | ||
| 176 | its _start writes the result file. Returns 0 on success, 1 on failure. | ||
| 177 | Implemented in Rust. " | ||
| 178 | input String fileNamePrefix; | ||
| 179 | input String resultFile; | ||
| 180 | input String simflags; | ||
| 181 | output Integer status; | ||
| 182 | algorithm | ||
| 183 | status := 0; | ||
| 184 | end runSimulationWasmtime; | ||
| 185 | |||
| 186 | function fmuCsSolvers | ||
| 187 | " The `method=` values a Co-Simulation wasm FMU can integrate with: the solvers | ||
| 188 | the driver linked into the component carries. `buildModelFMU` folds an accepted | ||
| 189 | method into the FMU's `resources/<prefix>_flags.json`, the only channel the | ||
| 190 | component reads its solver from. Empty without the Rust export. Implemented in Rust. " | ||
| 191 | output list<String> methods; | ||
| 192 | algorithm | ||
| 193 | methods := {}; | ||
| 194 | end fmuCsSolvers; | ||
| 195 | |||
| 196 | function fmuAcceptsFlag | ||
| 197 | " Whether a wasm FMU can honour the simulation flag `-name=value` (an empty | ||
| 198 | value for a flag that takes none). One it cannot is dropped rather than baked | ||
| 199 | into `resources/<prefix>_flags.json`, where it would only surface at the | ||
| 200 | importer's first instantiate. False without the Rust export. Implemented in Rust. " | ||
| 201 | input String name; | ||
| 202 | input String value; | ||
| 203 | output Boolean accepted; | ||
| 204 | algorithm | ||
| 205 | accepted := false; | ||
| 206 | end fmuAcceptsFlag; | ||
| 207 | |||
| 208 | annotation(__OpenModelica_Interface="codegen_wasm_jit"); | ||
| 209 | end CodegenWasmJit; | ||
| 210 |