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