annotate runtime/object.c @ 41:1b86a1ee500a

Added faster allocator for small objects
author Mike Pavone <pavone@retrodev.com>
date Sat, 10 Oct 2009 16:40:50 -0400
parents 789a146a48e1
children 3e20ed8959c4 b218af069da7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1 #include "object.h"
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
2 #include "builtin.h"
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
3 #include <stdlib.h>
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
4 #include <string.h>
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
5 #include <stdio.h>
41
1b86a1ee500a Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents: 40
diff changeset
6 #include "fixed_alloc.h"
7
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
7
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
8 blueprint ** registered_types = NULL;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
9 uint32_t max_registered_type = 0;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
10 uint32_t type_storage = 0;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
11
41
1b86a1ee500a Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents: 40
diff changeset
12 mem_manager * manager = NULL;
1b86a1ee500a Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents: 40
diff changeset
13
7
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
14 returntype call_method(uint32_t methodid, calldata * params)
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
15 {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
16 int i;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
17 blueprint * bp = get_blueprint(params->params[0]);
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
18 if(methodid >= bp->first_methodid && methodid < bp->last_methodid && bp->method_lookup[methodid - bp->first_methodid])
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
19 {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
20 params->tail_func = bp->method_lookup[methodid - bp->first_methodid];
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
21 return TAIL_RETURN;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
22 } else {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
23 if(METHOD_MISSING >= bp->first_methodid && METHOD_MISSING < bp->last_methodid && bp->method_lookup[METHOD_MISSING - bp->first_methodid])
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
24 {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
25 params->tail_func = bp->method_lookup[METHOD_MISSING - bp->first_methodid];
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
26 return TAIL_RETURN;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
27 } else {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
28 //TODO: Add useful info to exception
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
29 for(i = 0; i < params->num_params; ++i)
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
30 release_ref(params->params[i]);
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
31 params->params[0] = new_object(TYPE_METHODMISSINGEXCEPTION);
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
32 return EXCEPTION_RETURN;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
33 }
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
34 }
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
35 }
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
36
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
37 returntype set_field(uint32_t setfieldid, calldata * params)
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
38 {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
39 int i;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
40 blueprint * bp = get_blueprint(params->params[0]);
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
41 if(setfieldid >= bp->first_setfieldid && setfieldid < bp->last_setfieldid && bp->setter_lookup[setfieldid - bp->first_setfieldid])
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
42 {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
43 params->tail_func = bp->setter_lookup[setfieldid - bp->first_setfieldid];
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
44 return TAIL_RETURN;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
45 } else {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
46 if(METHOD_SETFIELDMISSING >= bp->first_setfieldid && METHOD_SETFIELDMISSING < bp->last_setfieldid && bp->method_lookup[METHOD_SETFIELDMISSING - bp->first_methodid])
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
47 {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
48 params->tail_func = bp->method_lookup[METHOD_SETFIELDMISSING - bp->first_methodid];
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
49 params->original_methodid = setfieldid;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
50 return TAIL_RETURN;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
51 } else {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
52 //TODO: Add useful info to exception
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
53 for(i = 0; i < params->num_params; ++i)
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
54 release_ref(params->params[i]);
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
55 params->params[0] = new_object(TYPE_FIELDMISSINGEXCEPTION);
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
56 return EXCEPTION_RETURN;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
57 }
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
58 }
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
59 }
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
60
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
61
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
62 returntype get_field(uint32_t getfieldid, calldata * params)
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
63 {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
64 int i;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
65 blueprint * bp = get_blueprint(params->params[0]);
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
66 if(getfieldid >= bp->first_getfieldid && getfieldid < bp->last_getfieldid && bp->getter_lookup[getfieldid - bp->first_getfieldid])
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
67 {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
68 params->tail_func = bp->getter_lookup[getfieldid - bp->first_getfieldid];
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
69 return TAIL_RETURN;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
70 } else {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
71 if(METHOD_GETFIELDMISSING >= bp->first_getfieldid && METHOD_GETFIELDMISSING < bp->last_getfieldid && bp->method_lookup[METHOD_GETFIELDMISSING - bp->first_methodid])
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
72 {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
73 params->tail_func = bp->method_lookup[METHOD_GETFIELDMISSING - bp->first_methodid];
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
74 params->original_methodid = getfieldid;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
75 return TAIL_RETURN;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
76 } else {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
77 //TODO: Add useful info to exception
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
78 for(i = 0; i < params->num_params; ++i)
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
79 release_ref(params->params[i]);
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
80 params->params[0] = new_object(TYPE_FIELDMISSINGEXCEPTION);
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
81 return EXCEPTION_RETURN;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
82 }
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
83 }
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
84 }
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
85
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
86 returntype convert_to(uint32_t convertto, calldata * params)
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
87 {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
88 int i;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
89 blueprint * bp = get_blueprint(params->params[0]);
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
90 if(convertto >= bp->first_convertto && convertto < bp->last_convertto && bp->convert_to[convertto])
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
91 {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
92 params->tail_func = bp->convert_to[convertto - bp->first_convertto];
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
93 return TAIL_RETURN;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
94 } else {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
95 return NO_CONVERSION;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
96 }
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
97 }
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
98
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
99 returntype convert_from(uint32_t convertfrom, calldata * params)
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
100 {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
101 int i;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
102 blueprint * bp = registered_types[convertfrom];
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
103 if(convertfrom >= bp->first_convertfrom && convertfrom < bp->last_convertfrom && bp->convert_from[convertfrom])
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
104 {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
105 params->tail_func = bp->convert_from[convertfrom - bp->first_convertfrom];
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
106 return TAIL_RETURN;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
107 } else {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
108 return NO_CONVERSION;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
109 }
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
110 }
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
111
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
112 returntype coerce_value(uint32_t type, calldata * params)
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
113 {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
114 int i;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
115 blueprint * bp = get_blueprint(params->params[0]);
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
116 if(bp == registered_types[type])
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
117 return NORMAL_RETURN;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
118 if(convert_to(type, params) == TAIL_RETURN)
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
119 return TAIL_RETURN;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
120 if(convert_from(type, params) == TAIL_RETURN)
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
121 return TAIL_RETURN;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
122 //TODO: Add useful info to exception
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
123 for(i = 0; i < params->num_params; ++i)
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
124 release_ref(params->params[i]);
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
125 params->params[0] = new_object(TYPE_WRONGTYPEEXCEPTION);
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
126 return EXCEPTION_RETURN;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
127 }
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
128
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
129 object * alloc_object(blueprint * bp)
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
130 {
41
1b86a1ee500a Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents: 40
diff changeset
131 return falloc(bp->boxed_size, manager);
1b86a1ee500a Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents: 40
diff changeset
132 //return malloc(bp->boxed_size);
7
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
133 }
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
134
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
135 void dealloc_object(blueprint * bp, object * obj)
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
136 {
41
1b86a1ee500a Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents: 40
diff changeset
137 ffree(obj, bp->boxed_size, manager);
7
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
138 }
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
139
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
140 object * new_object(uint32_t type)
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
141 {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
142 blueprint * bp;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
143 object * ret;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
144 if(type >= max_registered_type || !registered_types[type])
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
145 return NULL;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
146 bp = registered_types[type];
36
495dddadd058 User defined types work in the compiler now
Mike Pavone <pavone@retrodev.com>
parents: 34
diff changeset
147 return new_object_bp(bp);
495dddadd058 User defined types work in the compiler now
Mike Pavone <pavone@retrodev.com>
parents: 34
diff changeset
148 }
495dddadd058 User defined types work in the compiler now
Mike Pavone <pavone@retrodev.com>
parents: 34
diff changeset
149
495dddadd058 User defined types work in the compiler now
Mike Pavone <pavone@retrodev.com>
parents: 34
diff changeset
150 object * new_object_bp(blueprint * bp)
495dddadd058 User defined types work in the compiler now
Mike Pavone <pavone@retrodev.com>
parents: 34
diff changeset
151 {
495dddadd058 User defined types work in the compiler now
Mike Pavone <pavone@retrodev.com>
parents: 34
diff changeset
152 object * ret;
7
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
153 ret = alloc_object(bp);
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
154 if(ret)
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
155 {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
156 ret->bprint = bp;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
157 rh_atomic_set(ret, refcount, 1);
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
158 memset(((char *)ret) + sizeof(object), '\0', bp->size);
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
159 bp->init(ret);
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: 36
diff changeset
160 } else {
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: 36
diff changeset
161 fprintf(stderr, "Could not allocate new object with size %d\n", bp->boxed_size);
7
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
162 }
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
163 return ret;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
164 }
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
165
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
166 multisize * new_multisize(uint32_t type, uint32_t size)
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
167 {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
168 blueprint *bp;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
169 multisize * ret;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
170 if(type >= max_registered_type || !registered_types[type])
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
171 return NULL;
41
1b86a1ee500a Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents: 40
diff changeset
172 ret = falloc(sizeof(multisize) + size, manager);
7
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
173 if(ret)
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
174 {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
175 bp = registered_types[type];
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
176 ret->base.bprint = bp;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
177 ret->size = size;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
178 rh_atomic_set(&(ret->base), refcount, 1);
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
179 memset(((char *)ret) + sizeof(multisize), '\0', size);
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
180 bp->init((object *)ret);
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
181 }
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
182 return ret;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
183 }
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
184
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
185 object * copy_object(object * tocopy)
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
186 {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
187 object * copy;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
188 multisize * mcopy, *mtocopy;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
189 blueprint * bp;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
190 if(rh_atomic_get(tocopy, refcount) == 1)
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
191 return tocopy;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
192 bp = get_blueprint(tocopy);
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
193 if(bp->size < 0) {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
194 mtocopy = (multisize *)tocopy;
41
1b86a1ee500a Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents: 40
diff changeset
195 mcopy = falloc(sizeof(multisize) + mtocopy->size, manager);
7
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
196 mcopy->size = mtocopy->size;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
197 memcpy(((char *)mcopy)+sizeof(multisize), ((char *)mtocopy)+sizeof(multisize), mtocopy->size);
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
198 copy = (object *)mcopy;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
199 } else {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
200 copy = alloc_object(bp);
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
201 memcpy(((char *)copy) + sizeof(object), ((char *)tocopy)+sizeof(object), bp->size);
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
202 }
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
203 copy->bprint = bp;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
204 rh_atomic_set(copy, refcount, 1);
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
205 bp->copy(copy);
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
206 release_ref(tocopy);
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
207 return copy;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
208 }
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
209
40
789a146a48e1 Started adding support for naked values in user defined objects
Mike Pavone <pavone@retrodev.com>
parents: 37
diff changeset
210 object * naked_to_boxed(uint32_t type, void * rawdata)
789a146a48e1 Started adding support for naked values in user defined objects
Mike Pavone <pavone@retrodev.com>
parents: 37
diff changeset
211 {
789a146a48e1 Started adding support for naked values in user defined objects
Mike Pavone <pavone@retrodev.com>
parents: 37
diff changeset
212 object * dest;
789a146a48e1 Started adding support for naked values in user defined objects
Mike Pavone <pavone@retrodev.com>
parents: 37
diff changeset
213 blueprint * bp = get_blueprint_byid(type);
41
1b86a1ee500a Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents: 40
diff changeset
214 if(!bp->boxed_size)
40
789a146a48e1 Started adding support for naked values in user defined objects
Mike Pavone <pavone@retrodev.com>
parents: 37
diff changeset
215 return NULL; //We don't know how big a naked multi-size object is so we can't do anything with it
789a146a48e1 Started adding support for naked values in user defined objects
Mike Pavone <pavone@retrodev.com>
parents: 37
diff changeset
216 dest = alloc_object(bp);
789a146a48e1 Started adding support for naked values in user defined objects
Mike Pavone <pavone@retrodev.com>
parents: 37
diff changeset
217 memcpy(((char *)dest) + sizeof(object), rawdata, bp->size);
789a146a48e1 Started adding support for naked values in user defined objects
Mike Pavone <pavone@retrodev.com>
parents: 37
diff changeset
218 dest->bprint = bp;
789a146a48e1 Started adding support for naked values in user defined objects
Mike Pavone <pavone@retrodev.com>
parents: 37
diff changeset
219 rh_atomic_set(dest, refcount, 1);
789a146a48e1 Started adding support for naked values in user defined objects
Mike Pavone <pavone@retrodev.com>
parents: 37
diff changeset
220 bp->copy(dest);
789a146a48e1 Started adding support for naked values in user defined objects
Mike Pavone <pavone@retrodev.com>
parents: 37
diff changeset
221 return dest;
789a146a48e1 Started adding support for naked values in user defined objects
Mike Pavone <pavone@retrodev.com>
parents: 37
diff changeset
222 }
789a146a48e1 Started adding support for naked values in user defined objects
Mike Pavone <pavone@retrodev.com>
parents: 37
diff changeset
223
789a146a48e1 Started adding support for naked values in user defined objects
Mike Pavone <pavone@retrodev.com>
parents: 37
diff changeset
224 void boxed_to_naked(object * src, void * dest)
789a146a48e1 Started adding support for naked values in user defined objects
Mike Pavone <pavone@retrodev.com>
parents: 37
diff changeset
225 {
789a146a48e1 Started adding support for naked values in user defined objects
Mike Pavone <pavone@retrodev.com>
parents: 37
diff changeset
226 blueprint * bp = get_blueprint(src);
41
1b86a1ee500a Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents: 40
diff changeset
227 if(!bp->boxed_size)
40
789a146a48e1 Started adding support for naked values in user defined objects
Mike Pavone <pavone@retrodev.com>
parents: 37
diff changeset
228 return; //We don't know how big a naked multi-size object is so we can't do anything with it
789a146a48e1 Started adding support for naked values in user defined objects
Mike Pavone <pavone@retrodev.com>
parents: 37
diff changeset
229 memcpy(dest, ((char *)src) + sizeof(object), bp->size);
789a146a48e1 Started adding support for naked values in user defined objects
Mike Pavone <pavone@retrodev.com>
parents: 37
diff changeset
230 bp->copy(src);
789a146a48e1 Started adding support for naked values in user defined objects
Mike Pavone <pavone@retrodev.com>
parents: 37
diff changeset
231 }
789a146a48e1 Started adding support for naked values in user defined objects
Mike Pavone <pavone@retrodev.com>
parents: 37
diff changeset
232
7
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
233 void release_ref(object * obj)
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
234 {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
235 if(rh_atomic_sub_testzero(obj, refcount, 1))
41
1b86a1ee500a Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents: 40
diff changeset
236 get_blueprint(obj)->free(obj);
7
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
237 }
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
238
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
239 void check_type_storage(type)
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
240 {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
241 uint32_t type_storage_temp;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
242 blueprint ** temp;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
243 if(type >= type_storage)
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
244 if(type_storage)
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
245 {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
246 type_storage_temp = (type + (type_storage >> 1));
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
247 temp = realloc(registered_types, type_storage_temp * sizeof(blueprint *));
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
248 if(temp)
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
249 {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
250 registered_types = temp;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
251 memset(registered_types + type_storage, '\0', (type_storage_temp - type_storage) * sizeof(blueprint *));
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
252 type_storage = type_storage_temp;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
253 }
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
254 else
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
255 {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
256 free(registered_types);
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
257 fprintf(stderr, "Couldn't allocate %d bytes for type storage array\n", type_storage_temp * sizeof(blueprint *));
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
258 exit(-1);
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
259 }
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
260 } else {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
261
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
262 if(type < INITIAL_TYPE_STORAGE)
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
263 type_storage =INITIAL_TYPE_STORAGE;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
264 else
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
265 type_storage = type + 8;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
266 registered_types = malloc(type_storage * sizeof(blueprint *));
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
267 if(registered_types)
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
268 memset(registered_types, '\0', type_storage * sizeof(blueprint *));
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
269 else
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
270 {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
271 fprintf(stderr, "Couldn't allocate %d bytes for type storage array\n", type_storage * sizeof(blueprint *));
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
272 exit(-1);
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
273 }
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
274
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
275 }
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
276 }
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
277
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
278 void default_action(object * obj)
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
279 {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
280 }
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
281
41
1b86a1ee500a Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents: 40
diff changeset
282 void normal_free(object * obj)
1b86a1ee500a Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents: 40
diff changeset
283 {
1b86a1ee500a Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents: 40
diff changeset
284 blueprint * bp = get_blueprint(obj);
1b86a1ee500a Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents: 40
diff changeset
285 if(bp->cleanup)
1b86a1ee500a Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents: 40
diff changeset
286 bp->cleanup(obj);
1b86a1ee500a Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents: 40
diff changeset
287 ffree(obj, bp->boxed_size, manager);
1b86a1ee500a Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents: 40
diff changeset
288 }
1b86a1ee500a Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents: 40
diff changeset
289
1b86a1ee500a Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents: 40
diff changeset
290 void multi_free(object * obj)
1b86a1ee500a Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents: 40
diff changeset
291 {
1b86a1ee500a Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents: 40
diff changeset
292 multisize * multi = (multisize *)obj;
1b86a1ee500a Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents: 40
diff changeset
293 blueprint * bp = get_blueprint(obj);
1b86a1ee500a Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents: 40
diff changeset
294 if(bp->cleanup)
1b86a1ee500a Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents: 40
diff changeset
295 bp->cleanup(obj);
1b86a1ee500a Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents: 40
diff changeset
296 ffree(multi, sizeof(multi) + multi->size, manager);
1b86a1ee500a Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents: 40
diff changeset
297 }
1b86a1ee500a Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents: 40
diff changeset
298
7
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
299 blueprint * new_blueprint(uint32_t type, uint32_t size, special_func init, special_func copy, special_func cleanup)
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
300 {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
301 blueprint * bp = malloc(sizeof(blueprint));
41
1b86a1ee500a Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents: 40
diff changeset
302 //dirty hack!, move elsewhere
1b86a1ee500a Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents: 40
diff changeset
303 if (!manager) {
1b86a1ee500a Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents: 40
diff changeset
304 fixed_alloc_init();
1b86a1ee500a Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents: 40
diff changeset
305 manager = new_mem_manager();
1b86a1ee500a Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents: 40
diff changeset
306 }
7
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
307 if(bp)
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
308 {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
309 bp->size = size;
41
1b86a1ee500a Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents: 40
diff changeset
310 bp->boxed_size = size >= 0 ? size + sizeof(object) : 0;
7
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
311 bp->method_lookup = bp->getter_lookup = bp->setter_lookup = bp->convert_to = bp->convert_from = NULL;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
312 bp->init = init ? init : default_action;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
313 bp->copy = copy ? copy : default_action;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
314 bp->cleanup = cleanup ? cleanup : default_action;
41
1b86a1ee500a Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents: 40
diff changeset
315 bp->free = size >= 0 ? normal_free : multi_free;
7
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
316 bp->first_methodid = bp->last_methodid = bp->first_getfieldid = bp->last_getfieldid = bp->first_setfieldid = bp->last_setfieldid = bp->first_convertto = bp->last_convertto = bp->first_convertfrom = bp->last_convertfrom = 0;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
317 //TODO: Handle names
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
318 bp->name = NULL;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
319 }
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
320 return bp;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
321 }
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
322
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
323 blueprint * register_type_byid(uint32_t type, uint32_t size, special_func init, special_func copy, special_func cleanup)
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
324 {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
325 check_type_storage(type);
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
326 if(registered_types[type])
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
327 return registered_types[type];
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
328 registered_types[type] = new_blueprint(type, size, init, copy, cleanup);
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
329 if(!registered_types[type])
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
330 {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
331 fputs("Couldn't allocate new object blueprint\n", stderr);
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
332 exit(-1);
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
333 }
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
334 if(type >= max_registered_type)
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
335 max_registered_type = type + 1;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
336 return registered_types[type];
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
337 }
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
338
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
339 void add_method(blueprint * bp, uint32_t methodid, rhope_func impl)
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
340 {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
341 rhope_func * temp;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
342 if(methodid < 1) {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
343 fputs("Attempt to add a method with an ID < 1\n", stderr);
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
344 exit(-1);
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
345 }
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
346 if (!bp->method_lookup)
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
347 {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
348 bp->method_lookup = malloc(sizeof(rhope_func) * INITIAL_METHOD_LOOKUP);
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
349 if(!bp->method_lookup) {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
350 fprintf(stderr, "Couldn't allocate %d bytes for method lookup table\n", sizeof(rhope_func) * INITIAL_METHOD_LOOKUP);
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
351 exit(-1);
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
352 }
12
31f8182f3433 Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents: 7
diff changeset
353 if(BELOW_INITIAL_METHOD > methodid) {
7
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
354 bp->first_methodid = 1;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
355 bp->last_methodid = 1+INITIAL_METHOD_LOOKUP;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
356 } else {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
357 bp->first_methodid = methodid - BELOW_INITIAL_METHOD;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
358 bp->last_methodid = bp->first_methodid + INITIAL_METHOD_LOOKUP;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
359 }
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
360 memset(bp->method_lookup, '\0', sizeof(rhope_func) * INITIAL_METHOD_LOOKUP);
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
361 } else {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
362 if (methodid < bp->first_methodid) {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
363 temp = bp->method_lookup;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
364 //Note: if this gets changed to generating an exception on failure, we need to restore the original buffer
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
365 bp->method_lookup = malloc(sizeof(rhope_func) * (bp->last_methodid-methodid));
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
366 if(!bp->method_lookup) {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
367 fprintf(stderr, "Couldn't allocate %d bytes for method lookup table\n", sizeof(rhope_func) * (bp->last_methodid-methodid));
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
368 exit(-1);
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
369 }
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
370 memset(bp->method_lookup, '\0', (bp->first_methodid-methodid) * sizeof(rhope_func));
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
371 memcpy(bp->method_lookup + bp->first_methodid-methodid, temp, (bp->last_methodid-bp->first_methodid)*sizeof(rhope_func));
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
372 free(temp);
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
373 bp->first_methodid = methodid;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
374 } else if(methodid >= bp->last_methodid) {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
375 //Note: if this gets changed to generating an exception on failure, we need to restore the original buffer
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
376 bp->method_lookup = realloc(bp->method_lookup, (methodid+1-bp->first_methodid) * sizeof(rhope_func));
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
377 if(!bp->method_lookup) {
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
378 fprintf(stderr, "Couldn't resize method lookup table to %d bytes\n", (methodid+1-bp->first_methodid) * sizeof(rhope_func));
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
379 exit(-1);
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
380 }
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
381 memset(bp->method_lookup+bp->last_methodid, '\0', (methodid+1)-bp->last_methodid);
12
31f8182f3433 Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents: 7
diff changeset
382 bp->last_methodid = methodid+1;
7
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
383 }
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
384 }
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
385 bp->method_lookup[methodid-bp->first_methodid] = impl;
d61550e2c001 Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
386 }
30
914ad38f9b59 Compiler now works for some simple programs
Mike Pavone <pavone@retrodev.com>
parents: 12
diff changeset
387
34
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
388 void add_getter(blueprint * bp, uint32_t getfieldid, rhope_func impl)
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
389 {
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
390 rhope_func * temp;
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
391 if(getfieldid < 1) {
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
392 fputs("Attempt to add a method with an ID < 1\n", stderr);
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
393 exit(-1);
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
394 }
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
395 if (!bp->getter_lookup)
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
396 {
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
397 bp->getter_lookup = malloc(sizeof(rhope_func) * INITIAL_METHOD_LOOKUP);
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
398 if(!bp->getter_lookup) {
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
399 fprintf(stderr, "Couldn't allocate %d bytes for getter lookup table\n", sizeof(rhope_func) * INITIAL_METHOD_LOOKUP);
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
400 exit(-1);
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
401 }
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
402 if(BELOW_INITIAL_METHOD > getfieldid) {
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
403 bp->first_getfieldid = 1;
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
404 bp->last_getfieldid = 1+INITIAL_METHOD_LOOKUP;
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
405 } else {
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
406 bp->first_getfieldid = getfieldid - BELOW_INITIAL_METHOD;
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
407 bp->last_getfieldid = bp->first_getfieldid + INITIAL_METHOD_LOOKUP;
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
408 }
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
409 memset(bp->getter_lookup, '\0', sizeof(rhope_func) * INITIAL_METHOD_LOOKUP);
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
410 } else {
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
411 if (getfieldid < bp->first_getfieldid) {
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
412 temp = bp->getter_lookup;
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
413 //Note: if this gets changed to generating an exception on failure, we need to restore the original buffer
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
414 bp->getter_lookup = malloc(sizeof(rhope_func) * (bp->last_getfieldid-getfieldid));
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
415 if(!bp->getter_lookup) {
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
416 fprintf(stderr, "Couldn't allocate %d bytes for getter lookup table\n", sizeof(rhope_func) * (bp->last_getfieldid-getfieldid));
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
417 exit(-1);
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
418 }
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
419 memset(bp->getter_lookup, '\0', (bp->first_getfieldid-getfieldid) * sizeof(rhope_func));
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
420 memcpy(bp->getter_lookup + bp->first_getfieldid-getfieldid, temp, (bp->last_getfieldid-bp->first_getfieldid)*sizeof(rhope_func));
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
421 free(temp);
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
422 bp->first_getfieldid = getfieldid;
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
423 } else if(getfieldid >= bp->last_getfieldid) {
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
424 //Note: if this gets changed to generating an exception on failure, we need to restore the original buffer
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
425 bp->getter_lookup = realloc(bp->getter_lookup, (getfieldid+1-bp->first_getfieldid) * sizeof(rhope_func));
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
426 if(!bp->getter_lookup) {
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
427 fprintf(stderr, "Couldn't resize getter lookup table to %d bytes\n", (getfieldid+1-bp->first_getfieldid) * sizeof(rhope_func));
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
428 exit(-1);
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
429 }
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
430 memset(bp->getter_lookup+bp->last_getfieldid, '\0', (getfieldid+1)-bp->last_getfieldid);
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
431 bp->last_getfieldid = getfieldid+1;
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
432 }
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
433 }
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
434 bp->getter_lookup[getfieldid-bp->first_getfieldid] = impl;
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
435 }
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
436
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
437 void add_setter(blueprint * bp, uint32_t setfieldid, rhope_func impl)
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
438 {
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
439 rhope_func * temp;
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
440 if(setfieldid < 1) {
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
441 fputs("Attempt to add a method with an ID < 1\n", stderr);
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
442 exit(-1);
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
443 }
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
444 if (!bp->setter_lookup)
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
445 {
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
446 bp->setter_lookup = malloc(sizeof(rhope_func) * INITIAL_METHOD_LOOKUP);
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
447 if(!bp->setter_lookup) {
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
448 fprintf(stderr, "Couldn't allocate %d bytes for setter lookup table\n", sizeof(rhope_func) * INITIAL_METHOD_LOOKUP);
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
449 exit(-1);
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
450 }
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
451 if(BELOW_INITIAL_METHOD > setfieldid) {
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
452 bp->first_setfieldid = 1;
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
453 bp->last_setfieldid = 1+INITIAL_METHOD_LOOKUP;
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
454 } else {
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
455 bp->first_setfieldid = setfieldid - BELOW_INITIAL_METHOD;
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
456 bp->last_setfieldid = bp->first_setfieldid + INITIAL_METHOD_LOOKUP;
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
457 }
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
458 memset(bp->setter_lookup, '\0', sizeof(rhope_func) * INITIAL_METHOD_LOOKUP);
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
459 } else {
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
460 if (setfieldid < bp->first_setfieldid) {
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
461 temp = bp->setter_lookup;
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
462 //Note: if this sets changed to generating an exception on failure, we need to restore the original buffer
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
463 bp->setter_lookup = malloc(sizeof(rhope_func) * (bp->last_setfieldid-setfieldid));
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
464 if(!bp->setter_lookup) {
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
465 fprintf(stderr, "Couldn't allocate %d bytes for setter lookup table\n", sizeof(rhope_func) * (bp->last_setfieldid-setfieldid));
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
466 exit(-1);
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
467 }
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
468 memset(bp->setter_lookup, '\0', (bp->first_setfieldid-setfieldid) * sizeof(rhope_func));
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
469 memcpy(bp->setter_lookup + bp->first_setfieldid-setfieldid, temp, (bp->last_setfieldid-bp->first_setfieldid)*sizeof(rhope_func));
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
470 free(temp);
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
471 bp->first_setfieldid = setfieldid;
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
472 } else if(setfieldid >= bp->last_setfieldid) {
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
473 //Note: if this sets changed to generating an exception on failure, we need to restore the original buffer
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
474 bp->setter_lookup = realloc(bp->setter_lookup, (setfieldid+1-bp->first_setfieldid) * sizeof(rhope_func));
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
475 if(!bp->setter_lookup) {
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
476 fprintf(stderr, "Couldn't resize setter lookup table to %d bytes\n", (setfieldid+1-bp->first_setfieldid) * sizeof(rhope_func));
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
477 exit(-1);
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
478 }
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
479 memset(bp->setter_lookup+bp->last_setfieldid, '\0', (setfieldid+1)-bp->last_setfieldid);
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
480 bp->last_setfieldid = setfieldid+1;
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
481 }
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
482 }
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
483 bp->setter_lookup[setfieldid-bp->first_setfieldid] = impl;
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
484 }
df038cef648b More work on supporting user defined types in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 30
diff changeset
485
30
914ad38f9b59 Compiler now works for some simple programs
Mike Pavone <pavone@retrodev.com>
parents: 12
diff changeset
486 blueprint * get_blueprint_byid(uint32_t type)
914ad38f9b59 Compiler now works for some simple programs
Mike Pavone <pavone@retrodev.com>
parents: 12
diff changeset
487 {
914ad38f9b59 Compiler now works for some simple programs
Mike Pavone <pavone@retrodev.com>
parents: 12
diff changeset
488 if(type >= max_registered_type)
914ad38f9b59 Compiler now works for some simple programs
Mike Pavone <pavone@retrodev.com>
parents: 12
diff changeset
489 return NULL;
914ad38f9b59 Compiler now works for some simple programs
Mike Pavone <pavone@retrodev.com>
parents: 12
diff changeset
490 return registered_types[type];
914ad38f9b59 Compiler now works for some simple programs
Mike Pavone <pavone@retrodev.com>
parents: 12
diff changeset
491 }
914ad38f9b59 Compiler now works for some simple programs
Mike Pavone <pavone@retrodev.com>
parents: 12
diff changeset
492