authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-04-12 21:03:36-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-04-13 02:47:16-04:00
loge2fe1907ecac075e4d4a37776359144318b6055a
treefa8289bc558540dfe7fcfdc0d06eb99f0e189396
parent856a9c2e3120d9ffa1166eed13641600230946da

add c_char type

closes #875

11 files changed, 62 insertions(+), 2 deletions(-)

lib/std/start.zig+2-2
......@@ -512,7 +512,7 @@ fn callMainWithArgs(argc: usize, argv: [*][*:0]u8, envp: [][*:0]u8) u8 {
512512 return initEventLoopAndCallMain();
513513}
514514
515fn main(c_argc: c_int, c_argv: [*][*:0]u8, c_envp: [*:null]?[*:0]u8) callconv(.C) c_int {
515fn main(c_argc: c_int, c_argv: [*][*:0]c_char, c_envp: [*:null]?[*:0]c_char) callconv(.C) c_int {
516516 var env_count: usize = 0;
517517 while (c_envp[env_count] != null) : (env_count += 1) {}
518518 const envp = @ptrCast([*][*:0]u8, c_envp)[0..env_count];
......@@ -527,7 +527,7 @@ fn main(c_argc: c_int, c_argv: [*][*:0]u8, c_envp: [*:null]?[*:0]u8) callconv(.C
527527 return @call(.always_inline, callMainWithArgs, .{ @intCast(usize, c_argc), @ptrCast([*][*:0]u8, c_argv), envp });
528528}
529529
530fn mainWithoutEnv(c_argc: c_int, c_argv: [*][*:0]u8) callconv(.C) c_int {
530fn mainWithoutEnv(c_argc: c_int, c_argv: [*][*:0]c_char) callconv(.C) c_int {
531531 std.os.argv = @ptrCast([*][*:0]u8, c_argv)[0..@intCast(usize, c_argc)];
532532 return @call(.always_inline, callMain, .{});
533533}
lib/std/target.zig+20
......@@ -1905,6 +1905,7 @@ pub const Target = struct {
19051905 }
19061906
19071907 pub const CType = enum {
1908 char,
19081909 short,
19091910 ushort,
19101911 int,
......@@ -1920,6 +1921,7 @@ pub const Target = struct {
19201921
19211922 pub fn c_type_byte_size(t: Target, c_type: CType) u16 {
19221923 return switch (c_type) {
1924 .char,
19231925 .short,
19241926 .ushort,
19251927 .int,
......@@ -1948,21 +1950,25 @@ pub const Target = struct {
19481950 switch (target.os.tag) {
19491951 .freestanding, .other => switch (target.cpu.arch) {
19501952 .msp430 => switch (c_type) {
1953 .char => return 8,
19511954 .short, .ushort, .int, .uint => return 16,
19521955 .float, .long, .ulong => return 32,
19531956 .longlong, .ulonglong, .double, .longdouble => return 64,
19541957 },
19551958 .avr => switch (c_type) {
1959 .char => return 8,
19561960 .short, .ushort, .int, .uint => return 16,
19571961 .long, .ulong, .float, .double, .longdouble => return 32,
19581962 .longlong, .ulonglong => return 64,
19591963 },
19601964 .tce, .tcele => switch (c_type) {
1965 .char => return 8,
19611966 .short, .ushort => return 16,
19621967 .int, .uint, .long, .ulong, .longlong, .ulonglong => return 32,
19631968 .float, .double, .longdouble => return 32,
19641969 },
19651970 .mips64, .mips64el => switch (c_type) {
1971 .char => return 8,
19661972 .short, .ushort => return 16,
19671973 .int, .uint, .float => return 32,
19681974 .long, .ulong => return if (target.abi != .gnuabin32) 64 else 32,
......@@ -1970,6 +1976,7 @@ pub const Target = struct {
19701976 .longdouble => return 128,
19711977 },
19721978 .x86_64 => switch (c_type) {
1979 .char => return 8,
19731980 .short, .ushort => return 16,
19741981 .int, .uint, .float => return 32,
19751982 .long, .ulong => switch (target.abi) {
......@@ -1980,6 +1987,7 @@ pub const Target = struct {
19801987 .longdouble => return 80,
19811988 },
19821989 else => switch (c_type) {
1990 .char => return 8,
19831991 .short, .ushort => return 16,
19841992 .int, .uint, .float => return 32,
19851993 .long, .ulong => return target.cpu.arch.ptrBitWidth(),
......@@ -2036,21 +2044,25 @@ pub const Target = struct {
20362044 .minix,
20372045 => switch (target.cpu.arch) {
20382046 .msp430 => switch (c_type) {
2047 .char => return 8,
20392048 .short, .ushort, .int, .uint => return 16,
20402049 .long, .ulong, .float => return 32,
20412050 .longlong, .ulonglong, .double, .longdouble => return 64,
20422051 },
20432052 .avr => switch (c_type) {
2053 .char => return 8,
20442054 .short, .ushort, .int, .uint => return 16,
20452055 .long, .ulong, .float, .double, .longdouble => return 32,
20462056 .longlong, .ulonglong => return 64,
20472057 },
20482058 .tce, .tcele => switch (c_type) {
2059 .char => return 8,
20492060 .short, .ushort => return 16,
20502061 .int, .uint, .long, .ulong, .longlong, .ulonglong => return 32,
20512062 .float, .double, .longdouble => return 32,
20522063 },
20532064 .mips64, .mips64el => switch (c_type) {
2065 .char => return 8,
20542066 .short, .ushort => return 16,
20552067 .int, .uint, .float => return 32,
20562068 .long, .ulong => return if (target.abi != .gnuabin32) 64 else 32,
......@@ -2058,6 +2070,7 @@ pub const Target = struct {
20582070 .longdouble => if (target.os.tag == .freebsd) return 64 else return 128,
20592071 },
20602072 .x86_64 => switch (c_type) {
2073 .char => return 8,
20612074 .short, .ushort => return 16,
20622075 .int, .uint, .float => return 32,
20632076 .long, .ulong => switch (target.abi) {
......@@ -2068,6 +2081,7 @@ pub const Target = struct {
20682081 .longdouble => return 80,
20692082 },
20702083 else => switch (c_type) {
2084 .char => return 8,
20712085 .short, .ushort => return 16,
20722086 .int, .uint, .float => return 32,
20732087 .long, .ulong => return target.cpu.arch.ptrBitWidth(),
......@@ -2128,6 +2142,7 @@ pub const Target = struct {
21282142
21292143 .windows, .uefi => switch (target.cpu.arch) {
21302144 .x86 => switch (c_type) {
2145 .char => return 8,
21312146 .short, .ushort => return 16,
21322147 .int, .uint, .float => return 32,
21332148 .long, .ulong => return 32,
......@@ -2138,6 +2153,7 @@ pub const Target = struct {
21382153 },
21392154 },
21402155 .x86_64 => switch (c_type) {
2156 .char => return 8,
21412157 .short, .ushort => return 16,
21422158 .int, .uint, .float => return 32,
21432159 .long, .ulong => switch (target.abi) {
......@@ -2151,6 +2167,7 @@ pub const Target = struct {
21512167 },
21522168 },
21532169 else => switch (c_type) {
2170 .char => return 8,
21542171 .short, .ushort => return 16,
21552172 .int, .uint, .float => return 32,
21562173 .long, .ulong => return 32,
......@@ -2160,6 +2177,7 @@ pub const Target = struct {
21602177 },
21612178
21622179 .macos, .ios, .tvos, .watchos => switch (c_type) {
2180 .char => return 8,
21632181 .short, .ushort => return 16,
21642182 .int, .uint, .float => return 32,
21652183 .long, .ulong => switch (target.cpu.arch) {
......@@ -2182,6 +2200,7 @@ pub const Target = struct {
21822200 },
21832201
21842202 .nvcl, .cuda => switch (c_type) {
2203 .char => return 8,
21852204 .short, .ushort => return 16,
21862205 .int, .uint, .float => return 32,
21872206 .long, .ulong => switch (target.cpu.arch) {
......@@ -2194,6 +2213,7 @@ pub const Target = struct {
21942213 },
21952214
21962215 .amdhsa, .amdpal => switch (c_type) {
2216 .char => return 8,
21972217 .short, .ushort => return 16,
21982218 .int, .uint, .float => return 32,
21992219 .long, .ulong, .longlong, .ulonglong, .double => return 64,
lib/std/zig/primitives.zig+1
......@@ -11,6 +11,7 @@ pub const names = std.ComptimeStringMap(void, .{
1111 .{"c_long"},
1212 .{"c_longdouble"},
1313 .{"c_longlong"},
14 .{"c_char"},
1415 .{"c_short"},
1516 .{"c_uint"},
1617 .{"c_ulong"},
src/AstGen.zig+4
......@@ -9054,6 +9054,7 @@ const primitive_instrs = std.ComptimeStringMap(Zir.Inst.Ref, .{
90549054 .{ "c_long", .c_long_type },
90559055 .{ "c_longdouble", .c_longdouble_type },
90569056 .{ "c_longlong", .c_longlong_type },
9057 .{ "c_char", .c_char_type },
90579058 .{ "c_short", .c_short_type },
90589059 .{ "c_uint", .c_uint_type },
90599060 .{ "c_ulong", .c_ulong_type },
......@@ -9802,6 +9803,7 @@ fn nodeImpliesMoreThanOnePossibleValue(tree: *const Ast, start_node: Ast.Node.In
98029803 .c_long_type,
98039804 .c_longdouble_type,
98049805 .c_longlong_type,
9806 .c_char_type,
98059807 .c_short_type,
98069808 .c_uint_type,
98079809 .c_ulong_type,
......@@ -10047,6 +10049,7 @@ fn nodeImpliesComptimeOnly(tree: *const Ast, start_node: Ast.Node.Index) bool {
1004710049 .c_long_type,
1004810050 .c_longdouble_type,
1004910051 .c_longlong_type,
10052 .c_char_type,
1005010053 .c_short_type,
1005110054 .c_uint_type,
1005210055 .c_ulong_type,
......@@ -10187,6 +10190,7 @@ fn rvalue(
1018710190 as_ty | @enumToInt(Zir.Inst.Ref.i64_type),
1018810191 as_ty | @enumToInt(Zir.Inst.Ref.usize_type),
1018910192 as_ty | @enumToInt(Zir.Inst.Ref.isize_type),
10193 as_ty | @enumToInt(Zir.Inst.Ref.c_char_type),
1019010194 as_ty | @enumToInt(Zir.Inst.Ref.c_short_type),
1019110195 as_ty | @enumToInt(Zir.Inst.Ref.c_ushort_type),
1019210196 as_ty | @enumToInt(Zir.Inst.Ref.c_int_type),
src/Sema.zig+3
......@@ -30622,6 +30622,7 @@ pub fn resolveTypeRequiresComptime(sema: *Sema, ty: Type) CompileError!bool {
3062230622 .i128,
3062330623 .usize,
3062430624 .isize,
30625 .c_char,
3062530626 .c_short,
3062630627 .c_ushort,
3062730628 .c_int,
......@@ -32010,6 +32011,7 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value {
3201032011 .i128,
3201132012 .usize,
3201232013 .isize,
32014 .c_char,
3201332015 .c_short,
3201432016 .c_ushort,
3201532017 .c_int,
......@@ -32640,6 +32642,7 @@ pub fn typeRequiresComptime(sema: *Sema, ty: Type) CompileError!bool {
3264032642 .i128,
3264132643 .usize,
3264232644 .isize,
32645 .c_char,
3264332646 .c_short,
3264432647 .c_ushort,
3264532648 .c_int,
src/TypedValue.zig+1
......@@ -92,6 +92,7 @@ pub fn print(
9292 .i128_type => return writer.writeAll("i128"),
9393 .isize_type => return writer.writeAll("isize"),
9494 .usize_type => return writer.writeAll("usize"),
95 .c_char_type => return writer.writeAll("c_char"),
9596 .c_short_type => return writer.writeAll("c_short"),
9697 .c_ushort_type => return writer.writeAll("c_ushort"),
9798 .c_int_type => return writer.writeAll("c_int"),
src/Zir.zig+5
......@@ -2062,6 +2062,7 @@ pub const Inst = struct {
20622062 i128_type,
20632063 usize_type,
20642064 isize_type,
2065 c_char_type,
20652066 c_short_type,
20662067 c_ushort_type,
20672068 c_int_type,
......@@ -2201,6 +2202,10 @@ pub const Inst = struct {
22012202 .ty = Type.initTag(.type),
22022203 .val = Value.initTag(.isize_type),
22032204 },
2205 .c_char_type = .{
2206 .ty = Type.initTag(.type),
2207 .val = Value.initTag(.c_char_type),
2208 },
22042209 .c_short_type = .{
22052210 .ty = Type.initTag(.type),
22062211 .val = Value.initTag(.c_short_type),
src/codegen/c/type.zig+1
......@@ -1358,6 +1358,7 @@ pub const CType = extern union {
13581358 else if (ty.isAbiInt()) switch (ty.tag()) {
13591359 .usize => self.init(.uintptr_t),
13601360 .isize => self.init(.intptr_t),
1361 .c_char => self.init(.char),
13611362 .c_short => self.init(.short),
13621363 .c_ushort => self.init(.@"unsigned short"),
13631364 .c_int => self.init(.int),
src/type.zig+20
......@@ -47,6 +47,7 @@ pub const Type = extern union {
4747 .i128,
4848 .usize,
4949 .isize,
50 .c_char,
5051 .c_short,
5152 .c_ushort,
5253 .c_int,
......@@ -546,6 +547,7 @@ pub const Type = extern union {
546547 // Detect that e.g. u64 != usize, even if the bits match on a particular target.
547548 .usize,
548549 .isize,
550 .c_char,
549551 .c_short,
550552 .c_ushort,
551553 .c_int,
......@@ -946,6 +948,7 @@ pub const Type = extern union {
946948
947949 .usize,
948950 .isize,
951 .c_char,
949952 .c_short,
950953 .c_ushort,
951954 .c_int,
......@@ -1292,6 +1295,7 @@ pub const Type = extern union {
12921295 .i128,
12931296 .usize,
12941297 .isize,
1298 .c_char,
12951299 .c_short,
12961300 .c_ushort,
12971301 .c_int,
......@@ -1584,6 +1588,7 @@ pub const Type = extern union {
15841588 .i128,
15851589 .usize,
15861590 .isize,
1591 .c_char,
15871592 .c_short,
15881593 .c_ushort,
15891594 .c_int,
......@@ -1974,6 +1979,7 @@ pub const Type = extern union {
19741979 .i128,
19751980 .usize,
19761981 .isize,
1982 .c_char,
19771983 .c_short,
19781984 .c_ushort,
19791985 .c_int,
......@@ -2290,6 +2296,7 @@ pub const Type = extern union {
22902296 .i64 => return Value.initTag(.i64_type),
22912297 .usize => return Value.initTag(.usize_type),
22922298 .isize => return Value.initTag(.isize_type),
2299 .c_char => return Value.initTag(.c_char_type),
22932300 .c_short => return Value.initTag(.c_short_type),
22942301 .c_ushort => return Value.initTag(.c_ushort_type),
22952302 .c_int => return Value.initTag(.c_int_type),
......@@ -2376,6 +2383,7 @@ pub const Type = extern union {
23762383 .i128,
23772384 .usize,
23782385 .isize,
2386 .c_char,
23792387 .c_short,
23802388 .c_ushort,
23812389 .c_int,
......@@ -2603,6 +2611,7 @@ pub const Type = extern union {
26032611 .i128,
26042612 .usize,
26052613 .isize,
2614 .c_char,
26062615 .c_short,
26072616 .c_ushort,
26082617 .c_int,
......@@ -2938,6 +2947,7 @@ pub const Type = extern union {
29382947 .anyframe_T,
29392948 => return AbiAlignmentAdvanced{ .scalar = @divExact(target.cpu.arch.ptrBitWidth(), 8) },
29402949
2950 .c_char => return AbiAlignmentAdvanced{ .scalar = target.c_type_alignment(.char) },
29412951 .c_short => return AbiAlignmentAdvanced{ .scalar = target.c_type_alignment(.short) },
29422952 .c_ushort => return AbiAlignmentAdvanced{ .scalar = target.c_type_alignment(.ushort) },
29432953 .c_int => return AbiAlignmentAdvanced{ .scalar = target.c_type_alignment(.int) },
......@@ -3435,6 +3445,7 @@ pub const Type = extern union {
34353445 else => return AbiSizeAdvanced{ .scalar = @divExact(target.cpu.arch.ptrBitWidth(), 8) },
34363446 },
34373447
3448 .c_char => return AbiSizeAdvanced{ .scalar = target.c_type_byte_size(.char) },
34383449 .c_short => return AbiSizeAdvanced{ .scalar = target.c_type_byte_size(.short) },
34393450 .c_ushort => return AbiSizeAdvanced{ .scalar = target.c_type_byte_size(.ushort) },
34403451 .c_int => return AbiSizeAdvanced{ .scalar = target.c_type_byte_size(.int) },
......@@ -3742,6 +3753,7 @@ pub const Type = extern union {
37423753 .manyptr_const_u8_sentinel_0,
37433754 => return target.cpu.arch.ptrBitWidth(),
37443755
3756 .c_char => return target.c_type_bit_size(.char),
37453757 .c_short => return target.c_type_bit_size(.short),
37463758 .c_ushort => return target.c_type_bit_size(.ushort),
37473759 .c_int => return target.c_type_bit_size(.int),
......@@ -4553,6 +4565,7 @@ pub const Type = extern union {
45534565 .int_signed,
45544566 .i8,
45554567 .isize,
4568 .c_char,
45564569 .c_short,
45574570 .c_int,
45584571 .c_long,
......@@ -4625,6 +4638,7 @@ pub const Type = extern union {
46254638 .i128 => return .{ .signedness = .signed, .bits = 128 },
46264639 .usize => return .{ .signedness = .unsigned, .bits = target.cpu.arch.ptrBitWidth() },
46274640 .isize => return .{ .signedness = .signed, .bits = target.cpu.arch.ptrBitWidth() },
4641 .c_char => return .{ .signedness = .signed, .bits = target.c_type_bit_size(.char) },
46284642 .c_short => return .{ .signedness = .signed, .bits = target.c_type_bit_size(.short) },
46294643 .c_ushort => return .{ .signedness = .unsigned, .bits = target.c_type_bit_size(.ushort) },
46304644 .c_int => return .{ .signedness = .signed, .bits = target.c_type_bit_size(.int) },
......@@ -4664,6 +4678,7 @@ pub const Type = extern union {
46644678 return switch (self.tag()) {
46654679 .usize,
46664680 .isize,
4681 .c_char,
46674682 .c_short,
46684683 .c_ushort,
46694684 .c_int,
......@@ -4921,6 +4936,7 @@ pub const Type = extern union {
49214936 .i128,
49224937 .usize,
49234938 .isize,
4939 .c_char,
49244940 .c_short,
49254941 .c_ushort,
49264942 .c_int,
......@@ -4964,6 +4980,7 @@ pub const Type = extern union {
49644980 .i128,
49654981 .usize,
49664982 .isize,
4983 .c_char,
49674984 .c_short,
49684985 .c_ushort,
49694986 .c_int,
......@@ -5155,6 +5172,7 @@ pub const Type = extern union {
51555172 .i128,
51565173 .usize,
51575174 .isize,
5175 .c_char,
51585176 .c_short,
51595177 .c_ushort,
51605178 .c_int,
......@@ -5993,6 +6011,7 @@ pub const Type = extern union {
59936011 i128,
59946012 usize,
59956013 isize,
6014 c_char,
59966015 c_short,
59976016 c_ushort,
59986017 c_int,
......@@ -6118,6 +6137,7 @@ pub const Type = extern union {
61186137 .i128,
61196138 .usize,
61206139 .isize,
6140 .c_char,
61216141 .c_short,
61226142 .c_ushort,
61236143 .c_int,
src/value.zig+5
......@@ -40,6 +40,7 @@ pub const Value = extern union {
4040 i128_type,
4141 usize_type,
4242 isize_type,
43 c_char_type,
4344 c_short_type,
4445 c_ushort_type,
4546 c_int_type,
......@@ -210,6 +211,7 @@ pub const Value = extern union {
210211 .i128_type,
211212 .usize_type,
212213 .isize_type,
214 .c_char_type,
213215 .c_short_type,
214216 .c_ushort_type,
215217 .c_int_type,
......@@ -413,6 +415,7 @@ pub const Value = extern union {
413415 .i128_type,
414416 .usize_type,
415417 .isize_type,
418 .c_char_type,
416419 .c_short_type,
417420 .c_ushort_type,
418421 .c_int_type,
......@@ -679,6 +682,7 @@ pub const Value = extern union {
679682 .i128_type => return out_stream.writeAll("i128"),
680683 .isize_type => return out_stream.writeAll("isize"),
681684 .usize_type => return out_stream.writeAll("usize"),
685 .c_char_type => return out_stream.writeAll("c_char"),
682686 .c_short_type => return out_stream.writeAll("c_short"),
683687 .c_ushort_type => return out_stream.writeAll("c_ushort"),
684688 .c_int_type => return out_stream.writeAll("c_int"),
......@@ -919,6 +923,7 @@ pub const Value = extern union {
919923 .i128_type => Type.initTag(.i128),
920924 .usize_type => Type.initTag(.usize),
921925 .isize_type => Type.initTag(.isize),
926 .c_char_type => Type.initTag(.c_char),
922927 .c_short_type => Type.initTag(.c_short),
923928 .c_ushort_type => Type.initTag(.c_ushort),
924929 .c_int_type => Type.initTag(.c_int),
stage1/zig1.wasm
Binary files a/stage1/zig1.wasm and b/stage1/zig1.wasm differ