ref: ffdb1fbfe5e74ba9015ffaf0285f61a8e71d95b1
parent: 4b33b4b3875635f1c1a1aba1a2beb0e946d175c5
author: ISSOtm <[email protected]>
date: Tue Mar 10 22:39:36 EDT 2020
Split macro arg management into its own file It has no relation to symbols, and helps a tiny bit deflate `symbol.c`
--- a/Makefile
+++ b/Makefile
@@ -57,6 +57,7 @@
src/asm/fstack.o \
src/asm/globlex.o \
src/asm/lexer.o \
+ src/asm/macro.o \
src/asm/main.o \
src/asm/math.o \
src/asm/output.o \
--- /dev/null
+++ b/include/asm/macro.h
@@ -1,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
--- a/include/asm/symbol.h
+++ b/include/asm/symbol.h
@@ -90,23 +90,14 @@
struct sSymbol *sym_AddReloc(char const *tzSym);
void sym_Export(char const *tzSym);
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_AddSet(char const *tzSym, int32_t value);
void sym_Init(void);
uint32_t sym_GetConstantValue(char const *s);
struct sSymbol *sym_FindSymbol(char const *tzName);
-char *sym_FindMacroArg(int32_t i);
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);
void sym_Ref(char const *tzSym);
-void sym_ShiftCurrentMacroArgs(void);
struct sSymbol *sym_AddString(char const *tzSym, char const *tzValue);
uint32_t sym_GetDefinedValue(char const *s);
void sym_Purge(char const *tzName);
--- a/src/asm/asmy.y
+++ b/src/asm/asmy.y
@@ -20,6 +20,7 @@
#include "asm/charmap.h"
#include "asm/fstack.h"
#include "asm/lexer.h"
+#include "asm/macro.h"
#include "asm/main.h"
#include "asm/mymath.h"
#include "asm/rpn.h"
--- a/src/asm/fstack.c
+++ b/src/asm/fstack.c
@@ -19,9 +19,9 @@
#include "asm/fstack.h"
#include "asm/lexer.h"
+#include "asm/macro.h"
#include "asm/main.h"
#include "asm/output.h"
-#include "asm/symbol.h"
#include "asm/warning.h"
#include "extern/err.h"
--- a/src/asm/globlex.c
+++ b/src/asm/globlex.c
@@ -15,10 +15,10 @@
#include "asm/asm.h"
#include "asm/lexer.h"
+#include "asm/macro.h"
#include "asm/main.h"
#include "asm/rpn.h"
#include "asm/section.h"
-#include "asm/symbol.h"
#include "asm/warning.h"
#include "helpers.h"
--- a/src/asm/lexer.c
+++ b/src/asm/lexer.c
@@ -17,6 +17,7 @@
#include "asm/asm.h"
#include "asm/fstack.h"
#include "asm/lexer.h"
+#include "asm/macro.h"
#include "asm/main.h"
#include "asm/rpn.h"
#include "asm/section.h"
--- /dev/null
+++ b/src/asm/macro.c
@@ -1,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;
+ }
+}
--- a/src/asm/symbol.c
+++ b/src/asm/symbol.c
@@ -18,10 +18,11 @@
#include "asm/asm.h"
#include "asm/fstack.h"
-#include "asm/symbol.h"
+#include "asm/macro.h"
#include "asm/main.h"
#include "asm/mymath.h"
#include "asm/section.h"
+#include "asm/symbol.h"
#include "asm/util.h"
#include "asm/warning.h"
@@ -35,8 +36,6 @@
struct sSymbol *pPCSymbol;
static struct sSymbol *p_NARGSymbol;
static struct sSymbol *p__LINE__Symbol;
-static char *currentmacroargs[MAXMACROARGS + 1];
-static char *newmacroargs[MAXMACROARGS + 1];
static char SavedTIME[256];
static char SavedDATE[256];
static char SavedTIMESTAMP_ISO8601_LOCAL[256];
@@ -52,12 +51,7 @@
static int32_t Callback_NARG(struct sSymbol const *self)
{
(void)self;
- uint32_t i = 0;
-
- while (currentmacroargs[i] && i < MAXMACROARGS)
- i++;
-
- return i;
+ return sym_NbMacroArgs();
}
static int32_t Callback__LINE__(struct sSymbol const *self)
@@ -306,97 +300,6 @@
}
/*
- * 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
*/
struct sSymbol *sym_FindMacro(char const *s)
@@ -667,14 +570,9 @@
*/
void sym_Init(void)
{
- int32_t i;
+ macro_Init();
- for (i = 0; i < MAXMACROARGS; i++) {
- currentmacroargs[i] = NULL;
- newmacroargs[i] = NULL;
- }
-
- for (i = 0; i < HASHSIZE; i++)
+ for (int32_t i = 0; i < HASHSIZE; i++)
tHashedSymbols[i] = NULL;
pPCSymbol = sym_AddReloc("@");