aboutsummaryrefslogtreecommitdiff
path: root/src/examples/Mini/ASTProgram.java
blob: b6d240fb0fb8dbc670690a81cf6db5da28348da9 (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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License. 
 *
 */
/* Generated By:JJTree: Do not edit this line. ASTProgram.java */
/* JJT: 0.3pre1 */

package Mini;
import java.io.PrintWriter;

import org.apache.bcel.classfile.Field;
import org.apache.bcel.generic.ALOAD;
import org.apache.bcel.generic.ClassGen;
import org.apache.bcel.generic.ConstantPoolGen;
import org.apache.bcel.generic.GETSTATIC;
import org.apache.bcel.generic.ILOAD;
import org.apache.bcel.generic.INVOKESPECIAL;
import org.apache.bcel.generic.INVOKESTATIC;
import org.apache.bcel.generic.INVOKEVIRTUAL;
import org.apache.bcel.generic.InstructionConstants;
import org.apache.bcel.generic.InstructionList;
import org.apache.bcel.generic.MethodGen;
import org.apache.bcel.generic.NEW;
import org.apache.bcel.generic.PUSH;
import org.apache.bcel.generic.PUTSTATIC;
import org.apache.bcel.generic.RETURN;
import org.apache.bcel.generic.Type;

/**
 * Root node of everything, direct children are nodes of type FunDecl
 *
 * @version $Id$
 * @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
 */
public class ASTProgram extends SimpleNode
implements MiniParserConstants, MiniParserTreeConstants, org.apache.bcel.Constants {
  private ASTFunDecl[] fun_decls; // Children: Function declarations
  private Environment  env;       // Environment contains variables and functions

  ASTProgram(int id) {
    super(id);

    env = new Environment();

    /* Add predefined functions WRITE/READ.
     * WRITE has one arg of type T_INT, both return T_INT.
     */
    ASTIdent   ident  = new ASTIdent("WRITE", T_INT, -1, -1);
    ASTIdent[] args   = { new ASTIdent("", T_INT, -1, -1) }; 
    Function   fun    = new Function(ident, args, true);
    env.put(fun);

    ident = new ASTIdent("READ", T_INT, -1, -1);
    args  = new ASTIdent[0];
    fun   = new Function(ident, args, true);
    env.put(fun);
    
    /* Add predefined idents TRUE/FALSE of type T_BOOLEAN
     */
    ident = new ASTIdent("TRUE", T_BOOLEAN, -1, -1);
    Variable var = new Variable(ident, true);
    env.put(var);

    ident = new ASTIdent("FALSE", T_BOOLEAN, -1, -1);
    var   = new Variable(ident, true);
    env.put(var);
  }

  ASTProgram(MiniParser p, int id) {
    super(p, id);
  }

  public static Node jjtCreate(MiniParser p, int id) {
    return new ASTProgram(p, id);
  }

  /**
   * Overrides SimpleNode.closeNode().
   * Cast children to appropiate type.
   */
  @Override
  public void closeNode() {
    if(children != null) { // Non-empty program ?
      fun_decls = new ASTFunDecl[children.length];
      System.arraycopy(children, 0, fun_decls, 0, children.length);
      children=null; // Throw away old reference
    }
  }

  /**
   * First pass of parse tree.
   *
   * Put everything into the environment, which is copied appropiately to each
   * recursion level, i.e. each FunDecl gets its own copy that it can further
   * manipulate. 
   *
   * Checks for name clashes of function declarations.
   */
  public ASTProgram traverse() {
    ASTFunDecl f;
    ASTIdent   name;
    String     fname;
    EnvEntry   fun;
    Function   main=null;

    if(fun_decls != null) {
      // Put function names into hash table aka. environment
      for(int i=0; i < fun_decls.length; i++) {
        f     = fun_decls[i];
        name  = f.getName();
        fname = name.getName();
        fun   = env.get(fname); // Lookup in env
        
        if(fun != null) {
        MiniC.addError(f.getLine(), f.getColumn(),
                         "Redeclaration of " + fun + ".");
    } else {
        env.put(new Function(name, null)); // `args' will be set by FunDecl.traverse()
    }

        
      }

      // Go for it
      for(int i=0; i < fun_decls.length; i++) {
        fun_decls[i] = fun_decls[i].traverse((Environment)env.clone());
        
        // Look for `main' routine
        fname = fun_decls[i].getName().getName();
        if(fname.equals("main")) {
        main = (Function)env.get(fname);
    }
      }
      
      if(main == null) {
        MiniC.addError(0, 0, "You didn't declare a `main' function.");
    } else if(main.getNoArgs() != 0) {
        MiniC.addError(main.getLine(), main.getColumn(), 
                        "Main function has too many arguments declared.");
    }
    }

    return this;
  }

  /** 
   * Second pass, determine type of each node, if possible.
   */
  public void eval(int pass) {

    for(int i=0; i < fun_decls.length; i++) {
      fun_decls[i].eval(pass);

      if(pass == 3) { // Final check for unresolved types
        ASTIdent name = fun_decls[i].getName();

        if(name.getType() == T_UNKNOWN) {
        MiniC.addError(name.getColumn(), name.getLine(),
                         "Type of function " + name.getName() +
                         " can not be determined (infinite recursion?).");
    }
      }
    }
  }

  /**
   * Fifth pass, produce Java code.
   */
  public void code(PrintWriter out, String name) {
    out.println("import java.io.BufferedReader;");
    out.println("import java.io.InputStreamReader;");
    out.println("import java.io.IOException;\n");

    out.println("public final class " + name + " {");
    out.println("  private static BufferedReader _in = new BufferedReader" + 
                "(new InputStreamReader(System.in));\n");

    out.println("  private static int _readInt() throws IOException {\n" +
                "    System.out.print(\"Please enter a number> \");\n" + 
                "    return Integer.parseInt(_in.readLine());\n  }\n");

    out.println("  private static int _writeInt(int n) {\n" +
                "    System.out.println(\"Result: \" + n);\n    return 0;\n  }\n");

    for(int i=0; i < fun_decls.length; i++) {
        fun_decls[i].code(out);
    }

    out.println("}");
  }

  /**
   * Fifth pass, produce Java byte code.
   */
  public void byte_code(ClassGen class_gen, ConstantPoolGen cp) {
    /* private static BufferedReader _in;
     */
    class_gen.addField(new Field(ACC_PRIVATE | ACC_STATIC,
                                 cp.addUtf8("_in"),
                                 cp.addUtf8("Ljava/io/BufferedReader;"),
                                 null, cp.getConstantPool()));

    MethodGen       method;
    InstructionList il = new InstructionList();
    String          class_name = class_gen.getClassName();

    /* Often used constant pool entries
     */
    int             _in = cp.addFieldref(class_name, "_in", "Ljava/io/BufferedReader;");

    int             out = cp.addFieldref("java.lang.System", "out",
                                         "Ljava/io/PrintStream;");

    il.append(new GETSTATIC(out));
    il.append(new PUSH(cp, "Please enter a number> "));
    il.append(new INVOKEVIRTUAL(cp.addMethodref("java.io.PrintStream",
                                                "print",
                                                "(Ljava/lang/String;)V")));
    il.append(new GETSTATIC(_in));
    il.append(new INVOKEVIRTUAL(cp.addMethodref("java.io.BufferedReader",
                                                "readLine",
                                                "()Ljava/lang/String;")));
    il.append(new INVOKESTATIC(cp.addMethodref("java.lang.Integer",
                                                "parseInt",
                                                "(Ljava/lang/String;)I")));
    il.append(InstructionConstants.IRETURN);

    /* private static int _readInt() throws IOException
     */
    method = new MethodGen(ACC_STATIC | ACC_PRIVATE | ACC_FINAL,
                           Type.INT, Type.NO_ARGS, null,
                           "_readInt", class_name, il, cp);

    method.addException("java.io.IOException");

    method.setMaxStack(2);
    class_gen.addMethod(method.getMethod());

    /* private static int _writeInt(int i) throws IOException
     */
    Type[]   args = { Type.INT };
    String[] argv = { "i" } ;
    il = new InstructionList();
    il.append(new GETSTATIC(out));
    il.append(new NEW(cp.addClass("java.lang.StringBuffer")));
    il.append(InstructionConstants.DUP);
    il.append(new PUSH(cp, "Result: "));
    il.append(new INVOKESPECIAL(cp.addMethodref("java.lang.StringBuffer",
                                                "<init>",
                                                "(Ljava/lang/String;)V")));

    il.append(new ILOAD(0));
    il.append(new INVOKEVIRTUAL(cp.addMethodref("java.lang.StringBuffer",
                                                "append",
                                                "(I)Ljava/lang/StringBuffer;")));

    il.append(new INVOKEVIRTUAL(cp.addMethodref("java.lang.StringBuffer",
                                                "toString",
                                                "()Ljava/lang/String;")));
    
    il.append(new INVOKEVIRTUAL(cp.addMethodref("java.io.PrintStream",
                                                "println",
                                                "(Ljava/lang/String;)V")));
    il.append(new PUSH(cp, 0));
    il.append(InstructionConstants.IRETURN); // Reuse objects, if possible

    method = new MethodGen(ACC_STATIC | ACC_PRIVATE | ACC_FINAL,
                           Type.INT, args, argv,
                           "_writeInt", class_name, il, cp);

    method.setMaxStack(4);
    class_gen.addMethod(method.getMethod());

    /* public <init> -- constructor
     */
    il.dispose(); // Dispose instruction handles for better memory utilization

    il = new InstructionList();
    il.append(new ALOAD(0)); // Push `this'
    il.append(new INVOKESPECIAL(cp.addMethodref("java.lang.Object",
                                                "<init>", "()V")));
    il.append(new RETURN());

    method = new MethodGen(ACC_PUBLIC, Type.VOID, Type.NO_ARGS, null,
                           "<init>", class_name, il, cp);
    
    method.setMaxStack(1);
    class_gen.addMethod(method.getMethod());

    /* class initializer
     */
    il.dispose(); // Dispose instruction handles for better memory utilization
    il = new InstructionList();
    il.append(new NEW(cp.addClass("java.io.BufferedReader")));
    il.append(InstructionConstants.DUP);
    il.append(new NEW(cp.addClass("java.io.InputStreamReader")));
    il.append(InstructionConstants.DUP);
    il.append(new GETSTATIC(cp.addFieldref("java.lang.System", "in",
                                           "Ljava/io/InputStream;")));
    il.append(new INVOKESPECIAL(cp.addMethodref("java.io.InputStreamReader",
                                                "<init>", "(Ljava/io/InputStream;)V")));
    il.append(new INVOKESPECIAL(cp.addMethodref("java.io.BufferedReader",
                                                "<init>", "(Ljava/io/Reader;)V")));
    il.append(new PUTSTATIC(_in));
    il.append(InstructionConstants.RETURN); // Reuse instruction constants

    method = new MethodGen(ACC_STATIC, Type.VOID, Type.NO_ARGS, null,
                           "<clinit>", class_name, il, cp);

    method.setMaxStack(5);
    class_gen.addMethod(method.getMethod());

    for(int i=0; i < fun_decls.length; i++) {
        fun_decls[i].byte_code(class_gen, cp);
    }
  }

  @Override
  public void dump(String prefix) {
    System.out.println(toString(prefix));

    for(int i = 0; i < fun_decls.length; ++i) {
        fun_decls[i].dump(prefix + " ");
    }
  }
}