mirror of
https://github.com/odin-lang/Odin.git
synced 2026-04-06 06:38:20 +00:00
Merge pull request #6466 from jakubtomsu/bit-field-intrin
Bit field intrinsics and fixes
This commit is contained in:
@@ -205,6 +205,9 @@ type_bit_set_underlying_type :: proc($T: typeid) -> typeid where type_is_bit_set
|
||||
type_has_field :: proc($T: typeid, $name: string) -> bool ---
|
||||
type_field_type :: proc($T: typeid, $name: string) -> typeid ---
|
||||
|
||||
type_field_bit_size :: proc($T: typeid, $name: string) -> int where type_is_bit_field(T) ---
|
||||
type_field_bit_offset :: proc($T: typeid, $name: string) -> int where type_is_bit_field(T) ---
|
||||
|
||||
type_proc_parameter_count :: proc($T: typeid) -> int where type_is_proc(T) ---
|
||||
type_proc_return_count :: proc($T: typeid) -> int where type_is_proc(T) ---
|
||||
|
||||
|
||||
@@ -2821,9 +2821,9 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As
|
||||
}
|
||||
|
||||
|
||||
if (is_type_array(type)) {
|
||||
if (is_type_array(type) || is_type_bit_field(type)) {
|
||||
gbString t = type_to_string(type);
|
||||
error(field_arg, "Invalid a struct type for '%.*s', got '%s'", LIT(builtin_name), t);
|
||||
error(field_arg, "Expected a struct type for '%.*s', got '%s'", LIT(builtin_name), t);
|
||||
gb_string_free(t);
|
||||
return false;
|
||||
}
|
||||
@@ -6880,6 +6880,68 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case BuiltinProc_type_field_bit_offset:
|
||||
case BuiltinProc_type_field_bit_size:
|
||||
{
|
||||
Operand op = {};
|
||||
Type *bt = check_type(c, ce->args[0]);
|
||||
Type *type = base_type(bt);
|
||||
if (type == nullptr || type == t_invalid) {
|
||||
error(ce->args[0], "Expected a type for '%.*s'", LIT(builtin_name));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!is_type_bit_field(type)) {
|
||||
error(operand->expr, "Expected a bit field type for '%.*s'", LIT(builtin_name));
|
||||
operand->mode = Addressing_Invalid;
|
||||
operand->type = t_invalid;
|
||||
return false;
|
||||
}
|
||||
|
||||
Operand x = {};
|
||||
check_expr(c, &x, ce->args[1]);
|
||||
|
||||
if (!is_type_string(x.type) || x.mode != Addressing_Constant || x.value.kind != ExactValue_String) {
|
||||
error(ce->args[1], "Expected a constant string for field argument");
|
||||
return false;
|
||||
}
|
||||
|
||||
InternedString field_name = string_interner_insert(x.value.value_string);
|
||||
|
||||
i64 bit_offset = 0;
|
||||
i64 bit_size = 0;
|
||||
for_array(i, type->BitField.fields) {
|
||||
Entity *f = type->BitField.fields[i];
|
||||
if (f->kind != Entity_Variable || (f->flags & EntityFlag_Field) == 0) {
|
||||
continue;
|
||||
}
|
||||
auto str = entity_interned_name(f);
|
||||
if (field_name == str) {
|
||||
bit_offset = type->BitField.bit_offsets[i];
|
||||
bit_size = type->BitField.bit_sizes[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
i64 value = 0;
|
||||
switch (id) {
|
||||
case BuiltinProc_type_field_bit_offset:
|
||||
value = bit_offset;
|
||||
break;
|
||||
case BuiltinProc_type_field_bit_size:
|
||||
value = bit_size;
|
||||
break;
|
||||
default:
|
||||
GB_ASSERT(false);
|
||||
}
|
||||
|
||||
operand->mode = Addressing_Constant;
|
||||
operand->type = t_untyped_integer;
|
||||
operand->value = exact_value_i64(value);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case BuiltinProc_type_is_specialization_of:
|
||||
{
|
||||
|
||||
@@ -314,6 +314,9 @@ BuiltinProc__type_simple_boolean_end,
|
||||
|
||||
BuiltinProc_type_has_field,
|
||||
BuiltinProc_type_field_type,
|
||||
|
||||
BuiltinProc_type_field_bit_offset,
|
||||
BuiltinProc_type_field_bit_size,
|
||||
|
||||
BuiltinProc_type_is_specialization_of,
|
||||
|
||||
@@ -694,6 +697,9 @@ gb_global BuiltinProc builtin_procs[BuiltinProc_COUNT] = {
|
||||
|
||||
{STR_LIT("type_has_field"), 2, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
||||
{STR_LIT("type_field_type"), 2, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
||||
|
||||
{STR_LIT("type_field_bit_offset"), 2, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
||||
{STR_LIT("type_field_bit_size"), 2, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
||||
|
||||
{STR_LIT("type_is_specialization_of"), 2, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
||||
|
||||
|
||||
Reference in New Issue
Block a user