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 ")";
58 # Most lines just get copied from the source as-is, including the start
59 # comment for directives.
65 # Handlers for all of the directives.
68 emission == "opcodes" {
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]);
78 emission == "first-opcodes" {
81 for (i = 0; i <= MAX_OPCODE; i++) {
82 if (isUnused(i) || isOptimized(i)) continue;
83 if (isFirst[i] == "true") {
84 printf(" // Opcodes.%s\n", constName[i]);
92 for (i = 0; i <= MAX_OPCODE; i++) {
93 if (isUnused(i) || isOptimized(i)) continue;
95 nextOp = nextOpcode[i];
96 nextOp = (nextOp == -1) ? "NO_NEXT" : constName[nextOp];
98 printf(" public static final Dop %s =\n" \
99 " new Dop(Opcodes.%s, Opcodes.%s,\n" \
100 " Opcodes.%s, Form%s.THE_ONE, %s);\n\n",
101 constName[i], constName[i], family[i], nextOp, format[i],
106 emission == "opcode-info-defs" {
109 for (i = 0; i <= MAX_OPCODE; i++) {
110 if (isUnused(i) || isOptimized(i)) continue;
112 itype = toupper(indexType[i]);
113 gsub(/-/, "_", itype);
115 printf(" public static final Info %s =\n" \
116 " new Info(Opcodes.%s, \"%s\",\n" \
117 " InstructionCodec.FORMAT_%s, IndexType.%s);\n\n", \
118 constName[i], constName[i], name[i], toupper(format[i]), itype);
122 emission == "dops-init" || emission == "opcode-info-init" {
125 for (i = 0; i <= MAX_OPCODE; i++) {
126 if (isUnused(i) || isOptimized(i)) continue;
127 printf(" set(%s);\n", constName[i]);
131 emission == "libcore-opcodes" {
134 for (i = 0; i <= MAX_OPCODE; i++) {
135 if (isUnused(i) || isOptimized(i)) continue;
136 printf(" int OP_%-28s = 0x%04x;\n", constName[i], i);
140 emission == "libcore-maximum-values" {
143 printf(" MAXIMUM_VALUE = %d;\n", MAX_OPCODE);
144 printf(" MAXIMUM_PACKED_VALUE = %d;\n", MAX_PACKED_OPCODE);
147 emission == "libdex-maximum-values" {
150 printf("#define kMaxOpcodeValue 0x%x\n", MAX_OPCODE);
151 printf("#define kNumPackedOpcodes 0x%x\n", MAX_PACKED_OPCODE + 1);
154 emission == "libdex-opcode-enum" {
157 for (i = 0; i <= MAX_PACKED_OPCODE; i++) {
158 printf(" OP_%-28s = 0x%02x,\n", packedConstName[i], i);
162 emission == "libdex-goto-table" {
165 for (i = 0; i <= MAX_PACKED_OPCODE; i++) {
166 content = sprintf(" H(OP_%s),", packedConstName[i]);
167 printf("%-78s\\\n", content);
171 emission == "libdex-opcode-names" {
174 for (i = 0; i <= MAX_PACKED_OPCODE; i++) {
175 printf(" \"%s\",\n", packedName[i]);
179 emission == "libdex-widths" {
183 for (i = 0; i <= MAX_PACKED_OPCODE; i++) {
184 value = sprintf("%d,", packedWidth[i]);
185 col = colPrint(value, (i == MAX_PACKED_OPCODE), col, 16, 2, " ");
189 emission == "libdex-flags" {
192 for (i = 0; i <= MAX_PACKED_OPCODE; i++) {
193 value = flagsToC(packedFlags[i]);
194 printf(" %s,\n", value);
198 emission == "libdex-formats" {
202 for (i = 0; i <= MAX_PACKED_OPCODE; i++) {
203 value = sprintf("kFmt%s,", packedFormat[i]);
204 col = colPrint(value, (i == MAX_PACKED_OPCODE), col, 7, 9, " ");
208 emission == "libdex-index-types" {
212 for (i = 0; i <= MAX_PACKED_OPCODE; i++) {
213 value = sprintf("%s,", indexTypeValues[packedIndexType[i]]);
214 col = colPrint(value, (i == MAX_PACKED_OPCODE), col, 3, 19, " ");
218 # Handle the end of directive processing (must appear after the directive
221 if (!emissionHandled) {
222 printf("WARNING: unknown tag \"%s\"\n", emission) >"/dev/stderr";
233 # Helper to print out an element in a multi-column fashion. It returns
234 # the (one-based) column number that the next element will be printed
236 function colPrint(value, isLast, col, numCols, colWidth, linePrefix) {
237 isLast = (isLast || (col == numCols));
239 (col == 1) ? linePrefix : " ",
240 isLast ? 1 : colWidth, value,
243 return (col % numCols) + 1;
246 # Read the bytecode description file.
247 function readBytecodes(i, parts, line, cmd, status, count) {
248 # locals: parts, line, cmd, status, count
251 status = getline line <bytecodeFile;
252 if (status == 0) break;
254 print "trouble reading bytecode file";
258 # Clean up the line and extract the command.
259 gsub(/ */, " ", line);
260 sub(/ *#.*$/, "", line);
263 count = split(line, parts);
264 if (count == 0) continue; # Blank or comment line.
266 sub(/^[a-z][a-z]* */, "", line); # Remove the command from line.
269 status = defineOpcode(line);
270 } else if (cmd == "format") {
271 status = defineFormat(line);
277 printf("syntax error on line: %s\n", line) >"/dev/stderr";
286 function defineOpcode(line, count, parts, idx) {
287 # locals: count, parts, idx
288 count = split(line, parts);
289 if (count != 6) return -1;
290 idx = parseHex(parts[1]);
291 if (idx < 0) return -1;
293 # Extract directly specified values from the line.
295 name[idx] = parts[2];
296 format[idx] = parts[3];
297 hasResult[idx] = (parts[4] == "n") ? "false" : "true";
298 indexType[idx] = parts[5];
299 flags[idx] = parts[6];
301 # Calculate derived values.
303 constName[idx] = toupper(name[idx]);
304 gsub("[/-]", "_", constName[idx]); # Dash and slash become underscore.
305 gsub("[+^]", "", constName[idx]); # Plus and caret are removed.
306 split(name[idx], parts, "/");
308 family[idx] = toupper(parts[1]);
309 gsub("-", "_", family[idx]); # Dash becomes underscore.
310 gsub("[+^]", "", family[idx]); # Plus and caret are removed.
312 split(format[idx], parts, ""); # Width is the first format char.
313 width[idx] = parts[1];
315 # This association is used when computing "next" opcodes.
316 familyFormat[family[idx],format[idx]] = idx;
320 if (nextFormat[format[idx]] == "") {
321 printf("unknown format: %s\n", format[idx]) >"/dev/stderr";
325 if (indexTypeValues[indexType[idx]] == "") {
326 printf("unknown index type: %s\n", indexType[idx]) >"/dev/stderr";
330 if (flagsToC(flags[idx]) == "") {
331 printf("bogus flags: %s\n", flags[idx]) >"/dev/stderr";
338 # Define a format family.
339 function defineFormat(line, count, parts, i) {
340 # locals: count, parts, i
341 count = split(line, parts);
342 if (count < 1) return -1;
343 formats[parts[1]] = line;
345 parts[count + 1] = "none";
346 for (i = 1; i <= count; i++) {
347 nextFormat[parts[i]] = parts[i + 1];
353 # Produce the nextOpcode and isFirst arrays. The former indicates, for
354 # each opcode, which one should be tried next when doing instruction
355 # fitting. The latter indicates which opcodes are at the head of an
356 # instruction fitting chain.
357 function deriveOpcodeChains(i, op) {
360 for (i = 0; i <= MAX_OPCODE; i++) {
361 if (isUnused(i)) continue;
365 for (i = 0; i <= MAX_OPCODE; i++) {
366 if (isUnused(i)) continue;
367 op = findNextOpcode(i);
370 isFirst[op] = "false";
375 # Given an opcode by index, find the next opcode in the same family
376 # (that is, with the same base name) to try when matching instructions
377 # to opcodes. This simply walks the nextFormat chain looking for a
378 # match. This returns the index of the matching opcode or -1 if there
380 function findNextOpcode(idx, fam, fmt, result) {
381 # locals: fam, fmt, result
385 # Not every opcode has a version with every possible format, so
386 # we have to iterate down the chain until we find one or run out of
388 for (fmt = nextFormat[format[idx]]; fmt != "none"; fmt = nextFormat[fmt]) {
389 result = familyFormat[fam,fmt];
398 # Construct the tables of info indexed by packed opcode. The packed opcode
399 # values are in the range 0-0x1ff, whereas the unpacked opcodes sparsely
400 # span the range 0-0xffff.
401 function createPackedTables(i, op) {
403 for (i = 0; i <= MAX_PACKED_OPCODE; i++) {
404 op = unpackOpcode(i);
406 packedName[i] = unusedName(op);
407 packedConstName[i] = unusedConstName(op);
408 packedFormat[i] = "00x";
411 packedIndexType[i] = "unknown";
413 packedName[i] = name[op];
414 packedConstName[i] = constName[op];
415 packedFormat[i] = format[op];
416 packedFlags[i] = flags[op];
417 packedWidth[i] = width[op];
418 packedIndexType[i] = indexType[op];
423 # Given a packed opcode, returns the raw (unpacked) opcode value.
424 function unpackOpcode(idx) {
425 # Note: This must be the inverse of the corresponding code in
426 # libdex/DexOpcodes.h.
431 return (idx * 256) + 255;
435 # Returns the "unused" name of the given opcode (by index).
436 # That is, this is the human-oriented name to use for an opcode
437 # definition in cases
438 # where the opcode isn't used.
439 function unusedName(idx) {
441 return sprintf("unused-%02x", idx);
443 return sprintf("unused-%04x", idx);
447 # Returns the "unused" constant name of the given opcode (by index).
448 # That is, this is the name to use for a constant definition in cases
449 # where the opcode isn't used.
450 function unusedConstName(idx) {
452 return toupper(sprintf("UNUSED_%02x", idx));
454 return toupper(sprintf("UNUSED_%04x", idx));
458 # Convert a hex value to an int.
459 function parseHex(hex, result, chars, count, c, i) {
460 # locals: result, chars, count, c, i
462 count = split(hex, chars, "");
464 for (i = 1; i <= count; i++) {
465 c = index("0123456789abcdef", chars[i]);
467 printf("bogus hex value: %s\n", hex) >"/dev/stderr";
470 result = (result * 16) + c - 1;
475 # Initialize the indexTypes data.
476 function initIndexTypes() {
477 indexTypeValues["unknown"] = "kIndexUnknown";
478 indexTypeValues["none"] = "kIndexNone";
479 indexTypeValues["varies"] = "kIndexVaries";
480 indexTypeValues["type-ref"] = "kIndexTypeRef";
481 indexTypeValues["string-ref"] = "kIndexStringRef";
482 indexTypeValues["method-ref"] = "kIndexMethodRef";
483 indexTypeValues["field-ref"] = "kIndexFieldRef";
484 indexTypeValues["inline-method"] = "kIndexInlineMethod";
485 indexTypeValues["vtable-offset"] = "kIndexVtableOffset";
486 indexTypeValues["field-offset"] = "kIndexFieldOffset";
489 # Initialize the flags data.
490 function initFlags() {
491 flagValues["branch"] = "kInstrCanBranch";
492 flagValues["continue"] = "kInstrCanContinue";
493 flagValues["switch"] = "kInstrCanSwitch";
494 flagValues["throw"] = "kInstrCanThrow";
495 flagValues["return"] = "kInstrCanReturn";
496 flagValues["invoke"] = "kInstrInvoke";
497 flagValues["optimized"] = "0"; # Not represented in C output
498 flagValues["0"] = "0";
501 # Translate the given flags into the equivalent C expression. Returns
503 function flagsToC(f, parts, result, i) {
504 # locals: parts, result, i
505 count = split(f, parts, /\|/); # Split input at pipe characters.
508 for (i = 1; i <= count; i++) {
509 f = flagValues[parts[i]];
511 printf("bogus flag: %s\n", f) >"/dev/stderr";
512 return ""; # Bogus flag name.
513 } else if (f == "0") {
514 # Nothing to append for this case.
515 } else if (result == "0") {
518 result = result "|" f;
525 # Returns true if the given opcode (by index) is an "optimized" opcode.
526 function isOptimized(idx, parts, f) {
528 split(flags[idx], parts, /\|/); # Split flags[idx] at pipes.
530 if (parts[f] == "optimized") return 1;
535 # Returns true if there is no definition for the given opcode (by index).
536 function isUnused(idx) {
537 return (name[idx] == "");