Fix inline asm labels in template functions.

This is a giant kludge to avoid a redesign of the inline asm
handling code. I'd be glad if somebody came up with a better
solution.

GitHub: Fixes #340.
This commit is contained in:
David Nadlinger
2013-05-11 20:57:42 +02:00
parent 15fd67be03
commit 5742a0219c
2 changed files with 26 additions and 1 deletions

View File

@@ -1890,7 +1890,9 @@ namespace AsmParserx8664
void addLabel ( char* id )
{
printLabelName(insnTemplate, sc->func->mangle(), id);
// We need to delay emitting the actual function name, see
// replace_func_name in asmstmt.cpp for details.
printLabelName(insnTemplate, "<<func>>", id);
}
/* Determines whether the operand is a register, memory reference

View File

@@ -149,6 +149,26 @@ AsmParserCommon* asmparser = NULL;
#include "asm-x86.h" // x86_64 assembly parser
#undef ASM_X86_64
/**
* Replaces <<func>> with the name of the currently codegen'd function.
*
* This kludge is required to handle labels correctly, as the instruction
* strings for jumps, … are generated during semantic3, but attribute inference
* might change the function type (and hence the mangled name) right at the end
* of semantic3.
*/
static void replace_func_name(IRState* p, std::string& insnt)
{
static const std::string needle("<<func>>");
size_t pos;
while (std::string::npos != (pos = insnt.find(needle)))
{
// This will only happen for few instructions, and only once for those.
insnt.replace(pos, needle.size(), p->func()->decl->mangle(false));
}
}
Statement *AsmStatement::semantic(Scope *sc)
{
if (sc->func && sc->func->isSafe())
@@ -442,6 +462,8 @@ AsmStatement::toIR(IRState * irs)
// excessive commas are removed later...
replace_func_name(irs, code->insnTemplate);
// push asm statement
IRAsmStmt* asmStmt = new IRAsmStmt;
asmStmt->code = code->insnTemplate;
@@ -836,6 +858,7 @@ void AsmStatement::toNakedIR(IRState *p)
AsmCode * code = (AsmCode *) asmcode;
// build asm stmt
replace_func_name(p, code->insnTemplate);
p->nakedAsm << "\t" << code->insnTemplate << std::endl;
}