what does __p in unsigned int __p mean?

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View
Hello frenz,

 I was searching for driver code for SMC91C111 and came across some
code in uboot. the following is a piece of code from that the
smc91c111.h file.
#define    SMC_outb(d,r)    ({    word __d = (byte)(d);  \
                word __w = SMC_inw((r)&~1);  \
            __w &= ((r)&1) ? 0x00FF : 0xFF00;  \
            __w |= ((r)&1) ? __d<<8 : __d;  \
            SMC_outw(__w,(r)&~1);  \
            })

I would like to know the significance of the underscore(__) before the
p variable. I think it is some sort of memory qualifier,but not sure.
Could anybody give me clear picture

Regards
Anil


Re: what does __p in unsigned int __p mean?

Quoted text here. Click to load it

Since it is tight with the variable name it can't be. It is simply to
make it a bit more unique.

--
42Bastian
Do not email to snipped-for-privacy@yahoo.com, it's a spam-only account :-)
We've slightly trimmed the long signature. Click to see the full one.
Re: what does __p in unsigned int __p mean?
hi,
  I could have considered it that way but the following piece of code
does not support that idea.

#define SMC_inb(p)    ({ \
    unsigned int __p = (unsigned int)(SMC_BASE_ADDRESS + (p)); \
    unsigned int __v = *(volatile unsigned short *)((__p) & ~1); \
    if (__p & 1) __v >>= 8; \
    else __v &= 0xff; \
    __v; })

the significance of the underscore in the last line of macro is still
unclear.


Re: what does __p in unsigned int __p mean?
Quoted text here. Click to load it

It looks as if the underscores have been put there by the code's author to
ensure that the (local) variable names don't conflict with other similar,
single letter, variable names that might be present in application code.

    Andrew



Re: what does __p in unsigned int __p mean?
Quoted text here. Click to load it

Unfortunately identifiers beginning __ are reserved by the compiler for
any purpose (as are _ followed by an uppercase letter).


--
Paul Black                        mailto: snipped-for-privacy@oxsemi.com
Oxford Semiconductor Ltd          http://www.oxsemi.com
We've slightly trimmed the long signature. Click to see the full one.
Re: what does __p in unsigned int __p mean?
Quoted text here. Click to load it

Indeed: someone not following the standard!

    Andrew



Re: what does __p in unsigned int __p mean?
Quoted text here. Click to load it


That's not necessarily true.  Remember this macro was found in a
hardware-specific header file of Uboot.  Uboot is infrastructure code,
just like the OS on a more conventional hosted implementation.  So its
headers could justifiably be considered part of the C implementation
for normal programs, which would allow it to use the reserved names.

--
Hans-Bernhard Broeker ( snipped-for-privacy@physik.rwth-aachen.de)
Even if all the snow were burnt, ashes would remain.

Re: what does __p in unsigned int __p mean?
Quoted text here. Click to load it


The significance of the underscores is that they are part of the name of
the variable.  The final line of the macro here is a gcc extension that
makes the return value of the macro call available to the caller:
http://gcc.gnu.org/onlinedocs/gcc-4.0.1/gcc/Statement-Exprs.html

I can't think of any good reason why this is written as a macro rather
than an inline function, however - after all, an inline function has all
the benefits of a macro (for this sort of usage) without having to worry
about naming conflicts.

mvh.,

David

Re: what does __p in unsigned int __p mean?
Quoted text here. Click to load it


You seem to have a particlarly ugly case of writing
inline functions. The code on both functions is
complicated enough to need an own function each.

In the SMC_inb() function, there is only a variable
'__v', no variable called 'v'.

Probably, the purpose of the last statetment is to
get the value of __v as the return value of the function.

The function finds the 16-bit word for the register
at offset p in the SMC controller register block,
reads the 16-bit register and splits the desired
byte and returns it.

A function for similar use:

   static unsigned char SMC_inb(int p)
      {
      volatile unsigned short *wordaddress;
      unsigned int value;

      wordaddress = (volatile unsigned short *)
                     (SMC_BASE_ADDRESS + (p & ~1));

      value = *wordaddress;

      if (p & 1)
          return (unsigned char)(value >> 8);

      return (unsigned char)(value & 0xff);
      }

