OSDev.org

The Place to Start for Operating System Developers
It is currently Sun Apr 28, 2024 7:32 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: making macros
PostPosted: Sat Jan 07, 2006 5:52 pm 
hello all, ok i'm trying to turn this:
Code:
void cli()
{
    asm("cli");
}

in to a macro:
Code:
#define cli() { asm("cli"); }

or
Code:
#define cli() { asm("cli") }

but this makes horrible errors i cant explain, like random undifinded veriables in c files that dont even use these functions!!!, help, thx


Top
  
 
 Post subject: Re:making macros
PostPosted: Sat Jan 07, 2006 6:26 pm 
How about the C compiler you are using and which version? What do the errors say?


Top
  
 
 Post subject: Re:making macros
PostPosted: Sat Jan 07, 2006 6:34 pm 
djgpp, as for errors, when i include the semi-colin every file #includeing syslib.h(where i put the macro) goes crazy ??? ??? ???, but when i dont include the semi-colin i get a few "pause error befor '}' token" right after where i use this macro


Top
  
 
 Post subject: Re:making macros
PostPosted: Sat Jan 07, 2006 6:36 pm 
Try it this way:

Code:
#define cli() __asm volatile("cli\n")


Then use it like this:

Code:
cli();


Top
  
 
 Post subject: Re:making macros
PostPosted: Sat Jan 07, 2006 7:05 pm 
thx, that worked, but what about more advanced function to macro convertions???:
Code:
inline unsigned char inport(unsigned short port)
{
    unsigned char result;
    asm volatile("inb %1, %0" : "=a" (result) : "dN" (port));
    return result;
}

inline void outport(unsigned short port, unsigned char data)
{
    asm volatile("outb %1, %0" : : "dN" (port), "a" (data));
}

inline unsigned short inportw(unsigned short port)
{
    unsigned short result;
    asm volatile("inw %1, %0" : "=a" (result) : "dN" (port));
    return result;
}

inline void outportw(unsigned short port, unsigned short data)
{
    asm volatile("outw %1, %0" : : "dN" (port), "a" (data));
}


Top
  
 
 Post subject: Re:making macros
PostPosted: Sun Jan 08, 2006 4:46 am 
Well, the single line ones can be done using exactly the same method AFAIK


Top
  
 
 Post subject: Re:making macros
PostPosted: Sun Jan 08, 2006 6:41 am 
Offline
Member
Member
User avatar

Joined: Thu Nov 16, 2006 12:01 pm
Posts: 7614
Location: Germany
The problem here is that your first cli() is a function, whereas your second cli() (the macro) is a statement.

Remember that macros are expanded by the preprocessor, i.e. before the compiler even gets to see the source.

If you define:

Code:
#define cli() { asm("cli") }


and then write

Code:
int main()
{
    foo();
    cli();
    bar();
}


that will get expanded to

Code:
int main()
{
    foo();
    { asm("cli"); };
    bar();
}


It should be easy to see where the extra brackets and the additional semicolon could lead to troubles, e.g. in an [tt]if[/tt] statement.

A traditional way of encapsuling multi-line blocks in a macro (taking the first of your later examples) is the do-while-block:

Code:
#define cli() do { asm("cli"); } while (0)


That way, the trailing semicolon gets "eaten" by the while-statement.

A macro cannot "return" a value, as it is not a function. With a bit of skill, you can make a macro evaluate to a certain value, which for all practical purposes is the same. Beware of side-effects, syntactical loopholes and multiple evaluation of parameters, though.

Bottom line, stay away from macros unless you really must use them.

_________________
Every good solution is obvious once you've found it.


Top
 Profile  
 
 Post subject: Re:making macros
PostPosted: Sun Jan 08, 2006 9:48 am 
ok, thx, so i put those functions in the syslib.h file but now every file that include that file and uses those functions gets multiple definition errors, is there a way to inline functions with out errors???, thx


Top
  
 
 Post subject: Re:making macros
PostPosted: Sun Jan 08, 2006 10:24 am 
Something to check... have you actually defined things more than once? Check the header files aren't included from more than one place as well.


Top
  
 
 Post subject: Re:making macros
PostPosted: Sun Jan 08, 2006 11:02 am 
Offline
Member
Member
User avatar

Joined: Thu Nov 16, 2006 12:01 pm
Posts: 7614
Location: Germany
GLneo wrote:
ok, thx, so i put those functions in the syslib.h file but now every file that include that file and uses those functions gets multiple definition errors, is there a way to inline functions with out errors???, thx


Read GCC manual on "inline" functions.

_________________
Every good solution is obvious once you've found it.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 24 guests


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 post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group