flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > [fasmg] How to issue unique labels in macro calminstruction?

Author
Thread Post new topic Reply to topic
fabbel



Joined: 30 Oct 2012
Posts: 83
fabbel 21 Sep 2023, 10:40
Hi Tomasz

Need ur help/insight with CALM again pls :
Am trying to define a macro calminstruction?.xxx (cud also be calminstruction calminstruction?.xxx), which need to generate labels as part of its implementation... i.e. produce labels inside some enclosing CALM inx to be complied with corresponding body ...
How can i make such labels somehow unique to allow non-conflicting multiple invocation within the same ?? Can't seem to get this to work...

assume the following - for the sake of example :

Code:
macro calminstruction?.dispsymbol? sym
        local @empty, @cont

                match , sym
                jyes @empty

                display '<not empty>'
                jump @cont

        @empty:
                display '<empty>'

        @cont:
end macro

calminstruction test arg1, arg2
                display '> arg1: '
                dispsymbol arg1
                display 13
                display 10

                display '> arg2: '
                dispsymbol arg2
                display 13
                display 10
end calminstruction
    



.. which fails to compile with below err :
Quote:

Error: definition of 'test:@empty' in conflict with already defined symbol



Can u suggest a workaround ???
Tx !
Post 21 Sep 2023, 10:40
View user's profile Send private message Reply with quote
fabbel



Joined: 30 Oct 2012
Posts: 83
fabbel 21 Sep 2023, 10:55
... Sub-question : how is the line 'local @empty, @cont' processed ?
... i.e :
* is it treated as locals definition at macro calminstruction?.dispsymbol? level ?
* or conversely, is it only 'blindly' including such line within calminstruction test body - thereby creating corresp. locals within calminstruction test ?
( obv not the intent here .... )
Post 21 Sep 2023, 10:55
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8344
Location: Kraków, Poland
Tomasz Grysztar 21 Sep 2023, 12:21
In current implementation LOCAL in context of CALM definition is always seen as CALM command, not as a macro directive of the same name. This makes it impossible to create macro local symbols in such context. There are some quirks in the engine that make it hard to change, so until I find a solution please consider it an undefined behavior.

To generate unique labels, you would therefore need to make a "calminstruction calminstruction?.dispsymbol?" instead, and generate labels with numeric suffix, etc.
Post 21 Sep 2023, 12:21
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8344
Location: Kraków, Poland
Tomasz Grysztar 21 Sep 2023, 13:25
I prepared a very simple framework that you could use to automatically generate unique symbols inside CALM definitions:
Code:
include 'xcalm.inc'

define loc? loc?

calminstruction calminstruction?.new? name*
        local   new, i
        init    i, 0
        compute i, i+1
        arrange new, name#i
        arrange name, loc.name
        publish name, new
end calminstruction

calminstruction calminstruction?.@? statement&
        transform statement, loc
        assemble statement
end calminstruction


macro calminstruction?.dispsymbol? sym
                match , sym
new @empty
@               jyes @empty

                display '<not empty>'
new @cont
@               jump @cont

@       @empty:
                display '<empty>'

@       @cont:
end macro

calminstruction test arg1, arg2
                display '> arg1: '
                dispsymbol arg1
                display 13
                display 10

                display '> arg2: '
                dispsymbol arg2
                display 13
                display 10
end calminstruction    
Post 21 Sep 2023, 13:25
View user's profile Send private message Visit poster's website Reply with quote
fabbel



Joined: 30 Oct 2012
Posts: 83
fabbel 21 Sep 2023, 13:36
tx for feedback ...

... however, regarding :
Quote:

To generate unique labels, you would therefore need to make a "calminstruction calminstruction?.dispsymbol?" instead, and generate labels with numeric suffix, etc.


.... I already considered this.. but I see name collisions may occcur nonetheless ... it seems labels are just plain names w/o any context or namespace separation - no ?
(typ. can't include dot '.') ?
....typ., assuming"calminstruction calminstruction?.dispsymbol?" happens to generate e.g. label '@empty_1',
can fasmg differentiate that from a label '@empty_1' that calminstruction test may also define for its own purpose ?
(... need to be 'agnostic' to internal impl of "calminstruction calminstruction?.dispsymbol?")
Post 21 Sep 2023, 13:36
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8344
Location: Kraków, Poland
Tomasz Grysztar 21 Sep 2023, 15:20
fabbel wrote:
can fasmg differentiate that from a label '@empty_1' that calminstruction test may also define for its own purpose ?
No, the CALM jump targets are handled specially (differently from all other symbols in fasmg) and are not affected by namespace context - the name itself is the only distinguisher. The best you can do is to synthesise a long UID-like name that has a low chance of conflict.

Another solution could be to make a complete CALM preprocessor with help of unconditional interceptor, to rewrite entire definition on your own terms - then you could, for example, use a different prefix for user-defined names, and different one for synthesized ones.
Post 21 Sep 2023, 15:20
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.