commit fb8fa375a7e6d6b02bb5525678ac236d765a0981
parent 23e36ee7be6254d01bee13a7d23db5ebe574e73a
Author: thing1 <l.standen@posteo.com>
Date: Sun, 5 Oct 2025 18:27:50 +0100
created the time core, made simple brainfuck compiler
Diffstat:
10 files changed, 166 insertions(+), 6 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -1,5 +1,6 @@
lli
lliasm
+examples/bf/bf
*.rom
*.so
*.tar
diff --git a/cores/Makefile b/cores/Makefile
@@ -1,8 +1,10 @@
CFLAGS=-ggdb -fPIC -shared
-all: unix
+all: unix time
unix: unix_core.c
cc unix_core.c -o unix_core.so ${CFLAGS}
+time: time_core.c
+ cc time_core.c -o time_core.so ${CFLAGS}
clean:
rm -rf *.so
diff --git a/cores/time_core.c b/cores/time_core.c
@@ -0,0 +1,60 @@
+#include <time.h>
+#include <string.h>
+#include <stdint.h>
+
+/* table of memory
+ * | function | size | loc |
+ * +----------+------+-----+
+ * | year | 2 | 0 |
+ * | month | 1 | 2 |
+ * | day | 1 | 3 |
+ * | hour | 1 | 4 |
+ * | min | 1 | 5 |
+ * | sec | 1 | 6 |
+ */
+
+enum table {
+ YEAR = 0,
+ MONTH = 2,
+ DAY,
+ HOUR,
+ MIN,
+ SEC,
+};
+
+int
+getsize() {
+ return 7;
+}
+
+void
+onread(char *addr, char *loc) {
+ time_t t;
+ struct tm *tval;
+ if (loc - addr < 7 && loc - addr > 0) {
+ t = time(NULL);
+ tval = localtime(&t);
+ } else return;
+
+ uint8_t val8;
+ uint16_t val16;
+
+ switch (loc - addr) {
+ case YEAR: val16 = tval->tm_year; break;
+ case MONTH: val8 = tval->tm_mon; break;
+ case DAY: val8 = tval->tm_mday; break;
+ case HOUR: val8 = tval->tm_hour; break;
+ case MIN: val8 = tval->tm_min; break;
+ case SEC: val8 = tval->tm_sec; break;
+ }
+
+ if (loc - addr == YEAR)
+ memcpy(loc, (uint16_t *)&val16, sizeof(uint16_t));
+ else
+ memcpy(loc, (uint8_t *)&val8, sizeof(uint8_t));
+}
+
+void
+onwrite() {
+ return;
+}
diff --git a/cores/unix_core.c b/cores/unix_core.c
@@ -59,6 +59,11 @@ dosyscall(uint8_t num, int args[4], char *mem) {
}
}
+int
+getsize() {
+ return 30;
+}
+
void
onwrite(char *addr) {
if (*(addr + 1)) {
diff --git a/examples/Makefile b/examples/Makefile
@@ -1,4 +1,4 @@
-all: cat loadprog callstack print conditional fs fork
+all: cat loadprog callstack print conditional fs fork time bf
cat: cat.ll
lliasm -o cat.rom cat.ll
@@ -14,6 +14,12 @@ fs: fs.ll
lliasm -o fs.rom fs.ll
fork: fork.ll
lliasm -o fork.rom fork.ll
+time: time.ll
+ lliasm -o time.rom time.ll
+bf:
+ cd bf && make
clean:
rm -rf *.rom
+ cd bf && make clean
+
diff --git a/examples/bf/Makefile b/examples/bf/Makefile
@@ -0,0 +1,8 @@
+CFLAGS=-ggdb
+
+all: bf
+
+bf: bf.c
+ cc bf.c -o bf ${CFLAGS}
+clean:
+ rm -rf *.ll *.rom bf
diff --git a/examples/bf/bf.c b/examples/bf/bf.c
@@ -0,0 +1,57 @@
+#include <stdio.h>
+#include <string.h>
+
+int
+main() {
+ FILE *in = stdin, *out = stdout;
+ int c, cdepth = 0, hidepth = 0;
+
+ fprintf(out, "(unix core) |0x0 @stdin |0x1 @stdout |0x2 @stderr |0x3 @syscall |0x4 @arg1 |0x8 @arg2 |0xc @arg3 |0x10 @arg4 |0x14 @result |0x18 @fd |0x1c @read |0x1d @write\n");
+ fprintf(out, "|0x30 @ptr |0x34\n");
+ fprintf(out, "@arr\n");
+ fprintf(out, "|0x1000 @main\n&arr &ptr storei\n");
+
+ for (c; c != EOF; c = getc(in)) {
+ switch (c) {
+ case '+':
+ fprintf(out, "&ptr loadi loadb b2i\n");
+ fprintf(out, "#1 add i2b\n");
+ fprintf(out, "&ptr loadi storeb\n");
+ break;
+ case '-':
+ fprintf(out, "&ptr loadi loadb b2i\n");
+ fprintf(out, "#1 sub i2b\n");
+ fprintf(out, "&ptr loadi storeb\n");
+ break;
+ case '>':
+ fprintf(out, "&ptr loadi #1 add\n");
+ fprintf(out, "&ptr storei\n");
+ break;
+ case '<':
+ fprintf(out, "&ptr loadi #1 sub\n");
+ fprintf(out, "&ptr storei\n");
+ break;
+ case '.':
+ fprintf(out, "&ptr loadi loadb &stdout storeb\n");
+ break;
+ case ',':
+ fprintf(out, "&stdin loadb b2i\n");
+ fprintf(out, "&ptr loadi storei\n");
+ break;
+
+ case '[':
+ fprintf(out, "@start%d\n", hidepth);
+ fprintf(out, "&ptr loadi loadb b2i #0 equ\n");
+ fprintf(out, "&end%d jnz\n", cdepth);
+ hidepth++;
+ cdepth++;
+ break;
+ case ']':
+ cdepth--;
+ fprintf(out, "&start%d jmp\n", cdepth);
+ fprintf(out, "@end%d\n", cdepth);
+ break;
+ }
+ }
+ fprintf(out, "halt\n");
+}
diff --git a/examples/bf/test.bf b/examples/bf/test.bf
@@ -0,0 +1 @@
++++++[>+++++++++++++<-]>.
diff --git a/examples/time.ll b/examples/time.ll
@@ -0,0 +1,7 @@
+(unix core) |0x0 @stdin |0x1 @stdout |0x2 @stderr |0x3 @syscall |0x4 @arg1 |0x8 @arg2 |0xc @arg3 |0x10 @arg4 |0x14 @result |0x18 @fd |0x1c @read |0x1d @write
+(time core) |0x1e @year |0x20 @month |0x21 @day |0x22 @hour |0x23 @min |0x24 @sec
+
+|0x1000 @main
+&min loadb b2i &arg1 storei
+#3 i2b &syscall storeb
+halt
diff --git a/lli.c b/lli.c
@@ -51,10 +51,11 @@ mkpush(uint8_t);
void *corehandles[8] = {0};
void (*onread[8])(char *, char *);
void (*onwrite[8])(char *);
+int (*getsize[8])();
int address[8] = {0};
int corecount = 0;
-char memory[INT32_MAX];
+char memory[INT32_MAX] = {0};
char *pc = &memory[0x1000];
void
@@ -309,17 +310,21 @@ usage() {
int
main(int argc, char **argv) {
FILE *f;
- int len;
+ int len, pos = 0;
+ char *tmp;
ARGBEGIN {
case 'd': debug = true; break;
case 'h': usage(); break;
case 'c':
corehandles[corecount] = dlopen(EARGF(usage()), RTLD_LAZY);
+ argv++;
if (!corehandles[corecount]) {
fprintf(stderr, "%s\n", dlerror());
exit(1);
}
- address[corecount] = strtol(EARGF(usage()), NULL, 0);
+ tmp = *argv;
+ if (!tmp) usage();
+ address[corecount] = strtol(tmp, NULL, 0);
if (!(onread[corecount] = dlsym(corehandles[corecount], "onread"))) {
fprintf(stderr, "failed to open function: onread\n");
exit(1);
@@ -328,8 +333,16 @@ main(int argc, char **argv) {
fprintf(stderr, "failed to open function: onwrite\n");
exit(1);
}
+ if (!(getsize[corecount] = dlsym(corehandles[corecount], "getsize"))) {
+ fprintf(stderr, "failed to open function: getsize\n");
+ exit(1);
+ }
+ if (address[corecount] < pos - 1) {
+ fprintf(stderr, "failed to load core: alignment is not possible\n");
+ exit(1);
+ }
+ pos += getsize[corecount]();
corecount++;
- argv++;
break;
default: usage();