Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 190 lines (142 sloc) 4.746 kB
b70780f rename rts include file
Edwin Brady authored
1 #ifndef _IDRISRTS_H
2 #define _IDRISRTS_H
ffc7b69 First baby steps towards a new RTS (and towards removing epic depende…
Edwin Brady authored
3
204d938 Starting to make new RTS work
Edwin Brady authored
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <stdarg.h>
c02a58c Added fork (but not working properly yet)
Edwin Brady authored
9 #include <pthread.h>
ffc7b69 First baby steps towards a new RTS (and towards removing epic depende…
Edwin Brady authored
10
11 // Closures
204d938 Starting to make new RTS work
Edwin Brady authored
12
13 typedef enum {
314246b Make getArgs work with new backend
Edwin Brady authored
14 CON, INT, BIGINT, FLOAT, STRING, UNIT, PTR, FWD
204d938 Starting to make new RTS work
Edwin Brady authored
15 } ClosureType;
16
17 typedef struct {
18 int tag;
19 int arity;
20 void* args;
21 } con;
22
23 typedef struct {
24 ClosureType ty;
25 union {
26 con c;
27 int i;
28 double f;
29 char* str;
30 void* ptr;
31 } info;
32 } Closure;
33
34 typedef Closure* VAL;
ffc7b69 First baby steps towards a new RTS (and towards removing epic depende…
Edwin Brady authored
35
36 typedef struct {
37 VAL* valstack;
38 int* intstack;
39 double* floatstack;
204d938 Starting to make new RTS work
Edwin Brady authored
40 VAL* valstack_top;
41 VAL* valstack_base;
ffc7b69 First baby steps towards a new RTS (and towards removing epic depende…
Edwin Brady authored
42 int* intstack_ptr;
43 double* floatstack_ptr;
3009319 Added a simple copying GC
Edwin Brady authored
44 char* heap;
f2766a6 Fix GC error
Edwin Brady authored
45 char* oldheap;
3009319 Added a simple copying GC
Edwin Brady authored
46 char* heap_next;
47 char* heap_end;
6b46ffc New RTS almost ready, but for garbage collection problem
Edwin Brady authored
48 VAL* stack_max;
c02a58c Added fork (but not working properly yet)
Edwin Brady authored
49
50 pthread_mutex_t inbox_lock;
51 pthread_mutex_t inbox_block;
52 pthread_cond_t inbox_waiting;
53
54 VAL* inbox; // Block of memory for storing messages
55 VAL* inbox_end; // End of block of memory
56
57 VAL* inbox_ptr; // Next message to read
58 VAL* inbox_write; // Location of next message to write
59
314246b Make getArgs work with new backend
Edwin Brady authored
60 int argc;
61 VAL* argv; // command line arguments
62
3009319 Added a simple copying GC
Edwin Brady authored
63 size_t heap_size;
64 size_t heap_growth;
f2766a6 Fix GC error
Edwin Brady authored
65 int allocations;
3009319 Added a simple copying GC
Edwin Brady authored
66 int collections;
2ec60c8 Added binding form to syntax rules (and updated tutorial accordingly)
Edwin Brady authored
67 VAL ret;
6b46ffc New RTS almost ready, but for garbage collection problem
Edwin Brady authored
68 VAL reg1;
ffc7b69 First baby steps towards a new RTS (and towards removing epic depende…
Edwin Brady authored
69 } VM;
70
c02a58c Added fork (but not working properly yet)
Edwin Brady authored
71 // Create a new VM
314246b Make getArgs work with new backend
Edwin Brady authored
72 VM* init_vm(int stack_size, size_t heap_size, int argc, char* argv[]);
c02a58c Added fork (but not working properly yet)
Edwin Brady authored
73 // Clean up a VM once it's no longer needed
74 void terminate(VM* vm);
ffc7b69 First baby steps towards a new RTS (and towards removing epic depende…
Edwin Brady authored
75
204d938 Starting to make new RTS work
Edwin Brady authored
76 // Functions all take a pointer to their VM, and previous stack base,
77 // and return nothing.
78 typedef void(*func)(VM*, VAL*);
ffc7b69 First baby steps towards a new RTS (and towards removing epic depende…
Edwin Brady authored
79
204d938 Starting to make new RTS work
Edwin Brady authored
80 // Register access
2ec60c8 Added binding form to syntax rules (and updated tutorial accordingly)
Edwin Brady authored
81
204d938 Starting to make new RTS work
Edwin Brady authored
82 #define RVAL (vm->ret)
83 #define LOC(x) (*(vm->valstack_base + (x)))
84 #define TOP(x) (*(vm->valstack_top + (x)))
6b46ffc New RTS almost ready, but for garbage collection problem
Edwin Brady authored
85 #define REG1 (vm->reg1)
ffc7b69 First baby steps towards a new RTS (and towards removing epic depende…
Edwin Brady authored
86
204d938 Starting to make new RTS work
Edwin Brady authored
87 // Retrieving values
ffc7b69 First baby steps towards a new RTS (and towards removing epic depende…
Edwin Brady authored
88
204d938 Starting to make new RTS work
Edwin Brady authored
89 #define GETSTR(x) (((VAL)(x))->info.str)
d4df20e More primitives; started FFI in IRTS
Edwin Brady authored
90 #define GETPTR(x) (((VAL)(x))->info.ptr)
91 #define GETFLOAT(x) (((VAL)(x))->info.f)
ffc7b69 First baby steps towards a new RTS (and towards removing epic depende…
Edwin Brady authored
92
b49000a Support linking external .o files in new compiler
Edwin Brady authored
93 #define TAG(x) (ISINT(x) || x == NULL ? (-1) : ( (x)->ty == CON ? (x)->info.c.tag : (-1)) )
204d938 Starting to make new RTS work
Edwin Brady authored
94
95 // Integers, floats and operators
ffc7b69 First baby steps towards a new RTS (and towards removing epic depende…
Edwin Brady authored
96
204d938 Starting to make new RTS work
Edwin Brady authored
97 typedef intptr_t i_int;
ffc7b69 First baby steps towards a new RTS (and towards removing epic depende…
Edwin Brady authored
98
204d938 Starting to make new RTS work
Edwin Brady authored
99 #define MKINT(x) ((void*)((x)<<1)+1)
100 #define GETINT(x) ((i_int)(x)>>1)
101 #define ISINT(x) ((((i_int)x)&1) == 1)
ffc7b69 First baby steps towards a new RTS (and towards removing epic depende…
Edwin Brady authored
102
204d938 Starting to make new RTS work
Edwin Brady authored
103 #define INTOP(op,x,y) MKINT((i_int)((((i_int)x)>>1) op (((i_int)y)>>1)))
ef3aed2 Fix FLOATOP properly
Edwin Brady authored
104 #define FLOATOP(op,x,y) MKFLOAT(vm, ((GETFLOAT(x)) op (GETFLOAT(y))))
204d938 Starting to make new RTS work
Edwin Brady authored
105 #define FLOATBOP(op,x,y) MKINT((i_int)(((GETFLOAT(x)) op (GETFLOAT(y)))))
106 #define ADD(x,y) (void*)(((i_int)x)+(((i_int)y)-1))
107 #define MULT(x,y) (MKINT((((i_int)x)>>1) * (((i_int)y)>>1)))
ffc7b69 First baby steps towards a new RTS (and towards removing epic depende…
Edwin Brady authored
108
204d938 Starting to make new RTS work
Edwin Brady authored
109 // Stack management
ffc7b69 First baby steps towards a new RTS (and towards removing epic depende…
Edwin Brady authored
110
204d938 Starting to make new RTS work
Edwin Brady authored
111 #define INITFRAME VAL* myoldbase
112 #define REBASE vm->valstack_base = oldbase
6b46ffc New RTS almost ready, but for garbage collection problem
Edwin Brady authored
113 #define RESERVE(x) if (vm->valstack_top+(x) > vm->stack_max) { stackOverflow(); } \
f959008 @edwinb New RTS in place
authored
114 else { bzero(vm->valstack_top, (x)*sizeof(VAL)); }
204d938 Starting to make new RTS work
Edwin Brady authored
115 #define ADDTOP(x) vm->valstack_top += (x)
116 #define TOPBASE(x) vm->valstack_top = vm->valstack_base + (x)
117 #define BASETOP(x) vm->valstack_base = vm->valstack_top + (x)
118 #define STOREOLD myoldbase = vm->valstack_base
119
120 #define CALL(f) f(vm, myoldbase);
121 #define TAILCALL(f) f(vm, oldbase);
ffc7b69 First baby steps towards a new RTS (and towards removing epic depende…
Edwin Brady authored
122
123 // Creating new values (each value placed at the top of the stack)
204d938 Starting to make new RTS work
Edwin Brady authored
124 VAL MKFLOAT(VM* vm, double val);
125 VAL MKSTR(VM* vm, char* str);
d4df20e More primitives; started FFI in IRTS
Edwin Brady authored
126 VAL MKPTR(VM* vm, void* ptr);
204d938 Starting to make new RTS work
Edwin Brady authored
127
6b46ffc New RTS almost ready, but for garbage collection problem
Edwin Brady authored
128 VAL MKCON(VM* vm, VAL cl, int tag, int arity, ...);
129
130 #define SETTAG(x, a) (x)->info.c.tag = (a)
131 #define SETARG(x, i, a) ((VAL*)((x)->info.c.args))[i] = ((VAL)(a))
0f957f6 @edwinb Add Proj operator to runtime version of TT
authored
132 #define GETARG(x, i) ((VAL*)((x)->info.c.args))[i]
ffc7b69 First baby steps towards a new RTS (and towards removing epic depende…
Edwin Brady authored
133
204d938 Starting to make new RTS work
Edwin Brady authored
134 void PROJECT(VM* vm, VAL r, int loc, int arity);
ccc0f67 Added SLIDE for tail calls
Edwin Brady authored
135 void SLIDE(VM* vm, int args);
ffc7b69 First baby steps towards a new RTS (and towards removing epic depende…
Edwin Brady authored
136
0af107c Added GMP interface for BigInts
Edwin Brady authored
137 void* allocate(VM* vm, size_t size);
3009319 Added a simple copying GC
Edwin Brady authored
138 void* allocCon(VM* vm, int arity);
0af107c Added GMP interface for BigInts
Edwin Brady authored
139
c02a58c Added fork (but not working properly yet)
Edwin Brady authored
140 void* vmThread(VM* callvm, func f, VAL arg);
141
142 // Copy a structure to another vm's heap
143 VAL copyTo(VM* newVM, VAL x);
144
145 // Add a message to another VM's message queue
146 void sendMessage(VM* sender, VM* dest, VAL msg);
147 // block until there is a message in the queue
148 VAL recvMessage(VM* vm);
149
204d938 Starting to make new RTS work
Edwin Brady authored
150 void dumpVal(VAL r);
6b46ffc New RTS almost ready, but for garbage collection problem
Edwin Brady authored
151 void dumpStack(VM* vm);
38a9365 More towards the new compiler
Edwin Brady authored
152
d4df20e More primitives; started FFI in IRTS
Edwin Brady authored
153 // Casts
154
155 #define idris_castIntFloat(x) MKFLOAT(vm, (double)(GETINT(x)))
156 #define idris_castFloatInt(x) MKINT((i_int)(GETFLOAT(x)))
157
158 VAL idris_castIntStr(VM* vm, VAL i);
159 VAL idris_castStrInt(VM* vm, VAL i);
160 VAL idris_castFloatStr(VM* vm, VAL i);
161 VAL idris_castStrFloat(VM* vm, VAL i);
162
163 // String primitives
164
165 VAL idris_concat(VM* vm, VAL l, VAL r);
166 VAL idris_strlt(VM* vm, VAL l, VAL r);
167 VAL idris_streq(VM* vm, VAL l, VAL r);
168 VAL idris_strlen(VM* vm, VAL l);
169 VAL idris_readStr(VM* vm, FILE* h);
170
7ab5216 String/Float primitives
Edwin Brady authored
171 VAL idris_strHead(VM* vm, VAL str);
172 VAL idris_strTail(VM* vm, VAL str);
173 VAL idris_strCons(VM* vm, VAL x, VAL xs);
174 VAL idris_strIndex(VM* vm, VAL str, VAL i);
175 VAL idris_strRev(VM* vm, VAL str);
176
314246b Make getArgs work with new backend
Edwin Brady authored
177 // Command line args
178
179 int idris_numArgs(VM* vm);
180 VAL idris_getArg(VM* vm, int i);
181
2ec60c8 Added binding form to syntax rules (and updated tutorial accordingly)
Edwin Brady authored
182 // Handle stack overflow.
183 // Just reports an error and exits.
184
185 void stackOverflow();
186
0af107c Added GMP interface for BigInts
Edwin Brady authored
187 #include "idris_gmp.h"
3009319 Added a simple copying GC
Edwin Brady authored
188
ffc7b69 First baby steps towards a new RTS (and towards removing epic depende…
Edwin Brady authored
189 #endif
Something went wrong with that request. Please try again.