aboutsummaryrefslogtreecommitdiff
path: root/src/share/vm/prims/jvmtiEnvFill.java
blob: dfd28c306bcaf7d28e384c7dad31b6fb92b6d166 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
 * Copyright 2003-2005 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 *
 */

import java.io.*;
import java.util.*;

class jvmtiEnvFill {

    public static void main(String[] args) throws IOException {
        if (args.length != 3) {
            System.err.println("usage: <filledFile> <stubFile> <resultFile>");
            System.exit(1);
        }
        String filledFN = args[0];
        String stubFN = args[1];
        String resultFN = args[2];

        SourceFile filledSF = new SourceFile(filledFN);
        SourceFile stubSF = new SourceFile(stubFN);


        stubSF.fill(filledSF);

        PrintWriter out = new PrintWriter(new FileWriter(resultFN));
        stubSF.output(out);
        out.close();
    }
}

class SourceFile {

    static final String endFilePrefix = "// end file prefix";
    static final String functionPrefix = "JvmtiEnv::";

    final String fn;
    LineNumberReader in;
    String line;
    List<String> top = new ArrayList<String>();
    List<String> before = new ArrayList<String>();
    boolean inFilePrefix = true;
    List<Function> functions = new ArrayList<Function>();
    Map<String, Function> functionMap = new HashMap<String, Function>();

    class Function {
      String name;
      String args;
      String compareArgs;
      List comment;
      List<String> body = new ArrayList<String>();

      Function() throws IOException {
        line = in.readLine();
        String trimmed = line.trim();
        if (!trimmed.startsWith(functionPrefix)) {
            error("expected '" + functionPrefix + "'");
        }
        int index = trimmed.indexOf('(', functionPrefix.length());
        if (index == -1) {
            error("missing open paren");
        }
        name = trimmed.substring(functionPrefix.length(), index);
        int index2 = trimmed.indexOf(')', index);
        if (index2 == -1) {
            error("missing close paren - must be on same line");
        }
        args = trimmed.substring(index+1, index2);
        compareArgs = args.replaceAll("\\s", "");
        String tail = trimmed.substring(index2+1).trim();
        if (!tail.equals("{")) {
            error("function declaration first line must end with open bracket '{', instead got '" +
                   tail + "'");
        }
        while(true) {
            line = in.readLine();
            if (line == null) {
                line = ""; // so error does not look wierd
                error("unexpected end of file");
            }
            if (line.startsWith("}")) {
                break;
            }
            body.add(line);
        }
        String expected = "} /* end " + name + " */";
        trimmed = line.replaceAll("\\s","");
        if (!trimmed.equals(expected.replaceAll("\\s",""))) {
            error("function end is malformed - should be: " + expected);
        }
        // copy over the comment prefix
        comment = before;
        before = new ArrayList<String>();
      }

      void remove() {
        functionMap.remove(name);
      }

      String fileName() {
        return fn;
      }

      void fill(Function filledFunc) {
        if (filledFunc == null) {
            System.err.println("Warning: function " + name + " missing from filled file");
            body.add(0, "    /*** warning: function added and not filled in ***/");
        } else {
            int fbsize = filledFunc.body.size();
            int bsize = body.size();
            if (fbsize > bsize  || !body.subList(bsize-fbsize,bsize).equals(filledFunc.body)) {
                // it has actually been filled in
                body = filledFunc.body;
                if (!compareArgs.equals(filledFunc.compareArgs)) {
                    System.err.println("Warning: function " + name +
                                       ": filled and stub arguments differ");
                    System.err.println("  old (filled): " + filledFunc.args);
                    System.err.println("  new (stub): " + args);
                    body.add(0, "    /*** warning: arguments changed, were: " +
                             filledFunc.args + " ***/");
                }
            }
            filledFunc.remove();  // mark used
        }
      }

      void output(PrintWriter out) {
            Iterator it = comment.iterator();
            while (it.hasNext()) {
                out.println(it.next());
            }
            out.println("jvmtiError");
            out.print(functionPrefix);
            out.print(name);
            out.print('(');
            out.print(args);
            out.println(") {");
            it = body.iterator();
            while (it.hasNext()) {
                out.println(it.next());
            }
            out.print("} /* end ");
            out.print(name);
            out.println(" */");
      }
    }

    SourceFile(String fn) throws IOException {
        this.fn = fn;
        Reader reader = new FileReader(fn);
        in = new LineNumberReader(reader);

        while (readGaps()) {
            Function func = new Function();
            functionMap.put(func.name, func);
            functions.add(func);
        }

        in.close();
    }

    void error(String msg) {
        System.err.println("Fatal error parsing file: " + fn);
        System.err.println("Line number: " + in.getLineNumber());
        System.err.println("Error message: " + msg);
        System.err.println("Source line: " + line);
        System.exit(1);
    }

    boolean readGaps() throws IOException {
        while(true) {
            line = in.readLine();
            if (line == null) {
                return false; // end of file
            }
            if (!inFilePrefix && line.startsWith("}")) {
                error("unexpected close bracket in first column, outside of function.\n");
            }
            String trimmed = line.trim();
            if (line.startsWith("jvmtiError")) {
                if (trimmed.equals("jvmtiError")) {
                    if (inFilePrefix) {
                        error("unexpected 'jvmtiError' line in file prefix.\n" +
                              "is '" + endFilePrefix + "'... line missing?");
                    }
                    return true; // beginning of a function
                } else {
                    error("extra characters at end of 'jvmtiError'");
                }
            }
            if (inFilePrefix) {
                top.add(line);
            } else {
                trimmed = line.trim();
                if (!trimmed.equals("") && !trimmed.startsWith("//") && !trimmed.startsWith("#")) {
                    error("only comments and blank lines allowed between functions");
                }
                before.add(line);
            }
            if (line.replaceAll("\\s","").toLowerCase().startsWith(endFilePrefix.replaceAll("\\s",""))) {
                if (!inFilePrefix) {
                    error("excess '" + endFilePrefix + "'");
                }
                inFilePrefix = false;
            }
        }
    }

    void fill(SourceFile filledSF) {
        // copy beginning of file straight from filled file
        top = filledSF.top;

        // file in functions
        Iterator it = functions.iterator();
        while (it.hasNext()) {
            Function stubFunc = (Function)(it.next());
            Function filledFunc = (Function)filledSF.functionMap.get(stubFunc.name);
            stubFunc.fill(filledFunc);
        }
        if (filledSF.functionMap.size() > 0) {
            System.err.println("Warning: the following functions were present in the " +
                                "filled file but missing in the stub file and thus not copied:");
            it  = filledSF.functionMap.values().iterator();
            while (it.hasNext()) {
                System.err.println("        " + ((Function)(it.next())).name);
            }
        }
    }

    void output(PrintWriter out) {
        Iterator it = top.iterator();
        while (it.hasNext()) {
            out.println(it.next());
        }
        it = functions.iterator();
        while (it.hasNext()) {
            Function stubFunc = (Function)(it.next());
            stubFunc.output(out);
        }
    }
}