1/*
2 * Copyright (c) 2014, 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 */
23package org.graalvm.compiler.lir.stackslotalloc;
24
25import org.graalvm.compiler.lir.VirtualStackSlot;
26
27import jdk.vm.ci.code.StackSlot;
28import jdk.vm.ci.meta.ValueKind;
29
30public final class StackInterval {
31
32    private static final int INVALID_START = Integer.MAX_VALUE;
33    private static final int INVALID_END = Integer.MIN_VALUE;
34    private final VirtualStackSlot operand;
35    private StackInterval hint;
36    private final ValueKind<?> kind;
37    private int from = INVALID_START;
38    private int to = INVALID_END;
39    private StackSlot location;
40
41    public StackInterval(VirtualStackSlot operand, ValueKind<?> kind) {
42        this.operand = operand;
43        this.kind = kind;
44    }
45
46    public boolean verify(int maxOpId) {
47        // maxOpId + 1 is the last position in the last block (i.e. the "write position")
48        assert 0 <= from && from <= to && to <= maxOpId + 1 : String.format("from %d, to %d, maxOpId %d", from, to, maxOpId);
49        return true;
50    }
51
52    public VirtualStackSlot getOperand() {
53        return operand;
54    }
55
56    public void addTo(int opId) {
57        if (opId >= to) {
58            to = opId;
59        }
60    }
61
62    protected void addFrom(int opId) {
63        if (from > opId) {
64            from = opId;
65            // set opId also as to if it has not yet been set
66            if (to == INVALID_END) {
67                to = opId;
68            }
69        }
70    }
71
72    public ValueKind<?> kind() {
73        return kind;
74    }
75
76    public StackSlot location() {
77        return location;
78    }
79
80    public void setLocation(StackSlot location) {
81        this.location = location;
82    }
83
84    public int from() {
85        return from;
86    }
87
88    public int to() {
89        return to;
90    }
91
92    public void fixFrom() {
93        if (from == INVALID_START) {
94            from = 0;
95        }
96    }
97
98    public boolean isFixed() {
99        return from == 0;
100    }
101
102    @Override
103    public String toString() {
104        return String.format("SI[%d-%d] k=%s o=%s l=%s h=%s", from, to, kind, operand, location, hint != null ? hint.getOperand() : "null");
105    }
106
107    public void setLocationHint(StackInterval locationHint) {
108        hint = locationHint;
109    }
110
111    public StackInterval locationHint() {
112        return hint;
113    }
114
115}
116