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