001/*
002 * Copyright (c) 2015, 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 jdk.internal.jvmci.code;
024
025/**
026 * Represents a location where a value can be stored. This can be either a {@link Register} or a
027 * stack slot.
028 */
029public final class Location {
030
031    public final Register reg;
032    public final int offset;
033
034    private Location(Register reg, int offset) {
035        this.reg = reg;
036        this.offset = offset;
037    }
038
039    /**
040     * Create a {@link Location} for a register.
041     */
042    public static Location register(Register reg) {
043        return new Location(reg, 0);
044    }
045
046    /**
047     * Create a {@link Location} for a vector subregister.
048     *
049     * @param reg the {@link Register vector register}
050     * @param offset the offset in bytes into the vector register
051     */
052    public static Location subregister(Register reg, int offset) {
053        return new Location(reg, offset);
054    }
055
056    /**
057     * Create a {@link Location} for a stack slot.
058     */
059    public static Location stack(int offset) {
060        return new Location(null, offset);
061    }
062
063    public boolean isRegister() {
064        return reg != null;
065    }
066
067    public boolean isStack() {
068        return reg == null;
069    }
070
071    @Override
072    public String toString() {
073        String regName;
074        if (isRegister()) {
075            regName = reg.name + ":";
076        } else {
077            regName = "stack:";
078        }
079        return regName + offset;
080    }
081}