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 * 10395689a700bfea5e2d78380a442fc2903cc40a3f2Mark Brophy * <p>Most methods should contain 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 /** 50995689a700bfea5e2d78380a442fc2903cc40a3f2Mark Brophy * Executes {@code op} and sets {@code target} to the result. For most 51095689a700bfea5e2d78380a442fc2903cc40a3f2Mark Brophy * binary operations, the types of {@code a} and {@code b} must be the same. 51195689a700bfea5e2d78380a442fc2903cc40a3f2Mark Brophy * Shift operations (like {@link BinaryOp#SHIFT_LEFT}) require {@code b} to 51295689a700bfea5e2d78380a442fc2903cc40a3f2Mark Brophy * be an {@code int}, even when {@code a} is a {@code long}. 5133e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 51495689a700bfea5e2d78380a442fc2903cc40a3f2Mark Brophy public <T1, T2> void op(BinaryOp op, Local<T1> target, Local<T1> a, Local<T2> b) { 515579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson Rop rop = op.rop(StdTypeList.make(a.type.ropType, b.type.ropType)); 516579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson RegisterSpecList sources = RegisterSpecList.make(a.spec(), b.spec()); 517579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 518579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson if (rop.getBranchingness() == BRANCH_NONE) { 519579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson addInstruction(new PlainInsn(rop, sourcePosition, target.spec(), sources)); 520579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } else { 521579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson addInstruction(new ThrowingInsn(rop, sourcePosition, sources, catches)); 522579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson moveResult(target, true); 523579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 524579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 525579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 526579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson // instructions: branches 527579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 528579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson /** 529d6c77efc0b187577dd7956070adfc7c335f65698Jesse Wilson * Compare ints or references. If the comparison is true, execution jumps to 530d6c77efc0b187577dd7956070adfc7c335f65698Jesse Wilson * {@code trueLabel}. If it is false, execution continues to the next 531d6c77efc0b187577dd7956070adfc7c335f65698Jesse Wilson * instruction. 532579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson */ 5330e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson public <T> void compare(Comparison comparison, Label trueLabel, Local<T> a, Local<T> b) { 53423abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson adopt(trueLabel); 535d6c77efc0b187577dd7956070adfc7c335f65698Jesse Wilson // TODO: ops to compare with zero/null: just omit the 2nd local in StdTypeList.make() 536579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson Rop rop = comparison.rop(StdTypeList.make(a.type.ropType, b.type.ropType)); 537579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson addInstruction(new PlainInsn(rop, sourcePosition, null, 538579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson RegisterSpecList.make(a.spec(), b.spec())), trueLabel); 539579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 540579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 541579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson /** 5424838105a39ae6c608c2a0e242ead249d8683a5d0Jesse Wilson * Compare floats or doubles. This stores -1 in {@code target} if {@code 5434838105a39ae6c608c2a0e242ead249d8683a5d0Jesse Wilson * a < b}, 0 in {@code target} if {@code a == b} and 1 in target if {@code 5444838105a39ae6c608c2a0e242ead249d8683a5d0Jesse Wilson * a > b}. This stores {@code nanValue} in {@code target} if either value 5454838105a39ae6c608c2a0e242ead249d8683a5d0Jesse Wilson * is {@code NaN}. 546579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson */ 547d6c77efc0b187577dd7956070adfc7c335f65698Jesse Wilson public <T extends Number> void compareFloatingPoint( 548d6c77efc0b187577dd7956070adfc7c335f65698Jesse Wilson Local<Integer> target, Local<T> a, Local<T> b, int nanValue) { 549579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson Rop rop; 550579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson if (nanValue == 1) { 551579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson rop = Rops.opCmpg(a.type.ropType); 552579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } else if (nanValue == -1) { 553579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson rop = Rops.opCmpl(a.type.ropType); 554579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } else { 555579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson throw new IllegalArgumentException("expected 1 or -1 but was " + nanValue); 556579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 557579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson addInstruction(new PlainInsn(rop, sourcePosition, target.spec(), 558579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson RegisterSpecList.make(a.spec(), b.spec()))); 559579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 560579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 561579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson /** 5624838105a39ae6c608c2a0e242ead249d8683a5d0Jesse Wilson * Compare longs. This stores -1 in {@code target} if {@code 5634838105a39ae6c608c2a0e242ead249d8683a5d0Jesse Wilson * a < b}, 0 in {@code target} if {@code a == b} and 1 in target if {@code 5644838105a39ae6c608c2a0e242ead249d8683a5d0Jesse Wilson * a > b}. 565579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson */ 566d6c77efc0b187577dd7956070adfc7c335f65698Jesse Wilson public void compareLongs(Local<Integer> target, Local<Long> a, Local<Long> b) { 567579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson addInstruction(new PlainInsn(Rops.CMPL_LONG, sourcePosition, target.spec(), 568579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson RegisterSpecList.make(a.spec(), b.spec()))); 569579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 570579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 571579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson // instructions: fields 572579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 5733e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 5743e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Copies the value in instance field {@code fieldId} of {@code instance} to 5753e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * {@code target}. 5763e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 5770e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson public <D, V> void iget(FieldId<D, V> fieldId, Local<V> target, Local<D> instance) { 578579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson addInstruction(new ThrowingCstInsn(Rops.opGetField(target.type.ropType), sourcePosition, 579579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson RegisterSpecList.make(instance.spec()), catches, fieldId.constant)); 580579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson moveResult(target, true); 581579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 582579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 5833e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 5843e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Copies the value in {@code source} to the instance field {@code fieldId} 5853e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * of {@code instance}. 5863e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 5873e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson public <D, V> void iput(FieldId<D, V> fieldId, Local<D> instance, Local<V> source) { 588579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson addInstruction(new ThrowingCstInsn(Rops.opPutField(source.type.ropType), sourcePosition, 589579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson RegisterSpecList.make(source.spec(), instance.spec()), catches, fieldId.constant)); 590579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 591579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 5923e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 5938ec4b1db3afa51730508be7064d2111b723ac2cdHugo Hudson * Copies the value in the static field {@code fieldId} to {@code target}. 5943e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 595579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson public <V> void sget(FieldId<?, V> fieldId, Local<V> target) { 596579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson addInstruction(new ThrowingCstInsn(Rops.opGetStatic(target.type.ropType), sourcePosition, 597579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson RegisterSpecList.EMPTY, catches, fieldId.constant)); 598579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson moveResult(target, true); 599579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 600579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 6013e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 6023e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Copies the value in {@code source} to the static field {@code fieldId}. 6033e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 604579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson public <V> void sput(FieldId<?, V> fieldId, Local<V> source) { 605579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson addInstruction(new ThrowingCstInsn(Rops.opPutStatic(source.type.ropType), sourcePosition, 606579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson RegisterSpecList.make(source.spec()), catches, fieldId.constant)); 607579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 608579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 609579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson // instructions: invoke 610579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 6113e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 6123e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Calls the constructor {@code constructor} using {@code args} and assigns 6133e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * the new instance to {@code target}. 6143e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 615579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson public <T> void newInstance(Local<T> target, MethodId<T, Void> constructor, Local<?>... args) { 616579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson if (target == null) { 617579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson throw new IllegalArgumentException(); 618579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 619579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson addInstruction(new ThrowingCstInsn(Rops.NEW_INSTANCE, sourcePosition, 620579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson RegisterSpecList.EMPTY, catches, constructor.declaringType.constant)); 621579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson moveResult(target, true); 622579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson invokeDirect(constructor, null, target, args); 623579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 624579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 6253e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 6263e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Calls the static method {@code method} using {@code args} and assigns the 6273e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * result to {@code target}. 6283e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * 6293e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * @param target the local to receive the method's return value, or {@code 6303e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * null} if the return type is {@code void} or if its value not needed. 6313e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 632579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson public <R> void invokeStatic(MethodId<?, R> method, Local<? super R> target, Local<?>... args) { 633579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson invoke(Rops.opInvokeStatic(method.prototype(true)), method, target, null, args); 634579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 635579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 6363e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 6373e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Calls the non-private instance method {@code method} of {@code instance} 6383e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * using {@code args} and assigns the result to {@code target}. 6393e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * 6403e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * @param method a non-private, non-static, method declared on a class. May 6413e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * not be an interface method or a constructor. 6423e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * @param target the local to receive the method's return value, or {@code 6433e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * null} if the return type is {@code void} or if its value not needed. 6443e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 645579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson public <D, R> void invokeVirtual(MethodId<D, R> method, Local<? super R> target, 6463e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson Local<? extends D> instance, Local<?>... args) { 6473e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson invoke(Rops.opInvokeVirtual(method.prototype(true)), method, target, instance, args); 648579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 649579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 6503e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 6513e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Calls {@code method} of {@code instance} using {@code args} and assigns 6523e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * the result to {@code target}. 6533e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * 6543e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * @param method either a private method or the superclass's constructor in 6553e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * a constructor's call to {@code super()}. 6563e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * @param target the local to receive the method's return value, or {@code 6573e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * null} if the return type is {@code void} or if its value not needed. 6583e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 659579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson public <D, R> void invokeDirect(MethodId<D, R> method, Local<? super R> target, 6603e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson Local<? extends D> instance, Local<?>... args) { 6613e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson invoke(Rops.opInvokeDirect(method.prototype(true)), method, target, instance, args); 662579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 663579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 6643e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 6653e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Calls the closest superclass's virtual method {@code method} of {@code 6663e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * instance} using {@code args} and assigns the result to {@code target}. 6673e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * 6683e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * @param target the local to receive the method's return value, or {@code 6693e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * null} if the return type is {@code void} or if its value not needed. 6703e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 671579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson public <D, R> void invokeSuper(MethodId<D, R> method, Local<? super R> target, 6723e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson Local<? extends D> instance, Local<?>... args) { 6733e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson invoke(Rops.opInvokeSuper(method.prototype(true)), method, target, instance, args); 674579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 675579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 6763e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 6773e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Calls the interface method {@code method} of {@code instance} using 6783e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * {@code args} and assigns the result to {@code target}. 6793e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * 6803e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * @param method a method declared on an interface. 6813e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * @param target the local to receive the method's return value, or {@code 6823e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * null} if the return type is {@code void} or if its value not needed. 6833e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 684579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson public <D, R> void invokeInterface(MethodId<D, R> method, Local<? super R> target, 6853e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson Local<? extends D> instance, Local<?>... args) { 6863e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson invoke(Rops.opInvokeInterface(method.prototype(true)), method, target, instance, args); 687579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 688579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 689579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson private <D, R> void invoke(Rop rop, MethodId<D, R> method, Local<? super R> target, 690579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson Local<? extends D> object, Local<?>... args) { 691579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson addInstruction(new ThrowingCstInsn(rop, sourcePosition, concatenate(object, args), 692579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson catches, method.constant)); 693579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson if (target != null) { 694579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson moveResult(target, false); 695579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 696579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 697579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 698579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson // instructions: types 699579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 7003e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 7013e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Tests if the value in {@code source} is assignable to {@code type}. If it 7023e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * is, {@code target} is assigned to 1; otherwise {@code target} is assigned 7033e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * to 0. 7043e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 7050e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson public void instanceOfType(Local<?> target, Local<?> source, TypeId<?> type) { 706579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson addInstruction(new ThrowingCstInsn(Rops.INSTANCE_OF, sourcePosition, 707579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson RegisterSpecList.make(source.spec()), catches, type.constant)); 708579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson moveResult(target, true); 709579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 710579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 71197b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson /** 71297b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * Performs either a numeric cast or a type cast. 71397b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * 71497b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * <h3>Numeric Casts</h3> 71597b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * Converts a primitive to a different representation. Numeric casts may 71697b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * be lossy. For example, converting the double {@code 1.8d} to an integer 71797b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * yields {@code 1}, losing the fractional part. Converting the integer 71897b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * {@code 0x12345678} to a short yields {@code 0x5678}, losing the high 71997b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * bytes. The following numeric casts are supported: 72097b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * 72197b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * <p><table border="1"> 72297b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * <tr><th>From</th><th>To</th></tr> 72397b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * <tr><td>int</td><td>byte, char, short, long, float, double</td></tr> 72497b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * <tr><td>long</td><td>int, float, double</td></tr> 72597b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * <tr><td>float</td><td>int, long, double</td></tr> 72697b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * <tr><td>double</td><td>int, long, float</td></tr> 72797b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * </table> 72897b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * 72997b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * <p>For some primitive conversions it will be necessary to chain multiple 73097b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * cast operations. For example, to go from float to short one would first 73197b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * cast float to int and then int to short. 73297b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * 73397b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * <p>Numeric casts never throw {@link ClassCastException}. 73497b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * 73597b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * <h3>Type Casts</h3> 73697b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * Checks that a reference value is assignable to the target type. If it is 73797b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * assignable it is copied to the target local. If it is not assignable a 73897b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson * {@link ClassCastException} is thrown. 73997b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson */ 74097b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson public void cast(Local<?> target, Local<?> source) { 74197b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson if (source.getType().ropType.isReference()) { 74297b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson addInstruction(new ThrowingCstInsn(Rops.CHECK_CAST, sourcePosition, 74397b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson RegisterSpecList.make(source.spec()), catches, target.type.constant)); 74497b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson moveResult(target, true); 74597b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson } else { 74697b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson addInstruction(new PlainInsn(getCastRop(source.type.ropType, target.type.ropType), 74797b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson sourcePosition, target.spec(), source.spec())); 74897b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson } 74997b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson } 75097b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson 75197b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson private Rop getCastRop(com.android.dx.rop.type.Type sourceType, 75297b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson com.android.dx.rop.type.Type targetType) { 75397b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson if (sourceType.getBasicType() == BT_INT) { 75497b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson switch (targetType.getBasicType()) { 75597b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson case BT_SHORT: 75697b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson return Rops.TO_SHORT; 75797b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson case BT_CHAR: 75897b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson return Rops.TO_CHAR; 75997b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson case BT_BYTE: 76097b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson return Rops.TO_BYTE; 76197b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson } 76297b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson } 76397b0be6b3da9df87e9026f880b0b0bffc7242450Jesse Wilson return Rops.opConv(targetType, sourceType); 764579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 765579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 766579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson // instructions: arrays 767579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 7683e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 7693e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Sets {@code target} to the length of the array in {@code array}. 7703e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 7710e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson public <T> void arrayLength(Local<Integer> target, Local<T> array) { 772579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson addInstruction(new ThrowingInsn(Rops.ARRAY_LENGTH, sourcePosition, 773579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson RegisterSpecList.make(array.spec()), catches)); 774579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson moveResult(target, true); 775579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 776579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 7773e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 7783e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Assigns {@code target} to a newly allocated array of length {@code 7793e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * length}. The array's type is the same as {@code target}'s type. 7803e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 7810e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson public <T> void newArray(Local<T> target, Local<Integer> length) { 782579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson addInstruction(new ThrowingCstInsn(Rops.opNewArray(target.type.ropType), sourcePosition, 783579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson RegisterSpecList.make(length.spec()), catches, target.type.constant)); 784579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson moveResult(target, true); 785579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 786579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 7873e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 7888ec4b1db3afa51730508be7064d2111b723ac2cdHugo Hudson * Assigns the element at {@code index} in {@code array} to {@code target}. 7893e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 7900e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson public void aget(Local<?> target, Local<?> array, Local<Integer> index) { 791579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson addInstruction(new ThrowingInsn(Rops.opAget(target.type.ropType), sourcePosition, 792579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson RegisterSpecList.make(array.spec(), index.spec()), catches)); 793579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson moveResult(target, true); 794579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 795579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 7963e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 7978ec4b1db3afa51730508be7064d2111b723ac2cdHugo Hudson * Assigns {@code source} to the element at {@code index} in {@code array}. 7983e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 799579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson public void aput(Local<?> array, Local<Integer> index, Local<?> source) { 800579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson addInstruction(new ThrowingInsn(Rops.opAput(source.type.ropType), sourcePosition, 801579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson RegisterSpecList.make(source.spec(), array.spec(), index.spec()), catches)); 802579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 803579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 804579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson // instructions: return 805579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 8063e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 8073e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Returns from a {@code void} method. After a return it is an error to 8083e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * define further instructions after a return without first {@link #mark 8093e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * marking} an existing unmarked label. 8103e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 811579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson public void returnVoid() { 8120e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson if (!method.returnType.equals(TypeId.VOID)) { 813579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson throw new IllegalArgumentException("declared " + method.returnType 814579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson + " but returned void"); 815579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 816579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson addInstruction(new PlainInsn(Rops.RETURN_VOID, sourcePosition, null, 817579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson RegisterSpecList.EMPTY)); 818579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 819579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 8203e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 8213e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Returns the value in {@code result} to the calling method. After a return 8223e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * it is an error to define further instructions after a return without 8233e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * first {@link #mark marking} an existing unmarked label. 8243e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 825579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson public void returnValue(Local<?> result) { 826579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson if (!result.type.equals(method.returnType)) { 827579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson // TODO: this is probably too strict. 828579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson throw new IllegalArgumentException("declared " + method.returnType 829579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson + " but returned " + result.type); 830579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 831579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson addInstruction(new PlainInsn(Rops.opReturn(result.type.ropType), sourcePosition, 832579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson null, RegisterSpecList.make(result.spec()))); 833579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 834579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 835579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson private void moveResult(Local<?> target, boolean afterNonInvokeThrowingInsn) { 836579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson Rop rop = afterNonInvokeThrowingInsn 837579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson ? Rops.opMoveResultPseudo(target.type.ropType) 838579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson : Rops.opMoveResult(target.type.ropType); 839579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson addInstruction(new PlainInsn(rop, sourcePosition, target.spec(), RegisterSpecList.EMPTY)); 840579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 841579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 8425624228626d7cdf206de25a6981ba8107be61057Jesse Wilson // instructions; synchronized 8435624228626d7cdf206de25a6981ba8107be61057Jesse Wilson 8443e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 8453e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Awaits the lock on {@code monitor}, and acquires it. 8463e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 8475624228626d7cdf206de25a6981ba8107be61057Jesse Wilson public void monitorEnter(Local<?> monitor) { 8485624228626d7cdf206de25a6981ba8107be61057Jesse Wilson addInstruction(new ThrowingInsn(Rops.MONITOR_ENTER, sourcePosition, 8495624228626d7cdf206de25a6981ba8107be61057Jesse Wilson RegisterSpecList.make(monitor.spec()), catches)); 8505624228626d7cdf206de25a6981ba8107be61057Jesse Wilson } 8515624228626d7cdf206de25a6981ba8107be61057Jesse Wilson 8523e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 8533e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Releases the held lock on {@code monitor}. 8543e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 8555624228626d7cdf206de25a6981ba8107be61057Jesse Wilson public void monitorExit(Local<?> monitor) { 8565624228626d7cdf206de25a6981ba8107be61057Jesse Wilson addInstruction(new ThrowingInsn(Rops.MONITOR_ENTER, sourcePosition, 8575624228626d7cdf206de25a6981ba8107be61057Jesse Wilson RegisterSpecList.make(monitor.spec()), catches)); 8585624228626d7cdf206de25a6981ba8107be61057Jesse Wilson } 8595624228626d7cdf206de25a6981ba8107be61057Jesse Wilson 860579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson // produce BasicBlocks for dex 861579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 862579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson BasicBlockList toBasicBlocks() { 863579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson if (!localsInitialized) { 864579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson initializeLocals(); 865579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 866579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 867579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson cleanUpLabels(); 868579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 869579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson BasicBlockList result = new BasicBlockList(labels.size()); 870579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson for (int i = 0; i < labels.size(); i++) { 871579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson result.set(i, labels.get(i).toBasicBlock()); 872579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 873579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson return result; 874579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 875579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 876579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson /** 877579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * Removes empty labels and assigns IDs to non-empty labels. 878579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson */ 879579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson private void cleanUpLabels() { 880579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson int id = 0; 881579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson for (Iterator<Label> i = labels.iterator(); i.hasNext();) { 882579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson Label label = i.next(); 883579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson if (label.isEmpty()) { 884579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson i.remove(); 885579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } else { 886579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson label.compact(); 887579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson label.id = id++; 888579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 889579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 890579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 891579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 892579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson private static RegisterSpecList concatenate(Local<?> first, Local<?>[] rest) { 893579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson int offset = (first != null) ? 1 : 0; 894579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson RegisterSpecList result = new RegisterSpecList(offset + rest.length); 895579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson if (first != null) { 896579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson result.set(0, first.spec()); 897579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 898579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson for (int i = 0; i < rest.length; i++) { 899579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson result.set(i + offset, rest[i].spec()); 900579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 901579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson return result; 902579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 903579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson} 904