Skip to content

Commit

Permalink
Merge pull request #2169 from odin-lang/location-byval
Browse files Browse the repository at this point in the history
Ad-hoc pass source code location directly by pointer without stack copy
  • Loading branch information
gingerBill authored Nov 1, 2022
2 parents 6cc07dc + c39ef1b commit 382bd87
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/llvm_backend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ lbValue lb_find_value_from_entity(lbModule *m, Entity *e);

void lb_store_type_case_implicit(lbProcedure *p, Ast *clause, lbValue value);
lbAddr lb_store_range_stmt_val(lbProcedure *p, Ast *stmt_val, lbValue value);
lbValue lb_emit_source_code_location(lbProcedure *p, String const &procedure, TokenPos const &pos);
lbValue lb_emit_source_code_location_const(lbProcedure *p, String const &procedure, TokenPos const &pos);

lbValue lb_handle_param_value(lbProcedure *p, Type *parameter_type, ParameterValue const &param_value, TokenPos const &pos);

Expand Down
10 changes: 5 additions & 5 deletions src/llvm_backend_const.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ lbValue lb_expr_untyped_const_to_typed(lbModule *m, Ast *expr, Type *t) {
return lb_const_value(m, t, tv.value);
}

lbValue lb_emit_source_code_location(lbProcedure *p, String const &procedure, TokenPos const &pos) {
lbValue lb_emit_source_code_location_const(lbProcedure *p, String const &procedure, TokenPos const &pos) {
lbModule *m = p->module;

LLVMValueRef fields[4] = {};
Expand All @@ -271,7 +271,7 @@ lbValue lb_emit_source_code_location(lbProcedure *p, String const &procedure, To
return res;
}

lbValue lb_emit_source_code_location(lbProcedure *p, Ast *node) {
lbValue lb_emit_source_code_location_const(lbProcedure *p, Ast *node) {
String proc_name = {};
if (p->entity) {
proc_name = p->entity->token.string;
Expand All @@ -280,19 +280,19 @@ lbValue lb_emit_source_code_location(lbProcedure *p, Ast *node) {
if (node) {
pos = ast_token(node).pos;
}
return lb_emit_source_code_location(p, proc_name, pos);
return lb_emit_source_code_location_const(p, proc_name, pos);
}

lbValue lb_emit_source_code_location_as_global(lbProcedure *p, String const &procedure, TokenPos const &pos) {
lbValue loc = lb_emit_source_code_location(p, procedure, pos);
lbValue loc = lb_emit_source_code_location_const(p, procedure, pos);
lbAddr addr = lb_add_global_generated(p->module, loc.type, loc, nullptr);
lb_make_global_private_const(addr);
return lb_addr_load(p, addr);
}


lbValue lb_emit_source_code_location_as_global(lbProcedure *p, Ast *node) {
lbValue loc = lb_emit_source_code_location(p, node);
lbValue loc = lb_emit_source_code_location_const(p, node);
lbAddr addr = lb_add_global_generated(p->module, loc.type, loc, nullptr);
lb_make_global_private_const(addr);
return lb_addr_load(p, addr);
Expand Down
11 changes: 9 additions & 2 deletions src/llvm_backend_proc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,9 @@ lbValue lb_emit_call(lbProcedure *p, lbValue value, Array<lbValue> const &args,
auto processed_args = array_make<lbValue>(permanent_allocator(), 0, args.count);

{

bool is_odin_cc = is_calling_convention_odin(pt->Proc.calling_convention);

lbFunctionType *ft = lb_get_function_type(m, p, pt);
bool return_by_pointer = ft->ret.kind == lbArg_Indirect;

Expand Down Expand Up @@ -946,8 +949,12 @@ lbValue lb_emit_call(lbProcedure *p, lbValue value, Array<lbValue> const &args,
} else if (arg->kind == lbArg_Indirect) {
lbValue ptr = {};
if (arg->is_byval) {
ptr = lb_copy_value_to_ptr(p, x, original_type, arg->byval_alignment);
} else if (is_calling_convention_odin(pt->Proc.calling_convention)) {
if (is_odin_cc && are_types_identical(original_type, t_source_code_location)) {
ptr = lb_address_from_load_or_generate_local(p, x);
} else {
ptr = lb_copy_value_to_ptr(p, x, original_type, arg->byval_alignment);
}
} else if (is_odin_cc) {
// NOTE(bill): Odin parameters are immutable so the original value can be passed if possible
// i.e. `T const &` in C++
ptr = lb_address_from_load_or_generate_local(p, x);
Expand Down
2 changes: 1 addition & 1 deletion src/llvm_backend_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ void lb_setup_type_info_data(lbProcedure *p) { // NOTE(bill): Setup type_info da
}
TokenPos pos = t->Named.type_name->token.pos;

lbValue loc = lb_emit_source_code_location(p, proc_name, pos);
lbValue loc = lb_emit_source_code_location_const(p, proc_name, pos);

LLVMValueRef vals[4] = {
lb_const_string(p->module, t->Named.type_name->token.string).value,
Expand Down

0 comments on commit 382bd87

Please sign in to comment.