langtools/src/share/classes/com/sun/tools/javac/jvm/Gen.java
author ksrini
Fri, 02 Sep 2011 07:54:16 -0700
changeset 10457 644ffe54b5c7
parent 9303 eae35c201e19
child 10811 4d4ed480210e
permissions -rw-r--r--
7024096: Stack trace has invalid line numbers Reviewed-by: jjg, darcy Contributed-by: bruce.chapman.nz@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
8031
d5fe2c1cecfc 6992999: fully remove JSR 308 from langtools
jjg
parents: 7681
diff changeset
     2
 * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5492
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5492
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5492
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5492
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5492
diff changeset
    23
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.javac.jvm;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
import java.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
3149
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 2723
diff changeset
    29
import javax.lang.model.element.ElementKind;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 2723
diff changeset
    30
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import com.sun.tools.javac.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import com.sun.tools.javac.util.List;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import com.sun.tools.javac.code.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import com.sun.tools.javac.comp.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import com.sun.tools.javac.tree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import com.sun.tools.javac.code.Symbol.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import com.sun.tools.javac.code.Type.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import com.sun.tools.javac.jvm.Code.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
import com.sun.tools.javac.jvm.Items.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
import com.sun.tools.javac.tree.JCTree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
import static com.sun.tools.javac.code.Flags.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
import static com.sun.tools.javac.code.Kinds.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
import static com.sun.tools.javac.code.TypeTags.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
import static com.sun.tools.javac.jvm.ByteCodes.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
import static com.sun.tools.javac.jvm.CRTFlags.*;
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6354
diff changeset
    49
