Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions bootstrap/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7405,6 +7405,19 @@ the parser used to read it as `{}` followed by a negation",
// Code Generator
// ============================================================================

/// The whole host syscall layer (Linux): open/read/write/close on a real fd, return the count or -errno.
const HOST_IO_ZIG: &str = r#"
const __t27_posix = @import("std").posix;
fn __t27_host_ret(r: usize) i32 { return @truncate(@as(isize, @bitCast(r))); }
fn __t27_host_open(path: [*:0]const u8, mode: i32) i32 {
const f: __t27_posix.O = if (mode != 0) .{ .ACCMODE = .WRONLY, .CREAT = true, .TRUNC = true } else .{};
return __t27_host_ret(__t27_posix.system.open(path, f, 0o644));
}
fn __t27_host_read(fd: i32, b: []u8) i32 { return __t27_host_ret(__t27_posix.system.read(fd, b.ptr, b.len)); }
fn __t27_host_write(fd: i32, b: []const u8) i32 { return __t27_host_ret(__t27_posix.system.write(fd, b.ptr, b.len)); }
fn __t27_host_close(fd: i32) i32 { return __t27_host_ret(__t27_posix.system.close(fd)); }
"#;

pub struct Codegen {
output: String,
indent: u32,
Expand Down Expand Up @@ -8290,6 +8303,12 @@ impl Codegen {
self.gen_decl(decl);
}
}
// `--backend ml`: the host's file syscalls, only for a spec that calls them.
let mut host = false;
Self::visit_calls(&ast.children, &mut |c| host |= matches!(c.name.as_str(), "host_open" | "host_read" | "host_write" | "host_close"));
if host {
self.write(HOST_IO_ZIG);
}
}

/// Generate Zig code with project-aware import resolution.
Expand Down Expand Up @@ -10448,6 +10467,10 @@ impl Codegen {
"sin" => Some("@sin"),
"cos" => Some("@cos"),
"tanh" => Some("std.math.tanh"),
"host_open" => Some("__t27_host_open"),
"host_read" => Some("__t27_host_read"),
"host_write" => Some("__t27_host_write"),
"host_close" => Some("__t27_host_close"),
_ => None,
};
if let Some(b) = builtin {
Expand Down
14 changes: 12 additions & 2 deletions bootstrap/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1336,7 +1336,7 @@ enum Commands {
Compile {
/// Input file path
input: String,
/// Backend: zig, verilog, or c
/// Backend: zig, verilog, c, or ml
#[arg(long, default_value = "zig")]
backend: String,
/// Output file path (default: input with backend extension)
Expand Down Expand Up @@ -6220,7 +6220,10 @@ fn compile_source(source: &str, backend: &str) -> Result<String, String> {

fn run_compile(input_path: &str, backend: &str, output: Option<&str>) -> anyhow::Result<()> {
let path = Path::new(input_path);
let source = fs::read_to_string(path)?;
let mut source = fs::read_to_string(path)?;
if backend == "ml" {
source = use_resolve::resolve(path, &source);
}

let ast = compiler::Compiler::parse_ast(&source)
.map_err(|e| anyhow::anyhow!("Parse error: {}", e))?;
Expand Down Expand Up @@ -6269,6 +6272,13 @@ fn run_compile(input_path: &str, backend: &str, output: Option<&str>) -> anyhow:
}
fs::write(&out_path, &code)?;
println!("wrote {}", out_path.display());
if backend == "ml" {
let exe = out_path.with_extension("");
let mut zig = std::process::Command::new("zig");
zig.arg("build-exe").arg(&out_path).arg(format!("-femit-bin={}", exe.display()));
anyhow::ensure!(zig.status()?.success(), "zig build-exe failed");
println!("built {}", exe.display());
}
Ok(())
}

Expand Down
Loading
Loading