mirror of
https://github.com/odin-lang/Odin.git
synced 2026-06-10 04:18:11 +00:00
Restart LLVM IR SSA generation
This is the third go and I'm going for it!
This commit is contained in:
4
examples/test.c
Normal file
4
examples/test.c
Normal file
@@ -0,0 +1,4 @@
|
||||
int main() {
|
||||
float a = 0.5;
|
||||
return 0;
|
||||
}
|
||||
16
examples/test.ll
Normal file
16
examples/test.ll
Normal file
@@ -0,0 +1,16 @@
|
||||
define void @main() {
|
||||
entry:
|
||||
%0 = alloca i64, align 8
|
||||
store i64 zeroinitializer, i64* %0
|
||||
store i64 137, i64* %0
|
||||
%1 = load i64, i64* %0
|
||||
add i64 1, %1
|
||||
store i64 %2, i64* %0
|
||||
%3 = alloca float, align 4
|
||||
store float zeroinitializer, float* %3
|
||||
store float 0x3f8147ae00000000, float* %3
|
||||
%4 = load float, float* %3
|
||||
fadd float %4, 0x3f7d70a400000000
|
||||
store float %5, float* %3
|
||||
ret void
|
||||
}
|
||||
@@ -1,6 +1,11 @@
|
||||
type Vec2: struct { x, y: f32 }
|
||||
|
||||
|
||||
main :: proc() {
|
||||
// x : string;
|
||||
// x = "Hello";
|
||||
x : int;
|
||||
x = 137;
|
||||
x = 1 + x;
|
||||
|
||||
y : f32;
|
||||
y = 1.01;
|
||||
y = y + 0.99;
|
||||
}
|
||||
|
||||
5
run.bat
5
run.bat
@@ -3,6 +3,5 @@
|
||||
|
||||
rem del "..\examples\test.bc"
|
||||
call ..\bin\odin.exe ..\examples/test.odin
|
||||
rem call lli ..\examples/test.ll
|
||||
|
||||
|
||||
call lli ..\examples/test.ll
|
||||
rem call clang ..\examples/test.c -S -emit-llvm -o -
|
||||
|
||||
@@ -122,7 +122,6 @@ enum BuiltinProcedureId {
|
||||
BuiltinProcedure_len,
|
||||
BuiltinProcedure_cap,
|
||||
BuiltinProcedure_copy,
|
||||
BuiltinProcedure_copy_bytes,
|
||||
BuiltinProcedure_print,
|
||||
BuiltinProcedure_println,
|
||||
|
||||
@@ -146,7 +145,6 @@ gb_global BuiltinProcedure builtin_procedures[BuiltinProcedure_Count] = {
|
||||
{STR_LIT("len"), 1, false, Expression_Expression},
|
||||
{STR_LIT("cap"), 1, false, Expression_Expression},
|
||||
{STR_LIT("copy"), 2, false, Expression_Expression},
|
||||
{STR_LIT("copy_bytes"), 3, false, Expression_Statement},
|
||||
{STR_LIT("print"), 1, true, Expression_Statement},
|
||||
{STR_LIT("println"), 1, true, Expression_Statement},
|
||||
};
|
||||
@@ -156,14 +154,18 @@ struct CheckerContext {
|
||||
DeclarationInfo *decl;
|
||||
};
|
||||
|
||||
struct Checker {
|
||||
Parser * parser;
|
||||
struct CheckerInfo {
|
||||
Map<TypeAndValue> types; // Key: AstNode * | Expression -> Type (and value)
|
||||
Map<Entity *> definitions; // Key: AstNode * | Identifier -> Entity
|
||||
Map<Entity *> uses; // Key: AstNode * | Identifier -> Entity
|
||||
Map<Scope *> scopes; // Key: AstNode * | Node -> Scope
|
||||
Map<ExpressionInfo> untyped; // Key: AstNode * | Expression -> ExpressionInfo
|
||||
Map<DeclarationInfo *> entities; // Key: Entity *
|
||||
};
|
||||
|
||||
struct Checker {
|
||||
Parser * parser;
|
||||
CheckerInfo info;
|
||||
|
||||
AstFile * curr_ast_file;
|
||||
BaseTypeSizes sizes;
|
||||
@@ -195,12 +197,13 @@ Scope *make_scope(Scope *parent, gbAllocator allocator) {
|
||||
}
|
||||
|
||||
void destroy_scope(Scope *scope) {
|
||||
isize element_count = gb_array_count(scope->elements.entries);
|
||||
for (isize i = 0; i < element_count; i++) {
|
||||
gb_for_array(i, scope->elements.entries) {
|
||||
Entity *e =scope->elements.entries[i].value;
|
||||
if (e->kind == Entity_Variable) {
|
||||
if (!e->variable.used) {
|
||||
#if 0
|
||||
warning(e->token, "Unused variable `%.*s`", LIT(e->token.string));
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -262,7 +265,7 @@ void add_dependency(DeclarationInfo *d, Entity *e) {
|
||||
|
||||
void add_declaration_dependency(Checker *c, Entity *e) {
|
||||
if (c->context.decl) {
|
||||
auto found = map_get(&c->entities, hash_pointer(e));
|
||||
auto found = map_get(&c->info.entities, hash_pointer(e));
|
||||
if (found) {
|
||||
add_dependency(c->context.decl, e);
|
||||
}
|
||||
@@ -324,22 +327,35 @@ void init_universal_scope(void) {
|
||||
|
||||
|
||||
|
||||
void init_checker_info(CheckerInfo *i) {
|
||||
gbAllocator a = gb_heap_allocator();
|
||||
map_init(&i->types, a);
|
||||
map_init(&i->definitions, a);
|
||||
map_init(&i->uses, a);
|
||||
map_init(&i->scopes, a);
|
||||
map_init(&i->entities, a);
|
||||
map_init(&i->untyped, a);
|
||||
|
||||
}
|
||||
|
||||
void destroy_checker_info(CheckerInfo *i) {
|
||||
map_destroy(&i->types);
|
||||
map_destroy(&i->definitions);
|
||||
map_destroy(&i->uses);
|
||||
map_destroy(&i->scopes);
|
||||
map_destroy(&i->entities);
|
||||
map_destroy(&i->untyped);
|
||||
}
|
||||
|
||||
|
||||
void init_checker(Checker *c, Parser *parser) {
|
||||
gbAllocator a = gb_heap_allocator();
|
||||
|
||||
c->parser = parser;
|
||||
map_init(&c->types, gb_heap_allocator());
|
||||
map_init(&c->definitions, gb_heap_allocator());
|
||||
map_init(&c->uses, gb_heap_allocator());
|
||||
map_init(&c->scopes, gb_heap_allocator());
|
||||
map_init(&c->entities, gb_heap_allocator());
|
||||
init_checker_info(&c->info);
|
||||
c->sizes.word_size = 8;
|
||||
c->sizes.max_align = 8;
|
||||
|
||||
map_init(&c->untyped, a);
|
||||
|
||||
gb_array_init(c->procedure_stack, a);
|
||||
gb_array_init(c->procedures, a);
|
||||
@@ -347,7 +363,7 @@ void init_checker(Checker *c, Parser *parser) {
|
||||
// NOTE(bill): Is this big enough or too small?
|
||||
isize item_size = gb_max(gb_max(gb_size_of(Entity), gb_size_of(Type)), gb_size_of(Scope));
|
||||
isize total_token_count = 0;
|
||||
for (isize i = 0; i < gb_array_count(c->parser->files); i++) {
|
||||
gb_for_array(i, c->parser->files) {
|
||||
AstFile *f = &c->parser->files[i];
|
||||
total_token_count += gb_array_count(f->tokens);
|
||||
}
|
||||
@@ -360,12 +376,7 @@ void init_checker(Checker *c, Parser *parser) {
|
||||
}
|
||||
|
||||
void destroy_checker(Checker *c) {
|
||||
map_destroy(&c->types);
|
||||
map_destroy(&c->definitions);
|
||||
map_destroy(&c->uses);
|
||||
map_destroy(&c->scopes);
|
||||
map_destroy(&c->untyped);
|
||||
map_destroy(&c->entities);
|
||||
destroy_checker_info(&c->info);
|
||||
destroy_scope(c->global_scope);
|
||||
gb_array_free(c->procedure_stack);
|
||||
gb_array_free(c->procedures);
|
||||
@@ -374,30 +385,30 @@ void destroy_checker(Checker *c) {
|
||||
}
|
||||
|
||||
|
||||
TypeAndValue *type_and_value_of_expression(Checker *c, AstNode *expression) {
|
||||
TypeAndValue *found = map_get(&c->types, hash_pointer(expression));
|
||||
TypeAndValue *type_and_value_of_expression(CheckerInfo *i, AstNode *expression) {
|
||||
TypeAndValue *found = map_get(&i->types, hash_pointer(expression));
|
||||
return found;
|
||||
}
|
||||
|
||||
|
||||
Entity *entity_of_identifier(Checker *c, AstNode *identifier) {
|
||||
Entity *entity_of_identifier(CheckerInfo *i, AstNode *identifier) {
|
||||
GB_ASSERT(identifier->kind == AstNode_Identifier);
|
||||
Entity **found = map_get(&c->definitions, hash_pointer(identifier));
|
||||
Entity **found = map_get(&i->definitions, hash_pointer(identifier));
|
||||
if (found)
|
||||
return *found;
|
||||
|
||||
found = map_get(&c->uses, hash_pointer(identifier));
|
||||
found = map_get(&i->uses, hash_pointer(identifier));
|
||||
if (found)
|
||||
return *found;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Type *type_of_expression(Checker *c, AstNode *expression) {
|
||||
TypeAndValue *found = type_and_value_of_expression(c, expression);
|
||||
Type *type_of_expression(CheckerInfo *i, AstNode *expression) {
|
||||
TypeAndValue *found = type_and_value_of_expression(i, expression);
|
||||
if (found)
|
||||
return found->type;
|
||||
if (expression->kind == AstNode_Identifier) {
|
||||
Entity *entity = entity_of_identifier(c, expression);
|
||||
Entity *entity = entity_of_identifier(i, expression);
|
||||
if (entity)
|
||||
return entity->type;
|
||||
}
|
||||
@@ -406,12 +417,12 @@ Type *type_of_expression(Checker *c, AstNode *expression) {
|
||||
}
|
||||
|
||||
|
||||
void add_untyped(Checker *c, AstNode *expression, b32 lhs, AddressingMode mode, Type *basic_type, ExactValue value) {
|
||||
map_set(&c->untyped, hash_pointer(expression), make_expression_info(lhs, mode, basic_type, value));
|
||||
void add_untyped(CheckerInfo *i, AstNode *expression, b32 lhs, AddressingMode mode, Type *basic_type, ExactValue value) {
|
||||
map_set(&i->untyped, hash_pointer(expression), make_expression_info(lhs, mode, basic_type, value));
|
||||
}
|
||||
|
||||
|
||||
void add_type_and_value(Checker *c, AstNode *expression, AddressingMode mode, Type *type, ExactValue value) {
|
||||
void add_type_and_value(CheckerInfo *i, AstNode *expression, AddressingMode mode, Type *type, ExactValue value) {
|
||||
GB_ASSERT(expression != NULL);
|
||||
GB_ASSERT(type != NULL);
|
||||
if (mode == Addressing_Invalid)
|
||||
@@ -425,14 +436,14 @@ void add_type_and_value(Checker *c, AstNode *expression, AddressingMode mode, Ty
|
||||
TypeAndValue tv = {};
|
||||
tv.type = type;
|
||||
tv.value = value;
|
||||
map_set(&c->types, hash_pointer(expression), tv);
|
||||
map_set(&i->types, hash_pointer(expression), tv);
|
||||
}
|
||||
|
||||
void add_entity_definition(Checker *c, AstNode *identifier, Entity *entity) {
|
||||
void add_entity_definition(CheckerInfo *i, AstNode *identifier, Entity *entity) {
|
||||
GB_ASSERT(identifier != NULL);
|
||||
GB_ASSERT(identifier->kind == AstNode_Identifier);
|
||||
u64 key = hash_pointer(identifier);
|
||||
map_set(&c->definitions, key, entity);
|
||||
map_set(&i->definitions, key, entity);
|
||||
}
|
||||
|
||||
void add_entity(Checker *c, Scope *scope, AstNode *identifier, Entity *entity) {
|
||||
@@ -444,24 +455,23 @@ void add_entity(Checker *c, Scope *scope, AstNode *identifier, Entity *entity) {
|
||||
}
|
||||
}
|
||||
if (identifier != NULL)
|
||||
add_entity_definition(c, identifier, entity);
|
||||
add_entity_definition(&c->info, identifier, entity);
|
||||
}
|
||||
|
||||
void add_entity_use(Checker *c, AstNode *identifier, Entity *entity) {
|
||||
void add_entity_use(CheckerInfo *i, AstNode *identifier, Entity *entity) {
|
||||
GB_ASSERT(identifier != NULL);
|
||||
GB_ASSERT(identifier->kind == AstNode_Identifier);
|
||||
u64 key = hash_pointer(identifier);
|
||||
map_set(&c->uses, key, entity);
|
||||
map_set(&i->uses, key, entity);
|
||||
}
|
||||
|
||||
|
||||
void add_file_entity(Checker *c, AstNode *identifier, Entity *e, DeclarationInfo *d) {
|
||||
GB_ASSERT(are_strings_equal(identifier->identifier.token.string, e->token.string));
|
||||
|
||||
|
||||
add_entity(c, c->global_scope, identifier, e);
|
||||
map_set(&c->entities, hash_pointer(e), d);
|
||||
e->order = gb_array_count(c->entities.entries);
|
||||
map_set(&c->info.entities, hash_pointer(e), d);
|
||||
e->order = gb_array_count(c->info.entities.entries);
|
||||
}
|
||||
|
||||
|
||||
@@ -480,7 +490,7 @@ void check_procedure_later(Checker *c, AstFile *file, Token token, DeclarationIn
|
||||
void add_scope(Checker *c, AstNode *node, Scope *scope) {
|
||||
GB_ASSERT(node != NULL);
|
||||
GB_ASSERT(scope != NULL);
|
||||
map_set(&c->scopes, hash_pointer(node), scope);
|
||||
map_set(&c->info.scopes, hash_pointer(node), scope);
|
||||
}
|
||||
|
||||
|
||||
@@ -518,16 +528,9 @@ void add_curr_ast_file(Checker *c, AstFile *file) {
|
||||
|
||||
|
||||
|
||||
|
||||
GB_COMPARE_PROC(entity_order_cmp) {
|
||||
Entity const *p = cast(Entity const *)a;
|
||||
Entity const *q = cast(Entity const *)b;
|
||||
return p->order < q->order ? -1 : p->order > q->order;
|
||||
}
|
||||
|
||||
void check_parsed_files(Checker *c) {
|
||||
// Collect Entities
|
||||
for (isize i = 0; i < gb_array_count(c->parser->files); i++) {
|
||||
gb_for_array(i, c->parser->files) {
|
||||
AstFile *f = &c->parser->files[i];
|
||||
add_curr_ast_file(c, f);
|
||||
for (AstNode *decl = f->declarations; decl != NULL; decl = decl->next) {
|
||||
@@ -625,8 +628,8 @@ void check_parsed_files(Checker *c) {
|
||||
add_entity(c, c->global_scope, identifier, e);
|
||||
DeclarationInfo *d = make_declaration_info(c->allocator, e->parent);
|
||||
d->proc_decl = decl;
|
||||
map_set(&c->entities, hash_pointer(e), d);
|
||||
e->order = gb_array_count(c->entities.entries);
|
||||
map_set(&c->info.entities, hash_pointer(e), d);
|
||||
e->order = gb_array_count(c->info.entities.entries);
|
||||
|
||||
} break;
|
||||
|
||||
@@ -641,46 +644,33 @@ void check_parsed_files(Checker *c) {
|
||||
}
|
||||
}
|
||||
|
||||
{ // Order entities
|
||||
gbArray(Entity *) entities;
|
||||
isize count = gb_array_count(c->entities.entries);
|
||||
gb_array_init_reserve(entities, gb_heap_allocator(), count);
|
||||
defer (gb_array_free(entities));
|
||||
|
||||
for (isize i = 0; i < count; i++) {
|
||||
u64 key = c->entities.entries[i].key;
|
||||
Entity *e = cast(Entity *)cast(uintptr)key;
|
||||
gb_array_append(entities, e);
|
||||
}
|
||||
|
||||
gb_sort_array(entities, count, entity_order_cmp);
|
||||
|
||||
for (isize i = 0; i < count; i++) {
|
||||
check_entity_declaration(c, entities[i], NULL);
|
||||
}
|
||||
gb_for_array(i, c->info.entities.entries) {
|
||||
auto *entry = &c->info.entities.entries[i];
|
||||
Entity *e = cast(Entity *)cast(uintptr)entry->key;
|
||||
DeclarationInfo *d = entry->value;
|
||||
check_entity_declaration(c, e, d, NULL);
|
||||
}
|
||||
|
||||
|
||||
// Check procedure bodies
|
||||
for (isize i = 0; i < gb_array_count(c->procedures); i++) {
|
||||
gb_for_array(i, c->procedures) {
|
||||
ProcedureInfo *pi = &c->procedures[i];
|
||||
add_curr_ast_file(c, pi->file);
|
||||
check_procedure_body(c, pi->token, pi->decl, pi->type, pi->body);
|
||||
}
|
||||
|
||||
|
||||
{ // Add untyped expression values
|
||||
isize count = gb_array_count(c->untyped.entries);
|
||||
for (isize i = 0; i < count; i++) {
|
||||
auto *entry = c->untyped.entries + i;
|
||||
u64 key = entry->key;
|
||||
AstNode *expr = cast(AstNode *)cast(uintptr)key;
|
||||
ExpressionInfo *info = &entry->value;
|
||||
if (is_type_typed(info->type)) {
|
||||
GB_PANIC("%s (type %s) is typed!", expression_to_string(expr), info->type);
|
||||
}
|
||||
add_type_and_value(c, expr, info->mode, info->type, info->value);
|
||||
// Add untyped expression values
|
||||
gb_for_array(i, c->info.untyped.entries) {
|
||||
auto *entry = c->info.untyped.entries + i;
|
||||
u64 key = entry->key;
|
||||
AstNode *expr = cast(AstNode *)cast(uintptr)key;
|
||||
ExpressionInfo *info = &entry->value;
|
||||
if (is_type_typed(info->type)) {
|
||||
GB_PANIC("%s (type %s) is typed!", expression_to_string(expr), info->type);
|
||||
}
|
||||
add_type_and_value(&c->info, expr, info->mode, info->type, info->value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,19 +2,30 @@ struct Scope;
|
||||
struct Checker;
|
||||
enum BuiltinProcedureId;
|
||||
|
||||
#define ENTITY_KINDS \
|
||||
ENTITY_KIND(Invalid), \
|
||||
ENTITY_KIND(Constant), \
|
||||
ENTITY_KIND(Variable), \
|
||||
ENTITY_KIND(TypeName), \
|
||||
ENTITY_KIND(AliasName), \
|
||||
ENTITY_KIND(Procedure), \
|
||||
ENTITY_KIND(Builtin), \
|
||||
ENTITY_KIND(Count),
|
||||
|
||||
|
||||
enum EntityKind {
|
||||
Entity_Invalid,
|
||||
|
||||
Entity_Constant,
|
||||
Entity_Variable,
|
||||
Entity_TypeName,
|
||||
Entity_AliasName,
|
||||
Entity_Procedure,
|
||||
Entity_Builtin,
|
||||
|
||||
Entity_Count,
|
||||
#define ENTITY_KIND(k) GB_JOIN2(Entity_, k)
|
||||
ENTITY_KINDS
|
||||
#undef ENTITY_KIND
|
||||
};
|
||||
|
||||
String const entity_strings[] = {
|
||||
#define ENTITY_KIND(k) {cast(u8 *)#k, gb_size_of(#k)-1}
|
||||
ENTITY_KINDS
|
||||
#undef ENTITY_KIND
|
||||
};
|
||||
|
||||
|
||||
typedef i64 EntityGuid;
|
||||
|
||||
struct Entity {
|
||||
|
||||
@@ -9,8 +9,8 @@ void check_selector (Checker *c, Operand *operand, AstNode *n
|
||||
void check_not_tuple (Checker *c, Operand *operand);
|
||||
void convert_to_typed (Checker *c, Operand *operand, Type *target_type);
|
||||
gbString expression_to_string (AstNode *expression);
|
||||
void check_entity_declaration(Checker *c, Entity *e, Type *named_type);
|
||||
void check_procedure_body(Checker *c, Token token, DeclarationInfo *decl, Type *type, AstNode *body);
|
||||
void check_entity_declaration(Checker *c, Entity *e, DeclarationInfo *decl, Type *named_type);
|
||||
void check_procedure_body (Checker *c, Token token, DeclarationInfo *decl, Type *type, AstNode *body);
|
||||
|
||||
|
||||
void check_struct_type(Checker *c, Type *struct_type, AstNode *node) {
|
||||
@@ -51,7 +51,7 @@ void check_struct_type(Checker *c, Type *struct_type, AstNode *node) {
|
||||
map_set(&entity_map, key, e);
|
||||
fields[field_index++] = e;
|
||||
}
|
||||
add_entity_use(c, name, e);
|
||||
add_entity_use(&c->info, name, e);
|
||||
}
|
||||
}
|
||||
struct_type->structure.fields = fields;
|
||||
@@ -143,9 +143,16 @@ void check_identifier(Checker *c, Operand *o, AstNode *n, Type *named_type) {
|
||||
"Undeclared type or identifier `%.*s`", LIT(n->identifier.token.string));
|
||||
return;
|
||||
}
|
||||
add_entity_use(c, n, e);
|
||||
add_entity_use(&c->info, n, e);
|
||||
|
||||
check_entity_declaration(c, e, named_type);
|
||||
if (e->type == NULL) {
|
||||
auto *found = map_get(&c->info.entities, hash_pointer(e));
|
||||
if (found != NULL) {
|
||||
check_entity_declaration(c, e, *found, named_type);
|
||||
} else {
|
||||
GB_PANIC("Internal Compiler Error: DeclarationInfo not found!");
|
||||
}
|
||||
}
|
||||
|
||||
if (e->type == NULL) {
|
||||
GB_PANIC("Compiler error: How did this happen? type: %s; identifier: %.*s\n", type_to_string(e->type), LIT(n->identifier.token.string));
|
||||
@@ -386,7 +393,7 @@ Type *check_type(Checker *c, AstNode *e, Type *named_type) {
|
||||
|
||||
end:
|
||||
GB_ASSERT(is_type_typed(type));
|
||||
add_type_and_value(c, e, Addressing_Type, type, null_value);
|
||||
add_type_and_value(&c->info, e, Addressing_Type, type, null_value);
|
||||
return type;
|
||||
}
|
||||
|
||||
@@ -758,7 +765,7 @@ void check_binary_expression(Checker *c, Operand *x, AstNode *node) {
|
||||
|
||||
|
||||
void update_expression_type(Checker *c, AstNode *e, Type *type) {
|
||||
ExpressionInfo *found = map_get(&c->untyped, hash_pointer(e));
|
||||
ExpressionInfo *found = map_get(&c->info.untyped, hash_pointer(e));
|
||||
if (!found)
|
||||
return;
|
||||
|
||||
@@ -786,7 +793,7 @@ void update_expression_type(Checker *c, AstNode *e, Type *type) {
|
||||
}
|
||||
|
||||
void update_expression_value(Checker *c, AstNode *e, ExactValue value) {
|
||||
ExpressionInfo *found = map_get(&c->untyped, hash_pointer(e));
|
||||
ExpressionInfo *found = map_get(&c->info.untyped, hash_pointer(e));
|
||||
if (found)
|
||||
found->value = value;
|
||||
}
|
||||
@@ -977,7 +984,7 @@ void check_selector(Checker *c, Operand *operand, AstNode *node) {
|
||||
operand->expression = node;
|
||||
return;
|
||||
}
|
||||
add_entity_use(c, selector, entity);
|
||||
add_entity_use(&c->info, selector, entity);
|
||||
|
||||
operand->type = entity->type;
|
||||
operand->expression = node;
|
||||
@@ -1241,53 +1248,6 @@ b32 check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id)
|
||||
operand->mode = Addressing_Value;
|
||||
} break;
|
||||
|
||||
case BuiltinProcedure_copy_bytes: {
|
||||
// copy_bytes :: proc(dest, source: rawptr, byte_count: int)
|
||||
Type *dest_type = NULL, *src_type = NULL;
|
||||
|
||||
Type *d = get_base_type(operand->type);
|
||||
if (is_type_pointer(d))
|
||||
dest_type = d;
|
||||
|
||||
Operand op = {};
|
||||
check_expression(c, &op, ce->arg_list->next);
|
||||
if (op.mode == Addressing_Invalid)
|
||||
return false;
|
||||
Type *s = get_base_type(op.type);
|
||||
if (is_type_pointer(s))
|
||||
src_type = s;
|
||||
|
||||
if (dest_type == NULL || src_type == NULL) {
|
||||
error(&c->error_collector, ast_node_token(call), "`copy_bytes` only expects pointers for the destintation and source");
|
||||
return false;
|
||||
}
|
||||
|
||||
check_expression(c, &op, ce->arg_list->next->next);
|
||||
if (op.mode == Addressing_Invalid)
|
||||
return false;
|
||||
|
||||
convert_to_typed(c, &op, &basic_types[Basic_int]);
|
||||
if (op.mode == Addressing_Invalid ||
|
||||
op.type->kind != Type_Basic ||
|
||||
op.type->basic.kind != Basic_int) {
|
||||
gbString str = type_to_string(op.type);
|
||||
defer (gb_string_free(str));
|
||||
error(&c->error_collector, ast_node_token(call), "`copy_bytes` 3rd argument must be of type `int`, a `%s` was given", str);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (op.mode == Addressing_Constant) {
|
||||
if (exact_value_to_integer(op.value).value_integer <= 0) {
|
||||
error(&c->error_collector, ast_node_token(call), "You cannot copy a zero or negative amount of bytes with `copy_bytes`");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
operand->type = NULL;
|
||||
operand->mode = Addressing_NoValue;
|
||||
} break;
|
||||
|
||||
|
||||
case BuiltinProcedure_print:
|
||||
case BuiltinProcedure_println: {
|
||||
for (AstNode *arg = ce->arg_list; arg != NULL; arg = arg->next) {
|
||||
@@ -1896,9 +1856,9 @@ ExpressionKind check_expression_base(Checker *c, Operand *o, AstNode *node, Type
|
||||
|
||||
if (type != NULL) {
|
||||
if (is_type_untyped(type)) {
|
||||
add_untyped(c, node, false, o->mode, type, value);
|
||||
add_untyped(&c->info, node, false, o->mode, type, value);
|
||||
} else {
|
||||
add_type_and_value(c, node, o->mode, type, value);
|
||||
add_type_and_value(&c->info, node, o->mode, type, value);
|
||||
}
|
||||
}
|
||||
return kind;
|
||||
|
||||
@@ -172,7 +172,7 @@ Type *check_assignment_variable(Checker *c, Operand *op_a, AstNode *lhs) {
|
||||
// NOTE(bill): Ignore assignments to `_`
|
||||
if (node->kind == AstNode_Identifier &&
|
||||
are_strings_equal(node->identifier.token.string, make_string("_"))) {
|
||||
add_entity_definition(c, node, NULL);
|
||||
add_entity_definition(&c->info, node, NULL);
|
||||
check_assignment(c, op_a, NULL, make_string("assignment to `_` identifier"));
|
||||
if (op_a->mode == Addressing_Invalid)
|
||||
return NULL;
|
||||
@@ -279,9 +279,8 @@ void check_init_variables(Checker *c, Entity **lhs, isize lhs_count, AstNode *in
|
||||
}
|
||||
}
|
||||
|
||||
if (i < lhs_count && i < init_count) {
|
||||
if (lhs[i]->type == NULL)
|
||||
error(&c->error_collector, lhs[i]->token, "Too few values on the right hand side of the declaration");
|
||||
if (i < lhs_count && lhs[i]->type == NULL) {
|
||||
error(&c->error_collector, lhs[i]->token, "Too few values on the right hand side of the declaration");
|
||||
} else if (rhs != NULL) {
|
||||
error(&c->error_collector, ast_node_token(rhs), "Too many values on the right hand side of the declaration");
|
||||
}
|
||||
@@ -401,9 +400,9 @@ void check_procedure_declaration(Checker *c, Entity *e, DeclarationInfo *d, b32
|
||||
#if 1
|
||||
Scope *original_curr_scope = c->context.scope;
|
||||
c->context.scope = c->global_scope;
|
||||
check_open_scope(c, pd->procedure_type);
|
||||
check_open_scope(c, pd->type);
|
||||
#endif
|
||||
check_procedure_type(c, proc_type, pd->procedure_type);
|
||||
check_procedure_type(c, proc_type, pd->type);
|
||||
b32 is_foreign = false;
|
||||
b32 is_inline = false;
|
||||
b32 is_no_inline = false;
|
||||
@@ -487,16 +486,9 @@ void check_variable_declaration(Checker *c, Entity *e, Entity **entities, isize
|
||||
|
||||
|
||||
|
||||
void check_entity_declaration(Checker *c, Entity *e, Type *named_type) {
|
||||
void check_entity_declaration(Checker *c, Entity *e, DeclarationInfo *d, Type *named_type) {
|
||||
if (e->type != NULL)
|
||||
return;
|
||||
|
||||
DeclarationInfo **found = map_get(&c->entities, hash_pointer(e));
|
||||
if (found == NULL) {
|
||||
GB_PANIC("Compiler error: This entity should be declared!");
|
||||
}
|
||||
DeclarationInfo *d = *found;
|
||||
|
||||
switch (e->kind) {
|
||||
case Entity_Constant:
|
||||
c->context.decl = d;
|
||||
@@ -580,6 +572,7 @@ void check_statement(Checker *c, AstNode *node, u32 flags) {
|
||||
basic_lit.basic_literal = s->op;
|
||||
basic_lit.basic_literal.kind = Token_Integer;
|
||||
basic_lit.basic_literal.string = make_string("1");
|
||||
|
||||
AstNode be = {AstNode_BinaryExpression};
|
||||
be.binary_expression.op = op;
|
||||
be.binary_expression.left = s->expression;;
|
||||
@@ -801,7 +794,7 @@ void check_statement(Checker *c, AstNode *node, u32 flags) {
|
||||
if (!can_be_ignored) {
|
||||
new_entities[new_entity_count++] = entity;
|
||||
}
|
||||
add_entity_definition(c, name, entity);
|
||||
add_entity_definition(&c->info, name, entity);
|
||||
} else {
|
||||
entity = found;
|
||||
}
|
||||
@@ -833,7 +826,6 @@ void check_statement(Checker *c, AstNode *node, u32 flags) {
|
||||
e->type = init_type;
|
||||
}
|
||||
|
||||
|
||||
check_init_variables(c, entities, entity_count, vd->value_list, vd->value_count, make_string("variable declaration"));
|
||||
|
||||
AstNode *name = vd->name_list;
|
||||
|
||||
@@ -51,21 +51,32 @@ struct BasicType {
|
||||
};
|
||||
|
||||
|
||||
|
||||
#define TYPE_KINDS \
|
||||
TYPE_KIND(Invalid), \
|
||||
TYPE_KIND(Basic), \
|
||||
TYPE_KIND(Array), \
|
||||
TYPE_KIND(Slice), \
|
||||
TYPE_KIND(Structure), \
|
||||
TYPE_KIND(Pointer), \
|
||||
TYPE_KIND(Named), \
|
||||
TYPE_KIND(Alias), \
|
||||
TYPE_KIND(Tuple), \
|
||||
TYPE_KIND(Procedure), \
|
||||
TYPE_KIND(Count),
|
||||
|
||||
enum TypeKind {
|
||||
Type_Invalid,
|
||||
|
||||
Type_Basic,
|
||||
Type_Array,
|
||||
Type_Slice,
|
||||
Type_Structure,
|
||||
Type_Pointer,
|
||||
Type_Named,
|
||||
Type_Alias,
|
||||
Type_Tuple,
|
||||
Type_Procedure,
|
||||
|
||||
Type_Count,
|
||||
#define TYPE_KIND(k) GB_JOIN2(Type_, k)
|
||||
TYPE_KINDS
|
||||
#undef TYPE_KIND
|
||||
};
|
||||
|
||||
String const type_strings[] = {
|
||||
#define TYPE_KIND(k) {cast(u8 *)#k, gb_size_of(#k)-1}
|
||||
TYPE_KINDS
|
||||
#undef TYPE_KIND
|
||||
};
|
||||
|
||||
struct Type {
|
||||
TypeKind kind;
|
||||
union {
|
||||
|
||||
318
src/codegen/codegen.cpp
Normal file
318
src/codegen/codegen.cpp
Normal file
@@ -0,0 +1,318 @@
|
||||
#include "ssa.cpp"
|
||||
#include "print.cpp"
|
||||
|
||||
struct ssaGen {
|
||||
ssaModule module;
|
||||
gbFile output_file;
|
||||
};
|
||||
|
||||
b32 ssa_gen_init(ssaGen *s, Checker *c) {
|
||||
if (c->error_collector.count != 0)
|
||||
return false;
|
||||
|
||||
gb_for_array(i, c->parser->files) {
|
||||
AstFile *f = &c->parser->files[i];
|
||||
if (f->error_collector.count != 0)
|
||||
return false;
|
||||
if (f->tokenizer.error_count != 0)
|
||||
return false;
|
||||
}
|
||||
|
||||
ssa_module_init(&s->module, c);
|
||||
|
||||
gbFileError err = gb_file_create(&s->output_file, "../examples/test.ll");
|
||||
if (err != gbFileError_None)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void ssa_gen_destroy(ssaGen *s) {
|
||||
ssa_module_destroy(&s->module);
|
||||
gb_file_close(&s->output_file);
|
||||
}
|
||||
|
||||
void ssa_gen_code(ssaGen *s) {
|
||||
ssaModule *m = &s->module;
|
||||
CheckerInfo *info = m->info;
|
||||
gbAllocator a = m->allocator;
|
||||
gb_for_array(i, info->entities.entries) {
|
||||
auto *entry = &info->entities.entries[i];
|
||||
Entity *e = cast(Entity *)cast(uintptr)entry->key;
|
||||
DeclarationInfo *decl = entry->value;
|
||||
|
||||
String name = e->token.string;
|
||||
|
||||
switch (e->kind) {
|
||||
case Entity_TypeName: {
|
||||
ssaValue *t = ssa_make_value_type_name(a, e);
|
||||
map_set(&m->members, hash_string(name), t);
|
||||
} break;
|
||||
|
||||
case Entity_Variable: {
|
||||
ssaValue *g = ssa_make_value_global(a, e, NULL);
|
||||
map_set(&m->values, hash_pointer(e), g);
|
||||
map_set(&m->members, hash_string(name), g);
|
||||
} break;
|
||||
|
||||
case Entity_Procedure: {
|
||||
ssaValue *p = ssa_make_value_procedure(a, e, decl, m);
|
||||
map_set(&m->values, hash_pointer(e), p);
|
||||
map_set(&m->members, hash_string(name), p);
|
||||
} break;
|
||||
}
|
||||
}
|
||||
|
||||
gb_for_array(i, m->members.entries) {
|
||||
auto *entry = &m->members.entries[i];
|
||||
ssaValue *v = entry->value;
|
||||
if (v->kind == ssaValue_Procedure)
|
||||
ssa_build_procedure(v);
|
||||
}
|
||||
|
||||
ssa_print_llvm_ir(&s->output_file, &s->module);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#if 0
|
||||
#include "type.cpp"
|
||||
#include "ir.cpp"
|
||||
|
||||
struct Codegen {
|
||||
Checker *checker;
|
||||
gbFile file;
|
||||
gbAllocator allocator;
|
||||
|
||||
irModule module;
|
||||
|
||||
ErrorCollector error_collector;
|
||||
};
|
||||
|
||||
b32 init_codegen(Codegen *c, Checker *checker) {
|
||||
c->checker = checker;
|
||||
|
||||
if (c->error_collector.count != 0)
|
||||
return false;
|
||||
for (isize i = 0; i < gb_array_count(checker->parser->files); i++) {
|
||||
AstFile *f = &checker->parser->files[i];
|
||||
if (f->error_collector.count != 0)
|
||||
return false;
|
||||
if (f->tokenizer.error_count != 0)
|
||||
return false;
|
||||
}
|
||||
|
||||
c->allocator = gb_heap_allocator();
|
||||
|
||||
ir_module_init(&c->module, c->checker);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void destroy_codegen(Codegen *c) {
|
||||
ir_module_destroy(&c->module);
|
||||
}
|
||||
|
||||
b32 is_blank_identifier(AstNode *identifier) {
|
||||
if (identifier->kind == AstNode_Identifier) {
|
||||
return are_strings_equal(identifier->identifier.token.string, make_string("_"));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
irValue *ir_add_basic_block(gbAllocator a, irValue *p, String label) {
|
||||
irValue *b = ir_make_value_basic_block(a, gb_array_count(p->procedure.blocks), label, p);
|
||||
gb_array_append(p->procedure.blocks, b);
|
||||
return b;
|
||||
}
|
||||
|
||||
irValue *ir_emit_from_block(irValue *b, irInstruction *i) {
|
||||
GB_ASSERT(b->kind == irValue_BasicBlock);
|
||||
i->block = b;
|
||||
gb_array_append(b->basic_block.instructions, i);
|
||||
return ir_make_value_instruction(gb_heap_allocator(), i);
|
||||
}
|
||||
|
||||
|
||||
irValue *ir_emit(irValue *p, irInstruction *i) {
|
||||
GB_ASSERT(p->kind == irValue_Procedure);
|
||||
return ir_emit_from_block(p->procedure.curr_block, i);
|
||||
}
|
||||
|
||||
|
||||
irInstruction *ir_add_local(irValue *p, Type *type, TokenPos pos) {
|
||||
irInstruction *i = ir_alloc_instruction(gb_heap_allocator(), irInstruction_Alloca);
|
||||
i->reg.type = type;
|
||||
i->reg.pos = pos;
|
||||
gb_array_append(p->procedure.locals, ir_emit(p, i));
|
||||
return i;
|
||||
}
|
||||
|
||||
irInstruction *ir_add_named_local(irValue *p, Entity *e) {
|
||||
irInstruction *i = ir_add_local(p, e->type, e->token.pos);
|
||||
i->alloca.label = e->token.string;
|
||||
// map_set(&p->procedure.variables, hash_pointer(e), );
|
||||
return i;
|
||||
}
|
||||
|
||||
irInstruction *ir_add_local_for_identifier(irValue *p, AstNode *i) {
|
||||
GB_ASSERT(p->kind == irValue_Procedure);
|
||||
GB_ASSERT(i->kind == AstNode_Identifier);
|
||||
auto *found = map_get(&p->procedure.module->checker->definitions, hash_pointer(i));
|
||||
return ir_add_named_local(p, *found);
|
||||
}
|
||||
|
||||
|
||||
void ir_build_variable_declaration(irValue *p, AstNode *d) {
|
||||
GB_ASSERT(p->kind == irValue_Procedure);
|
||||
auto *vd = &d->variable_declaration;
|
||||
|
||||
if (vd->name_count == vd->value_count) {
|
||||
AstNode *name = vd->name_list;
|
||||
AstNode *value = vd->value_list;
|
||||
for (;
|
||||
name != NULL && value != NULL;
|
||||
name = name->next, value = value->next) {
|
||||
if (!is_blank_identifier(name)) {
|
||||
ir_add_local_for_identifier(p, name);
|
||||
}
|
||||
// auto lvalue = build_address(p, name, false);
|
||||
// build_assignment(p, lvalue, value, true, NULL);
|
||||
}
|
||||
} else if (vd->value_count == 0) {
|
||||
AstNode *name = vd->name_list;
|
||||
for (;
|
||||
name != NULL;
|
||||
name = name->next) {
|
||||
if (!is_blank_identifier(name)) {
|
||||
|
||||
}
|
||||
|
||||
// build_assignment(p, )
|
||||
}
|
||||
} else {
|
||||
// TODO(bill): Tuple
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
void ir_build_expression(irValue *p, AstNode *e) {
|
||||
GB_ASSERT(p->kind == irValue_Procedure);
|
||||
|
||||
}
|
||||
|
||||
|
||||
void ir_build_statement(irValue *p, AstNode *s);
|
||||
|
||||
void ir_build_statement_list(irValue *p, AstNode *list) {
|
||||
GB_ASSERT(p->kind == irValue_Procedure);
|
||||
for (AstNode *item = list; item != NULL; item = item->next) {
|
||||
ir_build_statement(p, item);
|
||||
}
|
||||
}
|
||||
|
||||
void ir_build_statement(irValue *p, AstNode *s) {
|
||||
GB_ASSERT(p->kind == irValue_Procedure);
|
||||
|
||||
switch (s->kind) {
|
||||
case AstNode_EmptyStatement:
|
||||
break;
|
||||
|
||||
case AstNode_VariableDeclaration: {
|
||||
auto *vd = &s->variable_declaration;
|
||||
if (vd->kind == Declaration_Mutable) {
|
||||
ir_build_variable_declaration(p, s);
|
||||
}
|
||||
} break;
|
||||
|
||||
|
||||
case AstNode_ExpressionStatement:
|
||||
ir_build_expression(p, s->expression_statement.expression);
|
||||
break;
|
||||
|
||||
case AstNode_BlockStatement:
|
||||
ir_build_statement_list(p, s->block_statement.list);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void ir_begin_procedure_body(irValue *p) {
|
||||
gbAllocator a = gb_heap_allocator();
|
||||
p->procedure.curr_block = ir_add_basic_block(a, p, make_string("entry"));
|
||||
map_init(&p->procedure.variables, a);
|
||||
}
|
||||
|
||||
void ir_end_procedure_body(irValue *p) {
|
||||
p->procedure.curr_block = NULL;
|
||||
map_destroy(&p->procedure.variables);
|
||||
}
|
||||
|
||||
|
||||
void ir_build_procedure(irModule *m, irValue *p) {
|
||||
if (p->procedure.blocks != NULL)
|
||||
return;
|
||||
AstNode *proc_type = NULL;
|
||||
AstNode *body = NULL;
|
||||
switch (p->procedure.node->kind) {
|
||||
case AstNode_ProcedureDeclaration:
|
||||
proc_type = p->procedure.node->procedure_declaration.procedure_type;
|
||||
body = p->procedure.node->procedure_declaration.body;
|
||||
break;
|
||||
case AstNode_ProcedureLiteral:
|
||||
proc_type = p->procedure.node->procedure_literal.type;
|
||||
body = p->procedure.node->procedure_literal.body;
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
if (body == NULL) {
|
||||
// NOTE(bill): External procedure
|
||||
return;
|
||||
}
|
||||
|
||||
defer (gb_printf("build procedure %.*s\n", LIT(p->procedure.token.string)));
|
||||
|
||||
|
||||
ir_begin_procedure_body(p);
|
||||
ir_build_statement(p, body);
|
||||
ir_end_procedure_body(p);
|
||||
}
|
||||
|
||||
void ir_build_proc_decl(irModule *m, AstNode *decl) {
|
||||
GB_ASSERT(decl != NULL);
|
||||
auto *pd = &decl->procedure_declaration;
|
||||
if (is_blank_identifier(pd->name))
|
||||
return;
|
||||
|
||||
Entity *e = entity_of_identifier(m->checker, pd->name);
|
||||
irValue *p = *map_get(&m->values, hash_pointer(e));
|
||||
ir_build_procedure(m, p);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void generate_code(Codegen *c) {
|
||||
gbAllocator a = gb_heap_allocator();
|
||||
|
||||
ir_module_create(&c->module);
|
||||
|
||||
for (isize i = 0; i < gb_array_count(c->module.values.entries); i++) {
|
||||
irValue *v = c->module.values.entries[i].value;
|
||||
switch (v->kind) {
|
||||
case irValue_Procedure:
|
||||
ir_build_proc_decl(&c->module, v->procedure.node);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
401
src/codegen/print.cpp
Normal file
401
src/codegen/print.cpp
Normal file
@@ -0,0 +1,401 @@
|
||||
void ssa_fprintf(gbFile *f, char *fmt, ...) {
|
||||
va_list va;
|
||||
va_start(va, fmt);
|
||||
gb_fprintf_va(f, fmt, va);
|
||||
#if 1
|
||||
gb_printf_va(fmt, va);
|
||||
#endif
|
||||
va_end(va);
|
||||
}
|
||||
|
||||
|
||||
b32 ssa_valid_char(u8 c) {
|
||||
if (gb_char_is_alphanumeric(c))
|
||||
return true;
|
||||
|
||||
switch (c) {
|
||||
case '$':
|
||||
case '-':
|
||||
case '.':
|
||||
case '_':
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void ssa_print_escape_string(gbFile *f, String name) {
|
||||
isize extra = 0;
|
||||
for (isize i = 0; i < name.len; i++) {
|
||||
u8 c = name.text[i];
|
||||
if (!ssa_valid_char(c))
|
||||
extra += 2;
|
||||
}
|
||||
|
||||
if (extra == 0) {
|
||||
ssa_fprintf(f, "%.*s", LIT(name));
|
||||
return;
|
||||
}
|
||||
|
||||
char hex_table[] = "0123456789ABCDEF";
|
||||
isize buf_len = name.len + extra;
|
||||
u8 *buf = gb_alloc_array(gb_heap_allocator(), u8, buf_len);
|
||||
defer (gb_free(gb_heap_allocator(), buf));
|
||||
|
||||
isize j = 0;
|
||||
for (isize i = 0; i < name.len; i++) {
|
||||
u8 c = name.text[i];
|
||||
if (ssa_valid_char(c)) {
|
||||
buf[j++] = c;
|
||||
} else {
|
||||
buf[j] = '\\';
|
||||
buf[j+1] = hex_table[c >> 4];
|
||||
buf[j+2] = hex_table[c & 0x0f];
|
||||
j += 3;
|
||||
}
|
||||
}
|
||||
|
||||
gb_file_write(f, buf, buf_len);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void ssa_print_encoded_local(gbFile *f, String name) {
|
||||
ssa_fprintf(f, "%%");
|
||||
ssa_print_escape_string(f, name);
|
||||
}
|
||||
|
||||
void ssa_print_encoded_global(gbFile *f, String name) {
|
||||
ssa_fprintf(f, "@");
|
||||
ssa_print_escape_string(f, name);
|
||||
}
|
||||
|
||||
|
||||
void ssa_print_type(gbFile *f, BaseTypeSizes s, Type *t) {
|
||||
i64 word_bits = 8*s.word_size;
|
||||
GB_ASSERT_NOT_NULL(t);
|
||||
t = default_type(t);
|
||||
|
||||
switch (t->kind) {
|
||||
case Type_Basic:
|
||||
switch (t->basic.kind) {
|
||||
case Basic_bool: ssa_fprintf(f, "i1"); break;
|
||||
case Basic_i8: ssa_fprintf(f, "i8"); break;
|
||||
case Basic_i16: ssa_fprintf(f, "i16"); break;
|
||||
case Basic_i32: ssa_fprintf(f, "i32"); break;
|
||||
case Basic_i64: ssa_fprintf(f, "i64"); break;
|
||||
case Basic_u8: ssa_fprintf(f, "i8"); break;
|
||||
case Basic_u16: ssa_fprintf(f, "i16"); break;
|
||||
case Basic_u32: ssa_fprintf(f, "i32"); break;
|
||||
case Basic_u64: ssa_fprintf(f, "i64"); break;
|
||||
case Basic_f32: ssa_fprintf(f, "float"); break;
|
||||
case Basic_f64: ssa_fprintf(f, "double"); break;
|
||||
case Basic_rawptr: ssa_fprintf(f, "void*"); break;
|
||||
case Basic_string: ssa_fprintf(f, "{i8*, i%lld}", word_bits); break;
|
||||
case Basic_int: ssa_fprintf(f, "i%lld", word_bits); break;
|
||||
case Basic_uint: ssa_fprintf(f, "i%lld", word_bits); break;
|
||||
}
|
||||
break;
|
||||
case Type_Array:
|
||||
ssa_fprintf(f, "[%lld x ", t->array.count);
|
||||
ssa_print_type(f, s, t->array.element);
|
||||
ssa_fprintf(f, "]");
|
||||
break;
|
||||
case Type_Slice:
|
||||
ssa_fprintf(f, "{");
|
||||
ssa_print_type(f, s, t->slice.element);
|
||||
ssa_fprintf(f, "*, %lld, %lld}", word_bits, word_bits);
|
||||
break;
|
||||
case Type_Structure:
|
||||
ssa_fprintf(f, "{");
|
||||
for (isize i = 0; i < t->structure.field_count; i++) {
|
||||
if (i > 0) ssa_fprintf(f, ", ");
|
||||
ssa_print_type(f, s, t->structure.fields[i]->type);
|
||||
}
|
||||
ssa_fprintf(f, "}");
|
||||
break;
|
||||
case Type_Pointer:
|
||||
ssa_print_type(f, s, t->pointer.element);
|
||||
ssa_fprintf(f, "*");
|
||||
break;
|
||||
case Type_Named:
|
||||
ssa_print_encoded_local(f, t->named.name);
|
||||
break;
|
||||
case Type_Alias:
|
||||
ssa_print_type(f, s, t->alias.base);
|
||||
break;
|
||||
case Type_Tuple:
|
||||
if (t->tuple.variable_count == 1) {
|
||||
ssa_print_type(f, s, t->tuple.variables[0]->type);
|
||||
} else {
|
||||
ssa_fprintf(f, "{");
|
||||
for (isize i = 0; i < t->tuple.variable_count; i++) {
|
||||
if (i > 0) ssa_fprintf(f, ", ");
|
||||
ssa_print_type(f, s, t->tuple.variables[i]->type);
|
||||
}
|
||||
ssa_fprintf(f, "}");
|
||||
}
|
||||
break;
|
||||
case Type_Procedure:
|
||||
if (t->procedure.result_count == 0)
|
||||
ssa_fprintf(f, "void");
|
||||
else
|
||||
ssa_print_type(f, s, t->procedure.results);
|
||||
ssa_fprintf(f, " (");
|
||||
for (isize i = 0; i < t->procedure.param_count; i++) {
|
||||
if (i > 0) ssa_fprintf(f, ", ");
|
||||
ssa_print_type(f, s, &t->procedure.params[i]);
|
||||
}
|
||||
ssa_fprintf(f, ")");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void ssa_print_exact_value(gbFile *f, ssaModule *m, ExactValue value, Type *type) {
|
||||
switch (value.kind) {
|
||||
case ExactValue_Bool:
|
||||
ssa_fprintf(f, (value.value_bool ? "true" : "false"));
|
||||
break;
|
||||
case ExactValue_String: {
|
||||
ssa_fprintf(f, "{");
|
||||
ssa_print_type(f, m->sizes, &basic_types[Basic_i8]);
|
||||
ssa_fprintf(f, "* \"");
|
||||
// TODO(bill): Make unquote string function
|
||||
String unquoted = value.value_string;
|
||||
unquoted.text++;
|
||||
unquoted.len -= 2;
|
||||
ssa_print_escape_string(f, unquoted);
|
||||
ssa_fprintf(f, "\", ");
|
||||
ssa_print_type(f, m->sizes, &basic_types[Basic_int]);
|
||||
ssa_fprintf(f, " %td}", value.value_string.len);
|
||||
} break;
|
||||
case ExactValue_Integer:
|
||||
ssa_fprintf(f, "%lld", value.value_integer);
|
||||
break;
|
||||
case ExactValue_Float: {
|
||||
u64 u = 0;
|
||||
if (is_type_float(type) && type->basic.kind == Basic_f32) {
|
||||
// IMPORTANT NOTE(bill): LLVM requires all floating point constants to be
|
||||
// a 64 bit number if bits_of(float type) <= 64.
|
||||
// To overcome this problem, fill the "bottom" 32 bits with zeros
|
||||
// https://groups.google.com/forum/#!topic/llvm-dev/IlqV3TbSk6M
|
||||
f32 fp = cast(f32)value.value_float;
|
||||
u = *cast(u32 *)&fp;
|
||||
u <<= 32;
|
||||
|
||||
} else {
|
||||
u = *cast(u64 *)&value.value_float;
|
||||
}
|
||||
ssa_fprintf(f, "0x%llx", u);
|
||||
} break;
|
||||
case ExactValue_Pointer:
|
||||
if (value.value_float == NULL) {
|
||||
ssa_fprintf(f, "null");
|
||||
} else {
|
||||
GB_PANIC("TODO(bill): ExactValue_Pointer");
|
||||
}
|
||||
break;
|
||||
default:
|
||||
GB_PANIC("Invalid ExactValue");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void ssa_print_value(gbFile *f, ssaModule *m, ssaValue *value, Type *type_hint) {
|
||||
if (value == NULL) {
|
||||
ssa_fprintf(f, "!!!NULL_VALUE");
|
||||
return;
|
||||
}
|
||||
switch (value->kind) {
|
||||
case ssaValue_TypeName:
|
||||
ssa_print_encoded_local(f, value->type_name->token.string);
|
||||
break;
|
||||
case ssaValue_Global:
|
||||
ssa_print_encoded_global(f, value->global.entity->token.string);
|
||||
break;
|
||||
case ssaValue_Procedure:
|
||||
ssa_print_encoded_global(f, value->procedure.entity->token.string);
|
||||
break;
|
||||
case ssaValue_Constant: {
|
||||
ssa_print_exact_value(f, m, value->constant.value, type_hint);
|
||||
} break;
|
||||
case ssaValue_Instruction:
|
||||
ssa_fprintf(f, "%%%d", value->id);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void ssa_print_instruction(gbFile *f, ssaModule *m, ssaValue *value) {
|
||||
GB_ASSERT(value->kind == ssaValue_Instruction);
|
||||
|
||||
ssa_fprintf(f, "\t");
|
||||
|
||||
ssaInstruction *instr = &value->instruction;
|
||||
switch (instr->kind) {
|
||||
case ssaInstruction_Local: {
|
||||
Type *type = instr->local.entity->type;
|
||||
ssa_fprintf(f, "%%%d = alloca ", value->id);
|
||||
ssa_print_type(f, m->sizes, type);
|
||||
ssa_fprintf(f, ", align %lld\n", type_align_of(m->sizes, gb_heap_allocator(), type));
|
||||
ssa_fprintf(f, "\tstore ");
|
||||
ssa_print_type(f, m->sizes, type);
|
||||
ssa_fprintf(f, " zeroinitializer, ");
|
||||
ssa_print_type(f, m->sizes, type);
|
||||
ssa_fprintf(f, "* %%%d\n", value->id);
|
||||
} break;
|
||||
|
||||
case ssaInstruction_Store: {
|
||||
Type *type = ssa_value_type(instr->store.address);
|
||||
ssa_fprintf(f, "store ");
|
||||
ssa_print_type(f, m->sizes, type);
|
||||
ssa_fprintf(f, " ");
|
||||
ssa_print_value(f, m, instr->store.value, type);
|
||||
ssa_fprintf(f, ", ");
|
||||
ssa_print_type(f, m->sizes, type);
|
||||
ssa_fprintf(f, "* ");
|
||||
ssa_print_value(f, m, instr->store.address, type);
|
||||
ssa_fprintf(f, "\n");
|
||||
} break;
|
||||
|
||||
case ssaInstruction_Load: {
|
||||
Type *type = ssa_value_type(instr->load.address);
|
||||
ssa_fprintf(f, "%%%d = load ", value->id);
|
||||
ssa_print_type(f, m->sizes, type);
|
||||
ssa_fprintf(f, ", ");
|
||||
ssa_print_type(f, m->sizes, type);
|
||||
ssa_fprintf(f, "* ");
|
||||
ssa_print_value(f, m, instr->load.address, type);
|
||||
ssa_fprintf(f, "\n");
|
||||
} break;
|
||||
|
||||
|
||||
case ssaInstruction_BinaryOp: {
|
||||
ssaBinaryOp *bo = &value->instruction.binary_op;
|
||||
Type *type = ssa_value_type(bo->left);
|
||||
|
||||
if (is_type_float(type))
|
||||
ssa_fprintf(f, "f");
|
||||
|
||||
switch (bo->op.kind) {
|
||||
case Token_Add: ssa_fprintf(f, "add"); break;
|
||||
case Token_Sub: ssa_fprintf(f, "sub"); break;
|
||||
case Token_And: ssa_fprintf(f, "and"); break;
|
||||
case Token_Or: ssa_fprintf(f, "or"); break;
|
||||
case Token_Xor: ssa_fprintf(f, "xor"); break;
|
||||
|
||||
case Token_AndNot: GB_PANIC("TODO(bill): print Token_AndNot");
|
||||
|
||||
case Token_Mul: ssa_fprintf(f, "mul"); break;
|
||||
|
||||
default: {
|
||||
if (!is_type_float(type)) {
|
||||
if (is_type_unsigned(type))
|
||||
ssa_fprintf(f, "u");
|
||||
else
|
||||
ssa_fprintf(f, "s");
|
||||
}
|
||||
|
||||
switch (bo->op.kind) {
|
||||
case Token_Quo: ssa_fprintf(f, "div"); break;
|
||||
case Token_Mod: ssa_fprintf(f, "rem"); break;
|
||||
}
|
||||
} break;
|
||||
}
|
||||
|
||||
ssa_fprintf(f, " ");
|
||||
ssa_print_type(f, m->sizes, type);
|
||||
ssa_fprintf(f, " ");
|
||||
ssa_print_value(f, m, bo->left, type);
|
||||
ssa_fprintf(f, ", ");
|
||||
ssa_print_value(f, m, bo->right, type);
|
||||
ssa_fprintf(f, "\n");
|
||||
|
||||
} break;
|
||||
|
||||
default:
|
||||
ssa_fprintf(f, "; <unknown instr> %d\n", instr->kind);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void ssa_print_llvm_ir(gbFile *f, ssaModule *m) {
|
||||
gb_printf("-- Printing LLVM-IR -- \n\n");
|
||||
gb_for_array(member_index, m->members.entries) {
|
||||
auto *entry = &m->members.entries[member_index];
|
||||
ssaValue *v = entry->value;
|
||||
switch (v->kind) {
|
||||
case ssaValue_TypeName: {
|
||||
ssa_print_encoded_local(f, v->type_name->token.string);
|
||||
ssa_fprintf(f, " = type ");
|
||||
ssa_print_type(f, m->sizes, get_base_type(v->type_name->type));
|
||||
ssa_fprintf(f, "\n");
|
||||
} break;
|
||||
|
||||
case ssaValue_Global: {
|
||||
ssaGlobal *g = &v->global;
|
||||
ssa_print_encoded_global(f, g->entity->token.string);
|
||||
ssa_fprintf(f, " = global ");
|
||||
ssa_print_type(f, m->sizes, get_base_type(g->entity->type));
|
||||
ssa_fprintf(f, " ");
|
||||
ssa_print_value(f, m, g->value, g->entity->type);
|
||||
ssa_fprintf(f, ", align %td\n", type_align_of(m->sizes, gb_heap_allocator(), g->entity->type));
|
||||
} break;
|
||||
|
||||
case ssaValue_Procedure: {
|
||||
ssaProcedure *proc = &v->procedure;
|
||||
if (proc->body == NULL) {
|
||||
ssa_fprintf(f, "declare ");
|
||||
} else {
|
||||
ssa_fprintf(f, "define ");
|
||||
}
|
||||
|
||||
auto *proc_type = &proc->entity->type->procedure;
|
||||
|
||||
if (proc_type->result_count == 0) {
|
||||
ssa_fprintf(f, "void");
|
||||
} else {
|
||||
ssa_print_type(f, m->sizes, proc_type->results);
|
||||
}
|
||||
|
||||
ssa_fprintf(f, " ");
|
||||
|
||||
ssa_print_encoded_global(f, proc->name);
|
||||
ssa_fprintf(f, "(");
|
||||
|
||||
if (proc_type->param_count > 0) {
|
||||
auto *params = &proc_type->params->tuple;
|
||||
for (isize i = 0; i < params->variable_count; i++) {
|
||||
Entity *e = params->variables[i];
|
||||
if (i > 0)
|
||||
ssa_fprintf(f, ", ");
|
||||
ssa_print_type(f, m->sizes, e->type);
|
||||
ssa_fprintf(f, " %%%.*s", LIT(e->token.string));
|
||||
}
|
||||
}
|
||||
|
||||
ssa_fprintf(f, ") ");
|
||||
|
||||
if (proc->body == NULL) {
|
||||
ssa_fprintf(f, "\n");
|
||||
} else {
|
||||
ssa_fprintf(f, "{\n");
|
||||
gb_for_array(i, proc->blocks) {
|
||||
ssaBlock *block = &proc->blocks[i]->block;
|
||||
ssa_fprintf(f, "%.*s:\n", LIT(block->label));
|
||||
gb_for_array(j, block->instructions) {
|
||||
ssaValue *value = block->instructions[j];
|
||||
ssa_print_instruction(f, m, value);
|
||||
}
|
||||
}
|
||||
|
||||
if (proc_type->result_count == 0) {
|
||||
ssa_fprintf(f, "\tret void\n");
|
||||
}
|
||||
ssa_fprintf(f, "}\n");
|
||||
}
|
||||
|
||||
} break;
|
||||
}
|
||||
}
|
||||
}
|
||||
746
src/codegen/ssa.cpp
Normal file
746
src/codegen/ssa.cpp
Normal file
@@ -0,0 +1,746 @@
|
||||
struct ssaModule;
|
||||
struct ssaProcedure;
|
||||
struct ssaBlock;
|
||||
struct ssaValue;
|
||||
|
||||
|
||||
struct ssaModule {
|
||||
CheckerInfo *info;
|
||||
BaseTypeSizes sizes;
|
||||
gbAllocator allocator;
|
||||
|
||||
Map<ssaValue *> values; // Key: Entity *
|
||||
Map<ssaValue *> members; // Key: String
|
||||
i32 global_string_index;
|
||||
};
|
||||
|
||||
|
||||
struct ssaBlock {
|
||||
i32 id;
|
||||
AstNode *node;
|
||||
Scope *scope;
|
||||
String label;
|
||||
ssaProcedure *parent;
|
||||
|
||||
gbArray(ssaValue *) instructions;
|
||||
gbArray(ssaValue *) values;
|
||||
};
|
||||
|
||||
struct ssaProcedure {
|
||||
ssaModule *module;
|
||||
String name;
|
||||
Entity *entity;
|
||||
DeclarationInfo *decl;
|
||||
AstNode *type_expr;
|
||||
AstNode *body;
|
||||
|
||||
gbArray(ssaValue *) blocks;
|
||||
ssaBlock *curr_block;
|
||||
};
|
||||
|
||||
|
||||
struct ssaLocal {
|
||||
Entity *entity;
|
||||
};
|
||||
struct ssaGlobal {
|
||||
b32 generated;
|
||||
Entity *entity;
|
||||
ssaValue *value;
|
||||
};
|
||||
struct ssaStore {
|
||||
ssaValue *address;
|
||||
ssaValue *value;
|
||||
};
|
||||
struct ssaLoad {
|
||||
ssaValue *address;
|
||||
};
|
||||
struct ssaBinaryOp {
|
||||
Token op;
|
||||
ssaValue *left, *right;
|
||||
};
|
||||
struct ssaGetElementPtr {
|
||||
ssaValue *address;
|
||||
Type *result_type;
|
||||
Type *element_type;
|
||||
gbArray(ssaValue *) indices;
|
||||
};
|
||||
|
||||
enum ssaInstructionKind {
|
||||
ssaInstruction_Invalid,
|
||||
|
||||
ssaInstruction_Local,
|
||||
ssaInstruction_Store,
|
||||
ssaInstruction_Load,
|
||||
ssaInstruction_GetElementPtr,
|
||||
|
||||
ssaInstruction_BinaryOp,
|
||||
|
||||
ssaInstruction_Count,
|
||||
};
|
||||
|
||||
struct ssaInstruction {
|
||||
ssaInstructionKind kind;
|
||||
|
||||
ssaBlock *parent;
|
||||
Type *type;
|
||||
TokenPos pos;
|
||||
|
||||
union {
|
||||
ssaLocal local;
|
||||
ssaStore store;
|
||||
ssaLoad load;
|
||||
ssaGetElementPtr get_element_ptr;
|
||||
|
||||
ssaBinaryOp binary_op;
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
enum ssaValueKind {
|
||||
ssaValue_Invalid,
|
||||
|
||||
ssaValue_TypeName,
|
||||
ssaValue_Global,
|
||||
ssaValue_Procedure,
|
||||
ssaValue_Constant,
|
||||
|
||||
ssaValue_Block,
|
||||
ssaValue_Instruction,
|
||||
|
||||
ssaValue_Count,
|
||||
};
|
||||
|
||||
struct ssaValue {
|
||||
ssaValueKind kind;
|
||||
i32 id;
|
||||
|
||||
union {
|
||||
Entity * type_name;
|
||||
ssaGlobal global;
|
||||
ssaProcedure procedure;
|
||||
TypeAndValue constant;
|
||||
ssaBlock block;
|
||||
ssaInstruction instruction;
|
||||
};
|
||||
};
|
||||
|
||||
enum ssaLvalueKind {
|
||||
ssaLvalue_Blank,
|
||||
ssaLvalue_Address,
|
||||
|
||||
ssaLvalue_Count,
|
||||
};
|
||||
|
||||
struct ssaLvalue {
|
||||
ssaLvalueKind kind;
|
||||
union {
|
||||
struct {} blank;
|
||||
struct {
|
||||
ssaValue *value;
|
||||
AstNode *expr;
|
||||
} address;
|
||||
};
|
||||
};
|
||||
|
||||
void ssa_module_init(ssaModule *m, Checker *c) {
|
||||
m->allocator = gb_heap_allocator();
|
||||
m->info = &c->info;
|
||||
m->sizes = c->sizes;
|
||||
|
||||
map_init(&m->values, m->allocator);
|
||||
map_init(&m->members, m->allocator);
|
||||
}
|
||||
|
||||
void ssa_module_destroy(ssaModule *m) {
|
||||
map_destroy(&m->values);
|
||||
map_destroy(&m->members);
|
||||
}
|
||||
|
||||
void ssa_module_add_value(ssaModule *m, Entity *e, ssaValue *v) {
|
||||
map_set(&m->values, hash_pointer(e), v);
|
||||
}
|
||||
|
||||
|
||||
Type *ssa_value_type(ssaValue *value);
|
||||
|
||||
Type *ssa_instruction_type(ssaInstruction *instr) {
|
||||
switch (instr->kind) {
|
||||
case ssaInstruction_Local:
|
||||
return instr->local.entity->type;
|
||||
case ssaInstruction_Store:
|
||||
return ssa_value_type(instr->store.address);
|
||||
case ssaInstruction_Load:
|
||||
return ssa_value_type(instr->load.address);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Type *ssa_value_type(ssaValue *value) {
|
||||
switch (value->kind) {
|
||||
case ssaValue_TypeName:
|
||||
return value->type_name->type;
|
||||
case ssaValue_Global:
|
||||
return value->global.entity->type;
|
||||
case ssaValue_Procedure:
|
||||
return value->procedure.entity->type;
|
||||
case ssaValue_Constant:
|
||||
return value->constant.type;
|
||||
case ssaValue_Instruction:
|
||||
return ssa_instruction_type(&value->instruction);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
ssaValue *ssa_alloc_value(gbAllocator a, ssaValueKind kind) {
|
||||
ssaValue *v = gb_alloc_item(a, ssaValue);
|
||||
v->kind = kind;
|
||||
return v;
|
||||
}
|
||||
|
||||
ssaValue *ssa_alloc_instruction(gbAllocator a, ssaInstructionKind kind) {
|
||||
ssaValue *v = ssa_alloc_value(a, ssaValue_Instruction);
|
||||
v->instruction.kind = kind;
|
||||
return v;
|
||||
}
|
||||
|
||||
ssaValue *ssa_make_value_type_name(gbAllocator a, Entity *e) {
|
||||
ssaValue *v = ssa_alloc_value(a, ssaValue_TypeName);
|
||||
v->type_name = e;
|
||||
return v;
|
||||
}
|
||||
|
||||
ssaValue *ssa_make_value_global(gbAllocator a, Entity *e, ssaValue *value) {
|
||||
ssaValue *v = ssa_alloc_value(a, ssaValue_Global);
|
||||
v->global.entity = e;
|
||||
v->global.value = value;
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
|
||||
ssaValue *ssa_make_instruction_local(ssaProcedure *p, Entity *e) {
|
||||
ssaValue *v = ssa_alloc_instruction(p->module->allocator, ssaInstruction_Local);
|
||||
ssaInstruction *i = &v->instruction;
|
||||
i->local.entity = e;
|
||||
if (p->curr_block) {
|
||||
gb_array_append(p->curr_block->values, v);
|
||||
}
|
||||
ssa_module_add_value(p->module, e, v);
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
ssaValue *ssa_make_instruction_store(ssaProcedure *p, ssaValue *address, ssaValue *value) {
|
||||
ssaValue *v = ssa_alloc_instruction(p->module->allocator, ssaInstruction_Store);
|
||||
ssaInstruction *i = &v->instruction;
|
||||
i->store.address = address;
|
||||
i->store.value = value;
|
||||
if (p->curr_block) {
|
||||
gb_array_append(p->curr_block->values, v);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
ssaValue *ssa_make_instruction_load(ssaProcedure *p, ssaValue *address) {
|
||||
ssaValue *v = ssa_alloc_instruction(p->module->allocator, ssaInstruction_Load);
|
||||
ssaInstruction *i = &v->instruction;
|
||||
i->load.address = address;
|
||||
if (p->curr_block) {
|
||||
gb_array_append(p->curr_block->values, v);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
ssaValue *ssa_make_instruction_get_element_ptr(ssaProcedure *p, ssaValue *address) {
|
||||
ssaValue *v = ssa_alloc_instruction(p->module->allocator, ssaInstruction_GetElementPtr);
|
||||
ssaInstruction *i = &v->instruction;
|
||||
i->get_element_ptr.address = address;
|
||||
if (p->curr_block) {
|
||||
gb_array_append(p->curr_block->values, v);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
ssaValue *ssa_make_instruction_binary_op(ssaProcedure *p, Token op, ssaValue *left, ssaValue *right) {
|
||||
ssaValue *v = ssa_alloc_instruction(p->module->allocator, ssaInstruction_BinaryOp);
|
||||
ssaInstruction *i = &v->instruction;
|
||||
i->binary_op.op = op;
|
||||
i->binary_op.left = left;
|
||||
i->binary_op.right = right;
|
||||
if (p->curr_block) {
|
||||
gb_array_append(p->curr_block->values, v);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
ssaValue *ssa_make_value_constant(gbAllocator a, Type *type, ExactValue value) {
|
||||
ssaValue *v = ssa_alloc_value(a, ssaValue_Constant);
|
||||
v->constant.type = type;
|
||||
v->constant.value = value;
|
||||
return v;
|
||||
}
|
||||
|
||||
ssaValue *ssa_make_value_procedure(gbAllocator a, Entity *e, DeclarationInfo *decl, ssaModule *m) {
|
||||
ssaValue *v = ssa_alloc_value(a, ssaValue_Procedure);
|
||||
v->procedure.module = m;
|
||||
v->procedure.entity = e;
|
||||
v->procedure.decl = decl;
|
||||
v->procedure.name = e->token.string;
|
||||
return v;
|
||||
}
|
||||
|
||||
ssaValue *ssa_make_value_block(gbAllocator a, ssaProcedure *proc, AstNode *node, Scope *scope, String label) {
|
||||
ssaValue *v = ssa_alloc_value(a, ssaValue_Block);
|
||||
v->block.label = label;
|
||||
v->block.node = node;
|
||||
v->block.scope = scope;
|
||||
v->block.parent = proc;
|
||||
|
||||
gb_array_init(v->block.instructions, gb_heap_allocator());
|
||||
gb_array_init(v->block.values, gb_heap_allocator());
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
|
||||
ssaValue *ssa_add_global_string(ssaProcedure *proc, ExactValue value) {
|
||||
GB_ASSERT(value.kind == ExactValue_String);
|
||||
gbAllocator a = gb_heap_allocator();
|
||||
|
||||
|
||||
isize max_len = 4+8+1;
|
||||
u8 *str = cast(u8 *)gb_alloc_array(a, u8, max_len);
|
||||
isize len = gb_snprintf(cast(char *)str, max_len, ".str%x", proc->module->global_string_index);
|
||||
proc->module->global_string_index++;
|
||||
|
||||
String name = make_string(str, len-1);
|
||||
Token token = {Token_String};
|
||||
token.string = name;
|
||||
Type *type = &basic_types[Basic_string];
|
||||
Entity *entity = make_entity_constant(a, NULL, token, type, value);
|
||||
ssaValue *v = ssa_make_value_constant(a, type, value);
|
||||
|
||||
|
||||
ssaValue *g = ssa_make_value_global(a, entity, v);
|
||||
g->global.generated = true;
|
||||
|
||||
map_set(&proc->module->values, hash_pointer(entity), g);
|
||||
map_set(&proc->module->members, hash_string(name), g);
|
||||
|
||||
return g;
|
||||
}
|
||||
|
||||
|
||||
ssaValue *ssa_add_block(ssaProcedure *proc, AstNode *node, String label) {
|
||||
gbAllocator a = proc->module->allocator;
|
||||
Scope *scope = NULL;
|
||||
Scope **found = map_get(&proc->module->info->scopes, hash_pointer(node));
|
||||
if (found) scope = *found;
|
||||
ssaValue *block = ssa_make_value_block(a, proc, node, scope, label);
|
||||
gb_array_append(proc->blocks, block);
|
||||
return block;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void ssa_begin_procedure_body(ssaProcedure *proc) {
|
||||
gb_array_init(proc->blocks, gb_heap_allocator());
|
||||
ssaValue *b = ssa_add_block(proc, proc->body, make_string("entry"));
|
||||
proc->curr_block = &b->block;
|
||||
}
|
||||
|
||||
|
||||
void ssa_end_procedure_body(ssaProcedure *proc) {
|
||||
|
||||
// Number registers
|
||||
i32 reg_id = 0;
|
||||
gb_for_array(i, proc->blocks) {
|
||||
ssaBlock *b = &proc->blocks[i]->block;
|
||||
gb_for_array(j, b->instructions) {
|
||||
ssaValue *value = b->instructions[j];
|
||||
ssaInstruction *instr = &value->instruction;
|
||||
if (instr->kind == ssaInstruction_Store) {
|
||||
continue;
|
||||
}
|
||||
value->id = reg_id;
|
||||
reg_id++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
b32 ssa_is_blank_identifier(AstNode *i) {
|
||||
GB_ASSERT(i->kind == AstNode_Identifier);
|
||||
return are_strings_equal(i->identifier.token.string, make_string("_"));
|
||||
}
|
||||
|
||||
|
||||
ssaValue *ssa_block_emit(ssaBlock *b, ssaValue *instr) {
|
||||
instr->instruction.parent = b;
|
||||
gb_array_append(b->instructions, instr);
|
||||
return instr;
|
||||
}
|
||||
|
||||
ssaValue *ssa_emit(ssaProcedure *proc, ssaValue *instr) {
|
||||
return ssa_block_emit(proc->curr_block, instr);
|
||||
}
|
||||
|
||||
|
||||
ssaValue *ssa_add_local(ssaProcedure *proc, Entity *e) {
|
||||
ssaValue *instr = ssa_make_instruction_local(proc, e);
|
||||
ssa_emit(proc, instr);
|
||||
return instr;
|
||||
}
|
||||
|
||||
ssaValue *ssa_add_local_for_identifier(ssaProcedure *proc, AstNode *name) {
|
||||
Entity **found = map_get(&proc->module->info->definitions, hash_pointer(name));
|
||||
if (found) {
|
||||
return ssa_add_local(proc, *found);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
ssaValue *ssa_emit_store(ssaProcedure *p, ssaValue *address, ssaValue *value) {
|
||||
ssaValue *store = ssa_make_instruction_store(p, address, value);
|
||||
ssa_emit(p, store);
|
||||
return store;
|
||||
}
|
||||
|
||||
ssaValue *ssa_emit_load(ssaProcedure *p, ssaValue *address) {
|
||||
ssaValue *v = ssa_make_instruction_load(p, address);
|
||||
ssa_emit(p, v);
|
||||
return v;
|
||||
}
|
||||
|
||||
ssaValue *ssa_lvalue_store(ssaLvalue lval, ssaProcedure *p, ssaValue *value) {
|
||||
switch (lval.kind) {
|
||||
case ssaLvalue_Address:
|
||||
return ssa_emit_store(p, lval.address.value, value);
|
||||
}
|
||||
GB_PANIC("Illegal lvalue store");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ssaValue *ssa_lvalue_load(ssaLvalue lval, ssaProcedure *p) {
|
||||
switch (lval.kind) {
|
||||
case ssaLvalue_Address:
|
||||
return ssa_emit_load(p, lval.address.value);
|
||||
}
|
||||
GB_PANIC("Illegal lvalue load");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
ssaValue *ssa_lvalue_address(ssaLvalue lval, ssaProcedure *p) {
|
||||
switch (lval.kind) {
|
||||
case ssaLvalue_Address:
|
||||
return lval.address.value;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ssaValue *ssa_build_expression(ssaProcedure *proc, AstNode *expr);
|
||||
ssaValue *ssa_build_single_expression(ssaProcedure *proc, AstNode *expr, TypeAndValue *tv);
|
||||
|
||||
ssaValue *ssa_emit_conversion(ssaProcedure *proc, ssaValue *value, Type *a_type) {
|
||||
Type *b_type = ssa_value_type(value);
|
||||
if (are_types_identical(a_type, b_type))
|
||||
return value;
|
||||
|
||||
Type *a = get_base_type(a_type);
|
||||
Type *b = get_base_type(b_type);
|
||||
|
||||
if (value->kind == ssaValue_Constant) {
|
||||
if (a->kind == Type_Basic)
|
||||
return ssa_make_value_constant(proc->module->allocator, a_type, value->constant.value);
|
||||
}
|
||||
|
||||
GB_PANIC("TODO(bill): ssa_emit_conversion");
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ssaValue *ssa_emit_arith(ssaProcedure *proc, Token op, ssaValue *left, ssaValue *right, Type *type) {
|
||||
switch (op.kind) {
|
||||
case Token_Add:
|
||||
case Token_Sub:
|
||||
case Token_Mul:
|
||||
case Token_Quo:
|
||||
case Token_Mod:
|
||||
case Token_And:
|
||||
case Token_Or:
|
||||
case Token_Xor:
|
||||
case Token_AndNot:
|
||||
left = ssa_emit_conversion(proc, left, type);
|
||||
right = ssa_emit_conversion(proc, right, type);
|
||||
break;
|
||||
}
|
||||
|
||||
ssaValue *v = ssa_make_instruction_binary_op(proc, op, left, right);
|
||||
return ssa_emit(proc, v);
|
||||
}
|
||||
|
||||
ssaValue *ssa_build_single_expression(ssaProcedure *proc, AstNode *expr, TypeAndValue *tv) {
|
||||
switch (expr->kind) {
|
||||
case AstNode_Identifier: {
|
||||
Entity *e = *map_get(&proc->module->info->uses, hash_pointer(expr));
|
||||
if (e->kind == Entity_Builtin) {
|
||||
// TODO(bill): Entity_Builtin
|
||||
return NULL;
|
||||
}
|
||||
|
||||
auto *found = map_get(&proc->module->values, hash_pointer(e));
|
||||
if (found) {
|
||||
return ssa_emit_load(proc, *found);
|
||||
}
|
||||
} break;
|
||||
|
||||
case AstNode_UnaryExpression: {
|
||||
auto *ue = &expr->unary_expression;
|
||||
switch (ue->op.kind) {
|
||||
case Token_Add:
|
||||
return ssa_build_expression(proc, ue->operand);
|
||||
case Token_Sub:
|
||||
return NULL;
|
||||
case Token_Xor:
|
||||
return NULL;
|
||||
case Token_Not:
|
||||
return NULL;
|
||||
case Token_Pointer:
|
||||
return NULL;
|
||||
}
|
||||
} break;
|
||||
|
||||
case AstNode_BinaryExpression: {
|
||||
auto *be = &expr->binary_expression;
|
||||
switch (be->op.kind) {
|
||||
case Token_Add:
|
||||
case Token_Sub:
|
||||
case Token_Mul:
|
||||
case Token_Quo:
|
||||
case Token_Mod:
|
||||
case Token_And:
|
||||
case Token_Or:
|
||||
case Token_Xor:
|
||||
case Token_AndNot:
|
||||
return ssa_emit_arith(proc, be->op,
|
||||
ssa_build_expression(proc, be->left),
|
||||
ssa_build_expression(proc, be->right),
|
||||
tv->type);
|
||||
}
|
||||
} break;
|
||||
case AstNode_ProcedureLiteral:
|
||||
break;
|
||||
case AstNode_CastExpression:
|
||||
break;
|
||||
case AstNode_CallExpression:
|
||||
break;
|
||||
case AstNode_SliceExpression:
|
||||
break;
|
||||
case AstNode_IndexExpression:
|
||||
break;
|
||||
case AstNode_SelectorExpression:
|
||||
break;
|
||||
}
|
||||
|
||||
GB_PANIC("Unexpected expression");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
ssaValue *ssa_build_expression(ssaProcedure *proc, AstNode *expr) {
|
||||
expr = unparen_expression(expr);
|
||||
|
||||
TypeAndValue *tv = map_get(&proc->module->info->types, hash_pointer(expr));
|
||||
if (tv) {
|
||||
if (tv->value.kind != ExactValue_Invalid) {
|
||||
if (tv->value.kind == ExactValue_String) {
|
||||
ssaValue *global_str = ssa_add_global_string(proc, tv->value);
|
||||
return ssa_emit_load(proc, global_str);
|
||||
}
|
||||
return ssa_make_value_constant(proc->module->allocator, tv->type, tv->value);
|
||||
}
|
||||
|
||||
ssaValue *value = NULL;
|
||||
if (tv->mode == Addressing_Variable) {
|
||||
gb_printf("!Addressable!\n");
|
||||
// TODO(bill): Addressing_Variable
|
||||
} else {
|
||||
value = ssa_build_single_expression(proc, expr, tv);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
ssaLvalue ssa_build_address(ssaProcedure *proc, AstNode *expr) {
|
||||
switch (expr->kind) {
|
||||
case AstNode_Identifier: {
|
||||
if (!ssa_is_blank_identifier(expr)) {
|
||||
Entity *e = entity_of_identifier(proc->module->info, expr);
|
||||
|
||||
ssaLvalue val = {ssaLvalue_Address};
|
||||
val.address.expr = expr;
|
||||
ssaValue **found = map_get(&proc->module->values, hash_pointer(e));
|
||||
if (found) {
|
||||
val.address.value = *found;
|
||||
}
|
||||
return val;
|
||||
}
|
||||
} break;
|
||||
|
||||
case AstNode_ParenExpression:
|
||||
return ssa_build_address(proc, unparen_expression(expr));
|
||||
|
||||
case AstNode_DereferenceExpression: {
|
||||
ssaLvalue val = {ssaLvalue_Address};
|
||||
val.address.value = ssa_build_expression(proc, expr);
|
||||
val.address.expr = expr;
|
||||
return val;
|
||||
} break;
|
||||
|
||||
case AstNode_SelectorExpression:
|
||||
break;
|
||||
|
||||
case AstNode_IndexExpression:
|
||||
break;
|
||||
|
||||
// TODO(bill): Others address
|
||||
}
|
||||
|
||||
ssaLvalue blank = {ssaLvalue_Blank};
|
||||
return blank;
|
||||
}
|
||||
|
||||
void ssa_build_statement(ssaProcedure *proc, AstNode *s);
|
||||
|
||||
void ssa_build_statement_list(ssaProcedure *proc, AstNode *list) {
|
||||
for (AstNode *stmt = list ; stmt != NULL; stmt = stmt->next)
|
||||
ssa_build_statement(proc, stmt);
|
||||
}
|
||||
|
||||
void ssa_build_statement(ssaProcedure *proc, AstNode *s) {
|
||||
switch (s->kind) {
|
||||
case AstNode_EmptyStatement:
|
||||
break;
|
||||
case AstNode_VariableDeclaration: {
|
||||
auto *vd = &s->variable_declaration;
|
||||
if (vd->kind == Declaration_Mutable) {
|
||||
if (vd->name_count == vd->value_count) { // 1:1 assigment
|
||||
|
||||
} else if (vd->value_count == 0) { // declared and zero-initialized
|
||||
for (AstNode *name = vd->name_list; name != NULL; name = name->next) {
|
||||
if (!ssa_is_blank_identifier(name)) {
|
||||
// TODO(bill): add local
|
||||
ssa_add_local_for_identifier(proc, name);
|
||||
}
|
||||
}
|
||||
} else { // Tuple(s)
|
||||
GB_PANIC("TODO(bill): tuple assignment variable declaration");
|
||||
}
|
||||
}
|
||||
} break;
|
||||
|
||||
case AstNode_AssignStatement: {
|
||||
auto *assign = &s->assign_statement;
|
||||
switch (assign->op.kind) {
|
||||
case Token_Eq: {
|
||||
gbArray(ssaLvalue) lvals;
|
||||
gb_array_init(lvals, gb_heap_allocator());
|
||||
defer (gb_array_free(lvals));
|
||||
|
||||
for (AstNode *lhs = assign->lhs_list;
|
||||
lhs != NULL;
|
||||
lhs = lhs->next) {
|
||||
ssaLvalue lval = {};
|
||||
if (!ssa_is_blank_identifier(lhs)) {
|
||||
lval = ssa_build_address(proc, lhs);
|
||||
}
|
||||
gb_array_append(lvals, lval);
|
||||
}
|
||||
|
||||
if (assign->lhs_count == assign->rhs_count) {
|
||||
if (assign->lhs_count == 1) {
|
||||
AstNode *lhs = assign->lhs_list;
|
||||
AstNode *rhs = assign->rhs_list;
|
||||
ssaValue *init = ssa_build_expression(proc, rhs);
|
||||
ssa_lvalue_store(lvals[0], proc, init);
|
||||
} else {
|
||||
GB_PANIC("TODO(bill): parallel assignment");
|
||||
}
|
||||
} else {
|
||||
GB_PANIC("TODO(bill): tuple assignment");
|
||||
}
|
||||
|
||||
} break;
|
||||
|
||||
default: // +=, -=, etc
|
||||
break;
|
||||
}
|
||||
} break;
|
||||
|
||||
case AstNode_ExpressionStatement:
|
||||
ssa_build_expression(proc, s->expression_statement.expression);
|
||||
break;
|
||||
|
||||
case AstNode_BlockStatement:
|
||||
ssa_build_statement_list(proc, s->block_statement.list);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void ssa_build_procedure(ssaValue *value) {
|
||||
ssaProcedure *proc = &value->procedure;
|
||||
|
||||
gb_printf("Building %.*s: %.*s\n", LIT(entity_strings[proc->entity->kind]), LIT(proc->name));
|
||||
|
||||
|
||||
AstNode *proc_decl = proc->decl->proc_decl;
|
||||
switch (proc_decl->kind) {
|
||||
case AstNode_ProcedureDeclaration:
|
||||
proc->type_expr = proc_decl->procedure_declaration.type;
|
||||
proc->body = proc_decl->procedure_declaration.body;
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
if (proc->body == NULL) {
|
||||
// TODO(bill): External procedure
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
ssa_begin_procedure_body(proc);
|
||||
ssa_build_statement(proc, proc->body);
|
||||
ssa_end_procedure_body(proc);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
0
src/codegen/value.cpp
Normal file
0
src/codegen/value.cpp
Normal file
@@ -11,6 +11,8 @@ typedef struct String {
|
||||
#define LIT(x) (x).len, (x).text
|
||||
|
||||
|
||||
|
||||
|
||||
gb_inline String make_string(u8 *text, isize len) {
|
||||
String s;
|
||||
s.text = text;
|
||||
@@ -90,6 +92,9 @@ gb_inline u64 hash_pointer(void *ptr) {
|
||||
|
||||
|
||||
|
||||
#define gb_for_array(index_, array_) for (isize index_ = 0; index_ < gb_array_count(array_); index_++)
|
||||
|
||||
|
||||
// Doubly Linked Lists
|
||||
|
||||
#define DLIST_SET(curr_element, next_element) do { \
|
||||
|
||||
@@ -179,7 +179,7 @@ ExactValue exact_unary_operator_value(Token op, ExactValue v, i32 precision) {
|
||||
}
|
||||
|
||||
failure:
|
||||
GB_PANIC("Invalid unary operation, %s", token_kind_to_string(op.kind));
|
||||
GB_PANIC("Invalid unary operation, %.*s", LIT(token_strings[op.kind]));
|
||||
|
||||
ExactValue error_value = {};
|
||||
return error_value;
|
||||
|
||||
137
src/gb/gb.h
137
src/gb/gb.h
@@ -1,4 +1,4 @@
|
||||
/* gb.h - v0.26b - Ginger Bill's C Helper Library - public domain
|
||||
/* gb.h - v0.26c - Ginger Bill's C Helper Library - public domain
|
||||
- no warranty implied; use at your own risk
|
||||
|
||||
This is a single header file with a bunch of useful stuff
|
||||
@@ -58,6 +58,7 @@ TODOS
|
||||
- More date & time functions
|
||||
|
||||
VERSION HISTORY
|
||||
0.26c - gb_str_to_f* fix
|
||||
0.26b - Minor fixes
|
||||
0.26a - gbString Fix
|
||||
0.26 - Default allocator flags and generic hash table
|
||||
@@ -6223,90 +6224,65 @@ gb_inline void gb_u64_to_str(u64 value, char *string, i32 base) {
|
||||
}
|
||||
|
||||
gb_inline f32 gb_str_to_f32(char const *str, char **end_ptr) {
|
||||
f32 result = 0.0f;
|
||||
isize signed_exponent = 0;
|
||||
char c;
|
||||
for (c = *str; c != '\0' && gb_char_is_digit(c); c = *str++)
|
||||
result = result*10.0f + (c-'0');
|
||||
|
||||
if (c == '.') {
|
||||
for (c = *str++; c != '\0' && gb_char_is_digit(c); c = *str++) {
|
||||
result = result*1.0f + (c-'0');
|
||||
signed_exponent--;
|
||||
}
|
||||
}
|
||||
|
||||
if (c == 'e' || c == 'E') {
|
||||
i64 sign = +1, i = 0;
|
||||
c = *str++;
|
||||
if (c == '+') {
|
||||
c = *str++;
|
||||
} else if (c == '-') {
|
||||
c = *str++;
|
||||
sign = -1;
|
||||
}
|
||||
while (gb_char_is_digit(c)) {
|
||||
i = i*10 + (c-'0');
|
||||
c = *str++;
|
||||
}
|
||||
signed_exponent += i*sign;
|
||||
}
|
||||
|
||||
while (signed_exponent > 0) {
|
||||
result *= 10.0f;
|
||||
signed_exponent--;
|
||||
}
|
||||
while (signed_exponent < 0) {
|
||||
result *= 0.1f;
|
||||
signed_exponent++;
|
||||
}
|
||||
|
||||
if (end_ptr) *end_ptr = cast(char *)str;
|
||||
|
||||
return result;
|
||||
f64 f = gb_str_to_f64(str, end_ptr);
|
||||
f32 r = cast(f32)f;
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
|
||||
gb_inline f64 gb_str_to_f64(char const *str, char **end_ptr) {
|
||||
f64 result = 0.0;
|
||||
isize signed_exponent = 0;
|
||||
char c;
|
||||
for (c = *str; c != '\0' && gb_char_is_digit(c); c = *str++)
|
||||
result = result*10.0 + (c-'0');
|
||||
f64 result, value, sign, scale;
|
||||
i32 frac;
|
||||
|
||||
while (gb_char_is_space(*str)) {
|
||||
str++;
|
||||
}
|
||||
|
||||
if (c == '.') {
|
||||
for (c = *str; c != '\0' && gb_char_is_digit(c); c = *str++) {
|
||||
result = result*1.0 + (c-'0');
|
||||
signed_exponent--;
|
||||
sign = 1.0;
|
||||
if (*str == '-') {
|
||||
sign = -1.0;
|
||||
str++;
|
||||
} else if (*str == '+') {
|
||||
str++;
|
||||
}
|
||||
|
||||
for (value = 0.0; gb_char_is_digit(*str); str++) {
|
||||
value = value * 10.0 + (*str-'0');
|
||||
}
|
||||
|
||||
if (*str == '.') {
|
||||
f64 pow10 = 10.0;
|
||||
str++;
|
||||
while (gb_char_is_digit(*str)) {
|
||||
value += (*str-'0') / pow10;
|
||||
pow10 *= 10.0;
|
||||
str++;
|
||||
}
|
||||
}
|
||||
|
||||
if (c == 'e' || c == 'E') {
|
||||
i64 sign = +1, i = 0;
|
||||
c = *str++;
|
||||
if (c == '+') {
|
||||
c = *str++;
|
||||
} else if (c == '-') {
|
||||
c = *str++;
|
||||
sign = -1;
|
||||
frac = 0;
|
||||
scale = 1.0;
|
||||
if ((*str == 'e') || (*str == 'E')) {
|
||||
u32 exp;
|
||||
|
||||
str++;
|
||||
if (*str == '-') {
|
||||
frac = 1;
|
||||
str++;
|
||||
} else if (*str == '+') {
|
||||
str++;
|
||||
}
|
||||
while (gb_char_is_digit(c)) {
|
||||
i = i*10 + (c-'0');
|
||||
c = *str++;
|
||||
|
||||
for (exp = 0; gb_char_is_digit(*str); str++) {
|
||||
exp = exp * 10 + (*str-'0');
|
||||
}
|
||||
signed_exponent += i*sign;
|
||||
if (exp > 308) exp = 308;
|
||||
|
||||
while (exp >= 50) { scale *= 1e50; exp -= 50; }
|
||||
while (exp >= 8) { scale *= 1e8; exp -= 8; }
|
||||
while (exp > 0) { scale *= 10.0; exp -= 1; }
|
||||
}
|
||||
|
||||
while (signed_exponent > 0) {
|
||||
result *= 10.0;
|
||||
signed_exponent--;
|
||||
}
|
||||
while (signed_exponent < 0) {
|
||||
result *= 0.1;
|
||||
signed_exponent++;
|
||||
}
|
||||
result = sign * (frac ? (value / scale) : (value * scale));
|
||||
|
||||
if (end_ptr) *end_ptr = cast(char *)str;
|
||||
|
||||
@@ -8006,13 +7982,13 @@ gb_no_inline isize gb_snprintf_va(char *text, isize max_len, char const *fmt, va
|
||||
|
||||
if (*fmt == '%') {
|
||||
do {
|
||||
switch (*fmt++) {
|
||||
case '-': info.flags |= gbFmt_Minus; fmt++; break;
|
||||
case '+': info.flags |= gbFmt_Plus; fmt++; break;
|
||||
case '#': info.flags |= gbFmt_Alt; fmt++; break;
|
||||
case ' ': info.flags |= gbFmt_Space; fmt++; break;
|
||||
case '0': info.flags |= gbFmt_Zero; fmt++; break;
|
||||
default: info.flags |= gbFmt_Done; break;
|
||||
switch (*++fmt) {
|
||||
case '-': info.flags |= gbFmt_Minus; break;
|
||||
case '+': info.flags |= gbFmt_Plus; break;
|
||||
case '#': info.flags |= gbFmt_Alt; break;
|
||||
case ' ': info.flags |= gbFmt_Space; break;
|
||||
case '0': info.flags |= gbFmt_Zero; break;
|
||||
default: info.flags |= gbFmt_Done; break;
|
||||
}
|
||||
} while (!(info.flags & gbFmt_Done));
|
||||
}
|
||||
@@ -8043,6 +8019,7 @@ gb_no_inline isize gb_snprintf_va(char *text, isize max_len, char const *fmt, va
|
||||
info.flags &= ~gbFmt_Zero;
|
||||
}
|
||||
|
||||
|
||||
switch (*fmt++) {
|
||||
case 'h':
|
||||
if (*fmt == 'h') { // hh => char
|
||||
|
||||
13
src/main.cpp
13
src/main.cpp
@@ -3,7 +3,7 @@
|
||||
#include "parser.cpp"
|
||||
#include "printer.cpp"
|
||||
#include "checker/checker.cpp"
|
||||
// #include "codegen/codegen.cpp"
|
||||
#include "codegen/codegen.cpp"
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (argc < 2) {
|
||||
@@ -29,14 +29,13 @@ int main(int argc, char **argv) {
|
||||
defer (destroy_checker(&checker));
|
||||
|
||||
check_parsed_files(&checker);
|
||||
#if 0
|
||||
Codegen codegen = {};
|
||||
if (init_codegen(&codegen, &checker)) {
|
||||
defer (destroy_codegen(&codegen));
|
||||
|
||||
generate_code(&codegen, file_node);
|
||||
ssaGen ssa = {};
|
||||
if (ssa_gen_init(&ssa, &checker)) {
|
||||
defer (ssa_gen_destroy(&ssa));
|
||||
|
||||
ssa_gen_code(&ssa);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,8 +35,7 @@ struct AstFile {
|
||||
AstScope *curr_scope;
|
||||
isize scope_level;
|
||||
|
||||
isize error_count;
|
||||
TokenPos error_prev_pos;
|
||||
ErrorCollector error_collector;
|
||||
|
||||
// NOTE(bill): Error recovery
|
||||
#define PARSER_MAX_FIX_COUNT 6
|
||||
@@ -247,10 +246,10 @@ struct AstNode {
|
||||
// TODO(bill): Unify Procedure Declarations and Literals
|
||||
struct {
|
||||
DeclarationKind kind;
|
||||
AstNode *name; // AstNode_Identifier
|
||||
AstNode *procedure_type; // AstNode_ProcedureType
|
||||
AstNode *body; // AstNode_BlockStatement
|
||||
AstNode *tag_list; // AstNode_TagExpression
|
||||
AstNode *name; // AstNode_Identifier
|
||||
AstNode *type; // AstNode_ProcedureType
|
||||
AstNode *body; // AstNode_BlockStatement
|
||||
AstNode *tag_list; // AstNode_TagExpression
|
||||
isize tag_count;
|
||||
} procedure_declaration;
|
||||
struct {
|
||||
@@ -459,10 +458,10 @@ AstEntity *ast_scope_insert(AstScope *scope, AstEntity entity) {
|
||||
#define ast_file_err(f, token, fmt, ...) ast_file_err_(f, __FUNCTION__, token, fmt, ##__VA_ARGS__)
|
||||
void ast_file_err_(AstFile *file, char *function, Token token, char *fmt, ...) {
|
||||
// NOTE(bill): Duplicate error, skip it
|
||||
if (!token_pos_are_equal(file->error_prev_pos, token.pos)) {
|
||||
if (!token_pos_are_equal(file->error_collector.prev, token.pos)) {
|
||||
va_list va;
|
||||
|
||||
file->error_prev_pos = token.pos;
|
||||
file->error_collector.prev = token.pos;
|
||||
|
||||
#if 0
|
||||
gb_printf_err("%s()\n", function);
|
||||
@@ -473,7 +472,7 @@ void ast_file_err_(AstFile *file, char *function, Token token, char *fmt, ...) {
|
||||
gb_bprintf_va(fmt, va));
|
||||
va_end(va);
|
||||
}
|
||||
file->error_count++;
|
||||
file->error_collector.count++;
|
||||
}
|
||||
|
||||
|
||||
@@ -760,7 +759,7 @@ gb_inline AstNode *make_procedure_declaration(AstFile *f, DeclarationKind kind,
|
||||
AstNode *result = make_node(f, AstNode_ProcedureDeclaration);
|
||||
result->procedure_declaration.kind = kind;
|
||||
result->procedure_declaration.name = name;
|
||||
result->procedure_declaration.procedure_type = procedure_type;
|
||||
result->procedure_declaration.type = procedure_type;
|
||||
result->procedure_declaration.body = body;
|
||||
result->procedure_declaration.tag_list = tag_list;
|
||||
result->procedure_declaration.tag_count = tag_count;
|
||||
@@ -828,9 +827,9 @@ gb_inline b32 next_token(AstFile *f) {
|
||||
gb_inline Token expect_token(AstFile *f, TokenKind kind) {
|
||||
Token prev = f->cursor[0];
|
||||
if (prev.kind != kind) {
|
||||
ast_file_err(f, f->cursor[0], "Expected `%s`, got `%s`",
|
||||
token_kind_to_string(kind),
|
||||
token_kind_to_string(prev.kind));
|
||||
ast_file_err(f, f->cursor[0], "Expected `%.*s`, got `%.*s`",
|
||||
LIT(token_strings[kind]),
|
||||
LIT(token_strings[prev.kind]));
|
||||
}
|
||||
next_token(f);
|
||||
return prev;
|
||||
@@ -839,8 +838,8 @@ gb_inline Token expect_token(AstFile *f, TokenKind kind) {
|
||||
gb_inline Token expect_operator(AstFile *f) {
|
||||
Token prev = f->cursor[0];
|
||||
if (!gb_is_between(prev.kind, Token__OperatorBegin+1, Token__OperatorEnd-1)) {
|
||||
ast_file_err(f, f->cursor[0], "Expected an operator, got `%s`",
|
||||
token_kind_to_string(prev.kind));
|
||||
ast_file_err(f, f->cursor[0], "Expected an operator, got `%.*s`",
|
||||
LIT(token_strings[prev.kind]));
|
||||
}
|
||||
next_token(f);
|
||||
return prev;
|
||||
@@ -849,8 +848,8 @@ gb_inline Token expect_operator(AstFile *f) {
|
||||
gb_inline Token expect_keyword(AstFile *f) {
|
||||
Token prev = f->cursor[0];
|
||||
if (!gb_is_between(prev.kind, Token__KeywordBegin+1, Token__KeywordEnd-1)) {
|
||||
ast_file_err(f, f->cursor[0], "Expected a keyword, got `%s`",
|
||||
token_kind_to_string(prev.kind));
|
||||
ast_file_err(f, f->cursor[0], "Expected a keyword, got `%.*s`",
|
||||
LIT(token_strings[prev.kind]));
|
||||
}
|
||||
next_token(f);
|
||||
return prev;
|
||||
@@ -1914,7 +1913,9 @@ AstNode *parse_statement(AstFile *f) {
|
||||
case Token_Not:
|
||||
s = parse_simple_statement(f);
|
||||
if (s->kind != AstNode_ProcedureDeclaration && !allow_token(f, Token_Semicolon)) {
|
||||
ast_file_err(f, f->cursor[0], "Expected `;` after statement, got `%s`", token_kind_to_string(f->cursor[0].kind));
|
||||
ast_file_err(f, f->cursor[0],
|
||||
"Expected `;` after statement, got `%.*s`",
|
||||
LIT(token_strings[f->cursor[0].kind]));
|
||||
}
|
||||
return s;
|
||||
|
||||
@@ -1948,7 +1949,9 @@ AstNode *parse_statement(AstFile *f) {
|
||||
|
||||
}
|
||||
|
||||
ast_file_err(f, token, "Expected a statement, got `%s`", token_kind_to_string(token.kind));
|
||||
ast_file_err(f, token,
|
||||
"Expected a statement, got `%.*s`",
|
||||
LIT(token_strings[token.kind]));
|
||||
fix_advance_to_next_statement(f);
|
||||
return make_bad_statement(f, token, f->cursor[0]);
|
||||
}
|
||||
@@ -2031,11 +2034,11 @@ b32 init_parser(Parser *p) {
|
||||
|
||||
void destroy_parser(Parser *p) {
|
||||
// TODO(bill): Fix memory leak
|
||||
for (isize i = 0; i < gb_array_count(p->files); i++) {
|
||||
gb_for_array(i, p->files) {
|
||||
destroy_ast_file(&p->files[i]);
|
||||
}
|
||||
#if 1
|
||||
for (isize i = 0; i < gb_array_count(p->imports); i++) {
|
||||
gb_for_array(i, p->imports) {
|
||||
// gb_free(gb_heap_allocator(), p->imports[i].text);
|
||||
}
|
||||
#endif
|
||||
@@ -2045,7 +2048,7 @@ void destroy_parser(Parser *p) {
|
||||
|
||||
// NOTE(bill): Returns true if it's added
|
||||
b32 try_add_import_path(Parser *p, String import_file) {
|
||||
for (isize i = 0; i < gb_array_count(p->imports); i++) {
|
||||
gb_for_array(i, p->imports) {
|
||||
String import = p->imports[i];
|
||||
if (are_strings_equal(import, import_file)) {
|
||||
return false;
|
||||
@@ -2161,7 +2164,7 @@ ParseFileError parse_files(Parser *p, char *init_filename) {
|
||||
String init_fullpath = make_string(fullpath_str);
|
||||
gb_array_append(p->imports, init_fullpath);
|
||||
|
||||
for (isize i = 0; i < gb_array_count(p->imports); i++) {
|
||||
gb_for_array(i, p->imports) {
|
||||
String import_path = p->imports[i];
|
||||
AstFile file = {};
|
||||
ParseFileError err = init_ast_file(&file, import_path);
|
||||
|
||||
@@ -150,7 +150,7 @@ void print_ast(AstNode *node, isize indent) {
|
||||
gb_printf("(decl:proc,mutable)\n");
|
||||
else if (node->procedure_declaration.kind == Declaration_Immutable)
|
||||
gb_printf("(decl:proc,immutable)\n");
|
||||
print_ast(node->procedure_declaration.procedure_type, indent+1);
|
||||
print_ast(node->procedure_declaration.type, indent+1);
|
||||
print_ast(node->procedure_declaration.body, indent+1);
|
||||
print_ast(node->procedure_declaration.tag_list, indent+1);
|
||||
break;
|
||||
@@ -162,6 +162,14 @@ void print_ast(AstNode *node, isize indent) {
|
||||
print_ast(node->type_declaration.type_expression, indent+1);
|
||||
break;
|
||||
|
||||
case AstNode_AliasDeclaration:
|
||||
print_indent(indent);
|
||||
gb_printf("(alias)\n");
|
||||
print_ast(node->alias_declaration.name, indent+1);
|
||||
print_ast(node->alias_declaration.type_expression, indent+1);
|
||||
break;
|
||||
|
||||
|
||||
case AstNode_ProcedureType:
|
||||
print_indent(indent);
|
||||
gb_printf("(type:proc)(%td -> %td)\n", node->procedure_type.param_count, node->procedure_type.result_count);
|
||||
|
||||
@@ -25,189 +25,107 @@ b32 rune_is_whitespace(Rune r) {
|
||||
return false;
|
||||
}
|
||||
|
||||
typedef enum TokenKind TokenKind;
|
||||
#define TOKEN_KINDS \
|
||||
TOKEN_KIND(Invalid, "Invalid"), \
|
||||
TOKEN_KIND(EOF, "EOF"), \
|
||||
\
|
||||
TOKEN_KIND(_LiteralBegin, "_LiteralBegin"), \
|
||||
TOKEN_KIND(Identifier, "Identifier"), \
|
||||
TOKEN_KIND(Integer, "Integer"), \
|
||||
TOKEN_KIND(Float, "Float"), \
|
||||
TOKEN_KIND(Rune, "Rune"), \
|
||||
TOKEN_KIND(String, "String"), \
|
||||
TOKEN_KIND(_LiteralEnd, "_LiteralEnd"), \
|
||||
\
|
||||
TOKEN_KIND(_OperatorBegin, "_OperatorBegin"), \
|
||||
TOKEN_KIND(Eq, "="), \
|
||||
TOKEN_KIND(Not, "!"), \
|
||||
TOKEN_KIND(Hash, "#"), \
|
||||
TOKEN_KIND(At, "@"), \
|
||||
TOKEN_KIND(Pointer, "^"), \
|
||||
TOKEN_KIND(Add, "+"), \
|
||||
TOKEN_KIND(Sub, "-"), \
|
||||
TOKEN_KIND(Mul, "*"), \
|
||||
TOKEN_KIND(Quo, "/"), \
|
||||
TOKEN_KIND(Mod, "%"), \
|
||||
TOKEN_KIND(AddEq, "+="), \
|
||||
TOKEN_KIND(SubEq, "-="), \
|
||||
TOKEN_KIND(MulEq, "*="), \
|
||||
TOKEN_KIND(QuoEq, "/="), \
|
||||
TOKEN_KIND(ModEq, "%="), \
|
||||
TOKEN_KIND(And, "&"), \
|
||||
TOKEN_KIND(Or, "|"), \
|
||||
TOKEN_KIND(Xor, "~"), \
|
||||
TOKEN_KIND(AndNot, "&~"), \
|
||||
TOKEN_KIND(AndEq, "&="), \
|
||||
TOKEN_KIND(OrEq, "|="), \
|
||||
TOKEN_KIND(XorEq, "~="), \
|
||||
TOKEN_KIND(AndNotEq, "&~"), \
|
||||
TOKEN_KIND(Increment, "++"), \
|
||||
TOKEN_KIND(Decrement, "--"), \
|
||||
TOKEN_KIND(ArrowRight, "->"), \
|
||||
TOKEN_KIND(ArrowLeft, "<-"), \
|
||||
TOKEN_KIND(CmpAnd, "&&"), \
|
||||
TOKEN_KIND(CmpOr, "||"), \
|
||||
\
|
||||
TOKEN_KIND(_ComparisonBegin, "_ComparisonBegin"), \
|
||||
TOKEN_KIND(CmpEq, "=="), \
|
||||
TOKEN_KIND(Lt, "<"), \
|
||||
TOKEN_KIND(Gt, ">"), \
|
||||
TOKEN_KIND(NotEq, "!="), \
|
||||
TOKEN_KIND(LtEq, "<="), \
|
||||
TOKEN_KIND(GtEq, ">="), \
|
||||
TOKEN_KIND(_ComparisonEnd, "_ComparisonEnd"), \
|
||||
\
|
||||
TOKEN_KIND(CmpAndEq, "&&="), \
|
||||
TOKEN_KIND(CmpOrEq, "||="), \
|
||||
TOKEN_KIND(OpenParen, "("), \
|
||||
TOKEN_KIND(CloseParen, ")"), \
|
||||
TOKEN_KIND(OpenBracket, "["), \
|
||||
TOKEN_KIND(CloseBracket, "]"), \
|
||||
TOKEN_KIND(OpenBrace, "{"), \
|
||||
TOKEN_KIND(CloseBrace, "}"), \
|
||||
TOKEN_KIND(Colon, ":"), \
|
||||
TOKEN_KIND(Semicolon, ";"), \
|
||||
TOKEN_KIND(Period, "."), \
|
||||
TOKEN_KIND(Comma, ","), \
|
||||
TOKEN_KIND(Ellipsis, "..."), \
|
||||
TOKEN_KIND(_OperatorEnd, "_OperatorEnd"), \
|
||||
\
|
||||
TOKEN_KIND(_KeywordBegin, "_KeywordBegin"), \
|
||||
TOKEN_KIND(type, "type"), \
|
||||
TOKEN_KIND(alias, "alias"), \
|
||||
TOKEN_KIND(proc, "proc"), \
|
||||
TOKEN_KIND(match, "match"), \
|
||||
TOKEN_KIND(break, "break"), \
|
||||
TOKEN_KIND(continue, "continue"), \
|
||||
TOKEN_KIND(fallthrough, "fallthrough"), \
|
||||
TOKEN_KIND(case, "case"), \
|
||||
TOKEN_KIND(if, "if"), \
|
||||
TOKEN_KIND(else, "else"), \
|
||||
TOKEN_KIND(for, "for"), \
|
||||
TOKEN_KIND(defer, "defer"), \
|
||||
TOKEN_KIND(return, "return"), \
|
||||
TOKEN_KIND(import, "import"), \
|
||||
TOKEN_KIND(cast, "cast"), \
|
||||
TOKEN_KIND(struct, "struct"), \
|
||||
TOKEN_KIND(union, "union"), \
|
||||
TOKEN_KIND(enum, "enum"), \
|
||||
TOKEN_KIND(_KeywordEnd, "_KeywordEnd"), \
|
||||
\
|
||||
TOKEN_KIND(Count, ""), \
|
||||
|
||||
|
||||
enum TokenKind {
|
||||
Token_Invalid,
|
||||
Token_EOF,
|
||||
|
||||
Token__LiteralBegin,
|
||||
Token_Identifier,
|
||||
Token_Integer,
|
||||
Token_Float,
|
||||
Token_Rune,
|
||||
Token_String,
|
||||
Token__LiteralEnd,
|
||||
|
||||
Token__OperatorBegin,
|
||||
Token_Eq, // =
|
||||
|
||||
Token_Not, // ! (Unary Boolean)
|
||||
Token_Hash, // #
|
||||
Token_At, // @ // TODO(bill): Remove
|
||||
Token_Pointer, // ^
|
||||
|
||||
Token_Add, // +
|
||||
Token_Sub, // -
|
||||
Token_Mul, // *
|
||||
Token_Quo, // /
|
||||
Token_Mod, // %
|
||||
|
||||
Token_AddEq, // +=
|
||||
Token_SubEq, // -=
|
||||
Token_MulEq, // *=
|
||||
Token_QuoEq, // /=
|
||||
Token_ModEq, // %=
|
||||
|
||||
Token_And, // &
|
||||
Token_Or, // |
|
||||
Token_Xor, // ~
|
||||
Token_AndNot, // &~
|
||||
|
||||
Token_AndEq, // &=
|
||||
Token_OrEq, // |=
|
||||
Token_XorEq, // ~=
|
||||
Token_AndNotEq, // &~=
|
||||
|
||||
Token_Increment, // ++
|
||||
Token_Decrement, // --
|
||||
Token_ArrowRight, // ->
|
||||
Token_ArrowLeft, // <-
|
||||
|
||||
Token_CmpAnd, // &&
|
||||
Token_CmpOr, // ||
|
||||
Token__ComparisonBegin,
|
||||
Token_CmpEq, // ==
|
||||
Token_Lt, // <
|
||||
Token_Gt, // >
|
||||
Token_NotEq, // !=
|
||||
Token_LtEq, // <=
|
||||
Token_GtEq, // >=
|
||||
Token__ComparisonEnd,
|
||||
Token_CmpAndEq, // &&=
|
||||
Token_CmpOrEq, // ||=
|
||||
|
||||
Token_OpenParen, // (
|
||||
Token_CloseParen, // )
|
||||
Token_OpenBracket, // [
|
||||
Token_CloseBracket, // ]
|
||||
Token_OpenBrace, // {
|
||||
Token_CloseBrace, // }
|
||||
|
||||
Token_Colon, // :
|
||||
Token_Semicolon, // ;
|
||||
Token_Period, // .
|
||||
Token_Comma, // ,
|
||||
Token_Ellipsis, // ...
|
||||
Token__OperatorEnd,
|
||||
|
||||
Token__KeywordBegin,
|
||||
Token_type,
|
||||
Token_alias,
|
||||
Token_proc,
|
||||
Token_match, // TODO(bill): switch vs match?
|
||||
Token_break,
|
||||
Token_continue,
|
||||
Token_fallthrough,
|
||||
Token_case,
|
||||
|
||||
Token_if,
|
||||
Token_else,
|
||||
Token_for,
|
||||
Token_defer,
|
||||
Token_return,
|
||||
Token_import,
|
||||
Token_cast,
|
||||
|
||||
Token_struct,
|
||||
Token_union,
|
||||
Token_enum,
|
||||
Token__KeywordEnd,
|
||||
|
||||
Token_Count,
|
||||
#define TOKEN_KIND(e, s) GB_JOIN2(Token_, e)
|
||||
TOKEN_KINDS
|
||||
#undef TOKEN_KIND
|
||||
};
|
||||
|
||||
char const *TOKEN_STRINGS[] = {
|
||||
"Invalid",
|
||||
"EOF",
|
||||
"_LiteralBegin",
|
||||
"Identifier",
|
||||
"Integer",
|
||||
"Float",
|
||||
"Rune",
|
||||
"String",
|
||||
"_LiteralEnd",
|
||||
"_OperatorBegin",
|
||||
"=",
|
||||
"!",
|
||||
"#",
|
||||
"@",
|
||||
"^",
|
||||
"+",
|
||||
"-",
|
||||
"*",
|
||||
"/",
|
||||
"%",
|
||||
"+=",
|
||||
"-=",
|
||||
"*=",
|
||||
"/=",
|
||||
"%=",
|
||||
"&",
|
||||
"|",
|
||||
"~",
|
||||
"&~",
|
||||
"&=",
|
||||
"|=",
|
||||
"~=",
|
||||
"&~=",
|
||||
"++",
|
||||
"--",
|
||||
"->",
|
||||
"<-",
|
||||
"&&",
|
||||
"||",
|
||||
"_ComparisonBegin",
|
||||
"==",
|
||||
"<",
|
||||
">",
|
||||
"!=",
|
||||
"<=",
|
||||
">=",
|
||||
"_ComparisonEnd",
|
||||
"&&=",
|
||||
"||=",
|
||||
"(",
|
||||
")",
|
||||
"[",
|
||||
"]",
|
||||
"{",
|
||||
"}",
|
||||
":",
|
||||
";",
|
||||
".",
|
||||
",",
|
||||
"...",
|
||||
"_OperatorEnd",
|
||||
"_KeywordBegin",
|
||||
"type",
|
||||
"alias",
|
||||
"proc",
|
||||
"match",
|
||||
"break",
|
||||
"continue",
|
||||
"fallthrough",
|
||||
"case",
|
||||
"if",
|
||||
"else",
|
||||
"for",
|
||||
"defer",
|
||||
"return",
|
||||
"import",
|
||||
"cast",
|
||||
"struct",
|
||||
"union",
|
||||
"enum",
|
||||
"_KeywordEnd",
|
||||
String const token_strings[] = {
|
||||
#define TOKEN_KIND(e, s) {cast(u8 *)s, gb_size_of(s)-1}
|
||||
TOKEN_KINDS
|
||||
#undef TOKEN_KIND
|
||||
};
|
||||
|
||||
|
||||
@@ -275,11 +193,6 @@ void warning(Token token, char *fmt, ...) {
|
||||
|
||||
|
||||
|
||||
|
||||
char const *token_kind_to_string(TokenKind kind) {
|
||||
return TOKEN_STRINGS[kind];
|
||||
}
|
||||
|
||||
i32 token_precedence(Token t) {
|
||||
switch (t.kind) {
|
||||
case Token_CmpOr: return 1;
|
||||
@@ -693,34 +606,12 @@ Token tokenizer_get_token(Tokenizer *t) {
|
||||
|
||||
// NOTE(bill): ALL identifiers are > 1
|
||||
if (token.string.len > 1) {
|
||||
#define KWB if (0) {}
|
||||
#define KWT(keyword, token_type) else if ((gb_size_of(keyword)-1) == token.string.len && gb_strncmp((char *)token.string.text, keyword, token.string.len) == 0) token.kind = token_type
|
||||
#define KWE else {}
|
||||
|
||||
KWB
|
||||
KWT("type", Token_type);
|
||||
KWT("alias", Token_alias);
|
||||
KWT("proc", Token_proc);
|
||||
KWT("match", Token_match);
|
||||
KWT("break", Token_break);
|
||||
KWT("continue", Token_continue);
|
||||
KWT("fallthrough", Token_fallthrough);
|
||||
KWT("case", Token_case);
|
||||
KWT("if", Token_if);
|
||||
KWT("else", Token_else);
|
||||
KWT("for", Token_for);
|
||||
KWT("defer", Token_defer);
|
||||
KWT("return", Token_return);
|
||||
KWT("import", Token_import);
|
||||
KWT("cast", Token_cast);
|
||||
KWT("struct", Token_struct);
|
||||
KWT("union", Token_union);
|
||||
KWT("enum", Token_enum);
|
||||
KWE
|
||||
|
||||
#undef KWB
|
||||
#undef KWT
|
||||
#undef KWE
|
||||
for (i32 k = Token__KeywordBegin+1; k < Token__KeywordEnd; k++) {
|
||||
if (are_strings_equal(token.string, token_strings[k])) {
|
||||
token.kind = cast(TokenKind)k;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} else if (gb_is_between(curr_rune, '0', '9')) {
|
||||
|
||||
Reference in New Issue
Block a user