authorgravatar for evan@lagerdata.comEvan Haas <evan@lagerdata.com> 2022-07-28 21:04:17-07:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-07-30 13:24:03+03:00
log8632e4fc7b6cad33b951f71298ee6ac478e133cb
tree580e88e3c7305e5b7bbf0ac09f061fc78f03c8ce
parent3cf8f283d3f271b6d0b5de39f55e4ea40b83eece

translate-c: use correct number of initializers for vectors

Fixes #12264

3 files changed, 52 insertions(+), 30 deletions(-)

src/translate_c.zig+22-8
...@@ -2688,16 +2688,26 @@ fn transInitListExprVector(...@@ -2688,16 +2688,26 @@ fn transInitListExprVector(
2688) TransError!Node {2688) TransError!Node {
2689 _ = ty;2689 _ = ty;
2690 const qt = getExprQualType(c, @ptrCast(*const clang.Expr, expr));2690 const qt = getExprQualType(c, @ptrCast(*const clang.Expr, expr));
2691 const vector_type = try transQualType(c, scope, qt, loc);2691 const vector_ty = @ptrCast(*const clang.VectorType, qualTypeCanon(qt));
2692
2692 const init_count = expr.getNumInits();2693 const init_count = expr.getNumInits();
2694 const num_elements = vector_ty.getNumElements();
2695 const element_qt = vector_ty.getElementType();
26932696
2694 if (init_count == 0) {2697 if (init_count == 0) {
2695 return Tag.container_init.create(c.arena, .{2698 const zero_node = try Tag.as.create(c.arena, .{
2696 .lhs = vector_type,2699 .lhs = try transQualType(c, scope, element_qt, loc),
2697 .inits = try c.arena.alloc(ast.Payload.ContainerInit.Initializer, 0),2700 .rhs = Tag.zero_literal.init(),
2701 });
2702
2703 return Tag.vector_zero_init.create(c.arena, .{
2704 .lhs = try transCreateNodeNumber(c, num_elements, .int),
2705 .rhs = zero_node,
2698 });2706 });
2699 }2707 }
27002708
2709 const vector_type = try transQualType(c, scope, qt, loc);
2710
2701 var block_scope = try Scope.Block.init(c, scope, true);2711 var block_scope = try Scope.Block.init(c, scope, true);
2702 defer block_scope.deinit();2712 defer block_scope.deinit();
27032713
...@@ -2716,11 +2726,15 @@ fn transInitListExprVector(...@@ -2716,11 +2726,15 @@ fn transInitListExprVector(
2716 try block_scope.statements.append(tmp_decl_node);2726 try block_scope.statements.append(tmp_decl_node);
2717 }2727 }
27182728
2719 const init_list = try c.arena.alloc(Node, init_count);2729 const init_list = try c.arena.alloc(Node, num_elements);
2720 for (init_list) |*init, init_index| {2730 for (init_list) |*init, init_index| {
2721 const tmp_decl = block_scope.statements.items[init_index];2731 if (init_index < init_count) {
2722 const name = tmp_decl.castTag(.var_simple).?.data.name;2732 const tmp_decl = block_scope.statements.items[init_index];
2723 init.* = try Tag.identifier.create(c.arena, name);2733 const name = tmp_decl.castTag(.var_simple).?.data.name;
2734 init.* = try Tag.identifier.create(c.arena, name);
2735 } else {
2736 init.* = Tag.undefined_literal.init();
2737 }
2724 }2738 }
27252739
2726 const array_init = try Tag.array_init.create(c.arena, .{2740 const array_init = try Tag.array_init.create(c.arena, .{
src/translate_c/ast.zig+8
...@@ -154,6 +154,8 @@ pub const Node = extern union {...@@ -154,6 +154,8 @@ pub const Node = extern union {
154 div_exact,154 div_exact,
155 /// @offsetOf(lhs, rhs)155 /// @offsetOf(lhs, rhs)
156 offset_of,156 offset_of,
157 /// @splat(lhs, rhs)
158 vector_zero_init,
157 /// @shuffle(type, a, b, mask)159 /// @shuffle(type, a, b, mask)
158 shuffle,160 shuffle,
159161
...@@ -328,6 +330,7 @@ pub const Node = extern union {...@@ -328,6 +330,7 @@ pub const Node = extern union {
328 .div_exact,330 .div_exact,
329 .offset_of,331 .offset_of,
330 .helpers_cast,332 .helpers_cast,
333 .vector_zero_init,
331 => Payload.BinOp,334 => Payload.BinOp,
332335
333 .integer_literal,336 .integer_literal,
...@@ -1829,6 +1832,10 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {...@@ -1829,6 +1832,10 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
1829 const type_expr = try renderNode(c, payload.cond);1832 const type_expr = try renderNode(c, payload.cond);
1830 return renderArrayInit(c, type_expr, payload.cases);1833 return renderArrayInit(c, type_expr, payload.cases);
1831 },1834 },
1835 .vector_zero_init => {
1836 const payload = node.castTag(.vector_zero_init).?.data;
1837 return renderBuiltinCall(c, "@splat", &.{ payload.lhs, payload.rhs });
1838 },
1832 .field_access => {1839 .field_access => {
1833 const payload = node.castTag(.field_access).?.data;1840 const payload = node.castTag(.field_access).?.data;
1834 const lhs = try renderNodeGrouped(c, payload.lhs);1841 const lhs = try renderNodeGrouped(c, payload.lhs);
...@@ -2305,6 +2312,7 @@ fn renderNodeGrouped(c: *Context, node: Node) !NodeIndex {...@@ -2305,6 +2312,7 @@ fn renderNodeGrouped(c: *Context, node: Node) !NodeIndex {
2305 .@"struct",2312 .@"struct",
2306 .@"union",2313 .@"union",
2307 .array_init,2314 .array_init,
2315 .vector_zero_init,
2308 .tuple,2316 .tuple,
2309 .container_init,2317 .container_init,
2310 .container_init_dot,2318 .container_init_dot,
test/run_translated_c.zig+22-22
...@@ -1322,28 +1322,28 @@ pub fn addCases(cases: *tests.RunTranslatedCContext) void {...@@ -1322,28 +1322,28 @@ pub fn addCases(cases: *tests.RunTranslatedCContext) void {
1322 \\}1322 \\}
1323 , "");1323 , "");
13241324
1325 if (@import("builtin").zig_backend == .stage1) {1325 cases.add("basic vector expressions",
1326 // https://github.com/ziglang/zig/issues/122641326 \\#include <stdlib.h>
1327 cases.add("basic vector expressions",1327 \\#include <stdint.h>
1328 \\#include <stdlib.h>1328 \\typedef int16_t __v8hi __attribute__((__vector_size__(16)));
1329 \\#include <stdint.h>1329 \\int main(int argc, char**argv) {
1330 \\typedef int16_t __v8hi __attribute__((__vector_size__(16)));1330 \\ __v8hi uninitialized;
1331 \\int main(int argc, char**argv) {1331 \\ __v8hi empty_init = {};
1332 \\ __v8hi uninitialized;1332 \\ for (int i = 0; i < 8; i++) {
1333 \\ __v8hi empty_init = {};1333 \\ if (empty_init[i] != 0) abort();
1334 \\ __v8hi partial_init = {0, 1, 2, 3};1334 \\ }
1335 \\1335 \\ __v8hi partial_init = {0, 1, 2, 3};
1336 \\ __v8hi a = {0, 1, 2, 3, 4, 5, 6, 7};1336 \\
1337 \\ __v8hi b = (__v8hi) {100, 200, 300, 400, 500, 600, 700, 800};1337 \\ __v8hi a = {0, 1, 2, 3, 4, 5, 6, 7};
1338 \\1338 \\ __v8hi b = (__v8hi) {100, 200, 300, 400, 500, 600, 700, 800};
1339 \\ __v8hi sum = a + b;1339 \\
1340 \\ for (int i = 0; i < 8; i++) {1340 \\ __v8hi sum = a + b;
1341 \\ if (sum[i] != a[i] + b[i]) abort();1341 \\ for (int i = 0; i < 8; i++) {
1342 \\ }1342 \\ if (sum[i] != a[i] + b[i]) abort();
1343 \\ return 0;1343 \\ }
1344 \\}1344 \\ return 0;
1345 , "");1345 \\}
1346 }1346 , "");
13471347
1348 cases.add("__builtin_shufflevector",1348 cases.add("__builtin_shufflevector",
1349 \\#include <stdlib.h>1349 \\#include <stdlib.h>