Skip to content

Commit

Permalink
Anonymous labels work across macro invocations
Browse files Browse the repository at this point in the history
  • Loading branch information
mungre committed Aug 26, 2024
1 parent 3bdee75 commit 845ac61
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/anonymouslabels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ AnonymousLabels AnonymousLabels::m_gInstance;


// Update the forward references when a '+' label is seen
void AnonymousLabels::UpdateForwardReferences(int pc)
void AnonymousLabelsData::UpdateForwardReferences(int pc)
{
for (std::vector<ScopedSymbolName>::iterator it = m_forwardReferences.begin(); it != m_forwardReferences.end(); ++it)
{
Expand Down
43 changes: 40 additions & 3 deletions src/anonymouslabels.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@
#include <vector>


class AnonymousLabels
class AnonymousLabelsData
{
public:
static inline AnonymousLabels& Instance() { return m_gInstance; }

// Get PC of last '-' label
int GetBackReference() { return m_backReference; }
// Set PC of '-' label
Expand All @@ -47,6 +45,45 @@ class AnonymousLabels
private:
int m_backReference;
std::vector<ScopedSymbolName> m_forwardReferences;
};


class AnonymousLabels
{
public:
static inline AnonymousLabelsData& Instance() { return m_gInstance.Current(); }

AnonymousLabels() : m_level(0)
{
}

static void EnterMacro() { m_gInstance.Enter(); }
static void LeaveMacro() { m_gInstance.Leave(); }

private:
void Enter()
{
m_level++;
}
void Leave()
{
if ( m_level < m_data.size() )
{
m_data[m_level].Clear();
}
m_level--;
}
AnonymousLabelsData& Current()
{
if ( m_level >= m_data.size() )
{
m_data.resize(m_level + 1);
}
return m_data[m_level];
}

unsigned int m_level;
std::vector<AnonymousLabelsData> m_data;

static AnonymousLabels m_gInstance;
};
Expand Down
26 changes: 15 additions & 11 deletions src/lineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "symboltable.h"
#include "globaldata.h"
#include "sourcefile.h"
#include "anonymouslabels.h"


using namespace std;
Expand Down Expand Up @@ -124,17 +125,6 @@ void LineParser::Process( const string& line )

m_column = oldColumn;

// Check for an anonymous label declaration, + or -
// Must be followed by a newline or space.
if ( !bIsSymbolAssignment )
{
if (( m_line[ m_column ] == '+' || m_line[ m_column ] == '-' ) && (m_column + 1 == m_line.length() || m_line[ m_column + 1 ] == ' '))
{
HandleAnonymousLabel();
continue;
}
}

// first check tokens - they have priority over opcodes, so that they can have names
// like INCLUDE (which would otherwise be interpreted as INC LUDE)

Expand All @@ -159,6 +149,18 @@ void LineParser::Process( const string& line )
continue;
}

// Check for an anonymous label declaration, + or -
// Must be followed by a newline or space.

if ( !bIsSymbolAssignment )
{
if (( m_line[ m_column ] == '+' || m_line[ m_column ] == '-' ) && (m_column + 1 == m_line.length() || m_line[ m_column + 1 ] == ' '))
{
HandleAnonymousLabel();
continue;
}
}

// No token match - check against opcodes

if ( !bIsSymbolAssignment )
Expand Down Expand Up @@ -238,6 +240,7 @@ void LineParser::Process( const string& line )
cout << "Macro " << macroName << ":" << endl;
}

AnonymousLabels::EnterMacro();
HandleOpenBrace();

for ( int i = 0; i < macro->GetNumberOfParameters(); i++ )
Expand Down Expand Up @@ -291,6 +294,7 @@ void LineParser::Process( const string& line )
macroInstance.Process();

HandleCloseBrace();
AnonymousLabels::LeaveMacro();

if ( m_sourceCode->ShouldOutputAsm() )
{
Expand Down
22 changes: 22 additions & 0 deletions test/4-assembler/anonmacro.6502
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
MACRO anon N
LDY #N
- JSR &FFEE
DEY
BNE -
ENDMACRO

ORG &2000
.start
LDX #0
-
LDA text,X
BEQ +
anon 2
INX
BNE -
+ RTS
.text
EQUS "Hello", 0
.end

SAVE "hello",start,end
Binary file added test/4-assembler/anonmacro.gold.ssd
Binary file not shown.

0 comments on commit 845ac61

Please sign in to comment.