1579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson/* 2579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * Copyright (C) 2011 The Android Open Source Project 3579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * 4579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * Licensed under the Apache License, Version 2.0 (the "License"); 5579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * you may not use this file except in compliance with the License. 6579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * You may obtain a copy of the License at 7579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * 8579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * http://www.apache.org/licenses/LICENSE-2.0 9579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * 10579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * Unless required by applicable law or agreed to in writing, software 11579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS, 12579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * See the License for the specific language governing permissions and 14579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * limitations under the License. 15579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson */ 16579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 17579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonpackage com.google.dexmaker; 18579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 19579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport com.android.dx.rop.code.BasicBlockList; 20579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport com.android.dx.rop.code.Insn; 21579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport com.android.dx.rop.code.PlainCstInsn; 22579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport com.android.dx.rop.code.PlainInsn; 23579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport com.android.dx.rop.code.RegisterSpecList; 24579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport com.android.dx.rop.code.Rop; 25579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport static com.android.dx.rop.code.Rop.BRANCH_GOTO; 26579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport static com.android.dx.rop.code.Rop.BRANCH_NONE; 27579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport static com.android.dx.rop.code.Rop.BRANCH_RETURN; 28579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport com.android.dx.rop.code.Rops; 29579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport com.android.dx.rop.code.SourcePosition; 30579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport com.android.dx.rop.code.ThrowingCstInsn; 31579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport com.android.dx.rop.code.ThrowingInsn; 32579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport com.android.dx.rop.cst.CstInteger; 33579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport com.android.dx.rop.type.StdTypeList; 34579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport static com.android.dx.rop.type.Type.BT_BYTE; 35579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport static com.android.dx.rop.type.Type.BT_CHAR; 36579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport static com.android.dx.rop.type.Type.BT_INT; 37579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport static com.android.dx.rop.type.Type.BT_SHORT; 38579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport java.util.ArrayList; 39579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport java.util.Collections; 40579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport java.util.Iterator; 41579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport java.util.List; 42579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 43579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson/** 44579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * Builds a sequence of instructions. 45d6c77efc0b187577dd7956070adfc7c335f65698Jesse Wilson * 46d6c77efc0b187577dd7956070adfc7c335f65698Jesse Wilson * <h3>Locals</h3> 47d6c77efc0b187577dd7956070adfc7c335f65698Jesse Wilson * All data manipulation takes place in local variables. Each parameter gets its 483e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * own local by default; access these using {@link #getParameter 493e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * getParameter()}. Non-static methods and constructors also have a {@code this} 503e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * parameter; it's available as {@link #getThis getThis()}. Allocate a new local 513e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * variable using {@link #newLocal newLocal()}, and assign a default value to it 523e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * with {@link #loadConstant loadConstant()}. Copy a value from one local to 533e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * another with {@link #move move()}. 543e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * 553e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * <p>Every local variable has a fixed type. This is either a primitive type (of 563e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * any size) or a reference type. This class emits instructions appropriate to 573e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * the types they operate on. Not all operations are local on all types; 583e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * attempting to emit such an operation will fail with an unchecked exception. 59d6c77efc0b187577dd7956070adfc7c335f65698Jesse Wilson * 60d6c77efc0b187577dd7956070adfc7c335f65698Jesse Wilson * <h3>Math and Bit Operations</h3> 61d6c77efc0b187577dd7956070adfc7c335f65698Jesse Wilson * Transform a single value into another related value using {@link 623e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * #op(UnaryOp,Local,Local) op(UnaryOp, Local, Local)}. Transform two values 633e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * into a third value using {@link #op(BinaryOp,Local,Local,Local) op(BinaryOp, 643e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Local, Local, Local)}. In either overload the first {@code Local} parameter 653e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * is where the result will be sent; the other {@code Local} parameters are the 663e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * inputs. 67d6c77efc0b187577dd7956070adfc7c335f65698Jesse Wilson * 68ff561a27def418f8c19a36df5fec727dfc8bb17aJesse Wilson * <h3>Comparisons</h3> 69d6c77efc0b187577dd7956070adfc7c335f65698Jesse Wilson * There are three different comparison operations each with different 70d6c77efc0b187577dd7956070adfc7c335f65698Jesse Wilson * constraints: 71d6c77efc0b187577dd7956070adfc7c335f65698Jesse Wilson * <ul> 723e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * <li>{@link #compareLongs compareLongs()} compares two locals each 73d6c77efc0b187577dd7956070adfc7c335f65698Jesse Wilson * containing a {@code long} primitive. This is the only operation that 74d6c77efc0b187577dd7956070adfc7c335f65698Jesse Wilson * can compare longs. The result of the comparison is written to another 75d6c77efc0b187577dd7956070adfc7c335f65698Jesse Wilson * {@code int} local.</li> 763e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * <li>{@link #compareFloatingPoint compareFloatingPoint()} compares two 77d6c77efc0b187577dd7956070adfc7c335f65698Jesse Wilson * locals; both {@code float} primitives or both {@code double} 78d6c77efc0b187577dd7956070adfc7c335f65698Jesse Wilson * primitives. This is the only operation that can compare floating 79d6c77efc0b187577dd7956070adfc7c335f65698Jesse Wilson * point values. This comparison takes an extra parameter that sets 80d6c77efc0b187577dd7956070adfc7c335f65698Jesse Wilson * the desired result if either parameter is {@code NaN}. The result of 81d6c77efc0b187577dd7956070adfc7c335f65698Jesse Wilson * the comparison is wrtten to another {@code int} local. 823e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * <li>{@link #compare compare()} compares two locals. The {@link 833e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Comparison#EQ} and {@link Comparison#NE} options compare either 843e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * {@code int} primitives or references. The other options compare only 853e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * {@code int} primitives. This comparison takes a {@link Label} that 863e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * will be jumped to if the comparison is true. If the comparison is 873e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * false the next instruction in sequence will be executed. 88d6c77efc0b187577dd7956070adfc7c335f65698Jesse Wilson * </ul> 89d6c77efc0b187577dd7956070adfc7c335f65698Jesse Wilson * There's no single operation to compare longs and jump, or to compare ints and 90d6c77efc0b187577dd7956070adfc7c335f65698Jesse Wilson * store the result in a local. Accomplish these goals by chaining multiple 91d6c77efc0b187577dd7956070adfc7c335f65698Jesse Wilson * operations together. 92d6c77efc0b187577dd7956070adfc7c335f65698Jesse Wilson * 93ff561a27def418f8c19a36df5fec727dfc8bb17aJesse Wilson * <h3>Branches, Labels and Returns</h3> 94ff561a27def418f8c19a36df5fec727dfc8bb17aJesse Wilson * Basic control flow is expressed using jumps and labels. Each label must be 95ff561a27def418f8c19a36df5fec727dfc8bb17aJesse Wilson * marked exactly once and may be jumped to any number of times. Create a label 963e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * using its constructor: {@code new Label()}, and mark it using {@link #mark 973e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * mark(Label)}. All jumps to a label will execute instructions starting from 983e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * that label. You can jump to a label that hasn't yet been marked (jumping 993e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * forward) or to a label that has already been marked (jumping backward). Jump 1003e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * unconditionally with {@link #jump jump(Label)} or conditionally based on a 1013e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * comparison using {@link #compare compare()}. 102ff561a27def418f8c19a36df5fec727dfc8bb17aJesse Wilson * 103ff561a27def418f8c19a36df5fec727dfc8bb17aJesse Wilson * <p>Most methods should contain either a return instruction. Void methods 1043e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * should use {@link #returnVoid()}; non-void methods should use {@link 1053e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * #returnValue returnValue()} with a local whose return type matches the 1063e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * method's return type. Constructors are considered void methods and should 1073e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * call {@link #returnVoid()}. Methods may make multiple returns. Methods 1083e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * containing no return statements must either loop infinitely or throw 1093e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * unconditionally; it is not legal to end a sequence of instructions without a 1103e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * jump, return or throw. 111d6c77efc0b187577dd7956070adfc7c335f65698Jesse Wilson * 112ff561a27def418f8c19a36df5fec727dfc8bb17aJesse Wilson * <h3>Throwing and Catching</h3> 113ff561a27def418f8c19a36df5fec727dfc8bb17aJesse Wilson * This API uses labels to handle thrown exceptions, errors and throwables. Call 1143e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * {@link #addCatchClause addCatchClause()} to register the target label and 1153e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * throwable class. All statements that follow will jump to that catch clause if 1163e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * they throw a {@link Throwable} assignable to that type. Use {@link 1173e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * #removeCatchClause removeCatchClause()} to unregister the throwable class. 11897b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * 119ff561a27def418f8c19a36df5fec727dfc8bb17aJesse Wilson * <p>Throw an throwable by first assigning it to a local and then calling 1203e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * {@link #throwValue throwValue()}. Control flow will jump to the nearest label 1213e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * assigned to a type assignable to the thrown type. In this context, "nearest" 1223e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * means the label requiring the fewest stack frames to be popped. 123d6c77efc0b187577dd7956070adfc7c335f65698Jesse Wilson * 124ff561a27def418f8c19a36df5fec727dfc8bb17aJesse Wilson * <h3>Calling methods</h3> 125ff561a27def418f8c19a36df5fec727dfc8bb17aJesse Wilson * A method's caller must know its return type, name, parameters, and invoke 1263e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * kind. Lookup a method on a type using {@link TypeId#getMethod 1273e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * TypeId.getMethod()}. This is more onerous than Java language invokes, which 1283e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * can infer the target method using the target object and parameters. There are 1293e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * four invoke kinds: 130ff561a27def418f8c19a36df5fec727dfc8bb17aJesse Wilson * <ul> 1313e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * <li>{@link #invokeStatic invokeStatic()} is used for static methods.</li> 1323e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * <li>{@link #invokeDirect invokeDirect()} is used for private instance 1333e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * methods and for constructors to call their superclass's 1343e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * constructor.</li> 1353e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * <li>{@link #invokeInterface invokeInterface()} is used to invoke a method 1363e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * whose declaring type is an interface.</li> 1373e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * <li>{@link #invokeVirtual invokeVirtual()} is used to invoke any other 1383e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * method. The target must not be static, private, a constructor, or an 1393e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * interface method.</li> 1403e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * <li>{@link #invokeSuper invokeSuper()} is used to invoke the closest 1413e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * superclass's virtual method. The target must not be static, private, 1423e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * a constructor method, or an interface method.</li> 1433e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * <li>{@link #newInstance newInstance()} is used to invoke a 1443e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * constructor.</li> 145ff561a27def418f8c19a36df5fec727dfc8bb17aJesse Wilson * </ul> 146ff561a27def418f8c19a36df5fec727dfc8bb17aJesse Wilson * All invoke methods take a local for the return value. For void methods this 147ff561a27def418f8c19a36df5fec727dfc8bb17aJesse Wilson * local is unused and may be null. 148d6c77efc0b187577dd7956070adfc7c335f65698Jesse Wilson * 149ff561a27def418f8c19a36df5fec727dfc8bb17aJesse Wilson * <h3>Field Access</h3> 1503e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Read static fields using {@link #sget sget()}; write them using {@link 1513e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * #sput sput()}. For instance values you'll need to specify the declaring 1523e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * instance; use {@link #getThis getThis()} in an instance method to use {@code 1533e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * this}. Read instance values using {@link #iget iget()} and write them with 1543e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * {@link #iput iput()}. 155d6c77efc0b187577dd7956070adfc7c335f65698Jesse Wilson * 156ff561a27def418f8c19a36df5fec727dfc8bb17aJesse Wilson * <h3>Array Access</h3> 1573e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Allocate an array using {@link #newArray newArray()}. Read an array's length 1583e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * with {@link #arrayLength arrayLength()} and its elements with {@link #aget 1593e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * aget()}. Write an array's elements with {@link #aput aput()}. 160d6c77efc0b187577dd7956070adfc7c335f65698Jesse Wilson * 161ff561a27def418f8c19a36df5fec727dfc8bb17aJesse Wilson * <h3>Types</h3> 1623e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Use {@link #cast cast()} to perform either a <strong>numeric cast</strong> or 1633e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * a <strong>type cast</strong>. Interrogate the type of a value in a local 1643e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * using {@link #instanceOfType instanceOfType()}. 1655624228626d7cdf206de25a6981ba8107be61057Jesse Wilson * 1665624228626d7cdf206de25a6981ba8107be61057Jesse Wilson * <h3>Synchronization</h3> 1673e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Acquire a monitor using {@link #monitorEnter monitorEnter()}; release it with 1683e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * {@link #monitorExit monitorExit()}. It is the caller's responsibility to 1693e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * guarantee that enter and exit calls are balanced, even in the presence of 1703e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * exceptions thrown. 1715624228626d7cdf206de25a6981ba8107be61057Jesse Wilson * 1725624228626d7cdf206de25a6981ba8107be61057Jesse Wilson * <strong>Warning:</strong> Even if a method has the {@code synchronized} flag, 1735624228626d7cdf206de25a6981ba8107be61057Jesse Wilson * dex requires instructions to acquire and release monitors manually. A method 1745624228626d7cdf206de25a6981ba8107be61057Jesse Wilson * declared with {@link java.lang.reflect.Modifier#SYNCHRONIZED SYNCHRONIZED} 1755624228626d7cdf206de25a6981ba8107be61057Jesse Wilson * but without manual calls to {@code monitorEnter()} and {@code monitorExit()} 1765624228626d7cdf206de25a6981ba8107be61057Jesse Wilson * will not be synchronized when executed. 177b0f6ea8cec29bd1b2453e8fd15d9c6f65ca3ea2cJesse Wilson */ 178579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonpublic final class Code { 179579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson private final MethodId<?, ?> method; 180579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson /** 181579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * All allocated labels. Although the order of the labels in this list 182579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * shouldn't impact behavior, it is used to determine basic block indices. 183579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson */ 184579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson private final List<Label> labels = new ArrayList<Label>(); 185579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 186579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson /** 187579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * The label currently receiving instructions. This is null if the most 188579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * recent instruction was a return or goto. 189579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson */ 190579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson private Label currentLabel; 191579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 192579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson /** true once we've fixed the positions of the parameter registers */ 193579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson private boolean localsInitialized; 194579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 195579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson private final Local<?> thisLocal; 196b3b96215f3dcbacb3f0d86780ac635cfc14ae9cfJesse Wilson 197b3b96215f3dcbacb3f0d86780ac635cfc14ae9cfJesse Wilson /** 198b3b96215f3dcbacb3f0d86780ac635cfc14ae9cfJesse Wilson * The parameters on this method. If this is non-static, the first parameter 199b3b96215f3dcbacb3f0d86780ac635cfc14ae9cfJesse Wilson * is 'thisLocal' and we have to offset the user's indices by one. 200b3b96215f3dcbacb3f0d86780ac635cfc14ae9cfJesse Wilson */ 201579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson private final List<Local<?>> parameters = new ArrayList<Local<?>>(); 202579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson private final List<Local<?>> locals = new ArrayList<Local<?>>(); 203579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson private SourcePosition sourcePosition = SourcePosition.NO_INFO; 2040e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson private final List<TypeId<?>> catchTypes = new ArrayList<TypeId<?>>(); 205579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson private final List<Label> catchLabels = new ArrayList<Label>(); 206579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson private StdTypeList catches = StdTypeList.EMPTY; 207579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 208ab220f004db90fa94ef9349ca1adde5f89012e8dJesse Wilson Code(DexMaker.MethodDeclaration methodDeclaration) { 209579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson this.method = methodDeclaration.method; 210b3b96215f3dcbacb3f0d86780ac635cfc14ae9cfJesse Wilson if (methodDeclaration.isStatic()) { 211b3b96215f3dcbacb3f0d86780ac635cfc14ae9cfJesse Wilson thisLocal = null; 212b3b96215f3dcbacb3f0d86780ac635cfc14ae9cfJesse Wilson } else { 213b3b96215f3dcbacb3f0d86780ac635cfc14ae9cfJesse Wilson thisLocal = Local.get(this, method.declaringType); 214b3b96215f3dcbacb3f0d86780ac635cfc14ae9cfJesse Wilson parameters.add(thisLocal); 215b3b96215f3dcbacb3f0d86780ac635cfc14ae9cfJesse Wilson } 2160e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson for (TypeId<?> parameter : method.parameters.types) { 217579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson parameters.add(Local.get(this, parameter)); 218579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 21923abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson this.currentLabel = new Label(); 22023abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson adopt(this.currentLabel); 221579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson this.currentLabel.marked = true; 222579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 223579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 2243e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 2253e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Allocates a new local variable of type {@code type}. It is an error to 2263e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * allocate a local after instructions have been emitted. 2273e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 2280e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson public <T> Local<T> newLocal(TypeId<T> type) { 229579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson if (localsInitialized) { 230579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson throw new IllegalStateException("Cannot allocate locals after adding instructions"); 231579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 232579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson Local<T> result = Local.get(this, type); 233579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson locals.add(result); 234579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson return result; 235579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 236579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 2373e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 2383e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Returns the local for the parameter at index {@code index} and of type 2393e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * {@code type}. 2403e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 2410e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson public <T> Local<T> getParameter(int index, TypeId<T> type) { 242b3b96215f3dcbacb3f0d86780ac635cfc14ae9cfJesse Wilson if (thisLocal != null) { 243b3b96215f3dcbacb3f0d86780ac635cfc14ae9cfJesse Wilson index++; // adjust for the hidden 'this' parameter 244b3b96215f3dcbacb3f0d86780ac635cfc14ae9cfJesse Wilson } 245579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson return coerce(parameters.get(index), type); 246579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 247579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 2483e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 2493e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Returns the local for {@code this} of type {@code type}. It is an error 2503e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * to call {@code getThis()} if this is a static method. 2513e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 2520e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson public <T> Local<T> getThis(TypeId<T> type) { 253579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson if (thisLocal == null) { 254579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson throw new IllegalStateException("static methods cannot access 'this'"); 255579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 256579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson return coerce(thisLocal, type); 257579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 258579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 259579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson @SuppressWarnings("unchecked") // guarded by an equals check 2600e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson private <T> Local<T> coerce(Local<?> local, TypeId<T> expectedType) { 261579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson if (!local.type.equals(expectedType)) { 262579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson throw new IllegalArgumentException( 263579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson "requested " + expectedType + " but was " + local.type); 264579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 265579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson return (Local<T>) local; 266579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 267579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 268579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson /** 269579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * Assigns registers to locals. From the spec: 270579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * "the N arguments to a method land in the last N registers of the 271579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * method's invocation frame, in order. Wide arguments consume two 272579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * registers. Instance methods are passed a this reference as their 273579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * first argument." 274579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * 275579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * In addition to assigning registers to each of the locals, this creates 276579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * instructions to move parameters into their initial registers. These 277579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * instructions are inserted before the code's first real instruction. 278579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson */ 279579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson void initializeLocals() { 280579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson if (localsInitialized) { 281579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson throw new AssertionError(); 282579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 283579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson localsInitialized = true; 284579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 285579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson int reg = 0; 286579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson for (Local<?> local : locals) { 287579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson reg += local.initialize(reg); 288579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 289579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson int firstParamReg = reg; 290579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson List<Insn> moveParameterInstructions = new ArrayList<Insn>(); 291579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson for (Local<?> local : parameters) { 292579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson CstInteger paramConstant = CstInteger.make(reg - firstParamReg); 293579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson reg += local.initialize(reg); 294579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson moveParameterInstructions.add(new PlainCstInsn(Rops.opMoveParam(local.type.ropType), 295579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson sourcePosition, local.spec(), RegisterSpecList.EMPTY, paramConstant)); 296579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 297579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson labels.get(0).instructions.addAll(0, moveParameterInstructions); 298579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 299579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 300b3b96215f3dcbacb3f0d86780ac635cfc14ae9cfJesse Wilson /** 301b3b96215f3dcbacb3f0d86780ac635cfc14ae9cfJesse Wilson * Returns the number of registers to hold the parameters. This includes the 302b3b96215f3dcbacb3f0d86780ac635cfc14ae9cfJesse Wilson * 'this' parameter if it exists. 303b3b96215f3dcbacb3f0d86780ac635cfc14ae9cfJesse Wilson */ 304579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson int paramSize() { 305579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson int result = 0; 306579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson for (Local<?> local : parameters) { 307579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson result += local.size(); 308579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 309579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson return result; 310579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 311579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 312579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson // labels 313579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 314579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson /** 31523abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson * Assigns {@code target} to this code. 316579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson */ 31723abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson private void adopt(Label target) { 31823abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson if (target.code == this) { 31923abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson return; // already adopted 32023abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson } 32123abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson if (target.code != null) { 32223abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson throw new IllegalArgumentException("Cannot adopt label; it belongs to another Code"); 32323abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson } 32423abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson target.code = this; 32523abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson labels.add(target); 326579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 327579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 328579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson /** 329579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * Start defining instructions for the named label. 330579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson */ 331579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson public void mark(Label label) { 33223abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson adopt(label); 333579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson if (label.marked) { 334579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson throw new IllegalStateException("already marked"); 335579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 336579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson label.marked = true; 337579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson if (currentLabel != null) { 338579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson jump(label); // blocks must end with a branch, return or throw 339579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 340579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson currentLabel = label; 341579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 342579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 3433e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 3443e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Transfers flow control to the instructions at {@code target}. It is an 3453e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * error to jump to a label not marked on this {@code Code}. 3463e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 347579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson public void jump(Label target) { 34823abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson adopt(target); 349579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson addInstruction(new PlainInsn(Rops.GOTO, sourcePosition, null, RegisterSpecList.EMPTY), 350579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson target); 351579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 352579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 3533e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 3543e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Registers {@code catchClause} as a branch target for all instructions 3553e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * in this frame that throw a class assignable to {@code toCatch}. This 3563e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * includes methods invoked from this frame. Deregister the clause using 3573e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * {@link #removeCatchClause removeCatchClause()}. It is an error to 3583e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * register a catch clause without also {@link #mark marking it} in the same 3593e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * {@code Code} instance. 3603e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 3613e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson public void addCatchClause(TypeId<? extends Throwable> toCatch, Label catchClause) { 3623e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson if (catchTypes.contains(toCatch)) { 3633e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson throw new IllegalArgumentException("Already caught: " + toCatch); 364579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 36523abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson adopt(catchClause); 3663e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson catchTypes.add(toCatch); 367579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson catches = toTypeList(catchTypes); 368579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson catchLabels.add(catchClause); 369579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 370579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 3713e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 3723e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Deregisters the catch clause label for {@code toCatch} and returns it. 3733e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 3743e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson public Label removeCatchClause(TypeId<? extends Throwable> toCatch) { 3753e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson int index = catchTypes.indexOf(toCatch); 376579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson if (index == -1) { 3773e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson throw new IllegalArgumentException("No catch clause: " + toCatch); 378579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 379579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson catchTypes.remove(index); 380579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson catches = toTypeList(catchTypes); 381579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson return catchLabels.remove(index); 382579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 383579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 3843e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 3853e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Throws the throwable in {@code toThrow}. 3863e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 3873e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson public void throwValue(Local<? extends Throwable> toThrow) { 388579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson addInstruction(new ThrowingInsn(Rops.THROW, sourcePosition, 3893e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson RegisterSpecList.make(toThrow.spec()), catches)); 390579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 391579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 3920e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson private StdTypeList toTypeList(List<TypeId<?>> types) { 393579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson StdTypeList result = new StdTypeList(types.size()); 394579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson for (int i = 0; i < types.size(); i++) { 395579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson result.set(i, types.get(i).ropType); 396579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 397579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson return result; 398579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 399579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 400579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson private void addInstruction(Insn insn) { 401579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson addInstruction(insn, null); 402579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 403579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 404579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson /** 405579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * @param branch the branches to follow; interpretation depends on the 406579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * instruction's branchingness. 407579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson */ 408579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson private void addInstruction(Insn insn, Label branch) { 409579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson if (currentLabel == null || !currentLabel.marked) { 410579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson throw new IllegalStateException("no current label"); 411579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 412579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson currentLabel.instructions.add(insn); 413579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 414579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson switch (insn.getOpcode().getBranchingness()) { 415579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson case BRANCH_NONE: 416579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson if (branch != null) { 417579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson throw new IllegalArgumentException("unexpected branch: " + branch); 418579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 419579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson return; 420579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 421579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson case BRANCH_RETURN: 422579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson if (branch != null) { 423579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson throw new IllegalArgumentException("unexpected branch: " + branch); 424579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 425579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson currentLabel = null; 426579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson break; 427579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 428579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson case BRANCH_GOTO: 429579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson if (branch == null) { 430579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson throw new IllegalArgumentException("branch == null"); 431579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 432579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson currentLabel.primarySuccessor = branch; 433579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson currentLabel = null; 434579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson break; 435579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 436579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson case Rop.BRANCH_IF: 437579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson if (branch == null) { 438579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson throw new IllegalArgumentException("branch == null"); 439579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 440579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson splitCurrentLabel(branch, Collections.<Label>emptyList()); 441579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson break; 442579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 443579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson case Rop.BRANCH_THROW: 444579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson if (branch != null) { 445579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson throw new IllegalArgumentException("unexpected branch: " + branch); 446579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 447579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson splitCurrentLabel(null, new ArrayList<Label>(catchLabels)); 448579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson break; 449579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 450579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson default: 451579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson throw new IllegalArgumentException(); 452579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 453579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 454579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 455579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson /** 456579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * Closes the current label and starts a new one. 457579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * 458579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * @param catchLabels an immutable list of catch labels 459579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson */ 460579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson private void splitCurrentLabel(Label alternateSuccessor, List<Label> catchLabels) { 46123abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson Label newLabel = new Label(); 46223abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson adopt(newLabel); 463579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson currentLabel.primarySuccessor = newLabel; 464579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson currentLabel.alternateSuccessor = alternateSuccessor; 465579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson currentLabel.catchLabels = catchLabels; 466579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson currentLabel = newLabel; 467579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson currentLabel.marked = true; 468579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 469579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 4704838105a39ae6c608c2a0e242ead249d8683a5d0Jesse Wilson // instructions: locals 471579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 4723e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 4733e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Copies the constant value {@code value} to {@code target}. The constant 4743e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * must be a primitive, String, Class, TypeId, or null. 4753e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 476579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson public <T> void loadConstant(Local<T> target, T value) { 477579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson Rop rop = value == null 478579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson ? Rops.CONST_OBJECT_NOTHROW 479579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson : Rops.opConst(target.type.ropType); 480579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson if (rop.getBranchingness() == BRANCH_NONE) { 481579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson addInstruction(new PlainCstInsn(rop, sourcePosition, target.spec(), 482579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson RegisterSpecList.EMPTY, Constants.getConstant(value))); 483579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } else { 484579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson addInstruction(new ThrowingCstInsn(rop, sourcePosition, 485579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson RegisterSpecList.EMPTY, catches, Constants.getConstant(value))); 486579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson moveResult(target, true); 487579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 488579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 489579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 4904838105a39ae6c608c2a0e242ead249d8683a5d0Jesse Wilson /** 4914838105a39ae6c608c2a0e242ead249d8683a5d0Jesse Wilson * Copies the value in {@code source} to {@code target}. 4924838105a39ae6c608c2a0e242ead249d8683a5d0Jesse Wilson */ 4934838105a39ae6c608c2a0e242ead249d8683a5d0Jesse Wilson public <T> void move(Local<T> target, Local<T> source) { 4944838105a39ae6c608c2a0e242ead249d8683a5d0Jesse Wilson addInstruction(new PlainInsn(Rops.opMove(source.type.ropType), 4954838105a39ae6c608c2a0e242ead249d8683a5d0Jesse Wilson sourcePosition, target.spec(), source.spec())); 4964838105a39ae6c608c2a0e242ead249d8683a5d0Jesse Wilson } 4974838105a39ae6c608c2a0e242ead249d8683a5d0Jesse Wilson 498d6c77efc0b187577dd7956070adfc7c335f65698Jesse Wilson // instructions: unary and binary 499579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 5003e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 5013e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Executes {@code op} and sets {@code target} to the result. 5023e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 503b0f6ea8cec29bd1b2453e8fd15d9c6f65ca3ea2cJesse Wilson public <T> void op(UnaryOp op, Local<T> target, Local<T> source) { 50497b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson addInstruction(new PlainInsn(op.rop(source.type), sourcePosition, 50597b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson target.spec(), source.spec())); 506579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 507579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 5083e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 5093e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Executes {@code op} and sets {@code target} to the result. 5103e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 511579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson public <T> void op(BinaryOp op, Local<T> target, Local<T> a, Local<T> b) { 512579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson Rop rop = op.rop(StdTypeList.make(a.type.ropType, b.type.ropType)); 513579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson RegisterSpecList sources = RegisterSpecList.make(a.spec(), b.spec()); 514579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 515579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson if (rop.getBranchingness() == BRANCH_NONE) { 516579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson addInstruction(new PlainInsn(rop, sourcePosition, target.spec(), sources)); 517579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } else { 518579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson addInstruction(new ThrowingInsn(rop, sourcePosition, sources, catches)); 519579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson moveResult(target, true); 520579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 521579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 522579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 523579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson // instructions: branches 524579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 525579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson /** 526d6c77efc0b187577dd7956070adfc7c335f65698Jesse Wilson * Compare ints or references. If the comparison is true, execution jumps to 527d6c77efc0b187577dd7956070adfc7c335f65698Jesse Wilson * {@code trueLabel}. If it is false, execution continues to the next 528d6c77efc0b187577dd7956070adfc7c335f65698Jesse Wilson * instruction. 529579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson */ 5300e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson public <T> void compare(Comparison comparison, Label trueLabel, Local<T> a, Local<T> b) { 53123abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson adopt(trueLabel); 532d6c77efc0b187577dd7956070adfc7c335f65698Jesse Wilson // TODO: ops to compare with zero/null: just omit the 2nd local in StdTypeList.make() 533579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson Rop rop = comparison.rop(StdTypeList.make(a.type.ropType, b.type.ropType)); 534579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson addInstruction(new PlainInsn(rop, sourcePosition, null, 535579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson RegisterSpecList.make(a.spec(), b.spec())), trueLabel); 536579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 537579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 538579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson /** 5394838105a39ae6c608c2a0e242ead249d8683a5d0Jesse Wilson * Compare floats or doubles. This stores -1 in {@code target} if {@code 5404838105a39ae6c608c2a0e242ead249d8683a5d0Jesse Wilson * a < b}, 0 in {@code target} if {@code a == b} and 1 in target if {@code 5414838105a39ae6c608c2a0e242ead249d8683a5d0Jesse Wilson * a > b}. This stores {@code nanValue} in {@code target} if either value 5424838105a39ae6c608c2a0e242ead249d8683a5d0Jesse Wilson * is {@code NaN}. 543579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson */ 544d6c77efc0b187577dd7956070adfc7c335f65698Jesse Wilson public <T extends Number> void compareFloatingPoint( 545d6c77efc0b187577dd7956070adfc7c335f65698Jesse Wilson Local<Integer> target, Local<T> a, Local<T> b, int nanValue) { 546579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson Rop rop; 547579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson if (nanValue == 1) { 548579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson rop = Rops.opCmpg(a.type.ropType); 549579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } else if (nanValue == -1) { 550579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson rop = Rops.opCmpl(a.type.ropType); 551579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } else { 552579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson throw new IllegalArgumentException("expected 1 or -1 but was " + nanValue); 553579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 554579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson addInstruction(new PlainInsn(rop, sourcePosition, target.spec(), 555579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson RegisterSpecList.make(a.spec(), b.spec()))); 556579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 557579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 558579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson /** 5594838105a39ae6c608c2a0e242ead249d8683a5d0Jesse Wilson * Compare longs. This stores -1 in {@code target} if {@code 5604838105a39ae6c608c2a0e242ead249d8683a5d0Jesse Wilson * a < b}, 0 in {@code target} if {@code a == b} and 1 in target if {@code 5614838105a39ae6c608c2a0e242ead249d8683a5d0Jesse Wilson * a > b}. 562579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson */ 563d6c77efc0b187577dd7956070adfc7c335f65698Jesse Wilson public void compareLongs(Local<Integer> target, Local<Long> a, Local<Long> b) { 564579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson addInstruction(new PlainInsn(Rops.CMPL_LONG, sourcePosition, target.spec(), 565579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson RegisterSpecList.make(a.spec(), b.spec()))); 566579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 567579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 568579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson // instructions: fields 569579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 5703e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 5713e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Copies the value in instance field {@code fieldId} of {@code instance} to 5723e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * {@code target}. 5733e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 5740e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson public <D, V> void iget(FieldId<D, V> fieldId, Local<V> target, Local<D> instance) { 575579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson addInstruction(new ThrowingCstInsn(Rops.opGetField(target.type.ropType), sourcePosition, 576579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson RegisterSpecList.make(instance.spec()), catches, fieldId.constant)); 577579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson moveResult(target, true); 578579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 579579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 5803e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 5813e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Copies the value in {@code source} to the instance field {@code fieldId} 5823e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * of {@code instance}. 5833e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 5843e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson public <D, V> void iput(FieldId<D, V> fieldId, Local<D> instance, Local<V> source) { 585579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson addInstruction(new ThrowingCstInsn(Rops.opPutField(source.type.ropType), sourcePosition, 586579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson RegisterSpecList.make(source.spec(), instance.spec()), catches, fieldId.constant)); 587579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 588579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 5893e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 5908ec4b1db3afa51730508be7064d2111b723ac2cdHugo Hudson * Copies the value in the static field {@code fieldId} to {@code target}. 5913e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 592579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson public <V> void sget(FieldId<?, V> fieldId, Local<V> target) { 593579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson addInstruction(new ThrowingCstInsn(Rops.opGetStatic(target.type.ropType), sourcePosition, 594579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson RegisterSpecList.EMPTY, catches, fieldId.constant)); 595579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson moveResult(target, true); 596579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 597579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 5983e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 5993e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Copies the value in {@code source} to the static field {@code fieldId}. 6003e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 601579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson public <V> void sput(FieldId<?, V> fieldId, Local<V> source) { 602579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson addInstruction(new ThrowingCstInsn(Rops.opPutStatic(source.type.ropType), sourcePosition, 603579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson RegisterSpecList.make(source.spec()), catches, fieldId.constant)); 604579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 605579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 606579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson // instructions: invoke 607579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 6083e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 6093e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Calls the constructor {@code constructor} using {@code args} and assigns 6103e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * the new instance to {@code target}. 6113e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 612579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson public <T> void newInstance(Local<T> target, MethodId<T, Void> constructor, Local<?>... args) { 613579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson if (target == null) { 614579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson throw new IllegalArgumentException(); 615579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 616579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson addInstruction(new ThrowingCstInsn(Rops.NEW_INSTANCE, sourcePosition, 617579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson RegisterSpecList.EMPTY, catches, constructor.declaringType.constant)); 618579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson moveResult(target, true); 619579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson invokeDirect(constructor, null, target, args); 620579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 621579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 6223e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 6233e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Calls the static method {@code method} using {@code args} and assigns the 6243e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * result to {@code target}. 6253e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * 6263e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * @param target the local to receive the method's return value, or {@code 6273e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * null} if the return type is {@code void} or if its value not needed. 6283e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 629579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson public <R> void invokeStatic(MethodId<?, R> method, Local<? super R> target, Local<?>... args) { 630579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson invoke(Rops.opInvokeStatic(method.prototype(true)), method, target, null, args); 631579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 632579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 6333e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 6343e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Calls the non-private instance method {@code method} of {@code instance} 6353e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * using {@code args} and assigns the result to {@code target}. 6363e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * 6373e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * @param method a non-private, non-static, method declared on a class. May 6383e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * not be an interface method or a constructor. 6393e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * @param target the local to receive the method's return value, or {@code 6403e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * null} if the return type is {@code void} or if its value not needed. 6413e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 642579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson public <D, R> void invokeVirtual(MethodId<D, R> method, Local<? super R> target, 6433e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson Local<? extends D> instance, Local<?>... args) { 6443e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson invoke(Rops.opInvokeVirtual(method.prototype(true)), method, target, instance, args); 645579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 646579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 6473e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 6483e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Calls {@code method} of {@code instance} using {@code args} and assigns 6493e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * the result to {@code target}. 6503e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * 6513e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * @param method either a private method or the superclass's constructor in 6523e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * a constructor's call to {@code super()}. 6533e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * @param target the local to receive the method's return value, or {@code 6543e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * null} if the return type is {@code void} or if its value not needed. 6553e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 656579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson public <D, R> void invokeDirect(MethodId<D, R> method, Local<? super R> target, 6573e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson Local<? extends D> instance, Local<?>... args) { 6583e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson invoke(Rops.opInvokeDirect(method.prototype(true)), method, target, instance, args); 659579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 660579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 6613e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 6623e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Calls the closest superclass's virtual method {@code method} of {@code 6633e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * instance} using {@code args} and assigns the result to {@code target}. 6643e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * 6653e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * @param target the local to receive the method's return value, or {@code 6663e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * null} if the return type is {@code void} or if its value not needed. 6673e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 668579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson public <D, R> void invokeSuper(MethodId<D, R> method, Local<? super R> target, 6693e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson Local<? extends D> instance, Local<?>... args) { 6703e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson invoke(Rops.opInvokeSuper(method.prototype(true)), method, target, instance, args); 671579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 672579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 6733e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 6743e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Calls the interface method {@code method} of {@code instance} using 6753e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * {@code args} and assigns the result to {@code target}. 6763e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * 6773e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * @param method a method declared on an interface. 6783e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * @param target the local to receive the method's return value, or {@code 6793e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * null} if the return type is {@code void} or if its value not needed. 6803e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 681579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson public <D, R> void invokeInterface(MethodId<D, R> method, Local<? super R> target, 6823e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson Local<? extends D> instance, Local<?>... args) { 6833e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson invoke(Rops.opInvokeInterface(method.prototype(true)), method, target, instance, args); 684579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 685579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 686579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson private <D, R> void invoke(Rop rop, MethodId<D, R> method, Local<? super R> target, 687579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson Local<? extends D> object, Local<?>... args) { 688579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson addInstruction(new ThrowingCstInsn(rop, sourcePosition, concatenate(object, args), 689579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson catches, method.constant)); 690579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson if (target != null) { 691579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson moveResult(target, false); 692579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 693579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 694579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 695579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson // instructions: types 696579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 6973e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 6983e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Tests if the value in {@code source} is assignable to {@code type}. If it 6993e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * is, {@code target} is assigned to 1; otherwise {@code target} is assigned 7003e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * to 0. 7013e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 7020e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson public void instanceOfType(Local<?> target, Local<?> source, TypeId<?> type) { 703579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson addInstruction(new ThrowingCstInsn(Rops.INSTANCE_OF, sourcePosition, 704579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson RegisterSpecList.make(source.spec()), catches, type.constant)); 705579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson moveResult(target, true); 706579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 707579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 70897b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson /** 70997b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * Performs either a numeric cast or a type cast. 71097b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * 71197b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * <h3>Numeric Casts</h3> 71297b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * Converts a primitive to a different representation. Numeric casts may 71397b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * be lossy. For example, converting the double {@code 1.8d} to an integer 71497b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * yields {@code 1}, losing the fractional part. Converting the integer 71597b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * {@code 0x12345678} to a short yields {@code 0x5678}, losing the high 71697b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * bytes. The following numeric casts are supported: 71797b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * 71897b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * <p><table border="1"> 71997b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * <tr><th>From</th><th>To</th></tr> 72097b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * <tr><td>int</td><td>byte, char, short, long, float, double</td></tr> 72197b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * <tr><td>long</td><td>int, float, double</td></tr> 72297b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * <tr><td>float</td><td>int, long, double</td></tr> 72397b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * <tr><td>double</td><td>int, long, float</td></tr> 72497b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * </table> 72597b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * 72697b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * <p>For some primitive conversions it will be necessary to chain multiple 72797b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * cast operations. For example, to go from float to short one would first 72897b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * cast float to int and then int to short. 72997b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * 73097b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * <p>Numeric casts never throw {@link ClassCastException}. 73197b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * 73297b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * <h3>Type Casts</h3> 73397b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * Checks that a reference value is assignable to the target type. If it is 73497b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * assignable it is copied to the target local. If it is not assignable a 73597b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * {@link ClassCastException} is thrown. 73697b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson */ 73797b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson public void cast(Local<?> target, Local<?> source) { 73897b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson if (source.getType().ropType.isReference()) { 73997b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson addInstruction(new ThrowingCstInsn(Rops.CHECK_CAST, sourcePosition, 74097b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson RegisterSpecList.make(source.spec()), catches, target.type.constant)); 74197b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson moveResult(target, true); 74297b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson } else { 74397b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson addInstruction(new PlainInsn(getCastRop(source.type.ropType, target.type.ropType), 74497b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson sourcePosition, target.spec(), source.spec())); 74597b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson } 74697b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson } 74797b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson 74897b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson private Rop getCastRop(com.android.dx.rop.type.Type sourceType, 74997b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson com.android.dx.rop.type.Type targetType) { 75097b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson if (sourceType.getBasicType() == BT_INT) { 75197b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson switch (targetType.getBasicType()) { 75297b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson case BT_SHORT: 75397b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson return Rops.TO_SHORT; 75497b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson case BT_CHAR: 75597b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson return Rops.TO_CHAR; 75697b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson case BT_BYTE: 75797b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson return Rops.TO_BYTE; 75897b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson } 75997b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson } 76097b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson return Rops.opConv(targetType, sourceType); 761579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 762579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 763579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson // instructions: arrays 764579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 7653e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 7663e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Sets {@code target} to the length of the array in {@code array}. 7673e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 7680e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson public <T> void arrayLength(Local<Integer> target, Local<T> array) { 769579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson addInstruction(new ThrowingInsn(Rops.ARRAY_LENGTH, sourcePosition, 770579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson RegisterSpecList.make(array.spec()), catches)); 771579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson moveResult(target, true); 772579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 773579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 7743e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 7753e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Assigns {@code target} to a newly allocated array of length {@code 7763e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * length}. The array's type is the same as {@code target}'s type. 7773e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 7780e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson public <T> void newArray(Local<T> target, Local<Integer> length) { 779579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson addInstruction(new ThrowingCstInsn(Rops.opNewArray(target.type.ropType), sourcePosition, 780579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson RegisterSpecList.make(length.spec()), catches, target.type.constant)); 781579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson moveResult(target, true); 782579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 783579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 7843e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 7858ec4b1db3afa51730508be7064d2111b723ac2cdHugo Hudson * Assigns the element at {@code index} in {@code array} to {@code target}. 7863e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 7870e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson public void aget(Local<?> target, Local<?> array, Local<Integer> index) { 788579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson addInstruction(new ThrowingInsn(Rops.opAget(target.type.ropType), sourcePosition, 789579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson RegisterSpecList.make(array.spec(), index.spec()), catches)); 790579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson moveResult(target, true); 791579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 792579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 7933e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 7948ec4b1db3afa51730508be7064d2111b723ac2cdHugo Hudson * Assigns {@code source} to the element at {@code index} in {@code array}. 7953e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 796579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson public void aput(Local<?> array, Local<Integer> index, Local<?> source) { 797579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson addInstruction(new ThrowingInsn(Rops.opAput(source.type.ropType), sourcePosition, 798579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson RegisterSpecList.make(source.spec(), array.spec(), index.spec()), catches)); 799579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 800579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 801579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson // instructions: return 802579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 8033e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 8043e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Returns from a {@code void} method. After a return it is an error to 8053e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * define further instructions after a return without first {@link #mark 8063e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * marking} an existing unmarked label. 8073e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 808579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson public void returnVoid() { 8090e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson if (!method.returnType.equals(TypeId.VOID)) { 810579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson throw new IllegalArgumentException("declared " + method.returnType 811579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson + " but returned void"); 812579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 813579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson addInstruction(new PlainInsn(Rops.RETURN_VOID, sourcePosition, null, 814579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson RegisterSpecList.EMPTY)); 815579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 816579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 8173e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 8183e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Returns the value in {@code result} to the calling method. After a return 8193e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * it is an error to define further instructions after a return without 8203e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * first {@link #mark marking} an existing unmarked label. 8213e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 822579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson public void returnValue(Local<?> result) { 823579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson if (!result.type.equals(method.returnType)) { 824579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson // TODO: this is probably too strict. 825579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson throw new IllegalArgumentException("declared " + method.returnType 826579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson + " but returned " + result.type); 827579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 828579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson addInstruction(new PlainInsn(Rops.opReturn(result.type.ropType), sourcePosition, 829579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson null, RegisterSpecList.make(result.spec()))); 830579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 831579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 832579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson private void moveResult(Local<?> target, boolean afterNonInvokeThrowingInsn) { 833579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson Rop rop = afterNonInvokeThrowingInsn 834579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson ? Rops.opMoveResultPseudo(target.type.ropType) 835579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson : Rops.opMoveResult(target.type.ropType); 836579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson addInstruction(new PlainInsn(rop, sourcePosition, target.spec(), RegisterSpecList.EMPTY)); 837579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 838579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 8395624228626d7cdf206de25a6981ba8107be61057Jesse Wilson // instructions; synchronized 8405624228626d7cdf206de25a6981ba8107be61057Jesse Wilson 8413e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 8423e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Awaits the lock on {@code monitor}, and acquires it. 8433e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 8445624228626d7cdf206de25a6981ba8107be61057Jesse Wilson public void monitorEnter(Local<?> monitor) { 8455624228626d7cdf206de25a6981ba8107be61057Jesse Wilson addInstruction(new ThrowingInsn(Rops.MONITOR_ENTER, sourcePosition, 8465624228626d7cdf206de25a6981ba8107be61057Jesse Wilson RegisterSpecList.make(monitor.spec()), catches)); 8475624228626d7cdf206de25a6981ba8107be61057Jesse Wilson } 8485624228626d7cdf206de25a6981ba8107be61057Jesse Wilson 8493e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 8503e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Releases the held lock on {@code monitor}. 8513e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 8525624228626d7cdf206de25a6981ba8107be61057Jesse Wilson public void monitorExit(Local<?> monitor) { 8535624228626d7cdf206de25a6981ba8107be61057Jesse Wilson addInstruction(new ThrowingInsn(Rops.MONITOR_ENTER, sourcePosition, 8545624228626d7cdf206de25a6981ba8107be61057Jesse Wilson RegisterSpecList.make(monitor.spec()), catches)); 8555624228626d7cdf206de25a6981ba8107be61057Jesse Wilson } 8565624228626d7cdf206de25a6981ba8107be61057Jesse Wilson 857579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson // produce BasicBlocks for dex 858579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 859579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson BasicBlockList toBasicBlocks() { 860579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson if (!localsInitialized) { 861579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson initializeLocals(); 862579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 863579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 864579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson cleanUpLabels(); 865579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 866579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson BasicBlockList result = new BasicBlockList(labels.size()); 867579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson for (int i = 0; i < labels.size(); i++) { 868579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson result.set(i, labels.get(i).toBasicBlock()); 869579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 870579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson return result; 871579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 872579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 873579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson /** 874579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * Removes empty labels and assigns IDs to non-empty labels. 875579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson */ 876579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson private void cleanUpLabels() { 877579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson int id = 0; 878579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson for (Iterator<Label> i = labels.iterator(); i.hasNext();) { 879579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson Label label = i.next(); 880579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson if (label.isEmpty()) { 881579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson i.remove(); 882579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } else { 883579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson label.compact(); 884579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson label.id = id++; 885579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 886579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 887579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 888579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 889579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson private static RegisterSpecList concatenate(Local<?> first, Local<?>[] rest) { 890579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson int offset = (first != null) ? 1 : 0; 891579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson RegisterSpecList result = new RegisterSpecList(offset + rest.length); 892579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson if (first != null) { 893579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson result.set(0, first.spec()); 894579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 895579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson for (int i = 0; i < rest.length; i++) { 896579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson result.set(i + offset, rest[i].spec()); 897579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 898579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson return result; 899579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 900579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson} 901