If you're using GCC, the effect of the original macro
can be achieved by declaring the function above with
the 'inline' qualifier (and it's much cleaner).

Similar considerations apply to the output function/macro.

--

Tauno Voipio
tauno voipio (at) iki fi


Re: what does __p in unsigned int __p mean?

Quoted text here. Click to load it

What is "that way" in this context ?

Quoted text here. Click to load it
This declares a variable called __p, which is available only in this
{} block.

Quoted text here. Click to load it

and calculate and store the address of the register into __p

Quoted text here. Click to load it

Make that address even, read a 16 bit word (in little endian format)
from the hardware register and store it into the local variable __v.

Quoted text here. Click to load it

Depending if the odd or even port register was requested, move the 8
bit port value to the lower part of the __v variable, which also is
the return value for the SMC_inb macro "function".

This macro simply gets a byte from a hardware register that only
support 16 bit word access at even addresses.

Paul

Re: what does __p in unsigned int __p mean?
@g49g2000cwa.googlegroups.com:

Quoted text here. Click to load it

It is strictly to make it far less likely that the names will conflict with
variable names created by users of the function macro.

--
Richard

Re: what does __p in unsigned int __p mean?

Quoted text here. Click to load it

How so?

Quoted text here. Click to load it

There is none.  It's just a name.  It happens to start with
underscores to minimize the chances that it will collide with
another name.


--
Grant Edwards                   grante             Yow!  Darling, my ELBOW
                                  at               is FLYING over FRANKFURT,
We've slightly trimmed the long signature. Click to see the full one.
Re: what does __p in unsigned int __p mean?
p and __p are two totally different expressions.

p is what the compiler will resolve each time the macro is "called".
whereas __p is a stack allocated variable on the stack frame created by
the macro. By watching the code it is clear that the variable __p is a
copy of what p might be at compile-time.

There is NOTHING special about the underscores preceding the declared
temp variable as Grant Edwards points out.

Cheers

Grant Edwards wrote:
Quoted text here. Click to load it


Re: what does __p in unsigned int __p mean?
Quoted text here. Click to load it


It has none --- for starters because there *is* no variable named "p"
nor "__p" in the above.

The __ has no significance of its own.  It's not even a syntactical
element of its own: it's just a part of the variable names "__d" and
"__w", put there to avoid collisions of these names with variables
used in functions that might invoke this macro.  It's essentially a
"no user-serviceable parts inside" sticker.

Frankly, if you don't understand this much of C, I don't think you
should be messing around with system header files yet.

--
Hans-Bernhard Broeker ( snipped-for-privacy@physik.rwth-aachen.de)
Even if all the snow were burnt, ashes would remain.

Re: what does __p in unsigned int __p mean?

Quoted text here. Click to load it

In C, identifiers beginning with two underscores or an underscore
followed by a uppercase letter are reserved for the implementation
(compiler), thus are not to be declared by the programmer.  There are
other reserved identifiers as well.  This way the implementor can
declare symbols guaranteed not to collide with user code which conforms
to the standard.

That said, the above code uses a non-standard extension in which a
bracketed list of statements is contained within an expression, which
should make declared variables strictly local to the declaration,
obviating the need for variables in the implementor's name space.

Thad


Re: what does __p in unsigned int __p mean?
comp.arch.embedded:

Quoted text here. Click to load it

Actually, the identifiers defined with leading double underscores in
this macro, and the other macro you quote in another post, are
absolutely unnecessary inclusions by someone whose understanding of
the C language standard is somewhat weak.

As others have pointed out, all identifiers beginning with two
underscores, or an underscore followed by an upper case letter, are
reserved for the implementation.  That means the compiler, its
headers, and its libraries, not a 3rd party application like uboot, no
matter how "system" it seems to be.

But regardless of whether or not uboot should be using identifiers of
this pattern is rather immaterial, because they are absolutely
unnecessary in this context.

Both macros open a block that contains their variable definitions and
code.  That block establishes its own scope, so if they happened to
define a variable, say 'x', that exists in an outer scope, the 'x'
defined inside the block completely hides the 'x' in the outer scope.

--
Jack Klein
Home: http://JK-Technology.Com
We've slightly trimmed the long signature. Click to see the full one.
Re: what does __p in unsigned int __p mean?
Quoted text here. Click to load it

Some compilers will warn about a variable hidden in outer scope;
perhaps that is what the author was trying to prevent.

--
John W. Temples, III

Re: what does __p in unsigned int __p mean?

Quoted text here. Click to load it

Which would be a bad thing if the user supplied 'x' as the macro
parameter.

Paul


Re: what does __p in unsigned int __p mean?

Quoted text here. Click to load it

Wrong.  For a macro like this, they're absolutely necessary to keep
the macro universally usable.  They avoid a name conflict between the
actual macro argument and the variables created inside the macro body.

Quoted text here. Click to load it

Which will explode nicely into your face as soon as somebody invokes the
macros with the parameter being named 'x', too.

But all this is somewhat beside the point: those macros are 100% evil.
Creating local variables inside a macro is a gold-plated invitation
for nasty problems.  This should really be done either in an inline
function.  Failing that (some compilers may not support inline), it
should be re-written as a single-statement macro: no local variables,
no strange compiler-specific extensions to allow a block to return a
value.

--
Hans-Bernhard Broeker ( snipped-for-privacy@physik.rwth-aachen.de)
Even if all the snow were burnt, ashes would remain.

Re: what does __p in unsigned int __p mean?
Quoted text here. Click to load it

Good point.


Sorry, I can't agree with the 100% evil.  They are simply the
implementation for access functions in a custom header.  Yes, they use
extensions that aren't strictly required, but that's OK for a specific
compiler+target, which this is.  You might consider it an invitation,
but nothing bad happens if you simply use the code as is.  Its worse
aspect, i think, is that it sets a poor example for the average Joe
programmer peeking under the hood, but it works.

Thad


Site Timeline