Tizen 2.0 Release
[profile/ivi/osmesa.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_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
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
61 add_printf_test(struct gallivm_state *gallivm)
62 {
63    LLVMModuleRef module = gallivm->module;
64    LLVMTypeRef args[1] = { LLVMIntTypeInContext(gallivm->context, 32) };
65    LLVMValueRef func = LLVMAddFunction(module, "test_printf", LLVMFunctionType(LLVMVoidTypeInContext(gallivm->context), args, 1, 0));
66    LLVMBuilderRef builder = gallivm->builder;
67    LLVMBasicBlockRef block = LLVMAppendBasicBlockInContext(gallivm->context, func, "entry");
68
69    LLVMSetFunctionCallConv(func, LLVMCCallConv);
70
71    LLVMPositionBuilderAtEnd(builder, block);
72    lp_build_printf(gallivm, "hello, world\n");
73    lp_build_printf(gallivm, "print 5 6: %d %d\n", LLVMConstInt(LLVMInt32TypeInContext(gallivm->context), 5, 0),
74                                 LLVMConstInt(LLVMInt32TypeInContext(gallivm->context), 6, 0));
75
76    /* Also test lp_build_assert().  This should not fail. */
77    lp_build_assert(gallivm, LLVMConstInt(LLVMInt32TypeInContext(gallivm->context), 1, 0), "assert(1)");
78
79    LLVMBuildRetVoid(builder);
80
81    return func;
82 }
83
84
85 PIPE_ALIGN_STACK
86 static boolean
87 test_printf(struct gallivm_state *gallivm,
88             unsigned verbose, FILE *fp,
89             const struct printf_test_case *testcase)
90 {
91    LLVMExecutionEngineRef engine = gallivm->engine;
92    LLVMModuleRef module = gallivm->module;
93    LLVMValueRef test;
94    char *error = NULL;
95    test_printf_t test_printf_func;
96    boolean success = TRUE;
97    void *code;
98
99    test = add_printf_test(gallivm);
100
101    if(LLVMVerifyModule(module, LLVMPrintMessageAction, &error)) {
102       LLVMDumpModule(module);
103       abort();
104    }
105    LLVMDisposeMessage(error);
106
107    code = LLVMGetPointerToGlobal(engine, test);
108    test_printf_func = (test_printf_t) pointer_to_func(code);
109
110    // LLVMDumpModule(module);
111
112    test_printf_func(0);
113
114    LLVMFreeMachineCodeForFunction(engine, test);
115
116    return success;
117 }
118
119
120 boolean
121 test_all(struct gallivm_state *gallivm, unsigned verbose, FILE *fp)
122 {
123    boolean success = TRUE;
124
125    test_printf(gallivm, verbose, fp, NULL);
126
127    return success;
128 }
129
130
131 boolean
132 test_some(struct gallivm_state *gallivm, unsigned verbose, FILE *fp,
133           unsigned long n)
134 {
135    return test_all(gallivm, verbose, fp);
136 }
137
138
139 boolean
140 test_single(struct gallivm_state *gallivm, unsigned verbose, FILE *fp)
141 {
142    printf("no test_single()");
143    return TRUE;
144 }