/
Trylon_.c
336 lines (258 loc) · 7.13 KB
/
Trylon_.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/* Trylon_.c */
#include "Trylon_.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#ifdef OSX_FINK
#include "gc.h"
#else
#include "gc/gc.h"
#endif
static obj_ SendMessageNotUnderstood_(obj_ object, ...)
{
UsingMethod_(message_not_understood);
return Call_(message_not_understood, object);
}
fn_ptr_ Dispatch_(selector_ selector, obj_ object)
{
struct RDTableEntry_* entry =
&dispatchTable_[selector + ClassNumFor_(object)];
if (entry->selector == selector)
return entry->method;
// Send message-not-understood:instead.
// *** Eventually, we want to actually specify *which* message wasn't
// *** understood, and also pass the arguments.
return (fn_ptr_) &SendMessageNotUnderstood_;
}
obj_ RespondsTo_(obj_ object, selector_ selector)
{
struct RDTableEntry_* entry =
&dispatchTable_[selector + ClassNumFor_(object)];
return Bool_(entry->selector == selector);
}
obj_ AllocObjFromClassInfo_(struct ClassInfo* classInfo)
{
obj_ object =
(obj_) GC_MALLOC(
sizeof(classref_) + classInfo->numSlots * sizeof(obj_));
object->class_ = classInfo;
return object;
}
static ExceptionCatcher_* currentExceptionCatcher = NULL;
obj_ currentException_ = NULL;
void PushException_(ExceptionCatcher_* catcher)
{
catcher->nextCatcher = currentExceptionCatcher;
currentExceptionCatcher = catcher;
}
void Throw_(obj_ object)
{
if (currentExceptionCatcher == NULL) {
// Catastrophic failure!
printf("\n**** Uncaught exception! ****\n\n");
exit(1);
}
currentException_ = object;
longjmp(currentExceptionCatcher->jumpBuf, 1);
}
void PopException_()
{
if (currentExceptionCatcher == NULL) {
// Catastrophic failure, and it's probably the compiler's fault!
printf("\n**** Compiler error: generated bad _PopException_()! ***\n\n");
exit(1);
}
currentExceptionCatcher = currentExceptionCatcher->nextCatcher;
}
static void FinalizeObject_(void* obj, void* clientData)
{
UsingMethod_(destroy);
Call_(destroy, (obj_) obj);
}
void RegisterFinalizer_(obj_ object)
{
GC_finalization_proc oldProc;
void* oldData;
GC_REGISTER_FINALIZER(object, &FinalizeObject_, NULL, &oldProc, &oldData);
}
obj_ BuildInt_(int value)
{
struct Standard__Int__internal* result =
(struct Standard__Int__internal*)
GC_MALLOC(sizeof(struct Standard__Int__internal));
result->class_ = StdClassRef_(Int);
result->value = value;
return (obj_) result;
}
obj_ BuildFloat_(double value)
{
struct Standard__Float__internal* result =
(struct Standard__Float__internal*)
GC_MALLOC(sizeof(struct Standard__Float__internal));
result->class_ = StdClassRef_(Float);
result->value = value;
return (obj_) result;
}
obj_ BuildBytePtr_(void* value)
{
struct Standard__BytePtr__internal* result =
(struct Standard__BytePtr__internal*)
GC_MALLOC(sizeof(struct Standard__BytePtr__internal));
result->class_ = StdClassRef_(BytePtr);
result->value = value;
return (obj_) result;
}
obj_ BuildString_(const char* cString)
{
unsigned int length;
char* heapString;
obj_ start, stopper;
struct Standard__String__internal* strObj;
// Copy the string to the heap.
length = strlen(cString);
heapString = (char*) GC_MALLOC_ATOMIC(length + 1);
memcpy(heapString, cString, length + 1);
// Build the string object.
strObj =
(struct Standard__String__internal*)
GC_MALLOC(sizeof(struct Standard__String__internal));
strObj->class_ = StdClassRef_(String);
strObj->start = BuildBytePtr_((byte_ptr_) heapString);
strObj->stopper = BuildBytePtr_((byte_ptr_) heapString + length);
return (obj_) strObj;
}
obj_ BuildStringOfLength_(const char* cString, unsigned int length)
{
char* heapString;
struct Standard__String__internal* strObj;
// Copy the string to the heap.
heapString = (char*) GC_MALLOC(length + 1);
memcpy(heapString, cString, length + 1);
// Build the string object.
strObj =
(struct Standard__String__internal*)
GC_MALLOC(sizeof(struct Standard__String__internal));
strObj->class_ = StdClassRef_(String);
strObj->start = BuildBytePtr_((byte_ptr_) heapString);
strObj->stopper = BuildBytePtr_((byte_ptr_) heapString + length);
return (obj_) strObj;
}
obj_ BuildStringStartStopper_(const char* start, const char* stopper)
{
struct Standard__String__internal* strObj;
// Build the string object.
strObj =
(struct Standard__String__internal*)
GC_MALLOC(sizeof(struct Standard__String__internal));
strObj->class_ = StdClassRef_(String);
strObj->start = BuildBytePtr_((byte_ptr_) start);
strObj->stopper = BuildBytePtr_((byte_ptr_) stopper);
return (obj_) strObj;
}
char* CString_(obj_ str)
{
struct Standard__String__internal* strObj =
(struct Standard__String__internal*) str;
char* start;
unsigned int length;
char* cString;
start = StringStart_(strObj);
length = StringStopper_(strObj) - start;
cString = GC_MALLOC_ATOMIC(length + 1);
memcpy(cString, start, length);
cString[length] = 0;
return cString;
}
void* Allocate_(int numBytes)
{
return GC_MALLOC(numBytes);
}
void* AllocNonPtr_(int numBytes)
{
return GC_MALLOC_ATOMIC(numBytes);
}
obj_ CloneObj_(obj_ object)
{
return AllocObjFromClassInfo_(object->class_);
}
obj_ CloneObjExtra_(obj_ object, int numExtraFields)
{
size_t size =
sizeof(classref_) +
(object->class_->numSlots + numExtraFields) * sizeof(obj_);
obj_ newObject = (obj_) GC_MALLOC(size);
newObject->class_ = object->class_;
return newObject;
}
int SymToEnum_(
obj_ symbol, const EnumDictEntry_* dict, int dictSize, int notFoundValue)
{
/* The dictionary is sorted by symbol name, and the symbol objects are sorted
in memory, so we can use a binary search. */
unsigned int low = 0;
unsigned int high = dictSize - 1;
while (low <= high) {
unsigned int mid = (low + high) / 2;
const EnumDictEntry_* entry = &dict[mid];
if (entry->symbol > symbol)
high = mid - 1;
else if (entry->symbol < symbol)
low = mid + 1;
else
return entry->value;
}
/* Not found. */
return notFoundValue;
}
obj_ EnumToSym_(int value, const EnumDictEntry_* dict, int dictSize)
{
/* Linear search. */
const EnumDictEntry_* entry = dict;
const EnumDictEntry_* stopper = &dict[dictSize];
for (; entry < stopper; ++entry) {
if (entry->value == value)
return entry->symbol;
}
return nil;
}
int main(int argc, char* argv[])
{
obj_ args, result;
int whichArg;
extern obj_ main_co___Main(obj_ this_, obj_ args);
extern obj_ new__List__Standard(obj_ this_);
UsingMethod_(append_co_)
UsingClass_(Main) UsingClass_(List__Standard)
GC_INIT();
// Build the list of args.
args = new__List__Standard(Proto_(List__Standard));
for (whichArg = 0; whichArg < argc; ++whichArg)
Call_(append_co_, args, BuildString_(argv[whichArg]));
// Call the main function.
result = main_co___Main(Proto_(Main), args);
// Return the result.
if (result && result->class_ == StdClassRef_(Int))
return IntValue_(result);
else
return 1;
}
/* Debugging */
const char* className_(obj_ object)
{
if (object == NULL)
return "NULL";
return StringStart_(object->class_->name);
}
obj_ showObj_(obj_ object)
{
obj_ str;
UsingMethod_(debug_write)
if (object == NULL)
return NULL;
str = Call_(debug_write, object);
return object;
}
/*
Copyright 2005 - 2007 Steve Folta.
See the License file.
*/