blob: 4c2def2fb2b417d0ceb96425a00e96f58114d245 [file] [log] [blame]
Elvira Khabirova09294222015-08-04 01:47:02 +03001function compare_indices(i1, v1, i2, v2) {
2 c1 = strtonum(sprintf("%s", i1))
3 c2 = strtonum(sprintf("%s", i2))
4 if (c1 < c2)
5 return -1
6 return (c1 != c2)
7}
Dmitry V. Levin59992182015-12-15 00:26:46 +00008function array_get(array_idx, array_member, array_return)
9{
10 array_return = array[array_idx][array_member]
11 if ("" == array_return) {
12 printf("%s: index [%s] without %s\n",
13 FILENAME, array_idx, array_member) > "/dev/stderr"
14 exit 1
15 }
16 return array_return
17}
Dmitry V. Levin54d18a22015-12-15 03:35:26 +000018function enter(array_idx)
19{
20 if (called[array_idx]) {
21 printf("%s: index loop detected:", FILENAME) > "/dev/stderr"
22 for (item in called)
23 printf(" %s", item) > "/dev/stderr"
24 print "" > "/dev/stderr"
25 exit 1
26 }
27 called[array_idx] = 1
28}
29function leave(array_idx, to_return)
30{
31 delete called[array_idx]
32 return to_return
33}
Elvira Khabirovaa5ffa182015-11-26 03:03:24 +030034function what_is(what_idx, type_idx, special, item, \
35 location, prev_location, prev_returned_size)
Elvira Khabirova09294222015-08-04 01:47:02 +030036{
Dmitry V. Levin54d18a22015-12-15 03:35:26 +000037 enter(what_idx)
Dmitry V. Levin59992182015-12-15 00:26:46 +000038 special = array_get(what_idx, "special")
Elvira Khabirova09294222015-08-04 01:47:02 +030039 switch (special) {
40 case "base_type":
Dmitry V. Levin59992182015-12-15 00:26:46 +000041 switch (array_get(what_idx, "encoding")) {
Elvira Khabirova09294222015-08-04 01:47:02 +030042 case 5: # signed
43 printf("%s ", "int" \
Dmitry V. Levin59992182015-12-15 00:26:46 +000044 8 * array_get(what_idx, "byte_size") "_t")
Elvira Khabirova09294222015-08-04 01:47:02 +030045 break
46 case 7: # unsigned
47 printf("%s ", "uint" \
Dmitry V. Levin59992182015-12-15 00:26:46 +000048 8 * array_get(what_idx, "byte_size") "_t")
Elvira Khabirova09294222015-08-04 01:47:02 +030049 break
50 default: # float, signed/unsigned char
Dmitry V. Levin59992182015-12-15 00:26:46 +000051 printf("%s ", array_get(what_idx, "name"))
Elvira Khabirova09294222015-08-04 01:47:02 +030052 break
53 }
Dmitry V. Levin59992182015-12-15 00:26:46 +000054 returned_size = array_get(what_idx, "byte_size")
Elvira Khabirova09294222015-08-04 01:47:02 +030055 break
56 case "enumeration_type":
Dmitry V. Levin59992182015-12-15 00:26:46 +000057 type_idx = array_get(what_idx, "type")
58 returned_size = array_get(what_idx, "byte_size")
59 printf("%s ", "uint" 8 * returned_size "_t")
Elvira Khabirova09294222015-08-04 01:47:02 +030060 break
61 case "pointer_type":
62 printf("%s", "mpers_ptr_t ")
Dmitry V. Levin59992182015-12-15 00:26:46 +000063 returned_size = array_get(what_idx, "byte_size")
Elvira Khabirova09294222015-08-04 01:47:02 +030064 break
65 case "array_type":
Dmitry V. Levin59992182015-12-15 00:26:46 +000066 type_idx = array_get(what_idx, "type")
Elvira Khabirova09294222015-08-04 01:47:02 +030067 what_is(type_idx)
68 to_return = array[what_idx]["upper_bound"]
Dmitry V. Levin59992182015-12-15 00:26:46 +000069 returned_size = to_return * returned_size
70 if ("" == to_return)
71 to_return = "00"
Dmitry V. Levin54d18a22015-12-15 03:35:26 +000072 return leave(what_idx, to_return)
Elvira Khabirova09294222015-08-04 01:47:02 +030073 break
74 case "structure_type":
Elvira Khabirovaa5ffa182015-11-26 03:03:24 +030075 print "struct {"
76 prev_location = 0
77 location = 0
78 returned_size = 0
79 prev_returned_size = 0
Elvira Khabirova09294222015-08-04 01:47:02 +030080 for (item in array) {
Elvira Khabirovaa5ffa182015-11-26 03:03:24 +030081 if ("parent" in array[item] && \
Dmitry V. Levin59992182015-12-15 00:26:46 +000082 array_get(item, "parent") == what_idx) {
83 location = array_get(item, "location")
Elvira Khabirovaa5ffa182015-11-26 03:03:24 +030084 loc_diff = location - prev_location - \
85 prev_returned_size
86 if (loc_diff != 0) {
87 printf("%s", \
88 "unsigned char mpers_filler_" \
89 item "[" loc_diff "];\n")
90 }
91 prev_location = location
92 returned = what_is(item)
93 prev_returned_size = returned_size
94 printf("%s", array[item]["name"])
95 if (returned) {
Dmitry V. Levin59992182015-12-15 00:26:46 +000096 printf("[%s]", returned)
Elvira Khabirovaa5ffa182015-11-26 03:03:24 +030097 }
98 print ";"
99 }
100 }
Dmitry V. Levin59992182015-12-15 00:26:46 +0000101 returned_size = array_get(what_idx, "byte_size")
Elvira Khabirovaa5ffa182015-11-26 03:03:24 +0300102 loc_diff = returned_size - prev_location - prev_returned_size
103 if (loc_diff != 0) {
104 printf("%s", "unsigned char mpers_end_filler_" \
105 item "[" loc_diff "];\n")
106 }
107 printf("%s", "} ATTRIBUTE_PACKED ")
108 break
109 case "union_type":
110 print "union {"
111 for (item in array) {
112 if ("parent" in array[item] && \
Dmitry V. Levin59992182015-12-15 00:26:46 +0000113 array_get(item, "parent") == what_idx) {
Elvira Khabirova09294222015-08-04 01:47:02 +0300114 returned = what_is(item)
Dmitry V. Levin59992182015-12-15 00:26:46 +0000115 printf("%s", array_get(item, "name"))
Elvira Khabirova09294222015-08-04 01:47:02 +0300116 if (returned) {
Dmitry V. Levin59992182015-12-15 00:26:46 +0000117 printf("[%s]", returned)
Elvira Khabirova09294222015-08-04 01:47:02 +0300118 }
119 print ";"
120 }
121 }
122 printf("%s", "} ")
Dmitry V. Levin59992182015-12-15 00:26:46 +0000123 returned_size = array_get(what_idx, "byte_size")
Elvira Khabirova09294222015-08-04 01:47:02 +0300124 break
125 case "typedef":
Dmitry V. Levin59992182015-12-15 00:26:46 +0000126 type_idx = array_get(what_idx, "type")
Dmitry V. Levin54d18a22015-12-15 03:35:26 +0000127 return leave(what_idx, what_is(type_idx))
Elvira Khabirova09294222015-08-04 01:47:02 +0300128 break
129 case "member":
Dmitry V. Levin59992182015-12-15 00:26:46 +0000130 type_idx = array_get(what_idx, "type")
Dmitry V. Levin54d18a22015-12-15 03:35:26 +0000131 return leave(what_idx, what_is(type_idx))
Elvira Khabirova09294222015-08-04 01:47:02 +0300132 break
133 default:
Dmitry V. Levin59992182015-12-15 00:26:46 +0000134 type_idx = array_get(what_idx, "type")
Elvira Khabirova09294222015-08-04 01:47:02 +0300135 what_is(type_idx)
136 break
137 }
Dmitry V. Levin54d18a22015-12-15 03:35:26 +0000138 return leave(what_idx, 0)
Elvira Khabirova09294222015-08-04 01:47:02 +0300139}
140BEGIN {
141 print "#include <inttypes.h>"
142}
143/^<[[:xdigit:]]+>/ {
144 match($0, /([[:alnum:]]+)><([[:alnum:]]+)/, matches)
145 level = matches[1]
146 idx = "0x" matches[2]
147 array[idx]["idx"] = idx
148 parent[level] = idx
149 if (level > 1) {
150 array[idx]["parent"] = parent[level-1]
151 }
152}
153/^DW_AT_data_member_location/ {
154 match($0, /[[:digit:]]+/, temparray)
Elvira Khabirovaa5ffa182015-11-26 03:03:24 +0300155 array[idx]["location"] = temparray[0]
Elvira Khabirova09294222015-08-04 01:47:02 +0300156}
157/^DW_AT_name/ {
Elvira Khabirovaa5ffa182015-11-26 03:03:24 +0300158 match($0, /:[[:space:]]+([[:alpha:]_][[:alnum:]_[:space:]]*)/, \
159 temparray)
Elvira Khabirova09294222015-08-04 01:47:02 +0300160 array[idx]["name"] = temparray[1]
161}
162/^DW_AT_byte_size/ {
163 match($0, /[[:digit:]]+/, temparray)
164 array[idx]["byte_size"] = temparray[0]
165}
166/^DW_AT_encoding/ {
167 match($0, /[[:digit:]]+/, temparray)
168 array[idx]["encoding"] = temparray[0]
169}
170/^DW_AT_type/ {
171 match($0, /:[[:space:]]+<(0x[[:xdigit:]]*)>$/, temparray)
172 array[idx]["type"] = temparray[1]
173}
174/^DW_AT_upper_bound/ {
175 match($0, /[[:digit:]]+/, temparray)
176 array[parent[level-1]]["upper_bound"] = temparray[0] + 1
177}
178/^Abbrev Number:[^(]+\(DW_TAG_/ {
179 if (match($0, /typedef|union_type|structure_type|pointer_type\
180|enumeration_type|array_type|base_type|member/, temparray)) {
181 array[idx]["special"] = temparray[0]
182 }
183}
184END {
185 PROCINFO["sorted_in"] = "compare_indices"
186 for (item in array) {
187 if (array[item]["special"] == "pointer_type") {
188 print "typedef uint" \
Dmitry V. Levin59992182015-12-15 00:26:46 +0000189 8 * array_get(item, "byte_size") "_t mpers_ptr_t;"
Elvira Khabirova09294222015-08-04 01:47:02 +0300190 break
191 }
192 }
193 for (item in array) {
194 if (array[item]["name"] == VAR_NAME) {
Dmitry V. Levin59992182015-12-15 00:26:46 +0000195 type = array_get(item, "type")
Elvira Khabirova09294222015-08-04 01:47:02 +0300196 print "typedef"
Dmitry V. Levin59992182015-12-15 00:26:46 +0000197 what_is(type)
198 name = array_get(type, "name")
199 print ARCH_FLAG "_" name ";"
Elvira Khabirova09294222015-08-04 01:47:02 +0300200 print "#define MPERS_" \
Dmitry V. Levin59992182015-12-15 00:26:46 +0000201 ARCH_FLAG "_" name " " \
202 ARCH_FLAG "_" name
Elvira Khabirova09294222015-08-04 01:47:02 +0300203 break
204 }
205 }
206}