mirror of
https://github.com/Ed94/Odin.git
synced 2026-07-31 03:40:08 +00:00
Tuple support in codegen
This commit is contained in:
@@ -58,6 +58,7 @@ void check_struct_type(Checker *c, Type *struct_type, AstNode *node) {
|
||||
}
|
||||
struct_type->structure.fields = fields;
|
||||
struct_type->structure.field_count = field_count;
|
||||
struct_type->structure.is_packed = st->is_packed;
|
||||
}
|
||||
|
||||
Type *check_get_params(Checker *c, Scope *scope, AstNode *field_list, isize field_count) {
|
||||
|
||||
+25
-15
@@ -103,6 +103,7 @@ struct Type {
|
||||
isize field_count; // == offset_count
|
||||
i64 * offsets;
|
||||
b32 are_offsets_set;
|
||||
b32 is_packed;
|
||||
} structure;
|
||||
struct { Type *elem; } pointer;
|
||||
struct {
|
||||
@@ -563,37 +564,46 @@ i64 type_align_of(BaseTypeSizes s, gbAllocator allocator, Type *t) {
|
||||
} break;
|
||||
|
||||
case Type_Structure: {
|
||||
i64 max = 1;
|
||||
for (isize i = 0; i < t->structure.field_count; i++) {
|
||||
i64 align = type_align_of(s, allocator, t->structure.fields[i]->type);
|
||||
if (max < align)
|
||||
max = align;
|
||||
if (!t->structure.is_packed) {
|
||||
i64 max = 1;
|
||||
for (isize i = 0; i < t->structure.field_count; i++) {
|
||||
i64 align = type_align_of(s, allocator, t->structure.fields[i]->type);
|
||||
if (max < align)
|
||||
max = align;
|
||||
}
|
||||
return max;
|
||||
}
|
||||
return max;
|
||||
} break;
|
||||
}
|
||||
|
||||
return gb_clamp(type_size_of(s, allocator, t), 1, s.max_align);
|
||||
return gb_clamp(next_pow2(type_size_of(s, allocator, t)), 1, s.max_align);
|
||||
}
|
||||
|
||||
i64 *type_set_offsets_of(BaseTypeSizes s, gbAllocator allocator, Entity **fields, isize field_count) {
|
||||
i64 *type_set_offsets_of(BaseTypeSizes s, gbAllocator allocator, Entity **fields, isize field_count, b32 is_packed) {
|
||||
// TODO(bill): use arena allocation
|
||||
i64 *offsets = gb_alloc_array(allocator, i64, field_count);
|
||||
i64 curr_offset = 0;
|
||||
for (isize i = 0; i < field_count; i++) {
|
||||
i64 align = type_align_of(s, allocator, fields[i]->type);
|
||||
curr_offset = align_formula(curr_offset, align);
|
||||
offsets[i] = curr_offset;
|
||||
curr_offset += type_size_of(s, allocator, fields[i]->type);
|
||||
}
|
||||
if (is_packed) {
|
||||
for (isize i = 0; i < field_count; i++) {
|
||||
offsets[i] = curr_offset;
|
||||
curr_offset += type_size_of(s, allocator, fields[i]->type);
|
||||
}
|
||||
|
||||
} else {
|
||||
for (isize i = 0; i < field_count; i++) {
|
||||
i64 align = type_align_of(s, allocator, fields[i]->type);
|
||||
curr_offset = align_formula(curr_offset, align);
|
||||
offsets[i] = curr_offset;
|
||||
curr_offset += type_size_of(s, allocator, fields[i]->type);
|
||||
}
|
||||
}
|
||||
return offsets;
|
||||
}
|
||||
|
||||
b32 type_set_offsets(BaseTypeSizes s, gbAllocator allocator, Type *t) {
|
||||
GB_ASSERT(t->kind == Type_Structure);
|
||||
if (!t->structure.are_offsets_set) {
|
||||
t->structure.offsets = type_set_offsets_of(s, allocator, t->structure.fields, t->structure.field_count);
|
||||
t->structure.offsets = type_set_offsets_of(s, allocator, t->structure.fields, t->structure.field_count, t->structure.is_packed);
|
||||
t->structure.are_offsets_set = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -81,6 +81,8 @@ void ssa_gen_code(ssaGen *s) {
|
||||
ssa_build_proc(v);
|
||||
}
|
||||
|
||||
// m->layout = make_string("e-p:64:64:64");
|
||||
|
||||
ssa_print_llvm_ir(&s->output_file, &s->module);
|
||||
}
|
||||
|
||||
|
||||
@@ -129,6 +129,9 @@ void ssa_print_type(gbFile *f, BaseTypeSizes s, Type *t) {
|
||||
ssa_fprintf(f, "*, i%lld, i%lld}", word_bits, word_bits);
|
||||
break;
|
||||
case Type_Structure:
|
||||
if (t->structure.is_packed) {
|
||||
ssa_fprintf(f, "<");
|
||||
}
|
||||
ssa_fprintf(f, "{");
|
||||
for (isize i = 0; i < t->structure.field_count; i++) {
|
||||
if (i > 0) {
|
||||
@@ -137,6 +140,10 @@ void ssa_print_type(gbFile *f, BaseTypeSizes s, Type *t) {
|
||||
ssa_print_type(f, s, t->structure.fields[i]->type);
|
||||
}
|
||||
ssa_fprintf(f, "}");
|
||||
if (t->structure.is_packed) {
|
||||
ssa_fprintf(f, ">");
|
||||
}
|
||||
|
||||
break;
|
||||
case Type_Pointer:
|
||||
ssa_print_type(f, s, t->pointer.elem);
|
||||
|
||||
+80
-10
@@ -1244,6 +1244,11 @@ ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue
|
||||
GB_PANIC("TODO(bill): ssa_build_single_expr ProcLit");
|
||||
case_end;
|
||||
|
||||
|
||||
case_ast_node(pl, CompoundLit, expr);
|
||||
GB_PANIC("TODO(bill): ssa_build_single_expr CompoundLit");
|
||||
case_end;
|
||||
|
||||
case_ast_node(ce, CastExpr, expr);
|
||||
return ssa_emit_conv(proc, ssa_build_expr(proc, ce->expr), tv->type);
|
||||
case_end;
|
||||
@@ -1305,19 +1310,20 @@ ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue
|
||||
ssaValue *a = ssa_build_expr(proc, arg);
|
||||
Type *at = ssa_value_type(a);
|
||||
if (at->kind == Type_Tuple) {
|
||||
GB_PANIC("TODO(bill): tuple call arguments");
|
||||
ssaValue *tuple = ssa_add_local_generated(proc, at);
|
||||
ssa_emit_store(proc, tuple, a);
|
||||
for (isize i = 0; i < at->tuple.variable_count; i++) {
|
||||
Entity *e = at->tuple.variables[i];
|
||||
ssaValue *index = ssa_make_value_constant(proc->module->allocator, t_i32, make_exact_value_integer(i));
|
||||
ssaValue *v = ssa_emit_struct_gep(proc, tuple, index, e->type);
|
||||
v = ssa_emit_load(proc, v);
|
||||
args[arg_index++] = v;
|
||||
}
|
||||
} else {
|
||||
args[arg_index++] = a;
|
||||
}
|
||||
}
|
||||
|
||||
#if 0
|
||||
for (isize i = 0; i < arg_count; i++) {
|
||||
Entity *e = type->params->tuple.variables[i];
|
||||
args[i] = ssa_emit_conv(proc, args[i], e->type);
|
||||
}
|
||||
#endif
|
||||
|
||||
ssaValue *call = ssa_make_instr_call(proc, value, args, arg_count, tv->type);
|
||||
ssa_value_set_type(call, proc_type_);
|
||||
return ssa_emit(proc, call);
|
||||
@@ -1569,7 +1575,46 @@ void ssa_build_stmt(ssaProcedure *proc, AstNode *node) {
|
||||
}
|
||||
}
|
||||
} else { // Tuple(s)
|
||||
GB_PANIC("TODO(bill): tuple assignment variable declaration");
|
||||
gbArray(ssaLvalue) lvals;
|
||||
gbArray(ssaValue *) inits;
|
||||
gb_array_init_reserve(lvals, gb_heap_allocator(), vd->name_count);
|
||||
gb_array_init_reserve(inits, gb_heap_allocator(), vd->name_count);
|
||||
defer (gb_array_free(lvals));
|
||||
defer (gb_array_free(inits));
|
||||
|
||||
for (AstNode *name = vd->name_list; name != NULL; name = name->next) {
|
||||
ssaLvalue lval = {ssaLvalue_Blank};
|
||||
if (!ssa_is_blank_ident(name)) {
|
||||
ssa_add_local_for_identifier(proc, name);
|
||||
lval = ssa_build_addr(proc, name);
|
||||
}
|
||||
|
||||
gb_array_append(lvals, lval);
|
||||
}
|
||||
|
||||
for (AstNode *value = vd->value_list; value != NULL; value = value->next) {
|
||||
ssaValue *init = ssa_build_expr(proc, value);
|
||||
Type *t = ssa_value_type(init);
|
||||
if (t->kind == Type_Tuple) {
|
||||
ssaValue *tuple = ssa_add_local_generated(proc, t);
|
||||
ssa_emit_store(proc, tuple, init);
|
||||
for (isize i = 0; i < t->tuple.variable_count; i++) {
|
||||
Entity *e = t->tuple.variables[i];
|
||||
ssaValue *index = ssa_make_value_constant(proc->module->allocator, t_i32, make_exact_value_integer(i));
|
||||
ssaValue *v = ssa_emit_struct_gep(proc, tuple, index, e->type);
|
||||
v = ssa_emit_load(proc, v);
|
||||
gb_array_append(inits, v);
|
||||
}
|
||||
} else {
|
||||
gb_array_append(inits, init);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
gb_for_array(i, inits) {
|
||||
ssaValue *v = ssa_emit_conv(proc, inits[i], ssa_lvalue_type(lvals[i]));
|
||||
ssa_lvalue_store(lvals[i], proc, v);
|
||||
}
|
||||
}
|
||||
}
|
||||
case_end;
|
||||
@@ -1624,7 +1669,32 @@ void ssa_build_stmt(ssaProcedure *proc, AstNode *node) {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
GB_PANIC("TODO(bill): tuple assignment");
|
||||
gbArray(ssaValue *) inits;
|
||||
gb_array_init_reserve(inits, gb_heap_allocator(), gb_array_count(lvals));
|
||||
defer (gb_array_free(inits));
|
||||
|
||||
for (AstNode *rhs = as->rhs_list; rhs != NULL; rhs = rhs->next) {
|
||||
ssaValue *init = ssa_build_expr(proc, rhs);
|
||||
Type *t = ssa_value_type(init);
|
||||
// TODO(bill): refactor for code reuse as this is repeated a bit
|
||||
if (t->kind == Type_Tuple) {
|
||||
ssaValue *tuple = ssa_add_local_generated(proc, t);
|
||||
ssa_emit_store(proc, tuple, init);
|
||||
for (isize i = 0; i < t->tuple.variable_count; i++) {
|
||||
Entity *e = t->tuple.variables[i];
|
||||
ssaValue *index = ssa_make_value_constant(proc->module->allocator, t_i32, make_exact_value_integer(i));
|
||||
ssaValue *v = ssa_emit_struct_gep(proc, tuple, index, e->type);
|
||||
v = ssa_emit_load(proc, v);
|
||||
gb_array_append(inits, v);
|
||||
}
|
||||
} else {
|
||||
gb_array_append(inits, init);
|
||||
}
|
||||
}
|
||||
|
||||
gb_for_array(i, inits) {
|
||||
ssa_lvalue_store(lvals[i], proc, inits[i]);
|
||||
}
|
||||
}
|
||||
|
||||
} break;
|
||||
|
||||
+13
-2
@@ -206,6 +206,7 @@ AST_NODE_KIND(_TypeBegin, struct{}) \
|
||||
Token token; \
|
||||
AstNode *field_list; \
|
||||
isize field_count; \
|
||||
b32 is_packed; \
|
||||
}) \
|
||||
AST_NODE_KIND(_TypeEnd, struct{}) \
|
||||
AST_NODE_KIND(Count, struct{})
|
||||
@@ -740,11 +741,12 @@ gb_inline AstNode *make_vector_type(AstFile *f, Token token, AstNode *count, Ast
|
||||
return result;
|
||||
}
|
||||
|
||||
gb_inline AstNode *make_struct_type(AstFile *f, Token token, AstNode *field_list, isize field_count) {
|
||||
gb_inline AstNode *make_struct_type(AstFile *f, Token token, AstNode *field_list, isize field_count, b32 is_packed) {
|
||||
AstNode *result = make_node(f, AstNode_StructType);
|
||||
result->StructType.token = token;
|
||||
result->StructType.field_list = field_list;
|
||||
result->StructType.field_count = field_count;
|
||||
result->StructType.is_packed = is_packed;
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -1491,12 +1493,21 @@ AstNode *parse_identifier_or_type(AstFile *f) {
|
||||
AstNode *params = NULL;
|
||||
isize param_count = 0;
|
||||
AstScope *scope = make_ast_scope(f, NULL); // NOTE(bill): The struct needs its own scope with NO parent
|
||||
b32 is_packed = false;
|
||||
if (allow_token(f, Token_Hash)) {
|
||||
Token tag = expect_token(f, Token_Identifier);
|
||||
if (are_strings_equal(tag.string, make_string("packed"))) {
|
||||
is_packed = true;
|
||||
} else {
|
||||
ast_file_err(f, tag, "Expected a `#packed` tag");
|
||||
}
|
||||
}
|
||||
|
||||
open = expect_token(f, Token_OpenBrace);
|
||||
params = parse_parameter_list(f, scope, ¶m_count);
|
||||
close = expect_token(f, Token_CloseBrace);
|
||||
|
||||
return make_struct_type(f, token, params, param_count);
|
||||
return make_struct_type(f, token, params, param_count, is_packed);
|
||||
}
|
||||
|
||||
case Token_proc:
|
||||
|
||||
Reference in New Issue
Block a user