| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 1 | # Copyright (C) 2007 The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| Dan Bornstein | 973171b | 2010-12-03 13:01:06 -0800 | [diff] [blame] | 15 | # |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 16 | # Awk helper script for opcode-gen. |
| Dan Bornstein | 973171b | 2010-12-03 13:01:06 -0800 | [diff] [blame] | 17 | # |
| 18 | |
| 19 | # |
| 20 | # Initialization. |
| 21 | # |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 22 | |
| 23 | BEGIN { |
| 24 | MAX_OPCODE = 65535; |
| Dan Bornstein | 3c4dc3b | 2010-12-02 16:05:51 -0800 | [diff] [blame] | 25 | MAX_PACKED_OPCODE = 511; |
| 26 | MAX_PACKED_OPCODE = 255; # TODO: Not for long! |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 27 | initIndexTypes(); |
| 28 | initFlags(); |
| 29 | if (readBytecodes()) exit 1; |
| 30 | deriveOpcodeChains(); |
| Dan Bornstein | 3c4dc3b | 2010-12-02 16:05:51 -0800 | [diff] [blame] | 31 | createPackedTables(); |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 32 | consumeUntil = ""; |
| Dan Bornstein | 973171b | 2010-12-03 13:01:06 -0800 | [diff] [blame] | 33 | emission = ""; |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 34 | } |
| 35 | |
| Dan Bornstein | 973171b | 2010-12-03 13:01:06 -0800 | [diff] [blame] | 36 | # |
| 37 | # General control (must appear above directive handlers). |
| 38 | # |
| 39 | |
| 40 | # Clear out the preexisting output within a directive section. |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 41 | consumeUntil != "" { |
| 42 | if (index($0, consumeUntil) != 0) { |
| 43 | consumeUntil = ""; |
| Dan Bornstein | 973171b | 2010-12-03 13:01:06 -0800 | [diff] [blame] | 44 | print; |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 45 | } |
| Dan Bornstein | 973171b | 2010-12-03 13:01:06 -0800 | [diff] [blame] | 46 | |
| 47 | next; |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 48 | } |
| 49 | |
| Dan Bornstein | 973171b | 2010-12-03 13:01:06 -0800 | [diff] [blame] | 50 | # Detect directives. |
| Dan Bornstein | a277f14 | 2010-12-03 15:11:55 -0800 | [diff] [blame] | 51 | /BEGIN\([a-z-]*\)/ { |
| 52 | i = match($0, /BEGIN\([a-z-]*\)/); |
| Dan Bornstein | 973171b | 2010-12-03 13:01:06 -0800 | [diff] [blame] | 53 | emission = substr($0, i + 6, RLENGTH - 7); |
| 54 | consumeUntil = "END(" emission ")"; |
| 55 | emissionHandled = 0; |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 56 | print; |
| Dan Bornstein | 973171b | 2010-12-03 13:01:06 -0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | # |
| 60 | # Handlers for all of the directives. |
| 61 | # |
| 62 | |
| 63 | emission == "opcodes" { |
| 64 | emissionHandled = 1; |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 65 | |
| 66 | for (i = 0; i <= MAX_OPCODE; i++) { |
| 67 | if (isUnused(i) || isOptimized(i)) continue; |
| 68 | printf(" public static final int %s = 0x%s;\n", |
| 69 | constName[i], hex[i]); |
| 70 | } |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| Dan Bornstein | 973171b | 2010-12-03 13:01:06 -0800 | [diff] [blame] | 73 | emission == "first-opcodes" { |
| 74 | emissionHandled = 1; |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 75 | |
| 76 | for (i = 0; i <= MAX_OPCODE; i++) { |
| 77 | if (isUnused(i) || isOptimized(i)) continue; |
| 78 | if (isFirst[i] == "true") { |
| 79 | printf(" // DalvOps.%s\n", constName[i]); |
| 80 | } |
| 81 | } |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| Dan Bornstein | 973171b | 2010-12-03 13:01:06 -0800 | [diff] [blame] | 84 | emission == "dops" { |
| 85 | emissionHandled = 1; |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 86 | |
| 87 | for (i = 0; i <= MAX_OPCODE; i++) { |
| 88 | if (isUnused(i) || isOptimized(i)) continue; |
| 89 | |
| 90 | nextOp = nextOpcode[i]; |
| 91 | nextOp = (nextOp == -1) ? "NO_NEXT" : constName[nextOp]; |
| 92 | |
| 93 | printf(" public static final Dop %s =\n" \ |
| 94 | " new Dop(DalvOps.%s, DalvOps.%s,\n" \ |
| 95 | " DalvOps.%s, Form%s.THE_ONE, %s,\n" \ |
| 96 | " \"%s\");\n\n", |
| 97 | constName[i], constName[i], family[i], nextOp, format[i], |
| 98 | hasResult[i], name[i]); |
| 99 | } |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| Dan Bornstein | 973171b | 2010-12-03 13:01:06 -0800 | [diff] [blame] | 102 | emission == "dops-init" { |
| 103 | emissionHandled = 1; |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 104 | |
| 105 | for (i = 0; i <= MAX_OPCODE; i++) { |
| 106 | if (isUnused(i) || isOptimized(i)) continue; |
| 107 | printf(" set(%s);\n", constName[i]); |
| 108 | } |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| Dan Bornstein | 973171b | 2010-12-03 13:01:06 -0800 | [diff] [blame] | 111 | emission == "libcore-opcodes" { |
| 112 | emissionHandled = 1; |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 113 | |
| Dan Bornstein | 3c4dc3b | 2010-12-02 16:05:51 -0800 | [diff] [blame] | 114 | for (i = 0; i <= MAX_OPCODE; i++) { |
| 115 | if (isUnused(i) || isOptimized(i)) continue; |
| Dan Bornstein | 21f322d | 2010-12-03 13:16:08 -0800 | [diff] [blame] | 116 | printf(" int OP_%-28s = 0x%04x;\n", constName[i], i); |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 117 | } |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| Dan Bornstein | 973171b | 2010-12-03 13:01:06 -0800 | [diff] [blame] | 120 | emission == "libcore-maximum-values" { |
| 121 | emissionHandled = 1; |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 122 | |
| Dan Bornstein | 3c4dc3b | 2010-12-02 16:05:51 -0800 | [diff] [blame] | 123 | printf(" MAXIMUM_VALUE = %d;\n", MAX_OPCODE); |
| 124 | printf(" MAXIMUM_PACKED_VALUE = %d;\n", MAX_PACKED_OPCODE); |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| Dan Bornstein | ccaab18 | 2010-12-03 15:32:40 -0800 | [diff] [blame] | 127 | emission == "libdex-maximum-values" { |
| 128 | emissionHandled = 1; |
| 129 | |
| Dan Bornstein | 29d5b5d | 2010-12-03 17:48:27 -0800 | [diff] [blame^] | 130 | printf("#define kMaxOpcodeValue 0x%x\n", MAX_OPCODE); |
| 131 | printf("#define kNumPackedOpcodes 0x%x\n", MAX_PACKED_OPCODE + 1); |
| Dan Bornstein | ccaab18 | 2010-12-03 15:32:40 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| Dan Bornstein | 973171b | 2010-12-03 13:01:06 -0800 | [diff] [blame] | 134 | emission == "libdex-opcode-enum" { |
| 135 | emissionHandled = 1; |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 136 | |
| Dan Bornstein | 3c4dc3b | 2010-12-02 16:05:51 -0800 | [diff] [blame] | 137 | for (i = 0; i <= MAX_PACKED_OPCODE; i++) { |
| 138 | printf(" OP_%-28s = 0x%02x,\n", packedConstName[i], i); |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 139 | } |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| Dan Bornstein | 973171b | 2010-12-03 13:01:06 -0800 | [diff] [blame] | 142 | emission == "libdex-goto-table" { |
| 143 | emissionHandled = 1; |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 144 | |
| Dan Bornstein | 3c4dc3b | 2010-12-02 16:05:51 -0800 | [diff] [blame] | 145 | for (i = 0; i <= MAX_PACKED_OPCODE; i++) { |
| 146 | content = sprintf(" H(OP_%s),", packedConstName[i]); |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 147 | printf("%-78s\\\n", content); |
| 148 | } |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| Dan Bornstein | 973171b | 2010-12-03 13:01:06 -0800 | [diff] [blame] | 151 | emission == "libdex-opcode-names" { |
| 152 | emissionHandled = 1; |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 153 | |
| Dan Bornstein | 3c4dc3b | 2010-12-02 16:05:51 -0800 | [diff] [blame] | 154 | for (i = 0; i <= MAX_PACKED_OPCODE; i++) { |
| 155 | printf(" \"%s\",\n", packedName[i]); |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 156 | } |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| Dan Bornstein | 973171b | 2010-12-03 13:01:06 -0800 | [diff] [blame] | 159 | emission == "libdex-widths" { |
| 160 | emissionHandled = 1; |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 161 | |
| 162 | col = 1; |
| Dan Bornstein | 3c4dc3b | 2010-12-02 16:05:51 -0800 | [diff] [blame] | 163 | for (i = 0; i <= MAX_PACKED_OPCODE; i++) { |
| 164 | value = sprintf("%d,", packedWidth[i]); |
| 165 | col = colPrint(value, (i == MAX_PACKED_OPCODE), col, 16, 2, " "); |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 166 | } |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 167 | } |
| 168 | |
| Dan Bornstein | 973171b | 2010-12-03 13:01:06 -0800 | [diff] [blame] | 169 | emission == "libdex-flags" { |
| 170 | emissionHandled = 1; |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 171 | |
| Dan Bornstein | 3c4dc3b | 2010-12-02 16:05:51 -0800 | [diff] [blame] | 172 | for (i = 0; i <= MAX_PACKED_OPCODE; i++) { |
| 173 | value = flagsToC(packedFlags[i]); |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 174 | printf(" %s,\n", value); |
| 175 | } |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 176 | } |
| 177 | |
| Dan Bornstein | 973171b | 2010-12-03 13:01:06 -0800 | [diff] [blame] | 178 | emission == "libdex-formats" { |
| 179 | emissionHandled = 1; |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 180 | |
| 181 | col = 1; |
| Dan Bornstein | 3c4dc3b | 2010-12-02 16:05:51 -0800 | [diff] [blame] | 182 | for (i = 0; i <= MAX_PACKED_OPCODE; i++) { |
| 183 | value = sprintf("kFmt%s,", packedFormat[i]); |
| 184 | col = colPrint(value, (i == MAX_PACKED_OPCODE), col, 7, 9, " "); |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 185 | } |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 186 | } |
| 187 | |
| Dan Bornstein | 973171b | 2010-12-03 13:01:06 -0800 | [diff] [blame] | 188 | emission == "libdex-index-types" { |
| 189 | emissionHandled = 1; |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 190 | |
| 191 | col = 1; |
| Dan Bornstein | 3c4dc3b | 2010-12-02 16:05:51 -0800 | [diff] [blame] | 192 | for (i = 0; i <= MAX_PACKED_OPCODE; i++) { |
| 193 | value = sprintf("%s,", indexTypeValues[packedIndexType[i]]); |
| 194 | col = colPrint(value, (i == MAX_PACKED_OPCODE), col, 3, 19, " "); |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 195 | } |
| Dan Bornstein | 973171b | 2010-12-03 13:01:06 -0800 | [diff] [blame] | 196 | } |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 197 | |
| Dan Bornstein | 973171b | 2010-12-03 13:01:06 -0800 | [diff] [blame] | 198 | # |
| 199 | # General control (must appear after the directives). |
| 200 | # |
| 201 | |
| 202 | # Handle the end of directive processing. |
| 203 | emission != "" { |
| 204 | if (!emissionHandled) { |
| 205 | printf("WARNING: unknown tag \"%s\"\n", emission) >"/dev/stderr"; |
| 206 | consumeUntil = ""; |
| 207 | } |
| 208 | |
| 209 | emission = ""; |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 210 | next; |
| 211 | } |
| 212 | |
| Dan Bornstein | 973171b | 2010-12-03 13:01:06 -0800 | [diff] [blame] | 213 | # Most lines just get copied from the source as-is. |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 214 | { print; } |
| 215 | |
| Dan Bornstein | 973171b | 2010-12-03 13:01:06 -0800 | [diff] [blame] | 216 | # |
| 217 | # Helper functions. |
| 218 | # |
| 219 | |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 220 | # Helper to print out an element in a multi-column fashion. It returns |
| 221 | # the (one-based) column number that the next element will be printed |
| 222 | # in. |
| 223 | function colPrint(value, isLast, col, numCols, colWidth, linePrefix) { |
| 224 | isLast = (isLast || (col == numCols)); |
| 225 | printf("%s%-*s%s", |
| 226 | (col == 1) ? linePrefix : " ", |
| 227 | isLast ? 1 : colWidth, value, |
| 228 | isLast ? "\n" : ""); |
| 229 | |
| 230 | return (col % numCols) + 1; |
| 231 | } |
| 232 | |
| 233 | # Read the bytecode description file. |
| 234 | function readBytecodes(i, parts, line, cmd, status, count) { |
| 235 | # locals: parts, line, cmd, status, count |
| 236 | for (;;) { |
| 237 | # Read a line. |
| 238 | status = getline line <bytecodeFile; |
| 239 | if (status == 0) break; |
| 240 | if (status < 0) { |
| 241 | print "trouble reading bytecode file"; |
| 242 | exit 1; |
| 243 | } |
| 244 | |
| 245 | # Clean up the line and extract the command. |
| 246 | gsub(/ */, " ", line); |
| 247 | sub(/ *#.*$/, "", line); |
| 248 | sub(/ $/, "", line); |
| 249 | sub(/^ /, "", line); |
| 250 | count = split(line, parts); |
| 251 | if (count == 0) continue; # Blank or comment line. |
| 252 | cmd = parts[1]; |
| 253 | sub(/^[a-z][a-z]* */, "", line); # Remove the command from line. |
| 254 | |
| 255 | if (cmd == "op") { |
| 256 | status = defineOpcode(line); |
| 257 | } else if (cmd == "format") { |
| 258 | status = defineFormat(line); |
| 259 | } else { |
| 260 | status = -1; |
| 261 | } |
| 262 | |
| 263 | if (status != 0) { |
| Dan Bornstein | 973171b | 2010-12-03 13:01:06 -0800 | [diff] [blame] | 264 | printf("syntax error on line: %s\n", line) >"/dev/stderr"; |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 265 | return 1; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | # Define an opcode. |
| 273 | function defineOpcode(line, count, parts, idx) { |
| 274 | # locals: count, parts, idx |
| 275 | count = split(line, parts); |
| 276 | if (count != 6) return -1; |
| 277 | idx = parseHex(parts[1]); |
| 278 | if (idx < 0) return -1; |
| 279 | |
| 280 | # Extract directly specified values from the line. |
| 281 | hex[idx] = parts[1]; |
| 282 | name[idx] = parts[2]; |
| 283 | format[idx] = parts[3]; |
| 284 | hasResult[idx] = (parts[4] == "n") ? "false" : "true"; |
| 285 | indexType[idx] = parts[5]; |
| 286 | flags[idx] = parts[6]; |
| 287 | |
| 288 | # Calculate derived values. |
| 289 | |
| 290 | constName[idx] = toupper(name[idx]); |
| 291 | gsub("[---/]", "_", constName[idx]); # Dash and slash become underscore. |
| 292 | gsub("[+^]", "", constName[idx]); # Plus and caret are removed. |
| 293 | split(name[idx], parts, "/"); |
| 294 | |
| 295 | family[idx] = toupper(parts[1]); |
| 296 | gsub("-", "_", family[idx]); # Dash becomes underscore. |
| 297 | gsub("[+^]", "", family[idx]); # Plus and caret are removed. |
| 298 | |
| 299 | split(format[idx], parts, ""); # Width is the first format char. |
| 300 | width[idx] = parts[1]; |
| 301 | |
| 302 | # This association is used when computing "next" opcodes. |
| 303 | familyFormat[family[idx],format[idx]] = idx; |
| 304 | |
| 305 | # Verify values. |
| 306 | |
| 307 | if (nextFormat[format[idx]] == "") { |
| Dan Bornstein | 973171b | 2010-12-03 13:01:06 -0800 | [diff] [blame] | 308 | printf("unknown format: %s\n", format[idx]) >"/dev/stderr"; |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 309 | return 1; |
| 310 | } |
| 311 | |
| 312 | if (indexTypeValues[indexType[idx]] == "") { |
| Dan Bornstein | 973171b | 2010-12-03 13:01:06 -0800 | [diff] [blame] | 313 | printf("unknown index type: %s\n", indexType[idx]) >"/dev/stderr"; |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 314 | return 1; |
| 315 | } |
| 316 | |
| 317 | if (flagsToC(flags[idx]) == "") { |
| Dan Bornstein | 973171b | 2010-12-03 13:01:06 -0800 | [diff] [blame] | 318 | printf("bogus flags: %s\n", flags[idx]) >"/dev/stderr"; |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 319 | return 1; |
| 320 | } |
| 321 | |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | # Define a format family. |
| 326 | function defineFormat(line, count, parts, i) { |
| 327 | # locals: count, parts, i |
| 328 | count = split(line, parts); |
| 329 | if (count < 1) return -1; |
| 330 | formats[parts[1]] = line; |
| 331 | |
| 332 | parts[count + 1] = "none"; |
| 333 | for (i = 1; i <= count; i++) { |
| 334 | nextFormat[parts[i]] = parts[i + 1]; |
| 335 | } |
| 336 | |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | # Produce the nextOpcode and isFirst arrays. The former indicates, for |
| 341 | # each opcode, which one should be tried next when doing instruction |
| 342 | # fitting. The latter indicates which opcodes are at the head of an |
| 343 | # instruction fitting chain. |
| 344 | function deriveOpcodeChains(i, op) { |
| 345 | # locals: i, op |
| 346 | |
| 347 | for (i = 0; i <= MAX_OPCODE; i++) { |
| 348 | if (isUnused(i)) continue; |
| 349 | isFirst[i] = "true"; |
| 350 | } |
| 351 | |
| 352 | for (i = 0; i <= MAX_OPCODE; i++) { |
| 353 | if (isUnused(i)) continue; |
| 354 | op = findNextOpcode(i); |
| 355 | nextOpcode[i] = op; |
| 356 | if (op != -1) { |
| 357 | isFirst[op] = "false"; |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | # Given an opcode by index, find the next opcode in the same family |
| 363 | # (that is, with the same base name) to try when matching instructions |
| 364 | # to opcodes. This simply walks the nextFormat chain looking for a |
| 365 | # match. This returns the index of the matching opcode or -1 if there |
| 366 | # is none. |
| 367 | function findNextOpcode(idx, fam, fmt, result) { |
| 368 | # locals: fam, fmt, result |
| 369 | fam = family[idx]; |
| 370 | fmt = format[idx]; |
| 371 | |
| 372 | # Not every opcode has a version with every possible format, so |
| 373 | # we have to iterate down the chain until we find one or run out of |
| 374 | # formats to try. |
| 375 | for (fmt = nextFormat[format[idx]]; fmt != "none"; fmt = nextFormat[fmt]) { |
| 376 | result = familyFormat[fam,fmt]; |
| 377 | if (result != "") { |
| 378 | return result; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | return -1; |
| 383 | } |
| 384 | |
| Dan Bornstein | 3c4dc3b | 2010-12-02 16:05:51 -0800 | [diff] [blame] | 385 | # Construct the tables of info indexed by packed opcode. The packed opcode |
| 386 | # values are in the range 0-0x1ff, whereas the unpacked opcodes sparsely |
| 387 | # span the range 0-0xffff. |
| 388 | function createPackedTables(i, op) { |
| 389 | # locals: i, op |
| 390 | for (i = 0; i <= MAX_PACKED_OPCODE; i++) { |
| 391 | op = unpackOpcode(i); |
| 392 | if (i == 255) { |
| 393 | # Special case: This is the low-opcode slot for a would-be |
| 394 | # extended opcode dispatch implementation. |
| 395 | packedName[i] = "dispatch-ff"; |
| 396 | packedConstName[i] = "DISPATCH_FF"; |
| 397 | packedFormat[i] = "00x"; |
| 398 | packedFlags[i] = 0; |
| 399 | packedWidth[i] = 0; |
| 400 | packedIndexType[i] = "unknown"; |
| 401 | } else if (isUnused(op)) { |
| 402 | packedName[i] = unusedName(op); |
| 403 | packedConstName[i] = unusedConstName(op); |
| 404 | packedFormat[i] = "00x"; |
| 405 | packedFlags[i] = 0; |
| 406 | packedWidth[i] = 0; |
| 407 | packedIndexType[i] = "unknown"; |
| 408 | } else { |
| 409 | packedName[i] = name[op]; |
| 410 | packedConstName[i] = constName[op]; |
| 411 | packedFormat[i] = format[op]; |
| 412 | packedFlags[i] = flags[op]; |
| 413 | packedWidth[i] = width[op]; |
| 414 | packedIndexType[i] = indexType[op]; |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | # Given a packed opcode, returns the raw (unpacked) opcode value. |
| 420 | function unpackOpcode(idx) { |
| 421 | # Note: This must be the inverse of the corresponding code in |
| 422 | # libdex/DexOpcodes.h. |
| 423 | if (idx <= 255) { |
| 424 | return idx; |
| 425 | } else { |
| Dan Bornstein | 21f322d | 2010-12-03 13:16:08 -0800 | [diff] [blame] | 426 | idx -= 256; |
| Dan Bornstein | 3c4dc3b | 2010-12-02 16:05:51 -0800 | [diff] [blame] | 427 | return (idx * 256) + 255; |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | # Returns the "unused" name of the given opcode (by index). |
| 432 | # That is, this is the human-oriented name to use for an opcode |
| 433 | # definition in cases |
| 434 | # where the opcode isn't used. |
| 435 | function unusedName(idx) { |
| 436 | if (idx <= 255) { |
| 437 | return sprintf("unused-%02x", idx); |
| 438 | } else { |
| 439 | return sprintf("unused-%04x", idx); |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | # Returns the "unused" constant name of the given opcode (by index). |
| 444 | # That is, this is the name to use for a constant definition in cases |
| 445 | # where the opcode isn't used. |
| 446 | function unusedConstName(idx) { |
| 447 | if (idx <= 255) { |
| 448 | return toupper(sprintf("UNUSED_%02x", idx)); |
| 449 | } else { |
| 450 | return toupper(sprintf("UNUSED_%04x", idx)); |
| 451 | } |
| 452 | } |
| 453 | |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 454 | # Convert a hex value to an int. |
| 455 | function parseHex(hex, result, chars, count, c, i) { |
| 456 | # locals: result, chars, count, c, i |
| 457 | hex = tolower(hex); |
| 458 | count = split(hex, chars, ""); |
| 459 | result = 0; |
| 460 | for (i = 1; i <= count; i++) { |
| 461 | c = index("0123456789abcdef", chars[i]); |
| 462 | if (c == 0) { |
| Dan Bornstein | 973171b | 2010-12-03 13:01:06 -0800 | [diff] [blame] | 463 | printf("bogus hex value: %s\n", hex) >"/dev/stderr"; |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 464 | return -1; |
| 465 | } |
| 466 | result = (result * 16) + c - 1; |
| 467 | } |
| 468 | return result; |
| 469 | } |
| 470 | |
| 471 | # Initialize the indexTypes data. |
| 472 | function initIndexTypes() { |
| 473 | indexTypeValues["unknown"] = "kIndexUnknown"; |
| 474 | indexTypeValues["none"] = "kIndexNone"; |
| 475 | indexTypeValues["varies"] = "kIndexVaries"; |
| 476 | indexTypeValues["type-ref"] = "kIndexTypeRef"; |
| 477 | indexTypeValues["string-ref"] = "kIndexStringRef"; |
| 478 | indexTypeValues["method-ref"] = "kIndexMethodRef"; |
| 479 | indexTypeValues["field-ref"] = "kIndexFieldRef"; |
| 480 | indexTypeValues["inline-method"] = "kIndexInlineMethod"; |
| 481 | indexTypeValues["vtable-offset"] = "kIndexVtableOffset"; |
| 482 | indexTypeValues["field-offset"] = "kIndexFieldOffset"; |
| 483 | } |
| 484 | |
| 485 | # Initialize the flags data. |
| 486 | function initFlags() { |
| 487 | flagValues["branch"] = "kInstrCanBranch"; |
| 488 | flagValues["continue"] = "kInstrCanContinue"; |
| 489 | flagValues["switch"] = "kInstrCanSwitch"; |
| 490 | flagValues["throw"] = "kInstrCanThrow"; |
| 491 | flagValues["return"] = "kInstrCanReturn"; |
| 492 | flagValues["invoke"] = "kInstrInvoke"; |
| 493 | flagValues["optimized"] = "0"; # Not represented in C output |
| 494 | flagValues["0"] = "0"; |
| 495 | } |
| 496 | |
| 497 | # Translate the given flags into the equivalent C expression. Returns |
| 498 | # "" on error. |
| 499 | function flagsToC(f, parts, result, i) { |
| 500 | # locals: parts, result, i |
| 501 | count = split(f, parts, /\|/); # Split input at pipe characters. |
| 502 | result = "0"; |
| 503 | |
| 504 | for (i = 1; i <= count; i++) { |
| 505 | f = flagValues[parts[i]]; |
| 506 | if (f == "") { |
| Dan Bornstein | 973171b | 2010-12-03 13:01:06 -0800 | [diff] [blame] | 507 | printf("bogus flag: %s\n", f) >"/dev/stderr"; |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 508 | return ""; # Bogus flag name. |
| 509 | } else if (f == "0") { |
| 510 | # Nothing to append for this case. |
| 511 | } else if (result == "0") { |
| 512 | result = f; |
| 513 | } else { |
| 514 | result = result "|" f; |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | return result; |
| 519 | } |
| 520 | |
| Dan Bornstein | d12de17 | 2010-12-02 15:21:59 -0800 | [diff] [blame] | 521 | # Returns true if the given opcode (by index) is an "optimized" opcode. |
| 522 | function isOptimized(idx, parts, f) { |
| 523 | # locals: parts, f |
| 524 | split(flags[idx], parts, /\|/); # Split flags[idx] at pipes. |
| 525 | for (f in parts) { |
| 526 | if (parts[f] == "optimized") return 1; |
| 527 | } |
| 528 | return 0; |
| 529 | } |
| 530 | |
| 531 | # Returns true if there is no definition for the given opcode (by index). |
| 532 | function isUnused(idx) { |
| 533 | return (name[idx] == ""); |
| 534 | } |