blob: 82e4fcf367c0dfc3e320cf657295bb4d0d50a50d [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 Bornsteind12de172010-12-02 15:21:59 -080056 print;
Dan Bornstein973171b2010-12-03 13:01:06 -080057}
58
59#
60# Handlers for all of the directives.
61#
62
63emission == "opcodes" {
64 emissionHandled = 1;
Dan Bornsteind12de172010-12-02 15:21:59 -080065
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]);
70 }
Dan Bornsteind12de172010-12-02 15:21:59 -080071}
72
Dan Bornstein973171b2010-12-03 13:01:06 -080073emission == "first-opcodes" {
74 emissionHandled = 1;
Dan Bornsteind12de172010-12-02 15:21:59 -080075
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]);
80 }
81 }
Dan Bornsteind12de172010-12-02 15:21:59 -080082}
83
Dan Bornstein973171b2010-12-03 13:01:06 -080084emission == "dops" {
85 emissionHandled = 1;
Dan Bornsteind12de172010-12-02 15:21:59 -080086
87 for (i = 0; i <= MAX_OPCODE; i++) {
88 if (isUnused(i) || isOptimized(i)) continue;
89
90 nextOp = nextOpcode[i];
91 nextOp = (nextOp == -1) ? "NO_NEXT" : constName[nextOp];
92
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" \
96 " \"%s\");\n\n",
97 constName[i], constName[i], family[i], nextOp, format[i],
98 hasResult[i], name[i]);
99 }
Dan Bornsteind12de172010-12-02 15:21:59 -0800100}
101
Dan Bornstein973171b2010-12-03 13:01:06 -0800102emission == "dops-init" {
103 emissionHandled = 1;
Dan Bornsteind12de172010-12-02 15:21:59 -0800104
105 for (i = 0; i <= MAX_OPCODE; i++) {
106 if (isUnused(i) || isOptimized(i)) continue;
107 printf(" set(%s);\n", constName[i]);
108 }
Dan Bornsteind12de172010-12-02 15:21:59 -0800109}
110
Dan Bornstein973171b2010-12-03 13:01:06 -0800111emission == "libcore-opcodes" {
112 emissionHandled = 1;
Dan Bornsteind12de172010-12-02 15:21:59 -0800113
Dan Bornstein3c4dc3b2010-12-02 16:05:51 -0800114 for (i = 0; i <= MAX_OPCODE; i++) {
115 if (isUnused(i) || isOptimized(i)) continue;
Dan Bornstein21f322d2010-12-03 13:16:08 -0800116 printf(" int OP_%-28s = 0x%04x;\n", constName[i], i);
Dan Bornsteind12de172010-12-02 15:21:59 -0800117 }
Dan Bornsteind12de172010-12-02 15:21:59 -0800118}
119
Dan Bornstein973171b2010-12-03 13:01:06 -0800120emission == "libcore-maximum-values" {
121 emissionHandled = 1;
Dan Bornsteind12de172010-12-02 15:21:59 -0800122
Dan Bornstein3c4dc3b2010-12-02 16:05:51 -0800123 printf(" MAXIMUM_VALUE = %d;\n", MAX_OPCODE);
124 printf(" MAXIMUM_PACKED_VALUE = %d;\n", MAX_PACKED_OPCODE);
Dan Bornsteind12de172010-12-02 15:21:59 -0800125}
126
Dan Bornsteinccaab182010-12-03 15:32:40 -0800127emission == "libdex-maximum-values" {
128 emissionHandled = 1;
129
Dan Bornstein29d5b5d2010-12-03 17:48:27 -0800130 printf("#define kMaxOpcodeValue 0x%x\n", MAX_OPCODE);
131 printf("#define kNumPackedOpcodes 0x%x\n", MAX_PACKED_OPCODE + 1);
Dan Bornsteinccaab182010-12-03 15:32:40 -0800132}
133
Dan Bornstein973171b2010-12-03 13:01:06 -0800134emission == "libdex-opcode-enum" {
135 emissionHandled = 1;
Dan Bornsteind12de172010-12-02 15:21:59 -0800136
Dan Bornstein3c4dc3b2010-12-02 16:05:51 -0800137 for (i = 0; i <= MAX_PACKED_OPCODE; i++) {
138 printf(" OP_%-28s = 0x%02x,\n", packedConstName[i], i);
Dan Bornsteind12de172010-12-02 15:21:59 -0800139 }
Dan Bornsteind12de172010-12-02 15:21:59 -0800140}
141
Dan Bornstein973171b2010-12-03 13:01:06 -0800142emission == "libdex-goto-table" {
143 emissionHandled = 1;
Dan Bornsteind12de172010-12-02 15:21:59 -0800144
Dan Bornstein3c4dc3b2010-12-02 16:05:51 -0800145 for (i = 0; i <= MAX_PACKED_OPCODE; i++) {
146 content = sprintf(" H(OP_%s),", packedConstName[i]);
Dan Bornsteind12de172010-12-02 15:21:59 -0800147 printf("%-78s\\\n", content);
148 }
Dan Bornsteind12de172010-12-02 15:21:59 -0800149}
150
Dan Bornstein973171b2010-12-03 13:01:06 -0800151emission == "libdex-opcode-names" {
152 emissionHandled = 1;
Dan Bornsteind12de172010-12-02 15:21:59 -0800153
Dan Bornstein3c4dc3b2010-12-02 16:05:51 -0800154 for (i = 0; i <= MAX_PACKED_OPCODE; i++) {
155 printf(" \"%s\",\n", packedName[i]);
Dan Bornsteind12de172010-12-02 15:21:59 -0800156 }
Dan Bornsteind12de172010-12-02 15:21:59 -0800157}
158
Dan Bornstein973171b2010-12-03 13:01:06 -0800159emission == "libdex-widths" {
160 emissionHandled = 1;
Dan Bornsteind12de172010-12-02 15:21:59 -0800161
162 col = 1;
Dan Bornstein3c4dc3b2010-12-02 16:05:51 -0800163 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, " ");
Dan Bornsteind12de172010-12-02 15:21:59 -0800166 }
Dan Bornsteind12de172010-12-02 15:21:59 -0800167}
168
Dan Bornstein973171b2010-12-03 13:01:06 -0800169emission == "libdex-flags" {
170 emissionHandled = 1;
Dan Bornsteind12de172010-12-02 15:21:59 -0800171
Dan Bornstein3c4dc3b2010-12-02 16:05:51 -0800172 for (i = 0; i <= MAX_PACKED_OPCODE; i++) {
173 value = flagsToC(packedFlags[i]);
Dan Bornsteind12de172010-12-02 15:21:59 -0800174 printf(" %s,\n", value);
175 }
Dan Bornsteind12de172010-12-02 15:21:59 -0800176}
177
Dan Bornstein973171b2010-12-03 13:01:06 -0800178emission == "libdex-formats" {
179 emissionHandled = 1;
Dan Bornsteind12de172010-12-02 15:21:59 -0800180
181 col = 1;
Dan Bornstein3c4dc3b2010-12-02 16:05:51 -0800182 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, " ");
Dan Bornsteind12de172010-12-02 15:21:59 -0800185 }
Dan Bornsteind12de172010-12-02 15:21:59 -0800186}
187
Dan Bornstein973171b2010-12-03 13:01:06 -0800188emission == "libdex-index-types" {
189 emissionHandled = 1;
Dan Bornsteind12de172010-12-02 15:21:59 -0800190
191 col = 1;
Dan Bornstein3c4dc3b2010-12-02 16:05:51 -0800192 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, " ");
Dan Bornsteind12de172010-12-02 15:21:59 -0800195 }
Dan Bornstein973171b2010-12-03 13:01:06 -0800196}
Dan Bornsteind12de172010-12-02 15:21:59 -0800197
Dan Bornstein973171b2010-12-03 13:01:06 -0800198#
199# General control (must appear after the directives).
200#
201
202# Handle the end of directive processing.
203emission != "" {
204 if (!emissionHandled) {
205 printf("WARNING: unknown tag \"%s\"\n", emission) >"/dev/stderr";
206 consumeUntil = "";
207 }
208
209 emission = "";
Dan Bornsteind12de172010-12-02 15:21:59 -0800210 next;
211}
212
Dan Bornstein973171b2010-12-03 13:01:06 -0800213# Most lines just get copied from the source as-is.
Dan Bornsteind12de172010-12-02 15:21:59 -0800214{ print; }
215
Dan Bornstein973171b2010-12-03 13:01:06 -0800216#
217# Helper functions.
218#
219
Dan Bornsteind12de172010-12-02 15:21:59 -0800220# 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
222# in.
223function colPrint(value, isLast, col, numCols, colWidth, linePrefix) {
224 isLast = (isLast || (col == numCols));
225 printf("%s%-*s%s",
226 (col == 1) ? linePrefix : " ",
227 isLast ? 1 : colWidth, value,
228 isLast ? "\n" : "");
229
230 return (col % numCols) + 1;
231}
232
233# Read the bytecode description file.
234function readBytecodes(i, parts, line, cmd, status, count) {
235 # locals: parts, line, cmd, status, count
236 for (;;) {
237 # Read a line.
238 status = getline line <bytecodeFile;
239 if (status == 0) break;
240 if (status < 0) {
241 print "trouble reading bytecode file";
242 exit 1;
243 }
244
245 # Clean up the line and extract the command.
246 gsub(/ */, " ", line);
247 sub(/ *#.*$/, "", line);
248 sub(/ $/, "", line);
249 sub(/^ /, "", line);
250 count = split(line, parts);
251 if (count == 0) continue; # Blank or comment line.
252 cmd = parts[1];
253 sub(/^[a-z][a-z]* */, "", line); # Remove the command from line.
254
255 if (cmd == "op") {
256 status = defineOpcode(line);
257 } else if (cmd == "format") {
258 status = defineFormat(line);
259 } else {
260 status = -1;
261 }
262
263 if (status != 0) {
Dan Bornstein973171b2010-12-03 13:01:06 -0800264 printf("syntax error on line: %s\n", line) >"/dev/stderr";
Dan Bornsteind12de172010-12-02 15:21:59 -0800265 return 1;
266 }
267 }
268
269 return 0;
270}
271
272# Define an opcode.
273function 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;
279
280 # Extract directly specified values from the line.
281 hex[idx] = parts[1];
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];
287
288 # Calculate derived values.
289
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, "/");
294
295 family[idx] = toupper(parts[1]);
296 gsub("-", "_", family[idx]); # Dash becomes underscore.
297 gsub("[+^]", "", family[idx]); # Plus and caret are removed.
298
299 split(format[idx], parts, ""); # Width is the first format char.
300 width[idx] = parts[1];
301
302 # This association is used when computing "next" opcodes.
303 familyFormat[family[idx],format[idx]] = idx;
304
305 # Verify values.
306
307 if (nextFormat[format[idx]] == "") {
Dan Bornstein973171b2010-12-03 13:01:06 -0800308 printf("unknown format: %s\n", format[idx]) >"/dev/stderr";
Dan Bornsteind12de172010-12-02 15:21:59 -0800309 return 1;
310 }
311
312 if (indexTypeValues[indexType[idx]] == "") {
Dan Bornstein973171b2010-12-03 13:01:06 -0800313 printf("unknown index type: %s\n", indexType[idx]) >"/dev/stderr";
Dan Bornsteind12de172010-12-02 15:21:59 -0800314 return 1;
315 }
316
317 if (flagsToC(flags[idx]) == "") {
Dan Bornstein973171b2010-12-03 13:01:06 -0800318 printf("bogus flags: %s\n", flags[idx]) >"/dev/stderr";
Dan Bornsteind12de172010-12-02 15:21:59 -0800319 return 1;
320 }
321
322 return 0;
323}
324
325# Define a format family.
326function 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;
331
332 parts[count + 1] = "none";
333 for (i = 1; i <= count; i++) {
334 nextFormat[parts[i]] = parts[i + 1];
335 }
336
337 return 0;
338}
339
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.
344function deriveOpcodeChains(i, op) {
345 # locals: i, op
346
347 for (i = 0; i <= MAX_OPCODE; i++) {
348 if (isUnused(i)) continue;
349 isFirst[i] = "true";
350 }
351
352 for (i = 0; i <= MAX_OPCODE; i++) {
353 if (isUnused(i)) continue;
354 op = findNextOpcode(i);
355 nextOpcode[i] = op;
356 if (op != -1) {
357 isFirst[op] = "false";
358 }
359 }
360}
361
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
366# is none.
367function findNextOpcode(idx, fam, fmt, result) {
368 # locals: fam, fmt, result
369 fam = family[idx];
370 fmt = format[idx];
371
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
374 # formats to try.
375 for (fmt = nextFormat[format[idx]]; fmt != "none"; fmt = nextFormat[fmt]) {
376 result = familyFormat[fam,fmt];
377 if (result != "") {
378 return result;
379 }
380 }
381
382 return -1;
383}
384
Dan Bornstein3c4dc3b2010-12-02 16:05:51 -0800385# 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.
388function createPackedTables(i, op) {
389 # locals: i, op
390 for (i = 0; i <= MAX_PACKED_OPCODE; i++) {
391 op = unpackOpcode(i);
392 if (i == 255) {
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";
398 packedFlags[i] = 0;
399 packedWidth[i] = 0;
400 packedIndexType[i] = "unknown";
401 } else if (isUnused(op)) {
402 packedName[i] = unusedName(op);
403 packedConstName[i] = unusedConstName(op);
404 packedFormat[i] = "00x";
405 packedFlags[i] = 0;
406 packedWidth[i] = 0;
407 packedIndexType[i] = "unknown";
408 } else {
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];
415 }
416 }
417}
418
419# Given a packed opcode, returns the raw (unpacked) opcode value.
420function unpackOpcode(idx) {
421 # Note: This must be the inverse of the corresponding code in
422 # libdex/DexOpcodes.h.
423 if (idx <= 255) {
424 return idx;
425 } else {
Dan Bornstein21f322d2010-12-03 13:16:08 -0800426 idx -= 256;
Dan Bornstein3c4dc3b2010-12-02 16:05:51 -0800427 return (idx * 256) + 255;
428 }
429}
430
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.
435function unusedName(idx) {
436 if (idx <= 255) {
437 return sprintf("unused-%02x", idx);
438 } else {
439 return sprintf("unused-%04x", idx);
440 }
441}
442
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.
446function unusedConstName(idx) {
447 if (idx <= 255) {
448 return toupper(sprintf("UNUSED_%02x", idx));
449 } else {
450 return toupper(sprintf("UNUSED_%04x", idx));
451 }
452}
453
Dan Bornsteind12de172010-12-02 15:21:59 -0800454# Convert a hex value to an int.
455function parseHex(hex, result, chars, count, c, i) {
456 # locals: result, chars, count, c, i
457 hex = tolower(hex);
458 count = split(hex, chars, "");
459 result = 0;
460 for (i = 1; i <= count; i++) {
461 c = index("0123456789abcdef", chars[i]);
462 if (c == 0) {
Dan Bornstein973171b2010-12-03 13:01:06 -0800463 printf("bogus hex value: %s\n", hex) >"/dev/stderr";
Dan Bornsteind12de172010-12-02 15:21:59 -0800464 return -1;
465 }
466 result = (result * 16) + c - 1;
467 }
468 return result;
469}
470
471# Initialize the indexTypes data.
472function 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";
483}
484
485# Initialize the flags data.
486function 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";
495}
496
497# Translate the given flags into the equivalent C expression. Returns
498# "" on error.
499function flagsToC(f, parts, result, i) {
500 # locals: parts, result, i
501 count = split(f, parts, /\|/); # Split input at pipe characters.
502 result = "0";
503
504 for (i = 1; i <= count; i++) {
505 f = flagValues[parts[i]];
506 if (f == "") {
Dan Bornstein973171b2010-12-03 13:01:06 -0800507 printf("bogus flag: %s\n", f) >"/dev/stderr";
Dan Bornsteind12de172010-12-02 15:21:59 -0800508 return ""; # Bogus flag name.
509 } else if (f == "0") {
510 # Nothing to append for this case.
511 } else if (result == "0") {
512 result = f;
513 } else {
514 result = result "|" f;
515 }
516 }
517
518 return result;
519}
520
Dan Bornsteind12de172010-12-02 15:21:59 -0800521# Returns true if the given opcode (by index) is an "optimized" opcode.
522function isOptimized(idx, parts, f) {
523 # locals: parts, f
524 split(flags[idx], parts, /\|/); # Split flags[idx] at pipes.
525 for (f in parts) {
526 if (parts[f] == "optimized") return 1;
527 }
528 return 0;
529}
530
531# Returns true if there is no definition for the given opcode (by index).
532function isUnused(idx) {
533 return (name[idx] == "");
534}