1bf215546Sopenharmony_ci/************************************************************************** 2bf215546Sopenharmony_ci * 3bf215546Sopenharmony_ci * Copyright 2010 VMware, Inc. 4bf215546Sopenharmony_ci * All Rights Reserved. 5bf215546Sopenharmony_ci * 6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the 8bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including 9bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish, 10bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to 11bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to 12bf215546Sopenharmony_ci * the following conditions: 13bf215546Sopenharmony_ci * 14bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the 15bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions 16bf215546Sopenharmony_ci * of the Software. 17bf215546Sopenharmony_ci * 18bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 21bf215546Sopenharmony_ci * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR 22bf215546Sopenharmony_ci * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23bf215546Sopenharmony_ci * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24bf215546Sopenharmony_ci * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25bf215546Sopenharmony_ci * 26bf215546Sopenharmony_ci **************************************************************************/ 27bf215546Sopenharmony_ci 28bf215546Sopenharmony_ci 29bf215546Sopenharmony_ci#include <stdlib.h> 30bf215546Sopenharmony_ci#include <stdio.h> 31bf215546Sopenharmony_ci 32bf215546Sopenharmony_ci#include "util/u_pointer.h" 33bf215546Sopenharmony_ci#include "gallivm/lp_bld.h" 34bf215546Sopenharmony_ci#include "gallivm/lp_bld_init.h" 35bf215546Sopenharmony_ci#include "gallivm/lp_bld_assert.h" 36bf215546Sopenharmony_ci#include "gallivm/lp_bld_printf.h" 37bf215546Sopenharmony_ci 38bf215546Sopenharmony_ci#include "lp_test.h" 39bf215546Sopenharmony_ci 40bf215546Sopenharmony_ci 41bf215546Sopenharmony_cistruct printf_test_case { 42bf215546Sopenharmony_ci int foo; 43bf215546Sopenharmony_ci}; 44bf215546Sopenharmony_ci 45bf215546Sopenharmony_civoid 46bf215546Sopenharmony_ciwrite_tsv_header(FILE *fp) 47bf215546Sopenharmony_ci{ 48bf215546Sopenharmony_ci fprintf(fp, 49bf215546Sopenharmony_ci "result\t" 50bf215546Sopenharmony_ci "format\n"); 51bf215546Sopenharmony_ci 52bf215546Sopenharmony_ci fflush(fp); 53bf215546Sopenharmony_ci} 54bf215546Sopenharmony_ci 55bf215546Sopenharmony_ci 56bf215546Sopenharmony_ci 57bf215546Sopenharmony_citypedef void (*test_printf_t)(int i); 58bf215546Sopenharmony_ci 59bf215546Sopenharmony_ci 60bf215546Sopenharmony_cistatic LLVMValueRef 61bf215546Sopenharmony_ciadd_printf_test(struct gallivm_state *gallivm) 62bf215546Sopenharmony_ci{ 63bf215546Sopenharmony_ci LLVMModuleRef module = gallivm->module; 64bf215546Sopenharmony_ci LLVMTypeRef args[1] = { LLVMIntTypeInContext(gallivm->context, 32) }; 65bf215546Sopenharmony_ci LLVMValueRef func = LLVMAddFunction(module, "test_printf", LLVMFunctionType(LLVMVoidTypeInContext(gallivm->context), args, 1, 0)); 66bf215546Sopenharmony_ci LLVMBuilderRef builder = gallivm->builder; 67bf215546Sopenharmony_ci LLVMBasicBlockRef block = LLVMAppendBasicBlockInContext(gallivm->context, func, "entry"); 68bf215546Sopenharmony_ci 69bf215546Sopenharmony_ci LLVMSetFunctionCallConv(func, LLVMCCallConv); 70bf215546Sopenharmony_ci 71bf215546Sopenharmony_ci LLVMPositionBuilderAtEnd(builder, block); 72bf215546Sopenharmony_ci lp_build_printf(gallivm, "hello, world\n"); 73bf215546Sopenharmony_ci lp_build_printf(gallivm, "print 5 6: %d %d\n", LLVMConstInt(LLVMInt32TypeInContext(gallivm->context), 5, 0), 74bf215546Sopenharmony_ci LLVMConstInt(LLVMInt32TypeInContext(gallivm->context), 6, 0)); 75bf215546Sopenharmony_ci 76bf215546Sopenharmony_ci /* Also test lp_build_assert(). This should not fail. */ 77bf215546Sopenharmony_ci lp_build_assert(gallivm, LLVMConstInt(LLVMInt32TypeInContext(gallivm->context), 1, 0), "assert(1)"); 78bf215546Sopenharmony_ci 79bf215546Sopenharmony_ci LLVMBuildRetVoid(builder); 80bf215546Sopenharmony_ci 81bf215546Sopenharmony_ci gallivm_verify_function(gallivm, func); 82bf215546Sopenharmony_ci 83bf215546Sopenharmony_ci return func; 84bf215546Sopenharmony_ci} 85bf215546Sopenharmony_ci 86bf215546Sopenharmony_ci 87bf215546Sopenharmony_ciPIPE_ALIGN_STACK 88bf215546Sopenharmony_cistatic boolean 89bf215546Sopenharmony_citest_printf(unsigned verbose, FILE *fp, 90bf215546Sopenharmony_ci const struct printf_test_case *testcase) 91bf215546Sopenharmony_ci{ 92bf215546Sopenharmony_ci LLVMContextRef context; 93bf215546Sopenharmony_ci struct gallivm_state *gallivm; 94bf215546Sopenharmony_ci LLVMValueRef test; 95bf215546Sopenharmony_ci test_printf_t test_printf_func; 96bf215546Sopenharmony_ci boolean success = TRUE; 97bf215546Sopenharmony_ci 98bf215546Sopenharmony_ci context = LLVMContextCreate(); 99bf215546Sopenharmony_ci#if LLVM_VERSION_MAJOR >= 15 100bf215546Sopenharmony_ci LLVMContextSetOpaquePointers(context, false); 101bf215546Sopenharmony_ci#endif 102bf215546Sopenharmony_ci gallivm = gallivm_create("test_module", context, NULL); 103bf215546Sopenharmony_ci 104bf215546Sopenharmony_ci test = add_printf_test(gallivm); 105bf215546Sopenharmony_ci 106bf215546Sopenharmony_ci gallivm_compile_module(gallivm); 107bf215546Sopenharmony_ci 108bf215546Sopenharmony_ci test_printf_func = (test_printf_t) gallivm_jit_function(gallivm, test); 109bf215546Sopenharmony_ci 110bf215546Sopenharmony_ci gallivm_free_ir(gallivm); 111bf215546Sopenharmony_ci 112bf215546Sopenharmony_ci test_printf_func(0); 113bf215546Sopenharmony_ci 114bf215546Sopenharmony_ci gallivm_destroy(gallivm); 115bf215546Sopenharmony_ci LLVMContextDispose(context); 116bf215546Sopenharmony_ci 117bf215546Sopenharmony_ci return success; 118bf215546Sopenharmony_ci} 119bf215546Sopenharmony_ci 120bf215546Sopenharmony_ci 121bf215546Sopenharmony_ciboolean 122bf215546Sopenharmony_citest_all(unsigned verbose, FILE *fp) 123bf215546Sopenharmony_ci{ 124bf215546Sopenharmony_ci boolean success = TRUE; 125bf215546Sopenharmony_ci 126bf215546Sopenharmony_ci test_printf(verbose, fp, NULL); 127bf215546Sopenharmony_ci 128bf215546Sopenharmony_ci return success; 129bf215546Sopenharmony_ci} 130bf215546Sopenharmony_ci 131bf215546Sopenharmony_ci 132bf215546Sopenharmony_ciboolean 133bf215546Sopenharmony_citest_some(unsigned verbose, FILE *fp, 134bf215546Sopenharmony_ci unsigned long n) 135bf215546Sopenharmony_ci{ 136bf215546Sopenharmony_ci return test_all(verbose, fp); 137bf215546Sopenharmony_ci} 138bf215546Sopenharmony_ci 139bf215546Sopenharmony_ci 140bf215546Sopenharmony_ciboolean 141bf215546Sopenharmony_citest_single(unsigned verbose, FILE *fp) 142bf215546Sopenharmony_ci{ 143bf215546Sopenharmony_ci printf("no test_single()"); 144bf215546Sopenharmony_ci return TRUE; 145bf215546Sopenharmony_ci} 146