Skip to content

Commit 72ddccb

Browse files
committed
Revert "Merge pull request #13914 from Vexu/variadic"
This reverts commit aca9c74, reversing changes made to d93edad. These changes were not tested with LLVM with assertions enabled. They caused a regression in master branch, triggering LLVM assertions when running behavior tests. Reopens #515
1 parent c2db646 commit 72ddccb

25 files changed

+25
-660
lines changed

doc/langref.html.in

Lines changed: 6 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -8137,35 +8137,6 @@ test "main" {
81378137
{#see_also|Import from C Header File|@cImport|@cDefine|@cInclude#}
81388138
{#header_close#}
81398139

8140-
{#header_open|@cVaArg#}
8141-
<pre>{#syntax#}@cVaArg(operand: *std.builtin.VaList, comptime T: type) T{#endsyntax#}</pre>
8142-
<p>
8143-
Implements the C macro {#syntax#}va_arg{#endsyntax#}.
8144-
</p>
8145-
{#see_also|@cVaCopy|@cVaEnd|@cVaStart#}
8146-
{#header_close#}
8147-
{#header_open|@cVaCopy#}
8148-
<pre>{#syntax#}@cVaCopy(src: *std.builtin.VaList) std.builtin.VaList{#endsyntax#}</pre>
8149-
<p>
8150-
Implements the C macro {#syntax#}va_copy{#endsyntax#}.
8151-
</p>
8152-
{#see_also|@cVaArg|@cVaEnd|@cVaStart#}
8153-
{#header_close#}
8154-
{#header_open|@cVaEnd#}
8155-
<pre>{#syntax#}@cVaEnd(src: *std.builtin.VaList) void{#endsyntax#}</pre>
8156-
<p>
8157-
Implements the C macro {#syntax#}va_end{#endsyntax#}.
8158-
</p>
8159-
{#see_also|@cVaArg|@cVaCopy|@cVaStart#}
8160-
{#header_close#}
8161-
{#header_open|@cVaStart#}
8162-
<pre>{#syntax#}@cVaStart() std.builtin.VaList{#endsyntax#}</pre>
8163-
<p>
8164-
Implements the C macro {#syntax#}va_start{#endsyntax#}. Only valid inside a variadic function.
8165-
</p>
8166-
{#see_also|@cVaArg|@cVaCopy|@cVaEnd#}
8167-
{#header_close#}
8168-
81698140
{#header_open|@divExact#}
81708141
<pre>{#syntax#}@divExact(numerator: T, denominator: T) T{#endsyntax#}</pre>
81718142
<p>
@@ -10874,32 +10845,14 @@ test "variadic function" {
1087410845
}
1087510846
{#code_end#}
1087610847
<p>
10877-
Variadic functions can be implemented using {#link|@cVaStart#}, {#link|@cVaEnd#}, {#link|@cVaArg#} and {#link|@cVaCopy#}
10848+
Non extern variadic functions are currently not implemented, but there
10849+
is an accepted proposal. See <a href="https://github.com/ziglang/zig/issues/515">#515</a>.
1087810850
</p>
10879-
{#code_begin|test|defining_variadic_function#}
10880-
const std = @import("std");
10881-
const testing = std.testing;
10882-
const builtin = @import("builtin");
10851+
{#code_begin|obj_err|non-extern function is variadic#}
10852+
export fn printf(format: [*:0]const u8, ...) c_int {
10853+
_ = format;
1088310854

10884-
fn add(count: c_int, ...) callconv(.C) c_int {
10885-
var ap = @cVaStart();
10886-
defer @cVaEnd(&ap);
10887-
var i: usize = 0;
10888-
var sum: c_int = 0;
10889-
while (i < count) : (i += 1) {
10890-
sum += @cVaArg(&ap, c_int);
10891-
}
10892-
return sum;
10893-
}
10894-
10895-
test "defining a variadic function" {
10896-
// Variadic functions are currently disabled on some targets due to miscompilations.
10897-
if (builtin.cpu.arch == .aarch64 and builtin.os.tag != .windows and builtin.os.tag != .macos) return error.SkipZigTest;
10898-
if (builtin.cpu.arch == .x86_64 and builtin.os.tag == .windows) return error.SkipZigTest;
10899-
10900-
try std.testing.expectEqual(@as(c_int, 0), add(0));
10901-
try std.testing.expectEqual(@as(c_int, 1), add(1, @as(c_int, 1)));
10902-
try std.testing.expectEqual(@as(c_int, 3), add(2, @as(c_int, 1), @as(c_int, 2)));
10855+
return 0;
1090310856
}
1090410857
{#code_end#}
1090510858
{#header_close#}

lib/std/builtin.zig

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -620,87 +620,6 @@ pub const CallModifier = enum {
620620
compile_time,
621621
};
622622

623-
/// This data structure is used by the Zig language code generation and
624-
/// therefore must be kept in sync with the compiler implementation.
625-
pub const VaListAarch64 = extern struct {
626-
__stack: *anyopaque,
627-
__gr_top: *anyopaque,
628-
__vr_top: *anyopaque,
629-
__gr_offs: c_int,
630-
__vr_offs: c_int,
631-
};
632-
633-
/// This data structure is used by the Zig language code generation and
634-
/// therefore must be kept in sync with the compiler implementation.
635-
pub const VaListHexagon = extern struct {
636-
__gpr: c_long,
637-
__fpr: c_long,
638-
__overflow_arg_area: *anyopaque,
639-
__reg_save_area: *anyopaque,
640-
};
641-
642-
/// This data structure is used by the Zig language code generation and
643-
/// therefore must be kept in sync with the compiler implementation.
644-
pub const VaListPowerPc = extern struct {
645-
gpr: u8,
646-
fpr: u8,
647-
reserved: c_ushort,
648-
overflow_arg_area: *anyopaque,
649-
reg_save_area: *anyopaque,
650-
};
651-
652-
/// This data structure is used by the Zig language code generation and
653-
/// therefore must be kept in sync with the compiler implementation.
654-
pub const VaListS390x = extern struct {
655-
__current_saved_reg_area_pointer: *anyopaque,
656-
__saved_reg_area_end_pointer: *anyopaque,
657-
__overflow_area_pointer: *anyopaque,
658-
};
659-
660-
/// This data structure is used by the Zig language code generation and
661-
/// therefore must be kept in sync with the compiler implementation.
662-
pub const VaListX86_64 = extern struct {
663-
gp_offset: c_uint,
664-
fp_offset: c_uint,
665-
overflow_arg_area: *anyopaque,
666-
reg_save_area: *anyopaque,
667-
};
668-
669-
/// This data structure is used by the Zig language code generation and
670-
/// therefore must be kept in sync with the compiler implementation.
671-
pub const VaList = switch (builtin.cpu.arch) {
672-
.aarch64 => switch (builtin.os.tag) {
673-
.windows => *u8,
674-
.ios, .macos, .tvos, .watchos => *u8,
675-
else => @compileError("disabled due to miscompilations"), // VaListAarch64,
676-
},
677-
.arm => switch (builtin.os.tag) {
678-
.ios, .macos, .tvos, .watchos => *u8,
679-
else => *anyopaque,
680-
},
681-
.amdgcn => *u8,
682-
.avr => *anyopaque,
683-
.bpfel, .bpfeb => *anyopaque,
684-
.hexagon => if (builtin.target.isMusl()) VaListHexagon else *u8,
685-
.mips, .mipsel, .mips64, .mips64el => *anyopaque,
686-
.riscv32, .riscv64 => *anyopaque,
687-
.powerpc, .powerpcle => switch (builtin.os.tag) {
688-
.ios, .macos, .tvos, .watchos, .aix => *u8,
689-
else => VaListPowerPc,
690-
},
691-
.powerpc64, .powerpc64le => *u8,
692-
.sparc, .sparcel, .sparc64 => *anyopaque,
693-
.spirv32, .spirv64 => *anyopaque,
694-
.s390x => VaListS390x,
695-
.wasm32, .wasm64 => *anyopaque,
696-
.x86 => *u8,
697-
.x86_64 => switch (builtin.os.tag) {
698-
.windows => @compileError("disabled due to miscompilations"), // *u8,
699-
else => VaListX86_64,
700-
},
701-
else => @compileError("VaList not supported for this target yet"),
702-
};
703-
704623
/// This data structure is used by the Zig language code generation and
705624
/// therefore must be kept in sync with the compiler implementation.
706625
pub const PrefetchOptions = struct {

src/Air.zig

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -741,19 +741,6 @@ pub const Inst = struct {
741741
/// Uses the `vector_store_elem` field.
742742
vector_store_elem,
743743

744-
/// Implements @cVaArg builtin.
745-
/// Uses the `ty_op` field.
746-
c_va_arg,
747-
/// Implements @cVaCopy builtin.
748-
/// Uses the `ty_op` field.
749-
c_va_copy,
750-
/// Implements @cVaEnd builtin.
751-
/// Uses the `un_op` field.
752-
c_va_end,
753-
/// Implements @cVaStart builtin.
754-
/// Uses the `ty` field.
755-
c_va_start,
756-
757744
pub fn fromCmpOp(op: std.math.CompareOperator, optimized: bool) Tag {
758745
switch (op) {
759746
.lt => return if (optimized) .cmp_lt_optimized else .cmp_lt,
@@ -1105,7 +1092,6 @@ pub fn typeOfIndex(air: Air, inst: Air.Inst.Index) Type {
11051092
.ret_ptr,
11061093
.arg,
11071094
.err_return_trace,
1108-
.c_va_start,
11091095
=> return datas[inst].ty,
11101096

11111097
.assembly,
@@ -1170,8 +1156,6 @@ pub fn typeOfIndex(air: Air, inst: Air.Inst.Index) Type {
11701156
.byte_swap,
11711157
.bit_reverse,
11721158
.addrspace_cast,
1173-
.c_va_arg,
1174-
.c_va_copy,
11751159
=> return air.getRefType(datas[inst].ty_op.ty),
11761160

11771161
.loop,
@@ -1203,7 +1187,6 @@ pub fn typeOfIndex(air: Air, inst: Air.Inst.Index) Type {
12031187
.prefetch,
12041188
.set_err_return_trace,
12051189
.vector_store_elem,
1206-
.c_va_end,
12071190
=> return Type.void,
12081191

12091192
.ptrtoint,

src/AstGen.zig

Lines changed: 6 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ string_table: std.HashMapUnmanaged(u32, void, StringIndexContext, std.hash_map.d
4242
compile_errors: ArrayListUnmanaged(Zir.Inst.CompileErrors.Item) = .{},
4343
/// The topmost block of the current function.
4444
fn_block: ?*GenZir = null,
45-
fn_var_args: bool = false,
4645
/// Maps string table indexes to the first `@import` ZIR instruction
4746
/// that uses this string as the operand.
4847
imports: std.AutoArrayHashMapUnmanaged(u32, Ast.TokenIndex) = .{},
@@ -3891,17 +3890,17 @@ fn fnDecl(
38913890
.noalias_bits = noalias_bits,
38923891
});
38933892
} else func: {
3893+
if (is_var_args) {
3894+
return astgen.failTok(fn_proto.ast.fn_token, "non-extern function is variadic", .{});
3895+
}
3896+
38943897
// as a scope, fn_gz encloses ret_gz, but for instruction list, fn_gz stacks on ret_gz
38953898
fn_gz.instructions_top = ret_gz.instructions.items.len;
38963899

38973900
const prev_fn_block = astgen.fn_block;
38983901
astgen.fn_block = &fn_gz;
38993902
defer astgen.fn_block = prev_fn_block;
39003903

3901-
const prev_var_args = astgen.fn_var_args;
3902-
astgen.fn_var_args = is_var_args;
3903-
defer astgen.fn_var_args = prev_var_args;
3904-
39053904
astgen.advanceSourceCursorToNode(body_node);
39063905
const lbrace_line = astgen.source_line - decl_gz.decl_line;
39073906
const lbrace_column = astgen.source_column;
@@ -6070,15 +6069,15 @@ fn whileExpr(
60706069
const tag: Zir.Inst.Tag = if (payload_is_ref) .is_non_err_ptr else .is_non_err;
60716070
break :c .{
60726071
.inst = err_union,
6073-
.bool_bit = try cond_scope.addUnNode(tag, err_union, while_full.ast.cond_expr),
6072+
.bool_bit = try cond_scope.addUnNode(tag, err_union, while_full.ast.then_expr),
60746073
};
60756074
} else if (while_full.payload_token) |_| {
60766075
const cond_ri: ResultInfo = .{ .rl = if (payload_is_ref) .ref else .none };
60776076
const optional = try expr(&cond_scope, &cond_scope.base, cond_ri, while_full.ast.cond_expr);
60786077
const tag: Zir.Inst.Tag = if (payload_is_ref) .is_non_null_ptr else .is_non_null;
60796078
break :c .{
60806079
.inst = optional,
6081-
.bool_bit = try cond_scope.addUnNode(tag, optional, while_full.ast.cond_expr),
6080+
.bool_bit = try cond_scope.addUnNode(tag, optional, while_full.ast.then_expr),
60826081
};
60836082
} else {
60846083
const cond = try expr(&cond_scope, &cond_scope.base, bool_ri, while_full.ast.cond_expr);
@@ -8369,46 +8368,6 @@ fn builtinCall(
83698368
});
83708369
return rvalue(gz, ri, result, node);
83718370
},
8372-
.c_va_arg => {
8373-
if (astgen.fn_block == null) {
8374-
return astgen.failNode(node, "'@cVaArg' outside function scope", .{});
8375-
}
8376-
const result = try gz.addExtendedPayload(.c_va_arg, Zir.Inst.BinNode{
8377-
.node = gz.nodeIndexToRelative(node),
8378-
.lhs = try expr(gz, scope, .{ .rl = .none }, params[0]),
8379-
.rhs = try typeExpr(gz, scope, params[1]),
8380-
});
8381-
return rvalue(gz, ri, result, node);
8382-
},
8383-
.c_va_copy => {
8384-
if (astgen.fn_block == null) {
8385-
return astgen.failNode(node, "'@cVaCopy' outside function scope", .{});
8386-
}
8387-
const result = try gz.addExtendedPayload(.c_va_copy, Zir.Inst.UnNode{
8388-
.node = gz.nodeIndexToRelative(node),
8389-
.operand = try expr(gz, scope, .{ .rl = .none }, params[0]),
8390-
});
8391-
return rvalue(gz, ri, result, node);
8392-
},
8393-
.c_va_end => {
8394-
if (astgen.fn_block == null) {
8395-
return astgen.failNode(node, "'@cVaEnd' outside function scope", .{});
8396-
}
8397-
const result = try gz.addExtendedPayload(.c_va_end, Zir.Inst.UnNode{
8398-
.node = gz.nodeIndexToRelative(node),
8399-
.operand = try expr(gz, scope, .{ .rl = .none }, params[0]),
8400-
});
8401-
return rvalue(gz, ri, result, node);
8402-
},
8403-
.c_va_start => {
8404-
if (astgen.fn_block == null) {
8405-
return astgen.failNode(node, "'@cVaStart' outside function scope", .{});
8406-
}
8407-
if (!astgen.fn_var_args) {
8408-
return astgen.failNode(node, "'@cVaStart' in a non-variadic function", .{});
8409-
}
8410-
return rvalue(gz, ri, try gz.addNodeExtended(.c_va_start, node), node);
8411-
},
84128371
}
84138372
}
84148373

src/BuiltinFn.zig

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ pub const Tag = enum {
3030
compile_log,
3131
ctz,
3232
c_undef,
33-
c_va_arg,
34-
c_va_copy,
35-
c_va_end,
36-
c_va_start,
3733
div_exact,
3834
div_floor,
3935
div_trunc,
@@ -358,30 +354,6 @@ pub const list = list: {
358354
.param_count = 1,
359355
},
360356
},
361-
.{
362-
"@cVaArg", .{
363-
.tag = .c_va_arg,
364-
.param_count = 2,
365-
},
366-
},
367-
.{
368-
"@cVaCopy", .{
369-
.tag = .c_va_copy,
370-
.param_count = 1,
371-
},
372-
},
373-
.{
374-
"@cVaEnd", .{
375-
.tag = .c_va_end,
376-
.param_count = 1,
377-
},
378-
},
379-
.{
380-
"@cVaStart", .{
381-
.tag = .c_va_start,
382-
.param_count = 0,
383-
},
384-
},
385357
.{
386358
"@divExact",
387359
.{

src/Liveness.zig

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,6 @@ pub fn categorizeOperand(
238238
.wasm_memory_size,
239239
.err_return_trace,
240240
.save_err_return_trace_index,
241-
.c_va_start,
242241
=> return .none,
243242

244243
.fence => return .write,
@@ -280,8 +279,6 @@ pub fn categorizeOperand(
280279
.splat,
281280
.error_set_has_value,
282281
.addrspace_cast,
283-
.c_va_arg,
284-
.c_va_copy,
285282
=> {
286283
const o = air_datas[inst].ty_op;
287284
if (o.operand == operand_ref) return matchOperandSmallIndex(l, inst, 0, .none);
@@ -325,7 +322,6 @@ pub fn categorizeOperand(
325322
.trunc_float,
326323
.neg,
327324
.cmp_lt_errors_len,
328-
.c_va_end,
329325
=> {
330326
const o = air_datas[inst].un_op;
331327
if (o == operand_ref) return matchOperandSmallIndex(l, inst, 0, .none);
@@ -861,7 +857,6 @@ fn analyzeInst(
861857
.wasm_memory_size,
862858
.err_return_trace,
863859
.save_err_return_trace_index,
864-
.c_va_start,
865860
=> return trackOperands(a, new_set, inst, main_tomb, .{ .none, .none, .none }),
866861

867862
.not,
@@ -903,8 +898,6 @@ fn analyzeInst(
903898
.splat,
904899
.error_set_has_value,
905900
.addrspace_cast,
906-
.c_va_arg,
907-
.c_va_copy,
908901
=> {
909902
const o = inst_datas[inst].ty_op;
910903
return trackOperands(a, new_set, inst, main_tomb, .{ o.operand, .none, .none });
@@ -943,7 +936,6 @@ fn analyzeInst(
943936
.neg_optimized,
944937
.cmp_lt_errors_len,
945938
.set_err_return_trace,
946-
.c_va_end,
947939
=> {
948940
const operand = inst_datas[inst].un_op;
949941
return trackOperands(a, new_set, inst, main_tomb, .{ operand, .none, .none });

0 commit comments

Comments
 (0)