9ba53e97348a3856dc0d98e806b140a889068bd7
1 /*******************************************************************************
2 Copyright (c) 2016, Michael Wallner <mike@php.net>.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
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.
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 *******************************************************************************/
26 #include "php_psi_stdinc.h"
31 struct psi_decl_struct
* psi_decl_struct_init(const char *name
,
32 struct psi_plist
*args
)
34 struct psi_decl_struct
*s
= calloc(1, sizeof(*s
));
35 s
->name
= strdup(name
);
40 void psi_decl_struct_free(struct psi_decl_struct
**s_ptr
)
43 struct psi_decl_struct
*s
= *s_ptr
;
50 psi_plist_free(s
->args
);
52 if (s
->engine
.type
&& s
->engine
.dtor
) {
53 s
->engine
.dtor(s
->engine
.type
);
60 void psi_decl_struct_dump(int fd
, struct psi_decl_struct
*strct
)
62 dprintf(fd
, "struct %s::(%zu, %zu)", strct
->name
, strct
->align
,
64 if (psi_plist_count(strct
->args
)) {
65 psi_decl_type_dump_args_with_layout(fd
, strct
->args
, 0);
71 struct psi_decl_arg
*psi_decl_struct_get_arg(struct psi_decl_struct
*s
,
72 struct psi_decl_var
*var
)
75 return psi_decl_arg_get_by_var(var
, s
->args
, NULL
);
81 bool psi_decl_struct_validate(struct psi_data
*data
, struct psi_decl_struct
*s
,
82 struct psi_validate_stack
*type_stack
)
84 size_t i
, pos
, len
, size
, align
;
85 struct psi_decl_arg
*darg
, *prev_arg
;
90 if (psi_validate_stack_has_struct(type_stack
, s
->name
)) {
93 psi_validate_stack_add_struct(type_stack
, s
->name
, s
);
95 if (!s
->size
&& !psi_plist_count(s
->args
)) {
96 data
->error(data
, s
->token
, PSI_WARNING
,
97 "Cannot compute size of empty struct '%s'", s
->name
);
101 for (i
= 0; psi_plist_get(s
->args
, i
, &darg
); ++i
) {
102 darg
->var
->arg
= darg
;
104 if (!psi_decl_arg_validate(data
, darg
, type_stack
)) {
108 if (darg
->layout
&& darg
->layout
->len
) {
109 pos
= darg
->layout
->pos
;
110 align
= psi_decl_arg_align(darg
, &pos
, &len
);
113 data
->error(data
, darg
->token
, PSI_WARNING
,
114 "Computed zero alignment of %s.%s of type '%s'",
115 len
, s
->name
, darg
->var
->name
, darg
->type
->name
);
119 if (darg
->layout
->len
!= len
) {
120 data
->error(data
, darg
->token
, PSI_WARNING
,
121 "Computed size %zu of %s.%s does not match"
122 " pre-defined size %zu of type '%s'",
123 len
, s
->name
, darg
->var
->name
, darg
->layout
->len
,
126 if (darg
->layout
->pos
!= pos
) {
127 data
->error(data
, darg
->token
, PSI_WARNING
,
128 "Computed offset %zu of %s.%s does not match"
129 " pre-defined offset %zu",
130 pos
, s
->name
, darg
->var
->name
, darg
->layout
->pos
);
134 if (prev_arg
->layout
&& prev_arg
->layout
->bfw
&& darg
->layout
&& darg
->layout
->bfw
) {
135 struct psi_decl_type
*real
= NULL
;
136 size_t max_bfw
= 8 * psi_decl_type_get_size(prev_arg
->type
, &real
);
138 switch (real
->type
) {
149 data
->error(data
, darg
->token
, PSI_WARNING
,
150 "Unsupported type for bit field: %s", real
->name
);
153 darg
->layout
->bfw
->pos
= prev_arg
->layout
->bfw
->pos
+ prev_arg
->layout
->bfw
->len
;
154 if (max_bfw
>= darg
->layout
->bfw
->pos
+ darg
->layout
->bfw
->len
) {
155 pos
= prev_arg
->layout
->pos
;
157 darg
->layout
->bfw
->pos
= 0;
158 pos
= prev_arg
->layout
->pos
+ prev_arg
->layout
->len
;
161 pos
= prev_arg
->layout
->pos
+ prev_arg
->layout
->len
;
167 align
= psi_decl_arg_align(darg
, &pos
, &len
);
170 if (darg
->layout
->pos
!= pos
) {
171 data
->error(data
, darg
->token
, PSI_WARNING
,
172 "Computed offset %zu of %s.%s does not match"
173 " pre-defined offset %zu",
174 pos
, s
->name
, darg
->var
->name
, darg
->layout
->pos
);
176 darg
->layout
->pos
= pos
;
177 darg
->layout
->len
= len
;
179 darg
->layout
= psi_layout_init(pos
, len
, NULL
);
183 if (align
> s
->align
) {
189 if (psi_plist_count(s
->args
)) {
190 psi_plist_sort(s
->args
, psi_layout_sort_cmp
, NULL
);
191 psi_plist_get(s
->args
, psi_plist_count(s
->args
) - 1, &darg
);
193 size
= darg
->layout
->pos
+ darg
->layout
->len
;
194 if (s
->size
< size
) {
195 s
->size
= psi_align(size
, s
->align
);
202 size_t psi_decl_struct_get_align(struct psi_decl_struct
*s
)
208 s
->align
= psi_decl_type_get_args_align(s
->args
);