Appearance
| 1 | namespace IR.Values is | |
| 2 | use Semantic.Types.Type | |
| 3 | ||
| 4 | // One map/filter stage of a fused-consumer loop. The stage's lambda | |
| 5 | // delegate is hoisted into a local (built once) and `apply` invokes it | |
| 6 | // on the current element, yielding either a bool (a `filter` stage - | |
| 7 | // branch to the next element when false) or the transformed element (a | |
| 8 | // `map` stage - stored into `output` for the next stage). The | |
| 9 | // delegate is called rather than inlined so a lambda that captures an | |
| 10 | // enclosing local (or nests another closure) keeps its normal capture | |
| 11 | // context. | |
| 12 | // | |
| 13 | // A stage is one shape or the other, so each constructor fills its | |
| 14 | // own fields and leaves the other shape's alone; `is_countdown` | |
| 15 | // says which are live. | |
| 16 | @suppress("field-definite-assignment") | |
| 17 | class FUSED_CONSUMER_STAGE is | |
| 18 | is_filter: bool public | |
| 19 | delegate_local: IR.LOCAL public | |
| 20 | delegate_init: Value public | |
| 21 | apply: Value public | |
| 22 | output: IR.LOCAL? public | |
| 23 | ||
| 24 | // A `take`/`skip` stage carries no delegate: the count is evaluated | |
| 25 | // once into `counter` and each element decrements it (the | |
| 26 | // loop stops pulling once a take's runs out, skip drops the element | |
| 27 | // while its remains). `is_countdown` marks either. | |
| 28 | is_take: bool public | |
| 29 | is_skip: bool public | |
| 30 | counter: IR.LOCAL public | |
| 31 | count_init: Value? public | |
| 32 | ||
| 33 | // An index stage carries a running counter like a countdown does, | |
| 34 | // but produces an output local as a map stage does: `apply` builds | |
| 35 | // the INDEXED_VALUE from the counter and the incoming element. | |
| 36 | is_index: bool public | |
| 37 | ||
| 38 | is_countdown: bool => is_take \/ is_skip | |
| 39 | ||
| 40 | init(is_filter: bool, delegate_local: IR.LOCAL, delegate_init: Value, apply: Value, output: IR.LOCAL?) is | |
| 41 | self.is_filter = is_filter | |
| 42 | self.delegate_local = delegate_local | |
| 43 | self.delegate_init = delegate_init | |
| 44 | self.apply = apply | |
| 45 | self.output = output | |
| 46 | si | |
| 47 | ||
| 48 | init(is_take: bool, is_skip: bool, counter: IR.LOCAL, count_init: Value) is | |
| 49 | self.is_take = is_take | |
| 50 | self.is_skip = is_skip | |
| 51 | self.counter = counter | |
| 52 | self.count_init = count_init | |
| 53 | si | |
| 54 | ||
| 55 | init(counter: IR.LOCAL, start_init: Value, output: IR.LOCAL, apply: Value) is | |
| 56 | self.is_index = true | |
| 57 | self.counter = counter | |
| 58 | self.count_init = start_init | |
| 59 | self.output = output | |
| 60 | self.apply = apply | |
| 61 | si | |
| 62 | si | |
| 63 | ||
| 64 | // A terminal Pipe consumer (`count`, `any`, `all`, `each`, `reduce`, | |
| 65 | // `find`, `first`, `collect_mutable`) fused with its chain: drives the | |
| 66 | // chain's source iterator directly, applies the map/filter stages per | |
| 67 | // element by calling each stage's hoisted delegate, runs the consumer's | |
| 68 | // per-element body, and leaves the consumer's result on the stack - | |
| 69 | // emitted in place of the consumer method call, so it flows through any | |
| 70 | // expression context (like NULL_COALESCE_VALUE). | |
| 71 | class FUSED_CONSUMER: Composite is | |
| 72 | _result_type: Type | |
| 73 | ||
| 74 | ||
| 75 | // Loop skeleton, pre-built against the '.fc_iter.N' iterator local. | |
| 76 | // `iterator_init` produces the source's own iterator (or is the | |
| 77 | // source itself when it is already an iterator); move_next / | |
| 78 | // read_current drive it. | |
| 79 | has_iterator: bool public | |
| 80 | iterator_init: Value public | |
| 81 | iterator: IR.LOCAL public | |
| 82 | move_next: Value public | |
| 83 | read_current: Value public | |
| 84 | ||
| 85 | // The element local the innermost stage reads. | |
| 86 | element: IR.LOCAL public | |
| 87 | ||
| 88 | stages: Collections.List[FUSED_CONSUMER_STAGE] public | |
| 89 | ||
| 90 | // The surviving element after every stage - what the consumer body | |
| 91 | // operates on (`collect` adds it, `find` returns it, ...). | |
| 92 | final_element: IR.LOCAL public | |
| 93 | ||
| 94 | // Consumer specifics. | |
| 95 | consumer_kind: string public | |
| 96 | ||
| 97 | // join. The result local holds the interpolation handler the | |
| 98 | // elements are written into and `finish` reads it back as a | |
| 99 | // string, so what is left is the separator, evaluated once | |
| 100 | // before the loop, and whether anything has been written yet. | |
| 101 | // A flag rather than the handler's own length, because an | |
| 102 | // element whose text is empty still counts as written. | |
| 103 | join_separator: IR.LOCAL? public | |
| 104 | join_separator_init: Value? public | |
| 105 | join_seen: IR.LOCAL? public | |
| 106 | join_append_separator: Value? public | |
| 107 | join_append_element: Value? public | |
| 108 | ||
| 109 | // The result local, named in compile-expressions so pre-built | |
| 110 | // values (a `reduce` accumulator) can reference it. It holds the | |
| 111 | // consumer's own type unless `result_local_type` says otherwise: | |
| 112 | // `collect` fills a list there and its `finish` derives the array. | |
| 113 | result: IR.LOCAL public | |
| 114 | result_local_type: Type? public | |
| 115 | ||
| 116 | // A consumer with a function argument (`any`/`all`/`find`/`each` | |
| 117 | // predicate/action, `reduce` accumulator) hoists it into a local so | |
| 118 | // the delegate is built once. `consumer_apply` is that argument | |
| 119 | // applied per element (to the final element, and for `reduce` the | |
| 120 | // running result too), pre-built against the hoisted local. | |
| 121 | consumer_arg: IR.LOCAL? public | |
| 122 | consumer_arg_init: Value? public | |
| 123 | consumer_apply: Value? public | |
| 124 | ||
| 125 | // `reduce` seeds the result with its first argument; `find`/`first` | |
| 126 | // seed the result with an empty MAYBE; `collect` seeds it with a | |
| 127 | // fresh list. | |
| 128 | seed_init: Value? public | |
| 129 | ||
| 130 | // `find`/`first` wrap the surviving element in a MAYBE; `collect` | |
| 131 | // adds it to the result list. Pre-built against the element / result | |
| 132 | // locals. | |
| 133 | wrap_element: Value? public | |
| 134 | add_element: Value? public | |
| 135 | ||
| 136 | // A consumer whose result is derived from the result local after | |
| 137 | // the loop rather than being that local: `collect` fills a list | |
| 138 | // and hands back its array. Pre-built against the result local; | |
| 139 | // leaves the consumer's value on the stack. | |
| 140 | finish: Value? public | |
| 141 | ||
| 142 | // The source, evaluated once into a local before the iterator is | |
| 143 | // taken from it, where the consumer reads the source twice - for | |
| 144 | // its iterator and for its count. `iterator_init` and the count | |
| 145 | // are then pre-built over that local. Absent, `iterator_init` | |
| 146 | // evaluates the source itself. | |
| 147 | source: IR.LOCAL? public | |
| 148 | source_init: Value? public | |
| 149 | ||
| 150 | // A collect over a source whose count is known before the loop, | |
| 151 | // through stages that keep it, sizes its result up front. For a | |
| 152 | // list result the count is the capacity `seed_init` was built | |
| 153 | // with. For an array result `presize_array` is set, | |
| 154 | // `presize_count` is the count pre-built over the source local, | |
| 155 | // and the loop writes each element at `presize_index` | |
| 156 | // into an array allocated at that count, so no list is filled | |
| 157 | // and no copy is made. | |
| 158 | presize_count: Value? public | |
| 159 | presize_array: bool public | |
| 160 | presize_index: IR.LOCAL? public | |
| 161 | presize_element_type: Type? public | |
| 162 | ||
| 163 | type: Type => _result_type | |
| 164 | is_lightweight_pure: bool => false | |
| 165 | ||
| 166 | init( | |
| 167 | result_type: Type, | |
| 168 | iterator_init: Value, | |
| 169 | has_iterator: bool, | |
| 170 | iterator: IR.LOCAL, | |
| 171 | move_next: Value, | |
| 172 | read_current: Value, | |
| 173 | element: IR.LOCAL, | |
| 174 | stages: Collections.List[FUSED_CONSUMER_STAGE], | |
| 175 | final_element: IR.LOCAL, | |
| 176 | consumer_kind: string, | |
| 177 | result: IR.LOCAL | |
| 178 | ) is | |
| 179 | super.init() | |
| 180 | ||
| 181 | self._result_type = result_type | |
| 182 | self.iterator_init = iterator_init | |
| 183 | self.has_iterator = has_iterator | |
| 184 | self.iterator = iterator | |
| 185 | self.move_next = move_next | |
| 186 | self.read_current = read_current | |
| 187 | self.element = element | |
| 188 | self.stages = stages | |
| 189 | self.final_element = final_element | |
| 190 | self.consumer_kind = consumer_kind | |
| 191 | self.result = result | |
| 192 | si | |
| 193 | ||
| 194 | is_void: bool => Syntax.Process.PIPE_CONSUMER_KIND.is_void(consumer_kind) | |
| 195 | ||
| 196 | // The fused loop, leaving the consumer's result (nothing for a | |
| 197 | // void `each`). | |
| 198 | expand() -> BLOCK is | |
| 199 | let expansion = IR.EXPANSION(_result_type) | |
| 200 | let int_type = IoC.CONTAINER.instance.innate_symbol_lookup.get_int_type() | |
| 201 | ||
| 202 | // A source read more than once - for its iterator and for its | |
| 203 | // count - is evaluated once, first, and everything below reads | |
| 204 | // the local. | |
| 205 | if let local = source, init = source_init then | |
| 206 | expansion.store(expansion.declare(local), init) | |
| 207 | fi | |
| 208 | ||
| 209 | result.type = result_local_type ?? _result_type | |
| 210 | ||
| 211 | if !is_void then | |
| 212 | expansion.declare(result) | |
| 213 | _expand_result_init(expansion, result, int_type) | |
| 214 | fi | |
| 215 | ||
| 216 | // A consumer's function argument is built once into a local. | |
| 217 | if let local = consumer_arg, arg_init = consumer_arg_init then | |
| 218 | expansion.store(expansion.declare(local), arg_init) | |
| 219 | fi | |
| 220 | ||
| 221 | // Each stage's lambda delegate is built once before the loop; a | |
| 222 | // take/skip stage instead seeds its running counter with the | |
| 223 | // count evaluated once. | |
| 224 | for stage in stages do | |
| 225 | if stage.is_countdown \/ stage.is_index then | |
| 226 | expansion.store(expansion.declare(stage.counter), stage.count_init!) | |
| 227 | else | |
| 228 | expansion.store(expansion.declare(stage.delegate_local), stage.delegate_init) | |
| 229 | fi | |
| 230 | od | |
| 231 | ||
| 232 | // The iterator always lives in a local - whether it came from | |
| 233 | // the source's iterator member or the source is itself an | |
| 234 | // iterator (a value-type range), move_next / current drive it | |
| 235 | // through the local (by address for a value-type iterator). | |
| 236 | expansion.store(expansion.declare(iterator), iterator_init) | |
| 237 | ||
| 238 | let loop = expansion.loop() | |
| 239 | ||
| 240 | // Leave before pulling once any take stage has spent its count, | |
| 241 | // so the loop pulls exactly the elements it yields and never | |
| 242 | // advances the source past them. | |
| 243 | for stage in stages do | |
| 244 | if stage.is_take then | |
| 245 | expansion.exit_if( | |
| 246 | loop, | |
| 247 | IR.BRANCH.LE, | |
| 248 | stage.counter.load(), | |
| 249 | Literal.NUMBER(0, int_type)) | |
| 250 | fi | |
| 251 | od | |
| 252 | ||
| 253 | expansion.exit_if(loop, IR.BRANCH.Z, move_next) | |
| 254 | expansion.store(expansion.declare(element), read_current) | |
| 255 | ||
| 256 | for stage in stages do | |
| 257 | if stage.is_countdown then | |
| 258 | // This element spends one of the stage's count. A take's | |
| 259 | // check at the top of the loop decides when to stop; a | |
| 260 | // skip drops the element while its counter stays | |
| 261 | // non-negative. | |
| 262 | let counter = stage.counter | |
| 263 | ||
| 264 | expansion.store( | |
| 265 | counter, | |
| 266 | ARITHMETIC( | |
| 267 | IR.ArithmeticOperation.SUBTRACT, | |
| 268 | counter.load(), | |
| 269 | Literal.NUMBER(1, int_type), | |
| 270 | int_type).unmarked()) | |
| 271 | ||
| 272 | if stage.is_skip then | |
| 273 | expansion.continue_if(loop, IR.BRANCH.GE, counter.load(), Literal.NUMBER(0, int_type)) | |
| 274 | fi | |
| 275 | elif stage.is_filter then | |
| 276 | expansion.continue_if(loop, IR.BRANCH.Z, stage.apply) | |
| 277 | else | |
| 278 | expansion.store(expansion.declare(stage.output!), stage.apply) | |
| 279 | fi | |
| 280 | od | |
| 281 | ||
| 282 | _expand_element_body(expansion, loop, result, int_type) | |
| 283 | ||
| 284 | expansion.close(loop) | |
| 285 | ||
| 286 | if let derived = finish then | |
| 287 | expansion.add(derived) | |
| 288 | elif !is_void then | |
| 289 | expansion.add(result.load()) | |
| 290 | fi | |
| 291 | ||
| 292 | return expansion.result | |
| 293 | si | |
| 294 | ||
| 295 | // Initialise the result local before the loop. | |
| 296 | _expand_result_init(expansion: IR.EXPANSION, result: IR.LOCAL, int_type: Type) is | |
| 297 | if consumer_kind =~ "count" \/ consumer_kind =~ "any" then | |
| 298 | // count starts at 0; any starts false (0). | |
| 299 | expansion.store(result, Literal.NUMBER(0, result.type)) | |
| 300 | elif consumer_kind =~ "all" then | |
| 301 | expansion.store(result, Literal.NUMBER(1, result.type)) | |
| 302 | elif presize_array then | |
| 303 | // The array, allocated at the source's count, and the | |
| 304 | // index the loop writes at, from zero. | |
| 305 | expansion.store(result, NEW_ARRAY(result.type, presize_element_type!, presize_count!)) | |
| 306 | expansion.store(expansion.declare(presize_index!), Literal.NUMBER(0, int_type)) | |
| 307 | elif let seed = seed_init then | |
| 308 | // reduce (seed), find / first (empty MAYBE), collect (list), | |
| 309 | // join (the interpolation handler). | |
| 310 | expansion.store(result, seed) | |
| 311 | fi | |
| 312 | ||
| 313 | if let separator = join_separator_init, local = join_separator then | |
| 314 | let seen = join_seen! | |
| 315 | ||
| 316 | expansion.store(expansion.declare(local), separator) | |
| 317 | expansion.store(expansion.declare(seen), Literal.NUMBER(0, seen.type)) | |
| 318 | fi | |
| 319 | si | |
| 320 | ||
| 321 | // Per-element consumer body (after the stages have produced the | |
| 322 | // surviving element). | |
| 323 | _expand_element_body(expansion: IR.EXPANSION, loop: LOOP, result: IR.LOCAL, int_type: Type) is | |
| 324 | let element = final_element.load() | |
| 325 | ||
| 326 | if consumer_kind =~ "count" then | |
| 327 | expansion.store(result, _add(result.load(), Literal.NUMBER(1, result.type), result.type)) | |
| 328 | elif consumer_kind =~ "any" then | |
| 329 | _expand_answer(expansion, loop, result, IR.BRANCH.Z, Literal.NUMBER(1, result.type)) | |
| 330 | elif consumer_kind =~ "all" then | |
| 331 | _expand_answer(expansion, loop, result, IR.BRANCH.NZ, Literal.NUMBER(0, result.type)) | |
| 332 | elif consumer_kind =~ "each" then | |
| 333 | expansion.add(consumer_apply!) | |
| 334 | elif consumer_kind =~ "reduce" then | |
| 335 | expansion.store(result, consumer_apply!) | |
| 336 | elif consumer_kind =~ "join" then | |
| 337 | // The separator goes in ahead of every element but the | |
| 338 | // first, then the element's own text, as the runtime's | |
| 339 | // append_to writes it. | |
| 340 | let seen = join_seen! | |
| 341 | let first = expansion.region() | |
| 342 | ||
| 343 | expansion.exit_if(first, IR.BRANCH.Z, seen.load()) | |
| 344 | expansion.add(join_append_separator!) | |
| 345 | expansion.close(first) | |
| 346 | ||
| 347 | expansion.add(join_append_element!) | |
| 348 | expansion.store(seen, Literal.NUMBER(1, seen.type)) | |
| 349 | elif consumer_kind =~ "sum_by" then | |
| 350 | expansion.store(result, _add(result.load(), consumer_apply!, result.type)) | |
| 351 | elif consumer_kind =~ "sum" then | |
| 352 | // As the one instruction the element type's own `+` is. A | |
| 353 | // type whose addition is an operator method declines at | |
| 354 | // recognition. | |
| 355 | expansion.store(result, _add(result.load(), element, result.type)) | |
| 356 | elif consumer_kind =~ "find" then | |
| 357 | _expand_answer(expansion, loop, result, IR.BRANCH.Z, wrap_element!) | |
| 358 | elif consumer_kind =~ "first" then | |
| 359 | expansion.store(result, wrap_element!) | |
| 360 | expansion.exit(loop) | |
| 361 | elif presize_array then | |
| 362 | let element_type = presize_element_type! | |
| 363 | let index = presize_index! | |
| 364 | ||
| 365 | expansion.add( | |
| 366 | STORE_ARRAY_ELEMENT( | |
| 367 | result.load(), | |
| 368 | index.load(), | |
| 369 | IoC.CONTAINER.instance.value_boxer.box_if_needed(element, element_type), | |
| 370 | element_type)) | |
| 371 | ||
| 372 | expansion.store(index, _add(index.load(), Literal.NUMBER(1, int_type), int_type)) | |
| 373 | elif consumer_kind =~ "collect" \/ consumer_kind =~ "collect_array" then | |
| 374 | expansion.add(add_element!) | |
| 375 | fi | |
| 376 | si | |
| 377 | ||
| 378 | // Unless the consumer's argument answers `skip` for this element, | |
| 379 | // the loop stops with `answer` as the result. | |
| 380 | _expand_answer(expansion: IR.EXPANSION, loop: LOOP, result: IR.LOCAL, skip: IR.BRANCH, answer: Value) is | |
| 381 | let next = expansion.region() | |
| 382 | ||
| 383 | expansion.exit_if(next, skip, consumer_apply!) | |
| 384 | expansion.store(result, answer) | |
| 385 | expansion.exit(loop) | |
| 386 | expansion.close(next) | |
| 387 | si | |
| 388 | ||
| 389 | _add(left: Value, right: Value, type: Type) -> Value => | |
| 390 | ARITHMETIC(IR.ArithmeticOperation.ADD, left, right, type).unmarked() | |
| 391 | ||
| 392 | to_string() -> string => | |
| 393 | "fused-consumer:[{consumer_kind}]({_result_type})" | |
| 394 | si | |
| 395 | si |