typedef struct and start removing auto

This commit is contained in:
Ginger Bill
2016-11-23 11:07:35 +00:00
parent 8ecfca0c9b
commit aa2bcb166f
12 changed files with 286 additions and 308 deletions
+37 -60
View File
@@ -7,7 +7,7 @@
#define MAP_NAME MapEntity #define MAP_NAME MapEntity
#include "../map.c" #include "../map.c"
enum AddressingMode { typedef enum AddressingMode {
Addressing_Invalid, Addressing_Invalid,
Addressing_NoValue, Addressing_NoValue,
Addressing_Value, Addressing_Value,
@@ -16,25 +16,25 @@ enum AddressingMode {
Addressing_Type, Addressing_Type,
Addressing_Builtin, Addressing_Builtin,
Addressing_Count, Addressing_Count,
}; } AddressingMode;
struct Operand { typedef struct Operand {
AddressingMode mode; AddressingMode mode;
Type * type; Type * type;
ExactValue value; ExactValue value;
AstNode * expr; AstNode * expr;
BuiltinProcId builtin_id; BuiltinProcId builtin_id;
}; } Operand;
struct TypeAndValue { typedef struct TypeAndValue {
AddressingMode mode; AddressingMode mode;
Type * type; Type * type;
ExactValue value; ExactValue value;
}; } TypeAndValue;
struct DeclInfo { typedef struct DeclInfo {
Scope *scope; Scope *scope;
Entity **entities; Entity **entities;
@@ -46,30 +46,30 @@ struct DeclInfo {
u32 var_decl_tags; u32 var_decl_tags;
MapBool deps; // Key: Entity * MapBool deps; // Key: Entity *
}; } DeclInfo;
struct ExprInfo { typedef struct ExprInfo {
bool is_lhs; // Debug info bool is_lhs; // Debug info
AddressingMode mode; AddressingMode mode;
Type * type; // Type_Basic Type * type; // Type_Basic
ExactValue value; ExactValue value;
}; } ExprInfo;
ExprInfo make_expr_info(bool is_lhs, AddressingMode mode, Type *type, ExactValue value) { ExprInfo make_expr_info(bool is_lhs, AddressingMode mode, Type *type, ExactValue value) {
ExprInfo ei = {is_lhs, mode, type, value}; ExprInfo ei = {is_lhs, mode, type, value};
return ei; return ei;
} }
struct ProcedureInfo { typedef struct ProcedureInfo {
AstFile * file; AstFile * file;
Token token; Token token;
DeclInfo *decl; DeclInfo *decl;
Type * type; // Type_Procedure Type * type; // Type_Procedure
AstNode * body; // AstNode_BlockStatement AstNode * body; // AstNode_BlockStatement
u32 tags; u32 tags;
}; } ProcedureInfo;
struct Scope { typedef struct Scope {
Scope * parent; Scope * parent;
Scope * prev, *next; Scope * prev, *next;
Scope * first_child; Scope * first_child;
@@ -84,15 +84,15 @@ struct Scope {
bool is_file; bool is_file;
bool is_init; bool is_init;
AstFile * file; AstFile * file;
}; } Scope;
gb_global Scope *universal_scope = NULL; gb_global Scope *universal_scope = NULL;
enum ExprKind { typedef enum ExprKind {
Expr_Expr, Expr_Expr,
Expr_Stmt, Expr_Stmt,
}; } ExprKind;
enum BuiltinProcId { typedef enum BuiltinProcId {
BuiltinProc_Invalid, BuiltinProc_Invalid,
BuiltinProc_new, BuiltinProc_new,
@@ -129,13 +129,13 @@ enum BuiltinProcId {
BuiltinProc_enum_to_string, BuiltinProc_enum_to_string,
BuiltinProc_Count, BuiltinProc_Count,
}; } BuiltinProcId;
struct BuiltinProc { typedef struct BuiltinProc {
String name; String name;
isize arg_count; isize arg_count;
bool variadic; bool variadic;
ExprKind kind; ExprKind kind;
}; } BuiltinProc;
gb_global BuiltinProc builtin_procs[BuiltinProc_Count] = { gb_global BuiltinProc builtin_procs[BuiltinProc_Count] = {
{STR_LIT(""), 0, false, Expr_Stmt}, {STR_LIT(""), 0, false, Expr_Stmt},
@@ -173,28 +173,28 @@ gb_global BuiltinProc builtin_procs[BuiltinProc_Count] = {
{STR_LIT("enum_to_string"), 1, false, Expr_Expr}, {STR_LIT("enum_to_string"), 1, false, Expr_Expr},
}; };
enum ImplicitValueId { typedef enum ImplicitValueId {
ImplicitValue_Invalid, ImplicitValue_Invalid,
ImplicitValue_context, ImplicitValue_context,
ImplicitValue_Count, ImplicitValue_Count,
}; } ImplicitValueId;
struct ImplicitValueInfo { typedef struct ImplicitValueInfo {
String name; String name;
String backing_name; String backing_name;
Type * type; Type * type;
}; } ImplicitValueInfo;
// NOTE(bill): This is initialized later // NOTE(bill): This is initialized later
gb_global ImplicitValueInfo implicit_value_infos[ImplicitValue_Count] = {}; gb_global ImplicitValueInfo implicit_value_infos[ImplicitValue_Count] = {};
struct CheckerContext { typedef struct CheckerContext {
Scope * scope; Scope * scope;
DeclInfo *decl; DeclInfo *decl;
u32 stmt_state_flags; u32 stmt_state_flags;
}; } CheckerContext;
#define MAP_TYPE TypeAndValue #define MAP_TYPE TypeAndValue
#define MAP_FUNC map_tav_ #define MAP_FUNC map_tav_
@@ -223,7 +223,7 @@ struct CheckerContext {
// NOTE(bill): Symbol tables // NOTE(bill): Symbol tables
struct CheckerInfo { typedef struct CheckerInfo {
MapTypeAndValue types; // Key: AstNode * | Expression -> Type (and value) MapTypeAndValue types; // Key: AstNode * | Expression -> Type (and value)
MapEntity definitions; // Key: AstNode * | Identifier -> Entity MapEntity definitions; // Key: AstNode * | Identifier -> Entity
MapEntity uses; // Key: AstNode * | Identifier -> Entity MapEntity uses; // Key: AstNode * | Identifier -> Entity
@@ -235,9 +235,9 @@ struct CheckerInfo {
MapIsize type_info_map; // Key: Type * MapIsize type_info_map; // Key: Type *
isize type_info_count; isize type_info_count;
Entity * implicit_values[ImplicitValue_Count]; Entity * implicit_values[ImplicitValue_Count];
}; } CheckerInfo;
struct Checker { typedef struct Checker {
Parser * parser; Parser * parser;
CheckerInfo info; CheckerInfo info;
@@ -255,11 +255,11 @@ struct Checker {
Array(Type *) proc_stack; Array(Type *) proc_stack;
bool in_defer; // TODO(bill): Actually handle correctly bool in_defer; // TODO(bill): Actually handle correctly
}; } Checker;
struct CycleChecker { typedef struct CycleChecker {
Array(Entity *) path; // Entity_TypeName Array(Entity *) path; // Entity_TypeName
}; } CycleChecker;
@@ -766,7 +766,7 @@ void add_type_info_type(Checker *c, Type *t) {
isize ti_index = -1; isize ti_index = -1;
for_array(i, c->info.type_info_map.entries) { for_array(i, c->info.type_info_map.entries) {
auto *e = &c->info.type_info_map.entries.e[i]; MapIsizeEntry *e = &c->info.type_info_map.entries.e[i];
Type *prev_type = cast(Type *)e->key.ptr; Type *prev_type = cast(Type *)e->key.ptr;
if (are_types_identical(t, prev_type)) { if (are_types_identical(t, prev_type)) {
// Duplicate entry // Duplicate entry
@@ -931,7 +931,7 @@ MapEntity generate_minimum_dependency_map(CheckerInfo *info, Entity *start) {
map_entity_init(&map, heap_allocator()); map_entity_init(&map, heap_allocator());
for_array(i, info->entities.entries) { for_array(i, info->entities.entries) {
auto *entry = &info->entities.entries.e[i]; MapDeclInfoEntry *entry = &info->entities.entries.e[i];
Entity *e = cast(Entity *)cast(uintptr)entry->key.key; Entity *e = cast(Entity *)cast(uintptr)entry->key.key;
if (e->scope->is_global) { if (e->scope->is_global) {
// NOTE(bill): Require runtime stuff // NOTE(bill): Require runtime stuff
@@ -1023,7 +1023,7 @@ void add_implicit_value(Checker *c, ImplicitValueId id, String name, String back
void check_global_entity(Checker *c, EntityKind kind) { void check_global_entity(Checker *c, EntityKind kind) {
for_array(i, c->info.entities.entries) { for_array(i, c->info.entities.entries) {
auto *entry = &c->info.entities.entries.e[i]; MapDeclInfoEntry *entry = &c->info.entities.entries.e[i];
Entity *e = cast(Entity *)cast(uintptr)entry->key.key; Entity *e = cast(Entity *)cast(uintptr)entry->key.key;
if (e->kind == kind) { if (e->kind == kind) {
DeclInfo *d = entry->value; DeclInfo *d = entry->value;
@@ -1203,7 +1203,7 @@ void check_parsed_files(Checker *c) {
ast_node(id, ImportDecl, decl); ast_node(id, ImportDecl, decl);
HashKey key = hash_string(id->fullpath); HashKey key = hash_string(id->fullpath);
auto found = map_scope_get(&file_scopes, key); Scope **found = map_scope_get(&file_scopes, key);
GB_ASSERT_MSG(found != NULL, "Unable to find scope for file: %.*s", LIT(id->fullpath)); GB_ASSERT_MSG(found != NULL, "Unable to find scope for file: %.*s", LIT(id->fullpath));
Scope *scope = *found; Scope *scope = *found;
@@ -1338,7 +1338,7 @@ void check_parsed_files(Checker *c) {
// Add untyped expression values // Add untyped expression values
for_array(i, c->info.untyped.entries) { for_array(i, c->info.untyped.entries) {
auto *entry = &c->info.untyped.entries.e[i]; MapExprInfoEntry *entry = &c->info.untyped.entries.e[i];
HashKey key = entry->key; HashKey key = entry->key;
AstNode *expr = cast(AstNode *)cast(uintptr)key.key; AstNode *expr = cast(AstNode *)cast(uintptr)key.key;
ExprInfo *info = &entry->value; ExprInfo *info = &entry->value;
@@ -1364,29 +1364,6 @@ void check_parsed_files(Checker *c) {
} }
} }
// for_array(i, c->info.type_info_map.entries) {
// auto *e = &c->info.type_info_map.entries[i];
// Type *prev_type = cast(Type *)e->key.ptr;
// gb_printf("%td - %s\n", i, type_to_string(prev_type));
// }
// for_array(i, c->info.type_info_map.entries) {
// auto *p = &c->info.type_info_map.entries[i];
// for (isize j = 0; j < i-1; j++) {
// auto *q = &c->info.type_info_map.entries[j];
// Type *a = cast(Type *)p->key.ptr;
// Type *b = cast(Type *)q->key.ptr;
// p->value = i;
// // GB_ASSERT(!are_types_identical(a, b));
// }
// }
// for_array(i, c->info.type_info_map.entries) {
// auto *e = &c->info.type_info_map.entries[i];
// Type *prev_type = cast(Type *)e->key.ptr;
// gb_printf("%td - %s\n", e->value, type_to_string(prev_type));
// }
map_scope_destroy(&file_scopes); map_scope_destroy(&file_scopes);
array_free(&import_decls); array_free(&import_decls);
} }
+11 -11
View File
@@ -1,8 +1,8 @@
struct Scope; typedef struct Scope Scope;
struct Checker; typedef struct Checker Checker;
struct Type; typedef struct Type Type;
enum BuiltinProcId; typedef enum BuiltinProcId BuiltinProcId;
enum ImplicitValueId; typedef enum ImplicitValueId ImplicitValueId;
#define ENTITY_KINDS \ #define ENTITY_KINDS \
ENTITY_KIND(Invalid) \ ENTITY_KIND(Invalid) \
@@ -16,11 +16,11 @@ enum ImplicitValueId;
ENTITY_KIND(ImplicitValue) \ ENTITY_KIND(ImplicitValue) \
ENTITY_KIND(Count) ENTITY_KIND(Count)
enum EntityKind { typedef enum EntityKind {
#define ENTITY_KIND(k) GB_JOIN2(Entity_, k), #define ENTITY_KIND(k) GB_JOIN2(Entity_, k),
ENTITY_KINDS ENTITY_KINDS
#undef ENTITY_KIND #undef ENTITY_KIND
}; } EntityKind;
String const entity_strings[] = { String const entity_strings[] = {
#define ENTITY_KIND(k) {cast(u8 *)#k, gb_size_of(#k)-1}, #define ENTITY_KIND(k) {cast(u8 *)#k, gb_size_of(#k)-1},
@@ -28,16 +28,16 @@ String const entity_strings[] = {
#undef ENTITY_KIND #undef ENTITY_KIND
}; };
enum EntityFlag : u32 { typedef enum EntityFlag {
EntityFlag_Visited = 1<<0, EntityFlag_Visited = 1<<0,
EntityFlag_Used = 1<<1, EntityFlag_Used = 1<<1,
EntityFlag_Anonymous = 1<<2, EntityFlag_Anonymous = 1<<2,
EntityFlag_Field = 1<<3, EntityFlag_Field = 1<<3,
EntityFlag_Param = 1<<4, EntityFlag_Param = 1<<4,
EntityFlag_VectorElem = 1<<5, EntityFlag_VectorElem = 1<<5,
}; } EntityFlag;
struct Entity { typedef struct Entity {
EntityKind kind; EntityKind kind;
u32 flags; u32 flags;
Token token; Token token;
@@ -75,7 +75,7 @@ struct Entity {
Entity * backing; Entity * backing;
} ImplicitValue; } ImplicitValue;
}; };
}; } Entity;
bool is_entity_exported(Entity *e) { bool is_entity_exported(Entity *e) {
if (e->kind == Entity_ImportName) { if (e->kind == Entity_ImportName) {
+45 -46
View File
@@ -3573,6 +3573,49 @@ void check_expr_with_type_hint(Checker *c, Operand *o, AstNode *e, Type *t) {
} }
} }
bool check_set_index_data(Operand *o, Type *t, i64 *max_count) {
t = base_type(type_deref(t));
switch (t->kind) {
case Type_Basic:
if (is_type_string(t)) {
if (o->mode == Addressing_Constant) {
*max_count = o->value.value_string.len;
}
if (o->mode != Addressing_Variable) {
o->mode = Addressing_Value;
}
o->type = t_u8;
return true;
}
break;
case Type_Array:
*max_count = t->Array.count;
if (o->mode != Addressing_Variable) {
o->mode = Addressing_Value;
}
o->type = t->Array.elem;
return true;
case Type_Vector:
*max_count = t->Vector.count;
if (o->mode != Addressing_Variable) {
o->mode = Addressing_Value;
}
o->type = t->Vector.elem;
return true;
case Type_Slice:
o->type = t->Slice.elem;
o->mode = Addressing_Variable;
return true;
}
return false;
}
ExprKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *type_hint) { ExprKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *type_hint) {
ExprKind kind = Expr_Stmt; ExprKind kind = Expr_Stmt;
@@ -3892,52 +3935,8 @@ ExprKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *type_hint
Type *t = base_type(type_deref(o->type)); Type *t = base_type(type_deref(o->type));
bool is_const = o->mode == Addressing_Constant; bool is_const = o->mode == Addressing_Constant;
auto set_index_data = [](Operand *o, Type *t, i64 *max_count) -> bool {
t = base_type(type_deref(t));
switch (t->kind) {
case Type_Basic:
if (is_type_string(t)) {
if (o->mode == Addressing_Constant) {
*max_count = o->value.value_string.len;
}
if (o->mode != Addressing_Variable) {
o->mode = Addressing_Value;
}
o->type = t_u8;
return true;
}
break;
case Type_Array:
*max_count = t->Array.count;
if (o->mode != Addressing_Variable) {
o->mode = Addressing_Value;
}
o->type = t->Array.elem;
return true;
case Type_Vector:
*max_count = t->Vector.count;
if (o->mode != Addressing_Variable) {
o->mode = Addressing_Value;
}
o->type = t->Vector.elem;
return true;
case Type_Slice:
o->type = t->Slice.elem;
o->mode = Addressing_Variable;
return true;
}
return false;
};
i64 max_count = -1; i64 max_count = -1;
bool valid = set_index_data(o, t, &max_count); bool valid = check_set_index_data(o, t, &max_count);
if (is_const) { if (is_const) {
valid = false; valid = false;
@@ -3946,7 +3945,7 @@ ExprKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *type_hint
if (!valid && (is_type_struct(t) || is_type_raw_union(t))) { if (!valid && (is_type_struct(t) || is_type_raw_union(t))) {
Entity *found = find_using_index_expr(t); Entity *found = find_using_index_expr(t);
if (found != NULL) { if (found != NULL) {
valid = set_index_data(o, found->type, &max_count); valid = check_set_index_data(o, found->type, &max_count);
} }
} }
+50 -48
View File
@@ -1,6 +1,6 @@
struct Scope; typedef struct Scope Scope;
enum BasicKind { typedef enum BasicKind {
Basic_Invalid, Basic_Invalid,
Basic_bool, Basic_bool,
Basic_i8, Basic_i8,
@@ -34,9 +34,9 @@ enum BasicKind {
Basic_byte = Basic_u8, Basic_byte = Basic_u8,
Basic_rune = Basic_i32, Basic_rune = Basic_i32,
}; } BasicKind;
enum BasicFlag : u32 { typedef enum BasicFlag {
BasicFlag_Boolean = GB_BIT(0), BasicFlag_Boolean = GB_BIT(0),
BasicFlag_Integer = GB_BIT(1), BasicFlag_Integer = GB_BIT(1),
BasicFlag_Unsigned = GB_BIT(2), BasicFlag_Unsigned = GB_BIT(2),
@@ -49,14 +49,14 @@ enum BasicFlag : u32 {
BasicFlag_Numeric = BasicFlag_Integer | BasicFlag_Float, BasicFlag_Numeric = BasicFlag_Integer | BasicFlag_Float,
BasicFlag_Ordered = BasicFlag_Numeric | BasicFlag_String | BasicFlag_Pointer, BasicFlag_Ordered = BasicFlag_Numeric | BasicFlag_String | BasicFlag_Pointer,
BasicFlag_ConstantType = BasicFlag_Boolean | BasicFlag_Numeric | BasicFlag_Pointer | BasicFlag_String | BasicFlag_Rune, BasicFlag_ConstantType = BasicFlag_Boolean | BasicFlag_Numeric | BasicFlag_Pointer | BasicFlag_String | BasicFlag_Rune,
}; } BasicFlag;
struct BasicType { typedef struct BasicType {
BasicKind kind; BasicKind kind;
u32 flags; u32 flags;
i64 size; // -1 if arch. dep. i64 size; // -1 if arch. dep.
String name; String name;
}; } BasicType;
@@ -74,19 +74,19 @@ struct BasicType {
TYPE_KIND(Proc), \ TYPE_KIND(Proc), \
TYPE_KIND(Count), TYPE_KIND(Count),
enum TypeKind { typedef enum TypeKind {
#define TYPE_KIND(k) GB_JOIN2(Type_, k) #define TYPE_KIND(k, ...) GB_JOIN2(Type_, k)
TYPE_KINDS TYPE_KINDS
#undef TYPE_KIND #undef TYPE_KIND
}; } TypeKind;
String const type_strings[] = { String const type_strings[] = {
#define TYPE_KIND(k) {cast(u8 *)#k, gb_size_of(#k)-1} #define TYPE_KIND(k, ...) {cast(u8 *)#k, gb_size_of(#k)-1}
TYPE_KINDS TYPE_KINDS
#undef TYPE_KIND #undef TYPE_KIND
}; };
enum TypeRecordKind { typedef enum TypeRecordKind {
TypeRecord_Invalid, TypeRecord_Invalid,
TypeRecord_Struct, TypeRecord_Struct,
@@ -95,9 +95,9 @@ enum TypeRecordKind {
TypeRecord_Union, // Tagged TypeRecord_Union, // Tagged
TypeRecord_Count, TypeRecord_Count,
}; } TypeRecordKind;
struct Type { typedef struct Type {
TypeKind kind; TypeKind kind;
union { union {
BasicType Basic; BasicType Basic;
@@ -167,7 +167,42 @@ struct Type {
bool variadic; bool variadic;
} Proc; } Proc;
}; };
}; } Type;
// NOTE(bill): Internal sizes of certain types
// string: 2*word_size (ptr+len)
// slice: 3*word_size (ptr+len+cap)
// array: count*size_of(elem) aligned
// NOTE(bill): Alignment of structures and other types are to be compatible with C
typedef struct BaseTypeSizes {
i64 word_size;
i64 max_align;
} BaseTypeSizes;
typedef Array(isize) Array_isize;
typedef struct Selection {
Entity * entity;
Array_isize index;
bool indirect; // Set if there was a pointer deref anywhere down the line
} Selection;
Selection empty_selection = {};
Selection make_selection(Entity *entity, Array_isize index, bool indirect) {
Selection s = {entity, index, indirect};
return s;
}
void selection_add_index(Selection *s, isize index) {
// IMPORTANT NOTE(bill): this requires a stretchy buffer/dynamic array so it requires some form
// of heap allocation
if (s->index.e == NULL) {
array_init(&s->index, heap_allocator());
}
array_add(&s->index, index);
}
@@ -793,40 +828,7 @@ Type *default_type(Type *type) {
} }
// NOTE(bill): Internal sizes of certain types
// string: 2*word_size (ptr+len)
// slice: 3*word_size (ptr+len+cap)
// array: count*size_of(elem) aligned
// NOTE(bill): Alignment of structures and other types are to be compatible with C
struct BaseTypeSizes {
i64 word_size;
i64 max_align;
};
typedef Array(isize) Array_isize;
struct Selection {
Entity * entity;
Array_isize index;
bool indirect; // Set if there was a pointer deref anywhere down the line
};
Selection empty_selection = {};
Selection make_selection(Entity *entity, Array_isize index, bool indirect) {
Selection s = {entity, index, indirect};
return s;
}
void selection_add_index(Selection *s, isize index) {
// IMPORTANT NOTE(bill): this requires a stretchy buffer/dynamic array so it requires some form
// of heap allocation
if (s->index.e == NULL) {
array_init(&s->index, heap_allocator());
}
array_add(&s->index, index);
}
gb_global Entity *entity__any_type_info = NULL; gb_global Entity *entity__any_type_info = NULL;
gb_global Entity *entity__any_data = NULL; gb_global Entity *entity__any_data = NULL;
+5 -5
View File
@@ -3,9 +3,9 @@
// TODO(bill): Big numbers // TODO(bill): Big numbers
// IMPORTANT TODO(bill): This needs to be completely fixed!!!!!!!! // IMPORTANT TODO(bill): This needs to be completely fixed!!!!!!!!
struct AstNode; typedef struct AstNode AstNode;
enum ExactValueKind { typedef enum ExactValueKind {
ExactValue_Invalid, ExactValue_Invalid,
ExactValue_Bool, ExactValue_Bool,
@@ -16,9 +16,9 @@ enum ExactValueKind {
ExactValue_Compound, // TODO(bill): Is this good enough? ExactValue_Compound, // TODO(bill): Is this good enough?
ExactValue_Count, ExactValue_Count,
}; } ExactValueKind;
struct ExactValue { typedef struct ExactValue {
ExactValueKind kind; ExactValueKind kind;
union { union {
bool value_bool; bool value_bool;
@@ -28,7 +28,7 @@ struct ExactValue {
i64 value_pointer; i64 value_pointer;
AstNode *value_compound; AstNode *value_compound;
}; };
}; } ExactValue;
HashKey hash_exact_value(ExactValue v) { HashKey hash_exact_value(ExactValue v) {
return hashing_proc(&v, gb_size_of(ExactValue)); return hashing_proc(&v, gb_size_of(ExactValue));
+2 -2
View File
@@ -56,12 +56,12 @@ i32 win32_exec_command_line_app(char *name, char *fmt, ...) {
return exit_code; return exit_code;
} }
typedef enum { typedef enum ArchKind {
ArchKind_x64, ArchKind_x64,
ArchKind_x86, ArchKind_x86,
} ArchKind; } ArchKind;
typedef struct { typedef struct ArchData {
BaseTypeSizes sizes; BaseTypeSizes sizes;
String llc_flags; String llc_flags;
String link_flags; String link_flags;
+23 -23
View File
@@ -1,8 +1,8 @@
struct AstNode; typedef struct AstNode AstNode;
struct Scope; typedef struct Scope Scope;
struct DeclInfo; typedef struct DeclInfo DeclInfo;
enum ParseFileError { typedef enum ParseFileError {
ParseFile_None, ParseFile_None,
ParseFile_WrongExtension, ParseFile_WrongExtension,
@@ -13,11 +13,11 @@ enum ParseFileError {
ParseFile_InvalidToken, ParseFile_InvalidToken,
ParseFile_Count, ParseFile_Count,
}; } ParseFileError;
typedef Array(AstNode *) AstNodeArray; typedef Array(AstNode *) AstNodeArray;
struct AstFile { typedef struct AstFile {
i32 id; i32 id;
gbArena arena; gbArena arena;
Tokenizer tokenizer; Tokenizer tokenizer;
@@ -43,15 +43,15 @@ struct AstFile {
#define PARSER_MAX_FIX_COUNT 6 #define PARSER_MAX_FIX_COUNT 6
isize fix_count; isize fix_count;
TokenPos fix_prev_pos; TokenPos fix_prev_pos;
}; } AstFile;
struct ImportedFile { typedef struct ImportedFile {
String path; String path;
String rel_path; String rel_path;
TokenPos pos; // #import TokenPos pos; // #import
}; } ImportedFile;
struct Parser { typedef struct Parser {
String init_fullpath; String init_fullpath;
Array(AstFile) files; Array(AstFile) files;
Array(ImportedFile) imports; Array(ImportedFile) imports;
@@ -59,9 +59,9 @@ struct Parser {
Array(String) foreign_libraries; Array(String) foreign_libraries;
isize total_token_count; isize total_token_count;
gbMutex mutex; gbMutex mutex;
}; } Parser;
enum ProcTag : u64 { typedef enum ProcTag {
ProcTag_bounds_check = GB_BIT(0), ProcTag_bounds_check = GB_BIT(0),
ProcTag_no_bounds_check = GB_BIT(1), ProcTag_no_bounds_check = GB_BIT(1),
@@ -75,23 +75,23 @@ enum ProcTag : u64 {
ProcTag_stdcall = GB_BIT(16), ProcTag_stdcall = GB_BIT(16),
ProcTag_fastcall = GB_BIT(17), ProcTag_fastcall = GB_BIT(17),
// ProcTag_cdecl = GB_BIT(18), // ProcTag_cdecl = GB_BIT(18),
}; } ProcTag;
enum VarDeclTag { typedef enum VarDeclTag {
VarDeclTag_thread_local = GB_BIT(0), VarDeclTag_thread_local = GB_BIT(0),
}; } VarDeclTag;
enum StmtStateFlag : u32 { typedef enum StmtStateFlag {
StmtStateFlag_bounds_check = GB_BIT(0), StmtStateFlag_bounds_check = GB_BIT(0),
StmtStateFlag_no_bounds_check = GB_BIT(1), StmtStateFlag_no_bounds_check = GB_BIT(1),
}; } StmtStateFlag;
enum CallExprKind { typedef enum CallExprKind {
CallExpr_Prefix, // call(...) CallExpr_Prefix, // call(...)
CallExpr_Postfix, // a'call CallExpr_Postfix, // a'call
CallExpr_Infix, // a ''call b CallExpr_Infix, // a ''call b
}; } CallExprKind;
AstNodeArray make_ast_node_array(AstFile *f) { AstNodeArray make_ast_node_array(AstFile *f) {
AstNodeArray a; AstNodeArray a;
@@ -320,11 +320,11 @@ AST_NODE_KIND(_TypeBegin, "", struct{}) \
AST_NODE_KIND(_TypeEnd, "", struct{}) \ AST_NODE_KIND(_TypeEnd, "", struct{}) \
AST_NODE_KIND(Count, "", struct{}) AST_NODE_KIND(Count, "", struct{})
enum AstNodeKind { typedef enum AstNodeKind {
#define AST_NODE_KIND(_kind_name_, ...) GB_JOIN2(AstNode_, _kind_name_), #define AST_NODE_KIND(_kind_name_, ...) GB_JOIN2(AstNode_, _kind_name_),
AST_NODE_KINDS AST_NODE_KINDS
#undef AST_NODE_KIND #undef AST_NODE_KIND
}; } AstNodeKind;
String const ast_node_strings[] = { String const ast_node_strings[] = {
#define AST_NODE_KIND(_kind_name_, name, ...) {cast(u8 *)name, gb_size_of(name)-1}, #define AST_NODE_KIND(_kind_name_, name, ...) {cast(u8 *)name, gb_size_of(name)-1},
@@ -336,7 +336,7 @@ String const ast_node_strings[] = {
AST_NODE_KINDS AST_NODE_KINDS
#undef AST_NODE_KIND #undef AST_NODE_KIND
struct AstNode { typedef struct AstNode {
AstNodeKind kind; AstNodeKind kind;
// AstNode *prev, *next; // NOTE(bill): allow for Linked list // AstNode *prev, *next; // NOTE(bill): allow for Linked list
u32 stmt_state_flags; u32 stmt_state_flags;
@@ -345,7 +345,7 @@ struct AstNode {
AST_NODE_KINDS AST_NODE_KINDS
#undef AST_NODE_KIND #undef AST_NODE_KIND
}; };
}; } AstNode;
#define ast_node(n_, Kind_, node_) GB_JOIN2(AstNode, Kind_) *n_ = &(node_)->Kind_; GB_ASSERT((node_)->kind == GB_JOIN2(AstNode_, Kind_)) #define ast_node(n_, Kind_, node_) GB_JOIN2(AstNode, Kind_) *n_ = &(node_)->Kind_; GB_ASSERT((node_)->kind == GB_JOIN2(AstNode_, Kind_))
+86 -85
View File
@@ -15,7 +15,7 @@ typedef Array(ssaValue *) ssaValueArray;
#define MAP_NAME MapSsaDebugInfo #define MAP_NAME MapSsaDebugInfo
#include "map.c" #include "map.c"
struct ssaModule { typedef struct ssaModule {
CheckerInfo * info; CheckerInfo * info;
BaseTypeSizes sizes; BaseTypeSizes sizes;
gbArena arena; gbArena arena;
@@ -41,17 +41,17 @@ struct ssaModule {
Array(ssaProcedure *) procs; // NOTE(bill): All procedures with bodies Array(ssaProcedure *) procs; // NOTE(bill): All procedures with bodies
ssaValueArray procs_to_generate; // NOTE(bill): Procedures to generate ssaValueArray procs_to_generate; // NOTE(bill): Procedures to generate
}; } ssaModule;
// NOTE(bill): For more info, see https://en.wikipedia.org/wiki/Dominator_(graph_theory) // NOTE(bill): For more info, see https://en.wikipedia.org/wiki/Dominator_(graph_theory)
struct ssaDomNode { typedef struct ssaDomNode {
ssaBlock * idom; // Parent (Immediate Dominator) ssaBlock * idom; // Parent (Immediate Dominator)
Array(ssaBlock *) children; Array(ssaBlock *) children;
i32 pre, post; // Ordering in tree i32 pre, post; // Ordering in tree
}; } ssaDomNode;
struct ssaBlock { typedef struct ssaBlock {
i32 index; i32 index;
String label; String label;
ssaProcedure *parent; ssaProcedure *parent;
@@ -66,8 +66,9 @@ struct ssaBlock {
Array(ssaBlock *) preds; Array(ssaBlock *) preds;
Array(ssaBlock *) succs; Array(ssaBlock *) succs;
}; } ssaBlock;
typedef struct ssaTargetList ssaTargetList;
struct ssaTargetList { struct ssaTargetList {
ssaTargetList *prev; ssaTargetList *prev;
ssaBlock * break_; ssaBlock * break_;
@@ -75,17 +76,17 @@ struct ssaTargetList {
ssaBlock * fallthrough_; ssaBlock * fallthrough_;
}; };
enum ssaDeferExitKind { typedef enum ssaDeferExitKind {
ssaDeferExit_Default, ssaDeferExit_Default,
ssaDeferExit_Return, ssaDeferExit_Return,
ssaDeferExit_Branch, ssaDeferExit_Branch,
}; } ssaDeferExitKind;
enum ssaDeferKind { typedef enum ssaDeferKind {
ssaDefer_Node, ssaDefer_Node,
ssaDefer_Instr, ssaDefer_Instr,
}; } ssaDeferKind;
struct ssaDefer { typedef struct ssaDefer {
ssaDeferKind kind; ssaDeferKind kind;
isize scope_index; isize scope_index;
ssaBlock * block; ssaBlock * block;
@@ -94,8 +95,9 @@ struct ssaDefer {
// NOTE(bill): `instr` will be copied every time to create a new one // NOTE(bill): `instr` will be copied every time to create a new one
ssaValue *instr; ssaValue *instr;
}; };
}; } ssaDefer;
typedef struct ssaProcedure ssaProcedure;
struct ssaProcedure { struct ssaProcedure {
ssaProcedure * parent; ssaProcedure * parent;
Array(ssaProcedure *) children; Array(ssaProcedure *) children;
@@ -172,11 +174,11 @@ struct ssaProcedure {
SSA_CONV_KIND(inttoptr), \ SSA_CONV_KIND(inttoptr), \
SSA_CONV_KIND(bitcast), SSA_CONV_KIND(bitcast),
enum ssaInstrKind { typedef enum ssaInstrKind {
#define SSA_INSTR_KIND(x) GB_JOIN2(ssaInstr_, x) #define SSA_INSTR_KIND(x) GB_JOIN2(ssaInstr_, x)
SSA_INSTR_KINDS SSA_INSTR_KINDS
#undef SSA_INSTR_KIND #undef SSA_INSTR_KIND
}; } ssaInstrKind;
String const ssa_instr_strings[] = { String const ssa_instr_strings[] = {
#define SSA_INSTR_KIND(x) {cast(u8 *)#x, gb_size_of(#x)-1} #define SSA_INSTR_KIND(x) {cast(u8 *)#x, gb_size_of(#x)-1}
@@ -184,11 +186,11 @@ String const ssa_instr_strings[] = {
#undef SSA_INSTR_KIND #undef SSA_INSTR_KIND
}; };
enum ssaConvKind { typedef enum ssaConvKind {
#define SSA_CONV_KIND(x) GB_JOIN2(ssaConv_, x) #define SSA_CONV_KIND(x) GB_JOIN2(ssaConv_, x)
SSA_CONV_KINDS SSA_CONV_KINDS
#undef SSA_CONV_KIND #undef SSA_CONV_KIND
}; } ssaConvKind;
String const ssa_conv_strings[] = { String const ssa_conv_strings[] = {
#define SSA_CONV_KIND(x) {cast(u8 *)#x, gb_size_of(#x)-1} #define SSA_CONV_KIND(x) {cast(u8 *)#x, gb_size_of(#x)-1}
@@ -196,6 +198,7 @@ String const ssa_conv_strings[] = {
#undef SSA_CONV_KIND #undef SSA_CONV_KIND
}; };
typedef struct ssaInstr ssaInstr;
struct ssaInstr { struct ssaInstr {
ssaInstrKind kind; ssaInstrKind kind;
@@ -330,7 +333,7 @@ struct ssaInstr {
}; };
enum ssaValueKind { typedef enum ssaValueKind {
ssaValue_Invalid, ssaValue_Invalid,
ssaValue_Constant, ssaValue_Constant,
@@ -345,9 +348,9 @@ enum ssaValueKind {
ssaValue_Instr, ssaValue_Instr,
ssaValue_Count, ssaValue_Count,
}; } ssaValueKind;
struct ssaValue { typedef struct ssaValue {
ssaValueKind kind; ssaValueKind kind;
i32 index; i32 index;
union { union {
@@ -387,7 +390,7 @@ struct ssaValue {
ssaBlock Block; ssaBlock Block;
ssaInstr Instr; ssaInstr Instr;
}; };
}; } ssaValue;
gb_global ssaValue *v_zero = NULL; gb_global ssaValue *v_zero = NULL;
gb_global ssaValue *v_one = NULL; gb_global ssaValue *v_one = NULL;
@@ -397,19 +400,19 @@ gb_global ssaValue *v_two32 = NULL;
gb_global ssaValue *v_false = NULL; gb_global ssaValue *v_false = NULL;
gb_global ssaValue *v_true = NULL; gb_global ssaValue *v_true = NULL;
enum ssaAddrKind { typedef enum ssaAddrKind {
ssaAddr_Default, ssaAddr_Default,
ssaAddr_Vector, ssaAddr_Vector,
}; } ssaAddrKind;
struct ssaAddr { typedef struct ssaAddr {
ssaValue * addr; ssaValue * addr;
AstNode * expr; // NOTE(bill): Just for testing - probably remove later AstNode * expr; // NOTE(bill): Just for testing - probably remove later
ssaAddrKind kind; ssaAddrKind kind;
union { union {
struct { ssaValue *index; } Vector; struct { ssaValue *index; } Vector;
}; };
}; } ssaAddr;
ssaAddr ssa_make_addr(ssaValue *addr, AstNode *expr) { ssaAddr ssa_make_addr(ssaValue *addr, AstNode *expr) {
ssaAddr v = {addr, expr}; ssaAddr v = {addr, expr};
@@ -424,7 +427,7 @@ ssaAddr ssa_make_addr_vector(ssaValue *addr, ssaValue *index, AstNode *expr) {
enum ssaDebugEncoding { typedef enum ssaDebugEncoding {
ssaDebugBasicEncoding_Invalid = 0, ssaDebugBasicEncoding_Invalid = 0,
ssaDebugBasicEncoding_address = 1, ssaDebugBasicEncoding_address = 1,
@@ -444,9 +447,9 @@ enum ssaDebugEncoding {
ssaDebugBasicEncoding_structure_type = 19, ssaDebugBasicEncoding_structure_type = 19,
ssaDebugBasicEncoding_union_type = 23, ssaDebugBasicEncoding_union_type = 23,
}; } ssaDebugEncoding;
enum ssaDebugInfoKind { typedef enum ssaDebugInfoKind {
ssaDebugInfo_Invalid, ssaDebugInfo_Invalid,
ssaDebugInfo_CompileUnit, ssaDebugInfo_CompileUnit,
@@ -465,8 +468,9 @@ enum ssaDebugInfoKind {
ssaDebugInfo_Count, ssaDebugInfo_Count,
}; } ssaDebugInfoKind;
typedef struct ssaDebugInfo ssaDebugInfo;
struct ssaDebugInfo { struct ssaDebugInfo {
ssaDebugInfoKind kind; ssaDebugInfoKind kind;
i32 id; i32 id;
@@ -547,11 +551,11 @@ struct ssaDebugInfo {
}; };
}; };
struct ssaGen { typedef struct ssaGen {
ssaModule module; ssaModule module;
gbFile output_file; gbFile output_file;
bool opt_called; bool opt_called;
}; } ssaGen;
ssaValue *ssa_lookup_member(ssaModule *m, String name) { ssaValue *ssa_lookup_member(ssaModule *m, String name) {
ssaValue **v = map_ssa_value_get(&m->members, hash_string(name)); ssaValue **v = map_ssa_value_get(&m->members, hash_string(name));
@@ -2348,7 +2352,7 @@ isize ssa_type_info_index(CheckerInfo *info, Type *type) {
// NOTE(bill): Do manual search // NOTE(bill): Do manual search
// TODO(bill): This is O(n) and can be very slow // TODO(bill): This is O(n) and can be very slow
for_array(i, info->type_info_map.entries){ for_array(i, info->type_info_map.entries){
auto *e = &info->type_info_map.entries.e[i]; MapIsizeEntry *e = &info->type_info_map.entries.e[i];
Type *prev_type = cast(Type *)e->key.ptr; Type *prev_type = cast(Type *)e->key.ptr;
if (are_types_identical(prev_type, type)) { if (are_types_identical(prev_type, type)) {
entry_index = e->value; entry_index = e->value;
@@ -3277,6 +3281,18 @@ ssaValue *ssa_add_using_variable(ssaProcedure *proc, Entity *e) {
return var; return var;
} }
bool ssa_is_elem_const(ssaModule *m, AstNode *elem, Type *elem_type) {
if (base_type(elem_type) == t_any) {
return false;
}
if (elem->kind == AstNode_FieldValue) {
elem = elem->FieldValue.value;
}
TypeAndValue *tav = type_and_value_of_expression(m->info, elem);
GB_ASSERT(tav != NULL);
return tav->value.kind != ExactValue_Invalid;
}
ssaAddr ssa_build_addr(ssaProcedure *proc, AstNode *expr) { ssaAddr ssa_build_addr(ssaProcedure *proc, AstNode *expr) {
switch (expr->kind) { switch (expr->kind) {
case_ast_node(i, Ident, expr); case_ast_node(i, Ident, expr);
@@ -3608,18 +3624,6 @@ ssaAddr ssa_build_addr(ssaProcedure *proc, AstNode *expr) {
case Type_Slice: et = bt->Slice.elem; break; case Type_Slice: et = bt->Slice.elem; break;
} }
auto is_elem_const = [](ssaModule *m, AstNode *elem, Type *elem_type) -> bool {
if (base_type(elem_type) == t_any) {
return false;
}
if (elem->kind == AstNode_FieldValue) {
elem = elem->FieldValue.value;
}
TypeAndValue *tav = type_and_value_of_expression(m->info, elem);
GB_ASSERT(tav != NULL);
return tav->value.kind != ExactValue_Invalid;
};
switch (bt->kind) { switch (bt->kind) {
default: GB_PANIC("Unknown CompoundLit type: %s", type_to_string(type)); break; default: GB_PANIC("Unknown CompoundLit type: %s", type_to_string(type)); break;
@@ -3627,7 +3631,7 @@ ssaAddr ssa_build_addr(ssaProcedure *proc, AstNode *expr) {
ssaValue *result = ssa_add_module_constant(proc->module, type, make_exact_value_compound(expr)); ssaValue *result = ssa_add_module_constant(proc->module, type, make_exact_value_compound(expr));
for_array(index, cl->elems) { for_array(index, cl->elems) {
AstNode *elem = cl->elems.e[index]; AstNode *elem = cl->elems.e[index];
if (is_elem_const(proc->module, elem, et)) { if (ssa_is_elem_const(proc->module, elem, et)) {
continue; continue;
} }
ssaValue *field_elem = ssa_build_expr(proc, elem); ssaValue *field_elem = ssa_build_expr(proc, elem);
@@ -3675,7 +3679,7 @@ ssaAddr ssa_build_addr(ssaProcedure *proc, AstNode *expr) {
} }
field = st->fields[index]; field = st->fields[index];
if (is_elem_const(proc->module, elem, field->type)) { if (ssa_is_elem_const(proc->module, elem, field->type)) {
continue; continue;
} }
@@ -3695,7 +3699,7 @@ ssaAddr ssa_build_addr(ssaProcedure *proc, AstNode *expr) {
ssa_emit_store(proc, v, ssa_add_module_constant(proc->module, type, make_exact_value_compound(expr))); ssa_emit_store(proc, v, ssa_add_module_constant(proc->module, type, make_exact_value_compound(expr)));
for_array(i, cl->elems) { for_array(i, cl->elems) {
AstNode *elem = cl->elems.e[i]; AstNode *elem = cl->elems.e[i];
if (is_elem_const(proc->module, elem, et)) { if (ssa_is_elem_const(proc->module, elem, et)) {
continue; continue;
} }
ssaValue *field_expr = ssa_build_expr(proc, elem); ssaValue *field_expr = ssa_build_expr(proc, elem);
@@ -3719,7 +3723,7 @@ ssaAddr ssa_build_addr(ssaProcedure *proc, AstNode *expr) {
for_array(i, cl->elems) { for_array(i, cl->elems) {
AstNode *elem = cl->elems.e[i]; AstNode *elem = cl->elems.e[i];
if (is_elem_const(proc->module, elem, et)) { if (ssa_is_elem_const(proc->module, elem, et)) {
continue; continue;
} }
@@ -3911,7 +3915,7 @@ void ssa_build_stmt_internal(ssaProcedure *proc, AstNode *node) {
case_ast_node(pd, ProcDecl, node); case_ast_node(pd, ProcDecl, node);
if (pd->body != NULL) { if (pd->body != NULL) {
auto *info = proc->module->info; CheckerInfo *info = proc->module->info;
Entity **found = map_entity_get(&info->definitions, hash_pointer(pd->name)); Entity **found = map_entity_get(&info->definitions, hash_pointer(pd->name));
GB_ASSERT_MSG(found != NULL, "Unable to find: %.*s", LIT(pd->name->Ident.string)); GB_ASSERT_MSG(found != NULL, "Unable to find: %.*s", LIT(pd->name->Ident.string));
@@ -3948,7 +3952,7 @@ void ssa_build_stmt_internal(ssaProcedure *proc, AstNode *node) {
array_add(&proc->children, &value->Proc); array_add(&proc->children, &value->Proc);
array_add(&proc->module->procs_to_generate, value); array_add(&proc->module->procs_to_generate, value);
} else { } else {
auto *info = proc->module->info; CheckerInfo *info = proc->module->info;
Entity **found = map_entity_get(&info->definitions, hash_pointer(pd->name)); Entity **found = map_entity_get(&info->definitions, hash_pointer(pd->name));
GB_ASSERT_MSG(found != NULL, "Unable to find: %.*s", LIT(pd->name->Ident.string)); GB_ASSERT_MSG(found != NULL, "Unable to find: %.*s", LIT(pd->name->Ident.string));
@@ -4709,7 +4713,7 @@ void ssa_init_module(ssaModule *m, Checker *c) {
isize count = 0; isize count = 0;
for_array(entry_index, m->info->type_info_map.entries) { for_array(entry_index, m->info->type_info_map.entries) {
auto *entry = &m->info->type_info_map.entries.e[entry_index]; MapIsizeEntry *entry = &m->info->type_info_map.entries.e[entry_index];
Type *t = cast(Type *)cast(uintptr)entry->key.key; Type *t = cast(Type *)cast(uintptr)entry->key.key;
switch (t->kind) { switch (t->kind) {
@@ -4824,6 +4828,17 @@ String ssa_mangle_name(ssaGen *s, String path, String name) {
return make_string(new_name, new_name_len-1); return make_string(new_name, new_name_len-1);
} }
ssaValue *ssa_get_type_info_ptr(ssaProcedure *proc, ssaValue *type_info_data, Type *type) {
i32 index = cast(i32)ssa_type_info_index(proc->module->info, type);
// gb_printf_err("%d %s\n", index, type_to_string(type));
return ssa_emit_array_ep(proc, type_info_data, index);
}
ssaValue *ssa_type_info_member_offset(ssaProcedure *proc, ssaValue *data, isize count, i32 *index) {
ssaValue *offset = ssa_emit_array_ep(proc, data, *index);
*index += count;
return offset;
}
void ssa_gen_tree(ssaGen *s) { void ssa_gen_tree(ssaGen *s) {
ssaModule *m = &s->module; ssaModule *m = &s->module;
@@ -4844,7 +4859,7 @@ void ssa_gen_tree(ssaGen *s) {
Entity *entry_point = NULL; Entity *entry_point = NULL;
for_array(i, info->entities.entries) { for_array(i, info->entities.entries) {
auto *entry = &info->entities.entries.e[i]; MapDeclInfoEntry *entry = &info->entities.entries.e[i];
Entity *e = cast(Entity *)cast(uintptr)entry->key.key; Entity *e = cast(Entity *)cast(uintptr)entry->key.key;
String name = e->token.string; String name = e->token.string;
if (e->kind == Entity_Variable) { if (e->kind == Entity_Variable) {
@@ -4866,7 +4881,7 @@ void ssa_gen_tree(ssaGen *s) {
m->min_dep_map = generate_minimum_dependency_map(info, entry_point); m->min_dep_map = generate_minimum_dependency_map(info, entry_point);
for_array(i, info->entities.entries) { for_array(i, info->entities.entries) {
auto *entry = &info->entities.entries.e[i]; MapDeclInfoEntry *entry = &info->entities.entries.e[i];
Entity *e = cast(Entity *)entry->key.ptr; Entity *e = cast(Entity *)entry->key.ptr;
String name = e->token.string; String name = e->token.string;
DeclInfo *decl = entry->value; DeclInfo *decl = entry->value;
@@ -4948,7 +4963,7 @@ void ssa_gen_tree(ssaGen *s) {
} }
for_array(i, m->members.entries) { for_array(i, m->members.entries) {
auto *entry = &m->members.entries.e[i]; MapSsaValueEntry *entry = &m->members.entries.e[i];
ssaValue *v = entry->value; ssaValue *v = entry->value;
if (v->kind == ssaValue_Proc) if (v->kind == ssaValue_Proc)
ssa_build_proc(v, NULL); ssa_build_proc(v, NULL);
@@ -4960,7 +4975,7 @@ void ssa_gen_tree(ssaGen *s) {
isize all_proc_max_count = 0; isize all_proc_max_count = 0;
for_array(i, m->debug_info.entries) { for_array(i, m->debug_info.entries) {
auto *entry = &m->debug_info.entries.e[i]; MapSsaDebugInfoEntry *entry = &m->debug_info.entries.e[i];
ssaDebugInfo *di = entry->value; ssaDebugInfo *di = entry->value;
di->id = i; di->id = i;
if (di->kind == ssaDebugInfo_Proc) { if (di->kind == ssaDebugInfo_Proc) {
@@ -4974,7 +4989,7 @@ void ssa_gen_tree(ssaGen *s) {
for_array(i, m->debug_info.entries) { for_array(i, m->debug_info.entries) {
auto *entry = &m->debug_info.entries.e[i]; MapSsaDebugInfoEntry *entry = &m->debug_info.entries.e[i];
ssaDebugInfo *di = entry->value; ssaDebugInfo *di = entry->value;
di->id = i; di->id = i;
if (di->kind == ssaDebugInfo_Proc) { if (di->kind == ssaDebugInfo_Proc) {
@@ -5050,24 +5065,10 @@ void ssa_gen_tree(ssaGen *s) {
Type *t_i64_slice_ptr = make_type_pointer(a, make_type_slice(a, t_i64)); Type *t_i64_slice_ptr = make_type_pointer(a, make_type_slice(a, t_i64));
Type *t_string_slice_ptr = make_type_pointer(a, make_type_slice(a, t_string)); Type *t_string_slice_ptr = make_type_pointer(a, make_type_slice(a, t_string));
auto get_type_info_ptr = [](ssaProcedure *proc, ssaValue *type_info_data, Type *type) -> ssaValue * {
i32 index = cast(i32)ssa_type_info_index(proc->module->info, type);
// gb_printf_err("%d %s\n", index, type_to_string(type));
return ssa_emit_array_ep(proc, type_info_data, index);
};
i32 type_info_member_index = 0; i32 type_info_member_index = 0;
auto type_info_member_offset = [](ssaProcedure *proc, ssaValue *data, isize count, i32 *index) -> ssaValue * {
ssaValue *offset = ssa_emit_array_ep(proc, data, *index);
*index += count;
return offset;
};
for_array(type_info_map_index, info->type_info_map.entries) { for_array(type_info_map_index, info->type_info_map.entries) {
auto *entry = &info->type_info_map.entries.e[type_info_map_index]; MapIsizeEntry *entry = &info->type_info_map.entries.e[type_info_map_index];
Type *t = cast(Type *)cast(uintptr)entry->key.key; Type *t = cast(Type *)cast(uintptr)entry->key.key;
t = default_type(t); t = default_type(t);
isize entry_index = entry->value; isize entry_index = entry->value;
@@ -5080,7 +5081,7 @@ void ssa_gen_tree(ssaGen *s) {
// TODO(bill): Which is better? The mangled name or actual name? // TODO(bill): Which is better? The mangled name or actual name?
ssaValue *name = ssa_make_const_string(a, t->Named.type_name->token.string); ssaValue *name = ssa_make_const_string(a, t->Named.type_name->token.string);
ssaValue *gtip = get_type_info_ptr(proc, type_info_data, t->Named.base); ssaValue *gtip = ssa_get_type_info_ptr(proc, type_info_data, t->Named.base);
ssa_emit_store(proc, ssa_emit_struct_ep(proc, tag, 0), name); ssa_emit_store(proc, ssa_emit_struct_ep(proc, tag, 0), name);
ssa_emit_store(proc, ssa_emit_struct_ep(proc, tag, 1), gtip); ssa_emit_store(proc, ssa_emit_struct_ep(proc, tag, 1), gtip);
@@ -5137,17 +5138,17 @@ void ssa_gen_tree(ssaGen *s) {
case Type_Pointer: { case Type_Pointer: {
tag = ssa_add_local_generated(proc, t_type_info_pointer); tag = ssa_add_local_generated(proc, t_type_info_pointer);
ssaValue *gep = get_type_info_ptr(proc, type_info_data, t->Pointer.elem); ssaValue *gep = ssa_get_type_info_ptr(proc, type_info_data, t->Pointer.elem);
ssa_emit_store(proc, ssa_emit_struct_ep(proc, tag, 0), gep); ssa_emit_store(proc, ssa_emit_struct_ep(proc, tag, 0), gep);
} break; } break;
case Type_Maybe: { case Type_Maybe: {
tag = ssa_add_local_generated(proc, t_type_info_maybe); tag = ssa_add_local_generated(proc, t_type_info_maybe);
ssaValue *gep = get_type_info_ptr(proc, type_info_data, t->Maybe.elem); ssaValue *gep = ssa_get_type_info_ptr(proc, type_info_data, t->Maybe.elem);
ssa_emit_store(proc, ssa_emit_struct_ep(proc, tag, 0), gep); ssa_emit_store(proc, ssa_emit_struct_ep(proc, tag, 0), gep);
} break; } break;
case Type_Array: { case Type_Array: {
tag = ssa_add_local_generated(proc, t_type_info_array); tag = ssa_add_local_generated(proc, t_type_info_array);
ssaValue *gep = get_type_info_ptr(proc, type_info_data, t->Array.elem); ssaValue *gep = ssa_get_type_info_ptr(proc, type_info_data, t->Array.elem);
ssa_emit_store(proc, ssa_emit_struct_ep(proc, tag, 0), gep); ssa_emit_store(proc, ssa_emit_struct_ep(proc, tag, 0), gep);
isize ez = type_size_of(m->sizes, a, t->Array.elem); isize ez = type_size_of(m->sizes, a, t->Array.elem);
@@ -5160,7 +5161,7 @@ void ssa_gen_tree(ssaGen *s) {
} break; } break;
case Type_Slice: { case Type_Slice: {
tag = ssa_add_local_generated(proc, t_type_info_slice); tag = ssa_add_local_generated(proc, t_type_info_slice);
ssaValue *gep = get_type_info_ptr(proc, type_info_data, t->Slice.elem); ssaValue *gep = ssa_get_type_info_ptr(proc, type_info_data, t->Slice.elem);
ssa_emit_store(proc, ssa_emit_struct_ep(proc, tag, 0), gep); ssa_emit_store(proc, ssa_emit_struct_ep(proc, tag, 0), gep);
isize ez = type_size_of(m->sizes, a, t->Slice.elem); isize ez = type_size_of(m->sizes, a, t->Slice.elem);
@@ -5170,7 +5171,7 @@ void ssa_gen_tree(ssaGen *s) {
} break; } break;
case Type_Vector: { case Type_Vector: {
tag = ssa_add_local_generated(proc, t_type_info_vector); tag = ssa_add_local_generated(proc, t_type_info_vector);
ssaValue *gep = get_type_info_ptr(proc, type_info_data, t->Vector.elem); ssaValue *gep = ssa_get_type_info_ptr(proc, type_info_data, t->Vector.elem);
ssa_emit_store(proc, ssa_emit_struct_ep(proc, tag, 0), gep); ssa_emit_store(proc, ssa_emit_struct_ep(proc, tag, 0), gep);
isize ez = type_size_of(m->sizes, a, t->Vector.elem); isize ez = type_size_of(m->sizes, a, t->Vector.elem);
@@ -5200,13 +5201,13 @@ void ssa_gen_tree(ssaGen *s) {
ssa_emit_store(proc, ssa_emit_struct_ep(proc, tag, 4), ordered); ssa_emit_store(proc, ssa_emit_struct_ep(proc, tag, 4), ordered);
} }
ssaValue *memory = type_info_member_offset(proc, type_info_member_data, t->Record.field_count, &type_info_member_index); ssaValue *memory = ssa_type_info_member_offset(proc, type_info_member_data, t->Record.field_count, &type_info_member_index);
type_set_offsets(m->sizes, a, t); // NOTE(bill): Just incase the offsets have not been set yet type_set_offsets(m->sizes, a, t); // NOTE(bill): Just incase the offsets have not been set yet
for (isize source_index = 0; source_index < t->Record.field_count; source_index++) { for (isize source_index = 0; source_index < t->Record.field_count; source_index++) {
// TODO(bill): Order fields in source order not layout order // TODO(bill): Order fields in source order not layout order
Entity *f = t->Record.fields_in_src_order[source_index]; Entity *f = t->Record.fields_in_src_order[source_index];
ssaValue *tip = get_type_info_ptr(proc, type_info_data, f->type); ssaValue *tip = ssa_get_type_info_ptr(proc, type_info_data, f->type);
i64 foffset = t->Record.struct_offsets[f->Variable.field_index]; i64 foffset = t->Record.struct_offsets[f->Variable.field_index];
GB_ASSERT(f->kind == Entity_Variable && f->flags & EntityFlag_Field); GB_ASSERT(f->kind == Entity_Variable && f->flags & EntityFlag_Field);
@@ -5253,7 +5254,7 @@ void ssa_gen_tree(ssaGen *s) {
ssa_emit_store(proc, ssa_emit_struct_ep(proc, tag, 2), align); ssa_emit_store(proc, ssa_emit_struct_ep(proc, tag, 2), align);
} }
ssaValue *memory = type_info_member_offset(proc, type_info_member_data, t->Record.field_count, &type_info_member_index); ssaValue *memory = ssa_type_info_member_offset(proc, type_info_member_data, t->Record.field_count, &type_info_member_index);
for (isize i = 0; i < t->Record.field_count; i++) { for (isize i = 0; i < t->Record.field_count; i++) {
ssaValue *field = ssa_emit_ptr_offset(proc, memory, ssa_make_const_int(a, i)); ssaValue *field = ssa_emit_ptr_offset(proc, memory, ssa_make_const_int(a, i));
@@ -5262,7 +5263,7 @@ void ssa_gen_tree(ssaGen *s) {
ssaValue *offset = ssa_emit_struct_ep(proc, field, 2); ssaValue *offset = ssa_emit_struct_ep(proc, field, 2);
Entity *f = t->Record.fields[i]; Entity *f = t->Record.fields[i];
ssaValue *tip = get_type_info_ptr(proc, type_info_data, f->type); ssaValue *tip = ssa_get_type_info_ptr(proc, type_info_data, f->type);
if (f->token.string.len > 0) { if (f->token.string.len > 0) {
ssa_emit_store(proc, name, ssa_make_const_string(a, f->token.string)); ssa_emit_store(proc, name, ssa_make_const_string(a, f->token.string));
@@ -5291,7 +5292,7 @@ void ssa_gen_tree(ssaGen *s) {
enum_base = t_int; enum_base = t_int;
} }
ssaValue *base = ssa_emit_struct_ep(proc, tag, 0); ssaValue *base = ssa_emit_struct_ep(proc, tag, 0);
ssa_emit_store(proc, base, get_type_info_ptr(proc, type_info_data, enum_base)); ssa_emit_store(proc, base, ssa_get_type_info_ptr(proc, type_info_data, enum_base));
if (t->Record.other_field_count > 0) { if (t->Record.other_field_count > 0) {
Entity **fields = t->Record.other_fields; Entity **fields = t->Record.other_fields;
@@ -5368,7 +5369,7 @@ void ssa_gen_tree(ssaGen *s) {
ssa_emit_store(proc, ssa_emit_struct_ep(proc, tag, 2), align); ssa_emit_store(proc, ssa_emit_struct_ep(proc, tag, 2), align);
} }
ssaValue *memory = type_info_member_offset(proc, type_info_member_data, t->Tuple.variable_count, &type_info_member_index); ssaValue *memory = ssa_type_info_member_offset(proc, type_info_member_data, t->Tuple.variable_count, &type_info_member_index);
for (isize i = 0; i < t->Tuple.variable_count; i++) { for (isize i = 0; i < t->Tuple.variable_count; i++) {
ssaValue *field = ssa_emit_ptr_offset(proc, memory, ssa_make_const_int(a, i)); ssaValue *field = ssa_emit_ptr_offset(proc, memory, ssa_make_const_int(a, i));
@@ -5377,7 +5378,7 @@ void ssa_gen_tree(ssaGen *s) {
// NOTE(bill): offset is not used for tuples // NOTE(bill): offset is not used for tuples
Entity *f = t->Tuple.variables[i]; Entity *f = t->Tuple.variables[i];
ssaValue *tip = get_type_info_ptr(proc, type_info_data, f->type); ssaValue *tip = ssa_get_type_info_ptr(proc, type_info_data, f->type);
if (f->token.string.len > 0) { if (f->token.string.len > 0) {
ssa_emit_store(proc, name, ssa_make_const_string(a, f->token.string)); ssa_emit_store(proc, name, ssa_make_const_string(a, f->token.string));
@@ -5407,10 +5408,10 @@ void ssa_gen_tree(ssaGen *s) {
ssaValue *variadic = ssa_emit_struct_ep(proc, tag, 2); ssaValue *variadic = ssa_emit_struct_ep(proc, tag, 2);
if (t->Proc.params) { if (t->Proc.params) {
ssa_emit_store(proc, params, get_type_info_ptr(proc, type_info_data, t->Proc.params)); ssa_emit_store(proc, params, ssa_get_type_info_ptr(proc, type_info_data, t->Proc.params));
} }
if (t->Proc.results) { if (t->Proc.results) {
ssa_emit_store(proc, results, get_type_info_ptr(proc, type_info_data, t->Proc.results)); ssa_emit_store(proc, results, ssa_get_type_info_ptr(proc, type_info_data, t->Proc.results));
} }
ssa_emit_store(proc, variadic, ssa_make_const_bool(a, t->Proc.variadic)); ssa_emit_store(proc, variadic, ssa_make_const_bool(a, t->Proc.variadic));
+9 -10
View File
@@ -142,7 +142,7 @@ ssaValueArray ssa_get_block_phi_nodes(ssaBlock *b) {
} }
void ssa_remove_pred(ssaBlock *b, ssaBlock *p) { void ssa_remove_pred(ssaBlock *b, ssaBlock *p) {
auto phis = ssa_get_block_phi_nodes(b); ssaValueArray phis = ssa_get_block_phi_nodes(b);
isize i = 0; isize i = 0;
for_array(j, b->preds) { for_array(j, b->preds) {
ssaBlock *pred = b->preds.e[j]; ssaBlock *pred = b->preds.e[j];
@@ -288,7 +288,7 @@ void ssa_opt_build_referrers(ssaProcedure *proc) {
if (op == NULL) { if (op == NULL) {
continue; continue;
} }
auto *refs = ssa_value_referrers(op); ssaValueArray *refs = ssa_value_referrers(op);
if (refs != NULL) { if (refs != NULL) {
array_add(refs, instr); array_add(refs, instr);
} }
@@ -307,13 +307,13 @@ void ssa_opt_build_referrers(ssaProcedure *proc) {
// State of Lengauer-Tarjan algorithm // State of Lengauer-Tarjan algorithm
// Based on this paper: http://jgaa.info/accepted/2006/GeorgiadisTarjanWerneck2006.10.1.pdf // Based on this paper: http://jgaa.info/accepted/2006/GeorgiadisTarjanWerneck2006.10.1.pdf
struct ssaLTState { typedef struct ssaLTState {
isize count; isize count;
// NOTE(bill): These are arrays // NOTE(bill): These are arrays
ssaBlock **sdom; // Semidominator ssaBlock **sdom; // Semidominator
ssaBlock **parent; // Parent in DFS traversal of CFG ssaBlock **parent; // Parent in DFS traversal of CFG
ssaBlock **ancestor; ssaBlock **ancestor;
}; } ssaLTState;
// §2.2 - bottom of page // §2.2 - bottom of page
void ssa_lt_link(ssaLTState *lt, ssaBlock *p, ssaBlock *q) { void ssa_lt_link(ssaLTState *lt, ssaBlock *p, ssaBlock *q) {
@@ -347,9 +347,9 @@ ssaBlock *ssa_lt_eval(ssaLTState *lt, ssaBlock *v) {
return u; return u;
} }
struct ssaDomPrePost { typedef struct ssaDomPrePost {
i32 pre, post; i32 pre, post;
}; } ssaDomPrePost;
ssaDomPrePost ssa_opt_number_dom_tree(ssaBlock *v, i32 pre, i32 post) { ssaDomPrePost ssa_opt_number_dom_tree(ssaBlock *v, i32 pre, i32 post) {
ssaDomPrePost result = {pre, post}; ssaDomPrePost result = {pre, post};
@@ -440,12 +440,11 @@ void ssa_opt_build_dom_tree(ssaProcedure *proc) {
} }
// Calculate children relation as inverse of idom // Calculate children relation as inverse of idom
auto *children = &w->dom.idom->dom.children; if (w->dom.idom->dom.children.e == NULL) {
if (children->e == NULL) {
// TODO(bill): Is this good enough for memory allocations? // TODO(bill): Is this good enough for memory allocations?
array_init(children, heap_allocator()); array_init(&w->dom.idom->dom.children, heap_allocator());
} }
array_add(children, w); array_add(&w->dom.idom->dom.children, w);
} }
} }
+2 -2
View File
@@ -1,8 +1,8 @@
struct ssaFileBuffer { typedef struct ssaFileBuffer {
gbVirtualMemory vm; gbVirtualMemory vm;
isize offset; isize offset;
gbFile * output; gbFile * output;
}; } ssaFileBuffer;
void ssa_file_buffer_init(ssaFileBuffer *f, gbFile *output) { void ssa_file_buffer_init(ssaFileBuffer *f, gbFile *output) {
isize size = 8*gb_virtual_memory_page_size(NULL); isize size = 8*gb_virtual_memory_page_size(NULL);
+4 -4
View File
@@ -1,14 +1,14 @@
struct TimeStamp { typedef struct TimeStamp {
u64 start; u64 start;
u64 finish; u64 finish;
String label; String label;
}; } TimeStamp;
struct Timings { typedef struct Timings {
TimeStamp total; TimeStamp total;
Array(TimeStamp) sections; Array(TimeStamp) sections;
u64 freq; u64 freq;
}; } Timings;
u64 win32_time_stamp_time_now(void) { u64 win32_time_stamp_time_now(void) {
+12 -12
View File
@@ -113,11 +113,11 @@ TOKEN_KIND(Token__KeywordBegin, "_KeywordBegin"), \
TOKEN_KIND(Token__KeywordEnd, "_KeywordEnd"), \ TOKEN_KIND(Token__KeywordEnd, "_KeywordEnd"), \
TOKEN_KIND(Token_Count, "") TOKEN_KIND(Token_Count, "")
enum TokenKind { typedef enum TokenKind {
#define TOKEN_KIND(e, s) e #define TOKEN_KIND(e, s) e
TOKEN_KINDS TOKEN_KINDS
#undef TOKEN_KIND #undef TOKEN_KIND
}; } TokenKind;
String const token_strings[] = { String const token_strings[] = {
#define TOKEN_KIND(e, s) {cast(u8 *)s, gb_size_of(s)-1} #define TOKEN_KIND(e, s) {cast(u8 *)s, gb_size_of(s)-1}
@@ -126,11 +126,11 @@ String const token_strings[] = {
}; };
struct TokenPos { typedef struct TokenPos {
String file; String file;
isize line; isize line;
isize column; isize column;
}; } TokenPos;
i32 token_pos_cmp(TokenPos a, TokenPos b) { i32 token_pos_cmp(TokenPos a, TokenPos b) {
if (a.line == b.line) { if (a.line == b.line) {
@@ -149,11 +149,11 @@ bool token_pos_are_equal(TokenPos a, TokenPos b) {
} }
// NOTE(bill): Text is UTF-8, thus why u8 and not char // NOTE(bill): Text is UTF-8, thus why u8 and not char
struct Token { typedef struct Token {
TokenKind kind; TokenKind kind;
String string; String string;
TokenPos pos; TokenPos pos;
}; } Token;
Token empty_token = {Token_Invalid}; Token empty_token = {Token_Invalid};
Token blank_token = {Token_Identifier, {cast(u8 *)"_", 1}}; Token blank_token = {Token_Identifier, {cast(u8 *)"_", 1}};
@@ -164,12 +164,12 @@ Token make_token_ident(String s) {
} }
struct ErrorCollector { typedef struct ErrorCollector {
TokenPos prev; TokenPos prev;
i64 count; i64 count;
i64 warning_count; i64 warning_count;
gbMutex mutex; gbMutex mutex;
}; } ErrorCollector;
gb_global ErrorCollector global_error_collector; gb_global ErrorCollector global_error_collector;
@@ -272,7 +272,7 @@ gb_inline bool token_is_shift(Token t) {
gb_inline void print_token(Token t) { gb_printf("%.*s\n", LIT(t.string)); } gb_inline void print_token(Token t) { gb_printf("%.*s\n", LIT(t.string)); }
enum TokenizerInitError { typedef enum TokenizerInitError {
TokenizerInit_None, TokenizerInit_None,
TokenizerInit_Invalid, TokenizerInit_Invalid,
@@ -281,10 +281,10 @@ enum TokenizerInitError {
TokenizerInit_Empty, TokenizerInit_Empty,
TokenizerInit_Count, TokenizerInit_Count,
}; } TokenizerInitError;
struct Tokenizer { typedef struct Tokenizer {
String fullpath; String fullpath;
u8 *start; u8 *start;
u8 *end; u8 *end;
@@ -297,7 +297,7 @@ struct Tokenizer {
isize error_count; isize error_count;
Array(String) allocated_strings; Array(String) allocated_strings;
}; } Tokenizer;
void tokenizer_err(Tokenizer *t, char *msg, ...) { void tokenizer_err(Tokenizer *t, char *msg, ...) {