001/*
002 *  This file is part of the Jikes RVM project (http://jikesrvm.org).
003 *
004 *  This file is licensed to You under the Eclipse Public License (EPL);
005 *  You may not use this file except in compliance with the License. You
006 *  may obtain a copy of the License at
007 *
008 *      http://www.opensource.org/licenses/eclipse-1.0.php
009 *
010 *  See the COPYRIGHT.txt file distributed with this work for information
011 *  regarding copyright ownership.
012 */
013package org.jikesrvm.osr.bytecodes;
014
015import static org.jikesrvm.classloader.BytecodeConstants.JBC_goto;
016import static org.jikesrvm.classloader.BytecodeConstants.JBC_goto_w;
017
018/**
019 * goto instruction
020 */
021public class Goto extends PseudoBytecode {
022  private int offset;
023  private byte[] codes;
024  private int bsize;
025
026  public Goto(int off) {
027    this.offset = off;
028    adjustFields();
029  }
030
031  @Override
032  public byte[] getBytes() {
033    return codes;
034  }
035
036  @Override
037  public int getSize() {
038    return bsize;
039  }
040
041  public int getOffset() {
042    return this.offset;
043  }
044
045  @Override
046  public int stackChanges() {
047    return 0;
048  }
049
050  public void patch(int off) {
051    this.offset = off;
052    adjustFields();
053  }
054
055  private void adjustFields() {
056    if ((offset >= -32768) && (offset <= 32767)) {
057      bsize = 3;
058      codes = new byte[3];
059      codes[0] = (byte) JBC_goto;
060      codes[1] = (byte) (offset >> 8);
061      codes[2] = (byte) (offset & 0xFF);
062    } else {
063      bsize = 5;
064      codes = new byte[5];
065      codes[0] = (byte) JBC_goto_w;
066      int2bytes(codes, 1, offset);
067    }
068  }
069
070  @Override
071  public String toString() {
072    return "goto " + this.offset;
073  }
074}