1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.dx.dex.code;
18
19import com.android.dx.rop.code.RegisterSpec;
20import com.android.dx.rop.code.RegisterSpecList;
21import com.android.dx.rop.code.SourcePosition;
22
23/**
24 * Pseudo-instruction which is used to introduce a new local variable. That
25 * is, an instance of this class in an instruction stream indicates that
26 * starting with the subsequent instruction, the indicated variable
27 * is bound.
28 */
29public final class LocalStart extends ZeroSizeInsn {
30    /**
31     * {@code non-null;} register spec representing the local variable introduced
32     * by this instance
33     */
34    private final RegisterSpec local;
35
36    /**
37     * Returns the local variable listing string for a single register spec.
38     *
39     * @param spec {@code non-null;} the spec to convert
40     * @return {@code non-null;} the string form
41     */
42    public static String localString(RegisterSpec spec) {
43        return spec.regString() + ' ' + spec.getLocalItem().toString() + ": " +
44            spec.getTypeBearer().toHuman();
45    }
46
47    /**
48     * Constructs an instance. The output address of this instance is initially
49     * unknown ({@code -1}).
50     *
51     * @param position {@code non-null;} source position
52     * @param local {@code non-null;} register spec representing the local
53     * variable introduced by this instance
54     */
55    public LocalStart(SourcePosition position, RegisterSpec local) {
56        super(position);
57
58        if (local == null) {
59            throw new NullPointerException("local == null");
60        }
61
62        this.local = local;
63    }
64
65    /** {@inheritDoc} */
66    @Override
67    public DalvInsn withRegisterOffset(int delta) {
68        return new LocalStart(getPosition(), local.withOffset(delta));
69    }
70
71    /** {@inheritDoc} */
72    @Override
73    public DalvInsn withRegisters(RegisterSpecList registers) {
74        return new LocalStart(getPosition(), local);
75    }
76
77    /**
78     * Gets the register spec representing the local variable introduced
79     * by this instance.
80     *
81     * @return {@code non-null;} the register spec
82     */
83    public RegisterSpec getLocal() {
84        return local;
85    }
86
87    /** {@inheritDoc} */
88    @Override
89    protected String argString() {
90        return local.toString();
91    }
92
93    /** {@inheritDoc} */
94    @Override
95    protected String listingString0(boolean noteIndices) {
96        return "local-start " + localString(local);
97    }
98}
99