hello world

This commit is contained in:
David Gonzalez Martin 2024-02-02 08:45:52 +01:00
parent ce83fdc4cf
commit 40734d52ee
17 changed files with 2873 additions and 1072 deletions

File diff suppressed because it is too large Load Diff

View File

@ -288,11 +288,16 @@ extern "C" BasicBlock* NativityLLVMCreateBasicBlock(LLVMContext& context, const
return basic_block; return basic_block;
} }
extern "C" PHINode* NativityLLVMCreatePhiNode(Type* type, unsigned reserved_value_count, const char* name_ptr, size_t name_len, BasicBlock* basic_block) extern "C" PHINode* NativityLLVMBuilderCreatePhi(IRBuilder<> builder, Type* type, unsigned reserved_value_count, const char* name_ptr, size_t name_len)
{ {
auto name = StringRef(name_ptr, name_len); auto name = StringRef(name_ptr, name_len);
auto* phi_node = PHINode::Create(type, reserved_value_count, name, basic_block); auto* phi = builder.CreatePHI(type, reserved_value_count, name);
return phi_node; return phi;
}
extern "C" void NativityLLVMPhiAddIncoming(PHINode& node, Value* value, BasicBlock* basic_block)
{
node.addIncoming(value, basic_block);
} }
extern "C" void NativityLLVMBasicBlockRemoveFromParent(BasicBlock* basic_block) extern "C" void NativityLLVMBasicBlockRemoveFromParent(BasicBlock* basic_block)
@ -317,6 +322,12 @@ extern "C" Type* NativityLLVMValueGetType(Value* value)
return type; return type;
} }
extern "C" PoisonValue* NativityLLVMGetPoisonValue(Type* type)
{
auto* poison_value = PoisonValue::get(type);
return poison_value;
}
extern "C" void NativityLLVMFunctionGetArguments(Function& function, Argument** argument_ptr, size_t* argument_count) extern "C" void NativityLLVMFunctionGetArguments(Function& function, Argument** argument_ptr, size_t* argument_count)
{ {
auto actual_argument_count = function.arg_size(); auto actual_argument_count = function.arg_size();
@ -588,21 +599,36 @@ extern "C" ConstantInt* NativityLLVMContextGetConstantInt(LLVMContext& context,
return constant_int; return constant_int;
} }
extern "C" Constant* NativityLLVMContextGetConstString(LLVMContext& context, const char* string_ptr, size_t string_len, bool null_terminate) extern "C" Constant* NativityLLVMContextGetConstantString(LLVMContext& context, const char* string_ptr, size_t string_len, bool null_terminate)
{ {
auto string = StringRef(string_ptr, string_len); auto string = StringRef(string_ptr, string_len);
auto* constant = ConstantDataArray::getString(context, string, null_terminate); auto* constant = ConstantDataArray::getString(context, string, null_terminate);
return constant; return constant;
} }
extern "C" Constant* NativityLLVMContextGetConstArray(ArrayType* array_type, Constant** value_ptr, size_t value_count) extern "C" Constant* NativityLLVMGetConstantArray(ArrayType* array_type, Constant** value_ptr, size_t value_count)
{ {
auto values = ArrayRef<Constant*>(value_ptr, value_count); auto values = ArrayRef<Constant*>(value_ptr, value_count);
auto* constant_array = ConstantArray::get(array_type, values); auto* constant_array = ConstantArray::get(array_type, values);
return constant_array; return constant_array;
} }
extern "C" Constant* NativityLLVMContextCreateGlobalStringPointer(IRBuilder<>& builder, const char* string_ptr, size_t string_len, const char* name_ptr, size_t name_len, unsigned address_space, Module* module) extern "C" Constant* NativityLLVMGetConstantStruct(StructType* struct_type, Constant** constant_ptr, size_t constant_len)
{
auto values = ArrayRef<Constant*>(constant_ptr, constant_len);
auto* constant_struct = ConstantStruct::get(struct_type, values);
return constant_struct;
}
extern "C" GlobalVariable* NativityLLVMBuilderCreateGlobalString(IRBuilder<>& builder, const char* string_ptr, size_t string_len, const char* name_ptr, size_t name_len, unsigned address_space, Module* module)
{
auto string = StringRef(string_ptr, string_len);
auto name = StringRef(name_ptr, name_len);
auto* string_global_variable = builder.CreateGlobalString(string, name, address_space, module);
return string_global_variable;
}
extern "C" Constant* NativityLLVMBuilderCreateGlobalStringPointer(IRBuilder<>& builder, const char* string_ptr, size_t string_len, const char* name_ptr, size_t name_len, unsigned address_space, Module* module)
{ {
auto string = StringRef(string_ptr, string_len); auto string = StringRef(string_ptr, string_len);
auto name = StringRef(name_ptr, name_len); auto name = StringRef(name_ptr, name_len);
@ -770,13 +796,6 @@ extern "C" void NativityLLVMCallSetCallingConvention(CallBase& call_instruction,
call_instruction.setCallingConv(calling_convention); call_instruction.setCallingConv(calling_convention);
} }
extern "C" Constant* NativityLLVMGetStruct(StructType* struct_type, Constant** constants_ptr, size_t constant_count)
{
auto constants = ArrayRef(constants_ptr, constant_count);
auto* named_struct = ConstantStruct::get(struct_type, constants);
return named_struct;
}
namespace lld { namespace lld {
namespace coff { namespace coff {
bool link(llvm::ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS, bool link(llvm::ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,

File diff suppressed because it is too large Load Diff

View File

@ -49,8 +49,9 @@ pub extern fn NativityLLVMFunctionGetReturnType(function: *LLVM.Value.Function)
pub extern fn NativityLLVMBuilderCreateAlloca(builder: *LLVM.Builder, type: *LLVM.Type, address_space: c_uint, array_size: ?*LLVM.Value, name_ptr: [*]const u8, name_len: usize) ?*LLVM.Value.Instruction.Alloca; pub extern fn NativityLLVMBuilderCreateAlloca(builder: *LLVM.Builder, type: *LLVM.Type, address_space: c_uint, array_size: ?*LLVM.Value, name_ptr: [*]const u8, name_len: usize) ?*LLVM.Value.Instruction.Alloca;
pub extern fn NativityLLVMBuilderCreateStore(builder: *LLVM.Builder, value: *LLVM.Value, pointer: *LLVM.Value, is_volatile: bool) ?*LLVM.Value.Instruction.Store; pub extern fn NativityLLVMBuilderCreateStore(builder: *LLVM.Builder, value: *LLVM.Value, pointer: *LLVM.Value, is_volatile: bool) ?*LLVM.Value.Instruction.Store;
pub extern fn NativityLLVMContextGetConstantInt(context: *LLVM.Context, bit_count: c_uint, value: u64, is_signed: bool) ?*LLVM.Value.Constant.Int; pub extern fn NativityLLVMContextGetConstantInt(context: *LLVM.Context, bit_count: c_uint, value: u64, is_signed: bool) ?*LLVM.Value.Constant.Int;
pub extern fn NativityLLVMContextGetConstString(context: *LLVM.Context, name_ptr: [*]const u8, name_len: usize, null_terminate: bool) ?*LLVM.Value.Constant; pub extern fn NativityLLVMContextGetConstantString(context: *LLVM.Context, name_ptr: [*]const u8, name_len: usize, null_terminate: bool) ?*LLVM.Value.Constant;
pub extern fn NativityLLVMContextGetConstArray(array_type: *LLVM.Type.Array, value_ptr: [*]const *LLVM.Value.Constant, value_count: usize) ?*LLVM.Value.Constant; pub extern fn NativityLLVMGetConstantArray(array_type: *LLVM.Type.Array, value_ptr: [*]const *LLVM.Value.Constant, value_count: usize) ?*LLVM.Value.Constant;
pub extern fn NativityLLVMGetConstantStruct(struct_type: *LLVM.Type.Struct, constant_ptr: [*]const *LLVM.Value.Constant, constant_len: usize) ?*LLVM.Value.Constant;
pub extern fn NativityLLVMBuilderCreateICmp(builder: *LLVM.Builder, integer_comparison: LLVM.Value.Instruction.ICmp.Kind, left: *LLVM.Value, right: *LLVM.Value, name_ptr: [*]const u8, name_len: usize) ?*LLVM.Value; pub extern fn NativityLLVMBuilderCreateICmp(builder: *LLVM.Builder, integer_comparison: LLVM.Value.Instruction.ICmp.Kind, left: *LLVM.Value, right: *LLVM.Value, name_ptr: [*]const u8, name_len: usize) ?*LLVM.Value;
pub extern fn NativityLLVMBuilderCreateLoad(builder: *LLVM.Builder, type: *LLVM.Type, value: *LLVM.Value, is_volatile: bool, name_ptr: [*]const u8, name_len: usize) ?*LLVM.Value.Instruction.Load; pub extern fn NativityLLVMBuilderCreateLoad(builder: *LLVM.Builder, type: *LLVM.Type, value: *LLVM.Value, is_volatile: bool, name_ptr: [*]const u8, name_len: usize) ?*LLVM.Value.Instruction.Load;
pub extern fn NativityLLVMBuilderCreateRet(builder: *LLVM.Builder, value: ?*LLVM.Value) ?*LLVM.Value.Instruction.Ret; pub extern fn NativityLLVMBuilderCreateRet(builder: *LLVM.Builder, value: ?*LLVM.Value) ?*LLVM.Value.Instruction.Ret;
@ -90,6 +91,7 @@ pub extern fn NativityLLVMFunctionToString(function: *LLVM.Value.Function, messa
pub extern fn NativityLLVMBuilderIsCurrentBlockTerminated(builder: *LLVM.Builder) bool; pub extern fn NativityLLVMBuilderIsCurrentBlockTerminated(builder: *LLVM.Builder) bool;
pub extern fn NativityLLVMGetUndefined(type: *LLVM.Type) ?*LLVM.Value.Constant.Undefined; pub extern fn NativityLLVMGetUndefined(type: *LLVM.Type) ?*LLVM.Value.Constant.Undefined;
pub extern fn NativityLLVMGetPoisonValue(type: *LLVM.Type) ?*LLVM.Value.Constant.Poison;
pub extern fn NativityLLVMFunctionSetCallingConvention(function: *LLVM.Value.Function, calling_convention: LLVM.Value.Function.CallingConvention) void; pub extern fn NativityLLVMFunctionSetCallingConvention(function: *LLVM.Value.Function, calling_convention: LLVM.Value.Function.CallingConvention) void;
pub extern fn NativityLLVMFunctionGetCallingConvention(function: *LLVM.Value.Function) LLVM.Value.Function.CallingConvention; pub extern fn NativityLLVMFunctionGetCallingConvention(function: *LLVM.Value.Function) LLVM.Value.Function.CallingConvention;
pub extern fn NativityLLVMFunctionSetSubprogram(function: *LLVM.Value.Function, subprogram: *LLVM.DebugInfo.Subprogram) void; pub extern fn NativityLLVMFunctionSetSubprogram(function: *LLVM.Value.Function, subprogram: *LLVM.DebugInfo.Subprogram) void;
@ -114,9 +116,11 @@ pub extern fn NativityLLVMModuleGetIntrinsicDeclaration(module: *LLVM.Module, in
pub extern fn NativityLLVMContextGetIntrinsicType(context: *LLVM.Context, intrinsic_id: LLVM.Value.IntrinsicID, parameter_type_ptr: [*]const *LLVM.Type, parameter_type_count: usize) ?*LLVM.Type.Function; pub extern fn NativityLLVMContextGetIntrinsicType(context: *LLVM.Context, intrinsic_id: LLVM.Value.IntrinsicID, parameter_type_ptr: [*]const *LLVM.Type, parameter_type_count: usize) ?*LLVM.Type.Function;
pub extern fn NativityLLVMBuilderCreateExtractValue(builder: *LLVM.Builder, aggregate: *LLVM.Value, indices_ptr: [*]const c_uint, indices_len: usize, name_ptr: [*]const u8, name_len: usize) ?*LLVM.Value; pub extern fn NativityLLVMBuilderCreateExtractValue(builder: *LLVM.Builder, aggregate: *LLVM.Value, indices_ptr: [*]const c_uint, indices_len: usize, name_ptr: [*]const u8, name_len: usize) ?*LLVM.Value;
pub extern fn NativityLLVMBuilderCreateInsertValue(builder: *LLVM.Builder, aggregate: *LLVM.Value, value: *LLVM.Value, indices_ptr: [*]const c_uint, indices_len: usize, name_ptr: [*]const u8, name_len: usize) ?*LLVM.Value; pub extern fn NativityLLVMBuilderCreateInsertValue(builder: *LLVM.Builder, aggregate: *LLVM.Value, value: *LLVM.Value, indices_ptr: [*]const c_uint, indices_len: usize, name_ptr: [*]const u8, name_len: usize) ?*LLVM.Value;
pub extern fn NativityLLVMContextCreateGlobalStringPointer(builder: *LLVM.Builder, string_ptr: [*]const u8, string_len: usize, name_ptr: [*]const u8, name_len: usize, address_space: c_uint, module: *LLVM.Module) ?*LLVM.Value.Constant; pub extern fn NativityLLVMBuilderCreateGlobalString(builder: *LLVM.Builder, string_ptr: [*]const u8, string_len: usize, name_ptr: [*]const u8, name_len: usize, address_space: c_uint, module: *LLVM.Module) ?*LLVM.Value.Constant.GlobalVariable;
pub extern fn NativityLLVMBuilderCreateGlobalStringPointer(builder: *LLVM.Builder, string_ptr: [*]const u8, string_len: usize, name_ptr: [*]const u8, name_len: usize, address_space: c_uint, module: *LLVM.Module) ?*LLVM.Value.Constant;
pub extern fn NativityLLVMCompareTypes(a: *LLVM.Type, b: *LLVM.Type) bool; pub extern fn NativityLLVMCompareTypes(a: *LLVM.Type, b: *LLVM.Type) bool;
pub extern fn NativityLLVMCreatePhiNode(type: *LLVM.Type, reserved_value_count: c_uint, name_ptr: [*]const u8, name_len: usize, basic_block: ?*LLVM.Value.BasicBlock) ?*LLVM.Value.Instruction.PhiNode; pub extern fn NativityLLVMBuilderCreatePhi(builder: *LLVM.Builder, type: *LLVM.Type, reserved_value_count: c_uint, name_ptr: [*]const u8, name_len: usize) ?*LLVM.Value.Instruction.PhiNode;
pub extern fn NativityLLVMPhiAddIncoming(phi: *LLVM.Value.Instruction.PhiNode, value: *LLVM.Value, basic_block: *LLVM.Value.BasicBlock) void;
pub extern fn NativityLLVMAllocatGetAllocatedType(alloca: *LLVM.Value.Instruction.Alloca) *LLVM.Type; pub extern fn NativityLLVMAllocatGetAllocatedType(alloca: *LLVM.Value.Instruction.Alloca) *LLVM.Type;
pub extern fn NativityLLVMValueToAlloca(value: *LLVM.Value) ?*LLVM.Value.Instruction.Alloca; pub extern fn NativityLLVMValueToAlloca(value: *LLVM.Value) ?*LLVM.Value.Instruction.Alloca;

View File

@ -171,6 +171,11 @@ pub fn enumFromString(comptime E: type, string: []const u8) ?E {
} else null; } else null;
} }
pub fn hash(string: []const u8) u32 {
const string_key: u32 = @truncate(std.hash.Wyhash.hash(0, string));
return string_key;
}
pub fn StringKeyMap(comptime Value: type) type { pub fn StringKeyMap(comptime Value: type) type {
return struct { return struct {
list: std.MultiArrayList(Data) = .{}, list: std.MultiArrayList(Data) = .{},

View File

@ -1583,7 +1583,7 @@ const Analyzer = struct {
else => unreachable, else => unreachable,
}; };
const type_node = if (analyzer.peekToken() == .operator_left_parenthesis) b: { const type_node = if (analyzer.hasTokens() and analyzer.peekToken() == .operator_left_parenthesis) b: {
analyzer.consumeToken(); analyzer.consumeToken();
const result = try analyzer.typeExpression(); const result = try analyzer.typeExpression();
_ = try analyzer.expectToken(.operator_right_parenthesis); _ = try analyzer.expectToken(.operator_right_parenthesis);

View File

@ -29,41 +29,78 @@ pub fn main() !void {
standalone_test_dir.close(); standalone_test_dir.close();
const total_compilation_count = standalone_test_names.items.len;
var ran_compilation_count: usize = 0;
var failed_compilation_count: usize = 0;
var ran_test_count: usize = 0; var ran_test_count: usize = 0;
var failed_test_count: usize = 0; var failed_test_count: usize = 0;
const total_test_count = standalone_test_names.items.len; const total_test_count = standalone_test_names.items.len;
for (standalone_test_names.items) |standalone_test_name| { for (standalone_test_names.items) |standalone_test_name| {
defer ran_test_count += 1;
std.debug.print("{s}... ", .{standalone_test_name}); std.debug.print("{s}... ", .{standalone_test_name});
const source_file_path = try std.mem.concat(allocator, u8, &.{standalone_test_dir_path, "/", standalone_test_name, "/main.nat"}); const source_file_path = try std.mem.concat(allocator, u8, &.{standalone_test_dir_path, "/", standalone_test_name, "/main.nat"});
const process_run = try std.ChildProcess.run(.{ const compile_run = try std.ChildProcess.run(.{
.allocator = allocator, .allocator = allocator,
// TODO: delete -main_source_file? // TODO: delete -main_source_file?
.argv = &.{"zig-out/bin/nat", "exe", "-main_source_file", source_file_path}, .argv = &.{"zig-out/bin/nat", "exe", "-main_source_file", source_file_path},
}); });
const result: TestError!bool = switch (process_run.term) { ran_compilation_count += 1;
const compilation_result: TestError!bool = switch (compile_run.term) {
.Exited => |exit_code| if (exit_code == 0) true else error.abnormal_exit_code, .Exited => |exit_code| if (exit_code == 0) true else error.abnormal_exit_code,
.Signal => error.signaled, .Signal => error.signaled,
.Stopped => error.stopped, .Stopped => error.stopped,
.Unknown => error.unknown, .Unknown => error.unknown,
}; };
const success = result catch b: { const compilation_success = compilation_result catch b: {
failed_compilation_count += 1;
break :b false;
};
std.debug.print("[COMPILATION {s}] ", .{if (compilation_success) "OK" else "FAILED"});
if (compile_run.stdout.len > 0) {
std.debug.print("STDOUT:\n\n{s}\n\n", .{compile_run.stdout});
}
if (compile_run.stderr.len > 0) {
std.debug.print("STDERR:\n\n{s}\n\n", .{compile_run.stderr});
}
if (compilation_success) {
const test_path = try std.mem.concat(allocator, u8, &.{"nat/", standalone_test_name});
const test_run = try std.ChildProcess.run(.{
.allocator = allocator,
// TODO: delete -main_source_file?
.argv = &.{ test_path },
});
ran_test_count += 1;
const test_result: TestError!bool = switch (test_run.term) {
.Exited => |exit_code| if (exit_code == 0) true else error.abnormal_exit_code,
.Signal => error.signaled,
.Stopped => error.stopped,
.Unknown => error.unknown,
};
const test_success = test_result catch b: {
failed_test_count += 1; failed_test_count += 1;
break :b false; break :b false;
}; };
std.debug.print("[{s}]\n", .{if (success) "OK" else "FAIL"}); std.debug.print("[TEST {s}]\n", .{if (test_success) "OK" else "FAILED"});
if (process_run.stdout.len > 0) { if (test_run.stdout.len > 0) {
std.debug.print("STDOUT:\n\n{s}\n\n", .{process_run.stdout}); std.debug.print("STDOUT:\n\n{s}\n\n", .{test_run.stdout});
} }
if (process_run.stderr.len > 0) { if (test_run.stderr.len > 0) {
std.debug.print("STDERR:\n\n{s}\n\n", .{process_run.stderr}); std.debug.print("STDERR:\n\n{s}\n\n", .{test_run.stderr});
}
} else {
std.debug.print("\n", .{});
} }
} }
std.debug.print("\nTOTAL: {}. RAN: {}. FAILED: {}\n", .{total_test_count, ran_test_count, failed_test_count}); std.debug.print("\nTOTAL COMPILATIONS: {}. FAILED: {}\n", .{total_compilation_count, failed_compilation_count});
if (failed_test_count > 0) { std.debug.print("\nTOTAL TESTS: {}. RAN: {}. FAILED: {}\n", .{total_test_count, ran_test_count, failed_test_count});
if (failed_compilation_count > 0 or failed_test_count > 0) {
return error.fail; return error.fail;
} }
} }

View File

@ -30,7 +30,7 @@ const FileDescriptor = struct{
switch (current) { switch (current) {
.linux => { .linux => {
const len: usize = #min(max_file_operation_byte_count, bytes.len); const len: usize = #min(max_file_operation_byte_count, bytes.len);
if (linux.unwrapSyscall(syscall_result = #syscall(#cast(linux.Syscall.read), #cast(file_descriptor.handle), #cast(bytes.ptr), len))) |byte_count| { if (linux.unwrap_syscall(syscall_result = #syscall(#cast(linux.Syscall.read), #cast(file_descriptor.handle), #cast(bytes.ptr), len))) |byte_count| {
return byte_count; return byte_count;
} else { } else {
return null; return null;
@ -48,7 +48,7 @@ const FileDescriptor = struct{
.linux => { .linux => {
const len: usize = #min(max_file_operation_byte_count, bytes.len); const len: usize = #min(max_file_operation_byte_count, bytes.len);
const raw_result = #syscall(#cast(linux.Syscall.write), #cast(file_descriptor.handle), #cast(bytes.ptr), len); const raw_result = #syscall(#cast(linux.Syscall.write), #cast(file_descriptor.handle), #cast(bytes.ptr), len);
if (linux.unwrapSyscall(syscall_result = raw_result)) |byte_count| { if (linux.unwrap_syscall(syscall_result = raw_result)) |byte_count| {
return byte_count; return byte_count;
} else { } else {
return null; return null;
@ -93,7 +93,7 @@ const allocate_virtual_memory = fn(address: ?[&]u8, length: usize, general_prote
switch (current) { switch (current) {
.linux => { .linux => {
if (linux.unwrapSyscall(syscall_result = linux.mmap(address, length, protection_flags, map_flags, fd = -1, offset = 0))) |result_address| { if (linux.unwrap_syscall(syscall_result = linux.mmap(address, length, protection_flags, map_flags, fd = -1, offset = 0))) |result_address| {
const pointer: [&]u8 = #cast(result_address); const pointer: [&]u8 = #cast(result_address);
return pointer; return pointer;
} else { } else {
@ -108,7 +108,7 @@ const allocate_virtual_memory = fn(address: ?[&]u8, length: usize, general_prote
const free_virtual_memory = fn(bytes_ptr: [&]const u8, bytes_len: usize) bool { const free_virtual_memory = fn(bytes_ptr: [&]const u8, bytes_len: usize) bool {
switch (current) { switch (current) {
.linux => { .linux => {
if (linux.unwrapSyscall(syscall_result = linux.munmap(bytes_ptr, bytes_len))) |result| { if (linux.unwrap_syscall(syscall_result = linux.munmap(bytes_ptr, bytes_len))) |result| {
return result == 0; return result == 0;
} else { } else {
return false; return false;
@ -123,7 +123,7 @@ const readlink = fn(file_path: [&:0]const u8, buffer: []u8) ?[]u8 {
.linux => { .linux => {
const raw_result = linux.readlink(file_path, bytes_ptr = buffer.ptr, bytes_len = buffer.len); const raw_result = linux.readlink(file_path, bytes_ptr = buffer.ptr, bytes_len = buffer.len);
if (linux.unwrapSyscall(syscall_result = raw_result)) |byte_count| { if (linux.unwrap_syscall(syscall_result = raw_result)) |byte_count| {
const bytes = buffer[0..byte_count]; const bytes = buffer[0..byte_count];
return bytes; return bytes;
} else { } else {
@ -159,7 +159,7 @@ const Process = struct{
const duplicate_process = fn () ?Process.Id { const duplicate_process = fn () ?Process.Id {
switch (current) { switch (current) {
.linux => { .linux => {
if (linux.unwrapSyscall(syscall_result = linux.fork())) |fork_result| { if (linux.unwrap_syscall(syscall_result = linux.fork())) |fork_result| {
return #cast(fork_result); return #cast(fork_result);
} else { } else {
return null; return null;
@ -181,7 +181,7 @@ const execute = fn(path: [&:0]const u8, argv: [&:null]const ?[&:0]const u8, env:
const event_file_descriptor = fn(initial_value: u32, flags: u32) ?s32 { const event_file_descriptor = fn(initial_value: u32, flags: u32) ?s32 {
switch (current) { switch (current) {
.linux => { .linux => {
if (linux.unwrapSyscall(syscall_result = linux.event_file_descriptor(count = initial_value, flags))) |raw_result| { if (linux.unwrap_syscall(syscall_result = linux.event_file_descriptor(count = initial_value, flags))) |raw_result| {
const result: s32 = #cast(raw_result); const result: s32 = #cast(raw_result);
return result; return result;
} else { } else {
@ -195,7 +195,7 @@ const event_file_descriptor = fn(initial_value: u32, flags: u32) ?s32 {
const dup2 = fn(old_file_descriptor: system.FileDescriptor, new_file_descriptor: system.FileDescriptor) bool { const dup2 = fn(old_file_descriptor: system.FileDescriptor, new_file_descriptor: system.FileDescriptor) bool {
switch (current) { switch (current) {
.linux => { .linux => {
if (linux.unwrapSyscall(syscall_result = linux.dup2(old = old_file_descriptor, new = new_file_descriptor))) |_| { if (linux.unwrap_syscall(syscall_result = linux.dup2(old = old_file_descriptor, new = new_file_descriptor))) |_| {
return true; return true;
} else { } else {
return false; return false;
@ -208,7 +208,7 @@ const dup2 = fn(old_file_descriptor: system.FileDescriptor, new_file_descriptor:
const open = fn(path: [&:0]const u8, flags: u32, permissions: u32) ?FileDescriptor{ const open = fn(path: [&:0]const u8, flags: u32, permissions: u32) ?FileDescriptor{
switch (current) { switch (current) {
.linux => { .linux => {
if (linux.unwrapSyscall(syscall_result = linux.open(path, flags, permissions))) |raw_result| { if (linux.unwrap_syscall(syscall_result = linux.open(path, flags, permissions))) |raw_result| {
const file_descriptor = FileDescriptor{ const file_descriptor = FileDescriptor{
.handle = #cast(raw_result), .handle = #cast(raw_result),
}; };
@ -225,7 +225,7 @@ const open = fn(path: [&:0]const u8, flags: u32, permissions: u32) ?FileDescript
const close = fn(file_descriptor: s32) bool { const close = fn(file_descriptor: s32) bool {
switch (current) { switch (current) {
.linux => { .linux => {
if (linux.unwrapSyscall(syscall_result = linux.close(file_descriptor))) |_| { if (linux.unwrap_syscall(syscall_result = linux.close(file_descriptor))) |_| {
return true; return true;
} else { } else {
return false; return false;
@ -239,7 +239,7 @@ const pipe2 = fn(flags: u32) ?[2]system.FileDescriptor{
switch (current) { switch (current) {
.linux => { .linux => {
var pipe: [2]s32 = undefined; var pipe: [2]s32 = undefined;
if (linux.unwrapSyscall(syscall_result = linux.pipe2(pipe_pointer = pipe.&, flags))) |_| { if (linux.unwrap_syscall(syscall_result = linux.pipe2(pipe_pointer = pipe.&, flags))) |_| {
return pipe; return pipe;
} else { } else {
return null; return null;
@ -268,7 +268,7 @@ const PollFileDescriptor = system.PollFileDescriptor;
const poll = fn(file_descriptors: []PollFileDescriptor, timeout: s32) ?usize { const poll = fn(file_descriptors: []PollFileDescriptor, timeout: s32) ?usize {
switch (current) { switch (current) {
.linux => { .linux => {
if (linux.unwrapSyscall(syscall_result = linux.poll(file_descriptors = file_descriptors.ptr, file_descriptor_count = file_descriptors.len, timeout = timeout))) |result| { if (linux.unwrap_syscall(syscall_result = linux.poll(file_descriptors = file_descriptors.ptr, file_descriptor_count = file_descriptors.len, timeout = timeout))) |result| {
return result; return result;
} else { } else {
return null; return null;
@ -339,7 +339,7 @@ const waitpid = fn(pid: Process.Id, flags: u32) ?u32 {
const raw_syscall_result = linux.waitpid(pid, status = status.&, flags, resource_usage = 0); const raw_syscall_result = linux.waitpid(pid, status = status.&, flags, resource_usage = 0);
const signed_syscall_result: ssize = #cast(raw_syscall_result); const signed_syscall_result: ssize = #cast(raw_syscall_result);
if (raw_syscall_result != -4) { if (raw_syscall_result != -4) {
if (linux.unwrapSyscall(syscall_result = raw_syscall_result)) |_| { if (linux.unwrap_syscall(syscall_result = raw_syscall_result)) |_| {
return status; return status;
} else { } else {
return null; return null;
@ -355,7 +355,7 @@ const waitpid = fn(pid: Process.Id, flags: u32) ?u32 {
const memfd_create = fn(name: [&:0]const u8, flags: u32) ?FileDescriptor{ const memfd_create = fn(name: [&:0]const u8, flags: u32) ?FileDescriptor{
switch (current) { switch (current) {
.linux => { .linux => {
if (linux.unwrapSyscall(syscall_result = linux.memfd_create(path, flags))) |raw_result| { if (linux.unwrap_syscall(syscall_result = linux.memfd_create(path, flags))) |raw_result| {
const file_descriptor = FileDescriptor{ const file_descriptor = FileDescriptor{
.handle = #cast(raw_result), .handle = #cast(raw_result),
}; };

View File

@ -490,7 +490,7 @@ const memfd_create = fn(name: [&:0]const u8, flags: u32) usize {
return result; return result;
} }
const unwrapSyscall = fn(syscall_result: usize) ?usize { const unwrap_syscall = fn(syscall_result: usize) ?usize {
const signed_syscall_result: ssize = #cast(syscall_result); const signed_syscall_result: ssize = #cast(syscall_result);
if (signed_syscall_result >= 0) { if (signed_syscall_result >= 0) {
return syscall_result; return syscall_result;

View File

@ -18,7 +18,7 @@ const print = fn(bytes: []const u8) void {
const file_writer = FileWriter{ const file_writer = FileWriter{
.descriptor = file_descriptor, .descriptor = file_descriptor,
}; };
_ = file_writer.writeAll(bytes); _ = file_writer.write_all(bytes);
} }
const format_usize = fn(n: usize, buffer: &[65]u8) []u8 { const format_usize = fn(n: usize, buffer: &[65]u8) []u8 {
@ -46,7 +46,7 @@ const print_usize = fn(n: usize) void {
const file_writer = FileWriter{ const file_writer = FileWriter{
.descriptor = file_descriptor, .descriptor = file_descriptor,
}; };
_ = file_writer.writeAll(bytes); _ = file_writer.write_all(bytes);
} }
const print_u8 = fn(n: u8) void { const print_u8 = fn(n: u8) void {
@ -145,7 +145,7 @@ const FileWriter = struct{
return file_writer.descriptor.write(bytes); return file_writer.descriptor.write(bytes);
} }
const writeAll = fn(file_writer: FileWriter, bytes: []const u8) bool { const write_all = fn(file_writer: FileWriter, bytes: []const u8) bool {
var bytes_written: usize = 0; var bytes_written: usize = 0;
while (bytes_written < bytes.len) { while (bytes_written < bytes.len) {

View File

@ -0,0 +1,21 @@
const main = fn () s32 {
const a = foo(5);
if (a != 123) {
return 1;
}
const b = foo(5);
if (b != 123) {
return 1;
}
return a - b;
}
const foo = fn (arg: s32) s32 {
if (arg < 0) {
return 12312;
} else if (arg > 0) {
return 123;
} else {
return 0;
}
}

View File

@ -0,0 +1,19 @@
const main = fn () s32 {
const a = foo(5);
if (a != 6) {
return 1;
}
const b = foo(5);
if (b != 6) {
return 1;
}
return a - b;
}
const foo = fn (arg: s32) s32 {
if (arg > 1) {
return 6;
} else {
return 0;
}
}

View File

@ -0,0 +1,19 @@
const main = fn() s32 {
const a = foo(5);
if (a != 12412) {
return 1;
}
const b = foo(5);
if (b != 12412) {
return 1;
}
return a - b;
}
const foo = fn(arg: s32) s32 {
if (arg > 1241) {
return 125151;
}
return 12412;
}

View File

@ -0,0 +1,24 @@
const main = fn () s32 {
const a = foo(5142);
if (a != 2501) {
return 1;
}
const b = foo(5142);
if (b != 2501) {
return 1;
}
return #cast(a - b);
}
const foo = fn (arg: u32) u32 {
var i: u32 = 0;
while (i < arg) {
if (i > 2500) {
return i;
}
i += 1;
}
return 321;
}

View File

@ -0,0 +1,19 @@
const main = fn () s32 {
const a = foo(5142);
const b = foo(5142);
return #cast(a - b);
}
const foo = fn (arg: u32) u32 {
var i: u32 = 0;
while (i < arg) {
if (i < 2500) {
i += 1;
} else {
return i - 100;
}
}
return 321;
}

View File

@ -0,0 +1,17 @@
const main = fn () s32 {
const a = foo(5);
const b = foo(5);
return a - b;
}
const foo = fn (arg: s32) s32 {
if (arg > 1) {
if (arg < 5) {
return 6;
} else {
return 5;
}
} else {
return 0;
}
}