Skip to content

Commit e619541

Browse files
committed
Fix MonoException
Add DebugHandler to be able to collect labels info on begin instruction
1 parent 845a24e commit e619541

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,8 @@ bool AsmPrinter::doInitialization(Module &M) {
633633
if (EnableMonoEH) {
634634
MonoException *mono_eh = new MonoException (this, DisableGNUEH);
635635
Handlers.push_back(std::unique_ptr<MonoException> (mono_eh));
636+
MonoExceptionDebugHandler *mono_eh_dbg = new MonoExceptionDebugHandler(this, mono_eh);
637+
addDebugHandler(std::unique_ptr<MonoExceptionDebugHandler> (mono_eh_dbg));
636638
}
637639

638640
return false;

llvm/lib/CodeGen/AsmPrinter/MonoException.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,3 +652,27 @@ MonoException::endModule()
652652
streamer.emitLabel(tableEndSym);
653653
Asm->emitAlignment(llvm::Align(8));
654654
}
655+
656+
MonoExceptionDebugHandler::MonoExceptionDebugHandler(AsmPrinter *A, MonoException *ME)
657+
: DebugHandlerBase(A), me(ME)
658+
{
659+
}
660+
661+
void
662+
MonoExceptionDebugHandler::beginInstruction(const MachineInstr *MI)
663+
{
664+
if (MI->getOpcode() == TargetOpcode::CFI_INSTRUCTION) {
665+
unsigned CFIIndex = MI->getOperand(0).getCFIIndex();
666+
667+
//outs () << "D: " << CFIIndex << " " << EHLabels.size() << "\n";
668+
669+
/* Emit a label and save the label-cfi index association */
670+
if (CFIIndex != me->EHLabels.size())
671+
assert (0);
672+
673+
MCSymbol *Label = Asm->OutContext.createTempSymbol();
674+
Asm->OutStreamer->emitLabel(Label);
675+
676+
me->EHLabels.push_back(Label);
677+
}
678+
}

llvm/lib/CodeGen/AsmPrinter/MonoException.h

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@
1212

1313
#include "EHStreamer.h"
1414
#include "llvm/CodeGen/AsmPrinter.h"
15+
#include "llvm/CodeGen/DebugHandlerBase.h"
1516

1617
namespace llvm {
1718

1819
class TargetRegisterInfo;
1920

20-
// TODO: we need to use DebugHandlerBase and addDebugHandler, so that beginInstruction is called
21-
// context https://github.com/llvm/llvm-project/pull/96785
2221
class MonoException : public EHStreamer {
2322
public:
2423
MonoException(AsmPrinter *A, bool disableGNUEH);
@@ -30,6 +29,8 @@ class MonoException : public EHStreamer {
3029

3130
void endFunction(const MachineFunction *) override;
3231

32+
std::vector<MCSymbol*> EHLabels;
33+
3334
private:
3435

3536
struct MonoCallSiteEntry {
@@ -71,12 +72,28 @@ class MonoException : public EHStreamer {
7172
void EmitFnStart();
7273
void EmitFnEnd();
7374

74-
std::vector<MCSymbol*> EHLabels;
7575
std::vector<EHInfo> Frames;
7676
StringMap<int> FuncIndexes;
7777
const TargetRegisterInfo *RI;
7878
bool DisableGNUEH;
7979
};
80+
81+
class MonoExceptionDebugHandler : public DebugHandlerBase {
82+
83+
public:
84+
85+
MonoExceptionDebugHandler(AsmPrinter *A, MonoException* ME);
86+
87+
void beginInstruction(const MachineInstr *MI) override;
88+
89+
void beginFunctionImpl(const MachineFunction *MF) override {}
90+
void endFunctionImpl(const MachineFunction *MF) override {}
91+
void endModule() override {}
92+
93+
private:
94+
MonoException* me;
95+
};
96+
8097
} // End of namespace llvm
8198

8299
#endif

0 commit comments

Comments
 (0)