From 7da1e1ef563e671b71223e1006643b7502b1da12 Mon Sep 17 00:00:00 2001 From: Paul Koning Date: Sun, 26 Jun 2022 14:27:19 -0400 Subject: [PATCH] Implement .ENABLE MCL --- assemble.c | 88 +++++++++------------------------------------- assemble_globals.c | 3 ++ assemble_globals.h | 2 ++ macros.c | 78 ++++++++++++++++++++++++++++++++++++++++ macros.h | 5 ++- 5 files changed, 104 insertions(+), 72 deletions(-) diff --git a/assemble.c b/assemble.c index 2845146..c9e166b 100644 --- a/assemble.c +++ b/assemble.c @@ -264,6 +264,7 @@ O 75 .endc /* Try to resolve macro */ +do_mcalled_macro: op = lookup_sym(label, ¯o_st); if (op /*&& op->stmtno < stmtno*/) { STREAM *macstr; @@ -591,15 +592,6 @@ O 75 .endc case P_MCALL: { - STREAM *macstr; - BUFFER *macbuf; - char *maccp; - int saveline; - MACRO *mac; - int i; - char macfile[FILENAME_MAX]; - char hitfile[FILENAME_MAX]; - for (;;) { cp = skipdelim(cp); @@ -633,68 +625,8 @@ O 75 .endc continue; } - /* Find the macro in the list of included - macro libraries */ - macbuf = NULL; - for (i = 0; i < nr_mlbs; i++) - if ((macbuf = mlb_entry(mlbs[i], label)) != NULL) - break; - if (macbuf != NULL) { - macstr = new_buffer_stream(macbuf, label); - buffer_free(macbuf); - } else { - char *bufend = &macfile[sizeof(macfile)], - *end; - end = stpncpy(macfile, label, sizeof(macfile) - 5); - if (end >= bufend - 5) { - report(stack->top, ".MCALL: name too long: '%s'\n", label); - return 0; - } - stpncpy(end, ".MAC", bufend - end); - my_searchenv(macfile, "MCALL", hitfile, sizeof(hitfile)); - if (hitfile[0]) - macstr = new_file_stream(hitfile); - else - macstr = NULL; - } - - if (macstr != NULL) { - for (;;) { - char *mlabel; - - maccp = macstr->vtbl->getline(macstr); - if (maccp == NULL) - break; - mlabel = get_symbol(maccp, &maccp, NULL); - if (mlabel == NULL) - continue; - op = lookup_sym(mlabel, &system_st); - free(mlabel); - if (op == NULL) - continue; - if (op->value == P_MACRO) - break; - } - - if (maccp != NULL) { - STACK macstack = { - macstr - }; - int savelist = list_level; - - saveline = stmtno; - list_level = -1; - mac = defmacro(maccp, &macstack, CALLED_NOLIST); - if (mac == NULL) { - report(stack->top, "Failed to define macro called %s\n", label); - } - - stmtno = saveline; - list_level = savelist; - } - - macstr->vtbl->delete(macstr); - } else + /* Do the actual macro library search */ + if (!do_mcall (label, stack)) report(stack->top, "MACRO %s not found\n", label); free(label); @@ -757,6 +689,8 @@ O 75 .endc enabl_lc = 1; } else if (strcmp(label, "LCM") == 0) { enabl_lcm = 1; + } else if (strcmp(label, "MCL") == 0) { + enabl_mcl = 1; } free(label); cp = skipdelim(cp); @@ -778,6 +712,8 @@ O 75 .endc enabl_lc = 0; } else if (strcmp(label, "LCM") == 0) { enabl_lcm = 0; + } else if (strcmp(label, "MCL") == 0) { + enabl_mcl = 0; } free(label); cp = skipdelim(cp); @@ -1853,6 +1789,16 @@ O 75 .endc } /* end if (op is a symbol) */ } + /* If .ENABL MCL is in effect, try to define the symbol as a + * library macro if it is not a defined symbol. */ + if (enabl_mcl) { + if (lookup_sym(label, &symbol_st) == NULL) { + if (do_mcall (label, stack)) { + goto do_mcalled_macro; + } + } + } + /* Only thing left is an implied .WORD directive */ /*JH: fall through in case of illegal opcode, illegal label! */ free(label); diff --git a/assemble_globals.c b/assemble_globals.c index e0ac75e..2cfbdcb 100644 --- a/assemble_globals.c +++ b/assemble_globals.c @@ -40,6 +40,9 @@ int enabl_lc = 1; /* If lowercase disabled, convert assembler int enabl_lcm = 0; /* If lowercase disabled, .IF IDN/DIF are case-sensitive. */ +int enabl_mcl = 0; /* When set, unknown symbols are looked up + as if .MCALL had been done. */ + int suppressed = 0; /* Assembly suppressed by failed conditional */ diff --git a/assemble_globals.h b/assemble_globals.h index 4a21eb2..3b957d7 100644 --- a/assemble_globals.h +++ b/assemble_globals.h @@ -55,6 +55,8 @@ extern int suppressed; /* Assembly suppressed by failed conditional */ extern MLB *mlbs[MAX_MLBS]; /* macro libraries specified on the command line */ extern int nr_mlbs; /* Number of macro libraries */ +extern int enabl_mcl; /* If MCALL of unknown symbols is enabled. */ + extern COND conds[MAX_CONDS]; /* Stack of recent conditions */ extern int last_cond; /* 0 means no stacked cond. */ diff --git a/macros.c b/macros.c index d9cbe91..d753f23 100644 --- a/macros.c +++ b/macros.c @@ -618,3 +618,81 @@ void free_macro( free_args(mac->args); free_sym(&mac->sym); } + +int do_mcall (char *label, STACK *stack) +{ + SYMBOL *op; /* The operation SYMBOL */ + STREAM *macstr; + BUFFER *macbuf; + char *maccp; + int saveline; + MACRO *mac; + int i; + char macfile[FILENAME_MAX]; + char hitfile[FILENAME_MAX]; + + /* Find the macro in the list of included + macro libraries */ + macbuf = NULL; + for (i = 0; i < nr_mlbs; i++) + if ((macbuf = mlb_entry(mlbs[i], label)) != NULL) + break; + if (macbuf != NULL) { + macstr = new_buffer_stream(macbuf, label); + buffer_free(macbuf); + } else { + char *bufend = &macfile[sizeof(macfile)], + *end; + end = stpncpy(macfile, label, sizeof(macfile) - 5); + if (end >= bufend - 5) { + report(stack->top, ".MCALL: name too long: '%s'\n", label); + return 0; + } + stpncpy(end, ".MAC", bufend - end); + my_searchenv(macfile, "MCALL", hitfile, sizeof(hitfile)); + if (hitfile[0]) + macstr = new_file_stream(hitfile); + else + macstr = NULL; + } + + if (macstr != NULL) { + for (;;) { + char *mlabel; + + maccp = macstr->vtbl->getline(macstr); + if (maccp == NULL) + break; + mlabel = get_symbol(maccp, &maccp, NULL); + if (mlabel == NULL) + continue; + op = lookup_sym(mlabel, &system_st); + free(mlabel); + if (op == NULL) + continue; + if (op->value == P_MACRO) + break; + } + + if (maccp != NULL) { + STACK macstack = { + macstr + }; + int savelist = list_level; + + saveline = stmtno; + list_level = -1; + mac = defmacro(maccp, &macstack, CALLED_NOLIST); + if (mac == NULL) { + report(stack->top, "Failed to define macro called %s\n", label); + } + + stmtno = saveline; + list_level = savelist; + } + + macstr->vtbl->delete(macstr); + return 1; + } + return 0; +} diff --git a/macros.h b/macros.h index b4af2a6..0fe966a 100644 --- a/macros.h +++ b/macros.h @@ -71,6 +71,9 @@ BUFFER *subst_args( BUFFER *text, ARG *args); - +int do_mcall ( + char *label, + STACK *stack); + #endif