1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
31 * Helper functions for manipulation structures.
33 * @author Jose Fonseca <jfonseca@vmware.com>
37 #include "util/u_debug.h"
38 #include "util/u_memory.h"
40 #include "lp_bld_debug.h"
41 #include "lp_bld_struct.h"
45 lp_build_struct_get_ptr(LLVMBuilderRef builder
,
50 LLVMValueRef indices
[2];
51 LLVMValueRef member_ptr
;
52 assert(LLVMGetTypeKind(LLVMTypeOf(ptr
)) == LLVMPointerTypeKind
);
53 assert(LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(ptr
))) == LLVMStructTypeKind
);
54 indices
[0] = LLVMConstInt(LLVMInt32Type(), 0, 0);
55 indices
[1] = LLVMConstInt(LLVMInt32Type(), member
, 0);
56 member_ptr
= LLVMBuildGEP(builder
, ptr
, indices
, Elements(indices
), "");
57 lp_build_name(member_ptr
, "%s.%s_ptr", LLVMGetValueName(ptr
), name
);
63 lp_build_struct_get(LLVMBuilderRef builder
,
68 LLVMValueRef member_ptr
;
70 assert(LLVMGetTypeKind(LLVMTypeOf(ptr
)) == LLVMPointerTypeKind
);
71 assert(LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(ptr
))) == LLVMStructTypeKind
);
72 member_ptr
= lp_build_struct_get_ptr(builder
, ptr
, member
, name
);
73 res
= LLVMBuildLoad(builder
, member_ptr
, "");
74 lp_build_name(res
, "%s.%s", LLVMGetValueName(ptr
), name
);
80 lp_build_array_get_ptr(LLVMBuilderRef builder
,
84 LLVMValueRef indices
[2];
85 LLVMValueRef element_ptr
;
86 assert(LLVMGetTypeKind(LLVMTypeOf(ptr
)) == LLVMPointerTypeKind
);
87 assert(LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(ptr
))) == LLVMArrayTypeKind
);
88 indices
[0] = LLVMConstInt(LLVMInt32Type(), 0, 0);
90 element_ptr
= LLVMBuildGEP(builder
, ptr
, indices
, Elements(indices
), "");
92 lp_build_name(element_ptr
, "&%s[%s]",
93 LLVMGetValueName(ptr
), LLVMGetValueName(index
));
100 lp_build_array_get(LLVMBuilderRef builder
,
104 LLVMValueRef element_ptr
;
106 assert(LLVMGetTypeKind(LLVMTypeOf(ptr
)) == LLVMPointerTypeKind
);
107 assert(LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(ptr
))) == LLVMArrayTypeKind
);
108 element_ptr
= lp_build_array_get_ptr(builder
, ptr
, index
);
109 res
= LLVMBuildLoad(builder
, element_ptr
, "");
111 lp_build_name(res
, "%s[%s]", LLVMGetValueName(ptr
), LLVMGetValueName(index
));
118 lp_build_array_set(LLVMBuilderRef builder
,
123 LLVMValueRef element_ptr
;
124 assert(LLVMGetTypeKind(LLVMTypeOf(ptr
)) == LLVMPointerTypeKind
);
125 assert(LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(ptr
))) == LLVMArrayTypeKind
);
126 element_ptr
= lp_build_array_get_ptr(builder
, ptr
, index
);
127 LLVMBuildStore(builder
, value
, element_ptr
);
132 lp_build_pointer_get(LLVMBuilderRef builder
,
136 LLVMValueRef element_ptr
;
138 assert(LLVMGetTypeKind(LLVMTypeOf(ptr
)) == LLVMPointerTypeKind
);
139 element_ptr
= LLVMBuildGEP(builder
, ptr
, &index
, 1, "");
140 res
= LLVMBuildLoad(builder
, element_ptr
, "");
142 lp_build_name(res
, "%s[%s]", LLVMGetValueName(ptr
), LLVMGetValueName(index
));
149 lp_build_pointer_set(LLVMBuilderRef builder
,
154 LLVMValueRef element_ptr
;
155 element_ptr
= LLVMBuildGEP(builder
, ptr
, &index
, 1, "");
156 LLVMBuildStore(builder
, value
, element_ptr
);