Implicit Values: context; Fix lvalue selector assignments; Fix offset_of* for using fields.

This commit is contained in:
Ginger Bill
2016-10-10 10:27:50 +01:00
parent 90babbfbf3
commit f5318c46d1
15 changed files with 226 additions and 6908 deletions
-6755
View File
File diff suppressed because it is too large Load Diff
-1
View File
@@ -1,6 +1,5 @@
#import "fmt.odin" #import "fmt.odin"
main :: proc() { main :: proc() {
fmt.println(123)
} }
+5 -9
View File
@@ -34,7 +34,7 @@ Type_Info :: union {
String: struct #ordered {} String: struct #ordered {}
Boolean: struct #ordered {} Boolean: struct #ordered {}
Pointer: struct #ordered { Pointer: struct #ordered {
elem: ^Type_Info elem: ^Type_Info // nil -> rawptr
} }
Maybe: struct #ordered { Maybe: struct #ordered {
elem: ^Type_Info elem: ^Type_Info
@@ -138,10 +138,6 @@ Context :: struct #ordered {
DEFAULT_ALIGNMENT :: align_of({4}f32) DEFAULT_ALIGNMENT :: align_of({4}f32)
current_context :: proc() -> Context { // Copy of context
return __context
}
__check_context :: proc() { __check_context :: proc() {
c := ^__context c := ^__context
@@ -157,27 +153,27 @@ alloc :: proc(size: int) -> rawptr #inline { return alloc_align(size, DEFAULT_AL
alloc_align :: proc(size, alignment: int) -> rawptr #inline { alloc_align :: proc(size, alignment: int) -> rawptr #inline {
__check_context() __check_context()
a := current_context().allocator a := context.allocator
return a.procedure(a.data, Allocator.Mode.ALLOC, size, alignment, nil, 0, 0) return a.procedure(a.data, Allocator.Mode.ALLOC, size, alignment, nil, 0, 0)
} }
free :: proc(ptr: rawptr) #inline { free :: proc(ptr: rawptr) #inline {
__check_context() __check_context()
a := current_context().allocator a := context.allocator
if ptr != nil { if ptr != nil {
a.procedure(a.data, Allocator.Mode.FREE, 0, 0, ptr, 0, 0) a.procedure(a.data, Allocator.Mode.FREE, 0, 0, ptr, 0, 0)
} }
} }
free_all :: proc() #inline { free_all :: proc() #inline {
__check_context() __check_context()
a := current_context().allocator a := context.allocator
a.procedure(a.data, Allocator.Mode.FREE_ALL, 0, 0, nil, 0, 0) a.procedure(a.data, Allocator.Mode.FREE_ALL, 0, 0, nil, 0, 0)
} }
resize :: proc(ptr: rawptr, old_size, new_size: int) -> rawptr #inline { return resize_align(ptr, old_size, new_size, DEFAULT_ALIGNMENT) } resize :: proc(ptr: rawptr, old_size, new_size: int) -> rawptr #inline { return resize_align(ptr, old_size, new_size, DEFAULT_ALIGNMENT) }
resize_align :: proc(ptr: rawptr, old_size, new_size, alignment: int) -> rawptr #inline { resize_align :: proc(ptr: rawptr, old_size, new_size, alignment: int) -> rawptr #inline {
a := current_context().allocator a := context.allocator
return a.procedure(a.data, Allocator.Mode.RESIZE, new_size, alignment, ptr, old_size, 0) return a.procedure(a.data, Allocator.Mode.RESIZE, new_size, alignment, ptr, old_size, 0)
} }
+9 -2
View File
@@ -226,7 +226,14 @@ print_type_to_buffer :: proc(buf: ^[]byte, ti: ^Type_Info) {
case String: print_string_to_buffer(buf, "string") case String: print_string_to_buffer(buf, "string")
case Boolean: print_string_to_buffer(buf, "bool") case Boolean: print_string_to_buffer(buf, "bool")
case Pointer: case Pointer:
print_string_to_buffer(buf, "^") if info.elem == nil {
print_string_to_buffer(buf, "rawptr")
} else {
print_string_to_buffer(buf, "^")
print_type_to_buffer(buf, info.elem)
}
case Maybe:
print_string_to_buffer(buf, "?")
print_type_to_buffer(buf, info.elem) print_type_to_buffer(buf, info.elem)
case Procedure: case Procedure:
print_string_to_buffer(buf, "proc") print_string_to_buffer(buf, "proc")
@@ -415,7 +422,7 @@ print_any_to_buffer :: proc(buf: ^[]byte, arg: any) {
case Maybe: case Maybe:
if arg.data != nil { if arg.data != nil {
// TODO(bill): print maybe
} else { } else {
print_string_to_buffer(buf, "<nil>") print_string_to_buffer(buf, "<nil>")
} }
+1 -1
View File
@@ -131,7 +131,7 @@ init_arena_from_memory :: proc(using a: ^Arena, data: []byte) {
} }
init_arena_from_context :: proc(using a: ^Arena, size: int) { init_arena_from_context :: proc(using a: ^Arena, size: int) {
backing = current_context().allocator backing = context.allocator
memory = new_slice(u8, 0, size) memory = new_slice(u8, 0, size)
temp_count = 0 temp_count = 0
} }
+1 -1
View File
@@ -1,3 +1,3 @@
@echo off @echo off
call devenv odin.sln call devenv.exe odin.sln
+38 -39
View File
@@ -24,7 +24,6 @@ String const addressing_mode_strings[] = {
#undef ADDRESSING_MODE #undef ADDRESSING_MODE
}; };
struct Operand { struct Operand {
AddressingMode mode; AddressingMode mode;
Type *type; Type *type;
@@ -32,7 +31,6 @@ struct Operand {
AstNode *expr; AstNode *expr;
BuiltinProcId builtin_id; BuiltinProcId builtin_id;
}; };
b32 is_operand_nil(Operand *o) { b32 is_operand_nil(Operand *o) {
return o->mode == Addressing_Value && o->type == t_untyped_nil; return o->mode == Addressing_Value && o->type == t_untyped_nil;
} }
@@ -58,33 +56,6 @@ struct DeclInfo {
}; };
void init_declaration_info(DeclInfo *d, Scope *scope) {
d->scope = scope;
map_init(&d->deps, gb_heap_allocator());
}
DeclInfo *make_declaration_info(gbAllocator a, Scope *scope) {
DeclInfo *d = gb_alloc_item(a, DeclInfo);
init_declaration_info(d, scope);
return d;
}
void destroy_declaration_info(DeclInfo *d) {
map_destroy(&d->deps);
}
b32 has_init(DeclInfo *d) {
if (d->init_expr != NULL)
return true;
if (d->proc_decl != NULL) {
ast_node(pd, ProcDecl, d->proc_decl);
if (pd->body != NULL)
return true;
}
return false;
}
struct ExpressionInfo { struct ExpressionInfo {
b32 is_lhs; // Debug info b32 is_lhs; // Debug info
@@ -94,11 +65,7 @@ struct ExpressionInfo {
}; };
ExpressionInfo make_expression_info(b32 is_lhs, AddressingMode mode, Type *type, ExactValue value) { ExpressionInfo make_expression_info(b32 is_lhs, AddressingMode mode, Type *type, ExactValue value) {
ExpressionInfo ei = {}; ExpressionInfo ei = {is_lhs, mode, type, value};
ei.is_lhs = is_lhs;
ei.mode = mode;
ei.type = type;
ei.value = value;
return ei; return ei;
} }
@@ -126,6 +93,7 @@ struct Scope {
b32 is_init; b32 is_init;
AstFile *file; AstFile *file;
}; };
gb_global Scope *universal_scope = NULL;
enum ExprKind { enum ExprKind {
Expr_Expr, Expr_Expr,
@@ -253,12 +221,13 @@ struct Checker {
b32 in_defer; // TODO(bill): Actually handle correctly b32 in_defer; // TODO(bill): Actually handle correctly
}; };
gb_global Scope *universal_scope = NULL;
struct CycleChecker { struct CycleChecker {
Array<Entity *> path; // Entity_TypeName Array<Entity *> path; // Entity_TypeName
}; };
CycleChecker *cycle_checker_add(CycleChecker *cc, Entity *e) { CycleChecker *cycle_checker_add(CycleChecker *cc, Entity *e) {
if (cc == NULL) { if (cc == NULL) {
return NULL; return NULL;
@@ -278,6 +247,36 @@ void cycle_checker_destroy(CycleChecker *cc) {
} }
void init_declaration_info(DeclInfo *d, Scope *scope) {
d->scope = scope;
map_init(&d->deps, gb_heap_allocator());
}
DeclInfo *make_declaration_info(gbAllocator a, Scope *scope) {
DeclInfo *d = gb_alloc_item(a, DeclInfo);
init_declaration_info(d, scope);
return d;
}
void destroy_declaration_info(DeclInfo *d) {
map_destroy(&d->deps);
}
b32 decl_info_has_init(DeclInfo *d) {
if (d->init_expr != NULL)
return true;
if (d->proc_decl != NULL) {
ast_node(pd, ProcDecl, d->proc_decl);
if (pd->body != NULL)
return true;
}
return false;
}
Scope *make_scope(Scope *parent, gbAllocator allocator) { Scope *make_scope(Scope *parent, gbAllocator allocator) {
Scope *s = gb_alloc_item(allocator, Scope); Scope *s = gb_alloc_item(allocator, Scope);
@@ -489,8 +488,6 @@ void add_global_constant(gbAllocator a, String name, Type *type, ExactValue valu
void init_universal_scope(void) { void init_universal_scope(void) {
// NOTE(bill): No need to free these // NOTE(bill): No need to free these
gbAllocator a = gb_heap_allocator(); gbAllocator a = gb_heap_allocator();
@@ -508,7 +505,7 @@ void init_universal_scope(void) {
add_global_constant(a, make_string("true"), t_untyped_bool, make_exact_value_bool(true)); add_global_constant(a, make_string("true"), t_untyped_bool, make_exact_value_bool(true));
add_global_constant(a, make_string("false"), t_untyped_bool, make_exact_value_bool(false)); add_global_constant(a, make_string("false"), t_untyped_bool, make_exact_value_bool(false));
add_global_entity(make_entity_nil(a, NULL, make_token_ident(make_string("nil")), t_untyped_nil)); add_global_entity(make_entity_nil(a, make_string("nil"), t_untyped_nil));
// Builtin Procedures // Builtin Procedures
for (isize i = 0; i < gb_count_of(builtin_procs); i++) { for (isize i = 0; i < gb_count_of(builtin_procs); i++) {
@@ -947,6 +944,8 @@ void init_preload_types(Checker *c) {
} }
t_context = e->type; t_context = e->type;
t_context_ptr = make_type_pointer(c->allocator, t_context); t_context_ptr = make_type_pointer(c->allocator, t_context);
add_global_entity(make_entity_implicit_value(gb_heap_allocator(), make_string("context"), t_context));
} }
} }
+25 -22
View File
@@ -1,5 +1,6 @@
struct Scope; struct Scope;
struct Checker; struct Checker;
struct Type;
enum BuiltinProcId; enum BuiltinProcId;
#define ENTITY_KINDS \ #define ENTITY_KINDS \
@@ -11,6 +12,7 @@ enum BuiltinProcId;
ENTITY_KIND(Builtin), \ ENTITY_KIND(Builtin), \
ENTITY_KIND(ImportName), \ ENTITY_KIND(ImportName), \
ENTITY_KIND(Nil), \ ENTITY_KIND(Nil), \
ENTITY_KIND(ImplicitValue), \
ENTITY_KIND(Count), ENTITY_KIND(Count),
@@ -26,33 +28,27 @@ String const entity_strings[] = {
#undef ENTITY_KIND #undef ENTITY_KIND
}; };
typedef struct Type Type;
struct Entity { struct Entity {
EntityKind kind; EntityKind kind;
Scope * scope; Scope * scope;
Token token; Token token;
Type * type; Type * type;
AstNode * identifier; // Can be NULL AstNode * identifier; // Can be NULL
Entity * using_parent; Entity * using_parent;
AstNode * using_expr; AstNode * using_expr;
union { union {
struct { struct {
ExactValue value; ExactValue value;
} Constant; } Constant;
struct { struct {
b8 visited; // Cycle detection b8 visited; // Cycle detection
b8 used; // Variable is used b8 used; // Variable is used
b8 anonymous; // Variable is an anonymous b8 anonymous; // Variable is an anonymous
b8 is_using; // `using` variable b8 field; // Is Record field
b8 param; // Is procedure parameter
i32 field_index; i32 field_index;
i32 field_src_index; i32 field_src_index;
b8 is_field; // Is struct field
} Variable; } Variable;
struct { struct {
b32 used; b32 used;
@@ -69,8 +65,8 @@ struct Entity {
Scope *scope; Scope *scope;
b32 used; b32 used;
} ImportName; } ImportName;
struct { struct {} Nil;
} Nil; struct {} ImplicitValue;
}; };
}; };
@@ -105,7 +101,7 @@ Entity *make_entity_using_variable(gbAllocator a, Entity *parent, Token token, T
GB_ASSERT(parent != NULL); GB_ASSERT(parent != NULL);
Entity *entity = alloc_entity(a, Entity_Variable, parent->scope, token, type); Entity *entity = alloc_entity(a, Entity_Variable, parent->scope, token, type);
entity->using_parent = parent; entity->using_parent = parent;
entity->Variable.is_using = true; entity->Variable.anonymous = true;
return entity; return entity;
} }
@@ -121,20 +117,20 @@ Entity *make_entity_type_name(gbAllocator a, Scope *scope, Token token, Type *ty
return entity; return entity;
} }
Entity *make_entity_param(gbAllocator a, Scope *scope, Token token, Type *type, b32 is_anonymous) { Entity *make_entity_param(gbAllocator a, Scope *scope, Token token, Type *type, b32 anonymous) {
Entity *entity = make_entity_variable(a, scope, token, type); Entity *entity = make_entity_variable(a, scope, token, type);
entity->Variable.used = true; entity->Variable.used = true;
entity->Variable.anonymous = cast(b8)is_anonymous; entity->Variable.anonymous = cast(b8)anonymous;
entity->Variable.param = true;
return entity; return entity;
} }
Entity *make_entity_field(gbAllocator a, Scope *scope, Token token, Type *type, b32 is_anonymous, i32 field_src_index) { Entity *make_entity_field(gbAllocator a, Scope *scope, Token token, Type *type, b32 anonymous, i32 field_src_index) {
Entity *entity = make_entity_variable(a, scope, token, type); Entity *entity = make_entity_variable(a, scope, token, type);
entity->Variable.field_src_index = field_src_index; entity->Variable.field_src_index = field_src_index;
entity->Variable.field_index = field_src_index; entity->Variable.field_index = field_src_index;
entity->Variable.is_field = true; entity->Variable.field = true;
entity->Variable.anonymous = cast(b8)is_anonymous; entity->Variable.anonymous = cast(b8)anonymous;
entity->Variable.is_using = cast(b8)is_anonymous;
return entity; return entity;
} }
@@ -158,8 +154,15 @@ Entity *make_entity_import_name(gbAllocator a, Scope *scope, Token token, Type *
return entity; return entity;
} }
Entity *make_entity_nil(gbAllocator a, Scope *scope, Token token, Type *type) { Entity *make_entity_nil(gbAllocator a, String name, Type *type) {
Entity *entity = alloc_entity(a, Entity_Nil, scope, token, type); Token token = make_token_ident(name);
Entity *entity = alloc_entity(a, Entity_Nil, NULL, token, type);
return entity;
}
Entity *make_entity_implicit_value(gbAllocator a, String name, Type *type) {
Token token = make_token_ident(name);
Entity *entity = alloc_entity(a, Entity_ImplicitValue, NULL, token, type);
return entity; return entity;
} }
+53 -19
View File
@@ -973,6 +973,10 @@ void check_identifier(Checker *c, Operand *o, AstNode *n, Type *named_type, Cycl
o->mode = Addressing_Value; o->mode = Addressing_Value;
break; break;
case Entity_ImplicitValue:
o->mode = Addressing_Value;
break;
default: default:
compiler_error("Compiler error: Unknown EntityKind"); compiler_error("Compiler error: Unknown EntityKind");
break; break;
@@ -1708,7 +1712,7 @@ String check_down_cast_name(Type *dst_, Type *src_) {
GB_ASSERT(is_type_struct(dst_s) || is_type_raw_union(dst_s)); GB_ASSERT(is_type_struct(dst_s) || is_type_raw_union(dst_s));
for (isize i = 0; i < dst_s->Record.field_count; i++) { for (isize i = 0; i < dst_s->Record.field_count; i++) {
Entity *f = dst_s->Record.fields[i]; Entity *f = dst_s->Record.fields[i];
GB_ASSERT(f->kind == Entity_Variable && f->Variable.is_field); GB_ASSERT(f->kind == Entity_Variable && f->Variable.field);
if (f->Variable.anonymous) { if (f->Variable.anonymous) {
if (are_types_identical(f->type, src_)) { if (are_types_identical(f->type, src_)) {
return f->token.string; return f->token.string;
@@ -2216,6 +2220,8 @@ Entity *check_selector(Checker *c, Operand *operand, AstNode *node) {
b32 check_op_expr = true; b32 check_op_expr = true;
Entity *entity = NULL; Entity *entity = NULL;
Selection sel = {}; // NOTE(bill): Not used if it's an import name
AstNode *op_expr = se->expr; AstNode *op_expr = se->expr;
AstNode *selector = unparen_expr(se->selector); AstNode *selector = unparen_expr(se->selector);
@@ -2267,8 +2273,10 @@ Entity *check_selector(Checker *c, Operand *operand, AstNode *node) {
} }
} }
if (entity == NULL) { if (entity == NULL) {
entity = lookup_field(c->allocator, operand->type, selector->Ident.string, operand->mode == Addressing_Type).entity; sel = lookup_field(c->allocator, operand->type, selector->Ident.string, operand->mode == Addressing_Type);
entity = sel.entity;
} }
if (entity == NULL) { if (entity == NULL) {
gbString op_str = expr_to_string(op_expr); gbString op_str = expr_to_string(op_expr);
@@ -2284,15 +2292,16 @@ Entity *check_selector(Checker *c, Operand *operand, AstNode *node) {
add_entity_use(c, selector, entity); add_entity_use(c, selector, entity);
operand->type = entity->type;
operand->expr = node;
switch (entity->kind) { switch (entity->kind) {
case Entity_Constant: case Entity_Constant:
operand->mode = Addressing_Constant; operand->mode = Addressing_Constant;
operand->value = entity->Constant.value; operand->value = entity->Constant.value;
break; break;
case Entity_Variable: case Entity_Variable:
operand->mode = Addressing_Variable; // TODO(bill): This is the rule I need?
if (sel.indirect || operand->mode != Addressing_Value) {
operand->mode = Addressing_Variable;
}
break; break;
case Entity_TypeName: case Entity_TypeName:
operand->mode = Addressing_Type; operand->mode = Addressing_Type;
@@ -2304,8 +2313,19 @@ Entity *check_selector(Checker *c, Operand *operand, AstNode *node) {
operand->mode = Addressing_Builtin; operand->mode = Addressing_Builtin;
operand->builtin_id = entity->Builtin.id; operand->builtin_id = entity->Builtin.id;
break; break;
// NOTE(bill): These cases should never be hit but are here for sanity reasons
case Entity_Nil:
operand->mode = Addressing_Value;
break;
case Entity_ImplicitValue:
operand->mode = Addressing_Value;
break;
} }
operand->type = entity->type;
operand->expr = node;
return entity; return entity;
error: error:
@@ -2473,10 +2493,6 @@ b32 check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id)
error(ast_node_token(ce->args[0]), "Expected a type for `offset_of`"); error(ast_node_token(ce->args[0]), "Expected a type for `offset_of`");
return false; return false;
} }
if (!is_type_struct(type)) {
error(ast_node_token(ce->args[0]), "Expected a struct type for `offset_of`");
return false;
}
AstNode *field_arg = unparen_expr(ce->args[1]); AstNode *field_arg = unparen_expr(ce->args[1]);
if (field_arg == NULL || if (field_arg == NULL ||
@@ -2484,6 +2500,10 @@ b32 check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id)
error(ast_node_token(field_arg), "Expected an identifier for field argument"); error(ast_node_token(field_arg), "Expected an identifier for field argument");
return false; return false;
} }
if (is_type_array(type) || is_type_vector(type)) {
error(ast_node_token(field_arg), "Invalid type for `offset_of`");
return false;
}
ast_node(arg, Ident, field_arg); ast_node(arg, Ident, field_arg);
@@ -2495,10 +2515,16 @@ b32 check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id)
"`%s` has no field named `%.*s`", type_str, LIT(arg->string)); "`%s` has no field named `%.*s`", type_str, LIT(arg->string));
return false; return false;
} }
if (sel.indirect) {
gbString type_str = type_to_string(bt);
defer (gb_string_free(type_str));
error(ast_node_token(ce->args[0]),
"Field `%.*s` is embedded via a pointer in `%s`", LIT(arg->string), type_str);
return false;
}
operand->mode = Addressing_Constant; operand->mode = Addressing_Constant;
// IMPORTANT TODO(bill): Fix for anonymous fields operand->value = make_exact_value_integer(type_offset_of_from_selection(c->sizes, c->allocator, type, sel));
operand->value = make_exact_value_integer(type_offset_of(c->sizes, c->allocator, type, sel.index[0]));
operand->type = t_int; operand->type = t_int;
} break; } break;
@@ -2523,13 +2549,11 @@ b32 check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id)
type = p->Pointer.elem; type = p->Pointer.elem;
} }
} }
if (is_type_array(type) || is_type_vector(type)) {
if (!is_type_struct(type)) { error(ast_node_token(arg), "Invalid type for `offset_of_val`");
error(ast_node_token(ce->args[0]), "Expected a struct type for `offset_of_val`");
return false; return false;
} }
ast_node(i, Ident, s->selector); ast_node(i, Ident, s->selector);
Selection sel = lookup_field(c->allocator, type, i->string, operand->mode == Addressing_Type); Selection sel = lookup_field(c->allocator, type, i->string, operand->mode == Addressing_Type);
if (sel.entity == NULL) { if (sel.entity == NULL) {
@@ -2538,10 +2562,18 @@ b32 check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id)
"`%s` has no field named `%.*s`", type_str, LIT(i->string)); "`%s` has no field named `%.*s`", type_str, LIT(i->string));
return false; return false;
} }
if (sel.indirect) {
gbString type_str = type_to_string(type);
defer (gb_string_free(type_str));
error(ast_node_token(ce->args[0]),
"Field `%.*s` is embedded via a pointer in `%s`", LIT(i->string), type_str);
return false;
}
operand->mode = Addressing_Constant; operand->mode = Addressing_Constant;
// IMPORTANT TODO(bill): Fix for anonymous fields // IMPORTANT TODO(bill): Fix for anonymous fields
operand->value = make_exact_value_integer(type_offset_of(c->sizes, c->allocator, type, sel.index[0])); operand->value = make_exact_value_integer(type_offset_of_from_selection(c->sizes, c->allocator, type, sel));
operand->type = t_int; operand->type = t_int;
} break; } break;
@@ -3227,7 +3259,7 @@ Entity *find_using_index_expr(Type *t) {
for (isize i = 0; i < t->Record.field_count; i++) { for (isize i = 0; i < t->Record.field_count; i++) {
Entity *f = t->Record.fields[i]; Entity *f = t->Record.fields[i];
if (f->kind == Entity_Variable && if (f->kind == Entity_Variable &&
f->Variable.is_field && f->Variable.anonymous) { f->Variable.field && f->Variable.anonymous) {
if (is_type_indexable(f->type)) { if (is_type_indexable(f->type)) {
return f; return f;
} }
@@ -3632,8 +3664,9 @@ ExprKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *type_hint
if (o->mode == Addressing_Constant) { if (o->mode == Addressing_Constant) {
*max_count = o->value.value_string.len; *max_count = o->value.value_string.len;
} }
if (o->mode != Addressing_Variable) if (o->mode != Addressing_Variable) {
o->mode = Addressing_Value; o->mode = Addressing_Value;
}
o->type = t_u8; o->type = t_u8;
return true; return true;
} }
@@ -3700,8 +3733,9 @@ ExprKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *type_hint
PROF_SCOPED("check__expr_base - SliceExpr"); PROF_SCOPED("check__expr_base - SliceExpr");
check_expr(c, o, se->expr); check_expr(c, o, se->expr);
if (o->mode == Addressing_Invalid) if (o->mode == Addressing_Invalid) {
goto error; goto error;
}
b32 valid = false; b32 valid = false;
i64 max_count = -1; i64 max_count = -1;
+13 -5
View File
@@ -1,5 +1,4 @@
// Statements and Declarations // Statements and Declarations
enum StmtFlag : u32 { enum StmtFlag : u32 {
Stmt_BreakAllowed = GB_BIT(0), Stmt_BreakAllowed = GB_BIT(0),
Stmt_ContinueAllowed = GB_BIT(1), Stmt_ContinueAllowed = GB_BIT(1),
@@ -268,10 +267,10 @@ Type *check_assignment_variable(Checker *c, Operand *op_a, AstNode *lhs) {
} }
switch (op_b.mode) { switch (op_b.mode) {
case Addressing_Variable:
break;
case Addressing_Invalid: case Addressing_Invalid:
return NULL; return NULL;
case Addressing_Variable:
break;
default: { default: {
if (op_b.expr->kind == AstNode_SelectorExpr) { if (op_b.expr->kind == AstNode_SelectorExpr) {
// NOTE(bill): Extra error checks // NOTE(bill): Extra error checks
@@ -282,7 +281,15 @@ Type *check_assignment_variable(Checker *c, Operand *op_a, AstNode *lhs) {
gbString str = expr_to_string(op_b.expr); gbString str = expr_to_string(op_b.expr);
defer (gb_string_free(str)); defer (gb_string_free(str));
error(ast_node_token(op_b.expr), "Cannot assign to `%s`", str); switch (op_b.mode) {
case Addressing_Value:
error(ast_node_token(op_b.expr), "Cannot assign to `%s`", str);
break;
default:
error(ast_node_token(op_b.expr), "Cannot assign to `%s`", str);
break;
}
} break; } break;
} }
@@ -507,8 +514,9 @@ void check_proc_body(Checker *c, Token token, DeclInfo *decl, Type *type, AstNod
for (isize i = 0; i < params->variable_count; i++) { for (isize i = 0; i < params->variable_count; i++) {
Entity *e = params->variables[i]; Entity *e = params->variables[i];
GB_ASSERT(e->kind == Entity_Variable); GB_ASSERT(e->kind == Entity_Variable);
if (!e->Variable.anonymous) if (!e->Variable.anonymous) {
continue; continue;
}
String name = e->token.string; String name = e->token.string;
Type *t = base_type(type_deref(e->type)); Type *t = base_type(type_deref(e->type));
if (is_type_struct(t) || is_type_raw_union(t)) { if (is_type_struct(t) || is_type_raw_union(t)) {
+30 -7
View File
@@ -812,6 +812,8 @@ Selection lookup_field(gbAllocator a, Type *type_, String field_name, b32 is_typ
Type *type = type_deref(type_); Type *type = type_deref(type_);
b32 is_ptr = type != type_; b32 is_ptr = type != type_;
sel.indirect = sel.indirect || is_ptr;
type = base_type(type); type = base_type(type);
if (type->kind == Type_Basic) { if (type->kind == Type_Basic) {
@@ -918,7 +920,6 @@ Selection lookup_field(gbAllocator a, Type *type_, String field_name, b32 is_typ
String str = f->token.string; String str = f->token.string;
if (field_name == str) { if (field_name == str) {
Selection sel = {};
sel.entity = f; sel.entity = f;
selection_add_index(&sel, i); selection_add_index(&sel, i);
return sel; return sel;
@@ -932,7 +933,6 @@ Selection lookup_field(gbAllocator a, Type *type_, String field_name, b32 is_typ
String str = f->token.string; String str = f->token.string;
if (field_name == str) { if (field_name == str) {
Selection sel = {};
sel.entity = f; sel.entity = f;
selection_add_index(&sel, i); selection_add_index(&sel, i);
return sel; return sel;
@@ -955,7 +955,7 @@ Selection lookup_field(gbAllocator a, Type *type_, String field_name, b32 is_typ
} else if (!is_type_enum(type) && !is_type_union(type)) { } else if (!is_type_enum(type) && !is_type_union(type)) {
for (isize i = 0; i < type->Record.field_count; i++) { for (isize i = 0; i < type->Record.field_count; i++) {
Entity *f = type->Record.fields[i]; Entity *f = type->Record.fields[i];
GB_ASSERT(f->kind == Entity_Variable && f->Variable.is_field); GB_ASSERT(f->kind == Entity_Variable && f->Variable.field);
String str = f->token.string; String str = f->token.string;
if (field_name == str) { if (field_name == str) {
selection_add_index(&sel, i); // HACK(bill): Leaky memory selection_add_index(&sel, i); // HACK(bill): Leaky memory
@@ -970,8 +970,9 @@ Selection lookup_field(gbAllocator a, Type *type_, String field_name, b32 is_typ
sel = lookup_field(a, f->type, field_name, is_type, sel); sel = lookup_field(a, f->type, field_name, is_type, sel);
if (sel.entity != NULL) { if (sel.entity != NULL) {
if (is_type_pointer(f->type)) if (is_type_pointer(f->type)) {
sel.indirect = true; sel.indirect = true;
}
return sel; return sel;
} }
sel.index.count = prev_count; sel.index.count = prev_count;
@@ -1199,26 +1200,48 @@ i64 type_size_of(BaseTypeSizes s, gbAllocator allocator, Type *t) {
} }
i64 type_offset_of(BaseTypeSizes s, gbAllocator allocator, Type *t, isize index) { i64 type_offset_of(BaseTypeSizes s, gbAllocator allocator, Type *t, isize index) {
t = base_type(t);
if (t->kind == Type_Record && t->Record.kind == TypeRecord_Struct) { if (t->kind == Type_Record && t->Record.kind == TypeRecord_Struct) {
type_set_offsets(s, allocator, t); type_set_offsets(s, allocator, t);
if (gb_is_between(index, 0, t->Record.field_count-1)) { if (gb_is_between(index, 0, t->Record.field_count-1)) {
return t->Record.struct_offsets[index]; return t->Record.struct_offsets[index];
} }
} else if (t->kind == Type_Basic) {
gb_printf_err("here!!\n");
if (t->Basic.kind == Basic_string) {
switch (index) {
case 0: return 0;
case 1: return s.word_size;
}
} else if (t->Basic.kind == Basic_any) {
switch (index) {
case 0: return 0;
case 1: return s.word_size;
}
}
} else if (t->kind == Type_Slice) {
switch (index) {
case 0: return 0;
case 1: return 1*s.word_size;
case 2: return 2*s.word_size;
}
} }
return 0; return 0;
} }
i64 type_offset_of_from_selection(BaseTypeSizes s, gbAllocator allocator, Type *t, Selection sel) { i64 type_offset_of_from_selection(BaseTypeSizes s, gbAllocator allocator, Type *t, Selection sel) {
GB_ASSERT(sel.indirect == false);
i64 offset = 0; i64 offset = 0;
for_array(i, sel.index) { for_array(i, sel.index) {
isize index = sel.index[i]; isize index = sel.index[i];
t = base_type(t); t = base_type(t);
offset += type_offset_of(s, allocator, t, index);
if (t->kind == Type_Record && t->Record.kind == TypeRecord_Struct) { if (t->kind == Type_Record && t->Record.kind == TypeRecord_Struct) {
type_set_offsets(s, allocator, t);
GB_ASSERT(gb_is_between(index, 0, t->Record.field_count-1));
offset += t->Record.struct_offsets[index];
t = t->Record.fields[index]->type; t = t->Record.fields[index]->type;
} else {
// NOTE(bill): string/any/slices don't have record fields so this case doesn't need to be handled
} }
} }
return offset; return offset;
+1 -1
View File
@@ -450,7 +450,7 @@ void ssa_gen_tree(ssaGen *s) {
Entity *f = t->Record.fields_in_src_order[i]; Entity *f = t->Record.fields_in_src_order[i];
ssaValue *tip = get_type_info_ptr(proc, type_info_data, f->type); ssaValue *tip = get_type_info_ptr(proc, type_info_data, f->type);
i64 foffset = t->Record.struct_offsets[f->Variable.field_index]; i64 foffset = t->Record.struct_offsets[f->Variable.field_index];
GB_ASSERT(f->kind == Entity_Variable && f->Variable.is_field); GB_ASSERT(f->kind == Entity_Variable && f->Variable.field);
isize source_index = f->Variable.field_index; isize source_index = f->Variable.field_index;
ssaValue *field = ssa_emit_ptr_offset(proc, memory, ssa_make_const_int(a, source_index)); ssaValue *field = ssa_emit_ptr_offset(proc, memory, ssa_make_const_int(a, source_index));
+38 -31
View File
@@ -4,7 +4,6 @@ struct ssaBlock;
struct ssaValue; struct ssaValue;
struct ssaInstr; struct ssaInstr;
enum ssaDebugInfoKind { enum ssaDebugInfoKind {
ssaDebugInfo_Invalid, ssaDebugInfo_Invalid,
@@ -389,7 +388,6 @@ ssaAddr ssa_make_addr(ssaValue *addr, AstNode *expr) {
ssaAddr v = {addr, expr, false, NULL}; ssaAddr v = {addr, expr, false, NULL};
return v; return v;
} }
ssaAddr ssa_make_addr_vector(ssaValue *addr, ssaValue *index, AstNode *expr) { ssaAddr ssa_make_addr_vector(ssaValue *addr, ssaValue *index, AstNode *expr) {
ssaAddr v = {addr, expr, true, index}; ssaAddr v = {addr, expr, true, index};
return v; return v;
@@ -1421,7 +1419,6 @@ void ssa_end_procedure_body(ssaProcedure *proc) {
ssa_emit_jump(proc, proc->entry_block); ssa_emit_jump(proc, proc->entry_block);
ssa_optimize_blocks(proc); ssa_optimize_blocks(proc);
#if 0
ssa_build_referrers(proc); ssa_build_referrers(proc);
ssa_build_dom_tree(proc); ssa_build_dom_tree(proc);
@@ -1431,7 +1428,7 @@ void ssa_end_procedure_body(ssaProcedure *proc) {
// [ ] Local stored once? Replace loads with dominating store // [ ] Local stored once? Replace loads with dominating store
// [ ] Convert to phi nodes // [ ] Convert to phi nodes
ssa_opt_mem2reg(proc); ssa_opt_mem2reg(proc);
#endif
// Number registers // Number registers
i32 reg_index = 0; i32 reg_index = 0;
@@ -2275,6 +2272,21 @@ void ssa_slice_bounds_check(ssaProcedure *proc, Token token, ssaValue *low, ssaV
} }
} }
ssaValue *ssa_find_global_variable(ssaProcedure *proc, String name) {
ssaValue *value = *map_get(&proc->module->members, hash_string(name));
return value;
}
ssaValue *ssa_emit_implicit_value(ssaProcedure *proc, Entity *e) {
String name = e->token.string;
ssaValue *g = NULL;
if (name == "context") {
g = ssa_emit_load(proc, ssa_find_global_variable(proc, make_string("__context")));
}
GB_ASSERT(g != NULL);
return g;
}
ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue *tv) { ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue *tv) {
switch (expr->kind) { switch (expr->kind) {
@@ -2292,13 +2304,16 @@ ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue
return NULL; return NULL;
} else if (e->kind == Entity_Nil) { } else if (e->kind == Entity_Nil) {
return ssa_make_value_nil(proc->module->allocator, tv->type); return ssa_make_value_nil(proc->module->allocator, tv->type);
} else if (e->kind == Entity_ImplicitValue) {
return ssa_emit_implicit_value(proc, e);
} }
auto *found = map_get(&proc->module->values, hash_pointer(e)); auto *found = map_get(&proc->module->values, hash_pointer(e));
if (found) { if (found) {
ssaValue *v = *found; ssaValue *v = *found;
if (v->kind == ssaValue_Proc) if (v->kind == ssaValue_Proc) {
return v; return v;
}
return ssa_emit_load(proc, v); return ssa_emit_load(proc, v);
} }
return NULL; return NULL;
@@ -3043,16 +3058,10 @@ ssaValue *ssa_build_expr(ssaProcedure *proc, AstNode *expr) {
return value; return value;
} }
ssaValue *ssa_add_using_variable(ssaProcedure *proc, Entity *e) { ssaValue *ssa_add_using_variable(ssaProcedure *proc, Entity *e) {
GB_ASSERT(e->kind == Entity_Variable && e->Variable.is_using); GB_ASSERT(e->kind == Entity_Variable && e->Variable.anonymous);
String name = e->token.string; String name = e->token.string;
Entity *parent = e->using_parent; Entity *parent = e->using_parent;
ssaValue *p = NULL;
if (parent->kind == Entity_Variable && parent->Variable.is_using) {
p = ssa_add_using_variable(proc, parent);
}
Selection sel = lookup_field(proc->module->allocator, parent->type, name, false); Selection sel = lookup_field(proc->module->allocator, parent->type, name, false);
GB_ASSERT(sel.entity != NULL); GB_ASSERT(sel.entity != NULL);
ssaValue **pv = map_get(&proc->module->values, hash_pointer(parent)); ssaValue **pv = map_get(&proc->module->values, hash_pointer(parent));
@@ -3077,6 +3086,9 @@ ssaAddr ssa_build_addr(ssaProcedure *proc, AstNode *expr) {
} }
Entity *e = entity_of_ident(proc->module->info, expr); Entity *e = entity_of_ident(proc->module->info, expr);
TypeAndValue *tv = map_get(&proc->module->info->types, hash_pointer(expr));
// GB_ASSERT(tv == NULL || tv->mode == Addressing_Variable);
if (e->kind == Entity_Constant) { if (e->kind == Entity_Constant) {
if (base_type(e->type) == t_string) { if (base_type(e->type) == t_string) {
@@ -3098,11 +3110,17 @@ ssaAddr ssa_build_addr(ssaProcedure *proc, AstNode *expr) {
ssaValue **found = map_get(&proc->module->values, hash_pointer(e)); ssaValue **found = map_get(&proc->module->values, hash_pointer(e));
if (found) { if (found) {
v = *found; v = *found;
} else if (e->kind == Entity_Variable && e->Variable.is_using) { } else if (e->kind == Entity_Variable && e->Variable.anonymous) {
v = ssa_add_using_variable(proc, e); v = ssa_add_using_variable(proc, e);
} else if (e->kind == Entity_ImplicitValue) {
ssaValue *g = ssa_emit_implicit_value(proc, e);
// NOTE(bill): Create a copy as it's by value
v = ssa_add_local_generated(proc, ssa_type(g));
ssa_emit_store(proc, v, g);
} }
if (v == NULL) { if (v == NULL) {
GB_PANIC("Unknown value: %s, entity: %p\n", expr_to_string(expr), e); GB_PANIC("Unknown value: %s, entity: %p %.*s\n", expr_to_string(expr), e, LIT(entity_strings[e->kind]));
} }
return ssa_make_addr(v, expr); return ssa_make_addr(v, expr);
@@ -4151,7 +4169,7 @@ void ssa_build_stmt(ssaProcedure *proc, AstNode *node) {
ssa_open_scope(proc); ssa_open_scope(proc);
defer (ssa_close_scope(proc, ssaDeferExit_Default, NULL)); defer (ssa_close_scope(proc, ssaDeferExit_Default, NULL));
ssaValue *context_ptr = *map_get(&proc->module->members, hash_string(make_string("__context"))); ssaValue *context_ptr = ssa_find_global_variable(proc, make_string("__context"));
ssaValue *prev_context = ssa_add_local_generated(proc, t_context); ssaValue *prev_context = ssa_add_local_generated(proc, t_context);
ssa_emit_store(proc, prev_context, ssa_emit_load(proc, context_ptr)); ssa_emit_store(proc, prev_context, ssa_emit_load(proc, context_ptr));
@@ -4170,7 +4188,7 @@ void ssa_build_stmt(ssaProcedure *proc, AstNode *node) {
ssa_open_scope(proc); ssa_open_scope(proc);
defer (ssa_close_scope(proc, ssaDeferExit_Default, NULL)); defer (ssa_close_scope(proc, ssaDeferExit_Default, NULL));
ssaValue *context_ptr = *map_get(&proc->module->members, hash_string(make_string("__context"))); ssaValue *context_ptr = ssa_find_global_variable(proc, make_string("__context"));
ssaValue *prev_context = ssa_add_local_generated(proc, t_context); ssaValue *prev_context = ssa_add_local_generated(proc, t_context);
ssa_emit_store(proc, prev_context, ssa_emit_load(proc, context_ptr)); ssa_emit_store(proc, prev_context, ssa_emit_load(proc, context_ptr));
@@ -4257,11 +4275,6 @@ void ssa_build_proc(ssaValue *value, ssaProcedure *parent) {
b32 ssa_is_instr_jump(ssaValue *v) { b32 ssa_is_instr_jump(ssaValue *v) {
if (v->kind != ssaValue_Instr) { if (v->kind != ssaValue_Instr) {
return false; return false;
@@ -4289,7 +4302,6 @@ Array<ssaValue *> ssa_get_block_phi_nodes(ssaBlock *b) {
void ssa_remove_pred(ssaBlock *b, ssaBlock *p) { void ssa_remove_pred(ssaBlock *b, ssaBlock *p) {
auto phis = ssa_get_block_phi_nodes(b); auto phis = ssa_get_block_phi_nodes(b);
isize i = 0; isize i = 0;
for_array(j, b->preds) { for_array(j, b->preds) {
ssaBlock *pred = b->preds[j]; ssaBlock *pred = b->preds[j];
@@ -4302,7 +4314,6 @@ void ssa_remove_pred(ssaBlock *b, ssaBlock *p) {
i++; i++;
} }
} }
b->preds.count = i; b->preds.count = i;
for_array(k, phis) { for_array(k, phis) {
auto *phi = &phis[k]->Instr.Phi; auto *phi = &phis[k]->Instr.Phi;
@@ -4322,7 +4333,6 @@ void ssa_remove_dead_blocks(ssaProcedure *proc) {
b->index = j; b->index = j;
proc->blocks[j++] = b; proc->blocks[j++] = b;
} }
proc->blocks.count = j; proc->blocks.count = j;
} }
@@ -4470,8 +4480,7 @@ void ssa_lt_link(ssaLTState *lt, ssaBlock *p, ssaBlock *q) {
i32 ssa_lt_depth_first_search(ssaLTState *lt, ssaBlock *p, i32 i, ssaBlock **preorder) { i32 ssa_lt_depth_first_search(ssaLTState *lt, ssaBlock *p, i32 i, ssaBlock **preorder) {
preorder[i] = p; preorder[i] = p;
p->dom.pre = i; p->dom.pre = i++;
i++;
lt->sdom[p->index] = p; lt->sdom[p->index] = p;
ssa_lt_link(lt, NULL, p); ssa_lt_link(lt, NULL, p);
for_array(index, p->succs) { for_array(index, p->succs) {
@@ -4497,8 +4506,7 @@ ssaBlock *ssa_lt_eval(ssaLTState *lt, ssaBlock *v) {
} }
void ssa_number_dom_tree(ssaBlock *v, i32 pre, i32 post, i32 *pre_out, i32 *post_out) { void ssa_number_dom_tree(ssaBlock *v, i32 pre, i32 post, i32 *pre_out, i32 *post_out) {
v->dom.pre = pre; v->dom.pre = pre++;
pre++;
for_array(i, v->dom.children) { for_array(i, v->dom.children) {
ssaBlock *child = v->dom.children[i]; ssaBlock *child = v->dom.children[i];
i32 new_pre = 0, new_post = 0; i32 new_pre = 0, new_post = 0;
@@ -4506,8 +4514,7 @@ void ssa_number_dom_tree(ssaBlock *v, i32 pre, i32 post, i32 *pre_out, i32 *post
pre = new_pre; pre = new_pre;
post = new_post; post = new_post;
} }
v->dom.post = post; v->dom.post = post++;
post++;
*pre_out = pre; *pre_out = pre;
*post_out = post; *post_out = post;
} }
@@ -4606,5 +4613,5 @@ void ssa_build_dom_tree(ssaProcedure *proc) {
void ssa_opt_mem2reg(ssaProcedure *proc) { void ssa_opt_mem2reg(ssaProcedure *proc) {
// TODO(bill):
} }
+1 -4
View File
@@ -7,9 +7,8 @@
#include "checker/checker.cpp" #include "checker/checker.cpp"
#include "codegen/codegen.cpp" #include "codegen/codegen.cpp"
// NOTE(bill): `name` is used in debugging and profiling modes
i32 win32_exec_command_line_app(char *name, char *fmt, ...) { i32 win32_exec_command_line_app(char *name, char *fmt, ...) {
// PROF_SCOPED_STR(name);
STARTUPINFOW start_info = {gb_size_of(STARTUPINFOW)}; STARTUPINFOW start_info = {gb_size_of(STARTUPINFOW)};
PROCESS_INFORMATION pi = {}; PROCESS_INFORMATION pi = {};
char cmd_line[2048] = {}; char cmd_line[2048] = {};
@@ -88,8 +87,6 @@ ArchData make_arch_data(ArchKind kind) {
return data; return data;
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
if (argc < 2) { if (argc < 2) {
gb_printf_err("using: %s [run] <filename> \n", argv[0]); gb_printf_err("using: %s [run] <filename> \n", argv[0]);
+11 -11
View File
@@ -2257,17 +2257,17 @@ AstNode *parse_decl(AstFile *f, AstNodeArray names) {
AstNodeArray values = {}; AstNodeArray values = {};
AstNode *type = NULL; AstNode *type = NULL;
// for_array(i, names) { for_array(i, names) {
// AstNode *name = names[i]; AstNode *name = names[i];
// if (name->kind == AstNode_Ident) { if (name->kind == AstNode_Ident) {
// String n = name->Ident.string; String n = name->Ident.string;
// // NOTE(bill): Check for reserved identifiers // NOTE(bill): Check for reserved identifiers
// if (n == "context") { if (n == "context") {
// syntax_error(ast_node_token(name), "`context` is a reserved identifier"); syntax_error(ast_node_token(name), "`context` is a reserved identifier");
// break; break;
// } }
// } }
// } }
if (allow_token(f, Token_Colon)) { if (allow_token(f, Token_Colon)) {
if (!allow_token(f, Token_type)) { if (!allow_token(f, Token_type)) {