36 bool emitFPOEndPrologue(
SMLoc L)
override;
37 bool emitFPOEndProc(
SMLoc L)
override;
39 bool emitFPOPushReg(
unsigned Reg,
SMLoc L)
override;
40 bool emitFPOStackAlloc(
unsigned StackAlloc,
SMLoc L)
override;
41 bool emitFPOStackAlign(
unsigned Align,
SMLoc L)
override;
42 bool emitFPOSetFrame(
unsigned Reg,
SMLoc L)
override;
46 struct FPOInstruction {
62 unsigned ParamsSize = 0;
73 std::unique_ptr<FPOData> CurFPOData;
75 bool haveOpenFPOData() {
return !!CurFPOData; }
79 bool checkInFPOPrologue(
SMLoc L);
83 MCContext &getContext() {
return getStreamer().getContext(); }
90 bool emitFPOEndPrologue(
SMLoc L)
override;
91 bool emitFPOEndProc(
SMLoc L)
override;
93 bool emitFPOPushReg(
unsigned Reg,
SMLoc L)
override;
94 bool emitFPOStackAlloc(
unsigned StackAlloc,
SMLoc L)
override;
95 bool emitFPOStackAlign(
unsigned Align,
SMLoc L)
override;
96 bool emitFPOSetFrame(
unsigned Reg,
SMLoc L)
override;
101 unsigned ParamsSize,
SMLoc L) {
102 OS <<
"\t.cv_fpo_proc\t";
103 ProcSym->print(OS, getStreamer().getContext().getAsmInfo());
104 OS <<
' ' << ParamsSize <<
'\n';
108 bool X86WinCOFFAsmTargetStreamer::emitFPOEndPrologue(
SMLoc L) {
109 OS <<
"\t.cv_fpo_endprologue\n";
113 bool X86WinCOFFAsmTargetStreamer::emitFPOEndProc(
SMLoc L) {
114 OS <<
"\t.cv_fpo_endproc\n";
120 OS <<
"\t.cv_fpo_data\t";
121 ProcSym->print(OS, getStreamer().getContext().getAsmInfo());
126 bool X86WinCOFFAsmTargetStreamer::emitFPOPushReg(
unsigned Reg,
SMLoc L) {
127 OS <<
"\t.cv_fpo_pushreg\t";
128 InstPrinter.printRegName(OS,
Reg);
133 bool X86WinCOFFAsmTargetStreamer::emitFPOStackAlloc(
unsigned StackAlloc,
135 OS <<
"\t.cv_fpo_stackalloc\t" << StackAlloc <<
'\n';
139 bool X86WinCOFFAsmTargetStreamer::emitFPOStackAlign(
unsigned Align,
SMLoc L) {
140 OS <<
"\t.cv_fpo_stackalign\t" <<
Align <<
'\n';
144 bool X86WinCOFFAsmTargetStreamer::emitFPOSetFrame(
unsigned Reg,
SMLoc L) {
145 OS <<
"\t.cv_fpo_setframe\t";
146 InstPrinter.printRegName(OS,
Reg);
151 bool X86WinCOFFTargetStreamer::checkInFPOPrologue(
SMLoc L) {
152 if (!haveOpenFPOData() || CurFPOData->PrologueEnd) {
153 getContext().reportError(
155 "directive must appear between .cv_fpo_proc and .cv_fpo_endprologue");
161 MCSymbol *X86WinCOFFTargetStreamer::emitFPOLabel() {
162 MCSymbol *
Label = getContext().createTempSymbol(
"cfi",
true);
163 getStreamer().emitLabel(Label);
168 unsigned ParamsSize,
SMLoc L) {
169 if (haveOpenFPOData()) {
170 getContext().reportError(
171 L,
"opening new .cv_fpo_proc before closing previous frame");
174 CurFPOData = std::make_unique<FPOData>();
175 CurFPOData->Function =
ProcSym;
176 CurFPOData->Begin = emitFPOLabel();
177 CurFPOData->ParamsSize = ParamsSize;
181 bool X86WinCOFFTargetStreamer::emitFPOEndProc(
SMLoc L) {
182 if (!haveOpenFPOData()) {
183 getContext().reportError(L,
".cv_fpo_endproc must appear after .cv_proc");
186 if (!CurFPOData->PrologueEnd) {
188 if (!CurFPOData->Instructions.empty()) {
189 getContext().reportError(L,
"missing .cv_fpo_endprologue");
190 CurFPOData->Instructions.clear();
195 CurFPOData->PrologueEnd = CurFPOData->Begin;
198 CurFPOData->End = emitFPOLabel();
199 const MCSymbol *Fn = CurFPOData->Function;
200 AllFPOData.insert({Fn,
std::move(CurFPOData)});
204 bool X86WinCOFFTargetStreamer::emitFPOSetFrame(
unsigned Reg,
SMLoc L) {
205 if (checkInFPOPrologue(L))
208 Inst.Label = emitFPOLabel();
209 Inst.Op = FPOInstruction::SetFrame;
210 Inst.RegOrOffset =
Reg;
211 CurFPOData->Instructions.push_back(Inst);
215 bool X86WinCOFFTargetStreamer::emitFPOPushReg(
unsigned Reg,
SMLoc L) {
216 if (checkInFPOPrologue(L))
219 Inst.Label = emitFPOLabel();
220 Inst.Op = FPOInstruction::PushReg;
221 Inst.RegOrOffset =
Reg;
222 CurFPOData->Instructions.push_back(Inst);
226 bool X86WinCOFFTargetStreamer::emitFPOStackAlloc(
unsigned StackAlloc,
SMLoc L) {
227 if (checkInFPOPrologue(L))
230 Inst.Label = emitFPOLabel();
231 Inst.Op = FPOInstruction::StackAlloc;
232 Inst.RegOrOffset = StackAlloc;
233 CurFPOData->Instructions.push_back(Inst);
237 bool X86WinCOFFTargetStreamer::emitFPOStackAlign(
unsigned Align,
SMLoc L) {
238 if (checkInFPOPrologue(L))
240 if (
llvm::none_of(CurFPOData->Instructions, [](
const FPOInstruction &Inst) {
241 return Inst.Op == FPOInstruction::SetFrame;
243 getContext().reportError(
244 L,
"a frame register must be established before aligning the stack");
248 Inst.Label = emitFPOLabel();
250 Inst.RegOrOffset =
Align;
251 CurFPOData->Instructions.push_back(Inst);
255 bool X86WinCOFFTargetStreamer::emitFPOEndPrologue(
SMLoc L) {
256 if (checkInFPOPrologue(L))
258 CurFPOData->PrologueEnd = emitFPOLabel();
263 struct RegSaveOffset {
270 struct FPOStateMachine {
271 explicit FPOStateMachine(
const FPOData *FPO) :
FPO(
FPO) {}
273 const FPOData *
FPO =
nullptr;
274 unsigned FrameReg = 0;
275 unsigned FrameRegOff = 0;
276 unsigned CurOffset = 0;
278 unsigned SavedRegSize = 0;
279 unsigned StackOffsetBeforeAlign = 0;
303 case X86::EBP: OS <<
"$ebp";
break;
304 case X86::EIP: OS <<
"$eip";
break;
307 OS <<
'$' <<
MRI->getCodeViewRegNum(LLVMReg);
314 unsigned CurFlags = Flags;
315 if (Label ==
FPO->Begin)
323 "cannot align stack without frame reg");
328 FuncOS << CFAVar <<
' ' <<
printFPOReg(
MRI, FrameReg) <<
' ' << FrameRegOff
336 FuncOS <<
"$T0 " << CFAVar <<
' ' << StackOffsetBeforeAlign <<
" - "
344 FuncOS << CFAVar <<
" .raSearch = ";
348 FuncOS <<
"$eip " << CFAVar <<
" ^ = ";
349 FuncOS <<
"$esp " << CFAVar <<
" 4 + = ";
352 for (RegSaveOffset RO : RegSaveOffsets)
353 FuncOS <<
printFPOReg(
MRI, RO.Reg) <<
' ' << CFAVar <<
' ' << RO.Offset
361 unsigned MaxStackSize = 0;
391 if (
I == AllFPOData.end()) {
396 const FPOData *
FPO =
I->second.get();
397 assert(
FPO->Begin &&
FPO->End &&
FPO->PrologueEnd &&
"missing FPO label");
412 FPOStateMachine FSM(FPO);
414 FSM.emitFrameDataRecord(OS,
FPO->Begin);
415 for (
const FPOInstruction &Inst :
FPO->Instructions) {
417 case FPOInstruction::PushReg:
419 FSM.SavedRegSize += 4;
420 FSM.RegSaveOffsets.push_back({Inst.RegOrOffset, FSM.CurOffset});
422 case FPOInstruction::SetFrame:
423 FSM.FrameReg = Inst.RegOrOffset;
424 FSM.FrameRegOff = FSM.CurOffset;
427 FSM.StackOffsetBeforeAlign = FSM.CurOffset;
428 FSM.StackAlign = Inst.RegOrOffset;
430 case FPOInstruction::StackAlloc:
431 FSM.CurOffset += Inst.RegOrOffset;
432 FSM.LocalSize += Inst.RegOrOffset;
438 FSM.emitFrameDataRecord(OS, Inst.Label);
452 return new X86WinCOFFAsmTargetStreamer(
S, OS, *InstPrinter);
461 return new X86WinCOFFTargetStreamer(
S);