Merge remote branch 'origin/7.8'
[mesa.git] / src / gallium / drivers / llvmpipe / lp_test_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
29 #include <stdlib.h>
30 #include <stdio.h>
31
32 #include "gallivm/lp_bld.h"
33 #include "gallivm/lp_bld_printf.h"
34
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>
39
40 #include "lp_test.h"
41
42
43 struct printf_test_case {
44 };
45
46 void
47 write_tsv_header(FILE *fp)
48 {
49 fprintf(fp,
50 "result\t"
51 "format\n");
52
53 fflush(fp);
54 }
55
56
57
58 typedef void (*test_printf_t)(int i);
59
60 static LLVMValueRef
61 add_printf_test(LLVMModuleRef module)
62 {
63 LLVMTypeRef args[1] = { LLVMIntType(32) };
64 LLVMValueRef func = LLVMAddFunction(module, "test_printf", LLVMFunctionType(LLVMVoidType(), args, 1, 0));
65 LLVMBuilderRef builder = LLVMCreateBuilder();
66 LLVMBasicBlockRef block = LLVMAppendBasicBlock(func, "entry");
67
68 LLVMSetFunctionCallConv(func, LLVMCCallConv);
69
70 LLVMPositionBuilderAtEnd(builder, block);
71 lp_build_printf(builder, "hello, world\n");
72 lp_build_printf(builder, "print 5 6: %d %d\n", LLVMConstInt(LLVMInt32Type(), 5, 0),
73 LLVMConstInt(LLVMInt32Type(), 6, 0));
74 LLVMBuildRetVoid(builder);
75 LLVMDisposeBuilder(builder);
76 return func;
77 }
78
79
80 PIPE_ALIGN_STACK
81 static boolean
82 test_printf(unsigned verbose, FILE *fp, const struct printf_test_case *testcase)
83 {
84 LLVMModuleRef module = NULL;
85 LLVMValueRef test = NULL;
86 LLVMExecutionEngineRef engine = NULL;
87 LLVMModuleProviderRef provider = NULL;
88 LLVMPassManagerRef pass = NULL;
89 char *error = NULL;
90 test_printf_t test_printf;
91 float unpacked[4];
92 unsigned packed;
93 boolean success = TRUE;
94
95 module = LLVMModuleCreateWithName("test");
96
97 test = add_printf_test(module);
98
99 if(LLVMVerifyModule(module, LLVMPrintMessageAction, &error)) {
100 LLVMDumpModule(module);
101 abort();
102 }
103 LLVMDisposeMessage(error);
104
105 provider = LLVMCreateModuleProviderForExistingModule(module);
106 if (LLVMCreateJITCompiler(&engine, provider, 1, &error)) {
107 fprintf(stderr, "%s\n", error);
108 LLVMDisposeMessage(error);
109 abort();
110 }
111
112 #if 0
113 pass = LLVMCreatePassManager();
114 LLVMAddTargetData(LLVMGetExecutionEngineTargetData(engine), pass);
115 /* These are the passes currently listed in llvm-c/Transforms/Scalar.h,
116 * but there are more on SVN. */
117 LLVMAddConstantPropagationPass(pass);
118 LLVMAddInstructionCombiningPass(pass);
119 LLVMAddPromoteMemoryToRegisterPass(pass);
120 LLVMAddGVNPass(pass);
121 LLVMAddCFGSimplificationPass(pass);
122 LLVMRunPassManager(pass, module);
123 #else
124 (void)pass;
125 #endif
126
127 test_printf = (test_printf_t)LLVMGetPointerToGlobal(engine, test);
128
129 memset(unpacked, 0, sizeof unpacked);
130 packed = 0;
131
132
133 // LLVMDumpModule(module);
134
135 test_printf(0);
136
137 LLVMFreeMachineCodeForFunction(engine, test);
138
139 LLVMDisposeExecutionEngine(engine);
140 if(pass)
141 LLVMDisposePassManager(pass);
142
143 return success;
144 }
145
146
147 boolean
148 test_all(unsigned verbose, FILE *fp)
149 {
150 bool success = TRUE;
151
152 test_printf(verbose, fp, NULL);
153
154 return success;
155 }
156
157
158 boolean
159 test_some(unsigned verbose, FILE *fp, unsigned long n)
160 {
161 return test_all(verbose, fp);
162 }