Restart LLVM IR SSA generation

This is the third go and I'm going for it!
This commit is contained in:
gingerBill
2016-07-30 00:17:13 +01:00
parent 32ab8fcf99
commit 776dc0e8f1
20 changed files with 1843 additions and 507 deletions
+69 -79
View File
@@ -122,7 +122,6 @@ enum BuiltinProcedureId {
BuiltinProcedure_len,
BuiltinProcedure_cap,
BuiltinProcedure_copy,
BuiltinProcedure_copy_bytes,
BuiltinProcedure_print,
BuiltinProcedure_println,
@@ -146,7 +145,6 @@ gb_global BuiltinProcedure builtin_procedures[BuiltinProcedure_Count] = {
{STR_LIT("len"), 1, false, Expression_Expression},
{STR_LIT("cap"), 1, false, Expression_Expression},
{STR_LIT("copy"), 2, false, Expression_Expression},
{STR_LIT("copy_bytes"), 3, false, Expression_Statement},
{STR_LIT("print"), 1, true, Expression_Statement},
{STR_LIT("println"), 1, true, Expression_Statement},
};
@@ -156,14 +154,18 @@ struct CheckerContext {
DeclarationInfo *decl;
};
struct Checker {
Parser * parser;
struct CheckerInfo {
Map<TypeAndValue> types; // Key: AstNode * | Expression -> Type (and value)
Map<Entity *> definitions; // Key: AstNode * | Identifier -> Entity
Map<Entity *> uses; // Key: AstNode * | Identifier -> Entity
Map<Scope *> scopes; // Key: AstNode * | Node -> Scope
Map<ExpressionInfo> untyped; // Key: AstNode * | Expression -> ExpressionInfo
Map<DeclarationInfo *> entities; // Key: Entity *
};
struct Checker {
Parser * parser;
CheckerInfo info;
AstFile * curr_ast_file;
BaseTypeSizes sizes;
@@ -195,12 +197,13 @@ Scope *make_scope(Scope *parent, gbAllocator allocator) {
}
void destroy_scope(Scope *scope) {
isize element_count = gb_array_count(scope->elements.entries);
for (isize i = 0; i < element_count; i++) {
gb_for_array(i, scope->elements.entries) {
Entity *e =scope->elements.entries[i].value;
if (e->kind == Entity_Variable) {
if (!e->variable.used) {
#if 0
warning(e->token, "Unused variable `%.*s`", LIT(e->token.string));
#endif
}
}
}
@@ -262,7 +265,7 @@ void add_dependency(DeclarationInfo *d, Entity *e) {
void add_declaration_dependency(Checker *c, Entity *e) {
if (c->context.decl) {
auto found = map_get(&c->entities, hash_pointer(e));
auto found = map_get(&c->info.entities, hash_pointer(e));
if (found) {
add_dependency(c->context.decl, e);
}
@@ -324,22 +327,35 @@ void init_universal_scope(void) {
void init_checker_info(CheckerInfo *i) {
gbAllocator a = gb_heap_allocator();
map_init(&i->types, a);
map_init(&i->definitions, a);
map_init(&i->uses, a);
map_init(&i->scopes, a);
map_init(&i->entities, a);
map_init(&i->untyped, a);
}
void destroy_checker_info(CheckerInfo *i) {
map_destroy(&i->types);
map_destroy(&i->definitions);
map_destroy(&i->uses);
map_destroy(&i->scopes);
map_destroy(&i->entities);
map_destroy(&i->untyped);
}
void init_checker(Checker *c, Parser *parser) {
gbAllocator a = gb_heap_allocator();
c->parser = parser;
map_init(&c->types, gb_heap_allocator());
map_init(&c->definitions, gb_heap_allocator());
map_init(&c->uses, gb_heap_allocator());
map_init(&c->scopes, gb_heap_allocator());
map_init(&c->entities, gb_heap_allocator());
init_checker_info(&c->info);
c->sizes.word_size = 8;
c->sizes.max_align = 8;
map_init(&c->untyped, a);
gb_array_init(c->procedure_stack, a);
gb_array_init(c->procedures, a);
@@ -347,7 +363,7 @@ void init_checker(Checker *c, Parser *parser) {
// NOTE(bill): Is this big enough or too small?
isize item_size = gb_max(gb_max(gb_size_of(Entity), gb_size_of(Type)), gb_size_of(Scope));
isize total_token_count = 0;
for (isize i = 0; i < gb_array_count(c->parser->files); i++) {
gb_for_array(i, c->parser->files) {
AstFile *f = &c->parser->files[i];
total_token_count += gb_array_count(f->tokens);
}
@@ -360,12 +376,7 @@ void init_checker(Checker *c, Parser *parser) {
}
void destroy_checker(Checker *c) {
map_destroy(&c->types);
map_destroy(&c->definitions);
map_destroy(&c->uses);
map_destroy(&c->scopes);
map_destroy(&c->untyped);
map_destroy(&c->entities);
destroy_checker_info(&c->info);
destroy_scope(c->global_scope);
gb_array_free(c->procedure_stack);
gb_array_free(c->procedures);
@@ -374,30 +385,30 @@ void destroy_checker(Checker *c) {
}
TypeAndValue *type_and_value_of_expression(Checker *c, AstNode *expression) {
TypeAndValue *found = map_get(&c->types, hash_pointer(expression));
TypeAndValue *type_and_value_of_expression(CheckerInfo *i, AstNode *expression) {
TypeAndValue *found = map_get(&i->types, hash_pointer(expression));
return found;
}
Entity *entity_of_identifier(Checker *c, AstNode *identifier) {
Entity *entity_of_identifier(CheckerInfo *i, AstNode *identifier) {
GB_ASSERT(identifier->kind == AstNode_Identifier);
Entity **found = map_get(&c->definitions, hash_pointer(identifier));
Entity **found = map_get(&i->definitions, hash_pointer(identifier));
if (found)
return *found;
found = map_get(&c->uses, hash_pointer(identifier));
found = map_get(&i->uses, hash_pointer(identifier));
if (found)
return *found;
return NULL;
}
Type *type_of_expression(Checker *c, AstNode *expression) {
TypeAndValue *found = type_and_value_of_expression(c, expression);
Type *type_of_expression(CheckerInfo *i, AstNode *expression) {
TypeAndValue *found = type_and_value_of_expression(i, expression);
if (found)
return found->type;
if (expression->kind == AstNode_Identifier) {
Entity *entity = entity_of_identifier(c, expression);
Entity *entity = entity_of_identifier(i, expression);
if (entity)
return entity->type;
}
@@ -406,12 +417,12 @@ Type *type_of_expression(Checker *c, AstNode *expression) {
}
void add_untyped(Checker *c, AstNode *expression, b32 lhs, AddressingMode mode, Type *basic_type, ExactValue value) {
map_set(&c->untyped, hash_pointer(expression), make_expression_info(lhs, mode, basic_type, value));
void add_untyped(CheckerInfo *i, AstNode *expression, b32 lhs, AddressingMode mode, Type *basic_type, ExactValue value) {
map_set(&i->untyped, hash_pointer(expression), make_expression_info(lhs, mode, basic_type, value));
}
void add_type_and_value(Checker *c, AstNode *expression, AddressingMode mode, Type *type, ExactValue value) {
void add_type_and_value(CheckerInfo *i, AstNode *expression, AddressingMode mode, Type *type, ExactValue value) {
GB_ASSERT(expression != NULL);
GB_ASSERT(type != NULL);
if (mode == Addressing_Invalid)
@@ -425,14 +436,14 @@ void add_type_and_value(Checker *c, AstNode *expression, AddressingMode mode, Ty
TypeAndValue tv = {};
tv.type = type;
tv.value = value;
map_set(&c->types, hash_pointer(expression), tv);
map_set(&i->types, hash_pointer(expression), tv);
}
void add_entity_definition(Checker *c, AstNode *identifier, Entity *entity) {
void add_entity_definition(CheckerInfo *i, AstNode *identifier, Entity *entity) {
GB_ASSERT(identifier != NULL);
GB_ASSERT(identifier->kind == AstNode_Identifier);
u64 key = hash_pointer(identifier);
map_set(&c->definitions, key, entity);
map_set(&i->definitions, key, entity);
}
void add_entity(Checker *c, Scope *scope, AstNode *identifier, Entity *entity) {
@@ -444,24 +455,23 @@ void add_entity(Checker *c, Scope *scope, AstNode *identifier, Entity *entity) {
}
}
if (identifier != NULL)
add_entity_definition(c, identifier, entity);
add_entity_definition(&c->info, identifier, entity);
}
void add_entity_use(Checker *c, AstNode *identifier, Entity *entity) {
void add_entity_use(CheckerInfo *i, AstNode *identifier, Entity *entity) {
GB_ASSERT(identifier != NULL);
GB_ASSERT(identifier->kind == AstNode_Identifier);
u64 key = hash_pointer(identifier);
map_set(&c->uses, key, entity);
map_set(&i->uses, key, entity);
}
void add_file_entity(Checker *c, AstNode *identifier, Entity *e, DeclarationInfo *d) {
GB_ASSERT(are_strings_equal(identifier->identifier.token.string, e->token.string));
add_entity(c, c->global_scope, identifier, e);
map_set(&c->entities, hash_pointer(e), d);
e->order = gb_array_count(c->entities.entries);
map_set(&c->info.entities, hash_pointer(e), d);
e->order = gb_array_count(c->info.entities.entries);
}
@@ -480,7 +490,7 @@ void check_procedure_later(Checker *c, AstFile *file, Token token, DeclarationIn
void add_scope(Checker *c, AstNode *node, Scope *scope) {
GB_ASSERT(node != NULL);
GB_ASSERT(scope != NULL);
map_set(&c->scopes, hash_pointer(node), scope);
map_set(&c->info.scopes, hash_pointer(node), scope);
}
@@ -518,16 +528,9 @@ void add_curr_ast_file(Checker *c, AstFile *file) {
GB_COMPARE_PROC(entity_order_cmp) {
Entity const *p = cast(Entity const *)a;
Entity const *q = cast(Entity const *)b;
return p->order < q->order ? -1 : p->order > q->order;
}
void check_parsed_files(Checker *c) {
// Collect Entities
for (isize i = 0; i < gb_array_count(c->parser->files); i++) {
gb_for_array(i, c->parser->files) {
AstFile *f = &c->parser->files[i];
add_curr_ast_file(c, f);
for (AstNode *decl = f->declarations; decl != NULL; decl = decl->next) {
@@ -625,8 +628,8 @@ void check_parsed_files(Checker *c) {
add_entity(c, c->global_scope, identifier, e);
DeclarationInfo *d = make_declaration_info(c->allocator, e->parent);
d->proc_decl = decl;
map_set(&c->entities, hash_pointer(e), d);
e->order = gb_array_count(c->entities.entries);
map_set(&c->info.entities, hash_pointer(e), d);
e->order = gb_array_count(c->info.entities.entries);
} break;
@@ -641,46 +644,33 @@ void check_parsed_files(Checker *c) {
}
}
{ // Order entities
gbArray(Entity *) entities;
isize count = gb_array_count(c->entities.entries);
gb_array_init_reserve(entities, gb_heap_allocator(), count);
defer (gb_array_free(entities));
for (isize i = 0; i < count; i++) {
u64 key = c->entities.entries[i].key;
Entity *e = cast(Entity *)cast(uintptr)key;
gb_array_append(entities, e);
}
gb_sort_array(entities, count, entity_order_cmp);
for (isize i = 0; i < count; i++) {
check_entity_declaration(c, entities[i], NULL);
}
gb_for_array(i, c->info.entities.entries) {
auto *entry = &c->info.entities.entries[i];
Entity *e = cast(Entity *)cast(uintptr)entry->key;
DeclarationInfo *d = entry->value;
check_entity_declaration(c, e, d, NULL);
}
// Check procedure bodies
for (isize i = 0; i < gb_array_count(c->procedures); i++) {
gb_for_array(i, c->procedures) {
ProcedureInfo *pi = &c->procedures[i];
add_curr_ast_file(c, pi->file);
check_procedure_body(c, pi->token, pi->decl, pi->type, pi->body);
}
{ // Add untyped expression values
isize count = gb_array_count(c->untyped.entries);
for (isize i = 0; i < count; i++) {
auto *entry = c->untyped.entries + i;
u64 key = entry->key;
AstNode *expr = cast(AstNode *)cast(uintptr)key;
ExpressionInfo *info = &entry->value;
if (is_type_typed(info->type)) {
GB_PANIC("%s (type %s) is typed!", expression_to_string(expr), info->type);
}
add_type_and_value(c, expr, info->mode, info->type, info->value);
// Add untyped expression values
gb_for_array(i, c->info.untyped.entries) {
auto *entry = c->info.untyped.entries + i;
u64 key = entry->key;
AstNode *expr = cast(AstNode *)cast(uintptr)key;
ExpressionInfo *info = &entry->value;
if (is_type_typed(info->type)) {
GB_PANIC("%s (type %s) is typed!", expression_to_string(expr), info->type);
}
add_type_and_value(&c->info, expr, info->mode, info->type, info->value);
}
}
+21 -10
View File
@@ -2,19 +2,30 @@ struct Scope;
struct Checker;
enum BuiltinProcedureId;
#define ENTITY_KINDS \
ENTITY_KIND(Invalid), \
ENTITY_KIND(Constant), \
ENTITY_KIND(Variable), \
ENTITY_KIND(TypeName), \
ENTITY_KIND(AliasName), \
ENTITY_KIND(Procedure), \
ENTITY_KIND(Builtin), \
ENTITY_KIND(Count),
enum EntityKind {
Entity_Invalid,
Entity_Constant,
Entity_Variable,
Entity_TypeName,
Entity_AliasName,
Entity_Procedure,
Entity_Builtin,
Entity_Count,
#define ENTITY_KIND(k) GB_JOIN2(Entity_, k)
ENTITY_KINDS
#undef ENTITY_KIND
};
String const entity_strings[] = {
#define ENTITY_KIND(k) {cast(u8 *)#k, gb_size_of(#k)-1}
ENTITY_KINDS
#undef ENTITY_KIND
};
typedef i64 EntityGuid;
struct Entity {
+18 -58
View File
@@ -9,8 +9,8 @@ void check_selector (Checker *c, Operand *operand, AstNode *n
void check_not_tuple (Checker *c, Operand *operand);
void convert_to_typed (Checker *c, Operand *operand, Type *target_type);
gbString expression_to_string (AstNode *expression);
void check_entity_declaration(Checker *c, Entity *e, Type *named_type);
void check_procedure_body(Checker *c, Token token, DeclarationInfo *decl, Type *type, AstNode *body);
void check_entity_declaration(Checker *c, Entity *e, DeclarationInfo *decl, Type *named_type);
void check_procedure_body (Checker *c, Token token, DeclarationInfo *decl, Type *type, AstNode *body);
void check_struct_type(Checker *c, Type *struct_type, AstNode *node) {
@@ -51,7 +51,7 @@ void check_struct_type(Checker *c, Type *struct_type, AstNode *node) {
map_set(&entity_map, key, e);
fields[field_index++] = e;
}
add_entity_use(c, name, e);
add_entity_use(&c->info, name, e);
}
}
struct_type->structure.fields = fields;
@@ -143,9 +143,16 @@ void check_identifier(Checker *c, Operand *o, AstNode *n, Type *named_type) {
"Undeclared type or identifier `%.*s`", LIT(n->identifier.token.string));
return;
}
add_entity_use(c, n, e);
add_entity_use(&c->info, n, e);
check_entity_declaration(c, e, named_type);
if (e->type == NULL) {
auto *found = map_get(&c->info.entities, hash_pointer(e));
if (found != NULL) {
check_entity_declaration(c, e, *found, named_type);
} else {
GB_PANIC("Internal Compiler Error: DeclarationInfo not found!");
}
}
if (e->type == NULL) {
GB_PANIC("Compiler error: How did this happen? type: %s; identifier: %.*s\n", type_to_string(e->type), LIT(n->identifier.token.string));
@@ -386,7 +393,7 @@ Type *check_type(Checker *c, AstNode *e, Type *named_type) {
end:
GB_ASSERT(is_type_typed(type));
add_type_and_value(c, e, Addressing_Type, type, null_value);
add_type_and_value(&c->info, e, Addressing_Type, type, null_value);
return type;
}
@@ -758,7 +765,7 @@ void check_binary_expression(Checker *c, Operand *x, AstNode *node) {
void update_expression_type(Checker *c, AstNode *e, Type *type) {
ExpressionInfo *found = map_get(&c->untyped, hash_pointer(e));
ExpressionInfo *found = map_get(&c->info.untyped, hash_pointer(e));
if (!found)
return;
@@ -786,7 +793,7 @@ void update_expression_type(Checker *c, AstNode *e, Type *type) {
}
void update_expression_value(Checker *c, AstNode *e, ExactValue value) {
ExpressionInfo *found = map_get(&c->untyped, hash_pointer(e));
ExpressionInfo *found = map_get(&c->info.untyped, hash_pointer(e));
if (found)
found->value = value;
}
@@ -977,7 +984,7 @@ void check_selector(Checker *c, Operand *operand, AstNode *node) {
operand->expression = node;
return;
}
add_entity_use(c, selector, entity);
add_entity_use(&c->info, selector, entity);
operand->type = entity->type;
operand->expression = node;
@@ -1241,53 +1248,6 @@ b32 check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id)
operand->mode = Addressing_Value;
} break;
case BuiltinProcedure_copy_bytes: {
// copy_bytes :: proc(dest, source: rawptr, byte_count: int)
Type *dest_type = NULL, *src_type = NULL;
Type *d = get_base_type(operand->type);
if (is_type_pointer(d))
dest_type = d;
Operand op = {};
check_expression(c, &op, ce->arg_list->next);
if (op.mode == Addressing_Invalid)
return false;
Type *s = get_base_type(op.type);
if (is_type_pointer(s))
src_type = s;
if (dest_type == NULL || src_type == NULL) {
error(&c->error_collector, ast_node_token(call), "`copy_bytes` only expects pointers for the destintation and source");
return false;
}
check_expression(c, &op, ce->arg_list->next->next);
if (op.mode == Addressing_Invalid)
return false;
convert_to_typed(c, &op, &basic_types[Basic_int]);
if (op.mode == Addressing_Invalid ||
op.type->kind != Type_Basic ||
op.type->basic.kind != Basic_int) {
gbString str = type_to_string(op.type);
defer (gb_string_free(str));
error(&c->error_collector, ast_node_token(call), "`copy_bytes` 3rd argument must be of type `int`, a `%s` was given", str);
return false;
}
if (op.mode == Addressing_Constant) {
if (exact_value_to_integer(op.value).value_integer <= 0) {
error(&c->error_collector, ast_node_token(call), "You cannot copy a zero or negative amount of bytes with `copy_bytes`");
return false;
}
}
operand->type = NULL;
operand->mode = Addressing_NoValue;
} break;
case BuiltinProcedure_print:
case BuiltinProcedure_println: {
for (AstNode *arg = ce->arg_list; arg != NULL; arg = arg->next) {
@@ -1896,9 +1856,9 @@ ExpressionKind check_expression_base(Checker *c, Operand *o, AstNode *node, Type
if (type != NULL) {
if (is_type_untyped(type)) {
add_untyped(c, node, false, o->mode, type, value);
add_untyped(&c->info, node, false, o->mode, type, value);
} else {
add_type_and_value(c, node, o->mode, type, value);
add_type_and_value(&c->info, node, o->mode, type, value);
}
}
return kind;
+8 -16
View File
@@ -172,7 +172,7 @@ Type *check_assignment_variable(Checker *c, Operand *op_a, AstNode *lhs) {
// NOTE(bill): Ignore assignments to `_`
if (node->kind == AstNode_Identifier &&
are_strings_equal(node->identifier.token.string, make_string("_"))) {
add_entity_definition(c, node, NULL);
add_entity_definition(&c->info, node, NULL);
check_assignment(c, op_a, NULL, make_string("assignment to `_` identifier"));
if (op_a->mode == Addressing_Invalid)
return NULL;
@@ -279,9 +279,8 @@ void check_init_variables(Checker *c, Entity **lhs, isize lhs_count, AstNode *in
}
}
if (i < lhs_count && i < init_count) {
if (lhs[i]->type == NULL)
error(&c->error_collector, lhs[i]->token, "Too few values on the right hand side of the declaration");
if (i < lhs_count && lhs[i]->type == NULL) {
error(&c->error_collector, lhs[i]->token, "Too few values on the right hand side of the declaration");
} else if (rhs != NULL) {
error(&c->error_collector, ast_node_token(rhs), "Too many values on the right hand side of the declaration");
}
@@ -401,9 +400,9 @@ void check_procedure_declaration(Checker *c, Entity *e, DeclarationInfo *d, b32
#if 1
Scope *original_curr_scope = c->context.scope;
c->context.scope = c->global_scope;
check_open_scope(c, pd->procedure_type);
check_open_scope(c, pd->type);
#endif
check_procedure_type(c, proc_type, pd->procedure_type);
check_procedure_type(c, proc_type, pd->type);
b32 is_foreign = false;
b32 is_inline = false;
b32 is_no_inline = false;
@@ -487,16 +486,9 @@ void check_variable_declaration(Checker *c, Entity *e, Entity **entities, isize
void check_entity_declaration(Checker *c, Entity *e, Type *named_type) {
void check_entity_declaration(Checker *c, Entity *e, DeclarationInfo *d, Type *named_type) {
if (e->type != NULL)
return;
DeclarationInfo **found = map_get(&c->entities, hash_pointer(e));
if (found == NULL) {
GB_PANIC("Compiler error: This entity should be declared!");
}
DeclarationInfo *d = *found;
switch (e->kind) {
case Entity_Constant:
c->context.decl = d;
@@ -580,6 +572,7 @@ void check_statement(Checker *c, AstNode *node, u32 flags) {
basic_lit.basic_literal = s->op;
basic_lit.basic_literal.kind = Token_Integer;
basic_lit.basic_literal.string = make_string("1");
AstNode be = {AstNode_BinaryExpression};
be.binary_expression.op = op;
be.binary_expression.left = s->expression;;
@@ -801,7 +794,7 @@ void check_statement(Checker *c, AstNode *node, u32 flags) {
if (!can_be_ignored) {
new_entities[new_entity_count++] = entity;
}
add_entity_definition(c, name, entity);
add_entity_definition(&c->info, name, entity);
} else {
entity = found;
}
@@ -833,7 +826,6 @@ void check_statement(Checker *c, AstNode *node, u32 flags) {
e->type = init_type;
}
check_init_variables(c, entities, entity_count, vd->value_list, vd->value_count, make_string("variable declaration"));
AstNode *name = vd->name_list;
+24 -13
View File
@@ -51,21 +51,32 @@ struct BasicType {
};
#define TYPE_KINDS \
TYPE_KIND(Invalid), \
TYPE_KIND(Basic), \
TYPE_KIND(Array), \
TYPE_KIND(Slice), \
TYPE_KIND(Structure), \
TYPE_KIND(Pointer), \
TYPE_KIND(Named), \
TYPE_KIND(Alias), \
TYPE_KIND(Tuple), \
TYPE_KIND(Procedure), \
TYPE_KIND(Count),
enum TypeKind {
Type_Invalid,
Type_Basic,
Type_Array,
Type_Slice,
Type_Structure,
Type_Pointer,
Type_Named,
Type_Alias,
Type_Tuple,
Type_Procedure,
Type_Count,
#define TYPE_KIND(k) GB_JOIN2(Type_, k)
TYPE_KINDS
#undef TYPE_KIND
};
String const type_strings[] = {
#define TYPE_KIND(k) {cast(u8 *)#k, gb_size_of(#k)-1}
TYPE_KINDS
#undef TYPE_KIND
};
struct Type {
TypeKind kind;
union {