Fix error in recursive macro expansion

This commit is contained in:
drmortalwombat 2022-12-13 08:18:25 +01:00
parent 0055911491
commit de3c7415b3
3 changed files with 25 additions and 11 deletions

View File

@ -14815,6 +14815,9 @@ NativeCodeBasicBlock * NativeCodeBasicBlock::SplitMatchingTails(NativeCodeProced
nblock->mEntryBlocks.Push(bi); nblock->mEntryBlocks.Push(bi);
nblock->mNumEntries++; nblock->mNumEntries++;
nblock->mEntryRequiredRegs = mEntryRequiredRegs;
nblock->mExitRequiredRegs = mEntryRequiredRegs;
bi->mTrueJump = nblock; bi->mTrueJump = nblock;
mEntryBlocks[i] = nullptr; mEntryBlocks[i] = nullptr;
mNumEntries--; mNumEntries--;
@ -14863,6 +14866,9 @@ NativeCodeBasicBlock* NativeCodeBasicBlock::AddDominatorBlock(NativeCodeProcedur
mEntryBlocks[mEntryBlocks.IndexOf(pblock)] = tblock; mEntryBlocks[mEntryBlocks.IndexOf(pblock)] = tblock;
tblock->mEntryRequiredRegs = mEntryRequiredRegs;
tblock->mExitRequiredRegs = mEntryRequiredRegs;
if (pblock->mTrueJump == this) if (pblock->mTrueJump == this)
pblock->mTrueJump = tblock; pblock->mTrueJump = tblock;
if (pblock->mFalseJump == this) if (pblock->mFalseJump == this)
@ -26936,8 +26942,10 @@ bool NativeCodeBasicBlock::PeepHoleOptimizer(NativeCodeProcedure* proc, int pass
mIns[i + 1].mType == ASMIT_DEC && mIns[i + 1].mMode == ASMIM_ZERO_PAGE && mIns[i + 1].mAddress == mIns[i + 0].mAddress && mIns[i + 1].mType == ASMIT_DEC && mIns[i + 1].mMode == ASMIM_ZERO_PAGE && mIns[i + 1].mAddress == mIns[i + 0].mAddress &&
!(mIns[i + 2].mLive & LIVE_CPU_REG_C)) !(mIns[i + 2].mLive & LIVE_CPU_REG_C))
{ {
mIns[i + 0].mType = ASMIT_SEC; mIns[i + 0].mMode = ASMIM_IMPLIED; mIns[i + 0].mLive |= LIVE_CPU_REG_C; mIns[i + 0].mType = ASMIT_SEC; mIns[i + 0].mMode = ASMIM_IMPLIED; mIns[i + 0].mLive |= LIVE_CPU_REG_C | LIVE_CPU_REG_A;
mIns[i + 1].mType = ASMIT_SBC; mIns[i + 1].mMode = ASMIM_IMMEDIATE; mIns[i + 1].mAddress = 1; mIns[i + 1].mType = ASMIT_SBC; mIns[i + 1].mMode = ASMIM_IMMEDIATE; mIns[i + 1].mAddress = 1; mIns[i + 1].mLive |= LIVE_CPU_REG_A;
if (mIns[i + 2].mLive & LIVE_CPU_REG_Z)
mIns[i + 1].mLive |= LIVE_CPU_REG_Z;
mIns[i + 2].mType = ASMIT_STA; mIns[i + 2].mType = ASMIT_STA;
progress = true; progress = true;
} }

View File

@ -149,8 +149,8 @@ const char* TokenNames[] =
}; };
Macro::Macro(const Ident* ident) Macro::Macro(const Ident* ident, MacroDict * scope)
: mIdent(ident), mString(nullptr), mNumArguments(-1) : mIdent(ident), mString(nullptr), mNumArguments(-1), mScope(scope)
{ {
} }
@ -389,7 +389,7 @@ static inline int HexValue(char ch)
void Scanner::AddMacro(const Ident* ident, const char* value) void Scanner::AddMacro(const Ident* ident, const char* value)
{ {
Macro* macro = new Macro(ident); Macro* macro = new Macro(ident, nullptr);
macro->SetString(value); macro->SetString(value);
mDefines->Insert(macro); mDefines->Insert(macro);
} }
@ -501,7 +501,7 @@ void Scanner::NextToken(void)
{ {
if (mToken != TK_IDENT) if (mToken != TK_IDENT)
mTokenIdent = Ident::Unique(TokenNames[mToken]); mTokenIdent = Ident::Unique(TokenNames[mToken]);
Macro* macro = new Macro(mTokenIdent); Macro* macro = new Macro(mTokenIdent, nullptr);
if (mTokenChar == '(') if (mTokenChar == '(')
{ {
@ -614,7 +614,7 @@ void Scanner::NextToken(void)
Macro* macro = mDefines->Lookup(ident); Macro* macro = mDefines->Lookup(ident);
if (!macro) if (!macro)
{ {
macro = new Macro(ident); macro = new Macro(ident, nullptr);
mDefines->Insert(macro); mDefines->Insert(macro);
} }
char buffer[20]; char buffer[20];
@ -739,6 +739,8 @@ void Scanner::NextToken(void)
} }
else if (def->mNumArguments > 0) else if (def->mNumArguments > 0)
{ {
MacroDict* scope = mDefineArguments;
mDefineArguments = new MacroDict(); mDefineArguments = new MacroDict();
NextRawToken(); NextRawToken();
if (mToken == TK_OPEN_PARENTHESIS) if (mToken == TK_OPEN_PARENTHESIS)
@ -765,7 +767,7 @@ void Scanner::NextToken(void)
} }
offset++; offset++;
} }
Macro* arg = new Macro(def->mArguments[i]); Macro* arg = new Macro(def->mArguments[i], scope);
arg->SetString(mLine + mOffset, offset - mOffset); arg->SetString(mLine + mOffset, offset - mOffset);
mDefineArguments->Insert(arg); mDefineArguments->Insert(arg);
mOffset = offset; mOffset = offset;
@ -790,7 +792,7 @@ void Scanner::NextToken(void)
NextChar(); NextChar();
} }
else else
mDefineArguments = nullptr; mDefineArguments = def->mScope;
ex->mLine = mLine; ex->mLine = mLine;
ex->mOffset = mOffset; ex->mOffset = mOffset;
@ -1628,7 +1630,7 @@ bool Scanner::NextChar(void)
if (mMacroExpansion) if (mMacroExpansion)
{ {
MacroExpansion* mac = mMacroExpansion->mLink; MacroExpansion* mac = mMacroExpansion->mLink;
delete mDefineArguments; // delete mDefineArguments;
mLine = mMacroExpansion->mLine; mLine = mMacroExpansion->mLine;
mOffset = mMacroExpansion->mOffset; mOffset = mMacroExpansion->mOffset;

View File

@ -150,10 +150,12 @@ enum Token
extern const char* TokenNames[]; extern const char* TokenNames[];
class MacroDict;
class Macro class Macro
{ {
public: public:
Macro(const Ident* ident); Macro(const Ident* ident, MacroDict* scope);
~Macro(void); ~Macro(void);
void SetString(const char* str); void SetString(const char* str);
@ -164,6 +166,7 @@ public:
const char* mString; const char* mString;
int mNumArguments; int mNumArguments;
const Ident * mArguments[32]; const Ident * mArguments[32];
MacroDict* mScope;
}; };
typedef Macro* MacroPtr; typedef Macro* MacroPtr;
@ -181,6 +184,7 @@ public:
protected: protected:
MacroPtr * mHash; MacroPtr * mHash;
int mHashSize, mHashFill; int mHashSize, mHashFill;
MacroDict * mParent;
}; };
class Scanner class Scanner