summaryrefslogtreecommitdiffstats
path: root/dx/src/com/android/dx/cf/code/BaseMachine.java
blob: 7bff2ff2f0c9c6af49c034fe974d88a8ee96f41f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.dx.cf.code;

import com.android.dx.rop.code.LocalItem;
import com.android.dx.rop.code.RegisterSpec;
import com.android.dx.rop.cst.Constant;
import com.android.dx.rop.type.Prototype;
import com.android.dx.rop.type.StdTypeList;
import com.android.dx.rop.type.Type;
import com.android.dx.rop.type.TypeBearer;
import java.util.ArrayList;

/**
 * Base implementation of {@link Machine}.
 *
 * <p><b>Note:</b> For the most part, the documentation for this class
 * ignores the distinction between {@link Type} and {@link
 * TypeBearer}.</p>
 */
public abstract class BaseMachine implements Machine {
    /* {@code non-null;} the prototype for the associated method */
    private final Prototype prototype;

    /** {@code non-null;} primary arguments */
    private TypeBearer[] args;

    /** {@code >= 0;} number of primary arguments */
    private int argCount;

    /** {@code null-ok;} type of the operation, if salient */
    private Type auxType;

    /** auxiliary {@code int} argument */
    private int auxInt;

    /** {@code null-ok;} auxiliary constant argument */
    private Constant auxCst;

    /** auxiliary branch target argument */
    private int auxTarget;

    /** {@code null-ok;} auxiliary switch cases argument */
    private SwitchList auxCases;

    /** {@code null-ok;} auxiliary initial value list for newarray */
    private ArrayList<Constant> auxInitValues;

    /** {@code >= -1;} last local accessed */
    private int localIndex;

    /** specifies if local has info in the local variable table */
    private boolean localInfo;

    /** {@code null-ok;} local target spec, if salient and calculated */
    private RegisterSpec localTarget;

    /** {@code non-null;} results */
    private TypeBearer[] results;

    /**
     * {@code >= -1;} count of the results, or {@code -1} if no results
     * have been set
     */
    private int resultCount;

    /**
     * Constructs an instance.
     *
     * @param prototype {@code non-null;} the prototype for the
     * associated method
     */
    public BaseMachine(Prototype prototype) {
        if (prototype == null) {
            throw new NullPointerException("prototype == null");
        }

        this.prototype = prototype;
        args = new TypeBearer[10];
        results = new TypeBearer[6];
        clearArgs();
    }

    /** {@inheritDoc} */
    public Prototype getPrototype() {
        return prototype;
    }

    /** {@inheritDoc} */
    public final void clearArgs() {
        argCount = 0;
        auxType = null;
        auxInt = 0;
        auxCst = null;
        auxTarget = 0;
        auxCases = null;
        auxInitValues = null;
        localIndex = -1;
        localInfo = false;
        localTarget = null;
        resultCount = -1;
    }

    /** {@inheritDoc} */
    public final void popArgs(Frame frame, int count) {
        ExecutionStack stack = frame.getStack();

        clearArgs();

        if (count > args.length) {
            // Grow args, and add a little extra room to grow even more.
            args = new TypeBearer[count + 10];
        }

        for (int i = count - 1; i >= 0; i--) {
            args[i] = stack.pop();
        }

        argCount = count;
    }

    /** {@inheritDoc} */
    public void popArgs(Frame frame, Prototype prototype) {
        StdTypeList types = prototype.getParameterTypes();
        int size = types.size();

        // Use the above method to do the actual popping...
        popArgs(frame, size);

        // ...and then verify the popped types.

        for (int i = 0; i < size; i++) {
            if (! Merger.isPossiblyAssignableFrom(types.getType(i), args[i])) {
                throw new SimException("at stack depth " + (size - 1 - i) +
                        ", expected type " + types.getType(i).toHuman() +
                        " but found " + args[i].getType().toHuman());
            }
        }
    }

    public final void popArgs(Frame frame, Type type) {
        // Use the above method to do the actual popping...
        popArgs(frame, 1);

        // ...and then verify the popped type.
        if (! Merger.isPossiblyAssignableFrom(type, args[0])) {
            throw new SimException("expected type " + type.toHuman() +
                    " but found " + args[0].getType().toHuman());
        }
    }

    /** {@inheritDoc} */
    public final void popArgs(Frame frame, Type type1, Type type2) {
        // Use the above method to do the actual popping...
        popArgs(frame, 2);

        // ...and then verify the popped types.

        if (! Merger.isPossiblyAssignableFrom(type1, args[0])) {
            throw new SimException("expected type " + type1.toHuman() +
                    " but found " + args[0].getType().toHuman());
        }

        if (! Merger.isPossiblyAssignableFrom(type2, args[1])) {
            throw new SimException("expected type " + type2.toHuman() +
                    " but found " + args[1].getType().toHuman());
        }
    }

