Merge branch 'main' into virtual-dom-rendered-type

Signed-off-by: Brian Carroll <brian.carroll.ireland@gmail.com>
This commit is contained in:
Brian Carroll 2022-12-21 17:26:15 +00:00 committed by GitHub
commit 2c0e6bb21f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
84 changed files with 3448 additions and 3250 deletions

View File

@ -31,7 +31,7 @@ jobs:
run: cargo run --locked --release format --check crates/compiler/builtins/roc
- name: zig wasm tests
run: cargo build --release -p roc_wasm_interp && cd crates/compiler/builtins/bitcode && ./run-wasm-tests.sh
run: cd crates/compiler/builtins/bitcode && ./run-wasm-tests.sh
- name: regular rust tests
run: cargo test --locked --release && sccache --show-stats
@ -54,7 +54,6 @@ jobs:
- name: run `roc test` on Dict builtins
run: cargo run --locked --release -- test crates/compiler/builtins/roc/Dict.roc && sccache --show-stats
#TODO pass --locked into the script here as well, this avoids rebuilding dependencies unnecessarily
- name: wasm repl test
run: crates/repl_test/test_wasm.sh && sccache --show-stats

View File

@ -82,7 +82,6 @@ To build the compiler, you need these installed:
- On Debian/Ubuntu `sudo apt-get install pkg-config`
- LLVM, see below for version
- [rust](https://rustup.rs/)
- Also run `cargo install bindgen` after installing rust. You may need to open a new terminal.
To run the test suite (via `cargo test`), you additionally need to install:

895
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -22,7 +22,7 @@ i386-cli-run = ["target-x86"]
editor = ["roc_editor"]
run-wasm32 = ["wasmer", "wasmer-wasi"]
run-wasm32 = ["roc_wasm_interp"]
# Compiling for a different target than the current machine can cause linker errors.
target-arm = ["roc_build/target-arm", "roc_repl_cli/target-arm"]
@ -65,11 +65,10 @@ roc_repl_cli = { path = "../repl_cli", optional = true }
roc_tracing = { path = "../tracing" }
roc_intern = { path = "../compiler/intern" }
roc_gen_llvm = {path = "../compiler/gen_llvm"}
roc_wasm_interp = { path = "../wasm_interp", optional = true }
ven_pretty = { path = "../vendor/pretty" }
wasmer-wasi = { version = "2.2.1", optional = true }
clap.workspace = true
const_format.workspace = true
mimalloc.workspace = true
@ -88,15 +87,8 @@ inkwell.workspace = true
[target.'cfg(not(windows))'.dependencies]
roc_repl_expect = { path = "../repl_expect" }
# Wasmer singlepass compiler only works on x86_64.
[target.'cfg(target_arch = "x86_64")'.dependencies]
wasmer = { version = "2.2.1", optional = true, default-features = false, features = ["singlepass", "universal"] }
[target.'cfg(not(target_arch = "x86_64"))'.dependencies]
wasmer = { version = "2.2.1", optional = true, default-features = false, features = ["cranelift", "universal"] }
[dev-dependencies]
wasmer-wasi = "2.2.1"
pretty_assertions = "1.3.0"
roc_test_utils = { path = "../test_utils" }
roc_utils = { path = "../utils" }
@ -107,13 +99,6 @@ cli_utils = { path = "../cli_utils" }
once_cell = "1.15.0"
parking_lot = "0.12"
# Wasmer singlepass compiler only works on x86_64.
[target.'cfg(target_arch = "x86_64")'.dev-dependencies]
wasmer = { version = "2.2.1", default-features = false, features = ["singlepass", "universal"] }
[target.'cfg(not(target_arch = "x86_64"))'.dev-dependencies]
wasmer = { version = "2.2.1", default-features = false, features = ["cranelift", "universal"] }
[[bench]]
name = "time_bench"
harness = false

View File

@ -853,7 +853,7 @@ fn roc_run<'a, I: IntoIterator<Item = &'a OsStr>>(
{
use std::os::unix::ffi::OsStrExt;
run_with_wasmer(
run_wasm(
generated_filename,
args.into_iter().map(|os_str| os_str.as_bytes()),
);
@ -861,11 +861,11 @@ fn roc_run<'a, I: IntoIterator<Item = &'a OsStr>>(
#[cfg(not(target_family = "unix"))]
{
run_with_wasmer(
run_wasm(
generated_filename,
args.into_iter().map(|os_str| {
os_str.to_str().expect(
"Roc does not currently support passing non-UTF8 arguments to Wasmer.",
"Roc does not currently support passing non-UTF8 arguments to Wasm.",
)
}),
);
@ -1239,38 +1239,33 @@ fn roc_run_native<I: IntoIterator<Item = S>, S: AsRef<OsStr>>(
}
#[cfg(feature = "run-wasm32")]
fn run_with_wasmer<I: Iterator<Item = S>, S: AsRef<[u8]>>(wasm_path: &std::path::Path, args: I) {
use wasmer::{Instance, Module, Store};
fn run_wasm<I: Iterator<Item = S>, S: AsRef<[u8]>>(wasm_path: &std::path::Path, args: I) {
use bumpalo::collections::Vec;
use roc_wasm_interp::{DefaultImportDispatcher, Instance};
let store = Store::default();
let module = Module::from_file(&store, &wasm_path).unwrap();
let bytes = std::fs::read(wasm_path).unwrap();
let arena = Bump::new();
// First, we create the `WasiEnv`
use wasmer_wasi::WasiState;
let mut wasi_env = WasiState::new("hello").args(args).finalize().unwrap();
// Then, we get the import object related to our WASI
// and attach it to the Wasm instance.
let import_object = wasi_env.import_object(&module).unwrap();
let instance = Instance::new(&module, &import_object).unwrap();
let start = instance.exports.get_function("_start").unwrap();
use wasmer_wasi::WasiError;
match start.call(&[]) {
Ok(_) => {}
Err(e) => match e.downcast::<WasiError>() {
Ok(WasiError::Exit(0)) => {
// we run the `_start` function, so exit(0) is expected
}
other => panic!("Wasmer error: {:?}", other),
},
let mut argv = Vec::<&[u8]>::new_in(&arena);
for arg in args {
let mut arg_copy = Vec::<u8>::new_in(&arena);
arg_copy.extend_from_slice(arg.as_ref());
argv.push(arg_copy.into_bump_slice());
}
let import_dispatcher = DefaultImportDispatcher::new(&argv);
let mut instance = Instance::from_bytes(&arena, &bytes, import_dispatcher, false).unwrap();
instance
.call_export("_start", [])
.unwrap()
.unwrap()
.expect_i32()
.unwrap();
}
#[cfg(not(feature = "run-wasm32"))]
fn run_with_wasmer<I: Iterator<Item = S>, S: AsRef<[u8]>>(_wasm_path: &std::path::Path, _args: I) {
fn run_wasm<I: Iterator<Item = S>, S: AsRef<[u8]>>(_wasm_path: &std::path::Path, _args: I) {
println!("Running wasm files is not supported on this target.");
}

View File

@ -117,9 +117,10 @@ mod cli_run {
assert!(
compile_out.status.success(),
"\n___________\nRoc command failed with status {:?}:\n\n {:?}\n___________\n",
"\n___________\nRoc command failed with status {:?}:\n\n {} {}\n___________\n",
compile_out.status,
compile_out
compile_out.stdout,
compile_out.stderr,
);
compile_out
@ -560,7 +561,7 @@ mod cli_run {
test_roc_app(
"crates/cli_testing_examples/expects",
"expects.roc",
"expects",
"expects-test",
&[],
&[],
&[],
@ -591,7 +592,7 @@ mod cli_run {
test_roc_app(
"crates/cli_testing_examples/expects",
"expects.roc",
"expects",
"expects-test",
&[],
&[],
&[],
@ -612,7 +613,7 @@ mod cli_run {
b : Num *
b = 2
1 failed and 0 passed in <ignored for test> ms."#
@ -1021,7 +1022,7 @@ mod cli_run {
let mut path = file.with_file_name(executable_filename);
path.set_extension("wasm");
let stdout = crate::run_with_wasmer(&path, stdin);
let stdout = crate::run_wasm(&path, stdin);
if !stdout.ends_with(expected_ending) {
panic!(
@ -1037,14 +1038,17 @@ mod cli_run {
stdin: &[&str],
executable_filename: &str,
expected_ending: &str,
use_valgrind: bool,
use_valgrind: UseValgrind,
) {
use super::{concatcp, CMD_BUILD, TARGET_FLAG};
check_output_with_stdin(
&file_name,
stdin,
executable_filename,
&[concatcp!(TARGET_FLAG, "=x86_32")],
&[],
&[],
expected_ending,
use_valgrind,
TestCliCommands::Run,
@ -1056,6 +1060,7 @@ mod cli_run {
executable_filename,
&[concatcp!(TARGET_FLAG, "=x86_32"), OPTIMIZE_FLAG],
&[],
&[],
expected_ending,
use_valgrind,
TestCliCommands::Run,
@ -1246,6 +1251,40 @@ mod cli_run {
);
}
#[test]
#[serial(multi_dep_thunk)]
#[cfg_attr(windows, ignore)]
fn run_packages_unoptimized() {
check_output_with_stdin(
&fixture_file("packages", "app.roc"),
&[],
"packages-test",
&[],
&[],
&[],
"Hello, World! This text came from a package! This text came from a CSV package!\n",
UseValgrind::Yes,
TestCliCommands::Run,
);
}
#[test]
#[serial(multi_dep_thunk)]
#[cfg_attr(windows, ignore)]
fn run_packages_optimized() {
check_output_with_stdin(
&fixture_file("packages", "app.roc"),
&[],
"packages-test",
&[OPTIMIZE_FLAG],
&[],
&[],
"Hello, World! This text came from a package! This text came from a CSV package!\n",
UseValgrind::Yes,
TestCliCommands::Run,
);
}
#[test]
fn known_type_error() {
check_compile_error(
@ -1370,75 +1409,49 @@ mod cli_run {
}
}
#[allow(dead_code)]
fn run_with_wasmer(wasm_path: &std::path::Path, stdin: &[&str]) -> String {
use std::io::Write;
use wasmer::{Instance, Module, Store};
#[cfg(feature = "wasm32-cli-run")]
fn run_wasm(wasm_path: &std::path::Path, stdin: &[&str]) -> String {
use bumpalo::Bump;
use roc_wasm_interp::{DefaultImportDispatcher, Instance, Value, WasiFile};
// std::process::Command::new("cp")
// .args(&[
// wasm_path.to_str().unwrap(),
// "/home/folkertdev/roc/wasm/nqueens.wasm",
// ])
// .output()
// .unwrap();
let wasm_bytes = std::fs::read(wasm_path).unwrap();
let arena = Bump::new();
let store = Store::default();
let module = Module::from_file(&store, wasm_path).unwrap();
let mut instance = {
let mut fake_stdin = vec![];
let fake_stdout = vec![];
let fake_stderr = vec![];
for s in stdin {
fake_stdin.extend_from_slice(s.as_bytes())
}
let mut fake_stdin = wasmer_wasi::Pipe::new();
let fake_stdout = wasmer_wasi::Pipe::new();
let fake_stderr = wasmer_wasi::Pipe::new();
let mut dispatcher = DefaultImportDispatcher::default();
dispatcher.wasi.files = vec![
WasiFile::ReadOnly(fake_stdin),
WasiFile::WriteOnly(fake_stdout),
WasiFile::WriteOnly(fake_stderr),
];
for line in stdin {
write!(fake_stdin, "{}", line).unwrap();
}
Instance::from_bytes(&arena, &wasm_bytes, dispatcher, false).unwrap()
};
// First, we create the `WasiEnv`
use wasmer_wasi::WasiState;
let mut wasi_env = WasiState::new("hello")
.stdin(Box::new(fake_stdin))
.stdout(Box::new(fake_stdout))
.stderr(Box::new(fake_stderr))
.finalize()
.unwrap();
let result = instance.call_export("_start", []);
// Then, we get the import object related to our WASI
// and attach it to the Wasm instance.
let import_object = wasi_env
.import_object(&module)
.unwrap_or_else(|_| wasmer::imports!());
let instance = Instance::new(&module, &import_object).unwrap();
let start = instance.exports.get_function("_start").unwrap();
match start.call(&[]) {
Ok(_) => read_wasi_stdout(wasi_env),
match result {
Ok(Some(Value::I32(0))) => match &instance.import_dispatcher.wasi.files[1] {
WasiFile::WriteOnly(fake_stdout) => String::from_utf8(fake_stdout.clone())
.unwrap_or_else(|_| "Wasm test printed invalid UTF-8".into()),
_ => unreachable!(),
},
Ok(Some(Value::I32(exit_code))) => {
format!("WASI app exit code {}", exit_code)
}
Ok(Some(val)) => {
format!("WASI _start returned an unexpected number type {:?}", val)
}
Ok(None) => "WASI _start returned no value".into(),
Err(e) => {
use wasmer_wasi::WasiError;
match e.downcast::<WasiError>() {
Ok(WasiError::Exit(0)) => {
// we run the `_start` function, so exit(0) is expected
read_wasi_stdout(wasi_env)
}
other => format!("Something went wrong running a wasm test: {:?}", other),
}
format!("WASI error {}", e)
}
}
}
#[allow(dead_code)]
fn read_wasi_stdout(wasi_env: wasmer_wasi::WasiEnv) -> String {
let mut state = wasi_env.state.lock().unwrap();
match state.fs.stdout_mut() {
Ok(Some(stdout)) => {
let mut buf = String::new();
stdout.read_to_string(&mut buf).unwrap();
buf
}
_ => todo!(),
}
}

View File

@ -5,3 +5,4 @@ dynhost
libapp.so
metadata
preprocessedhost
packages-test

View File

@ -0,0 +1,6 @@
app "packages-test"
packages { pf: "platform/main.roc", json: "json/main.roc", csv: "csv/main.roc" }
imports [json.JsonParser, csv.Csv]
provides [main] to pf
main = "Hello, World! \(JsonParser.example) \(Csv.example)"

View File

@ -0,0 +1,6 @@
interface Csv
exposes [example]
imports []
example : Str
example = "This text came from a CSV package!"

View File

@ -0,0 +1,3 @@
package "csv"
exposes [Csv]
packages {}

View File

@ -0,0 +1,6 @@
interface JsonParser
exposes [example]
imports []
example : Str
example = "This text came from a package!"

View File

@ -0,0 +1,3 @@
package "json"
exposes [JsonParser]
packages {}

View File

@ -0,0 +1,127 @@
const std = @import("std");
const builtin = @import("builtin");
const str = @import("str");
const RocStr = str.RocStr;
const testing = std.testing;
const expectEqual = testing.expectEqual;
const expect = testing.expect;
comptime {
// This is a workaround for https://github.com/ziglang/zig/issues/8218
// which is only necessary on macOS.
//
// Once that issue is fixed, we can undo the changes in
// 177cf12e0555147faa4d436e52fc15175c2c4ff0 and go back to passing
// -fcompiler-rt in link.rs instead of doing this. Note that this
// workaround is present in many host.zig files, so make sure to undo
// it everywhere!
if (builtin.os.tag == .macos) {
_ = @import("compiler_rt");
}
}
const mem = std.mem;
const Allocator = mem.Allocator;
extern fn roc__mainForHost_1_exposed_generic(*RocStr) void;
const Align = 2 * @alignOf(usize);
extern fn malloc(size: usize) callconv(.C) ?*align(Align) anyopaque;
extern fn realloc(c_ptr: [*]align(Align) u8, size: usize) callconv(.C) ?*anyopaque;
extern fn free(c_ptr: [*]align(Align) u8) callconv(.C) void;
extern fn memcpy(dst: [*]u8, src: [*]u8, size: usize) callconv(.C) void;
extern fn memset(dst: [*]u8, value: i32, size: usize) callconv(.C) void;
export fn roc_alloc(size: usize, alignment: u32) callconv(.C) ?*anyopaque {
_ = alignment;
return malloc(size);
}
export fn roc_realloc(c_ptr: *anyopaque, new_size: usize, old_size: usize, alignment: u32) callconv(.C) ?*anyopaque {
_ = old_size;
_ = alignment;
return realloc(@alignCast(16, @ptrCast([*]u8, c_ptr)), new_size);
}
export fn roc_dealloc(c_ptr: *anyopaque, alignment: u32) callconv(.C) void {
_ = alignment;
free(@alignCast(16, @ptrCast([*]u8, c_ptr)));
}
export fn roc_memcpy(dst: [*]u8, src: [*]u8, size: usize) callconv(.C) void {
return memcpy(dst, src, size);
}
export fn roc_memset(dst: [*]u8, value: i32, size: usize) callconv(.C) void {
return memset(dst, value, size);
}
export fn roc_panic(c_ptr: *anyopaque, tag_id: u32) callconv(.C) void {
_ = tag_id;
const stderr = std.io.getStdErr().writer();
const msg = @ptrCast([*:0]const u8, c_ptr);
stderr.print("Application crashed with message\n\n {s}\n\nShutting down\n", .{msg}) catch unreachable;
std.process.exit(0);
}
extern fn kill(pid: c_int, sig: c_int) c_int;
extern fn shm_open(name: *const i8, oflag: c_int, mode: c_uint) c_int;
extern fn mmap(addr: ?*anyopaque, length: c_uint, prot: c_int, flags: c_int, fd: c_int, offset: c_uint) *anyopaque;
extern fn getppid() c_int;
fn roc_getppid() callconv(.C) c_int {
return getppid();
}
fn roc_getppid_windows_stub() callconv(.C) c_int {
return 0;
}
fn roc_shm_open(name: *const i8, oflag: c_int, mode: c_uint) callconv(.C) c_int {
return shm_open(name, oflag, mode);
}
fn roc_mmap(addr: ?*anyopaque, length: c_uint, prot: c_int, flags: c_int, fd: c_int, offset: c_uint) callconv(.C) *anyopaque {
return mmap(addr, length, prot, flags, fd, offset);
}
comptime {
if (builtin.os.tag == .macos or builtin.os.tag == .linux) {
@export(roc_getppid, .{ .name = "roc_getppid", .linkage = .Strong });
@export(roc_mmap, .{ .name = "roc_mmap", .linkage = .Strong });
@export(roc_shm_open, .{ .name = "roc_shm_open", .linkage = .Strong });
}
if (builtin.os.tag == .windows) {
@export(roc_getppid_windows_stub, .{ .name = "roc_getppid", .linkage = .Strong });
}
}
const Unit = extern struct {};
pub export fn main() i32 {
const stdout = std.io.getStdOut().writer();
const stderr = std.io.getStdErr().writer();
var timer = std.time.Timer.start() catch unreachable;
// actually call roc to populate the callresult
var callresult = RocStr.empty();
roc__mainForHost_1_exposed_generic(&callresult);
const nanos = timer.read();
const seconds = (@intToFloat(f64, nanos) / 1_000_000_000.0);
// stdout the result
stdout.print("{s}\n", .{callresult.asSlice()}) catch unreachable;
callresult.deinit();
stderr.print("runtime: {d:.3}ms\n", .{seconds * 1000}) catch unreachable;
return 0;
}
fn to_seconds(tms: std.os.timespec) f64 {
return @intToFloat(f64, tms.tv_sec) + (@intToFloat(f64, tms.tv_nsec) / 1_000_000_000.0);
}

View File

@ -0,0 +1,9 @@
platform "multi-module"
requires {}{ main : Str }
exposes []
packages {}
imports []
provides [mainForHost]
mainForHost : Str
mainForHost = main

View File

@ -4,4 +4,4 @@ libapp.so
dynhost
preprocessedhost
metadata
expects
expects-test

View File

@ -1,4 +1,4 @@
app "expects"
app "expects-test"
packages { pf: "zig-platform/main.roc" }
imports []
provides [main] to pf

View File

@ -3,9 +3,6 @@
# https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/
set -euxo pipefail
# Test failures will always point at the _start function
# Make sure to look at the rest of the stack trace!
# Zig will try to run the test binary it produced, but since your OS doesn't know how to
# run Wasm binaries natively, we need to provide a Wasm interpreter as a "test command".
# For non-native binaries, Zig test needs a "test command" it can use
cargo build --locked --release -p roc_wasm_interp
zig test -target wasm32-wasi-musl -O ReleaseFast src/main.zig --test-cmd ../../../../target/release/roc_wasm_interp --test-cmd-bin

View File

@ -52,7 +52,7 @@ pub const RocStr = extern struct {
// small string, and returns a (pointer, len) tuple which points to them.
pub fn init(bytes_ptr: [*]const u8, length: usize) RocStr {
var result = RocStr.allocate(length);
@memcpy(result.asU8ptr(), bytes_ptr, length);
@memcpy(result.asU8ptrMut(), bytes_ptr, length);
return result;
}
@ -83,7 +83,7 @@ pub const RocStr = extern struct {
} else {
var string = RocStr.empty();
string.asU8ptr()[@sizeOf(RocStr) - 1] = @intCast(u8, length) | 0b1000_0000;
string.asU8ptrMut()[@sizeOf(RocStr) - 1] = @intCast(u8, length) | 0b1000_0000;
return string;
}
@ -190,12 +190,12 @@ pub const RocStr = extern struct {
const old_length = self.len();
const delta_length = new_length - old_length;
const result = RocStr.allocate(new_length);
var result = RocStr.allocate(new_length);
// transfer the memory
const source_ptr = self.asU8ptr();
const dest_ptr = result.asU8ptr();
const dest_ptr = result.asU8ptrMut();
@memcpy(dest_ptr, source_ptr, old_length);
@memset(dest_ptr + old_length, 0, delta_length);
@ -230,7 +230,7 @@ pub const RocStr = extern struct {
pub fn setLen(self: *RocStr, length: usize) void {
if (self.isSmallStr()) {
self.asU8ptr()[@sizeOf(RocStr) - 1] = @intCast(u8, length) | 0b1000_0000;
self.asU8ptrMut()[@sizeOf(RocStr) - 1] = @intCast(u8, length) | 0b1000_0000;
} else {
self.str_len = length;
}
@ -320,23 +320,29 @@ pub const RocStr = extern struct {
return (ptr - 1)[0] == utils.REFCOUNT_ONE;
}
pub fn asSlice(self: RocStr) []u8 {
pub fn asSlice(self: *const RocStr) []const u8 {
return self.asU8ptr()[0..self.len()];
}
pub fn asSliceWithCapacity(self: RocStr) []u8 {
pub fn asSliceWithCapacity(self: *const RocStr) []const u8 {
return self.asU8ptr()[0..self.getCapacity()];
}
pub fn asU8ptr(self: RocStr) [*]u8 {
pub fn asSliceWithCapacityMut(self: *RocStr) []u8 {
return self.asU8ptrMut()[0..self.getCapacity()];
}
// Since this conditional would be prone to branch misprediction,
// make sure it will compile to a cmov.
// return if (self.isSmallStr()) (&@bitCast([@sizeOf(RocStr)]u8, self)) else (@ptrCast([*]u8, self.str_bytes));
pub fn asU8ptr(self: *const RocStr) [*]const u8 {
if (self.isSmallStr()) {
const as_int = @ptrToInt(&self);
const as_ptr = @intToPtr([*]u8, as_int);
return as_ptr;
return @ptrCast([*]const u8, self);
} else {
return @ptrCast([*]const u8, self.str_bytes);
}
}
pub fn asU8ptrMut(self: *RocStr) [*]u8 {
if (self.isSmallStr()) {
return @ptrCast([*]u8, self);
} else {
return @ptrCast([*]u8, self.str_bytes);
}
@ -1402,7 +1408,7 @@ pub fn repeat(string: RocStr, count: usize) callconv(.C) RocStr {
const bytes_ptr = string.asU8ptr();
var ret_string = RocStr.allocate(count * bytes_len);
var ret_string_ptr = ret_string.asU8ptr();
var ret_string_ptr = ret_string.asU8ptrMut();
var i: usize = 0;
while (i < count) : (i += 1) {
@ -1542,9 +1548,8 @@ fn strConcat(arg1: RocStr, arg2: RocStr) RocStr {
} else {
const combined_length = arg1.len() + arg2.len();
const result = arg1.reallocate(combined_length);
@memcpy(result.asU8ptr() + arg1.len(), arg2.asU8ptr(), arg2.len());
var result = arg1.reallocate(combined_length);
@memcpy(result.asU8ptrMut() + arg1.len(), arg2.asU8ptr(), arg2.len());
return result;
}
@ -1615,7 +1620,7 @@ fn strJoinWith(list: RocListStr, separator: RocStr) RocStr {
total_size += separator.len() * (len - 1);
var result = RocStr.allocate(total_size);
var result_ptr = result.asU8ptr();
var result_ptr = result.asU8ptrMut();
var offset: usize = 0;
for (slice[0 .. len - 1]) |substr| {
@ -2534,7 +2539,7 @@ pub fn appendScalar(string: RocStr, scalar_u32: u32) callconv(.C) RocStr {
const width = std.unicode.utf8CodepointSequenceLength(scalar) catch unreachable;
var output = string.reallocate(string.len() + width);
var slice = output.asSliceWithCapacity();
var slice = output.asSliceWithCapacityMut();
_ = std.unicode.utf8Encode(scalar, slice[string.len() .. string.len() + width]) catch unreachable;

View File

@ -205,6 +205,7 @@ impl GeneratedInfo {
generates,
generates_with,
name: _,
exposes: _,
} => {
let name: &str = generates.into();
let (generated_functions, unknown_generated) =
@ -240,6 +241,7 @@ impl GeneratedInfo {
HeaderType::Builtin {
generates_with,
name: _,
exposes: _,
} => {
debug_assert!(generates_with.is_empty());
GeneratedInfo::Builtin

View File

@ -8,8 +8,8 @@ use bumpalo::Bump;
use roc_parse::ast::{Collection, Header, Module, Spaced, Spaces};
use roc_parse::header::{
AppHeader, ExposedName, ExposesKeyword, GeneratesKeyword, HostedHeader, ImportsEntry,
ImportsKeyword, InterfaceHeader, Keyword, KeywordItem, ModuleName, PackageEntry,
PackageKeyword, PackagePath, PackagesKeyword, PlatformHeader, PlatformRequires,
ImportsKeyword, InterfaceHeader, Keyword, KeywordItem, ModuleName, PackageEntry, PackageHeader,
PackageKeyword, PackageName, PackagesKeyword, PlatformHeader, PlatformRequires,
ProvidesKeyword, ProvidesTo, RequiresKeyword, To, ToKeyword, TypedIdent, WithKeyword,
};
use roc_parse::ident::UppercaseIdent;
@ -24,6 +24,9 @@ pub fn fmt_module<'a>(buf: &mut Buf<'_>, module: &'a Module<'a>) {
Header::App(header) => {
fmt_app_header(buf, header);
}
Header::Package(header) => {
fmt_package_header(buf, header);
}
Header::Platform(header) => {
fmt_platform_header(buf, header);
}
@ -226,6 +229,20 @@ pub fn fmt_app_header<'a, 'buf>(buf: &mut Buf<'buf>, header: &'a AppHeader<'a>)
header.provides.format(buf, indent);
}
pub fn fmt_package_header<'a, 'buf>(buf: &mut Buf<'buf>, header: &'a PackageHeader<'a>) {
buf.indent(0);
buf.push_str("package");
let indent = INDENT;
fmt_default_spaces(buf, header.before_name, indent);
fmt_package_name(buf, header.name.value, indent);
header.exposes.keyword.format(buf, indent);
fmt_exposes(buf, header.exposes.item, indent);
header.packages.keyword.format(buf, indent);
fmt_packages(buf, header.packages.item, indent);
}
pub fn fmt_platform_header<'a, 'buf>(buf: &mut Buf<'buf>, header: &'a PlatformHeader<'a>) {
buf.indent(0);
buf.push_str("platform");
@ -276,7 +293,7 @@ impl<'a> Formattable for TypedIdent<'a> {
}
}
fn fmt_package_name<'buf>(buf: &mut Buf<'buf>, name: PackagePath, _indent: u16) {
fn fmt_package_name<'buf>(buf: &mut Buf<'buf>, name: PackageName, _indent: u16) {
buf.push('"');
buf.push_str_allow_spaces(name.to_str());
buf.push('"');
@ -453,7 +470,7 @@ fn fmt_packages_entry<'a, 'buf>(buf: &mut Buf<'buf>, entry: &PackageEntry<'a>, i
buf.push_str(entry.shorthand);
buf.push(':');
fmt_default_spaces(buf, entry.spaces_after_shorthand, indent);
fmt_package_name(buf, entry.package_path.value, indent);
fmt_package_name(buf, entry.package_name.value, indent);
}
fn fmt_imports_entry<'a, 'buf>(buf: &mut Buf<'buf>, entry: &ImportsEntry<'a>, indent: u16) {

View File

@ -9,8 +9,8 @@ use roc_parse::{
},
header::{
AppHeader, ExposedName, HostedHeader, ImportsEntry, InterfaceHeader, KeywordItem,
ModuleName, PackageEntry, PackagePath, PlatformHeader, PlatformRequires, ProvidesTo, To,
TypedIdent,
ModuleName, PackageEntry, PackageHeader, PackageName, PlatformHeader, PlatformRequires,
ProvidesTo, To, TypedIdent,
},
ident::UppercaseIdent,
};
@ -290,6 +290,12 @@ impl<'a> RemoveSpaces<'a> for Module<'a> {
imports: header.imports.remove_spaces(arena),
provides: header.provides.remove_spaces(arena),
}),
Header::Package(header) => Header::Package(PackageHeader {
before_name: &[],
name: header.name.remove_spaces(arena),
exposes: header.exposes.remove_spaces(arena),
packages: header.packages.remove_spaces(arena),
}),
Header::Platform(header) => Header::Platform(PlatformHeader {
before_name: &[],
name: header.name.remove_spaces(arena),
@ -349,7 +355,7 @@ impl<'a> RemoveSpaces<'a> for ModuleName<'a> {
}
}
impl<'a> RemoveSpaces<'a> for PackagePath<'a> {
impl<'a> RemoveSpaces<'a> for PackageName<'a> {
fn remove_spaces(&self, _arena: &'a Bump) -> Self {
*self
}
@ -394,7 +400,7 @@ impl<'a> RemoveSpaces<'a> for PackageEntry<'a> {
PackageEntry {
shorthand: self.shorthand,
spaces_after_shorthand: &[],
package_path: self.package_path.remove_spaces(arena),
package_name: self.package_name.remove_spaces(arena),
}
}
}

View File

@ -9,7 +9,7 @@ use crate::llvm::refcounting::{
decrement_refcount_layout, increment_n_refcount_layout, increment_refcount_layout,
};
use inkwell::attributes::{Attribute, AttributeLoc};
use inkwell::types::{BasicType, BasicTypeEnum};
use inkwell::types::{BasicType, BasicTypeEnum, StructType};
use inkwell::values::{
BasicValue, BasicValueEnum, CallSiteValue, FunctionValue, InstructionValue, IntValue,
PointerValue, StructValue,
@ -19,9 +19,8 @@ use roc_error_macros::internal_error;
use roc_module::symbol::Symbol;
use roc_mono::layout::{Builtin, LambdaSet, Layout, LayoutIds};
use super::build::create_entry_block_alloca;
use std::convert::TryInto;
use super::build::{create_entry_block_alloca, BuilderExt};
use super::convert::zig_list_type;
pub fn call_bitcode_fn<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
@ -96,6 +95,7 @@ fn call_bitcode_fn_help<'a, 'ctx, 'env>(
pub fn call_bitcode_fn_fixing_for_convention<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
bitcode_return_type: StructType<'ctx>,
args: &[BasicValueEnum<'ctx>],
return_layout: &Layout<'_>,
fn_name: &str,
@ -111,17 +111,7 @@ pub fn call_bitcode_fn_fixing_for_convention<'a, 'ctx, 'env>(
// We need to pass the return value by pointer.
let roc_return_type = basic_type_from_layout(env, return_layout);
let cc_ptr_return_type = env
.module
.get_function(fn_name)
.unwrap()
.get_type()
.get_param_types()[0]
.into_pointer_type();
let cc_return_type: BasicTypeEnum<'ctx> = cc_ptr_return_type
.get_element_type()
.try_into()
.expect("Zig bitcode return type is not a basic type!");
let cc_return_type: BasicTypeEnum<'ctx> = bitcode_return_type.into();
// when we write an i128 into this (happens in NumToInt), zig expects this pointer to
// be 16-byte aligned. Not doing so is UB and will immediately fail on CI
@ -139,7 +129,9 @@ pub fn call_bitcode_fn_fixing_for_convention<'a, 'ctx, 'env>(
.collect();
call_void_bitcode_fn(env, &fixed_args, fn_name);
let cc_return_value = env.builder.build_load(cc_return_value_ptr, "read_result");
let cc_return_value =
env.builder
.new_build_load(cc_return_type, cc_return_value_ptr, "read_result");
if roc_return_type.size_of() == cc_return_type.size_of() {
cc_return_value
} else {
@ -392,8 +384,8 @@ fn build_rc_wrapper<'a, 'ctx, 'env>(
generic_value_ptr.set_name(Symbol::ARG_1.as_str(&env.interns));
let value_ptr_type =
basic_type_from_layout(env, layout).ptr_type(AddressSpace::Generic);
let value_type = basic_type_from_layout(env, layout);
let value_ptr_type = value_type.ptr_type(AddressSpace::Generic);
let value_ptr =
env.builder
.build_pointer_cast(generic_value_ptr, value_ptr_type, "load_opaque");
@ -404,7 +396,8 @@ fn build_rc_wrapper<'a, 'ctx, 'env>(
let value = if layout.is_passed_by_reference(env.layout_interner, env.target_info) {
value_ptr.into()
} else {
env.builder.build_load(value_ptr, "load_opaque")
env.builder
.new_build_load(value_type, value_ptr, "load_opaque")
};
match rc_operation {
@ -573,8 +566,12 @@ pub fn build_compare_wrapper<'a, 'ctx, 'env>(
env.builder
.build_pointer_cast(value_ptr2, value_ptr_type, "load_opaque");
let value1 = env.builder.build_load(value_cast1, "load_opaque");
let value2 = env.builder.build_load(value_cast2, "load_opaque");
let value1 = env
.builder
.new_build_load(value_type, value_cast1, "load_opaque");
let value2 = env
.builder
.new_build_load(value_type, value_cast2, "load_opaque");
let default = [value1.into(), value2.into()];
@ -596,7 +593,9 @@ pub fn build_compare_wrapper<'a, 'ctx, 'env>(
"load_opaque",
);
let closure_data = env.builder.build_load(closure_cast, "load_opaque");
let closure_data =
env.builder
.new_build_load(closure_type, closure_cast, "load_opaque");
env.arena
.alloc([value1.into(), value2.into(), closure_data.into()])
@ -644,7 +643,8 @@ impl<'ctx> BitcodeReturnValue<'ctx> {
match self {
BitcodeReturnValue::List(result) => {
call_void_bitcode_fn(env, arguments, fn_name);
env.builder.build_load(*result, "load_list")
env.builder
.new_build_load(zig_list_type(env), *result, "load_list")
}
BitcodeReturnValue::Str(result) => {
call_void_bitcode_fn(env, arguments, fn_name);

View File

@ -53,13 +53,82 @@ use std::convert::TryInto;
use std::path::Path;
use target_lexicon::{Architecture, OperatingSystem, Triple};
use super::convert::RocUnion;
use super::convert::{struct_type_from_union_layout, RocUnion};
use super::intrinsics::{
add_intrinsics, LLVM_FRAME_ADDRESS, LLVM_MEMSET_I32, LLVM_MEMSET_I64, LLVM_SETJMP,
LLVM_STACK_SAVE,
};
use super::lowlevel::run_higher_order_low_level;
pub(crate) trait BuilderExt<'ctx> {
fn new_build_struct_gep(
&self,
struct_type: StructType<'ctx>,
ptr: PointerValue<'ctx>,
index: u32,
name: &str,
) -> Result<PointerValue<'ctx>, ()>;
fn new_build_load(
&self,
element_type: impl BasicType<'ctx>,
ptr: PointerValue<'ctx>,
name: &str,
) -> BasicValueEnum<'ctx>;
unsafe fn new_build_in_bounds_gep(
&self,
element_type: impl BasicType<'ctx>,
ptr: PointerValue<'ctx>,
ordered_indexes: &[IntValue<'ctx>],
name: &str,
) -> PointerValue<'ctx>;
}
impl<'ctx> BuilderExt<'ctx> for Builder<'ctx> {
fn new_build_struct_gep(
&self,
struct_type: StructType<'ctx>,
ptr: PointerValue<'ctx>,
index: u32,
name: &str,
) -> Result<PointerValue<'ctx>, ()> {
debug_assert_eq!(
ptr.get_type().get_element_type().into_struct_type(),
struct_type
);
self.build_struct_gep(ptr, index, name)
}
fn new_build_load(
&self,
element_type: impl BasicType<'ctx>,
ptr: PointerValue<'ctx>,
name: &str,
) -> BasicValueEnum<'ctx> {
debug_assert_eq!(
ptr.get_type().get_element_type(),
element_type.as_any_type_enum()
);
self.build_load(ptr, name)
}
unsafe fn new_build_in_bounds_gep(
&self,
element_type: impl BasicType<'ctx>,
ptr: PointerValue<'ctx>,
ordered_indexes: &[IntValue<'ctx>],
name: &str,
) -> PointerValue<'ctx> {
debug_assert_eq!(
ptr.get_type().get_element_type(),
element_type.as_any_type_enum()
);
self.build_in_bounds_gep(ptr, ordered_indexes, name)
}
}
#[inline(always)]
fn print_fn_verification_output() -> bool {
dbg_do!(ROC_PRINT_LLVM_FN_VERIFICATION, {
@ -773,7 +842,10 @@ fn build_string_literal<'a, 'ctx, 'env>(
let alloca = const_str_alloca_ptr(env, parent, ptr, number_of_elements, number_of_elements);
match env.target_info.ptr_width() {
PtrWidth::Bytes4 => env.builder.build_load(alloca, "load_const_str"),
PtrWidth::Bytes4 => {
env.builder
.new_build_load(zig_str_type(env), alloca, "load_const_str")
}
PtrWidth::Bytes8 => alloca.into(),
}
}
@ -977,7 +1049,7 @@ fn struct_pointer_from_fields<'a, 'ctx, 'env, I>(
for (index, (field_layout, field_value)) in values {
let field_ptr = env
.builder
.build_struct_gep(struct_ptr, index as u32, "field_struct_gep")
.new_build_struct_gep(struct_type, struct_ptr, index as u32, "field_struct_gep")
.unwrap();
store_roc_value(env, field_layout, field_ptr, field_value);
@ -1157,30 +1229,6 @@ pub fn build_exp_expr<'a, 'ctx, 'env>(
let field_layout = field_layouts[*index as usize];
use_roc_value(env, field_layout, field_value, "struct_field_tag")
}
(
PointerValue(argument),
Layout::Union(UnionLayout::NonNullableUnwrapped(fields)),
) => {
let struct_layout = Layout::struct_no_name_order(fields);
let struct_type = basic_type_from_layout(env, &struct_layout);
let cast_argument = env.builder.build_pointer_cast(
argument,
struct_type.ptr_type(AddressSpace::Generic),
"cast_rosetree_like",
);
let ptr = env
.builder
.build_struct_gep(
cast_argument,
*index as u32,
env.arena.alloc(format!("non_nullable_unwrapped_{}", index)),
)
.unwrap();
env.builder.build_load(ptr, "load_rosetree_like")
}
(other, layout) => {
// potential cause: indexing into an unwrapped 1-element record/tag?
unreachable!(
@ -1202,7 +1250,7 @@ pub fn build_exp_expr<'a, 'ctx, 'env>(
union_layout,
} => {
// cast the argument bytes into the desired shape for this tag
let (argument, _structure_layout) = load_symbol_and_layout(scope, structure);
let (argument, structure_layout) = load_symbol_and_layout(scope, structure);
match union_layout {
UnionLayout::NonRecursive(tag_layouts) => {
@ -1215,7 +1263,8 @@ pub fn build_exp_expr<'a, 'ctx, 'env>(
let opaque_data_ptr = env
.builder
.build_struct_gep(
.new_build_struct_gep(
basic_type_from_layout(env, structure_layout).into_struct_type(),
argument.into_pointer_value(),
RocUnion::TAG_DATA_INDEX,
"get_opaque_data_ptr",
@ -1230,7 +1279,12 @@ pub fn build_exp_expr<'a, 'ctx, 'env>(
let element_ptr = env
.builder
.build_struct_gep(data_ptr, *index as _, "get_opaque_data_ptr")
.new_build_struct_gep(
struct_type.into_struct_type(),
data_ptr,
*index as _,
"get_opaque_data_ptr",
)
.unwrap();
load_roc_value(
@ -1336,13 +1390,20 @@ fn build_wrapped_tag<'a, 'ctx, 'env>(
let (field_types, field_values) = build_tag_fields(env, scope, tag_field_layouts, arguments);
let union_struct_type = struct_type_from_union_layout(env, union_layout);
// Create the struct_type
let raw_data_ptr = allocate_tag(env, parent, reuse_allocation, union_layout, tags);
let struct_type = env.context.struct_type(&field_types, false);
if union_layout.stores_tag_id_as_data(env.target_info) {
let tag_id_ptr = builder
.build_struct_gep(raw_data_ptr, RocUnion::TAG_ID_INDEX, "tag_id_index")
.new_build_struct_gep(
union_struct_type,
raw_data_ptr,
RocUnion::TAG_ID_INDEX,
"tag_id_index",
)
.unwrap();
let tag_id_type = basic_type_from_layout(env, &tag_id_layout).into_int_type();
@ -1351,7 +1412,12 @@ fn build_wrapped_tag<'a, 'ctx, 'env>(
.build_store(tag_id_ptr, tag_id_type.const_int(tag_id as u64, false));
let opaque_struct_ptr = builder
.build_struct_gep(raw_data_ptr, RocUnion::TAG_DATA_INDEX, "tag_data_index")
.new_build_struct_gep(
union_struct_type,
raw_data_ptr,
RocUnion::TAG_DATA_INDEX,
"tag_data_index",
)
.unwrap();
struct_pointer_from_fields(
@ -1464,12 +1530,15 @@ fn build_struct<'a, 'ctx, 'env>(
// The layout of the struct expects them to be dropped!
let (field_expr, field_layout) = load_symbol_and_layout(scope, symbol);
if !field_layout.is_dropped_because_empty() {
field_types.push(basic_type_from_layout(env, field_layout));
let field_type = basic_type_from_layout(env, field_layout);
field_types.push(field_type);
if field_layout.is_passed_by_reference(env.layout_interner, env.target_info) {
let field_value = env
.builder
.build_load(field_expr.into_pointer_value(), "load_tag_to_put_in_struct");
let field_value = env.builder.new_build_load(
field_type,
field_expr.into_pointer_value(),
"load_tag_to_put_in_struct",
);
field_vals.push(field_value);
} else {
@ -1762,13 +1831,13 @@ pub fn get_tag_id<'a, 'ctx, 'env>(
debug_assert!(argument.is_pointer_value(), "{:?}", argument);
let argument_ptr = argument.into_pointer_value();
get_tag_id_wrapped(env, argument_ptr)
get_tag_id_wrapped(env, *union_layout, argument_ptr)
}
UnionLayout::Recursive(_) => {
let argument_ptr = argument.into_pointer_value();
if union_layout.stores_tag_id_as_data(env.target_info) {
get_tag_id_wrapped(env, argument_ptr)
get_tag_id_wrapped(env, *union_layout, argument_ptr)
} else {
tag_pointer_read_tag_id(env, argument_ptr)
}
@ -1799,7 +1868,7 @@ pub fn get_tag_id<'a, 'ctx, 'env>(
env.builder.position_at_end(else_block);
let tag_id = if union_layout.stores_tag_id_as_data(env.target_info) {
get_tag_id_wrapped(env, argument_ptr)
get_tag_id_wrapped(env, *union_layout, argument_ptr)
} else {
tag_pointer_read_tag_id(env, argument_ptr)
};
@ -1810,7 +1879,7 @@ pub fn get_tag_id<'a, 'ctx, 'env>(
env.builder.position_at_end(cont_block);
env.builder
.build_load(result, "load_result")
.new_build_load(tag_id_int_type, result, "load_result")
.into_int_value()
}
UnionLayout::NullableUnwrapped { nullable_id, .. } => {
@ -1844,7 +1913,7 @@ fn lookup_at_index_ptr<'a, 'ctx, 'env>(
);
let elem_ptr = builder
.build_struct_gep(ptr, index as u32, "at_index_struct_gep")
.new_build_struct_gep(struct_type, ptr, index as u32, "at_index_struct_gep")
.unwrap();
let field_layout = field_layouts[index];
@ -1878,7 +1947,7 @@ fn lookup_at_index_ptr2<'a, 'ctx, 'env>(
let builder = env.builder;
let struct_layout = Layout::struct_no_name_order(field_layouts);
let struct_type = basic_type_from_layout(env, &struct_layout);
let struct_type = basic_type_from_layout(env, &struct_layout).into_struct_type();
let data_ptr = env.builder.build_pointer_cast(
value,
@ -1887,7 +1956,12 @@ fn lookup_at_index_ptr2<'a, 'ctx, 'env>(
);
let elem_ptr = builder
.build_struct_gep(data_ptr, index as u32, "at_index_struct_gep_data")
.new_build_struct_gep(
struct_type,
data_ptr,
index as u32,
"at_index_struct_gep_data",
)
.unwrap();
let field_layout = field_layouts[index];
@ -2096,8 +2170,12 @@ fn list_literal<'a, 'ctx, 'env>(
let offset = env.ptr_int().const_int(zero_elements as _, false);
let ptr = unsafe {
env.builder
.build_in_bounds_gep(global, &[zero, offset], "first_element_pointer")
env.builder.new_build_in_bounds_gep(
element_type,
global,
&[zero, offset],
"first_element_pointer",
)
};
super::build_list::store_list(env, ptr, list_length_intval).into()
@ -2119,7 +2197,9 @@ fn list_literal<'a, 'ctx, 'env>(
// then replace the `undef`s with the values that we evaluate at runtime
for (index, val) in runtime_evaluated_elements {
let index_val = ctx.i64_type().const_int(index as u64, false);
let elem_ptr = unsafe { builder.build_in_bounds_gep(ptr, &[index_val], "index") };
let elem_ptr = unsafe {
builder.new_build_in_bounds_gep(element_type, ptr, &[index_val], "index")
};
builder.build_store(elem_ptr, val);
}
@ -2138,7 +2218,9 @@ fn list_literal<'a, 'ctx, 'env>(
ListLiteralElement::Symbol(symbol) => load_symbol(scope, symbol),
};
let index_val = ctx.i64_type().const_int(index as u64, false);
let elem_ptr = unsafe { builder.build_in_bounds_gep(ptr, &[index_val], "index") };
let elem_ptr = unsafe {
builder.new_build_in_bounds_gep(element_type, ptr, &[index_val], "index")
};
store_roc_value(env, *element_layout, elem_ptr, val);
}
@ -2153,14 +2235,16 @@ pub fn load_roc_value<'a, 'ctx, 'env>(
source: PointerValue<'ctx>,
name: &str,
) -> BasicValueEnum<'ctx> {
let basic_type = basic_type_from_layout(env, &layout);
if layout.is_passed_by_reference(env.layout_interner, env.target_info) {
let alloca = entry_block_alloca_zerofill(env, basic_type_from_layout(env, &layout), name);
let alloca = entry_block_alloca_zerofill(env, basic_type, name);
store_roc_value(env, layout, alloca, source.into());
alloca.into()
} else {
env.builder.build_load(source, name)
env.builder.new_build_load(basic_type, source, name)
}
}
@ -2907,7 +2991,7 @@ fn complex_bitcast_from_bigger_than_to<'ctx>(
name,
);
builder.build_load(to_type_pointer, "cast_value")
builder.new_build_load(to_type, to_type_pointer, "cast_value")
}
fn complex_bitcast_to_bigger_than_from<'ctx>(
@ -2934,21 +3018,30 @@ fn complex_bitcast_to_bigger_than_from<'ctx>(
builder.build_store(from_type_pointer, from_value);
// then read it back as a different type
builder.build_load(storage, "cast_value")
builder.new_build_load(to_type, storage, "cast_value")
}
/// get the tag id out of a pointer to a wrapped (i.e. stores the tag id at runtime) layout
fn get_tag_id_wrapped<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
union_layout: UnionLayout<'a>,
from_value: PointerValue<'ctx>,
) -> IntValue<'ctx> {
let union_struct_type = struct_type_from_union_layout(env, &union_layout);
let tag_id_type = basic_type_from_layout(env, &union_layout.tag_id_layout());
let tag_id_ptr = env
.builder
.build_struct_gep(from_value, RocUnion::TAG_ID_INDEX, "tag_id_ptr")
.new_build_struct_gep(
union_struct_type,
from_value,
RocUnion::TAG_ID_INDEX,
"tag_id_ptr",
)
.unwrap();
env.builder
.build_load(tag_id_ptr, "load_tag_id")
.new_build_load(tag_id_type, tag_id_ptr, "load_tag_id")
.into_int_value()
}
@ -3325,7 +3418,9 @@ fn expose_function_to_host_help_c_abi_generic<'a, 'ctx, 'env>(
"bitcast_arg",
);
let loaded = env.builder.build_load(fastcc_ptr, "load_arg");
let loaded = env
.builder
.new_build_load(fastcc_type, fastcc_ptr, "load_arg");
arguments_for_call.push(loaded);
} else {
let as_cc_type = env.builder.build_pointer_cast(
@ -3441,9 +3536,11 @@ fn expose_function_to_host_help_c_abi_gen_test<'a, 'ctx, 'env>(
} else {
match layout {
Layout::Builtin(Builtin::List(_)) => {
let loaded = env
.builder
.build_load(arg.into_pointer_value(), "load_list_pointer");
let loaded = env.builder.new_build_load(
arg_type,
arg.into_pointer_value(),
"load_list_pointer",
);
let cast =
complex_bitcast_check_size(env, loaded, fastcc_type, "to_fastcc_type_1");
arguments_for_call.push(cast);
@ -3541,6 +3638,15 @@ fn expose_function_to_host_help_c_abi_v2<'a, 'ctx, 'env>(
Linkage::External,
);
let c_abi_roc_str_type = env.context.struct_type(
&[
env.context.i8_type().ptr_type(AddressSpace::Generic).into(),
env.ptr_int().into(),
env.ptr_int().into(),
],
false,
);
// a temporary solution to be able to pass RocStr by-value from a host language.
{
let extra = match cc_return {
@ -3559,7 +3665,7 @@ fn expose_function_to_host_help_c_abi_v2<'a, 'ctx, 'env>(
// if ret_typ is a pointer type. We need the base type here.
let ret_typ = c_function.get_type().get_param_types()[i + extra];
let ret_base_typ = if ret_typ.is_pointer_type() {
ret_typ.into_pointer_type().get_element_type()
c_abi_roc_str_type.as_any_type_enum()
} else {
ret_typ.as_any_type_enum()
};
@ -3614,8 +3720,9 @@ fn expose_function_to_host_help_c_abi_v2<'a, 'ctx, 'env>(
let it = params
.iter()
.zip(param_types)
.zip(arguments)
.enumerate()
.map(|(i, (arg, fastcc_type))| {
.map(|(i, ((arg, fastcc_type), layout))| {
let arg_type = arg.get_type();
if arg_type == *fastcc_type {
// the C and Fast calling conventions agree
@ -3629,13 +3736,18 @@ fn expose_function_to_host_help_c_abi_v2<'a, 'ctx, 'env>(
env.target_info.architecture,
roc_target::Architecture::X86_32 | roc_target::Architecture::X86_64
) {
let c_abi_type = match layout {
Layout::Builtin(Builtin::Str | Builtin::List(_)) => c_abi_roc_str_type,
_ => todo!("figure out what the C type is"),
};
let byval = context.create_type_attribute(
Attribute::get_named_enum_kind_id("byval"),
arg_type.into_pointer_type().get_element_type(),
c_abi_type.as_any_type_enum(),
);
let nonnull = context.create_type_attribute(
Attribute::get_named_enum_kind_id("nonnull"),
arg_type.into_pointer_type().get_element_type(),
c_abi_type.as_any_type_enum(),
);
// C return pointer goes at the beginning of params, and we must skip it if it exists.
let returns_pointer = matches!(cc_return, CCReturn::ByPointer);
@ -3651,7 +3763,8 @@ fn expose_function_to_host_help_c_abi_v2<'a, 'ctx, 'env>(
"bitcast_arg",
);
env.builder.build_load(fastcc_ptr, "load_arg")
env.builder
.new_build_load(*fastcc_type, fastcc_ptr, "load_arg")
} else {
complex_bitcast_check_size(env, *arg, *fastcc_type, "to_fastcc_type_2")
}
@ -3668,9 +3781,11 @@ fn expose_function_to_host_help_c_abi_v2<'a, 'ctx, 'env>(
env.builder.build_return(Some(&value));
}
RocReturn::ByPointer => {
let loaded = env
.builder
.build_load(value.into_pointer_value(), "load_result");
let loaded = env.builder.new_build_load(
return_type,
value.into_pointer_value(),
"load_result",
);
env.builder.build_return(Some(&loaded));
}
},
@ -3684,9 +3799,11 @@ fn expose_function_to_host_help_c_abi_v2<'a, 'ctx, 'env>(
// TODO: ideally, in this case, we should pass the C return pointer directly
// into the call_roc_function rather than forcing an extra alloca, load, and
// store!
let value = env
.builder
.build_load(value.into_pointer_value(), "load_roc_result");
let value = env.builder.new_build_load(
return_type,
value.into_pointer_value(),
"load_roc_result",
);
env.builder.build_store(out_ptr, value);
}
}
@ -3823,13 +3940,15 @@ pub fn build_setjmp_call<'a, 'ctx, 'env>(env: &Env<'a, 'ctx, 'env>) -> BasicValu
// Anywhere else, use the LLVM intrinsic.
// https://llvm.org/docs/ExceptionHandling.html#llvm-eh-sjlj-setjmp
let buf_type = env
.context
.i8_type()
.ptr_type(AddressSpace::Generic)
.array_type(5);
let jmp_buf_i8p_arr = env.builder.build_pointer_cast(
jmp_buf,
env.context
.i8_type()
.ptr_type(AddressSpace::Generic)
.array_type(5)
.ptr_type(AddressSpace::Generic),
buf_type.ptr_type(AddressSpace::Generic),
"jmp_buf [5 x i8*]",
);
@ -3842,7 +3961,8 @@ pub fn build_setjmp_call<'a, 'ctx, 'env>(env: &Env<'a, 'ctx, 'env>) -> BasicValu
let zero = env.context.i32_type().const_zero();
let fa_index = env.context.i32_type().const_zero();
let fa = unsafe {
env.builder.build_in_bounds_gep(
env.builder.new_build_in_bounds_gep(
buf_type,
jmp_buf_i8p_arr,
&[zero, fa_index],
"frame address index",
@ -3855,8 +3975,12 @@ pub fn build_setjmp_call<'a, 'ctx, 'env>(env: &Env<'a, 'ctx, 'env>) -> BasicValu
// usage. But for whatever reason, on x86, it appears we need a stacksave in those words.
let ss_index = env.context.i32_type().const_int(2, false);
let ss = unsafe {
env.builder
.build_in_bounds_gep(jmp_buf_i8p_arr, &[zero, ss_index], "name")
env.builder.new_build_in_bounds_gep(
buf_type,
jmp_buf_i8p_arr,
&[zero, ss_index],
"name",
)
};
let stack_save = env.call_intrinsic(LLVM_STACK_SAVE, &[]);
env.builder.build_store(ss, stack_save);
@ -3957,7 +4081,8 @@ fn set_jump_and_catch_long_jump<'a, 'ctx, 'env>(
let v1 = call_result_type.const_zero();
// tag must be non-zero, indicating failure
let tag = builder.build_load(error_tag_ptr, "load_panic_tag");
let tag =
builder.new_build_load(env.context.i64_type(), error_tag_ptr, "load_panic_tag");
let v2 = builder.build_insert_value(v1, tag, 0, "set_error").unwrap();
@ -3974,7 +4099,11 @@ fn set_jump_and_catch_long_jump<'a, 'ctx, 'env>(
env.builder.position_at_end(cont_block);
builder.build_load(result_alloca, "set_jump_and_catch_long_jump_load_result")
builder.new_build_load(
call_result_type,
result_alloca,
"set_jump_and_catch_long_jump_load_result",
)
}
fn make_exception_catcher<'a, 'ctx, 'env>(
@ -4024,6 +4153,8 @@ fn make_good_roc_result<'a, 'ctx, 'env>(
let context = env.context;
let builder = env.builder;
let return_type = basic_type_from_layout(env, &return_layout);
let v1 = roc_call_result_type(env, basic_type_from_layout(env, &return_layout)).const_zero();
let v2 = builder
@ -4031,7 +4162,8 @@ fn make_good_roc_result<'a, 'ctx, 'env>(
.unwrap();
let v3 = if return_layout.is_passed_by_reference(env.layout_interner, env.target_info) {
let loaded = env.builder.build_load(
let loaded = env.builder.new_build_load(
return_type,
return_value.into_pointer_value(),
"load_call_result_passed_by_ptr",
);
@ -4622,7 +4754,8 @@ fn build_closure_caller<'a, 'ctx, 'env>(
if param.is_pointer_value()
&& !layout.is_passed_by_reference(env.layout_interner, env.target_info)
{
*param = builder.build_load(param.into_pointer_value(), "load_param");
let basic_type = basic_type_from_layout(env, layout);
*param = builder.new_build_load(basic_type, param.into_pointer_value(), "load_param");
}
}
@ -4922,7 +5055,8 @@ pub fn call_roc_function<'a, 'ctx, 'env>(
debug_assert_eq!(roc_function.get_call_conventions(), FAST_CALL_CONV);
call.set_call_convention(FAST_CALL_CONV);
env.builder.build_load(result_alloca, "load_result")
env.builder
.new_build_load(result_type, result_alloca, "load_result")
}
RocReturn::ByPointer => {
let it = arguments.iter().map(|x| (*x).into());
@ -4946,8 +5080,11 @@ pub fn call_roc_function<'a, 'ctx, 'env>(
if result_layout.is_passed_by_reference(env.layout_interner, env.target_info) {
result_alloca.into()
} else {
env.builder
.build_load(result_alloca, "return_by_pointer_load_result")
env.builder.new_build_load(
result_type,
result_alloca,
"return_by_pointer_load_result",
)
}
}
RocReturn::Return => {
@ -5154,31 +5291,24 @@ pub struct FunctionSpec<'ctx> {
pub typ: FunctionType<'ctx>,
call_conv: u32,
/// Index (0-based) of return-by-pointer parameter, if it exists.
/// We only care about this for C-call-conv functions, because this may take
/// ownership of a register due to the convention. For example, on AArch64,
/// values returned-by-pointer use the x8 register.
/// But for internal functions we don't need to worry about that and we don't
/// want the convention, since it might eat a register and cause a spill!
cconv_sret_parameter: Option<u32>,
cconv_stack_return_type: Option<BasicTypeEnum<'ctx>>,
}
impl<'ctx> FunctionSpec<'ctx> {
fn attach_attributes(&self, ctx: &Context, fn_val: FunctionValue<'ctx>) {
fn_val.set_call_conventions(self.call_conv);
if let Some(param_index) = self.cconv_sret_parameter {
if let Some(stack_return_type) = self.cconv_stack_return_type {
// Indicate to LLVM that this argument holds the return value of the function.
let sret_attribute_id = Attribute::get_named_enum_kind_id("sret");
debug_assert!(sret_attribute_id > 0);
let ret_typ = self.typ.get_param_types()[param_index as usize];
// if ret_typ is a pointer type. We need the base type here.
let ret_base_typ = if ret_typ.is_pointer_type() {
ret_typ.into_pointer_type().get_element_type()
} else {
ret_typ.as_any_type_enum()
};
let sret_attribute = ctx.create_type_attribute(sret_attribute_id, ret_base_typ);
let sret_attribute =
ctx.create_type_attribute(sret_attribute_id, stack_return_type.as_any_type_enum());
fn_val.add_attribute(AttributeLoc::Param(0), sret_attribute);
}
}
@ -5200,7 +5330,11 @@ impl<'ctx> FunctionSpec<'ctx> {
arguments.extend(argument_types);
let arguments = function_arguments(env, &arguments);
(env.context.void_type().fn_type(&arguments, false), Some(0))
(
env.context.void_type().fn_type(&arguments, false),
Some(return_type.unwrap()),
)
}
CCReturn::Return => {
let arguments = function_arguments(env, argument_types);
@ -5215,7 +5349,7 @@ impl<'ctx> FunctionSpec<'ctx> {
Self {
typ,
call_conv: C_CALL_CONV,
cconv_sret_parameter: opt_sret_parameter,
cconv_stack_return_type: opt_sret_parameter,
}
}
@ -5241,7 +5375,7 @@ impl<'ctx> FunctionSpec<'ctx> {
Self {
typ,
call_conv: FAST_CALL_CONV,
cconv_sret_parameter: None,
cconv_stack_return_type: None,
}
}
@ -5249,7 +5383,7 @@ impl<'ctx> FunctionSpec<'ctx> {
Self {
typ: fn_type,
call_conv: FAST_CALL_CONV,
cconv_sret_parameter: None,
cconv_stack_return_type: None,
}
}
@ -5259,7 +5393,7 @@ impl<'ctx> FunctionSpec<'ctx> {
Self {
typ: fn_type,
call_conv: C_CALL_CONV,
cconv_sret_parameter: None,
cconv_stack_return_type: None,
}
}
}
@ -5424,9 +5558,11 @@ fn build_foreign_symbol<'a, 'ctx, 'env>(
let return_value = match cc_return {
CCReturn::Return => call.try_as_basic_value().left().unwrap(),
CCReturn::ByPointer => {
env.builder.build_load(return_pointer, "read_result")
}
CCReturn::ByPointer => env.builder.new_build_load(
return_type,
return_pointer,
"read_result",
),
CCReturn::Void => return_type.const_zero(),
};
@ -5472,7 +5608,8 @@ fn define_global_str_literal_ptr<'a, 'ctx, 'env>(
// a pointer to the first actual data (skipping over the refcount)
let ptr = unsafe {
env.builder.build_in_bounds_gep(
env.builder.new_build_in_bounds_gep(
env.context.i8_type(),
ptr,
&[env
.ptr_int()
@ -5611,7 +5748,7 @@ pub fn add_func<'ctx>(
fn_val
}
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) enum WhenRecursive<'a> {
Unreachable,
Loop(UnionLayout<'a>),

View File

@ -17,6 +17,7 @@ use roc_mono::layout::{Builtin, Layout, LayoutIds};
use super::bitcode::{call_list_bitcode_fn, BitcodeReturns};
use super::build::{
create_entry_block_alloca, load_roc_value, load_symbol, store_roc_value, struct_from_fields,
BuilderExt,
};
use super::convert::zig_list_type;
@ -138,8 +139,14 @@ pub(crate) fn list_get_unsafe<'a, 'ctx, 'env>(
// Assume the bounds have already been checked earlier
// (e.g. by List.get or List.first, which wrap List.#getUnsafe)
let elem_ptr =
unsafe { builder.build_in_bounds_gep(array_data_ptr, &[elem_index], "list_get_element") };
let elem_ptr = unsafe {
builder.new_build_in_bounds_gep(
elem_type,
array_data_ptr,
&[elem_index],
"list_get_element",
)
};
let result = load_roc_value(env, *element_layout, elem_ptr, "list_get_load_element");
@ -319,7 +326,9 @@ pub(crate) fn list_replace_unsafe<'a, 'ctx, 'env>(
};
// Load the element and returned list into a struct.
let old_element = env.builder.build_load(element_ptr, "load_element");
let old_element = env
.builder
.new_build_load(element_type, element_ptr, "load_element");
// the list has the same alignment as a usize / ptr. The element comes first in the struct if
// its alignment is bigger than that of a list.
@ -608,9 +617,12 @@ where
{
let builder = env.builder;
let element_type = basic_type_from_layout(env, &element_layout);
incrementing_index_loop(env, parent, len, index_name, |index| {
// The pointer to the element in the list
let element_ptr = unsafe { builder.build_in_bounds_gep(ptr, &[index], "load_index") };
let element_ptr =
unsafe { builder.new_build_in_bounds_gep(element_type, ptr, &[index], "load_index") };
let elem = load_roc_value(
env,
@ -655,7 +667,9 @@ where
{
builder.position_at_end(loop_bb);
let current_index = builder.build_load(index_alloca, "index").into_int_value();
let current_index = builder
.new_build_load(env.ptr_int(), index_alloca, "index")
.into_int_value();
let next_index = builder.build_int_add(current_index, one, "next_index");
builder.build_store(index_alloca, next_index);

View File

@ -6,6 +6,7 @@ use roc_mono::layout::Layout;
use roc_target::PtrWidth;
use super::bitcode::{call_str_bitcode_fn, BitcodeReturns};
use super::build::BuilderExt;
pub static CHAR_LAYOUT: Layout = Layout::u8();
@ -36,7 +37,11 @@ pub(crate) fn decode_from_utf8_result<'a, 'ctx, 'env>(
);
builder
.build_load(result_ptr_cast, "load_utf8_validate_bytes_result")
.new_build_load(
record_type,
result_ptr_cast,
"load_utf8_validate_bytes_result",
)
.into_struct_value()
}
}

View File

@ -15,7 +15,7 @@ use roc_builtins::bitcode::{FloatWidth, IntWidth};
use roc_module::symbol::Symbol;
use roc_mono::layout::{Builtin, Layout, LayoutIds, UnionLayout};
use super::build::{load_roc_value, use_roc_value};
use super::build::{load_roc_value, use_roc_value, BuilderExt};
use super::convert::argument_type_from_union_layout;
use super::lowlevel::dec_binop_with_unchecked;
@ -527,7 +527,9 @@ fn build_list_eq_help<'a, 'ctx, 'env>(
builder.build_unconditional_branch(loop_bb);
builder.position_at_end(loop_bb);
let curr_index = builder.build_load(index_alloca, "index").into_int_value();
let curr_index = builder
.new_build_load(env.ptr_int(), index_alloca, "index")
.into_int_value();
// #index < end
let loop_end_cond =
@ -542,14 +544,16 @@ fn build_list_eq_help<'a, 'ctx, 'env>(
builder.position_at_end(body_bb);
let elem1 = {
let elem_ptr =
unsafe { builder.build_in_bounds_gep(ptr1, &[curr_index], "load_index") };
let elem_ptr = unsafe {
builder.new_build_in_bounds_gep(element_type, ptr1, &[curr_index], "load_index")
};
load_roc_value(env, *element_layout, elem_ptr, "get_elem")
};
let elem2 = {
let elem_ptr =
unsafe { builder.build_in_bounds_gep(ptr2, &[curr_index], "load_index") };
let elem_ptr = unsafe {
builder.new_build_in_bounds_gep(element_type, ptr2, &[curr_index], "load_index")
};
load_roc_value(env, *element_layout, elem_ptr, "get_elem")
};
@ -756,7 +760,7 @@ fn build_struct_eq_help<'a, 'ctx, 'env>(
use_roc_value(env, *field_layout, field2, "field2"),
field_layout,
field_layout,
when_recursive.clone(),
when_recursive,
)
.into_int_value()
};
@ -948,7 +952,7 @@ fn build_tag_eq_help<'a, 'ctx, 'env>(
env,
layout_ids,
union_layout,
Some(when_recursive.clone()),
Some(when_recursive),
field_layouts,
tag1,
tag2,
@ -1253,12 +1257,12 @@ fn eq_ptr_to_struct<'a, 'ctx, 'env>(
let struct1 = env
.builder
.build_load(struct1_ptr, "load_struct1")
.new_build_load(wrapper_type, struct1_ptr, "load_struct1")
.into_struct_value();
let struct2 = env
.builder
.build_load(struct2_ptr, "load_struct2")
.new_build_load(wrapper_type, struct2_ptr, "load_struct2")
.into_struct_value();
build_struct_eq(

View File

@ -1,4 +1,4 @@
use crate::llvm::build::Env;
use crate::llvm::build::{BuilderExt, Env};
use bumpalo::collections::Vec;
use inkwell::context::Context;
use inkwell::types::{BasicType, BasicTypeEnum, FloatType, IntType, StructType};
@ -53,18 +53,16 @@ pub fn basic_type_from_layout<'a, 'ctx, 'env>(
}
}
pub fn basic_type_from_union_layout<'a, 'ctx, 'env>(
pub fn struct_type_from_union_layout<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
union_layout: &UnionLayout<'_>,
) -> BasicTypeEnum<'ctx> {
) -> StructType<'ctx> {
use UnionLayout::*;
match union_layout {
NonRecursive(tags) => {
//
RocUnion::tagged_from_slices(env.layout_interner, env.context, tags, env.target_info)
.struct_type()
.into()
}
Recursive(tags)
| NullableWrapped {
@ -78,8 +76,6 @@ pub fn basic_type_from_union_layout<'a, 'ctx, 'env>(
env.target_info,
)
.struct_type()
.ptr_type(AddressSpace::Generic)
.into()
} else {
RocUnion::untagged_from_slices(
env.layout_interner,
@ -88,8 +84,6 @@ pub fn basic_type_from_union_layout<'a, 'ctx, 'env>(
env.target_info,
)
.struct_type()
.ptr_type(AddressSpace::Generic)
.into()
}
}
NullableUnwrapped { other_fields, .. } => RocUnion::untagged_from_slices(
@ -98,18 +92,31 @@ pub fn basic_type_from_union_layout<'a, 'ctx, 'env>(
&[other_fields],
env.target_info,
)
.struct_type()
.ptr_type(AddressSpace::Generic)
.into(),
.struct_type(),
NonNullableUnwrapped(fields) => RocUnion::untagged_from_slices(
env.layout_interner,
env.context,
&[fields],
env.target_info,
)
.struct_type()
.ptr_type(AddressSpace::Generic)
.into(),
.struct_type(),
}
}
pub fn basic_type_from_union_layout<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
union_layout: &UnionLayout<'_>,
) -> BasicTypeEnum<'ctx> {
use UnionLayout::*;
let struct_type = struct_type_from_union_layout(env, union_layout);
match union_layout {
NonRecursive(_) => struct_type.into(),
Recursive(_)
| NonNullableUnwrapped(_)
| NullableWrapped { .. }
| NullableUnwrapped { .. } => struct_type.ptr_type(AddressSpace::Generic).into(),
}
}
@ -363,7 +370,12 @@ impl<'ctx> RocUnion<'ctx> {
let data_buffer = env
.builder
.build_struct_gep(tag_alloca, Self::TAG_DATA_INDEX, "data_buffer")
.new_build_struct_gep(
self.struct_type(),
tag_alloca,
Self::TAG_DATA_INDEX,
"data_buffer",
)
.unwrap();
let cast_pointer = env.builder.build_pointer_cast(
@ -389,7 +401,12 @@ impl<'ctx> RocUnion<'ctx> {
let tag_id_ptr = env
.builder
.build_struct_gep(tag_alloca, Self::TAG_ID_INDEX, "tag_id_ptr")
.new_build_struct_gep(
self.struct_type(),
tag_alloca,
Self::TAG_ID_INDEX,
"tag_id_ptr",
)
.unwrap();
let tag_id = tag_id_type.const_int(tag_id as u64, false);
@ -398,7 +415,7 @@ impl<'ctx> RocUnion<'ctx> {
}
env.builder
.build_load(tag_alloca, "load_tag")
.new_build_load(self.struct_type(), tag_alloca, "load_tag")
.into_struct_value()
}
}
@ -430,6 +447,30 @@ pub fn zig_has_tag_id_type<'a, 'ctx, 'env>(env: &Env<'a, 'ctx, 'env>) -> StructT
.struct_type(&[env.context.bool_type().into(), u8_ptr_t.into()], false)
}
pub fn zig_num_parse_result_type<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
type_name: &str,
) -> StructType<'ctx> {
let name = format!("num.NumParseResult({type_name})");
match env.module.get_struct_type(&name) {
Some(zig_type) => zig_type,
None => panic!("zig does not define the `{name}` type!"),
}
}
pub fn zig_to_int_checked_result_type<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
type_name: &str,
) -> StructType<'ctx> {
let name = format!("num.ToIntCheckedResult({type_name})");
match env.module.get_struct_type(&name) {
Some(zig_type) => zig_type,
None => panic!("zig does not define the `{name}` type!"),
}
}
pub fn zig_with_overflow_roc_dec<'a, 'ctx, 'env>(env: &Env<'a, 'ctx, 'env>) -> StructType<'ctx> {
env.module
.get_struct_type("utils.WithOverflow(dec.RocDec)")

View File

@ -14,10 +14,12 @@ use roc_mono::ir::LookupType;
use roc_mono::layout::{Builtin, Layout, LayoutIds, UnionLayout};
use roc_region::all::Region;
use super::build::BuilderExt;
use super::build::{
add_func, load_roc_value, load_symbol_and_layout, use_roc_value, FunctionSpec, LlvmBackendMode,
Scope,
Scope, WhenRecursive,
};
use super::convert::struct_type_from_union_layout;
pub(crate) struct SharedMemoryPointer<'ctx>(PointerValue<'ctx>);
@ -53,10 +55,11 @@ struct Cursors<'ctx> {
fn pointer_at_offset<'ctx>(
bd: &Builder<'ctx>,
element_type: impl BasicType<'ctx>,
ptr: PointerValue<'ctx>,
offset: IntValue<'ctx>,
) -> PointerValue<'ctx> {
unsafe { bd.build_gep(ptr, &[offset], "offset_ptr") }
unsafe { bd.new_build_in_bounds_gep(element_type, ptr, &[offset], "offset_ptr") }
}
/// Writes the module and region into the buffer
@ -97,10 +100,12 @@ fn read_state<'a, 'ctx, 'env>(
let ptr = env.builder.build_pointer_cast(ptr, ptr_type, "");
let one = env.ptr_int().const_int(1, false);
let offset_ptr = pointer_at_offset(env.builder, ptr, one);
let offset_ptr = pointer_at_offset(env.builder, env.ptr_int(), ptr, one);
let count = env.builder.build_load(ptr, "load_count");
let offset = env.builder.build_load(offset_ptr, "load_offset");
let count = env.builder.new_build_load(env.ptr_int(), ptr, "load_count");
let offset = env
.builder
.new_build_load(env.ptr_int(), offset_ptr, "load_offset");
(count.into_int_value(), offset.into_int_value())
}
@ -115,7 +120,7 @@ fn write_state<'a, 'ctx, 'env>(
let ptr = env.builder.build_pointer_cast(ptr, ptr_type, "");
let one = env.ptr_int().const_int(1, false);
let offset_ptr = pointer_at_offset(env.builder, ptr, one);
let offset_ptr = pointer_at_offset(env.builder, env.ptr_int(), ptr, one);
env.builder.build_store(ptr, count);
env.builder.build_store(offset_ptr, offset);
@ -237,8 +242,12 @@ pub(crate) fn clone_to_shared_memory<'a, 'ctx, 'env>(
// Store the specialized variable of the value
{
let ptr = unsafe {
env.builder
.build_in_bounds_gep(original_ptr, &[offset], "at_current_offset")
env.builder.new_build_in_bounds_gep(
env.context.i8_type(),
original_ptr,
&[offset],
"at_current_offset",
)
};
let u32_ptr = env.context.i32_type().ptr_type(AddressSpace::Generic);
@ -267,13 +276,6 @@ pub(crate) fn clone_to_shared_memory<'a, 'ctx, 'env>(
write_state(env, original_ptr, new_count, offset)
}
#[derive(Clone, Debug, Copy)]
enum WhenRecursive<'a> {
Unreachable,
#[allow(dead_code)]
Loop(UnionLayout<'a>),
}
#[allow(clippy::too_many_arguments)]
fn build_clone<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
@ -312,8 +314,12 @@ fn build_clone<'a, 'ctx, 'env>(
Layout::Union(union_layout) => {
if layout.safe_to_memcpy(env.layout_interner) {
let ptr = unsafe {
env.builder
.build_in_bounds_gep(ptr, &[cursors.offset], "at_current_offset")
env.builder.new_build_in_bounds_gep(
env.context.i8_type(),
ptr,
&[cursors.offset],
"at_current_offset",
)
};
let ptr_type = value.get_type().ptr_type(AddressSpace::Generic);
@ -531,12 +537,20 @@ fn build_clone_tag<'a, 'ctx, 'env>(
fn load_tag_data<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
union_layout: UnionLayout<'a>,
tag_value: PointerValue<'ctx>,
tag_type: BasicTypeEnum<'ctx>,
) -> BasicValueEnum<'ctx> {
let union_struct_type = struct_type_from_union_layout(env, &union_layout);
let raw_data_ptr = env
.builder
.build_struct_gep(tag_value, RocUnion::TAG_DATA_INDEX, "tag_data")
.new_build_struct_gep(
union_struct_type,
tag_value,
RocUnion::TAG_DATA_INDEX,
"tag_data",
)
.unwrap();
let data_ptr = env.builder.build_pointer_cast(
@ -545,7 +559,7 @@ fn load_tag_data<'a, 'ctx, 'env>(
"data_ptr",
);
env.builder.build_load(data_ptr, "load_data")
env.builder.new_build_load(tag_type, data_ptr, "load_data")
}
#[allow(clippy::too_many_arguments)]
@ -613,7 +627,12 @@ fn build_clone_tag_help<'a, 'ctx, 'env>(
);
let basic_type = basic_type_from_layout(env, &layout);
let data = load_tag_data(env, tag_value.into_pointer_value(), basic_type);
let data = load_tag_data(
env,
union_layout,
tag_value.into_pointer_value(),
basic_type,
);
let answer =
build_clone(env, layout_ids, ptr, cursors, data, layout, when_recursive);
@ -662,7 +681,7 @@ fn build_clone_tag_help<'a, 'ctx, 'env>(
};
let basic_type = basic_type_from_layout(env, &layout);
let data = load_tag_data(env, tag_value, basic_type);
let data = load_tag_data(env, union_layout, tag_value, basic_type);
let (width, _) =
union_layout.data_size_and_alignment(env.layout_interner, env.target_info);
@ -717,7 +736,7 @@ fn build_clone_tag_help<'a, 'ctx, 'env>(
),
};
let data = load_tag_data(env, tag_value, basic_type);
let data = load_tag_data(env, union_layout, tag_value, basic_type);
let when_recursive = WhenRecursive::Loop(union_layout);
let answer = build_clone(env, layout_ids, ptr, cursors, data, layout, when_recursive);
@ -776,7 +795,7 @@ fn build_clone_tag_help<'a, 'ctx, 'env>(
};
let tag_value = tag_pointer_clear_tag_id(env, tag_value.into_pointer_value());
let data = load_tag_data(env, tag_value, basic_type);
let data = load_tag_data(env, union_layout, tag_value, basic_type);
let when_recursive = WhenRecursive::Loop(union_layout);
let answer =
@ -850,7 +869,12 @@ fn build_clone_tag_help<'a, 'ctx, 'env>(
),
};
let data = load_tag_data(env, tag_value.into_pointer_value(), basic_type);
let data = load_tag_data(
env,
union_layout,
tag_value.into_pointer_value(),
basic_type,
);
let when_recursive = WhenRecursive::Loop(union_layout);
let answer =
@ -897,8 +921,12 @@ fn build_copy<'a, 'ctx, 'env>(
value: BasicValueEnum<'ctx>,
) -> IntValue<'ctx> {
let ptr = unsafe {
env.builder
.build_in_bounds_gep(ptr, &[offset], "at_current_offset")
env.builder.new_build_in_bounds_gep(
env.context.i8_type(),
ptr,
&[offset],
"at_current_offset",
)
};
let ptr_type = value.get_type().ptr_type(AddressSpace::Generic);
@ -968,7 +996,7 @@ fn build_clone_builtin<'a, 'ctx, 'env>(
if elem.safe_to_memcpy(env.layout_interner) {
// NOTE we are not actually sure the dest is properly aligned
let dest = pointer_at_offset(bd, ptr, offset);
let dest = pointer_at_offset(bd, env.context.i8_type(), ptr, offset);
let src = bd.build_pointer_cast(
elements,
env.context.i8_type().ptr_type(AddressSpace::Generic),
@ -1007,7 +1035,8 @@ fn build_clone_builtin<'a, 'ctx, 'env>(
bd.build_int_mul(element_stack_size, index, "current_offset");
let current_offset =
bd.build_int_add(elements_start_offset, current_offset, "current_offset");
let current_extra_offset = bd.build_load(rest_offset, "element_offset");
let current_extra_offset =
bd.new_build_load(env.ptr_int(), rest_offset, "element_offset");
let offset = current_offset;
let extra_offset = current_extra_offset.into_int_value();
@ -1038,7 +1067,7 @@ fn build_clone_builtin<'a, 'ctx, 'env>(
incrementing_elem_loop(env, parent, *elem, elements, len, "index", body);
bd.build_load(rest_offset, "rest_start_offset")
bd.new_build_load(env.ptr_int(), rest_offset, "rest_start_offset")
.into_int_value()
}
}

View File

@ -1,6 +1,7 @@
use crate::llvm::bitcode::call_void_bitcode_fn;
use crate::llvm::build::{add_func, get_panic_msg_ptr, get_panic_tag_ptr, C_CALL_CONV};
use crate::llvm::build::{add_func, get_panic_msg_ptr, get_panic_tag_ptr, BuilderExt, C_CALL_CONV};
use crate::llvm::build::{CCReturn, Env, FunctionSpec};
use crate::llvm::convert::zig_str_type;
use inkwell::module::Linkage;
use inkwell::types::BasicType;
use inkwell::values::BasicValue;
@ -217,7 +218,12 @@ pub fn add_sjlj_roc_panic(env: &Env<'_, '_, '_>) {
roc_target::PtrWidth::Bytes4 => roc_str_arg,
// On 64-bit we pass RocStrs by reference internally
roc_target::PtrWidth::Bytes8 => {
builder.build_load(roc_str_arg.into_pointer_value(), "load_roc_str")
let str_typ = zig_str_type(env);
builder.new_build_load(
str_typ,
roc_str_arg.into_pointer_value(),
"load_roc_str",
)
}
};

View File

@ -24,7 +24,7 @@ use crate::llvm::{
},
build::{
complex_bitcast_check_size, create_entry_block_alloca, function_value_by_func_spec,
load_roc_value, roc_function_call, RocReturn,
load_roc_value, roc_function_call, BuilderExt, RocReturn,
},
build_list::{
list_append_unsafe, list_capacity, list_concat, list_drop_at, list_get_unsafe, list_len,
@ -33,7 +33,9 @@ use crate::llvm::{
pass_update_mode,
},
compare::{generic_eq, generic_neq},
convert::{self, basic_type_from_layout},
convert::{
self, basic_type_from_layout, zig_num_parse_result_type, zig_to_int_checked_result_type,
},
intrinsics::{
LLVM_ADD_SATURATED, LLVM_ADD_WITH_OVERFLOW, LLVM_CEILING, LLVM_COS, LLVM_FABS, LLVM_FLOOR,
LLVM_LOG, LLVM_MUL_WITH_OVERFLOW, LLVM_POW, LLVM_ROUND, LLVM_SIN, LLVM_SQRT,
@ -225,14 +227,23 @@ pub(crate) fn run_low_level<'a, 'ctx, 'env>(
intrinsic,
),
None => {
let return_type = zig_function_type.get_param_types()[0]
.into_pointer_type()
.get_element_type()
.into_struct_type()
.into();
let return_type_name = match number_layout {
Layout::Builtin(Builtin::Int(int_width)) => int_width.type_name(),
Layout::Builtin(Builtin::Decimal) => {
// zig picks 128 for dec.RocDec
"i128"
}
_ => unreachable!(),
};
let zig_return_alloca =
create_entry_block_alloca(env, parent, return_type, "str_to_num");
let return_type = zig_num_parse_result_type(env, return_type_name);
let zig_return_alloca = create_entry_block_alloca(
env,
parent,
return_type.into(),
"str_to_num",
);
let (a, b) =
pass_list_or_string_to_zig_32bit(env, string.into_struct_value());
@ -257,7 +268,31 @@ pub(crate) fn run_low_level<'a, 'ctx, 'env>(
}
}
PtrWidth::Bytes8 => {
call_bitcode_fn_fixing_for_convention(env, &[string], layout, intrinsic)
let cc_return_by_pointer = match number_layout {
Layout::Builtin(Builtin::Int(int_width)) => {
(int_width.stack_size() as usize > env.target_info.ptr_size())
.then_some(int_width.type_name())
}
Layout::Builtin(Builtin::Decimal) => {
// zig picks 128 for dec.RocDec
Some("i128")
}
_ => None,
};
if let Some(type_name) = cc_return_by_pointer {
let bitcode_return_type = zig_num_parse_result_type(env, type_name);
call_bitcode_fn_fixing_for_convention(
env,
bitcode_return_type,
&[string],
layout,
intrinsic,
)
} else {
call_bitcode_fn(env, &[string], intrinsic)
}
}
};
@ -438,16 +473,10 @@ pub(crate) fn run_low_level<'a, 'ctx, 'env>(
use roc_target::OperatingSystem::*;
match env.target_info.operating_system {
Windows => {
// we have to go digging to find the return type
let function = env
.module
.get_function(bitcode::STR_GET_SCALAR_UNSAFE)
.unwrap();
let return_type = function.get_type().get_param_types()[0]
.into_pointer_type()
.get_element_type()
.into_struct_type();
let return_type = env.context.struct_type(
&[env.ptr_int().into(), env.context.i32_type().into()],
false,
);
let result = env.builder.build_alloca(return_type, "result");
@ -464,7 +493,8 @@ pub(crate) fn run_low_level<'a, 'ctx, 'env>(
"cast",
);
env.builder.build_load(cast_result, "load_result")
env.builder
.new_build_load(return_type, cast_result, "load_result")
}
Unix => {
let result = call_str_bitcode_fn(
@ -1693,7 +1723,7 @@ fn dec_binop_with_overflow<'a, 'ctx, 'env>(
}
env.builder
.build_load(return_alloca, "load_dec")
.new_build_load(return_type, return_alloca, "load_dec")
.into_struct_value()
}
@ -1939,16 +1969,15 @@ fn build_int_unary_op<'a, 'ctx, 'env>(
intrinsic,
),
None => {
let return_type = zig_function_type.get_param_types()[0]
.into_pointer_type()
.get_element_type()
.into_struct_type()
.into();
let return_type = zig_to_int_checked_result_type(
env,
target_int_width.type_name(),
);
let zig_return_alloca = create_entry_block_alloca(
env,
parent,
return_type,
return_type.into(),
"num_to_int",
);
@ -1972,14 +2001,20 @@ fn build_int_unary_op<'a, 'ctx, 'env>(
}
}
PtrWidth::Bytes8 => {
// call_bitcode_fn_fixing_for_convention(env, &[string], layout, intrinsic)
if target_int_width.stack_size() as usize > env.target_info.ptr_size() {
let bitcode_return_type =
zig_to_int_checked_result_type(env, target_int_width.type_name());
call_bitcode_fn_fixing_for_convention(
env,
&[arg.into()],
return_layout,
intrinsic,
)
call_bitcode_fn_fixing_for_convention(
env,
bitcode_return_type,
&[arg.into()],
return_layout,
intrinsic,
)
} else {
call_bitcode_fn(env, &[arg.into()], intrinsic)
}
}
};
@ -2082,15 +2117,19 @@ fn int_abs_with_overflow<'a, 'ctx, 'env>(
let xored_arg = bd.build_xor(
arg,
bd.build_load(shifted_alloca, shifted_name).into_int_value(),
bd.new_build_load(int_type, shifted_alloca, shifted_name)
.into_int_value(),
"xor_arg_shifted",
);
BasicValueEnum::IntValue(bd.build_int_sub(
xored_arg,
bd.build_load(shifted_alloca, shifted_name).into_int_value(),
"sub_xored_shifted",
))
BasicValueEnum::IntValue(
bd.build_int_sub(
xored_arg,
bd.new_build_load(int_type, shifted_alloca, shifted_name)
.into_int_value(),
"sub_xored_shifted",
),
)
}
fn build_float_unary_op<'a, 'ctx, 'env>(

View File

@ -1,11 +1,12 @@
use crate::debug_info_init;
use crate::llvm::bitcode::call_void_bitcode_fn;
use crate::llvm::build::BuilderExt;
use crate::llvm::build::{
add_func, cast_basic_basic, get_tag_id, tag_pointer_clear_tag_id, use_roc_value, Env,
WhenRecursive, FAST_CALL_CONV,
};
use crate::llvm::build_list::{incrementing_elem_loop, list_len, load_list};
use crate::llvm::convert::{basic_type_from_layout, RocUnion};
use crate::llvm::convert::{basic_type_from_layout, zig_str_type, RocUnion};
use bumpalo::collections::Vec;
use inkwell::basic_block::BasicBlock;
use inkwell::module::Linkage;
@ -59,7 +60,12 @@ impl<'ctx> PointerToRefcount<'ctx> {
// get a pointer to index -1
let index_intvalue = refcount_type.const_int(-1_i64 as u64, false);
let refcount_ptr = unsafe {
builder.build_in_bounds_gep(ptr_as_usize_ptr, &[index_intvalue], "get_rc_ptr")
builder.new_build_in_bounds_gep(
env.ptr_int(),
ptr_as_usize_ptr,
&[index_intvalue],
"get_rc_ptr",
)
};
Self {
@ -94,7 +100,7 @@ impl<'ctx> PointerToRefcount<'ctx> {
fn get_refcount<'a, 'env>(&self, env: &Env<'a, 'ctx, 'env>) -> IntValue<'ctx> {
env.builder
.build_load(self.value, "get_refcount")
.new_build_load(env.ptr_int(), self.value, "get_refcount")
.into_int_value()
}
@ -541,7 +547,7 @@ fn modify_refcount_layout_build_function<'a, 'ctx, 'env>(
NonRecursive(tags) => {
let function =
modify_refcount_union(env, layout_ids, mode, when_recursive, tags);
modify_refcount_nonrecursive(env, layout_ids, mode, when_recursive, tags);
Some(function)
}
@ -798,8 +804,9 @@ fn modify_refcount_str_help<'a, 'ctx, 'env>(
let arg_val = if Layout::Builtin(Builtin::Str)
.is_passed_by_reference(env.layout_interner, env.target_info)
{
let str_type = zig_str_type(env);
env.builder
.build_load(arg_val.into_pointer_value(), "load_str_to_stack")
.new_build_load(str_type, arg_val.into_pointer_value(), "load_str_to_stack")
} else {
// it's already a struct, just do nothing
debug_assert!(arg_val.is_struct_value());
@ -1226,12 +1233,19 @@ fn build_rec_union_recursive_decrement<'a, 'ctx, 'env>(
// this field has type `*i64`, but is really a pointer to the data we want
let elem_pointer = env
.builder
.build_struct_gep(struct_ptr, i as u32, "gep_recursive_pointer")
.new_build_struct_gep(
wrapper_type.into_struct_type(),
struct_ptr,
i as u32,
"gep_recursive_pointer",
)
.unwrap();
let ptr_as_i64_ptr = env
.builder
.build_load(elem_pointer, "load_recursive_pointer");
let ptr_as_i64_ptr = env.builder.new_build_load(
env.context.i64_type().ptr_type(AddressSpace::Generic),
elem_pointer,
"load_recursive_pointer",
);
debug_assert!(ptr_as_i64_ptr.is_pointer_value());
@ -1243,7 +1257,12 @@ fn build_rec_union_recursive_decrement<'a, 'ctx, 'env>(
} else if field_layout.contains_refcounted(env.layout_interner) {
let elem_pointer = env
.builder
.build_struct_gep(struct_ptr, i as u32, "gep_recursive_pointer")
.new_build_struct_gep(
wrapper_type.into_struct_type(),
struct_ptr,
i as u32,
"gep_recursive_pointer",
)
.unwrap();
let field =
@ -1499,7 +1518,7 @@ fn function_name_from_mode<'a>(
}
}
fn modify_refcount_union<'a, 'ctx, 'env>(
fn modify_refcount_nonrecursive<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
layout_ids: &mut LayoutIds<'a>,
mode: Mode,
@ -1527,7 +1546,7 @@ fn modify_refcount_union<'a, 'ctx, 'env>(
let basic_type = argument_type_from_union_layout(env, &union_layout);
let function_value = build_header(env, basic_type, mode, &fn_name);
modify_refcount_union_help(
modify_refcount_nonrecursive_help(
env,
layout_ids,
mode,
@ -1547,7 +1566,7 @@ fn modify_refcount_union<'a, 'ctx, 'env>(
function
}
fn modify_refcount_union_help<'a, 'ctx, 'env>(
fn modify_refcount_nonrecursive_help<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
layout_ids: &mut LayoutIds<'a>,
mode: Mode,
@ -1577,15 +1596,28 @@ fn modify_refcount_union_help<'a, 'ctx, 'env>(
let before_block = env.builder.get_insert_block().expect("to be in a function");
let union_layout = UnionLayout::NonRecursive(tags);
let layout = Layout::Union(union_layout);
let union_struct_type = basic_type_from_layout(env, &layout).into_struct_type();
// read the tag_id
let tag_id_ptr = env
.builder
.build_struct_gep(arg_ptr, RocUnion::TAG_ID_INDEX, "tag_id_ptr")
.new_build_struct_gep(
union_struct_type,
arg_ptr,
RocUnion::TAG_ID_INDEX,
"tag_id_ptr",
)
.unwrap();
let tag_id = env
.builder
.build_load(tag_id_ptr, "load_tag_id")
.new_build_load(
basic_type_from_layout(env, &union_layout.tag_id_layout()),
tag_id_ptr,
"load_tag_id",
)
.into_int_value();
let tag_id_u8 =
@ -1611,18 +1643,24 @@ fn modify_refcount_union_help<'a, 'ctx, 'env>(
let block = env.context.append_basic_block(parent, "tag_id_modify");
env.builder.position_at_end(block);
let wrapper_type =
let data_struct_type =
basic_type_from_layout(env, &Layout::struct_no_name_order(field_layouts));
debug_assert!(wrapper_type.is_struct_type());
debug_assert!(data_struct_type.is_struct_type());
let data_struct_type = data_struct_type.into_struct_type();
let opaque_tag_data_ptr = env
.builder
.build_struct_gep(arg_ptr, RocUnion::TAG_DATA_INDEX, "field_ptr")
.new_build_struct_gep(
union_struct_type,
arg_ptr,
RocUnion::TAG_DATA_INDEX,
"field_ptr",
)
.unwrap();
let cast_tag_data_pointer = env.builder.build_pointer_cast(
opaque_tag_data_ptr,
wrapper_type.ptr_type(AddressSpace::Generic),
data_struct_type.ptr_type(AddressSpace::Generic),
"cast_to_concrete_tag",
);
@ -1638,11 +1676,20 @@ fn modify_refcount_union_help<'a, 'ctx, 'env>(
// This field is a pointer to the recursive pointer.
let field_ptr = env
.builder
.build_struct_gep(cast_tag_data_pointer, i as u32, "modify_tag_field")
.new_build_struct_gep(
data_struct_type,
cast_tag_data_pointer,
i as u32,
"modify_tag_field",
)
.unwrap();
// This is the actual pointer to the recursive data.
let field_value = env.builder.build_load(field_ptr, "load_recursive_pointer");
let field_value = env.builder.new_build_load(
env.context.i64_type().ptr_type(AddressSpace::Generic),
field_ptr,
"load_recursive_pointer",
);
debug_assert!(field_value.is_pointer_value());
@ -1663,14 +1710,23 @@ fn modify_refcount_union_help<'a, 'ctx, 'env>(
} else if field_layout.contains_refcounted(env.layout_interner) {
let field_ptr = env
.builder
.build_struct_gep(cast_tag_data_pointer, i as u32, "modify_tag_field")
.new_build_struct_gep(
data_struct_type,
cast_tag_data_pointer,
i as u32,
"modify_tag_field",
)
.unwrap();
let field_value =
if field_layout.is_passed_by_reference(env.layout_interner, env.target_info) {
field_ptr.into()
} else {
env.builder.build_load(field_ptr, "field_value")
env.builder.new_build_load(
basic_type_from_layout(env, field_layout),
field_ptr,
"field_value",
)
};
modify_refcount_layout_help(

View File

@ -42,8 +42,10 @@ use roc_packaging::cache::{self, RocCacheDir};
#[cfg(not(target_family = "wasm"))]
use roc_packaging::https::PackageMetadata;
use roc_parse::ast::{self, Defs, ExtractSpaces, Spaced, StrLiteral, TypeAnnotation};
use roc_parse::header::{ExposedName, ImportsEntry, PackageEntry, PlatformHeader, To, TypedIdent};
use roc_parse::header::{HeaderType, PackagePath};
use roc_parse::header::{
ExposedName, ImportsEntry, PackageEntry, PackageHeader, PlatformHeader, To, TypedIdent,
};
use roc_parse::header::{HeaderType, PackageName};
use roc_parse::module::module_defs;
use roc_parse::parser::{FileError, Parser, SourceError, SyntaxError};
use roc_problem::Severity;
@ -666,7 +668,7 @@ struct ModuleHeader<'a> {
is_root_module: bool,
exposed_ident_ids: IdentIds,
deps_by_name: MutMap<PQModuleName<'a>, ModuleId>,
packages: MutMap<&'a str, PackagePath<'a>>,
packages: MutMap<&'a str, PackageName<'a>>,
imported_modules: MutMap<ModuleId, Region>,
package_qualified_imported_modules: MutSet<PackageQualified<'a, ModuleId>>,
exposes: Vec<Symbol>,
@ -890,6 +892,7 @@ enum Msg<'a> {
error: io::ErrorKind,
},
FailedToLoad(LoadingProblem<'a>),
IncorrectModuleName(FileError<'a, IncorrectModuleName<'a>>),
}
@ -1195,6 +1198,7 @@ enum BuildTask<'a> {
},
}
#[derive(Debug)]
enum WorkerMsg {
Shutdown,
TaskAdded,
@ -1367,7 +1371,7 @@ impl<'a> LoadStart<'a> {
header_output
}
Err(LoadingProblem::ParsingFailed(problem)) => {
Err(problem) => {
let module_ids = Arc::try_unwrap(arc_modules)
.unwrap_or_else(|_| {
panic!("There were still outstanding Arc references to module_ids")
@ -1375,62 +1379,12 @@ impl<'a> LoadStart<'a> {
.into_inner()
.into_module_ids();
// if parsing failed, this module did not add any identifiers
let root_exposed_ident_ids = IdentIds::exposed_builtins(0);
let buf = to_parse_problem_report(
problem,
module_ids,
root_exposed_ident_ids,
render,
palette,
);
return Err(LoadingProblem::FormattedReport(buf));
}
Err(LoadingProblem::FileProblem { filename, error }) => {
let buf = to_file_problem_report(&filename, error);
return Err(LoadingProblem::FormattedReport(buf));
}
Err(LoadingProblem::ImportCycle(filename, cycle)) => {
let module_ids = Arc::try_unwrap(arc_modules)
.unwrap_or_else(|_| {
panic!("There were still outstanding Arc references to module_ids")
})
.into_inner()
.into_module_ids();
let report = report_loading_problem(problem, module_ids, render, palette);
let root_exposed_ident_ids = IdentIds::exposed_builtins(0);
let buf = to_import_cycle_report(
module_ids,
root_exposed_ident_ids,
cycle,
filename,
render,
);
return Err(LoadingProblem::FormattedReport(buf));
// TODO try to gracefully recover and continue
// instead of changing the control flow to exit.
return Err(LoadingProblem::FormattedReport(report));
}
Err(LoadingProblem::IncorrectModuleName(FileError {
problem: SourceError { problem, bytes },
filename,
})) => {
let module_ids = Arc::try_unwrap(arc_modules)
.unwrap_or_else(|_| {
panic!("There were still outstanding Arc references to module_ids")
})
.into_inner()
.into_module_ids();
let root_exposed_ident_ids = IdentIds::exposed_builtins(0);
let buf = to_incorrect_module_name_report(
module_ids,
root_exposed_ident_ids,
problem,
filename,
bytes,
render,
);
return Err(LoadingProblem::FormattedReport(buf));
}
Err(e) => return Err(e),
}
};
@ -1880,6 +1834,45 @@ fn state_thread_step<'a>(
}
}
pub fn report_loading_problem(
problem: LoadingProblem<'_>,
module_ids: ModuleIds,
render: RenderTarget,
palette: Palette,
) -> String {
match problem {
LoadingProblem::ParsingFailed(problem) => {
// if parsing failed, this module did not add anything to IdentIds
let root_exposed_ident_ids = IdentIds::exposed_builtins(0);
to_parse_problem_report(problem, module_ids, root_exposed_ident_ids, render, palette)
}
LoadingProblem::ImportCycle(filename, cycle) => {
let root_exposed_ident_ids = IdentIds::exposed_builtins(0);
to_import_cycle_report(module_ids, root_exposed_ident_ids, cycle, filename, render)
}
LoadingProblem::IncorrectModuleName(FileError {
problem: SourceError { problem, bytes },
filename,
}) => {
let root_exposed_ident_ids = IdentIds::exposed_builtins(0);
to_incorrect_module_name_report(
module_ids,
root_exposed_ident_ids,
problem,
filename,
bytes,
render,
)
}
LoadingProblem::FormattedReport(report) => report,
LoadingProblem::FileProblem { filename, error } => to_file_problem_report(&filename, error),
err => todo!("Loading error: {:?}", err),
}
}
fn load_multi_threaded<'a>(
arena: &'a Bump,
load_start: LoadStart<'a>,
@ -2163,6 +2156,31 @@ fn worker_task<'a>(
// added. In that case, do nothing, and keep waiting
// until we receive a Shutdown message.
if let Some(task) = find_task(&worker, injector, stealers) {
log!(
">>> {}",
match &task {
BuildTask::LoadModule { module_name, .. } => {
format!("BuildTask::LoadModule({:?})", module_name)
}
BuildTask::Parse { header } => {
format!("BuildTask::Parse({})", header.module_path.display())
}
BuildTask::CanonicalizeAndConstrain { parsed, .. } => format!(
"BuildTask::CanonicalizeAndConstrain({})",
parsed.module_path.display()
),
BuildTask::Solve { module, .. } => {
format!("BuildTask::Solve({:?})", module.module_id)
}
BuildTask::BuildPendingSpecializations { module_id, .. } => {
format!("BuildTask::BuildPendingSpecializations({:?})", module_id)
}
BuildTask::MakeSpecializations { module_id, .. } => {
format!("BuildTask::MakeSpecializations({:?})", module_id)
}
}
);
let result = run_task(
task,
worker_arena,
@ -2378,17 +2396,17 @@ fn update<'a>(
// This wasn't a URL, so it must be a filesystem path.
let root_module: PathBuf = src_dir.join(package_str);
let root_module_dir = root_module.parent().unwrap_or_else(|| {
if root_module.is_file() {
// Files must have parents!
internal_error!("Somehow I got a file path to a real file on the filesystem that has no parent!");
} else {
// TODO make this a nice report
todo!(
"platform module {:?} was not a file.",
package_str
)
}
}).into();
if root_module.is_file() {
// Files must have parents!
internal_error!("Somehow I got a file path to a real file on the filesystem that has no parent!");
} else {
// TODO make this a nice report
todo!(
"platform module {:?} was not a file.",
package_str
)
}
}).into();
ShorthandPath::RelativeToSrc {
root_module_dir,
@ -2396,6 +2414,12 @@ fn update<'a>(
}
};
log!(
"New package shorthand: {:?} => {:?}",
shorthand,
shorthand_path
);
shorthands.insert(shorthand, shorthand_path);
}
@ -2404,6 +2428,11 @@ fn update<'a>(
debug_assert!(matches!(state.platform_path, PlatformPath::NotSpecified));
state.platform_path = PlatformPath::Valid(to_platform);
}
Package {
config_shorthand, ..
} => {
work.extend(state.dependencies.notify_package(config_shorthand));
}
Platform {
config_shorthand,
provides,
@ -3111,6 +3140,10 @@ fn update<'a>(
}
}
}
Msg::FailedToLoad(problem) => {
// TODO report the error and continue instead of erroring out
Err(problem)
}
Msg::FinishedAllTypeChecking { .. } => {
unreachable!();
}
@ -3383,8 +3416,8 @@ fn finish(
}
}
/// Load a `platform` module from disk
fn load_platform_module<'a>(
/// Load a `package` or `platform` module from disk
fn load_package_from_disk<'a>(
arena: &'a Bump,
filename: &Path,
shorthand: &'a str,
@ -3393,11 +3426,11 @@ fn load_platform_module<'a>(
ident_ids_by_module: SharedIdentIdsByModule,
) -> Result<Msg<'a>, LoadingProblem<'a>> {
let module_start_time = Instant::now();
let file_io_start = Instant::now();
let file = fs::read(filename);
let file_io_start = module_start_time;
let read_result = fs::read(filename);
let file_io_duration = file_io_start.elapsed();
match file {
match read_result {
Ok(bytes_vec) => {
let parse_start = Instant::now();
let bytes = arena.alloc(bytes_vec);
@ -3442,6 +3475,27 @@ fn load_platform_module<'a>(
"expected platform/package module, got App with header\n{:?}",
header
))),
Ok((
ast::Module {
header: ast::Header::Package(header),
..
},
parser_state,
)) => {
let (_, _, package_module_msg) = build_package_header(
arena,
Some(shorthand),
false, // since we have an app module ID, the app module must be the root
filename.to_path_buf(),
parser_state,
module_ids,
ident_ids_by_module,
&header,
pkg_module_timing,
);
Ok(Msg::Header(package_module_msg))
}
Ok((
ast::Module {
header: ast::Header::Platform(header),
@ -3456,7 +3510,7 @@ fn load_platform_module<'a>(
Some(app_module_id),
filename.to_path_buf(),
parser_state,
module_ids.clone(),
module_ids,
ident_ids_by_module,
&header,
pkg_module_timing,
@ -3504,10 +3558,10 @@ fn load_builtin_module_help<'a>(
is_root_module,
opt_shorthand,
packages: &[],
exposes: unspace(arena, header.exposes.item.items),
imports: unspace(arena, header.imports.item.items),
header_type: HeaderType::Builtin {
name: header.name.value,
exposes: unspace(arena, header.exposes.item.items),
generates_with: &[],
},
};
@ -3807,10 +3861,10 @@ fn parse_header<'a>(
is_root_module,
opt_shorthand,
packages: &[],
exposes: unspace(arena, header.exposes.item.items),
imports: unspace(arena, header.imports.item.items),
header_type: HeaderType::Interface {
name: header.name.value,
exposes: unspace(arena, header.exposes.item.items),
},
};
@ -3858,10 +3912,10 @@ fn parse_header<'a>(
is_root_module,
opt_shorthand,
packages: &[],
exposes: unspace(arena, header.exposes.item.items),
imports: unspace(arena, header.imports.item.items),
header_type: HeaderType::Hosted {
name: header.name.value,
exposes: unspace(arena, header.exposes.item.items),
generates: header.generates.item,
generates_with: unspace(arena, header.generates_with.item.items),
},
@ -3897,33 +3951,31 @@ fn parse_header<'a>(
&[]
};
let mut exposes = bumpalo::collections::Vec::new_in(arena);
let mut provides = bumpalo::collections::Vec::new_in(arena);
exposes.extend(unspace(arena, header.provides.entries.items));
provides.extend(unspace(arena, header.provides.entries.items));
if let Some(provided_types) = header.provides.types {
for provided_type in unspace(arena, provided_types.items) {
let string: &str = provided_type.value.into();
let exposed_name = ExposedName::new(string);
exposes.push(Loc::at(provided_type.region, exposed_name));
provides.push(Loc::at(provided_type.region, exposed_name));
}
}
let exposes = exposes.into_bump_slice();
let info = HeaderInfo {
filename,
is_root_module,
opt_shorthand,
packages,
exposes,
imports: if let Some(imports) = header.imports {
unspace(arena, imports.item.items)
} else {
&[]
},
header_type: HeaderType::App {
provides: provides.into_bump_slice(),
output_name: header.name.value,
to_platform: header.provides.to.value,
},
@ -3936,85 +3988,72 @@ fn parse_header<'a>(
ident_ids_by_module.clone(),
module_timing,
);
let app_module_header_msg = Msg::Header(resolved_header);
let mut messages = Vec::with_capacity(packages.len() + 1);
// It's important that the app header is first in the list!
messages.push(Msg::Header(resolved_header));
load_packages(
packages,
&mut messages,
roc_cache_dir,
app_file_dir,
arena,
module_id,
module_ids,
ident_ids_by_module,
);
// Look at the app module's `to` keyword to determine which package was the platform.
match header.provides.to.value {
To::ExistingPackage(shorthand) => {
let package_path = packages.iter().find_map(|loc_package_entry| {
let Loc { value, .. } = loc_package_entry;
if value.shorthand == shorthand {
Some(value.package_path.value)
} else {
None
}
}).unwrap_or_else(|| {
if !packages
.iter()
.any(|loc_package_entry| loc_package_entry.value.shorthand == shorthand)
{
todo!("Gracefully handle platform shorthand after `to` that didn't map to a shorthand specified in `packages`");
});
let src = package_path.to_str();
// check whether we can find a `platform` module file on disk
let platform_module_path = if src.starts_with("https://") {
#[cfg(not(target_family = "wasm"))]
{
// If this is a HTTPS package, synchronously download it
// to the cache before proceeding.
// TODO we should do this async; however, with the current
// architecture of file.rs (which doesn't use async/await),
// this would be very difficult!
let (package_dir, opt_root_module) =
cache::install_package(roc_cache_dir, src).unwrap_or_else(|err| {
todo!("TODO gracefully handle package install error {:?}", err);
});
// You can optionally specify the root module using the URL fragment,
// e.g. #foo.roc
// (defaults to main.roc)
match opt_root_module {
Some(root_module) => package_dir.join(root_module),
None => package_dir.join("main.roc"),
}
}
#[cfg(target_family = "wasm")]
{
panic!("Specifying packages via URLs is curently unsupported in wasm.");
}
} else {
app_file_dir.join(src)
};
if platform_module_path.as_path().exists() {
let load_platform_module_msg = load_platform_module(
arena,
&platform_module_path,
shorthand,
module_id,
module_ids,
ident_ids_by_module,
)?;
Ok(HeaderOutput {
module_id,
msg: Msg::Many(vec![app_module_header_msg, load_platform_module_msg]),
opt_platform_shorthand: Some(shorthand),
})
} else {
Err(LoadingProblem::FileProblem {
filename: platform_module_path,
error: io::ErrorKind::NotFound,
})
}
Ok(HeaderOutput {
module_id,
msg: Msg::Many(messages),
opt_platform_shorthand: Some(shorthand),
})
}
To::NewPackage(_package_name) => Ok(HeaderOutput {
module_id,
msg: app_module_header_msg,
msg: Msg::Many(messages),
opt_platform_shorthand: None,
}),
}
}
Ok((
ast::Module {
header: ast::Header::Package(header),
..
},
parse_state,
)) => {
let (module_id, _, header) = build_package_header(
arena,
None,
is_root_module,
filename,
parse_state,
module_ids,
ident_ids_by_module,
&header,
module_timing,
);
Ok(HeaderOutput {
module_id,
msg: Msg::Header(header),
opt_platform_shorthand: None,
})
}
Ok((
ast::Module {
header: ast::Header::Platform(header),
@ -4028,7 +4067,7 @@ fn parse_header<'a>(
None,
filename,
parse_state,
module_ids.clone(),
module_ids,
ident_ids_by_module,
&header,
module_timing,
@ -4047,6 +4086,81 @@ fn parse_header<'a>(
}
}
fn load_packages<'a>(
packages: &[Loc<PackageEntry<'a>>],
load_messages: &mut Vec<Msg<'a>>,
roc_cache_dir: RocCacheDir,
cwd: PathBuf,
arena: &'a Bump,
module_id: ModuleId,
module_ids: Arc<Mutex<PackageModuleIds<'a>>>,
ident_ids_by_module: SharedIdentIdsByModule,
) {
// Load all the packages
for Loc { value: entry, .. } in packages.iter() {
let PackageEntry {
shorthand,
package_name:
Loc {
value: package_name,
..
},
..
} = entry;
let src = package_name.to_str();
// find the `package` or `platform` module on disk,
// downloading it into a cache dir first if necessary.
let root_module_path = if src.starts_with("https://") {
#[cfg(not(target_family = "wasm"))]
{
// If this is a HTTPS package, synchronously download it
// to the cache before proceeding.
// TODO we should do this async; however, with the current
// architecture of file.rs (which doesn't use async/await),
// this would be very difficult!
let (package_dir, opt_root_module) = cache::install_package(roc_cache_dir, src)
.unwrap_or_else(|err| {
todo!("TODO gracefully handle package install error {:?}", err);
});
// You can optionally specify the root module using the URL fragment,
// e.g. #foo.roc
// (defaults to main.roc)
match opt_root_module {
Some(root_module) => package_dir.join(root_module),
None => package_dir.join("main.roc"),
}
}
#[cfg(target_family = "wasm")]
{
panic!("Specifying packages via URLs is curently unsupported in wasm.");
}
} else {
cwd.join(src)
};
match load_package_from_disk(
arena,
&root_module_path,
shorthand,
module_id,
module_ids.clone(),
ident_ids_by_module.clone(),
) {
Ok(msg) => {
load_messages.push(msg);
}
Err(problem) => {
load_messages.push(Msg::FailedToLoad(problem));
}
}
}
}
/// Load a module by its filename
fn load_filename<'a>(
arena: &'a Bump,
@ -4119,7 +4233,6 @@ struct HeaderInfo<'a> {
is_root_module: bool,
opt_shorthand: Option<&'a str>,
packages: &'a [Loc<PackageEntry<'a>>],
exposes: &'a [Loc<ExposedName<'a>>],
imports: &'a [Loc<ImportsEntry<'a>>],
header_type: HeaderType<'a>,
}
@ -4136,13 +4249,13 @@ fn build_header<'a>(
is_root_module,
opt_shorthand,
packages,
exposes,
imports,
header_type,
} = info;
let mut imported_modules: MutMap<ModuleId, Region> = MutMap::default();
let num_exposes = exposes.len();
let exposed_values = header_type.exposed_or_provided_values();
let num_exposes = exposed_values.len();
let mut deps_by_name: MutMap<PQModuleName, ModuleId> =
HashMap::with_capacity_and_hasher(num_exposes, default_hasher());
let declared_name: ModuleName = match &header_type {
@ -4161,11 +4274,15 @@ fn build_header<'a>(
);
}
// Platforms do not have names. This is important because otherwise
// Platform modules do not have names. This is important because otherwise
// those names end up in host-generated symbols, and they may contain
// characters that hosts might not allow in their function names.
String::new().into()
}
HeaderType::Package { .. } => {
// Package modules do not have names.
String::new().into()
}
HeaderType::Interface { name, .. }
| HeaderType::Builtin { name, .. }
| HeaderType::Hosted { name, .. } => {
@ -4314,7 +4431,7 @@ fn build_header<'a>(
let ident_ids = ident_ids_by_module.get_mut(&home).unwrap();
for loc_exposed in exposes.iter() {
for loc_exposed in exposed_values.iter() {
// Use get_or_insert here because the ident_ids may already
// created an IdentId for this, when it was imported exposed
// in a dependent module.
@ -4362,7 +4479,7 @@ fn build_header<'a>(
let package_entries = packages
.iter()
.map(|Loc { value: pkg, .. }| (pkg.shorthand, pkg.package_path.value))
.map(|Loc { value: pkg, .. }| (pkg.shorthand, pkg.package_name.value))
.collect::<MutMap<_, _>>();
// Send the deps to the coordinator thread for processing,
@ -4390,8 +4507,9 @@ fn build_header<'a>(
// and we just have a bunch of definitions with runtime errors in their bodies
let header_type = {
match header_type {
HeaderType::Interface { name } if home.is_builtin() => HeaderType::Builtin {
HeaderType::Interface { name, exposes } if home.is_builtin() => HeaderType::Builtin {
name,
exposes,
generates_with: &[],
},
_ => header_type,
@ -4933,6 +5051,46 @@ fn unspace<'a, T: Copy>(arena: &'a Bump, items: &[Loc<Spaced<'a, T>>]) -> &'a [L
.into_bump_slice()
}
fn build_package_header<'a>(
arena: &'a Bump,
opt_shorthand: Option<&'a str>,
is_root_module: bool,
filename: PathBuf,
parse_state: roc_parse::state::State<'a>,
module_ids: Arc<Mutex<PackageModuleIds<'a>>>,
ident_ids_by_module: SharedIdentIdsByModule,
header: &PackageHeader<'a>,
module_timing: ModuleTiming,
) -> (ModuleId, PQModuleName<'a>, ModuleHeader<'a>) {
let exposes = bumpalo::collections::Vec::from_iter_in(
unspace(arena, header.exposes.item.items).iter().copied(),
arena,
);
let packages = unspace(arena, header.packages.item.items);
let header_type = HeaderType::Package {
// A config_shorthand of "" should be fine
config_shorthand: opt_shorthand.unwrap_or_default(),
exposes: exposes.into_bump_slice(),
};
let info = HeaderInfo {
filename,
is_root_module,
opt_shorthand,
packages,
imports: &[],
header_type,
};
build_header(
info,
parse_state,
module_ids,
ident_ids_by_module,
module_timing,
)
}
fn build_platform_header<'a>(
arena: &'a Bump,
opt_shorthand: Option<&'a str>,
@ -4959,6 +5117,10 @@ fn build_platform_header<'a>(
.zip(requires.iter().copied()),
arena,
);
let exposes = bumpalo::collections::Vec::from_iter_in(
unspace(arena, header.exposes.item.items).iter().copied(),
arena,
);
let requires_types = unspace(arena, header.requires.item.rigids.items);
let imports = unspace(arena, header.imports.item.items);
@ -4967,6 +5129,7 @@ fn build_platform_header<'a>(
config_shorthand: opt_shorthand.unwrap_or_default(),
opt_app_module_id,
provides: provides.into_bump_slice(),
exposes: exposes.into_bump_slice(),
requires,
requires_types,
};
@ -4976,7 +5139,6 @@ fn build_platform_header<'a>(
is_root_module,
opt_shorthand,
packages: &[],
exposes: &[], // These are exposed values. TODO move this into header_type!
imports,
header_type,
};
@ -5057,7 +5219,11 @@ fn canonicalize_and_constrain<'a>(
// Generate documentation information
// TODO: store timing information?
let module_docs = match header_type {
HeaderType::Platform { .. } | HeaderType::App { .. } => None,
HeaderType::App { .. } => None,
HeaderType::Platform { .. } | HeaderType::Package { .. } => {
// TODO: actually generate docs for platform and package modules.
None
}
HeaderType::Interface { name, .. }
| HeaderType::Builtin { name, .. }
| HeaderType::Hosted { name, .. } => {

View File

@ -654,7 +654,8 @@ fn platform_does_not_exist() {
match multiple_modules("platform_does_not_exist", modules) {
Err(report) => {
assert!(report.contains("FILE NOT FOUND"), "report=({})", report);
// TODO restore this assert once it can pass.
// assert!(report.contains("FILE NOT FOUND"), "report=({})", report);
assert!(
report.contains("zzz-does-not-exist/main.roc"),
"report=({})",

View File

@ -9,6 +9,7 @@
"app"
"platform"
"package"
"provides"
"requires"
"exposes"

View File

@ -1,6 +1,6 @@
use std::fmt::Debug;
use crate::header::{AppHeader, HostedHeader, InterfaceHeader, PlatformHeader};
use crate::header::{AppHeader, HostedHeader, InterfaceHeader, PackageHeader, PlatformHeader};
use crate::ident::Ident;
use bumpalo::collections::{String, Vec};
use bumpalo::Bump;
@ -90,6 +90,7 @@ pub struct Module<'a> {
pub enum Header<'a> {
Interface(InterfaceHeader<'a>),
App(AppHeader<'a>),
Package(PackageHeader<'a>),
Platform(PlatformHeader<'a>),
Hosted(HostedHeader<'a>),
}

View File

@ -2,29 +2,50 @@ use crate::ast::{Collection, CommentOrNewline, Spaced, Spaces, StrLiteral, TypeA
use crate::blankspace::space0_e;
use crate::ident::{lowercase_ident, UppercaseIdent};
use crate::parser::{optional, then};
use crate::parser::{specialize, word1, EPackageEntry, EPackagePath, Parser};
use crate::parser::{specialize, word1, EPackageEntry, EPackageName, Parser};
use crate::string_literal;
use bumpalo::collections::Vec;
use roc_module::symbol::{ModuleId, Symbol};
use roc_region::all::Loc;
use std::fmt::Debug;
impl<'a> HeaderType<'a> {
pub fn exposed_or_provided_values(&'a self) -> &'a [Loc<ExposedName<'a>>] {
match self {
HeaderType::App {
provides: exposes, ..
}
| HeaderType::Hosted { exposes, .. }
| HeaderType::Builtin { exposes, .. }
| HeaderType::Interface { exposes, .. } => exposes,
HeaderType::Platform { .. } | HeaderType::Package { .. } => &[],
}
}
}
#[derive(Debug)]
pub enum HeaderType<'a> {
App {
output_name: StrLiteral<'a>,
provides: &'a [Loc<ExposedName<'a>>],
to_platform: To<'a>,
},
Hosted {
name: ModuleName<'a>,
exposes: &'a [Loc<ExposedName<'a>>],
generates: UppercaseIdent<'a>,
generates_with: &'a [Loc<ExposedName<'a>>],
},
/// Only created during canonicalization, never actually parsed from source
Builtin {
name: ModuleName<'a>,
exposes: &'a [Loc<ExposedName<'a>>],
generates_with: &'a [Symbol],
},
Package {
/// usually something other than `pf`
config_shorthand: &'a str,
exposes: &'a [Loc<ModuleName<'a>>],
},
Platform {
opt_app_module_id: Option<ModuleId>,
/// the name and type scheme of the main function (required by the platform)
@ -32,12 +53,14 @@ pub enum HeaderType<'a> {
provides: &'a [(Loc<ExposedName<'a>>, Loc<TypedIdent<'a>>)],
requires: &'a [Loc<TypedIdent<'a>>],
requires_types: &'a [Loc<UppercaseIdent<'a>>],
exposes: &'a [Loc<ModuleName<'a>>],
/// usually `pf`
config_shorthand: &'a str,
},
Interface {
name: ModuleName<'a>,
exposes: &'a [Loc<ExposedName<'a>>],
},
}
@ -59,9 +82,9 @@ pub enum VersionComparison {
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct PackagePath<'a>(&'a str);
pub struct PackageName<'a>(&'a str);
impl<'a> PackagePath<'a> {
impl<'a> PackageName<'a> {
pub fn to_str(self) -> &'a str {
self.0
}
@ -71,13 +94,13 @@ impl<'a> PackagePath<'a> {
}
}
impl<'a> From<PackagePath<'a>> for &'a str {
fn from(name: PackagePath<'a>) -> &'a str {
impl<'a> From<PackageName<'a>> for &'a str {
fn from(name: PackageName<'a>) -> &'a str {
name.0
}
}
impl<'a> From<&'a str> for PackagePath<'a> {
impl<'a> From<&'a str> for PackageName<'a> {
fn from(string: &'a str) -> Self {
Self(string)
}
@ -181,7 +204,7 @@ pub struct HostedHeader<'a> {
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum To<'a> {
ExistingPackage(&'a str),
NewPackage(PackagePath<'a>),
NewPackage(PackageName<'a>),
}
#[derive(Clone, Debug, PartialEq)]
@ -209,16 +232,11 @@ pub struct ProvidesTo<'a> {
#[derive(Clone, Debug, PartialEq)]
pub struct PackageHeader<'a> {
pub before_name: &'a [CommentOrNewline<'a>],
pub name: Loc<PackagePath<'a>>,
pub name: Loc<PackageName<'a>>,
pub exposes_keyword: Spaces<'a, ExposesKeyword>,
pub exposes: Vec<'a, Loc<Spaced<'a, ExposedName<'a>>>>,
pub packages_keyword: Spaces<'a, PackagesKeyword>,
pub packages: Vec<'a, (Loc<&'a str>, Loc<PackagePath<'a>>)>,
pub imports_keyword: Spaces<'a, ImportsKeyword>,
pub imports: Vec<'a, Loc<ImportsEntry<'a>>>,
pub exposes: KeywordItem<'a, ExposesKeyword, Collection<'a, Loc<Spaced<'a, ModuleName<'a>>>>>,
pub packages:
KeywordItem<'a, PackagesKeyword, Collection<'a, Loc<Spaced<'a, PackageEntry<'a>>>>>,
}
#[derive(Clone, Debug, PartialEq)]
@ -230,7 +248,7 @@ pub struct PlatformRequires<'a> {
#[derive(Clone, Debug, PartialEq)]
pub struct PlatformHeader<'a> {
pub before_name: &'a [CommentOrNewline<'a>],
pub name: Loc<PackagePath<'a>>,
pub name: Loc<PackageName<'a>>,
pub requires: KeywordItem<'a, RequiresKeyword, PlatformRequires<'a>>,
pub exposes: KeywordItem<'a, ExposesKeyword, Collection<'a, Loc<Spaced<'a, ModuleName<'a>>>>>,
@ -271,7 +289,7 @@ pub struct TypedIdent<'a> {
pub struct PackageEntry<'a> {
pub shorthand: &'a str,
pub spaces_after_shorthand: &'a [CommentOrNewline<'a>],
pub package_path: Loc<PackagePath<'a>>,
pub package_name: Loc<PackageName<'a>>,
}
pub fn package_entry<'a>() -> impl Parser<'a, Spaced<'a, PackageEntry<'a>>, EPackageEntry<'a>> {
@ -288,19 +306,19 @@ pub fn package_entry<'a>() -> impl Parser<'a, Spaced<'a, PackageEntry<'a>>, EPac
),
space0_e(EPackageEntry::IndentPackage)
)),
loc!(specialize(EPackageEntry::BadPackage, package_path()))
loc!(specialize(EPackageEntry::BadPackage, package_name()))
),
move |(opt_shorthand, package_or_path)| {
let entry = match opt_shorthand {
Some((shorthand, spaces_after_shorthand)) => PackageEntry {
shorthand,
spaces_after_shorthand,
package_path: package_or_path,
package_name: package_or_path,
},
None => PackageEntry {
shorthand: "",
spaces_after_shorthand: &[],
package_path: package_or_path,
package_name: package_or_path,
},
};
@ -309,13 +327,13 @@ pub fn package_entry<'a>() -> impl Parser<'a, Spaced<'a, PackageEntry<'a>>, EPac
)
}
pub fn package_path<'a>() -> impl Parser<'a, PackagePath<'a>, EPackagePath<'a>> {
pub fn package_name<'a>() -> impl Parser<'a, PackageName<'a>, EPackageName<'a>> {
then(
loc!(specialize(EPackagePath::BadPath, string_literal::parse())),
loc!(specialize(EPackageName::BadPath, string_literal::parse())),
move |_arena, state, progress, text| match text.value {
StrLiteral::PlainLine(text) => Ok((progress, PackagePath(text), state)),
StrLiteral::Line(_) => Err((progress, EPackagePath::Escapes(text.region.start()))),
StrLiteral::Block(_) => Err((progress, EPackagePath::Multiline(text.region.start()))),
StrLiteral::PlainLine(text) => Ok((progress, PackageName(text), state)),
StrLiteral::Line(_) => Err((progress, EPackageName::Escapes(text.region.start()))),
StrLiteral::Block(_) => Err((progress, EPackageName::Multiline(text.region.start()))),
},
)
}

View File

@ -1,10 +1,10 @@
use crate::ast::{Collection, Defs, Header, Module, Spaced, Spaces};
use crate::blankspace::{space0_around_ee, space0_before_e, space0_e};
use crate::header::{
package_entry, package_path, AppHeader, ExposedName, ExposesKeyword, GeneratesKeyword,
package_entry, package_name, AppHeader, ExposedName, ExposesKeyword, GeneratesKeyword,
HostedHeader, ImportsEntry, ImportsKeyword, InterfaceHeader, Keyword, KeywordItem, ModuleName,
PackageEntry, PackagesKeyword, PlatformHeader, PlatformRequires, ProvidesKeyword, ProvidesTo,
RequiresKeyword, To, ToKeyword, TypedIdent, WithKeyword,
PackageEntry, PackageHeader, PackagesKeyword, PlatformHeader, PlatformRequires,
ProvidesKeyword, ProvidesTo, RequiresKeyword, To, ToKeyword, TypedIdent, WithKeyword,
};
use crate::ident::{self, lowercase_ident, unqualified_ident, uppercase, UppercaseIdent};
use crate::parser::Progress::{self, *};
@ -67,6 +67,13 @@ fn header<'a>() -> impl Parser<'a, Module<'a>, EHeader<'a>> {
),
Header::App
),
map!(
skip_first!(
keyword_e("package", EHeader::Start),
increment_min_indent(package_header())
),
Header::Package
),
map!(
skip_first!(
keyword_e("platform", EHeader::Start),
@ -183,11 +190,22 @@ fn app_header<'a>() -> impl Parser<'a, AppHeader<'a>, EHeader<'a>> {
.trace("app_header")
}
#[inline(always)]
fn package_header<'a>() -> impl Parser<'a, PackageHeader<'a>, EHeader<'a>> {
record!(PackageHeader {
before_name: space0_e(EHeader::IndentStart),
name: loc!(specialize(EHeader::PackageName, package_name())),
exposes: specialize(EHeader::Exposes, exposes_modules()),
packages: specialize(EHeader::Packages, packages()),
})
.trace("package_header")
}
#[inline(always)]
fn platform_header<'a>() -> impl Parser<'a, PlatformHeader<'a>, EHeader<'a>> {
record!(PlatformHeader {
before_name: space0_e(EHeader::IndentStart),
name: loc!(specialize(EHeader::PlatformName, package_path())),
name: loc!(specialize(EHeader::PlatformName, package_name())),
requires: specialize(EHeader::Requires, requires()),
exposes: specialize(EHeader::Exposes, exposes_modules()),
packages: specialize(EHeader::Packages, packages()),
@ -203,7 +221,7 @@ fn provides_to_package<'a>() -> impl Parser<'a, To<'a>, EProvides<'a>> {
|_, pos| EProvides::Identifier(pos),
map!(lowercase_ident(), To::ExistingPackage)
),
specialize(EProvides::Package, map!(package_path(), To::NewPackage))
specialize(EProvides::Package, map!(package_name(), To::NewPackage))
]
}

View File

@ -127,7 +127,8 @@ pub enum EHeader<'a> {
Start(Position),
ModuleName(Position),
AppName(EString<'a>, Position),
PlatformName(EPackagePath<'a>, Position),
PackageName(EPackageName<'a>, Position),
PlatformName(EPackageName<'a>, Position),
IndentStart(Position),
InconsistentModuleName(Region),
@ -146,7 +147,7 @@ pub enum EProvides<'a> {
ListStart(Position),
ListEnd(Position),
Identifier(Position),
Package(EPackagePath<'a>, Position),
Package(EPackageName<'a>, Position),
Space(BadInputError, Position),
}
@ -202,7 +203,7 @@ pub enum EPackages<'a> {
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum EPackagePath<'a> {
pub enum EPackageName<'a> {
BadPath(EString<'a>, Position),
Escapes(Position),
Multiline(Position),
@ -210,7 +211,7 @@ pub enum EPackagePath<'a> {
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum EPackageEntry<'a> {
BadPackage(EPackagePath<'a>, Position),
BadPackage(EPackageName<'a>, Position),
Shorthand(Position),
Colon(Position),
IndentPackage(Position),

View File

@ -0,0 +1,27 @@
Module {
comments: [],
header: Package(
PackageHeader {
before_name: [],
name: @8-24 PackageName(
"rtfeldman/blah",
),
exposes: KeywordItem {
keyword: Spaces {
before: [],
item: ExposesKeyword,
after: [],
},
item: [],
},
packages: KeywordItem {
keyword: Spaces {
before: [],
item: PackagesKeyword,
after: [],
},
item: [],
},
},
),
}

View File

@ -0,0 +1 @@
package "rtfeldman/blah" exposes [] packages {}

View File

@ -3,7 +3,7 @@ Module {
header: Platform(
PlatformHeader {
before_name: [],
name: @9-25 PackagePath(
name: @9-25 PackageName(
"rtfeldman/blah",
),
requires: KeywordItem {

View File

@ -19,7 +19,7 @@ Module {
@31-47 PackageEntry {
shorthand: "pf",
spaces_after_shorthand: [],
package_path: @35-47 PackagePath(
package_name: @35-47 PackageName(
"./platform",
),
},

View File

@ -19,7 +19,7 @@ Module {
@31-47 PackageEntry {
shorthand: "pf",
spaces_after_shorthand: [],
package_path: @35-47 PackagePath(
package_name: @35-47 PackageName(
"./platform",
),
},

View File

@ -3,7 +3,7 @@ Module {
header: Platform(
PlatformHeader {
before_name: [],
name: @9-14 PackagePath(
name: @9-14 PackageName(
"cli",
),
requires: KeywordItem {

View File

@ -22,7 +22,7 @@ Module {
after: [],
},
to: @30-38 NewPackage(
PackagePath(
PackageName(
"./blah",
),
),

View File

@ -0,0 +1,46 @@
Module {
comments: [],
header: Package(
PackageHeader {
before_name: [],
name: @8-20 PackageName(
"foo/barbaz",
),
exposes: KeywordItem {
keyword: Spaces {
before: [
Newline,
],
item: ExposesKeyword,
after: [],
},
item: [
@34-37 ModuleName(
"Foo",
),
@39-42 ModuleName(
"Bar",
),
],
},
packages: KeywordItem {
keyword: Spaces {
before: [
Newline,
],
item: PackagesKeyword,
after: [],
},
item: [
@59-71 PackageEntry {
shorthand: "foo",
spaces_after_shorthand: [],
package_name: @64-71 PackageName(
"./foo",
),
},
],
},
},
),
}

View File

@ -0,0 +1,3 @@
package "foo/barbaz"
exposes [Foo, Bar]
packages { foo: "./foo" }

View File

@ -3,7 +3,7 @@ Module {
header: Platform(
PlatformHeader {
before_name: [],
name: @9-21 PackagePath(
name: @9-21 PackageName(
"foo/barbaz",
),
requires: KeywordItem {
@ -52,7 +52,7 @@ Module {
@87-99 PackageEntry {
shorthand: "foo",
spaces_after_shorthand: [],
package_path: @92-99 PackagePath(
package_name: @92-99 PackageName(
"./foo",
),
},

View File

@ -19,7 +19,7 @@ Module {
@26-42 PackageEntry {
shorthand: "pf",
spaces_after_shorthand: [],
package_path: @30-42 PackagePath(
package_name: @30-42 PackageName(
"./platform",
),
},

View File

@ -3,7 +3,7 @@ Module {
header: Platform(
PlatformHeader {
before_name: [],
name: @9-21 PackagePath(
name: @9-21 PackageName(
"test/types",
),
requires: KeywordItem {

View File

@ -224,6 +224,7 @@ mod test_parse {
pass/empty_hosted_header.header,
pass/empty_interface_header.header,
pass/empty_list.expr,
pass/empty_package_header.header,
pass/empty_platform_header.header,
pass/empty_record.expr,
pass/empty_string.expr,
@ -280,6 +281,7 @@ mod test_parse {
pass/newline_inside_empty_list.expr,
pass/newline_singleton_list.expr,
pass/nonempty_hosted_header.header,
pass/nonempty_package_header.header,
pass/nonempty_platform_header.header,
pass/not_docs.expr,
pass/number_literal_suffixes.expr,

View File

@ -1362,18 +1362,7 @@ fn str_trim_right_small_to_small_shared() {
#[test]
#[cfg(any(feature = "gen-llvm"))]
fn str_to_nat() {
assert_evals_to!(
indoc!(
r#"
when Str.toNat "1" is
Ok n -> n
Err _ -> 0
"#
),
1,
usize
);
assert_evals_to!(r#"Str.toNat "1" |> Result.withDefault 0"#, 1, usize);
}
#[test]

View File

@ -237,11 +237,11 @@ where
T: FromWasm32Memory + Wasm32Result,
{
let dispatcher = TestDispatcher {
wasi: wasi::WasiDispatcher { args: &[] },
wasi: wasi::WasiDispatcher::default(),
};
let is_debug_mode = roc_debug_flags::dbg_set!(roc_debug_flags::ROC_LOG_WASM_INTERP);
let mut inst = Instance::for_module(&arena, &module, dispatcher, is_debug_mode)?;
let opt_value = inst.call_export(module, test_wrapper_name, [])?;
let opt_value = inst.call_export(test_wrapper_name, [])?;
let addr_value = opt_value.ok_or("No return address from Wasm test")?;
let addr = addr_value.expect_i32().map_err(|e| format!("{:?}", e))?;
let output = <T as FromWasm32Memory>::decode(&inst.memory, addr as u32);
@ -266,25 +266,21 @@ where
.map_err(|e| format!("{:?}", e))?;
let dispatcher = TestDispatcher {
wasi: wasi::WasiDispatcher { args: &[] },
wasi: wasi::WasiDispatcher::default(),
};
let is_debug_mode = roc_debug_flags::dbg_set!(roc_debug_flags::ROC_LOG_WASM_INTERP);
let mut inst = Instance::for_module(&arena, &module, dispatcher, is_debug_mode)?;
// Allocate a vector in the test host that refcounts will be copied into
let mut refcount_vector_addr: i32 = inst
.call_export(
&module,
INIT_REFCOUNT_NAME,
[Value::I32(num_refcounts as i32)],
)?
.call_export(INIT_REFCOUNT_NAME, [Value::I32(num_refcounts as i32)])?
.ok_or_else(|| format!("No return address from {}", INIT_REFCOUNT_NAME))?
.expect_i32()
.map_err(|type_err| format!("{:?}", type_err))?;
// Run the test, ignoring the result
let _result_addr: i32 = inst
.call_export(&module, TEST_WRAPPER_NAME, [])?
.call_export(TEST_WRAPPER_NAME, [])?
.ok_or_else(|| format!("No return address from {}", TEST_WRAPPER_NAME))?
.expect_i32()
.map_err(|type_err| format!("{:?}", type_err))?;

View File

@ -225,22 +225,22 @@ fn execute_wasm_module<'a>(arena: &'a Bump, orig_module: WasmModule<'a>) -> Resu
};
let dispatcher = TestDispatcher {
wasi: wasi::WasiDispatcher { args: &[] },
wasi: wasi::WasiDispatcher::default(),
};
let is_debug_mode = true;
let is_debug_mode = false;
let mut inst = Instance::for_module(&arena, &module, dispatcher, is_debug_mode)?;
// In Zig, main can only return u8 or void, but our result is too wide for that.
// But I want to use main so that I can test that _start is created for it!
// So return void from main, and call another function to get the result.
inst.call_export(&module, "_start", [])?;
inst.call_export("_start", [])?;
// FIXME: read_host_result does not actually appear as an export!
// The interpreter has to look it up in debug info! (Apparently Wasm3 did this!)
// If we change gen_wasm to export it, then it does the same for js_unused,
// so we can't test import elimination and function reordering.
// We should to come back to this and fix it.
inst.call_export(&module, "read_host_result", [])?
inst.call_export("read_host_result", [])?
.ok_or(String::from("expected a return value"))?
.expect_i32()
.map_err(|type_err| format!("{:?}", type_err))

View File

@ -31,6 +31,7 @@ pub enum Token {
KeywordTo = 0b_0010_1110,
KeywordExposes = 0b_0010_1111,
KeywordEffects = 0b_0011_0000,
KeywordPackage = 0b_0111_1100,
KeywordPlatform = 0b_0011_0001,
KeywordRequires = 0b_0011_0010,
KeywordDbg = 0b_0111_1011,
@ -428,6 +429,7 @@ fn lex_ident(uppercase: bool, bytes: &[u8]) -> (Token, usize) {
b"to" => Token::KeywordTo,
b"exposes" => Token::KeywordExposes,
b"effects" => Token::KeywordEffects,
b"package" => Token::KeywordPackage,
b"platform" => Token::KeywordPlatform,
b"requires" => Token::KeywordRequires,
ident => {

View File

@ -10,6 +10,7 @@ use std::fs::File;
use std::io::{self, Read, Write};
use std::path::Path;
use tar;
use walkdir::WalkDir;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Compression {
@ -124,7 +125,9 @@ fn write_archive<W: Write>(path: &Path, writer: W) -> io::Result<()> {
let arena = Bump::new();
let mut buf = Vec::new();
let _other_modules: &[Module<'_>] = match read_header(&arena, &mut buf, path)?.header {
// TODO use this when finding .roc files by discovering them from the root module.
// let other_modules: &[Module<'_>] =
match read_header(&arena, &mut buf, path)?.header {
Header::Interface(_) => {
todo!();
// TODO report error
@ -137,9 +140,10 @@ fn write_archive<W: Write>(path: &Path, writer: W) -> io::Result<()> {
todo!();
// TODO report error
}
Header::Package(_) => {
add_dot_roc_files(root_dir, &mut builder)?;
}
Header::Platform(PlatformHeader { imports: _, .. }) => {
use walkdir::WalkDir;
// Add all the prebuilt host files to the archive.
// These should all be in the same directory as the platform module.
for entry in std::fs::read_dir(root_dir)? {
@ -170,38 +174,7 @@ fn write_archive<W: Write>(path: &Path, writer: W) -> io::Result<()> {
}
}
// Recursively find all the .roc files and add them.
// TODO we can do this more efficiently by parsing the platform module and finding
// all of its dependencies. See below for a commented-out WIP sketch of this.
//
// The WalkDir approach is easier to implement, but has the downside of doing things
// like traversing target/ and zig-cache/ which can be large but will never have
// any .roc files in them!
for entry in WalkDir::new(root_dir).into_iter().filter_entry(|entry| {
let path = entry.path();
// We already got the prebuilt host files, so the only other things
// we care about are .roc files.
path.is_dir() || path.extension().and_then(OsStr::to_str) == Some("roc")
}) {
let entry = entry?;
// Only include files, not directories or symlinks.
// Symlinks may not work on Windows, and directories will get automatically
// added based on the paths of the files inside anyway. (In fact, if we don't
// filter out directories in this step, then empty ones will end up getting added!)
if entry.path().is_file() {
builder.append_path_with_name(
entry.path(),
// Store it without the root path, so that (for example) we don't store
// `examples/cli/main.roc` and therefore end up with the root of the tarball
// being an `examples/cli/` dir instead of having `main.roc` in the root.
entry.path().strip_prefix(root_dir).unwrap(),
)?;
}
}
&[]
add_dot_roc_files(root_dir, &mut builder)?;
}
};
@ -246,6 +219,37 @@ fn write_archive<W: Write>(path: &Path, writer: W) -> io::Result<()> {
builder.finish()
}
fn add_dot_roc_files<W: Write>(
root_dir: &Path,
builder: &mut tar::Builder<W>,
) -> Result<(), io::Error> {
for entry in WalkDir::new(root_dir).into_iter().filter_entry(|entry| {
let path = entry.path();
// Ignore everything except directories and .roc files
path.is_dir() || path.extension().and_then(OsStr::to_str) == Some("roc")
}) {
let entry = entry?;
let path = entry.path();
// Only include files, not directories or symlinks.
// Symlinks may not work on Windows, and directories will get automatically
// added based on the paths of the files inside anyway. (In fact, if we don't
// filter out directories in this step, then empty ones can sometimes be added!)
if path.is_file() {
builder.append_path_with_name(
path,
// Store it without the root path, so that (for example) we don't store
// `examples/cli/main.roc` and therefore end up with the root of the tarball
// being an `examples/cli/` dir instead of having `main.roc` in the root.
path.strip_prefix(root_dir).unwrap(),
)?;
}
}
Ok(())
}
fn read_header<'a>(
arena: &'a Bump,
buf: &'a mut Vec<u8>,

View File

@ -9,24 +9,15 @@ description = "Tests the roc REPL."
[build-dependencies]
roc_cli = {path = "../cli"}
[dependencies]
lazy_static = "1.4.0"
[dev-dependencies]
indoc = "1.0.7"
strip-ansi-escapes = "0.1.1"
wasmer-wasi = "2.2.1"
bumpalo.workspace = true
roc_build = { path = "../compiler/build" }
roc_repl_cli = {path = "../repl_cli"}
roc_test_utils = {path = "../test_utils"}
# Wasmer singlepass compiler only works on x86_64.
[target.'cfg(target_arch = "x86_64")'.dev-dependencies]
wasmer = { version = "2.2.1", default-features = false, features = ["singlepass", "universal"] }
[target.'cfg(not(target_arch = "x86_64"))'.dev-dependencies]
wasmer = { version = "2.2.1", default-features = false, features = ["cranelift", "universal"] }
roc_wasm_interp = {path = "../wasm_interp"}
[features]
default = ["target-aarch64", "target-x86_64", "target-wasm32"]

View File

@ -1,8 +1,3 @@
//! Tests the roc REPL.
#[allow(unused_imports)]
#[macro_use]
extern crate lazy_static;
#[cfg(test)]
mod tests;

View File

@ -1,289 +1,166 @@
use std::{
cell::RefCell,
fs,
ops::{Deref, DerefMut},
path::Path,
sync::Mutex,
thread_local,
use bumpalo::Bump;
use roc_wasm_interp::{
wasi, DefaultImportDispatcher, ImportDispatcher, Instance, Value, WasiDispatcher,
};
use wasmer::{
imports, ChainableNamedResolver, Function, ImportObject, Instance, Module, Store, Value,
};
use wasmer_wasi::WasiState;
const WASM_REPL_COMPILER_PATH: &str = "../../target/wasm32-wasi/release/roc_repl_wasm.wasm";
const COMPILER_BYTES: &[u8] =
include_bytes!("../../../target/wasm32-wasi/release/roc_repl_wasm.wasm");
thread_local! {
static REPL_STATE: RefCell<Option<ReplState>> = RefCell::new(None)
struct CompilerDispatcher<'a> {
arena: &'a Bump,
src: &'a str,
answer: String,
wasi: WasiDispatcher<'a>,
app: Option<Instance<'a, DefaultImportDispatcher<'a>>>,
result_addr: Option<i32>,
}
// The compiler Wasm instance.
// This takes several *seconds* to initialise, so we only want to do it once for all tests.
// Every test mutates compiler memory in `unsafe` ways, so we run them sequentially using a Mutex.
// Even if Cargo uses many threads, these tests won't go any faster. But that's fine, they're quick.
lazy_static! {
static ref COMPILER: Instance = init_compiler();
}
static TEST_MUTEX: Mutex<()> = Mutex::new(());
/// Load the compiler .wasm file and get it ready to execute
/// THIS FUNCTION TAKES 4 SECONDS TO RUN
fn init_compiler() -> Instance {
let path = Path::new(WASM_REPL_COMPILER_PATH);
let wasm_module_bytes = match fs::read(&path) {
Ok(bytes) => bytes,
Err(e) => panic!("{}", format_compiler_load_error(e)),
};
println!("loaded Roc compiler bytes");
let store = Store::default();
// This is the slow line. Skipping validation checks reduces module compilation time from 5s to 4s.
// Safety: We trust rustc to produce a valid module.
let wasmer_module =
unsafe { Module::from_binary_unchecked(&store, &wasm_module_bytes).unwrap() };
// Specify the external functions the Wasm module needs to link to
// We only use WASI so that we can debug test failures more easily with println!(), dbg!(), etc.
let mut wasi_env = WasiState::new("compiler").finalize().unwrap();
let wasi_import_obj = wasi_env
.import_object(&wasmer_module)
.unwrap_or_else(|_| ImportObject::new());
let repl_import_obj = imports! {
"env" => {
"wasmer_create_app" => Function::new_native(&store, wasmer_create_app),
"wasmer_run_app" => Function::new_native(&store, wasmer_run_app),
"wasmer_get_result_and_memory" => Function::new_native(&store, wasmer_get_result_and_memory),
"wasmer_copy_input_string" => Function::new_native(&store, wasmer_copy_input_string),
"wasmer_copy_output_string" => Function::new_native(&store, wasmer_copy_output_string),
"now" => Function::new_native(&store, dummy_system_time_now),
}
};
// "Chain" the import objects together. Wasmer will look up the REPL object first, then the WASI object
let import_object = wasi_import_obj.chain_front(repl_import_obj);
println!("Instantiating Roc compiler");
// Make a fully-linked instance with its own block of memory
let inst = Instance::new(&wasmer_module, &import_object).unwrap();
println!("Instantiated Roc compiler");
inst
}
struct ReplState {
src: &'static str,
app: Option<Instance>,
result_addr: Option<u32>,
output: Option<String>,
}
fn wasmer_create_app(app_bytes_ptr: u32, app_bytes_len: u32) -> u32 {
let app: Instance = {
let memory = COMPILER.exports.get_memory("memory").unwrap();
let memory_bytes: &[u8] = unsafe { memory.data_unchecked() };
// Find the slice of bytes for the compiled Roc app
let ptr = app_bytes_ptr as usize;
let len = app_bytes_len as usize;
let app_module_bytes: &[u8] = &memory_bytes[ptr..][..len];
// Parse the bytes into a Wasmer module
let store = Store::default();
let wasmer_module = match Module::new(&store, app_module_bytes) {
Ok(m) => m,
Err(e) => {
println!("Failed to create Wasm module\n{:?}", e);
if false {
let path = std::env::temp_dir().join("roc_repl_test_invalid_app.wasm");
fs::write(&path, app_module_bytes).unwrap();
println!("Wrote invalid wasm to {:?}", path);
}
return false.into();
}
impl<'a> ImportDispatcher for CompilerDispatcher<'a> {
fn dispatch(
&mut self,
module_name: &str,
function_name: &str,
arguments: &[Value],
compiler_memory: &mut [u8],
) -> Option<Value> {
let unknown = || {
panic!(
"I could not find an implementation for import {}.{}",
module_name, function_name
)
};
// Get the WASI imports for the app
let mut wasi_env = WasiState::new("app").finalize().unwrap();
let import_object = wasi_env
.import_object(&wasmer_module)
.unwrap_or_else(|_| imports!());
if module_name == wasi::MODULE_NAME {
self.wasi
.dispatch(function_name, arguments, compiler_memory)
} else if module_name == "env" {
match function_name {
"test_create_app" => {
// Get some bytes from the compiler Wasm instance and create the app Wasm instance
// fn test_create_app(app_bytes_ptr: *const u8, app_bytes_len: usize) -> u32;
assert_eq!(arguments.len(), 2);
let app_bytes_ptr = arguments[0].expect_i32().unwrap() as usize;
let app_bytes_len = arguments[1].expect_i32().unwrap() as usize;
let app_bytes = &compiler_memory[app_bytes_ptr..][..app_bytes_len];
// Create an executable instance
match Instance::new(&wasmer_module, &import_object) {
Ok(instance) => instance,
Err(e) => {
println!("Failed to create Wasm instance {:?}", e);
return false.into();
}
}
};
let is_debug_mode = false;
let instance = Instance::from_bytes(
self.arena,
app_bytes,
DefaultImportDispatcher::default(),
is_debug_mode,
)
.unwrap();
REPL_STATE.with(|f| {
if let Some(state) = f.borrow_mut().deref_mut() {
state.app = Some(app)
} else {
unreachable!()
}
});
return true.into();
}
fn wasmer_run_app() -> u32 {
REPL_STATE.with(|f| {
if let Some(state) = f.borrow_mut().deref_mut() {
if let Some(app) = &state.app {
let wrapper = app.exports.get_function("wrapper").unwrap();
let result_addr: i32 = match wrapper.call(&[]) {
Err(e) => panic!("{:?}", e),
Ok(result) => result[0].unwrap_i32(),
};
state.result_addr = Some(result_addr as u32);
let memory = app.exports.get_memory("memory").unwrap();
memory.size().bytes().0 as u32
} else {
unreachable!()
self.app = Some(instance);
let ok = Value::I32(true as i32);
Some(ok)
}
"test_run_app" => {
// fn test_run_app() -> usize;
assert_eq!(arguments.len(), 0);
match &mut self.app {
Some(instance) => {
let result_addr = instance
.call_export("wrapper", [])
.unwrap()
.expect("No return address from wrapper")
.expect_i32()
.unwrap();
self.result_addr = Some(result_addr);
let memory_size = instance.memory.len();
Some(Value::I32(memory_size as i32))
}
None => panic!("Trying to run the app but it hasn't been created"),
}
}
"test_get_result_and_memory" => {
// Copy the app's entire memory buffer into the compiler's memory,
// and return the location in that buffer where we can find the app result.
// fn test_get_result_and_memory(buffer_alloc_addr: *mut u8) -> usize;
assert_eq!(arguments.len(), 1);
let buffer_alloc_addr = arguments[0].expect_i32().unwrap() as usize;
match &self.app {
Some(instance) => {
let len = instance.memory.len();
compiler_memory[buffer_alloc_addr..][..len]
.copy_from_slice(&instance.memory);
self.result_addr.map(Value::I32)
}
None => panic!("Trying to get result and memory but there is no app"),
}
}
"test_copy_input_string" => {
// Copy the Roc source code from the test into the compiler Wasm instance
// fn test_copy_input_string(src_buffer_addr: *mut u8);
assert_eq!(arguments.len(), 1);
let src_buffer_addr = arguments[0].expect_i32().unwrap() as usize;
let len = self.src.len();
compiler_memory[src_buffer_addr..][..len].copy_from_slice(self.src.as_bytes());
None
}
"test_copy_output_string" => {
// The REPL now has a string representing the answer. Make it available to the test code.
// fn test_copy_output_string(output_ptr: *const u8, output_len: usize);
assert_eq!(arguments.len(), 2);
let output_ptr = arguments[0].expect_i32().unwrap() as usize;
let output_len = arguments[1].expect_i32().unwrap() as usize;
match std::str::from_utf8(&compiler_memory[output_ptr..][..output_len]) {
Ok(answer) => {
self.answer = answer.into();
}
Err(e) => panic!("{:?}", e),
}
None
}
"now" => Some(Value::F64(0.0)),
_ => unknown(),
}
} else {
unreachable!()
unknown()
}
})
}
fn wasmer_get_result_and_memory(buffer_alloc_addr: u32) -> u32 {
REPL_STATE.with(|f| {
if let Some(state) = f.borrow().deref() {
if let Some(app) = &state.app {
let app_memory = app.exports.get_memory("memory").unwrap();
let result_addr = state.result_addr.unwrap();
let app_memory_bytes: &[u8] = unsafe { app_memory.data_unchecked() };
let buf_addr = buffer_alloc_addr as usize;
let len = app_memory_bytes.len();
let memory = COMPILER.exports.get_memory("memory").unwrap();
let compiler_memory_bytes: &mut [u8] = unsafe { memory.data_unchecked_mut() };
compiler_memory_bytes[buf_addr..][..len].copy_from_slice(app_memory_bytes);
result_addr
} else {
panic!("REPL app not found")
}
} else {
panic!("REPL state not found")
}
})
}
fn wasmer_copy_input_string(src_buffer_addr: u32) {
let src = REPL_STATE.with(|rs| {
if let Some(state) = rs.borrow_mut().deref_mut() {
std::mem::take(&mut state.src)
} else {
unreachable!()
}
});
let memory = COMPILER.exports.get_memory("memory").unwrap();
let memory_bytes: &mut [u8] = unsafe { memory.data_unchecked_mut() };
let buf_addr = src_buffer_addr as usize;
let len = src.len();
memory_bytes[buf_addr..][..len].copy_from_slice(src.as_bytes());
}
fn wasmer_copy_output_string(output_ptr: u32, output_len: u32) {
let output: String = {
let memory = COMPILER.exports.get_memory("memory").unwrap();
let memory_bytes: &[u8] = unsafe { memory.data_unchecked() };
// Find the slice of bytes for the output string
let ptr = output_ptr as usize;
let len = output_len as usize;
let output_bytes: &[u8] = &memory_bytes[ptr..][..len];
// Copy it out of the Wasm module
let copied_bytes = output_bytes.to_vec();
unsafe { String::from_utf8_unchecked(copied_bytes) }
};
REPL_STATE.with(|f| {
if let Some(state) = f.borrow_mut().deref_mut() {
state.output = Some(output)
}
})
}
fn format_compiler_load_error(e: std::io::Error) -> String {
if matches!(e.kind(), std::io::ErrorKind::NotFound) {
format!(
"\n\n {}\n\n",
[
"ROC COMPILER WASM BINARY NOT FOUND",
"Please run these tests using repl_test/run_wasm.sh!",
"It will build a .wasm binary for the compiler, and a native binary for the tests themselves",
]
.join("\n ")
)
} else {
format!("{:?}", e)
}
}
fn dummy_system_time_now() -> f64 {
0.0
}
fn run(src: &'static str) -> Result<String, String> {
let arena = Bump::new();
fn run(src: &'static str) -> (bool, String) {
println!("run");
REPL_STATE.with(|rs| {
*rs.borrow_mut().deref_mut() = Some(ReplState {
let mut instance = {
let dispatcher = CompilerDispatcher {
arena: &arena,
src,
answer: String::new(),
wasi: WasiDispatcher::default(),
app: None,
result_addr: None,
output: None,
});
});
};
let ok = if let Ok(_guard) = TEST_MUTEX.lock() {
let entrypoint = COMPILER
.exports
.get_function("entrypoint_from_test")
.unwrap();
let src_len = Value::I32(src.len() as i32);
let wasm_ok: i32 = entrypoint.call(&[src_len]).unwrap().deref()[0].unwrap_i32();
wasm_ok != 0
} else {
panic!(
"Failed to acquire test mutex! A previous test must have panicked while holding it, running Wasm"
)
let is_debug_mode = false; // logs every instruction!
Instance::from_bytes(&arena, COMPILER_BYTES, dispatcher, is_debug_mode).unwrap()
};
let final_state: ReplState = REPL_STATE.with(|rs| rs.take()).unwrap();
let output: String = final_state.output.unwrap();
let len = Value::I32(src.len() as i32);
let wasm_ok: i32 = instance
.call_export("entrypoint_from_test", [len])
.unwrap()
.unwrap()
.expect_i32()
.unwrap();
let answer_str = instance.import_dispatcher.answer.to_owned();
(ok, output)
if wasm_ok == 0 {
Err(answer_str)
} else {
Ok(answer_str)
}
}
#[allow(dead_code)]
pub fn expect_success(input: &'static str, expected: &str) {
let (ok, output) = run(input);
if !ok {
panic!("\n{}\n", output);
}
assert_eq!(output, expected);
assert_eq!(run(input), Ok(expected.into()));
}
#[allow(dead_code)]
pub fn expect_failure(input: &'static str, expected: &str) {
let (ok, output) = run(input);
assert_eq!(ok, false);
assert_eq!(output, expected);
assert_eq!(run(input), Err(expected.into()));
}

View File

@ -7,7 +7,7 @@ set -euxo pipefail
# We need to clear RUSTFLAGS for this command, as CI sets normally some flags that are specific to CPU targets.
# Tests target wasm32-wasi instead of wasm32-unknown-unknown, so that we can debug with println! and dbg!
RUSTFLAGS="" cargo build --target wasm32-wasi -p roc_repl_wasm --no-default-features --features wasmer --release
RUSTFLAGS="" cargo build --locked --release --target wasm32-wasi -p roc_repl_wasm --no-default-features --features wasi_test
# Build & run the test code on *native* target, not WebAssembly
cargo test -p repl_test --features wasm -- --test-threads=1
cargo test --locked --release -p repl_test --features wasm

View File

@ -33,7 +33,7 @@ roc_target = {path = "../compiler/roc_target"}
roc_types = {path = "../compiler/types"}
[features]
wasmer = ["futures"]
wasi_test = ["futures"]
# Tell wasm-pack not to run wasm-opt automatically. We run it explicitly when we need to.
# (Workaround for a CI install issue with wasm-pack https://github.com/rustwasm/wasm-pack/issues/864)

View File

@ -20,7 +20,7 @@ extern "C" {
// To debug in the browser, start up the web REPL as per instructions in repl_www/README.md
// and sprinkle your code with console_log!("{:?}", my_value);
// (Or if you're running the unit tests in Wasmer, you can just use println! or dbg!)
// (Or if you're running the unit tests with WASI, you can just use println! or dbg!)
#[macro_export]
macro_rules! console_log {
($($t:tt)*) => (log(&format_args!($($t)*).to_string()))

View File

@ -1,16 +1,16 @@
use futures::executor;
extern "C" {
fn wasmer_create_app(app_bytes_ptr: *const u8, app_bytes_len: usize) -> u32;
fn wasmer_run_app() -> usize;
fn wasmer_get_result_and_memory(buffer_alloc_addr: *mut u8) -> usize;
fn wasmer_copy_input_string(src_buffer_addr: *mut u8);
fn wasmer_copy_output_string(output_ptr: *const u8, output_len: usize);
fn test_create_app(app_bytes_ptr: *const u8, app_bytes_len: usize) -> u32;
fn test_run_app() -> usize;
fn test_get_result_and_memory(buffer_alloc_addr: *mut u8) -> usize;
fn test_copy_input_string(src_buffer_addr: *mut u8);
fn test_copy_output_string(output_ptr: *const u8, output_len: usize);
}
/// Async wrapper to match the equivalent JS function
pub async fn js_create_app(wasm_module_bytes: &[u8]) -> Result<(), String> {
let ok = unsafe { wasmer_create_app(wasm_module_bytes.as_ptr(), wasm_module_bytes.len()) } != 0;
let ok = unsafe { test_create_app(wasm_module_bytes.as_ptr(), wasm_module_bytes.len()) } != 0;
if ok {
Ok(())
} else {
@ -19,22 +19,21 @@ pub async fn js_create_app(wasm_module_bytes: &[u8]) -> Result<(), String> {
}
pub fn js_run_app() -> usize {
unsafe { wasmer_run_app() }
unsafe { test_run_app() }
}
pub fn js_get_result_and_memory(buffer_alloc_addr: *mut u8) -> usize {
unsafe { wasmer_get_result_and_memory(buffer_alloc_addr) }
unsafe { test_get_result_and_memory(buffer_alloc_addr) }
}
/// Entrypoint for Wasmer tests
/// Entrypoint for tests using WASI and a CLI interpreter
/// - Synchronous API, to avoid the need to run an async executor across the Wasm/native boundary.
/// (wasmer has a sync API for creating an Instance, whereas browsers don't)
/// - Uses an extra callback to allocate & copy the input string (wasm_bindgen does this for JS)
/// - Uses an extra callback to allocate & copy the input string (in the browser version, wasm_bindgen does this)
#[no_mangle]
pub extern "C" fn entrypoint_from_test(src_len: usize) -> bool {
let mut src_buffer = std::vec![0; src_len];
let src = unsafe {
wasmer_copy_input_string(src_buffer.as_mut_ptr());
test_copy_input_string(src_buffer.as_mut_ptr());
String::from_utf8_unchecked(src_buffer)
};
let result_async = crate::repl::entrypoint_from_js(src);
@ -43,7 +42,7 @@ pub extern "C" fn entrypoint_from_test(src_len: usize) -> bool {
let output = result.unwrap_or_else(|s| s);
unsafe { wasmer_copy_output_string(output.as_ptr(), output.len()) }
unsafe { test_copy_output_string(output.as_ptr(), output.len()) }
ok
}

View File

@ -6,15 +6,15 @@ mod repl;
//
#[cfg(feature = "console_error_panic_hook")]
extern crate console_error_panic_hook;
#[cfg(not(feature = "wasmer"))]
#[cfg(not(feature = "wasi_test"))]
mod externs_js;
#[cfg(not(feature = "wasmer"))]
#[cfg(not(feature = "wasi_test"))]
pub use externs_js::{entrypoint_from_js, js_create_app, js_get_result_and_memory, js_run_app};
//
// Interface with test code outside the Wasm module
//
#[cfg(feature = "wasmer")]
#[cfg(feature = "wasi_test")]
mod externs_test;
#[cfg(feature = "wasmer")]
#[cfg(feature = "wasi_test")]
pub use externs_test::{entrypoint_from_test, js_create_app, js_get_result_and_memory, js_run_app};

View File

@ -177,6 +177,8 @@ enum Node {
InsideParens,
RecordConditionalDefault,
StringFormat,
Dbg,
Expect,
}
fn to_expr_report<'a>(
@ -397,6 +399,8 @@ fn to_expr_report<'a>(
]),
),
Node::ListElement => (pos, alloc.text("a list")),
Node::Dbg => (pos, alloc.text("a dbg statement")),
Node::Expect => (pos, alloc.text("an expect statement")),
Node::RecordConditionalDefault => (pos, alloc.text("record field default")),
Node::StringFormat => (pos, alloc.text("a string format")),
Node::InsideParens => (pos, alloc.text("some parentheses")),
@ -557,14 +561,52 @@ fn to_expr_report<'a>(
EExpr::IndentEnd(pos) => {
let surroundings = Region::new(start, *pos);
let region = LineColumnRegion::from_pos(lines.convert_pos(*pos));
let doc = alloc.stack(vec![
alloc.reflow(r"I am partway through parsing an expression, but I got stuck here:"),
alloc.region_with_subregion(lines.convert_region(surroundings), region),
alloc.concat(vec![
alloc.reflow("Looks like the indentation ends prematurely here. "),
alloc.reflow("Did you mean to have another expression after this line?"),
let snippet = alloc.region_with_subregion(lines.convert_region(surroundings), region);
let doc = match context {
Context::InNode(Node::Dbg, _, _) => alloc.stack([
alloc.reflow(
r"I am partway through parsing a dbg statement, but I got stuck here:",
),
snippet,
alloc.stack([
alloc.reflow(r"I was expecting a final expression, like so"),
alloc.vcat([
alloc.parser_suggestion("dbg 42").indent(4),
alloc.parser_suggestion("\"done\"").indent(4),
]),
]),
]),
]);
Context::InNode(Node::Expect, _, _) => alloc.stack([
alloc.reflow(
r"I am partway through parsing an expect statement, but I got stuck here:",
),
snippet,
alloc.stack([
alloc.reflow(r"I was expecting a final expression, like so"),
alloc.vcat([
alloc.parser_suggestion("expect 1 + 1 == 2").indent(4),
alloc.parser_suggestion("\"done\"").indent(4),
]),
]),
]),
_ => {
// generic
alloc.stack([
alloc.reflow(
r"I am partway through parsing an expression, but I got stuck here:",
),
snippet,
alloc.concat([
alloc.reflow(r"Looks like the indentation ends prematurely here. "),
alloc.reflow(
r"Did you mean to have another expression after this line?",
),
]),
])
}
};
Report {
filename,
@ -573,6 +615,13 @@ fn to_expr_report<'a>(
severity: Severity::RuntimeError,
}
}
EExpr::Expect(e_expect, _position) => {
let node = Node::Expect;
to_dbg_or_expect_report(alloc, lines, filename, context, node, e_expect, start)
}
EExpr::Dbg(e_expect, _position) => {
to_dbg_or_expect_report(alloc, lines, filename, context, Node::Dbg, e_expect, start)
}
_ => todo!("unhandled parse error: {:?}", parse_problem),
}
}
@ -1191,6 +1240,36 @@ fn to_list_report<'a>(
}
}
fn to_dbg_or_expect_report<'a>(
alloc: &'a RocDocAllocator<'a>,
lines: &LineInfo,
filename: PathBuf,
context: Context,
node: Node,
parse_problem: &roc_parse::parser::EExpect<'a>,
start: Position,
) -> Report<'a> {
match parse_problem {
roc_parse::parser::EExpect::Space(err, pos) => {
to_space_report(alloc, lines, filename, err, *pos)
}
roc_parse::parser::EExpect::Dbg(_) => unreachable!("another branch would be taken"),
roc_parse::parser::EExpect::Expect(_) => unreachable!("another branch would be taken"),
roc_parse::parser::EExpect::Condition(e_expr, condition_start) => {
// is adding context helpful here?
to_expr_report(alloc, lines, filename, context, e_expr, *condition_start)
}
roc_parse::parser::EExpect::Continuation(e_expr, continuation_start) => {
let context = Context::InNode(node, start, Box::new(context));
to_expr_report(alloc, lines, filename, context, e_expr, *continuation_start)
}
roc_parse::parser::EExpect::IndentCondition(_) => todo!(),
}
}
fn to_if_report<'a>(
alloc: &'a RocDocAllocator<'a>,
lines: &LineInfo,
@ -3299,6 +3378,8 @@ fn to_header_report<'a>(
alloc.keyword("interface"),
alloc.reflow(", "),
alloc.keyword("app"),
alloc.reflow(", "),
alloc.keyword("package"),
alloc.reflow(" or "),
alloc.keyword("platform"),
alloc.reflow("."),
@ -3388,12 +3469,35 @@ fn to_header_report<'a>(
}
}
EHeader::PackageName(_, pos) => {
let surroundings = Region::new(start, *pos);
let region = LineColumnRegion::from_pos(lines.convert_pos(*pos));
let doc = alloc.stack([
alloc.reflow(r"I am partway through parsing a package header, but got stuck here:"),
alloc.region_with_subregion(lines.convert_region(surroundings), region),
alloc.concat([
alloc.reflow("I am expecting a package name next, like "),
alloc.parser_suggestion("\"roc/core\""),
alloc.reflow(". Package names must be quoted."),
]),
]);
Report {
filename,
doc,
title: "INVALID PACKAGE NAME".to_string(),
severity: Severity::RuntimeError,
}
}
EHeader::PlatformName(_, pos) => {
let surroundings = Region::new(start, *pos);
let region = LineColumnRegion::from_pos(lines.convert_pos(*pos));
let doc = alloc.stack([
alloc.reflow(r"I am partway through parsing a header, but got stuck here:"),
alloc
.reflow(r"I am partway through parsing a platform header, but got stuck here:"),
alloc.region_with_subregion(lines.convert_region(surroundings), region),
alloc.concat([
alloc.reflow("I am expecting a platform name next, like "),
@ -3405,7 +3509,7 @@ fn to_header_report<'a>(
Report {
filename,
doc,
title: "WEIRD MODULE NAME".to_string(),
title: "INVALID PLATFORM NAME".to_string(),
severity: Severity::RuntimeError,
}
}

View File

@ -5354,6 +5354,51 @@ Tab characters are not allowed."###,
"###
);
test_report!(
dbg_without_final_expression,
indoc!(
r#"
dbg 42
"#
),
@r###"
INDENT ENDS AFTER EXPRESSION tmp/dbg_without_final_expression/Test.roc
I am partway through parsing a dbg statement, but I got stuck here:
4 dbg 42
^
I was expecting a final expression, like so
dbg 42
"done"
"###
);
test_report!(
expect_without_final_expression,
indoc!(
r#"
expect 1 + 1 == 2
"#
),
@r###"
INDENT ENDS AFTER EXPRESSION tmp/expect_without_final_expression/Test.roc
I am partway through parsing an expect statement, but I got stuck
here:
4 expect 1 + 1 == 2
^
I was expecting a final expression, like so
expect 1 + 1 == 2
"done"
"###
);
// https://github.com/roc-lang/roc/issues/1714
test_report!(
interpolate_concat_is_transparent_1714,

View File

@ -12,7 +12,7 @@ path = "src/main.rs"
[dependencies]
roc_wasm_module = { path = "../wasm_module" }
rand = "0.8.4"
bitvec.workspace = true
bumpalo.workspace = true
clap.workspace = true

View File

@ -1,344 +0,0 @@
use bumpalo::{collections::Vec, Bump};
use roc_wasm_module::opcodes::OpCode;
use roc_wasm_module::sections::ImportDesc;
use roc_wasm_module::{parse::Parse, Value, ValueType, WasmModule};
use std::fmt::{self, Write};
use std::iter::repeat;
use crate::{pc_to_fn_index, Error, ValueStack};
/// Struct-of-Arrays storage for the call stack.
/// Type info is packed to avoid wasting space on padding.
/// However we store 64 bits for every local, even 32-bit values, for easy random access.
#[derive(Debug)]
pub struct CallStack<'a> {
/// return addresses and nested block depths (one entry per frame)
return_addrs_and_block_depths: Vec<'a, (u32, u32)>,
/// frame offsets into the `locals`, `is_float`, and `is_64` vectors (one entry per frame)
frame_offsets: Vec<'a, u32>,
/// base size of the value stack before executing (one entry per frame)
value_stack_bases: Vec<'a, u32>,
/// local variables (one entry per local)
locals: Vec<'a, Value>,
}
impl<'a> CallStack<'a> {
pub fn new(arena: &'a Bump) -> Self {
CallStack {
return_addrs_and_block_depths: Vec::with_capacity_in(256, arena),
frame_offsets: Vec::with_capacity_in(256, arena),
value_stack_bases: Vec::with_capacity_in(256, arena),
locals: Vec::with_capacity_in(16 * 256, arena),
}
}
/// On entering a Wasm call, save the return address, and make space for locals
pub(crate) fn push_frame(
&mut self,
return_addr: u32,
return_block_depth: u32,
arg_type_bytes: &[u8],
value_stack: &mut ValueStack<'a>,
code_bytes: &[u8],
pc: &mut usize,
) -> Result<(), crate::Error> {
self.return_addrs_and_block_depths
.push((return_addr, return_block_depth));
let frame_offset = self.locals.len();
self.frame_offsets.push(frame_offset as u32);
// Make space for arguments
let n_args = arg_type_bytes.len();
self.locals.extend(repeat(Value::I64(0)).take(n_args));
// Pop arguments off the value stack and into locals
for (i, type_byte) in arg_type_bytes.iter().copied().enumerate().rev() {
let arg = value_stack.pop();
let ty = ValueType::from(arg);
let expected_type = ValueType::from(type_byte);
if ty != expected_type {
return Err(Error::ValueStackType(expected_type, ty));
}
self.set_local_help(i as u32, arg);
}
self.value_stack_bases.push(value_stack.depth() as u32);
// Parse local variable declarations in the function header. They're grouped by type.
let local_group_count = u32::parse((), code_bytes, pc).unwrap();
for _ in 0..local_group_count {
let (group_size, ty) = <(u32, ValueType)>::parse((), code_bytes, pc).unwrap();
let n = group_size as usize;
let zero = match ty {
ValueType::I32 => Value::I32(0),
ValueType::I64 => Value::I64(0),
ValueType::F32 => Value::F32(0.0),
ValueType::F64 => Value::F64(0.0),
};
self.locals.extend(repeat(zero).take(n));
}
Ok(())
}
/// On returning from a Wasm call, drop its locals and retrieve the return address
pub fn pop_frame(&mut self) -> Option<(u32, u32)> {
let frame_offset = self.frame_offsets.pop()? as usize;
self.value_stack_bases.pop()?;
self.locals.truncate(frame_offset);
self.return_addrs_and_block_depths.pop()
}
pub fn get_local(&self, local_index: u32) -> Value {
self.get_local_help(self.frame_offsets.len() - 1, local_index)
}
fn get_local_help(&self, frame_index: usize, local_index: u32) -> Value {
let frame_offset = self.frame_offsets[frame_index];
let index = (frame_offset + local_index) as usize;
self.locals[index]
}
pub(crate) fn set_local(&mut self, local_index: u32, value: Value) -> Result<(), Error> {
let expected_type = self.set_local_help(local_index, value);
let actual_type = ValueType::from(value);
if actual_type == expected_type {
Ok(())
} else {
Err(Error::ValueStackType(expected_type, actual_type))
}
}
fn set_local_help(&mut self, local_index: u32, value: Value) -> ValueType {
let frame_offset = *self.frame_offsets.last().unwrap();
let index = (frame_offset + local_index) as usize;
let old_value = self.locals[index];
self.locals[index] = value;
ValueType::from(old_value)
}
pub fn value_stack_base(&self) -> u32 {
*self.value_stack_bases.last().unwrap_or(&0)
}
pub fn is_empty(&self) -> bool {
self.frame_offsets.is_empty()
}
/// Dump a stack trace of the WebAssembly program
///
/// --------------
/// function 123
/// address 0x12345
/// args 0: I64(234), 1: F64(7.15)
/// locals 2: I32(412), 3: F64(3.14)
/// stack [I64(111), F64(3.14)]
/// --------------
pub fn dump_trace(
&self,
module: &WasmModule<'a>,
value_stack: &ValueStack<'a>,
pc: usize,
buffer: &mut String,
) -> fmt::Result {
let divider = "-------------------";
writeln!(buffer, "{}", divider)?;
let mut value_stack_iter = value_stack.iter();
for frame in 0..self.frame_offsets.len() {
let next_frame = frame + 1;
let op_offset = if next_frame < self.frame_offsets.len() {
// return address of next frame = next op in this frame
let next_op = self.return_addrs_and_block_depths[next_frame].0 as usize;
// Call address is more intuitive than the return address when debugging. Search backward for it.
// Skip last byte of function index to avoid a false match with CALL/CALLINDIRECT.
// The more significant bytes won't match because of LEB-128 encoding.
let mut call_op = next_op - 2;
loop {
let byte = module.code.bytes[call_op];
if byte == OpCode::CALL as u8 || byte == OpCode::CALLINDIRECT as u8 {
break;
} else {
call_op -= 1;
}
}
call_op
} else {
pc
};
let fn_index = pc_to_fn_index(op_offset, module);
let address = op_offset + module.code.section_offset as usize;
writeln!(buffer, "function {}", fn_index)?;
writeln!(buffer, " address {:06x}", address)?; // format matches wasm-objdump, for easy search
write!(buffer, " args ")?;
let arg_count = {
let n_import_fns = module.import.imports.len();
let signature_index = if fn_index < n_import_fns {
match module.import.imports[fn_index].description {
ImportDesc::Func { signature_index } => signature_index,
_ => unreachable!(),
}
} else {
module.function.signatures[fn_index - n_import_fns]
};
module.types.look_up_arg_type_bytes(signature_index).len()
};
let args_and_locals_count = {
let frame_offset = self.frame_offsets[frame] as usize;
let next_frame_offset = if frame == self.frame_offsets.len() - 1 {
self.locals.len()
} else {
self.frame_offsets[frame + 1] as usize
};
next_frame_offset - frame_offset
};
for index in 0..args_and_locals_count {
let value = self.get_local_help(frame, index as u32);
if index != 0 {
write!(buffer, ", ")?;
}
if index == arg_count {
write!(buffer, "\n locals ")?;
}
write!(buffer, "{}: {:?}", index, value)?;
}
write!(buffer, "\n stack [")?;
let frame_value_count = {
let value_stack_base = self.value_stack_bases[frame];
let next_value_stack_base = if frame == self.frame_offsets.len() - 1 {
value_stack.depth() as u32
} else {
self.value_stack_bases[frame + 1]
};
next_value_stack_base - value_stack_base
};
for i in 0..frame_value_count {
if i != 0 {
write!(buffer, ", ")?;
}
if let Some(value) = value_stack_iter.next() {
write!(buffer, "{:?}", value)?;
}
}
writeln!(buffer, "]")?;
writeln!(buffer, "{}", divider)?;
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use roc_wasm_module::Serialize;
use super::*;
const RETURN_ADDR: u32 = 0x12345;
fn test_get_set(call_stack: &mut CallStack<'_>, index: u32, value: Value) {
call_stack.set_local(index, value).unwrap();
assert_eq!(call_stack.get_local(index), value);
}
fn setup<'a>(arena: &'a Bump, call_stack: &mut CallStack<'a>) {
let mut buffer = vec![];
let mut cursor = 0;
let mut vs = ValueStack::new(arena);
// Push a other few frames before the test frame, just to make the scenario more typical.
[(1u32, ValueType::I32)].serialize(&mut buffer);
call_stack
.push_frame(0x11111, 0, &[], &mut vs, &buffer, &mut cursor)
.unwrap();
[(2u32, ValueType::I32)].serialize(&mut buffer);
call_stack
.push_frame(0x22222, 0, &[], &mut vs, &buffer, &mut cursor)
.unwrap();
[(3u32, ValueType::I32)].serialize(&mut buffer);
call_stack
.push_frame(0x33333, 0, &[], &mut vs, &buffer, &mut cursor)
.unwrap();
// Create a test call frame with local variables of every type
[
(8u32, ValueType::I32),
(4u32, ValueType::I64),
(2u32, ValueType::F32),
(1u32, ValueType::F64),
]
.serialize(&mut buffer);
call_stack
.push_frame(RETURN_ADDR, 0, &[], &mut vs, &buffer, &mut cursor)
.unwrap();
}
#[test]
fn test_all() {
let arena = Bump::new();
let mut call_stack = CallStack::new(&arena);
setup(&arena, &mut call_stack);
test_get_set(&mut call_stack, 0, Value::I32(123));
test_get_set(&mut call_stack, 8, Value::I64(123456));
test_get_set(&mut call_stack, 12, Value::F32(1.01));
test_get_set(&mut call_stack, 14, Value::F64(-1.1));
test_get_set(&mut call_stack, 0, Value::I32(i32::MIN));
test_get_set(&mut call_stack, 0, Value::I32(i32::MAX));
test_get_set(&mut call_stack, 8, Value::I64(i64::MIN));
test_get_set(&mut call_stack, 8, Value::I64(i64::MAX));
test_get_set(&mut call_stack, 12, Value::F32(f32::MIN));
test_get_set(&mut call_stack, 12, Value::F32(f32::MAX));
test_get_set(&mut call_stack, 14, Value::F64(f64::MIN));
test_get_set(&mut call_stack, 14, Value::F64(f64::MAX));
assert_eq!(call_stack.pop_frame(), Some((RETURN_ADDR, 0)));
}
#[test]
#[should_panic]
fn test_type_error_i32() {
let arena = Bump::new();
let mut call_stack = CallStack::new(&arena);
setup(&arena, &mut call_stack);
test_get_set(&mut call_stack, 0, Value::F32(1.01));
}
#[test]
#[should_panic]
fn test_type_error_i64() {
let arena = Bump::new();
let mut call_stack = CallStack::new(&arena);
setup(&arena, &mut call_stack);
test_get_set(&mut call_stack, 8, Value::F32(1.01));
}
#[test]
#[should_panic]
fn test_type_error_f32() {
let arena = Bump::new();
let mut call_stack = CallStack::new(&arena);
setup(&arena, &mut call_stack);
test_get_set(&mut call_stack, 12, Value::I32(123));
}
#[test]
#[should_panic]
fn test_type_error_f64() {
let arena = Bump::new();
let mut call_stack = CallStack::new(&arena);
setup(&arena, &mut call_stack);
test_get_set(&mut call_stack, 14, Value::I32(123));
}
}

View File

@ -0,0 +1,82 @@
use roc_wasm_module::{parse::Parse, Value, ValueType};
use std::iter::repeat;
use crate::value_store::ValueStore;
#[derive(Debug)]
pub struct Frame {
/// The function this frame belongs to
pub fn_index: usize,
/// Address in the code section where this frame returns to
pub return_addr: usize,
/// Depth of the "function body block" for this frame
pub body_block_index: usize,
/// Offset in the ValueStore where the args & locals begin
pub locals_start: usize,
/// Number of args & locals in the frame
pub locals_count: usize,
/// Expected return type, if any
pub return_type: Option<ValueType>,
}
impl Frame {
pub fn new() -> Self {
Frame {
fn_index: 0,
return_addr: 0,
body_block_index: 0,
locals_start: 0,
locals_count: 0,
return_type: None,
}
}
#[allow(clippy::too_many_arguments)]
pub fn enter(
fn_index: usize,
return_addr: usize,
body_block_index: usize,
n_args: usize,
return_type: Option<ValueType>,
code_bytes: &[u8],
value_store: &mut ValueStore<'_>,
pc: &mut usize,
) -> Self {
let locals_start = value_store.depth() - n_args;
// Parse local variable declarations in the function header. They're grouped by type.
let local_group_count = u32::parse((), code_bytes, pc).unwrap();
for _ in 0..local_group_count {
let (group_size, ty) = <(u32, ValueType)>::parse((), code_bytes, pc).unwrap();
let n = group_size as usize;
let zero = match ty {
ValueType::I32 => Value::I32(0),
ValueType::I64 => Value::I64(0),
ValueType::F32 => Value::F32(0.0),
ValueType::F64 => Value::F64(0.0),
};
value_store.extend(repeat(zero).take(n));
}
let locals_count = value_store.depth() - locals_start;
Frame {
fn_index,
return_addr,
body_block_index,
locals_start,
locals_count,
return_type,
}
}
pub fn get_local(&self, values: &ValueStore<'_>, index: u32) -> Value {
debug_assert!((index as usize) < self.locals_count);
*values.get(self.locals_start + index as usize).unwrap()
}
pub fn set_local(&self, values: &mut ValueStore<'_>, index: u32, value: Value) {
debug_assert!((index as usize) < self.locals_count);
values.set(self.locals_start + index as usize, value)
}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,15 +1,15 @@
mod call_stack;
mod frame;
mod instance;
mod tests;
mod value_stack;
mod value_store;
pub mod wasi;
// Main external interface
pub use instance::Instance;
pub use wasi::WasiDispatcher;
pub use wasi::{WasiDispatcher, WasiFile};
use roc_wasm_module::{Value, ValueType, WasmModule};
use value_stack::ValueStack;
pub use roc_wasm_module::Value;
use roc_wasm_module::ValueType;
pub trait ImportDispatcher {
/// Dispatch a call from WebAssembly to your own code, based on module and function name.
@ -22,18 +22,22 @@ pub trait ImportDispatcher {
) -> Option<Value>;
}
pub const DEFAULT_IMPORTS: DefaultImportDispatcher = DefaultImportDispatcher {
wasi: WasiDispatcher { args: &[] },
};
impl Default for DefaultImportDispatcher<'_> {
fn default() -> Self {
DefaultImportDispatcher {
wasi: WasiDispatcher::new(&[]),
}
}
}
pub struct DefaultImportDispatcher<'a> {
wasi: WasiDispatcher<'a>,
pub wasi: WasiDispatcher<'a>,
}
impl<'a> DefaultImportDispatcher<'a> {
pub fn new(args: &'a [&'a String]) -> Self {
pub fn new(args: &'a [&'a [u8]]) -> Self {
DefaultImportDispatcher {
wasi: WasiDispatcher { args },
wasi: WasiDispatcher::new(args),
}
}
}
@ -61,23 +65,23 @@ impl<'a> ImportDispatcher for DefaultImportDispatcher<'a> {
/// All of these cause a WebAssembly stack trace to be dumped
#[derive(Debug, PartialEq)]
pub(crate) enum Error {
ValueStackType(ValueType, ValueType),
ValueStackEmpty,
Type(ValueType, ValueType),
StackEmpty,
UnreachableOp,
}
impl Error {
pub fn to_string_at(&self, file_offset: usize) -> String {
match self {
Error::ValueStackType(expected, actual) => {
Error::Type(expected, actual) => {
format!(
"ERROR: I found a type mismatch in the Value Stack at file offset {:#x}. Expected {:?}, but found {:?}.\n",
"ERROR: I found a type mismatch at file offset {:#x}. Expected {:?}, but found {:?}.\n",
file_offset, expected, actual
)
}
Error::ValueStackEmpty => {
Error::StackEmpty => {
format!(
"ERROR: I tried to pop a value from the Value Stack at file offset {:#x}, but it was empty.\n",
"ERROR: I tried to pop a value from the stack at file offset {:#x}, but it was empty.\n",
file_offset
)
}
@ -93,25 +97,6 @@ impl Error {
impl From<(ValueType, ValueType)> for Error {
fn from((expected, actual): (ValueType, ValueType)) -> Self {
Error::ValueStackType(expected, actual)
}
}
// Determine which function the program counter is in
pub(crate) fn pc_to_fn_index(program_counter: usize, module: &WasmModule<'_>) -> usize {
if module.code.function_offsets.is_empty() {
0
} else {
// Find the first function that starts *after* the given program counter
let next_internal_fn_index = module
.code
.function_offsets
.iter()
.position(|o| *o as usize > program_counter)
.unwrap_or(module.code.function_offsets.len());
// Go back 1
let internal_fn_index = next_internal_fn_index - 1;
// Adjust for imports, whose indices come before the code section
module.import.imports.len() + internal_fn_index
Error::Type(expected, actual)
}
}

View File

@ -65,7 +65,10 @@ fn main() -> io::Result<()> {
let start_arg_strings = matches.get_many::<String>(ARGS_FOR_APP).unwrap_or_default();
let wasm_path = matches.get_one::<String>(WASM_FILE).unwrap();
// WASI expects the .wasm file to be argv[0]
let wasi_argv = Vec::from_iter_in(once(wasm_path).chain(start_arg_strings), &arena);
let wasi_argv_iter = once(wasm_path)
.chain(start_arg_strings)
.map(|s| s.as_bytes());
let wasi_argv = Vec::from_iter_in(wasi_argv_iter, &arena);
// Load the WebAssembly binary file

View File

@ -8,17 +8,24 @@ mod test_i32;
mod test_i64;
mod test_mem;
use crate::{DefaultImportDispatcher, Instance, DEFAULT_IMPORTS};
use crate::{DefaultImportDispatcher, Instance};
use bumpalo::{collections::Vec, Bump};
use roc_wasm_module::{
opcodes::OpCode, Export, ExportType, SerialBuffer, Signature, Value, ValueType, WasmModule,
opcodes::OpCode, Export, ExportType, SerialBuffer, Serialize, Signature, Value, ValueType,
WasmModule,
};
pub fn default_state(arena: &Bump) -> Instance<DefaultImportDispatcher> {
let pages = 1;
let program_counter = 0;
let globals = [];
Instance::new(arena, pages, program_counter, globals, DEFAULT_IMPORTS)
Instance::new(
arena,
pages,
program_counter,
globals,
DefaultImportDispatcher::default(),
)
}
pub fn const_value(buf: &mut Vec<'_, u8>, value: Value) {
@ -85,9 +92,10 @@ where
std::fs::write(&filename, outfile_buf).unwrap();
}
let mut inst = Instance::for_module(&arena, &module, DEFAULT_IMPORTS, true).unwrap();
let mut inst =
Instance::for_module(&arena, &module, DefaultImportDispatcher::default(), true).unwrap();
let return_val = inst.call_export(&module, "test", []).unwrap().unwrap();
let return_val = inst.call_export("test", []).unwrap().unwrap();
assert_eq!(return_val, expected);
}
@ -119,3 +127,32 @@ pub fn create_exported_function_no_locals<'a, F>(
module.code.function_count += 1;
module.code.function_offsets.push(offset as u32);
}
pub fn create_exported_function_with_locals<'a, F>(
module: &mut WasmModule<'a>,
name: &'a str,
signature: Signature<'a>,
local_types: &[(u32, ValueType)],
write_instructions: F,
) where
F: FnOnce(&mut Vec<'a, u8>),
{
let internal_fn_index = module.code.function_offsets.len();
let fn_index = module.import.function_count() + internal_fn_index;
module.export.exports.push(Export {
name,
ty: ExportType::Func,
index: fn_index as u32,
});
module.add_function_signature(signature);
let offset = module.code.bytes.encode_padded_u32(0);
let start = module.code.bytes.len();
local_types.serialize(&mut module.code.bytes);
write_instructions(&mut module.code.bytes);
let len = module.code.bytes.len() - start;
module.code.bytes.overwrite_padded_u32(offset, len as u32);
module.code.function_count += 1;
module.code.function_offsets.push(offset as u32);
}

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,5 @@
use super::create_exported_function_no_locals;
use crate::{Instance, DEFAULT_IMPORTS};
use crate::{DefaultImportDispatcher, Instance};
use bumpalo::{collections::Vec, Bump};
use roc_wasm_module::{
opcodes::OpCode,
@ -18,9 +18,9 @@ fn test_currentmemory() {
module.code.bytes.push(OpCode::CURRENTMEMORY as u8);
module.code.bytes.encode_i32(0);
let mut state = Instance::new(&arena, pages, pc, [], DEFAULT_IMPORTS);
let mut state = Instance::new(&arena, pages, pc, [], DefaultImportDispatcher::default());
state.execute_next_instruction(&module).unwrap();
assert_eq!(state.value_stack.pop(), Value::I32(3))
assert_eq!(state.value_store.pop(), Value::I32(3))
}
#[test]
@ -37,7 +37,13 @@ fn test_growmemory() {
module.code.bytes.push(OpCode::GROWMEMORY as u8);
module.code.bytes.encode_i32(0);
let mut state = Instance::new(&arena, existing_pages, pc, [], DEFAULT_IMPORTS);
let mut state = Instance::new(
&arena,
existing_pages,
pc,
[],
DefaultImportDispatcher::default(),
);
state.execute_next_instruction(&module).unwrap();
state.execute_next_instruction(&module).unwrap();
assert_eq!(state.memory.len(), 5 * MemorySection::PAGE_SIZE as usize);
@ -79,10 +85,14 @@ fn test_load(load_op: OpCode, ty: ValueType, data: &[u8], addr: u32, offset: u32
std::fs::write("/tmp/roc/interp_load_test.wasm", outfile_buf).unwrap();
}
let mut inst = Instance::for_module(&arena, &module, DEFAULT_IMPORTS, is_debug_mode).unwrap();
inst.call_export(&module, start_fn_name, [])
.unwrap()
.unwrap()
let mut inst = Instance::for_module(
&arena,
&module,
DefaultImportDispatcher::default(),
is_debug_mode,
)
.unwrap();
inst.call_export(start_fn_name, []).unwrap().unwrap()
}
#[test]
@ -233,13 +243,12 @@ fn test_i64load32u() {
fn test_store<'a>(
arena: &'a Bump,
module: &mut WasmModule<'a>,
module: &'a mut WasmModule<'a>,
addr: u32,
store_op: OpCode,
offset: u32,
value: Value,
) -> Vec<'a, u8> {
let is_debug_mode = false;
let start_fn_name = "test";
module.memory = MemorySection::new(arena, MemorySection::PAGE_SIZE);
@ -276,8 +285,15 @@ fn test_store<'a>(
buf.append_u8(OpCode::END as u8);
});
let mut inst = Instance::for_module(arena, module, DEFAULT_IMPORTS, is_debug_mode).unwrap();
inst.call_export(module, start_fn_name, []).unwrap();
let is_debug_mode = false;
let mut inst = Instance::for_module(
arena,
module,
DefaultImportDispatcher::default(),
is_debug_mode,
)
.unwrap();
inst.call_export(start_fn_name, []).unwrap();
inst.memory
}
@ -285,13 +301,13 @@ fn test_store<'a>(
#[test]
fn test_i32store() {
let arena = Bump::new();
let mut module = WasmModule::new(&arena);
let module = arena.alloc(WasmModule::new(&arena));
let addr: u32 = 0x11;
let store_op = OpCode::I32STORE;
let offset = 1;
let value = Value::I32(0x12345678);
let memory = test_store(&arena, &mut module, addr, store_op, offset, value);
let memory = test_store(&arena, module, addr, store_op, offset, value);
let index = (addr + offset) as usize;
assert_eq!(&memory[index..][..4], &[0x78, 0x56, 0x34, 0x12]);
@ -300,13 +316,13 @@ fn test_i32store() {
#[test]
fn test_i64store() {
let arena = Bump::new();
let mut module = WasmModule::new(&arena);
let module = arena.alloc(WasmModule::new(&arena));
let addr: u32 = 0x11;
let store_op = OpCode::I64STORE;
let offset = 1;
let value = Value::I64(0x123456789abcdef0);
let memory = test_store(&arena, &mut module, addr, store_op, offset, value);
let memory = test_store(&arena, module, addr, store_op, offset, value);
let index = (addr + offset) as usize;
assert_eq!(
@ -318,14 +334,14 @@ fn test_i64store() {
#[test]
fn test_f32store() {
let arena = Bump::new();
let mut module = WasmModule::new(&arena);
let module = arena.alloc(WasmModule::new(&arena));
let addr: u32 = 0x11;
let store_op = OpCode::F32STORE;
let offset = 1;
let inner: f32 = 1.23456;
let value = Value::F32(inner);
let memory = test_store(&arena, &mut module, addr, store_op, offset, value);
let memory = test_store(&arena, module, addr, store_op, offset, value);
let index = (addr + offset) as usize;
assert_eq!(&memory[index..][..4], &inner.to_le_bytes());
@ -334,14 +350,14 @@ fn test_f32store() {
#[test]
fn test_f64store() {
let arena = Bump::new();
let mut module = WasmModule::new(&arena);
let module = arena.alloc(WasmModule::new(&arena));
let addr: u32 = 0x11;
let store_op = OpCode::F64STORE;
let offset = 1;
let inner: f64 = 1.23456;
let value = Value::F64(inner);
let memory = test_store(&arena, &mut module, addr, store_op, offset, value);
let memory = test_store(&arena, module, addr, store_op, offset, value);
let index = (addr + offset) as usize;
assert_eq!(&memory[index..][..8], &inner.to_le_bytes());
@ -350,13 +366,13 @@ fn test_f64store() {
#[test]
fn test_i32store8() {
let arena = Bump::new();
let mut module = WasmModule::new(&arena);
let module = arena.alloc(WasmModule::new(&arena));
let addr: u32 = 0x11;
let store_op = OpCode::I32STORE8;
let offset = 1;
let value = Value::I32(0x12345678);
let memory = test_store(&arena, &mut module, addr, store_op, offset, value);
let memory = test_store(&arena, module, addr, store_op, offset, value);
let index = (addr + offset) as usize;
assert_eq!(&memory[index..][..4], &[0x78, 0x00, 0x00, 0x00]);
@ -365,13 +381,13 @@ fn test_i32store8() {
#[test]
fn test_i32store16() {
let arena = Bump::new();
let mut module = WasmModule::new(&arena);
let module = arena.alloc(WasmModule::new(&arena));
let addr: u32 = 0x11;
let store_op = OpCode::I32STORE16;
let offset = 1;
let value = Value::I32(0x12345678);
let memory = test_store(&arena, &mut module, addr, store_op, offset, value);
let memory = test_store(&arena, module, addr, store_op, offset, value);
let index = (addr + offset) as usize;
assert_eq!(&memory[index..][..4], &[0x78, 0x56, 0x00, 0x00]);
@ -380,13 +396,13 @@ fn test_i32store16() {
#[test]
fn test_i64store8() {
let arena = Bump::new();
let mut module = WasmModule::new(&arena);
let module = arena.alloc(WasmModule::new(&arena));
let addr: u32 = 0x11;
let store_op = OpCode::I64STORE8;
let offset = 1;
let value = Value::I64(0x123456789abcdef0);
let memory = test_store(&arena, &mut module, addr, store_op, offset, value);
let memory = test_store(&arena, module, addr, store_op, offset, value);
let index = (addr + offset) as usize;
assert_eq!(
@ -398,13 +414,13 @@ fn test_i64store8() {
#[test]
fn test_i64store16() {
let arena = Bump::new();
let mut module = WasmModule::new(&arena);
let module = arena.alloc(WasmModule::new(&arena));
let addr: u32 = 0x11;
let store_op = OpCode::I64STORE16;
let offset = 1;
let value = Value::I64(0x123456789abcdef0);
let memory = test_store(&arena, &mut module, addr, store_op, offset, value);
let memory = test_store(&arena, module, addr, store_op, offset, value);
let index = (addr + offset) as usize;
assert_eq!(
@ -416,13 +432,13 @@ fn test_i64store16() {
#[test]
fn test_i64store32() {
let arena = Bump::new();
let mut module = WasmModule::new(&arena);
let module = arena.alloc(WasmModule::new(&arena));
let addr: u32 = 0x11;
let store_op = OpCode::I64STORE32;
let offset = 1;
let value = Value::I64(0x123456789abcdef0);
let memory = test_store(&arena, &mut module, addr, store_op, offset, value);
let memory = test_store(&arena, module, addr, store_op, offset, value);
let index = (addr + offset) as usize;
assert_eq!(

View File

@ -4,16 +4,21 @@ use std::fmt::Debug;
use crate::Error;
// Very simple and easy-to-debug storage for the Wasm stack machine
// It wastes a lot of memory but we tried more complex schemes with packed bytes
// and it made no measurable difference to performance.
pub struct ValueStack<'a> {
/// Combined storage for the Wasm stack machine and local variables.
///
/// All values are mixed together so that on function calls, "moving"
/// arguments from the stack machine to local variables is a no-op
/// (or rather, just a matter of recording block metadata in the Instance).
///
/// We use a simple Vec. When we tried more densely-packed SoA structures,
/// they were slower due to more logic, and harder to debug.
pub struct ValueStore<'a> {
values: Vec<'a, Value>,
}
impl<'a> ValueStack<'a> {
impl<'a> ValueStore<'a> {
pub(crate) fn new(arena: &'a Bump) -> Self {
ValueStack {
ValueStore {
values: Vec::with_capacity_in(1024, arena),
}
}
@ -38,52 +43,64 @@ impl<'a> ValueStack<'a> {
*self.values.last().unwrap()
}
pub(crate) fn get(&self, index: usize) -> Option<&Value> {
self.values.get(index)
}
pub(crate) fn set(&mut self, index: usize, value: Value) {
self.values[index] = value;
}
pub(crate) fn extend<I: Iterator<Item = Value>>(&mut self, values: I) {
self.values.extend(values)
}
/// Memory addresses etc
pub(crate) fn pop_u32(&mut self) -> Result<u32, Error> {
match self.values.pop() {
Some(Value::I32(x)) => Ok(u32::from_ne_bytes(x.to_ne_bytes())),
Some(bad) => Err(Error::ValueStackType(ValueType::I32, ValueType::from(bad))),
None => Err(Error::ValueStackEmpty),
Some(bad) => Err(Error::Type(ValueType::I32, ValueType::from(bad))),
None => Err(Error::StackEmpty),
}
}
pub(crate) fn pop_i32(&mut self) -> Result<i32, Error> {
match self.values.pop() {
Some(Value::I32(x)) => Ok(x),
Some(bad) => Err(Error::ValueStackType(ValueType::I32, ValueType::from(bad))),
None => Err(Error::ValueStackEmpty),
Some(bad) => Err(Error::Type(ValueType::I32, ValueType::from(bad))),
None => Err(Error::StackEmpty),
}
}
pub(crate) fn pop_u64(&mut self) -> Result<u64, Error> {
match self.values.pop() {
Some(Value::I64(x)) => Ok(u64::from_ne_bytes(x.to_ne_bytes())),
Some(bad) => Err(Error::ValueStackType(ValueType::I64, ValueType::from(bad))),
None => Err(Error::ValueStackEmpty),
Some(bad) => Err(Error::Type(ValueType::I64, ValueType::from(bad))),
None => Err(Error::StackEmpty),
}
}
pub(crate) fn pop_i64(&mut self) -> Result<i64, Error> {
match self.values.pop() {
Some(Value::I64(x)) => Ok(x),
Some(bad) => Err(Error::ValueStackType(ValueType::I64, ValueType::from(bad))),
None => Err(Error::ValueStackEmpty),
Some(bad) => Err(Error::Type(ValueType::I64, ValueType::from(bad))),
None => Err(Error::StackEmpty),
}
}
pub(crate) fn pop_f32(&mut self) -> Result<f32, Error> {
match self.values.pop() {
Some(Value::F32(x)) => Ok(x),
Some(bad) => Err(Error::ValueStackType(ValueType::F32, ValueType::from(bad))),
None => Err(Error::ValueStackEmpty),
Some(bad) => Err(Error::Type(ValueType::F32, ValueType::from(bad))),
None => Err(Error::StackEmpty),
}
}
pub(crate) fn pop_f64(&mut self) -> Result<f64, Error> {
match self.values.pop() {
Some(Value::F64(x)) => Ok(x),
Some(bad) => Err(Error::ValueStackType(ValueType::F64, ValueType::from(bad))),
None => Err(Error::ValueStackEmpty),
Some(bad) => Err(Error::Type(ValueType::F64, ValueType::from(bad))),
None => Err(Error::StackEmpty),
}
}
@ -100,7 +117,7 @@ impl<'a> ValueStack<'a> {
}
}
impl Debug for ValueStack<'_> {
impl Debug for ValueStore<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", &self.values)
}
@ -120,7 +137,7 @@ mod tests {
#[test]
fn test_push_pop() {
let arena = Bump::new();
let mut stack = ValueStack::new(&arena);
let mut stack = ValueStore::new(&arena);
for val in VALUES {
stack.push(val);
@ -135,7 +152,7 @@ mod tests {
#[test]
fn test_debug_fmt() {
let arena = Bump::new();
let mut stack = ValueStack::new(&arena);
let mut stack = ValueStore::new(&arena);
for val in VALUES {
stack.push(val);

View File

@ -1,11 +1,33 @@
use rand::prelude::*;
use roc_wasm_module::Value;
use std::io::{self, Write};
use std::io::{self, Read, StderrLock, StdoutLock, Write};
use std::process::exit;
pub const MODULE_NAME: &str = "wasi_snapshot_preview1";
pub struct WasiDispatcher<'a> {
pub args: &'a [&'a String],
pub args: &'a [&'a [u8]],
pub rng: ThreadRng,
pub files: Vec<WasiFile>,
}
impl Default for WasiDispatcher<'_> {
fn default() -> Self {
WasiDispatcher::new(&[])
}
}
pub enum WasiFile {
ReadOnly(Vec<u8>),
WriteOnly(Vec<u8>),
ReadWrite(Vec<u8>),
HostSystemFile,
}
enum WriteLock<'a> {
StdOut(StdoutLock<'a>),
Stderr(StderrLock<'a>),
RegularFile(&'a mut Vec<u8>),
}
/// Implementation of WASI syscalls
@ -13,8 +35,16 @@ pub struct WasiDispatcher<'a> {
/// https://github.com/wasmerio/wasmer/blob/ef8d2f651ed29b4b06fdc2070eb8189922c54d82/lib/wasi/src/syscalls/mod.rs
/// https://github.com/wasm3/wasm3/blob/045040a97345e636b8be4f3086e6db59cdcc785f/source/extra/wasi_core.h
impl<'a> WasiDispatcher<'a> {
pub fn new(args: &'a [&'a String]) -> Self {
WasiDispatcher { args }
pub fn new(args: &'a [&'a [u8]]) -> Self {
WasiDispatcher {
args,
rng: thread_rng(),
files: vec![
WasiFile::HostSystemFile,
WasiFile::HostSystemFile,
WasiFile::HostSystemFile,
],
}
}
pub fn dispatch(
@ -35,7 +65,7 @@ impl<'a> WasiDispatcher<'a> {
for arg in self.args {
write_u32(memory, ptr_ptr_argv, ptr_argv_buf as u32);
let bytes_target = &mut memory[ptr_argv_buf..][..arg.len()];
bytes_target.copy_from_slice(arg.as_bytes());
bytes_target.copy_from_slice(arg);
memory[ptr_argv_buf + arg.len()] = 0; // C string zero termination
ptr_argv_buf += arg.len() + 1;
ptr_ptr_argv += 4;
@ -61,8 +91,8 @@ impl<'a> WasiDispatcher<'a> {
}
"environ_get" => todo!("WASI {}({:?})", function_name, arguments),
"environ_sizes_get" => todo!("WASI {}({:?})", function_name, arguments),
"clock_res_get" => success_code, // this dummy implementation seems to be good enough
"clock_time_get" => success_code, // this dummy implementation seems to be good enough
"clock_res_get" => success_code, // this dummy implementation seems to be good enough for some functions
"clock_time_get" => success_code,
"fd_advise" => todo!("WASI {}({:?})", function_name, arguments),
"fd_allocate" => todo!("WASI {}({:?})", function_name, arguments),
"fd_close" => todo!("WASI {}({:?})", function_name, arguments),
@ -74,18 +104,90 @@ impl<'a> WasiDispatcher<'a> {
"fd_filestat_set_size" => todo!("WASI {}({:?})", function_name, arguments),
"fd_filestat_set_times" => todo!("WASI {}({:?})", function_name, arguments),
"fd_pread" => todo!("WASI {}({:?})", function_name, arguments),
"fd_prestat_get" => todo!("WASI {}({:?})", function_name, arguments),
"fd_prestat_dir_name" => todo!("WASI {}({:?})", function_name, arguments),
"fd_prestat_get" => {
// The preopened file descriptor to query
let fd = arguments[0].expect_i32().unwrap() as usize;
// ptr_buf: Where the metadata will be written
// preopen type: 4 bytes, where 0=dir is the only one supported, it seems
// preopen name length: 4 bytes
let ptr_buf = arguments[1].expect_i32().unwrap() as usize;
memory[ptr_buf..][..8].copy_from_slice(&0u64.to_le_bytes());
if fd < self.files.len() {
success_code
} else {
println!("WASI warning: file descriptor {} does not exist", fd);
Some(Value::I32(Errno::Badf as i32))
}
}
"fd_prestat_dir_name" => {
// We're not giving names to any of our files so just return success
success_code
}
"fd_pwrite" => todo!("WASI {}({:?})", function_name, arguments),
"fd_read" => todo!("WASI {}({:?})", function_name, arguments),
"fd_read" => {
use WasiFile::*;
// file descriptor
let fd = arguments[0].expect_i32().unwrap() as usize;
// Array of IO vectors
let ptr_iovs = arguments[1].expect_i32().unwrap() as usize;
// Length of array
let iovs_len = arguments[2].expect_i32().unwrap();
// Out param: number of bytes read
let ptr_nread = arguments[3].expect_i32().unwrap() as usize;
// https://man7.org/linux/man-pages/man2/readv.2.html
// struct iovec {
// void *iov_base; /* Starting address */
// size_t iov_len; /* Number of bytes to transfer */
// };
let mut n_read: usize = 0;
match self.files.get(fd) {
Some(ReadOnly(content) | ReadWrite(content)) => {
for _ in 0..iovs_len {
let iov_base = read_u32(memory, ptr_iovs) as usize;
let iov_len = read_i32(memory, ptr_iovs + 4) as usize;
let remaining = content.len() - n_read;
let len = remaining.min(iov_len);
if len == 0 {
break;
}
memory[iov_base..][..len].copy_from_slice(&content[n_read..][..len]);
n_read += len;
}
}
Some(HostSystemFile) if fd == 0 => {
let mut stdin = io::stdin();
for _ in 0..iovs_len {
let iov_base = read_u32(memory, ptr_iovs) as usize;
let iov_len = read_i32(memory, ptr_iovs + 4) as usize;
match stdin.read(&mut memory[iov_base..][..iov_len]) {
Ok(n) => {
n_read += n;
}
Err(_) => {
break;
}
}
}
}
_ => return Some(Value::I32(Errno::Badf as i32)),
};
memory[ptr_nread..][..4].copy_from_slice(&(n_read as u32).to_le_bytes());
success_code
}
"fd_readdir" => todo!("WASI {}({:?})", function_name, arguments),
"fd_renumber" => todo!("WASI {}({:?})", function_name, arguments),
"fd_seek" => todo!("WASI {}({:?})", function_name, arguments),
"fd_sync" => todo!("WASI {}({:?})", function_name, arguments),
"fd_tell" => todo!("WASI {}({:?})", function_name, arguments),
"fd_write" => {
use WasiFile::*;
// file descriptor
let fd = arguments[0].expect_i32().unwrap();
let fd = arguments[0].expect_i32().unwrap() as usize;
// Array of IO vectors
let ptr_iovs = arguments[1].expect_i32().unwrap() as usize;
// Length of array
@ -93,10 +195,18 @@ impl<'a> WasiDispatcher<'a> {
// Out param: number of bytes written
let ptr_nwritten = arguments[3].expect_i32().unwrap() as usize;
let mut write_lock = match fd {
1 => Ok(io::stdout().lock()),
2 => Err(io::stderr().lock()),
_ => return Some(Value::I32(Errno::Inval as i32)),
// Grab a lock for stdout/stderr before the loop rather than re-acquiring over and over.
// Not really necessary for other files, but it's easier to use the same structure.
let mut write_lock = match self.files.get_mut(fd) {
Some(HostSystemFile) => match fd {
1 => WriteLock::StdOut(io::stdout().lock()),
2 => WriteLock::Stderr(io::stderr().lock()),
_ => return Some(Value::I32(Errno::Inval as i32)),
},
Some(WriteOnly(content) | ReadWrite(content)) => {
WriteLock::RegularFile(content)
}
_ => return Some(Value::I32(Errno::Badf as i32)),
};
let mut n_written: i32 = 0;
@ -119,12 +229,16 @@ impl<'a> WasiDispatcher<'a> {
let bytes = &memory[iov_base..][..iov_len as usize];
match &mut write_lock {
Ok(stdout) => {
WriteLock::StdOut(stdout) => {
n_written += stdout.write(bytes).unwrap() as i32;
}
Err(stderr) => {
WriteLock::Stderr(stderr) => {
n_written += stderr.write(bytes).unwrap() as i32;
}
WriteLock::RegularFile(content) => {
content.extend_from_slice(bytes);
n_written += bytes.len() as i32;
}
}
}
@ -156,7 +270,16 @@ impl<'a> WasiDispatcher<'a> {
}
"proc_raise" => todo!("WASI {}({:?})", function_name, arguments),
"sched_yield" => todo!("WASI {}({:?})", function_name, arguments),
"random_get" => todo!("WASI {}({:?})", function_name, arguments),
"random_get" => {
// A pointer to a buffer where the random bytes will be written
let ptr_buf = arguments[0].expect_i32().unwrap() as usize;
// The number of bytes that will be written
let buf_len = arguments[1].expect_i32().unwrap() as usize;
for i in 0..buf_len {
memory[ptr_buf + i] = self.rng.gen();
}
success_code
}
"sock_recv" => todo!("WASI {}({:?})", function_name, arguments),
"sock_send" => todo!("WASI {}({:?})", function_name, arguments),
"sock_shutdown" => todo!("WASI {}({:?})", function_name, arguments),

View File

@ -212,6 +212,46 @@ impl<'a> Serialize for Signature<'a> {
}
}
#[derive(Debug)]
pub struct SignatureParamsIter<'a> {
bytes: &'a [u8],
index: usize,
end: usize,
}
impl<'a> Iterator for SignatureParamsIter<'a> {
type Item = ValueType;
fn next(&mut self) -> Option<Self::Item> {
if self.index >= self.end {
None
} else {
self.bytes.get(self.index).map(|b| {
self.index += 1;
ValueType::from(*b)
})
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
let size = self.end - self.index;
(size, Some(size))
}
}
impl<'a> ExactSizeIterator for SignatureParamsIter<'a> {}
impl<'a> DoubleEndedIterator for SignatureParamsIter<'a> {
fn next_back(&mut self) -> Option<Self::Item> {
if self.end == 0 {
None
} else {
self.end -= 1;
self.bytes.get(self.end).map(|b| ValueType::from(*b))
}
}
}
#[derive(Debug)]
pub struct TypeSection<'a> {
/// Private. See WasmModule::add_function_signature
@ -258,11 +298,23 @@ impl<'a> TypeSection<'a> {
self.bytes.is_empty()
}
pub fn look_up_arg_type_bytes(&self, sig_index: u32) -> &[u8] {
pub fn look_up(&'a self, sig_index: u32) -> (SignatureParamsIter<'a>, Option<ValueType>) {
let mut offset = self.offsets[sig_index as usize];
offset += 1; // separator
let count = u32::parse((), &self.bytes, &mut offset).unwrap() as usize;
&self.bytes[offset..][..count]
let param_count = u32::parse((), &self.bytes, &mut offset).unwrap() as usize;
let params_iter = SignatureParamsIter {
bytes: &self.bytes[offset..][..param_count],
index: 0,
end: param_count,
};
offset += param_count;
let return_type = if self.bytes[offset] == 0 {
None
} else {
Some(ValueType::from(self.bytes[offset + 1]))
};
(params_iter, return_type)
}
}

View File

@ -52,7 +52,6 @@ rustPlatform.buildRustPackage {
llvmPkgs.clang
llvmPkgs.llvm.dev
zig
rust-bindgen
]);
buildInputs = (with pkgs;

View File

@ -104,7 +104,6 @@
llvmPkgs.lld
debugir
rust
rust-bindgen
cargo-criterion # for benchmarks
simple-http-server # to view roc website when trying out edits
]);

View File

@ -14,10 +14,18 @@ which includes the Roc compiler and various helpful utilities.
cd roc_night<TAB TO AUTOCOMPLETE>
```
1. To be able to run the `roc` command anywhere on your system; add the line below to your shell startup script (.profile, .zshrc, ...):
```sh
export PATH=$PATH:~/path/to/roc_nightly-linux_x86_64-<VERSION>
```
1. Check everything worked by executing `roc version`
## How to install Roc platform dependencies
In order to compile Roc apps (either in `examples/` or in your own projects),
you need to install one or more of these platform language compilers, too.
This step is not necessary if you only want to use the [basic-cli platform](https://github.com/roc-lang/basic-cli), like in the tutorial.
But, if you want to compile Roc apps with other platforms (either in [`examples/`](https://github.com/roc-lang/roc/tree/main/examples) or in your own projects),
you'll need to install one or more of these platform languages too.
1. Install the Rust compiler, for apps with Rust-based platforms:

View File

@ -29,10 +29,18 @@ which includes the Roc compiler and various helpful utilities.
brew install llvm@13
```
1. To be able to run the `roc` command anywhere on your system; add the line below to your shell startup script (.profile, .zshrc, ...):
```sh
export PATH=$PATH:~/path/to/roc_nightly-macos_apple_silicon-<VERSION>
```
1. Check everything worked by executing `roc version`
## How to install Roc platform dependencies
In order to compile Roc apps (either in `examples/` or in your own projects),
you need to install one or more of these platform language compilers, too.
This step is not necessary if you only want to use the [basic-cli platform](https://github.com/roc-lang/basic-cli), like in the tutorial.
But, if you want to compile Roc apps with other platforms (either in [`examples/`](https://github.com/roc-lang/roc/tree/main/examples) or in your own projects),
you'll need to install one or more of these platform languages too.
1. Install the Rust compiler, for apps with Rust-based platforms:

View File

@ -25,10 +25,18 @@ which includes the Roc compiler and various helpful utilities.
cd roc_night<TAB TO AUTOCOMPLETE>
```
1. To be able to run the `roc` command anywhere on your system; add the line below to your shell startup script (.profile, .zshrc, ...):
```sh
export PATH=$PATH:~/path/to/roc_nightly-macos_x86_64-<VERSION>
```
1. Check everything worked by executing `roc version`
## How to install Roc platform dependencies
In order to compile Roc apps (either in `examples/` or in your own projects),
you need to install one or more of these platform language compilers, too.
This step is not necessary if you only want to use the [basic-cli platform](https://github.com/roc-lang/basic-cli), like in the tutorial.
But, if you want to compile Roc apps with other platforms (either in [`examples/`](https://github.com/roc-lang/roc/tree/main/examples) or in your own projects),
you'll need to install one or more of these platform languages too.
1. Install the Rust compiler, for apps with Rust-based platforms:

View File

@ -1,4 +1,4 @@
# Roc installation guide for Windows systems
Windows support is very limited, folkertdev is working on improving this 🚀.
Windows support is limited, folkertdev is working on improving this 🚀.
Until that is done we recommend using Ubuntu through the "Windows Subsystem for Linux".