Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

translate-c: correctly translate pointers to cv-qualified values #23394

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/compiler/aro_translate_c/ast.zig
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ pub const Node = extern union {
array_type,
null_sentinel_array_type,

/// @import("std").zig.c_translation.Volatile(operand)
helpers_volatile,
/// @import("std").zig.c_translation.sizeof(operand)
helpers_sizeof,
/// @import("std").zig.c_translation.FlexibleArrayType(lhs, rhs)
Expand Down Expand Up @@ -286,6 +288,7 @@ pub const Node = extern union {
.const_cast,
.volatile_cast,
.vector_zero_init,
.helpers_volatile,
=> Payload.UnOp,

.add,
Expand Down Expand Up @@ -935,6 +938,11 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
const import_node = try renderStdImport(c, &.{ "zig", "c_translation", "shuffleVectorIndex" });
return renderCall(c, import_node, &.{ payload.lhs, payload.rhs });
},
.helpers_volatile => {
const payload = node.castTag(.helpers_volatile).?.data;
const import_node = try renderStdImport(c, &.{ "zig", "c_translation", "Volatile" });
return renderCall(c, import_node, &.{payload});
},
.vector => {
const payload = node.castTag(.vector).?.data;
return renderBuiltinCall(c, "@Vector", &.{ payload.lhs, payload.rhs });
Expand Down Expand Up @@ -2356,6 +2364,7 @@ fn renderNodeGrouped(c: *Context, node: Node) !NodeIndex {
.helpers_promoteIntLiteral,
.helpers_shuffle_vector_index,
.helpers_flexible_array_type,
.helpers_volatile,
.std_mem_zeroinit,
.integer_literal,
.float_literal,
Expand Down
36 changes: 36 additions & 0 deletions lib/std/zig/c_translation.zig
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,42 @@ test "Flexible Array Type" {
try testing.expectEqual(FlexibleArrayType(*const volatile Container, c_int), [*c]const volatile c_int);
}

pub fn Volatile(comptime T: type) type {
return extern struct {
inner: T = std.mem.zeroes(T),

pub inline fn ptr(v: *volatile Volatile(T)) *volatile T {
return @ptrCast(v);
}

pub inline fn constPtr(v: *const volatile Volatile(T)) *const volatile T {
return @ptrCast(v);
}

pub inline fn load(v: *const volatile Volatile(T)) T {
return v.constPtr().*;
}

pub inline fn store(v: *volatile Volatile(T), value: T) void {
v.ptr().* = value;
}
};
}

test "Volatile" {
try testing.expectEqual(@sizeOf(Volatile(c_int)), @sizeOf(c_int));
try testing.expectEqual(@alignOf(Volatile(c_int)), @alignOf(c_int));

try testing.expectEqual(@sizeOf([7]Volatile(c_int)), @sizeOf([7]c_int));
try testing.expectEqual(@alignOf([7]Volatile(c_int)), @alignOf([7]c_int));

try testing.expectEqual(@sizeOf(Volatile(u32)), @sizeOf(u32));
try testing.expectEqual(@alignOf(Volatile(u32)), @alignOf(u32));

try testing.expectEqual(@sizeOf(Volatile(u64)), @sizeOf(u64));
try testing.expectEqual(@alignOf(Volatile(u64)), @alignOf(u64));
}

/// C `%` operator for signed integers
/// C standard states: "If the quotient a/b is representable, the expression (a/b)*b + a%b shall equal a"
/// The quotient is not representable if denominator is zero, or if numerator is the minimum integer for
Expand Down
3 changes: 3 additions & 0 deletions src/clang.zig
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ pub const QualType = extern struct {
pub const isVolatileQualified = ZigClangQualType_isVolatileQualified;
extern fn ZigClangQualType_isVolatileQualified(QualType) bool;

pub const isLocalVolatileQualified = ZigClangQualType_isLocalVolatileQualified;
extern fn ZigClangQualType_isLocalVolatileQualified(QualType) bool;

pub const isRestrictQualified = ZigClangQualType_isRestrictQualified;
extern fn ZigClangQualType_isRestrictQualified(QualType) bool;
};
Expand Down
Loading
Loading