1 /*
   2  * Copyright (c) 2009, 2012, 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  */
  23 
  24 package com.oracle.graal.asm.hsail;
  25 
  26 import com.oracle.graal.api.code.*;
  27 
  28 import static com.oracle.graal.api.code.ValueUtil.*;
  29 import com.oracle.graal.api.meta.*;
  30 import com.oracle.graal.hsail.*;
  31 import com.oracle.graal.graph.GraalInternalError;
  32 import com.amd.okra.OkraUtil;
  33 
  34 /**
  35  * This class contains routines to emit HSAIL assembly code.
  36  */
  37 public class HSAILAssembler extends AbstractHSAILAssembler {
  38 
  39     /**
  40      * Stack size in bytes (used to keep track of spilling).
  41      */
  42     private int maxDataTypeSize;
  43 
  44     /**
  45      * Maximum stack offset used by a store operation.
  46      */
  47     private long maxStackOffset = 0;
  48 
  49     public long upperBoundStackSize() {
  50         return maxStackOffset + maxDataTypeSize;
  51     }
  52 
  53     public HSAILAssembler(TargetDescription target) {
  54         super(target);
  55     }
  56 
  57     @Override
  58     public HSAILAddress makeAddress(Register base, int displacement) {
  59         return new HSAILAddress(base, displacement);
  60     }
  61 
  62     @Override
  63     public HSAILAddress getPlaceholder() {
  64         return null;
  65     }
  66 
  67     public final void undefined(String str) {
  68         emitString("undefined operation " + str);
  69     }
  70 
  71     public final void exit() {
  72         emitString("ret;" + "");
  73     }
  74 
  75     /**
  76      * An Object is only moved into a register when it is a class constant (which is not really a
  77      * constant because it can be moved by GC). Because we can't patch the HSAIL once it is
  78      * finalized, we handle changes due to GC movement by dereferencing a global reference that is
  79      * created by JNI since these JNI global references do not move.
  80      */
  81     public final void mov(Register a, Object obj) {
  82         String regName = "$d" + a.encoding();
  83         if (obj instanceof Class) {
  84             Class<?> clazz = (Class<?>) obj;
  85             long refHandle = OkraUtil.getRefHandle(clazz);
  86             String className = clazz.getName();
  87             emitString("mov_b64 " + regName + ", 0x" + Long.toHexString(refHandle) + ";  // handle for " + className);
  88             emitString("ld_global_u64 " + regName + ", [" + regName + "];");
  89         } else if (obj == null) {
  90             emitString("mov_b64 " + regName + ", 0x0;  // null object");
  91         } else {
  92             throw GraalInternalError.shouldNotReachHere("mov from object not a class");
  93         }
  94 
  95     }
  96 
  97     public final void emitMov(Value dst, Value src) {
  98         if (isRegister(dst) && isConstant(src) && src.getKind().getStackKind() == Kind.Object) {
  99             mov(asRegister(dst), (asConstant(src)).asObject());
 100         } else {
 101             String argtype = getArgType(dst).substring(1);
 102             emitString("mov_b" + argtype + " " + mapRegOrConstToString(dst) + ", " + mapRegOrConstToString(src) + ";");
 103         }
 104     }
 105 
 106     private void emitAddrOp(String instr, Value reg, HSAILAddress addr) {
 107         emitString(instr + " " + HSAIL.mapRegister(reg) + ", " + mapAddress(addr) + ";");
 108     }
 109 
 110     public final void emitLoad(Value dest, HSAILAddress addr) {
 111         emitLoad(dest, addr, getArgType(dest));
 112     }
 113 
 114     public final void emitLoad(Value dest, HSAILAddress addr, String argTypeStr) {
 115         emitAddrOp("ld_global_" + argTypeStr, dest, addr);
 116     }
 117 
 118     public final void emitLda(Value dest, HSAILAddress addr) {
 119         emitAddrOp("lda_global_u64", dest, addr);
 120     }
 121 
 122     public final void emitStore(Value src, HSAILAddress addr) {
 123         emitStore(src, addr, getArgType(src));
 124     }
 125 
 126     public final void emitStore(Value dest, HSAILAddress addr, String argTypeStr) {
 127         emitAddrOp("st_global_" + argTypeStr, dest, addr);
 128     }
 129 
 130     public final void emitSpillLoad(Value dest, Value src) {
 131         emitString("ld_spill_" + getArgType(dest) + " " + HSAIL.mapRegister(dest) + ", " + mapStackSlot(src, getArgSize(dest)) + ";");
 132     }
 133 
 134     public final void emitSpillStore(Value src, Value dest) {
 135         int sizestored = getArgSize(src);
 136         if (maxDataTypeSize < sizestored) {
 137             maxDataTypeSize = sizestored;
 138         }
 139         int stackoffset = HSAIL.getStackOffset(dest);
 140         if (maxStackOffset < stackoffset) {
 141             maxStackOffset = stackoffset;
 142         }
 143         emitString("st_spill_" + getArgType(src) + " " + HSAIL.mapRegister(src) + ", " + mapStackSlot(dest, getArgSize(src)) + ";");
 144     }
 145 
 146     /**
 147      * The mapping to stack slots is always relative to the beginning
 148      * of the spillseg.  HSAIL.getStackOffset returns the positive
 149      * version of the originally negative offset.  Then we back up
 150      * from that by the argSize in bytes.  This ensures that slots of
 151      * different size do not overlap, even though we have converted
 152      * from negative to positive offsets.
 153      */
 154     public static String mapStackSlot(Value reg, int argSize) {
 155         long offset = HSAIL.getStackOffset(reg);
 156         int argSizeBytes = argSize / 8;
 157         return "[%spillseg]" + "[" + (offset - argSizeBytes) + "]";
 158     }
 159 
 160     public void cbr(String target1) {
 161         emitString("cbr " + "$c0" + ", " + target1 + ";");
 162     }
 163 
 164     public int getArgSize(Value src) {
 165         switch (src.getKind()) {
 166             case Int:
 167             case Float:
 168                 return 32;
 169             case Double:
 170             case Long:
 171             case Object:
 172                 return 64;
 173             default:
 174                 throw GraalInternalError.shouldNotReachHere();
 175         }
 176     }
 177 
 178     public static final String getArgType(Value src) {
 179         String prefix = "";
 180         switch (src.getKind()) {
 181             case Float:
 182                 prefix = "f32";
 183                 break;
 184             case Double:
 185                 prefix = "f64";
 186                 break;
 187             case Int:
 188                 prefix = "s32";
 189                 break;
 190             case Long:
 191                 prefix = "s64";
 192                 break;
 193             case Object:
 194                 prefix = "u64";
 195                 break;
 196             default:
 197                 throw GraalInternalError.shouldNotReachHere();
 198         }
 199         return prefix;
 200     }
 201 
 202     public static final String getArgTypeForceUnsigned(Value src) {
 203         switch (src.getKind()) {
 204             case Int:
 205                 return "u32";
 206             case Long:
 207             case Object:
 208                 return "u64";
 209             default:
 210                 throw GraalInternalError.shouldNotReachHere();
 211         }
 212     }
 213 
 214     public void emitCompare(Value src0, Value src1, String condition, boolean unordered, boolean isUnsignedCompare) {
 215         String prefix = "cmp_" + condition + (unordered ? "u" : "") + "_b1_" + (isUnsignedCompare ? getArgTypeForceUnsigned(src1) : getArgType(src1));
 216         String comment = (isConstant(src1) && (src1.getKind() == Kind.Object)
 217                           && (asConstant(src1).asObject() == null) ? " // null test " : "");
 218         emitString(prefix + " $c0, " + mapRegOrConstToString(src0) + ", " + mapRegOrConstToString(src1) + ";" + comment);
 219     }
 220 
 221     public void emitConvert(Value dest, Value src) {
 222         String prefix = (getArgType(dest).equals("f32") && getArgType(src).equals("f64")) ? "cvt_near_" : "cvt_";
 223         emitString(prefix + getArgType(dest) + "_" + getArgType(src) + " " + HSAIL.mapRegister(dest) + ", " + HSAIL.mapRegister(src) + ";");
 224     }
 225 
 226     public void emitArg1(String mnemonic, Value dest, Value src) {
 227         emitString(mnemonic + "_" + getArgType(src) + " " + HSAIL.mapRegister(dest) + ", " + mapRegOrConstToString(src) + ";" + "");
 228     }
 229 
 230     public static String mapAddress(HSAILAddress addr) {
 231         return "[$d" + addr.getBase().encoding() + " + " + addr.getDisplacement() + "]";
 232     }
 233 
 234     private static String mapRegOrConstToString(Value src) {
 235         if (!isConstant(src)) {
 236             return HSAIL.mapRegister(src);
 237         } else {
 238             Constant consrc = asConstant(src);
 239             switch (src.getKind()) {
 240                 case Int:
 241                     return Integer.toString(consrc.asInt());
 242                 case Float:
 243                     return Float.toString(consrc.asFloat()) + "f";
 244                 case Double:
 245                     return Double.toString(consrc.asDouble());
 246                 case Long:
 247                     return "0x" + Long.toHexString(consrc.asLong());
 248                 case Object:
 249                     Object obj = consrc.asObject();
 250                     if (obj == null) {
 251                         return "0";
 252                     } else {
 253                         throw GraalInternalError.shouldNotReachHere("unknown type: " + src);
 254                     }
 255                 default:
 256                     throw GraalInternalError.shouldNotReachHere("unknown type: " + src);
 257             }
 258         }
 259 
 260     }
 261 
 262     public final void emit(String mnemonic, Value dest, Value src0, Value src1) {
 263         String prefix = getArgType(dest);
 264         emit(mnemonic + "_" + prefix, dest, "", src0, src1);
 265     }
 266 
 267     public final void emitForceUnsigned(String mnemonic, Value dest, Value src0, Value src1) {
 268         String prefix = getArgTypeForceUnsigned(dest);
 269         emit(mnemonic + "_" + prefix, dest, "", src0, src1);
 270     }
 271 
 272 
 273     private void emit(String instr, Value dest, String controlRegString, Value src0, Value src1) {
 274         assert (!isConstant(dest));
 275         emitString(String.format("%s %s, %s%s, %s;", instr, HSAIL.mapRegister(dest), controlRegString, mapRegOrConstToString(src0), mapRegOrConstToString(src1)));
 276     }
 277 
 278     private void emit(String instr, Value dest, Value src0, Value src1, Value src2) {
 279         assert (!isConstant(dest));
 280         emitString(String.format("%s %s, %s, %s, %s;", instr, HSAIL.mapRegister(dest), mapRegOrConstToString(src0),
 281                                  mapRegOrConstToString(src1), mapRegOrConstToString(src2)));
 282     }
 283 
 284 
 285     public final void cmovCommon(Value dest, Value trueReg, Value falseReg, int width) {
 286         String instr = (width == 32 ? "cmov_b32" : "cmov_b64");
 287         emit(instr, dest, "$c0, ", trueReg, falseReg);
 288     }
 289 
 290 
 291     /**
 292      * Emit code to build a 64-bit pointer from a compressed-oop and the associated base and shift.
 293      * We only emit this if base and shift are not both zero.
 294      */
 295     public void emitCompressedOopDecode(Value result, long narrowOopBase, int narrowOopShift) {
 296         if (narrowOopBase == 0) {
 297             emit("shl", result, result, Constant.forInt(narrowOopShift));
 298         } else if (narrowOopShift == 0) {
 299             // only use add if result is not starting as null (unsigned compare)
 300             emitCompare(result, Constant.forLong(0), "eq", false, true);
 301             emit("add", result, result, Constant.forLong(narrowOopBase));
 302             cmovCommon(result, Constant.forLong(0), result, 64);
 303         } else {
 304             // only use mad if result is not starting as null (unsigned compare)
 305             emitCompare(result, Constant.forLong(0), "eq", false, true);
 306             emit("mad_u64 ", result, result, Constant.forInt(1 << narrowOopShift), Constant.forLong(narrowOopBase));
 307             cmovCommon(result, Constant.forLong(0), result, 64);
 308         }
 309     }
 310 
 311     /**
 312      * Emit code to build a 32-bit compressed pointer from a full
 313      * 64-bit pointer using the associated base and shift.  We only
 314      * emit this if base and shift are not both zero.
 315      */
 316     public void emitCompressedOopEncode(Value result, long narrowOopBase, int narrowOopShift) {
 317         if (narrowOopBase != 0) {
 318             // only use sub if result is not starting as null (unsigned compare)
 319             emitCompare(result, Constant.forLong(0), "eq", false, true);
 320             emit("sub", result, result, Constant.forLong(narrowOopBase));
 321             cmovCommon(result, Constant.forLong(0), result, 64);
 322         }
 323         if (narrowOopShift != 0) {
 324             emit("shr", result, result, Constant.forInt(narrowOopShift));
 325         }
 326     }
 327 
 328     public void emitComment(String comment) {
 329         emitString(comment);
 330     }
 331 }