OMCompiler/Compiler/MidCode/MidCode.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 MidCode | ||
| 37 | |||
| 38 | import Absyn; | ||
| 39 | import DAE; | ||
| 40 | import DAEDump; | ||
| 41 | |||
| 42 | uniontype Program | ||
| 43 | record PROGRAM | ||
| 44 | String name; | ||
| 45 | list<Function> functions; | ||
| 46 | end PROGRAM; | ||
| 47 | end Program; | ||
| 48 | |||
| 49 | uniontype Var | ||
| 50 | record VAR | ||
| 51 | String name; | ||
| 52 | DAE.Type ty; | ||
| 53 | Boolean volatile "Used for setjmp semantics in C."; // Codegen detail that doesn't really belong in midcode. | ||
| 54 | end VAR; | ||
| 55 | end Var; | ||
| 56 | |||
| 57 | uniontype VarBuf | ||
| 58 | /* | ||
| 59 | A MidCode variable containing a jmp_buf for longjmp. | ||
| 60 | */ | ||
| 61 | record VARBUF | ||
| 62 | String name; | ||
| 63 | end VARBUF; | ||
| 64 | end VarBuf; | ||
| 65 | |||
| 66 | uniontype VarBufPtr | ||
| 67 | /* | ||
| 68 | A MidCode variable containing a jmp_buf pointer for longjmp. | ||
| 69 | */ | ||
| 70 | record VARBUFPTR | ||
| 71 | String name; | ||
| 72 | end VARBUFPTR; | ||
| 73 | end VarBufPtr; | ||
| 74 | |||
| 75 | uniontype OutVar | ||
| 76 | record OUT_VAR | ||
| 77 | Var var; | ||
| 78 | end OUT_VAR; | ||
| 79 | record OUT_WILD end OUT_WILD; | ||
| 80 | end OutVar; | ||
| 81 | |||
| 82 | public function varString | ||
| 83 | input Var var; | ||
| 84 | output String str; | ||
| 85 | algorithm | ||
| 86 | ✗ | str := "(" + DAEDump.daeTypeStr(var.ty) + ") " + var.name; | |
| 87 | end varString; | ||
| 88 | |||
| 89 | uniontype Function | ||
| 90 | record FUNCTION | ||
| 91 | Absyn.Path name; | ||
| 92 | list<Var> locals; | ||
| 93 | list<VarBuf> localBufs; // jmb_buf for longjmp | ||
| 94 | list<VarBufPtr> localBufPtrs; // jmb_buf pointers for longjmp | ||
| 95 | list<Var> inputs; | ||
| 96 | list<Var> outputs; | ||
| 97 | list<Block> body; | ||
| 98 | Integer entryId; | ||
| 99 | Integer exitId; | ||
| 100 | end FUNCTION; | ||
| 101 | end Function; | ||
| 102 | |||
| 103 | uniontype Block | ||
| 104 | record BLOCK | ||
| 105 | "Basic block. | ||
| 106 | No control flow within block. | ||
| 107 | Can branch or jump on exit, called the block's terminator." | ||
| 108 | Integer id; | ||
| 109 | list<Stmt> stmts; | ||
| 110 | Terminator terminator; | ||
| 111 | end BLOCK; | ||
| 112 | end Block; | ||
| 113 | |||
| 114 | uniontype Terminator | ||
| 115 | record GOTO | ||
| 116 | Integer next; | ||
| 117 | end GOTO; | ||
| 118 | |||
| 119 | record BRANCH | ||
| 120 | Var condition; | ||
| 121 | Integer onTrue; | ||
| 122 | Integer onFalse; | ||
| 123 | end BRANCH; | ||
| 124 | |||
| 125 | record CALL | ||
| 126 | Absyn.Path func; | ||
| 127 | Boolean builtin; //vilka övriga CallAttributes relevanta? | ||
| 128 | list<Var> inputs; | ||
| 129 | list<OutVar> outputs; | ||
| 130 | Integer next; | ||
| 131 | end CALL; | ||
| 132 | |||
| 133 | record RETURN | ||
| 134 | end RETURN; | ||
| 135 | |||
| 136 | record SWITCH | ||
| 137 | Var condition; | ||
| 138 | list<tuple<Integer,Integer>> cases; | ||
| 139 | end SWITCH; | ||
| 140 | |||
| 141 | record LONGJMP "used for fail() stmts" | ||
| 142 | end LONGJMP; | ||
| 143 | |||
| 144 | record PUSHJMP "used for match-continue fail() handling" | ||
| 145 | VarBufPtr old_buf "where to save old jmp_buf"; | ||
| 146 | VarBuf new_buf "what to use as new jmp_buf"; | ||
| 147 | Integer next "where to goto next and the setjmp target"; | ||
| 148 | end PUSHJMP; | ||
| 149 | |||
| 150 | /* POPJMP does not cause control flow but | ||
| 151 | if it is a terminator to simplify matching | ||
| 152 | with their respective PUSHJMPS. | ||
| 153 | */ | ||
| 154 | record POPJMP "used for match-continue fail() handling" | ||
| 155 | VarBufPtr old_buf "what to reset to"; | ||
| 156 | Integer next; | ||
| 157 | end POPJMP; | ||
| 158 | |||
| 159 | record ASSERT | ||
| 160 | Var condition; | ||
| 161 | Var message; | ||
| 162 | Var level; | ||
| 163 | Integer next; | ||
| 164 | end ASSERT; | ||
| 165 | |||
| 166 | record TERMINATE | ||
| 167 | Var message; | ||
| 168 | end TERMINATE; | ||
| 169 | |||
| 170 | end Terminator; | ||
| 171 | |||
| 172 | uniontype Stmt | ||
| 173 | record NOP | ||
| 174 | end NOP; | ||
| 175 | |||
| 176 | record ASSIGN | ||
| 177 | Var dest; | ||
| 178 | RValue src; | ||
| 179 | end ASSIGN; | ||
| 180 | end Stmt; | ||
| 181 | |||
| 182 | uniontype RValue | ||
| 183 | record VARIABLE | ||
| 184 | Var src; | ||
| 185 | end VARIABLE; | ||
| 186 | |||
| 187 | record UNARYOP | ||
| 188 | UnaryOp op; | ||
| 189 | Var src; | ||
| 190 | end UNARYOP; | ||
| 191 | |||
| 192 | record BINARYOP | ||
| 193 | BinaryOp op; | ||
| 194 | Var lsrc; | ||
| 195 | Var rsrc; | ||
| 196 | end BINARYOP; | ||
| 197 | |||
| 198 | record LITERALINTEGER | ||
| 199 | Integer value; | ||
| 200 | end LITERALINTEGER; | ||
| 201 | |||
| 202 | record LITERALREAL | ||
| 203 | Real value; | ||
| 204 | end LITERALREAL; | ||
| 205 | |||
| 206 | record LITERALBOOLEAN | ||
| 207 | Boolean value; | ||
| 208 | end LITERALBOOLEAN; | ||
| 209 | |||
| 210 | record LITERALSTRING | ||
| 211 | String value; | ||
| 212 | end LITERALSTRING; | ||
| 213 | |||
| 214 | record LITERALMETATYPE | ||
| 215 | list<Var> elements; | ||
| 216 | DAE.Type ty; | ||
| 217 | end LITERALMETATYPE; | ||
| 218 | |||
| 219 | |||
| 220 | |||
| 221 | /* | ||
| 222 | CTOR SLOTS | ||
| 223 | 0 0 nil för list | ||
| 224 | 0 1+ tuple | ||
| 225 | 1 0 none för option | ||
| 226 | 1 1 some för option | ||
| 227 | 1 2 cons för list | ||
| 228 | 2 0+ array | ||
| 229 | 3+ 0+ record | ||
| 230 | */ | ||
| 231 | |||
| 232 | record UNIONTYPEVARIANT | ||
| 233 | Var src; | ||
| 234 | end UNIONTYPEVARIANT; | ||
| 235 | |||
| 236 | record ISSOME | ||
| 237 | Var src; | ||
| 238 | end ISSOME; | ||
| 239 | |||
| 240 | record ISCONS | ||
| 241 | Var src; | ||
| 242 | end ISCONS; | ||
| 243 | |||
| 244 | record METAFIELD "get value from metamodelica object" | ||
| 245 | Var src; | ||
| 246 | Integer index; | ||
| 247 | DAE.Type ty "type of value"; | ||
| 248 | end METAFIELD; | ||
| 249 | end RValue; | ||
| 250 | |||
| 251 | uniontype UnaryOp | ||
| 252 | record MOVE end MOVE; | ||
| 253 | record UMINUS end UMINUS; | ||
| 254 | record NOT end NOT; | ||
| 255 | record UNBOX end UNBOX; | ||
| 256 | record BOX end BOX; | ||
| 257 | end UnaryOp; | ||
| 258 | |||
| 259 | uniontype BinaryOp | ||
| 260 | record ADD end ADD; | ||
| 261 | record SUB end SUB; | ||
| 262 | record MUL end MUL; | ||
| 263 | record DIV end DIV; | ||
| 264 | record POW end POW; | ||
| 265 | record LESS end LESS; | ||
| 266 | record LESSEQ end LESSEQ; | ||
| 267 | record GREATER end GREATER; | ||
| 268 | record GREATEREQ end GREATEREQ; | ||
| 269 | record EQUAL end EQUAL; | ||
| 270 | record NEQUAL end NEQUAL; | ||
| 271 | end BinaryOp; | ||
| 272 | |||
| 273 | annotation(__OpenModelica_Interface="codegen_util"); | ||
| 274 | end MidCode; | ||
| 275 |