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 /**
43 * Generates LLVM IR to call debug_printf.
44 */
45 static LLVMValueRef
lp_build_print_args(struct gallivm_state * gallivm,int argcount,LLVMValueRef * args)46 lp_build_print_args(struct gallivm_state* gallivm,
47 int argcount,
48 LLVMValueRef* args)
49 {
50 LLVMBuilderRef builder = gallivm->builder;
51 LLVMContextRef context = gallivm->context;
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 if (!gallivm->debug_printf_hook) {
67 LLVMTypeRef printf_type = LLVMFunctionType(LLVMInt32TypeInContext(context), NULL, 0, 1);
68 gallivm->debug_printf_hook = LLVMAddFunction(gallivm->module, "debug_printf", printf_type);
69 }
70 return LLVMBuildCall(builder, gallivm->debug_printf_hook, args, argcount, "");
71 }
72
73
74 /**
75 * Print a LLVM value of any type
76 */
77 LLVMValueRef
lp_build_print_value(struct gallivm_state * gallivm,const char * msg,LLVMValueRef value)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) == 64) {
110 snprintf(type_fmt + 2, 3, "%s", PRId64);
111 } else if (LLVMGetIntTypeWidth(type_ref) == 8) {
112 type_fmt[2] = 'u';
113 } else {
114 type_fmt[2] = 'i';
115 }
116 } else if (type_kind == LLVMPointerTypeKind) {
117 type_fmt[2] = 'p';
118 } else {
119 /* Unsupported type */
120 assert(0);
121 }
122
123 /* Create format string and arguments */
124 assert(strlen(format) + strlen(type_fmt) * length + 2 <= sizeof format);
125
126 params[1] = lp_build_const_string(gallivm, msg);
127 if (length == 1) {
128 strncat(format, type_fmt, sizeof(format) - strlen(format) - 1);
129 params[2] = value;
130 } else {
131 for (i = 0; i < length; ++i) {
132 LLVMValueRef param;
133 strncat(format, type_fmt, sizeof(format) - strlen(format) - 1);
134 param = LLVMBuildExtractElement(builder, value, lp_build_const_int32(gallivm, i), "");
135 if (type_kind == LLVMIntegerTypeKind &&
136 LLVMGetIntTypeWidth(type_ref) < sizeof(int) * 8) {
137 LLVMTypeRef int_type = LLVMIntTypeInContext(gallivm->context, sizeof(int) * 8);
138 if (LLVMGetIntTypeWidth(type_ref) == 8) {
139 param = LLVMBuildZExt(builder, param, int_type, "");
140 } else {
141 param = LLVMBuildSExt(builder, param, int_type, "");
142 }
143 }
144 params[2 + i] = param;
145 }
146 }
147
148 strncat(format, "\n", sizeof(format) - strlen(format) - 1);
149
150 params[0] = lp_build_const_string(gallivm, format);
151 return lp_build_print_args(gallivm, 2 + length, params);
152 }
153
154
155 static unsigned
lp_get_printf_arg_count(const char * fmt)156 lp_get_printf_arg_count(const char *fmt)
157 {
158 unsigned count = 0;
159 const char *p = fmt;
160 int c;
161
162 while ((c = *p++)) {
163 if (c != '%')
164 continue;
165 switch (*p) {
166 case '\0':
167 continue;
168 case '%':
169 p++;
170 continue;
171 case '.':
172 if (p[1] == '*' && p[2] == 's') {
173 count += 2;
174 p += 3;
175 continue;
176 }
177 FALLTHROUGH;
178 default:
179 count ++;
180 }
181 }
182 return count;
183 }
184
185
186 /**
187 * Generate LLVM IR for a c style printf
188 */
189 LLVMValueRef
lp_build_printf(struct gallivm_state * gallivm,const char * fmt,...)190 lp_build_printf(struct gallivm_state *gallivm,
191 const char *fmt, ...)
192 {
193 LLVMValueRef params[50];
194 va_list arglist;
195 unsigned argcount, i;
196
197 argcount = lp_get_printf_arg_count(fmt);
198 assert(ARRAY_SIZE(params) >= argcount + 1);
199
200 va_start(arglist, fmt);
201 for (i = 1; i <= argcount; i++) {
202 params[i] = va_arg(arglist, LLVMValueRef);
203 }
204 va_end(arglist);
205
206 params[0] = lp_build_const_string(gallivm, fmt);
207 return lp_build_print_args(gallivm, argcount + 1, params);
208 }
209