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