1324da26a71a5ebc024c8701053dad3be76bcfcb
[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 #include "lp_bld_type.h"
38
39
40 /**
41 * Generates LLVM IR to call debug_printf.
42 */
43 static LLVMValueRef
44 lp_build_print_args(struct gallivm_state* gallivm,
45 int argcount,
46 LLVMValueRef* args)
47 {
48 LLVMBuilderRef builder = gallivm->builder;
49 LLVMContextRef context = gallivm->context;
50 LLVMValueRef func_printf;
51 LLVMTypeRef printf_type;
52 int i;
53
54 assert(args);
55 assert(argcount > 0);
56 assert(LLVMTypeOf(args[0]) == LLVMPointerType(LLVMInt8TypeInContext(context), 0));
57
58 /* Cast any float arguments to doubles as printf expects */
59 for (i = 1; i < argcount; i++) {
60 LLVMTypeRef type = LLVMTypeOf(args[i]);
61
62 if (LLVMGetTypeKind(type) == LLVMFloatTypeKind)
63 args[i] = LLVMBuildFPExt(builder, args[i], LLVMDoubleTypeInContext(context), "");
64 }
65
66 printf_type = LLVMFunctionType(LLVMInt32TypeInContext(context), NULL, 0, 1);
67 func_printf = lp_build_const_int_pointer(gallivm, func_to_pointer((func_pointer)debug_printf));
68 func_printf = LLVMBuildBitCast(builder, func_printf, LLVMPointerType(printf_type, 0), "debug_printf");
69
70 return LLVMBuildCall(builder, func_printf, args, argcount, "");
71 }
72
73
74 /**
75 * Print a LLVM value of any type
76 */
77 LLVMValueRef
78 lp_build_print_value(struct gallivm_state *gallivm,
79 const char *msg,
80 LLVMValueRef value)
81 {
82 LLVMBuilderRef builder = gallivm->builder;
83 LLVMTypeKind type_kind;
84 LLVMTypeRef type_ref;
85 LLVMValueRef params[2 + LP_MAX_VECTOR_LENGTH];
86 char type_fmt[6] = " %x";
87 char format[2 + 5 * LP_MAX_VECTOR_LENGTH + 2] = "%s";
88 unsigned length;
89 unsigned i;
90
91 type_ref = LLVMTypeOf(value);
92 type_kind = LLVMGetTypeKind(type_ref);
93
94 if (type_kind == LLVMVectorTypeKind) {
95 length = LLVMGetVectorSize(type_ref);
96
97 type_ref = LLVMGetElementType(type_ref);
98 type_kind = LLVMGetTypeKind(type_ref);
99 } else {
100 length = 1;
101 }
102
103 if (type_kind == LLVMFloatTypeKind || type_kind == LLVMDoubleTypeKind) {
104 type_fmt[2] = '.';
105 type_fmt[3] = '9';
106 type_fmt[4] = 'g';
107 type_fmt[5] = '\0';
108 } else if (type_kind == LLVMIntegerTypeKind) {
109 if (LLVMGetIntTypeWidth(type_ref) == 8) {
110 type_fmt[2] = 'u';
111 } else {
112 type_fmt[2] = 'i';
113 }
114 } else if (type_kind == LLVMPointerTypeKind) {
115 type_fmt[2] = 'p';
116 } else {
117 /* Unsupported type */
118 assert(0);
119 }
120
121 /* Create format string and arguments */
122 assert(strlen(format) + strlen(type_fmt) * length + 2 <= sizeof format);
123
124 params[1] = lp_build_const_string(gallivm, msg);
125 if (length == 1) {
126 util_strncat(format, type_fmt, sizeof(format) - strlen(format) - 1);
127 params[2] = value;
128 } else {
129 for (i = 0; i < length; ++i) {
130 LLVMValueRef param;
131 util_strncat(format, type_fmt, sizeof(format) - strlen(format) - 1);
132 param = LLVMBuildExtractElement(builder, value, lp_build_const_int32(gallivm, i), "");
133 if (type_kind == LLVMIntegerTypeKind &&
134 LLVMGetIntTypeWidth(type_ref) < sizeof(int) * 8) {
135 LLVMTypeRef int_type = LLVMIntTypeInContext(gallivm->context, sizeof(int) * 8);
136 if (LLVMGetIntTypeWidth(type_ref) == 8) {
137 param = LLVMBuildZExt(builder, param, int_type, "");
138 } else {
139 param = LLVMBuildSExt(builder, param, int_type, "");
140 }
141 }
142 params[2 + i] = param;
143 }
144 }
145
146 util_strncat(format, "\n", sizeof(format) - strlen(format) - 1);
147
148 params[0] = lp_build_const_string(gallivm, format);
149 return lp_build_print_args(gallivm, 2 + length, params);
150 }
151
152
153 static int
154 lp_get_printf_arg_count(const char *fmt)
155 {
156 int count =0;
157 const char *p = fmt;
158 int c;
159
160 while ((c = *p++)) {
161 if (c != '%')
162 continue;
163 switch (*p) {
164 case '\0':
165 continue;
166 case '%':
167 p++;
168 continue;
169 case '.':
170 if (p[1] == '*' && p[2] == 's') {
171 count += 2;
172 p += 3;
173 continue;
174 }
175 /* fallthrough */
176 default:
177 count ++;
178 }
179 }
180 return count;
181 }
182
183
184 /**
185 * Generate LLVM IR for a c style printf
186 */
187 LLVMValueRef
188 lp_build_printf(struct gallivm_state *gallivm,
189 const char *fmt, ...)
190 {
191 LLVMValueRef params[50];
192 va_list arglist;
193 int argcount;
194 int i;
195
196 argcount = lp_get_printf_arg_count(fmt);
197 assert(Elements(params) >= argcount + 1);
198
199 va_start(arglist, fmt);
200 for (i = 1; i <= argcount; i++) {
201 params[i] = va_arg(arglist, LLVMValueRef);
202 }
203 va_end(arglist);
204
205 params[0] = lp_build_const_string(gallivm, fmt);
206 return lp_build_print_args(gallivm, argcount + 1, params);
207 }