| @@ -131,11 +131,18 @@ pub(crate) fn emit( |
| 131 | 131 | entries: BTreeSet::new(), |
| 132 | 132 | externs: BTreeMap::new(), |
| 133 | 133 | thunks: String::new(), |
| 134 | + constants: BTreeSet::new(), |
| 135 | + constant_defs: String::new(), |
| 134 | 136 | body: String::new(), |
| 135 | 137 | labels: 0, |
| 136 | 138 | }; |
| 137 | 139 | for f in reachable { |
| 138 | | − e.function(*f, &program.fns[*f as usize])?; |
| 140 | + e.constant(*f, &program.fns[*f as usize]); |
| 141 | + } |
| 142 | + for f in reachable { |
| 143 | + if !e.constants.contains(f) { |
| 144 | + e.function(*f, &program.fns[*f as usize])?; |
| 145 | + } |
| 139 | 146 | } |
| 140 | 147 | let mut out = String::from(PRELUDE); |
| 141 | 148 | for (sym, arity) in &e.externs { |
| @@ -145,7 +152,7 @@ pub(crate) fn emit( |
| 145 | 152 | out.push_str(&e.thunks); |
| 146 | 153 | out.push_str(&e.lits); |
| 147 | 154 | out.push('\n'); |
| 148 | | − for f in reachable { |
| 155 | + for f in reachable.difference(&e.constants) { |
| 149 | 156 | let _ = writeln!( |
| 150 | 157 | out, |
| 151 | 158 | "static tl_value f{f}(tl_ctx *ctx, tl_value a0, tl_value a1, tl_value a2, tl_value a3, tl_value a4, tl_value a5);" |
| @@ -158,6 +165,7 @@ pub(crate) fn emit( |
| 158 | 165 | ); |
| 159 | 166 | } |
| 160 | 167 | out.push('\n'); |
| 168 | + out.push_str(&e.constant_defs); |
| 161 | 169 | out.push_str(&e.body); |
| 162 | 170 | for (f, n) in e.entries.clone() { |
| 163 | 171 | e.closure_entry(&mut out, f, n); |
| @@ -241,6 +249,9 @@ struct Emitter<'p> { |
| 241 | 249 | externs: BTreeMap<String, usize>, |
| 242 | 250 | /// Thunks for `blocking` externs. |
| 243 | 251 | thunks: String, |
| 252 | + /// Functions emitted as a static, immortal value (see `constant`). |
| 253 | + constants: BTreeSet<FnId>, |
| 254 | + constant_defs: String, |
| 244 | 255 | body: String, |
| 245 | 256 | labels: u32, |
| 246 | 257 | } |
| @@ -313,6 +324,66 @@ impl Emitter<'_> { |
| 313 | 324 | Ok(()) |
| 314 | 325 | } |
| 315 | 326 | |
| 327 | + /// Record `decl` as a constant if it has no parameters and only builds |
| 328 | + /// closures without captures and tuples of them: a trait dictionary |
| 329 | + /// without constraints, such as `Num<Int>`. It is emitted as static, |
| 330 | + /// immortal blocks (count 0, like literals), so a call to it costs no |
| 331 | + /// allocation and its result needs no counting. |
| 332 | + fn constant(&mut self, id: FnId, decl: &FnDecl) { |
| 333 | + if !decl.params.is_empty() { |
| 334 | + return; |
| 335 | + } |
| 336 | + let mut defs = String::new(); |
| 337 | + let mut entries = Vec::new(); |
| 338 | + let mut e = &decl.body; |
| 339 | + loop { |
| 340 | + match e { |
| 341 | + Expr::Let(v, value, rest) => { |
| 342 | + let name = format!("tl_const{id}_{v}"); |
| 343 | + match &**value { |
| 344 | + Value::Closure(f, caps) if caps.is_empty() => { |
| 345 | + entries.push(*f); |
| 346 | + let _ = writeln!( |
| 347 | + defs, |
| 348 | + "static const struct {{ tl_header h; tl_closure_fn code; }} __attribute__((aligned(8))) {name} = {{ {{0, TL_TAG_CLOSURE, 0, 0}}, (tl_closure_fn)f{f}_c0 }};" |
| 349 | + ); |
| 350 | + } |
| 351 | + Value::Tuple(args) if !args.is_empty() && args.len() <= 254 => { |
| 352 | + let mut fields = Vec::new(); |
| 353 | + for a in args { |
| 354 | + let Atom::Var(x) = a else { return }; |
| 355 | + fields.push(format!("(tl_value)&tl_const{id}_{x}")); |
| 356 | + } |
| 357 | + let _ = writeln!( |
| 358 | + defs, |
| 359 | + "static const struct {{ tl_header h; tl_value fields[{n}]; }} __attribute__((aligned(8))) {name} = {{ {{0, 0, {n}, 0}}, {{ {} }} }};", |
| 360 | + fields.join(", "), |
| 361 | + n = args.len() |
| 362 | + ); |
| 363 | + } |
| 364 | + _ => return, |
| 365 | + } |
| 366 | + e = rest; |
| 367 | + } |
| 368 | + Expr::Return(Atom::Var(r)) => { |
| 369 | + let _ = writeln!( |
| 370 | + defs, |
| 371 | + "/* {} */\n#define tl_const{id} ((tl_value)&tl_const{id}_{r})", |
| 372 | + decl.name |
| 373 | + ); |
| 374 | + break; |
| 375 | + } |
| 376 | + _ => return, |
| 377 | + } |
| 378 | + } |
| 379 | + // Only now: a closure's entry is emitted only if it is used. |
| 380 | + for f in entries { |
| 381 | + self.entries.insert((f, 0)); |
| 382 | + } |
| 383 | + self.constants.insert(id); |
| 384 | + self.constant_defs.push_str(&defs); |
| 385 | + } |
| 386 | + |
| 316 | 387 | fn atom(&mut self, a: &Atom) -> Result<String, Unsupported> { |
| 317 | 388 | Ok(match a { |
| 318 | 389 | Atom::Var(v) => format!("v{v}"), |
| @@ -420,7 +491,7 @@ impl Emitter<'_> { |
| 420 | 491 | let _ = writeln!(out, "{ind} goto entry;\n{ind}}}"); |
| 421 | 492 | return Ok(None); |
| 422 | 493 | } |
| 423 | | − Value::CallFn(g, args) => { |
| 494 | + Value::CallFn(g, args) if !self.constants.contains(g) => { |
| 424 | 495 | let slots = self.atoms(args)?; |
| 425 | 496 | store_xargs(&slots, &ind, out); |
| 426 | 497 | let _ = writeln!( |
| @@ -553,9 +624,8 @@ impl Emitter<'_> { |
| 553 | 624 | /// Whether computing `value` may start or continue unwinding. |
| 554 | 625 | fn may_unwind(&self, value: &Value) -> bool { |
| 555 | 626 | match value { |
| 556 | | − Value::CallFn(..) | Value::Call(..) | Value::Handle { .. } | Value::Region { .. } => { |
| 557 | | − true |
| 558 | | − } |
| 627 | + Value::CallFn(g, _) => !self.constants.contains(g), |
| 628 | + Value::Call(..) | Value::Handle { .. } | Value::Region { .. } => true, |
| 559 | 629 | Value::Perform { effect, .. } => !self.program.effects[*effect as usize] |
| 560 | 630 | .name |
| 561 | 631 | .starts_with("std/ref."), |
| @@ -581,6 +651,9 @@ impl Emitter<'_> { |
| 581 | 651 | let a = self.atom(a)?; |
| 582 | 652 | let _ = writeln!(out, "{ind}v{v} = {a};"); |
| 583 | 653 | } |
| 654 | + Value::CallFn(g, _) if self.constants.contains(g) => { |
| 655 | + let _ = writeln!(out, "{ind}v{v} = tl_const{g};"); |
| 656 | + } |
| 584 | 657 | Value::CallFn(g, args) => { |
| 585 | 658 | let slots = self.atoms(args)?; |
| 586 | 659 | store_xargs(&slots, ind, out); |