|
1 #include <cstring> |
|
2 #include <cstdio> |
|
3 #include <cstdlib> |
|
4 #include <cstdint> |
|
5 #include <ctime> |
|
6 |
|
7 #include <string> |
|
8 #include <iostream> |
|
9 #include <iomanip> |
|
10 #include <fstream> |
|
11 #include <vector> |
|
12 |
|
13 #include <netinet/in.h> |
|
14 |
|
15 using namespace std; |
|
16 |
|
17 typedef void *obj; |
|
18 typedef intptr_t smi; |
|
19 |
|
20 static inline int isSmi(obj o) { return ((intptr_t) o) & 1; } |
|
21 static inline obj mkSmi(intptr_t v) { return (obj) ((v << 1) | 1); } |
|
22 static inline smi unSmi(obj o) { return ((smi) o) >> 1; } |
|
23 |
|
24 static inline intptr_t rawCount(obj o) { return ((intptr_t *) o)[0]; } |
|
25 static inline int isBytes(obj o) { return rawCount(o) & 1; } |
|
26 static inline unsigned slotCount(obj o) { return rawCount(o) >> 1; } |
|
27 static inline unsigned bytesCount(obj o) { return rawCount(o) >> 1; } |
|
28 static inline obj slotAt(obj o, unsigned i) { return ((obj *) o)[i + 2]; } |
|
29 static inline void slotAtPut(obj o, unsigned i, obj v) { ((obj *) o)[i + 2] = v; } |
|
30 static inline uint8_t *bvBytes(obj o) { return (uint8_t *) &(((obj *) o)[2]); } |
|
31 static inline string bvString(obj o) { return string((char *) bvBytes(o), bytesCount(o)); } |
|
32 |
|
33 static inline intptr_t rawCount(unsigned count, int isBs) { |
|
34 return (intptr_t) count << 1 | (isBs & 1); |
|
35 } |
|
36 |
|
37 static inline void _fillWith(obj o, unsigned n, obj v) { |
|
38 for (unsigned i = 0; i < n; i++) { |
|
39 slotAtPut(o, i, v); |
|
40 } |
|
41 } |
|
42 |
|
43 static int32_t nextInt(istream &f) { |
|
44 int32_t i; |
|
45 f.read((char *) &i, sizeof(int32_t)); |
|
46 return ntohl(i); |
|
47 } |
|
48 |
|
49 struct VM; |
|
50 |
|
51 typedef obj (*primitive_handler_t)(unsigned, VM &, obj); |
|
52 |
|
53 static obj unhandledPrimitive(unsigned, VM &, obj); |
|
54 |
|
55 struct VM { |
|
56 obj *allocBase; |
|
57 obj *allocPtr; |
|
58 obj *allocLimit; |
|
59 |
|
60 obj _nil; |
|
61 obj _true; |
|
62 obj _false; |
|
63 obj _Array; |
|
64 obj _Block; |
|
65 obj _Context; |
|
66 obj _Integer; |
|
67 |
|
68 primitive_handler_t primitiveTable[256]; |
|
69 |
|
70 inline obj objClass(obj o) { |
|
71 return isSmi(o) ? _Integer : ((obj *) o)[1]; |
|
72 } |
|
73 |
|
74 string className(obj c) { |
|
75 if (c == _nil) return "(nil)"; |
|
76 return bvString(slotAt(c, 0)); |
|
77 } |
|
78 |
|
79 string objClassName(obj o) { |
|
80 return className(objClass(o)); |
|
81 } |
|
82 |
|
83 VM() { |
|
84 unsigned heapSize = 1048576000; |
|
85 allocBase = allocPtr = new obj[heapSize]; |
|
86 allocLimit = &allocPtr[heapSize]; |
|
87 for (int i = 0; i < 256; i++) primitiveTable[i] = unhandledPrimitive; |
|
88 } |
|
89 |
|
90 void gc() { |
|
91 cerr << "GC! urk" << endl; |
|
92 exit(1); |
|
93 } |
|
94 |
|
95 obj allocObj(unsigned nSlots, obj klass) { |
|
96 retry: |
|
97 obj result = (obj) allocPtr; |
|
98 *allocPtr++ = (obj) rawCount(nSlots, 0); |
|
99 *allocPtr++ = klass; |
|
100 allocPtr += nSlots; |
|
101 if (allocPtr >= allocLimit) { |
|
102 allocPtr = (obj *) result; |
|
103 gc(); |
|
104 goto retry; |
|
105 } |
|
106 return result; |
|
107 } |
|
108 |
|
109 obj allocBytes(unsigned nBytes, obj klass) { |
|
110 retry: |
|
111 obj result = (obj) allocPtr; |
|
112 *allocPtr++ = (obj) rawCount(nBytes, 1); |
|
113 *allocPtr++ = klass; |
|
114 nBytes = (nBytes + sizeof(obj) - 1) & ~(sizeof(obj) - 1); |
|
115 allocPtr = (obj *) (((uint8_t *) allocPtr) + nBytes); |
|
116 if (allocPtr >= allocLimit) { |
|
117 allocPtr = (obj *) result; |
|
118 gc(); |
|
119 goto retry; |
|
120 } |
|
121 return result; |
|
122 } |
|
123 |
|
124 obj allocBytes(string const &s, obj klass) { |
|
125 obj o = allocBytes(s.length(), klass); |
|
126 memcpy(bvBytes(o), s.c_str(), s.length()); |
|
127 return o; |
|
128 } |
|
129 |
|
130 obj allocRawArray(unsigned n) { |
|
131 return allocObj(n, _Array); |
|
132 } |
|
133 |
|
134 obj allocArray(unsigned n, obj v) { |
|
135 obj o = allocRawArray(n); |
|
136 _fillWith(o, n, v); |
|
137 return o; |
|
138 } |
|
139 |
|
140 obj allocArray(unsigned n) { |
|
141 return allocArray(n, _nil); |
|
142 } |
|
143 |
|
144 void print(ostream &f, obj o) { |
|
145 if (isSmi(o)) { |
|
146 f << unSmi(o); |
|
147 return; |
|
148 } |
|
149 |
|
150 string className = objClassName(o); |
|
151 int isString = className.compare("String") == 0; |
|
152 |
|
153 if (!isString) { |
|
154 f << className; |
|
155 } |
|
156 |
|
157 if (isBytes(o)) { |
|
158 f << (isString ? '\"' : '{'); |
|
159 ios_base::fmtflags saved(f.flags()); |
|
160 char fill = f.fill('0'); |
|
161 f << hex; |
|
162 |
|
163 for (int i = 0; i < bytesCount(o); i++) { |
|
164 char c = bvBytes(o)[i]; |
|
165 if (isString) { |
|
166 switch (c) { |
|
167 case '\"': f << "\\\""; break; |
|
168 case '\n': f << "\\n"; break; |
|
169 default: f << c; break; |
|
170 } |
|
171 } else { |
|
172 f << setw(2) << ((unsigned) c & 0xff); |
|
173 } |
|
174 } |
|
175 |
|
176 f.flags(saved); |
|
177 f.fill(fill); |
|
178 |
|
179 f << (isString ? '\"' : '}'); |
|
180 } else { |
|
181 } |
|
182 } |
|
183 |
|
184 int loadImage(string const &filename) { |
|
185 ifstream f(filename); |
|
186 vector<obj> table; |
|
187 |
|
188 if (!f.is_open()) return 0; |
|
189 if (nextInt(f) != 1 || !f) return 0; // wrong image version |
|
190 |
|
191 while (true) { |
|
192 int32_t len = nextInt(f); |
|
193 if (!f) break; |
|
194 int32_t code = nextInt(f); |
|
195 int32_t classIndex = nextInt(f); |
|
196 int32_t oopCount = nextInt(f); |
|
197 |
|
198 switch (code) { |
|
199 case 0: |
|
200 if (len != 5) return 0; // weird SmallInt object length |
|
201 if (oopCount != 0) return 0; // SmallInt doesn't get to have slots |
|
202 table.push_back(mkSmi(nextInt(f))); |
|
203 break; |
|
204 |
|
205 case 1: { |
|
206 int32_t byteCount = len - oopCount - 4; |
|
207 obj bv = allocBytes(byteCount, mkSmi(classIndex)); |
|
208 f.read((char *) bvBytes(bv), byteCount); |
|
209 table.push_back(bv); |
|
210 break; |
|
211 } |
|
212 |
|
213 case 2: { |
|
214 obj o = allocObj(oopCount, mkSmi(classIndex)); |
|
215 for (int i = 0; i < oopCount; i++) { |
|
216 slotAtPut(o, i, mkSmi(nextInt(f))); |
|
217 } |
|
218 table.push_back(o); |
|
219 break; |
|
220 } |
|
221 |
|
222 default: |
|
223 return 0; // unhandled code |
|
224 } |
|
225 } |
|
226 |
|
227 for (vector<obj>::iterator it = table.begin(); it != table.end(); ++it) { |
|
228 obj o = *it; |
|
229 if (!isSmi(o)) { |
|
230 unsigned count = 1 + (isBytes(o) ? 0 : slotCount(o)); |
|
231 for (unsigned i = 0; i < count; i++) { |
|
232 (((obj *) o)[i + 1]) = table[unSmi(((obj *) o)[i + 1])]; |
|
233 } |
|
234 } |
|
235 } |
|
236 |
|
237 _nil = table[0]; |
|
238 _true = table[1]; |
|
239 _false = table[2]; |
|
240 _Array = table[3]; |
|
241 _Block = table[4]; |
|
242 _Context = table[5]; |
|
243 _Integer = table[6]; |
|
244 |
|
245 return 1; |
|
246 } |
|
247 |
|
248 obj searchClassMethodDictionary(obj c, obj selector) { |
|
249 obj methods = slotAt(c, 2); |
|
250 unsigned selectorLen = bytesCount(selector); |
|
251 char const *selectorBytes = (char const *) bvBytes(selector); |
|
252 for (unsigned i = 0; i < slotCount(methods); i++) { |
|
253 obj m = slotAt(methods, i); |
|
254 obj mname = slotAt(m, 0); |
|
255 // cerr << " " << selectorBytes << " =?= " << bvString(mname) << endl; |
|
256 if ((bytesCount(mname) == selectorLen) && |
|
257 (memcmp(bvBytes(mname), selectorBytes, selectorLen) == 0)) { |
|
258 return m; |
|
259 } |
|
260 } |
|
261 return 0; |
|
262 } |
|
263 |
|
264 obj buildContext(obj prevCtx, obj args, obj method) { |
|
265 unsigned tempCount = (unsigned) unSmi(slotAt(method, 4)); |
|
266 unsigned maxStack = (unsigned) unSmi(slotAt(method, 3)); |
|
267 obj ctx = allocObj(7, _Context); |
|
268 slotAtPut(ctx, 0, method); |
|
269 slotAtPut(ctx, 1, args); |
|
270 slotAtPut(ctx, 2, allocArray(tempCount)); |
|
271 slotAtPut(ctx, 3, allocArray(maxStack)); |
|
272 slotAtPut(ctx, 4, mkSmi(0)); |
|
273 slotAtPut(ctx, 5, mkSmi(0)); |
|
274 slotAtPut(ctx, 6, prevCtx); |
|
275 return ctx; |
|
276 } |
|
277 |
|
278 obj __ctx, __method, __args, __temps, __stack, __prevCtx, __receiver, __literals; |
|
279 unsigned __ip, __stackTop; |
|
280 uint8_t *__bytecode; |
|
281 |
|
282 void push(obj v) { |
|
283 slotAtPut(__stack, __stackTop++, v); |
|
284 } |
|
285 |
|
286 obj pop() { |
|
287 return slotAt(__stack, --__stackTop); |
|
288 } |
|
289 |
|
290 obj peek() { |
|
291 return slotAt(__stack, __stackTop - 1); |
|
292 } |
|
293 |
|
294 uint8_t nextByte() { |
|
295 return __bytecode[__ip++]; |
|
296 } |
|
297 |
|
298 obj popArray(uint8_t count) { |
|
299 obj a = allocRawArray(count); |
|
300 __stackTop -= count; |
|
301 for (int i = 0; i < count; i++) { |
|
302 slotAtPut(a, i, slotAt(__stack, __stackTop + i)); |
|
303 } |
|
304 return a; |
|
305 } |
|
306 |
|
307 void loadContext(obj ctx); |
|
308 |
|
309 void loadContextAndPush(obj ctx, obj v) { |
|
310 loadContext(ctx); |
|
311 push(v); |
|
312 } |
|
313 |
|
314 void storeRegisters() { |
|
315 slotAtPut(__ctx, 4, mkSmi(__ip)); |
|
316 slotAtPut(__ctx, 5, mkSmi(__stackTop)); |
|
317 } |
|
318 |
|
319 obj lookupMethod(obj c, obj selector) { |
|
320 while (c != _nil) { |
|
321 // cerr << "Searching in " << className(c) << " for "; print(cerr, selector); cerr << endl; |
|
322 obj m = searchClassMethodDictionary(c, selector); |
|
323 if (m != 0) return m; |
|
324 c = slotAt(c, 1); |
|
325 } |
|
326 return 0; |
|
327 } |
|
328 |
|
329 void sendMessage(obj c, obj newArgs, obj selector) { |
|
330 storeRegisters(); |
|
331 // cerr << "Sending " << bvString(selector) << " via " << className(c) << " to "; |
|
332 // print(cerr, slotAt(newArgs, 0)); |
|
333 // cerr << endl; |
|
334 obj method = lookupMethod(c, selector); |
|
335 if (method == 0) { |
|
336 cerr << "DNU "; |
|
337 print(cerr, slotAt(newArgs, 0)); |
|
338 cerr << ' '; |
|
339 print(cerr, selector); |
|
340 cerr << endl; |
|
341 exit(2); |
|
342 } |
|
343 loadContext(buildContext(__ctx, newArgs, method)); |
|
344 } |
|
345 |
|
346 void interpret() { |
|
347 while (__ctx != _nil) { |
|
348 uint8_t opcode, arg; |
|
349 opcode = nextByte(); |
|
350 arg = opcode & 0xf; |
|
351 opcode = opcode >> 4; |
|
352 if (opcode == 0) { |
|
353 opcode = arg; |
|
354 arg = nextByte(); |
|
355 } |
|
356 |
|
357 // cout << (int) opcode << ' ' << (int) arg; |
|
358 // for (int i = 0; i < __stackTop; i++) { |
|
359 // cout << ' '; |
|
360 // print(cout, slotAt(__stack, i)); |
|
361 // } |
|
362 // cout << endl; |
|
363 // cout << flush; |
|
364 |
|
365 switch (opcode) { |
|
366 case 1: push(slotAt(__receiver, arg)); continue; |
|
367 case 2: push(slotAt(__args, arg)); continue; |
|
368 case 3: push(slotAt(__temps, arg)); continue; |
|
369 case 4: push(slotAt(__literals, arg)); continue; |
|
370 case 5: |
|
371 if (arg < 10) { |
|
372 push(mkSmi(arg)); |
|
373 } else { |
|
374 switch (arg) { |
|
375 case 10: push(_nil); break; |
|
376 case 11: push(_true); break; |
|
377 case 12: push(_false); break; |
|
378 } |
|
379 } |
|
380 continue; |
|
381 case 6: slotAtPut(__receiver, arg, peek()); continue; |
|
382 case 7: slotAtPut(__temps, arg, peek()); continue; |
|
383 case 8: push(popArray(arg)); continue; |
|
384 case 9: { |
|
385 obj newArgs = pop(); |
|
386 sendMessage(objClass(slotAt(newArgs, 0)), newArgs, slotAt(__literals, arg)); |
|
387 continue; |
|
388 } |
|
389 case 10: |
|
390 switch (arg) { |
|
391 case 0: push((pop() == _nil) ? _true : _false); continue; |
|
392 case 1: push((pop() != _nil) ? _true : _false); continue; |
|
393 } |
|
394 case 11: { |
|
395 obj j = pop(); |
|
396 obj i = pop(); |
|
397 if (isSmi(i) && isSmi(j)) { |
|
398 switch (arg) { |
|
399 case 0: push((unSmi(i) < unSmi(j)) ? _true : _false); continue; |
|
400 case 1: push((unSmi(i) <= unSmi(j)) ? _true : _false); continue; |
|
401 case 2: push(mkSmi(unSmi(i) + unSmi(j))); continue; |
|
402 } |
|
403 } else { |
|
404 obj newArgs = allocRawArray(2); |
|
405 slotAtPut(newArgs, 0, i); |
|
406 slotAtPut(newArgs, 1, j); |
|
407 obj selector; |
|
408 switch (arg) { |
|
409 case 0: selector = allocBytes("<", _nil); break; |
|
410 case 1: selector = allocBytes("<=", _nil); break; |
|
411 case 2: selector = allocBytes("+", _nil); break; |
|
412 } |
|
413 sendMessage(objClass(i), newArgs, selector); |
|
414 continue; |
|
415 } |
|
416 } |
|
417 case 12: { |
|
418 uint8_t target = nextByte(); |
|
419 obj block = allocObj(10, _Block); |
|
420 slotAtPut(block, 0, __method); |
|
421 slotAtPut(block, 1, __args); |
|
422 slotAtPut(block, 2, __temps); |
|
423 slotAtPut(block, 3, __stack); |
|
424 slotAtPut(block, 4, mkSmi(__ip)); |
|
425 slotAtPut(block, 5, mkSmi(0)); |
|
426 slotAtPut(block, 6, __prevCtx); |
|
427 slotAtPut(block, 7, mkSmi(arg)); |
|
428 slotAtPut(block, 8, __ctx); |
|
429 slotAtPut(block, 9, mkSmi(__ip)); |
|
430 push(block); |
|
431 __ip = target; |
|
432 continue; |
|
433 } |
|
434 case 13: { |
|
435 uint8_t primNumber = nextByte(); |
|
436 // cerr << " primNumber = " << (int) primNumber << endl; |
|
437 switch (primNumber) { |
|
438 case 6: { |
|
439 loadContext(pop()); |
|
440 continue; |
|
441 } |
|
442 case 8: { |
|
443 obj block = pop(); |
|
444 unsigned argloc = unSmi(slotAt(block, 7)); |
|
445 unsigned argcount = arg - 1; |
|
446 for (unsigned i = 0; i < argcount; i++) { |
|
447 slotAtPut(slotAt(block, 2), |
|
448 argloc + i, |
|
449 slotAt(__stack, __stackTop - argcount + i)); |
|
450 } |
|
451 __stackTop = __stackTop - argcount; |
|
452 storeRegisters(); |
|
453 obj blockCtx = allocObj(10, _Block); |
|
454 slotAtPut(blockCtx, 0, slotAt(block, 0)); |
|
455 slotAtPut(blockCtx, 1, slotAt(block, 1)); |
|
456 slotAtPut(blockCtx, 2, slotAt(block, 2)); |
|
457 slotAtPut(blockCtx, 3, allocArray(slotCount(slotAt(block, 3)))); |
|
458 slotAtPut(blockCtx, 4, slotAt(block, 9)); |
|
459 slotAtPut(blockCtx, 5, mkSmi(0)); |
|
460 slotAtPut(blockCtx, 6, slotAt(__ctx, 6)); |
|
461 slotAtPut(blockCtx, 7, slotAt(block, 7)); |
|
462 slotAtPut(blockCtx, 8, slotAt(block, 8)); |
|
463 slotAtPut(blockCtx, 9, slotAt(block, 9)); |
|
464 loadContext(blockCtx); |
|
465 continue; |
|
466 } |
|
467 case 34: return; |
|
468 case 35: push(__ctx); continue; |
|
469 default: |
|
470 push(primitiveTable[primNumber](primNumber, *this, popArray(arg))); |
|
471 continue; |
|
472 } |
|
473 } |
|
474 case 14: push(slotAt(objClass(__receiver), arg + 5)); continue; |
|
475 case 15: |
|
476 switch (arg) { |
|
477 case 1: loadContextAndPush(__prevCtx, __receiver); continue; |
|
478 case 2: loadContextAndPush(__prevCtx, pop()); continue; |
|
479 case 3: loadContextAndPush(slotAt(slotAt(__ctx, 8), 6), pop()); continue; |
|
480 case 4: push(peek()); continue; |
|
481 case 5: pop(); continue; |
|
482 case 6: __ip = nextByte(); continue; |
|
483 case 7: { |
|
484 unsigned target = nextByte(); |
|
485 if (pop() == _true) __ip = target; |
|
486 continue; |
|
487 } |
|
488 case 8: { |
|
489 unsigned target = nextByte(); |
|
490 if (pop() == _false) __ip = target; |
|
491 continue; |
|
492 } |
|
493 case 11: { |
|
494 obj selector = slotAt(__literals, nextByte()); |
|
495 obj newArgs = pop(); |
|
496 obj definingClass = slotAt(__method, 5); |
|
497 obj super = slotAt(definingClass, 1); |
|
498 sendMessage(super, newArgs, selector); |
|
499 continue; |
|
500 } |
|
501 default: |
|
502 cerr << "Unhandled opcode 15 arg " << (int) arg << endl; |
|
503 exit(1); |
|
504 } |
|
505 default: |
|
506 cerr << "Unhandled opcode " << (int) opcode << ", " << (int) arg << endl; |
|
507 exit(1); |
|
508 } |
|
509 } |
|
510 } |
|
511 |
|
512 void installPrimitive(unsigned primNumber, primitive_handler_t handler) { |
|
513 primitiveTable[primNumber] = handler; |
|
514 } |
|
515 }; |
|
516 |
|
517 void VM::loadContext(obj ctx) { |
|
518 __ctx = ctx; |
|
519 if (ctx != _nil) { |
|
520 __method = slotAt(ctx, 0); |
|
521 __args = slotAt(ctx, 1); |
|
522 __temps = slotAt(ctx, 2); |
|
523 __stack = slotAt(ctx, 3); |
|
524 __ip = unSmi(slotAt(ctx, 4)); |
|
525 __stackTop = unSmi(slotAt(ctx, 5)); |
|
526 __prevCtx = slotAt(ctx, 6); |
|
527 |
|
528 __receiver = slotAt(__args, 0); |
|
529 |
|
530 __bytecode = bvBytes(slotAt(__method, 1)); |
|
531 __literals = slotAt(__method, 2); |
|
532 } |
|
533 } |
|
534 |
|
535 static obj unhandledPrimitive(unsigned primNumber, VM &vm, obj args) { |
|
536 cerr << "Primitive " << primNumber << " is unhandled" << endl; |
|
537 exit(1); |
|
538 } |
|
539 |
|
540 static obj prim_eq(unsigned primNumber, VM &vm, obj args) { |
|
541 return (slotAt(args, 0) == slotAt(args, 1)) ? vm._true : vm._false; |
|
542 } |
|
543 |
|
544 static obj prim_getClass(unsigned primNumber, VM &vm, obj args) { |
|
545 return vm.objClass(slotAt(args, 0)); |
|
546 } |
|
547 |
|
548 static obj prim_objectSize(unsigned primNumber, VM &vm, obj args) { |
|
549 obj o = slotAt(args, 0); |
|
550 if (isSmi(o)) return mkSmi(0); |
|
551 if (isBytes(o)) return mkSmi(bytesCount(o)); |
|
552 return mkSmi(slotCount(o)); |
|
553 } |
|
554 |
|
555 static obj prim_slotAtPut(unsigned primNumber, VM &vm, obj args) { |
|
556 slotAtPut(slotAt(args, 1), unSmi(slotAt(args, 2)) - 1, slotAt(args, 0)); |
|
557 return slotAt(args, 1); |
|
558 } |
|
559 |
|
560 static obj prim_mkObject(unsigned primNumber, VM &vm, obj args) { |
|
561 obj r = vm.allocObj(unSmi(slotAt(args, 1)), slotAt(args, 0)); |
|
562 _fillWith(r, slotCount(r), vm._nil); |
|
563 return r; |
|
564 } |
|
565 |
|
566 static obj prim_numericQuotient(unsigned primNumber, VM &vm, obj args) { |
|
567 return mkSmi(unSmi(slotAt(args, 0)) / unSmi(slotAt(args, 1))); |
|
568 } |
|
569 |
|
570 static obj prim_numericModulo(unsigned primNumber, VM &vm, obj args) { |
|
571 return mkSmi(unSmi(slotAt(args, 0)) % unSmi(slotAt(args, 1))); |
|
572 } |
|
573 |
|
574 static obj prim_numericEq(unsigned primNumber, VM &vm, obj args) { |
|
575 return (slotAt(args, 0) == slotAt(args, 1)) ? vm._true : vm._false; |
|
576 } |
|
577 |
|
578 static obj prim_numericSub(unsigned primNumber, VM &vm, obj args) { |
|
579 return mkSmi(unSmi(slotAt(args, 0)) - unSmi(slotAt(args, 1))); |
|
580 } |
|
581 |
|
582 static obj prim_numericMul(unsigned primNumber, VM &vm, obj args) { |
|
583 // TODO overflow checks |
|
584 return mkSmi(unSmi(slotAt(args, 0)) * unSmi(slotAt(args, 1))); |
|
585 } |
|
586 |
|
587 static obj prim_mkBytes(unsigned primNumber, VM &vm, obj args) { |
|
588 return vm.allocBytes(unSmi(slotAt(args, 1)), slotAt(args, 0)); |
|
589 } |
|
590 |
|
591 static obj prim_bytesRef(unsigned primNumber, VM &vm, obj args) { |
|
592 return mkSmi(bvBytes(slotAt(args, 0))[unSmi(slotAt(args, 1)) - 1]); |
|
593 } |
|
594 |
|
595 static obj prim_bytesSet(unsigned primNumber, VM &vm, obj args) { |
|
596 bvBytes(slotAt(args, 1))[unSmi(slotAt(args, 2)) - 1] = unSmi(slotAt(args, 0)); |
|
597 return slotAt(args, 1); |
|
598 } |
|
599 |
|
600 static obj prim_bytesAppend(unsigned primNumber, VM &vm, obj args) { |
|
601 obj b = slotAt(args, 0); |
|
602 obj a = slotAt(args, 1); |
|
603 obj r = vm.allocBytes(bytesCount(a) + bytesCount(b), vm.objClass(a)); |
|
604 memcpy(bvBytes(r), bvBytes(a), bytesCount(a)); |
|
605 memcpy(bvBytes(r) + bytesCount(a), bvBytes(b), bytesCount(b)); |
|
606 return r; |
|
607 } |
|
608 |
|
609 static obj prim_bytesCmp(unsigned primNumber, VM &vm, obj args) { |
|
610 obj a = slotAt(args, 0); |
|
611 obj b = slotAt(args, 1); |
|
612 int alen = bytesCount(a); |
|
613 int blen = bytesCount(a); |
|
614 int minlen = alen < blen ? alen : blen; |
|
615 int result = memcmp(bvBytes(a), bvBytes(b), minlen); |
|
616 if (result == 0) { |
|
617 result = alen - blen; |
|
618 } |
|
619 if (result < 0) return mkSmi(-1); |
|
620 if (result > 0) return mkSmi(1); |
|
621 return mkSmi(0); |
|
622 } |
|
623 |
|
624 static obj prim_slotAt(unsigned primNumber, VM &vm, obj args) { |
|
625 return slotAt(slotAt(args, 0), unSmi(slotAt(args, 1)) - 1); |
|
626 } |
|
627 |
|
628 static obj prim_extendObj(unsigned primNumber, VM &vm, obj args) { |
|
629 obj v = slotAt(args, 0); |
|
630 obj o = slotAt(args, 1); |
|
631 obj r = vm.allocObj(slotCount(o) + 1, vm.objClass(o)); |
|
632 for (int i = 0; i < slotCount(o); i++) slotAtPut(r, i, slotAt(o, i)); |
|
633 slotAtPut(r, slotCount(o), v); |
|
634 return r; |
|
635 } |
|
636 |
|
637 static obj prim_createWindow(unsigned primNumber, VM &vm, obj args) { |
|
638 cerr << "Creating window "; |
|
639 vm.print(cerr, slotAt(args, 0)); |
|
640 cerr << endl; |
|
641 return vm.allocObj(0, slotAt(args, 0)); |
|
642 } |
|
643 |
|
644 static obj prim_showWindow(unsigned primNumber, VM &vm, obj args) { |
|
645 cerr << "Show/hide window " << slotAt(args, 0) << " " << (slotAt(args, 1) == vm._true) << endl; |
|
646 return slotAt(args, 1); |
|
647 } |
|
648 |
|
649 static obj prim_setContentPane(unsigned primNumber, VM &vm, obj args) { |
|
650 cerr << "Setting content pane " << slotAt(args, 0) << endl; |
|
651 return slotAt(args, 0); |
|
652 } |
|
653 |
|
654 static obj prim_setWindowSize(unsigned primNumber, VM &vm, obj args) { |
|
655 cout << slotAt(args, 0) << "prim_setWindowSize:" |
|
656 << " height " << unSmi(slotAt(args, 1)) |
|
657 << " width " << unSmi(slotAt(args, 2)) << endl; |
|
658 return slotAt(args, 0); |
|
659 } |
|
660 |
|
661 static obj prim_setWindowMenu(unsigned primNumber, VM &vm, obj args) { |
|
662 cout << slotAt(args, 0) << "prim_setWindowMenu" << endl; |
|
663 return slotAt(args, 0); |
|
664 } |
|
665 |
|
666 static obj prim_setWindowTitle(unsigned primNumber, VM &vm, obj args) { |
|
667 cout << slotAt(args, 0) << "prim_setWindowTitle: " << bvString(slotAt(args, 1)) << endl; |
|
668 return slotAt(args, 0); |
|
669 } |
|
670 |
|
671 static obj prim_repaintWindow(unsigned primNumber, VM &vm, obj args) { |
|
672 cout << slotAt(args, 0) << "prim_repaintWindow" << endl; |
|
673 return slotAt(args, 0); |
|
674 } |
|
675 |
|
676 static obj prim_createButton(unsigned primNumber, VM &vm, obj args) { |
|
677 cerr << "Creating button "; |
|
678 vm.print(cerr, slotAt(args, 0)); |
|
679 cerr << " with label " << bvString(slotAt(args, 1)) << endl; |
|
680 return vm.allocObj(0, slotAt(args, 0)); |
|
681 } |
|
682 |
|
683 static obj prim_createTextArea(unsigned primNumber, VM &vm, obj args) { |
|
684 cerr << "Creating textarea "; |
|
685 vm.print(cerr, slotAt(args, 0)); |
|
686 cerr << endl; |
|
687 return vm.allocObj(0, slotAt(args, 0)); |
|
688 } |
|
689 |
|
690 static obj prim_updateTextArea(unsigned primNumber, VM &vm, obj args) { |
|
691 cout << slotAt(args, 0) << "prim_updateTextArea: " << bvString(slotAt(args, 1)) << endl; |
|
692 return slotAt(args, 1); |
|
693 } |
|
694 |
|
695 static obj prim_createMenu(unsigned primNumber, VM &vm, obj args) { |
|
696 cerr << "Creating menu "; |
|
697 vm.print(cerr, slotAt(args, 0)); |
|
698 cerr << " with title " << bvString(slotAt(args, 1)) << endl; |
|
699 return vm.allocObj(0, slotAt(args, 0)); |
|
700 } |
|
701 |
|
702 static obj prim_createMenuItem(unsigned primNumber, VM &vm, obj args) { |
|
703 cerr << "Creating menu item "; |
|
704 vm.print(cerr, slotAt(args, 0)); |
|
705 cerr << " with title " << bvString(slotAt(args, 1)) << endl; |
|
706 return slotAt(args, 0); |
|
707 } |
|
708 |
|
709 static obj prim_closeWindowHandler(unsigned primNumber, VM &vm, obj args) { |
|
710 cerr << "Ignoring prim_closeWindowHandler" << endl; |
|
711 return slotAt(args, 0); |
|
712 } |
|
713 |
|
714 static struct timespec ts_start; |
|
715 static obj prim_getMilliseconds(unsigned primNumber, VM &vm, obj args) { |
|
716 struct timespec ts_end; |
|
717 clock_gettime(CLOCK_MONOTONIC, &ts_end); |
|
718 intptr_t delta = (ts_end.tv_sec - ts_start.tv_sec) * 1000; |
|
719 delta += (ts_end.tv_nsec / 1000000) - (ts_start.tv_nsec / 1000000); |
|
720 return mkSmi(delta); |
|
721 } |
|
722 |
|
723 int main(int argc, char *argv[]) { |
|
724 VM vm; |
|
725 |
|
726 clock_gettime(CLOCK_MONOTONIC, &ts_start); |
|
727 |
|
728 vm.installPrimitive(1, prim_eq); |
|
729 vm.installPrimitive(2, prim_getClass); |
|
730 vm.installPrimitive(4, prim_objectSize); |
|
731 vm.installPrimitive(5, prim_slotAtPut); |
|
732 vm.installPrimitive(7, prim_mkObject); |
|
733 vm.installPrimitive(11, prim_numericQuotient); |
|
734 vm.installPrimitive(12, prim_numericModulo); |
|
735 vm.installPrimitive(14, prim_numericEq); |
|
736 vm.installPrimitive(15, prim_numericMul); |
|
737 vm.installPrimitive(16, prim_numericSub); |
|
738 vm.installPrimitive(20, prim_mkBytes); |
|
739 vm.installPrimitive(21, prim_bytesRef); |
|
740 vm.installPrimitive(22, prim_bytesSet); |
|
741 vm.installPrimitive(24, prim_bytesAppend); |
|
742 vm.installPrimitive(26, prim_bytesCmp); |
|
743 vm.installPrimitive(30, prim_slotAt); |
|
744 vm.installPrimitive(31, prim_extendObj); |
|
745 vm.installPrimitive(60, prim_createWindow); |
|
746 vm.installPrimitive(61, prim_showWindow); |
|
747 vm.installPrimitive(62, prim_setContentPane); |
|
748 vm.installPrimitive(63, prim_setWindowSize); |
|
749 vm.installPrimitive(64, prim_setWindowMenu); |
|
750 vm.installPrimitive(65, prim_setWindowTitle); |
|
751 vm.installPrimitive(66, prim_repaintWindow); |
|
752 vm.installPrimitive(71, prim_createButton); |
|
753 vm.installPrimitive(73, prim_createTextArea); |
|
754 vm.installPrimitive(82, prim_updateTextArea); |
|
755 vm.installPrimitive(90, prim_createMenu); |
|
756 vm.installPrimitive(91, prim_createMenuItem); |
|
757 vm.installPrimitive(118, prim_closeWindowHandler); |
|
758 vm.installPrimitive(119, prim_getMilliseconds); |
|
759 |
|
760 if (!vm.loadImage("SmallWorld/src/image")) { |
|
761 cerr << "Could not load image" << endl; |
|
762 return 1; |
|
763 } |
|
764 |
|
765 cout << "Loaded!" << endl; |
|
766 |
|
767 { |
|
768 string bootCode = "[SmallWorld startUp. \ |
|
769 Transcript show: 0 tinyBenchmarks.] value"; |
|
770 // "SmallWorld startUp"; |
|
771 obj _String = vm.objClass(slotAt(vm.objClass(vm._true), 0)); // class String |
|
772 obj code = vm.allocBytes(bootCode, _String); |
|
773 obj selector = vm.allocBytes("doIt", _String); |
|
774 obj args = vm.allocRawArray(1); |
|
775 slotAtPut(args, 0, code); |
|
776 obj doIt = vm.searchClassMethodDictionary(vm.objClass(code), selector); |
|
777 obj ctx = vm.buildContext(vm._nil, args, doIt); |
|
778 vm.loadContext(ctx); |
|
779 vm.interpret(); |
|
780 cout << "Boot complete" << endl; |
|
781 cout << "Used: " << vm.allocPtr - vm.allocBase << endl; |
|
782 cout << flush; |
|
783 } |
|
784 |
|
785 return 0; |
|
786 } |