Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 1 | function 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. Levin | 5999218 | 2015-12-15 00:26:46 +0000 | [diff] [blame] | 8 | function 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. Levin | c4afd6d | 2015-12-15 12:58:42 +0000 | [diff] [blame^] | 18 | function array_seq(array_idx) |
| 19 | { |
| 20 | if ("seq" in array[array_idx]) |
| 21 | return array[array_idx]["seq"] |
| 22 | index_seq++ |
| 23 | array[array_idx]["seq"] = index_seq |
| 24 | return index_seq |
| 25 | } |
Dmitry V. Levin | 54d18a2 | 2015-12-15 03:35:26 +0000 | [diff] [blame] | 26 | function enter(array_idx) |
| 27 | { |
Dmitry V. Levin | c4afd6d | 2015-12-15 12:58:42 +0000 | [diff] [blame^] | 28 | if (array_idx in called) { |
Dmitry V. Levin | 54d18a2 | 2015-12-15 03:35:26 +0000 | [diff] [blame] | 29 | printf("%s: index loop detected:", FILENAME) > "/dev/stderr" |
| 30 | for (item in called) |
| 31 | printf(" %s", item) > "/dev/stderr" |
| 32 | print "" > "/dev/stderr" |
| 33 | exit 1 |
| 34 | } |
| 35 | called[array_idx] = 1 |
| 36 | } |
| 37 | function leave(array_idx, to_return) |
| 38 | { |
| 39 | delete called[array_idx] |
| 40 | return to_return |
| 41 | } |
Elvira Khabirova | a5ffa18 | 2015-11-26 03:03:24 +0300 | [diff] [blame] | 42 | function what_is(what_idx, type_idx, special, item, \ |
| 43 | location, prev_location, prev_returned_size) |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 44 | { |
Dmitry V. Levin | 54d18a2 | 2015-12-15 03:35:26 +0000 | [diff] [blame] | 45 | enter(what_idx) |
Dmitry V. Levin | 5999218 | 2015-12-15 00:26:46 +0000 | [diff] [blame] | 46 | special = array_get(what_idx, "special") |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 47 | switch (special) { |
| 48 | case "base_type": |
Dmitry V. Levin | 5999218 | 2015-12-15 00:26:46 +0000 | [diff] [blame] | 49 | switch (array_get(what_idx, "encoding")) { |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 50 | case 5: # signed |
Dmitry V. Levin | c4afd6d | 2015-12-15 12:58:42 +0000 | [diff] [blame^] | 51 | printf("int%s_t ", |
| 52 | 8 * array_get(what_idx, "byte_size")) |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 53 | break |
| 54 | case 7: # unsigned |
Dmitry V. Levin | c4afd6d | 2015-12-15 12:58:42 +0000 | [diff] [blame^] | 55 | printf("uint%s_t ", |
| 56 | 8 * array_get(what_idx, "byte_size")) |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 57 | break |
| 58 | default: # float, signed/unsigned char |
Dmitry V. Levin | 5999218 | 2015-12-15 00:26:46 +0000 | [diff] [blame] | 59 | printf("%s ", array_get(what_idx, "name")) |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 60 | break |
| 61 | } |
Dmitry V. Levin | 5999218 | 2015-12-15 00:26:46 +0000 | [diff] [blame] | 62 | returned_size = array_get(what_idx, "byte_size") |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 63 | break |
| 64 | case "enumeration_type": |
Dmitry V. Levin | 5999218 | 2015-12-15 00:26:46 +0000 | [diff] [blame] | 65 | type_idx = array_get(what_idx, "type") |
| 66 | returned_size = array_get(what_idx, "byte_size") |
Dmitry V. Levin | c4afd6d | 2015-12-15 12:58:42 +0000 | [diff] [blame^] | 67 | printf("uint%s_t ", 8 * returned_size) |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 68 | break |
| 69 | case "pointer_type": |
Dmitry V. Levin | c4afd6d | 2015-12-15 12:58:42 +0000 | [diff] [blame^] | 70 | printf("mpers_ptr_t ") |
Dmitry V. Levin | 5999218 | 2015-12-15 00:26:46 +0000 | [diff] [blame] | 71 | returned_size = array_get(what_idx, "byte_size") |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 72 | break |
| 73 | case "array_type": |
Dmitry V. Levin | 5999218 | 2015-12-15 00:26:46 +0000 | [diff] [blame] | 74 | type_idx = array_get(what_idx, "type") |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 75 | what_is(type_idx) |
| 76 | to_return = array[what_idx]["upper_bound"] |
Dmitry V. Levin | 5999218 | 2015-12-15 00:26:46 +0000 | [diff] [blame] | 77 | if ("" == to_return) |
Dmitry V. Levin | c4afd6d | 2015-12-15 12:58:42 +0000 | [diff] [blame^] | 78 | to_return = 0 |
| 79 | returned_size = to_return * returned_size |
Dmitry V. Levin | 54d18a2 | 2015-12-15 03:35:26 +0000 | [diff] [blame] | 80 | return leave(what_idx, to_return) |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 81 | break |
| 82 | case "structure_type": |
Elvira Khabirova | a5ffa18 | 2015-11-26 03:03:24 +0300 | [diff] [blame] | 83 | print "struct {" |
| 84 | prev_location = 0 |
| 85 | location = 0 |
| 86 | returned_size = 0 |
| 87 | prev_returned_size = 0 |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 88 | for (item in array) { |
Elvira Khabirova | a5ffa18 | 2015-11-26 03:03:24 +0300 | [diff] [blame] | 89 | if ("parent" in array[item] && \ |
Dmitry V. Levin | 5999218 | 2015-12-15 00:26:46 +0000 | [diff] [blame] | 90 | array_get(item, "parent") == what_idx) { |
| 91 | location = array_get(item, "location") |
Elvira Khabirova | a5ffa18 | 2015-11-26 03:03:24 +0300 | [diff] [blame] | 92 | loc_diff = location - prev_location - \ |
| 93 | prev_returned_size |
| 94 | if (loc_diff != 0) { |
Dmitry V. Levin | c4afd6d | 2015-12-15 12:58:42 +0000 | [diff] [blame^] | 95 | printf("unsigned char mpers_%s_%s[%s];\n", |
| 96 | "filler", array_seq(item), loc_diff) |
Elvira Khabirova | a5ffa18 | 2015-11-26 03:03:24 +0300 | [diff] [blame] | 97 | } |
| 98 | prev_location = location |
| 99 | returned = what_is(item) |
| 100 | prev_returned_size = returned_size |
| 101 | printf("%s", array[item]["name"]) |
Dmitry V. Levin | c4afd6d | 2015-12-15 12:58:42 +0000 | [diff] [blame^] | 102 | if ("" != returned) { |
Dmitry V. Levin | 5999218 | 2015-12-15 00:26:46 +0000 | [diff] [blame] | 103 | printf("[%s]", returned) |
Elvira Khabirova | a5ffa18 | 2015-11-26 03:03:24 +0300 | [diff] [blame] | 104 | } |
| 105 | print ";" |
| 106 | } |
| 107 | } |
Dmitry V. Levin | 5999218 | 2015-12-15 00:26:46 +0000 | [diff] [blame] | 108 | returned_size = array_get(what_idx, "byte_size") |
Elvira Khabirova | a5ffa18 | 2015-11-26 03:03:24 +0300 | [diff] [blame] | 109 | loc_diff = returned_size - prev_location - prev_returned_size |
| 110 | if (loc_diff != 0) { |
Dmitry V. Levin | c4afd6d | 2015-12-15 12:58:42 +0000 | [diff] [blame^] | 111 | printf("unsigned char mpers_%s_%s[%s];\n", |
| 112 | "end_filler", array_seq(item), loc_diff) |
Elvira Khabirova | a5ffa18 | 2015-11-26 03:03:24 +0300 | [diff] [blame] | 113 | } |
Dmitry V. Levin | c4afd6d | 2015-12-15 12:58:42 +0000 | [diff] [blame^] | 114 | printf("} ATTRIBUTE_PACKED ") |
Elvira Khabirova | a5ffa18 | 2015-11-26 03:03:24 +0300 | [diff] [blame] | 115 | break |
| 116 | case "union_type": |
| 117 | print "union {" |
| 118 | for (item in array) { |
| 119 | if ("parent" in array[item] && \ |
Dmitry V. Levin | 5999218 | 2015-12-15 00:26:46 +0000 | [diff] [blame] | 120 | array_get(item, "parent") == what_idx) { |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 121 | returned = what_is(item) |
Dmitry V. Levin | 5999218 | 2015-12-15 00:26:46 +0000 | [diff] [blame] | 122 | printf("%s", array_get(item, "name")) |
Dmitry V. Levin | c4afd6d | 2015-12-15 12:58:42 +0000 | [diff] [blame^] | 123 | if ("" != returned) { |
Dmitry V. Levin | 5999218 | 2015-12-15 00:26:46 +0000 | [diff] [blame] | 124 | printf("[%s]", returned) |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 125 | } |
| 126 | print ";" |
| 127 | } |
| 128 | } |
Dmitry V. Levin | c4afd6d | 2015-12-15 12:58:42 +0000 | [diff] [blame^] | 129 | printf("} ") |
Dmitry V. Levin | 5999218 | 2015-12-15 00:26:46 +0000 | [diff] [blame] | 130 | returned_size = array_get(what_idx, "byte_size") |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 131 | break |
| 132 | case "typedef": |
Dmitry V. Levin | 5999218 | 2015-12-15 00:26:46 +0000 | [diff] [blame] | 133 | type_idx = array_get(what_idx, "type") |
Dmitry V. Levin | 54d18a2 | 2015-12-15 03:35:26 +0000 | [diff] [blame] | 134 | return leave(what_idx, what_is(type_idx)) |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 135 | break |
| 136 | case "member": |
Dmitry V. Levin | 5999218 | 2015-12-15 00:26:46 +0000 | [diff] [blame] | 137 | type_idx = array_get(what_idx, "type") |
Dmitry V. Levin | 54d18a2 | 2015-12-15 03:35:26 +0000 | [diff] [blame] | 138 | return leave(what_idx, what_is(type_idx)) |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 139 | break |
| 140 | default: |
Dmitry V. Levin | 5999218 | 2015-12-15 00:26:46 +0000 | [diff] [blame] | 141 | type_idx = array_get(what_idx, "type") |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 142 | what_is(type_idx) |
| 143 | break |
| 144 | } |
Dmitry V. Levin | c4afd6d | 2015-12-15 12:58:42 +0000 | [diff] [blame^] | 145 | return leave(what_idx, "") |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 146 | } |
| 147 | BEGIN { |
| 148 | print "#include <inttypes.h>" |
| 149 | } |
| 150 | /^<[[:xdigit:]]+>/ { |
| 151 | match($0, /([[:alnum:]]+)><([[:alnum:]]+)/, matches) |
| 152 | level = matches[1] |
| 153 | idx = "0x" matches[2] |
| 154 | array[idx]["idx"] = idx |
| 155 | parent[level] = idx |
| 156 | if (level > 1) { |
| 157 | array[idx]["parent"] = parent[level-1] |
| 158 | } |
| 159 | } |
| 160 | /^DW_AT_data_member_location/ { |
| 161 | match($0, /[[:digit:]]+/, temparray) |
Elvira Khabirova | a5ffa18 | 2015-11-26 03:03:24 +0300 | [diff] [blame] | 162 | array[idx]["location"] = temparray[0] |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 163 | } |
| 164 | /^DW_AT_name/ { |
Elvira Khabirova | a5ffa18 | 2015-11-26 03:03:24 +0300 | [diff] [blame] | 165 | match($0, /:[[:space:]]+([[:alpha:]_][[:alnum:]_[:space:]]*)/, \ |
| 166 | temparray) |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 167 | array[idx]["name"] = temparray[1] |
| 168 | } |
| 169 | /^DW_AT_byte_size/ { |
| 170 | match($0, /[[:digit:]]+/, temparray) |
| 171 | array[idx]["byte_size"] = temparray[0] |
| 172 | } |
| 173 | /^DW_AT_encoding/ { |
| 174 | match($0, /[[:digit:]]+/, temparray) |
| 175 | array[idx]["encoding"] = temparray[0] |
| 176 | } |
| 177 | /^DW_AT_type/ { |
| 178 | match($0, /:[[:space:]]+<(0x[[:xdigit:]]*)>$/, temparray) |
| 179 | array[idx]["type"] = temparray[1] |
| 180 | } |
| 181 | /^DW_AT_upper_bound/ { |
| 182 | match($0, /[[:digit:]]+/, temparray) |
| 183 | array[parent[level-1]]["upper_bound"] = temparray[0] + 1 |
| 184 | } |
| 185 | /^Abbrev Number:[^(]+\(DW_TAG_/ { |
| 186 | if (match($0, /typedef|union_type|structure_type|pointer_type\ |
| 187 | |enumeration_type|array_type|base_type|member/, temparray)) { |
| 188 | array[idx]["special"] = temparray[0] |
| 189 | } |
| 190 | } |
| 191 | END { |
| 192 | PROCINFO["sorted_in"] = "compare_indices" |
| 193 | for (item in array) { |
| 194 | if (array[item]["special"] == "pointer_type") { |
| 195 | print "typedef uint" \ |
Dmitry V. Levin | 5999218 | 2015-12-15 00:26:46 +0000 | [diff] [blame] | 196 | 8 * array_get(item, "byte_size") "_t mpers_ptr_t;" |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 197 | break |
| 198 | } |
| 199 | } |
| 200 | for (item in array) { |
| 201 | if (array[item]["name"] == VAR_NAME) { |
Dmitry V. Levin | 5999218 | 2015-12-15 00:26:46 +0000 | [diff] [blame] | 202 | type = array_get(item, "type") |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 203 | print "typedef" |
Dmitry V. Levin | 5999218 | 2015-12-15 00:26:46 +0000 | [diff] [blame] | 204 | what_is(type) |
| 205 | name = array_get(type, "name") |
| 206 | print ARCH_FLAG "_" name ";" |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 207 | print "#define MPERS_" \ |
Dmitry V. Levin | 5999218 | 2015-12-15 00:26:46 +0000 | [diff] [blame] | 208 | ARCH_FLAG "_" name " " \ |
| 209 | ARCH_FLAG "_" name |
Elvira Khabirova | 0929422 | 2015-08-04 01:47:02 +0300 | [diff] [blame] | 210 | break |
| 211 | } |
| 212 | } |
| 213 | } |