Mocking out call related stuff

This commit is contained in:
gingerBill
2023-07-14 17:34:00 +01:00
parent b17ebeb6f6
commit ca442defbb
6 changed files with 207 additions and 14 deletions

View File

@@ -708,14 +708,12 @@ gb_internal void cg_procedure_begin(cgProcedure *p) {
if (p == nullptr || p->func == nullptr) {
return;
}
gb_printf_err("cg_procedure_begin %.*s\n", LIT(p->name));
}
gb_internal void cg_procedure_end(cgProcedure *p) {
if (p == nullptr || p->func == nullptr) {
return;
}
gb_printf_err("cg_procedure_end %.*s\n", LIT(p->name));
tb_inst_ret(p->func, 0, nullptr);
tb_module_compile_function(p->module->mod, p->func, TB_ISEL_FAST);
}
@@ -731,18 +729,16 @@ gb_internal void cg_procedure_generate(cgProcedure *p) {
p->name != "main") {
return;
}
if (p->body != nullptr) {
cg_build_stmt(p, p->body);
}
cg_build_stmt(p, p->body);
}
#include "tilde_const.cpp"
#include "tilde_expr.cpp"
#include "tilde_proc.cpp"
#include "tilde_stmt.cpp"
gb_internal bool cg_generate_code(Checker *c) {
TIME_SECTION("Tilde Module Initializtion");

View File

@@ -194,8 +194,22 @@ gb_internal cgAddr cg_addr(cgValue const &value);
gb_internal cgValue cg_const_value(cgProcedure *p, Type *type, ExactValue const &value);
gb_internal cgValue cg_const_nil(cgProcedure *p, Type *type);
gb_internal void cg_build_stmt(cgProcedure *p, Ast *stmt);
gb_internal void cg_build_stmt_list(cgProcedure *p, Slice<Ast *> const &stmts);
gb_internal void cg_build_when_stmt(cgProcedure *p, AstWhenStmt *ws);
gb_internal void cg_build_when_stmt(cgProcedure *p, AstWhenStmt *ws);
gb_internal cgValue cg_build_expr(cgProcedure *p, Ast *expr);
gb_internal cgAddr cg_build_addr(cgProcedure *p, Ast *expr);
gb_internal Type * cg_addr_type(cgAddr const &addr);
gb_internal cgValue cg_addr_load(cgProcedure *p, cgAddr addr);
gb_internal void cg_addr_store(cgProcedure *p, cgAddr addr, cgValue value);
gb_internal cgValue cg_emit_load(cgProcedure *p, cgValue const &ptr, bool is_volatile=false);
gb_internal void cg_emit_store(cgProcedure *p, cgValue dst, cgValue const &src, bool is_volatile=false);
gb_internal cgAddr cg_add_local(cgProcedure *p, Type *type, Entity *e, bool zero_init);
gb_internal cgValue cg_build_call_expr(cgProcedure *p, Ast *expr);

View File

@@ -17,7 +17,7 @@ gb_internal cgValue cg_const_nil(cgProcedure *p, Type *type) {
}
if (is_type_internally_pointer_like(type)) {
return cg_value(tb_inst_ptr(p->func, 0), type);
return cg_value(tb_inst_uint(p->func, dt, 0), type);
} else if (is_type_integer(type) || is_type_boolean(type) || is_type_bit_set(type)) {
return cg_value(tb_inst_uint(p->func, dt, 0), type);
} else if (is_type_float(type)) {
@@ -34,7 +34,7 @@ gb_internal cgValue cg_const_nil(cgProcedure *p, Type *type) {
return {};
}
gb_internal cgValue cg_const_value(cgProcedure *p, Type *type, ExactValue const &value) {
gb_internal cgValue cg_const_value(cgModule *m, cgProcedure *p, Type *type, ExactValue const &value) {
TB_Node *node = nullptr;
if (value.kind == ExactValue_Invalid) {
@@ -42,4 +42,9 @@ gb_internal cgValue cg_const_value(cgProcedure *p, Type *type, ExactValue const
}
return cg_value(node, type);
}
gb_internal cgValue cg_const_value(cgProcedure *p, Type *type, ExactValue const &value) {
GB_ASSERT(p != nullptr);
return cg_const_value(p->module, p, type, value);
}

View File

@@ -1,8 +1,52 @@
gb_internal cgValue cg_typeid(cgModule *m, Type *t) {
GB_ASSERT("TODO(bill): cg_typeid");
return {};
}
gb_internal cgValue cg_emit_conv(cgProcedure *p, cgValue value, Type *type) {
// TODO(bill): cg_emit_conv
return value;
}
gb_internal cgValue cg_emit_transmute(cgProcedure *p, cgValue value, Type *type) {
GB_ASSERT(type_size_of(value.type) == type_size_of(type));
if (value.kind == cgValue_Symbol) {
GB_ASSERT(is_type_pointer(value.type));
value = cg_value(tb_inst_get_symbol_address(p->func, value.symbol), type);
}
i64 src_align = type_align_of(value.type);
i64 dst_align = type_align_of(type);
if (dst_align > src_align) {
cgAddr local = cg_add_local(p, type, nullptr, false);
cgValue dst = local.addr;
dst.type = alloc_type_pointer(value.type);
cg_emit_store(p, dst, value);
return cg_addr_load(p, local);
}
TB_DataType dt = cg_data_type(type);
switch (value.kind) {
case cgValue_Value:
GB_ASSERT(!TB_IS_VOID_TYPE(dt));
value.type = type;
value.node = tb_inst_bitcast(p->func, value.node, dt);
return value;
case cgValue_Addr:
value.type = type;
return value;
case cgValue_Symbol:
GB_PANIC("should be handled above");
break;
}
return value;
}
gb_internal cgValue cg_build_expr_internal(cgProcedure *p, Ast *expr);
gb_internal cgValue cg_build_expr(cgProcedure *p, Ast *expr) {
@@ -68,8 +112,138 @@ gb_internal cgValue cg_build_expr(cgProcedure *p, Ast *expr) {
}
gb_internal cgValue cg_build_expr_internal(cgProcedure *p, Ast *expr) {
cgModule *m = p->module;
expr = unparen_expr(expr);
TokenPos expr_pos = ast_token(expr).pos;
TypeAndValue tv = type_and_value_of_expr(expr);
Type *type = type_of_expr(expr);
GB_ASSERT_MSG(tv.mode != Addressing_Invalid, "invalid expression '%s' (tv.mode = %d, tv.type = %s) @ %s\n Current Proc: %.*s : %s", expr_to_string(expr), tv.mode, type_to_string(tv.type), token_pos_to_string(expr_pos), LIT(p->name), type_to_string(p->type));
if (tv.value.kind != ExactValue_Invalid) {
// NOTE(bill): The commented out code below is just for debug purposes only
// if (is_type_untyped(type)) {
// gb_printf_err("%s %s : %s @ %p\n", token_pos_to_string(expr_pos), expr_to_string(expr), type_to_string(expr->tav.type), expr);
// GB_PANIC("%s\n", type_to_string(tv.type));
// }
// NOTE(bill): Short on constant values
return cg_const_value(p, type, tv.value);
} else if (tv.mode == Addressing_Type) {
// NOTE(bill, 2023-01-16): is this correct? I hope so at least
return cg_typeid(m, tv.type);
}
switch (expr->kind) {
case_ast_node(bl, BasicLit, expr);
TokenPos pos = bl->token.pos;
GB_PANIC("Non-constant basic literal %s - %.*s", token_pos_to_string(pos), LIT(token_strings[bl->token.kind]));
case_end;
case_ast_node(bd, BasicDirective, expr);
TokenPos pos = bd->token.pos;
GB_PANIC("Non-constant basic literal %s - %.*s", token_pos_to_string(pos), LIT(bd->name.string));
case_end;
case_ast_node(i, Ident, expr);
Entity *e = entity_from_expr(expr);
e = strip_entity_wrapping(e);
GB_ASSERT_MSG(e != nullptr, "%s in %.*s %p", expr_to_string(expr), LIT(p->name), expr);
if (e->kind == Entity_Builtin) {
Token token = ast_token(expr);
GB_PANIC("TODO(bill): lb_build_expr Entity_Builtin '%.*s'\n"
"\t at %s", LIT(builtin_procs[e->Builtin.id].name),
token_pos_to_string(token.pos));
return {};
} else if (e->kind == Entity_Nil) {
// TODO(bill): is this correct?
return cg_value(cast(TB_Node *)nullptr, e->type);
}
GB_ASSERT(e->kind != Entity_ProcGroup);
// return cg_find_ident(p, m, e, expr);
GB_PANIC("TODO: cg_find_ident");
return {};
case_end;
case_ast_node(i, Implicit, expr);
return cg_addr_load(p, cg_build_addr(p, expr));
case_end;
case_ast_node(u, Uninit, expr);
if (is_type_untyped(type)) {
return cg_value(cast(TB_Node *)nullptr, t_untyped_uninit);
}
return cg_value(tb_inst_poison(p->func), type);
case_end;
case_ast_node(de, DerefExpr, expr);
return cg_addr_load(p, cg_build_addr(p, expr));
case_end;
case_ast_node(se, SelectorExpr, expr);
TypeAndValue tav = type_and_value_of_expr(expr);
GB_ASSERT(tav.mode != Addressing_Invalid);
return cg_addr_load(p, cg_build_addr(p, expr));
case_end;
case_ast_node(ise, ImplicitSelectorExpr, expr);
TypeAndValue tav = type_and_value_of_expr(expr);
GB_ASSERT(tav.mode == Addressing_Constant);
return cg_const_value(p, type, tv.value);
case_end;
case_ast_node(se, SelectorCallExpr, expr);
GB_ASSERT(se->modified_call);
return cg_build_call_expr(p, se->call);
case_end;
case_ast_node(i, CallExpr, expr);
return cg_build_call_expr(p, expr);
case_end;
case_ast_node(te, TernaryIfExpr, expr);
GB_PANIC("TODO(bill): TernaryIfExpr");
case_end;
case_ast_node(te, TernaryWhenExpr, expr);
TypeAndValue tav = type_and_value_of_expr(te->cond);
GB_ASSERT(tav.mode == Addressing_Constant);
GB_ASSERT(tav.value.kind == ExactValue_Bool);
if (tav.value.value_bool) {
return cg_build_expr(p, te->x);
} else {
return cg_build_expr(p, te->y);
}
case_end;
case_ast_node(tc, TypeCast, expr);
cgValue e = cg_build_expr(p, tc->expr);
switch (tc->token.kind) {
case Token_cast:
return cg_emit_conv(p, e, type);
case Token_transmute:
return cg_emit_transmute(p, e, type);
}
GB_PANIC("Invalid AST TypeCast");
case_end;
case_ast_node(ac, AutoCast, expr);
cgValue value = cg_build_expr(p, ac->expr);
return cg_emit_conv(p, value, type);
case_end;
}
GB_PANIC("TODO(bill): cg_build_expr_internal %.*s", LIT(ast_strings[expr->kind]));
return {};
}

5
src/tilde_proc.cpp Normal file
View File

@@ -0,0 +1,5 @@
gb_internal cgValue cg_build_call_expr(cgProcedure *p, Ast *expr) {
ast_node(ce, CallExpr, expr);
// TODO(bill): cg_build_call_expr
return {};
}

View File

@@ -1,5 +1,4 @@
gb_internal cgValue cg_emit_load(cgProcedure *p, cgValue const &ptr, bool is_volatile=false) {
gb_internal cgValue cg_emit_load(cgProcedure *p, cgValue const &ptr, bool is_volatile) {
GB_ASSERT(is_type_pointer(ptr.type));
Type *type = type_deref(ptr.type);
TB_DataType dt = cg_data_type(type);
@@ -33,10 +32,10 @@ gb_internal cgValue cg_emit_load(cgProcedure *p, cgValue const &ptr, bool is_vol
return cg_value(tb_inst_load(p->func, dt, the_ptr, alignment, is_volatile), type);
}
gb_internal void cg_emit_store(cgProcedure *p, cgValue dst, cgValue const &src, bool is_volatile=false) {
gb_internal void cg_emit_store(cgProcedure *p, cgValue dst, cgValue const &src, bool is_volatile) {
if (dst.kind == cgValue_Addr) {
dst = cg_emit_load(p, dst, is_volatile);
} else if (dst.kind = cgValue_Symbol) {
} else if (dst.kind == cgValue_Symbol) {
dst = cg_value(tb_inst_get_symbol_address(p->func, dst.symbol), dst.type);
}