1/************************************************************************** 2 * 3 * Copyright 2010 VMware, Inc. 4 * All Rights Reserved. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sub license, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * The above copyright notice and this permission notice (including the 15 * next paragraph) shall be included in all copies or substantial portions 16 * of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR 22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 * 26 **************************************************************************/ 27 28#include "util/u_debug.h" 29#include "util/u_memory.h" 30#include "lp_bld_assert.h" 31#include "lp_bld_init.h" 32#include "lp_bld_const.h" 33#include "lp_bld_printf.h" 34 35 36/** 37 * A call to lp_build_assert() will build a function call to this function. 38 */ 39static void 40lp_assert(int condition, const char *msg) 41{ 42 if (!condition) { 43 debug_printf("LLVM assertion '%s' failed!\n", msg); 44 assert(condition); 45 } 46} 47 48 49 50/** 51 * lp_build_assert. 52 * 53 * Build an assertion in LLVM IR by building a function call to the 54 * lp_assert() function above. 55 * 56 * \param condition should be an 'i1' or 'i32' value 57 * \param msg a string to print if the assertion fails. 58 */ 59void 60lp_build_assert(struct gallivm_state *gallivm, 61 LLVMValueRef condition, 62 const char *msg) 63{ 64 LLVMBuilderRef builder = gallivm->builder; 65 LLVMContextRef context = gallivm->context; 66 LLVMTypeRef arg_types[2]; 67 LLVMTypeRef ret_type; 68 LLVMValueRef function; 69 LLVMValueRef args[2]; 70 LLVMValueRef msg_string; 71 72 msg_string = lp_build_const_string(gallivm, msg); 73 74 ret_type = LLVMVoidTypeInContext(context); 75 arg_types[0] = LLVMInt32TypeInContext(context); 76 arg_types[1] = LLVMPointerType(LLVMInt8TypeInContext(context), 0); 77 78 LLVMTypeRef function_type = LLVMFunctionType(ret_type, arg_types, 2, 0); 79 80 function = lp_build_const_func_pointer_from_type(gallivm, 81 func_to_pointer((func_pointer)lp_assert), 82 function_type, 83 "assert"); 84 85 /* build function call param list */ 86 args[0] = LLVMBuildZExt(builder, condition, arg_types[0], ""); 87 args[1] = msg_string; 88 89 /* check arg types */ 90 assert(LLVMTypeOf(args[0]) == arg_types[0]); 91 assert(LLVMTypeOf(args[1]) == arg_types[1]); 92 LLVMBuildCall2(builder, function_type, function, args, ARRAY_SIZE(args), ""); 93} 94