mirror of
https://github.com/Ed94/Odin.git
synced 2026-06-20 04:35:00 -07:00
Minor refactor and basic library
This commit is contained in:
+3
-243
@@ -20,7 +20,9 @@ b32 ssa_gen_init(ssaGen *s, Checker *c) {
|
||||
|
||||
ssa_module_init(&s->module, c);
|
||||
|
||||
gbFileError err = gb_file_create(&s->output_file, "../examples/test.ll");
|
||||
// TODO(bill): generate appropriate output name
|
||||
isize pos = string_extension_position(c->parser->init_fullpath);
|
||||
gbFileError err = gb_file_create(&s->output_file, gb_bprintf("%.*s.ll", pos, c->parser->init_fullpath.text));
|
||||
if (err != gbFileError_None)
|
||||
return false;
|
||||
|
||||
@@ -83,245 +85,3 @@ void ssa_gen_code(ssaGen *s) {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#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
|
||||
|
||||
+23
-13
@@ -48,6 +48,7 @@ void ssa_print_escape_string(gbFile *f, String name) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
char hex_table[] = "0123456789ABCDEF";
|
||||
isize buf_len = name.len + extra;
|
||||
u8 *buf = gb_alloc_array(gb_heap_allocator(), u8, buf_len);
|
||||
@@ -101,20 +102,20 @@ void ssa_print_type(gbFile *f, BaseTypeSizes s, Type *t) {
|
||||
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_rawptr: ssa_fprintf(f, "%%-rawptr"); break;
|
||||
case Basic_string: ssa_fprintf(f, "%%-string"); break;
|
||||
case Basic_uint: ssa_fprintf(f, "i%lld", word_bits); break;
|
||||
case Basic_int: 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_print_type(f, s, t->array.elem);
|
||||
ssa_fprintf(f, "]");
|
||||
break;
|
||||
case Type_Slice:
|
||||
ssa_fprintf(f, "{");
|
||||
ssa_print_type(f, s, t->slice.element);
|
||||
ssa_print_type(f, s, t->slice.elem);
|
||||
ssa_fprintf(f, "*, i%lld, i%lld}", word_bits, word_bits);
|
||||
break;
|
||||
case Type_Structure:
|
||||
@@ -128,7 +129,7 @@ void ssa_print_type(gbFile *f, BaseTypeSizes s, Type *t) {
|
||||
ssa_fprintf(f, "}");
|
||||
break;
|
||||
case Type_Pointer:
|
||||
ssa_print_type(f, s, t->pointer.element);
|
||||
ssa_print_type(f, s, t->pointer.elem);
|
||||
ssa_fprintf(f, "*");
|
||||
break;
|
||||
case Type_Named:
|
||||
@@ -149,18 +150,18 @@ void ssa_print_type(gbFile *f, BaseTypeSizes s, Type *t) {
|
||||
ssa_fprintf(f, "}");
|
||||
}
|
||||
break;
|
||||
case Type_Procedure:
|
||||
if (t->procedure.result_count == 0) {
|
||||
case Type_Proc:
|
||||
if (t->proc.result_count == 0) {
|
||||
ssa_fprintf(f, "void");
|
||||
} else {
|
||||
ssa_print_type(f, s, t->procedure.results);
|
||||
ssa_print_type(f, s, t->proc.results);
|
||||
}
|
||||
ssa_fprintf(f, " (");
|
||||
for (isize i = 0; i < t->procedure.param_count; i++) {
|
||||
for (isize i = 0; i < t->proc.param_count; i++) {
|
||||
if (i > 0) {
|
||||
ssa_fprintf(f, ", ");
|
||||
}
|
||||
ssa_print_type(f, s, &t->procedure.params[i]);
|
||||
ssa_print_type(f, s, &t->proc.params[i]);
|
||||
}
|
||||
ssa_fprintf(f, ")*");
|
||||
break;
|
||||
@@ -292,7 +293,7 @@ void ssa_print_instr(gbFile *f, ssaModule *m, ssaValue *value) {
|
||||
} break;
|
||||
|
||||
case ssaInstr_GetElementPtr: {
|
||||
Type *et = instr->get_element_ptr.element_type;
|
||||
Type *et = instr->get_element_ptr.elem_type;
|
||||
ssa_fprintf(f, "%%%d = getelementptr ", value->id);
|
||||
if (instr->get_element_ptr.inbounds)
|
||||
ssa_fprintf(f, "inbounds ");
|
||||
@@ -478,6 +479,15 @@ void ssa_print_llvm_ir(gbFile *f, ssaModule *m) {
|
||||
ssa_fprintf(f, "target datalayout = %.*s\n", LIT(m->layout));
|
||||
}
|
||||
|
||||
ssa_print_encoded_local(f, make_string("-string"));
|
||||
ssa_fprintf(f, " = type {i8*, ");
|
||||
ssa_print_type(f, m->sizes, t_int);
|
||||
ssa_fprintf(f, "} ; Basic_string\n\n");
|
||||
|
||||
ssa_print_encoded_local(f, make_string("-rawptr"));
|
||||
ssa_fprintf(f, " = type i8*");
|
||||
ssa_fprintf(f, " ; Basic_rawptr\n\n");
|
||||
|
||||
gb_for_array(member_index, m->members.entries) {
|
||||
auto *entry = &m->members.entries[member_index];
|
||||
ssaValue *v = entry->value;
|
||||
@@ -513,7 +523,7 @@ void ssa_print_llvm_ir(gbFile *f, ssaModule *m) {
|
||||
ssa_fprintf(f, "define ");
|
||||
}
|
||||
|
||||
auto *proc_type = &proc->entity->type->procedure;
|
||||
auto *proc_type = &proc->entity->type->proc;
|
||||
|
||||
if (proc_type->result_count == 0) {
|
||||
ssa_fprintf(f, "void");
|
||||
@@ -540,7 +550,7 @@ void ssa_print_llvm_ir(gbFile *f, ssaModule *m) {
|
||||
ssa_fprintf(f, ") ");
|
||||
|
||||
if (proc->body == NULL) {
|
||||
ssa_fprintf(f, "\n");
|
||||
ssa_fprintf(f, "\t; foreign procedure\n\n");
|
||||
} else {
|
||||
ssa_fprintf(f, "{\n");
|
||||
gb_for_array(i, proc->blocks) {
|
||||
|
||||
+145
-116
@@ -129,7 +129,7 @@ struct ssaInstr {
|
||||
struct {
|
||||
ssaValue *address;
|
||||
Type * result_type;
|
||||
Type * element_type;
|
||||
Type * elem_type;
|
||||
ssaValue *indices[2];
|
||||
isize index_count;
|
||||
b32 inbounds;
|
||||
@@ -436,7 +436,7 @@ ssaValue *ssa_make_instr_get_element_ptr(ssaProcedure *p, ssaValue *address,
|
||||
i->get_element_ptr.indices[0] = index0;
|
||||
i->get_element_ptr.indices[1] = index1;
|
||||
i->get_element_ptr.index_count = index_count;
|
||||
i->get_element_ptr.element_type = ssa_value_type(address);
|
||||
i->get_element_ptr.elem_type = ssa_value_type(address);
|
||||
i->get_element_ptr.inbounds = inbounds;
|
||||
if (p->curr_block) {
|
||||
gb_array_append(p->curr_block->values, v);
|
||||
@@ -627,7 +627,8 @@ ssaValue *ssa_lvalue_address(ssaLvalue lval, ssaProcedure *p) {
|
||||
Type *ssa_lvalue_type(ssaLvalue lval) {
|
||||
switch (lval.kind) {
|
||||
case ssaLvalue_Address:
|
||||
return type_deref(ssa_value_type(lval.address.value));
|
||||
// return type_deref(ssa_value_type(lval.address.value));
|
||||
return ssa_value_type(lval.address.value);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
@@ -701,8 +702,8 @@ void ssa_begin_procedure_body(ssaProcedure *proc) {
|
||||
gb_array_init(proc->blocks, gb_heap_allocator());
|
||||
proc->curr_block = ssa_add_block(proc, proc->type_expr, make_string("entry"));
|
||||
|
||||
if (proc->type->procedure.params != NULL) {
|
||||
auto *params = &proc->type->procedure.params->tuple;
|
||||
if (proc->type->proc.params != NULL) {
|
||||
auto *params = &proc->type->proc.params->tuple;
|
||||
for (isize i = 0; i < params->variable_count; i++) {
|
||||
Entity *e = params->variables[i];
|
||||
ssa_add_param(proc, e);
|
||||
@@ -711,7 +712,7 @@ void ssa_begin_procedure_body(ssaProcedure *proc) {
|
||||
}
|
||||
|
||||
void ssa_end_procedure_body(ssaProcedure *proc) {
|
||||
if (proc->type->procedure.result_count == 0) {
|
||||
if (proc->type->proc.result_count == 0) {
|
||||
ssa_emit_ret(proc, NULL);
|
||||
}
|
||||
|
||||
@@ -758,83 +759,6 @@ void ssa_pop_target_list(ssaProcedure *proc) {
|
||||
|
||||
|
||||
|
||||
|
||||
ssaValue *ssa_emit_conv(ssaProcedure *proc, ssaValue *value, Type *t) {
|
||||
Type *src_type = ssa_value_type(value);
|
||||
if (are_types_identical(t, src_type))
|
||||
return value;
|
||||
|
||||
Type *src = get_base_type(src_type);
|
||||
Type *dst = get_base_type(t);
|
||||
|
||||
if (value->kind == ssaValue_Constant) {
|
||||
if (dst->kind == Type_Basic)
|
||||
return ssa_make_value_constant(proc->module->allocator, t, value->constant.value);
|
||||
}
|
||||
|
||||
// integer -> integer
|
||||
if (is_type_integer(src) && is_type_integer(dst)) {
|
||||
i64 sz = basic_type_sizes[src->basic.kind];
|
||||
i64 dz = basic_type_sizes[dst->basic.kind];
|
||||
ssaConvKind kind = ssaConv_trunc;
|
||||
if (dz >= sz) {
|
||||
kind = ssaConv_zext;
|
||||
}
|
||||
return ssa_emit(proc, ssa_make_instr_conv(proc, kind, value, src, dst));
|
||||
}
|
||||
|
||||
// float -> float
|
||||
if (is_type_float(src) && is_type_float(dst)) {
|
||||
i64 sz = basic_type_sizes[src->basic.kind];
|
||||
i64 dz = basic_type_sizes[dst->basic.kind];
|
||||
ssaConvKind kind = ssaConv_fptrunc;
|
||||
if (dz >= sz) {
|
||||
kind = ssaConv_fpext;
|
||||
}
|
||||
return ssa_emit(proc, ssa_make_instr_conv(proc, kind, value, src, dst));
|
||||
}
|
||||
|
||||
// float -> integer
|
||||
if (is_type_float(src) && is_type_integer(dst)) {
|
||||
ssaConvKind kind = ssaConv_fptosi;
|
||||
if (is_type_unsigned(dst)) {
|
||||
kind = ssaConv_fptoui;
|
||||
}
|
||||
return ssa_emit(proc, ssa_make_instr_conv(proc, kind, value, src, dst));
|
||||
}
|
||||
|
||||
// integer -> float
|
||||
if (is_type_integer(src) && is_type_float(dst)) {
|
||||
ssaConvKind kind = ssaConv_sitofp;
|
||||
if (is_type_unsigned(dst)) {
|
||||
kind = ssaConv_uitofp;
|
||||
}
|
||||
return ssa_emit(proc, ssa_make_instr_conv(proc, kind, value, src, dst));
|
||||
}
|
||||
|
||||
// Pointer to int
|
||||
if (is_type_pointer(src) && is_type_integer(dst)) {
|
||||
return ssa_emit(proc, ssa_make_instr_conv(proc, ssaConv_ptrtoint, value, src, dst));
|
||||
}
|
||||
|
||||
// int to Pointer
|
||||
if (is_type_integer(src) && is_type_pointer(dst)) {
|
||||
return ssa_emit(proc, ssa_make_instr_conv(proc, ssaConv_inttoptr, value, src, dst));
|
||||
}
|
||||
|
||||
// Pointer to Pointer
|
||||
if (is_type_pointer(src) && is_type_pointer(dst)) {
|
||||
return ssa_emit(proc, ssa_make_instr_conv(proc, ssaConv_bitcast, value, src, dst));
|
||||
}
|
||||
|
||||
|
||||
GB_PANIC("TODO(bill): ssa_emit_conv");
|
||||
GB_PANIC("TODO(bill): string -> []byte");
|
||||
GB_PANIC("TODO(bill): []byte -> string");
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ssaValue *ssa_emit_arith(ssaProcedure *proc, Token op, ssaValue *left, ssaValue *right, Type *type) {
|
||||
switch (op.kind) {
|
||||
case Token_AndNot: {
|
||||
@@ -860,6 +784,7 @@ ssaValue *ssa_emit_arith(ssaProcedure *proc, Token op, ssaValue *left, ssaValue
|
||||
}
|
||||
|
||||
ssaValue *v = ssa_make_instr_binary_op(proc, op, left, right);
|
||||
ssa_value_set_type(v, type);
|
||||
return ssa_emit(proc, v);
|
||||
}
|
||||
|
||||
@@ -885,7 +810,7 @@ ssaValue *ssa_emit_ptr_offset(ssaProcedure *proc, ssaValue *ptr, ssaValue *offse
|
||||
ssaValue *gep = NULL;
|
||||
offset = ssa_emit_conv(proc, offset, t_int);
|
||||
gep = ssa_make_instr_get_element_ptr(proc, ptr, offset, NULL, 1, false);
|
||||
gep->instr.get_element_ptr.element_type = type_deref(type);
|
||||
gep->instr.get_element_ptr.elem_type = type_deref(type);
|
||||
gep->instr.get_element_ptr.result_type = type;
|
||||
return ssa_emit(proc, gep);
|
||||
}
|
||||
@@ -895,7 +820,7 @@ ssaValue *ssa_emit_struct_gep(ssaProcedure *proc, ssaValue *s, ssaValue *index,
|
||||
// NOTE(bill): For some weird legacy reason in LLVM, structure elements must be accessed as an i32
|
||||
index = ssa_emit_conv(proc, index, t_i32);
|
||||
gep = ssa_make_instr_get_element_ptr(proc, s, v_zero, index, 2, true);
|
||||
gep->instr.get_element_ptr.element_type = ssa_value_type(s);
|
||||
gep->instr.get_element_ptr.elem_type = ssa_value_type(s);
|
||||
gep->instr.get_element_ptr.result_type = result_type;
|
||||
|
||||
return ssa_emit(proc, gep);
|
||||
@@ -905,10 +830,10 @@ ssaValue *ssa_emit_struct_gep(ssaProcedure *proc, ssaValue *s, ssaValue *index,
|
||||
ssaValue *ssa_array_elem(ssaProcedure *proc, ssaValue *array) {
|
||||
Type *t = ssa_value_type(array);
|
||||
GB_ASSERT(t->kind == Type_Array);
|
||||
Type *base_type = t->array.element;
|
||||
Type *base_type = t->array.elem;
|
||||
ssaValue *elem = ssa_make_instr_get_element_ptr(proc, array, v_zero, v_zero, 2, true);
|
||||
Type *result_type = make_type_pointer(proc->module->allocator, base_type);
|
||||
elem->instr.get_element_ptr.element_type = t;
|
||||
elem->instr.get_element_ptr.elem_type = t;
|
||||
elem->instr.get_element_ptr.result_type = result_type;
|
||||
return ssa_emit(proc, elem);
|
||||
}
|
||||
@@ -925,7 +850,7 @@ ssaValue *ssa_slice_elem(ssaProcedure *proc, ssaValue *slice) {
|
||||
Type *t = ssa_value_type(slice);
|
||||
GB_ASSERT(t->kind == Type_Slice);
|
||||
|
||||
Type *result_type = make_type_pointer(proc->module->allocator, t->slice.element);
|
||||
Type *result_type = make_type_pointer(proc->module->allocator, t->slice.elem);
|
||||
return ssa_emit_load(proc, ssa_emit_struct_gep(proc, slice, v_zero32, result_type));
|
||||
}
|
||||
ssaValue *ssa_slice_len(ssaProcedure *proc, ssaValue *slice) {
|
||||
@@ -945,7 +870,7 @@ ssaValue *ssa_string_elem(ssaProcedure *proc, ssaValue *string) {
|
||||
Type *base_type = t_u8;
|
||||
ssaValue *elem = ssa_make_instr_get_element_ptr(proc, string, v_zero, v_zero32, 2, true);
|
||||
Type *result_type = make_type_pointer(proc->module->allocator, base_type);
|
||||
elem->instr.get_element_ptr.element_type = t;
|
||||
elem->instr.get_element_ptr.elem_type = t;
|
||||
elem->instr.get_element_ptr.result_type = result_type;
|
||||
ssa_emit(proc, elem);
|
||||
|
||||
@@ -956,9 +881,6 @@ ssaValue *ssa_string_len(ssaProcedure *proc, ssaValue *string) {
|
||||
GB_ASSERT(t->kind == Type_Basic && t->basic.kind == Basic_string);
|
||||
return ssa_emit_load(proc, ssa_emit_struct_gep(proc, string, v_one32, t_int));
|
||||
}
|
||||
ssaValue *ssa_string_cap(ssaProcedure *proc, ssaValue *string) {
|
||||
return ssa_string_len(proc, string);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1070,7 +992,8 @@ ssaValue *ssa_add_global_string_array(ssaProcedure *proc, ExactValue value) {
|
||||
ssaValue *ssa_emit_string(ssaProcedure *proc, ssaValue *elem, ssaValue *len) {
|
||||
Type *t_u8_ptr = ssa_value_type(elem);
|
||||
GB_ASSERT(t_u8_ptr->kind == Type_Pointer);
|
||||
GB_ASSERT(t_u8_ptr->pointer.element == t_u8);
|
||||
|
||||
GB_ASSERT(is_type_byte(t_u8_ptr->pointer.elem));
|
||||
|
||||
ssaValue *str = ssa_add_local_generated(proc, t_string);
|
||||
ssaValue *str_elem = ssa_emit_struct_gep(proc, str, v_zero32, t_u8_ptr);
|
||||
@@ -1081,6 +1004,102 @@ ssaValue *ssa_emit_string(ssaProcedure *proc, ssaValue *elem, ssaValue *len) {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
ssaValue *ssa_emit_conv(ssaProcedure *proc, ssaValue *value, Type *t) {
|
||||
Type *src_type = ssa_value_type(value);
|
||||
if (are_types_identical(t, src_type))
|
||||
return value;
|
||||
|
||||
Type *src = get_base_type(src_type);
|
||||
Type *dst = get_base_type(t);
|
||||
|
||||
if (value->kind == ssaValue_Constant) {
|
||||
if (dst->kind == Type_Basic)
|
||||
return ssa_make_value_constant(proc->module->allocator, t, value->constant.value);
|
||||
}
|
||||
|
||||
// integer -> integer
|
||||
if (is_type_integer(src) && is_type_integer(dst)) {
|
||||
i64 sz = basic_type_sizes[src->basic.kind];
|
||||
i64 dz = basic_type_sizes[dst->basic.kind];
|
||||
ssaConvKind kind = ssaConv_trunc;
|
||||
if (dz >= sz) {
|
||||
kind = ssaConv_zext;
|
||||
}
|
||||
return ssa_emit(proc, ssa_make_instr_conv(proc, kind, value, src, dst));
|
||||
}
|
||||
|
||||
// float -> float
|
||||
if (is_type_float(src) && is_type_float(dst)) {
|
||||
i64 sz = basic_type_sizes[src->basic.kind];
|
||||
i64 dz = basic_type_sizes[dst->basic.kind];
|
||||
ssaConvKind kind = ssaConv_fptrunc;
|
||||
if (dz >= sz) {
|
||||
kind = ssaConv_fpext;
|
||||
}
|
||||
return ssa_emit(proc, ssa_make_instr_conv(proc, kind, value, src, dst));
|
||||
}
|
||||
|
||||
// float <-> integer
|
||||
if (is_type_float(src) && is_type_integer(dst)) {
|
||||
ssaConvKind kind = ssaConv_fptosi;
|
||||
if (is_type_unsigned(dst)) {
|
||||
kind = ssaConv_fptoui;
|
||||
}
|
||||
return ssa_emit(proc, ssa_make_instr_conv(proc, kind, value, src, dst));
|
||||
}
|
||||
if (is_type_integer(src) && is_type_float(dst)) {
|
||||
ssaConvKind kind = ssaConv_sitofp;
|
||||
if (is_type_unsigned(dst)) {
|
||||
kind = ssaConv_uitofp;
|
||||
}
|
||||
return ssa_emit(proc, ssa_make_instr_conv(proc, kind, value, src, dst));
|
||||
}
|
||||
|
||||
// Pointer <-> int
|
||||
if (is_type_pointer(src) && is_type_int_or_uint(dst)) {
|
||||
ssaValue *p = ssa_emit_load(proc, value);
|
||||
return ssa_emit(proc, ssa_make_instr_conv(proc, ssaConv_ptrtoint, p, src, dst));
|
||||
}
|
||||
if (is_type_int_or_uint(src) && is_type_pointer(dst)) {
|
||||
ssaValue *i = ssa_emit_load(proc, value);
|
||||
return ssa_emit(proc, ssa_make_instr_conv(proc, ssaConv_inttoptr, i, src, dst));
|
||||
}
|
||||
|
||||
// Pointer <-> Pointer
|
||||
if (is_type_pointer(src) && is_type_pointer(dst)) {
|
||||
return ssa_emit(proc, ssa_make_instr_conv(proc, ssaConv_bitcast, value, src, dst));
|
||||
}
|
||||
|
||||
|
||||
// []byte/[]u8 <-> string
|
||||
if (is_type_byte_slice(src) && is_type_string(dst)) {
|
||||
ssaValue *slice = ssa_add_local_generated(proc, src);
|
||||
ssa_emit_store(proc, slice, value);
|
||||
ssaValue *elem = ssa_slice_elem(proc, slice);
|
||||
ssaValue *len = ssa_slice_len(proc, slice);
|
||||
return ssa_emit_string(proc, elem, len);
|
||||
}
|
||||
if (is_type_string(src) && is_type_byte_slice(dst)) {
|
||||
ssaValue *str = ssa_add_local_generated(proc, src);
|
||||
ssa_emit_store(proc, str, value);
|
||||
ssaValue *elem = ssa_string_elem(proc, str);
|
||||
ssaValue *len = ssa_string_len(proc, str);
|
||||
ssaValue *v = ssa_emit_slice(proc, dst, elem, v_zero, len, len);
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
GB_PANIC("Invalid type conversion: `%s` to `%s`", type_to_string(src_type), type_to_string(t));
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue *tv) {
|
||||
switch (expr->kind) {
|
||||
case_ast_node(bl, BasicLit, expr);
|
||||
@@ -1117,8 +1136,10 @@ ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue
|
||||
|
||||
case_ast_node(ue, UnaryExpr, expr);
|
||||
switch (ue->op.kind) {
|
||||
case Token_Pointer:
|
||||
return ssa_lvalue_address(ssa_build_addr(proc, ue->expr), proc);
|
||||
case Token_Pointer: {
|
||||
ssaLvalue lval = ssa_build_addr(proc, ue->expr);
|
||||
return ssa_lvalue_address(lval, proc);
|
||||
}
|
||||
case Token_Add:
|
||||
return ssa_build_expr(proc, ue->expr);
|
||||
case Token_Sub: {
|
||||
@@ -1181,8 +1202,7 @@ ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue
|
||||
case_end;
|
||||
|
||||
case_ast_node(ce, CastExpr, expr);
|
||||
ssaValue *v = ssa_build_expr(proc, ce->expr);
|
||||
return ssa_emit_conv(proc, v, tv->type);
|
||||
return ssa_emit_conv(proc, ssa_build_expr(proc, ce->expr), tv->type);
|
||||
case_end;
|
||||
|
||||
case_ast_node(ce, CallExpr, expr);
|
||||
@@ -1192,7 +1212,8 @@ ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue
|
||||
if (found && (*found)->kind == Entity_Builtin) {
|
||||
Entity *e = *found;
|
||||
switch (e->builtin.id) {
|
||||
case BuiltinProcedure_len: {
|
||||
case BuiltinProc_len: {
|
||||
// NOTE(bill): len of an array is a constant expression
|
||||
ssaValue *v = ssa_lvalue_address(ssa_build_addr(proc, ce->arg_list), proc);
|
||||
Type *t = get_base_type(ssa_value_type(v));
|
||||
if (t == t_string)
|
||||
@@ -1200,22 +1221,27 @@ ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue
|
||||
else if (t->kind == Type_Slice)
|
||||
return ssa_slice_len(proc, v);
|
||||
} break;
|
||||
case BuiltinProcedure_cap: {
|
||||
case BuiltinProc_cap: {
|
||||
// NOTE(bill): cap of an array is a constant expression
|
||||
ssaValue *v = ssa_lvalue_address(ssa_build_addr(proc, ce->arg_list), proc);
|
||||
Type *t = get_base_type(ssa_value_type(v));
|
||||
if (t == t_string)
|
||||
return ssa_string_cap(proc, v);
|
||||
else if (t->kind == Type_Slice)
|
||||
return ssa_slice_cap(proc, v);
|
||||
return ssa_slice_cap(proc, v);
|
||||
} break;
|
||||
case BuiltinProcedure_copy: {
|
||||
GB_PANIC("TODO(bill): BuiltinProcedure_copy");
|
||||
case BuiltinProc_copy: {
|
||||
GB_PANIC("TODO(bill): BuiltinProc_copy");
|
||||
// TODO(bill): Should this be llvm.memmove internally?
|
||||
// http://llvm.org/docs/LangRef.html#llvm-memmove-intrinsic
|
||||
// declare void @llvm.memmove.p0i8.p0i8.i32(i8* <dest>, i8* <src>, i32 <len>, i32 <align>, i1 <isvolatile>)
|
||||
// declare void @llvm.memmove.p0i8.p0i8.i64(i8* <dest>, i8* <src>, i64 <len>, i32 <align>, i1 <isvolatile>)
|
||||
} break;
|
||||
case BuiltinProcedure_print: {
|
||||
GB_PANIC("TODO(bill): BuiltinProcedure_print");
|
||||
case BuiltinProc_append: {
|
||||
GB_PANIC("TODO(bill): BuiltinProc_append");
|
||||
} break;
|
||||
case BuiltinProcedure_println: {
|
||||
GB_PANIC("TODO(bill): BuiltinProcedure_println");
|
||||
case BuiltinProc_print: {
|
||||
GB_PANIC("TODO(bill): BuiltinProc_print");
|
||||
} break;
|
||||
case BuiltinProc_println: {
|
||||
GB_PANIC("TODO(bill): BuiltinProc_println");
|
||||
} break;
|
||||
}
|
||||
}
|
||||
@@ -1225,8 +1251,8 @@ ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue
|
||||
// NOTE(bill): Regular call
|
||||
ssaValue *value = ssa_build_expr(proc, ce->proc);
|
||||
Type *proc_type_ = ssa_value_type(value);
|
||||
GB_ASSERT(proc_type_->kind == Type_Procedure);
|
||||
auto *type = &proc_type_->procedure;
|
||||
GB_ASSERT(proc_type_->kind == Type_Proc);
|
||||
auto *type = &proc_type_->proc;
|
||||
|
||||
isize arg_index = 0;
|
||||
isize arg_count = type->param_count;
|
||||
@@ -1242,10 +1268,12 @@ ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue
|
||||
}
|
||||
}
|
||||
|
||||
#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);
|
||||
return ssa_emit(proc, call);
|
||||
@@ -1391,7 +1419,7 @@ ssaLvalue ssa_build_addr(ssaProcedure *proc, AstNode *expr) {
|
||||
ssaValue *gep = ssa_make_instr_get_element_ptr(proc, e, NULL, NULL, 0, false);
|
||||
Type *t = type_deref(get_base_type(ssa_value_type(e)));
|
||||
gep->instr.get_element_ptr.result_type = t;
|
||||
gep->instr.get_element_ptr.element_type = t;
|
||||
gep->instr.get_element_ptr.elem_type = t;
|
||||
ssaValue *v = ssa_emit(proc, gep);
|
||||
return ssa_make_lvalue_address(v, expr);
|
||||
case_end;
|
||||
@@ -1484,7 +1512,8 @@ void ssa_build_stmt(ssaProcedure *proc, AstNode *node) {
|
||||
|
||||
|
||||
gb_for_array(i, inits) {
|
||||
ssa_lvalue_store(lvals[i], proc, inits[i]);
|
||||
ssaValue *v = ssa_emit_conv(proc, inits[i], ssa_lvalue_type(lvals[i]));
|
||||
ssa_lvalue_store(lvals[i], proc, v);
|
||||
}
|
||||
|
||||
} else if (vd->value_count == 0) { // declared and zero-initialized
|
||||
@@ -1583,8 +1612,8 @@ void ssa_build_stmt(ssaProcedure *proc, AstNode *node) {
|
||||
|
||||
case_ast_node(rs, ReturnStmt, node);
|
||||
ssaValue *v = NULL;
|
||||
auto *return_type_tuple = &proc->type->procedure.results->tuple;
|
||||
isize return_count = proc->type->procedure.result_count;
|
||||
auto *return_type_tuple = &proc->type->proc.results->tuple;
|
||||
isize return_count = proc->type->proc.result_count;
|
||||
if (rs->result_count == 1 && return_count > 1) {
|
||||
GB_PANIC("ReturnStmt tuple return statement");
|
||||
} else if (return_count == 1) {
|
||||
@@ -1595,7 +1624,7 @@ void ssa_build_stmt(ssaProcedure *proc, AstNode *node) {
|
||||
// No return values
|
||||
} else {
|
||||
// 1:1 multiple return values
|
||||
Type *ret_type = proc->type->procedure.results;
|
||||
Type *ret_type = proc->type->proc.results;
|
||||
v = ssa_add_local_generated(proc, ret_type);
|
||||
isize i = 0;
|
||||
AstNode *r = rs->result_list;
|
||||
|
||||
Reference in New Issue
Block a user