1 /*
   2  * Copyright (c) 2016, 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 jdk.tools.jaotc.aarch64;
  25 
  26 import jdk.tools.jaotc.StubInformation;
  27 import jdk.tools.jaotc.ELFMacroAssembler;
  28 
  29 import org.graalvm.compiler.asm.aarch64.AArch64Address;
  30 import org.graalvm.compiler.asm.aarch64.AArch64MacroAssembler;
  31 
  32 
  33 import jdk.vm.ci.code.TargetDescription;
  34 import jdk.vm.ci.code.Register;
  35 
  36 import static jdk.vm.ci.aarch64.AArch64.*;
  37 
  38 public final class AArch64ELFMacroAssembler extends AArch64MacroAssembler implements ELFMacroAssembler {
  39 
  40     private int currentEndOfInstruction;
  41 
  42     public AArch64ELFMacroAssembler(TargetDescription target) {
  43         super(target);
  44     }
  45 
  46     @Override
  47     public int currentEndOfInstruction() {
  48         return currentEndOfInstruction;
  49     }
  50 
  51     @Override
  52     public byte[] getPLTJumpCode() {
  53         // The main dispatch instruction
  54         addressOf(r16);
  55         ldr(64, r16, AArch64Address.createBaseRegisterOnlyAddress(r16));
  56         jmp(r16);
  57 
  58         currentEndOfInstruction = position();
  59 
  60         align(8);
  61 
  62         return close(true);
  63     }
  64 
  65     @Override
  66     public byte[] getPLTStaticEntryCode(StubInformation stub) {
  67         // The main dispatch instruction
  68         addressOf(r16);
  69         ldr(64, r16, AArch64Address.createBaseRegisterOnlyAddress(r16));
  70         jmp(r16);
  71         stub.setDispatchJumpOffset(position());
  72 
  73         // C2I stub used to call interpreter.  First load r12
  74         // (i.e. rmethod) with a pointer to the Method structure ...
  75         addressOf(r12);
  76         ldr(64, r12, AArch64Address.createBaseRegisterOnlyAddress(r12));
  77         nop();
  78         stub.setMovOffset(position());
  79 
  80         // ... then jump to the interpreter.
  81         addressOf(r16);
  82         ldr(64, r16, AArch64Address.createBaseRegisterOnlyAddress(r16));
  83         jmp(r16);
  84         stub.setC2IJumpOffset(position());
  85 
  86         // Call to VM runtime to resolve the call.
  87         stub.setResolveJumpStart(position());
  88         addressOf(r16);
  89         ldr(64, r16, AArch64Address.createBaseRegisterOnlyAddress(r16));
  90         jmp(r16);
  91         stub.setResolveJumpOffset(position());
  92         currentEndOfInstruction = position();
  93 
  94         align(8);
  95         stub.setSize(position());
  96 
  97         return close(true);
  98     }
  99 
 100     @Override
 101     public byte[] getPLTVirtualEntryCode(StubInformation stub) {
 102         // Fixup an inline cache.
 103         // Load r9 with a pointer to the Klass.
 104         addressOf(r17);
 105         ldr(64, r9, AArch64Address.createBaseRegisterOnlyAddress(r17));
 106         nop();
 107         stub.setMovOffset(position());
 108 
 109         // Jump to the method.
 110         addressOf(r16);
 111         ldr(64, r16, AArch64Address.createBaseRegisterOnlyAddress(r16));
 112         jmp(r16);
 113         stub.setDispatchJumpOffset(position());
 114 
 115         // Call to VM runtime to resolve the call.
 116         stub.setResolveJumpStart(position());
 117         addressOf(r16);
 118         ldr(64, r16, AArch64Address.createBaseRegisterOnlyAddress(r16));
 119         jmp(r16);
 120         stub.setResolveJumpOffset(position());
 121         currentEndOfInstruction = position();
 122 
 123         align(8);
 124         stub.setSize(position());
 125 
 126         return close(true);
 127     }
 128 }