Merge branch 'master' into glsl2
[mesa.git] / src / gallium / auxiliary / gallivm / lp_bld_assert.c
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_printf.h"
33
34
35 /**
36 * A call to lp_build_assert() will build a function call to this function.
37 */
38 static void
39 lp_assert(int condition, const char *msg)
40 {
41 if (!condition) {
42 debug_printf("LLVM assertion '%s' failed!\n", msg);
43 assert(condition);
44 }
45 }
46
47
48
49 /**
50 * lp_build_assert.
51 *
52 * Build an assertion in LLVM IR by building a function call to the
53 * lp_assert() function above.
54 *
55 * \param condition should be an 'i1' or 'i32' value
56 * \param msg a string to print if the assertion fails.
57 */
58 LLVMValueRef
59 lp_build_assert(LLVMBuilderRef builder, LLVMValueRef condition,
60 const char *msg)
61 {
62 LLVMModuleRef module;
63 LLVMTypeRef arg_types[2];
64 LLVMValueRef msg_string, assert_func, params[2], r;
65
66 module = LLVMGetGlobalParent(LLVMGetBasicBlockParent(
67 LLVMGetInsertBlock(builder)));
68
69 msg_string = lp_build_const_string_variable(module, msg, strlen(msg) + 1);
70
71 arg_types[0] = LLVMInt32Type();
72 arg_types[1] = LLVMPointerType(LLVMInt8Type(), 0);
73
74 /* lookup the lp_assert function */
75 assert_func = LLVMGetNamedFunction(module, "lp_assert");
76
77 /* Create the assertion function if not found */
78 if (!assert_func) {
79 LLVMTypeRef func_type =
80 LLVMFunctionType(LLVMVoidType(), arg_types, 2, 0);
81
82 assert_func = LLVMAddFunction(module, "lp_assert", func_type);
83 LLVMSetFunctionCallConv(assert_func, LLVMCCallConv);
84 LLVMSetLinkage(assert_func, LLVMExternalLinkage);
85 LLVMAddGlobalMapping(lp_build_engine, assert_func,
86 func_to_pointer((func_pointer)lp_assert));
87 }
88 assert(assert_func);
89
90 /* build function call param list */
91 params[0] = LLVMBuildZExt(builder, condition, arg_types[0], "");
92 params[1] = LLVMBuildBitCast(builder, msg_string, arg_types[1], "");
93
94 /* check arg types */
95 assert(LLVMTypeOf(params[0]) == arg_types[0]);
96 assert(LLVMTypeOf(params[1]) == arg_types[1]);
97
98 r = LLVMBuildCall(builder, assert_func, params, 2, "");
99
100 return r;
101 }