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 "util/u_pointer.h"
33 #include "gallivm/lp_bld.h"
34 #include "gallivm/lp_bld_printf.h"
36 #include <llvm-c/Analysis.h>
37 #include <llvm-c/ExecutionEngine.h>
38 #include <llvm-c/Target.h>
39 #include <llvm-c/Transforms/Scalar.h>
44 struct printf_test_case
{
49 write_tsv_header(FILE *fp
)
60 typedef void (*test_printf_t
)(int i
);
64 add_printf_test(LLVMModuleRef module
)
66 LLVMTypeRef args
[1] = { LLVMIntType(32) };
67 LLVMValueRef func
= LLVMAddFunction(module
, "test_printf", LLVMFunctionType(LLVMVoidType(), args
, 1, 0));
68 LLVMBuilderRef builder
= LLVMCreateBuilder();
69 LLVMBasicBlockRef block
= LLVMAppendBasicBlock(func
, "entry");
71 LLVMSetFunctionCallConv(func
, LLVMCCallConv
);
73 LLVMPositionBuilderAtEnd(builder
, block
);
74 lp_build_printf(builder
, "hello, world\n");
75 lp_build_printf(builder
, "print 5 6: %d %d\n", LLVMConstInt(LLVMInt32Type(), 5, 0),
76 LLVMConstInt(LLVMInt32Type(), 6, 0));
77 LLVMBuildRetVoid(builder
);
78 LLVMDisposeBuilder(builder
);
85 test_printf(unsigned verbose
, FILE *fp
, const struct printf_test_case
*testcase
)
87 LLVMModuleRef module
= NULL
;
88 LLVMValueRef test
= NULL
;
89 LLVMExecutionEngineRef engine
= NULL
;
90 LLVMModuleProviderRef provider
= NULL
;
91 LLVMPassManagerRef pass
= NULL
;
93 test_printf_t test_printf
;
96 boolean success
= TRUE
;
99 module
= LLVMModuleCreateWithName("test");
101 test
= add_printf_test(module
);
103 if(LLVMVerifyModule(module
, LLVMPrintMessageAction
, &error
)) {
104 LLVMDumpModule(module
);
107 LLVMDisposeMessage(error
);
109 provider
= LLVMCreateModuleProviderForExistingModule(module
);
110 if (LLVMCreateJITCompiler(&engine
, provider
, 1, &error
)) {
111 fprintf(stderr
, "%s\n", error
);
112 LLVMDisposeMessage(error
);
117 pass
= LLVMCreatePassManager();
118 LLVMAddTargetData(LLVMGetExecutionEngineTargetData(engine
), pass
);
119 /* These are the passes currently listed in llvm-c/Transforms/Scalar.h,
120 * but there are more on SVN. */
121 LLVMAddConstantPropagationPass(pass
);
122 LLVMAddInstructionCombiningPass(pass
);
123 LLVMAddPromoteMemoryToRegisterPass(pass
);
124 LLVMAddGVNPass(pass
);
125 LLVMAddCFGSimplificationPass(pass
);
126 LLVMRunPassManager(pass
, module
);
131 code
= LLVMGetPointerToGlobal(engine
, test
);
132 test_printf
= (test_printf_t
)pointer_to_func(code
);
134 memset(unpacked
, 0, sizeof unpacked
);
138 // LLVMDumpModule(module);
142 LLVMFreeMachineCodeForFunction(engine
, test
);
144 LLVMDisposeExecutionEngine(engine
);
146 LLVMDisposePassManager(pass
);
153 test_all(unsigned verbose
, FILE *fp
)
155 boolean success
= TRUE
;
157 test_printf(verbose
, fp
, NULL
);
164 test_some(unsigned verbose
, FILE *fp
, unsigned long n
)
166 return test_all(verbose
, fp
);
171 test_single(unsigned verbose
, FILE *fp
)
173 printf("no test_single()");