001/*
002 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.lir.stackslotalloc;
024
025import jdk.internal.jvmci.code.*;
026import jdk.internal.jvmci.meta.*;
027
028public final class StackInterval {
029
030    private static final int INVALID_START = Integer.MAX_VALUE;
031    private static final int INVALID_END = Integer.MIN_VALUE;
032    private final VirtualStackSlot operand;
033    private StackInterval hint;
034    private final LIRKind kind;
035    private int from = INVALID_START;
036    private int to = INVALID_END;
037    private StackSlot location;
038
039    public StackInterval(VirtualStackSlot operand, LIRKind kind) {
040        this.operand = operand;
041        this.kind = kind;
042    }
043
044    public boolean verify(int maxOpId) {
045        // maxOpId + 1 is the last position in the last block (i.e. the "write position")
046        assert 0 <= from && from <= to && to <= maxOpId + 1 : String.format("from %d, to %d, maxOpId %d", from, to, maxOpId);
047        return true;
048    }
049
050    public VirtualStackSlot getOperand() {
051        return operand;
052    }
053
054    public void addTo(int opId) {
055        if (opId >= to) {
056            to = opId;
057        }
058    }
059
060    protected void addFrom(int opId) {
061        if (from > opId) {
062            from = opId;
063            // set opId also as to if it has not yet been set
064            if (to == INVALID_END) {
065                to = opId;
066            }
067        }
068    }
069
070    public LIRKind kind() {
071        return kind;
072    }
073
074    public StackSlot location() {
075        return location;
076    }
077
078    public void setLocation(StackSlot location) {
079        this.location = location;
080    }
081
082    public int from() {
083        return from;
084    }
085
086    public int to() {
087        return to;
088    }
089
090    public void fixFrom() {
091        if (from == INVALID_START) {
092            from = 0;
093        }
094    }
095
096    public boolean isFixed() {
097        return from == 0;
098    }
099
100    @Override
101    public String toString() {
102        return String.format("SI[%d-%d] k=%s o=%s l=%s h=%s", from, to, kind, operand, location, hint != null ? hint.getOperand() : "null");
103    }
104
105    public void setLocationHint(StackInterval locationHint) {
106        hint = locationHint;
107    }
108
109    public StackInterval locationHint() {
110        return hint;
111    }
112
113}