Path: blob/21.2-virgl/src/gallium/auxiliary/gallivm/lp_bld_assert.c
4565 views
/**************************************************************************1*2* Copyright 2010 VMware, Inc.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.20* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR21* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25**************************************************************************/2627#include "util/u_debug.h"28#include "util/u_memory.h"29#include "lp_bld_assert.h"30#include "lp_bld_init.h"31#include "lp_bld_const.h"32#include "lp_bld_printf.h"333435/**36* A call to lp_build_assert() will build a function call to this function.37*/38static void39lp_assert(int condition, const char *msg)40{41if (!condition) {42debug_printf("LLVM assertion '%s' failed!\n", msg);43assert(condition);44}45}46474849/**50* lp_build_assert.51*52* Build an assertion in LLVM IR by building a function call to the53* lp_assert() function above.54*55* \param condition should be an 'i1' or 'i32' value56* \param msg a string to print if the assertion fails.57*/58void59lp_build_assert(struct gallivm_state *gallivm,60LLVMValueRef condition,61const char *msg)62{63LLVMBuilderRef builder = gallivm->builder;64LLVMContextRef context = gallivm->context;65LLVMTypeRef arg_types[2];66LLVMTypeRef ret_type;67LLVMValueRef function;68LLVMValueRef args[2];69LLVMValueRef msg_string;7071msg_string = lp_build_const_string(gallivm, msg);7273ret_type = LLVMVoidTypeInContext(context);74arg_types[0] = LLVMInt32TypeInContext(context);75arg_types[1] = LLVMPointerType(LLVMInt8TypeInContext(context), 0);7677function = lp_build_const_func_pointer(gallivm,78func_to_pointer((func_pointer)lp_assert),79ret_type, arg_types, ARRAY_SIZE(arg_types),80"assert");8182/* build function call param list */83args[0] = LLVMBuildZExt(builder, condition, arg_types[0], "");84args[1] = msg_string;8586/* check arg types */87assert(LLVMTypeOf(args[0]) == arg_types[0]);88assert(LLVMTypeOf(args[1]) == arg_types[1]);8990LLVMBuildCall(builder, function, args, ARRAY_SIZE(args), "");91}929394