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_init.h"
35 #include "gallivm/lp_bld_assert.h"
36 #include "gallivm/lp_bld_printf.h"
37
38 #include "lp_test.h"
39
40
41 struct printf_test_case {
42 int foo;
43 };
44
45 void
write_tsv_header(FILE * fp)46 write_tsv_header(FILE *fp)
47 {
48 fprintf(fp,
49 "result\t"
50 "format\n");
51
52 fflush(fp);
53 }
54
55
56
57 typedef void (*test_printf_t)(int i);
58
59
60 static LLVMValueRef
add_printf_test(struct gallivm_state * gallivm,int n,char * func_name)61 add_printf_test(struct gallivm_state *gallivm, int n, char *func_name)
62 {
63 LLVMModuleRef module = gallivm->module;
64 LLVMTypeRef args[1] = { LLVMIntTypeInContext(gallivm->context, 32) };
65 snprintf(func_name, 64 * sizeof(char), "test_lookup_multiple_%d", n);
66 LLVMValueRef func = LLVMAddFunction(module, func_name, LLVMFunctionType(LLVMVoidTypeInContext(gallivm->context), args, 1, 0));
67 LLVMBuilderRef builder = gallivm->builder;
68 LLVMBasicBlockRef block = LLVMAppendBasicBlockInContext(gallivm->context, func, "entry");
69
70 LLVMSetFunctionCallConv(func, LLVMCCallConv);
71
72 LLVMPositionBuilderAtEnd(builder, block);
73 lp_build_printf(gallivm, "hello, world from ");
74 lp_build_printf(gallivm, func_name);
75 lp_build_printf(gallivm, "print 5 6: %d %d\n", LLVMConstInt(LLVMInt32TypeInContext(gallivm->context), 5, 0),
76 LLVMConstInt(LLVMInt32TypeInContext(gallivm->context), 6, 0));
77
78 /* Also test lp_build_assert(). This should not fail. */
79 lp_build_assert(gallivm, LLVMConstInt(LLVMInt32TypeInContext(gallivm->context), 1, 0), "assert(1)");
80
81 LLVMBuildRetVoid(builder);
82
83 gallivm_verify_function(gallivm, func);
84
85 return func;
86 }
87
88
89 static bool
test_lookup_multiple(unsigned verbose,FILE * fp,const struct printf_test_case * testcase)90 test_lookup_multiple(unsigned verbose, FILE *fp,
91 const struct printf_test_case *testcase)
92 {
93 #define NUM_FUNC 10
94 struct gallivm_state *gallivm;
95 LLVMValueRef *func =
96 (LLVMValueRef *) malloc(NUM_FUNC * sizeof(LLVMValueRef));
97 char func_name[NUM_FUNC][64];
98 test_printf_t *test_lookup_multiple_func =
99 (test_printf_t *)malloc(NUM_FUNC * sizeof(test_printf_t));
100 bool success = true;
101 int i;
102
103 lp_context_ref context;
104 lp_context_create(&context);
105
106 gallivm = gallivm_create("test_module", &context, NULL);
107
108 for(i = 0; i < NUM_FUNC; i++){
109 func[i] = add_printf_test(gallivm, i, func_name[i]);
110 }
111
112 gallivm_compile_module(gallivm);
113
114 for(i = 0; i < NUM_FUNC; i++){
115 test_lookup_multiple_func[i] = (test_printf_t) gallivm_jit_function(gallivm, func[i], func_name[i]);
116 }
117
118 gallivm_free_ir(gallivm);
119
120 for(i = 0; i < NUM_FUNC; i++){
121 test_lookup_multiple_func[i](0);
122 }
123 FREE(func);
124 FREE(test_lookup_multiple_func);
125 gallivm_destroy(gallivm);
126 lp_context_destroy(&context);
127 return success;
128 }
129
130
131 bool
test_all(unsigned verbose,FILE * fp)132 test_all(unsigned verbose, FILE *fp)
133 {
134 bool success = true;
135
136 test_lookup_multiple(verbose, fp, NULL);
137
138 return success;
139 }
140
141
142 bool
test_some(unsigned verbose,FILE * fp,unsigned long n)143 test_some(unsigned verbose, FILE *fp,
144 unsigned long n)
145 {
146 return test_all(verbose, fp);
147 }
148
149
150 bool
test_single(unsigned verbose,FILE * fp)151 test_single(unsigned verbose, FILE *fp)
152 {
153 printf("no test_single()");
154 return true;
155 }
156