Author Topic: recursive macros supported?  (Read 9689 times)

Offline c051n3

  • Jr. Member
  • *
  • Posts: 22
recursive macros supported?
« on: July 23, 2010, 02:24:58 AM »
There is a feature I am trying to implement that requires macro recursion. I've narrowed it down to the bare minimal code and tested as follows:

Code: [Select]
CPU X64

; Establish a context
%push CTX

%macro mac1 0-*.nolist
    %warning entry
    %ifndef %$_var
        %warning var not previously defined
        %define %$_var 1
        mac2
    %endif
    %warning exit
%endmacro

%macro mac2 0-*.nolist
    %warning entry
    mac1
    %warning exit
%endmacro

; call macros
mac2

On assembly output I only receive one warning: (mac2:1) entry message although I am expecting to see 2 messages.
In the NASM docs Chapter 4 - The NASM Preprocessor 4.1.1 states:
"There is a mechanism which detects when a macro call has occurred as a result of a previous expansion of the same macro, to guard against circular references and infinite loops. If this happens, the preprocessor will only expand the first occurrence of the macro."
Is this why macros cannot be recursive?


Offline Bryant Keller

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 360
  • Country: us
    • About Bryant Keller
Re: recursive macros supported?
« Reply #1 on: July 23, 2010, 10:53:12 AM »
This is something which has already been brought up and an implementation is "in progress". There is a %rmacro (recursive macro) directive which was added, but then immediately removed because it was found to be buggy. Until the dents are hammered out of %rmacro, we just have to live without recursive support. This is the very reason that adding OOP support in the first place was so difficult because OOP/Inheritance is recursive in nature. :)

About Bryant Keller
bkeller@about.me

Offline c051n3

  • Jr. Member
  • *
  • Posts: 22
Re: recursive macros supported?
« Reply #2 on: July 23, 2010, 02:02:36 PM »
Ouch!  :o That is exactly what I was working on at the moment - recursive macros to create an oop class hierarchy. All of a sudden I feel your pain  :(

Offline Cyrill Gorcunov

  • NASM Developer
  • Full Member
  • *****
  • Posts: 179
  • Country: 00
Re: recursive macros supported?
« Reply #3 on: July 23, 2010, 03:24:29 PM »
Yeah, unfortunately we didn't implement rmacro still. But there is a hope :)

Offline Phopojijo

  • Jr. Member
  • *
  • Posts: 4
Re: recursive macros supported?
« Reply #4 on: July 23, 2010, 05:40:55 PM »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: recursive macros supported?
« Reply #5 on: July 23, 2010, 06:43:17 PM »
Good one, Phopojijo! :)

recursion error: see recursion error

While I didn't come close to getting c051n3's code to work, I noticed some odd behavior. An "orphan label" warning on "line 24" where the macro is invoked. If I do "mac2 1" in the "%if" clause of mac1, that becomes "instruction expected". Strange!

I guess it's just not going to work (with current Nasm). I wonder if there's some kludge that would create an "oop class heirarchy"(?) without using recursion? There often *is* an alternative to recursive functions... usually pretty ugly...

I'm way out of my league here - just wanted to let Phopojijo know I appreciated his joke.

Best,
Frank


Offline Rob Neff

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 429
  • Country: us
Re: recursive macros supported?
« Reply #6 on: July 23, 2010, 11:46:38 PM »
Not having recursion can indeed lead to "ugly"  :o code, as Frank put it, as the iterative form of a recursive function tends to obscure the natural logic of what otherwise would have been an easily identifiable method.

@Phopojijo - Thanks for the laugh  :)

@Cyrill - When you DO implement recursive macros please don't place restrictions for its use. If you are going to try and prevent the programmer from "looping endlessly" at least set the upper bounds high enough that, for example - after the 1000th invocation, a fatal occurs. I'm sure someone in the world will eventually want that removed too but I just don't want to hit any artificial glass ceilings.  I know where my power button is when I lock up my machine ;D