984 lines
43 KiB
Plaintext
984 lines
43 KiB
Plaintext
#! @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, and checks its contracts, as `core`'s and std's containers
|
|
#! check what they are asked for. One that is not checked does none of that, as
|
|
#! C does. 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`. `--contracts on` or
|
|
#! `--contracts off` checks the contracts, or leaves them out, whichever the
|
|
#! program is.
|
|
#!
|
|
#! `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.
|
|
#!
|
|
#! A Windows executable may be shown as a program of its own: `--icon FILE`, a
|
|
#! PNG or an icon file, given again for each size, is its icon, which Explorer,
|
|
#! a shortcut to it, and its windows show, the PNGs kept whole, as Windows has
|
|
#! read them since Vista, and any wider or taller than an icon's 256 pixels
|
|
#! left out, so one meant for Linux's larger icons builds too; `--title TEXT`
|
|
#! and `--semv VERSION` are what its properties say of it, as Task Manager
|
|
#! names it by the title; and `--windowed` runs it in windows of its own, with
|
|
#! no console. The icon's files are among those `--deps` writes. A Linux
|
|
#! executable holds none of these, as Linux's desktops find a program's icon
|
|
#! by a desktop entry installed beside it, so they're left out there, saying so.
|
|
#!
|
|
#! `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;
|
|
import binary::pe;
|
|
|
|
#! 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, where `--contracts` says, rather
|
|
#! than as the program is.
|
|
enum contracting: u8 {
|
|
@choice{ name = "on", help = "Each is checked where it is, even where the program is not checked." } on,
|
|
@choice{ name = "off", help = "Each is left out, conditions and all, even where the program is checked." } off,
|
|
}
|
|
|
|
#! Whether a program's contracts are left out: as `--contracts` says, or else
|
|
#! where the program is not checked, so a checked program checks them too.
|
|
#! @param said What `--contracts` says, if it was given.
|
|
#! @param checks_off Whether the program is lowered unchecked.
|
|
#! @returns Whether they are.
|
|
def contracts_left_out(said: maybe<contracting>, checks_off: bool): bool {
|
|
return said is some(how) ? how == contracting::off : checks_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, rather than as the code is." }
|
|
var contracts: maybe<contracting>;
|
|
@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 its own integer arithmetic, stopping where it overflows, 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 or Linux." }
|
|
var interpret: bool;
|
|
@option{ value = "on|off", help = "Whether the program's contracts are checked as it runs, or left out, rather than as the program is." }
|
|
var contracts: maybe<contracting>;
|
|
@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, and the program's own integer arithmetic, 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, rather than as the program is." }
|
|
var contracts: maybe<contracting>;
|
|
@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;
|
|
@option{ value = "FILE", help = "A PNG or an icon file a Windows executable is shown by, as Explorer shows it; given again for each size. A PNG wider or taller than 256 pixels is left out." }
|
|
var icon: vector<strview>;
|
|
@option{ value = "TEXT", help = "What a Windows executable's properties call it, as Task Manager does, rather than its file's name." }
|
|
var title: strview;
|
|
@option{ value = "VERSION", help = "The version a Windows executable's properties give, as `1.2.0`." }
|
|
var semv: strview;
|
|
@option{ help = "Builds a Windows executable that runs in windows of its own, with no console." }
|
|
var windowed: bool;
|
|
@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 = contracts_left_out(c.contracts, !c.checks_report && (c.fast || (c.target is some(_) && !c.checks))),
|
|
checks_off = !c.checks_report && (c.fast || (c.target is some(_) && !c.checks)),
|
|
overflows = c.checks, reports = c.checks_report, opposed = c.checks && c.fast,
|
|
libraries = c.reading.libraries.span, deps = c.reading.deps, threads = threads_of(c.threads), files = c.files.span
|
|
}));
|
|
run(&r) => exit(work(&job {
|
|
runs = true, verify = r.verify, checks = r.checks, overflows = r.checks, contracts_off = contracts_left_out(r.contracts, r.fast),
|
|
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.span,
|
|
deps = r.reading.deps, threads = threads_of(r.threads), files = r.files.span, given = r.arguments.span
|
|
}));
|
|
build(&b) => exit(work(&job {
|
|
builds = true,
|
|
verify = b.verify,
|
|
debug = b.debug,
|
|
contracts_off = contracts_left_out(b.contracts, !b.checks),
|
|
checks_off = !b.checks,
|
|
overflows = 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.span,
|
|
deps = b.reading.deps,
|
|
output = b.output,
|
|
threads = threads_of(b.threads),
|
|
files = b.files.span,
|
|
icons = b.icon.span,
|
|
title = b.title,
|
|
semv = b.semv,
|
|
windowed = b.windowed
|
|
}));
|
|
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 overflows: bool; #! Whether integer arithmetic in the program's own files stops where it overflows, as `--checks` asks.
|
|
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.
|
|
var icons: span<strview>; #! The PNG and icon files a Windows executable is shown by.
|
|
var title: strview; #! What a Windows executable's properties call it, or empty.
|
|
var semv: strview; #! The version they give, or empty.
|
|
var windowed: bool; #! Whether a Windows executable runs in windows of its own.
|
|
}
|
|
|
|
#! 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.span) {
|
|
output.append(".exe");
|
|
}
|
|
# What a Windows executable is shown by is read first, so a file that can't be
|
|
# read stops the build before the program is compiled. An executable for
|
|
# Linux holds none of it: a desktop entry beside it names its icon there.
|
|
var icons := vector::<string>::new();
|
|
if asked.builds {
|
|
val dressed := asked.icons.length != 0 || asked.title.length != 0 || asked.semv.length != 0 || asked.windowed;
|
|
if dressed && asked.system != "windows" {
|
|
system::fd::stderr.put("sakura: `--icon`, `--title`, `--semv`, and `--windowed` are for Windows executables, so this one is built without them\n");
|
|
} else if !read_icons(asked.icons, &mut icons) {
|
|
return 2;
|
|
}
|
|
}
|
|
var icon_bytes := vector::<strview>::new();
|
|
for &each in icons {
|
|
icon_bytes.append(each.span);
|
|
}
|
|
# 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.read_for(asked.system, "x86_64", asked.builds || asked.native ? "native" : "vm");
|
|
finder.whole("core", core.span);
|
|
val platform := toolchain_folder("system");
|
|
if platform.length != 0 {
|
|
finder.add_library("system", platform.span);
|
|
}
|
|
if runtime.length != 0 {
|
|
finder.add_library("runtime", runtime.span);
|
|
}
|
|
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.span.index_of(&'=') else { continue; };
|
|
finder.add_library(each.span.substr(0, split), each.span.substr(split + 1, each.length - split - 1));
|
|
}
|
|
paths.append(string::from(builtin_path, nothing {}));
|
|
every_file(core.span, &mut paths);
|
|
for &each in paths {
|
|
finder.mark(each.span);
|
|
}
|
|
# The runtime's helpers are called by the compiler, which nothing written
|
|
# reaches, so an executable's runtime is checked whole, as the program's own
|
|
# files are; the libraries are checked where the program reaches them.
|
|
val own_files := asked.files.length;
|
|
val runtime_from := paths.length;
|
|
if asked.builds {
|
|
finder.want("runtime", &mut paths);
|
|
}
|
|
val runtime_to := paths.length;
|
|
|
|
var names := names::new();
|
|
var notes := vector::<note>::new();
|
|
var sources := vector::<source>::new();
|
|
var syntaxes := vector::<syntax>::new();
|
|
var roots := vector::<u32>::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. Each round of reading
|
|
# makes room for its files, which moves those before, so the thread
|
|
# reporting, which reads the files' sources, begins once every file is read,
|
|
# and what reading noted waits for it.
|
|
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;
|
|
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;
|
|
# A file of another target is read as far as its section's line.
|
|
var cuts := vector::<u64>::new();
|
|
for j in from..to {
|
|
cuts.append(finder.cut_of(paths[j].span));
|
|
}
|
|
sources.resize(to);
|
|
while syntaxes.length < to {
|
|
syntaxes.append(syntax::new());
|
|
}
|
|
share_out(to - from, asked.threads, def(k: u64): void {
|
|
val j := from + k;
|
|
if paths[j].span == builtin_path {
|
|
sources.set(j, source::from(builtin_path, builtin_text));
|
|
} else {
|
|
sources.set(j, source::open(paths[j].span, cuts[k]));
|
|
}
|
|
});
|
|
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;
|
|
}
|
|
val icons_read := icons.length != 0 ? asked.icons : span::<strview>::blank();
|
|
if asked.deps.length != 0 && !write_deps(asked.deps, paths.span, icons_read) {
|
|
system::fd::stderr.put($"sakura: the files read could not be written to {asked.deps}\n");
|
|
}
|
|
|
|
# Every file is read, so the thread reporting notes begins, with what reading
|
|
# noted waiting for it; where none begins, what waits is reported here.
|
|
if !alone && !start(def() { report_notes(&mut told, &sources, errors); }, &mut reporting) {
|
|
_ = told.send(news { ended = nothing {} });
|
|
report_notes(&mut told, &sources, errors);
|
|
alone = true;
|
|
}
|
|
|
|
# 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;
|
|
checker.whole = asked.all;
|
|
target_checker(&mut checker, asked.system, asked.builds || asked.native);
|
|
for j in 0..paths.length {
|
|
val own := j < own_files || j >= runtime_from && j < runtime_to;
|
|
checker.add(sources.at(j), syntaxes.at(j), roots.at(j), own);
|
|
}
|
|
|
|
# What only running gives is evaluated as checking asks for it, which notes
|
|
# what goes wrong as checking does, unless reading noted what was wrong, as a
|
|
# program that does not parse is not run.
|
|
var results := check_evaluating(&mut checker, noted.load() == 0);
|
|
|
|
# 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,
|
|
overflow_files = asked.overflows ? own_files as u32 : 0,
|
|
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 windows := asked.system == "windows";
|
|
val made := build_executable(&mut lowered, build_options {
|
|
output = output.span, verify = asked.verify, debug = asked.debug, windows = windows,
|
|
threads = asked.threads, icons = icon_bytes.span, title = windows ? asked.title : "",
|
|
semv = windows ? asked.semv : "", windowed = windows && asked.windowed
|
|
});
|
|
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.span != 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", "overflow"];
|
|
const checks_names: strview[checked::count as u64] = ["indexes", "union fields", "enum values", "faults", "overflows"];
|
|
|
|
#! 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.span);
|
|
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.span);
|
|
val found := found_std(asked.libraries.span);
|
|
var libraries := vector::<strview>::new();
|
|
for each in asked.libraries {
|
|
libraries.append(each);
|
|
}
|
|
for &each in found {
|
|
libraries.append(each.span);
|
|
}
|
|
return serve(server_options {
|
|
native = asked.target is some(_), system = system_of(asked.target), libraries = libraries.span
|
|
});
|
|
}
|
|
|
|
#! 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.span);
|
|
}
|
|
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.span);
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
#! Reads the files a Windows executable's icon is made of, as `--icon` names
|
|
#! them: each a PNG, kept whole, or an icon file. A PNG wider or taller than an
|
|
#! icon's image may be is left out, saying so, so one meant for Linux's larger
|
|
#! icons builds on Windows too; one that can't be read, or is no image an icon
|
|
#! holds, stops the build.
|
|
#! @param named The files.
|
|
#! @param into Where each kept is put, its bytes.
|
|
#! @returns Whether each could be read.
|
|
def read_icons(named: span<strview>, into: &mut vector<string>): bool {
|
|
for &each in named {
|
|
val at := path::parse(each) else (_) {
|
|
system::fd::stderr.put($"sakura: `{each}` is no path to an icon\n");
|
|
return false;
|
|
};
|
|
var bytes := io::read_file(&at) else (why) {
|
|
system::fd::stderr.put($"sakura: the icon {each} could not be read: {why}\n");
|
|
return false;
|
|
};
|
|
var probe := pe::icon_group::new();
|
|
val read := probe.add(bytes.span);
|
|
if read == pe::icon_read::unknown {
|
|
system::fd::stderr.put($"sakura: the icon {each} is neither a PNG nor an icon file\n");
|
|
return false;
|
|
}
|
|
if read == pe::icon_read::broken {
|
|
system::fd::stderr.put($"sakura: the icon {each} is cut short, or its images aren't where it says\n");
|
|
return false;
|
|
}
|
|
if read == pe::icon_read::too_large {
|
|
val size := pe::png_size(bytes.span);
|
|
system::fd::stderr.put($"sakura: the icon {each} is {size[0]}x{size[1]}, larger than a Windows icon's images, at most 256x256, so it's left out\n");
|
|
continue;
|
|
}
|
|
into.append(move(bytes));
|
|
}
|
|
return true;
|
|
}
|
|
|
|
#! 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.
|
|
#! @param also The paths of other files the build read, as its icons.
|
|
#! @returns Whether they were written.
|
|
def write_deps(at: strview, paths: span<string>, also: span<strview>): bool {
|
|
var text := string::new(nothing {});
|
|
for &each in paths {
|
|
continue if each.span == builtin_path;
|
|
text.append(each.span);
|
|
text.push('\n');
|
|
}
|
|
for &each in also {
|
|
text.append(each);
|
|
text.push('\n');
|
|
}
|
|
var found := path::parse(at) else (_) { return false; };
|
|
return io::write_file(&found, text.span) is value(_);
|
|
}
|