gen capacity operations in LLVM

This commit is contained in:
Richard Feldman 2022-07-13 11:59:12 -04:00
parent af14efea91
commit e744cd2a33
No known key found for this signature in database
GPG key ID: 7E4127D1E4241798
3 changed files with 71 additions and 5 deletions

View file

@ -3,14 +3,15 @@ use crate::llvm::bitcode::{
call_str_bitcode_fn, call_void_bitcode_fn,
};
use crate::llvm::build_dict::{
self, dict_contains, dict_difference, dict_empty, dict_get, dict_insert, dict_intersection,
dict_keys, dict_len, dict_remove, dict_union, dict_values, dict_walk, set_from_list,
self, dict_capacity, dict_contains, dict_difference, dict_empty, dict_get, dict_insert,
dict_intersection, dict_keys, dict_len, dict_remove, dict_union, dict_values, dict_walk,
set_capacity, set_from_list,
};
use crate::llvm::build_hash::generic_hash;
use crate::llvm::build_list::{
self, allocate_list, empty_polymorphic_list, list_append_unsafe, list_concat, list_drop_at,
list_get_unsafe, list_len, list_map, list_map2, list_map3, list_map4, list_prepend,
list_replace_unsafe, list_reserve, list_sort_with, list_sublist, list_swap,
self, allocate_list, empty_polymorphic_list, list_append_unsafe, list_capacity, list_concat,
list_drop_at, list_get_unsafe, list_len, list_map, list_map2, list_map3, list_map4,
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};
@ -5592,6 +5593,13 @@ fn run_low_level<'a, 'ctx, 'env>(
let string = load_symbol(scope, &args[0]);
call_bitcode_fn(env, &[string], bitcode::STR_COUNT_UTF8_BYTES)
}
StrGetCapacity => {
// Str.capacity : Str -> Nat
debug_assert_eq!(args.len(), 1);
let string = load_symbol(scope, &args[0]);
call_bitcode_fn(env, &[string], bitcode::STR_CAPACITY)
}
StrSubstringUnsafe => {
// Str.substringUnsafe : Str, Nat, Nat -> Str
debug_assert_eq!(args.len(), 3);
@ -5646,6 +5654,14 @@ fn run_low_level<'a, 'ctx, 'env>(
list_len(env.builder, arg.into_struct_value()).into()
}
ListGetCapacity => {
// List.capacity : List * -> Nat
debug_assert_eq!(args.len(), 1);
let arg = load_symbol(scope, &args[0]);
list_capacity(env.builder, arg.into_struct_value()).into()
}
ListWithCapacity => {
// List.withCapacity : Nat -> List a
debug_assert_eq!(args.len(), 1);
@ -6139,6 +6155,14 @@ fn run_low_level<'a, 'ctx, 'env>(
debug_assert_eq!(args.len(), 1);
dict_len(env, scope, args[0])
}
DictGetCapacity => {
// Dict.capacity : Dict * * -> Nat
debug_assert_eq!(args.len(), 1);
let arg = load_symbol(scope, &args[0]);
dict_capacity(env.builder, arg.into_struct_value()).into()
}
DictEmpty => {
debug_assert_eq!(args.len(), 0);
dict_empty(env)
@ -6236,6 +6260,14 @@ fn run_low_level<'a, 'ctx, 'env>(
set
}
SetGetCapacity => {
// Set.capacity : Set * -> Nat
debug_assert_eq!(args.len(), 1);
let arg = load_symbol(scope, &args[0]);
set_capacity(env.builder, arg.into_struct_value()).into()
}
ListMap | ListMap2 | ListMap3 | ListMap4 | ListSortWith | DictWalk => {
unreachable!("these are higher order, and are handled elsewhere")

View file

@ -10,6 +10,7 @@ use crate::llvm::build_list::{layout_width, pass_as_opaque};
use crate::llvm::convert::{basic_type_from_layout, zig_dict_type};
use crate::llvm::refcounting::Mode;
use inkwell::attributes::{Attribute, AttributeLoc};
use inkwell::builder::Builder;
use inkwell::context::Context;
use inkwell::types::BasicType;
use inkwell::values::{BasicValue, BasicValueEnum, FunctionValue, IntValue, StructValue};
@ -697,6 +698,28 @@ pub fn dict_values<'a, 'ctx, 'env>(
)
}
/// Dict.capacity : Dict * * -> Nat
pub fn dict_capacity<'ctx>(
builder: &Builder<'ctx>,
wrapper_struct: StructValue<'ctx>,
) -> IntValue<'ctx> {
builder
.build_extract_value(wrapper_struct, Builtin::WRAPPER_CAPACITY, "dict_capacity")
.unwrap()
.into_int_value()
}
/// Set.capacity : Set * -> Nat
pub fn set_capacity<'ctx>(
builder: &Builder<'ctx>,
wrapper_struct: StructValue<'ctx>,
) -> IntValue<'ctx> {
builder
.build_extract_value(wrapper_struct, Builtin::WRAPPER_CAPACITY, "set_capacity")
.unwrap()
.into_int_value()
}
#[allow(clippy::too_many_arguments)]
pub fn set_from_list<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,

View file

@ -362,6 +362,17 @@ pub fn list_len<'ctx>(
.into_int_value()
}
/// List.capacity : List * -> Nat
pub fn list_capacity<'ctx>(
builder: &Builder<'ctx>,
wrapper_struct: StructValue<'ctx>,
) -> IntValue<'ctx> {
builder
.build_extract_value(wrapper_struct, Builtin::WRAPPER_CAPACITY, "list_capacity")
.unwrap()
.into_int_value()
}
/// List.sortWith : List a, (a, a -> Ordering) -> List a
pub fn list_sort_with<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,