Mercurial > repos > rhope
annotate runtime/context.c @ 176:327bcf35e094
Add bytecode interpreter
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Wed, 08 Jun 2011 23:24:29 -0700 |
parents | 7bbdc034e347 |
children | ba35ab624ec2 |
rev | line source |
---|---|
8
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1 #include "context.h" |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
2 #include "object.h" |
140
c14698c512f1
Untested addition of Pause/Resume
Mike Pavone <pavone@retrodev.com>
parents:
139
diff
changeset
|
3 #include "thread.h" |
8
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
4 #include <stdlib.h> |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
5 #include <stddef.h> |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
6 #include <stdio.h> |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
7 |
140
c14698c512f1
Untested addition of Pause/Resume
Mike Pavone <pavone@retrodev.com>
parents:
139
diff
changeset
|
8 context * contextqueue[32]; |
c14698c512f1
Untested addition of Pause/Resume
Mike Pavone <pavone@retrodev.com>
parents:
139
diff
changeset
|
9 int32_t cq_readloc=0; |
c14698c512f1
Untested addition of Pause/Resume
Mike Pavone <pavone@retrodev.com>
parents:
139
diff
changeset
|
10 int32_t cq_writeloc=0; |
c14698c512f1
Untested addition of Pause/Resume
Mike Pavone <pavone@retrodev.com>
parents:
139
diff
changeset
|
11 rh_mutex(cq_lock) |
c14698c512f1
Untested addition of Pause/Resume
Mike Pavone <pavone@retrodev.com>
parents:
139
diff
changeset
|
12 |
c14698c512f1
Untested addition of Pause/Resume
Mike Pavone <pavone@retrodev.com>
parents:
139
diff
changeset
|
13 void cqueue_init() |
c14698c512f1
Untested addition of Pause/Resume
Mike Pavone <pavone@retrodev.com>
parents:
139
diff
changeset
|
14 { |
c14698c512f1
Untested addition of Pause/Resume
Mike Pavone <pavone@retrodev.com>
parents:
139
diff
changeset
|
15 rh_mutex_init(cq_lock); |
c14698c512f1
Untested addition of Pause/Resume
Mike Pavone <pavone@retrodev.com>
parents:
139
diff
changeset
|
16 } |
c14698c512f1
Untested addition of Pause/Resume
Mike Pavone <pavone@retrodev.com>
parents:
139
diff
changeset
|
17 |
c14698c512f1
Untested addition of Pause/Resume
Mike Pavone <pavone@retrodev.com>
parents:
139
diff
changeset
|
18 context * get_cqueue() |
c14698c512f1
Untested addition of Pause/Resume
Mike Pavone <pavone@retrodev.com>
parents:
139
diff
changeset
|
19 { |
c14698c512f1
Untested addition of Pause/Resume
Mike Pavone <pavone@retrodev.com>
parents:
139
diff
changeset
|
20 context * ret; |
c14698c512f1
Untested addition of Pause/Resume
Mike Pavone <pavone@retrodev.com>
parents:
139
diff
changeset
|
21 rh_lock(cq_lock); |
c14698c512f1
Untested addition of Pause/Resume
Mike Pavone <pavone@retrodev.com>
parents:
139
diff
changeset
|
22 if (cq_readloc == cq_writeloc) |
c14698c512f1
Untested addition of Pause/Resume
Mike Pavone <pavone@retrodev.com>
parents:
139
diff
changeset
|
23 { |
c14698c512f1
Untested addition of Pause/Resume
Mike Pavone <pavone@retrodev.com>
parents:
139
diff
changeset
|
24 rh_unlock(cq_lock); |
c14698c512f1
Untested addition of Pause/Resume
Mike Pavone <pavone@retrodev.com>
parents:
139
diff
changeset
|
25 return NULL; |
c14698c512f1
Untested addition of Pause/Resume
Mike Pavone <pavone@retrodev.com>
parents:
139
diff
changeset
|
26 } |
c14698c512f1
Untested addition of Pause/Resume
Mike Pavone <pavone@retrodev.com>
parents:
139
diff
changeset
|
27 ret = contextqueue[cq_readloc++]; |
c14698c512f1
Untested addition of Pause/Resume
Mike Pavone <pavone@retrodev.com>
parents:
139
diff
changeset
|
28 if (cq_readloc == 32) |
c14698c512f1
Untested addition of Pause/Resume
Mike Pavone <pavone@retrodev.com>
parents:
139
diff
changeset
|
29 cq_readloc = 0; |
c14698c512f1
Untested addition of Pause/Resume
Mike Pavone <pavone@retrodev.com>
parents:
139
diff
changeset
|
30 rh_unlock(cq_lock); |
c14698c512f1
Untested addition of Pause/Resume
Mike Pavone <pavone@retrodev.com>
parents:
139
diff
changeset
|
31 return ret; |
c14698c512f1
Untested addition of Pause/Resume
Mike Pavone <pavone@retrodev.com>
parents:
139
diff
changeset
|
32 } |
c14698c512f1
Untested addition of Pause/Resume
Mike Pavone <pavone@retrodev.com>
parents:
139
diff
changeset
|
33 |
c14698c512f1
Untested addition of Pause/Resume
Mike Pavone <pavone@retrodev.com>
parents:
139
diff
changeset
|
34 int32_t put_cqueue(context * ct) |
c14698c512f1
Untested addition of Pause/Resume
Mike Pavone <pavone@retrodev.com>
parents:
139
diff
changeset
|
35 { |
c14698c512f1
Untested addition of Pause/Resume
Mike Pavone <pavone@retrodev.com>
parents:
139
diff
changeset
|
36 rh_lock(cq_lock); |
142
7bbdc034e347
Fix some bugs. Get basic network code working (epoll listener + accept connections). Start porting webserver.
Mike Pavone <pavone@retrodev.com>
parents:
141
diff
changeset
|
37 if (((cq_writeloc+1)&31) == cq_readloc) |
7bbdc034e347
Fix some bugs. Get basic network code working (epoll listener + accept connections). Start porting webserver.
Mike Pavone <pavone@retrodev.com>
parents:
141
diff
changeset
|
38 { |
140
c14698c512f1
Untested addition of Pause/Resume
Mike Pavone <pavone@retrodev.com>
parents:
139
diff
changeset
|
39 rh_unlock(cq_lock); |
c14698c512f1
Untested addition of Pause/Resume
Mike Pavone <pavone@retrodev.com>
parents:
139
diff
changeset
|
40 return 0; |
c14698c512f1
Untested addition of Pause/Resume
Mike Pavone <pavone@retrodev.com>
parents:
139
diff
changeset
|
41 } |
c14698c512f1
Untested addition of Pause/Resume
Mike Pavone <pavone@retrodev.com>
parents:
139
diff
changeset
|
42 contextqueue[cq_writeloc++] = ct; |
142
7bbdc034e347
Fix some bugs. Get basic network code working (epoll listener + accept connections). Start porting webserver.
Mike Pavone <pavone@retrodev.com>
parents:
141
diff
changeset
|
43 if(cq_writeloc == 32) |
7bbdc034e347
Fix some bugs. Get basic network code working (epoll listener + accept connections). Start porting webserver.
Mike Pavone <pavone@retrodev.com>
parents:
141
diff
changeset
|
44 cq_writeloc = 0; |
140
c14698c512f1
Untested addition of Pause/Resume
Mike Pavone <pavone@retrodev.com>
parents:
139
diff
changeset
|
45 rh_unlock(cq_lock); |
c14698c512f1
Untested addition of Pause/Resume
Mike Pavone <pavone@retrodev.com>
parents:
139
diff
changeset
|
46 return 1; |
c14698c512f1
Untested addition of Pause/Resume
Mike Pavone <pavone@retrodev.com>
parents:
139
diff
changeset
|
47 } |
c14698c512f1
Untested addition of Pause/Resume
Mike Pavone <pavone@retrodev.com>
parents:
139
diff
changeset
|
48 |
8
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
49 stackchunk * new_stack() |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
50 { |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
51 stackchunk * st = malloc(sizeof(stackchunk)); |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
52 st->prev = NULL; |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
53 st->next = NULL; |
67 | 54 st->free_space = st->data; |
8
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
55 return st; |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
56 } |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
57 |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
58 context * new_context() |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
59 { |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
60 context * c = malloc(sizeof(context)); |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
61 c->stack_begin = new_stack(); |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
62 c->current_stack = c->stack_begin; |
140
c14698c512f1
Untested addition of Pause/Resume
Mike Pavone <pavone@retrodev.com>
parents:
139
diff
changeset
|
63 c->resume_cdata = c->runafter = c->transaction = NULL; |
c14698c512f1
Untested addition of Pause/Resume
Mike Pavone <pavone@retrodev.com>
parents:
139
diff
changeset
|
64 c->resumeable = 0; |
8
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
65 return c; |
66
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
66 } |
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
67 |
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
68 void free_context(context * c) |
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
69 { |
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
70 stackchunk *next,*current = c->stack_begin; |
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
71 while(current) |
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
72 { |
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
73 next = current->next; |
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
74 free(current); |
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
75 current = next; |
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
76 } |
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
77 free(c); |
8
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
78 } |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
79 |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
80 void * alloc_stack(context * ct, uint32_t size) |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
81 { |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
82 void * ret; |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
83 stackchunk * current = ct->current_stack; |
67 | 84 char * next_free = current->free_space + size; |
85 if (next_free <= (current->data + STACK_CHUNK_SIZE)) | |
8
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
86 { |
67 | 87 ret = current->free_space; |
88 current->free_space = next_free; | |
89 return ret; | |
8
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
90 } |
67 | 91 if (!current->next) |
37
640f541e9116
Added support for type declarations on user defined workers and added a few more methods to Int32 in the runtime for the C backend
Mike Pavone <pavone@retrodev.com>
parents:
12
diff
changeset
|
92 { |
67 | 93 current->next = new_stack(); |
94 current->next->prev = current; | |
37
640f541e9116
Added support for type declarations on user defined workers and added a few more methods to Int32 in the runtime for the C backend
Mike Pavone <pavone@retrodev.com>
parents:
12
diff
changeset
|
95 } |
67 | 96 current = current->next; |
8
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
97 ct->current_stack = current; |
67 | 98 current->free_space = current->data + size; |
99 return current->data; | |
8
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
100 } |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
101 |
56
d2f9b0a9403d
Initial experiment with goto and switch
Mike Pavone <pavone@retrodev.com>
parents:
37
diff
changeset
|
102 calldata * alloc_cdata(context * ct, calldata * lastframe, uint32_t num_params) |
8
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
103 { |
66
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
104 //Make sure we have enough space for at least 32 return values |
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
105 calldata * retval = alloc_stack(ct, sizeof(calldata)+(31)*sizeof(object *)); |
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
106 //But only actually reserve space for the number requested |
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
107 free_stack(ct, retval->params + num_params); |
56
d2f9b0a9403d
Initial experiment with goto and switch
Mike Pavone <pavone@retrodev.com>
parents:
37
diff
changeset
|
108 retval->lastframe = lastframe; |
66
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
109 retval->callspace = num_params; |
56
d2f9b0a9403d
Initial experiment with goto and switch
Mike Pavone <pavone@retrodev.com>
parents:
37
diff
changeset
|
110 return retval; |
8
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
111 } |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
112 |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
113 void free_stack(context * ct, void * data) |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
114 { |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
115 char * cdata = data; |
67 | 116 while(cdata < ct->current_stack->data || cdata >= ct->current_stack->free_space) |
117 { | |
12
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
118 if(ct->current_stack == ct->stack_begin) |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
119 { |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
120 fprintf(stderr, "Attempt to free memory at %X using free_stack, but %X doesn't appear to be stack allocated\n", data, data); |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
121 exit(-1); |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
122 } |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
123 ct->current_stack = ct->current_stack->prev; |
8
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
124 } |
67 | 125 ct->current_stack->free_space = data; |
126 if(ct->current_stack->free_space == ct->current_stack->data && ct->current_stack->prev) | |
8
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
127 ct->current_stack = ct->current_stack->prev; |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
128 } |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
129 |