blob: 934413fd6bbc07bb861764f4fc42997346a28693 [file] [log] [blame]
Dan Bornsteind12de172010-12-02 15:21:59 -08001# 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 Bornstein973171b2010-12-03 13:01:06 -080015#
Dan Bornsteind12de172010-12-02 15:21:59 -080016# Awk helper script for opcode-gen.
Dan Bornstein973171b2010-12-03 13:01:06 -080017#
18
19#
20# Initialization.
21#
Dan Bornsteind12de172010-12-02 15:21:59 -080022
23BEGIN {
24 MAX_OPCODE = 65535;
Dan Bornstein3c4dc3b2010-12-02 16:05:51 -080025 MAX_PACKED_OPCODE = 511;
26 MAX_PACKED_OPCODE = 255; # TODO: Not for long!
Dan Bornsteind12de172010-12-02 15:21:59 -080027 initIndexTypes();
28 initFlags();
29 if (readBytecodes()) exit 1;
30 deriveOpcodeChains();
Dan Bornstein3c4dc3b2010-12-02 16:05:51 -080031 createPackedTables();
Dan Bornsteind12de172010-12-02 15:21:59 -080032 consumeUntil = "";
Dan Bornstein973171b2010-12-03 13:01:06 -080033 emission = "";
Dan Bornsteind12de172010-12-02 15:21:59 -080034}
35
Dan Bornstein973171b2010-12-03 13:01:06 -080036#
37# General control (must appear above directive handlers).
38#
39
40# Clear out the preexisting output within a directive section.
Dan Bornsteind12de172010-12-02 15:21:59 -080041consumeUntil != "" {
42 if (index($0, consumeUntil) != 0) {
43 consumeUntil = "";
Dan Bornstein973171b2010-12-03 13:01:06 -080044 print;
Dan Bornsteind12de172010-12-02 15:21:59 -080045 }
Dan Bornstein973171b2010-12-03 13:01:06 -080046
47 next;
Dan Bornsteind12de172010-12-02 15:21:59 -080048}
49
Dan Bornstein973171b2010-12-03 13:01:06 -080050# Detect directives.
Dan Bornsteina277f142010-12-03 15:11:55 -080051/BEGIN\([a-z-]*\)/ {
52 i = match($0, /BEGIN\([a-z-]*\)/);
Dan Bornstein973171b2010-12-03 13:01:06 -080053 emission = substr($0, i + 6, RLENGTH - 7);
54 consumeUntil = "END(" emission ")";
55 emissionHandled = 0;
Dan Bornstein9dc57d32010-12-03 17:58:01 -080056}
57
58# Most lines just get copied from the source as-is, including the start
59# comment for directives.
60{
Dan Bornsteind12de172010-12-02 15:21:59 -080061 print;
Dan Bornstein973171b2010-12-03 13:01:06 -080062}
63
64#
65# Handlers for all of the directives.
66#
67
68emission == "opcodes" {
69 emissionHandled = 1;
Dan Bornsteind12de172010-12-02 15:21:59 -080070
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 Bornsteind12de172010-12-02 15:21:59 -080076}
77
Dan Bornstein973171b2010-12-03 13:01:06 -080078emission == "first-opcodes" {
79 emissionHandled = 1;
Dan Bornsteind12de172010-12-02 15:21:59 -080080
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 Bornsteind12de172010-12-02 15:21:59 -080087}
88
Dan Bornstein973171b2010-12-03 13:01:06 -080089emission == "dops" {
90 emissionHandled = 1;
Dan Bornsteind12de172010-12-02 15:21:59 -080091
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 Bornsteind12de172010-12-02 15:21:59 -0800105}
106
Dan Bornstein973171b2010-12-03 13:01:06 -0800107emission == "dops-init" {
108 emissionHandled = 1;
Dan Bornsteind12de172010-12-02 15:21:59 -0800109
110 for (i = 0; i <= MAX_OPCODE; i++) {
111 if (isUnused(i) || isOptimized(i)) continue;
112 printf(" set(%s);\n", constName[i]);
113 }
Dan Bornsteind12de172010-12-02 15:21:59 -0800114}
115
Dan Bornstein973171b2010-12-03 13:01:06 -0800116emission == "libcore-opcodes" {
117 emissionHandled = 1;
Dan Bornsteind12de172010-12-02 15:21:59 -0800118
Dan Bornstein3c4dc3b2010-12-02 16:05:51 -0800119 for (i = 0; i <= MAX_OPCODE; i++) {
120 if (isUnused(i) || isOptimized(i)) continue;
Dan Bornstein21f322d2010-12-03 13:16:08 -0800121 printf(" int OP_%-28s = 0x%04x;\n", constName[i], i);
Dan Bornsteind12de172010-12-02 15:21:59 -0800122 }
Dan Bornsteind12de172010-12-02 15:21:59 -0800123}
124
Dan Bornstein973171b2010-12-03 13:01:06 -0800125emission == "libcore-maximum-values" {
126 emissionHandled = 1;
Dan Bornsteind12de172010-12-02 15:21:59 -0800127
Dan Bornstein3c4dc3b2010-12-02 16:05:51 -0800128 printf(" MAXIMUM_VALUE = %d;\n", MAX_OPCODE);
129 printf(" MAXIMUM_PACKED_VALUE = %d;\n", MAX_PACKED_OPCODE);
Dan Bornsteind12de172010-12-02 15:21:59 -0800130}
131
Dan Bornsteinccaab182010-12-03 15:32:40 -0800132emission == "libdex-maximum-values" {
133 emissionHandled = 1;
134
Dan Bornstein29d5b5d2010-12-03 17:48:27 -0800135 printf("#define kMaxOpcodeValue 0x%x\n", MAX_OPCODE);
136 printf("#define kNumPackedOpcodes 0x%x\n", MAX_PACKED_OPCODE + 1);
Dan Bornsteinccaab182010-12-03 15:32:40 -0800137}
138
Dan Bornstein973171b2010-12-03 13:01:06 -0800139emission == "libdex-opcode-enum" {
140 emissionHandled = 1;
Dan Bornsteind12de172010-12-02 15:21:59 -0800141
Dan Bornstein3c4dc3b2010-12-02 16:05:51 -0800142 for (i = 0; i <= MAX_PACKED_OPCODE; i++) {
143 printf(" OP_%-28s = 0x%02x,\n", packedConstName[i], i);
Dan Bornsteind12de172010-12-02 15:21:59 -0800144 }
Dan Bornsteind12de172010-12-02 15:21:59 -0800145}
146
Dan Bornstein973171b2010-12-03 13:01:06 -0800147emission == "libdex-goto-table" {
148 emissionHandled = 1;
Dan Bornsteind12de172010-12-02 15:21:59 -0800149
Dan Bornstein3c4dc3b2010-12-02 16:05:51 -0800150 for (i = 0; i <= MAX_PACKED_OPCODE; i++) {
151 content = sprintf(" H(OP_%s),", packedConstName[i]);
Dan Bornsteind12de172010-12-02 15:21:59 -0800152 printf("%-78s\\\n", content);
153 }
Dan Bornsteind12de172010-12-02 15:21:59 -0800154}
155
Dan Bornstein973171b2010-12-03 13:01:06 -0800156emission == "libdex-opcode-names" {
157 emissionHandled = 1;
Dan Bornsteind12de172010-12-02 15:21:59 -0800158
Dan Bornstein3c4dc3b2010-12-02 16:05:51 -0800159 for (i = 0; i <= MAX_PACKED_OPCODE; i++) {
160 printf(" \"%s\",\n", packedName[i]);
Dan Bornsteind12de172010-12-02 15:21:59 -0800161 }
Dan Bornsteind12de172010-12-02 15:21:59 -0800162}
163
Dan Bornstein973171b2010-12-03 13:01:06 -0800164emission == "libdex-widths" {
165 emissionHandled = 1;
Dan Bornsteind12de172010-12-02 15:21:59 -0800166
167 col = 1;
Dan Bornstein3c4dc3b2010-12-02 16:05:51 -0800168 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 Bornsteind12de172010-12-02 15:21:59 -0800171 }
Dan Bornsteind12de172010-12-02 15:21:59 -0800172}
173
Dan Bornstein973171b2010-12-03 13:01:06 -0800174emission == "libdex-flags" {
175 emissionHandled = 1;
Dan Bornsteind12de172010-12-02 15:21:59 -0800176
Dan Bornstein3c4dc3b2010-12-02 16:05:51 -0800177 for (i = 0; i <= MAX_PACKED_OPCODE; i++) {
178 value = flagsToC(packedFlags[i]);
Dan Bornsteind12de172010-12-02 15:21:59 -0800179 printf(" %s,\n", value);
180 }
Dan Bornsteind12de172010-12-02 15:21:59 -0800181}
182
Dan Bornstein973171b2010-12-03 13:01:06 -0800183emission == "libdex-formats" {
184 emissionHandled = 1;
Dan Bornsteind12de172010-12-02 15:21:59 -0800185
186 col = 1;
Dan Bornstein3c4dc3b2010-12-02 16:05:51 -0800187 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 Bornsteind12de172010-12-02 15:21:59 -0800190 }
Dan Bornsteind12de172010-12-02 15:21:59 -0800191}
192
Dan Bornstein973171b2010-12-03 13:01:06 -0800193emission == "libdex-index-types" {
194 emissionHandled = 1;
Dan Bornsteind12de172010-12-02 15:21:59 -0800195
196 col = 1;
Dan Bornstein3c4dc3b2010-12-02 16:05:51 -0800197 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 Bornsteind12de172010-12-02 15:21:59 -0800200 }
Dan Bornstein973171b2010-12-03 13:01:06 -0800201}
Dan Bornsteind12de172010-12-02 15:21:59 -0800202
Dan Bornstein9dc57d32010-12-03 17:58:01 -0800203# Handle the end of directive processing (must appear after the directive
204# clauses).
Dan Bornstein973171b2010-12-03 13:01:06 -0800205emission != "" {
206 if (!emissionHandled) {
207 printf("WARNING: unknown tag \"%s\"\n", emission) >"/dev/stderr";
208 consumeUntil = "";
209 }
210
211 emission = "";
Dan Bornsteind12de172010-12-02 15:21:59 -0800212}
213
Dan Bornstein973171b2010-12-03 13:01:06 -0800214#
215# Helper functions.
216#
217
Dan Bornsteind12de172010-12-02 15:21:59 -0800218# 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.
221function 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.
232function 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 Bornstein973171b2010-12-03 13:01:06 -0800262 printf("syntax error on line: %s\n", line) >"/dev/stderr";
Dan Bornsteind12de172010-12-02 15:21:59 -0800263 return 1;
264 }
265 }
266
267 return 0;
268}
269
270# Define an opcode.
271function 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 Bornstein9dc57d32010-12-03 17:58:01 -0800289 gsub("[/-]", "_", constName[idx]); # Dash and slash become underscore.
Dan Bornsteind12de172010-12-02 15:21:59 -0800290 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 Bornstein973171b2010-12-03 13:01:06 -0800306 printf("unknown format: %s\n", format[idx]) >"/dev/stderr";
Dan Bornsteind12de172010-12-02 15:21:59 -0800307 return 1;
308 }
309
310 if (indexTypeValues[indexType[idx]] == "") {
Dan Bornstein973171b2010-12-03 13:01:06 -0800311 printf("unknown index type: %s\n", indexType[idx]) >"/dev/stderr";
Dan Bornsteind12de172010-12-02 15:21:59 -0800312 return 1;
313 }
314
315 if (flagsToC(flags[idx]) == "") {
Dan Bornstein973171b2010-12-03 13:01:06 -0800316 printf("bogus flags: %s\n", flags[idx]) >"/dev/stderr";
Dan Bornsteind12de172010-12-02 15:21:59 -0800317 return 1;
318 }
319
320 return 0;
321}
322
323# Define a format family.
324function 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.
342function 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.
365function 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 Bornstein3c4dc3b2010-12-02 16:05:51 -0800383# 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.
386function 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.
418function 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 Bornstein21f322d2010-12-03 13:16:08 -0800424 idx -= 256;
Dan Bornstein3c4dc3b2010-12-02 16:05:51 -0800425 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.
433function 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.
444function 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 Bornsteind12de172010-12-02 15:21:59 -0800452# Convert a hex value to an int.
453function 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 Bornstein973171b2010-12-03 13:01:06 -0800461 printf("bogus hex value: %s\n", hex) >"/dev/stderr";
Dan Bornsteind12de172010-12-02 15:21:59 -0800462 return -1;
463 }
464 result = (result * 16) + c - 1;
465 }
466 return result;
467}
468
469# Initialize the indexTypes data.
470function 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.
484function 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.
497function 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 Bornstein973171b2010-12-03 13:01:06 -0800505 printf("bogus flag: %s\n", f) >"/dev/stderr";
Dan Bornsteind12de172010-12-02 15:21:59 -0800506 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 Bornsteind12de172010-12-02 15:21:59 -0800519# Returns true if the given opcode (by index) is an "optimized" opcode.
520function 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).
530function isUnused(idx) {
531 return (name[idx] == "");
532}