This is the mail archive of the crossgcc@cygnus.com mailing list for the crossgcc project.


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

Re: Code Gen problem calling functions through pointers.


> I have a problem with calling functions through pointers. I have assigned 
> some unused vector table entries on 68K as holding places for functions 
> that are called from applications link at a separate time. To call the 
> functions (via the vector table entry) I use the following macros
> 
> #define TEST_FN_VECTOR	0x100
> #define TestFunction() 	(*(void (*)(void))(TEST_FN_VECTOR<<2))()
> 
> I have found that the compiler sometimes generates the correct code. Eg.
> 
>         move.w #256,%a2
>         jsr (%a2)
> 
> But at other times generates
> 
>         jsr 256.w
> 

How about this instead:

	// vector numbers

	enum Vectors
	{
	  // ... lots of vectors

	  TEST_FN_VECTOR,  

	  // ... lots more vectors

	  NUM_VECTORS    
	};


	// vector table, elsewhere

	extern void (*vectors[NUM_VECTORS])(void) 
       		__attribute__ ((section (".vectors")));


	// client function

	void foo()
	{
	  vectors[TEST_FN_VECTOR]();	// call through vector table
	}


Then use the linker to put the ".vectors" section at the appropriate place,
and mark it "NOLOAD".

Note that this method entirely avoids the preprocessor, thus the compiler
fully type-checks all of the declarations.  Note also that the use of an
array avoids the necessity of knowing the size of a pointer, increasing
portability.  Using an enum also helps avoid duplication of the vector
numbers.

As an aside, our company has built an embedded real-time operating system
entirely in C++; it purposely contains almost no preprocessor macros.  The
preprocessor is a great tool, but many of its uses can be replaced by common
C++ constructs such as consts, enums, inline functions, and templates. 
Doing so often yields better type checking and maintainability.

Regards, 

David Querbach
Real-Time Systems Inc.

_______________________________________________
New CrossGCC FAQ: http://www.objsw.com/CrossGCC
_______________________________________________
To remove yourself from the crossgcc list, send
mail to crossgcc-request@cygnus.com with the
text 'unsubscribe' (without the quotes) in the
body of the message.

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