#include "util/u_memory.h"
#include "lp_bld_assert.h"
#include "lp_bld_init.h"
+#include "lp_bld_const.h"
#include "lp_bld_printf.h"
* \param condition should be an 'i1' or 'i32' value
* \param msg a string to print if the assertion fails.
*/
-LLVMValueRef
-lp_build_assert(LLVMBuilderRef builder, LLVMValueRef condition,
+void
+lp_build_assert(struct gallivm_state *gallivm,
+ LLVMValueRef condition,
const char *msg)
{
- LLVMModuleRef module;
+ LLVMBuilderRef builder = gallivm->builder;
+ LLVMContextRef context = gallivm->context;
LLVMTypeRef arg_types[2];
- LLVMValueRef msg_string, assert_func, params[2], r;
+ LLVMTypeRef ret_type;
+ LLVMValueRef function;
+ LLVMValueRef args[2];
+ LLVMValueRef msg_string;
- module = LLVMGetGlobalParent(LLVMGetBasicBlockParent(
- LLVMGetInsertBlock(builder)));
+ msg_string = lp_build_const_string(gallivm, msg);
- msg_string = lp_build_const_string_variable(module, msg, strlen(msg) + 1);
+ ret_type = LLVMVoidTypeInContext(context);
+ arg_types[0] = LLVMInt32TypeInContext(context);
+ arg_types[1] = LLVMPointerType(LLVMInt8TypeInContext(context), 0);
- arg_types[0] = LLVMInt32Type();
- arg_types[1] = LLVMPointerType(LLVMInt8Type(), 0);
-
- /* lookup the lp_assert function */
- assert_func = LLVMGetNamedFunction(module, "lp_assert");
-
- /* Create the assertion function if not found */
- if (!assert_func) {
- LLVMTypeRef func_type =
- LLVMFunctionType(LLVMVoidType(), arg_types, 2, 0);
-
- assert_func = LLVMAddFunction(module, "lp_assert", func_type);
- LLVMSetFunctionCallConv(assert_func, LLVMCCallConv);
- LLVMSetLinkage(assert_func, LLVMExternalLinkage);
- LLVMAddGlobalMapping(lp_build_engine, assert_func,
- func_to_pointer((func_pointer)lp_assert));
- }
- assert(assert_func);
+ function = lp_build_const_func_pointer(gallivm,
+ func_to_pointer((func_pointer)lp_assert),
+ ret_type, arg_types, ARRAY_SIZE(arg_types),
+ "assert");
/* build function call param list */
- params[0] = LLVMBuildZExt(builder, condition, arg_types[0], "");
- params[1] = LLVMBuildBitCast(builder, msg_string, arg_types[1], "");
+ args[0] = LLVMBuildZExt(builder, condition, arg_types[0], "");
+ args[1] = msg_string;
/* check arg types */
- assert(LLVMTypeOf(params[0]) == arg_types[0]);
- assert(LLVMTypeOf(params[1]) == arg_types[1]);
-
- r = LLVMBuildCall(builder, assert_func, params, 2, "");
+ assert(LLVMTypeOf(args[0]) == arg_types[0]);
+ assert(LLVMTypeOf(args[1]) == arg_types[1]);
- return r;
+ LLVMBuildCall(builder, function, args, ARRAY_SIZE(args), "");
}