Glue code working

This commit is contained in:
Joshua Hoeflich 2021-08-08 11:06:10 -05:00
parent 0ca85a54fe
commit 4231b340ee
11 changed files with 251 additions and 2 deletions

View file

@ -14,7 +14,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_join_with, str_number_of_bytes, str_split, str_starts_with,
str_from_utf8, str_from_utf8_range, str_join_with, str_number_of_bytes, str_split, str_starts_with,
str_starts_with_code_point, str_to_bytes,
};
use crate::llvm::compare::{generic_eq, generic_neq};
@ -4444,6 +4444,15 @@ fn run_low_level<'a, 'ctx, 'env>(
str_from_utf8(env, parent, original_wrapper)
}
StrFromUtf8Range => {
// Str.fromUtf8 : List U8 -> Result Str Utf8Problem
debug_assert_eq!(args.len(), 2);
let list_wrapper = load_symbol(scope, &args[0]).into_struct_value();
let count_and_start = load_symbol(scope, &args[1]).into_struct_value();
str_from_utf8_range(env, parent, list_wrapper, count_and_start)
}
StrToBytes => {
// Str.fromInt : Str -> List U8
debug_assert_eq!(args.len(), 1);

View file

@ -250,6 +250,64 @@ pub fn str_to_bytes<'a, 'ctx, 'env>(
call_bitcode_fn_returns_list(env, &[string], bitcode::STR_TO_BYTES)
}
/// Str.fromUtf8 : List U8, { count : Nat, start : Nat } -> { a : Bool, b : Str, c : Nat, d : I8 }
pub fn str_from_utf8_range<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
_parent: FunctionValue<'ctx>,
list_wrapper: StructValue<'ctx>,
count_and_start: StructValue<'ctx>,
) -> BasicValueEnum<'ctx> {
let builder = env.builder;
let ctx = env.context;
let result_type = env.module.get_struct_type("str.FromUtf8Result").unwrap();
let result_ptr = builder.build_alloca(result_type, "alloca_utf8_validate_bytes_result");
call_void_bitcode_fn(
env,
&[
complex_bitcast(
env.builder,
list_wrapper.into(),
env.context.i128_type().into(),
"to_i128",
),
// TODO: This won't work for 32 bit targets!
complex_bitcast(
env.builder,
count_and_start.into(),
env.context.i128_type().into(),
"to_i128",
),
result_ptr.into(),
],
bitcode::STR_FROM_UTF8_RANGE,
);
let record_type = env.context.struct_type(
&[
env.ptr_int().into(),
super::convert::zig_str_type(env).into(),
env.context.bool_type().into(),
ctx.i8_type().into(),
],
false,
);
let result_ptr_cast = env
.builder
.build_bitcast(
result_ptr,
record_type.ptr_type(AddressSpace::Generic),
"to_unnamed",
)
.into_pointer_value();
builder.build_load(result_ptr_cast, "load_utf8_validate_bytes_result")
}
/// Str.fromUtf8 : List U8 -> { a : Bool, b : Str, c : Nat, d : I8 }
pub fn str_from_utf8<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,