summaryrefslogtreecommitdiff
path: root/chapter32/src/main/smp/sys/libc.smp
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2026-11-15 16:11:39 -0800
committerCliff Click <cliffc@acm.org>2026-11-15 16:11:39 -0800
commit9fe7c6f9697b5bbf3041261fdc60040e75cc674f (patch)
treed92a8ff20ed984938d90ad8917b4d67fba9fb745 /chapter32/src/main/smp/sys/libc.smp
parentf0a6c6757a795b8e186de0076836a87acef59fa7 (diff)
downloadSimple-ch32.tar.gz
Simple-ch32.zip
Split off chapter 31ch32
Ch 32 will be focused on compiler performance issues.
Diffstat (limited to 'chapter32/src/main/smp/sys/libc.smp')
-rw-r--r--chapter32/src/main/smp/sys/libc.smp61
1 files changed, 61 insertions, 0 deletions
diff --git a/chapter32/src/main/smp/sys/libc.smp b/chapter32/src/main/smp/sys/libc.smp
new file mode 100644
index 00000000..8e417461
--- /dev/null
+++ b/chapter32/src/main/smp/sys/libc.smp
@@ -0,0 +1,61 @@
+
+// libc bindings:
+// https://www.man7.org/linux/man-pages/man2/<libc call>.2.html
+
+// The buffers are i64 addresses of zero-terminated strings.
+// i64 buf = aryu8.str0("some_file");
+// buf mode perm-> fd
+{ i64 i32 i32 -> i32 } open = "C";
+// buf perm -> fd
+{ i64 i32 -> i32 } creat = "C";
+
+// fd buf len -> len
+{ i32 i64 i64 -> i64 } write = "C";
+// fd buf len -> len
+{ i32 i64 i64 -> i64 } read = "C";
+// fd -> err
+{ i32 -> i32 } close = "C";
+// path -> err
+{ i64 -> i32 } unlink = "C";
+
+i32 O_RDONLY = 0;
+i32 O_WRONLY = 1;
+i32 O_RDWR = 2;
+i32 O_CREAT = 512; // 02000 on x86_64-pc-cygwin
+i32 O_TRUNC = 1024; // 04000 on x86_64-pc-cygwin
+
+i32 STDIN = 0;
+i32 STDOUT = 1;
+i32 STDERR = 2;
+i32 EOF = -1; // simple_getch sets errno to EOF on end-of-input.
+
+// status
+{ i32 -> i32 } _exit = "C";
+
+// errno is not an ordinary C data symbol; read it through the native runtime.
+{ i32 } simple_errno = "C";
+val errno = { -> simple_errno(); };
+{ i32 -> i32 } simple_set_errno = "C";
+// Set errno and return -1, matching the native I/O error convention.
+val set_errno = { i32 error -> simple_set_errno(error); };
+
+i32 EINTR = "C";
+i32 EIO = "C";
+i32 EINVAL = "C";
+i32 EOVERFLOW = "C";
+
+// Read one byte from stdin, without <enter> or echo. Restores the previous
+// terminal mode before returning. Files and pipes also work. Returns 0..255,
+// or -1 with errno set to EOF or an OS error. Ctrl-C is byte 3; escape/UTF-8
+// sequences are multiple bytes.
+{ i32 } simple_getch = "C";
+
+// addr len prot flags fd off -> void*
+{ i64 i64 i32 i32 i32 i32 -> i64 } mmap = "C";
+// mmap flags
+i32 PROT_EXEC = 4;
+i32 PROT_READ = 1;
+i32 PROT_WRITE = 2;
+i32 PROT_NONE = 0;
+i32 MAP_PRIVATE = 2;
+i32 MAP_ANON = 32;