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