Debian Codegraph @

terps/glulxe/accel.c

raw symbols SCIP
1 /* accel.c: Glulxe code for accelerated functions
2 Designed by Andrew Plotkin <[email protected]>
3 http://eblong.com/zarf/glulx/index.html
4 */
5
6 #include "glk.h"
7 #include "glulxe.h"
8
9 /* Git passes along function arguments in reverse order. To make our lives
10 more interesting. */
11 #ifdef ARGS_REVERSED
12 #define ARG(argv, argc, ix) (argv[(argc-1)-ix])
13 #else
14 #define ARG(argv, argc, ix) (argv[ix])
15 #endif
16
17 /* Any function can be called with any number of arguments. This macro
18 lets us snarf a given argument, or zero if it wasn't supplied. */
19 #define ARG_IF_GIVEN(argv, argc, ix) ((argc > ix) ? (ARG(argv, argc, ix)) : 0)
20
21 static void accel_error(char *msg);
22 static glui32 func_1_z__region(glui32 argc, glui32 *argv);
23 static glui32 func_2_cp__tab(glui32 argc, glui32 *argv);
24 static glui32 func_3_ra__pr(glui32 argc, glui32 *argv);
25 static glui32 func_4_rl__pr(glui32 argc, glui32 *argv);
26 static glui32 func_5_oc__cl(glui32 argc, glui32 *argv);
27 static glui32 func_6_rv__pr(glui32 argc, glui32 *argv);
28 static glui32 func_7_op__pr(glui32 argc, glui32 *argv);
29 static glui32 func_8_cp__tab(glui32 argc, glui32 *argv);
30 static glui32 func_9_ra__pr(glui32 argc, glui32 *argv);
31 static glui32 func_10_rl__pr(glui32 argc, glui32 *argv);
32 static glui32 func_11_oc__cl(glui32 argc, glui32 *argv);
33 static glui32 func_12_rv__pr(glui32 argc, glui32 *argv);
34 static glui32 func_13_op__pr(glui32 argc, glui32 *argv);
35
36 static int obj_in_class(glui32 obj);
37 static glui32 get_prop(glui32 obj, glui32 id);
38 static glui32 get_prop_new(glui32 obj, glui32 id);
39
40 /* Parameters, set by @accelparam. */
41 static glui32 classes_table = 0; /* class object array */
42 static glui32 indiv_prop_start = 0; /* first individual prop ID */
43 static glui32 class_metaclass = 0; /* "Class" class object */
44 static glui32 object_metaclass = 0; /* "Object" class object */
45 static glui32 routine_metaclass = 0; /* "Routine" class object */
46 static glui32 string_metaclass = 0; /* "String" class object */
47 static glui32 self = 0; /* address of global "self" */
48 static glui32 num_attr_bytes = 0; /* number of attributes / 8 */
49 static glui32 cpv__start = 0; /* array of common prop defaults */
50
51 typedef struct accelentry_struct {
52 glui32 addr;
53 glui32 index;
54 acceleration_func func;
55 struct accelentry_struct *next;
56 } accelentry_t;
57
58 #define ACCEL_HASH_SIZE (511)
59
60 static accelentry_t **accelentries = NULL;
61
62 void init_accel()
63 {
64 accelentries = NULL;
65 }
66
67 acceleration_func accel_find_func(glui32 index)
68 {
69 switch (index) {
70 case 0: return NULL; /* 0 always means no acceleration */
71 case 1: return func_1_z__region;
72 case 2: return func_2_cp__tab;
73 case 3: return func_3_ra__pr;
74 case 4: return func_4_rl__pr;
75 case 5: return func_5_oc__cl;
76 case 6: return func_6_rv__pr;
77 case 7: return func_7_op__pr;
78 case 8: return func_8_cp__tab;
79 case 9: return func_9_ra__pr;
80 case 10: return func_10_rl__pr;
81 case 11: return func_11_oc__cl;
82 case 12: return func_12_rv__pr;
83 case 13: return func_13_op__pr;
84 }
85 return NULL;
86 }
87
88 acceleration_func accel_get_func(glui32 addr)
89 {
90 int bucknum;
91 accelentry_t *ptr;
92
93 if (!accelentries)
94 return NULL;
95
96 bucknum = (addr % ACCEL_HASH_SIZE);
97 for (ptr = accelentries[bucknum]; ptr; ptr = ptr->next) {
98 if (ptr->addr == addr)
99 return ptr->func;
100 }
101 return NULL;
102 }
103
104 /* Iterate the entire acceleration table, calling the callback for
105 each (non-NULL) entry. This is used only for autosave. */
106 void accel_iterate_funcs(void (*func)(glui32 index, glui32 addr))
107 {
108 int bucknum;
109 accelentry_t *ptr;
110
111 if (!accelentries)
112 return;
113
114 for (bucknum=0; bucknum<ACCEL_HASH_SIZE; bucknum++) {
115 for (ptr = accelentries[bucknum]; ptr; ptr = ptr->next) {
116 if (ptr->func) {
117 func(ptr->index, ptr->addr);
118 }
119 }
120 }
121 }
122
123 void accel_set_func(glui32 index, glui32 addr)
124 {
125 int bucknum;
126 accelentry_t *ptr;
127 int functype;
128 acceleration_func new_func = NULL;
129
130 /* Check the Glulx type identifier byte. */
131 functype = Mem1(addr);
132 if (functype != 0xC0 && functype != 0xC1) {
133 fatal_error_i("Attempt to accelerate non-function.", addr);
134 }
135
136 if (!accelentries) {
137 accelentries = (accelentry_t **)glulx_malloc(ACCEL_HASH_SIZE
138 * sizeof(accelentry_t *));
139 if (!accelentries)
140 fatal_error("Cannot malloc acceleration table.");
141 for (bucknum=0; bucknum<ACCEL_HASH_SIZE; bucknum++)
142 accelentries[bucknum] = NULL;
143 }
144
145 new_func = accel_find_func(index);
146 /* Might be NULL, if the index is zero or not recognized. */
147
148 bucknum = (addr % ACCEL_HASH_SIZE);
149 for (ptr = accelentries[bucknum]; ptr; ptr = ptr->next) {
150 if (ptr->addr == addr)
151 break;
152 }
153 if (!ptr) {
154 if (!new_func) {
155 return; /* no need for a new entry */
156 }
157 ptr = (accelentry_t *)glulx_malloc(sizeof(accelentry_t));
158 if (!ptr)
159 fatal_error("Cannot malloc acceleration entry.");
160 ptr->addr = addr;
161 ptr->index = 0;
162 ptr->func = NULL;
163 ptr->next = accelentries[bucknum];
164 accelentries[bucknum] = ptr;
165 }
166
167 ptr->index = index;
168 ptr->func = new_func;
169 }
170
171 void accel_set_param(glui32 index, glui32 val)
172 {
173 switch (index) {
174 case 0: classes_table = val; break;
175 case 1: indiv_prop_start = val; break;
176 case 2: class_metaclass = val; break;
177 case 3: object_metaclass = val; break;
178 case 4: routine_metaclass = val; break;
179 case 5: string_metaclass = val; break;
180 case 6: self = val; break;
181 case 7: num_attr_bytes = val; break;
182 case 8: cpv__start = val; break;
183 }
184 }
185
186 /* This is used only for autosave. */
187 glui32 accel_get_param_count()
188 {
189 return 9;
190 }
191
192 /* This is used only for autosave. */
193 glui32 accel_get_param(glui32 index)
194 {
195 switch (index) {
196 case 0: return classes_table;
197 case 1: return indiv_prop_start;
198 case 2: return class_metaclass;
199 case 3: return object_metaclass;
200 case 4: return routine_metaclass;
201 case 5: return string_metaclass;
202 case 6: return self;
203 case 7: return num_attr_bytes;
204 case 8: return cpv__start;
205 default: return 0;
206 }
207 }
208
209 static void accel_error(char *msg)
210 {
211 glui32 mode, rock;
212 stream_get_iosys(&mode, &rock);
213 if (mode == 2) { /* iosys_Glk */
214 glk_put_char('\n');
215 glk_put_string(msg);
216 glk_put_char('\n');
217 }
218 /* If we're in iosys_Filter, there's no way to validly call the filter
219 function, and the Glk printing path might throw a fatal error.
220 Just give up. */
221 }
222
223 static int obj_in_class(glui32 obj)
224 {
225 /* This checks whether obj is contained in Class, not whether
226 it is a member of Class. */
227 return (Mem4(obj + 13 + num_attr_bytes) == class_metaclass);
228 }
229
230 /* Look up a property entry. */
231 static glui32 get_prop(glui32 obj, glui32 id)
232 {
233 glui32 cla = 0;
234 glui32 prop;
235 glui32 call_argv[2];
236
237 if (id & 0xFFFF0000) {
238 cla = Mem4(classes_table+((id & 0xFFFF) * 4));
239 ARG(call_argv, 2, 0) = obj;
240 ARG(call_argv, 2, 1) = cla;
241 if (func_5_oc__cl(2, call_argv) == 0)
242 return 0;
243
244 id >>= 16;
245 obj = cla;
246 }
247
248 ARG(call_argv, 2, 0) = obj;
249 ARG(call_argv, 2, 1) = id;
250 prop = func_2_cp__tab(2, call_argv);
251 if (prop == 0)
252 return 0;
253
254 if (obj_in_class(obj) && (cla == 0)) {
255 if ((id < indiv_prop_start) || (id >= indiv_prop_start+8))
256 return 0;
257 }
258
259 if (Mem4(self) != obj) {
260 if (Mem1(prop + 9) & 1)
261 return 0;
262 }
263 return prop;
264 }
265
266 /* Look up a property entry. This is part of the newer set of accel
267 functions (8 through 13), which support increasing NUM_ATTR_BYTES.
268 It is identical to get_prop() except that it calls the new versions
269 of func_5 and func_2. */
270 static glui32 get_prop_new(glui32 obj, glui32 id)
271 {
272 glui32 cla = 0;
273 glui32 prop;
274 glui32 call_argv[2];
275
276 if (id & 0xFFFF0000) {
277 cla = Mem4(classes_table+((id & 0xFFFF) * 4));
278 ARG(call_argv, 2, 0) = obj;
279 ARG(call_argv, 2, 1) = cla;
280 if (func_11_oc__cl(2, call_argv) == 0)
281 return 0;
282
283 id >>= 16;
284 obj = cla;
285 }
286
287 ARG(call_argv, 2, 0) = obj;
288 ARG(call_argv, 2, 1) = id;
289 prop = func_8_cp__tab(2, call_argv);
290 if (prop == 0)
291 return 0;
292
293 if (obj_in_class(obj) && (cla == 0)) {
294 if ((id < indiv_prop_start) || (id >= indiv_prop_start+8))
295 return 0;
296 }
297
298 if (Mem4(self) != obj) {
299 if (Mem1(prop + 9) & 1)
300 return 0;
301 }
302 return prop;
303 }
304
305 static glui32 func_1_z__region(glui32 argc, glui32 *argv)
306 {
307 glui32 addr;
308 glui32 tb;
309
310 if (argc < 1)
311 return 0;
312
313 addr = ARG(argv, argc, 0);
314 if (addr < 36)
315 return 0;
316 if (addr >= endmem)
317 return 0;
318
319 tb = Mem1(addr);
320 if (tb >= 0xE0) {
321 return 3;
322 }
323 if (tb >= 0xC0) {
324 return 2;
325 }
326 if (tb >= 0x70 && tb <= 0x7F && addr >= ramstart) {
327 return 1;
328 }
329 return 0;
330 }
331
332 /* The old set of accel functions (2 through 7) are deprecated; they
333 behave badly if the Inform 6 NUM_ATTR_BYTES option (parameter 7) is
334 changed from its default value (7). They will not be removed, but
335 new games should use functions 8 through 13 instead. */
336
337 static glui32 func_2_cp__tab(glui32 argc, glui32 *argv)
338 {
339 glui32 obj;
340 glui32 id;
341 glui32 otab, max;
342
343 obj = ARG_IF_GIVEN(argv, argc, 0);
344 id = ARG_IF_GIVEN(argv, argc, 1);
345
346 if (func_1_z__region(1, &obj) != 1) {
347 accel_error("[** Programming error: tried to find the \".\" of (something) **]");
348 return 0;
349 }
350
351 otab = Mem4(obj + 16);
352 if (!otab)
353 return 0;
354
355 max = Mem4(otab);
356 otab += 4;
357 /* @binarysearch id 2 otab 10 max 0 0 res; */
358 return binary_search(id, 2, otab, 10, max, 0, 0);
359 }
360
361 static glui32 func_3_ra__pr(glui32 argc, glui32 *argv)
362 {
363 glui32 obj;
364 glui32 id;
365 glui32 prop;
366
367 obj = ARG_IF_GIVEN(argv, argc, 0);
368 id = ARG_IF_GIVEN(argv, argc, 1);
369
370 prop = get_prop(obj, id);
371 if (prop == 0)
372 return 0;
373
374 return Mem4(prop + 4);
375 }
376
377 static glui32 func_4_rl__pr(glui32 argc, glui32 *argv)
378 {
379 glui32 obj;
380 glui32 id;
381 glui32 prop;
382
383 obj = ARG_IF_GIVEN(argv, argc, 0);
384 id = ARG_IF_GIVEN(argv, argc, 1);
385
386 prop = get_prop(obj, id);
387 if (prop == 0)
388 return 0;
389
390 return 4 * Mem2(prop + 2);
391 }
392
393 static glui32 func_5_oc__cl(glui32 argc, glui32 *argv)
394 {
395 glui32 obj;
396 glui32 cla;
397 glui32 zr, prop, inlist, inlistlen, jx;
398
399 obj = ARG_IF_GIVEN(argv, argc, 0);
400 cla = ARG_IF_GIVEN(argv, argc, 1);
401
402 zr = func_1_z__region(1, &obj);
403 if (zr == 3)
404 return (cla == string_metaclass) ? 1 : 0;
405 if (zr == 2)
406 return (cla == routine_metaclass) ? 1 : 0;
407 if (zr != 1)
408 return 0;
409
410 if (cla == class_metaclass) {
411 if (obj_in_class(obj))
412 return 1;
413 if (obj == class_metaclass)
414 return 1;
415 if (obj == string_metaclass)
416 return 1;
417 if (obj == routine_metaclass)
418 return 1;
419 if (obj == object_metaclass)
420 return 1;
421 return 0;
422 }
423 if (cla == object_metaclass) {
424 if (obj_in_class(obj))
425 return 0;
426 if (obj == class_metaclass)
427 return 0;
428 if (obj == string_metaclass)
429 return 0;
430 if (obj == routine_metaclass)
431 return 0;
432 if (obj == object_metaclass)
433 return 0;
434 return 1;
435 }
436 if ((cla == string_metaclass) || (cla == routine_metaclass))
437 return 0;
438
439 if (!obj_in_class(cla)) {
440 accel_error("[** Programming error: tried to apply 'ofclass' with non-class **]");
441 return 0;
442 }
443
444 prop = get_prop(obj, 2);
445 if (prop == 0)
446 return 0;
447
448 inlist = Mem4(prop + 4);
449 if (inlist == 0)
450 return 0;
451
452 inlistlen = Mem2(prop + 2);
453 for (jx = 0; jx < inlistlen; jx++) {
454 if (Mem4(inlist + (4 * jx)) == cla)
455 return 1;
456 }
457 return 0;
458 }
459
460 static glui32 func_6_rv__pr(glui32 argc, glui32 *argv)
461 {
462 glui32 id;
463 glui32 addr;
464
465 id = ARG_IF_GIVEN(argv, argc, 1);
466
467 addr = func_3_ra__pr(argc, argv);
468
469 if (addr == 0) {
470 if ((id > 0) && (id < indiv_prop_start))
471 return Mem4(cpv__start + (4 * id));
472
473 accel_error("[** Programming error: tried to read (something) **]");
474 return 0;
475 }
476
477 return Mem4(addr);
478 }
479
480 static glui32 func_7_op__pr(glui32 argc, glui32 *argv)
481 {
482 glui32 obj;
483 glui32 id;
484 glui32 zr;
485
486 obj = ARG_IF_GIVEN(argv, argc, 0);
487 id = ARG_IF_GIVEN(argv, argc, 1);
488
489 zr = func_1_z__region(1, &obj);
490 if (zr == 3) {
491 /* print is INDIV_PROP_START+6 */
492 if (id == indiv_prop_start+6)
493 return 1;
494 /* print_to_array is INDIV_PROP_START+7 */
495 if (id == indiv_prop_start+7)
496 return 1;
497 return 0;
498 }
499 if (zr == 2) {
500 /* call is INDIV_PROP_START+5 */
501 return ((id == indiv_prop_start+5) ? 1 : 0);
502 }
503 if (zr != 1)
504 return 0;
505
506 if ((id >= indiv_prop_start) && (id < indiv_prop_start+8)) {
507 if (obj_in_class(obj))
508 return 1;
509 }
510
511 return ((func_3_ra__pr(argc, argv)) ? 1 : 0);
512 }
513
514 /* Here are the newer functions, which support changing NUM_ATTR_BYTES.
515 These call get_prop_new() instead of get_prop(). */
516
517 static glui32 func_8_cp__tab(glui32 argc, glui32 *argv)
518 {
519 glui32 obj;
520 glui32 id;
521 glui32 otab, max;
522
523 obj = ARG_IF_GIVEN(argv, argc, 0);
524 id = ARG_IF_GIVEN(argv, argc, 1);
525
526 if (func_1_z__region(1, &obj) != 1) {
527 accel_error("[** Programming error: tried to find the \".\" of (something) **]");
528 return 0;
529 }
530
531 otab = Mem4(obj + 4*(3+(int)(num_attr_bytes/4)));
532 if (!otab)
533 return 0;
534
535 max = Mem4(otab);
536 otab += 4;
537 /* @binarysearch id 2 otab 10 max 0 0 res; */
538 return binary_search(id, 2, otab, 10, max, 0, 0);
539 }
540
541 static glui32 func_9_ra__pr(glui32 argc, glui32 *argv)
542 {
543 glui32 obj;
544 glui32 id;
545 glui32 prop;
546
547 obj = ARG_IF_GIVEN(argv, argc, 0);
548 id = ARG_IF_GIVEN(argv, argc, 1);
549
550 prop = get_prop_new(obj, id);
551 if (prop == 0)
552 return 0;
553
554 return Mem4(prop + 4);
555 }
556
557 static glui32 func_10_rl__pr(glui32 argc, glui32 *argv)
558 {
559 glui32 obj;
560 glui32 id;
561 glui32 prop;
562
563 obj = ARG_IF_GIVEN(argv, argc, 0);
564 id = ARG_IF_GIVEN(argv, argc, 1);
565
566 prop = get_prop_new(obj, id);
567 if (prop == 0)
568 return 0;
569
570 return 4 * Mem2(prop + 2);
571 }
572
573 static glui32 func_11_oc__cl(glui32 argc, glui32 *argv)
574 {
575 glui32 obj;
576 glui32 cla;
577 glui32 zr, prop, inlist, inlistlen, jx;
578
579 obj = ARG_IF_GIVEN(argv, argc, 0);
580 cla = ARG_IF_GIVEN(argv, argc, 1);
581
582 zr = func_1_z__region(1, &obj);
583 if (zr == 3)
584 return (cla == string_metaclass) ? 1 : 0;
585 if (zr == 2)
586 return (cla == routine_metaclass) ? 1 : 0;
587 if (zr != 1)
588 return 0;
589
590 if (cla == class_metaclass) {
591 if (obj_in_class(obj))
592 return 1;
593 if (obj == class_metaclass)
594 return 1;
595 if (obj == string_metaclass)
596 return 1;
597 if (obj == routine_metaclass)
598 return 1;
599 if (obj == object_metaclass)
600 return 1;
601 return 0;
602 }
603 if (cla == object_metaclass) {
604 if (obj_in_class(obj))
605 return 0;
606 if (obj == class_metaclass)
607 return 0;
608 if (obj == string_metaclass)
609 return 0;
610 if (obj == routine_metaclass)
611 return 0;
612 if (obj == object_metaclass)
613 return 0;
614 return 1;
615 }
616 if ((cla == string_metaclass) || (cla == routine_metaclass))
617 return 0;
618
619 if (!obj_in_class(cla)) {
620 accel_error("[** Programming error: tried to apply 'ofclass' with non-class **]");
621 return 0;
622 }
623
624 prop = get_prop_new(obj, 2);
625 if (prop == 0)
626 return 0;
627
628 inlist = Mem4(prop + 4);
629 if (inlist == 0)
630 return 0;
631
632 inlistlen = Mem2(prop + 2);
633 for (jx = 0; jx < inlistlen; jx++) {
634 if (Mem4(inlist + (4 * jx)) == cla)
635 return 1;
636 }
637 return 0;
638 }
639
640 static glui32 func_12_rv__pr(glui32 argc, glui32 *argv)
641 {
642 glui32 id;
643 glui32 addr;
644
645 id = ARG_IF_GIVEN(argv, argc, 1);
646
647 addr = func_9_ra__pr(argc, argv);
648
649 if (addr == 0) {
650 if ((id > 0) && (id < indiv_prop_start))
651 return Mem4(cpv__start + (4 * id));
652
653 accel_error("[** Programming error: tried to read (something) **]");
654 return 0;
655 }
656
657 return Mem4(addr);
658 }
659
660 static glui32 func_13_op__pr(glui32 argc, glui32 *argv)
661 {
662 glui32 obj;
663 glui32 id;
664 glui32 zr;
665
666 obj = ARG_IF_GIVEN(argv, argc, 0);
667 id = ARG_IF_GIVEN(argv, argc, 1);
668
669 zr = func_1_z__region(1, &obj);
670 if (zr == 3) {
671 /* print is INDIV_PROP_START+6 */
672 if (id == indiv_prop_start+6)
673 return 1;
674 /* print_to_array is INDIV_PROP_START+7 */
675 if (id == indiv_prop_start+7)
676 return 1;
677 return 0;
678 }
679 if (zr == 2) {
680 /* call is INDIV_PROP_START+5 */
681 return ((id == indiv_prop_start+5) ? 1 : 0);
682 }
683 if (zr != 1)
684 return 0;
685
686 if ((id >= indiv_prop_start) && (id < indiv_prop_start+8)) {
687 if (obj_in_class(obj))
688 return 1;
689 }
690
691 return ((func_9_ra__pr(argc, argv)) ? 1 : 0);
692 }