From c0d41d0404285df4f3a8728ddc3b451e54011c7a Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Fonseca?= Date: Fri, 3 Sep 2010 11:53:48 +0100 Subject: [PATCH] gallivm: Helper functions for pointer indirection. --- src/gallium/auxiliary/gallivm/lp_bld_struct.c | 39 +++++++++++++++++++ src/gallium/auxiliary/gallivm/lp_bld_struct.h | 17 ++++++++ 2 files changed, 56 insertions(+) diff --git a/src/gallium/auxiliary/gallivm/lp_bld_struct.c b/src/gallium/auxiliary/gallivm/lp_bld_struct.c index c25b2f6493c..4693c2de6f9 100644 --- a/src/gallium/auxiliary/gallivm/lp_bld_struct.c +++ b/src/gallium/auxiliary/gallivm/lp_bld_struct.c @@ -49,6 +49,8 @@ lp_build_struct_get_ptr(LLVMBuilderRef builder, { LLVMValueRef indices[2]; LLVMValueRef member_ptr; + assert(LLVMGetTypeKind(LLVMTypeOf(ptr)) == LLVMPointerTypeKind); + assert(LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(ptr))) == LLVMStructTypeKind); indices[0] = LLVMConstInt(LLVMInt32Type(), 0, 0); indices[1] = LLVMConstInt(LLVMInt32Type(), member, 0); member_ptr = LLVMBuildGEP(builder, ptr, indices, Elements(indices), ""); @@ -65,6 +67,8 @@ lp_build_struct_get(LLVMBuilderRef builder, { LLVMValueRef member_ptr; LLVMValueRef res; + assert(LLVMGetTypeKind(LLVMTypeOf(ptr)) == LLVMPointerTypeKind); + assert(LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(ptr))) == LLVMStructTypeKind); member_ptr = lp_build_struct_get_ptr(builder, ptr, member, name); res = LLVMBuildLoad(builder, member_ptr, ""); lp_build_name(res, "%s.%s", LLVMGetValueName(ptr), name); @@ -79,6 +83,8 @@ lp_build_array_get_ptr(LLVMBuilderRef builder, { LLVMValueRef indices[2]; LLVMValueRef element_ptr; + assert(LLVMGetTypeKind(LLVMTypeOf(ptr)) == LLVMPointerTypeKind); + assert(LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(ptr))) == LLVMArrayTypeKind); indices[0] = LLVMConstInt(LLVMInt32Type(), 0, 0); indices[1] = index; element_ptr = LLVMBuildGEP(builder, ptr, indices, Elements(indices), ""); @@ -97,6 +103,8 @@ lp_build_array_get(LLVMBuilderRef builder, { LLVMValueRef element_ptr; LLVMValueRef res; + assert(LLVMGetTypeKind(LLVMTypeOf(ptr)) == LLVMPointerTypeKind); + assert(LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(ptr))) == LLVMArrayTypeKind); element_ptr = lp_build_array_get_ptr(builder, ptr, index); res = LLVMBuildLoad(builder, element_ptr, ""); #ifdef DEBUG @@ -113,6 +121,37 @@ lp_build_array_set(LLVMBuilderRef builder, LLVMValueRef value) { LLVMValueRef element_ptr; + assert(LLVMGetTypeKind(LLVMTypeOf(ptr)) == LLVMPointerTypeKind); + assert(LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(ptr))) == LLVMArrayTypeKind); element_ptr = lp_build_array_get_ptr(builder, ptr, index); LLVMBuildStore(builder, value, element_ptr); } + + +LLVMValueRef +lp_build_pointer_get(LLVMBuilderRef builder, + LLVMValueRef ptr, + LLVMValueRef index) +{ + LLVMValueRef element_ptr; + LLVMValueRef res; + assert(LLVMGetTypeKind(LLVMTypeOf(ptr)) == LLVMPointerTypeKind); + element_ptr = LLVMBuildGEP(builder, ptr, &index, 1, ""); + res = LLVMBuildLoad(builder, element_ptr, ""); +#ifdef DEBUG + lp_build_name(res, "%s[%s]", LLVMGetValueName(ptr), LLVMGetValueName(index)); +#endif + return res; +} + + +void +lp_build_pointer_set(LLVMBuilderRef builder, + LLVMValueRef ptr, + LLVMValueRef index, + LLVMValueRef value) +{ + LLVMValueRef element_ptr; + element_ptr = LLVMBuildGEP(builder, ptr, &index, 1, ""); + LLVMBuildStore(builder, value, element_ptr); +} diff --git a/src/gallium/auxiliary/gallivm/lp_bld_struct.h b/src/gallium/auxiliary/gallivm/lp_bld_struct.h index f8b6dab4bfb..eb87a8eee9e 100644 --- a/src/gallium/auxiliary/gallivm/lp_bld_struct.h +++ b/src/gallium/auxiliary/gallivm/lp_bld_struct.h @@ -96,4 +96,21 @@ lp_build_array_set(LLVMBuilderRef builder, LLVMValueRef index, LLVMValueRef value); +/** + * Get the value of an array element. + */ +LLVMValueRef +lp_build_pointer_get(LLVMBuilderRef builder, + LLVMValueRef ptr, + LLVMValueRef index); + +/** + * Set the value of an array element. + */ +void +lp_build_pointer_set(LLVMBuilderRef builder, + LLVMValueRef ptr, + LLVMValueRef index, + LLVMValueRef value); + #endif /* !LP_BLD_STRUCT_H */ -- 2.30.2