aboutsummaryrefslogtreecommitdiff
path: root/sys/gen.py
diff options
context:
space:
mode:
authorNicholas Noll <nbnoll@eml.cc>2021-11-20 09:53:11 -0800
committerNicholas Noll <nbnoll@eml.cc>2021-11-20 09:53:11 -0800
commit1c8d4e69205fd875f6bec3fa3bd929c2e7f52f62 (patch)
tree8bb6233e8f59d574e99a55a8c9aaf4d90396a2ce /sys/gen.py
parent59d87ccc99f431da06a0ffab0d3e702d9ef82f48 (diff)
Feature: self hosting prototype implemented
This is a large change. In order to remove myself from libc's arcane interface, I implemented an independent runtime layer. It is based on musl's wonderful implementation mostly. Critically, if libc is linked to the program, then we cooperate. Namely, we call start main and let libc do all initialization. If not, then we have a noop defined in rt3.a. The general structure of the file is: 1. sys/$os/$arch contains all architecture dependent code 2. sys/$os/port contains all code that depends on the os, but is portable 3. rt/$arch contains all the runtime architecture dependent code 4. rt/* contains the portable runtime code. Obviously testing is needed. Specifically, while code is checked in for the most popular architectures, it only has been tested on one computer! Overall this is exciting and as been educational.
Diffstat (limited to 'sys/gen.py')
-rwxr-xr-xsys/gen.py48
1 files changed, 45 insertions, 3 deletions
diff --git a/sys/gen.py b/sys/gen.py
index 02ff952..4dcbc4d 100755
--- a/sys/gen.py
+++ b/sys/gen.py
@@ -289,6 +289,44 @@ def fmtsyscallold(writer, musl):
print(f"# {word[0][1:]:<8}{name[0]:<10} {name[1]}", file=writer)
# ------------------------------------------------------------------------
+# atomic
+
+def snaketolower(name):
+ return "".join(w for w in name.split("_"))
+
+def modifytype(line):
+ return line.replace("uint64_t", "uint64").replace("uint32_t", "uint32").replace("uintptr_t","uintptr")
+
+def fmtatomic(writer, reader):
+ def fmt(name):
+ prefix = ""
+ index = name.find("a_")
+ if index > 0:
+ prefix = name[:index]
+
+ name = name[index:]
+ name = name.replace("a_", "")
+ return prefix + "atomicĀ·" + snaketolower(name)
+
+ for line in reader:
+ word = [modifytype(w) for w in line.split(" ")]
+ if len(word) == 3:
+ if word[0].strip() == "#define" and word[1].strip() == word[2].strip():
+ continue
+ if len(word) == 2:
+ if word[0].strip() == "#define" and "libc.h" in word[1]:
+ continue
+
+ ours = [ fmt(w) if "a_" in w else w for w in word ]
+ line = " ".join(ours)
+ line = line.replace("static inline void ", "static inline void\n")\
+ .replace("static inline int ", "static inline int\n")\
+ .replace("if (", "if(").replace("for (", "for(").replace("while (", "while(")\
+ .replace("do {", "do{").replace("} while", "}while").replace(") {", "){")
+
+ print(line, file=writer, end="")
+
+# ------------------------------------------------------------------------
# types
stdintdefs=[
@@ -384,6 +422,10 @@ if __name__ == "__main__":
with open(f"{libnarch}/bits.h", "w") as io:
fmtbits(io, musl, source)
+ print("--> emitting atomics", file=stderr)
+ with open(f"{musl}/atomic_arch.h") as reader, open(f"{libnarch}/atomic.h", "w") as writer:
+ fmtatomic(writer,reader)
+
print("--> emitting syscalls", file=stderr)
with open(f"{libn}/syscall.h", "w") as io:
fmtsyscall(io,musl)
@@ -394,17 +436,17 @@ if __name__ == "__main__":
source, target = "generic", "port"
musl = f"{muslroot}/arch/{source}"
- libn = f"linux/{target}/arch"
+ libn = f"linux/{target}/os"
mkpath(libnarch, exist_ok=True)
with open(f"{libn}/errno.h","w") as io:
fmterrno(io, musl)
musl = f"{muslroot}/src/errno"
- libn = "src/errno.inc.h"
+ libn = "linux/src/errno.inc.h"
with open(libn,"w") as io:
fmtstrerror(io, musl)
- libn = "src/internal.h"
+ libn = "linux/src/internal.h"
musl = f"{muslroot}/src/internal/syscall.h"
needfix = False