From 9caee0c52637f445a0aa043f9001dc258f704190 Mon Sep 17 00:00:00 2001 From: Dmitry Boulytchev Date: Thu, 25 Oct 2018 03:15:24 +0300 Subject: [PATCH] Arithmetic corrections --- regression/Makefile | 4 ++-- regression/expressions/Makefile | 6 +++--- runtime/runtime.c | 26 ++++++++++++++++---------- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/regression/Makefile b/regression/Makefile index 31e136a99..0fc4b3f96 100644 --- a/regression/Makefile +++ b/regression/Makefile @@ -8,8 +8,8 @@ check: $(TESTS) $(TESTS): %: %.expr @$(RC) $< && cat $@.input | ./$@ > $@.log && diff $@.log orig/$@.log - cat $@.input | $(RC) -i $< > $@.log && diff $@.log orig/$@.log - cat $@.input | $(RC) -s $< > $@.log && diff $@.log orig/$@.log + @cat $@.input | $(RC) -i $< > $@.log && diff $@.log orig/$@.log + @cat $@.input | $(RC) -s $< > $@.log && diff $@.log orig/$@.log clean: rm -f test*.log *.s *~ $(TESTS) diff --git a/regression/expressions/Makefile b/regression/expressions/Makefile index 0ffefb2f6..fe0e5468b 100644 --- a/regression/expressions/Makefile +++ b/regression/expressions/Makefile @@ -7,9 +7,9 @@ RC = ../../src/rc.opt check: $(TESTS) $(TESTS): %: %.expr - RC_RUNTIME=../../runtime $(RC) $< && cat $@.input | ./$@ > $@.log && diff $@.log orig/$@.log -# @cat $@.input | $(RC) -i $< > $@.log && diff $@.log orig/$@.log -# @cat $@.input | $(RC) -s $< > $@.log && diff $@.log orig/$@.log + @RC_RUNTIME=../../runtime $(RC) $< && cat $@.input | ./$@ > $@.log && diff $@.log orig/$@.log + @cat $@.input | $(RC) -i $< > $@.log && diff $@.log orig/$@.log + @cat $@.input | $(RC) -s $< > $@.log && diff $@.log orig/$@.log clean: rm -f *.log *.s *~ $(TESTS) diff --git a/runtime/runtime.c b/runtime/runtime.c index 67b0fd7ee..8fb1e1b49 100644 --- a/runtime/runtime.c +++ b/runtime/runtime.c @@ -17,6 +17,9 @@ # define TO_DATA(x) ((data*)((char*)(x)-sizeof(int))) # define TO_SEXP(x) ((sexp*)((char*)(x)-2*sizeof(int))) +# define UNBOX(x) (((int) (x)) >> 1) +# define BOX(x) ((((int) (x)) << 1) | 0x0001) + typedef struct { int tag; char contents[0]; @@ -29,15 +32,18 @@ typedef struct { extern int Blength (void *p) { data *a = TO_DATA(p); - return LEN(a->tag); + return BOX(LEN(a->tag)); } extern void* Belem (void *p, int i) { data *a = TO_DATA(p); + i = UNBOX(i); + + /* printf ("elem %d = %p\n", i, (void*) ((int*) a->contents)[i]); */ - if (TAG(a->tag) == STRING_TAG) return (void*)(int)(a->contents[i]); - - //printf ("elem %d = %p\n", i, (void*) ((int*) a->contents)[i]); + if (TAG(a->tag) == STRING_TAG) { + return (void*) BOX(a->contents[i]); + } return (void*) ((int*) a->contents)[i]; } @@ -98,7 +104,7 @@ extern void* Bsexp (int n, ...) { extern int Btag (void *d, int t) { data *r = TO_DATA(d); - return TAG(r->tag) == SEXP_TAG && TO_SEXP(d)->tag == t; + return ((TAG(r->tag) == SEXP_TAG && TO_SEXP(d)->tag == t) << 1) | 1; } extern void Bsta (int n, int v, void *s, ...) { @@ -109,14 +115,14 @@ extern void Bsta (int n, int v, void *s, ...) { va_start(args, s); for (i=0; itag) == STRING_TAG)((char*) s)[k] = (char) v; + if (TAG(a->tag) == STRING_TAG)((char*) s)[k] = (char) (v >> 1); else ((int*) s)[k] = v; } @@ -170,12 +176,12 @@ extern int Lread () { fflush (stdout); scanf ("%d", &result); - return (result << 1) | 0x0001; + return BOX(result); } /* Lwrite is an implementation of the "write" construct */ extern int Lwrite (int n) { - printf ("%d\n", n >> 1); + printf ("%d\n", UNBOX(n)); fflush (stdout); return 0;