Skip to content

Commit 613b757

Browse files
committed
First part of support for generating dwarf for assembly source files with the
-g flag. In this part we generate the .file for the source being assembled and the .loc's for the assembled instructions. The next part will be to generate the dwarf Compile Unit DIE and a dwarf subprogram DIE for each non-temporary label. Once the next part is done test cases will be added. rdar://9275556 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@143509 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 60cb643 commit 613b757

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

include/llvm/MC/MCContext.h

+18
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,17 @@ namespace llvm {
9898
MCDwarfLoc CurrentDwarfLoc;
9999
bool DwarfLocSeen;
100100

101+
/// Generate dwarf debugging info for assembly source files.
102+
bool GenDwarfForAssembly;
103+
104+
/// The current dwarf file number when generate dwarf debugging info for
105+
/// assembly source files.
106+
unsigned GenDwarfFileNumber;
107+
108+
/// The default initial text section that we generate dwarf debugging line
109+
/// info for when generating dwarf assembly source files.
110+
const MCSection *GenDwarfSection;
111+
101112
/// Honor temporary labels, this is useful for debugging semantic
102113
/// differences between temporary and non-temporary labels (primarily on
103114
/// Darwin).
@@ -252,6 +263,13 @@ namespace llvm {
252263
bool getDwarfLocSeen() { return DwarfLocSeen; }
253264
const MCDwarfLoc &getCurrentDwarfLoc() { return CurrentDwarfLoc; }
254265

266+
bool getGenDwarfForAssembly() { return GenDwarfForAssembly; }
267+
void setGenDwarfForAssembly(bool Value) { GenDwarfForAssembly = Value; }
268+
unsigned getGenDwarfFileNumber() { return GenDwarfFileNumber; }
269+
unsigned nextGenDwarfFileNumber() { return ++GenDwarfFileNumber; }
270+
const MCSection *getGenDwarfSection() { return GenDwarfSection; }
271+
void setGenDwarfSection(const MCSection *Sec) { GenDwarfSection = Sec; }
272+
255273
/// @}
256274

257275
char *getSecureLogFile() { return SecureLogFile; }

lib/MC/MCContext.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ MCContext::MCContext(const MCAsmInfo &mai, const MCRegisterInfo &mri,
4343
SecureLogUsed = false;
4444

4545
DwarfLocSeen = false;
46+
GenDwarfForAssembly = false;
47+
GenDwarfFileNumber = 0;
4648
}
4749

4850
MCContext::~MCContext() {

lib/MC/MCParser/AsmParser.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,14 @@ bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
464464
HadError = false;
465465
AsmCond StartingCondState = TheCondState;
466466

467+
// If we are generating dwarf for assembly source files save the initial text
468+
// section and generate a .file directive.
469+
if (getContext().getGenDwarfForAssembly()) {
470+
getContext().setGenDwarfSection(getStreamer().getCurrentSection());
471+
getStreamer().EmitDwarfFileDirective(getContext().nextGenDwarfFileNumber(),
472+
StringRef(), SrcMgr.getMemoryBuffer(CurBuffer)->getBufferIdentifier());
473+
}
474+
467475
// While we have input, parse each statement.
468476
while (Lexer.isNot(AsmToken::Eof)) {
469477
if (!ParseStatement()) continue;
@@ -1211,6 +1219,18 @@ bool AsmParser::ParseStatement() {
12111219
PrintMessage(IDLoc, SourceMgr::DK_Note, OS.str());
12121220
}
12131221

1222+
// If we are generating dwarf for assembly source files and the current
1223+
// section is the initial text section then generate a .loc directive for
1224+
// the instruction.
1225+
if (!HadError && getContext().getGenDwarfForAssembly() &&
1226+
getContext().getGenDwarfSection() == getStreamer().getCurrentSection() ) {
1227+
getStreamer().EmitDwarfLocDirective(getContext().getGenDwarfFileNumber(),
1228+
SrcMgr.FindLineNumber(IDLoc, CurBuffer),
1229+
0, DWARF2_LINE_DEFAULT_IS_STMT ?
1230+
DWARF2_FLAG_IS_STMT : 0, 0, 0,
1231+
StringRef());
1232+
}
1233+
12141234
// If parsing succeeded, match the instruction.
12151235
if (!HadError)
12161236
HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
@@ -2342,6 +2362,10 @@ bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
23422362
if (getLexer().isNot(AsmToken::EndOfStatement))
23432363
return TokError("unexpected token in '.file' directive");
23442364

2365+
if (getContext().getGenDwarfForAssembly() == true)
2366+
Error(DirectiveLoc, "input can't have .file dwarf directives when -g is "
2367+
"used to generate dwarf debug info for assembly code");
2368+
23452369
if (FileNumber == -1)
23462370
getStreamer().EmitFileDirective(Filename);
23472371
else {

tools/llvm-mc/llvm-mc.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ NoInitialTextSection("n", cl::desc("Don't assume assembly file starts "
152152
static cl::opt<bool>
153153
SaveTempLabels("L", cl::desc("Don't discard temporary labels"));
154154

155+
static cl::opt<bool>
156+
GenDwarfForAssembly("g", cl::desc("Generate dwarf debugging info for assembly "
157+
"source files"));
158+
155159
enum ActionType {
156160
AC_AsLex,
157161
AC_Assemble,
@@ -377,6 +381,8 @@ static int AssembleInput(const char *ProgName) {
377381
if (SaveTempLabels)
378382
Ctx.setAllowTemporaryLabels(false);
379383

384+
Ctx.setGenDwarfForAssembly(GenDwarfForAssembly);
385+
380386
// Package up features to be passed to target/subtarget
381387
std::string FeaturesStr;
382388
if (MAttrs.size()) {

0 commit comments

Comments
 (0)