feature: added logical operators

This commit is contained in:
Sven Vogel 2024-05-28 23:12:56 +02:00
parent 28a4f619a0
commit 3ba11ec97b
2 changed files with 47 additions and 44 deletions

View File

@ -15,7 +15,7 @@ BackendError impl_bitwise_operation(LLVMBackendCompileUnit* unit,
LLVMValueRef lhs = NULL;
LLVMValueRef op = NULL;
if (operation->kind == BitwiseNot) {
if (operation->impl.bitwise == BitwiseNot) {
// single operand
} else {
// two operands
@ -39,6 +39,24 @@ BackendError impl_bitwise_operation(LLVMBackendCompileUnit* unit,
return SUCCESS;
}
/**
* @brief Convert any integral type (integer) to a boolean value.
* A boolean value hereby meaning an integer of the same type as the input
* value but with the value of either 0 or one.
* @param builder
* @param integral
* @return
*/
static LLVMValueRef convert_integral_to_boolean(
LLVMBuilderRef builder, LLVMValueRef integral) {
// type of input
LLVMTypeRef valueType = LLVMTypeOf(integral);
// zero value of same type as integral
LLVMValueRef zero = LLVMConstIntOfString(valueType, "0", 10);
// returns 1 if integral is not zero and zero otherwise
return LLVMBuildICmp(builder, LLVMIntNE, zero, integral, "to boolean");
}
BackendError impl_logical_operation(LLVMBackendCompileUnit *unit,
LLVMLocalScope *scope,
LLVMBuilderRef builder,
@ -51,8 +69,11 @@ BackendError impl_logical_operation(LLVMBackendCompileUnit* unit,
if (operation->kind == BitwiseNot) {
// single operand
op = convert_integral_to_boolean(builder, op);
} else {
// two operands
lhs = convert_integral_to_boolean(builder, lhs);
rhs = convert_integral_to_boolean(builder, rhs);
}
switch (operation->impl.bitwise) {
@ -139,6 +160,7 @@ BackendError impl_typecast(LLVMBackendCompileUnit* unit, LLVMLocalScope* scope,
return err;
}
BackendError impl_expr(LLVMBackendCompileUnit *unit, LLVMLocalScope *scope,
LLVMBuilderRef builder, Expression *expr,
LLVMValueRef *llvm_result) {

View File

@ -210,25 +210,6 @@ LLVMGlobalScope* new_global_scope() {
return scope;
}
LLVMLocalScope* new_local_scope(LLVMGlobalScope* global_scope,
LLVMLocalScope* parent_scope) {
DEBUG("creating local scope...");
LLVMLocalScope* scope = malloc(sizeof(LLVMLocalScope));
scope->variables = g_hash_table_new(g_str_hash, g_str_equal);
scope->params = g_hash_table_new(g_str_hash, g_str_equal);
scope->global_scope = global_scope;
scope->parent_scope = parent_scope;
return scope;
}
void delete_local_scope(LLVMLocalScope* scope) {
DEBUG("deleting global scope...");
g_hash_table_unref(scope->variables);
free(scope);
}
void delete_global_scope(LLVMGlobalScope* scope) {
DEBUG("deleting global scope...");
g_hash_table_unref(scope->functions);