1 /*
   2  * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2018, Red Hat Inc. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 
  26 
  27 package jdk.tools.jaotc.aarch64;
  28 
  29 import static jdk.vm.ci.aarch64.AArch64.r12;
  30 import static jdk.vm.ci.aarch64.AArch64.r16;
  31 import static jdk.vm.ci.aarch64.AArch64.r17;
  32 import static jdk.vm.ci.aarch64.AArch64.r9;
  33 
  34 import org.graalvm.compiler.asm.aarch64.AArch64Address;
  35 import org.graalvm.compiler.asm.aarch64.AArch64MacroAssembler;
  36 
  37 import jdk.tools.jaotc.ELFMacroAssembler;
  38 import jdk.tools.jaotc.StubInformation;
  39 import jdk.vm.ci.code.TargetDescription;
  40 
  41 public final class AArch64ELFMacroAssembler extends AArch64MacroAssembler implements ELFMacroAssembler {
  42 
  43     private int currentEndOfInstruction;
  44 
  45     public AArch64ELFMacroAssembler(TargetDescription target) {
  46         super(target);
  47     }
  48 
  49     @Override
  50     public int currentEndOfInstruction() {
  51         return currentEndOfInstruction;
  52     }
  53 
  54     @Override
  55     public byte[] getPLTJumpCode() {
  56         // The main dispatch instruction
  57         addressOf(r16);
  58         ldr(64, r16, AArch64Address.createBaseRegisterOnlyAddress(r16));
  59         jmp(r16);
  60 
  61         currentEndOfInstruction = position();
  62 
  63         align(8);
  64 
  65         return close(true);
  66     }
  67 
  68     @Override
  69     public byte[] getPLTStaticEntryCode(StubInformation stub) {
  70         // The main dispatch instruction
  71         addressOf(r16);
  72         ldr(64, r16, AArch64Address.createBaseRegisterOnlyAddress(r16));
  73         jmp(r16);
  74         stub.setDispatchJumpOffset(position());
  75 
  76         // C2I stub used to call interpreter. First load r12
  77         // (i.e. rmethod) with a pointer to the Method structure ...
  78         addressOf(r12);
  79         ldr(64, r12, AArch64Address.createBaseRegisterOnlyAddress(r12));
  80         nop();
  81         stub.setMovOffset(position());
  82 
  83         // ... then jump to the interpreter.
  84         addressOf(r16);
  85         ldr(64, r16, AArch64Address.createBaseRegisterOnlyAddress(r16));
  86         jmp(r16);
  87         stub.setC2IJumpOffset(position());
  88 
  89         // Call to VM runtime to resolve the call.
  90         stub.setResolveJumpStart(position());
  91         addressOf(r16);
  92         ldr(64, r16, AArch64Address.createBaseRegisterOnlyAddress(r16));
  93         jmp(r16);
  94         stub.setResolveJumpOffset(position());
  95         currentEndOfInstruction = position();
  96 
  97         align(8);
  98         stub.setSize(position());
  99 
 100         return close(true);
 101     }
 102 
 103     @Override
 104     public byte[] getPLTVirtualEntryCode(StubInformation stub) {
 105         // Fixup an inline cache.
 106         // Load r9 with a pointer to the Klass.
 107         addressOf(r17);
 108         ldr(64, r9, AArch64Address.createBaseRegisterOnlyAddress(r17));
 109         nop();
 110         stub.setMovOffset(position());
 111 
 112         // Jump to the method.
 113         addressOf(r16);
 114         ldr(64, r16, AArch64Address.createBaseRegisterOnlyAddress(r16));
 115         jmp(r16);
 116         stub.setDispatchJumpOffset(position());
 117 
 118         // Call to VM runtime to resolve the call.
 119         stub.setResolveJumpStart(position());
 120         addressOf(r16);
 121         ldr(64, r16, AArch64Address.createBaseRegisterOnlyAddress(r16));
 122         jmp(r16);
 123         stub.setResolveJumpOffset(position());
 124         currentEndOfInstruction = position();
 125 
 126         align(8);
 127         stub.setSize(position());
 128 
 129         return close(true);
 130     }
 131 }