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

add ptr_cast builtin #16

Merged
merged 1 commit into from
Jan 10, 2024
Merged
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
1 change: 1 addition & 0 deletions bootstrap/ast.zig
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ pub const BuiltinFunction = enum {
int_to_ptr,
is_pointer,
ptr_to_int,
ptr_cast,
size_of,
syscall,
This,
Expand Down
17 changes: 17 additions & 0 deletions bootstrap/sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,23 @@ fn semaASTExpr(
.value_type = .pointer_int_type,
}});
},
.ptr_cast => {
const type_arg = ast.expressions.getOpt(curr_ast_arg).?.function_argument;
const expr_arg = ast.expressions.getOpt(type_arg.next).?.function_argument;
std.debug.assert(expr_arg.next == .none);

const ty = try semaASTExpr(scope_idx, type_arg.value, true, .type, null);
std.debug.assert(types.get(values.get(ty).type_idx).* == .pointer);
const value = try semaASTExpr(scope_idx, expr_arg.value, false, null, null);

if(force_comptime_eval) @panic("TODO: Comptime ptr_cast");
break :outer values.insert(.{.runtime = .{
.expr = ExpressionIndex.toOpt(expressions.insert(.{
.value = value,
})),
.value_type = ty,
}});
},
.truncate => {
const type_arg = ast.expressions.getOpt(curr_ast_arg).?.function_argument;
const expr_arg = ast.expressions.getOpt(type_arg.next).?.function_argument;
Expand Down
6 changes: 3 additions & 3 deletions std/mem.n
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ fn set(dest: *u8, value: u8, len: u64) void {
}

fn output(comptime T: type, ptr: *const T, comptime len: u64) inline |out_buf| [len]T {
copy(@int_to_ptr(*u8, @ptr_to_int(out_buf)), @int_to_ptr(*const u8, @ptr_to_int(ptr)), len * @size_of(T));
copy(@ptr_cast(*u8, out_buf), @ptr_cast(*const u8, ptr), len * @size_of(T));
}

fn output_value(comptime T: type, ptr: *const T) inline |out_buf| T {
copy(@int_to_ptr(*u8, @ptr_to_int(out_buf)), @int_to_ptr(*const u8, @ptr_to_int(ptr)), @size_of(T));
copy(@ptr_cast(*u8, out_buf), @ptr_cast(*const u8, ptr), @size_of(T));
}

fn zeroes(comptime T: type, comptime len: u64) inline |out_buf| [len]T {
set(@int_to_ptr(*u8, @ptr_to_int(out_buf)), 0, len * @size_of(T));
set(@ptr_cast(*u8, out_buf), 0, len * @size_of(T));
}

fn equals(comptime T: type, lhs: *const T, rhs: *const T, len: u64) bool {
Expand Down
10 changes: 5 additions & 5 deletions std/os.n
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,28 @@ fn fd_path(fd: u32) |out_buf| [MAX_PATH]u8 {

fn dir_name(path: *const u8) |out_buf| [MAX_PATH]u8 {
if (path.* == 0) {
std.mem.copy(@int_to_ptr(*u8, @ptr_to_int(out_buf)), ".".&, 2);
std.mem.copy(@ptr_cast(*u8, out_buf), ".".&, 2);
return;
}
var i = string.len(path) - 1;
while(path[i] == '/') {
i -= 1;
}
if(i == 0) {
std.mem.copy(@int_to_ptr(*u8, @ptr_to_int(out_buf)), "/".&, 2);
std.mem.copy(@ptr_cast(*u8, out_buf), "/".&, 2);
return;
}
while(path[i] != '/') {
i -= 1;
}
if(i == 0) {
std.mem.copy(@int_to_ptr(*u8, @ptr_to_int(out_buf)), ".".&, 2);
std.mem.copy(@ptr_cast(*u8, out_buf), ".".&, 2);
return;
}
while(path[i] == '/') {
i -= 1;
}
std.mem.copy(@int_to_ptr(*u8, @ptr_to_int(out_buf)), path, i + 1);
std.mem.copy(@ptr_cast(*u8, out_buf), path, i + 1);
out_buf.*[i + 1] = 0;
return;
}
Expand All @@ -49,5 +49,5 @@ fn self_path() |out_buf| [MAX_PATH]u8 {

fn self_dir() |out_buf| [MAX_PATH]u8 {
const self = self_path();
out_buf.* = dir_name(@int_to_ptr(@ptr_to_int(self.&)));
out_buf.* = dir_name(@ptr_cast(*const u8, self.&));
}
2 changes: 1 addition & 1 deletion std/print.n
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn char(ch: u8) void {

fn unsigned_decimal(value: u64) void {
const buf = std.string.write_u64_decimal(value);
str(@int_to_ptr(*const u8, @ptr_to_int(buf.&)));
str(@ptr_cast(*const u8, buf.&));
}

fn unsigned_hex_upper(value: u64) void {
Expand Down
4 changes: 2 additions & 2 deletions std/string.n
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ fn write_u64_decimal(value: u64) |out_buf| [21]u8 {
length += 1;
if(value == 0) {
out_buf.*[length] = 0;
mem.reverse(u8, @int_to_ptr(*u8, @ptr_to_int(out_buf)), length);
mem.reverse(u8, @ptr_cast(*u8, out_buf), length);
return;
}
}
}

fn write_u32_decimal(value: u32) inline |out_buf| [11]u8 {
@int_to_ptr(*[21]u8, @ptr_to_int(out_buf)).* = write_u64_decimal(value);
@ptr_cast(*[21]u8, out_buf).* = write_u64_decimal(value);
}

fn len(string: *const u8) u64 {
Expand Down
2 changes: 1 addition & 1 deletion tests/std.mem.output/main.n
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const test_lib = @import("../test_lib.n");

fn main() noreturn {
const string = std.mem.output(u8, "Hello, world!\n".&, 15);
std.print.str(@int_to_ptr(*u8, @ptr_to_int(string.&)));
std.print.str(@ptr_cast(*u8, string.&));
test_lib.expect_unflushed_output("Hello, world!\n".&, 14);
test_lib.pass_test();
}