mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-05 07:08:48 +00:00
Big Refactor to type less :P
This commit is contained in:
@@ -1,6 +1,4 @@
|
|||||||
define void @main() {
|
define void @main() {
|
||||||
entry:
|
entry:
|
||||||
%0 = alloca i64, align 8 ; a
|
|
||||||
store i64 zeroinitializer, i64* %0
|
|
||||||
ret void
|
ret void
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,2 @@
|
|||||||
main :: proc() {
|
main :: proc() {
|
||||||
a : int;
|
|
||||||
}
|
}
|
||||||
|
|||||||
+56
-57
@@ -19,8 +19,7 @@ struct Operand {
|
|||||||
AddressingMode mode;
|
AddressingMode mode;
|
||||||
Type *type;
|
Type *type;
|
||||||
ExactValue value;
|
ExactValue value;
|
||||||
|
AstNode *expr;
|
||||||
AstNode *expression;
|
|
||||||
BuiltinProcedureId builtin_id;
|
BuiltinProcedureId builtin_id;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -30,7 +29,7 @@ struct TypeAndValue {
|
|||||||
ExactValue value;
|
ExactValue value;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct DeclarationInfo {
|
struct DeclInfo {
|
||||||
Scope *scope;
|
Scope *scope;
|
||||||
|
|
||||||
Entity **entities;
|
Entity **entities;
|
||||||
@@ -45,26 +44,26 @@ struct DeclarationInfo {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
void init_declaration_info(DeclarationInfo *d, Scope *scope) {
|
void init_declaration_info(DeclInfo *d, Scope *scope) {
|
||||||
d->scope = scope;
|
d->scope = scope;
|
||||||
map_init(&d->deps, gb_heap_allocator());
|
map_init(&d->deps, gb_heap_allocator());
|
||||||
}
|
}
|
||||||
|
|
||||||
DeclarationInfo *make_declaration_info(gbAllocator a, Scope *scope) {
|
DeclInfo *make_declaration_info(gbAllocator a, Scope *scope) {
|
||||||
DeclarationInfo *d = gb_alloc_item(a, DeclarationInfo);
|
DeclInfo *d = gb_alloc_item(a, DeclInfo);
|
||||||
init_declaration_info(d, scope);
|
init_declaration_info(d, scope);
|
||||||
return d;
|
return d;
|
||||||
}
|
}
|
||||||
|
|
||||||
void destroy_declaration_info(DeclarationInfo *d) {
|
void destroy_declaration_info(DeclInfo *d) {
|
||||||
map_destroy(&d->deps);
|
map_destroy(&d->deps);
|
||||||
}
|
}
|
||||||
|
|
||||||
b32 has_init(DeclarationInfo *d) {
|
b32 has_init(DeclInfo *d) {
|
||||||
if (d->init_expr != NULL)
|
if (d->init_expr != NULL)
|
||||||
return true;
|
return true;
|
||||||
if (d->proc_decl != NULL) {
|
if (d->proc_decl != NULL) {
|
||||||
if (d->proc_decl->procedure_declaration.body != NULL)
|
if (d->proc_decl->proc_decl.body != NULL)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,7 +90,7 @@ ExpressionInfo make_expression_info(b32 is_lhs, AddressingMode mode, Type *type,
|
|||||||
struct ProcedureInfo {
|
struct ProcedureInfo {
|
||||||
AstFile *file;
|
AstFile *file;
|
||||||
Token token;
|
Token token;
|
||||||
DeclarationInfo *decl;
|
DeclInfo *decl;
|
||||||
Type * type; // Type_Procedure
|
Type * type; // Type_Procedure
|
||||||
AstNode * body; // AstNode_BlockStatement
|
AstNode * body; // AstNode_BlockStatement
|
||||||
};
|
};
|
||||||
@@ -151,7 +150,7 @@ gb_global BuiltinProcedure builtin_procedures[BuiltinProcedure_Count] = {
|
|||||||
|
|
||||||
struct CheckerContext {
|
struct CheckerContext {
|
||||||
Scope *scope;
|
Scope *scope;
|
||||||
DeclarationInfo *decl;
|
DeclInfo *decl;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct CheckerInfo {
|
struct CheckerInfo {
|
||||||
@@ -160,7 +159,7 @@ struct CheckerInfo {
|
|||||||
Map<Entity *> uses; // Key: AstNode * | Identifier -> Entity
|
Map<Entity *> uses; // Key: AstNode * | Identifier -> Entity
|
||||||
Map<Scope *> scopes; // Key: AstNode * | Node -> Scope
|
Map<Scope *> scopes; // Key: AstNode * | Node -> Scope
|
||||||
Map<ExpressionInfo> untyped; // Key: AstNode * | Expression -> ExpressionInfo
|
Map<ExpressionInfo> untyped; // Key: AstNode * | Expression -> ExpressionInfo
|
||||||
Map<DeclarationInfo *> entities; // Key: Entity *
|
Map<DeclInfo *> entities; // Key: Entity *
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Checker {
|
struct Checker {
|
||||||
@@ -259,7 +258,7 @@ Entity *scope_insert_entity(Scope *s, Entity *entity) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void add_dependency(DeclarationInfo *d, Entity *e) {
|
void add_dependency(DeclInfo *d, Entity *e) {
|
||||||
map_set(&d->deps, hash_pointer(e), cast(b32)true);
|
map_set(&d->deps, hash_pointer(e), cast(b32)true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -391,8 +390,8 @@ TypeAndValue *type_and_value_of_expression(CheckerInfo *i, AstNode *expression)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Entity *entity_of_identifier(CheckerInfo *i, AstNode *identifier) {
|
Entity *entity_of_ident(CheckerInfo *i, AstNode *identifier) {
|
||||||
GB_ASSERT(identifier->kind == AstNode_Identifier);
|
GB_ASSERT(identifier->kind == AstNode_Ident);
|
||||||
Entity **found = map_get(&i->definitions, hash_pointer(identifier));
|
Entity **found = map_get(&i->definitions, hash_pointer(identifier));
|
||||||
if (found)
|
if (found)
|
||||||
return *found;
|
return *found;
|
||||||
@@ -403,12 +402,12 @@ Entity *entity_of_identifier(CheckerInfo *i, AstNode *identifier) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
Type *type_of_expression(CheckerInfo *i, AstNode *expression) {
|
Type *type_of_expr(CheckerInfo *i, AstNode *expression) {
|
||||||
TypeAndValue *found = type_and_value_of_expression(i, expression);
|
TypeAndValue *found = type_and_value_of_expression(i, expression);
|
||||||
if (found)
|
if (found)
|
||||||
return found->type;
|
return found->type;
|
||||||
if (expression->kind == AstNode_Identifier) {
|
if (expression->kind == AstNode_Ident) {
|
||||||
Entity *entity = entity_of_identifier(i, expression);
|
Entity *entity = entity_of_ident(i, expression);
|
||||||
if (entity)
|
if (entity)
|
||||||
return entity->type;
|
return entity->type;
|
||||||
}
|
}
|
||||||
@@ -441,7 +440,7 @@ void add_type_and_value(CheckerInfo *i, AstNode *expression, AddressingMode mode
|
|||||||
|
|
||||||
void add_entity_definition(CheckerInfo *i, AstNode *identifier, Entity *entity) {
|
void add_entity_definition(CheckerInfo *i, AstNode *identifier, Entity *entity) {
|
||||||
GB_ASSERT(identifier != NULL);
|
GB_ASSERT(identifier != NULL);
|
||||||
GB_ASSERT(identifier->kind == AstNode_Identifier);
|
GB_ASSERT(identifier->kind == AstNode_Ident);
|
||||||
u64 key = hash_pointer(identifier);
|
u64 key = hash_pointer(identifier);
|
||||||
map_set(&i->definitions, key, entity);
|
map_set(&i->definitions, key, entity);
|
||||||
}
|
}
|
||||||
@@ -460,14 +459,14 @@ void add_entity(Checker *c, Scope *scope, AstNode *identifier, Entity *entity) {
|
|||||||
|
|
||||||
void add_entity_use(CheckerInfo *i, AstNode *identifier, Entity *entity) {
|
void add_entity_use(CheckerInfo *i, AstNode *identifier, Entity *entity) {
|
||||||
GB_ASSERT(identifier != NULL);
|
GB_ASSERT(identifier != NULL);
|
||||||
GB_ASSERT(identifier->kind == AstNode_Identifier);
|
GB_ASSERT(identifier->kind == AstNode_Ident);
|
||||||
u64 key = hash_pointer(identifier);
|
u64 key = hash_pointer(identifier);
|
||||||
map_set(&i->uses, key, entity);
|
map_set(&i->uses, key, entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void add_file_entity(Checker *c, AstNode *identifier, Entity *e, DeclarationInfo *d) {
|
void add_file_entity(Checker *c, AstNode *identifier, Entity *e, DeclInfo *d) {
|
||||||
GB_ASSERT(are_strings_equal(identifier->identifier.token.string, e->token.string));
|
GB_ASSERT(are_strings_equal(identifier->ident.token.string, e->token.string));
|
||||||
|
|
||||||
add_entity(c, c->global_scope, identifier, e);
|
add_entity(c, c->global_scope, identifier, e);
|
||||||
map_set(&c->info.entities, hash_pointer(e), d);
|
map_set(&c->info.entities, hash_pointer(e), d);
|
||||||
@@ -475,7 +474,7 @@ void add_file_entity(Checker *c, AstNode *identifier, Entity *e, DeclarationInfo
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void check_procedure_later(Checker *c, AstFile *file, Token token, DeclarationInfo *decl, Type *type, AstNode *body) {
|
void check_procedure_later(Checker *c, AstFile *file, Token token, DeclInfo *decl, Type *type, AstNode *body) {
|
||||||
ProcedureInfo info = {};
|
ProcedureInfo info = {};
|
||||||
info.file = file;
|
info.file = file;
|
||||||
info.token = token;
|
info.token = token;
|
||||||
@@ -533,27 +532,27 @@ void check_parsed_files(Checker *c) {
|
|||||||
gb_for_array(i, c->parser->files) {
|
gb_for_array(i, c->parser->files) {
|
||||||
AstFile *f = &c->parser->files[i];
|
AstFile *f = &c->parser->files[i];
|
||||||
add_curr_ast_file(c, f);
|
add_curr_ast_file(c, f);
|
||||||
for (AstNode *decl = f->declarations; decl != NULL; decl = decl->next) {
|
for (AstNode *decl = f->decls; decl != NULL; decl = decl->next) {
|
||||||
if (!is_ast_node_declaration(decl))
|
if (!is_ast_node_decl(decl))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
switch (decl->kind) {
|
switch (decl->kind) {
|
||||||
case AstNode_BadDeclaration:
|
case AstNode_BadDecl:
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case AstNode_VariableDeclaration: {
|
case AstNode_VarDecl: {
|
||||||
auto *vd = &decl->variable_declaration;
|
auto *vd = &decl->var_decl;
|
||||||
|
|
||||||
switch (vd->kind) {
|
switch (vd->kind) {
|
||||||
case Declaration_Immutable: {
|
case Declaration_Immutable: {
|
||||||
for (AstNode *name = vd->name_list, *value = vd->value_list;
|
for (AstNode *name = vd->name_list, *value = vd->value_list;
|
||||||
name != NULL && value != NULL;
|
name != NULL && value != NULL;
|
||||||
name = name->next, value = value->next) {
|
name = name->next, value = value->next) {
|
||||||
GB_ASSERT(name->kind == AstNode_Identifier);
|
GB_ASSERT(name->kind == AstNode_Ident);
|
||||||
ExactValue v = {ExactValue_Invalid};
|
ExactValue v = {ExactValue_Invalid};
|
||||||
Entity *e = make_entity_constant(c->allocator, c->context.scope, name->identifier.token, NULL, v);
|
Entity *e = make_entity_constant(c->allocator, c->context.scope, name->ident.token, NULL, v);
|
||||||
DeclarationInfo *di = make_declaration_info(c->allocator, c->global_scope);
|
DeclInfo *di = make_declaration_info(c->allocator, c->global_scope);
|
||||||
di->type_expr = vd->type_expression;
|
di->type_expr = vd->type;
|
||||||
di->init_expr = value;
|
di->init_expr = value;
|
||||||
add_file_entity(c, name, e, di);
|
add_file_entity(c, name, e, di);
|
||||||
}
|
}
|
||||||
@@ -561,7 +560,7 @@ void check_parsed_files(Checker *c) {
|
|||||||
isize lhs_count = vd->name_count;
|
isize lhs_count = vd->name_count;
|
||||||
isize rhs_count = vd->value_count;
|
isize rhs_count = vd->value_count;
|
||||||
|
|
||||||
if (rhs_count == 0 && vd->type_expression == NULL) {
|
if (rhs_count == 0 && vd->type == NULL) {
|
||||||
error(&c->error_collector, ast_node_token(decl), "Missing type or initial expression");
|
error(&c->error_collector, ast_node_token(decl), "Missing type or initial expression");
|
||||||
} else if (lhs_count < rhs_count) {
|
} else if (lhs_count < rhs_count) {
|
||||||
error(&c->error_collector, ast_node_token(decl), "Extra initial expression");
|
error(&c->error_collector, ast_node_token(decl), "Extra initial expression");
|
||||||
@@ -572,25 +571,25 @@ void check_parsed_files(Checker *c) {
|
|||||||
isize entity_count = vd->name_count;
|
isize entity_count = vd->name_count;
|
||||||
isize entity_index = 0;
|
isize entity_index = 0;
|
||||||
Entity **entities = gb_alloc_array(c->allocator, Entity *, entity_count);
|
Entity **entities = gb_alloc_array(c->allocator, Entity *, entity_count);
|
||||||
DeclarationInfo *di = NULL;
|
DeclInfo *di = NULL;
|
||||||
if (vd->value_count == 1) {
|
if (vd->value_count == 1) {
|
||||||
di = make_declaration_info(gb_heap_allocator(), c->global_scope);
|
di = make_declaration_info(gb_heap_allocator(), c->global_scope);
|
||||||
di->entities = entities;
|
di->entities = entities;
|
||||||
di->entity_count = entity_count;
|
di->entity_count = entity_count;
|
||||||
di->type_expr = vd->type_expression;
|
di->type_expr = vd->type;
|
||||||
di->init_expr = vd->value_list;
|
di->init_expr = vd->value_list;
|
||||||
}
|
}
|
||||||
|
|
||||||
AstNode *value = vd->value_list;
|
AstNode *value = vd->value_list;
|
||||||
for (AstNode *name = vd->name_list; name != NULL; name = name->next) {
|
for (AstNode *name = vd->name_list; name != NULL; name = name->next) {
|
||||||
Entity *e = make_entity_variable(c->allocator, c->global_scope, name->identifier.token, NULL);
|
Entity *e = make_entity_variable(c->allocator, c->global_scope, name->ident.token, NULL);
|
||||||
entities[entity_index++] = e;
|
entities[entity_index++] = e;
|
||||||
|
|
||||||
DeclarationInfo *d = di;
|
DeclInfo *d = di;
|
||||||
if (d == NULL) {
|
if (d == NULL) {
|
||||||
AstNode *init_expr = value;
|
AstNode *init_expr = value;
|
||||||
d = make_declaration_info(gb_heap_allocator(), c->global_scope);
|
d = make_declaration_info(gb_heap_allocator(), c->global_scope);
|
||||||
d->type_expr = vd->type_expression;
|
d->type_expr = vd->type;
|
||||||
d->init_expr = init_expr;
|
d->init_expr = init_expr;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -605,35 +604,35 @@ void check_parsed_files(Checker *c) {
|
|||||||
|
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case AstNode_TypeDeclaration: {
|
case AstNode_TypeDecl: {
|
||||||
AstNode *identifier = decl->type_declaration.name;
|
AstNode *identifier = decl->type_decl.name;
|
||||||
Entity *e = make_entity_type_name(c->allocator, c->global_scope, identifier->identifier.token, NULL);
|
Entity *e = make_entity_type_name(c->allocator, c->global_scope, identifier->ident.token, NULL);
|
||||||
DeclarationInfo *d = make_declaration_info(c->allocator, e->parent);
|
DeclInfo *d = make_declaration_info(c->allocator, e->parent);
|
||||||
d->type_expr = decl->type_declaration.type_expression;
|
d->type_expr = decl->type_decl.type;
|
||||||
add_file_entity(c, identifier, e, d);
|
add_file_entity(c, identifier, e, d);
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case AstNode_AliasDeclaration: {
|
case AstNode_AliasDecl: {
|
||||||
AstNode *identifier = decl->alias_declaration.name;
|
AstNode *identifier = decl->alias_decl.name;
|
||||||
Entity *e = make_entity_alias_name(c->allocator, c->global_scope, identifier->identifier.token, NULL);
|
Entity *e = make_entity_alias_name(c->allocator, c->global_scope, identifier->ident.token, NULL);
|
||||||
DeclarationInfo *d = make_declaration_info(c->allocator, e->parent);
|
DeclInfo *d = make_declaration_info(c->allocator, e->parent);
|
||||||
d->type_expr = decl->alias_declaration.type_expression;
|
d->type_expr = decl->alias_decl.type;
|
||||||
add_file_entity(c, identifier, e, d);
|
add_file_entity(c, identifier, e, d);
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case AstNode_ProcedureDeclaration: {
|
case AstNode_ProcDecl: {
|
||||||
AstNode *identifier = decl->procedure_declaration.name;
|
AstNode *identifier = decl->proc_decl.name;
|
||||||
Token token = identifier->identifier.token;
|
Token token = identifier->ident.token;
|
||||||
Entity *e = make_entity_procedure(c->allocator, c->global_scope, token, NULL);
|
Entity *e = make_entity_procedure(c->allocator, c->global_scope, token, NULL);
|
||||||
add_entity(c, c->global_scope, identifier, e);
|
add_entity(c, c->global_scope, identifier, e);
|
||||||
DeclarationInfo *d = make_declaration_info(c->allocator, e->parent);
|
DeclInfo *d = make_declaration_info(c->allocator, e->parent);
|
||||||
d->proc_decl = decl;
|
d->proc_decl = decl;
|
||||||
map_set(&c->info.entities, hash_pointer(e), d);
|
map_set(&c->info.entities, hash_pointer(e), d);
|
||||||
e->order = gb_array_count(c->info.entities.entries);
|
e->order = gb_array_count(c->info.entities.entries);
|
||||||
|
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case AstNode_ImportDeclaration:
|
case AstNode_ImportDecl:
|
||||||
// NOTE(bill): ignore
|
// NOTE(bill): ignore
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -648,8 +647,8 @@ void check_parsed_files(Checker *c) {
|
|||||||
gb_for_array(i, c->info.entities.entries) {
|
gb_for_array(i, c->info.entities.entries) {
|
||||||
auto *entry = &c->info.entities.entries[i];
|
auto *entry = &c->info.entities.entries[i];
|
||||||
Entity *e = cast(Entity *)cast(uintptr)entry->key;
|
Entity *e = cast(Entity *)cast(uintptr)entry->key;
|
||||||
DeclarationInfo *d = entry->value;
|
DeclInfo *d = entry->value;
|
||||||
check_entity_declaration(c, e, d, NULL);
|
check_entity_decl(c, e, d, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -657,7 +656,7 @@ void check_parsed_files(Checker *c) {
|
|||||||
gb_for_array(i, c->procedures) {
|
gb_for_array(i, c->procedures) {
|
||||||
ProcedureInfo *pi = &c->procedures[i];
|
ProcedureInfo *pi = &c->procedures[i];
|
||||||
add_curr_ast_file(c, pi->file);
|
add_curr_ast_file(c, pi->file);
|
||||||
check_procedure_body(c, pi->token, pi->decl, pi->type, pi->body);
|
check_proc_body(c, pi->token, pi->decl, pi->type, pi->body);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -668,7 +667,7 @@ void check_parsed_files(Checker *c) {
|
|||||||
AstNode *expr = cast(AstNode *)cast(uintptr)key;
|
AstNode *expr = cast(AstNode *)cast(uintptr)key;
|
||||||
ExpressionInfo *info = &entry->value;
|
ExpressionInfo *info = &entry->value;
|
||||||
if (is_type_typed(info->type)) {
|
if (is_type_typed(info->type)) {
|
||||||
GB_PANIC("%s (type %s) is typed!", expression_to_string(expr), info->type);
|
GB_PANIC("%s (type %s) is typed!", expr_to_string(expr), info->type);
|
||||||
}
|
}
|
||||||
add_type_and_value(&c->info, expr, info->mode, info->type, info->value);
|
add_type_and_value(&c->info, expr, info->mode, info->type, info->value);
|
||||||
}
|
}
|
||||||
|
|||||||
+264
-264
File diff suppressed because it is too large
Load Diff
+145
-145
@@ -6,12 +6,12 @@ enum StatementFlag : u32 {
|
|||||||
// Statement_FallthroughAllowed = GB_BIT(2), // TODO(bill): fallthrough
|
// Statement_FallthroughAllowed = GB_BIT(2), // TODO(bill): fallthrough
|
||||||
};
|
};
|
||||||
|
|
||||||
void check_statement(Checker *c, AstNode *node, u32 flags);
|
void check_stmt(Checker *c, AstNode *node, u32 flags);
|
||||||
|
|
||||||
void check_statement_list(Checker *c, AstNode *node, u32 flags) {
|
void check_stmt_list(Checker *c, AstNode *node, u32 flags) {
|
||||||
for (; node != NULL; node = node->next) {
|
for (; node != NULL; node = node->next) {
|
||||||
if (node->kind != AstNode_EmptyStatement) {
|
if (node->kind != AstNode_EmptyStmt) {
|
||||||
check_statement(c, node, flags);
|
check_stmt(c, node, flags);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -27,7 +27,7 @@ b32 check_is_terminating_list(Checker *c, AstNode *list) {
|
|||||||
|
|
||||||
// Iterate backwards
|
// Iterate backwards
|
||||||
for (AstNode *n = list; n != NULL; n = n->prev) {
|
for (AstNode *n = list; n != NULL; n = n->prev) {
|
||||||
if (n->kind != AstNode_EmptyStatement)
|
if (n->kind != AstNode_EmptyStmt)
|
||||||
return check_is_terminating(c, n);
|
return check_is_terminating(c, n);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,26 +39,26 @@ b32 check_is_terminating_list(Checker *c, AstNode *list) {
|
|||||||
// TODO(bill): Warn/err against code after `return` that it won't be executed
|
// TODO(bill): Warn/err against code after `return` that it won't be executed
|
||||||
b32 check_is_terminating(Checker *c, AstNode *node) {
|
b32 check_is_terminating(Checker *c, AstNode *node) {
|
||||||
switch (node->kind) {
|
switch (node->kind) {
|
||||||
case AstNode_ReturnStatement:
|
case AstNode_ReturnStmt:
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
case AstNode_BlockStatement:
|
case AstNode_BlockStmt:
|
||||||
return check_is_terminating_list(c, node->block_statement.list);
|
return check_is_terminating_list(c, node->block_stmt.list);
|
||||||
|
|
||||||
case AstNode_ExpressionStatement:
|
case AstNode_ExprStmt:
|
||||||
return check_is_terminating(c, node->expression_statement.expression);
|
return check_is_terminating(c, node->expr_stmt.expr);
|
||||||
|
|
||||||
case AstNode_IfStatement:
|
case AstNode_IfStmt:
|
||||||
if (node->if_statement.else_statement != NULL) {
|
if (node->if_stmt.else_stmt != NULL) {
|
||||||
if (check_is_terminating(c, node->if_statement.body) &&
|
if (check_is_terminating(c, node->if_stmt.body) &&
|
||||||
check_is_terminating(c, node->if_statement.else_statement)) {
|
check_is_terminating(c, node->if_stmt.else_stmt)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case AstNode_ForStatement:
|
case AstNode_ForStmt:
|
||||||
if (node->for_statement.cond == NULL) {
|
if (node->for_stmt.cond == NULL) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -141,14 +141,14 @@ void check_assignment(Checker *c, Operand *operand, Type *type, String context_n
|
|||||||
if (!check_is_assignable_to(c, operand, type)) {
|
if (!check_is_assignable_to(c, operand, type)) {
|
||||||
gbString type_string = type_to_string(type);
|
gbString type_string = type_to_string(type);
|
||||||
gbString op_type_string = type_to_string(operand->type);
|
gbString op_type_string = type_to_string(operand->type);
|
||||||
gbString expr_str = expression_to_string(operand->expression);
|
gbString expr_str = expr_to_string(operand->expr);
|
||||||
defer (gb_string_free(type_string));
|
defer (gb_string_free(type_string));
|
||||||
defer (gb_string_free(op_type_string));
|
defer (gb_string_free(op_type_string));
|
||||||
defer (gb_string_free(expr_str));
|
defer (gb_string_free(expr_str));
|
||||||
|
|
||||||
|
|
||||||
// TODO(bill): is this a good enough error message?
|
// TODO(bill): is this a good enough error message?
|
||||||
error(&c->error_collector, ast_node_token(operand->expression),
|
error(&c->error_collector, ast_node_token(operand->expr),
|
||||||
"Cannot assign value `%s` of type `%s` to `%s` in %.*s",
|
"Cannot assign value `%s` of type `%s` to `%s` in %.*s",
|
||||||
expr_str,
|
expr_str,
|
||||||
op_type_string,
|
op_type_string,
|
||||||
@@ -167,11 +167,11 @@ Type *check_assignment_variable(Checker *c, Operand *op_a, AstNode *lhs) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
AstNode *node = unparen_expression(lhs);
|
AstNode *node = unparen_expr(lhs);
|
||||||
|
|
||||||
// NOTE(bill): Ignore assignments to `_`
|
// NOTE(bill): Ignore assignments to `_`
|
||||||
if (node->kind == AstNode_Identifier &&
|
if (node->kind == AstNode_Ident &&
|
||||||
are_strings_equal(node->identifier.token.string, make_string("_"))) {
|
are_strings_equal(node->ident.token.string, make_string("_"))) {
|
||||||
add_entity_definition(&c->info, node, NULL);
|
add_entity_definition(&c->info, node, NULL);
|
||||||
check_assignment(c, op_a, NULL, make_string("assignment to `_` identifier"));
|
check_assignment(c, op_a, NULL, make_string("assignment to `_` identifier"));
|
||||||
if (op_a->mode == Addressing_Invalid)
|
if (op_a->mode == Addressing_Invalid)
|
||||||
@@ -181,8 +181,8 @@ Type *check_assignment_variable(Checker *c, Operand *op_a, AstNode *lhs) {
|
|||||||
|
|
||||||
Entity *e = NULL;
|
Entity *e = NULL;
|
||||||
b32 used = false;
|
b32 used = false;
|
||||||
if (node->kind == AstNode_Identifier) {
|
if (node->kind == AstNode_Ident) {
|
||||||
e = scope_lookup_entity(c->context.scope, node->identifier.token.string);
|
e = scope_lookup_entity(c->context.scope, node->ident.token.string);
|
||||||
if (e != NULL && e->kind == Entity_Variable) {
|
if (e != NULL && e->kind == Entity_Variable) {
|
||||||
used = e->variable.used; // TODO(bill): Make backup just in case
|
used = e->variable.used; // TODO(bill): Make backup just in case
|
||||||
}
|
}
|
||||||
@@ -190,7 +190,7 @@ Type *check_assignment_variable(Checker *c, Operand *op_a, AstNode *lhs) {
|
|||||||
|
|
||||||
|
|
||||||
Operand op_b = {Addressing_Invalid};
|
Operand op_b = {Addressing_Invalid};
|
||||||
check_expression(c, &op_b, lhs);
|
check_expr(c, &op_b, lhs);
|
||||||
if (e) e->variable.used = used;
|
if (e) e->variable.used = used;
|
||||||
|
|
||||||
if (op_b.mode == Addressing_Invalid ||
|
if (op_b.mode == Addressing_Invalid ||
|
||||||
@@ -204,15 +204,15 @@ Type *check_assignment_variable(Checker *c, Operand *op_a, AstNode *lhs) {
|
|||||||
case Addressing_Invalid:
|
case Addressing_Invalid:
|
||||||
return NULL;
|
return NULL;
|
||||||
default: {
|
default: {
|
||||||
if (op_b.expression->kind == AstNode_SelectorExpression) {
|
if (op_b.expr->kind == AstNode_SelectorExpr) {
|
||||||
// NOTE(bill): Extra error checks
|
// NOTE(bill): Extra error checks
|
||||||
Operand op_c = {Addressing_Invalid};
|
Operand op_c = {Addressing_Invalid};
|
||||||
check_expression(c, &op_c, op_b.expression->selector_expression.operand);
|
check_expr(c, &op_c, op_b.expr->selector_expr.expr);
|
||||||
}
|
}
|
||||||
|
|
||||||
gbString str = expression_to_string(op_b.expression);
|
gbString str = expr_to_string(op_b.expr);
|
||||||
defer (gb_string_free(str));
|
defer (gb_string_free(str));
|
||||||
error(&c->error_collector, ast_node_token(op_b.expression), "Cannot assign to `%s`", str);
|
error(&c->error_collector, ast_node_token(op_b.expr), "Cannot assign to `%s`", str);
|
||||||
} break;
|
} break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -264,7 +264,7 @@ void check_init_variables(Checker *c, Entity **lhs, isize lhs_count, AstNode *in
|
|||||||
i < lhs_count && i < init_count && rhs != NULL;
|
i < lhs_count && i < init_count && rhs != NULL;
|
||||||
i++, rhs = rhs->next) {
|
i++, rhs = rhs->next) {
|
||||||
Operand operand = {};
|
Operand operand = {};
|
||||||
check_multi_expression(c, &operand, rhs);
|
check_multi_expr(c, &operand, rhs);
|
||||||
if (operand.type->kind != Type_Tuple) {
|
if (operand.type->kind != Type_Tuple) {
|
||||||
check_init_variable(c, lhs[i], &operand, context_name);
|
check_init_variable(c, lhs[i], &operand, context_name);
|
||||||
} else {
|
} else {
|
||||||
@@ -297,8 +297,8 @@ void check_init_constant(Checker *c, Entity *e, Operand *operand) {
|
|||||||
|
|
||||||
if (operand->mode != Addressing_Constant) {
|
if (operand->mode != Addressing_Constant) {
|
||||||
// TODO(bill): better error
|
// TODO(bill): better error
|
||||||
error(&c->error_collector, ast_node_token(operand->expression),
|
error(&c->error_collector, ast_node_token(operand->expr),
|
||||||
"`%.*s` is not a constant", LIT(ast_node_token(operand->expression).string));
|
"`%.*s` is not a constant", LIT(ast_node_token(operand->expr).string));
|
||||||
if (e->type == NULL)
|
if (e->type == NULL)
|
||||||
e->type = &basic_types[Basic_Invalid];
|
e->type = &basic_types[Basic_Invalid];
|
||||||
return;
|
return;
|
||||||
@@ -319,7 +319,7 @@ void check_init_constant(Checker *c, Entity *e, Operand *operand) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void check_constant_declaration(Checker *c, Entity *e, AstNode *type_expr, AstNode *init_expr) {
|
void check_const_decl(Checker *c, Entity *e, AstNode *type_expr, AstNode *init_expr) {
|
||||||
GB_ASSERT(e->type == NULL);
|
GB_ASSERT(e->type == NULL);
|
||||||
|
|
||||||
if (e->variable.visited) {
|
if (e->variable.visited) {
|
||||||
@@ -343,11 +343,11 @@ void check_constant_declaration(Checker *c, Entity *e, AstNode *type_expr, AstNo
|
|||||||
|
|
||||||
Operand operand = {Addressing_Invalid};
|
Operand operand = {Addressing_Invalid};
|
||||||
if (init_expr)
|
if (init_expr)
|
||||||
check_expression(c, &operand, init_expr);
|
check_expr(c, &operand, init_expr);
|
||||||
check_init_constant(c, e, &operand);
|
check_init_constant(c, e, &operand);
|
||||||
}
|
}
|
||||||
|
|
||||||
void check_type_declaration(Checker *c, Entity *e, AstNode *type_expr, Type *named_type) {
|
void check_type_decl(Checker *c, Entity *e, AstNode *type_expr, Type *named_type) {
|
||||||
GB_ASSERT(e->type == NULL);
|
GB_ASSERT(e->type == NULL);
|
||||||
Type *named = make_type_named(c->allocator, e->token.string, NULL, e);
|
Type *named = make_type_named(c->allocator, e->token.string, NULL, e);
|
||||||
named->named.type_name = e;
|
named->named.type_name = e;
|
||||||
@@ -359,7 +359,7 @@ void check_type_declaration(Checker *c, Entity *e, AstNode *type_expr, Type *nam
|
|||||||
set_base_type(named, get_base_type(get_base_type(named)));
|
set_base_type(named, get_base_type(get_base_type(named)));
|
||||||
}
|
}
|
||||||
|
|
||||||
void check_alias_declaration(Checker *c, Entity *e, AstNode *type_expr, Type *alias_type) {
|
void check_alias_decl(Checker *c, Entity *e, AstNode *type_expr, Type *alias_type) {
|
||||||
GB_ASSERT(e->type == NULL);
|
GB_ASSERT(e->type == NULL);
|
||||||
Type *named = make_type_alias(c->allocator, e->token.string, NULL, e);
|
Type *named = make_type_alias(c->allocator, e->token.string, NULL, e);
|
||||||
named->alias.alias_name = e;
|
named->alias.alias_name = e;
|
||||||
@@ -371,18 +371,18 @@ void check_alias_declaration(Checker *c, Entity *e, AstNode *type_expr, Type *al
|
|||||||
set_base_type(named, get_base_type(get_base_type(named)));
|
set_base_type(named, get_base_type(get_base_type(named)));
|
||||||
}
|
}
|
||||||
|
|
||||||
void check_procedure_body(Checker *c, Token token, DeclarationInfo *decl, Type *type, AstNode *body) {
|
void check_proc_body(Checker *c, Token token, DeclInfo *decl, Type *type, AstNode *body) {
|
||||||
GB_ASSERT(body->kind == AstNode_BlockStatement);
|
GB_ASSERT(body->kind == AstNode_BlockStmt);
|
||||||
|
|
||||||
CheckerContext old_context = c->context;
|
CheckerContext old_context = c->context;
|
||||||
c->context.scope = decl->scope;
|
c->context.scope = decl->scope;
|
||||||
c->context.decl = decl;
|
c->context.decl = decl;
|
||||||
|
|
||||||
push_procedure(c, type);
|
push_procedure(c, type);
|
||||||
check_statement_list(c, body->block_statement.list, 0);
|
check_stmt_list(c, body->block_stmt.list, 0);
|
||||||
if (type->procedure.result_count > 0) {
|
if (type->procedure.result_count > 0) {
|
||||||
if (!check_is_terminating(c, body)) {
|
if (!check_is_terminating(c, body)) {
|
||||||
error(&c->error_collector, body->block_statement.close, "Missing return statement at the end of the procedure");
|
error(&c->error_collector, body->block_stmt.close, "Missing return statement at the end of the procedure");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pop_procedure(c);
|
pop_procedure(c);
|
||||||
@@ -390,12 +390,12 @@ void check_procedure_body(Checker *c, Token token, DeclarationInfo *decl, Type *
|
|||||||
c->context = old_context;
|
c->context = old_context;
|
||||||
}
|
}
|
||||||
|
|
||||||
void check_procedure_declaration(Checker *c, Entity *e, DeclarationInfo *d, b32 check_body_later) {
|
void check_proc_decl(Checker *c, Entity *e, DeclInfo *d, b32 check_body_later) {
|
||||||
GB_ASSERT(e->type == NULL);
|
GB_ASSERT(e->type == NULL);
|
||||||
|
|
||||||
Type *proc_type = make_type_procedure(c->allocator, e->parent, NULL, 0, NULL, 0);
|
Type *proc_type = make_type_procedure(c->allocator, e->parent, NULL, 0, NULL, 0);
|
||||||
e->type = proc_type;
|
e->type = proc_type;
|
||||||
auto *pd = &d->proc_decl->procedure_declaration;
|
auto *pd = &d->proc_decl->proc_decl;
|
||||||
|
|
||||||
#if 1
|
#if 1
|
||||||
Scope *original_curr_scope = c->context.scope;
|
Scope *original_curr_scope = c->context.scope;
|
||||||
@@ -407,9 +407,9 @@ void check_procedure_declaration(Checker *c, Entity *e, DeclarationInfo *d, b32
|
|||||||
b32 is_inline = false;
|
b32 is_inline = false;
|
||||||
b32 is_no_inline = false;
|
b32 is_no_inline = false;
|
||||||
for (AstNode *tag = pd->tag_list; tag != NULL; tag = tag->next) {
|
for (AstNode *tag = pd->tag_list; tag != NULL; tag = tag->next) {
|
||||||
GB_ASSERT(tag->kind == AstNode_TagExpression);
|
GB_ASSERT(tag->kind == AstNode_TagExpr);
|
||||||
|
|
||||||
String tag_name = tag->tag_expression.name.string;
|
String tag_name = tag->tag_expr.name.string;
|
||||||
if (are_strings_equal(tag_name, make_string("foreign"))) {
|
if (are_strings_equal(tag_name, make_string("foreign"))) {
|
||||||
is_foreign = true;
|
is_foreign = true;
|
||||||
} else if (are_strings_equal(tag_name, make_string("inline"))) {
|
} else if (are_strings_equal(tag_name, make_string("inline"))) {
|
||||||
@@ -435,11 +435,11 @@ void check_procedure_declaration(Checker *c, Entity *e, DeclarationInfo *d, b32
|
|||||||
|
|
||||||
d->scope = c->context.scope;
|
d->scope = c->context.scope;
|
||||||
|
|
||||||
GB_ASSERT(pd->body->kind == AstNode_BlockStatement);
|
GB_ASSERT(pd->body->kind == AstNode_BlockStmt);
|
||||||
if (check_body_later) {
|
if (check_body_later) {
|
||||||
check_procedure_later(c, c->curr_ast_file, e->token, d, proc_type, pd->body);
|
check_procedure_later(c, c->curr_ast_file, e->token, d, proc_type, pd->body);
|
||||||
} else {
|
} else {
|
||||||
check_procedure_body(c, e->token, d, proc_type, pd->body);
|
check_proc_body(c, e->token, d, proc_type, pd->body);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -450,7 +450,7 @@ void check_procedure_declaration(Checker *c, Entity *e, DeclarationInfo *d, b32
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void check_variable_declaration(Checker *c, Entity *e, Entity **entities, isize entity_count, AstNode *type_expr, AstNode *init_expr) {
|
void check_var_decl(Checker *c, Entity *e, Entity **entities, isize entity_count, AstNode *type_expr, AstNode *init_expr) {
|
||||||
GB_ASSERT(e->type == NULL);
|
GB_ASSERT(e->type == NULL);
|
||||||
GB_ASSERT(e->kind == Entity_Variable);
|
GB_ASSERT(e->kind == Entity_Variable);
|
||||||
|
|
||||||
@@ -472,7 +472,7 @@ void check_variable_declaration(Checker *c, Entity *e, Entity **entities, isize
|
|||||||
if (entities == NULL || entity_count == 1) {
|
if (entities == NULL || entity_count == 1) {
|
||||||
GB_ASSERT(entities == NULL || entities[0] == e);
|
GB_ASSERT(entities == NULL || entities[0] == e);
|
||||||
Operand operand = {};
|
Operand operand = {};
|
||||||
check_expression(c, &operand, init_expr);
|
check_expr(c, &operand, init_expr);
|
||||||
check_init_variable(c, e, &operand, make_string("variable declaration"));
|
check_init_variable(c, e, &operand, make_string("variable declaration"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -486,26 +486,26 @@ void check_variable_declaration(Checker *c, Entity *e, Entity **entities, isize
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
void check_entity_declaration(Checker *c, Entity *e, DeclarationInfo *d, Type *named_type) {
|
void check_entity_decl(Checker *c, Entity *e, DeclInfo *d, Type *named_type) {
|
||||||
if (e->type != NULL)
|
if (e->type != NULL)
|
||||||
return;
|
return;
|
||||||
switch (e->kind) {
|
switch (e->kind) {
|
||||||
case Entity_Constant:
|
case Entity_Constant:
|
||||||
c->context.decl = d;
|
c->context.decl = d;
|
||||||
check_constant_declaration(c, e, d->type_expr, d->init_expr);
|
check_const_decl(c, e, d->type_expr, d->init_expr);
|
||||||
break;
|
break;
|
||||||
case Entity_Variable:
|
case Entity_Variable:
|
||||||
c->context.decl = d;
|
c->context.decl = d;
|
||||||
check_variable_declaration(c, e, d->entities, d->entity_count, d->type_expr, d->init_expr);
|
check_var_decl(c, e, d->entities, d->entity_count, d->type_expr, d->init_expr);
|
||||||
break;
|
break;
|
||||||
case Entity_TypeName:
|
case Entity_TypeName:
|
||||||
check_type_declaration(c, e, d->type_expr, named_type);
|
check_type_decl(c, e, d->type_expr, named_type);
|
||||||
break;
|
break;
|
||||||
case Entity_AliasName:
|
case Entity_AliasName:
|
||||||
check_alias_declaration(c, e, d->type_expr, named_type);
|
check_alias_decl(c, e, d->type_expr, named_type);
|
||||||
break;
|
break;
|
||||||
case Entity_Procedure:
|
case Entity_Procedure:
|
||||||
check_procedure_declaration(c, e, d, true);
|
check_proc_decl(c, e, d, true);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -514,15 +514,15 @@ void check_entity_declaration(Checker *c, Entity *e, DeclarationInfo *d, Type *n
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
void check_statement(Checker *c, AstNode *node, u32 flags) {
|
void check_stmt(Checker *c, AstNode *node, u32 flags) {
|
||||||
switch (node->kind) {
|
switch (node->kind) {
|
||||||
case AstNode_EmptyStatement: break;
|
case AstNode_EmptyStmt: break;
|
||||||
case AstNode_BadStatement: break;
|
case AstNode_BadStmt: break;
|
||||||
case AstNode_BadDeclaration: break;
|
case AstNode_BadDecl: break;
|
||||||
|
|
||||||
case AstNode_ExpressionStatement: {
|
case AstNode_ExprStmt: {
|
||||||
Operand operand = {Addressing_Invalid};
|
Operand operand = {Addressing_Invalid};
|
||||||
ExpressionKind kind = check_expression_base(c, &operand, node->expression_statement.expression);
|
ExpressionKind kind = check_expr_base(c, &operand, node->expr_stmt.expr);
|
||||||
switch (operand.mode) {
|
switch (operand.mode) {
|
||||||
case Addressing_Type:
|
case Addressing_Type:
|
||||||
error(&c->error_collector, ast_node_token(node), "Is not an expression");
|
error(&c->error_collector, ast_node_token(node), "Is not an expression");
|
||||||
@@ -535,15 +535,15 @@ void check_statement(Checker *c, AstNode *node, u32 flags) {
|
|||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case AstNode_TagStatement:
|
case AstNode_TagStmt:
|
||||||
// TODO(bill): Tag Statements
|
// TODO(bill): Tag Statements
|
||||||
error(&c->error_collector, ast_node_token(node), "Tag statements are not supported yet");
|
error(&c->error_collector, ast_node_token(node), "Tag statements are not supported yet");
|
||||||
check_statement(c, node->tag_statement.statement, flags);
|
check_stmt(c, node->tag_stmt.stmt, flags);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case AstNode_IncDecStatement: {
|
case AstNode_IncDecStmt: {
|
||||||
Token op = {};
|
Token op = {};
|
||||||
auto *s = &node->inc_dec_statement;
|
auto *s = &node->inc_dec_stmt;
|
||||||
op = s->op;
|
op = s->op;
|
||||||
switch (s->op.kind) {
|
switch (s->op.kind) {
|
||||||
case Token_Increment:
|
case Token_Increment:
|
||||||
@@ -560,7 +560,7 @@ void check_statement(Checker *c, AstNode *node, u32 flags) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Operand operand = {Addressing_Invalid};
|
Operand operand = {Addressing_Invalid};
|
||||||
check_expression(c, &operand, s->expression);
|
check_expr(c, &operand, s->expr);
|
||||||
if (operand.mode == Addressing_Invalid)
|
if (operand.mode == Addressing_Invalid)
|
||||||
return;
|
return;
|
||||||
if (!is_type_numeric(operand.type)) {
|
if (!is_type_numeric(operand.type)) {
|
||||||
@@ -568,36 +568,36 @@ void check_statement(Checker *c, AstNode *node, u32 flags) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
AstNode basic_lit = {AstNode_BasicLiteral};
|
AstNode basic_lit = {AstNode_BasicLit};
|
||||||
basic_lit.basic_literal = s->op;
|
basic_lit.basic_lit = s->op;
|
||||||
basic_lit.basic_literal.kind = Token_Integer;
|
basic_lit.basic_lit.kind = Token_Integer;
|
||||||
basic_lit.basic_literal.string = make_string("1");
|
basic_lit.basic_lit.string = make_string("1");
|
||||||
|
|
||||||
AstNode be = {AstNode_BinaryExpression};
|
AstNode be = {AstNode_BinaryExpr};
|
||||||
be.binary_expression.op = op;
|
be.binary_expr.op = op;
|
||||||
be.binary_expression.left = s->expression;;
|
be.binary_expr.left = s->expr;;
|
||||||
be.binary_expression.right = &basic_lit;
|
be.binary_expr.right = &basic_lit;
|
||||||
check_binary_expression(c, &operand, &be);
|
check_binary_expr(c, &operand, &be);
|
||||||
|
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case AstNode_AssignStatement:
|
case AstNode_AssignStmt:
|
||||||
switch (node->assign_statement.op.kind) {
|
switch (node->assign_stmt.op.kind) {
|
||||||
case Token_Eq: {
|
case Token_Eq: {
|
||||||
// a, b, c = 1, 2, 3; // Multisided
|
// a, b, c = 1, 2, 3; // Multisided
|
||||||
if (node->assign_statement.lhs_count == 0) {
|
if (node->assign_stmt.lhs_count == 0) {
|
||||||
error(&c->error_collector, node->assign_statement.op, "Missing lhs in assignment statement");
|
error(&c->error_collector, node->assign_stmt.op, "Missing lhs in assignment statement");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Operand operand = {};
|
Operand operand = {};
|
||||||
AstNode *lhs = node->assign_statement.lhs_list;
|
AstNode *lhs = node->assign_stmt.lhs_list;
|
||||||
AstNode *rhs = node->assign_statement.rhs_list;
|
AstNode *rhs = node->assign_stmt.rhs_list;
|
||||||
isize i = 0;
|
isize i = 0;
|
||||||
for (;
|
for (;
|
||||||
lhs != NULL && rhs != NULL;
|
lhs != NULL && rhs != NULL;
|
||||||
lhs = lhs->next, rhs = rhs->next) {
|
lhs = lhs->next, rhs = rhs->next) {
|
||||||
check_multi_expression(c, &operand, rhs);
|
check_multi_expr(c, &operand, rhs);
|
||||||
if (operand.type->kind != Type_Tuple) {
|
if (operand.type->kind != Type_Tuple) {
|
||||||
check_assignment_variable(c, &operand, lhs);
|
check_assignment_variable(c, &operand, lhs);
|
||||||
i++;
|
i++;
|
||||||
@@ -615,7 +615,7 @@ void check_statement(Checker *c, AstNode *node, u32 flags) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (i < node->assign_statement.lhs_count && i < node->assign_statement.rhs_count) {
|
if (i < node->assign_stmt.lhs_count && i < node->assign_stmt.rhs_count) {
|
||||||
if (lhs == NULL)
|
if (lhs == NULL)
|
||||||
error(&c->error_collector, ast_node_token(lhs), "Too few values on the right hand side of the declaration");
|
error(&c->error_collector, ast_node_token(lhs), "Too few values on the right hand side of the declaration");
|
||||||
} else if (rhs != NULL) {
|
} else if (rhs != NULL) {
|
||||||
@@ -625,9 +625,9 @@ void check_statement(Checker *c, AstNode *node, u32 flags) {
|
|||||||
|
|
||||||
default: {
|
default: {
|
||||||
// a += 1; // Single-sided
|
// a += 1; // Single-sided
|
||||||
Token op = node->assign_statement.op;
|
Token op = node->assign_stmt.op;
|
||||||
if (node->assign_statement.lhs_count != 1 ||
|
if (node->assign_stmt.lhs_count != 1 ||
|
||||||
node->assign_statement.rhs_count != 1) {
|
node->assign_stmt.rhs_count != 1) {
|
||||||
error(&c->error_collector, op, "Assignment operation `%.*s` requires single-valued expressions", LIT(op.string));
|
error(&c->error_collector, op, "Assignment operation `%.*s` requires single-valued expressions", LIT(op.string));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -637,61 +637,61 @@ void check_statement(Checker *c, AstNode *node, u32 flags) {
|
|||||||
}
|
}
|
||||||
// TODO(bill): Check if valid assignment operator
|
// TODO(bill): Check if valid assignment operator
|
||||||
Operand operand = {Addressing_Invalid};
|
Operand operand = {Addressing_Invalid};
|
||||||
AstNode be = {AstNode_BinaryExpression};
|
AstNode be = {AstNode_BinaryExpr};
|
||||||
be.binary_expression.op = op;
|
be.binary_expr.op = op;
|
||||||
// NOTE(bill): Only use the first one will be used
|
// NOTE(bill): Only use the first one will be used
|
||||||
be.binary_expression.left = node->assign_statement.lhs_list;
|
be.binary_expr.left = node->assign_stmt.lhs_list;
|
||||||
be.binary_expression.right = node->assign_statement.rhs_list;
|
be.binary_expr.right = node->assign_stmt.rhs_list;
|
||||||
|
|
||||||
check_binary_expression(c, &operand, &be);
|
check_binary_expr(c, &operand, &be);
|
||||||
if (operand.mode == Addressing_Invalid)
|
if (operand.mode == Addressing_Invalid)
|
||||||
return;
|
return;
|
||||||
// NOTE(bill): Only use the first one will be used
|
// NOTE(bill): Only use the first one will be used
|
||||||
check_assignment_variable(c, &operand, node->assign_statement.lhs_list);
|
check_assignment_variable(c, &operand, node->assign_stmt.lhs_list);
|
||||||
} break;
|
} break;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case AstNode_BlockStatement:
|
case AstNode_BlockStmt:
|
||||||
check_open_scope(c, node);
|
check_open_scope(c, node);
|
||||||
check_statement_list(c, node->block_statement.list, flags);
|
check_stmt_list(c, node->block_stmt.list, flags);
|
||||||
check_close_scope(c);
|
check_close_scope(c);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case AstNode_IfStatement: {
|
case AstNode_IfStmt: {
|
||||||
check_open_scope(c, node);
|
check_open_scope(c, node);
|
||||||
defer (check_close_scope(c));
|
defer (check_close_scope(c));
|
||||||
auto *is = &node->if_statement;
|
auto *is = &node->if_stmt;
|
||||||
|
|
||||||
if (is->init != NULL)
|
if (is->init != NULL)
|
||||||
check_statement(c, is->init, 0);
|
check_stmt(c, is->init, 0);
|
||||||
|
|
||||||
Operand operand = {Addressing_Invalid};
|
Operand operand = {Addressing_Invalid};
|
||||||
check_expression(c, &operand, node->if_statement.cond);
|
check_expr(c, &operand, node->if_stmt.cond);
|
||||||
if (operand.mode != Addressing_Invalid &&
|
if (operand.mode != Addressing_Invalid &&
|
||||||
!is_type_boolean(operand.type)) {
|
!is_type_boolean(operand.type)) {
|
||||||
error(&c->error_collector, ast_node_token(node->if_statement.cond),
|
error(&c->error_collector, ast_node_token(node->if_stmt.cond),
|
||||||
"Non-boolean condition in `if` statement");
|
"Non-boolean condition in `if` statement");
|
||||||
}
|
}
|
||||||
|
|
||||||
check_statement(c, node->if_statement.body, flags);
|
check_stmt(c, node->if_stmt.body, flags);
|
||||||
|
|
||||||
if (node->if_statement.else_statement) {
|
if (node->if_stmt.else_stmt) {
|
||||||
switch (node->if_statement.else_statement->kind) {
|
switch (node->if_stmt.else_stmt->kind) {
|
||||||
case AstNode_IfStatement:
|
case AstNode_IfStmt:
|
||||||
case AstNode_BlockStatement:
|
case AstNode_BlockStmt:
|
||||||
check_statement(c, node->if_statement.else_statement, flags);
|
check_stmt(c, node->if_stmt.else_stmt, flags);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
error(&c->error_collector, ast_node_token(node->if_statement.else_statement),
|
error(&c->error_collector, ast_node_token(node->if_stmt.else_stmt),
|
||||||
"Invalid `else` statement in `if` statement");
|
"Invalid `else` statement in `if` statement");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case AstNode_ReturnStatement: {
|
case AstNode_ReturnStmt: {
|
||||||
auto *rs = &node->return_statement;
|
auto *rs = &node->return_stmt;
|
||||||
GB_ASSERT(gb_array_count(c->procedure_stack) > 0);
|
GB_ASSERT(gb_array_count(c->procedure_stack) > 0);
|
||||||
|
|
||||||
if (c->in_defer) {
|
if (c->in_defer) {
|
||||||
@@ -712,45 +712,45 @@ void check_statement(Checker *c, AstNode *node, u32 flags) {
|
|||||||
} else if (result_count > 0) {
|
} else if (result_count > 0) {
|
||||||
auto *tuple = &proc_type->procedure.results->tuple;
|
auto *tuple = &proc_type->procedure.results->tuple;
|
||||||
check_init_variables(c, tuple->variables, tuple->variable_count,
|
check_init_variables(c, tuple->variables, tuple->variable_count,
|
||||||
rs->results, rs->result_count, make_string("return statement"));
|
rs->result_list, rs->result_count, make_string("return statement"));
|
||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case AstNode_ForStatement: {
|
case AstNode_ForStmt: {
|
||||||
flags |= Statement_BreakAllowed | Statement_ContinueAllowed;
|
flags |= Statement_BreakAllowed | Statement_ContinueAllowed;
|
||||||
check_open_scope(c, node);
|
check_open_scope(c, node);
|
||||||
defer (check_close_scope(c));
|
defer (check_close_scope(c));
|
||||||
|
|
||||||
if (node->for_statement.init != NULL)
|
if (node->for_stmt.init != NULL)
|
||||||
check_statement(c, node->for_statement.init, 0);
|
check_stmt(c, node->for_stmt.init, 0);
|
||||||
if (node->for_statement.cond) {
|
if (node->for_stmt.cond) {
|
||||||
Operand operand = {Addressing_Invalid};
|
Operand operand = {Addressing_Invalid};
|
||||||
check_expression(c, &operand, node->for_statement.cond);
|
check_expr(c, &operand, node->for_stmt.cond);
|
||||||
if (operand.mode != Addressing_Invalid &&
|
if (operand.mode != Addressing_Invalid &&
|
||||||
!is_type_boolean(operand.type)) {
|
!is_type_boolean(operand.type)) {
|
||||||
error(&c->error_collector, ast_node_token(node->for_statement.cond),
|
error(&c->error_collector, ast_node_token(node->for_stmt.cond),
|
||||||
"Non-boolean condition in `for` statement");
|
"Non-boolean condition in `for` statement");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (node->for_statement.end != NULL)
|
if (node->for_stmt.end != NULL)
|
||||||
check_statement(c, node->for_statement.end, 0);
|
check_stmt(c, node->for_stmt.end, 0);
|
||||||
check_statement(c, node->for_statement.body, flags);
|
check_stmt(c, node->for_stmt.body, flags);
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case AstNode_DeferStatement: {
|
case AstNode_DeferStmt: {
|
||||||
auto *ds = &node->defer_statement;
|
auto *ds = &node->defer_stmt;
|
||||||
if (is_ast_node_declaration(ds->statement)) {
|
if (is_ast_node_decl(ds->stmt)) {
|
||||||
error(&c->error_collector, ds->token, "You cannot defer a declaration");
|
error(&c->error_collector, ds->token, "You cannot defer a declaration");
|
||||||
} else {
|
} else {
|
||||||
b32 out_in_defer = c->in_defer;
|
b32 out_in_defer = c->in_defer;
|
||||||
c->in_defer = true;
|
c->in_defer = true;
|
||||||
check_statement(c, ds->statement, 0);
|
check_stmt(c, ds->stmt, 0);
|
||||||
c->in_defer = out_in_defer;
|
c->in_defer = out_in_defer;
|
||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case AstNode_BranchStatement: {
|
case AstNode_BranchStmt: {
|
||||||
Token token = node->branch_statement.token;
|
Token token = node->branch_stmt.token;
|
||||||
switch (token.kind) {
|
switch (token.kind) {
|
||||||
case Token_break:
|
case Token_break:
|
||||||
if ((flags & Statement_BreakAllowed) == 0)
|
if ((flags & Statement_BreakAllowed) == 0)
|
||||||
@@ -768,8 +768,8 @@ void check_statement(Checker *c, AstNode *node, u32 flags) {
|
|||||||
|
|
||||||
|
|
||||||
// Declarations
|
// Declarations
|
||||||
case AstNode_VariableDeclaration: {
|
case AstNode_VarDecl: {
|
||||||
auto *vd = &node->variable_declaration;
|
auto *vd = &node->var_decl;
|
||||||
isize entity_count = vd->name_count;
|
isize entity_count = vd->name_count;
|
||||||
isize entity_index = 0;
|
isize entity_index = 0;
|
||||||
Entity **entities = gb_alloc_array(c->allocator, Entity *, entity_count);
|
Entity **entities = gb_alloc_array(c->allocator, Entity *, entity_count);
|
||||||
@@ -780,8 +780,8 @@ void check_statement(Checker *c, AstNode *node, u32 flags) {
|
|||||||
|
|
||||||
for (AstNode *name = vd->name_list; name != NULL; name = name->next) {
|
for (AstNode *name = vd->name_list; name != NULL; name = name->next) {
|
||||||
Entity *entity = NULL;
|
Entity *entity = NULL;
|
||||||
Token token = name->identifier.token;
|
Token token = name->ident.token;
|
||||||
if (name->kind == AstNode_Identifier) {
|
if (name->kind == AstNode_Ident) {
|
||||||
String str = token.string;
|
String str = token.string;
|
||||||
Entity *found = NULL;
|
Entity *found = NULL;
|
||||||
// NOTE(bill): Ignore assignments to `_`
|
// NOTE(bill): Ignore assignments to `_`
|
||||||
@@ -807,8 +807,8 @@ void check_statement(Checker *c, AstNode *node, u32 flags) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Type *init_type = NULL;
|
Type *init_type = NULL;
|
||||||
if (vd->type_expression) {
|
if (vd->type) {
|
||||||
init_type = check_type(c, vd->type_expression, NULL);
|
init_type = check_type(c, vd->type, NULL);
|
||||||
if (init_type == NULL)
|
if (init_type == NULL)
|
||||||
init_type = &basic_types[Basic_Invalid];
|
init_type = &basic_types[Basic_Invalid];
|
||||||
}
|
}
|
||||||
@@ -839,18 +839,18 @@ void check_statement(Checker *c, AstNode *node, u32 flags) {
|
|||||||
for (AstNode *name = vd->name_list, *value = vd->value_list;
|
for (AstNode *name = vd->name_list, *value = vd->value_list;
|
||||||
name != NULL && value != NULL;
|
name != NULL && value != NULL;
|
||||||
name = name->next, value = value->next) {
|
name = name->next, value = value->next) {
|
||||||
GB_ASSERT(name->kind == AstNode_Identifier);
|
GB_ASSERT(name->kind == AstNode_Ident);
|
||||||
ExactValue v = {ExactValue_Invalid};
|
ExactValue v = {ExactValue_Invalid};
|
||||||
Entity *e = make_entity_constant(c->allocator, c->context.scope, name->identifier.token, NULL, v);
|
Entity *e = make_entity_constant(c->allocator, c->context.scope, name->ident.token, NULL, v);
|
||||||
entities[entity_index++] = e;
|
entities[entity_index++] = e;
|
||||||
check_constant_declaration(c, e, vd->type_expression, value);
|
check_const_decl(c, e, vd->type, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
isize lhs_count = vd->name_count;
|
isize lhs_count = vd->name_count;
|
||||||
isize rhs_count = vd->value_count;
|
isize rhs_count = vd->value_count;
|
||||||
|
|
||||||
// TODO(bill): Better error messages or is this good enough?
|
// TODO(bill): Better error messages or is this good enough?
|
||||||
if (rhs_count == 0 && vd->type_expression == NULL) {
|
if (rhs_count == 0 && vd->type == NULL) {
|
||||||
error(&c->error_collector, ast_node_token(node), "Missing type or initial expression");
|
error(&c->error_collector, ast_node_token(node), "Missing type or initial expression");
|
||||||
} else if (lhs_count < rhs_count) {
|
} else if (lhs_count < rhs_count) {
|
||||||
error(&c->error_collector, ast_node_token(node), "Extra initial expression");
|
error(&c->error_collector, ast_node_token(node), "Extra initial expression");
|
||||||
@@ -868,32 +868,32 @@ void check_statement(Checker *c, AstNode *node, u32 flags) {
|
|||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case AstNode_ProcedureDeclaration: {
|
case AstNode_ProcDecl: {
|
||||||
auto *pd = &node->procedure_declaration;
|
auto *pd = &node->proc_decl;
|
||||||
Entity *e = make_entity_procedure(c->allocator, c->context.scope, pd->name->identifier.token, NULL);
|
Entity *e = make_entity_procedure(c->allocator, c->context.scope, pd->name->ident.token, NULL);
|
||||||
add_entity(c, c->context.scope, pd->name, e);
|
add_entity(c, c->context.scope, pd->name, e);
|
||||||
|
|
||||||
DeclarationInfo decl = {};
|
DeclInfo decl = {};
|
||||||
init_declaration_info(&decl, e->parent);
|
init_declaration_info(&decl, e->parent);
|
||||||
decl.proc_decl = node;
|
decl.proc_decl = node;
|
||||||
check_procedure_declaration(c, e, &decl, false);
|
check_proc_decl(c, e, &decl, false);
|
||||||
destroy_declaration_info(&decl);
|
destroy_declaration_info(&decl);
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case AstNode_TypeDeclaration: {
|
case AstNode_TypeDecl: {
|
||||||
auto *td = &node->type_declaration;
|
auto *td = &node->type_decl;
|
||||||
AstNode *name = td->name;
|
AstNode *name = td->name;
|
||||||
Entity *e = make_entity_type_name(c->allocator, c->context.scope, name->identifier.token, NULL);
|
Entity *e = make_entity_type_name(c->allocator, c->context.scope, name->ident.token, NULL);
|
||||||
add_entity(c, c->context.scope, name, e);
|
add_entity(c, c->context.scope, name, e);
|
||||||
check_type_declaration(c, e, td->type_expression, NULL);
|
check_type_decl(c, e, td->type, NULL);
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case AstNode_AliasDeclaration: {
|
case AstNode_AliasDecl: {
|
||||||
auto *ad = &node->alias_declaration;
|
auto *ad = &node->alias_decl;
|
||||||
AstNode *name = ad->name;
|
AstNode *name = ad->name;
|
||||||
Entity *e = make_entity_alias_name(c->allocator, c->context.scope, name->identifier.token, NULL);
|
Entity *e = make_entity_alias_name(c->allocator, c->context.scope, name->ident.token, NULL);
|
||||||
add_entity(c, c->context.scope, name, e);
|
add_entity(c, c->context.scope, name, e);
|
||||||
check_alias_declaration(c, e, ad->type_expression, NULL);
|
check_alias_decl(c, e, ad->type, NULL);
|
||||||
} break;
|
} break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ void ssa_gen_code(ssaGen *s) {
|
|||||||
gb_for_array(i, info->entities.entries) {
|
gb_for_array(i, info->entities.entries) {
|
||||||
auto *entry = &info->entities.entries[i];
|
auto *entry = &info->entities.entries[i];
|
||||||
Entity *e = cast(Entity *)cast(uintptr)entry->key;
|
Entity *e = cast(Entity *)cast(uintptr)entry->key;
|
||||||
DeclarationInfo *decl = entry->value;
|
DeclInfo *decl = entry->value;
|
||||||
|
|
||||||
String name = e->token.string;
|
String name = e->token.string;
|
||||||
|
|
||||||
|
|||||||
+29
-4
@@ -228,10 +228,9 @@ void ssa_print_value(gbFile *f, ssaModule *m, ssaValue *value, Type *type_hint)
|
|||||||
|
|
||||||
void ssa_print_instruction(gbFile *f, ssaModule *m, ssaValue *value) {
|
void ssa_print_instruction(gbFile *f, ssaModule *m, ssaValue *value) {
|
||||||
GB_ASSERT(value->kind == ssaValue_Instruction);
|
GB_ASSERT(value->kind == ssaValue_Instruction);
|
||||||
|
ssaInstruction *instr = &value->instruction;
|
||||||
|
|
||||||
ssa_fprintf(f, "\t");
|
ssa_fprintf(f, "\t");
|
||||||
|
|
||||||
ssaInstruction *instr = &value->instruction;
|
|
||||||
switch (instr->kind) {
|
switch (instr->kind) {
|
||||||
case ssaInstruction_Local: {
|
case ssaInstruction_Local: {
|
||||||
Type *type = instr->local.entity->type;
|
Type *type = instr->local.entity->type;
|
||||||
@@ -261,7 +260,7 @@ void ssa_print_instruction(gbFile *f, ssaModule *m, ssaValue *value) {
|
|||||||
} break;
|
} break;
|
||||||
|
|
||||||
case ssaInstruction_Load: {
|
case ssaInstruction_Load: {
|
||||||
Type *type = ssa_value_type(instr->load.address);
|
Type *type = instr->load.type;
|
||||||
ssa_fprintf(f, "%%%d = load ", value->id);
|
ssa_fprintf(f, "%%%d = load ", value->id);
|
||||||
ssa_print_type(f, m->sizes, type);
|
ssa_print_type(f, m->sizes, type);
|
||||||
ssa_fprintf(f, ", ");
|
ssa_fprintf(f, ", ");
|
||||||
@@ -271,6 +270,32 @@ void ssa_print_instruction(gbFile *f, ssaModule *m, ssaValue *value) {
|
|||||||
ssa_fprintf(f, "\n");
|
ssa_fprintf(f, "\n");
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
|
case ssaInstruction_GetElementPtr: {
|
||||||
|
Type *rt = instr->get_element_ptr.result_type;
|
||||||
|
Type *et = instr->get_element_ptr.element_type;
|
||||||
|
Type *t_int = &basic_types[Basic_int];
|
||||||
|
ssa_fprintf(f, "%%%d = getelementptr ", value->id);
|
||||||
|
if (instr->get_element_ptr.inbounds)
|
||||||
|
ssa_fprintf(f, "inbounds ");
|
||||||
|
|
||||||
|
ssa_print_type(f, m->sizes, et);
|
||||||
|
ssa_fprintf(f, ", ");
|
||||||
|
ssa_print_type(f, m->sizes, et);
|
||||||
|
ssa_fprintf(f, "* ");
|
||||||
|
ssa_print_value(f, m, instr->get_element_ptr.address, et);
|
||||||
|
ssa_fprintf(f, ", ");
|
||||||
|
ssa_print_type(f, m->sizes, t_int);
|
||||||
|
ssa_fprintf(f, " ");
|
||||||
|
ssa_print_value(f, m, instr->get_element_ptr.indices[0], t_int);
|
||||||
|
if (instr->get_element_ptr.index_count == 2) {
|
||||||
|
ssa_fprintf(f, ", ");
|
||||||
|
ssa_print_type(f, m->sizes, t_int);
|
||||||
|
ssa_fprintf(f, " ");
|
||||||
|
ssa_print_value(f, m, instr->get_element_ptr.indices[1], t_int);
|
||||||
|
}
|
||||||
|
ssa_fprintf(f, "\n");
|
||||||
|
} break;
|
||||||
|
|
||||||
|
|
||||||
case ssaInstruction_BinaryOp: {
|
case ssaInstruction_BinaryOp: {
|
||||||
auto *bo = &value->instruction.binary_op;
|
auto *bo = &value->instruction.binary_op;
|
||||||
@@ -366,7 +391,7 @@ void ssa_print_llvm_ir(gbFile *f, ssaModule *m) {
|
|||||||
} break;
|
} break;
|
||||||
|
|
||||||
case ssaValue_Global: {
|
case ssaValue_Global: {
|
||||||
ssaGlobal *g = &v->global;
|
auto *g = &v->global;
|
||||||
ssa_print_encoded_global(f, g->entity->token.string);
|
ssa_print_encoded_global(f, g->entity->token.string);
|
||||||
ssa_fprintf(f, " = global ");
|
ssa_fprintf(f, " = global ");
|
||||||
ssa_print_type(f, m->sizes, get_base_type(g->entity->type));
|
ssa_print_type(f, m->sizes, get_base_type(g->entity->type));
|
||||||
|
|||||||
+205
-123
@@ -31,7 +31,7 @@ struct ssaProcedure {
|
|||||||
String name;
|
String name;
|
||||||
Entity *entity;
|
Entity *entity;
|
||||||
Type *type;
|
Type *type;
|
||||||
DeclarationInfo *decl;
|
DeclInfo *decl;
|
||||||
AstNode *type_expr;
|
AstNode *type_expr;
|
||||||
AstNode *body;
|
AstNode *body;
|
||||||
|
|
||||||
@@ -42,36 +42,26 @@ struct ssaProcedure {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
struct ssaTypeName {
|
#define SSA_INSTRUCTION_KINDS \
|
||||||
Entity *entity;
|
SSA_INSTRUCTION_KIND(Invalid), \
|
||||||
Type *type;
|
SSA_INSTRUCTION_KIND(Local), \
|
||||||
};
|
SSA_INSTRUCTION_KIND(Store), \
|
||||||
struct ssaGlobal {
|
SSA_INSTRUCTION_KIND(Load), \
|
||||||
b32 generated;
|
SSA_INSTRUCTION_KIND(GetElementPtr), \
|
||||||
Entity *entity;
|
SSA_INSTRUCTION_KIND(Convert), \
|
||||||
Type *type;
|
SSA_INSTRUCTION_KIND(BinaryOp), \
|
||||||
ssaValue *value;
|
SSA_INSTRUCTION_KIND(Count),
|
||||||
};
|
|
||||||
struct ssaConstant {
|
|
||||||
Type *type;
|
|
||||||
ExactValue value;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
enum ssaInstructionKind {
|
enum ssaInstructionKind {
|
||||||
ssaInstruction_Invalid,
|
#define SSA_INSTRUCTION_KIND(x) GB_JOIN2(ssaInstruction_, x)
|
||||||
|
SSA_INSTRUCTION_KINDS
|
||||||
|
#undef SSA_INSTRUCTION_KIND
|
||||||
|
};
|
||||||
|
|
||||||
ssaInstruction_Local,
|
String const ssa_instruction_strings[] = {
|
||||||
ssaInstruction_Store,
|
#define SSA_INSTRUCTION_KIND(x) {cast(u8 *)#x, gb_size_of(#x)-1}
|
||||||
ssaInstruction_Load,
|
SSA_INSTRUCTION_KINDS
|
||||||
ssaInstruction_GetElementPtr,
|
#undef SSA_INSTRUCTION_KIND
|
||||||
|
|
||||||
ssaInstruction_Convert,
|
|
||||||
|
|
||||||
ssaInstruction_BinaryOp,
|
|
||||||
|
|
||||||
ssaInstruction_Count,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ssaInstruction {
|
struct ssaInstruction {
|
||||||
@@ -91,18 +81,18 @@ struct ssaInstruction {
|
|||||||
ssaValue *value;
|
ssaValue *value;
|
||||||
} store;
|
} store;
|
||||||
struct {
|
struct {
|
||||||
|
Type *type;
|
||||||
ssaValue *address;
|
ssaValue *address;
|
||||||
} load;
|
} load;
|
||||||
struct {
|
struct {
|
||||||
ssaValue *address;
|
ssaValue *address;
|
||||||
Type *result_type;
|
Type * result_type;
|
||||||
Type *element_type;
|
Type * element_type;
|
||||||
isize index_count;
|
ssaValue *indices[2];
|
||||||
isize indices[2];
|
isize index_count;
|
||||||
|
b32 inbounds;
|
||||||
} get_element_ptr;
|
} get_element_ptr;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
struct {
|
struct {
|
||||||
Type *type;
|
Type *type;
|
||||||
Token op;
|
Token op;
|
||||||
@@ -131,9 +121,20 @@ struct ssaValue {
|
|||||||
i32 id;
|
i32 id;
|
||||||
|
|
||||||
union {
|
union {
|
||||||
ssaConstant constant;
|
struct {
|
||||||
ssaTypeName type_name;
|
Type * type;
|
||||||
ssaGlobal global;
|
ExactValue value;
|
||||||
|
} constant;
|
||||||
|
struct {
|
||||||
|
Entity *entity;
|
||||||
|
Type * type;
|
||||||
|
} type_name;
|
||||||
|
struct {
|
||||||
|
b32 generated;
|
||||||
|
Entity * entity;
|
||||||
|
Type * type;
|
||||||
|
ssaValue *value;
|
||||||
|
} global;
|
||||||
ssaProcedure procedure;
|
ssaProcedure procedure;
|
||||||
ssaBlock block;
|
ssaBlock block;
|
||||||
ssaInstruction instruction;
|
ssaInstruction instruction;
|
||||||
@@ -188,7 +189,9 @@ Type *ssa_instruction_type(ssaInstruction *instr) {
|
|||||||
case ssaInstruction_Store:
|
case ssaInstruction_Store:
|
||||||
return ssa_value_type(instr->store.address);
|
return ssa_value_type(instr->store.address);
|
||||||
case ssaInstruction_Load:
|
case ssaInstruction_Load:
|
||||||
return ssa_value_type(instr->load.address);
|
return instr->load.type;
|
||||||
|
case ssaInstruction_GetElementPtr:
|
||||||
|
return instr->get_element_ptr.result_type;
|
||||||
case ssaInstruction_BinaryOp:
|
case ssaInstruction_BinaryOp:
|
||||||
return instr->binary_op.type;
|
return instr->binary_op.type;
|
||||||
}
|
}
|
||||||
@@ -204,7 +207,10 @@ void ssa_instruction_set_type(ssaInstruction *instr, Type *type) {
|
|||||||
ssa_value_set_type(instr->store.value, type);
|
ssa_value_set_type(instr->store.value, type);
|
||||||
break;
|
break;
|
||||||
case ssaInstruction_Load:
|
case ssaInstruction_Load:
|
||||||
// NOTE(bill): Do nothing
|
instr->load.type = type;
|
||||||
|
break;
|
||||||
|
case ssaInstruction_GetElementPtr:
|
||||||
|
instr->get_element_ptr.result_type = type;
|
||||||
break;
|
break;
|
||||||
case ssaInstruction_BinaryOp:
|
case ssaInstruction_BinaryOp:
|
||||||
instr->binary_op.type = type;
|
instr->binary_op.type = type;
|
||||||
@@ -251,8 +257,8 @@ void ssa_value_set_type(ssaValue *value, Type *type) {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
ssaValue *ssa_build_expression(ssaProcedure *proc, AstNode *expr);
|
ssaValue *ssa_build_expr(ssaProcedure *proc, AstNode *expr);
|
||||||
ssaValue *ssa_build_single_expression(ssaProcedure *proc, AstNode *expr, TypeAndValue *tv);
|
ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue *tv);
|
||||||
ssaLvalue ssa_build_address(ssaProcedure *proc, AstNode *expr);
|
ssaLvalue ssa_build_address(ssaProcedure *proc, AstNode *expr);
|
||||||
ssaValue *ssa_emit_conversion(ssaProcedure *proc, ssaValue *value, Type *a_type);
|
ssaValue *ssa_emit_conversion(ssaProcedure *proc, ssaValue *value, Type *a_type);
|
||||||
|
|
||||||
@@ -318,16 +324,24 @@ ssaValue *ssa_make_instruction_load(ssaProcedure *p, ssaValue *address) {
|
|||||||
ssaValue *v = ssa_alloc_instruction(p->module->allocator, ssaInstruction_Load);
|
ssaValue *v = ssa_alloc_instruction(p->module->allocator, ssaInstruction_Load);
|
||||||
ssaInstruction *i = &v->instruction;
|
ssaInstruction *i = &v->instruction;
|
||||||
i->load.address = address;
|
i->load.address = address;
|
||||||
|
i->load.type = ssa_value_type(address);
|
||||||
if (p->curr_block) {
|
if (p->curr_block) {
|
||||||
gb_array_append(p->curr_block->values, v);
|
gb_array_append(p->curr_block->values, v);
|
||||||
}
|
}
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
ssaValue *ssa_make_instruction_get_element_ptr(ssaProcedure *p, ssaValue *address) {
|
ssaValue *ssa_make_instruction_get_element_ptr(ssaProcedure *p, ssaValue *address,
|
||||||
|
ssaValue *index0, ssaValue *index1, isize index_count,
|
||||||
|
b32 inbounds) {
|
||||||
ssaValue *v = ssa_alloc_instruction(p->module->allocator, ssaInstruction_GetElementPtr);
|
ssaValue *v = ssa_alloc_instruction(p->module->allocator, ssaInstruction_GetElementPtr);
|
||||||
ssaInstruction *i = &v->instruction;
|
ssaInstruction *i = &v->instruction;
|
||||||
i->get_element_ptr.address = address;
|
i->get_element_ptr.address = address;
|
||||||
|
i->get_element_ptr.indices[0] = index0;
|
||||||
|
i->get_element_ptr.indices[1] = index1;
|
||||||
|
i->get_element_ptr.index_count = index_count;
|
||||||
|
i->get_element_ptr.element_type = ssa_value_type(address);
|
||||||
|
i->get_element_ptr.inbounds = inbounds;
|
||||||
if (p->curr_block) {
|
if (p->curr_block) {
|
||||||
gb_array_append(p->curr_block->values, v);
|
gb_array_append(p->curr_block->values, v);
|
||||||
}
|
}
|
||||||
@@ -354,7 +368,7 @@ ssaValue *ssa_make_value_constant(gbAllocator a, Type *type, ExactValue value) {
|
|||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
ssaValue *ssa_make_value_procedure(gbAllocator a, Entity *e, DeclarationInfo *decl, ssaModule *m) {
|
ssaValue *ssa_make_value_procedure(gbAllocator a, Entity *e, DeclInfo *decl, ssaModule *m) {
|
||||||
ssaValue *v = ssa_alloc_value(a, ssaValue_Procedure);
|
ssaValue *v = ssa_alloc_value(a, ssaValue_Procedure);
|
||||||
v->procedure.module = m;
|
v->procedure.module = m;
|
||||||
v->procedure.entity = e;
|
v->procedure.entity = e;
|
||||||
@@ -446,8 +460,8 @@ void ssa_end_procedure_body(ssaProcedure *proc) {
|
|||||||
|
|
||||||
|
|
||||||
b32 ssa_is_blank_identifier(AstNode *i) {
|
b32 ssa_is_blank_identifier(AstNode *i) {
|
||||||
GB_ASSERT(i->kind == AstNode_Identifier);
|
GB_ASSERT(i->kind == AstNode_Ident);
|
||||||
return are_strings_equal(i->identifier.token.string, make_string("_"));
|
return are_strings_equal(i->ident.token.string, make_string("_"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -485,8 +499,6 @@ ssaValue *ssa_emit_store(ssaProcedure *p, ssaValue *address, ssaValue *value) {
|
|||||||
|
|
||||||
ssaValue *ssa_emit_load(ssaProcedure *p, ssaValue *address) {
|
ssaValue *ssa_emit_load(ssaProcedure *p, ssaValue *address) {
|
||||||
ssaValue *v = ssa_make_instruction_load(p, address);
|
ssaValue *v = ssa_make_instruction_load(p, address);
|
||||||
Type *t = ssa_value_type(address);
|
|
||||||
ssa_value_set_type(v, type_deref(t));
|
|
||||||
ssa_emit(p, v);
|
ssa_emit(p, v);
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
@@ -590,12 +602,12 @@ ssaValue *ssa_emit_compare(ssaProcedure *proc, Token op, ssaValue *left, ssaValu
|
|||||||
return ssa_emit(proc, v);
|
return ssa_emit(proc, v);
|
||||||
}
|
}
|
||||||
|
|
||||||
ssaValue *ssa_build_single_expression(ssaProcedure *proc, AstNode *expr, TypeAndValue *tv) {
|
ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue *tv) {
|
||||||
switch (expr->kind) {
|
switch (expr->kind) {
|
||||||
case AstNode_Identifier: {
|
case AstNode_Ident: {
|
||||||
Entity *e = *map_get(&proc->module->info->uses, hash_pointer(expr));
|
Entity *e = *map_get(&proc->module->info->uses, hash_pointer(expr));
|
||||||
if (e->kind == Entity_Builtin) {
|
if (e->kind == Entity_Builtin) {
|
||||||
// TODO(bill): Entity_Builtin
|
GB_PANIC("TODO(bill): Entity_Builtin");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -605,32 +617,34 @@ ssaValue *ssa_build_single_expression(ssaProcedure *proc, AstNode *expr, TypeAnd
|
|||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case AstNode_ParenExpression:
|
case AstNode_ParenExpr:
|
||||||
return ssa_build_single_expression(proc, unparen_expression(expr), tv);
|
return ssa_build_single_expr(proc, unparen_expr(expr), tv);
|
||||||
|
|
||||||
case AstNode_DereferenceExpression: {
|
case AstNode_DerefExpr: {
|
||||||
ssaLvalue addr = ssa_build_address(proc, expr->dereference_expression.operand);
|
ssaLvalue addr = ssa_build_address(proc, expr);
|
||||||
return ssa_lvalue_load(addr, proc);
|
ssaValue *load = ssa_lvalue_load(addr, proc);
|
||||||
|
ssa_value_set_type(load, type_deref(ssa_value_type(load)));
|
||||||
|
return load;
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case AstNode_UnaryExpression: {
|
case AstNode_UnaryExpr: {
|
||||||
auto *ue = &expr->unary_expression;
|
auto *ue = &expr->unary_expr;
|
||||||
switch (ue->op.kind) {
|
switch (ue->op.kind) {
|
||||||
case Token_Pointer:
|
case Token_Pointer:
|
||||||
return ssa_lvalue_address(ssa_build_address(proc, ue->operand), proc);
|
return ssa_lvalue_address(ssa_build_address(proc, ue->expr), proc);
|
||||||
case Token_Add:
|
case Token_Add:
|
||||||
return ssa_build_expression(proc, ue->operand);
|
return ssa_build_expr(proc, ue->expr);
|
||||||
case Token_Sub: {
|
case Token_Sub: {
|
||||||
// NOTE(bill): -x == 0 - x
|
// NOTE(bill): -`x` == 0 - `x`
|
||||||
ExactValue zero = make_exact_value_integer(0);
|
ExactValue zero = make_exact_value_integer(0);
|
||||||
ssaValue *left = ssa_make_value_constant(proc->module->allocator, tv->type, zero);
|
ssaValue *left = ssa_make_value_constant(proc->module->allocator, tv->type, zero);
|
||||||
ssaValue *right = ssa_build_expression(proc, ue->operand);
|
ssaValue *right = ssa_build_expr(proc, ue->expr);
|
||||||
return ssa_emit_arith(proc, ue->op, left, right, tv->type);
|
return ssa_emit_arith(proc, ue->op, left, right, tv->type);
|
||||||
} break;
|
} break;
|
||||||
case Token_Xor: { // Bitwise not
|
case Token_Xor: { // Bitwise not
|
||||||
// NOTE(bill): "not" x == x "xor" -1
|
// NOTE(bill): "not" `x` == `x` "xor" `-1`
|
||||||
ExactValue neg_one = make_exact_value_integer(-1);
|
ExactValue neg_one = make_exact_value_integer(-1);
|
||||||
ssaValue *left = ssa_build_expression(proc, ue->operand);
|
ssaValue *left = ssa_build_expr(proc, ue->expr);
|
||||||
ssaValue *right = ssa_make_value_constant(proc->module->allocator, tv->type, neg_one);
|
ssaValue *right = ssa_make_value_constant(proc->module->allocator, tv->type, neg_one);
|
||||||
return ssa_emit_arith(proc, ue->op, left, right, tv->type);
|
return ssa_emit_arith(proc, ue->op, left, right, tv->type);
|
||||||
} break;
|
} break;
|
||||||
@@ -641,8 +655,8 @@ ssaValue *ssa_build_single_expression(ssaProcedure *proc, AstNode *expr, TypeAnd
|
|||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case AstNode_BinaryExpression: {
|
case AstNode_BinaryExpr: {
|
||||||
auto *be = &expr->binary_expression;
|
auto *be = &expr->binary_expr;
|
||||||
switch (be->op.kind) {
|
switch (be->op.kind) {
|
||||||
case Token_Add:
|
case Token_Add:
|
||||||
case Token_Sub:
|
case Token_Sub:
|
||||||
@@ -653,17 +667,17 @@ ssaValue *ssa_build_single_expression(ssaProcedure *proc, AstNode *expr, TypeAnd
|
|||||||
case Token_Or:
|
case Token_Or:
|
||||||
case Token_Xor:
|
case Token_Xor:
|
||||||
return ssa_emit_arith(proc, be->op,
|
return ssa_emit_arith(proc, be->op,
|
||||||
ssa_build_expression(proc, be->left),
|
ssa_build_expr(proc, be->left),
|
||||||
ssa_build_expression(proc, be->right),
|
ssa_build_expr(proc, be->right),
|
||||||
tv->type);
|
tv->type);
|
||||||
|
|
||||||
case Token_AndNot: {
|
case Token_AndNot: {
|
||||||
AstNode ue = {AstNode_UnaryExpression};
|
AstNode ue = {AstNode_UnaryExpr};
|
||||||
ue.unary_expression.op = be->op;
|
ue.unary_expr.op = be->op;
|
||||||
ue.unary_expression.op.kind = Token_Xor;
|
ue.unary_expr.op.kind = Token_Xor;
|
||||||
ue.unary_expression.operand = be->right;
|
ue.unary_expr.expr = be->right;
|
||||||
ssaValue *left = ssa_build_expression(proc, be->left);
|
ssaValue *left = ssa_build_expr(proc, be->left);
|
||||||
ssaValue *right = ssa_build_expression(proc, &ue);
|
ssaValue *right = ssa_build_expr(proc, &ue);
|
||||||
Token op = be->op;
|
Token op = be->op;
|
||||||
op.kind = Token_And;
|
op.kind = Token_And;
|
||||||
return ssa_emit_arith(proc, op, left, right, tv->type);
|
return ssa_emit_arith(proc, op, left, right, tv->type);
|
||||||
@@ -676,8 +690,8 @@ ssaValue *ssa_build_single_expression(ssaProcedure *proc, AstNode *expr, TypeAnd
|
|||||||
case Token_Gt:
|
case Token_Gt:
|
||||||
case Token_GtEq: {
|
case Token_GtEq: {
|
||||||
ssaValue *cmp = ssa_emit_compare(proc, be->op,
|
ssaValue *cmp = ssa_emit_compare(proc, be->op,
|
||||||
ssa_build_expression(proc, be->left),
|
ssa_build_expr(proc, be->left),
|
||||||
ssa_build_expression(proc, be->right));
|
ssa_build_expr(proc, be->right));
|
||||||
return ssa_emit_conversion(proc, cmp, default_type(tv->type));
|
return ssa_emit_conversion(proc, cmp, default_type(tv->type));
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
@@ -685,18 +699,43 @@ ssaValue *ssa_build_single_expression(ssaProcedure *proc, AstNode *expr, TypeAnd
|
|||||||
GB_PANIC("Invalid binary expression");
|
GB_PANIC("Invalid binary expression");
|
||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
case AstNode_ProcedureLiteral:
|
case AstNode_ProcLit:
|
||||||
break;
|
break;
|
||||||
case AstNode_CastExpression:
|
case AstNode_CastExpr:
|
||||||
break;
|
break;
|
||||||
case AstNode_CallExpression:
|
case AstNode_CallExpr:
|
||||||
break;
|
break;
|
||||||
case AstNode_SliceExpression:
|
case AstNode_SliceExpr:
|
||||||
break;
|
break;
|
||||||
case AstNode_IndexExpression: {
|
case AstNode_IndexExpr: {
|
||||||
|
auto *ie = &expr->index_expr;
|
||||||
|
Type *t = type_of_expr(proc->module->info, ie->expr);
|
||||||
|
t = get_base_type(t);
|
||||||
|
switch (t->kind) {
|
||||||
|
case Type_Basic: {
|
||||||
|
// TODO(bill): Strings AstNode_IndexExpression
|
||||||
|
} break;
|
||||||
|
|
||||||
|
case Type_Array: {
|
||||||
|
Type *t_int = &basic_types[Basic_int];
|
||||||
|
ssaValue *e = ssa_lvalue_address(ssa_build_address(proc, ie->expr), proc);
|
||||||
|
ssaValue *i0 = ssa_make_value_constant(proc->module->allocator, t_int, make_exact_value_integer(0));
|
||||||
|
ssaValue *i1 = ssa_emit_conversion(proc, ssa_build_expr(proc, ie->index), t_int);
|
||||||
|
ssaValue *gep = ssa_make_instruction_get_element_ptr(proc, e,
|
||||||
|
i0, i1, 2,
|
||||||
|
true);
|
||||||
|
ssa_value_set_type(gep, t->array.element);
|
||||||
|
return ssa_emit_load(proc, ssa_emit(proc, gep));
|
||||||
|
} break;
|
||||||
|
|
||||||
|
case Type_Slice:
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Type_Pointer:
|
||||||
|
break;
|
||||||
|
}
|
||||||
} break;
|
} break;
|
||||||
case AstNode_SelectorExpression:
|
case AstNode_SelectorExpr:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -705,8 +744,8 @@ ssaValue *ssa_build_single_expression(ssaProcedure *proc, AstNode *expr, TypeAnd
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
ssaValue *ssa_build_expression(ssaProcedure *proc, AstNode *expr) {
|
ssaValue *ssa_build_expr(ssaProcedure *proc, AstNode *expr) {
|
||||||
expr = unparen_expression(expr);
|
expr = unparen_expr(expr);
|
||||||
|
|
||||||
TypeAndValue *tv = map_get(&proc->module->info->types, hash_pointer(expr));
|
TypeAndValue *tv = map_get(&proc->module->info->types, hash_pointer(expr));
|
||||||
if (tv) {
|
if (tv) {
|
||||||
@@ -723,7 +762,7 @@ ssaValue *ssa_build_expression(ssaProcedure *proc, AstNode *expr) {
|
|||||||
gb_printf("!Addressable!\n");
|
gb_printf("!Addressable!\n");
|
||||||
// TODO(bill): Addressing_Variable
|
// TODO(bill): Addressing_Variable
|
||||||
} else {
|
} else {
|
||||||
value = ssa_build_single_expression(proc, expr, tv);
|
value = ssa_build_single_expr(proc, expr, tv);
|
||||||
}
|
}
|
||||||
|
|
||||||
return value;
|
return value;
|
||||||
@@ -734,9 +773,9 @@ ssaValue *ssa_build_expression(ssaProcedure *proc, AstNode *expr) {
|
|||||||
|
|
||||||
ssaLvalue ssa_build_address(ssaProcedure *proc, AstNode *expr) {
|
ssaLvalue ssa_build_address(ssaProcedure *proc, AstNode *expr) {
|
||||||
switch (expr->kind) {
|
switch (expr->kind) {
|
||||||
case AstNode_Identifier: {
|
case AstNode_Ident: {
|
||||||
if (!ssa_is_blank_identifier(expr)) {
|
if (!ssa_is_blank_identifier(expr)) {
|
||||||
Entity *e = entity_of_identifier(proc->module->info, expr);
|
Entity *e = entity_of_ident(proc->module->info, expr);
|
||||||
|
|
||||||
ssaLvalue val = {ssaLvalue_Address};
|
ssaLvalue val = {ssaLvalue_Address};
|
||||||
val.address.expr = expr;
|
val.address.expr = expr;
|
||||||
@@ -748,22 +787,65 @@ ssaLvalue ssa_build_address(ssaProcedure *proc, AstNode *expr) {
|
|||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case AstNode_ParenExpression:
|
case AstNode_ParenExpr:
|
||||||
return ssa_build_address(proc, unparen_expression(expr));
|
return ssa_build_address(proc, unparen_expr(expr));
|
||||||
|
|
||||||
|
/*
|
||||||
|
ssaLvalue addr = ssa_build_address(proc, expr->dereference_expr.operand);
|
||||||
|
ssaValue *load = ssa_lvalue_load(addr, proc);
|
||||||
|
ssaValue *deref = ssa_emit_load(proc, load);
|
||||||
|
ssa_value_set_type(deref, type_deref(ssa_value_type(deref)));
|
||||||
|
return deref;
|
||||||
|
*/
|
||||||
|
|
||||||
|
#if 1
|
||||||
|
case AstNode_DerefExpr: {
|
||||||
|
AstNode *operand = expr->deref_expr.expr;
|
||||||
|
ssaLvalue addr = ssa_build_address(proc, operand);
|
||||||
|
ssaValue *value = ssa_lvalue_load(addr, proc);
|
||||||
|
|
||||||
case AstNode_DereferenceExpression: {
|
|
||||||
ssaLvalue val = {ssaLvalue_Address};
|
ssaLvalue val = {ssaLvalue_Address};
|
||||||
AstNode *operand = expr->dereference_expression.operand;
|
val.address.value = value;
|
||||||
val.address.value = ssa_build_expression(proc, operand);
|
|
||||||
val.address.expr = expr;
|
val.address.expr = expr;
|
||||||
return val;
|
return val;
|
||||||
} break;
|
} break;
|
||||||
|
#endif
|
||||||
|
|
||||||
case AstNode_SelectorExpression:
|
case AstNode_SelectorExpr:
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case AstNode_IndexExpression:
|
case AstNode_IndexExpr: {
|
||||||
break;
|
ssaValue *v = NULL;
|
||||||
|
Type *element_type = NULL;
|
||||||
|
auto *ie = &expr->index_expr;
|
||||||
|
Type *t = type_of_expr(proc->module->info, expr->index_expr.expr);
|
||||||
|
t = get_base_type(t);
|
||||||
|
switch (t->kind) {
|
||||||
|
case Type_Array: {
|
||||||
|
Type *t_int = &basic_types[Basic_int];
|
||||||
|
ssaValue *e = ssa_lvalue_address(ssa_build_address(proc, ie->expr), proc);
|
||||||
|
ssaValue *i0 = ssa_make_value_constant(proc->module->allocator, t_int, make_exact_value_integer(0));
|
||||||
|
ssaValue *i1 = ssa_emit_conversion(proc, ssa_build_expr(proc, ie->index), t_int);
|
||||||
|
ssaValue *gep = ssa_make_instruction_get_element_ptr(proc, e,
|
||||||
|
i0, i1, 2,
|
||||||
|
true);
|
||||||
|
element_type = t->array.element;
|
||||||
|
v = gep;
|
||||||
|
} break;
|
||||||
|
case Type_Pointer:
|
||||||
|
GB_PANIC("ssa_build_address AstNode_IndexExpression Type_Slice");
|
||||||
|
break;
|
||||||
|
case Type_Slice:
|
||||||
|
GB_PANIC("ssa_build_address AstNode_IndexExpression Type_Slice");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
ssa_value_set_type(v, element_type);
|
||||||
|
ssaLvalue val = {ssaLvalue_Address};
|
||||||
|
val.address.value = ssa_emit(proc, v);
|
||||||
|
val.address.expr = expr;
|
||||||
|
return val;
|
||||||
|
} break;
|
||||||
|
|
||||||
// TODO(bill): Others address
|
// TODO(bill): Others address
|
||||||
}
|
}
|
||||||
@@ -782,19 +864,19 @@ void ssa_build_assign_op(ssaProcedure *proc, ssaLvalue lhs, ssaValue *value, Tok
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void ssa_build_statement(ssaProcedure *proc, AstNode *s);
|
void ssa_build_stmt(ssaProcedure *proc, AstNode *s);
|
||||||
|
|
||||||
void ssa_build_statement_list(ssaProcedure *proc, AstNode *list) {
|
void ssa_build_stmt_list(ssaProcedure *proc, AstNode *list) {
|
||||||
for (AstNode *stmt = list ; stmt != NULL; stmt = stmt->next)
|
for (AstNode *stmt = list ; stmt != NULL; stmt = stmt->next)
|
||||||
ssa_build_statement(proc, stmt);
|
ssa_build_stmt(proc, stmt);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ssa_build_statement(ssaProcedure *proc, AstNode *s) {
|
void ssa_build_stmt(ssaProcedure *proc, AstNode *s) {
|
||||||
switch (s->kind) {
|
switch (s->kind) {
|
||||||
case AstNode_EmptyStatement:
|
case AstNode_EmptyStmt:
|
||||||
break;
|
break;
|
||||||
case AstNode_VariableDeclaration: {
|
case AstNode_VarDecl: {
|
||||||
auto *vd = &s->variable_declaration;
|
auto *vd = &s->var_decl;
|
||||||
if (vd->kind == Declaration_Mutable) {
|
if (vd->kind == Declaration_Mutable) {
|
||||||
if (vd->name_count == vd->value_count) { // 1:1 assigment
|
if (vd->name_count == vd->value_count) { // 1:1 assigment
|
||||||
gbArray(ssaLvalue) lvals;
|
gbArray(ssaLvalue) lvals;
|
||||||
@@ -815,7 +897,7 @@ void ssa_build_statement(ssaProcedure *proc, AstNode *s) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (AstNode *value = vd->value_list; value != NULL; value = value->next) {
|
for (AstNode *value = vd->value_list; value != NULL; value = value->next) {
|
||||||
ssaValue *init = ssa_build_expression(proc, value);
|
ssaValue *init = ssa_build_expr(proc, value);
|
||||||
gb_array_append(inits, init);
|
gb_array_append(inits, init);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -837,22 +919,22 @@ void ssa_build_statement(ssaProcedure *proc, AstNode *s) {
|
|||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case AstNode_IncDecStatement: {
|
case AstNode_IncDecStmt: {
|
||||||
Token op = s->inc_dec_statement.op;
|
Token op = s->inc_dec_stmt.op;
|
||||||
if (op.kind == Token_Increment) {
|
if (op.kind == Token_Increment) {
|
||||||
op.kind = Token_Add;
|
op.kind = Token_Add;
|
||||||
} else if (op.kind == Token_Decrement) {
|
} else if (op.kind == Token_Decrement) {
|
||||||
op.kind = Token_Sub;
|
op.kind = Token_Sub;
|
||||||
}
|
}
|
||||||
ssaLvalue lval = ssa_build_address(proc, s->inc_dec_statement.expression);
|
ssaLvalue lval = ssa_build_address(proc, s->inc_dec_stmt.expr);
|
||||||
ssaValue *one = ssa_make_value_constant(proc->module->allocator, ssa_lvalue_type(lval),
|
ssaValue *one = ssa_make_value_constant(proc->module->allocator, ssa_lvalue_type(lval),
|
||||||
make_exact_value_integer(1));
|
make_exact_value_integer(1));
|
||||||
ssa_build_assign_op(proc, lval, one, op);
|
ssa_build_assign_op(proc, lval, one, op);
|
||||||
|
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case AstNode_AssignStatement: {
|
case AstNode_AssignStmt: {
|
||||||
auto *assign = &s->assign_statement;
|
auto *assign = &s->assign_stmt;
|
||||||
switch (assign->op.kind) {
|
switch (assign->op.kind) {
|
||||||
case Token_Eq: {
|
case Token_Eq: {
|
||||||
gbArray(ssaLvalue) lvals;
|
gbArray(ssaLvalue) lvals;
|
||||||
@@ -873,7 +955,7 @@ void ssa_build_statement(ssaProcedure *proc, AstNode *s) {
|
|||||||
if (assign->lhs_count == 1) {
|
if (assign->lhs_count == 1) {
|
||||||
AstNode *lhs = assign->lhs_list;
|
AstNode *lhs = assign->lhs_list;
|
||||||
AstNode *rhs = assign->rhs_list;
|
AstNode *rhs = assign->rhs_list;
|
||||||
ssaValue *init = ssa_build_expression(proc, rhs);
|
ssaValue *init = ssa_build_expr(proc, rhs);
|
||||||
ssa_lvalue_store(lvals[0], proc, init);
|
ssa_lvalue_store(lvals[0], proc, init);
|
||||||
} else {
|
} else {
|
||||||
gbArray(ssaValue *) inits;
|
gbArray(ssaValue *) inits;
|
||||||
@@ -881,7 +963,7 @@ void ssa_build_statement(ssaProcedure *proc, AstNode *s) {
|
|||||||
defer (gb_array_free(inits));
|
defer (gb_array_free(inits));
|
||||||
|
|
||||||
for (AstNode *rhs = assign->rhs_list; rhs != NULL; rhs = rhs->next) {
|
for (AstNode *rhs = assign->rhs_list; rhs != NULL; rhs = rhs->next) {
|
||||||
ssaValue *init = ssa_build_expression(proc, rhs);
|
ssaValue *init = ssa_build_expr(proc, rhs);
|
||||||
gb_array_append(inits, init);
|
gb_array_append(inits, init);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -903,33 +985,33 @@ void ssa_build_statement(ssaProcedure *proc, AstNode *s) {
|
|||||||
kind += Token_Add - Token_AddEq; // Convert += to +
|
kind += Token_Add - Token_AddEq; // Convert += to +
|
||||||
op.kind = cast(TokenKind)kind;
|
op.kind = cast(TokenKind)kind;
|
||||||
ssaLvalue lhs = ssa_build_address(proc, assign->lhs_list);
|
ssaLvalue lhs = ssa_build_address(proc, assign->lhs_list);
|
||||||
ssaValue *value = ssa_build_expression(proc, assign->rhs_list);
|
ssaValue *value = ssa_build_expr(proc, assign->rhs_list);
|
||||||
ssa_build_assign_op(proc, lhs, value, op);
|
ssa_build_assign_op(proc, lhs, value, op);
|
||||||
} break;
|
} break;
|
||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case AstNode_ExpressionStatement:
|
case AstNode_ExprStmt:
|
||||||
ssa_build_expression(proc, s->expression_statement.expression);
|
ssa_build_expr(proc, s->expr_stmt.expr);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case AstNode_BlockStatement:
|
case AstNode_BlockStmt:
|
||||||
ssa_build_statement_list(proc, s->block_statement.list);
|
ssa_build_stmt_list(proc, s->block_stmt.list);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case AstNode_IfStatement:
|
case AstNode_IfStmt:
|
||||||
GB_PANIC("AstNode_IfStatement");
|
GB_PANIC("AstNode_IfStatement");
|
||||||
break;
|
break;
|
||||||
case AstNode_ReturnStatement:
|
case AstNode_ReturnStmt:
|
||||||
GB_PANIC("AstNode_ReturnStatement");
|
GB_PANIC("AstNode_ReturnStatement");
|
||||||
break;
|
break;
|
||||||
case AstNode_ForStatement:
|
case AstNode_ForStmt:
|
||||||
GB_PANIC("AstNode_ForStatement");
|
GB_PANIC("AstNode_ForStatement");
|
||||||
break;
|
break;
|
||||||
case AstNode_DeferStatement:
|
case AstNode_DeferStmt:
|
||||||
GB_PANIC("AstNode_DeferStatement");
|
GB_PANIC("AstNode_DeferStatement");
|
||||||
break;
|
break;
|
||||||
case AstNode_BranchStatement:
|
case AstNode_BranchStmt:
|
||||||
GB_PANIC("AstNode_BranchStatement");
|
GB_PANIC("AstNode_BranchStatement");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -945,9 +1027,9 @@ void ssa_build_procedure(ssaValue *value) {
|
|||||||
|
|
||||||
AstNode *proc_decl = proc->decl->proc_decl;
|
AstNode *proc_decl = proc->decl->proc_decl;
|
||||||
switch (proc_decl->kind) {
|
switch (proc_decl->kind) {
|
||||||
case AstNode_ProcedureDeclaration:
|
case AstNode_ProcDecl:
|
||||||
proc->type_expr = proc_decl->procedure_declaration.type;
|
proc->type_expr = proc_decl->proc_decl.type;
|
||||||
proc->body = proc_decl->procedure_declaration.body;
|
proc->body = proc_decl->proc_decl.body;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return;
|
return;
|
||||||
@@ -960,7 +1042,7 @@ void ssa_build_procedure(ssaValue *value) {
|
|||||||
|
|
||||||
|
|
||||||
ssa_begin_procedure_body(proc);
|
ssa_begin_procedure_body(proc);
|
||||||
ssa_build_statement(proc, proc->body);
|
ssa_build_stmt(proc, proc->body);
|
||||||
ssa_end_procedure_body(proc);
|
ssa_end_procedure_body(proc);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+612
-602
File diff suppressed because it is too large
Load Diff
+86
-86
@@ -10,190 +10,190 @@ void print_ast(AstNode *node, isize indent) {
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
switch (node->kind) {
|
switch (node->kind) {
|
||||||
case AstNode_BasicLiteral:
|
case AstNode_BasicLit:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
print_token(node->basic_literal);
|
print_token(node->basic_lit);
|
||||||
break;
|
break;
|
||||||
case AstNode_Identifier:
|
case AstNode_Ident:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
print_token(node->identifier.token);
|
print_token(node->ident.token);
|
||||||
break;
|
break;
|
||||||
case AstNode_ProcedureLiteral:
|
case AstNode_ProcLit:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
gb_printf("(proc lit)\n");
|
gb_printf("(proc lit)\n");
|
||||||
print_ast(node->procedure_literal.type, indent+1);
|
print_ast(node->proc_lit.type, indent+1);
|
||||||
print_ast(node->procedure_literal.body, indent+1);
|
print_ast(node->proc_lit.body, indent+1);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case AstNode_CompoundLiteral:
|
case AstNode_CompoundLit:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
gb_printf("(compound lit)\n");
|
gb_printf("(compound lit)\n");
|
||||||
print_ast(node->compound_literal.type_expression, indent+1);
|
print_ast(node->compound_lit.type, indent+1);
|
||||||
print_ast(node->compound_literal.element_list, indent+1);
|
print_ast(node->compound_lit.elem_list, indent+1);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
||||||
case AstNode_TagExpression:
|
case AstNode_TagExpr:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
gb_printf("(tag)\n");
|
gb_printf("(tag)\n");
|
||||||
print_indent(indent+1);
|
print_indent(indent+1);
|
||||||
print_token(node->tag_expression.name);
|
print_token(node->tag_expr.name);
|
||||||
print_ast(node->tag_expression.expression, indent+1);
|
print_ast(node->tag_expr.expr, indent+1);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case AstNode_UnaryExpression:
|
case AstNode_UnaryExpr:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
print_token(node->unary_expression.op);
|
print_token(node->unary_expr.op);
|
||||||
print_ast(node->unary_expression.operand, indent+1);
|
print_ast(node->unary_expr.expr, indent+1);
|
||||||
break;
|
break;
|
||||||
case AstNode_BinaryExpression:
|
case AstNode_BinaryExpr:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
print_token(node->binary_expression.op);
|
print_token(node->binary_expr.op);
|
||||||
print_ast(node->binary_expression.left, indent+1);
|
print_ast(node->binary_expr.left, indent+1);
|
||||||
print_ast(node->binary_expression.right, indent+1);
|
print_ast(node->binary_expr.right, indent+1);
|
||||||
break;
|
break;
|
||||||
case AstNode_CallExpression:
|
case AstNode_CallExpr:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
gb_printf("(call)\n");
|
gb_printf("(call)\n");
|
||||||
print_ast(node->call_expression.proc, indent+1);
|
print_ast(node->call_expr.proc, indent+1);
|
||||||
print_ast(node->call_expression.arg_list, indent+1);
|
print_ast(node->call_expr.arg_list, indent+1);
|
||||||
break;
|
break;
|
||||||
case AstNode_SelectorExpression:
|
case AstNode_SelectorExpr:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
gb_printf(".\n");
|
gb_printf(".\n");
|
||||||
print_ast(node->selector_expression.operand, indent+1);
|
print_ast(node->selector_expr.expr, indent+1);
|
||||||
print_ast(node->selector_expression.selector, indent+1);
|
print_ast(node->selector_expr.selector, indent+1);
|
||||||
break;
|
break;
|
||||||
case AstNode_IndexExpression:
|
case AstNode_IndexExpr:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
gb_printf("([])\n");
|
gb_printf("([])\n");
|
||||||
print_ast(node->index_expression.expression, indent+1);
|
print_ast(node->index_expr.expr, indent+1);
|
||||||
print_ast(node->index_expression.value, indent+1);
|
print_ast(node->index_expr.index, indent+1);
|
||||||
break;
|
break;
|
||||||
case AstNode_CastExpression:
|
case AstNode_CastExpr:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
gb_printf("(cast)\n");
|
gb_printf("(cast)\n");
|
||||||
print_ast(node->cast_expression.type_expression, indent+1);
|
print_ast(node->cast_expr.type, indent+1);
|
||||||
print_ast(node->cast_expression.operand, indent+1);
|
print_ast(node->cast_expr.expr, indent+1);
|
||||||
break;
|
break;
|
||||||
case AstNode_DereferenceExpression:
|
case AstNode_DerefExpr:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
gb_printf("(dereference)\n");
|
gb_printf("(deref)\n");
|
||||||
print_ast(node->dereference_expression.operand, indent+1);
|
print_ast(node->deref_expr.expr, indent+1);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
||||||
case AstNode_ExpressionStatement:
|
case AstNode_ExprStmt:
|
||||||
print_ast(node->expression_statement.expression, indent);
|
print_ast(node->expr_stmt.expr, indent);
|
||||||
break;
|
break;
|
||||||
case AstNode_IncDecStatement:
|
case AstNode_IncDecStmt:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
print_token(node->inc_dec_statement.op);
|
print_token(node->inc_dec_stmt.op);
|
||||||
print_ast(node->inc_dec_statement.expression, indent+1);
|
print_ast(node->inc_dec_stmt.expr, indent+1);
|
||||||
break;
|
break;
|
||||||
case AstNode_AssignStatement:
|
case AstNode_AssignStmt:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
print_token(node->assign_statement.op);
|
print_token(node->assign_stmt.op);
|
||||||
print_ast(node->assign_statement.lhs_list, indent+1);
|
print_ast(node->assign_stmt.lhs_list, indent+1);
|
||||||
print_ast(node->assign_statement.rhs_list, indent+1);
|
print_ast(node->assign_stmt.rhs_list, indent+1);
|
||||||
break;
|
break;
|
||||||
case AstNode_BlockStatement:
|
case AstNode_BlockStmt:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
gb_printf("(block)\n");
|
gb_printf("(block)\n");
|
||||||
print_ast(node->block_statement.list, indent+1);
|
print_ast(node->block_stmt.list, indent+1);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case AstNode_IfStatement:
|
case AstNode_IfStmt:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
gb_printf("(if)\n");
|
gb_printf("(if)\n");
|
||||||
print_ast(node->if_statement.cond, indent+1);
|
print_ast(node->if_stmt.cond, indent+1);
|
||||||
print_ast(node->if_statement.body, indent+1);
|
print_ast(node->if_stmt.body, indent+1);
|
||||||
if (node->if_statement.else_statement) {
|
if (node->if_stmt.else_stmt) {
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
gb_printf("(else)\n");
|
gb_printf("(else)\n");
|
||||||
print_ast(node->if_statement.else_statement, indent+1);
|
print_ast(node->if_stmt.else_stmt, indent+1);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case AstNode_ReturnStatement:
|
case AstNode_ReturnStmt:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
gb_printf("(return)\n");
|
gb_printf("(return)\n");
|
||||||
print_ast(node->return_statement.results, indent+1);
|
print_ast(node->return_stmt.result_list, indent+1);
|
||||||
break;
|
break;
|
||||||
case AstNode_ForStatement:
|
case AstNode_ForStmt:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
gb_printf("(for)\n");
|
gb_printf("(for)\n");
|
||||||
print_ast(node->for_statement.init, indent+1);
|
print_ast(node->for_stmt.init, indent+1);
|
||||||
print_ast(node->for_statement.cond, indent+1);
|
print_ast(node->for_stmt.cond, indent+1);
|
||||||
print_ast(node->for_statement.end, indent+1);
|
print_ast(node->for_stmt.end, indent+1);
|
||||||
print_ast(node->for_statement.body, indent+1);
|
print_ast(node->for_stmt.body, indent+1);
|
||||||
break;
|
break;
|
||||||
case AstNode_DeferStatement:
|
case AstNode_DeferStmt:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
gb_printf("(defer)\n");
|
gb_printf("(defer)\n");
|
||||||
print_ast(node->defer_statement.statement, indent+1);
|
print_ast(node->defer_stmt.stmt, indent+1);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
||||||
case AstNode_VariableDeclaration:
|
case AstNode_VarDecl:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
if (node->variable_declaration.kind == Declaration_Mutable)
|
if (node->var_decl.kind == Declaration_Mutable)
|
||||||
gb_printf("(decl:var,mutable)\n");
|
gb_printf("(decl:var,mutable)\n");
|
||||||
else if (node->variable_declaration.kind == Declaration_Immutable)
|
else if (node->var_decl.kind == Declaration_Immutable)
|
||||||
gb_printf("(decl:var,immutable)\n");
|
gb_printf("(decl:var,immutable)\n");
|
||||||
print_ast(node->variable_declaration.name_list, indent+1);
|
print_ast(node->var_decl.name_list, indent+1);
|
||||||
print_ast(node->variable_declaration.type_expression, indent+1);
|
print_ast(node->var_decl.type, indent+1);
|
||||||
print_ast(node->variable_declaration.value_list, indent+1);
|
print_ast(node->var_decl.value_list, indent+1);
|
||||||
break;
|
break;
|
||||||
case AstNode_ProcedureDeclaration:
|
case AstNode_ProcDecl:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
if (node->procedure_declaration.kind == Declaration_Mutable)
|
if (node->proc_decl.kind == Declaration_Mutable)
|
||||||
gb_printf("(decl:proc,mutable)\n");
|
gb_printf("(decl:proc,mutable)\n");
|
||||||
else if (node->procedure_declaration.kind == Declaration_Immutable)
|
else if (node->proc_decl.kind == Declaration_Immutable)
|
||||||
gb_printf("(decl:proc,immutable)\n");
|
gb_printf("(decl:proc,immutable)\n");
|
||||||
print_ast(node->procedure_declaration.type, indent+1);
|
print_ast(node->proc_decl.type, indent+1);
|
||||||
print_ast(node->procedure_declaration.body, indent+1);
|
print_ast(node->proc_decl.body, indent+1);
|
||||||
print_ast(node->procedure_declaration.tag_list, indent+1);
|
print_ast(node->proc_decl.tag_list, indent+1);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case AstNode_TypeDeclaration:
|
case AstNode_TypeDecl:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
gb_printf("(type)\n");
|
gb_printf("(type)\n");
|
||||||
print_ast(node->type_declaration.name, indent+1);
|
print_ast(node->type_decl.name, indent+1);
|
||||||
print_ast(node->type_declaration.type_expression, indent+1);
|
print_ast(node->type_decl.type, indent+1);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case AstNode_AliasDeclaration:
|
case AstNode_AliasDecl:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
gb_printf("(alias)\n");
|
gb_printf("(alias)\n");
|
||||||
print_ast(node->alias_declaration.name, indent+1);
|
print_ast(node->alias_decl.name, indent+1);
|
||||||
print_ast(node->alias_declaration.type_expression, indent+1);
|
print_ast(node->alias_decl.type, indent+1);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
||||||
case AstNode_ProcedureType:
|
case AstNode_ProcType:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
gb_printf("(type:proc)(%td -> %td)\n", node->procedure_type.param_count, node->procedure_type.result_count);
|
gb_printf("(type:proc)(%td -> %td)\n", node->proc_type.param_count, node->proc_type.result_count);
|
||||||
print_ast(node->procedure_type.param_list, indent+1);
|
print_ast(node->proc_type.param_list, indent+1);
|
||||||
if (node->procedure_type.result_list) {
|
if (node->proc_type.result_list) {
|
||||||
print_indent(indent+1);
|
print_indent(indent+1);
|
||||||
gb_printf("->\n");
|
gb_printf("->\n");
|
||||||
print_ast(node->procedure_type.result_list, indent+1);
|
print_ast(node->proc_type.result_list, indent+1);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case AstNode_Field:
|
case AstNode_Field:
|
||||||
print_ast(node->field.name_list, indent);
|
print_ast(node->field.name_list, indent);
|
||||||
print_ast(node->field.type_expression, indent);
|
print_ast(node->field.type, indent);
|
||||||
break;
|
break;
|
||||||
case AstNode_PointerType:
|
case AstNode_PointerType:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
print_token(node->pointer_type.token);
|
print_token(node->pointer_type.token);
|
||||||
print_ast(node->pointer_type.type_expression, indent+1);
|
print_ast(node->pointer_type.type, indent+1);
|
||||||
break;
|
break;
|
||||||
case AstNode_ArrayType:
|
case AstNode_ArrayType:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
gb_printf("[]\n");
|
gb_printf("[]\n");
|
||||||
print_ast(node->array_type.count, indent+1);
|
print_ast(node->array_type.count, indent+1);
|
||||||
print_ast(node->array_type.element, indent+1);
|
print_ast(node->array_type.elem, indent+1);
|
||||||
break;
|
break;
|
||||||
case AstNode_StructType:
|
case AstNode_StructType:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
|
|||||||
Reference in New Issue
Block a user