import static com.sun.tools.javac.main.OptionName.*;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
/** This pass maps flat Java (i.e. without inner classes) to bytecodes.
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    53
 *  <p><b>This is NOT part of any supported API.
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    54
 *  If you write code that depends on this, you do so at your own risk.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
public class Gen extends JCTree.Visitor {
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
    protected static final Context.Key<Gen> genKey =
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
        new Context.Key<Gen>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    private final Log log;
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
    private final Symtab syms;
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
    private final Check chk;
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
    private final Resolve rs;
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
    private final TreeMaker make;
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 10
diff changeset
    67
    private final Names names;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
    private final Target target;
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    private final Type stringBufferType;
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    private final Map<Type,Symbol> stringBufferAppend;
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
    private Name accessDollar;
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
    private final Types types;
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
    /** Switch: GJ mode?
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
    private final boolean allowGenerics;
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
    /** Set when Miranda method stubs are to be generated. */
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
    private final boolean generateIproxies;
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
    /** Format of stackmap tables to be generated. */
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
    private final Code.StackMapFormat stackMap;
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
    /** A type that serves as the expected type for all method expressions.
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
    private final Type methodType;
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
    public static Gen instance(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
        Gen instance = context.get(genKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
        if (instance == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
            instance = new Gen(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
        return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
    protected Gen(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
        context.put(genKey, this);
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 10
diff changeset
    98
        names = Names.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
        log = Log.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
        syms = Symtab.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
        chk = Check.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
        rs = Resolve.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
        make = TreeMaker.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
        target = Target.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
        types = Types.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
        methodType = new MethodType(null, null, null, syms.methodClass);
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
        allowGenerics = Source.instance(context).allowGenerics();
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
        stringBufferType = target.useStringBuilder()
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
            ? syms.stringBuilderType
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
            : syms.stringBufferType;
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
        stringBufferAppend = new HashMap<Type,Symbol>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
        accessDollar = names.
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
            fromString("access" + target.syntheticNameChar());
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
        Options options = Options.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
        lineDebugInfo =
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6354
diff changeset
   117
            options.isUnset(G_CUSTOM) ||
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6354
diff changeset
   118
            options.isSet(G_CUSTOM, "lines");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
        varDebugInfo =
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6354
diff changeset
   120
            options.isUnset(G_CUSTOM)
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6354
diff changeset
   121
            ? options.isSet(G)
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6354
diff changeset
   122
            : options.isSet(G_CUSTOM, "vars");
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6354
diff changeset
   123
        genCrt = options.isSet(XJCOV);
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6354
diff changeset
   124
        debugCode = options.isSet("debugcode");
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6354
diff changeset
   125
        allowInvokedynamic = target.hasInvokedynamic() || options.isSet("invokedynamic");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
        generateIproxies =
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
            target.requiresIproxy() ||
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6354
diff changeset
   129
            options.isSet("miranda");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
        if (target.generateStackMapTable()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
            // ignore cldc because we cannot have both stackmap formats
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
            this.stackMap = StackMapFormat.JSR202;
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
            if (target.generateCLDCStackmap()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
                this.stackMap = StackMapFormat.CLDC;
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
                this.stackMap = StackMapFormat.NONE;
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
        // by default, avoid jsr's for simple finalizers
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
        int setjsrlimit = 50;
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
        String jsrlimitString = options.get("jsrlimit");
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
        if (jsrlimitString != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
                setjsrlimit = Integer.parseInt(jsrlimitString);
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
            } catch (NumberFormatException ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
                // ignore ill-formed numbers for jsrlimit
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
        this.jsrlimit = setjsrlimit;
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
        this.useJsrLocally = false; // reset in visitTry
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
    /** Switches
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
    private final boolean lineDebugInfo;
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
    private final boolean varDebugInfo;
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
    private final boolean genCrt;
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
    private final boolean debugCode;
2723
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 1264
diff changeset
   162
    private final boolean allowInvokedynamic;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
    /** Default limit of (approximate) size of finalizer to inline.
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
     *  Zero means always use jsr.  100 or greater means never use
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
     *  jsr.
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
    private final int jsrlimit;
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
    /** True if jsr is used.
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
    private boolean useJsrLocally;
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
    /* Constant pool, reset by genClass.
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
    private Pool pool = new Pool();
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
    /** Code buffer, set by genMethod.
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
    private Code code;
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
    /** Items structure, set by genMethod.
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
    private Items items;
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
    /** Environment for symbol lookup, set by genClass
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
    private Env<AttrContext> attrEnv;
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
    /** The top level tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
    private JCCompilationUnit toplevel;
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
    /** The number of code-gen errors in this class.
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
    private int nerrs = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
    /** A hash table mapping syntax trees to their ending source positions.
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
    private Map<JCTree, Integer> endPositions;
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
    /** Generate code to load an integer constant.
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
     *  @param n     The integer to be loaded.
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
    void loadIntConst(int n) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
        items.makeImmediateItem(syms.intType, n).load();
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
    /** The opcode that loads a zero constant of a given type code.
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
     *  @param tc   The given type code (@see ByteCode).
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
    public static int zero(int tc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
        switch(tc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
        case INTcode: case BYTEcode: case SHORTcode: case CHARcode:
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
            return iconst_0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
        case LONGcode:
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
            return lconst_0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
        case FLOATcode:
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
            return fconst_0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
        case DOUBLEcode:
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
            return dconst_0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
            throw new AssertionError("zero");
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
    /** The opcode that loads a one constant of a given type code.
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
     *  @param tc   The given type code (@see ByteCode).
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
    public static int one(int tc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
        return zero(tc) + 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
    /** Generate code to load -1 of the given type code (either int or long).
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
     *  @param tc   The given type code (@see ByteCode).
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
    void emitMinusOne(int tc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
        if (tc == LONGcode) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
            items.makeImmediateItem(syms.longType, new Long(-1)).load();
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
            code.emitop0(iconst_m1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
    /** Construct a symbol to reflect the qualifying type that should
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
     *  appear in the byte code as per JLS 13.1.
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
     *  For target >= 1.2: Clone a method with the qualifier as owner (except
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
     *  for those cases where we need to work around VM bugs).
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
     *  For target <= 1.1: If qualified variable or method is defined in a
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
     *  non-accessible class, clone it with the qualifier class as owner.
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
     *  @param sym    The accessed symbol
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
     *  @param site   The qualifier's type.
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
    Symbol binaryQualifier(Symbol sym, Type site) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
        if (site.tag == ARRAY) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
            if (sym == syms.lengthVar ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
                sym.owner != syms.arrayClass)
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
                return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
            // array clone can be qualified by the array type in later targets
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
            Symbol qualifier = target.arrayBinaryCompatibility()
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
                ? new ClassSymbol(Flags.PUBLIC, site.tsym.name,
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
                                  site, syms.noSymbol)
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
                : syms.objectType.tsym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
            return sym.clone(qualifier);
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
        if (sym.owner == site.tsym ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
            (sym.flags() & (STATIC | SYNTHETIC)) == (STATIC | SYNTHETIC)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
            return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
        if (!target.obeyBinaryCompatibility())
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
            return rs.isAccessible(attrEnv, (TypeSymbol)sym.owner)
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
                ? sym
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
                : sym.clone(site.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
        if (!target.interfaceFieldsBinaryCompatibility()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
            if ((sym.owner.flags() & INTERFACE) != 0 && sym.kind == VAR)
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
                return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
        // leave alone methods inherited from Object
9303
eae35c201e19 7032975: API files in javax.annotation.processing need to be updated for references to JLS
jjh
parents: 9300
diff changeset
   286
        // JLS 13.1.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
        if (sym.owner == syms.objectType.tsym)
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
            return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
        if (!target.interfaceObjectOverridesBinaryCompatibility()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
            if ((sym.owner.flags() & INTERFACE) != 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
                syms.objectType.tsym.members().lookup(sym.name).scope != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
                return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
        return sym.clone(site.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
    /** Insert a reference to given type in the constant pool,
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
     *  checking for an array with too many dimensions;
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
     *  return the reference's index.
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
     *  @param type   The type for which a reference is inserted.
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
    int makeRef(DiagnosticPosition pos, Type type) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
        checkDimension(pos, type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
        return pool.put(type.tag == CLASS ? (Object)type.tsym : (Object)type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
    /** Check if the given type is an array with too many dimensions.
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
    private void checkDimension(DiagnosticPosition pos, Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
        switch (t.tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
        case METHOD:
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
            checkDimension(pos, t.getReturnType());
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
            for (List<Type> args = t.getParameterTypes(); args.nonEmpty(); args = args.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
                checkDimension(pos, args.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
        case ARRAY:
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
            if (types.dimensions(t) > ClassFile.MAX_DIMENSIONS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
                log.error(pos, "limit.dimensions");
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
                nerrs++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
    /** Create a tempory variable.
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
     *  @param type   The variable's type.
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
    LocalItem makeTemp(Type type) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
        VarSymbol v = new VarSymbol(Flags.SYNTHETIC,
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
                                    names.empty,
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
                                    type,
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
                                    env.enclMethod.sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
        code.newLocal(v);
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
        return items.makeLocalItem(v);
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
    /** Generate code to call a non-private method or constructor.
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
     *  @param pos         Position to be used for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
     *  @param site        The type of which the method is a member.
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
     *  @param name        The method's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
     *  @param argtypes    The method's argument types.
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
     *  @param isStatic    A flag that indicates whether we call a
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
     *                     static or instance method.
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
    void callMethod(DiagnosticPosition pos,
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
                    Type site, Name name, List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
                    boolean isStatic) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
        Symbol msym = rs.
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
            resolveInternalMethod(pos, attrEnv, site, name, argtypes, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
        if (isStatic) items.makeStaticItem(msym).invoke();
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
        else items.makeMemberItem(msym, name == names.init).invoke();
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
    /** Is the given method definition an access method
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
     *  resulting from a qualified super? This is signified by an odd
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
     *  access code.
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
    private boolean isAccessSuper(JCMethodDecl enclMethod) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
        return
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
            (enclMethod.mods.flags & SYNTHETIC) != 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
            isOddAccessName(enclMethod.name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
    /** Does given name start with "access$" and end in an odd digit?
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
    private boolean isOddAccessName(Name name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
        return
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
            name.startsWith(accessDollar) &&
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 10
diff changeset
   373
            (name.getByteAt(name.getByteLength() - 1) & 1) == 1;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
/* ************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
 * Non-local exits
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
 *************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
    /** Generate code to invoke the finalizer associated with given
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
     *  environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
     *  Any calls to finalizers are appended to the environments `cont' chain.
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
     *  Mark beginning of gap in catch all range for finalizer.
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
    void genFinalizer(Env<GenContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   386
        if (code.isAlive() && env.info.finalize != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
            env.info.finalize.gen();
06bc494ca11e Initial load
duke
parents:
diff changeset
   388
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
    /** Generate code to call all finalizers of structures aborted by
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
     *  a non-local
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
     *  exit.  Return target environment of the non-local exit.
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
     *  @param target      The tree representing the structure that's aborted
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
     *  @param env         The environment current at the non-local exit.
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
    Env<GenContext> unwind(JCTree target, Env<GenContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
        Env<GenContext> env1 = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
        while (true) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
            genFinalizer(env1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
            if (env1.tree == target) break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
            env1 = env1.next;
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
        return env1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
    /** Mark end of gap in catch-all range for finalizer.
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
     *  @param env   the environment which might contain the finalizer
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
     *               (if it does, env.info.gaps != null).
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
    void endFinalizerGap(Env<GenContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
        if (env.info.gaps != null && env.info.gaps.length() % 2 == 1)
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
            env.info.gaps.append(code.curPc());
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
    /** Mark end of all gaps in catch-all ranges for finalizers of environments
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
     *  lying between, and including to two environments.
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
     *  @param from    the most deeply nested environment to mark
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
     *  @param to      the least deeply nested environment to mark
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
    void endFinalizerGaps(Env<GenContext> from, Env<GenContext> to) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
        Env<GenContext> last = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
        while (last != to) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
            endFinalizerGap(from);
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
            last = from;
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
            from = from.next;
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   428
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
    /** Do any of the structures aborted by a non-local exit have
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
     *  finalizers that require an empty stack?
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
     *  @param target      The tree representing the structure that's aborted
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
     *  @param env         The environment current at the non-local exit.
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
    boolean hasFinally(JCTree target, Env<GenContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
        while (env.tree != target) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
            if (env.tree.getTag() == JCTree.TRY && env.info.finalize.hasFinalizer())
06bc494ca11e Initial load
duke
parents:
diff changeset
   437
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
            env = env.next;
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
/* ************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
 * Normalizing class-members.
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
 *************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
06bc494ca11e Initial load
duke
parents:
diff changeset
   447
    /** Distribute member initializer code into constructors and <clinit>
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
     *  method.
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
     *  @param defs         The list of class member declarations.
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
     *  @param c            The enclosing class.
06bc494ca11e Initial load
duke
parents:
diff changeset
   451
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   452
    List<JCTree> normalizeDefs(List<JCTree> defs, ClassSymbol c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   453
        ListBuffer<JCStatement> initCode = new ListBuffer<JCStatement>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   454
        ListBuffer<JCStatement> clinitCode = new ListBuffer<JCStatement>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
        ListBuffer<JCTree> methodDefs = new ListBuffer<JCTree>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   456
        // Sort definitions into three listbuffers:
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
        //  - initCode for instance initializers
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
        //  - clinitCode for class initializers
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
        //  - methodDefs for method definitions
06bc494ca11e Initial load
duke
parents:
diff changeset
   460
        for (List<JCTree> l = defs; l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
            JCTree def = l.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
            switch (def.getTag()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
            case JCTree.BLOCK:
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
                JCBlock block = (JCBlock)def;
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
                if ((block.flags & STATIC) != 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   466
                    clinitCode.append(block);
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
   468
                    initCode.append(block);
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   470
            case JCTree.METHODDEF:
06bc494ca11e Initial load
duke
parents:
diff changeset
   471
                methodDefs.append(def);
06bc494ca11e Initial load
duke
parents:
diff changeset
   472
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   473
            case JCTree.VARDEF:
06bc494ca11e Initial load
duke
parents:
diff changeset
   474
                JCVariableDecl vdef = (JCVariableDecl) def;
06bc494ca11e Initial load
duke
parents:
diff changeset
   475
                VarSymbol sym = vdef.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   476
                checkDimension(vdef.pos(), sym.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   477
                if (vdef.init != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
                    if ((sym.flags() & STATIC) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
                        // Always initialize instance variables.
06bc494ca11e Initial load
duke
parents:
diff changeset
   480
                        JCStatement init = make.at(vdef.pos()).
06bc494ca11e Initial load
duke
parents:
diff changeset
   481
                            Assignment(sym, vdef.init);
06bc494ca11e Initial load
duke
parents:
diff changeset
   482
                        initCode.append(init);
06bc494ca11e Initial load
duke
parents:
diff changeset
   483
                        if (endPositions != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   484
                            Integer endPos = endPositions.remove(vdef);
06bc494ca11e Initial load
duke
parents:
diff changeset
   485
                            if (endPos != null) endPositions.put(init, endPos);
06bc494ca11e Initial load
duke
parents:
diff changeset
   486
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   487
                    } else if (sym.getConstValue() == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   488
                        // Initialize class (static) variables only if
06bc494ca11e Initial load
duke
parents:
diff changeset
   489
                        // they are not compile-time constants.
06bc494ca11e Initial load
duke
parents:
diff changeset
   490
                        JCStatement init = make.at(vdef.pos).
06bc494ca11e Initial load
duke
parents:
diff changeset
   491
                            Assignment(sym, vdef.init);
06bc494ca11e Initial load
duke
parents:
diff changeset
   492
                        clinitCode.append(init);
06bc494ca11e Initial load
duke
parents:
diff changeset
   493
                        if (endPositions != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   494
                            Integer endPos = endPositions.remove(vdef);
06bc494ca11e Initial load
duke
parents:
diff changeset
   495
                            if (endPos != null) endPositions.put(init, endPos);
06bc494ca11e Initial load
duke
parents:
diff changeset
   496
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   497
                    } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
                        checkStringConstant(vdef.init.pos(), sym.getConstValue());
06bc494ca11e Initial load
duke
parents:
diff changeset
   499
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   500
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   501
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
            default:
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 8031
diff changeset
   503
                Assert.error();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   505
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   506
        // Insert any instance initializers into all constructors.
06bc494ca11e Initial load
duke
parents:
diff changeset
   507
        if (initCode.length() != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   508
            List<JCStatement> inits = initCode.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
            for (JCTree t : methodDefs) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
                normalizeMethod((JCMethodDecl)t, inits);
06bc494ca11e Initial load
duke
parents:
diff changeset
   511
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   513
        // If there are class initializers, create a <clinit> method
06bc494ca11e Initial load
duke
parents:
diff changeset
   514
        // that contains them as its body.
06bc494ca11e Initial load
duke
parents:
diff changeset
   515
        if (clinitCode.length() != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   516
            MethodSymbol clinit = new MethodSymbol(
06bc494ca11e Initial load
duke
parents:
diff changeset
   517
                STATIC, names.clinit,
06bc494ca11e Initial load
duke
parents:
diff changeset
   518
                new MethodType(
06bc494ca11e Initial load
duke
parents:
diff changeset
   519
                    List.<Type>nil(), syms.voidType,
06bc494ca11e Initial load
duke
parents:
diff changeset
   520
                    List.<Type>nil(), syms.methodClass),
06bc494ca11e Initial load
duke
parents:
diff changeset
   521
                c);
06bc494ca11e Initial load
duke
parents:
diff changeset
   522
            c.members().enter(clinit);
06bc494ca11e Initial load
duke
parents:
diff changeset
   523
            List<JCStatement> clinitStats = clinitCode.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
   524
            JCBlock block = make.at(clinitStats.head.pos()).Block(0, clinitStats);
06bc494ca11e Initial load
duke
parents:
diff changeset
   525
            block.endpos = TreeInfo.endPos(clinitStats.last());
06bc494ca11e Initial load
duke
parents:
diff changeset
   526
            methodDefs.append(make.MethodDef(clinit, block));
06bc494ca11e Initial load
duke
parents:
diff changeset
   527
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   528
        // Return all method definitions.
06bc494ca11e Initial load
duke
parents:
diff changeset
   529
        return methodDefs.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
   530
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   531
06bc494ca11e Initial load
duke
parents:
diff changeset
   532
    /** Check a constant value and report if it is a string that is
06bc494ca11e Initial load
duke
parents:
diff changeset
   533
     *  too large.
06bc494ca11e Initial load
duke
parents:
diff changeset
   534
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   535
    private void checkStringConstant(DiagnosticPosition pos, Object constValue) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   536
        if (nerrs != 0 || // only complain about a long string once
06bc494ca11e Initial load
duke
parents:
diff changeset
   537
            constValue == null ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   538
            !(constValue instanceof String) ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   539
            ((String)constValue).length() < Pool.MAX_STRING_LENGTH)
06bc494ca11e Initial load
duke
parents:
diff changeset
   540
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   541
        log.error(pos, "limit.string");
06bc494ca11e Initial load
duke
parents:
diff changeset
   542
        nerrs++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   543
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   544
06bc494ca11e Initial load
duke
parents:
diff changeset
   545
    /** Insert instance initializer code into initial constructor.
06bc494ca11e Initial load
duke
parents:
diff changeset
   546
     *  @param md        The tree potentially representing a
06bc494ca11e Initial load
duke
parents:
diff changeset
   547
     *                   constructor's definition.
06bc494ca11e Initial load
duke
parents:
diff changeset
   548
     *  @param initCode  The list of instance initializer statements.
06bc494ca11e Initial load
duke
parents:
diff changeset
   549
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   550
    void normalizeMethod(JCMethodDecl md, List<JCStatement> initCode) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   551
        if (md.name == names.init && TreeInfo.isInitialConstructor(md)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   552
            // We are seeing a constructor that does not call another
06bc494ca11e Initial load
duke
parents:
diff changeset
   553
            // constructor of the same class.
06bc494ca11e Initial load
duke
parents:
diff changeset
   554
            List<JCStatement> stats = md.body.stats;
06bc494ca11e Initial load
duke
parents:
diff changeset
   555
            ListBuffer<JCStatement> newstats = new ListBuffer<JCStatement>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   556
06bc494ca11e Initial load
duke
parents:
diff changeset
   557
            if (stats.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   558
                // Copy initializers of synthetic variables generated in
06bc494ca11e Initial load
duke
parents:
diff changeset
   559
                // the translation of inner classes.
06bc494ca11e Initial load
duke
parents:
diff changeset
   560
                while (TreeInfo.isSyntheticInit(stats.head)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   561
                    newstats.append(stats.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   562
                    stats = stats.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   563
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   564
                // Copy superclass constructor call
06bc494ca11e Initial load
duke
parents:
diff changeset
   565
                newstats.append(stats.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   566
                stats = stats.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   567
                // Copy remaining synthetic initializers.
06bc494ca11e Initial load
duke
parents:
diff changeset
   568
                while (stats.nonEmpty() &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   569
                       TreeInfo.isSyntheticInit(stats.head)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   570
                    newstats.append(stats.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   571
                    stats = stats.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   572
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   573
                // Now insert the initializer code.
06bc494ca11e Initial load
duke
parents:
diff changeset
   574
                newstats.appendList(initCode);
06bc494ca11e Initial load
duke
parents:
diff changeset
   575
                // And copy all remaining statements.
06bc494ca11e Initial load
duke
parents:
diff changeset
   576
                while (stats.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   577
                    newstats.append(stats.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   578
                    stats = stats.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   579
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   580
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   581
            md.body.stats = newstats.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
   582
            if (md.body.endpos == Position.NOPOS)
06bc494ca11e Initial load
duke
parents:
diff changeset
   583
                md.body.endpos = TreeInfo.endPos(md.body.stats.last());
06bc494ca11e Initial load
duke
parents:
diff changeset
   584
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   585
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   586
06bc494ca11e Initial load
duke
parents:
diff changeset
   587
/* ********************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   588
 * Adding miranda methods
06bc494ca11e Initial load
duke
parents:
diff changeset
   589
 *********************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   590
06bc494ca11e Initial load
duke
parents:
diff changeset
   591
    /** Add abstract methods for all methods defined in one of
06bc494ca11e Initial load
duke
parents:
diff changeset
   592
     *  the interfaces of a given class,
06bc494ca11e Initial load
duke
parents:
diff changeset
   593
     *  provided they are not already implemented in the class.
06bc494ca11e Initial load
duke
parents:
diff changeset
   594
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   595
     *  @param c      The class whose interfaces are searched for methods
06bc494ca11e Initial load
duke
parents:
diff changeset
   596
     *                for which Miranda methods should be added.
06bc494ca11e Initial load
duke
parents:
diff changeset
   597
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   598
    void implementInterfaceMethods(ClassSymbol c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   599
        implementInterfaceMethods(c, c);
06bc494ca11e Initial load
duke
parents:
diff changeset
   600
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   601
06bc494ca11e Initial load
duke
parents:
diff changeset
   602
    /** Add abstract methods for all methods defined in one of
06bc494ca11e Initial load
duke
parents:
diff changeset
   603
     *  the interfaces of a given class,
06bc494ca11e Initial load
duke
parents:
diff changeset
   604
     *  provided they are not already implemented in the class.
06bc494ca11e Initial load
duke
parents:
diff changeset
   605
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   606
     *  @param c      The class whose interfaces are searched for methods
06bc494ca11e Initial load
duke
parents:
diff changeset
   607
     *                for which Miranda methods should be added.
06bc494ca11e Initial load
duke
parents:
diff changeset
   608
     *  @param site   The class in which a definition may be needed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   609
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   610
    void implementInterfaceMethods(ClassSymbol c, ClassSymbol site) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   611
        for (List<Type> l = types.interfaces(c.type); l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   612
            ClassSymbol i = (ClassSymbol)l.head.tsym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   613
            for (Scope.Entry e = i.members().elems;
06bc494ca11e Initial load
duke
parents:
diff changeset
   614
                 e != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   615
                 e = e.sibling)
06bc494ca11e Initial load
duke
parents:
diff changeset
   616
            {
06bc494ca11e Initial load
duke
parents:
diff changeset
   617
                if (e.sym.kind == MTH && (e.sym.flags() & STATIC) == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   618
                {
06bc494ca11e Initial load
duke
parents:
diff changeset
   619
                    MethodSymbol absMeth = (MethodSymbol)e.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   620
                    MethodSymbol implMeth = absMeth.binaryImplementation(site, types);
06bc494ca11e Initial load
duke
parents:
diff changeset
   621
                    if (implMeth == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   622
                        addAbstractMethod(site, absMeth);
06bc494ca11e Initial load
duke
parents:
diff changeset
   623
                    else if ((implMeth.flags() & IPROXY) != 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   624
                        adjustAbstractMethod(site, implMeth, absMeth);
06bc494ca11e Initial load
duke
parents:
diff changeset
   625
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   626
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   627
            implementInterfaceMethods(i, site);
06bc494ca11e Initial load
duke
parents:
diff changeset
   628
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   629
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   630
06bc494ca11e Initial load
duke
parents:
diff changeset
   631
    /** Add an abstract methods to a class
06bc494ca11e Initial load
duke
parents:
diff changeset
   632
     *  which implicitly implements a method defined in some interface
06bc494ca11e Initial load
duke
parents:
diff changeset
   633
     *  implemented by the class. These methods are called "Miranda methods".
06bc494ca11e Initial load
duke
parents:
diff changeset
   634
     *  Enter the newly created method into its enclosing class scope.
06bc494ca11e Initial load
duke
parents:
diff changeset
   635
     *  Note that it is not entered into the class tree, as the emitter
06bc494ca11e Initial load
duke
parents:
diff changeset
   636
     *  doesn't need to see it there to emit an abstract method.
06bc494ca11e Initial load
duke
parents:
diff changeset
   637
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   638
     *  @param c      The class to which the Miranda method is added.
06bc494ca11e Initial load
duke
parents:
diff changeset
   639
     *  @param m      The interface method symbol for which a Miranda method
06bc494ca11e Initial load
duke
parents:
diff changeset
   640
     *                is added.
06bc494ca11e Initial load
duke
parents:
diff changeset
   641
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   642
    private void addAbstractMethod(ClassSymbol c,
06bc494ca11e Initial load
duke
parents:
diff changeset
   643
                                   MethodSymbol m) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   644
        MethodSymbol absMeth = new MethodSymbol(
06bc494ca11e Initial load
duke
parents:
diff changeset
   645
            m.flags() | IPROXY | SYNTHETIC, m.name,
06bc494ca11e Initial load
duke
parents:
diff changeset
   646
            m.type, // was c.type.memberType(m), but now only !generics supported
06bc494ca11e Initial load
duke
parents:
diff changeset
   647
            c);
06bc494ca11e Initial load
duke
parents:
diff changeset
   648
        c.members().enter(absMeth); // add to symbol table
06bc494ca11e Initial load
duke
parents:
diff changeset
   649
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   650
06bc494ca11e Initial load
duke
parents:
diff changeset
   651
    private void adjustAbstractMethod(ClassSymbol c,
06bc494ca11e Initial load
duke
parents:
diff changeset
   652
                                      MethodSymbol pm,
06bc494ca11e Initial load
duke
parents:
diff changeset
   653
                                      MethodSymbol im) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   654
        MethodType pmt = (MethodType)pm.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
   655
        Type imt = types.memberType(c.type, im);
06bc494ca11e Initial load
duke
parents:
diff changeset
   656
        pmt.thrown = chk.intersect(pmt.getThrownTypes(), imt.getThrownTypes());
06bc494ca11e Initial load
duke
parents:
diff changeset
   657
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   658
06bc494ca11e Initial load
duke
parents:
diff changeset
   659
/* ************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   660
 * Traversal methods
06bc494ca11e Initial load
duke
parents:
diff changeset
   661
 *************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   662
06bc494ca11e Initial load
duke
parents:
diff changeset
   663
    /** Visitor argument: The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
   664
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   665
    Env<GenContext> env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   666
06bc494ca11e Initial load
duke
parents:
diff changeset
   667
    /** Visitor argument: The expected type (prototype).
06bc494ca11e Initial load
duke
parents:
diff changeset
   668
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   669
    Type pt;
06bc494ca11e Initial load
duke
parents:
diff changeset
   670
06bc494ca11e Initial load
duke
parents:
diff changeset
   671
    /** Visitor result: The item representing the computed value.
06bc494ca11e Initial load
duke
parents:
diff changeset
   672
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   673
    Item result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   674
06bc494ca11e Initial load
duke
parents:
diff changeset
   675
    /** Visitor method: generate code for a definition, catching and reporting
06bc494ca11e Initial load
duke
parents:
diff changeset
   676
     *  any completion failures.
06bc494ca11e Initial load
duke
parents:
diff changeset
   677
     *  @param tree    The definition to be visited.
06bc494ca11e Initial load
duke
parents:
diff changeset
   678
     *  @param env     The environment current at the definition.
06bc494ca11e Initial load
duke
parents:
diff changeset
   679
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   680
    public void genDef(JCTree tree, Env<GenContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   681
        Env<GenContext> prevEnv = this.env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   682
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   683
            this.env = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   684
            tree.accept(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   685
        } catch (CompletionFailure ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   686
            chk.completionError(tree.pos(), ex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   687
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   688
            this.env = prevEnv;
06bc494ca11e Initial load
duke
parents:
diff changeset
   689
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   690
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   691
06bc494ca11e Initial load
duke
parents:
diff changeset
   692
    /** Derived visitor method: check whether CharacterRangeTable
06bc494ca11e Initial load
duke
parents:
diff changeset
   693
     *  should be emitted, if so, put a new entry into CRTable
06bc494ca11e Initial load
duke
parents:
diff changeset
   694
     *  and call method to generate bytecode.
06bc494ca11e Initial load
duke
parents:
diff changeset
   695
     *  If not, just call method to generate bytecode.
06bc494ca11e Initial load
duke
parents:
diff changeset
   696
     *  @see    #genStat(Tree, Env)
06bc494ca11e Initial load
duke
parents:
diff changeset
   697
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   698
     *  @param  tree     The tree to be visited.
06bc494ca11e Initial load
duke
parents:
diff changeset
   699
     *  @param  env      The environment to use.
06bc494ca11e Initial load
duke
parents:
diff changeset
   700
     *  @param  crtFlags The CharacterRangeTable flags
06bc494ca11e Initial load
duke
parents:
diff changeset
   701
     *                   indicating type of the entry.
06bc494ca11e Initial load
duke
parents:
diff changeset
   702
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   703
    public void genStat(JCTree tree, Env<GenContext> env, int crtFlags) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   704
        if (!genCrt) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   705
            genStat(tree, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   706
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   707
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   708
        int startpc = code.curPc();
06bc494ca11e Initial load
duke
parents:
diff changeset
   709
        genStat(tree, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   710
        if (tree.getTag() == JCTree.BLOCK) crtFlags |= CRT_BLOCK;
06bc494ca11e Initial load
duke
parents:
diff changeset
   711
        code.crt.put(tree, crtFlags, startpc, code.curPc());
06bc494ca11e Initial load
duke
parents:
diff changeset
   712
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   713
06bc494ca11e Initial load
duke
parents:
diff changeset
   714
    /** Derived visitor method: generate code for a statement.
06bc494ca11e Initial load
duke
parents:
diff changeset
   715
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   716
    public void genStat(JCTree tree, Env<GenContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   717
        if (code.isAlive()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   718
            code.statBegin(tree.pos);
06bc494ca11e Initial load
duke
parents:
diff changeset
   719
            genDef(tree, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   720
        } else if (env.info.isSwitch && tree.getTag() == JCTree.VARDEF) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   721
            // variables whose declarations are in a switch
06bc494ca11e Initial load
duke
parents:
diff changeset
   722
            // can be used even if the decl is unreachable.
06bc494ca11e Initial load
duke
parents:
diff changeset
   723
            code.newLocal(((JCVariableDecl) tree).sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   724
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   725
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   726
06bc494ca11e Initial load
duke
parents:
diff changeset
   727
    /** Derived visitor method: check whether CharacterRangeTable
06bc494ca11e Initial load
duke
parents:
diff changeset
   728
     *  should be emitted, if so, put a new entry into CRTable
06bc494ca11e Initial load
duke
parents:
diff changeset
   729
     *  and call method to generate bytecode.
06bc494ca11e Initial load
duke
parents:
diff changeset
   730
     *  If not, just call method to generate bytecode.
06bc494ca11e Initial load
duke
parents:
diff changeset
   731
     *  @see    #genStats(List, Env)
06bc494ca11e Initial load
duke
parents:
diff changeset
   732
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   733
     *  @param  trees    The list of trees to be visited.
06bc494ca11e Initial load
duke
parents:
diff changeset
   734
     *  @param  env      The environment to use.
06bc494ca11e Initial load
duke
parents:
diff changeset
   735
     *  @param  crtFlags The CharacterRangeTable flags
06bc494ca11e Initial load
duke
parents:
diff changeset
   736
     *                   indicating type of the entry.
06bc494ca11e Initial load
duke
parents:
diff changeset
   737
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   738
    public void genStats(List<JCStatement> trees, Env<GenContext> env, int crtFlags) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   739
        if (!genCrt) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   740
            genStats(trees, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   741
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   742
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   743
        if (trees.length() == 1) {        // mark one statement with the flags
06bc494ca11e Initial load
duke
parents:
diff changeset
   744
            genStat(trees.head, env, crtFlags | CRT_STATEMENT);
06bc494ca11e Initial load
duke
parents:
diff changeset
   745
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   746
            int startpc = code.curPc();
06bc494ca11e Initial load
duke
parents:
diff changeset
   747
            genStats(trees, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   748
            code.crt.put(trees, crtFlags, startpc, code.curPc());
06bc494ca11e Initial load
duke
parents:
diff changeset
   749
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   750
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   751
06bc494ca11e Initial load
duke
parents:
diff changeset
   752
    /** Derived visitor method: generate code for a list of statements.
06bc494ca11e Initial load
duke
parents:
diff changeset
   753
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   754
    public void genStats(List<? extends JCTree> trees, Env<GenContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   755
        for (List<? extends JCTree> l = trees; l.nonEmpty(); l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
   756
            genStat(l.head, env, CRT_STATEMENT);
06bc494ca11e Initial load
duke
parents:
diff changeset
   757
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   758
06bc494ca11e Initial load
duke
parents:
diff changeset
   759
    /** Derived visitor method: check whether CharacterRangeTable
06bc494ca11e Initial load
duke
parents:
diff changeset
   760
     *  should be emitted, if so, put a new entry into CRTable
06bc494ca11e Initial load
duke
parents:
diff changeset
   761
     *  and call method to generate bytecode.
06bc494ca11e Initial load
duke
parents:
diff changeset
   762
     *  If not, just call method to generate bytecode.
06bc494ca11e Initial load
duke
parents:
diff changeset
   763
     *  @see    #genCond(Tree,boolean)
06bc494ca11e Initial load
duke
parents:
diff changeset
   764
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   765
     *  @param  tree     The tree to be visited.
06bc494ca11e Initial load
duke
parents:
diff changeset
   766
     *  @param  crtFlags The CharacterRangeTable flags
06bc494ca11e Initial load
duke
parents:
diff changeset
   767
     *                   indicating type of the entry.
06bc494ca11e Initial load
duke
parents:
diff changeset
   768
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   769
    public CondItem genCond(JCTree tree, int crtFlags) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   770
        if (!genCrt) return genCond(tree, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
   771
        int startpc = code.curPc();
06bc494ca11e Initial load
duke
parents:
diff changeset
   772
        CondItem item = genCond(tree, (crtFlags & CRT_FLOW_CONTROLLER) != 0);
06bc494ca11e Initial load
duke
parents:
diff changeset
   773
        code.crt.put(tree, crtFlags, startpc, code.curPc());
06bc494ca11e Initial load
duke
parents:
diff changeset
   774
        return item;
06bc494ca11e Initial load
duke
parents:
diff changeset
   775
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   776
06bc494ca11e Initial load
duke
parents:
diff changeset
   777
    /** Derived visitor method: generate code for a boolean
06bc494ca11e Initial load
duke
parents:
diff changeset
   778
     *  expression in a control-flow context.
06bc494ca11e Initial load
duke
parents:
diff changeset
   779
     *  @param _tree         The expression to be visited.
06bc494ca11e Initial load
duke
parents:
diff changeset
   780
     *  @param markBranches The flag to indicate that the condition is
06bc494ca11e Initial load
duke
parents:
diff changeset
   781
     *                      a flow controller so produced conditions
06bc494ca11e Initial load
duke
parents:
diff changeset
   782
     *                      should contain a proper tree to generate
06bc494ca11e Initial load
duke
parents:
diff changeset
   783
     *                      CharacterRangeTable branches for them.
06bc494ca11e Initial load
duke
parents:
diff changeset
   784
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   785
    public CondItem genCond(JCTree _tree, boolean markBranches) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   786
        JCTree inner_tree = TreeInfo.skipParens(_tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   787
        if (inner_tree.getTag() == JCTree.CONDEXPR) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   788
            JCConditional tree = (JCConditional)inner_tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   789
            CondItem cond = genCond(tree.cond, CRT_FLOW_CONTROLLER);
06bc494ca11e Initial load
duke
parents:
diff changeset
   790
            if (cond.isTrue()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   791
                code.resolve(cond.trueJumps);
06bc494ca11e Initial load
duke
parents:
diff changeset
   792
                CondItem result = genCond(tree.truepart, CRT_FLOW_TARGET);
06bc494ca11e Initial load
duke
parents:
diff changeset
   793
                if (markBranches) result.tree = tree.truepart;
06bc494ca11e Initial load
duke
parents:
diff changeset
   794
                return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   795
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   796
            if (cond.isFalse()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   797
                code.resolve(cond.falseJumps);
06bc494ca11e Initial load
duke
parents:
diff changeset
   798
                CondItem result = genCond(tree.falsepart, CRT_FLOW_TARGET);
06bc494ca11e Initial load
duke
parents:
diff changeset
   799
                if (markBranches) result.tree = tree.falsepart;
06bc494ca11e Initial load
duke
parents:
diff changeset
   800
                return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   801
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   802
            Chain secondJumps = cond.jumpFalse();
06bc494ca11e Initial load
duke
parents:
diff changeset
   803
            code.resolve(cond.trueJumps);
06bc494ca11e Initial load
duke
parents:
diff changeset
   804
            CondItem first = genCond(tree.truepart, CRT_FLOW_TARGET);
06bc494ca11e Initial load
duke
parents:
diff changeset
   805
            if (markBranches) first.tree = tree.truepart;
06bc494ca11e Initial load
duke
parents:
diff changeset
   806
            Chain falseJumps = first.jumpFalse();
06bc494ca11e Initial load
duke
parents:
diff changeset
   807
            code.resolve(first.trueJumps);
06bc494ca11e Initial load
duke
parents:
diff changeset
   808
            Chain trueJumps = code.branch(goto_);
06bc494ca11e Initial load
duke
parents:
diff changeset
   809
            code.resolve(secondJumps);
06bc494ca11e Initial load
duke
parents:
diff changeset
   810
            CondItem second = genCond(tree.falsepart, CRT_FLOW_TARGET);
06bc494ca11e Initial load
duke
parents:
diff changeset
   811
            CondItem result = items.makeCondItem(second.opcode,
5004
6c2694dc2da0 6929544: langtools source code uses statics qualified by instance variables
jjg
parents: 4875
diff changeset
   812
                                      Code.mergeChains(trueJumps, second.trueJumps),
6c2694dc2da0 6929544: langtools source code uses statics qualified by instance variables
jjg
parents: 4875
diff changeset
   813
                                      Code.mergeChains(falseJumps, second.falseJumps));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   814
            if (markBranches) result.tree = tree.falsepart;
06bc494ca11e Initial load
duke
parents:
diff changeset
   815
            return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   816
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   817
            CondItem result = genExpr(_tree, syms.booleanType).mkCond();
06bc494ca11e Initial load
duke
parents:
diff changeset
   818
            if (markBranches) result.tree = _tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   819
            return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   820
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   821
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   822
06bc494ca11e Initial load
duke
parents:
diff changeset
   823
    /** Visitor method: generate code for an expression, catching and reporting
06bc494ca11e Initial load
duke
parents:
diff changeset
   824
     *  any completion failures.
06bc494ca11e Initial load
duke
parents:
diff changeset
   825
     *  @param tree    The expression to be visited.
06bc494ca11e Initial load
duke
parents:
diff changeset
   826
     *  @param pt      The expression's expected type (proto-type).
06bc494ca11e Initial load
duke
parents:
diff changeset
   827
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   828
    public Item genExpr(JCTree tree, Type pt) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   829
        Type prevPt = this.pt;
06bc494ca11e Initial load
duke
parents:
diff changeset
   830
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   831
            if (tree.type.constValue() != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   832
                // Short circuit any expressions which are constants
06bc494ca11e Initial load
duke
parents:
diff changeset
   833
                checkStringConstant(tree.pos(), tree.type.constValue());
06bc494ca11e Initial load
duke
parents:
diff changeset
   834
                result = items.makeImmediateItem(tree.type, tree.type.constValue());
06bc494ca11e Initial load
duke
parents:
diff changeset
   835
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   836
                this.pt = pt;
06bc494ca11e Initial load
duke
parents:
diff changeset
   837
                tree.accept(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   838
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   839
            return result.coerce(pt);
06bc494ca11e Initial load
duke
parents:
diff changeset
   840
        } catch (CompletionFailure ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   841
            chk.completionError(tree.pos(), ex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   842
            code.state.stacksize = 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   843
            return items.makeStackItem(pt);
06bc494ca11e Initial load
duke
parents:
diff changeset
   844
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   845
            this.pt = prevPt;
06bc494ca11e Initial load
duke
parents:
diff changeset
   846
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   847
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   848
06bc494ca11e Initial load
duke
parents:
diff changeset
   849
    /** Derived visitor method: generate code for a list of method arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
   850
     *  @param trees    The argument expressions to be visited.
06bc494ca11e Initial load
duke
parents:
diff changeset
   851
     *  @param pts      The expression's expected types (i.e. the formal parameter
06bc494ca11e Initial load
duke
parents:
diff changeset
   852
     *                  types of the invoked method).
06bc494ca11e Initial load
duke
parents:
diff changeset
   853
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   854
    public void genArgs(List<JCExpression> trees, List<Type> pts) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   855
        for (List<JCExpression> l = trees; l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   856
            genExpr(l.head, pts.head).load();
06bc494ca11e Initial load
duke
parents:
diff changeset
   857
            pts = pts.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   858
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   859
        // require lists be of same length
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 8031
diff changeset
   860
        Assert.check(pts.isEmpty());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   861
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   862
06bc494ca11e Initial load
duke
parents:
diff changeset
   863
/* ************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   864
 * Visitor methods for statements and definitions
06bc494ca11e Initial load
duke
parents:
diff changeset
   865
 *************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   866
06bc494ca11e Initial load
duke
parents:
diff changeset
   867
    /** Thrown when the byte code size exceeds limit.
06bc494ca11e Initial load
duke
parents:
diff changeset
   868
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   869
    public static class CodeSizeOverflow extends RuntimeException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   870
        private static final long serialVersionUID = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   871
        public CodeSizeOverflow() {}
06bc494ca11e Initial load
duke
parents:
diff changeset
   872
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   873
06bc494ca11e Initial load
duke
parents:
diff changeset
   874
    public void visitMethodDef(JCMethodDecl tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   875
        // Create a new local environment that points pack at method
06bc494ca11e Initial load
duke
parents:
diff changeset
   876
        // definition.
06bc494ca11e Initial load
duke
parents:
diff changeset
   877
        Env<GenContext> localEnv = env.dup(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   878
        localEnv.enclMethod = tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   879
06bc494ca11e Initial load
duke
parents:
diff changeset
   880
        // The expected type of every return statement in this method
06bc494ca11e Initial load
duke
parents:
diff changeset
   881
        // is the method's return type.
06bc494ca11e Initial load
duke
parents:
diff changeset
   882
        this.pt = tree.sym.erasure(types).getReturnType();
06bc494ca11e Initial load
duke
parents:
diff changeset
   883
06bc494ca11e Initial load
duke
parents:
diff changeset
   884
        checkDimension(tree.pos(), tree.sym.erasure(types));
06bc494ca11e Initial load
duke
parents:
diff changeset
   885
        genMethod(tree, localEnv, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
   886
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   887
//where
06bc494ca11e Initial load
duke
parents:
diff changeset
   888
        /** Generate code for a method.
06bc494ca11e Initial load
duke
parents:
diff changeset
   889
         *  @param tree     The tree representing the method definition.
06bc494ca11e Initial load
duke
parents:
diff changeset
   890
         *  @param env      The environment current for the method body.
06bc494ca11e Initial load
duke
parents:
diff changeset
   891
         *  @param fatcode  A flag that indicates whether all jumps are
06bc494ca11e Initial load
duke
parents:
diff changeset
   892
         *                  within 32K.  We first invoke this method under
06bc494ca11e Initial load
duke
parents:
diff changeset
   893
         *                  the assumption that fatcode == false, i.e. all
06bc494ca11e Initial load
duke
parents:
diff changeset
   894
         *                  jumps are within 32K.  If this fails, fatcode
06bc494ca11e Initial load
duke
parents:
diff changeset
   895
         *                  is set to true and we try again.
06bc494ca11e Initial load
duke
parents:
diff changeset
   896
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   897
        void genMethod(JCMethodDecl tree, Env<GenContext> env, boolean fatcode) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   898
            MethodSymbol meth = tree.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   899
//      System.err.println("Generating " + meth + " in " + meth.owner); //DEBUG
06bc494ca11e Initial load
duke
parents:
diff changeset
   900
            if (Code.width(types.erasure(env.enclMethod.sym.type).getParameterTypes())  +
06bc494ca11e Initial load
duke
parents:
diff changeset
   901
                (((tree.mods.flags & STATIC) == 0 || meth.isConstructor()) ? 1 : 0) >
06bc494ca11e Initial load
duke
parents:
diff changeset
   902
                ClassFile.MAX_PARAMETERS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   903
                log.error(tree.pos(), "limit.parameters");
06bc494ca11e Initial load
duke
parents:
diff changeset
   904
                nerrs++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   905
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   906
06bc494ca11e Initial load
duke
parents:
diff changeset
   907
            else if (tree.body != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   908
                // Create a new code structure and initialize it.
06bc494ca11e Initial load
duke
parents:
diff changeset
   909
                int startpcCrt = initCode(tree, env, fatcode);
06bc494ca11e Initial load
duke
parents:
diff changeset
   910
06bc494ca11e Initial load
duke
parents:
diff changeset
   911
                try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   912
                    genStat(tree.body, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   913
                } catch (CodeSizeOverflow e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   914
                    // Failed due to code limit, try again with jsr/ret
06bc494ca11e Initial load
duke
parents:
diff changeset
   915
                    startpcCrt = initCode(tree, env, fatcode);
06bc494ca11e Initial load
duke
parents:
diff changeset
   916
                    genStat(tree.body, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   917
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   918
06bc494ca11e Initial load
duke
parents:
diff changeset
   919
                if (code.state.stacksize != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   920
                    log.error(tree.body.pos(), "stack.sim.error", tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   921
                    throw new AssertionError();
06bc494ca11e Initial load
duke
parents:
diff changeset
   922
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   923
06bc494ca11e Initial load
duke
parents:
diff changeset
   924
                // If last statement could complete normally, insert a
06bc494ca11e Initial load
duke
parents:
diff changeset
   925
                // return at the end.
06bc494ca11e Initial load
duke
parents:
diff changeset
   926
                if (code.isAlive()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   927
                    code.statBegin(TreeInfo.endPos(tree.body));
06bc494ca11e Initial load
duke
parents:
diff changeset
   928
                    if (env.enclMethod == null ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   929
                        env.enclMethod.sym.type.getReturnType().tag == VOID) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   930
                        code.emitop0(return_);
06bc494ca11e Initial load
duke
parents:
diff changeset
   931
                    } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   932
                        // sometime dead code seems alive (4415991);
06bc494ca11e Initial load
duke
parents:
diff changeset
   933
                        // generate a small loop instead
06bc494ca11e Initial load
duke
parents:
diff changeset
   934
                        int startpc = code.entryPoint();
06bc494ca11e Initial load
duke
parents:
diff changeset
   935
                        CondItem c = items.makeCondItem(goto_);
06bc494ca11e Initial load
duke
parents:
diff changeset
   936
                        code.resolve(c.jumpTrue(), startpc);
06bc494ca11e Initial load
duke
parents:
diff changeset
   937
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   938
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   939
                if (genCrt)
06bc494ca11e Initial load
duke
parents:
diff changeset
   940
                    code.crt.put(tree.body,
06bc494ca11e Initial load
duke
parents:
diff changeset
   941
                                 CRT_BLOCK,
06bc494ca11e Initial load
duke
parents:
diff changeset
   942
                                 startpcCrt,
06bc494ca11e Initial load
duke
parents:
diff changeset
   943
                                 code.curPc());
06bc494ca11e Initial load
duke
parents:
diff changeset
   944
06bc494ca11e Initial load
duke
parents:
diff changeset
   945
                code.endScopes(0);
06bc494ca11e Initial load
duke
parents:
diff changeset
   946
06bc494ca11e Initial load
duke
parents:
diff changeset
   947
                // If we exceeded limits, panic
06bc494ca11e Initial load
duke
parents:
diff changeset
   948
                if (code.checkLimits(tree.pos(), log)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   949
                    nerrs++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   950
                    return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   951
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   952
06bc494ca11e Initial load
duke
parents:
diff changeset
   953
                // If we generated short code but got a long jump, do it again
06bc494ca11e Initial load
duke
parents:
diff changeset
   954
                // with fatCode = true.
06bc494ca11e Initial load
duke
parents:
diff changeset
   955
                if (!fatcode && code.fatcode) genMethod(tree, env, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
   956
06bc494ca11e Initial load
duke
parents:
diff changeset
   957
                // Clean up
06bc494ca11e Initial load
duke
parents:
diff changeset
   958
                if(stackMap == StackMapFormat.JSR202) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   959
                    code.lastFrame = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   960
                    code.frameBeforeLast = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   961
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   962
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   963
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   964
06bc494ca11e Initial load
duke
parents:
diff changeset
   965
        private int initCode(JCMethodDecl tree, Env<GenContext> env, boolean fatcode) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   966
            MethodSymbol meth = tree.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   967
06bc494ca11e Initial load
duke
parents:
diff changeset
   968
            // Create a new code structure.
06bc494ca11e Initial load
duke
parents:
diff changeset
   969
            meth.code = code = new Code(meth,
06bc494ca11e Initial load
duke
parents:
diff changeset
   970
                                        fatcode,
06bc494ca11e Initial load
duke
parents:
diff changeset
   971
                                        lineDebugInfo ? toplevel.lineMap : null,
06bc494ca11e Initial load
duke
parents:
diff changeset
   972
                                        varDebugInfo,
06bc494ca11e Initial load
duke
parents:
diff changeset
   973
                                        stackMap,
06bc494ca11e Initial load
duke
parents:
diff changeset
   974
                                        debugCode,
06bc494ca11e Initial load
duke
parents:
diff changeset
   975
                                        genCrt ? new CRTable(tree, env.toplevel.endPositions)
06bc494ca11e Initial load
duke
parents:
diff changeset
   976
                                               : null,
06bc494ca11e Initial load
duke
parents:
diff changeset
   977
                                        syms,
06bc494ca11e Initial load
duke
parents:
diff changeset
   978
                                        types,
06bc494ca11e Initial load
duke
parents:
diff changeset
   979
                                        pool);
06bc494ca11e Initial load
duke
parents:
diff changeset
   980
            items = new Items(pool, code, syms, types);
06bc494ca11e Initial load
duke
parents:
diff changeset
   981
            if (code.debugCode)
06bc494ca11e Initial load
duke
parents:
diff changeset
   982
                System.err.println(meth + " for body " + tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   983
06bc494ca11e Initial load
duke
parents:
diff changeset
   984
            // If method is not static, create a new local variable address
06bc494ca11e Initial load
duke
parents:
diff changeset
   985
            // for `this'.
06bc494ca11e Initial load
duke
parents:
diff changeset
   986
            if ((tree.mods.flags & STATIC) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   987
                Type selfType = meth.owner.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
   988
                if (meth.isConstructor() && selfType != syms.objectType)
06bc494ca11e Initial load
duke
parents:
diff changeset
   989
                    selfType = UninitializedType.uninitializedThis(selfType);
06bc494ca11e Initial load
duke
parents:
diff changeset
   990
                code.setDefined(
06bc494ca11e Initial load
duke
parents:
diff changeset
   991
                        code.newLocal(
06bc494ca11e Initial load
duke
parents:
diff changeset
   992
                            new VarSymbol(FINAL, names._this, selfType, meth.owner)));
06bc494ca11e Initial load
duke
parents:
diff changeset
   993
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   994
06bc494ca11e Initial load
duke
parents:
diff changeset
   995
            // Mark all parameters as defined from the beginning of
06bc494ca11e Initial load
duke
parents:
diff changeset
   996
            // the method.
06bc494ca11e Initial load
duke
parents:
diff changeset
   997
            for (List<JCVariableDecl> l = tree.params; l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   998
                checkDimension(l.head.pos(), l.head.sym.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   999
                code.setDefined(code.newLocal(l.head.sym));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1000
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1001
06bc494ca11e Initial load
duke
parents:
diff changeset
  1002
            // Get ready to generate code for method body.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1003
            int startpcCrt = genCrt ? code.curPc() : 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1004
            code.entryPoint();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1005
06bc494ca11e Initial load
duke
parents:
diff changeset
  1006
            // Suppress initial stackmap
06bc494ca11e Initial load
duke
parents:
diff changeset
  1007
            code.pendingStackMap = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1008
06bc494ca11e Initial load
duke
parents:
diff changeset
  1009
            return startpcCrt;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1010
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1011
06bc494ca11e Initial load
duke
parents:
diff changeset
  1012
    public void visitVarDef(JCVariableDecl tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1013
        VarSymbol v = tree.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1014
        code.newLocal(v);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1015
        if (tree.init != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1016
            checkStringConstant(tree.init.pos(), v.getConstValue());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1017
            if (v.getConstValue() == null || varDebugInfo) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1018
                genExpr(tree.init, v.erasure(types)).load();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1019
                items.makeLocalItem(v).store();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1020
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1021
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1022
        checkDimension(tree.pos(), v.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1023
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1024
06bc494ca11e Initial load
duke
parents:
diff changeset
  1025
    public void visitSkip(JCSkip tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1026
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1027
06bc494ca11e Initial load
duke
parents:
diff changeset
  1028
    public void visitBlock(JCBlock tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1029
        int limit = code.nextreg;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1030
        Env<GenContext> localEnv = env.dup(tree, new GenContext());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1031
        genStats(tree.stats, localEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1032
        // End the scope of all block-local variables in variable info.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1033
        if (env.tree.getTag() != JCTree.METHODDEF) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1034
            code.statBegin(tree.endpos);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1035
            code.endScopes(limit);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1036
            code.pendingStatPos = Position.NOPOS;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1037
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1038
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1039
06bc494ca11e Initial load
duke
parents:
diff changeset
  1040
    public void visitDoLoop(JCDoWhileLoop tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1041
        genLoop(tree, tree.body, tree.cond, List.<JCExpressionStatement>nil(), false);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1042
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1043
06bc494ca11e Initial load
duke
parents:
diff changeset
  1044
    public void visitWhileLoop(JCWhileLoop tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1045
        genLoop(tree, tree.body, tree.cond, List.<JCExpressionStatement>nil(), true);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1046
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1047
06bc494ca11e Initial load
duke
parents:
diff changeset
  1048
    public void visitForLoop(JCForLoop tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1049
        int limit = code.nextreg;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1050
        genStats(tree.init, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1051
        genLoop(tree, tree.body, tree.cond, tree.step, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1052
        code.endScopes(limit);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1053
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1054
    //where
06bc494ca11e Initial load
duke
parents:
diff changeset
  1055
        /** Generate code for a loop.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1056
         *  @param loop       The tree representing the loop.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1057
         *  @param body       The loop's body.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1058
         *  @param cond       The loop's controling condition.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1059
         *  @param step       "Step" statements to be inserted at end of
06bc494ca11e Initial load
duke
parents:
diff changeset
  1060
         *                    each iteration.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1061
         *  @param testFirst  True if the loop test belongs before the body.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1062
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1063
        private void genLoop(JCStatement loop,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1064
                             JCStatement body,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1065
                             JCExpression cond,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1066
                             List<JCExpressionStatement> step,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1067
                             boolean testFirst) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1068
            Env<GenContext> loopEnv = env.dup(loop, new GenContext());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1069
            int startpc = code.entryPoint();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1070
            if (testFirst) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1071
                CondItem c;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1072
                if (cond != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1073
                    code.statBegin(cond.pos);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1074
                    c = genCond(TreeInfo.skipParens(cond), CRT_FLOW_CONTROLLER);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1075
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1076
                    c = items.makeCondItem(goto_);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1077
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1078
                Chain loopDone = c.jumpFalse();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1079
                code.resolve(c.trueJumps);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1080
                genStat(body, loopEnv, CRT_STATEMENT | CRT_FLOW_TARGET);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1081
                code.resolve(loopEnv.info.cont);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1082
                genStats(step, loopEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1083
                code.resolve(code.branch(goto_), startpc);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1084
                code.resolve(loopDone);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1085
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1086
                genStat(body, loopEnv, CRT_STATEMENT | CRT_FLOW_TARGET);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1087
                code.resolve(loopEnv.info.cont);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1088
                genStats(step, loopEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1089
                CondItem c;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1090
                if (cond != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1091
                    code.statBegin(cond.pos);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1092
                    c = genCond(TreeInfo.skipParens(cond), CRT_FLOW_CONTROLLER);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1093
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1094
                    c = items.makeCondItem(goto_);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1095
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1096
                code.resolve(c.jumpTrue(), startpc);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1097
                code.resolve(c.falseJumps);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1098
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1099
            code.resolve(loopEnv.info.exit);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1100
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1101
06bc494ca11e Initial load
duke
parents:
diff changeset
  1102
    public void visitForeachLoop(JCEnhancedForLoop tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1103
        throw new AssertionError(); // should have been removed by Lower.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1104
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1105
06bc494ca11e Initial load
duke
parents:
diff changeset
  1106
    public void visitLabelled(JCLabeledStatement tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1107
        Env<GenContext> localEnv = env.dup(tree, new GenContext());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1108
        genStat(tree.body, localEnv, CRT_STATEMENT);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1109
        code.resolve(localEnv.info.exit);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1110
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1111
06bc494ca11e Initial load
duke
parents:
diff changeset
  1112
    public void visitSwitch(JCSwitch tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1113
        int limit = code.nextreg;
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 8031
diff changeset
  1114
        Assert.check(tree.selector.type.tag != CLASS);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1115
        int startpcCrt = genCrt ? code.curPc() : 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1116
        Item sel = genExpr(tree.selector, syms.intType);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1117
        List<JCCase> cases = tree.cases;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1118
        if (cases.isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1119
            // We are seeing:  switch <sel> {}
06bc494ca11e Initial load
duke
parents:
diff changeset
  1120
            sel.load().drop();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1121
            if (genCrt)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1122
                code.crt.put(TreeInfo.skipParens(tree.selector),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1123
                             CRT_FLOW_CONTROLLER, startpcCrt, code.curPc());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1124
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1125
            // We are seeing a nonempty switch.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1126
            sel.load();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1127
            if (genCrt)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1128
                code.crt.put(TreeInfo.skipParens(tree.selector),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1129
                             CRT_FLOW_CONTROLLER, startpcCrt, code.curPc());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1130
            Env<GenContext> switchEnv = env.dup(tree, new GenContext());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1131
            switchEnv.info.isSwitch = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1132
06bc494ca11e Initial load
duke
parents:
diff changeset
  1133
            // Compute number of labels and minimum and maximum label values.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1134
            // For each case, store its label in an array.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1135
            int lo = Integer.MAX_VALUE;  // minimum label.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1136
            int hi = Integer.MIN_VALUE;  // maximum label.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1137
            int nlabels = 0;               // number of labels.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1138
06bc494ca11e Initial load
duke
parents:
diff changeset
  1139
            int[] labels = new int[cases.length()];  // the label array.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1140
            int defaultIndex = -1;     // the index of the default clause.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1141
06bc494ca11e Initial load
duke
parents:
diff changeset
  1142
            List<JCCase> l = cases;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1143
            for (int i = 0; i < labels.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1144
                if (l.head.pat != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1145
                    int val = ((Number)l.head.pat.type.constValue()).intValue();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1146
                    labels[i] = val;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1147
                    if (val < lo) lo = val;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1148
                    if (hi < val) hi = val;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1149
                    nlabels++;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1150
                } else {
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 8031
diff changeset
  1151
                    Assert.check(defaultIndex == -1);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1152
                    defaultIndex = i;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1153
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1154
                l = l.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1155
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1156
06bc494ca11e Initial load
duke
parents:
diff changeset
  1157
            // Determine whether to issue a tableswitch or a lookupswitch
06bc494ca11e Initial load
duke
parents:
diff changeset
  1158
            // instruction.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1159
            long table_space_cost = 4 + ((long) hi - lo + 1); // words
06bc494ca11e Initial load
duke
parents:
diff changeset
  1160
            long table_time_cost = 3; // comparisons
06bc494ca11e Initial load
duke
parents:
diff changeset
  1161
            long lookup_space_cost = 3 + 2 * (long) nlabels;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1162
            long lookup_time_cost = nlabels;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1163
            int opcode =
06bc494ca11e Initial load
duke
parents:
diff changeset
  1164
                nlabels > 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1165
                table_space_cost + 3 * table_time_cost <=
06bc494ca11e Initial load
duke
parents:
diff changeset
  1166
                lookup_space_cost + 3 * lookup_time_cost
06bc494ca11e Initial load
duke
parents:
diff changeset
  1167
                ?
06bc494ca11e Initial load
duke
parents:
diff changeset
  1168
                tableswitch : lookupswitch;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1169
06bc494ca11e Initial load
duke
parents:
diff changeset
  1170
            int startpc = code.curPc();    // the position of the selector operation
06bc494ca11e Initial load
duke
parents:
diff changeset
  1171
            code.emitop0(opcode);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1172
            code.align(4);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1173
            int tableBase = code.curPc();  // the start of the jump table
06bc494ca11e Initial load
duke
parents:
diff changeset
  1174
            int[] offsets = null;          // a table of offsets for a lookupswitch
06bc494ca11e Initial load
duke
parents:
diff changeset
  1175
            code.emit4(-1);                // leave space for default offset
06bc494ca11e Initial load
duke
parents:
diff changeset
  1176
            if (opcode == tableswitch) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1177
                code.emit4(lo);            // minimum label
06bc494ca11e Initial load
duke
parents:
diff changeset
  1178
                code.emit4(hi);            // maximum label
06bc494ca11e Initial load
duke
parents:
diff changeset
  1179
                for (long i = lo; i <= hi; i++) {  // leave space for jump table
06bc494ca11e Initial load
duke
parents:
diff changeset
  1180
                    code.emit4(-1);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1181
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1182
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1183
                code.emit4(nlabels);    // number of labels
06bc494ca11e Initial load
duke
parents:
diff changeset
  1184
                for (int i = 0; i < nlabels; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1185
                    code.emit4(-1); code.emit4(-1); // leave space for lookup table
06bc494ca11e Initial load
duke
parents:
diff changeset
  1186
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1187
                offsets = new int[labels.length];
06bc494ca11e Initial load
duke
parents:
diff changeset
  1188
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1189
            Code.State stateSwitch = code.state.dup();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1190
            code.markDead();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1191
06bc494ca11e Initial load
duke
parents:
diff changeset
  1192
            // For each case do:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1193
            l = cases;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1194
            for (int i = 0; i < labels.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1195
                JCCase c = l.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1196
                l = l.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1197
06bc494ca11e Initial load
duke
parents:
diff changeset
  1198
                int pc = code.entryPoint(stateSwitch);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1199
                // Insert offset directly into code or else into the
06bc494ca11e Initial load
duke
parents:
diff changeset
  1200
                // offsets table.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1201
                if (i != defaultIndex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1202
                    if (opcode == tableswitch) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1203
                        code.put4(
06bc494ca11e Initial load
duke
parents:
diff changeset
  1204
                            tableBase + 4 * (labels[i] - lo + 3),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1205
                            pc - startpc);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1206
                    } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1207
                        offsets[i] = pc - startpc;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1208
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1209
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1210
                    code.put4(tableBase, pc - startpc);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1211
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1212
06bc494ca11e Initial load
duke
parents:
diff changeset
  1213
                // Generate code for the statements in this case.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1214
                genStats(c.stats, switchEnv, CRT_FLOW_TARGET);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1215
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1216
06bc494ca11e Initial load
duke
parents:
diff changeset
  1217
            // Resolve all breaks.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1218
            code.resolve(switchEnv.info.exit);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1219
06bc494ca11e Initial load
duke
parents:
diff changeset
  1220
            // If we have not set the default offset, we do so now.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1221
            if (code.get4(tableBase) == -1) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1222
                code.put4(tableBase, code.entryPoint(stateSwitch) - startpc);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1223
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1224
06bc494ca11e Initial load
duke
parents:
diff changeset
  1225
            if (opcode == tableswitch) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1226
                // Let any unfilled slots point to the default case.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1227
                int defaultOffset = code.get4(tableBase);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1228
                for (long i = lo; i <= hi; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1229
                    int t = (int)(tableBase + 4 * (i - lo + 3));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1230
                    if (code.get4(t) == -1)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1231
                        code.put4(t, defaultOffset);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1232
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1233
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1234
                // Sort non-default offsets and copy into lookup table.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1235
                if (defaultIndex >= 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1236
                    for (int i = defaultIndex; i < labels.length - 1; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1237
                        labels[i] = labels[i+1];
06bc494ca11e Initial load
duke
parents:
diff changeset
  1238
                        offsets[i] = offsets[i+1];
06bc494ca11e Initial load
duke
parents:
diff changeset
  1239
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1240
                if (nlabels > 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1241
                    qsort2(labels, offsets, 0, nlabels - 1);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1242
                for (int i = 0; i < nlabels; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1243
                    int caseidx = tableBase + 8 * (i + 1);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1244
                    code.put4(caseidx, labels[i]);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1245
                    code.put4(caseidx + 4, offsets[i]);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1246
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1247
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1248
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1249
        code.endScopes(limit);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1250
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1251
//where
06bc494ca11e Initial load
duke
parents:
diff changeset
  1252
        /** Sort (int) arrays of keys and values
06bc494ca11e Initial load
duke
parents:
diff changeset
  1253
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1254
       static void qsort2(int[] keys, int[] values, int lo, int hi) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1255
            int i = lo;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1256
            int j = hi;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1257
            int pivot = keys[(i+j)/2];
06bc494ca11e Initial load
duke
parents:
diff changeset
  1258
            do {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1259
                while (keys[i] < pivot) i++;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1260
                while (pivot < keys[j]) j--;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1261
                if (i <= j) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1262
                    int temp1 = keys[i];
06bc494ca11e Initial load
duke
parents:
diff changeset
  1263
                    keys[i] = keys[j];
06bc494ca11e Initial load
duke
parents:
diff changeset
  1264
                    keys[j] = temp1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1265
                    int temp2 = values[i];
06bc494ca11e Initial load
duke
parents:
diff changeset
  1266
                    values[i] = values[j];
06bc494ca11e Initial load
duke
parents:
diff changeset
  1267
                    values[j] = temp2;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1268
                    i++;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1269
                    j--;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1270
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1271
            } while (i <= j);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1272
            if (lo < j) qsort2(keys, values, lo, j);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1273
            if (i < hi) qsort2(keys, values, i, hi);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1274
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1275
06bc494ca11e Initial load
duke
parents:
diff changeset
  1276
    public void visitSynchronized(JCSynchronized tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1277
        int limit = code.nextreg;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1278
        // Generate code to evaluate lock and save in temporary variable.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1279
        final LocalItem lockVar = makeTemp(syms.objectType);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1280
        genExpr(tree.lock, tree.lock.type).load().duplicate();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1281
        lockVar.store();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1282
06bc494ca11e Initial load
duke
parents:
diff changeset
  1283
        // Generate code to enter monitor.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1284
        code.emitop0(monitorenter);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1285
        code.state.lock(lockVar.reg);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1286
06bc494ca11e Initial load
duke
parents:
diff changeset
  1287
        // Generate code for a try statement with given body, no catch clauses
06bc494ca11e Initial load
duke
parents:
diff changeset
  1288
        // in a new environment with the "exit-monitor" operation as finalizer.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1289
        final Env<GenContext> syncEnv = env.dup(tree, new GenContext());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1290
        syncEnv.info.finalize = new GenFinalizer() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1291
            void gen() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1292
                genLast();
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 8031
diff changeset
  1293
                Assert.check(syncEnv.info.gaps.length() % 2 == 0);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1294
                syncEnv.info.gaps.append(code.curPc());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1295
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1296
            void genLast() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1297
                if (code.isAlive()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1298
                    lockVar.load();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1299
                    code.emitop0(monitorexit);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1300
                    code.state.unlock(lockVar.reg);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1301
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1302
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1303
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
  1304
        syncEnv.info.gaps = new ListBuffer<Integer>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1305
        genTry(tree.body, List.<JCCatch>nil(), syncEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1306
        code.endScopes(limit);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1307
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1308
06bc494ca11e Initial load
duke
parents:
diff changeset
  1309
    public void visitTry(final JCTry tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1310
        // Generate code for a try statement with given body and catch clauses,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1311
        // in a new environment which calls the finally block if there is one.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1312
        final Env<GenContext> tryEnv = env.dup(tree, new GenContext());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1313
        final Env<GenContext> oldEnv = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1314
        if (!useJsrLocally) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1315
            useJsrLocally =
06bc494ca11e Initial load
duke
parents:
diff changeset
  1316
                (stackMap == StackMapFormat.NONE) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1317
                (jsrlimit <= 0 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  1318
                jsrlimit < 100 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1319
                estimateCodeComplexity(tree.finalizer)>jsrlimit);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1320
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1321
        tryEnv.info.finalize = new GenFinalizer() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1322
            void gen() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1323
                if (useJsrLocally) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1324
                    if (tree.finalizer != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1325
                        Code.State jsrState = code.state.dup();
5004
6c2694dc2da0 6929544: langtools source code uses statics qualified by instance variables
jjg
parents: 4875
diff changeset
  1326
                        jsrState.push(Code.jsrReturnValue);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1327
                        tryEnv.info.cont =
06bc494ca11e Initial load
duke
parents:
diff changeset
  1328
                            new Chain(code.emitJump(jsr),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1329
                                      tryEnv.info.cont,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1330
                                      jsrState);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1331
                    }
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 8031
diff changeset
  1332
                    Assert.check(tryEnv.info.gaps.length() % 2 == 0);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1333
                    tryEnv.info.gaps.append(code.curPc());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1334
                } else {
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 8031
diff changeset
  1335
                    Assert.check(tryEnv.info.gaps.length() % 2 == 0);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1336
                    tryEnv.info.gaps.append(code.curPc());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1337
                    genLast();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1338
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1339
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1340
            void genLast() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1341
                if (tree.finalizer != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1342
                    genStat(tree.finalizer, oldEnv, CRT_BLOCK);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1343
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1344
            boolean hasFinalizer() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1345
                return tree.finalizer != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1346
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1347
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
  1348
        tryEnv.info.gaps = new ListBuffer<Integer>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1349
        genTry(tree.body, tree.catchers, tryEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1350
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1351
    //where
06bc494ca11e Initial load
duke
parents:
diff changeset
  1352
        /** Generate code for a try or synchronized statement
06bc494ca11e Initial load
duke
parents:
diff changeset
  1353
         *  @param body      The body of the try or synchronized statement.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1354
         *  @param catchers  The lis of catch clauses.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1355
         *  @param env       the environment current for the body.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1356
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1357
        void genTry(JCTree body, List<JCCatch> catchers, Env<GenContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1358
            int limit = code.nextreg;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1359
            int startpc = code.curPc();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1360
            Code.State stateTry = code.state.dup();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1361
            genStat(body, env, CRT_BLOCK);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1362
            int endpc = code.curPc();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1363
            boolean hasFinalizer =
06bc494ca11e Initial load
duke
parents:
diff changeset
  1364
                env.info.finalize != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1365
                env.info.finalize.hasFinalizer();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1366
            List<Integer> gaps = env.info.gaps.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1367
            code.statBegin(TreeInfo.endPos(body));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1368
            genFinalizer(env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1369
            code.statBegin(TreeInfo.endPos(env.tree));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1370
            Chain exitChain = code.branch(goto_);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1371
            endFinalizerGap(env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1372
            if (startpc != endpc) for (List<JCCatch> l = catchers; l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1373
                // start off with exception on stack
06bc494ca11e Initial load
duke
parents:
diff changeset
  1374
                code.entryPoint(stateTry, l.head.param.sym.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1375
                genCatch(l.head, env, startpc, endpc, gaps);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1376
                genFinalizer(env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1377
                if (hasFinalizer || l.tail.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1378
                    code.statBegin(TreeInfo.endPos(env.tree));
5004
6c2694dc2da0 6929544: langtools source code uses statics qualified by instance variables
jjg
parents: 4875
diff changeset
  1379
                    exitChain = Code.mergeChains(exitChain,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1380
                                                 code.branch(goto_));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1381
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1382
                endFinalizerGap(env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1383
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1384
            if (hasFinalizer) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1385
                // Create a new register segement to avoid allocating
06bc494ca11e Initial load
duke
parents:
diff changeset
  1386
                // the same variables in finalizers and other statements.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1387
                code.newRegSegment();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1388
06bc494ca11e Initial load
duke
parents:
diff changeset
  1389
                // Add a catch-all clause.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1390
06bc494ca11e Initial load
duke
parents:
diff changeset
  1391
                // start off with exception on stack
06bc494ca11e Initial load
duke
parents:
diff changeset
  1392
                int catchallpc = code.entryPoint(stateTry, syms.throwableType);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1393
06bc494ca11e Initial load
duke
parents:
diff changeset
  1394
                // Register all exception ranges for catch all clause.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1395
                // The range of the catch all clause is from the beginning
06bc494ca11e Initial load
duke
parents:
diff changeset
  1396
                // of the try or synchronized block until the present
06bc494ca11e Initial load
duke
parents:
diff changeset
  1397
                // code pointer excluding all gaps in the current
06bc494ca11e Initial load
duke
parents:
diff changeset
  1398
                // environment's GenContext.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1399
                int startseg = startpc;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1400
                while (env.info.gaps.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1401
                    int endseg = env.info.gaps.next().intValue();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1402
                    registerCatch(body.pos(), startseg, endseg,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1403
                                  catchallpc, 0);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1404
                    startseg = env.info.gaps.next().intValue();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1405
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1406
                code.statBegin(TreeInfo.finalizerPos(env.tree));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1407
                code.markStatBegin();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1408
06bc494ca11e Initial load
duke
parents:
diff changeset
  1409
                Item excVar = makeTemp(syms.throwableType);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1410
                excVar.store();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1411
                genFinalizer(env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1412
                excVar.load();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1413
                registerCatch(body.pos(), startseg,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1414
                              env.info.gaps.next().intValue(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1415
                              catchallpc, 0);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1416
                code.emitop0(athrow);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1417
                code.markDead();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1418
06bc494ca11e Initial load
duke
parents:
diff changeset
  1419
                // If there are jsr's to this finalizer, ...
06bc494ca11e Initial load
duke
parents:
diff changeset
  1420
                if (env.info.cont != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1421
                    // Resolve all jsr's.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1422
                    code.resolve(env.info.cont);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1423
06bc494ca11e Initial load
duke
parents:
diff changeset
  1424
                    // Mark statement line number
06bc494ca11e Initial load
duke
parents:
diff changeset
  1425
                    code.statBegin(TreeInfo.finalizerPos(env.tree));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1426
                    code.markStatBegin();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1427
06bc494ca11e Initial load
duke
parents:
diff changeset
  1428
                    // Save return address.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1429
                    LocalItem retVar = makeTemp(syms.throwableType);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1430
                    retVar.store();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1431
06bc494ca11e Initial load
duke
parents:
diff changeset
  1432
                    // Generate finalizer code.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1433
                    env.info.finalize.genLast();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1434
06bc494ca11e Initial load
duke
parents:
diff changeset
  1435
                    // Return.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1436
                    code.emitop1w(ret, retVar.reg);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1437
                    code.markDead();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1438
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1439
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1440
06bc494ca11e Initial load
duke
parents:
diff changeset
  1441
            // Resolve all breaks.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1442
            code.resolve(exitChain);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1443
06bc494ca11e Initial load
duke
parents:
diff changeset
  1444
            code.endScopes(limit);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1445
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1446
06bc494ca11e Initial load
duke
parents:
diff changeset
  1447
        /** Generate code for a catch clause.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1448
         *  @param tree     The catch clause.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1449
         *  @param env      The environment current in the enclosing try.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1450
         *  @param startpc  Start pc of try-block.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1451
         *  @param endpc    End pc of try-block.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1452
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1453
        void genCatch(JCCatch tree,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1454
                      Env<GenContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1455
                      int startpc, int endpc,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1456
                      List<Integer> gaps) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1457
            if (startpc != endpc) {
5492
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5004
diff changeset
  1458
                List<JCExpression> subClauses = TreeInfo.isMultiCatch(tree) ?
9300
c2de4dd9853b 7033809: Rename "disjunctive" to "union" in javax.lang.model
darcy
parents: 8036
diff changeset
  1459
                        ((JCTypeUnion)tree.param.vartype).alternatives :
6354
f50c012cd1f0 6978574: return statement in try block with multi-catch causes ClassFormatError
mcimadamore
parents: 5857
diff changeset
  1460
                        List.of(tree.param.vartype);
f50c012cd1f0 6978574: return statement in try block with multi-catch causes ClassFormatError
mcimadamore
parents: 5857
diff changeset
  1461
                while (gaps.nonEmpty()) {
f50c012cd1f0 6978574: return statement in try block with multi-catch causes ClassFormatError
mcimadamore
parents: 5857
diff changeset
  1462
                    for (JCExpression subCatch : subClauses) {
f50c012cd1f0 6978574: return statement in try block with multi-catch causes ClassFormatError
mcimadamore
parents: 5857
diff changeset
  1463
                        int catchType = makeRef(tree.pos(), subCatch.type);
f50c012cd1f0 6978574: return statement in try block with multi-catch causes ClassFormatError
mcimadamore
parents: 5857
diff changeset
  1464
                        int end = gaps.head.intValue();
5492
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5004
diff changeset
  1465
                        registerCatch(tree.pos(),
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5004
diff changeset
  1466
                                      startpc,  end, code.curPc(),
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5004
diff changeset
  1467
                                      catchType);
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5004
diff changeset
  1468
                    }
6354
f50c012cd1f0 6978574: return statement in try block with multi-catch causes ClassFormatError
mcimadamore
parents: 5857
diff changeset
  1469
                    gaps = gaps.tail;
f50c012cd1f0 6978574: return statement in try block with multi-catch causes ClassFormatError
mcimadamore
parents: 5857
diff changeset
  1470
                    startpc = gaps.head.intValue();
f50c012cd1f0 6978574: return statement in try block with multi-catch causes ClassFormatError
mcimadamore
parents: 5857
diff changeset
  1471
                    gaps = gaps.tail;
f50c012cd1f0 6978574: return statement in try block with multi-catch causes ClassFormatError
mcimadamore
parents: 5857
diff changeset
  1472
                }
f50c012cd1f0 6978574: return statement in try block with multi-catch causes ClassFormatError
mcimadamore
parents: 5857
diff changeset
  1473
                if (startpc < endpc) {
f50c012cd1f0 6978574: return statement in try block with multi-catch causes ClassFormatError
mcimadamore
parents: 5857
diff changeset
  1474
                    for (JCExpression subCatch : subClauses) {
f50c012cd1f0 6978574: return statement in try block with multi-catch causes ClassFormatError
mcimadamore
parents: 5857
diff changeset
  1475
                        int catchType = makeRef(tree.pos(), subCatch.type);
5492
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5004
diff changeset
  1476
                        registerCatch(tree.pos(),
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5004
diff changeset
  1477
                                      startpc, endpc, code.curPc(),
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5004
diff changeset
  1478
                                      catchType);
6354
f50c012cd1f0 6978574: return statement in try block with multi-catch causes ClassFormatError
mcimadamore
parents: 5857
diff changeset
  1479
                    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1480
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1481
                VarSymbol exparam = tree.param.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1482
                code.statBegin(tree.pos);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1483
                code.markStatBegin();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1484
                int limit = code.nextreg;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1485
                int exlocal = code.newLocal(exparam);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1486
                items.makeLocalItem(exparam).store();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1487
                code.statBegin(TreeInfo.firstStatPos(tree.body));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1488
                genStat(tree.body, env, CRT_BLOCK);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1489
                code.endScopes(limit);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1490
                code.statBegin(TreeInfo.endPos(tree.body));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1491
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1492
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1493
06bc494ca11e Initial load
duke
parents:
diff changeset
  1494
        /** Register a catch clause in the "Exceptions" code-attribute.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1495
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1496
        void registerCatch(DiagnosticPosition pos,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1497
                           int startpc, int endpc,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1498
                           int handler_pc, int catch_type) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1499
            if (startpc != endpc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1500
                char startpc1 = (char)startpc;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1501
                char endpc1 = (char)endpc;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1502
                char handler_pc1 = (char)handler_pc;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1503
                if (startpc1 == startpc &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1504
                    endpc1 == endpc &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1505
                    handler_pc1 == handler_pc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1506
                    code.addCatch(startpc1, endpc1, handler_pc1,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1507
                                  (char)catch_type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1508
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1509
                    if (!useJsrLocally && !target.generateStackMapTable()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1510
                        useJsrLocally = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1511
                        throw new CodeSizeOverflow();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1512
                    } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1513
                        log.error(pos, "limit.code.too.large.for.try.stmt");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1514
                        nerrs++;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1515
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1516
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1517
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1518
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1519
06bc494ca11e Initial load
duke
parents:
diff changeset
  1520
    /** Very roughly estimate the number of instructions needed for
06bc494ca11e Initial load
duke
parents:
diff changeset
  1521
     *  the given tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1522
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1523
    int estimateCodeComplexity(JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1524
        if (tree == null) return 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1525
        class ComplexityScanner extends TreeScanner {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1526
            int complexity = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1527
            public void scan(JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1528
                if (complexity > jsrlimit) return;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1529
                super.scan(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1530
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1531
            public void visitClassDef(JCClassDecl tree) {}
06bc494ca11e Initial load
duke
parents:
diff changeset
  1532
            public void visitDoLoop(JCDoWhileLoop tree)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1533
                { super.visitDoLoop(tree); complexity++; }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1534
            public void visitWhileLoop(JCWhileLoop tree)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1535
                { super.visitWhileLoop(tree); complexity++; }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1536
            public void visitForLoop(JCForLoop tree)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1537
                { super.visitForLoop(tree); complexity++; }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1538
            public void visitSwitch(JCSwitch tree)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1539
                { super.visitSwitch(tree); complexity+=5; }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1540
            public void visitCase(JCCase tree)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1541
                { super.visitCase(tree); complexity++; }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1542
            public void visitSynchronized(JCSynchronized tree)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1543
                { super.visitSynchronized(tree); complexity+=6; }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1544
            public void visitTry(JCTry tree)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1545
                { super.visitTry(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1546
                  if (tree.finalizer != null) complexity+=6; }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1547
            public void visitCatch(JCCatch tree)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1548
                { super.visitCatch(tree); complexity+=2; }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1549
            public void visitConditional(JCConditional tree)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1550
                { super.visitConditional(tree); complexity+=2; }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1551
            public void visitIf(JCIf tree)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1552
                { super.visitIf(tree); complexity+=2; }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1553
            // note: for break, continue, and return we don't take unwind() into account.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1554
            public void visitBreak(JCBreak tree)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1555
                { super.visitBreak(tree); complexity+=1; }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1556
            public void visitContinue(JCContinue tree)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1557
                { super.visitContinue(tree); complexity+=1; }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1558
            public void visitReturn(JCReturn tree)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1559
                { super.visitReturn(tree); complexity+=1; }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1560
            public void visitThrow(JCThrow tree)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1561
                { super.visitThrow(tree); complexity+=1; }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1562
            public void visitAssert(JCAssert tree)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1563
                { super.visitAssert(tree); complexity+=5; }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1564
            public void visitApply(JCMethodInvocation tree)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1565
                { super.visitApply(tree); complexity+=2; }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1566
            public void visitNewClass(JCNewClass tree)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1567
                { scan(tree.encl); scan(tree.args); complexity+=2; }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1568
            public void visitNewArray(JCNewArray tree)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1569
                { super.visitNewArray(tree); complexity+=5; }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1570
            public void visitAssign(JCAssign tree)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1571
                { super.visitAssign(tree); complexity+=1; }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1572
            public void visitAssignop(JCAssignOp tree)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1573
                { super.visitAssignop(tree); complexity+=2; }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1574
            public void visitUnary(JCUnary tree)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1575
                { complexity+=1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1576
                  if (tree.type.constValue() == null) super.visitUnary(tree); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1577
            public void visitBinary(JCBinary tree)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1578
                { complexity+=1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1579
                  if (tree.type.constValue() == null) super.visitBinary(tree); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1580
            public void visitTypeTest(JCInstanceOf tree)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1581
                { super.visitTypeTest(tree); complexity+=1; }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1582
            public void visitIndexed(JCArrayAccess tree)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1583
                { super.visitIndexed(tree); complexity+=1; }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1584
            public void visitSelect(JCFieldAccess tree)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1585
                { super.visitSelect(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1586
                  if (tree.sym.kind == VAR) complexity+=1; }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1587
            public void visitIdent(JCIdent tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1588
                if (tree.sym.kind == VAR) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1589
                    complexity+=1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1590
                    if (tree.type.constValue() == null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1591
                        tree.sym.owner.kind == TYP)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1592
                        complexity+=1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1593
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1594
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1595
            public void visitLiteral(JCLiteral tree)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1596
                { complexity+=1; }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1597
            public void visitTree(JCTree tree) {}
06bc494ca11e Initial load
duke
parents:
diff changeset
  1598
            public void visitWildcard(JCWildcard tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1599
                throw new AssertionError(this.getClass().getName());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1600
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1601
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1602
        ComplexityScanner scanner = new ComplexityScanner();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1603
        tree.accept(scanner);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1604
        return scanner.complexity;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1605
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1606
06bc494ca11e Initial load
duke
parents:
diff changeset
  1607
    public void visitIf(JCIf tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1608
        int limit = code.nextreg;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1609
        Chain thenExit = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1610
        CondItem c = genCond(TreeInfo.skipParens(tree.cond),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1611
                             CRT_FLOW_CONTROLLER);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1612
        Chain elseChain = c.jumpFalse();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1613
        if (!c.isFalse()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1614
            code.resolve(c.trueJumps);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1615
            genStat(tree.thenpart, env, CRT_STATEMENT | CRT_FLOW_TARGET);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1616
            thenExit = code.branch(goto_);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1617
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1618
        if (elseChain != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1619
            code.resolve(elseChain);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1620
            if (tree.elsepart != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1621
                genStat(tree.elsepart, env,CRT_STATEMENT | CRT_FLOW_TARGET);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1622
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1623
        code.resolve(thenExit);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1624
        code.endScopes(limit);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1625
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1626
06bc494ca11e Initial load
duke
parents:
diff changeset
  1627
    public void visitExec(JCExpressionStatement tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1628
        // Optimize x++ to ++x and x-- to --x.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1629
        JCExpression e = tree.expr;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1630
        switch (e.getTag()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1631
            case JCTree.POSTINC:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1632
                ((JCUnary) e).setTag(JCTree.PREINC);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1633
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1634
            case JCTree.POSTDEC:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1635
                ((JCUnary) e).setTag(JCTree.PREDEC);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1636
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1637
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1638
        genExpr(tree.expr, tree.expr.type).drop();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1639
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1640
06bc494ca11e Initial load
duke
parents:
diff changeset
  1641
    public void visitBreak(JCBreak tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1642
        Env<GenContext> targetEnv = unwind(tree.target, env);
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 8031
diff changeset
  1643
        Assert.check(code.state.stacksize == 0);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1644
        targetEnv.info.addExit(code.branch(goto_));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1645
        endFinalizerGaps(env, targetEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1646
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1647
06bc494ca11e Initial load
duke
parents:
diff changeset
  1648
    public void visitContinue(JCContinue tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1649
        Env<GenContext> targetEnv = unwind(tree.target, env);
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 8031
diff changeset
  1650
        Assert.check(code.state.stacksize == 0);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1651
        targetEnv.info.addCont(code.branch(goto_));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1652
        endFinalizerGaps(env, targetEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1653
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1654
06bc494ca11e Initial load
duke
parents:
diff changeset
  1655
    public void visitReturn(JCReturn tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1656
        int limit = code.nextreg;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1657
        final Env<GenContext> targetEnv;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1658
        if (tree.expr != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1659
            Item r = genExpr(tree.expr, pt).load();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1660
            if (hasFinally(env.enclMethod, env)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1661
                r = makeTemp(pt);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1662
                r.store();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1663
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1664
            targetEnv = unwind(env.enclMethod, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1665
            r.load();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1666
            code.emitop0(ireturn + Code.truncate(Code.typecode(pt)));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1667
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1668
            targetEnv = unwind(env.enclMethod, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1669
            code.emitop0(return_);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1670
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1671
        endFinalizerGaps(env, targetEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1672
        code.endScopes(limit);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1673
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1674
06bc494ca11e Initial load
duke
parents:
diff changeset
  1675
    public void visitThrow(JCThrow tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1676
        genExpr(tree.expr, tree.expr.type).load();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1677
        code.emitop0(athrow);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1678
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1679
06bc494ca11e Initial load
duke
parents:
diff changeset
  1680
/* ************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
  1681
 * Visitor methods for expressions
06bc494ca11e Initial load
duke
parents:
diff changeset
  1682
 *************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
  1683
06bc494ca11e Initial load
duke
parents:
diff changeset
  1684
    public void visitApply(JCMethodInvocation tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1685
        // Generate code for method.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1686
        Item m = genExpr(tree.meth, methodType);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1687
        // Generate code for all arguments, where the expected types are
06bc494ca11e Initial load
duke
parents:
diff changeset
  1688
        // the parameters of the method's external type (that is, any implicit
06bc494ca11e Initial load
duke
parents:
diff changeset
  1689
        // outer instance of a super(...) call appears as first parameter).
06bc494ca11e Initial load
duke
parents:
diff changeset
  1690
        genArgs(tree.args,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1691
                TreeInfo.symbol(tree.meth).externalType(types).getParameterTypes());
10457
644ffe54b5c7 7024096: Stack trace has invalid line numbers
ksrini
parents: 9303
diff changeset
  1692
        code.statBegin(tree.pos);
644ffe54b5c7 7024096: Stack trace has invalid line numbers
ksrini
parents: 9303
diff changeset
  1693
        code.markStatBegin();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1694
        result = m.invoke();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1695
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1696
06bc494ca11e Initial load
duke
parents:
diff changeset
  1697
    public void visitConditional(JCConditional tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1698
        Chain thenExit = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1699
        CondItem c = genCond(tree.cond, CRT_FLOW_CONTROLLER);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1700
        Chain elseChain = c.jumpFalse();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1701
        if (!c.isFalse()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1702
            code.resolve(c.trueJumps);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1703
            int startpc = genCrt ? code.curPc() : 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1704
            genExpr(tree.truepart, pt).load();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1705
            code.state.forceStackTop(tree.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1706
            if (genCrt) code.crt.put(tree.truepart, CRT_FLOW_TARGET,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1707
                                     startpc, code.curPc());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1708
            thenExit = code.branch(goto_);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1709
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1710
        if (elseChain != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1711
            code.resolve(elseChain);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1712
            int startpc = genCrt ? code.curPc() : 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1713
            genExpr(tree.falsepart, pt).load();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1714
            code.state.forceStackTop(tree.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1715
            if (genCrt) code.crt.put(tree.falsepart, CRT_FLOW_TARGET,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1716
                                     startpc, code.curPc());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1717
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1718
        code.resolve(thenExit);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1719
        result = items.makeStackItem(pt);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1720
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1721
06bc494ca11e Initial load
duke
parents:
diff changeset
  1722
    public void visitNewClass(JCNewClass tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1723
        // Enclosing instances or anonymous classes should have been eliminated
06bc494ca11e Initial load
duke
parents:
diff changeset
  1724
        // by now.
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 8031
diff changeset
  1725
        Assert.check(tree.encl == null && tree.def == null);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1726
06bc494ca11e Initial load
duke
parents:
diff changeset
  1727
        code.emitop2(new_, makeRef(tree.pos(), tree.type));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1728
        code.emitop0(dup);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1729
06bc494ca11e Initial load
duke
parents:
diff changeset
  1730
        // Generate code for all arguments, where the expected types are
06bc494ca11e Initial load
duke
parents:
diff changeset
  1731
        // the parameters of the constructor's external type (that is,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1732
        // any implicit outer instance appears as first parameter).
06bc494ca11e Initial load
duke
parents:
diff changeset
  1733
        genArgs(tree.args, tree.constructor.externalType(types).getParameterTypes());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1734
06bc494ca11e Initial load
duke
parents:
diff changeset
  1735
        items.makeMemberItem(tree.constructor, true).invoke();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1736
        result = items.makeStackItem(tree.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1737
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1738
06bc494ca11e Initial load
duke
parents:
diff changeset
  1739
    public void visitNewArray(JCNewArray tree) {
3149
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 2723
diff changeset
  1740
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1741
        if (tree.elems != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1742
            Type elemtype = types.elemtype(tree.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1743
            loadIntConst(tree.elems.length());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1744
            Item arr = makeNewArray(tree.pos(), tree.type, 1);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1745
            int i = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1746
            for (List<JCExpression> l = tree.elems; l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1747
                arr.duplicate();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1748
                loadIntConst(i);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1749
                i++;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1750
                genExpr(l.head, elemtype).load();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1751
                items.makeIndexedItem(elemtype).store();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1752
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1753
            result = arr;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1754
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1755
            for (List<JCExpression> l = tree.dims; l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1756
                genExpr(l.head, syms.intType).load();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1757
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1758
            result = makeNewArray(tree.pos(), tree.type, tree.dims.length());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1759
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1760
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1761
//where
06bc494ca11e Initial load
duke
parents:
diff changeset
  1762
        /** Generate code to create an array with given element type and number
06bc494ca11e Initial load
duke
parents:
diff changeset
  1763
         *  of dimensions.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1764
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1765
        Item makeNewArray(DiagnosticPosition pos, Type type, int ndims) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1766
            Type elemtype = types.elemtype(type);
7630
850c97ee59f2 4917091: javac rejects array over 128 in length
jjg
parents: 7074
diff changeset
  1767
            if (types.dimensions(type) > ClassFile.MAX_DIMENSIONS) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1768
                log.error(pos, "limit.dimensions");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1769
                nerrs++;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1770
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1771
            int elemcode = Code.arraycode(elemtype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1772
            if (elemcode == 0 || (elemcode == 1 && ndims == 1)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1773
                code.emitAnewarray(makeRef(pos, elemtype), type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1774
            } else if (elemcode == 1) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1775
                code.emitMultianewarray(ndims, makeRef(pos, type), type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1776
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1777
                code.emitNewarray(elemcode, type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1778
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1779
            return items.makeStackItem(type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1780
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1781
06bc494ca11e Initial load
duke
parents:
diff changeset
  1782
    public void visitParens(JCParens tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1783
        result = genExpr(tree.expr, tree.expr.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1784
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1785
06bc494ca11e Initial load
duke
parents:
diff changeset
  1786
    public void visitAssign(JCAssign tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1787
        Item l = genExpr(tree.lhs, tree.lhs.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1788
        genExpr(tree.rhs, tree.lhs.type).load();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1789
        result = items.makeAssignItem(l);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1790
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1791
06bc494ca11e Initial load
duke
parents:
diff changeset
  1792
    public void visitAssignop(JCAssignOp tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1793
        OperatorSymbol operator = (OperatorSymbol) tree.operator;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1794
        Item l;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1795
        if (operator.opcode == string_add) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1796
            // Generate code to make a string buffer
06bc494ca11e Initial load
duke
parents:
diff changeset
  1797
            makeStringBuffer(tree.pos());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1798
06bc494ca11e Initial load
duke
parents:
diff changeset
  1799
            // Generate code for first string, possibly save one
06bc494ca11e Initial load
duke
parents:
diff changeset
  1800
            // copy under buffer
06bc494ca11e Initial load
duke
parents:
diff changeset
  1801
            l = genExpr(tree.lhs, tree.lhs.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1802
            if (l.width() > 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1803
                code.emitop0(dup_x1 + 3 * (l.width() - 1));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1804
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1805
06bc494ca11e Initial load
duke
parents:
diff changeset
  1806
            // Load first string and append to buffer.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1807
            l.load();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1808
            appendString(tree.lhs);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1809
06bc494ca11e Initial load
duke
parents:
diff changeset
  1810
            // Append all other strings to buffer.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1811
            appendStrings(tree.rhs);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1812
06bc494ca11e Initial load
duke
parents:
diff changeset
  1813
            // Convert buffer to string.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1814
            bufferToString(tree.pos());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1815
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1816
            // Generate code for first expression
06bc494ca11e Initial load
duke
parents:
diff changeset
  1817
            l = genExpr(tree.lhs, tree.lhs.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1818
06bc494ca11e Initial load
duke
parents:
diff changeset
  1819
            // If we have an increment of -32768 to +32767 of a local
06bc494ca11e Initial load
duke
parents:
diff changeset
  1820
            // int variable we can use an incr instruction instead of
06bc494ca11e Initial load
duke
parents:
diff changeset
  1821
            // proceeding further.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1822
            if ((tree.getTag() == JCTree.PLUS_ASG || tree.getTag() == JCTree.MINUS_ASG) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1823
                l instanceof LocalItem &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1824
                tree.lhs.type.tag <= INT &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1825
                tree.rhs.type.tag <= INT &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1826
                tree.rhs.type.constValue() != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1827
                int ival = ((Number) tree.rhs.type.constValue()).intValue();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1828
                if (tree.getTag() == JCTree.MINUS_ASG) ival = -ival;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1829
                ((LocalItem)l).incr(ival);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1830
                result = l;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1831
                return;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1832
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1833
            // Otherwise, duplicate expression, load one copy
06bc494ca11e Initial load
duke
parents:
diff changeset
  1834
            // and complete binary operation.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1835
            l.duplicate();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1836
            l.coerce(operator.type.getParameterTypes().head).load();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1837
            completeBinop(tree.lhs, tree.rhs, operator).coerce(tree.lhs.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1838
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1839
        result = items.makeAssignItem(l);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1840
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1841
06bc494ca11e Initial load
duke
parents:
diff changeset
  1842
    public void visitUnary(JCUnary tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1843
        OperatorSymbol operator = (OperatorSymbol)tree.operator;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1844
        if (tree.getTag() == JCTree.NOT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1845
            CondItem od = genCond(tree.arg, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1846
            result = od.negate();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1847
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1848
            Item od = genExpr(tree.arg, operator.type.getParameterTypes().head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1849
            switch (tree.getTag()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1850
            case JCTree.POS:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1851
                result = od.load();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1852
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1853
            case JCTree.NEG:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1854
                result = od.load();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1855
                code.emitop0(operator.opcode);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1856
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1857
            case JCTree.COMPL:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1858
                result = od.load();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1859
                emitMinusOne(od.typecode);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1860
                code.emitop0(operator.opcode);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1861
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1862
            case JCTree.PREINC: case JCTree.PREDEC:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1863
                od.duplicate();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1864
                if (od instanceof LocalItem &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1865
                    (operator.opcode == iadd || operator.opcode == isub)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1866
                    ((LocalItem)od).incr(tree.getTag() == JCTree.PREINC ? 1 : -1);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1867
                    result = od;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1868
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1869
                    od.load();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1870
                    code.emitop0(one(od.typecode));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1871
                    code.emitop0(operator.opcode);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1872
                    // Perform narrowing primitive conversion if byte,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1873
                    // char, or short.  Fix for 4304655.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1874
                    if (od.typecode != INTcode &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1875
                        Code.truncate(od.typecode) == INTcode)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1876
                      code.emitop0(int2byte + od.typecode - BYTEcode);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1877
                    result = items.makeAssignItem(od);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1878
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1879
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1880
            case JCTree.POSTINC: case JCTree.POSTDEC:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1881
                od.duplicate();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1882
                if (od instanceof LocalItem &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1883
                    (operator.opcode == iadd || operator.opcode == isub)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1884
                    Item res = od.load();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1885
                    ((LocalItem)od).incr(tree.getTag() == JCTree.POSTINC ? 1 : -1);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1886
                    result = res;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1887
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1888
                    Item res = od.load();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1889
                    od.stash(od.typecode);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1890
                    code.emitop0(one(od.typecode));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1891
                    code.emitop0(operator.opcode);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1892
                    // Perform narrowing primitive conversion if byte,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1893
                    // char, or short.  Fix for 4304655.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1894
                    if (od.typecode != INTcode &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1895
                        Code.truncate(od.typecode) == INTcode)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1896
                      code.emitop0(int2byte + od.typecode - BYTEcode);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1897
                    od.store();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1898
                    result = res;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1899
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1900
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1901
            case JCTree.NULLCHK:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1902
                result = od.load();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1903
                code.emitop0(dup);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1904
                genNullCheck(tree.pos());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1905
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1906
            default:
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 8031
diff changeset
  1907
                Assert.error();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1908
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1909
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1910
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1911
06bc494ca11e Initial load
duke
parents:
diff changeset
  1912
    /** Generate a null check from the object value at stack top. */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1913
    private void genNullCheck(DiagnosticPosition pos) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1914
        callMethod(pos, syms.objectType, names.getClass,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1915
                   List.<Type>nil(), false);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1916
        code.emitop0(pop);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1917
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1918
06bc494ca11e Initial load
duke
parents:
diff changeset
  1919
    public void visitBinary(JCBinary tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1920
        OperatorSymbol operator = (OperatorSymbol)tree.operator;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1921
        if (operator.opcode == string_add) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1922
            // Create a string buffer.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1923
            makeStringBuffer(tree.pos());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1924
            // Append all strings to buffer.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1925
            appendStrings(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1926
            // Convert buffer to string.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1927
            bufferToString(tree.pos());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1928
            result = items.makeStackItem(syms.stringType);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1929
        } else if (tree.getTag() == JCTree.AND) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1930
            CondItem lcond = genCond(tree.lhs, CRT_FLOW_CONTROLLER);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1931
            if (!lcond.isFalse()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1932
                Chain falseJumps = lcond.jumpFalse();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1933
                code.resolve(lcond.trueJumps);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1934
                CondItem rcond = genCond(tree.rhs, CRT_FLOW_TARGET);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1935
                result = items.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1936
                    makeCondItem(rcond.opcode,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1937
                                 rcond.trueJumps,
5004
6c2694dc2da0 6929544: langtools source code uses statics qualified by instance variables
jjg
parents: 4875
diff changeset
  1938
                                 Code.mergeChains(falseJumps,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1939
                                                  rcond.falseJumps));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1940
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1941
                result = lcond;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1942
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1943
        } else if (tree.getTag() == JCTree.OR) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1944
            CondItem lcond = genCond(tree.lhs, CRT_FLOW_CONTROLLER);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1945
            if (!lcond.isTrue()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1946
                Chain trueJumps = lcond.jumpTrue();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1947
                code.resolve(lcond.falseJumps);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1948
                CondItem rcond = genCond(tree.rhs, CRT_FLOW_TARGET);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1949
                result = items.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1950
                    makeCondItem(rcond.opcode,
5004
6c2694dc2da0 6929544: langtools source code uses statics qualified by instance variables
jjg
parents: 4875
diff changeset
  1951
                                 Code.mergeChains(trueJumps, rcond.trueJumps),
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1952
                                 rcond.falseJumps);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1953
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1954
                result = lcond;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1955
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1956
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1957
            Item od = genExpr(tree.lhs, operator.type.getParameterTypes().head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1958
            od.load();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1959
            result = completeBinop(tree.lhs, tree.rhs, operator);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1960
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1961
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1962
//where
06bc494ca11e Initial load
duke
parents:
diff changeset
  1963
        /** Make a new string buffer.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1964
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1965
        void makeStringBuffer(DiagnosticPosition pos) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1966
            code.emitop2(new_, makeRef(pos, stringBufferType));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1967
            code.emitop0(dup);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1968
            callMethod(
06bc494ca11e Initial load
duke
parents:
diff changeset
  1969
                pos, stringBufferType, names.init, List.<Type>nil(), false);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1970
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1971
06bc494ca11e Initial load
duke
parents:
diff changeset
  1972
        /** Append value (on tos) to string buffer (on tos - 1).
06bc494ca11e Initial load
duke
parents:
diff changeset
  1973
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1974
        void appendString(JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1975
            Type t = tree.type.baseType();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1976
            if (t.tag > lastBaseTag && t.tsym != syms.stringType.tsym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1977
                t = syms.objectType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1978
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1979
            items.makeMemberItem(getStringBufferAppend(tree, t), false).invoke();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1980
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1981
        Symbol getStringBufferAppend(JCTree tree, Type t) {
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 8031
diff changeset
  1982
            Assert.checkNull(t.constValue());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1983
            Symbol method = stringBufferAppend.get(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1984
            if (method == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1985
                method = rs.resolveInternalMethod(tree.pos(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1986
                                                  attrEnv,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1987
                                                  stringBufferType,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1988
                                                  names.append,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1989
                                                  List.of(t),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1990
                                                  null);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1991
                stringBufferAppend.put(t, method);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1992
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1993
            return method;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1994
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1995
06bc494ca11e Initial load
duke
parents:
diff changeset
  1996
        /** Add all strings in tree to string buffer.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1997
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1998
        void appendStrings(JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1999
            tree = TreeInfo.skipParens(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2000
            if (tree.getTag() == JCTree.PLUS && tree.type.constValue() == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2001
                JCBinary op = (JCBinary) tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2002
                if (op.operator.kind == MTH &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2003
                    ((OperatorSymbol) op.operator).opcode == string_add) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2004
                    appendStrings(op.lhs);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2005
                    appendStrings(op.rhs);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2006
                    return;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2007
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2008
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2009
            genExpr(tree, tree.type).load();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2010
            appendString(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2011
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2012
06bc494ca11e Initial load
duke
parents:
diff changeset
  2013
        /** Convert string buffer on tos to string.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2014
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2015
        void bufferToString(DiagnosticPosition pos) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2016
            callMethod(
06bc494ca11e Initial load
duke
parents:
diff changeset
  2017
                pos,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2018
                stringBufferType,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2019
                names.toString,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2020
                List.<Type>nil(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  2021
                false);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2022
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2023
06bc494ca11e Initial load
duke
parents:
diff changeset
  2024
        /** Complete generating code for operation, with left operand
06bc494ca11e Initial load
duke
parents:
diff changeset
  2025
         *  already on stack.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2026
         *  @param lhs       The tree representing the left operand.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2027
         *  @param rhs       The tree representing the right operand.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2028
         *  @param operator  The operator symbol.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2029
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2030
        Item completeBinop(JCTree lhs, JCTree rhs, OperatorSymbol operator) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2031
            MethodType optype = (MethodType)operator.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2032
            int opcode = operator.opcode;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2033
            if (opcode >= if_icmpeq && opcode <= if_icmple &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2034
                rhs.type.constValue() instanceof Number &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2035
                ((Number) rhs.type.constValue()).intValue() == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2036
                opcode = opcode + (ifeq - if_icmpeq);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2037
            } else if (opcode >= if_acmpeq && opcode <= if_acmpne &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2038
                       TreeInfo.isNull(rhs)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2039
                opcode = opcode + (if_acmp_null - if_acmpeq);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2040
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2041
                // The expected type of the right operand is
06bc494ca11e Initial load
duke
parents:
diff changeset
  2042
                // the second parameter type of the operator, except for
06bc494ca11e Initial load
duke
parents:
diff changeset
  2043
                // shifts with long shiftcount, where we convert the opcode
06bc494ca11e Initial load
duke
parents:
diff changeset
  2044
                // to a short shift and the expected type to int.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2045
                Type rtype = operator.erasure(types).getParameterTypes().tail.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2046
                if (opcode >= ishll && opcode <= lushrl) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2047
                    opcode = opcode + (ishl - ishll);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2048
                    rtype = syms.intType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2049
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2050
                // Generate code for right operand and load.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2051
                genExpr(rhs, rtype).load();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2052
                // If there are two consecutive opcode instructions,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2053
                // emit the first now.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2054
                if (opcode >= (1 << preShift)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2055
                    code.emitop0(opcode >> preShift);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2056
                    opcode = opcode & 0xFF;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2057
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2058
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2059
            if (opcode >= ifeq && opcode <= if_acmpne ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  2060
                opcode == if_acmp_null || opcode == if_acmp_nonnull) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2061
                return items.makeCondItem(opcode);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2062
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2063
                code.emitop0(opcode);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2064
                return items.makeStackItem(optype.restype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2065
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2066
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2067
06bc494ca11e Initial load
duke
parents:
diff changeset
  2068
    public void visitTypeCast(JCTypeCast tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2069
        result = genExpr(tree.expr, tree.clazz.type).load();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2070
        // Additional code is only needed if we cast to a reference type
06bc494ca11e Initial load
duke
parents:
diff changeset
  2071
        // which is not statically a supertype of the expression's type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2072
        // For basic types, the coerce(...) in genExpr(...) will do
06bc494ca11e Initial load
duke
parents:
diff changeset
  2073
        // the conversion.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2074
        if (tree.clazz.type.tag > lastBaseTag &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2075
            types.asSuper(tree.expr.type, tree.clazz.type.tsym) == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2076
            code.emitop2(checkcast, makeRef(tree.pos(), tree.clazz.type));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2077
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2078
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2079
06bc494ca11e Initial load
duke
parents:
diff changeset
  2080
    public void visitWildcard(JCWildcard tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2081
        throw new AssertionError(this.getClass().getName());
06bc494ca11e Initial load
duke
parents:
diff changeset
  2082
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2083
06bc494ca11e Initial load
duke
parents:
diff changeset
  2084
    public void visitTypeTest(JCInstanceOf tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2085
        genExpr(tree.expr, tree.expr.type).load();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2086
        code.emitop2(instanceof_, makeRef(tree.pos(), tree.clazz.type));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2087
        result = items.makeStackItem(syms.booleanType);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2088
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2089
06bc494ca11e Initial load
duke
parents:
diff changeset
  2090
    public void visitIndexed(JCArrayAccess tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2091
        genExpr(tree.indexed, tree.indexed.type).load();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2092
        genExpr(tree.index, syms.intType).load();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2093
        result = items.makeIndexedItem(tree.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2094
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2095
06bc494ca11e Initial load
duke
parents:
diff changeset
  2096
    public void visitIdent(JCIdent tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2097
        Symbol sym = tree.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2098
        if (tree.name == names._this || tree.name == names._super) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2099
            Item res = tree.name == names._this
06bc494ca11e Initial load
duke
parents:
diff changeset
  2100
                ? items.makeThisItem()
06bc494ca11e Initial load
duke
parents:
diff changeset
  2101
                : items.makeSuperItem();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2102
            if (sym.kind == MTH) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2103
                // Generate code to address the constructor.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2104
                res.load();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2105
                res = items.makeMemberItem(sym, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2106
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2107
            result = res;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2108
        } else if (sym.kind == VAR && sym.owner.kind == MTH) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2109
            result = items.makeLocalItem((VarSymbol)sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2110
        } else if ((sym.flags() & STATIC) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2111
            if (!isAccessSuper(env.enclMethod))
06bc494ca11e Initial load
duke
parents:
diff changeset
  2112
                sym = binaryQualifier(sym, env.enclClass.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2113
            result = items.makeStaticItem(sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2114
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2115
            items.makeThisItem().load();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2116
            sym = binaryQualifier(sym, env.enclClass.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2117
            result = items.makeMemberItem(sym, (sym.flags() & PRIVATE) != 0);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2118
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2119
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2120
06bc494ca11e Initial load
duke
parents:
diff changeset
  2121
    public void visitSelect(JCFieldAccess tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2122
        Symbol sym = tree.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2123
06bc494ca11e Initial load
duke
parents:
diff changeset
  2124
        if (tree.name == names._class) {
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 8031
diff changeset
  2125
            Assert.check(target.hasClassLiterals());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2126
            code.emitop2(ldc2, makeRef(tree.pos(), tree.selected.type));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2127
            result = items.makeStackItem(pt);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2128
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2129
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2130
06bc494ca11e Initial load
duke
parents:
diff changeset
  2131
        Symbol ssym = TreeInfo.symbol(tree.selected);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2132
06bc494ca11e Initial load
duke
parents:
diff changeset
  2133
        // Are we selecting via super?
06bc494ca11e Initial load
duke
parents:
diff changeset
  2134
        boolean selectSuper =
06bc494ca11e Initial load
duke
parents:
diff changeset
  2135
            ssym != null && (ssym.kind == TYP || ssym.name == names._super);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2136
06bc494ca11e Initial load
duke
parents:
diff changeset
  2137
        // Are we accessing a member of the superclass in an access method
06bc494ca11e Initial load
duke
parents:
diff changeset
  2138
        // resulting from a qualified super?
06bc494ca11e Initial load
duke
parents:
diff changeset
  2139
        boolean accessSuper = isAccessSuper(env.enclMethod);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2140
06bc494ca11e Initial load
duke
parents:
diff changeset
  2141
        Item base = (selectSuper)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2142
            ? items.makeSuperItem()
06bc494ca11e Initial load
duke
parents:
diff changeset
  2143
            : genExpr(tree.selected, tree.selected.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2144
06bc494ca11e Initial load
duke
parents:
diff changeset
  2145
        if (sym.kind == VAR && ((VarSymbol) sym).getConstValue() != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2146
            // We are seeing a variable that is constant but its selecting
06bc494ca11e Initial load
duke
parents:
diff changeset
  2147
            // expression is not.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2148
            if ((sym.flags() & STATIC) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2149
                if (!selectSuper && (ssym == null || ssym.kind != TYP))
06bc494ca11e Initial load
duke
parents:
diff changeset
  2150
                    base = base.load();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2151
                base.drop();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2152
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2153
                base.load();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2154
                genNullCheck(tree.selected.pos());
06bc494ca11e Initial load
duke
parents:
diff changeset
  2155
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2156
            result = items.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2157
                makeImmediateItem(sym.type, ((VarSymbol) sym).getConstValue());
06bc494ca11e Initial load
duke
parents:
diff changeset
  2158
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2159
            if (!accessSuper)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2160
                sym = binaryQualifier(sym, tree.selected.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2161
            if ((sym.flags() & STATIC) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2162
                if (!selectSuper && (ssym == null || ssym.kind != TYP))
06bc494ca11e Initial load
duke
parents:
diff changeset
  2163
                    base = base.load();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2164
                base.drop();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2165
                result = items.makeStaticItem(sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2166
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2167
                base.load();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2168
                if (sym == syms.lengthVar) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2169
                    code.emitop0(arraylength);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2170
                    result = items.makeStackItem(syms.intType);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2171
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2172
                    result = items.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2173
                        makeMemberItem(sym,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2174
                                       (sym.flags() & PRIVATE) != 0 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  2175
                                       selectSuper || accessSuper);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2176
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2177
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2178
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2179
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2180
06bc494ca11e Initial load
duke
parents:
diff changeset
  2181
    public void visitLiteral(JCLiteral tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2182
        if (tree.type.tag == TypeTags.BOT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2183
            code.emitop0(aconst_null);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2184
            if (types.dimensions(pt) > 1) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2185
                code.emitop2(checkcast, makeRef(tree.pos(), pt));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2186
                result = items.makeStackItem(pt);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2187
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2188
                result = items.makeStackItem(tree.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2189
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2190
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2191
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
  2192
            result = items.makeImmediateItem(tree.type, tree.value);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2193
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2194
06bc494ca11e Initial load
duke
parents:
diff changeset
  2195
    public void visitLetExpr(LetExpr tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2196
        int limit = code.nextreg;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2197
        genStats(tree.defs, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2198
        result = genExpr(tree.expr, tree.expr.type).load();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2199
        code.endScopes(limit);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2200
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2201
06bc494ca11e Initial load
duke
parents:
diff changeset
  2202
/* ************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
  2203
 * main method
06bc494ca11e Initial load
duke
parents:
diff changeset
  2204
 *************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
  2205
06bc494ca11e Initial load
duke
parents:
diff changeset
  2206
    /** Generate code for a class definition.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2207
     *  @param env   The attribution environment that belongs to the
06bc494ca11e Initial load
duke
parents:
diff changeset
  2208
     *               outermost class containing this class definition.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2209
     *               We need this for resolving some additional symbols.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2210
     *  @param cdef  The tree representing the class definition.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2211
     *  @return      True if code is generated with no errors.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2212
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2213
    public boolean genClass(Env<AttrContext> env, JCClassDecl cdef) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2214
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2215
            attrEnv = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2216
            ClassSymbol c = cdef.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2217
            this.toplevel = env.toplevel;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2218
            this.endPositions = toplevel.endPositions;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2219
            // If this is a class definition requiring Miranda methods,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2220
            // add them.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2221
            if (generateIproxies &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2222
                (c.flags() & (INTERFACE|ABSTRACT)) == ABSTRACT
06bc494ca11e Initial load
duke
parents:
diff changeset
  2223
                && !allowGenerics // no Miranda methods available with generics
06bc494ca11e Initial load
duke
parents:
diff changeset
  2224
                )
06bc494ca11e Initial load
duke
parents:
diff changeset
  2225
                implementInterfaceMethods(c);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2226
            cdef.defs = normalizeDefs(cdef.defs, c);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2227
            c.pool = pool;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2228
            pool.reset();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2229
            Env<GenContext> localEnv =
06bc494ca11e Initial load
duke
parents:
diff changeset
  2230
                new Env<GenContext>(cdef, new GenContext());
06bc494ca11e Initial load
duke
parents:
diff changeset
  2231
            localEnv.toplevel = env.toplevel;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2232
            localEnv.enclClass = cdef;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2233
            for (List<JCTree> l = cdef.defs; l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2234
                genDef(l.head, localEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2235
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2236
            if (pool.numEntries() > Pool.MAX_ENTRIES) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2237
                log.error(cdef.pos(), "limit.pool");
06bc494ca11e Initial load
duke
parents:
diff changeset
  2238
                nerrs++;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2239
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2240
            if (nerrs != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2241
                // if errors, discard code
06bc494ca11e Initial load
duke
parents:
diff changeset
  2242
                for (List<JCTree> l = cdef.defs; l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2243
                    if (l.head.getTag() == JCTree.METHODDEF)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2244
                        ((JCMethodDecl) l.head).sym.code = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2245
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2246
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2247
            cdef.defs = List.nil(); // discard trees
06bc494ca11e Initial load
duke
parents:
diff changeset
  2248
            return nerrs == 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2249
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2250
            // note: this method does NOT support recursion.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2251
            attrEnv = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2252
            this.env = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2253
            toplevel = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2254
            endPositions = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2255
            nerrs = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2256
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2257
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2258
06bc494ca11e Initial load
duke
parents:
diff changeset
  2259
/* ************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
  2260
 * Auxiliary classes
06bc494ca11e Initial load
duke
parents:
diff changeset
  2261
 *************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
  2262
06bc494ca11e Initial load
duke
parents:
diff changeset
  2263
    /** An abstract class for finalizer generation.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2264
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2265
    abstract class GenFinalizer {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2266
        /** Generate code to clean up when unwinding. */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2267
        abstract void gen();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2268
06bc494ca11e Initial load
duke
parents:
diff changeset
  2269
        /** Generate code to clean up at last. */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2270
        abstract void genLast();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2271
06bc494ca11e Initial load
duke
parents:
diff changeset
  2272
        /** Does this finalizer have some nontrivial cleanup to perform? */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2273
        boolean hasFinalizer() { return true; }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2274
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2275
06bc494ca11e Initial load
duke
parents:
diff changeset
  2276
    /** code generation contexts,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2277
     *  to be used as type parameter for environments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2278
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2279
    static class GenContext {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2280
06bc494ca11e Initial load
duke
parents:
diff changeset
  2281
        /** A chain for all unresolved jumps that exit the current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2282
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2283
        Chain exit = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2284
06bc494ca11e Initial load
duke
parents:
diff changeset
  2285
        /** A chain for all unresolved jumps that continue in the
06bc494ca11e Initial load
duke
parents:
diff changeset
  2286
         *  current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2287
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2288
        Chain cont = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2289
06bc494ca11e Initial load
duke
parents:
diff changeset
  2290
        /** A closure that generates the finalizer of the current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2291
         *  Only set for Synchronized and Try contexts.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2292
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2293
        GenFinalizer finalize = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2294
06bc494ca11e Initial load
duke
parents:
diff changeset
  2295
        /** Is this a switch statement?  If so, allocate registers
06bc494ca11e Initial load
duke
parents:
diff changeset
  2296
         * even when the variable declaration is unreachable.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2297
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2298
        boolean isSwitch = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2299
06bc494ca11e Initial load
duke
parents:
diff changeset
  2300
        /** A list buffer containing all gaps in the finalizer range,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2301
         *  where a catch all exception should not apply.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2302
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2303
        ListBuffer<Integer> gaps = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2304
06bc494ca11e Initial load
duke
parents:
diff changeset
  2305
        /** Add given chain to exit chain.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2306
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2307
        void addExit(Chain c)  {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2308
            exit = Code.mergeChains(c, exit);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2309
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2310
06bc494ca11e Initial load
duke
parents:
diff changeset
  2311
        /** Add given chain to cont chain.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2312
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2313
        void addCont(Chain c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2314
            cont = Code.mergeChains(c, cont);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2315
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2316
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2317
}