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_impdep1;
016import static org.jikesrvm.classloader.BytecodeConstants.JBC_wide;
017import static org.jikesrvm.runtime.JavaSizeConstants.LOG_BITS_IN_BYTE;
018import static org.jikesrvm.runtime.UnboxedSizeConstants.BYTES_IN_ADDRESS;
019
020import org.vmmagic.unboxed.Word;
021
022/**
023 * OSR_PseudoBytecode is super class of all pseudo instructions.
024 */
025public abstract class PseudoBytecode {
026
027  public PseudoBytecode next;
028
029  public abstract byte[] getBytes();
030
031  public abstract int getSize();
032
033  public abstract int stackChanges();
034
035  public static byte[] initBytes(int size, int instruction) {
036    byte[] code = new byte[size];
037    code[0] = (byte) JBC_impdep1;
038    code[1] = (byte) instruction;
039    return code;
040  }
041
042  public static void int2bytes(byte[] to, int p, int value) {
043
044    for (int i = 3; i >= 0; i--) {
045      to[p++] = (byte) ((value >>> (i << LOG_BITS_IN_BYTE)) & 0x0FF);
046    }
047  }
048
049  public static void long2bytes(byte[] to, int p, long value) {
050
051    for (int i = 7; i >= 0; i--) {
052      to[p++] = (byte) ((value >>> (i << LOG_BITS_IN_BYTE)) & 0x0FF);
053    }
054  }
055
056  public static void word2bytes(byte[] to, int p, Word value) {
057
058    for (int i = BYTES_IN_ADDRESS - 1; i >= 0; i--) {
059      to[p++] = (byte) (value.rshl(i << LOG_BITS_IN_BYTE).toInt() & 0x0FF);
060    }
061  }
062
063  public static void float2bytes(byte[] to, int p, float value) {
064
065    int v = Float.floatToIntBits(value);
066    int2bytes(to, p, v);
067  }
068
069  public static void double2bytes(byte[] to, int p, double value) {
070
071    long v = Double.doubleToLongBits(value);
072    long2bytes(to, p, v);
073  }
074
075  public static byte[] makeOUUcode(int op, int idx) {
076
077    byte[] codes = new byte[3];
078    codes[0] = (byte) op;
079    codes[1] = (byte) ((idx >> 8) & 0x0FF);
080    codes[2] = (byte) (idx & 0x0FF);
081
082    return codes;
083  }
084
085  public static byte[] makeWOUUcode(int op, int idx) {
086    byte[] codes = new byte[4];
087    codes[0] = (byte) JBC_wide;
088    codes[1] = (byte) op;
089    codes[2] = (byte) ((idx >> 8) & 0x0FF);
090    codes[3] = (byte) (idx & 0x0FF);
091    return codes;
092  }
093
094  public static byte[] makeOUcode(int op, int idx) {
095    byte[] codes = new byte[2];
096    codes[0] = (byte) op;
097    codes[1] = (byte) (idx & 0x0FF);
098    return codes;
099  }
100}