1/*
2 * Copyright (C) 2008 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.dexgen.rop.code;
18
19import com.android.dexgen.rop.cst.Constant;
20import com.android.dexgen.rop.type.StdTypeList;
21import com.android.dexgen.rop.type.Type;
22import com.android.dexgen.rop.type.TypeList;
23
24import java.util.ArrayList;
25
26/**
27 * Instruction which fills a newly created array with a predefined list of
28 * constant values.
29 */
30public final class FillArrayDataInsn
31        extends Insn {
32
33    /** non-null: initial values to fill the newly created array */
34    private final ArrayList<Constant> initValues;
35
36    /**
37     * non-null: type of the array. Will be used to determine the width of
38     * elements in the array-data table.
39     */
40    private final Constant arrayType;
41
42    /**
43     * Constructs an instance.
44     *
45     * @param opcode {@code non-null;} the opcode
46     * @param position {@code non-null;} source position
47     * @param sources {@code non-null;} specs for all the sources
48     * @param initValues {@code non-null;} list of initial values to fill the array
49     * @param cst {@code non-null;} type of the new array
50     */
51    public FillArrayDataInsn(Rop opcode, SourcePosition position,
52                             RegisterSpecList sources,
53                             ArrayList<Constant> initValues,
54                             Constant cst) {
55        super(opcode, position, null, sources);
56
57        if (opcode.getBranchingness() != Rop.BRANCH_NONE) {
58            throw new IllegalArgumentException("bogus branchingness");
59        }
60
61        this.initValues = initValues;
62        this.arrayType = cst;
63    }
64
65
66    /** {@inheritDoc} */
67    @Override
68    public TypeList getCatches() {
69        return StdTypeList.EMPTY;
70    }
71
72    /**
73     * Return the list of init values
74     * @return {@code non-null;} list of init values
75     */
76    public ArrayList<Constant> getInitValues() {
77        return initValues;
78    }
79
80    /**
81     * Return the type of the newly created array
82     * @return {@code non-null;} array type
83     */
84    public Constant getConstant() {
85        return arrayType;
86    }
87
88    /** {@inheritDoc} */
89    @Override
90    public void accept(Visitor visitor) {
91        visitor.visitFillArrayDataInsn(this);
92    }
93
94    /** {@inheritDoc} */
95    @Override
96    public Insn withAddedCatch(Type type) {
97        throw new  UnsupportedOperationException("unsupported");
98    }
99
100    /** {@inheritDoc} */
101    @Override
102    public Insn withRegisterOffset(int delta) {
103        return new FillArrayDataInsn(getOpcode(), getPosition(),
104                                     getSources().withOffset(delta),
105                                     initValues, arrayType);
106    }
107
108    /** {@inheritDoc} */
109    @Override
110    public Insn withNewRegisters(RegisterSpec result,
111            RegisterSpecList sources) {
112
113        return new FillArrayDataInsn(getOpcode(), getPosition(),
114                                     sources, initValues, arrayType);
115    }
116}
117