aboutsummaryrefslogtreecommitdiff
path: root/mod
diff options
context:
space:
mode:
Diffstat (limited to 'mod')
-rw-r--r--mod/Makefile10
-rw-r--r--mod/io.c31
-rw-r--r--mod/mem.c50
-rw-r--r--mod/util.c20
4 files changed, 111 insertions, 0 deletions
diff --git a/mod/Makefile b/mod/Makefile
new file mode 100644
index 0000000..9121afd
--- /dev/null
+++ b/mod/Makefile
@@ -0,0 +1,10 @@
+all: libfwdio.so libfwdmem.so libfwdutil.so
+
+libfwdio.so: io.c ../include/fwd/mod.h
+ $(CC) -I../include -fPIC -O2 -g -Wall -Wextra -shared io.c -o libfwdio.so
+
+libfwdmem.so: mem.c ../include/fwd/mod.h
+ $(CC) -I../include -fPIC -O2 -g -Wall -Wextra -shared mem.c -o libfwdmem.so
+
+libfwdutil.so: util.c ../include/fwd/mod.h
+ $(CC) -I../include -fPIC -O2 -g -Wall -Wextra -shared util.c -o libfwdutil.so
diff --git a/mod/io.c b/mod/io.c
new file mode 100644
index 0000000..d376d1c
--- /dev/null
+++ b/mod/io.c
@@ -0,0 +1,31 @@
+#include <stdint.h>
+#include <assert.h>
+#include <stdio.h>
+
+#include <fwd/mod.h>
+
+long fwdprint_nl(fwd_extern_args_t args)
+{
+ assert(args.argc == 0);
+ putchar('\n');
+ return 0;
+}
+
+long fwdprint_i64(fwd_extern_args_t args)
+{
+ assert(args.argc == 1);
+ int64_t n = FWD_ARG_T(args, 0, int64_t);
+ printf("%lld", (long long)n);
+ return 0;
+}
+
+int fwdopen(fwd_state_t *state)
+{
+ FWD_REGISTER(state, fwdprint_nl,
+ FWD_VOID);
+
+ FWD_REGISTER(state, fwdprint_i64,
+ FWD_VOID, FWD_T(int64_t));
+
+ return 0;
+}
diff --git a/mod/mem.c b/mod/mem.c
new file mode 100644
index 0000000..96d6d0e
--- /dev/null
+++ b/mod/mem.c
@@ -0,0 +1,50 @@
+#include <fwd/mod.h>
+
+long fwdmalloc(fwd_extern_args_t args)
+{
+ assert(args.argc == 1);
+ size_t n = FWD_ARG_T(args, 0, size_t);
+ FWD_RET(FWD_PTR, malloc(n));
+ return 0;
+}
+
+long fwdrealloc(fwd_extern_args_t args)
+{
+ assert(args.argc == 2);
+ void *ptr = FWD_ARG(args, 0, void *, FWD_PTR);
+ size_t n = FWD_ARG_T(args, 1, size_t);
+ FWD_RET(FWD_PTR, realloc(ptr, n));
+ return 0;
+}
+
+long fwdfree(fwd_extern_args_t args)
+{
+ assert(args.argc == 1);
+ void *ptr = FWD_ARG(args, 0, void *, FWD_PTR);
+ return 0;
+}
+
+long fwdptradd(fwd_extern_args_t args)
+{
+ assert(args.argc == 2);
+ void *ptr = FWD_ARG(args, 0, void *, FWD_PTR);
+ int64_t add = FWD_ARG_T(args, 1, int64_t);
+ FWD_RET(FWD_PTR, (char *)ptr + add);
+ return 0;
+}
+
+int fwdopen(fwd_state_t *state)
+{
+ FWD_REGISTER(state, fwdmalloc,
+ FWD_PTR, FWD_T(size_t));
+
+ FWD_REGISTER(state, fwdrealloc,
+ FWD_PTR, FWD_PTR, FWD_T(size_t));
+
+ FWD_REGISTER(state, fwdfree,
+ FWD_VOID, FWD_PTR);
+
+ FWD_REGISTER(state, fwdptradd,
+ FWD_PTR, FWD_PTR, FWD_T(int64_t));
+ return 0;
+}
diff --git a/mod/util.c b/mod/util.c
new file mode 100644
index 0000000..c967349
--- /dev/null
+++ b/mod/util.c
@@ -0,0 +1,20 @@
+#include <fwd/mod.h>
+#include <stdio.h>
+
+long fwdpanic(fwd_extern_args_t args)
+{
+ assert(args.argc == 1);
+ char *str = FWD_ARG_T(args, 0, char *);
+ fprintf(stderr, "%s", str);
+ exit(1);
+ return 0;
+}
+
+int fwdopen(fwd_state_t *state)
+{
+ /** @todo passing around strings might be common enough to warrant its
+ * own type? */
+ FWD_REGISTER(state, fwdpanic,
+ FWD_VOID, FWD_PTR);
+ return 0;
+}