| @@ -3,7 +3,7 @@ |
| 3 | 3 | use std::collections::{BTreeSet, HashMap}; |
| 4 | 4 | use std::fmt::Write; |
| 5 | 5 | |
| 6 | | −use tilc_core::ir::{Atom, Expr, FnDecl, FnId, Lit, Prim, Program, Value, VarId}; |
| 6 | +use tilc_core::ir::{Atom, Expr, FnDecl, FnId, Lit, OpKind, Prim, Program, Value, VarId}; |
| 7 | 7 | |
| 8 | 8 | use crate::Unsupported; |
| 9 | 9 | |
| @@ -25,13 +25,34 @@ static inline bool tl_str_is(tl_value s, const char *p, size_t n) { |
| 25 | 25 | return tl_string_len(s) == n && memcmp(tl_string_ptr(s), p, n) == 0; |
| 26 | 26 | } |
| 27 | 27 | |
| 28 | | −/* Function entry: count a reduction (preemption point). */ |
| 29 | | −#define TL_TICK(ctx) \ |
| 30 | | − do { if (tl_unlikely(--(ctx)->reductions <= 0)) tl_preempt(ctx); } while (0) |
| 28 | +/* Call a `final ctl` clause with the arguments in `payload` (the |
| 29 | + * operation's index, then its arguments), consuming both. Unused argument |
| 30 | + * slots are never read (all functions share one signature). */ |
| 31 | +static tl_value tl_call_payload(tl_ctx *ctx, tl_value clause, tl_value payload) { |
| 32 | + tl_block *p = tl_as_block(payload); |
| 33 | + int n = p->h.scan_fsize - 1; |
| 34 | + tl_value a[5] = {TL_UNIT, TL_UNIT, TL_UNIT, TL_UNIT, TL_UNIT}; |
| 35 | + for (int i = 0; i < n; i++) { |
| 36 | + tl_value x = tl_dup(p->fields[1 + i]); |
| 37 | + if (i < 5) a[i] = x; else ctx->xargs[i - 5] = x; |
| 38 | + } |
| 39 | + tl_drop(ctx, payload); |
| 40 | + return ((tl_fn)tl_closure_code(clause))(ctx, clause, a[0], a[1], a[2], a[3], a[4]); |
| 41 | +} |
| 31 | 42 | |
| 32 | 43 | "#; |
| 33 | 44 | |
| 34 | 45 | /// Runtime functions declared in tilrt.h (or defined inline there). |
| 46 | +/// Externs that may panic (and so unwind). |
| 47 | +const PANICKING: &[&str] = &[ |
| 48 | + "tl_int_add", |
| 49 | + "tl_int_sub", |
| 50 | + "tl_int_mul", |
| 51 | + "tl_int_div", |
| 52 | + "tl_int_rem", |
| 53 | + "tl_panic", |
| 54 | +]; |
| 55 | + |
| 35 | 56 | const DECLARED: &[&str] = &[ |
| 36 | 57 | "tl_int_add", |
| 37 | 58 | "tl_int_sub", |
| @@ -119,8 +140,7 @@ static tl_value tl_entry(tl_ctx *ctx) {{ |
| 119 | 140 | }} |
| 120 | 141 | |
| 121 | 142 | int main(void) {{ |
| 122 | | − tl_set_fatal_panics(); |
| 123 | | − int code = tl_main(tl_entry); |
| 143 | + int code = tl_main_program(tl_entry); |
| 124 | 144 | tl_report_leaks(); |
| 125 | 145 | return code; |
| 126 | 146 | }} |
| @@ -129,6 +149,29 @@ int main(void) {{ |
| 129 | 149 | Ok(out) |
| 130 | 150 | } |
| 131 | 151 | |
| 152 | +/// The function being emitted. |
| 153 | +struct Func<'a> { |
| 154 | + id: FnId, |
| 155 | + decl: &'a FnDecl, |
| 156 | +} |
| 157 | + |
| 158 | +/// Remove one reference to `v` from `held`. |
| 159 | +fn take(held: &mut Vec<VarId>, v: VarId) { |
| 160 | + if let Some(i) = held.iter().position(|h| *h == v) { |
| 161 | + held.swap_remove(i); |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +/// Unwinding out of this frame: drop what it holds, return. |
| 166 | +fn unwind(held: &[VarId]) -> String { |
| 167 | + let mut s = String::from("{ "); |
| 168 | + for v in held { |
| 169 | + let _ = write!(s, "tl_drop(ctx, v{v}); "); |
| 170 | + } |
| 171 | + s.push_str("return TL_UNIT; }"); |
| 172 | + s |
| 173 | +} |
| 174 | + |
| 132 | 175 | /// Where the value of an expression goes. |
| 133 | 176 | #[derive(Clone, Copy)] |
| 134 | 177 | enum Dest { |
| @@ -199,8 +242,21 @@ impl Emitter<'_> { |
| 199 | 242 | for chunk in locals.chunks(8) { |
| 200 | 243 | let _ = writeln!(out, " tl_value {};", chunk.join(", ")); |
| 201 | 244 | } |
| 202 | | − out.push_str("entry:\n TL_TICK(ctx);\n"); |
| 203 | | − self.expr(&mut out, id, decl, &decl.body, Dest::Return, 1)?; |
| 245 | + // A preemption point; an exit signal unwinds from here. |
| 246 | + let _ = writeln!( |
| 247 | + out, |
| 248 | + "entry:\n if (tl_unlikely(--ctx->reductions <= 0)) {{\n tl_preempt(ctx);\n if (TL_YIELDING(ctx)) {}\n }}", |
| 249 | + unwind(&decl.params) |
| 250 | + ); |
| 251 | + let func = Func { id, decl }; |
| 252 | + self.expr( |
| 253 | + &mut out, |
| 254 | + &func, |
| 255 | + &decl.body, |
| 256 | + Dest::Return, |
| 257 | + decl.params.clone(), |
| 258 | + 1, |
| 259 | + )?; |
| 204 | 260 | out.push_str("}\n\n"); |
| 205 | 261 | self.body.push_str(&out); |
| 206 | 262 | Ok(()) |
| @@ -275,16 +331,20 @@ impl Emitter<'_> { |
| 275 | 331 | ) |
| 276 | 332 | } |
| 277 | 333 | |
| 278 | | − #[allow(clippy::too_many_arguments)] |
| 334 | + /// Emit `e`. `held` are the references the frame holds before it (a |
| 335 | + /// multiset, as in `tilc_perceus::verify`): what an unwind from inside |
| 336 | + /// `e` must drop. Returns the references held when a block's value is |
| 337 | + /// produced (`Dest::Assign`), or `None` if every path leaves the |
| 338 | + /// function. |
| 279 | 339 | fn expr( |
| 280 | 340 | &mut self, |
| 281 | 341 | out: &mut String, |
| 282 | | − id: FnId, |
| 283 | | − decl: &FnDecl, |
| 342 | + f: &Func, |
| 284 | 343 | e: &Expr, |
| 285 | 344 | dest: Dest, |
| 345 | + mut held: Vec<VarId>, |
| 286 | 346 | depth: usize, |
| 287 | | − ) -> Result<(), Unsupported> { |
| 347 | + ) -> Result<Option<Vec<VarId>>, Unsupported> { |
| 288 | 348 | let ind = " ".repeat(depth); |
| 289 | 349 | match e { |
| 290 | 350 | Expr::Let(v, value, rest) => { |
| @@ -292,19 +352,22 @@ impl Emitter<'_> { |
| 292 | 352 | && matches!(&**rest, Expr::Return(Atom::Var(r)) if r == v); |
| 293 | 353 | if tail { |
| 294 | 354 | match &**value { |
| 295 | | − Value::Block(inner) => return self.expr(out, id, decl, inner, dest, depth), |
| 296 | | − Value::CallFn(g, args) if *g == id => { |
| 297 | | − // Self tail call: a jump. |
| 355 | + Value::Block(inner) => { |
| 356 | + return self.expr(out, f, inner, dest, held, depth); |
| 357 | + } |
| 358 | + Value::CallFn(g, args) if *g == f.id => { |
| 359 | + // Self tail call: a jump. Perceus consumed or |
| 360 | + // dropped everything else before it. |
| 298 | 361 | let args = self.atoms(args)?; |
| 299 | 362 | let _ = writeln!(out, "{ind}{{"); |
| 300 | 363 | for (i, a) in args.iter().enumerate() { |
| 301 | 364 | let _ = writeln!(out, "{ind} tl_value t{i} = {a};"); |
| 302 | 365 | } |
| 303 | | − for (i, p) in decl.params.iter().enumerate() { |
| 366 | + for (i, p) in f.decl.params.iter().enumerate() { |
| 304 | 367 | let _ = writeln!(out, "{ind} v{p} = t{i};"); |
| 305 | 368 | } |
| 306 | 369 | let _ = writeln!(out, "{ind} goto entry;\n{ind}}}"); |
| 307 | | − return Ok(()); |
| 370 | + return Ok(None); |
| 308 | 371 | } |
| 309 | 372 | Value::CallFn(g, args) => { |
| 310 | 373 | let slots = self.atoms(args)?; |
| @@ -314,14 +377,14 @@ impl Emitter<'_> { |
| 314 | 377 | "{ind}TL_MUSTTAIL return f{g}(ctx, {});", |
| 315 | 378 | register_args(&slots) |
| 316 | 379 | ); |
| 317 | | − return Ok(()); |
| 380 | + return Ok(None); |
| 318 | 381 | } |
| 319 | | − Value::Call(f, args) => { |
| 320 | | − let f = self.atom(f)?; |
| 382 | + Value::Call(c, args) => { |
| 383 | + let c = self.atom(c)?; |
| 321 | 384 | let args = self.atoms(args)?; |
| 322 | | − let call = Self::closure_call(&f, &args, &ind, out); |
| 385 | + let call = Self::closure_call(&c, &args, &ind, out); |
| 323 | 386 | let _ = writeln!(out, "{ind}TL_MUSTTAIL return {call};"); |
| 324 | | − return Ok(()); |
| 387 | + return Ok(None); |
| 325 | 388 | } |
| 326 | 389 | _ => {} |
| 327 | 390 | } |
| @@ -329,53 +392,76 @@ impl Emitter<'_> { |
| 329 | 392 | if let Value::Block(inner) = &**value { |
| 330 | 393 | self.labels += 1; |
| 331 | 394 | let label = self.labels; |
| 332 | | − self.expr(out, id, decl, inner, Dest::Assign(*v, label), depth)?; |
| 395 | + let after = self.expr(out, f, inner, Dest::Assign(*v, label), held, depth)?; |
| 333 | 396 | let _ = writeln!(out, "{ind}b{label}:;"); |
| 334 | | − } else { |
| 335 | | − self.value(out, *v, value, &ind)?; |
| 397 | + let Some(mut after) = after else { |
| 398 | + // Every path through the block leaves the function. |
| 399 | + return Ok(None); |
| 400 | + }; |
| 401 | + after.push(*v); |
| 402 | + return self.expr(out, f, rest, dest, after, depth); |
| 403 | + } |
| 404 | + let (owned, _) = tilc_perceus::uses(value); |
| 405 | + for o in owned { |
| 406 | + take(&mut held, o); |
| 407 | + } |
| 408 | + self.value(out, *v, value, &ind, &held)?; |
| 409 | + if self.may_unwind(value) { |
| 410 | + let _ = writeln!(out, "{ind}if (TL_YIELDING(ctx)) {}", unwind(&held)); |
| 336 | 411 | } |
| 337 | | − self.expr(out, id, decl, rest, dest, depth) |
| 412 | + held.push(*v); |
| 413 | + self.expr(out, f, rest, dest, held, depth) |
| 338 | 414 | } |
| 339 | 415 | Expr::Return(a) => { |
| 416 | + if let Atom::Var(x) = a { |
| 417 | + take(&mut held, *x); |
| 418 | + } |
| 340 | 419 | let a = self.atom(a)?; |
| 341 | 420 | match dest { |
| 342 | 421 | Dest::Return => { |
| 343 | 422 | let _ = writeln!(out, "{ind}return {a};"); |
| 423 | + Ok(None) |
| 344 | 424 | } |
| 345 | 425 | Dest::Assign(v, label) => { |
| 346 | 426 | let _ = writeln!(out, "{ind}v{v} = {a};\n{ind}goto b{label};"); |
| 427 | + Ok(Some(held)) |
| 347 | 428 | } |
| 348 | 429 | } |
| 349 | | − Ok(()) |
| 350 | 430 | } |
| 351 | 431 | Expr::Dup(v, rest) => { |
| 352 | 432 | let _ = writeln!(out, "{ind}tl_dup(v{v});"); |
| 353 | | − self.expr(out, id, decl, rest, dest, depth) |
| 433 | + held.push(*v); |
| 434 | + self.expr(out, f, rest, dest, held, depth) |
| 354 | 435 | } |
| 355 | 436 | Expr::Drop(v, rest) => { |
| 356 | 437 | let _ = writeln!(out, "{ind}tl_drop(ctx, v{v});"); |
| 357 | | − self.expr(out, id, decl, rest, dest, depth) |
| 438 | + take(&mut held, *v); |
| 439 | + self.expr(out, f, rest, dest, held, depth) |
| 358 | 440 | } |
| 359 | 441 | Expr::Panic(msg) => { |
| 360 | 442 | let _ = writeln!( |
| 361 | 443 | out, |
| 362 | | − "{ind}tl_panic_str(ctx, \"{}\");\n{ind}return TL_UNIT;", |
| 363 | | − c_escape(msg) |
| 444 | + "{ind}tl_panic_str(ctx, \"{}\");\n{ind}{}", |
| 445 | + c_escape(msg), |
| 446 | + unwind(&held) |
| 364 | 447 | ); |
| 365 | | − Ok(()) |
| 448 | + Ok(None) |
| 366 | 449 | } |
| 367 | 450 | Expr::Switch { on, cases, default } => { |
| 368 | 451 | let on = self.atom(on)?; |
| 452 | + let mut after = None; |
| 369 | 453 | let _ = writeln!(out, "{ind}switch (tl_tag_of({on})) {{"); |
| 370 | 454 | for (tag, body) in cases { |
| 371 | 455 | let _ = writeln!(out, "{ind}case {tag}: {{"); |
| 372 | | − self.expr(out, id, decl, body, dest, depth + 1)?; |
| 456 | + let a = self.expr(out, f, body, dest, held.clone(), depth + 1)?; |
| 457 | + after = after.or(a); |
| 373 | 458 | let _ = writeln!(out, "{ind}}}"); |
| 374 | 459 | } |
| 375 | 460 | match default { |
| 376 | 461 | Some(d) => { |
| 377 | 462 | let _ = writeln!(out, "{ind}default: {{"); |
| 378 | | − self.expr(out, id, decl, d, dest, depth + 1)?; |
| 463 | + let a = self.expr(out, f, d, dest, held, depth + 1)?; |
| 464 | + after = after.or(a); |
| 379 | 465 | let _ = writeln!(out, "{ind}}}"); |
| 380 | 466 | } |
| 381 | 467 | None => { |
| @@ -383,10 +469,14 @@ impl Emitter<'_> { |
| 383 | 469 | } |
| 384 | 470 | } |
| 385 | 471 | let _ = writeln!(out, "{ind}}}"); |
| 386 | | − Ok(()) |
| 472 | + Ok(after) |
| 387 | 473 | } |
| 388 | 474 | Expr::SwitchLit { on, cases, default } => { |
| 475 | + if cases.is_empty() { |
| 476 | + return self.expr(out, f, default, dest, held, depth); |
| 477 | + } |
| 389 | 478 | let on = self.atom(on)?; |
| 479 | + let mut after = None; |
| 390 | 480 | for (i, (lit, body)) in cases.iter().enumerate() { |
| 391 | 481 | let test = match lit { |
| 392 | 482 | Lit::Int(n) => format!("{on} == tl_int(INT64_C({n}))"), |
| @@ -397,26 +487,41 @@ impl Emitter<'_> { |
| 397 | 487 | }; |
| 398 | 488 | let kw = if i == 0 { "if" } else { "} else if" }; |
| 399 | 489 | let _ = writeln!(out, "{ind}{kw} ({test}) {{"); |
| 400 | | − self.expr(out, id, decl, body, dest, depth + 1)?; |
| 401 | | − } |
| 402 | | − if cases.is_empty() { |
| 403 | | − return self.expr(out, id, decl, default, dest, depth); |
| 490 | + let a = self.expr(out, f, body, dest, held.clone(), depth + 1)?; |
| 491 | + after = after.or(a); |
| 404 | 492 | } |
| 405 | 493 | let _ = writeln!(out, "{ind}}} else {{"); |
| 406 | | − self.expr(out, id, decl, default, dest, depth + 1)?; |
| 494 | + let a = self.expr(out, f, default, dest, held, depth + 1)?; |
| 495 | + after = after.or(a); |
| 407 | 496 | let _ = writeln!(out, "{ind}}}"); |
| 408 | | − Ok(()) |
| 497 | + Ok(after) |
| 498 | + } |
| 499 | + } |
| 500 | + } |
| 501 | + |
| 502 | + /// Whether computing `value` may start or continue unwinding. |
| 503 | + fn may_unwind(&self, value: &Value) -> bool { |
| 504 | + match value { |
| 505 | + Value::CallFn(..) | Value::Call(..) | Value::Handle { .. } | Value::Region { .. } => { |
| 506 | + true |
| 409 | 507 | } |
| 508 | + Value::Perform { effect, .. } => !self.program.effects[*effect as usize] |
| 509 | + .name |
| 510 | + .starts_with("std/ref."), |
| 511 | + Value::CallExtern(sym, _) => PANICKING.contains(&sym.as_str()), |
| 512 | + _ => false, |
| 410 | 513 | } |
| 411 | 514 | } |
| 412 | 515 | |
| 413 | | − /// `v = value;` (not a block). |
| 516 | + /// `v = value;` (not a block). `held`: references held during it, |
| 517 | + /// for the unwinding paths it contains. |
| 414 | 518 | fn value( |
| 415 | 519 | &mut self, |
| 416 | 520 | out: &mut String, |
| 417 | 521 | v: VarId, |
| 418 | 522 | value: &Value, |
| 419 | 523 | ind: &str, |
| 524 | + held: &[VarId], |
| 420 | 525 | ) -> Result<(), Unsupported> { |
| 421 | 526 | match value { |
| 422 | 527 | Value::Atom(a) => { |
| @@ -428,10 +533,10 @@ impl Emitter<'_> { |
| 428 | 533 | store_xargs(&slots, ind, out); |
| 429 | 534 | let _ = writeln!(out, "{ind}v{v} = f{g}(ctx, {});", register_args(&slots)); |
| 430 | 535 | } |
| 431 | | − Value::Call(f, args) => { |
| 432 | | − let f = self.atom(f)?; |
| 536 | + Value::Call(c, args) => { |
| 537 | + let c = self.atom(c)?; |
| 433 | 538 | let args = self.atoms(args)?; |
| 434 | | − let call = Self::closure_call(&f, &args, ind, out); |
| 539 | + let call = Self::closure_call(&c, &args, ind, out); |
| 435 | 540 | let _ = writeln!(out, "{ind}v{v} = {call};"); |
| 436 | 541 | } |
| 437 | 542 | Value::CallExtern(sym, args) => { |
| @@ -463,17 +568,17 @@ impl Emitter<'_> { |
| 463 | 568 | let b = self.atom(base)?; |
| 464 | 569 | let _ = writeln!(out, "{ind}v{v} = tl_dup(tl_as_block({b})->fields[{i}]);"); |
| 465 | 570 | } |
| 466 | | − Value::Closure(f, caps) => { |
| 571 | + Value::Closure(fid, caps) => { |
| 467 | 572 | let caps = self.atoms(caps)?; |
| 468 | 573 | if caps.len() > 254 { |
| 469 | 574 | return Err(Unsupported( |
| 470 | 575 | "closures capturing more than 254 values".into(), |
| 471 | 576 | )); |
| 472 | 577 | } |
| 473 | | − self.entries.insert((*f, caps.len())); |
| 578 | + self.entries.insert((*fid, caps.len())); |
| 474 | 579 | let _ = writeln!( |
| 475 | 580 | out, |
| 476 | | − "{ind}v{v} = tl_closure_alloc(ctx, (tl_closure_fn)f{f}_c{}, {});", |
| 581 | + "{ind}v{v} = tl_closure_alloc(ctx, (tl_closure_fn)f{fid}_c{}, {});", |
| 477 | 582 | caps.len(), |
| 478 | 583 | caps.len() |
| 479 | 584 | ); |
| @@ -488,21 +593,23 @@ impl Emitter<'_> { |
| 488 | 593 | args, |
| 489 | 594 | } => { |
| 490 | 595 | let decl = &self.program.effects[*effect as usize]; |
| 596 | + let n_ops = decl.ops.len(); |
| 597 | + let kind = decl.ops[*op as usize].1; |
| 491 | 598 | let cap = self.atom(cap)?; |
| 492 | 599 | let args = self.atoms(args)?; |
| 493 | 600 | let op_name = decl.ops[*op as usize].0.as_str(); |
| 494 | | − match (decl.name.as_str(), op_name) { |
| 495 | | − ("std/ref.alloc", _) => { |
| 601 | + match (decl.name.as_str(), op_name, kind) { |
| 602 | + ("std/ref.alloc", _, _) => { |
| 496 | 603 | let _ = writeln!(out, "{ind}v{v} = tl_ref_new(ctx, {cap}, {});", args[0]); |
| 497 | 604 | } |
| 498 | | − ("std/ref.st", "get") => { |
| 605 | + ("std/ref.st", "get", _) => { |
| 499 | 606 | let _ = writeln!( |
| 500 | 607 | out, |
| 501 | 608 | "{ind}v{v} = tl_ref_get(ctx, {r});\n{ind}tl_drop(ctx, {r});", |
| 502 | 609 | r = args[0] |
| 503 | 610 | ); |
| 504 | 611 | } |
| 505 | | − ("std/ref.st", _) => { |
| 612 | + ("std/ref.st", _, _) => { |
| 506 | 613 | let _ = writeln!( |
| 507 | 614 | out, |
| 508 | 615 | "{ind}v{v} = tl_ref_set(ctx, {r}, {x});\n{ind}tl_drop(ctx, {r});", |
| @@ -510,6 +617,19 @@ impl Emitter<'_> { |
| 510 | 617 | x = args[1] |
| 511 | 618 | ); |
| 512 | 619 | } |
| 620 | + (_, _, OpKind::FinalCtl) => { |
| 621 | + // Unwind to the handler: the payload is the |
| 622 | + // operation's index and arguments. |
| 623 | + let mut payload = vec![format!("tl_int({op})")]; |
| 624 | + payload.extend(args); |
| 625 | + let _ = writeln!(out, "{ind}{{"); |
| 626 | + let inner = format!("{ind} "); |
| 627 | + self.block(out, v, 0, &payload, &inner)?; |
| 628 | + let _ = writeln!( |
| 629 | + out, |
| 630 | + "{inner}uint64_t marker = (uint64_t)tl_int_get(tl_as_block({cap})->fields[{n_ops}]);\n{inner}v{v} = tl_yield_final(ctx, marker, v{v});\n{ind}}}" |
| 631 | + ); |
| 632 | + } |
| 513 | 633 | _ => { |
| 514 | 634 | // A `fn` operation: call the clause in the handler. |
| 515 | 635 | let clause = format!("tl_dup(tl_as_block({cap})->fields[{op}])"); |
| @@ -521,29 +641,53 @@ impl Emitter<'_> { |
| 521 | 641 | } |
| 522 | 642 | } |
| 523 | 643 | Value::Handle { |
| 524 | | − body, ret, clauses, .. |
| 644 | + body, |
| 645 | + ret, |
| 646 | + clauses, |
| 647 | + effect, |
| 525 | 648 | } => { |
| 526 | 649 | let body = self.atom(body)?; |
| 527 | 650 | let ret = ret.as_ref().map(|r| self.atom(r)).transpose()?; |
| 528 | | − let clauses = self.atoms(clauses)?; |
| 651 | + let mut fields = self.atoms(clauses)?; |
| 652 | + // The capability: the clauses, then the handler's marker |
| 653 | + // (what `final ctl` operations unwind to). |
| 654 | + fields.push("tl_int((int64_t)marker)".into()); |
| 655 | + let catches = self.program.effects[*effect as usize] |
| 656 | + .ops |
| 657 | + .iter() |
| 658 | + .any(|(_, k)| *k == OpKind::FinalCtl); |
| 529 | 659 | let _ = writeln!(out, "{ind}{{"); |
| 530 | 660 | let inner = format!("{ind} "); |
| 531 | | − // The capability: a block holding the clauses. |
| 532 | | − self.block(out, v, 0, &clauses, &inner)?; |
| 533 | | − let _ = writeln!(out, "{inner}tl_value cap = v{v};"); |
| 534 | | − let call = Self::closure_call(&body, &["cap".into()], &inner, out); |
| 661 | + let _ = writeln!(out, "{inner}uint64_t marker = tl_fresh_marker(ctx);"); |
| 662 | + self.block(out, v, 0, &fields, &inner)?; |
| 663 | + let _ = writeln!(out, "{inner}tl_value cap = tl_dup(v{v});"); |
| 664 | + let call = |
| 665 | + Self::closure_call(&body, &["v".to_owned() + &v.to_string()], &inner, out); |
| 535 | 666 | let _ = writeln!(out, "{inner}v{v} = {call};"); |
| 667 | + let _ = writeln!(out, "{inner}if (TL_YIELDING(ctx)) {{"); |
| 668 | + if catches { |
| 669 | + let _ = writeln!( |
| 670 | + out, |
| 671 | + "{inner} tl_value payload;\n{inner} if (tl_catch_final(ctx, marker, &payload)) {{\n{inner} tl_value clause = tl_dup(tl_as_block(cap)->fields[tl_int_get(tl_as_block(payload)->fields[0])]);\n{inner} v{v} = tl_call_payload(ctx, clause, payload);\n{inner} }}" |
| 672 | + ); |
| 673 | + } |
| 674 | + let _ = writeln!(out, "{inner}}} else {{"); |
| 536 | 675 | if let Some(r) = ret { |
| 537 | | − let call = Self::closure_call(&r, &[format!("v{v}")], &inner, out); |
| 538 | | − let _ = writeln!(out, "{inner}v{v} = {call};"); |
| 676 | + let deeper = format!("{inner} "); |
| 677 | + let call = Self::closure_call(&r, &[format!("v{v}")], &deeper, out); |
| 678 | + let _ = writeln!(out, "{deeper}v{v} = {call};"); |
| 679 | + } else { |
| 680 | + let _ = writeln!(out, "{inner} /* no return clause */"); |
| 539 | 681 | } |
| 540 | | − let _ = writeln!(out, "{ind}}}"); |
| 682 | + let _ = writeln!(out, "{inner}}}\n{inner}tl_drop(ctx, cap);\n{ind}}}"); |
| 683 | + let _ = held; |
| 541 | 684 | } |
| 542 | 685 | Value::Region { body, .. } => { |
| 543 | 686 | let body = self.atom(body)?; |
| 544 | 687 | let _ = writeln!(out, "{ind}{{\n{ind} tl_value region = tl_region_new(ctx);"); |
| 545 | 688 | let inner = format!("{ind} "); |
| 546 | 689 | let call = Self::closure_call(&body, &["tl_dup(region)".into()], &inner, out); |
| 690 | + // The region ends whether its body returns or unwinds. |
| 547 | 691 | let _ = writeln!( |
| 548 | 692 | out, |
| 549 | 693 | "{inner}v{v} = {call};\n{inner}tl_region_end(ctx, region);\n{inner}tl_drop(ctx, region);\n{ind}}}" |