This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Passing an address to a #define in C


Hello Andi:
I really appreciatte your support.

While your solution seems to be a good alternative, it didn't work in my
particular case. I guess I should explain in details what my goals are:

I need to put an specific machine code in any part of the memory. This code
is for some hardware module that can understand it. Of course, gcc will not
understand it, so I need to use .word 0xfc to create that code.
Now, I would like to create a pseudo assembler for my module, in following
way:

#define       LOAD         asm(".word 0xFC \n\t");
#define       INSTRUCTION2   asm(.word  0x4b \n\t");
#define       GOTO(label)         asm(".word " #label " \n\t");


void mycode(void) {

  LOAD
Label1:
  INSTRUCTION2  
  GOTO(Label1)
}


so, the compiler should create 

0x000000FC
0x0000004B
.....

In this context, the label should be used. When I use your solution, it
seems like the compiler creates processor specific code in that are, and not
the kind of machine code I want to put there....

Any thoughts?

Thank you very much!
Gus






Andi Hellmund wrote:
> 
> Hi Gus,
> 
>> I'm aware of the Labels as a Value feature. However, it seems it only
>> work
>> with goto label instruction.
>> In my particular case, I need to use the label just to pass the relative
>> address of the label as a parameter to a @define statement.
> 
> Yes, the label-as-value is primarily used for goto expressions, but as you
> can see from the online documentation:
> 
> "You can get the address of a label defined in the current function (or a
> containing function) with the unary operator `&&'. The value has type void
> *. This value is a constant and can be used wherever a constant of that
> type is valid. For example:
> 
>      void *ptr;
>      /* ... */
>      ptr = &&foo;
> "
> 
> So, this label-as-value thing will return the address of the label which
> should be the address of the first instruction following the label.
> 
>> I tried using this and it didn't work.
> 
> This is because your code finally ends up with this expression:
> 
> (void*)ptr >> 4
> 
> and gcc doesn't allow the shift operation to be performed on pointer
> types. A possible solution to your problem could be the following code:
> 
> ===
> #include <stdio.h>
> #include <stdint.h>
> 
> #define SHADDR(ptr) (void*)((uintptr_t)(ptr) >> 4)
> 
> int main ()
> {
>   int a, b=10;
> 
>   label1:
>     a = b + 20;
>    void *ptr = SHADDR(&&label1);
> }
> ===
> 
> Hope that helps,
> Andi
> 
> 
>> Andi Hellmund wrote:
>>>
>>> Hi Gus,
>>>> However, it seems like it only works in GCC if goto(label1) instruction
>>>> is
>>>> used... Is there any other way to do this?
>>>>
>>>
>>> you might want to have a look at this online doc about label-as-values:
>>>
>>> http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html
>>>
>>> If I remember correctly, this is not part of an official standard
>>> (except the GNU C standard), so the portability of your code might
>>> decrease.
>>>
>>> Best regards,
>>> Andi
>>>
>>>
>>>
>>>
>>
>> --
>> View this message in context:
>> http://old.nabble.com/Passing-an-address-to-a--define-in-C-tp31890487p31894925.html
>> Sent from the gcc - Help mailing list archive at Nabble.com.
>>
>>
>>
> 
> 
> 

-- 
View this message in context: http://old.nabble.com/Passing-an-address-to-a--define-in-C-tp31890487p31896523.html
Sent from the gcc - Help mailing list archive at Nabble.com.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]