aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorgingerBill <gingerBill@users.noreply.github.com>2020-12-06 00:49:48 +0000
committerGitHub <noreply@github.com>2020-12-06 00:49:48 +0000
commitf0683c910231513db9adab83f7c2fca9dd8d2613 (patch)
tree2539634b5b71caf5148d8927c9298ba20bad5246 /src
parent54fbdabc380905a925ab5e922749fa2b1ccb2621 (diff)
parentca4657fd31b9efc7ab52f7e1b6f4145d5ed28fb7 (diff)
Merge branch 'master' into parser-experiments
Diffstat (limited to 'src')
-rw-r--r--src/array.cpp89
-rw-r--r--src/build_settings.cpp51
-rw-r--r--src/check_decl.cpp13
-rw-r--r--src/check_expr.cpp499
-rw-r--r--src/check_stmt.cpp35
-rw-r--r--src/check_type.cpp280
-rw-r--r--src/checker.cpp267
-rw-r--r--src/checker.hpp32
-rw-r--r--src/checker_builtin_procs.hpp6
-rw-r--r--src/common.cpp141
-rw-r--r--src/docs.cpp356
-rw-r--r--src/entity.cpp7
-rw-r--r--src/exact_value.cpp110
-rw-r--r--src/gb/gb.h105
-rw-r--r--src/ir.cpp520
-rw-r--r--src/ir_print.cpp65
-rw-r--r--src/llvm_abi.cpp959
-rw-r--r--src/llvm_backend.cpp2035
-rw-r--r--src/llvm_backend.hpp33
-rw-r--r--src/main.cpp561
-rw-r--r--src/parser.cpp284
-rw-r--r--src/parser.hpp81
-rw-r--r--src/ptr_set.cpp137
-rw-r--r--src/string.cpp1
-rw-r--r--src/tokenizer.cpp2
-rw-r--r--src/types.cpp292
26 files changed, 5297 insertions, 1664 deletions
diff --git a/src/array.cpp b/src/array.cpp
index 6fe54b847..dc52eeb8d 100644
--- a/src/array.cpp
+++ b/src/array.cpp
@@ -43,11 +43,96 @@ template <typename T> void array_set_capacity (Array<T> *array, isize capac
template <typename T> Array<T> array_slice (Array<T> const &array, isize lo, isize hi);
template <typename T> Array<T> array_clone (gbAllocator const &a, Array<T> const &array);
-
-
template <typename T> void array_ordered_remove (Array<T> *array, isize index);
template <typename T> void array_unordered_remove(Array<T> *array, isize index);
+template <typename T> void array_copy(Array<T> *array, Array<T> const &data, isize offset);
+template <typename T> void array_copy(Array<T> *array, Array<T> const &data, isize offset, isize count);
+
+template <typename T> T *array_end_ptr(Array<T> *array);
+
+
+template <typename T>
+struct Slice {
+ T *data;
+ isize count;
+
+ T &operator[](isize index) {
+ #if !defined(NO_ARRAY_BOUNDS_CHECK)
+ GB_ASSERT_MSG(0 <= index && index < count, "Index %td is out of bounds ranges 0..<%td", index, count);
+ #endif
+ return data[index];
+ }
+
+ T const &operator[](isize index) const {
+ #if !defined(NO_ARRAY_BOUNDS_CHECK)
+ GB_ASSERT_MSG(0 <= index && index < count, "Index %td is out of bounds ranges 0..<%td", index, count);
+ #endif
+ return data[index];
+ }
+};
+
+template <typename T> Slice<T> slice_from_array(Array<T> const &a);
+
+
+
+template <typename T>
+Slice<T> slice_make(gbAllocator const &allocator, isize count) {
+ Slice<T> s = {};
+ s.data = gb_alloc_array(allocator, T, count);
+ s.count = count;
+ return s;
+}
+
+
+template <typename T>
+Slice<T> slice_from_array(Array<T> const &a) {
+ return {a.data, a.count};
+}
+template <typename T>
+Slice<T> slice_clone(gbAllocator const &allocator, Slice<T> const &a) {
+ T *data = cast(T *)gb_alloc_copy_align(allocator, a.data, a.count*gb_size_of(T), gb_align_of(T));
+ return {data, a.count};
+}
+
+template <typename T>
+Slice<T> slice_clone_from_array(gbAllocator const &allocator, Array<T> const &a) {
+ auto c = array_clone(allocator, a);
+ return {c.data, c.count};
+}
+
+
+template <typename T>
+void slice_copy(Slice<T> *slice, Slice<T> const &data, isize offset) {
+ gb_memmove(slice->data+offset, data.data, gb_size_of(T)*data.count);
+}
+template <typename T>
+void slice_copy(Slice<T> *slice, Slice<T> const &data, isize offset, isize count) {
+ gb_memmove(slice->data+offset, data.data, gb_size_of(T)*gb_min(data.count, count));
+}
+
+
+
+template <typename T>
+void slice_ordered_remove(Slice<T> *array, isize index) {
+ GB_ASSERT(0 <= index && index < array->count);
+
+ isize bytes = gb_size_of(T) * (array->count-(index+1));
+ gb_memmove(array->data+index, array->data+index+1, bytes);
+ array->count -= 1;
+}
+
+template <typename T>
+void slice_unordered_remove(Slice<T> *array, isize index) {
+ GB_ASSERT(0 <= index && index < array->count);
+
+ isize n = array->count-1;
+ if (index != n) {
+ gb_memmove(array->data+index, array->data+n, gb_size_of(T));
+ }
+ array->count -= 1;
+}
+
template <typename T>
void array_copy(Array<T> *array, Array<T> const &data, isize offset) {
diff --git a/src/build_settings.cpp b/src/build_settings.cpp
index 931dcf88e..b4c414f03 100644
--- a/src/build_settings.cpp
+++ b/src/build_settings.cpp
@@ -104,6 +104,37 @@ enum BuildModeKind {
BuildMode_Assembly,
};
+enum CommandKind : u32 {
+ Command_run = 1<<0,
+ Command_build = 1<<1,
+ Command_check = 1<<3,
+ Command_query = 1<<4,
+ Command_doc = 1<<5,
+ Command_version = 1<<6,
+ Command_test = 1<<7,
+
+ Command__does_check = Command_run|Command_build|Command_check|Command_query|Command_doc|Command_test,
+ Command__does_build = Command_run|Command_build|Command_test,
+ Command_all = ~(u32)0,
+};
+
+char const *odin_command_strings[32] = {
+ "run",
+ "build",
+ "check",
+ "query",
+ "doc",
+ "version",
+};
+
+
+
+enum CmdDocFlag : u32 {
+ CmdDocFlag_Short = 1<<0,
+ CmdDocFlag_AllPackages = 1<<1,
+};
+
+
// This stores the information for the specify architecture of this build
struct BuildContext {
@@ -124,6 +155,7 @@ struct BuildContext {
i64 word_size; // Size of a pointer, must be >= 4
i64 max_align; // max alignment, must be >= 1 (and typically >= word_size)
+ CommandKind command_kind;
String command;
TargetMetrics metrics;
@@ -143,6 +175,8 @@ struct BuildContext {
bool generate_docs;
i32 optimization_level;
bool show_timings;
+ bool show_unused;
+ bool show_unused_with_location;
bool show_more_timings;
bool show_system_calls;
bool keep_temp_files;
@@ -151,6 +185,7 @@ struct BuildContext {
bool no_dynamic_literals;
bool no_output_files;
bool no_crt;
+ bool no_entry_point;
bool use_lld;
bool vet;
bool cross_compiling;
@@ -165,6 +200,9 @@ struct BuildContext {
bool ignore_microsoft_magic;
bool linker_map_file;
+ u32 cmd_doc_flags;
+ Array<String> extra_packages;
+
QueryDataSetSettings query_data_set_settings;
gbAffinity affinity;
@@ -297,6 +335,19 @@ bool is_excluded_target_filename(String name) {
String original_name = name;
name = remove_extension_from_path(name);
+ if (string_starts_with(name, str_lit("."))) {
+ // Ignore .*.odin files
+ return true;
+ }
+
+ String test_suffix = str_lit("_test");
+ if (build_context.command_kind != Command_test) {
+ if (string_ends_with(name, test_suffix) && name != test_suffix) {
+ // Ignore *_test.odin files
+ return true;
+ }
+ }
+
String str1 = {};
String str2 = {};
isize n = 0;
diff --git a/src/check_decl.cpp b/src/check_decl.cpp
index 1aafa6e1c..5234955fb 100644
--- a/src/check_decl.cpp
+++ b/src/check_decl.cpp
@@ -113,7 +113,7 @@ Type *check_init_variable(CheckerContext *ctx, Entity *e, Operand *operand, Stri
return e->type;
}
-void check_init_variables(CheckerContext *ctx, Entity **lhs, isize lhs_count, Array<Ast *> const &inits, String context_name) {
+void check_init_variables(CheckerContext *ctx, Entity **lhs, isize lhs_count, Slice<Ast *> const &inits, String context_name) {
if ((lhs == nullptr || lhs_count == 0) && inits.count == 0) {
return;
}
@@ -121,8 +121,7 @@ void check_init_variables(CheckerContext *ctx, Entity **lhs, isize lhs_count, Ar
// NOTE(bill): If there is a bad syntax error, rhs > lhs which would mean there would need to be
// an extra allocation
- auto operands = array_make<Operand>(ctx->allocator, 0, 2*lhs_count);
- defer (array_free(&operands));
+ auto operands = array_make<Operand>(temporary_allocator(), 0, 2*lhs_count);
check_unpack_arguments(ctx, lhs, lhs_count, &operands, inits, true, false);
isize rhs_count = operands.count;
@@ -317,7 +316,6 @@ void check_type_decl(CheckerContext *ctx, Entity *e, Ast *init_expr, Type *def)
break;
default:
error(e->token, "Only struct types can have custom atom operations");
- gb_free(heap_allocator(), ac.atom_op_table);
break;
}
}
@@ -638,7 +636,7 @@ String handle_link_name(CheckerContext *ctx, Token token, String link_name, Stri
error(token, "'link_name' and 'link_prefix' cannot be used together");
} else {
isize len = link_prefix.len + token.string.len;
- u8 *name = gb_alloc_array(ctx->allocator, u8, len+1);
+ u8 *name = gb_alloc_array(permanent_allocator(), u8, len+1);
gb_memmove(name, &link_prefix[0], link_prefix.len);
gb_memmove(name+link_prefix.len, &token.string[0], token.string.len);
name[len] = 0;
@@ -975,7 +973,7 @@ void check_proc_group_decl(CheckerContext *ctx, Entity *pg_entity, DeclInfo *d)
ast_node(pg, ProcGroup, d->init_expr);
- pge->entities = array_make<Entity*>(ctx->allocator, 0, pg->args.count);
+ pge->entities = array_make<Entity*>(permanent_allocator(), 0, pg->args.count);
// NOTE(bill): This must be set here to prevent cycles in checking if someone
// places the entity within itself
@@ -1009,11 +1007,10 @@ void check_proc_group_decl(CheckerContext *ctx, Entity *pg_entity, DeclInfo *d)
continue;
}
- if (ptr_set_exists(&entity_set, e)) {
+ if (ptr_set_update(&entity_set, e)) {
error(arg, "Previous use of `%.*s` in procedure group", LIT(e->token.string));
continue;
}
- ptr_set_add(&entity_set, e);
array_add(&pge->entities, e);
}
diff --git a/src/check_expr.cpp b/src/check_expr.cpp
index 02b54c80a..ea460ce09 100644
--- a/src/check_expr.cpp
+++ b/src/check_expr.cpp
@@ -73,7 +73,7 @@ void update_expr_type (CheckerContext *c, Ast *e, Type *type,
bool check_is_terminating (Ast *node, String const &label);
bool check_has_break (Ast *stmt, String const &label, bool implicit);
void check_stmt (CheckerContext *c, Ast *node, u32 flags);
-void check_stmt_list (CheckerContext *c, Array<Ast *> const &stmts, u32 flags);
+void check_stmt_list (CheckerContext *c, Slice<Ast *> const &stmts, u32 flags);
void check_init_constant (CheckerContext *c, Entity *e, Operand *operand);
bool check_representable_as_constant(CheckerContext *c, ExactValue in_value, Type *type, ExactValue *out_value);
bool check_procedure_type (CheckerContext *c, Type *type, Ast *proc_type_node, Array<Operand> *operands = nullptr);
@@ -89,9 +89,9 @@ Type * check_init_variable (CheckerContext *c, Entity *e, Operand *
Type *type_to_abi_compat_param_type(gbAllocator a, Type *original_type, ProcCallingConvention cc);
Type *type_to_abi_compat_result_type(gbAllocator a, Type *original_type, ProcCallingConvention cc);
bool abi_compat_return_by_pointer(gbAllocator a, ProcCallingConvention cc, Type *abi_return_type);
-void set_procedure_abi_types(gbAllocator a, Type *type);
+void set_procedure_abi_types(Type *type);
void check_assignment_error_suggestion(CheckerContext *c, Operand *o, Type *type);
-
+void add_map_key_type_dependencies(CheckerContext *ctx, Type *key);
Type *make_soa_struct_slice(CheckerContext *ctx, Ast *array_typ_expr, Ast *elem_expr, Type *elem);
Type *make_soa_struct_dynamic_array(CheckerContext *ctx, Ast *array_typ_expr, Ast *elem_expr, Type *elem);
@@ -133,7 +133,7 @@ void error_operand_no_value(Operand *o) {
}
-void check_scope_decls(CheckerContext *c, Array<Ast *> const &nodes, isize reserve_size) {
+void check_scope_decls(CheckerContext *c, Slice<Ast *> const &nodes, isize reserve_size) {
Scope *s = c->scope;
check_collect_entities(c, nodes);
@@ -267,7 +267,7 @@ bool find_or_generate_polymorphic_procedure(CheckerContext *c, Entity *base_enti
CheckerContext nctx = *c;
- Scope *scope = create_scope(base_entity->scope, a);
+ Scope *scope = create_scope(base_entity->scope);
scope->flags |= ScopeFlag_Proc;
nctx.scope = scope;
nctx.allow_polymorphic_types = true;
@@ -366,7 +366,7 @@ bool find_or_generate_polymorphic_procedure(CheckerContext *c, Entity *base_enti
u64 tags = base_entity->Procedure.tags;
Ast *ident = clone_ast(base_entity->identifier);
Token token = ident->Ident.token;
- DeclInfo *d = make_decl_info(nctx.allocator, scope, old_decl->parent);
+ DeclInfo *d = make_decl_info(scope, old_decl->parent);
d->gen_proc_type = final_proc_type;
d->type_expr = pl->type;
d->proc_lit = proc_lit;
@@ -1012,7 +1012,7 @@ bool is_polymorphic_type_assignable(CheckerContext *c, Type *poly, Type *source,
}
if (modify_type) {
- set_procedure_abi_types(c->allocator, source);
+ set_procedure_abi_types(source);
}
return true;
@@ -1075,8 +1075,10 @@ Entity *check_ident(CheckerContext *c, Operand *o, Ast *n, Type *named_type, Typ
if (e->parent_proc_decl != nullptr &&
e->parent_proc_decl != c->curr_proc_decl) {
if (e->kind == Entity_Variable) {
- error(n, "Nested procedures do not capture its parent's variables: %.*s", LIT(name));
- return nullptr;
+ if ((e->flags & EntityFlag_Static) == 0) {
+ error(n, "Nested procedures do not capture its parent's variables: %.*s", LIT(name));
+ return nullptr;
+ }
} else if (e->kind == Entity_Label) {
error(n, "Nested procedures do not capture its parent's labels: %.*s", LIT(name));
return nullptr;
@@ -1834,10 +1836,6 @@ void check_comparison(CheckerContext *c, Operand *x, Operand *y, TokenKind op) {
gbString err_str = nullptr;
- defer (if (err_str != nullptr) {
- gb_string_free(err_str);
- });
-
if (check_is_assignable_to(c, x, y->type) ||
check_is_assignable_to(c, y, x->type)) {
Type *err_type = x->type;
@@ -1867,8 +1865,8 @@ void check_comparison(CheckerContext *c, Operand *x, Operand *y, TokenKind op) {
}
gbString type_string = type_to_string(err_type);
defer (gb_string_free(type_string));
- err_str = gb_string_make(c->allocator,
- gb_bprintf("operator '%.*s' not defined for type '%s'", LIT(token_strings[op]), type_string));
+ err_str = gb_string_make(temporary_allocator(),
+ gb_bprintf("operator '%.*s' not defined for type '%s'", LIT(token_strings[op]), type_string));
}
} else {
gbString xt, yt;
@@ -1882,8 +1880,7 @@ void check_comparison(CheckerContext *c, Operand *x, Operand *y, TokenKind op) {
} else {
yt = type_to_string(y->type);
}
- err_str = gb_string_make(c->allocator,
- gb_bprintf("mismatched types '%s' and '%s'", xt, yt));
+ err_str = gb_string_make(temporary_allocator(), gb_bprintf("mismatched types '%s' and '%s'", xt, yt));
gb_string_free(yt);
gb_string_free(xt);
}
@@ -2166,6 +2163,17 @@ bool check_is_castable_to(CheckerContext *c, Operand *operand, Type *y) {
return true;
}
+ if (is_constant && is_type_untyped(src) && is_type_string(src)) {
+ if (is_type_u8_array(dst)) {
+ String s = operand->value.value_string;
+ return s.len == dst->Array.count;
+ }
+ if (is_type_rune_array(dst)) {
+ String s = operand->value.value_string;
+ return gb_utf8_strnlen(s.text, s.len) == dst->Array.count;
+ }
+ }
+
if (dst->kind == Type_Array && src->kind == Type_Array) {
if (are_types_identical(dst->Array.elem, src->Array.elem)) {
@@ -2960,6 +2968,19 @@ void convert_to_typed(CheckerContext *c, Operand *operand, Type *target_type) {
if (check_is_assignable_to(c, operand, elem)) {
operand->mode = Addressing_Value;
} else {
+ if (operand->value.kind == ExactValue_String) {
+ String s = operand->value.value_string;
+ if (is_type_u8_array(t)) {
+ if (s.len == t->Array.count) {
+ break;
+ }
+ } else if (is_type_rune_array(t)) {
+ isize rune_count = gb_utf8_strnlen(s.text, s.len);
+ if (rune_count == t->Array.count) {
+ break;
+ }
+ }
+ }
operand->mode = Addressing_Invalid;
convert_untyped_error(c, operand, target_type);
return;
@@ -2979,8 +3000,7 @@ void convert_to_typed(CheckerContext *c, Operand *operand, Type *target_type) {
case Type_Union:
if (!is_operand_nil(*operand) && !is_operand_undef(*operand)) {
isize count = t->Union.variants.count;
- ValidIndexAndScore *valids = gb_alloc_array(c->allocator, ValidIndexAndScore, count);
- defer (gb_free(c->allocator, valids));
+ ValidIndexAndScore *valids = gb_alloc_array(temporary_allocator(), ValidIndexAndScore, count);
isize valid_count = 0;
isize first_success_index = -1;
for_array(i, t->Union.variants) {
@@ -3389,7 +3409,7 @@ ExactValue get_constant_field(CheckerContext *c, Operand const *operand, Selecti
return value;
} else if (value.kind == ExactValue_Quaternion) {
// @QuaternionLayout
- Quaternion256 q = value.value_quaternion;
+ Quaternion256 q = *value.value_quaternion;
GB_ASSERT(sel.index.count == 1);
switch (sel.index[0]) {
@@ -3414,7 +3434,7 @@ ExactValue get_constant_field(CheckerContext *c, Operand const *operand, Selecti
return empty_exact_value;
} else if (value.kind == ExactValue_Complex) {
// @QuaternionLayout
- Complex128 c = value.value_complex;
+ Complex128 c = *value.value_complex;
GB_ASSERT(sel.index.count == 1);
switch (sel.index[0]) {
@@ -3443,6 +3463,13 @@ Entity *check_selector(CheckerContext *c, Operand *operand, Ast *node, Type *typ
Entity *entity = nullptr;
Selection sel = {}; // NOTE(bill): Not used if it's an import name
+ if (!c->allow_arrow_right_selector_expr && se->token.kind == Token_ArrowRight) {
+ error(node, "Illegal use of -> selector shorthand outside of a call");
+ operand->mode = Addressing_Invalid;
+ operand->expr = node;
+ return nullptr;
+ }
+
operand->expr = node;
Ast *op_expr = se->expr;
@@ -4343,7 +4370,6 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32
add_type_info_type(c, t);
- t = base_type(t);
if (o.mode != Addressing_Type) {
error(expr, "Expected a type for 'typeid_of'");
return false;
@@ -4351,6 +4377,7 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32
operand->mode = Addressing_Value;
operand->type = t_typeid;
+ operand->value = exact_value_typeid(t);
break;
}
@@ -4702,8 +4729,8 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32
if (is_type_complex(x->type)) {
if (x->mode == Addressing_Constant) {
ExactValue v = exact_value_to_complex(x->value);
- f64 r = v.value_complex.real;
- f64 i = -v.value_complex.imag;
+ f64 r = v.value_complex->real;
+ f64 i = -v.value_complex->imag;
x->value = exact_value_complex(r, i);
x->mode = Addressing_Constant;
} else {
@@ -4712,10 +4739,10 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32
} else if (is_type_quaternion(x->type)) {
if (x->mode == Addressing_Constant) {
ExactValue v = exact_value_to_quaternion(x->value);
- f64 r = v.value_quaternion.real;
- f64 i = -v.value_quaternion.imag;
- f64 j = -v.value_quaternion.jmag;
- f64 k = -v.value_quaternion.kmag;
+ f64 r = +v.value_quaternion->real;
+ f64 i = -v.value_quaternion->imag;
+ f64 j = -v.value_quaternion->jmag;
+ f64 k = -v.value_quaternion->kmag;
x->value = exact_value_quaternion(r, i, j, k);
x->mode = Addressing_Constant;
} else {
@@ -4739,7 +4766,7 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32
gb_string_free(type_str);
return false;
}
- gbAllocator a = c->allocator;
+ gbAllocator a = permanent_allocator();
Type *tuple = alloc_type_tuple();
@@ -5132,8 +5159,8 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32
operand->value.value_float = gb_abs(operand->value.value_float);
break;
case ExactValue_Complex: {
- f64 r = operand->value.value_complex.real;
- f64 i = operand->value.value_complex.imag;
+ f64 r = operand->value.value_complex->real;
+ f64 i = operand->value.value_complex->imag;
operand->value = exact_value_float(gb_sqrt(r*r + i*i));
break;
@@ -5356,7 +5383,7 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32
soa_struct->Struct.soa_elem = elem;
soa_struct->Struct.soa_count = count;
- scope = create_scope(c->scope, c->allocator);
+ scope = create_scope(c->scope);
soa_struct->Struct.scope = scope;
String params_xyzw[4] = {
@@ -5389,7 +5416,7 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32
soa_struct->Struct.soa_elem = elem;
soa_struct->Struct.soa_count = count;
- scope = create_scope(old_struct->Struct.scope->parent, c->allocator);
+ scope = create_scope(old_struct->Struct.scope->parent);
soa_struct->Struct.scope = scope;
for_array(i, old_struct->Struct.fields) {
@@ -6035,6 +6062,50 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32
break;
}
break;
+
+ case BuiltinProc_type_equal_proc:
+ {
+ 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_comparable(type)) {
+ gbString t = type_to_string(type);
+ error(ce->args[0], "Expected a comparable type for '%.*s', got %s", LIT(builtin_name), t);
+ gb_string_free(t);
+ return false;
+ }
+
+ operand->mode = Addressing_Value;
+ operand->type = t_equal_proc;
+ break;
+ }
+
+ case BuiltinProc_type_hasher_proc:
+ {
+ 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_valid_for_keys(type)) {
+ gbString t = type_to_string(type);
+ error(ce->args[0], "Expected a valid type for map keys for '%.*s', got %s", LIT(builtin_name), t);
+ gb_string_free(t);
+ return false;
+ }
+
+ add_map_key_type_dependencies(c, type);
+
+ operand->mode = Addressing_Value;
+ operand->type = t_hasher_proc;
+ break;
+ }
}
return true;
@@ -6061,7 +6132,7 @@ isize add_dependencies_from_unpacking(CheckerContext *c, Entity **lhs, isize lhs
}
-bool check_assignment_arguments(CheckerContext *ctx, Array<Operand> const &lhs, Array<Operand> *operands, Array<Ast *> const &rhs) {
+bool check_assignment_arguments(CheckerContext *ctx, Array<Operand> const &lhs, Array<Operand> *operands, Slice<Ast *> const &rhs) {
bool optional_ok = false;
isize tuple_index = 0;
for_array(i, rhs) {
@@ -6145,7 +6216,7 @@ bool check_assignment_arguments(CheckerContext *ctx, Array<Operand> const &lhs,
-bool check_unpack_arguments(CheckerContext *ctx, Entity **lhs, isize lhs_count, Array<Operand> *operands, Array<Ast *> const &rhs, bool allow_ok, bool is_variadic) {
+bool check_unpack_arguments(CheckerContext *ctx, Entity **lhs, isize lhs_count, Array<Operand> *operands, Slice<Ast *> const &rhs, bool allow_ok, bool is_variadic) {
bool optional_ok = false;
isize tuple_index = 0;
for_array(i, rhs) {
@@ -6540,10 +6611,8 @@ CALL_ARGUMENT_CHECKER(check_named_call_arguments) {
CallArgumentError err = CallArgumentError_None;
isize param_count = pt->param_count;
- bool *visited = gb_alloc_array(c->allocator, bool, param_count);
- defer (gb_free(c->allocator, visited));
- auto ordered_operands = array_make<Operand>(c->allocator, param_count);
- defer (array_free(&ordered_operands));
+ bool *visited = gb_alloc_array(temporary_allocator(), bool, param_count);
+ auto ordered_operands = array_make<Operand>(temporary_allocator(), param_count);
defer ({
for_array(i, ordered_operands) {
Operand const &o = ordered_operands[i];
@@ -6749,7 +6818,7 @@ Entity **populate_proc_parameter_list(CheckerContext *c, Type *proc_type, isize
}
-bool evaluate_where_clauses(CheckerContext *ctx, Ast *call_expr, Scope *scope, Array<Ast *> *clauses, bool print_err) {
+bool evaluate_where_clauses(CheckerContext *ctx, Ast *call_expr, Scope *scope, Slice<Ast *> *clauses, bool print_err) {
if (clauses != nullptr) {
for_array(i, *clauses) {
Ast *clause = (*clauses)[i];
@@ -6814,7 +6883,7 @@ bool evaluate_where_clauses(CheckerContext *ctx, Ast *call_expr, Scope *scope, A
}
-CallArgumentData check_call_arguments(CheckerContext *c, Operand *operand, Type *proc_type, Ast *call, Array<Ast *> const &args) {
+CallArgumentData check_call_arguments(CheckerContext *c, Operand *operand, Type *proc_type, Ast *call, Slice<Ast *> const &args) {
ast_node(ce, CallExpr, call);
CallArgumentCheckerType *call_checker = check_call_arguments_internal;
@@ -7338,6 +7407,7 @@ CallArgumentError check_polymorphic_record_type(CheckerContext *c, Operand *oper
Entity *e = params->variables[i];
if (e->kind == Entity_Constant) {
check_expr_with_type_hint(c, &operands[i], fv->value, e->type);
+ continue;
}
}
@@ -7371,13 +7441,26 @@ CallArgumentError check_polymorphic_record_type(CheckerContext *c, Operand *oper
TypeTuple *tuple = get_record_polymorphic_params(original_type);
isize param_count = tuple->variables.count;
+ isize minimum_param_count = param_count;
+ for (minimum_param_count = tuple->variables.count-1; minimum_param_count >= 0; minimum_param_count--) {
+ Entity *e = tuple->variables[minimum_param_count];
+ if (e->kind != Entity_Constant) {
+ break;
+ }
+ if (e->Constant.param_value.kind == ParameterValue_Invalid) {
+ break;
+ }
+ }
Array<Operand> ordered_operands = operands;
- if (named_fields) {
- bool *visited = gb_alloc_array(c->allocator, bool, param_count);
+ if (!named_fields) {
+ ordered_operands = array_make<Operand>(permanent_allocator(), param_count);
+ array_copy(&ordered_operands, operands, 0);
+ } else {
+ bool *visited = gb_alloc_array(temporary_allocator(), bool, param_count);
// LEAK(bill)
- ordered_operands = array_make<Operand>(c->allocator, param_count);
+ ordered_operands = array_make<Operand>(permanent_allocator(), param_count);
for_array(i, ce->args) {
Ast *arg = ce->args[i];
@@ -7440,26 +7523,55 @@ CallArgumentError check_polymorphic_record_type(CheckerContext *c, Operand *oper
return err;
}
- if (param_count < ordered_operands.count) {
- error(call, "Too many polymorphic type arguments, expected %td, got %td", param_count, ordered_operands.count);
- err = CallArgumentError_TooManyArguments;
- } else if (param_count > ordered_operands.count) {
- error(call, "Too few polymorphic type arguments, expected %td, got %td", param_count, ordered_operands.count);
- err = CallArgumentError_TooFewArguments;
+ if (minimum_param_count != param_count) {
+ if (param_count < ordered_operands.count) {
+ error(call, "Too many polymorphic type arguments, expected a maximum of %td, got %td", param_count, ordered_operands.count);
+ err = CallArgumentError_TooManyArguments;
+ } else if (minimum_param_count > ordered_operands.count) {
+ error(call, "Too few polymorphic type arguments, expected a minimum of %td, got %td", minimum_param_count, ordered_operands.count);
+ err = CallArgumentError_TooFewArguments;
+ }
+ } else {
+ if (param_count < ordered_operands.count) {
+ error(call, "Too many polymorphic type arguments, expected %td, got %td", param_count, ordered_operands.count);
+ err = CallArgumentError_TooManyArguments;
+ } else if (param_count > ordered_operands.count) {
+ error(call, "Too few polymorphic type arguments, expected %td, got %td", param_count, ordered_operands.count);
+ err = CallArgumentError_TooFewArguments;
+ }
}
if (err != 0) {
return err;
}
+ if (minimum_param_count != param_count) {
+ isize missing_count = 0;
+ // NOTE(bill): Replace missing operands with the default values (if possible)
+ for_array(i, ordered_operands) {
+ Operand *o = &ordered_operands[i];
+ if (o->expr == nullptr) {
+ Entity *e = tuple->variables[i];
+ if (e->kind == Entity_Constant) {
+ missing_count += 1;
+ o->mode = Addressing_Constant;
+ o->type = default_type(e->type);
+ o->expr = unparen_expr(e->Constant.param_value.original_ast_expr);
+ if (e->Constant.param_value.kind == ParameterValue_Constant) {
+ o->value = e->Constant.param_value.value;
+ }
+ }
+ }
+ }
+ }
+
i64 score = 0;
for (isize i = 0; i < param_count; i++) {
+ Entity *e = tuple->variables[i];
Operand *o = &ordered_operands[i];
if (o->mode == Addressing_Invalid) {
continue;
}
- Entity *e = tuple->variables[i];
-
if (e->kind == Entity_TypeName) {
if (o->mode != Addressing_Type) {
if (show_error) {
@@ -7506,8 +7618,6 @@ CallArgumentError check_polymorphic_record_type(CheckerContext *c, Operand *oper
}
{
- gbAllocator a = c->allocator;
-
bool failure = false;
Entity *found_entity = find_polymorphic_record_entity(c, original_type, param_count, ordered_operands, &failure);
if (found_entity) {
@@ -7558,7 +7668,7 @@ CallArgumentError check_polymorphic_record_type(CheckerContext *c, Operand *oper
-ExprKind check_call_expr(CheckerContext *c, Operand *operand, Ast *call, Ast *proc, Array<Ast *> const &args, ProcInlining inlining, Type *type_hint) {
+ExprKind check_call_expr(CheckerContext *c, Operand *operand, Ast *call, Ast *proc, Slice<Ast *> const &args, ProcInlining inlining, Type *type_hint) {
if (proc != nullptr &&
proc->kind == Ast_BasicDirective) {
ast_node(bd, BasicDirective, proc);
@@ -7595,7 +7705,7 @@ ExprKind check_call_expr(CheckerContext *c, Operand *operand, Ast *call, Ast *pr
mix = arg->kind == Ast_FieldValue;
}
if (mix) {
- error(arg, "Mixture of 'field = value' and value elements in a procedure all is not allowed");
+ error(arg, "Mixture of 'field = value' and value elements in a procedure call is not allowed");
fail = true;
}
}
@@ -7800,7 +7910,7 @@ void check_expr_with_type_hint(CheckerContext *c, Operand *o, Ast *e, Type *t) {
err_str = "used as a value";
break;
case Addressing_Type:
- err_str = "is not an expression";
+ err_str = "is not an expression but a type";
break;
case Addressing_Builtin:
err_str = "must be called";
@@ -8110,7 +8220,7 @@ ExprKind check_expr_base_internal(CheckerContext *c, Operand *o, Ast *node, Type
case_ast_node(bl, BasicLit, node);
Type *t = t_invalid;
- switch (bl->value.kind) {
+ switch (node->tav.value.kind) {
case ExactValue_String: t = t_untyped_string; break;
case ExactValue_Float: t = t_untyped_float; break;
case ExactValue_Complex: t = t_untyped_complex; break;
@@ -8128,7 +8238,7 @@ ExprKind check_expr_base_internal(CheckerContext *c, Operand *o, Ast *node, Type
o->mode = Addressing_Constant;
o->type = t;
- o->value = bl->value;
+ o->value = node->tav.value;
case_end;
case_ast_node(bd, BasicDirective, node);
@@ -8170,7 +8280,7 @@ ExprKind check_expr_base_internal(CheckerContext *c, Operand *o, Ast *node, Type
Type *type = alloc_type(Type_Proc);
check_open_scope(&ctx, pl->type);
{
- decl = make_decl_info(ctx.allocator, ctx.scope, ctx.decl);
+ decl = make_decl_info(ctx.scope, ctx.decl);
decl->proc_lit = node;
ctx.decl = decl;
defer (ctx.decl = ctx.decl->parent);
@@ -8467,7 +8577,7 @@ ExprKind check_expr_base_internal(CheckerContext *c, Operand *o, Ast *node, Type
}
if (cl->elems[0]->kind == Ast_FieldValue) {
- bool *fields_visited = gb_alloc_array(c->allocator, bool, field_count);
+ bool *fields_visited = gb_alloc_array(temporary_allocator(), bool, field_count);
for_array(i, cl->elems) {
Ast *elem = cl->elems[i];
@@ -9065,6 +9175,9 @@ ExprKind check_expr_base_internal(CheckerContext *c, Operand *o, Ast *node, Type
}
is_constant = false;
{ // Checker values
+ bool key_is_typeid = is_type_typeid(t->Map.key);
+ bool value_is_typeid = is_type_typeid(t->Map.value);
+
for_array(i, cl->elems) {
Ast *elem = cl->elems[i];
if (elem->kind != Ast_FieldValue) {
@@ -9072,13 +9185,22 @@ ExprKind check_expr_base_internal(CheckerContext *c, Operand *o, Ast *node, Type
continue;
}
ast_node(fv, FieldValue, elem);
- check_expr_with_type_hint(c, o, fv->field, t->Map.key);
+
+ if (key_is_typeid) {
+ check_expr_or_type(c, o, fv->field, t->Map.key);
+ } else {
+ check_expr_with_type_hint(c, o, fv->field, t->Map.key);
+ }
check_assignment(c, o, t->Map.key, str_lit("map literal"));
if (o->mode == Addressing_Invalid) {
continue;
}
- check_expr_with_type_hint(c, o, fv->value, t->Map.value);
+ if (value_is_typeid) {
+ check_expr_or_type(c, o, fv->value, t->Map.value);
+ } else {
+ check_expr_with_type_hint(c, o, fv->value, t->Map.value);
+ }
check_assignment(c, o, t->Map.value, str_lit("map literal"));
}
}
@@ -9349,6 +9471,7 @@ ExprKind check_expr_base_internal(CheckerContext *c, Operand *o, Ast *node, Type
}
add_package_dependency(c, "runtime", "type_assertion_check");
+ add_package_dependency(c, "runtime", "type_assertion_check2");
case_end;
case_ast_node(tc, TypeCast, node);
@@ -9447,8 +9570,13 @@ ExprKind check_expr_base_internal(CheckerContext *c, Operand *o, Ast *node, Type
//
// NOTE(bill, 2020-05-22): I'm going to regret this decision, ain't I?
+ bool allow_arrow_right_selector_expr;
+ allow_arrow_right_selector_expr = c->allow_arrow_right_selector_expr;
+ c->allow_arrow_right_selector_expr = true;
Operand x = {};
ExprKind kind = check_expr_base(c, &x, se->expr, nullptr);
+ c->allow_arrow_right_selector_expr = allow_arrow_right_selector_expr;
+
if (x.mode == Addressing_Invalid || x.type == t_invalid) {
o->mode = Addressing_Invalid;
o->type = t_invalid;
@@ -9543,13 +9671,17 @@ ExprKind check_expr_base_internal(CheckerContext *c, Operand *o, Ast *node, Type
- auto modified_args = array_make<Ast *>(heap_allocator(), ce->args.count+1);
+ auto modified_args = slice_make<Ast *>(heap_allocator(), ce->args.count+1);
modified_args[0] = first_arg;
- array_copy(&modified_args, ce->args, 1);
+ slice_copy(&modified_args, ce->args, 1);
ce->args = modified_args;
se->modified_call = true;
+ allow_arrow_right_selector_expr = c->allow_arrow_right_selector_expr;
+ c->allow_arrow_right_selector_expr = true;
check_expr_base(c, o, se->call, type_hint);
+ c->allow_arrow_right_selector_expr = allow_arrow_right_selector_expr;
+
o->expr = node;
return Expr_Expr;
case_end;
@@ -9634,7 +9766,11 @@ ExprKind check_expr_base_internal(CheckerContext *c, Operand *o, Ast *node, Type
if (is_type_map(t)) {
Operand key = {};
- check_expr_with_type_hint(c, &key, ie->index, t->Map.key);
+ if (is_type_typeid(t->Map.key)) {
+ check_expr_or_type(c, &key, ie->index, t->Map.key);
+ } else {
+ check_expr_with_type_hint(c, &key, ie->index, t->Map.key);
+ }
check_assignment(c, &key, t->Map.key, str_lit("map index"));
if (key.mode == Addressing_Invalid) {
o->mode = Addressing_Invalid;
@@ -10033,7 +10169,7 @@ ExprKind check_expr_base_internal(CheckerContext *c, Operand *o, Ast *node, Type
error(x.expr, "Expected a constant string for the inline asm constraints parameter");
}
- Scope *scope = create_scope(c->scope, heap_allocator());
+ Scope *scope = create_scope(c->scope);
scope->flags |= ScopeFlag_Proc;
Type *params = alloc_type_tuple();
@@ -10143,14 +10279,14 @@ void check_expr_or_type(CheckerContext *c, Operand *o, Ast *e, Type *type_hint)
}
-gbString write_expr_to_string(gbString str, Ast *node);
+gbString write_expr_to_string(gbString str, Ast *node, bool shorthand);
-gbString write_struct_fields_to_string(gbString str, Array<Ast *> const &params) {
+gbString write_struct_fields_to_string(gbString str, Slice<Ast *> const &params) {
for_array(i, params) {
if (i > 0) {
str = gb_string_appendc(str, ", ");
}
- str = write_expr_to_string(str, params[i]);
+ str = write_expr_to_string(str, params[i], false);
}
return str;
}
@@ -10164,11 +10300,23 @@ gbString string_append_string(gbString str, String string) {
gbString string_append_token(gbString str, Token token) {
- return string_append_string(str, token.string);
+ if (token.kind == Token_String) {
+ str = gb_string_append_rune(str, '"');
+ } else if (token.kind == Token_Rune) {
+ str = gb_string_append_rune(str, '\'');
+ }
+ str = string_append_string(str, token.string);
+ if (token.kind == Token_String) {
+ str = gb_string_append_rune(str, '"');
+ } else if (token.kind == Token_Rune) {
+ str = gb_string_append_rune(str, '\'');
+ }
+
+ return str;
}
-gbString write_expr_to_string(gbString str, Ast *node) {
+gbString write_expr_to_string(gbString str, Ast *node, bool shorthand) {
if (node == nullptr)
return str;
@@ -10206,21 +10354,30 @@ gbString write_expr_to_string(gbString str, Ast *node) {
str = gb_string_appendc(str, "proc{");
for_array(i, pg->args) {
if (i > 0) str = gb_string_appendc(str, ", ");
- str = write_expr_to_string(str, pg->args[i]);
+ str = write_expr_to_string(str, pg->args[i], shorthand);
}
str = gb_string_append_rune(str, '}');
case_end;
case_ast_node(pl, ProcLit, node);
- str = write_expr_to_string(str, pl->type);
+ str = write_expr_to_string(str, pl->type, shorthand);
+ if (pl->body) {
+ str = gb_string_appendc(str, " {...}");
+ } else {
+ str = gb_string_appendc(str, " ---");
+ }
case_end;
case_ast_node(cl, CompoundLit, node);
- str = write_expr_to_string(str, cl->type);
+ str = write_expr_to_string(str, cl->type, shorthand);
str = gb_string_append_rune(str, '{');
- for_array(i, cl->elems) {
- if (i > 0) str = gb_string_appendc(str, ", ");
- str = write_expr_to_string(str, cl->elems[i]);
+ if (shorthand) {
+ str = gb_string_appendc(str, "...");
+ } else {
+ for_array(i, cl->elems) {
+ if (i > 0) str = gb_string_appendc(str, ", ");
+ str = write_expr_to_string(str, cl->elems[i], shorthand);
+ }
}
str = gb_string_append_rune(str, '}');
case_end;
@@ -10229,71 +10386,71 @@ gbString write_expr_to_string(gbString str, Ast *node) {
case_ast_node(te, TagExpr, node);
str = gb_string_append_rune(str, '#');
str = string_append_token(str, te->name);
- str = write_expr_to_string(str, te->expr);
+ str = write_expr_to_string(str, te->expr, shorthand);
case_end;
case_ast_node(ue, UnaryExpr, node);
str = string_append_token(str, ue->op);
- str = write_expr_to_string(str, ue->expr);
+ str = write_expr_to_string(str, ue->expr, shorthand);
case_end;
case_ast_node(de, DerefExpr, node);
- str = write_expr_to_string(str, de->expr);
+ str = write_expr_to_string(str, de->expr, shorthand);
str = gb_string_append_rune(str, '^');
case_end;
case_ast_node(be, BinaryExpr, node);
- str = write_expr_to_string(str, be->left);
+ str = write_expr_to_string(str, be->left, shorthand);
str = gb_string_append_rune(str, ' ');
str = string_append_token(str, be->op);
str = gb_string_append_rune(str, ' ');
- str = write_expr_to_string(str, be->right);
+ str = write_expr_to_string(str, be->right, shorthand);
case_end;
case_ast_node(te, TernaryExpr, node);
- str = write_expr_to_string(str, te->cond);
+ str = write_expr_to_string(str, te->cond, shorthand);
str = gb_string_appendc(str, " ? ");
- str = write_expr_to_string(str, te->x);
+ str = write_expr_to_string(str, te->x, shorthand);
str = gb_string_appendc(str, " : ");
- str = write_expr_to_string(str, te->y);
+ str = write_expr_to_string(str, te->y, shorthand);
case_end;
case_ast_node(te, TernaryIfExpr, node);
- str = write_expr_to_string(str, te->x);
+ str = write_expr_to_string(str, te->x, shorthand);
str = gb_string_appendc(str, " if ");
- str = write_expr_to_string(str, te->cond);
+ str = write_expr_to_string(str, te->cond, shorthand);
str = gb_string_appendc(str, " else ");
- str = write_expr_to_string(str, te->y);
+ str = write_expr_to_string(str, te->y, shorthand);
case_end;
case_ast_node(te, TernaryWhenExpr, node);
- str = write_expr_to_string(str, te->x);
+ str = write_expr_to_string(str, te->x, shorthand);
str = gb_string_appendc(str, " when ");
- str = write_expr_to_string(str, te->cond);
+ str = write_expr_to_string(str, te->cond, shorthand);
str = gb_string_appendc(str, " else ");
- str = write_expr_to_string(str, te->y);
+ str = write_expr_to_string(str, te->y, shorthand);
case_end;
case_ast_node(pe, ParenExpr, node);
str = gb_string_append_rune(str, '(');
- str = write_expr_to_string(str, pe->expr);
+ str = write_expr_to_string(str, pe->expr, shorthand);
str = gb_string_append_rune(str, ')');
case_end;
case_ast_node(se, SelectorExpr, node);
- str = write_expr_to_string(str, se->expr);
+ str = write_expr_to_string(str, se->expr, shorthand);
str = string_append_token(str, se->token);
- str = write_expr_to_string(str, se->selector);
+ str = write_expr_to_string(str, se->selector, shorthand);
case_end;
case_ast_node(se, ImplicitSelectorExpr, node);
str = gb_string_append_rune(str, '.');
- str = write_expr_to_string(str, se->selector);
+ str = write_expr_to_string(str, se->selector, shorthand);
case_end;
case_ast_node(se, SelectorCallExpr, node);
- str = write_expr_to_string(str, se->expr);
+ str = write_expr_to_string(str, se->expr, shorthand);
str = gb_string_appendc(str, "(");
ast_node(ce, CallExpr, se->call);
isize start = se->modified_call ? 1 : 0;
@@ -10302,86 +10459,86 @@ gbString write_expr_to_string(gbString str, Ast *node) {
if (i > start) {
str = gb_string_appendc(str, ", ");
}
- str = write_expr_to_string(str, arg);
+ str = write_expr_to_string(str, arg, shorthand);
}
str = gb_string_appendc(str, ")");
case_end;
case_ast_node(ta, TypeAssertion, node);
- str = write_expr_to_string(str, ta->expr);
+ str = write_expr_to_string(str, ta->expr, shorthand);
str = gb_string_appendc(str, ".(");
- str = write_expr_to_string(str, ta->type);
+ str = write_expr_to_string(str, ta->type, shorthand);
str = gb_string_append_rune(str, ')');
case_end;
case_ast_node(tc, TypeCast, node);
str = string_append_token(str, tc->token);
str = gb_string_append_rune(str, '(');
- str = write_expr_to_string(str, tc->type);
+ str = write_expr_to_string(str, tc->type, shorthand);
str = gb_string_append_rune(str, ')');
- str = write_expr_to_string(str, tc->expr);
+ str = write_expr_to_string(str, tc->expr, shorthand);
case_end;
case_ast_node(ac, AutoCast, node);
str = string_append_token(str, ac->token);
str = gb_string_append_rune(str, ' ');
- str = write_expr_to_string(str, ac->expr);
+ str = write_expr_to_string(str, ac->expr, shorthand);
case_end;
case_ast_node(ie, IndexExpr, node);
- str = write_expr_to_string(str, ie->expr);
+ str = write_expr_to_string(str, ie->expr, shorthand);
str = gb_string_append_rune(str, '[');
- str = write_expr_to_string(str, ie->index);
+ str = write_expr_to_string(str, ie->index, shorthand);
str = gb_string_append_rune(str, ']');
case_end;
case_ast_node(se, SliceExpr, node);
- str = write_expr_to_string(str, se->expr);
+ str = write_expr_to_string(str, se->expr, shorthand);
str = gb_string_append_rune(str, '[');
- str = write_expr_to_string(str, se->low);
+ str = write_expr_to_string(str, se->low, shorthand);
str = string_append_token(str, se->interval);
- str = write_expr_to_string(str, se->high);
+ str = write_expr_to_string(str, se->high, shorthand);
str = gb_string_append_rune(str, ']');
case_end;
case_ast_node(e, Ellipsis, node);
str = gb_string_appendc(str, "..");
- str = write_expr_to_string(str, e->expr);
+ str = write_expr_to_string(str, e->expr, shorthand);
case_end;
case_ast_node(fv, FieldValue, node);
- str = write_expr_to_string(str, fv->field);
+ str = write_expr_to_string(str, fv->field, shorthand);
str = gb_string_appendc(str, " = ");
- str = write_expr_to_string(str, fv->value);
+ str = write_expr_to_string(str, fv->value, shorthand);
case_end;
case_ast_node(ht, HelperType, node);
str = gb_string_appendc(str, "#type ");
- str = write_expr_to_string(str, ht->type);
+ str = write_expr_to_string(str, ht->type, shorthand);
case_end;
case_ast_node(ht, DistinctType, node);
str = gb_string_appendc(str, "distinct ");
- str = write_expr_to_string(str, ht->type);
+ str = write_expr_to_string(str, ht->type, shorthand);
case_end;
case_ast_node(ht, OpaqueType, node);
str = gb_string_appendc(str, "opaque ");
- str = write_expr_to_string(str, ht->type);
+ str = write_expr_to_string(str, ht->type, shorthand);
case_end;
case_ast_node(pt, PolyType, node);
str = gb_string_append_rune(str, '$');
- str = write_expr_to_string(str, pt->type);
+ str = write_expr_to_string(str, pt->type, shorthand);
if (pt->specialization != nullptr) {
str = gb_string_append_rune(str, '/');
- str = write_expr_to_string(str, pt->specialization);
+ str = write_expr_to_string(str, pt->specialization, shorthand);
}
case_end;
case_ast_node(pt, PointerType, node);
str = gb_string_append_rune(str, '^');
- str = write_expr_to_string(str, pt->type);
+ str = write_expr_to_string(str, pt->type, shorthand);
case_end;
case_ast_node(at, ArrayType, node);
@@ -10391,40 +10548,44 @@ gbString write_expr_to_string(gbString str, Ast *node) {
at->count->UnaryExpr.op.kind == Token_Question) {
str = gb_string_appendc(str, "?");
} else {
- str = write_expr_to_string(str, at->count);
+ str = write_expr_to_string(str, at->count, shorthand);
}
str = gb_string_append_rune(str, ']');
- str = write_expr_to_string(str, at->elem);
+ str = write_expr_to_string(str, at->elem, shorthand);
case_end;
case_ast_node(at, DynamicArrayType, node);
str = gb_string_appendc(str, "[dynamic]");
- str = write_expr_to_string(str, at->elem);
+ str = write_expr_to_string(str, at->elem, shorthand);
case_end;
case_ast_node(bf, BitFieldType, node);
str = gb_string_appendc(str, "bit_field ");
if (bf->align) {
str = gb_string_appendc(str, "#align ");
- str = write_expr_to_string(str, bf->align);
+ str = write_expr_to_string(str, bf->align, shorthand);
}
str = gb_string_appendc(str, "{");
- str = write_struct_fields_to_string(str, bf->fields);
+ if (shorthand) {
+ str = gb_string_appendc(str, "...");
+ } else {
+ str = write_struct_fields_to_string(str, bf->fields);
+ }
str = gb_string_appendc(str, "}");
case_end;
case_ast_node(bs, BitSetType, node);
str = gb_string_appendc(str, "bit_set[");
- str = write_expr_to_string(str, bs->elem);
+ str = write_expr_to_string(str, bs->elem, shorthand);
str = gb_string_appendc(str, "]");
case_end;
case_ast_node(mt, MapType, node);
str = gb_string_appendc(str, "map[");
- str = write_expr_to_string(str, mt->key);
+ str = write_expr_to_string(str, mt->key, shorthand);
str = gb_string_append_rune(str, ']');
- str = write_expr_to_string(str, mt->value);
+ str = write_expr_to_string(str, mt->value, shorthand);
case_end;
case_ast_node(f, Field, node);
@@ -10444,7 +10605,7 @@ gbString write_expr_to_string(gbString str, Ast *node) {
for_array(i, f->names) {
Ast *name = f->names[i];
if (i > 0) str = gb_string_appendc(str, ", ");
- str = write_expr_to_string(str, name);
+ str = write_expr_to_string(str, name, shorthand);
}
if (f->names.count > 0) {
if (f->type == nullptr && f->default_value != nullptr) {
@@ -10454,14 +10615,14 @@ gbString write_expr_to_string(gbString str, Ast *node) {
}
if (f->type != nullptr) {
str = gb_string_append_rune(str, ' ');
- str = write_expr_to_string(str, f->type);
+ str = write_expr_to_string(str, f->type, shorthand);
}
if (f->default_value != nullptr) {
if (f->type != nullptr) {
str = gb_string_append_rune(str, ' ');
}
str = gb_string_appendc(str, "= ");
- str = write_expr_to_string(str, f->default_value);
+ str = write_expr_to_string(str, f->default_value, shorthand);
}
case_end;
@@ -10487,7 +10648,7 @@ gbString write_expr_to_string(gbString str, Ast *node) {
for_array(i, f->list) {
if (i > 0) str = gb_string_appendc(str, ", ");
if (has_name) {
- str = write_expr_to_string(str, f->list[i]);
+ str = write_expr_to_string(str, f->list[i], shorthand);
} else {
ast_node(field, Field, f->list[i]);
@@ -10501,7 +10662,7 @@ gbString write_expr_to_string(gbString str, Ast *node) {
str = gb_string_appendc(str, "#c_vararg ");
}
- str = write_expr_to_string(str, field->type);
+ str = write_expr_to_string(str, field->type, shorthand);
}
}
case_end;
@@ -10516,7 +10677,7 @@ gbString write_expr_to_string(gbString str, Ast *node) {
break;
}
- str = write_expr_to_string(str, ce->proc);
+ str = write_expr_to_string(str, ce->proc, shorthand);
str = gb_string_appendc(str, "(");
for_array(i, ce->args) {
@@ -10524,7 +10685,7 @@ gbString write_expr_to_string(gbString str, Ast *node) {
if (i > 0) {
str = gb_string_appendc(str, ", ");
}
- str = write_expr_to_string(str, arg);
+ str = write_expr_to_string(str, arg, shorthand);
}
str = gb_string_appendc(str, ")");
case_end;
@@ -10533,17 +10694,36 @@ gbString write_expr_to_string(gbString str, Ast *node) {
str = gb_string_appendc(str, "typeid");
if (tt->specialization) {
str = gb_string_appendc(str, "/");
- str = write_expr_to_string(str, tt->specialization);
+ str = write_expr_to_string(str, tt->specialization, shorthand);
}
case_end;
case_ast_node(pt, ProcType, node);
str = gb_string_appendc(str, "proc(");
- str = write_expr_to_string(str, pt->params);
+ str = write_expr_to_string(str, pt->params, shorthand);
str = gb_string_appendc(str, ")");
if (pt->results != nullptr) {
str = gb_string_appendc(str, " -> ");
- str = write_expr_to_string(str, pt->results);
+
+ bool parens_needed = false;
+ if (pt->results && pt->results->kind == Ast_FieldList) {
+ for_array(i, pt->results->FieldList.list) {
+ Ast *field = pt->results->FieldList.list[i];
+ ast_node(f, Field, field);
+ if (f->names.count != 0) {
+ parens_needed = true;
+ break;
+ }
+ }
+ }
+
+ if (parens_needed) {
+ str = gb_string_append_rune(str, '(');
+ }
+ str = write_expr_to_string(str, pt->results, shorthand);
+ if (parens_needed) {
+ str = gb_string_append_rune(str, ')');
+ }
}
case_end;
@@ -10553,7 +10733,11 @@ gbString write_expr_to_string(gbString str, Ast *node) {
if (st->is_packed) str = gb_string_appendc(str, "#packed ");
if (st->is_raw_union) str = gb_string_appendc(str, "#raw_union ");
str = gb_string_append_rune(str, '{');
- str = write_struct_fields_to_string(str, st->fields);
+ if (shorthand) {
+ str = gb_string_appendc(str, "...");
+ } else {
+ str = write_struct_fields_to_string(str, st->fields);
+ }
str = gb_string_append_rune(str, '}');
case_end;
@@ -10561,30 +10745,38 @@ gbString write_expr_to_string(gbString str, Ast *node) {
case_ast_node(st, UnionType, node);
str = gb_string_appendc(str, "union ");
str = gb_string_append_rune(str, '{');
- str = write_struct_fields_to_string(str, st->variants);
+ if (shorthand) {
+ str = gb_string_appendc(str, "...");
+ } else {
+ str = write_struct_fields_to_string(str, st->variants);
+ }
str = gb_string_append_rune(str, '}');
case_end;
case_ast_node(et, EnumType, node);
str = gb_string_appendc(str, "enum ");
if (et->base_type != nullptr) {
- str = write_expr_to_string(str, et->base_type);
+ str = write_expr_to_string(str, et->base_type, shorthand);
str = gb_string_append_rune(str, ' ');
}
str = gb_string_append_rune(str, '{');
- for_array(i, et->fields) {
- if (i > 0) {
- str = gb_string_appendc(str, ", ");
+ if (shorthand) {
+ str = gb_string_appendc(str, "...");
+ } else {
+ for_array(i, et->fields) {
+ if (i > 0) {
+ str = gb_string_appendc(str, ", ");
+ }
+ str = write_expr_to_string(str, et->fields[i], shorthand);
}
- str = write_expr_to_string(str, et->fields[i]);
}
str = gb_string_append_rune(str, '}');
case_end;
case_ast_node(rt, RelativeType, node);
- str = write_expr_to_string(str, rt->tag);
+ str = write_expr_to_string(str, rt->tag, shorthand);
str = gb_string_appendc(str, "" );
- str = write_expr_to_string(str, rt->type);
+ str = write_expr_to_string(str, rt->type, shorthand);
case_end;
@@ -10594,12 +10786,12 @@ gbString write_expr_to_string(gbString str, Ast *node) {
if (i > 0) {
str = gb_string_appendc(str, ", ");
}
- str = write_expr_to_string(str, ia->param_types[i]);
+ str = write_expr_to_string(str, ia->param_types[i], shorthand);
}
str = gb_string_appendc(str, ")");
if (ia->return_type != nullptr) {
str = gb_string_appendc(str, " -> ");
- str = write_expr_to_string(str, ia->return_type);
+ str = write_expr_to_string(str, ia->return_type, shorthand);
}
if (ia->has_side_effects) {
str = gb_string_appendc(str, " #side_effects");
@@ -10612,9 +10804,13 @@ gbString write_expr_to_string(gbString str, Ast *node) {
str = gb_string_appendc(str, inline_asm_dialect_strings[ia->dialect]);
}
str = gb_string_appendc(str, " {");
- str = write_expr_to_string(str, ia->asm_string);
- str = gb_string_appendc(str, ", ");
- str = write_expr_to_string(str, ia->constraints_string);
+ if (shorthand) {
+ str = gb_string_appendc(str, "...");
+ } else {
+ str = write_expr_to_string(str, ia->asm_string, shorthand);
+ str = gb_string_appendc(str, ", ");
+ str = write_expr_to_string(str, ia->constraints_string, shorthand);
+ }
str = gb_string_appendc(str, "}");
case_end;
}
@@ -10623,5 +10819,8 @@ gbString write_expr_to_string(gbString str, Ast *node) {
}
gbString expr_to_string(Ast *expression) {
- return write_expr_to_string(gb_string_make(heap_allocator(), ""), expression);
+ return write_expr_to_string(gb_string_make(heap_allocator(), ""), expression, false);
+}
+gbString expr_to_string_shorthand(Ast *expression) {
+ return write_expr_to_string(gb_string_make(heap_allocator(), ""), expression, true);
}
diff --git a/src/check_stmt.cpp b/src/check_stmt.cpp
index e6902f6a3..8a41dd12b 100644
--- a/src/check_stmt.cpp
+++ b/src/check_stmt.cpp
@@ -15,7 +15,7 @@ bool is_divigering_stmt(Ast *stmt) {
return t->kind == Type_Proc && t->Proc.diverging;
}
-void check_stmt_list(CheckerContext *ctx, Array<Ast *> const &stmts, u32 flags) {
+void check_stmt_list(CheckerContext *ctx, Slice<Ast *> const &stmts, u32 flags) {
if (stmts.count == 0) {
return;
}
@@ -78,7 +78,7 @@ void check_stmt_list(CheckerContext *ctx, Array<Ast *> const &stmts, u32 flags)
}
}
-bool check_is_terminating_list(Array<Ast *> const &stmts, String const &label) {
+bool check_is_terminating_list(Slice<Ast *> const &stmts, String const &label) {
// Iterate backwards
for (isize n = stmts.count-1; n >= 0; n--) {
Ast *stmt = stmts[n];
@@ -96,7 +96,7 @@ bool check_is_terminating_list(Array<Ast *> const &stmts, String const &label) {
return false;
}
-bool check_has_break_list(Array<Ast *> const &stmts, String const &label, bool implicit) {
+bool check_has_break_list(Slice<Ast *> const &stmts, String const &label, bool implicit) {
for_array(i, stmts) {
Ast *stmt = stmts[i];
if (check_has_break(stmt, label, implicit)) {
@@ -641,8 +641,7 @@ void add_constant_switch_case(CheckerContext *ctx, Map<TypeAndToken> *seen, Oper
TypeAndToken *found = map_get(seen, key);
if (found != nullptr) {
isize count = multi_map_count(seen, key);
- TypeAndToken *taps = gb_alloc_array(ctx->allocator, TypeAndToken, count);
- defer (gb_free(ctx->allocator, taps));
+ TypeAndToken *taps = gb_alloc_array(temporary_allocator(), TypeAndToken, count);
multi_map_get_all(seen, key, taps);
for (isize i = 0; i < count; i++) {
@@ -859,8 +858,7 @@ void check_switch_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags) {
token.pos = ast_token(ss->body).pos;
token.string = str_lit("true");
- x.expr = gb_alloc_item(ctx->allocator, Ast);
- x.expr->kind = Ast_Ident;
+ x.expr = alloc_ast_node(nullptr, Ast_Ident);
x.expr->Ident.token = token;
}
@@ -1025,8 +1023,7 @@ void check_switch_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags) {
GB_ASSERT(is_type_enum(et));
auto fields = et->Enum.fields;
- auto unhandled = array_make<Entity *>(ctx->allocator, 0, fields.count);
- defer (array_free(&unhandled));
+ auto unhandled = array_make<Entity *>(temporary_allocator(), 0, fields.count);
for_array(i, fields) {
Entity *f = fields[i];
@@ -1265,8 +1262,7 @@ void check_type_switch_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags) {
GB_ASSERT(is_type_union(ut));
auto variants = ut->Union.variants;
- auto unhandled = array_make<Type *>(ctx->allocator, 0, variants.count);
- defer (array_free(&unhandled));
+ auto unhandled = array_make<Type *>(temporary_allocator(), 0, variants.count);
for_array(i, variants) {
Type *t = variants[i];
@@ -1433,12 +1429,11 @@ void check_stmt_internal(CheckerContext *ctx, Ast *node, u32 flags) {
return;
}
+
// NOTE(bill): If there is a bad syntax error, rhs > lhs which would mean there would need to be
// an extra allocation
- auto lhs_operands = array_make<Operand>(ctx->allocator, lhs_count);
- auto rhs_operands = array_make<Operand>(ctx->allocator, 0, 2*lhs_count);
- defer (array_free(&lhs_operands));
- defer (array_free(&rhs_operands));
+ auto lhs_operands = array_make<Operand>(temporary_allocator(), lhs_count);
+ auto rhs_operands = array_make<Operand>(temporary_allocator(), 0, 2*lhs_count);
for_array(i, as->lhs) {
if (is_blank_ident(as->lhs[i])) {
@@ -1462,8 +1457,7 @@ void check_stmt_internal(CheckerContext *ctx, Ast *node, u32 flags) {
}
}
- auto lhs_to_ignore = array_make<bool>(ctx->allocator, lhs_count);
- defer (array_free(&lhs_to_ignore));
+ auto lhs_to_ignore = array_make<bool>(temporary_allocator(), lhs_count);
isize max = gb_min(lhs_count, rhs_count);
// NOTE(bill, 2020-05-02): This is an utter hack to get these custom atom operations working
@@ -1642,8 +1636,7 @@ void check_stmt_internal(CheckerContext *ctx, Ast *node, u32 flags) {
} else if (operands.count != result_count) {
error(node, "Expected %td return values, got %td", result_count, operands.count);
} else {
- isize max_count = rs->results.count;
- for (isize i = 0; i < max_count; i++) {
+ for (isize i = 0; i < result_count; i++) {
Entity *e = pt->results->Tuple.variables[i];
check_assignment(ctx, &operands[i], e->type, str_lit("return statement"));
}
@@ -1878,7 +1871,7 @@ void check_stmt_internal(CheckerContext *ctx, Ast *node, u32 flags) {
DeclInfo *d = decl_info_of_entity(e);
GB_ASSERT(d == nullptr);
add_entity(ctx->checker, ctx->scope, e->identifier, e);
- d = make_decl_info(ctx->allocator, ctx->scope, ctx->decl);
+ d = make_decl_info(ctx->scope, ctx->decl);
add_entity_and_decl_info(ctx, e->identifier, e, d);
}
@@ -2036,7 +2029,7 @@ void check_stmt_internal(CheckerContext *ctx, Ast *node, u32 flags) {
case_ast_node(vd, ValueDecl, node);
if (vd->is_mutable) {
- Entity **entities = gb_alloc_array(ctx->allocator, Entity *, vd->names.count);
+ Entity **entities = gb_alloc_array(permanent_allocator(), Entity *, vd->names.count);
isize entity_count = 0;
isize new_name_count = 0;
diff --git a/src/check_type.cpp b/src/check_type.cpp
index 93040e493..ab69c89bc 100644
--- a/src/check_type.cpp
+++ b/src/check_type.cpp
@@ -1,3 +1,4 @@
+ParameterValue handle_parameter_value(CheckerContext *ctx, Type *in_type, Type **out_type_, Ast *expr, bool allow_caller_location);
void populate_using_array_index(CheckerContext *ctx, Ast *node, AstField *field, Type *t, String name, i32 idx) {
t = base_type(t);
@@ -116,7 +117,7 @@ bool does_field_type_allow_using(Type *t) {
return false;
}
-void check_struct_fields(CheckerContext *ctx, Ast *node, Array<Entity *> *fields, Array<String> *tags, Array<Ast *> const &params,
+void check_struct_fields(CheckerContext *ctx, Ast *node, Array<Entity *> *fields, Array<String> *tags, Slice<Ast *> const &params,
isize init_field_capacity, Type *struct_type, String context) {
*fields = array_make<Entity *>(heap_allocator(), 0, init_field_capacity);
*tags = array_make<String>(heap_allocator(), 0, init_field_capacity);
@@ -388,7 +389,7 @@ void check_struct_type(CheckerContext *ctx, Type *struct_type, Ast *node, Array<
if (st->polymorphic_params != nullptr) {
ast_node(field_list, FieldList, st->polymorphic_params);
- Array<Ast *> params = field_list->list;
+ Slice<Ast *> params = field_list->list;
if (params.count != 0) {
isize variable_count = 0;
for_array(i, params) {
@@ -399,7 +400,7 @@ void check_struct_type(CheckerContext *ctx, Type *struct_type, Ast *node, Array<
}
}
- auto entities = array_make<Entity *>(ctx->allocator, 0, variable_count);
+ auto entities = array_make<Entity *>(permanent_allocator(), 0, variable_count);
for_array(i, params) {
Ast *param = params[i];
@@ -408,32 +409,50 @@ void check_struct_type(CheckerContext *ctx, Type *struct_type, Ast *node, Array<
}
ast_node(p, Field, param);
Ast *type_expr = p->type;
+ Ast *default_value = unparen_expr(p->default_value);
Type *type = nullptr;
bool is_type_param = false;
bool is_type_polymorphic_type = false;
- if (type_expr == nullptr) {
+ if (type_expr == nullptr && default_value == nullptr) {
error(param, "Expected a type for this parameter");
continue;
}
- if (type_expr->kind == Ast_Ellipsis) {
- type_expr = type_expr->Ellipsis.expr;
- error(param, "A polymorphic parameter cannot be variadic");
+
+ if (type_expr != nullptr) {
+ if (type_expr->kind == Ast_Ellipsis) {
+ type_expr = type_expr->Ellipsis.expr;
+ error(param, "A polymorphic parameter cannot be variadic");
+ }
+ if (type_expr->kind == Ast_TypeidType) {
+ is_type_param = true;
+ Type *specialization = nullptr;
+ if (type_expr->TypeidType.specialization != nullptr) {
+ Ast *s = type_expr->TypeidType.specialization;
+ specialization = check_type(ctx, s);
+ }
+ type = alloc_type_generic(ctx->scope, 0, str_lit(""), specialization);
+ } else {
+ type = check_type(ctx, type_expr);
+ if (is_type_polymorphic(type)) {
+ is_type_polymorphic_type = true;
+ }
+ }
}
- if (type_expr->kind == Ast_TypeidType) {
- is_type_param = true;
- Type *specialization = nullptr;
- if (type_expr->TypeidType.specialization != nullptr) {
- Ast *s = type_expr->TypeidType.specialization;
- specialization = check_type(ctx, s);
+
+ ParameterValue param_value = {};
+ if (default_value != nullptr) {
+ Type *out_type = nullptr;
+ param_value = handle_parameter_value(ctx, type, &out_type, default_value, false);
+ if (type == nullptr && out_type != nullptr) {
+ type = out_type;
}
- type = alloc_type_generic(ctx->scope, 0, str_lit(""), specialization);
- } else {
- type = check_type(ctx, type_expr);
- if (is_type_polymorphic(type)) {
- is_type_polymorphic_type = true;
+ if (param_value.kind != ParameterValue_Constant && param_value.kind != ParameterValue_Nil) {
+ error(default_value, "Invalid parameter value");
+ param_value = {};
}
}
+
if (type == nullptr) {
error(params[i], "Invalid parameter type");
type = t_invalid;
@@ -471,7 +490,14 @@ void check_struct_type(CheckerContext *ctx, Type *struct_type, Ast *node, Array<
Token token = name->Ident.token;
if (poly_operands != nullptr) {
- Operand operand = (*poly_operands)[entities.count];
+ Operand operand = {};
+ operand.type = t_invalid;
+ if (entities.count < poly_operands->count) {
+ operand = (*poly_operands)[entities.count];
+ } else if (param_value.kind != ParameterValue_Invalid) {
+ operand.mode = Addressing_Constant;
+ operand.value = param_value.value;
+ }
if (is_type_param) {
if (is_type_polymorphic(base_type(operand.type))) {
is_polymorphic = true;
@@ -486,6 +512,7 @@ void check_struct_type(CheckerContext *ctx, Type *struct_type, Ast *node, Array<
}
if (e == nullptr) {
e = alloc_entity_constant(scope, token, operand.type, operand.value);
+ e->Constant.param_value = param_value;
}
}
} else {
@@ -493,7 +520,8 @@ void check_struct_type(CheckerContext *ctx, Type *struct_type, Ast *node, Array<
e = alloc_entity_type_name(scope, token, type);
e->TypeName.is_type_alias = true;
} else {
- e = alloc_entity_constant(scope, token, type, empty_exact_value);
+ e = alloc_entity_constant(scope, token, type, param_value.value);
+ e->Constant.param_value = param_value;
}
}
@@ -568,7 +596,7 @@ void check_union_type(CheckerContext *ctx, Type *union_type, Ast *node, Array<Op
Entity *using_index_expr = nullptr;
- auto variants = array_make<Type *>(ctx->allocator, 0, variant_count);
+ auto variants = array_make<Type *>(permanent_allocator(), 0, variant_count);
union_type->Union.scope = ctx->scope;
@@ -579,7 +607,7 @@ void check_union_type(CheckerContext *ctx, Type *union_type, Ast *node, Array<Op
if (ut->polymorphic_params != nullptr) {
ast_node(field_list, FieldList, ut->polymorphic_params);
- Array<Ast *> params = field_list->list;
+ Slice<Ast *> params = field_list->list;
if (params.count != 0) {
isize variable_count = 0;
for_array(i, params) {
@@ -590,7 +618,7 @@ void check_union_type(CheckerContext *ctx, Type *union_type, Ast *node, Array<Op
}
}
- auto entities = array_make<Entity *>(ctx->allocator, 0, variable_count);
+ auto entities = array_make<Entity *>(permanent_allocator(), 0, variable_count);
for_array(i, params) {
Ast *param = params[i];
@@ -599,29 +627,45 @@ void check_union_type(CheckerContext *ctx, Type *union_type, Ast *node, Array<Op
}
ast_node(p, Field, param);
Ast *type_expr = p->type;
+ Ast *default_value = unparen_expr(p->default_value);
Type *type = nullptr;
bool is_type_param = false;
bool is_type_polymorphic_type = false;
- if (type_expr == nullptr) {
+ if (type_expr == nullptr && default_value == nullptr) {
error(param, "Expected a type for this parameter");
continue;
}
- if (type_expr->kind == Ast_Ellipsis) {
- type_expr = type_expr->Ellipsis.expr;
- error(param, "A polymorphic parameter cannot be variadic");
+ if (type_expr != nullptr) {
+ if (type_expr->kind == Ast_Ellipsis) {
+ type_expr = type_expr->Ellipsis.expr;
+ error(param, "A polymorphic parameter cannot be variadic");
+ }
+ if (type_expr->kind == Ast_TypeidType) {
+ is_type_param = true;
+ Type *specialization = nullptr;
+ if (type_expr->TypeidType.specialization != nullptr) {
+ Ast *s = type_expr->TypeidType.specialization;
+ specialization = check_type(ctx, s);
+ }
+ type = alloc_type_generic(ctx->scope, 0, str_lit(""), specialization);
+ } else {
+ type = check_type(ctx, type_expr);
+ if (is_type_polymorphic(type)) {
+ is_type_polymorphic_type = true;
+ }
+ }
}
- if (type_expr->kind == Ast_TypeidType) {
- is_type_param = true;
- Type *specialization = nullptr;
- if (type_expr->TypeidType.specialization != nullptr) {
- Ast *s = type_expr->TypeidType.specialization;
- specialization = check_type(ctx, s);
+
+ ParameterValue param_value = {};
+ if (default_value != nullptr) {
+ Type *out_type = nullptr;
+ param_value = handle_parameter_value(ctx, type, &out_type, default_value, false);
+ if (type == nullptr && out_type != nullptr) {
+ type = out_type;
}
- type = alloc_type_generic(ctx->scope, 0, str_lit(""), specialization);
- } else {
- type = check_type(ctx, type_expr);
- if (is_type_polymorphic(type)) {
- is_type_polymorphic_type = true;
+ if (param_value.kind != ParameterValue_Constant && param_value.kind != ParameterValue_Nil) {
+ error(default_value, "Invalid parameter value");
+ param_value = {};
}
}
@@ -662,7 +706,14 @@ void check_union_type(CheckerContext *ctx, Type *union_type, Ast *node, Array<Op
Token token = name->Ident.token;
if (poly_operands != nullptr) {
- Operand operand = (*poly_operands)[entities.count];
+ Operand operand = {};
+ operand.type = t_invalid;
+ if (entities.count < poly_operands->count) {
+ operand = (*poly_operands)[entities.count];
+ } else if (param_value.kind != ParameterValue_Invalid) {
+ operand.mode = Addressing_Constant;
+ operand.value = param_value.value;
+ }
if (is_type_param) {
GB_ASSERT(operand.mode == Addressing_Type ||
operand.mode == Addressing_Invalid);
@@ -675,6 +726,7 @@ void check_union_type(CheckerContext *ctx, Type *union_type, Ast *node, Array<Op
} else {
// GB_ASSERT(operand.mode == Addressing_Constant);
e = alloc_entity_constant(scope, token, operand.type, operand.value);
+ e->Constant.param_value = param_value;
}
} else {
if (is_type_param) {
@@ -682,6 +734,7 @@ void check_union_type(CheckerContext *ctx, Type *union_type, Ast *node, Array<Op
e->TypeName.is_type_alias = true;
} else {
e = alloc_entity_constant(scope, token, type, empty_exact_value);
+ e->Constant.param_value = param_value;
}
}
@@ -816,7 +869,7 @@ void check_enum_type(CheckerContext *ctx, Type *enum_type, Type *named_type, Ast
enum_type->Enum.base_type = base_type;
enum_type->Enum.scope = ctx->scope;
- auto fields = array_make<Entity *>(ctx->allocator, 0, et->fields.count);
+ auto fields = array_make<Entity *>(permanent_allocator(), 0, et->fields.count);
Type *constant_type = enum_type;
if (named_type != nullptr) {
@@ -933,9 +986,9 @@ void check_bit_field_type(CheckerContext *ctx, Type *bit_field_type, Ast *node)
ast_node(bft, BitFieldType, node);
GB_ASSERT(is_type_bit_field(bit_field_type));
- auto fields = array_make<Entity*>(ctx->allocator, 0, bft->fields.count);
- auto sizes = array_make<u32> (ctx->allocator, 0, bft->fields.count);
- auto offsets = array_make<u32> (ctx->allocator, 0, bft->fields.count);
+ auto fields = array_make<Entity*>(permanent_allocator(), 0, bft->fields.count);
+ auto sizes = array_make<u32> (permanent_allocator(), 0, bft->fields.count);
+ auto offsets = array_make<u32> (permanent_allocator(), 0, bft->fields.count);
scope_reserve(ctx->scope, bft->fields.count);
@@ -1337,7 +1390,7 @@ Type *determine_type_from_polymorphic(CheckerContext *ctx, Type *poly_type, Oper
if (is_polymorphic_type_assignable(ctx, poly_type, operand.type, false, modify_type)) {
if (show_error) {
- set_procedure_abi_types(ctx->allocator, poly_type);
+ set_procedure_abi_types(poly_type);
}
return poly_type;
}
@@ -1463,7 +1516,7 @@ Type *check_get_params(CheckerContext *ctx, Scope *scope, Ast *_params, bool *is
bool success = true;
ast_node(field_list, FieldList, _params);
- Array<Ast *> params = field_list->list;
+ Slice<Ast *> params = field_list->list;
if (params.count == 0) {
if (success_) *success_ = success;
@@ -1496,7 +1549,7 @@ Type *check_get_params(CheckerContext *ctx, Scope *scope, Ast *_params, bool *is
bool is_variadic = false;
isize variadic_index = -1;
bool is_c_vararg = false;
- auto variables = array_make<Entity *>(ctx->allocator, 0, variable_count);
+ auto variables = array_make<Entity *>(permanent_allocator(), 0, variable_count);
for_array(i, params) {
Ast *param = params[i];
if (param->kind != Ast_Field) {
@@ -1822,7 +1875,7 @@ Type *check_get_results(CheckerContext *ctx, Scope *scope, Ast *_results) {
return nullptr;
}
ast_node(field_list, FieldList, _results);
- Array<Ast *> results = field_list->list;
+ Slice<Ast *> results = field_list->list;
if (results.count == 0) {
return nullptr;
@@ -1838,7 +1891,7 @@ Type *check_get_results(CheckerContext *ctx, Scope *scope, Ast *_results) {
}
}
- auto variables = array_make<Entity *>(ctx->allocator, 0, variable_count);
+ auto variables = array_make<Entity *>(permanent_allocator(), 0, variable_count);
for_array(i, results) {
ast_node(field, Field, results[i]);
Ast *default_value = unparen_expr(field->default_value);
@@ -2209,6 +2262,11 @@ Type *type_to_abi_compat_param_type(gbAllocator a, Type *original_type, ProcCall
return new_type;
}
+ if (is_type_proc(original_type)) {
+ // NOTE(bill): Force a cast to prevent a possible type cycle
+ return t_rawptr;
+ }
+
if (cc == ProcCC_None || cc == ProcCC_PureNone || cc == ProcCC_InlineAsm) {
return new_type;
}
@@ -2221,7 +2279,11 @@ Type *type_to_abi_compat_param_type(gbAllocator a, Type *original_type, ProcCall
return new_type;
}
if (build_context.ODIN_ARCH == "amd64") {
- if (is_type_integer_128bit(original_type)) {
+ bool is_128 = is_type_integer_128bit(original_type);
+ if (!is_128 && is_type_bit_set(original_type) && type_size_of(original_type) == 16) {
+ // is_128 = true;
+ }
+ if (is_128) {
if (build_context.ODIN_OS == "windows") {
return alloc_type_simd_vector(2, t_u64);
} else {
@@ -2332,6 +2394,11 @@ Type *type_to_abi_compat_result_type(gbAllocator a, Type *original_type, ProcCal
return new_type;
}
+ if (is_type_proc(single_type)) {
+ // NOTE(bill): Force a cast to prevent a possible type cycle
+ return t_rawptr;
+ }
+
if (is_type_simd_vector(single_type)) {
return new_type;
}
@@ -2445,16 +2512,21 @@ bool abi_compat_return_by_pointer(gbAllocator a, ProcCallingConvention cc, Type
return false;
}
-void set_procedure_abi_types(gbAllocator allocator, Type *type) {
+void set_procedure_abi_types(Type *type) {
type = base_type(type);
if (type->kind != Type_Proc) {
return;
}
- if (type->Proc.abi_types_set) {
+ if (type->Proc.abi_types_set || type->flags & TypeFlag_InProcessOfCheckingABI) {
return;
}
+ gbAllocator allocator = permanent_allocator();
+
+ u32 flags = type->flags;
+ type->flags |= TypeFlag_InProcessOfCheckingABI;
+
type->Proc.abi_compat_params = array_make<Type *>(allocator, cast(isize)type->Proc.param_count);
for (i32 i = 0; i < type->Proc.param_count; i++) {
Entity *e = type->Proc.params->Tuple.variables[i];
@@ -2466,7 +2538,7 @@ void set_procedure_abi_types(gbAllocator allocator, Type *type) {
case ProcCC_Odin:
case ProcCC_Contextless:
case ProcCC_Pure:
- if (is_type_pointer(new_type) & !is_type_pointer(e->type)) {
+ if (is_type_pointer(new_type) && !is_type_pointer(e->type) && !is_type_proc(e->type)) {
e->flags |= EntityFlag_ImplicitReference;
}
break;
@@ -2474,7 +2546,7 @@ void set_procedure_abi_types(gbAllocator allocator, Type *type) {
if (build_context.ODIN_OS == "linux" ||
build_context.ODIN_OS == "darwin") {
- if (is_type_pointer(new_type) & !is_type_pointer(e->type)) {
+ if (is_type_pointer(new_type) & !is_type_pointer(e->type) && !is_type_proc(e->type)) {
e->flags |= EntityFlag_ByVal;
}
}
@@ -2484,13 +2556,13 @@ void set_procedure_abi_types(gbAllocator allocator, Type *type) {
for (i32 i = 0; i < type->Proc.param_count; i++) {
Entity *e = type->Proc.params->Tuple.variables[i];
if (e->kind == Entity_Variable) {
- set_procedure_abi_types(allocator, e->type);
+ set_procedure_abi_types(e->type);
}
}
for (i32 i = 0; i < type->Proc.result_count; i++) {
Entity *e = type->Proc.results->Tuple.variables[i];
if (e->kind == Entity_Variable) {
- set_procedure_abi_types(allocator, e->type);
+ set_procedure_abi_types(e->type);
}
}
@@ -2499,6 +2571,7 @@ void set_procedure_abi_types(gbAllocator allocator, Type *type) {
type->Proc.return_by_pointer = abi_compat_return_by_pointer(allocator, type->Proc.calling_convention, type->Proc.abi_compat_result_type);
type->Proc.abi_types_set = true;
+ type->flags = flags;
}
// NOTE(bill): 'operands' is for generating non generic procedure type
@@ -2711,30 +2784,29 @@ void init_map_entry_type(Type *type) {
if (type->Map.entry_type != nullptr) return;
// NOTE(bill): The preload types may have not been set yet
- GB_ASSERT(t_map_key != nullptr);
- gbAllocator a = heap_allocator();
+ GB_ASSERT(t_map_hash != nullptr);
Type *entry_type = alloc_type_struct();
/*
struct {
- hash: __MapKey;
- next: int;
- key: Key;
- value: Value;
+ hash: runtime.Map_Hash,
+ next: int,
+ key: Key,
+ value: Value,
}
*/
Ast *dummy_node = alloc_ast_node(nullptr, Ast_Invalid);
- Scope *s = create_scope(builtin_pkg->scope, a);
+ Scope *s = create_scope(builtin_pkg->scope);
- auto fields = array_make<Entity *>(a, 0, 3);
- array_add(&fields, alloc_entity_field(s, make_token_ident(str_lit("key")), t_map_key, false, 0, EntityState_Resolved));
- array_add(&fields, alloc_entity_field(s, make_token_ident(str_lit("next")), t_int, false, 1, EntityState_Resolved));
- array_add(&fields, alloc_entity_field(s, make_token_ident(str_lit("value")), type->Map.value, false, 2, EntityState_Resolved));
+ auto fields = array_make<Entity *>(permanent_allocator(), 0, 4);
+ array_add(&fields, alloc_entity_field(s, make_token_ident(str_lit("hash")), t_uintptr, false, cast(i32)fields.count, EntityState_Resolved));
+ array_add(&fields, alloc_entity_field(s, make_token_ident(str_lit("next")), t_int, false, cast(i32)fields.count, EntityState_Resolved));
+ array_add(&fields, alloc_entity_field(s, make_token_ident(str_lit("key")), type->Map.key, false, cast(i32)fields.count, EntityState_Resolved));
+ array_add(&fields, alloc_entity_field(s, make_token_ident(str_lit("value")), type->Map.value, false, cast(i32)fields.count, EntityState_Resolved));
entry_type->Struct.fields = fields;
- // type_set_offsets(a, entry_type);
type->Map.entry_type = entry_type;
}
@@ -2757,15 +2829,14 @@ void init_map_internal_types(Type *type) {
entries: [dynamic]EntryType;
}
*/
- gbAllocator a = heap_allocator();
Ast *dummy_node = alloc_ast_node(nullptr, Ast_Invalid);
- Scope *s = create_scope(builtin_pkg->scope, a);
+ Scope *s = create_scope(builtin_pkg->scope);
Type *hashes_type = alloc_type_slice(t_int);
Type *entries_type = alloc_type_dynamic_array(type->Map.entry_type);
- auto fields = array_make<Entity *>(a, 0, 2);
+ auto fields = array_make<Entity *>(permanent_allocator(), 0, 2);
array_add(&fields, alloc_entity_field(s, make_token_ident(str_lit("hashes")), hashes_type, false, 0, EntityState_Resolved));
array_add(&fields, alloc_entity_field(s, make_token_ident(str_lit("entries")), entries_type, false, 1, EntityState_Resolved));
@@ -2777,6 +2848,47 @@ void init_map_internal_types(Type *type) {
type->Map.lookup_result_type = make_optional_ok_type(value);
}
+void add_map_key_type_dependencies(CheckerContext *ctx, Type *key) {
+ key = core_type(key);
+
+ if (is_type_cstring(key)) {
+ add_package_dependency(ctx, "runtime", "default_hasher_cstring");
+ } else if (is_type_string(key)) {
+ add_package_dependency(ctx, "runtime", "default_hasher_string");
+ } else if (!is_type_polymorphic(key)) {
+ if (!is_type_comparable(key)) {
+ return;
+ }
+
+ if (is_type_simple_compare(key)) {
+ i64 sz = type_size_of(key);
+ if (1 <= sz && sz <= 16) {
+ char buf[20] = {};
+ gb_snprintf(buf, 20, "default_hasher%d", cast(i32)sz);
+ add_package_dependency(ctx, "runtime", buf);
+ return;
+ } else {
+ add_package_dependency(ctx, "runtime", "default_hasher_n");
+ return;
+ }
+ }
+
+ if (key->kind == Type_Struct) {
+ add_package_dependency(ctx, "runtime", "default_hasher_n");
+ for_array(i, key->Struct.fields) {
+ Entity *field = key->Struct.fields[i];
+ add_map_key_type_dependencies(ctx, field->type);
+ }
+ } else if (key->kind == Type_EnumeratedArray) {
+ add_package_dependency(ctx, "runtime", "default_hasher_n");
+ add_map_key_type_dependencies(ctx, key->EnumeratedArray.elem);
+ } else if (key->kind == Type_Array) {
+ add_package_dependency(ctx, "runtime", "default_hasher_n");
+ add_map_key_type_dependencies(ctx, key->Array.elem);
+ }
+ }
+}
+
void check_map_type(CheckerContext *ctx, Type *type, Ast *node) {
GB_ASSERT(type->kind == Type_Map);
ast_node(mt, MapType, node);
@@ -2793,16 +2905,16 @@ void check_map_type(CheckerContext *ctx, Type *type, Ast *node) {
gb_string_free(str);
}
}
+ if (type_size_of(key) == 0) {
+ gbString str = type_to_string(key);
+ error(node, "Invalid type of a key for a map of size 0, got '%s'", str);
+ gb_string_free(str);
+ }
type->Map.key = key;
type->Map.value = value;
- if (is_type_string(key)) {
- add_package_dependency(ctx, "runtime", "default_hash_string");
- } else {
- add_package_dependency(ctx, "runtime", "default_hash_ptr");
- }
-
+ add_map_key_type_dependencies(ctx, key);
init_core_map_type(ctx->checker);
init_map_internal_types(type);
@@ -2833,7 +2945,7 @@ Type *make_soa_struct_fixed(CheckerContext *ctx, Ast *array_typ_expr, Ast *elem_
soa_struct->Struct.soa_elem = elem;
soa_struct->Struct.soa_count = count;
- scope = create_scope(ctx->scope, ctx->allocator);
+ scope = create_scope(ctx->scope);
soa_struct->Struct.scope = scope;
String params_xyzw[4] = {
@@ -2866,7 +2978,7 @@ Type *make_soa_struct_fixed(CheckerContext *ctx, Ast *array_typ_expr, Ast *elem_
soa_struct->Struct.soa_elem = elem;
soa_struct->Struct.soa_count = count;
- scope = create_scope(old_struct->Struct.scope->parent, ctx->allocator);
+ scope = create_scope(old_struct->Struct.scope->parent);
soa_struct->Struct.scope = scope;
for_array(i, old_struct->Struct.fields) {
@@ -2927,7 +3039,7 @@ Type *make_soa_struct_slice(CheckerContext *ctx, Ast *array_typ_expr, Ast *elem_
soa_struct->Struct.soa_count = 0;
soa_struct->Struct.is_polymorphic = true;
- scope = create_scope(ctx->scope, ctx->allocator);
+ scope = create_scope(ctx->scope);
soa_struct->Struct.scope = scope;
} else if (is_type_array(elem)) {
Type *old_array = base_type(elem);
@@ -2941,7 +3053,7 @@ Type *make_soa_struct_slice(CheckerContext *ctx, Ast *array_typ_expr, Ast *elem_
soa_struct->Struct.soa_elem = elem;
soa_struct->Struct.soa_count = 0;
- scope = create_scope(ctx->scope, ctx->allocator);
+ scope = create_scope(ctx->scope);
soa_struct->Struct.scope = scope;
String params_xyzw[4] = {
@@ -2977,7 +3089,7 @@ Type *make_soa_struct_slice(CheckerContext *ctx, Ast *array_typ_expr, Ast *elem_
soa_struct->Struct.soa_elem = elem;
soa_struct->Struct.soa_count = 0;
- scope = create_scope(old_struct->Struct.scope->parent, ctx->allocator);
+ scope = create_scope(old_struct->Struct.scope->parent);
soa_struct->Struct.scope = scope;
for_array(i, old_struct->Struct.fields) {
@@ -3044,7 +3156,7 @@ Type *make_soa_struct_dynamic_array(CheckerContext *ctx, Ast *array_typ_expr, As
soa_struct->Struct.soa_count = 0;
soa_struct->Struct.is_polymorphic = true;
- scope = create_scope(ctx->scope, ctx->allocator);
+ scope = create_scope(ctx->scope);
soa_struct->Struct.scope = scope;
} else if (is_type_array(elem)) {
Type *old_array = base_type(elem);
@@ -3058,7 +3170,7 @@ Type *make_soa_struct_dynamic_array(CheckerContext *ctx, Ast *array_typ_expr, As
soa_struct->Struct.soa_elem = elem;
soa_struct->Struct.soa_count = 0;
- scope = create_scope(ctx->scope, ctx->allocator);
+ scope = create_scope(ctx->scope);
soa_struct->Struct.scope = scope;
String params_xyzw[4] = {
@@ -3093,7 +3205,7 @@ Type *make_soa_struct_dynamic_array(CheckerContext *ctx, Ast *array_typ_expr, As
soa_struct->Struct.soa_elem = elem;
soa_struct->Struct.soa_count = 0;
- scope = create_scope(old_struct->Struct.scope->parent, ctx->allocator);
+ scope = create_scope(old_struct->Struct.scope->parent);
soa_struct->Struct.scope = scope;
for_array(i, old_struct->Struct.fields) {
diff --git a/src/checker.cpp b/src/checker.cpp
index 5c93e12b6..c1a107c15 100644
--- a/src/checker.cpp
+++ b/src/checker.cpp
@@ -187,8 +187,8 @@ void init_decl_info(DeclInfo *d, Scope *scope, DeclInfo *parent) {
array_init (&d->labels, heap_allocator());
}
-DeclInfo *make_decl_info(gbAllocator a, Scope *scope, DeclInfo *parent) {
- DeclInfo *d = gb_alloc_item(a, DeclInfo);
+DeclInfo *make_decl_info(Scope *scope, DeclInfo *parent) {
+ DeclInfo *d = gb_alloc_item(permanent_allocator(), DeclInfo);
init_decl_info(d, scope, parent);
return d;
}
@@ -219,8 +219,8 @@ bool decl_info_has_init(DeclInfo *d) {
-Scope *create_scope(Scope *parent, gbAllocator allocator, isize init_elements_capacity=DEFAULT_SCOPE_CAPACITY) {
- Scope *s = gb_alloc_item(allocator, Scope);
+Scope *create_scope(Scope *parent, isize init_elements_capacity=DEFAULT_SCOPE_CAPACITY) {
+ Scope *s = gb_alloc_item(permanent_allocator(), Scope);
s->parent = parent;
string_map_init(&s->elements, heap_allocator(), init_elements_capacity);
ptr_set_init(&s->imported, heap_allocator(), 0);
@@ -244,7 +244,7 @@ Scope *create_scope_from_file(CheckerContext *c, AstFile *f) {
GB_ASSERT(f->pkg != nullptr);
GB_ASSERT(f->pkg->scope != nullptr);
- Scope *s = create_scope(f->pkg->scope, c->allocator);
+ Scope *s = create_scope(f->pkg->scope);
array_reserve(&s->delayed_imports, f->imports.count);
array_reserve(&s->delayed_directives, f->directive_count);
@@ -264,7 +264,7 @@ Scope *create_scope_from_package(CheckerContext *c, AstPackage *pkg) {
decl_count += pkg->files[i]->decls.count;
}
isize init_elements_capacity = 2*decl_count;
- Scope *s = create_scope(builtin_pkg->scope, c->allocator, init_elements_capacity);
+ Scope *s = create_scope(builtin_pkg->scope, init_elements_capacity);
s->flags |= ScopeFlag_Pkg;
s->pkg = pkg;
@@ -324,7 +324,7 @@ void check_open_scope(CheckerContext *c, Ast *node) {
GB_ASSERT(node->kind == Ast_Invalid ||
is_ast_stmt(node) ||
is_ast_type(node));
- Scope *scope = create_scope(c->scope, c->allocator);
+ Scope *scope = create_scope(c->scope);
add_scope(c, node, scope);
switch (node->kind) {
case Ast_ProcType:
@@ -368,9 +368,14 @@ void scope_lookup_parent(Scope *scope, String const &name, Scope **scope_, Entit
if (e->kind == Entity_Label) {
continue;
}
- if (e->kind == Entity_Variable &&
- !(e->scope->flags&ScopeFlag_File)) {
- continue;
+ if (e->kind == Entity_Variable) {
+ if (e->scope->flags&ScopeFlag_File) {
+ // Global variables are file to access
+ } else if (e->flags&EntityFlag_Static) {
+ // Allow static/thread_local variables to be referenced
+ } else {
+ continue;
+ }
}
}
@@ -690,31 +695,33 @@ void add_global_type_entity(String name, Type *type) {
void init_universal(void) {
BuildContext *bc = &build_context;
+
// NOTE(bill): No need to free these
- gbAllocator a = heap_allocator();
+ // gbAllocator a = heap_allocator();
+ gbAllocator a = permanent_allocator();
builtin_pkg = gb_alloc_item(a, AstPackage);
builtin_pkg->name = str_lit("builtin");
builtin_pkg->kind = Package_Normal;
- builtin_pkg->scope = create_scope(nullptr, a);
- builtin_pkg->scope->flags |= ScopeFlag_Pkg | ScopeFlag_Global;
+ builtin_pkg->scope = create_scope(nullptr);
+ builtin_pkg->scope->flags |= ScopeFlag_Pkg | ScopeFlag_Global | ScopeFlag_Builtin;
builtin_pkg->scope->pkg = builtin_pkg;
intrinsics_pkg = gb_alloc_item(a, AstPackage);
intrinsics_pkg->name = str_lit("intrinsics");
intrinsics_pkg->kind = Package_Normal;
- intrinsics_pkg->scope = create_scope(nullptr, a);
- intrinsics_pkg->scope->flags |= ScopeFlag_Pkg | ScopeFlag_Global;
+ intrinsics_pkg->scope = create_scope(nullptr);
+ intrinsics_pkg->scope->flags |= ScopeFlag_Pkg | ScopeFlag_Global | ScopeFlag_Builtin;
intrinsics_pkg->scope->pkg = intrinsics_pkg;
config_pkg = gb_alloc_item(a, AstPackage);
config_pkg->name = str_lit("config");
config_pkg->kind = Package_Normal;
- config_pkg->scope = create_scope(nullptr, a);
- config_pkg->scope->flags |= ScopeFlag_Pkg | ScopeFlag_Global;
+ config_pkg->scope = create_scope(nullptr);
+ config_pkg->scope->flags |= ScopeFlag_Pkg | ScopeFlag_Global | ScopeFlag_Builtin;
config_pkg->scope->pkg = config_pkg;
@@ -724,6 +731,18 @@ void init_universal(void) {
}
add_global_type_entity(str_lit("byte"), &basic_types[Basic_u8]);
+ {
+ void set_procedure_abi_types(Type *type);
+
+ Type *equal_args[2] = {t_rawptr, t_rawptr};
+ t_equal_proc = alloc_type_proc_from_types(equal_args, 2, t_bool, false, ProcCC_Contextless);
+ set_procedure_abi_types(t_equal_proc);
+
+ Type *hasher_args[2] = {t_rawptr, t_uintptr};
+ t_hasher_proc = alloc_type_proc_from_types(hasher_args, 2, t_uintptr, false, ProcCC_Contextless);
+ set_procedure_abi_types(t_hasher_proc);
+ }
+
// Constants
add_global_constant(str_lit("true"), t_untyped_bool, exact_value_bool(true));
add_global_constant(str_lit("false"), t_untyped_bool, exact_value_bool(false));
@@ -742,6 +761,7 @@ void init_universal(void) {
add_global_constant(str_lit("ODIN_DEFAULT_TO_NIL_ALLOCATOR"), t_untyped_bool, exact_value_bool(bc->ODIN_DEFAULT_TO_NIL_ALLOCATOR));
add_global_constant(str_lit("ODIN_USE_LLVM_API"), t_untyped_bool, exact_value_bool(bc->use_llvm_api));
add_global_constant(str_lit("ODIN_NO_DYNAMIC_LITERALS"), t_untyped_bool, exact_value_bool(bc->no_dynamic_literals));
+ add_global_constant(str_lit("ODIN_TEST"), t_untyped_bool, exact_value_bool(bc->command_kind == Command_test));
// Builtin Procedures
@@ -837,6 +857,8 @@ void init_checker_info(CheckerInfo *i) {
array_init(&i->variable_init_order, a);
array_init(&i->required_foreign_imports_through_force, a);
array_init(&i->required_global_variables, a);
+ array_init(&i->testing_procedures, a, 0, 0);
+
i->allow_identifier_uses = build_context.query_data_set_settings.kind == QueryDataSet_GoToDefinitions;
if (i->allow_identifier_uses) {
@@ -870,7 +892,6 @@ CheckerContext make_checker_context(Checker *c) {
CheckerContext ctx = c->init_ctx;
ctx.checker = c;
ctx.info = &c->info;
- ctx.allocator = c->allocator;
ctx.scope = builtin_pkg->scope;
ctx.pkg = builtin_pkg;
@@ -895,6 +916,7 @@ bool init_checker(Checker *c, Parser *parser) {
gbAllocator a = heap_allocator();
init_checker_info(&c->info);
+ c->info.checker = c;
array_init(&c->procs_to_check, a);
array_init(&c->procs_with_deferred_to_check, a);
@@ -904,8 +926,6 @@ bool init_checker(Checker *c, Parser *parser) {
isize total_token_count = c->parser->total_token_count;
isize arena_size = 2 * item_size * total_token_count;
- c->allocator = heap_allocator();
-
c->init_ctx = make_checker_context(c);
return true;
}
@@ -1502,11 +1522,10 @@ void add_min_dep_type_info(Checker *c, Type *t) {
ti_index = type_info_index(&c->info, t, false);
}
GB_ASSERT(ti_index >= 0);
- if (ptr_set_exists(set, ti_index)) {
+ if (ptr_set_update(set, ti_index)) {
// Type Already exists
return;
}
- ptr_set_add(set, ti_index);
// Add nested types
if (t->kind == Type_Named) {
@@ -1680,8 +1699,6 @@ void add_dependency_to_set(Checker *c, Entity *entity) {
CheckerInfo *info = &c->info;
auto *set = &info->minimum_dependency_set;
- String name = entity->token.string;
-
if (entity->type != nullptr &&
is_type_polymorphic(entity->type)) {
@@ -1691,12 +1708,10 @@ void add_dependency_to_set(Checker *c, Entity *entity) {
}
}
- if (ptr_set_exists(set, entity)) {
+ if (ptr_set_update(set, entity)) {
return;
}
-
- ptr_set_add(set, entity);
DeclInfo *decl = decl_info_of_entity(entity);
if (decl == nullptr) {
return;
@@ -1715,16 +1730,15 @@ void add_dependency_to_set(Checker *c, Entity *entity) {
if (fl != nullptr) {
GB_ASSERT_MSG(fl->kind == Entity_LibraryName &&
(fl->flags&EntityFlag_Used),
- "%.*s", LIT(name));
+ "%.*s", LIT(entity->token.string));
add_dependency_to_set(c, fl);
}
- }
- if (e->kind == Entity_Variable && e->Variable.is_foreign) {
+ } else if (e->kind == Entity_Variable && e->Variable.is_foreign) {
Entity *fl = e->Variable.foreign_library;
if (fl != nullptr) {
GB_ASSERT_MSG(fl->kind == Entity_LibraryName &&
(fl->flags&EntityFlag_Used),
- "%.*s", LIT(name));
+ "%.*s", LIT(entity->token.string));
add_dependency_to_set(c, fl);
}
}
@@ -1733,7 +1747,10 @@ void add_dependency_to_set(Checker *c, Entity *entity) {
void generate_minimum_dependency_set(Checker *c, Entity *start) {
- ptr_set_init(&c->info.minimum_dependency_set, heap_allocator());
+ isize entity_count = c->info.entities.count;
+ isize min_dep_set_cap = next_pow2_isize(entity_count*4); // empirically determined factor
+
+ ptr_set_init(&c->info.minimum_dependency_set, heap_allocator(), min_dep_set_cap);
ptr_set_init(&c->info.minimum_dependency_type_info_set, heap_allocator());
String required_runtime_entities[] = {
@@ -1769,6 +1786,7 @@ void generate_minimum_dependency_set(Checker *c, Entity *start) {
str_lit("memcpy"),
str_lit("memmove"),
+ str_lit("memory_equal"),
str_lit("memory_compare"),
str_lit("memory_compare_zero"),
@@ -1842,7 +1860,68 @@ void generate_minimum_dependency_set(Checker *c, Entity *start) {
add_dependency_to_set(c, e);
}
- add_dependency_to_set(c, start);
+ for_array(i, c->info.entities) {
+ Entity *e = c->info.entities[i];
+ switch (e->kind) {
+ case Entity_Variable:
+ if (e->Variable.is_export) {
+ add_dependency_to_set(c, e);
+ }
+ break;
+ case Entity_Procedure:
+ if (e->Procedure.is_export) {
+ add_dependency_to_set(c, e);
+ }
+ break;
+ }
+ }
+
+ if (build_context.command_kind == Command_test) {
+ AstPackage *pkg = c->info.init_package;
+ Scope *s = pkg->scope;
+ for_array(i, s->elements.entries) {
+ Entity *e = s->elements.entries[i].value;
+ if (e->kind != Entity_Procedure) {
+ continue;
+ }
+
+ if (e->file == nullptr || !e->file->is_test) {
+ continue;
+ }
+
+ String name = e->token.string;
+ String prefix = str_lit("test_");
+
+ if (!string_starts_with(name, prefix)) {
+ continue;
+ }
+
+ bool is_tester = false;
+ if (name != prefix) {
+ is_tester = true;
+ } else {
+ error(e->token, "Invalid testing procedure name: %.*s", LIT(name));
+ }
+
+ Type *t = base_type(e->type);
+ GB_ASSERT(t->kind == Type_Proc);
+ if (t->Proc.param_count == 0 && t->Proc.result_count == 0) {
+ // Good
+ } else {
+ gbString str = type_to_string(t);
+ error(e->token, "Testing procedures must have a signature type of proc(), got %s", str);
+ gb_string_free(str);
+ is_tester = false;
+ }
+
+ if (is_tester) {
+ add_dependency_to_set(c, e);
+ array_add(&c->info.testing_procedures, e);
+ }
+ }
+ } else {
+ add_dependency_to_set(c, start);
+ }
}
bool is_entity_a_dependency(Entity *e) {
@@ -1881,19 +1960,17 @@ void add_entity_dependency_from_procedure_parameters(Map<EntityGraphNode *> *M,
}
-Array<EntityGraphNode *> generate_entity_dependency_graph(CheckerInfo *info) {
+Array<EntityGraphNode *> generate_entity_dependency_graph(CheckerInfo *info, gbAllocator allocator) {
#define TIME_SECTION(str) do { if (build_context.show_more_timings) timings_start_section(&global_timings, str_lit(str)); } while (0)
- gbAllocator a = heap_allocator();
-
Map<EntityGraphNode *> M = {}; // Key: Entity *
- map_init(&M, a, info->entities.count);
+ map_init(&M, allocator, info->entities.count);
defer (map_destroy(&M));
for_array(i, info->entities) {
Entity *e = info->entities[i];
DeclInfo *d = e->decl_info;
if (is_entity_a_dependency(e)) {
- EntityGraphNode *n = gb_alloc_item(a, EntityGraphNode);
+ EntityGraphNode *n = gb_alloc_item(allocator, EntityGraphNode);
n->entity = e;
map_set(&M, hash_pointer(e), n);
}
@@ -1928,7 +2005,7 @@ Array<EntityGraphNode *> generate_entity_dependency_graph(CheckerInfo *info) {
// This means that the entity graph node set will have to be thread safe
TIME_SECTION("generate_entity_dependency_graph: Calculate edges for graph M - Part 2");
- auto G = array_make<EntityGraphNode *>(a, 0, M.entries.count);
+ auto G = array_make<EntityGraphNode *>(allocator, 0, M.entries.count);
for_array(i, M.entries) {
auto *entry = &M.entries[i];
@@ -1949,17 +2026,27 @@ Array<EntityGraphNode *> generate_entity_dependency_graph(CheckerInfo *info) {
EntityGraphNode *s = n->succ.entries[k].ptr;
// Ignore self-cycles
if (s != n) {
+ if (p->entity->kind == Entity_Procedure &&
+ s->entity->kind == Entity_Procedure) {
+ // NOTE(bill, 2020-11-15): Only care about variable initialization ordering
+ // TODO(bill): This is probably wrong!!!!
+ continue;
+ }
+ // IMPORTANT NOTE/TODO(bill, 2020-11-15): These three calls take the majority of the
+ // the time to process
+
entity_graph_node_set_add(&p->succ, s);
entity_graph_node_set_add(&s->pred, p);
// Remove edge to 'n'
entity_graph_node_set_remove(&s->pred, n);
}
}
+
// Remove edge to 'n'
entity_graph_node_set_remove(&p->succ, n);
}
}
- } else {
+ } else if (e->kind == Entity_Variable) {
array_add(&G, n);
}
}
@@ -1972,6 +2059,28 @@ Array<EntityGraphNode *> generate_entity_dependency_graph(CheckerInfo *info) {
GB_ASSERT(n->dep_count >= 0);
}
+ // f64 succ_count = 0.0;
+ // f64 pred_count = 0.0;
+ // f64 succ_capacity = 0.0;
+ // f64 pred_capacity = 0.0;
+ // f64 succ_max = 0.0;
+ // f64 pred_max = 0.0;
+ // for_array(i, G) {
+ // EntityGraphNode *n = G[i];
+ // succ_count += n->succ.entries.count;
+ // pred_count += n->pred.entries.count;
+ // succ_capacity += n->succ.entries.capacity;
+ // pred_capacity += n->pred.entries.capacity;
+
+ // succ_max = gb_max(succ_max, n->succ.entries.capacity);
+ // pred_max = gb_max(pred_max, n->pred.entries.capacity);
+
+ // }
+ // f64 count = cast(f64)G.count;
+ // gb_printf_err(">>>count pred: %f succ: %f\n", pred_count/count, succ_count/count);
+ // gb_printf_err(">>>capacity pred: %f succ: %f\n", pred_capacity/count, succ_capacity/count);
+ // gb_printf_err(">>>max pred: %f succ: %f\n", pred_max, succ_max);
+
return G;
#undef TIME_SECTION
@@ -2088,9 +2197,9 @@ void init_core_type_info(Checker *c) {
t_type_info_enum_value = type_info_enum_value->type;
t_type_info_enum_value_ptr = alloc_type_pointer(t_type_info_enum_value);
- GB_ASSERT(tis->fields.count == 4);
+ GB_ASSERT(tis->fields.count == 5);
- Entity *type_info_variant = tis->fields[3];
+ Entity *type_info_variant = tis->fields[4];
Type *tiv_type = type_info_variant->type;
GB_ASSERT(is_type_union(tiv_type));
@@ -2186,14 +2295,14 @@ void init_core_source_code_location(Checker *c) {
}
void init_core_map_type(Checker *c) {
- if (t_map_key == nullptr) {
- Entity *e = find_core_entity(c, str_lit("Map_Key"));
+ if (t_map_hash == nullptr) {
+ Entity *e = find_core_entity(c, str_lit("Map_Hash"));
if (e->state == EntityState_Unresolved) {
auto ctx = c->init_ctx;
check_entity_decl(&ctx, e, nullptr, nullptr);
}
- t_map_key = e->type;
- GB_ASSERT(t_map_key != nullptr);
+ t_map_hash = e->type;
+ GB_ASSERT(t_map_hash != nullptr);
}
if (t_map_header == nullptr) {
@@ -2579,7 +2688,7 @@ DECL_ATTRIBUTE_PROC(type_decl_attribute) {
if (valid && build_context.use_llvm_api) {
if (ac->atom_op_table == nullptr) {
- ac->atom_op_table = gb_alloc_item(heap_allocator(), TypeAtomOpTable);
+ ac->atom_op_table = gb_alloc_item(permanent_allocator(), TypeAtomOpTable);
}
ac->atom_op_table->op[TypeAtomOp_index_get] = e;
}
@@ -2638,7 +2747,7 @@ DECL_ATTRIBUTE_PROC(type_decl_attribute) {
if (valid && build_context.use_llvm_api) {
if (ac->atom_op_table == nullptr) {
- ac->atom_op_table = gb_alloc_item(heap_allocator(), TypeAtomOpTable);
+ ac->atom_op_table = gb_alloc_item(permanent_allocator(), TypeAtomOpTable);
}
ac->atom_op_table->op[TypeAtomOp_index_set] = e;
}
@@ -2720,7 +2829,7 @@ DECL_ATTRIBUTE_PROC(type_decl_attribute) {
if (valid && build_context.use_llvm_api) {
if (ac->atom_op_table == nullptr) {
- ac->atom_op_table = gb_alloc_item(heap_allocator(), TypeAtomOpTable);
+ ac->atom_op_table = gb_alloc_item(permanent_allocator(), TypeAtomOpTable);
}
ac->atom_op_table->op[TypeAtomOp_slice] = e;
}
@@ -2809,7 +2918,7 @@ void check_decl_attributes(CheckerContext *c, Array<Ast *> const &attributes, De
}
-isize get_total_value_count(Array<Ast *> const &values) {
+isize get_total_value_count(Slice<Ast *> const &values) {
isize count = 0;
for_array(i, values) {
Type *t = type_of_expr(values[i]);
@@ -2967,8 +3076,8 @@ void check_builtin_attributes(CheckerContext *ctx, Entity *e, Array<Ast *> *attr
}
void check_collect_value_decl(CheckerContext *c, Ast *decl) {
- if (decl->been_handled) return;
- decl->been_handled = true;
+ if (decl->state_flags & StateFlag_BeenHandled) return;
+ decl->state_flags |= StateFlag_BeenHandled;
ast_node(vd, ValueDecl, decl);
@@ -3023,7 +3132,7 @@ void check_collect_value_decl(CheckerContext *c, Ast *decl) {
} else {
entity_visibility_kind = kind;
}
- array_unordered_remove(elems, j);
+ slice_unordered_remove(elems, j);
j -= 1;
}
}
@@ -3072,7 +3181,10 @@ void check_collect_value_decl(CheckerContext *c, Ast *decl) {
}
Ast *init_expr = value;
- DeclInfo *d = make_decl_info(heap_allocator(), c->scope, c->decl);
+ DeclInfo *d = make_decl_info(c->scope, c->decl);
+ d->decl_node = decl;
+ d->comment = vd->comment;
+ d->docs = vd->docs;
d->entity = e;
d->type_expr = vd->type;
d->init_expr = init_expr;
@@ -3100,9 +3212,12 @@ void check_collect_value_decl(CheckerContext *c, Ast *decl) {
Token token = name->Ident.token;
Ast *fl = c->foreign_context.curr_library;
- DeclInfo *d = make_decl_info(c->allocator, c->scope, c->decl);
Entity *e = nullptr;
+ DeclInfo *d = make_decl_info(c->scope, c->decl);
+ d->decl_node = decl;
+ d->comment = vd->comment;
+ d->docs = vd->docs;
d->attributes = vd->attributes;
d->type_expr = vd->type;
d->init_expr = init;
@@ -3186,8 +3301,8 @@ void check_collect_value_decl(CheckerContext *c, Ast *decl) {
}
void check_add_foreign_block_decl(CheckerContext *ctx, Ast *decl) {
- if (decl->been_handled) return;
- decl->been_handled = true;
+ if (decl->state_flags & StateFlag_BeenHandled) return;
+ decl->state_flags |= StateFlag_BeenHandled;
ast_node(fb, ForeignBlockDecl, decl);
Ast *foreign_library = fb->foreign_library;
@@ -3207,7 +3322,7 @@ void check_add_foreign_block_decl(CheckerContext *ctx, Ast *decl) {
}
// NOTE(bill): If file_scopes == nullptr, this will act like a local scope
-void check_collect_entities(CheckerContext *c, Array<Ast *> const &nodes) {
+void check_collect_entities(CheckerContext *c, Slice<Ast *> const &nodes) {
for_array(decl_index, nodes) {
Ast *decl = nodes[decl_index];
if (!is_ast_decl(decl) && !is_ast_when_stmt(decl)) {
@@ -3522,11 +3637,9 @@ struct ImportPathItem {
Array<ImportPathItem> find_import_path(Checker *c, AstPackage *start, AstPackage *end, PtrSet<AstPackage *> *visited) {
Array<ImportPathItem> empty_path = {};
- if (ptr_set_exists(visited, start)) {
+ if (ptr_set_update(visited, start)) {
return empty_path;
}
- ptr_set_add(visited, start);
-
String path = start->fullpath;
AstPackage **found = string_map_get(&c->info.packages, path);
@@ -3571,8 +3684,8 @@ Array<ImportPathItem> find_import_path(Checker *c, AstPackage *start, AstPackage
}
#endif
void check_add_import_decl(CheckerContext *ctx, Ast *decl) {
- if (decl->been_handled) return;
- decl->been_handled = true;
+ if (decl->state_flags & StateFlag_BeenHandled) return;
+ decl->state_flags |= StateFlag_BeenHandled;
ast_node(id, ImportDecl, decl);
Token token = id->relpath;
@@ -3612,10 +3725,8 @@ void check_add_import_decl(CheckerContext *ctx, Ast *decl) {
GB_ASSERT(scope->flags&ScopeFlag_Pkg);
- if (ptr_set_exists(&parent_scope->imported, scope)) {
+ if (ptr_set_update(&parent_scope->imported, scope)) {
// error(token, "Multiple import of the same file within this scope");
- } else {
- ptr_set_add(&parent_scope->imported, scope);
}
String import_name = path_to_entity_name(id->import_name.string, id->fullpath, false);
@@ -3686,8 +3797,8 @@ DECL_ATTRIBUTE_PROC(foreign_import_decl_attribute) {
}
void check_add_foreign_import_decl(CheckerContext *ctx, Ast *decl) {
- if (decl->been_handled) return;
- decl->been_handled = true;
+ if (decl->state_flags & StateFlag_BeenHandled) return;
+ decl->state_flags |= StateFlag_BeenHandled;
ast_node(fl, ForeignImportDecl, decl);
@@ -3738,7 +3849,7 @@ void check_add_foreign_import_decl(CheckerContext *ctx, Ast *decl) {
}
}
-bool collect_checked_packages_from_decl_list(Checker *c, Array<Ast *> const &decls) {
+bool collect_checked_packages_from_decl_list(Checker *c, Slice<Ast *> const &decls) {
bool new_files = false;
for_array(i, decls) {
Ast *decl = decls[i];
@@ -3760,7 +3871,7 @@ bool collect_checked_packages_from_decl_list(Checker *c, Array<Ast *> const &dec
}
// Returns true if a new package is present
-bool collect_file_decls(CheckerContext *ctx, Array<Ast *> const &decls);
+bool collect_file_decls(CheckerContext *ctx, Slice<Ast *> const &decls);
bool collect_file_decls_from_when_stmt(CheckerContext *ctx, AstWhenStmt *ws);
bool collect_when_stmt_from_file(CheckerContext *ctx, AstWhenStmt *ws) {
@@ -3835,7 +3946,7 @@ bool collect_file_decls_from_when_stmt(CheckerContext *ctx, AstWhenStmt *ws) {
return false;
}
-bool collect_file_decls(CheckerContext *ctx, Array<Ast *> const &decls) {
+bool collect_file_decls(CheckerContext *ctx, Slice<Ast *> const &decls) {
GB_ASSERT(ctx->scope->flags&ScopeFlag_File);
if (collect_checked_packages_from_decl_list(ctx->checker, decls)) {
@@ -3968,10 +4079,9 @@ void check_import_entities(Checker *c) {
if (pkg == nullptr) {
continue;
}
- if (ptr_set_exists(&emitted, pkg)) {
+ if (ptr_set_update(&emitted, pkg)) {
continue;
}
- ptr_set_add(&emitted, pkg);
array_add(&package_order, n);
}
@@ -4162,7 +4272,7 @@ void calculate_global_init_order(Checker *c) {
CheckerInfo *info = &c->info;
TIME_SECTION("calculate_global_init_order: generate entity dependency graph");
- Array<EntityGraphNode *> dep_graph = generate_entity_dependency_graph(info);
+ Array<EntityGraphNode *> dep_graph = generate_entity_dependency_graph(info, heap_allocator());
defer ({
for_array(i, dep_graph) {
entity_graph_node_destroy(dep_graph[i], heap_allocator());
@@ -4214,11 +4324,9 @@ void calculate_global_init_order(Checker *c) {
// if (!decl_info_has_init(d)) {
// continue;
// }
- if (ptr_set_exists(&emitted, d)) {
+ if (ptr_set_update(&emitted, d)) {
continue;
}
- ptr_set_add(&emitted, d);
-
array_add(&info->variable_init_order, d);
}
@@ -4299,10 +4407,11 @@ void check_parsed_files(Checker *c) {
for_array(i, c->parser->packages) {
AstPackage *p = c->parser->packages[i];
Scope *scope = create_scope_from_package(&c->init_ctx, p);
- p->decl_info = make_decl_info(c->allocator, scope, c->init_ctx.decl);
+ p->decl_info = make_decl_info(scope, c->init_ctx.decl);
string_map_set(&c->info.packages, p->fullpath, p);
if (scope->flags&ScopeFlag_Init) {
+ c->info.init_package = p;
c->info.init_scope = scope;
}
if (p->kind == Package_Runtime) {
@@ -4572,7 +4681,7 @@ void check_parsed_files(Checker *c) {
TIME_SECTION("check entry point");
- if (build_context.build_mode == BuildMode_Executable) {
+ if (build_context.build_mode == BuildMode_Executable && !build_context.no_entry_point && build_context.command_kind != Command_test) {
Scope *s = c->info.init_scope;
GB_ASSERT(s != nullptr);
GB_ASSERT(s->flags&ScopeFlag_Init);
diff --git a/src/checker.hpp b/src/checker.hpp
index 3d21a4cec..168c00d33 100644
--- a/src/checker.hpp
+++ b/src/checker.hpp
@@ -45,7 +45,7 @@ enum StmtFlag {
Stmt_TypeSwitch = 1<<4,
- Stmt_CheckScopeDecls = 1<<5,
+ Stmt_CheckScopeDecls = 1<<5,
};
enum BuiltinProcPkg {
@@ -132,6 +132,7 @@ struct DeclInfo {
Entity *entity;
+ Ast * decl_node;
Ast * type_expr;
Ast * init_expr;
Array<Ast *> attributes;
@@ -140,6 +141,9 @@ struct DeclInfo {
bool is_using;
bool where_clauses_evaluated;
+ CommentGroup *comment;
+ CommentGroup *docs;
+
PtrSet<Entity *> deps;
PtrSet<Type *> type_info_deps;
Array<BlockLabel> labels;
@@ -160,12 +164,13 @@ struct ProcInfo {
enum ScopeFlag : i32 {
- ScopeFlag_Pkg = 1<<1,
- ScopeFlag_Global = 1<<2,
- ScopeFlag_File = 1<<3,
- ScopeFlag_Init = 1<<4,
- ScopeFlag_Proc = 1<<5,
- ScopeFlag_Type = 1<<6,
+ ScopeFlag_Pkg = 1<<1,
+ ScopeFlag_Builtin = 1<<2,
+ ScopeFlag_Global = 1<<3,
+ ScopeFlag_File = 1<<4,
+ ScopeFlag_Init = 1<<5,
+ ScopeFlag_Proc = 1<<6,
+ ScopeFlag_Type = 1<<7,
ScopeFlag_HasBeenImported = 1<<10, // This is only applicable to file scopes
@@ -247,8 +252,12 @@ struct AtomOpMapEntry {
};
+struct CheckerContext;
+
// CheckerInfo stores all the symbol information for a type-checked program
struct CheckerInfo {
+ Checker *checker;
+
Map<ExprInfo> untyped; // Key: Ast * | Expression -> ExprInfo
// NOTE(bill): This needs to be a map and not on the Ast
// as it needs to be iterated across
@@ -268,6 +277,7 @@ struct CheckerInfo {
AstPackage * builtin_package;
AstPackage * runtime_package;
+ AstPackage * init_package;
Scope * init_scope;
Entity * entry_point;
PtrSet<Entity *> minimum_dependency_set;
@@ -278,6 +288,7 @@ struct CheckerInfo {
Map<AtomOpMapEntry> atom_op_map; // Key: Ast *
+ Array<Entity *> testing_procedures;
bool allow_identifier_uses;
Array<Ast *> identifier_uses; // only used by 'odin query'
@@ -301,7 +312,6 @@ struct CheckerContext {
ProcCallingConvention curr_proc_calling_convention;
bool in_proc_sig;
ForeignContext foreign_context;
- gbAllocator allocator;
CheckerTypePath *type_path;
isize type_level; // TODO(bill): Actually handle correctly
@@ -317,6 +327,7 @@ struct CheckerContext {
bool no_polymorphic_errors;
bool hide_polymorphic_errors;
bool in_polymorphic_specialization;
+ bool allow_arrow_right_selector_expr;
Scope * polymorphic_scope;
Ast *assignment_lhs_hint;
@@ -331,14 +342,11 @@ struct Checker {
Array<Entity *> procs_with_deferred_to_check;
CheckerContext *curr_ctx;
- gbAllocator allocator;
CheckerContext init_ctx;
};
-
-
gb_global AstPackage *builtin_pkg = nullptr;
gb_global AstPackage *intrinsics_pkg = nullptr;
gb_global AstPackage *config_pkg = nullptr;
@@ -387,7 +395,7 @@ void check_add_foreign_import_decl(CheckerContext *c, Ast *decl);
bool check_arity_match(CheckerContext *c, AstValueDecl *vd, bool is_global = false);
-void check_collect_entities(CheckerContext *c, Array<Ast *> const &nodes);
+void check_collect_entities(CheckerContext *c, Slice<Ast *> const &nodes);
void check_collect_entities_from_when_stmt(CheckerContext *c, AstWhenStmt *ws);
void check_delayed_file_import_entity(CheckerContext *c, Ast *decl);
diff --git a/src/checker_builtin_procs.hpp b/src/checker_builtin_procs.hpp
index d0e009cc0..b2157b3c1 100644
--- a/src/checker_builtin_procs.hpp
+++ b/src/checker_builtin_procs.hpp
@@ -183,6 +183,9 @@ BuiltinProc__type_simple_boolean_end,
BuiltinProc_type_field_index_of,
+ BuiltinProc_type_equal_proc,
+ BuiltinProc_type_hasher_proc,
+
BuiltinProc__type_end,
@@ -367,5 +370,8 @@ gb_global BuiltinProc builtin_procs[BuiltinProc_COUNT] = {
{STR_LIT("type_field_index_of"), 2, false, Expr_Expr, BuiltinProcPkg_intrinsics},
+ {STR_LIT("type_equal_proc"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
+ {STR_LIT("type_hasher_proc"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
+
{STR_LIT(""), 0, false, Expr_Stmt, BuiltinProcPkg_intrinsics},
};
diff --git a/src/common.cpp b/src/common.cpp
index 350127e1e..0147f27d5 100644
--- a/src/common.cpp
+++ b/src/common.cpp
@@ -56,6 +56,14 @@ gb_inline isize align_formula_isize(isize size, isize align) {
}
return size;
}
+gb_inline void *align_formula_ptr(void *ptr, isize align) {
+ if (align > 0) {
+ uintptr result = (cast(uintptr)ptr) + align-1;
+ return (void *)(result - result%align);
+ }
+ return ptr;
+}
+
GB_ALLOCATOR_PROC(heap_allocator_proc);
@@ -373,13 +381,16 @@ typedef struct Arena {
gbAllocator backing;
isize block_size;
gbMutex mutex;
-
isize total_used;
+ bool use_mutex;
} Arena;
#define ARENA_MIN_ALIGNMENT 16
#define ARENA_DEFAULT_BLOCK_SIZE (8*1024*1024)
+
+gb_global Arena permanent_arena = {};
+
void arena_init(Arena *arena, gbAllocator backing, isize block_size=ARENA_DEFAULT_BLOCK_SIZE) {
arena->backing = backing;
arena->block_size = block_size;
@@ -388,8 +399,9 @@ void arena_init(Arena *arena, gbAllocator backing, isize block_size=ARENA_DEFAUL
}
void arena_grow(Arena *arena, isize min_size) {
- // gb_mutex_lock(&arena->mutex);
- // defer (gb_mutex_unlock(&arena->mutex));
+ if (arena->use_mutex) {
+ gb_mutex_lock(&arena->mutex);
+ }
isize size = gb_max(arena->block_size, min_size);
size = ALIGN_UP(size, ARENA_MIN_ALIGNMENT);
@@ -399,11 +411,16 @@ void arena_grow(Arena *arena, isize min_size) {
GB_ASSERT(arena->ptr == ALIGN_DOWN_PTR(arena->ptr, ARENA_MIN_ALIGNMENT));
arena->end = arena->ptr + size;
array_add(&arena->blocks, arena->ptr);
+
+ if (arena->use_mutex) {
+ gb_mutex_unlock(&arena->mutex);
+ }
}
void *arena_alloc(Arena *arena, isize size, isize alignment) {
- // gb_mutex_lock(&arena->mutex);
- // defer (gb_mutex_unlock(&arena->mutex));
+ if (arena->use_mutex) {
+ gb_mutex_lock(&arena->mutex);
+ }
arena->total_used += size;
@@ -419,12 +436,17 @@ void *arena_alloc(Arena *arena, isize size, isize alignment) {
GB_ASSERT(arena->ptr <= arena->end);
GB_ASSERT(ptr == ALIGN_DOWN_PTR(ptr, align));
// zero_size(ptr, size);
+
+ if (arena->use_mutex) {
+ gb_mutex_unlock(&arena->mutex);
+ }
return ptr;
}
void arena_free_all(Arena *arena) {
- // gb_mutex_lock(&arena->mutex);
- // defer (gb_mutex_unlock(&arena->mutex));
+ if (arena->use_mutex) {
+ gb_mutex_lock(&arena->mutex);
+ }
for_array(i, arena->blocks) {
gb_free(arena->backing, arena->blocks[i]);
@@ -432,8 +454,11 @@ void arena_free_all(Arena *arena) {
array_clear(&arena->blocks);
arena->ptr = nullptr;
arena->end = nullptr;
-}
+ if (arena->use_mutex) {
+ gb_mutex_unlock(&arena->mutex);
+ }
+}
@@ -460,7 +485,14 @@ GB_ALLOCATOR_PROC(arena_allocator_proc) {
// GB_PANIC("gbAllocation_Free not supported");
break;
case gbAllocation_Resize:
- GB_PANIC("gbAllocation_Resize: not supported");
+ if (size == 0) {
+ ptr = nullptr;
+ } else if (size <= old_size) {
+ ptr = old_memory;
+ } else {
+ ptr = arena_alloc(arena, size, alignment);
+ gb_memmove(ptr, old_memory, old_size);
+ }
break;
case gbAllocation_FreeAll:
arena_free_all(arena);
@@ -471,6 +503,97 @@ GB_ALLOCATOR_PROC(arena_allocator_proc) {
}
+gbAllocator permanent_allocator() {
+ return arena_allocator(&permanent_arena);
+ // return heap_allocator();
+}
+
+
+
+struct Temp_Allocator {
+ u8 *data;
+ isize len;
+ isize curr_offset;
+ gbAllocator backup_allocator;
+ Array<void *> leaked_allocations;
+};
+
+gb_global Temp_Allocator temporary_allocator_data = {};
+
+void temp_allocator_init(Temp_Allocator *s, isize size) {
+ s->backup_allocator = heap_allocator();
+ s->data = cast(u8 *)gb_alloc_align(s->backup_allocator, size, 16);
+ s->curr_offset = 0;
+ s->leaked_allocations.allocator = s->backup_allocator;
+}
+
+void *temp_allocator_alloc(Temp_Allocator *s, isize size, isize alignment) {
+ size = align_formula_isize(size, alignment);
+ if (s->curr_offset+size <= s->len) {
+ u8 *start = s->data;
+ u8 *ptr = start + s->curr_offset;
+ ptr = cast(u8 *)align_formula_ptr(ptr, alignment);
+ // assume memory is zero
+
+ isize offset = ptr - start;
+ s->curr_offset = offset + size;
+ return ptr;
+ } else if (size <= s->len) {
+ u8 *start = s->data;
+ u8 *ptr = cast(u8 *)align_formula_ptr(start, alignment);
+ // assume memory is zero
+
+ isize offset = ptr - start;
+ s->curr_offset = offset + size;
+ return ptr;
+ }
+
+ void *ptr = gb_alloc_align(s->backup_allocator, size, alignment);
+ array_add(&s->leaked_allocations, ptr);
+ return ptr;
+}
+
+void temp_allocator_free_all(Temp_Allocator *s) {
+ s->curr_offset = 0;
+ for_array(i, s->leaked_allocations) {
+ gb_free(s->backup_allocator, s->leaked_allocations[i]);
+ }
+ array_clear(&s->leaked_allocations);
+ gb_zero_size(s->data, s->len);
+}
+
+GB_ALLOCATOR_PROC(temp_allocator_proc) {
+ void *ptr = nullptr;
+ Temp_Allocator *s = cast(Temp_Allocator *)allocator_data;
+ GB_ASSERT_NOT_NULL(s);
+
+ switch (type) {
+ case gbAllocation_Alloc:
+ return temp_allocator_alloc(s, size, alignment);
+ case gbAllocation_Free:
+ break;
+ case gbAllocation_Resize:
+ if (size == 0) {
+ ptr = nullptr;
+ } else if (size <= old_size) {
+ ptr = old_memory;
+ } else {
+ ptr = temp_allocator_alloc(s, size, alignment);
+ gb_memmove(ptr, old_memory, old_size);
+ }
+ break;
+ case gbAllocation_FreeAll:
+ temp_allocator_free_all(s);
+ break;
+ }
+
+ return ptr;
+}
+
+
+gbAllocator temporary_allocator() {
+ return {temp_allocator_proc, &temporary_allocator_data};
+}
diff --git a/src/docs.cpp b/src/docs.cpp
index 3bc0da649..aa1b89560 100644
--- a/src/docs.cpp
+++ b/src/docs.cpp
@@ -1,22 +1,102 @@
// Generates Documentation
-gbString expr_to_string(Ast *expression);
+gb_global int print_entity_kind_ordering[Entity_Count] = {
+ /*Invalid*/ -1,
+ /*Constant*/ 0,
+ /*Variable*/ 1,
+ /*TypeName*/ 4,
+ /*Procedure*/ 2,
+ /*ProcGroup*/ 3,
+ /*Builtin*/ -1,
+ /*ImportName*/ -1,
+ /*LibraryName*/ -1,
+ /*Nil*/ -1,
+ /*Label*/ -1,
+};
+gb_global char const *print_entity_names[Entity_Count] = {
+ /*Invalid*/ "",
+ /*Constant*/ "constants",
+ /*Variable*/ "variables",
+ /*TypeName*/ "types",
+ /*Procedure*/ "procedures",
+ /*ProcGroup*/ "proc_group",
+ /*Builtin*/ "",
+ /*ImportName*/ "import names",
+ /*LibraryName*/ "library names",
+ /*Nil*/ "",
+ /*Label*/ "",
+};
-String alloc_comment_group_string(gbAllocator a, CommentGroup g) {
+
+GB_COMPARE_PROC(cmp_entities_for_printing) {
+ GB_ASSERT(a != nullptr);
+ GB_ASSERT(b != nullptr);
+ Entity *x = *cast(Entity **)a;
+ Entity *y = *cast(Entity **)b;
+ int res = 0;
+ res = string_compare(x->pkg->name, y->pkg->name);
+ if (res != 0) {
+ return res;
+ }
+ int ox = print_entity_kind_ordering[x->kind];
+ int oy = print_entity_kind_ordering[y->kind];
+ res = ox - oy;
+ if (res != 0) {
+ return res;
+ }
+ res = string_compare(x->token.string, y->token.string);
+ return res;
+}
+
+GB_COMPARE_PROC(cmp_ast_package_by_name) {
+ GB_ASSERT(a != nullptr);
+ GB_ASSERT(b != nullptr);
+ AstPackage *x = *cast(AstPackage **)a;
+ AstPackage *y = *cast(AstPackage **)b;
+ return string_compare(x->name, y->name);
+}
+
+void print_doc_line(i32 indent, char const *fmt, ...) {
+ while (indent --> 0) {
+ gb_printf("\t");
+ }
+ va_list va;
+ va_start(va, fmt);
+ gb_printf_va(fmt, va);
+ va_end(va);
+ gb_printf("\n");
+}
+void print_doc_line_no_newline(i32 indent, char const *fmt, ...) {
+ while (indent --> 0) {
+ gb_printf("\t");
+ }
+ va_list va;
+ va_start(va, fmt);
+ gb_printf_va(fmt, va);
+ va_end(va);
+}
+
+bool print_doc_comment_group_string(i32 indent, CommentGroup *g) {
+ if (g == nullptr) {
+ return false;
+ }
isize len = 0;
- for_array(i, g.list) {
- String comment = g.list[i].string;
+ for_array(i, g->list) {
+ String comment = g->list[i].string;
len += comment.len;
len += 1; // for \n
}
- if (len == 0) {
- return make_string(nullptr, 0);
+ if (len <= g->list.count) {
+ return false;
}
- u8 *text = gb_alloc_array(a, u8, len+1);
- len = 0;
- for_array(i, g.list) {
- String comment = g.list[i].string;
+ isize count = 0;
+ for_array(i, g->list) {
+ String comment = g->list[i].string;
+ String original_comment = comment;
+
+ bool slash_slash = comment[1] == '/';
+ bool slash_star = comment[1] == '*';
if (comment[1] == '/') {
comment.text += 2;
comment.len -= 2;
@@ -24,84 +104,216 @@ String alloc_comment_group_string(gbAllocator a, CommentGroup g) {
comment.text += 2;
comment.len -= 4;
}
- comment = string_trim_whitespace(comment);
- gb_memmove(text+len, comment.text, comment.len);
- len += comment.len;
- text[len++] = '\n';
- }
- return make_string(text, len);
-}
-#if 0
-void print_type_spec(Ast *spec) {
- ast_node(ts, TypeSpec, spec);
- GB_ASSERT(ts->name->kind == Ast_Ident);
- String name = ts->name->Ident.string;
- if (name.len == 0) {
- return;
+ // Ignore the first space
+ if (comment.len > 0 && comment[0] == ' ') {
+ comment.text += 1;
+ comment.len -= 1;
+ }
+
+ if (slash_slash) {
+ if (string_starts_with(comment, str_lit("+"))) {
+ continue;
+ }
+ if (string_starts_with(comment, str_lit("@("))) {
+ continue;
+ }
+ }
+
+ if (slash_slash) {
+ print_doc_line(indent, "%.*s", LIT(comment));
+ count += 1;
+ } else {
+ isize pos = 0;
+ for (; pos < comment.len; pos++) {
+ isize end = pos;
+ for (; end < comment.len; end++) {
+ if (comment[end] == '\n') {
+ break;
+ }
+ }
+ String line = substring(comment, pos, end);
+ pos = end+1;
+ String trimmed_line = string_trim_whitespace(line);
+ if (trimmed_line.len == 0) {
+ if (count == 0) {
+ continue;
+ }
+ }
+ /*
+ * Remove comments with
+ * styles
+ * like this
+ */
+ if (string_starts_with(line, str_lit("* "))) {
+ line = substring(line, 2, line.len);
+ }
+
+ print_doc_line(indent, "%.*s", LIT(line));
+ count += 1;
+ }
+ }
}
- if (name[0] == '_') {
- return;
+
+ if (count > 0) {
+ print_doc_line(0, "");
+ return true;
}
- gb_printf("type %.*s\n", LIT(name));
+ return false;
}
-void print_proc_decl(AstProcDecl *pd) {
- GB_ASSERT(pd->name->kind == Ast_Ident);
- String name = pd->name->Ident.string;
- if (name.len == 0) {
- return;
+
+
+
+void print_doc_expr(Ast *expr) {
+ gbString s = nullptr;
+ if (build_context.cmd_doc_flags & CmdDocFlag_Short) {
+ s = expr_to_string_shorthand(expr);
+ } else {
+ s = expr_to_string(expr);
}
- if (name[0] == '_') {
+ gb_file_write(gb_file_get_standard(gbFileStandard_Output), s, gb_string_length(s));
+ gb_string_free(s);
+}
+
+
+void print_doc_package(CheckerInfo *info, AstPackage *pkg) {
+ if (pkg == nullptr) {
return;
}
- String docs = alloc_comment_group_string(heap_allocator(), pd->docs);
- defer (gb_free(heap_allocator(), docs.text));
+ print_doc_line(0, "package %.*s", LIT(pkg->name));
- if (docs.len > 0) {
- gb_file_write(&gb__std_files[gbFileStandard_Output], docs.text, docs.len);
- } else {
- return;
+
+ for_array(i, pkg->files) {
+ AstFile *f = pkg->files[i];
+ if (f->pkg_decl) {
+ GB_ASSERT(f->pkg_decl->kind == Ast_PackageDecl);
+ print_doc_comment_group_string(1, f->pkg_decl->PackageDecl.docs);
+ }
}
- ast_node(proc_type, ProcType, pd->type);
-
- gbString params = expr_to_string(proc_type->params);
- defer (gb_string_free(params));
- gb_printf("proc %.*s(%s)", LIT(name), params);
- if (proc_type->results != nullptr) {
- ast_node(fl, FieldList, proc_type->results);
- isize count = fl->list.count;
- if (count > 0) {
- gbString results = expr_to_string(proc_type->results);
- defer (gb_string_free(results));
- gb_printf(" -> ");
- if (count != 1) {
- gb_printf("(");
+ if (pkg->scope != nullptr) {
+ auto entities = array_make<Entity *>(heap_allocator(), 0, pkg->scope->elements.entries.count);
+ defer (array_free(&entities));
+ for_array(i, pkg->scope->elements.entries) {
+ Entity *e = pkg->scope->elements.entries[i].value;
+ switch (e->kind) {
+ case Entity_Invalid:
+ case Entity_Builtin:
+ case Entity_Nil:
+ case Entity_Label:
+ continue;
+ case Entity_Constant:
+ case Entity_Variable:
+ case Entity_TypeName:
+ case Entity_Procedure:
+ case Entity_ProcGroup:
+ case Entity_ImportName:
+ case Entity_LibraryName:
+ // Fine
+ break;
+ }
+ array_add(&entities, e);
+ }
+ gb_sort_array(entities.data, entities.count, cmp_entities_for_printing);
+
+ bool show_docs = (build_context.cmd_doc_flags & CmdDocFlag_Short) == 0;
+
+ EntityKind curr_entity_kind = Entity_Invalid;
+ for_array(i, entities) {
+ Entity *e = entities[i];
+ if (e->pkg != pkg) {
+ continue;
+ }
+ if (!is_entity_exported(e)) {
+ continue;
+ }
+
+
+ if (curr_entity_kind != e->kind) {
+ if (curr_entity_kind != Entity_Invalid) {
+ print_doc_line(0, "");
+ }
+ curr_entity_kind = e->kind;
+ print_doc_line(1, "%s", print_entity_names[e->kind]);
+ }
+
+ Ast *type_expr = nullptr;
+ Ast *init_expr = nullptr;
+ Ast *decl_node = nullptr;
+ CommentGroup *comment = nullptr;
+ CommentGroup *docs = nullptr;
+ if (e->decl_info != nullptr) {
+ type_expr = e->decl_info->type_expr;
+ init_expr = e->decl_info->init_expr;
+ decl_node = e->decl_info->decl_node;
+ comment = e->decl_info->comment;
+ docs = e->decl_info->docs;
}
- gb_printf("%s", results);
- if (count != 1) {
- gb_printf(")");
+ GB_ASSERT(type_expr != nullptr || init_expr != nullptr);
+
+ print_doc_line_no_newline(2, "%.*s", LIT(e->token.string));
+ if (type_expr != nullptr) {
+ gbString t = expr_to_string(type_expr);
+ gb_printf(": %s ", t);
+ gb_string_free(t);
+ } else {
+ gb_printf(" :");
+ }
+ if (e->kind == Entity_Variable) {
+ if (init_expr != nullptr) {
+ gb_printf("= ");
+ print_doc_expr(init_expr);
+ }
+ } else {
+ gb_printf(": ");
+ print_doc_expr(init_expr);
+ }
+
+ gb_printf(";\n");
+
+ if (show_docs) {
+ print_doc_comment_group_string(3, docs);
}
}
+ print_doc_line(0, "");
}
- gb_printf("\n\n");
-}
-#endif
-void print_declaration(Ast *decl) {
+
+ if (pkg->fullpath.len != 0) {
+ print_doc_line(0, "");
+ print_doc_line(1, "fullpath:");
+ print_doc_line(2, "%.*s", LIT(pkg->fullpath));
+ print_doc_line(1, "files:");
+ for_array(i, pkg->files) {
+ AstFile *f = pkg->files[i];
+ String filename = remove_directory_from_path(f->fullpath);
+ print_doc_line(2, "%.*s", LIT(filename));
+ }
+ }
+
}
-void generate_documentation(Parser *parser) {
- // for_array(file_index, parser->files) {
- // AstFile *file = parser->files[file_index];
- // Tokenizer *tokenizer = &file->tokenizer;
- // String fullpath = tokenizer->fullpath;
- // gb_printf("%.*s\n", LIT(fullpath));
-
- // for_array(decl_index, file->decls) {
- // Ast *decl = file->decls[decl_index];
- // print_declaration(decl);
- // }
- // }
+void generate_documentation(Checker *c) {
+ CheckerInfo *info = &c->info;
+
+ auto pkgs = array_make<AstPackage *>(permanent_allocator(), 0, info->packages.entries.count);
+ for_array(i, info->packages.entries) {
+ AstPackage *pkg = info->packages.entries[i].value;
+ if (build_context.cmd_doc_flags & CmdDocFlag_AllPackages) {
+ array_add(&pkgs, pkg);
+ } else {
+ if (pkg->kind == Package_Init) {
+ array_add(&pkgs, pkg);
+ } else if (pkg->is_extra) {
+ array_add(&pkgs, pkg);
+ }
+ }
+ }
+
+ gb_sort_array(pkgs.data, pkgs.count, cmp_ast_package_by_name);
+
+ for_array(i, pkgs) {
+ print_doc_package(info, pkgs[i]);
+ }
}
diff --git a/src/entity.cpp b/src/entity.cpp
index 3d354b9c8..0aece39c3 100644
--- a/src/entity.cpp
+++ b/src/entity.cpp
@@ -120,6 +120,7 @@ struct Entity {
union {
struct {
ExactValue value;
+ ParameterValue param_value;
} Constant;
struct {
Ast *init_expr; // only used for some variables within procedure bodies
@@ -164,7 +165,7 @@ struct Entity {
Scope *scope;
} ImportName;
struct {
- Array<String> paths;
+ Slice<String> paths;
String name;
} LibraryName;
i32 Nil;
@@ -219,7 +220,7 @@ bool entity_has_deferred_procedure(Entity *e) {
gb_global u64 global_entity_id = 0;
Entity *alloc_entity(EntityKind kind, Scope *scope, Token token, Type *type) {
- gbAllocator a = heap_allocator();
+ gbAllocator a = permanent_allocator();
Entity *entity = gb_alloc_item(a, Entity);
entity->kind = kind;
entity->state = EntityState_Unresolved;
@@ -332,7 +333,7 @@ Entity *alloc_entity_import_name(Scope *scope, Token token, Type *type,
}
Entity *alloc_entity_library_name(Scope *scope, Token token, Type *type,
- Array<String> paths, String name) {
+ Slice<String> paths, String name) {
Entity *entity = alloc_entity(Entity_LibraryName, scope, token, type);
entity->LibraryName.paths = paths;
entity->LibraryName.name = name;
diff --git a/src/exact_value.cpp b/src/exact_value.cpp
index 9e22c0483..326f4d587 100644
--- a/src/exact_value.cpp
+++ b/src/exact_value.cpp
@@ -46,16 +46,16 @@ enum ExactValueKind {
struct ExactValue {
ExactValueKind kind;
union {
- bool value_bool;
- String value_string;
- BigInt value_integer; // NOTE(bill): This must be an integer and not a pointer
- f64 value_float;
- i64 value_pointer;
- Complex128 value_complex;
- Quaternion256 value_quaternion;
- Ast * value_compound;
- Ast * value_procedure;
- Type * value_typeid;
+ bool value_bool;
+ String value_string;
+ BigInt value_integer; // NOTE(bill): This must be an integer and not a pointer
+ f64 value_float;
+ i64 value_pointer;
+ Complex128 *value_complex;
+ Quaternion256 *value_quaternion;
+ Ast * value_compound;
+ Ast * value_procedure;
+ Type * value_typeid;
};
};
@@ -85,9 +85,9 @@ HashKey hash_exact_value(ExactValue v) {
case ExactValue_Pointer:
return hash_integer(v.value_pointer);
case ExactValue_Complex:
- return hashing_proc(&v.value_complex, gb_size_of(Complex128));
+ return hashing_proc(v.value_complex, gb_size_of(Complex128));
case ExactValue_Quaternion:
- return hashing_proc(&v.value_quaternion, gb_size_of(Quaternion256));
+ return hashing_proc(v.value_quaternion, gb_size_of(Quaternion256));
case ExactValue_Compound:
return hash_pointer(v.value_compound);
case ExactValue_Procedure:
@@ -139,17 +139,19 @@ ExactValue exact_value_float(f64 f) {
ExactValue exact_value_complex(f64 real, f64 imag) {
ExactValue result = {ExactValue_Complex};
- result.value_complex.real = real;
- result.value_complex.imag = imag;
+ result.value_complex = gb_alloc_item(permanent_allocator(), Complex128);
+ result.value_complex->real = real;
+ result.value_complex->imag = imag;
return result;
}
ExactValue exact_value_quaternion(f64 real, f64 imag, f64 jmag, f64 kmag) {
ExactValue result = {ExactValue_Quaternion};
- result.value_quaternion.real = real;
- result.value_quaternion.imag = imag;
- result.value_quaternion.jmag = jmag;
- result.value_quaternion.kmag = kmag;
+ result.value_quaternion = gb_alloc_item(permanent_allocator(), Quaternion256);
+ result.value_quaternion->real = real;
+ result.value_quaternion->imag = imag;
+ result.value_quaternion->jmag = jmag;
+ result.value_quaternion->kmag = kmag;
return result;
}
@@ -373,6 +375,7 @@ ExactValue exact_value_to_complex(ExactValue v) {
// return exact_value_complex(v.value_quaternion.real, v.value_quaternion.imag);
}
ExactValue r = {ExactValue_Invalid};
+ v.value_complex = gb_alloc_item(permanent_allocator(), Complex128);
return r;
}
ExactValue exact_value_to_quaternion(ExactValue v) {
@@ -382,11 +385,12 @@ ExactValue exact_value_to_quaternion(ExactValue v) {
case ExactValue_Float:
return exact_value_quaternion(v.value_float, 0, 0, 0);
case ExactValue_Complex:
- return exact_value_quaternion(v.value_complex.real, v.value_complex.imag, 0, 0);
+ return exact_value_quaternion(v.value_complex->real, v.value_complex->imag, 0, 0);
case ExactValue_Quaternion:
return v;
}
ExactValue r = {ExactValue_Invalid};
+ v.value_quaternion = gb_alloc_item(permanent_allocator(), Quaternion256);
return r;
}
@@ -396,9 +400,9 @@ ExactValue exact_value_real(ExactValue v) {
case ExactValue_Float:
return v;
case ExactValue_Complex:
- return exact_value_float(v.value_complex.real);
+ return exact_value_float(v.value_complex->real);
case ExactValue_Quaternion:
- return exact_value_float(v.value_quaternion.real);
+ return exact_value_float(v.value_quaternion->real);
}
ExactValue r = {ExactValue_Invalid};
return r;
@@ -410,9 +414,9 @@ ExactValue exact_value_imag(ExactValue v) {
case ExactValue_Float:
return exact_value_i64(0);
case ExactValue_Complex:
- return exact_value_float(v.value_complex.imag);
+ return exact_value_float(v.value_complex->imag);
case ExactValue_Quaternion:
- return exact_value_float(v.value_quaternion.imag);
+ return exact_value_float(v.value_quaternion->imag);
}
ExactValue r = {ExactValue_Invalid};
return r;
@@ -425,7 +429,7 @@ ExactValue exact_value_jmag(ExactValue v) {
case ExactValue_Complex:
return exact_value_i64(0);
case ExactValue_Quaternion:
- return exact_value_float(v.value_quaternion.jmag);
+ return exact_value_float(v.value_quaternion->jmag);
}
ExactValue r = {ExactValue_Invalid};
return r;
@@ -438,7 +442,7 @@ ExactValue exact_value_kmag(ExactValue v) {
case ExactValue_Complex:
return exact_value_i64(0);
case ExactValue_Quaternion:
- return exact_value_float(v.value_quaternion.kmag);
+ return exact_value_float(v.value_quaternion->kmag);
}
ExactValue r = {ExactValue_Invalid};
return r;
@@ -532,15 +536,15 @@ ExactValue exact_unary_operator_value(TokenKind op, ExactValue v, i32 precision,
return i;
}
case ExactValue_Complex: {
- f64 real = v.value_complex.real;
- f64 imag = v.value_complex.imag;
+ f64 real = v.value_complex->real;
+ f64 imag = v.value_complex->imag;
return exact_value_complex(-real, -imag);
}
case ExactValue_Quaternion: {
- f64 real = v.value_quaternion.real;
- f64 imag = v.value_quaternion.imag;
- f64 jmag = v.value_quaternion.jmag;
- f64 kmag = v.value_quaternion.kmag;
+ f64 real = v.value_quaternion->real;
+ f64 imag = v.value_quaternion->imag;
+ f64 jmag = v.value_quaternion->jmag;
+ f64 kmag = v.value_quaternion->kmag;
return exact_value_quaternion(-real, -imag, -jmag, -kmag);
}
}
@@ -685,6 +689,8 @@ ExactValue exact_binary_operator_value(TokenKind op, ExactValue x, ExactValue y)
case Token_CmpOr: return exact_value_bool(x.value_bool || y.value_bool);
case Token_And: return exact_value_bool(x.value_bool & y.value_bool);
case Token_Or: return exact_value_bool(x.value_bool | y.value_bool);
+ case Token_AndNot: return exact_value_bool(x.value_bool & !y.value_bool);
+ case Token_Xor: return exact_value_bool((x.value_bool && !y.value_bool) || (!x.value_bool && y.value_bool));
default: goto error;
}
break;
@@ -730,10 +736,10 @@ ExactValue exact_binary_operator_value(TokenKind op, ExactValue x, ExactValue y)
case ExactValue_Complex: {
y = exact_value_to_complex(y);
- f64 a = x.value_complex.real;
- f64 b = x.value_complex.imag;
- f64 c = y.value_complex.real;
- f64 d = y.value_complex.imag;
+ f64 a = x.value_complex->real;
+ f64 b = x.value_complex->imag;
+ f64 c = y.value_complex->real;
+ f64 d = y.value_complex->imag;
f64 real = 0;
f64 imag = 0;
switch (op) {
@@ -763,14 +769,14 @@ ExactValue exact_binary_operator_value(TokenKind op, ExactValue x, ExactValue y)
case ExactValue_Quaternion: {
y = exact_value_to_quaternion(y);
- f64 xr = x.value_quaternion.real;
- f64 xi = x.value_quaternion.imag;
- f64 xj = x.value_quaternion.jmag;
- f64 xk = x.value_quaternion.kmag;
- f64 yr = y.value_quaternion.real;
- f64 yi = y.value_quaternion.imag;
- f64 yj = y.value_quaternion.jmag;
- f64 yk = y.value_quaternion.kmag;
+ f64 xr = x.value_quaternion->real;
+ f64 xi = x.value_quaternion->imag;
+ f64 xj = x.value_quaternion->jmag;
+ f64 xk = x.value_quaternion->kmag;
+ f64 yr = y.value_quaternion->real;
+ f64 yi = y.value_quaternion->imag;
+ f64 yj = y.value_quaternion->jmag;
+ f64 yk = y.value_quaternion->kmag;
f64 real = 0;
@@ -897,10 +903,10 @@ bool compare_exact_values(TokenKind op, ExactValue x, ExactValue y) {
}
case ExactValue_Complex: {
- f64 a = x.value_complex.real;
- f64 b = x.value_complex.imag;
- f64 c = y.value_complex.real;
- f64 d = y.value_complex.imag;
+ f64 a = x.value_complex->real;
+ f64 b = x.value_complex->imag;
+ f64 c = y.value_complex->real;
+ f64 d = y.value_complex->imag;
switch (op) {
case Token_CmpEq: return cmp_f64(a, c) == 0 && cmp_f64(b, d) == 0;
case Token_NotEq: return cmp_f64(a, c) != 0 || cmp_f64(b, d) != 0;
@@ -945,7 +951,7 @@ bool compare_exact_values(TokenKind op, ExactValue x, ExactValue y) {
Entity *strip_entity_wrapping(Ast *expr);
Entity *strip_entity_wrapping(Entity *e);
-gbString write_expr_to_string(gbString str, Ast *node);
+gbString write_expr_to_string(gbString str, Ast *node, bool shorthand);
gbString write_exact_value_to_string(gbString str, ExactValue const &v, isize string_limit=36) {
switch (v.kind) {
@@ -976,14 +982,16 @@ gbString write_exact_value_to_string(gbString str, ExactValue const &v, isize st
case ExactValue_Float:
return gb_string_append_fmt(str, "%f", v.value_float);
case ExactValue_Complex:
- return gb_string_append_fmt(str, "%f+%fi", v.value_complex.real, v.value_complex.imag);
+ return gb_string_append_fmt(str, "%f+%fi", v.value_complex->real, v.value_complex->imag);
+ case ExactValue_Quaternion:
+ return gb_string_append_fmt(str, "%f+%fi+%fj+%fk", v.value_quaternion->real, v.value_quaternion->imag, v.value_quaternion->jmag, v.value_quaternion->kmag);
case ExactValue_Pointer:
return str;
case ExactValue_Compound:
- return write_expr_to_string(str, v.value_compound);
+ return write_expr_to_string(str, v.value_compound, false);
case ExactValue_Procedure:
- return write_expr_to_string(str, v.value_procedure);
+ return write_expr_to_string(str, v.value_procedure, false);
}
return str;
};
diff --git a/src/gb/gb.h b/src/gb/gb.h
index 848f27628..9981b9e34 100644
--- a/src/gb/gb.h
+++ b/src/gb/gb.h
@@ -157,7 +157,7 @@ extern "C" {
#endif
#endif
-#if defined(_WIN64) || defined(__x86_64__) || defined(_M_X64) || defined(__64BIT__) || defined(__powerpc64__) || defined(__ppc64__)
+#if defined(_WIN64) || defined(__x86_64__) || defined(_M_X64) || defined(__64BIT__) || defined(__powerpc64__) || defined(__ppc64__) || defined(__aarch64__)
#ifndef GB_ARCH_64_BIT
#define GB_ARCH_64_BIT 1
#endif
@@ -230,7 +230,7 @@ extern "C" {
#define GB_CACHE_LINE_SIZE 128
#endif
-#elif defined(__arm__)
+#elif defined(__arm__) || defined(__aarch64__) || defined(_M_ARM) || defined(_M_ARM64)
#ifndef GB_CPU_ARM
#define GB_CPU_ARM 1
#endif
@@ -3702,6 +3702,12 @@ gb_inline void *gb_memcopy(void *dest, void const *source, isize n) {
void *dest_copy = dest;
__asm__ __volatile__("rep movsb" : "+D"(dest_copy), "+S"(source), "+c"(n) : : "memory");
+#elif defined(GB_CPU_ARM)
+ u8 *s = cast(u8 *)source;
+ u8 *d = cast(u8 *)dest;
+ for (isize i = 0; i < n; i++) {
+ *d++ = *s++;
+ }
#else
u8 *d = cast(u8 *)dest;
u8 const *s = cast(u8 const *)source;
@@ -4438,6 +4444,76 @@ gb_inline i64 gb_atomic64_fetch_or(gbAtomic64 volatile *a, i64 operand) {
#endif
}
+#elif defined(GB_CPU_ARM)
+
+gb_inline i32 gb_atomic32_load (gbAtomic32 const volatile *a) {
+ return __atomic_load_n(&a->value, __ATOMIC_SEQ_CST);
+}
+gb_inline void gb_atomic32_store(gbAtomic32 volatile *a, i32 value) {
+ __atomic_store_n(&a->value, value, __ATOMIC_SEQ_CST);
+}
+
+gb_inline i32 gb_atomic32_compare_exchange(gbAtomic32 volatile *a, i32 expected, i32 desired) {
+ i32 expected_copy = expected;
+ auto result = __atomic_compare_exchange_n(&a->value, &expected_copy, desired, true, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
+ if (result) {
+ return expected;
+ } else {
+ return expected_copy;
+ }
+}
+
+gb_inline i32 gb_atomic32_exchanged(gbAtomic32 volatile *a, i32 desired) {
+ return __atomic_exchange_n(&a->value, desired, __ATOMIC_SEQ_CST);
+}
+
+gb_inline i32 gb_atomic32_fetch_add(gbAtomic32 volatile *a, i32 operand) {
+ return __atomic_fetch_add(&a->value, operand, __ATOMIC_SEQ_CST);
+}
+
+gb_inline i32 gb_atomic32_fetch_and(gbAtomic32 volatile *a, i32 operand) {
+ return __atomic_fetch_and(&a->value, operand, __ATOMIC_SEQ_CST);
+}
+
+gb_inline i32 gb_atomic32_fetch_or(gbAtomic32 volatile *a, i32 operand) {
+ return __atomic_fetch_or(&a->value, operand, __ATOMIC_SEQ_CST);
+}
+
+gb_inline i64 gb_atomic64_load(gbAtomic64 const volatile *a) {
+ return __atomic_load_n(&a->value, __ATOMIC_SEQ_CST);
+}
+
+gb_inline void gb_atomic64_store(gbAtomic64 volatile *a, i64 value) {
+ __atomic_store_n(&a->value, value, __ATOMIC_SEQ_CST);
+}
+
+gb_inline i64 gb_atomic64_compare_exchange(gbAtomic64 volatile *a, i64 expected, i64 desired) {
+ i64 expected_copy = expected;
+ auto result = __atomic_compare_exchange_n(&a->value, &expected_copy, desired, true, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
+ if (result) {
+ return expected;
+ } else {
+ return expected_copy;
+ }
+}
+
+gb_inline i64 gb_atomic64_exchanged(gbAtomic64 volatile *a, i64 desired) {
+ return __atomic_exchange_n(&a->value, desired, __ATOMIC_SEQ_CST);
+}
+
+gb_inline i64 gb_atomic64_fetch_add(gbAtomic64 volatile *a, i64 operand) {
+ return __atomic_fetch_add(&a->value, operand, __ATOMIC_SEQ_CST);
+}
+
+gb_inline i64 gb_atomic64_fetch_and(gbAtomic64 volatile *a, i64 operand) {
+ return __atomic_fetch_and(&a->value, operand, __ATOMIC_SEQ_CST);
+}
+
+gb_inline i64 gb_atomic64_fetch_or(gbAtomic64 volatile *a, i64 operand) {
+ return __atomic_fetch_or(&a->value, operand, __ATOMIC_SEQ_CST);
+}
+
+
#else
#error TODO(bill): Implement Atomics for this CPU
#endif
@@ -4563,7 +4639,11 @@ gb_inline void gb_yield_thread(void) {
#if defined(GB_SYSTEM_WINDOWS)
_mm_pause();
#elif defined(GB_SYSTEM_OSX)
+ #if defined(GB_CPU_X86)
__asm__ volatile ("" : : : "memory");
+ #elif defined(GB_CPU_ARM)
+ __asm__ volatile ("yield" : : : "memory");
+ #endif
#elif defined(GB_CPU_X86)
_mm_pause();
#else
@@ -4575,7 +4655,11 @@ gb_inline void gb_mfence(void) {
#if defined(GB_SYSTEM_WINDOWS)
_ReadWriteBarrier();
#elif defined(GB_SYSTEM_OSX)
+ #if defined(GB_CPU_X86)
__sync_synchronize();
+ #elif defined(GB_CPU_ARM)
+ __atomic_thread_fence(__ATOMIC_SEQ_CST);
+ #endif
#elif defined(GB_CPU_X86)
_mm_mfence();
#else
@@ -4587,7 +4671,12 @@ gb_inline void gb_sfence(void) {
#if defined(GB_SYSTEM_WINDOWS)
_WriteBarrier();
#elif defined(GB_SYSTEM_OSX)
+ #if defined(GB_CPU_X86)
__asm__ volatile ("" : : : "memory");
+ #elif defined(GB_CPU_ARM)
+ // TODO(bill): is this correct?
+ __atomic_thread_fence(__ATOMIC_SEQ_CST);
+ #endif
#elif defined(GB_CPU_X86)
_mm_sfence();
#else
@@ -5156,7 +5245,7 @@ b32 gb_affinity_set(gbAffinity *a, isize core, isize thread_index) {
index = core * a->threads_per_core + thread_index;
thread = pthread_self();
-
+
cpuset_t mn;
CPU_ZERO(&mn);
@@ -5202,7 +5291,7 @@ void gb_affinity_init(gbAffinity *a) {
for (;;) {
// The 'temporary char'. Everything goes into this char,
// so that we can check against EOF at the end of this loop.
- char c;
+ int c;
#define AF__CHECK(letter) ((c = getc(cpu_info)) == letter)
if (AF__CHECK('c') && AF__CHECK('p') && AF__CHECK('u') && AF__CHECK(' ') &&
@@ -8808,6 +8897,14 @@ gb_inline gbDllProc gb_dll_proc_address(gbDllHandle dll, char const *proc_name)
return result;
}
+#elif defined(__aarch64__)
+ gb_inline u64 gb_rdtsc(void) {
+ int64_t virtual_timer_value;
+ asm volatile("mrs %0, cntvct_el0" : "=r"(virtual_timer_value));
+ return virtual_timer_value;
+ }
+#else
+#error "gb_rdtsc not supported"
#endif
#if defined(GB_SYSTEM_WINDOWS)
diff --git a/src/ir.cpp b/src/ir.cpp
index 2b3bd35df..e46eb27fb 100644
--- a/src/ir.cpp
+++ b/src/ir.cpp
@@ -25,6 +25,9 @@ struct irModule {
Map<irDebugInfo *> debug_info; // Key: Unique pointer
Map<irValue *> anonymous_proc_lits; // Key: Ast *
+ Map<irValue *> equal_procs; // Key: Type *
+ Map<irValue *> hasher_procs; // Key: Type *
+
irDebugInfo * debug_compile_unit;
Array<irDebugInfo *> debug_location_stack;
@@ -161,6 +164,7 @@ struct irProcedure {
Ast * return_ptr_hint_ast;
bool return_ptr_hint_used;
+ bool ignore_dead_instr;
Array<irBranchBlocks> branch_blocks;
@@ -454,7 +458,6 @@ struct irValueSourceCodeLocation {
irValue *line;
irValue *column;
irValue *procedure;
- u64 hash;
};
@@ -525,6 +528,11 @@ struct irAddr {
Type *ir_type(irValue *value);
irValue *ir_gen_anonymous_proc_lit(irModule *m, String prefix_name, Ast *expr, irProcedure *proc = nullptr);
+void ir_begin_procedure_body(irProcedure *proc);
+void ir_end_procedure_body(irProcedure *proc);
+irValue *ir_get_equal_proc_for_type(irModule *m, Type *type);
+irValue *ir_get_hasher_proc_for_type(irModule *m, Type *type);
+
irAddr ir_addr(irValue *addr) {
irAddr v = {irAddr_Default, addr};
@@ -1159,7 +1167,7 @@ irValue *ir_instr_atomic_cxchg(irProcedure *p, Type *type, irValue *address, irV
GB_ASSERT(type->Tuple.variables.count == 2);
Type *elem = type->Tuple.variables[0]->type;
// LEAK TODO(bill): LLVM returns {T, i1} whilst Odin does {T, bool}, fix this mapping hack
- gbAllocator a = heap_allocator();
+ gbAllocator a = permanent_allocator();
Type *llvm_type = alloc_type_tuple();
array_init(&llvm_type->Tuple.variables, a, 0, 2);
array_add (&llvm_type->Tuple.variables, alloc_entity_field(nullptr, blank_token, elem, false, 0));
@@ -1799,7 +1807,7 @@ irValue *ir_add_local(irProcedure *proc, Entity *e, Ast *expr, bool zero_initial
if (zero_initialized) {
ir_emit_zero_init(proc, instr, expr);
}
- set_procedure_abi_types(heap_allocator(), e->type);
+ set_procedure_abi_types(e->type);
// if (proc->module->generate_debug_info && expr != nullptr && proc->entity != nullptr) {
// if (proc->module->generate_debug_info && proc->entity != nullptr) {
@@ -2132,7 +2140,7 @@ irDebugInfo *ir_add_debug_info_field(irModule *module, irDebugInfo *scope, Entit
if (e->token.string.len == 0) {
// If no name available for field, use its field index as its name.
isize max_len = 8;
- u8 *str = cast(u8 *)gb_alloc_array(heap_allocator(), u8, max_len);
+ u8 *str = cast(u8 *)gb_alloc_array(permanent_allocator(), u8, max_len);
isize len = gb_snprintf(cast(char *)str, 8, "%d", index);
di->DerivedType.name = make_string(str, len-1);
}
@@ -3282,7 +3290,7 @@ irValue *ir_emit_call(irProcedure *p, irValue *value, Array<irValue *> const &ar
context_ptr = ir_find_or_generate_context_ptr(p);
}
- set_procedure_abi_types(heap_allocator(), pt);
+ set_procedure_abi_types(pt);
bool is_c_vararg = pt->Proc.c_vararg;
isize param_count = pt->Proc.param_count;
@@ -3293,7 +3301,7 @@ irValue *ir_emit_call(irProcedure *p, irValue *value, Array<irValue *> const &ar
GB_ASSERT_MSG(param_count == args.count, "%.*s %td == %td", LIT(p->entity->token.string), param_count, args.count);
}
- auto processed_args = array_make<irValue *>(heap_allocator(), 0, args.count);
+ auto processed_args = array_make<irValue *>(permanent_allocator(), 0, args.count);
for (isize i = 0; i < param_count; i++) {
Entity *e = pt->Proc.params->Tuple.variables[i];
@@ -3416,7 +3424,7 @@ irValue *ir_emit_call(irProcedure *p, irValue *value, Array<irValue *> const &ar
case DeferredProcedure_in_out:
{
auto out_args = ir_value_to_array(p, result);
- array_init(&result_as_args, heap_allocator(), in_args.count + out_args.count);
+ array_init(&result_as_args, permanent_allocator(), in_args.count + out_args.count);
array_copy(&result_as_args, in_args, 0);
array_copy(&result_as_args, out_args, in_args.count);
}
@@ -3587,65 +3595,69 @@ irValue *ir_gen_map_header(irProcedure *proc, irValue *map_val_ptr, Type *map_ty
irValue *m = ir_emit_conv(proc, map_val_ptr, type_deref(ir_type(gep0)));
ir_emit_store(proc, gep0, m);
- ir_emit_store(proc, ir_emit_struct_ep(proc, h, 1), ir_const_bool(is_type_string(key_type)));
i64 entry_size = type_size_of (map_type->Map.entry_type);
i64 entry_align = type_align_of (map_type->Map.entry_type);
- i64 value_offset = type_offset_of(map_type->Map.entry_type, 2);
+ i64 key_offset = type_offset_of(map_type->Map.entry_type, 2);
+ i64 key_size = type_size_of (map_type->Map.key);
+ i64 value_offset = type_offset_of(map_type->Map.entry_type, 3);
i64 value_size = type_size_of (map_type->Map.value);
+ ir_emit_store(proc, ir_emit_struct_ep(proc, h, 1), ir_get_equal_proc_for_type(proc->module, key_type));
ir_emit_store(proc, ir_emit_struct_ep(proc, h, 2), ir_const_int(entry_size));
ir_emit_store(proc, ir_emit_struct_ep(proc, h, 3), ir_const_int(entry_align));
- ir_emit_store(proc, ir_emit_struct_ep(proc, h, 4), ir_const_uintptr(value_offset));
- ir_emit_store(proc, ir_emit_struct_ep(proc, h, 5), ir_const_int(value_size));
+ ir_emit_store(proc, ir_emit_struct_ep(proc, h, 4), ir_const_uintptr(key_offset));
+ ir_emit_store(proc, ir_emit_struct_ep(proc, h, 5), ir_const_int(key_size));
+ ir_emit_store(proc, ir_emit_struct_ep(proc, h, 6), ir_const_uintptr(value_offset));
+ ir_emit_store(proc, ir_emit_struct_ep(proc, h, 7), ir_const_int(value_size));
return ir_emit_load(proc, h);
}
-irValue *ir_gen_map_key(irProcedure *proc, irValue *key, Type *key_type) {
- Type *hash_type = t_u64;
- irValue *v = ir_add_local_generated(proc, t_map_key, true);
- Type *t = base_type(ir_type(key));
- key = ir_emit_conv(proc, key, key_type);
-
- if (is_type_string(t)) {
- irValue *str = ir_emit_conv(proc, key, t_string);
- irValue *hashed_str = nullptr;
+irValue *ir_const_hash(irModule *m, irValue *key, Type *key_type) {
+ irValue *hashed_key = nullptr;
- if (str->kind == irValue_Constant) {
- ExactValue ev = str->Constant.value;
- GB_ASSERT(ev.kind == ExactValue_String);
- u64 hs = fnv64a(ev.value_string.text, ev.value_string.len);
- hashed_str = ir_value_constant(t_u64, exact_value_u64(hs));
+ if (key->kind == irValue_Constant) {
+ u64 hash = 0xcbf29ce484222325;
+ if (is_type_string(key_type)) {
+ GB_ASSERT(key->Constant.value.kind == ExactValue_String);
+ String s = key->Constant.value.value_string;
+ hash = fnv64a(s.text, s.len);
} else {
- auto args = array_make<irValue *>(ir_allocator(), 1);
- args[0] = str;
- hashed_str = ir_emit_runtime_call(proc, "default_hash_string", args);
+ return nullptr;
}
- ir_emit_store(proc, ir_emit_struct_ep(proc, v, 0), hashed_str);
+ // TODO(bill): other const hash types
- irValue *key_data = ir_emit_struct_ep(proc, v, 1);
- key_data = ir_emit_conv(proc, key_data, alloc_type_pointer(key_type));
- ir_emit_store(proc, key_data, str);
- } else {
- i64 sz = type_size_of(t);
- GB_ASSERT(sz <= 8);
- if (sz != 0) {
- auto args = array_make<irValue *>(ir_allocator(), 2);
- args[0] = ir_address_from_load_or_generate_local(proc, key);
- args[1] = ir_const_int(sz);
- irValue *hash = ir_emit_runtime_call(proc, "default_hash_ptr", args);
+ if (build_context.word_size == 4) {
+ hash &= 0xffffffffull;
+ }
+ hashed_key = ir_const_uintptr(hash);
+ }
+ return hashed_key;
+}
- irValue *hash_ptr = ir_emit_struct_ep(proc, v, 0);
- irValue *key_data = ir_emit_struct_ep(proc, v, 1);
- key_data = ir_emit_conv(proc, key_data, alloc_type_pointer(key_type));
+irValue *ir_gen_map_hash(irProcedure *proc, irValue *key, Type *key_type) {
+ Type *hash_type = t_u64;
+ irValue *v = ir_add_local_generated(proc, t_map_hash, true);
+ Type *t = base_type(ir_type(key));
+ key = ir_emit_conv(proc, key, key_type);
- ir_emit_store(proc, hash_ptr, hash);
- ir_emit_store(proc, key_data, key);
- }
+ irValue *key_ptr = ir_address_from_load_or_generate_local(proc, key);
+ key_ptr = ir_emit_conv(proc, key_ptr, t_rawptr);
+
+ irValue *hashed_key = ir_const_hash(proc->module, key, key_type);
+ if (hashed_key == nullptr) {
+ irValue *hasher = ir_get_hasher_proc_for_type(proc->module, key_type);
+ auto args = array_make<irValue *>(permanent_allocator(), 2);
+ args[0] = key_ptr;
+ args[1] = ir_value_constant(t_uintptr, exact_value_i64(0));
+ hashed_key = ir_emit_call(proc, hasher, args);
}
+ ir_emit_store(proc, ir_emit_struct_ep(proc, v, 0), hashed_key);
+ ir_emit_store(proc, ir_emit_struct_ep(proc, v, 1), key_ptr);
+
return ir_emit_load(proc, v);
}
@@ -3701,7 +3713,7 @@ irValue *ir_insert_dynamic_map_key_and_value(irProcedure *proc, irValue *addr, T
map_type = base_type(map_type);
irValue *h = ir_gen_map_header(proc, addr, map_type);
- irValue *key = ir_gen_map_key(proc, map_key, map_type->Map.key);
+ irValue *key = ir_gen_map_hash(proc, map_key, map_type->Map.key);
irValue *v = ir_emit_conv(proc, map_value, map_type->Map.value);
irValue *ptr = ir_add_local_generated(proc, ir_type(v), false);
@@ -4058,7 +4070,7 @@ irValue *ir_addr_load(irProcedure *proc, irAddr const &addr) {
Type *map_type = base_type(addr.map_type);
irValue *v = ir_add_local_generated(proc, map_type->Map.lookup_result_type, true);
irValue *h = ir_gen_map_header(proc, addr.addr, map_type);
- irValue *key = ir_gen_map_key(proc, addr.map_key, map_type->Map.key);
+ irValue *key = ir_gen_map_hash(proc, addr.map_key, map_type->Map.key);
auto args = array_make<irValue *>(ir_allocator(), 2);
args[0] = h;
@@ -4226,7 +4238,7 @@ irValue *ir_addr_get_ptr(irProcedure *proc, irAddr const &addr, bool allow_refer
if (allow_reference) {
Type *map_type = base_type(addr.map_type);
irValue *h = ir_gen_map_header(proc, addr.addr, map_type);
- irValue *key = ir_gen_map_key(proc, addr.map_key, map_type->Map.key);
+ irValue *key = ir_gen_map_hash(proc, addr.map_key, map_type->Map.key);
auto args = array_make<irValue *>(ir_allocator(), 2);
args[0] = h;
@@ -4537,7 +4549,7 @@ irValue *ir_emit_arith(irProcedure *proc, TokenKind op, irValue *left, irValue *
Type *ft = base_complex_elem_type(t_left);
if (op == Token_Quo) {
- auto args = array_make<irValue *>(heap_allocator(), 2);
+ auto args = array_make<irValue *>(permanent_allocator(), 2);
args[0] = left;
args[1] = right;
@@ -4615,7 +4627,7 @@ irValue *ir_emit_arith(irProcedure *proc, TokenKind op, irValue *left, irValue *
return ir_emit_load(proc, res);
} else if (op == Token_Mul) {
- auto args = array_make<irValue *>(heap_allocator(), 2);
+ auto args = array_make<irValue *>(permanent_allocator(), 2);
args[0] = left;
args[1] = right;
@@ -4625,7 +4637,7 @@ irValue *ir_emit_arith(irProcedure *proc, TokenKind op, irValue *left, irValue *
default: GB_PANIC("Unknown float type"); break;
}
} else if (op == Token_Quo) {
- auto args = array_make<irValue *>(heap_allocator(), 2);
+ auto args = array_make<irValue *>(permanent_allocator(), 2);
args[0] = left;
args[1] = right;
@@ -4828,7 +4840,7 @@ irValue *ir_emit_comp_against_nil(irProcedure *proc, TokenKind op_kind, irValue
irValue *invalid_typeid = ir_value_constant(t_typeid, exact_value_i64(0));
return ir_emit_comp(proc, op_kind, x, invalid_typeid);
} else if (is_type_bit_field(t)) {
- auto args = array_make<irValue *>(heap_allocator(), 2);
+ auto args = array_make<irValue *>(permanent_allocator(), 2);
irValue *lhs = ir_address_from_load_or_generate_local(proc, x);
args[0] = ir_emit_conv(proc, lhs, t_rawptr);
args[1] = ir_const_int(type_size_of(t));
@@ -4848,7 +4860,7 @@ irValue *ir_emit_comp_against_nil(irProcedure *proc, TokenKind op_kind, irValue
return ir_emit_comp(proc, op_kind, cap, v_zero);
}
} else if (is_type_struct(t) && type_has_nil(t)) {
- auto args = array_make<irValue *>(heap_allocator(), 2);
+ auto args = array_make<irValue *>(permanent_allocator(), 2);
irValue *lhs = ir_address_from_load_or_generate_local(proc, x);
args[0] = ir_emit_conv(proc, lhs, t_rawptr);
args[1] = ir_const_int(type_size_of(t));
@@ -4859,6 +4871,244 @@ irValue *ir_emit_comp_against_nil(irProcedure *proc, TokenKind op_kind, irValue
return nullptr;
}
+irValue *ir_get_equal_proc_for_type(irModule *m, Type *type) {
+ Type *original_type = type;
+ type = base_type(type);
+ Type *pt = alloc_type_pointer(type);
+
+ auto key = hash_type(type);
+ irValue **found = map_get(&m->equal_procs, key);
+ if (found) {
+ return *found;
+ }
+
+ static u32 proc_index = 0;
+
+ char buf[16] = {};
+ isize n = gb_snprintf(buf, 16, "__$equal%u", ++proc_index);
+ char *str = gb_alloc_str_len(permanent_allocator(), buf, n-1);
+ String proc_name = make_string_c(str);
+
+
+ Ast *body = alloc_ast_node(nullptr, Ast_Invalid);
+ Entity *e = alloc_entity_procedure(nullptr, make_token_ident(proc_name), t_equal_proc, 0);
+ e->Procedure.link_name = proc_name;
+ irValue *p = ir_value_procedure(m, e, t_equal_proc, nullptr, body, proc_name);
+ map_set(&m->values, hash_entity(e), p);
+ string_map_set(&m->members, proc_name, p);
+ map_set(&m->equal_procs, key, p);
+
+ irProcedure *proc = &p->Proc;
+ proc->is_startup = true;
+ proc->ignore_dead_instr = true;
+ ir_begin_procedure_body(proc);
+ // ir_start_block(proc, proc->decl_block);
+ GB_ASSERT(proc->curr_block != nullptr);
+
+ irValue *x = proc->params[0];
+ irValue *y = proc->params[1];
+ irValue *lhs = ir_emit_conv(proc, x, pt);
+ irValue *rhs = ir_emit_conv(proc, y, pt);
+
+ irBlock *block_same_ptr = ir_new_block(proc, nullptr, "same_ptr");
+ irBlock *block_diff_ptr = ir_new_block(proc, nullptr, "diff_ptr");
+
+ irValue *same_ptr = ir_emit_comp(proc, Token_CmpEq, lhs, rhs);
+ ir_emit_if(proc, same_ptr, block_same_ptr, block_diff_ptr);
+ ir_start_block(proc, block_same_ptr);
+ ir_emit(proc, ir_instr_return(proc, ir_const_bool(true)));
+
+ ir_start_block(proc, block_diff_ptr);
+
+ if (type->kind == Type_Struct) {
+ type_set_offsets(type);
+
+ irBlock *done = ir_new_block(proc, nullptr, "done"); // NOTE(bill): Append later
+
+ irBlock *block_false = ir_new_block(proc, nullptr, "bfalse");
+
+ for_array(i, type->Struct.fields) {
+ irBlock *next_block = ir_new_block(proc, nullptr, "btrue");
+
+ irValue *pleft = ir_emit_struct_ep(proc, lhs, cast(i32)i);
+ irValue *pright = ir_emit_struct_ep(proc, rhs, cast(i32)i);
+ irValue *left = ir_emit_load(proc, pleft);
+ irValue *right = ir_emit_load(proc, pright);
+ irValue *ok = ir_emit_comp(proc, Token_CmpEq, left, right);
+
+ ir_emit_if(proc, ok, next_block, block_false);
+
+ ir_emit_jump(proc, next_block);
+ ir_start_block(proc, next_block);
+ }
+
+ ir_emit_jump(proc, done);
+ ir_start_block(proc, block_false);
+
+ ir_emit(proc, ir_instr_return(proc, ir_const_bool(false)));
+
+ ir_emit_jump(proc, done);
+ ir_start_block(proc, done);
+ ir_emit(proc, ir_instr_return(proc, ir_const_bool(true)));
+ } else {
+ irValue *left = ir_emit_load(proc, lhs);
+ irValue *right = ir_emit_load(proc, rhs);
+ irValue *ok = ir_emit_comp(proc, Token_CmpEq, left, right);
+ ok = ir_emit_conv(proc, ok, t_bool);
+ ir_emit(proc, ir_instr_return(proc, ok));
+ }
+
+ ir_end_procedure_body(proc);
+
+ return p;
+}
+
+
+irValue *ir_simple_compare_hash(irProcedure *p, Type *type, irValue *data, irValue *seed) {
+ GB_ASSERT_MSG(is_type_simple_compare(type), "%s", type_to_string(type));
+
+ i64 sz = type_size_of(type);
+ if (1 <= sz && sz <= 16) {
+ char name[20] = {};
+ gb_snprintf(name, 20, "default_hasher%d", cast(i32)sz);
+
+ auto args = array_make<irValue *>(permanent_allocator(), 2);
+ args[0] = data;
+ args[1] = seed;
+ return ir_emit_runtime_call(p, name, args);
+ }
+
+ auto args = array_make<irValue *>(permanent_allocator(), 3);
+ args[0] = data;
+ args[1] = seed;
+ args[2] = ir_const_int(type_size_of(type));
+ return ir_emit_runtime_call(p, "default_hasher_n", args);
+}
+
+irValue *ir_get_hasher_proc_for_type(irModule *m, Type *type) {
+ Type *original_type = type;
+ type = core_type(type);
+ Type *pt = alloc_type_pointer(type);
+
+ GB_ASSERT(is_type_valid_for_keys(type));
+
+ auto key = hash_type(type);
+ irValue **found = map_get(&m->hasher_procs, key);
+ if (found) {
+ return *found;
+ }
+
+ static u32 proc_index = 0;
+
+ char buf[16] = {};
+ isize n = gb_snprintf(buf, 16, "__$hasher%u", ++proc_index);
+ char *str = gb_alloc_str_len(permanent_allocator(), buf, n-1);
+ String proc_name = make_string_c(str);
+
+
+ Ast *body = alloc_ast_node(nullptr, Ast_Invalid);
+ Entity *e = alloc_entity_procedure(nullptr, make_token_ident(proc_name), t_hasher_proc, 0);
+ e->Procedure.link_name = proc_name;
+ irValue *p = ir_value_procedure(m, e, t_hasher_proc, nullptr, body, proc_name);
+ map_set(&m->values, hash_entity(e), p);
+ string_map_set(&m->members, proc_name, p);
+ map_set(&m->hasher_procs, key, p);
+
+ irProcedure *proc = &p->Proc;
+ proc->is_startup = true;
+ proc->ignore_dead_instr = true;
+ ir_begin_procedure_body(proc);
+ defer (ir_end_procedure_body(proc));
+
+ // ir_start_block(proc, proc->decl_block);
+ GB_ASSERT(proc->curr_block != nullptr);
+
+ irValue *data = proc->params[0];
+ irValue *seed = proc->params[1];
+
+ if (is_type_simple_compare(type)) {
+ irValue *res = ir_simple_compare_hash(proc, type, data, seed);
+ ir_emit(proc, ir_instr_return(proc, res));
+ return p;
+ }
+
+ if (is_type_cstring(type)) {
+ auto args = array_make<irValue *>(permanent_allocator(), 2);
+ args[0] = data;
+ args[1] = seed;
+ irValue *res = ir_emit_runtime_call(proc, "default_hasher_cstring", args);
+ ir_emit(proc, ir_instr_return(proc, res));
+ } else if (is_type_string(type)) {
+ auto args = array_make<irValue *>(permanent_allocator(), 2);
+ args[0] = data;
+ args[1] = seed;
+ irValue *res = ir_emit_runtime_call(proc, "default_hasher_string", args);
+ ir_emit(proc, ir_instr_return(proc, res));
+ } else if (type->kind == Type_Struct) {
+ type_set_offsets(type);
+ data = ir_emit_conv(proc, data, t_u8_ptr);
+
+ auto args = array_make<irValue *>(permanent_allocator(), 2);
+ for_array(i, type->Struct.fields) {
+ i64 offset = type->Struct.offsets[i];
+ Entity *field = type->Struct.fields[i];
+ irValue *field_hasher = ir_get_hasher_proc_for_type(m, field->type);
+ irValue *ptr = ir_emit_ptr_offset(proc, data, ir_const_uintptr(offset));
+
+ args[0] = ptr;
+ args[1] = seed;
+ seed = ir_emit_call(proc, field_hasher, args);
+ }
+ ir_emit(proc, ir_instr_return(proc, seed));
+ } else if (type->kind == Type_Array) {
+ irValue *pres = ir_add_local_generated(proc, t_uintptr, false);
+ ir_emit_store(proc, pres, seed);
+
+ auto args = array_make<irValue *>(permanent_allocator(), 2);
+ irValue *elem_hasher = ir_get_hasher_proc_for_type(m, type->Array.elem);
+
+ auto loop_data = ir_loop_start(proc, type->Array.count, t_i32);
+
+ data = ir_emit_conv(proc, data, pt);
+
+ irValue *ptr = ir_emit_array_ep(proc, data, loop_data.idx);
+ args[0] = ptr;
+ args[1] = ir_emit_load(proc, pres);
+ irValue *new_seed = ir_emit_call(proc, elem_hasher, args);
+ ir_emit_store(proc, pres, new_seed);
+
+ ir_loop_end(proc, loop_data);
+
+ irValue *res = ir_emit_load(proc, pres);
+ ir_emit(proc, ir_instr_return(proc, res));
+ } else if (type->kind == Type_EnumeratedArray) {
+ irValue *pres = ir_add_local_generated(proc, t_uintptr, false);
+ ir_emit_store(proc, pres, seed);
+
+ auto args = array_make<irValue *>(permanent_allocator(), 2);
+ irValue *elem_hasher = ir_get_hasher_proc_for_type(m, type->Array.elem);
+
+ auto loop_data = ir_loop_start(proc, type->Array.count, t_i32);
+
+ data = ir_emit_conv(proc, data, pt);
+
+ irValue *ptr = ir_emit_array_ep(proc, data, loop_data.idx);
+ args[0] = ptr;
+ args[1] = ir_emit_load(proc, pres);
+ irValue *new_seed = ir_emit_call(proc, elem_hasher, args);
+ ir_emit_store(proc, pres, new_seed);
+
+ ir_loop_end(proc, loop_data);
+
+ irValue *res = ir_emit_load(proc, pres);
+ ir_emit(proc, ir_instr_return(proc, res));
+ } else {
+ GB_PANIC("Unhandled type for hasher: %s", type_to_string(type));
+ }
+
+ return p;
+}
+
irValue *ir_emit_comp(irProcedure *proc, TokenKind op_kind, irValue *left, irValue *right) {
Type *a = base_type(ir_type(left));
Type *b = base_type(ir_type(right));
@@ -4966,7 +5216,7 @@ irValue *ir_emit_comp(irProcedure *proc, TokenKind op_kind, irValue *left, irVal
} else {
if (is_type_simple_compare(tl) && (op_kind == Token_CmpEq || op_kind == Token_NotEq)) {
// TODO(bill): Test to see if this is actually faster!!!!
- auto args = array_make<irValue *>(heap_allocator(), 3);
+ auto args = array_make<irValue *>(permanent_allocator(), 3);
args[0] = ir_emit_conv(proc, lhs, t_rawptr);
args[1] = ir_emit_conv(proc, rhs, t_rawptr);
args[2] = ir_const_int(type_size_of(tl));
@@ -4992,6 +5242,30 @@ irValue *ir_emit_comp(irProcedure *proc, TokenKind op_kind, irValue *left, irVal
}
}
+ if (is_type_struct(a) && is_type_comparable(a)) {
+ irValue *left_ptr = ir_address_from_load_or_generate_local(proc, left);
+ irValue *right_ptr = ir_address_from_load_or_generate_local(proc, right);
+ irValue *res = {};
+ if (is_type_simple_compare(a)) {
+ // TODO(bill): Test to see if this is actually faster!!!!
+ auto args = array_make<irValue *>(permanent_allocator(), 3);
+ args[0] = ir_emit_conv(proc, left_ptr, t_rawptr);
+ args[1] = ir_emit_conv(proc, right_ptr, t_rawptr);
+ args[2] = ir_const_int(type_size_of(a));
+ res = ir_emit_runtime_call(proc, "memory_equal", args);
+ } else {
+ irValue *value = ir_get_equal_proc_for_type(proc->module, a);
+ auto args = array_make<irValue *>(permanent_allocator(), 2);
+ args[0] = ir_emit_conv(proc, left_ptr, t_rawptr);
+ args[1] = ir_emit_conv(proc, right_ptr, t_rawptr);
+ res = ir_emit_call(proc, value, args);
+ }
+ if (op_kind == Token_NotEq) {
+ res = ir_emit_unary_arith(proc, Token_Not, res, ir_type(res));
+ }
+ return res;
+ }
+
if (is_type_string(a)) {
if (is_type_cstring(a)) {
left = ir_emit_conv(proc, left, t_string);
@@ -6636,7 +6910,7 @@ void ir_mangle_add_sub_type_name(irModule *m, Entity *field, String parent) {
return;
}
if (is_type_proc(field->type)) {
- set_procedure_abi_types(heap_allocator(), field->type);
+ set_procedure_abi_types(field->type);
}
String cn = field->token.string;
@@ -6733,7 +7007,7 @@ irValue *ir_gen_anonymous_proc_lit(irModule *m, String prefix_name, Ast *expr, i
String name = make_string(name_text, name_len-1);
Type *type = type_of_expr(expr);
- set_procedure_abi_types(heap_allocator(), type);
+ set_procedure_abi_types(type);
irValue *value = ir_value_procedure(m, nullptr, type, pl->type, pl->body, name);
value->Proc.tags = pl->tags;
@@ -6789,6 +7063,9 @@ void ir_gen_global_type_name(irModule *m, Entity *e, String name) {
if (!ir_min_dep_entity(m, e)) {
return;
}
+ if (is_type_proc(e->type)) {
+ return;
+ }
irValue *t = ir_value_type_name(name, e->type);
ir_module_add_value(m, e, t);
string_map_set(&m->members, name, t);
@@ -6884,7 +7161,7 @@ irValue *ir_find_global_variable(irProcedure *proc, String name) {
return *value;
}
-void ir_build_stmt_list(irProcedure *proc, Array<Ast *> stmts);
+void ir_build_stmt_list(irProcedure *proc, Slice<Ast *> stmts);
void ir_build_assign_op(irProcedure *proc, irAddr const &lhs, irValue *value, TokenKind op);
bool is_double_pointer(Type *t) {
@@ -6898,17 +7175,6 @@ bool is_double_pointer(Type *t) {
return is_type_pointer(td);
}
-
-u64 ir_generate_source_code_location_hash(TokenPos pos) {
- u64 h = 0xcbf29ce484222325;
- for (isize i = 0; i < pos.file.len; i++) {
- h = (h ^ u64(pos.file[i])) * 0x100000001b3;
- }
- h = h ^ (u64(pos.line) * 0x100000001b3);
- h = h ^ (u64(pos.column) * 0x100000001b3);
- return h;
-}
-
irValue *ir_emit_source_code_location(irProcedure *proc, String procedure, TokenPos pos) {
gbAllocator a = ir_allocator();
irValue *v = ir_alloc_value(irValue_SourceCodeLocation);
@@ -6916,7 +7182,6 @@ irValue *ir_emit_source_code_location(irProcedure *proc, String procedure, Token
v->SourceCodeLocation.line = ir_const_int(pos.line);
v->SourceCodeLocation.column = ir_const_int(pos.column);
v->SourceCodeLocation.procedure = ir_find_or_add_entity_string(proc->module, procedure);
- v->SourceCodeLocation.hash = ir_generate_source_code_location_hash(pos);
return v;
}
@@ -7355,7 +7620,7 @@ irValue *ir_build_builtin_proc(irProcedure *proc, Ast *expr, TypeAndValue tv, Bu
// "Intrinsics"
case BuiltinProc_alloca:
{
- auto args = array_make<irValue *>(heap_allocator(), 2);
+ auto args = array_make<irValue *>(permanent_allocator(), 2);
args[0] = ir_emit_conv(proc, ir_build_expr(proc, ce->args[0]), t_i32);
args[1] = ir_build_expr(proc, ce->args[1]);
return ir_emit(proc, ir_instr_inline_code(proc, id, args, t_u8_ptr));
@@ -7459,7 +7724,11 @@ irValue *ir_build_builtin_proc(irProcedure *proc, Ast *expr, TypeAndValue tv, Bu
return ir_emit(proc, ir_instr_atomic_cxchg(proc, type, address, old_value, new_value, id));
}
+ case BuiltinProc_type_equal_proc:
+ return ir_get_equal_proc_for_type(proc->module, ce->args[0]->tav.type);
+ case BuiltinProc_type_hasher_proc:
+ return ir_get_hasher_proc_for_type(proc->module, ce->args[0]->tav.type);
}
GB_PANIC("Unhandled built-in procedure");
@@ -7584,7 +7853,7 @@ irValue *ir_build_call_expr(irProcedure *proc, Ast *expr) {
Type *proc_type_ = base_type(ir_type(value));
GB_ASSERT(proc_type_->kind == Type_Proc);
TypeProc *pt = &proc_type_->Proc;
- set_procedure_abi_types(heap_allocator(), proc_type_);
+ set_procedure_abi_types(proc_type_);
if (is_call_expr_field_value(ce)) {
auto args = array_make<irValue *>(ir_allocator(), pt->param_count);
@@ -7801,7 +8070,11 @@ irValue *ir_build_expr_internal(irProcedure *proc, Ast *expr) {
if (tv.value.kind != ExactValue_Invalid) {
// NOTE(bill): Edge case
- if (tv.value.kind != ExactValue_Compound &&
+ if (is_type_u8_array(tv.type) && tv.value.kind == ExactValue_String) {
+ return ir_add_module_constant(proc->module, tv.type, tv.value);
+ } else if (is_type_rune_array(tv.type) && tv.value.kind == ExactValue_String) {
+ return ir_add_module_constant(proc->module, tv.type, tv.value);
+ } else if (tv.value.kind != ExactValue_Compound &&
is_type_array(tv.type)) {
Type *elem = core_array_type(tv.type);
ExactValue value = convert_exact_value_for_type(tv.value, elem);
@@ -8207,7 +8480,7 @@ irValue *ir_build_expr_internal(irProcedure *proc, Ast *expr) {
irValue *addr = ir_address_from_load_or_generate_local(proc, right);
irValue *h = ir_gen_map_header(proc, addr, rt);
- irValue *key = ir_gen_map_key(proc, left, rt->Map.key);
+ irValue *key = ir_gen_map_hash(proc, left, rt->Map.key);
auto args = array_make<irValue *>(ir_allocator(), 2);
args[0] = h;
@@ -9024,8 +9297,7 @@ irAddr ir_build_addr(irProcedure *proc, Ast *expr) {
if (cl->elems.count > 0) {
ir_emit_store(proc, v, ir_add_module_constant(proc->module, type, exact_value_compound(expr)));
- auto temp_data = array_make<irCompoundLitElemTempData>(heap_allocator(), 0, cl->elems.count);
- defer (array_free(&temp_data));
+ auto temp_data = array_make<irCompoundLitElemTempData>(temporary_allocator(), 0, cl->elems.count);
// NOTE(bill): Separate value, gep, store into their own chunks
for_array(i, cl->elems) {
@@ -9123,8 +9395,7 @@ irAddr ir_build_addr(irProcedure *proc, Ast *expr) {
if (cl->elems.count > 0) {
ir_emit_store(proc, v, ir_add_module_constant(proc->module, type, exact_value_compound(expr)));
- auto temp_data = array_make<irCompoundLitElemTempData>(heap_allocator(), 0, cl->elems.count);
- defer (array_free(&temp_data));
+ auto temp_data = array_make<irCompoundLitElemTempData>(temporary_allocator(), 0, cl->elems.count);
// NOTE(bill): Separate value, gep, store into their own chunks
for_array(i, cl->elems) {
@@ -9232,8 +9503,7 @@ irAddr ir_build_addr(irProcedure *proc, Ast *expr) {
irValue *data = ir_emit_array_ep(proc, slice->ConstantSlice.backing_array, v_zero32);
- auto temp_data = array_make<irCompoundLitElemTempData>(heap_allocator(), 0, cl->elems.count);
- defer (array_free(&temp_data));
+ auto temp_data = array_make<irCompoundLitElemTempData>(temporary_allocator(), 0, cl->elems.count);
for_array(i, cl->elems) {
Ast *elem = cl->elems[i];
@@ -9574,7 +9844,7 @@ void ir_build_nested_proc(irProcedure *proc, AstProcLit *pd, Entity *e) {
name_len = gb_snprintf(cast(char *)name_text, name_len, "%.*s.%.*s-%d", LIT(proc->name), LIT(pd_name), guid);
String name = make_string(name_text, name_len-1);
- set_procedure_abi_types(heap_allocator(), e->type);
+ set_procedure_abi_types(e->type);
irValue *value = ir_value_procedure(proc->module, e, e->type, pd->type, pd->body, name);
value->Proc.tags = pd->tags;
@@ -9673,7 +9943,7 @@ void ir_build_constant_value_decl(irProcedure *proc, AstValueDecl *vd) {
return;
}
- set_procedure_abi_types(heap_allocator(), e->type);
+ set_procedure_abi_types(e->type);
irValue *value = ir_value_procedure(proc->module, e, e->type, pl->type, pl->body, name);
value->Proc.tags = pl->tags;
@@ -9692,7 +9962,7 @@ void ir_build_constant_value_decl(irProcedure *proc, AstValueDecl *vd) {
}
}
-void ir_build_stmt_list(irProcedure *proc, Array<Ast *> stmts) {
+void ir_build_stmt_list(irProcedure *proc, Slice<Ast *> stmts) {
// NOTE(bill): Precollect constant entities
for_array(i, stmts) {
Ast *stmt = stmts[i];
@@ -9844,13 +10114,8 @@ void ir_build_range_indexed(irProcedure *proc, irValue *expr, Type *val_type, ir
elem = ir_emit_load(proc, elem);
irValue *entry = ir_emit_ptr_offset(proc, elem, idx);
- val = ir_emit_load(proc, ir_emit_struct_ep(proc, entry, 2));
-
- irValue *key_raw = ir_emit_struct_ep(proc, entry, 0);
- key_raw = ir_emit_struct_ep(proc, key_raw, 1);
- irValue *key = ir_emit_conv(proc, key_raw, alloc_type_pointer(expr_type->Map.key));
-
- idx = ir_emit_load(proc, key);
+ idx = ir_emit_load(proc, ir_emit_struct_ep(proc, entry, 2));
+ val = ir_emit_load(proc, ir_emit_struct_ep(proc, entry, 3));
break;
}
@@ -9995,7 +10260,7 @@ void ir_build_range_enum(irProcedure *proc, Type *enum_type, Type *val_type, irV
irValue *max_count = ir_const_int(enum_count);
irValue *ti = ir_type_info(proc, t);
- irValue *variant = ir_emit_struct_ep(proc, ti, 3);
+ irValue *variant = ir_emit_struct_ep(proc, ti, 4);
irValue *eti_ptr = ir_emit_conv(proc, variant, t_type_info_enum_ptr);
irValue *values = ir_emit_load(proc, ir_emit_struct_ep(proc, eti_ptr, 2));
irValue *values_data = ir_slice_elem(proc, values);
@@ -10179,7 +10444,7 @@ void ir_build_stmt_internal(irProcedure *proc, Ast *node) {
String mangled_name = {};
{
- gbString str = gb_string_make_length(heap_allocator(), proc->name.text, proc->name.len);
+ gbString str = gb_string_make_length(permanent_allocator(), proc->name.text, proc->name.len);
str = gb_string_appendc(str, "-");
str = gb_string_append_fmt(str, ".%.*s-%llu", LIT(name), cast(long long)e->id);
mangled_name.text = cast(u8 *)str;
@@ -10902,7 +11167,7 @@ void ir_build_stmt_internal(irProcedure *proc, Ast *node) {
ast_node(body, BlockStmt, ss->body);
- Array<Ast *> default_stmts = {};
+ Slice<Ast *> default_stmts = {};
irBlock *default_fall = nullptr;
irBlock *default_block = nullptr;
@@ -11349,6 +11614,9 @@ void ir_begin_procedure_body(irProcedure *proc) {
bool ir_remove_dead_instr(irProcedure *proc) {
+ if (proc->ignore_dead_instr) {
+ return false;
+ }
isize elimination_count = 0;
retry:
#if 1
@@ -11471,11 +11739,11 @@ void ir_insert_code_before_proc(irProcedure* proc, irProcedure *parent) {
void ir_build_proc(irValue *value, irProcedure *parent) {
irProcedure *proc = &value->Proc;
- set_procedure_abi_types(heap_allocator(), proc->type);
+ set_procedure_abi_types(proc->type);
proc->parent = parent;
- if (proc->body != nullptr) {
+ if (proc->body != nullptr && proc->body->kind != Ast_Invalid) {
u64 prev_state_flags = proc->module->state_flags;
if (proc->tags != 0) {
@@ -11577,6 +11845,8 @@ void ir_init_module(irModule *m, Checker *c) {
map_init(&m->debug_info, heap_allocator());
map_init(&m->entity_names, heap_allocator());
map_init(&m->anonymous_proc_lits, heap_allocator());
+ map_init(&m->equal_procs, heap_allocator());
+ map_init(&m->hasher_procs, heap_allocator());
array_init(&m->procs, heap_allocator());
array_init(&m->procs_to_generate, heap_allocator());
array_init(&m->foreign_library_paths, heap_allocator());
@@ -11860,6 +12130,8 @@ void ir_setup_type_info_data(irProcedure *proc) { // NOTE(bill): Setup type_info
// Useful types
Type *t_i64_slice_ptr = alloc_type_pointer(alloc_type_slice(t_i64));
Type *t_string_slice_ptr = alloc_type_pointer(alloc_type_slice(t_string));
+ Entity *type_info_flags_entity = find_core_entity(info->checker, str_lit("Type_Info_Flags"));
+ Type *t_type_info_flags = type_info_flags_entity->type;
i32 type_info_member_types_index = 0;
i32 type_info_member_names_index = 0;
@@ -11879,11 +12151,14 @@ void ir_setup_type_info_data(irProcedure *proc) { // NOTE(bill): Setup type_info
irValue *tag = nullptr;
irValue *ti_ptr = ir_emit_array_epi(proc, ir_global_type_info_data, cast(i32)entry_index);
- irValue *variant_ptr = ir_emit_struct_ep(proc, ti_ptr, 3);
+ irValue *variant_ptr = ir_emit_struct_ep(proc, ti_ptr, 4);
+
+ irValue *type_info_flags = ir_value_constant(t_type_info_flags, exact_value_i64(type_info_flags_of_type(t)));
ir_emit_store(proc, ir_emit_struct_ep(proc, ti_ptr, 0), ir_const_int(type_size_of(t)));
ir_emit_store(proc, ir_emit_struct_ep(proc, ti_ptr, 1), ir_const_int(type_align_of(t)));
- ir_emit_store(proc, ir_emit_struct_ep(proc, ti_ptr, 2), ir_typeid(proc->module, t));
+ ir_emit_store(proc, ir_emit_struct_ep(proc, ti_ptr, 2), type_info_flags);
+ ir_emit_store(proc, ir_emit_struct_ep(proc, ti_ptr, 3), ir_typeid(proc->module, t));
switch (t->kind) {
@@ -11897,6 +12172,21 @@ void ir_setup_type_info_data(irProcedure *proc) { // NOTE(bill): Setup type_info
ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 0), name);
ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 1), gtip);
+
+ if (t->Named.type_name->pkg) {
+ irValue *name = ir_const_string(proc->module, t->Named.type_name->pkg->name);
+ ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 2), name);
+ }
+
+ String proc_name = {};
+ if (t->Named.type_name->parent_proc_decl) {
+ DeclInfo *decl = t->Named.type_name->parent_proc_decl;
+ if (decl->entity && decl->entity->kind == Entity_Procedure) {
+ proc_name = decl->entity->token.string;
+ }
+ }
+ irValue *loc = ir_emit_source_code_location(proc, proc_name, t->Named.type_name->token.pos);
+ ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 3), loc);
break;
}
@@ -12234,8 +12524,13 @@ void ir_setup_type_info_data(irProcedure *proc) { // NOTE(bill): Setup type_info
ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 6), is_raw_union);
ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 7), is_custom_align);
+ if (is_type_comparable(t) && !is_type_simple_compare(t)) {
+ ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 8), ir_get_equal_proc_for_type(proc->module, t));
+ }
+
+
if (t->Struct.soa_kind != StructSoa_None) {
- irValue *kind = ir_emit_struct_ep(proc, tag, 8);
+ irValue *kind = ir_emit_struct_ep(proc, tag, 9);
Type *kind_type = type_deref(ir_type(kind));
irValue *soa_kind = ir_value_constant(kind_type, exact_value_i64(t->Struct.soa_kind));
@@ -12244,8 +12539,8 @@ void ir_setup_type_info_data(irProcedure *proc) { // NOTE(bill): Setup type_info
ir_emit_store(proc, kind, soa_kind);
- ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 9), soa_type);
- ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 10), soa_len);
+ ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 10), soa_type);
+ ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 11), soa_len);
}
}
@@ -12308,10 +12603,14 @@ void ir_setup_type_info_data(irProcedure *proc) { // NOTE(bill): Setup type_info
irValue *key = ir_emit_struct_ep(proc, tag, 0);
irValue *value = ir_emit_struct_ep(proc, tag, 1);
irValue *generated_struct = ir_emit_struct_ep(proc, tag, 2);
+ irValue *key_equal = ir_emit_struct_ep(proc, tag, 3);
+ irValue *key_hasher = ir_emit_struct_ep(proc, tag, 4);
ir_emit_store(proc, key, ir_get_type_info_ptr(proc, t->Map.key));
ir_emit_store(proc, value, ir_get_type_info_ptr(proc, t->Map.value));
ir_emit_store(proc, generated_struct, ir_get_type_info_ptr(proc, t->Map.generated_struct_type));
+ ir_emit_store(proc, key_equal, ir_get_equal_proc_for_type(proc->module, t->Map.key));
+ ir_emit_store(proc, key_hasher, ir_get_hasher_proc_for_type(proc->module, t->Map.key));
break;
}
@@ -12612,7 +12911,7 @@ void ir_gen_tree(irGen *s) {
Ast *type_expr = pl->type;
- set_procedure_abi_types(heap_allocator(), e->type);
+ set_procedure_abi_types(e->type);
irValue *p = ir_value_procedure(m, e, e->type, type_expr, body, name);
p->Proc.tags = pl->tags;
p->Proc.inlining = pl->inlining;
@@ -12646,7 +12945,7 @@ void ir_gen_tree(irGen *s) {
#if defined(GB_SYSTEM_WINDOWS)
- if (build_context.build_mode == BuildMode_DynamicLibrary && !has_dll_main) {
+ if (build_context.build_mode == BuildMode_DynamicLibrary && !has_dll_main && !build_context.no_entry_point) {
// DllMain :: proc(inst: rawptr, reason: u32, reserved: rawptr) -> i32
String name = str_lit("DllMain");
Type *proc_params = alloc_type_tuple();
@@ -12717,7 +13016,7 @@ void ir_gen_tree(irGen *s) {
ir_emit_return(proc, v_one32);
}
#endif
- if (!(build_context.build_mode == BuildMode_DynamicLibrary && !has_dll_main)) {
+ if (!(build_context.build_mode == BuildMode_DynamicLibrary && !has_dll_main) && !build_context.no_entry_point) {
// main :: proc(argc: i32, argv: ^^u8) -> i32
String name = str_lit("main");
@@ -12784,11 +13083,18 @@ void ir_gen_tree(irGen *s) {
ir_fill_slice(proc, global_args, argv, ir_emit_conv(proc, argc, t_int));
ir_emit(proc, ir_alloc_instr(proc, irInstr_StartupRuntime));
- {
+ Array<irValue *> empty_args = {};
+ if (build_context.command_kind == Command_test) {
+ for_array(i, m->info->testing_procedures) {
+ Entity *e = m->info->testing_procedures[i];
+ irValue **found = map_get(&proc->module->values, hash_entity(e));
+ GB_ASSERT(found != nullptr);
+ ir_emit_call(proc, *found, empty_args);
+ }
+ } else {
irValue **found = map_get(&proc->module->values, hash_entity(entry_point));
if (found != nullptr) {
- Array<irValue *> args = {};
- ir_emit_call(proc, *found, args);
+ ir_emit_call(proc, *found, empty_args);
}
}
@@ -12796,7 +13102,7 @@ void ir_gen_tree(irGen *s) {
}
#if defined(GB_SYSTEM_WINDOWS)
- if (build_context.build_mode != BuildMode_DynamicLibrary && build_context.no_crt) {
+ if (build_context.build_mode != BuildMode_DynamicLibrary && build_context.no_crt && !build_context.no_entry_point) {
s->print_chkstk = true;
{
diff --git a/src/ir_print.cpp b/src/ir_print.cpp
index ceb95c5c3..a58ddbe0f 100644
--- a/src/ir_print.cpp
+++ b/src/ir_print.cpp
@@ -76,7 +76,6 @@ void ir_write_u64(irFileBuffer *f, u64 i) {
}
void ir_write_big_int(irFileBuffer *f, BigInt const &x, Type *type, bool swap_endian) {
if (x.len == 2) {
- gbAllocator a = heap_allocator(); // TODO(bill): Change this allocator
u64 words[2] = {};
BigInt y = x;
if (swap_endian) {
@@ -88,9 +87,8 @@ void ir_write_big_int(irFileBuffer *f, BigInt const &x, Type *type, bool swap_en
y.d.words = words;
}
- String s = big_int_to_string(a, &y, 10);
+ String s = big_int_to_string(temporary_allocator(), &y, 10);
ir_write_string(f, s);
- gb_free(a, s.text);
} else {
i64 i = 0;
if (x.neg) {
@@ -296,7 +294,7 @@ void ir_print_alignment_prefix_hack(irFileBuffer *f, i64 alignment) {
void ir_print_proc_results(irFileBuffer *f, irModule *m, Type *t) {
- set_procedure_abi_types(heap_allocator(), t);
+ set_procedure_abi_types(t);
GB_ASSERT(is_type_proc(t));
t = base_type(t);
@@ -325,7 +323,7 @@ void ir_print_proc_results(irFileBuffer *f, irModule *m, Type *t) {
void ir_print_proc_type_without_pointer(irFileBuffer *f, irModule *m, Type *t) {
- set_procedure_abi_types(heap_allocator(), t);
+ set_procedure_abi_types(t);
i64 word_bits = 8*build_context.word_size;
t = base_type(t);
@@ -736,6 +734,28 @@ void ir_print_exact_value(irFileBuffer *f, irModule *m, ExactValue value, Type *
if (is_type_array(type) && value.kind == ExactValue_String && !is_type_u8(core_array_type(type))) {
i64 count = type->Array.count;
Type *elem = type->Array.elem;
+
+ if (is_type_rune_array(type)) {
+ Rune rune;
+ isize offset = 0;
+ isize width = 1;
+ String s = value.value_string;
+ ir_write_byte(f, '[');
+ for (i64 i = 0; i < count && offset < s.len; i++) {
+ width = gb_utf8_decode(s.text+offset, s.len-offset, &rune);
+ if (i > 0) ir_write_str_lit(f, ", ");
+ ir_print_type(f, m, elem);
+ ir_write_byte(f, ' ');
+ ir_print_exact_value(f, m, exact_value_i64(rune), elem);
+ offset += width;
+ }
+ GB_ASSERT(offset == s.len);
+
+ ir_write_byte(f, ']');
+ return;
+ }
+
+
ir_write_byte(f, '[');
for (i64 i = 0; i < count; i++) {
@@ -747,7 +767,7 @@ void ir_print_exact_value(irFileBuffer *f, irModule *m, ExactValue value, Type *
ir_write_byte(f, ']');
return;
- } else if (is_type_array(type) &&
+ } else if (is_type_array(type) &&
value.kind != ExactValue_Invalid &&
value.kind != ExactValue_String &&
value.kind != ExactValue_Compound) {
@@ -798,7 +818,11 @@ void ir_print_exact_value(irFileBuffer *f, irModule *m, ExactValue value, Type *
GB_ASSERT(is_type_array(type));
ir_write_str_lit(f, "c\"");
ir_print_escape_string(f, str, false, false);
- ir_write_str_lit(f, "\\00\"");
+ if (type->Array.count == str.len) {
+ ir_write_str_lit(f, "\"");
+ } else {
+ ir_write_str_lit(f, "\\00\"");
+ }
} else if (is_type_cstring(t)) {
// HACK NOTE(bill): This is a hack but it works because strings are created at the very end
// of the .ll file
@@ -812,7 +836,7 @@ void ir_print_exact_value(irFileBuffer *f, irModule *m, ExactValue value, Type *
ir_write_str_lit(f, ", ");
ir_print_type(f, m, t_i32);
ir_write_str_lit(f, " 0, i32 0)");
- }else {
+ } else {
// HACK NOTE(bill): This is a hack but it works because strings are created at the very end
// of the .ll file
irValue *str_array = ir_add_global_string_array(m, str);
@@ -929,9 +953,9 @@ void ir_print_exact_value(irFileBuffer *f, irModule *m, ExactValue value, Type *
ir_write_byte(f, ' ');
ir_write_byte(f, '{');
ir_print_type(f, m, ft); ir_write_byte(f, ' ');
- ir_print_exact_value(f, m, exact_value_float(value.value_complex.real), ft);
+ ir_print_exact_value(f, m, exact_value_float(value.value_complex->real), ft);
ir_write_str_lit(f, ", "); ir_print_type(f, m, ft); ir_write_byte(f, ' ');
- ir_print_exact_value(f, m, exact_value_float(value.value_complex.imag), ft);
+ ir_print_exact_value(f, m, exact_value_float(value.value_complex->imag), ft);
ir_write_byte(f, '}');
break;
}
@@ -944,13 +968,13 @@ void ir_print_exact_value(irFileBuffer *f, irModule *m, ExactValue value, Type *
ir_write_byte(f, ' ');
ir_write_byte(f, '{');
ir_print_type(f, m, ft); ir_write_byte(f, ' ');
- ir_print_exact_value(f, m, exact_value_float(value.value_quaternion.imag), ft);
+ ir_print_exact_value(f, m, exact_value_float(value.value_quaternion->imag), ft);
ir_write_str_lit(f, ", "); ir_print_type(f, m, ft); ir_write_byte(f, ' ');
- ir_print_exact_value(f, m, exact_value_float(value.value_quaternion.jmag), ft);
+ ir_print_exact_value(f, m, exact_value_float(value.value_quaternion->jmag), ft);
ir_write_str_lit(f, ", "); ir_print_type(f, m, ft); ir_write_byte(f, ' ');
- ir_print_exact_value(f, m, exact_value_float(value.value_quaternion.kmag), ft);
+ ir_print_exact_value(f, m, exact_value_float(value.value_quaternion->kmag), ft);
ir_write_str_lit(f, ", "); ir_print_type(f, m, ft); ir_write_byte(f, ' ');
- ir_print_exact_value(f, m, exact_value_float(value.value_quaternion.real), ft);
+ ir_print_exact_value(f, m, exact_value_float(value.value_quaternion->real), ft);
ir_write_byte(f, '}');
break;
}
@@ -1406,7 +1430,6 @@ void ir_print_value(irFileBuffer *f, irModule *m, irValue *value, Type *type_hin
irValue *line = value->SourceCodeLocation.line;
irValue *column = value->SourceCodeLocation.column;
irValue *procedure = value->SourceCodeLocation.procedure;
- u64 hash = value->SourceCodeLocation.hash;
ir_write_byte(f, '{');
ir_print_type(f, m, t_string); ir_write_byte(f, ' '); ir_print_value(f, m, file, t_string);
@@ -1416,8 +1439,6 @@ void ir_print_value(irFileBuffer *f, irModule *m, irValue *value, Type *type_hin
ir_print_type(f, m, t_int); ir_write_byte(f, ' '); ir_print_value(f, m, column, t_int);
ir_write_string(f, str_lit(", "));
ir_print_type(f, m, t_string); ir_write_byte(f, ' '); ir_print_value(f, m, procedure, t_string);
- ir_write_string(f, str_lit(", "));
- ir_print_type(f, m, t_u64); ir_write_byte(f, ' '); ir_write_u64(f, hash);
ir_write_byte(f, '}');
break;
}
@@ -1551,7 +1572,11 @@ void ir_print_instr(irFileBuffer *f, irModule *m, irValue *value) {
break;
case BuiltinProc_cpu_relax:
- ir_write_str_lit(f, "call void asm sideeffect \"pause\", \"\"()");
+ if (build_context.metrics.arch == TargetArch_amd64) {
+ ir_write_str_lit(f, "call void asm sideeffect \"pause\", \"\"()");
+ } else {
+ // ir_write_str_lit(f, "call void asm sideeffect \"yield\", \"\"()");
+ }
break;
default: GB_PANIC("Unknown inline code %d", instr->InlineCode.id); break;
}
@@ -2189,7 +2214,7 @@ void ir_print_instr(irFileBuffer *f, irModule *m, irValue *value) {
irInstrCall *call = &instr->Call;
Type *proc_type = base_type(ir_type(call->value));
GB_ASSERT(is_type_proc(proc_type));
- set_procedure_abi_types(heap_allocator(), proc_type);
+ set_procedure_abi_types(proc_type);
bool is_c_vararg = proc_type->Proc.c_vararg;
Type *result_type = call->type;
@@ -2396,7 +2421,7 @@ void ir_print_instr(irFileBuffer *f, irModule *m, irValue *value) {
void ir_print_proc(irFileBuffer *f, irModule *m, irProcedure *proc) {
- set_procedure_abi_types(heap_allocator(), proc->type);
+ set_procedure_abi_types(proc->type);
if (proc->body == nullptr) {
ir_write_str_lit(f, "declare ");
diff --git a/src/llvm_abi.cpp b/src/llvm_abi.cpp
new file mode 100644
index 000000000..77d4b42b0
--- /dev/null
+++ b/src/llvm_abi.cpp
@@ -0,0 +1,959 @@
+enum lbArgKind {
+ lbArg_Direct,
+ lbArg_Indirect,
+ lbArg_Ignore,
+};
+
+struct lbArgType {
+ lbArgKind kind;
+ LLVMTypeRef type;
+ LLVMTypeRef cast_type; // Optional
+ LLVMTypeRef pad_type; // Optional
+ LLVMAttributeRef attribute; // Optional
+};
+
+lbArgType lb_arg_type_direct(LLVMTypeRef type, LLVMTypeRef cast_type, LLVMTypeRef pad_type, LLVMAttributeRef attr) {
+ return lbArgType{lbArg_Direct, type, cast_type, pad_type, attr};
+}
+lbArgType lb_arg_type_direct(LLVMTypeRef type) {
+ return lb_arg_type_direct(type, nullptr, nullptr, nullptr);
+}
+
+lbArgType lb_arg_type_indirect(LLVMTypeRef type, LLVMAttributeRef attr) {
+ return lbArgType{lbArg_Indirect, type, nullptr, nullptr, attr};
+}
+
+lbArgType lb_arg_type_ignore(LLVMTypeRef type) {
+ return lbArgType{lbArg_Ignore, type, nullptr, nullptr, nullptr};
+}
+
+struct lbFunctionType {
+ LLVMContextRef ctx;
+ ProcCallingConvention calling_convention;
+ Array<lbArgType> args;
+ lbArgType ret;
+};
+
+i64 llvm_align_formula(i64 off, i64 a) {
+ return (off + a - 1) / a * a;
+}
+
+
+bool lb_is_type_kind(LLVMTypeRef type, LLVMTypeKind kind) {
+ if (type == nullptr) {
+ return false;
+ }
+ return LLVMGetTypeKind(type) == kind;
+}
+
+LLVMTypeRef lb_function_type_to_llvm_ptr(lbFunctionType *ft, bool is_var_arg) {
+ unsigned arg_count = cast(unsigned)ft->args.count;
+ unsigned offset = 0;
+
+ LLVMTypeRef ret = nullptr;
+ if (ft->ret.kind == lbArg_Direct) {
+ if (ft->ret.cast_type != nullptr) {
+ ret = ft->ret.cast_type;
+ } else {
+ ret = ft->ret.type;
+ }
+ } else if (ft->ret.kind == lbArg_Indirect) {
+ offset += 1;
+ ret = LLVMVoidTypeInContext(ft->ctx);
+ } else if (ft->ret.kind == lbArg_Ignore) {
+ ret = LLVMVoidTypeInContext(ft->ctx);
+ }
+ GB_ASSERT_MSG(ret != nullptr, "%d", ft->ret.kind);
+
+ unsigned maximum_arg_count = offset+arg_count;
+ LLVMTypeRef *args = gb_alloc_array(heap_allocator(), LLVMTypeRef, maximum_arg_count);
+ if (offset == 1) {
+ GB_ASSERT(ft->ret.kind == lbArg_Indirect);
+ args[0] = LLVMPointerType(ft->ret.type, 0);
+ }
+
+ unsigned arg_index = offset;
+ for (unsigned i = 0; i < arg_count; i++) {
+ lbArgType *arg = &ft->args[i];
+ if (arg->kind == lbArg_Direct) {
+ LLVMTypeRef arg_type = nullptr;
+ if (ft->args[i].cast_type != nullptr) {
+ arg_type = arg->cast_type;
+ } else {
+ arg_type = arg->type;
+ }
+ args[arg_index++] = arg_type;
+ } else if (arg->kind == lbArg_Indirect) {
+ GB_ASSERT(!lb_is_type_kind(arg->type, LLVMPointerTypeKind));
+ args[arg_index++] = LLVMPointerType(arg->type, 0);
+ } else if (arg->kind == lbArg_Ignore) {
+ // ignore
+ }
+ }
+ unsigned total_arg_count = arg_index;
+ LLVMTypeRef func_type = LLVMFunctionType(ret, args, total_arg_count, is_var_arg);
+ return LLVMPointerType(func_type, 0);
+}
+
+
+void lb_add_function_type_attributes(LLVMValueRef fn, lbFunctionType *ft, ProcCallingConvention calling_convention) {
+ if (ft == nullptr) {
+ return;
+ }
+ unsigned arg_count = cast(unsigned)ft->args.count;
+ unsigned offset = 0;
+ if (ft->ret.kind == lbArg_Indirect) {
+ offset += 1;
+ }
+
+ LLVMContextRef c = ft->ctx;
+ LLVMAttributeRef noalias_attr = lb_create_enum_attribute(c, "noalias", true);
+ LLVMAttributeRef nonnull_attr = lb_create_enum_attribute(c, "nonnull", true);
+ LLVMAttributeRef nocapture_attr = lb_create_enum_attribute(c, "nocapture", true);
+
+ unsigned arg_index = offset;
+ for (unsigned i = 0; i < arg_count; i++) {
+ lbArgType *arg = &ft->args[i];
+ if (arg->kind == lbArg_Ignore) {
+ continue;
+ }
+
+ if (arg->attribute) {
+ LLVMAddAttributeAtIndex(fn, arg_index+1, arg->attribute);
+ }
+
+ arg_index++;
+ }
+
+ if (offset != 0 && ft->ret.kind == lbArg_Indirect && ft->ret.attribute != nullptr) {
+ LLVMAddAttributeAtIndex(fn, offset, ft->ret.attribute);
+ LLVMAddAttributeAtIndex(fn, offset, noalias_attr);
+ }
+
+ lbCallingConventionKind cc_kind = lbCallingConvention_C;
+ // TODO(bill): Clean up this logic
+ if (build_context.metrics.os != TargetOs_js) {
+ cc_kind = lb_calling_convention_map[calling_convention];
+ }
+ LLVMSetFunctionCallConv(fn, cc_kind);
+ if (calling_convention == ProcCC_Odin) {
+ unsigned context_index = offset+arg_count;
+ LLVMAddAttributeAtIndex(fn, context_index, noalias_attr);
+ LLVMAddAttributeAtIndex(fn, context_index, nonnull_attr);
+ LLVMAddAttributeAtIndex(fn, context_index, nocapture_attr);
+ }
+
+}
+
+i64 lb_sizeof(LLVMTypeRef type);
+i64 lb_alignof(LLVMTypeRef type);
+
+i64 lb_sizeof(LLVMTypeRef type) {
+ LLVMTypeKind kind = LLVMGetTypeKind(type);
+ switch (kind) {
+ case LLVMVoidTypeKind:
+ return 0;
+ case LLVMIntegerTypeKind:
+ {
+ unsigned w = LLVMGetIntTypeWidth(type);
+ return (w + 7)/8;
+ }
+ case LLVMFloatTypeKind:
+ return 4;
+ case LLVMDoubleTypeKind:
+ return 8;
+ case LLVMPointerTypeKind:
+ return build_context.word_size;
+ case LLVMStructTypeKind:
+ {
+ unsigned field_count = LLVMCountStructElementTypes(type);
+ i64 offset = 0;
+ if (LLVMIsPackedStruct(type)) {
+ for (unsigned i = 0; i < field_count; i++) {
+ LLVMTypeRef field = LLVMStructGetTypeAtIndex(type, i);
+ offset += lb_sizeof(field);
+ }
+ } else {
+ for (unsigned i = 0; i < field_count; i++) {
+ LLVMTypeRef field = LLVMStructGetTypeAtIndex(type, i);
+ i64 align = lb_alignof(field);
+ offset = llvm_align_formula(offset, align);
+ offset += lb_sizeof(field);
+ }
+ offset = llvm_align_formula(offset, lb_alignof(type));
+ }
+ return offset;
+ }
+ break;
+ case LLVMArrayTypeKind:
+ {
+ LLVMTypeRef elem = LLVMGetElementType(type);
+ i64 elem_size = lb_sizeof(elem);
+ i64 count = LLVMGetArrayLength(type);
+ i64 size = count * elem_size;
+ return size;
+ }
+ break;
+
+ case LLVMX86_MMXTypeKind:
+ return 8;
+ case LLVMVectorTypeKind:
+ {
+ LLVMTypeRef elem = LLVMGetElementType(type);
+ i64 elem_size = lb_sizeof(elem);
+ i64 count = LLVMGetVectorSize(type);
+ i64 size = count * elem_size;
+ return gb_clamp(next_pow2(size), 1, build_context.max_align);
+ }
+
+ }
+ GB_PANIC("Unhandled type for lb_sizeof -> %s", LLVMPrintTypeToString(type));
+
+ return 0;
+}
+
+i64 lb_alignof(LLVMTypeRef type) {
+ LLVMTypeKind kind = LLVMGetTypeKind(type);
+ switch (kind) {
+ case LLVMVoidTypeKind:
+ return 1;
+ case LLVMIntegerTypeKind:
+ {
+ unsigned w = LLVMGetIntTypeWidth(type);
+ return gb_clamp((w + 7)/8, 1, build_context.max_align);
+ }
+ case LLVMFloatTypeKind:
+ return 4;
+ case LLVMDoubleTypeKind:
+ return 8;
+ case LLVMPointerTypeKind:
+ return build_context.word_size;
+ case LLVMStructTypeKind:
+ {
+ if (LLVMIsPackedStruct(type)) {
+ return 1;
+ } else {
+ unsigned field_count = LLVMCountStructElementTypes(type);
+ i64 max_align = 1;
+ for (unsigned i = 0; i < field_count; i++) {
+ LLVMTypeRef field = LLVMStructGetTypeAtIndex(type, i);
+ i64 field_align = lb_alignof(field);
+ max_align = gb_max(max_align, field_align);
+ }
+ return max_align;
+ }
+ }
+ break;
+ case LLVMArrayTypeKind:
+ return lb_alignof(LLVMGetElementType(type));
+
+ case LLVMX86_MMXTypeKind:
+ return 8;
+ case LLVMVectorTypeKind:
+ {
+ LLVMTypeRef elem = LLVMGetElementType(type);
+ i64 elem_size = lb_sizeof(elem);
+ i64 count = LLVMGetVectorSize(type);
+ i64 size = count * elem_size;
+ return gb_clamp(next_pow2(size), 1, build_context.max_align);
+ }
+
+ }
+ GB_PANIC("Unhandled type for lb_sizeof -> %s", LLVMPrintTypeToString(type));
+
+ // LLVMValueRef v = LLVMAlignOf(type);
+ // GB_ASSERT(LLVMIsConstant(v));
+ // return LLVMConstIntGetSExtValue(v);
+ return 1;
+}
+
+#if 0
+Type *lb_abi_to_odin_type(lbModule *m, LLVMTypeRef type, bool is_return, u32 level = 0) {
+ Type **found = map_get(&m->llvm_types, hash_pointer(type));
+ if (found) {
+ return *found;
+ }
+ GB_ASSERT_MSG(level < 64, "%s %d", LLVMPrintTypeToString(type), is_return);
+
+ LLVMTypeKind kind = LLVMGetTypeKind(type);
+ switch (kind) {
+ case LLVMVoidTypeKind:
+ return nullptr;
+ case LLVMIntegerTypeKind:
+ {
+ unsigned w = LLVMGetIntTypeWidth(type);
+ if (w == 1) {
+ return t_llvm_bool;
+ }
+ unsigned bytes = (w + 7)/8;
+ switch (bytes) {
+ case 1: return t_u8;
+ case 2: return t_u16;
+ case 4: return t_u32;
+ case 8: return t_u64;
+ case 16: return t_u128;
+ }
+ GB_PANIC("Unhandled integer type");
+ }
+ case LLVMFloatTypeKind:
+ return t_f32;
+ case LLVMDoubleTypeKind:
+ return t_f64;
+ case LLVMPointerTypeKind:
+ {
+ LLVMTypeRef elem = LLVMGetElementType(type);
+ if (lb_is_type_kind(elem, LLVMFunctionTypeKind)) {
+ unsigned param_count = LLVMCountParamTypes(elem);
+ LLVMTypeRef *params = gb_alloc_array(heap_allocator(), LLVMTypeRef, param_count);
+ defer (gb_free(heap_allocator(), params));
+ LLVMGetParamTypes(elem, params);
+
+ Type **param_types = gb_alloc_array(heap_allocator(), Type *, param_count);
+ defer (gb_free(heap_allocator(), param_types));
+
+ for (unsigned i = 0; i < param_count; i++) {
+ param_types[i] = lb_abi_to_odin_type(m, params[i], false, level+1);
+ }
+
+ LLVMTypeRef ret = LLVMGetReturnType(elem);
+ Type *ret_type = lb_abi_to_odin_type(m, ret, true, level+1);
+
+ bool is_c_vararg = !!LLVMIsFunctionVarArg(elem);
+ return alloc_type_proc_from_types(param_types, param_count, ret_type, is_c_vararg);
+ }
+ return alloc_type_pointer(lb_abi_to_odin_type(m, elem, false, level+1));
+ }
+ case LLVMFunctionTypeKind:
+ GB_PANIC("LLVMFunctionTypeKind should not be seen on its own");
+ break;
+
+ case LLVMStructTypeKind:
+ {
+ unsigned field_count = LLVMCountStructElementTypes(type);
+ Type **fields = gb_alloc_array(heap_allocator(), Type *, field_count);
+ for (unsigned i = 0; i < field_count; i++) {
+ LLVMTypeRef field_type = LLVMStructGetTypeAtIndex(type, i);
+ if (lb_is_type_kind(field_type, LLVMPointerTypeKind) && level > 0) {
+ fields[i] = t_rawptr;
+ } else {
+ fields[i] = lb_abi_to_odin_type(m, field_type, false, level+1);
+ }
+ }
+ if (is_return) {
+ return alloc_type_tuple_from_field_types(fields, field_count, !!LLVMIsPackedStruct(type), false);
+ } else {
+ return alloc_type_struct_from_field_types(fields, field_count, !!LLVMIsPackedStruct(type));
+ }
+ }
+ break;
+ case LLVMArrayTypeKind:
+ {
+
+ i64 count = LLVMGetArrayLength(type);
+ Type *elem = lb_abi_to_odin_type(m, LLVMGetElementType(type), false, level+1);
+ return alloc_type_array(elem, count);
+ }
+ break;
+
+ case LLVMX86_MMXTypeKind:
+ return t_vector_x86_mmx;
+ case LLVMVectorTypeKind:
+ {
+ i64 count = LLVMGetVectorSize(type);
+ Type *elem = lb_abi_to_odin_type(m, LLVMGetElementType(type), false, level+1);
+ return alloc_type_simd_vector(count, elem);
+ }
+
+ }
+ GB_PANIC("Unhandled type for lb_abi_to_odin_type -> %s", LLVMPrintTypeToString(type));
+
+ return 0;
+}
+#endif
+
+
+#define LB_ABI_INFO(name) lbFunctionType *name(LLVMContextRef c, LLVMTypeRef *arg_types, unsigned arg_count, LLVMTypeRef return_type, bool return_is_defined, ProcCallingConvention calling_convention)
+typedef LB_ABI_INFO(lbAbiInfoType);
+
+
+// NOTE(bill): I hate `namespace` in C++ but this is just because I don't want to prefix everything
+namespace lbAbi386 {
+ Array<lbArgType> compute_arg_types(LLVMContextRef c, LLVMTypeRef *arg_types, unsigned arg_count);
+ lbArgType compute_return_type(LLVMContextRef c, LLVMTypeRef return_type, bool return_is_defined);
+
+ LB_ABI_INFO(abi_info) {
+ lbFunctionType *ft = gb_alloc_item(heap_allocator(), lbFunctionType);
+ ft->ctx = c;
+ ft->args = compute_arg_types(c, arg_types, arg_count);
+ ft->ret = compute_return_type(c, return_type, return_is_defined);
+ ft->calling_convention = calling_convention;
+ return ft;
+ }
+
+ lbArgType non_struct(LLVMContextRef c, LLVMTypeRef type, bool is_return) {
+ if (!is_return && lb_sizeof(type) > 8) {
+ return lb_arg_type_indirect(type, nullptr);
+ }
+
+ if (build_context.metrics.os == TargetOs_windows &&
+ build_context.word_size == 8 &&
+ lb_is_type_kind(type, LLVMIntegerTypeKind) &&
+ type == LLVMIntTypeInContext(c, 128)) {
+ // NOTE(bill): Because Windows AMD64 is weird
+ LLVMTypeRef cast_type = LLVMVectorType(LLVMInt64TypeInContext(c), 2);
+ return lb_arg_type_direct(type, cast_type, nullptr, nullptr);
+ }
+
+ LLVMAttributeRef attr = nullptr;
+ LLVMTypeRef i1 = LLVMInt1TypeInContext(c);
+ if (type == i1) {
+ attr = lb_create_enum_attribute(c, "zeroext", true);
+ }
+ return lb_arg_type_direct(type, nullptr, nullptr, attr);
+ }
+
+ Array<lbArgType> compute_arg_types(LLVMContextRef c, LLVMTypeRef *arg_types, unsigned arg_count) {
+ auto args = array_make<lbArgType>(heap_allocator(), arg_count);
+
+ for (unsigned i = 0; i < arg_count; i++) {
+ LLVMTypeRef t = arg_types[i];
+ LLVMTypeKind kind = LLVMGetTypeKind(t);
+ i64 sz = lb_sizeof(t);
+ if (kind == LLVMStructTypeKind) {
+ if (sz == 0) {
+ args[i] = lb_arg_type_ignore(t);
+ } else {
+ args[i] = lb_arg_type_indirect(t, lb_create_enum_attribute(c, "byval", true));
+ }
+ } else {
+ args[i] = non_struct(c, t, false);
+ }
+ }
+ return args;
+ }
+
+ lbArgType compute_return_type(LLVMContextRef c, LLVMTypeRef return_type, bool return_is_defined) {
+ if (!return_is_defined) {
+ return lb_arg_type_direct(LLVMVoidTypeInContext(c));
+ } else if (lb_is_type_kind(return_type, LLVMStructTypeKind) || lb_is_type_kind(return_type, LLVMArrayTypeKind)) {
+ i64 sz = lb_sizeof(return_type);
+ switch (sz) {
+ case 1: return lb_arg_type_direct(return_type, LLVMIntTypeInContext(c, 8), nullptr, nullptr);
+ case 2: return lb_arg_type_direct(return_type, LLVMIntTypeInContext(c, 16), nullptr, nullptr);
+ case 4: return lb_arg_type_direct(return_type, LLVMIntTypeInContext(c, 32), nullptr, nullptr);
+ case 8: return lb_arg_type_direct(return_type, LLVMIntTypeInContext(c, 64), nullptr, nullptr);
+ }
+ return lb_arg_type_indirect(return_type, lb_create_enum_attribute(c, "sret", true));
+ }
+ return non_struct(c, return_type, true);
+ }
+};
+
+namespace lbAbiAmd64Win64 {
+ Array<lbArgType> compute_arg_types(LLVMContextRef c, LLVMTypeRef *arg_types, unsigned arg_count);
+
+
+ LB_ABI_INFO(abi_info) {
+ lbFunctionType *ft = gb_alloc_item(heap_allocator(), lbFunctionType);
+ ft->ctx = c;
+ ft->args = compute_arg_types(c, arg_types, arg_count);
+ ft->ret = lbAbi386::compute_return_type(c, return_type, return_is_defined);
+ ft->calling_convention = calling_convention;
+ return ft;
+ }
+
+ Array<lbArgType> compute_arg_types(LLVMContextRef c, LLVMTypeRef *arg_types, unsigned arg_count) {
+ auto args = array_make<lbArgType>(heap_allocator(), arg_count);
+
+ for (unsigned i = 0; i < arg_count; i++) {
+ LLVMTypeRef t = arg_types[i];
+ LLVMTypeKind kind = LLVMGetTypeKind(t);
+ if (kind == LLVMStructTypeKind) {
+ i64 sz = lb_sizeof(t);
+ switch (sz) {
+ case 1:
+ case 2:
+ case 4:
+ case 8:
+ args[i] = lb_arg_type_direct(t, LLVMIntTypeInContext(c, 8*cast(unsigned)sz), nullptr, nullptr);
+ break;
+ default:
+ args[i] = lb_arg_type_indirect(t, nullptr);
+ break;
+ }
+ } else {
+ args[i] = lbAbi386::non_struct(c, t, false);
+ }
+ }
+ return args;
+ }
+};
+
+// NOTE(bill): I hate `namespace` in C++ but this is just because I don't want to prefix everything
+namespace lbAbiAmd64SysV {
+ enum RegClass {
+ RegClass_NoClass,
+ RegClass_Int,
+ RegClass_SSEFs,
+ RegClass_SSEFv,
+ RegClass_SSEDs,
+ RegClass_SSEDv,
+ RegClass_SSEInt8,
+ RegClass_SSEInt16,
+ RegClass_SSEInt32,
+ RegClass_SSEInt64,
+ RegClass_SSEUp,
+ RegClass_X87,
+ RegClass_X87Up,
+ RegClass_ComplexX87,
+ RegClass_Memory,
+ };
+
+ bool is_sse(RegClass reg_class) {
+ switch (reg_class) {
+ case RegClass_SSEFs:
+ case RegClass_SSEFv:
+ case RegClass_SSEDv:
+ return true;
+ }
+ return false;
+ }
+
+ void all_mem(Array<RegClass> *cs) {
+ for_array(i, *cs) {
+ (*cs)[i] = RegClass_Memory;
+ }
+ }
+
+ enum Amd64TypeAttributeKind {
+ Amd64TypeAttribute_None,
+ Amd64TypeAttribute_ByVal,
+ Amd64TypeAttribute_StructRect,
+ };
+
+ Array<lbArgType> compute_arg_types(LLVMContextRef c, LLVMTypeRef *arg_types, unsigned arg_count);
+ lbArgType compute_return_type(LLVMContextRef c, LLVMTypeRef return_type, bool return_is_defined);
+ void classify_with(LLVMTypeRef t, Array<RegClass> *cls, i64 ix, i64 off);
+ void fixup(LLVMTypeRef t, Array<RegClass> *cls);
+ lbArgType amd64_type(LLVMContextRef c, LLVMTypeRef type, Amd64TypeAttributeKind attribute_kind);
+ Array<RegClass> classify(LLVMTypeRef t);
+ LLVMTypeRef llreg(LLVMContextRef c, Array<RegClass> const &reg_classes);
+
+ LB_ABI_INFO(abi_info) {
+ lbFunctionType *ft = gb_alloc_item(heap_allocator(), lbFunctionType);
+ ft->ctx = c;
+ ft->calling_convention = calling_convention;
+
+ ft->args = array_make<lbArgType>(heap_allocator(), arg_count);
+ for (unsigned i = 0; i < arg_count; i++) {
+ ft->args[i] = amd64_type(c, arg_types[i], Amd64TypeAttribute_ByVal);
+ }
+
+ if (return_is_defined) {
+ ft->ret = amd64_type(c, return_type, Amd64TypeAttribute_StructRect);
+ } else {
+ ft->ret = lb_arg_type_direct(LLVMVoidTypeInContext(c));
+ }
+
+ return ft;
+ }
+
+ bool is_mem_cls(Array<RegClass> const &cls, Amd64TypeAttributeKind attribute_kind) {
+ if (attribute_kind == Amd64TypeAttribute_ByVal) {
+ if (cls.count == 0) {
+ return false;
+ }
+ auto first = cls[0];
+ return first == RegClass_Memory || first == RegClass_X87 || first == RegClass_ComplexX87;
+ } else if (attribute_kind == Amd64TypeAttribute_StructRect) {
+ if (cls.count == 0) {
+ return false;
+ }
+ return cls[0] == RegClass_Memory;
+ }
+ return false;
+ }
+
+ bool is_register(LLVMTypeRef type) {
+ LLVMTypeKind kind = LLVMGetTypeKind(type);
+ switch (kind) {
+ case LLVMIntegerTypeKind:
+ case LLVMFloatTypeKind:
+ case LLVMDoubleTypeKind:
+ case LLVMPointerTypeKind:
+ return true;
+ }
+ return false;
+ }
+
+ lbArgType amd64_type(LLVMContextRef c, LLVMTypeRef type, Amd64TypeAttributeKind attribute_kind) {
+ if (is_register(type)) {
+ LLVMAttributeRef attribute = nullptr;
+ if (type == LLVMInt1TypeInContext(c)) {
+ attribute = lb_create_enum_attribute(c, "zeroext", true);
+ }
+ return lb_arg_type_direct(type, nullptr, nullptr, attribute);
+ }
+
+ auto cls = classify(type);
+ if (is_mem_cls(cls, attribute_kind)) {
+ LLVMAttributeRef attribute = nullptr;
+ if (attribute_kind == Amd64TypeAttribute_ByVal) {
+ attribute = lb_create_enum_attribute(c, "byval", true);
+ } else if (attribute_kind == Amd64TypeAttribute_StructRect) {
+ attribute = lb_create_enum_attribute(c, "sret", true);
+ }
+ return lb_arg_type_indirect(type, attribute);
+ } else {
+ return lb_arg_type_direct(type, llreg(c, cls), nullptr, nullptr);
+ }
+ }
+
+ lbArgType non_struct(LLVMContextRef c, LLVMTypeRef type) {
+ LLVMAttributeRef attr = nullptr;
+ LLVMTypeRef i1 = LLVMInt1TypeInContext(c);
+ if (type == i1) {
+ attr = lb_create_enum_attribute(c, "zeroext", true);
+ }
+ return lb_arg_type_direct(type, nullptr, nullptr, attr);
+ }
+
+ Array<RegClass> classify(LLVMTypeRef t) {
+ i64 sz = lb_sizeof(t);
+ i64 words = (sz + 7)/8;
+ auto reg_classes = array_make<RegClass>(heap_allocator(), cast(isize)words);
+ if (words > 4) {
+ all_mem(&reg_classes);
+ } else {
+ classify_with(t, &reg_classes, 0, 0);
+ fixup(t, &reg_classes);
+ }
+ return reg_classes;
+ }
+
+ void unify(Array<RegClass> *cls, i64 i, RegClass newv) {
+ RegClass &oldv = (*cls)[i];
+ if (oldv == newv) {
+ return;
+ } else if (oldv == RegClass_NoClass) {
+ oldv = newv;
+ } else if (newv == RegClass_NoClass) {
+ return;
+ } else if (oldv == RegClass_Memory || newv == RegClass_Memory) {
+ return;
+ } else if (oldv == RegClass_Int || newv == RegClass_Int) {
+ return;
+ } else if (oldv == RegClass_X87 || oldv == RegClass_X87Up || oldv == RegClass_ComplexX87 ||
+ newv == RegClass_X87 || newv == RegClass_X87Up || newv == RegClass_ComplexX87) {
+ oldv = RegClass_Memory;
+ } else {
+ oldv = newv;
+ }
+ }
+
+ void fixup(LLVMTypeRef t, Array<RegClass> *cls) {
+ i64 i = 0;
+ i64 e = cls->count;
+ if (e > 2 && (lb_is_type_kind(t, LLVMStructTypeKind) || lb_is_type_kind(t, LLVMArrayTypeKind))) {
+ RegClass &oldv = (*cls)[i];
+ if (is_sse(oldv)) {
+ for (i++; i < e; i++) {
+ if (oldv != RegClass_SSEUp) {
+ all_mem(cls);
+ return;
+ }
+ }
+ } else {
+ all_mem(cls);
+ return;
+ }
+ } else {
+ while (i < e) {
+ RegClass &oldv = (*cls)[i];
+ if (oldv == RegClass_Memory) {
+ all_mem(cls);
+ return;
+ } else if (oldv == RegClass_X87Up) {
+ // NOTE(bill): Darwin
+ all_mem(cls);
+ return;
+ } else if (oldv == RegClass_SSEUp) {
+ oldv = RegClass_SSEDv;
+ } else if (is_sse(oldv)) {
+ i++;
+ while (i != e && oldv == RegClass_SSEUp) {
+ i++;
+ }
+ } else if (oldv == RegClass_X87) {
+ i++;
+ while (i != e && oldv == RegClass_X87Up) {
+ i++;
+ }
+ } else {
+ i++;
+ }
+ }
+ }
+ }
+
+ unsigned llvec_len(Array<RegClass> const &reg_classes, isize offset) {
+ unsigned len = 1;
+ for (isize i = offset+1; i < reg_classes.count; i++) {
+ if (reg_classes[offset] != RegClass_SSEFv && reg_classes[i] != RegClass_SSEUp) {
+ break;
+ }
+ len++;
+ }
+ return len;
+ }
+
+
+ LLVMTypeRef llreg(LLVMContextRef c, Array<RegClass> const &reg_classes) {
+ auto types = array_make<LLVMTypeRef>(heap_allocator(), 0, reg_classes.count);
+ for_array(i, reg_classes) {
+ RegClass reg_class = reg_classes[i];
+ switch (reg_class) {
+ case RegClass_Int:
+ array_add(&types, LLVMIntTypeInContext(c, 64));
+ break;
+ case RegClass_SSEFv:
+ case RegClass_SSEDv:
+ case RegClass_SSEInt8:
+ case RegClass_SSEInt16:
+ case RegClass_SSEInt32:
+ case RegClass_SSEInt64:
+ {
+ unsigned elems_per_word = 0;
+ LLVMTypeRef elem_type = nullptr;
+ switch (reg_class) {
+ case RegClass_SSEFv:
+ elems_per_word = 2;
+ elem_type = LLVMFloatTypeInContext(c);
+ break;
+ case RegClass_SSEDv:
+ elems_per_word = 1;
+ elem_type = LLVMDoubleTypeInContext(c);
+ break;
+ case RegClass_SSEInt8:
+ elems_per_word = 64/8;
+ elem_type = LLVMIntTypeInContext(c, 8);
+ break;
+ case RegClass_SSEInt16:
+ elems_per_word = 64/16;
+ elem_type = LLVMIntTypeInContext(c, 16);
+ break;
+ case RegClass_SSEInt32:
+ elems_per_word = 64/32;
+ elem_type = LLVMIntTypeInContext(c, 32);
+ break;
+ case RegClass_SSEInt64:
+ elems_per_word = 64/64;
+ elem_type = LLVMIntTypeInContext(c, 64);
+ break;
+ }
+
+ unsigned vec_len = llvec_len(reg_classes, i);
+ LLVMTypeRef vec_type = LLVMVectorType(elem_type, vec_len * elems_per_word);
+ array_add(&types, vec_type);
+ i += vec_len;
+ continue;
+ }
+ break;
+ case RegClass_SSEFs:
+ array_add(&types, LLVMFloatTypeInContext(c));
+ break;
+ case RegClass_SSEDs:
+ array_add(&types, LLVMDoubleTypeInContext(c));
+ break;
+ default:
+ GB_PANIC("Unhandled RegClass");
+ }
+ }
+
+ GB_ASSERT(types.count != 0);
+ if (types.count == 1) {
+ return types[0];
+ }
+ return LLVMStructTypeInContext(c, types.data, cast(unsigned)types.count, false);
+ }
+
+ void classify_with(LLVMTypeRef t, Array<RegClass> *cls, i64 ix, i64 off) {
+ i64 t_align = lb_alignof(t);
+ i64 t_size = lb_sizeof(t);
+
+ i64 misalign = off % t_align;
+ if (misalign != 0) {
+ i64 e = (off + t_size + 7) / 8;
+ for (i64 i = off / 8; i < e; i++) {
+ unify(cls, ix+i, RegClass_Memory);
+ }
+ return;
+ }
+
+ switch (LLVMGetTypeKind(t)) {
+ case LLVMIntegerTypeKind:
+ case LLVMPointerTypeKind:
+ unify(cls, ix + off/8, RegClass_Int);
+ break;
+ case LLVMFloatTypeKind:
+ unify(cls, ix + off/8, (off%8 == 4) ? RegClass_SSEFv : RegClass_SSEFs);
+ break;
+ case LLVMDoubleTypeKind:
+ unify(cls, ix + off/8, RegClass_SSEDs);
+ break;
+ case LLVMStructTypeKind:
+ {
+ LLVMBool packed = LLVMIsPackedStruct(t);
+ unsigned field_count = LLVMCountStructElementTypes(t);
+
+ i64 field_off = off;
+ for (unsigned field_index = 0; field_index < field_count; field_index++) {
+ LLVMTypeRef field_type = LLVMStructGetTypeAtIndex(t, field_index);
+ if (!packed) {
+ field_off = llvm_align_formula(field_off, lb_alignof(field_type));
+ }
+ classify_with(field_type, cls, ix, field_off);
+ field_off += lb_sizeof(field_type);
+ }
+ }
+ break;
+ case LLVMArrayTypeKind:
+ {
+ i64 len = LLVMGetArrayLength(t);
+ LLVMTypeRef elem = LLVMGetElementType(t);
+ i64 elem_sz = lb_sizeof(elem);
+ for (i64 i = 0; i < len; i++) {
+ classify_with(elem, cls, ix, off + i*elem_sz);
+ }
+ }
+ break;
+ case LLVMVectorTypeKind:
+ {
+ i64 len = LLVMGetVectorSize(t);
+ LLVMTypeRef elem = LLVMGetElementType(t);
+ i64 elem_sz = lb_sizeof(elem);
+ LLVMTypeKind elem_kind = LLVMGetTypeKind(elem);
+ RegClass reg = RegClass_NoClass;
+ switch (elem_kind) {
+ case LLVMIntegerTypeKind:
+ switch (LLVMGetIntTypeWidth(elem)) {
+ case 8: reg = RegClass_SSEInt8;
+ case 16: reg = RegClass_SSEInt16;
+ case 32: reg = RegClass_SSEInt32;
+ case 64: reg = RegClass_SSEInt64;
+ default:
+ GB_PANIC("Unhandled integer width for vector type");
+ }
+ break;
+ case LLVMFloatTypeKind:
+ reg = RegClass_SSEFv;
+ break;
+ case LLVMDoubleTypeKind:
+ reg = RegClass_SSEDv;
+ break;
+ default:
+ GB_PANIC("Unhandled vector element type");
+ }
+
+ for (i64 i = 0; i < len; i++) {
+ unify(cls, ix + (off + i*elem_sz)/8, reg);
+ // NOTE(bill): Everything after the first one is the upper
+ // half of a register
+ reg = RegClass_SSEUp;
+ }
+ }
+ break;
+ default:
+ GB_PANIC("Unhandled type");
+ break;
+ }
+ }
+
+ Array<lbArgType> compute_arg_types(LLVMContextRef c, LLVMTypeRef *arg_types, unsigned arg_count) {
+ auto args = array_make<lbArgType>(heap_allocator(), arg_count);
+
+ for (unsigned i = 0; i < arg_count; i++) {
+ LLVMTypeRef t = arg_types[i];
+ LLVMTypeKind kind = LLVMGetTypeKind(t);
+ if (kind == LLVMStructTypeKind) {
+ i64 sz = lb_sizeof(t);
+ if (sz == 0) {
+ args[i] = lb_arg_type_ignore(t);
+ } else {
+ args[i] = lb_arg_type_indirect(t, lb_create_enum_attribute(c, "byval", true));
+ }
+ } else {
+ args[i] = non_struct(c, t);
+ }
+ }
+ return args;
+ }
+
+ lbArgType compute_return_type(LLVMContextRef c, LLVMTypeRef return_type, bool return_is_defined) {
+ if (!return_is_defined) {
+ return lb_arg_type_direct(LLVMVoidTypeInContext(c));
+ } else if (lb_is_type_kind(return_type, LLVMStructTypeKind)) {
+ i64 sz = lb_sizeof(return_type);
+ switch (sz) {
+ case 1: return lb_arg_type_direct(return_type, LLVMIntTypeInContext(c, 8), nullptr, nullptr);
+ case 2: return lb_arg_type_direct(return_type, LLVMIntTypeInContext(c, 16), nullptr, nullptr);
+ case 4: return lb_arg_type_direct(return_type, LLVMIntTypeInContext(c, 32), nullptr, nullptr);
+ case 8: return lb_arg_type_direct(return_type, LLVMIntTypeInContext(c, 64), nullptr, nullptr);
+ }
+ return lb_arg_type_indirect(return_type, lb_create_enum_attribute(c, "sret", true));
+ } else if (build_context.metrics.os == TargetOs_windows && lb_is_type_kind(return_type, LLVMIntegerTypeKind) && lb_sizeof(return_type) == 16) {
+ return lb_arg_type_direct(return_type, LLVMIntTypeInContext(c, 128), nullptr, nullptr);
+ }
+ return non_struct(c, return_type);
+ }
+};
+
+
+namespace lbAbiAarch64 {
+ LB_ABI_INFO(abi_info) {
+ lbFunctionType *ft = gb_alloc_item(heap_allocator(), lbFunctionType);
+ ft->ctx = c;
+ // ft->args = compute_arg_types(c, arg_types, arg_count);
+ // ft->ret = lbAbi386::compute_return_type(c, return_type, return_is_defined);
+ // ft->calling_convention = calling_convention;
+ return ft;
+ }
+}
+
+
+LB_ABI_INFO(lb_get_abi_info) {
+ switch (calling_convention) {
+ case ProcCC_None:
+ case ProcCC_PureNone:
+ case ProcCC_InlineAsm:
+ {
+ lbFunctionType *ft = gb_alloc_item(heap_allocator(), lbFunctionType);
+ ft->ctx = c;
+ ft->args = array_make<lbArgType>(heap_allocator(), arg_count);
+ for (unsigned i = 0; i < arg_count; i++) {
+ ft->args[i] = lb_arg_type_direct(arg_types[i]);
+ }
+ if (return_is_defined) {
+ ft->ret = lb_arg_type_direct(return_type);
+ } else {
+ ft->ret = lb_arg_type_direct(LLVMVoidTypeInContext(c));
+ }
+ ft->calling_convention = calling_convention;
+ return ft;
+ }
+ }
+
+ if (build_context.metrics.arch == TargetArch_amd64) {
+ if (build_context.metrics.os == TargetOs_windows) {
+ return lbAbiAmd64Win64::abi_info(c, arg_types, arg_count, return_type, return_is_defined, calling_convention);
+ } else {
+ return lbAbiAmd64SysV::abi_info(c, arg_types, arg_count, return_type, return_is_defined, calling_convention);
+ }
+ } else if (build_context.metrics.arch == TargetArch_386) {
+ return lbAbi386::abi_info(c, arg_types, arg_count, return_type, return_is_defined, calling_convention);
+ } else if (build_context.metrics.arch == TargetArch_wasm32) {
+ return lbAbi386::abi_info(c, arg_types, arg_count, return_type, return_is_defined, calling_convention);
+ }
+ GB_PANIC("Unsupported ABI");
+ return {};
+}
diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp
index 4abc65ab4..d92108044 100644
--- a/src/llvm_backend.cpp
+++ b/src/llvm_backend.cpp
@@ -1,4 +1,11 @@
#include "llvm_backend.hpp"
+#include "llvm_abi.cpp"
+
+#ifdef USE_NEW_LLVM_ABI_SYSTEM
+#define USE_LLVM_ABI 1
+#else
+#define USE_LLVM_ABI 0
+#endif
gb_global lbAddr lb_global_type_info_data = {};
gb_global lbAddr lb_global_type_info_member_types = {};
@@ -135,9 +142,9 @@ lbValue lb_addr_get_ptr(lbProcedure *p, lbAddr const &addr) {
case lbAddr_Map: {
Type *map_type = base_type(addr.map.type);
lbValue h = lb_gen_map_header(p, addr.addr, map_type);
- lbValue key = lb_gen_map_key(p, addr.map.key, map_type->Map.key);
+ lbValue key = lb_gen_map_hash(p, addr.map.key, map_type->Map.key);
- auto args = array_make<lbValue>(heap_allocator(), 2);
+ auto args = array_make<lbValue>(permanent_allocator(), 2);
args[0] = h;
args[1] = key;
@@ -202,7 +209,7 @@ void lb_emit_bounds_check(lbProcedure *p, Token token, lbValue index, lbValue le
lbValue line = lb_const_int(p->module, t_int, token.pos.line);
lbValue column = lb_const_int(p->module, t_int, token.pos.column);
- auto args = array_make<lbValue>(heap_allocator(), 5);
+ auto args = array_make<lbValue>(permanent_allocator(), 5);
args[0] = file;
args[1] = line;
args[2] = column;
@@ -226,7 +233,7 @@ void lb_emit_slice_bounds_check(lbProcedure *p, Token token, lbValue low, lbValu
high = lb_emit_conv(p, high, t_int);
if (!lower_value_used) {
- auto args = array_make<lbValue>(heap_allocator(), 5);
+ auto args = array_make<lbValue>(permanent_allocator(), 5);
args[0] = file;
args[1] = line;
args[2] = column;
@@ -238,7 +245,7 @@ void lb_emit_slice_bounds_check(lbProcedure *p, Token token, lbValue low, lbValu
// No need to convert unless used
low = lb_emit_conv(p, low, t_int);
- auto args = array_make<lbValue>(heap_allocator(), 6);
+ auto args = array_make<lbValue>(permanent_allocator(), 6);
args[0] = file;
args[1] = line;
args[2] = column;
@@ -350,7 +357,7 @@ void lb_addr_store(lbProcedure *p, lbAddr addr, lbValue value) {
}
- auto args = array_make<lbValue>(heap_allocator(), gb_max(arg_count, param_count));
+ auto args = array_make<lbValue>(permanent_allocator(), gb_max(arg_count, param_count));
args[0] = ptr;
args[1] = index;
args[2] = value;
@@ -459,7 +466,11 @@ void lb_addr_store(lbProcedure *p, lbAddr addr, lbValue value) {
GB_ASSERT(value.value != nullptr);
value = lb_emit_conv(p, value, lb_addr_type(addr));
- LLVMBuildStore(p->builder, value.value, addr.addr.value);
+ if (USE_LLVM_ABI) {
+ lb_emit_store(p, addr.addr, value);
+ } else {
+ LLVMBuildStore(p->builder, value.value, addr.addr.value);
+ }
}
void lb_const_store(lbValue ptr, lbValue value) {
@@ -480,11 +491,25 @@ void lb_emit_store(lbProcedure *p, lbValue ptr, lbValue value) {
Type *ca = core_type(a);
if (ca->kind == Type_Basic) {
GB_ASSERT_MSG(are_types_identical(ca, core_type(value.type)), "%s != %s", type_to_string(a), type_to_string(value.type));
- } else {
- GB_ASSERT_MSG(are_types_identical(a, value.type), "%s != %s", type_to_string(a), type_to_string(value.type));
}
- LLVMBuildStore(p->builder, value.value, ptr.value);
+ if (is_type_proc(a)) {
+ // NOTE(bill, 2020-11-11): Because of certain LLVM rules, a procedure value may be
+ // stored as regular pointer with no procedure information
+
+ LLVMTypeRef src_t = LLVMGetElementType(LLVMTypeOf(ptr.value));
+ LLVMValueRef v = LLVMBuildPointerCast(p->builder, value.value, src_t, "");
+ LLVMBuildStore(p->builder, v, ptr.value);
+ } else {
+ Type *ca = core_type(a);
+ if (ca->kind == Type_Basic || ca->kind == Type_Proc) {
+ GB_ASSERT_MSG(are_types_identical(ca, core_type(value.type)), "%s != %s", type_to_string(a), type_to_string(value.type));
+ } else {
+ GB_ASSERT_MSG(are_types_identical(a, value.type), "%s != %s", type_to_string(a), type_to_string(value.type));
+ }
+
+ LLVMBuildStore(p->builder, value.value, ptr.value);
+ }
}
lbValue lb_emit_load(lbProcedure *p, lbValue value) {
@@ -567,9 +592,9 @@ lbValue lb_addr_load(lbProcedure *p, lbAddr const &addr) {
Type *map_type = base_type(addr.map.type);
lbAddr v = lb_add_local_generated(p, map_type->Map.lookup_result_type, true);
lbValue h = lb_gen_map_header(p, addr.addr, map_type);
- lbValue key = lb_gen_map_key(p, addr.map.key, map_type->Map.key);
+ lbValue key = lb_gen_map_hash(p, addr.map.key, map_type->Map.key);
- auto args = array_make<lbValue>(heap_allocator(), 2);
+ auto args = array_make<lbValue>(permanent_allocator(), 2);
args[0] = h;
args[1] = key;
@@ -633,12 +658,14 @@ lbValue lb_addr_load(lbProcedure *p, lbAddr const &addr) {
res.value = LLVMBuildZExtOrBitCast(p->builder, field, lb_type(p->module, int_type), "");
return res;
} else if (addr.kind == lbAddr_Context) {
+ lbValue a = addr.addr;
+ a.value = LLVMBuildPointerCast(p->builder, a.value, lb_type(p->module, t_context_ptr), "");
+
if (addr.ctx.sel.index.count > 0) {
- lbValue a = addr.addr;
lbValue b = lb_emit_deep_field_gep(p, a, addr.ctx.sel);
return lb_emit_load(p, b);
} else {
- return lb_emit_load(p, addr.addr);
+ return lb_emit_load(p, a);
}
} else if (addr.kind == lbAddr_SoaVariable) {
Type *t = type_deref(addr.addr.type);
@@ -748,7 +775,6 @@ void lb_emit_store_union_variant_tag(lbProcedure *p, lbValue parent, Type *varia
}
void lb_emit_store_union_variant(lbProcedure *p, lbValue parent, lbValue variant, Type *variant_type) {
- gbAllocator a = heap_allocator();
lbValue underlying = lb_emit_conv(p, parent, alloc_type_pointer(variant_type));
lb_emit_store(p, underlying, variant);
@@ -758,10 +784,9 @@ void lb_emit_store_union_variant(lbProcedure *p, lbValue parent, lbValue variant
void lb_clone_struct_type(LLVMTypeRef dst, LLVMTypeRef src) {
unsigned field_count = LLVMCountStructElementTypes(src);
- LLVMTypeRef *fields = gb_alloc_array(heap_allocator(), LLVMTypeRef, field_count);
+ LLVMTypeRef *fields = gb_alloc_array(temporary_allocator(), LLVMTypeRef, field_count);
LLVMGetStructElementTypes(src, fields);
LLVMStructSetBody(dst, fields, field_count, LLVMIsPackedStruct(src));
- gb_free(heap_allocator(), fields);
}
LLVMTypeRef lb_alignment_prefix_type_hack(lbModule *m, i64 alignment) {
@@ -796,8 +821,6 @@ bool lb_is_elem_const(Ast *elem, Type *elem_type) {
}
String lb_mangle_name(lbModule *m, Entity *e) {
- gbAllocator a = heap_allocator();
-
String name = e->token.string;
AstPackage *pkg = e->pkg;
@@ -823,7 +846,7 @@ String lb_mangle_name(lbModule *m, Entity *e) {
max_len += 21;
}
- char *new_name = gb_alloc_array(a, char, max_len);
+ char *new_name = gb_alloc_array(permanent_allocator(), char, max_len);
isize new_name_len = gb_snprintf(
new_name, max_len,
"%.*s.%.*s", LIT(pkgn), LIT(name)
@@ -874,7 +897,7 @@ String lb_set_nested_type_name_ir_mangled_name(Entity *e, lbProcedure *p) {
if (p != nullptr) {
isize name_len = p->name.len + 1 + ts_name.len + 1 + 10 + 1;
- char *name_text = gb_alloc_array(heap_allocator(), char, name_len);
+ char *name_text = gb_alloc_array(permanent_allocator(), char, name_len);
u32 guid = ++p->module->nested_type_name_guid;
name_len = gb_snprintf(name_text, name_len, "%.*s.%.*s-%u", LIT(p->name), LIT(ts_name), guid);
@@ -884,7 +907,7 @@ String lb_set_nested_type_name_ir_mangled_name(Entity *e, lbProcedure *p) {
} else {
// NOTE(bill): a nested type be required before its parameter procedure exists. Just give it a temp name for now
isize name_len = 9 + 1 + ts_name.len + 1 + 10 + 1;
- char *name_text = gb_alloc_array(heap_allocator(), char, name_len);
+ char *name_text = gb_alloc_array(permanent_allocator(), char, name_len);
static u32 guid = 0;
guid += 1;
name_len = gb_snprintf(name_text, name_len, "_internal.%.*s-%u", LIT(ts_name), guid);
@@ -1093,7 +1116,7 @@ LLVMTypeRef lb_type_internal(lbModule *m, Type *type) {
return type;
}
- case Basic_typeid: return LLVMIntType(8*cast(unsigned)build_context.word_size);
+ case Basic_typeid: return LLVMIntTypeInContext(m->ctx, 8*cast(unsigned)build_context.word_size);
// Endian Specific Types
case Basic_i16le: return LLVMInt16TypeInContext(ctx);
@@ -1132,7 +1155,7 @@ LLVMTypeRef lb_type_internal(lbModule *m, Type *type) {
switch (base->kind) {
case Type_Basic:
- return lb_type(m, base);
+ return lb_type_internal(m, base);
case Type_Named:
case Type_Generic:
@@ -1141,7 +1164,7 @@ LLVMTypeRef lb_type_internal(lbModule *m, Type *type) {
break;
case Type_Opaque:
- return lb_type(m, base->Opaque.elem);
+ return lb_type_internal(m, base->Opaque.elem);
case Type_Pointer:
case Type_Array:
@@ -1152,21 +1175,21 @@ LLVMTypeRef lb_type_internal(lbModule *m, Type *type) {
case Type_Enum:
case Type_BitSet:
case Type_SimdVector:
- return lb_type(m, base);
+ return lb_type_internal(m, base);
// TODO(bill): Deal with this correctly. Can this be named?
case Type_Proc:
- return lb_type(m, base);
+ return lb_type_internal(m, base);
case Type_Tuple:
- return lb_type(m, base);
+ return lb_type_internal(m, base);
}
LLVMTypeRef *found = map_get(&m->types, hash_type(base));
if (found) {
LLVMTypeKind kind = LLVMGetTypeKind(*found);
if (kind == LLVMStructTypeKind) {
- char const *name = alloc_cstring(heap_allocator(), lb_get_entity_name(m, type->Named.type_name));
+ char const *name = alloc_cstring(permanent_allocator(), lb_get_entity_name(m, type->Named.type_name));
LLVMTypeRef llvm_type = LLVMGetTypeByName(m->mod, name);
if (llvm_type != nullptr) {
return llvm_type;
@@ -1183,7 +1206,7 @@ LLVMTypeRef lb_type_internal(lbModule *m, Type *type) {
case Type_Union:
case Type_BitField:
{
- char const *name = alloc_cstring(heap_allocator(), lb_get_entity_name(m, type->Named.type_name));
+ char const *name = alloc_cstring(permanent_allocator(), lb_get_entity_name(m, type->Named.type_name));
LLVMTypeRef llvm_type = LLVMGetTypeByName(m->mod, name);
if (llvm_type != nullptr) {
return llvm_type;
@@ -1196,7 +1219,7 @@ LLVMTypeRef lb_type_internal(lbModule *m, Type *type) {
}
- return lb_type(m, base);
+ return lb_type_internal(m, base);
}
case Type_Pointer:
@@ -1240,7 +1263,7 @@ LLVMTypeRef lb_type_internal(lbModule *m, Type *type) {
{
if (type->Struct.is_raw_union) {
unsigned field_count = 2;
- LLVMTypeRef *fields = gb_alloc_array(heap_allocator(), LLVMTypeRef, field_count);
+ LLVMTypeRef *fields = gb_alloc_array(permanent_allocator(), LLVMTypeRef, field_count);
i64 alignment = type_align_of(type);
unsigned size_of_union = cast(unsigned)type_size_of(type);
fields[0] = lb_alignment_prefix_type_hack(m, alignment);
@@ -1253,16 +1276,18 @@ LLVMTypeRef lb_type_internal(lbModule *m, Type *type) {
offset = 1;
}
+ m->internal_type_level += 1;
+ defer (m->internal_type_level -= 1);
+
unsigned field_count = cast(unsigned)(type->Struct.fields.count + offset);
- LLVMTypeRef *fields = gb_alloc_array(heap_allocator(), LLVMTypeRef, field_count);
- GB_ASSERT(fields != nullptr);
- defer (gb_free(heap_allocator(), fields));
+ LLVMTypeRef *fields = gb_alloc_array(temporary_allocator(), LLVMTypeRef, field_count);
for_array(i, type->Struct.fields) {
Entity *field = type->Struct.fields[i];
fields[i+offset] = lb_type(m, field->type);
}
+
if (type->Struct.custom_align > 0) {
fields[0] = lb_alignment_prefix_type_hack(m, type->Struct.custom_align);
}
@@ -1315,12 +1340,15 @@ LLVMTypeRef lb_type_internal(lbModule *m, Type *type) {
return lb_type(m, type->Tuple.variables[0]->type);
} else {
unsigned field_count = cast(unsigned)(type->Tuple.variables.count);
- LLVMTypeRef *fields = gb_alloc_array(heap_allocator(), LLVMTypeRef, field_count);
- defer (gb_free(heap_allocator(), fields));
+ LLVMTypeRef *fields = gb_alloc_array(temporary_allocator(), LLVMTypeRef, field_count);
for_array(i, type->Tuple.variables) {
Entity *field = type->Tuple.variables[i];
- fields[i] = lb_type(m, field->type);
+
+ LLVMTypeRef param_type = nullptr;
+ param_type = lb_type(m, field->type);
+
+ fields[i] = param_type;
}
return LLVMStructTypeInContext(ctx, fields, field_count, type->Tuple.is_packed);
@@ -1328,64 +1356,150 @@ LLVMTypeRef lb_type_internal(lbModule *m, Type *type) {
case Type_Proc:
{
- set_procedure_abi_types(heap_allocator(), type);
+ if (USE_LLVM_ABI) {
+ if (m->internal_type_level > 256) { // TODO HACK(bill): is this really enough?
+ return LLVMPointerType(LLVMIntTypeInContext(m->ctx, 8), 0);
+ } else {
+ unsigned param_count = 0;
+ if (type->Proc.calling_convention == ProcCC_Odin) {
+ param_count += 1;
+ }
- LLVMTypeRef return_type = LLVMVoidTypeInContext(ctx);
- if (type->Proc.return_by_pointer) {
- // Void
- } else if (type->Proc.abi_compat_result_type != nullptr) {
- return_type = lb_type(m, type->Proc.abi_compat_result_type);
- }
+ if (type->Proc.param_count != 0) {
+ GB_ASSERT(type->Proc.params->kind == Type_Tuple);
+ for_array(i, type->Proc.params->Tuple.variables) {
+ Entity *e = type->Proc.params->Tuple.variables[i];
+ if (e->kind != Entity_Variable) {
+ continue;
+ }
+ param_count += 1;
+ }
+ }
+ m->internal_type_level += 1;
+ defer (m->internal_type_level -= 1);
+
+ LLVMTypeRef ret = nullptr;
+ LLVMTypeRef *params = gb_alloc_array(heap_allocator(), LLVMTypeRef, param_count);
+ if (type->Proc.result_count != 0) {
+ Type *single_ret = reduce_tuple_to_single_type(type->Proc.results);
+ ret = lb_type(m, single_ret);
+ if (ret != nullptr) {
+ if (is_type_boolean(single_ret) &&
+ is_calling_convention_none(type->Proc.calling_convention) &&
+ type_size_of(single_ret) <= 1) {
+ ret = LLVMInt1TypeInContext(m->ctx);
+ }
+ }
+ }
- isize extra_param_count = 0;
- if (type->Proc.return_by_pointer) {
- extra_param_count += 1;
- }
- if (type->Proc.calling_convention == ProcCC_Odin) {
- extra_param_count += 1;
- }
+ isize param_index = 0;
+ if (type->Proc.param_count != 0) {
+ GB_ASSERT(type->Proc.params->kind == Type_Tuple);
+ for_array(i, type->Proc.params->Tuple.variables) {
+ Entity *e = type->Proc.params->Tuple.variables[i];
+ if (e->kind != Entity_Variable) {
+ continue;
+ }
- isize param_count = type->Proc.abi_compat_params.count + extra_param_count;
- auto param_types = array_make<LLVMTypeRef>(heap_allocator(), 0, param_count);
- defer (array_free(&param_types));
+ Type *e_type = reduce_tuple_to_single_type(e->type);
+
+ LLVMTypeRef param_type = nullptr;
+ if (is_type_boolean(e_type) &&
+ type_size_of(e_type) <= 1) {
+ param_type = LLVMInt1TypeInContext(m->ctx);
+ } else {
+ if (is_type_proc(e_type)) {
+ param_type = lb_type(m, t_rawptr);
+ } else {
+ param_type = lb_type(m, e_type);
+ }
+ }
- if (type->Proc.return_by_pointer) {
- array_add(&param_types, LLVMPointerType(lb_type(m, type->Proc.abi_compat_result_type), 0));
- }
+ params[param_index++] = param_type;
+ }
+ }
+ if (param_index < param_count) {
+ params[param_index++] = lb_type(m, t_rawptr);
+ // params[param_index++] = lb_type(m, t_context_ptr);
+ }
+ GB_ASSERT(param_index == param_count);
- for_array(i, type->Proc.abi_compat_params) {
- Type *param = type->Proc.abi_compat_params[i];
- if (param == nullptr) {
- continue;
- }
- if (type->Proc.params->Tuple.variables[i]->flags & EntityFlag_CVarArg) {
- GB_ASSERT(i+1 == type->Proc.abi_compat_params.count);
- break;
+
+ lbFunctionType *ft = lb_get_abi_info(m->ctx, params, param_count, ret, ret != nullptr, type->Proc.calling_convention);
+ map_set(&m->function_type_map, hash_type(type), ft);
+ LLVMTypeRef new_abi_fn_ptr_type = lb_function_type_to_llvm_ptr(ft, type->Proc.c_vararg);
+ LLVMTypeRef new_abi_fn_type = LLVMGetElementType(new_abi_fn_ptr_type);
+
+ // LLVMTypeRef new_ret = LLVMGetReturnType(new_abi_fn_type);
+ // LLVMTypeRef old_ret = LLVMGetReturnType(old_abi_fn_type);
+ // unsigned new_count = LLVMCountParamTypes(new_abi_fn_type);
+ // unsigned old_count = LLVMCountParamTypes(old_abi_fn_type);
+ // GB_ASSERT_MSG(new_count == old_count, "%u %u, %s %s", new_count, old_count, LLVMPrintTypeToString(new_abi_fn_type), LLVMPrintTypeToString(old_abi_fn_type));
+ return new_abi_fn_ptr_type;
}
- if (is_type_tuple(param)) {
- param = base_type(param);
- for_array(j, param->Tuple.variables) {
- Entity *v = param->Tuple.variables[j];
- if (v->kind != Entity_Variable) {
- // Sanity check
+ } else {
+ LLVMTypeRef old_abi_fn_type = nullptr;
+
+ set_procedure_abi_types(type);
+ LLVMTypeRef return_type = LLVMVoidTypeInContext(ctx);
+ if (type->Proc.return_by_pointer) {
+ // Void
+ } else if (type->Proc.abi_compat_result_type != nullptr) {
+ return_type = lb_type(m, type->Proc.abi_compat_result_type);
+ }
+
+ isize extra_param_count = 0;
+ if (type->Proc.return_by_pointer) {
+ extra_param_count += 1;
+ }
+ if (type->Proc.calling_convention == ProcCC_Odin) {
+ extra_param_count += 1;
+ }
+
+ isize param_count = type->Proc.abi_compat_params.count + extra_param_count;
+ auto param_types = array_make<LLVMTypeRef>(temporary_allocator(), 0, param_count);
+
+ if (type->Proc.return_by_pointer) {
+ array_add(&param_types, LLVMPointerType(lb_type(m, type->Proc.abi_compat_result_type), 0));
+ }
+
+ for_array(i, type->Proc.abi_compat_params) {
+ Type *param = type->Proc.abi_compat_params[i];
+ if (param == nullptr) {
continue;
}
- array_add(&param_types, lb_type(m, v->type));
+ if (type->Proc.params->Tuple.variables[i]->flags & EntityFlag_CVarArg) {
+ GB_ASSERT(i+1 == type->Proc.abi_compat_params.count);
+ break;
+ }
+ if (is_type_tuple(param)) {
+ param = base_type(param);
+ for_array(j, param->Tuple.variables) {
+ Entity *v = param->Tuple.variables[j];
+ if (v->kind != Entity_Variable) {
+ // Sanity check
+ continue;
+ }
+ LLVMTypeRef t = lb_type(m, v->type);
+ array_add(&param_types, t);
+ }
+ } else {
+ array_add(&param_types, lb_type(m, param));
+ }
+ }
+ if (type->Proc.calling_convention == ProcCC_Odin) {
+ array_add(&param_types, lb_type(m, t_rawptr));
+ // array_add(&param_types, lb_type(m, t_context_ptr));
}
- } else {
- array_add(&param_types, lb_type(m, param));
- }
- }
- if (type->Proc.calling_convention == ProcCC_Odin) {
- array_add(&param_types, lb_type(m, t_context_ptr));
- }
- LLVMTypeRef t = LLVMFunctionType(return_type, param_types.data, cast(unsigned)param_types.count, type->Proc.c_vararg);
- return LLVMPointerType(t, 0);
+ old_abi_fn_type = LLVMFunctionType(return_type, param_types.data, cast(unsigned)param_types.count, type->Proc.c_vararg);
+ return LLVMPointerType(old_abi_fn_type, 0);
+ }
}
+
break;
case Type_BitFieldValue:
- return LLVMIntType(type->BitFieldValue.bits);
+ return LLVMIntTypeInContext(m->ctx, type->BitFieldValue.bits);
case Type_BitField:
{
@@ -1393,12 +1507,11 @@ LLVMTypeRef lb_type_internal(lbModule *m, Type *type) {
{
GB_ASSERT(type->BitField.fields.count == type->BitField.sizes.count);
unsigned field_count = cast(unsigned)type->BitField.fields.count;
- LLVMTypeRef *fields = gb_alloc_array(heap_allocator(), LLVMTypeRef, field_count);
- defer (gb_free(heap_allocator(), fields));
+ LLVMTypeRef *fields = gb_alloc_array(temporary_allocator(), LLVMTypeRef, field_count);
for_array(i, type->BitField.sizes) {
u32 size = type->BitField.sizes[i];
- fields[i] = LLVMIntType(size);
+ fields[i] = LLVMIntTypeInContext(m->ctx, size);
}
internal_type = LLVMStructTypeInContext(ctx, fields, field_count, true);
@@ -1417,7 +1530,11 @@ LLVMTypeRef lb_type_internal(lbModule *m, Type *type) {
}
break;
case Type_BitSet:
- return LLVMIntType(8*cast(unsigned)type_size_of(type));
+ {
+ Type *ut = bit_set_to_int(type);
+ return lb_type(m, ut);
+ }
+
case Type_SimdVector:
if (type->SimdVector.is_x86_mmx) {
return LLVMX86MMXTypeInContext(ctx);
@@ -1451,10 +1568,17 @@ LLVMTypeRef lb_type(lbModule *m, Type *type) {
return *found;
}
- LLVMTypeRef llvm_type = lb_type_internal(m, type);
-
- map_set(&m->types, hash_type(type), llvm_type);
+ LLVMTypeRef llvm_type = nullptr;
+ m->internal_type_level += 1;
+ llvm_type = lb_type_internal(m, type);
+ m->internal_type_level -= 1;
+ if (USE_LLVM_ABI && m->internal_type_level == 0) {
+ map_set(&m->types, hash_type(type), llvm_type);
+ if (is_type_named(type)) {
+ map_set(&m->llvm_types, hash_pointer(llvm_type), type);
+ }
+ }
return llvm_type;
}
@@ -1855,7 +1979,7 @@ LLVMMetadataRef lb_debug_type_internal(lbModule *m, Type *type) {
case Type_Proc:
{
return nullptr;
- // set_procedure_abi_types(heap_allocator(), type);
+ // set_procedure_abi_types(type);
// LLVMTypeRef return_type = LLVMVoidTypeInContext(ctx);
// isize offset = 0;
@@ -1895,7 +2019,7 @@ LLVMMetadataRef lb_debug_type_internal(lbModule *m, Type *type) {
break;
case Type_BitFieldValue:
return nullptr;
- // return LLVMIntType(type->BitFieldValue.bits);
+ // return LLVMIntTypeInContext(m->ctx, type->BitFieldValue.bits);
case Type_BitField:
{
@@ -1909,7 +2033,7 @@ LLVMMetadataRef lb_debug_type_internal(lbModule *m, Type *type) {
// for_array(i, type->BitField.sizes) {
// u32 size = type->BitField.sizes[i];
- // fields[i] = LLVMIntType(size);
+ // fields[i] = LLVMIntTypeInContext(m->ctx, size);
// }
// internal_type = LLVMStructTypeInContext(ctx, fields, field_count, true);
@@ -1929,7 +2053,7 @@ LLVMMetadataRef lb_debug_type_internal(lbModule *m, Type *type) {
break;
case Type_BitSet:
return nullptr;
- // return LLVMIntType(8*cast(unsigned)type_size_of(type));
+ // return LLVMIntTypeInContext(m->ctx, 8*cast(unsigned)type_size_of(type));
case Type_SimdVector:
return nullptr;
// if (type->SimdVector.is_x86_mmx) {
@@ -1998,7 +2122,7 @@ lbValue lb_emit_string(lbProcedure *p, lbValue str_elem, lbValue str_len) {
LLVMAttributeRef lb_create_enum_attribute(LLVMContextRef ctx, char const *name, u64 value) {
unsigned kind = LLVMGetEnumAttributeKindForName(name, gb_strlen(name));
- GB_ASSERT(kind != 0);
+ GB_ASSERT_MSG(kind != 0, "unknown attribute: %s", name);
return LLVMCreateEnumAttribute(ctx, kind, value);
}
@@ -2032,7 +2156,7 @@ lbProcedure *lb_create_procedure(lbModule *m, Entity *entity) {
}
- lbProcedure *p = gb_alloc_item(heap_allocator(), lbProcedure);
+ lbProcedure *p = gb_alloc_item(permanent_allocator(), lbProcedure);
p->module = m;
entity->code_gen_module = m;
@@ -2046,7 +2170,7 @@ lbProcedure *lb_create_procedure(lbModule *m, Entity *entity) {
Type *pt = base_type(entity->type);
GB_ASSERT(pt->kind == Type_Proc);
- set_procedure_abi_types(heap_allocator(), entity->type);
+ set_procedure_abi_types(entity->type);
p->type = entity->type;
p->type_expr = decl->type_expr;
@@ -2069,18 +2193,32 @@ lbProcedure *lb_create_procedure(lbModule *m, Entity *entity) {
lb_add_foreign_library_path(p->module, entity->Procedure.foreign_library);
}
- char *c_link_name = alloc_cstring(heap_allocator(), p->name);
+ char *c_link_name = alloc_cstring(permanent_allocator(), p->name);
LLVMTypeRef func_ptr_type = lb_type(m, p->type);
LLVMTypeRef func_type = LLVMGetElementType(func_ptr_type);
p->value = LLVMAddFunction(m->mod, c_link_name, func_type);
- lbCallingConventionKind cc_kind = lbCallingConvention_C;
- // TODO(bill): Clean up this logic
- if (build_context.metrics.os != TargetOs_js) {
- cc_kind = lb_calling_convention_map[pt->Proc.calling_convention];
+ lbFunctionType **ft_found = map_get(&m->function_type_map, hash_type(p->type));
+ if (USE_LLVM_ABI && ft_found) {
+ lbFunctionType *abi_ft = *ft_found;
+ p->abi_function_type = abi_ft;
+ lb_add_function_type_attributes(p->value, abi_ft, abi_ft->calling_convention);
+ } else {
+ lbCallingConventionKind cc_kind = lbCallingConvention_C;
+ // TODO(bill): Clean up this logic
+ if (build_context.metrics.os != TargetOs_js) {
+ cc_kind = lb_calling_convention_map[pt->Proc.calling_convention];
+ }
+ LLVMSetFunctionCallConv(p->value, cc_kind);
}
- LLVMSetFunctionCallConv(p->value, cc_kind);
+
+ // lbCallingConventionKind cc_kind = lbCallingConvention_C;
+ // // TODO(bill): Clean up this logic
+ // if (build_context.metrics.os != TargetOs_js) {
+ // cc_kind = lb_calling_convention_map[pt->Proc.calling_convention];
+ // }
+ // LLVMSetFunctionCallConv(p->value, cc_kind);
lbValue proc_value = {p->value, p->type};
lb_add_entity(m, entity, proc_value);
lb_add_member(m, p->name, proc_value);
@@ -2092,19 +2230,19 @@ lbProcedure *lb_create_procedure(lbModule *m, Entity *entity) {
LLVMSetVisibility(p->value, LLVMDefaultVisibility);
if (build_context.metrics.os == TargetOs_js) {
- char const *export_name = alloc_cstring(heap_allocator(), p->name);
+ char const *export_name = alloc_cstring(permanent_allocator(), p->name);
LLVMAddTargetDependentFunctionAttr(p->value, "wasm-export-name", export_name);
}
}
if (p->is_foreign) {
if (build_context.metrics.os == TargetOs_js) {
- char const *import_name = alloc_cstring(heap_allocator(), p->name);
+ char const *import_name = alloc_cstring(permanent_allocator(), p->name);
char const *module_name = "env";
if (entity->Procedure.foreign_library != nullptr) {
Entity *foreign_library = entity->Procedure.foreign_library;
GB_ASSERT(foreign_library->kind == Entity_LibraryName);
if (foreign_library->LibraryName.paths.count > 0) {
- module_name = alloc_cstring(heap_allocator(), foreign_library->LibraryName.paths[0]);
+ module_name = alloc_cstring(permanent_allocator(), foreign_library->LibraryName.paths[0]);
}
}
LLVMAddTargetDependentFunctionAttr(p->value, "wasm-import-name", import_name);
@@ -2115,8 +2253,10 @@ lbProcedure *lb_create_procedure(lbModule *m, Entity *entity) {
// NOTE(bill): offset==0 is the return value
isize offset = 1;
if (pt->Proc.return_by_pointer) {
- lb_add_proc_attribute_at_index(p, 1, "sret");
- lb_add_proc_attribute_at_index(p, 1, "noalias");
+ if (!USE_LLVM_ABI) {
+ lb_add_proc_attribute_at_index(p, 1, "sret");
+ lb_add_proc_attribute_at_index(p, 1, "noalias");
+ }
offset = 2;
}
@@ -2149,7 +2289,7 @@ lbProcedure *lb_create_procedure(lbModule *m, Entity *entity) {
}
}
- if (pt->Proc.calling_convention == ProcCC_Odin) {
+ if (!USE_LLVM_ABI && pt->Proc.calling_convention == ProcCC_Odin) {
lb_add_proc_attribute_at_index(p, offset+parameter_index, "noalias");
lb_add_proc_attribute_at_index(p, offset+parameter_index, "nonnull");
lb_add_proc_attribute_at_index(p, offset+parameter_index, "nocapture");
@@ -2191,7 +2331,7 @@ lbProcedure *lb_create_dummy_procedure(lbModule *m, String link_name, Type *type
GB_ASSERT(found == nullptr);
}
- lbProcedure *p = gb_alloc_item(heap_allocator(), lbProcedure);
+ lbProcedure *p = gb_alloc_item(permanent_allocator(), lbProcedure);
p->module = m;
p->name = link_name;
@@ -2205,7 +2345,7 @@ lbProcedure *lb_create_dummy_procedure(lbModule *m, String link_name, Type *type
p->is_export = false;
p->is_entry_point = false;
- gbAllocator a = heap_allocator();
+ gbAllocator a = permanent_allocator();
p->children.allocator = a;
p->params.allocator = a;
p->defer_stmts.allocator = a;
@@ -2214,7 +2354,7 @@ lbProcedure *lb_create_dummy_procedure(lbModule *m, String link_name, Type *type
p->context_stack.allocator = a;
- char *c_link_name = alloc_cstring(heap_allocator(), p->name);
+ char *c_link_name = alloc_cstring(permanent_allocator(), p->name);
LLVMTypeRef func_ptr_type = lb_type(m, p->type);
LLVMTypeRef func_type = LLVMGetElementType(func_ptr_type);
@@ -2241,7 +2381,7 @@ lbProcedure *lb_create_dummy_procedure(lbModule *m, String link_name, Type *type
}
isize parameter_index = 0;
- if (pt->Proc.param_count) {
+ if (!USE_LLVM_ABI && pt->Proc.param_count) {
TypeTuple *params = &pt->Proc.params->Tuple;
for (isize i = 0; i < pt->Proc.param_count; i++) {
Entity *e = params->variables[i];
@@ -2396,6 +2536,69 @@ void lb_start_block(lbProcedure *p, lbBlock *b) {
p->curr_block = b;
}
+LLVMValueRef OdinLLVMBuildTransmute(lbProcedure *p, LLVMValueRef val, LLVMTypeRef dst_type) {
+ LLVMContextRef ctx = p->module->ctx;
+
+ LLVMTypeRef src_type = LLVMTypeOf(val);
+
+ if (src_type == dst_type) {
+ return val;
+ }
+
+ i64 src_size = lb_sizeof(src_type);
+ i64 dst_size = lb_sizeof(dst_type);
+
+ if (dst_type == LLVMInt1TypeInContext(ctx)) {
+ GB_ASSERT(lb_is_type_kind(src_type, LLVMIntegerTypeKind));
+ return LLVMBuildICmp(p->builder, LLVMIntNE, val, LLVMConstNull(src_type), "");
+ } else if (src_type == LLVMInt1TypeInContext(ctx)) {
+ GB_ASSERT(lb_is_type_kind(src_type, LLVMIntegerTypeKind));
+ return LLVMBuildZExtOrBitCast(p->builder, val, dst_type, "");
+ }
+
+ if (src_size != dst_size && (lb_is_type_kind(src_type, LLVMVectorTypeKind) ^ lb_is_type_kind(dst_type, LLVMVectorTypeKind))) {
+ // Okay
+ } else {
+ GB_ASSERT_MSG(src_size == dst_size, "%s == %s", LLVMPrintTypeToString(src_type), LLVMPrintTypeToString(dst_type));
+ }
+
+ LLVMTypeKind src_kind = LLVMGetTypeKind(src_type);
+ LLVMTypeKind dst_kind = LLVMGetTypeKind(dst_type);
+ if (src_kind == dst_kind) {
+ if (src_kind == LLVMPointerTypeKind) {
+ return LLVMBuildPointerCast(p->builder, val, dst_type, "");
+ } else if (src_kind != LLVMStructTypeKind) {
+ return LLVMBuildBitCast(p->builder, val, dst_type, "");
+ }
+ } else {
+ if (src_kind == LLVMPointerTypeKind && dst_kind == LLVMIntegerTypeKind) {
+ return LLVMBuildPtrToInt(p->builder, val, dst_type, "");
+ } else if (src_kind == LLVMIntegerTypeKind && dst_kind == LLVMPointerTypeKind) {
+ return LLVMBuildIntToPtr(p->builder, val, dst_type, "");
+ }
+ }
+
+ if (LLVMIsALoadInst(val)) {
+ LLVMValueRef val_ptr = LLVMGetOperand(val, 0);
+ val_ptr = LLVMBuildPointerCast(p->builder, val_ptr, LLVMPointerType(dst_type, 0), "");
+ return LLVMBuildLoad(p->builder, val_ptr, "");
+ } else {
+ GB_ASSERT(p->decl_block != p->curr_block);
+ LLVMPositionBuilderAtEnd(p->builder, p->decl_block->block);
+
+ LLVMValueRef ptr = LLVMBuildAlloca(p->builder, dst_type, "");
+ LLVMPositionBuilderAtEnd(p->builder, p->curr_block->block);
+ i64 max_align = gb_max(lb_alignof(src_type), lb_alignof(dst_type));
+ max_align = gb_max(max_align, 4);
+ LLVMSetAlignment(ptr, cast(unsigned)max_align);
+
+ LLVMValueRef nptr = LLVMBuildPointerCast(p->builder, ptr, LLVMPointerType(src_type, 0), "");
+ LLVMBuildStore(p->builder, val, nptr);
+
+ return LLVMBuildLoad(p->builder, ptr, "");
+ }
+}
+
void lb_begin_procedure_body(lbProcedure *p) {
DeclInfo *decl = decl_info_of_entity(p->entity);
@@ -2428,82 +2631,186 @@ void lb_begin_procedure_body(lbProcedure *p) {
GB_ASSERT(p->type != nullptr);
- i32 parameter_index = 0;
+ if (p->abi_function_type) {
+ lbFunctionType *ft = p->abi_function_type;
+ unsigned param_offset = 0;
- lbValue return_ptr_value = {};
- if (p->type->Proc.return_by_pointer) {
- // NOTE(bill): this must be parameter 0
- Type *ptr_type = alloc_type_pointer(reduce_tuple_to_single_type(p->type->Proc.results));
- Entity *e = alloc_entity_param(nullptr, make_token_ident(str_lit("agg.result")), ptr_type, false, false);
- e->flags |= EntityFlag_Sret | EntityFlag_NoAlias;
+ lbValue return_ptr_value = {};
+ if (ft->ret.kind == lbArg_Indirect) {
+ // NOTE(bill): this must be parameter 0
+ Type *ptr_type = alloc_type_pointer(reduce_tuple_to_single_type(p->type->Proc.results));
+ Entity *e = alloc_entity_param(nullptr, make_token_ident(str_lit("agg.result")), ptr_type, false, false);
+ e->flags |= EntityFlag_Sret | EntityFlag_NoAlias;
- return_ptr_value.value = LLVMGetParam(p->value, 0);
- return_ptr_value.type = ptr_type;
- p->return_ptr = lb_addr(return_ptr_value);
+ return_ptr_value.value = LLVMGetParam(p->value, 0);
+ return_ptr_value.type = ptr_type;
+ p->return_ptr = lb_addr(return_ptr_value);
- lb_add_entity(p->module, e, return_ptr_value);
+ lb_add_entity(p->module, e, return_ptr_value);
- parameter_index += 1;
- }
+ param_offset += 1;
+ }
- if (p->type->Proc.params != nullptr) {
- TypeTuple *params = &p->type->Proc.params->Tuple;
- auto abi_types = p->type->Proc.abi_compat_params;
+ if (p->type->Proc.params != nullptr) {
+ TypeTuple *params = &p->type->Proc.params->Tuple;
- for_array(i, params->variables) {
- Entity *e = params->variables[i];
- if (e->kind != Entity_Variable) {
- continue;
- }
- Type *abi_type = e->type;
- if (abi_types.count > 0) {
- abi_type = abi_types[i];
- }
- if (e->token.string != "") {
- lb_add_param(p, e, nullptr, abi_type, parameter_index);
- }
- if (is_type_tuple(abi_type)) {
- parameter_index += cast(i32)abi_type->Tuple.variables.count;
- } else {
- parameter_index += 1;
+ unsigned param_index = 0;
+ for_array(i, params->variables) {
+ Entity *e = params->variables[i];
+ if (e->kind != Entity_Variable) {
+ continue;
+ }
+
+ lbArgType *arg_type = &ft->args[param_index];
+ if (arg_type->kind == lbArg_Ignore) {
+ continue;
+ } else if (arg_type->kind == lbArg_Direct) {
+ lbParamPasskind kind = lbParamPass_Value;
+ LLVMTypeRef param_type = lb_type(p->module, e->type);
+ if (param_type != arg_type->type) {
+ kind = lbParamPass_BitCast;
+ }
+ LLVMValueRef value = LLVMGetParam(p->value, param_offset+param_index);
+
+ value = OdinLLVMBuildTransmute(p, value, param_type);
+
+ lbValue param = {};
+ param.value = value;
+ param.type = e->type;
+ array_add(&p->params, param);
+
+ if (e->token.string.len != 0) {
+ lbAddr l = lb_add_local(p, e->type, e, false, param_index);
+ lb_addr_store(p, l, param);
+ }
+
+ param_index += 1;
+ } else if (arg_type->kind == lbArg_Indirect) {
+ LLVMValueRef value_ptr = LLVMGetParam(p->value, param_offset+param_index);
+ LLVMValueRef value = LLVMBuildLoad(p->builder, value_ptr, "");
+
+ lbValue param = {};
+ param.value = value;
+ param.type = e->type;
+ array_add(&p->params, param);
+
+ lbValue ptr = {};
+ ptr.value = value_ptr;
+ ptr.type = alloc_type_pointer(e->type);
+
+ lb_add_entity(p->module, e, ptr);
+ param_index += 1;
+ }
}
}
- }
+ if (p->type->Proc.has_named_results) {
+ GB_ASSERT(p->type->Proc.result_count > 0);
+ TypeTuple *results = &p->type->Proc.results->Tuple;
- if (p->type->Proc.has_named_results) {
- GB_ASSERT(p->type->Proc.result_count > 0);
- TypeTuple *results = &p->type->Proc.results->Tuple;
+ for_array(i, results->variables) {
+ Entity *e = results->variables[i];
+ GB_ASSERT(e->kind == Entity_Variable);
- for_array(i, results->variables) {
- Entity *e = results->variables[i];
- GB_ASSERT(e->kind == Entity_Variable);
+ if (e->token.string != "") {
+ GB_ASSERT(!is_blank_ident(e->token));
- if (e->token.string != "") {
- GB_ASSERT(!is_blank_ident(e->token));
+ lbAddr res = {};
+ if (return_ptr_value.value) {
+ lbValue ptr = return_ptr_value;
+ if (results->variables.count != 1) {
+ ptr = lb_emit_struct_ep(p, ptr, cast(i32)i);
+ }
- lbAddr res = {};
- if (p->type->Proc.return_by_pointer) {
- lbValue ptr = return_ptr_value;
- if (results->variables.count != 1) {
- ptr = lb_emit_struct_ep(p, ptr, cast(i32)i);
+ res = lb_addr(ptr);
+ lb_add_entity(p->module, e, ptr);
+ } else {
+ res = lb_add_local(p, e->type, e);
}
- res = lb_addr(ptr);
- lb_add_entity(p->module, e, ptr);
+ if (e->Variable.param_value.kind != ParameterValue_Invalid) {
+ lbValue c = lb_handle_param_value(p, e->type, e->Variable.param_value, e->token.pos);
+ lb_addr_store(p, res, c);
+ }
+ }
+ }
+ }
+ } else {
+ i32 parameter_index = 0;
+ lbValue return_ptr_value = {};
+ if (p->type->Proc.return_by_pointer) {
+ // NOTE(bill): this must be parameter 0
+ Type *ptr_type = alloc_type_pointer(reduce_tuple_to_single_type(p->type->Proc.results));
+ Entity *e = alloc_entity_param(nullptr, make_token_ident(str_lit("agg.result")), ptr_type, false, false);
+ e->flags |= EntityFlag_Sret | EntityFlag_NoAlias;
+
+ return_ptr_value.value = LLVMGetParam(p->value, 0);
+ return_ptr_value.type = ptr_type;
+ p->return_ptr = lb_addr(return_ptr_value);
+
+ lb_add_entity(p->module, e, return_ptr_value);
+
+ parameter_index += 1;
+ }
+
+ if (p->type->Proc.params != nullptr) {
+ TypeTuple *params = &p->type->Proc.params->Tuple;
+ auto abi_types = p->type->Proc.abi_compat_params;
+
+ for_array(i, params->variables) {
+ Entity *e = params->variables[i];
+ if (e->kind != Entity_Variable) {
+ continue;
+ }
+ Type *abi_type = e->type;
+ if (abi_types.count > 0) {
+ abi_type = abi_types[i];
+ }
+ if (e->token.string != "") {
+ lb_add_param(p, e, nullptr, abi_type, parameter_index);
+ }
+ if (is_type_tuple(abi_type)) {
+ parameter_index += cast(i32)abi_type->Tuple.variables.count;
} else {
- res = lb_add_local(p, e->type, e);
+ parameter_index += 1;
}
+ }
+ }
+
- if (e->Variable.param_value.kind != ParameterValue_Invalid) {
- lbValue c = lb_handle_param_value(p, e->type, e->Variable.param_value, e->token.pos);
- lb_addr_store(p, res, c);
+ if (p->type->Proc.has_named_results) {
+ GB_ASSERT(p->type->Proc.result_count > 0);
+ TypeTuple *results = &p->type->Proc.results->Tuple;
+
+ for_array(i, results->variables) {
+ Entity *e = results->variables[i];
+ GB_ASSERT(e->kind == Entity_Variable);
+
+ if (e->token.string != "") {
+ GB_ASSERT(!is_blank_ident(e->token));
+
+ lbAddr res = {};
+ if (p->type->Proc.return_by_pointer) {
+ lbValue ptr = return_ptr_value;
+ if (results->variables.count != 1) {
+ ptr = lb_emit_struct_ep(p, ptr, cast(i32)i);
+ }
+
+ res = lb_addr(ptr);
+ lb_add_entity(p->module, e, ptr);
+ } else {
+ res = lb_add_local(p, e->type, e);
+ }
+
+ if (e->Variable.param_value.kind != ParameterValue_Invalid) {
+ lbValue c = lb_handle_param_value(p, e->type, e->Variable.param_value, e->token.pos);
+ lb_addr_store(p, res, c);
+ }
}
}
}
}
-
if (p->type->Proc.calling_convention == ProcCC_Odin) {
Entity *e = alloc_entity_param(nullptr, make_token_ident(str_lit("__.context_ptr")), t_context_ptr, false, false);
e->flags |= EntityFlag_NoAlias;
@@ -2559,7 +2866,7 @@ void lb_add_edge(lbBlock *from, lbBlock *to) {
lbBlock *lb_create_block(lbProcedure *p, char const *name, bool append) {
- lbBlock *b = gb_alloc_item(heap_allocator(), lbBlock);
+ lbBlock *b = gb_alloc_item(permanent_allocator(), lbBlock);
b->block = LLVMCreateBasicBlockInContext(p->module->ctx, name);
b->appended = false;
if (append) {
@@ -2658,16 +2965,34 @@ lbAddr lb_add_local(lbProcedure *p, Type *type, Entity *e, bool zero_init, i32 p
char const *name = "";
if (e != nullptr) {
- // name = alloc_cstring(heap_allocator(), e->token.string);
+ // name = alloc_cstring(permanent_allocator(), e->token.string);
}
LLVMTypeRef llvm_type = lb_type(p->module, type);
LLVMValueRef ptr = LLVMBuildAlloca(p->builder, llvm_type, name);
- LLVMSetAlignment(ptr, 16); // TODO(bill): Make this configurable
+
+ // unsigned alignment = 16; // TODO(bill): Make this configurable
+ unsigned alignment = cast(unsigned)lb_alignof(llvm_type);
+ LLVMSetAlignment(ptr, alignment);
LLVMPositionBuilderAtEnd(p->builder, p->curr_block->block);
if (zero_init) {
- LLVMBuildStore(p->builder, LLVMConstNull(lb_type(p->module, type)), ptr);
+ LLVMTypeKind kind = LLVMGetTypeKind(llvm_type);
+
+ switch (kind) {
+ case LLVMStructTypeKind:
+ case LLVMArrayTypeKind:
+ {
+ // NOTE(bill): Enforce zeroing through memset to make sure padding is zeroed too
+ LLVMTypeRef type_i8 = LLVMInt8TypeInContext(p->module->ctx);
+ LLVMTypeRef type_i32 = LLVMInt32TypeInContext(p->module->ctx);
+ i32 sz = cast(i32)type_size_of(type);
+ LLVMBuildMemSet(p->builder, ptr, LLVMConstNull(type_i8), LLVMConstInt(type_i32, sz, false), alignment);
+ }
+ break;
+ default:
+ LLVMBuildStore(p->builder, LLVMConstNull(lb_type(p->module, type)), ptr);
+ }
}
lbValue val = {};
@@ -2706,13 +3031,13 @@ void lb_build_nested_proc(lbProcedure *p, AstProcLit *pd, Entity *e) {
isize name_len = p->name.len + 1 + pd_name.len + 1 + 10 + 1;
- char *name_text = gb_alloc_array(heap_allocator(), char, name_len);
+ char *name_text = gb_alloc_array(permanent_allocator(), char, name_len);
i32 guid = cast(i32)p->children.count;
name_len = gb_snprintf(name_text, name_len, "%.*s.%.*s-%d", LIT(p->name), LIT(pd_name), guid);
String name = make_string(cast(u8 *)name_text, name_len-1);
- set_procedure_abi_types(heap_allocator(), e->type);
+ set_procedure_abi_types(e->type);
e->Procedure.link_name = name;
@@ -2849,7 +3174,7 @@ void lb_build_constant_value_decl(lbProcedure *p, AstValueDecl *vd) {
return;
}
- set_procedure_abi_types(heap_allocator(), e->type);
+ set_procedure_abi_types(e->type);
e->Procedure.link_name = name;
lbProcedure *nested_proc = lb_create_procedure(p->module, e);
@@ -2869,7 +3194,7 @@ void lb_build_constant_value_decl(lbProcedure *p, AstValueDecl *vd) {
}
-void lb_build_stmt_list(lbProcedure *p, Array<Ast *> const &stmts) {
+void lb_build_stmt_list(lbProcedure *p, Slice<Ast *> const &stmts) {
for_array(i, stmts) {
Ast *stmt = stmts[i];
switch (stmt->kind) {
@@ -2905,7 +3230,7 @@ lbBranchBlocks lb_lookup_branch_blocks(lbProcedure *p, Ast *ident) {
lbTargetList *lb_push_target_list(lbProcedure *p, Ast *label, lbBlock *break_, lbBlock *continue_, lbBlock *fallthrough_) {
- lbTargetList *tl = gb_alloc_item(heap_allocator(), lbTargetList);
+ lbTargetList *tl = gb_alloc_item(permanent_allocator(), lbTargetList);
tl->prev = p->target_list;
tl->break_ = break_;
tl->continue_ = continue_;
@@ -3063,13 +3388,8 @@ void lb_build_range_indexed(lbProcedure *p, lbValue expr, Type *val_type, lbValu
elem = lb_emit_load(p, elem);
lbValue entry = lb_emit_ptr_offset(p, elem, idx);
- val = lb_emit_load(p, lb_emit_struct_ep(p, entry, 2));
-
- lbValue key_raw = lb_emit_struct_ep(p, entry, 0);
- key_raw = lb_emit_struct_ep(p, key_raw, 1);
- lbValue key = lb_emit_conv(p, key_raw, alloc_type_pointer(expr_type->Map.key));
-
- idx = lb_emit_load(p, key);
+ idx = lb_emit_load(p, lb_emit_struct_ep(p, entry, 2));
+ val = lb_emit_load(p, lb_emit_struct_ep(p, entry, 3));
break;
}
@@ -3126,7 +3446,7 @@ void lb_build_range_string(lbProcedure *p, lbValue expr, Type *val_type,
lbValue str_elem = lb_emit_ptr_offset(p, lb_string_elem(p, expr), offset);
lbValue str_len = lb_emit_arith(p, Token_Sub, count, offset, t_int);
- auto args = array_make<lbValue>(heap_allocator(), 1);
+ auto args = array_make<lbValue>(permanent_allocator(), 1);
args[0] = lb_emit_string(p, str_elem, str_len);
lbValue rune_and_len = lb_emit_runtime_call(p, "string_decode_rune", args);
lbValue len = lb_emit_struct_ev(p, rune_and_len, 1);
@@ -3217,7 +3537,7 @@ void lb_build_range_enum(lbProcedure *p, Type *enum_type, Type *val_type, lbValu
lbValue max_count = lb_const_int(m, t_int, enum_count);
lbValue ti = lb_type_info(m, t);
- lbValue variant = lb_emit_struct_ep(p, ti, 3);
+ lbValue variant = lb_emit_struct_ep(p, ti, 4);
lbValue eti_ptr = lb_emit_conv(p, variant, t_type_info_enum_ptr);
lbValue values = lb_emit_load(p, lb_emit_struct_ep(p, eti_ptr, 2));
lbValue values_data = lb_slice_elem(p, values);
@@ -3600,7 +3920,7 @@ void lb_build_switch_stmt(lbProcedure *p, AstSwitchStmt *ss) {
ast_node(body, BlockStmt, ss->body);
- Array<Ast *> default_stmts = {};
+ Slice<Ast *> default_stmts = {};
lbBlock *default_fall = nullptr;
lbBlock *default_block = nullptr;
@@ -4001,14 +4321,14 @@ void lb_build_stmt(lbProcedure *p, Ast *node) {
String mangled_name = {};
{
- gbString str = gb_string_make_length(heap_allocator(), p->name.text, p->name.len);
+ gbString str = gb_string_make_length(permanent_allocator(), p->name.text, p->name.len);
str = gb_string_appendc(str, "-");
str = gb_string_append_fmt(str, ".%.*s-%llu", LIT(name), cast(long long)e->id);
mangled_name.text = cast(u8 *)str;
mangled_name.len = gb_string_length(str);
}
- char *c_name = alloc_cstring(heap_allocator(), mangled_name);
+ char *c_name = alloc_cstring(permanent_allocator(), mangled_name);
LLVMValueRef global = LLVMAddGlobal(p->module->mod, lb_type(p->module, e->type), c_name);
LLVMSetInitializer(global, LLVMConstNull(lb_type(p->module, e->type)));
@@ -4055,8 +4375,8 @@ void lb_build_stmt(lbProcedure *p, Ast *node) {
}
}
} else { // Tuple(s)
- auto lvals = array_make<lbAddr>(heap_allocator(), 0, vd->names.count);
- auto inits = array_make<lbValue>(heap_allocator(), 0, vd->names.count);
+ auto lvals = array_make<lbAddr>(permanent_allocator(), 0, vd->names.count);
+ auto inits = array_make<lbValue>(permanent_allocator(), 0, vd->names.count);
for_array(i, vd->names) {
Ast *name = vd->names[i];
@@ -4093,7 +4413,7 @@ void lb_build_stmt(lbProcedure *p, Ast *node) {
case_ast_node(as, AssignStmt, node);
if (as->op.kind == Token_Eq) {
- auto lvals = array_make<lbAddr>(heap_allocator(), 0, as->lhs.count);
+ auto lvals = array_make<lbAddr>(permanent_allocator(), 0, as->lhs.count);
for_array(i, as->lhs) {
Ast *lhs = as->lhs[i];
@@ -4111,7 +4431,7 @@ void lb_build_stmt(lbProcedure *p, Ast *node) {
lbValue init = lb_build_expr(p, rhs);
lb_addr_store(p, lvals[0], init);
} else {
- auto inits = array_make<lbValue>(heap_allocator(), 0, lvals.count);
+ auto inits = array_make<lbValue>(permanent_allocator(), 0, lvals.count);
for_array(i, as->rhs) {
lbValue init = lb_build_expr(p, as->rhs[i]);
@@ -4125,7 +4445,7 @@ void lb_build_stmt(lbProcedure *p, Ast *node) {
}
}
} else {
- auto inits = array_make<lbValue>(heap_allocator(), 0, lvals.count);
+ auto inits = array_make<lbValue>(permanent_allocator(), 0, lvals.count);
for_array(i, as->rhs) {
lbValue init = lb_build_expr(p, as->rhs[i]);
@@ -4217,7 +4537,7 @@ void lb_build_stmt(lbProcedure *p, Ast *node) {
}
} else {
- auto results = array_make<lbValue>(heap_allocator(), 0, return_count);
+ auto results = array_make<lbValue>(permanent_allocator(), 0, return_count);
if (res_count != 0) {
for (isize res_index = 0; res_index < res_count; res_index++) {
@@ -4276,27 +4596,53 @@ void lb_build_stmt(lbProcedure *p, Ast *node) {
}
- if (p->type->Proc.return_by_pointer) {
- if (res.value != nullptr) {
- lb_addr_store(p, p->return_ptr, res);
+ if (p->abi_function_type) {
+ if (p->abi_function_type->ret.kind == lbArg_Indirect) {
+ if (res.value != nullptr) {
+ LLVMBuildStore(p->builder, res.value, p->return_ptr.addr.value);
+ } else {
+ LLVMBuildStore(p->builder, LLVMConstNull(p->abi_function_type->ret.type), p->return_ptr.addr.value);
+ }
+
+ lb_emit_defer_stmts(p, lbDeferExit_Return, nullptr);
+
+ LLVMBuildRetVoid(p->builder);
} else {
- lb_addr_store(p, p->return_ptr, lb_const_nil(p->module, p->type->Proc.abi_compat_result_type));
+ LLVMValueRef ret_val = res.value;
+ ret_val = OdinLLVMBuildTransmute(p, ret_val, p->abi_function_type->ret.type);
+ if (p->abi_function_type->ret.cast_type != nullptr) {
+ ret_val = OdinLLVMBuildTransmute(p, ret_val, p->abi_function_type->ret.cast_type);
+ }
+
+ lb_emit_defer_stmts(p, lbDeferExit_Return, nullptr);
+ LLVMBuildRet(p->builder, ret_val);
}
+ } else {
+ GB_ASSERT(!USE_LLVM_ABI);
- lb_emit_defer_stmts(p, lbDeferExit_Return, nullptr);
+ if (p->type->Proc.return_by_pointer) {
+ if (res.value != nullptr) {
+ lb_addr_store(p, p->return_ptr, res);
+ } else {
+ lb_addr_store(p, p->return_ptr, lb_const_nil(p->module, p->type->Proc.abi_compat_result_type));
+ }
- LLVMBuildRetVoid(p->builder);
- } else {
- GB_ASSERT_MSG(res.value != nullptr, "%.*s", LIT(p->name));
- Type *abi_rt = p->type->Proc.abi_compat_result_type;
- if (!are_types_identical(res.type, abi_rt)) {
- res = lb_emit_transmute(p, res, abi_rt);
- }
+ lb_emit_defer_stmts(p, lbDeferExit_Return, nullptr);
- lb_emit_defer_stmts(p, lbDeferExit_Return, nullptr);
+ LLVMBuildRetVoid(p->builder);
+ } else {
+ GB_ASSERT_MSG(res.value != nullptr, "%.*s", LIT(p->name));
- LLVMBuildRet(p->builder, res.value);
+ Type *abi_rt = p->type->Proc.abi_compat_result_type;
+ if (!are_types_identical(res.type, abi_rt)) {
+ res = lb_emit_transmute(p, res, abi_rt);
+ }
+ lb_emit_defer_stmts(p, lbDeferExit_Return, nullptr);
+ LLVMBuildRet(p->builder, res.value);
+ }
}
+
+
case_end;
case_ast_node(is, IfStmt, node);
@@ -4496,9 +4842,8 @@ lbValue lb_emit_min(lbProcedure *p, Type *t, lbValue x, lbValue y) {
y = lb_emit_conv(p, y, t);
if (is_type_float(t)) {
- gbAllocator a = heap_allocator();
i64 sz = 8*type_size_of(t);
- auto args = array_make<lbValue>(heap_allocator(), 2);
+ auto args = array_make<lbValue>(permanent_allocator(), 2);
args[0] = x;
args[1] = y;
switch (sz) {
@@ -4514,9 +4859,8 @@ lbValue lb_emit_max(lbProcedure *p, Type *t, lbValue x, lbValue y) {
y = lb_emit_conv(p, y, t);
if (is_type_float(t)) {
- gbAllocator a = heap_allocator();
i64 sz = 8*type_size_of(t);
- auto args = array_make<lbValue>(heap_allocator(), 2);
+ auto args = array_make<lbValue>(permanent_allocator(), 2);
args[0] = x;
args[1] = y;
switch (sz) {
@@ -4552,7 +4896,7 @@ LLVMValueRef lb_find_or_add_entity_string_ptr(lbModule *m, String const &str) {
isize max_len = 7+8+1;
- char *name = gb_alloc_array(heap_allocator(), char, max_len);
+ char *name = gb_alloc_array(permanent_allocator(), char, max_len);
isize len = gb_snprintf(name, max_len, "csbs$%x", m->global_array_index);
len -= 1;
m->global_array_index++;
@@ -4594,7 +4938,7 @@ lbValue lb_find_or_add_entity_string_byte_slice(lbModule *m, String const &str)
char *name = nullptr;
{
isize max_len = 7+8+1;
- name = gb_alloc_array(heap_allocator(), char, max_len);
+ name = gb_alloc_array(permanent_allocator(), char, max_len);
isize len = gb_snprintf(name, max_len, "csbs$%x", m->global_array_index);
len -= 1;
m->global_array_index++;
@@ -4634,7 +4978,7 @@ isize lb_type_info_index(CheckerInfo *info, Type *type, bool err_on_not_found=tr
return -1;
}
-lbValue lb_typeid(lbModule *m, Type *type, Type *typeid_type) {
+lbValue lb_typeid(lbModule *m, Type *type) {
type = default_type(type);
u64 id = cast(u64)lb_type_info_index(m->info, type);
@@ -4686,6 +5030,7 @@ lbValue lb_typeid(lbModule *m, Type *type, Type *typeid_type) {
u64 data = 0;
if (build_context.word_size == 4) {
+ GB_ASSERT(id <= (1u<<24u));
data |= (id &~ (1u<<24)) << 0u; // index
data |= (kind &~ (1u<<5)) << 24u; // kind
data |= (named &~ (1u<<1)) << 29u; // kind
@@ -4693,6 +5038,7 @@ lbValue lb_typeid(lbModule *m, Type *type, Type *typeid_type) {
data |= (reserved &~ (1u<<1)) << 31u; // kind
} else {
GB_ASSERT(build_context.word_size == 8);
+ GB_ASSERT(id <= (1ull<<56u));
data |= (id &~ (1ull<<56)) << 0ul; // index
data |= (kind &~ (1ull<<5)) << 56ull; // kind
data |= (named &~ (1ull<<1)) << 61ull; // kind
@@ -4700,10 +5046,9 @@ lbValue lb_typeid(lbModule *m, Type *type, Type *typeid_type) {
data |= (reserved &~ (1ull<<1)) << 63ull; // kind
}
-
lbValue res = {};
- res.value = LLVMConstInt(lb_type(m, typeid_type), data, false);
- res.type = typeid_type;
+ res.value = LLVMConstInt(lb_type(m, t_typeid), data, false);
+ res.type = t_typeid;
return res;
}
@@ -4738,7 +5083,7 @@ lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_loc
value = convert_exact_value_for_type(value, type);
if (value.kind == ExactValue_Typeid) {
- return lb_typeid(m, value.value_typeid, original_type);
+ return lb_typeid(m, value.value_typeid);
}
if (value.kind == ExactValue_Invalid) {
@@ -4804,7 +5149,7 @@ lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_loc
}
} else {
isize max_len = 7+8+1;
- char *str = gb_alloc_array(heap_allocator(), char, max_len);
+ char *str = gb_alloc_array(permanent_allocator(), char, max_len);
isize len = gb_snprintf(str, max_len, "csba$%x", m->global_array_index);
m->global_array_index++;
@@ -4835,10 +5180,44 @@ lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_loc
}
} else if (is_type_array(type) && value.kind == ExactValue_String && !is_type_u8(core_array_type(type))) {
+ if (is_type_rune_array(type) && value.kind == ExactValue_String) {
+ i64 count = type->Array.count;
+ Type *elem = type->Array.elem;
+ LLVMTypeRef et = lb_type(m, elem);
+
+ Rune rune;
+ isize offset = 0;
+ isize width = 1;
+ String s = value.value_string;
+
+ LLVMValueRef *elems = gb_alloc_array(permanent_allocator(), LLVMValueRef, count);
+
+ for (i64 i = 0; i < count && offset < s.len; i++) {
+ width = gb_utf8_decode(s.text+offset, s.len-offset, &rune);
+ offset += width;
+
+ elems[i] = LLVMConstInt(et, rune, true);
+
+ }
+ GB_ASSERT(offset == s.len);
+
+ res.value = LLVMConstArray(et, elems, cast(unsigned)count);
+ return res;
+ }
+ GB_PANIC("HERE!\n");
+
LLVMValueRef data = LLVMConstStringInContext(ctx,
cast(char const *)value.value_string.text,
cast(unsigned)value.value_string.len,
- false);
+ false /*DontNullTerminate*/);
+ res.value = data;
+ return res;
+ } else if (is_type_u8_array(type) && value.kind == ExactValue_String) {
+ GB_ASSERT(type->Array.count == value.value_string.len);
+ LLVMValueRef data = LLVMConstStringInContext(ctx,
+ cast(char const *)value.value_string.text,
+ cast(unsigned)value.value_string.len,
+ true /*DontNullTerminate*/);
res.value = data;
return res;
} else if (is_type_array(type) &&
@@ -4852,7 +5231,7 @@ lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_loc
lbValue single_elem = lb_const_value(m, elem, value, allow_local);
- LLVMValueRef *elems = gb_alloc_array(heap_allocator(), LLVMValueRef, count);
+ LLVMValueRef *elems = gb_alloc_array(permanent_allocator(), LLVMValueRef, count);
for (i64 i = 0; i < count; i++) {
elems[i] = single_elem.value;
}
@@ -4905,7 +5284,7 @@ lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_loc
isize byte_len = gb_size_of(u64)*len;
u8 *old_bytes = cast(u8 *)words;
// TODO(bill): Use a different allocator here for a temporary allocation
- u8 *new_bytes = cast(u8 *)gb_alloc_align(heap_allocator(), byte_len, gb_align_of(u64));
+ u8 *new_bytes = cast(u8 *)gb_alloc_align(permanent_allocator(), byte_len, gb_align_of(u64));
for (i64 i = 0; i < sz; i++) {
new_bytes[i] = old_bytes[sz-1-i];
}
@@ -4937,12 +5316,12 @@ lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_loc
LLVMValueRef values[2] = {};
switch (8*type_size_of(type)) {
case 64:
- values[0] = lb_const_f32(m, cast(f32)value.value_complex.real);
- values[1] = lb_const_f32(m, cast(f32)value.value_complex.imag);
+ values[0] = lb_const_f32(m, cast(f32)value.value_complex->real);
+ values[1] = lb_const_f32(m, cast(f32)value.value_complex->imag);
break;
case 128:
- values[0] = LLVMConstReal(lb_type(m, t_f64), value.value_complex.real);
- values[1] = LLVMConstReal(lb_type(m, t_f64), value.value_complex.imag);
+ values[0] = LLVMConstReal(lb_type(m, t_f64), value.value_complex->real);
+ values[1] = LLVMConstReal(lb_type(m, t_f64), value.value_complex->imag);
break;
}
@@ -4956,17 +5335,17 @@ lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_loc
switch (8*type_size_of(type)) {
case 128:
// @QuaternionLayout
- values[3] = lb_const_f32(m, cast(f32)value.value_quaternion.real);
- values[0] = lb_const_f32(m, cast(f32)value.value_quaternion.imag);
- values[1] = lb_const_f32(m, cast(f32)value.value_quaternion.jmag);
- values[2] = lb_const_f32(m, cast(f32)value.value_quaternion.kmag);
+ values[3] = lb_const_f32(m, cast(f32)value.value_quaternion->real);
+ values[0] = lb_const_f32(m, cast(f32)value.value_quaternion->imag);
+ values[1] = lb_const_f32(m, cast(f32)value.value_quaternion->jmag);
+ values[2] = lb_const_f32(m, cast(f32)value.value_quaternion->kmag);
break;
case 256:
// @QuaternionLayout
- values[3] = LLVMConstReal(lb_type(m, t_f64), value.value_quaternion.real);
- values[0] = LLVMConstReal(lb_type(m, t_f64), value.value_quaternion.imag);
- values[1] = LLVMConstReal(lb_type(m, t_f64), value.value_quaternion.jmag);
- values[2] = LLVMConstReal(lb_type(m, t_f64), value.value_quaternion.kmag);
+ values[3] = LLVMConstReal(lb_type(m, t_f64), value.value_quaternion->real);
+ values[0] = LLVMConstReal(lb_type(m, t_f64), value.value_quaternion->imag);
+ values[1] = LLVMConstReal(lb_type(m, t_f64), value.value_quaternion->jmag);
+ values[2] = LLVMConstReal(lb_type(m, t_f64), value.value_quaternion->kmag);
break;
}
@@ -4991,9 +5370,7 @@ lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_loc
}
if (cl->elems[0]->kind == Ast_FieldValue) {
// TODO(bill): This is O(N*M) and will be quite slow; it should probably be sorted before hand
-
- LLVMValueRef *values = gb_alloc_array(heap_allocator(), LLVMValueRef, type->Array.count);
- defer (gb_free(heap_allocator(), values));
+ LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, type->Array.count);
isize value_index = 0;
for (i64 i = 0; i < type->Array.count; i++) {
@@ -5050,8 +5427,7 @@ lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_loc
} else {
GB_ASSERT_MSG(elem_count == type->Array.count, "%td != %td", elem_count, type->Array.count);
- LLVMValueRef *values = gb_alloc_array(heap_allocator(), LLVMValueRef, type->Array.count);
- defer (gb_free(heap_allocator(), values));
+ LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, type->Array.count);
for (isize i = 0; i < elem_count; i++) {
TypeAndValue tav = cl->elems[i]->tav;
@@ -5074,9 +5450,7 @@ lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_loc
}
if (cl->elems[0]->kind == Ast_FieldValue) {
// TODO(bill): This is O(N*M) and will be quite slow; it should probably be sorted before hand
-
- LLVMValueRef *values = gb_alloc_array(heap_allocator(), LLVMValueRef, type->EnumeratedArray.count);
- defer (gb_free(heap_allocator(), values));
+ LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, type->EnumeratedArray.count);
isize value_index = 0;
@@ -5137,8 +5511,7 @@ lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_loc
} else {
GB_ASSERT_MSG(elem_count == type->EnumeratedArray.count, "%td != %td", elem_count, type->EnumeratedArray.count);
- LLVMValueRef *values = gb_alloc_array(heap_allocator(), LLVMValueRef, type->EnumeratedArray.count);
- defer (gb_free(heap_allocator(), values));
+ LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, type->EnumeratedArray.count);
for (isize i = 0; i < elem_count; i++) {
TypeAndValue tav = cl->elems[i]->tav;
@@ -5163,8 +5536,7 @@ lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_loc
GB_ASSERT(elem_type_can_be_constant(elem_type));
isize total_elem_count = type->SimdVector.count;
- LLVMValueRef *values = gb_alloc_array(heap_allocator(), LLVMValueRef, total_elem_count);
- defer (gb_free(heap_allocator(), values));
+ LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, total_elem_count);
for (isize i = 0; i < elem_count; i++) {
TypeAndValue tav = cl->elems[i]->tav;
@@ -5190,12 +5562,8 @@ lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_loc
}
isize value_count = type->Struct.fields.count + offset;
- LLVMValueRef *values = gb_alloc_array(heap_allocator(), LLVMValueRef, value_count);
- bool *visited = gb_alloc_array(heap_allocator(), bool, value_count);
- defer (gb_free(heap_allocator(), values));
- defer (gb_free(heap_allocator(), visited));
-
-
+ LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, value_count);
+ bool *visited = gb_alloc_array(temporary_allocator(), bool, value_count);
if (cl->elems.count > 0) {
if (cl->elems[0]->kind == Ast_FieldValue) {
@@ -5293,34 +5661,23 @@ lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_loc
}
break;
case ExactValue_Typeid:
- return lb_typeid(m, value.value_typeid, original_type);
+ return lb_typeid(m, value.value_typeid);
}
return lb_const_nil(m, original_type);
}
-u64 lb_generate_source_code_location_hash(TokenPos const &pos) {
- u64 h = 0xcbf29ce484222325;
- for (isize i = 0; i < pos.file.len; i++) {
- h = (h ^ u64(pos.file[i])) * 0x100000001b3;
- }
- h = h ^ (u64(pos.line) * 0x100000001b3);
- h = h ^ (u64(pos.column) * 0x100000001b3);
- return h;
-}
-
lbValue lb_emit_source_code_location(lbProcedure *p, String const &procedure, TokenPos const &pos) {
lbModule *m = p->module;
- LLVMValueRef fields[5] = {};
+ LLVMValueRef fields[4] = {};
fields[0]/*file*/ = lb_find_or_add_entity_string(p->module, pos.file).value;
fields[1]/*line*/ = lb_const_int(m, t_int, pos.line).value;
fields[2]/*column*/ = lb_const_int(m, t_int, pos.column).value;
fields[3]/*procedure*/ = lb_find_or_add_entity_string(p->module, procedure).value;
- fields[4]/*hash*/ = lb_const_int(m, t_u64, lb_generate_source_code_location_hash(pos)).value;
lbValue res = {};
- res.value = LLVMConstNamedStruct(lb_type(m, t_source_code_location), fields, 5);
+ res.value = LLVMConstNamedStruct(lb_type(m, t_source_code_location), fields, gb_count_of(fields));
res.type = t_source_code_location;
return res;
}
@@ -5523,7 +5880,7 @@ lbValue lb_emit_arith(lbProcedure *p, TokenKind op, lbValue lhs, lbValue rhs, Ty
Type *ft = base_complex_elem_type(type);
if (op == Token_Quo) {
- auto args = array_make<lbValue>(heap_allocator(), 2);
+ auto args = array_make<lbValue>(permanent_allocator(), 2);
args[0] = lhs;
args[1] = rhs;
@@ -5597,7 +5954,7 @@ lbValue lb_emit_arith(lbProcedure *p, TokenKind op, lbValue lhs, lbValue rhs, Ty
return lb_addr_load(p, res);
} else if (op == Token_Mul) {
- auto args = array_make<lbValue>(heap_allocator(), 2);
+ auto args = array_make<lbValue>(permanent_allocator(), 2);
args[0] = lhs;
args[1] = rhs;
@@ -5607,7 +5964,7 @@ lbValue lb_emit_arith(lbProcedure *p, TokenKind op, lbValue lhs, lbValue rhs, Ty
default: GB_PANIC("Unknown float type"); break;
}
} else if (op == Token_Quo) {
- auto args = array_make<lbValue>(heap_allocator(), 2);
+ auto args = array_make<lbValue>(permanent_allocator(), 2);
args[0] = lhs;
args[1] = rhs;
@@ -5803,10 +6160,10 @@ lbValue lb_build_binary_expr(lbProcedure *p, Ast *expr) {
lbValue right = {};
if (be->left->tav.mode == Addressing_Type) {
- left = lb_typeid(p->module, be->left->tav.type, t_typeid);
+ left = lb_typeid(p->module, be->left->tav.type);
}
if (be->right->tav.mode == Addressing_Type) {
- right = lb_typeid(p->module, be->right->tav.type, t_typeid);
+ right = lb_typeid(p->module, be->right->tav.type);
}
if (left.value == nullptr) left = lb_build_expr(p, be->left);
if (right.value == nullptr) right = lb_build_expr(p, be->right);
@@ -5831,9 +6188,9 @@ lbValue lb_build_binary_expr(lbProcedure *p, Ast *expr) {
{
lbValue addr = lb_address_from_load_or_generate_local(p, right);
lbValue h = lb_gen_map_header(p, addr, rt);
- lbValue key = lb_gen_map_key(p, left, rt->Map.key);
+ lbValue key = lb_gen_map_hash(p, left, rt->Map.key);
- auto args = array_make<lbValue>(heap_allocator(), 2);
+ auto args = array_make<lbValue>(permanent_allocator(), 2);
args[0] = h;
args[1] = key;
@@ -6079,7 +6436,7 @@ lbValue lb_emit_conv(lbProcedure *p, lbValue value, Type *t) {
if (are_types_identical(src, t_cstring) && are_types_identical(dst, t_string)) {
lbValue c = lb_emit_conv(p, value, t_cstring);
- auto args = array_make<lbValue>(heap_allocator(), 1);
+ auto args = array_make<lbValue>(permanent_allocator(), 1);
args[0] = c;
lbValue s = lb_emit_runtime_call(p, "cstring_to_string", args);
return lb_emit_conv(p, s, dst);
@@ -6096,7 +6453,6 @@ lbValue lb_emit_conv(lbProcedure *p, lbValue value, Type *t) {
// float -> float
if (is_type_float(src) && is_type_float(dst)) {
- gbAllocator a = heap_allocator();
i64 sz = type_size_of(src);
i64 dz = type_size_of(dst);
@@ -6322,8 +6678,6 @@ lbValue lb_emit_conv(lbProcedure *p, lbValue value, Type *t) {
return res;
}
-
-
// []byte/[]u8 <-> string
if (is_type_u8_slice(src) && is_type_string(dst)) {
return lb_emit_transmute(p, value, t);
@@ -6373,6 +6727,34 @@ lbValue lb_emit_conv(lbProcedure *p, lbValue value, Type *t) {
return lb_addr_load(p, result);
}
+
+ i64 src_sz = type_size_of(src);
+ i64 dst_sz = type_size_of(dst);
+
+ if (src_sz == dst_sz) {
+ // bit_set <-> integer
+ if (is_type_integer(src) && is_type_bit_set(dst)) {
+ lbValue res = lb_emit_conv(p, value, bit_set_to_int(dst));
+ res.type = dst;
+ return res;
+ }
+ if (is_type_bit_set(src) && is_type_integer(dst)) {
+ lbValue bs = value;
+ bs.type = bit_set_to_int(src);
+ return lb_emit_conv(p, bs, dst);
+ }
+
+ // typeid <-> integer
+ if (is_type_integer(src) && is_type_typeid(dst)) {
+ return lb_emit_transmute(p, value, dst);
+ }
+ if (is_type_typeid(src) && is_type_integer(dst)) {
+ return lb_emit_transmute(p, value, dst);
+ }
+ }
+
+
+
if (is_type_untyped(src)) {
if (is_type_string(src) && is_type_string(dst)) {
lbAddr result = lb_add_local_generated(p, t, false);
@@ -6453,6 +6835,14 @@ lbValue lb_emit_transmute(lbProcedure *p, lbValue value, Type *t) {
i64 sz = type_size_of(src);
i64 dz = type_size_of(dst);
+ if (sz != dz) {
+ LLVMTypeRef s = lb_type(m, src);
+ LLVMTypeRef d = lb_type(m, dst);
+ i64 llvm_sz = lb_sizeof(s);
+ i64 llvm_dz = lb_sizeof(d);
+ GB_ASSERT_MSG(llvm_sz == llvm_dz, "%s %s", LLVMPrintTypeToString(s), LLVMPrintTypeToString(d));
+ }
+
GB_ASSERT_MSG(sz == dz, "Invalid transmute conversion: '%s' to '%s'", type_to_string(src_type), type_to_string(t));
// NOTE(bill): Casting between an integer and a pointer cannot be done through a bitcast
@@ -6504,8 +6894,7 @@ void lb_emit_init_context(lbProcedure *p, lbAddr addr) {
GB_ASSERT(addr.ctx.sel.index.count == 0);
lbModule *m = p->module;
- gbAllocator a = heap_allocator();
- auto args = array_make<lbValue>(a, 1);
+ auto args = array_make<lbValue>(permanent_allocator(), 1);
args[0] = addr.addr;
lb_emit_runtime_call(p, "__init_context", args);
}
@@ -6569,12 +6958,11 @@ lbValue lb_copy_value_to_ptr(lbProcedure *p, lbValue val, Type *new_type, i64 al
lbAddr ptr = lb_add_local_generated(p, new_type, false);
LLVMSetAlignment(ptr.addr.value, cast(unsigned)alignment);
lb_addr_store(p, ptr, val);
- ptr.kind = lbAddr_Context;
+ // ptr.kind = lbAddr_Context;
return ptr.addr;
}
lbValue lb_emit_struct_ep(lbProcedure *p, lbValue s, i32 index) {
- gbAllocator a = heap_allocator();
GB_ASSERT(is_type_pointer(s.type));
Type *t = base_type(type_deref(s.type));
Type *result_type = nullptr;
@@ -6682,7 +7070,6 @@ lbValue lb_emit_struct_ev(lbProcedure *p, lbValue s, i32 index) {
return lb_emit_load(p, ptr);
}
- gbAllocator a = heap_allocator();
Type *t = base_type(s.type);
Type *result_type = nullptr;
@@ -6788,7 +7175,6 @@ lbValue lb_emit_struct_ev(lbProcedure *p, lbValue s, i32 index) {
lbValue lb_emit_deep_field_gep(lbProcedure *p, lbValue e, Selection sel) {
GB_ASSERT(sel.index.count > 0);
Type *type = type_deref(e.type);
- gbAllocator a = heap_allocator();
for_array(i, sel.index) {
i32 index = cast(i32)sel.index[i];
@@ -6959,14 +7345,14 @@ Array<lbValue> lb_value_to_array(lbProcedure *p, lbValue value) {
GB_ASSERT(t->kind == Type_Tuple);
auto *rt = &t->Tuple;
if (rt->variables.count > 0) {
- array = array_make<lbValue>(heap_allocator(), rt->variables.count);
+ array = array_make<lbValue>(permanent_allocator(), rt->variables.count);
for_array(i, rt->variables) {
lbValue elem = lb_emit_struct_ev(p, value, cast(i32)i);
array[i] = elem;
}
}
} else {
- array = array_make<lbValue>(heap_allocator(), 1);
+ array = array_make<lbValue>(permanent_allocator(), 1);
array[0] = value;
}
return array;
@@ -6983,7 +7369,7 @@ lbValue lb_emit_call_internal(lbProcedure *p, lbValue value, lbValue return_ptr,
arg_count += 1;
}
- LLVMValueRef *args = gb_alloc_array(heap_allocator(), LLVMValueRef, arg_count);
+ LLVMValueRef *args = gb_alloc_array(permanent_allocator(), LLVMValueRef, arg_count);
isize arg_index = 0;
if (return_ptr.value != nullptr) {
args[arg_index++] = return_ptr.value;
@@ -6993,17 +7379,37 @@ lbValue lb_emit_call_internal(lbProcedure *p, lbValue value, lbValue return_ptr,
args[arg_index++] = arg.value;
}
if (context_ptr.addr.value != nullptr) {
- args[arg_index++] = context_ptr.addr.value;
+ LLVMValueRef cp = context_ptr.addr.value;
+ cp = LLVMBuildPointerCast(p->builder, cp, lb_type(p->module, t_rawptr), "");
+ args[arg_index++] = cp;
}
-
LLVMBasicBlockRef curr_block = LLVMGetInsertBlock(p->builder);
GB_ASSERT(curr_block != p->decl_block->block);
- LLVMValueRef ret = LLVMBuildCall2(p->builder, LLVMGetElementType(lb_type(p->module, value.type)), value.value, args, arg_count, "");;
- lbValue res = {};
- res.value = ret;
- res.type = abi_rt;
- return res;
+ if (USE_LLVM_ABI) {
+
+ LLVMTypeRef ftp = lb_type(p->module, value.type);
+ LLVMTypeRef ft = LLVMGetElementType(ftp);
+ LLVMValueRef fn = value.value;
+ if (!lb_is_type_kind(LLVMTypeOf(value.value), LLVMFunctionTypeKind)) {
+ fn = LLVMBuildPointerCast(p->builder, fn, ftp, "");
+ }
+ LLVMTypeRef fnp = LLVMGetElementType(LLVMTypeOf(fn));
+ GB_ASSERT_MSG(lb_is_type_kind(fnp, LLVMFunctionTypeKind), "%s", LLVMPrintTypeToString(fnp));
+
+ LLVMValueRef ret = LLVMBuildCall2(p->builder, ft, fn, args, arg_count, "");;
+ lbValue res = {};
+ res.value = ret;
+ res.type = abi_rt;
+ return res;
+ } else {
+
+ LLVMValueRef ret = LLVMBuildCall2(p->builder, LLVMGetElementType(lb_type(p->module, value.type)), value.value, args, arg_count, "");;
+ lbValue res = {};
+ res.value = ret;
+ res.type = abi_rt;
+ return res;
+ }
}
lbValue lb_emit_runtime_call(lbProcedure *p, char const *c_name, Array<lbValue> const &args) {
@@ -7048,7 +7454,7 @@ lbValue lb_emit_call(lbProcedure *p, lbValue value, Array<lbValue> const &args,
LLVMBuildUnreachable(p->builder);
});
- set_procedure_abi_types(heap_allocator(), pt);
+ set_procedure_abi_types(pt);
bool is_c_vararg = pt->Proc.c_vararg;
isize param_count = pt->Proc.param_count;
@@ -7059,95 +7465,191 @@ lbValue lb_emit_call(lbProcedure *p, lbValue value, Array<lbValue> const &args,
GB_ASSERT_MSG(param_count == args.count, "%td == %td", param_count, args.count);
}
- auto processed_args = array_make<lbValue>(heap_allocator(), 0, args.count);
+ lbValue result = {};
- for (isize i = 0; i < param_count; i++) {
- Entity *e = pt->Proc.params->Tuple.variables[i];
- if (e->kind != Entity_Variable) {
- // array_add(&processed_args, args[i]);
- continue;
+ auto processed_args = array_make<lbValue>(permanent_allocator(), 0, args.count);
+
+ if (USE_LLVM_ABI) {
+ lbFunctionType **ft_found = nullptr;
+ ft_found = map_get(&m->function_type_map, hash_type(pt));
+ if (!ft_found) {
+ LLVMTypeRef llvm_proc_type = lb_type(p->module, pt);
+ ft_found = map_get(&m->function_type_map, hash_type(pt));
}
- GB_ASSERT(e->flags & EntityFlag_Param);
-
- Type *original_type = e->type;
- Type *new_type = pt->Proc.abi_compat_params[i];
- Type *arg_type = args[i].type;
-
- if (are_types_identical(arg_type, new_type)) {
- // NOTE(bill): Done
- array_add(&processed_args, args[i]);
- } else if (!are_types_identical(original_type, new_type)) {
- if (is_type_pointer(new_type) && !is_type_pointer(original_type)) {
- Type *av = core_type(type_deref(new_type));
- if (are_types_identical(av, core_type(original_type))) {
- if (e->flags&EntityFlag_ImplicitReference) {
- array_add(&processed_args, lb_address_from_load_or_generate_local(p, args[i]));
- } else if (!is_type_pointer(arg_type)) {
- array_add(&processed_args, lb_copy_value_to_ptr(p, args[i], original_type, 16));
- }
+ GB_ASSERT(ft_found != nullptr);
+
+ lbFunctionType *ft = *ft_found;
+ bool return_by_pointer = ft->ret.kind == lbArg_Indirect;
+
+ unsigned param_index = 0;
+ for (isize i = 0; i < param_count; i++) {
+ Entity *e = pt->Proc.params->Tuple.variables[i];
+ if (e->kind != Entity_Variable) {
+ continue;
+ }
+ GB_ASSERT(e->flags & EntityFlag_Param);
+
+ Type *original_type = e->type;
+ lbArgType *arg = &ft->args[param_index];
+ if (arg->kind == lbArg_Ignore) {
+ continue;
+ }
+
+ lbValue x = lb_emit_conv(p, args[i], original_type);
+ LLVMTypeRef xt = lb_type(p->module, x.type);
+
+ if (arg->kind == lbArg_Direct) {
+ LLVMTypeRef abi_type = arg->cast_type;
+ if (!abi_type) {
+ abi_type = arg->type;
+ }
+ if (xt == abi_type) {
+ array_add(&processed_args, x);
} else {
- array_add(&processed_args, lb_emit_transmute(p, args[i], new_type));
+ x.value = OdinLLVMBuildTransmute(p, x.value, abi_type);
+ array_add(&processed_args, x);
}
- } else if (new_type == t_llvm_bool) {
- array_add(&processed_args, lb_emit_conv(p, args[i], new_type));
- } else if (is_type_integer(new_type) || is_type_float(new_type) || is_type_boolean(new_type)) {
- array_add(&processed_args, lb_emit_transmute(p, args[i], new_type));
- } else if (is_type_simd_vector(new_type)) {
- array_add(&processed_args, lb_emit_transmute(p, args[i], new_type));
- } else if (is_type_tuple(new_type)) {
- Type *abi_type = pt->Proc.abi_compat_params[i];
- Type *st = struct_type_from_systemv_distribute_struct_fields(abi_type);
- lbValue x = {};
- i64 st_sz = type_size_of(st);
- i64 arg_sz = type_size_of(args[i].type);
- if (st_sz == arg_sz) {
- x = lb_emit_transmute(p, args[i], st);
+
+ } else if (arg->kind == lbArg_Indirect) {
+ lbValue ptr = {};
+ if (is_calling_convention_odin(pt->Proc.calling_convention)) {
+ // NOTE(bill): Odin parameters are immutable so the original value can be passed if possible
+ // i.e. `T const &` in C++
+ ptr = lb_address_from_load_or_generate_local(p, x);
} else {
- // NOTE(bill): struct{f32, f32, f32} != struct{#simd[2]f32, f32}
- GB_ASSERT(st_sz > arg_sz);
- lbAddr xx = lb_add_local_generated(p, st, false);
- lbValue pp = lb_emit_conv(p, xx.addr, alloc_type_pointer(args[i].type));
- lb_emit_store(p, pp, args[i]);
- x = lb_addr_load(p, xx);
+ ptr = lb_copy_value_to_ptr(p, x, original_type, 16);
}
- for (isize j = 0; j < new_type->Tuple.variables.count; j++) {
- lbValue xx = lb_emit_struct_ev(p, x, cast(i32)j);
- array_add(&processed_args, xx);
+ array_add(&processed_args, ptr);
+ }
+
+ param_index += 1;
+ }
+
+ if (inlining == ProcInlining_none) {
+ inlining = p->inlining;
+ }
+
+ Type *rt = reduce_tuple_to_single_type(results);
+ if (return_by_pointer) {
+ lbValue return_ptr = {};
+ if (use_return_ptr_hint && p->return_ptr_hint_value.value != nullptr) {
+ if (are_types_identical(type_deref(p->return_ptr_hint_value.type), rt)) {
+ return_ptr = p->return_ptr_hint_value;
+ p->return_ptr_hint_used = true;
}
}
+ if (return_ptr.value == nullptr) {
+ lbAddr r = lb_add_local_generated(p, rt, true);
+ return_ptr = r.addr;
+ }
+ GB_ASSERT(is_type_pointer(return_ptr.type));
+ lb_emit_call_internal(p, value, return_ptr, processed_args, nullptr, context_ptr, inlining);
+ result = lb_emit_load(p, return_ptr);
+ } else if (rt != nullptr) {
+ result = lb_emit_call_internal(p, value, {}, processed_args, rt, context_ptr, inlining);
+ if (ft->ret.cast_type) {
+ result.value = OdinLLVMBuildTransmute(p, result.value, ft->ret.cast_type);
+ }
+ result.value = OdinLLVMBuildTransmute(p, result.value, ft->ret.type);
+ result.type = rt;
+ if (LLVMTypeOf(result.value) == LLVMInt1TypeInContext(p->module->ctx)) {
+ result.type = t_llvm_bool;
+ }
+ if (!is_type_tuple(rt)) {
+ result = lb_emit_conv(p, result, rt);
+ }
} else {
- lbValue x = lb_emit_conv(p, args[i], new_type);
- array_add(&processed_args, x);
+ lb_emit_call_internal(p, value, {}, processed_args, nullptr, context_ptr, inlining);
}
- }
- if (inlining == ProcInlining_none) {
- inlining = p->inlining;
- }
-
- lbValue result = {};
+ } else {
+ for (isize i = 0; i < param_count; i++) {
+ Entity *e = pt->Proc.params->Tuple.variables[i];
+ if (e->kind != Entity_Variable) {
+ // array_add(&processed_args, args[i]);
+ continue;
+ }
+ GB_ASSERT(e->flags & EntityFlag_Param);
- Type *abi_rt = reduce_tuple_to_single_type(pt->Proc.abi_compat_result_type);
- Type *rt = reduce_tuple_to_single_type(results);
- if (pt->Proc.return_by_pointer) {
- lbValue return_ptr = {};
- if (use_return_ptr_hint && p->return_ptr_hint_value.value != nullptr) {
- if (are_types_identical(type_deref(p->return_ptr_hint_value.type), rt)) {
- return_ptr = p->return_ptr_hint_value;
- p->return_ptr_hint_used = true;
+ Type *original_type = e->type;
+ Type *new_type = pt->Proc.abi_compat_params[i];
+ Type *arg_type = args[i].type;
+
+ if (are_types_identical(arg_type, new_type)) {
+ // NOTE(bill): Done
+ array_add(&processed_args, args[i]);
+ } else if (!are_types_identical(original_type, new_type)) {
+ if (is_type_pointer(new_type) && !is_type_pointer(original_type)) {
+ Type *av = core_type(type_deref(new_type));
+ if (are_types_identical(av, core_type(original_type))) {
+ if (e->flags&EntityFlag_ImplicitReference) {
+ array_add(&processed_args, lb_address_from_load_or_generate_local(p, args[i]));
+ } else if (!is_type_pointer(arg_type)) {
+ array_add(&processed_args, lb_copy_value_to_ptr(p, args[i], original_type, 16));
+ }
+ } else {
+ array_add(&processed_args, lb_emit_transmute(p, args[i], new_type));
+ }
+ } else if (new_type == t_llvm_bool) {
+ array_add(&processed_args, lb_emit_conv(p, args[i], new_type));
+ } else if (is_type_integer(new_type) || is_type_float(new_type) || is_type_boolean(new_type)) {
+ array_add(&processed_args, lb_emit_transmute(p, args[i], new_type));
+ } else if (is_type_simd_vector(new_type)) {
+ array_add(&processed_args, lb_emit_transmute(p, args[i], new_type));
+ } else if (is_type_tuple(new_type)) {
+ Type *abi_type = pt->Proc.abi_compat_params[i];
+ Type *st = struct_type_from_systemv_distribute_struct_fields(abi_type);
+ lbValue x = {};
+ i64 st_sz = type_size_of(st);
+ i64 arg_sz = type_size_of(args[i].type);
+ if (st_sz == arg_sz) {
+ x = lb_emit_transmute(p, args[i], st);
+ } else {
+ // NOTE(bill): struct{f32, f32, f32} != struct{#simd[2]f32, f32}
+ GB_ASSERT(st_sz > arg_sz);
+ lbAddr xx = lb_add_local_generated(p, st, false);
+ lbValue pp = lb_emit_conv(p, xx.addr, alloc_type_pointer(args[i].type));
+ lb_emit_store(p, pp, args[i]);
+ x = lb_addr_load(p, xx);
+ }
+ for (isize j = 0; j < new_type->Tuple.variables.count; j++) {
+ lbValue xx = lb_emit_struct_ev(p, x, cast(i32)j);
+ array_add(&processed_args, xx);
+ }
+ }
+ } else {
+ lbValue x = lb_emit_conv(p, args[i], new_type);
+ array_add(&processed_args, x);
}
}
- if (return_ptr.value == nullptr) {
- lbAddr r = lb_add_local_generated(p, rt, true);
- return_ptr = r.addr;
+
+ if (inlining == ProcInlining_none) {
+ inlining = p->inlining;
}
- GB_ASSERT(is_type_pointer(return_ptr.type));
- lb_emit_call_internal(p, value, return_ptr, processed_args, nullptr, context_ptr, inlining);
- result = lb_emit_load(p, return_ptr);
- } else {
- result = lb_emit_call_internal(p, value, {}, processed_args, abi_rt, context_ptr, inlining);
- if (abi_rt != rt) {
- result = lb_emit_transmute(p, result, rt);
+
+
+ Type *abi_rt = reduce_tuple_to_single_type(pt->Proc.abi_compat_result_type);
+ Type *rt = reduce_tuple_to_single_type(results);
+ if (pt->Proc.return_by_pointer) {
+ lbValue return_ptr = {};
+ if (use_return_ptr_hint && p->return_ptr_hint_value.value != nullptr) {
+ if (are_types_identical(type_deref(p->return_ptr_hint_value.type), rt)) {
+ return_ptr = p->return_ptr_hint_value;
+ p->return_ptr_hint_used = true;
+ }
+ }
+ if (return_ptr.value == nullptr) {
+ lbAddr r = lb_add_local_generated(p, rt, true);
+ return_ptr = r.addr;
+ }
+ GB_ASSERT(is_type_pointer(return_ptr.type));
+ lb_emit_call_internal(p, value, return_ptr, processed_args, nullptr, context_ptr, inlining);
+ result = lb_emit_load(p, return_ptr);
+ } else {
+ result = lb_emit_call_internal(p, value, {}, processed_args, abi_rt, context_ptr, inlining);
+ if (abi_rt != rt) {
+ result = lb_emit_transmute(p, result, rt);
+ }
}
}
@@ -7176,7 +7678,7 @@ lbValue lb_emit_call(lbProcedure *p, lbValue value, Array<lbValue> const &args,
case DeferredProcedure_in_out:
{
auto out_args = lb_value_to_array(p, result);
- array_init(&result_as_args, heap_allocator(), in_args.count + out_args.count);
+ array_init(&result_as_args, permanent_allocator(), in_args.count + out_args.count);
array_copy(&result_as_args, in_args, 0);
array_copy(&result_as_args, out_args, in_args.count);
}
@@ -7285,7 +7787,7 @@ lbValue lb_string_len(lbProcedure *p, lbValue string) {
lbValue lb_cstring_len(lbProcedure *p, lbValue value) {
GB_ASSERT(is_type_cstring(value.type));
- auto args = array_make<lbValue>(heap_allocator(), 1);
+ auto args = array_make<lbValue>(permanent_allocator(), 1);
args[0] = lb_emit_conv(p, value, t_cstring);
return lb_emit_runtime_call(p, "cstring_len", args);
}
@@ -7323,7 +7825,6 @@ lbValue lb_dynamic_array_allocator(lbProcedure *p, lbValue da) {
}
lbValue lb_map_entries(lbProcedure *p, lbValue value) {
- gbAllocator a = heap_allocator();
Type *t = base_type(value.type);
GB_ASSERT_MSG(t->kind == Type_Map, "%s", type_to_string(t));
init_map_internal_types(t);
@@ -7334,7 +7835,6 @@ lbValue lb_map_entries(lbProcedure *p, lbValue value) {
}
lbValue lb_map_entries_ptr(lbProcedure *p, lbValue value) {
- gbAllocator a = heap_allocator();
Type *t = base_type(type_deref(value.type));
GB_ASSERT_MSG(t->kind == Type_Map, "%s", type_to_string(t));
init_map_internal_types(t);
@@ -7458,7 +7958,7 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv,
}
GB_ASSERT(is_type_typeid(tav.type));
- auto args = array_make<lbValue>(heap_allocator(), 1);
+ auto args = array_make<lbValue>(permanent_allocator(), 1);
args[0] = lb_build_expr(p, arg);
return lb_emit_runtime_call(p, "__type_info_of", args);
}
@@ -7466,16 +7966,9 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv,
case BuiltinProc_typeid_of: {
Ast *arg = ce->args[0];
TypeAndValue tav = type_and_value_of_expr(arg);
- if (tav.mode == Addressing_Type) {
- Type *t = default_type(type_of_expr(arg));
- return lb_typeid(p->module, t);
- }
- Type *t = base_type(tav.type);
- GB_ASSERT(are_types_identical(t, t_type_info_ptr));
-
- auto args = array_make<lbValue>(heap_allocator(), 1);
- args[0] = lb_emit_conv(p, lb_build_expr(p, arg), t_type_info_ptr);
- return lb_emit_runtime_call(p, "__typeid_of", args);
+ GB_ASSERT(tav.mode == Addressing_Type);
+ Type *t = default_type(type_of_expr(arg));
+ return lb_typeid(p->module, t);
}
case BuiltinProc_len: {
@@ -7542,7 +8035,7 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv,
}
unsigned mask_len = cast(unsigned)index_count;
- LLVMValueRef *mask_elems = gb_alloc_array(heap_allocator(), LLVMValueRef, index_count);
+ LLVMValueRef *mask_elems = gb_alloc_array(permanent_allocator(), LLVMValueRef, index_count);
for (isize i = 1; i < ce->args.count; i++) {
TypeAndValue tv = type_and_value_of_expr(ce->args[i]);
GB_ASSERT(is_type_integer(tv.type));
@@ -7772,7 +8265,6 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv,
}
case BuiltinProc_abs: {
- gbAllocator a = heap_allocator();
lbValue x = lb_build_expr(p, ce->args[0]);
Type *t = x.type;
if (is_type_unsigned(t)) {
@@ -7780,7 +8272,7 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv,
}
if (is_type_quaternion(t)) {
i64 sz = 8*type_size_of(t);
- auto args = array_make<lbValue>(heap_allocator(), 1);
+ auto args = array_make<lbValue>(permanent_allocator(), 1);
args[0] = x;
switch (sz) {
case 128: return lb_emit_runtime_call(p, "abs_quaternion128", args);
@@ -7789,7 +8281,7 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv,
GB_PANIC("Unknown complex type");
} else if (is_type_complex(t)) {
i64 sz = 8*type_size_of(t);
- auto args = array_make<lbValue>(heap_allocator(), 1);
+ auto args = array_make<lbValue>(permanent_allocator(), 1);
args[0] = x;
switch (sz) {
case 64: return lb_emit_runtime_call(p, "abs_complex64", args);
@@ -7798,7 +8290,7 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv,
GB_PANIC("Unknown complex type");
} else if (is_type_float(t)) {
i64 sz = 8*type_size_of(t);
- auto args = array_make<lbValue>(heap_allocator(), 1);
+ auto args = array_make<lbValue>(permanent_allocator(), 1);
args[0] = x;
switch (sz) {
case 32: return lb_emit_runtime_call(p, "abs_f32", args);
@@ -7838,8 +8330,8 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv,
{
LLVMTypeRef func_type = LLVMFunctionType(LLVMVoidTypeInContext(p->module->ctx), nullptr, 0, false);
LLVMValueRef the_asm = LLVMGetInlineAsm(func_type,
- "pause", 5,
- "", 0,
+ cast(char *)"pause", 5,
+ cast(char *)"", 0,
/*HasSideEffects*/true, /*IsAlignStack*/false,
LLVMInlineAsmDialectATT
);
@@ -8054,7 +8546,7 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv,
GB_ASSERT(tv.type->kind == Type_Tuple);
Type *fix_typed = alloc_type_tuple();
- array_init(&fix_typed->Tuple.variables, heap_allocator(), 2);
+ array_init(&fix_typed->Tuple.variables, permanent_allocator(), 2);
fix_typed->Tuple.variables[0] = tv.type->Tuple.variables[0];
fix_typed->Tuple.variables[1] = alloc_entity_field(nullptr, blank_token, t_llvm_bool, false, 1);
@@ -8063,6 +8555,13 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv,
res.type = fix_typed;
return res;
}
+
+
+ case BuiltinProc_type_equal_proc:
+ return lb_get_equal_proc_for_type(p->module, ce->args[0]->tav.type);
+
+ case BuiltinProc_type_hasher_proc:
+ return lb_get_hasher_proc_for_type(p->module, ce->args[0]->tav.type);
}
GB_PANIC("Unhandled built-in procedure %.*s", LIT(builtin_procs[id].name));
@@ -8169,10 +8668,10 @@ lbValue lb_build_call_expr(lbProcedure *p, Ast *expr) {
Type *proc_type_ = base_type(value.type);
GB_ASSERT(proc_type_->kind == Type_Proc);
TypeProc *pt = &proc_type_->Proc;
- set_procedure_abi_types(heap_allocator(), proc_type_);
+ set_procedure_abi_types(proc_type_);
if (is_call_expr_field_value(ce)) {
- auto args = array_make<lbValue>(heap_allocator(), pt->param_count);
+ auto args = array_make<lbValue>(permanent_allocator(), pt->param_count);
for_array(arg_index, ce->args) {
Ast *arg = ce->args[arg_index];
@@ -8241,7 +8740,7 @@ lbValue lb_build_call_expr(lbProcedure *p, Ast *expr) {
param_count = pt->params->Tuple.variables.count;
}
- auto args = array_make<lbValue>(heap_allocator(), cast(isize)gb_max(param_count, arg_count));
+ auto args = array_make<lbValue>(permanent_allocator(), cast(isize)gb_max(param_count, arg_count));
isize variadic_index = pt->variadic_index;
bool variadic = pt->variadic && variadic_index >= 0;
bool vari_expand = ce->ellipsis.pos.line != 0;
@@ -8349,7 +8848,6 @@ lbValue lb_build_call_expr(lbProcedure *p, Ast *expr) {
if (variadic && !vari_expand && !is_c_vararg) {
// variadic call argument generation
- gbAllocator allocator = heap_allocator();
Type *slice_type = param_tuple->variables[variadic_index]->type;
Type *elem_type = base_type(slice_type)->Slice.elem;
lbAddr slice = lb_add_local_generated(p, slice_type, true);
@@ -8625,7 +9123,7 @@ lbValue lb_emit_comp_against_nil(lbProcedure *p, TokenKind op_kind, lbValue x) {
lbValue invalid_typeid = lb_const_value(p->module, t_typeid, exact_value_i64(0));
return lb_emit_comp(p, op_kind, x, invalid_typeid);
} else if (is_type_bit_field(t)) {
- auto args = array_make<lbValue>(heap_allocator(), 2);
+ auto args = array_make<lbValue>(permanent_allocator(), 2);
lbValue lhs = lb_address_from_load_or_generate_local(p, x);
args[0] = lb_emit_conv(p, lhs, t_rawptr);
args[1] = lb_const_int(p->module, t_int, type_size_of(t));
@@ -8654,7 +9152,7 @@ lbValue lb_emit_comp_against_nil(lbProcedure *p, TokenKind op_kind, lbValue x) {
}
}
} else if (is_type_struct(t) && type_has_nil(t)) {
- auto args = array_make<lbValue>(heap_allocator(), 2);
+ auto args = array_make<lbValue>(permanent_allocator(), 2);
lbValue lhs = lb_address_from_load_or_generate_local(p, x);
args[0] = lb_emit_conv(p, lhs, t_rawptr);
args[1] = lb_const_int(p->module, t_int, type_size_of(t));
@@ -8665,6 +9163,230 @@ lbValue lb_emit_comp_against_nil(lbProcedure *p, TokenKind op_kind, lbValue x) {
return {};
}
+lbValue lb_get_equal_proc_for_type(lbModule *m, Type *type) {
+ Type *original_type = type;
+ type = base_type(type);
+ GB_ASSERT(is_type_comparable(type));
+
+ Type *pt = alloc_type_pointer(type);
+ LLVMTypeRef ptr_type = lb_type(m, pt);
+
+ auto key = hash_type(type);
+ lbProcedure **found = map_get(&m->equal_procs, key);
+ lbProcedure *compare_proc = nullptr;
+ if (found) {
+ compare_proc = *found;
+ GB_ASSERT(compare_proc != nullptr);
+ return {compare_proc->value, compare_proc->type};
+ }
+
+ static u32 proc_index = 0;
+
+ char buf[16] = {};
+ isize n = gb_snprintf(buf, 16, "__$equal%u", ++proc_index);
+ char *str = gb_alloc_str_len(permanent_allocator(), buf, n-1);
+ String proc_name = make_string_c(str);
+
+ lbProcedure *p = lb_create_dummy_procedure(m, proc_name, t_equal_proc);
+ map_set(&m->equal_procs, key, p);
+ lb_begin_procedure_body(p);
+
+ LLVMValueRef x = LLVMGetParam(p->value, 0);
+ LLVMValueRef y = LLVMGetParam(p->value, 1);
+ x = LLVMBuildPointerCast(p->builder, x, ptr_type, "");
+ y = LLVMBuildPointerCast(p->builder, y, ptr_type, "");
+ lbValue lhs = {x, pt};
+ lbValue rhs = {y, pt};
+
+
+ lbBlock *block_same_ptr = lb_create_block(p, "same_ptr");
+ lbBlock *block_diff_ptr = lb_create_block(p, "diff_ptr");
+
+ lbValue same_ptr = lb_emit_comp(p, Token_CmpEq, lhs, rhs);
+ lb_emit_if(p, same_ptr, block_same_ptr, block_diff_ptr);
+ lb_start_block(p, block_same_ptr);
+ LLVMBuildRet(p->builder, LLVMConstInt(lb_type(m, t_bool), 1, false));
+
+ lb_start_block(p, block_diff_ptr);
+
+ if (type->kind == Type_Struct) {
+ type_set_offsets(type);
+
+ lbBlock *block_false = lb_create_block(p, "bfalse");
+ lbValue res = lb_const_bool(m, t_bool, true);
+
+ for_array(i, type->Struct.fields) {
+ lbBlock *next_block = lb_create_block(p, "btrue");
+
+ lbValue pleft = lb_emit_struct_ep(p, lhs, cast(i32)i);
+ lbValue pright = lb_emit_struct_ep(p, rhs, cast(i32)i);
+ lbValue left = lb_emit_load(p, pleft);
+ lbValue right = lb_emit_load(p, pright);
+ lbValue ok = lb_emit_comp(p, Token_CmpEq, left, right);
+
+ lb_emit_if(p, ok, next_block, block_false);
+
+ lb_emit_jump(p, next_block);
+ lb_start_block(p, next_block);
+ }
+
+ LLVMBuildRet(p->builder, LLVMConstInt(lb_type(m, t_bool), 1, false));
+
+ lb_start_block(p, block_false);
+
+ LLVMBuildRet(p->builder, LLVMConstInt(lb_type(m, t_bool), 0, false));
+ } else {
+ lbValue left = lb_emit_load(p, lhs);
+ lbValue right = lb_emit_load(p, rhs);
+ lbValue ok = lb_emit_comp(p, Token_CmpEq, left, right);
+ ok = lb_emit_conv(p, ok, t_bool);
+ LLVMBuildRet(p->builder, ok.value);
+ }
+
+ lb_end_procedure_body(p);
+
+ compare_proc = p;
+ return {compare_proc->value, compare_proc->type};
+}
+
+lbValue lb_simple_compare_hash(lbProcedure *p, Type *type, lbValue data, lbValue seed) {
+ GB_ASSERT_MSG(is_type_simple_compare(type), "%s", type_to_string(type));
+
+ i64 sz = type_size_of(type);
+
+ if (1 <= sz && sz <= 16) {
+ char name[20] = {};
+ gb_snprintf(name, 20, "default_hasher%d", cast(i32)sz);
+
+ auto args = array_make<lbValue>(permanent_allocator(), 2);
+ args[0] = data;
+ args[1] = seed;
+ return lb_emit_runtime_call(p, name, args);
+ }
+
+ auto args = array_make<lbValue>(permanent_allocator(), 3);
+ args[0] = data;
+ args[1] = seed;
+ args[2] = lb_const_int(p->module, t_int, type_size_of(type));
+ return lb_emit_runtime_call(p, "default_hasher_n", args);
+}
+
+lbValue lb_get_hasher_proc_for_type(lbModule *m, Type *type) {
+ Type *original_type = type;
+ type = core_type(type);
+ GB_ASSERT(is_type_valid_for_keys(type));
+
+ Type *pt = alloc_type_pointer(type);
+ LLVMTypeRef ptr_type = lb_type(m, pt);
+
+ auto key = hash_type(type);
+ lbProcedure **found = map_get(&m->hasher_procs, key);
+ if (found) {
+ GB_ASSERT(*found != nullptr);
+ return {(*found)->value, (*found)->type};
+ }
+
+ static u32 proc_index = 0;
+
+ char buf[16] = {};
+ isize n = gb_snprintf(buf, 16, "__$hasher%u", ++proc_index);
+ char *str = gb_alloc_str_len(permanent_allocator(), buf, n-1);
+ String proc_name = make_string_c(str);
+
+ lbProcedure *p = lb_create_dummy_procedure(m, proc_name, t_hasher_proc);
+ map_set(&m->hasher_procs, key, p);
+ lb_begin_procedure_body(p);
+ defer (lb_end_procedure_body(p));
+
+ LLVMValueRef x = LLVMGetParam(p->value, 0);
+ LLVMValueRef y = LLVMGetParam(p->value, 1);
+ lbValue data = {x, t_rawptr};
+ lbValue seed = {y, t_uintptr};
+
+ if (is_type_simple_compare(type)) {
+ lbValue res = lb_simple_compare_hash(p, type, data, seed);
+ LLVMBuildRet(p->builder, res.value);
+ return {p->value, p->type};
+ }
+
+ if (type->kind == Type_Struct) {
+ type_set_offsets(type);
+ data = lb_emit_conv(p, data, t_u8_ptr);
+
+ auto args = array_make<lbValue>(permanent_allocator(), 2);
+ for_array(i, type->Struct.fields) {
+ i64 offset = type->Struct.offsets[i];
+ Entity *field = type->Struct.fields[i];
+ lbValue field_hasher = lb_get_hasher_proc_for_type(m, field->type);
+ lbValue ptr = lb_emit_ptr_offset(p, data, lb_const_int(m, t_uintptr, offset));
+
+ args[0] = ptr;
+ args[1] = seed;
+ seed = lb_emit_call(p, field_hasher, args);
+ }
+ LLVMBuildRet(p->builder, seed.value);
+ } else if (type->kind == Type_Array) {
+ lbAddr pres = lb_add_local_generated(p, t_uintptr, false);
+ lb_addr_store(p, pres, seed);
+
+ auto args = array_make<lbValue>(permanent_allocator(), 2);
+ lbValue elem_hasher = lb_get_hasher_proc_for_type(m, type->Array.elem);
+
+ auto loop_data = lb_loop_start(p, type->Array.count, t_i32);
+
+ data = lb_emit_conv(p, data, pt);
+
+ lbValue ptr = lb_emit_array_ep(p, data, loop_data.idx);
+ args[0] = ptr;
+ args[1] = lb_addr_load(p, pres);
+ lbValue new_seed = lb_emit_call(p, elem_hasher, args);
+ lb_addr_store(p, pres, new_seed);
+
+ lb_loop_end(p, loop_data);
+
+ lbValue res = lb_addr_load(p, pres);
+ LLVMBuildRet(p->builder, res.value);
+ } else if (type->kind == Type_EnumeratedArray) {
+ lbAddr res = lb_add_local_generated(p, t_uintptr, false);
+ lb_addr_store(p, res, seed);
+
+ auto args = array_make<lbValue>(permanent_allocator(), 2);
+ lbValue elem_hasher = lb_get_hasher_proc_for_type(m, type->EnumeratedArray.elem);
+
+ auto loop_data = lb_loop_start(p, type->EnumeratedArray.count, t_i32);
+
+ data = lb_emit_conv(p, data, pt);
+
+ lbValue ptr = lb_emit_array_ep(p, data, loop_data.idx);
+ args[0] = ptr;
+ args[1] = lb_addr_load(p, res);
+ lbValue new_seed = lb_emit_call(p, elem_hasher, args);
+ lb_addr_store(p, res, new_seed);
+
+ lb_loop_end(p, loop_data);
+
+ lbValue vres = lb_addr_load(p, res);
+ LLVMBuildRet(p->builder, vres.value);
+ } else if (is_type_cstring(type)) {
+ auto args = array_make<lbValue>(permanent_allocator(), 2);
+ args[0] = data;
+ args[1] = seed;
+ lbValue res = lb_emit_runtime_call(p, "default_hasher_cstring", args);
+ LLVMBuildRet(p->builder, res.value);
+ } else if (is_type_string(type)) {
+ auto args = array_make<lbValue>(permanent_allocator(), 2);
+ args[0] = data;
+ args[1] = seed;
+ lbValue res = lb_emit_runtime_call(p, "default_hasher_string", args);
+ LLVMBuildRet(p->builder, res.value);
+ } else {
+ GB_PANIC("Unhandled type for hasher: %s", type_to_string(type));
+ }
+
+ return {p->value, p->type};
+}
+
+
lbValue lb_emit_comp(lbProcedure *p, TokenKind op_kind, lbValue left, lbValue right) {
Type *a = core_type(left.type);
@@ -8689,8 +9411,6 @@ lbValue lb_emit_comp(lbProcedure *p, TokenKind op_kind, lbValue left, lbValue ri
} else if (lb_is_const(right) || lb_is_const_nil(right)) {
right = lb_emit_conv(p, right, left.type);
} else {
- gbAllocator a = heap_allocator();
-
Type *lt = left.type;
Type *rt = right.type;
@@ -8770,7 +9490,7 @@ lbValue lb_emit_comp(lbProcedure *p, TokenKind op_kind, lbValue left, lbValue ri
} else {
if (is_type_simple_compare(tl) && (op_kind == Token_CmpEq || op_kind == Token_NotEq)) {
// TODO(bill): Test to see if this is actually faster!!!!
- auto args = array_make<lbValue>(heap_allocator(), 3);
+ auto args = array_make<lbValue>(permanent_allocator(), 3);
args[0] = lb_emit_conv(p, lhs, t_rawptr);
args[1] = lb_emit_conv(p, rhs, t_rawptr);
args[2] = lb_const_int(p->module, t_int, type_size_of(tl));
@@ -8796,6 +9516,31 @@ lbValue lb_emit_comp(lbProcedure *p, TokenKind op_kind, lbValue left, lbValue ri
}
}
+
+ if (is_type_struct(a) && is_type_comparable(a)) {
+ lbValue left_ptr = lb_address_from_load_or_generate_local(p, left);
+ lbValue right_ptr = lb_address_from_load_or_generate_local(p, right);
+ lbValue res = {};
+ if (is_type_simple_compare(a)) {
+ // TODO(bill): Test to see if this is actually faster!!!!
+ auto args = array_make<lbValue>(permanent_allocator(), 3);
+ args[0] = lb_emit_conv(p, left_ptr, t_rawptr);
+ args[1] = lb_emit_conv(p, right_ptr, t_rawptr);
+ args[2] = lb_const_int(p->module, t_int, type_size_of(a));
+ res = lb_emit_runtime_call(p, "memory_equal", args);
+ } else {
+ lbValue value = lb_get_equal_proc_for_type(p->module, a);
+ auto args = array_make<lbValue>(permanent_allocator(), 2);
+ args[0] = lb_emit_conv(p, left_ptr, t_rawptr);
+ args[1] = lb_emit_conv(p, right_ptr, t_rawptr);
+ res = lb_emit_call(p, value, args);
+ }
+ if (op_kind == Token_NotEq) {
+ res = lb_emit_unary_arith(p, Token_Not, res, res.type);
+ }
+ return res;
+ }
+
if (is_type_string(a)) {
if (is_type_cstring(a)) {
left = lb_emit_conv(p, left, t_string);
@@ -8813,7 +9558,7 @@ lbValue lb_emit_comp(lbProcedure *p, TokenKind op_kind, lbValue left, lbValue ri
}
GB_ASSERT(runtime_procedure != nullptr);
- auto args = array_make<lbValue>(heap_allocator(), 2);
+ auto args = array_make<lbValue>(permanent_allocator(), 2);
args[0] = left;
args[1] = right;
return lb_emit_runtime_call(p, runtime_procedure, args);
@@ -8838,7 +9583,7 @@ lbValue lb_emit_comp(lbProcedure *p, TokenKind op_kind, lbValue left, lbValue ri
}
GB_ASSERT(runtime_procedure != nullptr);
- auto args = array_make<lbValue>(heap_allocator(), 2);
+ auto args = array_make<lbValue>(permanent_allocator(), 2);
args[0] = left;
args[1] = right;
return lb_emit_runtime_call(p, runtime_procedure, args);
@@ -8863,7 +9608,7 @@ lbValue lb_emit_comp(lbProcedure *p, TokenKind op_kind, lbValue left, lbValue ri
}
GB_ASSERT(runtime_procedure != nullptr);
- auto args = array_make<lbValue>(heap_allocator(), 2);
+ auto args = array_make<lbValue>(permanent_allocator(), 2);
args[0] = left;
args[1] = right;
return lb_emit_runtime_call(p, runtime_procedure, args);
@@ -9012,14 +9757,14 @@ lbValue lb_generate_anonymous_proc_lit(lbModule *m, String const &prefix_name, A
// NOTE(bill): Generate a new name
// parent$count
isize name_len = prefix_name.len + 1 + 8 + 1;
- char *name_text = gb_alloc_array(heap_allocator(), char, name_len);
+ char *name_text = gb_alloc_array(permanent_allocator(), char, name_len);
i32 name_id = cast(i32)m->anonymous_proc_lits.entries.count;
name_len = gb_snprintf(name_text, name_len, "%.*s$anon-%d", LIT(prefix_name), name_id);
String name = make_string((u8 *)name_text, name_len-1);
Type *type = type_of_expr(expr);
- set_procedure_abi_types(heap_allocator(), type);
+ set_procedure_abi_types(type);
Token token = {};
@@ -9115,7 +9860,7 @@ lbValue lb_emit_union_cast(lbProcedure *p, lbValue value, Type *type, TokenPos p
Type *dst_type = tuple->Tuple.variables[0]->type;
lbValue ok = lb_emit_load(p, lb_emit_struct_ep(p, v.addr, 1));
- auto args = array_make<lbValue>(heap_allocator(), 6);
+ auto args = array_make<lbValue>(permanent_allocator(), 7);
args[0] = ok;
args[1] = lb_const_string(m, pos.file);
@@ -9124,7 +9869,8 @@ lbValue lb_emit_union_cast(lbProcedure *p, lbValue value, Type *type, TokenPos p
args[4] = lb_typeid(m, src_type);
args[5] = lb_typeid(m, dst_type);
- lb_emit_runtime_call(p, "type_assertion_check", args);
+ args[6] = lb_emit_conv(p, value_, t_rawptr);
+ lb_emit_runtime_call(p, "type_assertion_check2", args);
}
return lb_emit_load(p, lb_emit_struct_ep(p, v.addr, 0));
@@ -9176,7 +9922,7 @@ lbAddr lb_emit_any_cast_addr(lbProcedure *p, lbValue value, Type *type, TokenPos
// NOTE(bill): Panic on invalid conversion
lbValue ok = lb_emit_load(p, lb_emit_struct_ep(p, v.addr, 1));
- auto args = array_make<lbValue>(heap_allocator(), 6);
+ auto args = array_make<lbValue>(permanent_allocator(), 7);
args[0] = ok;
args[1] = lb_const_string(m, pos.file);
@@ -9185,7 +9931,8 @@ lbAddr lb_emit_any_cast_addr(lbProcedure *p, lbValue value, Type *type, TokenPos
args[4] = any_typeid;
args[5] = dst_typeid;
- lb_emit_runtime_call(p, "type_assertion_check", args);
+ args[6] = lb_emit_struct_ev(p, value, 0);;
+ lb_emit_runtime_call(p, "type_assertion_check2", args);
return lb_addr(lb_emit_struct_ep(p, v.addr, 0));
}
@@ -9460,7 +10207,6 @@ lbValue lb_build_expr(lbProcedure *p, Ast *expr) {
return addr.addr;
} else if (ue_expr->kind == Ast_TypeAssertion) {
- gbAllocator a = heap_allocator();
GB_ASSERT(is_type_pointer(tv.type));
ast_node(ta, TypeAssertion, ue_expr);
@@ -9482,7 +10228,7 @@ lbValue lb_build_expr(lbProcedure *p, Ast *expr) {
lbValue dst_tag = lb_const_union_tag(p->module, src_type, dst_type);
lbValue ok = lb_emit_comp(p, Token_CmpEq, src_tag, dst_tag);
- auto args = array_make<lbValue>(heap_allocator(), 6);
+ auto args = array_make<lbValue>(permanent_allocator(), 6);
args[0] = ok;
args[1] = lb_find_or_add_entity_string(p->module, pos.file);
@@ -9507,7 +10253,7 @@ lbValue lb_build_expr(lbProcedure *p, Ast *expr) {
lbValue ok = lb_emit_comp(p, Token_CmpEq, any_id, id);
- auto args = array_make<lbValue>(heap_allocator(), 6);
+ auto args = array_make<lbValue>(permanent_allocator(), 6);
args[0] = ok;
args[1] = lb_find_or_add_entity_string(p->module, pos.file);
@@ -9658,7 +10404,6 @@ lbAddr lb_build_addr_from_entity(lbProcedure *p, Entity *e, Ast *expr) {
lbValue lb_gen_map_header(lbProcedure *p, lbValue map_val_ptr, Type *map_type) {
GB_ASSERT_MSG(is_type_pointer(map_val_ptr.type), "%s", type_to_string(map_val_ptr.type));
- gbAllocator a = heap_allocator();
lbAddr h = lb_add_local_generated(p, t_map_header, false); // all the values will be initialzed later
map_type = base_type(map_type);
GB_ASSERT(map_type->kind == Type_Map);
@@ -9671,65 +10416,96 @@ lbValue lb_gen_map_header(lbProcedure *p, lbValue map_val_ptr, Type *map_type) {
lbValue m = lb_emit_conv(p, map_val_ptr, type_deref(gep0.type));
lb_emit_store(p, gep0, m);
- lb_emit_store(p, lb_emit_struct_ep(p, h.addr, 1), lb_const_bool(p->module, t_bool, is_type_string(key_type)));
-
i64 entry_size = type_size_of (map_type->Map.entry_type);
i64 entry_align = type_align_of (map_type->Map.entry_type);
- i64 value_offset = type_offset_of(map_type->Map.entry_type, 2);
+
+ i64 key_offset = type_offset_of(map_type->Map.entry_type, 2);
+ i64 key_size = type_size_of (map_type->Map.key);
+
+ i64 value_offset = type_offset_of(map_type->Map.entry_type, 3);
i64 value_size = type_size_of (map_type->Map.value);
+ lb_emit_store(p, lb_emit_struct_ep(p, h.addr, 1), lb_get_equal_proc_for_type(p->module, key_type));
lb_emit_store(p, lb_emit_struct_ep(p, h.addr, 2), lb_const_int(p->module, t_int, entry_size));
lb_emit_store(p, lb_emit_struct_ep(p, h.addr, 3), lb_const_int(p->module, t_int, entry_align));
- lb_emit_store(p, lb_emit_struct_ep(p, h.addr, 4), lb_const_int(p->module, t_uintptr, value_offset));
- lb_emit_store(p, lb_emit_struct_ep(p, h.addr, 5), lb_const_int(p->module, t_int, value_size));
+ lb_emit_store(p, lb_emit_struct_ep(p, h.addr, 4), lb_const_int(p->module, t_uintptr, key_offset));
+ lb_emit_store(p, lb_emit_struct_ep(p, h.addr, 5), lb_const_int(p->module, t_int, key_size));
+ lb_emit_store(p, lb_emit_struct_ep(p, h.addr, 6), lb_const_int(p->module, t_uintptr, value_offset));
+ lb_emit_store(p, lb_emit_struct_ep(p, h.addr, 7), lb_const_int(p->module, t_int, value_size));
return lb_addr_load(p, h);
}
-lbValue lb_gen_map_key(lbProcedure *p, lbValue key, Type *key_type) {
- Type *hash_type = t_u64;
- lbAddr v = lb_add_local_generated(p, t_map_key, true);
- lbValue vp = lb_addr_get_ptr(p, v);
- Type *t = base_type(key.type);
- key = lb_emit_conv(p, key, key_type);
+lbValue lb_const_hash(lbModule *m, lbValue key, Type *key_type) {
+ if (true) {
+ return {};
+ }
+
+ lbValue hashed_key = {};
- if (is_type_string(t)) {
- lbValue str = lb_emit_conv(p, key, t_string);
- lbValue hashed_str = {};
- if (lb_is_const(str)) {
- String v = lb_get_const_string(p->module, str);
- u64 hs = fnv64a(v.text, v.len);
- hashed_str = lb_const_int(p->module, t_u64, hs);
+ if (lb_is_const(key)) {
+ u64 hash = 0xcbf29ce484222325;
+ if (is_type_cstring(key_type)) {
+ size_t length = 0;
+ char const *text = LLVMGetAsString(key.value, &length);
+ hash = fnv64a(text, cast(isize)length);
+ } else if (is_type_string(key_type)) {
+ unsigned data_indices[] = {0};
+ unsigned len_indices[] = {1};
+ LLVMValueRef data = LLVMConstExtractValue(key.value, data_indices, gb_count_of(data_indices));
+ LLVMValueRef len = LLVMConstExtractValue(key.value, len_indices, gb_count_of(len_indices));
+ isize length = LLVMConstIntGetSExtValue(len);
+ char const *text = nullptr;
+ if (length != 0) {
+ if (LLVMGetConstOpcode(data) != LLVMGetElementPtr) {
+ return {};
+ }
+ // TODO(bill): THIS IS BROKEN! THIS NEEDS FIXING :P
+
+ size_t ulength = 0;
+ text = LLVMGetAsString(data, &ulength);
+ gb_printf_err("%td %td %s\n", length, ulength, text);
+ length = gb_min(length, cast(isize)ulength);
+ }
+ hash = fnv64a(text, cast(isize)length);
} else {
- auto args = array_make<lbValue>(heap_allocator(), 1);
- args[0] = str;
- hashed_str = lb_emit_runtime_call(p, "default_hash_string", args);
+ return {};
}
- lb_emit_store(p, lb_emit_struct_ep(p, vp, 0), hashed_str);
+ // TODO(bill): other const hash types
- lbValue key_data = lb_emit_struct_ep(p, vp, 1);
- key_data = lb_emit_conv(p, key_data, alloc_type_pointer(key_type));
- lb_emit_store(p, key_data, str);
- } else {
- i64 sz = type_size_of(t);
- GB_ASSERT(sz <= 8);
- if (sz != 0) {
- auto args = array_make<lbValue>(heap_allocator(), 2);
- args[0] = lb_address_from_load_or_generate_local(p, key);
- args[1] = lb_const_int(p->module, t_int, sz);
- lbValue hash = lb_emit_runtime_call(p, "default_hash_ptr", args);
+ if (build_context.word_size == 4) {
+ hash &= 0xffffffffull;
+ }
+ hashed_key = lb_const_int(m, t_uintptr, hash);
+ }
+
+ return hashed_key;
+}
+
+lbValue lb_gen_map_hash(lbProcedure *p, lbValue key, Type *key_type) {
+ Type *hash_type = t_u64;
+ lbAddr v = lb_add_local_generated(p, t_map_hash, true);
+ lbValue vp = lb_addr_get_ptr(p, v);
+ Type *t = base_type(key.type);
+ key = lb_emit_conv(p, key, key_type);
+ lbValue key_ptr = lb_address_from_load_or_generate_local(p, key);
+ key_ptr = lb_emit_conv(p, key_ptr, t_rawptr);
- lbValue hash_ptr = lb_emit_struct_ep(p, vp, 0);
- lbValue key_data = lb_emit_struct_ep(p, vp, 1);
- key_data = lb_emit_conv(p, key_data, alloc_type_pointer(key_type));
+ lbValue hashed_key = lb_const_hash(p->module, key, key_type);
+ if (hashed_key.value == nullptr) {
+ lbValue hasher = lb_get_hasher_proc_for_type(p->module, key_type);
- lb_emit_store(p, hash_ptr, hash);
- lb_emit_store(p, key_data, key);
- }
+ auto args = array_make<lbValue>(permanent_allocator(), 2);
+ args[0] = key_ptr;
+ args[1] = lb_const_int(p->module, t_uintptr, 0);
+ hashed_key = lb_emit_call(p, hasher, args);
}
+ lb_emit_store(p, lb_emit_struct_ep(p, vp, 0), hashed_key);
+ lb_emit_store(p, lb_emit_struct_ep(p, vp, 1), key_ptr);
+
return lb_addr_load(p, v);
}
@@ -9739,13 +10515,13 @@ void lb_insert_dynamic_map_key_and_value(lbProcedure *p, lbAddr addr, Type *map_
GB_ASSERT(map_type->kind == Type_Map);
lbValue h = lb_gen_map_header(p, addr.addr, map_type);
- lbValue key = lb_gen_map_key(p, map_key, map_type->Map.key);
+ lbValue key = lb_gen_map_hash(p, map_key, map_type->Map.key);
lbValue v = lb_emit_conv(p, map_value, map_type->Map.value);
lbAddr value_addr = lb_add_local_generated(p, v.type, false);
lb_addr_store(p, value_addr, v);
- auto args = array_make<lbValue>(heap_allocator(), 4);
+ auto args = array_make<lbValue>(permanent_allocator(), 4);
args[0] = h;
args[1] = key;
args[2] = lb_emit_conv(p, value_addr.addr, t_rawptr);
@@ -9904,7 +10680,6 @@ lbAddr lb_build_addr(lbProcedure *p, Ast *expr) {
case_end;
case_ast_node(ta, TypeAssertion, expr);
- gbAllocator a = heap_allocator();
TokenPos pos = ast_token(expr).pos;
lbValue e = lb_build_expr(p, ta->expr);
Type *t = type_deref(e.type);
@@ -9941,7 +10716,6 @@ lbAddr lb_build_addr(lbProcedure *p, Ast *expr) {
case_ast_node(ie, IndexExpr, expr);
Type *t = base_type(type_of_expr(ie->expr));
- gbAllocator a = heap_allocator();
bool deref = is_type_pointer(t);
t = base_type(type_deref(t));
@@ -10141,7 +10915,6 @@ lbAddr lb_build_addr(lbProcedure *p, Ast *expr) {
case_end;
case_ast_node(se, SliceExpr, expr);
- gbAllocator a = heap_allocator();
lbValue low = lb_const_int(p->module, t_int, 0);
lbValue high = {};
@@ -10440,9 +11213,8 @@ lbAddr lb_build_addr(lbProcedure *p, Ast *expr) {
if (cl->elems.count == 0) {
break;
}
- gbAllocator a = heap_allocator();
{
- auto args = array_make<lbValue>(a, 3);
+ auto args = array_make<lbValue>(permanent_allocator(), 3);
args[0] = lb_gen_map_header(p, v.addr, type);
args[1] = lb_const_int(p->module, t_int, 2*cl->elems.count);
args[2] = lb_emit_source_code_location(p, proc_name, pos);
@@ -10463,8 +11235,7 @@ lbAddr lb_build_addr(lbProcedure *p, Ast *expr) {
if (cl->elems.count > 0) {
lb_addr_store(p, v, lb_const_value(p->module, type, exact_value_compound(expr)));
- auto temp_data = array_make<lbCompoundLitElemTempData>(heap_allocator(), 0, cl->elems.count);
- defer (array_free(&temp_data));
+ auto temp_data = array_make<lbCompoundLitElemTempData>(temporary_allocator(), 0, cl->elems.count);
// NOTE(bill): Separate value, gep, store into their own chunks
for_array(i, cl->elems) {
@@ -10563,8 +11334,7 @@ lbAddr lb_build_addr(lbProcedure *p, Ast *expr) {
if (cl->elems.count > 0) {
lb_addr_store(p, v, lb_const_value(p->module, type, exact_value_compound(expr)));
- auto temp_data = array_make<lbCompoundLitElemTempData>(heap_allocator(), 0, cl->elems.count);
- defer (array_free(&temp_data));
+ auto temp_data = array_make<lbCompoundLitElemTempData>(temporary_allocator(), 0, cl->elems.count);
// NOTE(bill): Separate value, gep, store into their own chunks
for_array(i, cl->elems) {
@@ -10672,8 +11442,7 @@ lbAddr lb_build_addr(lbProcedure *p, Ast *expr) {
lbValue data = lb_slice_elem(p, slice);
- auto temp_data = array_make<lbCompoundLitElemTempData>(heap_allocator(), 0, cl->elems.count);
- defer (array_free(&temp_data));
+ auto temp_data = array_make<lbCompoundLitElemTempData>(temporary_allocator(), 0, cl->elems.count);
for_array(i, cl->elems) {
Ast *elem = cl->elems[i];
@@ -10766,14 +11535,13 @@ lbAddr lb_build_addr(lbProcedure *p, Ast *expr) {
break;
}
Type *et = bt->DynamicArray.elem;
- gbAllocator a = heap_allocator();
lbValue size = lb_const_int(p->module, t_int, type_size_of(et));
lbValue align = lb_const_int(p->module, t_int, type_align_of(et));
i64 item_count = gb_max(cl->max_count, cl->elems.count);
{
- auto args = array_make<lbValue>(a, 5);
+ auto args = array_make<lbValue>(permanent_allocator(), 5);
args[0] = lb_emit_conv(p, lb_addr_get_ptr(p, v), t_rawptr);
args[1] = size;
args[2] = align;
@@ -10827,7 +11595,7 @@ lbAddr lb_build_addr(lbProcedure *p, Ast *expr) {
}
{
- auto args = array_make<lbValue>(a, 6);
+ auto args = array_make<lbValue>(permanent_allocator(), 6);
args[0] = lb_emit_conv(p, v.addr, t_rawptr);
args[1] = size;
args[2] = align;
@@ -10968,12 +11736,16 @@ void lb_init_module(lbModule *m, Checker *c) {
gb_mutex_init(&m->mutex);
gbAllocator a = heap_allocator();
map_init(&m->types, a);
+ map_init(&m->llvm_types, a);
map_init(&m->values, a);
string_map_init(&m->members, a);
map_init(&m->procedure_values, a);
string_map_init(&m->procedures, a);
string_map_init(&m->const_strings, a);
map_init(&m->anonymous_proc_lits, a);
+ map_init(&m->function_type_map, a);
+ map_init(&m->equal_procs, a);
+ map_init(&m->hasher_procs, a);
array_init(&m->procedures_to_generate, a);
array_init(&m->foreign_library_paths, a);
@@ -11038,7 +11810,7 @@ lbAddr lb_add_global_generated(lbModule *m, Type *type, lbValue value) {
type = default_type(type);
isize max_len = 7+8+1;
- u8 *str = cast(u8 *)gb_alloc_array(heap_allocator(), u8, max_len);
+ u8 *str = cast(u8 *)gb_alloc_array(permanent_allocator(), u8, max_len);
isize len = gb_snprintf(cast(char *)str, max_len, "ggv$%x", m->global_generated_index);
m->global_generated_index++;
String name = make_string(str, len-1);
@@ -11118,12 +11890,11 @@ lbValue lb_generate_local_array(lbProcedure *p, Type *elem_type, i64 count, bool
}
lbValue lb_generate_global_array(lbModule *m, Type *elem_type, i64 count, String prefix, i64 id) {
- gbAllocator a = heap_allocator();
Token token = {Token_Ident};
isize name_len = prefix.len + 1 + 20;
auto suffix_id = cast(unsigned long long)id;
- char *text = gb_alloc_array(a, char, name_len+1);
+ char *text = gb_alloc_array(permanent_allocator(), char, name_len+1);
gb_snprintf(text, name_len,
"%.*s-%llu", LIT(prefix), suffix_id);
text[name_len] = 0;
@@ -11145,7 +11916,6 @@ lbValue lb_generate_global_array(lbModule *m, Type *elem_type, i64 count, String
void lb_setup_type_info_data(lbProcedure *p) { // NOTE(bill): Setup type_info data
lbModule *m = p->module;
LLVMContextRef ctx = m->ctx;
- gbAllocator a = heap_allocator();
CheckerInfo *info = m->info;
{
@@ -11168,6 +11938,9 @@ void lb_setup_type_info_data(lbProcedure *p) { // NOTE(bill): Setup type_info da
// Useful types
Type *t_i64_slice_ptr = alloc_type_pointer(alloc_type_slice(t_i64));
Type *t_string_slice_ptr = alloc_type_pointer(alloc_type_slice(t_string));
+ Entity *type_info_flags_entity = find_core_entity(info->checker, str_lit("Type_Info_Flags"));
+ Type *t_type_info_flags = type_info_flags_entity->type;
+
i32 type_info_member_types_index = 0;
i32 type_info_member_names_index = 0;
@@ -11187,19 +11960,43 @@ void lb_setup_type_info_data(lbProcedure *p) { // NOTE(bill): Setup type_info da
lbValue tag = {};
lbValue ti_ptr = lb_emit_array_epi(p, lb_global_type_info_data.addr, cast(i32)entry_index);
- lbValue variant_ptr = lb_emit_struct_ep(p, ti_ptr, 3);
+ lbValue variant_ptr = lb_emit_struct_ep(p, ti_ptr, 4);
+
+ lbValue type_info_flags = lb_const_int(p->module, t_type_info_flags, type_info_flags_of_type(t));
lb_emit_store(p, lb_emit_struct_ep(p, ti_ptr, 0), lb_const_int(m, t_int, type_size_of(t)));
lb_emit_store(p, lb_emit_struct_ep(p, ti_ptr, 1), lb_const_int(m, t_int, type_align_of(t)));
- lb_emit_store(p, lb_emit_struct_ep(p, ti_ptr, 2), lb_typeid(m, t));
+ lb_emit_store(p, lb_emit_struct_ep(p, ti_ptr, 2), type_info_flags);
+ lb_emit_store(p, lb_emit_struct_ep(p, ti_ptr, 3), lb_typeid(m, t));
switch (t->kind) {
case Type_Named: {
tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_named_ptr);
- LLVMValueRef vals[2] = {
+
+ LLVMValueRef pkg_name = nullptr;
+ if (t->Named.type_name->pkg) {
+ pkg_name = lb_const_string(m, t->Named.type_name->pkg->name).value;
+ } else {
+ pkg_name = LLVMConstNull(lb_type(m, t_string));
+ }
+
+ String proc_name = {};
+ if (t->Named.type_name->parent_proc_decl) {
+ DeclInfo *decl = t->Named.type_name->parent_proc_decl;
+ if (decl->entity && decl->entity->kind == Entity_Procedure) {
+ proc_name = decl->entity->token.string;
+ }
+ }
+ TokenPos pos = t->Named.type_name->token.pos;
+
+ lbValue loc = lb_emit_source_code_location(p, proc_name, pos);
+
+ LLVMValueRef vals[4] = {
lb_const_string(p->module, t->Named.type_name->token.string).value,
lb_get_type_info_ptr(m, t->Named.base).value,
+ pkg_name,
+ loc.value
};
lbValue res = {};
@@ -11523,10 +12320,8 @@ void lb_setup_type_info_data(lbProcedure *p) { // NOTE(bill): Setup type_info da
str_lit("$enum_values"), cast(i64)entry_index);
- LLVMValueRef *name_values = gb_alloc_array(heap_allocator(), LLVMValueRef, fields.count);
- LLVMValueRef *value_values = gb_alloc_array(heap_allocator(), LLVMValueRef, fields.count);
- defer (gb_free(heap_allocator(), name_values));
- defer (gb_free(heap_allocator(), value_values));
+ LLVMValueRef *name_values = gb_alloc_array(temporary_allocator(), LLVMValueRef, fields.count);
+ LLVMValueRef *value_values = gb_alloc_array(temporary_allocator(), LLVMValueRef, fields.count);
GB_ASSERT(is_type_integer(t->Enum.base_type));
@@ -11610,7 +12405,7 @@ void lb_setup_type_info_data(lbProcedure *p) { // NOTE(bill): Setup type_info da
case Type_Struct: {
tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_struct_ptr);
- LLVMValueRef vals[11] = {};
+ LLVMValueRef vals[12] = {};
{
@@ -11620,18 +12415,22 @@ void lb_setup_type_info_data(lbProcedure *p) { // NOTE(bill): Setup type_info da
vals[5] = is_packed.value;
vals[6] = is_raw_union.value;
vals[7] = is_custom_align.value;
+ if (is_type_comparable(t) && !is_type_simple_compare(t)) {
+ vals[8] = lb_get_equal_proc_for_type(m, t).value;
+ }
+
if (t->Struct.soa_kind != StructSoa_None) {
- lbValue kind = lb_emit_struct_ep(p, tag, 8);
+ lbValue kind = lb_emit_struct_ep(p, tag, 9);
Type *kind_type = type_deref(kind.type);
lbValue soa_kind = lb_const_value(m, kind_type, exact_value_i64(t->Struct.soa_kind));
lbValue soa_type = lb_type_info(m, t->Struct.soa_elem);
lbValue soa_len = lb_const_int(m, t_int, t->Struct.soa_count);
- vals[8] = soa_kind.value;
- vals[9] = soa_type.value;
- vals[10] = soa_len.value;
+ vals[9] = soa_kind.value;
+ vals[10] = soa_type.value;
+ vals[11] = soa_len.value;
}
}
@@ -11703,10 +12502,12 @@ void lb_setup_type_info_data(lbProcedure *p) { // NOTE(bill): Setup type_info da
tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_map_ptr);
init_map_internal_types(t);
- LLVMValueRef vals[3] = {
+ LLVMValueRef vals[5] = {
lb_get_type_info_ptr(m, t->Map.key).value,
lb_get_type_info_ptr(m, t->Map.value).value,
lb_get_type_info_ptr(m, t->Map.generated_struct_type).value,
+ lb_get_equal_proc_for_type(m, t->Map.key).value,
+ lb_get_hasher_proc_for_type(m, t->Map.key).value
};
lbValue res = {};
@@ -11875,10 +12676,6 @@ void lb_generate_code(lbGenerator *gen) {
LLVMModuleRef mod = gen->module.mod;
CheckerInfo *info = gen->info;
- Arena temp_arena = {};
- arena_init(&temp_arena, heap_allocator());
- gbAllocator temp_allocator = arena_allocator(&temp_arena);
-
auto *min_dep_set = &info->minimum_dependency_set;
@@ -11891,8 +12688,8 @@ void lb_generate_code(lbGenerator *gen) {
LLVMInitializeNativeTarget();
- char const *target_triple = alloc_cstring(heap_allocator(), build_context.metrics.target_triplet);
- char const *target_data_layout = alloc_cstring(heap_allocator(), build_context.metrics.target_data_layout);
+ char const *target_triple = alloc_cstring(permanent_allocator(), build_context.metrics.target_triplet);
+ char const *target_data_layout = alloc_cstring(permanent_allocator(), build_context.metrics.target_data_layout);
LLVMSetTarget(mod, target_triple);
LLVMTargetRef target = {};
@@ -11914,7 +12711,7 @@ void lb_generate_code(lbGenerator *gen) {
if (build_context.microarch == "native") {
llvm_cpu = host_cpu_name;
} else {
- llvm_cpu = alloc_cstring(heap_allocator(), build_context.microarch);
+ llvm_cpu = alloc_cstring(permanent_allocator(), build_context.microarch);
}
if (gb_strcmp(llvm_cpu, host_cpu_name) == 0) {
llvm_features = LLVMGetHostCPUFeatures();
@@ -11957,6 +12754,10 @@ void lb_generate_code(lbGenerator *gen) {
1, "", 0,
LLVMDWARFEmissionFull, 0, true,
true
+ #if LLVM_VERSION_MAJOR > 10
+ , "", 0,
+ "", 0
+ #endif
);
}
@@ -12084,8 +12885,9 @@ void lb_generate_code(lbGenerator *gen) {
lbValue var;
lbValue init;
DeclInfo *decl;
+ bool is_initialized;
};
- auto global_variables = array_make<GlobalVariable>(heap_allocator(), 0, global_variable_max_count);
+ auto global_variables = array_make<GlobalVariable>(permanent_allocator(), 0, global_variable_max_count);
for_array(i, info->variable_init_order) {
DeclInfo *d = info->variable_init_order[i];
@@ -12112,7 +12914,7 @@ void lb_generate_code(lbGenerator *gen) {
lbValue g = {};
- g.value = LLVMAddGlobal(m->mod, lb_type(m, e->type), alloc_cstring(heap_allocator(), name));
+ g.value = LLVMAddGlobal(m->mod, lb_type(m, e->type), alloc_cstring(permanent_allocator(), name));
g.type = alloc_type_pointer(e->type);
if (e->Variable.thread_local_model != "") {
LLVMSetThreadLocal(g.value, true);
@@ -12146,15 +12948,22 @@ void lb_generate_code(lbGenerator *gen) {
var.var = g;
var.decl = decl;
- if (decl->init_expr != nullptr && !is_type_any(e->type)) {
+ if (decl->init_expr != nullptr) {
TypeAndValue tav = type_and_value_of_expr(decl->init_expr);
- if (tav.mode != Addressing_Invalid) {
- if (tav.value.kind != ExactValue_Invalid) {
- ExactValue v = tav.value;
- lbValue init = lb_const_value(m, tav.type, v);
- LLVMSetInitializer(g.value, init.value);
+ if (!is_type_any(e->type)) {
+ if (tav.mode != Addressing_Invalid) {
+ if (tav.value.kind != ExactValue_Invalid) {
+ ExactValue v = tav.value;
+ lbValue init = lb_const_value(m, tav.type, v);
+ LLVMSetInitializer(g.value, init.value);
+ var.is_initialized = true;
+ }
}
}
+ if (!var.is_initialized &&
+ (is_type_untyped_nil(tav.type) || is_type_untyped_undef(tav.type))) {
+ var.is_initialized = true;
+ }
}
array_add(&global_variables, var);
@@ -12166,9 +12975,6 @@ void lb_generate_code(lbGenerator *gen) {
TIME_SECTION("LLVM Global Procedures and Types");
for_array(i, info->entities) {
- // arena_free_all(&temp_arena);
- // gbAllocator a = temp_allocator;
-
Entity *e = info->entities[i];
String name = e->token.string;
DeclInfo *decl = e->decl_info;
@@ -12229,101 +13035,85 @@ void lb_generate_code(lbGenerator *gen) {
LLVMPassManagerRef default_function_pass_manager = LLVMCreateFunctionPassManagerForModule(mod);
defer (LLVMDisposePassManager(default_function_pass_manager));
- /*{
- LLVMAddMemCpyOptPass(default_function_pass_manager);
- LLVMAddPromoteMemoryToRegisterPass(default_function_pass_manager);
- LLVMAddMergedLoadStoreMotionPass(default_function_pass_manager);
- LLVMAddAggressiveInstCombinerPass(default_function_pass_manager);
- LLVMAddConstantPropagationPass(default_function_pass_manager);
- LLVMAddAggressiveDCEPass(default_function_pass_manager);
- LLVMAddMergedLoadStoreMotionPass(default_function_pass_manager);
- LLVMAddPromoteMemoryToRegisterPass(default_function_pass_manager);
- LLVMAddCFGSimplificationPass(default_function_pass_manager);
- // LLVMAddUnifyFunctionExitNodesPass(default_function_pass_manager);
-
- if (build_context.optimization_level >= 2) {
- LLVMAddAggressiveInstCombinerPass(default_function_pass_manager);
- LLVMAddEarlyCSEPass(default_function_pass_manager);
- LLVMAddEarlyCSEMemSSAPass(default_function_pass_manager);
- LLVMAddLowerExpectIntrinsicPass(default_function_pass_manager);
-
- LLVMAddAlignmentFromAssumptionsPass(default_function_pass_manager);
- LLVMAddLoopRotatePass(default_function_pass_manager);
- LLVMAddDeadStoreEliminationPass(default_function_pass_manager);
- LLVMAddScalarizerPass(default_function_pass_manager);
- LLVMAddReassociatePass(default_function_pass_manager);
- LLVMAddAddDiscriminatorsPass(default_function_pass_manager);
- LLVMAddPromoteMemoryToRegisterPass(default_function_pass_manager);
- LLVMAddCorrelatedValuePropagationPass(default_function_pass_manager);
-
- LLVMAddSLPVectorizePass(default_function_pass_manager);
- LLVMAddLoopVectorizePass(default_function_pass_manager);
-
- }
- }*/
- if (build_context.optimization_level == 0 && false) {
- auto dfpm = default_function_pass_manager;
-
- LLVMAddMemCpyOptPass(dfpm);
- LLVMAddPromoteMemoryToRegisterPass(dfpm);
- LLVMAddMergedLoadStoreMotionPass(dfpm);
- LLVMAddAggressiveInstCombinerPass(dfpm);
- LLVMAddConstantPropagationPass(dfpm);
- LLVMAddAggressiveDCEPass(dfpm);
- LLVMAddMergedLoadStoreMotionPass(dfpm);
- LLVMAddPromoteMemoryToRegisterPass(dfpm);
- LLVMAddCFGSimplificationPass(dfpm);
- LLVMAddScalarizerPass(dfpm);
- } else {
+ {
auto dfpm = default_function_pass_manager;
- LLVMAddMemCpyOptPass(dfpm);
- LLVMAddPromoteMemoryToRegisterPass(dfpm);
- LLVMAddMergedLoadStoreMotionPass(dfpm);
- LLVMAddAggressiveInstCombinerPass(dfpm);
- LLVMAddConstantPropagationPass(dfpm);
- LLVMAddAggressiveDCEPass(dfpm);
- LLVMAddMergedLoadStoreMotionPass(dfpm);
- LLVMAddPromoteMemoryToRegisterPass(dfpm);
- LLVMAddCFGSimplificationPass(dfpm);
-
+ if (build_context.optimization_level == 0) {
+ LLVMAddMemCpyOptPass(dfpm);
+ LLVMAddPromoteMemoryToRegisterPass(dfpm);
+ LLVMAddMergedLoadStoreMotionPass(dfpm);
+ LLVMAddAggressiveInstCombinerPass(dfpm);
+ LLVMAddEarlyCSEPass(dfpm);
+ LLVMAddEarlyCSEMemSSAPass(dfpm);
+ LLVMAddConstantPropagationPass(dfpm);
+ LLVMAddAggressiveDCEPass(dfpm);
+ LLVMAddMergedLoadStoreMotionPass(dfpm);
+ LLVMAddPromoteMemoryToRegisterPass(dfpm);
+ LLVMAddCFGSimplificationPass(dfpm);
+
+
+ // LLVMAddInstructionCombiningPass(dfpm);
+ LLVMAddSLPVectorizePass(dfpm);
+ LLVMAddLoopVectorizePass(dfpm);
+
+ LLVMAddScalarizerPass(dfpm);
+ LLVMAddLoopIdiomPass(dfpm);
+ } else {
+ bool do_extra_passes = true;
- // LLVMAddInstructionCombiningPass(dfpm);
- LLVMAddSLPVectorizePass(dfpm);
- LLVMAddLoopVectorizePass(dfpm);
- LLVMAddEarlyCSEPass(dfpm);
- LLVMAddEarlyCSEMemSSAPass(dfpm);
+ int repeat_count = cast(int)build_context.optimization_level;
+ do {
+ LLVMAddMemCpyOptPass(dfpm);
+ LLVMAddPromoteMemoryToRegisterPass(dfpm);
+ LLVMAddMergedLoadStoreMotionPass(dfpm);
+ LLVMAddAlignmentFromAssumptionsPass(dfpm);
+ LLVMAddEarlyCSEPass(dfpm);
+ LLVMAddEarlyCSEMemSSAPass(dfpm);
+ LLVMAddConstantPropagationPass(dfpm);
+ if (do_extra_passes) {
+ LLVMAddAggressiveInstCombinerPass(dfpm);
+ LLVMAddAggressiveDCEPass(dfpm);
+ }
+ LLVMAddMergedLoadStoreMotionPass(dfpm);
+ LLVMAddPromoteMemoryToRegisterPass(dfpm);
+ LLVMAddCFGSimplificationPass(dfpm);
+ LLVMAddTailCallEliminationPass(dfpm);
- LLVMAddScalarizerPass(dfpm);
- LLVMAddLoopIdiomPass(dfpm);
+ if (do_extra_passes) {
+ LLVMAddSLPVectorizePass(dfpm);
+ LLVMAddLoopVectorizePass(dfpm);
- // LLVMAddAggressiveInstCombinerPass(dfpm);
- // LLVMAddLowerExpectIntrinsicPass(dfpm);
+ LLVMAddConstantPropagationPass(dfpm);
+ LLVMAddScalarizerPass(dfpm);
+ LLVMAddLoopIdiomPass(dfpm);
- // LLVMAddPartiallyInlineLibCallsPass(dfpm);
+ LLVMAddAggressiveInstCombinerPass(dfpm);
+ LLVMAddLowerExpectIntrinsicPass(dfpm);
- // LLVMAddAlignmentFromAssumptionsPass(dfpm);
- // LLVMAddDeadStoreEliminationPass(dfpm);
- // LLVMAddReassociatePass(dfpm);
- // LLVMAddAddDiscriminatorsPass(dfpm);
- // LLVMAddPromoteMemoryToRegisterPass(dfpm);
- // LLVMAddCorrelatedValuePropagationPass(dfpm);
- // LLVMAddMemCpyOptPass(dfpm);
+ LLVMAddDeadStoreEliminationPass(dfpm);
+ LLVMAddReassociatePass(dfpm);
+ LLVMAddAddDiscriminatorsPass(dfpm);
+ LLVMAddPromoteMemoryToRegisterPass(dfpm);
+ LLVMAddCorrelatedValuePropagationPass(dfpm);
+ }
+ } while (repeat_count --> 0);
+ }
}
LLVMPassManagerRef default_function_pass_manager_without_memcpy = LLVMCreateFunctionPassManagerForModule(mod);
defer (LLVMDisposePassManager(default_function_pass_manager_without_memcpy));
{
- LLVMAddPromoteMemoryToRegisterPass(default_function_pass_manager_without_memcpy);
- LLVMAddMergedLoadStoreMotionPass(default_function_pass_manager_without_memcpy);
- LLVMAddAggressiveInstCombinerPass(default_function_pass_manager_without_memcpy);
- LLVMAddConstantPropagationPass(default_function_pass_manager_without_memcpy);
- LLVMAddAggressiveDCEPass(default_function_pass_manager_without_memcpy);
- LLVMAddMergedLoadStoreMotionPass(default_function_pass_manager_without_memcpy);
- LLVMAddPromoteMemoryToRegisterPass(default_function_pass_manager_without_memcpy);
- LLVMAddCFGSimplificationPass(default_function_pass_manager_without_memcpy);
- // LLVMAddUnifyFunctionExitNodesPass(default_function_pass_manager_without_memcpy);
+ auto dfpm = default_function_pass_manager_without_memcpy;
+ LLVMAddPromoteMemoryToRegisterPass(dfpm);
+ LLVMAddMergedLoadStoreMotionPass(dfpm);
+ LLVMAddAggressiveInstCombinerPass(dfpm);
+ LLVMAddConstantPropagationPass(dfpm);
+ LLVMAddAggressiveDCEPass(dfpm);
+ LLVMAddMergedLoadStoreMotionPass(dfpm);
+ LLVMAddPromoteMemoryToRegisterPass(dfpm);
+ LLVMAddCFGSimplificationPass(dfpm);
+ // LLVMAddUnifyFunctionExitNodesPass(dfpm);
}
TIME_SECTION("LLVM Runtime Creation");
@@ -12374,7 +13164,12 @@ void lb_generate_code(lbGenerator *gen) {
auto *var = &global_variables[i];
if (var->decl->init_expr != nullptr) {
lbValue init = lb_build_expr(p, var->decl->init_expr);
- if (!lb_is_const(init)) {
+ if (lb_is_const(init)) {
+ if (!var->is_initialized) {
+ LLVMSetInitializer(var->var.value, init.value);
+ var->is_initialized = true;
+ }
+ } else {
var->init = init;
}
}
@@ -12392,6 +13187,7 @@ void lb_generate_code(lbGenerator *gen) {
}
if (var->init.value != nullptr) {
+ GB_ASSERT(!var->is_initialized);
Type *t = type_deref(var->var.type);
if (is_type_any(t)) {
@@ -12408,6 +13204,8 @@ void lb_generate_code(lbGenerator *gen) {
} else {
lb_emit_store(p, var->var, lb_emit_conv(p, var->init, t));
}
+
+ var->is_initialized = true;
}
}
@@ -12453,12 +13251,12 @@ void lb_generate_code(lbGenerator *gen) {
if (build_context.metrics.os == TargetOs_windows && build_context.metrics.arch == TargetArch_386) {
name = str_lit("mainCRTStartup");
} else {
- array_init(&params->Tuple.variables, heap_allocator(), 2);
+ array_init(&params->Tuple.variables, permanent_allocator(), 2);
params->Tuple.variables[0] = alloc_entity_param(nullptr, make_token_ident("argc"), t_i32, false, true);
params->Tuple.variables[1] = alloc_entity_param(nullptr, make_token_ident("argv"), alloc_type_pointer(t_cstring), false, true);
}
- array_init(&results->Tuple.variables, heap_allocator(), 1);
+ array_init(&results->Tuple.variables, permanent_allocator(), 1);
results->Tuple.variables[0] = alloc_entity_param(nullptr, make_token_ident("_"), t_i32, false, true);
Type *proc_type = alloc_type_proc(nullptr,
@@ -12471,11 +13269,21 @@ void lb_generate_code(lbGenerator *gen) {
lb_begin_procedure_body(p);
- lbValue *found = map_get(&m->values, hash_entity(entry_point));
- GB_ASSERT(found != nullptr);
-
LLVMBuildCall2(p->builder, LLVMGetElementType(lb_type(m, startup_runtime->type)), startup_runtime->value, nullptr, 0, "");
- LLVMBuildCall2(p->builder, LLVMGetElementType(lb_type(m, found->type)), found->value, nullptr, 0, "");
+
+ if (build_context.command_kind == Command_test) {
+ for_array(i, m->info->testing_procedures) {
+ Entity *e = m->info->testing_procedures[i];
+ lbValue *found = map_get(&m->values, hash_entity(e));
+ GB_ASSERT(found != nullptr);
+ lb_emit_call(p, *found, {});
+ }
+ } else {
+ lbValue *found = map_get(&m->values, hash_entity(entry_point));
+ GB_ASSERT(found != nullptr);
+ LLVMBuildCall2(p->builder, LLVMGetElementType(lb_type(m, found->type)), found->value, nullptr, 0, "");
+ }
+
LLVMBuildRet(p->builder, LLVMConstInt(lb_type(m, t_i32), 0, false));
lb_end_procedure_body(p);
@@ -12490,6 +13298,9 @@ void lb_generate_code(lbGenerator *gen) {
LLVMRunFunctionPassManager(default_function_pass_manager, p->value);
}
+
+ String filepath_ll = concatenate_strings(permanent_allocator(), gen->output_base, STR_LIT(".ll"));
+
TIME_SECTION("LLVM Procedure Generation");
for_array(i, m->procedures_to_generate) {
lbProcedure *p = m->procedures_to_generate[i];
@@ -12520,7 +13331,11 @@ void lb_generate_code(lbGenerator *gen) {
gb_printf_err("LLVM CODE GEN FAILED FOR PROCEDURE: %.*s\n", LIT(p->name));
LLVMDumpValue(p->value);
gb_printf_err("\n\n\n\n");
- LLVMVerifyFunction(p->value, LLVMAbortProcessAction);
+ if (LLVMPrintModuleToFile(mod, cast(char const *)filepath_ll.text, &llvm_error)) {
+ gb_printf_err("LLVM Error: %s\n", llvm_error);
+ }
+ LLVMVerifyFunction(p->value, LLVMPrintMessageAction);
+ gb_exit(1);
}
}
@@ -12541,6 +13356,15 @@ void lb_generate_code(lbGenerator *gen) {
}
}
+ for_array(i, m->equal_procs.entries) {
+ lbProcedure *p = m->equal_procs.entries[i].value;
+ LLVMRunFunctionPassManager(default_function_pass_manager, p->value);
+ }
+ for_array(i, m->hasher_procs.entries) {
+ lbProcedure *p = m->hasher_procs.entries[i].value;
+ LLVMRunFunctionPassManager(default_function_pass_manager, p->value);
+ }
+
TIME_SECTION("LLVM Module Pass");
@@ -12567,35 +13391,41 @@ void lb_generate_code(lbGenerator *gen) {
llvm_error = nullptr;
defer (LLVMDisposeMessage(llvm_error));
- String filepath_ll = concatenate_strings(heap_allocator(), gen->output_base, STR_LIT(".ll"));
- defer (gb_free(heap_allocator(), filepath_ll.text));
-
String filepath_obj = {};
LLVMCodeGenFileType code_gen_file_type = LLVMObjectFile;
if (build_context.build_mode == BuildMode_Assembly) {
- filepath_obj = concatenate_strings(heap_allocator(), gen->output_base, STR_LIT(".S"));
+ filepath_obj = concatenate_strings(permanent_allocator(), gen->output_base, STR_LIT(".S"));
code_gen_file_type = LLVMAssemblyFile;
} else {
switch (build_context.metrics.os) {
case TargetOs_windows:
- filepath_obj = concatenate_strings(heap_allocator(), gen->output_base, STR_LIT(".obj"));
+ filepath_obj = concatenate_strings(permanent_allocator(), gen->output_base, STR_LIT(".obj"));
break;
case TargetOs_darwin:
case TargetOs_linux:
case TargetOs_essence:
- filepath_obj = concatenate_strings(heap_allocator(), gen->output_base, STR_LIT(".o"));
+ filepath_obj = concatenate_strings(permanent_allocator(), gen->output_base, STR_LIT(".o"));
break;
case TargetOs_js:
- filepath_obj = concatenate_strings(heap_allocator(), gen->output_base, STR_LIT(".wasm-obj"));
+ filepath_obj = concatenate_strings(permanent_allocator(), gen->output_base, STR_LIT(".wasm-obj"));
break;
}
}
LLVMDIBuilderFinalize(m->debug_builder);
- if (LLVMVerifyModule(mod, LLVMAbortProcessAction, &llvm_error)) {
- gb_printf_err("LLVM Error: %s\n", llvm_error);
+ if (LLVMVerifyModule(mod, LLVMReturnStatusAction, &llvm_error)) {
+ gb_printf_err("LLVM Error:\n%s\n", llvm_error);
+ if (build_context.keep_temp_files) {
+ TIME_SECTION("LLVM Print Module to File");
+ if (LLVMPrintModuleToFile(mod, cast(char const *)filepath_ll.text, &llvm_error)) {
+ gb_printf_err("LLVM Error: %s\n", llvm_error);
+ gb_exit(1);
+ return;
+ }
+ }
+ gb_exit(1);
return;
}
llvm_error = nullptr;
@@ -12603,6 +13433,7 @@ void lb_generate_code(lbGenerator *gen) {
TIME_SECTION("LLVM Print Module to File");
if (LLVMPrintModuleToFile(mod, cast(char const *)filepath_ll.text, &llvm_error)) {
gb_printf_err("LLVM Error: %s\n", llvm_error);
+ gb_exit(1);
return;
}
}
diff --git a/src/llvm_backend.hpp b/src/llvm_backend.hpp
index 23b2c7654..34a5dfacf 100644
--- a/src/llvm_backend.hpp
+++ b/src/llvm_backend.hpp
@@ -1,3 +1,5 @@
+#if defined(LLVM_BACKEND_SUPPORT)
+#if defined(GB_SYSTEM_WINDOWS)
#include "llvm-c/Core.h"
#include "llvm-c/ExecutionEngine.h"
#include "llvm-c/Target.h"
@@ -12,6 +14,23 @@
#include "llvm-c/Transforms/Scalar.h"
#include "llvm-c/Transforms/Utils.h"
#include "llvm-c/Transforms/Vectorize.h"
+#else
+#include <llvm-c/Core.h>
+#include <llvm-c/ExecutionEngine.h>
+#include <llvm-c/Target.h>
+#include <llvm-c/Analysis.h>
+#include <llvm-c/Object.h>
+#include <llvm-c/BitWriter.h>
+#include <llvm-c/DebugInfo.h>
+#include <llvm-c/Transforms/AggressiveInstCombine.h>
+#include <llvm-c/Transforms/InstCombine.h>
+#include <llvm-c/Transforms/IPO.h>
+#include <llvm-c/Transforms/PassManagerBuilder.h>
+#include <llvm-c/Transforms/Scalar.h>
+#include <llvm-c/Transforms/Utils.h>
+#include <llvm-c/Transforms/Vectorize.h>
+#endif
+#endif
struct lbProcedure;
@@ -74,6 +93,8 @@ struct lbModule {
gbMutex mutex;
Map<LLVMTypeRef> types; // Key: Type *
+ Map<Type *> llvm_types; // Key: LLVMTypeRef
+ i32 internal_type_level;
Map<lbValue> values; // Key: Entity *
StringMap<lbValue> members;
@@ -83,6 +104,10 @@ struct lbModule {
StringMap<LLVMValueRef> const_strings;
Map<lbProcedure *> anonymous_proc_lits; // Key: Ast *
+ Map<struct lbFunctionType *> function_type_map; // Key: Type *
+
+ Map<lbProcedure *> equal_procs; // Key: Type *
+ Map<lbProcedure *> hasher_procs; // Key: Type *
u32 global_array_index;
u32 global_generated_index;
@@ -199,6 +224,7 @@ struct lbProcedure {
bool is_entry_point;
bool is_startup;
+ lbFunctionType *abi_function_type;
LLVMValueRef value;
LLVMBuilderRef builder;
@@ -301,7 +327,7 @@ lbAddr lb_add_local(lbProcedure *p, Type *type, Entity *e=nullptr, bool zero_ini
void lb_add_foreign_library_path(lbModule *m, Entity *e);
-lbValue lb_typeid(lbModule *m, Type *type, Type *typeid_type=t_typeid);
+lbValue lb_typeid(lbModule *m, Type *type);
lbValue lb_address_from_load_or_generate_local(lbProcedure *p, lbValue value);
lbValue lb_address_from_load(lbProcedure *p, lbValue value);
@@ -344,7 +370,7 @@ String lb_get_const_string(lbModule *m, lbValue value);
lbValue lb_generate_local_array(lbProcedure *p, Type *elem_type, i64 count, bool zero_init=true);
lbValue lb_generate_global_array(lbModule *m, Type *elem_type, i64 count, String prefix, i64 id);
lbValue lb_gen_map_header(lbProcedure *p, lbValue map_val_ptr, Type *map_type);
-lbValue lb_gen_map_key(lbProcedure *p, lbValue key, Type *key_type);
+lbValue lb_gen_map_hash(lbProcedure *p, lbValue key, Type *key_type);
void lb_insert_dynamic_map_key_and_value(lbProcedure *p, lbAddr addr, Type *map_type, lbValue map_key, lbValue map_value, Ast *node);
@@ -354,6 +380,9 @@ lbValue lb_emit_source_code_location(lbProcedure *p, String const &procedure, To
lbValue lb_handle_param_value(lbProcedure *p, Type *parameter_type, ParameterValue const &param_value, TokenPos const &pos);
+lbValue lb_get_equal_proc_for_type(lbModule *m, Type *type);
+lbValue lb_get_hasher_proc_for_type(lbModule *m, Type *type);
+lbValue lb_emit_conv(lbProcedure *p, lbValue value, Type *t);
#define LB_STARTUP_RUNTIME_PROC_NAME "__$startup_runtime"
#define LB_STARTUP_TYPE_INFO_PROC_NAME "__$startup_type_info"
diff --git a/src/main.cpp b/src/main.cpp
index 95491d6b2..b55908aef 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -11,15 +11,19 @@
gb_global Timings global_timings = {0};
#if defined(LLVM_BACKEND_SUPPORT)
+#if defined(GB_SYSTEM_WINDOWS)
#include "llvm-c/Types.h"
+#else
+#include <llvm-c/Types.h>
+#endif
#endif
#include "parser.hpp"
#include "checker.hpp"
#include "parser.cpp"
-#include "docs.cpp"
#include "checker.cpp"
+#include "docs.cpp"
#if defined(LLVM_BACKEND_SUPPORT)
@@ -85,6 +89,10 @@ i32 system_exec_command_line_app(char const *name, char const *fmt, ...) {
exit_code = -1;
}
+ if (exit_code) {
+ exit(exit_code);
+ }
+
return exit_code;
#elif defined(GB_SYSTEM_OSX) || defined(GB_SYSTEM_UNIX)
@@ -133,6 +141,10 @@ i32 system_exec_command_line_app(char const *name, char const *fmt, ...) {
// exit_code = status;
+ if (exit_code) {
+ exit(exit_code);
+ }
+
return exit_code;
#endif
}
@@ -141,29 +153,23 @@ i32 system_exec_command_line_app(char const *name, char const *fmt, ...) {
#if defined(LLVM_BACKEND_SUPPORT)
-i32 linker_stage(lbGenerator *gen) {
- i32 exit_code = 0;
-
+void linker_stage(lbGenerator *gen) {
Timings *timings = &global_timings;
String output_base = gen->output_base;
if (build_context.metrics.os == TargetOs_js) {
timings_start_section(timings, str_lit("wasm-ld"));
- exit_code = system_exec_command_line_app("wasm-ld",
+ system_exec_command_line_app("wasm-ld",
"\"%.*s\\bin\\wasm-ld\" \"%.*s.wasm-obj\" -o \"%.*s.wasm\" %.*s %.*s",
LIT(build_context.ODIN_ROOT),
LIT(output_base), LIT(output_base), LIT(build_context.link_flags), LIT(build_context.extra_linker_flags));
- if (exit_code != 0) {
- return exit_code;
- }
- return exit_code;
}
if (build_context.cross_compiling && selected_target_metrics->metrics == &target_essence_amd64) {
#ifdef GB_SYSTEM_UNIX
system_exec_command_line_app("linker", "x86_64-essence-gcc \"%.*s.o\" -o \"%.*s\" %.*s %.*s",
- LIT(output_base), LIT(output_base), LIT(build_context.link_flags), LIT(build_context.extra_linker_flags));
+ LIT(output_base), LIT(output_base), LIT(build_context.link_flags), LIT(build_context.extra_linker_flags));
#else
gb_printf_err("Linking for cross compilation for this platform is not yet supported (%.*s %.*s)\n",
LIT(target_os_names[build_context.metrics.os]),
@@ -194,7 +200,7 @@ i32 linker_stage(lbGenerator *gen) {
if (find_result.windows_sdk_version == 0) {
gb_printf_err("Windows SDK not found.\n");
- return 1;
+ exit(1);
}
if (build_context.ignore_microsoft_magic) {
@@ -259,17 +265,13 @@ i32 linker_stage(lbGenerator *gen) {
char const *subsystem_str = build_context.use_subsystem_windows ? "WINDOWS" : "CONSOLE";
if (!build_context.use_lld) { // msvc
if (build_context.has_resource) {
- exit_code = system_exec_command_line_app("msvc-link",
+ system_exec_command_line_app("msvc-link",
"\"rc.exe\" /nologo /fo \"%.*s.res\" \"%.*s.rc\"",
LIT(output_base),
LIT(build_context.resource_filepath)
);
- if (exit_code != 0) {
- return exit_code;
- }
-
- exit_code = system_exec_command_line_app("msvc-link",
+ system_exec_command_line_app("msvc-link",
"\"%.*slink.exe\" %s \"%.*s.res\" -OUT:\"%.*s.%s\" %s "
"/nologo /incremental:no /opt:ref /subsystem:%s "
" %.*s "
@@ -284,7 +286,7 @@ i32 linker_stage(lbGenerator *gen) {
lib_str
);
} else {
- exit_code = system_exec_command_line_app("msvc-link",
+ system_exec_command_line_app("msvc-link",
"\"%.*slink.exe\" %s -OUT:\"%.*s.%s\" %s "
"/nologo /incremental:no /opt:ref /subsystem:%s "
" %.*s "
@@ -300,7 +302,7 @@ i32 linker_stage(lbGenerator *gen) {
);
}
} else { // lld
- exit_code = system_exec_command_line_app("msvc-link",
+ system_exec_command_line_app("msvc-link",
"\"%.*s\\bin\\lld-link\" %s -OUT:\"%.*s.%s\" %s "
"/nologo /incremental:no /opt:ref /subsystem:%s "
" %.*s "
@@ -341,11 +343,11 @@ i32 linker_stage(lbGenerator *gen) {
String lib_name = lib;
lib_name = remove_extension_from_path(lib_name);
lib_str = gb_string_append_fmt(lib_str, " -framework %.*s ", LIT(lib_name));
- } else if (string_ends_with(lib, str_lit(".a"))) {
- // static libs, absolute full path relative to the file in which the lib was imported from
- lib_str = gb_string_append_fmt(lib_str, " %.*s ", LIT(lib));
- } else if (string_ends_with(lib, str_lit(".dylib"))) {
+ } else if (string_ends_with(lib, str_lit(".a")) || string_ends_with(lib, str_lit(".o")) || string_ends_with(lib, str_lit(".dylib"))) {
+ // For:
+ // object
// dynamic lib
+ // static libs, absolute full path relative to the file in which the lib was imported from
lib_str = gb_string_append_fmt(lib_str, " %.*s ", LIT(lib));
} else {
// dynamic or static system lib, just link regularly searching system library paths
@@ -382,20 +384,33 @@ i32 linker_stage(lbGenerator *gen) {
// Unlike the Win32 linker code, the output_ext includes the dot, because
// typically executable files on *NIX systems don't have extensions.
String output_ext = {};
- char const *link_settings = "";
+ gbString link_settings = gb_string_make_reserve(heap_allocator(), 32);
char const *linker;
if (build_context.build_mode == BuildMode_DynamicLibrary) {
+ // NOTE(tetra, 2020-11-06): __$startup_runtime must be called at DLL load time.
+ // Clang, for some reason, won't let us pass the '-init' flag that lets us do this,
+ // so use ld instead.
+ // :UseLDForShared
+ linker = "ld";
+ link_settings = gb_string_appendc(link_settings, "-init '__$startup_runtime' ");
// Shared libraries are .dylib on MacOS and .so on Linux.
#if defined(GB_SYSTEM_OSX)
output_ext = STR_LIT(".dylib");
- link_settings = "-dylib -dynamic";
+ link_settings = gb_string_appendc(link_settings, "-dylib -dynamic ");
#else
output_ext = STR_LIT(".so");
- link_settings = "-shared";
+ link_settings = gb_string_appendc(link_settings, "-shared ");
#endif
} else {
- // TODO: Do I need anything here?
- link_settings = "";
+ #if defined(GB_SYSTEM_OSX)
+ linker = "ld";
+ #else
+ // TODO(zangent): Figure out how to make ld work on Linux.
+ // It probably has to do with including the entire CRT, but
+ // that's quite a complicated issue to solve while remaining distro-agnostic.
+ // Clang can figure out linker flags for us, and that's good enough _for now_.
+ linker = "clang -Wno-unused-command-line-argument";
+ #endif
}
if (build_context.out_filepath.len > 0) {
@@ -406,17 +421,7 @@ i32 linker_stage(lbGenerator *gen) {
}
}
- #if defined(GB_SYSTEM_OSX)
- linker = "ld";
- #else
- // TODO(zangent): Figure out how to make ld work on Linux.
- // It probably has to do with including the entire CRT, but
- // that's quite a complicated issue to solve while remaining distro-agnostic.
- // Clang can figure out linker flags for us, and that's good enough _for now_.
- linker = "clang -Wno-unused-command-line-argument";
- #endif
-
- exit_code = system_exec_command_line_app("ld-link",
+ system_exec_command_line_app("ld-link",
"%s %s -o \"%.*s%.*s\" %s "
" %s "
" %.*s "
@@ -431,37 +436,30 @@ i32 linker_stage(lbGenerator *gen) {
" -e _main "
#endif
, linker, object_files, LIT(output_base), LIT(output_ext),
- lib_str,
- #if defined(GB_SYSTEM_OSX)
- "-lSystem -lm -syslibroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk",
- #else
- "-lc -lm",
- #endif
+ #if defined(GB_SYSTEM_OSX)
+ "-lSystem -lm -syslibroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -L/usr/local/lib",
+ #else
+ "-lc -lm",
+ #endif
+ lib_str,
LIT(build_context.link_flags),
LIT(build_context.extra_linker_flags),
link_settings);
- if (exit_code != 0) {
- return exit_code;
- }
#if defined(GB_SYSTEM_OSX)
if (build_context.ODIN_DEBUG) {
// NOTE: macOS links DWARF symbols dynamically. Dsymutil will map the stubs in the exe
// to the symbols in the object file
- exit_code = system_exec_command_line_app("dsymutil",
+ system_exec_command_line_app("dsymutil",
"dsymutil %.*s%.*s", LIT(output_base), LIT(output_ext)
);
-
- if (exit_code != 0) {
- return exit_code;
- }
}
#endif
#endif
}
- return exit_code;
+ return;
}
#endif
@@ -519,7 +517,7 @@ void usage(String argv0) {
print_usage_line(1, "run same as 'build', but also then runs the newly compiled executable.");
print_usage_line(1, "check parse and type check .odin file");
print_usage_line(1, "query parse, type check, and output a .json file containing information about the program");
- print_usage_line(1, "docs generate documentation for a .odin file");
+ print_usage_line(1, "doc generate documentation .odin file, or directory of .odin files");
print_usage_line(1, "version print version");
print_usage_line(0, "");
print_usage_line(0, "For more information of flags, apply the flag to see what is possible");
@@ -565,6 +563,8 @@ enum BuildFlagKind {
BuildFlag_OutFile,
BuildFlag_OptimizationLevel,
BuildFlag_ShowTimings,
+ BuildFlag_ShowUnused,
+ BuildFlag_ShowUnusedWithLocation,
BuildFlag_ShowMoreTimings,
BuildFlag_ShowSystemCalls,
BuildFlag_ThreadCount,
@@ -578,6 +578,7 @@ enum BuildFlagKind {
BuildFlag_NoBoundsCheck,
BuildFlag_NoDynamicLiterals,
BuildFlag_NoCRT,
+ BuildFlag_NoEntryPoint,
BuildFlag_UseLLD,
BuildFlag_Vet,
BuildFlag_UseLLVMApi,
@@ -593,6 +594,9 @@ enum BuildFlagKind {
BuildFlag_GlobalDefinitions,
BuildFlag_GoToDefinitions,
+ BuildFlag_Short,
+ BuildFlag_AllPackages,
+
#if defined(GB_SYSTEM_WINDOWS)
BuildFlag_IgnoreVsSearch,
BuildFlag_ResourceFile,
@@ -618,10 +622,13 @@ struct BuildFlag {
BuildFlagKind kind;
String name;
BuildFlagParamKind param_kind;
+ u32 command_support;
+ bool allow_mulitple;
};
-void add_flag(Array<BuildFlag> *build_flags, BuildFlagKind kind, String name, BuildFlagParamKind param_kind) {
- BuildFlag flag = {kind, name, param_kind};
+
+void add_flag(Array<BuildFlag> *build_flags, BuildFlagKind kind, String name, BuildFlagParamKind param_kind, u32 command_support, bool allow_mulitple=false) {
+ BuildFlag flag = {kind, name, param_kind, command_support, allow_mulitple};
array_add(build_flags, flag);
}
@@ -662,44 +669,50 @@ ExactValue build_param_to_exact_value(String name, String param) {
bool parse_build_flags(Array<String> args) {
auto build_flags = array_make<BuildFlag>(heap_allocator(), 0, BuildFlag_COUNT);
- add_flag(&build_flags, BuildFlag_Help, str_lit("help"), BuildFlagParam_None);
- add_flag(&build_flags, BuildFlag_OutFile, str_lit("out"), BuildFlagParam_String);
- add_flag(&build_flags, BuildFlag_OptimizationLevel, str_lit("opt"), BuildFlagParam_Integer);
- add_flag(&build_flags, BuildFlag_ShowTimings, str_lit("show-timings"), BuildFlagParam_None);
- add_flag(&build_flags, BuildFlag_ShowMoreTimings, str_lit("show-more-timings"), BuildFlagParam_None);
- add_flag(&build_flags, BuildFlag_ShowSystemCalls, str_lit("show-system-calls"), BuildFlagParam_None);
- add_flag(&build_flags, BuildFlag_ThreadCount, str_lit("thread-count"), BuildFlagParam_Integer);
- add_flag(&build_flags, BuildFlag_KeepTempFiles, str_lit("keep-temp-files"), BuildFlagParam_None);
- add_flag(&build_flags, BuildFlag_Collection, str_lit("collection"), BuildFlagParam_String);
- add_flag(&build_flags, BuildFlag_Define, str_lit("define"), BuildFlagParam_String);
- add_flag(&build_flags, BuildFlag_BuildMode, str_lit("build-mode"), BuildFlagParam_String);
- add_flag(&build_flags, BuildFlag_Target, str_lit("target"), BuildFlagParam_String);
- add_flag(&build_flags, BuildFlag_Debug, str_lit("debug"), BuildFlagParam_None);
- add_flag(&build_flags, BuildFlag_DisableAssert, str_lit("disable-assert"), BuildFlagParam_None);
- add_flag(&build_flags, BuildFlag_NoBoundsCheck, str_lit("no-bounds-check"), BuildFlagParam_None);
- add_flag(&build_flags, BuildFlag_NoDynamicLiterals, str_lit("no-dynamic-literals"), BuildFlagParam_None);
- add_flag(&build_flags, BuildFlag_NoCRT, str_lit("no-crt"), BuildFlagParam_None);
- add_flag(&build_flags, BuildFlag_UseLLD, str_lit("lld"), BuildFlagParam_None);
- add_flag(&build_flags, BuildFlag_Vet, str_lit("vet"), BuildFlagParam_None);
- add_flag(&build_flags, BuildFlag_UseLLVMApi, str_lit("llvm-api"), BuildFlagParam_None);
- add_flag(&build_flags, BuildFlag_IgnoreUnknownAttributes, str_lit("ignore-unknown-attributes"), BuildFlagParam_None);
- add_flag(&build_flags, BuildFlag_ExtraLinkerFlags, str_lit("extra-linker-flags"), BuildFlagParam_String);
- add_flag(&build_flags, BuildFlag_Microarch, str_lit("microarch"), BuildFlagParam_String);
-
- add_flag(&build_flags, BuildFlag_DisallowDo, str_lit("disallow-do"), BuildFlagParam_None);
- add_flag(&build_flags, BuildFlag_DefaultToNilAllocator, str_lit("default-to-nil-allocator"), BuildFlagParam_None);
- add_flag(&build_flags, BuildFlag_InsertSemicolon, str_lit("insert-semicolon"), BuildFlagParam_None);
-
- add_flag(&build_flags, BuildFlag_Compact, str_lit("compact"), BuildFlagParam_None);
- add_flag(&build_flags, BuildFlag_GlobalDefinitions, str_lit("global-definitions"), BuildFlagParam_None);
- add_flag(&build_flags, BuildFlag_GoToDefinitions, str_lit("go-to-definitions"), BuildFlagParam_None);
+ add_flag(&build_flags, BuildFlag_Help, str_lit("help"), BuildFlagParam_None, Command_all);
+ add_flag(&build_flags, BuildFlag_OutFile, str_lit("out"), BuildFlagParam_String, Command__does_build &~ Command_test);
+ add_flag(&build_flags, BuildFlag_OptimizationLevel, str_lit("opt"), BuildFlagParam_Integer, Command__does_build);
+ add_flag(&build_flags, BuildFlag_ShowTimings, str_lit("show-timings"), BuildFlagParam_None, Command__does_check);
+ add_flag(&build_flags, BuildFlag_ShowMoreTimings, str_lit("show-more-timings"), BuildFlagParam_None, Command__does_check);
+ add_flag(&build_flags, BuildFlag_ShowUnused, str_lit("show-unused"), BuildFlagParam_None, Command_check);
+ add_flag(&build_flags, BuildFlag_ShowUnusedWithLocation, str_lit("show-unused-with-location"), BuildFlagParam_None, Command_check);
+ add_flag(&build_flags, BuildFlag_ShowSystemCalls, str_lit("show-system-calls"), BuildFlagParam_None, Command_all);
+ add_flag(&build_flags, BuildFlag_ThreadCount, str_lit("thread-count"), BuildFlagParam_Integer, Command_all);
+ add_flag(&build_flags, BuildFlag_KeepTempFiles, str_lit("keep-temp-files"), BuildFlagParam_None, Command__does_build);
+ add_flag(&build_flags, BuildFlag_Collection, str_lit("collection"), BuildFlagParam_String, Command__does_check);
+ add_flag(&build_flags, BuildFlag_Define, str_lit("define"), BuildFlagParam_String, Command__does_check, true);
+ add_flag(&build_flags, BuildFlag_BuildMode, str_lit("build-mode"), BuildFlagParam_String, Command__does_build); // Commands_build is not used to allow for a better error message
+ add_flag(&build_flags, BuildFlag_Target, str_lit("target"), BuildFlagParam_String, Command__does_build);
+ add_flag(&build_flags, BuildFlag_Debug, str_lit("debug"), BuildFlagParam_None, Command__does_check);
+ add_flag(&build_flags, BuildFlag_DisableAssert, str_lit("disable-assert"), BuildFlagParam_None, Command__does_check);
+ add_flag(&build_flags, BuildFlag_NoBoundsCheck, str_lit("no-bounds-check"), BuildFlagParam_None, Command__does_check);
+ add_flag(&build_flags, BuildFlag_NoDynamicLiterals, str_lit("no-dynamic-literals"), BuildFlagParam_None, Command__does_check);
+ add_flag(&build_flags, BuildFlag_NoCRT, str_lit("no-crt"), BuildFlagParam_None, Command__does_build);
+ add_flag(&build_flags, BuildFlag_NoEntryPoint, str_lit("no-entry-point"), BuildFlagParam_None, Command__does_check &~ Command_test);
+ add_flag(&build_flags, BuildFlag_UseLLD, str_lit("lld"), BuildFlagParam_None, Command__does_build);
+ add_flag(&build_flags, BuildFlag_Vet, str_lit("vet"), BuildFlagParam_None, Command__does_check);
+ add_flag(&build_flags, BuildFlag_UseLLVMApi, str_lit("llvm-api"), BuildFlagParam_None, Command__does_build);
+ add_flag(&build_flags, BuildFlag_IgnoreUnknownAttributes, str_lit("ignore-unknown-attributes"), BuildFlagParam_None, Command__does_check);
+ add_flag(&build_flags, BuildFlag_ExtraLinkerFlags, str_lit("extra-linker-flags"), BuildFlagParam_String, Command__does_build);
+ add_flag(&build_flags, BuildFlag_Microarch, str_lit("microarch"), BuildFlagParam_String, Command__does_build);
+
+ add_flag(&build_flags, BuildFlag_DisallowDo, str_lit("disallow-do"), BuildFlagParam_None, Command__does_check);
+ add_flag(&build_flags, BuildFlag_DefaultToNilAllocator, str_lit("default-to-nil-allocator"), BuildFlagParam_None, Command__does_check);
+ add_flag(&build_flags, BuildFlag_InsertSemicolon, str_lit("insert-semicolon"), BuildFlagParam_None);
+ add_flag(&build_flags, BuildFlag_Compact, str_lit("compact"), BuildFlagParam_None, Command_query);
+ add_flag(&build_flags, BuildFlag_GlobalDefinitions, str_lit("global-definitions"), BuildFlagParam_None, Command_query);
+ add_flag(&build_flags, BuildFlag_GoToDefinitions, str_lit("go-to-definitions"), BuildFlagParam_None, Command_query);
+
+ add_flag(&build_flags, BuildFlag_Short, str_lit("short"), BuildFlagParam_None, Command_doc);
+ add_flag(&build_flags, BuildFlag_AllPackages, str_lit("all-packages"), BuildFlagParam_None, Command_doc);
+
#if defined(GB_SYSTEM_WINDOWS)
- add_flag(&build_flags, BuildFlag_IgnoreVsSearch, str_lit("ignore-vs-search"), BuildFlagParam_None);
- add_flag(&build_flags, BuildFlag_ResourceFile, str_lit("resource"), BuildFlagParam_String);
- add_flag(&build_flags, BuildFlag_WindowsPdbName, str_lit("pdb-name"), BuildFlagParam_String);
- add_flag(&build_flags, BuildFlag_Subsystem, str_lit("subsystem"), BuildFlagParam_String);
+ add_flag(&build_flags, BuildFlag_IgnoreVsSearch, str_lit("ignore-vs-search"), BuildFlagParam_None, Command__does_build);
+ add_flag(&build_flags, BuildFlag_ResourceFile, str_lit("resource"), BuildFlagParam_String, Command__does_build);
+ add_flag(&build_flags, BuildFlag_WindowsPdbName, str_lit("pdb-name"), BuildFlagParam_String, Command__does_build);
+ add_flag(&build_flags, BuildFlag_Subsystem, str_lit("subsystem"), BuildFlagParam_String, Command__does_build);
#endif
GB_ASSERT(args.count >= 3);
@@ -729,11 +742,19 @@ bool parse_build_flags(Array<String> args) {
String param = {};
if (end < flag.len-1) param = substring(flag, 2+end, flag.len);
+ bool is_supported = true;
bool found = false;
+ BuildFlag found_bf = {};
for_array(build_flag_index, build_flags) {
BuildFlag bf = build_flags[build_flag_index];
if (bf.name == name) {
found = true;
+ found_bf = bf;
+ if ((bf.command_support & build_context.command_kind) == 0) {
+ is_supported = false;
+ break;
+ }
+
if (set_flags[bf.kind]) {
gb_printf_err("Previous flag set: '%.*s'\n", LIT(name));
bad_flags = true;
@@ -835,7 +856,7 @@ bool parse_build_flags(Array<String> args) {
GB_ASSERT(value.kind == ExactValue_String);
String path = value.value_string;
path = string_trim_whitespace(path);
- if (is_import_path_valid(path)) {
+ if (is_build_flag_path_valid(path)) {
#if defined(GB_SYSTEM_WINDOWS)
String ext = path_extension(path);
if (ext == ".exe") {
@@ -857,6 +878,15 @@ bool parse_build_flags(Array<String> args) {
GB_ASSERT(value.kind == ExactValue_Invalid);
build_context.show_timings = true;
break;
+ case BuildFlag_ShowUnused:
+ GB_ASSERT(value.kind == ExactValue_Invalid);
+ build_context.show_unused = true;
+ break;
+ case BuildFlag_ShowUnusedWithLocation:
+ GB_ASSERT(value.kind == ExactValue_Invalid);
+ build_context.show_unused = true;
+ build_context.show_unused_with_location = true;
+ break;
case BuildFlag_ShowMoreTimings:
GB_ASSERT(value.kind == ExactValue_Invalid);
build_context.show_timings = true;
@@ -1094,6 +1124,10 @@ bool parse_build_flags(Array<String> args) {
build_context.no_crt = true;
break;
+ case BuildFlag_NoEntryPoint:
+ build_context.no_entry_point = true;
+ break;
+
case BuildFlag_UseLLD:
build_context.use_lld = true;
break;
@@ -1165,6 +1199,15 @@ bool parse_build_flags(Array<String> args) {
}
break;
+ case BuildFlag_Short:
+ build_context.cmd_doc_flags |= CmdDocFlag_Short;
+ break;
+ case BuildFlag_AllPackages:
+ build_context.cmd_doc_flags |= CmdDocFlag_AllPackages;
+ break;
+
+
+
#if defined(GB_SYSTEM_WINDOWS)
case BuildFlag_IgnoreVsSearch:
GB_ASSERT(value.kind == ExactValue_Invalid);
@@ -1175,7 +1218,7 @@ bool parse_build_flags(Array<String> args) {
GB_ASSERT(value.kind == ExactValue_String);
String path = value.value_string;
path = string_trim_whitespace(path);
- if (is_import_path_valid(path)) {
+ if (is_build_flag_path_valid(path)) {
if(!string_ends_with(path, str_lit(".rc"))) {
gb_printf_err("Invalid -resource path %.*s, missing .rc\n", LIT(path));
bad_flags = true;
@@ -1193,7 +1236,7 @@ bool parse_build_flags(Array<String> args) {
GB_ASSERT(value.kind == ExactValue_String);
String path = value.value_string;
path = string_trim_whitespace(path);
- if (is_import_path_valid(path)) {
+ if (is_build_flag_path_valid(path)) {
// #if defined(GB_SYSTEM_WINDOWS)
// String ext = path_extension(path);
// if (ext != ".pdb") {
@@ -1228,26 +1271,35 @@ bool parse_build_flags(Array<String> args) {
}
}
-
- switch (bf.kind) {
- case BuildFlag_Define:
- // Allow for multiple
- break;
- default:
+ if (!bf.allow_mulitple) {
set_flags[bf.kind] = ok;
- break;
}
}
break;
}
}
- if (!found) {
+ if (found && !is_supported) {
+ gb_printf_err("Unknown flag for 'odin %.*s': '%.*s'\n", LIT(build_context.command), LIT(name));
+ gb_printf_err("'%.*s' is supported with the following commands:\n", LIT(name));
+ gb_printf_err("\t");
+ i32 count = 0;
+ for (u32 i = 0; i < 32; i++) {
+ if (found_bf.command_support & (1<<i)) {
+ if (count > 0) {
+ gb_printf_err(", ");
+ }
+ gb_printf_err("%s", odin_command_strings[i]);
+ count += 1;
+ }
+ }
+ gb_printf_err("\n");
+ bad_flags = true;
+ } else if (!found) {
gb_printf_err("Unknown flag: '%.*s'\n", LIT(name));
bad_flags = true;
}
}
-
if (build_context.query_data_set_settings.ok) {
if (build_context.query_data_set_settings.kind == QueryDataSet_Invalid) {
gb_printf_err("'odin query' requires a flag determining the kind of query data set to be returned\n");
@@ -1474,22 +1526,39 @@ void print_show_help(String const arg0, String const &command) {
print_usage_line(1, "run same as 'build', but also then runs the newly compiled executable.");
} else if (command == "check") {
print_usage_line(1, "check parse and type check .odin file");
+ } else if (command == "test") {
+ print_usage_line(1, "test build ands runs 'test_*' procedures in the initial package");
} else if (command == "query") {
print_usage_line(1, "query [experimental] parse, type check, and output a .json file containing information about the program");
- } else if (command == "docs") {
- print_usage_line(1, "docs generate documentation for a .odin file");
+ } else if (command == "doc") {
+ print_usage_line(1, "doc generate documentation from a .odin file, or directory of .odin files");
+ print_usage_line(2, "Examples:");
+ print_usage_line(3, "odin doc core/path");
+ print_usage_line(3, "odin doc core/path core/path/filepath");
} else if (command == "version") {
print_usage_line(1, "version print version");
}
+ bool doc = command == "doc";
bool build = command == "build";
- bool run_or_build = command == "run" || command == "build";
- bool check = command == "run" || command == "build" || command == "check";
+ bool run_or_build = command == "run" || command == "build" || command == "test";
+ bool check_only = command == "check";
+ bool check = run_or_build || command == "check";
print_usage_line(0, "");
print_usage_line(1, "Flags");
print_usage_line(0, "");
+ if (doc) {
+ print_usage_line(1, "-short");
+ print_usage_line(2, "Show shortened documentation for the packages");
+ print_usage_line(0, "");
+
+ print_usage_line(1, "-all-packages");
+ print_usage_line(2, "Generates documentation for all packages used in the current project");
+ print_usage_line(0, "");
+ }
+
if (run_or_build) {
print_usage_line(1, "-out:<filepath>");
print_usage_line(2, "Set the file name of the outputted executable");
@@ -1518,6 +1587,15 @@ void print_show_help(String const arg0, String const &command) {
print_usage_line(0, "");
}
+ if (check_only) {
+ print_usage_line(1, "-show-unused");
+ print_usage_line(2, "Shows unused package declarations within the current project");
+ print_usage_line(0, "");
+ print_usage_line(1, "-show-unused-with-location");
+ print_usage_line(2, "Shows unused package declarations within the current project with the declarations source location");
+ print_usage_line(0, "");
+ }
+
if (run_or_build) {
print_usage_line(1, "-keep-temp-files");
print_usage_line(2, "Keeps the temporary files generated during compilation");
@@ -1596,6 +1674,23 @@ void print_show_help(String const arg0, String const &command) {
print_usage_line(1, "-extra-linker-flags:<string>");
print_usage_line(2, "Adds extra linker specific flags in a string");
print_usage_line(0, "");
+
+ print_usage_line(1, "-microarch:<string>");
+ print_usage_line(2, "Specifies the specific micro-architecture for the build in a string");
+ print_usage_line(2, "Examples:");
+ print_usage_line(3, "-microarch:sandybridge");
+ print_usage_line(3, "-microarch:native");
+ print_usage_line(0, "");
+ }
+
+ if (check) {
+ print_usage_line(1, "-disallow-do");
+ print_usage_line(2, "Disallows the 'do' keyword in the project");
+ print_usage_line(0, "");
+
+ print_usage_line(1, "-default-to-nil-allocator");
+ print_usage_line(2, "Sets the default allocator to be the nil_allocator, an allocator which does nothing");
+ print_usage_line(0, "");
}
if (run_or_build) {
@@ -1629,6 +1724,80 @@ void print_show_help(String const arg0, String const &command) {
}
}
+void print_show_unused(Checker *c) {
+ CheckerInfo *info = &c->info;
+
+ auto unused = array_make<Entity *>(permanent_allocator(), 0, info->entities.count);
+ for_array(i, info->entities) {
+ Entity *e = info->entities[i];
+ if (e == nullptr) {
+ continue;
+ }
+ if (e->pkg == nullptr || e->pkg->scope == nullptr) {
+ continue;
+ }
+ if (e->pkg->scope->flags & ScopeFlag_Builtin) {
+ continue;
+ }
+ switch (e->kind) {
+ case Entity_Invalid:
+ case Entity_Builtin:
+ case Entity_Nil:
+ case Entity_Label:
+ continue;
+ case Entity_Constant:
+ case Entity_Variable:
+ case Entity_TypeName:
+ case Entity_Procedure:
+ case Entity_ProcGroup:
+ case Entity_ImportName:
+ case Entity_LibraryName:
+ // Fine
+ break;
+ }
+ if ((e->scope->flags & (ScopeFlag_Pkg|ScopeFlag_File)) == 0) {
+ continue;
+ }
+ if (e->token.string.len == 0) {
+ continue;
+ }
+ if (e->token.string == "_") {
+ continue;
+ }
+ if (ptr_set_exists(&info->minimum_dependency_set, e)) {
+ continue;
+ }
+ array_add(&unused, e);
+ }
+
+ gb_sort_array(unused.data, unused.count, cmp_entities_for_printing);
+
+ print_usage_line(0, "Unused Package Declarations");
+
+ AstPackage *curr_pkg = nullptr;
+ EntityKind curr_entity_kind = Entity_Invalid;
+ for_array(i, unused) {
+ Entity *e = unused[i];
+ if (curr_pkg != e->pkg) {
+ curr_pkg = e->pkg;
+ curr_entity_kind = Entity_Invalid;
+ print_usage_line(0, "");
+ print_usage_line(0, "package %.*s", LIT(curr_pkg->name));
+ }
+ if (curr_entity_kind != e->kind) {
+ curr_entity_kind = e->kind;
+ print_usage_line(1, "%s", print_entity_names[e->kind]);
+ }
+ if (build_context.show_unused_with_location) {
+ TokenPos pos = e->token.pos;
+ print_usage_line(2, "%.*s(%td:%td) %.*s", LIT(pos.file), pos.line, pos.column, LIT(e->token.string));
+ } else {
+ print_usage_line(2, "%.*s", LIT(e->token.string));
+ }
+ }
+ print_usage_line(0, "");
+}
+
int main(int arg_count, char const **arg_ptr) {
if (arg_count < 2) {
usage(make_string_c(arg_ptr[0]));
@@ -1640,18 +1809,24 @@ int main(int arg_count, char const **arg_ptr) {
timings_init(timings, str_lit("Total Time"), 128);
defer (timings_destroy(timings));
+ arena_init(&permanent_arena, heap_allocator());
+ temp_allocator_init(&temporary_allocator_data, 16*1024*1024);
+ arena_init(&global_ast_arena, heap_allocator());
+ permanent_arena.use_mutex = true;
+
init_string_buffer_memory();
init_string_interner();
init_global_error_collector();
init_keyword_hash_table();
global_big_int_init();
- arena_init(&global_ast_arena, heap_allocator());
array_init(&library_collections, heap_allocator());
// NOTE(bill): 'core' cannot be (re)defined by the user
add_library_collection(str_lit("core"), get_fullpath_relative(heap_allocator(), odin_root_dir(), str_lit("core")));
map_init(&build_context.defined_values, heap_allocator());
+ build_context.extra_packages.allocator = heap_allocator();
+
Array<String> args = setup_args(arg_count, arg_ptr);
@@ -1660,11 +1835,15 @@ int main(int arg_count, char const **arg_ptr) {
String run_args_string = {};
bool run_output = false;
- if (command == "run") {
+ if (command == "run" || command == "test") {
if (args.count < 3) {
usage(args[0]);
return 1;
}
+ build_context.command_kind = Command_run;
+ if (command == "test") {
+ build_context.command_kind = Command_test;
+ }
Array<String> run_args = array_make<String>(heap_allocator(), 0, arg_count);
defer (array_free(&run_args));
@@ -1684,17 +1863,20 @@ int main(int arg_count, char const **arg_ptr) {
run_args_string = string_join_and_quote(heap_allocator(), run_args);
init_filename = args[2];
run_output = true;
+
} else if (command == "build") {
if (args.count < 3) {
usage(args[0]);
return 1;
}
+ build_context.command_kind = Command_build;
init_filename = args[2];
} else if (command == "check") {
if (args.count < 3) {
usage(args[0]);
return 1;
}
+ build_context.command_kind = Command_check;
build_context.no_output_files = true;
init_filename = args[2];
} else if (command == "query") {
@@ -1702,22 +1884,41 @@ int main(int arg_count, char const **arg_ptr) {
usage(args[0]);
return 1;
}
+ build_context.command_kind = Command_query;
build_context.no_output_files = true;
build_context.query_data_set_settings.ok = true;
init_filename = args[2];
- } else if (command == "docs") {
+ } else if (command == "doc") {
if (args.count < 3) {
usage(args[0]);
return 1;
}
+ build_context.command_kind = Command_doc;
init_filename = args[2];
+ for (isize i = 3; i < args.count; i++) {
+ auto arg = args[i];
+ if (string_starts_with(arg, str_lit("-"))) {
+ break;
+ }
+ array_add(&build_context.extra_packages, arg);
+ }
+ isize extra_count = build_context.extra_packages.count;
+ if (extra_count > 0) {
+ gb_memmove(args.data + 3, args.data + 3 + extra_count, extra_count * gb_size_of(*args.data));
+ args.count -= extra_count;
+ }
+
+
+ build_context.no_output_files = true;
build_context.generate_docs = true;
- #if 1
+ build_context.no_entry_point = true; // ignore entry point
+ #if 0
print_usage_line(0, "Documentation generation is not yet supported");
return 1;
#endif
} else if (command == "version") {
+ build_context.command_kind = Command_version;
gb_printf("%.*s version %.*s", LIT(args[0]), LIT(ODIN_VERSION));
#ifdef NIGHTLY
@@ -1792,10 +1993,8 @@ int main(int arg_count, char const **arg_ptr) {
return 1;
}
- if (build_context.generate_docs) {
- // generate_documentation(&parser);
- return 0;
- }
+ temp_allocator_free_all(&temporary_allocator_data);
+
timings_start_section(timings, str_lit("type check"));
Checker checker = {0};
@@ -1809,8 +2008,21 @@ int main(int arg_count, char const **arg_ptr) {
check_parsed_files(&checker);
}
+ temp_allocator_free_all(&temporary_allocator_data);
+
+ if (build_context.generate_docs) {
+ if (global_error_collector.count != 0) {
+ return 1;
+ }
+ generate_documentation(&checker);
+ return 0;
+ }
if (build_context.no_output_files) {
+ if (build_context.show_unused) {
+ print_show_unused(&checker);
+ }
+
if (build_context.query_data_set_settings.ok) {
generate_and_print_query_data(&checker, timings);
} else {
@@ -1839,15 +2051,12 @@ int main(int arg_count, char const **arg_ptr) {
}
lb_generate_code(&gen);
+ temp_allocator_free_all(&temporary_allocator_data);
+
switch (build_context.build_mode) {
case BuildMode_Executable:
case BuildMode_DynamicLibrary:
- {
- i32 linker_stage_exit_count = linker_stage(&gen);
- if (linker_stage_exit_count != 0) {
- return linker_stage_exit_count;
- }
- }
+ linker_stage(&gen);
break;
}
@@ -1864,9 +2073,6 @@ int main(int arg_count, char const **arg_ptr) {
SIZE_T virtual_mem_used_by_me = pmc.PrivateUsage;
gb_printf_err("virtual_memory_used: %tu B\n", virtual_mem_used_by_me);
- gb_printf_err("total_allocated_node_memory: %lld B\n", total_allocated_node_memory.value);
- gb_printf_err("total_subtype_node_memory_test: %lld B\n", total_subtype_node_memory_test.value);
- gb_printf_err("fraction: %.6f\n", (f64)total_subtype_node_memory_test.value/(f64)total_allocated_node_memory.value);
Parser *p = checker.parser;
isize lines = p->total_line_count;
isize tokens = p->total_token_count;
@@ -1916,31 +2122,29 @@ int main(int arg_count, char const **arg_ptr) {
timings_start_section(timings, str_lit("llvm ir gen"));
ir_gen_tree(&ir_gen);
+ temp_allocator_free_all(&temporary_allocator_data);
+
timings_start_section(timings, str_lit("llvm ir opt tree"));
ir_opt_tree(&ir_gen);
+ temp_allocator_free_all(&temporary_allocator_data);
+
timings_start_section(timings, str_lit("llvm ir print"));
print_llvm_ir(&ir_gen);
+ temp_allocator_free_all(&temporary_allocator_data);
+
String output_name = ir_gen.output_name;
String output_base = ir_gen.output_base;
build_context.optimization_level = gb_clamp(build_context.optimization_level, 0, 3);
- i32 exit_code = 0;
-
timings_start_section(timings, str_lit("llvm-opt"));
- exit_code = exec_llvm_opt(output_base);
- if (exit_code != 0) {
- return exit_code;
- }
+ exec_llvm_opt(output_base);
timings_start_section(timings, str_lit("llvm-llc"));
- exit_code = exec_llvm_llc(output_base);
- if (exit_code != 0) {
- return exit_code;
- }
+ exec_llvm_llc(output_base);
if (build_context.build_mode == BuildMode_Object) {
// Ignore the linker
@@ -1949,7 +2153,7 @@ int main(int arg_count, char const **arg_ptr) {
}
remove_temp_files(output_base);
- return exit_code;
+ return 0;
}
if (build_context.cross_compiling && selected_target_metrics->metrics == &target_essence_amd64) {
@@ -2046,17 +2250,13 @@ int main(int arg_count, char const **arg_ptr) {
if (!build_context.use_lld) { // msvc
if (build_context.has_resource) {
- exit_code = system_exec_command_line_app("msvc-link",
+ system_exec_command_line_app("msvc-link",
"\"rc.exe\" /nologo /fo \"%.*s.res\" \"%.*s.rc\"",
LIT(output_base),
LIT(build_context.resource_filepath)
);
- if (exit_code != 0) {
- return exit_code;
- }
-
- exit_code = system_exec_command_line_app("msvc-link",
+ system_exec_command_line_app("msvc-link",
"\"%.*slink.exe\" \"%.*s.obj\" \"%.*s.res\" -OUT:\"%.*s.%s\" %s "
"/nologo /incremental:no /opt:ref /subsystem:%s "
" %.*s "
@@ -2071,7 +2271,7 @@ int main(int arg_count, char const **arg_ptr) {
lib_str
);
} else {
- exit_code = system_exec_command_line_app("msvc-link",
+ system_exec_command_line_app("msvc-link",
"\"%.*slink.exe\" \"%.*s.obj\" -OUT:\"%.*s.%s\" %s "
"/nologo /incremental:no /opt:ref /subsystem:%s "
" %.*s "
@@ -2087,7 +2287,7 @@ int main(int arg_count, char const **arg_ptr) {
);
}
} else { // lld
- exit_code = system_exec_command_line_app("msvc-link",
+ system_exec_command_line_app("msvc-link",
"\"%.*s\\bin\\lld-link\" \"%.*s.obj\" -OUT:\"%.*s.%s\" %s "
"/nologo /incremental:no /opt:ref /subsystem:%s "
" %.*s "
@@ -2104,10 +2304,6 @@ int main(int arg_count, char const **arg_ptr) {
);
}
- if (exit_code != 0) {
- return exit_code;
- }
-
if (build_context.show_timings) {
show_timings(&checker, timings);
}
@@ -2142,13 +2338,14 @@ int main(int arg_count, char const **arg_ptr) {
String lib_name = lib;
lib_name = remove_extension_from_path(lib_name);
lib_str = gb_string_append_fmt(lib_str, " -framework %.*s ", LIT(lib_name));
- } else if (string_ends_with(lib, str_lit(".a"))) {
- // static libs, absolute full path relative to the file in which the lib was imported from
- lib_str = gb_string_append_fmt(lib_str, " %.*s ", LIT(lib));
- } else if (string_ends_with(lib, str_lit(".dylib"))) {
- // dynamic lib
- lib_str = gb_string_append_fmt(lib_str, " %.*s ", LIT(lib));
- } else {
+
+ } else if (string_ends_with(lib, str_lit(".a")) || string_ends_with(lib, str_lit(".o")) || string_ends_with(lib, str_lit(".dylib"))) {
+ // For:
+ // object
+ // dynamic lib
+ // static libs, absolute full path relative to the file in which the lib was imported from
+ lib_str = gb_string_append_fmt(lib_str, " %.*s ", LIT(lib));
+ } else {
// dynamic or static system lib, just link regularly searching system library paths
lib_str = gb_string_append_fmt(lib_str, " -l%.*s ", LIT(lib));
}
@@ -2176,22 +2373,36 @@ int main(int arg_count, char const **arg_ptr) {
// Unlike the Win32 linker code, the output_ext includes the dot, because
// typically executable files on *NIX systems don't have extensions.
String output_ext = {};
- char const *link_settings = "";
+ gbString link_settings = gb_string_make_reserve(heap_allocator(), 32);
char const *linker;
if (build_context.build_mode == BuildMode_DynamicLibrary) {
+ // NOTE(tetra, 2020-11-06): __$startup_runtime must be called at DLL load time.
+ // Clang, for some reason, won't let us pass the '-init' flag that lets us do this,
+ // so use ld instead.
+ // :UseLDForShared
+ linker = "ld";
+ link_settings = gb_string_appendc(link_settings, "-init '__$startup_runtime' ");
// Shared libraries are .dylib on MacOS and .so on Linux.
#if defined(GB_SYSTEM_OSX)
output_ext = STR_LIT(".dylib");
- link_settings = "-dylib -dynamic";
+ link_settings = gb_string_appendc(link_settings, "-dylib -dynamic ");
#else
output_ext = STR_LIT(".so");
- link_settings = "-shared";
+ link_settings = gb_string_appendc(link_settings, "-shared ");
#endif
} else {
- // TODO: Do I need anything here?
- link_settings = "";
+ #if defined(GB_SYSTEM_OSX)
+ linker = "ld";
+ #else
+ // TODO(zangent): Figure out how to make ld work on Linux.
+ // It probably has to do with including the entire CRT, but
+ // that's quite a complicated issue to solve while remaining distro-agnostic.
+ // Clang can figure out linker flags for us, and that's good enough _for now_.
+ linker = "clang -Wno-unused-command-line-argument";
+ #endif
}
+
if (build_context.out_filepath.len > 0) {
//NOTE(thebirk): We have a custom -out arguments, so we should use the extension from that
isize pos = string_extension_position(build_context.out_filepath);
@@ -2200,17 +2411,8 @@ int main(int arg_count, char const **arg_ptr) {
}
}
- #if defined(GB_SYSTEM_OSX)
- linker = "ld";
- #else
- // TODO(zangent): Figure out how to make ld work on Linux.
- // It probably has to do with including the entire CRT, but
- // that's quite a complicated issue to solve while remaining distro-agnostic.
- // Clang can figure out linker flags for us, and that's good enough _for now_.
- linker = "clang -Wno-unused-command-line-argument";
- #endif
- exit_code = system_exec_command_line_app("ld-link",
+ system_exec_command_line_app("ld-link",
"%s \"%.*s.o\" -o \"%.*s%.*s\" %s "
" %s "
" %.*s "
@@ -2226,29 +2428,22 @@ int main(int arg_count, char const **arg_ptr) {
#endif
, linker, LIT(output_base), LIT(output_base), LIT(output_ext),
lib_str,
- #if defined(GB_SYSTEM_OSX)
- "-lSystem -lm -syslibroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk",
- #else
- "-lc -lm",
- #endif
+ #if defined(GB_SYSTEM_OSX)
+ "-lSystem -lm -syslibroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -L/usr/local/lib",
+ #else
+ "-lc -lm",
+ #endif
LIT(build_context.link_flags),
LIT(build_context.extra_linker_flags),
link_settings);
- if (exit_code != 0) {
- return exit_code;
- }
#if defined(GB_SYSTEM_OSX)
if (build_context.ODIN_DEBUG) {
// NOTE: macOS links DWARF symbols dynamically. Dsymutil will map the stubs in the exe
// to the symbols in the object file
- exit_code = system_exec_command_line_app("dsymutil",
+ system_exec_command_line_app("dsymutil",
"dsymutil %.*s%.*s", LIT(output_base), LIT(output_ext)
);
-
- if (exit_code != 0) {
- return exit_code;
- }
}
#endif
diff --git a/src/parser.cpp b/src/parser.cpp
index b89d9f9f5..46af47c2d 100644
--- a/src/parser.cpp
+++ b/src/parser.cpp
@@ -108,8 +108,25 @@ Token ast_token(Ast *node) {
return empty_token;
}
+
+isize ast_node_size(AstKind kind) {
+ return align_formula_isize(gb_size_of(AstCommonStuff) + ast_variant_sizes[kind], gb_align_of(void *));
+
+}
+// NOTE(bill): And this below is why is I/we need a new language! Discriminated unions are a pain in C/C++
+Ast *alloc_ast_node(AstFile *f, AstKind kind) {
+ gbAllocator a = ast_allocator(f);
+
+ isize size = ast_node_size(kind);
+
+ Ast *node = cast(Ast *)gb_alloc(a, size);
+ node->kind = kind;
+ node->file = f;
+ return node;
+}
+
Ast *clone_ast(Ast *node);
-Array<Ast *> clone_ast_array(Array<Ast *> array) {
+Array<Ast *> clone_ast_array(Array<Ast *> const &array) {
Array<Ast *> result = {};
if (array.count > 0) {
result = array_make<Ast *>(ast_allocator(nullptr), array.count);
@@ -119,13 +136,23 @@ Array<Ast *> clone_ast_array(Array<Ast *> array) {
}
return result;
}
+Slice<Ast *> clone_ast_array(Slice<Ast *> const &array) {
+ Slice<Ast *> result = {};
+ if (array.count > 0) {
+ result = slice_clone(permanent_allocator(), array);
+ for_array(i, array) {
+ result[i] = clone_ast(array[i]);
+ }
+ }
+ return result;
+}
Ast *clone_ast(Ast *node) {
if (node == nullptr) {
return nullptr;
}
Ast *n = alloc_ast_node(node->file, node->kind);
- gb_memmove(n, node, gb_size_of(Ast));
+ gb_memmove(n, node, ast_node_size(node->kind));
switch (n->kind) {
default: GB_PANIC("Unhandled Ast %.*s", LIT(ast_strings[n->kind])); break;
@@ -463,23 +490,6 @@ bool ast_node_expect(Ast *node, AstKind kind) {
return true;
}
-
-gb_global gbAtomic64 total_allocated_node_memory = {0};
-gb_global gbAtomic64 total_subtype_node_memory_test = {0};
-
-// NOTE(bill): And this below is why is I/we need a new language! Discriminated unions are a pain in C/C++
-Ast *alloc_ast_node(AstFile *f, AstKind kind) {
- gbAllocator a = ast_allocator(f);
-
- gb_atomic64_fetch_add(&total_allocated_node_memory, cast(i64)(gb_size_of(Ast)));
- gb_atomic64_fetch_add(&total_subtype_node_memory_test, cast(i64)(gb_size_of(AstCommonStuff) + ast_variant_sizes[kind]));
-
- Ast *node = gb_alloc_item(a, Ast);
- node->kind = kind;
- node->file = f;
- return node;
-}
-
Ast *ast_bad_expr(AstFile *f, Token begin, Token end) {
Ast *result = alloc_ast_node(f, Ast_BadExpr);
result->BadExpr.begin = begin;
@@ -537,10 +547,10 @@ Ast *ast_paren_expr(AstFile *f, Ast *expr, Token open, Token close) {
return result;
}
-Ast *ast_call_expr(AstFile *f, Ast *proc, Array<Ast *> args, Token open, Token close, Token ellipsis) {
+Ast *ast_call_expr(AstFile *f, Ast *proc, Array<Ast *> const &args, Token open, Token close, Token ellipsis) {
Ast *result = alloc_ast_node(f, Ast_CallExpr);
result->CallExpr.proc = proc;
- result->CallExpr.args = args;
+ result->CallExpr.args = slice_from_array(args);
result->CallExpr.open = open;
result->CallExpr.close = close;
result->CallExpr.ellipsis = ellipsis;
@@ -624,7 +634,8 @@ Ast *ast_undef(AstFile *f, Token token) {
Ast *ast_basic_lit(AstFile *f, Token basic_lit) {
Ast *result = alloc_ast_node(f, Ast_BasicLit);
result->BasicLit.token = basic_lit;
- result->BasicLit.value = exact_value_from_basic_literal(basic_lit);
+ result->tav.mode = Addressing_Constant;
+ result->tav.value = exact_value_from_basic_literal(basic_lit);
return result;
}
@@ -643,12 +654,12 @@ Ast *ast_ellipsis(AstFile *f, Token token, Ast *expr) {
}
-Ast *ast_proc_group(AstFile *f, Token token, Token open, Token close, Array<Ast *> args) {
+Ast *ast_proc_group(AstFile *f, Token token, Token open, Token close, Array<Ast *> const &args) {
Ast *result = alloc_ast_node(f, Ast_ProcGroup);
result->ProcGroup.token = token;
result->ProcGroup.open = open;
result->ProcGroup.close = close;
- result->ProcGroup.args = args;
+ result->ProcGroup.args = slice_from_array(args);
return result;
}
@@ -658,7 +669,7 @@ Ast *ast_proc_lit(AstFile *f, Ast *type, Ast *body, u64 tags, Token where_token,
result->ProcLit.body = body;
result->ProcLit.tags = tags;
result->ProcLit.where_token = where_token;
- result->ProcLit.where_clauses = where_clauses;
+ result->ProcLit.where_clauses = slice_from_array(where_clauses);
return result;
}
@@ -670,10 +681,10 @@ Ast *ast_field_value(AstFile *f, Ast *field, Ast *value, Token eq) {
return result;
}
-Ast *ast_compound_lit(AstFile *f, Ast *type, Array<Ast *> elems, Token open, Token close) {
+Ast *ast_compound_lit(AstFile *f, Ast *type, Array<Ast *> const &elems, Token open, Token close) {
Ast *result = alloc_ast_node(f, Ast_CompoundLit);
result->CompoundLit.type = type;
- result->CompoundLit.elems = elems;
+ result->CompoundLit.elems = slice_from_array(elems);
result->CompoundLit.open = open;
result->CompoundLit.close = close;
return result;
@@ -736,7 +747,7 @@ Ast *ast_inline_asm_expr(AstFile *f, Token token, Token open, Token close,
result->InlineAsmExpr.token = token;
result->InlineAsmExpr.open = open;
result->InlineAsmExpr.close = close;
- result->InlineAsmExpr.param_types = param_types;
+ result->InlineAsmExpr.param_types = slice_from_array(param_types);
result->InlineAsmExpr.return_type = return_type;
result->InlineAsmExpr.asm_string = asm_string;
result->InlineAsmExpr.constraints_string = constraints_string;
@@ -768,18 +779,18 @@ Ast *ast_expr_stmt(AstFile *f, Ast *expr) {
return result;
}
-Ast *ast_assign_stmt(AstFile *f, Token op, Array<Ast *> lhs, Array<Ast *> rhs) {
+Ast *ast_assign_stmt(AstFile *f, Token op, Array<Ast *> const &lhs, Array<Ast *> const &rhs) {
Ast *result = alloc_ast_node(f, Ast_AssignStmt);
result->AssignStmt.op = op;
- result->AssignStmt.lhs = lhs;
- result->AssignStmt.rhs = rhs;
+ result->AssignStmt.lhs = slice_from_array(lhs);
+ result->AssignStmt.rhs = slice_from_array(rhs);
return result;
}
-Ast *ast_block_stmt(AstFile *f, Array<Ast *> stmts, Token open, Token close) {
+Ast *ast_block_stmt(AstFile *f, Array<Ast *> const &stmts, Token open, Token close) {
Ast *result = alloc_ast_node(f, Ast_BlockStmt);
- result->BlockStmt.stmts = stmts;
+ result->BlockStmt.stmts = slice_from_array(stmts);
result->BlockStmt.open = open;
result->BlockStmt.close = close;
return result;
@@ -805,10 +816,10 @@ Ast *ast_when_stmt(AstFile *f, Token token, Ast *cond, Ast *body, Ast *else_stmt
}
-Ast *ast_return_stmt(AstFile *f, Token token, Array<Ast *> results) {
+Ast *ast_return_stmt(AstFile *f, Token token, Array<Ast *> const &results) {
Ast *result = alloc_ast_node(f, Ast_ReturnStmt);
result->ReturnStmt.token = token;
- result->ReturnStmt.results = results;
+ result->ReturnStmt.results = slice_from_array(results);
return result;
}
@@ -866,11 +877,11 @@ Ast *ast_type_switch_stmt(AstFile *f, Token token, Ast *tag, Ast *body) {
return result;
}
-Ast *ast_case_clause(AstFile *f, Token token, Array<Ast *> list, Array<Ast *> stmts) {
+Ast *ast_case_clause(AstFile *f, Token token, Array<Ast *> const &list, Array<Ast *> const &stmts) {
Ast *result = alloc_ast_node(f, Ast_CaseClause);
result->CaseClause.token = token;
- result->CaseClause.list = list;
- result->CaseClause.stmts = stmts;
+ result->CaseClause.list = slice_from_array(list);
+ result->CaseClause.stmts = slice_from_array(stmts);
return result;
}
@@ -889,10 +900,10 @@ Ast *ast_branch_stmt(AstFile *f, Token token, Ast *label) {
return result;
}
-Ast *ast_using_stmt(AstFile *f, Token token, Array<Ast *> list) {
+Ast *ast_using_stmt(AstFile *f, Token token, Array<Ast *> const &list) {
Ast *result = alloc_ast_node(f, Ast_UsingStmt);
result->UsingStmt.token = token;
- result->UsingStmt.list = list;
+ result->UsingStmt.list = slice_from_array(list);
return result;
}
@@ -905,10 +916,10 @@ Ast *ast_bad_decl(AstFile *f, Token begin, Token end) {
return result;
}
-Ast *ast_field(AstFile *f, Array<Ast *> names, Ast *type, Ast *default_value, u32 flags, Token tag,
+Ast *ast_field(AstFile *f, Array<Ast *> const &names, Ast *type, Ast *default_value, u32 flags, Token tag,
CommentGroup *docs, CommentGroup *comment) {
Ast *result = alloc_ast_node(f, Ast_Field);
- result->Field.names = names;
+ result->Field.names = slice_from_array(names);
result->Field.type = type;
result->Field.default_value = default_value;
result->Field.flags = flags;
@@ -918,10 +929,10 @@ Ast *ast_field(AstFile *f, Array<Ast *> names, Ast *type, Ast *default_value, u3
return result;
}
-Ast *ast_field_list(AstFile *f, Token token, Array<Ast *> list) {
+Ast *ast_field_list(AstFile *f, Token token, Array<Ast *> const &list) {
Ast *result = alloc_ast_node(f, Ast_FieldList);
result->FieldList.token = token;
- result->FieldList.list = list;
+ result->FieldList.list = slice_from_array(list);
return result;
}
@@ -1002,7 +1013,7 @@ Ast *ast_dynamic_array_type(AstFile *f, Token token, Ast *elem) {
return result;
}
-Ast *ast_struct_type(AstFile *f, Token token, Array<Ast *> fields, isize field_count,
+Ast *ast_struct_type(AstFile *f, Token token, Slice<Ast *> fields, isize field_count,
Ast *polymorphic_params, bool is_packed, bool is_raw_union,
Ast *align,
Token where_token, Array<Ast *> const &where_clauses) {
@@ -1015,38 +1026,38 @@ Ast *ast_struct_type(AstFile *f, Token token, Array<Ast *> fields, isize field_c
result->StructType.is_raw_union = is_raw_union;
result->StructType.align = align;
result->StructType.where_token = where_token;
- result->StructType.where_clauses = where_clauses;
+ result->StructType.where_clauses = slice_from_array(where_clauses);
return result;
}
-Ast *ast_union_type(AstFile *f, Token token, Array<Ast *> variants, Ast *polymorphic_params, Ast *align, bool no_nil, bool maybe,
+Ast *ast_union_type(AstFile *f, Token token, Array<Ast *> const &variants, Ast *polymorphic_params, Ast *align, bool no_nil, bool maybe,
Token where_token, Array<Ast *> const &where_clauses) {
Ast *result = alloc_ast_node(f, Ast_UnionType);
result->UnionType.token = token;
- result->UnionType.variants = variants;
+ result->UnionType.variants = slice_from_array(variants);
result->UnionType.polymorphic_params = polymorphic_params;
result->UnionType.align = align;
result->UnionType.no_nil = no_nil;
- result->UnionType.maybe = maybe;
+ result->UnionType.maybe = maybe;
result->UnionType.where_token = where_token;
- result->UnionType.where_clauses = where_clauses;
+ result->UnionType.where_clauses = slice_from_array(where_clauses);
return result;
}
-Ast *ast_enum_type(AstFile *f, Token token, Ast *base_type, Array<Ast *> fields) {
+Ast *ast_enum_type(AstFile *f, Token token, Ast *base_type, Array<Ast *> const &fields) {
Ast *result = alloc_ast_node(f, Ast_EnumType);
result->EnumType.token = token;
result->EnumType.base_type = base_type;
- result->EnumType.fields = fields;
+ result->EnumType.fields = slice_from_array(fields);
return result;
}
-Ast *ast_bit_field_type(AstFile *f, Token token, Array<Ast *> fields, Ast *align) {
+Ast *ast_bit_field_type(AstFile *f, Token token, Array<Ast *> const &fields, Ast *align) {
Ast *result = alloc_ast_node(f, Ast_BitFieldType);
result->BitFieldType.token = token;
- result->BitFieldType.fields = fields;
+ result->BitFieldType.fields = slice_from_array(fields);
result->BitFieldType.align = align;
return result;
}
@@ -1069,7 +1080,7 @@ Ast *ast_map_type(AstFile *f, Token token, Ast *key, Ast *value) {
Ast *ast_foreign_block_decl(AstFile *f, Token token, Ast *foreign_library, Ast *body,
- CommentGroup *docs) {
+ CommentGroup *docs) {
Ast *result = alloc_ast_node(f, Ast_ForeignBlockDecl);
result->ForeignBlockDecl.token = token;
result->ForeignBlockDecl.foreign_library = foreign_library;
@@ -1087,12 +1098,12 @@ Ast *ast_label_decl(AstFile *f, Token token, Ast *name) {
return result;
}
-Ast *ast_value_decl(AstFile *f, Array<Ast *> names, Ast *type, Array<Ast *> values, bool is_mutable,
- CommentGroup *docs, CommentGroup *comment) {
+Ast *ast_value_decl(AstFile *f, Array<Ast *> const &names, Ast *type, Array<Ast *> const &values, bool is_mutable,
+ CommentGroup *docs, CommentGroup *comment) {
Ast *result = alloc_ast_node(f, Ast_ValueDecl);
- result->ValueDecl.names = names;
+ result->ValueDecl.names = slice_from_array(names);
result->ValueDecl.type = type;
- result->ValueDecl.values = values;
+ result->ValueDecl.values = slice_from_array(values);
result->ValueDecl.is_mutable = is_mutable;
result->ValueDecl.docs = docs;
result->ValueDecl.comment = comment;
@@ -1111,7 +1122,7 @@ Ast *ast_package_decl(AstFile *f, Token token, Token name, CommentGroup *docs, C
}
Ast *ast_import_decl(AstFile *f, Token token, bool is_using, Token relpath, Token import_name,
- CommentGroup *docs, CommentGroup *comment) {
+ CommentGroup *docs, CommentGroup *comment) {
Ast *result = alloc_ast_node(f, Ast_ImportDecl);
result->ImportDecl.token = token;
result->ImportDecl.is_using = is_using;
@@ -1123,10 +1134,10 @@ Ast *ast_import_decl(AstFile *f, Token token, bool is_using, Token relpath, Toke
}
Ast *ast_foreign_import_decl(AstFile *f, Token token, Array<Token> filepaths, Token library_name,
- CommentGroup *docs, CommentGroup *comment) {
+ CommentGroup *docs, CommentGroup *comment) {
Ast *result = alloc_ast_node(f, Ast_ForeignImportDecl);
result->ForeignImportDecl.token = token;
- result->ForeignImportDecl.filepaths = filepaths;
+ result->ForeignImportDecl.filepaths = slice_from_array(filepaths);
result->ForeignImportDecl.library_name = library_name;
result->ForeignImportDecl.docs = docs;
result->ForeignImportDecl.comment = comment;
@@ -1136,11 +1147,11 @@ Ast *ast_foreign_import_decl(AstFile *f, Token token, Array<Token> filepaths, To
}
-Ast *ast_attribute(AstFile *f, Token token, Token open, Token close, Array<Ast *> elems) {
+Ast *ast_attribute(AstFile *f, Token token, Token open, Token close, Array<Ast *> const &elems) {
Ast *result = alloc_ast_node(f, Ast_Attribute);
result->Attribute.token = token;
result->Attribute.open = open;
- result->Attribute.elems = elems;
+ result->Attribute.elems = slice_from_array(elems);
result->Attribute.close = close;
return result;
}
@@ -1182,6 +1193,12 @@ CommentGroup *consume_comment_group(AstFile *f, isize n, isize *end_line_) {
Array<Token> list = {};
list.allocator = heap_allocator();
isize end_line = f->curr_token.pos.line;
+ if (f->curr_token_index == 1 &&
+ f->prev_token.kind == Token_Comment &&
+ f->prev_token.pos.line+1 == f->curr_token.pos.line) {
+ // NOTE(bill): Special logic for the first comment in the file
+ array_add(&list, f->prev_token);
+ }
while (f->curr_token.kind == Token_Comment &&
f->curr_token.pos.line <= end_line+n) {
array_add(&list, consume_comment(f, &end_line));
@@ -1192,7 +1209,7 @@ CommentGroup *consume_comment_group(AstFile *f, isize n, isize *end_line_) {
CommentGroup *comments = nullptr;
if (list.count > 0) {
comments = gb_alloc_item(heap_allocator(), CommentGroup);
- comments->list = list;
+ comments->list = slice_from_array(list);
array_add(&f->comments, comments);
}
return comments;
@@ -1894,12 +1911,18 @@ Ast *parse_operand(AstFile *f, bool lhs) {
case Token_opaque: {
Token token = expect_token(f, Token_opaque);
+ warning(token, "opaque is deprecated, please use #opaque");
Ast *type = parse_type(f);
return ast_opaque_type(f, token, type);
}
case Token_Hash: {
Token token = expect_token(f, Token_Hash);
+ if (allow_token(f, Token_opaque)) {
+ Ast *type = parse_type(f);
+ return ast_opaque_type(f, token, type);
+ }
+
Token name = expect_token(f, Token_Ident);
if (name.string == "type") {
return ast_helper_type(f, token, parse_type(f));
@@ -2201,7 +2224,7 @@ Ast *parse_operand(AstFile *f, bool lhs) {
if (allow_token(f, Token_OpenParen)) {
isize param_count = 0;
- polymorphic_params = parse_field_list(f, &param_count, 0, Token_CloseParen, false, true);
+ polymorphic_params = parse_field_list(f, &param_count, 0, Token_CloseParen, true, true);
if (param_count == 0) {
syntax_error(polymorphic_params, "Expected at least 1 polymorphic parameter");
polymorphic_params = nullptr;
@@ -2262,7 +2285,7 @@ Ast *parse_operand(AstFile *f, bool lhs) {
Ast *fields = parse_struct_field_list(f, &name_count);
Token close = expect_token(f, Token_CloseBrace);
- Array<Ast *> decls = {};
+ Slice<Ast *> decls = {};
if (fields != nullptr) {
GB_ASSERT(fields->kind == Ast_FieldList);
decls = fields->FieldList.list;
@@ -2284,7 +2307,7 @@ Ast *parse_operand(AstFile *f, bool lhs) {
if (allow_token(f, Token_OpenParen)) {
isize param_count = 0;
- polymorphic_params = parse_field_list(f, &param_count, 0, Token_CloseParen, false, true);
+ polymorphic_params = parse_field_list(f, &param_count, 0, Token_CloseParen, true, true);
if (param_count == 0) {
syntax_error(polymorphic_params, "Expected at least 1 polymorphic parametric");
polymorphic_params = nullptr;
@@ -2586,7 +2609,15 @@ Ast *parse_call_expr(AstFile *f, Ast *operand) {
f->expr_level--;
close_paren = expect_closing(f, Token_CloseParen, str_lit("argument list"));
- return ast_call_expr(f, operand, args, open_paren, close_paren, ellipsis);
+
+ Ast *call = ast_call_expr(f, operand, args, open_paren, close_paren, ellipsis);
+
+ Ast *o = unparen_expr(operand);
+ if (o->kind == Ast_SelectorExpr && o->SelectorExpr.token.kind == Token_ArrowRight) {
+ return ast_selector_call_expr(f, o->SelectorExpr.token, o, call);
+ }
+
+ return call;
}
Ast *parse_atom_expr(AstFile *f, Ast *operand, bool lhs) {
@@ -2638,11 +2669,10 @@ Ast *parse_atom_expr(AstFile *f, Ast *operand, bool lhs) {
case Token_ArrowRight: {
Token token = advance_token(f);
- // syntax_error(token, "Selector expressions use '.' rather than '->'");
- Ast *sel = ast_selector_expr(f, token, operand, parse_ident(f));
- Ast *call = parse_call_expr(f, sel);
- operand = ast_selector_call_expr(f, token, sel, call);
+ operand = ast_selector_expr(f, token, operand, parse_ident(f));
+ // Ast *call = parse_call_expr(f, sel);
+ // operand = ast_selector_call_expr(f, token, sel, call);
break;
}
@@ -3317,11 +3347,10 @@ FieldPrefixKind is_token_field_prefix(AstFile *f) {
return FieldPrefix_no_alias;
} else if (f->curr_token.string == "c_vararg") {
return FieldPrefix_c_var_arg;
+ } else if (f->curr_token.string == "const") {
+ return FieldPrefix_const;
}
break;
-
- case Token_const:
- return FieldPrefix_const;
}
return FieldPrefix_Unknown;
}
@@ -4723,18 +4752,14 @@ void parser_add_foreign_file_to_process(Parser *p, AstPackage *pkg, AstForeignFi
// NOTE(bill): Returns true if it's added
-bool try_add_import_path(Parser *p, String const &path, String const &rel_path, TokenPos pos, PackageKind kind = Package_Normal) {
- if (build_context.generate_docs) {
- return false;
- }
-
+AstPackage *try_add_import_path(Parser *p, String const &path, String const &rel_path, TokenPos pos, PackageKind kind = Package_Normal) {
String const FILE_EXT = str_lit(".odin");
gb_mutex_lock(&p->file_add_mutex);
defer (gb_mutex_unlock(&p->file_add_mutex));
if (string_set_exists(&p->imported_files, path)) {
- return false;
+ return nullptr;
}
string_set_add(&p->imported_files, path);
@@ -4757,7 +4782,7 @@ bool try_add_import_path(Parser *p, String const &path, String const &rel_path,
pkg->is_single_file = true;
parser_add_file_to_process(p, pkg, fi, pos);
parser_add_package(p, pkg);
- return true;
+ return pkg;
}
@@ -4773,22 +4798,22 @@ bool try_add_import_path(Parser *p, String const &path, String const &rel_path,
switch (rd_err) {
case ReadDirectory_InvalidPath:
syntax_error(pos, "Invalid path: %.*s", LIT(rel_path));
- return false;
+ return nullptr;
case ReadDirectory_NotExists:
syntax_error(pos, "Path does not exist: %.*s", LIT(rel_path));
- return false;
+ return nullptr;
case ReadDirectory_Permission:
syntax_error(pos, "Unknown error whilst reading path %.*s", LIT(rel_path));
- return false;
+ return nullptr;
case ReadDirectory_NotDir:
syntax_error(pos, "Expected a directory for a package, got a file: %.*s", LIT(rel_path));
- return false;
+ return nullptr;
case ReadDirectory_Empty:
syntax_error(pos, "Empty directory: %.*s", LIT(rel_path));
- return false;
+ return nullptr;
case ReadDirectory_Unknown:
syntax_error(pos, "Unknown error whilst reading path %.*s", LIT(rel_path));
- return false;
+ return nullptr;
}
for_array(list_index, list) {
@@ -4810,7 +4835,7 @@ bool try_add_import_path(Parser *p, String const &path, String const &rel_path,
parser_add_package(p, pkg);
- return true;
+ return pkg;
}
gb_global Rune illegal_import_runes[] = {
@@ -4829,7 +4854,7 @@ bool is_import_path_valid(String path) {
u8 *curr = start;
while (curr < end) {
isize width = 1;
- Rune r = curr[0];
+ Rune r = *curr;
if (r >= 0x80) {
width = gb_utf8_decode(curr, end-curr, &r);
if (r == GB_RUNE_INVALID && width == 1) {
@@ -4854,6 +4879,45 @@ bool is_import_path_valid(String path) {
return false;
}
+bool is_build_flag_path_valid(String path) {
+ if (path.len > 0) {
+ u8 *start = path.text;
+ u8 *end = path.text + path.len;
+ u8 *curr = start;
+ isize index = 0;
+ while (curr < end) {
+ isize width = 1;
+ Rune r = *curr;
+ if (r >= 0x80) {
+ width = gb_utf8_decode(curr, end-curr, &r);
+ if (r == GB_RUNE_INVALID && width == 1) {
+ return false;
+ }
+ else if (r == GB_RUNE_BOM && curr-start > 0) {
+ return false;
+ }
+ }
+
+ for (isize i = 0; i < gb_count_of(illegal_import_runes); i++) {
+#if defined(GB_SYSTEM_WINDOWS)
+ if (r == '\\') {
+ break;
+ }
+#endif
+ if (r == illegal_import_runes[i]) {
+ return false;
+ }
+ }
+
+ curr += width;
+ index += 1;
+ }
+
+ return true;
+ }
+ return false;
+}
+
bool is_package_name_reserved(String const &name) {
if (name == "builtin") {
@@ -4974,7 +5038,7 @@ bool determine_path_from_string(gbMutex *file_mutex, Ast *node, String base_dir,
-void parse_setup_file_decls(Parser *p, AstFile *f, String base_dir, Array<Ast *> &decls);
+void parse_setup_file_decls(Parser *p, AstFile *f, String base_dir, Slice<Ast *> &decls);
void parse_setup_file_when_stmt(Parser *p, AstFile *f, String base_dir, AstWhenStmt *ws) {
if (ws->body != nullptr) {
@@ -4995,7 +5059,7 @@ void parse_setup_file_when_stmt(Parser *p, AstFile *f, String base_dir, AstWhenS
}
}
-void parse_setup_file_decls(Parser *p, AstFile *f, String base_dir, Array<Ast *> &decls) {
+void parse_setup_file_decls(Parser *p, AstFile *f, String base_dir, Slice<Ast *> &decls) {
for_array(i, decls) {
Ast *node = decls[i];
if (!is_ast_decl(node) &&
@@ -5034,8 +5098,7 @@ void parse_setup_file_decls(Parser *p, AstFile *f, String base_dir, Array<Ast *>
} else if (node->kind == Ast_ForeignImportDecl) {
ast_node(fl, ForeignImportDecl, node);
- fl->fullpaths.allocator = heap_allocator();
- array_reserve(&fl->fullpaths, fl->filepaths.count);
+ auto fullpaths = array_make<String>(permanent_allocator(), 0, fl->filepaths.count);
for_array(fp_idx, fl->filepaths) {
String file_str = fl->filepaths[fp_idx].string;
@@ -5049,14 +5112,17 @@ void parse_setup_file_decls(Parser *p, AstFile *f, String base_dir, Array<Ast *>
}
fullpath = foreign_path;
}
- array_add(&fl->fullpaths, fullpath);
+ array_add(&fullpaths, fullpath);
}
- if (fl->fullpaths.count == 0) {
+ if (fullpaths.count == 0) {
syntax_error(decls[i], "No foreign paths found");
decls[i] = ast_bad_decl(f, fl->filepaths[0], fl->filepaths[fl->filepaths.count-1]);
goto end;
}
+ fl->fullpaths = slice_from_array(fullpaths);
+
+
} else if (node->kind == Ast_WhenStmt) {
ast_node(ws, WhenStmt, node);
parse_setup_file_when_stmt(p, f, base_dir, ws);
@@ -5218,12 +5284,12 @@ bool parse_file(Parser *p, AstFile *f) {
f->pkg_decl = pd;
if (f->error_count == 0) {
- f->decls = array_make<Ast *>(heap_allocator());
+ auto decls = array_make<Ast *>(heap_allocator());
while (f->curr_token.kind != Token_EOF) {
Ast *stmt = parse_stmt(f);
if (stmt && stmt->kind != Ast_EmptyStmt) {
- array_add(&f->decls, stmt);
+ array_add(&decls, stmt);
if (stmt->kind == Ast_ExprStmt &&
stmt->ExprStmt.expr != nullptr &&
stmt->ExprStmt.expr->kind == Ast_ProcLit) {
@@ -5232,6 +5298,8 @@ bool parse_file(Parser *p, AstFile *f) {
}
}
+ f->decls = slice_from_array(decls);
+
parse_setup_file_decls(p, f, base_dir, f->decls);
}
@@ -5325,7 +5393,7 @@ ParseFileError parse_packages(Parser *p, String init_filename) {
}
TokenPos init_pos = {};
- if (!build_context.generate_docs) {
+ {
String s = get_fullpath_core(heap_allocator(), str_lit("runtime"));
try_add_import_path(p, s, s, init_pos, Package_Runtime);
}
@@ -5333,6 +5401,22 @@ ParseFileError parse_packages(Parser *p, String init_filename) {
try_add_import_path(p, init_fullpath, init_fullpath, init_pos, Package_Init);
p->init_fullpath = init_fullpath;
+ for_array(i, build_context.extra_packages) {
+ String path = build_context.extra_packages[i];
+ String fullpath = path_to_full_path(heap_allocator(), path); // LEAK?
+ if (!path_is_directory(fullpath)) {
+ String const ext = str_lit(".odin");
+ if (!string_ends_with(fullpath, ext)) {
+ error_line("Expected either a directory or a .odin file, got '%.*s'\n", LIT(fullpath));
+ return ParseFile_WrongExtension;
+ }
+ }
+ AstPackage *pkg = try_add_import_path(p, fullpath, fullpath, init_pos, Package_Normal);
+ if (pkg) {
+ pkg->is_extra = true;
+ }
+ }
+
thread_pool_start(&parser_thread_pool);
thread_pool_wait_to_process(&parser_thread_pool);
diff --git a/src/parser.hpp b/src/parser.hpp
index 8e210876f..cd0d59a48 100644
--- a/src/parser.hpp
+++ b/src/parser.hpp
@@ -46,7 +46,7 @@ enum ParseFileError {
};
struct CommentGroup {
- Array<Token> list; // Token_Comment
+ Slice<Token> list; // Token_Comment
};
@@ -98,8 +98,8 @@ struct AstFile {
bool in_foreign_block;
bool allow_type;
- Array<Ast *> decls;
- Array<Ast *> imports; // 'import' 'using import'
+ Slice<Ast *> decls;
+ Array<Ast *> imports; // 'import'
isize directive_count;
Ast * curr_proc;
@@ -107,6 +107,8 @@ struct AstFile {
f64 time_to_tokenize; // seconds
f64 time_to_parse; // seconds
+ bool is_test;
+
CommentGroup *lead_comment; // Comment (block) before the decl
CommentGroup *line_comment; // Comment after the semicolon
CommentGroup *docs; // current docs
@@ -148,6 +150,7 @@ struct AstPackage {
Scope * scope;
DeclInfo *decl_info;
bool used;
+ bool is_extra;
};
@@ -217,14 +220,16 @@ enum ProcCallingConvention {
ProcCC_ForeignBlockDefault = -1,
};
-enum StateFlag {
+enum StateFlag : u16 {
StateFlag_bounds_check = 1<<0,
StateFlag_no_bounds_check = 1<<1,
StateFlag_no_deferred = 1<<5,
+
+ StateFlag_BeenHandled = 1<<15,
};
-enum ViralStateFlag {
+enum ViralStateFlag : u16 {
ViralStateFlag_ContainsDeferredProcedure = 1<<0,
};
@@ -275,7 +280,6 @@ char const *inline_asm_dialect_strings[InlineAsmDialect_COUNT] = {
AST_KIND(Undef, "undef", Token) \
AST_KIND(BasicLit, "basic literal", struct { \
Token token; \
- ExactValue value; \
}) \
AST_KIND(BasicDirective, "basic directive", struct { \
Token token; \
@@ -289,7 +293,7 @@ char const *inline_asm_dialect_strings[InlineAsmDialect_COUNT] = {
Token token; \
Token open; \
Token close; \
- Array<Ast *> args; \
+ Slice<Ast *> args; \
}) \
AST_KIND(ProcLit, "procedure literal", struct { \
Ast *type; \
@@ -297,12 +301,12 @@ char const *inline_asm_dialect_strings[InlineAsmDialect_COUNT] = {
u64 tags; \
ProcInlining inlining; \
Token where_token; \
- Array<Ast *> where_clauses; \
+ Slice<Ast *> where_clauses; \
DeclInfo *decl; \
}) \
AST_KIND(CompoundLit, "compound literal", struct { \
Ast *type; \
- Array<Ast *> elems; \
+ Slice<Ast *> elems; \
Token open, close; \
i64 max_count; \
}) \
@@ -325,7 +329,7 @@ AST_KIND(_ExprBegin, "", bool) \
}) \
AST_KIND(CallExpr, "call expression", struct { \
Ast * proc; \
- Array<Ast *> args; \
+ Slice<Ast *> args; \
Token open; \
Token close; \
Token ellipsis; \
@@ -342,7 +346,7 @@ AST_KIND(_ExprBegin, "", bool) \
AST_KIND(InlineAsmExpr, "inline asm expression", struct { \
Token token; \
Token open, close; \
- Array<Ast *> param_types; \
+ Slice<Ast *> param_types; \
Ast *return_type; \
Ast *asm_string; \
Ast *constraints_string; \
@@ -362,11 +366,11 @@ AST_KIND(_StmtBegin, "", bool) \
}) \
AST_KIND(AssignStmt, "assign statement", struct { \
Token op; \
- Array<Ast *> lhs, rhs; \
+ Slice<Ast *> lhs, rhs; \
}) \
AST_KIND(_ComplexStmtBegin, "", bool) \
AST_KIND(BlockStmt, "block statement", struct { \
- Array<Ast *> stmts; \
+ Slice<Ast *> stmts; \
Ast *label; \
Token open, close; \
}) \
@@ -388,7 +392,7 @@ AST_KIND(_ComplexStmtBegin, "", bool) \
}) \
AST_KIND(ReturnStmt, "return statement", struct { \
Token token; \
- Array<Ast *> results; \
+ Slice<Ast *> results; \
}) \
AST_KIND(ForStmt, "for statement", struct { \
Token token; \
@@ -418,8 +422,8 @@ AST_KIND(_ComplexStmtBegin, "", bool) \
}) \
AST_KIND(CaseClause, "case clause", struct { \
Token token; \
- Array<Ast *> list; \
- Array<Ast *> stmts; \
+ Slice<Ast *> list; \
+ Slice<Ast *> stmts; \
Entity *implicit_entity; \
}) \
AST_KIND(SwitchStmt, "switch statement", struct { \
@@ -436,12 +440,12 @@ AST_KIND(_ComplexStmtBegin, "", bool) \
Ast *tag; \
Ast *body; \
bool partial; \
-}) \
+ }) \
AST_KIND(DeferStmt, "defer statement", struct { Token token; Ast *stmt; }) \
AST_KIND(BranchStmt, "branch statement", struct { Token token; Ast *label; }) \
AST_KIND(UsingStmt, "using statement", struct { \
Token token; \
- Array<Ast *> list; \
+ Slice<Ast *> list; \
}) \
AST_KIND(_ComplexStmtEnd, "", bool) \
AST_KIND(_StmtEnd, "", bool) \
@@ -459,9 +463,9 @@ AST_KIND(_DeclBegin, "", bool) \
Ast *name; \
}) \
AST_KIND(ValueDecl, "value declaration", struct { \
- Array<Ast *> names; \
+ Slice<Ast *> names; \
Ast * type; \
- Array<Ast *> values; \
+ Slice<Ast *> values; \
Array<Ast *> attributes; \
CommentGroup *docs; \
CommentGroup *comment; \
@@ -486,10 +490,10 @@ AST_KIND(_DeclBegin, "", bool) \
}) \
AST_KIND(ForeignImportDecl, "foreign import declaration", struct { \
Token token; \
- Array<Token> filepaths; \
+ Slice<Token> filepaths; \
Token library_name; \
String collection_name; \
- Array<String> fullpaths; \
+ Slice<String> fullpaths; \
Array<Ast *> attributes; \
CommentGroup *docs; \
CommentGroup *comment; \
@@ -497,11 +501,11 @@ AST_KIND(_DeclBegin, "", bool) \
AST_KIND(_DeclEnd, "", bool) \
AST_KIND(Attribute, "attribute", struct { \
Token token; \
- Array<Ast *> elems; \
+ Slice<Ast *> elems; \
Token open, close; \
}) \
AST_KIND(Field, "field", struct { \
- Array<Ast *> names; \
+ Slice<Ast *> names; \
Ast * type; \
Ast * default_value; \
Token tag; \
@@ -511,7 +515,7 @@ AST_KIND(_DeclEnd, "", bool) \
}) \
AST_KIND(FieldList, "field list", struct { \
Token token; \
- Array<Ast *> list; \
+ Slice<Ast *> list; \
}) \
AST_KIND(_TypeBegin, "", bool) \
AST_KIND(TypeidType, "typeid", struct { \
@@ -565,34 +569,34 @@ AST_KIND(_TypeBegin, "", bool) \
}) \
AST_KIND(StructType, "struct type", struct { \
Token token; \
- Array<Ast *> fields; \
+ Slice<Ast *> fields; \
isize field_count; \
Ast *polymorphic_params; \
Ast *align; \
Token where_token; \
- Array<Ast *> where_clauses; \
+ Slice<Ast *> where_clauses; \
bool is_packed; \
bool is_raw_union; \
}) \
AST_KIND(UnionType, "union type", struct { \
Token token; \
- Array<Ast *> variants; \
+ Slice<Ast *> variants; \
Ast *polymorphic_params; \
Ast * align; \
bool maybe; \
bool no_nil; \
Token where_token; \
- Array<Ast *> where_clauses; \
+ Slice<Ast *> where_clauses; \
}) \
AST_KIND(EnumType, "enum type", struct { \
Token token; \
Ast * base_type; \
- Array<Ast *> fields; /* FieldValue */ \
+ Slice<Ast *> fields; /* FieldValue */ \
bool is_using; \
}) \
AST_KIND(BitFieldType, "bit field type", struct { \
Token token; \
- Array<Ast *> fields; /* FieldValue with : */ \
+ Slice<Ast *> fields; /* FieldValue with : */ \
Ast * align; \
}) \
AST_KIND(BitSetType, "bit set type", struct { \
@@ -638,23 +642,22 @@ isize const ast_variant_sizes[] = {
struct AstCommonStuff {
AstKind kind;
- u32 state_flags;
- u32 viral_state_flags;
- bool been_handled;
+ u16 state_flags;
+ u16 viral_state_flags;
AstFile * file;
Scope * scope;
- TypeAndValue tav;
+ TypeAndValue tav; // TODO(bill): Make this a pointer to minimize pointer size
};
struct Ast {
AstKind kind;
- u32 state_flags;
- u32 viral_state_flags;
- bool been_handled;
+ u16 state_flags;
+ u16 viral_state_flags;
AstFile * file;
Scope * scope;
- TypeAndValue tav;
+ TypeAndValue tav; // TODO(bill): Make this a pointer to minimize pointer size
+ // IMPORTANT NOTE(bill): This must be at the end since the AST is allocated to be size of the variant
union {
#define AST_KIND(_kind_name_, name, ...) GB_JOIN2(Ast, _kind_name_) _kind_name_;
AST_KINDS
diff --git a/src/ptr_set.cpp b/src/ptr_set.cpp
index e343628af..5432fa094 100644
--- a/src/ptr_set.cpp
+++ b/src/ptr_set.cpp
@@ -1,25 +1,30 @@
+typedef u32 PtrSetIndex;
+
struct PtrSetFindResult {
- isize hash_index;
- isize entry_prev;
- isize entry_index;
+ PtrSetIndex hash_index;
+ PtrSetIndex entry_prev;
+ PtrSetIndex entry_index;
};
+enum : PtrSetIndex { PTR_SET_SENTINEL = ~(PtrSetIndex)0 };
+
template <typename T>
struct PtrSetEntry {
- T ptr;
- isize next;
+ T ptr;
+ PtrSetIndex next;
};
template <typename T>
struct PtrSet {
- Array<isize> hashes;
+ Array<PtrSetIndex> hashes;
Array<PtrSetEntry<T>> entries;
};
template <typename T> void ptr_set_init (PtrSet<T> *s, gbAllocator a, isize capacity = 16);
template <typename T> void ptr_set_destroy(PtrSet<T> *s);
template <typename T> T ptr_set_add (PtrSet<T> *s, T ptr);
+template <typename T> bool ptr_set_update (PtrSet<T> *s, T ptr); // returns true if it previously existsed
template <typename T> bool ptr_set_exists (PtrSet<T> *s, T ptr);
template <typename T> void ptr_set_remove (PtrSet<T> *s, T ptr);
template <typename T> void ptr_set_clear (PtrSet<T> *s);
@@ -27,12 +32,31 @@ template <typename T> void ptr_set_grow (PtrSet<T> *s);
template <typename T> void ptr_set_rehash (PtrSet<T> *s, isize new_count);
+isize next_pow2_isize(isize n) {
+ if (n <= 0) {
+ return 0;
+ }
+ n--;
+ n |= n >> 1;
+ n |= n >> 2;
+ n |= n >> 4;
+ n |= n >> 8;
+ n |= n >> 16;
+ if (gb_size_of(isize) == 8) {
+ n |= n >> 32;
+ }
+ n++;
+ return n;
+}
+
template <typename T>
void ptr_set_init(PtrSet<T> *s, gbAllocator a, isize capacity) {
+ capacity = next_pow2_isize(gb_max(16, capacity));
+
array_init(&s->hashes, a, capacity);
array_init(&s->entries, a, 0, capacity);
for (isize i = 0; i < capacity; i++) {
- s->hashes.data[i] = -1;
+ s->hashes.data[i] = PTR_SET_SENTINEL;
}
}
@@ -43,72 +67,69 @@ void ptr_set_destroy(PtrSet<T> *s) {
}
template <typename T>
-gb_internal isize ptr_set__add_entry(PtrSet<T> *s, T ptr) {
+gb_internal PtrSetIndex ptr_set__add_entry(PtrSet<T> *s, T ptr) {
PtrSetEntry<T> e = {};
e.ptr = ptr;
- e.next = -1;
+ e.next = PTR_SET_SENTINEL;
array_add(&s->entries, e);
- return s->entries.count-1;
+ return cast(PtrSetIndex)(s->entries.count-1);
}
template <typename T>
gb_internal PtrSetFindResult ptr_set__find(PtrSet<T> *s, T ptr) {
- PtrSetFindResult fr = {-1, -1, -1};
- if (s->hashes.count > 0) {
+ PtrSetFindResult fr = {PTR_SET_SENTINEL, PTR_SET_SENTINEL, PTR_SET_SENTINEL};
+ if (s->hashes.count != 0) {
u64 hash = 0xcbf29ce484222325ull ^ cast(u64)cast(uintptr)ptr;
u64 n = cast(u64)s->hashes.count;
- fr.hash_index = cast(isize)(hash % n);
- fr.entry_index = s->hashes[fr.hash_index];
- while (fr.entry_index >= 0) {
- if (s->entries[fr.entry_index].ptr == ptr) {
+ fr.hash_index = cast(PtrSetIndex)(hash & (n-1));
+ fr.entry_index = s->hashes.data[fr.hash_index];
+ while (fr.entry_index != PTR_SET_SENTINEL) {
+ if (s->entries.data[fr.entry_index].ptr == ptr) {
return fr;
}
fr.entry_prev = fr.entry_index;
- fr.entry_index = s->entries[fr.entry_index].next;
+ fr.entry_index = s->entries.data[fr.entry_index].next;
}
}
return fr;
}
template <typename T>
-gb_internal b32 ptr_set__full(PtrSet<T> *s) {
+gb_internal bool ptr_set__full(PtrSet<T> *s) {
return 0.75f * s->hashes.count <= s->entries.count;
}
-#define PTR_ARRAY_GROW_FORMULA(x) (4*(x) + 7)
-GB_STATIC_ASSERT(PTR_ARRAY_GROW_FORMULA(0) > 0);
-
template <typename T>
gb_inline void ptr_set_grow(PtrSet<T> *s) {
- isize new_count = PTR_ARRAY_GROW_FORMULA(s->entries.count);
+ isize new_count = s->hashes.count*2;
ptr_set_rehash(s, new_count);
}
template <typename T>
void ptr_set_rehash(PtrSet<T> *s, isize new_count) {
- isize i, j;
+ PtrSetIndex i, j;
PtrSet<T> ns = {};
ptr_set_init(&ns, s->hashes.allocator);
array_resize(&ns.hashes, new_count);
array_reserve(&ns.entries, s->entries.count);
for (i = 0; i < new_count; i++) {
- ns.hashes[i] = -1;
+ ns.hashes.data[i] = PTR_SET_SENTINEL;
}
for (i = 0; i < s->entries.count; i++) {
- PtrSetEntry<T> *e = &s->entries[i];
+ PtrSetEntry<T> *e = &s->entries.data[i];
PtrSetFindResult fr;
if (ns.hashes.count == 0) {
ptr_set_grow(&ns);
}
fr = ptr_set__find(&ns, e->ptr);
j = ptr_set__add_entry(&ns, e->ptr);
- if (fr.entry_prev < 0) {
- ns.hashes[fr.hash_index] = j;
+ if (fr.entry_prev == PTR_SET_SENTINEL) {
+ ns.hashes.data[fr.hash_index] = j;
} else {
- ns.entries[fr.entry_prev].next = j;
+ ns.entries.data[fr.entry_prev].next = j;
}
- ns.entries[j].next = fr.entry_index;
+ ns.entries.data[j].next = fr.entry_index;
if (ptr_set__full(&ns)) {
ptr_set_grow(&ns);
}
@@ -120,26 +141,24 @@ void ptr_set_rehash(PtrSet<T> *s, isize new_count) {
template <typename T>
gb_inline bool ptr_set_exists(PtrSet<T> *s, T ptr) {
isize index = ptr_set__find(s, ptr).entry_index;
- return index >= 0;
+ return index != PTR_SET_SENTINEL;
}
// Returns true if it already exists
template <typename T>
T ptr_set_add(PtrSet<T> *s, T ptr) {
- isize index;
+ PtrSetIndex index;
PtrSetFindResult fr;
if (s->hashes.count == 0) {
ptr_set_grow(s);
}
fr = ptr_set__find(s, ptr);
- if (fr.entry_index >= 0) {
- index = fr.entry_index;
- } else {
+ if (fr.entry_index == PTR_SET_SENTINEL) {
index = ptr_set__add_entry(s, ptr);
- if (fr.entry_prev >= 0) {
- s->entries[fr.entry_prev].next = index;
+ if (fr.entry_prev != PTR_SET_SENTINEL) {
+ s->entries.data[fr.entry_prev].next = index;
} else {
- s->hashes[fr.hash_index] = index;
+ s->hashes.data[fr.hash_index] = index;
}
}
if (ptr_set__full(s)) {
@@ -148,32 +167,58 @@ T ptr_set_add(PtrSet<T> *s, T ptr) {
return ptr;
}
+template <typename T>
+bool ptr_set_update(PtrSet<T> *s, T ptr) { // returns true if it previously existsed
+ bool exists = false;
+ PtrSetIndex index;
+ PtrSetFindResult fr;
+ if (s->hashes.count == 0) {
+ ptr_set_grow(s);
+ }
+ fr = ptr_set__find(s, ptr);
+ if (fr.entry_index != PTR_SET_SENTINEL) {
+ exists = true;
+ } else {
+ index = ptr_set__add_entry(s, ptr);
+ if (fr.entry_prev != PTR_SET_SENTINEL) {
+ s->entries.data[fr.entry_prev].next = index;
+ } else {
+ s->hashes.data[fr.hash_index] = index;
+ }
+ }
+ if (ptr_set__full(s)) {
+ ptr_set_grow(s);
+ }
+ return exists;
+}
+
+
template <typename T>
void ptr_set__erase(PtrSet<T> *s, PtrSetFindResult fr) {
PtrSetFindResult last;
- if (fr.entry_prev < 0) {
- s->hashes[fr.hash_index] = s->entries[fr.entry_index].next;
+ if (fr.entry_prev == PTR_SET_SENTINEL) {
+ s->hashes.data[fr.hash_index] = s->entries.data[fr.entry_index].next;
} else {
- s->entries[fr.entry_prev].next = s->entries[fr.entry_index].next;
+ s->entries.data[fr.entry_prev].next = s->entries.data[fr.entry_index].next;
}
if (fr.entry_index == s->entries.count-1) {
array_pop(&s->entries);
return;
}
- s->entries[fr.entry_index] = s->entries[s->entries.count-1];
- last = ptr_set__find(s, s->entries[fr.entry_index].ptr);
- if (last.entry_prev >= 0) {
- s->entries[last.entry_prev].next = fr.entry_index;
+ s->entries.data[fr.entry_index] = s->entries.data[s->entries.count-1];
+ last = ptr_set__find(s, s->entries.data[fr.entry_index].ptr);
+ if (last.entry_prev != PTR_SET_SENTINEL) {
+ s->entries.data[last.entry_prev].next = fr.entry_index;
} else {
- s->hashes[last.hash_index] = fr.entry_index;
+ s->hashes.data[last.hash_index] = fr.entry_index;
}
}
template <typename T>
void ptr_set_remove(PtrSet<T> *s, T ptr) {
PtrSetFindResult fr = ptr_set__find(s, ptr);
- if (fr.entry_index >= 0) {
+ if (fr.entry_index != PTR_SET_SENTINEL) {
ptr_set__erase(s, fr);
}
}
diff --git a/src/string.cpp b/src/string.cpp
index 6a0192507..408f53d72 100644
--- a/src/string.cpp
+++ b/src/string.cpp
@@ -164,6 +164,7 @@ int string_compare(String const &x, String const &y) {
return cast(int)x[offset] - cast(int)y[offset];
}
}
+ return cast(int)(x.len - y.len);
}
return 0;
}
diff --git a/src/tokenizer.cpp b/src/tokenizer.cpp
index 72448b869..bef82633f 100644
--- a/src/tokenizer.cpp
+++ b/src/tokenizer.cpp
@@ -118,8 +118,6 @@ TOKEN_KIND(Token__KeywordBegin, ""), \
TOKEN_KIND(Token_no_inline, "no_inline"), \
TOKEN_KIND(Token_context, "context"), \
TOKEN_KIND(Token_asm, "asm"), \
- TOKEN_KIND(Token_macro, "macro"), \
- TOKEN_KIND(Token_const, "const"), \
TOKEN_KIND(Token__KeywordEnd, ""), \
TOKEN_KIND(Token_Count, "")
diff --git a/src/types.cpp b/src/types.cpp
index fc4544385..df87cb645 100644
--- a/src/types.cpp
+++ b/src/types.cpp
@@ -323,6 +323,8 @@ String const type_strings[] = {
enum TypeFlag : u32 {
TypeFlag_Polymorphic = 1<<1,
TypeFlag_PolySpecialized = 1<<2,
+ TypeFlag_InProcessOfCheckingPolymorphic = 1<<3,
+ TypeFlag_InProcessOfCheckingABI = 1<<4,
};
struct Type {
@@ -371,7 +373,28 @@ enum Typeid_Kind : u8 {
Typeid_Relative_Slice,
};
+// IMPORTANT NOTE(bill): This must match the same as the in core.odin
+enum TypeInfoFlag : u32 {
+ TypeInfoFlag_Comparable = 1<<0,
+ TypeInfoFlag_Simple_Compare = 1<<1,
+};
+bool is_type_comparable(Type *t);
+bool is_type_simple_compare(Type *t);
+
+u32 type_info_flags_of_type(Type *type) {
+ if (type == nullptr) {
+ return 0;
+ }
+ u32 flags = 0;
+ if (is_type_comparable(type)) {
+ flags |= TypeInfoFlag_Comparable;
+ }
+ if (is_type_simple_compare(type)) {
+ flags |= TypeInfoFlag_Comparable;
+ }
+ return flags;
+}
// TODO(bill): Should I add extra information here specifying the kind of selection?
@@ -661,12 +684,15 @@ gb_global Type *t_context_ptr = nullptr;
gb_global Type *t_source_code_location = nullptr;
gb_global Type *t_source_code_location_ptr = nullptr;
-gb_global Type *t_map_key = nullptr;
+gb_global Type *t_map_hash = nullptr;
gb_global Type *t_map_header = nullptr;
gb_global Type *t_vector_x86_mmx = nullptr;
+gb_global Type *t_equal_proc = nullptr;
+gb_global Type *t_hasher_proc = nullptr;
+
i64 type_size_of (Type *t);
i64 type_align_of (Type *t);
@@ -769,7 +795,8 @@ void set_base_type(Type *t, Type *base) {
Type *alloc_type(TypeKind kind) {
- gbAllocator a = heap_allocator();
+ // gbAllocator a = heap_allocator();
+ gbAllocator a = permanent_allocator();
Type *t = gb_alloc_item(a, Type);
zero_item(t);
t->kind = kind;
@@ -884,6 +911,25 @@ Type *alloc_type_named(String name, Type *base, Entity *type_name) {
return t;
}
+bool is_calling_convention_none(ProcCallingConvention calling_convention) {
+ switch (calling_convention) {
+ case ProcCC_None:
+ case ProcCC_PureNone:
+ case ProcCC_InlineAsm:
+ return true;
+ }
+ return false;
+}
+
+bool is_calling_convention_odin(ProcCallingConvention calling_convention) {
+ switch (calling_convention) {
+ case ProcCC_Odin:
+ case ProcCC_Contextless:
+ return true;
+ }
+ return false;
+}
+
Type *alloc_type_tuple() {
Type *t = alloc_type(Type_Tuple);
return t;
@@ -918,7 +964,6 @@ bool is_type_valid_for_keys(Type *t);
Type *alloc_type_map(i64 count, Type *key, Type *value) {
if (key != nullptr) {
- GB_ASSERT(is_type_valid_for_keys(key));
GB_ASSERT(value != nullptr);
}
Type *t = alloc_type(Type_Map);
@@ -1192,20 +1237,6 @@ bool is_type_slice(Type *t) {
t = base_type(t);
return t->kind == Type_Slice;
}
-bool is_type_u8_slice(Type *t) {
- t = base_type(t);
- if (t->kind == Type_Slice) {
- return is_type_u8(t->Slice.elem);
- }
- return false;
-}
-bool is_type_u8_ptr(Type *t) {
- t = base_type(t);
- if (t->kind == Type_Pointer) {
- return is_type_u8(t->Slice.elem);
- }
- return false;
-}
bool is_type_proc(Type *t) {
t = base_type(t);
return t->kind == Type_Proc;
@@ -1249,6 +1280,37 @@ bool is_type_relative_slice(Type *t) {
return t->kind == Type_RelativeSlice;
}
+bool is_type_u8_slice(Type *t) {
+ t = base_type(t);
+ if (t->kind == Type_Slice) {
+ return is_type_u8(t->Slice.elem);
+ }
+ return false;
+}
+bool is_type_u8_array(Type *t) {
+ t = base_type(t);
+ if (t->kind == Type_Array) {
+ return is_type_u8(t->Array.elem);
+ }
+ return false;
+}
+bool is_type_u8_ptr(Type *t) {
+ t = base_type(t);
+ if (t->kind == Type_Pointer) {
+ return is_type_u8(t->Slice.elem);
+ }
+ return false;
+}
+bool is_type_rune_array(Type *t) {
+ t = base_type(t);
+ if (t->kind == Type_Array) {
+ return is_type_rune(t->Array.elem);
+ }
+ return false;
+}
+
+
+
Type *core_array_type(Type *t) {
for (;;) {
@@ -1261,53 +1323,7 @@ Type *core_array_type(Type *t) {
return t;
}
-// NOTE(bill): type can be easily compared using memcmp
-bool is_type_simple_compare(Type *t) {
- t = core_type(t);
- switch (t->kind) {
- case Type_Array:
- return is_type_simple_compare(t->Array.elem);
-
- case Type_EnumeratedArray:
- return is_type_simple_compare(t->EnumeratedArray.elem);
-
- case Type_Basic:
- if (t->Basic.flags & BasicFlag_SimpleCompare) {
- return true;
- }
- return false;
-
- case Type_Pointer:
- case Type_Proc:
- case Type_BitSet:
- case Type_BitField:
- return true;
-
- case Type_Struct:
- for_array(i, t->Struct.fields) {
- Entity *f = t->Struct.fields[i];
- if (!is_type_simple_compare(f->type)) {
- return false;
- }
- }
- return true;
-
- case Type_Union:
- for_array(i, t->Union.variants) {
- Type *v = t->Union.variants[i];
- if (!is_type_simple_compare(v)) {
- return false;
- }
- }
- return true;
-
- case Type_SimdVector:
- return is_type_simple_compare(t->SimdVector.elem);
-
- }
- return false;
-}
Type *base_complex_elem_type(Type *t) {
t = core_type(t);
@@ -1526,6 +1542,8 @@ bool is_type_valid_for_keys(Type *t) {
if (is_type_untyped(t)) {
return false;
}
+ return is_type_comparable(t);
+#if 0
if (is_type_integer(t)) {
return true;
}
@@ -1541,8 +1559,15 @@ bool is_type_valid_for_keys(Type *t) {
if (is_type_typeid(t)) {
return true;
}
+ if (is_type_simple_compare(t)) {
+ return true;
+ }
+ if (is_type_comparable(t)) {
+ return true;
+ }
return false;
+#endif
}
bool is_type_valid_bit_set_elem(Type *t) {
@@ -1695,12 +1720,23 @@ TypeTuple *get_record_polymorphic_params(Type *t) {
bool is_type_polymorphic(Type *t, bool or_specialized=false) {
+ if (t->flags & TypeFlag_InProcessOfCheckingPolymorphic) {
+ return false;
+ }
+
switch (t->kind) {
case Type_Generic:
return true;
case Type_Named:
- return is_type_polymorphic(t->Named.base, or_specialized);
+ {
+ u32 flags = t->flags;
+ t->flags |= TypeFlag_InProcessOfCheckingPolymorphic;
+ bool ok = is_type_polymorphic(t->Named.base, or_specialized);
+ t->flags = flags;
+ return ok;
+ }
+
case Type_Opaque:
return is_type_polymorphic(t->Opaque.elem, or_specialized);
case Type_Pointer:
@@ -1892,10 +1928,77 @@ bool is_type_comparable(Type *t) {
case Type_Opaque:
return is_type_comparable(t->Opaque.elem);
+
+ case Type_Struct:
+ if (type_size_of(t) == 0) {
+ return false;
+ }
+ if (t->Struct.is_raw_union) {
+ return is_type_simple_compare(t);
+ }
+ for_array(i, t->Struct.fields) {
+ Entity *f = t->Struct.fields[i];
+ if (!is_type_comparable(f->type)) {
+ return false;
+ }
+ }
+ return true;
+ }
+ return false;
+}
+
+// NOTE(bill): type can be easily compared using memcmp
+bool is_type_simple_compare(Type *t) {
+ t = core_type(t);
+ switch (t->kind) {
+ case Type_Array:
+ return is_type_simple_compare(t->Array.elem);
+
+ case Type_EnumeratedArray:
+ return is_type_simple_compare(t->EnumeratedArray.elem);
+
+ case Type_Basic:
+ if (t->Basic.flags & BasicFlag_SimpleCompare) {
+ return true;
+ }
+ if (t->Basic.kind == Basic_typeid) {
+ return true;
+ }
+ return false;
+
+ case Type_Pointer:
+ case Type_Proc:
+ case Type_BitSet:
+ case Type_BitField:
+ return true;
+
+ case Type_Struct:
+ for_array(i, t->Struct.fields) {
+ Entity *f = t->Struct.fields[i];
+ if (!is_type_simple_compare(f->type)) {
+ return false;
+ }
+ }
+ return true;
+
+ case Type_Union:
+ for_array(i, t->Union.variants) {
+ Type *v = t->Union.variants[i];
+ if (!is_type_simple_compare(v)) {
+ return false;
+ }
+ }
+ return true;
+
+ case Type_SimdVector:
+ return is_type_simple_compare(t->SimdVector.elem);
+
}
+
return false;
}
+
Type *strip_type_aliasing(Type *x) {
if (x == nullptr) {
return x;
@@ -2317,7 +2420,7 @@ Selection lookup_field_from_index(Type *type, i64 index) {
GB_ASSERT(is_type_struct(type) || is_type_union(type) || is_type_tuple(type));
type = base_type(type);
- gbAllocator a = heap_allocator();
+ gbAllocator a = permanent_allocator();
isize max_count = 0;
switch (type->kind) {
case Type_Struct: max_count = type->Struct.fields.count; break;
@@ -2365,7 +2468,6 @@ Selection lookup_field_from_index(Type *type, i64 index) {
return empty_selection;
}
-
Entity *scope_lookup_current(Scope *s, String const &name);
Selection lookup_field_with_selection(Type *type_, String field_name, bool is_type, Selection sel, bool allow_blank_ident) {
@@ -2375,7 +2477,6 @@ Selection lookup_field_with_selection(Type *type_, String field_name, bool is_ty
return empty_selection;
}
- gbAllocator a = heap_allocator();
Type *type = type_deref(type_);
bool is_ptr = type != type_;
sel.indirect = sel.indirect || is_ptr;
@@ -2964,7 +3065,7 @@ i64 type_align_of_internal(Type *t, TypePath *path) {
}
Array<i64> type_set_offsets_of(Array<Entity *> const &fields, bool is_packed, bool is_raw_union) {
- gbAllocator a = heap_allocator();
+ gbAllocator a = permanent_allocator();
auto offsets = array_make<i64>(a, fields.count);
i64 curr_offset = 0;
if (is_raw_union) {
@@ -3353,6 +3454,58 @@ Type *reduce_tuple_to_single_type(Type *original_type) {
}
+Type *alloc_type_struct_from_field_types(Type **field_types, isize field_count, bool is_packed) {
+ Type *t = alloc_type_struct();
+ t->Struct.fields = array_make<Entity *>(heap_allocator(), field_count);
+
+ Scope *scope = nullptr;
+ for_array(i, t->Struct.fields) {
+ t->Struct.fields[i] = alloc_entity_field(scope, blank_token, field_types[i], false, cast(i32)i, EntityState_Resolved);
+ }
+ t->Struct.is_packed = is_packed;
+
+ return t;
+}
+
+Type *alloc_type_tuple_from_field_types(Type **field_types, isize field_count, bool is_packed, bool must_be_tuple) {
+ if (field_count == 0) {
+ return nullptr;
+ }
+ if (!must_be_tuple && field_count == 1) {
+ return field_types[0];
+ }
+
+ Type *t = alloc_type_tuple();
+ t->Tuple.variables = array_make<Entity *>(heap_allocator(), field_count);
+
+ Scope *scope = nullptr;
+ for_array(i, t->Tuple.variables) {
+ t->Tuple.variables[i] = alloc_entity_param(scope, blank_token, field_types[i], false, false);
+ }
+ t->Tuple.is_packed = is_packed;
+
+ return t;
+}
+
+Type *alloc_type_proc_from_types(Type **param_types, unsigned param_count, Type *results, bool is_c_vararg, ProcCallingConvention calling_convention) {
+
+ Type *params = alloc_type_tuple_from_field_types(param_types, param_count, false, true);
+ isize results_count = 0;
+ if (results != nullptr) {
+ if (results->kind != Type_Tuple) {
+ results = alloc_type_tuple_from_field_types(&results, 1, false, true);
+ }
+ results_count = results->Tuple.variables.count;
+ }
+
+ Scope *scope = nullptr;
+ Type *t = alloc_type_proc(scope, params, param_count, results, results_count, false, calling_convention);
+ t->Proc.c_vararg = is_c_vararg;
+ return t;
+}
+
+
+
gbString write_type_to_string(gbString str, Type *type) {
if (type == nullptr) {
return gb_string_appendc(str, "<no type>");
@@ -3671,3 +3824,6 @@ gbString type_to_string(Type *type) {
return write_type_to_string(gb_string_make(heap_allocator(), ""), type);
}
+
+
+