Merge branch 'trunk' into assoc-list-dict

This commit is contained in:
Folkert de Vries 2022-07-14 16:47:50 +02:00 committed by GitHub
commit 1b1b63aad0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
66 changed files with 2681 additions and 780 deletions

View file

@ -8,7 +8,7 @@ use crate::llvm::build_list::{
list_prepend, list_replace_unsafe, list_reserve, list_sort_with, list_sublist, list_swap,
list_symbol_to_c_abi, list_to_c_abi, list_with_capacity, pass_update_mode,
};
use crate::llvm::build_str::{str_from_float, str_from_int};
use crate::llvm::build_str::{dec_to_str, str_from_float, str_from_int};
use crate::llvm::compare::{generic_eq, generic_neq};
use crate::llvm::convert::{
self, argument_type_from_layout, basic_type_from_builtin, basic_type_from_layout,
@ -5434,7 +5434,14 @@ fn run_low_level<'a, 'ctx, 'env>(
// Str.fromFloat : Float * -> Str
debug_assert_eq!(args.len(), 1);
str_from_float(env, scope, args[0])
let (float, float_layout) = load_symbol_and_layout(scope, &args[0]);
let float_width = match float_layout {
Layout::Builtin(Builtin::Float(float_width)) => *float_width,
_ => unreachable!(),
};
str_from_float(env, float, float_width)
}
StrFromUtf8Range => {
debug_assert_eq!(args.len(), 3);
@ -5768,9 +5775,10 @@ fn run_low_level<'a, 'ctx, 'env>(
str_from_int(env, int, *int_width)
}
Layout::Builtin(Builtin::Float(_float_width)) => {
str_from_float(env, scope, args[0])
Layout::Builtin(Builtin::Float(float_width)) => {
str_from_float(env, num, *float_width)
}
Layout::Builtin(Builtin::Decimal) => dec_to_str(env, num),
_ => unreachable!(),
}
}

View file

@ -3,7 +3,7 @@ use crate::llvm::build::{Env, Scope};
use inkwell::builder::Builder;
use inkwell::values::{BasicValueEnum, IntValue, PointerValue, StructValue};
use inkwell::AddressSpace;
use roc_builtins::bitcode::{self, IntWidth};
use roc_builtins::bitcode::{self, FloatWidth, IntWidth};
use roc_module::symbol::Symbol;
use roc_mono::layout::{Builtin, Layout};
use roc_target::PtrWidth;
@ -103,15 +103,37 @@ pub fn decode_from_utf8_result<'a, 'ctx, 'env>(
}
}
/// Str.fromFloat : Int -> Str
/// Str.fromFloat : Float * -> Str
pub fn str_from_float<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
scope: &Scope<'a, 'ctx>,
int_symbol: Symbol,
float: BasicValueEnum<'ctx>,
float_width: FloatWidth,
) -> BasicValueEnum<'ctx> {
let float = load_symbol(scope, &int_symbol);
call_str_bitcode_fn(env, &[float], &bitcode::STR_FROM_FLOAT[float_width])
}
call_str_bitcode_fn(env, &[float], bitcode::STR_FROM_FLOAT)
/// Dec.toStr : Dec -> Str
pub fn dec_to_str<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
dec: BasicValueEnum<'ctx>,
) -> BasicValueEnum<'ctx> {
let dec = dec.into_int_value();
let int_64 = env.context.i128_type().const_int(64, false);
let int_64_type = env.context.i64_type();
let dec_right_shift = env
.builder
.build_right_shift(dec, int_64, false, "dec_left_bits");
let right_bits = env.builder.build_int_cast(dec, int_64_type, "");
let left_bits = env.builder.build_int_cast(dec_right_shift, int_64_type, "");
call_str_bitcode_fn(
env,
&[right_bits.into(), left_bits.into()],
bitcode::DEC_TO_STR,
)
}
/// Str.equal : Str, Str -> Bool