Merge branch 'trunk' into dev-backend-num-is-zero

This commit is contained in:
satotake 2021-11-24 10:59:42 +00:00 committed by GitHub
commit 10afadd810
21 changed files with 726 additions and 506 deletions

62
Cargo.lock generated
View file

@ -245,6 +245,18 @@ dependencies = [
"typenum",
]
[[package]]
name = "bitvec"
version = "0.22.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5237f00a8c86130a0cc317830e558b966dd7850d48a953d998c813f01a41b527"
dependencies = [
"funty",
"radium",
"tap",
"wyz",
]
[[package]]
name = "block"
version = "0.1.6"
@ -1274,6 +1286,12 @@ version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2022715d62ab30faffd124d40b76f4134a550a87792276512b18d63272333394"
[[package]]
name = "funty"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1847abb9cb65d566acd5942e94aea9c8f547ad02c98e1649326fc0e8910b8b1e"
[[package]]
name = "futures"
version = "0.3.17"
@ -2512,6 +2530,28 @@ dependencies = [
"ttf-parser 0.12.3",
]
[[package]]
name = "packed_struct"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c48e482b9a59ad6c2cdb06f7725e7bd33fe3525baaf4699fde7bfea6a5b77b1"
dependencies = [
"bitvec",
"packed_struct_codegen",
"serde",
]
[[package]]
name = "packed_struct_codegen"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56e3692b867ec1d48ccb441e951637a2cc3130d0912c0059e48319e1c83e44bc"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "page_size"
version = "0.4.2"
@ -2887,6 +2927,12 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "radium"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "643f8f41a8ebc4c5dc4515c82bb8abd397b527fc20fd681b7c011c2aee5d44fb"
[[package]]
name = "rand"
version = "0.8.4"
@ -3345,6 +3391,7 @@ version = "0.1.0"
dependencies = [
"bumpalo",
"object 0.26.2",
"packed_struct",
"roc_builtins",
"roc_can",
"roc_collections",
@ -4017,6 +4064,12 @@ dependencies = [
"unicode-xid",
]
[[package]]
name = "tap"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369"
[[package]]
name = "target-lexicon"
version = "0.12.2"
@ -5049,6 +5102,15 @@ dependencies = [
"rand_core 0.6.3",
]
[[package]]
name = "wyz"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "129e027ad65ce1453680623c3fb5163cbf7107bfe1aa32257e7d0e63f9ced188"
dependencies = [
"tap",
]
[[package]]
name = "x11-clipboard"
version = "0.5.3"

View file

@ -810,17 +810,13 @@ fn type_to_variable<'a>(
*/
let mut arg_vars = Vec::with_capacity(args.len());
let mut new_aliases = BumpMap::new_in(arena);
for (arg, arg_type_id) in args.iter(mempool) {
for (_, arg_type_id) in args.iter(mempool) {
let arg_type = mempool.get(*arg_type_id);
let arg_var = type_to_variable(arena, mempool, subs, rank, pools, cached, arg_type);
let arg_str = arg.as_str(mempool);
arg_vars.push((roc_module::ident::Lowercase::from(arg_str), arg_var));
new_aliases.insert(arg_str, arg_var);
arg_vars.push(arg_var);
}
let arg_vars = AliasVariables::insert_into_subs(subs, arg_vars, []);

39
cli/src/format.rs Normal file
View file

@ -0,0 +1,39 @@
use std::path::PathBuf;
use bumpalo::collections::String;
use bumpalo::Bump;
use roc_fmt::def::fmt_def;
use roc_fmt::module::fmt_module;
use roc_parse::{
module::{self, module_defs},
parser::{Parser, State},
};
use roc_reporting::user_error;
pub fn format(files: Vec<PathBuf>) {
for file in files {
let arena = Bump::new();
let src = std::fs::read_to_string(&file).unwrap();
match module::parse_header(&arena, State::new(src.as_bytes())) {
Ok((result, state)) => {
let mut buf = String::new_in(&arena);
fmt_module(&mut buf, &result);
match module_defs().parse(&arena, state) {
Ok((_, loc_defs, _)) => {
for loc_def in loc_defs {
fmt_def(&mut buf, arena.alloc(loc_def.value), 0);
}
}
Err(error) => user_error!("Unexpected parse failure when parsing this for defs formatting:\n\n{:?}\n\nParse error was:\n\n{:?}\n\n", src, error)
}
std::fs::write(&file, buf).unwrap();
}
Err(error) => user_error!("Unexpected parse failure when parsing this for module header formatting:\n\n{:?}\n\nParse error was:\n\n{:?}\n\n", src, error)
};
}
}

View file

@ -9,14 +9,17 @@ use roc_load::file::LoadingProblem;
use roc_mono::ir::OptLevel;
use std::env;
use std::io;
use std::path::{Path, PathBuf};
use std::path::Path;
use std::path::PathBuf;
use std::process;
use std::process::Command;
use target_lexicon::BinaryFormat;
use target_lexicon::{Architecture, OperatingSystem, Triple, X86_32Architecture};
pub mod build;
mod format;
pub mod repl;
pub use format::format;
pub const CMD_BUILD: &str = "build";
pub const CMD_REPL: &str = "repl";
@ -24,6 +27,7 @@ pub const CMD_EDIT: &str = "edit";
pub const CMD_DOCS: &str = "docs";
pub const CMD_CHECK: &str = "check";
pub const CMD_VERSION: &str = "version";
pub const CMD_FORMAT: &str = "format";
pub const FLAG_DEBUG: &str = "debug";
pub const FLAG_DEV: &str = "dev";
@ -35,6 +39,7 @@ pub const FLAG_LINK: &str = "roc-linker";
pub const FLAG_PRECOMPILED: &str = "precompiled-host";
pub const FLAG_VALGRIND: &str = "valgrind";
pub const ROC_FILE: &str = "ROC_FILE";
pub const ROC_DIR: &str = "ROC_DIR";
pub const BACKEND: &str = "BACKEND";
pub const DIRECTORY_OR_FILES: &str = "DIRECTORY_OR_FILES";
pub const ARGS_FOR_APP: &str = "ARGS_FOR_APP";
@ -111,6 +116,14 @@ pub fn build_app<'a>() -> App<'a> {
.subcommand(App::new(CMD_REPL)
.about("Launch the interactive Read Eval Print Loop (REPL)")
)
.subcommand(App::new(CMD_FORMAT)
.about("Format Roc code")
.arg(
Arg::new(DIRECTORY_OR_FILES)
.index(1)
.multiple_values(true)
.required(false))
)
.subcommand(App::new(CMD_VERSION)
.about("Print version information")
)

View file

@ -1,7 +1,7 @@
use roc_cli::build::check_file;
use roc_cli::{
build_app, docs, repl, BuildConfig, CMD_BUILD, CMD_CHECK, CMD_DOCS, CMD_EDIT, CMD_REPL,
CMD_VERSION, DIRECTORY_OR_FILES, FLAG_TIME, ROC_FILE,
build_app, docs, format, repl, BuildConfig, CMD_BUILD, CMD_CHECK, CMD_DOCS, CMD_EDIT,
CMD_FORMAT, CMD_REPL, CMD_VERSION, DIRECTORY_OR_FILES, FLAG_TIME, ROC_FILE,
};
use roc_load::file::LoadingProblem;
use std::fs::{self, FileType};
@ -101,7 +101,10 @@ fn main() -> io::Result<()> {
match maybe_values {
None => {
let mut os_string_values: Vec<OsString> = Vec::new();
read_all_roc_files(&OsStr::new("./").to_os_string(), &mut os_string_values)?;
read_all_roc_files(
&std::env::current_dir()?.as_os_str().to_os_string(),
&mut os_string_values,
)?;
for os_string in os_string_values {
values.push(os_string);
}
@ -125,6 +128,44 @@ fn main() -> io::Result<()> {
Ok(0)
}
Some(CMD_FORMAT) => {
let maybe_values = matches
.subcommand_matches(CMD_FORMAT)
.unwrap()
.values_of_os(DIRECTORY_OR_FILES);
let mut values: Vec<OsString> = Vec::new();
match maybe_values {
None => {
let mut os_string_values: Vec<OsString> = Vec::new();
read_all_roc_files(
&std::env::current_dir()?.as_os_str().to_os_string(),
&mut os_string_values,
)?;
for os_string in os_string_values {
values.push(os_string);
}
}
Some(os_values) => {
for os_str in os_values {
values.push(os_str.to_os_string());
}
}
}
let mut roc_files = Vec::new();
// Populate roc_files
for os_str in values {
let metadata = fs::metadata(os_str.clone())?;
roc_files_recursive(os_str.as_os_str(), metadata.file_type(), &mut roc_files)?;
}
format(roc_files);
Ok(0)
}
Some(CMD_VERSION) => {
println!("roc {}", concatcp!(include_str!("../../version.txt"), "\n"));

View file

@ -23,6 +23,7 @@ target-lexicon = "0.12.2"
# Probably just need to specify an extra field that used to be implicit or something.
# When fixed also update the version of object in the linker.
object = { version = "0.26.2", features = ["write"] }
packed_struct = "0.10.0"
[dev-dependencies]
roc_can = { path = "../can" }

View file

@ -1,6 +1,7 @@
use crate::generic64::{Assembler, CallConv, RegTrait, SymbolStorage};
use crate::Relocation;
use bumpalo::collections::Vec;
use packed_struct::prelude::*;
use roc_collections::all::MutMap;
use roc_module::symbol::Symbol;
use roc_mono::layout::Layout;
@ -42,8 +43,16 @@ pub enum AArch64GeneralReg {
/// This can mean Zero or Stack Pointer depending on the context.
ZRSP = 31,
}
impl RegTrait for AArch64GeneralReg {}
impl AArch64GeneralReg {
#[inline(always)]
fn id(&self) -> u8 {
*self as u8
}
}
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
#[allow(dead_code)]
pub enum AArch64FloatReg {}
@ -518,90 +527,165 @@ impl Assembler<AArch64GeneralReg, AArch64FloatReg> for AArch64Assembler {
impl AArch64Assembler {}
/// AArch64Instruction, maps all instructions to an enum.
/// Decoding the function should be cheap because we will always inline.
/// All of the operations should resolved by constants, leave just some bit manipulation.
/// Enums may not be complete since we will only add what we need.
#[derive(Debug)]
enum AArch64Instruction {
_Reserved,
_SVE,
DPImm(DPImmGroup),
Branch(BranchGroup),
LdStr(LdStrGroup),
DPReg(DPRegGroup),
_DPFloat,
// Instructions
// ARM manual section C3
// https://developer.arm.com/documentation/ddi0487/ga
// Map all instructions to a packed struct.
trait Aarch64Bytes: PackedStruct {
#[inline(always)]
fn bytes(&self) -> [u8; 4] {
let mut bytes: [u8; 4] = [0, 0, 0, 0];
self.pack_to_slice(&mut bytes).unwrap();
bytes.reverse();
bytes
}
}
#[derive(Debug)]
enum BranchGroup {
UnconditionBranchReg {
opc: u8,
op2: u8,
op3: u8,
reg_n: AArch64GeneralReg,
op4: u8,
},
#[derive(PackedStruct, Debug)]
#[packed_struct(endian = "msb")]
pub struct MoveWideImmediate {
sf: bool,
opc: Integer<u8, packed_bits::Bits<2>>,
fixed: Integer<u8, packed_bits::Bits<6>>, // = 0b100101,
hw: Integer<u8, packed_bits::Bits<2>>,
imm16: u16,
reg_d: Integer<u8, packed_bits::Bits<5>>, // AArch64GeneralReg
}
#[derive(Debug)]
enum DPRegGroup {
AddSubShifted {
sf: bool,
subtract: bool,
set_flags: bool,
shift: u8,
reg_m: AArch64GeneralReg,
imm6: u8,
reg_n: AArch64GeneralReg,
reg_d: AArch64GeneralReg,
},
Logical {
sf: bool,
op: DPRegLogicalOp,
shift: u8,
reg_m: AArch64GeneralReg,
imm6: u8,
reg_n: AArch64GeneralReg,
reg_d: AArch64GeneralReg,
},
impl Aarch64Bytes for MoveWideImmediate {}
impl MoveWideImmediate {
#[inline(always)]
fn new(opc: u8, rd: AArch64GeneralReg, imm16: u16, hw: u8, sf: bool) -> Self {
// TODO: revisit this is we change where we want to check the shift
// currently this is done in the assembler above
// assert!(shift % 16 == 0 && shift <= 48);
debug_assert!(hw <= 0b11);
debug_assert!(opc <= 0b11);
Self {
reg_d: rd.id().into(),
imm16,
hw: hw.into(),
opc: opc.into(),
sf,
fixed: 0b100101.into(),
}
}
}
#[derive(Debug)]
enum DPImmGroup {
AddSubImm {
sf: bool,
subtract: bool,
set_flags: bool,
shift: bool,
#[derive(PackedStruct, Debug)]
#[packed_struct(endian = "msb")]
pub struct ArithmeticImmediate {
sf: bool,
op: bool, // add or subtract
s: bool,
fixed: Integer<u8, packed_bits::Bits<6>>, // = 0b100010,
sh: bool, // shift
imm12: Integer<u16, packed_bits::Bits<12>>,
reg_n: Integer<u8, packed_bits::Bits<5>>,
reg_d: Integer<u8, packed_bits::Bits<5>>,
}
impl Aarch64Bytes for ArithmeticImmediate {}
impl ArithmeticImmediate {
#[inline(always)]
fn new(
op: bool,
s: bool,
rd: AArch64GeneralReg,
rn: AArch64GeneralReg,
imm12: u16,
reg_n: AArch64GeneralReg,
reg_d: AArch64GeneralReg,
},
MoveWide {
sf: bool,
opc: u8,
hw: u8,
imm16: u16,
reg_d: AArch64GeneralReg,
},
sh: bool,
) -> Self {
debug_assert!(imm12 <= 0xFFF);
Self {
reg_d: rd.id().into(),
reg_n: rn.id().into(),
imm12: imm12.into(),
sh,
s,
op,
// true for 64 bit addition
// false for 32 bit addition
sf: true,
fixed: 0b100010.into(),
}
}
}
#[derive(Debug)]
enum LdStrGroup {
UnsignedImm {
size: u8,
v: bool,
opc: u8,
imm12: u16,
reg_n: AArch64GeneralReg,
reg_t: AArch64GeneralReg,
},
#[derive(Clone, Copy)]
#[allow(dead_code)]
enum ShiftType {
LSL = 0,
LSR = 1,
ASR = 2,
ROR = 3,
}
impl ShiftType {
#[inline(always)]
fn id(&self) -> u8 {
*self as u8
}
}
#[derive(PackedStruct)]
#[packed_struct(endian = "msb")]
pub struct ArithmeticShifted {
sf: bool,
op: bool, // add or subtract
s: bool,
fixed: Integer<u8, packed_bits::Bits<5>>, // = 0b01011,
shift: Integer<u8, packed_bits::Bits<2>>, // shift
fixed2: bool, // = 0b0,
reg_m: Integer<u8, packed_bits::Bits<5>>,
imm6: Integer<u8, packed_bits::Bits<6>>,
reg_n: Integer<u8, packed_bits::Bits<5>>,
reg_d: Integer<u8, packed_bits::Bits<5>>,
}
impl Aarch64Bytes for ArithmeticShifted {}
impl ArithmeticShifted {
#[inline(always)]
fn new(
op: bool,
s: bool,
shift: ShiftType,
imm6: u8,
rm: AArch64GeneralReg,
rn: AArch64GeneralReg,
rd: AArch64GeneralReg,
) -> Self {
debug_assert!(imm6 <= 0b111111);
Self {
reg_d: rd.id().into(),
reg_n: rn.id().into(),
imm6: imm6.into(),
reg_m: rm.id().into(),
fixed2: false,
shift: shift.id().into(),
fixed: 0b01011.into(),
s,
op,
// true for 64 bit addition
// false for 32 bit addition
sf: true,
}
}
}
#[derive(Debug)]
#[allow(dead_code)]
enum DPRegLogicalOp {
enum LogicalOp {
AND,
BIC,
ORR,
@ -612,155 +696,142 @@ enum DPRegLogicalOp {
BICS,
}
#[inline(always)]
fn build_instruction(inst: AArch64Instruction) -> [u8; 4] {
let mut out: u32 = 0;
match inst {
AArch64Instruction::Branch(branch) => {
out |= 0b101 << 26;
match branch {
BranchGroup::UnconditionBranchReg {
opc,
op2,
op3,
reg_n,
op4,
} => {
debug_assert!(opc <= 0b1111);
debug_assert!(op2 <= 0b11111);
debug_assert!(op3 <= 0b111111);
debug_assert!(op4 <= 0b1111);
out |= 0b1101011 << 25;
out |= (opc as u32) << 21;
out |= (op2 as u32) << 16;
out |= (op3 as u32) << 10;
out |= (reg_n as u32) << 5;
out |= op4 as u32;
}
}
#[derive(PackedStruct)]
#[packed_struct(endian = "msb")]
pub struct LogicalShiftedRegister {
sf: bool,
op: Integer<u8, packed_bits::Bits<2>>,
fixed: Integer<u8, packed_bits::Bits<5>>, // = 0b01010,
shift: Integer<u8, packed_bits::Bits<2>>, // shift
n: bool,
reg_m: Integer<u8, packed_bits::Bits<5>>,
imm6: Integer<u8, packed_bits::Bits<6>>,
reg_n: Integer<u8, packed_bits::Bits<5>>,
reg_d: Integer<u8, packed_bits::Bits<5>>,
}
impl Aarch64Bytes for LogicalShiftedRegister {}
impl LogicalShiftedRegister {
#[inline(always)]
fn new(
op: LogicalOp,
shift: ShiftType,
imm6: u8,
rm: AArch64GeneralReg,
rn: AArch64GeneralReg,
rd: AArch64GeneralReg,
) -> Self {
debug_assert!(imm6 <= 0b111111);
let (op, n) = match op {
LogicalOp::AND => (0b00, false),
LogicalOp::BIC => (0b00, true),
LogicalOp::ORR => (0b01, false),
LogicalOp::ORN => (0b01, true),
LogicalOp::EOR => (0b10, false),
LogicalOp::EON => (0b10, true),
LogicalOp::ANDS => (0b11, false),
LogicalOp::BICS => (0b11, true),
};
Self {
reg_d: rd.id().into(),
reg_n: rn.id().into(),
imm6: imm6.into(),
reg_m: rm.id().into(),
n,
shift: shift.id().into(),
fixed: 0b01010.into(),
op: op.into(),
// true for 64 bit addition
// false for 32 bit addition
sf: true,
}
AArch64Instruction::DPImm(dpimm) => {
out |= 0b100 << 26;
match dpimm {
DPImmGroup::MoveWide {
sf,
opc,
hw,
imm16,
reg_d,
} => {
out |= (sf as u32) << 31;
out |= (opc as u32) << 29;
out |= 0b101 << 23;
out |= (hw as u32) << 21;
out |= (imm16 as u32) << 5;
out |= reg_d as u32;
}
DPImmGroup::AddSubImm {
sf,
subtract,
set_flags,
shift,
imm12,
reg_n,
reg_d,
} => {
debug_assert!(imm12 <= 0xFFF);
out |= (sf as u32) << 31;
out |= (subtract as u32) << 30;
out |= (set_flags as u32) << 29;
out |= 0b010 << 23;
out |= (shift as u32) << 22;
out |= (imm12 as u32) << 10;
out |= (reg_n as u32) << 5;
out |= reg_d as u32;
}
}
}
AArch64Instruction::DPReg(dpreg) => {
out |= 0b101 << 25;
match dpreg {
DPRegGroup::Logical {
sf,
op,
shift,
reg_m,
imm6,
reg_n,
reg_d,
} => {
debug_assert!(shift <= 0b11);
debug_assert!(imm6 <= 0b111111);
let (opc, n) = match op {
DPRegLogicalOp::AND => (0b00, 0),
DPRegLogicalOp::BIC => (0b00, 1),
DPRegLogicalOp::ORR => (0b01, 0),
DPRegLogicalOp::ORN => (0b01, 1),
DPRegLogicalOp::EOR => (0b10, 0),
DPRegLogicalOp::EON => (0b10, 1),
DPRegLogicalOp::ANDS => (0b11, 0),
DPRegLogicalOp::BICS => (0b11, 1),
};
out |= (sf as u32) << 31;
out |= opc << 29;
out |= (shift as u32) << 22;
out |= n << 21;
out |= (reg_m as u32) << 16;
out |= (imm6 as u32) << 10;
out |= (reg_n as u32) << 5;
out |= reg_d as u32;
}
DPRegGroup::AddSubShifted {
sf,
subtract,
set_flags,
shift,
reg_m,
imm6,
reg_n,
reg_d,
} => {
debug_assert!(shift <= 0b11);
debug_assert!(imm6 <= 0b111111);
out |= (sf as u32) << 31;
out |= (subtract as u32) << 30;
out |= (set_flags as u32) << 29;
out |= 0b1 << 24;
out |= (shift as u32) << 22;
out |= (reg_m as u32) << 16;
out |= (imm6 as u32) << 10;
out |= (reg_n as u32) << 5;
out |= reg_d as u32;
}
}
}
AArch64Instruction::LdStr(ldstr) => {
out |= 0b1 << 27;
match ldstr {
LdStrGroup::UnsignedImm {
size,
v,
opc,
imm12,
reg_n,
reg_t,
} => {
debug_assert!(size <= 0b11);
debug_assert!(imm12 <= 0xFFF);
out |= (size as u32) << 30;
out |= 0b11 << 28;
out |= (v as u32) << 26;
out |= 0b1 << 24;
out |= (opc as u32) << 22;
out |= (imm12 as u32) << 10;
out |= (reg_n as u32) << 5;
out |= reg_t as u32;
}
}
}
x => unimplemented!("The instruction, {:?}, has not be implemented yet", x),
}
out.to_le_bytes()
}
#[derive(PackedStruct)]
pub struct UnconditionalBranchRegister {
fixed: Integer<u8, packed_bits::Bits<7>>,
z: bool,
fixed2: bool,
op: Integer<u8, packed_bits::Bits<2>>,
fixed3: Integer<u8, packed_bits::Bits<5>>,
fixed4: Integer<u8, packed_bits::Bits<4>>,
a: bool,
m: bool,
rn: Integer<u8, packed_bits::Bits<5>>,
fixed5: Integer<u8, packed_bits::Bits<5>>,
}
impl Aarch64Bytes for UnconditionalBranchRegister {}
impl UnconditionalBranchRegister {
#[inline(always)]
fn new(op: u8, rn: AArch64GeneralReg) -> Self {
debug_assert!(op <= 0b11);
Self {
fixed5: 0b00000.into(),
rn: rn.id().into(),
m: false,
a: false,
fixed4: 0b0000.into(),
fixed3: 0b11111.into(),
op: op.into(),
fixed2: false,
z: false,
fixed: 0b1101011.into(),
}
}
}
// Uses unsigned Offset
// opc = 0b01 means load
// opc = 0b00 means store
#[derive(PackedStruct, Debug)]
#[packed_struct(endian = "msb")]
pub struct LoadStoreRegisterImmediate {
size: Integer<u8, packed_bits::Bits<2>>,
fixed: Integer<u8, packed_bits::Bits<3>>, // = 0b111,
fixed2: bool,
fixed3: Integer<u8, packed_bits::Bits<2>>,
opc: Integer<u8, packed_bits::Bits<2>>,
imm12: Integer<u16, packed_bits::Bits<12>>,
rn: Integer<u8, packed_bits::Bits<5>>,
rt: Integer<u8, packed_bits::Bits<5>>,
}
impl Aarch64Bytes for LoadStoreRegisterImmediate {}
impl LoadStoreRegisterImmediate {
#[inline(always)]
fn new(size: u8, opc: u8, imm12: u16, rn: AArch64GeneralReg, rt: AArch64GeneralReg) -> Self {
debug_assert!(size <= 0b11);
debug_assert!(imm12 <= 0xFFF);
Self {
rt: rt.id().into(),
rn: rn.id().into(),
imm12: imm12.into(),
opc: opc.into(),
fixed3: 0b01.into(),
fixed2: false,
fixed: 0b111.into(),
size: size.into(),
}
}
#[inline(always)]
fn new_load(size: u8, imm12: u16, rn: AArch64GeneralReg, rt: AArch64GeneralReg) -> Self {
Self::new(size, 0b01, imm12, rn, rt)
}
#[inline(always)]
fn new_store(size: u8, imm12: u16, rn: AArch64GeneralReg, rt: AArch64GeneralReg) -> Self {
Self::new(size, 0b00, imm12, rn, rt)
}
}
// Below here are the functions for all of the assembly instructions.
@ -777,17 +848,9 @@ fn add_reg64_reg64_imm12(
src: AArch64GeneralReg,
imm12: u16,
) {
buf.extend(&build_instruction(AArch64Instruction::DPImm(
DPImmGroup::AddSubImm {
sf: true,
subtract: false,
set_flags: false,
shift: false,
imm12,
reg_n: src,
reg_d: dst,
},
)));
let inst = ArithmeticImmediate::new(false, false, dst, src, imm12, false);
buf.extend(inst.bytes());
}
/// `ADD Xd, Xm, Xn` -> Add Xm and Xn and place the result into Xd.
@ -798,18 +861,9 @@ fn add_reg64_reg64_reg64(
src1: AArch64GeneralReg,
src2: AArch64GeneralReg,
) {
buf.extend(&build_instruction(AArch64Instruction::DPReg(
DPRegGroup::AddSubShifted {
sf: true,
subtract: false,
set_flags: false,
shift: 0,
reg_m: src1,
imm6: 0,
reg_n: src2,
reg_d: dst,
},
)));
let inst = ArithmeticShifted::new(false, false, ShiftType::LSL, 0, src1, src2, dst);
buf.extend(inst.bytes());
}
/// `LDR Xt, [Xn, #offset]` -> Load Xn + Offset Xt. ZRSP is SP.
@ -821,66 +875,41 @@ fn ldr_reg64_imm12(
base: AArch64GeneralReg,
imm12: u16,
) {
debug_assert!(imm12 <= 0xFFF);
buf.extend(&build_instruction(AArch64Instruction::LdStr(
LdStrGroup::UnsignedImm {
size: 0b11,
v: false,
opc: 0b01,
imm12,
reg_n: base,
reg_t: dst,
},
)));
let inst = LoadStoreRegisterImmediate::new_load(0b11, imm12, base, dst);
buf.extend(inst.bytes());
}
/// `MOV Xd, Xm` -> Move Xm to Xd.
#[inline(always)]
fn mov_reg64_reg64(buf: &mut Vec<'_, u8>, dst: AArch64GeneralReg, src: AArch64GeneralReg) {
// MOV is equvalent to `ORR Xd, XZR, XM` in AARCH64.
buf.extend(&build_instruction(AArch64Instruction::DPReg(
DPRegGroup::Logical {
sf: true,
op: DPRegLogicalOp::ORR,
shift: 0,
reg_m: src,
imm6: 0,
reg_n: AArch64GeneralReg::ZRSP,
reg_d: dst,
},
)));
let inst = LogicalShiftedRegister::new(
LogicalOp::ORR,
ShiftType::LSL,
0,
src,
AArch64GeneralReg::ZRSP,
dst,
);
buf.extend(inst.bytes());
}
/// `MOVK Xd, imm16` -> Keeps Xd and moves an optionally shifted imm16 to Xd.
#[inline(always)]
fn movk_reg64_imm16(buf: &mut Vec<'_, u8>, dst: AArch64GeneralReg, imm16: u16, hw: u8) {
debug_assert!(hw <= 0b11);
// MOV is equvalent to `ORR Xd, XZR, XM` in AARCH64.
buf.extend(&build_instruction(AArch64Instruction::DPImm(
DPImmGroup::MoveWide {
sf: true,
opc: 0b11,
hw,
imm16,
reg_d: dst,
},
)));
let inst = MoveWideImmediate::new(0b11, dst, imm16, hw, true);
buf.extend(inst.bytes());
}
/// `MOVZ Xd, imm16` -> Zeros Xd and moves an optionally shifted imm16 to Xd.
#[inline(always)]
fn movz_reg64_imm16(buf: &mut Vec<'_, u8>, dst: AArch64GeneralReg, imm16: u16, hw: u8) {
debug_assert!(hw <= 0b11);
// MOV is equvalent to `ORR Xd, XZR, XM` in AARCH64.
buf.extend(&build_instruction(AArch64Instruction::DPImm(
DPImmGroup::MoveWide {
sf: true,
opc: 0b10,
hw,
imm16,
reg_d: dst,
},
)));
let inst = MoveWideImmediate::new(0b10, dst, imm16, hw, true);
buf.extend(inst.bytes());
}
/// `STR Xt, [Xn, #offset]` -> Store Xt to Xn + Offset. ZRSP is SP.
@ -892,17 +921,9 @@ fn str_reg64_imm12(
base: AArch64GeneralReg,
imm12: u16,
) {
debug_assert!(imm12 <= 0xFFF);
buf.extend(&build_instruction(AArch64Instruction::LdStr(
LdStrGroup::UnsignedImm {
size: 0b11,
v: false,
opc: 0b00,
imm12,
reg_n: base,
reg_t: src,
},
)));
let inst = LoadStoreRegisterImmediate::new_store(0b11, imm12, base, src);
buf.extend(inst.bytes());
}
/// `SUB Xd, Xn, imm12` -> Subtract Xn and imm12 and place the result into Xd.
@ -913,31 +934,17 @@ fn sub_reg64_reg64_imm12(
src: AArch64GeneralReg,
imm12: u16,
) {
buf.extend(&build_instruction(AArch64Instruction::DPImm(
DPImmGroup::AddSubImm {
sf: true,
subtract: true,
set_flags: false,
shift: false,
imm12,
reg_n: src,
reg_d: dst,
},
)));
let inst = ArithmeticImmediate::new(true, false, dst, src, imm12, false);
buf.extend(inst.bytes());
}
/// `RET Xn` -> Return to the address stored in Xn.
#[inline(always)]
fn ret_reg64(buf: &mut Vec<'_, u8>, xn: AArch64GeneralReg) {
buf.extend(&build_instruction(AArch64Instruction::Branch(
BranchGroup::UnconditionBranchReg {
opc: 0b0010,
op2: 0b11111,
op3: 0b000000,
reg_n: xn,
op4: 0b000,
},
)));
let inst = UnconditionalBranchRegister::new(0b10, xn);
buf.extend(inst.bytes());
}
#[cfg(test)]

View file

@ -82,6 +82,28 @@ pub fn build_module<'a>(
Object::new(BinaryFormat::Elf, Architecture::Aarch64, Endianness::Little),
)
}
Triple {
architecture: TargetArch::Aarch64(_),
binary_format: TargetBF::Macho,
..
} if cfg!(feature = "target-aarch64") => {
let backend: Backend64Bit<
aarch64::AArch64GeneralReg,
aarch64::AArch64FloatReg,
aarch64::AArch64Assembler,
aarch64::AArch64Call,
> = Backend::new(env)?;
build_object(
env,
procedures,
backend,
Object::new(
BinaryFormat::MachO,
Architecture::Aarch64,
Endianness::Little,
),
)
}
x => Err(format! {
"the target, {:?}, is not yet implemented",
x}),

View file

@ -7,7 +7,7 @@ use roc_module::symbol::Symbol;
use roc_mono::ir::{CallType, Expr, JoinPointId, Literal, Proc, Stmt};
use roc_mono::layout::{Builtin, Layout, LayoutIds};
use crate::layout::WasmLayout;
use crate::layout::{StackMemoryFormat, WasmLayout};
use crate::low_level::{decode_low_level, LowlevelBuildResult};
use crate::storage::{Storage, StoredValue, StoredValueKind};
use crate::wasm_module::linking::{
@ -237,33 +237,38 @@ impl<'a> WasmBackend<'a> {
fn build_stmt(&mut self, stmt: &Stmt<'a>, ret_layout: &Layout<'a>) -> Result<(), String> {
match stmt {
Stmt::Let(sym, expr, layout, following) => {
let wasm_layout = WasmLayout::new(layout);
Stmt::Let(_, _, _, _) => {
let mut current_stmt = stmt;
while let Stmt::Let(sym, expr, layout, following) = current_stmt {
let wasm_layout = WasmLayout::new(layout);
let kind = match following {
Stmt::Ret(ret_sym) if *sym == *ret_sym => StoredValueKind::ReturnValue,
_ => StoredValueKind::Variable,
};
let kind = match following {
Stmt::Ret(ret_sym) if *sym == *ret_sym => StoredValueKind::ReturnValue,
_ => StoredValueKind::Variable,
};
let sym_storage = self.storage.allocate(&wasm_layout, *sym, kind);
let sym_storage = self.storage.allocate(&wasm_layout, *sym, kind);
self.build_expr(sym, expr, layout, &sym_storage)?;
self.build_expr(sym, expr, layout, &sym_storage)?;
// For primitives, we record that this symbol is at the top of the VM stack
// (For other values, we wrote to memory and there's nothing on the VM stack)
if let WasmLayout::Primitive(value_type, size) = wasm_layout {
let vm_state = self.code_builder.set_top_symbol(*sym);
self.storage.symbol_storage_map.insert(
*sym,
StoredValue::VirtualMachineStack {
vm_state,
value_type,
size,
},
);
// For primitives, we record that this symbol is at the top of the VM stack
// (For other values, we wrote to memory and there's nothing on the VM stack)
if let WasmLayout::Primitive(value_type, size) = wasm_layout {
let vm_state = self.code_builder.set_top_symbol(*sym);
self.storage.symbol_storage_map.insert(
*sym,
StoredValue::VirtualMachineStack {
vm_state,
value_type,
size,
},
);
}
current_stmt = *following;
}
self.build_stmt(following, ret_layout)?;
self.build_stmt(current_stmt, ret_layout)?;
Ok(())
}
@ -803,15 +808,35 @@ impl<'a> WasmBackend<'a> {
Some(ret_layout.value_type())
};
// Zig's "fast calling convention" packs structs into CPU registers (stack machine slots) if possible.
// If they're small enough they can go into an I32 or I64. If they're big, they're pointers (I32).
for arg in arguments {
param_types.push(match self.storage.get(arg) {
StoredValue::StackMemory { size, .. } if *size > 4 && *size <= 8 => {
ValueType::I64
match self.storage.get(arg) {
StoredValue::StackMemory { size, format, .. } => {
use StackMemoryFormat::*;
match format {
Aggregate => {
// Zig's "fast calling convention" packs structs into CPU registers
// (stack machine slots) if possible. If they're small enough they
// can go into an I32 or I64. If they're big, they're pointers (I32).
if *size > 4 && *size <= 8 {
param_types.push(ValueType::I64)
} else {
// either
//
// - this is a small value, that fits in an i32
// - this is a big value, we pass a memory address
param_types.push(ValueType::I32)
}
}
Int128 | Float128 | Decimal => {
// these types are passed as 2 i64s
param_types.push(ValueType::I64);
param_types.push(ValueType::I64);
}
}
}
stored => stored.value_type(),
});
stored => param_types.push(stored.value_type()),
}
}
let signature_index = self.module.types.insert(Signature {

View file

@ -2,7 +2,7 @@ use roc_builtins::bitcode::{self, FloatWidth};
use roc_module::low_level::{LowLevel, LowLevel::*};
use roc_module::symbol::Symbol;
use crate::layout::WasmLayout;
use crate::layout::{StackMemoryFormat, WasmLayout};
use crate::storage::Storage;
use crate::wasm_module::{
CodeBuilder,
@ -63,11 +63,20 @@ pub fn decode_low_level<'a>(
return NotImplemented;
}
NumAdd => match ret_layout.value_type() {
I32 => code_builder.i32_add(),
I64 => code_builder.i64_add(),
F32 => code_builder.f32_add(),
F64 => code_builder.f64_add(),
NumAdd => match ret_layout {
WasmLayout::Primitive(value_type, _) => match value_type {
I32 => code_builder.i32_add(),
I64 => code_builder.i64_add(),
F32 => code_builder.f32_add(),
F64 => code_builder.f64_add(),
},
WasmLayout::StackMemory { format, .. } => match format {
StackMemoryFormat::Aggregate => return NotImplemented,
StackMemoryFormat::Int128 => return NotImplemented,
StackMemoryFormat::Float128 => return NotImplemented,
StackMemoryFormat::Decimal => return BuiltinCall(bitcode::DEC_ADD_WITH_OVERFLOW),
},
WasmLayout::HeapMemory { .. } => return NotImplemented,
},
NumAddWrap => match ret_layout.value_type() {
I32 => {

View file

@ -247,21 +247,51 @@ impl<'a> Storage<'a> {
} => {
let (local_id, offset) = location.local_and_offset(self.stack_frame_pointer);
// Load the address of the value
code_builder.get_local(local_id);
if format == StackMemoryFormat::Aggregate {
if offset != 0 {
code_builder.i32_const(offset as i32);
code_builder.i32_add();
}
} else {
// It's one of the 128-bit numbers, all of which we load as two i64's
// (Mark the same Symbol twice. Shouldn't matter except for debugging.)
code_builder.i64_load(Align::Bytes8, offset);
code_builder.set_top_symbol(sym);
code_builder.get_local(local_id);
code_builder.i64_load(Align::Bytes8, offset + 8);
}
code_builder.set_top_symbol(sym);
}
}
}
/// stack memory values are returned by pointer. e.g. a roc function
///
/// add : I128, I128 -> I128
///
/// is given the wasm type
///
/// add : (i32, i64, i64, i64, i64) -> nil
///
/// The returned value is written to the address passed as the first argument
fn load_return_address_ccc(&mut self, code_builder: &mut CodeBuilder, sym: Symbol) {
let storage = self.get(&sym).to_owned();
match storage {
StoredValue::VirtualMachineStack { .. } | StoredValue::Local { .. } => {
unreachable!("these storage types are not returned by writing to a pointer")
}
StoredValue::StackMemory { location, .. } => {
let (local_id, offset) = location.local_and_offset(self.stack_frame_pointer);
code_builder.get_local(local_id);
if offset != 0 {
code_builder.i32_const(offset as i32);
code_builder.i32_add();
}
if format != StackMemoryFormat::Aggregate {
// It's one of the 128-bit numbers, all of which we load as two i64's
// Mark the same Symbol twice in the VM value stack! Shouldn't matter except debug.
code_builder.i64_load(Align::Bytes8, offset);
code_builder.set_top_symbol(sym);
code_builder.i64_load(Align::Bytes8, offset + 8);
}
code_builder.set_top_symbol(sym);
}
}
@ -299,9 +329,8 @@ impl<'a> Storage<'a> {
if return_layout.is_stack_memory() {
// Load the address where the return value should be written
// Apparently for return values we still use a pointer to stack memory
self.load_symbol_ccc(code_builder, return_symbol);
};
self.load_return_address_ccc(code_builder, return_symbol);
}
for sym in symbols {
if let StoredValue::StackMemory {

View file

@ -29,7 +29,7 @@ impl std::ops::Deref for ModuleName {
}
/// An uncapitalized identifier, such as a field name or local variable
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[derive(Clone, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Lowercase(IdentStr);
/// A capitalized identifier, such as a tag name or module name

View file

@ -100,15 +100,17 @@ impl Pools {
}
pub fn get_mut(&mut self, rank: Rank) -> &mut Vec<Variable> {
self.0
.get_mut(rank.into_usize())
.unwrap_or_else(|| panic!("Compiler bug: could not find pool at rank {}", rank))
match self.0.get_mut(rank.into_usize()) {
Some(reference) => reference,
None => panic!("Compiler bug: could not find pool at rank {}", rank),
}
}
pub fn get(&self, rank: Rank) -> &Vec<Variable> {
self.0
.get(rank.into_usize())
.unwrap_or_else(|| panic!("Compiler bug: could not find pool at rank {}", rank))
match self.0.get(rank.into_usize()) {
Some(reference) => reference,
None => panic!("Compiler bug: could not find pool at rank {}", rank),
}
}
pub fn iter(&self) -> std::slice::Iter<'_, Vec<Variable>> {
@ -628,18 +630,6 @@ fn type_to_var(
type_to_variable(subs, rank, pools, &arena, typ)
}
/// Abusing existing functions for our purposes
/// this is to put a solved type back into subs
pub fn insert_type_into_subs(subs: &mut Subs, typ: &Type) -> Variable {
let rank = Rank::NONE;
let mut pools = Pools::default();
// capacity based on the false hello world program
let arena = bumpalo::Bump::with_capacity(4 * 1024);
type_to_variable(subs, rank, &mut pools, &arena, typ)
}
fn type_to_variable<'a>(
subs: &mut Subs,
rank: Rank,
@ -652,14 +642,13 @@ fn type_to_variable<'a>(
match typ {
Variable(var) => *var,
Apply(symbol, args) => {
let mut new_arg_vars = Vec::with_capacity_in(args.len(), arena);
let arg_vars = VariableSubsSlice::reserve_into_subs(subs, args.len());
for arg in args {
for (i, arg) in (arg_vars.slice.start as usize..).zip(args) {
let var = type_to_variable(subs, rank, pools, arena, arg);
new_arg_vars.push(var);
subs.variables[i] = var;
}
let arg_vars = VariableSubsSlice::insert_into_subs(subs, new_arg_vars);
let flat_type = FlatType::Apply(*symbol, arg_vars);
let content = Content::Structure(flat_type);
@ -669,16 +658,14 @@ fn type_to_variable<'a>(
EmptyTagUnion => Variable::EMPTY_TAG_UNION,
// This case is important for the rank of boolean variables
Function(arg_vars, closure_type, ret_type) => {
let mut new_arg_vars = Vec::with_capacity_in(arg_vars.len(), arena);
Function(args, closure_type, ret_type) => {
let arg_vars = VariableSubsSlice::reserve_into_subs(subs, args.len());
for arg in arg_vars {
for (i, arg) in (arg_vars.slice.start as usize..).zip(args) {
let var = type_to_variable(subs, rank, pools, arena, arg);
new_arg_vars.push(var);
subs.variables[i] = var;
}
let arg_vars = VariableSubsSlice::insert_into_subs(subs, new_arg_vars);
let ret_var = type_to_variable(subs, rank, pools, arena, ret_type);
let closure_var = type_to_variable(subs, rank, pools, arena, closure_type);
let content = Content::Structure(FlatType::Func(arg_vars, closure_var, ret_var));
@ -801,10 +788,10 @@ fn type_to_variable<'a>(
let mut arg_vars = Vec::with_capacity_in(args.len(), arena);
for (arg, arg_type) in args {
for (_, arg_type) in args {
let arg_var = type_to_variable(subs, rank, pools, arena, arg_type);
arg_vars.push((arg.clone(), arg_var));
arg_vars.push(arg_var);
}
let lambda_set_variables_it = lambda_set_variables
@ -829,10 +816,10 @@ fn type_to_variable<'a>(
} => {
let mut arg_vars = Vec::with_capacity_in(args.len(), arena);
for (arg, arg_type) in args {
for (_, arg_type) in args {
let arg_var = type_to_variable(subs, rank, pools, arena, arg_type);
arg_vars.push((arg.clone(), arg_var));
arg_vars.push(arg_var);
}
let lambda_set_variables_it = lambda_set_variables
@ -884,14 +871,13 @@ fn type_to_union_tags<'a>(
let mut tag_vars = Vec::with_capacity_in(tags.len(), arena);
let mut tag_argument_vars = Vec::with_capacity_in(tags.len(), arena);
for (tag, tag_argument_types) in tags {
for arg_type in tag_argument_types {
let new_var = type_to_variable(subs, rank, pools, arena, arg_type);
tag_argument_vars.push(new_var);
}
let new_slice = VariableSubsSlice::reserve_into_subs(subs, tag_argument_types.len());
let new_slice = VariableSubsSlice::insert_into_subs(subs, tag_argument_vars.drain(..));
for (i, arg) in (new_slice.slice.start as usize..).zip(tag_argument_types) {
let var = type_to_variable(subs, rank, pools, arena, arg);
subs.variables[i] = var;
}
tag_vars.push((tag.clone(), new_slice));
}
@ -1654,12 +1640,14 @@ fn deep_copy_var_help(
}
fn register(subs: &mut Subs, rank: Rank, pools: &mut Pools, content: Content) -> Variable {
let var = subs.fresh(Descriptor {
let descriptor = Descriptor {
content,
rank,
mark: Mark::NONE,
copy: OptVariable::NONE,
});
};
let var = subs.fresh(descriptor);
pools.get_mut(rank).push(var);

View file

@ -609,7 +609,7 @@ fn gen_float_eq() {
}
#[test]
#[cfg(any(feature = "gen-llvm"))]
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm"))]
fn gen_add_dec() {
assert_evals_to!(
indoc!(

View file

@ -199,7 +199,7 @@ fn find_names_needed(
}
Alias(_symbol, args, _actual) => {
// only find names for named parameters!
for var_index in args.variables().into_iter().take(args.len()) {
for var_index in args.into_iter().take(args.len()) {
let var = subs[var_index];
find_names_needed(var, subs, roots, root_appearances, names_taken);
}
@ -309,7 +309,6 @@ fn write_content(env: &Env, content: &Content, subs: &Subs, buf: &mut String, pa
debug_assert_eq!(args.len(), 1);
let arg_var_index = args
.variables()
.into_iter()
.next()
.expect("Num was not applied to a type argument!");
@ -337,7 +336,7 @@ fn write_content(env: &Env, content: &Content, subs: &Subs, buf: &mut String, pa
_ => write_parens!(write_parens, buf, {
write_symbol(env, *symbol, buf);
for var_index in args.variables() {
for var_index in args.into_iter() {
let var = subs[var_index];
buf.push(' ');
write_content(

View file

@ -229,7 +229,7 @@ impl SolvedType {
}
}
pub fn from_var(subs: &Subs, var: Variable) -> Self {
fn from_var(subs: &Subs, var: Variable) -> Self {
let mut seen = RecursionVars::default();
Self::from_var_help(subs, &mut seen, var)
}
@ -254,16 +254,17 @@ impl SolvedType {
Alias(symbol, args, actual_var) => {
let mut new_args = Vec::with_capacity(args.len());
for (name_index, var_index) in args.named_type_arguments() {
for var_index in args.named_type_arguments() {
let arg_var = subs[var_index];
new_args.push((
subs[name_index].clone(),
Self::from_var_help(subs, recursion_vars, arg_var),
));
let node = Self::from_var_help(subs, recursion_vars, arg_var);
// NOTE we fake the lowercase here: the user will never get to see it anyway
new_args.push((Lowercase::default(), node));
}
let mut solved_lambda_sets = Vec::with_capacity(0);
for var_index in args.unnamed_type_arguments() {
let var = subs[var_index];

View file

@ -797,7 +797,7 @@ fn integer_type(
Content::Structure(FlatType::TagUnion(tags, Variable::EMPTY_TAG_UNION))
});
let vars = AliasVariables::insert_into_subs(subs, [("range".into(), signed64)], []);
let vars = AliasVariables::insert_into_subs(subs, [signed64], []);
subs.set_content(num_integer_signed64, {
Content::Alias(Symbol::NUM_INTEGER, vars, at_signed64)
});
@ -812,7 +812,7 @@ fn integer_type(
Content::Structure(FlatType::TagUnion(tags, Variable::EMPTY_TAG_UNION))
});
let vars = AliasVariables::insert_into_subs(subs, [("range".into(), integer_signed64)], []);
let vars = AliasVariables::insert_into_subs(subs, [integer_signed64], []);
subs.set_content(num_integer_signed64, {
Content::Alias(Symbol::NUM_NUM, vars, at_num_integer_signed64)
});
@ -1358,7 +1358,7 @@ impl From<Content> for Descriptor {
static_assertions::assert_eq_size!([u8; 4 * 8], Content);
static_assertions::assert_eq_size!([u8; 4 * 8], (Variable, Option<Lowercase>));
static_assertions::assert_eq_size!([u8; 3 * 8], (Symbol, AliasVariables, Variable));
static_assertions::assert_eq_size!([u8; 12], AliasVariables);
static_assertions::assert_eq_size!([u8; 8], AliasVariables);
static_assertions::assert_eq_size!([u8; 3 * 8], FlatType);
#[derive(Clone, Debug)]
@ -1382,29 +1382,26 @@ pub enum Content {
#[derive(Clone, Copy, Debug, Default)]
pub struct AliasVariables {
pub lowercases_start: u32,
pub variables_start: u32,
pub lowercases_len: u16,
pub variables_len: u16,
pub all_variables_len: u16,
/// an alias has type variables and lambda set variables
pub type_variables_len: u16,
}
impl AliasVariables {
pub const fn names(&self) -> SubsSlice<Lowercase> {
SubsSlice::new(self.lowercases_start, self.lowercases_len)
}
pub const fn variables(&self) -> VariableSubsSlice {
VariableSubsSlice {
slice: SubsSlice::new(self.variables_start, self.variables_len),
slice: SubsSlice::new(self.variables_start, self.all_variables_len),
}
}
pub const fn len(&self) -> usize {
self.lowercases_len as usize
self.type_variables_len as usize
}
pub const fn is_empty(&self) -> bool {
self.lowercases_len == 0
self.type_variables_len == 0
}
pub fn replace_variables(
@ -1416,24 +1413,21 @@ impl AliasVariables {
subs.variables.extend(variables);
let variables_len = (subs.variables.len() - variables_start as usize) as u16;
debug_assert_eq!(variables_len, self.variables_len);
debug_assert_eq!(variables_len, self.all_variables_len);
self.variables_start = variables_start;
}
pub fn named_type_arguments(
&self,
) -> impl Iterator<Item = (SubsIndex<Lowercase>, SubsIndex<Variable>)> {
let names = self.names();
let vars = self.variables();
names.into_iter().zip(vars.into_iter())
pub fn named_type_arguments(&self) -> impl Iterator<Item = SubsIndex<Variable>> {
self.variables()
.into_iter()
.take(self.type_variables_len as usize)
}
pub fn unnamed_type_arguments(&self) -> impl Iterator<Item = SubsIndex<Variable>> {
self.variables()
.into_iter()
.skip(self.lowercases_len as usize)
.skip(self.type_variables_len as usize)
}
pub fn insert_into_subs<I1, I2>(
@ -1442,38 +1436,41 @@ impl AliasVariables {
unnamed_arguments: I2,
) -> Self
where
I1: IntoIterator<Item = (Lowercase, Variable)>,
I1: IntoIterator<Item = Variable>,
I2: IntoIterator<Item = Variable>,
{
let lowercases_start = subs.field_names.len() as u32;
let variables_start = subs.variables.len() as u32;
let it1 = type_arguments.into_iter();
let it2 = unnamed_arguments.into_iter();
subs.variables.extend(type_arguments);
subs.variables
.reserve(it1.size_hint().0 + it2.size_hint().0);
subs.field_names.reserve(it1.size_hint().0);
let type_variables_len = (subs.variables.len() as u32 - variables_start) as u16;
for (field_name, var) in it1 {
subs.field_names.push(field_name);
subs.variables.push(var);
subs.variables.extend(unnamed_arguments);
let all_variables_len = (subs.variables.len() as u32 - variables_start) as u16;
if type_variables_len == 3 {
panic!();
}
subs.variables.extend(it2);
let lowercases_len = (subs.field_names.len() as u32 - lowercases_start) as u16;
let variables_len = (subs.variables.len() as u32 - variables_start) as u16;
Self {
lowercases_start,
variables_start,
lowercases_len,
variables_len,
type_variables_len,
all_variables_len,
}
}
}
impl IntoIterator for AliasVariables {
type Item = <VariableSubsSlice as IntoIterator>::Item;
type IntoIter = <VariableSubsSlice as IntoIterator>::IntoIter;
fn into_iter(self) -> Self::IntoIter {
self.variables().into_iter()
}
}
impl Content {
#[inline(always)]
pub fn is_number(&self) -> bool {
@ -1548,6 +1545,15 @@ impl VariableSubsSlice {
}
}
pub fn reserve_into_subs(subs: &mut Subs, length: usize) -> Self {
let start = subs.variables.len() as u32;
subs.variables
.extend(std::iter::repeat(Variable::NULL).take(length));
Self::new(start, length as u16)
}
pub fn insert_into_subs<I>(subs: &mut Subs, input: I) -> Self
where
I: IntoIterator<Item = Variable>,
@ -2081,7 +2087,7 @@ fn occurs(
let mut new_seen = seen.clone();
new_seen.insert(root_var);
for var_index in args.variables().into_iter() {
for var_index in args.into_iter() {
let var = subs[var_index];
short_circuit_help(subs, root_var, &new_seen, var)?;
}
@ -2265,7 +2271,7 @@ fn explicit_substitute(
in_var
}
Alias(symbol, args, actual) => {
for index in args.variables().into_iter() {
for index in args.into_iter() {
let var = subs[index];
let new_var = explicit_substitute(subs, from, to, var, seen);
subs[index] = new_var;
@ -2322,12 +2328,9 @@ fn get_var_names(
RigidVar(name) => add_name(subs, 0, name, var, RigidVar, taken_names),
Alias(_, args, _) => args
.variables()
.into_iter()
.fold(taken_names, |answer, arg_var| {
get_var_names(subs, subs[arg_var], answer)
}),
Alias(_, args, _) => args.into_iter().fold(taken_names, |answer, arg_var| {
get_var_names(subs, subs[arg_var], answer)
}),
Structure(flat_type) => match flat_type {
FlatType::Apply(_, args) => {
@ -2528,15 +2531,14 @@ fn content_to_err_type(
Alias(symbol, args, aliased_to) => {
let err_type = var_to_err_type(subs, state, aliased_to);
let mut err_args = Vec::with_capacity(args.names().len());
let mut err_args = Vec::with_capacity(args.len());
for (name_index, var_index) in args.named_type_arguments() {
let name = subs[name_index].clone();
for var_index in args.into_iter() {
let var = subs[var_index];
let arg = var_to_err_type(subs, state, var);
err_args.push((name, arg));
err_args.push(arg);
}
ErrorType::Alias(symbol, err_args, Box::new(err_type))
@ -2989,7 +2991,6 @@ impl StorageSubs {
offsets: &StorageSubsOffsets,
mut alias_variables: AliasVariables,
) -> AliasVariables {
alias_variables.lowercases_start += offsets.field_names;
alias_variables.variables_start += offsets.variables;
alias_variables
@ -3357,9 +3358,9 @@ fn deep_copy_var_to_help<'a>(
}
Alias(symbol, mut args, real_type_var) => {
let mut new_vars = Vec::with_capacity_in(args.variables().len(), arena);
let mut new_vars = Vec::with_capacity_in(args.len(), arena);
for var_index in args.variables() {
for var_index in args.into_iter() {
let var = source[var_index];
let new_var = deep_copy_var_to_help(arena, visited, source, target, max_rank, var);
@ -3368,12 +3369,6 @@ fn deep_copy_var_to_help<'a>(
args.replace_variables(target, new_vars);
let lowercases = &source.field_names[args.lowercases_start as usize..]
[..args.lowercases_len as usize];
args.lowercases_start = target.field_names.len() as u32;
target.field_names.extend(lowercases.iter().cloned());
let new_real_type_var =
deep_copy_var_to_help(arena, visited, source, target, max_rank, real_type_var);
let new_content = Alias(symbol, args, new_real_type_var);

View file

@ -1241,7 +1241,7 @@ pub enum ErrorType {
TagUnion(SendMap<TagName, Vec<ErrorType>>, TypeExt),
RecursiveTagUnion(Box<ErrorType>, SendMap<TagName, Vec<ErrorType>>, TypeExt),
Function(Vec<ErrorType>, Box<ErrorType>, Box<ErrorType>),
Alias(Symbol, Vec<(Lowercase, ErrorType)>, Box<ErrorType>),
Alias(Symbol, Vec<ErrorType>, Box<ErrorType>),
Error,
}
@ -1303,7 +1303,7 @@ fn write_error_type_help(
Alias(Symbol::NUM_NUM, mut arguments, _actual) => {
debug_assert!(arguments.len() == 1);
let argument = arguments.remove(0).1;
let argument = arguments.remove(0);
match argument {
Type(Symbol::NUM_INTEGER, _) => {
@ -1421,7 +1421,7 @@ fn write_debug_error_type_help(error_type: ErrorType, buf: &mut String, parens:
Alias(Symbol::NUM_NUM, mut arguments, _actual) => {
debug_assert!(arguments.len() == 1);
let argument = arguments.remove(0).1;
let argument = arguments.remove(0);
match argument {
Type(Symbol::NUM_INTEGER, _) => {
@ -1456,7 +1456,7 @@ fn write_debug_error_type_help(error_type: ErrorType, buf: &mut String, parens:
for arg in arguments {
buf.push(' ');
write_debug_error_type_help(arg.1, buf, Parens::InTypeParam);
write_debug_error_type_help(arg, buf, Parens::InTypeParam);
}
// useful for debugging

View file

@ -209,7 +209,8 @@ fn unify_alias(
problems
} else {
mismatch!("{}", symbol)
dbg!(args.len(), other_args.len());
mismatch!("{:?}", symbol)
}
} else {
unify_pool(subs, pool, real_var, *other_real_var)

View file

@ -471,12 +471,7 @@ fn read_main_roc_file(project_dir_path_opt: Option<&Path>) -> (PathStr, String)
let project_dir_path_str = path_to_string(project_dir_path);
if let Some(&roc_file_name) = roc_file_names.first() {
let full_roc_file_path_str = vec![
project_dir_path_str.clone(),
"/".to_owned(),
roc_file_name.clone(),
]
.join("");
let full_roc_file_path_str = path_to_string(&project_dir_path.join(roc_file_name));
let file_as_str = std::fs::read_to_string(&Path::new(&full_roc_file_path_str))
.unwrap_or_else(|err| panic!("In the provided project {:?}, I found the roc file {}, but I failed to read it: {}", &project_dir_path_str, &full_roc_file_path_str, err));

View file

@ -1493,7 +1493,7 @@ pub fn to_doc<'b>(
parens,
alloc.symbol_foreign_qualified(symbol),
args.into_iter()
.map(|(_, arg)| to_doc(alloc, Parens::InTypeParam, arg))
.map(|arg| to_doc(alloc, Parens::InTypeParam, arg))
.collect(),
),
@ -1657,10 +1657,7 @@ fn to_diff<'b>(
}
(Alias(symbol1, args1, _), Alias(symbol2, args2, _)) if symbol1 == symbol2 => {
// TODO remove collects
let a1 = args1.into_iter().map(|(_, v)| v).collect::<Vec<_>>();
let a2 = args2.into_iter().map(|(_, v)| v).collect::<Vec<_>>();
let args_diff = traverse(alloc, Parens::InTypeParam, a1, a2);
let args_diff = traverse(alloc, Parens::InTypeParam, args1, args2);
let left = report_text::apply(
alloc,
parens,
@ -1729,8 +1726,8 @@ fn to_diff<'b>(
ErrorType::Alias(Symbol::NUM_NUM, args, _) => {
matches!(
&args.get(0),
Some((_, ErrorType::Type(Symbol::NUM_INTEGER, _)))
| Some((_, ErrorType::Alias(Symbol::NUM_INTEGER, _, _)))
Some(ErrorType::Type(Symbol::NUM_INTEGER, _))
| Some(ErrorType::Alias(Symbol::NUM_INTEGER, _, _))
)
}
_ => false,
@ -1750,8 +1747,8 @@ fn to_diff<'b>(
ErrorType::Alias(Symbol::NUM_NUM, args, _) => {
matches!(
&args.get(0),
Some((_, ErrorType::Type(Symbol::NUM_FLOATINGPOINT, _)))
| Some((_, ErrorType::Alias(Symbol::NUM_FLOATINGPOINT, _, _)))
Some(ErrorType::Type(Symbol::NUM_FLOATINGPOINT, _))
| Some(ErrorType::Alias(Symbol::NUM_FLOATINGPOINT, _, _))
)
}
_ => false,