26 formatted_raw_ostream &OS;
27 MCInstPrinter &InstPrinter;
30 X86WinCOFFAsmTargetStreamer(MCStreamer &S, formatted_raw_ostream &OS,
31 MCInstPrinter &InstPrinter)
32 : X86TargetStreamer(S), OS(OS), InstPrinter(InstPrinter) {}
34 void emitCode16()
override;
35 void emitCode32()
override;
36 void emitCode64()
override;
38 bool emitFPOProc(
const MCSymbol *ProcSym,
unsigned ParamsSize,
40 bool emitFPOEndPrologue(SMLoc L)
override;
41 bool emitFPOEndProc(SMLoc L)
override;
42 bool emitFPOData(
const MCSymbol *ProcSym, SMLoc L)
override;
43 bool emitFPOPushReg(MCRegister
Reg, SMLoc L)
override;
44 bool emitFPOStackAlloc(
unsigned StackAlloc, SMLoc L)
override;
45 bool emitFPOStackAlign(
unsigned Align, SMLoc L)
override;
46 bool emitFPOSetFrame(MCRegister
Reg, SMLoc L)
override;
50struct FPOInstruction {
66 unsigned ParamsSize = 0;
74 DenseMap<const MCSymbol *, std::unique_ptr<FPOData>> AllFPOData;
77 std::unique_ptr<FPOData> CurFPOData;
79 bool haveOpenFPOData() {
return !!CurFPOData; }
83 bool checkInFPOPrologue(SMLoc L);
88 X86WinCOFFTargetStreamer(MCStreamer &S) : X86TargetStreamer(S) {}
90 bool emitFPOProc(
const MCSymbol *ProcSym,
unsigned ParamsSize,
92 bool emitFPOEndPrologue(SMLoc L)
override;
93 bool emitFPOEndProc(SMLoc L)
override;
94 bool emitFPOData(
const MCSymbol *ProcSym, SMLoc L)
override;
95 bool emitFPOPushReg(MCRegister
Reg, SMLoc L)
override;
96 bool emitFPOStackAlloc(
unsigned StackAlloc, SMLoc L)
override;
97 bool emitFPOStackAlign(
unsigned Align, SMLoc L)
override;
98 bool emitFPOSetFrame(MCRegister
Reg, SMLoc L)
override;
102void X86WinCOFFAsmTargetStreamer::emitCode16() { OS <<
"\t.code16\n"; }
104void X86WinCOFFAsmTargetStreamer::emitCode32() { OS <<
"\t.code32\n"; }
106void X86WinCOFFAsmTargetStreamer::emitCode64() { OS <<
"\t.code64\n"; }
108bool X86WinCOFFAsmTargetStreamer::emitFPOProc(
const MCSymbol *ProcSym,
109 unsigned ParamsSize, SMLoc L) {
110 OS <<
"\t.cv_fpo_proc\t";
112 OS <<
' ' << ParamsSize <<
'\n';
116bool X86WinCOFFAsmTargetStreamer::emitFPOEndPrologue(SMLoc L) {
117 OS <<
"\t.cv_fpo_endprologue\n";
121bool X86WinCOFFAsmTargetStreamer::emitFPOEndProc(SMLoc L) {
122 OS <<
"\t.cv_fpo_endproc\n";
126bool X86WinCOFFAsmTargetStreamer::emitFPOData(
const MCSymbol *ProcSym,
128 OS <<
"\t.cv_fpo_data\t";
134bool X86WinCOFFAsmTargetStreamer::emitFPOPushReg(MCRegister
Reg, SMLoc L) {
135 OS <<
"\t.cv_fpo_pushreg\t";
141bool X86WinCOFFAsmTargetStreamer::emitFPOStackAlloc(
unsigned StackAlloc,
143 OS <<
"\t.cv_fpo_stackalloc\t" << StackAlloc <<
'\n';
147bool X86WinCOFFAsmTargetStreamer::emitFPOStackAlign(
unsigned Align, SMLoc L) {
148 OS <<
"\t.cv_fpo_stackalign\t" <<
Align <<
'\n';
152bool X86WinCOFFAsmTargetStreamer::emitFPOSetFrame(MCRegister
Reg, SMLoc L) {
153 OS <<
"\t.cv_fpo_setframe\t";
159bool X86WinCOFFTargetStreamer::checkInFPOPrologue(SMLoc L) {
160 if (!haveOpenFPOData() || CurFPOData->PrologueEnd) {
163 "directive must appear between .cv_fpo_proc and .cv_fpo_endprologue");
169MCSymbol *X86WinCOFFTargetStreamer::emitFPOLabel() {
171 getStreamer().emitLabel(Label);
175bool X86WinCOFFTargetStreamer::emitFPOProc(
const MCSymbol *ProcSym,
176 unsigned ParamsSize, SMLoc L) {
177 if (haveOpenFPOData()) {
179 L,
"opening new .cv_fpo_proc before closing previous frame");
182 CurFPOData = std::make_unique<FPOData>();
183 CurFPOData->Function = ProcSym;
184 CurFPOData->Begin = emitFPOLabel();
185 CurFPOData->ParamsSize = ParamsSize;
189bool X86WinCOFFTargetStreamer::emitFPOEndProc(SMLoc L) {
190 if (!haveOpenFPOData()) {
191 getContext().reportError(L,
".cv_fpo_endproc must appear after .cv_proc");
194 if (!CurFPOData->PrologueEnd) {
196 if (!CurFPOData->Instructions.empty()) {
197 getContext().reportError(L,
"missing .cv_fpo_endprologue");
198 CurFPOData->Instructions.clear();
203 CurFPOData->PrologueEnd = CurFPOData->Begin;
206 CurFPOData->End = emitFPOLabel();
207 const MCSymbol *Fn = CurFPOData->Function;
208 AllFPOData.insert({Fn, std::move(CurFPOData)});
212bool X86WinCOFFTargetStreamer::emitFPOSetFrame(MCRegister
Reg, SMLoc L) {
213 if (checkInFPOPrologue(L))
216 Inst.Label = emitFPOLabel();
217 Inst.Op = FPOInstruction::SetFrame;
218 Inst.RegOrOffset =
Reg;
219 CurFPOData->Instructions.push_back(Inst);
223bool X86WinCOFFTargetStreamer::emitFPOPushReg(MCRegister
Reg, SMLoc L) {
224 if (checkInFPOPrologue(L))
227 Inst.Label = emitFPOLabel();
228 Inst.Op = FPOInstruction::PushReg;
229 Inst.RegOrOffset =
Reg;
230 CurFPOData->Instructions.push_back(Inst);
234bool X86WinCOFFTargetStreamer::emitFPOStackAlloc(
unsigned StackAlloc, SMLoc L) {
235 if (checkInFPOPrologue(L))
238 Inst.Label = emitFPOLabel();
239 Inst.Op = FPOInstruction::StackAlloc;
240 Inst.RegOrOffset = StackAlloc;
241 CurFPOData->Instructions.push_back(Inst);
245bool X86WinCOFFTargetStreamer::emitFPOStackAlign(
unsigned Align, SMLoc L) {
246 if (checkInFPOPrologue(L))
248 if (
llvm::none_of(CurFPOData->Instructions, [](
const FPOInstruction &Inst) {
249 return Inst.Op == FPOInstruction::SetFrame;
252 L,
"a frame register must be established before aligning the stack");
256 Inst.Label = emitFPOLabel();
257 Inst.Op = FPOInstruction::StackAlign;
258 Inst.RegOrOffset =
Align;
259 CurFPOData->Instructions.push_back(Inst);
263bool X86WinCOFFTargetStreamer::emitFPOEndPrologue(SMLoc L) {
264 if (checkInFPOPrologue(L))
266 CurFPOData->PrologueEnd = emitFPOLabel();
271struct RegSaveOffset {
278struct FPOStateMachine {
279 explicit FPOStateMachine(
const FPOData *FPO) :
FPO(
FPO) {}
281 const FPOData *FPO =
nullptr;
282 unsigned FrameReg = 0;
283 unsigned FrameRegOff = 0;
284 unsigned CurOffset = 0;
285 unsigned LocalSize = 0;
286 unsigned SavedRegSize = 0;
287 unsigned StackOffsetBeforeAlign = 0;
288 unsigned StackAlign = 0;
291 SmallString<128> FrameFunc;
295 void emitFrameDataRecord(MCStreamer &OS, MCSymbol *Label);
304 case X86::EAX: OS <<
"$eax";
break;
305 case X86::EBX: OS <<
"$ebx";
break;
306 case X86::ECX: OS <<
"$ecx";
break;
307 case X86::EDX: OS <<
"$edx";
break;
308 case X86::EDI: OS <<
"$edi";
break;
309 case X86::ESI: OS <<
"$esi";
break;
310 case X86::ESP: OS <<
"$esp";
break;
311 case X86::EBP: OS <<
"$ebp";
break;
312 case X86::EIP: OS <<
"$eip";
break;
315 OS <<
'$' <<
MRI->getCodeViewRegNum(LLVMReg);
321void FPOStateMachine::emitFrameDataRecord(MCStreamer &OS, MCSymbol *Label) {
322 unsigned CurFlags = Flags;
323 if (Label == FPO->Begin)
328 raw_svector_ostream FuncOS(FrameFunc);
330 assert((StackAlign == 0 || FrameReg != 0) &&
331 "cannot align stack without frame reg");
332 StringRef CFAVar = StackAlign == 0 ?
"$T0" :
"$T1";
336 FuncOS << CFAVar <<
' ' <<
printFPOReg(
MRI, FrameReg) <<
' ' << FrameRegOff
344 FuncOS <<
"$T0 " << CFAVar <<
' ' << StackOffsetBeforeAlign <<
" - "
345 << StackAlign <<
" @ = ";
352 FuncOS << CFAVar <<
" .raSearch = ";
356 FuncOS <<
"$eip " << CFAVar <<
" ^ = ";
357 FuncOS <<
"$esp " << CFAVar <<
" 4 + = ";
360 for (RegSaveOffset RO : RegSaveOffsets)
361 FuncOS <<
printFPOReg(
MRI, RO.Reg) <<
' ' << CFAVar <<
' ' << RO.Offset
369 unsigned MaxStackSize = 0;
394bool X86WinCOFFTargetStreamer::emitFPOData(
const MCSymbol *ProcSym, SMLoc L) {
395 MCStreamer &OS = getStreamer();
398 auto I = AllFPOData.find(ProcSym);
399 if (
I == AllFPOData.end()) {
400 Ctx.
reportError(L, Twine(
"no FPO data found for symbol ") +
404 const FPOData *
FPO =
I->second.get();
405 assert(
FPO->Begin &&
FPO->End &&
FPO->PrologueEnd &&
"missing FPO label");
410 OS.
emitInt32(
unsigned(DebugSubsectionKind::FrameData));
420 FPOStateMachine FSM(FPO);
422 FSM.emitFrameDataRecord(OS,
FPO->Begin);
423 for (
const FPOInstruction &Inst :
FPO->Instructions) {
425 case FPOInstruction::PushReg:
427 FSM.SavedRegSize += 4;
428 FSM.RegSaveOffsets.push_back({Inst.RegOrOffset, FSM.CurOffset});
430 case FPOInstruction::SetFrame:
431 FSM.FrameReg = Inst.RegOrOffset;
432 FSM.FrameRegOff = FSM.CurOffset;
434 case FPOInstruction::StackAlign:
435 FSM.StackOffsetBeforeAlign = FSM.CurOffset;
436 FSM.StackAlign = Inst.RegOrOffset;
438 case FPOInstruction::StackAlloc:
439 FSM.CurOffset += Inst.RegOrOffset;
440 FSM.LocalSize += Inst.RegOrOffset;
446 FSM.emitFrameDataRecord(OS, Inst.Label);
459 return new X86WinCOFFAsmTargetStreamer(S, OS, *InstPrinter);
468 return new X86WinCOFFTargetStreamer(S);
unsigned const MachineRegisterInfo * MRI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static Printable printFPOReg(const MCRegisterInfo *MRI, unsigned LLVMReg)
std::pair< StringRef, unsigned > addToStringTable(StringRef S)
Add something to the string table.
LLVM_ABI MCSymbol * createTempSymbol()
Create a temporary symbol with a unique name.
LLVM_ABI CodeViewContext & getCVContext()
const MCRegisterInfo * getRegisterInfo() const
LLVM_ABI void reportError(SMLoc L, const Twine &Msg)
This is an instance of a target assembly language printer that converts an MCInst to valid target ass...
virtual void printRegName(raw_ostream &OS, MCRegister Reg)
Print the assembler register name.
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
Streaming machine code generation interface.
MCContext & getContext() const
void emitValue(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc())
virtual void emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo, unsigned Size)
Emit the absolute difference between two symbols.
virtual void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc())
Emit a label for Symbol into the current section.
virtual void emitValueToAlignment(Align Alignment, int64_t Fill=0, uint8_t FillLen=1, unsigned MaxBytesToEmit=0)
Emit some number of copies of Value until the byte alignment ByteAlignment is reached.
void emitInt16(uint64_t Value)
void emitInt32(uint64_t Value)
Generic base class for all target subtargets.
const Triple & getTargetTriple() const
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx, SMLoc Loc=SMLoc())
LLVM_ABI void print(raw_ostream &OS, const MCAsmInfo *MAI) const
print - Print the value to the stream OS.
StringRef getName() const
getName - Get the symbol name.
Target specific streamer interface.
Simple wrapper around std::function<void(raw_ostream&)>.
bool isOSBinFormatCOFF() const
Tests whether the OS uses the COFF binary format.
X86 target streamer implementing x86-only assembly directives.
This class implements an extremely fast bulk output stream that can only output to a stream.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
Context & getContext() const
This is an optimization pass for GlobalISel generic memory operations.
MCTargetStreamer * createX86ObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI)
Implements X86-only directives for object files.
MCTargetStreamer * createX86AsmTargetStreamer(MCStreamer &S, formatted_raw_ostream &OS, MCInstPrinter *InstPrinter)
Implements X86-only directives for assembly emission.
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...