mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-28 14:24:45 +00:00
Merge branch 'trunk' into single-record-update
This commit is contained in:
commit
001e94a071
18 changed files with 3046 additions and 382 deletions
2
.github/workflows/ci.yml
vendored
2
.github/workflows/ci.yml
vendored
|
@ -11,7 +11,7 @@ env:
|
||||||
jobs:
|
jobs:
|
||||||
build-fmt-clippy-test:
|
build-fmt-clippy-test:
|
||||||
name: fmt, clippy, test --release
|
name: fmt, clippy, test --release
|
||||||
runs-on: [self-hosted]
|
runs-on: [self-hosted, i5-4690K]
|
||||||
timeout-minutes: 90
|
timeout-minutes: 90
|
||||||
env:
|
env:
|
||||||
FORCE_COLOR: 1
|
FORCE_COLOR: 1
|
||||||
|
|
646
Cargo.lock
generated
646
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -34,6 +34,8 @@ install-zig-llvm-valgrind-clippy-rustfmt:
|
||||||
RUN rustup component add rustfmt
|
RUN rustup component add rustfmt
|
||||||
# criterion
|
# criterion
|
||||||
RUN cargo install cargo-criterion
|
RUN cargo install cargo-criterion
|
||||||
|
# wasm
|
||||||
|
RUN apt -y install libxkbcommon-dev
|
||||||
# sccache
|
# sccache
|
||||||
RUN apt -y install libssl-dev
|
RUN apt -y install libssl-dev
|
||||||
RUN cargo install sccache
|
RUN cargo install sccache
|
||||||
|
|
|
@ -327,7 +327,7 @@ mod cli_run {
|
||||||
|
|
||||||
// TODO fix QuicksortApp and then remove this!
|
// TODO fix QuicksortApp and then remove this!
|
||||||
match benchmark.filename {
|
match benchmark.filename {
|
||||||
"QuicksortApp.roc" | "TestBase64.roc" => {
|
"QuicksortApp.roc" => {
|
||||||
eprintln!("WARNING: skipping testing benchmark {} because the test is broken right now!", benchmark.filename);
|
eprintln!("WARNING: skipping testing benchmark {} because the test is broken right now!", benchmark.filename);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -608,6 +608,14 @@ fn run_with_wasmer(wasm_path: &std::path::Path, stdin: &[&str]) -> String {
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use wasmer::{Instance, Module, Store};
|
use wasmer::{Instance, Module, Store};
|
||||||
|
|
||||||
|
// std::process::Command::new("cp")
|
||||||
|
// .args(&[
|
||||||
|
// wasm_path.to_str().unwrap(),
|
||||||
|
// "/home/folkertdev/roc/wasm/nqueens.wasm",
|
||||||
|
// ])
|
||||||
|
// .output()
|
||||||
|
// .unwrap();
|
||||||
|
|
||||||
let store = Store::default();
|
let store = Store::default();
|
||||||
let module = Module::from_file(&store, &wasm_path).unwrap();
|
let module = Module::from_file(&store, &wasm_path).unwrap();
|
||||||
|
|
||||||
|
|
|
@ -606,26 +606,27 @@ fn link_wasm32(
|
||||||
) -> io::Result<(Child, PathBuf)> {
|
) -> io::Result<(Child, PathBuf)> {
|
||||||
let zig_str_path = find_zig_str_path();
|
let zig_str_path = find_zig_str_path();
|
||||||
|
|
||||||
let child =
|
let child = Command::new("zig9")
|
||||||
Command::new("/home/folkertdev/Downloads/zig-linux-x86_64-0.9.0-dev.848+d5ef5da59/zig")
|
// .env_clear()
|
||||||
// .env_clear()
|
// .env("PATH", &env_path)
|
||||||
// .env("PATH", &env_path)
|
.args(&["build-exe"])
|
||||||
.args(&["build-exe"])
|
.args(input_paths)
|
||||||
.args(input_paths)
|
.args([
|
||||||
.args([
|
&format!("-femit-bin={}", output_path.to_str().unwrap()),
|
||||||
&format!("-femit-bin={}", output_path.to_str().unwrap()),
|
// include libc
|
||||||
// include libc
|
"-lc",
|
||||||
"-lc",
|
"-target",
|
||||||
"-target",
|
"wasm32-wasi-musl",
|
||||||
"wasm32-wasi",
|
"--pkg-begin",
|
||||||
"--pkg-begin",
|
"str",
|
||||||
"str",
|
zig_str_path.to_str().unwrap(),
|
||||||
zig_str_path.to_str().unwrap(),
|
"--pkg-end",
|
||||||
"--pkg-end",
|
"--strip",
|
||||||
// useful for debugging
|
// "-O", "ReleaseSmall",
|
||||||
// "-femit-llvm-ir=/home/folkertdev/roc/roc/examples/benchmarks/platform/host.ll",
|
// useful for debugging
|
||||||
])
|
// "-femit-llvm-ir=/home/folkertdev/roc/roc/examples/benchmarks/platform/host.ll",
|
||||||
.spawn()?;
|
])
|
||||||
|
.spawn()?;
|
||||||
|
|
||||||
Ok((child, output_path))
|
Ok((child, output_path))
|
||||||
}
|
}
|
||||||
|
|
|
@ -1150,8 +1150,8 @@ fn strToBytes(arg: RocStr) RocList {
|
||||||
}
|
}
|
||||||
|
|
||||||
const FromUtf8Result = extern struct {
|
const FromUtf8Result = extern struct {
|
||||||
byte_index: usize,
|
|
||||||
string: RocStr,
|
string: RocStr,
|
||||||
|
byte_index: usize,
|
||||||
is_ok: bool,
|
is_ok: bool,
|
||||||
problem_code: Utf8ByteProblem,
|
problem_code: Utf8ByteProblem,
|
||||||
};
|
};
|
||||||
|
|
|
@ -2189,8 +2189,15 @@ fn list_literal<'a, 'ctx, 'env>(
|
||||||
let global = {
|
let global = {
|
||||||
let mut global_elements = Vec::with_capacity_in(list_length, env.arena);
|
let mut global_elements = Vec::with_capacity_in(list_length, env.arena);
|
||||||
|
|
||||||
// insert NULL bytes for the refcount
|
// Add zero bytes that represent the refcount
|
||||||
// these elements are (dropped again if the list contains non-constants)
|
//
|
||||||
|
// - if all elements are const, then we store the whole list as a constant.
|
||||||
|
// It then needs a refcount before the first element.
|
||||||
|
// - but if the list is not all constants, then we will just copy the constant values,
|
||||||
|
// and we do not need that refcount at the start
|
||||||
|
//
|
||||||
|
// In the latter case, we won't store the zeros in the globals
|
||||||
|
// (we slice them off again below)
|
||||||
for _ in 0..zero_elements {
|
for _ in 0..zero_elements {
|
||||||
global_elements.push(element_type.const_zero());
|
global_elements.push(element_type.const_zero());
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use crate::llvm::bitcode::{call_bitcode_fn, call_void_bitcode_fn};
|
use crate::llvm::bitcode::{call_bitcode_fn, call_void_bitcode_fn};
|
||||||
use crate::llvm::build::{complex_bitcast, Env, Scope};
|
use crate::llvm::build::{complex_bitcast, struct_from_fields, Env, Scope};
|
||||||
use crate::llvm::build_list::{allocate_list, call_bitcode_fn_returns_list, store_list};
|
use crate::llvm::build_list::{allocate_list, call_bitcode_fn_returns_list, store_list};
|
||||||
use inkwell::builder::Builder;
|
use inkwell::builder::Builder;
|
||||||
use inkwell::values::{BasicValueEnum, FunctionValue, IntValue, PointerValue, StructValue};
|
use inkwell::values::{BasicValueEnum, FunctionValue, IntValue, PointerValue, StructValue};
|
||||||
|
@ -260,6 +260,73 @@ pub fn str_to_utf8<'a, 'ctx, 'env>(
|
||||||
call_bitcode_fn_returns_list(env, &[string], bitcode::STR_TO_UTF8)
|
call_bitcode_fn_returns_list(env, &[string], bitcode::STR_TO_UTF8)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn decode_from_utf8_result<'a, 'ctx, 'env>(
|
||||||
|
env: &Env<'a, 'ctx, 'env>,
|
||||||
|
pointer: PointerValue<'ctx>,
|
||||||
|
) -> StructValue<'ctx> {
|
||||||
|
let builder = env.builder;
|
||||||
|
let ctx = env.context;
|
||||||
|
|
||||||
|
let fields = match env.ptr_bytes {
|
||||||
|
8 => [
|
||||||
|
env.ptr_int().into(),
|
||||||
|
super::convert::zig_str_type(env).into(),
|
||||||
|
env.context.bool_type().into(),
|
||||||
|
ctx.i8_type().into(),
|
||||||
|
],
|
||||||
|
4 => [
|
||||||
|
super::convert::zig_str_type(env).into(),
|
||||||
|
env.ptr_int().into(),
|
||||||
|
env.context.bool_type().into(),
|
||||||
|
ctx.i8_type().into(),
|
||||||
|
],
|
||||||
|
_ => unreachable!(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let record_type = env.context.struct_type(&fields, false);
|
||||||
|
|
||||||
|
match env.ptr_bytes {
|
||||||
|
8 => {
|
||||||
|
let zig_struct = builder
|
||||||
|
.build_load(pointer, "load_utf8_validate_bytes_result")
|
||||||
|
.into_struct_value();
|
||||||
|
|
||||||
|
let string = builder
|
||||||
|
.build_extract_value(zig_struct, 0, "string")
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let byte_index = builder
|
||||||
|
.build_extract_value(zig_struct, 1, "byte_index")
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let is_ok = builder.build_extract_value(zig_struct, 2, "is_ok").unwrap();
|
||||||
|
|
||||||
|
let problem_code = builder
|
||||||
|
.build_extract_value(zig_struct, 3, "problem_code")
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let values = [byte_index, string, is_ok, problem_code];
|
||||||
|
|
||||||
|
struct_from_fields(env, record_type, values.iter().copied().enumerate())
|
||||||
|
}
|
||||||
|
4 => {
|
||||||
|
let result_ptr_cast = env
|
||||||
|
.builder
|
||||||
|
.build_bitcast(
|
||||||
|
pointer,
|
||||||
|
record_type.ptr_type(AddressSpace::Generic),
|
||||||
|
"to_unnamed",
|
||||||
|
)
|
||||||
|
.into_pointer_value();
|
||||||
|
|
||||||
|
builder
|
||||||
|
.build_load(result_ptr_cast, "load_utf8_validate_bytes_result")
|
||||||
|
.into_struct_value()
|
||||||
|
}
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Str.fromUtf8 : List U8, { count : Nat, start : Nat } -> { a : Bool, b : Str, c : Nat, d : I8 }
|
/// Str.fromUtf8 : List U8, { count : Nat, start : Nat } -> { a : Bool, b : Str, c : Nat, d : I8 }
|
||||||
pub fn str_from_utf8_range<'a, 'ctx, 'env>(
|
pub fn str_from_utf8_range<'a, 'ctx, 'env>(
|
||||||
env: &Env<'a, 'ctx, 'env>,
|
env: &Env<'a, 'ctx, 'env>,
|
||||||
|
@ -268,7 +335,6 @@ pub fn str_from_utf8_range<'a, 'ctx, 'env>(
|
||||||
count_and_start: StructValue<'ctx>,
|
count_and_start: StructValue<'ctx>,
|
||||||
) -> BasicValueEnum<'ctx> {
|
) -> BasicValueEnum<'ctx> {
|
||||||
let builder = env.builder;
|
let builder = env.builder;
|
||||||
let ctx = env.context;
|
|
||||||
|
|
||||||
let result_type = env.module.get_struct_type("str.FromUtf8Result").unwrap();
|
let result_type = env.module.get_struct_type("str.FromUtf8Result").unwrap();
|
||||||
let result_ptr = builder.build_alloca(result_type, "alloca_utf8_validate_bytes_result");
|
let result_ptr = builder.build_alloca(result_type, "alloca_utf8_validate_bytes_result");
|
||||||
|
@ -293,26 +359,7 @@ pub fn str_from_utf8_range<'a, 'ctx, 'env>(
|
||||||
bitcode::STR_FROM_UTF8_RANGE,
|
bitcode::STR_FROM_UTF8_RANGE,
|
||||||
);
|
);
|
||||||
|
|
||||||
let record_type = env.context.struct_type(
|
decode_from_utf8_result(env, result_ptr).into()
|
||||||
&[
|
|
||||||
env.ptr_int().into(),
|
|
||||||
super::convert::zig_str_type(env).into(),
|
|
||||||
env.context.bool_type().into(),
|
|
||||||
ctx.i8_type().into(),
|
|
||||||
],
|
|
||||||
false,
|
|
||||||
);
|
|
||||||
|
|
||||||
let result_ptr_cast = env
|
|
||||||
.builder
|
|
||||||
.build_bitcast(
|
|
||||||
result_ptr,
|
|
||||||
record_type.ptr_type(AddressSpace::Generic),
|
|
||||||
"to_unnamed",
|
|
||||||
)
|
|
||||||
.into_pointer_value();
|
|
||||||
|
|
||||||
builder.build_load(result_ptr_cast, "load_utf8_validate_bytes_result")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Str.fromUtf8 : List U8 -> { a : Bool, b : Str, c : Nat, d : I8 }
|
/// Str.fromUtf8 : List U8 -> { a : Bool, b : Str, c : Nat, d : I8 }
|
||||||
|
@ -322,7 +369,6 @@ pub fn str_from_utf8<'a, 'ctx, 'env>(
|
||||||
original_wrapper: StructValue<'ctx>,
|
original_wrapper: StructValue<'ctx>,
|
||||||
) -> BasicValueEnum<'ctx> {
|
) -> BasicValueEnum<'ctx> {
|
||||||
let builder = env.builder;
|
let builder = env.builder;
|
||||||
let ctx = env.context;
|
|
||||||
|
|
||||||
let result_type = env.module.get_struct_type("str.FromUtf8Result").unwrap();
|
let result_type = env.module.get_struct_type("str.FromUtf8Result").unwrap();
|
||||||
let result_ptr = builder.build_alloca(result_type, "alloca_utf8_validate_bytes_result");
|
let result_ptr = builder.build_alloca(result_type, "alloca_utf8_validate_bytes_result");
|
||||||
|
@ -341,26 +387,7 @@ pub fn str_from_utf8<'a, 'ctx, 'env>(
|
||||||
bitcode::STR_FROM_UTF8,
|
bitcode::STR_FROM_UTF8,
|
||||||
);
|
);
|
||||||
|
|
||||||
let record_type = env.context.struct_type(
|
decode_from_utf8_result(env, result_ptr).into()
|
||||||
&[
|
|
||||||
env.ptr_int().into(),
|
|
||||||
super::convert::zig_str_type(env).into(),
|
|
||||||
env.context.bool_type().into(),
|
|
||||||
ctx.i8_type().into(),
|
|
||||||
],
|
|
||||||
false,
|
|
||||||
);
|
|
||||||
|
|
||||||
let result_ptr_cast = env
|
|
||||||
.builder
|
|
||||||
.build_bitcast(
|
|
||||||
result_ptr,
|
|
||||||
record_type.ptr_type(AddressSpace::Generic),
|
|
||||||
"to_unnamed",
|
|
||||||
)
|
|
||||||
.into_pointer_value();
|
|
||||||
|
|
||||||
builder.build_load(result_ptr_cast, "load_utf8_validate_bytes_result")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Str.fromInt : Int -> Str
|
/// Str.fromInt : Int -> Str
|
||||||
|
|
3
compiler/gen_wasm/.gitignore
vendored
Normal file
3
compiler/gen_wasm/.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
*.wasm
|
||||||
|
*.wat
|
||||||
|
/notes.md
|
170
compiler/gen_wasm/README.md
Normal file
170
compiler/gen_wasm/README.md
Normal file
|
@ -0,0 +1,170 @@
|
||||||
|
# Development backend for WebAssembly
|
||||||
|
|
||||||
|
## Plan
|
||||||
|
|
||||||
|
- Initial bringup
|
||||||
|
- Get a wasm backend working for some of the number tests.
|
||||||
|
- Use a separate `gen_wasm` directory for now, to avoid trying to do bringup and integration at the same time.
|
||||||
|
- Integration
|
||||||
|
- Move wasm files to `gen_dev/src/wasm`
|
||||||
|
- Share tests between wasm and x64, with some way of saying which tests work on which backends, and dispatching to different eval helpers based on that.
|
||||||
|
- Get `build_module` in object_builder.rs to dispatch to the wasm generator (adding some Wasm options to the `Triple` struct)
|
||||||
|
- Get `build_module` to write to a file, or maybe return `Vec<u8>`, instead of returning an Object structure
|
||||||
|
- Code sharing
|
||||||
|
- Try to ensure that both Wasm and x64 use the same `Backend` trait so that we can share code.
|
||||||
|
- We need to work towards this after we've progressed a bit more with Wasm and gained more understanding and experience of the differences.
|
||||||
|
- We will have to think about how to deal with the `Backend` code that doesn't apply to Wasm. Perhaps we will end up with more traits like `RegisterBackend` / `StackBackend` or `NativeBackend` / `WasmBackend`, and perhaps even some traits to do with backends that support jumps and those that don't.
|
||||||
|
|
||||||
|
## Structured control flow
|
||||||
|
|
||||||
|
🚨 **This is an area that could be tricky** 🚨
|
||||||
|
|
||||||
|
One of the security features of WebAssembly is that it does not allow unrestricted "jumps" to anywhere you like. It does not have an instruction for that. All of the [control instructions][control-inst] can only implement "structured" control flow, and have names like `if`, `loop`, `block` that you'd normally associate with high-level languages. There are branch (`br`) instructions that can jump to labelled blocks within the same function, but the blocks have to be nested in sensible ways.
|
||||||
|
|
||||||
|
[control-inst]: https://webassembly.github.io/spec/core/syntax/instructions.html#control-instructions
|
||||||
|
|
||||||
|
Implications:
|
||||||
|
|
||||||
|
Roc, like most modern languages, is already enforcing structured control flow in the source program. Constructs from the Roc AST like `When`, `If` and `LetRec` can all be converted straightforwardly to Wasm constructs.
|
||||||
|
|
||||||
|
However the Mono IR converts this to jumps and join points, which are more of a Control Flow Graph than a tree. That doesn't map so directly to the Wasm structures. This is such a common issue for compiler back-ends that the WebAssembly compiler toolkit `binaryen` has an [API for control-flow graphs][cfg-api]. We're not using `binaryen` right now. It's a C++ library, though it does have a (very thin and somewhat hard-to-use) [Rust wrapper][binaryen-rs]. We should probably investigate this area sooner rather than later. If relooping turns out to be necessary or difficult, we might need to switch from parity_wasm to binaryen.
|
||||||
|
|
||||||
|
> By the way, it's not obvious how to pronounce "binaryen" but apparently it rhymes with "Targaryen", the family name from the "Game of Thrones" TV series
|
||||||
|
|
||||||
|
[cfg-api]: https://github.com/WebAssembly/binaryen/wiki/Compiling-to-WebAssembly-with-Binaryen#cfg-api
|
||||||
|
[binaryen-rs]: https://crates.io/crates/binaryen
|
||||||
|
|
||||||
|
Binaryen's control-flow graph API implements the "Relooper" algorithm developed by the Emscripten project and described in [this paper](https://github.com/emscripten-core/emscripten/blob/main/docs/paper.pdf).
|
||||||
|
|
||||||
|
There is an alternative algorithm that is supposed to be an improvement on Relooper, called ["Stackifier"](https://medium.com/leaningtech/solving-the-structured-control-flow-problem-once-and-for-all-5123117b1ee2).
|
||||||
|
|
||||||
|
|
||||||
|
## Stack machine vs register machine
|
||||||
|
|
||||||
|
Wasm's instruction set is based on a stack-machine VM. Whereas CPU instructions have named registers that they operate on, Wasm has no named registers at all. The instructions don't contain register names. Instructions can oly operate on whatever data is at the top of the stack.
|
||||||
|
|
||||||
|
For example the instruction `i64.add` takes two operands. It pops the top two arguments off the VM stack and pushes the result back.
|
||||||
|
|
||||||
|
In the [spec][spec-instructions], every instruction has a type signature! This is not something you would see for CPU instructions. The type signature for i64.add is `[i64 i64] → [i64]` because it pushes two i64's and pops an i64.
|
||||||
|
|
||||||
|
[spec-instructions]: https://webassembly.github.io/spec/core/appendix/index-instructions.html
|
||||||
|
|
||||||
|
This means that WebAssembly has a concept of type checking. When you load a .wasm file as a chunk of bytes into a Wasm runtime (like a browser or [wasmer](https://wasmer.io/)), the runtime will first _validate_ those bytes. They have some fast way of checking whether the types being pushed and popped are consistent. So if you try to do the i64.add instruction when you have floats on the stack, it will fail validation.
|
||||||
|
|
||||||
|
Note that the instruction makes no mention of any source or destination registers, because there is no such thing. It just pops two values and pushes one. (This architecture choice helps to keep WebAssembly programs quite compact. There are no extra bytes specifying source and destination registers.)
|
||||||
|
|
||||||
|
Implications of the stack machine for Roc:
|
||||||
|
|
||||||
|
- There is no such thing as register allocation, since there are no registers! There is no reason to maintain hashmaps of what registers are free or not. And there is no need to do a pass over the IR to find the "last seen" occurrence of a symbol in the IR. That means we don't need the `Backend` methods `scan_ast`, `scan_ast_call`, `set_last_seen`, `last_seen_map`, `free_map`, `free_symbols`, `free_symbol`, `set_free_map`.
|
||||||
|
|
||||||
|
- There is no random access to the stack. All instructions operate on the data at the _top_ of the stack. There is no instruction that says "get the value at index 17 in the stack". If such an instruction did exist, it wouldn't be a stack machine. And there is no way to "free up some of the slots in the stack". You have to consume the stuff at the top, then the stuff further down. However Wasm has a concept of local variables, which do allow random access. See below.
|
||||||
|
|
||||||
|
## Local variables
|
||||||
|
|
||||||
|
WebAssembly functions can have any number of local variables. They are declared at the beginning of the function, along with their types (just like C). WebAssembly has 4 value types: `i32`, `i64`, `f32`, `f64`.
|
||||||
|
|
||||||
|
In this backend, each symbol in the Mono IR gets one WebAssembly local. To illustrate, let's translate a simple Roc example to WebAssembly text format.
|
||||||
|
The WebAssembly code below is completely unoptimised and uses far more locals than necessary. But that does help to illustrate the concept of locals.
|
||||||
|
|
||||||
|
```
|
||||||
|
app "test" provides [ main ] to "./platform"
|
||||||
|
|
||||||
|
main =
|
||||||
|
1 + 2 + 4
|
||||||
|
```
|
||||||
|
|
||||||
|
The Mono IR contains two functions, `Num.add` and `main`, so we generate two corresponding WebAssembly functions.
|
||||||
|
|
||||||
|
```
|
||||||
|
(func (;0;) (param i64 i64) (result i64) ; declare function index 0 (Num.add) with two i64 parameters and an i64 result
|
||||||
|
local.get 0 ; load param 0 stack=[param0]
|
||||||
|
local.get 1 ; load param 1 stack=[param0, param1]
|
||||||
|
i64.add ; pop two values, add, and push result stack=[param0 + param1]
|
||||||
|
return) ; return the value at the top of the stack
|
||||||
|
|
||||||
|
(func (;1;) (result i64) ; declare function index 1 (main) with no parameters and an i64 result
|
||||||
|
(local i64 i64 i64 i64) ; declare 4 local variables, all with type i64, one for each symbol in the Mono IR
|
||||||
|
i64.const 1 ; load constant of type i64 and value 1 stack=[1]
|
||||||
|
local.set 0 ; store top of stack to local0 stack=[] local0=1
|
||||||
|
i64.const 2 ; load constant of type i64 and value 2 stack=[2] local0=1
|
||||||
|
local.set 1 ; store top of stack to local1 stack=[] local0=1 local1=2
|
||||||
|
local.get 0 ; load local0 to top of stack stack=[1] local0=1 local1=2
|
||||||
|
local.get 1 ; load local1 to top of stack stack=[1,2] local0=1 local1=2
|
||||||
|
call 0 ; call function index 0 (which pops 2 and pushes 1) stack=[3] local0=1 local1=2
|
||||||
|
local.set 2 ; store top of stack to local2 stack=[] local0=1 local1=2 local2=3
|
||||||
|
i64.const 4 ; load constant of type i64 and value 4 stack=[4] local0=1 local1=2 local2=3
|
||||||
|
local.set 3 ; store top of stack to local3 stack=[] local0=1 local1=2 local2=3 local3=4
|
||||||
|
local.get 2 ; load local2 to top of stack stack=[3] local0=1 local1=2 local2=3 local3=4
|
||||||
|
local.get 3 ; load local3 to top of stack stack=[3,4] local0=1 local1=2 local2=3 local3=4
|
||||||
|
call 0 ; call function index 0 (which pops 2 and pushes 1) stack=[7] local0=1 local1=2 local2=3 local3=4
|
||||||
|
return) ; return the value at the top of the stack
|
||||||
|
```
|
||||||
|
|
||||||
|
If we run this code through the `wasm-opt` tool from the [binaryen toolkit](https://github.com/WebAssembly/binaryen#tools), the unnecessary locals get optimised away. The command line below runs the minimum number of passes to achieve this (`--simplify-locals` must come first).
|
||||||
|
|
||||||
|
```
|
||||||
|
$ wasm-opt --simplify-locals --reorder-locals --vacuum example.wasm > opt.wasm
|
||||||
|
```
|
||||||
|
|
||||||
|
The optimised functions have no local variables, and the code shrinks to about 60% of its original size.
|
||||||
|
```
|
||||||
|
(func (;0;) (param i64 i64) (result i64)
|
||||||
|
local.get 0
|
||||||
|
local.get 1
|
||||||
|
i64.add)
|
||||||
|
(func (;1;) (result i64)
|
||||||
|
i64.const 1
|
||||||
|
i64.const 2
|
||||||
|
call 0
|
||||||
|
i64.const 4)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Memory
|
||||||
|
|
||||||
|
WebAssembly programs have a "linear memory" for storing data, which is a block of memory assigned to it by the host. You can assign a min and max size to the memory, and the WebAssembly program can request 64kB pages from the host, just like a "normal" program would request pages from the OS. Addresses start at zero and go up to whatever the current size is. Zero is a perfectly normal address like any other, and dereferencing it is not a segfault. But addresses beyond the current memory size are out of bounds and dereferencing them will cause a panic.
|
||||||
|
|
||||||
|
The program has full read/write access to the memory and can divide it into whatever sections it wants. Most programs will want to do the traditional split of static memory, stack memory and heap memory.
|
||||||
|
|
||||||
|
The WebAssembly module structure includes a data section that will be copied into the linear memory at a specified offset on initialisation, so you can use that for string literals etc. But the division of the rest of memory into "stack" and "heap" areas is not a first-class concept. It is up to the compiler to generate instructions to do whatever it wants with that memory.
|
||||||
|
|
||||||
|
## Stack machine vs stack memory
|
||||||
|
|
||||||
|
**There are two entirely different meanings of the word "stack" that are relevant to the WebAssembly backend.** It's unfortunate that the word "stack" is so overloaded. I guess it's just a useful data structure. The worst thing is that both of them tend to be referred to as just "the stack"! We need more precise terms.
|
||||||
|
|
||||||
|
When we are talking about the instruction set, I'll use the term _machine stack_ or _VM stack_. This is the implicit data structure that WebAssembly instructions operate on. In the examples above, it's where `i64.add` gets its arguments and stores its result. I think of it as an abstraction over CPU registers, that WebAssembly uses in order to be portable and compact.
|
||||||
|
|
||||||
|
When we are talking about how we store values in _memory_, I'll use the term _stack memory_ rather than just "the stack". It feels clunky but it's the best I can think of.
|
||||||
|
|
||||||
|
Of course our program can use another area of memory as a heap as well. WebAssembly doesn't mind how you divide up your memory. It just gives you some memory and some instructions for loading and storing.
|
||||||
|
|
||||||
|
## Function calls
|
||||||
|
|
||||||
|
In WebAssembly you call a function by pushing arguments to the stack and then issuing a `call` instruction, which specifies a function index. The VM knows how many values to pop off the stack by examining the _type_ of the function. In our example earlier, `Num.add` had the type `[i64 i64] → [i64]` so it expects to find two i64's on the stack and pushes one i64 back as the result. Remember, the runtime engine will validate the module before running it, and if your generated code is trying to call a function at a point in the program where the wrong value types are on the stack, it will fail validation.
|
||||||
|
|
||||||
|
Function arguments are restricted to the four value types, `i32`, `i64`, `f32` and `f64`. If those are all we need, then there is _no need for any stack memory_, stack pointer, etc. We saw this in our example earlier. We just said `call 0`. We didn't need any instructions to create a stack frame with a return address, and there was no "jump" instruction. Essentially, WebAssembly has a first-class concept of function calls, so you don't build it up from lower-level primitives. You could think of this as an abstraction over calling conventions.
|
||||||
|
|
||||||
|
That's all great for primitive values but what happens when we want to pass more complex data structures between functions?
|
||||||
|
|
||||||
|
Well, remember, "stack memory" is not a special kind of memory in WebAssembly, it's just an area of our memory where we _decide_ that we want to implement a stack data structure. So we can implement it however we want. A good choice would be to make our stack frame look the same as it would when we're targeting a CPU, except without the return address (since there's no need for one). We can also decide to pass numbers through the machine stack rather than in stack memory, since that takes fewer instructions.
|
||||||
|
|
||||||
|
The only other thing we need is a stack pointer. On CPU targets, there's often have a specific "stack pointer" register. WebAssembly has no equivalent to that, but we can use a `global` variable.
|
||||||
|
|
||||||
|
The system I've outlined above is based on my experience of compiling C to WebAssembly via the Emscripten toolchain (which is built on top of clang). It's also in line with what the WebAssembly project describes [here](https://github.com/WebAssembly/design/blob/main/Rationale.md#locals).
|
||||||
|
|
||||||
|
## Modules vs Instances
|
||||||
|
|
||||||
|
What's the difference between a Module and an Instance in WebAssembly?
|
||||||
|
|
||||||
|
Well, if I compare it to running a program on Linux, it's like the difference between an ELF binary and the executable image in memory that you get when you _load_ that ELF file. The ELF file is essentially a _specification_ for how to create the executable image. In order to start executing the program, the OS has to actually allocate a stack and a heap, and load the text and data. If you run multiple copies of the same program, they will each have their own memory and their own execution state. (More detail [here](https://wiki.osdev.org/ELF#Loading_ELF_Binaries)).
|
||||||
|
|
||||||
|
The Module is like the ELF file, and the Instance is like the executable image.
|
||||||
|
|
||||||
|
The Module is a _specification_ for how to create an Instance of the program. The Module says how much memory the program needs, but the Instance actually _contains_ that memory. In order to run the Wasm program, the VM needs to create an instance, allocate some memory for it, and copy the data section into that memory. If you run many copies of the same Wasm program, you will have one Module but many Instances. Each instance will have its own separate area of memory, and its own execution state.
|
||||||
|
|
||||||
|
## Modules, object files, and linking
|
||||||
|
|
||||||
|
A WebAssembly module is equivalent to an executable file. It doesn't normally need relocations since at the WebAssembly layer, there is no Address Space Layout Randomisation. If it has relocations then it's an object file.
|
||||||
|
|
||||||
|
The [official spec](https://webassembly.github.io/spec/core/binary/modules.html#sections) lists the sections that are part of the final module. It doesn't mention any sections for relocations or symbol names, but it has room for "custom sections" that in practice seem to be used for that.
|
||||||
|
|
||||||
|
The WebAssembly `tool-conventions` repo has a document on [linking](https://github.com/WebAssembly/tool-conventions/blob/main/Linking.md), and the `parity_wasm` crate supports "name" and "relocation" [sections](https://docs.rs/parity-wasm/0.42.2/parity_wasm/elements/enum.Section.html).
|
303
compiler/gen_wasm/src/backend.rs
Normal file
303
compiler/gen_wasm/src/backend.rs
Normal file
|
@ -0,0 +1,303 @@
|
||||||
|
use parity_wasm::builder;
|
||||||
|
use parity_wasm::builder::{CodeLocation, ModuleBuilder};
|
||||||
|
use parity_wasm::elements::{Instruction, Instruction::*, Instructions, Local, ValueType};
|
||||||
|
|
||||||
|
use roc_collections::all::MutMap;
|
||||||
|
use roc_module::low_level::LowLevel;
|
||||||
|
use roc_module::symbol::Symbol;
|
||||||
|
use roc_mono::ir::{CallType, Expr, Literal, Proc, Stmt};
|
||||||
|
use roc_mono::layout::{Builtin, Layout};
|
||||||
|
|
||||||
|
// Don't allocate any constant data at address zero or near it. Would be valid, but bug-prone.
|
||||||
|
// Follow Emscripten's example by using 1kB (4 bytes would probably do)
|
||||||
|
const UNUSED_DATA_SECTION_BYTES: u32 = 1024;
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug)]
|
||||||
|
struct LocalId(u32);
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug)]
|
||||||
|
struct LabelId(u32);
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct SymbolStorage(LocalId, WasmLayout);
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct WasmLayout {
|
||||||
|
value_type: ValueType,
|
||||||
|
stack_memory: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl WasmLayout {
|
||||||
|
fn new(layout: &Layout) -> Result<Self, String> {
|
||||||
|
match layout {
|
||||||
|
Layout::Builtin(Builtin::Int64) => Ok(Self {
|
||||||
|
value_type: ValueType::I64,
|
||||||
|
stack_memory: 0,
|
||||||
|
}),
|
||||||
|
Layout::Builtin(Builtin::Float64) => Ok(Self {
|
||||||
|
value_type: ValueType::F64,
|
||||||
|
stack_memory: 0,
|
||||||
|
}),
|
||||||
|
x => Err(format!("layout, {:?}, not implemented yet", x)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct WasmBackend<'a> {
|
||||||
|
// Module: Wasm AST
|
||||||
|
pub builder: ModuleBuilder,
|
||||||
|
|
||||||
|
// Module: internal state & IR mappings
|
||||||
|
_data_offset_map: MutMap<Literal<'a>, u32>,
|
||||||
|
_data_offset_next: u32,
|
||||||
|
proc_symbol_map: MutMap<Symbol, CodeLocation>,
|
||||||
|
|
||||||
|
// Functions: Wasm AST
|
||||||
|
instructions: std::vec::Vec<Instruction>,
|
||||||
|
ret_type: ValueType,
|
||||||
|
arg_types: std::vec::Vec<ValueType>,
|
||||||
|
locals: std::vec::Vec<Local>,
|
||||||
|
|
||||||
|
// Functions: internal state & IR mappings
|
||||||
|
stack_memory: u32,
|
||||||
|
symbol_storage_map: MutMap<Symbol, SymbolStorage>,
|
||||||
|
// joinpoint_label_map: MutMap<JoinPointId, LabelId>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> WasmBackend<'a> {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
WasmBackend {
|
||||||
|
// Module: Wasm AST
|
||||||
|
builder: builder::module(),
|
||||||
|
|
||||||
|
// Module: internal state & IR mappings
|
||||||
|
_data_offset_map: MutMap::default(),
|
||||||
|
_data_offset_next: UNUSED_DATA_SECTION_BYTES,
|
||||||
|
proc_symbol_map: MutMap::default(),
|
||||||
|
|
||||||
|
// Functions: Wasm AST
|
||||||
|
instructions: std::vec::Vec::with_capacity(256),
|
||||||
|
ret_type: ValueType::I32,
|
||||||
|
arg_types: std::vec::Vec::with_capacity(8),
|
||||||
|
locals: std::vec::Vec::with_capacity(32),
|
||||||
|
|
||||||
|
// Functions: internal state & IR mappings
|
||||||
|
stack_memory: 0,
|
||||||
|
symbol_storage_map: MutMap::default(),
|
||||||
|
// joinpoint_label_map: MutMap::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn reset(&mut self) {
|
||||||
|
// Functions: Wasm AST
|
||||||
|
self.instructions.clear();
|
||||||
|
self.arg_types.clear();
|
||||||
|
self.locals.clear();
|
||||||
|
|
||||||
|
// Functions: internal state & IR mappings
|
||||||
|
self.stack_memory = 0;
|
||||||
|
self.symbol_storage_map.clear();
|
||||||
|
// joinpoint_label_map.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn build_proc(&mut self, proc: Proc<'a>, sym: Symbol) -> Result<u32, String> {
|
||||||
|
let ret_layout = WasmLayout::new(&proc.ret_layout)?;
|
||||||
|
if ret_layout.stack_memory > 0 {
|
||||||
|
// TODO: if returning a struct by value, add an extra argument for a pointer to callee's stack memory
|
||||||
|
return Err(format!(
|
||||||
|
"Not yet implemented: Return in stack memory for non-primtitive layouts like {:?}",
|
||||||
|
proc.ret_layout
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
self.ret_type = ret_layout.value_type;
|
||||||
|
self.arg_types.reserve(proc.args.len());
|
||||||
|
|
||||||
|
for (layout, symbol) in proc.args {
|
||||||
|
let wasm_layout = WasmLayout::new(layout)?;
|
||||||
|
self.arg_types.push(wasm_layout.value_type);
|
||||||
|
self.insert_local(wasm_layout, *symbol);
|
||||||
|
}
|
||||||
|
|
||||||
|
self.build_stmt(&proc.body, &proc.ret_layout)?;
|
||||||
|
|
||||||
|
let signature = builder::signature()
|
||||||
|
.with_params(self.arg_types.clone()) // requires std::Vec, not Bumpalo
|
||||||
|
.with_result(self.ret_type)
|
||||||
|
.build_sig();
|
||||||
|
|
||||||
|
// functions must end with an End instruction/opcode
|
||||||
|
let mut instructions = self.instructions.clone();
|
||||||
|
instructions.push(Instruction::End);
|
||||||
|
|
||||||
|
let function_def = builder::function()
|
||||||
|
.with_signature(signature)
|
||||||
|
.body()
|
||||||
|
.with_locals(self.locals.clone())
|
||||||
|
.with_instructions(Instructions::new(instructions))
|
||||||
|
.build() // body
|
||||||
|
.build(); // function
|
||||||
|
|
||||||
|
let location = self.builder.push_function(function_def);
|
||||||
|
let function_index = location.body;
|
||||||
|
self.proc_symbol_map.insert(sym, location);
|
||||||
|
self.reset();
|
||||||
|
|
||||||
|
Ok(function_index)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn insert_local(&mut self, layout: WasmLayout, symbol: Symbol) -> LocalId {
|
||||||
|
self.stack_memory += layout.stack_memory;
|
||||||
|
let index = self.symbol_storage_map.len();
|
||||||
|
if index >= self.arg_types.len() {
|
||||||
|
self.locals.push(Local::new(1, layout.value_type));
|
||||||
|
}
|
||||||
|
let local_id = LocalId(index as u32);
|
||||||
|
let storage = SymbolStorage(local_id, layout);
|
||||||
|
self.symbol_storage_map.insert(symbol, storage);
|
||||||
|
local_id
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_symbol_storage(&self, sym: &Symbol) -> Result<&SymbolStorage, String> {
|
||||||
|
self.symbol_storage_map.get(sym).ok_or_else(|| {
|
||||||
|
format!(
|
||||||
|
"Symbol {:?} not found in function scope:\n{:?}",
|
||||||
|
sym, self.symbol_storage_map
|
||||||
|
)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn load_from_symbol(&mut self, sym: &Symbol) -> Result<(), String> {
|
||||||
|
let SymbolStorage(LocalId(local_id), _) = self.get_symbol_storage(sym)?;
|
||||||
|
let id: u32 = *local_id;
|
||||||
|
self.instructions.push(GetLocal(id));
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_stmt(&mut self, stmt: &Stmt<'a>, ret_layout: &Layout<'a>) -> Result<(), String> {
|
||||||
|
match stmt {
|
||||||
|
// This pattern is a simple optimisation to get rid of one local and two instructions per proc.
|
||||||
|
// If we are just returning the expression result, then don't SetLocal and immediately GetLocal
|
||||||
|
Stmt::Let(let_sym, expr, layout, Stmt::Ret(ret_sym)) if let_sym == ret_sym => {
|
||||||
|
self.build_expr(let_sym, expr, layout)?;
|
||||||
|
self.instructions.push(Return);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
Stmt::Let(sym, expr, layout, following) => {
|
||||||
|
let wasm_layout = WasmLayout::new(layout)?;
|
||||||
|
let local_id = self.insert_local(wasm_layout, *sym);
|
||||||
|
|
||||||
|
self.build_expr(sym, expr, layout)?;
|
||||||
|
self.instructions.push(SetLocal(local_id.0));
|
||||||
|
|
||||||
|
self.build_stmt(following, ret_layout)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
Stmt::Ret(sym) => {
|
||||||
|
if let Some(SymbolStorage(local_id, _)) = self.symbol_storage_map.get(sym) {
|
||||||
|
self.instructions.push(GetLocal(local_id.0));
|
||||||
|
self.instructions.push(Return);
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(format!(
|
||||||
|
"Not yet implemented: returning values with layout {:?}",
|
||||||
|
ret_layout
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
x => Err(format!("statement not yet implemented: {:?}", x)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_expr(
|
||||||
|
&mut self,
|
||||||
|
sym: &Symbol,
|
||||||
|
expr: &Expr<'a>,
|
||||||
|
layout: &Layout<'a>,
|
||||||
|
) -> Result<(), String> {
|
||||||
|
match expr {
|
||||||
|
Expr::Literal(lit) => self.load_literal(lit),
|
||||||
|
|
||||||
|
Expr::Call(roc_mono::ir::Call {
|
||||||
|
call_type,
|
||||||
|
arguments,
|
||||||
|
}) => match call_type {
|
||||||
|
CallType::ByName { name: func_sym, .. } => {
|
||||||
|
for arg in *arguments {
|
||||||
|
self.load_from_symbol(arg)?;
|
||||||
|
}
|
||||||
|
let function_location = self.proc_symbol_map.get(func_sym).ok_or(format!(
|
||||||
|
"Cannot find function {:?} called from {:?}",
|
||||||
|
func_sym, sym
|
||||||
|
))?;
|
||||||
|
self.instructions.push(Call(function_location.body));
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
CallType::LowLevel { op: lowlevel, .. } => {
|
||||||
|
self.build_call_low_level(lowlevel, arguments, layout)
|
||||||
|
}
|
||||||
|
x => Err(format!("the call type, {:?}, is not yet implemented", x)),
|
||||||
|
},
|
||||||
|
|
||||||
|
x => Err(format!("Expression is not yet implemented {:?}", x)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn load_literal(&mut self, lit: &Literal<'a>) -> Result<(), String> {
|
||||||
|
match lit {
|
||||||
|
Literal::Int(x) => {
|
||||||
|
self.instructions.push(I64Const(*x as i64));
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
Literal::Float(x) => {
|
||||||
|
let val: f64 = *x;
|
||||||
|
self.instructions.push(F64Const(val.to_bits()));
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
x => Err(format!("loading literal, {:?}, is not yet implemented", x)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_call_low_level(
|
||||||
|
&mut self,
|
||||||
|
lowlevel: &LowLevel,
|
||||||
|
args: &'a [Symbol],
|
||||||
|
return_layout: &Layout<'a>,
|
||||||
|
) -> Result<(), String> {
|
||||||
|
for arg in args {
|
||||||
|
self.load_from_symbol(arg)?;
|
||||||
|
}
|
||||||
|
let wasm_layout = WasmLayout::new(return_layout)?;
|
||||||
|
self.build_instructions_lowlevel(lowlevel, wasm_layout.value_type)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_instructions_lowlevel(
|
||||||
|
&mut self,
|
||||||
|
lowlevel: &LowLevel,
|
||||||
|
return_value_type: ValueType,
|
||||||
|
) -> Result<(), String> {
|
||||||
|
// TODO: Find a way to organise all the lowlevel ops and layouts! There's lots!
|
||||||
|
//
|
||||||
|
// Some Roc low-level ops care about wrapping, clipping, sign-extending...
|
||||||
|
// For those, we'll need to pre-process each argument before the main op,
|
||||||
|
// so simple arrays of instructions won't work. But there are common patterns.
|
||||||
|
let instructions: &[Instruction] = match lowlevel {
|
||||||
|
// Wasm type might not be enough, may need to sign-extend i8 etc. Maybe in load_from_symbol?
|
||||||
|
LowLevel::NumAdd => match return_value_type {
|
||||||
|
ValueType::I32 => &[I32Add],
|
||||||
|
ValueType::I64 => &[I64Add],
|
||||||
|
ValueType::F32 => &[F32Add],
|
||||||
|
ValueType::F64 => &[F64Add],
|
||||||
|
},
|
||||||
|
_ => {
|
||||||
|
return Err(format!("unsupported low-level op {:?}", lowlevel));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
self.instructions.extend_from_slice(instructions);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
|
@ -1 +1,62 @@
|
||||||
|
mod backend;
|
||||||
pub mod from_wasm32_memory;
|
pub mod from_wasm32_memory;
|
||||||
|
|
||||||
|
use bumpalo::Bump;
|
||||||
|
use parity_wasm::builder;
|
||||||
|
use parity_wasm::elements::Internal;
|
||||||
|
|
||||||
|
use roc_collections::all::{MutMap, MutSet};
|
||||||
|
use roc_module::symbol::{Interns, Symbol};
|
||||||
|
use roc_mono::ir::{Proc, ProcLayout};
|
||||||
|
use roc_mono::layout::LayoutIds;
|
||||||
|
|
||||||
|
use crate::backend::WasmBackend;
|
||||||
|
|
||||||
|
pub struct Env<'a> {
|
||||||
|
pub arena: &'a Bump, // not really using this much, parity_wasm works with std::vec a lot
|
||||||
|
pub interns: Interns,
|
||||||
|
pub exposed_to_host: MutSet<Symbol>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn build_module<'a>(
|
||||||
|
env: &'a Env,
|
||||||
|
procedures: MutMap<(Symbol, ProcLayout<'a>), Proc<'a>>,
|
||||||
|
) -> Result<std::vec::Vec<u8>, String> {
|
||||||
|
let mut backend = WasmBackend::new();
|
||||||
|
let mut layout_ids = LayoutIds::default();
|
||||||
|
|
||||||
|
for ((sym, layout), proc) in procedures {
|
||||||
|
let function_index = backend.build_proc(proc, sym)?;
|
||||||
|
if env.exposed_to_host.contains(&sym) {
|
||||||
|
let fn_name = layout_ids
|
||||||
|
.get_toplevel(sym, &layout)
|
||||||
|
.to_symbol_string(sym, &env.interns);
|
||||||
|
|
||||||
|
let export = builder::export()
|
||||||
|
.field(fn_name.as_str())
|
||||||
|
.with_internal(Internal::Function(function_index))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
backend.builder.push_export(export);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const MIN_MEMORY_SIZE_KB: u32 = 1024;
|
||||||
|
const PAGE_SIZE_KB: u32 = 64;
|
||||||
|
|
||||||
|
let memory = builder::MemoryBuilder::new()
|
||||||
|
.with_min(MIN_MEMORY_SIZE_KB / PAGE_SIZE_KB)
|
||||||
|
.build();
|
||||||
|
backend.builder.push_memory(memory);
|
||||||
|
|
||||||
|
let memory_export = builder::export()
|
||||||
|
.field("memory")
|
||||||
|
.with_internal(Internal::Memory(0))
|
||||||
|
.build();
|
||||||
|
backend.builder.push_export(memory_export);
|
||||||
|
|
||||||
|
let module = backend.builder.build();
|
||||||
|
module
|
||||||
|
.to_bytes()
|
||||||
|
.map_err(|e| -> String { format!("Error serialising Wasm module {:?}", e) })
|
||||||
|
}
|
||||||
|
|
202
compiler/gen_wasm/tests/helpers/eval.rs
Normal file
202
compiler/gen_wasm/tests/helpers/eval.rs
Normal file
|
@ -0,0 +1,202 @@
|
||||||
|
use roc_can::builtins::builtin_defs_map;
|
||||||
|
use roc_collections::all::{MutMap, MutSet};
|
||||||
|
// use roc_std::{RocDec, RocList, RocOrder, RocStr};
|
||||||
|
|
||||||
|
fn promote_expr_to_module(src: &str) -> String {
|
||||||
|
let mut buffer = String::from("app \"test\" provides [ main ] to \"./platform\"\n\nmain =\n");
|
||||||
|
|
||||||
|
for line in src.lines() {
|
||||||
|
// indent the body!
|
||||||
|
buffer.push_str(" ");
|
||||||
|
buffer.push_str(line);
|
||||||
|
buffer.push('\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn helper_wasm<'a>(
|
||||||
|
arena: &'a bumpalo::Bump,
|
||||||
|
src: &str,
|
||||||
|
stdlib: &'a roc_builtins::std::StdLib,
|
||||||
|
_is_gen_test: bool,
|
||||||
|
_ignore_problems: bool,
|
||||||
|
) -> wasmer::Instance {
|
||||||
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
|
let filename = PathBuf::from("Test.roc");
|
||||||
|
let src_dir = Path::new("fake/test/path");
|
||||||
|
|
||||||
|
let module_src;
|
||||||
|
let temp;
|
||||||
|
if src.starts_with("app") {
|
||||||
|
// this is already a module
|
||||||
|
module_src = src;
|
||||||
|
} else {
|
||||||
|
// this is an expression, promote it to a module
|
||||||
|
temp = promote_expr_to_module(src);
|
||||||
|
module_src = &temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
let exposed_types = MutMap::default();
|
||||||
|
let loaded = roc_load::file::load_and_monomorphize_from_str(
|
||||||
|
arena,
|
||||||
|
filename,
|
||||||
|
module_src,
|
||||||
|
stdlib,
|
||||||
|
src_dir,
|
||||||
|
exposed_types,
|
||||||
|
8,
|
||||||
|
builtin_defs_map,
|
||||||
|
);
|
||||||
|
|
||||||
|
let loaded = loaded.expect("failed to load module");
|
||||||
|
|
||||||
|
use roc_load::file::MonomorphizedModule;
|
||||||
|
let MonomorphizedModule {
|
||||||
|
procedures: top_procedures,
|
||||||
|
interns,
|
||||||
|
exposed_to_host,
|
||||||
|
..
|
||||||
|
} = loaded;
|
||||||
|
|
||||||
|
let mut procedures = MutMap::default();
|
||||||
|
|
||||||
|
for (key, proc) in top_procedures {
|
||||||
|
procedures.insert(key, proc);
|
||||||
|
}
|
||||||
|
|
||||||
|
// You can comment and uncomment this block out to get more useful information
|
||||||
|
// while you're working on the wasm backend!
|
||||||
|
// {
|
||||||
|
// println!("=========== Procedures ==========");
|
||||||
|
// println!("{:?}", procedures);
|
||||||
|
// println!("=================================\n");
|
||||||
|
|
||||||
|
// println!("=========== Interns ==========");
|
||||||
|
// println!("{:?}", interns);
|
||||||
|
// println!("=================================\n");
|
||||||
|
|
||||||
|
// println!("=========== Exposed ==========");
|
||||||
|
// println!("{:?}", exposed_to_host);
|
||||||
|
// println!("=================================\n");
|
||||||
|
// }
|
||||||
|
|
||||||
|
let exposed_to_host = exposed_to_host.keys().copied().collect::<MutSet<_>>();
|
||||||
|
|
||||||
|
let env = roc_gen_wasm::Env {
|
||||||
|
arena,
|
||||||
|
interns,
|
||||||
|
exposed_to_host,
|
||||||
|
};
|
||||||
|
|
||||||
|
let module_bytes = roc_gen_wasm::build_module(&env, procedures).unwrap();
|
||||||
|
|
||||||
|
// for debugging (e.g. with wasm2wat)
|
||||||
|
if false {
|
||||||
|
use std::io::Write;
|
||||||
|
let path = "/home/brian/Documents/roc/compiler/gen_wasm/debug.wasm";
|
||||||
|
|
||||||
|
match std::fs::File::create(path) {
|
||||||
|
Err(e) => eprintln!("Problem creating wasm debug file: {:?}", e),
|
||||||
|
Ok(mut file) => {
|
||||||
|
file.write_all(&module_bytes).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// now, do wasmer stuff
|
||||||
|
|
||||||
|
use wasmer::{Instance, Module, Store};
|
||||||
|
|
||||||
|
let store = Store::default();
|
||||||
|
// let module = Module::from_file(&store, &test_wasm_path).unwrap();
|
||||||
|
let module = Module::from_binary(&store, &module_bytes).unwrap();
|
||||||
|
|
||||||
|
// First, we create the `WasiEnv`
|
||||||
|
use wasmer_wasi::WasiState;
|
||||||
|
let mut wasi_env = WasiState::new("hello").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_or_else(|_| wasmer::imports!());
|
||||||
|
|
||||||
|
Instance::new(&module, &import_object).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn assert_wasm_evals_to_help<T>(src: &str, ignore_problems: bool) -> Result<T, String>
|
||||||
|
where
|
||||||
|
T: Copy,
|
||||||
|
{
|
||||||
|
let arena = bumpalo::Bump::new();
|
||||||
|
|
||||||
|
// NOTE the stdlib must be in the arena; just taking a reference will segfault
|
||||||
|
let stdlib = arena.alloc(roc_builtins::std::standard_stdlib());
|
||||||
|
|
||||||
|
let is_gen_test = true;
|
||||||
|
let instance =
|
||||||
|
crate::helpers::eval::helper_wasm(&arena, src, stdlib, is_gen_test, ignore_problems);
|
||||||
|
|
||||||
|
let main_function = instance.exports.get_function("#UserApp_main_1").unwrap();
|
||||||
|
|
||||||
|
match main_function.call(&[]) {
|
||||||
|
Err(e) => Err(format!("{:?}", e)),
|
||||||
|
Ok(result) => {
|
||||||
|
let integer = match result[0] {
|
||||||
|
wasmer::Value::I64(a) => a,
|
||||||
|
wasmer::Value::F64(a) => a.to_bits() as i64,
|
||||||
|
_ => panic!(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let output_ptr: &T;
|
||||||
|
unsafe {
|
||||||
|
output_ptr = std::mem::transmute::<&i64, &T>(&integer);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(*output_ptr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! assert_wasm_evals_to {
|
||||||
|
($src:expr, $expected:expr, $ty:ty, $transform:expr, $ignore_problems:expr) => {
|
||||||
|
match $crate::helpers::eval::assert_wasm_evals_to_help::<$ty>($src, $ignore_problems) {
|
||||||
|
Err(msg) => println!("{:?}", msg),
|
||||||
|
Ok(actual) => {
|
||||||
|
#[allow(clippy::bool_assert_comparison)]
|
||||||
|
assert_eq!($transform(actual), $expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
($src:expr, $expected:expr, $ty:ty) => {
|
||||||
|
$crate::assert_wasm_evals_to!($src, $expected, $ty, $crate::helpers::eval::identity, false);
|
||||||
|
};
|
||||||
|
|
||||||
|
($src:expr, $expected:expr, $ty:ty, $transform:expr) => {
|
||||||
|
$crate::assert_wasm_evals_to!($src, $expected, $ty, $transform, false);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! assert_evals_to {
|
||||||
|
($src:expr, $expected:expr, $ty:ty) => {{
|
||||||
|
assert_evals_to!($src, $expected, $ty, $crate::helpers::eval::identity);
|
||||||
|
}};
|
||||||
|
($src:expr, $expected:expr, $ty:ty, $transform:expr) => {
|
||||||
|
// Same as above, except with an additional transformation argument.
|
||||||
|
{
|
||||||
|
$crate::assert_wasm_evals_to!($src, $expected, $ty, $transform, false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn identity<T>(value: T) -> T {
|
||||||
|
value
|
||||||
|
}
|
44
compiler/gen_wasm/tests/helpers/mod.rs
Normal file
44
compiler/gen_wasm/tests/helpers/mod.rs
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
extern crate bumpalo;
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
pub mod eval;
|
||||||
|
|
||||||
|
/// Used in the with_larger_debug_stack() function, for tests that otherwise
|
||||||
|
/// run out of stack space in debug builds (but don't in --release builds)
|
||||||
|
#[allow(dead_code)]
|
||||||
|
const EXPANDED_STACK_SIZE: usize = 8 * 1024 * 1024;
|
||||||
|
|
||||||
|
/// Without this, some tests pass in `cargo test --release` but fail without
|
||||||
|
/// the --release flag because they run out of stack space. This increases
|
||||||
|
/// stack size for debug builds only, while leaving the stack space at the default
|
||||||
|
/// amount for release builds.
|
||||||
|
#[allow(dead_code)]
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
pub fn with_larger_debug_stack<F>(run_test: F)
|
||||||
|
where
|
||||||
|
F: FnOnce(),
|
||||||
|
F: Send,
|
||||||
|
F: 'static,
|
||||||
|
{
|
||||||
|
std::thread::Builder::new()
|
||||||
|
.stack_size(EXPANDED_STACK_SIZE)
|
||||||
|
.spawn(run_test)
|
||||||
|
.expect("Error while spawning expanded dev stack size thread")
|
||||||
|
.join()
|
||||||
|
.expect("Error while joining expanded dev stack size thread")
|
||||||
|
}
|
||||||
|
|
||||||
|
/// In --release builds, don't increase the stack size. Run the test normally.
|
||||||
|
/// This way, we find out if any of our tests are blowing the stack even after
|
||||||
|
/// optimizations in release builds.
|
||||||
|
#[allow(dead_code)]
|
||||||
|
#[cfg(not(debug_assertions))]
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn with_larger_debug_stack<F>(run_test: F)
|
||||||
|
where
|
||||||
|
F: FnOnce() -> (),
|
||||||
|
F: Send,
|
||||||
|
F: 'static,
|
||||||
|
{
|
||||||
|
run_test()
|
||||||
|
}
|
863
compiler/gen_wasm/tests/wasm_num.rs
Normal file
863
compiler/gen_wasm/tests/wasm_num.rs
Normal file
|
@ -0,0 +1,863 @@
|
||||||
|
#[macro_use]
|
||||||
|
extern crate pretty_assertions;
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
extern crate indoc;
|
||||||
|
|
||||||
|
extern crate bumpalo;
|
||||||
|
extern crate libc;
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
mod helpers;
|
||||||
|
|
||||||
|
#[cfg(all(test, any(target_os = "linux", target_os = "macos"), any(target_arch = "x86_64"/*, target_arch = "aarch64"*/)))]
|
||||||
|
mod dev_num {
|
||||||
|
#[test]
|
||||||
|
fn i64_values() {
|
||||||
|
assert_evals_to!("0", 0, i64);
|
||||||
|
assert_evals_to!("-0", 0, i64);
|
||||||
|
assert_evals_to!("-1", -1, i64);
|
||||||
|
assert_evals_to!("1", 1, i64);
|
||||||
|
assert_evals_to!("9_000_000_000_000", 9_000_000_000_000, i64);
|
||||||
|
assert_evals_to!("-9_000_000_000_000", -9_000_000_000_000, i64);
|
||||||
|
assert_evals_to!("0b1010", 0b1010, i64);
|
||||||
|
assert_evals_to!("0o17", 0o17, i64);
|
||||||
|
assert_evals_to!("0x1000_0000_0000_0000", 0x1000_0000_0000_0000, i64);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn f64_values() {
|
||||||
|
assert_evals_to!("0.0", 0.0, f64);
|
||||||
|
assert_evals_to!("-0.0", 0.0, f64);
|
||||||
|
assert_evals_to!("1.0", 1.0, f64);
|
||||||
|
assert_evals_to!("-1.0", -1.0, f64);
|
||||||
|
assert_evals_to!("3.1415926535897932", 3.141_592_653_589_793, f64);
|
||||||
|
assert_evals_to!(&format!("{:0.1}", f64::MIN), f64::MIN, f64);
|
||||||
|
assert_evals_to!(&format!("{:0.1}", f64::MAX), f64::MAX, f64);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn gen_add_i64() {
|
||||||
|
assert_evals_to!(
|
||||||
|
indoc!(
|
||||||
|
r#"
|
||||||
|
1 + 2 + 3
|
||||||
|
"#
|
||||||
|
),
|
||||||
|
6,
|
||||||
|
i64
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn gen_add_f64() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// 1.1 + 2.4 + 3
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 6.5,
|
||||||
|
// f64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn gen_sub_i64() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// 1 - 2 - 3
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// -4,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn gen_mul_i64() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// 2 * 4 * 6
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 48,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn i64_force_stack() {
|
||||||
|
// This claims 33 registers. One more than Arm and RISC-V, and many more than x86-64.
|
||||||
|
assert_evals_to!(
|
||||||
|
indoc!(
|
||||||
|
r#"
|
||||||
|
a = 0
|
||||||
|
b = 1
|
||||||
|
c = 2
|
||||||
|
d = 3
|
||||||
|
e = 4
|
||||||
|
f = 5
|
||||||
|
g = 6
|
||||||
|
h = 7
|
||||||
|
i = 8
|
||||||
|
j = 9
|
||||||
|
k = 10
|
||||||
|
l = 11
|
||||||
|
m = 12
|
||||||
|
n = 13
|
||||||
|
o = 14
|
||||||
|
p = 15
|
||||||
|
q = 16
|
||||||
|
r = 17
|
||||||
|
s = 18
|
||||||
|
t = 19
|
||||||
|
u = 20
|
||||||
|
v = 21
|
||||||
|
w = 22
|
||||||
|
x = 23
|
||||||
|
y = 24
|
||||||
|
z = 25
|
||||||
|
aa = 26
|
||||||
|
ab = 27
|
||||||
|
ac = 28
|
||||||
|
ad = 29
|
||||||
|
ae = 30
|
||||||
|
af = 31
|
||||||
|
ag = 32
|
||||||
|
|
||||||
|
a + b + c + d + e + f + g + h + i + j + k + l + m + n + o + p + q + r + s + t + u + v + w + x + y + z + aa + ab + ac + ad + ae + af + ag
|
||||||
|
"#
|
||||||
|
),
|
||||||
|
528,
|
||||||
|
i64
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn i64_abs() {
|
||||||
|
// assert_evals_to!("Num.abs -6", 6, i64);
|
||||||
|
// assert_evals_to!("Num.abs 7", 7, i64);
|
||||||
|
// assert_evals_to!("Num.abs 0", 0, i64);
|
||||||
|
// assert_evals_to!("Num.abs -0", 0, i64);
|
||||||
|
// assert_evals_to!("Num.abs -1", 1, i64);
|
||||||
|
// assert_evals_to!("Num.abs 1", 1, i64);
|
||||||
|
// assert_evals_to!("Num.abs 9_000_000_000_000", 9_000_000_000_000, i64);
|
||||||
|
// assert_evals_to!("Num.abs -9_000_000_000_000", 9_000_000_000_000, i64);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn gen_int_eq() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// 4 == 4
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// true,
|
||||||
|
// bool
|
||||||
|
// );
|
||||||
|
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// 3 == 4
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// false,
|
||||||
|
// bool
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn gen_basic_fn() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// always42 : Num.Num (Num.Integer Num.Signed64) -> Num.Num (Num.Integer Num.Signed64)
|
||||||
|
// always42 = \_ -> 42
|
||||||
|
|
||||||
|
// always42 5
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 42,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn gen_wrap_add_nums() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// add2 = \num1, num2 -> num1 + num2
|
||||||
|
|
||||||
|
// add2 4 5
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 9,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn gen_wrap_add_nums_force_stack() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// add9 = \num1, num2, num3, num4, num5, num6, num7, num8, num9 -> num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9
|
||||||
|
|
||||||
|
// add9 1 2 3 4 5 6 7 8 9
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 45,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn pow_int() {
|
||||||
|
// assert_evals_to!("Num.powInt 2 3", 8, i64);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn acos() {
|
||||||
|
// assert_evals_to!("Num.acos 0.5", 1.0471975511965979, f64);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn asin() {
|
||||||
|
// assert_evals_to!("Num.asin 0.5", 0.5235987755982989, f64);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn atan() {
|
||||||
|
// assert_evals_to!("Num.atan 10", 1.4711276743037347, f64);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn gen_if_fn() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// limitedNegate = \num ->
|
||||||
|
// x =
|
||||||
|
// if num == 1 then
|
||||||
|
// -1
|
||||||
|
// else if num == -1 then
|
||||||
|
// 1
|
||||||
|
// else
|
||||||
|
// num
|
||||||
|
// x
|
||||||
|
|
||||||
|
// limitedNegate 1
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// -1,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn gen_fib_fn() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// fib = \n ->
|
||||||
|
// if n == 0 then
|
||||||
|
// 0
|
||||||
|
// else if n == 1 then
|
||||||
|
// 1
|
||||||
|
// else
|
||||||
|
// (fib (n - 1)) + (fib (n - 2))
|
||||||
|
|
||||||
|
// fib 10
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 55,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn f64_abs() {
|
||||||
|
// assert_evals_to!("Num.abs -4.7", 4.7, f64);
|
||||||
|
// assert_evals_to!("Num.abs 5.8", 5.8, f64);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn f64_round() {
|
||||||
|
// assert_evals_to!("Num.round 3.6", 4, i64);
|
||||||
|
// assert_evals_to!("Num.round 3.4", 3, i64);
|
||||||
|
// assert_evals_to!("Num.round 2.5", 3, i64);
|
||||||
|
// assert_evals_to!("Num.round -2.3", -2, i64);
|
||||||
|
// assert_evals_to!("Num.round -2.5", -3, i64);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn f64_sqrt() {
|
||||||
|
// // FIXME this works with normal types, but fails when checking uniqueness types
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// when Num.sqrt 100 is
|
||||||
|
// Ok val -> val
|
||||||
|
// Err _ -> -1
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 10.0,
|
||||||
|
// f64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn gen_float_eq() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// 1.0 == 1.0
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// true,
|
||||||
|
// bool
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn gen_div_f64() {
|
||||||
|
// // FIXME this works with normal types, but fails when checking uniqueness types
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// when 48 / 2 is
|
||||||
|
// Ok val -> val
|
||||||
|
// Err _ -> -1
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 24.0,
|
||||||
|
// f64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn gen_int_neq() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// 4 != 5
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// true,
|
||||||
|
// bool
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn gen_wrap_int_neq() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// wrappedNotEq : a, a -> Bool
|
||||||
|
// wrappedNotEq = \num1, num2 ->
|
||||||
|
// num1 != num2
|
||||||
|
|
||||||
|
// wrappedNotEq 2 3
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// true,
|
||||||
|
// bool
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn gen_sub_f64() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// 1.5 - 2.4 - 3
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// -3.9,
|
||||||
|
// f64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn gen_div_i64() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// when 1000 // 10 is
|
||||||
|
// Ok val -> val
|
||||||
|
// Err _ -> -1
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 100,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn gen_div_by_zero_i64() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// when 1000 // 0 is
|
||||||
|
// Err DivByZero -> 99
|
||||||
|
// _ -> -24
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 99,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn gen_rem_i64() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// when Num.rem 8 3 is
|
||||||
|
// Ok val -> val
|
||||||
|
// Err _ -> -1
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 2,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn gen_rem_div_by_zero_i64() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// when Num.rem 8 0 is
|
||||||
|
// Err DivByZero -> 4
|
||||||
|
// Ok _ -> -23
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 4,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn gen_is_zero_i64() {
|
||||||
|
// assert_evals_to!("Num.isZero 0", true, bool);
|
||||||
|
// assert_evals_to!("Num.isZero 1", false, bool);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn gen_is_positive_i64() {
|
||||||
|
// assert_evals_to!("Num.isPositive 0", false, bool);
|
||||||
|
// assert_evals_to!("Num.isPositive 1", true, bool);
|
||||||
|
// assert_evals_to!("Num.isPositive -5", false, bool);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn gen_is_negative_i64() {
|
||||||
|
// assert_evals_to!("Num.isNegative 0", false, bool);
|
||||||
|
// assert_evals_to!("Num.isNegative 3", false, bool);
|
||||||
|
// assert_evals_to!("Num.isNegative -2", true, bool);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn gen_is_positive_f64() {
|
||||||
|
// assert_evals_to!("Num.isPositive 0.0", false, bool);
|
||||||
|
// assert_evals_to!("Num.isPositive 4.7", true, bool);
|
||||||
|
// assert_evals_to!("Num.isPositive -8.5", false, bool);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn gen_is_negative_f64() {
|
||||||
|
// assert_evals_to!("Num.isNegative 0.0", false, bool);
|
||||||
|
// assert_evals_to!("Num.isNegative 9.9", false, bool);
|
||||||
|
// assert_evals_to!("Num.isNegative -4.4", true, bool);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn gen_is_zero_f64() {
|
||||||
|
// assert_evals_to!("Num.isZero 0", true, bool);
|
||||||
|
// assert_evals_to!("Num.isZero 0_0", true, bool);
|
||||||
|
// assert_evals_to!("Num.isZero 0.0", true, bool);
|
||||||
|
// assert_evals_to!("Num.isZero 1", false, bool);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn gen_is_odd() {
|
||||||
|
// assert_evals_to!("Num.isOdd 4", false, bool);
|
||||||
|
// assert_evals_to!("Num.isOdd 5", true, bool);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn gen_is_even() {
|
||||||
|
// assert_evals_to!("Num.isEven 6", true, bool);
|
||||||
|
// assert_evals_to!("Num.isEven 7", false, bool);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn sin() {
|
||||||
|
// assert_evals_to!("Num.sin 0", 0.0, f64);
|
||||||
|
// assert_evals_to!("Num.sin 1.41421356237", 0.9877659459922529, f64);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn cos() {
|
||||||
|
// assert_evals_to!("Num.cos 0", 1.0, f64);
|
||||||
|
// assert_evals_to!("Num.cos 3.14159265359", -1.0, f64);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn tan() {
|
||||||
|
// assert_evals_to!("Num.tan 0", 0.0, f64);
|
||||||
|
// assert_evals_to!("Num.tan 1", 1.557407724654902, f64);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn lt_i64() {
|
||||||
|
// assert_evals_to!("1 < 2", true, bool);
|
||||||
|
// assert_evals_to!("1 < 1", false, bool);
|
||||||
|
// assert_evals_to!("2 < 1", false, bool);
|
||||||
|
// assert_evals_to!("0 < 0", false, bool);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn lte_i64() {
|
||||||
|
// assert_evals_to!("1 <= 1", true, bool);
|
||||||
|
// assert_evals_to!("2 <= 1", false, bool);
|
||||||
|
// assert_evals_to!("1 <= 2", true, bool);
|
||||||
|
// assert_evals_to!("0 <= 0", true, bool);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn gt_i64() {
|
||||||
|
// assert_evals_to!("2 > 1", true, bool);
|
||||||
|
// assert_evals_to!("2 > 2", false, bool);
|
||||||
|
// assert_evals_to!("1 > 1", false, bool);
|
||||||
|
// assert_evals_to!("0 > 0", false, bool);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn gte_i64() {
|
||||||
|
// assert_evals_to!("1 >= 1", true, bool);
|
||||||
|
// assert_evals_to!("1 >= 2", false, bool);
|
||||||
|
// assert_evals_to!("2 >= 1", true, bool);
|
||||||
|
// assert_evals_to!("0 >= 0", true, bool);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn lt_f64() {
|
||||||
|
// assert_evals_to!("1.1 < 1.2", true, bool);
|
||||||
|
// assert_evals_to!("1.1 < 1.1", false, bool);
|
||||||
|
// assert_evals_to!("1.2 < 1.1", false, bool);
|
||||||
|
// assert_evals_to!("0.0 < 0.0", false, bool);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn lte_f64() {
|
||||||
|
// assert_evals_to!("1.1 <= 1.1", true, bool);
|
||||||
|
// assert_evals_to!("1.2 <= 1.1", false, bool);
|
||||||
|
// assert_evals_to!("1.1 <= 1.2", true, bool);
|
||||||
|
// assert_evals_to!("0.0 <= 0.0", true, bool);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn gt_f64() {
|
||||||
|
// assert_evals_to!("2.2 > 1.1", true, bool);
|
||||||
|
// assert_evals_to!("2.2 > 2.2", false, bool);
|
||||||
|
// assert_evals_to!("1.1 > 2.2", false, bool);
|
||||||
|
// assert_evals_to!("0.0 > 0.0", false, bool);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn gte_f64() {
|
||||||
|
// assert_evals_to!("1.1 >= 1.1", true, bool);
|
||||||
|
// assert_evals_to!("1.1 >= 1.2", false, bool);
|
||||||
|
// assert_evals_to!("1.2 >= 1.1", true, bool);
|
||||||
|
// assert_evals_to!("0.0 >= 0.0", true, bool);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn gen_order_of_arithmetic_ops() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// 1 + 3 * 7 - 2
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 20,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn gen_order_of_arithmetic_ops_complex_float() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// 3 - 48 * 2.0
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// -93.0,
|
||||||
|
// f64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn if_guard_bind_variable_false() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// wrapper = \{} ->
|
||||||
|
// when 10 is
|
||||||
|
// x if x == 5 -> 0
|
||||||
|
// _ -> 42
|
||||||
|
|
||||||
|
// wrapper {}
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 42,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn if_guard_bind_variable_true() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// wrapper = \{} ->
|
||||||
|
// when 10 is
|
||||||
|
// x if x == 10 -> 42
|
||||||
|
// _ -> 0
|
||||||
|
|
||||||
|
// wrapper {}
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 42,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn tail_call_elimination() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// sum = \n, accum ->
|
||||||
|
// when n is
|
||||||
|
// 0 -> accum
|
||||||
|
// _ -> sum (n - 1) (n + accum)
|
||||||
|
|
||||||
|
// sum 1_000_000 0
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 500000500000,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn int_negate() {
|
||||||
|
// assert_evals_to!("Num.neg 123", -123, i64);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn gen_wrap_int_neg() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// wrappedNeg = \num -> -num
|
||||||
|
|
||||||
|
// wrappedNeg 3
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// -3,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn int_to_float() {
|
||||||
|
// assert_evals_to!("Num.toFloat 0x9", 9.0, f64);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn num_to_float() {
|
||||||
|
// assert_evals_to!("Num.toFloat 9", 9.0, f64);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn float_to_float() {
|
||||||
|
// assert_evals_to!("Num.toFloat 0.5", 0.5, f64);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn int_compare() {
|
||||||
|
// assert_evals_to!("Num.compare 0 1", RocOrder::Lt, RocOrder);
|
||||||
|
// assert_evals_to!("Num.compare 1 1", RocOrder::Eq, RocOrder);
|
||||||
|
// assert_evals_to!("Num.compare 1 0", RocOrder::Gt, RocOrder);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn float_compare() {
|
||||||
|
// assert_evals_to!("Num.compare 0.01 3.14", RocOrder::Lt, RocOrder);
|
||||||
|
// assert_evals_to!("Num.compare 3.14 3.14", RocOrder::Eq, RocOrder);
|
||||||
|
// assert_evals_to!("Num.compare 3.14 0.01", RocOrder::Gt, RocOrder);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn pow() {
|
||||||
|
// assert_evals_to!("Num.pow 2.0 2.0", 4.0, f64);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn ceiling() {
|
||||||
|
// assert_evals_to!("Num.ceiling 1.1", 2, i64);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn floor() {
|
||||||
|
// assert_evals_to!("Num.floor 1.9", 1, i64);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // #[test]
|
||||||
|
// // #[should_panic(expected = r#"Roc failed with message: "integer addition overflowed!"#)]
|
||||||
|
// // fn int_overflow() {
|
||||||
|
// // assert_evals_to!(
|
||||||
|
// // indoc!(
|
||||||
|
// // r#"
|
||||||
|
// // 9_223_372_036_854_775_807 + 1
|
||||||
|
// // "#
|
||||||
|
// // ),
|
||||||
|
// // 0,
|
||||||
|
// // i64
|
||||||
|
// // );
|
||||||
|
// // }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn int_add_checked() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// when Num.addChecked 1 2 is
|
||||||
|
// Ok v -> v
|
||||||
|
// _ -> -1
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 3,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// when Num.addChecked 9_223_372_036_854_775_807 1 is
|
||||||
|
// Err Overflow -> -1
|
||||||
|
// Ok v -> v
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// -1,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn int_add_wrap() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// Num.addWrap 9_223_372_036_854_775_807 1
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// std::i64::MIN,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn float_add_checked_pass() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// when Num.addChecked 1.0 0.0 is
|
||||||
|
// Ok v -> v
|
||||||
|
// Err Overflow -> -1.0
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 1.0,
|
||||||
|
// f64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn float_add_checked_fail() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// when Num.addChecked 1.7976931348623157e308 1.7976931348623157e308 is
|
||||||
|
// Err Overflow -> -1
|
||||||
|
// Ok v -> v
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// -1.0,
|
||||||
|
// f64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // #[test]
|
||||||
|
// // #[should_panic(expected = r#"Roc failed with message: "float addition overflowed!"#)]
|
||||||
|
// // fn float_overflow() {
|
||||||
|
// // assert_evals_to!(
|
||||||
|
// // indoc!(
|
||||||
|
// // r#"
|
||||||
|
// // 1.7976931348623157e308 + 1.7976931348623157e308
|
||||||
|
// // "#
|
||||||
|
// // ),
|
||||||
|
// // 0.0,
|
||||||
|
// // f64
|
||||||
|
// // );
|
||||||
|
// // }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn max_i128() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// Num.maxI128
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// i128::MAX,
|
||||||
|
// i128
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn num_max_int() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// Num.maxInt
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// i64::MAX,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn num_min_int() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// Num.minInt
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// i64::MIN,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
}
|
937
compiler/gen_wasm/tests/wasm_records.rs
Normal file
937
compiler/gen_wasm/tests/wasm_records.rs
Normal file
|
@ -0,0 +1,937 @@
|
||||||
|
#[macro_use]
|
||||||
|
extern crate indoc;
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
mod helpers;
|
||||||
|
|
||||||
|
#[cfg(all(test, target_os = "linux", any(target_arch = "x86_64"/*, target_arch = "aarch64"*/)))]
|
||||||
|
mod dev_records {
|
||||||
|
// #[test]
|
||||||
|
// fn basic_record() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// { y: 17, x: 15, z: 19 }.x
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 15,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
//
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// { x: 15, y: 17, z: 19 }.y
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 17,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
//
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// { x: 15, y: 17, z: 19 }.z
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 19,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// #[test]
|
||||||
|
// fn nested_record() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// { x: 15, y: { a: 12, b: 15, c: 2}, z: 19 }.x
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 15,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
//
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// { x: 15, y: { a: 12, b: 15, c: 2}, z: 19 }.y.a
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 12,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
//
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// { x: 15, y: { a: 12, b: 15, c: 2}, z: 19 }.y.b
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 15,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
//
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// { x: 15, y: { a: 12, b: 15, c: 2}, z: 19 }.y.c
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 2,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
//
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// { x: 15, y: { a: 12, b: 15, c: 2}, z: 19 }.z
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 19,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// #[test]
|
||||||
|
// fn f64_record() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// rec = { y: 17.2, x: 15.1, z: 19.3 }
|
||||||
|
//
|
||||||
|
// rec.x
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 15.1,
|
||||||
|
// f64
|
||||||
|
// );
|
||||||
|
//
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// rec = { y: 17.2, x: 15.1, z: 19.3 }
|
||||||
|
//
|
||||||
|
// rec.y
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 17.2,
|
||||||
|
// f64
|
||||||
|
// );
|
||||||
|
//
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// rec = { y: 17.2, x: 15.1, z: 19.3 }
|
||||||
|
//
|
||||||
|
// rec.z
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 19.3,
|
||||||
|
// f64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn fn_record() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// getRec = \x -> { y: 17, x, z: 19 }
|
||||||
|
|
||||||
|
// (getRec 15).x
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 15,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// rec = { x: 15, y: 17, z: 19 }
|
||||||
|
|
||||||
|
// rec.y
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 17,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// rec = { x: 15, y: 17, z: 19 }
|
||||||
|
|
||||||
|
// rec.z
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 19,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// rec = { x: 15, y: 17, z: 19 }
|
||||||
|
|
||||||
|
// rec.z + rec.x
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 34,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn def_record() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// rec = { y: 17, x: 15, z: 19 }
|
||||||
|
//
|
||||||
|
// rec.x
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 15,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
//
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// rec = { x: 15, y: 17, z: 19 }
|
||||||
|
//
|
||||||
|
// rec.y
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 17,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
//
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// rec = { x: 15, y: 17, z: 19 }
|
||||||
|
//
|
||||||
|
// rec.z
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 19,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// #[test]
|
||||||
|
// fn when_on_record() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// when { x: 0x2 } is
|
||||||
|
// { x } -> x + 3
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 5,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// #[test]
|
||||||
|
// fn when_record_with_guard_pattern() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// when { x: 0x2, y: 3.14 } is
|
||||||
|
// { x: var } -> var + 3
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 5,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// #[test]
|
||||||
|
// fn let_with_record_pattern() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// { x } = { x: 0x2, y: 3.14 }
|
||||||
|
//
|
||||||
|
// x
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 2,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// #[test]
|
||||||
|
// fn record_guard_pattern() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// when { x: 0x2, y: 3.14 } is
|
||||||
|
// { x: 0x4 } -> 5
|
||||||
|
// { x } -> x + 3
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 5,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// #[test]
|
||||||
|
// fn twice_record_access() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// x = {a: 0x2, b: 0x3 }
|
||||||
|
//
|
||||||
|
// x.a + x.b
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 5,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
// #[test]
|
||||||
|
// fn empty_record() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// v = {}
|
||||||
|
//
|
||||||
|
// v
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// (),
|
||||||
|
// ()
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// #[test]
|
||||||
|
// fn i64_record1_literal() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// { x: 3 }
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 3,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn i64_record2_literal() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// { x: 3, y: 5 }
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// (3, 5),
|
||||||
|
// (i64, i64)
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // #[test]
|
||||||
|
// // fn i64_record3_literal() {
|
||||||
|
// // assert_evals_to!(
|
||||||
|
// // indoc!(
|
||||||
|
// // r#"
|
||||||
|
// // { x: 3, y: 5, z: 17 }
|
||||||
|
// // "#
|
||||||
|
// // ),
|
||||||
|
// // (3, 5, 17),
|
||||||
|
// // (i64, i64, i64)
|
||||||
|
// // );
|
||||||
|
// // }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn f64_record2_literal() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// { x: 3.1, y: 5.1 }
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// (3.1, 5.1),
|
||||||
|
// (f64, f64)
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // #[test]
|
||||||
|
// // fn f64_record3_literal() {
|
||||||
|
// // assert_evals_to!(
|
||||||
|
// // indoc!(
|
||||||
|
// // r#"
|
||||||
|
// // { x: 3.1, y: 5.1, z: 17.1 }
|
||||||
|
// // "#
|
||||||
|
// // ),
|
||||||
|
// // (3.1, 5.1, 17.1),
|
||||||
|
// // (f64, f64, f64)
|
||||||
|
// // );
|
||||||
|
// // }
|
||||||
|
|
||||||
|
// // #[test]
|
||||||
|
// // fn bool_record4_literal() {
|
||||||
|
// // assert_evals_to!(
|
||||||
|
// // indoc!(
|
||||||
|
// // r#"
|
||||||
|
// // record : { a : Bool, b : Bool, c : Bool, d : Bool }
|
||||||
|
// // record = { a: True, b: True, c : True, d : Bool }
|
||||||
|
|
||||||
|
// // record
|
||||||
|
// // "#
|
||||||
|
// // ),
|
||||||
|
// // (true, false, false, true),
|
||||||
|
// // (bool, bool, bool, bool)
|
||||||
|
// // );
|
||||||
|
// // }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn i64_record1_literal() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// { a: 3 }
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 3,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // #[test]
|
||||||
|
// // fn i64_record9_literal() {
|
||||||
|
// // assert_evals_to!(
|
||||||
|
// // indoc!(
|
||||||
|
// // r#"
|
||||||
|
// // { a: 3, b: 5, c: 17, d: 1, e: 9, f: 12, g: 13, h: 14, i: 15 }
|
||||||
|
// // "#
|
||||||
|
// // ),
|
||||||
|
// // (3, 5, 17, 1, 9, 12, 13, 14, 15),
|
||||||
|
// // (i64, i64, i64, i64, i64, i64, i64, i64, i64)
|
||||||
|
// // );
|
||||||
|
// // }
|
||||||
|
|
||||||
|
// // #[test]
|
||||||
|
// // fn f64_record3_literal() {
|
||||||
|
// // assert_evals_to!(
|
||||||
|
// // indoc!(
|
||||||
|
// // r#"
|
||||||
|
// // { x: 3.1, y: 5.1, z: 17.1 }
|
||||||
|
// // "#
|
||||||
|
// // ),
|
||||||
|
// // (3.1, 5.1, 17.1),
|
||||||
|
// // (f64, f64, f64)
|
||||||
|
// // );
|
||||||
|
// // }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn bool_literal() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// x : Bool
|
||||||
|
// x = True
|
||||||
|
|
||||||
|
// x
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// true,
|
||||||
|
// bool
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn optional_field_when_use_default() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// app "test" provides [ main ] to "./platform"
|
||||||
|
|
||||||
|
// f = \r ->
|
||||||
|
// when r is
|
||||||
|
// { x: Blue, y ? 3 } -> y
|
||||||
|
// { x: Red, y ? 5 } -> y
|
||||||
|
|
||||||
|
// main =
|
||||||
|
// a = f { x: Blue, y: 7 }
|
||||||
|
// b = f { x: Blue }
|
||||||
|
// c = f { x: Red, y: 11 }
|
||||||
|
// d = f { x: Red }
|
||||||
|
|
||||||
|
// a * b * c * d
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 3 * 5 * 7 * 11,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn optional_field_when_use_default_nested() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// f = \r ->
|
||||||
|
// when r is
|
||||||
|
// { x: Blue, y ? 3 } -> y
|
||||||
|
// { x: Red, y ? 5 } -> y
|
||||||
|
|
||||||
|
// a = f { x: Blue, y: 7 }
|
||||||
|
// b = f { x: Blue }
|
||||||
|
// c = f { x: Red, y: 11 }
|
||||||
|
// d = f { x: Red }
|
||||||
|
|
||||||
|
// a * b * c * d
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 3 * 5 * 7 * 11,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn optional_field_when_no_use_default() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// app "test" provides [ main ] to "./platform"
|
||||||
|
|
||||||
|
// f = \r ->
|
||||||
|
// { x ? 10, y } = r
|
||||||
|
// x + y
|
||||||
|
|
||||||
|
// main =
|
||||||
|
// f { x: 4, y: 9 }
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 13,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn optional_field_when_no_use_default_nested() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// f = \r ->
|
||||||
|
// { x ? 10, y } = r
|
||||||
|
// x + y
|
||||||
|
|
||||||
|
// f { x: 4, y: 9 }
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 13,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn optional_field_let_use_default() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// app "test" provides [ main ] to "./platform"
|
||||||
|
|
||||||
|
// f = \r ->
|
||||||
|
// { x ? 10, y } = r
|
||||||
|
// x + y
|
||||||
|
|
||||||
|
// main =
|
||||||
|
// f { y: 9 }
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 19,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn optional_field_let_no_use_default() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// app "test" provides [ main ] to "./platform"
|
||||||
|
|
||||||
|
// f = \r ->
|
||||||
|
// { x ? 10, y } = r
|
||||||
|
// x + y
|
||||||
|
|
||||||
|
// main =
|
||||||
|
// f { x: 4, y: 9 }
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 13,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn optional_field_let_no_use_default_nested() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// f = \r ->
|
||||||
|
// { x ? 10, y } = r
|
||||||
|
// x + y
|
||||||
|
|
||||||
|
// f { x: 4, y: 9 }
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 13,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn optional_field_function_use_default() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// f = \{ x ? 10, y } -> x + y
|
||||||
|
|
||||||
|
// f { y: 9 }
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 19,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// #[ignore]
|
||||||
|
// fn optional_field_function_no_use_default() {
|
||||||
|
// // blocked on https://github.com/rtfeldman/roc/issues/786
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// app "test" provides [ main ] to "./platform"
|
||||||
|
|
||||||
|
// f = \{ x ? 10, y } -> x + y
|
||||||
|
|
||||||
|
// main =
|
||||||
|
// f { x: 4, y: 9 }
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 13,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// #[ignore]
|
||||||
|
// fn optional_field_function_no_use_default_nested() {
|
||||||
|
// // blocked on https://github.com/rtfeldman/roc/issues/786
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// f = \{ x ? 10, y } -> x + y
|
||||||
|
|
||||||
|
// f { x: 4, y: 9 }
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 13,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn optional_field_singleton_record() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// when { x : 4 } is
|
||||||
|
// { x ? 3 } -> x
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 4,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn optional_field_empty_record() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// when { } is
|
||||||
|
// { x ? 3 } -> x
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 3,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn return_record_2() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// { x: 3, y: 5 }
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// [3, 5],
|
||||||
|
// [i64; 2]
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn return_record_3() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// { x: 3, y: 5, z: 4 }
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// (3, 5, 4),
|
||||||
|
// (i64, i64, i64)
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn return_record_4() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// { a: 3, b: 5, c: 4, d: 2 }
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// [3, 5, 4, 2],
|
||||||
|
// [i64; 4]
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn return_record_5() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// { a: 3, b: 5, c: 4, d: 2, e: 1 }
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// [3, 5, 4, 2, 1],
|
||||||
|
// [i64; 5]
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn return_record_6() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// { a: 3, b: 5, c: 4, d: 2, e: 1, f: 7 }
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// [3, 5, 4, 2, 1, 7],
|
||||||
|
// [i64; 6]
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn return_record_7() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// { a: 3, b: 5, c: 4, d: 2, e: 1, f: 7, g: 8 }
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// [3, 5, 4, 2, 1, 7, 8],
|
||||||
|
// [i64; 7]
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn return_record_float_int() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// { a: 3.14, b: 0x1 }
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// (3.14, 0x1),
|
||||||
|
// (f64, i64)
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn return_record_int_float() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// { a: 0x1, b: 3.14 }
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// (0x1, 3.14),
|
||||||
|
// (i64, f64)
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn return_record_float_float() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// { a: 6.28, b: 3.14 }
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// (6.28, 3.14),
|
||||||
|
// (f64, f64)
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn return_record_float_float_float() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// { a: 6.28, b: 3.14, c: 0.1 }
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// (6.28, 3.14, 0.1),
|
||||||
|
// (f64, f64, f64)
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn return_nested_record() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// { flag: 0x0, payload: { a: 6.28, b: 3.14, c: 0.1 } }
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// (0x0, (6.28, 3.14, 0.1)),
|
||||||
|
// (i64, (f64, f64, f64))
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn accessor() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// .foo { foo: 4 } + .foo { bar: 6.28, foo: 3 }
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 7,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn accessor_single_element_record() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// .foo { foo: 4 }
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 4,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn update_record() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// rec = { foo: 42, bar: 6 }
|
||||||
|
|
||||||
|
// { rec & foo: rec.foo + 1 }
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// (6, 43),
|
||||||
|
// (i64, i64)
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn update_single_element_record() {
|
||||||
|
assert_evals_to!(
|
||||||
|
indoc!(
|
||||||
|
r#"
|
||||||
|
rec = { foo: 42}
|
||||||
|
|
||||||
|
{ rec & foo: rec.foo + 1 }
|
||||||
|
"#
|
||||||
|
),
|
||||||
|
43,
|
||||||
|
i64
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn booleans_in_record() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!("{ x: 1 == 1, y: 1 == 1 }"),
|
||||||
|
// (true, true),
|
||||||
|
// (bool, bool)
|
||||||
|
// );
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!("{ x: 1 != 1, y: 1 == 1 }"),
|
||||||
|
// (false, true),
|
||||||
|
// (bool, bool)
|
||||||
|
// );
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!("{ x: 1 == 1, y: 1 != 1 }"),
|
||||||
|
// (true, false),
|
||||||
|
// (bool, bool)
|
||||||
|
// );
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!("{ x: 1 != 1, y: 1 != 1 }"),
|
||||||
|
// (false, false),
|
||||||
|
// (bool, bool)
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn alignment_in_record() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!("{ c: 32, b: if True then Red else if True then Green else Blue, a: 1 == 1 }"),
|
||||||
|
// (32i64, true, 2u8),
|
||||||
|
// (i64, bool, u8)
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn blue_and_present() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// f = \r ->
|
||||||
|
// when r is
|
||||||
|
// { x: Blue, y ? 3 } -> y
|
||||||
|
// { x: Red, y ? 5 } -> y
|
||||||
|
|
||||||
|
// f { x: Blue, y: 7 }
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 7,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn blue_and_absent() {
|
||||||
|
// assert_evals_to!(
|
||||||
|
// indoc!(
|
||||||
|
// r#"
|
||||||
|
// f = \r ->
|
||||||
|
// when r is
|
||||||
|
// { x: Blue, y ? 3 } -> y
|
||||||
|
// { x: Red, y ? 5 } -> y
|
||||||
|
|
||||||
|
// f { x: Blue }
|
||||||
|
// "#
|
||||||
|
// ),
|
||||||
|
// 3,
|
||||||
|
// i64
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
}
|
|
@ -1151,10 +1151,15 @@ impl<'a> Builtin<'a> {
|
||||||
Float64 => align_of::<f64>() as u32,
|
Float64 => align_of::<f64>() as u32,
|
||||||
Float32 => align_of::<f32>() as u32,
|
Float32 => align_of::<f32>() as u32,
|
||||||
Float16 => align_of::<i16>() as u32,
|
Float16 => align_of::<i16>() as u32,
|
||||||
Str | EmptyStr => pointer_size,
|
|
||||||
Dict(_, _) | EmptyDict => pointer_size,
|
Dict(_, _) | EmptyDict => pointer_size,
|
||||||
Set(_) | EmptySet => pointer_size,
|
Set(_) | EmptySet => pointer_size,
|
||||||
List(_) | EmptyList => pointer_size,
|
// we often treat these as i128 (64-bit systems)
|
||||||
|
// or i64 (32-bit systems).
|
||||||
|
//
|
||||||
|
// In webassembly, For that to be safe
|
||||||
|
// they must be aligned to allow such access
|
||||||
|
List(_) | EmptyList => pointer_size.max(8),
|
||||||
|
Str | EmptyStr => pointer_size.max(8),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1240,9 +1245,10 @@ impl<'a> Builtin<'a> {
|
||||||
Builtin::Str => pointer_size,
|
Builtin::Str => pointer_size,
|
||||||
Builtin::Dict(k, v) => k
|
Builtin::Dict(k, v) => k
|
||||||
.alignment_bytes(pointer_size)
|
.alignment_bytes(pointer_size)
|
||||||
.max(v.alignment_bytes(pointer_size)),
|
.max(v.alignment_bytes(pointer_size))
|
||||||
Builtin::Set(k) => k.alignment_bytes(pointer_size),
|
.max(pointer_size),
|
||||||
Builtin::List(e) => e.alignment_bytes(pointer_size),
|
Builtin::Set(k) => k.alignment_bytes(pointer_size).max(pointer_size),
|
||||||
|
Builtin::List(e) => e.alignment_bytes(pointer_size).max(pointer_size),
|
||||||
Builtin::EmptyStr | Builtin::EmptyList | Builtin::EmptyDict | Builtin::EmptySet => {
|
Builtin::EmptyStr | Builtin::EmptyList | Builtin::EmptyDict | Builtin::EmptySet => {
|
||||||
unreachable!("not heap-allocated")
|
unreachable!("not heap-allocated")
|
||||||
}
|
}
|
||||||
|
|
|
@ -366,7 +366,7 @@ pub fn helper_wasm<'a>(
|
||||||
|
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
|
|
||||||
Command::new("/home/folkertdev/Downloads/zig-linux-x86_64-0.9.0-dev.848+d5ef5da59/zig")
|
Command::new("zig9")
|
||||||
.current_dir(dir_path)
|
.current_dir(dir_path)
|
||||||
.args(&[
|
.args(&[
|
||||||
"wasm-ld",
|
"wasm-ld",
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue