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