Forums

Mixed C/Assembler compile-time constants?

Started by Borrall Wonnell December 20, 2006
Hi all,

I'm using C and asm in a TI-based DSP project under Code Composer
Studio.  I want to use compile-time (or assemble-time, if you will)
constants.  Example (please ignore style):

C:       #define VAL 10
ASM:  _VAL   .set    10

Compile-time constants are useful for defining array sizes in C, and
assembler code is simpler and more efficient.  Some of the constants I
want to use are required in both C and ASM domains.

Is there a way to define a constant in one file such that it is visible
to C and ASM?  Right now, I am defining the same constant twice...once
in a "defs.h" for use in C and once in a "defs.inc" file for use in
ASM.  Code Composer Studio doesn't have a problem with this, and builds
even with "VAL" defined in both C and in ASM.

I am able to access values defined in ASM by doing something like this
in C:
extern int VAL;
#define cVAL ((int) (&VAL))

Using 'cVAL' results in the correct value, but it behaves as a run-time
value, not a compile-time constant; I cannot initialize any arrays
using this method.  I *could* get around this by writing a program that
generates basic C/asm definitions based on a single set of values...but
this seems like a lot of work and IMO doesn't really simplify things.

Any ideas, aside from status quo (maintaining duplicate sets of
constants)?


Cheers,
Dave

"Borrall Wonnell" <dave_bonnell@hotmail.com> writes:

> Hi all, > > I'm using C and asm in a TI-based DSP project under Code Composer > Studio. I want to use compile-time (or assemble-time, if you will) > constants. Example (please ignore style): > > C: #define VAL 10 > ASM: _VAL .set 10 > > Compile-time constants are useful for defining array sizes in C, and > assembler code is simpler and more efficient. Some of the constants I > want to use are required in both C and ASM domains. > > Is there a way to define a constant in one file such that it is visible > to C and ASM? Right now, I am defining the same constant twice...once > in a "defs.h" for use in C and once in a "defs.inc" file for use in > ASM. Code Composer Studio doesn't have a problem with this, and builds > even with "VAL" defined in both C and in ASM. > > I am able to access values defined in ASM by doing something like this > in C: > extern int VAL; > #define cVAL ((int) (&VAL)) > > Using 'cVAL' results in the correct value, but it behaves as a run-time > value, not a compile-time constant; I cannot initialize any arrays > using this method. I *could* get around this by writing a program that > generates basic C/asm definitions based on a single set of values...but > this seems like a lot of work and IMO doesn't really simplify things. > > Any ideas, aside from status quo (maintaining duplicate sets of > constants)?
Dave, the way I've handled this is to maintain such constants in one language and write a program (I like C) to convert the constants from one language to the other. In this manner, one of the languages' include file is a derived object, built by the C program. In my build file (I use gnumake) I then establish the proper dependencies and build rules. The syntax is usually simple enough that the translation program isn't too difficult to write. One potential problem is handling conditional compilation, but my projects have not required this. -- % Randy Yates % "Though you ride on the wheels of tomorrow, %% Fuquay-Varina, NC % you still wander the fields of your %%% 919-577-9882 % sorrow." %%%% <yates@ieee.org> % '21st Century Man', *Time*, ELO http://home.earthlink.net/~yatescr
Borrall Wonnell wrote:
> Is there a way to define a constant in one file such that it is visible > to C and ASM? Right now, I am defining the same constant twice...once > in a "defs.h" for use in C and once in a "defs.inc" file for use in > ASM.
Many assemblers have the option to preprocess assembler source files using the C preprocessor. This way you could use your defs.h in assembler as well. If your assembler doesn't support that, you could make a wrapper script or makefile rule to do it: preprocess into a temporary file and assemble that. This assumes a reasonably similar syntax, though. If your assembler wants "123h" instead of "0x123", or routinely uses things which are invalid C tokens (such as unbalanced quotes), this won't work easily. Another alternative would be to generate defs.inc from defs.h by a small Perl script or similar.
> Code Composer Studio doesn't have a problem with this, and builds > even with "VAL" defined in both C and in ASM.
Sure, C's "#define" usually doesn't end up in the object file and will not cause linking conflicts. Stefan
> Many assemblers have the option to preprocess assembler source files > using the C preprocessor.
I couldn't find a way to make this work. I was hoping that someone else had encountered this with CCS and managed a workaround.
> Another alternative would be to generate defs.inc from defs.h by a small > Perl script or similar.
I think this will be the eventual solution. Not terribly elegant, but at least it avoids duplicate definitions. Thanks for the input, Dave
David Bonnell wrote:
> > Many assemblers have the option to preprocess assembler source files > > using the C preprocessor. > > I couldn't find a way to make this work. I was hoping that someone > else had encountered this with CCS and managed a workaround. > > > Another alternative would be to generate defs.inc from defs.h by a small > > Perl script or similar. > > I think this will be the eventual solution. Not terribly elegant, but > at least it avoids duplicate definitions.
You're not going to like this, but I use the __asm keyword almost exclusively in embedded C/C++ code for this reason as well as a few others. My platform is Visual Studio 2005. With _declspec(naked) to make empty assembly functions and arbitrary byte-code emission from C++, there is not much else to ask for. Of course, if you're need to use pre-existing assembler source... -Le Chaud Lapin-
> >Any ideas, aside from status quo (maintaining duplicate sets of >constants)? > > >Cheers, >Dave >
Dave, I'm not sure if this works, since I no longer have access to CCS, but try this: asm("ARRAY_SIZE .equ 64"); short array[ARRAY_SIZE]; I was looking through some of my old TI code and I used something like this. Give it a quick compile an let me know if it works. Jeff