Menu

#53 add support for %# to prevent smac expansion

open
nobody
None
1
2003-10-24
2003-10-24
No

I have added support for %# to my local forked
version of NASM. This character sequence gets
removed, but tokenized to TOK_SMAC_SKIP, which
in turn prevents expand_smacro() from expanding
the very next token, if it happens to be a pre-
processor variable or a single-line macro. As
usual, %{#} can be used to achieve a literal %#
that won't result in the new %# functionality.

This allows the user to pass the name of a pre-
processor variable or single-line macro -- not
the value it expands into -- as an argument to
a multi-line macro. (Remember, the preprocessor
expands ppvars/smacs before it expands mmacs.)

For example:

%MACRO m1 2
%DEFINE2 %1 %2
%ENDMACRO

%MACRO m2 1
%UNDEF %1
%ENDMACRO

m1 s,1 ; works since "s" doesn't exist yet
m1 s,2 ; fails since "s" expands before "m1"
m1 %#s,2 ; works due to %# before "s"

m2 s ; fails since "s" expands before "m2"
m2 %#s ; works due to %# before "s"

The required preproc.c changes are pretty minor:

1. Add TOK_SMAC_SKIP to enum.

2. Add %# --> TOK_SMAC_SKIP to tokenise(), e.g.
just before TOK_STRING is handled:

} else if ( p[0] == '%' && p[1] == '#' ) {
p++;
p++;
type = TOK_SMAC_SKIP;
} ...

3. Add a new boolean flag to expand_smacro():

int skip = FALSE;

4. Honor that new flag in expand_smacro():

- if ( p ) {
+ if ( ( skip == FALSE ) && ( p ) ) {

This assumes a 0.98-like preprocessor that
uses p -- the code in 0.98.38 is slightly
different, but it should be easy to adjust
it to honor the new flag.

5. After expand_smacro() has expanded, but be-
fore it handles TOK_SMAC_END, add code to
handle the new TOK_SMAC_SKIP tokens:

skip = FALSE;
if ( tline->type == TOK_SMAC_SKIP ) {
t = tline;
tline = tline->next;
nasm_free ( t->text );
nasm_free ( t );
if ( tline ) {
skip = TRUE;
} else {
break;
}
}

Let me know if you are interested in testcases
or an actual diff. (Though the latter would not
be suitable for direct application to 0.98.38,
since my version was forked from 0.98 instead.)

Discussion

  • nasm64developer

    nasm64developer - 2003-10-24
    • priority: 5 --> 1
     
  • nasm64developer

    nasm64developer - 2003-11-01

    Logged In: YES
    user_id=804543

    To resolve SF #816111 at the same time, I have
    come up with the following minor enhancement:

    6. Only perform the "%$id" to "..@<n>.id"
    conversion in expand_smacro() when skip
    is FALSE.

    7. Remove the "%$id" to "..@<n>.id" code
    from detoken().

    8. Remove the %OPTION CONVERT=[EARLY|LATE]
    as well as the __OPTION_CONVERT__ support
    that was mentioned in SF #816111.

     
  • nasm64developer

    nasm64developer - 2003-11-03

    Logged In: YES
    user_id=804543

    A slight modification is required for item #5, to
    handle multiple successive TOK_SMAC_SKIP tokens.

    After "skip = TRUE", the following code is needed:

    if ( tline->type == TOK_SMAC_SKIP ) {
    continue;
    }

     
  • nasm64developer

    nasm64developer - 2003-11-10

    Logged In: YES
    user_id=804543

    Another slight modification is required for
    item #5, if the %> functionality described
    in SF request #839215 gets implemented too.

    if (
    ( tline->type == TOK_SMAC_SKIP ) ||
    ( tline->type == TOK_SEP )
    ) {
    continue;
    }

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.