1 /**************************************************************************
3 * Copyright 2010 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 #include "util/u_debug.h"
31 #include "util/u_memory.h"
32 #include "lp_bld_printf.h"
36 lp_get_printf_arg_count(const char *fmt
)
52 if (p
[1] == '*' && p
[2] == 's') {
66 lp_build_const_string_variable(LLVMModuleRef module
, const char *str
, int len
)
68 LLVMValueRef string
= LLVMAddGlobal(module
, LLVMArrayType(LLVMInt8Type(), len
+ 1), "");
69 LLVMSetGlobalConstant(string
, TRUE
);
70 LLVMSetLinkage(string
, LLVMInternalLinkage
);
71 LLVMSetInitializer(string
, LLVMConstString(str
, len
+ 1, TRUE
));
79 * Build printf call in LLVM IR. The output goes to stdout.
80 * The additional variable arguments need to have type
84 lp_build_printf(LLVMBuilderRef builder
, const char *fmt
, ...)
88 int argcount
= lp_get_printf_arg_count(fmt
);
89 LLVMModuleRef module
= LLVMGetGlobalParent(LLVMGetBasicBlockParent(LLVMGetInsertBlock(builder
)));
90 LLVMValueRef params
[50];
91 LLVMValueRef fmtarg
= lp_build_const_string_variable(module
, fmt
, strlen(fmt
) + 1);
92 LLVMValueRef int0
= LLVMConstInt(LLVMInt32Type(), 0, 0);
93 LLVMValueRef index
[2];
94 LLVMValueRef func_printf
= LLVMGetNamedFunction(module
, "printf");
96 assert(Elements(params
) >= argcount
+ 1);
98 index
[0] = index
[1] = int0
;
101 LLVMTypeRef printf_type
= LLVMFunctionType(LLVMIntType(32), NULL
, 0, 1);
102 func_printf
= LLVMAddFunction(module
, "printf", printf_type
);
105 params
[0] = LLVMBuildGEP(builder
, fmtarg
, index
, 2, "");
107 va_start(arglist
, fmt
);
108 for (i
= 1; i
<= argcount
; i
++) {
109 LLVMValueRef val
= va_arg(arglist
, LLVMValueRef
);
110 LLVMTypeRef type
= LLVMTypeOf(val
);
111 /* printf wants doubles, so lets convert so that
112 * we can actually print them */
113 if (LLVMGetTypeKind(type
) == LLVMFloatTypeKind
)
114 val
= LLVMBuildFPExt(builder
, val
, LLVMDoubleType(), "");
119 return LLVMBuildCall(builder
, func_printf
, params
, argcount
+ 1, "");