Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix aligned attribute on GCC

  • Loading branch information...
commit ceffc3b13f3cadafc499bca18c61ea35ab337dba 1 parent 10b007f
@jmckaskill jmckaskill authored
Showing with 51 additions and 70 deletions.
  1. +1 −1  Makefile
  2. +17 −15 parser.c
  3. +12 −27 test.c
  4. +21 −27 test.lua
View
2  Makefile
@@ -47,7 +47,7 @@ test_cdecl.so: test.o
$(SOCC) $^ -o $@
test_posix: test_cdecl.so $(MODSO)
- $(LUA) test.lua
+ LD_LIBRARY_PATH=./ $(LUA) test.lua
View
32 parser.c
@@ -310,6 +310,7 @@ static void calculate_member_position(lua_State* L, struct parser* P, struct cty
if (mt->is_variable_struct || mt->is_variable_array) {
luaL_error(L, "NYI: variable sized members in unions");
+ return;
} else if (mt->is_bitfield) {
msize = (mt->align_mask + 1);
@@ -1007,25 +1008,26 @@ static int parse_attribute(lua_State* L, struct parser* P, struct token* tok, st
return luaL_error(L, "unexpected token in align on line %d", P->line);
}
- /* TODO: strictly speaking __attribute__(aligned(#)) is only
- * supposed to increase alignment
+ /* __attribute__(aligned(#)) is only supposed to increase alignment
*/
- switch (tok->integer) {
- /* TODO: atm we just leave this as it was, but is there a
- * better way of handling this
- */
- case 0: break;
- case 1: ct->align_mask = 0; break;
- case 2: ct->align_mask = 1; break;
- case 4: ct->align_mask = 3; break;
- case 8: ct->align_mask = 7; break;
- case 16: ct->align_mask = 15; break;
- default:
- return luaL_error(L, "unsupported align size on line %d", P->line);
+ if (IS_LITERAL(*tok, "align") || tok->integer > ct->align_mask) {
+ switch (tok->integer) {
+ /* TODO: atm we just leave this as it was, but is there a
+ * better way of handling this
+ */
+ case 0: break;
+ case 1: ct->align_mask = 0; break;
+ case 2: ct->align_mask = 1; break;
+ case 4: ct->align_mask = 3; break;
+ case 8: ct->align_mask = 7; break;
+ case 16: ct->align_mask = 15; break;
+ default:
+ return luaL_error(L, "unsupported align size on line %d", P->line);
+ }
+ ct->align_is_forced = 1;
}
- ct->align_is_forced = 1;
check_token(L, P, TOK_CLOSE_PAREN, NULL, "expected align(#) on line %d", P->line);
} else if (IS_LITERAL(*tok, "packed")) {
View
39 test.c
@@ -43,6 +43,17 @@ bool have_complex()
#endif
}
+EXPORT bool is_msvc();
+
+bool is_msvc()
+{
+#ifdef _MSC_VER
+ return 1;
+#else
+ return 0;
+#endif
+}
+
EXPORT int test_pow(int v);
int test_pow(int v)
{ return v * v; }
@@ -157,8 +168,7 @@ ALIGN2(16, NO_ATTR)
#ifdef _MSC_VER
#define ATTR_(TYPE, ALIGN) __declspec(align(ALIGN)) TYPE v
#else
-/*#define ATTR_(TYPE, ALIGN) TYPE v __attribute__((aligned(ALIGN)))*/
-#define ATTR_(TYPE, ALIGN) __attribute__((aligned(ALIGN))) TYPE v
+#define ATTR_(TYPE, ALIGN) TYPE v __attribute__((aligned(ALIGN)))
#endif
#define ATTR1(TYPE) ATTR_(TYPE, 1)
@@ -167,36 +177,11 @@ ALIGN2(16, NO_ATTR)
#define ATTR8(TYPE) ATTR_(TYPE, 8)
#define ATTR16(TYPE) ATTR_(TYPE, 16)
-#pragma pack(push)
-#pragma pack(1)
ALIGN2(attr_1, ATTR1)
ALIGN2(attr_2, ATTR2)
ALIGN2(attr_4, ATTR4)
ALIGN2(attr_8, ATTR8)
ALIGN2(attr_16, ATTR16)
-#pragma pack(pop)
-
-EXPORT bool alignment_attribute_works(int align);
-
-/* GCC alignment attribute is broken */
-bool alignment_attribute_works(int align)
-{
-#pragma pack(push)
-#pragma pack(1)
- struct {char pad; ATTR2(void*);} s2;
- struct {char pad; ATTR4(void*);} s4;
- struct {char pad; ATTR8(void*);} s8;
- struct {char pad; ATTR16(void*);} s16;
-#pragma pack(pop)
- switch (align) {
- case 1: return 1;
- case 2: return ((char*) &s2.v - (char*) &s2) == align;
- case 4: return ((char*) &s4.v - (char*) &s4) == align;
- case 8: return ((char*) &s8.v - (char*) &s8) == align;
- case 16: return ((char*) &s16.v - (char*) &s16) == align;
- default: return 0;
- }
-}
#ifdef _MSC_VER
#define alignof(type) __alignof(type)
View
48 test.lua
@@ -20,9 +20,9 @@ end
print('Running test')
ffi.cdef [[
+bool is_msvc();
bool have_complex();
bool have_complex2() __asm("have_complex");
-bool alignment_attribute_works(int align);
int8_t add_i8(int8_t a, int8_t b);
uint8_t add_u8(uint8_t a, uint8_t b);
@@ -177,24 +177,6 @@ struct align_ALIGN_SUFFIX {
int print_align_ALIGN_SUFFIX(char* buf, struct align_ALIGN_SUFFIX* p);
]]
-local align_attr = [[
-#pragma pack(push)
-#pragma pack(2)
-/* check that attribute align overrides pack pragma */
-struct align_attr_ALIGN_SUFFIX {
- char pad;
- TYPE v __attribute__(align(ALIGN));
-};
-
-struct align2_attr_ALIGN_SUFFIX {
- char pad;
- __declspec(aligned(ALIGN)) TYPE v;
-};
-
-int print_align_attr_ALIGN_SUFFIX(char* buf, struct align_attr_ALIGN_SUFFIX* p);
-#pragma pack(pop)
-]]
-
local palign = [[
#pragma pack(push)
#pragma pack(ALIGN)
@@ -307,6 +289,22 @@ for convention,c in pairs(dlls) do
check(c.have_complex(), c.have_complex2())
+ local align_attr = c.is_msvc() and [[
+ struct align_attr_ALIGN_SUFFIX {
+ char pad;
+ __declspec(align(ALIGN)) TYPE v;
+ };
+
+ int print_align_attr_ALIGN_SUFFIX(char* buf, struct align_attr_ALIGN_SUFFIX* p);
+ ]] or [[
+ struct align_attr_ALIGN_SUFFIX {
+ char pad;
+ TYPE v __attribute__(aligned(ALIGN));
+ };
+
+ int print_align_attr_ALIGN_SUFFIX(char* buf, struct align_attr_ALIGN_SUFFIX* p);
+ ]]
+
for suffix, type in pairs(types) do
local test = test_values[type]
--print('checkbuf', suffix, type, buf, test)
@@ -325,15 +323,11 @@ for convention,c in pairs(dlls) do
ffi.cdef(align_attr:gsub('SUFFIX', suffix):gsub('TYPE', type):gsub('ALIGN', align))
end
- if c.alignment_attribute_works(align) then
- local v = ffi.new('struct align_' .. align .. '_' .. suffix, {0, test})
- checkalign(type, v, c['print_align_' .. align .. '_' .. suffix](buf, v))
+ local v = ffi.new('struct align_' .. align .. '_' .. suffix, {0, test})
+ checkalign(type, v, c['print_align_' .. align .. '_' .. suffix](buf, v))
- local v2 = ffi.new('struct align_attr_' .. align .. '_' .. suffix, {0, test})
- local v3 = ffi.new('struct align2_attr_' .. align .. '_' .. suffix, {0, test})
- checkalign(type, v2, c['print_align_attr_' .. align .. '_' .. suffix](buf, v2))
- checkalign(type, v3, c['print_align_attr_' .. align .. '_' .. suffix](buf, v2))
- end
+ local v2 = ffi.new('struct align_attr_' .. align .. '_' .. suffix, {0, test})
+ checkalign(type, v2, c['print_align_attr_' .. align .. '_' .. suffix](buf, v2))
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.