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 **************************************************************************/
32 #include "gallivm/lp_bld.h"
33 #include "gallivm/lp_bld_printf.h"
35 #include <llvm-c/Analysis.h>
36 #include <llvm-c/ExecutionEngine.h>
37 #include <llvm-c/Target.h>
38 #include <llvm-c/Transforms/Scalar.h>
43 struct printf_test_case
{
48 write_tsv_header(FILE *fp
)
59 typedef void (*test_printf_t
)(int i
);
63 voidptr_to_test_printf_t(void *p
)
75 add_printf_test(LLVMModuleRef module
)
77 LLVMTypeRef args
[1] = { LLVMIntType(32) };
78 LLVMValueRef func
= LLVMAddFunction(module
, "test_printf", LLVMFunctionType(LLVMVoidType(), args
, 1, 0));
79 LLVMBuilderRef builder
= LLVMCreateBuilder();
80 LLVMBasicBlockRef block
= LLVMAppendBasicBlock(func
, "entry");
82 LLVMSetFunctionCallConv(func
, LLVMCCallConv
);
84 LLVMPositionBuilderAtEnd(builder
, block
);
85 lp_build_printf(builder
, "hello, world\n");
86 lp_build_printf(builder
, "print 5 6: %d %d\n", LLVMConstInt(LLVMInt32Type(), 5, 0),
87 LLVMConstInt(LLVMInt32Type(), 6, 0));
88 LLVMBuildRetVoid(builder
);
89 LLVMDisposeBuilder(builder
);
96 test_printf(unsigned verbose
, FILE *fp
, const struct printf_test_case
*testcase
)
98 LLVMModuleRef module
= NULL
;
99 LLVMValueRef test
= NULL
;
100 LLVMExecutionEngineRef engine
= NULL
;
101 LLVMModuleProviderRef provider
= NULL
;
102 LLVMPassManagerRef pass
= NULL
;
104 test_printf_t test_printf
;
107 boolean success
= TRUE
;
110 module
= LLVMModuleCreateWithName("test");
112 test
= add_printf_test(module
);
114 if(LLVMVerifyModule(module
, LLVMPrintMessageAction
, &error
)) {
115 LLVMDumpModule(module
);
118 LLVMDisposeMessage(error
);
120 provider
= LLVMCreateModuleProviderForExistingModule(module
);
121 if (LLVMCreateJITCompiler(&engine
, provider
, 1, &error
)) {
122 fprintf(stderr
, "%s\n", error
);
123 LLVMDisposeMessage(error
);
128 pass
= LLVMCreatePassManager();
129 LLVMAddTargetData(LLVMGetExecutionEngineTargetData(engine
), pass
);
130 /* These are the passes currently listed in llvm-c/Transforms/Scalar.h,
131 * but there are more on SVN. */
132 LLVMAddConstantPropagationPass(pass
);
133 LLVMAddInstructionCombiningPass(pass
);
134 LLVMAddPromoteMemoryToRegisterPass(pass
);
135 LLVMAddGVNPass(pass
);
136 LLVMAddCFGSimplificationPass(pass
);
137 LLVMRunPassManager(pass
, module
);
142 code
= LLVMGetPointerToGlobal(engine
, test
);
143 test_printf
= voidptr_to_test_printf_t(code
);
145 memset(unpacked
, 0, sizeof unpacked
);
149 // LLVMDumpModule(module);
153 LLVMFreeMachineCodeForFunction(engine
, test
);
155 LLVMDisposeExecutionEngine(engine
);
157 LLVMDisposePassManager(pass
);
164 test_all(unsigned verbose
, FILE *fp
)
166 boolean success
= TRUE
;
168 test_printf(verbose
, fp
, NULL
);
175 test_some(unsigned verbose
, FILE *fp
, unsigned long n
)
177 return test_all(verbose
, fp
);