Merge pull request #1903 from rtfeldman/fix-str-from-int

Implement Str.fromInt for any integer type
This commit is contained in:
Richard Feldman 2021-11-07 23:42:59 +00:00 committed by GitHub
commit 0a347456ef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 41 additions and 41 deletions

View file

@ -6673,39 +6673,6 @@ fn build_float_unary_op<'a, 'ctx, 'env>(
}
}
pub fn call_bitcode_int_fn<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
fn_name: &str,
args: &[BasicValueEnum<'ctx>],
int_width: IntWidth,
) -> BasicValueEnum<'ctx> {
match int_width {
IntWidth::U8 => call_bitcode_fn(env, args, &format!("{}_u8", fn_name)),
IntWidth::U16 => call_bitcode_fn(env, args, &format!("{}_u16", fn_name)),
IntWidth::U32 => call_bitcode_fn(env, args, &format!("{}_u32", fn_name)),
IntWidth::U64 => call_bitcode_fn(env, args, &format!("{}_u64", fn_name)),
IntWidth::U128 => call_bitcode_fn(env, args, &format!("{}_u128", fn_name)),
IntWidth::I8 => call_bitcode_fn(env, args, &format!("{}_i8", fn_name)),
IntWidth::I16 => call_bitcode_fn(env, args, &format!("{}_i16", fn_name)),
IntWidth::I32 => call_bitcode_fn(env, args, &format!("{}_i32", fn_name)),
IntWidth::I64 => call_bitcode_fn(env, args, &format!("{}_i64", fn_name)),
IntWidth::I128 => call_bitcode_fn(env, args, &format!("{}_i128", fn_name)),
}
}
pub fn call_bitcode_float_fn<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
fn_name: &str,
args: &[BasicValueEnum<'ctx>],
float_width: FloatWidth,
) -> BasicValueEnum<'ctx> {
match float_width {
FloatWidth::F32 => call_bitcode_fn(env, args, &format!("{}_f32", fn_name)),
FloatWidth::F64 => call_bitcode_fn(env, args, &format!("{}_f64", fn_name)),
FloatWidth::F128 => todo!("suport 128-bit floats"),
}
}
fn define_global_str_literal_ptr<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
message: &str,

View file

@ -11,7 +11,7 @@ use roc_builtins::bitcode;
use roc_module::symbol::Symbol;
use roc_mono::layout::{Builtin, Layout};
use super::build::load_symbol;
use super::build::{intwidth_from_builtin, load_symbol, load_symbol_and_layout};
pub static CHAR_LAYOUT: Layout = Layout::Builtin(Builtin::Int8);
@ -265,9 +265,33 @@ pub fn str_from_int<'a, 'ctx, 'env>(
scope: &Scope<'a, 'ctx>,
int_symbol: Symbol,
) -> BasicValueEnum<'ctx> {
let int = load_symbol(scope, &int_symbol);
let (int, int_layout) = load_symbol_and_layout(scope, &int_symbol);
call_bitcode_fn(env, &[int], bitcode::STR_FROM_INT)
match int_layout {
Layout::Builtin(builtin) => match builtin {
Builtin::Usize
| Builtin::Int128
| Builtin::Int64
| Builtin::Int32
| Builtin::Int16
| Builtin::Int8 => {
let intwidth = intwidth_from_builtin(*builtin, env.ptr_bytes);
call_bitcode_fn(env, &[int], &bitcode::STR_FROM_INT[intwidth])
}
_ => {
unreachable!(
"Compiler bug: tried to convert numeric on invalid builtin layout: ({:?})",
int_layout
);
}
},
_ => {
unreachable!(
"Compiler bug: tried to convert numeric on invalid layout: {:?}",
int_layout
);
}
}
}
/// Str.toUtf8 : Str -> List U8