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