dasm-assembler / dasm

Macro assembler with support for several 8-bit microprocessors

Home Page:https://dasm-assembler.github.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Nested macro definitions

Rhialto opened this issue · comments

The docs say:

GENERAL:
    Most everything is recursive.  You cannot have a macro DEFINITION
    within a macro definition, but can nest macro calls, repeat loops,
    and include files.

Actually to have such a facility is useful sometimes. Digital's Macro-11 assembler allows it. One way this is commonly used (well, common for a specific assembler for an obsolete CPU architecture) is use-once macros. A macro, if it is used, re-defines itself to be empty. Something like this: (see also https://gitlab.com/Rhialto/macro11/-/blob/master/tests/test-endm.lst.ok for a listing output)

;;;;;
;
; Test nested macros and name on .ENDM
;

    .macro      M1
    .word       1
    .endm       M1      ; ok

    M1

    .macro      M2
    .word       2
    .macro      M3
    .word       3
    .endm       M3      ; ok
    .endm       M2      ; ok

    M2
    M3

    .macro      M4
    .word       4
    .macro      M4
    .endm       M4      ; ok
    .endm       M4      ; ok

    M4
    M4                  ; should be empty now

    .macro      M5
    .word       5
    .macro      M5
    .endm       notM5   ; wrong; detected when M5 is expanded
    .endm       M5      ; ok

    M5
    M5

    .macro      M6
    .endm       notM6   ; wrong

    .endm               ; end without macro
    .endr               ; end without repetition
    .endc               ; end without condition

This particular use-case can be easily replicated by defining a global inside the macro.
Then the macro simply checks if the global is defined (via IFCONST) just before that line.

  MAC onceonly
    IFNCONST onceonly_used_already
onceonly_used_already = 1
 ; body of macro
    ENDIF
  ENDM
; code....

 onceonly. ; body will be inserted
 onceonly ; body will not be inserted, because onceonly_used_already has been defined

My point here is that the solution is very easy with the existing code.
I don't think this is a good reason to try a rewrite.