This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Specify alignment for string literal.
On Sun, 09 Oct 2011 22:11:29 -0700, Ian Lance Taylor <iant@google.com>
wrote:
> Kaz Kylheku <kaz@kylheku.com> writes:
>> /* wchar_t is 16 bits and 2 byte aligned on this
>> platform; I want 4 byte alignment */
>>
>> wchar_t *foo = L"...";
>>
>> The attribute has to go on the literal object, not on foo, obviously.
>
> Perhaps you could write
>
> static wchar_t foo_str[] __attribute__ ((aligned (4))) = L"...";
> wchar_t *foo = foo-str;
>
> Ian
Hi Ian,
That's not a bad suggestion. In many cases it could be wrapped
with a macro. E.g.
aligned_literal("abc")
produces a GCC evaluation block:
({ static wchar_t f_o_o[] .... = L"abc"; f_o_o; })
The approach I went with is now documented here:
http://git.savannah.gnu.org/cgit/txr.git/tree/HACKING
In section 2.4.2
I'm adding tag bits to string literals so they can look like
objects in a system of dynamic types.
Turns out we can add a two-bit tag to a pointer even if it
is only two byte aligned, if we can ensure that no two such
objects land into the same four-byte word, and if we have a
way to find the real start of the object after ripping off
the tag.
Since the objects are C strings, it's easy to achieve both
goals with null padding.
Cheers ...