2017-06-28 07:58:19 +08:00
|
|
|
//===-- AArch64WinCOFFStreamer.cpp - ARM Target WinCOFF Streamer ----*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-06-28 07:58:19 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "AArch64WinCOFFStreamer.h"
|
2017-10-12 07:34:47 +08:00
|
|
|
#include "llvm/MC/MCAsmBackend.h"
|
|
|
|
#include "llvm/MC/MCCodeEmitter.h"
|
2018-05-19 02:26:45 +08:00
|
|
|
#include "llvm/MC/MCObjectWriter.h"
|
2018-10-27 14:13:06 +08:00
|
|
|
#include "llvm/MC/MCWin64EH.h"
|
|
|
|
#include "llvm/MC/MCWinCOFFStreamer.h"
|
2017-06-28 07:58:19 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class AArch64WinCOFFStreamer : public MCWinCOFFStreamer {
|
2018-10-27 14:13:06 +08:00
|
|
|
Win64EH::ARM64UnwindEmitter EHStreamer;
|
|
|
|
|
2017-06-28 07:58:19 +08:00
|
|
|
public:
|
2017-10-11 09:57:21 +08:00
|
|
|
AArch64WinCOFFStreamer(MCContext &C, std::unique_ptr<MCAsmBackend> AB,
|
2017-10-12 07:34:47 +08:00
|
|
|
std::unique_ptr<MCCodeEmitter> CE,
|
2018-05-19 02:26:45 +08:00
|
|
|
std::unique_ptr<MCObjectWriter> OW)
|
|
|
|
: MCWinCOFFStreamer(C, std::move(AB), std::move(CE), std::move(OW)) {}
|
2017-11-03 15:33:20 +08:00
|
|
|
|
2018-10-27 14:13:06 +08:00
|
|
|
void EmitWinEHHandlerData(SMLoc Loc) override;
|
|
|
|
void EmitWindowsUnwindTables() override;
|
2017-11-03 15:33:20 +08:00
|
|
|
void FinishImpl() override;
|
2017-06-28 07:58:19 +08:00
|
|
|
};
|
2017-11-03 15:33:20 +08:00
|
|
|
|
2018-10-27 14:13:06 +08:00
|
|
|
void AArch64WinCOFFStreamer::EmitWinEHHandlerData(SMLoc Loc) {
|
|
|
|
MCStreamer::EmitWinEHHandlerData(Loc);
|
|
|
|
|
|
|
|
// We have to emit the unwind info now, because this directive
|
|
|
|
// actually switches to the .xdata section!
|
|
|
|
EHStreamer.EmitUnwindInfo(*this, getCurrentWinFrameInfo());
|
|
|
|
}
|
|
|
|
|
|
|
|
void AArch64WinCOFFStreamer::EmitWindowsUnwindTables() {
|
|
|
|
if (!getNumWinFrameInfos())
|
|
|
|
return;
|
|
|
|
EHStreamer.Emit(*this);
|
|
|
|
}
|
|
|
|
|
2017-11-03 15:33:20 +08:00
|
|
|
void AArch64WinCOFFStreamer::FinishImpl() {
|
|
|
|
EmitFrames(nullptr);
|
2018-10-27 14:13:06 +08:00
|
|
|
EmitWindowsUnwindTables();
|
2017-11-03 15:33:20 +08:00
|
|
|
|
|
|
|
MCWinCOFFStreamer::FinishImpl();
|
|
|
|
}
|
2017-06-28 07:58:19 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
namespace llvm {
|
2018-10-27 14:13:06 +08:00
|
|
|
|
|
|
|
// Helper function to common out unwind code setup for those codes that can
|
|
|
|
// belong to both prolog and epilog.
|
|
|
|
// There are three types of Windows ARM64 SEH codes. They can
|
|
|
|
// 1) take no operands: SEH_Nop, SEH_PrologEnd, SEH_EpilogStart, SEH_EpilogEnd
|
|
|
|
// 2) take an offset: SEH_StackAlloc, SEH_SaveFPLR, SEH_SaveFPLR_X
|
|
|
|
// 3) take a register and an offset/size: all others
|
|
|
|
void AArch64TargetWinCOFFStreamer::EmitARM64WinUnwindCode(unsigned UnwindCode,
|
|
|
|
int Reg,
|
|
|
|
int Offset) {
|
|
|
|
auto &S = getStreamer();
|
|
|
|
WinEH::FrameInfo *CurFrame = S.EnsureValidWinFrameInfo(SMLoc());
|
|
|
|
if (!CurFrame)
|
|
|
|
return;
|
|
|
|
MCSymbol *Label = S.EmitCFILabel();
|
|
|
|
auto Inst = WinEH::Instruction(UnwindCode, Label, Reg, Offset);
|
|
|
|
if (InEpilogCFI)
|
|
|
|
CurFrame->EpilogMap[CurrentEpilog].push_back(Inst);
|
|
|
|
else
|
|
|
|
CurFrame->Instructions.push_back(Inst);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AArch64TargetWinCOFFStreamer::EmitARM64WinCFIAllocStack(unsigned Size) {
|
|
|
|
unsigned Op = Win64EH::UOP_AllocSmall;
|
|
|
|
if (Size >= 16384)
|
|
|
|
Op = Win64EH::UOP_AllocLarge;
|
|
|
|
else if (Size >= 512)
|
|
|
|
Op = Win64EH::UOP_AllocMedium;
|
|
|
|
EmitARM64WinUnwindCode(Op, -1, Size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AArch64TargetWinCOFFStreamer::EmitARM64WinCFISaveFPLR(int Offset) {
|
|
|
|
EmitARM64WinUnwindCode(Win64EH::UOP_SaveFPLR, -1, Offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AArch64TargetWinCOFFStreamer::EmitARM64WinCFISaveFPLRX(int Offset) {
|
|
|
|
EmitARM64WinUnwindCode(Win64EH::UOP_SaveFPLRX, -1, Offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AArch64TargetWinCOFFStreamer::EmitARM64WinCFISaveReg(unsigned Reg,
|
|
|
|
int Offset) {
|
|
|
|
assert(Offset >= 0 && Offset <= 504 &&
|
|
|
|
"Offset for save reg should be >= 0 && <= 504");
|
|
|
|
EmitARM64WinUnwindCode(Win64EH::UOP_SaveReg, Reg, Offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AArch64TargetWinCOFFStreamer::EmitARM64WinCFISaveRegX(unsigned Reg,
|
|
|
|
int Offset) {
|
|
|
|
EmitARM64WinUnwindCode(Win64EH::UOP_SaveRegX, Reg, Offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AArch64TargetWinCOFFStreamer::EmitARM64WinCFISaveRegP(unsigned Reg,
|
|
|
|
int Offset) {
|
|
|
|
EmitARM64WinUnwindCode(Win64EH::UOP_SaveRegP, Reg, Offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AArch64TargetWinCOFFStreamer::EmitARM64WinCFISaveRegPX(unsigned Reg,
|
|
|
|
int Offset) {
|
|
|
|
EmitARM64WinUnwindCode(Win64EH::UOP_SaveRegPX, Reg, Offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AArch64TargetWinCOFFStreamer::EmitARM64WinCFISaveFReg(unsigned Reg,
|
|
|
|
int Offset) {
|
|
|
|
assert(Offset >= 0 && Offset <= 504 &&
|
|
|
|
"Offset for save reg should be >= 0 && <= 504");
|
|
|
|
EmitARM64WinUnwindCode(Win64EH::UOP_SaveFReg, Reg, Offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AArch64TargetWinCOFFStreamer::EmitARM64WinCFISaveFRegX(unsigned Reg,
|
|
|
|
int Offset) {
|
|
|
|
EmitARM64WinUnwindCode(Win64EH::UOP_SaveFRegX, Reg, Offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AArch64TargetWinCOFFStreamer::EmitARM64WinCFISaveFRegP(unsigned Reg,
|
|
|
|
int Offset) {
|
|
|
|
EmitARM64WinUnwindCode(Win64EH::UOP_SaveFRegP, Reg, Offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AArch64TargetWinCOFFStreamer::EmitARM64WinCFISaveFRegPX(unsigned Reg,
|
|
|
|
int Offset) {
|
|
|
|
EmitARM64WinUnwindCode(Win64EH::UOP_SaveFRegPX, Reg, Offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AArch64TargetWinCOFFStreamer::EmitARM64WinCFISetFP() {
|
|
|
|
EmitARM64WinUnwindCode(Win64EH::UOP_SetFP, -1, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AArch64TargetWinCOFFStreamer::EmitARM64WinCFIAddFP(unsigned Offset) {
|
|
|
|
assert(Offset <= 2040 && "UOP_AddFP must have offset <= 2040");
|
|
|
|
EmitARM64WinUnwindCode(Win64EH::UOP_AddFP, -1, Offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AArch64TargetWinCOFFStreamer::EmitARM64WinCFINop() {
|
|
|
|
EmitARM64WinUnwindCode(Win64EH::UOP_Nop, -1, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The functions below handle opcodes that can end up in either a prolog or
|
|
|
|
// an epilog, but not both.
|
|
|
|
void AArch64TargetWinCOFFStreamer::EmitARM64WinCFIPrologEnd() {
|
|
|
|
auto &S = getStreamer();
|
|
|
|
WinEH::FrameInfo *CurFrame = S.EnsureValidWinFrameInfo(SMLoc());
|
|
|
|
if (!CurFrame)
|
|
|
|
return;
|
|
|
|
|
|
|
|
MCSymbol *Label = S.EmitCFILabel();
|
|
|
|
CurFrame->PrologEnd = Label;
|
|
|
|
WinEH::Instruction Inst = WinEH::Instruction(Win64EH::UOP_End, Label, -1, 0);
|
|
|
|
auto it = CurFrame->Instructions.begin();
|
|
|
|
CurFrame->Instructions.insert(it, Inst);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AArch64TargetWinCOFFStreamer::EmitARM64WinCFIEpilogStart() {
|
|
|
|
auto &S = getStreamer();
|
|
|
|
WinEH::FrameInfo *CurFrame = S.EnsureValidWinFrameInfo(SMLoc());
|
|
|
|
if (!CurFrame)
|
|
|
|
return;
|
|
|
|
|
|
|
|
InEpilogCFI = true;
|
|
|
|
CurrentEpilog = S.EmitCFILabel();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AArch64TargetWinCOFFStreamer::EmitARM64WinCFIEpilogEnd() {
|
|
|
|
auto &S = getStreamer();
|
|
|
|
WinEH::FrameInfo *CurFrame = S.EnsureValidWinFrameInfo(SMLoc());
|
|
|
|
if (!CurFrame)
|
|
|
|
return;
|
|
|
|
|
|
|
|
InEpilogCFI = false;
|
|
|
|
MCSymbol *Label = S.EmitCFILabel();
|
|
|
|
WinEH::Instruction Inst = WinEH::Instruction(Win64EH::UOP_End, Label, -1, 0);
|
|
|
|
CurFrame->EpilogMap[CurrentEpilog].push_back(Inst);
|
|
|
|
CurrentEpilog = nullptr;
|
|
|
|
}
|
|
|
|
|
2017-10-12 07:34:47 +08:00
|
|
|
MCWinCOFFStreamer *createAArch64WinCOFFStreamer(
|
|
|
|
MCContext &Context, std::unique_ptr<MCAsmBackend> MAB,
|
2018-05-19 02:26:45 +08:00
|
|
|
std::unique_ptr<MCObjectWriter> OW, std::unique_ptr<MCCodeEmitter> Emitter,
|
2017-10-12 07:34:47 +08:00
|
|
|
bool RelaxAll, bool IncrementalLinkerCompatible) {
|
|
|
|
auto *S = new AArch64WinCOFFStreamer(Context, std::move(MAB),
|
2018-05-19 02:26:45 +08:00
|
|
|
std::move(Emitter), std::move(OW));
|
2017-06-28 07:58:19 +08:00
|
|
|
S->getAssembler().setIncrementalLinkerCompatible(IncrementalLinkerCompatible);
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end llvm namespace
|