authorgravatar for evan@lagerdata.comEvan Haas <evan@lagerdata.com> 2021-03-06 09:17:17-08:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-03-07 14:53:33+02:00
log874c63f89cb6174d2dba32f4d08c140b5eed6744
treed4f68a819de0b2545bd3a8d8156112c68e0358ad
parenta7c0234eee97c11b1dd6879a3cf313ac9a7bdbd9

translate-c: translate align attribute for block scoped variables


2 files changed, 18 insertions(+), 29 deletions(-)

src/translate_c.zig+12-29
...@@ -671,15 +671,6 @@ fn visitVarDecl(c: *Context, var_decl: *const clang.VarDecl, mangled_name: ?[]co...@@ -671,15 +671,6 @@ fn visitVarDecl(c: *Context, var_decl: *const clang.VarDecl, mangled_name: ?[]co
671 break :blk null;671 break :blk null;
672 };672 };
673673
674 const alignment = blk: {
675 const alignment = var_decl.getAlignedAttribute(c.clang_context);
676 if (alignment != 0) {
677 // Clang reports the alignment in bits
678 break :blk alignment / 8;
679 }
680 break :blk null;
681 };
682
683 const node = try Tag.var_decl.create(c.arena, .{674 const node = try Tag.var_decl.create(c.arena, .{
684 .is_pub = is_pub,675 .is_pub = is_pub,
685 .is_const = is_const,676 .is_const = is_const,
...@@ -687,7 +678,7 @@ fn visitVarDecl(c: *Context, var_decl: *const clang.VarDecl, mangled_name: ?[]co...@@ -687,7 +678,7 @@ fn visitVarDecl(c: *Context, var_decl: *const clang.VarDecl, mangled_name: ?[]co
687 .is_export = is_export,678 .is_export = is_export,
688 .is_threadlocal = is_threadlocal,679 .is_threadlocal = is_threadlocal,
689 .linksection_string = linksection_string,680 .linksection_string = linksection_string,
690 .alignment = alignment,681 .alignment = zigAlignment(var_decl.getAlignedAttribute(c.clang_context)),
691 .name = checked_name,682 .name = checked_name,
692 .type = type_node,683 .type = type_node,
693 .init = init_node,684 .init = init_node,
...@@ -833,14 +824,7 @@ fn transRecordDecl(c: *Context, scope: *Scope, record_decl: *const clang.RecordD...@@ -833,14 +824,7 @@ fn transRecordDecl(c: *Context, scope: *Scope, record_decl: *const clang.RecordD
833 else => |e| return e,824 else => |e| return e,
834 };825 };
835826
836 const alignment = blk_2: {827 const alignment = zigAlignment(field_decl.getAlignedAttribute(c.clang_context));
837 const alignment = field_decl.getAlignedAttribute(c.clang_context);
838 if (alignment != 0) {
839 // Clang reports the alignment in bits
840 break :blk_2 alignment / 8;
841 }
842 break :blk_2 null;
843 };
844828
845 if (is_anon) {829 if (is_anon) {
846 try c.decl_table.putNoClobber(c.gpa, @ptrToInt(field_decl.getCanonicalDecl()), field_name);830 try c.decl_table.putNoClobber(c.gpa, @ptrToInt(field_decl.getCanonicalDecl()), field_name);
...@@ -1373,6 +1357,13 @@ fn transCStyleCastExprClass(...@@ -1373,6 +1357,13 @@ fn transCStyleCastExprClass(
1373 return maybeSuppressResult(c, scope, result_used, cast_node);1357 return maybeSuppressResult(c, scope, result_used, cast_node);
1374}1358}
13751359
1360/// Clang reports the alignment in bits, we use bytes
1361/// Clang uses 0 for "no alignment specified", we use null
1362fn zigAlignment(bit_alignment: c_uint) ?c_uint {
1363 if (bit_alignment == 0) return null;
1364 return bit_alignment / 8;
1365}
1366
1376fn transDeclStmtOne(1367fn transDeclStmtOne(
1377 c: *Context,1368 c: *Context,
1378 scope: *Scope,1369 scope: *Scope,
...@@ -1412,6 +1403,7 @@ fn transDeclStmtOne(...@@ -1412,6 +1403,7 @@ fn transDeclStmtOne(
1412 if (!qualTypeIsBoolean(qual_type) and isBoolRes(init_node)) {1403 if (!qualTypeIsBoolean(qual_type) and isBoolRes(init_node)) {
1413 init_node = try Tag.bool_to_int.create(c.arena, init_node);1404 init_node = try Tag.bool_to_int.create(c.arena, init_node);
1414 }1405 }
1406
1415 const node = try Tag.var_decl.create(c.arena, .{1407 const node = try Tag.var_decl.create(c.arena, .{
1416 .is_pub = false,1408 .is_pub = false,
1417 .is_const = is_const,1409 .is_const = is_const,
...@@ -1419,7 +1411,7 @@ fn transDeclStmtOne(...@@ -1419,7 +1411,7 @@ fn transDeclStmtOne(
1419 .is_export = false,1411 .is_export = false,
1420 .is_threadlocal = false,1412 .is_threadlocal = false,
1421 .linksection_string = null,1413 .linksection_string = null,
1422 .alignment = null,1414 .alignment = zigAlignment(var_decl.getAlignedAttribute(c.clang_context)),
1423 .name = mangled_name,1415 .name = mangled_name,
1424 .type = type_node,1416 .type = type_node,
1425 .init = init_node,1417 .init = init_node,
...@@ -4111,16 +4103,7 @@ fn finishTransFnProto(...@@ -4111,16 +4103,7 @@ fn finishTransFnProto(
4111 break :blk null;4103 break :blk null;
4112 };4104 };
41134105
4114 const alignment = blk: {4106 const alignment = if (fn_decl) |decl| zigAlignment(decl.getAlignedAttribute(c.clang_context)) else null;
4115 if (fn_decl) |decl| {
4116 const alignment = decl.getAlignedAttribute(c.clang_context);
4117 if (alignment != 0) {
4118 // Clang reports the alignment in bits
4119 break :blk alignment / 8;
4120 }
4121 }
4122 break :blk null;
4123 };
41244107
4125 const explicit_callconv = if ((is_export or is_extern) and cc == .C) null else cc;4108 const explicit_callconv = if ((is_export or is_extern) and cc == .C) null else cc;
41264109
test/translate_c.zig+6
...@@ -643,9 +643,15 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -643,9 +643,15 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
643 \\extern char my_array[16];643 \\extern char my_array[16];
644 \\__attribute__ ((aligned(128)))644 \\__attribute__ ((aligned(128)))
645 \\void my_fn(void) { }645 \\void my_fn(void) { }
646 \\void other_fn(void) {
647 \\ char ARR[16] __attribute__ ((aligned (16)));
648 \\}
646 , &[_][]const u8{649 , &[_][]const u8{
647 \\pub extern var my_array: [16]u8 align(128);650 \\pub extern var my_array: [16]u8 align(128);
648 \\pub export fn my_fn() align(128) void {}651 \\pub export fn my_fn() align(128) void {}
652 \\pub export fn other_fn() void {
653 \\ var ARR: [16]u8 align(16) = undefined;
654 \\}
649 });655 });
650656
651 cases.add("linksection() attribute",657 cases.add("linksection() attribute",