summaryrefslogtreecommitdiff
path: root/src/main/smp/sys/io.smp
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2026-08-10 11:11:18 -0700
committerCliff Click <cliffc@acm.org>2026-10-08 16:52:27 -0700
commitd32f97238c903765dec4e9a9f3cbfa5fd12cded6 (patch)
treea5ac731801ef7066bab6359c4e85c2bdd20082a6 /src/main/smp/sys/io.smp
parentb5ebb2032aa04cf4506d533127d373d4cf5a25c6 (diff)
downloadSimple-d32f97238c903765dec4e9a9f3cbfa5fd12cded6.tar.gz
Simple-d32f97238c903765dec4e9a9f3cbfa5fd12cded6.zip
Chapter 25: Modules, Separate Compilation, and SSA Constructionlinear
Co-authored-by: Hsiang-Ying Fu <66062782+fuhsnn@users.noreply.github.com> Co-authored-by: fuhsnn <66062782+fuhsnn@users.noreply.github.com> Co-authored-by: Symious <14933944+symious@users.noreply.github.com> Co-authored-by: Jun Kawasaki <jun784@gmail.com> Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Diffstat (limited to 'src/main/smp/sys/io.smp')
-rw-r--r--src/main/smp/sys/io.smp64
1 files changed, 64 insertions, 0 deletions
diff --git a/src/main/smp/sys/io.smp b/src/main/smp/sys/io.smp
new file mode 100644
index 00000000..d6263ffe
--- /dev/null
+++ b/src/main/smp/sys/io.smp
@@ -0,0 +1,64 @@
+
+// Simple I/O Library
+
+// Write byte array to stdout
+val p = { u8[~] str -> write(1,str); };
+val p_noInline = { u8[~] str -> write(1,str); };
+
+// Return fd contents as a u8[].
+val read = { i32 fd ->
+ var buf = new u8[8*1024];
+ i64 ptr = buf;
+ int len = 0;
+ while( true ) {
+ i32 rez = libc.read(fd,ptr,buf#);
+ if( rez > buf# ) libc._exit(libc.errno());
+ len += rez;
+ if( rez < buf# )
+ break;
+ libc._exit(-2); // Need to implement for more than 8K input
+ }
+ var out = new u8[len];
+ ary.copy(out,0,buf,0,len);
+};
+
+// Write all of str to fd.
+val write = { i32 fd, u8[~] str ->
+ i64 ptr = str; // Convert array pointer to a i64 for the native call
+ libc.write(fd,ptr,str#);
+};
+
+// Write all of str to fd.
+val writeOff = { i32 fd, u8[~] str, u32 off, u32 len ->
+ i64 ptr = str; // Convert array pointer to a i64 for the native call
+ ptr += off; // Skip offset
+ // Cap at available length
+ if( len > str# - off ) len = str# - off;
+ // Do the write
+ libc.write(fd,ptr,len);
+};
+
+// Return stdin as a u8[].
+val stdin = { -> read(0); };
+
+// Create file for writing. Return fd, or -1 and errno has the error code
+val create = { u8[~] str ->
+ i64 ptr = sys.aryu8.str0(str);
+ i32 perms = 6*64 + 6*8 + 6; // 0666
+ return libc.creat(ptr, perms);
+};
+
+// Open file for reading. Return fd, or -1 and errno has the error code
+val open = { u8[~] str ->
+ i64 ptr = sys.aryu8.str0(str);
+ return libc.open(ptr, sys.libc.O_RDONLY, 0);
+};
+
+// Close an open fd. Return 0, or -1 and errno has the error code
+val close = { i32 fd -> return libc.close(fd); };
+
+// Remove a file. Return 0, or -1 and errno has the error code
+val remove = { u8[~] str ->
+ i64 ptr = sys.aryu8.str0(str);
+ return libc.unlink(ptr);
+};