From ffecbd48c46029346db4ac4e9fafb1082a2ca159 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Sun, 24 Aug 2025 17:04:57 +0200 Subject: [PATCH] Implemented fs.readFile --- examples/readfile/readfile.q | 7 +++++++ lib/fs/const_linux_arm.q | 1 + lib/fs/const_linux_x86.q | 1 + lib/fs/const_mac_arm.q | 1 + lib/fs/const_mac_x86.q | 1 + lib/fs/const_windows.q | 6 ++++++ lib/fs/fs_linux.q | 6 ++++++ lib/fs/fs_mac.q | 6 ++++++ lib/fs/fs_windows.q | 21 +++++++++++++++++++++ lib/fs/readFile.q | 32 ++++++++++++++++++++++++++++++++ lib/fs/stat_unix.q | 22 ++++++++++++++++++++++ src/codegen/createHints.go | 5 ++++- 12 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 examples/readfile/readfile.q create mode 100644 lib/fs/const_windows.q create mode 100644 lib/fs/fs_windows.q create mode 100644 lib/fs/readFile.q create mode 100644 lib/fs/stat_unix.q diff --git a/examples/readfile/readfile.q b/examples/readfile/readfile.q new file mode 100644 index 0000000..5c62d07 --- /dev/null +++ b/examples/readfile/readfile.q @@ -0,0 +1,7 @@ +import fs +import io + +main() { + source := fs.readFile("examples/readfile/readfile.q") + io.write(source) +} \ No newline at end of file diff --git a/lib/fs/const_linux_arm.q b/lib/fs/const_linux_arm.q index 2d394b6..a10a0db 100644 --- a/lib/fs/const_linux_arm.q +++ b/lib/fs/const_linux_arm.q @@ -1,4 +1,5 @@ const { _openat 56 _close 57 + _fstat 80 } \ No newline at end of file diff --git a/lib/fs/const_linux_x86.q b/lib/fs/const_linux_x86.q index b29b0b7..6fada91 100644 --- a/lib/fs/const_linux_x86.q +++ b/lib/fs/const_linux_x86.q @@ -1,4 +1,5 @@ const { _openat 257 _close 3 + _fstat 5 } \ No newline at end of file diff --git a/lib/fs/const_mac_arm.q b/lib/fs/const_mac_arm.q index aec8ab1..51fbe31 100644 --- a/lib/fs/const_mac_arm.q +++ b/lib/fs/const_mac_arm.q @@ -1,4 +1,5 @@ const { _open 0x5 _close 0x6 + _fstat64 0x153 } \ No newline at end of file diff --git a/lib/fs/const_mac_x86.q b/lib/fs/const_mac_x86.q index 28d6500..9a35e9d 100644 --- a/lib/fs/const_mac_x86.q +++ b/lib/fs/const_mac_x86.q @@ -1,4 +1,5 @@ const { _open 0x2000005 _close 0x2000006 + _fstat64 0x2000153 } \ No newline at end of file diff --git a/lib/fs/const_windows.q b/lib/fs/const_windows.q new file mode 100644 index 0000000..8ec7608 --- /dev/null +++ b/lib/fs/const_windows.q @@ -0,0 +1,6 @@ +const { + FILE_ATTRIBUTE_NORMAL 0x80 + FILE_SHARE_READ 0x1 + GENERIC_READ 0x80000000 + OPEN_EXISTING 3 +} \ No newline at end of file diff --git a/lib/fs/fs_linux.q b/lib/fs/fs_linux.q index 5f0fa27..7d2b632 100644 --- a/lib/fs/fs_linux.q +++ b/lib/fs/fs_linux.q @@ -2,6 +2,12 @@ open(path *byte, flags int, mode int) -> int { return syscall(_openat, -100, path, flags, mode) } +size(fd int) -> int { + stats := new(stat) + syscall(_fstat, fd, stats) + return stats.st_size +} + close(fd int) -> int { return syscall(_close, fd) } \ No newline at end of file diff --git a/lib/fs/fs_mac.q b/lib/fs/fs_mac.q index ccd6608..6657efa 100644 --- a/lib/fs/fs_mac.q +++ b/lib/fs/fs_mac.q @@ -2,6 +2,12 @@ open(path *byte, flags int, mode int) -> int { return syscall(_open, path, flags, mode) } +size(fd int) -> int { + stats := new(stat) + syscall(_fstat64, fd, stats) + return stats.st_size +} + close(fd int) -> int { return syscall(_close, fd) } \ No newline at end of file diff --git a/lib/fs/fs_windows.q b/lib/fs/fs_windows.q new file mode 100644 index 0000000..678679d --- /dev/null +++ b/lib/fs/fs_windows.q @@ -0,0 +1,21 @@ +open(path *byte, _flags int, _mode int) -> int { + return kernel32.CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0) +} + +size(fd int) -> int { + fileSize := new(int64) + kernel32.GetFileSizeEx(fd, fileSize) + return [fileSize] +} + +close(fd int) -> bool { + return kernel32.CloseHandle(fd) +} + +extern { + kernel32 { + CloseHandle(handle int) -> (success bool) + CreateFileA(path *byte, desiredAccess uint32, shareMode uint32, securityAttributes *any, creationDisposition uint32, flagsAndAttributes uint32, templateFile int) -> (fd int) + GetFileSizeEx(fd int, fileSize *int64) -> (success bool) + } +} \ No newline at end of file diff --git a/lib/fs/readFile.q b/lib/fs/readFile.q new file mode 100644 index 0000000..942ff01 --- /dev/null +++ b/lib/fs/readFile.q @@ -0,0 +1,32 @@ +import io +import mem + +readFile(path string) -> string { + newPath := mem.alloc(path.len + 1) + + loop i := 0..path.len { + newPath[i] = path[i] + } + + fd := open(newPath.ptr, 0, 0) + mem.free(newPath) + fileSize := size(fd) + + if fileSize < 512 { + fileSize = 512 + } + + buffer := mem.alloc(fileSize) + pos := 0 + + loop { + n := io.readFrom(fd, buffer[pos..]) + + if n <= 0 { + close(fd) + return buffer[..pos] + } + + pos += n + } +} \ No newline at end of file diff --git a/lib/fs/stat_unix.q b/lib/fs/stat_unix.q new file mode 100644 index 0000000..02d8ce8 --- /dev/null +++ b/lib/fs/stat_unix.q @@ -0,0 +1,22 @@ +stat { + st_dev uint64 + st_ino uint64 + st_nlink uint64 + st_mode uint32 + st_uid uint32 + st_gid uint32 + _ uint32 + st_rdev uint64 + st_size int64 + st_blksize int64 + st_blocks int64 + st_atime int64 + st_atime_nsec int64 + st_mtime int64 + st_mtime_nsec int64 + st_ctime int64 + st_ctime_nsec int64 + _ int64 + _ int64 + _ int64 +} \ No newline at end of file diff --git a/src/codegen/createHints.go b/src/codegen/createHints.go index 962e40c..a3bd0e3 100644 --- a/src/codegen/createHints.go +++ b/src/codegen/createHints.go @@ -33,7 +33,10 @@ func (f *Function) createHints(step *Step) { for r, param := range instr.Arguments { if r >= len(f.CPU.ExternCall.In) { // Temporary hack to allow arguments 5 and 6 to be hinted as r10 and r11, then pushed later. - f.ValueToStep[param].hint(f.CPU.ExternCall.Clobbered[1+r]) + if 1+r < len(f.CPU.ExternCall.Clobbered) { + f.ValueToStep[param].hint(f.CPU.ExternCall.Clobbered[1+r]) + } + continue } -- knotmirror 0.0.1