1/*
2 * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24package jdk.test.lib.jittester.arrays;
25
26import java.util.ArrayList;
27import java.util.List;
28import java.util.stream.Collectors;
29import jdk.test.lib.jittester.IRNode;
30import jdk.test.lib.jittester.Literal;
31import jdk.test.lib.jittester.LocalVariable;
32import jdk.test.lib.jittester.types.TypeArray;
33import jdk.test.lib.jittester.visitors.Visitor;
34
35
36/*
37Array extraction produces and array with N dimentions from an array with M
38dimentions, where N < M.
39 */
40public class ArrayExtraction extends IRNode {
41    private final List<Byte> dims;
42    public ArrayExtraction(IRNode array, ArrayList<IRNode> dimensionExpressions) {
43        super(array.getResultType());
44        addChild(array);
45        addChildren(dimensionExpressions);
46        if (array instanceof ArrayCreation) {
47            dims = new ArrayList<>();
48            ArrayCreation ac = (ArrayCreation) array;
49            for (int i = dimensionExpressions.size(); i < ac.getDimensionsCount(); ++i) {
50                dims.add(ac.getDimensionSize(i));
51            }
52        } else if (array instanceof ArrayExtraction) {
53            dims = new ArrayList<>();
54            ArrayExtraction ae = (ArrayExtraction) array;
55            for (int i = dimensionExpressions.size(); i < ae.getDimsNumber(); ++i) {
56                dims.add(ae.getDim(i));
57            }
58        } else if (array instanceof LocalVariable) {
59            LocalVariable loc = (LocalVariable) array;
60            TypeArray type = (TypeArray) loc.getVariableInfo().type;
61            dims = type.getDims();
62            for (int i = dimensionExpressions.size(); i < type.dimensions; ++i) {
63                dims.add(type.getDims().get(i));
64            }
65        } else {
66            dims = dimensionExpressions.stream()
67                .map(d -> {
68                    if (d instanceof Literal) {
69                        Literal n = (Literal) d;
70                        return (Byte)n.getValue();
71                    }
72                    return (byte)0;
73                })
74                .collect(Collectors.toList());
75        }
76    }
77
78    @Override
79    public<T> T accept(Visitor<T> v) {
80        return v.visit(this);
81    }
82
83    public byte getDim(int dim) {
84        return dims.get(dim);
85    }
86
87    public int getDimsNumber() {
88        return dims.size();
89    }
90}
91