Mercurial > repos > rhope
annotate runtime/context.c @ 65:1db811fa4744
Handle native Rhope functions and plain C functions separately as part of move to new C strategy
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Tue, 01 Jun 2010 01:13:54 -0400 |
parents | 04baa003de5a |
children | d4b44ae2e34a |
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" |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
3 #include <stdlib.h> |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
4 #include <stddef.h> |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
5 #include <stdio.h> |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
6 |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
7 stackchunk * new_stack() |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
8 { |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
9 stackchunk * st = malloc(sizeof(stackchunk)); |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
10 st->prev = NULL; |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
11 st->next = NULL; |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
12 st->used = 0; |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
13 return st; |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
14 } |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
15 |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
16 context * new_context() |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
17 { |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
18 context * c = malloc(sizeof(context)); |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
19 c->stack_begin = new_stack(); |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
20 c->current_stack = c->stack_begin; |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
21 return c; |
52
079200bc3e75
String literals almost working. Print moved out of C runtime.
Mike Pavone <pavone@retrodev.com>
parents:
37
diff
changeset
|
22 } |
079200bc3e75
String literals almost working. Print moved out of C runtime.
Mike Pavone <pavone@retrodev.com>
parents:
37
diff
changeset
|
23 |
079200bc3e75
String literals almost working. Print moved out of C runtime.
Mike Pavone <pavone@retrodev.com>
parents:
37
diff
changeset
|
24 void free_context(context * c) |
079200bc3e75
String literals almost working. Print moved out of C runtime.
Mike Pavone <pavone@retrodev.com>
parents:
37
diff
changeset
|
25 { |
079200bc3e75
String literals almost working. Print moved out of C runtime.
Mike Pavone <pavone@retrodev.com>
parents:
37
diff
changeset
|
26 stackchunk *next,*current = c->stack_begin; |
079200bc3e75
String literals almost working. Print moved out of C runtime.
Mike Pavone <pavone@retrodev.com>
parents:
37
diff
changeset
|
27 while(current) |
079200bc3e75
String literals almost working. Print moved out of C runtime.
Mike Pavone <pavone@retrodev.com>
parents:
37
diff
changeset
|
28 { |
079200bc3e75
String literals almost working. Print moved out of C runtime.
Mike Pavone <pavone@retrodev.com>
parents:
37
diff
changeset
|
29 next = current->next; |
079200bc3e75
String literals almost working. Print moved out of C runtime.
Mike Pavone <pavone@retrodev.com>
parents:
37
diff
changeset
|
30 free(current); |
079200bc3e75
String literals almost working. Print moved out of C runtime.
Mike Pavone <pavone@retrodev.com>
parents:
37
diff
changeset
|
31 current = next; |
079200bc3e75
String literals almost working. Print moved out of C runtime.
Mike Pavone <pavone@retrodev.com>
parents:
37
diff
changeset
|
32 } |
079200bc3e75
String literals almost working. Print moved out of C runtime.
Mike Pavone <pavone@retrodev.com>
parents:
37
diff
changeset
|
33 free(c); |
8
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
34 } |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
35 |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
36 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
|
37 { |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
38 void * ret; |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
39 stackchunk * current = ct->current_stack; |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
40 if(size > STACK_CHUNK_SIZE) |
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
|
41 { |
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
|
42 fprintf(stderr, "%d is bigger than stack chunk size of %d\n", size, STACK_CHUNK_SIZE); |
8
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
43 return NULL; |
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
|
44 } |
8
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
45 while(current && STACK_CHUNK_SIZE - current->used < size) |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
46 { |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
47 if(!current->next) |
12
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
48 { |
8
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
49 current->next = new_stack(); |
12
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
50 current->next->prev = current; |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
51 } |
8
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
52 current = current->next; |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
53 } |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
54 if(!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
|
55 { |
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
|
56 fprintf(stderr, "Failed to allocate stack chunk"); |
8
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
57 return NULL; |
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
|
58 } |
8
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
59 ct->current_stack = current; |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
60 ret = current->data + current->used; |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
61 current->used += size; |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
62 return ret; |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
63 } |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
64 |
56
d2f9b0a9403d
Initial experiment with goto and switch
Mike Pavone <pavone@retrodev.com>
parents:
37
diff
changeset
|
65 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
|
66 { |
56
d2f9b0a9403d
Initial experiment with goto and switch
Mike Pavone <pavone@retrodev.com>
parents:
37
diff
changeset
|
67 calldata * retval = (calldata *)(((char *)alloc_stack(ct, sizeof(calldata)+(num_params-1)*sizeof(object *))) + sizeof(object *)*(num_params-1)); |
d2f9b0a9403d
Initial experiment with goto and switch
Mike Pavone <pavone@retrodev.com>
parents:
37
diff
changeset
|
68 retval->lastframe = lastframe; |
d2f9b0a9403d
Initial experiment with goto and switch
Mike Pavone <pavone@retrodev.com>
parents:
37
diff
changeset
|
69 return retval; |
8
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
70 } |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
71 |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
72 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
|
73 { |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
74 char * cdata = data; |
12
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
75 while(cdata < ct->current_stack->data || cdata >= ct->current_stack->data+STACK_CHUNK_SIZE) { |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
76 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
|
77 { |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
78 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
|
79 exit(-1); |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
80 } |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
81 ct->current_stack->used = 0; |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
82 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
|
83 } |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
84 ct->current_stack->used = cdata-ct->current_stack->data; |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
85 if(!ct->current_stack->used && ct->current_stack->prev) |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
86 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
|
87 } |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
88 |