Path: blob/21.2-virgl/src/gallium/auxiliary/gallivm/lp_bld_printf.c
4565 views
/**************************************************************************1*2* Copyright 2010 VMware, Inc.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.20* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR21* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25**************************************************************************/2627#include <stdio.h>28#include <inttypes.h>2930#include "util/compiler.h"31#include "util/u_debug.h"32#include "util/u_memory.h"33#include "util/u_string.h"34#include "lp_bld_const.h"35#include "lp_bld_init.h"36#include "lp_bld_const.h"37#include "lp_bld_printf.h"38#include "lp_bld_type.h"394041/**42* Generates LLVM IR to call debug_printf.43*/44static LLVMValueRef45lp_build_print_args(struct gallivm_state* gallivm,46int argcount,47LLVMValueRef* args)48{49LLVMBuilderRef builder = gallivm->builder;50LLVMContextRef context = gallivm->context;51int i;5253assert(args);54assert(argcount > 0);55assert(LLVMTypeOf(args[0]) == LLVMPointerType(LLVMInt8TypeInContext(context), 0));5657/* Cast any float arguments to doubles as printf expects */58for (i = 1; i < argcount; i++) {59LLVMTypeRef type = LLVMTypeOf(args[i]);6061if (LLVMGetTypeKind(type) == LLVMFloatTypeKind)62args[i] = LLVMBuildFPExt(builder, args[i], LLVMDoubleTypeInContext(context), "");63}6465if (!gallivm->debug_printf_hook) {66LLVMTypeRef printf_type = LLVMFunctionType(LLVMInt32TypeInContext(context), NULL, 0, 1);67gallivm->debug_printf_hook = LLVMAddFunction(gallivm->module, "debug_printf", printf_type);68}69return LLVMBuildCall(builder, gallivm->debug_printf_hook, args, argcount, "");70}717273/**74* Print a LLVM value of any type75*/76LLVMValueRef77lp_build_print_value(struct gallivm_state *gallivm,78const char *msg,79LLVMValueRef value)80{81LLVMBuilderRef builder = gallivm->builder;82LLVMTypeKind type_kind;83LLVMTypeRef type_ref;84LLVMValueRef params[2 + LP_MAX_VECTOR_LENGTH];85char type_fmt[6] = " %x";86char format[2 + 5 * LP_MAX_VECTOR_LENGTH + 2] = "%s";87unsigned length;88unsigned i;8990type_ref = LLVMTypeOf(value);91type_kind = LLVMGetTypeKind(type_ref);9293if (type_kind == LLVMVectorTypeKind) {94length = LLVMGetVectorSize(type_ref);9596type_ref = LLVMGetElementType(type_ref);97type_kind = LLVMGetTypeKind(type_ref);98} else {99length = 1;100}101102if (type_kind == LLVMFloatTypeKind || type_kind == LLVMDoubleTypeKind) {103type_fmt[2] = '.';104type_fmt[3] = '9';105type_fmt[4] = 'g';106type_fmt[5] = '\0';107} else if (type_kind == LLVMIntegerTypeKind) {108if (LLVMGetIntTypeWidth(type_ref) == 64) {109snprintf(type_fmt + 2, 3, "%s", PRId64);110} else if (LLVMGetIntTypeWidth(type_ref) == 8) {111type_fmt[2] = 'u';112} else {113type_fmt[2] = 'i';114}115} else if (type_kind == LLVMPointerTypeKind) {116type_fmt[2] = 'p';117} else {118/* Unsupported type */119assert(0);120}121122/* Create format string and arguments */123assert(strlen(format) + strlen(type_fmt) * length + 2 <= sizeof format);124125params[1] = lp_build_const_string(gallivm, msg);126if (length == 1) {127strncat(format, type_fmt, sizeof(format) - strlen(format) - 1);128params[2] = value;129} else {130for (i = 0; i < length; ++i) {131LLVMValueRef param;132strncat(format, type_fmt, sizeof(format) - strlen(format) - 1);133param = LLVMBuildExtractElement(builder, value, lp_build_const_int32(gallivm, i), "");134if (type_kind == LLVMIntegerTypeKind &&135LLVMGetIntTypeWidth(type_ref) < sizeof(int) * 8) {136LLVMTypeRef int_type = LLVMIntTypeInContext(gallivm->context, sizeof(int) * 8);137if (LLVMGetIntTypeWidth(type_ref) == 8) {138param = LLVMBuildZExt(builder, param, int_type, "");139} else {140param = LLVMBuildSExt(builder, param, int_type, "");141}142}143params[2 + i] = param;144}145}146147strncat(format, "\n", sizeof(format) - strlen(format) - 1);148149params[0] = lp_build_const_string(gallivm, format);150return lp_build_print_args(gallivm, 2 + length, params);151}152153154static unsigned155lp_get_printf_arg_count(const char *fmt)156{157unsigned count = 0;158const char *p = fmt;159int c;160161while ((c = *p++)) {162if (c != '%')163continue;164switch (*p) {165case '\0':166continue;167case '%':168p++;169continue;170case '.':171if (p[1] == '*' && p[2] == 's') {172count += 2;173p += 3;174continue;175}176FALLTHROUGH;177default:178count ++;179}180}181return count;182}183184185/**186* Generate LLVM IR for a c style printf187*/188LLVMValueRef189lp_build_printf(struct gallivm_state *gallivm,190const char *fmt, ...)191{192LLVMValueRef params[50];193va_list arglist;194unsigned argcount, i;195196argcount = lp_get_printf_arg_count(fmt);197assert(ARRAY_SIZE(params) >= argcount + 1);198199va_start(arglist, fmt);200for (i = 1; i <= argcount; i++) {201params[i] = va_arg(arglist, LLVMValueRef);202}203va_end(arglist);204205params[0] = lp_build_const_string(gallivm, fmt);206return lp_build_print_args(gallivm, argcount + 1, params);207}208209210