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