21df83f9d896b663a8b0cb9a89e58be4b2eccf87
[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 "util/u_pointer.h"
33 #include "gallivm/lp_bld.h"
34 #include "gallivm/lp_bld_printf.h"
35
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>
40
41 #include "lp_test.h"
42
43
44 struct printf_test_case {
45 int foo;
46 };
47
48 void
49 write_tsv_header(FILE *fp)
50 {
51 fprintf(fp,
52 "result\t"
53 "format\n");
54
55 fflush(fp);
56 }
57
58
59
60 typedef void (*test_printf_t)(int i);
61
62
63 static LLVMValueRef
64 add_printf_test(LLVMModuleRef module)
65 {
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");
70
71 LLVMSetFunctionCallConv(func, LLVMCCallConv);
72
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);
79 return func;
80 }
81
82
83 PIPE_ALIGN_STACK
84 static boolean
85 test_printf(unsigned verbose, FILE *fp, const struct printf_test_case *testcase)
86 {
87 LLVMModuleRef module = NULL;
88 LLVMValueRef test = NULL;
89 LLVMExecutionEngineRef engine = NULL;
90 LLVMModuleProviderRef provider = NULL;
91 LLVMPassManagerRef pass = NULL;
92 char *error = NULL;
93 test_printf_t test_printf;
94 float unpacked[4];
95 unsigned packed;
96 boolean success = TRUE;
97 void *code;
98
99 module = LLVMModuleCreateWithName("test");
100
101 test = add_printf_test(module);
102
103 if(LLVMVerifyModule(module, LLVMPrintMessageAction, &error)) {
104 LLVMDumpModule(module);
105 abort();
106 }
107 LLVMDisposeMessage(error);
108
109 provider = LLVMCreateModuleProviderForExistingModule(module);
110 if (LLVMCreateJITCompiler(&engine, provider, 1, &error)) {
111 fprintf(stderr, "%s\n", error);
112 LLVMDisposeMessage(error);
113 abort();
114 }
115
116 #if 0
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);
127 #else
128 (void)pass;
129 #endif
130
131 code = LLVMGetPointerToGlobal(engine, test);
132 test_printf = (test_printf_t)pointer_to_func(code);
133
134 memset(unpacked, 0, sizeof unpacked);
135 packed = 0;
136
137
138 // LLVMDumpModule(module);
139
140 test_printf(0);
141
142 LLVMFreeMachineCodeForFunction(engine, test);
143
144 LLVMDisposeExecutionEngine(engine);
145 if(pass)
146 LLVMDisposePassManager(pass);
147
148 return success;
149 }
150
151
152 boolean
153 test_all(unsigned verbose, FILE *fp)
154 {
155 boolean success = TRUE;
156
157 test_printf(verbose, fp, NULL);
158
159 return success;
160 }
161
162
163 boolean
164 test_some(unsigned verbose, FILE *fp, unsigned long n)
165 {
166 return test_all(verbose, fp);
167 }
168
169
170 boolean
171 test_single(unsigned verbose, FILE *fp)
172 {
173 printf("no test_single()");
174 return TRUE;
175 }