OSDN Git Service

am a38780c9: merge from open-source master
[android-x86/dalvik.git] / dx / etc / opcode-gen
1 #!/bin/bash
2 #
3 # Copyright (C) 2007 The Android Open Source Project
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #     http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # opcode-gen <file>
18 #
19 # Use the file bytecodes.txt to generate code inside <file>, based on
20 # the directives found in that file:
21 #
22 #     opcodes:   static final ints for each opcode
23 #     dops:      static final objects for each opcode
24 #     dops-init: initialization code for the "dops"
25
26 file="$1"
27 tmpfile="/tmp/$$.txt"
28
29 if [ "x$1" = "x" ]; then
30     echo "must specify a file"
31     exit 1
32 fi
33
34 # Set up prog to be the path of this script, including following symlinks,
35 # and set up progdir to be the fully-qualified pathname of its directory.
36 prog="$0"
37 while [ -h "${prog}" ]; do
38     newProg=`/bin/ls -ld "${prog}"`
39     newProg=`expr "${newProg}" : ".* -> \(.*\)$"`
40     if expr "x${newProg}" : 'x/' >/dev/null; then
41         prog="${newProg}"
42     else
43         progdir=`dirname "${prog}"`
44         prog="${progdir}/${newProg}"
45     fi
46 done
47 oldwd=`pwd`
48 progdir=`dirname "${prog}"`
49 cd "${progdir}"
50 progdir=`pwd`
51 prog="${progdir}"/`basename "${prog}"`
52 cd "${oldwd}"
53
54 bytecodeFile="$progdir/bytecode.txt"
55
56 awk -v "bytecodeFile=$bytecodeFile" '
57
58 BEGIN {
59     readBytecodes();
60     consumeUntil = "";
61 }
62
63 consumeUntil != "" {
64     if (index($0, consumeUntil) != 0) {
65         consumeUntil = "";
66     } else {
67         next;
68     }
69 }
70
71 /BEGIN\(opcodes\)/ {
72     consumeUntil = "END(opcodes)";
73     print;
74
75     for (i = 0; i < 256; i++) {
76         printf("    public static final int %s = 0x%s;\n",
77                uppername[i], hex[i]);
78     }
79
80     next;
81 }
82
83 /BEGIN\(dops\)/ {
84     consumeUntil = "END(dops)";
85     print;
86
87     for (i = 0; i < 256; i++) {
88         if (index(name[i], "unused") != 0) {
89             continue;
90         }
91         printf("    public static final Dop %s =\n" \
92                "        new Dop(DalvOps.%s, DalvOps.%s,\n" \
93                "            Form%s.THE_ONE, %s, \"%s\");\n\n",
94                uppername[i], uppername[i], family[i], format[i], hasres[i],
95                name[i]);
96     }
97
98     next;
99 }
100
101 /BEGIN\(dops-init\)/ {
102     consumeUntil = "END(dops-init)";
103     print;
104
105     for (i = 0; i < 256; i++) {
106         if (index(name[i], "unused") != 0) {
107             continue;
108         }
109         printf("        set(%s);\n", uppername[i]);
110     }
111
112     next;
113 }
114
115 { print; }
116
117 function readBytecodes(i, parts) {
118     for (i = 0; i < 256; i++) {
119         $0 = "";
120         while (($0 == "") || (index($0, "#") != 0)) {
121             if ((getline <bytecodeFile) != 1) {
122                 print "trouble reading bytecode file";
123                 exit 1;
124             }
125         }
126         split($0, parts);
127         hex[i] = parts[1];
128         format[i] = parts[2];
129         hasres[i] = (parts[3] == "n") ? "false" : "true";
130         name[i] = parts[4];
131         uppername[i] = toupper(parts[4]);
132         gsub("[---/]", "_", uppername[i]);
133         split(name[i], parts, "/");
134         family[i] = toupper(parts[1]);
135         gsub("-", "_", family[i]);
136     }
137 }
138 ' "$file" > "$tmpfile"
139
140 cp "$tmpfile" "$file"
141 rm "$tmpfile"