blob: 99248c57038625babd90e8247bc4686254dd426d [file] [log] [blame]
Dmitry V. Levin38a34c92015-12-17 17:56:48 +00001#!/bin/gawk
2#
3# Copyright (c) 2015 Elvira Khabirova <lineprinter0@gmail.com>
Dmitry V. Levincc902e32016-01-13 02:31:12 +00004# Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
Dmitry V. Levin38a34c92015-12-17 17:56:48 +00005# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
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.
17#
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.
28
Elvira Khabirova09294222015-08-04 01:47:02 +030029function compare_indices(i1, v1, i2, v2) {
30 c1 = strtonum(sprintf("%s", i1))
31 c2 = strtonum(sprintf("%s", i2))
32 if (c1 < c2)
33 return -1
34 return (c1 != c2)
35}
Dmitry V. Levin59992182015-12-15 00:26:46 +000036function array_get(array_idx, array_member, array_return)
37{
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"
42 exit 1
43 }
44 return array_return
45}
Dmitry V. Levinc4afd6d2015-12-15 12:58:42 +000046function array_seq(array_idx)
47{
48 if ("seq" in array[array_idx])
49 return array[array_idx]["seq"]
50 index_seq++
51 array[array_idx]["seq"] = index_seq
52 return index_seq
53}
Dmitry V. Levin54d18a22015-12-15 03:35:26 +000054function enter(array_idx)
55{
Dmitry V. Levinc4afd6d2015-12-15 12:58:42 +000056 if (array_idx in called) {
Dmitry V. Levin54d18a22015-12-15 03:35:26 +000057 printf("%s: index loop detected:", FILENAME) > "/dev/stderr"
58 for (item in called)
59 printf(" %s", item) > "/dev/stderr"
60 print "" > "/dev/stderr"
61 exit 1
62 }
63 called[array_idx] = 1
64}
65function leave(array_idx, to_return)
66{
67 delete called[array_idx]
68 return to_return
69}
Elvira Khabirovaa5ffa182015-11-26 03:03:24 +030070function what_is(what_idx, type_idx, special, item, \
71 location, prev_location, prev_returned_size)
Elvira Khabirova09294222015-08-04 01:47:02 +030072{
Dmitry V. Levin54d18a22015-12-15 03:35:26 +000073 enter(what_idx)
Dmitry V. Levin59992182015-12-15 00:26:46 +000074 special = array_get(what_idx, "special")
Elvira Khabirova09294222015-08-04 01:47:02 +030075 switch (special) {
76 case "base_type":
Dmitry V. Levin59992182015-12-15 00:26:46 +000077 switch (array_get(what_idx, "encoding")) {
Elvira Khabirova09294222015-08-04 01:47:02 +030078 case 5: # signed
Dmitry V. Levinc4afd6d2015-12-15 12:58:42 +000079 printf("int%s_t ",
80 8 * array_get(what_idx, "byte_size"))
Elvira Khabirova09294222015-08-04 01:47:02 +030081 break
82 case 7: # unsigned
Dmitry V. Levinc4afd6d2015-12-15 12:58:42 +000083 printf("uint%s_t ",
84 8 * array_get(what_idx, "byte_size"))
Elvira Khabirova09294222015-08-04 01:47:02 +030085 break
86 default: # float, signed/unsigned char
Dmitry V. Levin59992182015-12-15 00:26:46 +000087 printf("%s ", array_get(what_idx, "name"))
Elvira Khabirova09294222015-08-04 01:47:02 +030088 break
89 }
Dmitry V. Levin59992182015-12-15 00:26:46 +000090 returned_size = array_get(what_idx, "byte_size")
Elvira Khabirova09294222015-08-04 01:47:02 +030091 break
92 case "enumeration_type":
Dmitry V. Levin59992182015-12-15 00:26:46 +000093 type_idx = array_get(what_idx, "type")
94 returned_size = array_get(what_idx, "byte_size")
Dmitry V. Levinc4afd6d2015-12-15 12:58:42 +000095 printf("uint%s_t ", 8 * returned_size)
Elvira Khabirova09294222015-08-04 01:47:02 +030096 break
97 case "pointer_type":
Dmitry V. Levinc4afd6d2015-12-15 12:58:42 +000098 printf("mpers_ptr_t ")
Dmitry V. Levin59992182015-12-15 00:26:46 +000099 returned_size = array_get(what_idx, "byte_size")
Elvira Khabirova09294222015-08-04 01:47:02 +0300100 break
101 case "array_type":
Dmitry V. Levin59992182015-12-15 00:26:46 +0000102 type_idx = array_get(what_idx, "type")
Elvira Khabirova09294222015-08-04 01:47:02 +0300103 what_is(type_idx)
104 to_return = array[what_idx]["upper_bound"]
Dmitry V. Levin59992182015-12-15 00:26:46 +0000105 if ("" == to_return)
Dmitry V. Levinc4afd6d2015-12-15 12:58:42 +0000106 to_return = 0
107 returned_size = to_return * returned_size
Dmitry V. Levin54d18a22015-12-15 03:35:26 +0000108 return leave(what_idx, to_return)
Elvira Khabirova09294222015-08-04 01:47:02 +0300109 break
110 case "structure_type":
Elvira Khabirovaa5ffa182015-11-26 03:03:24 +0300111 print "struct {"
112 prev_location = 0
113 location = 0
114 returned_size = 0
115 prev_returned_size = 0
Elvira Khabirova09294222015-08-04 01:47:02 +0300116 for (item in array) {
Elvira Khabirovaa5ffa182015-11-26 03:03:24 +0300117 if ("parent" in array[item] && \
Dmitry V. Levin59992182015-12-15 00:26:46 +0000118 array_get(item, "parent") == what_idx) {
119 location = array_get(item, "location")
Elvira Khabirovaa5ffa182015-11-26 03:03:24 +0300120 loc_diff = location - prev_location - \
121 prev_returned_size
122 if (loc_diff != 0) {
Dmitry V. Levinc4afd6d2015-12-15 12:58:42 +0000123 printf("unsigned char mpers_%s_%s[%s];\n",
124 "filler", array_seq(item), loc_diff)
Elvira Khabirovaa5ffa182015-11-26 03:03:24 +0300125 }
126 prev_location = location
127 returned = what_is(item)
128 prev_returned_size = returned_size
129 printf("%s", array[item]["name"])
Dmitry V. Levinc4afd6d2015-12-15 12:58:42 +0000130 if ("" != returned) {
Dmitry V. Levin59992182015-12-15 00:26:46 +0000131 printf("[%s]", returned)
Elvira Khabirovaa5ffa182015-11-26 03:03:24 +0300132 }
133 print ";"
134 }
135 }
Dmitry V. Levin59992182015-12-15 00:26:46 +0000136 returned_size = array_get(what_idx, "byte_size")
Elvira Khabirovaa5ffa182015-11-26 03:03:24 +0300137 loc_diff = returned_size - prev_location - prev_returned_size
138 if (loc_diff != 0) {
Dmitry V. Levinc4afd6d2015-12-15 12:58:42 +0000139 printf("unsigned char mpers_%s_%s[%s];\n",
140 "end_filler", array_seq(item), loc_diff)
Elvira Khabirovaa5ffa182015-11-26 03:03:24 +0300141 }
Dmitry V. Levinc4afd6d2015-12-15 12:58:42 +0000142 printf("} ATTRIBUTE_PACKED ")
Elvira Khabirovaa5ffa182015-11-26 03:03:24 +0300143 break
144 case "union_type":
145 print "union {"
146 for (item in array) {
147 if ("parent" in array[item] && \
Dmitry V. Levin59992182015-12-15 00:26:46 +0000148 array_get(item, "parent") == what_idx) {
Elvira Khabirova09294222015-08-04 01:47:02 +0300149 returned = what_is(item)
Dmitry V. Levin59992182015-12-15 00:26:46 +0000150 printf("%s", array_get(item, "name"))
Dmitry V. Levinc4afd6d2015-12-15 12:58:42 +0000151 if ("" != returned) {
Dmitry V. Levin59992182015-12-15 00:26:46 +0000152 printf("[%s]", returned)
Elvira Khabirova09294222015-08-04 01:47:02 +0300153 }
154 print ";"
155 }
156 }
Dmitry V. Levinc4afd6d2015-12-15 12:58:42 +0000157 printf("} ")
Dmitry V. Levin59992182015-12-15 00:26:46 +0000158 returned_size = array_get(what_idx, "byte_size")
Elvira Khabirova09294222015-08-04 01:47:02 +0300159 break
160 case "typedef":
Dmitry V. Levin59992182015-12-15 00:26:46 +0000161 type_idx = array_get(what_idx, "type")
Dmitry V. Levin54d18a22015-12-15 03:35:26 +0000162 return leave(what_idx, what_is(type_idx))
Elvira Khabirova09294222015-08-04 01:47:02 +0300163 break
164 case "member":
Dmitry V. Levin59992182015-12-15 00:26:46 +0000165 type_idx = array_get(what_idx, "type")
Dmitry V. Levin54d18a22015-12-15 03:35:26 +0000166 return leave(what_idx, what_is(type_idx))
Elvira Khabirova09294222015-08-04 01:47:02 +0300167 break
168 default:
Dmitry V. Levin59992182015-12-15 00:26:46 +0000169 type_idx = array_get(what_idx, "type")
Elvira Khabirova09294222015-08-04 01:47:02 +0300170 what_is(type_idx)
171 break
172 }
Dmitry V. Levinc4afd6d2015-12-15 12:58:42 +0000173 return leave(what_idx, "")
Elvira Khabirova09294222015-08-04 01:47:02 +0300174}
175BEGIN {
Dmitry V. Levincc902e32016-01-13 02:31:12 +0000176 match(ARCH_FLAG, /[[:digit:]]+/, temparray)
177 default_pointer_size = temparray[0] / 8
Elvira Khabirova09294222015-08-04 01:47:02 +0300178 print "#include <inttypes.h>"
179}
180/^<[[:xdigit:]]+>/ {
181 match($0, /([[:alnum:]]+)><([[:alnum:]]+)/, matches)
182 level = matches[1]
183 idx = "0x" matches[2]
184 array[idx]["idx"] = idx
185 parent[level] = idx
Elvira Khabirova09294222015-08-04 01:47:02 +0300186}
187/^DW_AT_data_member_location/ {
Dmitry V. Levind2bcb162015-12-16 01:25:10 +0000188 if (!match($0, /\(DW_OP_plus_uconst:[[:space:]]+([[:digit:]]+)\)/, temparray))
189 match($0, /([[:digit:]]+)/, temparray)
190 array[idx]["location"] = temparray[1]
Elvira Khabirova09294222015-08-04 01:47:02 +0300191}
192/^DW_AT_name/ {
Elvira Khabirovaa5ffa182015-11-26 03:03:24 +0300193 match($0, /:[[:space:]]+([[:alpha:]_][[:alnum:]_[:space:]]*)/, \
194 temparray)
Elvira Khabirova09294222015-08-04 01:47:02 +0300195 array[idx]["name"] = temparray[1]
196}
197/^DW_AT_byte_size/ {
198 match($0, /[[:digit:]]+/, temparray)
199 array[idx]["byte_size"] = temparray[0]
200}
201/^DW_AT_encoding/ {
202 match($0, /[[:digit:]]+/, temparray)
203 array[idx]["encoding"] = temparray[0]
204}
205/^DW_AT_type/ {
206 match($0, /:[[:space:]]+<(0x[[:xdigit:]]*)>$/, temparray)
207 array[idx]["type"] = temparray[1]
208}
209/^DW_AT_upper_bound/ {
210 match($0, /[[:digit:]]+/, temparray)
211 array[parent[level-1]]["upper_bound"] = temparray[0] + 1
212}
Dmitry V. Levincc902e32016-01-13 02:31:12 +0000213/^DW_AT_count/ {
214 match($0, /[[:digit:]]+/, temparray)
215 array[parent[level-1]]["upper_bound"] = temparray[0]
216}
Elvira Khabirova09294222015-08-04 01:47:02 +0300217/^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]
Dmitry V. Levincc902e32016-01-13 02:31:12 +0000221 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]
Elvira Khabirova09294222015-08-04 01:47:02 +0300225 }
226}
227END {
228 PROCINFO["sorted_in"] = "compare_indices"
229 for (item in array) {
230 if (array[item]["special"] == "pointer_type") {
231 print "typedef uint" \
Dmitry V. Levin59992182015-12-15 00:26:46 +0000232 8 * array_get(item, "byte_size") "_t mpers_ptr_t;"
Elvira Khabirova09294222015-08-04 01:47:02 +0300233 break
234 }
235 }
236 for (item in array) {
237 if (array[item]["name"] == VAR_NAME) {
Dmitry V. Levin59992182015-12-15 00:26:46 +0000238 type = array_get(item, "type")
Elvira Khabirova09294222015-08-04 01:47:02 +0300239 print "typedef"
Dmitry V. Levin59992182015-12-15 00:26:46 +0000240 what_is(type)
241 name = array_get(type, "name")
242 print ARCH_FLAG "_" name ";"
Elvira Khabirova09294222015-08-04 01:47:02 +0300243 print "#define MPERS_" \
Dmitry V. Levin59992182015-12-15 00:26:46 +0000244 ARCH_FLAG "_" name " " \
245 ARCH_FLAG "_" name
Elvira Khabirova09294222015-08-04 01:47:02 +0300246 break
247 }
248 }
249}