Re: suggestion for GCC (1)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



* David Brown <david@xxxxxxxxxxxxxxx> wrote:

> gcc itself knows about /some/ of these things, given the correct flags 
> (it will probably know about alignment issues, but is unlikely to know 
> about cache line sizes).  Thus it will sometimes inline memcpy using the 
> builtin code, such as for small copies where the number of bytes is 
> fixed at compile time, and it will sometimes call an external function.

Apropos: is there some way to extend these builtins from external
libraries ?

Let's assume I'm writing some library for very common ATDs, eg. lists,
hashtables, etc. And for some specific targets I'd like to do some
heavy optimizations, which should be completely hidden behind the
API (just like done with memcpy, etc). So it would be nice to somehow
plug into the compiler's code generation, but w/o touching the compiler
itself, just giving it more deeper information beyond the usual C code.

An typical use case could be string concatenation, an strcat() that
takes multiple arguments. For example:

    char buffer[1024];
    const char* ptr const = "huh";
    strcatx(buffer, "hello", " world: ", strerror(errno), " blah ", ptr);

This could well be implemented as a macro using __VA_ARGS etc, but there's
still much room for optimization which would require compiler-internal
information. In this particular case we know several things, eg:

    * "hello" and " world: " are really constant strings, so the result
      of their concatenation is known at compile time: "hello world: "
    * strerror() maps a limited number range to fixed strings, to it
      could be a direct array lookup (after proper bounds checking)
    * concetenation of " blah " and ptr also has an known result at
      compile time: " blah huh"
    * sizes of the constant strings is already defined at compile time.

So the same could also be written that ways:

    const char* _errptr;
    int _errlen;
    if ((errno >= ERRNO_MIN_IDX) && (errno <= ERRNO_MAX_IDX))
    {
	_errptr = _errno_str_vector[errno];
	_errlen = _errno_sz_vector[errno];
    }
    else
	_errptr = "Unknown error code";
	_errlen = 18;
    }
    char buffer[1024];
    memcpy(buffer, "hello world: ");
    memcpy(buffer+13, sizeof(buffer)-13, _errptr);
    memcpy(buffer+13+_errlen, sizeof(buffer)-13-_errlen, " blah foo", 9);
    buffer[13+_errlen+9] = 0;


Is there a way to add those things by external libraries, w/o
touching the compiler itself ?

> There are plenty of complete toolchains based on gcc.  A good source of 
> these is www.codesourcery.com - the developers there do a lot of work on 
> various embedded gcc targets.  When you download a toolchain from them, 
> you get the assembler, linker and libraries included.  There are also 
> plenty of other ready-packaged gcc-based toolchains for other 
> microcontrollers.

Maybe crosstool-ng might also be worth looking at.
 

cu
-- 
----------------------------------------------------------------------
 Enrico Weigelt, metux IT service -- http://www.metux.de/

 phone:  +49 36207 519931  email: weigelt@xxxxxxxx
 mobile: +49 151 27565287  icq:   210169427         skype: nekrad666
----------------------------------------------------------------------
 Embedded-Linux / Portierung / Opensource-QM / Verteilte Systeme
----------------------------------------------------------------------


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux