#! @file unused.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the dropping of the functions nothing reaches once the program is #! optimized, as those whose every call inlining copied. What is reached is #! found from what is reached other than by the code's calls: `main`, the code #! initializing the globals, the function safe points call, each function a #! vtable holds, each C calls, and each the program takes as a value or the #! backend calls by name. Each function reached then reaches what its code #! calls, calls at safe points, and gives C to call, and its version. The rest #! are dropped as though never lowered, so no code is made of them, each #! keeping its index, and its name, which a trace of the code inlined from it #! still gives. module sakura::compiler; #! Drops the functions nothing reaches, as the file says, once the program is #! optimized and nothing more is lowered, but where every function is asked #! for, or the program evaluates constants. #! @param this The compiler. pub def lowerer::drop_unreached_functions(&mut this): void { return if this.options.everything || this.options.evaluating; val point := mark_scratch(); val count := this.program.functions.length; var reached := worklist::new(count, scratch()); this.reach_functions(&mut reached); for i in 0..count { if !reached.has(i as u32) { this.program.functions[i].lowered = false; } } release_scratch(point); } #! Finds the functions reached, as the file says. #! @param this The compiler. #! @param reached Where the functions reached are put, empty and as long as #! the functions are; each is in it after. pub def lowerer::reach_functions(&this, reached: &mut worklist): void { val count := this.program.functions.length; for i in 0..count { if this.program.functions[i].taken || this.program.functions[i].callback { reached.push_new(i as u32); } } val roots: u32[3] = [this.program.main, this.program.init, this.dispatch]; for r in 0..3u { if roots[r] != no_function { reached.push_new(roots[r]); } } for &each in this.program.vtables { for &function in each.functions { if function != no_function { reached.push_new(function); } } } while !reached.empty { val function := reached.pop(); val held := &this.program.functions[function]; if held.version != 0 { reached.push_new(held.version - 1); } continue if !held.lowered; var walk := held.body.walk_all(); while walk.more() { val at := walk.next(); val kind := held.body.insts[at].kind; if kind == op::call || kind == op::callback || kind == op::safepoint { reached.push_new(held.body.insts[at].aux); } } } }