mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-20 18:22:07 +00:00
Split macro arg management into its own file
It has no relation to symbols, and helps a tiny bit deflate `symbol.c`
This commit is contained in:
1
Makefile
1
Makefile
@@ -57,6 +57,7 @@ rgbasm_obj := \
|
|||||||
src/asm/fstack.o \
|
src/asm/fstack.o \
|
||||||
src/asm/globlex.o \
|
src/asm/globlex.o \
|
||||||
src/asm/lexer.o \
|
src/asm/lexer.o \
|
||||||
|
src/asm/macro.o \
|
||||||
src/asm/main.o \
|
src/asm/main.o \
|
||||||
src/asm/math.o \
|
src/asm/math.o \
|
||||||
src/asm/output.o \
|
src/asm/output.o \
|
||||||
|
|||||||
28
include/asm/macro.h
Normal file
28
include/asm/macro.h
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of RGBDS.
|
||||||
|
*
|
||||||
|
* Copyright (c) 2020, Carsten Sorensen and RGBDS contributors.
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef RGBDS_MACRO_H
|
||||||
|
#define RGBDS_MACRO_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#include "helpers.h"
|
||||||
|
|
||||||
|
void sym_AddNewMacroArg(char const *s);
|
||||||
|
void sym_SaveCurrentMacroArgs(char *save[]);
|
||||||
|
void sym_RestoreCurrentMacroArgs(char *save[]);
|
||||||
|
void sym_UseNewMacroArgs(void);
|
||||||
|
char *sym_FindMacroArg(int32_t i);
|
||||||
|
void sym_UseCurrentMacroArgs(void);
|
||||||
|
void sym_SetMacroArgID(uint32_t nMacroCount);
|
||||||
|
void sym_ShiftCurrentMacroArgs(void);
|
||||||
|
uint32_t sym_NbMacroArgs(void);
|
||||||
|
|
||||||
|
void macro_Init(void);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -90,23 +90,14 @@ struct sSymbol *sym_AddLocalReloc(char const *tzSym);
|
|||||||
struct sSymbol *sym_AddReloc(char const *tzSym);
|
struct sSymbol *sym_AddReloc(char const *tzSym);
|
||||||
void sym_Export(char const *tzSym);
|
void sym_Export(char const *tzSym);
|
||||||
struct sSymbol *sym_FindMacro(char const *s);
|
struct sSymbol *sym_FindMacro(char const *s);
|
||||||
void sym_InitNewMacroArgs(void);
|
|
||||||
void sym_AddNewMacroArg(char const *s);
|
|
||||||
void sym_SaveCurrentMacroArgs(char *save[]);
|
|
||||||
void sym_RestoreCurrentMacroArgs(char *save[]);
|
|
||||||
void sym_UseNewMacroArgs(void);
|
|
||||||
struct sSymbol *sym_AddEqu(char const *tzSym, int32_t value);
|
struct sSymbol *sym_AddEqu(char const *tzSym, int32_t value);
|
||||||
struct sSymbol *sym_AddSet(char const *tzSym, int32_t value);
|
struct sSymbol *sym_AddSet(char const *tzSym, int32_t value);
|
||||||
void sym_Init(void);
|
void sym_Init(void);
|
||||||
uint32_t sym_GetConstantValue(char const *s);
|
uint32_t sym_GetConstantValue(char const *s);
|
||||||
struct sSymbol *sym_FindSymbol(char const *tzName);
|
struct sSymbol *sym_FindSymbol(char const *tzName);
|
||||||
char *sym_FindMacroArg(int32_t i);
|
|
||||||
char *sym_GetStringValue(struct sSymbol const *sym);
|
char *sym_GetStringValue(struct sSymbol const *sym);
|
||||||
void sym_UseCurrentMacroArgs(void);
|
|
||||||
void sym_SetMacroArgID(uint32_t nMacroCount);
|
|
||||||
struct sSymbol *sym_AddMacro(char const *tzSym, int32_t nDefLineNo);
|
struct sSymbol *sym_AddMacro(char const *tzSym, int32_t nDefLineNo);
|
||||||
void sym_Ref(char const *tzSym);
|
void sym_Ref(char const *tzSym);
|
||||||
void sym_ShiftCurrentMacroArgs(void);
|
|
||||||
struct sSymbol *sym_AddString(char const *tzSym, char const *tzValue);
|
struct sSymbol *sym_AddString(char const *tzSym, char const *tzValue);
|
||||||
uint32_t sym_GetDefinedValue(char const *s);
|
uint32_t sym_GetDefinedValue(char const *s);
|
||||||
void sym_Purge(char const *tzName);
|
void sym_Purge(char const *tzName);
|
||||||
|
|||||||
@@ -20,6 +20,7 @@
|
|||||||
#include "asm/charmap.h"
|
#include "asm/charmap.h"
|
||||||
#include "asm/fstack.h"
|
#include "asm/fstack.h"
|
||||||
#include "asm/lexer.h"
|
#include "asm/lexer.h"
|
||||||
|
#include "asm/macro.h"
|
||||||
#include "asm/main.h"
|
#include "asm/main.h"
|
||||||
#include "asm/mymath.h"
|
#include "asm/mymath.h"
|
||||||
#include "asm/rpn.h"
|
#include "asm/rpn.h"
|
||||||
|
|||||||
@@ -19,9 +19,9 @@
|
|||||||
|
|
||||||
#include "asm/fstack.h"
|
#include "asm/fstack.h"
|
||||||
#include "asm/lexer.h"
|
#include "asm/lexer.h"
|
||||||
|
#include "asm/macro.h"
|
||||||
#include "asm/main.h"
|
#include "asm/main.h"
|
||||||
#include "asm/output.h"
|
#include "asm/output.h"
|
||||||
#include "asm/symbol.h"
|
|
||||||
#include "asm/warning.h"
|
#include "asm/warning.h"
|
||||||
|
|
||||||
#include "extern/err.h"
|
#include "extern/err.h"
|
||||||
|
|||||||
@@ -15,10 +15,10 @@
|
|||||||
|
|
||||||
#include "asm/asm.h"
|
#include "asm/asm.h"
|
||||||
#include "asm/lexer.h"
|
#include "asm/lexer.h"
|
||||||
|
#include "asm/macro.h"
|
||||||
#include "asm/main.h"
|
#include "asm/main.h"
|
||||||
#include "asm/rpn.h"
|
#include "asm/rpn.h"
|
||||||
#include "asm/section.h"
|
#include "asm/section.h"
|
||||||
#include "asm/symbol.h"
|
|
||||||
#include "asm/warning.h"
|
#include "asm/warning.h"
|
||||||
|
|
||||||
#include "helpers.h"
|
#include "helpers.h"
|
||||||
|
|||||||
@@ -17,6 +17,7 @@
|
|||||||
#include "asm/asm.h"
|
#include "asm/asm.h"
|
||||||
#include "asm/fstack.h"
|
#include "asm/fstack.h"
|
||||||
#include "asm/lexer.h"
|
#include "asm/lexer.h"
|
||||||
|
#include "asm/macro.h"
|
||||||
#include "asm/main.h"
|
#include "asm/main.h"
|
||||||
#include "asm/rpn.h"
|
#include "asm/rpn.h"
|
||||||
#include "asm/section.h"
|
#include "asm/section.h"
|
||||||
|
|||||||
118
src/asm/macro.c
Normal file
118
src/asm/macro.c
Normal file
@@ -0,0 +1,118 @@
|
|||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "asm/asm.h"
|
||||||
|
#include "asm/macro.h"
|
||||||
|
#include "asm/warning.h"
|
||||||
|
|
||||||
|
static char *currentmacroargs[MAXMACROARGS + 1];
|
||||||
|
static char *newmacroargs[MAXMACROARGS + 1];
|
||||||
|
|
||||||
|
void sym_AddNewMacroArg(char const *s)
|
||||||
|
{
|
||||||
|
int32_t i = 0;
|
||||||
|
|
||||||
|
while (i < MAXMACROARGS && newmacroargs[i] != NULL)
|
||||||
|
i++;
|
||||||
|
|
||||||
|
if (i < MAXMACROARGS) {
|
||||||
|
if (s)
|
||||||
|
newmacroargs[i] = strdup(s);
|
||||||
|
else
|
||||||
|
newmacroargs[i] = NULL;
|
||||||
|
} else {
|
||||||
|
yyerror("A maximum of %d arguments allowed", MAXMACROARGS);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void sym_SaveCurrentMacroArgs(char *save[])
|
||||||
|
{
|
||||||
|
int32_t i;
|
||||||
|
|
||||||
|
for (i = 0; i <= MAXMACROARGS; i++) {
|
||||||
|
save[i] = currentmacroargs[i];
|
||||||
|
currentmacroargs[i] = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void sym_RestoreCurrentMacroArgs(char *save[])
|
||||||
|
{
|
||||||
|
int32_t i;
|
||||||
|
|
||||||
|
for (i = 0; i <= MAXMACROARGS; i++) {
|
||||||
|
free(currentmacroargs[i]);
|
||||||
|
currentmacroargs[i] = save[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void sym_UseNewMacroArgs(void)
|
||||||
|
{
|
||||||
|
int32_t i;
|
||||||
|
|
||||||
|
for (i = 0; i <= MAXMACROARGS; i++) {
|
||||||
|
free(currentmacroargs[i]);
|
||||||
|
currentmacroargs[i] = newmacroargs[i];
|
||||||
|
newmacroargs[i] = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
char *sym_FindMacroArg(int32_t i)
|
||||||
|
{
|
||||||
|
if (i == -1)
|
||||||
|
i = MAXMACROARGS + 1;
|
||||||
|
|
||||||
|
assert(i >= 1);
|
||||||
|
|
||||||
|
assert((size_t)(i - 1)
|
||||||
|
< sizeof(currentmacroargs) / sizeof(*currentmacroargs));
|
||||||
|
|
||||||
|
return currentmacroargs[i - 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
void sym_UseCurrentMacroArgs(void)
|
||||||
|
{
|
||||||
|
int32_t i;
|
||||||
|
|
||||||
|
for (i = 1; i <= MAXMACROARGS; i++)
|
||||||
|
sym_AddNewMacroArg(sym_FindMacroArg(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
void sym_SetMacroArgID(uint32_t nMacroCount)
|
||||||
|
{
|
||||||
|
char s[256];
|
||||||
|
|
||||||
|
snprintf(s, sizeof(s) - 1, "_%u", nMacroCount);
|
||||||
|
newmacroargs[MAXMACROARGS] = strdup(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
void sym_ShiftCurrentMacroArgs(void)
|
||||||
|
{
|
||||||
|
int32_t i;
|
||||||
|
|
||||||
|
free(currentmacroargs[0]);
|
||||||
|
for (i = 0; i < MAXMACROARGS - 1; i++)
|
||||||
|
currentmacroargs[i] = currentmacroargs[i + 1];
|
||||||
|
|
||||||
|
currentmacroargs[MAXMACROARGS - 1] = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t sym_NbMacroArgs(void)
|
||||||
|
{
|
||||||
|
uint32_t i = 0;
|
||||||
|
|
||||||
|
while (currentmacroargs[i] && i < MAXMACROARGS)
|
||||||
|
i++;
|
||||||
|
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
void macro_Init(void)
|
||||||
|
{
|
||||||
|
for (uint32_t i = 0; i < MAXMACROARGS; i++) {
|
||||||
|
currentmacroargs[i] = NULL;
|
||||||
|
newmacroargs[i] = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
112
src/asm/symbol.c
112
src/asm/symbol.c
@@ -18,10 +18,11 @@
|
|||||||
|
|
||||||
#include "asm/asm.h"
|
#include "asm/asm.h"
|
||||||
#include "asm/fstack.h"
|
#include "asm/fstack.h"
|
||||||
#include "asm/symbol.h"
|
#include "asm/macro.h"
|
||||||
#include "asm/main.h"
|
#include "asm/main.h"
|
||||||
#include "asm/mymath.h"
|
#include "asm/mymath.h"
|
||||||
#include "asm/section.h"
|
#include "asm/section.h"
|
||||||
|
#include "asm/symbol.h"
|
||||||
#include "asm/util.h"
|
#include "asm/util.h"
|
||||||
#include "asm/warning.h"
|
#include "asm/warning.h"
|
||||||
|
|
||||||
@@ -35,8 +36,6 @@ static struct sSymbol *pScope; /* Current section symbol scope */
|
|||||||
struct sSymbol *pPCSymbol;
|
struct sSymbol *pPCSymbol;
|
||||||
static struct sSymbol *p_NARGSymbol;
|
static struct sSymbol *p_NARGSymbol;
|
||||||
static struct sSymbol *p__LINE__Symbol;
|
static struct sSymbol *p__LINE__Symbol;
|
||||||
static char *currentmacroargs[MAXMACROARGS + 1];
|
|
||||||
static char *newmacroargs[MAXMACROARGS + 1];
|
|
||||||
static char SavedTIME[256];
|
static char SavedTIME[256];
|
||||||
static char SavedDATE[256];
|
static char SavedDATE[256];
|
||||||
static char SavedTIMESTAMP_ISO8601_LOCAL[256];
|
static char SavedTIMESTAMP_ISO8601_LOCAL[256];
|
||||||
@@ -52,12 +51,7 @@ static bool exportall;
|
|||||||
static int32_t Callback_NARG(struct sSymbol const *self)
|
static int32_t Callback_NARG(struct sSymbol const *self)
|
||||||
{
|
{
|
||||||
(void)self;
|
(void)self;
|
||||||
uint32_t i = 0;
|
return sym_NbMacroArgs();
|
||||||
|
|
||||||
while (currentmacroargs[i] && i < MAXMACROARGS)
|
|
||||||
i++;
|
|
||||||
|
|
||||||
return i;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int32_t Callback__LINE__(struct sSymbol const *self)
|
static int32_t Callback__LINE__(struct sSymbol const *self)
|
||||||
@@ -305,97 +299,6 @@ void sym_SetCurrentSymbolScope(struct sSymbol *pNewScope)
|
|||||||
pScope = pNewScope;
|
pScope = pNewScope;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* Macro argument stuff
|
|
||||||
*/
|
|
||||||
void sym_ShiftCurrentMacroArgs(void)
|
|
||||||
{
|
|
||||||
int32_t i;
|
|
||||||
|
|
||||||
free(currentmacroargs[0]);
|
|
||||||
for (i = 0; i < MAXMACROARGS - 1; i++)
|
|
||||||
currentmacroargs[i] = currentmacroargs[i + 1];
|
|
||||||
|
|
||||||
currentmacroargs[MAXMACROARGS - 1] = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
char *sym_FindMacroArg(int32_t i)
|
|
||||||
{
|
|
||||||
if (i == -1)
|
|
||||||
i = MAXMACROARGS + 1;
|
|
||||||
|
|
||||||
assert(i >= 1);
|
|
||||||
|
|
||||||
assert((size_t)(i - 1)
|
|
||||||
< sizeof(currentmacroargs) / sizeof(*currentmacroargs));
|
|
||||||
|
|
||||||
return currentmacroargs[i - 1];
|
|
||||||
}
|
|
||||||
|
|
||||||
void sym_UseNewMacroArgs(void)
|
|
||||||
{
|
|
||||||
int32_t i;
|
|
||||||
|
|
||||||
for (i = 0; i <= MAXMACROARGS; i++) {
|
|
||||||
free(currentmacroargs[i]);
|
|
||||||
currentmacroargs[i] = newmacroargs[i];
|
|
||||||
newmacroargs[i] = NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void sym_SaveCurrentMacroArgs(char *save[])
|
|
||||||
{
|
|
||||||
int32_t i;
|
|
||||||
|
|
||||||
for (i = 0; i <= MAXMACROARGS; i++) {
|
|
||||||
save[i] = currentmacroargs[i];
|
|
||||||
currentmacroargs[i] = NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void sym_RestoreCurrentMacroArgs(char *save[])
|
|
||||||
{
|
|
||||||
int32_t i;
|
|
||||||
|
|
||||||
for (i = 0; i <= MAXMACROARGS; i++) {
|
|
||||||
free(currentmacroargs[i]);
|
|
||||||
currentmacroargs[i] = save[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void sym_AddNewMacroArg(char const *s)
|
|
||||||
{
|
|
||||||
int32_t i = 0;
|
|
||||||
|
|
||||||
while (i < MAXMACROARGS && newmacroargs[i] != NULL)
|
|
||||||
i++;
|
|
||||||
|
|
||||||
if (i < MAXMACROARGS) {
|
|
||||||
if (s)
|
|
||||||
newmacroargs[i] = strdup(s);
|
|
||||||
else
|
|
||||||
newmacroargs[i] = NULL;
|
|
||||||
} else {
|
|
||||||
yyerror("A maximum of %d arguments allowed", MAXMACROARGS);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void sym_SetMacroArgID(uint32_t nMacroCount)
|
|
||||||
{
|
|
||||||
char s[256];
|
|
||||||
|
|
||||||
snprintf(s, sizeof(s) - 1, "_%u", nMacroCount);
|
|
||||||
newmacroargs[MAXMACROARGS] = strdup(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
void sym_UseCurrentMacroArgs(void)
|
|
||||||
{
|
|
||||||
int32_t i;
|
|
||||||
|
|
||||||
for (i = 1; i <= MAXMACROARGS; i++)
|
|
||||||
sym_AddNewMacroArg(sym_FindMacroArg(i));
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Find a macro by name
|
* Find a macro by name
|
||||||
*/
|
*/
|
||||||
@@ -667,14 +570,9 @@ static inline char const *removeLeadingZeros(char const *ptr)
|
|||||||
*/
|
*/
|
||||||
void sym_Init(void)
|
void sym_Init(void)
|
||||||
{
|
{
|
||||||
int32_t i;
|
macro_Init();
|
||||||
|
|
||||||
for (i = 0; i < MAXMACROARGS; i++) {
|
for (int32_t i = 0; i < HASHSIZE; i++)
|
||||||
currentmacroargs[i] = NULL;
|
|
||||||
newmacroargs[i] = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i = 0; i < HASHSIZE; i++)
|
|
||||||
tHashedSymbols[i] = NULL;
|
tHashedSymbols[i] = NULL;
|
||||||
|
|
||||||
pPCSymbol = sym_AddReloc("@");
|
pPCSymbol = sym_AddReloc("@");
|
||||||
|
|||||||
Reference in New Issue
Block a user