mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-03 08:34:33 +00:00
Merge branch 'trunk' of github.com:rtfeldman/roc into single-quote-literal
This commit is contained in:
commit
555478cdf0
108 changed files with 7511 additions and 1611 deletions
|
@ -20,6 +20,8 @@ roc_solve = { path = "../solve" }
|
|||
roc_mono = { path = "../mono" }
|
||||
roc_load = { path = "../load" }
|
||||
roc_gen_llvm = { path = "../gen_llvm", optional = true }
|
||||
roc_gen_wasm = { path = "../gen_wasm" }
|
||||
roc_gen_dev = { path = "../gen_dev" }
|
||||
roc_reporting = { path = "../reporting" }
|
||||
roc_std = { path = "../../roc_std" }
|
||||
im = "14" # im and im-rc should always have the same version!
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
use crate::target::arch_str;
|
||||
#[cfg(feature = "llvm")]
|
||||
use libloading::{Error, Library};
|
||||
use roc_builtins::bitcode;
|
||||
#[cfg(feature = "llvm")]
|
||||
use roc_mono::ir::OptLevel;
|
||||
use std::collections::HashMap;
|
||||
|
@ -56,7 +57,7 @@ fn find_zig_str_path() -> PathBuf {
|
|||
return zig_str_path;
|
||||
}
|
||||
|
||||
panic!("cannot find `str.zig`")
|
||||
panic!("cannot find `str.zig`. Launch me from either the root of the roc repo or one level down(roc/examples, roc/cli...)")
|
||||
}
|
||||
|
||||
fn find_wasi_libc_path() -> PathBuf {
|
||||
|
@ -76,6 +77,7 @@ fn find_wasi_libc_path() -> PathBuf {
|
|||
}
|
||||
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn build_zig_host_native(
|
||||
env_path: &str,
|
||||
env_home: &str,
|
||||
|
@ -83,33 +85,48 @@ pub fn build_zig_host_native(
|
|||
zig_host_src: &str,
|
||||
zig_str_path: &str,
|
||||
target: &str,
|
||||
opt_level: OptLevel,
|
||||
shared_lib_path: Option<&Path>,
|
||||
) -> Output {
|
||||
Command::new("zig")
|
||||
let mut command = Command::new("zig");
|
||||
command
|
||||
.env_clear()
|
||||
.env("PATH", env_path)
|
||||
.env("HOME", env_home)
|
||||
.args(&[
|
||||
"build-obj",
|
||||
zig_host_src,
|
||||
emit_bin,
|
||||
"--pkg-begin",
|
||||
"str",
|
||||
zig_str_path,
|
||||
"--pkg-end",
|
||||
// include the zig runtime
|
||||
"-fcompiler-rt",
|
||||
// include libc
|
||||
"--library",
|
||||
"c",
|
||||
// cross-compile?
|
||||
"-target",
|
||||
target,
|
||||
])
|
||||
.output()
|
||||
.unwrap()
|
||||
.env("HOME", env_home);
|
||||
if let Some(shared_lib_path) = shared_lib_path {
|
||||
command.args(&[
|
||||
"build-exe",
|
||||
"-fPIE",
|
||||
shared_lib_path.to_str().unwrap(),
|
||||
bitcode::OBJ_PATH,
|
||||
]);
|
||||
} else {
|
||||
command.args(&["build-obj", "-fPIC"]);
|
||||
}
|
||||
command.args(&[
|
||||
zig_host_src,
|
||||
emit_bin,
|
||||
"--pkg-begin",
|
||||
"str",
|
||||
zig_str_path,
|
||||
"--pkg-end",
|
||||
// include the zig runtime
|
||||
"-fcompiler-rt",
|
||||
// include libc
|
||||
"--library",
|
||||
"c",
|
||||
// cross-compile?
|
||||
"-target",
|
||||
target,
|
||||
]);
|
||||
if matches!(opt_level, OptLevel::Optimize) {
|
||||
command.args(&["-O", "ReleaseSafe"]);
|
||||
}
|
||||
command.output().unwrap()
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn build_zig_host_native(
|
||||
env_path: &str,
|
||||
env_home: &str,
|
||||
|
@ -117,6 +134,8 @@ pub fn build_zig_host_native(
|
|||
zig_host_src: &str,
|
||||
zig_str_path: &str,
|
||||
_target: &str,
|
||||
opt_level: OptLevel,
|
||||
shared_lib_path: Option<&Path>,
|
||||
) -> Output {
|
||||
use serde_json::Value;
|
||||
|
||||
|
@ -158,29 +177,41 @@ pub fn build_zig_host_native(
|
|||
zig_compiler_rt_path.push("special");
|
||||
zig_compiler_rt_path.push("compiler_rt.zig");
|
||||
|
||||
Command::new("zig")
|
||||
let mut command = Command::new("zig");
|
||||
command
|
||||
.env_clear()
|
||||
.env("PATH", &env_path)
|
||||
.env("HOME", &env_home)
|
||||
.args(&[
|
||||
"build-obj",
|
||||
zig_host_src,
|
||||
emit_bin,
|
||||
"--pkg-begin",
|
||||
"str",
|
||||
zig_str_path,
|
||||
"--pkg-end",
|
||||
// include the zig runtime
|
||||
"--pkg-begin",
|
||||
"compiler_rt",
|
||||
zig_compiler_rt_path.to_str().unwrap(),
|
||||
"--pkg-end",
|
||||
// include libc
|
||||
"--library",
|
||||
"c",
|
||||
])
|
||||
.output()
|
||||
.unwrap()
|
||||
.env("HOME", &env_home);
|
||||
if let Some(shared_lib_path) = shared_lib_path {
|
||||
command.args(&[
|
||||
"build-exe",
|
||||
"-fPIE",
|
||||
shared_lib_path.to_str().unwrap(),
|
||||
bitcode::OBJ_PATH,
|
||||
]);
|
||||
} else {
|
||||
command.args(&["build-obj", "-fPIC"]);
|
||||
}
|
||||
command.args(&[
|
||||
zig_host_src,
|
||||
emit_bin,
|
||||
"--pkg-begin",
|
||||
"str",
|
||||
zig_str_path,
|
||||
"--pkg-end",
|
||||
// include the zig runtime
|
||||
"--pkg-begin",
|
||||
"compiler_rt",
|
||||
zig_compiler_rt_path.to_str().unwrap(),
|
||||
"--pkg-end",
|
||||
// include libc
|
||||
"--library",
|
||||
"c",
|
||||
]);
|
||||
if matches!(opt_level, OptLevel::Optimize) {
|
||||
command.args(&["-O", "ReleaseSafe"]);
|
||||
}
|
||||
command.output().unwrap()
|
||||
}
|
||||
|
||||
pub fn build_zig_host_wasm32(
|
||||
|
@ -189,7 +220,12 @@ pub fn build_zig_host_wasm32(
|
|||
emit_bin: &str,
|
||||
zig_host_src: &str,
|
||||
zig_str_path: &str,
|
||||
opt_level: OptLevel,
|
||||
shared_lib_path: Option<&Path>,
|
||||
) -> Output {
|
||||
if shared_lib_path.is_some() {
|
||||
unimplemented!("Linking a shared library to wasm not yet implemented");
|
||||
}
|
||||
// NOTE currently just to get compiler warnings if the host code is invalid.
|
||||
// the produced artifact is not used
|
||||
//
|
||||
|
@ -198,7 +234,8 @@ pub fn build_zig_host_wasm32(
|
|||
// we'd like to compile with `-target wasm32-wasi` but that is blocked on
|
||||
//
|
||||
// https://github.com/ziglang/zig/issues/9414
|
||||
Command::new("zig")
|
||||
let mut command = Command::new("zig");
|
||||
command
|
||||
.env_clear()
|
||||
.env("PATH", env_path)
|
||||
.env("HOME", env_home)
|
||||
|
@ -219,19 +256,68 @@ pub fn build_zig_host_wasm32(
|
|||
"i386-linux-musl",
|
||||
// "wasm32-wasi",
|
||||
// "-femit-llvm-ir=/home/folkertdev/roc/roc/examples/benchmarks/platform/host.ll",
|
||||
])
|
||||
.output()
|
||||
.unwrap()
|
||||
"-fPIC",
|
||||
"--strip",
|
||||
]);
|
||||
if matches!(opt_level, OptLevel::Optimize) {
|
||||
command.args(&["-O", "ReleaseSafe"]);
|
||||
}
|
||||
command.output().unwrap()
|
||||
}
|
||||
|
||||
pub fn rebuild_host(target: &Triple, host_input_path: &Path) {
|
||||
pub fn build_c_host_native(
|
||||
env_path: &str,
|
||||
env_home: &str,
|
||||
dest: &str,
|
||||
sources: &[&str],
|
||||
opt_level: OptLevel,
|
||||
shared_lib_path: Option<&Path>,
|
||||
) -> Output {
|
||||
let mut command = Command::new("clang");
|
||||
command
|
||||
.env_clear()
|
||||
.env("PATH", &env_path)
|
||||
.env("HOME", &env_home)
|
||||
.args(sources)
|
||||
.args(&["-o", dest]);
|
||||
if let Some(shared_lib_path) = shared_lib_path {
|
||||
command.args(&[
|
||||
shared_lib_path.to_str().unwrap(),
|
||||
bitcode::OBJ_PATH,
|
||||
"-fPIE",
|
||||
"-pie",
|
||||
"-lm",
|
||||
"-lpthread",
|
||||
"-ldl",
|
||||
"-lrt",
|
||||
"-lutil",
|
||||
]);
|
||||
} else {
|
||||
command.args(&["-fPIC", "-c"]);
|
||||
}
|
||||
if matches!(opt_level, OptLevel::Optimize) {
|
||||
command.arg("-O2");
|
||||
}
|
||||
command.output().unwrap()
|
||||
}
|
||||
|
||||
pub fn rebuild_host(
|
||||
opt_level: OptLevel,
|
||||
target: &Triple,
|
||||
host_input_path: &Path,
|
||||
shared_lib_path: Option<&Path>,
|
||||
) {
|
||||
let c_host_src = host_input_path.with_file_name("host.c");
|
||||
let c_host_dest = host_input_path.with_file_name("c_host.o");
|
||||
let zig_host_src = host_input_path.with_file_name("host.zig");
|
||||
let rust_host_src = host_input_path.with_file_name("host.rs");
|
||||
let rust_host_dest = host_input_path.with_file_name("rust_host.o");
|
||||
let cargo_host_src = host_input_path.with_file_name("Cargo.toml");
|
||||
let host_dest_native = host_input_path.with_file_name("host.o");
|
||||
let host_dest_native = host_input_path.with_file_name(if shared_lib_path.is_some() {
|
||||
"dynhost"
|
||||
} else {
|
||||
"host.o"
|
||||
});
|
||||
let host_dest_wasm = host_input_path.with_file_name("host.bc");
|
||||
|
||||
let env_path = env::var("PATH").unwrap_or_else(|_| "".to_string());
|
||||
|
@ -257,6 +343,8 @@ pub fn rebuild_host(target: &Triple, host_input_path: &Path) {
|
|||
&emit_bin,
|
||||
zig_host_src.to_str().unwrap(),
|
||||
zig_str_path.to_str().unwrap(),
|
||||
opt_level,
|
||||
shared_lib_path,
|
||||
)
|
||||
}
|
||||
Architecture::X86_64 => {
|
||||
|
@ -268,6 +356,8 @@ pub fn rebuild_host(target: &Triple, host_input_path: &Path) {
|
|||
zig_host_src.to_str().unwrap(),
|
||||
zig_str_path.to_str().unwrap(),
|
||||
"native",
|
||||
opt_level,
|
||||
shared_lib_path,
|
||||
)
|
||||
}
|
||||
Architecture::X86_32(_) => {
|
||||
|
@ -279,87 +369,142 @@ pub fn rebuild_host(target: &Triple, host_input_path: &Path) {
|
|||
zig_host_src.to_str().unwrap(),
|
||||
zig_str_path.to_str().unwrap(),
|
||||
"i386-linux-musl",
|
||||
opt_level,
|
||||
shared_lib_path,
|
||||
)
|
||||
}
|
||||
_ => panic!("Unsupported architecture {:?}", target.architecture),
|
||||
};
|
||||
|
||||
validate_output("host.zig", "zig", output)
|
||||
} else {
|
||||
// Compile host.c
|
||||
let output = Command::new("clang")
|
||||
.env_clear()
|
||||
.env("PATH", &env_path)
|
||||
.args(&[
|
||||
"-c",
|
||||
c_host_src.to_str().unwrap(),
|
||||
"-o",
|
||||
c_host_dest.to_str().unwrap(),
|
||||
])
|
||||
.output()
|
||||
.unwrap();
|
||||
|
||||
validate_output("host.c", "clang", output);
|
||||
}
|
||||
|
||||
if cargo_host_src.exists() {
|
||||
} else if cargo_host_src.exists() {
|
||||
// Compile and link Cargo.toml, if it exists
|
||||
let cargo_dir = host_input_path.parent().unwrap();
|
||||
let libhost_dir = cargo_dir.join("target").join("release");
|
||||
let libhost_dir =
|
||||
cargo_dir
|
||||
.join("target")
|
||||
.join(if matches!(opt_level, OptLevel::Optimize) {
|
||||
"release"
|
||||
} else {
|
||||
"debug"
|
||||
});
|
||||
let libhost = libhost_dir.join("libhost.a");
|
||||
|
||||
let output = Command::new("cargo")
|
||||
.args(&["build", "--release"])
|
||||
.current_dir(cargo_dir)
|
||||
.output()
|
||||
.unwrap();
|
||||
let mut command = Command::new("cargo");
|
||||
command.arg("build").current_dir(cargo_dir);
|
||||
if matches!(opt_level, OptLevel::Optimize) {
|
||||
command.arg("--release");
|
||||
}
|
||||
let output = command.output().unwrap();
|
||||
|
||||
validate_output("src/lib.rs", "cargo build --release", output);
|
||||
validate_output("src/lib.rs", "cargo build", output);
|
||||
|
||||
let output = Command::new("ld")
|
||||
.env_clear()
|
||||
.env("PATH", &env_path)
|
||||
.args(&[
|
||||
"-r",
|
||||
"-L",
|
||||
libhost_dir.to_str().unwrap(),
|
||||
c_host_dest.to_str().unwrap(),
|
||||
"-lhost",
|
||||
"-o",
|
||||
// Cargo hosts depend on a c wrapper for the api. Compile host.c as well.
|
||||
if shared_lib_path.is_some() {
|
||||
// If compiling to executable, let c deal with linking as well.
|
||||
let output = build_c_host_native(
|
||||
&env_path,
|
||||
&env_home,
|
||||
host_dest_native.to_str().unwrap(),
|
||||
])
|
||||
.output()
|
||||
.unwrap();
|
||||
&[c_host_src.to_str().unwrap(), libhost.to_str().unwrap()],
|
||||
opt_level,
|
||||
shared_lib_path,
|
||||
);
|
||||
validate_output("host.c", "clang", output);
|
||||
} else {
|
||||
let output = build_c_host_native(
|
||||
&env_path,
|
||||
&env_home,
|
||||
c_host_dest.to_str().unwrap(),
|
||||
&[c_host_src.to_str().unwrap()],
|
||||
opt_level,
|
||||
shared_lib_path,
|
||||
);
|
||||
validate_output("host.c", "clang", output);
|
||||
|
||||
validate_output("c_host.o", "ld", output);
|
||||
let output = Command::new("ld")
|
||||
.env_clear()
|
||||
.env("PATH", &env_path)
|
||||
.args(&[
|
||||
"-r",
|
||||
"-L",
|
||||
libhost_dir.to_str().unwrap(),
|
||||
c_host_dest.to_str().unwrap(),
|
||||
"-lhost",
|
||||
"-o",
|
||||
host_dest_native.to_str().unwrap(),
|
||||
])
|
||||
.output()
|
||||
.unwrap();
|
||||
validate_output("c_host.o", "ld", output);
|
||||
|
||||
// Clean up c_host.o
|
||||
let output = Command::new("rm")
|
||||
.env_clear()
|
||||
.args(&["-f", c_host_dest.to_str().unwrap()])
|
||||
.output()
|
||||
.unwrap();
|
||||
|
||||
validate_output("rust_host.o", "rm", output);
|
||||
}
|
||||
} else if rust_host_src.exists() {
|
||||
// Compile and link host.rs, if it exists
|
||||
let output = Command::new("rustc")
|
||||
.args(&[
|
||||
rust_host_src.to_str().unwrap(),
|
||||
"-o",
|
||||
rust_host_dest.to_str().unwrap(),
|
||||
])
|
||||
.output()
|
||||
.unwrap();
|
||||
let mut command = Command::new("rustc");
|
||||
command.args(&[
|
||||
rust_host_src.to_str().unwrap(),
|
||||
"-o",
|
||||
rust_host_dest.to_str().unwrap(),
|
||||
]);
|
||||
if matches!(opt_level, OptLevel::Optimize) {
|
||||
command.arg("-O");
|
||||
}
|
||||
let output = command.output().unwrap();
|
||||
|
||||
validate_output("host.rs", "rustc", output);
|
||||
|
||||
let output = Command::new("ld")
|
||||
.env_clear()
|
||||
.env("PATH", &env_path)
|
||||
.args(&[
|
||||
"-r",
|
||||
c_host_dest.to_str().unwrap(),
|
||||
rust_host_dest.to_str().unwrap(),
|
||||
"-o",
|
||||
// Rust hosts depend on a c wrapper for the api. Compile host.c as well.
|
||||
if shared_lib_path.is_some() {
|
||||
// If compiling to executable, let c deal with linking as well.
|
||||
let output = build_c_host_native(
|
||||
&env_path,
|
||||
&env_home,
|
||||
host_dest_native.to_str().unwrap(),
|
||||
])
|
||||
.output()
|
||||
.unwrap();
|
||||
&[
|
||||
c_host_src.to_str().unwrap(),
|
||||
rust_host_dest.to_str().unwrap(),
|
||||
],
|
||||
opt_level,
|
||||
shared_lib_path,
|
||||
);
|
||||
validate_output("host.c", "clang", output);
|
||||
} else {
|
||||
let output = build_c_host_native(
|
||||
&env_path,
|
||||
&env_home,
|
||||
c_host_dest.to_str().unwrap(),
|
||||
&[c_host_src.to_str().unwrap()],
|
||||
opt_level,
|
||||
shared_lib_path,
|
||||
);
|
||||
|
||||
validate_output("rust_host.o", "ld", output);
|
||||
validate_output("host.c", "clang", output);
|
||||
let output = Command::new("ld")
|
||||
.env_clear()
|
||||
.env("PATH", &env_path)
|
||||
.args(&[
|
||||
"-r",
|
||||
c_host_dest.to_str().unwrap(),
|
||||
rust_host_dest.to_str().unwrap(),
|
||||
"-o",
|
||||
host_dest_native.to_str().unwrap(),
|
||||
])
|
||||
.output()
|
||||
.unwrap();
|
||||
|
||||
// Clean up rust_host.o
|
||||
validate_output("rust_host.o", "ld", output);
|
||||
}
|
||||
|
||||
// Clean up rust_host.o and c_host.o
|
||||
let output = Command::new("rm")
|
||||
.env_clear()
|
||||
.args(&[
|
||||
|
@ -371,15 +516,17 @@ pub fn rebuild_host(target: &Triple, host_input_path: &Path) {
|
|||
.unwrap();
|
||||
|
||||
validate_output("rust_host.o", "rm", output);
|
||||
} else if c_host_dest.exists() {
|
||||
// Clean up c_host.o
|
||||
let output = Command::new("mv")
|
||||
.env_clear()
|
||||
.args(&[c_host_dest, host_dest_native])
|
||||
.output()
|
||||
.unwrap();
|
||||
|
||||
validate_output("c_host.o", "mv", output);
|
||||
} else if c_host_src.exists() {
|
||||
// Compile host.c, if it exists
|
||||
let output = build_c_host_native(
|
||||
&env_path,
|
||||
&env_home,
|
||||
host_dest_native.to_str().unwrap(),
|
||||
&[c_host_src.to_str().unwrap()],
|
||||
opt_level,
|
||||
shared_lib_path,
|
||||
);
|
||||
validate_output("host.c", "clang", output);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -521,6 +668,7 @@ fn link_linux(
|
|||
"--eh-frame-hdr",
|
||||
"-arch",
|
||||
arch_str(target),
|
||||
"-pie",
|
||||
libcrt_path.join("crti.o").to_str().unwrap(),
|
||||
libcrt_path.join("crtn.o").to_str().unwrap(),
|
||||
])
|
||||
|
|
|
@ -10,6 +10,8 @@ use roc_mono::ir::OptLevel;
|
|||
use std::path::{Path, PathBuf};
|
||||
use std::time::Duration;
|
||||
|
||||
use roc_collections::all::{MutMap, MutSet};
|
||||
|
||||
#[derive(Debug, Clone, Copy, Default)]
|
||||
pub struct CodeGenTiming {
|
||||
pub code_gen: Duration,
|
||||
|
@ -20,33 +22,15 @@ pub struct CodeGenTiming {
|
|||
// llvm we're using, consider moving me somewhere else.
|
||||
const LLVM_VERSION: &str = "12";
|
||||
|
||||
// TODO how should imported modules factor into this? What if those use builtins too?
|
||||
// TODO this should probably use more helper functions
|
||||
// TODO make this polymorphic in the llvm functions so it can be reused for another backend.
|
||||
#[cfg(feature = "llvm")]
|
||||
#[allow(clippy::cognitive_complexity)]
|
||||
pub fn gen_from_mono_module(
|
||||
arena: &bumpalo::Bump,
|
||||
mut loaded: MonomorphizedModule,
|
||||
roc_file_path: &Path,
|
||||
target: &target_lexicon::Triple,
|
||||
app_o_file: &Path,
|
||||
opt_level: OptLevel,
|
||||
emit_debug_info: bool,
|
||||
) -> CodeGenTiming {
|
||||
use crate::target::{self, convert_opt_level};
|
||||
use inkwell::attributes::{Attribute, AttributeLoc};
|
||||
use inkwell::context::Context;
|
||||
use inkwell::module::Linkage;
|
||||
use inkwell::targets::{CodeModel, FileType, RelocMode};
|
||||
use std::time::SystemTime;
|
||||
|
||||
// TODO instead of finding exhaustiveness problems in monomorphization, find
|
||||
// them after type checking (like Elm does) so we can complete the entire
|
||||
// `roc check` process without needing to monomorphize.
|
||||
/// Returns the number of problems reported.
|
||||
pub fn report_problems(loaded: &mut MonomorphizedModule) -> usize {
|
||||
use roc_reporting::report::{
|
||||
can_problem, mono_problem, type_problem, Report, RocDocAllocator, Severity::*,
|
||||
DEFAULT_PALETTE,
|
||||
};
|
||||
|
||||
let code_gen_start = SystemTime::now();
|
||||
let palette = DEFAULT_PALETTE;
|
||||
|
||||
// This will often over-allocate total memory, but it means we definitely
|
||||
|
@ -55,10 +39,10 @@ pub fn gen_from_mono_module(
|
|||
let mut warnings = Vec::with_capacity(total_problems);
|
||||
let mut errors = Vec::with_capacity(total_problems);
|
||||
|
||||
for (home, (module_path, src)) in loaded.sources {
|
||||
for (home, (module_path, src)) in loaded.sources.iter() {
|
||||
let mut src_lines: Vec<&str> = Vec::new();
|
||||
|
||||
if let Some((_, header_src)) = loaded.header_sources.get(&home) {
|
||||
if let Some((_, header_src)) = loaded.header_sources.get(home) {
|
||||
src_lines.extend(header_src.split('\n'));
|
||||
src_lines.extend(src.split('\n').skip(1));
|
||||
} else {
|
||||
|
@ -66,9 +50,10 @@ pub fn gen_from_mono_module(
|
|||
}
|
||||
|
||||
// Report parsing and canonicalization problems
|
||||
let alloc = RocDocAllocator::new(&src_lines, home, &loaded.interns);
|
||||
let alloc = RocDocAllocator::new(&src_lines, *home, &loaded.interns);
|
||||
|
||||
let problems = loaded.can_problems.remove(home).unwrap_or_default();
|
||||
|
||||
let problems = loaded.can_problems.remove(&home).unwrap_or_default();
|
||||
for problem in problems.into_iter() {
|
||||
let report = can_problem(&alloc, module_path.clone(), problem);
|
||||
let severity = report.severity;
|
||||
|
@ -86,25 +71,28 @@ pub fn gen_from_mono_module(
|
|||
}
|
||||
}
|
||||
|
||||
let problems = loaded.type_problems.remove(&home).unwrap_or_default();
|
||||
let problems = loaded.type_problems.remove(home).unwrap_or_default();
|
||||
|
||||
for problem in problems {
|
||||
let report = type_problem(&alloc, module_path.clone(), problem);
|
||||
let severity = report.severity;
|
||||
let mut buf = String::new();
|
||||
if let Some(report) = type_problem(&alloc, module_path.clone(), problem) {
|
||||
let severity = report.severity;
|
||||
let mut buf = String::new();
|
||||
|
||||
report.render_color_terminal(&mut buf, &alloc, &palette);
|
||||
report.render_color_terminal(&mut buf, &alloc, &palette);
|
||||
|
||||
match severity {
|
||||
Warning => {
|
||||
warnings.push(buf);
|
||||
}
|
||||
RuntimeError => {
|
||||
errors.push(buf);
|
||||
match severity {
|
||||
Warning => {
|
||||
warnings.push(buf);
|
||||
}
|
||||
RuntimeError => {
|
||||
errors.push(buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let problems = loaded.mono_problems.remove(&home).unwrap_or_default();
|
||||
let problems = loaded.mono_problems.remove(home).unwrap_or_default();
|
||||
|
||||
for problem in problems {
|
||||
let report = mono_problem(&alloc, module_path.clone(), problem);
|
||||
let severity = report.severity;
|
||||
|
@ -123,12 +111,18 @@ pub fn gen_from_mono_module(
|
|||
}
|
||||
}
|
||||
|
||||
let problems_reported;
|
||||
|
||||
// Only print warnings if there are no errors
|
||||
if errors.is_empty() {
|
||||
problems_reported = warnings.len();
|
||||
|
||||
for warning in warnings {
|
||||
println!("\n{}\n", warning);
|
||||
}
|
||||
} else {
|
||||
problems_reported = errors.len();
|
||||
|
||||
for error in errors {
|
||||
println!("\n{}\n", error);
|
||||
}
|
||||
|
@ -140,10 +134,35 @@ pub fn gen_from_mono_module(
|
|||
// The horizontal rule is nice when running the program right after
|
||||
// compiling it, as it lets you clearly see where the compiler
|
||||
// errors/warnings end and the program output begins.
|
||||
if total_problems > 0 {
|
||||
if problems_reported > 0 {
|
||||
println!("{}\u{001B}[0m\n", Report::horizontal_rule(&palette));
|
||||
}
|
||||
|
||||
problems_reported
|
||||
}
|
||||
|
||||
// TODO how should imported modules factor into this? What if those use builtins too?
|
||||
// TODO this should probably use more helper functions
|
||||
// TODO make this polymorphic in the llvm functions so it can be reused for another backend.
|
||||
#[cfg(feature = "llvm")]
|
||||
pub fn gen_from_mono_module_llvm(
|
||||
arena: &bumpalo::Bump,
|
||||
loaded: MonomorphizedModule,
|
||||
roc_file_path: &Path,
|
||||
target: &target_lexicon::Triple,
|
||||
app_o_file: &Path,
|
||||
opt_level: OptLevel,
|
||||
emit_debug_info: bool,
|
||||
) -> CodeGenTiming {
|
||||
use crate::target::{self, convert_opt_level};
|
||||
use inkwell::attributes::{Attribute, AttributeLoc};
|
||||
use inkwell::context::Context;
|
||||
use inkwell::module::Linkage;
|
||||
use inkwell::targets::{CodeModel, FileType, RelocMode};
|
||||
use std::time::SystemTime;
|
||||
|
||||
let code_gen_start = SystemTime::now();
|
||||
|
||||
// Generate the binary
|
||||
let ptr_bytes = target.pointer_width().unwrap().bytes() as u32;
|
||||
let context = Context::create();
|
||||
|
@ -286,6 +305,7 @@ pub fn gen_from_mono_module(
|
|||
.unwrap();
|
||||
|
||||
let llc_args = &[
|
||||
"-relocation-model=pic",
|
||||
"-filetype=obj",
|
||||
app_bc_file.to_str().unwrap(),
|
||||
"-o",
|
||||
|
@ -325,7 +345,7 @@ pub fn gen_from_mono_module(
|
|||
use target_lexicon::Architecture;
|
||||
match target.architecture {
|
||||
Architecture::X86_64 | Architecture::X86_32(_) | Architecture::Aarch64(_) => {
|
||||
let reloc = RelocMode::Default;
|
||||
let reloc = RelocMode::PIC;
|
||||
let model = CodeModel::Default;
|
||||
let target_machine =
|
||||
target::target_machine(target, convert_opt_level(opt_level), reloc, model)
|
||||
|
@ -354,3 +374,78 @@ pub fn gen_from_mono_module(
|
|||
emit_o_file,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn gen_from_mono_module_dev(
|
||||
arena: &bumpalo::Bump,
|
||||
loaded: MonomorphizedModule,
|
||||
target: &target_lexicon::Triple,
|
||||
app_o_file: &Path,
|
||||
) -> CodeGenTiming {
|
||||
use target_lexicon::Architecture;
|
||||
|
||||
match target.architecture {
|
||||
Architecture::Wasm32 => gen_from_mono_module_dev_wasm32(arena, loaded, app_o_file),
|
||||
Architecture::X86_64 => {
|
||||
gen_from_mono_module_dev_assembly(arena, loaded, target, app_o_file)
|
||||
}
|
||||
_ => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
fn gen_from_mono_module_dev_wasm32(
|
||||
arena: &bumpalo::Bump,
|
||||
loaded: MonomorphizedModule,
|
||||
app_o_file: &Path,
|
||||
) -> CodeGenTiming {
|
||||
let mut procedures = MutMap::default();
|
||||
|
||||
for (key, proc) in loaded.procedures {
|
||||
procedures.insert(key, proc);
|
||||
}
|
||||
|
||||
let exposed_to_host = loaded
|
||||
.exposed_to_host
|
||||
.keys()
|
||||
.copied()
|
||||
.collect::<MutSet<_>>();
|
||||
|
||||
let env = roc_gen_wasm::Env {
|
||||
arena,
|
||||
interns: loaded.interns,
|
||||
exposed_to_host,
|
||||
};
|
||||
|
||||
let bytes = roc_gen_wasm::build_module(&env, procedures).unwrap();
|
||||
|
||||
std::fs::write(&app_o_file, &bytes).expect("failed to write object to file");
|
||||
|
||||
CodeGenTiming::default()
|
||||
}
|
||||
|
||||
fn gen_from_mono_module_dev_assembly(
|
||||
arena: &bumpalo::Bump,
|
||||
loaded: MonomorphizedModule,
|
||||
target: &target_lexicon::Triple,
|
||||
app_o_file: &Path,
|
||||
) -> CodeGenTiming {
|
||||
let lazy_literals = true;
|
||||
let generate_allocators = false; // provided by the platform
|
||||
|
||||
let env = roc_gen_dev::Env {
|
||||
arena,
|
||||
interns: loaded.interns,
|
||||
exposed_to_host: loaded.exposed_to_host.keys().copied().collect(),
|
||||
lazy_literals,
|
||||
generate_allocators,
|
||||
};
|
||||
|
||||
let module_object = roc_gen_dev::build_module(&env, target, loaded.procedures)
|
||||
.expect("failed to compile module");
|
||||
|
||||
let module_out = module_object
|
||||
.write()
|
||||
.expect("failed to build output object");
|
||||
std::fs::write(&app_o_file, module_out).expect("failed to write object to file");
|
||||
|
||||
CodeGenTiming::default()
|
||||
}
|
||||
|
|
|
@ -106,7 +106,7 @@ pub fn target_machine(
|
|||
#[cfg(feature = "llvm")]
|
||||
pub fn convert_opt_level(level: OptLevel) -> OptimizationLevel {
|
||||
match level {
|
||||
OptLevel::Normal => OptimizationLevel::None,
|
||||
OptLevel::Development | OptLevel::Normal => OptimizationLevel::None,
|
||||
OptLevel::Optimize => OptimizationLevel::Aggressive,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -101,6 +101,7 @@ comptime {
|
|||
exportStrFn(str.strToUtf8C, "to_utf8");
|
||||
exportStrFn(str.fromUtf8C, "from_utf8");
|
||||
exportStrFn(str.fromUtf8RangeC, "from_utf8_range");
|
||||
exportStrFn(str.repeat, "repeat");
|
||||
}
|
||||
|
||||
// Utils
|
||||
|
@ -164,7 +165,12 @@ test "" {
|
|||
// https://github.com/ziglang/zig/blob/85755c51d529e7d9b406c6bdf69ce0a0f33f3353/lib/std/special/compiler_rt/muloti4.zig
|
||||
//
|
||||
// Thank you Zig Contributors!
|
||||
export fn __muloti4(a: i128, b: i128, overflow: *c_int) callconv(.C) i128 {
|
||||
|
||||
// Export it as weak incase it is alreadly linked in by something else.
|
||||
comptime {
|
||||
@export(__muloti4, .{ .name = "__muloti4", .linkage = .Weak });
|
||||
}
|
||||
fn __muloti4(a: i128, b: i128, overflow: *c_int) callconv(.C) i128 {
|
||||
// @setRuntimeSafety(std.builtin.is_test);
|
||||
|
||||
const min = @bitCast(i128, @as(u128, 1 << (128 - 1)));
|
||||
|
|
|
@ -866,6 +866,22 @@ pub fn startsWith(string: RocStr, prefix: RocStr) callconv(.C) bool {
|
|||
return true;
|
||||
}
|
||||
|
||||
// Str.repeat
|
||||
pub fn repeat(string: RocStr, count: usize) callconv(.C) RocStr {
|
||||
const bytes_len = string.len();
|
||||
const bytes_ptr = string.asU8ptr();
|
||||
|
||||
var ret_string = RocStr.allocate(.Clone, count * bytes_len);
|
||||
var ret_string_ptr = ret_string.asU8ptr();
|
||||
|
||||
var i: usize = 0;
|
||||
while (i < count) : (i += 1) {
|
||||
@memcpy(ret_string_ptr + (i * bytes_len), bytes_ptr, bytes_len);
|
||||
}
|
||||
|
||||
return ret_string;
|
||||
}
|
||||
|
||||
// Str.startsWithCodePt
|
||||
pub fn startsWithCodePt(string: RocStr, prefix: u32) callconv(.C) bool {
|
||||
const bytes_ptr = string.asU8ptr();
|
||||
|
@ -1150,8 +1166,8 @@ fn strToBytes(arg: RocStr) RocList {
|
|||
}
|
||||
|
||||
const FromUtf8Result = extern struct {
|
||||
string: RocStr,
|
||||
byte_index: usize,
|
||||
string: RocStr,
|
||||
is_ok: bool,
|
||||
problem_code: Utf8ByteProblem,
|
||||
};
|
||||
|
|
|
@ -28,6 +28,7 @@ pub const STR_EQUAL: &str = "roc_builtins.str.equal";
|
|||
pub const STR_TO_UTF8: &str = "roc_builtins.str.to_utf8";
|
||||
pub const STR_FROM_UTF8: &str = "roc_builtins.str.from_utf8";
|
||||
pub const STR_FROM_UTF8_RANGE: &str = "roc_builtins.str.from_utf8_range";
|
||||
pub const STR_REPEAT: &str = "roc_builtins.str.repeat";
|
||||
|
||||
pub const DICT_HASH: &str = "roc_builtins.dict.hash";
|
||||
pub const DICT_HASH_STR: &str = "roc_builtins.dict.hash_str";
|
||||
|
|
|
@ -618,6 +618,13 @@ pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
|
|||
Box::new(str_type())
|
||||
);
|
||||
|
||||
// repeat : Str, Nat -> Str
|
||||
add_top_level_function_type!(
|
||||
Symbol::STR_REPEAT,
|
||||
vec![str_type(), nat_type()],
|
||||
Box::new(str_type())
|
||||
);
|
||||
|
||||
// fromUtf8 : List U8 -> Result Str [ BadUtf8 Utf8Problem ]*
|
||||
{
|
||||
let bad_utf8 = SolvedType::TagUnion(
|
||||
|
|
|
@ -82,6 +82,7 @@ pub fn canonicalize_annotation(
|
|||
let mut introduced_variables = IntroducedVariables::default();
|
||||
let mut references = MutSet::default();
|
||||
let mut aliases = SendMap::default();
|
||||
|
||||
let typ = can_annotation_help(
|
||||
env,
|
||||
annotation,
|
||||
|
@ -249,28 +250,7 @@ fn can_annotation_help(
|
|||
actual: Box::new(actual),
|
||||
}
|
||||
}
|
||||
None => {
|
||||
let mut args = Vec::new();
|
||||
|
||||
references.insert(symbol);
|
||||
|
||||
for arg in *type_arguments {
|
||||
let arg_ann = can_annotation_help(
|
||||
env,
|
||||
&arg.value,
|
||||
region,
|
||||
scope,
|
||||
var_store,
|
||||
introduced_variables,
|
||||
local_aliases,
|
||||
references,
|
||||
);
|
||||
|
||||
args.push(arg_ann);
|
||||
}
|
||||
|
||||
Type::Apply(symbol, args)
|
||||
}
|
||||
None => Type::Apply(symbol, args),
|
||||
}
|
||||
}
|
||||
BoundVariable(v) => {
|
||||
|
|
|
@ -66,6 +66,7 @@ pub fn builtin_defs_map(symbol: Symbol, var_store: &mut VarStore) -> Option<Def>
|
|||
STR_FROM_UTF8_RANGE => str_from_utf8_range,
|
||||
STR_TO_UTF8 => str_to_utf8,
|
||||
STR_FROM_FLOAT=> str_from_float,
|
||||
STR_REPEAT => str_repeat,
|
||||
LIST_LEN => list_len,
|
||||
LIST_GET => list_get,
|
||||
LIST_SET => list_set,
|
||||
|
@ -1233,6 +1234,26 @@ fn str_split(symbol: Symbol, var_store: &mut VarStore) -> Def {
|
|||
)
|
||||
}
|
||||
|
||||
/// Str.repeat : Str, Nat -> Str
|
||||
fn str_repeat(symbol: Symbol, var_store: &mut VarStore) -> Def {
|
||||
let str_var = var_store.fresh();
|
||||
let nat_var = var_store.fresh();
|
||||
|
||||
let body = RunLowLevel {
|
||||
op: LowLevel::StrRepeat,
|
||||
args: vec![(str_var, Var(Symbol::ARG_1)), (nat_var, Var(Symbol::ARG_2))],
|
||||
ret_var: str_var,
|
||||
};
|
||||
|
||||
defn(
|
||||
symbol,
|
||||
vec![(str_var, Symbol::ARG_1), (nat_var, Symbol::ARG_2)],
|
||||
var_store,
|
||||
body,
|
||||
str_var,
|
||||
)
|
||||
}
|
||||
|
||||
/// Str.concat : Str, Str -> Str
|
||||
fn str_concat(symbol: Symbol, var_store: &mut VarStore) -> Def {
|
||||
let str_var = var_store.fresh();
|
||||
|
|
|
@ -226,6 +226,7 @@ impl CallConv<AArch64GeneralReg, AArch64FloatReg> for AArch64Call {
|
|||
|
||||
#[inline(always)]
|
||||
fn load_args<'a>(
|
||||
_buf: &mut Vec<'a, u8>,
|
||||
_symbol_map: &mut MutMap<Symbol, SymbolStorage<AArch64GeneralReg, AArch64FloatReg>>,
|
||||
_args: &'a [(Layout<'a>, Symbol)],
|
||||
_ret_layout: &Layout<'a>,
|
||||
|
|
|
@ -2,15 +2,14 @@ use crate::{Backend, Env, Relocation};
|
|||
use bumpalo::collections::Vec;
|
||||
use roc_collections::all::{MutMap, MutSet};
|
||||
use roc_module::symbol::Symbol;
|
||||
use roc_mono::ir::{BranchInfo, Literal, Stmt};
|
||||
use roc_mono::ir::{BranchInfo, JoinPointId, Literal, Param, SelfRecursive, Stmt};
|
||||
use roc_mono::layout::{Builtin, Layout};
|
||||
use std::marker::PhantomData;
|
||||
use target_lexicon::Triple;
|
||||
|
||||
pub mod aarch64;
|
||||
pub mod x86_64;
|
||||
|
||||
const PTR_SIZE: u32 = 64;
|
||||
const PTR_SIZE: u32 = 8;
|
||||
|
||||
pub trait CallConv<GeneralReg: RegTrait, FloatReg: RegTrait> {
|
||||
const GENERAL_PARAM_REGS: &'static [GeneralReg];
|
||||
|
@ -49,6 +48,7 @@ pub trait CallConv<GeneralReg: RegTrait, FloatReg: RegTrait> {
|
|||
|
||||
// load_args updates the symbol map to know where every arg is stored.
|
||||
fn load_args<'a>(
|
||||
buf: &mut Vec<'a, u8>,
|
||||
symbol_map: &mut MutMap<Symbol, SymbolStorage<GeneralReg, FloatReg>>,
|
||||
args: &'a [(Layout<'a>, Symbol)],
|
||||
// ret_layout is needed because if it is a complex type, we pass a pointer as the first arg.
|
||||
|
@ -211,12 +211,16 @@ pub struct Backend64Bit<
|
|||
env: &'a Env<'a>,
|
||||
buf: Vec<'a, u8>,
|
||||
relocs: Vec<'a, Relocation>,
|
||||
proc_name: Option<String>,
|
||||
is_self_recursive: Option<SelfRecursive>,
|
||||
|
||||
last_seen_map: MutMap<Symbol, *const Stmt<'a>>,
|
||||
layout_map: MutMap<Symbol, *const Layout<'a>>,
|
||||
layout_map: MutMap<Symbol, Layout<'a>>,
|
||||
free_map: MutMap<*const Stmt<'a>, Vec<'a, Symbol>>,
|
||||
|
||||
symbol_storage_map: MutMap<Symbol, SymbolStorage<GeneralReg, FloatReg>>,
|
||||
literal_map: MutMap<Symbol, Literal<'a>>,
|
||||
join_map: MutMap<JoinPointId, u64>,
|
||||
|
||||
// This should probably be smarter than a vec.
|
||||
// There are certain registers we should always use first. With pushing and popping, this could get mixed.
|
||||
|
@ -247,11 +251,13 @@ impl<
|
|||
CC: CallConv<GeneralReg, FloatReg>,
|
||||
> Backend<'a> for Backend64Bit<'a, GeneralReg, FloatReg, ASM, CC>
|
||||
{
|
||||
fn new(env: &'a Env, _target: &Triple) -> Result<Self, String> {
|
||||
fn new(env: &'a Env) -> Result<Self, String> {
|
||||
Ok(Backend64Bit {
|
||||
phantom_asm: PhantomData,
|
||||
phantom_cc: PhantomData,
|
||||
env,
|
||||
proc_name: None,
|
||||
is_self_recursive: None,
|
||||
buf: bumpalo::vec![in env.arena],
|
||||
relocs: bumpalo::vec![in env.arena],
|
||||
last_seen_map: MutMap::default(),
|
||||
|
@ -259,6 +265,7 @@ impl<
|
|||
free_map: MutMap::default(),
|
||||
symbol_storage_map: MutMap::default(),
|
||||
literal_map: MutMap::default(),
|
||||
join_map: MutMap::default(),
|
||||
general_free_regs: bumpalo::vec![in env.arena],
|
||||
general_used_regs: bumpalo::vec![in env.arena],
|
||||
general_used_callee_saved_regs: MutSet::default(),
|
||||
|
@ -275,12 +282,15 @@ impl<
|
|||
self.env
|
||||
}
|
||||
|
||||
fn reset(&mut self) {
|
||||
fn reset(&mut self, name: String, is_self_recursive: SelfRecursive) {
|
||||
self.proc_name = Some(name);
|
||||
self.is_self_recursive = Some(is_self_recursive);
|
||||
self.stack_size = 0;
|
||||
self.free_stack_chunks.clear();
|
||||
self.fn_call_stack_size = 0;
|
||||
self.last_seen_map.clear();
|
||||
self.layout_map.clear();
|
||||
self.join_map.clear();
|
||||
self.free_map.clear();
|
||||
self.symbol_storage_map.clear();
|
||||
self.buf.clear();
|
||||
|
@ -304,7 +314,7 @@ impl<
|
|||
&mut self.last_seen_map
|
||||
}
|
||||
|
||||
fn layout_map(&mut self) -> &mut MutMap<Symbol, *const Layout<'a>> {
|
||||
fn layout_map(&mut self) -> &mut MutMap<Symbol, Layout<'a>> {
|
||||
&mut self.layout_map
|
||||
}
|
||||
|
||||
|
@ -330,8 +340,49 @@ impl<
|
|||
)?;
|
||||
let setup_offset = out.len();
|
||||
|
||||
// Deal with jumps to the return address.
|
||||
let old_relocs = std::mem::replace(&mut self.relocs, bumpalo::vec![in self.env.arena]);
|
||||
|
||||
// Check if their is an unnessary jump to return right at the end of the function.
|
||||
let mut end_jmp_size = 0;
|
||||
for reloc in old_relocs
|
||||
.iter()
|
||||
.filter(|reloc| matches!(reloc, Relocation::JmpToReturn { .. }))
|
||||
{
|
||||
if let Relocation::JmpToReturn {
|
||||
inst_loc,
|
||||
inst_size,
|
||||
..
|
||||
} = reloc
|
||||
{
|
||||
if *inst_loc as usize + *inst_size as usize == self.buf.len() {
|
||||
end_jmp_size = *inst_size as usize;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Update jumps to returns.
|
||||
let ret_offset = self.buf.len() - end_jmp_size;
|
||||
let mut tmp = bumpalo::vec![in self.env.arena];
|
||||
for reloc in old_relocs
|
||||
.iter()
|
||||
.filter(|reloc| matches!(reloc, Relocation::JmpToReturn { .. }))
|
||||
{
|
||||
if let Relocation::JmpToReturn {
|
||||
inst_loc,
|
||||
inst_size,
|
||||
offset,
|
||||
} = reloc
|
||||
{
|
||||
if *inst_loc as usize + *inst_size as usize != self.buf.len() {
|
||||
self.update_jmp_imm32_offset(&mut tmp, *inst_loc, *offset, ret_offset as u64);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Add function body.
|
||||
out.extend(&self.buf);
|
||||
out.extend(&self.buf[..self.buf.len() - end_jmp_size]);
|
||||
|
||||
// Cleanup stack.
|
||||
CC::cleanup_stack(
|
||||
|
@ -342,23 +393,28 @@ impl<
|
|||
)?;
|
||||
ASM::ret(&mut out);
|
||||
|
||||
// Update relocs to include stack setup offset.
|
||||
// Update other relocs to include stack setup offset.
|
||||
let mut out_relocs = bumpalo::vec![in self.env.arena];
|
||||
let old_relocs = std::mem::replace(&mut self.relocs, bumpalo::vec![in self.env.arena]);
|
||||
out_relocs.extend(old_relocs.into_iter().map(|reloc| match reloc {
|
||||
Relocation::LocalData { offset, data } => Relocation::LocalData {
|
||||
offset: offset + setup_offset as u64,
|
||||
data,
|
||||
},
|
||||
Relocation::LinkedData { offset, name } => Relocation::LinkedData {
|
||||
offset: offset + setup_offset as u64,
|
||||
name,
|
||||
},
|
||||
Relocation::LinkedFunction { offset, name } => Relocation::LinkedFunction {
|
||||
offset: offset + setup_offset as u64,
|
||||
name,
|
||||
},
|
||||
}));
|
||||
out_relocs.extend(
|
||||
old_relocs
|
||||
.into_iter()
|
||||
.filter(|reloc| !matches!(reloc, Relocation::JmpToReturn { .. }))
|
||||
.map(|reloc| match reloc {
|
||||
Relocation::LocalData { offset, data } => Relocation::LocalData {
|
||||
offset: offset + setup_offset as u64,
|
||||
data,
|
||||
},
|
||||
Relocation::LinkedData { offset, name } => Relocation::LinkedData {
|
||||
offset: offset + setup_offset as u64,
|
||||
name,
|
||||
},
|
||||
Relocation::LinkedFunction { offset, name } => Relocation::LinkedFunction {
|
||||
offset: offset + setup_offset as u64,
|
||||
name,
|
||||
},
|
||||
Relocation::JmpToReturn { .. } => unreachable!(),
|
||||
}),
|
||||
);
|
||||
Ok((out.into_bump_slice(), out_relocs.into_bump_slice()))
|
||||
}
|
||||
|
||||
|
@ -367,7 +423,12 @@ impl<
|
|||
args: &'a [(Layout<'a>, Symbol)],
|
||||
ret_layout: &Layout<'a>,
|
||||
) -> Result<(), String> {
|
||||
CC::load_args(&mut self.symbol_storage_map, args, ret_layout)?;
|
||||
CC::load_args(
|
||||
&mut self.buf,
|
||||
&mut self.symbol_storage_map,
|
||||
args,
|
||||
ret_layout,
|
||||
)?;
|
||||
// Update used and free regs.
|
||||
for (sym, storage) in &self.symbol_storage_map {
|
||||
match storage {
|
||||
|
@ -401,29 +462,13 @@ impl<
|
|||
arg_layouts: &[Layout<'a>],
|
||||
ret_layout: &Layout<'a>,
|
||||
) -> Result<(), String> {
|
||||
if let Some(SelfRecursive::SelfRecursive(id)) = self.is_self_recursive {
|
||||
if &fn_name == self.proc_name.as_ref().unwrap() && self.join_map.contains_key(&id) {
|
||||
return self.build_jump(&id, args, arg_layouts, ret_layout);
|
||||
}
|
||||
}
|
||||
// Save used caller saved regs.
|
||||
let old_general_used_regs = std::mem::replace(
|
||||
&mut self.general_used_regs,
|
||||
bumpalo::vec![in self.env.arena],
|
||||
);
|
||||
for (reg, saved_sym) in old_general_used_regs.into_iter() {
|
||||
if CC::general_caller_saved(®) {
|
||||
self.general_free_regs.push(reg);
|
||||
self.free_to_stack(&saved_sym)?;
|
||||
} else {
|
||||
self.general_used_regs.push((reg, saved_sym));
|
||||
}
|
||||
}
|
||||
let old_float_used_regs =
|
||||
std::mem::replace(&mut self.float_used_regs, bumpalo::vec![in self.env.arena]);
|
||||
for (reg, saved_sym) in old_float_used_regs.into_iter() {
|
||||
if CC::float_caller_saved(®) {
|
||||
self.float_free_regs.push(reg);
|
||||
self.free_to_stack(&saved_sym)?;
|
||||
} else {
|
||||
self.float_used_regs.push((reg, saved_sym));
|
||||
}
|
||||
}
|
||||
self.push_used_caller_saved_regs_to_stack()?;
|
||||
|
||||
// Put values in param regs or on top of the stack.
|
||||
let tmp_stack_size = CC::store_args(
|
||||
|
@ -450,6 +495,25 @@ impl<
|
|||
ASM::mov_freg64_freg64(&mut self.buf, dst_reg, CC::FLOAT_RETURN_REGS[0]);
|
||||
Ok(())
|
||||
}
|
||||
Layout::Builtin(Builtin::Str) => {
|
||||
if CC::returns_via_arg_pointer(ret_layout)? {
|
||||
// This will happen on windows, return via pointer here.
|
||||
Err("FnCall: Returning strings via pointer not yet implemented".to_string())
|
||||
} else {
|
||||
let offset = self.claim_stack_size(16)?;
|
||||
self.symbol_storage_map.insert(
|
||||
*dst,
|
||||
SymbolStorage::Base {
|
||||
offset,
|
||||
size: 16,
|
||||
owned: true,
|
||||
},
|
||||
);
|
||||
ASM::mov_base32_reg64(&mut self.buf, offset, CC::GENERAL_RETURN_REGS[0]);
|
||||
ASM::mov_base32_reg64(&mut self.buf, offset + 8, CC::GENERAL_RETURN_REGS[1]);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
x => Err(format!(
|
||||
"FnCall: receiving return type, {:?}, is not yet implemented",
|
||||
x
|
||||
|
@ -486,7 +550,7 @@ impl<
|
|||
// Build unconditional jump to the end of this switch.
|
||||
// Since we don't know the offset yet, set it to 0 and overwrite later.
|
||||
let jmp_location = self.buf.len();
|
||||
let jmp_offset = ASM::jmp_imm32(&mut self.buf, 0);
|
||||
let jmp_offset = ASM::jmp_imm32(&mut self.buf, 0x1234_5678);
|
||||
ret_jumps.push((jmp_location, jmp_offset));
|
||||
|
||||
// Overwite the original jne with the correct offset.
|
||||
|
@ -510,12 +574,12 @@ impl<
|
|||
// Update all return jumps to jump past the default case.
|
||||
let ret_offset = self.buf.len();
|
||||
for (jmp_location, start_offset) in ret_jumps.into_iter() {
|
||||
tmp.clear();
|
||||
let jmp_offset = ret_offset - start_offset;
|
||||
ASM::jmp_imm32(&mut tmp, jmp_offset as i32);
|
||||
for (i, byte) in tmp.iter().enumerate() {
|
||||
self.buf[jmp_location + i] = *byte;
|
||||
}
|
||||
self.update_jmp_imm32_offset(
|
||||
&mut tmp,
|
||||
jmp_location as u64,
|
||||
start_offset as u64,
|
||||
ret_offset as u64,
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
} else {
|
||||
|
@ -526,6 +590,134 @@ impl<
|
|||
}
|
||||
}
|
||||
|
||||
fn build_join(
|
||||
&mut self,
|
||||
id: &JoinPointId,
|
||||
parameters: &'a [Param<'a>],
|
||||
body: &'a Stmt<'a>,
|
||||
remainder: &'a Stmt<'a>,
|
||||
ret_layout: &Layout<'a>,
|
||||
) -> Result<(), String> {
|
||||
// Create jump to remaining.
|
||||
let jmp_location = self.buf.len();
|
||||
let start_offset = ASM::jmp_imm32(&mut self.buf, 0x1234_5678);
|
||||
|
||||
// This section can essentially be seen as a sub function within the main function.
|
||||
// Thus we build using a new backend with some minor extra synchronization.
|
||||
let mut sub_backend = Self::new(self.env)?;
|
||||
sub_backend.reset(
|
||||
self.proc_name.as_ref().unwrap().clone(),
|
||||
self.is_self_recursive.as_ref().unwrap().clone(),
|
||||
);
|
||||
// Sync static maps of important information.
|
||||
sub_backend.last_seen_map = self.last_seen_map.clone();
|
||||
sub_backend.layout_map = self.layout_map.clone();
|
||||
sub_backend.free_map = self.free_map.clone();
|
||||
|
||||
// Setup join point.
|
||||
sub_backend.join_map.insert(*id, 0);
|
||||
self.join_map.insert(*id, self.buf.len() as u64);
|
||||
|
||||
// Sync stack size so the "sub function" doesn't mess up our stack.
|
||||
sub_backend.stack_size = self.stack_size;
|
||||
sub_backend.fn_call_stack_size = self.fn_call_stack_size;
|
||||
|
||||
// Load params as if they were args.
|
||||
let mut args = bumpalo::vec![in self.env.arena];
|
||||
for param in parameters {
|
||||
args.push((param.layout, param.symbol));
|
||||
}
|
||||
sub_backend.load_args(args.into_bump_slice(), ret_layout)?;
|
||||
|
||||
// Build all statements in body.
|
||||
sub_backend.build_stmt(body, ret_layout)?;
|
||||
|
||||
// Merge the "sub function" into the main function.
|
||||
let sub_func_offset = self.buf.len() as u64;
|
||||
self.buf.extend_from_slice(&sub_backend.buf);
|
||||
// Update stack based on how much was used by the sub function.
|
||||
self.stack_size = sub_backend.stack_size;
|
||||
self.fn_call_stack_size = sub_backend.fn_call_stack_size;
|
||||
// Relocations must be shifted to be merged correctly.
|
||||
self.relocs
|
||||
.extend(sub_backend.relocs.into_iter().map(|reloc| match reloc {
|
||||
Relocation::LocalData { offset, data } => Relocation::LocalData {
|
||||
offset: offset + sub_func_offset,
|
||||
data,
|
||||
},
|
||||
Relocation::LinkedData { offset, name } => Relocation::LinkedData {
|
||||
offset: offset + sub_func_offset,
|
||||
name,
|
||||
},
|
||||
Relocation::LinkedFunction { offset, name } => Relocation::LinkedFunction {
|
||||
offset: offset + sub_func_offset,
|
||||
name,
|
||||
},
|
||||
Relocation::JmpToReturn {
|
||||
inst_loc,
|
||||
inst_size,
|
||||
offset,
|
||||
} => Relocation::JmpToReturn {
|
||||
inst_loc: inst_loc + sub_func_offset,
|
||||
inst_size,
|
||||
offset: offset + sub_func_offset,
|
||||
},
|
||||
}));
|
||||
|
||||
// Overwite the original jump with the correct offset.
|
||||
let mut tmp = bumpalo::vec![in self.env.arena];
|
||||
self.update_jmp_imm32_offset(
|
||||
&mut tmp,
|
||||
jmp_location as u64,
|
||||
start_offset as u64,
|
||||
self.buf.len() as u64,
|
||||
);
|
||||
|
||||
// Build remainder of function.
|
||||
self.build_stmt(remainder, ret_layout)
|
||||
}
|
||||
|
||||
fn build_jump(
|
||||
&mut self,
|
||||
id: &JoinPointId,
|
||||
args: &'a [Symbol],
|
||||
arg_layouts: &[Layout<'a>],
|
||||
ret_layout: &Layout<'a>,
|
||||
) -> Result<(), String> {
|
||||
// Treat this like a function call, but with a jump instead of a call instruction at the end.
|
||||
|
||||
self.push_used_caller_saved_regs_to_stack()?;
|
||||
|
||||
let tmp_stack_size = CC::store_args(
|
||||
&mut self.buf,
|
||||
&self.symbol_storage_map,
|
||||
args,
|
||||
arg_layouts,
|
||||
ret_layout,
|
||||
)?;
|
||||
self.fn_call_stack_size = std::cmp::max(self.fn_call_stack_size, tmp_stack_size);
|
||||
|
||||
let jmp_location = self.buf.len();
|
||||
let start_offset = ASM::jmp_imm32(&mut self.buf, 0x1234_5678);
|
||||
|
||||
if let Some(offset) = self.join_map.get(id) {
|
||||
let offset = *offset;
|
||||
let mut tmp = bumpalo::vec![in self.env.arena];
|
||||
self.update_jmp_imm32_offset(
|
||||
&mut tmp,
|
||||
jmp_location as u64,
|
||||
start_offset as u64,
|
||||
offset,
|
||||
);
|
||||
Ok(())
|
||||
} else {
|
||||
Err(format!(
|
||||
"Jump: unknown point specified to jump to: {:?}",
|
||||
id
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
fn build_num_abs(
|
||||
&mut self,
|
||||
dst: &Symbol,
|
||||
|
@ -726,6 +918,35 @@ impl<
|
|||
ASM::mov_freg64_imm64(&mut self.buf, &mut self.relocs, reg, val);
|
||||
Ok(())
|
||||
}
|
||||
Literal::Str(x) if x.len() < 16 => {
|
||||
// Load small string.
|
||||
let reg = self.get_tmp_general_reg()?;
|
||||
|
||||
let offset = self.claim_stack_size(16)?;
|
||||
self.symbol_storage_map.insert(
|
||||
*sym,
|
||||
SymbolStorage::Base {
|
||||
offset,
|
||||
size: 16,
|
||||
owned: true,
|
||||
},
|
||||
);
|
||||
let mut bytes = [0; 16];
|
||||
bytes[..x.len()].copy_from_slice(x.as_bytes());
|
||||
bytes[15] = (x.len() as u8) | 0b1000_0000;
|
||||
|
||||
let mut num_bytes = [0; 8];
|
||||
num_bytes.copy_from_slice(&bytes[..8]);
|
||||
let num = i64::from_ne_bytes(num_bytes);
|
||||
ASM::mov_reg64_imm64(&mut self.buf, reg, num);
|
||||
ASM::mov_base32_reg64(&mut self.buf, offset, reg);
|
||||
|
||||
num_bytes.copy_from_slice(&bytes[8..]);
|
||||
let num = i64::from_ne_bytes(num_bytes);
|
||||
ASM::mov_reg64_imm64(&mut self.buf, reg, num);
|
||||
ASM::mov_base32_reg64(&mut self.buf, offset + 8, reg);
|
||||
Ok(())
|
||||
}
|
||||
x => Err(format!("loading literal, {:?}, is not yet implemented", x)),
|
||||
}
|
||||
}
|
||||
|
@ -828,29 +1049,39 @@ impl<
|
|||
fn return_symbol(&mut self, sym: &Symbol, layout: &Layout<'a>) -> Result<(), String> {
|
||||
let val = self.symbol_storage_map.get(sym);
|
||||
match val {
|
||||
Some(SymbolStorage::GeneralReg(reg)) if *reg == CC::GENERAL_RETURN_REGS[0] => Ok(()),
|
||||
Some(SymbolStorage::GeneralReg(reg)) if *reg == CC::GENERAL_RETURN_REGS[0] => {}
|
||||
Some(SymbolStorage::GeneralReg(reg)) => {
|
||||
// If it fits in a general purpose register, just copy it over to.
|
||||
// Technically this can be optimized to produce shorter instructions if less than 64bits.
|
||||
ASM::mov_reg64_reg64(&mut self.buf, CC::GENERAL_RETURN_REGS[0], *reg);
|
||||
Ok(())
|
||||
}
|
||||
Some(SymbolStorage::FloatReg(reg)) if *reg == CC::FLOAT_RETURN_REGS[0] => Ok(()),
|
||||
Some(SymbolStorage::FloatReg(reg)) if *reg == CC::FLOAT_RETURN_REGS[0] => {}
|
||||
Some(SymbolStorage::FloatReg(reg)) => {
|
||||
ASM::mov_freg64_freg64(&mut self.buf, CC::FLOAT_RETURN_REGS[0], *reg);
|
||||
Ok(())
|
||||
}
|
||||
Some(SymbolStorage::Base { offset, size, .. }) => match layout {
|
||||
Layout::Builtin(Builtin::Int64) => {
|
||||
ASM::mov_reg64_base32(&mut self.buf, CC::GENERAL_RETURN_REGS[0], *offset);
|
||||
Ok(())
|
||||
}
|
||||
Layout::Builtin(Builtin::Float64) => {
|
||||
ASM::mov_freg64_base32(&mut self.buf, CC::FLOAT_RETURN_REGS[0], *offset);
|
||||
Ok(())
|
||||
}
|
||||
Layout::Builtin(Builtin::Str) => {
|
||||
if self.symbol_storage_map.contains_key(&Symbol::RET_POINTER) {
|
||||
// This will happen on windows, return via pointer here.
|
||||
return Err("Returning strings via pointer not yet implemented".to_string());
|
||||
} else {
|
||||
ASM::mov_reg64_base32(&mut self.buf, CC::GENERAL_RETURN_REGS[0], *offset);
|
||||
ASM::mov_reg64_base32(
|
||||
&mut self.buf,
|
||||
CC::GENERAL_RETURN_REGS[1],
|
||||
*offset + 8,
|
||||
);
|
||||
}
|
||||
}
|
||||
Layout::Struct(field_layouts) => {
|
||||
let (offset, size) = (*offset, *size);
|
||||
// Nothing to do for empty struct
|
||||
if size > 0 {
|
||||
let ret_reg = if self.symbol_storage_map.contains_key(&Symbol::RET_POINTER)
|
||||
{
|
||||
|
@ -858,23 +1089,34 @@ impl<
|
|||
} else {
|
||||
None
|
||||
};
|
||||
CC::return_struct(&mut self.buf, offset, size, field_layouts, ret_reg)
|
||||
} else {
|
||||
// Nothing to do for empty struct
|
||||
Ok(())
|
||||
CC::return_struct(&mut self.buf, offset, size, field_layouts, ret_reg)?;
|
||||
}
|
||||
}
|
||||
x => Err(format!(
|
||||
"returning symbol with layout, {:?}, is not yet implemented",
|
||||
x
|
||||
)),
|
||||
x => {
|
||||
return Err(format!(
|
||||
"returning symbol with layout, {:?}, is not yet implemented",
|
||||
x
|
||||
));
|
||||
}
|
||||
},
|
||||
Some(x) => Err(format!(
|
||||
"returning symbol storage, {:?}, is not yet implemented",
|
||||
x
|
||||
)),
|
||||
None => Err(format!("Unknown return symbol: {}", sym)),
|
||||
Some(x) => {
|
||||
return Err(format!(
|
||||
"returning symbol storage, {:?}, is not yet implemented",
|
||||
x
|
||||
));
|
||||
}
|
||||
None => {
|
||||
return Err(format!("Unknown return symbol: {}", sym));
|
||||
}
|
||||
}
|
||||
let inst_loc = self.buf.len() as u64;
|
||||
let offset = ASM::jmp_imm32(&mut self.buf, 0x1234_5678) as u64;
|
||||
self.relocs.push(Relocation::JmpToReturn {
|
||||
inst_loc,
|
||||
inst_size: self.buf.len() as u64 - inst_loc,
|
||||
offset,
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1212,4 +1454,72 @@ impl<
|
|||
)),
|
||||
}
|
||||
}
|
||||
|
||||
fn push_used_caller_saved_regs_to_stack(&mut self) -> Result<(), String> {
|
||||
let old_general_used_regs = std::mem::replace(
|
||||
&mut self.general_used_regs,
|
||||
bumpalo::vec![in self.env.arena],
|
||||
);
|
||||
for (reg, saved_sym) in old_general_used_regs.into_iter() {
|
||||
if CC::general_caller_saved(®) {
|
||||
self.general_free_regs.push(reg);
|
||||
self.free_to_stack(&saved_sym)?;
|
||||
} else {
|
||||
self.general_used_regs.push((reg, saved_sym));
|
||||
}
|
||||
}
|
||||
let old_float_used_regs =
|
||||
std::mem::replace(&mut self.float_used_regs, bumpalo::vec![in self.env.arena]);
|
||||
for (reg, saved_sym) in old_float_used_regs.into_iter() {
|
||||
if CC::float_caller_saved(®) {
|
||||
self.float_free_regs.push(reg);
|
||||
self.free_to_stack(&saved_sym)?;
|
||||
} else {
|
||||
self.float_used_regs.push((reg, saved_sym));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Updates a jump instruction to a new offset and returns the number of bytes written.
|
||||
fn update_jmp_imm32_offset(
|
||||
&mut self,
|
||||
tmp: &mut Vec<'a, u8>,
|
||||
jmp_location: u64,
|
||||
base_offset: u64,
|
||||
target_offset: u64,
|
||||
) {
|
||||
tmp.clear();
|
||||
let jmp_offset = target_offset as i32 - base_offset as i32;
|
||||
ASM::jmp_imm32(tmp, jmp_offset);
|
||||
for (i, byte) in tmp.iter().enumerate() {
|
||||
self.buf[jmp_location as usize + i] = *byte;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! single_register_integers {
|
||||
() => {
|
||||
Builtin::Int1
|
||||
| Builtin::Int8
|
||||
| Builtin::Int16
|
||||
| Builtin::Int32
|
||||
| Builtin::Int64
|
||||
| Builtin::Usize
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! single_register_floats {
|
||||
() => {
|
||||
Builtin::Float32 | Builtin::Float64
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! single_register_builtins {
|
||||
() => {
|
||||
single_register_integers!() | single_register_floats!()
|
||||
};
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
use crate::generic64::{Assembler, CallConv, RegTrait, SymbolStorage, PTR_SIZE};
|
||||
use crate::Relocation;
|
||||
use crate::{
|
||||
single_register_builtins, single_register_floats, single_register_integers, Relocation,
|
||||
};
|
||||
use bumpalo::collections::Vec;
|
||||
use roc_collections::all::MutMap;
|
||||
use roc_module::symbol::Symbol;
|
||||
|
@ -175,6 +177,7 @@ impl CallConv<X86_64GeneralReg, X86_64FloatReg> for X86_64SystemV {
|
|||
|
||||
#[inline(always)]
|
||||
fn load_args<'a>(
|
||||
buf: &mut Vec<'a, u8>,
|
||||
symbol_map: &mut MutMap<Symbol, SymbolStorage<X86_64GeneralReg, X86_64FloatReg>>,
|
||||
args: &'a [(Layout<'a>, Symbol)],
|
||||
ret_layout: &Layout<'a>,
|
||||
|
@ -191,7 +194,7 @@ impl CallConv<X86_64GeneralReg, X86_64FloatReg> for X86_64SystemV {
|
|||
}
|
||||
for (layout, sym) in args.iter() {
|
||||
match layout {
|
||||
Layout::Builtin(Builtin::Int64) => {
|
||||
Layout::Builtin(single_register_integers!()) => {
|
||||
if general_i < Self::GENERAL_PARAM_REGS.len() {
|
||||
symbol_map.insert(
|
||||
*sym,
|
||||
|
@ -210,7 +213,7 @@ impl CallConv<X86_64GeneralReg, X86_64FloatReg> for X86_64SystemV {
|
|||
);
|
||||
}
|
||||
}
|
||||
Layout::Builtin(Builtin::Float64) => {
|
||||
Layout::Builtin(single_register_floats!()) => {
|
||||
if float_i < Self::FLOAT_PARAM_REGS.len() {
|
||||
symbol_map.insert(
|
||||
*sym,
|
||||
|
@ -229,6 +232,30 @@ impl CallConv<X86_64GeneralReg, X86_64FloatReg> for X86_64SystemV {
|
|||
);
|
||||
}
|
||||
}
|
||||
Layout::Builtin(Builtin::Str) => {
|
||||
if general_i + 1 < Self::GENERAL_PARAM_REGS.len() {
|
||||
// Load the value to the param reg.
|
||||
let dst1 = Self::GENERAL_PARAM_REGS[general_i];
|
||||
let dst2 = Self::GENERAL_PARAM_REGS[general_i + 1];
|
||||
base_offset += 16;
|
||||
X86_64Assembler::mov_reg64_base32(buf, dst1, base_offset - 8);
|
||||
X86_64Assembler::mov_reg64_base32(buf, dst2, base_offset);
|
||||
symbol_map.insert(
|
||||
*sym,
|
||||
SymbolStorage::Base {
|
||||
offset: base_offset,
|
||||
size: 16,
|
||||
owned: true,
|
||||
},
|
||||
);
|
||||
general_i += 2;
|
||||
} else {
|
||||
return Err(
|
||||
"loading strings args on the stack is not yet implemented".to_string()
|
||||
);
|
||||
}
|
||||
}
|
||||
Layout::Struct(&[]) => {}
|
||||
x => {
|
||||
return Err(format!(
|
||||
"Loading args with layout {:?} not yet implementd",
|
||||
|
@ -254,8 +281,7 @@ impl CallConv<X86_64GeneralReg, X86_64FloatReg> for X86_64SystemV {
|
|||
// For most return layouts we will do nothing.
|
||||
// In some cases, we need to put the return address as the first arg.
|
||||
match ret_layout {
|
||||
Layout::Builtin(Builtin::Int64) => {}
|
||||
Layout::Builtin(Builtin::Float64) => {}
|
||||
Layout::Builtin(single_register_builtins!() | Builtin::Str) => {}
|
||||
x => {
|
||||
return Err(format!(
|
||||
"receiving return type, {:?}, is not yet implemented",
|
||||
|
@ -265,7 +291,7 @@ impl CallConv<X86_64GeneralReg, X86_64FloatReg> for X86_64SystemV {
|
|||
}
|
||||
for (i, layout) in arg_layouts.iter().enumerate() {
|
||||
match layout {
|
||||
Layout::Builtin(Builtin::Int64) => {
|
||||
Layout::Builtin(single_register_integers!()) => {
|
||||
if general_i < Self::GENERAL_PARAM_REGS.len() {
|
||||
// Load the value to the param reg.
|
||||
let dst = Self::GENERAL_PARAM_REGS[general_i];
|
||||
|
@ -319,7 +345,7 @@ impl CallConv<X86_64GeneralReg, X86_64FloatReg> for X86_64SystemV {
|
|||
stack_offset += 8;
|
||||
}
|
||||
}
|
||||
Layout::Builtin(Builtin::Float64) => {
|
||||
Layout::Builtin(single_register_floats!()) => {
|
||||
if float_i < Self::FLOAT_PARAM_REGS.len() {
|
||||
// Load the value to the param reg.
|
||||
let dst = Self::FLOAT_PARAM_REGS[float_i];
|
||||
|
@ -371,6 +397,33 @@ impl CallConv<X86_64GeneralReg, X86_64FloatReg> for X86_64SystemV {
|
|||
stack_offset += 8;
|
||||
}
|
||||
}
|
||||
Layout::Builtin(Builtin::Str) => {
|
||||
if general_i + 1 < Self::GENERAL_PARAM_REGS.len() {
|
||||
// Load the value to the param reg.
|
||||
let dst1 = Self::GENERAL_PARAM_REGS[general_i];
|
||||
let dst2 = Self::GENERAL_PARAM_REGS[general_i + 1];
|
||||
match symbol_map
|
||||
.get(&args[i])
|
||||
.ok_or("function argument does not reference any symbol")?
|
||||
{
|
||||
SymbolStorage::Base { offset, .. } => {
|
||||
X86_64Assembler::mov_reg64_base32(buf, dst1, *offset);
|
||||
X86_64Assembler::mov_reg64_base32(buf, dst2, *offset + 8);
|
||||
}
|
||||
_ => {
|
||||
return Err("Strings only support being loaded from base offsets"
|
||||
.to_string());
|
||||
}
|
||||
}
|
||||
general_i += 2;
|
||||
} else {
|
||||
return Err(
|
||||
"calling functions with strings on the stack is not yet implemented"
|
||||
.to_string(),
|
||||
);
|
||||
}
|
||||
}
|
||||
Layout::Struct(&[]) => {}
|
||||
x => {
|
||||
return Err(format!(
|
||||
"calling with arg type, {:?}, is not yet implemented",
|
||||
|
@ -513,6 +566,7 @@ impl CallConv<X86_64GeneralReg, X86_64FloatReg> for X86_64WindowsFastcall {
|
|||
|
||||
#[inline(always)]
|
||||
fn load_args<'a>(
|
||||
_buf: &mut Vec<'a, u8>,
|
||||
symbol_map: &mut MutMap<Symbol, SymbolStorage<X86_64GeneralReg, X86_64FloatReg>>,
|
||||
args: &'a [(Layout<'a>, Symbol)],
|
||||
ret_layout: &Layout<'a>,
|
||||
|
@ -529,13 +583,23 @@ impl CallConv<X86_64GeneralReg, X86_64FloatReg> for X86_64WindowsFastcall {
|
|||
for (layout, sym) in args.iter() {
|
||||
if i < Self::GENERAL_PARAM_REGS.len() {
|
||||
match layout {
|
||||
Layout::Builtin(Builtin::Int64) => {
|
||||
Layout::Builtin(single_register_integers!()) => {
|
||||
symbol_map
|
||||
.insert(*sym, SymbolStorage::GeneralReg(Self::GENERAL_PARAM_REGS[i]));
|
||||
i += 1;
|
||||
}
|
||||
Layout::Builtin(Builtin::Float64) => {
|
||||
Layout::Builtin(single_register_floats!()) => {
|
||||
symbol_map.insert(*sym, SymbolStorage::FloatReg(Self::FLOAT_PARAM_REGS[i]));
|
||||
i += 1;
|
||||
}
|
||||
Layout::Builtin(Builtin::Str) => {
|
||||
// I think this just needs to be passed on the stack, so not a huge deal.
|
||||
return Err(
|
||||
"Passing str args with Windows fast call not yet implemented."
|
||||
.to_string(),
|
||||
);
|
||||
}
|
||||
Layout::Struct(&[]) => {}
|
||||
x => {
|
||||
return Err(format!(
|
||||
"Loading args with layout {:?} not yet implementd",
|
||||
|
@ -543,11 +607,9 @@ impl CallConv<X86_64GeneralReg, X86_64FloatReg> for X86_64WindowsFastcall {
|
|||
));
|
||||
}
|
||||
}
|
||||
i += 1;
|
||||
} else {
|
||||
base_offset += match layout {
|
||||
Layout::Builtin(Builtin::Int64) => 8,
|
||||
Layout::Builtin(Builtin::Float64) => 8,
|
||||
Layout::Builtin(single_register_builtins!()) => 8,
|
||||
x => {
|
||||
return Err(format!(
|
||||
"Loading args with layout {:?} not yet implemented",
|
||||
|
@ -577,12 +639,10 @@ impl CallConv<X86_64GeneralReg, X86_64FloatReg> for X86_64WindowsFastcall {
|
|||
ret_layout: &Layout<'a>,
|
||||
) -> Result<u32, String> {
|
||||
let mut stack_offset = Self::SHADOW_SPACE_SIZE as i32;
|
||||
let mut reg_i = 0;
|
||||
// For most return layouts we will do nothing.
|
||||
// In some cases, we need to put the return address as the first arg.
|
||||
match ret_layout {
|
||||
Layout::Builtin(Builtin::Int64) => {}
|
||||
Layout::Builtin(Builtin::Float64) => {}
|
||||
Layout::Builtin(single_register_builtins!()) => {}
|
||||
x => {
|
||||
return Err(format!(
|
||||
"receiving return type, {:?}, is not yet implemented",
|
||||
|
@ -592,10 +652,10 @@ impl CallConv<X86_64GeneralReg, X86_64FloatReg> for X86_64WindowsFastcall {
|
|||
}
|
||||
for (i, layout) in arg_layouts.iter().enumerate() {
|
||||
match layout {
|
||||
Layout::Builtin(Builtin::Int64) => {
|
||||
Layout::Builtin(single_register_integers!()) => {
|
||||
if i < Self::GENERAL_PARAM_REGS.len() {
|
||||
// Load the value to the param reg.
|
||||
let dst = Self::GENERAL_PARAM_REGS[reg_i];
|
||||
let dst = Self::GENERAL_PARAM_REGS[i];
|
||||
match symbol_map
|
||||
.get(&args[i])
|
||||
.ok_or("function argument does not reference any symbol")?
|
||||
|
@ -613,7 +673,6 @@ impl CallConv<X86_64GeneralReg, X86_64FloatReg> for X86_64WindowsFastcall {
|
|||
)
|
||||
}
|
||||
}
|
||||
reg_i += 1;
|
||||
} else {
|
||||
// Load the value to the stack.
|
||||
match symbol_map
|
||||
|
@ -646,10 +705,10 @@ impl CallConv<X86_64GeneralReg, X86_64FloatReg> for X86_64WindowsFastcall {
|
|||
stack_offset += 8;
|
||||
}
|
||||
}
|
||||
Layout::Builtin(Builtin::Float64) => {
|
||||
Layout::Builtin(single_register_floats!()) => {
|
||||
if i < Self::FLOAT_PARAM_REGS.len() {
|
||||
// Load the value to the param reg.
|
||||
let dst = Self::FLOAT_PARAM_REGS[reg_i];
|
||||
let dst = Self::FLOAT_PARAM_REGS[i];
|
||||
match symbol_map
|
||||
.get(&args[i])
|
||||
.ok_or("function argument does not reference any symbol")?
|
||||
|
@ -666,7 +725,6 @@ impl CallConv<X86_64GeneralReg, X86_64FloatReg> for X86_64WindowsFastcall {
|
|||
return Err("Cannot load general symbol into FloatReg".to_string())
|
||||
}
|
||||
}
|
||||
reg_i += 1;
|
||||
} else {
|
||||
// Load the value to the stack.
|
||||
match symbol_map
|
||||
|
@ -698,6 +756,13 @@ impl CallConv<X86_64GeneralReg, X86_64FloatReg> for X86_64WindowsFastcall {
|
|||
stack_offset += 8;
|
||||
}
|
||||
}
|
||||
Layout::Builtin(Builtin::Str) => {
|
||||
// I think this just needs to be passed on the stack, so not a huge deal.
|
||||
return Err(
|
||||
"Passing str args with Windows fast call not yet implemented.".to_string(),
|
||||
);
|
||||
}
|
||||
Layout::Struct(&[]) => {}
|
||||
x => {
|
||||
return Err(format!(
|
||||
"calling with arg type, {:?}, is not yet implemented",
|
||||
|
|
|
@ -9,10 +9,10 @@ use roc_module::ident::{ModuleName, TagName};
|
|||
use roc_module::low_level::LowLevel;
|
||||
use roc_module::symbol::{Interns, Symbol};
|
||||
use roc_mono::ir::{
|
||||
BranchInfo, CallType, Expr, JoinPointId, ListLiteralElement, Literal, Proc, Stmt,
|
||||
BranchInfo, CallType, Expr, JoinPointId, ListLiteralElement, Literal, Param, Proc,
|
||||
SelfRecursive, Stmt,
|
||||
};
|
||||
use roc_mono::layout::{Builtin, Layout, LayoutIds};
|
||||
use target_lexicon::Triple;
|
||||
|
||||
mod generic64;
|
||||
mod object_builder;
|
||||
|
@ -46,6 +46,11 @@ pub enum Relocation {
|
|||
offset: u64,
|
||||
name: String,
|
||||
},
|
||||
JmpToReturn {
|
||||
inst_loc: u64,
|
||||
inst_size: u64,
|
||||
offset: u64,
|
||||
},
|
||||
}
|
||||
|
||||
trait Backend<'a>
|
||||
|
@ -53,12 +58,13 @@ where
|
|||
Self: Sized,
|
||||
{
|
||||
/// new creates a new backend that will output to the specific Object.
|
||||
fn new(env: &'a Env, target: &Triple) -> Result<Self, String>;
|
||||
fn new(env: &'a Env) -> Result<Self, String>;
|
||||
|
||||
fn env(&self) -> &'a Env<'a>;
|
||||
|
||||
/// reset resets any registers or other values that may be occupied at the end of a procedure.
|
||||
fn reset(&mut self);
|
||||
/// It also passes basic procedure information to the builder for setup of the next function.
|
||||
fn reset(&mut self, name: String, is_self_recursive: SelfRecursive);
|
||||
|
||||
/// finalize does any setup and cleanup that should happen around the procedure.
|
||||
/// finalize does setup because things like stack size and jump locations are not know until the function is written.
|
||||
|
@ -79,17 +85,16 @@ where
|
|||
|
||||
/// build_proc creates a procedure and outputs it to the wrapped object writer.
|
||||
fn build_proc(&mut self, proc: Proc<'a>) -> Result<(&'a [u8], &[Relocation]), String> {
|
||||
self.reset();
|
||||
let proc_name = LayoutIds::default()
|
||||
.get(proc.name, &proc.ret_layout)
|
||||
.to_symbol_string(proc.name, &self.env().interns);
|
||||
self.reset(proc_name, proc.is_self_recursive);
|
||||
self.load_args(proc.args, &proc.ret_layout)?;
|
||||
for (layout, sym) in proc.args {
|
||||
self.set_layout_map(*sym, layout)?;
|
||||
}
|
||||
// let start = std::time::Instant::now();
|
||||
self.scan_ast(&proc.body);
|
||||
self.create_free_map();
|
||||
// let duration = start.elapsed();
|
||||
// println!("Time to calculate lifetimes: {:?}", duration);
|
||||
// println!("{:?}", self.last_seen_map());
|
||||
self.build_stmt(&proc.body, &proc.ret_layout)?;
|
||||
self.finalize()
|
||||
}
|
||||
|
@ -110,6 +115,11 @@ where
|
|||
self.free_symbols(stmt)?;
|
||||
Ok(())
|
||||
}
|
||||
Stmt::Refcounting(_modify, following) => {
|
||||
// TODO: actually deal with refcounting. For hello world, we just skipped it.
|
||||
self.build_stmt(following, ret_layout)?;
|
||||
Ok(())
|
||||
}
|
||||
Stmt::Switch {
|
||||
cond_symbol,
|
||||
cond_layout,
|
||||
|
@ -128,6 +138,35 @@ where
|
|||
self.free_symbols(stmt)?;
|
||||
Ok(())
|
||||
}
|
||||
Stmt::Join {
|
||||
id,
|
||||
parameters,
|
||||
body,
|
||||
remainder,
|
||||
} => {
|
||||
for param in parameters.iter() {
|
||||
self.set_layout_map(param.symbol, ¶m.layout)?;
|
||||
}
|
||||
self.build_join(id, parameters, body, remainder, ret_layout)?;
|
||||
self.free_symbols(stmt)?;
|
||||
Ok(())
|
||||
}
|
||||
Stmt::Jump(id, args) => {
|
||||
let mut arg_layouts: bumpalo::collections::Vec<Layout<'a>> =
|
||||
bumpalo::vec![in self.env().arena];
|
||||
arg_layouts.reserve(args.len());
|
||||
let layout_map = self.layout_map();
|
||||
for arg in *args {
|
||||
if let Some(layout) = layout_map.get(arg) {
|
||||
arg_layouts.push(*layout);
|
||||
} else {
|
||||
return Err(format!("the argument, {:?}, has no know layout", arg));
|
||||
}
|
||||
}
|
||||
self.build_jump(id, args, arg_layouts.into_bump_slice(), ret_layout)?;
|
||||
self.free_symbols(stmt)?;
|
||||
Ok(())
|
||||
}
|
||||
x => Err(format!("the statement, {:?}, is not yet implemented", x)),
|
||||
}
|
||||
}
|
||||
|
@ -141,6 +180,25 @@ where
|
|||
ret_layout: &Layout<'a>,
|
||||
) -> Result<(), String>;
|
||||
|
||||
// build_join generates a instructions for a join statement.
|
||||
fn build_join(
|
||||
&mut self,
|
||||
id: &JoinPointId,
|
||||
parameters: &'a [Param<'a>],
|
||||
body: &'a Stmt<'a>,
|
||||
remainder: &'a Stmt<'a>,
|
||||
ret_layout: &Layout<'a>,
|
||||
) -> Result<(), String>;
|
||||
|
||||
// build_jump generates a instructions for a jump statement.
|
||||
fn build_jump(
|
||||
&mut self,
|
||||
id: &JoinPointId,
|
||||
args: &'a [Symbol],
|
||||
arg_layouts: &[Layout<'a>],
|
||||
ret_layout: &Layout<'a>,
|
||||
) -> Result<(), String>;
|
||||
|
||||
/// build_expr builds the expressions for the specified symbol.
|
||||
/// The builder must keep track of the symbol because it may be referred to later.
|
||||
fn build_expr(
|
||||
|
@ -241,6 +299,13 @@ where
|
|||
arg_layouts,
|
||||
ret_layout,
|
||||
),
|
||||
Symbol::STR_CONCAT => self.build_run_low_level(
|
||||
sym,
|
||||
&LowLevel::StrConcat,
|
||||
arguments,
|
||||
arg_layouts,
|
||||
ret_layout,
|
||||
),
|
||||
x if x
|
||||
.module_string(&self.env().interns)
|
||||
.starts_with(ModuleName::APP) =>
|
||||
|
@ -263,8 +328,7 @@ where
|
|||
let layout_map = self.layout_map();
|
||||
for arg in *arguments {
|
||||
if let Some(layout) = layout_map.get(arg) {
|
||||
// This is safe because every value in the map is always set with a valid layout and cannot be null.
|
||||
arg_layouts.push(unsafe { *(*layout) });
|
||||
arg_layouts.push(*layout);
|
||||
} else {
|
||||
return Err(format!("the argument, {:?}, has no know layout", arg));
|
||||
}
|
||||
|
@ -414,6 +478,13 @@ where
|
|||
arg_layouts,
|
||||
ret_layout,
|
||||
),
|
||||
LowLevel::StrConcat => self.build_fn_call(
|
||||
sym,
|
||||
bitcode::STR_CONCAT.to_string(),
|
||||
args,
|
||||
arg_layouts,
|
||||
ret_layout,
|
||||
),
|
||||
x => Err(format!("low level, {:?}. is not yet implemented", x)),
|
||||
}
|
||||
}
|
||||
|
@ -507,7 +578,7 @@ where
|
|||
/// load_literal sets a symbol to be equal to a literal.
|
||||
fn load_literal(&mut self, sym: &Symbol, lit: &Literal<'a>) -> Result<(), String>;
|
||||
|
||||
/// return_symbol moves a symbol to the correct return location for the backend.
|
||||
/// return_symbol moves a symbol to the correct return location for the backend and adds a jump to the end of the function.
|
||||
fn return_symbol(&mut self, sym: &Symbol, layout: &Layout<'a>) -> Result<(), String>;
|
||||
|
||||
/// free_symbols will free all symbols for the given statement.
|
||||
|
@ -542,12 +613,10 @@ where
|
|||
|
||||
/// set_layout_map sets the layout for a specific symbol.
|
||||
fn set_layout_map(&mut self, sym: Symbol, layout: &Layout<'a>) -> Result<(), String> {
|
||||
if let Some(x) = self.layout_map().insert(sym, layout) {
|
||||
if let Some(old_layout) = self.layout_map().insert(sym, *layout) {
|
||||
// Layout map already contains the symbol. We should never need to overwrite.
|
||||
// If the layout is not the same, that is a bug.
|
||||
// There is always an old layout value and this dereference is safe.
|
||||
let old_layout = unsafe { *x };
|
||||
if old_layout != *layout {
|
||||
if &old_layout != layout {
|
||||
Err(format!(
|
||||
"Overwriting layout for symbol, {:?}. This should never happen. got {:?}, want {:?}",
|
||||
sym, layout, old_layout
|
||||
|
@ -561,7 +630,7 @@ where
|
|||
}
|
||||
|
||||
/// layout_map gets the map from symbol to layout.
|
||||
fn layout_map(&mut self) -> &mut MutMap<Symbol, *const Layout<'a>>;
|
||||
fn layout_map(&mut self) -> &mut MutMap<Symbol, Layout<'a>>;
|
||||
|
||||
fn create_free_map(&mut self) {
|
||||
let mut free_map = MutMap::default();
|
||||
|
|
|
@ -34,7 +34,7 @@ pub fn build_module<'a>(
|
|||
x86_64::X86_64FloatReg,
|
||||
x86_64::X86_64Assembler,
|
||||
x86_64::X86_64SystemV,
|
||||
> = Backend::new(env, target)?;
|
||||
> = Backend::new(env)?;
|
||||
build_object(
|
||||
env,
|
||||
procedures,
|
||||
|
@ -52,7 +52,7 @@ pub fn build_module<'a>(
|
|||
x86_64::X86_64FloatReg,
|
||||
x86_64::X86_64Assembler,
|
||||
x86_64::X86_64SystemV,
|
||||
> = Backend::new(env, target)?;
|
||||
> = Backend::new(env)?;
|
||||
build_object(
|
||||
env,
|
||||
procedures,
|
||||
|
@ -74,7 +74,7 @@ pub fn build_module<'a>(
|
|||
aarch64::AArch64FloatReg,
|
||||
aarch64::AArch64Assembler,
|
||||
aarch64::AArch64Call,
|
||||
> = Backend::new(env, target)?;
|
||||
> = Backend::new(env)?;
|
||||
build_object(
|
||||
env,
|
||||
procedures,
|
||||
|
@ -191,10 +191,16 @@ fn build_object<'a, B: Backend<'a>>(
|
|||
let mut layout_ids = roc_mono::layout::LayoutIds::default();
|
||||
let mut procs = Vec::with_capacity_in(procedures.len(), env.arena);
|
||||
for ((sym, layout), proc) in procedures {
|
||||
let fn_name = layout_ids
|
||||
let base_name = layout_ids
|
||||
.get_toplevel(sym, &layout)
|
||||
.to_symbol_string(sym, &env.interns);
|
||||
|
||||
let fn_name = if env.exposed_to_host.contains(&sym) {
|
||||
format!("roc_{}_exposed", base_name)
|
||||
} else {
|
||||
base_name
|
||||
};
|
||||
|
||||
let section_id = output.add_section(
|
||||
output.segment_name(StandardSegment::Text).to_vec(),
|
||||
format!(".text.{:x}", sym.as_u64()).as_bytes().to_vec(),
|
||||
|
@ -298,6 +304,7 @@ fn build_object<'a, B: Backend<'a>>(
|
|||
return Err(format!("failed to find fn symbol for {:?}", name));
|
||||
}
|
||||
}
|
||||
Relocation::JmpToReturn { .. } => unreachable!(),
|
||||
};
|
||||
relocations.push((section_id, elfreloc));
|
||||
}
|
||||
|
|
|
@ -1,12 +1,6 @@
|
|||
#[macro_use]
|
||||
extern crate pretty_assertions;
|
||||
|
||||
#[macro_use]
|
||||
extern crate indoc;
|
||||
|
||||
extern crate bumpalo;
|
||||
extern crate libc;
|
||||
|
||||
#[macro_use]
|
||||
mod helpers;
|
||||
|
||||
|
@ -281,6 +275,24 @@ mod dev_num {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gen_fast_fib_fn() {
|
||||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
fib = \n, a, b ->
|
||||
if n == 0 then
|
||||
a
|
||||
else
|
||||
fib (n - 1) b (a + b)
|
||||
fib 10 0 1
|
||||
"#
|
||||
),
|
||||
55,
|
||||
i64
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn f64_abs() {
|
||||
assert_evals_to!("Num.abs -4.7", 4.7, f64);
|
||||
|
@ -580,18 +592,18 @@ mod dev_num {
|
|||
// 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() {
|
||||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
1 + 3 * 7 - 2
|
||||
"#
|
||||
),
|
||||
20,
|
||||
i64
|
||||
);
|
||||
}
|
||||
|
||||
// #[test]
|
||||
// fn gen_order_of_arithmetic_ops_complex_float() {
|
||||
|
@ -606,59 +618,59 @@ mod dev_num {
|
|||
// );
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn if_guard_bind_variable_false() {
|
||||
// assert_evals_to!(
|
||||
// indoc!(
|
||||
// r#"
|
||||
// wrapper = \{} ->
|
||||
// when 10 is
|
||||
// x if x == 5 -> 0
|
||||
// _ -> 42
|
||||
#[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
|
||||
// );
|
||||
// }
|
||||
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
|
||||
#[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
|
||||
// );
|
||||
// }
|
||||
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)
|
||||
#[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
|
||||
// );
|
||||
// }
|
||||
sum 1_000_000 0
|
||||
"#
|
||||
),
|
||||
500000500000,
|
||||
i64
|
||||
);
|
||||
}
|
||||
|
||||
// #[test]
|
||||
// fn int_negate() {
|
||||
|
|
954
compiler/gen_dev/tests/dev_str.rs
Normal file
954
compiler/gen_dev/tests/dev_str.rs
Normal file
|
@ -0,0 +1,954 @@
|
|||
// #[macro_use]
|
||||
// extern crate indoc;
|
||||
|
||||
#[macro_use]
|
||||
mod helpers;
|
||||
|
||||
#[cfg(all(test, any(target_os = "linux", target_os = "macos"), any(target_arch = "x86_64"/*, target_arch = "aarch64"*/)))]
|
||||
mod dev_str {
|
||||
// use roc_std::{RocList, RocStr};
|
||||
// #[test]
|
||||
// fn str_split_bigger_delimiter_small_str() {
|
||||
// assert_evals_to!(
|
||||
// indoc!(
|
||||
// r#"
|
||||
// List.len (Str.split "hello" "JJJJ there")
|
||||
// "#
|
||||
// ),
|
||||
// 1,
|
||||
// i64
|
||||
// );
|
||||
|
||||
// assert_evals_to!(
|
||||
// indoc!(
|
||||
// r#"
|
||||
// when List.first (Str.split "JJJ" "JJJJ there") is
|
||||
// Ok str ->
|
||||
// Str.countGraphemes str
|
||||
|
||||
// _ ->
|
||||
// -1
|
||||
|
||||
// "#
|
||||
// ),
|
||||
// 3,
|
||||
// i64
|
||||
// );
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_split_str_concat_repeated() {
|
||||
// assert_evals_to!(
|
||||
// indoc!(
|
||||
// r#"
|
||||
// when List.first (Str.split "JJJJJ" "JJJJ there") is
|
||||
// Ok str ->
|
||||
// str
|
||||
// |> Str.concat str
|
||||
// |> Str.concat str
|
||||
// |> Str.concat str
|
||||
// |> Str.concat str
|
||||
|
||||
// _ ->
|
||||
// "Not Str!"
|
||||
|
||||
// "#
|
||||
// ),
|
||||
// RocStr::from_slice(b"JJJJJJJJJJJJJJJJJJJJJJJJJ"),
|
||||
// RocStr
|
||||
// );
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_split_small_str_bigger_delimiter() {
|
||||
// assert_evals_to!(
|
||||
// indoc!(
|
||||
// r#"
|
||||
// when
|
||||
// List.first
|
||||
// (Str.split "JJJ" "0123456789abcdefghi")
|
||||
// is
|
||||
// Ok str -> str
|
||||
// _ -> ""
|
||||
// "#
|
||||
// ),
|
||||
// RocStr::from_slice(b"JJJ"),
|
||||
// RocStr
|
||||
// );
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_split_big_str_small_delimiter() {
|
||||
// assert_evals_to!(
|
||||
// indoc!(
|
||||
// r#"
|
||||
// Str.split "01234567789abcdefghi?01234567789abcdefghi" "?"
|
||||
// "#
|
||||
// ),
|
||||
// RocList::from_slice(&[
|
||||
// RocStr::from_slice(b"01234567789abcdefghi"),
|
||||
// RocStr::from_slice(b"01234567789abcdefghi")
|
||||
// ]),
|
||||
// RocList<RocStr>
|
||||
// );
|
||||
|
||||
// assert_evals_to!(
|
||||
// indoc!(
|
||||
// r#"
|
||||
// Str.split "01234567789abcdefghi 3ch 01234567789abcdefghi" "3ch"
|
||||
// "#
|
||||
// ),
|
||||
// RocList::from_slice(&[
|
||||
// RocStr::from_slice(b"01234567789abcdefghi "),
|
||||
// RocStr::from_slice(b" 01234567789abcdefghi")
|
||||
// ]),
|
||||
// RocList<RocStr>
|
||||
// );
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_split_small_str_small_delimiter() {
|
||||
// assert_evals_to!(
|
||||
// indoc!(
|
||||
// r#"
|
||||
// Str.split "J!J!J" "!"
|
||||
// "#
|
||||
// ),
|
||||
// RocList::from_slice(&[
|
||||
// RocStr::from_slice(b"J"),
|
||||
// RocStr::from_slice(b"J"),
|
||||
// RocStr::from_slice(b"J")
|
||||
// ]),
|
||||
// RocList<RocStr>
|
||||
// );
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_split_bigger_delimiter_big_strs() {
|
||||
// assert_evals_to!(
|
||||
// indoc!(
|
||||
// r#"
|
||||
// Str.split
|
||||
// "string to split is shorter"
|
||||
// "than the delimiter which happens to be very very long"
|
||||
// "#
|
||||
// ),
|
||||
// RocList::from_slice(&[RocStr::from_slice(b"string to split is shorter")]),
|
||||
// RocList<RocStr>
|
||||
// );
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_split_empty_strs() {
|
||||
// assert_evals_to!(
|
||||
// indoc!(
|
||||
// r#"
|
||||
// Str.split "" ""
|
||||
// "#
|
||||
// ),
|
||||
// RocList::from_slice(&[RocStr::from_slice(b"")]),
|
||||
// RocList<RocStr>
|
||||
// );
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_split_minimal_example() {
|
||||
// assert_evals_to!(
|
||||
// indoc!(
|
||||
// r#"
|
||||
// Str.split "a," ","
|
||||
// "#
|
||||
// ),
|
||||
// RocList::from_slice(&[RocStr::from_slice(b"a"), RocStr::from_slice(b"")]),
|
||||
// RocList<RocStr>
|
||||
// )
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_split_small_str_big_delimiter() {
|
||||
// assert_evals_to!(
|
||||
// indoc!(
|
||||
// r#"
|
||||
// Str.split
|
||||
// "1---- ---- ---- ---- ----2---- ---- ---- ---- ----"
|
||||
// "---- ---- ---- ---- ----"
|
||||
// |> List.len
|
||||
// "#
|
||||
// ),
|
||||
// 3,
|
||||
// i64
|
||||
// );
|
||||
|
||||
// assert_evals_to!(
|
||||
// indoc!(
|
||||
// r#"
|
||||
// Str.split
|
||||
// "1---- ---- ---- ---- ----2---- ---- ---- ---- ----"
|
||||
// "---- ---- ---- ---- ----"
|
||||
// "#
|
||||
// ),
|
||||
// RocList::from_slice(&[
|
||||
// RocStr::from_slice(b"1"),
|
||||
// RocStr::from_slice(b"2"),
|
||||
// RocStr::from_slice(b"")
|
||||
// ]),
|
||||
// RocList<RocStr>
|
||||
// );
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_split_small_str_20_char_delimiter() {
|
||||
// assert_evals_to!(
|
||||
// indoc!(
|
||||
// r#"
|
||||
// Str.split
|
||||
// "3|-- -- -- -- -- -- |4|-- -- -- -- -- -- |"
|
||||
// "|-- -- -- -- -- -- |"
|
||||
// "#
|
||||
// ),
|
||||
// RocList::from_slice(&[
|
||||
// RocStr::from_slice(b"3"),
|
||||
// RocStr::from_slice(b"4"),
|
||||
// RocStr::from_slice(b"")
|
||||
// ]),
|
||||
// RocList<RocStr>
|
||||
// );
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_concat_big_to_big() {
|
||||
// assert_evals_to!(
|
||||
// indoc!(
|
||||
// r#"
|
||||
// Str.concat
|
||||
// "First string that is fairly long. Longer strings make for different errors. "
|
||||
// "Second string that is also fairly long. Two long strings test things that might not appear with short strings."
|
||||
// "#
|
||||
// ),
|
||||
// RocStr::from_slice(b"First string that is fairly long. Longer strings make for different errors. Second string that is also fairly long. Two long strings test things that might not appear with short strings."),
|
||||
// RocStr
|
||||
// );
|
||||
// }
|
||||
|
||||
#[test]
|
||||
fn small_str_literal() {
|
||||
assert_evals_to!(
|
||||
"\"JJJJJJJJJJJJJJJ\"",
|
||||
[
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0b1000_1111
|
||||
],
|
||||
[u8; 16]
|
||||
);
|
||||
}
|
||||
|
||||
// #[test]
|
||||
// fn small_str_zeroed_literal() {
|
||||
// // Verifies that we zero out unused bytes in the string.
|
||||
// // This is important so that string equality tests don't randomly
|
||||
// // fail due to unused memory being there!
|
||||
// assert_evals_to!(
|
||||
// "\"J\"",
|
||||
// [
|
||||
// 0x4a,
|
||||
// 0x00,
|
||||
// 0x00,
|
||||
// 0x00,
|
||||
// 0x00,
|
||||
// 0x00,
|
||||
// 0x00,
|
||||
// 0x00,
|
||||
// 0x00,
|
||||
// 0x00,
|
||||
// 0x00,
|
||||
// 0x00,
|
||||
// 0x00,
|
||||
// 0x00,
|
||||
// 0x00,
|
||||
// 0b1000_0001
|
||||
// ],
|
||||
// [u8; 16]
|
||||
// );
|
||||
// }
|
||||
|
||||
#[test]
|
||||
fn small_str_concat_empty_first_arg() {
|
||||
assert_evals_to!(
|
||||
r#"Str.concat "" "JJJJJJJJJJJJJJJ""#,
|
||||
[
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0b1000_1111
|
||||
],
|
||||
[u8; 16]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn small_str_concat_empty_second_arg() {
|
||||
assert_evals_to!(
|
||||
r#"Str.concat "JJJJJJJJJJJJJJJ" """#,
|
||||
[
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0b1000_1111
|
||||
],
|
||||
[u8; 16]
|
||||
);
|
||||
}
|
||||
|
||||
// #[test]
|
||||
// fn small_str_concat_small_to_big() {
|
||||
// assert_evals_to!(
|
||||
// r#"Str.concat "abc" " this is longer than 15 chars""#,
|
||||
// RocStr::from_slice(b"abc this is longer than 15 chars"),
|
||||
// RocStr
|
||||
// );
|
||||
// }
|
||||
|
||||
#[test]
|
||||
fn small_str_concat_small_to_small_staying_small() {
|
||||
assert_evals_to!(
|
||||
r#"Str.concat "J" "JJJJJJJJJJJJJJ""#,
|
||||
[
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0x4a,
|
||||
0b1000_1111
|
||||
],
|
||||
[u8; 16]
|
||||
);
|
||||
}
|
||||
|
||||
// #[test]
|
||||
// fn small_str_concat_small_to_small_overflow_to_big() {
|
||||
// assert_evals_to!(
|
||||
// r#"Str.concat "abcdefghijklm" "nopqrstuvwxyz""#,
|
||||
// RocStr::from_slice(b"abcdefghijklmnopqrstuvwxyz"),
|
||||
// RocStr
|
||||
// );
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_concat_empty() {
|
||||
// assert_evals_to!(r#"Str.concat "" """#, RocStr::default(), RocStr);
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn small_str_is_empty() {
|
||||
// assert_evals_to!(r#"Str.isEmpty "abc""#, false, bool);
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn big_str_is_empty() {
|
||||
// assert_evals_to!(
|
||||
// r#"Str.isEmpty "this is more than 15 chars long""#,
|
||||
// false,
|
||||
// bool
|
||||
// );
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn empty_str_is_empty() {
|
||||
// assert_evals_to!(r#"Str.isEmpty """#, true, bool);
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_starts_with() {
|
||||
// assert_evals_to!(r#"Str.startsWith "hello world" "hell""#, true, bool);
|
||||
// assert_evals_to!(r#"Str.startsWith "hello world" """#, true, bool);
|
||||
// assert_evals_to!(r#"Str.startsWith "nope" "hello world""#, false, bool);
|
||||
// assert_evals_to!(r#"Str.startsWith "hell" "hello world""#, false, bool);
|
||||
// assert_evals_to!(r#"Str.startsWith "" "hello world""#, false, bool);
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_starts_with_code_point() {
|
||||
// assert_evals_to!(
|
||||
// &format!(r#"Str.startsWithCodePt "foobar" {}"#, 'f' as u32),
|
||||
// true,
|
||||
// bool
|
||||
// );
|
||||
// assert_evals_to!(
|
||||
// &format!(r#"Str.startsWithCodePt "zoobar" {}"#, 'f' as u32),
|
||||
// false,
|
||||
// bool
|
||||
// );
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_ends_with() {
|
||||
// assert_evals_to!(r#"Str.endsWith "hello world" "world""#, true, bool);
|
||||
// assert_evals_to!(r#"Str.endsWith "nope" "hello world""#, false, bool);
|
||||
// assert_evals_to!(r#"Str.endsWith "" "hello world""#, false, bool);
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_count_graphemes_small_str() {
|
||||
// assert_evals_to!(r#"Str.countGraphemes "å🤔""#, 2, usize);
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_count_graphemes_three_js() {
|
||||
// assert_evals_to!(r#"Str.countGraphemes "JJJ""#, 3, usize);
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_count_graphemes_big_str() {
|
||||
// assert_evals_to!(
|
||||
// r#"Str.countGraphemes "6🤔å🤔e¥🤔çppkd🙃1jdal🦯asdfa∆ltråø˚waia8918.,🏅jjc""#,
|
||||
// 45,
|
||||
// usize
|
||||
// );
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_starts_with_same_big_str() {
|
||||
// assert_evals_to!(
|
||||
// r#"Str.startsWith "123456789123456789" "123456789123456789""#,
|
||||
// true,
|
||||
// bool
|
||||
// );
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_starts_with_different_big_str() {
|
||||
// assert_evals_to!(
|
||||
// r#"Str.startsWith "12345678912345678910" "123456789123456789""#,
|
||||
// true,
|
||||
// bool
|
||||
// );
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_starts_with_same_small_str() {
|
||||
// assert_evals_to!(r#"Str.startsWith "1234" "1234""#, true, bool);
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_starts_with_different_small_str() {
|
||||
// assert_evals_to!(r#"Str.startsWith "1234" "12""#, true, bool);
|
||||
// }
|
||||
// #[test]
|
||||
// fn str_starts_with_false_small_str() {
|
||||
// assert_evals_to!(r#"Str.startsWith "1234" "23""#, false, bool);
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_from_int() {
|
||||
// assert_evals_to!(
|
||||
// r#"Str.fromInt 1234"#,
|
||||
// roc_std::RocStr::from_slice("1234".as_bytes()),
|
||||
// roc_std::RocStr
|
||||
// );
|
||||
// assert_evals_to!(
|
||||
// r#"Str.fromInt 0"#,
|
||||
// roc_std::RocStr::from_slice("0".as_bytes()),
|
||||
// roc_std::RocStr
|
||||
// );
|
||||
// assert_evals_to!(
|
||||
// r#"Str.fromInt -1"#,
|
||||
// roc_std::RocStr::from_slice("-1".as_bytes()),
|
||||
// roc_std::RocStr
|
||||
// );
|
||||
|
||||
// let max = format!("{}", i64::MAX);
|
||||
// assert_evals_to!(
|
||||
// r#"Str.fromInt Num.maxInt"#,
|
||||
// RocStr::from_slice(max.as_bytes()),
|
||||
// RocStr
|
||||
// );
|
||||
|
||||
// let min = format!("{}", i64::MIN);
|
||||
// assert_evals_to!(
|
||||
// r#"Str.fromInt Num.minInt"#,
|
||||
// RocStr::from_slice(min.as_bytes()),
|
||||
// RocStr
|
||||
// );
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_from_utf8_pass_single_ascii() {
|
||||
// assert_evals_to!(
|
||||
// indoc!(
|
||||
// r#"
|
||||
// when Str.fromUtf8 [ 97 ] is
|
||||
// Ok val -> val
|
||||
// Err _ -> ""
|
||||
// "#
|
||||
// ),
|
||||
// roc_std::RocStr::from_slice("a".as_bytes()),
|
||||
// roc_std::RocStr
|
||||
// );
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_from_utf8_pass_many_ascii() {
|
||||
// assert_evals_to!(
|
||||
// indoc!(
|
||||
// r#"
|
||||
// when Str.fromUtf8 [ 97, 98, 99, 0x7E ] is
|
||||
// Ok val -> val
|
||||
// Err _ -> ""
|
||||
// "#
|
||||
// ),
|
||||
// roc_std::RocStr::from_slice("abc~".as_bytes()),
|
||||
// roc_std::RocStr
|
||||
// );
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_from_utf8_pass_single_unicode() {
|
||||
// assert_evals_to!(
|
||||
// indoc!(
|
||||
// r#"
|
||||
// when Str.fromUtf8 [ 0xE2, 0x88, 0x86 ] is
|
||||
// Ok val -> val
|
||||
// Err _ -> ""
|
||||
// "#
|
||||
// ),
|
||||
// roc_std::RocStr::from_slice("∆".as_bytes()),
|
||||
// roc_std::RocStr
|
||||
// );
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_from_utf8_pass_many_unicode() {
|
||||
// assert_evals_to!(
|
||||
// indoc!(
|
||||
// r#"
|
||||
// when Str.fromUtf8 [ 0xE2, 0x88, 0x86, 0xC5, 0x93, 0xC2, 0xAC ] is
|
||||
// Ok val -> val
|
||||
// Err _ -> ""
|
||||
// "#
|
||||
// ),
|
||||
// roc_std::RocStr::from_slice("∆œ¬".as_bytes()),
|
||||
// roc_std::RocStr
|
||||
// );
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_from_utf8_pass_single_grapheme() {
|
||||
// assert_evals_to!(
|
||||
// indoc!(
|
||||
// r#"
|
||||
// when Str.fromUtf8 [ 0xF0, 0x9F, 0x92, 0x96 ] is
|
||||
// Ok val -> val
|
||||
// Err _ -> ""
|
||||
// "#
|
||||
// ),
|
||||
// roc_std::RocStr::from_slice("💖".as_bytes()),
|
||||
// roc_std::RocStr
|
||||
// );
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_from_utf8_pass_many_grapheme() {
|
||||
// assert_evals_to!(
|
||||
// indoc!(
|
||||
// r#"
|
||||
// when Str.fromUtf8 [ 0xF0, 0x9F, 0x92, 0x96, 0xF0, 0x9F, 0xA4, 0xA0, 0xF0, 0x9F, 0x9A, 0x80 ] is
|
||||
// Ok val -> val
|
||||
// Err _ -> ""
|
||||
// "#
|
||||
// ),
|
||||
// roc_std::RocStr::from_slice("💖🤠🚀".as_bytes()),
|
||||
// roc_std::RocStr
|
||||
// );
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_from_utf8_pass_all() {
|
||||
// assert_evals_to!(
|
||||
// indoc!(
|
||||
// r#"
|
||||
// when Str.fromUtf8 [ 0xF0, 0x9F, 0x92, 0x96, 98, 0xE2, 0x88, 0x86 ] is
|
||||
// Ok val -> val
|
||||
// Err _ -> ""
|
||||
// "#
|
||||
// ),
|
||||
// roc_std::RocStr::from_slice("💖b∆".as_bytes()),
|
||||
// roc_std::RocStr
|
||||
// );
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_from_utf8_fail_invalid_start_byte() {
|
||||
// assert_evals_to!(
|
||||
// indoc!(
|
||||
// r#"
|
||||
// when Str.fromUtf8 [ 97, 98, 0x80, 99 ] is
|
||||
// Err (BadUtf8 InvalidStartByte byteIndex) ->
|
||||
// if byteIndex == 2 then
|
||||
// "a"
|
||||
// else
|
||||
// "b"
|
||||
// _ -> ""
|
||||
// "#
|
||||
// ),
|
||||
// roc_std::RocStr::from_slice("a".as_bytes()),
|
||||
// roc_std::RocStr
|
||||
// );
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_from_utf8_fail_unexpected_end_of_sequence() {
|
||||
// assert_evals_to!(
|
||||
// indoc!(
|
||||
// r#"
|
||||
// when Str.fromUtf8 [ 97, 98, 99, 0xC2 ] is
|
||||
// Err (BadUtf8 UnexpectedEndOfSequence byteIndex) ->
|
||||
// if byteIndex == 3 then
|
||||
// "a"
|
||||
// else
|
||||
// "b"
|
||||
// _ -> ""
|
||||
// "#
|
||||
// ),
|
||||
// roc_std::RocStr::from_slice("a".as_bytes()),
|
||||
// roc_std::RocStr
|
||||
// );
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_from_utf8_fail_expected_continuation() {
|
||||
// assert_evals_to!(
|
||||
// indoc!(
|
||||
// r#"
|
||||
// when Str.fromUtf8 [ 97, 98, 99, 0xC2, 0x00 ] is
|
||||
// Err (BadUtf8 ExpectedContinuation byteIndex) ->
|
||||
// if byteIndex == 3 then
|
||||
// "a"
|
||||
// else
|
||||
// "b"
|
||||
// _ -> ""
|
||||
// "#
|
||||
// ),
|
||||
// roc_std::RocStr::from_slice("a".as_bytes()),
|
||||
// roc_std::RocStr
|
||||
// );
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_from_utf8_fail_overlong_encoding() {
|
||||
// assert_evals_to!(
|
||||
// indoc!(
|
||||
// r#"
|
||||
// when Str.fromUtf8 [ 97, 0xF0, 0x80, 0x80, 0x80 ] is
|
||||
// Err (BadUtf8 OverlongEncoding byteIndex) ->
|
||||
// if byteIndex == 1 then
|
||||
// "a"
|
||||
// else
|
||||
// "b"
|
||||
// _ -> ""
|
||||
// "#
|
||||
// ),
|
||||
// roc_std::RocStr::from_slice("a".as_bytes()),
|
||||
// roc_std::RocStr
|
||||
// );
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_from_utf8_fail_codepoint_too_large() {
|
||||
// assert_evals_to!(
|
||||
// indoc!(
|
||||
// r#"
|
||||
// when Str.fromUtf8 [ 97, 0xF4, 0x90, 0x80, 0x80 ] is
|
||||
// Err (BadUtf8 CodepointTooLarge byteIndex) ->
|
||||
// if byteIndex == 1 then
|
||||
// "a"
|
||||
// else
|
||||
// "b"
|
||||
// _ -> ""
|
||||
// "#
|
||||
// ),
|
||||
// roc_std::RocStr::from_slice("a".as_bytes()),
|
||||
// roc_std::RocStr
|
||||
// );
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_from_utf8_fail_surrogate_half() {
|
||||
// assert_evals_to!(
|
||||
// indoc!(
|
||||
// r#"
|
||||
// when Str.fromUtf8 [ 97, 98, 0xED, 0xA0, 0x80 ] is
|
||||
// Err (BadUtf8 EncodesSurrogateHalf byteIndex) ->
|
||||
// if byteIndex == 2 then
|
||||
// "a"
|
||||
// else
|
||||
// "b"
|
||||
// _ -> ""
|
||||
// "#
|
||||
// ),
|
||||
// roc_std::RocStr::from_slice("a".as_bytes()),
|
||||
// roc_std::RocStr
|
||||
// );
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_equality() {
|
||||
// assert_evals_to!(r#""a" == "a""#, true, bool);
|
||||
// assert_evals_to!(
|
||||
// r#""loremipsumdolarsitamet" == "loremipsumdolarsitamet""#,
|
||||
// true,
|
||||
// bool
|
||||
// );
|
||||
// assert_evals_to!(r#""a" != "b""#, true, bool);
|
||||
// assert_evals_to!(r#""a" == "b""#, false, bool);
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_clone() {
|
||||
// use roc_std::RocStr;
|
||||
// let long = RocStr::from_slice("loremipsumdolarsitamet".as_bytes());
|
||||
// let short = RocStr::from_slice("x".as_bytes());
|
||||
// let empty = RocStr::from_slice("".as_bytes());
|
||||
|
||||
// debug_assert_eq!(long.clone(), long);
|
||||
// debug_assert_eq!(short.clone(), short);
|
||||
// debug_assert_eq!(empty.clone(), empty);
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn nested_recursive_literal() {
|
||||
// assert_evals_to!(
|
||||
// indoc!(
|
||||
// r#"
|
||||
// Expr : [ Add Expr Expr, Val I64, Var I64 ]
|
||||
|
||||
// expr : Expr
|
||||
// expr = Add (Add (Val 3) (Val 1)) (Add (Val 1) (Var 1))
|
||||
|
||||
// printExpr : Expr -> Str
|
||||
// printExpr = \e ->
|
||||
// when e is
|
||||
// Add a b ->
|
||||
// "Add ("
|
||||
// |> Str.concat (printExpr a)
|
||||
// |> Str.concat ") ("
|
||||
// |> Str.concat (printExpr b)
|
||||
// |> Str.concat ")"
|
||||
// Val v -> "Val " |> Str.concat (Str.fromInt v)
|
||||
// Var v -> "Var " |> Str.concat (Str.fromInt v)
|
||||
|
||||
// printExpr expr
|
||||
// "#
|
||||
// ),
|
||||
// RocStr::from_slice(b"Add (Add (Val 3) (Val 1)) (Add (Val 1) (Var 1))"),
|
||||
// RocStr
|
||||
// );
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_join_comma_small() {
|
||||
// assert_evals_to!(
|
||||
// r#"Str.joinWith ["1", "2"] ", " "#,
|
||||
// RocStr::from("1, 2"),
|
||||
// RocStr
|
||||
// );
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_join_comma_big() {
|
||||
// assert_evals_to!(
|
||||
// r#"Str.joinWith ["10000000", "2000000", "30000000"] ", " "#,
|
||||
// RocStr::from("10000000, 2000000, 30000000"),
|
||||
// RocStr
|
||||
// );
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_join_comma_single() {
|
||||
// assert_evals_to!(r#"Str.joinWith ["1"] ", " "#, RocStr::from("1"), RocStr);
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_from_float() {
|
||||
// assert_evals_to!(r#"Str.fromFloat 3.14"#, RocStr::from("3.14"), RocStr);
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_to_utf8() {
|
||||
// assert_evals_to!(
|
||||
// r#"Str.toUtf8 "hello""#,
|
||||
// RocList::from_slice(&[104, 101, 108, 108, 111]),
|
||||
// RocList<u8>
|
||||
// );
|
||||
// assert_evals_to!(
|
||||
// r#"Str.toUtf8 "this is a long string""#,
|
||||
// RocList::from_slice(&[
|
||||
// 116, 104, 105, 115, 32, 105, 115, 32, 97, 32, 108, 111, 110, 103, 32, 115, 116,
|
||||
// 114, 105, 110, 103
|
||||
// ]),
|
||||
// RocList<u8>
|
||||
// );
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_from_utf8_range() {
|
||||
// assert_evals_to!(
|
||||
// indoc!(
|
||||
// r#"
|
||||
// bytes = Str.toUtf8 "hello"
|
||||
// when Str.fromUtf8Range bytes { count: 5, start: 0 } is
|
||||
// Ok utf8String -> utf8String
|
||||
// _ -> ""
|
||||
// "#
|
||||
// ),
|
||||
// RocStr::from("hello"),
|
||||
// RocStr
|
||||
// );
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_from_utf8_range_slice() {
|
||||
// assert_evals_to!(
|
||||
// indoc!(
|
||||
// r#"
|
||||
// bytes = Str.toUtf8 "hello"
|
||||
// when Str.fromUtf8Range bytes { count: 4, start: 1 } is
|
||||
// Ok utf8String -> utf8String
|
||||
// _ -> ""
|
||||
// "#
|
||||
// ),
|
||||
// RocStr::from("ello"),
|
||||
// RocStr
|
||||
// );
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_from_utf8_range_slice_not_end() {
|
||||
// assert_evals_to!(
|
||||
// indoc!(
|
||||
// r#"
|
||||
// bytes = Str.toUtf8 "hello"
|
||||
// when Str.fromUtf8Range bytes { count: 3, start: 1 } is
|
||||
// Ok utf8String -> utf8String
|
||||
// _ -> ""
|
||||
// "#
|
||||
// ),
|
||||
// RocStr::from("ell"),
|
||||
// RocStr
|
||||
// );
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_from_utf8_range_order_does_not_matter() {
|
||||
// assert_evals_to!(
|
||||
// indoc!(
|
||||
// r#"
|
||||
// bytes = Str.toUtf8 "hello"
|
||||
// when Str.fromUtf8Range bytes { start: 1, count: 3 } is
|
||||
// Ok utf8String -> utf8String
|
||||
// _ -> ""
|
||||
// "#
|
||||
// ),
|
||||
// RocStr::from("ell"),
|
||||
// RocStr
|
||||
// );
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_from_utf8_range_out_of_bounds_start_value() {
|
||||
// assert_evals_to!(
|
||||
// indoc!(
|
||||
// r#"
|
||||
// bytes = Str.toUtf8 "hello"
|
||||
// when Str.fromUtf8Range bytes { start: 7, count: 3 } is
|
||||
// Ok _ -> ""
|
||||
// Err (BadUtf8 _ _) -> ""
|
||||
// Err OutOfBounds -> "out of bounds"
|
||||
// "#
|
||||
// ),
|
||||
// RocStr::from("out of bounds"),
|
||||
// RocStr
|
||||
// );
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_from_utf8_range_count_too_high() {
|
||||
// assert_evals_to!(
|
||||
// indoc!(
|
||||
// r#"
|
||||
// bytes = Str.toUtf8 "hello"
|
||||
// when Str.fromUtf8Range bytes { start: 0, count: 6 } is
|
||||
// Ok _ -> ""
|
||||
// Err (BadUtf8 _ _) -> ""
|
||||
// Err OutOfBounds -> "out of bounds"
|
||||
// "#
|
||||
// ),
|
||||
// RocStr::from("out of bounds"),
|
||||
// RocStr
|
||||
// );
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn str_from_utf8_range_count_too_high_for_start() {
|
||||
// assert_evals_to!(
|
||||
// indoc!(
|
||||
// r#"
|
||||
// bytes = Str.toUtf8 "hello"
|
||||
// when Str.fromUtf8Range bytes { start: 4, count: 3 } is
|
||||
// Ok _ -> ""
|
||||
// Err (BadUtf8 _ _) -> ""
|
||||
// Err OutOfBounds -> "out of bounds"
|
||||
// "#
|
||||
// ),
|
||||
// RocStr::from("out of bounds"),
|
||||
// RocStr
|
||||
// );
|
||||
// }
|
||||
}
|
|
@ -94,10 +94,12 @@ pub fn helper<'a>(
|
|||
let main_fn_layout = loaded.entry_point.layout;
|
||||
|
||||
let mut layout_ids = roc_mono::layout::LayoutIds::default();
|
||||
let main_fn_name = layout_ids
|
||||
let main_fn_name_base = layout_ids
|
||||
.get_toplevel(main_fn_symbol, &main_fn_layout)
|
||||
.to_symbol_string(main_fn_symbol, &interns);
|
||||
|
||||
let main_fn_name = format!("roc_{}_exposed", main_fn_name_base);
|
||||
|
||||
let mut lines = Vec::new();
|
||||
// errors whose reporting we delay (so we can see that code gen generates runtime errors)
|
||||
let mut delayed_errors = Vec::new();
|
||||
|
@ -143,12 +145,13 @@ pub fn helper<'a>(
|
|||
}
|
||||
|
||||
for problem in type_problems {
|
||||
let report = type_problem(&alloc, module_path.clone(), problem);
|
||||
let mut buf = String::new();
|
||||
if let Some(report) = type_problem(&alloc, module_path.clone(), problem) {
|
||||
let mut buf = String::new();
|
||||
|
||||
report.render_color_terminal(&mut buf, &alloc, &palette);
|
||||
report.render_color_terminal(&mut buf, &alloc, &palette);
|
||||
|
||||
lines.push(buf);
|
||||
lines.push(buf);
|
||||
}
|
||||
}
|
||||
|
||||
for problem in mono_problems {
|
||||
|
|
|
@ -10,7 +10,7 @@ use inkwell::types::{BasicType, BasicTypeEnum};
|
|||
use inkwell::values::{BasicValue, BasicValueEnum, CallSiteValue, FunctionValue, InstructionValue};
|
||||
use inkwell::AddressSpace;
|
||||
use roc_module::symbol::Symbol;
|
||||
use roc_mono::layout::{Layout, LayoutIds, UnionLayout};
|
||||
use roc_mono::layout::{LambdaSet, Layout, LayoutIds, UnionLayout};
|
||||
|
||||
pub fn call_bitcode_fn<'a, 'ctx, 'env>(
|
||||
env: &Env<'a, 'ctx, 'env>,
|
||||
|
@ -189,7 +189,7 @@ fn build_has_tag_id_help<'a, 'ctx, 'env>(
|
|||
pub fn build_transform_caller<'a, 'ctx, 'env>(
|
||||
env: &Env<'a, 'ctx, 'env>,
|
||||
function: FunctionValue<'ctx>,
|
||||
closure_data_layout: Layout<'a>,
|
||||
closure_data_layout: LambdaSet<'a>,
|
||||
argument_layouts: &[Layout<'a>],
|
||||
) -> FunctionValue<'ctx> {
|
||||
let fn_name: &str = &format!(
|
||||
|
@ -212,7 +212,7 @@ pub fn build_transform_caller<'a, 'ctx, 'env>(
|
|||
fn build_transform_caller_help<'a, 'ctx, 'env>(
|
||||
env: &Env<'a, 'ctx, 'env>,
|
||||
roc_function: FunctionValue<'ctx>,
|
||||
closure_data_layout: Layout<'a>,
|
||||
closure_data_layout: LambdaSet<'a>,
|
||||
argument_layouts: &[Layout<'a>],
|
||||
fn_name: &str,
|
||||
) -> FunctionValue<'ctx> {
|
||||
|
@ -270,7 +270,7 @@ fn build_transform_caller_help<'a, 'ctx, 'env>(
|
|||
arguments_cast.push(argument);
|
||||
}
|
||||
|
||||
match closure_data_layout {
|
||||
match closure_data_layout.runtime_representation() {
|
||||
Layout::Struct(&[]) => {
|
||||
// nothing to add
|
||||
}
|
||||
|
@ -529,7 +529,7 @@ pub fn build_eq_wrapper<'a, 'ctx, 'env>(
|
|||
pub fn build_compare_wrapper<'a, 'ctx, 'env>(
|
||||
env: &Env<'a, 'ctx, 'env>,
|
||||
roc_function: FunctionValue<'ctx>,
|
||||
closure_data_layout: Layout<'a>,
|
||||
closure_data_layout: LambdaSet<'a>,
|
||||
layout: &Layout<'a>,
|
||||
) -> FunctionValue<'ctx> {
|
||||
let block = env.builder.get_insert_block().expect("to be in a function");
|
||||
|
@ -595,7 +595,7 @@ pub fn build_compare_wrapper<'a, 'ctx, 'env>(
|
|||
|
||||
let default = [value1, value2];
|
||||
|
||||
let arguments_cast = match closure_data_layout {
|
||||
let arguments_cast = match closure_data_layout.runtime_representation() {
|
||||
Layout::Struct(&[]) => {
|
||||
// nothing to add
|
||||
&default
|
||||
|
|
|
@ -15,7 +15,7 @@ use crate::llvm::build_list::{
|
|||
};
|
||||
use crate::llvm::build_str::{
|
||||
empty_str, str_concat, str_count_graphemes, str_ends_with, str_from_float, str_from_int,
|
||||
str_from_utf8, str_from_utf8_range, str_join_with, str_number_of_bytes, str_split,
|
||||
str_from_utf8, str_from_utf8_range, str_join_with, str_number_of_bytes, str_repeat, str_split,
|
||||
str_starts_with, str_starts_with_code_point, str_to_utf8,
|
||||
};
|
||||
use crate::llvm::compare::{generic_eq, generic_neq};
|
||||
|
@ -638,7 +638,7 @@ pub fn construct_optimization_passes<'a>(
|
|||
|
||||
let pmb = PassManagerBuilder::create();
|
||||
match opt_level {
|
||||
OptLevel::Normal => {
|
||||
OptLevel::Development | OptLevel::Normal => {
|
||||
pmb.set_optimization_level(OptimizationLevel::None);
|
||||
}
|
||||
OptLevel::Optimize => {
|
||||
|
@ -704,8 +704,14 @@ fn promote_to_main_function<'a, 'ctx, 'env>(
|
|||
let main_fn_name = "$Test.main";
|
||||
|
||||
// Add main to the module.
|
||||
let main_fn =
|
||||
expose_function_to_host_help_c_abi(env, main_fn_name, roc_main_fn, &[], main_fn_name);
|
||||
let main_fn = expose_function_to_host_help_c_abi(
|
||||
env,
|
||||
main_fn_name,
|
||||
roc_main_fn,
|
||||
&[],
|
||||
top_level.result,
|
||||
main_fn_name,
|
||||
);
|
||||
|
||||
(main_fn_name, main_fn)
|
||||
}
|
||||
|
@ -1164,8 +1170,16 @@ pub fn build_exp_expr<'a, 'ctx, 'env>(
|
|||
StructAtIndex {
|
||||
index, structure, ..
|
||||
} => {
|
||||
let (value, layout) = load_symbol_and_layout(scope, structure);
|
||||
|
||||
let layout = if let Layout::LambdaSet(lambda_set) = layout {
|
||||
lambda_set.runtime_representation()
|
||||
} else {
|
||||
*layout
|
||||
};
|
||||
|
||||
// extract field from a record
|
||||
match load_symbol_and_layout(scope, structure) {
|
||||
match (value, layout) {
|
||||
(StructValue(argument), Layout::Struct(fields)) => {
|
||||
debug_assert!(!fields.is_empty());
|
||||
env.builder
|
||||
|
@ -2601,6 +2615,18 @@ pub fn load_symbol_and_layout<'a, 'ctx, 'b>(
|
|||
None => panic!("There was no entry for {:?} in scope {:?}", symbol, scope),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load_symbol_and_lambda_set<'a, 'ctx, 'b>(
|
||||
scope: &'b Scope<'a, 'ctx>,
|
||||
symbol: &Symbol,
|
||||
) -> (BasicValueEnum<'ctx>, LambdaSet<'a>) {
|
||||
match scope.get(symbol) {
|
||||
Some((Layout::LambdaSet(lambda_set), ptr)) => (*ptr, *lambda_set),
|
||||
Some((other, ptr)) => panic!("Not a lambda set: {:?}, {:?}", other, ptr),
|
||||
None => panic!("There was no entry for {:?} in scope {:?}", symbol, scope),
|
||||
}
|
||||
}
|
||||
|
||||
fn access_index_struct_value<'ctx>(
|
||||
builder: &Builder<'ctx>,
|
||||
from_value: StructValue<'ctx>,
|
||||
|
@ -3055,6 +3081,7 @@ fn expose_function_to_host<'a, 'ctx, 'env>(
|
|||
symbol: Symbol,
|
||||
roc_function: FunctionValue<'ctx>,
|
||||
arguments: &[Layout<'a>],
|
||||
return_layout: Layout<'a>,
|
||||
) {
|
||||
// Assumption: there is only one specialization of a host-exposed function
|
||||
let ident_string = symbol.as_str(&env.interns);
|
||||
|
@ -3065,26 +3092,19 @@ fn expose_function_to_host<'a, 'ctx, 'env>(
|
|||
ident_string,
|
||||
roc_function,
|
||||
arguments,
|
||||
return_layout,
|
||||
&c_function_name,
|
||||
);
|
||||
}
|
||||
|
||||
fn expose_function_to_host_help_c_abi<'a, 'ctx, 'env>(
|
||||
fn expose_function_to_host_help_c_abi_generic<'a, 'ctx, 'env>(
|
||||
env: &Env<'a, 'ctx, 'env>,
|
||||
ident_string: &str,
|
||||
roc_function: FunctionValue<'ctx>,
|
||||
arguments: &[Layout<'a>],
|
||||
c_function_name: &str,
|
||||
) -> FunctionValue<'ctx> {
|
||||
let context = env.context;
|
||||
|
||||
let wrapper_return_type = context.struct_type(
|
||||
&[
|
||||
context.i64_type().into(),
|
||||
roc_function.get_type().get_return_type().unwrap(),
|
||||
],
|
||||
false,
|
||||
);
|
||||
// NOTE we ingore env.is_gen_test here
|
||||
let wrapper_return_type = roc_function.get_type().get_return_type().unwrap();
|
||||
|
||||
let mut cc_argument_types = Vec::with_capacity_in(arguments.len(), env.arena);
|
||||
for layout in arguments {
|
||||
|
@ -3095,6 +3115,7 @@ fn expose_function_to_host_help_c_abi<'a, 'ctx, 'env>(
|
|||
// let mut argument_types = roc_function.get_type().get_param_types();
|
||||
let mut argument_types = cc_argument_types;
|
||||
let return_type = wrapper_return_type;
|
||||
|
||||
let output_type = return_type.ptr_type(AddressSpace::Generic);
|
||||
argument_types.push(output_type.into());
|
||||
|
||||
|
@ -3122,9 +3143,11 @@ fn expose_function_to_host_help_c_abi<'a, 'ctx, 'env>(
|
|||
debug_info_init!(env, c_function);
|
||||
|
||||
// drop the final argument, which is the pointer we write the result into
|
||||
let args = c_function.get_params();
|
||||
let output_arg_index = args.len() - 1;
|
||||
let args = &args[..args.len() - 1];
|
||||
let args_vector = c_function.get_params();
|
||||
let mut args = args_vector.as_slice();
|
||||
let args_length = args.len();
|
||||
|
||||
args = &args[..args.len() - 1];
|
||||
|
||||
let mut arguments_for_call = Vec::with_capacity_in(args.len(), env.arena);
|
||||
|
||||
|
@ -3169,19 +3192,187 @@ fn expose_function_to_host_help_c_abi<'a, 'ctx, 'env>(
|
|||
|
||||
let call_unwrapped_result = call_unwrapped.try_as_basic_value().left().unwrap();
|
||||
|
||||
make_good_roc_result(env, call_unwrapped_result)
|
||||
// make_good_roc_result(env, call_unwrapped_result)
|
||||
call_unwrapped_result
|
||||
}
|
||||
};
|
||||
|
||||
let output_arg_index = args_length - 1;
|
||||
|
||||
let output_arg = c_function
|
||||
.get_nth_param(output_arg_index as u32)
|
||||
.unwrap()
|
||||
.into_pointer_value();
|
||||
|
||||
builder.build_store(output_arg, call_result);
|
||||
|
||||
builder.build_return(None);
|
||||
|
||||
c_function
|
||||
}
|
||||
|
||||
fn expose_function_to_host_help_c_abi<'a, 'ctx, 'env>(
|
||||
env: &Env<'a, 'ctx, 'env>,
|
||||
ident_string: &str,
|
||||
roc_function: FunctionValue<'ctx>,
|
||||
arguments: &[Layout<'a>],
|
||||
return_layout: Layout<'a>,
|
||||
c_function_name: &str,
|
||||
) -> FunctionValue<'ctx> {
|
||||
let context = env.context;
|
||||
|
||||
// a generic version that writes the result into a passed *u8 pointer
|
||||
if !env.is_gen_test {
|
||||
expose_function_to_host_help_c_abi_generic(
|
||||
env,
|
||||
roc_function,
|
||||
arguments,
|
||||
&format!("{}_generic", c_function_name),
|
||||
);
|
||||
}
|
||||
|
||||
let wrapper_return_type = if env.is_gen_test {
|
||||
context
|
||||
.struct_type(
|
||||
&[
|
||||
context.i64_type().into(),
|
||||
roc_function.get_type().get_return_type().unwrap(),
|
||||
],
|
||||
false,
|
||||
)
|
||||
.into()
|
||||
} else {
|
||||
roc_function.get_type().get_return_type().unwrap()
|
||||
};
|
||||
|
||||
let mut cc_argument_types = Vec::with_capacity_in(arguments.len(), env.arena);
|
||||
for layout in arguments {
|
||||
cc_argument_types.push(to_cc_type(env, layout));
|
||||
}
|
||||
|
||||
// STEP 1: turn `f : a,b,c -> d` into `f : a,b,c, &d -> {}` if the C abi demands it
|
||||
let mut argument_types = cc_argument_types;
|
||||
let return_type = wrapper_return_type;
|
||||
|
||||
let cc_return = to_cc_return(env, &return_layout);
|
||||
|
||||
let c_function_type = match cc_return {
|
||||
CCReturn::Void if !env.is_gen_test => {
|
||||
env.context.void_type().fn_type(&argument_types, false)
|
||||
}
|
||||
CCReturn::Return if !env.is_gen_test => return_type.fn_type(&argument_types, false),
|
||||
_ => {
|
||||
let output_type = return_type.ptr_type(AddressSpace::Generic);
|
||||
argument_types.push(output_type.into());
|
||||
env.context.void_type().fn_type(&argument_types, false)
|
||||
}
|
||||
};
|
||||
|
||||
let c_function = add_func(
|
||||
env.module,
|
||||
c_function_name,
|
||||
c_function_type,
|
||||
Linkage::External,
|
||||
C_CALL_CONV,
|
||||
);
|
||||
|
||||
let subprogram = env.new_subprogram(c_function_name);
|
||||
c_function.set_subprogram(subprogram);
|
||||
|
||||
// STEP 2: build the exposed function's body
|
||||
let builder = env.builder;
|
||||
let context = env.context;
|
||||
|
||||
let entry = context.append_basic_block(c_function, "entry");
|
||||
|
||||
builder.position_at_end(entry);
|
||||
|
||||
debug_info_init!(env, c_function);
|
||||
|
||||
// drop the final argument, which is the pointer we write the result into
|
||||
let args_vector = c_function.get_params();
|
||||
let mut args = args_vector.as_slice();
|
||||
let args_length = args.len();
|
||||
|
||||
match cc_return {
|
||||
CCReturn::Return if !env.is_gen_test => {
|
||||
debug_assert_eq!(args.len(), roc_function.get_params().len());
|
||||
}
|
||||
CCReturn::Void if !env.is_gen_test => {
|
||||
debug_assert_eq!(args.len(), roc_function.get_params().len());
|
||||
}
|
||||
_ => {
|
||||
args = &args[..args.len() - 1];
|
||||
debug_assert_eq!(args.len(), roc_function.get_params().len());
|
||||
}
|
||||
}
|
||||
|
||||
let mut arguments_for_call = Vec::with_capacity_in(args.len(), env.arena);
|
||||
|
||||
let it = args.iter().zip(roc_function.get_type().get_param_types());
|
||||
for (arg, fastcc_type) in it {
|
||||
let arg_type = arg.get_type();
|
||||
if arg_type == fastcc_type {
|
||||
// the C and Fast calling conventions agree
|
||||
arguments_for_call.push(*arg);
|
||||
} else {
|
||||
let cast = complex_bitcast_check_size(env, *arg, fastcc_type, "to_fastcc_type");
|
||||
arguments_for_call.push(cast);
|
||||
}
|
||||
}
|
||||
|
||||
let arguments_for_call = &arguments_for_call.into_bump_slice();
|
||||
|
||||
let call_result = {
|
||||
if env.is_gen_test {
|
||||
let roc_wrapper_function = make_exception_catcher(env, roc_function);
|
||||
debug_assert_eq!(
|
||||
arguments_for_call.len(),
|
||||
roc_wrapper_function.get_params().len()
|
||||
);
|
||||
|
||||
builder.position_at_end(entry);
|
||||
|
||||
let call_wrapped = builder.build_call(
|
||||
roc_wrapper_function,
|
||||
arguments_for_call,
|
||||
"call_wrapped_function",
|
||||
);
|
||||
call_wrapped.set_call_convention(FAST_CALL_CONV);
|
||||
|
||||
call_wrapped.try_as_basic_value().left().unwrap()
|
||||
} else {
|
||||
let call_unwrapped =
|
||||
builder.build_call(roc_function, arguments_for_call, "call_unwrapped_function");
|
||||
call_unwrapped.set_call_convention(FAST_CALL_CONV);
|
||||
|
||||
let call_unwrapped_result = call_unwrapped.try_as_basic_value().left().unwrap();
|
||||
|
||||
// make_good_roc_result(env, call_unwrapped_result)
|
||||
call_unwrapped_result
|
||||
}
|
||||
};
|
||||
|
||||
match cc_return {
|
||||
CCReturn::Void if !env.is_gen_test => {
|
||||
// TODO return empty struct here?
|
||||
builder.build_return(None);
|
||||
}
|
||||
CCReturn::Return if !env.is_gen_test => {
|
||||
builder.build_return(Some(&call_result));
|
||||
}
|
||||
_ => {
|
||||
let output_arg_index = args_length - 1;
|
||||
|
||||
let output_arg = c_function
|
||||
.get_nth_param(output_arg_index as u32)
|
||||
.unwrap()
|
||||
.into_pointer_value();
|
||||
|
||||
builder.build_store(output_arg, call_result);
|
||||
builder.build_return(None);
|
||||
}
|
||||
}
|
||||
|
||||
// STEP 3: build a {} -> u64 function that gives the size of the return type
|
||||
let size_function_type = env.context.i64_type().fn_type(&[], false);
|
||||
let size_function_name: String = format!("roc__{}_size", ident_string);
|
||||
|
@ -3695,7 +3886,13 @@ fn build_proc_header<'a, 'ctx, 'env>(
|
|||
|
||||
if env.exposed_to_host.contains(&symbol) {
|
||||
let arguments = Vec::from_iter_in(proc.args.iter().map(|(layout, _)| *layout), env.arena);
|
||||
expose_function_to_host(env, symbol, fn_val, arguments.into_bump_slice());
|
||||
expose_function_to_host(
|
||||
env,
|
||||
symbol,
|
||||
fn_val,
|
||||
arguments.into_bump_slice(),
|
||||
proc.ret_layout,
|
||||
);
|
||||
}
|
||||
|
||||
fn_val
|
||||
|
@ -3762,23 +3959,17 @@ pub fn build_closure_caller<'a, 'ctx, 'env>(
|
|||
|
||||
builder.position_at_end(entry);
|
||||
|
||||
let mut parameters = function_value.get_params();
|
||||
let output = parameters.pop().unwrap().into_pointer_value();
|
||||
let mut evaluator_arguments = function_value.get_params();
|
||||
|
||||
let closure_data = if let Some(closure_data_ptr) = parameters.pop() {
|
||||
let closure_data =
|
||||
builder.build_load(closure_data_ptr.into_pointer_value(), "load_closure_data");
|
||||
// the final parameter is the output pointer, pop it
|
||||
let output = evaluator_arguments.pop().unwrap().into_pointer_value();
|
||||
|
||||
env.arena.alloc([closure_data]) as &[_]
|
||||
} else {
|
||||
&[]
|
||||
};
|
||||
|
||||
let mut parameters = parameters;
|
||||
|
||||
for param in parameters.iter_mut() {
|
||||
debug_assert!(param.is_pointer_value());
|
||||
*param = builder.build_load(param.into_pointer_value(), "load_param");
|
||||
// NOTE this may be incorrect in the long run
|
||||
// here we load any argument that is a pointer
|
||||
for param in evaluator_arguments.iter_mut() {
|
||||
if param.is_pointer_value() {
|
||||
*param = builder.build_load(param.into_pointer_value(), "load_param");
|
||||
}
|
||||
}
|
||||
|
||||
let call_result = if env.is_gen_test {
|
||||
|
@ -3787,13 +3978,13 @@ pub fn build_closure_caller<'a, 'ctx, 'env>(
|
|||
function_value,
|
||||
evaluator,
|
||||
evaluator.get_call_conventions(),
|
||||
closure_data,
|
||||
&evaluator_arguments,
|
||||
result_type,
|
||||
)
|
||||
} else {
|
||||
let call = env
|
||||
.builder
|
||||
.build_call(evaluator, closure_data, "call_function");
|
||||
.build_call(evaluator, &evaluator_arguments, "call_function");
|
||||
|
||||
call.set_call_convention(evaluator.get_call_conventions());
|
||||
|
||||
|
@ -4094,7 +4285,7 @@ fn roc_function_call<'a, 'ctx, 'env>(
|
|||
layout_ids: &mut LayoutIds<'a>,
|
||||
transform: FunctionValue<'ctx>,
|
||||
closure_data: BasicValueEnum<'ctx>,
|
||||
closure_data_layout: Layout<'a>,
|
||||
lambda_set: LambdaSet<'a>,
|
||||
closure_data_is_owned: bool,
|
||||
argument_layouts: &[Layout<'a>],
|
||||
) -> RocFunctionCall<'ctx> {
|
||||
|
@ -4105,15 +4296,15 @@ fn roc_function_call<'a, 'ctx, 'env>(
|
|||
.build_alloca(closure_data.get_type(), "closure_data_ptr");
|
||||
env.builder.build_store(closure_data_ptr, closure_data);
|
||||
|
||||
let stepper_caller =
|
||||
build_transform_caller(env, transform, closure_data_layout, argument_layouts)
|
||||
.as_global_value()
|
||||
.as_pointer_value();
|
||||
|
||||
let inc_closure_data = build_inc_n_wrapper(env, layout_ids, &closure_data_layout)
|
||||
let stepper_caller = build_transform_caller(env, transform, lambda_set, argument_layouts)
|
||||
.as_global_value()
|
||||
.as_pointer_value();
|
||||
|
||||
let inc_closure_data =
|
||||
build_inc_n_wrapper(env, layout_ids, &lambda_set.runtime_representation())
|
||||
.as_global_value()
|
||||
.as_pointer_value();
|
||||
|
||||
let closure_data_is_owned = env
|
||||
.context
|
||||
.bool_type()
|
||||
|
@ -4167,7 +4358,7 @@ fn run_higher_order_low_level<'a, 'ctx, 'env>(
|
|||
|
||||
let function = passed_function_at_index!(2);
|
||||
|
||||
let (closure, closure_layout) = load_symbol_and_layout(scope, &args[3]);
|
||||
let (closure, closure_layout) = load_symbol_and_lambda_set(scope, &args[3]);
|
||||
|
||||
match list_layout {
|
||||
Layout::Builtin(Builtin::EmptyList) => default,
|
||||
|
@ -4179,7 +4370,7 @@ fn run_higher_order_low_level<'a, 'ctx, 'env>(
|
|||
layout_ids,
|
||||
function,
|
||||
closure,
|
||||
*closure_layout,
|
||||
closure_layout,
|
||||
function_owns_closure_data,
|
||||
argument_layouts,
|
||||
);
|
||||
|
@ -4209,7 +4400,7 @@ fn run_higher_order_low_level<'a, 'ctx, 'env>(
|
|||
|
||||
let function = passed_function_at_index!(1);
|
||||
|
||||
let (closure, closure_layout) = load_symbol_and_layout(scope, &args[2]);
|
||||
let (closure, closure_layout) = load_symbol_and_lambda_set(scope, &args[2]);
|
||||
|
||||
match (list_layout, return_layout) {
|
||||
(Layout::Builtin(Builtin::EmptyList), _) => empty_list(env),
|
||||
|
@ -4224,7 +4415,7 @@ fn run_higher_order_low_level<'a, 'ctx, 'env>(
|
|||
layout_ids,
|
||||
function,
|
||||
closure,
|
||||
*closure_layout,
|
||||
closure_layout,
|
||||
function_owns_closure_data,
|
||||
argument_layouts,
|
||||
);
|
||||
|
@ -4241,7 +4432,7 @@ fn run_higher_order_low_level<'a, 'ctx, 'env>(
|
|||
let (list2, list2_layout) = load_symbol_and_layout(scope, &args[1]);
|
||||
|
||||
let function = passed_function_at_index!(2);
|
||||
let (closure, closure_layout) = load_symbol_and_layout(scope, &args[3]);
|
||||
let (closure, closure_layout) = load_symbol_and_lambda_set(scope, &args[3]);
|
||||
|
||||
match (list1_layout, list2_layout, return_layout) {
|
||||
(
|
||||
|
@ -4256,7 +4447,7 @@ fn run_higher_order_low_level<'a, 'ctx, 'env>(
|
|||
layout_ids,
|
||||
function,
|
||||
closure,
|
||||
*closure_layout,
|
||||
closure_layout,
|
||||
function_owns_closure_data,
|
||||
argument_layouts,
|
||||
);
|
||||
|
@ -4285,7 +4476,7 @@ fn run_higher_order_low_level<'a, 'ctx, 'env>(
|
|||
let (list3, list3_layout) = load_symbol_and_layout(scope, &args[2]);
|
||||
|
||||
let function = passed_function_at_index!(3);
|
||||
let (closure, closure_layout) = load_symbol_and_layout(scope, &args[4]);
|
||||
let (closure, closure_layout) = load_symbol_and_lambda_set(scope, &args[4]);
|
||||
|
||||
match (list1_layout, list2_layout, list3_layout, return_layout) {
|
||||
(
|
||||
|
@ -4302,7 +4493,7 @@ fn run_higher_order_low_level<'a, 'ctx, 'env>(
|
|||
layout_ids,
|
||||
function,
|
||||
closure,
|
||||
*closure_layout,
|
||||
closure_layout,
|
||||
function_owns_closure_data,
|
||||
argument_layouts,
|
||||
);
|
||||
|
@ -4334,7 +4525,7 @@ fn run_higher_order_low_level<'a, 'ctx, 'env>(
|
|||
|
||||
let function = passed_function_at_index!(1);
|
||||
|
||||
let (closure, closure_layout) = load_symbol_and_layout(scope, &args[2]);
|
||||
let (closure, closure_layout) = load_symbol_and_lambda_set(scope, &args[2]);
|
||||
|
||||
match (list_layout, return_layout) {
|
||||
(Layout::Builtin(Builtin::EmptyList), _) => empty_list(env),
|
||||
|
@ -4349,7 +4540,7 @@ fn run_higher_order_low_level<'a, 'ctx, 'env>(
|
|||
layout_ids,
|
||||
function,
|
||||
closure,
|
||||
*closure_layout,
|
||||
closure_layout,
|
||||
function_owns_closure_data,
|
||||
argument_layouts,
|
||||
);
|
||||
|
@ -4367,7 +4558,7 @@ fn run_higher_order_low_level<'a, 'ctx, 'env>(
|
|||
|
||||
let function = passed_function_at_index!(1);
|
||||
|
||||
let (closure, closure_layout) = load_symbol_and_layout(scope, &args[2]);
|
||||
let (closure, closure_layout) = load_symbol_and_lambda_set(scope, &args[2]);
|
||||
|
||||
match list_layout {
|
||||
Layout::Builtin(Builtin::EmptyList) => empty_list(env),
|
||||
|
@ -4379,7 +4570,7 @@ fn run_higher_order_low_level<'a, 'ctx, 'env>(
|
|||
layout_ids,
|
||||
function,
|
||||
closure,
|
||||
*closure_layout,
|
||||
closure_layout,
|
||||
function_owns_closure_data,
|
||||
argument_layouts,
|
||||
);
|
||||
|
@ -4397,7 +4588,7 @@ fn run_higher_order_low_level<'a, 'ctx, 'env>(
|
|||
|
||||
let function = passed_function_at_index!(1);
|
||||
|
||||
let (closure, closure_layout) = load_symbol_and_layout(scope, &args[2]);
|
||||
let (closure, closure_layout) = load_symbol_and_lambda_set(scope, &args[2]);
|
||||
|
||||
match (list_layout, return_layout) {
|
||||
(_, Layout::Builtin(Builtin::EmptyList))
|
||||
|
@ -4413,7 +4604,7 @@ fn run_higher_order_low_level<'a, 'ctx, 'env>(
|
|||
layout_ids,
|
||||
function,
|
||||
closure,
|
||||
*closure_layout,
|
||||
closure_layout,
|
||||
function_owns_closure_data,
|
||||
argument_layouts,
|
||||
);
|
||||
|
@ -4441,7 +4632,7 @@ fn run_higher_order_low_level<'a, 'ctx, 'env>(
|
|||
|
||||
let function = passed_function_at_index!(1);
|
||||
|
||||
let (closure, closure_layout) = load_symbol_and_layout(scope, &args[2]);
|
||||
let (closure, closure_layout) = load_symbol_and_lambda_set(scope, &args[2]);
|
||||
|
||||
match (list_layout, return_layout) {
|
||||
(_, Layout::Builtin(Builtin::EmptyList))
|
||||
|
@ -4457,7 +4648,7 @@ fn run_higher_order_low_level<'a, 'ctx, 'env>(
|
|||
layout_ids,
|
||||
function,
|
||||
closure,
|
||||
*closure_layout,
|
||||
closure_layout,
|
||||
function_owns_closure_data,
|
||||
argument_layouts,
|
||||
);
|
||||
|
@ -4494,7 +4685,7 @@ fn run_higher_order_low_level<'a, 'ctx, 'env>(
|
|||
|
||||
let function = passed_function_at_index!(1);
|
||||
|
||||
let (closure, closure_layout) = load_symbol_and_layout(scope, &args[2]);
|
||||
let (closure, closure_layout) = load_symbol_and_lambda_set(scope, &args[2]);
|
||||
|
||||
match list_layout {
|
||||
Layout::Builtin(Builtin::EmptyList) => empty_list(env),
|
||||
|
@ -4504,7 +4695,7 @@ fn run_higher_order_low_level<'a, 'ctx, 'env>(
|
|||
let argument_layouts = &[**element_layout, **element_layout];
|
||||
|
||||
let compare_wrapper =
|
||||
build_compare_wrapper(env, function, *closure_layout, element_layout)
|
||||
build_compare_wrapper(env, function, closure_layout, element_layout)
|
||||
.as_global_value()
|
||||
.as_pointer_value();
|
||||
|
||||
|
@ -4513,7 +4704,7 @@ fn run_higher_order_low_level<'a, 'ctx, 'env>(
|
|||
layout_ids,
|
||||
function,
|
||||
closure,
|
||||
*closure_layout,
|
||||
closure_layout,
|
||||
function_owns_closure_data,
|
||||
argument_layouts,
|
||||
);
|
||||
|
@ -4535,7 +4726,7 @@ fn run_higher_order_low_level<'a, 'ctx, 'env>(
|
|||
let (dict, dict_layout) = load_symbol_and_layout(scope, &args[0]);
|
||||
let (default, default_layout) = load_symbol_and_layout(scope, &args[1]);
|
||||
let function = passed_function_at_index!(2);
|
||||
let (closure, closure_layout) = load_symbol_and_layout(scope, &args[3]);
|
||||
let (closure, closure_layout) = load_symbol_and_lambda_set(scope, &args[3]);
|
||||
|
||||
match dict_layout {
|
||||
Layout::Builtin(Builtin::EmptyDict) => {
|
||||
|
@ -4550,7 +4741,7 @@ fn run_higher_order_low_level<'a, 'ctx, 'env>(
|
|||
layout_ids,
|
||||
function,
|
||||
closure,
|
||||
*closure_layout,
|
||||
closure_layout,
|
||||
function_owns_closure_data,
|
||||
argument_layouts,
|
||||
);
|
||||
|
@ -4662,6 +4853,12 @@ fn run_low_level<'a, 'ctx, 'env>(
|
|||
|
||||
str_to_utf8(env, string.into_struct_value())
|
||||
}
|
||||
StrRepeat => {
|
||||
// Str.repeat : Str, Nat -> Str
|
||||
debug_assert_eq!(args.len(), 2);
|
||||
|
||||
str_repeat(env, scope, args[0], args[1])
|
||||
}
|
||||
StrSplit => {
|
||||
// Str.split : Str, Str -> List Str
|
||||
debug_assert_eq!(args.len(), 2);
|
||||
|
@ -4836,7 +5033,7 @@ fn run_low_level<'a, 'ctx, 'env>(
|
|||
Usize | Int128 | Int64 | Int32 | Int16 | Int8 => {
|
||||
build_int_unary_op(env, arg.into_int_value(), arg_builtin, op)
|
||||
}
|
||||
Float128 | Float64 | Float32 | Float16 => {
|
||||
Float128 | Float64 | Float32 => {
|
||||
build_float_unary_op(env, arg.into_float_value(), op)
|
||||
}
|
||||
_ => {
|
||||
|
@ -4932,7 +5129,7 @@ fn run_low_level<'a, 'ctx, 'env>(
|
|||
"lt_or_gt",
|
||||
)
|
||||
}
|
||||
Float128 | Float64 | Float32 | Float16 => {
|
||||
Float128 | Float64 | Float32 => {
|
||||
let are_equal = env.builder.build_float_compare(
|
||||
FloatPredicate::OEQ,
|
||||
lhs_arg.into_float_value(),
|
||||
|
@ -5378,8 +5575,7 @@ fn to_cc_type_builtin<'a, 'ctx, 'env>(
|
|||
| Builtin::Decimal
|
||||
| Builtin::Float128
|
||||
| Builtin::Float64
|
||||
| Builtin::Float32
|
||||
| Builtin::Float16 => basic_type_from_builtin(env, builtin),
|
||||
| Builtin::Float32 => basic_type_from_builtin(env, builtin),
|
||||
Builtin::Str | Builtin::EmptyStr | Builtin::List(_) | Builtin::EmptyList => {
|
||||
env.str_list_c_abi().into()
|
||||
}
|
||||
|
@ -5742,7 +5938,7 @@ pub fn build_num_binop<'a, 'ctx, 'env>(
|
|||
rhs_layout,
|
||||
op,
|
||||
),
|
||||
Float128 | Float64 | Float32 | Float16 => build_float_binop(
|
||||
Float128 | Float64 | Float32 => build_float_binop(
|
||||
env,
|
||||
parent,
|
||||
lhs_arg.into_float_value(),
|
||||
|
|
|
@ -59,6 +59,15 @@ fn build_hash_layout<'a, 'ctx, 'env>(
|
|||
val.into_struct_value(),
|
||||
),
|
||||
|
||||
Layout::LambdaSet(lambda_set) => build_hash_layout(
|
||||
env,
|
||||
layout_ids,
|
||||
seed,
|
||||
val,
|
||||
&lambda_set.runtime_representation(),
|
||||
when_recursive,
|
||||
),
|
||||
|
||||
Layout::Union(union_layout) => {
|
||||
build_hash_tag(env, layout_ids, layout, union_layout, seed, val)
|
||||
}
|
||||
|
@ -123,7 +132,6 @@ fn hash_builtin<'a, 'ctx, 'env>(
|
|||
| Builtin::Float64
|
||||
| Builtin::Float32
|
||||
| Builtin::Float128
|
||||
| Builtin::Float16
|
||||
| Builtin::Decimal
|
||||
| Builtin::Usize => {
|
||||
let hash_bytes = store_and_use_as_u8_ptr(env, val, layout);
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use crate::llvm::bitcode::{call_bitcode_fn, call_void_bitcode_fn};
|
||||
use crate::llvm::build::{complex_bitcast, struct_from_fields, Env, Scope};
|
||||
use crate::llvm::build::{complex_bitcast, Env, Scope};
|
||||
use crate::llvm::build_list::{allocate_list, call_bitcode_fn_returns_list, store_list};
|
||||
use inkwell::builder::Builder;
|
||||
use inkwell::values::{BasicValueEnum, FunctionValue, IntValue, PointerValue, StructValue};
|
||||
|
@ -12,6 +12,18 @@ use super::build::load_symbol;
|
|||
|
||||
pub static CHAR_LAYOUT: Layout = Layout::Builtin(Builtin::Int8);
|
||||
|
||||
/// Str.repeat : Str, Nat -> Str
|
||||
pub fn str_repeat<'a, 'ctx, 'env>(
|
||||
env: &Env<'a, 'ctx, 'env>,
|
||||
scope: &Scope<'a, 'ctx>,
|
||||
str_symbol: Symbol,
|
||||
count_symbol: Symbol,
|
||||
) -> BasicValueEnum<'ctx> {
|
||||
let str_c_abi = str_symbol_to_c_abi(env, scope, str_symbol);
|
||||
let count = load_symbol(scope, &count_symbol);
|
||||
call_bitcode_fn(env, &[str_c_abi.into(), count], bitcode::STR_REPEAT)
|
||||
}
|
||||
|
||||
/// Str.split : Str, Str -> List Str
|
||||
pub fn str_split<'a, 'ctx, 'env>(
|
||||
env: &Env<'a, 'ctx, 'env>,
|
||||
|
@ -268,48 +280,19 @@ fn decode_from_utf8_result<'a, 'ctx, 'env>(
|
|||
let ctx = env.context;
|
||||
|
||||
let fields = match env.ptr_bytes {
|
||||
8 => [
|
||||
8 | 4 => [
|
||||
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 => {
|
||||
8 | 4 => {
|
||||
let result_ptr_cast = env
|
||||
.builder
|
||||
.build_bitcast(
|
||||
|
|
|
@ -103,7 +103,6 @@ fn build_eq_builtin<'a, 'ctx, 'env>(
|
|||
Builtin::Float128 => float_cmp(FloatPredicate::OEQ, "eq_f128"),
|
||||
Builtin::Float64 => float_cmp(FloatPredicate::OEQ, "eq_f64"),
|
||||
Builtin::Float32 => float_cmp(FloatPredicate::OEQ, "eq_f32"),
|
||||
Builtin::Float16 => float_cmp(FloatPredicate::OEQ, "eq_f16"),
|
||||
|
||||
Builtin::Str => str_equal(env, lhs_val, rhs_val),
|
||||
Builtin::List(elem) => build_list_eq(
|
||||
|
@ -156,6 +155,8 @@ fn build_eq<'a, 'ctx, 'env>(
|
|||
rhs_val.into_struct_value(),
|
||||
),
|
||||
|
||||
Layout::LambdaSet(_) => unreachable!("cannot compare closures"),
|
||||
|
||||
Layout::Union(union_layout) => build_tag_eq(
|
||||
env,
|
||||
layout_ids,
|
||||
|
@ -245,7 +246,6 @@ fn build_neq_builtin<'a, 'ctx, 'env>(
|
|||
Builtin::Float128 => float_cmp(FloatPredicate::ONE, "neq_f128"),
|
||||
Builtin::Float64 => float_cmp(FloatPredicate::ONE, "neq_f64"),
|
||||
Builtin::Float32 => float_cmp(FloatPredicate::ONE, "neq_f32"),
|
||||
Builtin::Float16 => float_cmp(FloatPredicate::ONE, "neq_f16"),
|
||||
|
||||
Builtin::Str => {
|
||||
let is_equal = str_equal(env, lhs_val, rhs_val).into_int_value();
|
||||
|
@ -336,6 +336,7 @@ fn build_neq<'a, 'ctx, 'env>(
|
|||
Layout::RecursivePointer => {
|
||||
unreachable!("recursion pointers should never be compared directly")
|
||||
}
|
||||
Layout::LambdaSet(_) => unreachable!("cannot compare closure"),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -27,6 +27,7 @@ pub fn basic_type_from_layout<'a, 'ctx, 'env>(
|
|||
|
||||
match layout {
|
||||
Struct(sorted_fields) => basic_type_from_record(env, sorted_fields),
|
||||
LambdaSet(lambda_set) => basic_type_from_layout(env, &lambda_set.runtime_representation()),
|
||||
Union(union_layout) => {
|
||||
use UnionLayout::*;
|
||||
|
||||
|
@ -96,7 +97,6 @@ pub fn basic_type_from_builtin<'a, 'ctx, 'env>(
|
|||
Float128 => context.f128_type().as_basic_type_enum(),
|
||||
Float64 => context.f64_type().as_basic_type_enum(),
|
||||
Float32 => context.f32_type().as_basic_type_enum(),
|
||||
Float16 => context.f16_type().as_basic_type_enum(),
|
||||
Dict(_, _) | EmptyDict => zig_dict_type(env).into(),
|
||||
Set(_) | EmptySet => zig_dict_type(env).into(),
|
||||
List(_) | EmptyList => zig_list_type(env).into(),
|
||||
|
|
|
@ -626,6 +626,14 @@ fn modify_refcount_layout_build_function<'a, 'ctx, 'env>(
|
|||
Some(function)
|
||||
}
|
||||
},
|
||||
LambdaSet(lambda_set) => modify_refcount_layout_build_function(
|
||||
env,
|
||||
parent,
|
||||
layout_ids,
|
||||
mode,
|
||||
when_recursive,
|
||||
&lambda_set.runtime_representation(),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -3,42 +3,57 @@
|
|||
## 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.
|
||||
- [x] Get a wasm backend working for some of the number tests.
|
||||
- [x] Use a separate `gen_wasm` directory for now, to avoid trying to do bringup and integration at the same time.
|
||||
- Get the fundamentals working
|
||||
|
||||
- [x] Come up with a way to do control flow
|
||||
- [x] Flesh out the details of value representations between local variables and stack memory
|
||||
- [x] Set up a way to write tests with any return value rather than just i64 and f64
|
||||
- [x] Implement stack memory
|
||||
- [x] Push and pop stack frames
|
||||
- [x] Deal with returning structs
|
||||
- [x] Distinguish which variables go in locals, own stack frame, caller stack frame, etc.
|
||||
- [ ] Ensure early Return statements don't skip stack cleanup
|
||||
- [ ] Vendor-in parity_wasm library so that we can use `bumpalo::Vec`
|
||||
- [ ] Implement relocations
|
||||
- Requires knowing the _byte_ offset of each call site. This is awkward as the backend builds a `Vec<Instruction>` rather than a `Vec<u8>`. It may be worth serialising each instruction as it is inserted.
|
||||
|
||||
- Refactor for code sharing with CPU backends
|
||||
|
||||
- [ ] Implement a `scan_ast` pre-pass like `Backend` does, but for reusing Wasm locals rather than CPU registers
|
||||
- [ ] Extract a trait from `WasmBackend` that looks as similar as possible to `Backend`, to prepare for code sharing
|
||||
- [ ] Refactor to actually share code between `WasmBackend` and `Backend` if it seems feasible
|
||||
|
||||
- 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:
|
||||
This way of representing control flow is similar to parts of the Roc AST like `When`, `If` and `LetRec`. But Mono IR converts this to jumps and join points, which are more of a Control Flow Graph than a tree. We need to map back from graph to a tree again in the Wasm backend.
|
||||
|
||||
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.
|
||||
Our solution is to wrap all joinpoint/jump graphs in an outer `loop`, with nested `block`s inside it.
|
||||
|
||||
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.
|
||||
### Possible future optimisations
|
||||
|
||||
> 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
|
||||
There are other algorithms available that may result in more optimised control flow. We are not focusing on that for our development backend, but here are some notes for future reference.
|
||||
|
||||
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]. 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).
|
||||
|
||||
> By the way, apparently "binaryen" rhymes with "Targaryen", the family name from the "Game of Thrones" TV series
|
||||
|
||||
There is also an improvement on Relooper called ["Stackifier"](https://medium.com/leaningtech/solving-the-structured-control-flow-problem-once-and-for-all-5123117b1ee2). It can reorder the joinpoints and jumps to make code more efficient. (It is also has things Roc wouldn't need but C++ does, like support for "irreducible" graphs that include `goto`).
|
||||
|
||||
[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.
|
||||
|
@ -84,29 +99,30 @@ The Mono IR contains two functions, `Num.add` and `main`, so we generate two cor
|
|||
|
||||
(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
|
||||
i64.const 1 ; stack=[1]
|
||||
local.set 0 ; stack=[] local0=1
|
||||
i64.const 2 ; stack=[2] local0=1
|
||||
local.set 1 ; stack=[] local0=1 local1=2
|
||||
local.get 0 ; stack=[1] local0=1 local1=2
|
||||
local.get 1 ; stack=[1,2] local0=1 local1=2
|
||||
call 0 ; stack=[3] local0=1 local1=2
|
||||
local.set 2 ; stack=[] local0=1 local1=2 local2=3
|
||||
i64.const 4 ; stack=[4] local0=1 local1=2 local2=3
|
||||
local.set 3 ; stack=[] local0=1 local1=2 local2=3 local3=4
|
||||
local.get 2 ; stack=[3] local0=1 local1=2 local2=3 local3=4
|
||||
local.get 3 ; stack=[3,4] local0=1 local1=2 local2=3 local3=4
|
||||
call 0 ; 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).
|
||||
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 (which is all of them in this example!). 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.
|
||||
The optimised functions have no local variables at all for this example. (Of course, this is an oversimplified toy example! It might not be so extreme in a real program.)
|
||||
|
||||
```
|
||||
(func (;0;) (param i64 i64) (result i64)
|
||||
local.get 0
|
||||
|
@ -116,9 +132,20 @@ The optimised functions have no local variables, and the code shrinks to about 6
|
|||
i64.const 1
|
||||
i64.const 2
|
||||
call 0
|
||||
i64.const 4)
|
||||
i64.const 4
|
||||
call 0)
|
||||
```
|
||||
|
||||
### Reducing sets and gets
|
||||
|
||||
It would be nice to find some cheap optimisation to reduce the number of `local.set` and `local.get` instructions.
|
||||
|
||||
We don't need a `local` if the value we want is already at the top of the VM stack. In fact, for our example above, it just so happens that if we simply skip generating the `local.set` instructions, everything _does_ appear on the VM stack in the right order, which means we can skip the `local.get` too. It ends up being very close to the fully optimised version! I assume this is because the Mono IR within the function is in dependency order, but I'm not sure...
|
||||
|
||||
Of course the trick is to do this reliably for more complex dependency graphs. I am investigating whether we can do it by optimistically assuming it's OK not to create a local, and then keeping track of which symbols are at which positions in the VM stack after every instruction. Then when we need to use a symbol we can first check if it's on the VM stack and only create a local if it's not. In cases where we _do_ need to create a local, we need to go back and insert a `local.set` instruction at an earlier point in the program. We can make this fast by waiting to do all of the insertions in one batch when we're finalising the procedure.
|
||||
|
||||
For a while we thought it would be very helpful to reuse the same local for multiple symbols at different points in the program. And we already have similar code in the CPU backends for register allocation. But on further examination, it doesn't actually buy us much! In our example above, we would still have the same number of `local.set` and `local.get` instructions - they'd just be operating on two locals instead of four! That doesn't shrink much code. Only the declaration at the top of the function would shrink from `(local i64 i64 i64 i64)` to `(local i64 i64)`... and in fact that's only smaller in the text format, it's the same size in the binary format! So the `scan_ast` pass doesn't seem worthwhile for Wasm.
|
||||
|
||||
## 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.
|
||||
|
@ -137,7 +164,7 @@ When we are talking about how we store values in _memory_, I'll use the term _st
|
|||
|
||||
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
|
||||
## Calling conventions & stack memory
|
||||
|
||||
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.
|
||||
|
||||
|
@ -145,11 +172,17 @@ Function arguments are restricted to the four value types, `i32`, `i64`, `f32` a
|
|||
|
||||
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.
|
||||
Well, remember, "stack memory" is not a special kind of memory in WebAssembly, and is separate from the VM stack. It's just an area of our memory where we implement a stack data structure. But there are some conventions that it makes sense to follow so that we can easily link to Wasm code generated from Zig or other languages.
|
||||
|
||||
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.
|
||||
### Observations from compiled C code
|
||||
|
||||
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).
|
||||
- `global 0` is used as the stack pointer, and its value is normally copied to a `local` as well (presumably because locals tend to be assigned to CPU registers)
|
||||
- Stack memory grows downwards
|
||||
- If a C function returns a struct, the compiled WebAssembly function has no return value, but instead has an extra _argument_. The argument is an `i32` pointer to space allocated in the caller's stack, that the called function can write to.
|
||||
- There is no maximum number of arguments for a WebAssembly function, and arguments are not passed via _stack memory_. This makes sense because the _VM stack_ has no size limit. It's like having a CPU with an unlimited number of registers.
|
||||
- Stack memory is only used for allocating local variables, not for passing arguments. And it's only used for values that cannot be stored in one of WebAssembly's primitive values (`i32`, `i64`, `f32`, `f64`).
|
||||
|
||||
These observations are based on experiments 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
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use parity_wasm::builder;
|
||||
use parity_wasm::builder::{CodeLocation, ModuleBuilder};
|
||||
use parity_wasm::builder::{CodeLocation, FunctionDefinition, ModuleBuilder, SignatureBuilder};
|
||||
use parity_wasm::elements::{
|
||||
BlockType, Instruction, Instruction::*, Instructions, Local, ValueType,
|
||||
};
|
||||
|
@ -10,47 +10,26 @@ use roc_module::symbol::Symbol;
|
|||
use roc_mono::ir::{CallType, Expr, JoinPointId, Literal, Proc, Stmt};
|
||||
use roc_mono::layout::{Builtin, Layout};
|
||||
|
||||
use crate::layout::WasmLayout;
|
||||
use crate::storage::SymbolStorage;
|
||||
use crate::{
|
||||
copy_memory, pop_stack_frame, push_stack_frame, round_up_to_alignment, LocalId, PTR_SIZE,
|
||||
PTR_TYPE,
|
||||
};
|
||||
|
||||
// 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(Clone, Copy, Debug)]
|
||||
struct WasmLayout {
|
||||
value_type: ValueType,
|
||||
stack_memory: u32,
|
||||
}
|
||||
|
||||
impl WasmLayout {
|
||||
fn new(layout: &Layout) -> Result<Self, String> {
|
||||
match layout {
|
||||
Layout::Builtin(Builtin::Int1 | Builtin::Int8 | Builtin::Int16 | Builtin::Int32) => {
|
||||
Ok(Self {
|
||||
value_type: ValueType::I32,
|
||||
stack_memory: 0,
|
||||
})
|
||||
}
|
||||
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)),
|
||||
}
|
||||
}
|
||||
enum LocalKind {
|
||||
Parameter,
|
||||
Variable,
|
||||
}
|
||||
|
||||
// TODO: use Bumpalo Vec once parity_wasm supports general iterators (>=0.43)
|
||||
pub struct WasmBackend<'a> {
|
||||
// Module: Wasm AST
|
||||
pub builder: ModuleBuilder,
|
||||
|
@ -62,12 +41,12 @@ pub struct WasmBackend<'a> {
|
|||
|
||||
// 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,
|
||||
stack_memory: i32,
|
||||
stack_frame_pointer: Option<LocalId>,
|
||||
symbol_storage_map: MutMap<Symbol, SymbolStorage>,
|
||||
/// how many blocks deep are we (used for jumps)
|
||||
block_depth: u32,
|
||||
|
@ -87,12 +66,12 @@ impl<'a> WasmBackend<'a> {
|
|||
|
||||
// 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,
|
||||
stack_frame_pointer: None,
|
||||
symbol_storage_map: MutMap::default(),
|
||||
block_depth: 0,
|
||||
joinpoint_label_map: MutMap::default(),
|
||||
|
@ -107,48 +86,18 @@ impl<'a> WasmBackend<'a> {
|
|||
|
||||
// Functions: internal state & IR mappings
|
||||
self.stack_memory = 0;
|
||||
self.stack_frame_pointer = None;
|
||||
self.symbol_storage_map.clear();
|
||||
// joinpoint_label_map.clear();
|
||||
self.joinpoint_label_map.clear();
|
||||
assert_eq!(self.block_depth, 0);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
let signature_builder = self.start_proc(&proc);
|
||||
|
||||
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 function_def = self.finalize_proc(signature_builder);
|
||||
let location = self.builder.push_function(function_def);
|
||||
let function_index = location.body;
|
||||
self.proc_symbol_map.insert(sym, location);
|
||||
|
@ -157,32 +106,169 @@ impl<'a> WasmBackend<'a> {
|
|||
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));
|
||||
fn start_proc(&mut self, proc: &Proc<'a>) -> SignatureBuilder {
|
||||
let ret_layout = WasmLayout::new(&proc.ret_layout);
|
||||
|
||||
let signature_builder = if let WasmLayout::StackMemory { .. } = ret_layout {
|
||||
self.arg_types.push(PTR_TYPE);
|
||||
self.start_block(BlockType::NoResult); // block to ensure all paths pop stack memory (if any)
|
||||
builder::signature()
|
||||
} else {
|
||||
let ret_type = ret_layout.value_type();
|
||||
self.start_block(BlockType::Value(ret_type)); // block to ensure all paths pop stack memory (if any)
|
||||
builder::signature().with_result(ret_type)
|
||||
};
|
||||
|
||||
for (layout, symbol) in proc.args {
|
||||
self.insert_local(WasmLayout::new(layout), *symbol, LocalKind::Parameter);
|
||||
}
|
||||
let local_id = LocalId(index as u32);
|
||||
let storage = SymbolStorage(local_id, layout);
|
||||
self.symbol_storage_map.insert(symbol, storage);
|
||||
local_id
|
||||
|
||||
signature_builder.with_params(self.arg_types.clone())
|
||||
}
|
||||
|
||||
fn get_symbol_storage(&self, sym: &Symbol) -> Result<&SymbolStorage, String> {
|
||||
self.symbol_storage_map.get(sym).ok_or_else(|| {
|
||||
format!(
|
||||
fn finalize_proc(&mut self, signature_builder: SignatureBuilder) -> FunctionDefinition {
|
||||
self.end_block(); // end the block from start_proc, to ensure all paths pop stack memory (if any)
|
||||
|
||||
let mut final_instructions = Vec::with_capacity(self.instructions.len() + 10);
|
||||
|
||||
if self.stack_memory > 0 {
|
||||
push_stack_frame(
|
||||
&mut final_instructions,
|
||||
self.stack_memory,
|
||||
self.stack_frame_pointer.unwrap(),
|
||||
);
|
||||
}
|
||||
|
||||
final_instructions.extend(self.instructions.drain(0..));
|
||||
|
||||
if self.stack_memory > 0 {
|
||||
pop_stack_frame(
|
||||
&mut final_instructions,
|
||||
self.stack_memory,
|
||||
self.stack_frame_pointer.unwrap(),
|
||||
);
|
||||
}
|
||||
final_instructions.push(End);
|
||||
|
||||
builder::function()
|
||||
.with_signature(signature_builder.build_sig())
|
||||
.body()
|
||||
.with_locals(self.locals.clone())
|
||||
.with_instructions(Instructions::new(final_instructions))
|
||||
.build() // body
|
||||
.build() // function
|
||||
}
|
||||
|
||||
fn insert_local(
|
||||
&mut self,
|
||||
wasm_layout: WasmLayout,
|
||||
symbol: Symbol,
|
||||
kind: LocalKind,
|
||||
) -> SymbolStorage {
|
||||
let local_id = LocalId((self.arg_types.len() + self.locals.len()) as u32);
|
||||
|
||||
let storage = match kind {
|
||||
LocalKind::Parameter => {
|
||||
// Already stack-allocated by the caller if needed.
|
||||
self.arg_types.push(wasm_layout.value_type());
|
||||
match wasm_layout {
|
||||
WasmLayout::LocalOnly(value_type, size) => SymbolStorage::ParamPrimitive {
|
||||
local_id,
|
||||
value_type,
|
||||
size,
|
||||
},
|
||||
|
||||
WasmLayout::HeapMemory => SymbolStorage::ParamPrimitive {
|
||||
local_id,
|
||||
value_type: PTR_TYPE,
|
||||
size: PTR_SIZE,
|
||||
},
|
||||
|
||||
WasmLayout::StackMemory {
|
||||
size,
|
||||
alignment_bytes,
|
||||
} => SymbolStorage::ParamStackMemory {
|
||||
local_id,
|
||||
size,
|
||||
alignment_bytes,
|
||||
},
|
||||
}
|
||||
}
|
||||
LocalKind::Variable => {
|
||||
self.locals.push(Local::new(1, wasm_layout.value_type()));
|
||||
|
||||
match wasm_layout {
|
||||
WasmLayout::LocalOnly(value_type, size) => SymbolStorage::VarPrimitive {
|
||||
local_id,
|
||||
value_type,
|
||||
size,
|
||||
},
|
||||
|
||||
WasmLayout::HeapMemory => SymbolStorage::VarHeapMemory { local_id },
|
||||
|
||||
WasmLayout::StackMemory {
|
||||
size,
|
||||
alignment_bytes,
|
||||
} => {
|
||||
let offset =
|
||||
round_up_to_alignment(self.stack_memory, alignment_bytes as i32);
|
||||
self.stack_memory = offset + size as i32;
|
||||
|
||||
match self.stack_frame_pointer {
|
||||
None => {
|
||||
// This is the first stack-memory variable in the function
|
||||
// That means we can reuse it as the stack frame pointer,
|
||||
// and it will get initialised at the start of the function
|
||||
self.stack_frame_pointer = Some(local_id);
|
||||
}
|
||||
|
||||
Some(frame_ptr_id) => {
|
||||
// This local points to the base of a struct, at an offset from the stack frame pointer
|
||||
// Having one local per variable means params and locals work the same way in code gen.
|
||||
// (alternatively we could use one frame pointer + offset for all struct variables)
|
||||
self.instructions.extend([
|
||||
GetLocal(frame_ptr_id.0),
|
||||
I32Const(offset),
|
||||
I32Add,
|
||||
SetLocal(local_id.0),
|
||||
]);
|
||||
}
|
||||
};
|
||||
|
||||
SymbolStorage::VarStackMemory {
|
||||
local_id,
|
||||
size,
|
||||
offset: offset as u32,
|
||||
alignment_bytes,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
self.symbol_storage_map.insert(symbol, storage.clone());
|
||||
|
||||
storage
|
||||
}
|
||||
|
||||
fn get_symbol_storage(&self, sym: &Symbol) -> &SymbolStorage {
|
||||
self.symbol_storage_map.get(sym).unwrap_or_else(|| {
|
||||
panic!(
|
||||
"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 local_id_from_symbol(&self, sym: &Symbol) -> LocalId {
|
||||
let storage = self.get_symbol_storage(sym);
|
||||
storage.local_id()
|
||||
}
|
||||
|
||||
fn load_symbol(&mut self, sym: &Symbol) {
|
||||
let storage = self.get_symbol_storage(sym);
|
||||
let index: u32 = storage.local_id().0;
|
||||
self.instructions.push(GetLocal(index));
|
||||
}
|
||||
|
||||
/// start a loop that leaves a value on the stack
|
||||
|
@ -193,12 +279,9 @@ impl<'a> WasmBackend<'a> {
|
|||
self.instructions.push(Loop(BlockType::Value(value_type)));
|
||||
}
|
||||
|
||||
fn start_block(&mut self) {
|
||||
fn start_block(&mut self, block_type: BlockType) {
|
||||
self.block_depth += 1;
|
||||
|
||||
// Our blocks always end with a `return` or `br`,
|
||||
// so they never leave extra values on the stack
|
||||
self.instructions.push(Block(BlockType::NoResult));
|
||||
self.instructions.push(Block(block_type));
|
||||
}
|
||||
|
||||
fn end_block(&mut self) {
|
||||
|
@ -208,36 +291,77 @@ impl<'a> WasmBackend<'a> {
|
|||
|
||||
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
|
||||
// Simple optimisation: if we are just returning the expression, we don't need a local
|
||||
Stmt::Let(let_sym, expr, layout, Stmt::Ret(ret_sym)) if let_sym == ret_sym => {
|
||||
let wasm_layout = WasmLayout::new(layout);
|
||||
if let WasmLayout::StackMemory {
|
||||
size,
|
||||
alignment_bytes,
|
||||
} = wasm_layout
|
||||
{
|
||||
// Map this symbol to the first argument (pointer into caller's stack)
|
||||
// Saves us from having to copy it later
|
||||
let storage = SymbolStorage::ParamStackMemory {
|
||||
local_id: LocalId(0),
|
||||
size,
|
||||
alignment_bytes,
|
||||
};
|
||||
self.symbol_storage_map.insert(*let_sym, storage);
|
||||
}
|
||||
self.build_expr(let_sym, expr, layout)?;
|
||||
self.instructions.push(Return);
|
||||
self.instructions.push(Br(self.block_depth)); // jump to end of function (stack frame pop)
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Stmt::Let(sym, expr, layout, following) => {
|
||||
let wasm_layout = WasmLayout::new(layout)?;
|
||||
let local_id = self.insert_local(wasm_layout, *sym);
|
||||
let wasm_layout = WasmLayout::new(layout);
|
||||
let local_id = self
|
||||
.insert_local(wasm_layout, *sym, LocalKind::Variable)
|
||||
.local_id();
|
||||
|
||||
self.build_expr(sym, expr, layout)?;
|
||||
self.instructions.push(SetLocal(local_id.0));
|
||||
|
||||
// If this local is shared with the stack frame pointer, it's already assigned
|
||||
match self.stack_frame_pointer {
|
||||
Some(sfp) if sfp == local_id => {}
|
||||
_ => 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
|
||||
))
|
||||
use crate::storage::SymbolStorage::*;
|
||||
|
||||
let storage = self.symbol_storage_map.get(sym).unwrap();
|
||||
|
||||
match storage {
|
||||
VarStackMemory {
|
||||
local_id,
|
||||
size,
|
||||
alignment_bytes,
|
||||
..
|
||||
}
|
||||
| ParamStackMemory {
|
||||
local_id,
|
||||
size,
|
||||
alignment_bytes,
|
||||
} => {
|
||||
let from = *local_id;
|
||||
let to = LocalId(0);
|
||||
copy_memory(&mut self.instructions, from, to, *size, *alignment_bytes, 0);
|
||||
}
|
||||
|
||||
ParamPrimitive { local_id, .. }
|
||||
| VarPrimitive { local_id, .. }
|
||||
| VarHeapMemory { local_id, .. } => {
|
||||
self.instructions.push(GetLocal(local_id.0));
|
||||
self.instructions.push(Br(self.block_depth)); // jump to end of function (for stack frame pop)
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Stmt::Switch {
|
||||
|
@ -253,19 +377,16 @@ impl<'a> WasmBackend<'a> {
|
|||
|
||||
// create (number_of_branches - 1) new blocks.
|
||||
for _ in 0..branches.len() {
|
||||
self.start_block()
|
||||
self.start_block(BlockType::NoResult)
|
||||
}
|
||||
|
||||
// the LocalId of the symbol that we match on
|
||||
let matched_on = match self.symbol_storage_map.get(cond_symbol) {
|
||||
Some(SymbolStorage(local_id, _)) => local_id.0,
|
||||
None => unreachable!("symbol not defined: {:?}", cond_symbol),
|
||||
};
|
||||
let matched_on = self.local_id_from_symbol(cond_symbol);
|
||||
|
||||
// then, we jump whenever the value under scrutiny is equal to the value of a branch
|
||||
for (i, (value, _, _)) in branches.iter().enumerate() {
|
||||
// put the cond_symbol on the top of the stack
|
||||
self.instructions.push(GetLocal(matched_on));
|
||||
self.instructions.push(GetLocal(matched_on.0));
|
||||
|
||||
self.instructions.push(I32Const(*value as i32));
|
||||
|
||||
|
@ -299,13 +420,15 @@ impl<'a> WasmBackend<'a> {
|
|||
// make locals for join pointer parameters
|
||||
let mut jp_parameter_local_ids = std::vec::Vec::with_capacity(parameters.len());
|
||||
for parameter in parameters.iter() {
|
||||
let wasm_layout = WasmLayout::new(¶meter.layout)?;
|
||||
let local_id = self.insert_local(wasm_layout, parameter.symbol);
|
||||
let wasm_layout = WasmLayout::new(¶meter.layout);
|
||||
let local_id = self
|
||||
.insert_local(wasm_layout, parameter.symbol, LocalKind::Variable)
|
||||
.local_id();
|
||||
|
||||
jp_parameter_local_ids.push(local_id);
|
||||
}
|
||||
|
||||
self.start_block();
|
||||
self.start_block(BlockType::NoResult);
|
||||
|
||||
self.joinpoint_label_map
|
||||
.insert(*id, (self.block_depth, jp_parameter_local_ids));
|
||||
|
@ -316,8 +439,8 @@ impl<'a> WasmBackend<'a> {
|
|||
|
||||
// A `return` inside of a `loop` seems to make it so that the `loop` itself
|
||||
// also "returns" (so, leaves on the stack) a value of the return type.
|
||||
let return_wasm_layout = WasmLayout::new(ret_layout)?;
|
||||
self.start_loop_with_return(return_wasm_layout.value_type);
|
||||
let return_wasm_layout = WasmLayout::new(ret_layout);
|
||||
self.start_loop_with_return(return_wasm_layout.value_type());
|
||||
|
||||
self.build_stmt(body, ret_layout)?;
|
||||
|
||||
|
@ -331,12 +454,8 @@ impl<'a> WasmBackend<'a> {
|
|||
|
||||
// put the arguments on the stack
|
||||
for (symbol, local_id) in arguments.iter().zip(locals.iter()) {
|
||||
let argument = match self.symbol_storage_map.get(symbol) {
|
||||
Some(SymbolStorage(local_id, _)) => local_id.0,
|
||||
None => unreachable!("symbol not defined: {:?}", symbol),
|
||||
};
|
||||
|
||||
self.instructions.push(GetLocal(argument));
|
||||
let argument = self.local_id_from_symbol(symbol);
|
||||
self.instructions.push(GetLocal(argument.0));
|
||||
self.instructions.push(SetLocal(local_id.0));
|
||||
}
|
||||
|
||||
|
@ -365,7 +484,7 @@ impl<'a> WasmBackend<'a> {
|
|||
}) => match call_type {
|
||||
CallType::ByName { name: func_sym, .. } => {
|
||||
for arg in *arguments {
|
||||
self.load_from_symbol(arg)?;
|
||||
self.load_symbol(arg);
|
||||
}
|
||||
let function_location = self.proc_symbol_map.get(func_sym).ok_or(format!(
|
||||
"Cannot find function {:?} called from {:?}",
|
||||
|
@ -381,38 +500,112 @@ impl<'a> WasmBackend<'a> {
|
|||
x => Err(format!("the call type, {:?}, is not yet implemented", x)),
|
||||
},
|
||||
|
||||
Expr::Struct(fields) => self.create_struct(sym, layout, fields),
|
||||
|
||||
x => Err(format!("Expression is not yet implemented {:?}", x)),
|
||||
}
|
||||
}
|
||||
|
||||
fn load_literal(&mut self, lit: &Literal<'a>, layout: &Layout<'a>) -> Result<(), String> {
|
||||
match lit {
|
||||
Literal::Bool(x) => {
|
||||
self.instructions.push(I32Const(*x as i32));
|
||||
Ok(())
|
||||
}
|
||||
Literal::Byte(x) => {
|
||||
self.instructions.push(I32Const(*x as i32));
|
||||
Ok(())
|
||||
}
|
||||
Literal::Int(x) => {
|
||||
match layout {
|
||||
Layout::Builtin(Builtin::Int32) => {
|
||||
self.instructions.push(I32Const(*x as i32));
|
||||
}
|
||||
Layout::Builtin(Builtin::Int64) => {
|
||||
self.instructions.push(I64Const(*x as i64));
|
||||
}
|
||||
x => panic!("loading literal, {:?}, is not yet implemented", x),
|
||||
let instruction = match lit {
|
||||
Literal::Bool(x) => I32Const(*x as i32),
|
||||
Literal::Byte(x) => I32Const(*x as i32),
|
||||
Literal::Int(x) => match layout {
|
||||
Layout::Builtin(Builtin::Int64) => I64Const(*x as i64),
|
||||
Layout::Builtin(
|
||||
Builtin::Int32
|
||||
| Builtin::Int16
|
||||
| Builtin::Int8
|
||||
| Builtin::Int1
|
||||
| Builtin::Usize,
|
||||
) => I32Const(*x as i32),
|
||||
x => {
|
||||
return Err(format!("loading literal, {:?}, is not yet implemented", x));
|
||||
}
|
||||
Ok(())
|
||||
},
|
||||
Literal::Float(x) => match layout {
|
||||
Layout::Builtin(Builtin::Float64) => F64Const((*x as f64).to_bits()),
|
||||
Layout::Builtin(Builtin::Float32) => F32Const((*x as f32).to_bits()),
|
||||
x => {
|
||||
return Err(format!("loading literal, {:?}, is not yet implemented", x));
|
||||
}
|
||||
},
|
||||
x => {
|
||||
return Err(format!("loading literal, {:?}, is not yet implemented", x));
|
||||
}
|
||||
Literal::Float(x) => {
|
||||
let val: f64 = *x;
|
||||
self.instructions.push(F64Const(val.to_bits()));
|
||||
Ok(())
|
||||
};
|
||||
self.instructions.push(instruction);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn create_struct(
|
||||
&mut self,
|
||||
sym: &Symbol,
|
||||
layout: &Layout<'a>,
|
||||
fields: &'a [Symbol],
|
||||
) -> Result<(), String> {
|
||||
let storage = self.get_symbol_storage(sym).to_owned();
|
||||
|
||||
if let Layout::Struct(field_layouts) = layout {
|
||||
match storage {
|
||||
SymbolStorage::VarStackMemory { local_id, size, .. }
|
||||
| SymbolStorage::ParamStackMemory { local_id, size, .. } => {
|
||||
if size > 0 {
|
||||
let mut relative_offset = 0;
|
||||
for (field, _) in fields.iter().zip(field_layouts.iter()) {
|
||||
relative_offset += self.copy_symbol_to_pointer_at_offset(
|
||||
local_id,
|
||||
relative_offset,
|
||||
field,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
return Err(format!("Not supported yet: zero-size struct at {:?}", sym));
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
return Err(format!(
|
||||
"Cannot create struct {:?} with storage {:?}",
|
||||
sym, storage
|
||||
));
|
||||
}
|
||||
}
|
||||
x => Err(format!("loading literal, {:?}, is not yet implemented", x)),
|
||||
} else {
|
||||
// Struct expression but not Struct layout => single element. Copy it.
|
||||
let field_storage = self.get_symbol_storage(&fields[0]).to_owned();
|
||||
self.copy_storage(&storage, &field_storage);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn copy_symbol_to_pointer_at_offset(
|
||||
&mut self,
|
||||
to_ptr: LocalId,
|
||||
to_offset: u32,
|
||||
from_symbol: &Symbol,
|
||||
) -> u32 {
|
||||
let from_storage = self.get_symbol_storage(from_symbol).to_owned();
|
||||
from_storage.copy_to_memory(&mut self.instructions, to_ptr, to_offset)
|
||||
}
|
||||
|
||||
fn copy_storage(&mut self, to: &SymbolStorage, from: &SymbolStorage) {
|
||||
let has_stack_memory = to.has_stack_memory();
|
||||
debug_assert!(from.has_stack_memory() == has_stack_memory);
|
||||
|
||||
if !has_stack_memory {
|
||||
debug_assert!(from.value_type() == to.value_type());
|
||||
self.instructions.push(GetLocal(from.local_id().0));
|
||||
self.instructions.push(SetLocal(to.local_id().0));
|
||||
} else {
|
||||
let (size, alignment_bytes) = from.stack_size_and_alignment();
|
||||
copy_memory(
|
||||
&mut self.instructions,
|
||||
from.local_id(),
|
||||
to.local_id(),
|
||||
size,
|
||||
alignment_bytes,
|
||||
0,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -423,10 +616,10 @@ impl<'a> WasmBackend<'a> {
|
|||
return_layout: &Layout<'a>,
|
||||
) -> Result<(), String> {
|
||||
for arg in args {
|
||||
self.load_from_symbol(arg)?;
|
||||
self.load_symbol(arg);
|
||||
}
|
||||
let wasm_layout = WasmLayout::new(return_layout)?;
|
||||
self.build_instructions_lowlevel(lowlevel, wasm_layout.value_type)?;
|
||||
let wasm_layout = WasmLayout::new(return_layout);
|
||||
self.build_instructions_lowlevel(lowlevel, wasm_layout.value_type())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -441,7 +634,7 @@ impl<'a> WasmBackend<'a> {
|
|||
// 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?
|
||||
// Wasm type might not be enough, may need to sign-extend i8 etc. Maybe in load_symbol?
|
||||
LowLevel::NumAdd => match return_value_type {
|
||||
ValueType::I32 => &[I32Add],
|
||||
ValueType::I64 => &[I64Add],
|
||||
|
|
82
compiler/gen_wasm/src/layout.rs
Normal file
82
compiler/gen_wasm/src/layout.rs
Normal file
|
@ -0,0 +1,82 @@
|
|||
use parity_wasm::elements::ValueType;
|
||||
use roc_mono::layout::{Layout, UnionLayout};
|
||||
|
||||
use crate::{PTR_SIZE, PTR_TYPE};
|
||||
|
||||
// See README for background information on Wasm locals, memory and function calls
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum WasmLayout {
|
||||
// Primitive number value. Just a Wasm local, without any stack memory.
|
||||
// For example, Roc i8 is represented as Wasm i32. Store the type and the original size.
|
||||
LocalOnly(ValueType, u32),
|
||||
|
||||
// Local pointer to stack memory
|
||||
StackMemory { size: u32, alignment_bytes: u32 },
|
||||
|
||||
// Local pointer to heap memory
|
||||
HeapMemory,
|
||||
}
|
||||
|
||||
impl WasmLayout {
|
||||
pub fn new(layout: &Layout) -> Self {
|
||||
use roc_mono::layout::Builtin::*;
|
||||
use UnionLayout::*;
|
||||
use ValueType::*;
|
||||
|
||||
let size = layout.stack_size(PTR_SIZE);
|
||||
let alignment_bytes = layout.alignment_bytes(PTR_SIZE);
|
||||
|
||||
match layout {
|
||||
Layout::Builtin(Int32 | Int16 | Int8 | Int1 | Usize) => Self::LocalOnly(I32, size),
|
||||
|
||||
Layout::Builtin(Int64) => Self::LocalOnly(I64, size),
|
||||
|
||||
Layout::Builtin(Float32) => Self::LocalOnly(F32, size),
|
||||
|
||||
Layout::Builtin(Float64) => Self::LocalOnly(F64, size),
|
||||
|
||||
Layout::Builtin(
|
||||
Int128
|
||||
| Decimal
|
||||
| Float128
|
||||
| Str
|
||||
| Dict(_, _)
|
||||
| Set(_)
|
||||
| List(_)
|
||||
| EmptyStr
|
||||
| EmptyList
|
||||
| EmptyDict
|
||||
| EmptySet,
|
||||
)
|
||||
| Layout::Struct(_)
|
||||
| Layout::LambdaSet(_)
|
||||
| Layout::Union(NonRecursive(_)) => Self::StackMemory {
|
||||
size,
|
||||
alignment_bytes,
|
||||
},
|
||||
|
||||
Layout::Union(
|
||||
Recursive(_)
|
||||
| NonNullableUnwrapped(_)
|
||||
| NullableWrapped { .. }
|
||||
| NullableUnwrapped { .. },
|
||||
)
|
||||
| Layout::RecursivePointer => Self::HeapMemory,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn value_type(&self) -> ValueType {
|
||||
match self {
|
||||
Self::LocalOnly(type_, _) => *type_,
|
||||
_ => PTR_TYPE,
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn stack_memory(&self) -> u32 {
|
||||
match self {
|
||||
Self::StackMemory { size, .. } => *size,
|
||||
_ => 0,
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,9 +1,11 @@
|
|||
mod backend;
|
||||
pub mod from_wasm32_memory;
|
||||
mod layout;
|
||||
mod storage;
|
||||
|
||||
use bumpalo::Bump;
|
||||
use parity_wasm::builder;
|
||||
use parity_wasm::elements::Internal;
|
||||
use parity_wasm::elements::{Instruction, Instruction::*, Internal, ValueType};
|
||||
|
||||
use roc_collections::all::{MutMap, MutSet};
|
||||
use roc_module::symbol::{Interns, Symbol};
|
||||
|
@ -12,6 +14,21 @@ use roc_mono::layout::LayoutIds;
|
|||
|
||||
use crate::backend::WasmBackend;
|
||||
|
||||
const PTR_SIZE: u32 = 4;
|
||||
const PTR_TYPE: ValueType = ValueType::I32;
|
||||
|
||||
// All usages of these alignment constants take u32, so an enum wouldn't add any safety.
|
||||
pub const ALIGN_1: u32 = 0;
|
||||
pub const ALIGN_2: u32 = 1;
|
||||
pub const ALIGN_4: u32 = 2;
|
||||
pub const ALIGN_8: u32 = 3;
|
||||
|
||||
pub const STACK_POINTER_GLOBAL_ID: u32 = 0;
|
||||
pub const STACK_ALIGNMENT_BYTES: i32 = 16;
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct LocalId(pub u32);
|
||||
|
||||
pub struct Env<'a> {
|
||||
pub arena: &'a Bump, // not really using this much, parity_wasm works with std::vec a lot
|
||||
pub interns: Interns,
|
||||
|
@ -21,7 +38,18 @@ pub struct Env<'a> {
|
|||
pub fn build_module<'a>(
|
||||
env: &'a Env,
|
||||
procedures: MutMap<(Symbol, ProcLayout<'a>), Proc<'a>>,
|
||||
) -> Result<std::vec::Vec<u8>, String> {
|
||||
) -> Result<Vec<u8>, String> {
|
||||
let (builder, _) = build_module_help(env, procedures)?;
|
||||
let module = builder.build();
|
||||
module
|
||||
.to_bytes()
|
||||
.map_err(|e| -> String { format!("Error serialising Wasm module {:?}", e) })
|
||||
}
|
||||
|
||||
pub fn build_module_help<'a>(
|
||||
env: &'a Env,
|
||||
procedures: MutMap<(Symbol, ProcLayout<'a>), Proc<'a>>,
|
||||
) -> Result<(builder::ModuleBuilder, u32), String> {
|
||||
let mut backend = WasmBackend::new();
|
||||
let mut layout_ids = LayoutIds::default();
|
||||
|
||||
|
@ -38,8 +66,9 @@ pub fn build_module<'a>(
|
|||
let mut procedures: std::vec::Vec<_> = procedures.into_iter().collect();
|
||||
procedures.sort_by(|a, b| b.0 .0.cmp(&a.0 .0));
|
||||
|
||||
let mut function_index: u32 = 0;
|
||||
for ((sym, layout), proc) in procedures {
|
||||
let function_index = backend.build_proc(proc, sym)?;
|
||||
function_index = backend.build_proc(proc, sym)?;
|
||||
if env.exposed_to_host.contains(&sym) {
|
||||
let fn_name = layout_ids
|
||||
.get_toplevel(sym, &layout)
|
||||
|
@ -54,6 +83,11 @@ pub fn build_module<'a>(
|
|||
}
|
||||
}
|
||||
|
||||
// Because of the sorting above, we know the last function in the `for` is the main function.
|
||||
// Here we grab its index and return it, so that the test_wrapper is able to call it.
|
||||
// This is a workaround until we implement object files with symbols and relocations.
|
||||
let main_function_index = function_index;
|
||||
|
||||
const MIN_MEMORY_SIZE_KB: u32 = 1024;
|
||||
const PAGE_SIZE_KB: u32 = 64;
|
||||
|
||||
|
@ -61,15 +95,98 @@ pub fn build_module<'a>(
|
|||
.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) })
|
||||
let stack_pointer_global = builder::global()
|
||||
.with_type(PTR_TYPE)
|
||||
.mutable()
|
||||
.init_expr(Instruction::I32Const((MIN_MEMORY_SIZE_KB * 1024) as i32))
|
||||
.build();
|
||||
backend.builder.push_global(stack_pointer_global);
|
||||
|
||||
Ok((backend.builder, main_function_index))
|
||||
}
|
||||
|
||||
fn encode_alignment(bytes: u32) -> u32 {
|
||||
match bytes {
|
||||
1 => ALIGN_1,
|
||||
2 => ALIGN_2,
|
||||
4 => ALIGN_4,
|
||||
8 => ALIGN_8,
|
||||
_ => panic!("{:?}-byte alignment is not supported", bytes),
|
||||
}
|
||||
}
|
||||
|
||||
fn copy_memory(
|
||||
instructions: &mut Vec<Instruction>,
|
||||
from_ptr: LocalId,
|
||||
to_ptr: LocalId,
|
||||
size: u32,
|
||||
alignment_bytes: u32,
|
||||
offset: u32,
|
||||
) {
|
||||
let alignment_flag = encode_alignment(alignment_bytes);
|
||||
let mut current_offset = offset;
|
||||
while size - current_offset >= 8 {
|
||||
instructions.push(GetLocal(to_ptr.0));
|
||||
instructions.push(GetLocal(from_ptr.0));
|
||||
instructions.push(I64Load(alignment_flag, current_offset));
|
||||
instructions.push(I64Store(alignment_flag, current_offset));
|
||||
current_offset += 8;
|
||||
}
|
||||
if size - current_offset >= 4 {
|
||||
instructions.push(GetLocal(to_ptr.0));
|
||||
instructions.push(GetLocal(from_ptr.0));
|
||||
instructions.push(I32Load(alignment_flag, current_offset));
|
||||
instructions.push(I32Store(alignment_flag, current_offset));
|
||||
current_offset += 4;
|
||||
}
|
||||
while size - current_offset > 0 {
|
||||
instructions.push(GetLocal(to_ptr.0));
|
||||
instructions.push(GetLocal(from_ptr.0));
|
||||
instructions.push(I32Load8U(alignment_flag, current_offset));
|
||||
instructions.push(I32Store8(alignment_flag, current_offset));
|
||||
current_offset += 1;
|
||||
}
|
||||
}
|
||||
|
||||
/// Round up to alignment_bytes (assumed to be a power of 2)
|
||||
pub fn round_up_to_alignment(unaligned: i32, alignment_bytes: i32) -> i32 {
|
||||
let mut aligned = unaligned;
|
||||
aligned += alignment_bytes - 1; // if lower bits are non-zero, push it over the next boundary
|
||||
aligned &= -alignment_bytes; // mask with a flag that has upper bits 1, lower bits 0
|
||||
aligned
|
||||
}
|
||||
|
||||
pub fn push_stack_frame(
|
||||
instructions: &mut Vec<Instruction>,
|
||||
size: i32,
|
||||
local_frame_pointer: LocalId,
|
||||
) {
|
||||
let aligned_size = round_up_to_alignment(size, STACK_ALIGNMENT_BYTES);
|
||||
instructions.extend([
|
||||
GetGlobal(STACK_POINTER_GLOBAL_ID),
|
||||
I32Const(aligned_size),
|
||||
I32Sub,
|
||||
TeeLocal(local_frame_pointer.0),
|
||||
SetGlobal(STACK_POINTER_GLOBAL_ID),
|
||||
]);
|
||||
}
|
||||
|
||||
pub fn pop_stack_frame(
|
||||
instructions: &mut Vec<Instruction>,
|
||||
size: i32,
|
||||
local_frame_pointer: LocalId,
|
||||
) {
|
||||
let aligned_size = round_up_to_alignment(size, STACK_ALIGNMENT_BYTES);
|
||||
instructions.extend([
|
||||
GetLocal(local_frame_pointer.0),
|
||||
I32Const(aligned_size),
|
||||
I32Add,
|
||||
SetGlobal(STACK_POINTER_GLOBAL_ID),
|
||||
]);
|
||||
}
|
||||
|
|
146
compiler/gen_wasm/src/storage.rs
Normal file
146
compiler/gen_wasm/src/storage.rs
Normal file
|
@ -0,0 +1,146 @@
|
|||
use crate::{copy_memory, LocalId, ALIGN_1, ALIGN_2, ALIGN_4, ALIGN_8};
|
||||
use parity_wasm::elements::{Instruction, Instruction::*, ValueType};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum SymbolStorage {
|
||||
VarPrimitive {
|
||||
local_id: LocalId,
|
||||
value_type: ValueType,
|
||||
size: u32,
|
||||
},
|
||||
ParamPrimitive {
|
||||
local_id: LocalId,
|
||||
value_type: ValueType,
|
||||
size: u32,
|
||||
},
|
||||
VarStackMemory {
|
||||
local_id: LocalId,
|
||||
size: u32,
|
||||
offset: u32,
|
||||
alignment_bytes: u32,
|
||||
},
|
||||
ParamStackMemory {
|
||||
local_id: LocalId,
|
||||
size: u32,
|
||||
alignment_bytes: u32,
|
||||
},
|
||||
VarHeapMemory {
|
||||
local_id: LocalId,
|
||||
},
|
||||
}
|
||||
|
||||
impl SymbolStorage {
|
||||
pub fn local_id(&self) -> LocalId {
|
||||
match self {
|
||||
Self::ParamPrimitive { local_id, .. } => *local_id,
|
||||
Self::ParamStackMemory { local_id, .. } => *local_id,
|
||||
Self::VarPrimitive { local_id, .. } => *local_id,
|
||||
Self::VarStackMemory { local_id, .. } => *local_id,
|
||||
Self::VarHeapMemory { local_id, .. } => *local_id,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn value_type(&self) -> ValueType {
|
||||
match self {
|
||||
Self::ParamPrimitive { value_type, .. } => *value_type,
|
||||
Self::VarPrimitive { value_type, .. } => *value_type,
|
||||
Self::ParamStackMemory { .. } => ValueType::I32,
|
||||
Self::VarStackMemory { .. } => ValueType::I32,
|
||||
Self::VarHeapMemory { .. } => ValueType::I32,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn has_stack_memory(&self) -> bool {
|
||||
match self {
|
||||
Self::ParamStackMemory { .. } => true,
|
||||
Self::VarStackMemory { .. } => true,
|
||||
Self::ParamPrimitive { .. } => false,
|
||||
Self::VarPrimitive { .. } => false,
|
||||
Self::VarHeapMemory { .. } => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn stack_size_and_alignment(&self) -> (u32, u32) {
|
||||
match self {
|
||||
Self::VarStackMemory {
|
||||
size,
|
||||
alignment_bytes,
|
||||
..
|
||||
}
|
||||
| Self::ParamStackMemory {
|
||||
size,
|
||||
alignment_bytes,
|
||||
..
|
||||
} => (*size, *alignment_bytes),
|
||||
|
||||
_ => (0, 0),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn copy_to_memory(
|
||||
&self,
|
||||
instructions: &mut Vec<Instruction>,
|
||||
to_pointer: LocalId,
|
||||
to_offset: u32,
|
||||
) -> u32 {
|
||||
match self {
|
||||
Self::ParamPrimitive {
|
||||
local_id,
|
||||
value_type,
|
||||
size,
|
||||
..
|
||||
}
|
||||
| Self::VarPrimitive {
|
||||
local_id,
|
||||
value_type,
|
||||
size,
|
||||
..
|
||||
} => {
|
||||
let store_instruction = match (value_type, size) {
|
||||
(ValueType::I64, 8) => I64Store(ALIGN_8, to_offset),
|
||||
(ValueType::I32, 4) => I32Store(ALIGN_4, to_offset),
|
||||
(ValueType::I32, 2) => I32Store16(ALIGN_2, to_offset),
|
||||
(ValueType::I32, 1) => I32Store8(ALIGN_1, to_offset),
|
||||
(ValueType::F32, 4) => F32Store(ALIGN_4, to_offset),
|
||||
(ValueType::F64, 8) => F64Store(ALIGN_8, to_offset),
|
||||
_ => {
|
||||
panic!("Cannot store {:?} with alignment of {:?}", value_type, size);
|
||||
}
|
||||
};
|
||||
instructions.push(GetLocal(to_pointer.0));
|
||||
instructions.push(GetLocal(local_id.0));
|
||||
instructions.push(store_instruction);
|
||||
*size
|
||||
}
|
||||
|
||||
Self::ParamStackMemory {
|
||||
local_id,
|
||||
size,
|
||||
alignment_bytes,
|
||||
}
|
||||
| Self::VarStackMemory {
|
||||
local_id,
|
||||
size,
|
||||
alignment_bytes,
|
||||
..
|
||||
} => {
|
||||
copy_memory(
|
||||
instructions,
|
||||
*local_id,
|
||||
to_pointer,
|
||||
*size,
|
||||
*alignment_bytes,
|
||||
to_offset,
|
||||
);
|
||||
*size
|
||||
}
|
||||
|
||||
Self::VarHeapMemory { local_id, .. } => {
|
||||
instructions.push(GetLocal(to_pointer.0));
|
||||
instructions.push(GetLocal(local_id.0));
|
||||
instructions.push(I32Store(ALIGN_4, to_offset));
|
||||
4
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,6 +1,10 @@
|
|||
use roc_can::builtins::builtin_defs_map;
|
||||
use roc_collections::all::{MutMap, MutSet};
|
||||
// use roc_std::{RocDec, RocList, RocOrder, RocStr};
|
||||
use crate::helpers::wasm32_test_result::Wasm32TestResult;
|
||||
use roc_gen_wasm::from_wasm32_memory::FromWasm32Memory;
|
||||
|
||||
const TEST_WRAPPER_NAME: &str = "test_wrapper";
|
||||
|
||||
fn promote_expr_to_module(src: &str) -> String {
|
||||
let mut buffer = String::from("app \"test\" provides [ main ] to \"./platform\"\n\nmain =\n");
|
||||
|
@ -16,12 +20,11 @@ fn promote_expr_to_module(src: &str) -> String {
|
|||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn helper_wasm<'a>(
|
||||
pub fn helper_wasm<'a, T: Wasm32TestResult>(
|
||||
arena: &'a bumpalo::Bump,
|
||||
src: &str,
|
||||
stdlib: &'a roc_builtins::std::StdLib,
|
||||
_is_gen_test: bool,
|
||||
_ignore_problems: bool,
|
||||
_result_type_dummy: &T,
|
||||
) -> wasmer::Instance {
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
|
@ -91,7 +94,11 @@ pub fn helper_wasm<'a>(
|
|||
exposed_to_host,
|
||||
};
|
||||
|
||||
let module_bytes = roc_gen_wasm::build_module(&env, procedures).unwrap();
|
||||
let (mut builder, main_function_index) =
|
||||
roc_gen_wasm::build_module_help(&env, procedures).unwrap();
|
||||
T::insert_test_wrapper(&mut builder, TEST_WRAPPER_NAME, main_function_index);
|
||||
|
||||
let module_bytes = builder.build().to_bytes().unwrap();
|
||||
|
||||
// for debugging (e.g. with wasm2wat)
|
||||
if false {
|
||||
|
@ -128,45 +135,40 @@ pub fn helper_wasm<'a>(
|
|||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn assert_wasm_evals_to_help<T>(src: &str, ignore_problems: bool) -> Result<T, String>
|
||||
pub fn assert_wasm_evals_to_help<T>(src: &str, expected: T) -> Result<T, String>
|
||||
where
|
||||
T: Copy,
|
||||
T: FromWasm32Memory + Wasm32TestResult,
|
||||
{
|
||||
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 instance = crate::helpers::eval::helper_wasm(&arena, src, stdlib, &expected);
|
||||
|
||||
let main_function = instance.exports.get_function("#UserApp_main_1").unwrap();
|
||||
let memory = instance.exports.get_memory("memory").unwrap();
|
||||
|
||||
match main_function.call(&[]) {
|
||||
let test_wrapper = instance.exports.get_function(TEST_WRAPPER_NAME).unwrap();
|
||||
|
||||
match test_wrapper.call(&[]) {
|
||||
Err(e) => Err(format!("{:?}", e)),
|
||||
Ok(result) => {
|
||||
let integer = match result[0] {
|
||||
wasmer::Value::I32(a) => a as i64,
|
||||
wasmer::Value::I64(a) => a,
|
||||
wasmer::Value::F64(a) => a.to_bits() as i64,
|
||||
let address = match result[0] {
|
||||
wasmer::Value::I32(a) => a,
|
||||
_ => panic!(),
|
||||
};
|
||||
|
||||
let output_ptr: &T;
|
||||
unsafe {
|
||||
output_ptr = std::mem::transmute::<&i64, &T>(&integer);
|
||||
}
|
||||
let output = <T as FromWasm32Memory>::decode(memory, address as u32);
|
||||
|
||||
Ok(*output_ptr)
|
||||
Ok(output)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[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) {
|
||||
($src:expr, $expected:expr, $ty:ty, $transform:expr) => {
|
||||
match $crate::helpers::eval::assert_wasm_evals_to_help::<$ty>($src, $expected) {
|
||||
Err(msg) => println!("{:?}", msg),
|
||||
Ok(actual) => {
|
||||
#[allow(clippy::bool_assert_comparison)]
|
||||
|
@ -176,11 +178,11 @@ macro_rules! assert_wasm_evals_to {
|
|||
};
|
||||
|
||||
($src:expr, $expected:expr, $ty:ty) => {
|
||||
$crate::assert_wasm_evals_to!($src, $expected, $ty, $crate::helpers::eval::identity, false);
|
||||
$crate::assert_wasm_evals_to!($src, $expected, $ty, $crate::helpers::eval::identity);
|
||||
};
|
||||
|
||||
($src:expr, $expected:expr, $ty:ty, $transform:expr) => {
|
||||
$crate::assert_wasm_evals_to!($src, $expected, $ty, $transform, false);
|
||||
$crate::assert_wasm_evals_to!($src, $expected, $ty, $transform);
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -192,7 +194,7 @@ macro_rules! assert_evals_to {
|
|||
($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);
|
||||
$crate::assert_wasm_evals_to!($src, $expected, $ty, $transform);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ extern crate bumpalo;
|
|||
|
||||
#[macro_use]
|
||||
pub mod eval;
|
||||
pub mod wasm32_test_result;
|
||||
|
||||
/// 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)
|
||||
|
|
278
compiler/gen_wasm/tests/helpers/wasm32_test_result.rs
Normal file
278
compiler/gen_wasm/tests/helpers/wasm32_test_result.rs
Normal file
|
@ -0,0 +1,278 @@
|
|||
use parity_wasm::builder;
|
||||
use parity_wasm::builder::ModuleBuilder;
|
||||
use parity_wasm::elements::{
|
||||
Instruction, Instruction::*, Instructions, Internal, Local, ValueType,
|
||||
};
|
||||
|
||||
use roc_gen_wasm::from_wasm32_memory::FromWasm32Memory;
|
||||
use roc_gen_wasm::*;
|
||||
use roc_std::{RocDec, RocList, RocOrder, RocStr};
|
||||
|
||||
const STACK_POINTER_LOCAL_ID: u32 = 0;
|
||||
|
||||
pub trait Wasm32TestResult {
|
||||
fn insert_test_wrapper(
|
||||
module_builder: &mut ModuleBuilder,
|
||||
wrapper_name: &str,
|
||||
main_function_index: u32,
|
||||
) {
|
||||
let instructions = Self::build_wrapper_body(main_function_index);
|
||||
|
||||
let signature = builder::signature().with_result(ValueType::I32).build_sig();
|
||||
|
||||
let stack_frame_pointer = Local::new(1, ValueType::I32);
|
||||
let function_def = builder::function()
|
||||
.with_signature(signature)
|
||||
.body()
|
||||
.with_locals(vec![stack_frame_pointer])
|
||||
.with_instructions(Instructions::new(instructions))
|
||||
.build() // body
|
||||
.build(); // function
|
||||
|
||||
let location = module_builder.push_function(function_def);
|
||||
let export = builder::export()
|
||||
.field(wrapper_name)
|
||||
.with_internal(Internal::Function(location.body))
|
||||
.build();
|
||||
|
||||
module_builder.push_export(export);
|
||||
}
|
||||
|
||||
fn build_wrapper_body(main_function_index: u32) -> Vec<Instruction>;
|
||||
}
|
||||
|
||||
macro_rules! build_wrapper_body_primitive {
|
||||
($store_instruction: expr, $align: expr) => {
|
||||
fn build_wrapper_body(main_function_index: u32) -> Vec<Instruction> {
|
||||
let size: i32 = 8;
|
||||
let mut instructions = Vec::with_capacity(16);
|
||||
push_stack_frame(&mut instructions, size, LocalId(STACK_POINTER_LOCAL_ID));
|
||||
instructions.extend([
|
||||
// load result address to prepare for the store instruction later
|
||||
GetLocal(STACK_POINTER_LOCAL_ID),
|
||||
//
|
||||
// Call the main function with no arguments. Get primitive back.
|
||||
Call(main_function_index),
|
||||
//
|
||||
// Store the primitive at the allocated address
|
||||
$store_instruction($align, 0),
|
||||
//
|
||||
// Return the result pointer
|
||||
GetLocal(STACK_POINTER_LOCAL_ID),
|
||||
]);
|
||||
pop_stack_frame(&mut instructions, size, LocalId(STACK_POINTER_LOCAL_ID));
|
||||
instructions.push(End);
|
||||
instructions
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! wasm_test_result_primitive {
|
||||
($type_name: ident, $store_instruction: expr, $align: expr) => {
|
||||
impl Wasm32TestResult for $type_name {
|
||||
build_wrapper_body_primitive!($store_instruction, $align);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
fn build_wrapper_body_stack_memory(main_function_index: u32, size: usize) -> Vec<Instruction> {
|
||||
let mut instructions = Vec::with_capacity(16);
|
||||
push_stack_frame(
|
||||
&mut instructions,
|
||||
size as i32,
|
||||
LocalId(STACK_POINTER_LOCAL_ID),
|
||||
);
|
||||
instructions.extend([
|
||||
//
|
||||
// Call the main function with the allocated address to write the result.
|
||||
// No value is returned to the VM stack. This is the same as in compiled C.
|
||||
GetLocal(STACK_POINTER_LOCAL_ID),
|
||||
Call(main_function_index),
|
||||
//
|
||||
// Return the result address
|
||||
GetLocal(STACK_POINTER_LOCAL_ID),
|
||||
]);
|
||||
pop_stack_frame(
|
||||
&mut instructions,
|
||||
size as i32,
|
||||
LocalId(STACK_POINTER_LOCAL_ID),
|
||||
);
|
||||
instructions.push(End);
|
||||
instructions
|
||||
}
|
||||
|
||||
macro_rules! wasm_test_result_stack_memory {
|
||||
($type_name: ident) => {
|
||||
impl Wasm32TestResult for $type_name {
|
||||
fn build_wrapper_body(main_function_index: u32) -> Vec<Instruction> {
|
||||
build_wrapper_body_stack_memory(main_function_index, $type_name::ACTUAL_WIDTH)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
wasm_test_result_primitive!(bool, I32Store8, ALIGN_1);
|
||||
wasm_test_result_primitive!(RocOrder, I32Store8, ALIGN_1);
|
||||
|
||||
wasm_test_result_primitive!(u8, I32Store8, ALIGN_1);
|
||||
wasm_test_result_primitive!(i8, I32Store8, ALIGN_1);
|
||||
wasm_test_result_primitive!(u16, I32Store16, ALIGN_2);
|
||||
wasm_test_result_primitive!(i16, I32Store16, ALIGN_2);
|
||||
wasm_test_result_primitive!(u32, I32Store, ALIGN_4);
|
||||
wasm_test_result_primitive!(i32, I32Store, ALIGN_4);
|
||||
wasm_test_result_primitive!(u64, I64Store, ALIGN_8);
|
||||
wasm_test_result_primitive!(i64, I64Store, ALIGN_8);
|
||||
|
||||
wasm_test_result_primitive!(f32, F32Store, ALIGN_8);
|
||||
wasm_test_result_primitive!(f64, F64Store, ALIGN_8);
|
||||
|
||||
wasm_test_result_stack_memory!(u128);
|
||||
wasm_test_result_stack_memory!(i128);
|
||||
wasm_test_result_stack_memory!(RocDec);
|
||||
wasm_test_result_stack_memory!(RocStr);
|
||||
|
||||
impl<T: Wasm32TestResult> Wasm32TestResult for RocList<T> {
|
||||
fn build_wrapper_body(main_function_index: u32) -> Vec<Instruction> {
|
||||
build_wrapper_body_stack_memory(main_function_index, 12)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Wasm32TestResult> Wasm32TestResult for &'_ T {
|
||||
build_wrapper_body_primitive!(I32Store, ALIGN_4);
|
||||
}
|
||||
|
||||
impl<T, const N: usize> Wasm32TestResult for [T; N]
|
||||
where
|
||||
T: Wasm32TestResult + FromWasm32Memory,
|
||||
{
|
||||
fn build_wrapper_body(main_function_index: u32) -> Vec<Instruction> {
|
||||
build_wrapper_body_stack_memory(main_function_index, N * T::ACTUAL_WIDTH)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, U> Wasm32TestResult for (T, U)
|
||||
where
|
||||
T: Wasm32TestResult + FromWasm32Memory,
|
||||
U: Wasm32TestResult + FromWasm32Memory,
|
||||
{
|
||||
fn build_wrapper_body(main_function_index: u32) -> Vec<Instruction> {
|
||||
build_wrapper_body_stack_memory(main_function_index, T::ACTUAL_WIDTH + U::ACTUAL_WIDTH)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, U, V> Wasm32TestResult for (T, U, V)
|
||||
where
|
||||
T: Wasm32TestResult + FromWasm32Memory,
|
||||
U: Wasm32TestResult + FromWasm32Memory,
|
||||
V: Wasm32TestResult + FromWasm32Memory,
|
||||
{
|
||||
fn build_wrapper_body(main_function_index: u32) -> Vec<Instruction> {
|
||||
build_wrapper_body_stack_memory(
|
||||
main_function_index,
|
||||
T::ACTUAL_WIDTH + U::ACTUAL_WIDTH + V::ACTUAL_WIDTH,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, U, V, W> Wasm32TestResult for (T, U, V, W)
|
||||
where
|
||||
T: Wasm32TestResult + FromWasm32Memory,
|
||||
U: Wasm32TestResult + FromWasm32Memory,
|
||||
V: Wasm32TestResult + FromWasm32Memory,
|
||||
W: Wasm32TestResult + FromWasm32Memory,
|
||||
{
|
||||
fn build_wrapper_body(main_function_index: u32) -> Vec<Instruction> {
|
||||
build_wrapper_body_stack_memory(
|
||||
main_function_index,
|
||||
T::ACTUAL_WIDTH + U::ACTUAL_WIDTH + V::ACTUAL_WIDTH + W::ACTUAL_WIDTH,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, U, V, W, X> Wasm32TestResult for (T, U, V, W, X)
|
||||
where
|
||||
T: Wasm32TestResult + FromWasm32Memory,
|
||||
U: Wasm32TestResult + FromWasm32Memory,
|
||||
V: Wasm32TestResult + FromWasm32Memory,
|
||||
W: Wasm32TestResult + FromWasm32Memory,
|
||||
X: Wasm32TestResult + FromWasm32Memory,
|
||||
{
|
||||
fn build_wrapper_body(main_function_index: u32) -> Vec<Instruction> {
|
||||
build_wrapper_body_stack_memory(
|
||||
main_function_index,
|
||||
T::ACTUAL_WIDTH + U::ACTUAL_WIDTH + V::ACTUAL_WIDTH + W::ACTUAL_WIDTH + X::ACTUAL_WIDTH,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, U, V, W, X, Y> Wasm32TestResult for (T, U, V, W, X, Y)
|
||||
where
|
||||
T: Wasm32TestResult + FromWasm32Memory,
|
||||
U: Wasm32TestResult + FromWasm32Memory,
|
||||
V: Wasm32TestResult + FromWasm32Memory,
|
||||
W: Wasm32TestResult + FromWasm32Memory,
|
||||
X: Wasm32TestResult + FromWasm32Memory,
|
||||
Y: Wasm32TestResult + FromWasm32Memory,
|
||||
{
|
||||
fn build_wrapper_body(main_function_index: u32) -> Vec<Instruction> {
|
||||
build_wrapper_body_stack_memory(
|
||||
main_function_index,
|
||||
T::ACTUAL_WIDTH
|
||||
+ U::ACTUAL_WIDTH
|
||||
+ V::ACTUAL_WIDTH
|
||||
+ W::ACTUAL_WIDTH
|
||||
+ X::ACTUAL_WIDTH
|
||||
+ Y::ACTUAL_WIDTH,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, U, V, W, X, Y, Z> Wasm32TestResult for (T, U, V, W, X, Y, Z)
|
||||
where
|
||||
T: Wasm32TestResult + FromWasm32Memory,
|
||||
U: Wasm32TestResult + FromWasm32Memory,
|
||||
V: Wasm32TestResult + FromWasm32Memory,
|
||||
W: Wasm32TestResult + FromWasm32Memory,
|
||||
X: Wasm32TestResult + FromWasm32Memory,
|
||||
Y: Wasm32TestResult + FromWasm32Memory,
|
||||
Z: Wasm32TestResult + FromWasm32Memory,
|
||||
{
|
||||
fn build_wrapper_body(main_function_index: u32) -> Vec<Instruction> {
|
||||
build_wrapper_body_stack_memory(
|
||||
main_function_index,
|
||||
T::ACTUAL_WIDTH
|
||||
+ U::ACTUAL_WIDTH
|
||||
+ V::ACTUAL_WIDTH
|
||||
+ W::ACTUAL_WIDTH
|
||||
+ X::ACTUAL_WIDTH
|
||||
+ Y::ACTUAL_WIDTH
|
||||
+ Z::ACTUAL_WIDTH,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, U, V, W, X, Y, Z, A> Wasm32TestResult for (T, U, V, W, X, Y, Z, A)
|
||||
where
|
||||
T: Wasm32TestResult + FromWasm32Memory,
|
||||
U: Wasm32TestResult + FromWasm32Memory,
|
||||
V: Wasm32TestResult + FromWasm32Memory,
|
||||
W: Wasm32TestResult + FromWasm32Memory,
|
||||
X: Wasm32TestResult + FromWasm32Memory,
|
||||
Y: Wasm32TestResult + FromWasm32Memory,
|
||||
Z: Wasm32TestResult + FromWasm32Memory,
|
||||
A: Wasm32TestResult + FromWasm32Memory,
|
||||
{
|
||||
fn build_wrapper_body(main_function_index: u32) -> Vec<Instruction> {
|
||||
build_wrapper_body_stack_memory(
|
||||
main_function_index,
|
||||
T::ACTUAL_WIDTH
|
||||
+ U::ACTUAL_WIDTH
|
||||
+ V::ACTUAL_WIDTH
|
||||
+ W::ACTUAL_WIDTH
|
||||
+ X::ACTUAL_WIDTH
|
||||
+ Y::ACTUAL_WIDTH
|
||||
+ Z::ACTUAL_WIDTH
|
||||
+ A::ACTUAL_WIDTH,
|
||||
)
|
||||
}
|
||||
}
|
|
@ -11,7 +11,7 @@ extern crate libc;
|
|||
mod helpers;
|
||||
|
||||
#[cfg(all(test, any(target_os = "linux", target_os = "macos"), any(target_arch = "x86_64"/*, target_arch = "aarch64"*/)))]
|
||||
mod dev_num {
|
||||
mod wasm_num {
|
||||
#[test]
|
||||
fn i64_values() {
|
||||
assert_evals_to!("0", 0, i64);
|
||||
|
@ -36,6 +36,101 @@ mod dev_num {
|
|||
assert_evals_to!(&format!("{:0.1}", f64::MAX), f64::MAX, f64);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn i8_add_wrap() {
|
||||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
x : I8
|
||||
x = 0x7f + 0x7f
|
||||
|
||||
x
|
||||
"#
|
||||
),
|
||||
-2,
|
||||
i8
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn i16_add_wrap() {
|
||||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
x : I16
|
||||
x = 0x7fff + 0x7fff
|
||||
|
||||
x
|
||||
"#
|
||||
),
|
||||
-2,
|
||||
i16
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn i32_add_wrap() {
|
||||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
x : I32
|
||||
x = 0x7fffffff + 0x7fffffff
|
||||
|
||||
x
|
||||
"#
|
||||
),
|
||||
-2,
|
||||
i32
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn u8_add_wrap() {
|
||||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
x : U8
|
||||
x = 0xff + 0xff
|
||||
|
||||
x
|
||||
"#
|
||||
),
|
||||
0xfe,
|
||||
u8
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn u16_add_wrap() {
|
||||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
x : U16
|
||||
x = 0xffff + 0xffff
|
||||
|
||||
x
|
||||
"#
|
||||
),
|
||||
0xfffe,
|
||||
u16
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn u32_add_wrap() {
|
||||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
x : U32
|
||||
x = 0xffffffff + 0xffffffff
|
||||
|
||||
x
|
||||
"#
|
||||
),
|
||||
0xfffffffe,
|
||||
u32
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gen_add_i64() {
|
||||
assert_evals_to!(
|
||||
|
@ -154,44 +249,44 @@ mod dev_num {
|
|||
);
|
||||
}
|
||||
|
||||
// #[test]
|
||||
// fn gen_add_f64() {
|
||||
// assert_evals_to!(
|
||||
// indoc!(
|
||||
// r#"
|
||||
// 1.1 + 2.4 + 3
|
||||
// "#
|
||||
// ),
|
||||
// 6.5,
|
||||
// f64
|
||||
// );
|
||||
// }
|
||||
#[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_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 gen_mul_i64() {
|
||||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
2 * 4 * 6
|
||||
"#
|
||||
),
|
||||
48,
|
||||
i64
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn i64_force_stack() {
|
||||
|
@ -476,18 +571,18 @@ mod dev_num {
|
|||
// );
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn gen_sub_f64() {
|
||||
// assert_evals_to!(
|
||||
// indoc!(
|
||||
// r#"
|
||||
// 1.5 - 2.4 - 3
|
||||
// "#
|
||||
// ),
|
||||
// -3.9,
|
||||
// f64
|
||||
// );
|
||||
// }
|
||||
#[test]
|
||||
fn gen_sub_f64() {
|
||||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
1.5 - 2.4 - 3
|
||||
"#
|
||||
),
|
||||
-3.9,
|
||||
f64
|
||||
);
|
||||
}
|
||||
|
||||
// #[test]
|
||||
// fn gen_div_i64() {
|
||||
|
@ -685,31 +780,31 @@ mod dev_num {
|
|||
// 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() {
|
||||
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 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() {
|
||||
|
|
|
@ -5,7 +5,7 @@ extern crate indoc;
|
|||
mod helpers;
|
||||
|
||||
#[cfg(all(test, target_os = "linux", any(target_arch = "x86_64"/*, target_arch = "aarch64"*/)))]
|
||||
mod dev_records {
|
||||
mod wasm_records {
|
||||
// #[test]
|
||||
// fn basic_record() {
|
||||
// assert_evals_to!(
|
||||
|
@ -307,142 +307,116 @@ mod dev_records {
|
|||
// ()
|
||||
// );
|
||||
// }
|
||||
//
|
||||
// #[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_record1_literal() {
|
||||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
{ x: 3 }
|
||||
"#
|
||||
),
|
||||
3,
|
||||
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 i64_record2_literal() {
|
||||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
{ x: 3, y: 5 }
|
||||
"#
|
||||
),
|
||||
(3, 5),
|
||||
(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 i64_record3_literal() {
|
||||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
{ x: 3, y: 5, z: 17 }
|
||||
"#
|
||||
),
|
||||
(3, 5, 17),
|
||||
(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 f64_record2_literal() {
|
||||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
{ x: 3.1, y: 5.1 }
|
||||
"#
|
||||
),
|
||||
(3.1, 5.1),
|
||||
(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 }
|
||||
#[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)
|
||||
);
|
||||
}
|
||||
|
||||
// // record
|
||||
// // "#
|
||||
// // ),
|
||||
// // (true, false, false, true),
|
||||
// // (bool, bool, bool, bool)
|
||||
// // );
|
||||
// // }
|
||||
#[test]
|
||||
fn bool_record4_literal() {
|
||||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
record : { a : Bool, b : Bool, c : Bool, d : Bool }
|
||||
record = { a: True, b: False, c : False, d : True }
|
||||
|
||||
// #[test]
|
||||
// fn i64_record1_literal() {
|
||||
// assert_evals_to!(
|
||||
// indoc!(
|
||||
// r#"
|
||||
// { a: 3 }
|
||||
// "#
|
||||
// ),
|
||||
// 3,
|
||||
// i64
|
||||
// );
|
||||
// }
|
||||
record
|
||||
"#
|
||||
),
|
||||
[true, false, false, true],
|
||||
[bool; 4]
|
||||
);
|
||||
}
|
||||
|
||||
// // #[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 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; 9]
|
||||
);
|
||||
}
|
||||
|
||||
// // #[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
|
||||
|
||||
// #[test]
|
||||
// fn bool_literal() {
|
||||
// assert_evals_to!(
|
||||
// indoc!(
|
||||
// r#"
|
||||
// x : Bool
|
||||
// x = True
|
||||
|
||||
// x
|
||||
// "#
|
||||
// ),
|
||||
// true,
|
||||
// bool
|
||||
// );
|
||||
// }
|
||||
x
|
||||
"#
|
||||
),
|
||||
true,
|
||||
bool
|
||||
);
|
||||
}
|
||||
|
||||
// #[test]
|
||||
// fn optional_field_when_use_default() {
|
||||
|
@ -667,135 +641,135 @@ mod dev_records {
|
|||
// );
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn return_record_2() {
|
||||
// assert_evals_to!(
|
||||
// indoc!(
|
||||
// r#"
|
||||
// { x: 3, y: 5 }
|
||||
// "#
|
||||
// ),
|
||||
// [3, 5],
|
||||
// [i64; 2]
|
||||
// );
|
||||
// }
|
||||
#[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_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_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_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_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_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_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_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() {
|
||||
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_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() {
|
||||
|
@ -851,20 +825,20 @@ mod dev_records {
|
|||
// );
|
||||
// }
|
||||
|
||||
#[test]
|
||||
fn update_single_element_record() {
|
||||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
rec = { foo: 42}
|
||||
// #[test]
|
||||
// fn update_single_element_record() {
|
||||
// assert_evals_to!(
|
||||
// indoc!(
|
||||
// r#"
|
||||
// rec = { foo: 42}
|
||||
|
||||
{ rec & foo: rec.foo + 1 }
|
||||
"#
|
||||
),
|
||||
43,
|
||||
i64
|
||||
);
|
||||
}
|
||||
// { rec & foo: rec.foo + 1 }
|
||||
// "#
|
||||
// ),
|
||||
// 43,
|
||||
// i64
|
||||
// );
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn booleans_in_record() {
|
||||
|
@ -899,6 +873,24 @@ mod dev_records {
|
|||
// );
|
||||
// }
|
||||
|
||||
#[test]
|
||||
fn stack_memory_return_from_branch() {
|
||||
// stack memory pointer should end up in the right place after returning from a branch
|
||||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
stackMemoryJunk = { x: 999, y: 111 }
|
||||
if True then
|
||||
{ x: 123, y: 321 }
|
||||
else
|
||||
stackMemoryJunk
|
||||
"#
|
||||
),
|
||||
(123, 321),
|
||||
(i64, i64)
|
||||
);
|
||||
}
|
||||
|
||||
// #[test]
|
||||
// fn blue_and_present() {
|
||||
// assert_evals_to!(
|
||||
|
|
|
@ -37,13 +37,13 @@ use roc_types::subs::{Subs, VarStore, Variable};
|
|||
use roc_types::types::{Alias, Type};
|
||||
use std::collections::hash_map::Entry::{Occupied, Vacant};
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::fs;
|
||||
use std::io;
|
||||
use std::iter;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::str::from_utf8_unchecked;
|
||||
use std::sync::Arc;
|
||||
use std::time::{Duration, SystemTime};
|
||||
use std::{env, fs};
|
||||
|
||||
/// Default name for the binary generated for an app, if an invalid one was specified.
|
||||
const DEFAULT_APP_OUTPUT_PATH: &str = "app";
|
||||
|
@ -1351,7 +1351,12 @@ where
|
|||
// doing .max(1) on the entire expression guards against
|
||||
// num_cpus returning 0, while also avoiding wrapping
|
||||
// unsigned subtraction overflow.
|
||||
let num_workers = num_cpus::get().max(2) - 1;
|
||||
let default_num_workers = num_cpus::get().max(2) - 1;
|
||||
|
||||
let num_workers = match env::var("ROC_NUM_WORKERS") {
|
||||
Ok(env_str) => env_str.parse::<usize>().unwrap_or(default_num_workers),
|
||||
Err(_) => default_num_workers,
|
||||
};
|
||||
|
||||
let worker_arenas = arena.alloc(bumpalo::collections::Vec::with_capacity_in(
|
||||
num_workers,
|
||||
|
@ -2113,8 +2118,6 @@ fn update<'a>(
|
|||
&mut state.procedures,
|
||||
);
|
||||
|
||||
Proc::insert_refcount_operations(arena, &mut state.procedures);
|
||||
|
||||
// display the mono IR of the module, for debug purposes
|
||||
if roc_mono::ir::PRETTY_PRINT_IR_SYMBOLS {
|
||||
let procs_string = state
|
||||
|
@ -2128,6 +2131,8 @@ fn update<'a>(
|
|||
println!("{}", result);
|
||||
}
|
||||
|
||||
Proc::insert_refcount_operations(arena, &mut state.procedures);
|
||||
|
||||
// This is not safe with the new non-recursive RC updates that we do for tag unions
|
||||
//
|
||||
// Proc::optimize_refcount_operations(
|
||||
|
|
|
@ -15,6 +15,7 @@ pub enum LowLevel {
|
|||
StrFromUtf8,
|
||||
StrFromUtf8Range,
|
||||
StrToUtf8,
|
||||
StrRepeat,
|
||||
StrFromFloat,
|
||||
ListLen,
|
||||
ListGetUnsafe,
|
||||
|
@ -114,19 +115,19 @@ impl LowLevel {
|
|||
match self {
|
||||
StrConcat | StrJoinWith | StrIsEmpty | StrStartsWith | StrStartsWithCodePt
|
||||
| StrEndsWith | StrSplit | StrCountGraphemes | StrFromInt | StrFromUtf8
|
||||
| StrFromUtf8Range | StrToUtf8 | StrFromFloat | ListLen | ListGetUnsafe | ListSet
|
||||
| ListDrop | ListSingle | ListRepeat | ListReverse | ListConcat | ListContains
|
||||
| ListAppend | ListPrepend | ListJoin | ListRange | ListSwap | DictSize | DictEmpty
|
||||
| DictInsert | DictRemove | DictContains | DictGetUnsafe | DictKeys | DictValues
|
||||
| DictUnion | DictIntersection | DictDifference | SetFromList | NumAdd | NumAddWrap
|
||||
| NumAddChecked | NumSub | NumSubWrap | NumSubChecked | NumMul | NumMulWrap
|
||||
| NumMulChecked | NumGt | NumGte | NumLt | NumLte | NumCompare | NumDivUnchecked
|
||||
| NumRemUnchecked | NumIsMultipleOf | NumAbs | NumNeg | NumSin | NumCos
|
||||
| NumSqrtUnchecked | NumLogUnchecked | NumRound | NumToFloat | NumPow | NumCeiling
|
||||
| NumPowInt | NumFloor | NumIsFinite | NumAtan | NumAcos | NumAsin | NumBitwiseAnd
|
||||
| NumBitwiseXor | NumBitwiseOr | NumShiftLeftBy | NumShiftRightBy | NumBytesToU16
|
||||
| NumBytesToU32 | NumShiftRightZfBy | NumIntCast | Eq | NotEq | And | Or | Not
|
||||
| Hash | ExpectTrue => false,
|
||||
| StrFromUtf8Range | StrToUtf8 | StrRepeat | StrFromFloat | ListLen | ListGetUnsafe
|
||||
| ListSet | ListDrop | ListSingle | ListRepeat | ListReverse | ListConcat
|
||||
| ListContains | ListAppend | ListPrepend | ListJoin | ListRange | ListSwap
|
||||
| DictSize | DictEmpty | DictInsert | DictRemove | DictContains | DictGetUnsafe
|
||||
| DictKeys | DictValues | DictUnion | DictIntersection | DictDifference
|
||||
| SetFromList | NumAdd | NumAddWrap | NumAddChecked | NumSub | NumSubWrap
|
||||
| NumSubChecked | NumMul | NumMulWrap | NumMulChecked | NumGt | NumGte | NumLt
|
||||
| NumLte | NumCompare | NumDivUnchecked | NumRemUnchecked | NumIsMultipleOf
|
||||
| NumAbs | NumNeg | NumSin | NumCos | NumSqrtUnchecked | NumLogUnchecked | NumRound
|
||||
| NumToFloat | NumPow | NumCeiling | NumPowInt | NumFloor | NumIsFinite | NumAtan
|
||||
| NumAcos | NumAsin | NumBitwiseAnd | NumBitwiseXor | NumBitwiseOr | NumShiftLeftBy
|
||||
| NumShiftRightBy | NumBytesToU16 | NumBytesToU32 | NumShiftRightZfBy | NumIntCast
|
||||
| Eq | NotEq | And | Or | Not | Hash | ExpectTrue => false,
|
||||
|
||||
ListMap | ListMap2 | ListMap3 | ListMapWithIndex | ListKeepIf | ListWalk
|
||||
| ListWalkUntil | ListWalkBackwards | ListKeepOks | ListKeepErrs | ListSortWith
|
||||
|
|
|
@ -927,6 +927,7 @@ define_builtins! {
|
|||
16 STR_STARTS_WITH_CODE_PT: "startsWithCodePt"
|
||||
17 STR_ALIAS_ANALYSIS_STATIC: "#aliasAnalysisStatic" // string with the static lifetime
|
||||
18 STR_FROM_UTF8_RANGE: "fromUtf8Range"
|
||||
19 STR_REPEAT: "repeat"
|
||||
}
|
||||
4 LIST: "List" => {
|
||||
0 LIST_LIST: "List" imported // the List.List type alias
|
||||
|
|
|
@ -16,6 +16,7 @@ use crate::layout::{Builtin, Layout, ListLayout, UnionLayout};
|
|||
pub const MOD_APP: ModName = ModName(b"UserApp");
|
||||
|
||||
pub const STATIC_STR_NAME: ConstName = ConstName(&Symbol::STR_ALIAS_ANALYSIS_STATIC.to_ne_bytes());
|
||||
pub const STATIC_LIST_NAME: ConstName = ConstName(b"THIS IS A STATIC LIST");
|
||||
|
||||
const ENTRY_POINT_NAME: &[u8] = b"mainForHost";
|
||||
|
||||
|
@ -128,6 +129,22 @@ where
|
|||
};
|
||||
m.add_const(STATIC_STR_NAME, static_str_def)?;
|
||||
|
||||
// a const that models all static lists
|
||||
let static_list_def = {
|
||||
let mut cbuilder = ConstDefBuilder::new();
|
||||
let block = cbuilder.add_block();
|
||||
let cell = cbuilder.add_new_heap_cell(block)?;
|
||||
|
||||
let unit_type = cbuilder.add_tuple_type(&[])?;
|
||||
let bag = cbuilder.add_empty_bag(block, unit_type)?;
|
||||
let value_id = cbuilder.add_make_tuple(block, &[cell, bag])?;
|
||||
let root = BlockExpr(block, value_id);
|
||||
let list_type_id = static_list_type(&mut cbuilder)?;
|
||||
|
||||
cbuilder.build(list_type_id, root)?
|
||||
};
|
||||
m.add_const(STATIC_LIST_NAME, static_list_def)?;
|
||||
|
||||
// the entry point wrapper
|
||||
let roc_main_bytes = func_name_bytes_help(
|
||||
entry_point.symbol,
|
||||
|
@ -589,9 +606,9 @@ fn call_spec(
|
|||
let index = builder.add_make_tuple(block, &[])?;
|
||||
|
||||
let argument = if closure_env_layout.is_none() {
|
||||
builder.add_make_tuple(block, &[first, index])?
|
||||
builder.add_make_tuple(block, &[index, first])?
|
||||
} else {
|
||||
builder.add_make_tuple(block, &[first, index, closure_env])?
|
||||
builder.add_make_tuple(block, &[index, first, closure_env])?
|
||||
};
|
||||
builder.add_call(block, spec_var, module, name, argument)?;
|
||||
}
|
||||
|
@ -1117,9 +1134,11 @@ fn expr_spec<'a>(
|
|||
let list = new_list(builder, block, type_id)?;
|
||||
|
||||
let mut bag = builder.add_get_tuple_field(block, list, LIST_BAG_INDEX)?;
|
||||
let mut all_constants = true;
|
||||
|
||||
for element in elems.iter() {
|
||||
let value_id = if let ListLiteralElement::Symbol(symbol) = element {
|
||||
all_constants = false;
|
||||
env.symbols[symbol]
|
||||
} else {
|
||||
builder.add_make_tuple(block, &[]).unwrap()
|
||||
|
@ -1128,9 +1147,13 @@ fn expr_spec<'a>(
|
|||
bag = builder.add_bag_insert(block, bag, value_id)?;
|
||||
}
|
||||
|
||||
let cell = builder.add_new_heap_cell(block)?;
|
||||
if all_constants {
|
||||
new_static_list(builder, block)
|
||||
} else {
|
||||
let cell = builder.add_new_heap_cell(block)?;
|
||||
|
||||
builder.add_make_tuple(block, &[cell, bag])
|
||||
builder.add_make_tuple(block, &[cell, bag])
|
||||
}
|
||||
}
|
||||
|
||||
EmptyArray => {
|
||||
|
@ -1191,6 +1214,11 @@ fn layout_spec_help(
|
|||
match layout {
|
||||
Builtin(builtin) => builtin_spec(builder, builtin, when_recursive),
|
||||
Struct(fields) => build_recursive_tuple_type(builder, fields, when_recursive),
|
||||
LambdaSet(lambda_set) => layout_spec_help(
|
||||
builder,
|
||||
&lambda_set.runtime_representation(),
|
||||
when_recursive,
|
||||
),
|
||||
Union(union_layout) => {
|
||||
let variant_types = build_variant_types(builder, union_layout)?;
|
||||
|
||||
|
@ -1236,7 +1264,7 @@ fn builtin_spec(
|
|||
|
||||
match builtin {
|
||||
Int128 | Int64 | Int32 | Int16 | Int8 | Int1 | Usize => builder.add_tuple_type(&[]),
|
||||
Decimal | Float128 | Float64 | Float32 | Float16 => builder.add_tuple_type(&[]),
|
||||
Decimal | Float128 | Float64 | Float32 => builder.add_tuple_type(&[]),
|
||||
Str | EmptyStr => str_type(builder),
|
||||
Dict(key_layout, value_layout) => {
|
||||
let value_type = layout_spec_help(builder, value_layout, when_recursive)?;
|
||||
|
@ -1291,6 +1319,14 @@ fn str_type<TC: TypeContext>(builder: &mut TC) -> Result<TypeId> {
|
|||
builder.add_tuple_type(&[cell_id])
|
||||
}
|
||||
|
||||
fn static_list_type<TC: TypeContext>(builder: &mut TC) -> Result<TypeId> {
|
||||
let unit_type = builder.add_tuple_type(&[])?;
|
||||
let cell = builder.add_heap_cell_type();
|
||||
let bag = builder.add_bag_type(unit_type)?;
|
||||
|
||||
builder.add_tuple_type(&[cell, bag])
|
||||
}
|
||||
|
||||
// const OK_TAG_ID: u8 = 1u8;
|
||||
// const ERR_TAG_ID: u8 = 0u8;
|
||||
|
||||
|
@ -1324,6 +1360,12 @@ fn new_static_string(builder: &mut FuncDefBuilder, block: BlockId) -> Result<Val
|
|||
builder.add_const_ref(block, module, STATIC_STR_NAME)
|
||||
}
|
||||
|
||||
fn new_static_list(builder: &mut FuncDefBuilder, block: BlockId) -> Result<ValueId> {
|
||||
let module = MOD_APP;
|
||||
|
||||
builder.add_const_ref(block, module, STATIC_LIST_NAME)
|
||||
}
|
||||
|
||||
fn new_num(builder: &mut FuncDefBuilder, block: BlockId) -> Result<ValueId> {
|
||||
// we model all our numbers as unit values
|
||||
builder.add_make_tuple(block, &[])
|
||||
|
|
|
@ -1013,6 +1013,7 @@ pub fn lowlevel_borrow_signature(arena: &Bump, op: LowLevel) -> &[bool] {
|
|||
StrFromUtf8 => arena.alloc_slice_copy(&[owned]),
|
||||
StrFromUtf8Range => arena.alloc_slice_copy(&[borrowed, irrelevant]),
|
||||
StrToUtf8 => arena.alloc_slice_copy(&[owned]),
|
||||
StrRepeat => arena.alloc_slice_copy(&[borrowed, irrelevant]),
|
||||
StrFromInt | StrFromFloat => arena.alloc_slice_copy(&[irrelevant]),
|
||||
Hash => arena.alloc_slice_copy(&[borrowed, irrelevant]),
|
||||
DictSize => arena.alloc_slice_copy(&[borrowed]),
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
use crate::exhaustive::{Ctor, RenderAs, TagId, Union};
|
||||
use crate::ir::{
|
||||
BranchInfo, DestructType, Env, Expr, JoinPointId, Literal, Param, Pattern, Procs, Stmt,
|
||||
BranchInfo, DestructType, Env, Expr, FloatPrecision, IntPrecision, JoinPointId, Literal, Param,
|
||||
Pattern, Procs, Stmt,
|
||||
};
|
||||
use crate::layout::{Builtin, Layout, LayoutCache, UnionLayout};
|
||||
use roc_collections::all::{MutMap, MutSet};
|
||||
|
@ -85,8 +86,8 @@ enum Test<'a> {
|
|||
union: crate::exhaustive::Union,
|
||||
arguments: Vec<(Pattern<'a>, Layout<'a>)>,
|
||||
},
|
||||
IsInt(i128),
|
||||
IsFloat(u64),
|
||||
IsInt(i128, IntPrecision),
|
||||
IsFloat(u64, FloatPrecision),
|
||||
IsDecimal(RocDec),
|
||||
IsStr(Box<str>),
|
||||
IsBit(bool),
|
||||
|
@ -95,6 +96,7 @@ enum Test<'a> {
|
|||
num_alts: usize,
|
||||
},
|
||||
}
|
||||
|
||||
use std::hash::{Hash, Hasher};
|
||||
impl<'a> Hash for Test<'a> {
|
||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||
|
@ -106,13 +108,15 @@ impl<'a> Hash for Test<'a> {
|
|||
tag_id.hash(state);
|
||||
// The point of this custom implementation is to not hash the tag arguments
|
||||
}
|
||||
IsInt(v) => {
|
||||
IsInt(v, width) => {
|
||||
state.write_u8(1);
|
||||
v.hash(state);
|
||||
width.hash(state);
|
||||
}
|
||||
IsFloat(v) => {
|
||||
IsFloat(v, width) => {
|
||||
state.write_u8(2);
|
||||
v.hash(state);
|
||||
width.hash(state);
|
||||
}
|
||||
IsStr(v) => {
|
||||
state.write_u8(3);
|
||||
|
@ -306,8 +310,8 @@ fn tests_are_complete_help(last_test: &Test, number_of_tests: usize) -> bool {
|
|||
Test::IsCtor { union, .. } => number_of_tests == union.alternatives.len(),
|
||||
Test::IsByte { num_alts, .. } => number_of_tests == *num_alts,
|
||||
Test::IsBit(_) => number_of_tests == 2,
|
||||
Test::IsInt(_) => false,
|
||||
Test::IsFloat(_) => false,
|
||||
Test::IsInt(_, _) => false,
|
||||
Test::IsFloat(_, _) => false,
|
||||
Test::IsDecimal(_) => false,
|
||||
Test::IsStr(_) => false,
|
||||
}
|
||||
|
@ -561,8 +565,8 @@ fn test_at_path<'a>(
|
|||
tag_id: *tag_id,
|
||||
num_alts: union.alternatives.len(),
|
||||
},
|
||||
IntLiteral(v) => IsInt(*v),
|
||||
FloatLiteral(v) => IsFloat(*v),
|
||||
IntLiteral(v, precision) => IsInt(*v, *precision),
|
||||
FloatLiteral(v, precision) => IsFloat(*v, *precision),
|
||||
DecimalLiteral(v) => IsDecimal(*v),
|
||||
StrLiteral(v) => IsStr(v.clone()),
|
||||
};
|
||||
|
@ -807,8 +811,9 @@ fn to_relevant_branch_help<'a>(
|
|||
_ => None,
|
||||
},
|
||||
|
||||
IntLiteral(int) => match test {
|
||||
IsInt(is_int) if int == *is_int => {
|
||||
IntLiteral(int, p1) => match test {
|
||||
IsInt(is_int, p2) if int == *is_int => {
|
||||
debug_assert_eq!(p1, *p2);
|
||||
start.extend(end);
|
||||
Some(Branch {
|
||||
goal: branch.goal,
|
||||
|
@ -819,8 +824,9 @@ fn to_relevant_branch_help<'a>(
|
|||
_ => None,
|
||||
},
|
||||
|
||||
FloatLiteral(float) => match test {
|
||||
IsFloat(test_float) if float == *test_float => {
|
||||
FloatLiteral(float, p1) => match test {
|
||||
IsFloat(test_float, p2) if float == *test_float => {
|
||||
debug_assert_eq!(p1, *p2);
|
||||
start.extend(end);
|
||||
Some(Branch {
|
||||
goal: branch.goal,
|
||||
|
@ -928,8 +934,8 @@ fn needs_tests(pattern: &Pattern) -> bool {
|
|||
| AppliedTag { .. }
|
||||
| BitLiteral { .. }
|
||||
| EnumLiteral { .. }
|
||||
| IntLiteral(_)
|
||||
| FloatLiteral(_)
|
||||
| IntLiteral(_, _)
|
||||
| FloatLiteral(_, _)
|
||||
| DecimalLiteral(_)
|
||||
| StrLiteral(_) => true,
|
||||
}
|
||||
|
@ -1280,22 +1286,22 @@ fn test_to_equality<'a>(
|
|||
_ => unreachable!("{:?}", (cond_layout, union)),
|
||||
}
|
||||
}
|
||||
Test::IsInt(test_int) => {
|
||||
Test::IsInt(test_int, precision) => {
|
||||
// TODO don't downcast i128 here
|
||||
debug_assert!(test_int <= i64::MAX as i128);
|
||||
let lhs = Expr::Literal(Literal::Int(test_int as i128));
|
||||
let lhs_symbol = env.unique_symbol();
|
||||
stores.push((lhs_symbol, Layout::Builtin(Builtin::Int64), lhs));
|
||||
stores.push((lhs_symbol, precision.as_layout(), lhs));
|
||||
|
||||
(stores, lhs_symbol, rhs_symbol, None)
|
||||
}
|
||||
|
||||
Test::IsFloat(test_int) => {
|
||||
Test::IsFloat(test_int, precision) => {
|
||||
// TODO maybe we can actually use i64 comparison here?
|
||||
let test_float = f64::from_bits(test_int as u64);
|
||||
let lhs = Expr::Literal(Literal::Float(test_float));
|
||||
let lhs_symbol = env.unique_symbol();
|
||||
stores.push((lhs_symbol, Layout::Builtin(Builtin::Float64), lhs));
|
||||
stores.push((lhs_symbol, precision.as_layout(), lhs));
|
||||
|
||||
(stores, lhs_symbol, rhs_symbol, None)
|
||||
}
|
||||
|
@ -1303,7 +1309,7 @@ fn test_to_equality<'a>(
|
|||
Test::IsDecimal(test_dec) => {
|
||||
let lhs = Expr::Literal(Literal::Int(test_dec.0));
|
||||
let lhs_symbol = env.unique_symbol();
|
||||
stores.push((lhs_symbol, Layout::Builtin(Builtin::Int128), lhs));
|
||||
stores.push((lhs_symbol, *cond_layout, lhs));
|
||||
|
||||
(stores, lhs_symbol, rhs_symbol, None)
|
||||
}
|
||||
|
@ -1737,8 +1743,8 @@ fn decide_to_branching<'a>(
|
|||
);
|
||||
|
||||
let tag = match test {
|
||||
Test::IsInt(v) => v as u64,
|
||||
Test::IsFloat(v) => v as u64,
|
||||
Test::IsInt(v, _) => v as u64,
|
||||
Test::IsFloat(v, _) => v as u64,
|
||||
Test::IsBit(v) => v as u64,
|
||||
Test::IsByte { tag_id, .. } => tag_id as u64,
|
||||
Test::IsCtor { tag_id, .. } => tag_id as u64,
|
||||
|
|
|
@ -65,8 +65,8 @@ fn simplify(pattern: &crate::ir::Pattern) -> Pattern {
|
|||
use crate::ir::Pattern::*;
|
||||
|
||||
match pattern {
|
||||
IntLiteral(v) => Literal(Literal::Int(*v)),
|
||||
FloatLiteral(v) => Literal(Literal::Float(*v)),
|
||||
IntLiteral(v, _) => Literal(Literal::Int(*v)),
|
||||
FloatLiteral(v, _) => Literal(Literal::Float(*v)),
|
||||
DecimalLiteral(v) => Literal(Literal::Decimal(*v)),
|
||||
StrLiteral(v) => Literal(Literal::Str(v.clone())),
|
||||
|
||||
|
|
|
@ -54,6 +54,7 @@ macro_rules! return_on_layout_error_help {
|
|||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum OptLevel {
|
||||
Development,
|
||||
Normal,
|
||||
Optimize,
|
||||
}
|
||||
|
@ -694,7 +695,20 @@ impl<'a> Procs<'a> {
|
|||
// the `layout` is a function pointer, while `_ignore_layout` can be a
|
||||
// closure. We only specialize functions, storing this value with a closure
|
||||
// layout will give trouble.
|
||||
self.specialized.insert((symbol, layout), Done(proc));
|
||||
let arguments =
|
||||
Vec::from_iter_in(proc.args.iter().map(|(l, _)| *l), env.arena)
|
||||
.into_bump_slice();
|
||||
|
||||
let proper_layout = ProcLayout {
|
||||
arguments,
|
||||
result: proc.ret_layout,
|
||||
};
|
||||
|
||||
// NOTE: some function are specialized to have a closure, but don't actually
|
||||
// need any closure argument. Here is where we correct this sort of thing,
|
||||
// by trusting the layout of the Proc, not of what we specialize for
|
||||
self.specialized.remove(&(symbol, layout));
|
||||
self.specialized.insert((symbol, proper_layout), Done(proc));
|
||||
}
|
||||
Err(error) => {
|
||||
panic!("TODO generate a RuntimeError message for {:?}", error);
|
||||
|
@ -1843,7 +1857,7 @@ fn generate_runtime_error_function<'a>(
|
|||
args.push((*arg, env.unique_symbol()));
|
||||
}
|
||||
|
||||
args.push((lambda_set.runtime_representation(), Symbol::ARG_CLOSURE));
|
||||
args.push((Layout::LambdaSet(lambda_set), Symbol::ARG_CLOSURE));
|
||||
|
||||
(args.into_bump_slice(), *ret_layout)
|
||||
}
|
||||
|
@ -1919,7 +1933,29 @@ fn specialize_external<'a>(
|
|||
match layout {
|
||||
RawFunctionLayout::Function(argument_layouts, lambda_set, return_layout) => {
|
||||
let assigned = env.unique_symbol();
|
||||
let unit = env.unique_symbol();
|
||||
|
||||
let mut argument_symbols =
|
||||
Vec::with_capacity_in(argument_layouts.len(), env.arena);
|
||||
let mut proc_arguments =
|
||||
Vec::with_capacity_in(argument_layouts.len() + 1, env.arena);
|
||||
let mut top_level_arguments =
|
||||
Vec::with_capacity_in(argument_layouts.len() + 1, env.arena);
|
||||
|
||||
for layout in argument_layouts {
|
||||
let symbol = env.unique_symbol();
|
||||
|
||||
proc_arguments.push((*layout, symbol));
|
||||
|
||||
argument_symbols.push(symbol);
|
||||
top_level_arguments.push(*layout);
|
||||
}
|
||||
|
||||
// the proc needs to take an extra closure argument
|
||||
let lambda_set_layout = Layout::LambdaSet(lambda_set);
|
||||
proc_arguments.push((lambda_set_layout, Symbol::ARG_CLOSURE));
|
||||
|
||||
// this should also be reflected in the TopLevel signature
|
||||
top_level_arguments.push(lambda_set_layout);
|
||||
|
||||
let hole = env.arena.alloc(Stmt::Ret(assigned));
|
||||
|
||||
|
@ -1927,20 +1963,16 @@ fn specialize_external<'a>(
|
|||
env,
|
||||
lambda_set,
|
||||
Symbol::ARG_CLOSURE,
|
||||
env.arena.alloc([unit]),
|
||||
argument_symbols.into_bump_slice(),
|
||||
argument_layouts,
|
||||
*return_layout,
|
||||
assigned,
|
||||
hole,
|
||||
);
|
||||
|
||||
let body = let_empty_struct(unit, env.arena.alloc(body));
|
||||
|
||||
let proc = Proc {
|
||||
name,
|
||||
args: env
|
||||
.arena
|
||||
.alloc([(lambda_set.runtime_representation(), Symbol::ARG_CLOSURE)]),
|
||||
args: proc_arguments.into_bump_slice(),
|
||||
body,
|
||||
closure_data_layout: None,
|
||||
ret_layout: *return_layout,
|
||||
|
@ -1951,7 +1983,7 @@ fn specialize_external<'a>(
|
|||
|
||||
let top_level = ProcLayout::new(
|
||||
env.arena,
|
||||
env.arena.alloc([lambda_set.runtime_representation()]),
|
||||
top_level_arguments.into_bump_slice(),
|
||||
*return_layout,
|
||||
);
|
||||
|
||||
|
@ -1999,7 +2031,7 @@ fn specialize_external<'a>(
|
|||
env.subs.rollback_to(snapshot);
|
||||
|
||||
let closure_data_layout = match opt_closure_layout {
|
||||
Some(closure_layout) => closure_layout.runtime_representation(),
|
||||
Some(lambda_set) => Layout::LambdaSet(lambda_set),
|
||||
None => Layout::Struct(&[]),
|
||||
};
|
||||
|
||||
|
@ -2149,7 +2181,7 @@ fn specialize_external<'a>(
|
|||
env.subs.rollback_to(snapshot);
|
||||
|
||||
let closure_data_layout = match opt_closure_layout {
|
||||
Some(closure_layout) => Some(closure_layout.runtime_representation()),
|
||||
Some(lambda_set) => Some(Layout::LambdaSet(lambda_set)),
|
||||
None => None,
|
||||
};
|
||||
|
||||
|
@ -2260,7 +2292,7 @@ fn build_specialized_proc<'a>(
|
|||
Some(lambda_set) if pattern_symbols.last() == Some(&Symbol::ARG_CLOSURE) => {
|
||||
// here we define the lifted (now top-level) f function. Its final argument is `Symbol::ARG_CLOSURE`,
|
||||
// it stores the closure structure (just an integer in this case)
|
||||
proc_args.push((lambda_set.runtime_representation(), Symbol::ARG_CLOSURE));
|
||||
proc_args.push((Layout::LambdaSet(lambda_set), Symbol::ARG_CLOSURE));
|
||||
|
||||
debug_assert_eq!(
|
||||
pattern_layouts_len + 1,
|
||||
|
@ -2297,7 +2329,7 @@ fn build_specialized_proc<'a>(
|
|||
}
|
||||
Ordering::Greater => {
|
||||
if pattern_symbols.is_empty() {
|
||||
let ret_layout = lambda_set.runtime_representation();
|
||||
let ret_layout = Layout::LambdaSet(lambda_set);
|
||||
Ok(FunctionPointerBody {
|
||||
closure: None,
|
||||
ret_layout,
|
||||
|
@ -2482,7 +2514,7 @@ where
|
|||
let raw = if procs.module_thunks.contains(&proc_name) {
|
||||
match raw {
|
||||
RawFunctionLayout::Function(_, lambda_set, _) => {
|
||||
RawFunctionLayout::ZeroArgumentThunk(lambda_set.runtime_representation())
|
||||
RawFunctionLayout::ZeroArgumentThunk(Layout::LambdaSet(lambda_set))
|
||||
}
|
||||
_ => raw,
|
||||
}
|
||||
|
@ -2656,6 +2688,7 @@ macro_rules! match_on_closure_argument {
|
|||
let ret_layout = top_level.result;
|
||||
|
||||
|
||||
|
||||
match closure_data_layout {
|
||||
RawFunctionLayout::Function(_, lambda_set, _) => {
|
||||
lowlevel_match_on_lambda_set(
|
||||
|
@ -2760,13 +2793,13 @@ pub fn with_hole<'a>(
|
|||
IntOrFloat::SignedIntType(precision) => Stmt::Let(
|
||||
assigned,
|
||||
Expr::Literal(Literal::Int(int)),
|
||||
Layout::Builtin(int_precision_to_builtin(precision)),
|
||||
precision.as_layout(),
|
||||
hole,
|
||||
),
|
||||
IntOrFloat::UnsignedIntType(precision) => Stmt::Let(
|
||||
assigned,
|
||||
Expr::Literal(Literal::Int(int)),
|
||||
Layout::Builtin(int_precision_to_builtin(precision)),
|
||||
precision.as_layout(),
|
||||
hole,
|
||||
),
|
||||
_ => unreachable!("unexpected float precision for integer"),
|
||||
|
@ -2778,7 +2811,7 @@ pub fn with_hole<'a>(
|
|||
IntOrFloat::BinaryFloatType(precision) => Stmt::Let(
|
||||
assigned,
|
||||
Expr::Literal(Literal::Float(float)),
|
||||
Layout::Builtin(float_precision_to_builtin(precision)),
|
||||
precision.as_layout(),
|
||||
hole,
|
||||
),
|
||||
IntOrFloat::DecimalFloatType => {
|
||||
|
@ -2810,19 +2843,19 @@ pub fn with_hole<'a>(
|
|||
IntOrFloat::SignedIntType(precision) => Stmt::Let(
|
||||
assigned,
|
||||
Expr::Literal(Literal::Int(num.into())),
|
||||
Layout::Builtin(int_precision_to_builtin(precision)),
|
||||
precision.as_layout(),
|
||||
hole,
|
||||
),
|
||||
IntOrFloat::UnsignedIntType(precision) => Stmt::Let(
|
||||
assigned,
|
||||
Expr::Literal(Literal::Int(num.into())),
|
||||
Layout::Builtin(int_precision_to_builtin(precision)),
|
||||
precision.as_layout(),
|
||||
hole,
|
||||
),
|
||||
IntOrFloat::BinaryFloatType(precision) => Stmt::Let(
|
||||
assigned,
|
||||
Expr::Literal(Literal::Float(num as f64)),
|
||||
Layout::Builtin(float_precision_to_builtin(precision)),
|
||||
precision.as_layout(),
|
||||
hole,
|
||||
),
|
||||
IntOrFloat::DecimalFloatType => {
|
||||
|
@ -4131,6 +4164,8 @@ fn construct_closure_data<'a>(
|
|||
assigned: Symbol,
|
||||
hole: &'a Stmt<'a>,
|
||||
) -> Stmt<'a> {
|
||||
let lambda_set_layout = Layout::LambdaSet(lambda_set);
|
||||
|
||||
match lambda_set.layout_for_member(name) {
|
||||
ClosureRepresentation::Union {
|
||||
tag_id,
|
||||
|
@ -4162,12 +4197,7 @@ fn construct_closure_data<'a>(
|
|||
arguments: symbols,
|
||||
};
|
||||
|
||||
Stmt::Let(
|
||||
assigned,
|
||||
expr,
|
||||
lambda_set.runtime_representation(),
|
||||
env.arena.alloc(hole),
|
||||
)
|
||||
Stmt::Let(assigned, expr, lambda_set_layout, env.arena.alloc(hole))
|
||||
}
|
||||
ClosureRepresentation::AlphabeticOrderStruct(field_layouts) => {
|
||||
debug_assert_eq!(field_layouts.len(), symbols.len());
|
||||
|
@ -4198,7 +4228,7 @@ fn construct_closure_data<'a>(
|
|||
|
||||
let expr = Expr::Struct(symbols);
|
||||
|
||||
Stmt::Let(assigned, expr, lambda_set.runtime_representation(), hole)
|
||||
Stmt::Let(assigned, expr, lambda_set_layout, hole)
|
||||
}
|
||||
ClosureRepresentation::Other(Layout::Builtin(Builtin::Int1)) => {
|
||||
debug_assert_eq!(symbols.len(), 0);
|
||||
|
@ -4207,7 +4237,7 @@ fn construct_closure_data<'a>(
|
|||
let tag_id = name != lambda_set.set[0].0;
|
||||
let expr = Expr::Literal(Literal::Bool(tag_id));
|
||||
|
||||
Stmt::Let(assigned, expr, lambda_set.runtime_representation(), hole)
|
||||
Stmt::Let(assigned, expr, lambda_set_layout, hole)
|
||||
}
|
||||
ClosureRepresentation::Other(Layout::Builtin(Builtin::Int8)) => {
|
||||
debug_assert_eq!(symbols.len(), 0);
|
||||
|
@ -4216,7 +4246,7 @@ fn construct_closure_data<'a>(
|
|||
let tag_id = lambda_set.set.iter().position(|(s, _)| *s == name).unwrap() as u8;
|
||||
let expr = Expr::Literal(Literal::Byte(tag_id));
|
||||
|
||||
Stmt::Let(assigned, expr, lambda_set.runtime_representation(), hole)
|
||||
Stmt::Let(assigned, expr, lambda_set_layout, hole)
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
|
@ -5623,8 +5653,8 @@ fn store_pattern_help<'a>(
|
|||
// do nothing
|
||||
return StorePattern::NotProductive(stmt);
|
||||
}
|
||||
IntLiteral(_)
|
||||
| FloatLiteral(_)
|
||||
IntLiteral(_, _)
|
||||
| FloatLiteral(_, _)
|
||||
| DecimalLiteral(_)
|
||||
| EnumLiteral { .. }
|
||||
| BitLiteral { .. }
|
||||
|
@ -5758,8 +5788,8 @@ fn store_tag_pattern<'a>(
|
|||
Underscore => {
|
||||
// ignore
|
||||
}
|
||||
IntLiteral(_)
|
||||
| FloatLiteral(_)
|
||||
IntLiteral(_, _)
|
||||
| FloatLiteral(_, _)
|
||||
| DecimalLiteral(_)
|
||||
| EnumLiteral { .. }
|
||||
| BitLiteral { .. }
|
||||
|
@ -5834,8 +5864,8 @@ fn store_newtype_pattern<'a>(
|
|||
Underscore => {
|
||||
// ignore
|
||||
}
|
||||
IntLiteral(_)
|
||||
| FloatLiteral(_)
|
||||
IntLiteral(_, _)
|
||||
| FloatLiteral(_, _)
|
||||
| DecimalLiteral(_)
|
||||
| EnumLiteral { .. }
|
||||
| BitLiteral { .. }
|
||||
|
@ -5910,8 +5940,8 @@ fn store_record_destruct<'a>(
|
|||
// internally. But `y` is never used, so we must make sure it't not stored/loaded.
|
||||
return StorePattern::NotProductive(stmt);
|
||||
}
|
||||
IntLiteral(_)
|
||||
| FloatLiteral(_)
|
||||
IntLiteral(_, _)
|
||||
| FloatLiteral(_, _)
|
||||
| DecimalLiteral(_)
|
||||
| EnumLiteral { .. }
|
||||
| BitLiteral { .. }
|
||||
|
@ -6060,7 +6090,7 @@ fn reuse_function_symbol<'a>(
|
|||
let layout = match raw {
|
||||
RawFunctionLayout::ZeroArgumentThunk(layout) => layout,
|
||||
RawFunctionLayout::Function(_, lambda_set, _) => {
|
||||
lambda_set.runtime_representation()
|
||||
Layout::LambdaSet(lambda_set)
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -6158,7 +6188,7 @@ fn reuse_function_symbol<'a>(
|
|||
// TODO suspicious
|
||||
// let layout = Layout::Closure(argument_layouts, lambda_set, ret_layout);
|
||||
// panic!("suspicious");
|
||||
let layout = lambda_set.runtime_representation();
|
||||
let layout = Layout::LambdaSet(lambda_set);
|
||||
let top_level = ProcLayout::new(env.arena, &[], layout);
|
||||
procs.insert_passed_by_name(
|
||||
env,
|
||||
|
@ -6178,9 +6208,14 @@ fn reuse_function_symbol<'a>(
|
|||
layout_cache,
|
||||
);
|
||||
|
||||
// a function name (non-closure) that is passed along
|
||||
// it never has closure data, so we use the empty struct
|
||||
return let_empty_struct(symbol, env.arena.alloc(result));
|
||||
construct_closure_data(
|
||||
env,
|
||||
lambda_set,
|
||||
original,
|
||||
&[],
|
||||
symbol,
|
||||
env.arena.alloc(result),
|
||||
)
|
||||
}
|
||||
}
|
||||
RawFunctionLayout::ZeroArgumentThunk(ret_layout) => {
|
||||
|
@ -6348,7 +6383,7 @@ fn call_by_name<'a>(
|
|||
procs,
|
||||
fn_var,
|
||||
proc_name,
|
||||
env.arena.alloc(lambda_set.runtime_representation()),
|
||||
env.arena.alloc(Layout::LambdaSet(lambda_set)),
|
||||
layout_cache,
|
||||
assigned,
|
||||
hole,
|
||||
|
@ -6392,7 +6427,7 @@ fn call_by_name<'a>(
|
|||
procs,
|
||||
fn_var,
|
||||
proc_name,
|
||||
env.arena.alloc(lambda_set.runtime_representation()),
|
||||
env.arena.alloc(Layout::LambdaSet(lambda_set)),
|
||||
layout_cache,
|
||||
closure_data_symbol,
|
||||
env.arena.alloc(result),
|
||||
|
@ -6522,7 +6557,7 @@ fn call_by_name_help<'a>(
|
|||
force_thunk(
|
||||
env,
|
||||
proc_name,
|
||||
lambda_set.runtime_representation(),
|
||||
Layout::LambdaSet(lambda_set),
|
||||
assigned,
|
||||
hole,
|
||||
)
|
||||
|
@ -6836,13 +6871,7 @@ fn call_specialized_proc<'a>(
|
|||
arguments: field_symbols,
|
||||
};
|
||||
|
||||
build_call(
|
||||
env,
|
||||
call,
|
||||
assigned,
|
||||
lambda_set.runtime_representation(),
|
||||
hole,
|
||||
)
|
||||
build_call(env, call, assigned, Layout::LambdaSet(lambda_set), hole)
|
||||
}
|
||||
RawFunctionLayout::ZeroArgumentThunk(_) => {
|
||||
unreachable!()
|
||||
|
@ -6882,8 +6911,8 @@ fn call_specialized_proc<'a>(
|
|||
pub enum Pattern<'a> {
|
||||
Identifier(Symbol),
|
||||
Underscore,
|
||||
IntLiteral(i128),
|
||||
FloatLiteral(u64),
|
||||
IntLiteral(i128, IntPrecision),
|
||||
FloatLiteral(u64, FloatPrecision),
|
||||
DecimalLiteral(RocDec),
|
||||
BitLiteral {
|
||||
value: bool,
|
||||
|
@ -6961,22 +6990,36 @@ fn from_can_pattern_help<'a>(
|
|||
match can_pattern {
|
||||
Underscore => Ok(Pattern::Underscore),
|
||||
Identifier(symbol) => Ok(Pattern::Identifier(*symbol)),
|
||||
IntLiteral(_, _, int) => Ok(Pattern::IntLiteral(*int as i128)),
|
||||
IntLiteral(var, _, int) => {
|
||||
match num_argument_to_int_or_float(env.subs, env.ptr_bytes, *var, false) {
|
||||
IntOrFloat::SignedIntType(precision) | IntOrFloat::UnsignedIntType(precision) => {
|
||||
Ok(Pattern::IntLiteral(*int as i128, precision))
|
||||
}
|
||||
other => {
|
||||
panic!(
|
||||
"Invalid precision for int pattern: {:?} has {:?}",
|
||||
can_pattern, other
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
FloatLiteral(var, float_str, float) => {
|
||||
// TODO: Can I reuse num_argument_to_int_or_float here if I pass in true?
|
||||
match num_argument_to_int_or_float(env.subs, env.ptr_bytes, *var, true) {
|
||||
IntOrFloat::SignedIntType(_) => {
|
||||
panic!("Invalid percision for float literal = {:?}", var)
|
||||
IntOrFloat::SignedIntType(_) | IntOrFloat::UnsignedIntType(_) => {
|
||||
panic!("Invalid precision for float pattern {:?}", var)
|
||||
}
|
||||
IntOrFloat::UnsignedIntType(_) => {
|
||||
panic!("Invalid percision for float literal = {:?}", var)
|
||||
IntOrFloat::BinaryFloatType(precision) => {
|
||||
Ok(Pattern::FloatLiteral(f64::to_bits(*float), precision))
|
||||
}
|
||||
IntOrFloat::BinaryFloatType(_) => Ok(Pattern::FloatLiteral(f64::to_bits(*float))),
|
||||
IntOrFloat::DecimalFloatType => {
|
||||
let dec = match RocDec::from_str(float_str) {
|
||||
Some(d) => d,
|
||||
None => panic!("Invalid decimal for float literal = {}. TODO: Make this a nice, user-friendly error message", float_str),
|
||||
};
|
||||
Some(d) => d,
|
||||
None => panic!(
|
||||
r"Invalid decimal for float literal = {}. TODO: Make this a nice, user-friendly error message",
|
||||
float_str
|
||||
),
|
||||
};
|
||||
Ok(Pattern::DecimalLiteral(dec))
|
||||
}
|
||||
}
|
||||
|
@ -6993,9 +7036,15 @@ fn from_can_pattern_help<'a>(
|
|||
}
|
||||
NumLiteral(var, num_str, num) => {
|
||||
match num_argument_to_int_or_float(env.subs, env.ptr_bytes, *var, false) {
|
||||
IntOrFloat::SignedIntType(_) => Ok(Pattern::IntLiteral(*num as i128)),
|
||||
IntOrFloat::UnsignedIntType(_) => Ok(Pattern::IntLiteral(*num as i128)),
|
||||
IntOrFloat::BinaryFloatType(_) => Ok(Pattern::FloatLiteral(*num as u64)),
|
||||
IntOrFloat::SignedIntType(precision) => {
|
||||
Ok(Pattern::IntLiteral(*num as i128, precision))
|
||||
}
|
||||
IntOrFloat::UnsignedIntType(precision) => {
|
||||
Ok(Pattern::IntLiteral(*num as i128, precision))
|
||||
}
|
||||
IntOrFloat::BinaryFloatType(precision) => {
|
||||
Ok(Pattern::FloatLiteral(*num as u64, precision))
|
||||
}
|
||||
IntOrFloat::DecimalFloatType => {
|
||||
let dec = match RocDec::from_str(num_str) {
|
||||
Some(d) => d,
|
||||
|
@ -7577,7 +7626,7 @@ fn from_can_record_destruct<'a>(
|
|||
})
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Hash)]
|
||||
pub enum IntPrecision {
|
||||
Usize,
|
||||
I128,
|
||||
|
@ -7587,11 +7636,45 @@ pub enum IntPrecision {
|
|||
I8,
|
||||
}
|
||||
|
||||
impl IntPrecision {
|
||||
pub fn as_layout(&self) -> Layout<'static> {
|
||||
Layout::Builtin(self.as_builtin())
|
||||
}
|
||||
|
||||
pub fn as_builtin(&self) -> Builtin<'static> {
|
||||
use IntPrecision::*;
|
||||
match self {
|
||||
I128 => Builtin::Int128,
|
||||
I64 => Builtin::Int64,
|
||||
I32 => Builtin::Int32,
|
||||
I16 => Builtin::Int16,
|
||||
I8 => Builtin::Int8,
|
||||
Usize => Builtin::Usize,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Hash)]
|
||||
pub enum FloatPrecision {
|
||||
F64,
|
||||
F32,
|
||||
}
|
||||
|
||||
impl FloatPrecision {
|
||||
pub fn as_layout(&self) -> Layout<'static> {
|
||||
Layout::Builtin(self.as_builtin())
|
||||
}
|
||||
|
||||
pub fn as_builtin(&self) -> Builtin<'static> {
|
||||
use FloatPrecision::*;
|
||||
match self {
|
||||
F64 => Builtin::Float64,
|
||||
F32 => Builtin::Float32,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum IntOrFloat {
|
||||
SignedIntType(IntPrecision),
|
||||
UnsignedIntType(IntPrecision),
|
||||
|
@ -7599,26 +7682,6 @@ pub enum IntOrFloat {
|
|||
DecimalFloatType,
|
||||
}
|
||||
|
||||
fn float_precision_to_builtin(precision: FloatPrecision) -> Builtin<'static> {
|
||||
use FloatPrecision::*;
|
||||
match precision {
|
||||
F64 => Builtin::Float64,
|
||||
F32 => Builtin::Float32,
|
||||
}
|
||||
}
|
||||
|
||||
fn int_precision_to_builtin(precision: IntPrecision) -> Builtin<'static> {
|
||||
use IntPrecision::*;
|
||||
match precision {
|
||||
I128 => Builtin::Int128,
|
||||
I64 => Builtin::Int64,
|
||||
I32 => Builtin::Int32,
|
||||
I16 => Builtin::Int16,
|
||||
I8 => Builtin::Int8,
|
||||
Usize => Builtin::Usize,
|
||||
}
|
||||
}
|
||||
|
||||
/// Given the `a` in `Num a`, determines whether it's an int or a float
|
||||
pub fn num_argument_to_int_or_float(
|
||||
subs: &Subs,
|
||||
|
@ -7915,8 +7978,8 @@ fn match_on_lambda_set<'a>(
|
|||
|
||||
let result = union_lambda_set_to_switch(
|
||||
env,
|
||||
lambda_set.set,
|
||||
lambda_set.runtime_representation(),
|
||||
lambda_set,
|
||||
Layout::Union(union_layout),
|
||||
closure_tag_id_symbol,
|
||||
union_layout.tag_id_layout(),
|
||||
closure_data_symbol,
|
||||
|
@ -7946,6 +8009,7 @@ fn match_on_lambda_set<'a>(
|
|||
union_lambda_set_branch_help(
|
||||
env,
|
||||
function_symbol,
|
||||
lambda_set,
|
||||
closure_data_symbol,
|
||||
Layout::Struct(fields),
|
||||
argument_symbols,
|
||||
|
@ -7994,7 +8058,7 @@ fn match_on_lambda_set<'a>(
|
|||
#[allow(clippy::too_many_arguments)]
|
||||
fn union_lambda_set_to_switch<'a>(
|
||||
env: &mut Env<'a, '_>,
|
||||
lambda_set: &'a [(Symbol, &'a [Layout<'a>])],
|
||||
lambda_set: LambdaSet<'a>,
|
||||
closure_layout: Layout<'a>,
|
||||
closure_tag_id_symbol: Symbol,
|
||||
closure_tag_id_layout: Layout<'a>,
|
||||
|
@ -8005,7 +8069,7 @@ fn union_lambda_set_to_switch<'a>(
|
|||
assigned: Symbol,
|
||||
hole: &'a Stmt<'a>,
|
||||
) -> Stmt<'a> {
|
||||
if lambda_set.is_empty() {
|
||||
if lambda_set.set.is_empty() {
|
||||
// NOTE this can happen if there is a type error somewhere. Since the lambda set is empty,
|
||||
// there is really nothing we can do here. We generate a runtime error here which allows
|
||||
// code gen to proceed. We then assume that we hit another (more descriptive) error before
|
||||
|
@ -8017,11 +8081,12 @@ fn union_lambda_set_to_switch<'a>(
|
|||
|
||||
let join_point_id = JoinPointId(env.unique_symbol());
|
||||
|
||||
let mut branches = Vec::with_capacity_in(lambda_set.len(), env.arena);
|
||||
let mut branches = Vec::with_capacity_in(lambda_set.set.len(), env.arena);
|
||||
|
||||
for (i, (function_symbol, _)) in lambda_set.iter().enumerate() {
|
||||
for (i, (function_symbol, _)) in lambda_set.set.iter().enumerate() {
|
||||
let stmt = union_lambda_set_branch(
|
||||
env,
|
||||
lambda_set,
|
||||
join_point_id,
|
||||
*function_symbol,
|
||||
closure_data_symbol,
|
||||
|
@ -8064,6 +8129,7 @@ fn union_lambda_set_to_switch<'a>(
|
|||
#[allow(clippy::too_many_arguments)]
|
||||
fn union_lambda_set_branch<'a>(
|
||||
env: &mut Env<'a, '_>,
|
||||
lambda_set: LambdaSet<'a>,
|
||||
join_point_id: JoinPointId,
|
||||
function_symbol: Symbol,
|
||||
closure_data_symbol: Symbol,
|
||||
|
@ -8079,6 +8145,7 @@ fn union_lambda_set_branch<'a>(
|
|||
union_lambda_set_branch_help(
|
||||
env,
|
||||
function_symbol,
|
||||
lambda_set,
|
||||
closure_data_symbol,
|
||||
closure_data_layout,
|
||||
argument_symbols_slice,
|
||||
|
@ -8093,6 +8160,7 @@ fn union_lambda_set_branch<'a>(
|
|||
fn union_lambda_set_branch_help<'a>(
|
||||
env: &mut Env<'a, '_>,
|
||||
function_symbol: Symbol,
|
||||
lambda_set: LambdaSet<'a>,
|
||||
closure_data_symbol: Symbol,
|
||||
closure_data_layout: Layout<'a>,
|
||||
argument_symbols_slice: &'a [Symbol],
|
||||
|
@ -8105,12 +8173,19 @@ fn union_lambda_set_branch_help<'a>(
|
|||
Layout::Struct(&[]) | Layout::Builtin(Builtin::Int1) | Layout::Builtin(Builtin::Int8) => {
|
||||
(argument_layouts_slice, argument_symbols_slice)
|
||||
}
|
||||
_ if lambda_set.member_does_not_need_closure_argument(function_symbol) => {
|
||||
// sometimes unification causes a function that does not itself capture anything
|
||||
// to still get a lambda set that does store information. We must not pass a closure
|
||||
// argument in this case
|
||||
|
||||
(argument_layouts_slice, argument_symbols_slice)
|
||||
}
|
||||
_ => {
|
||||
// extend layouts with the layout of the closure environment
|
||||
let mut argument_layouts =
|
||||
Vec::with_capacity_in(argument_layouts_slice.len() + 1, env.arena);
|
||||
argument_layouts.extend(argument_layouts_slice);
|
||||
argument_layouts.push(closure_data_layout);
|
||||
argument_layouts.push(Layout::LambdaSet(lambda_set));
|
||||
|
||||
// extend symbols with the symbol of the closure environment
|
||||
let mut argument_symbols =
|
||||
|
|
|
@ -189,6 +189,7 @@ pub enum Layout<'a> {
|
|||
/// this is important for closures that capture zero-sized values
|
||||
Struct(&'a [Layout<'a>]),
|
||||
Union(UnionLayout<'a>),
|
||||
LambdaSet(LambdaSet<'a>),
|
||||
RecursivePointer,
|
||||
}
|
||||
|
||||
|
@ -454,6 +455,17 @@ impl<'a> LambdaSet<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn member_does_not_need_closure_argument(&self, function_symbol: Symbol) -> bool {
|
||||
match self.layout_for_member(function_symbol) {
|
||||
ClosureRepresentation::Union {
|
||||
alphabetic_order_fields,
|
||||
..
|
||||
} => alphabetic_order_fields.is_empty(),
|
||||
ClosureRepresentation::AlphabeticOrderStruct(fields) => fields.is_empty(),
|
||||
ClosureRepresentation::Other(_) => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn layout_for_member(&self, function_symbol: Symbol) -> ClosureRepresentation<'a> {
|
||||
debug_assert!(
|
||||
self.set.iter().any(|(s, _)| *s == function_symbol),
|
||||
|
@ -531,7 +543,7 @@ impl<'a> LambdaSet<'a> {
|
|||
_ => {
|
||||
let mut arguments = Vec::with_capacity_in(argument_layouts.len() + 1, arena);
|
||||
arguments.extend(argument_layouts);
|
||||
arguments.push(self.runtime_representation());
|
||||
arguments.push(Layout::LambdaSet(*self));
|
||||
|
||||
arguments.into_bump_slice()
|
||||
}
|
||||
|
@ -587,7 +599,7 @@ impl<'a> LambdaSet<'a> {
|
|||
// this can happen when there is a type error somewhere
|
||||
Ok(LambdaSet {
|
||||
set: &[],
|
||||
representation: arena.alloc(Layout::Union(UnionLayout::NonRecursive(&[]))),
|
||||
representation: arena.alloc(Layout::Struct(&[])),
|
||||
})
|
||||
}
|
||||
_ => panic!("called LambdaSet.from_var on invalid input"),
|
||||
|
@ -606,7 +618,9 @@ impl<'a> LambdaSet<'a> {
|
|||
use UnionVariant::*;
|
||||
match variant {
|
||||
Never => Layout::Union(UnionLayout::NonRecursive(&[])),
|
||||
Unit | UnitWithArguments | BoolUnion { .. } | ByteUnion(_) => {
|
||||
BoolUnion { .. } => Layout::Builtin(Builtin::Int1),
|
||||
ByteUnion { .. } => Layout::Builtin(Builtin::Int8),
|
||||
Unit | UnitWithArguments => {
|
||||
// no useful information to store
|
||||
Layout::Struct(&[])
|
||||
}
|
||||
|
@ -667,7 +681,6 @@ pub enum Builtin<'a> {
|
|||
Float128,
|
||||
Float64,
|
||||
Float32,
|
||||
Float16,
|
||||
Str,
|
||||
Dict(&'a Layout<'a>, &'a Layout<'a>),
|
||||
Set(&'a Layout<'a>),
|
||||
|
@ -826,6 +839,7 @@ impl<'a> Layout<'a> {
|
|||
}
|
||||
}
|
||||
}
|
||||
LambdaSet(lambda_set) => lambda_set.runtime_representation().safe_to_memcpy(),
|
||||
RecursivePointer => {
|
||||
// We cannot memcpy pointers, because then we would have the same pointer in multiple places!
|
||||
false
|
||||
|
@ -890,6 +904,9 @@ impl<'a> Layout<'a> {
|
|||
| NonNullableUnwrapped(_) => pointer_size,
|
||||
}
|
||||
}
|
||||
LambdaSet(lambda_set) => lambda_set
|
||||
.runtime_representation()
|
||||
.stack_size_without_alignment(pointer_size),
|
||||
RecursivePointer => pointer_size,
|
||||
}
|
||||
}
|
||||
|
@ -919,6 +936,9 @@ impl<'a> Layout<'a> {
|
|||
| NonNullableUnwrapped(_) => pointer_size,
|
||||
}
|
||||
}
|
||||
Layout::LambdaSet(lambda_set) => lambda_set
|
||||
.runtime_representation()
|
||||
.alignment_bytes(pointer_size),
|
||||
Layout::Builtin(builtin) => builtin.alignment_bytes(pointer_size),
|
||||
Layout::RecursivePointer => pointer_size,
|
||||
}
|
||||
|
@ -929,6 +949,9 @@ impl<'a> Layout<'a> {
|
|||
Layout::Builtin(builtin) => builtin.allocation_alignment_bytes(pointer_size),
|
||||
Layout::Struct(_) => unreachable!("not heap-allocated"),
|
||||
Layout::Union(union_layout) => union_layout.allocation_alignment_bytes(pointer_size),
|
||||
Layout::LambdaSet(lambda_set) => lambda_set
|
||||
.runtime_representation()
|
||||
.allocation_alignment_bytes(pointer_size),
|
||||
Layout::RecursivePointer => unreachable!("should be looked up to get an actual layout"),
|
||||
}
|
||||
}
|
||||
|
@ -979,6 +1002,7 @@ impl<'a> Layout<'a> {
|
|||
| NonNullableUnwrapped(_) => true,
|
||||
}
|
||||
}
|
||||
LambdaSet(lambda_set) => lambda_set.runtime_representation().contains_refcounted(),
|
||||
RecursivePointer => true,
|
||||
}
|
||||
}
|
||||
|
@ -1002,6 +1026,7 @@ impl<'a> Layout<'a> {
|
|||
.append(alloc.text("}"))
|
||||
}
|
||||
Union(union_layout) => union_layout.to_doc(alloc, parens),
|
||||
LambdaSet(lambda_set) => lambda_set.runtime_representation().to_doc(alloc, parens),
|
||||
RecursivePointer => alloc.text("*self"),
|
||||
}
|
||||
}
|
||||
|
@ -1093,7 +1118,6 @@ impl<'a> Builtin<'a> {
|
|||
const F128_SIZE: u32 = 16;
|
||||
const F64_SIZE: u32 = std::mem::size_of::<f64>() as u32;
|
||||
const F32_SIZE: u32 = std::mem::size_of::<f32>() as u32;
|
||||
const F16_SIZE: u32 = 2;
|
||||
|
||||
/// Number of machine words in an empty one of these
|
||||
pub const STR_WORDS: u32 = 2;
|
||||
|
@ -1123,7 +1147,6 @@ impl<'a> Builtin<'a> {
|
|||
Float128 => Builtin::F128_SIZE,
|
||||
Float64 => Builtin::F64_SIZE,
|
||||
Float32 => Builtin::F32_SIZE,
|
||||
Float16 => Builtin::F16_SIZE,
|
||||
Str | EmptyStr => Builtin::STR_WORDS * pointer_size,
|
||||
Dict(_, _) | EmptyDict => Builtin::DICT_WORDS * pointer_size,
|
||||
Set(_) | EmptySet => Builtin::SET_WORDS * pointer_size,
|
||||
|
@ -1150,7 +1173,6 @@ impl<'a> Builtin<'a> {
|
|||
Float128 => align_of::<i128>() as u32,
|
||||
Float64 => align_of::<f64>() as u32,
|
||||
Float32 => align_of::<f32>() as u32,
|
||||
Float16 => align_of::<i16>() as u32,
|
||||
Dict(_, _) | EmptyDict => pointer_size,
|
||||
Set(_) | EmptySet => pointer_size,
|
||||
// we often treat these as i128 (64-bit systems)
|
||||
|
@ -1158,8 +1180,8 @@ impl<'a> Builtin<'a> {
|
|||
//
|
||||
// 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),
|
||||
List(_) | EmptyList => pointer_size,
|
||||
Str | EmptyStr => pointer_size,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1168,7 +1190,7 @@ impl<'a> Builtin<'a> {
|
|||
|
||||
match self {
|
||||
Int128 | Int64 | Int32 | Int16 | Int8 | Int1 | Usize | Decimal | Float128 | Float64
|
||||
| Float32 | Float16 | EmptyStr | EmptyDict | EmptyList | EmptySet => true,
|
||||
| Float32 | EmptyStr | EmptyDict | EmptyList | EmptySet => true,
|
||||
Str | Dict(_, _) | Set(_) | List(_) => false,
|
||||
}
|
||||
}
|
||||
|
@ -1179,7 +1201,7 @@ impl<'a> Builtin<'a> {
|
|||
|
||||
match self {
|
||||
Int128 | Int64 | Int32 | Int16 | Int8 | Int1 | Usize | Decimal | Float128 | Float64
|
||||
| Float32 | Float16 | EmptyStr | EmptyDict | EmptyList | EmptySet => false,
|
||||
| Float32 | EmptyStr | EmptyDict | EmptyList | EmptySet => false,
|
||||
List(_) => true,
|
||||
|
||||
Str | Dict(_, _) | Set(_) => true,
|
||||
|
@ -1206,7 +1228,6 @@ impl<'a> Builtin<'a> {
|
|||
Float128 => alloc.text("Float128"),
|
||||
Float64 => alloc.text("Float64"),
|
||||
Float32 => alloc.text("Float32"),
|
||||
Float16 => alloc.text("Float16"),
|
||||
|
||||
EmptyStr => alloc.text("EmptyStr"),
|
||||
EmptyList => alloc.text("EmptyList"),
|
||||
|
@ -1240,8 +1261,7 @@ impl<'a> Builtin<'a> {
|
|||
| Builtin::Decimal
|
||||
| Builtin::Float128
|
||||
| Builtin::Float64
|
||||
| Builtin::Float32
|
||||
| Builtin::Float16 => unreachable!("not heap-allocated"),
|
||||
| Builtin::Float32 => unreachable!("not heap-allocated"),
|
||||
Builtin::Str => pointer_size,
|
||||
Builtin::Dict(k, v) => k
|
||||
.alignment_bytes(pointer_size)
|
||||
|
@ -1360,7 +1380,7 @@ fn layout_from_flat_type<'a>(
|
|||
Func(_, closure_var, _) => {
|
||||
let lambda_set = LambdaSet::from_var(env.arena, env.subs, closure_var, env.ptr_bytes)?;
|
||||
|
||||
Ok(lambda_set.runtime_representation())
|
||||
Ok(Layout::LambdaSet(lambda_set))
|
||||
}
|
||||
Record(fields, ext_var) => {
|
||||
// extract any values from the ext_var
|
||||
|
|
|
@ -1192,7 +1192,7 @@ fn not_found<'b>(
|
|||
alloc.reflow(" missing up-top"),
|
||||
]);
|
||||
|
||||
let default_yes = alloc.reflow("these names seem close though:");
|
||||
let default_yes = alloc.reflow("Did you mean one of these?");
|
||||
|
||||
let to_details = |no_suggestion_details, yes_suggestion_details| {
|
||||
if suggestions.is_empty() {
|
||||
|
@ -1240,7 +1240,7 @@ fn module_not_found<'b>(
|
|||
]);
|
||||
|
||||
let default_yes = alloc
|
||||
.reflow("Is there an import missing? Perhaps there is a typo, these names seem close:");
|
||||
.reflow("Is there an import missing? Perhaps there is a typo. Did you mean one of these?");
|
||||
|
||||
let to_details = |no_suggestion_details, yes_suggestion_details| {
|
||||
if suggestions.is_empty() {
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
use roc_can::expected::{Expected, PExpected};
|
||||
use roc_collections::all::{Index, MutSet, SendMap};
|
||||
use roc_module::ident::{IdentStr, Lowercase, TagName};
|
||||
use roc_module::ident::{Ident, IdentStr, Lowercase, TagName};
|
||||
use roc_module::symbol::Symbol;
|
||||
use roc_region::all::{Located, Region};
|
||||
use roc_solve::solve;
|
||||
use roc_types::pretty_print::Parens;
|
||||
use roc_types::types::{Category, ErrorType, PatternCategory, Reason, RecordField, TypeExt};
|
||||
|
@ -10,34 +11,39 @@ use std::path::PathBuf;
|
|||
use crate::report::{Annotation, Report, RocDocAllocator, RocDocBuilder, Severity};
|
||||
use ven_pretty::DocAllocator;
|
||||
|
||||
const DUPLICATE_NAME: &str = "DUPLICATE NAME";
|
||||
const ADD_ANNOTATIONS: &str = r#"Can more type annotations be added? Type annotations always help me give more specific messages, and I think they could help a lot in this case"#;
|
||||
|
||||
pub fn type_problem<'b>(
|
||||
alloc: &'b RocDocAllocator<'b>,
|
||||
filename: PathBuf,
|
||||
problem: solve::TypeError,
|
||||
) -> Report<'b> {
|
||||
) -> Option<Report<'b>> {
|
||||
use solve::TypeError::*;
|
||||
|
||||
fn report(title: String, doc: RocDocBuilder<'_>, filename: PathBuf) -> Report<'_> {
|
||||
Report {
|
||||
fn report(title: String, doc: RocDocBuilder<'_>, filename: PathBuf) -> Option<Report<'_>> {
|
||||
Some(Report {
|
||||
title,
|
||||
filename,
|
||||
doc,
|
||||
severity: Severity::RuntimeError,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
match problem {
|
||||
BadExpr(region, category, found, expected) => {
|
||||
to_expr_report(alloc, filename, region, category, found, expected)
|
||||
}
|
||||
BadPattern(region, category, found, expected) => {
|
||||
to_pattern_report(alloc, filename, region, category, found, expected)
|
||||
}
|
||||
CircularType(region, symbol, overall_type) => {
|
||||
to_circular_report(alloc, filename, region, symbol, overall_type)
|
||||
}
|
||||
BadExpr(region, category, found, expected) => Some(to_expr_report(
|
||||
alloc, filename, region, category, found, expected,
|
||||
)),
|
||||
BadPattern(region, category, found, expected) => Some(to_pattern_report(
|
||||
alloc, filename, region, category, found, expected,
|
||||
)),
|
||||
CircularType(region, symbol, overall_type) => Some(to_circular_report(
|
||||
alloc,
|
||||
filename,
|
||||
region,
|
||||
symbol,
|
||||
overall_type,
|
||||
)),
|
||||
UnexposedLookup(symbol) => {
|
||||
let title = "UNRECOGNIZED NAME".to_string();
|
||||
let doc = alloc
|
||||
|
@ -97,12 +103,40 @@ pub fn type_problem<'b>(
|
|||
report(title, doc, filename)
|
||||
}
|
||||
|
||||
SolvedTypeError => None, // Don't re-report cascading errors - see https://github.com/rtfeldman/roc/pull/1711
|
||||
|
||||
Shadowed(original_region, shadow) => {
|
||||
let doc = report_shadowing(alloc, original_region, shadow);
|
||||
let title = DUPLICATE_NAME.to_string();
|
||||
|
||||
report(title, doc, filename)
|
||||
}
|
||||
|
||||
other => panic!("unhandled bad type: {:?}", other),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn report_shadowing<'b>(
|
||||
alloc: &'b RocDocAllocator<'b>,
|
||||
original_region: Region,
|
||||
shadow: Located<Ident>,
|
||||
) -> RocDocBuilder<'b> {
|
||||
let line = r#"Since these types have the same name, it's easy to use the wrong one on accident. Give one of them a new name."#;
|
||||
|
||||
alloc.stack(vec![
|
||||
alloc
|
||||
.text("The ")
|
||||
.append(alloc.ident(shadow.value))
|
||||
.append(alloc.reflow(" name is first defined here:")),
|
||||
alloc.region(original_region),
|
||||
alloc.reflow("But then it's defined a second time here:"),
|
||||
alloc.region(shadow.region),
|
||||
alloc.reflow(line),
|
||||
])
|
||||
}
|
||||
|
||||
pub fn cyclic_alias<'b>(
|
||||
alloc: &'b RocDocAllocator<'b>,
|
||||
symbol: Symbol,
|
||||
|
|
|
@ -128,6 +128,7 @@ impl<'b> Report<'b> {
|
|||
pub struct Palette<'a> {
|
||||
pub primary: &'a str,
|
||||
pub code_block: &'a str,
|
||||
pub keyword: &'a str,
|
||||
pub variable: &'a str,
|
||||
pub type_variable: &'a str,
|
||||
pub structure: &'a str,
|
||||
|
@ -146,6 +147,7 @@ pub struct Palette<'a> {
|
|||
pub const DEFAULT_PALETTE: Palette = Palette {
|
||||
primary: WHITE_CODE,
|
||||
code_block: WHITE_CODE,
|
||||
keyword: GREEN_CODE,
|
||||
variable: BLUE_CODE,
|
||||
type_variable: YELLOW_CODE,
|
||||
structure: GREEN_CODE,
|
||||
|
@ -810,6 +812,9 @@ where
|
|||
Symbol => {
|
||||
self.write_str(self.palette.variable)?;
|
||||
}
|
||||
Keyword => {
|
||||
self.write_str(self.palette.keyword)?;
|
||||
}
|
||||
GutterBar => {
|
||||
self.write_str(self.palette.gutter_bar)?;
|
||||
}
|
||||
|
@ -837,7 +842,7 @@ where
|
|||
ParserSuggestion => {
|
||||
self.write_str(self.palette.parser_suggestion)?;
|
||||
}
|
||||
TypeBlock | GlobalTag | PrivateTag | RecordField | Keyword => { /* nothing yet */ }
|
||||
TypeBlock | GlobalTag | PrivateTag | RecordField => { /* nothing yet */ }
|
||||
}
|
||||
self.style_stack.push(*annotation);
|
||||
Ok(())
|
||||
|
@ -851,11 +856,11 @@ where
|
|||
Some(annotation) => match annotation {
|
||||
Emphasized | Url | TypeVariable | Alias | Symbol | BinOp | Error | GutterBar
|
||||
| Typo | TypoSuggestion | ParserSuggestion | Structure | CodeBlock | PlainText
|
||||
| LineNumber | Tip | Module | Header => {
|
||||
| LineNumber | Tip | Module | Header | Keyword => {
|
||||
self.write_str(RESET_CODE)?;
|
||||
}
|
||||
|
||||
TypeBlock | GlobalTag | PrivateTag | RecordField | Keyword => { /* nothing yet */ }
|
||||
TypeBlock | GlobalTag | PrivateTag | RecordField => { /* nothing yet */ }
|
||||
},
|
||||
}
|
||||
Ok(())
|
||||
|
|
|
@ -154,8 +154,9 @@ mod test_reporting {
|
|||
}
|
||||
|
||||
for problem in type_problems {
|
||||
let report = type_problem(&alloc, filename.clone(), problem.clone());
|
||||
reports.push(report);
|
||||
if let Some(report) = type_problem(&alloc, filename.clone(), problem.clone()) {
|
||||
reports.push(report);
|
||||
}
|
||||
}
|
||||
|
||||
for problem in mono_problems {
|
||||
|
@ -541,7 +542,7 @@ mod test_reporting {
|
|||
8│ 4 -> bar baz "yay"
|
||||
^^^
|
||||
|
||||
these names seem close though:
|
||||
Did you mean one of these?
|
||||
|
||||
baz
|
||||
Nat
|
||||
|
@ -739,7 +740,7 @@ mod test_reporting {
|
|||
<cyan>3<reset><cyan>│<reset> <white>theAdmin<reset>
|
||||
<red>^^^^^^^^<reset>
|
||||
|
||||
these names seem close though:
|
||||
Did you mean one of these?
|
||||
|
||||
Decimal
|
||||
Dec
|
||||
|
@ -1491,7 +1492,7 @@ mod test_reporting {
|
|||
2│ { foo: 2 } -> foo
|
||||
^^^
|
||||
|
||||
these names seem close though:
|
||||
Did you mean one of these?
|
||||
|
||||
Bool
|
||||
U8
|
||||
|
@ -1947,7 +1948,7 @@ mod test_reporting {
|
|||
2│ f = \_ -> ok 4
|
||||
^^
|
||||
|
||||
these names seem close though:
|
||||
Did you mean one of these?
|
||||
|
||||
U8
|
||||
f
|
||||
|
@ -3634,8 +3635,8 @@ mod test_reporting {
|
|||
1│ Foo.test
|
||||
^^^^^^^^
|
||||
|
||||
Is there an import missing? Perhaps there is a typo, these names seem
|
||||
close:
|
||||
Is there an import missing? Perhaps there is a typo. Did you mean one
|
||||
of these?
|
||||
|
||||
Bool
|
||||
Num
|
||||
|
@ -5797,7 +5798,7 @@ mod test_reporting {
|
|||
1│ [ "foo", bar("") ]
|
||||
^^^
|
||||
|
||||
these names seem close though:
|
||||
Did you mean one of these?
|
||||
|
||||
Nat
|
||||
Str
|
||||
|
|
|
@ -2017,3 +2017,45 @@ fn lists_with_incompatible_type_param_in_if() {
|
|||
RocStr
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn map_with_index_multi_record() {
|
||||
// see https://github.com/rtfeldman/roc/issues/1700
|
||||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
List.mapWithIndex [ { x: {}, y: {} } ] \_, _ -> {}
|
||||
"#
|
||||
),
|
||||
RocList::from_slice(&[((), ())]),
|
||||
RocList<((), ())>
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_list_of_function_type() {
|
||||
// see https://github.com/rtfeldman/roc/issues/1732
|
||||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
myList : List (Str -> Str)
|
||||
myList = []
|
||||
|
||||
myClosure : Str -> Str
|
||||
myClosure = \_ -> "bar"
|
||||
|
||||
choose =
|
||||
if False then
|
||||
myList
|
||||
else
|
||||
[ myClosure ]
|
||||
|
||||
when List.get choose 0 is
|
||||
Ok f -> f "foo"
|
||||
Err _ -> "bad!"
|
||||
"#
|
||||
),
|
||||
RocStr::from_slice(b"bar"),
|
||||
RocStr
|
||||
);
|
||||
}
|
||||
|
|
|
@ -1778,4 +1778,48 @@ mod gen_num {
|
|||
u32
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn when_on_i32() {
|
||||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
app "test" provides [ main ] to "./platform"
|
||||
|
||||
x : I32
|
||||
x = 0
|
||||
|
||||
main : I32
|
||||
main =
|
||||
when x is
|
||||
0 -> 42
|
||||
_ -> -1
|
||||
"#
|
||||
),
|
||||
42,
|
||||
i32
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn when_on_i16() {
|
||||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
app "test" provides [ main ] to "./platform"
|
||||
|
||||
x : I16
|
||||
x = 0
|
||||
|
||||
main : I16
|
||||
main =
|
||||
when x is
|
||||
0 -> 42
|
||||
_ -> -1
|
||||
"#
|
||||
),
|
||||
42,
|
||||
i16
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2531,6 +2531,8 @@ fn pattern_match_unit_tag() {
|
|||
);
|
||||
}
|
||||
|
||||
// see for why this is disabled on wasm32 https://github.com/rtfeldman/roc/issues/1687
|
||||
#[cfg(not(feature = "wasm-cli-run"))]
|
||||
#[test]
|
||||
fn mirror_llvm_alignment_padding() {
|
||||
// see https://github.com/rtfeldman/roc/issues/1569
|
||||
|
@ -2616,7 +2618,8 @@ fn lambda_set_struct_byte() {
|
|||
r = Red
|
||||
|
||||
p1 = (\u -> r == u)
|
||||
oneOfResult = List.map [p1, p1] (\p -> p Green)
|
||||
foobarbaz = (\p -> p Green)
|
||||
oneOfResult = List.map [p1, p1] foobarbaz
|
||||
|
||||
when oneOfResult is
|
||||
_ -> 32
|
||||
|
@ -2779,3 +2782,127 @@ fn value_not_exposed_hits_panic() {
|
|||
i64
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mix_function_and_closure() {
|
||||
// see https://github.com/rtfeldman/roc/pull/1706
|
||||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
app "test" provides [ main ] to "./platform"
|
||||
|
||||
# foo does not capture any variables
|
||||
# but through unification will get a lambda set that does store information
|
||||
# we must handle that correctly
|
||||
foo = \x -> x
|
||||
|
||||
bar = \y -> \_ -> y
|
||||
|
||||
main : Str
|
||||
main =
|
||||
(if 1 == 1 then foo else (bar "nope nope nope")) "hello world"
|
||||
"#
|
||||
),
|
||||
RocStr::from_slice(b"hello world"),
|
||||
RocStr
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mix_function_and_closure_level_of_indirection() {
|
||||
// see https://github.com/rtfeldman/roc/pull/1706
|
||||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
app "test" provides [ main ] to "./platform"
|
||||
|
||||
foo = \x -> x
|
||||
|
||||
bar = \y -> \_ -> y
|
||||
|
||||
f = (if 1 == 1 then foo else (bar "nope nope nope"))
|
||||
|
||||
main : Str
|
||||
main =
|
||||
f "hello world"
|
||||
"#
|
||||
),
|
||||
RocStr::from_slice(b"hello world"),
|
||||
RocStr
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn do_pass_bool_byte_closure_layout() {
|
||||
// see https://github.com/rtfeldman/roc/pull/1706
|
||||
// the distinction is actually important, dropping that info means some functions just get
|
||||
// skipped
|
||||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
app "test" provides [ main ] to "./platform"
|
||||
|
||||
## PARSER
|
||||
|
||||
Parser a : List U8 -> List [Pair a (List U8)]
|
||||
|
||||
|
||||
## ANY
|
||||
|
||||
# If succcessful, the any parser consumes one character
|
||||
|
||||
any: Parser U8
|
||||
any = \inp ->
|
||||
when List.first inp is
|
||||
Ok u -> [Pair u (List.drop inp 1)]
|
||||
_ -> [ ]
|
||||
|
||||
|
||||
|
||||
## SATISFY
|
||||
|
||||
satisfy : (U8 -> Bool) -> Parser U8
|
||||
satisfy = \predicate ->
|
||||
\input ->
|
||||
walker = \(Pair u rest), accum ->
|
||||
if predicate u then
|
||||
Stop [ Pair u rest ]
|
||||
|
||||
else
|
||||
Stop accum
|
||||
|
||||
List.walkUntil (any input) walker []
|
||||
|
||||
|
||||
|
||||
oneOf : List (Parser a) -> Parser a
|
||||
oneOf = \parserList ->
|
||||
\input ->
|
||||
walker = \p, accum ->
|
||||
output = p input
|
||||
if List.len output == 1 then
|
||||
Stop output
|
||||
|
||||
else
|
||||
Continue accum
|
||||
|
||||
List.walkUntil parserList walker []
|
||||
|
||||
|
||||
satisfyA = satisfy (\u -> u == 97) # recognize 97
|
||||
satisfyB = satisfy (\u -> u == 98) # recognize 98
|
||||
|
||||
test1 = if List.len ((oneOf [satisfyA, satisfyB]) [97, 98, 99, 100] ) == 1 then "PASS" else "FAIL"
|
||||
test2 = if List.len ((oneOf [satisfyA, satisfyB]) [98, 99, 100, 97] ) == 1 then "PASS" else "FAIL"
|
||||
test3 = if List.len ((oneOf [satisfyB , satisfyA]) [98, 99, 100, 97] ) == 1 then "PASS" else "FAIL"
|
||||
test4 = if List.len ((oneOf [satisfyA, satisfyB]) [99, 100, 101] ) == 0 then "PASS" else "FAIL"
|
||||
|
||||
|
||||
main : Str
|
||||
main = [test1, test2, test3, test4] |> Str.joinWith ", "
|
||||
"#
|
||||
),
|
||||
RocStr::from_slice(b"PASS, PASS, PASS, PASS"),
|
||||
RocStr
|
||||
);
|
||||
}
|
||||
|
|
|
@ -949,3 +949,31 @@ fn str_from_utf8_range_count_too_high_for_start() {
|
|||
RocStr
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn str_repeat_small() {
|
||||
assert_evals_to!(
|
||||
indoc!(r#"Str.repeat "Roc" 3"#),
|
||||
RocStr::from("RocRocRoc"),
|
||||
RocStr
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn str_repeat_big() {
|
||||
assert_evals_to!(
|
||||
indoc!(r#"Str.repeat "more than 16 characters" 2"#),
|
||||
RocStr::from("more than 16 charactersmore than 16 characters"),
|
||||
RocStr
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn str_repeat_empty_string() {
|
||||
assert_evals_to!(indoc!(r#"Str.repeat "" 3"#), RocStr::from(""), RocStr);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn str_repeat_zero_times() {
|
||||
assert_evals_to!(indoc!(r#"Str.repeat "Roc" 0"#), RocStr::from(""), RocStr);
|
||||
}
|
||||
|
|
|
@ -143,12 +143,13 @@ fn create_llvm_module<'a>(
|
|||
}
|
||||
|
||||
for problem in type_problems {
|
||||
let report = type_problem(&alloc, module_path.clone(), problem);
|
||||
let mut buf = String::new();
|
||||
if let Some(report) = type_problem(&alloc, module_path.clone(), problem) {
|
||||
let mut buf = String::new();
|
||||
|
||||
report.render_color_terminal(&mut buf, &alloc, &palette);
|
||||
report.render_color_terminal(&mut buf, &alloc, &palette);
|
||||
|
||||
lines.push(buf);
|
||||
lines.push(buf);
|
||||
}
|
||||
}
|
||||
|
||||
for problem in mono_problems {
|
||||
|
|
43
compiler/test_mono/generated/empty_list_of_function_type.txt
Normal file
43
compiler/test_mono/generated/empty_list_of_function_type.txt
Normal file
|
@ -0,0 +1,43 @@
|
|||
procedure List.3 (#Attr.2, #Attr.3):
|
||||
let Test.20 = lowlevel ListLen #Attr.2;
|
||||
let Test.17 = lowlevel NumLt #Attr.3 Test.20;
|
||||
if Test.17 then
|
||||
let Test.19 = lowlevel ListGetUnsafe #Attr.2 #Attr.3;
|
||||
let Test.18 = Ok Test.19;
|
||||
ret Test.18;
|
||||
else
|
||||
let Test.16 = Struct {};
|
||||
let Test.15 = Err Test.16;
|
||||
ret Test.15;
|
||||
|
||||
procedure Test.2 (Test.6):
|
||||
let Test.24 = "bar";
|
||||
ret Test.24;
|
||||
|
||||
procedure Test.0 ():
|
||||
let Test.1 = Array [];
|
||||
joinpoint Test.22 Test.3:
|
||||
let Test.14 = 0i64;
|
||||
let Test.7 = CallByName List.3 Test.3 Test.14;
|
||||
dec Test.3;
|
||||
let Test.11 = 1i64;
|
||||
let Test.12 = GetTagId Test.7;
|
||||
let Test.13 = lowlevel Eq Test.11 Test.12;
|
||||
if Test.13 then
|
||||
let Test.5 = UnionAtIndex (Id 1) (Index 0) Test.7;
|
||||
let Test.9 = "foo";
|
||||
let Test.8 = CallByName Test.2 Test.9;
|
||||
dec Test.9;
|
||||
ret Test.8;
|
||||
else
|
||||
let Test.10 = "bad!";
|
||||
ret Test.10;
|
||||
in
|
||||
let Test.25 = false;
|
||||
if Test.25 then
|
||||
jump Test.22 Test.1;
|
||||
else
|
||||
dec Test.1;
|
||||
let Test.23 = Struct {};
|
||||
let Test.21 = Array [Test.23];
|
||||
jump Test.22 Test.21;
|
|
@ -10,7 +10,7 @@ procedure Num.27 (#Attr.2, #Attr.3):
|
|||
let Test.26 = lowlevel NumLt #Attr.2 #Attr.3;
|
||||
ret Test.26;
|
||||
|
||||
procedure Test.1 (Test.29, Test.30, Test.31):
|
||||
procedure Test.1 (Test.27, Test.28, Test.29):
|
||||
joinpoint Test.12 Test.2 Test.3 Test.4:
|
||||
let Test.14 = CallByName Num.27 Test.3 Test.4;
|
||||
if Test.14 then
|
||||
|
@ -29,7 +29,7 @@ procedure Test.1 (Test.29, Test.30, Test.31):
|
|||
else
|
||||
ret Test.2;
|
||||
in
|
||||
jump Test.12 Test.29 Test.30 Test.31;
|
||||
jump Test.12 Test.27 Test.28 Test.29;
|
||||
|
||||
procedure Test.0 ():
|
||||
let Test.9 = Array [];
|
||||
|
|
|
@ -1,43 +1,53 @@
|
|||
procedure Num.24 (#Attr.2, #Attr.3):
|
||||
let Test.24 = lowlevel NumAdd #Attr.2 #Attr.3;
|
||||
ret Test.24;
|
||||
let Test.27 = lowlevel NumAdd #Attr.2 #Attr.3;
|
||||
ret Test.27;
|
||||
|
||||
procedure Num.26 (#Attr.2, #Attr.3):
|
||||
let Test.19 = lowlevel NumMul #Attr.2 #Attr.3;
|
||||
ret Test.19;
|
||||
|
||||
procedure Test.1 ():
|
||||
let Test.25 = 1i64;
|
||||
ret Test.25;
|
||||
|
||||
procedure Test.2 ():
|
||||
let Test.20 = 2i64;
|
||||
ret Test.20;
|
||||
|
||||
procedure Test.3 (Test.6):
|
||||
let Test.23 = CallByName Test.1;
|
||||
let Test.22 = CallByName Num.24 Test.6 Test.23;
|
||||
let Test.22 = lowlevel NumMul #Attr.2 #Attr.3;
|
||||
ret Test.22;
|
||||
|
||||
procedure Test.1 ():
|
||||
let Test.28 = 1i64;
|
||||
ret Test.28;
|
||||
|
||||
procedure Test.2 ():
|
||||
let Test.23 = 2i64;
|
||||
ret Test.23;
|
||||
|
||||
procedure Test.3 (Test.6):
|
||||
let Test.26 = CallByName Test.1;
|
||||
let Test.25 = CallByName Num.24 Test.6 Test.26;
|
||||
ret Test.25;
|
||||
|
||||
procedure Test.4 (Test.7):
|
||||
let Test.18 = CallByName Test.2;
|
||||
let Test.17 = CallByName Num.26 Test.7 Test.18;
|
||||
ret Test.17;
|
||||
let Test.21 = CallByName Test.2;
|
||||
let Test.20 = CallByName Num.26 Test.7 Test.21;
|
||||
ret Test.20;
|
||||
|
||||
procedure Test.5 (Test.8, Test.9):
|
||||
let Test.14 = CallByName Test.3 Test.9;
|
||||
ret Test.14;
|
||||
joinpoint Test.15 Test.14:
|
||||
ret Test.14;
|
||||
in
|
||||
switch Test.8:
|
||||
case 0:
|
||||
let Test.16 = CallByName Test.3 Test.9;
|
||||
jump Test.15 Test.16;
|
||||
|
||||
default:
|
||||
let Test.17 = CallByName Test.4 Test.9;
|
||||
jump Test.15 Test.17;
|
||||
|
||||
|
||||
procedure Test.0 ():
|
||||
joinpoint Test.16 Test.12:
|
||||
joinpoint Test.19 Test.12:
|
||||
let Test.13 = 42i64;
|
||||
let Test.11 = CallByName Test.5 Test.12 Test.13;
|
||||
ret Test.11;
|
||||
in
|
||||
let Test.21 = true;
|
||||
if Test.21 then
|
||||
let Test.3 = Struct {};
|
||||
jump Test.16 Test.3;
|
||||
let Test.24 = true;
|
||||
if Test.24 then
|
||||
let Test.3 = false;
|
||||
jump Test.19 Test.3;
|
||||
else
|
||||
let Test.4 = Struct {};
|
||||
jump Test.16 Test.4;
|
||||
let Test.4 = true;
|
||||
jump Test.19 Test.4;
|
||||
|
|
|
@ -1084,6 +1084,33 @@ fn specialize_lowlevel() {
|
|||
)
|
||||
}
|
||||
|
||||
#[mono_test]
|
||||
fn empty_list_of_function_type() {
|
||||
// see https://github.com/rtfeldman/roc/issues/1732
|
||||
indoc!(
|
||||
r#"
|
||||
app "test" provides [ main ] to "./platform"
|
||||
|
||||
main =
|
||||
myList : List (Str -> Str)
|
||||
myList = []
|
||||
|
||||
myClosure : Str -> Str
|
||||
myClosure = \_ -> "bar"
|
||||
|
||||
choose =
|
||||
if False then
|
||||
myList
|
||||
else
|
||||
[ myClosure ]
|
||||
|
||||
when List.get choose 0 is
|
||||
Ok f -> f "foo"
|
||||
Err _ -> "bad!"
|
||||
"#
|
||||
)
|
||||
}
|
||||
|
||||
// #[ignore]
|
||||
// #[mono_test]
|
||||
// fn static_str_closure() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue