-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLow-Level Operations.i7x
1909 lines (1478 loc) · 67.6 KB
/
Low-Level Operations.i7x
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Version 2 of Low-Level Operations (for Glulx only) by Brady Garvin begins here.
"Phrases for low-level Glulx operations."
Use authorial modesty.
Book "Copyright and License"
[Copyright 2013 Brady J. Garvin]
[This extension is released under the Creative Commons Attribution 3.0 Unported License (CC BY 3.0) so that it can qualify as a public Inform extension. See the LICENSE file included in the release for further details.]
Book "Extension Information"
Chapter "Use Options" - unindexed
[For hunting down bugs involving CocoaGlk with a modded version of the Inform IDE; full Glk logging gets noisy fast if we swap streams in string-to-array conversions, so we forgo stream safety in favor of legibility.]
Use quiet string printing under Cocoa Glk translates as (- Constant COCOA_QUIET; -).
Book "Transfer Registers" - unindexed
[We have some phrases that need to use their arguments as assembly operands. For this to work, however, we need a place to store the argument between evaluation and the
assembly or between the assembly and assignment to an l-value. We call such a place a transfer register; one is defined below.]
Include (-
Global llo_transfer;
-) after "Definitions.i6t".
The low-level operations transfer register is a number that varies.
The low-level operations transfer register variable translates into I6 as "llo_transfer".
Book "Numbers"
Chapter "Bitwise Operations"
Include (-
[ llo_xor value otherValue;
@bitxor value otherValue sp;
@return sp;
];
-).
To decide what number is the bitwise not of (I - a number): (- (~{I}) -).
To decide what number is the bitwise and of (I - a number) and (J - a number): (- ({I} & {J}) -).
To decide what number is the bitwise or of (I - a number) and (J - a number): (- ({I} | {J}) -).
To decide what number is the bitwise xor of (I - a number) and (J - a number): (- llo_xor({I}, {J}) -).
Chapter "Shift Operations"
Include (-
[ llo_leftShift value distance;
@shiftl value distance sp;
@return sp;
];
[ llo_logicalRightShift value distance;
@ushiftr value distance sp;
@return sp;
];
[ llo_arithmeticRightShift value distance;
@sshiftr value distance sp;
@return sp;
];
-).
To decide what number is (I - a number) shifted (D - a number) bit/bits left: (- llo_leftShift({I}, {D}) -).
To decide what number is (I - a number) logically shifted (D - a number) bit/bits right: (- llo_logicalRightShift({I}, {D}) -).
To decide what number is (I - a number) arithmetically shifted (D - a number) bit/bits right: (- llo_arithmeticRightShift({I}, {D}) -).
Chapter "Sign Extension"
Include (-
[ llo_extendByte value;
@sexb value sp;
@return sp;
];
[ llo_extendShort value;
@sexs value sp;
@return sp;
];
-).
To decide what number is (I - a number) sign extended from a byte: (- llo_extendByte({I}) -).
To decide what number is (I - a number) sign extended from a short: (- llo_extendShort({I}) -).
Chapter "Unsigned Numbers"
Section "Unsigned Comparisons"
Include (-
[ llo_unsignedLessThan value otherValue;
@"3:42" value otherValue 1; !jltu
@return 0;
];
[ llo_unsignedLessThanOrEqual value otherValue;
@"3:45" value otherValue 1; !jleu
@return 0;
];
[ llo_unsignedGreaterThanOrEqual value otherValue;
@"3:43" value otherValue 1; !jgeu
@return 0;
];
[ llo_unsignedGreaterThan value otherValue;
@"3:44" value otherValue 1; !jgtu
@return 0;
];
-).
To decide whether (I - a number) is/are unsigned less than (J - a number): (- llo_unsignedLessThan({I}, {J}) -).
To decide whether (I - a number) is/are unsigned fewer than (J - a number): (- llo_unsignedLessThan({I}, {J}) -).
To decide whether (I - a number) is/are unsigned at most (J - a number): (- llo_unsignedLessThanOrEqual({I}, {J}) -).
To decide whether (I - a number) is/are unsigned at least (J - a number): (- llo_unsignedGreaterThanOrEqual({I}, {J}) -).
To decide whether (I - a number) is/are unsigned greater than (J - a number): (- llo_unsignedGreaterThan({I}, {J}) -).
To decide whether (I - a number) is/are unsigned more than (J - a number): (- llo_unsignedGreaterThan({I}, {J}) -).
Section "Saying Unsigned Numbers"
To say (I - a number) as unsigned (this is saying an unsigned number):
let the leading digit be zero;
let the trailing digits be I;
[This loop runs at most four iterations.]
while the trailing digits are unsigned at least 1000000000:
increment the leading digit;
decrease the trailing digits by 1000000000;
say "[the leading digit][the trailing digits]".
Chapter "Other Bases"
Section "Digits and Masks" - unindexed
Include (-
Array LLO_DIGITS --> "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "A" "B" "C" "D" "E" "F";
-).
[@@]
To decide what text is digit (D - a number): (- (LLO_DIGITS-->{D}) -).
To decide what number is the upper bit mask: (- $80000000 -).
To decide what number is the upper nybble mask: (- $F0000000 -).
Section "Binary"
To say (N - a number) in unprefixed binary (this is saying a number in unprefixed binary):
if N is zero:
say "[the digit zero]";
otherwise:
let the bit shift be zero;
let the unprinted bits be N;
while the bitwise and of the unprinted bits and the upper bit mask is zero:
increment the bit shift;
now the unprinted bits are the unprinted bits shifted one bit left;
while the bit shift is less than 32:
let the masked value be the bitwise and of the unprinted bits and the upper bit mask;
let the bit be the masked value logically shifted 31 bits right;
say "[the digit bit]";
increment the bit shift;
now the unprinted bits are the unprinted bits shifted one bit left.
To say (N - a number) in binary (this is saying a number in prefixed binary):
say "0b[N in unprefixed binary]".
Section "Hexadecimal"
To say (N - a number) in unprefixed hexadecimal (this is saying a number in unprefixed hexadecimal):
if N is zero:
say "[the digit zero]";
otherwise:
let the nybble shift be zero;
let the unprinted nybbles be N;
while the bitwise and of the unprinted nybbles and the upper nybble mask is zero:
increment the nybble shift;
now the unprinted nybbles are the unprinted nybbles shifted four bits left;
while the nybble shift is less than 8:
let the masked value be the bitwise and of the unprinted nybbles and the upper nybble mask;
let the nybble be the masked value logically shifted 28 bits right;
say "[the digit nybble]";
increment the nybble shift;
now the unprinted nybbles are the unprinted nybbles shifted four bits left.
To say (N - a number) in hexadecimal (this is saying a number in prefixed hexadecimal):
say "0x[N in unprefixed hexadecimal]".
Book "Memory"
Chapter "Measuring Memory"
Include (-
[ llo_getMemorySize;
@getmemsize sp;
@return sp;
];
[ llo_validByteAddress value;
@getmemsize sp;
@"3:42" value sp 1; !jltu
@return 0;
];
[ llo_validShortAddress value;
@getmemsize sp;
@sub sp 1 sp;
@"3:42" value sp 1; !jltu
@return 0;
];
[ llo_validIntAddress value;
@getmemsize sp;
@sub sp 3 sp;
@"3:42" value sp 1; !jltu
@return 0;
];
-).
To decide what number is the size of memory: (- llo_getMemorySize() -).
To decide what number is the size of read-only memory: (- llo_getInt(8) -).
Definition: a number is a valid byte address rather than an invalid byte address if I6 routine "llo_validByteAddress" says so (it is less than the size of memory, using an unsigned comparison).
Definition: a number is a valid short address rather than an invalid short address if I6 routine "llo_validShortAddress" says so (it is less than the size of memory minus one, using an unsigned comparison).
Definition: a number is a valid integer address rather than an invalid integer address if I6 routine "llo_validIntAddress" says so (it is less than the size of memory minus three, using an unsigned comparison).
Chapter "Reading Memory"
Include (-
[ llo_getBit address bitOffset;
@aloadbit address bitOffset sp;
@return sp;
];
! If the high parameter is nonzero, get the high nybble (bits 4--7); otherwise get the low nybble (bits 0--3).
[ llo_getNybble address high;
if (high) {
return llo_logicalRightShift(llo_getByte(address), 4) & $F;
}
return llo_getByte(address) & $F;
];
[ llo_getByte address;
@aloadb address 0 sp;
@return sp;
];
[ llo_getShort address;
@aloads address 0 sp;
@return sp;
];
[ llo_getInt address;
@aload address 0 sp;
@return sp;
];
[ llo_getField base index;
@aload base index sp;
@return sp;
];
-).
To decide whether the bit at address (A - a number) and secondary address (B - a number) is set: (- llo_getBit({A}, {B}) -).
To decide what number is the nybble at address (A - a number) and secondary address (B - a number): (- llo_getNybble({A}, {B}) -).
To decide what number is the byte at address (A - a number): (- llo_getByte({A}) -).
To decide what number is the short at address (A - a number): (- llo_getShort({A}) -).
To decide what number is the integer at address (A - a number): (- llo_getInt({A}) -).
Chapter "Writing Memory"
Include (-
[ llo_setBit address bitOffset value;
@astorebit address bitOffset value;
];
! If the high parameter is nonzero, set the high nybble (bits 4--7); otherwise set the low nybble (bits 0--3).
[ llo_setNybble address high value;
if (high) {
return llo_setByte(address, (llo_getByte(address) & $F) | llo_leftShift(value & $F, 4));
}
return llo_setByte(address, (llo_getByte(address) & -$10) | (value & $F));
];
[ llo_setByte address value;
@astoreb address 0 value;
];
[ llo_setShort address value;
@astores address 0 value;
];
[ llo_setInt address value;
@astore address 0 value;
];
[ llo_setField base index value;
@astore base index value;
];
[llo_zero size address;
@mzero size address;
];
-).
To write the bit (I - a truth state) to address (A - a number) and secondary address (B - a number): (- llo_setBit({A}, {B}); -).
To write the nybble (I - a number) to address (A - a number) and secondary address (B - a number): (- llo_setNybble({A}, {B}, {I}); -).
To write the byte (I - a number) to address (A - a number): (- llo_setByte({A}, {I}); -).
To write the short (I - a number) to address (A - a number): (- llo_setShort({A}, {I}); -).
To write the integer (I - a number) to address (A - a number): (- llo_setInt({A}, {I}); -).
To zero (N - a number) bytes at address (A - a number): (- llo_zero({N}, {A}); -).
Chapter "Copying Memory"
[There are two possible implementations. We late bind according to the Glulx gestalt.]
Include (-
Global llo_copy = llo_copyResolve;
-) after "Definitions.i6t".
Include (-
[ llo_copyInVM size source destination;
@mcopy size source destination;
];
[ llo_copyByLoop size source destination i;
if (destination < source) {
for (i = 0: i < size: ++i) {
llo_setByte(destination + i, llo_getByte(source + i));
}
} else {
for (i = size: (i-- ) > 0:) {
llo_setByte(destination + i, llo_getByte(source + i));
}
}
];
[ llo_copyResolve size source destination;
if (llo_checkGestalt(6, 0)) {
llo_copy = llo_copyInVM;
} else {
llo_copy = llo_copyByLoop;
}
return llo_copy(size, source, destination);
];
-).
To copy (N - a number) byte/bytes from address (A - a number) to address (B - a number): (- llo_copy({N}, {A}, {B}); -).
Chapter "Managing Dynamic Memory"
[There isn't much special about the number four; we just want something that isn't null and also can't be a valid malloc.]
Include (-
Constant llo_zeroLengthAllocationAddress = 4;
-) after "Definitions.i6t".
To decide what number is the address for a zero-length allocation: (- llo_zeroLengthAllocationAddress -).
Include (-
Constant LLO_MIN_PERMANENT_MALLOC_GROWTH = $400000;
Global llo_permanentMallocPool = 0;
Global llo_permanentMallocPoolEnd = 0;
[ llo_permanentMalloc size
result growth;
result = llo_permanentMallocPool;
llo_permanentMallocPool = llo_permanentMallocPool + size;
if (llo_permanentMallocPool > llo_permanentMallocPoolEnd) {
if (size < LLO_MIN_PERMANENT_MALLOC_GROWTH) {
growth = LLO_MIN_PERMANENT_MALLOC_GROWTH;
} else {
growth = size;
}
@malloc growth llo_permanentMallocPool;
llo_permanentMallocPoolEnd = llo_permanentMallocPool + growth;
@copy llo_permanentMallocPool result;
llo_permanentMallocPool = llo_permanentMallocPool + size;
}
return result;
];
[ llo_malloc size;
@malloc size sp;
@return sp;
];
[ llo_free address;
@mfree address;
];
-).
To decide what number is a permanent memory allocation of (N - a number) byte/bytes: (- llo_permanentMalloc({N}) -).
To decide what number is a possibly zero-length permanent memory allocation of (N - a number) byte/bytes (this is allocating possibly zero-length permanent memory):
if N is zero:
decide on the address for a zero-length allocation;
decide on a permanent memory allocation of N bytes.
To decide what number is a memory allocation of (N - a number) byte/bytes: (- llo_malloc({N}) -).
To decide what number is a possibly zero-length memory allocation of (N - a number) byte/bytes (this is allocating possibly zero-length memory):
if N is zero:
decide on the address for a zero-length allocation;
decide on a memory allocation of N bytes.
To free the memory allocation at address (A - a number): (- llo_free({A}); -).
To free the possibly zero-length memory allocation at address (A - a number) (this is freeing possibly zero-length memory):
if A is not the address for a zero-length allocation:
free the memory allocation at address A.
Book "Text"
Chapter "Printing Characters even without Unicode Support"
[Note that glk_put_char_uni has been replaced by a function using @streamchar and @streamunichar, so that the phrase works with other I/O systems.]
Include (-
[ llo_printUnicode character
unicodeSupported;
@gestalt 5 0 unicodeSupported;
if (unicodeSupported) {
@streamunichar character;
} else if (character <= 128) {
@streamchar character;
} else {
print "?";
}
];
-).
[@]
To say (ch - Unicode character) -- running on (documented at phs_unicode): (- llo_printUnicode({ch}); -).
Chapter "Filter I/O"
To say invoking (F - a phrase number -> nothing) once for each character code -- beginning say_invoking: (- @getiosys sp sp; @aload {F} 1 sp; @setiosys 1 sp; -).
To say end invoking -- ending say_invoking: (- @stkswap; @setiosys sp sp; -).
Chapter "Latin-1 Arrays"
Include (-
Global llo_cocoaTargetAddress;
Global llo_cocoaSpaceRemaining;
Global llo_cocoaGlkDetected;
Global llo_stringToArrayChoice;
-) after "Definitions.i6t".
Include (-
[ llo_stringToArray text array length metrics
oldStream stream iosystem iorock;
oldStream = glk_stream_get_current();
stream = glk_stream_open_memory(array, length, filemode_Write, 0);
if (~~stream) {
rfalse;
}
@push say__p;
@push say__pc;
@push say__n;
@push debug_rules;
debug_rules = 0;
glk_stream_set_current(stream);
@getiosys iosystem iorock;
@setiosys 2 0;
! [@]
if (llo_getByte(text) == 224 or 225 or 226) {
print (string)text;
} else {
text();
}
@setiosys iosystem iorock;
glk_stream_set_current(oldStream);
glk_stream_close(stream, metrics);
@pull debug_rules;
@pull say__n;
@pull say__pc;
@pull say__p;
];
! Using the above implementation, CocoaGlk spends too much time flushing streams, and as a result some ordinarily split-second operations in Debug File Parsing take half a minute or so. If we detect CocoaGlk, we want the option of the filter I/O system available.
[ llo_cocoaPrint character;
if (llo_cocoaSpaceRemaining > 0) {
@astoreb llo_cocoaTargetAddress 0 character;
llo_cocoaTargetAddress++;
}
llo_cocoaSpaceRemaining--;
];
[ llo_stringToArrayCocoa text array length metrics
oldStream original iosystem iorock;
#ifndef COCOA_QUIET;
oldStream = glk_stream_get_current();
glk_stream_set_current(0); ! to error out on attempts to set a style
#endif;
@push say__p;
@push say__pc;
@push say__n;
@push debug_rules;
debug_rules = 0;
@push llo_cocoaTargetAddress;
@push llo_cocoaSpaceRemaining;
llo_cocoaTargetAddress = array;
llo_cocoaSpaceRemaining = length;
@getiosys iosystem iorock;
@setiosys 1 llo_cocoaPrint;
! [@]
if (llo_getByte(text) == 224 or 225 or 226) {
print (string)text;
} else {
text();
}
@setiosys iosystem iorock;
length = length - llo_cocoaSpaceRemaining;
@astore metrics 0 0;
@astore metrics 1 length;
@pull llo_cocoaSpaceRemaining;
@pull llo_cocoaTargetAddress;
@pull debug_rules;
@pull say__n;
@pull say__pc;
@pull say__p;
#ifndef COCOA_QUIET;
glk_stream_set_current(oldStream);
#endif;
];
Array llo_cocoaKeyWindowCheck --> 1;
[ llo_stringToArrayChoosingRule
root nonroot recreateRoot rootType rootRock firstWindow secondWindow iosystem iorock;
@getiosys iosystem iorock;
@setiosys 2 0;
! Detect CocoaGlk via Inform bug 819, without falling afoul of Inform bug 961.
llo_cocoaGlkDetected = false;
root = glk_window_get_root();
recreateRoot = false;
if (root) {
for (nonroot = 0: nonroot = glk_window_iterate(nonroot, 0):) {
if (nonroot ~= root) {
break;
}
}
if (nonroot) {
firstWindow = glk_window_open(nonroot, winmethod_Below | winmethod_Proportional, 50, wintype_TextBuffer, 0);
if (~~firstWindow) {
jump L0;
}
secondWindow = glk_window_open(firstWindow, winmethod_Below | winmethod_Proportional, 50, wintype_TextBuffer, 0);
if (~~secondWindow) {
jump L1;
}
} else {
! We're in a catch-22: we don't know if we're running under CocoaGlk, but if we check, we might trip a CocoaGlk bug (961) that will make the whole story invisible. At the moment we duck out of the dilemma by destroying the lone window and then recreating it. That leads to some side-effects: lost text, forgotten style hints, etc., not to mention possible invalidation of references, but they all seem less severe than the risk of a permanently blank gray screen.
recreateRoot = true;
rootType = glk_window_get_type(root);
rootRock = glk_window_get_rock(root);
glk_window_close(root);
firstWindow = glk_window_open(0, 0, 0, wintype_TextBuffer, 0);
if (~~firstWindow) {
jump L0;
}
secondWindow = glk_window_open(firstWindow, winmethod_Below | winmethod_Proportional, 50, wintype_TextBuffer, 0);
if (~~secondWindow) {
jump L1;
}
}
} else {
firstWindow = glk_window_open(0, 0, 0, wintype_TextBuffer, 0);
if (~~firstWindow) {
jump L0;
}
secondWindow = glk_window_open(firstWindow, winmethod_Below | winmethod_Proportional, 50, wintype_TextBuffer, 0);
if (~~secondWindow) {
jump L1;
}
}
glk_window_get_arrangement(glk_window_get_parent(firstWindow), 0, 0, llo_cocoaKeyWindowCheck);
llo_cocoaGlkDetected = (llo_cocoaKeyWindowCheck-->0) == firstWindow;
glk_window_close(secondWindow, 0);
.L1;
glk_window_close(firstWindow, 0);
.L0;
if (recreateRoot) {
glk_window_open(0, 0, 0, rootType, rootRock);
}
@setiosys iosystem iorock;
if (llo_cocoaGlkDetected) {
llo_stringToArrayChoice = llo_stringToArrayCocoa;
} else {
llo_stringToArrayChoice = llo_stringToArray;
}
rfalse;
];
-).
To print the text (T - some text) to the Latin-1 array at address (A - a number) with length (L - a number) and metrics structure at address (M - a number): (- llo_stringToArrayChoice({T}, {A}, {L}, {M}); -).
The string-to-array implementation choosing rule translates into I6 as "llo_stringToArrayChoosingRule".
Section "CocoaGlk Detection Flag" - unindexed
[Other low-level extensions might want to reuse our work sniffing the Glk implementation.]
To decide whether CocoaGlk is detected: (- llo_cocoaGlkDetected -).
Section "CocoaGlk Sniffing" (for use without Glulx Runtime Instrumentation Framework by Brady Garvin)
The string-to-array implementation choosing rule is listed before the initialise memory rule in the startup rulebook.
Chapter "Length of Text Printed by a Phrase"
Include (-
Array llo_streamToStringMetrics --> 0 0;
Global llo_oldStream;
Global llo_stream;
-) after "Definitions.i6t".
[This phrase is not used often enough for it's own CocoaGlk performance workaround.]
To record the number of characters printed when we (P - a phrase): (-
@push say__p;
@push say__pc;
@push say__n;
@push debug_rules;
debug_rules = 0;
llo_oldStream = glk_stream_get_current();
! To workaround a bug in older interpreters' Glk safety checks, use 1, not 0, as the array argument.
llo_stream = glk_stream_open_memory(1, 0, filemode_Write, llo_streamToStringMetrics);
if (llo_stream) {
glk_stream_set_current(llo_stream);
@getiosys sp sp;
@setiosys 2 0;
if (true) {P}
@stkswap;
@setiosys sp sp;
glk_stream_set_current(llo_oldStream);
glk_stream_close(llo_stream, llo_streamToStringMetrics);
}
@pull debug_rules;
@pull say__n;
@pull say__pc;
@pull say__p;
-).
To decide what number is the number of characters recorded: (- llo_getField(llo_streamToStringMetrics, 1) -).
Chapter "Text Length"
Include (-
[ llo_stringLength text
length;
! To workaround a bug in older interpreters' Glk safety checks, use 1, not 0, as the array argument.
if (llo_stringToArrayChoice(text, 1, 0, llo_streamToStringMetrics)) {
return llo_getField(llo_streamToStringMetrics, 1);
}
return 0;
];
-).
To decide what number is the length of (T - some text): (- llo_stringLength({T}) -).
Chapter "Text Hashing"
Section "Normal Hashing of Text"
Include (-
Array llo_stringPrefix --> 8;
[ llo_stringHash32 text;
llo_zero(32, llo_stringPrefix);
if (llo_stringToArrayChoice(text, llo_stringPrefix, 32, llo_streamToStringMetrics)) {
return
llo_getInt(llo_stringPrefix) +
llo_getField(llo_stringPrefix, 1) +
llo_getField(llo_stringPrefix, 2) +
llo_getField(llo_stringPrefix, 3) +
llo_getField(llo_stringPrefix, 4) +
llo_getField(llo_stringPrefix, 5) +
llo_getField(llo_stringPrefix, 6) +
llo_getField(llo_stringPrefix, 7);
}
return 0;
];
-).
To decide what number is the normal hash of (T - some text): (- llo_stringHash32({T}) -).
Book "Type Unsafety"
Chapter "Type Conversion"
To decide what K is (V - a value) converted to --/a/an/some (D - a description of values of kind K): (- {V} -).
Chapter "Type Identification"
Definition: a number is a function type indicator if it is 192 or it is 193.
[@]
Definition: a number is a string type indicator if it is 224 or it is 225 or it is 226.
Definition: a number is a object type indicator if it is at least 112 and it is at most 127.
To decide whether address (A - a number) could contain a function (this is deciding whether an address could contain a function):
if A is an invalid byte address:
decide no;
decide on whether or not the byte at address A is a function type indicator.
To decide whether address (A - a number) could not contain a function (this is deciding whether an address could not contain a function):
if A is an invalid byte address:
decide yes;
decide on whether or not the byte at address A is not a function type indicator.
To decide whether address (A - a number) could contain a string (this is deciding whether an address could contain a string):
if A is an invalid byte address:
decide no;
decide on whether or not the byte at address A is a string type indicator.
To decide whether address (A - a number) could not contain a string (this is deciding whether an address could not contain a string):
if A is an invalid byte address:
decide yes;
decide on whether or not the byte at address A is not a string type indicator.
To decide whether address (A - a number) could contain an object (this is deciding whether an address could contain an object):
if A is an invalid byte address:
decide no;
decide on whether or not the byte at address A is an object type indicator.
To decide whether address (A - a number) could not contain an object (this is deciding whether an address could not contain an object):
if A is an invalid byte address:
decide yes;
decide on whether or not the byte at address A is not an object type indicator.
Chapter "Workarounds for Inform Bug 473"
[@]
To decide whether (P - a phrase nothing -> nothing) applied (documented at ph_applied0): (- {-function-application} -).
[@]
To decide whether (P - a phrase value of kind K -> nothing) applied to (X - a K) (documented at ph_applied1): (- {-function-application} -).
[@]
To decide whether (P - a phrase (value of kind K, value of kind L) -> nothing) applied to (X - a K) and (Y - an L) (documented at ph_applied2): (- {-function-application} -).
[@]
To decide whether (P - a phrase (value of kind K, value of kind L, value of kind M) -> nothing) applied to (X - a K) and (Y - an L) and (Z - an M) (documented at ph_applied3): (- {-function-application} -).
Book "Loops"
Chapter "Loops without Built-in End Conditions"
To repeat until a break begin -- end: (- for (::) -).
Chapter "Loops over Half-Open Intervals"
To repeat with (I - a nonexisting K variable) running over the half-open interval from (J - an arithmetic value of kind K) to (K - a K) begin -- end: (- for ({I} = {J}: {I} < {K}: {I}++) -).
Chapter "Implicit Loops"
Section "Linear Searches"
Include (-
[ llo_byteIndex needle haystack haystackLength
result;
if (haystackLength <= 0) {
return -1;
}
@linearsearch needle 1 haystack 1 haystackLength 0 4 result;
return result;
];
[ llo_shortIndex needle haystack haystackLength
result;
! haystackLength is measured in shorts, not bytes.
if (haystackLength <= 0) {
return -1;
}
@linearsearch needle 2 haystack 2 haystackLength 0 4 result;
return result;
];
[ llo_intIndex needle haystack haystackLength
result;
! haystackLength is measured in ints, not bytes.
if (haystackLength <= 0) {
return -1;
}
@linearsearch needle 4 haystack 4 haystackLength 0 4 result;
return result;
];
[ llo_byteSubsequenceIndex needle needleLength haystack haystackLength
possibilities result;
if (needleLength > haystackLength) {
return -1;
}
possibilities = 1 + haystackLength - needleLength;
@linearsearch needle needleLength haystack 1 possibilities 0 5 result;
return result;
];
[ llo_shortSubsequenceIndex needle needleLength haystack haystackLength
possibilities result;
if (needleLength > haystackLength) {
return 0;
}
possibilities = 1 + haystackLength - needleLength;
needleLength = needleLength * 2;
@linearsearch needle needleLength haystack 4 possibilities 0 5 result;
return result;
];
[ llo_intSubsequenceIndex needle needleLength haystack haystackLength
possibilities result;
if (needleLength > haystackLength) {
return 0;
}
possibilities = 1 + haystackLength - needleLength;
needleLength = needleLength * 4;
@linearsearch needle needleLength haystack 4 possibilities 0 5 result;
return result;
];
[ llo_byteAddress needle haystack haystackLength
result;
if (haystackLength <= 0) {
return 0;
}
@linearsearch needle 1 haystack 1 haystackLength 0 0 result;
return result;
];
[ llo_shortAddress needle haystack haystackLength
result;
! haystackLength is measured in shorts, not bytes.
if (haystackLength <= 0) {
return 0;
}
@linearsearch needle 2 haystack 2 haystackLength 0 0 result;
return result;
];
[ llo_intAddress needle haystack haystackLength
result;
! haystackLength is measured in ints, not bytes.
if (haystackLength <= 0) {
return 0;
}
@linearsearch needle 4 haystack 4 haystackLength 0 0 result;
return result;
];
[ llo_byteSubsequenceAddress needle needleLength haystack haystackLength
possibilities result;
if (needleLength > haystackLength) {
return 0;
}
possibilities = 1 + haystackLength - needleLength;
@linearsearch needle needleLength haystack 1 possibilities 0 1 result;
return result;
];
[ llo_shortSubsequenceAddress needle needleLength haystack haystackLength
possibilities result;
if (needleLength > haystackLength) {
return 0;
}
possibilities = 1 + haystackLength - needleLength;
needleLength = needleLength * 2;
@linearsearch needle needleLength haystack 4 possibilities 0 1 result;
return result;
];
[ llo_intSubsequenceAddress needle needleLength haystack haystackLength
possibilities result;
if (needleLength > haystackLength) {
return 0;
}
possibilities = 1 + haystackLength - needleLength;
needleLength = needleLength * 4;
@linearsearch needle needleLength haystack 4 possibilities 0 1 result;
return result;
];
-).
To decide what number is the index of the byte (B - a number) in the (N - a number) bytes at address (A - a number): (- llo_byteIndex({B}, {A}, {N}) -).
To decide what number is the index of the short (S - a number) in the (N - a number) shorts at address (A - a number): (- llo_shortIndex({S}, {A}, {N}) -).
To decide what number is the index of the integer (I - a number) in the (N - a number) integers at address (A - a number): (- llo_intIndex({I}, {A}, {N}) -).
To decide what number is the index of the (N - a number) bytes at address (A - a number) in the (M - a number) bytes at address (B - a number): (- llo_byteSubsequenceIndex({A}, {N}, {B}, {M}) -).
To decide what number is the index of the (N - a number) shorts at address (A - a number) in the (M - a number) shorts at address (B - a number): (- llo_shortSubsequenceIndex({A}, {N}, {B}, {M}) -).
To decide what number is the index of the (N - a number) integers at address (A - a number) in the (M - a number) integers at address (B - a number): (- llo_intSubsequenceIndex({A}, {N}, {B}, {M}) -).
To decide what number is the address of the byte (B - a number) in the (N - a number) bytes at address (A - a number): (- llo_byteAddress({B}, {A}, {N}) -).
To decide what number is the address of the short (S - a number) in the (N - a number) shorts at address (A - a number): (- llo_shortAddress({S}, {A}, {N}) -).
To decide what number is the address of the integer (I - a number) in the (N - a number) integers at address (A - a number): (- llo_intAddress({I}, {A}, {N}) -).
To decide what number is the address of the (N - a number) bytes at address (A - a number) in the (M - a number) bytes at address (B - a number): (- llo_byteSubsequenceAddress({A}, {N}, {B}, {M}) -).
To decide what number is the address of the (N - a number) shorts at address (A - a number) in the (M - a number) shorts at address (B - a number): (- llo_shortSubsequenceAddress({A}, {N}, {B}, {M}) -).
To decide what number is the address of the (N - a number) integers at address (A - a number) in the (M - a number) integers at address (B - a number): (- llo_intSubsequenceAddress({A}, {N}, {B}, {M}) -).
Chapter "Ingredients for Complicated Loops"
Include (-
Global llo_oneTime = false;
Global llo_broken = true;
Global llo_advance = true;
-) after "Definitions.i6t".
Book "Function Pointers"
Chapter "Obtaining Function Addresses"
To decide what number is the function address of (N - a number): (- {N} -). [This is so taking the function address is idempotent.]
To decide what number is the function address of (R - a rule): (- {R} -). [This is so rules don't fall into the broader case of sayable values.]
To decide what number is the function address of (P - sayable value): (- llo_getField({P}, 1) -). [This is so we catch all kinds of phrases with just one phrase.]
Chapter "Calling Functions by Address"
To call the function at address (A - a number): (- (({A})()); -).
To call the function at address (A - a number) passing (V - a value): (- (({A})({V})); -).
Chapter "Rulebook Traversal"
[@]
Include (-
[ llo_traverseRulebook rulebook
i j k;
! We would like to think of the rulebook argument in terms of an array of rules, rather than as an index for rulebooks_array.
rulebook = llo_getField(rulebooks_array, rulebook);
! Inform has two formats for rulebooks, one of which is cued by an initial -2.
j = llo_getInt(rulebook);
if (j == -2) {
! Rules are divided into blocks with headers that say when they apply and how large they are.
! We ignore the conditions, but we need to be sure that we don't dereference them as rules.
for (i = 3: j ~= NULL: i = i + 2) { ! An outer loop where i points to the beginning of a block's rules; the header size is two entries.
k = llo_getField(rulebook, i - 1); ! The block length is usually in the previous entry; let k be it
if (k < 1 || k > 31) { ! If k is not in fact a block length,
k = 1; ! the block length is one
i--; ! and elided
}
for (j = llo_getField(rulebook, i), ! i has moved each time we enter this inner loop, so we need to recompute j.
k = i + k: ! Now let k be the index where the next block starts.
i < k: ! Run the inner loop until the end of the block.
++i, j = llo_getField(rulebook, i)) { ! Increment in the same way as in the simple case.
j();
}
}
} else {
! Everything in the array is a rule address; life is simple.
for (i = 0: j ~= NULL: ++i, j = llo_getField(rulebook, i)) {
j();
}
}
];
[ llo_shortCircuitTraverseRulebook rulebook
i j k;
! We would like to think of the rulebook argument in terms of an array of rules, rather than as an index for rulebooks_array.
rulebook = llo_getField(rulebooks_array, rulebook);
! Inform has two formats for rulebooks, one of which is cued by an initial -2.
j = llo_getInt(rulebook);
if (j == -2) {
! Rules are divided into blocks with headers that say when they apply and how large they are.
! We ignore the conditions, but we need to be sure that we don't dereference them as rules.
for (i = 3: j ~= NULL: i = i + 2) { ! An outer loop where i points to the beginning of a block's rules; the header size is two entries.
k = llo_getField(rulebook, i - 1); ! The block length is usually in the previous entry; let k be it
if (k < 1 || k > 31) { ! If k is not in fact a block length,
k = 1; ! the block length is one
i--; ! and elided
}
for (j = llo_getField(rulebook, i), ! i has moved each time we enter this inner loop, so we need to recompute j.
k = i + k: ! Now let k be the index where the next block starts.
i < k: ! Run the inner loop until the end of the block.
++i, j = llo_getField(rulebook, i)) { ! Increment in the same way as in the simple case.
if (j()) {
rtrue;
}
}
}
} else {
! Everything in the array is a rule address; life is simple.
for (i = 0: j ~= NULL: ++i, j = llo_getField(rulebook, i)) {
if (j()) {
rtrue;
}
}
}
];
-).
To traverse (R - a rulebook): (- llo_traverseRulebook({R}); -).
To traverse (R - a rulebook) with short-circuiting: (- llo_shortCircuitTraverseRulebook({R}); -).
To rule short-circuits rulebook traversal: (- rtrue; -).
Book "Checking Gestalts"
Include (-
[ llo_checkGestalt primary secondary;
@gestalt primary secondary sp;
@return sp;
];
[ llo_getStaticMemorySize result;
result = llo_checkGestalt(8, 0);
if (result) {
return result;
}
return llo_getMemorySize();
];
-).
To decide what number is the encoded Glulx version: (- llo_checkGestalt(0, 0) -).
To decide what number is the encoded interpreter version: (- llo_checkGestalt(1, 0) -).
To decide what number is the size of statically allocated memory: (- llo_getStaticMemorySize() -).
To decide whether memory map resizes are supported: (- llo_checkGestalt(2, 0) -).
To decide whether undo is supported: (- llo_checkGestalt(3, 0) -).
To decide whether Glk is supported: (- llo_checkGestalt(4, 2) -).
To decide whether Unicode is supported: (- llo_checkGestalt(5, 0) -).
To decide whether block memory copies are supported: (- llo_checkGestalt(6, 0) -).
To decide whether memory allocation is supported: (- llo_checkGestalt(7, 0) -).
To decide whether there are outstanding memory allocations: (- llo_checkGestalt(8, 0) -).
To decide whether function acceleration is supported: (- llo_checkGestalt(9, 0) -).
Book "Pushing and Popping"
To push (X - a value of kind K): (- llo_transfer = {X}; @push llo_transfer; -).
To pop/pull (X - a value of kind K variable): (- @pull llo_transfer; {X} = llo_transfer; -).
Low-Level Operations ends here.
---- DOCUMENTATION ----
Chapter: Synopsis
Low-Level Operations makes other extensions easier to write by wrapping common
uses of I6 and Glulx assembly code in human-friendly I7 phrases. Bit-level
arithmetic, access to memory by address, and unsafe conversions between kinds
are all included, as well as a few more obscure features, like unsigned numbers
and the ability to check Glulx gestalts.
Details are in the following chapters.
Chapter: Usage
Section: Checking interpreter support