14076c6b296d7cd8a72f5e001ddff4ab30ca6cd2
[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(zend_string *name,
32 struct psi_plist *args)
33 {
34 struct psi_decl_struct *s = pecalloc(1, sizeof(*s), 1);
35 s->name = zend_string_copy(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 psi_token_free(&s->token);
47 if (s->args) {
48 psi_plist_free(s->args);
49 }
50 if (s->engine.type && s->engine.dtor) {
51 s->engine.dtor(s->engine.type);
52 }
53 zend_string_release(s->name);
54 free(s);
55 }
56 }
57
58 void psi_decl_struct_dump(int fd, struct psi_decl_struct *strct)
59 {
60 dprintf(fd, "struct %s::(%zu, %zu)", strct->name->val, strct->align,
61 strct->size);
62 if (psi_plist_count(strct->args)) {
63 psi_decl_type_dump_args_with_layout(fd, strct->args, 0);
64 } else {
65 dprintf(fd, ";");
66 }
67 }
68
69 struct psi_decl_arg *psi_decl_struct_get_arg(struct psi_decl_struct *s,
70 struct psi_decl_var *var)
71 {
72 if (s->args) {
73 return psi_decl_arg_get_by_var(var, s->args, NULL);
74 }
75
76 return NULL;
77 }
78
79 bool psi_decl_struct_validate(struct psi_data *data, struct psi_decl_struct *s,
80 struct psi_validate_scope *scope)
81 {
82 size_t i, pos = 0, len = 0;
83 struct psi_decl_arg *darg, *prev_arg;
84
85 if (!s) {
86 return false;
87 }
88 if (psi_validate_scope_has_struct(scope, s->name)) {
89 return true;
90 }
91
92 if (!s->size && !psi_plist_count(s->args)) {
93 /* TODO: return true and check those structs are only used by address */
94 /* suppress needless warning
95 data->error(data, s->token, PSI_WARNING, "Empty struct %s",
96 s->name);
97 */
98 return false;
99 }
100
101 psi_validate_scope_add_struct(scope, s->name, s);
102
103 for (i = 0; psi_plist_get(s->args, i, &darg); ++i) {
104 size_t align;
105
106 darg->var->arg = darg;
107
108 if (!psi_decl_arg_validate(data, darg, scope)) {
109 psi_validate_scope_del_struct(scope, s->name);
110 return false;
111 }
112
113 if (darg->layout && darg->layout->len) {
114 pos = darg->layout->pos;
115 align = psi_decl_arg_align(darg, &pos, &len);
116
117 if (!align) {
118 data->error(data, darg->token, PSI_WARNING,
119 "Computed zero alignment of %s.%s of type '%s'",
120 len, s->name->val, darg->var->name->val,
121 darg->type->name->val);
122 psi_validate_scope_del_struct(scope, s->name);
123 return false;
124 }
125
126 if (darg->layout->len != len) {
127 data->error(data, darg->token, PSI_WARNING,
128 "Computed size %zu of %s.%s does not match"
129 " pre-defined size %zu of type '%s'",
130 len, s->name->val, darg->var->name->val,
131 darg->layout->len, darg->type->name->val);
132 }
133 if (darg->layout->pos != pos) {
134 data->error(data, darg->token, PSI_WARNING,
135 "Computed offset %zu of %s.%s does not match"
136 " pre-defined offset %zu",
137 pos, s->name->val, darg->var->name->val,
138 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",
159 real->name->val);
160 psi_validate_scope_del_struct(scope, s->name);
161 return false;
162 }
163 darg->layout->bfw->pos = prev_arg->layout->bfw->pos + prev_arg->layout->bfw->len;
164 if (max_bfw >= darg->layout->bfw->pos + darg->layout->bfw->len) {
165 pos = prev_arg->layout->pos;
166 } else {
167 darg->layout->bfw->pos = 0;
168 pos = prev_arg->layout->pos + prev_arg->layout->len;
169 }
170 } else {
171 pos = prev_arg->layout->pos + prev_arg->layout->len;
172 }
173 } else {
174 pos = 0;
175 }
176
177 align = psi_decl_arg_align(darg, &pos, &len);
178
179 if (darg->layout) {
180 if (darg->layout->pos != pos && !darg->layout->bfw) {
181 data->error(data, darg->token, PSI_WARNING,
182 "Computed offset %zu of %s.%s does not match"
183 " pre-defined offset %zu",
184 pos, s->name->val, darg->var->name->val,
185 darg->layout->pos);
186 }
187 darg->layout->pos = pos;
188 darg->layout->len = len;
189 } else {
190 darg->layout = psi_layout_init(pos, len, NULL);
191 }
192 }
193
194 if (align > s->align) {
195 s->align = align;
196 }
197 prev_arg = darg;
198 }
199
200 if (psi_plist_count(s->args)) {
201 size_t size;
202
203 psi_plist_sort(s->args, psi_layout_sort_cmp, NULL);
204 psi_plist_get(s->args, psi_plist_count(s->args) - 1, &darg);
205
206 size = darg->layout->pos + darg->layout->len;
207 if (s->size < size) {
208 s->size = psi_align(size, s->align);
209 }
210 }
211
212 assert(s->size);
213
214 return true;
215 }
216
217 size_t psi_decl_struct_get_align(struct psi_decl_struct *s)
218 {
219 if (!s) {
220 return 0;
221 }
222 if (!s->align) {
223 s->align = psi_decl_type_get_args_align(s->args);
224 }
225 return s->align;
226 }