Dmitry V. Levin | 38a34c9 | 2015-12-17 17:56:48 +0000 | [diff] [blame^] | 1 | #!/bin/gawk |
| 2 | # |
| 3 | # Copyright (c) 2015 Elvira Khabirova <lineprinter0@gmail.com> |
| 4 | # Copyright (c) 2015 Dmitry V. Levin <ldv@altlinux.org> |
| 5 | # 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 Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 29 | function 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. Levin | 5999218 | 2015-12-15 00:26:46 +0000 | [diff] [blame] | 36 | function 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. Levin | c4afd6d | 2015-12-15 12:58:42 +0000 | [diff] [blame] | 46 | function 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. Levin | 54d18a2 | 2015-12-15 03:35:26 +0000 | [diff] [blame] | 54 | function enter(array_idx) |
| 55 | { |
Dmitry V. Levin | c4afd6d | 2015-12-15 12:58:42 +0000 | [diff] [blame] | 56 | if (array_idx in called) { |
Dmitry V. Levin | 54d18a2 | 2015-12-15 03:35:26 +0000 | [diff] [blame] | 57 | 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 | } |
| 65 | function leave(array_idx, to_return) |
| 66 | { |
| 67 | delete called[array_idx] |
| 68 | return to_return |
| 69 | } |
Elvira Khabirova | a5ffa18 | 2015-11-26 03:03:24 +0300 | [diff] [blame] | 70 | function what_is(what_idx, type_idx, special, item, \ |
| 71 | location, prev_location, prev_returned_size) |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 72 | { |
Dmitry V. Levin | 54d18a2 | 2015-12-15 03:35:26 +0000 | [diff] [blame] | 73 | enter(what_idx) |
Dmitry V. Levin | 5999218 | 2015-12-15 00:26:46 +0000 | [diff] [blame] | 74 | special = array_get(what_idx, "special") |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 75 | switch (special) { |
| 76 | case "base_type": |
Dmitry V. Levin | 5999218 | 2015-12-15 00:26:46 +0000 | [diff] [blame] | 77 | switch (array_get(what_idx, "encoding")) { |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 78 | case 5: # signed |
Dmitry V. Levin | c4afd6d | 2015-12-15 12:58:42 +0000 | [diff] [blame] | 79 | printf("int%s_t ", |
| 80 | 8 * array_get(what_idx, "byte_size")) |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 81 | break |
| 82 | case 7: # unsigned |
Dmitry V. Levin | c4afd6d | 2015-12-15 12:58:42 +0000 | [diff] [blame] | 83 | printf("uint%s_t ", |
| 84 | 8 * array_get(what_idx, "byte_size")) |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 85 | break |
| 86 | default: # float, signed/unsigned char |
Dmitry V. Levin | 5999218 | 2015-12-15 00:26:46 +0000 | [diff] [blame] | 87 | printf("%s ", array_get(what_idx, "name")) |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 88 | break |
| 89 | } |
Dmitry V. Levin | 5999218 | 2015-12-15 00:26:46 +0000 | [diff] [blame] | 90 | returned_size = array_get(what_idx, "byte_size") |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 91 | break |
| 92 | case "enumeration_type": |
Dmitry V. Levin | 5999218 | 2015-12-15 00:26:46 +0000 | [diff] [blame] | 93 | type_idx = array_get(what_idx, "type") |
| 94 | returned_size = array_get(what_idx, "byte_size") |
Dmitry V. Levin | c4afd6d | 2015-12-15 12:58:42 +0000 | [diff] [blame] | 95 | printf("uint%s_t ", 8 * returned_size) |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 96 | break |
| 97 | case "pointer_type": |
Dmitry V. Levin | c4afd6d | 2015-12-15 12:58:42 +0000 | [diff] [blame] | 98 | printf("mpers_ptr_t ") |
Dmitry V. Levin | 5999218 | 2015-12-15 00:26:46 +0000 | [diff] [blame] | 99 | returned_size = array_get(what_idx, "byte_size") |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 100 | break |
| 101 | case "array_type": |
Dmitry V. Levin | 5999218 | 2015-12-15 00:26:46 +0000 | [diff] [blame] | 102 | type_idx = array_get(what_idx, "type") |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 103 | what_is(type_idx) |
| 104 | to_return = array[what_idx]["upper_bound"] |
Dmitry V. Levin | 5999218 | 2015-12-15 00:26:46 +0000 | [diff] [blame] | 105 | if ("" == to_return) |
Dmitry V. Levin | c4afd6d | 2015-12-15 12:58:42 +0000 | [diff] [blame] | 106 | to_return = 0 |
| 107 | returned_size = to_return * returned_size |
Dmitry V. Levin | 54d18a2 | 2015-12-15 03:35:26 +0000 | [diff] [blame] | 108 | return leave(what_idx, to_return) |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 109 | break |
| 110 | case "structure_type": |
Elvira Khabirova | a5ffa18 | 2015-11-26 03:03:24 +0300 | [diff] [blame] | 111 | print "struct {" |
| 112 | prev_location = 0 |
| 113 | location = 0 |
| 114 | returned_size = 0 |
| 115 | prev_returned_size = 0 |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 116 | for (item in array) { |
Elvira Khabirova | a5ffa18 | 2015-11-26 03:03:24 +0300 | [diff] [blame] | 117 | if ("parent" in array[item] && \ |
Dmitry V. Levin | 5999218 | 2015-12-15 00:26:46 +0000 | [diff] [blame] | 118 | array_get(item, "parent") == what_idx) { |
| 119 | location = array_get(item, "location") |
Elvira Khabirova | a5ffa18 | 2015-11-26 03:03:24 +0300 | [diff] [blame] | 120 | loc_diff = location - prev_location - \ |
| 121 | prev_returned_size |
| 122 | if (loc_diff != 0) { |
Dmitry V. Levin | c4afd6d | 2015-12-15 12:58:42 +0000 | [diff] [blame] | 123 | printf("unsigned char mpers_%s_%s[%s];\n", |
| 124 | "filler", array_seq(item), loc_diff) |
Elvira Khabirova | a5ffa18 | 2015-11-26 03:03:24 +0300 | [diff] [blame] | 125 | } |
| 126 | prev_location = location |
| 127 | returned = what_is(item) |
| 128 | prev_returned_size = returned_size |
| 129 | printf("%s", array[item]["name"]) |
Dmitry V. Levin | c4afd6d | 2015-12-15 12:58:42 +0000 | [diff] [blame] | 130 | if ("" != returned) { |
Dmitry V. Levin | 5999218 | 2015-12-15 00:26:46 +0000 | [diff] [blame] | 131 | printf("[%s]", returned) |
Elvira Khabirova | a5ffa18 | 2015-11-26 03:03:24 +0300 | [diff] [blame] | 132 | } |
| 133 | print ";" |
| 134 | } |
| 135 | } |
Dmitry V. Levin | 5999218 | 2015-12-15 00:26:46 +0000 | [diff] [blame] | 136 | returned_size = array_get(what_idx, "byte_size") |
Elvira Khabirova | a5ffa18 | 2015-11-26 03:03:24 +0300 | [diff] [blame] | 137 | loc_diff = returned_size - prev_location - prev_returned_size |
| 138 | if (loc_diff != 0) { |
Dmitry V. Levin | c4afd6d | 2015-12-15 12:58:42 +0000 | [diff] [blame] | 139 | printf("unsigned char mpers_%s_%s[%s];\n", |
| 140 | "end_filler", array_seq(item), loc_diff) |
Elvira Khabirova | a5ffa18 | 2015-11-26 03:03:24 +0300 | [diff] [blame] | 141 | } |
Dmitry V. Levin | c4afd6d | 2015-12-15 12:58:42 +0000 | [diff] [blame] | 142 | printf("} ATTRIBUTE_PACKED ") |
Elvira Khabirova | a5ffa18 | 2015-11-26 03:03:24 +0300 | [diff] [blame] | 143 | break |
| 144 | case "union_type": |
| 145 | print "union {" |
| 146 | for (item in array) { |
| 147 | if ("parent" in array[item] && \ |
Dmitry V. Levin | 5999218 | 2015-12-15 00:26:46 +0000 | [diff] [blame] | 148 | array_get(item, "parent") == what_idx) { |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 149 | returned = what_is(item) |
Dmitry V. Levin | 5999218 | 2015-12-15 00:26:46 +0000 | [diff] [blame] | 150 | printf("%s", array_get(item, "name")) |
Dmitry V. Levin | c4afd6d | 2015-12-15 12:58:42 +0000 | [diff] [blame] | 151 | if ("" != returned) { |
Dmitry V. Levin | 5999218 | 2015-12-15 00:26:46 +0000 | [diff] [blame] | 152 | printf("[%s]", returned) |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 153 | } |
| 154 | print ";" |
| 155 | } |
| 156 | } |
Dmitry V. Levin | c4afd6d | 2015-12-15 12:58:42 +0000 | [diff] [blame] | 157 | printf("} ") |
Dmitry V. Levin | 5999218 | 2015-12-15 00:26:46 +0000 | [diff] [blame] | 158 | returned_size = array_get(what_idx, "byte_size") |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 159 | break |
| 160 | case "typedef": |
Dmitry V. Levin | 5999218 | 2015-12-15 00:26:46 +0000 | [diff] [blame] | 161 | type_idx = array_get(what_idx, "type") |
Dmitry V. Levin | 54d18a2 | 2015-12-15 03:35:26 +0000 | [diff] [blame] | 162 | return leave(what_idx, what_is(type_idx)) |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 163 | break |
| 164 | case "member": |
Dmitry V. Levin | 5999218 | 2015-12-15 00:26:46 +0000 | [diff] [blame] | 165 | type_idx = array_get(what_idx, "type") |
Dmitry V. Levin | 54d18a2 | 2015-12-15 03:35:26 +0000 | [diff] [blame] | 166 | return leave(what_idx, what_is(type_idx)) |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 167 | break |
| 168 | default: |
Dmitry V. Levin | 5999218 | 2015-12-15 00:26:46 +0000 | [diff] [blame] | 169 | type_idx = array_get(what_idx, "type") |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 170 | what_is(type_idx) |
| 171 | break |
| 172 | } |
Dmitry V. Levin | c4afd6d | 2015-12-15 12:58:42 +0000 | [diff] [blame] | 173 | return leave(what_idx, "") |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 174 | } |
| 175 | BEGIN { |
| 176 | print "#include <inttypes.h>" |
| 177 | } |
| 178 | /^<[[:xdigit:]]+>/ { |
| 179 | match($0, /([[:alnum:]]+)><([[:alnum:]]+)/, matches) |
| 180 | level = matches[1] |
| 181 | idx = "0x" matches[2] |
| 182 | array[idx]["idx"] = idx |
| 183 | parent[level] = idx |
| 184 | if (level > 1) { |
| 185 | array[idx]["parent"] = parent[level-1] |
| 186 | } |
| 187 | } |
| 188 | /^DW_AT_data_member_location/ { |
Dmitry V. Levin | d2bcb16 | 2015-12-16 01:25:10 +0000 | [diff] [blame] | 189 | if (!match($0, /\(DW_OP_plus_uconst:[[:space:]]+([[:digit:]]+)\)/, temparray)) |
| 190 | match($0, /([[:digit:]]+)/, temparray) |
| 191 | array[idx]["location"] = temparray[1] |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 192 | } |
| 193 | /^DW_AT_name/ { |
Elvira Khabirova | a5ffa18 | 2015-11-26 03:03:24 +0300 | [diff] [blame] | 194 | match($0, /:[[:space:]]+([[:alpha:]_][[:alnum:]_[:space:]]*)/, \ |
| 195 | temparray) |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 196 | array[idx]["name"] = temparray[1] |
| 197 | } |
| 198 | /^DW_AT_byte_size/ { |
| 199 | match($0, /[[:digit:]]+/, temparray) |
| 200 | array[idx]["byte_size"] = temparray[0] |
| 201 | } |
| 202 | /^DW_AT_encoding/ { |
| 203 | match($0, /[[:digit:]]+/, temparray) |
| 204 | array[idx]["encoding"] = temparray[0] |
| 205 | } |
| 206 | /^DW_AT_type/ { |
| 207 | match($0, /:[[:space:]]+<(0x[[:xdigit:]]*)>$/, temparray) |
| 208 | array[idx]["type"] = temparray[1] |
| 209 | } |
| 210 | /^DW_AT_upper_bound/ { |
| 211 | match($0, /[[:digit:]]+/, temparray) |
| 212 | array[parent[level-1]]["upper_bound"] = temparray[0] + 1 |
| 213 | } |
| 214 | /^Abbrev Number:[^(]+\(DW_TAG_/ { |
| 215 | if (match($0, /typedef|union_type|structure_type|pointer_type\ |
| 216 | |enumeration_type|array_type|base_type|member/, temparray)) { |
| 217 | array[idx]["special"] = temparray[0] |
| 218 | } |
| 219 | } |
| 220 | END { |
| 221 | PROCINFO["sorted_in"] = "compare_indices" |
| 222 | for (item in array) { |
| 223 | if (array[item]["special"] == "pointer_type") { |
| 224 | print "typedef uint" \ |
Dmitry V. Levin | 5999218 | 2015-12-15 00:26:46 +0000 | [diff] [blame] | 225 | 8 * array_get(item, "byte_size") "_t mpers_ptr_t;" |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 226 | break |
| 227 | } |
| 228 | } |
| 229 | for (item in array) { |
| 230 | if (array[item]["name"] == VAR_NAME) { |
Dmitry V. Levin | 5999218 | 2015-12-15 00:26:46 +0000 | [diff] [blame] | 231 | type = array_get(item, "type") |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 232 | print "typedef" |
Dmitry V. Levin | 5999218 | 2015-12-15 00:26:46 +0000 | [diff] [blame] | 233 | what_is(type) |
| 234 | name = array_get(type, "name") |
| 235 | print ARCH_FLAG "_" name ";" |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 236 | print "#define MPERS_" \ |
Dmitry V. Levin | 5999218 | 2015-12-15 00:26:46 +0000 | [diff] [blame] | 237 | ARCH_FLAG "_" name " " \ |
| 238 | ARCH_FLAG "_" name |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 239 | break |
| 240 | } |
| 241 | } |
| 242 | } |