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