Searching \ for '[EE]: macro pre-processors' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: techref.massmind.org/techref/index.htm?key=macro+pre+processors
Search entire site for: 'macro pre-processors'.

Exact match. Not showing close matches.
PICList Thread
'[EE]: macro pre-processors'
2001\01\27@190054 by Bill Westfield

face picon face
(this time I'll correct the header tag AND change the body of the message
so that the duplicate-message filter won't eject it... Sigh.)

Are there any good macro pre-processors that are widely available and used
for dos/windows/etc environments?  I specifically need something that can
define macros within macros, or at least manipulate compile/assemble-time
variables within macros (most macro assemblers support the latter, but C's
standard preprocessor doesn't, as far as I know, since the only
"compile-time variables" are macros.)

In a unix world I'd probably use m4 (which IS available for dos/etc as part
of the gnu environment), but I don't like it that much and wouldn't mind
finding something better.

(Details of potential application follow....)

Essentially, I want to write macros to help write efficient state machines:

#macro DEF_STATE (name)
#ifndef %%statenum
#  set %%statenum 0                     (can't do this in Cpp!)
#else
#  set %%statenum = %%statenum+1
#endif
#endmacro

#macro STATE (s)
#set %%curstate = s                     (can't do this in Cpp!)
case s:
#endmacro

#macro NEWSTATE (s)
#if (s == %%curstate+1)                 (almost works in Cpp)
 break;
#else
 state = s;
 continue;
#endif
#endmacro

Should allow something like:

DEF_STATE IP_VERS
DEF_STATE IP_TOS
DEF_STATE IP_TL1
DEF_STATE IP_TL2
DEF_STATE IP_ID1
DEF_STATE DISCARD

while (1) {
   c = getc();
   switch (state) {
   STATE(IP_VERS)
       if (c == 0x45) {
           NEWSTATE(IP_TOS);
       }
       NEWSTATE(DISCARD);
   STATE(IP_TOS)
       NEWSTATE IP_TL1;
   STATE(IP_TL1)
       len = c*256;
       NEWSTATE(IP_TL2);
   STATE(IP_TL2)
       len +=c;
       NEWSTATE(IP_ID1)
   }
   state = state+1;
}

To pre-compile to:

while (1) {
   c = getc();
   switch (state) {
   case 0:
       if (c == 0x45) {
          break;
       }
       state = 5;
       continue;
   case 1:
       break;
   case 2:
       len = c*256;
       break;
   case 3:
       len +=c;
       break;
   }
   state = state+1;
}

You'll perhaps note that very similar macro definitions would work for
state machines in (any) assembly language (which is one reason I'd prefer
to use a separate macro processor rather than a good assembler.)  And pretty
easilly allow for states implemented by a single instruction.  Of course,
it's only really saving about one instruction per state as well, but...

Thanks
Bill W

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads


More... (looser matching)
- Last day of these posts
- In 2001 , 2002 only
- Today
- New search...