From 7bbcbba5f5f62e9d8c79f4fd2deabe7de7119e3b Mon Sep 17 00:00:00 2001 From: Olaf Seibert Date: Sun, 18 Apr 2021 18:31:35 +0200 Subject: [PATCH] Include undefined symbols in the symbol table for listing purposes. MACRO11 V05.05 does this: ``` .MAIN. MACRO V05.05 Sunday 18-APR-2021 16:29 Page 1 1 2 000000 012700 000000G mov #lab1,r0 3 4 .dsabl gbl 5 U 6 000004 012700 000000 mov #lab2,r0 7 8 000001 .end .MAIN. MACRO V05.05 Sunday 18-APR-2021 16:29 Page 1-1 Symbol table LAB1 = ****** GX LAB2 = ****** . ABS. 000000 000 (RW,I,GBL,ABS,OVR) 000010 001 (RW,I,LCL,REL,CON) Errors detected: 1 *** Assembler statistics Work file reads: 0 Work file writes: 0 Size of work file: 34 Words ( 1 Pages) Size of core pool: 9260 Words ( 35 Pages) Operating system: RSX-11M/M-PLUS Elapsed time: 00:00:00.01 GBL,GBL/-SP=GBL ``` --- assemble.c | 4 ++-- assemble_aux.c | 19 ++++++++++++++----- extree.c | 6 +++++- extree.h | 2 +- macro11.c | 1 + parse.c | 8 ++++++-- symbols.c | 5 ++++- tests/RunTests | 1 + tests/test-gbl.lst.ok | 26 ++++++++++++++++++++++++++ tests/test-gbl.mac | 13 +++++++++++++ 10 files changed, 73 insertions(+), 12 deletions(-) create mode 100644 tests/test-gbl.lst.ok create mode 100644 tests/test-gbl.mac diff --git a/assemble.c b/assemble.c index 111447c..a5cda58 100644 --- a/assemble.c +++ b/assemble.c @@ -800,12 +800,12 @@ static int assemble( if (!label) { report(stack->top, "Missing .(I)IF condition\n"); } else if (strcmp(label, "DF") == 0) { - value = parse_expr(cp, EVALUATE_UNDEF); + value = parse_expr(cp, EVALUATE_DEFINEDNESS); cp = value->cp; ok = eval_defined(value); free_tree(value); } else if (strcmp(label, "NDF") == 0) { - value = parse_expr(cp, EVALUATE_UNDEF); + value = parse_expr(cp, EVALUATE_DEFINEDNESS); cp = value->cp; ok = eval_undefined(value); free_tree(value); diff --git a/assemble_aux.c b/assemble_aux.c index 4c1f8bd..aa7b0ae 100644 --- a/assemble_aux.c +++ b/assemble_aux.c @@ -194,7 +194,8 @@ unsigned get_register( /* implicit_gbl is a self-recursive routine that adds undefined symbols - to the "implicit globals" symbol table. + to the "implicit globals" symbol table, or alternatively adds the + symbol as an UNDEFINED symbol. */ void implicit_gbl( @@ -203,15 +204,18 @@ void implicit_gbl( if (pass) return; /* Only do this in first pass */ - if (!enabl_gbl) - return; /* Option not enabled, don't do it. */ - switch (value->type) { case EX_UNDEFINED_SYM: { if (!(value->data.symbol->flags & SYMBOLFLAG_LOCAL)) { /* Unless it's a local symbol, */ - add_sym(value->data.symbol->label, 0, SYMBOLFLAG_GLOBAL, &absolute_section, &implicit_st); + if (enabl_gbl) { + /* Either make the undefined symbol into an implicit global */ + add_sym(value->data.symbol->label, 0, SYMBOLFLAG_GLOBAL, &absolute_section, &implicit_st); + } else { + /* or add it to the symbol table, purely for listing purposes. */ + add_sym(value->data.symbol->label, 0, SYMBOLFLAG_UNDEFINED, &absolute_section, &symbol_st); + } } } break; @@ -323,6 +327,11 @@ int complex_tree( { SYMBOL *sym = tree->data.symbol; + /* This check may not be needed; so far it made no difference. */ + if (sym->flags & SYMBOLFLAG_UNDEFINED) { + return 0; + } + if ((sym->flags & (SYMBOLFLAG_GLOBAL | SYMBOLFLAG_DEFINITION)) == SYMBOLFLAG_GLOBAL) { text_complex_global(tx, sym->label); } else { diff --git a/extree.c b/extree.c index 39e605c..dee77ed 100644 --- a/extree.c +++ b/extree.c @@ -281,7 +281,7 @@ EX_TREE *evaluate( /* Change some symbols to "undefined" */ - if (flags & EVALUATE_UNDEF) { + if (flags & EVALUATE_DEFINEDNESS) { int change = 0; /* I'd prefer this behavior, but MACRO.SAV is a bit too primitive. */ @@ -296,6 +296,10 @@ EX_TREE *evaluate( if ((sym->flags & (SYMBOLFLAG_GLOBAL | SYMBOLFLAG_DEFINITION)) == SYMBOLFLAG_GLOBAL) change = 1; + /* A symbol marked as undefined is undefined */ + if (sym->flags & SYMBOLFLAG_UNDEFINED) + change = 1; + if (change) { res = new_temp_sym(tp->data.symbol->label, tp->data.symbol->section, tp->data.symbol->value); diff --git a/extree.h b/extree.h index 5d91c60..42ad8a4 100644 --- a/extree.h +++ b/extree.h @@ -69,6 +69,6 @@ EX_TREE *evaluate( EX_TREE *tp, int flags); -#define EVALUATE_UNDEF 1 +#define EVALUATE_DEFINEDNESS 1 #endif diff --git a/macro11.c b/macro11.c index 33946bd..3eba161 100644 --- a/macro11.c +++ b/macro11.c @@ -205,6 +205,7 @@ void prepare_pass(int this_pass, STACK *stack, int nr_files, char **fnames) enabl_lcm = 0; enabl_lsb = 0; enabl_ama = opt_enabl_ama; + enabl_gbl = 1; } int main( diff --git a/parse.c b/parse.c index 5fcd604..b3fc671 100644 --- a/parse.c +++ b/parse.c @@ -1133,7 +1133,7 @@ EX_TREE *parse_unary( sym = lookup_sym(label, &system_st); } - if (sym != NULL) { + if (sym != NULL && !(sym->flags & SYMBOLFLAG_UNDEFINED)) { tp = new_ex_tree(); tp->cp = cp; tp->type = EX_SYM; @@ -1144,7 +1144,11 @@ EX_TREE *parse_unary( } /* The symbol was not found. Create an "undefined symbol" - reference. */ + reference. These symbols are freed in free_tree(), + in contrast to the symbol used in EX_SYM. + implicit_gbl() will either make it an implicit global, + or an undefined non-global symbol. + */ sym = memcheck(malloc(sizeof(SYMBOL))); sym->label = label; sym->flags = SYMBOLFLAG_UNDEFINED | local; diff --git a/symbols.c b/symbols.c index ec514c9..18d92be 100644 --- a/symbols.c +++ b/symbols.c @@ -208,7 +208,10 @@ SYMBOL *add_sym( if ((sym->flags & SYMBOLFLAG_UNDEFINED) && !(flags & SYMBOLFLAG_UNDEFINED)) { sym->flags &= ~(SYMBOLFLAG_PERMANENT | SYMBOLFLAG_UNDEFINED); } - + else if (!(sym->flags & SYMBOLFLAG_UNDEFINED) && (flags & SYMBOLFLAG_UNDEFINED)) { + report(NULL, "INTERNAL ERROR: Turning defined symbol '%s' into undefined\n", label); + return sym; + } /* Check for compatible definition */ else if (sym->section == section && sym->value == value) { sym->flags |= flags; /* Merge flags quietly */ diff --git a/tests/RunTests b/tests/RunTests index 0ef50e4..6a0be16 100755 --- a/tests/RunTests +++ b/tests/RunTests @@ -16,6 +16,7 @@ TESTS="test-asciz \ test-enabl-lcm \ test-endm \ test-float \ + test-gbl \ test-if \ test-impword \ test-include \ diff --git a/tests/test-gbl.lst.ok b/tests/test-gbl.lst.ok new file mode 100644 index 0000000..a9dbaf8 --- /dev/null +++ b/tests/test-gbl.lst.ok @@ -0,0 +1,26 @@ + 1 ;;;; + 2 ; + 3 ; Test that .dsabl gbl at the end of the source does not carry + 4 ; over to the beginning of the next pass. + 5 ; This is actually guaranteed automatically, since it is used in pass 1 only. + 6 ; The symbol should however be entered into the symbol table as UNKNOWN. + 7 ; + 8 + 9 000000 012700 000000G mov #lab1,r0 ; ok: implicitly global (imported) + 10 .dsabl gbl +test-gbl.mac:11: ***ERROR Invalid expression (complex relocation) + 11 000004 012700 000000 mov #lab2,r0 ; error: undefined + 12 + 13 .end + 13 + + +Symbol table + +. ******R 001 LAB1 = ****** GX LAB2 = ****** + + +Program sections: + +. ABS. 000000 000 (RW,I,GBL,ABS,OVR,NOSAV) + 000010 001 (RW,I,LCL,REL,CON,NOSAV) diff --git a/tests/test-gbl.mac b/tests/test-gbl.mac new file mode 100644 index 0000000..6713aaa --- /dev/null +++ b/tests/test-gbl.mac @@ -0,0 +1,13 @@ +;;;; +; +; Test that .dsabl gbl at the end of the source does not carry +; over to the beginning of the next pass. +; This is actually guaranteed automatically, since it is used in pass 1 only. +; The symbol should however be entered into the symbol table as UNKNOWN. +; + + mov #lab1,r0 ; ok: implicitly global (imported) + .dsabl gbl + mov #lab2,r0 ; error: undefined + + .end