1bf215546Sopenharmony_ci/**************************************************************************
2bf215546Sopenharmony_ci *
3bf215546Sopenharmony_ci * Copyright 2009 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/**
30bf215546Sopenharmony_ci * @file
31bf215546Sopenharmony_ci * Helper functions for manipulation structures.
32bf215546Sopenharmony_ci *
33bf215546Sopenharmony_ci * @author Jose Fonseca <jfonseca@vmware.com>
34bf215546Sopenharmony_ci */
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_ci#include "util/u_debug.h"
38bf215546Sopenharmony_ci#include "util/u_memory.h"
39bf215546Sopenharmony_ci
40bf215546Sopenharmony_ci#include "lp_bld_const.h"
41bf215546Sopenharmony_ci#include "lp_bld_debug.h"
42bf215546Sopenharmony_ci#include "lp_bld_struct.h"
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_ci
45bf215546Sopenharmony_ciLLVMValueRef
46bf215546Sopenharmony_cilp_build_struct_get_ptr(struct gallivm_state *gallivm,
47bf215546Sopenharmony_ci                        LLVMValueRef ptr,
48bf215546Sopenharmony_ci                        unsigned member,
49bf215546Sopenharmony_ci                        const char *name)
50bf215546Sopenharmony_ci{
51bf215546Sopenharmony_ci   LLVMValueRef indices[2];
52bf215546Sopenharmony_ci   LLVMValueRef member_ptr;
53bf215546Sopenharmony_ci   assert(LLVMGetTypeKind(LLVMTypeOf(ptr)) == LLVMPointerTypeKind);
54bf215546Sopenharmony_ci   assert(LLVM_VERSION_MAJOR >= 15 || LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(ptr))) == LLVMStructTypeKind);
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_ci   indices[0] = lp_build_const_int32(gallivm, 0);
57bf215546Sopenharmony_ci   indices[1] = lp_build_const_int32(gallivm, member);
58bf215546Sopenharmony_ci   member_ptr = LLVMBuildGEP(gallivm->builder, ptr, indices, ARRAY_SIZE(indices), "");
59bf215546Sopenharmony_ci   lp_build_name(member_ptr, "%s.%s_ptr", LLVMGetValueName(ptr), name);
60bf215546Sopenharmony_ci   return member_ptr;
61bf215546Sopenharmony_ci}
62bf215546Sopenharmony_ci
63bf215546Sopenharmony_ciLLVMValueRef
64bf215546Sopenharmony_cilp_build_struct_get(struct gallivm_state *gallivm,
65bf215546Sopenharmony_ci                    LLVMValueRef ptr,
66bf215546Sopenharmony_ci                    unsigned member,
67bf215546Sopenharmony_ci                    const char *name)
68bf215546Sopenharmony_ci{
69bf215546Sopenharmony_ci   LLVMValueRef member_ptr;
70bf215546Sopenharmony_ci   LLVMValueRef res;
71bf215546Sopenharmony_ci   assert(LLVMGetTypeKind(LLVMTypeOf(ptr)) == LLVMPointerTypeKind);
72bf215546Sopenharmony_ci   assert(LLVM_VERSION_MAJOR >= 15 || LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(ptr))) == LLVMStructTypeKind);
73bf215546Sopenharmony_ci   member_ptr = lp_build_struct_get_ptr(gallivm, ptr, member, name);
74bf215546Sopenharmony_ci   res = LLVMBuildLoad(gallivm->builder, member_ptr, "");
75bf215546Sopenharmony_ci   lp_build_name(res, "%s.%s", LLVMGetValueName(ptr), name);
76bf215546Sopenharmony_ci   return res;
77bf215546Sopenharmony_ci}
78bf215546Sopenharmony_ci
79bf215546Sopenharmony_ciLLVMValueRef
80bf215546Sopenharmony_cilp_build_struct_get_ptr2(struct gallivm_state *gallivm,
81bf215546Sopenharmony_ci                        LLVMTypeRef ptr_type,
82bf215546Sopenharmony_ci                        LLVMValueRef ptr,
83bf215546Sopenharmony_ci                        unsigned member,
84bf215546Sopenharmony_ci                        const char *name)
85bf215546Sopenharmony_ci{
86bf215546Sopenharmony_ci   LLVMValueRef indices[2];
87bf215546Sopenharmony_ci   LLVMValueRef member_ptr;
88bf215546Sopenharmony_ci   assert(LLVMGetTypeKind(LLVMTypeOf(ptr)) == LLVMPointerTypeKind);
89bf215546Sopenharmony_ci   assert(LLVM_VERSION_MAJOR >= 15 || LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(ptr))) == LLVMStructTypeKind);
90bf215546Sopenharmony_ci
91bf215546Sopenharmony_ci   indices[0] = lp_build_const_int32(gallivm, 0);
92bf215546Sopenharmony_ci   indices[1] = lp_build_const_int32(gallivm, member);
93bf215546Sopenharmony_ci   member_ptr = LLVMBuildGEP2(gallivm->builder, ptr_type, ptr, indices, ARRAY_SIZE(indices), "");
94bf215546Sopenharmony_ci   lp_build_name(member_ptr, "%s.%s_ptr", LLVMGetValueName(ptr), name);
95bf215546Sopenharmony_ci   return member_ptr;
96bf215546Sopenharmony_ci}
97bf215546Sopenharmony_ci
98bf215546Sopenharmony_ciLLVMValueRef
99bf215546Sopenharmony_cilp_build_struct_get2(struct gallivm_state *gallivm,
100bf215546Sopenharmony_ci                    LLVMTypeRef ptr_type,
101bf215546Sopenharmony_ci                    LLVMValueRef ptr,
102bf215546Sopenharmony_ci                    unsigned member,
103bf215546Sopenharmony_ci                    const char *name)
104bf215546Sopenharmony_ci{
105bf215546Sopenharmony_ci   LLVMValueRef member_ptr;
106bf215546Sopenharmony_ci   LLVMValueRef res;
107bf215546Sopenharmony_ci   assert(LLVMGetTypeKind(LLVMTypeOf(ptr)) == LLVMPointerTypeKind);
108bf215546Sopenharmony_ci   assert(LLVM_VERSION_MAJOR >= 15 || LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(ptr))) == LLVMStructTypeKind);
109bf215546Sopenharmony_ci   member_ptr = lp_build_struct_get_ptr2(gallivm, ptr_type, ptr, member, name);
110bf215546Sopenharmony_ci   res = LLVMBuildLoad(gallivm->builder, member_ptr, "");
111bf215546Sopenharmony_ci   lp_build_name(res, "%s.%s", LLVMGetValueName(ptr), name);
112bf215546Sopenharmony_ci   return res;
113bf215546Sopenharmony_ci}
114bf215546Sopenharmony_ci
115bf215546Sopenharmony_ciLLVMValueRef
116bf215546Sopenharmony_cilp_build_array_get_ptr(struct gallivm_state *gallivm,
117bf215546Sopenharmony_ci                       LLVMValueRef ptr,
118bf215546Sopenharmony_ci                       LLVMValueRef index)
119bf215546Sopenharmony_ci{
120bf215546Sopenharmony_ci   LLVMValueRef indices[2];
121bf215546Sopenharmony_ci   LLVMValueRef element_ptr;
122bf215546Sopenharmony_ci   assert(LLVMGetTypeKind(LLVMTypeOf(ptr)) == LLVMPointerTypeKind);
123bf215546Sopenharmony_ci   assert(LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(ptr))) == LLVMArrayTypeKind);
124bf215546Sopenharmony_ci   indices[0] = lp_build_const_int32(gallivm, 0);
125bf215546Sopenharmony_ci   indices[1] = index;
126bf215546Sopenharmony_ci   element_ptr = LLVMBuildGEP(gallivm->builder, ptr, indices, ARRAY_SIZE(indices), "");
127bf215546Sopenharmony_ci#ifdef DEBUG
128bf215546Sopenharmony_ci   lp_build_name(element_ptr, "&%s[%s]",
129bf215546Sopenharmony_ci                 LLVMGetValueName(ptr), LLVMGetValueName(index));
130bf215546Sopenharmony_ci#endif
131bf215546Sopenharmony_ci   return element_ptr;
132bf215546Sopenharmony_ci}
133bf215546Sopenharmony_ci
134bf215546Sopenharmony_ci
135bf215546Sopenharmony_ciLLVMValueRef
136bf215546Sopenharmony_cilp_build_array_get(struct gallivm_state *gallivm,
137bf215546Sopenharmony_ci                   LLVMValueRef ptr,
138bf215546Sopenharmony_ci                   LLVMValueRef index)
139bf215546Sopenharmony_ci{
140bf215546Sopenharmony_ci   LLVMValueRef element_ptr;
141bf215546Sopenharmony_ci   LLVMValueRef res;
142bf215546Sopenharmony_ci   assert(LLVMGetTypeKind(LLVMTypeOf(ptr)) == LLVMPointerTypeKind);
143bf215546Sopenharmony_ci   assert(LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(ptr))) == LLVMArrayTypeKind);
144bf215546Sopenharmony_ci   element_ptr = lp_build_array_get_ptr(gallivm, ptr, index);
145bf215546Sopenharmony_ci   res = LLVMBuildLoad(gallivm->builder, element_ptr, "");
146bf215546Sopenharmony_ci#ifdef DEBUG
147bf215546Sopenharmony_ci   lp_build_name(res, "%s[%s]", LLVMGetValueName(ptr), LLVMGetValueName(index));
148bf215546Sopenharmony_ci#endif
149bf215546Sopenharmony_ci   return res;
150bf215546Sopenharmony_ci}
151bf215546Sopenharmony_ci
152bf215546Sopenharmony_ci
153bf215546Sopenharmony_civoid
154bf215546Sopenharmony_cilp_build_array_set(struct gallivm_state *gallivm,
155bf215546Sopenharmony_ci                   LLVMValueRef ptr,
156bf215546Sopenharmony_ci                   LLVMValueRef index,
157bf215546Sopenharmony_ci                   LLVMValueRef value)
158bf215546Sopenharmony_ci{
159bf215546Sopenharmony_ci   LLVMValueRef element_ptr;
160bf215546Sopenharmony_ci   assert(LLVMGetTypeKind(LLVMTypeOf(ptr)) == LLVMPointerTypeKind);
161bf215546Sopenharmony_ci   assert(LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(ptr))) == LLVMArrayTypeKind);
162bf215546Sopenharmony_ci   element_ptr = lp_build_array_get_ptr(gallivm, ptr, index);
163bf215546Sopenharmony_ci   LLVMBuildStore(gallivm->builder, value, element_ptr);
164bf215546Sopenharmony_ci}
165bf215546Sopenharmony_ci
166bf215546Sopenharmony_ci
167bf215546Sopenharmony_ciLLVMValueRef
168bf215546Sopenharmony_cilp_build_pointer_get(LLVMBuilderRef builder,
169bf215546Sopenharmony_ci                     LLVMValueRef ptr,
170bf215546Sopenharmony_ci                     LLVMValueRef index)
171bf215546Sopenharmony_ci{
172bf215546Sopenharmony_ci   return lp_build_pointer_get_unaligned(builder, ptr, index, 0);
173bf215546Sopenharmony_ci}
174bf215546Sopenharmony_ci
175bf215546Sopenharmony_ci
176bf215546Sopenharmony_ciLLVMValueRef
177bf215546Sopenharmony_cilp_build_pointer_get_unaligned(LLVMBuilderRef builder,
178bf215546Sopenharmony_ci                               LLVMValueRef ptr,
179bf215546Sopenharmony_ci                               LLVMValueRef index,
180bf215546Sopenharmony_ci                               unsigned alignment)
181bf215546Sopenharmony_ci{
182bf215546Sopenharmony_ci   LLVMValueRef element_ptr;
183bf215546Sopenharmony_ci   LLVMValueRef res;
184bf215546Sopenharmony_ci   assert(LLVMGetTypeKind(LLVMTypeOf(ptr)) == LLVMPointerTypeKind);
185bf215546Sopenharmony_ci   element_ptr = LLVMBuildGEP(builder, ptr, &index, 1, "");
186bf215546Sopenharmony_ci   res = LLVMBuildLoad(builder, element_ptr, "");
187bf215546Sopenharmony_ci   if (alignment)
188bf215546Sopenharmony_ci      LLVMSetAlignment(res, alignment);
189bf215546Sopenharmony_ci#ifdef DEBUG
190bf215546Sopenharmony_ci   lp_build_name(res, "%s[%s]", LLVMGetValueName(ptr), LLVMGetValueName(index));
191bf215546Sopenharmony_ci#endif
192bf215546Sopenharmony_ci   return res;
193bf215546Sopenharmony_ci}
194bf215546Sopenharmony_ci
195bf215546Sopenharmony_ci
196bf215546Sopenharmony_civoid
197bf215546Sopenharmony_cilp_build_pointer_set(LLVMBuilderRef builder,
198bf215546Sopenharmony_ci                     LLVMValueRef ptr,
199bf215546Sopenharmony_ci                     LLVMValueRef index,
200bf215546Sopenharmony_ci                     LLVMValueRef value)
201bf215546Sopenharmony_ci{
202bf215546Sopenharmony_ci   LLVMValueRef element_ptr;
203bf215546Sopenharmony_ci   element_ptr = LLVMBuildGEP2(builder, LLVMTypeOf(value), ptr, &index, 1, "");
204bf215546Sopenharmony_ci   LLVMBuildStore(builder, value, element_ptr);
205bf215546Sopenharmony_ci}
206bf215546Sopenharmony_ci
207bf215546Sopenharmony_ci
208bf215546Sopenharmony_civoid
209bf215546Sopenharmony_cilp_build_pointer_set_unaligned(LLVMBuilderRef builder,
210bf215546Sopenharmony_ci                               LLVMValueRef ptr,
211bf215546Sopenharmony_ci                               LLVMValueRef index,
212bf215546Sopenharmony_ci                               LLVMValueRef value,
213bf215546Sopenharmony_ci                               unsigned alignment)
214bf215546Sopenharmony_ci{
215bf215546Sopenharmony_ci   LLVMValueRef element_ptr;
216bf215546Sopenharmony_ci   LLVMValueRef instr;
217bf215546Sopenharmony_ci   element_ptr = LLVMBuildGEP2(builder, LLVMTypeOf(value), ptr, &index, 1, "");
218bf215546Sopenharmony_ci   instr = LLVMBuildStore(builder, value, element_ptr);
219bf215546Sopenharmony_ci   LLVMSetAlignment(instr, alignment);
220bf215546Sopenharmony_ci}
221