Merge branch 'master' into pipe-video
[mesa.git] / src / gallium / auxiliary / gallivm / lp_bld_printf.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 <stdio.h>
29
30 #include "util/u_debug.h"
31 #include "util/u_memory.h"
32 #include "util/u_string.h"
33 #include "lp_bld_const.h"
34 #include "lp_bld_printf.h"
35
36
37 static int
38 lp_get_printf_arg_count(const char *fmt)
39 {
40 int count =0;
41 const char *p = fmt;
42 int c;
43
44 while ((c = *p++)) {
45 if (c != '%')
46 continue;
47 switch (*p) {
48 case '\0':
49 continue;
50 case '%':
51 p++;
52 continue;
53 case '.':
54 if (p[1] == '*' && p[2] == 's') {
55 count += 2;
56 p += 3;
57 continue;
58 }
59 /* fallthrough */
60 default:
61 count ++;
62 }
63 }
64 return count;
65 }
66
67 LLVMValueRef
68 lp_build_const_string_variable(LLVMModuleRef module, const char *str, int len)
69 {
70 LLVMValueRef string = LLVMAddGlobal(module, LLVMArrayType(LLVMInt8Type(), len + 1), "");
71 LLVMSetGlobalConstant(string, TRUE);
72 LLVMSetLinkage(string, LLVMInternalLinkage);
73 LLVMSetInitializer(string, LLVMConstString(str, len + 1, TRUE));
74 return string;
75 }
76
77
78 /**
79 * lp_build_printf.
80 *
81 * Build printf call in LLVM IR. The output goes to stdout.
82 * The additional variable arguments need to have type
83 * LLVMValueRef.
84 */
85 LLVMValueRef
86 lp_build_printf(LLVMBuilderRef builder, const char *fmt, ...)
87 {
88 va_list arglist;
89 int i = 0;
90 int argcount = lp_get_printf_arg_count(fmt);
91 LLVMModuleRef module = LLVMGetGlobalParent(LLVMGetBasicBlockParent(LLVMGetInsertBlock(builder)));
92 LLVMValueRef params[50];
93 LLVMValueRef fmtarg = lp_build_const_string_variable(module, fmt, strlen(fmt) + 1);
94 LLVMValueRef int0 = LLVMConstInt(LLVMInt32Type(), 0, 0);
95 LLVMValueRef index[2];
96 LLVMValueRef func_printf = LLVMGetNamedFunction(module, "printf");
97
98 assert(Elements(params) >= argcount + 1);
99
100 index[0] = index[1] = int0;
101
102 if (!func_printf) {
103 LLVMTypeRef printf_type = LLVMFunctionType(LLVMIntType(32), NULL, 0, 1);
104 func_printf = LLVMAddFunction(module, "printf", printf_type);
105 }
106
107 params[0] = LLVMBuildGEP(builder, fmtarg, index, 2, "");
108
109 va_start(arglist, fmt);
110 for (i = 1; i <= argcount; i++) {
111 LLVMValueRef val = va_arg(arglist, LLVMValueRef);
112 LLVMTypeRef type = LLVMTypeOf(val);
113 /* printf wants doubles, so lets convert so that
114 * we can actually print them */
115 if (LLVMGetTypeKind(type) == LLVMFloatTypeKind)
116 val = LLVMBuildFPExt(builder, val, LLVMDoubleType(), "");
117 params[i] = val;
118 }
119 va_end(arglist);
120
121 return LLVMBuildCall(builder, func_printf, params, argcount + 1, "");
122 }
123
124
125
126 /**
127 * Print a float[4] vector.
128 */
129 LLVMValueRef
130 lp_build_print_vec4(LLVMBuilderRef builder, const char *msg, LLVMValueRef vec)
131 {
132 char format[1000];
133 LLVMValueRef x, y, z, w;
134
135 x = LLVMBuildExtractElement(builder, vec, lp_build_const_int32(0), "");
136 y = LLVMBuildExtractElement(builder, vec, lp_build_const_int32(1), "");
137 z = LLVMBuildExtractElement(builder, vec, lp_build_const_int32(2), "");
138 w = LLVMBuildExtractElement(builder, vec, lp_build_const_int32(3), "");
139
140 util_snprintf(format, sizeof(format), "%s %%f %%f %%f %%f\n", msg);
141 return lp_build_printf(builder, format, x, y, z, w);
142 }