    /** {@inheritDoc} */
    public final void popArgs(Frame frame, Type type1, Type type2,
            Type type3) {
        // Use the above method to do the actual popping...
        popArgs(frame, 3);

        // ...and then verify the popped types.

        if (! Merger.isPossiblyAssignableFrom(type1, args[0])) {
            throw new SimException("expected type " + type1.toHuman() +
                    " but found " + args[0].getType().toHuman());
        }

        if (! Merger.isPossiblyAssignableFrom(type2, args[1])) {
            throw new SimException("expected type " + type2.toHuman() +
                    " but found " + args[1].getType().toHuman());
        }

        if (! Merger.isPossiblyAssignableFrom(type3, args[2])) {
            throw new SimException("expected type " + type3.toHuman() +
                    " but found " + args[2].getType().toHuman());
        }
    }

    /** {@inheritDoc} */
    public final void localArg(Frame frame, int idx) {
        clearArgs();
        args[0] = frame.getLocals().get(idx);
        argCount = 1;
        localIndex = idx;
    }

    /** {@inheritDoc} */
    public final void localInfo(boolean local) {
        localInfo = local;
    }

    /** {@inheritDoc} */
    public final void auxType(Type type) {
        auxType = type;
    }

    /** {@inheritDoc} */
    public final void auxIntArg(int value) {
        auxInt = value;
    }

    /** {@inheritDoc} */
    public final void auxCstArg(Constant cst) {
        if (cst == null) {
            throw new NullPointerException("cst == null");
        }

        auxCst = cst;
    }

    /** {@inheritDoc} */
    public final void auxTargetArg(int target) {
        auxTarget = target;
    }

    /** {@inheritDoc} */
    public final void auxSwitchArg(SwitchList cases) {
        if (cases == null) {
            throw new NullPointerException("cases == null");
        }

        auxCases = cases;
    }

    /** {@inheritDoc} */
    public final void auxInitValues(ArrayList<Constant> initValues) {
        auxInitValues = initValues;
    }

    /** {@inheritDoc} */
    public final void localTarget(int idx, Type type, LocalItem local) {
        localTarget = RegisterSpec.makeLocalOptional(idx, type, local);
    }

    /**
     * Gets the number of primary arguments.
     *
     * @return {@code >= 0;} the number of primary arguments
     */
    protected final int argCount() {
        return argCount;
    }

    /**
     * Gets the width of the arguments (where a category-2 value counts as
     * two).
     *
     * @return {@code >= 0;} the argument width
     */
    protected final int argWidth() {
        int result = 0;

        for (int i = 0; i < argCount; i++) {
            result += args[i].getType().getCategory();
        }

        return result;
    }

    /**
     * Gets the {@code n}th primary argument.
     *
     * @param n {@code >= 0, < argCount();} which argument
     * @return {@code non-null;} the indicated argument
     */
    protected final TypeBearer arg(int n) {
        if (n >= argCount) {
            throw new IllegalArgumentException("n >= argCount");
        }

        try {
            return args[n];
        } catch (ArrayIndexOutOfBoundsException ex) {
            // Translate the exception.
            throw new IllegalArgumentException("n < 0");
        }
    }

    /**
     * Gets the type auxiliary argument.
     *
     * @return {@code null-ok;} the salient type
     */
    protected final Type getAuxType() {
        return auxType;
    }

    /**
     * Gets the {@code int} auxiliary argument.
     *
     * @return the argument value
     */
    protected final int getAuxInt() {
        return auxInt;
    }

    /**
     * Gets the constant auxiliary argument.
     *
     * @return {@code null-ok;} the argument value
     */
    protected final Constant getAuxCst() {
        return auxCst;
    }

    /**
     * Gets the branch target auxiliary argument.
     *
     * @return the argument value
     */
    protected final int getAuxTarget() {
        return auxTarget;
    }

    /**
     * Gets the switch cases auxiliary argument.
     *
     * @return {@code null-ok;} the argument value
     */
    protected final SwitchList getAuxCases() {
        return auxCases;
    }

    /**
     * Gets the init values auxiliary argument.
     *
     * @return {@code null-ok;} the argument value
     */
    protected final ArrayList<Constant> getInitValues() {
        return auxInitValues;
    }
    /**
     * Gets the last local index accessed.
     *
     * @return {@code >= -1;} the salient local index or {@code -1} if none
     * was set since the last time {@link #clearArgs} was called
     */
    protected final int getLocalIndex() {
        return localIndex;
    }

    /**
     * Gets whether the loaded local has info in the local variable table.
     *
     * @return {@code true} if local arg has info in the local variable table
     */
    protected final boolean getLocalInfo() {
        return localInfo;
    }

