Mercurial > repos > rhope
annotate runtime/context.c @ 52:079200bc3e75
String literals almost working. Print moved out of C runtime.
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Wed, 28 Apr 2010 01:23:30 -0400 |
parents | 640f541e9116 |
children | 04baa003de5a |
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 c->unwind = NULL; |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
22 return c; |
52
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 |
079200bc3e75
String literals almost working. Print moved out of C runtime.
Mike Pavone <pavone@retrodev.com>
parents:
37
diff
changeset
|
25 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
|
26 { |
079200bc3e75
String literals almost working. Print moved out of C runtime.
Mike Pavone <pavone@retrodev.com>
parents:
37
diff
changeset
|
27 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
|
28 while(current) |
079200bc3e75
String literals almost working. Print moved out of C runtime.
Mike Pavone <pavone@retrodev.com>
parents:
37
diff
changeset
|
29 { |
079200bc3e75
String literals almost working. Print moved out of C runtime.
Mike Pavone <pavone@retrodev.com>
parents:
37
diff
changeset
|
30 next = current->next; |
079200bc3e75
String literals almost working. Print moved out of C runtime.
Mike Pavone <pavone@retrodev.com>
parents:
37
diff
changeset
|
31 free(current); |
079200bc3e75
String literals almost working. Print moved out of C runtime.
Mike Pavone <pavone@retrodev.com>
parents:
37
diff
changeset
|
32 current = next; |
079200bc3e75
String literals almost working. Print moved out of C runtime.
Mike Pavone <pavone@retrodev.com>
parents:
37
diff
changeset
|
33 } |
079200bc3e75
String literals almost working. Print moved out of C runtime.
Mike Pavone <pavone@retrodev.com>
parents:
37
diff
changeset
|
34 free(c); |
8
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 |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
37 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
|
38 { |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
39 void * ret; |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
40 stackchunk * current = ct->current_stack; |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
41 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
|
42 { |
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
|
43 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
|
44 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
|
45 } |
8
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
46 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
|
47 { |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
48 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
|
49 { |
8
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
50 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
|
51 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
|
52 } |
8
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
53 current = current->next; |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
54 } |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
55 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
|
56 { |
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
|
57 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
|
58 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
|
59 } |
8
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
60 ct->current_stack = current; |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
61 ret = current->data + current->used; |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
62 current->used += size; |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
63 return ret; |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
64 } |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
65 |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
66 calldata * alloc_cdata(context * ct, uint32_t num_params) |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
67 { |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
68 calldata * out = alloc_stack(ct, sizeof(calldata)+(num_params-1)*sizeof(object *)); |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
69 if(out) |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
70 out->ct = ct; |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
71 return out; |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
72 } |
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 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
|
75 { |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
76 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
|
77 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
|
78 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
|
79 { |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
80 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
|
81 exit(-1); |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
82 } |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
83 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
|
84 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
|
85 } |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
86 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
|
87 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
|
88 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
|
89 } |
8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
90 |