3 # Copyright (c) 2015 Elvira Khabirova <lineprinter0@gmail.com>
4 # Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
5 # Copyright (c) 2015-2018 The strace developers.
8 # SPDX-License-Identifier: LGPL-2.1-or-later
10 function array_get(array_idx, array_member, \
13 array_return = array[array_idx, array_member]
14 if ("" == array_return) {
15 printf("%s: index [%s] without %s\n",
16 FILENAME, array_idx, array_member) > "/dev/stderr"
21 function norm_idx(idx)
23 return sprintf("%016s", idx)
25 function array_seq(array_idx)
27 if ((array_idx, "seq") in array)
28 return array[array_idx, "seq"]
30 array[array_idx, "seq"] = index_seq
33 function enter(array_idx,
36 if (array_idx in called) {
37 printf("%s: index loop detected:", FILENAME) > "/dev/stderr"
39 printf(" %s", item) > "/dev/stderr"
40 print "" > "/dev/stderr"
45 function leave(array_idx, to_return)
47 delete called[array_idx]
50 function update_upper_bound(idx, val, \
53 count = array[idx, "count"]
56 array[idx, "count"] = count * val
57 array[idx, "upper_bound"] = array[idx, "upper_bound"] "[" val "]"
59 function what_is(what_idx, \
60 item, loc_diff, location, prev_location, prev_returned_size, \
61 special, to_return, type_idx, enc, i)
64 special = array_get(what_idx, "special")
65 if (special == "base_type") {
66 enc = array_get(what_idx, "encoding")
67 if (enc == 5) { # signed
69 8 * array_get(what_idx, "byte_size"))
70 } else if (enc == 7) { # unsigned
72 8 * array_get(what_idx, "byte_size"))
73 } else { # float, signed/unsigned char
74 printf("%s ", array_get(what_idx, "name"))
76 returned_size = array_get(what_idx, "byte_size")
77 } else if (special == "enumeration_type") {
78 returned_size = array_get(what_idx, "byte_size")
79 printf("uint%s_t ", 8 * returned_size)
80 } else if (special == "pointer_type") {
81 printf("mpers_ptr_t ")
82 returned_size = array_get(what_idx, "byte_size")
83 } else if (special == "array_type") {
84 type_idx = array_get(what_idx, "type")
86 to_return = array[what_idx, "upper_bound"]
89 returned_size = array[what_idx, "count"] * returned_size
90 return leave(what_idx, to_return)
91 } else if (special == "structure_type") {
96 prev_returned_size = 0
97 for (i = 1; i <= parents_cnt; i += 1) {
98 if (array_parents[aparents_keys[i]] == what_idx) {
99 location = array_get(aparents_keys[i], "location")
100 loc_diff = location - prev_location - \
103 printf("unsigned char mpers_%s_%s[%s];\n",
104 "filler", array_seq(aparents_keys[i]), loc_diff)
106 prev_location = location
107 returned = what_is(aparents_keys[i])
108 prev_returned_size = returned_size
109 printf("%s%s;\n", array[aparents_keys[i], "name"], returned)
112 returned_size = array_get(what_idx, "byte_size")
113 loc_diff = returned_size - prev_location - prev_returned_size
115 printf("unsigned char mpers_%s_%s[%s];\n",
116 "end_filler", array_seq(item), loc_diff)
118 printf("} ATTRIBUTE_PACKED ")
119 } else if (special == "union_type") {
121 for (i = 1; i <= parents_cnt; i += 1) {
122 if (array_parents[aparents_keys[i]] == what_idx) {
123 returned = what_is(aparents_keys[i])
124 printf("%s%s;\n", array[aparents_keys[i], "name"], returned)
128 returned_size = array_get(what_idx, "byte_size")
129 } else if (special == "typedef") {
130 type_idx = array_get(what_idx, "type")
131 return leave(what_idx, what_is(type_idx))
132 } else if (special == "member") {
133 type_idx = array_get(what_idx, "type")
134 return leave(what_idx, what_is(type_idx))
136 type_idx = array_get(what_idx, "type")
139 return leave(what_idx, "")
142 match(ARCH_FLAG, /[[:digit:]]+/, temparray)
143 default_pointer_size = temparray[0] / 8
144 print "#include <stdint.h>"
147 match($0, /([[:alnum:]]+)><([[:alnum:]]+)/, matches)
149 idx = norm_idx(matches[2])
150 array[idx, "idx"] = idx
153 /^DW_AT_data_member_location/ {
154 if (!match($0, /\(DW_OP_plus_uconst:[[:space:]]+([[:digit:]]+)\)/, temparray))
155 match($0, /([[:digit:]]+)/, temparray)
156 array[idx, "location"] = temparray[1]
159 match($0, /:[[:space:]]+([[:alpha:]_][[:alnum:]_[:space:]]*)/, \
162 array[idx, "name"] = temparray[1]
165 match($0, /[[:digit:]]+/, temparray)
166 array[idx, "byte_size"] = temparray[0]
169 match($0, /[[:digit:]]+/, temparray)
170 array[idx, "encoding"] = temparray[0]
173 match($0, /:[[:space:]]+<0x([[:xdigit:]]*)>$/, temparray)
174 array[idx, "type"] = norm_idx(temparray[1])
176 /^DW_AT_upper_bound/ {
177 match($0, /[[:digit:]]+/, temparray)
178 update_upper_bound(parent[level - 1], temparray[0] + 1)
181 match($0, /[[:digit:]]+/, temparray)
182 update_upper_bound(parent[level - 1], temparray[0])
184 /^Abbrev Number:[^(]+\(DW_TAG_/ {
185 if (match($0, /typedef|union_type|structure_type|pointer_type\
186 |enumeration_type|array_type|base_type|member/, temparray)) {
187 array_special[idx] = temparray[0]
188 array[idx, "special"] = temparray[0]
189 if ("pointer_type" == temparray[0])
190 array[idx, "byte_size"] = default_pointer_size
191 if (level > 1 && "member" == temparray[0])
192 array_parents[idx] = parent[level-1]
196 parents_cnt = asorti(array_parents, aparents_keys)
198 for (item in array_special) {
199 if (array[item, "special"] == "pointer_type") {
201 "uint" 8 * array_get(item, "byte_size") "_t"
202 print "#ifndef mpers_ptr_t_is_" mpers_ptr_t
203 print "typedef " mpers_ptr_t " mpers_ptr_t;"
204 print "#define mpers_ptr_t_is_" mpers_ptr_t
209 for (item in array_names) {
210 if (array[item, "name"] == VAR_NAME) {
211 type = array_get(item, "type")
214 name = array_get(type, "name")
215 print ARCH_FLAG "_" name ";"
216 print "#define MPERS_" \
217 ARCH_FLAG "_" name " " \