on master help sign in
Merge pull request #1300 from stepcut/master

modifications to RTS to support 8-bit platforms

commit504038db9e9e00423e0d9ed4aee5e9e0f1f6740f authorEdwin Brady <edwin.brady@gmail.com> dateMon 9 Jun 2014, 12:52 (12 years ago) parent13a6bf431b2346
5 files changed+56−25
config.mk+1−2 rts/idris_gc.c+11−2 rts/idris_heap.c+8−1 rts/idris_rts.c+23−11 rts/idris_rts.h+13−9
config.mk+1−2
@@ -1,6 +1,6 @@
11CC ?=cc
22CABAL :=cabal
3−CFLAGS :=-O2 -Wall $(CFLAGS)
3+CFLAGS :=-O2 -Wall -DHAS_PTHREAD $(CFLAGS)
44#CABALFLAGS :=
55## Disable building of Effects
66#CABALFLAGS :=-f NoEffects
@@ -35,4 +35,3 @@ else
3535 SHLIB_SUFFIX :=.so
3636endif
3737endif
38−
rts/idris_gc.c+11−2
@@ -103,7 +103,14 @@ void idris_gc(VM* vm) {
103103 free(vm->heap.old);
104104
105105 vm->heap.heap = newheap;
106− vm->heap.next = newheap;
106+#ifdef FORCE_ALIGNMENT
107+ if (((i_int)(vm->heap.heap)&1) == 1) {
108+ vm->heap.next = newheap + 1;
109+ } else
110+#endif
111+ {
112+ vm->heap.next = newheap;
113+ }
107114 vm->heap.end = newheap + vm->heap.size;
108115
109116 VAL* root;
@@ -111,10 +118,12 @@ void idris_gc(VM* vm) {
111118 for(root = vm->valstack; root < vm->valstack_top; ++root) {
112119 *root = copy(vm, *root);
113120 }
121+
122+#ifdef HAS_PTHREAD
114123 for(root = vm->inbox_ptr; root < vm->inbox_write; ++root) {
115124 *root = copy(vm, *root);
116125 }
117−
126+#endif
118127 vm->ret = copy(vm, vm->ret);
119128 vm->reg1 = copy(vm, vm->reg1);
120129
rts/idris_heap.c+8−1
@@ -16,7 +16,14 @@ void alloc_heap(Heap * h, size_t heap_size)
1616 }
1717
1818 h->heap = mem;
19− h->next = h->heap;
19+#ifdef FORCE_ALIGNMENT
20+ if (((i_int)(h->heap)&1) == 1) {
21+ h->next = h->heap + 1;
22+ } else
23+#endif
24+ {
25+ h->next = h->heap;
26+ }
2027 h->end = h->heap + heap_size;
2128
2229 h->size = heap_size;
rts/idris_rts.c+23−11
@@ -1,10 +1,11 @@
11#include <stdlib.h>
22#include <stdio.h>
33#include <string.h>
4−#include <unistd.h>
54#include <stdarg.h>
65#include <assert.h>
6+#ifdef HAS_PTHREAD
77#include <pthread.h>
8+#endif
89
910#include "idris_rts.h"
1011#include "idris_gc.h"
@@ -30,7 +31,7 @@ VM* init_vm(int stack_size, size_t heap_size,
3031
3132 vm->ret = NULL;
3233 vm->reg1 = NULL;
33−
34+#ifdef HAS_PTHREAD
3435 vm->inbox = malloc(1024*sizeof(VAL));
3536 memset(vm->inbox, 0, 1024*sizeof(VAL));
3637 vm->inbox_end = vm->inbox + 1024;
@@ -53,7 +54,7 @@ VM* init_vm(int stack_size, size_t heap_size,
5354
5455 vm->max_threads = max_threads;
5556 vm->processes = 0;
56−
57+#endif
5758 STATS_LEAVE_INIT(vm->stats)
5859 return vm;
5960}
@@ -61,14 +62,16 @@ VM* init_vm(int stack_size, size_t heap_size,
6162Stats terminate(VM* vm) {
6263 Stats stats = vm->stats;
6364 STATS_ENTER_EXIT(stats)
64−
65+#ifdef HAS_PTHREAD
6566 free(vm->inbox);
67+#endif
6668 free(vm->valstack);
6769 free_heap(&(vm->heap));
68−
70+#ifdef HAS_PTHREAD
6971 pthread_mutex_destroy(&(vm -> inbox_lock));
7072 pthread_mutex_destroy(&(vm -> inbox_block));
7173 pthread_cond_destroy(&(vm -> inbox_waiting));
74+#endif
7275 free(vm);
7376
7477 STATS_LEAVE_EXIT(stats)
@@ -79,18 +82,21 @@ void idris_requireAlloc(VM* vm, size_t size) {
7982 if (!(vm->heap.next + size < vm->heap.end)) {
8083 idris_gc(vm);
8184 }
82−
85+#ifdef HAS_PTHREAD
8386 int lock = vm->processes > 0;
8487 if (lock) { // We only need to lock if we're in concurrent mode
8588 pthread_mutex_lock(&vm->alloc_lock);
8689 }
90+#endif
8791}
8892
8993void idris_doneAlloc(VM* vm) {
94+#ifdef HAS_PTHREAD
9095 int lock = vm->processes > 0;
9196 if (lock) { // We only need to lock if we're in concurrent mode
9297 pthread_mutex_unlock(&vm->alloc_lock);
9398 }
99+#endif
94100}
95101
96102int space(VM* vm, size_t size) {
@@ -99,16 +105,18 @@ int space(VM* vm, size_t size) {
99105
100106void* allocate(VM* vm, size_t size, int outerlock) {
101107// return malloc(size);
108+#ifdef HAS_PTHREAD
102109 int lock = vm->processes > 0 && !outerlock;
103110
104111 if (lock) { // not message passing
105112 pthread_mutex_lock(&vm->alloc_lock);
106113 }
114+#endif
107115
108116 if ((size & 7)!=0) {
109117 size = 8 + ((size >> 3) << 3);
110118 }
111−
119+
112120 size_t chunk_size = size + sizeof(size_t);
113121
114122 if (vm->heap.next + chunk_size < vm->heap.end) {
@@ -120,16 +128,19 @@ void* allocate(VM* vm, size_t size, int outerlock) {
120128 assert(vm->heap.next <= vm->heap.end);
121129
122130 memset(ptr, 0, size);
123−
131+#ifdef HAS_PTHREAD
124132 if (lock) { // not message passing
125133 pthread_mutex_unlock(&vm->alloc_lock);
126134 }
135+#endif
127136 return ptr;
128137 } else {
129138 idris_gc(vm);
139+#ifdef HAS_PTHREAD
130140 if (lock) { // not message passing
131141 pthread_mutex_unlock(&vm->alloc_lock);
132142 }
143+#endif
133144 return allocate(vm, size, 0);
134145 }
135146
@@ -187,7 +198,7 @@ VAL MKPTR(VM* vm, void* ptr) {
187198 return cl;
188199}
189200
190−VAL MKMPTR(VM* vm, void* ptr, int size) {
201+VAL MKMPTR(VM* vm, void* ptr, size_t size) {
191202 Closure* cl = allocate(vm, sizeof(Closure) +
192203 sizeof(ManagedPtr) + size, 0);
193204 SETTY(cl, MANAGEDPTR);
@@ -222,7 +233,7 @@ VAL MKPTRc(VM* vm, void* ptr) {
222233 return cl;
223234}
224235
225−VAL MKMPTRc(VM* vm, void* ptr, int size) {
236+VAL MKMPTRc(VM* vm, void* ptr, size_t size) {
226237 Closure* cl = allocate(vm, sizeof(Closure) +
227238 sizeof(ManagedPtr) + size, 1);
228239 SETTY(cl, MANAGEDPTR);
@@ -760,6 +771,7 @@ typedef struct {
760771 VAL arg;
761772} ThreadData;
762773
774+#ifdef HAS_PTHREAD
763775void* runThread(void* arg) {
764776 ThreadData* td = (ThreadData*)arg;
765777 VM* vm = td->vm;
@@ -949,7 +961,7 @@ VAL idris_recvMessage(VM* vm) {
949961
950962 return msg;
951963}
952−
964+#endif
953965int __idris_argc;
954966char **__idris_argv;
955967
rts/idris_rts.h+13−9
@@ -4,9 +4,10 @@
44#include <stdlib.h>
55#include <stdio.h>
66#include <string.h>
7−#include <unistd.h>
87#include <stdarg.h>
8+#ifdef HAS_PTHREAD
99#include <pthread.h>
10+#endif
1011#include <stdint.h>
1112
1213#include "idris_heap.h"
@@ -23,13 +24,13 @@ typedef enum {
2324typedef struct Closure *VAL;
2425
2526typedef struct {
26− int tag_arity;
27+ uint32_t tag_arity;
2728 VAL args[];
2829} con;
2930
3031typedef struct {
3132 VAL str;
32− int offset;
33+ size_t offset;
3334} StrOffset;
3435
3536typedef struct {
@@ -49,7 +50,10 @@ typedef struct {
4950typedef struct Closure {
5051// Use top 16 bits of ty for saying which heap value is in
5152// Bottom 16 bits for closure type
52− ClosureType ty;
53+//
54+// NOTE: ty can not have type ClosureType because ty must be a
55+// uint32_t but enum is platform dependent
56+ uint32_t ty;
5357 union {
5458 con c;
5559 int i;
@@ -73,7 +77,7 @@ typedef struct {
7377 VAL* stack_max;
7478
7579 Heap heap;
76−
80+#ifdef HAS_PTHREAD
7781 pthread_mutex_t inbox_lock;
7882 pthread_mutex_t inbox_block;
7983 pthread_mutex_t alloc_lock;
@@ -87,7 +91,7 @@ typedef struct {
8791
8892 int processes; // Number of child processes
8993 int max_threads; // maximum number of threads to run in parallel
90−
94+#endif
9195 Stats stats;
9296
9397 VAL ret;
@@ -170,7 +174,7 @@ typedef intptr_t i_int;
170174VAL MKFLOAT(VM* vm, double val);
171175VAL MKSTR(VM* vm, const char* str);
172176VAL MKPTR(VM* vm, void* ptr);
173−VAL MKMPTR(VM* vm, void* ptr, int size);
177+VAL MKMPTR(VM* vm, void* ptr, size_t size);
174178VAL MKB8(VM* vm, uint8_t b);
175179VAL MKB16(VM* vm, uint16_t b);
176180VAL MKB32(VM* vm, uint32_t b);
@@ -181,7 +185,7 @@ VAL MKFLOATc(VM* vm, double val);
181185VAL MKSTROFFc(VM* vm, StrOffset* off);
182186VAL MKSTRc(VM* vm, char* str);
183187VAL MKPTRc(VM* vm, void* ptr);
184−VAL MKMPTRc(VM* vm, void* ptr, int size);
188+VAL MKMPTRc(VM* vm, void* ptr, size_t size);
185189VAL MKBUFFERc(VM* vm, Buffer* buf);
186190
187191char* GETSTROFF(VAL stroff);
@@ -208,7 +212,7 @@ void idris_doneAlloc(VM* vm);
208212#define allocCon(cl, vm, t, a, o) \
209213 cl = allocate(vm, sizeof(Closure) + sizeof(VAL)*a, o); \
210214 SETTY(cl, CON); \
211− cl->info.c.tag_arity = ((t) << 8) + (a);
215+ cl->info.c.tag_arity = ((t) << 8) | (a);
212216
213217void* vmThread(VM* callvm, func f, VAL arg);
214218