Merge branch 'master' into gallium-new-formats
[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 "lp_bld_printf.h"
31
32
33 static int
34 lp_get_printf_arg_count(const char *fmt)
35 {
36 int count =0;
37 const char *p = fmt;
38 int c;
39
40 while ((c = *p++)) {
41 if (c != '%')
42 continue;
43 switch (*p) {
44 case '\0':
45 continue;
46 case '%':
47 p++;
48 continue;
49 case '.':
50 if (p[1] == '*' && p[2] == 's') {
51 count += 2;
52 p += 3;
53 continue;
54 }
55 default:
56 count ++;
57 }
58 }
59 return count;
60 }
61
62 LLVMValueRef
63 lp_build_const_string_variable(LLVMModuleRef module, const char *str, int len)
64 {
65 LLVMValueRef string = LLVMAddGlobal(module, LLVMArrayType(LLVMInt8Type(), len + 1), "");
66 LLVMSetGlobalConstant(string, TRUE);
67 LLVMSetLinkage(string, LLVMInternalLinkage);
68 LLVMSetInitializer(string, LLVMConstString(str, len + 1, TRUE));
69 return string;
70 }
71
72
73 /**
74 * lp_build_printf.
75 *
76 * Build printf call in LLVM IR. The output goes to stdout.
77 * The additional variable arguments need to have type
78 * LLVMValueRef.
79 */
80 LLVMValueRef
81 lp_build_printf(LLVMBuilderRef builder, const char *fmt, ...)
82 {
83 va_list arglist;
84 int i = 0;
85 int argcount = lp_get_printf_arg_count(fmt);
86 LLVMModuleRef module = LLVMGetGlobalParent(LLVMGetBasicBlockParent(LLVMGetInsertBlock(builder)));
87 LLVMValueRef params[argcount + 1];
88 LLVMValueRef fmtarg = lp_build_const_string_variable(module, fmt, strlen(fmt) + 1);
89 LLVMValueRef int0 = LLVMConstInt(LLVMInt32Type(), 0, 0);
90 LLVMValueRef index[2];
91 LLVMValueRef func_printf = LLVMGetNamedFunction(module, "printf");
92
93 index[0] = index[1] = int0;
94
95 if (!func_printf) {
96 LLVMTypeRef printf_type = LLVMFunctionType(LLVMIntType(32), NULL, 0, 1);
97 func_printf = LLVMAddFunction(module, "printf", printf_type);
98 }
99
100 params[0] = LLVMBuildGEP(builder, fmtarg, index, 2, "");
101
102 va_start(arglist, fmt);
103 for (i = 1; i <= argcount; i++)
104 params[i] = va_arg(arglist, LLVMValueRef);
105 va_end(arglist);
106
107 return LLVMBuildCall(builder, func_printf, params, argcount + 1, "");
108 }
109