3 # Copyright (c) 2015 Elvira Khabirova <lineprinter0@gmail.com>
4 # Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
10 # 1. Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
15 # 3. The name of the author may not be used to endorse or promote products
16 # derived from this software without specific prior written permission.
18 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 function compare_indices(i1, v1, i2, v2) {
30 c1 = strtonum(sprintf("%s", i1))
31 c2 = strtonum(sprintf("%s", i2))
36 function array_get(array_idx, array_member, array_return)
38 array_return = array[array_idx][array_member]
39 if ("" == array_return) {
40 printf("%s: index [%s] without %s\n",
41 FILENAME, array_idx, array_member) > "/dev/stderr"
46 function array_seq(array_idx)
48 if ("seq" in array[array_idx])
49 return array[array_idx]["seq"]
51 array[array_idx]["seq"] = index_seq
54 function enter(array_idx)
56 if (array_idx in called) {
57 printf("%s: index loop detected:", FILENAME) > "/dev/stderr"
59 printf(" %s", item) > "/dev/stderr"
60 print "" > "/dev/stderr"
65 function leave(array_idx, to_return)
67 delete called[array_idx]
70 function what_is(what_idx, type_idx, special, item, \
71 location, prev_location, prev_returned_size)
74 special = array_get(what_idx, "special")
77 switch (array_get(what_idx, "encoding")) {
80 8 * array_get(what_idx, "byte_size"))
84 8 * array_get(what_idx, "byte_size"))
86 default: # float, signed/unsigned char
87 printf("%s ", array_get(what_idx, "name"))
90 returned_size = array_get(what_idx, "byte_size")
92 case "enumeration_type":
93 type_idx = array_get(what_idx, "type")
94 returned_size = array_get(what_idx, "byte_size")
95 printf("uint%s_t ", 8 * returned_size)
98 printf("mpers_ptr_t ")
99 returned_size = array_get(what_idx, "byte_size")
102 type_idx = array_get(what_idx, "type")
104 to_return = array[what_idx]["upper_bound"]
107 returned_size = to_return * returned_size
108 return leave(what_idx, to_return)
110 case "structure_type":
115 prev_returned_size = 0
116 for (item in array) {
117 if ("parent" in array[item] && \
118 array_get(item, "parent") == what_idx) {
119 location = array_get(item, "location")
120 loc_diff = location - prev_location - \
123 printf("unsigned char mpers_%s_%s[%s];\n",
124 "filler", array_seq(item), loc_diff)
126 prev_location = location
127 returned = what_is(item)
128 prev_returned_size = returned_size
129 printf("%s", array[item]["name"])
130 if ("" != returned) {
131 printf("[%s]", returned)
136 returned_size = array_get(what_idx, "byte_size")
137 loc_diff = returned_size - prev_location - prev_returned_size
139 printf("unsigned char mpers_%s_%s[%s];\n",
140 "end_filler", array_seq(item), loc_diff)
142 printf("} ATTRIBUTE_PACKED ")
146 for (item in array) {
147 if ("parent" in array[item] && \
148 array_get(item, "parent") == what_idx) {
149 returned = what_is(item)
150 printf("%s", array_get(item, "name"))
151 if ("" != returned) {
152 printf("[%s]", returned)
158 returned_size = array_get(what_idx, "byte_size")
161 type_idx = array_get(what_idx, "type")
162 return leave(what_idx, what_is(type_idx))
165 type_idx = array_get(what_idx, "type")
166 return leave(what_idx, what_is(type_idx))
169 type_idx = array_get(what_idx, "type")
173 return leave(what_idx, "")
176 match(ARCH_FLAG, /[[:digit:]]+/, temparray)
177 default_pointer_size = temparray[0] / 8
178 print "#include <inttypes.h>"
181 match($0, /([[:alnum:]]+)><([[:alnum:]]+)/, matches)
183 idx = "0x" matches[2]
184 array[idx]["idx"] = idx
187 /^DW_AT_data_member_location/ {
188 if (!match($0, /\(DW_OP_plus_uconst:[[:space:]]+([[:digit:]]+)\)/, temparray))
189 match($0, /([[:digit:]]+)/, temparray)
190 array[idx]["location"] = temparray[1]
193 match($0, /:[[:space:]]+([[:alpha:]_][[:alnum:]_[:space:]]*)/, \
195 array[idx]["name"] = temparray[1]
198 match($0, /[[:digit:]]+/, temparray)
199 array[idx]["byte_size"] = temparray[0]
202 match($0, /[[:digit:]]+/, temparray)
203 array[idx]["encoding"] = temparray[0]
206 match($0, /:[[:space:]]+<(0x[[:xdigit:]]*)>$/, temparray)
207 array[idx]["type"] = temparray[1]
209 /^DW_AT_upper_bound/ {
210 match($0, /[[:digit:]]+/, temparray)
211 array[parent[level-1]]["upper_bound"] = temparray[0] + 1
214 match($0, /[[:digit:]]+/, temparray)
215 array[parent[level-1]]["upper_bound"] = temparray[0]
217 /^Abbrev Number:[^(]+\(DW_TAG_/ {
218 if (match($0, /typedef|union_type|structure_type|pointer_type\
219 |enumeration_type|array_type|base_type|member/, temparray)) {
220 array[idx]["special"] = temparray[0]
221 if ("pointer_type" == temparray[0])
222 array[idx]["byte_size"] = default_pointer_size
223 if (level > 1 && "member" == temparray[0])
224 array[idx]["parent"] = parent[level-1]
228 PROCINFO["sorted_in"] = "compare_indices"
229 for (item in array) {
230 if (array[item]["special"] == "pointer_type") {
231 print "typedef uint" \
232 8 * array_get(item, "byte_size") "_t mpers_ptr_t;"
236 for (item in array) {
237 if (array[item]["name"] == VAR_NAME) {
238 type = array_get(item, "type")
241 name = array_get(type, "name")
242 print ARCH_FLAG "_" name ";"
243 print "#define MPERS_" \
244 ARCH_FLAG "_" name " " \