| @@ -9,7 +9,9 @@ use std::collections::{BTreeMap, BTreeSet, HashMap}; |
| 9 | 9 | use std::fmt::Write; |
| 10 | 10 | |
| 11 | 11 | use louise_core::externs::runtime_extern; |
| 12 | | −use louise_core::ir::{Atom, Expr, FnDecl, FnId, Lit, Prim, Program, Span, Value, VarId}; |
| 12 | +use louise_core::ir::{ |
| 13 | + Atom, Expr, FnDecl, FnId, Lit, Prim, Program, Span, UnboxedKind, Value, VarId, |
| 14 | +}; |
| 13 | 15 | use louise_rt_prims::SMALL_INT; |
| 14 | 16 | use louise_span::{FileId, SourceDb}; |
| 15 | 17 | |
| @@ -54,6 +56,12 @@ pub(crate) fn emit( |
| 54 | 56 | sources: Option<&SourceDb>, |
| 55 | 57 | units: usize, |
| 56 | 58 | ) -> Result<Vec<Unit>, Unsupported> { |
| 59 | + let ranges = if std::env::var_os("LOUISE_NO_FAST_INTS").is_some() { |
| 60 | + crate::ranges::Ranges::empty() |
| 61 | + } else { |
| 62 | + crate::ranges::analyse(program, entries, reachable) |
| 63 | + }; |
| 64 | + let workers = crate::unbox::plan(program, reachable, &ranges); |
| 57 | 65 | let mut e = Emitter { |
| 58 | 66 | program, |
| 59 | 67 | sources, |
| @@ -66,14 +74,10 @@ pub(crate) fn emit( |
| 66 | 74 | never_unwinds: BTreeSet::new(), |
| 67 | 75 | cyclic: BTreeSet::new(), |
| 68 | 76 | current: 0, |
| 69 | | − ranges: if std::env::var_os("LOUISE_NO_FAST_INTS").is_some() { |
| 70 | | − crate::ranges::Ranges::empty() |
| 71 | | − } else { |
| 72 | | − crate::ranges::analyse(program, entries, reachable) |
| 73 | | − }, |
| 74 | | − float_workers: crate::float::plan(program, reachable), |
| 77 | + ranges, |
| 78 | + workers, |
| 75 | 79 | raw: BTreeSet::new(), |
| 76 | | − in_worker: false, |
| 80 | + worker: None, |
| 77 | 81 | functions: BTreeMap::new(), |
| 78 | 82 | uses: Uses::default(), |
| 79 | 83 | labels: 0, |
| @@ -266,8 +270,8 @@ fn take(held: &mut Vec<VarId>, v: VarId) { |
| 266 | 270 | |
| 267 | 271 | /// Unwinding out of this frame: the C statements that drop what it holds |
| 268 | 272 | /// (`held`) and return `ret`, a value of the function's return type |
| 269 | | −/// (`TL_UNIT`, a dummy struct for a CPR worker, `0.0` for a float worker) |
| 270 | | −/// that the caller doesn't read on a yield. (A copying pass over a |
| 273 | +/// (`TL_UNIT`, a dummy struct for a CPR worker, `0.0`/`0` for a float/int |
| 274 | +/// worker) that the caller doesn't read on a yield. (A copying pass over a |
| 271 | 275 | /// `multi ctl` continuation, ADR 0017, takes the same path and dups |
| 272 | 276 | /// instead.) |
| 273 | 277 | fn unwind(held: &[VarId], ret: &str) -> String { |
| @@ -279,10 +283,14 @@ fn unwind(held: &[VarId], ret: &str) -> String { |
| 279 | 283 | s |
| 280 | 284 | } |
| 281 | 285 | |
| 282 | | −/// What `unwind` returns from a function of this return shape. |
| 283 | | −fn unwind_ret(cpr_arity: Option<u32>, in_worker: bool) -> String { |
| 284 | | − if in_worker { |
| 285 | | − return "0.0".to_owned(); |
| 286 | +/// What `unwind` returns from a function of this return shape. A |
| 287 | +/// worker's dummy is never the int bail sentinel: yielding is not an |
| 288 | +/// overflow, and the caller's sentinel check must not fire on it. |
| 289 | +fn unwind_ret(cpr_arity: Option<u32>, worker: Option<UnboxedKind>) -> String { |
| 290 | + match worker { |
| 291 | + Some(UnboxedKind::Float) => return "0.0".to_owned(), |
| 292 | + Some(UnboxedKind::Int) => return "0".to_owned(), |
| 293 | + None => {} |
| 286 | 294 | } |
| 287 | 295 | match cpr_arity { |
| 288 | 296 | Some(n) => { |
| @@ -341,12 +349,12 @@ struct Emitter<'p> { |
| 341 | 349 | current: FnId, |
| 342 | 350 | /// Proven integer ranges (ADR 0046, piece 3; `crate::ranges`). |
| 343 | 351 | ranges: crate::ranges::Ranges, |
| 344 | | − /// The unboxed-float worker plans (ADR 0045; `crate::float`), and, |
| 345 | | − /// while a worker's body is emitted, its raw-double locals (empty |
| 346 | | − /// otherwise). |
| 347 | | − float_workers: BTreeMap<FnId, crate::float::WorkerPlan>, |
| 352 | + /// The unboxed worker plans (ADR 0045; `crate::unbox`), and, while a |
| 353 | + /// worker's body is emitted, its raw locals (empty otherwise) and |
| 354 | + /// its kind. |
| 355 | + workers: BTreeMap<FnId, crate::unbox::WorkerPlan>, |
| 348 | 356 | raw: BTreeSet<VarId>, |
| 349 | | − in_worker: bool, |
| 357 | + worker: Option<UnboxedKind>, |
| 350 | 358 | /// Block labels, numbered within each function. |
| 351 | 359 | labels: u32, |
| 352 | 360 | /// The source line of the code being emitted (none outside located |
| @@ -371,68 +379,94 @@ fn store_xargs(slots: &[String], indent: &str, out: &mut String) { |
| 371 | 379 | } |
| 372 | 380 | |
| 373 | 381 | impl Emitter<'_> { |
| 374 | | − /// A worker's C symbol: the wrapper's, `$uf`-mangled (ADR 0045). |
| 382 | + /// A worker's C symbol: the wrapper's, `$uf`/`$ui`-mangled (ADR 0045). |
| 375 | 383 | fn worker_sym(&self, id: FnId) -> String { |
| 376 | | − format!("{}_24uf", self.syms[&id]) |
| 384 | + let suffix = match self.workers[&id].kind { |
| 385 | + UnboxedKind::Float => "_24uf", |
| 386 | + UnboxedKind::Int => "_24ui", |
| 387 | + }; |
| 388 | + format!("{}{suffix}", self.syms[&id]) |
| 377 | 389 | } |
| 378 | 390 | |
| 379 | | − /// A worker's prototype: the Float parameters and the result raw |
| 391 | + /// A worker's prototype: the marked parameters and the result raw |
| 380 | 392 | /// (called only when a plan exists). |
| 381 | 393 | fn worker_sig(&self, id: FnId) -> String { |
| 382 | 394 | let decl = &self.program.fns[id as usize]; |
| 383 | | − let sig = decl.float_sig.as_ref().expect("a plan has a signature"); |
| 395 | + let plan = &self.workers[&id]; |
| 396 | + let sig = decl.unbox_sig.as_ref().expect("a plan has a signature"); |
| 397 | + let raw_ty = match plan.kind { |
| 398 | + UnboxedKind::Float => "double", |
| 399 | + UnboxedKind::Int => "int64_t", |
| 400 | + }; |
| 384 | 401 | let params: String = decl |
| 385 | 402 | .params |
| 386 | 403 | .iter() |
| 387 | 404 | .enumerate() |
| 388 | 405 | .map(|(i, _)| { |
| 389 | | − if sig[i] { |
| 390 | | − format!(", double a{i}") |
| 406 | + if sig.marked(i) { |
| 407 | + format!(", {raw_ty} a{i}") |
| 391 | 408 | } else { |
| 392 | 409 | format!(", tl_value a{i}") |
| 393 | 410 | } |
| 394 | 411 | }) |
| 395 | 412 | .collect(); |
| 396 | | − format!("TL_FN double {}(tl_ctx *ctx{params})", self.worker_sym(id)) |
| 413 | + format!( |
| 414 | + "TL_FN {raw_ty} {}(tl_ctx *ctx{params})", |
| 415 | + self.worker_sym(id) |
| 416 | + ) |
| 397 | 417 | } |
| 398 | 418 | |
| 399 | | − /// `a` as a `double` operand (worker mode): a raw local directly, a |
| 400 | | − /// float literal as a C literal, anything else unboxed in place. |
| 419 | + /// `a` as a raw operand (worker mode): a raw local directly, a |
| 420 | + /// literal of the worker's kind as a C literal, anything else unboxed |
| 421 | + /// in place — for an int, only where the walk proved it small (a |
| 422 | + /// boxed Int may be a big integer). |
| 401 | 423 | fn raw_atom(&mut self, a: &Atom) -> Result<String, Unsupported> { |
| 402 | | − Ok(match a { |
| 403 | | − Atom::Var(v) if self.raw.contains(v) => format!("v{v}"), |
| 404 | | − Atom::Lit(Lit::Float(x)) => match crate::float::c_float(*x) { |
| 405 | | − Some(s) => s, |
| 406 | | − None => { |
| 407 | | − let name = self.float_lit(*x); |
| 408 | | − format!("tl_float_get((tl_value)&{name})") |
| 409 | | − } |
| 424 | + Ok(match self.worker { |
| 425 | + Some(UnboxedKind::Int) => match a { |
| 426 | + Atom::Var(v) if self.raw.contains(v) => format!("v{v}"), |
| 427 | + Atom::Lit(Lit::Int(n)) => format!("INT64_C({n})"), |
| 428 | + a => format!("tl_int_get({})", self.atom(a)?), |
| 429 | + }, |
| 430 | + _ => match a { |
| 431 | + Atom::Var(v) if self.raw.contains(v) => format!("v{v}"), |
| 432 | + Atom::Lit(Lit::Float(x)) => match crate::unbox::c_float(*x) { |
| 433 | + Some(s) => s, |
| 434 | + None => { |
| 435 | + let name = self.float_lit(*x); |
| 436 | + format!("tl_float_get((tl_value)&{name})") |
| 437 | + } |
| 438 | + }, |
| 439 | + a => format!("tl_float_get({})", self.atom(a)?), |
| 410 | 440 | }, |
| 411 | | − a => format!("tl_float_get({})", self.atom(a)?), |
| 412 | 441 | }) |
| 413 | 442 | } |
| 414 | 443 | |
| 415 | | − /// The unboxed-float worker of `id` (ADR 0045), if planned: its text |
| 416 | | − /// after the wrapper's. |
| 444 | + /// The unboxed worker of `id` (ADR 0045), if planned: its text after |
| 445 | + /// the wrapper's. |
| 417 | 446 | fn emit_worker(&mut self, id: FnId, decl: &FnDecl) -> Result<Option<String>, Unsupported> { |
| 418 | | − let Some(plan) = self.float_workers.get(&id) else { |
| 447 | + let Some(plan) = self.workers.get(&id) else { |
| 419 | 448 | return Ok(None); |
| 420 | 449 | }; |
| 421 | 450 | let raw = plan.raw.clone(); |
| 422 | | − let sig = decl.float_sig.clone().expect("a plan has a signature"); |
| 451 | + let kind = plan.kind; |
| 452 | + let sig = decl.unbox_sig.clone().expect("a plan has a signature"); |
| 453 | + let (raw_ty, raw_zero, tag) = match kind { |
| 454 | + UnboxedKind::Float => ("double", "0.0", "uf"), |
| 455 | + UnboxedKind::Int => ("int64_t", "0", "ui"), |
| 456 | + }; |
| 423 | 457 | let mut out = String::new(); |
| 424 | | − let _ = writeln!(out, "/* {}$uf */", decl.name); |
| 458 | + let _ = writeln!(out, "/* {}${tag} */", decl.name); |
| 425 | 459 | self.line = decl.span.and_then(|s| self.line_of(s)); |
| 426 | 460 | self.written = None; |
| 427 | 461 | self.sync_line(&mut out); |
| 428 | 462 | self.current = id; |
| 429 | 463 | self.raw = raw; |
| 430 | | − self.in_worker = true; |
| 464 | + self.worker = Some(kind); |
| 431 | 465 | let _ = writeln!(out, "{} {{", self.worker_sig(id)); |
| 432 | 466 | let params_set: BTreeSet<VarId> = decl.params.iter().copied().collect(); |
| 433 | 467 | for (i, p) in decl.params.iter().enumerate() { |
| 434 | | − if sig[i] { |
| 435 | | − let _ = writeln!(out, " double v{p} = a{i};"); |
| 468 | + if sig.marked(i) { |
| 469 | + let _ = writeln!(out, " {raw_ty} v{p} = a{i};"); |
| 436 | 470 | } else { |
| 437 | 471 | let _ = writeln!(out, " tl_value v{p} = a{i};"); |
| 438 | 472 | } |
| @@ -440,7 +474,7 @@ impl Emitter<'_> { |
| 440 | 474 | let (mut raw_locals, mut tagged_locals) = (Vec::new(), Vec::new()); |
| 441 | 475 | for v in (0..decl.n_vars).filter(|v| !params_set.contains(v)) { |
| 442 | 476 | if self.raw.contains(&v) { |
| 443 | | − raw_locals.push(format!("v{v} = 0.0")); |
| 477 | + raw_locals.push(format!("v{v} = {raw_zero}")); |
| 444 | 478 | } else { |
| 445 | 479 | tagged_locals.push(format!("v{v} = TL_UNIT")); |
| 446 | 480 | } |
| @@ -449,16 +483,16 @@ impl Emitter<'_> { |
| 449 | 483 | let _ = writeln!(out, " tl_value {};", chunk.join(", ")); |
| 450 | 484 | } |
| 451 | 485 | for chunk in raw_locals.chunks(8) { |
| 452 | | − let _ = writeln!(out, " double {};", chunk.join(", ")); |
| 486 | + let _ = writeln!(out, " {raw_ty} {};", chunk.join(", ")); |
| 453 | 487 | } |
| 454 | 488 | // What the frame holds on entry: the tagged parameters the caller |
| 455 | | − // owns (a double is not a reference, so it is neither dupped nor |
| 456 | | − // dropped). |
| 489 | + // owns (a raw number is not a reference, so it is neither dupped |
| 490 | + // nor dropped). |
| 457 | 491 | let owned: Vec<VarId> = decl |
| 458 | 492 | .params |
| 459 | 493 | .iter() |
| 460 | 494 | .enumerate() |
| 461 | | − .filter(|&(i, _)| !sig[i] && !decl.borrowed.contains(&i)) |
| 495 | + .filter(|&(i, _)| !sig.marked(i) && !decl.borrowed.contains(&i)) |
| 462 | 496 | .map(|(_, p)| *p) |
| 463 | 497 | .collect(); |
| 464 | 498 | // The worker of a recursive function keeps the preemption point: |
| @@ -467,13 +501,13 @@ impl Emitter<'_> { |
| 467 | 501 | let _ = writeln!( |
| 468 | 502 | out, |
| 469 | 503 | "entry:\n if (tl_unlikely(--ctx->reductions <= 0)) {{\n tl_preempt(ctx);\n if (TL_YIELDING(ctx)) {}\n }}", |
| 470 | | − unwind(&owned, "0.0") |
| 504 | + unwind(&owned, raw_zero) |
| 471 | 505 | ); |
| 472 | 506 | } |
| 473 | 507 | let func = Func { id, decl }; |
| 474 | 508 | self.expr(&mut out, &func, &decl.body, Dest::Return, owned, 1)?; |
| 475 | 509 | out.push_str("}\n\n"); |
| 476 | | − self.in_worker = false; |
| 510 | + self.worker = None; |
| 477 | 511 | self.raw.clear(); |
| 478 | 512 | let located = self.line.is_some(); |
| 479 | 513 | let text = if located { pin_lines(&out) } else { out }; |
| @@ -540,7 +574,7 @@ impl Emitter<'_> { |
| 540 | 574 | let _ = writeln!( |
| 541 | 575 | out, |
| 542 | 576 | "entry:\n if (tl_unlikely(--ctx->reductions <= 0)) {{\n tl_preempt(ctx);\n if (TL_YIELDING(ctx)) {}\n }}", |
| 543 | | − unwind(&owned, &unwind_ret(decl.cpr_arity, false)) |
| 577 | + unwind(&owned, &unwind_ret(decl.cpr_arity, None)) |
| 544 | 578 | ); |
| 545 | 579 | } |
| 546 | 580 | let func = Func { id, decl }; |
| @@ -700,8 +734,8 @@ int main(int argc, char **argv) {{ |
| 700 | 734 | "TL_FN {ret} {}(tl_ctx *ctx, tl_value a0, tl_value a1, tl_value a2, tl_value a3, tl_value a4, tl_value a5);", |
| 701 | 735 | self.syms[f] |
| 702 | 736 | ); |
| 703 | | − // Its unboxed-float worker, when it has one (ADR 0045). |
| 704 | | − if self.float_workers.contains_key(f) { |
| 737 | + // Its unboxed worker, when it has one (ADR 0045). |
| 738 | + if self.workers.contains_key(f) { |
| 705 | 739 | let _ = writeln!(out, "{};", self.worker_sig(*f)); |
| 706 | 740 | } |
| 707 | 741 | } |
| @@ -814,9 +848,10 @@ int main(int argc, char **argv) {{ |
| 814 | 848 | Ok(match a { |
| 815 | 849 | // A raw local escaping into a tagged position is boxed at the |
| 816 | 850 | // boundary (ADR 0045). |
| 817 | | − Atom::Var(v) if self.in_worker && self.raw.contains(v) => { |
| 818 | | − format!("tl_float_from(ctx, v{v})") |
| 819 | | − } |
| 851 | + Atom::Var(v) if self.worker.is_some() && self.raw.contains(v) => match self.worker { |
| 852 | + Some(UnboxedKind::Int) => format!("tl_int(v{v})"), |
| 853 | + _ => format!("tl_float_from(ctx, v{v})"), |
| 854 | + }, |
| 820 | 855 | Atom::Var(v) => format!("v{v}"), |
| 821 | 856 | Atom::Unit => "TL_UNIT".into(), |
| 822 | 857 | Atom::Lit(Lit::Int(n)) => { |
| @@ -969,7 +1004,7 @@ int main(int argc, char **argv) {{ |
| 969 | 1004 | out, |
| 970 | 1005 | "{ind}tl_panic_str(ctx, \"{}\");\n{ind}{}", |
| 971 | 1006 | c_escape(msg), |
| 972 | | − unwind(&held, &unwind_ret(f.decl.cpr_arity, self.in_worker)) |
| 1007 | + unwind(&held, &unwind_ret(f.decl.cpr_arity, self.worker)) |
| 973 | 1008 | ); |
| 974 | 1009 | return Ok(None); |
| 975 | 1010 | } |
| @@ -1067,12 +1102,12 @@ int main(int argc, char **argv) {{ |
| 1067 | 1102 | for o in owned { |
| 1068 | 1103 | take(held, o); |
| 1069 | 1104 | } |
| 1070 | | − self.value(out, v, value, &ind)?; |
| 1105 | + self.value(out, v, value, held, &ind)?; |
| 1071 | 1106 | if self.may_unwind(value) { |
| 1072 | 1107 | let _ = writeln!( |
| 1073 | 1108 | out, |
| 1074 | 1109 | "{ind}if (TL_YIELDING(ctx)) {}", |
| 1075 | | − unwind(held, &unwind_ret(f.decl.cpr_arity, self.in_worker)) |
| 1110 | + unwind(held, &unwind_ret(f.decl.cpr_arity, self.worker)) |
| 1076 | 1111 | ); |
| 1077 | 1112 | } |
| 1078 | 1113 | // A projection is borrowed (a `dup` follows if it is |
| @@ -1098,7 +1133,7 @@ int main(int argc, char **argv) {{ |
| 1098 | 1133 | } |
| 1099 | 1134 | match dest { |
| 1100 | 1135 | Dest::Return => { |
| 1101 | | − if self.in_worker { |
| 1136 | + if self.worker.is_some() { |
| 1102 | 1137 | let a = self.raw_atom(a)?; |
| 1103 | 1138 | let _ = writeln!(out, "{ind}return {a};"); |
| 1104 | 1139 | } else { |
| @@ -1110,7 +1145,7 @@ int main(int argc, char **argv) {{ |
| 1110 | 1145 | Dest::Assign(v, label) => { |
| 1111 | 1146 | // A block's value: raw or boxed as the |
| 1112 | 1147 | // destination's local is. |
| 1113 | | − let s = if self.in_worker && self.raw.contains(&v) { |
| 1148 | + let s = if self.worker.is_some() && self.raw.contains(&v) { |
| 1114 | 1149 | self.raw_atom(a)? |
| 1115 | 1150 | } else { |
| 1116 | 1151 | self.atom(a)? |
| @@ -1203,7 +1238,7 @@ int main(int argc, char **argv) {{ |
| 1203 | 1238 | let _ = writeln!( |
| 1204 | 1239 | out, |
| 1205 | 1240 | "{ind}if (TL_YIELDING(ctx)) {}", |
| 1206 | | − unwind(held, &unwind_ret(f.decl.cpr_arity, self.in_worker)) |
| 1241 | + unwind(held, &unwind_ret(f.decl.cpr_arity, self.worker)) |
| 1207 | 1242 | ); |
| 1208 | 1243 | } |
| 1209 | 1244 | for (i, v) in vars.iter().enumerate() { |
| @@ -1305,8 +1340,8 @@ int main(int argc, char **argv) {{ |
| 1305 | 1340 | /// A self tail call of `f` with `args`: a jump to its entry, the |
| 1306 | 1341 | /// arguments evaluated into temporaries before any parameter is |
| 1307 | 1342 | /// overwritten. Perceus consumed or dropped everything else before it. |
| 1308 | | − /// In a worker, the Float slots stay raw doubles (a CPR worker, whose |
| 1309 | | − /// `let (…)` self-calls come here too, never has a float worker). |
| 1343 | + /// In a worker, the marked slots stay raw (a CPR worker, whose |
| 1344 | + /// `let (…)` self-calls come here too, never has an unboxed worker). |
| 1310 | 1345 | fn self_tail_call( |
| 1311 | 1346 | &mut self, |
| 1312 | 1347 | out: &mut String, |
| @@ -1316,14 +1351,13 @@ int main(int argc, char **argv) {{ |
| 1316 | 1351 | ) -> Result<(), Unsupported> { |
| 1317 | 1352 | let _ = writeln!(out, "{ind}{{"); |
| 1318 | 1353 | for (i, a) in args.iter().enumerate() { |
| 1319 | | − if self.in_worker |
| 1320 | | − && f.decl |
| 1321 | | − .float_sig |
| 1322 | | − .as_ref() |
| 1323 | | − .is_some_and(|s| s.get(i).copied().unwrap_or(false)) |
| 1324 | | − { |
| 1354 | + if self.worker.is_some() && f.decl.unbox_sig.as_ref().is_some_and(|s| s.marked(i)) { |
| 1355 | + let raw_ty = match self.worker { |
| 1356 | + Some(UnboxedKind::Int) => "int64_t", |
| 1357 | + _ => "double", |
| 1358 | + }; |
| 1325 | 1359 | let a = self.raw_atom(a)?; |
| 1326 | | − let _ = writeln!(out, "{ind} double t{i} = {a};"); |
| 1360 | + let _ = writeln!(out, "{ind} {raw_ty} t{i} = {a};"); |
| 1327 | 1361 | } else { |
| 1328 | 1362 | let a = self.atom(a)?; |
| 1329 | 1363 | let _ = writeln!(out, "{ind} tl_value t{i} = {a};"); |
| @@ -1415,19 +1449,20 @@ int main(int argc, char **argv) {{ |
| 1415 | 1449 | } |
| 1416 | 1450 | |
| 1417 | 1451 | /// `v = value;` (not a block). Its unwinding path, if any, follows it |
| 1418 | | − /// (`expr`). |
| 1452 | + /// (`expr`). `held` is what the frame owns past the value (an int |
| 1453 | + /// worker's bail drops it before returning the sentinel). |
| 1419 | 1454 | fn value( |
| 1420 | 1455 | &mut self, |
| 1421 | 1456 | out: &mut String, |
| 1422 | 1457 | v: VarId, |
| 1423 | 1458 | value: &Value, |
| 1459 | + held: &[VarId], |
| 1424 | 1460 | ind: &str, |
| 1425 | 1461 | ) -> Result<(), Unsupported> { |
| 1426 | 1462 | match value { |
| 1427 | 1463 | Value::Atom(a) => { |
| 1428 | | − let s = if self.in_worker && self.raw.contains(&v) { |
| 1429 | | − // The target is a raw double: the literal or the |
| 1430 | | − // unboxed local. |
| 1464 | + let s = if self.worker.is_some() && self.raw.contains(&v) { |
| 1465 | + // The target is raw: the literal or the unboxed local. |
| 1431 | 1466 | self.raw_atom(a)? |
| 1432 | 1467 | } else { |
| 1433 | 1468 | self.atom(a)? |
| @@ -1438,16 +1473,17 @@ int main(int argc, char **argv) {{ |
| 1438 | 1473 | self.uses.fns.insert(*g); |
| 1439 | 1474 | let _ = writeln!(out, "{ind}v{v} = {}_k;", self.syms[g]); |
| 1440 | 1475 | } |
| 1441 | | − Value::CallFn(g, args) if self.in_worker && *g == self.current => { |
| 1442 | | − // The worker's own (non-tail) call: raw doubles in, raw |
| 1443 | | − // out. |
| 1476 | + Value::CallFn(g, args) if self.worker.is_some() && *g == self.current => { |
| 1477 | + // The worker's own (non-tail) call: raw in, raw out. An |
| 1478 | + // int worker's call can bail: the sentinel propagates |
| 1479 | + // (the frame's held references dropped first). |
| 1444 | 1480 | let sig = self.program.fns[self.current as usize] |
| 1445 | | − .float_sig |
| 1481 | + .unbox_sig |
| 1446 | 1482 | .clone() |
| 1447 | 1483 | .expect("a worker has a signature"); |
| 1448 | 1484 | let mut call = vec!["ctx".to_owned()]; |
| 1449 | 1485 | for (i, a) in args.iter().enumerate() { |
| 1450 | | − call.push(if sig.get(i).copied().unwrap_or(false) { |
| 1486 | + call.push(if sig.marked(i) { |
| 1451 | 1487 | self.raw_atom(a)? |
| 1452 | 1488 | } else { |
| 1453 | 1489 | self.atom(a)? |
| @@ -1456,9 +1492,16 @@ int main(int argc, char **argv) {{ |
| 1456 | 1492 | let wsym = self.worker_sym(self.current); |
| 1457 | 1493 | self.uses.fns.insert(*g); |
| 1458 | 1494 | let _ = writeln!(out, "{ind}v{v} = {wsym}({});", call.join(", ")); |
| 1495 | + if self.worker == Some(UnboxedKind::Int) { |
| 1496 | + let _ = writeln!( |
| 1497 | + out, |
| 1498 | + "{ind}if (tl_unlikely(v{v} == TL_INT_BAIL)) {}", |
| 1499 | + unwind(held, "TL_INT_BAIL") |
| 1500 | + ); |
| 1501 | + } |
| 1459 | 1502 | } |
| 1460 | | − Value::CallFn(g, args) if self.float_workers.contains_key(g) => { |
| 1461 | | − self.call_float_worker(out, v, *g, args, ind)?; |
| 1503 | + Value::CallFn(g, args) if self.workers.contains_key(g) => { |
| 1504 | + self.call_worker(out, v, *g, args, ind)?; |
| 1462 | 1505 | } |
| 1463 | 1506 | Value::CallFn(g, args) => { |
| 1464 | 1507 | let slots = self.atoms(args)?; |
| @@ -1478,7 +1521,8 @@ int main(int argc, char **argv) {{ |
| 1478 | 1521 | let _ = writeln!(out, "{ind}v{v} = {call};"); |
| 1479 | 1522 | } |
| 1480 | 1523 | Value::CallExtern(sym, args) |
| 1481 | | − if self.in_worker && crate::float::FLOAT_ARITH.contains(&sym.as_str()) => |
| 1524 | + if self.worker == Some(UnboxedKind::Float) |
| 1525 | + && crate::unbox::FLOAT_ARITH.contains(&sym.as_str()) => |
| 1482 | 1526 | { |
| 1483 | 1527 | // The raw op; the result is a raw double local. |
| 1484 | 1528 | let a = self.raw_atom(&args[0])?; |
| @@ -1491,12 +1535,16 @@ int main(int argc, char **argv) {{ |
| 1491 | 1535 | }; |
| 1492 | 1536 | let _ = writeln!(out, "{ind}v{v} = {a} {op} {b};"); |
| 1493 | 1537 | } |
| 1494 | | − Value::CallExtern(sym, args) if self.in_worker && sym == "tl_float_eq" => { |
| 1538 | + Value::CallExtern(sym, args) |
| 1539 | + if self.worker == Some(UnboxedKind::Float) && sym == "tl_float_eq" => |
| 1540 | + { |
| 1495 | 1541 | let a = self.raw_atom(&args[0])?; |
| 1496 | 1542 | let b = self.raw_atom(&args[1])?; |
| 1497 | 1543 | let _ = writeln!(out, "{ind}v{v} = ({a}) == ({b}) ? TL_TRUE : TL_FALSE;"); |
| 1498 | 1544 | } |
| 1499 | | − Value::CallExtern(sym, args) if self.in_worker && sym == "tl_float_compare" => { |
| 1545 | + Value::CallExtern(sym, args) |
| 1546 | + if self.worker == Some(UnboxedKind::Float) && sym == "tl_float_compare" => |
| 1547 | + { |
| 1500 | 1548 | let a = self.raw_atom(&args[0])?; |
| 1501 | 1549 | let b = self.raw_atom(&args[1])?; |
| 1502 | 1550 | let _ = writeln!( |
| @@ -1504,6 +1552,28 @@ int main(int argc, char **argv) {{ |
| 1504 | 1552 | "{ind}v{v} = tl_int((({a}) > ({b})) - (({a}) < ({b})) + 1);" |
| 1505 | 1553 | ); |
| 1506 | 1554 | } |
| 1555 | + Value::CallExtern(sym, args) |
| 1556 | + if self.worker == Some(UnboxedKind::Int) |
| 1557 | + && crate::unbox::INT_ARITH.contains(&sym.as_str()) => |
| 1558 | + { |
| 1559 | + self.int_arith(out, v, sym, args, held, ind)?; |
| 1560 | + } |
| 1561 | + Value::CallExtern(sym, args) |
| 1562 | + if self.worker == Some(UnboxedKind::Int) |
| 1563 | + && crate::unbox::INT_COMPARE.contains(&sym.as_str()) => |
| 1564 | + { |
| 1565 | + // The raw comparison; the result is a tagged local. |
| 1566 | + let a = self.raw_atom(&args[0])?; |
| 1567 | + let b = self.raw_atom(&args[1])?; |
| 1568 | + if sym == "tl_int_eq" { |
| 1569 | + let _ = writeln!(out, "{ind}v{v} = ({a}) == ({b}) ? TL_TRUE : TL_FALSE;"); |
| 1570 | + } else { |
| 1571 | + let _ = writeln!( |
| 1572 | + out, |
| 1573 | + "{ind}v{v} = tl_int((({a}) > ({b})) - (({a}) < ({b})) + 1);" |
| 1574 | + ); |
| 1575 | + } |
| 1576 | + } |
| 1507 | 1577 | Value::CallExtern(sym, args) => self.call_extern(out, v, sym, args, ind)?, |
| 1508 | 1578 | Value::Prim(p, args) => { |
| 1509 | 1579 | let a = self.atom(&args[0])?; |
| @@ -1580,12 +1650,16 @@ int main(int argc, char **argv) {{ |
| 1580 | 1650 | Ok(()) |
| 1581 | 1651 | } |
| 1582 | 1652 | |
| 1583 | | − /// `v = g(args)` through `g`'s unboxed-float worker: the worker's |
| 1584 | | − /// boundary (ADR 0045). The Float arguments are unboxed and the |
| 1585 | | − /// result boxed. A box the callee owned is read and freed before the |
| 1586 | | − /// call (the worker takes a raw double); a borrowed one is read in |
| 1587 | | − /// place. |
| 1588 | | − fn call_float_worker( |
| 1653 | + /// `v = g(args)` through `g`'s unboxed worker: the worker's boundary |
| 1654 | + /// (ADR 0045). The marked arguments are unboxed and the result boxed. |
| 1655 | + /// A box the callee owned is read and freed before the call (the |
| 1656 | + /// worker takes a raw double); a borrowed one is read in place. An |
| 1657 | + /// int worker's arguments are immediates (nothing to free), but the |
| 1658 | + /// worker runs only when every marked argument is a small int |
| 1659 | + /// (`tl_is_int`, or proven small by the range analysis) — a big |
| 1660 | + /// integer takes the boxed call — and its `TL_INT_BAIL` recomputes |
| 1661 | + /// the whole call with the boxed wrapper (Revision 2). |
| 1662 | + fn call_worker( |
| 1589 | 1663 | &mut self, |
| 1590 | 1664 | out: &mut String, |
| 1591 | 1665 | v: VarId, |
| @@ -1594,19 +1668,22 @@ int main(int argc, char **argv) {{ |
| 1594 | 1668 | ind: &str, |
| 1595 | 1669 | ) -> Result<(), Unsupported> { |
| 1596 | 1670 | let decl = &self.program.fns[g as usize]; |
| 1597 | | − let sig = decl.float_sig.clone().expect("a plan has a signature"); |
| 1671 | + let sig = decl.unbox_sig.clone().expect("a plan has a signature"); |
| 1672 | + if sig.kind == UnboxedKind::Int { |
| 1673 | + return self.call_int_worker(out, v, g, args, &sig, ind); |
| 1674 | + } |
| 1598 | 1675 | let borrowed = decl.borrowed.clone(); |
| 1599 | 1676 | let wsym = self.worker_sym(g); |
| 1600 | 1677 | let mut call = vec!["ctx".to_owned()]; |
| 1601 | 1678 | let mut drops = String::new(); |
| 1602 | 1679 | for (i, a) in args.iter().enumerate() { |
| 1603 | | − if !sig.get(i).copied().unwrap_or(false) { |
| 1680 | + if !sig.marked(i) { |
| 1604 | 1681 | call.push(self.atom(a)?); |
| 1605 | 1682 | continue; |
| 1606 | 1683 | } |
| 1607 | 1684 | match a { |
| 1608 | 1685 | Atom::Lit(Lit::Float(x)) => { |
| 1609 | | − call.push(match crate::float::c_float(*x) { |
| 1686 | + call.push(match crate::unbox::c_float(*x) { |
| 1610 | 1687 | Some(s) => s, |
| 1611 | 1688 | None => format!("tl_float_get({})", self.atom(a)?), |
| 1612 | 1689 | }); |
| @@ -1634,6 +1711,145 @@ int main(int argc, char **argv) {{ |
| 1634 | 1711 | Ok(()) |
| 1635 | 1712 | } |
| 1636 | 1713 | |
| 1714 | + /// The int worker's boundary (ADR 0045, Revision 2): the worker runs |
| 1715 | + /// only on small-int arguments; its `TL_INT_BAIL` (or a big-integer |
| 1716 | + /// argument) recomputes the call with the boxed wrapper — which the |
| 1717 | + /// caller's own yield check covers, as for any call. |
| 1718 | + fn call_int_worker( |
| 1719 | + &mut self, |
| 1720 | + out: &mut String, |
| 1721 | + v: VarId, |
| 1722 | + g: FnId, |
| 1723 | + args: &[Atom], |
| 1724 | + sig: &louise_core::ir::UnboxedSig, |
| 1725 | + ind: &str, |
| 1726 | + ) -> Result<(), Unsupported> { |
| 1727 | + let wsym = self.worker_sym(g); |
| 1728 | + let mut call = vec!["ctx".to_owned()]; |
| 1729 | + let mut guards: Vec<String> = Vec::new(); |
| 1730 | + for (i, a) in args.iter().enumerate() { |
| 1731 | + if !sig.marked(i) { |
| 1732 | + call.push(self.atom(a)?); |
| 1733 | + continue; |
| 1734 | + } |
| 1735 | + match a { |
| 1736 | + Atom::Lit(Lit::Int(n)) => call.push(format!("INT64_C({n})")), |
| 1737 | + // A raw local of the caller (itself a worker): already a |
| 1738 | + // small raw int, no tag round-trip. |
| 1739 | + Atom::Var(av) if self.raw.contains(av) => call.push(format!("v{av}")), |
| 1740 | + a if self.ranges.proven_small(self.current, a) => { |
| 1741 | + call.push(format!("tl_int_get({})", self.atom(a)?)); |
| 1742 | + } |
| 1743 | + a => { |
| 1744 | + let s = self.atom(a)?; |
| 1745 | + guards.push(format!("tl_is_int({s})")); |
| 1746 | + call.push(format!("tl_int_get({s})")); |
| 1747 | + } |
| 1748 | + } |
| 1749 | + } |
| 1750 | + let _ = writeln!(out, "{ind}{{\n{ind} int64_t r{v} = TL_INT_BAIL;"); |
| 1751 | + if guards.is_empty() { |
| 1752 | + let _ = writeln!(out, "{ind} r{v} = {wsym}({});", call.join(", ")); |
| 1753 | + } else { |
| 1754 | + let _ = writeln!( |
| 1755 | + out, |
| 1756 | + "{ind} if (tl_likely({})) r{v} = {wsym}({});", |
| 1757 | + guards.join(" && "), |
| 1758 | + call.join(", ") |
| 1759 | + ); |
| 1760 | + } |
| 1761 | + // The recompute: the boxed wrapper, with the arguments tagged. |
| 1762 | + let slots = self.atoms(args)?; |
| 1763 | + let mut recompute = String::new(); |
| 1764 | + store_xargs(&slots, &format!("{ind} "), &mut recompute); |
| 1765 | + let _ = writeln!( |
| 1766 | + out, |
| 1767 | + "{ind} if (tl_unlikely(r{v} == TL_INT_BAIL)) {{\n{recompute}{ind} v{v} = {}(ctx, {});\n{ind} }} else v{v} = tl_int(r{v});\n{ind}}}", |
| 1768 | + self.syms[&g], |
| 1769 | + register_args(&slots) |
| 1770 | + ); |
| 1771 | + self.uses.fns.insert(g); |
| 1772 | + Ok(()) |
| 1773 | + } |
| 1774 | + |
| 1775 | + /// `v = sym(a, b)` raw in an int worker (ADR 0045, Revision 2): the |
| 1776 | + /// operation on raw `int64_t`s, bailing to the boxed wrapper (the |
| 1777 | + /// whole call is recomputed, dropping what the frame holds) where |
| 1778 | + /// the result would leave the small-int range — or, for division and |
| 1779 | + /// remainder, where the boxed path would panic or promote. Operands |
| 1780 | + /// the range analysis proves small enough skip the check: it cannot |
| 1781 | + /// fire (`Ranges::unchecked`); the bitwise operations cannot |
| 1782 | + /// overflow. The walker guaranteed every operand raw or proven small. |
| 1783 | + fn int_arith( |
| 1784 | + &mut self, |
| 1785 | + out: &mut String, |
| 1786 | + v: VarId, |
| 1787 | + sym: &str, |
| 1788 | + args: &[Atom], |
| 1789 | + held: &[VarId], |
| 1790 | + ind: &str, |
| 1791 | + ) -> Result<(), Unsupported> { |
| 1792 | + let a = self.raw_atom(&args[0])?; |
| 1793 | + let b = args |
| 1794 | + .get(1) |
| 1795 | + .map(|b| self.raw_atom(b)) |
| 1796 | + .transpose()? |
| 1797 | + .unwrap_or_default(); |
| 1798 | + let bail = unwind(held, "TL_INT_BAIL"); |
| 1799 | + match sym { |
| 1800 | + "tl_int_add" | "tl_int_sub" | "tl_int_mul" => { |
| 1801 | + if self.ranges.unchecked(self.current, sym, args).is_some() { |
| 1802 | + let op = match sym { |
| 1803 | + "tl_int_add" => "+", |
| 1804 | + "tl_int_sub" => "-", |
| 1805 | + _ => "*", |
| 1806 | + }; |
| 1807 | + let _ = writeln!(out, "{ind}v{v} = {a} {op} {b};"); |
| 1808 | + } else { |
| 1809 | + let builtin = match sym { |
| 1810 | + "tl_int_add" => "__builtin_add_overflow", |
| 1811 | + "tl_int_sub" => "__builtin_sub_overflow", |
| 1812 | + _ => "__builtin_mul_overflow", |
| 1813 | + }; |
| 1814 | + let _ = writeln!( |
| 1815 | + out, |
| 1816 | + "{ind}if (tl_unlikely({builtin}({a}, {b}, &v{v}) || !TL_SMALL_FITS(v{v}))) {bail}" |
| 1817 | + ); |
| 1818 | + } |
| 1819 | + } |
| 1820 | + // Truncating division; only a zero divisor (a panic) and |
| 1821 | + // -2^62 / -1 (a promotion) leave the raw path. |
| 1822 | + "tl_int_div" | "tl_int_rem" => { |
| 1823 | + let op = if sym == "tl_int_div" { "/" } else { "%" }; |
| 1824 | + let zero = if sym == "tl_int_div" { |
| 1825 | + format!("d{v} == 0 || (d{v} == -1 && {a} == TL_SMALL_MIN)") |
| 1826 | + } else { |
| 1827 | + format!("d{v} == 0") |
| 1828 | + }; |
| 1829 | + let _ = writeln!( |
| 1830 | + out, |
| 1831 | + "{ind}{{\n{ind} int64_t d{v} = {b};\n{ind} if (tl_unlikely({zero})) {bail}\n{ind} v{v} = {a} {op} d{v};\n{ind}}}" |
| 1832 | + ); |
| 1833 | + } |
| 1834 | + // Two's complement on sign-extended 63-bit values stays in |
| 1835 | + // range: no check. |
| 1836 | + "tl_int_and" => { |
| 1837 | + let _ = writeln!(out, "{ind}v{v} = {a} & {b};"); |
| 1838 | + } |
| 1839 | + "tl_int_or" => { |
| 1840 | + let _ = writeln!(out, "{ind}v{v} = {a} | {b};"); |
| 1841 | + } |
| 1842 | + "tl_int_xor" => { |
| 1843 | + let _ = writeln!(out, "{ind}v{v} = {a} ^ {b};"); |
| 1844 | + } |
| 1845 | + "tl_int_not" => { |
| 1846 | + let _ = writeln!(out, "{ind}v{v} = ~{a};"); |
| 1847 | + } |
| 1848 | + _ => unreachable!("INT_ARITH holds {sym}"), |
| 1849 | + } |
| 1850 | + Ok(()) |
| 1851 | + } |
| 1852 | + |
| 1637 | 1853 | /// `v = sym(args)`, a foreign call: on the blocking pool for a |
| 1638 | 1854 | /// `blocking` extern, without its checks for a range-proven int op. |
| 1639 | 1855 | fn call_extern( |