-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathtables.c
2064 lines (1779 loc) · 76 KB
/
tables.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
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
/* ------------------------------------------------------------------------- */
/* "tables" : Constructs the story file (the output) up to the end */
/* of dynamic memory, gluing together all the required */
/* tables. */
/* */
/* Part of Inform 6.43 */
/* copyright (c) Graham Nelson 1993 - 2024 */
/* */
/* ------------------------------------------------------------------------- */
#include "header.h"
uchar *zmachine_paged_memory; /* Where we shall store the story file
constructed (contains all of paged
memory, i.e. all but code and the
static strings: allocated only when
we know how large it needs to be,
at the end of the compilation pass */
/* In Glulx, zmachine_paged_memory contains all of RAM -- i.e. all but
the header, the code, the static arrays, and the static strings. */
/* ------------------------------------------------------------------------- */
/* Offsets of various areas in the Z-machine: these are set to nominal */
/* values before the compilation pass, and to their calculated final */
/* values only when construct_storyfile() happens. These are then used to */
/* backpatch the incorrect values now existing in the Z-machine which */
/* used these nominal values. */
/* Most of the nominal values are 0x800 because this is guaranteed to */
/* be assembled as a long constant if it's needed in code, since the */
/* largest possible value of scale_factor is 8 and 0x800/8 = 256. */
/* */
/* In Glulx, I use 0x12345 instead of 0x800. This will always be a long */
/* (32-bit) constant, since there's no scale_factor. */
/* ------------------------------------------------------------------------- */
int32 code_offset,
actions_offset,
preactions_offset,
dictionary_offset,
adjectives_offset,
variables_offset,
strings_offset,
class_numbers_offset,
individuals_offset,
identifier_names_offset,
array_names_offset,
prop_defaults_offset,
prop_values_offset,
static_memory_offset,
attribute_names_offset,
action_names_offset,
fake_action_names_offset,
routine_names_offset,
constant_names_offset,
routines_array_offset,
constants_array_offset,
routine_flags_array_offset,
global_names_offset,
global_flags_array_offset,
array_flags_array_offset,
static_arrays_offset;
int32 arrays_offset,
object_tree_offset,
grammar_table_offset,
abbreviations_offset; /* Glulx */
int32 Out_Size, Write_Code_At, Write_Strings_At;
int32 RAM_Size, Write_RAM_At; /* Glulx */
int zcode_compact_globals_adjustment;
/* ------------------------------------------------------------------------- */
/* Story file header settings. (Written to in "directs.c" and "asm.c".) */
/* ------------------------------------------------------------------------- */
int release_number, /* Release number game is to have */
statusline_flag; /* Either TIME_STYLE or SCORE_STYLE */
int serial_code_given_in_program /* If TRUE, a Serial directive has */
= FALSE; /* specified this 6-digit serial code */
char serial_code_buffer[7]; /* (overriding the usual date-stamp) */
int flags2_requirements[16]; /* An array of which bits in Flags 2 of
the header will need to be set:
e.g. if the save_undo / restore_undo
opcodes are ever assembled, we have
to set the "games want UNDO" bit.
Values are 0 or 1. */
/* ------------------------------------------------------------------------- */
/* Construct story file (up to code area start). */
/* */
/* (To understand what follows, you really need to look at the run-time */
/* system's specification, the Z-Machine Standards document.) */
/* ------------------------------------------------------------------------- */
extern void write_serial_number(char *buffer)
{
/* Note that this function may require modification for "ANSI" compilers
which do not provide the standard time functions: what is needed is
the ability to work out today's date */
time_t tt; tt=time(0);
if (serial_code_given_in_program) {
strcpy(buffer, serial_code_buffer);
}
else {
#ifdef TIME_UNAVAILABLE
sprintf(buffer,"970000");
#else
/* Write a six-digit date, null-terminated. Fall back to "970000"
if that fails. */
int len = strftime(buffer,7,"%y%m%d",localtime(&tt));
if (len != 6)
sprintf(buffer,"970000");
#endif
}
}
static char percentage_buffer[64];
static char *show_percentage(int32 x, int32 total)
{
if (memory_map_setting < 2) {
percentage_buffer[0] = '\0';
}
else if (x == 0) {
sprintf(percentage_buffer, " ( --- )");
}
else if (memory_map_setting < 3) {
sprintf(percentage_buffer, " (%.1f %%)", (float)x * 100.0 / (float)total);
}
else {
sprintf(percentage_buffer, " (%.1f %%, %d bytes)", (float)x * 100.0 / (float)total, x);
}
return percentage_buffer;
}
static char *version_name(int v)
{
if (!glulx_mode) {
switch(v)
{ case 3: return "Standard";
case 4: return "Plus";
case 5: return "Advanced";
case 6: return "Graphical";
case 7: return "Extended Alternate";
case 8: return "Extended";
}
return "experimental format";
}
else {
return "Glulx";
}
}
static int32 rough_size_of_paged_memory_z(void)
{
/* This function calculates a modest over-estimate of the amount of
memory required to store the Z-machine's paged memory area
(that is, everything up to the start of the code area). */
int32 total, i;
ASSERT_ZCODE();
total = 64 /* header */
+ 2 + low_strings_top
/* low strings pool */
+ 6*32; /* abbreviations table */
total += 8; /* header extension table */
if (ZCODE_HEADER_EXT_WORDS>3) total += (ZCODE_HEADER_EXT_WORDS-3)*2;
if (alphabet_modified) total += 78; /* character set table */
if (zscii_defn_modified) /* Unicode translation table */
total += 2 + 2*zscii_high_water_mark;
total += 2*((version_number==3)?31:63) /* property default values */
+ no_objects*((version_number==3)?9:14) /* object tree table */
+ properties_table_size /* property values of objects */
+ (no_classes+1)*2
/* class object numbers table */
+ no_symbols*2 /* names of numerous things */
+ individuals_length /* tables of prop variables */
+ dynamic_array_area_size; /* variables and arrays */
for (i=0; i<no_Inform_verbs; i++)
total += 2 + 1 + /* address of grammar table, */
/* number of grammar lines */
((grammar_version_number == 1)?
(8*Inform_verbs[i].lines):0); /* grammar lines */
if (grammar_version_number != 1)
total += grammar_lines_top; /* size of grammar lines area */
total += 2 + 4*no_adjectives /* adjectives table */
+ 2*no_actions /* action routines */
+ 2*no_grammar_token_routines; /* general parsing routines */
total += (dictionary_top) /* dictionary size */
+ (0); /* module map */
total += static_array_area_size; /* static arrays */
total += scale_factor*0x100 /* maximum null bytes before code */
+ 1000; /* fudge factor (in case the above is wrong) */
return(total);
}
static int32 rough_size_of_paged_memory_g(void)
{
/* This function calculates a modest over-estimate of the amount of
memory required to store the machine's paged memory area
(that is, everything past the start of RAM). */
int32 total;
ASSERT_GLULX();
/* No header for us! */
total = 1000; /* bit of a fudge factor */
total += no_globals * 4; /* global variables */
total += dynamic_array_area_size; /* arrays */
total += no_objects * OBJECT_BYTE_LENGTH; /* object tables */
total += properties_table_size; /* property tables */
total += no_properties * 4; /* property defaults table */
total += 4 + no_classes * 4; /* class prototype object numbers */
total += 32; /* address/length of the identifier tables */
total += no_properties * 4;
total += (no_individual_properties-INDIV_PROP_START) * 4;
total += (NUM_ATTR_BYTES*8) * 4;
total += (no_actions + no_fake_actions) * 4;
total += 4 + no_arrays * 4;
total += 4 + no_Inform_verbs * 4; /* index of grammar tables */
total += grammar_lines_top; /* grammar tables */
total += 4 + no_actions * 4; /* actions functions table */
total += 4;
total += dictionary_top;
while (total % GPAGESIZE)
total++;
return(total);
}
static void construct_storyfile_z(void)
{ uchar *p;
int32 i, j, k, l, mark, objs, strings_length, code_length,
limit=0, excess=0, extend_offset=0, headerext_length=0;
int32 globals_at=0, dictionary_at=0, actions_at=0, preactions_at=0,
abbrevs_at=0, prop_defaults_at=0, object_tree_at=0, object_props_at=0,
grammar_table_at=0, charset_at=0, headerext_at=0,
terminating_chars_at=0, unicode_at=0, id_names_length=0,
arrays_at, static_arrays_at=0;
int32 rough_size;
int skip_backpatching = FALSE;
char *output_called = "story file";
ASSERT_ZCODE();
if (!OMIT_SYMBOL_TABLE) {
individual_name_strings =
my_calloc(sizeof(int32), no_individual_properties,
"identifier name strings");
action_name_strings =
my_calloc(sizeof(int32), no_actions + no_fake_actions,
"action name strings");
attribute_name_strings =
my_calloc(sizeof(int32), 48,
"attribute name strings");
array_name_strings =
my_calloc(sizeof(int32),
no_symbols,
"array name strings");
write_the_identifier_names();
}
/* We now know how large the buffer to hold our construction has to be */
rough_size = rough_size_of_paged_memory_z();
zmachine_paged_memory = my_malloc(rough_size, "output buffer");
/* Foolish code to make this routine compile on all ANSI compilers */
p = (uchar *) zmachine_paged_memory;
/* In what follows, the "mark" will move upwards in memory: at various
points its value will be recorded for milestones like
"dictionary table start". It begins at 0x40, just after the header */
for (mark=0; mark<0x40; mark++)
p[mark] = 0x0;
/* ----------------- Low Strings and Abbreviations -------------------- */
p[mark]=0x80; p[mark+1]=0; mark+=2; /* Start the low strings pool
with a useful default string, " " */
for (i=0; i<low_strings_top; mark++, i++) /* Low strings pool */
p[0x42+i]=low_strings[i];
abbrevs_at = mark;
if (MAX_ABBREVS + MAX_DYNAMIC_STRINGS != 96)
fatalerror("MAX_ABBREVS + MAX_DYNAMIC_STRINGS is not 96");
/* Initially all 96 entries are set to " ". (We store half of 0x40,
the address of the " " we wrote above.) */
for (i=0; i<3*32; i++)
{ p[mark++]=0; p[mark++]=0x20;
}
/* Entries from 0 to MAX_DYNAMIC_STRINGS (default 32) are "variable
strings". Write the abbreviations after these. */
k = abbrevs_at+2*MAX_DYNAMIC_STRINGS;
for (i=0; i<no_abbreviations; i++)
{ j=abbreviations[i].value;
p[k++]=j/256;
p[k++]=j%256;
}
/* ------------------- Header extension table ------------------------- */
headerext_at = mark;
headerext_length = ZCODE_HEADER_EXT_WORDS;
if (zscii_defn_modified) {
/* Need at least 3 words for unicode table address */
if (headerext_length < 3)
headerext_length = 3;
}
if (ZCODE_HEADER_FLAGS_3) {
/* Need at least 4 words for the flags-3 field (ZSpec 1.1) */
if (headerext_length < 4)
headerext_length = 4;
}
p[mark++] = 0; p[mark++] = headerext_length;
for (i=0; i<headerext_length; i++)
{ p[mark++] = 0; p[mark++] = 0;
}
/* -------------------- Z-character set table ------------------------- */
if (alphabet_modified)
{ charset_at = mark;
for (i=0;i<3;i++) for (j=0;j<26;j++)
{ if (alphabet[i][j] == '~') p[mark++] = '\"';
else p[mark++] = alphabet[i][j];
}
}
/* ------------------ Unicode translation table ----------------------- */
unicode_at = 0;
if (zscii_defn_modified)
{ unicode_at = mark;
p[mark++] = zscii_high_water_mark;
for (i=0;i<zscii_high_water_mark;i++)
{ j = zscii_to_unicode(155 + i);
if (j < 0 || j > 0xFFFF) {
error("Z-machine Unicode translation table cannot contain characters beyond $FFFF.");
}
p[mark++] = j/256; p[mark++] = j%256;
}
}
/* -------------------- Objects and Properties ------------------------ */
/* The object table must be word-aligned. The Z-machine spec does not
require this, but the RA__Pr() veneer routine does.
*/
while ((mark%2) != 0) p[mark++]=0;
prop_defaults_at = mark;
p[mark++]=0; p[mark++]=0;
for (i=2; i< ((version_number==3)?32:64); i++)
{ p[mark++]=commonprops[i].default_value/256;
p[mark++]=commonprops[i].default_value%256;
}
object_tree_at = mark;
mark += ((version_number==3)?9:14)*no_objects;
object_props_at = mark;
for (i=0; i<properties_table_size; i++)
p[mark+i]=properties_table[i];
for (i=0, objs=object_tree_at; i<no_objects; i++)
{
if (version_number == 3)
{ p[objs]=objectsz[i].atts[0];
p[objs+1]=objectsz[i].atts[1];
p[objs+2]=objectsz[i].atts[2];
p[objs+3]=objectsz[i].atts[3];
p[objs+4]=objectsz[i].parent;
p[objs+5]=objectsz[i].next;
p[objs+6]=objectsz[i].child;
p[objs+7]=mark/256;
p[objs+8]=mark%256;
objs+=9;
}
else
{ p[objs]=objectsz[i].atts[0];
p[objs+1]=objectsz[i].atts[1];
p[objs+2]=objectsz[i].atts[2];
p[objs+3]=objectsz[i].atts[3];
p[objs+4]=objectsz[i].atts[4];
p[objs+5]=objectsz[i].atts[5];
p[objs+6]=(objectsz[i].parent)/256;
p[objs+7]=(objectsz[i].parent)%256;
p[objs+8]=(objectsz[i].next)/256;
p[objs+9]=(objectsz[i].next)%256;
p[objs+10]=(objectsz[i].child)/256;
p[objs+11]=(objectsz[i].child)%256;
p[objs+12]=mark/256;
p[objs+13]=mark%256;
objs+=14;
}
mark+=objectsz[i].propsize;
}
/* ----------- Table of Class Prototype Object Numbers ---------------- */
class_numbers_offset = mark;
for (i=0; i<no_classes; i++)
{ p[mark++] = class_info[i].object_number/256;
p[mark++] = class_info[i].object_number%256;
}
p[mark++] = 0;
p[mark++] = 0;
/* ------------------- Table of Identifier Names ---------------------- */
identifier_names_offset = mark;
if (!OMIT_SYMBOL_TABLE)
{ p[mark++] = no_individual_properties/256;
p[mark++] = no_individual_properties%256;
for (i=1; i<no_individual_properties; i++)
{ p[mark++] = individual_name_strings[i]/256;
p[mark++] = individual_name_strings[i]%256;
}
attribute_names_offset = mark;
for (i=0; i<48; i++)
{ p[mark++] = attribute_name_strings[i]/256;
p[mark++] = attribute_name_strings[i]%256;
}
action_names_offset = mark;
fake_action_names_offset = mark + 2*no_actions;
for (i=0; i<no_actions + no_fake_actions; i++)
{
int ax = i;
if (i<no_actions && GRAMMAR_META_FLAG)
ax = sorted_actions[i].external_to_int;
j = action_name_strings[ax];
p[mark++] = j/256;
p[mark++] = j%256;
}
array_names_offset = mark;
global_names_offset = mark + 2*no_arrays;
routine_names_offset = global_names_offset + 2*no_globals;
constant_names_offset = routine_names_offset + 2*no_named_routines;
for (i=0; i<no_arrays + no_globals
+ no_named_routines + no_named_constants; i++)
{ if ((i == no_arrays) && (define_INFIX_switch == FALSE)) break;
p[mark++] = array_name_strings[i]/256;
p[mark++] = array_name_strings[i]%256;
}
id_names_length = (mark - identifier_names_offset)/2;
}
else {
attribute_names_offset = mark;
action_names_offset = mark;
fake_action_names_offset = mark;
array_names_offset = mark;
global_names_offset = mark;
routine_names_offset = mark;
constant_names_offset = mark;
id_names_length = 0;
}
routine_flags_array_offset = mark;
if (define_INFIX_switch)
{ for (i=0, k=1, l=0; i<no_named_routines; i++)
{ if (symbols[named_routine_symbols[i]].flags & STAR_SFLAG) l=l+k;
k=k*2;
if (k==256) { p[mark++] = l; k=1; l=0; }
}
if (k!=1) p[mark++]=l;
}
/* ---------------- Table of Indiv Property Values -------------------- */
individuals_offset = mark;
for (i=0; i<individuals_length; i++)
p[mark++] = individuals_table[i];
/* ----------------- Variables and Dynamic Arrays --------------------- */
globals_at = mark;
if (ZCODE_COMPACT_GLOBALS) {
for (i = 0; i < no_globals; i++) {
j = global_initial_value[i];
p[mark++] = j / 256; p[mark++] = j % 256;
}
arrays_at = mark;
for (i = (MAX_ZCODE_GLOBAL_VARS * WORDSIZE); i < dynamic_array_area_size; i++)
p[mark++] = dynamic_array_area[i];
/* When arrays move up we need a adjustment value to use when backkpatching */
zcode_compact_globals_adjustment = ((MAX_ZCODE_GLOBAL_VARS - no_globals) * WORDSIZE);
}
else
{
for (i = 0; i < dynamic_array_area_size; i++)
p[mark++] = dynamic_array_area[i];
for (i = 0; i < 240; i++)
{
j = global_initial_value[i];
p[globals_at + i * 2] = j / 256; p[globals_at + i * 2 + 1] = j % 256;
}
arrays_at = globals_at + (MAX_ZCODE_GLOBAL_VARS * WORDSIZE);
zcode_compact_globals_adjustment = 0;
}
/* ------------------ Terminating Characters Table -------------------- */
if (version_number >= 5)
{ terminating_chars_at = mark;
for (i=0; i<no_termcs; i++) p[mark++] = terminating_characters[i];
p[mark++] = 0;
}
/* ------------------------ Static Memory ----------------------------- */
/* Ensure that static memory begins at least 480 bytes after the globals.
There's normally 240 globals, but with ZCODE_COMPACT_GLOBALS it
might be less. */
if (mark < globals_at+480)
mark = globals_at+480;
/* ------------------------ Grammar Table ----------------------------- */
grammar_table_at = mark;
mark = mark + no_Inform_verbs*2;
for (i=0; i<no_Inform_verbs; i++)
{ p[grammar_table_at + i*2] = (mark/256);
p[grammar_table_at + i*2 + 1] = (mark%256);
if (!Inform_verbs[i].used) {
/* This verb was marked unused at locate_dead_grammar_lines()
time. Omit the grammar lines. */
p[mark++] = 0;
continue;
}
p[mark++] = Inform_verbs[i].lines;
for (j=0; j<Inform_verbs[i].lines; j++)
{ k = Inform_verbs[i].l[j];
if (grammar_version_number == 1)
{ int m, n;
p[mark+7] = grammar_lines[k+1];
for (m=1;m<=6;m++) p[mark + m] = 0;
k = k + 2; m = 1; n = 0;
while ((grammar_lines[k] != 15) && (m<=6))
{ p[mark + m] = grammar_lines[k];
if (grammar_lines[k] < 180) n++;
m++; k = k + 3;
}
p[mark] = n;
mark = mark + 8;
}
else if (grammar_version_number == 2)
{ int tok;
p[mark++] = grammar_lines[k++];
p[mark++] = grammar_lines[k++];
for (;;)
{ tok = grammar_lines[k++];
p[mark++] = tok;
if (tok == 15) break;
p[mark++] = grammar_lines[k++];
p[mark++] = grammar_lines[k++];
}
}
else if (grammar_version_number == 3)
{
int no_tok, l;
p[mark++] = grammar_lines[k++];
p[mark++] = grammar_lines[k++];
no_tok = ((p[mark - 2] & 0xF8) >> 3);
for (l = 0; l < no_tok; l++)
{
p[mark++] = grammar_lines[k++];
p[mark++] = grammar_lines[k++];
}
}
else
{
error("invalid grammar version for Z-code");
}
}
}
/* ------------------- Actions and Preactions ------------------------- */
/* (The term "preactions" is traditional: Inform uses the preactions */
/* table for a different purpose than Infocom used to.) */
/* The values are written later, when the Z-code offset is known. */
/* -------------------------------------------------------------------- */
actions_at = mark;
mark += no_actions*2;
preactions_at = mark;
if (grammar_version_number == 1 || grammar_version_number == 3)
mark += no_grammar_token_routines*2;
/* ----------------------- Adjectives Table --------------------------- */
if (grammar_version_number == 1)
{ p[mark]=0; p[mark+1]=no_adjectives; mark+=2; /* To assist "infodump" */
adjectives_offset = mark;
dictionary_offset = mark + 4*no_adjectives;
for (i=0; i<no_adjectives; i++)
{ j = final_dict_order[adjectives[no_adjectives-i-1]]
*DICT_ENTRY_BYTE_LENGTH
+ dictionary_offset + 7;
p[mark++]=j/256; p[mark++]=j%256; p[mark++]=0;
p[mark++]=(256-no_adjectives+i);
}
}
else if (grammar_version_number == 2)
{ p[mark]=0; p[mark+1]=0; mark+=2;
adjectives_offset = mark;
dictionary_offset = mark;
}
else if (grammar_version_number == 3)
{
p[mark] = 0; p[mark + 1] = no_adjectives; mark += 2;
adjectives_offset = mark; /* adjectives_offset points at start of
data, not at length of table word */
dictionary_offset = mark + 2 * no_adjectives;
for (i = 0; i < no_adjectives; i++)
{
j = final_dict_order[adjectives[i]]
* DICT_ENTRY_BYTE_LENGTH
+ dictionary_offset + 7;
p[mark++] = j / 256; p[mark++] = j % 256;
}
}
/* ------------------------- Dictionary ------------------------------- */
dictionary_at=mark;
dictionary[0]=3; dictionary[1]='.'; /* Non-space characters which */
dictionary[2]=','; /* force words apart */
dictionary[3]='"';
dictionary[4]=DICT_ENTRY_BYTE_LENGTH; /* Length of each entry */
dictionary[5]=(dict_entries/256); /* Number of entries */
dictionary[6]=(dict_entries%256);
for (i=0; i<7; i++) p[mark++] = dictionary[i];
for (i=0; i<dict_entries; i++)
{ k = 7 + i*DICT_ENTRY_BYTE_LENGTH;
j = mark + final_dict_order[i]*DICT_ENTRY_BYTE_LENGTH;
for (l = 0; l<DICT_ENTRY_BYTE_LENGTH; l++)
p[j++] = dictionary[k++];
}
mark += dict_entries * DICT_ENTRY_BYTE_LENGTH;
/* ------------------------- Module Map ------------------------------- */
/* (no longer used) */
/* ------------------------ Static Arrays ----------------------------- */
static_arrays_at = mark;
for (i=0; i<static_array_area_size; i++)
p[mark++] = static_array_area[i];
/* ----------------- A gap before the code area ----------------------- */
/* (so that it will start at an exact packed address and so that all */
/* routine packed addresses are >= 256, hence long constants) */
/* -------------------------------------------------------------------- */
while ((mark%length_scale_factor) != 0) p[mark++]=0;
while (mark < (scale_factor*0x100)) p[mark++]=0;
if (oddeven_packing_switch)
while ((mark%(scale_factor*2)) != 0) p[mark++]=0;
if (mark > 0x0FFFE)
{ error("This program has overflowed the maximum readable-memory \
size of the Z-machine format. See the memory map below: the start \
of the area marked \"above readable memory\" must be brought down to $FFFE \
or less.");
memory_map_setting = 1;
/* Backpatching the grammar tables requires us to trust some of the */
/* addresses we've written into Z-machine memory, but they may have */
/* been truncated to 16 bits, so we can't do it. */
skip_backpatching = TRUE;
}
/* -------------------------- Code Area ------------------------------- */
/* (From this point on we don't write any higher into the "p" buffer.) */
/* -------------------------------------------------------------------- */
if (mark > rough_size)
compiler_error("Paged size exceeds rough estimate.");
Write_Code_At = mark;
if (!OMIT_UNUSED_ROUTINES) {
code_length = zmachine_pc;
}
else {
if ((uint32)zmachine_pc != df_total_size_before_stripping)
compiler_error("Code size does not match (zmachine_pc and df_total_size).");
code_length = df_total_size_after_stripping;
}
mark += code_length;
/* ------------------ Another synchronising gap ----------------------- */
if (oddeven_packing_switch)
{
while ((mark%(scale_factor*2)) != scale_factor) mark++;
}
else
while ((mark%scale_factor) != 0) mark++;
/* ------------------------- Strings Area ----------------------------- */
Write_Strings_At = mark;
strings_length = static_strings_extent;
mark += strings_length;
/* --------------------- Module Linking Data -------------------------- */
/* (no longer used) */
/* --------------------- Is the file too big? ------------------------- */
Out_Size = mark;
switch(version_number)
{ case 3: excess = Out_Size-((int32) 0x20000L); limit = 128; break;
case 4:
case 5: excess = Out_Size-((int32) 0x40000L); limit = 256; break;
case 6:
case 7:
case 8: excess = Out_Size-((int32) 0x80000L); limit = 512; break;
}
if (excess > 0)
{
fatalerror_fmt(
"The %s exceeds version-%d limit (%dK) by %d bytes",
output_called, version_number, limit, excess);
}
/* --------------------------- Offsets -------------------------------- */
dictionary_offset = dictionary_at;
variables_offset = globals_at;
arrays_offset = arrays_at;
actions_offset = actions_at;
preactions_offset = preactions_at;
prop_defaults_offset = prop_defaults_at;
prop_values_offset = object_props_at;
static_memory_offset = grammar_table_at;
grammar_table_offset = grammar_table_at;
static_arrays_offset = static_arrays_at;
if (extend_memory_map)
{ extend_offset=256;
if (no_objects+9 > extend_offset) extend_offset=no_objects+9;
while ((extend_offset%length_scale_factor) != 0) extend_offset++;
/* Not sure why above line is necessary, but oddeven_packing
* will need extend_offset to be even */
code_offset = extend_offset*scale_factor;
if (oddeven_packing_switch)
strings_offset = code_offset + scale_factor;
else
strings_offset = code_offset + (Write_Strings_At-Write_Code_At);
/* With the extended memory model, need to specifically check that we
* haven't overflowed the packed address range for routines or strings.
* With the standard memory model, we only need the earlier total size
* check.
*/
excess = code_length + code_offset - (scale_factor*((int32) 0x10000L));
if (excess > 0)
{
fatalerror_fmt(
"The code area limit has been exceeded by %d bytes",
excess);
}
excess = strings_length + strings_offset - (scale_factor*((int32) 0x10000L));
if (excess > 0)
{
if (oddeven_packing_switch)
fatalerror_fmt(
"The strings area limit has been exceeded by %d bytes",
excess);
else
fatalerror_fmt(
"The code+strings area limit has been exceeded by %d bytes. \
Try running Inform again with -B on the command line.",
excess);
}
}
else
{ code_offset = Write_Code_At;
strings_offset = Write_Strings_At;
}
/* --------------------------- The Header ----------------------------- */
for (i=0; i<=0x3f; i++) p[i]=0; /* Begin with 64 blank bytes */
p[0] = version_number; /* Version number */
p[1] = statusline_flag*2; /* Bit 1 of Flags 1: statusline style */
p[2] = (release_number/256);
p[3] = (release_number%256); /* Release */
p[4] = (Write_Code_At/256);
p[5] = (Write_Code_At%256); /* End of paged memory */
if (version_number==6)
{ j=code_offset/scale_factor; /* Packed address of "Main__" */
p[6]=(j/256); p[7]=(j%256);
}
else
{ j=Write_Code_At+1; /* Initial PC value (bytes) */
p[6]=(j/256); p[7]=(j%256); /* (first opcode in "Main__") */
}
p[8] = (dictionary_at/256); p[9]=(dictionary_at%256); /* Dictionary */
p[10]=prop_defaults_at/256; p[11]=prop_defaults_at%256; /* Objects */
p[12]=(globals_at/256); p[13]=(globals_at%256); /* Dynamic area */
p[14]=(grammar_table_at/256);
p[15]=(grammar_table_at%256); /* Static area */
for (i=0, j=0, k=1;i<16;i++, k=k*2) /* Flags 2 as needed for any */
j+=k*flags2_requirements[i]; /* unusual opcodes assembled */
p[16]=j/256; p[17]=j%256;
write_serial_number((char *) (p+18)); /* Serial number: 6 chars of ASCII */
p[24]=abbrevs_at/256;
p[25]=abbrevs_at%256; /* Abbreviations table */
p[26]=0; p[27]=0; /* Length of file to be filled in "files.c" */
p[28]=0; p[29]=0; /* Checksum to be filled in "files.c" */
if (extend_memory_map)
{ j=(Write_Code_At - extend_offset*scale_factor)/length_scale_factor;
p[40]=j/256; p[41]=j%256; /* Routines offset */
if (oddeven_packing_switch)
j=(Write_Strings_At - extend_offset*scale_factor)/length_scale_factor;
p[42]=j/256; p[43]=j%256; /* = Strings offset */
}
if (version_number >= 5)
{ p[46] = terminating_chars_at/256; /* Terminating characters table */
p[47] = terminating_chars_at%256;
}
if (alphabet_modified)
{ j = charset_at;
p[52]=j/256; p[53]=j%256; } /* Character set table address */
j = headerext_at;
p[54] = j/256; p[55] = j%256; /* Header extension table address */
p[60] = '0' + ((RELEASE_NUMBER/100)%10);
p[61] = '.';
p[62] = '0' + ((RELEASE_NUMBER/10)%10);
p[63] = '0' + RELEASE_NUMBER%10;
/* ------------------------ Header Extension -------------------------- */
/* The numbering in the spec is a little weird -- it's headerext_length
words *after* the initial length word. We follow the spec numbering
in this switch statement, so the count is 1-based. */
for (i=1; i<=headerext_length; i++) {
switch (i) {
case 3:
j = unicode_at; /* Unicode translation table address */
break;
case 4:
j = ZCODE_HEADER_FLAGS_3; /* Flags 3 word */
break;
default:
j = 0;
break;
}
p[headerext_at+2*i+0] = j / 256;
p[headerext_at+2*i+1] = j % 256;
}
/* ----------------- The Header: Extras for modules ------------------- */
/* (no longer used) */
/* ---- Backpatch the Z-machine, now that all information is in ------- */
if (!skip_backpatching)
{ backpatch_zmachine_image_z();
/* The symbol name, action, and grammar tables must be backpatched specially. */
if (!OMIT_SYMBOL_TABLE) {
for (i=1; i<id_names_length; i++)
{ int32 v = 256*p[identifier_names_offset + i*2]
+ p[identifier_names_offset + i*2 + 1];
if (v!=0) v += strings_offset/scale_factor;
p[identifier_names_offset + i*2] = v/256;
p[identifier_names_offset + i*2 + 1] = v%256;
}
}
mark = actions_at;
for (i=0; i<no_actions; i++)
{
int ax = i;
if (GRAMMAR_META_FLAG)
ax = sorted_actions[i].external_to_int;
j=actions[ax].byte_offset;
if (OMIT_UNUSED_ROUTINES)
j = df_stripped_address_for_address(j);
j += code_offset/scale_factor;
p[mark++]=j/256; p[mark++]=j%256;
}
if (grammar_version_number == 1 || grammar_version_number == 3)
{ mark = preactions_at;
for (i=0; i<no_grammar_token_routines; i++)
{ j=grammar_token_routine[i];
if (OMIT_UNUSED_ROUTINES)
j = df_stripped_address_for_address(j);
j += code_offset/scale_factor;
p[mark++]=j/256; p[mark++]=j%256;
}
if (GRAMMAR_META_FLAG) {
/* backpatch the action numbers */
for (l = 0; l<no_Inform_verbs; l++)
{
int linecount;
k = grammar_table_at + 2*l;
i = p[k]*256 + p[k+1];
linecount = p[i++];
for (j=0; j<linecount; j++) {
int action = p[i+7];
action = sorted_actions[action].internal_to_ext;
p[i+7] = action;
}
}
}
}
else
{ for (l = 0; l<no_Inform_verbs; l++)
{
int linecount;
k = grammar_table_at + 2*l;
i = p[k]*256 + p[k+1];
linecount = p[i++];
for (j=0; j<linecount; j++)
{ int topbits; int32 value;
if (GRAMMAR_META_FLAG) {
/* backpatch the action number */
int word = p[i]*256 + p[i+1];
int action = word & 0x03FF;
int flags = word & 0xFC00;
if (action >= 0 && action < no_actions) {
action = sorted_actions[action].internal_to_ext;
word = flags | action;
p[i] = word/256; p[i+1] = word%256;
}
}
i = i + 2;