    /**
     * Gets the target local register spec of the current operation, if any.
     * The local target spec is the combination of the values indicated
     * by a previous call to {@link #localTarget} with the type of what
     * should be the sole result set by a call to {@link #setResult} (or
     * the combination {@link #clearResult} then {@link #addResult}.
     *
     * @param isMove {@code true} if the operation being performed on the
     * local is a move. This will cause constant values to be propagated
     * to the returned local
     * @return {@code null-ok;} the salient register spec or {@code null} if no
     * local target was set since the last time {@link #clearArgs} was
     * called
     */
    protected final RegisterSpec getLocalTarget(boolean isMove) {
        if (localTarget == null) {
            return null;
        }

        if (resultCount != 1) {
            throw new SimException("local target with " +
                    ((resultCount == 0) ? "no" : "multiple") + " results");
        }

        TypeBearer result = results[0];
        Type resultType = result.getType();
        Type localType = localTarget.getType();

        if (resultType == localType) {
            /*
             * If this is to be a move operation and the result is a
             * known value, make the returned localTarget embody that
             * value.
             */
            if (isMove) {
                return localTarget.withType(result);
            } else {
                return localTarget;
            }
        }

        if (! Merger.isPossiblyAssignableFrom(localType, resultType)) {
            // The result and local types are inconsistent. Complain!
            throwLocalMismatch(resultType, localType);
            return null;
        }

        if (localType == Type.OBJECT) {
            /*
             * The result type is more specific than the local type,
             * so use that instead.
             */
            localTarget = localTarget.withType(result);
        }

        return localTarget;
    }

    /**
     * Clears the results.
     */
    protected final void clearResult() {
        resultCount = 0;
    }

    /**
     * Sets the results list to be the given single value.
     *
     * <p><b>Note:</b> If there is more than one result value, the
     * others may be added by using {@link #addResult}.</p>
     *
     * @param result {@code non-null;} result value
     */
    protected final void setResult(TypeBearer result) {
        if (result == null) {
            throw new NullPointerException("result == null");
        }

        results[0] = result;
        resultCount = 1;
    }

    /**
     * Adds an additional element to the list of results.
     *
     * @see #setResult
     *
     * @param result {@code non-null;} result value
     */
    protected final void addResult(TypeBearer result) {
        if (result == null) {
            throw new NullPointerException("result == null");
        }

        results[resultCount] = result;
        resultCount++;
    }

    /**
     * Gets the count of results. This throws an exception if results were
     * never set. (Explicitly clearing the results counts as setting them.)
     *
     * @return {@code >= 0;} the count
     */
    protected final int resultCount() {
        if (resultCount < 0) {
            throw new SimException("results never set");
        }

        return resultCount;
    }

    /**
     * Gets the width of the results (where a category-2 value counts as
     * two).
     *
     * @return {@code >= 0;} the result width
     */
    protected final int resultWidth() {
        int width = 0;

        for (int i = 0; i < resultCount; i++) {
            width += results[i].getType().getCategory();
        }

        return width;
    }

    /**
     * Gets the {@code n}th result value.
     *
     * @param n {@code >= 0, < resultCount();} which result
     * @return {@code non-null;} the indicated result value
     */
    protected final TypeBearer result(int n) {
        if (n >= resultCount) {
            throw new IllegalArgumentException("n >= resultCount");
        }

        try {
            return results[n];
        } catch (ArrayIndexOutOfBoundsException ex) {
            // Translate the exception.
            throw new IllegalArgumentException("n < 0");
        }
    }

    /**
     * Stores the results of the latest operation into the given frame. If
     * there is a local target (see {@link #localTarget}), then the sole
     * result is stored to that target; otherwise any results are pushed
     * onto the stack.
     *
     * @param frame {@code non-null;} frame to operate on
     */
    protected final void storeResults(Frame frame) {
        if (resultCount < 0) {
            throw new SimException("results never set");
        }

        if (resultCount == 0) {
            // Nothing to do.
            return;
        }

        if (localTarget != null) {
            /*
             * Note: getLocalTarget() doesn't necessarily return
             * localTarget directly.
             */
            frame.getLocals().set(getLocalTarget(false));
        } else {
            ExecutionStack stack = frame.getStack();
            for (int i = 0; i < resultCount; i++) {
                if (localInfo) {
                    stack.setLocal();
                }
                stack.push(results[i]);
            }
        }
    }

    /**
     * Throws an exception that indicates a mismatch in local variable
     * types.
     *
     * @param found {@code non-null;} the encountered type
     * @param local {@code non-null;} the local variable's claimed type
     */
    public static void throwLocalMismatch(TypeBearer found,
            TypeBearer local) {
        throw new SimException("local variable type mismatch: " +
                "attempt to set or access a value of type " +
                found.toHuman() +
                " using a local variable of type " +
                local.toHuman() +
                ". This is symptomatic of .class transformation tools " +
                "that ignore local variable information.");
    }
}