fixed: parameter handling

This commit is contained in:
Sven Vogel 2024-07-16 19:44:57 +02:00
parent 0e03246fc3
commit 942c9484ac
2 changed files with 40 additions and 9 deletions

View File

@ -412,7 +412,7 @@ BackendError impl_parameter_load(LLVMBackendCompileUnit *unit, LLVMLocalScope *s
return new_backend_impl_error(Implementation, NULL, "Variable not found"); return new_backend_impl_error(Implementation, NULL, "Variable not found");
} }
if (decl.qualifier == In) { if (decl.qualifier == In || reference) {
*llvm_result = llvm_variable; *llvm_result = llvm_variable;
} else { } else {
// no referencing, load value // no referencing, load value

View File

@ -12,6 +12,27 @@
#include <assert.h> #include <assert.h>
#include <mem/cache.h> #include <mem/cache.h>
BackendError impl_param_load(
LLVMBackendCompileUnit *unit,
LLVMBuilderRef builder,
LLVMLocalScope *scope,
const StorageExpr *expr,
LLVMValueRef* storage_target) {
BackendError err = SUCCESS;
if (expr->impl.parameter->impl.declaration.qualifier == Out || expr->impl.parameter->impl.declaration.qualifier == InOut) {
LLVMTypeRef llvm_type = NULL;
err = get_type_impl(unit, scope->func_scope->global_scope, expr->impl.parameter->impl.declaration.type, &llvm_type);
if (err.kind != Success) {
return err;
}
*storage_target = LLVMBuildLoad2(builder, llvm_type, *storage_target, "strg.param.out.load");
}
return err;
}
BackendError impl_storage_expr( BackendError impl_storage_expr(
LLVMBackendCompileUnit *unit, LLVMBackendCompileUnit *unit,
LLVMBuilderRef LLVMBuilderRef
@ -45,6 +66,13 @@ BackendError impl_storage_expr(
return err; return err;
} }
if (expr->impl.dereference.array->kind == StorageExprKindParameter) {
err = impl_param_load(unit, builder, scope, expr->impl.dereference.array, &array);
if (err.kind != Success) {
return err;
}
}
if (expr->impl.dereference.array->kind == StorageExprKindDereference) { if (expr->impl.dereference.array->kind == StorageExprKindDereference) {
LLVMTypeRef deref_type = NULL; LLVMTypeRef deref_type = NULL;
err = get_type_impl(unit, scope->func_scope->global_scope, expr->impl.dereference.array->target_type, &deref_type); err = get_type_impl(unit, scope->func_scope->global_scope, expr->impl.dereference.array->target_type, &deref_type);
@ -216,21 +244,24 @@ BackendError impl_func_call(LLVMBackendCompileUnit *unit,
param_list = call->function->impl.declaration.parameter; param_list = call->function->impl.declaration.parameter;
} }
LLVMBool reference = FALSE; Parameter param = g_array_index(param_list, Parameter, i);
Parameter parameter = g_array_index(param_list, Parameter, i);
if (is_parameter_out(&parameter)) {
reference = TRUE;
} else if (parameter.impl.declaration.type->kind == TypeKindReference) {
reference = TRUE;
}
LLVMValueRef llvm_arg = NULL; LLVMValueRef llvm_arg = NULL;
err = impl_expr(unit, scope, builder, arg, reference, &llvm_arg); err = impl_expr(unit, scope, builder, arg, is_parameter_out(&param), &llvm_arg);
if (err.kind != Success) { if (err.kind != Success) {
break; break;
} }
if (is_parameter_out(&param)) {
if ((arg->kind == ExpressionKindParameter && !is_parameter_out(arg->impl.parameter)) || arg->kind != ExpressionKindParameter) {
LLVMValueRef index = LLVMConstInt(LLVMInt32Type(), 0, false);
LLVMTypeRef llvm_type = NULL;
get_type_impl(unit, scope->func_scope->global_scope, param.impl.declaration.type, &llvm_type);
llvm_arg = LLVMBuildGEP2(builder, llvm_type, llvm_arg, &index, 1, "");
}
}
arguments[i] = llvm_arg; arguments[i] = llvm_arg;
} }