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