# inside a macro body

M

Miki Tebeka

Hello All,

We're using in-house C pre processor for Assembly development.
We'd like to move to a standard "C" pre processor (such as gcc's "cpp").

However we have the following problem:
Out assembly code have the following syntax:
move #2, d1;

How can I write a macro that will produce the above line?
If I try:
#define mv(reg) move #2, reg;
I get (for cpp -E):
f:1:23: '#' is not followed by a macro parameter

I've tried with ## and \# but it doesn't work.
Is there a way around this?

Thanks.
Miki
 
S

S.Tobias

Miki Tebeka said:
Hello All,
Out assembly code have the following syntax:
move #2, d1;
How can I write a macro that will produce the above line?
If I try:
#define mv(reg) move #2, reg;

In definitions of function-like macros # and ## are operators.
I get (for cpp -E):
f:1:23: '#' is not followed by a macro parameter
I've tried with ## and \# but it doesn't work.
Is there a way around this?

Try this:

#define HASH2 #2
#define mv(reg) move HASH2, reg;

[Note:
If you're thinking of producing "#2" by pasting "#" and "2", then
this will never work, because "#2" is not a valid token:
#define HASH #
#define xcat(a, b) a ## b
#define cat(a, b) xcat(a, b)
#define mv_dest(n, reg) move cat(HASH, n), reg;
mv_dest(2, d1)
testcpp:11:7: pasting "#" and "2" does not give a valid preprocessing token

You can always do it like this (you have to pass "#2" as argument):
#define mv_dest(dest, reg) move dest, reg;
mv_dest(#2, d1)
]

C preprocessor is *not* a text processor, and might prove unsuitable
for your task. My advice would be rather stay with your in-house
solution, or look for a regular text processor (many would point to m4).
 
I

iolotusbobo

hello,
We're using in-house C pre processor for Assembly development.
We'd like to move to a standard "C" pre processor (such as gcc's "cpp").
First of all welcome to gcc :)
However we have the following problem:
Out assembly code have the following syntax:
move #2, d1;

How can I write a macro that will produce the above line?
If I try:
#define mv(reg) move #2, reg;
I get (for cpp -E):
f:1:23: '#' is not followed by a macro parameter

I've tried with ## and \# but it doesn't work.
Is there a way around this?
i think ## should work. The following line compiles well with my gcc

#define mv(reg) move ##2, reg

the details of my gcc are as follows -
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--disable-checking --with-system-zlib --enable-__cxa_atexit
--host=i386-redhat-linux
Thread model: posix
gcc version 3.3.2 20031022 (Red Hat Linux 3.3.2-1)

I hope that you find the information useful
 
A

Arthur J. O'Dwyer

We're using in-house C pre processor for Assembly development.
We'd like to move to a standard "C" pre processor (such as gcc's "cpp").

However we have the following problem:
Out assembly code have the following syntax:
move #2, d1;

From the GNU 'cpp' manpage:
The C preprocessor is intended to be used only with C, C++, and Objec-
tive-C source code. In the past, it has been abused as a general text
processor. It will choke on input which does not obey C's lexical
rules. For example, apostrophes will be interpreted as the beginning
of character constants, and cause errors. Also, you cannot rely on it
preserving characteristics of the input which are not significant to C-
family languages. If a Makefile is preprocessed, all the hard tabs
will be removed, and the Makefile will not work.

In short: You can't use 'cpp' to process assembly code; it won't work.
'cpp' only processes C and C++ code, and occasionally things that look
something like C or C++.

What you want is a general macro processor. Look up 'm4', which is
also available from GNU. Also try asking in comp.programming or
comp.lang.misc, where discussion of that kind of thing is on-topic.

HTH,
-Arthur
 
D

Derrick Coetzee

iolotusbobo said:
i think ## should work. The following line compiles well with my gcc

#define mv(reg) move ##2, reg

This does not do what you think. Here's the preprocessor output on mv(r5):

move2, r5

You simply pasted together the tokens "move" and "2" with the
token-pasting operator ##.
 
P

Paul Mensonides

S.Tobias said:
Try this:

#define HASH2 #2
#define mv(reg) move HASH2, reg;

or:

#define HASH() HASH_I
#define HASH_I #

#define mv(reg) move HASH()2, reg;

Regards,
Paul Mensonides
 
S

S.Tobias

Paul Mensonides said:
#define HASH() HASH_I
#define HASH_I #
#define mv(reg) move HASH()2, reg;

But with gcc cpp this yields:
move # 2, reg ;
^^ we probably don't want these spaces
I guess that a preprocessor needn't put them here, because
'#' and '2' are distinguishable as tokens, but may;
anyway, text output from cpp is not what the Std defines.

Regards!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top