blob: f511acff37d223e63913aa0e89c86db16e818a2d [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}
Elvira Khabirovaa5ffa182015-11-26 03:03:24 +030018function what_is(what_idx, type_idx, special, item, \
19 location, prev_location, prev_returned_size)
Elvira Khabirova09294222015-08-04 01:47:02 +030020{
Dmitry V. Levin59992182015-12-15 00:26:46 +000021 special = array_get(what_idx, "special")
Elvira Khabirova09294222015-08-04 01:47:02 +030022 switch (special) {
23 case "base_type":
Dmitry V. Levin59992182015-12-15 00:26:46 +000024 switch (array_get(what_idx, "encoding")) {
Elvira Khabirova09294222015-08-04 01:47:02 +030025 case 5: # signed
26 printf("%s ", "int" \
Dmitry V. Levin59992182015-12-15 00:26:46 +000027 8 * array_get(what_idx, "byte_size") "_t")
Elvira Khabirova09294222015-08-04 01:47:02 +030028 break
29 case 7: # unsigned
30 printf("%s ", "uint" \
Dmitry V. Levin59992182015-12-15 00:26:46 +000031 8 * array_get(what_idx, "byte_size") "_t")
Elvira Khabirova09294222015-08-04 01:47:02 +030032 break
33 default: # float, signed/unsigned char
Dmitry V. Levin59992182015-12-15 00:26:46 +000034 printf("%s ", array_get(what_idx, "name"))
Elvira Khabirova09294222015-08-04 01:47:02 +030035 break
36 }
Dmitry V. Levin59992182015-12-15 00:26:46 +000037 returned_size = array_get(what_idx, "byte_size")
Elvira Khabirova09294222015-08-04 01:47:02 +030038 break
39 case "enumeration_type":
Dmitry V. Levin59992182015-12-15 00:26:46 +000040 type_idx = array_get(what_idx, "type")
41 returned_size = array_get(what_idx, "byte_size")
42 printf("%s ", "uint" 8 * returned_size "_t")
Elvira Khabirova09294222015-08-04 01:47:02 +030043 break
44 case "pointer_type":
45 printf("%s", "mpers_ptr_t ")
Dmitry V. Levin59992182015-12-15 00:26:46 +000046 returned_size = array_get(what_idx, "byte_size")
Elvira Khabirova09294222015-08-04 01:47:02 +030047 break
48 case "array_type":
Dmitry V. Levin59992182015-12-15 00:26:46 +000049 type_idx = array_get(what_idx, "type")
Elvira Khabirova09294222015-08-04 01:47:02 +030050 what_is(type_idx)
51 to_return = array[what_idx]["upper_bound"]
Dmitry V. Levin59992182015-12-15 00:26:46 +000052 returned_size = to_return * returned_size
53 if ("" == to_return)
54 to_return = "00"
Elvira Khabirova09294222015-08-04 01:47:02 +030055 return to_return
56 break
57 case "structure_type":
Elvira Khabirovaa5ffa182015-11-26 03:03:24 +030058 print "struct {"
59 prev_location = 0
60 location = 0
61 returned_size = 0
62 prev_returned_size = 0
Elvira Khabirova09294222015-08-04 01:47:02 +030063 for (item in array) {
Elvira Khabirovaa5ffa182015-11-26 03:03:24 +030064 if ("parent" in array[item] && \
Dmitry V. Levin59992182015-12-15 00:26:46 +000065 array_get(item, "parent") == what_idx) {
66 location = array_get(item, "location")
Elvira Khabirovaa5ffa182015-11-26 03:03:24 +030067 loc_diff = location - prev_location - \
68 prev_returned_size
69 if (loc_diff != 0) {
70 printf("%s", \
71 "unsigned char mpers_filler_" \
72 item "[" loc_diff "];\n")
73 }
74 prev_location = location
75 returned = what_is(item)
76 prev_returned_size = returned_size
77 printf("%s", array[item]["name"])
78 if (returned) {
Dmitry V. Levin59992182015-12-15 00:26:46 +000079 printf("[%s]", returned)
Elvira Khabirovaa5ffa182015-11-26 03:03:24 +030080 }
81 print ";"
82 }
83 }
Dmitry V. Levin59992182015-12-15 00:26:46 +000084 returned_size = array_get(what_idx, "byte_size")
Elvira Khabirovaa5ffa182015-11-26 03:03:24 +030085 loc_diff = returned_size - prev_location - prev_returned_size
86 if (loc_diff != 0) {
87 printf("%s", "unsigned char mpers_end_filler_" \
88 item "[" loc_diff "];\n")
89 }
90 printf("%s", "} ATTRIBUTE_PACKED ")
91 break
92 case "union_type":
93 print "union {"
94 for (item in array) {
95 if ("parent" in array[item] && \
Dmitry V. Levin59992182015-12-15 00:26:46 +000096 array_get(item, "parent") == what_idx) {
Elvira Khabirova09294222015-08-04 01:47:02 +030097 returned = what_is(item)
Dmitry V. Levin59992182015-12-15 00:26:46 +000098 printf("%s", array_get(item, "name"))
Elvira Khabirova09294222015-08-04 01:47:02 +030099 if (returned) {
Dmitry V. Levin59992182015-12-15 00:26:46 +0000100 printf("[%s]", returned)
Elvira Khabirova09294222015-08-04 01:47:02 +0300101 }
102 print ";"
103 }
104 }
105 printf("%s", "} ")
Dmitry V. Levin59992182015-12-15 00:26:46 +0000106 returned_size = array_get(what_idx, "byte_size")
Elvira Khabirova09294222015-08-04 01:47:02 +0300107 break
108 case "typedef":
Dmitry V. Levin59992182015-12-15 00:26:46 +0000109 type_idx = array_get(what_idx, "type")
Elvira Khabirova09294222015-08-04 01:47:02 +0300110 return what_is(type_idx)
111 break
112 case "member":
Dmitry V. Levin59992182015-12-15 00:26:46 +0000113 type_idx = array_get(what_idx, "type")
Elvira Khabirova09294222015-08-04 01:47:02 +0300114 return what_is(type_idx)
115 break
116 default:
Dmitry V. Levin59992182015-12-15 00:26:46 +0000117 type_idx = array_get(what_idx, "type")
Elvira Khabirova09294222015-08-04 01:47:02 +0300118 what_is(type_idx)
119 break
120 }
121 return 0
122}
123BEGIN {
124 print "#include <inttypes.h>"
125}
126/^<[[:xdigit:]]+>/ {
127 match($0, /([[:alnum:]]+)><([[:alnum:]]+)/, matches)
128 level = matches[1]
129 idx = "0x" matches[2]
130 array[idx]["idx"] = idx
131 parent[level] = idx
132 if (level > 1) {
133 array[idx]["parent"] = parent[level-1]
134 }
135}
136/^DW_AT_data_member_location/ {
137 match($0, /[[:digit:]]+/, temparray)
Elvira Khabirovaa5ffa182015-11-26 03:03:24 +0300138 array[idx]["location"] = temparray[0]
Elvira Khabirova09294222015-08-04 01:47:02 +0300139}
140/^DW_AT_name/ {
Elvira Khabirovaa5ffa182015-11-26 03:03:24 +0300141 match($0, /:[[:space:]]+([[:alpha:]_][[:alnum:]_[:space:]]*)/, \
142 temparray)
Elvira Khabirova09294222015-08-04 01:47:02 +0300143 array[idx]["name"] = temparray[1]
144}
145/^DW_AT_byte_size/ {
146 match($0, /[[:digit:]]+/, temparray)
147 array[idx]["byte_size"] = temparray[0]
148}
149/^DW_AT_encoding/ {
150 match($0, /[[:digit:]]+/, temparray)
151 array[idx]["encoding"] = temparray[0]
152}
153/^DW_AT_type/ {
154 match($0, /:[[:space:]]+<(0x[[:xdigit:]]*)>$/, temparray)
155 array[idx]["type"] = temparray[1]
156}
157/^DW_AT_upper_bound/ {
158 match($0, /[[:digit:]]+/, temparray)
159 array[parent[level-1]]["upper_bound"] = temparray[0] + 1
160}
161/^Abbrev Number:[^(]+\(DW_TAG_/ {
162 if (match($0, /typedef|union_type|structure_type|pointer_type\
163|enumeration_type|array_type|base_type|member/, temparray)) {
164 array[idx]["special"] = temparray[0]
165 }
166}
167END {
168 PROCINFO["sorted_in"] = "compare_indices"
169 for (item in array) {
170 if (array[item]["special"] == "pointer_type") {
171 print "typedef uint" \
Dmitry V. Levin59992182015-12-15 00:26:46 +0000172 8 * array_get(item, "byte_size") "_t mpers_ptr_t;"
Elvira Khabirova09294222015-08-04 01:47:02 +0300173 break
174 }
175 }
176 for (item in array) {
177 if (array[item]["name"] == VAR_NAME) {
Dmitry V. Levin59992182015-12-15 00:26:46 +0000178 type = array_get(item, "type")
Elvira Khabirova09294222015-08-04 01:47:02 +0300179 print "typedef"
Dmitry V. Levin59992182015-12-15 00:26:46 +0000180 what_is(type)
181 name = array_get(type, "name")
182 print ARCH_FLAG "_" name ";"
Elvira Khabirova09294222015-08-04 01:47:02 +0300183 print "#define MPERS_" \
Dmitry V. Levin59992182015-12-15 00:26:46 +0000184 ARCH_FLAG "_" name " " \
185 ARCH_FLAG "_" name
Elvira Khabirova09294222015-08-04 01:47:02 +0300186 break
187 }
188 }
189}