fix pointer size calculation
[m6w6/ext-psi] / src / types / decl_struct.c
1 /*******************************************************************************
2 Copyright (c) 2016, Michael Wallner <mike@php.net>.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 * Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
18 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *******************************************************************************/
25
26 #include "php_psi_stdinc.h"
27 #include "data.h"
28
29 #include <assert.h>
30
31 struct psi_decl_struct* psi_decl_struct_init(const char *name,
32 struct psi_plist *args)
33 {
34 struct psi_decl_struct *s = calloc(1, sizeof(*s));
35 s->name = strdup(name);
36 s->args = args;
37 return s;
38 }
39
40 void psi_decl_struct_free(struct psi_decl_struct **s_ptr)
41 {
42 if (*s_ptr) {
43 struct psi_decl_struct *s = *s_ptr;
44
45 *s_ptr = NULL;
46 if (s->token) {
47 free(s->token);
48 }
49 if (s->args) {
50 psi_plist_free(s->args);
51 }
52 if (s->engine.type && s->engine.dtor) {
53 s->engine.dtor(s->engine.type);
54 }
55 free(s->name);
56 free(s);
57 }
58 }
59
60 void psi_decl_struct_dump(int fd, struct psi_decl_struct *strct)
61 {
62 dprintf(fd, "struct %s::(%zu, %zu)", strct->name, strct->align,
63 strct->size);
64 if (psi_plist_count(strct->args)) {
65 psi_decl_type_dump_args_with_layout(fd, strct->args, 0);
66 } else {
67 dprintf(fd, ";");
68 }
69 }
70
71 struct psi_decl_arg *psi_decl_struct_get_arg(struct psi_decl_struct *s,
72 struct psi_decl_var *var)
73 {
74 if (s->args) {
75 return psi_decl_arg_get_by_var(var, s->args, NULL);
76 }
77
78 return NULL;
79 }
80
81 bool psi_decl_struct_validate(struct psi_data *data, struct psi_decl_struct *s,
82 struct psi_validate_scope *scope)
83 {
84 size_t i, pos = 0, len = 0;
85 struct psi_decl_arg *darg, *prev_arg;
86
87 if (!s) {
88 return false;
89 }
90 if (psi_validate_scope_has_struct(scope, s->name)) {
91 return true;
92 }
93
94 if (!s->size && !psi_plist_count(s->args)) {
95 /* TODO: return true and check those structs are only used by address */
96 /* suppress needless warning
97 data->error(data, s->token, PSI_WARNING, "Empty struct %s",
98 s->name);
99 */
100 return false;
101 }
102
103 psi_validate_scope_add_struct(scope, s->name, s);
104
105 for (i = 0; psi_plist_get(s->args, i, &darg); ++i) {
106 size_t align;
107
108 darg->var->arg = darg;
109
110 if (!psi_decl_arg_validate(data, darg, scope)) {
111 psi_validate_scope_del_struct(scope, s->name);
112 return false;
113 }
114
115 if (darg->layout && darg->layout->len) {
116 pos = darg->layout->pos;
117 align = psi_decl_arg_align(darg, &pos, &len);
118
119 if (!align) {
120 data->error(data, darg->token, PSI_WARNING,
121 "Computed zero alignment of %s.%s of type '%s'",
122 len, s->name, darg->var->name, darg->type->name);
123 psi_validate_scope_del_struct(scope, s->name);
124 return false;
125 }
126
127 if (darg->layout->len != len) {
128 data->error(data, darg->token, PSI_WARNING,
129 "Computed size %zu of %s.%s does not match"
130 " pre-defined size %zu of type '%s'",
131 len, s->name, darg->var->name, darg->layout->len,
132 darg->type->name);
133 }
134 if (darg->layout->pos != pos) {
135 data->error(data, darg->token, PSI_WARNING,
136 "Computed offset %zu of %s.%s does not match"
137 " pre-defined offset %zu",
138 pos, s->name, darg->var->name, darg->layout->pos);
139 }
140 } else {
141 if (i) {
142 if (prev_arg->layout && prev_arg->layout->bfw && darg->layout && darg->layout->bfw) {
143 struct psi_decl_type *real = NULL;
144 size_t max_bfw = 8 * psi_decl_type_get_size(prev_arg->type, &real);
145
146 switch (real->type) {
147 case PSI_T_INT8:
148 case PSI_T_UINT8:
149 case PSI_T_INT16:
150 case PSI_T_UINT16:
151 case PSI_T_INT32:
152 case PSI_T_UINT32:
153 case PSI_T_INT64:
154 case PSI_T_UINT64:
155 break;
156 default:
157 data->error(data, darg->token, PSI_WARNING,
158 "Unsupported type for bit field: %s", real->name);
159 psi_validate_scope_del_struct(scope, s->name);
160 return false;
161 }
162 darg->layout->bfw->pos = prev_arg->layout->bfw->pos + prev_arg->layout->bfw->len;
163 if (max_bfw >= darg->layout->bfw->pos + darg->layout->bfw->len) {
164 pos = prev_arg->layout->pos;
165 } else {
166 darg->layout->bfw->pos = 0;
167 pos = prev_arg->layout->pos + prev_arg->layout->len;
168 }
169 } else {
170 pos = prev_arg->layout->pos + prev_arg->layout->len;
171 }
172 } else {
173 pos = 0;
174 }
175
176 align = psi_decl_arg_align(darg, &pos, &len);
177
178 if (darg->layout) {
179 if (darg->layout->pos != pos) {
180 data->error(data, darg->token, PSI_WARNING,
181 "Computed offset %zu of %s.%s does not match"
182 " pre-defined offset %zu",
183 pos, s->name, darg->var->name, darg->layout->pos);
184 }
185 darg->layout->pos = pos;
186 darg->layout->len = len;
187 } else {
188 darg->layout = psi_layout_init(pos, len, NULL);
189 }
190 }
191
192 if (align > s->align) {
193 s->align = align;
194 }
195 prev_arg = darg;
196 }
197
198 if (psi_plist_count(s->args)) {
199 size_t size;
200
201 psi_plist_sort(s->args, psi_layout_sort_cmp, NULL);
202 psi_plist_get(s->args, psi_plist_count(s->args) - 1, &darg);
203
204 size = darg->layout->pos + darg->layout->len;
205 if (s->size < size) {
206 s->size = psi_align(size, s->align);
207 }
208 }
209
210 assert(s->size);
211
212 return true;
213 }
214
215 size_t psi_decl_struct_get_align(struct psi_decl_struct *s)
216 {
217 if (!s) {
218 return 0;
219 }
220 if (!s->align) {
221 s->align = psi_decl_type_get_args_align(s->args);
222 }
223 return s->align;
224 }