1 # Copyright (C) 2007 The Android Open Source Project
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
16 # Awk helper script for opcode-gen.
25 MAX_PACKED_OPCODE = 511;
26 MAX_PACKED_OPCODE = 255; # TODO: Not for long!
29 if (readBytecodes()) exit 1;
37 # General control (must appear above directive handlers).
40 # Clear out the preexisting output within a directive section.
42 if (index($0, consumeUntil) != 0) {
52 i = match($0, /BEGIN\([a-z-]*\)/);
53 emission = substr($0, i + 6, RLENGTH - 7);
54 consumeUntil = "END(" emission ")";
60 # Handlers for all of the directives.
63 emission == "opcodes" {
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]);
73 emission == "first-opcodes" {
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]);
87 for (i = 0; i <= MAX_OPCODE; i++) {
88 if (isUnused(i) || isOptimized(i)) continue;
90 nextOp = nextOpcode[i];
91 nextOp = (nextOp == -1) ? "NO_NEXT" : constName[nextOp];
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" \
97 constName[i], constName[i], family[i], nextOp, format[i],
98 hasResult[i], name[i]);
102 emission == "dops-init" {
105 for (i = 0; i <= MAX_OPCODE; i++) {
106 if (isUnused(i) || isOptimized(i)) continue;
107 printf(" set(%s);\n", constName[i]);
111 emission == "libcore-opcodes" {
114 for (i = 0; i <= MAX_OPCODE; i++) {
115 if (isUnused(i) || isOptimized(i)) continue;
116 printf(" int OP_%-28s = 0x%04x;\n", constName[i], i);
120 emission == "libcore-maximum-values" {
123 printf(" MAXIMUM_VALUE = %d;\n", MAX_OPCODE);
124 printf(" MAXIMUM_PACKED_VALUE = %d;\n", MAX_PACKED_OPCODE);
127 emission == "libdex-maximum-values" {
130 printf("#define kMaxOpcodeValue 0x%x\n", MAX_OPCODE);
131 printf("#define kNumPackedOpcodes 0x%x\n", MAX_PACKED_OPCODE + 1);
134 emission == "libdex-opcode-enum" {
137 for (i = 0; i <= MAX_PACKED_OPCODE; i++) {
138 printf(" OP_%-28s = 0x%02x,\n", packedConstName[i], i);
142 emission == "libdex-goto-table" {
145 for (i = 0; i <= MAX_PACKED_OPCODE; i++) {
146 content = sprintf(" H(OP_%s),", packedConstName[i]);
147 printf("%-78s\\\n", content);
151 emission == "libdex-opcode-names" {
154 for (i = 0; i <= MAX_PACKED_OPCODE; i++) {
155 printf(" \"%s\",\n", packedName[i]);
159 emission == "libdex-widths" {
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, " ");
169 emission == "libdex-flags" {
172 for (i = 0; i <= MAX_PACKED_OPCODE; i++) {
173 value = flagsToC(packedFlags[i]);
174 printf(" %s,\n", value);
178 emission == "libdex-formats" {
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, " ");
188 emission == "libdex-index-types" {
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, " ");
199 # General control (must appear after the directives).
202 # Handle the end of directive processing.
204 if (!emissionHandled) {
205 printf("WARNING: unknown tag \"%s\"\n", emission) >"/dev/stderr";
213 # Most lines just get copied from the source as-is.
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
223 function colPrint(value, isLast, col, numCols, colWidth, linePrefix) {
224 isLast = (isLast || (col == numCols));
226 (col == 1) ? linePrefix : " ",
227 isLast ? 1 : colWidth, value,
230 return (col % numCols) + 1;
233 # Read the bytecode description file.
234 function readBytecodes(i, parts, line, cmd, status, count) {
235 # locals: parts, line, cmd, status, count
238 status = getline line <bytecodeFile;
239 if (status == 0) break;
241 print "trouble reading bytecode file";
245 # Clean up the line and extract the command.
246 gsub(/ */, " ", line);
247 sub(/ *#.*$/, "", line);
250 count = split(line, parts);
251 if (count == 0) continue; # Blank or comment line.
253 sub(/^[a-z][a-z]* */, "", line); # Remove the command from line.
256 status = defineOpcode(line);
257 } else if (cmd == "format") {
258 status = defineFormat(line);
264 printf("syntax error on line: %s\n", line) >"/dev/stderr";
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;
280 # Extract directly specified values from the line.
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];
288 # Calculate derived values.
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, "/");
295 family[idx] = toupper(parts[1]);
296 gsub("-", "_", family[idx]); # Dash becomes underscore.
297 gsub("[+^]", "", family[idx]); # Plus and caret are removed.
299 split(format[idx], parts, ""); # Width is the first format char.
300 width[idx] = parts[1];
302 # This association is used when computing "next" opcodes.
303 familyFormat[family[idx],format[idx]] = idx;
307 if (nextFormat[format[idx]] == "") {
308 printf("unknown format: %s\n", format[idx]) >"/dev/stderr";
312 if (indexTypeValues[indexType[idx]] == "") {
313 printf("unknown index type: %s\n", indexType[idx]) >"/dev/stderr";
317 if (flagsToC(flags[idx]) == "") {
318 printf("bogus flags: %s\n", flags[idx]) >"/dev/stderr";
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;
332 parts[count + 1] = "none";
333 for (i = 1; i <= count; i++) {
334 nextFormat[parts[i]] = parts[i + 1];
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) {
347 for (i = 0; i <= MAX_OPCODE; i++) {
348 if (isUnused(i)) continue;
352 for (i = 0; i <= MAX_OPCODE; i++) {
353 if (isUnused(i)) continue;
354 op = findNextOpcode(i);
357 isFirst[op] = "false";
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
367 function findNextOpcode(idx, fam, fmt, result) {
368 # locals: fam, fmt, result
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
375 for (fmt = nextFormat[format[idx]]; fmt != "none"; fmt = nextFormat[fmt]) {
376 result = familyFormat[fam,fmt];
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) {
390 for (i = 0; i <= MAX_PACKED_OPCODE; i++) {
391 op = unpackOpcode(i);
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";
400 packedIndexType[i] = "unknown";
401 } else if (isUnused(op)) {
402 packedName[i] = unusedName(op);
403 packedConstName[i] = unusedConstName(op);
404 packedFormat[i] = "00x";
407 packedIndexType[i] = "unknown";
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];
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.
427 return (idx * 256) + 255;
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) {
437 return sprintf("unused-%02x", idx);
439 return sprintf("unused-%04x", idx);
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) {
448 return toupper(sprintf("UNUSED_%02x", idx));
450 return toupper(sprintf("UNUSED_%04x", idx));
454 # Convert a hex value to an int.
455 function parseHex(hex, result, chars, count, c, i) {
456 # locals: result, chars, count, c, i
458 count = split(hex, chars, "");
460 for (i = 1; i <= count; i++) {
461 c = index("0123456789abcdef", chars[i]);
463 printf("bogus hex value: %s\n", hex) >"/dev/stderr";
466 result = (result * 16) + c - 1;
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";
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";
497 # Translate the given flags into the equivalent C expression. Returns
499 function flagsToC(f, parts, result, i) {
500 # locals: parts, result, i
501 count = split(f, parts, /\|/); # Split input at pipe characters.
504 for (i = 1; i <= count; i++) {
505 f = flagValues[parts[i]];
507 printf("bogus flag: %s\n", f) >"/dev/stderr";
508 return ""; # Bogus flag name.
509 } else if (f == "0") {
510 # Nothing to append for this case.
511 } else if (result == "0") {
514 result = result "|" f;
521 # Returns true if the given opcode (by index) is an "optimized" opcode.
522 function isOptimized(idx, parts, f) {
524 split(flags[idx], parts, /\|/); # Split flags[idx] at pipes.
526 if (parts[f] == "optimized") return 1;
531 # Returns true if there is no definition for the given opcode (by index).
532 function isUnused(idx) {
533 return (name[idx] == "");