Initial Commit
This commit is contained in:
706 files changed
+135430
No files matched your search
+864
@@ -0,0 +1,864 @@
|
||||
#! @file entry.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the entry of the rebuild as a program, whose subcommands lex, parse,
|
||||
#! and check the files they are given as one program, print what they note,
|
||||
#! and where asked, lower it to intermediate code, run it, or build it, or serve
|
||||
#! the language server protocol. It reads its arguments through `std::args`,
|
||||
#! so `sakura --help`, and `--help` after each subcommand, say what each takes:
|
||||
#!
|
||||
#! sakura check [OPTIONS] FILE...
|
||||
#! sakura run [OPTIONS] FILE... [-- ARGUMENT...]
|
||||
#! sakura build [OPTIONS] -o OUTPUT FILE...
|
||||
#! sakura lsp [OPTIONS]
|
||||
#!
|
||||
#! The rebuild runs itself as `sakura run LIBS entry.sakura -- COMMAND ...`.
|
||||
#! LIBS gives a `--lib` for each library it reads: its own `sakura::analyzer`,
|
||||
#! `sakura::compiler`, `sakura::machine`, `sakura::native`, and
|
||||
#! `sakura::server`, in the folders of their names; the standard library, as
|
||||
#! `std`, in its repository beside this one; and the back end's
|
||||
#! `binary::amd64`, `binary::elf`, `binary::dwarf`, `binary::pe`, and
|
||||
#! `binary::pdb`, as `docs/later.md` lists them.
|
||||
#!
|
||||
#! A program reads the modules of the standard library its files refer to,
|
||||
#! from the folder `--lib std=DIR` names, else a `std` folder beside the
|
||||
#! executable or in the nearest folder above it, as `core` is found, else the
|
||||
#! libraries of std in the `share/sakura` folder nearest above it, where
|
||||
#! `blossom install` puts them, as `std.io`. The runtime, the module and folder
|
||||
#! `runtime` found beside the compiler or above it as `core` and `system` are,
|
||||
#! is only part of executables, as its files say with `when backend == native`.
|
||||
#! `--lib NAME=DIR` names another library, the module `NAME` as the directory,
|
||||
#! which may be within another module, as `std::yaml` or `sakura::analyzer`;
|
||||
#! its modules are read where the files refer to them, as the standard
|
||||
#! library's are, and `lsp` takes it too.
|
||||
#! Programs are checked for the system the rebuild runs on, Linux or Windows,
|
||||
#! or with `--target` for an executable of the target.
|
||||
#!
|
||||
#! `check` says how many notes it made; `--lower` also lowers the program,
|
||||
#! verifies each function's code, and says how much it made and what it could
|
||||
#! not lower, `--ir` prints the code of each function too, `--bytecode` the
|
||||
#! machine's code of each, and `--only NAME` only that of the functions of the
|
||||
#! name, each leaving out the functions nothing reaches once the program is
|
||||
#! optimized, as a build makes no code of them. `--all` lowers every function,
|
||||
#! not only those `main` reaches, and keeps them all. The code is checked as
|
||||
#! `run`'s is, or, with `--target`, as `build`'s is, unless `--checks` or
|
||||
#! `--fast` says otherwise. `--checks-report` lowers the program checked and
|
||||
#! optimized, and lists the checks the optimizer could not leave out, in the
|
||||
#! program's own functions, by line: where an unchecked build trusts the
|
||||
#! program.
|
||||
#!
|
||||
#! A program is checked or not. A checked one stops at an index past the end,
|
||||
#! the wrong field of a union read, an integer converted to an enum none of
|
||||
#! whose members has its value, and a `match` finding no arm, with what went
|
||||
#! wrong and where, as std's containers stop where they are asked for what they
|
||||
#! do not hold. One that is not checked does none of that, as C does, which
|
||||
#! `checking()` tells the program. Every program stops at null reached through,
|
||||
#! at a division by zero, and as its stack runs out; only a checked executable
|
||||
#! compares an address with null where it reaches past the first page. `run`
|
||||
#! checks, unless `--fast`, and `build` does not, unless `--checks`.
|
||||
#!
|
||||
#! `run` runs the program, giving it the arguments after `--`, and exits with
|
||||
#! what it exits with. `--checks` also checks every read and write through a
|
||||
#! reference or pointer, stopping where the program holds no memory there,
|
||||
#! holds back the blocks it releases so that using one stops it, and reports
|
||||
#! the blocks it never released as it ends, by where each was allocated. Its
|
||||
#! code is optimized no more than `-O1` would, since inlining may leave out
|
||||
#! the frames a dangling reference refers to.
|
||||
#!
|
||||
#! `build` writes an x86-64 executable of the program to the file `-o` names,
|
||||
#! with the runtime compiled into it. One for Windows is named with `.exe`
|
||||
#! added where `-o` names no extension. `-g` also writes the debugging
|
||||
#! information debuggers such as gdb read to show where the program is, its
|
||||
#! functions, those inlined, and, built with `-O0`, its variables: on Linux to
|
||||
#! OUTPUT.petal, which the executable names, so debuggers find it beside it,
|
||||
#! and on Windows into the executable itself.
|
||||
#!
|
||||
#! `lsp` serves the language server protocol over the standard input and
|
||||
#! output, checking every `.sakura` file of the editor's workspace with the
|
||||
#! standard library, as the files change. Where several files declare `main`,
|
||||
#! the program of the one last opened, edited, or asked about is checked,
|
||||
#! leaving out the others.
|
||||
#!
|
||||
#! `-O0`, `-O1` and `-O2` say how much the code is optimized, `-O2` where none
|
||||
#! is given. `--inline N` inlines functions of up to N instructions rather than
|
||||
#! 16. `--verify` verifies the code of each function once it is lowered and
|
||||
#! after each pass, as `--lower` does, and in a build replays where each
|
||||
#! function's values were placed.
|
||||
#!
|
||||
#! `--threads N`, or `-j N`, says how many threads compile the program: one on
|
||||
#! each processor, up to eight, where it says 0 or nothing. They open and read
|
||||
#! the files, each lexing and parsing a file of its own; optimize the functions,
|
||||
#! each once those it calls are; assemble the machine's code of each function
|
||||
#! for `run`; and place the values of the functions for `build`, ahead of the
|
||||
#! one thread choosing their code. What is made is the same however many there
|
||||
#! are. While the files are read and checked, a thread of its own reports what
|
||||
#! is noted, as each thread making a note tells it, in the order the notes
|
||||
#! would be made by one thread alone. `-j 1` starts no thread at all.
|
||||
|
||||
import std::args;
|
||||
import std::io;
|
||||
import system;
|
||||
import std::thread;
|
||||
import sakura::analyzer;
|
||||
import sakura::compiler;
|
||||
import sakura::machine;
|
||||
import sakura::native;
|
||||
import sakura::server;
|
||||
|
||||
#! The subcommands of the rebuild.
|
||||
@args::command{ name = "sakura", help = "Checks, runs, and builds Sakura programs, and serves editors." }
|
||||
union commands {
|
||||
@args::command{ help = "Checks the files as one program, and lowers it where asked." }
|
||||
var check: check_args;
|
||||
@args::command{ help = "Checks the files, then runs their `main` on the machine." }
|
||||
var run: run_args;
|
||||
@args::command{ help = "Checks the files, then writes an executable of them." }
|
||||
var build: build_args;
|
||||
@args::command{ help = "Serves the language server protocol over the standard input and output." }
|
||||
var lsp: lsp_args;
|
||||
}
|
||||
|
||||
#! How much the code is optimized.
|
||||
enum optimization: u8 {
|
||||
@choice{ name = "0", help = "Not at all but for copying `@inline` functions, keeping every named local in a cell of its own." } none,
|
||||
@choice{ name = "1", help = "Within each function." } local,
|
||||
@choice{ name = "2", help = "Inlining small functions too." } inlined,
|
||||
}
|
||||
|
||||
#! Whether a program's contracts are checked.
|
||||
enum contracting: u8 {
|
||||
@choice{ name = "on", help = "Each is checked where it is, as where none is said." } on,
|
||||
@choice{ name = "off", help = "Each is left out, conditions and all." } off,
|
||||
}
|
||||
|
||||
#! What an executable is built for.
|
||||
enum platform: u8 {
|
||||
@choice{ name = "linux-x86_64", help = "Linux on x86-64." } linux,
|
||||
@choice{ name = "windows-x86_64", help = "Windows on x86-64." } windows,
|
||||
}
|
||||
|
||||
#! Where a program's libraries are, and where the files it read are written,
|
||||
#! which `check`, `run`, and `build` say alike.
|
||||
@group
|
||||
struct reading_args {
|
||||
@option{ name = "lib", value = "NAME=DIR", help = "A library, the module of the name, as `json` or `std::yaml`, in the directory, as blossom gives a package; given again for each." }
|
||||
var libraries: vector<strview>;
|
||||
@option{ value = "FILE", help = "Writes the path of each file the program read to the file, one to a line." }
|
||||
var deps: strview;
|
||||
}
|
||||
|
||||
#! What `check` takes.
|
||||
@args::command
|
||||
struct check_args {
|
||||
@group
|
||||
var reading: reading_args;
|
||||
@option{ help = "Checks the files for an executable of the target, rather than the machine." }
|
||||
var target: maybe<platform>;
|
||||
@option{ short = 'O', value = "LEVEL", help = "How much the code is optimized where it is lowered." }
|
||||
var optimize: optimization = optimization::inlined;
|
||||
@option{ name = "inline", value = "N", help = "Inlines functions of up to N instructions." }
|
||||
var inline_limit: u32 = compiler::inline_limit;
|
||||
@option{ help = "Verifies the code of each function once it is lowered and after each pass." }
|
||||
var verify: bool;
|
||||
@option{ help = "Lowers the program, and says how much it made and what it could not lower." }
|
||||
var lower: bool;
|
||||
@option{ help = "Lowers the program, and prints the code of each function." }
|
||||
var ir: bool;
|
||||
@option{ help = "Lowers the program, and prints the machine's code of each function." }
|
||||
var bytecode: bool;
|
||||
@option{ help = "Lowers every function, not only those `main` reaches." }
|
||||
var all: bool;
|
||||
@option{ value = "NAME", help = "Prints only the code of the functions of the name." }
|
||||
var only: strview;
|
||||
@option{ value = "on|off", help = "Whether the contracts the code lowered has are checked, or left out." }
|
||||
var contracts: contracting = contracting::on;
|
||||
@option{ help = "Lowers the code checked, as `run` and `build --checks` make it, where `--target` would not." }
|
||||
var checks: bool;
|
||||
@option{ help = "Lowers the code unchecked, as `run --fast` and `build` make it, where it would be checked." }
|
||||
var fast: bool;
|
||||
@option{ help = "Lowers the program checked and optimized, and lists the checks left in its own functions, by line." }
|
||||
var checks_report: bool;
|
||||
@option{ short = 'j', value = "N", help = "How many threads compile the program at once, or 0 for one on each processor, up to 8." }
|
||||
var threads: u32;
|
||||
@argument{ value = "FILE", help = "The files of the program." }
|
||||
var files: vector<strview>;
|
||||
}
|
||||
|
||||
#! How much the code is optimized, which `run` and `build` say alike. `check`
|
||||
#! says it of the code it lowers, so it declares these options itself.
|
||||
@group
|
||||
struct optimizing {
|
||||
@option{ short = 'O', value = "LEVEL", help = "How much the code is optimized." }
|
||||
var optimize: optimization = optimization::inlined;
|
||||
@option{ name = "inline", value = "N", help = "Inlines functions of up to N instructions." }
|
||||
var inline_limit: u32 = compiler::inline_limit;
|
||||
}
|
||||
|
||||
#! What `run` takes.
|
||||
@args::command
|
||||
struct run_args {
|
||||
@group
|
||||
var reading: reading_args;
|
||||
@group
|
||||
var optimizing: optimizing;
|
||||
@option{ help = "Verifies the code of each function once it is lowered and after each pass." }
|
||||
var verify: bool;
|
||||
@option{ help = "Also checks every read and write through a reference or pointer, stopping where the program holds no memory, and reports the blocks it never released as it ends." }
|
||||
var checks: bool;
|
||||
@option{ help = "Leaves out what a checked program checks, as indexes, as a build without `--checks` does." }
|
||||
var fast: bool;
|
||||
@option{ help = "Interprets every function, compiling none to the processor's code as the machine does where it runs on Windows." }
|
||||
var interpret: bool;
|
||||
@option{ value = "on|off", help = "Whether the program's contracts are checked as it runs, or left out." }
|
||||
var contracts: contracting = contracting::on;
|
||||
@option{ short = 'j', value = "N", help = "How many threads compile the program at once, or 0 for one on each processor, up to 8." }
|
||||
var threads: u32;
|
||||
@argument{ value = "FILE", help = "The files of the program." }
|
||||
var files: vector<strview>;
|
||||
@argument{ value = "ARGUMENT", rest = true, help = "What the program's `main` is given." }
|
||||
var arguments: vector<strview>;
|
||||
}
|
||||
|
||||
#! What `build` takes.
|
||||
@args::command
|
||||
struct build_args {
|
||||
@group
|
||||
var reading: reading_args;
|
||||
@option{ help = "What the executable is for, rather than the system the rebuild runs on." }
|
||||
var target: maybe<platform>;
|
||||
@group
|
||||
var optimizing: optimizing;
|
||||
@option{ help = "Verifies the code of each function, and replays where its values were placed." }
|
||||
var verify: bool;
|
||||
@option{ short = 'g', help = "Also writes debugging information: beside a Linux executable, and into a Windows one." }
|
||||
var debug: bool;
|
||||
@option{ help = "Checks indexes, the field a union holds, and the rest, as `run` does, stopping where one fails." }
|
||||
var checks: bool;
|
||||
@option{ value = "on|off", help = "Whether the program's contracts are checked as it runs, or left out." }
|
||||
var contracts: contracting = contracting::on;
|
||||
@option{ short = 'o', value = "OUTPUT", required = true, help = "Where the executable is written." }
|
||||
var output: strview;
|
||||
@option{ short = 'j', value = "N", help = "How many threads compile the program at once, or 0 for one on each processor, up to 8." }
|
||||
var threads: u32;
|
||||
@argument{ value = "FILE", help = "The files of the program." }
|
||||
var files: vector<strview>;
|
||||
}
|
||||
|
||||
#! What `lsp` takes.
|
||||
@args::command
|
||||
struct lsp_args {
|
||||
@option{ name = "lib", value = "NAME=DIR", help = "A library, the module of the name, as `json` or `std::yaml`, in the directory; given again for each." }
|
||||
var libraries: vector<strview>;
|
||||
@option{ help = "Checks the workspace for an executable of the target, rather than the machine." }
|
||||
var target: maybe<platform>;
|
||||
@option{ help = "Serves over the standard input and output, which is the only way it serves." }
|
||||
var stdio: bool;
|
||||
}
|
||||
|
||||
def main(args: span<strview>): void {
|
||||
# Windows cannot start a copy of the server, so the processes it works its
|
||||
# jobs out in are this program started again: `lsp --base`, holding the
|
||||
# workspace parsed, and `lsp --worker`, working out a job from nothing.
|
||||
when os == windows {
|
||||
if args.length == 2 && args[0] == "lsp" && (args[1] == "--base" || args[1] == "--worker") {
|
||||
exit(serve_process(args[1] == "--base"));
|
||||
}
|
||||
}
|
||||
|
||||
val chosen := args::parse_or_exit::<commands>(args);
|
||||
match chosen {
|
||||
check(&c) => exit(work(&job {
|
||||
lowers = c.lower || c.ir || c.bytecode || c.checks_report, prints = c.ir, codes = c.bytecode, all = c.all,
|
||||
verify = c.verify, native = c.target is some(_), system = system_of(c.target),
|
||||
level = c.optimize as u32, inline_limit = c.inline_limit, only = c.only, contracts_off = c.contracts == contracting::off,
|
||||
checks_off = !c.checks_report && (c.fast || (c.target is some(_) && !c.checks)),
|
||||
reports = c.checks_report, opposed = c.checks && c.fast,
|
||||
libraries = c.reading.libraries.view(), deps = c.reading.deps, threads = threads_of(c.threads), files = c.files.view()
|
||||
}));
|
||||
run(&r) => exit(work(&job {
|
||||
runs = true, verify = r.verify, checks = r.checks, contracts_off = r.contracts == contracting::off,
|
||||
checks_off = r.fast, opposed = r.checks && r.fast, interpret = r.interpret,
|
||||
system = host_system, level = r.optimizing.optimize as u32,
|
||||
inline_limit = r.optimizing.inline_limit, libraries = r.reading.libraries.view(),
|
||||
deps = r.reading.deps, threads = threads_of(r.threads), files = r.files.view(), given = r.arguments.view()
|
||||
}));
|
||||
build(&b) => exit(work(&job {
|
||||
builds = true,
|
||||
verify = b.verify,
|
||||
debug = b.debug,
|
||||
contracts_off = b.contracts == contracting::off,
|
||||
checks_off = !b.checks,
|
||||
native = true,
|
||||
system = system_of(b.target),
|
||||
level = b.optimizing.optimize as u32,
|
||||
inline_limit = b.optimizing.inline_limit,
|
||||
libraries = b.reading.libraries.view(),
|
||||
deps = b.reading.deps,
|
||||
output = b.output,
|
||||
threads = threads_of(b.threads),
|
||||
files = b.files.view()
|
||||
}));
|
||||
lsp(&l) => exit(serve_lsp(&l));
|
||||
}
|
||||
}
|
||||
|
||||
#! What a subcommand asks of the files it is given, as its options say.
|
||||
struct job {
|
||||
var runs: bool; #! Whether the program is run.
|
||||
var builds: bool; #! Whether an executable of it is written.
|
||||
var lowers: bool; #! Whether it is lowered, and how much it made is said.
|
||||
var prints: bool; #! Whether the code of each function is printed.
|
||||
var codes: bool; #! Whether the machine's code of each function is printed.
|
||||
var all: bool; #! Whether every function is lowered.
|
||||
var verify: bool; #! Whether the code of each function is verified.
|
||||
var checks: bool; #! Whether what references and pointers reach is checked as the program runs.
|
||||
var interpret: bool; #! Whether the machine interprets every function, compiling none.
|
||||
var checks_off: bool; #! Whether the program is lowered unchecked, leaving out indexes' checks and the rest.
|
||||
var opposed: bool; #! Whether `--checks` and `--fast` were both given.
|
||||
var reports: bool; #! Whether the checks the optimizer left are listed.
|
||||
var contracts_off: bool; #! Whether contracts are left out, conditions and all.
|
||||
var debug: bool; #! Whether debugging information is written.
|
||||
var native: bool; #! Whether it is checked for an executable.
|
||||
var system: strview; #! The operating system it is checked for, as `linux`.
|
||||
var level: u32; #! How much the code is optimized.
|
||||
var inline_limit: u32; #! The most instructions of a function inlined, or 0 for the default.
|
||||
var threads: u32; #! How many threads compile the program, the first among them.
|
||||
var only: strview; #! The name of the functions whose code alone is printed, or empty.
|
||||
var libraries: span<strview>; #! The libraries `--lib` names, each as `NAME=DIR`.
|
||||
var deps: strview; #! Where the path of each file read is written, or empty.
|
||||
var output: strview; #! Where an executable is written.
|
||||
var files: span<strview>; #! The files of the program.
|
||||
var given: span<strview>; #! What the program's `main` is given.
|
||||
}
|
||||
|
||||
#! How many threads compile a program where `--threads` asks for none in
|
||||
#! particular, at most. Every thread takes the same lock for the memory it
|
||||
#! takes from the heap, so more than these mostly wait on one another.
|
||||
val most_threads: u32 = 8;
|
||||
|
||||
#! How many threads compile a program: those asked for, or one on each
|
||||
#! processor, up to `most_threads`, where 0 is.
|
||||
#! @param asked How many `--threads` asks for.
|
||||
#! @returns How many.
|
||||
def threads_of(asked: u32): u32 {
|
||||
return asked if asked != 0;
|
||||
val processors := thread::cpu_count();
|
||||
return processors < most_threads ? processors : most_threads;
|
||||
}
|
||||
|
||||
#! The operating system a target is for, or the one the rebuild runs on where
|
||||
#! none is given.
|
||||
#! @param chosen The target, or nothing.
|
||||
#! @returns The system, as `linux`.
|
||||
def system_of(chosen: maybe<platform>): strview {
|
||||
if chosen is some(which) {
|
||||
return which == platform::linux ? "linux" : "windows";
|
||||
}
|
||||
return host_system;
|
||||
}
|
||||
|
||||
#! Checks the files of a program, and where a subcommand asks, lowers it, runs
|
||||
#! it, or builds it.
|
||||
#! @param asked What the subcommand asks.
|
||||
#! @returns The status to exit with.
|
||||
def work(asked: &job): i32 {
|
||||
if asked.opposed {
|
||||
system::fd::stderr.put("sakura: `--fast` leaves out the checks `--checks` asks for, so only one of them is given\n");
|
||||
return 2;
|
||||
}
|
||||
var paths := vector::<string>::new();
|
||||
for &each in asked.files {
|
||||
paths.append(string::from(each, nothing {}));
|
||||
}
|
||||
return 2 if !libraries_named(asked.libraries);
|
||||
val found := found_std(asked.libraries);
|
||||
val core := toolchain_folder("core");
|
||||
if core.length == 0 {
|
||||
system::fd::stderr.put("sakura: no `core` folder is beside the compiler or above it, and every program reads one\n");
|
||||
return 2;
|
||||
}
|
||||
var output := string::from(asked.output, nothing {});
|
||||
# Windows runs a program by its name alone only where the name ends in `.exe`.
|
||||
if asked.builds && asked.system == "windows" && !has_extension(output.view()) {
|
||||
output.append(".exe");
|
||||
}
|
||||
# The program's files are read, then `core`, whole, the types the syntax
|
||||
# makes first, then the modules of `system`, those `--lib` names, and the
|
||||
# standard library found, that the files read refer to, a round at a time,
|
||||
# until a round refers to nothing new. A library given first is the one
|
||||
# read, so one `--lib` names is read rather than one found. An executable
|
||||
# has the runtime too, from the folder `runtime`, which needs nothing of the
|
||||
# library.
|
||||
val runtime := toolchain_folder("runtime");
|
||||
if asked.builds && runtime.length == 0 {
|
||||
system::fd::stderr.put("sakura: no `runtime` folder is beside the compiler or above it, and every executable is built with its runtime\n");
|
||||
return 2;
|
||||
}
|
||||
var finder := resolver::new();
|
||||
finder.whole("core", core.view());
|
||||
val platform := toolchain_folder("system");
|
||||
if platform.length != 0 {
|
||||
finder.add_library("system", platform.view());
|
||||
}
|
||||
if runtime.length != 0 {
|
||||
finder.add_library("runtime", runtime.view());
|
||||
}
|
||||
for each in asked.libraries {
|
||||
val split := each.index_of(&'=') else { continue; };
|
||||
finder.add_library(each.substr(0, split), each.substr(split + 1, each.length - split - 1));
|
||||
}
|
||||
for &each in found {
|
||||
val split := each.view().index_of(&'=') else { continue; };
|
||||
finder.add_library(each.view().substr(0, split), each.view().substr(split + 1, each.length - split - 1));
|
||||
}
|
||||
paths.append(string::from(builtin_path, nothing {}));
|
||||
every_file(core.view(), &mut paths);
|
||||
for &each in paths {
|
||||
finder.mark(each.view());
|
||||
}
|
||||
if asked.builds {
|
||||
finder.want("runtime", &mut paths);
|
||||
}
|
||||
|
||||
var names := names::new();
|
||||
var notes := vector::<note>::new();
|
||||
var sources := vector::<source>::new();
|
||||
var syntaxes := vector::<syntax>::new();
|
||||
var roots := vector::<u32>::new();
|
||||
|
||||
# Each file's source and syntax stay where they are once the checker, and the
|
||||
# thread reporting notes, refer to them, so there is room for every file the
|
||||
# libraries hold before any is read.
|
||||
val room := paths.length + finder.files_held();
|
||||
sources.resize(room);
|
||||
syntaxes.reserve(room);
|
||||
for _ in 0..room {
|
||||
syntaxes.append(syntax::new());
|
||||
}
|
||||
|
||||
# What reading and checking note is reported as it is noted, rather than
|
||||
# kept: by a thread of its own, where there are threads, which the threads
|
||||
# reading the files and the one checking them tell each note as they make
|
||||
# it, and on the thread making it where there are none.
|
||||
val errors := asked.runs || asked.builds;
|
||||
var noted := atomic::<u64>::new(0);
|
||||
var warned := atomic::<u64>::new(0);
|
||||
var told := thread::channel::<news>::new(0);
|
||||
var reporting := vector::<thread::handle<void>>::new();
|
||||
var alone := asked.threads <= 1;
|
||||
if !alone {
|
||||
alone = !start(def() { report_notes(&mut told, &sources, errors); }, &mut reporting);
|
||||
}
|
||||
val hear := def(made: &mut note): void {
|
||||
_ = made.warning ? warned.fetch_add(1) : noted.fetch_add(1);
|
||||
if alone {
|
||||
report_note(made, &sources, errors);
|
||||
} else {
|
||||
_ = told.send(news { noted = move(made) });
|
||||
}
|
||||
};
|
||||
val heard := listener {
|
||||
noted = hear,
|
||||
read = def(file: u32): void {
|
||||
if !alone {
|
||||
_ = told.send(news { read = file });
|
||||
}
|
||||
}
|
||||
};
|
||||
# Each round's sources are opened on as many threads as there are, each into
|
||||
# its place, then read, and what they refer to is the next round.
|
||||
var done: u64 = 0;
|
||||
while done < paths.length {
|
||||
val from := done;
|
||||
val to := paths.length;
|
||||
share_out(to - from, asked.threads, def(k: u64): void {
|
||||
val j := from + k;
|
||||
if paths[j].view() == builtin_path {
|
||||
sources.set(j, source::from(builtin_path, builtin_text));
|
||||
} else {
|
||||
sources.set(j, source::open(paths[j].view()));
|
||||
}
|
||||
});
|
||||
read_files(&sources, &mut syntaxes, &mut roots, &mut names, asked.threads, &heard, from, to);
|
||||
for j in from..to {
|
||||
finder.refer(syntaxes.at(j), &names, &mut paths);
|
||||
}
|
||||
done = to;
|
||||
}
|
||||
if asked.deps.length != 0 && !write_deps(asked.deps, paths.view()) {
|
||||
system::fd::stderr.put($"sakura: the files read could not be written to {asked.deps}\n");
|
||||
}
|
||||
|
||||
# A program is checked once, so what an edit would check again is not
|
||||
# recorded.
|
||||
var checker := checker::new(&mut names, &mut notes);
|
||||
checker.sink.heard = hear;
|
||||
checker.tracks = false;
|
||||
checker.contracts_off = asked.contracts_off;
|
||||
checker.checks_off = asked.checks_off;
|
||||
target_checker(&mut checker, asked.system, asked.builds || asked.native);
|
||||
for j in 0..paths.length {
|
||||
checker.add(sources.at(j), syntaxes.at(j), roots.at(j));
|
||||
}
|
||||
checker.check();
|
||||
|
||||
# What checking leaves to evaluating is evaluated once the program checks,
|
||||
# which notes what goes wrong as checking does.
|
||||
var results := outcomes::new();
|
||||
if noted.load() == 0 {
|
||||
results = evaluate(&mut checker);
|
||||
}
|
||||
|
||||
# Nothing more is noted, so the reporting ends once it has reported what was.
|
||||
# What the checking lowering asks for might note is kept rather than heard,
|
||||
# as it was never reported, and lowering says what went wrong itself.
|
||||
_ = told.send(news { ended = nothing {} });
|
||||
for &mut each in reporting {
|
||||
each.join();
|
||||
}
|
||||
checker.sink.heard = nothing {};
|
||||
val count := noted.load();
|
||||
|
||||
val runs := asked.runs;
|
||||
val builds := asked.builds;
|
||||
if (runs || builds) && count != 0 {
|
||||
return 2;
|
||||
}
|
||||
if !runs && !builds {
|
||||
println($"{count} notes, {warned.load()} warnings, {checker.skipped} unchecked, {checker.decls.count} declarations, {checker.types.count} types");
|
||||
}
|
||||
# Notes end a check with 2, as they end a run or a build, so what runs it, as
|
||||
# blossom does, knows it failed.
|
||||
return 2 if count != 0;
|
||||
return 0 if !asked.lowers && !runs && !builds;
|
||||
|
||||
val options := lowering {
|
||||
verify = asked.verify || asked.lowers,
|
||||
checks = asked.checks,
|
||||
everything = asked.all,
|
||||
# A run checking its memory inlines nothing, so each function's locals end
|
||||
# where its frame does, as a reference to one past it finds.
|
||||
level = asked.checks && asked.level > 1 ? 1 : asked.level,
|
||||
keep_locals = asked.level == 0,
|
||||
describe = builds && asked.debug,
|
||||
inline_limit = asked.inline_limit,
|
||||
threads = asked.threads
|
||||
};
|
||||
var lowered := lowerer::new(&mut checker, options);
|
||||
lowered.evaluated = move(&mut results);
|
||||
lowered.compile();
|
||||
if builds {
|
||||
for &each in lowered.problems {
|
||||
system::fd::stderr.put($"{each}\n");
|
||||
}
|
||||
if lowered.problems.length != 0 {
|
||||
return 2;
|
||||
}
|
||||
val made := build_executable(&mut lowered, build_options {
|
||||
output = output.view(), verify = asked.verify, debug = asked.debug, windows = asked.system == "windows",
|
||||
threads = asked.threads
|
||||
});
|
||||
for &each in made {
|
||||
system::fd::stderr.put($"sakura: {each}\n");
|
||||
}
|
||||
return made.length != 0 ? 2 : 0;
|
||||
}
|
||||
if runs {
|
||||
for &each in lowered.problems {
|
||||
system::fd::stderr.put($"{each}\n");
|
||||
}
|
||||
if lowered.problems.length != 0 {
|
||||
return 2;
|
||||
}
|
||||
lowered.drop_unreached_functions();
|
||||
return run_program(&mut lowered, asked.given, asked.checks, asked.interpret);
|
||||
}
|
||||
|
||||
# What a run or a build would drop is left out, so what is said is what they
|
||||
# would make code of.
|
||||
lowered.drop_unreached_functions();
|
||||
if asked.reports {
|
||||
report_checks(&mut lowered, &sources);
|
||||
return 0;
|
||||
}
|
||||
if asked.codes {
|
||||
lowered.assemble_all();
|
||||
}
|
||||
var kept: u64 = 0;
|
||||
var instructions: u64 = 0;
|
||||
for &each in lowered.program.functions {
|
||||
continue if !each.lowered;
|
||||
kept++;
|
||||
instructions += each.body.insts.length - 1;
|
||||
continue if (!asked.prints && !asked.codes) || (asked.only.length != 0 && each.name.view() != asked.only);
|
||||
println($"function {each.name}:");
|
||||
if asked.prints {
|
||||
print(each.body.print());
|
||||
}
|
||||
if asked.codes {
|
||||
print(each.compiled.print());
|
||||
}
|
||||
}
|
||||
for &each in lowered.problems {
|
||||
println(each);
|
||||
}
|
||||
println($"{kept} functions, {instructions} instructions, {lowered.program.layouts.length} layouts, {lowered.problems.length} problems");
|
||||
return 0;
|
||||
}
|
||||
|
||||
#! The words `--checks-report` names each kind of check by, one and many.
|
||||
const check_names: strview[checked::count as u64] = ["index", "union field", "enum value", "fault"];
|
||||
const checks_names: strview[checked::count as u64] = ["indexes", "union fields", "enum values", "faults"];
|
||||
|
||||
#! Lists the checks the optimizer left in the program's own functions, as
|
||||
#! `--checks-report` asks: a line for each line of a function's source that
|
||||
#! makes any, with how many of each kind, then how many in all.
|
||||
#! @param lowered The program, lowered and optimized.
|
||||
#! @param sources The source of each file.
|
||||
def report_checks(lowered: &mut lowerer, sources: &vector<source>): void {
|
||||
val left := lowered.checks_left();
|
||||
var total: u64 = 0;
|
||||
var lines: u64 = 0;
|
||||
var i: u64 = 0;
|
||||
while i < left.length {
|
||||
val first := left[i];
|
||||
val held := &sources.at(pair_high(first.at) as u64 - 1);
|
||||
val line := held.lines.place_of(pair_low(first.at)).line;
|
||||
var counts: u64[checked::count as u64];
|
||||
while i < left.length && left[i].function == first.function && left[i].at >> 32 == first.at >> 32
|
||||
&& held.lines.place_of(pair_low(left[i].at)).line == line {
|
||||
counts[left[i].kind as u64] += left[i].count as u64;
|
||||
total += left[i].count as u64;
|
||||
i++;
|
||||
}
|
||||
var text := $"{held.path}:{line}: {lowered.program.functions[first.function].name}:";
|
||||
var listed := false;
|
||||
for k in 0..counts.length {
|
||||
continue if counts[k] == 0;
|
||||
text.append(listed ? ", " : " ");
|
||||
text.append($"{counts[k]} {counts[k] == 1 ? check_names[k] : checks_names[k]}");
|
||||
listed = true;
|
||||
}
|
||||
println(text);
|
||||
lines++;
|
||||
}
|
||||
println($"{total} {total == 1 ? "check" : "checks"} at {lines} {lines == 1 ? "line" : "lines"}");
|
||||
}
|
||||
|
||||
#! What the thread reporting what reading and checking a program note is told.
|
||||
union news {
|
||||
var noted: note; #! A note, as it was made.
|
||||
var read: u32; #! That the file of the index was read whole, so nothing more is noted of reading it.
|
||||
var ended: nothing; #! That nothing more is noted.
|
||||
}
|
||||
|
||||
#! Reports what reading and checking a program note, as the threads making the
|
||||
#! notes tell them, until told nothing more is. They are reported in the order
|
||||
#! they would be were the files read one after another: those of each file
|
||||
#! once every file before it was read whole, which holds those of a file read
|
||||
#! beside an earlier one until that one is, and those of checking, which
|
||||
#! begins once every file is read, as they come.
|
||||
#! @param told What the threads making the notes tell.
|
||||
#! @param sources The source of each file.
|
||||
#! @param errors Whether they are reported to the standard error, rather
|
||||
#! than the standard output.
|
||||
def report_notes(told: &mut thread::channel<news>, sources: &vector<source>, errors: bool): void {
|
||||
val count := sources.length;
|
||||
var read := vector::<bool>::new();
|
||||
read.resize(count);
|
||||
var next: u64 = 0;
|
||||
var held := vector::<note>::new();
|
||||
while true {
|
||||
var item := told.receive() else { return; };
|
||||
if item is noted(&mut each) {
|
||||
if each.file as u64 <= next {
|
||||
report_note(each, sources, errors);
|
||||
} else {
|
||||
held.append(move(each));
|
||||
}
|
||||
} else if item is read(file) {
|
||||
read[file as u64] = true;
|
||||
while next < count && read[next] {
|
||||
next++;
|
||||
var kept := vector::<note>::new();
|
||||
for &mut each in held {
|
||||
if each.file as u64 == next {
|
||||
report_note(each, sources, errors);
|
||||
} else {
|
||||
kept.append(move(each));
|
||||
}
|
||||
}
|
||||
held = move(&mut kept);
|
||||
}
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#! Starts a thread running a function.
|
||||
#! @param body What it runs.
|
||||
#! @param into Where its handle is kept.
|
||||
#! @returns Whether it started.
|
||||
def start(body: def(): void, into: &mut vector<thread::handle<void>>): bool {
|
||||
var made := thread::spawn(body) else (_) { return false; };
|
||||
into.append(move(&mut made));
|
||||
return true;
|
||||
}
|
||||
|
||||
#! Reports a note: where it is, what kind of mistake it is about, and what it
|
||||
#! says.
|
||||
#! @param each The note.
|
||||
#! @param sources The source of each file.
|
||||
#! @param errors Whether it is reported to the standard error, rather than
|
||||
#! the standard output.
|
||||
def report_note(each: ¬e, sources: &vector<source>, errors: bool): void {
|
||||
val held := &sources.at(each.file);
|
||||
val at := held.lines.place_of(each.offset);
|
||||
var line := each.warning ? $"{held.path}:{at.line}:{at.column}: warning: {each.code}: "
|
||||
: $"{held.path}:{at.line}:{at.column}: {each.code}: ";
|
||||
val text := note_text(each, held);
|
||||
line.append(text.view());
|
||||
if errors {
|
||||
system::fd::stderr.put($"{line}\n");
|
||||
} else {
|
||||
println(line);
|
||||
}
|
||||
}
|
||||
|
||||
#! Serves the language server protocol, checking for the target given, or for
|
||||
#! the machine.
|
||||
#! @param asked What `lsp` takes.
|
||||
#! @returns The status to exit with.
|
||||
def serve_lsp(asked: &lsp_args): i32 {
|
||||
return 2 if !libraries_named(asked.libraries.view());
|
||||
val found := found_std(asked.libraries.view());
|
||||
var libraries := vector::<strview>::new();
|
||||
for each in asked.libraries {
|
||||
libraries.append(each);
|
||||
}
|
||||
for &each in found {
|
||||
libraries.append(each.view());
|
||||
}
|
||||
return serve(server_options {
|
||||
native = asked.target is some(_), system = system_of(asked.target), libraries = libraries.view()
|
||||
});
|
||||
}
|
||||
|
||||
#! Whether each library `--lib` names is written `NAME=DIR`, of a module's name
|
||||
#! and a directory, saying which is not.
|
||||
#! @param given The libraries, as written.
|
||||
#! @returns Whether each is.
|
||||
def libraries_named(given: span<strview>): bool {
|
||||
for each in given {
|
||||
val split := each.index_of(&'=') else {
|
||||
system::fd::stderr.put($"sakura: `--lib {each}` names no library: write `NAME=DIR`\n");
|
||||
return false;
|
||||
};
|
||||
if !is_module_path(each.substr(0, split)) {
|
||||
system::fd::stderr.put($"sakura: `--lib {each}` names no module: write its path, as `json` or `std::yaml`\n");
|
||||
return false;
|
||||
}
|
||||
if !is_folder(each.substr(split + 1, each.length - split - 1)) {
|
||||
system::fd::stderr.put($"sakura: `--lib {each}` names no library: write `NAME=DIR`, of a directory\n");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#! Whether text is a module's path: names, each of letters, digits, and
|
||||
#! underscores, not beginning with a digit, between `::`.
|
||||
#! @param text The text.
|
||||
#! @returns Whether it is.
|
||||
def is_module_path(text: strview): bool {
|
||||
var at: u64 = 0;
|
||||
while true {
|
||||
return false if at >= text.length || text[at].is_digit;
|
||||
val start := at;
|
||||
while at < text.length && (text[at].is_alnum || text[at] == '_') {
|
||||
at++;
|
||||
}
|
||||
return false if at == start;
|
||||
return true if at == text.length;
|
||||
return false if text.length - at < 3 || text[at] != ':' || text[at + 1] != ':';
|
||||
at += 2;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#! Whether the last name of a path has an extension, as `.exe`, which a name
|
||||
#! only beginning with a dot does not.
|
||||
#! @param at The path.
|
||||
#! @returns Whether it does.
|
||||
def has_extension(at: strview): bool {
|
||||
var name := 0u;
|
||||
for i in 0..at.length {
|
||||
if at[i] == '/' || at[i] == '\\' {
|
||||
name = i + 1;
|
||||
}
|
||||
}
|
||||
for i in name + 1..at.length {
|
||||
return true if at[i] == '.';
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
when os == linux {
|
||||
# The system the rebuild runs on, which programs are checked for where no
|
||||
# target is given.
|
||||
val host_system: strview = "linux";
|
||||
}
|
||||
|
||||
when os == windows {
|
||||
# The system the rebuild runs on, which programs are checked for where no
|
||||
# target is given.
|
||||
val host_system: strview = "windows";
|
||||
}
|
||||
|
||||
#! Runs a program lowered, reporting a fault as the C machine does.
|
||||
#! @param lowered The program.
|
||||
#! @param args The arguments `main` is given.
|
||||
#! @param checks Whether its memory is checked, as `run --checks` asks.
|
||||
#! @param interpret Whether every function is interpreted, as `run --interpret` asks.
|
||||
#! @returns The status to exit with: what the program asked for, 1 where it
|
||||
#! faulted, and 0 otherwise.
|
||||
def run_program(lowered: &mut lowerer, args: span<strview>, checks: bool, interpret: bool): i32 {
|
||||
var running := interpreter::new(lowered, interpreting { checks = checks, interpret = interpret });
|
||||
val why := running.run(args);
|
||||
# Running with its memory checked, what the program never released is
|
||||
# reported as it ends, by exiting or returning from `main`.
|
||||
if checks && (why == halt::exited || why == halt::none) {
|
||||
var leaks := string::new(nothing {});
|
||||
running.report_leaks(&mut leaks);
|
||||
system::fd::stderr.put(leaks.view());
|
||||
}
|
||||
if why == halt::exited {
|
||||
return running.status;
|
||||
}
|
||||
if why != halt::none {
|
||||
var text := string::new(nothing {});
|
||||
running.report(why, &mut text);
|
||||
system::fd::stderr.put(text.view());
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#! Writes the path of each file a program read, one to a line, as `--deps` asks.
|
||||
#! @param at Where they are written.
|
||||
#! @param paths The paths, of which the types the syntax makes, which no file
|
||||
#! holds, are left out.
|
||||
#! @returns Whether they were written.
|
||||
def write_deps(at: strview, paths: span<string>): bool {
|
||||
var text := string::new(nothing {});
|
||||
for &each in paths {
|
||||
continue if each.view() == builtin_path;
|
||||
text.append(each.view());
|
||||
text.push('\n');
|
||||
}
|
||||
var found := path::parse(at) else (_) { return false; };
|
||||
return io::write_file(&found, text.view()) is value(_);
|
||||
}
|
||||
Reference in new issue
Block a user