summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2016-10-07 11:27:44 -0400
committerJune McEnroe <june@causal.agency>2016-10-07 11:27:44 -0400
commit0b045ed7e1f2d37195732fcd634a08fd05a922d5 (patch)
tree9f1d4f42eb604739486acfec849d773fea5dbeee
parentAdd OP_HIGH to jrp (diff)
downloadsrc-0b045ed7e1f2d37195732fcd634a08fd05a922d5.tar.gz
src-0b045ed7e1f2d37195732fcd634a08fd05a922d5.zip
Add runtime functions to jrp
The current JITed code causes a misaligned stack error when calling into
C code. Need to find a way to align the RPN stack to 16 bytes, or swap
back to the C stack before a CALL.
-rwxr-xr-x.bin/jrp.c55
1 files changed, 52 insertions, 3 deletions
diff --git a/.bin/jrp.c b/.bin/jrp.c
index daeb1477..b98f750d 100755
--- a/.bin/jrp.c
+++ b/.bin/jrp.c
@@ -10,6 +10,7 @@ exec cc -Wall -Wextra $@ -o $(dirname $0)/jrp $0
 
 typedef unsigned long long op;
 typedef long long value;
+typedef unsigned long long uvalue;
 typedef value *(*fptr)(value *);
 
 enum {
@@ -38,6 +39,44 @@ enum {
 #define IMMED_PUSH(x) ((op)(x) << 32)
 #define IMMED_HIGH(x) ((op)(x) & 0xffffffff00000000)
 
+static void rt_print_ascii(value val) {
+    printf("%c\n", (char)val);
+}
+
+static void rt_print_bin(value val) {
+    static char buf[sizeof(value) * 8 + 1];
+    uvalue uval = val;
+    int i = sizeof(buf);
+    do {
+        buf[--i] = '0' + (uval & 1);
+    } while (uval >>= 1);
+    printf("%s\n", &buf[i]);
+}
+
+static void rt_print_oct(value val) {
+    printf("%llo\n", val);
+}
+
+static void rt_print_dec(value val) {
+    printf("%lld\n", val);
+}
+
+static void rt_print_hex(value val) {
+    printf("%llx\n", val);
+}
+
+#define JIT_PUSH(p, val) { \
+    *p++ = OP_PUSH | IMMED_PUSH(val); \
+    if (IMMED_HIGH(val)) { \
+        *p++ = OP_HIGH | IMMED_HIGH(val); \
+    } \
+}
+
+#define JIT_CALL(p, fn) { \
+    JIT_PUSH(p, fn); \
+    *p++ = OP_CALL; \
+}
+
 int main() {
     int error;
     int page = getpagesize();
@@ -51,11 +90,21 @@ int main() {
 
     op *p = ops;
     *p++ = OP_PROL;
-    *p++ = OP_PUSH | IMMED_PUSH(1);
-    *p++ = OP_PUSH | IMMED_PUSH(2);
+    JIT_PUSH(p, 7);
+    JIT_PUSH(p, 10);
+    JIT_PUSH(p, 9);
+    *p++ = OP_MUL;
     *p++ = OP_ADD;
     *p++ = OP_DUP;
-    *p++ = OP_MUL;
+    *p++ = OP_DUP;
+    *p++ = OP_DUP;
+    *p++ = OP_DUP;
+    *p++ = OP_DUP;
+    JIT_CALL(p, rt_print_ascii);
+    JIT_CALL(p, rt_print_bin);
+    JIT_CALL(p, rt_print_oct);
+    JIT_CALL(p, rt_print_dec);
+    JIT_CALL(p, rt_print_hex);
     *p++ = OP_EPIL;
 
     error = mprotect(ops, page, PROT_READ | PROT_EXEC);