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(
26882688) TransError!Node {
26892689 _ = ty;
26902690 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
26922693 const init_count = expr.getNumInits();
2694 const num_elements = vector_ty.getNumElements();
2695 const element_qt = vector_ty.getElementType();
26932696
26942697 if (init_count == 0) {
2695 return Tag.container_init.create(c.arena, .{
2696 .lhs = vector_type,
2697 .inits = try c.arena.alloc(ast.Payload.ContainerInit.Initializer, 0),
2698 const zero_node = try Tag.as.create(c.arena, .{
2699 .lhs = try transQualType(c, scope, element_qt, loc),
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,
26982706 });
26992707 }
27002708
2709 const vector_type = try transQualType(c, scope, qt, loc);
2710
27012711 var block_scope = try Scope.Block.init(c, scope, true);
27022712 defer block_scope.deinit();
27032713
......@@ -2716,11 +2726,15 @@ fn transInitListExprVector(
27162726 try block_scope.statements.append(tmp_decl_node);
27172727 }
27182728
2719 const init_list = try c.arena.alloc(Node, init_count);
2729 const init_list = try c.arena.alloc(Node, num_elements);
27202730 for (init_list) |*init, init_index| {
2721 const tmp_decl = block_scope.statements.items[init_index];
2722 const name = tmp_decl.castTag(.var_simple).?.data.name;
2723 init.* = try Tag.identifier.create(c.arena, name);
2731 if (init_index < init_count) {
2732 const tmp_decl = block_scope.statements.items[init_index];
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 }
27242738 }
27252739
27262740 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 {
154154 div_exact,
155155 /// @offsetOf(lhs, rhs)
156156 offset_of,
157 /// @splat(lhs, rhs)
158 vector_zero_init,
157159 /// @shuffle(type, a, b, mask)
158160 shuffle,
159161
......@@ -328,6 +330,7 @@ pub const Node = extern union {
328330 .div_exact,
329331 .offset_of,
330332 .helpers_cast,
333 .vector_zero_init,
331334 => Payload.BinOp,
332335
333336 .integer_literal,
......@@ -1829,6 +1832,10 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
18291832 const type_expr = try renderNode(c, payload.cond);
18301833 return renderArrayInit(c, type_expr, payload.cases);
18311834 },
1835 .vector_zero_init => {
1836 const payload = node.castTag(.vector_zero_init).?.data;
1837 return renderBuiltinCall(c, "@splat", &.{ payload.lhs, payload.rhs });
1838 },
18321839 .field_access => {
18331840 const payload = node.castTag(.field_access).?.data;
18341841 const lhs = try renderNodeGrouped(c, payload.lhs);
......@@ -2305,6 +2312,7 @@ fn renderNodeGrouped(c: *Context, node: Node) !NodeIndex {
23052312 .@"struct",
23062313 .@"union",
23072314 .array_init,
2315 .vector_zero_init,
23082316 .tuple,
23092317 .container_init,
23102318 .container_init_dot,
test/run_translated_c.zig+22-22
......@@ -1322,28 +1322,28 @@ pub fn addCases(cases: *tests.RunTranslatedCContext) void {
13221322 \\}
13231323 , "");
13241324
1325 if (@import("builtin").zig_backend == .stage1) {
1326 // https://github.com/ziglang/zig/issues/12264
1327 cases.add("basic vector expressions",
1328 \\#include <stdlib.h>
1329 \\#include <stdint.h>
1330 \\typedef int16_t __v8hi __attribute__((__vector_size__(16)));
1331 \\int main(int argc, char**argv) {
1332 \\ __v8hi uninitialized;
1333 \\ __v8hi empty_init = {};
1334 \\ __v8hi partial_init = {0, 1, 2, 3};
1335 \\
1336 \\ __v8hi a = {0, 1, 2, 3, 4, 5, 6, 7};
1337 \\ __v8hi b = (__v8hi) {100, 200, 300, 400, 500, 600, 700, 800};
1338 \\
1339 \\ __v8hi sum = a + b;
1340 \\ for (int i = 0; i < 8; i++) {
1341 \\ if (sum[i] != a[i] + b[i]) abort();
1342 \\ }
1343 \\ return 0;
1344 \\}
1345 , "");
1346 }
1325 cases.add("basic vector expressions",
1326 \\#include <stdlib.h>
1327 \\#include <stdint.h>
1328 \\typedef int16_t __v8hi __attribute__((__vector_size__(16)));
1329 \\int main(int argc, char**argv) {
1330 \\ __v8hi uninitialized;
1331 \\ __v8hi empty_init = {};
1332 \\ for (int i = 0; i < 8; i++) {
1333 \\ if (empty_init[i] != 0) abort();
1334 \\ }
1335 \\ __v8hi partial_init = {0, 1, 2, 3};
1336 \\
1337 \\ __v8hi a = {0, 1, 2, 3, 4, 5, 6, 7};
1338 \\ __v8hi b = (__v8hi) {100, 200, 300, 400, 500, 600, 700, 800};
1339 \\
1340 \\ __v8hi sum = a + b;
1341 \\ for (int i = 0; i < 8; i++) {
1342 \\ if (sum[i] != a[i] + b[i]) abort();
1343 \\ }
1344 \\ return 0;
1345 \\}
1346 , "");
13471347
13481348 cases.add("__builtin_shufflevector",
13491349 \\#include <stdlib.h>