Welcome, Guest. Please login or register.

Author Topic: Help needed combining GCC/C++ with asm sources  (Read 5743 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline PiR

  • Full Member
  • ***
  • Join Date: Apr 2003
  • Posts: 148
    • Show all replies
Re: Help needed combining GCC/C++ with asm sources
« on: March 25, 2004, 11:37:15 AM »
Hi Karlos

As you have trouble to find 'anybody' you can count on me. ;-)

I don't know the convention of GNU, however having original SAS/C with printed documents I can tell you one thing for it:
d0/d1/a0/a1 can be used and trashed, but for C.

I'm sure that in C++ SAS uses one address register (don't remember wich one) for 'this' pointer, additionally one integer register in constructors/destructors for internal usage, and I guess one more register for something, but I can't remember at the moment. And of course while planning your shared library remember to reserve a6 for library base.
I guess GNU may have some similar requirements.

Honestly, being you, I would think of C++ inlines that simply call your functions via regular C interface.

Good luck
 

Offline PiR

  • Full Member
  • ***
  • Join Date: Apr 2003
  • Posts: 148
    • Show all replies
Re: Help needed combining GCC/C++ with asm sources
« Reply #1 on: March 25, 2004, 08:02:12 PM »
Hi again Karlos

I'm wondering why you wrote 'extern "C"' didn't work.
Why? What happend?
This would mean that some C headers are not compatible with C++ under GNU and that would be really strange. :-o

Thats great that your solution works, however repeating this for every function can mean a lot of stupid and potential buggy-introducing work.
The other thing is that simple 'jsr' makes your code position dependant always, regardles of the compiler options.

Besides this GNU asm-embeding mumbo-jumbo always scared me. :-o ;-)

Cheers
 

Offline PiR

  • Full Member
  • ***
  • Join Date: Apr 2003
  • Posts: 148
    • Show all replies
Re: Help needed combining GCC/C++ with asm sources
« Reply #2 on: March 25, 2004, 09:06:09 PM »
Hi Karlos

Thats pretty wired that you cannot specify registers in C++, despide extern "C". I will check it as soon as I'll manage to install working GNU compiler. ;-)

Honestly that makes me curious and thats another reason to install it. ;-)

BTW - have you checked if GNU doesn't have a convention in chosing registers in:

void func(register int intarg, register char *ptrarg);

I'm only guessing... It can be not allowed with GNU.

And would it make any change, if you:

extern "C" void func(int i __asm("d0"), int* j __asm("a0"));


jsr is easy - it accepts absolute address.
In small-code option bsr is used, which accepts only offsets from PC. This means that no matter where you load the whole code, your offset won't change.

If you (or someone using the code - even from statical library) wanted to produce such code, but you put 'jsr' via back-door this could lead to some wiredness.
However changing jsr to bsr could be mayby redesigned by linker? (rough guessing - and I doubt it).

Cheers
 

Offline PiR

  • Full Member
  • ***
  • Join Date: Apr 2003
  • Posts: 148
    • Show all replies
Re: Help needed combining GCC/C++ with asm sources
« Reply #3 on: March 26, 2004, 10:22:43 AM »
Hi Karlos

Especially for you I investigated few things.

1. SAS C++ (just to keep this info just in case):
A0 - 'this' for all nonstatic methods
D0 - class information (internal for compiler) for constructors/destructors
A1 - address of the place where to return value for functions that return whole complex objects

2. I know that 'register' is only a hint. However it is just a hint for local variables, but must be more strict meaning for function arguments, as every call must be made with arguments in the exact places, where the function expects them and the function can be defined in the other file than the call to it.
So the compiler can have a convention and SAS for example allocates address registers for pointers and data registers for integers, beginning with A0 and D0. Once the mechnism is checked you can assume that it must be consistent every time (at least with the same compiling options).

3. I've noticed that you omited 'register' in the example function. Mayby it is only mistype here, however if you did this in the code this may be the reason why GNU C++ complains about it. Correct syntax should be:

void func(register int i __asm("d0"), register int* j __asm("a0"));

I've checked it in my GNU documentation.
Well, installation of simple guides seems to be easier than the whole environment. Surprise, surprise...  :-D

4. Problems with relocation can occur only in very specific situations, rare in real life.
Lets suppose you want your code to reside in FAST, but it was loaded to CHIP. If your code is position independed you can simply copy it from place to place byte by byte and thats it. If you use any absolute addresses you must think about all relocations afterwards.
For Amiga executables relocation is done by loader that is part of OS. Every binary, besides the code itself, has additionally so called relocation tables, that describe the places in the code that needs to be recalculated after loading (practically just by adding address of the beginning of the code to the result place) to make the code usable (famous HUNK format). Once this is done, the code must reside under this exact address.

5. Unluckilly yes. 'jsr symbolname' evaluates to jump to absolute address, as this is the syntax of this opcode. Of course it will be correctly relocated during loadtime, so do not worry about it. I'm not sure if some cleaver linker cannot change it to bsr or jsr symbol(PC), but I would worry about changing the length of the command as this could destroy all code offsets calculated and used by the compiler. I meant that if someone wanted to produce position independent code and prepared compiler options for that, but used such code, he won't get position independence, but he won't know about it.


Sorry if this was too boring. ;-)
Cheers