gallivm: Helper functions for pointer indirection.
authorJosé Fonseca <jfonseca@vmware.com>
Fri, 3 Sep 2010 10:53:48 +0000 (11:53 +0100)
committerJosé Fonseca <jfonseca@vmware.com>
Sat, 11 Sep 2010 12:27:38 +0000 (13:27 +0100)
src/gallium/auxiliary/gallivm/lp_bld_struct.c
src/gallium/auxiliary/gallivm/lp_bld_struct.h

index c25b2f6493c01f2bb644b1cd2844b68e6ecbe237..4693c2de6f9c0bb0f604b995683c5ea3f7562a31 100644 (file)
@@ -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);
+}
index f8b6dab4bfb22a68d1e02d2dd3bc1ab37010a51b..eb87a8eee9ee40a24fbf74d1c2f695be4f669eeb 100644 (file)
@@ -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 */