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;
22import com.android.dx.ssa.RegisterMapper;
23
24/**
25 * Pseudo-instruction which is used to introduce a new local variable. That
26 * is, an instance of this class in an instruction stream indicates that
27 * starting with the subsequent instruction, the indicated variable
28 * is bound.
29 */
30public final class LocalStart extends ZeroSizeInsn {
31    /**
32     * {@code non-null;} register spec representing the local variable introduced
33     * by this instance
34     */
35    private final RegisterSpec local;
36
37    /**
38     * Returns the local variable listing string for a single register spec.
39     *
40     * @param spec {@code non-null;} the spec to convert
41     * @return {@code non-null;} the string form
42     */
43    public static String localString(RegisterSpec spec) {
44        return spec.regString() + ' ' + spec.getLocalItem().toString() + ": " +
45            spec.getTypeBearer().toHuman();
46    }
47
48    /**
49     * Constructs an instance. The output address of this instance is initially
50     * unknown ({@code -1}).
51     *
52     * @param position {@code non-null;} source position
53     * @param local {@code non-null;} register spec representing the local
54     * variable introduced by this instance
55     */
56    public LocalStart(SourcePosition position, RegisterSpec local) {
57        super(position);
58
59        if (local == null) {
60            throw new NullPointerException("local == null");
61        }
62
63        this.local = local;
64    }
65
66    /** {@inheritDoc} */
67    @Override
68    public DalvInsn withRegisterOffset(int delta) {
69        return new LocalStart(getPosition(), local.withOffset(delta));
70    }
71
72    /** {@inheritDoc} */
73    @Override
74    public DalvInsn withRegisters(RegisterSpecList registers) {
75        return new LocalStart(getPosition(), local);
76    }
77
78    /**
79     * Gets the register spec representing the local variable introduced
80     * by this instance.
81     *
82     * @return {@code non-null;} the register spec
83     */
84    public RegisterSpec getLocal() {
85        return local;
86    }
87
88    /** {@inheritDoc} */
89    @Override
90    protected String argString() {
91        return local.toString();
92    }
93
94    /** {@inheritDoc} */
95    @Override
96    protected String listingString0(boolean noteIndices) {
97        return "local-start " + localString(local);
98    }
99
100    /** {@inheritDoc} */
101    @Override
102    public DalvInsn withMapper(RegisterMapper mapper) {
103      return new LocalStart(getPosition(), mapper.map(local));
104    }
105}
106