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

@ -4,6 +4,7 @@ const utils = @import("utils.zig");
const ROC_BUILTINS = "roc_builtins";
const NUM = "num";
const STR = "str";
// Dec Module
const dec = @import("dec.zig");
@ -116,7 +117,6 @@ comptime {
exportStrFn(str.strConcatC, "concat");
exportStrFn(str.strJoinWithC, "joinWith");
exportStrFn(str.strNumberOfBytes, "number_of_bytes");
exportStrFn(str.strFromIntC, "from_int");
exportStrFn(str.strFromFloatC, "from_float");
exportStrFn(str.strEqual, "equal");
exportStrFn(str.strToUtf8C, "to_utf8");
@ -124,6 +124,10 @@ comptime {
exportStrFn(str.fromUtf8RangeC, "from_utf8_range");
exportStrFn(str.repeat, "repeat");
exportStrFn(str.strTrim, "trim");
inline for (INTEGERS) |T| {
str.exportFromInt(T, ROC_BUILTINS ++ "." ++ STR ++ ".from_int.");
}
}
// Utils

View file

@ -413,9 +413,14 @@ pub fn strNumberOfBytes(string: RocStr) callconv(.C) usize {
}
// Str.fromInt
pub fn strFromIntC(int: i64) callconv(.C) RocStr {
// prepare for having multiple integer types in the future
return @call(.{ .modifier = always_inline }, strFromIntHelp, .{ i64, int });
pub fn exportFromInt(comptime T: type, comptime name: []const u8) void {
comptime var f = struct {
fn func(int: T) callconv(.C) RocStr {
return @call(.{ .modifier = always_inline }, strFromIntHelp, .{ T, int });
}
}.func;
@export(f, .{ .name = name ++ @typeName(T), .linkage = .Strong });
}
fn strFromIntHelp(comptime T: type, int: T) RocStr {

View file

@ -135,7 +135,7 @@ pub const STR_STARTS_WITH: &str = "roc_builtins.str.starts_with";
pub const STR_STARTS_WITH_CODE_PT: &str = "roc_builtins.str.starts_with_code_point";
pub const STR_ENDS_WITH: &str = "roc_builtins.str.ends_with";
pub const STR_NUMBER_OF_BYTES: &str = "roc_builtins.str.number_of_bytes";
pub const STR_FROM_INT: &str = "roc_builtins.str.from_int";
pub const STR_FROM_INT: IntrinsicName = int_intrinsic!("roc_builtins.str.from_int");
pub const STR_FROM_FLOAT: &str = "roc_builtins.str.from_float";
pub const STR_EQUAL: &str = "roc_builtins.str.equal";
pub const STR_TO_UTF8: &str = "roc_builtins.str.to_utf8";

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