-
Notifications
You must be signed in to change notification settings - Fork 35
/
files.c
1804 lines (1570 loc) · 60.1 KB
/
files.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
/* ------------------------------------------------------------------------- */
/* "files" : File handling for source code, the transcript file and the */
/* debugging information file; file handling and splicing of */
/* the output file. */
/* */
/* Note that filenaming conventions are left to the top-level */
/* routines in "inform.c", since they are tied up with ICL */
/* settings and are very host OS-dependent. */
/* */
/* Part of Inform 6.43 */
/* copyright (c) Graham Nelson 1993 - 2024 */
/* */
/* ------------------------------------------------------------------------- */
#include "header.h"
int total_files; /* Number of files so far, including
#include and #origsource files */
int total_input_files; /* Number of source files so far
(excludes #origsource) */
int current_input_file; /* Most recently-opened source file */
static int current_origsource_file; /* Most recently-used #origsource */
int32 total_chars_read; /* Characters read in (from all
source files put together) */
static int checksum_low_byte, /* For calculating the Z-machine's */
checksum_high_byte; /* "verify" checksum */
static uint32 checksum_long; /* For the Glulx checksum, */
static int checksum_count; /* similarly */
/* ------------------------------------------------------------------------- */
/* Most of the information about source files is kept by "lexer.c"; this */
/* level is only concerned with file names and handles. */
/* ------------------------------------------------------------------------- */
FileId *InputFiles=NULL; /* Ids for all the source files
Allocated to total_files */
static memory_list InputFiles_memlist;
/* ------------------------------------------------------------------------- */
/* When emitting debug information, we won't have addresses of routines, */
/* sequence points, Glulx objects (addresses of Z-machine objects aren't */
/* needed), globals, arrays, or grammar lines. We only have their */
/* offsets from base addresses, which won't be known until the end of */
/* compilation. Since everything else in the relevant debug records is */
/* known much earlier and is less convenient to store up, we emit the */
/* debug records with a placeholder value and then backpatch these */
/* placeholders. The following structs each store either an offset or a */
/* symbol index and the point in the debug information file where the */
/* corresponding address should be written once the base address is known. */
/* ------------------------------------------------------------------------- */
#define INITIAL_DEBUG_INFORMATION_BACKPATCH_ALLOCATION 65536
typedef struct value_and_backpatch_position_struct
{ int32 value;
fpos_t backpatch_position;
} value_and_backpatch_position;
typedef struct debug_backpatch_accumulator_struct
{ int32 number_of_values_to_backpatch;
int32 number_of_available_backpatches;
value_and_backpatch_position *values_and_backpatch_positions;
int32 (* backpatching_function)(int32);
} debug_backpatch_accumulator;
static debug_backpatch_accumulator object_backpatch_accumulator;
static debug_backpatch_accumulator packed_code_backpatch_accumulator;
static debug_backpatch_accumulator code_backpatch_accumulator;
static debug_backpatch_accumulator global_backpatch_accumulator;
static debug_backpatch_accumulator array_backpatch_accumulator;
static debug_backpatch_accumulator grammar_backpatch_accumulator;
/* ------------------------------------------------------------------------- */
/* Opening and closing source code files */
/* ------------------------------------------------------------------------- */
#if defined(PC_WIN32) && defined(HAS_REALPATH)
#include <windows.h>
char *realpath(const char *path, char *resolved_path)
{
return GetFullPathNameA(path,PATHLEN,resolved_path,NULL) != 0 ? resolved_path : 0;
}
#endif
extern void load_sourcefile(char *filename_given, int same_directory_flag)
{
/* Meaning: open a new file of Inform source. (The lexer picks up on
this by noticing that input_file has increased.) */
char name[PATHLEN];
#ifdef HAS_REALPATH
char absolute_name[PATHLEN];
#endif
int x = 0;
FILE *handle;
ensure_memory_list_available(&InputFiles_memlist, total_files+1);
do
{ x = translate_in_filename(x, name, filename_given, same_directory_flag,
(total_files==0)?1:0);
handle = fopen(name,"rb");
} while ((handle == NULL) && (x != 0));
InputFiles[total_files].filename = my_malloc(strlen(name)+1, "filename storage");
strcpy(InputFiles[total_files].filename, name);
if (debugfile_switch)
{ debug_file_printf("<source index=\"%d\">", total_files);
debug_file_printf("<given-path>");
debug_file_print_with_entities(filename_given);
debug_file_printf("</given-path>");
#ifdef HAS_REALPATH
if (realpath(name, absolute_name))
{ debug_file_printf("<resolved-path>");
debug_file_print_with_entities(absolute_name);
debug_file_printf("</resolved-path>");
}
#endif
debug_file_printf("<language>Inform 6</language>");
debug_file_printf("</source>");
}
InputFiles[total_files].handle = handle;
if (InputFiles[total_files].handle==NULL)
fatalerror_named("Couldn't open source file", name);
InputFiles[total_files].is_input = TRUE;
InputFiles[total_files].initial_buffering = TRUE;
if (files_trace_setting > 0)
printf("Opening file \"%s\"\n",name);
total_files++;
total_input_files++;
current_input_file = total_files;
}
static void close_sourcefile(int file_number)
{
if (InputFiles[file_number-1].handle == NULL) return;
/* Close this file. But keep the InputFiles entry around, including
its filename. */
if (ferror(InputFiles[file_number-1].handle))
fatalerror_named("I/O failure: couldn't read from source file",
InputFiles[file_number-1].filename);
fclose(InputFiles[file_number-1].handle);
InputFiles[file_number-1].handle = NULL;
if (files_trace_setting > 0) {
char *str = (InputFiles[file_number-1].initial_buffering ? " (in initial buffering)" : "");
printf("Closing file \"%s\"%s\n", InputFiles[file_number-1].filename, str);
}
}
extern void close_all_source(void)
{ int i;
for (i=0; i<total_files; i++) close_sourcefile(i+1);
}
/* ------------------------------------------------------------------------- */
/* Register an #origsource filename. This goes in the InputFiles table, */
/* but we do not open the file or advance current_input_file. */
/* ------------------------------------------------------------------------- */
extern int register_orig_sourcefile(char *filename)
{
int ix;
char *name;
/* If the filename has already been used as an origsource filename,
return that entry. We check the most-recently-used file first, and
then search the list. */
if (current_origsource_file > 0 && current_origsource_file <= total_files) {
if (!strcmp(filename, InputFiles[current_origsource_file-1].filename))
return current_origsource_file;
}
for (ix=0; ix<total_files; ix++) {
if (InputFiles[ix].is_input)
continue;
if (!strcmp(filename, InputFiles[ix].filename)) {
current_origsource_file = ix+1;
return current_origsource_file;
}
}
/* This filename has never been used before. Allocate a new InputFiles
entry. */
name = filename; /* no translation */
ensure_memory_list_available(&InputFiles_memlist, total_files+1);
InputFiles[total_files].filename = my_malloc(strlen(name)+1, "filename storage");
strcpy(InputFiles[total_files].filename, name);
if (debugfile_switch)
{ debug_file_printf("<source index=\"%d\">", total_files);
debug_file_printf("<given-path>");
debug_file_print_with_entities(filename);
debug_file_printf("</given-path>");
debug_file_printf("<language>Inform 7</language>");
debug_file_printf("</source>");
}
InputFiles[total_files].handle = NULL;
InputFiles[total_files].is_input = FALSE;
InputFiles[total_files].initial_buffering = FALSE;
total_files++;
current_origsource_file = total_files;
return current_origsource_file;
}
/* ------------------------------------------------------------------------- */
/* Feeding source code up into the lexical analyser's buffer */
/* (see "lexer.c" for its specification) */
/* ------------------------------------------------------------------------- */
extern int file_load_chars(int file_number, char *buffer, int length)
{
int read_in; FILE *handle;
if (file_number-1 > total_files)
{ buffer[0] = 0; return 1; }
handle = InputFiles[file_number-1].handle;
if (handle == NULL)
{ buffer[0] = 0; return 1; }
read_in = fread(buffer, 1, length, handle);
total_chars_read += read_in;
if (read_in == length) return length;
close_sourcefile(file_number);
if (file_number == 1)
{ buffer[read_in] = 0;
buffer[read_in+1] = 0;
buffer[read_in+2] = 0;
buffer[read_in+3] = 0;
}
else
{ buffer[read_in] = '\n';
buffer[read_in+1] = ' ';
buffer[read_in+2] = ' ';
buffer[read_in+3] = ' ';
}
return(-(read_in+4));
}
/* ------------------------------------------------------------------------- */
/* Final assembly and output of the story file. */
/* ------------------------------------------------------------------------- */
FILE *sf_handle;
static void sf_put(int c)
{
if (!glulx_mode) {
/* The checksum is the unsigned sum mod 65536 of the bytes in the
story file from 0x0040 (first byte after header) to the end. */
checksum_low_byte += c;
if (checksum_low_byte>=256)
{ checksum_low_byte-=256;
if (++checksum_high_byte==256) checksum_high_byte=0;
}
}
else {
/* The checksum is the unsigned 32-bit sum of the entire story file,
considered as a list of 32-bit words, with the checksum field
being zero. */
switch (checksum_count) {
case 0:
checksum_long += (((uint32)(c & 0xFF)) << 24);
break;
case 1:
checksum_long += (((uint32)(c & 0xFF)) << 16);
break;
case 2:
checksum_long += (((uint32)(c & 0xFF)) << 8);
break;
case 3:
checksum_long += ((uint32)(c & 0xFF));
break;
}
checksum_count = (checksum_count+1) & 3;
}
fputc(c, sf_handle);
}
/* Recursive procedure to generate the Glulx compression table. */
static void output_compression(int entnum, uint32 *size, int *count)
{
huffentity_t *ent = &(huff_entities[entnum]);
int32 val;
char *cx;
sf_put(ent->type);
(*size)++;
(*count)++;
switch (ent->type) {
case 0:
val = Write_Strings_At + huff_entities[ent->u.branch[0]].addr;
sf_put((val >> 24) & 0xFF);
sf_put((val >> 16) & 0xFF);
sf_put((val >> 8) & 0xFF);
sf_put((val) & 0xFF);
(*size) += 4;
val = Write_Strings_At + huff_entities[ent->u.branch[1]].addr;
sf_put((val >> 24) & 0xFF);
sf_put((val >> 16) & 0xFF);
sf_put((val >> 8) & 0xFF);
sf_put((val) & 0xFF);
(*size) += 4;
output_compression(ent->u.branch[0], size, count);
output_compression(ent->u.branch[1], size, count);
break;
case 1:
/* no data */
break;
case 2:
sf_put(ent->u.ch);
(*size) += 1;
break;
case 3:
cx = abbreviation_text(ent->u.val);
while (*cx) {
sf_put(*cx);
cx++;
(*size) += 1;
}
sf_put('\0');
(*size) += 1;
break;
case 4:
val = unicode_usage_entries[ent->u.val].ch;
sf_put((val >> 24) & 0xFF);
sf_put((val >> 16) & 0xFF);
sf_put((val >> 8) & 0xFF);
sf_put((val) & 0xFF);
(*size) += 4;
break;
case 9:
val = abbreviations_offset + 4 + ent->u.val*4;
sf_put((val >> 24) & 0xFF);
sf_put((val >> 16) & 0xFF);
sf_put((val >> 8) & 0xFF);
sf_put((val) & 0xFF);
(*size) += 4;
break;
}
}
static void output_file_z(void)
{ char new_name[PATHLEN];
int32 length, blanks=0, i;
uint32 j, offset;
uint32 size, code_length, size_before_code, next_cons_check;
int use_function;
ASSERT_ZCODE();
/* At this point, construct_storyfile() has just been called. */
/* Enter the length information into the header. */
length=((int32) Write_Strings_At) + static_strings_extent;
/* Pad to a multiple of the scale factor (2, 4, or 8 bytes). This
avoids worry about an interpreter doing something strange when
accessing the highest possible packed address. */
while ((length%length_scale_factor)!=0) { length++; blanks++; }
length=length/length_scale_factor;
zmachine_paged_memory[26]=(length & 0xff00)/0x100;
zmachine_paged_memory[27]=(length & 0xff);
/* To assist interpreters running a paged virtual memory system, Inform
writes files which are padded with zeros to the next multiple of
0.5K. This calculates the number of bytes of padding needed: */
if (ZCODE_FILE_END_PADDING) {
while (((length_scale_factor*length)+blanks-1)%512 != 511) blanks++;
}
translate_out_filename(new_name, Code_Name);
sf_handle = fopen(new_name,"wb");
if (sf_handle == NULL)
fatalerror_named("Couldn't open output file", new_name);
#ifdef MAC_MPW
/* Set the type and creator to Andrew Plotkin's MaxZip, a popular
Z-code interpreter on the Macintosh */
fsetfileinfo(new_name, 'mxZR', 'ZCOD');
#endif
/* (1) Output the paged memory. */
for (i=0;i<64;i++)
fputc(zmachine_paged_memory[i], sf_handle);
size = 64;
checksum_low_byte = 0;
checksum_high_byte = 0;
for (i=64; i<Write_Code_At; i++)
{ sf_put(zmachine_paged_memory[i]); size++;
}
/* (2) Output the compiled code area. */
if (!OMIT_UNUSED_ROUTINES) {
/* This is the old-fashioned case, which is easy. All of zcode_area
(zmachine_pc bytes) will be output. next_cons_check will be
ignored, because j will never reach it. */
code_length = zmachine_pc;
use_function = TRUE;
next_cons_check = code_length+1;
}
else {
/* With dead function stripping, life is more complicated.
j will run from 0 to zmachine_pc, but only code_length of
those should be output. next_cons_check is the location of
the next function break; that's where we check whether
we're in a live function or a dead one.
(This logic is simplified by the assumption that a backpatch
marker will never straddle a function break.) */
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;
use_function = TRUE;
next_cons_check = 0;
df_prepare_function_iterate();
}
size_before_code = size;
j=0;
for (i=0; i<zcode_backpatch_size; i=i+3)
{ int long_flag = TRUE;
offset
= 256*zcode_backpatch_table[i+1]
+ zcode_backpatch_table[i+2];
backpatch_error_flag = FALSE;
backpatch_marker
= zcode_backpatch_table[i];
if (backpatch_marker >= 0x80) long_flag = FALSE;
backpatch_marker &= 0x7f;
offset = offset + (backpatch_marker/32)*0x10000;
while (offset+0x30000 < j) {
offset += 0x40000;
long_flag = !long_flag;
}
backpatch_marker &= 0x1f;
/* All code up until the next backpatch marker gets flushed out
as-is. (Unless we're in a stripped-out function.) */
while (j<offset) {
if (!use_function) {
while (j<offset && j<next_cons_check) {
j++;
}
}
else {
while (j<offset && j<next_cons_check) {
size++;
sf_put(zcode_area[j]);
j++;
}
}
if (j == next_cons_check)
next_cons_check = df_next_function_iterate(&use_function);
}
if (long_flag)
{ int32 v = zcode_area[j];
v = 256*v + (zcode_area[j+1]);
j += 2;
if (use_function) {
v = backpatch_value(v);
sf_put(v/256); sf_put(v%256);
size += 2;
}
}
else
{ int32 v = zcode_area[j];
j++;
if (use_function) {
v = backpatch_value(v);
sf_put(v);
size++;
}
}
if (j > next_cons_check)
compiler_error("Backpatch appears to straddle function break");
if (backpatch_error_flag)
{ printf("*** %s zcode offset=%08lx backpatch offset=%08lx ***\n",
(long_flag)?"long":"short", (long int) j, (long int) i);
}
}
/* Flush out the last bit of zcode_area, after the last backpatch
marker. */
offset = zmachine_pc;
while (j<offset) {
if (!use_function) {
while (j<offset && j<next_cons_check) {
j++;
}
}
else {
while (j<offset && j<next_cons_check) {
size++;
sf_put(zcode_area[j]);
j++;
}
}
if (j == next_cons_check)
next_cons_check = df_next_function_iterate(&use_function);
}
if (size_before_code + code_length != size)
compiler_error("Code output length did not match");
/* (3) Output any null bytes (required to reach a packed address)
before the strings area. */
while (size<(uint32)Write_Strings_At) { sf_put(0); size++; }
/* (4) Output the static strings area. */
for (i=0; i<static_strings_extent; i++) {
sf_put(static_strings_area[i]);
size++;
}
/* (5) When modules existed, we output link data here. */
/* (6) Output null bytes at the end. */
while (blanks>0) { sf_put(0); blanks--; }
if (ferror(sf_handle))
fatalerror("I/O failure: couldn't write to story file");
fseek(sf_handle, 28, SEEK_SET);
fputc(checksum_high_byte, sf_handle);
fputc(checksum_low_byte, sf_handle);
if (ferror(sf_handle))
fatalerror("I/O failure: couldn't backtrack on story file for checksum");
fclose(sf_handle);
/* Write a copy of the header into the debugging information file
(mainly so that it can be used to identify which story file matches
with which debugging info file). */
if (debugfile_switch)
{ debug_file_printf("<story-file-prefix>");
for (i = 0; i < 63; i += 3)
{ if (i == 27)
{ debug_file_print_base_64_triple
(zmachine_paged_memory[27],
checksum_high_byte,
checksum_low_byte);
} else
{ debug_file_print_base_64_triple
(zmachine_paged_memory[i],
zmachine_paged_memory[i + 1],
zmachine_paged_memory[i + 2]);
}
}
debug_file_print_base_64_single(zmachine_paged_memory[63]);
debug_file_printf("</story-file-prefix>");
}
#ifdef ARCHIMEDES
{ char settype_command[PATHLEN];
sprintf(settype_command, "settype %s %s",
new_name, riscos_file_type());
system(settype_command);
}
#endif
#ifdef MAC_FACE
InformFiletypes (new_name, INF_ZCODE_TYPE);
#endif
}
static void output_file_g(void)
{ char new_name[PATHLEN];
int32 i;
uint32 j, offset;
uint32 size, code_length, size_before_code, next_cons_check;
int use_function;
int first_byte_of_triple, second_byte_of_triple, third_byte_of_triple;
ASSERT_GLULX();
/* At this point, construct_storyfile() has just been called. */
translate_out_filename(new_name, Code_Name);
sf_handle = fopen(new_name,"wb+");
if (sf_handle == NULL)
fatalerror_named("Couldn't open output file", new_name);
#ifdef MAC_MPW
/* Set the type and creator to Andrew Plotkin's MaxZip, a popular
Z-code interpreter on the Macintosh */
fsetfileinfo(new_name, 'mxZR', 'GLUL');
#endif
checksum_long = 0;
checksum_count = 0;
/* Determine the version number. */
final_glulx_version = 0x00020000;
/* Increase for various features the game may have used. */
if (no_unicode_chars != 0 || (uses_unicode_features)) {
final_glulx_version = 0x00030000;
}
if (uses_memheap_features) {
final_glulx_version = 0x00030100;
}
if (uses_acceleration_features) {
final_glulx_version = 0x00030101;
}
if (uses_float_features) {
final_glulx_version = 0x00030102;
}
if (uses_double_features || uses_extundo_features) {
final_glulx_version = 0x00030103;
}
/* And check if the user has requested a specific version. */
if (requested_glulx_version) {
if (requested_glulx_version < final_glulx_version) {
warning_fmt("Version 0x%08lx requested, but game features require version 0x%08lx",
(long)requested_glulx_version, (long)final_glulx_version);
}
else {
final_glulx_version = requested_glulx_version;
}
}
/* (1) Output the header. We use sf_put here, instead of fputc,
because the header is included in the checksum. */
/* Magic number */
sf_put('G');
sf_put('l');
sf_put('u');
sf_put('l');
/* Version number. */
sf_put((final_glulx_version >> 24));
sf_put((final_glulx_version >> 16));
sf_put((final_glulx_version >> 8));
sf_put((final_glulx_version));
/* RAMSTART */
sf_put((Write_RAM_At >> 24));
sf_put((Write_RAM_At >> 16));
sf_put((Write_RAM_At >> 8));
sf_put((Write_RAM_At));
/* EXTSTART, or game file size */
sf_put((Out_Size >> 24));
sf_put((Out_Size >> 16));
sf_put((Out_Size >> 8));
sf_put((Out_Size));
/* ENDMEM, which the game file size plus MEMORY_MAP_EXTENSION */
i = Out_Size + MEMORY_MAP_EXTENSION;
sf_put((i >> 24));
sf_put((i >> 16));
sf_put((i >> 8));
sf_put((i));
/* STACKSIZE */
sf_put((MAX_STACK_SIZE >> 24));
sf_put((MAX_STACK_SIZE >> 16));
sf_put((MAX_STACK_SIZE >> 8));
sf_put((MAX_STACK_SIZE));
/* Initial function to call. Inform sets things up so that this
is the start of the executable-code area. */
sf_put((Write_Code_At >> 24));
sf_put((Write_Code_At >> 16));
sf_put((Write_Code_At >> 8));
sf_put((Write_Code_At));
/* String-encoding table. */
sf_put((Write_Strings_At >> 24));
sf_put((Write_Strings_At >> 16));
sf_put((Write_Strings_At >> 8));
sf_put((Write_Strings_At));
/* Checksum -- zero for the moment. */
sf_put(0x00);
sf_put(0x00);
sf_put(0x00);
sf_put(0x00);
size = GLULX_HEADER_SIZE;
/* (1a) Output the eight-byte memory layout identifier. */
sf_put('I'); sf_put('n'); sf_put('f'); sf_put('o');
sf_put(0); sf_put(1); sf_put(0); sf_put(0);
/* (1b) Output the rest of the Inform-specific data. */
/* Inform version number */
sf_put('0' + ((RELEASE_NUMBER/100)%10));
sf_put('.');
sf_put('0' + ((RELEASE_NUMBER/10)%10));
sf_put('0' + RELEASE_NUMBER%10);
/* Glulx back-end version number */
sf_put('0' + ((GLULX_RELEASE_NUMBER/100)%10));
sf_put('.');
sf_put('0' + ((GLULX_RELEASE_NUMBER/10)%10));
sf_put('0' + GLULX_RELEASE_NUMBER%10);
/* Game release number */
sf_put((release_number>>8) & 0xFF);
sf_put(release_number & 0xFF);
/* Game serial number */
{
char serialnum[8];
write_serial_number(serialnum);
for (i=0; i<6; i++)
sf_put(serialnum[i]);
}
size += GLULX_STATIC_ROM_SIZE;
/* (2) Output the compiled code area. */
if (!OMIT_UNUSED_ROUTINES) {
/* This is the old-fashioned case, which is easy. All of zcode_area
(zmachine_pc bytes) will be output. next_cons_check will be
ignored, because j will never reach it. */
code_length = zmachine_pc;
use_function = TRUE;
next_cons_check = code_length+1;
}
else {
/* With dead function stripping, life is more complicated.
j will run from 0 to zmachine_pc, but only code_length of
those should be output. next_cons_check is the location of
the next function break; that's where we check whether
we're in a live function or a dead one.
(This logic is simplified by the assumption that a backpatch
marker will never straddle a function break.) */
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;
use_function = TRUE;
next_cons_check = 0;
df_prepare_function_iterate();
}
size_before_code = size;
j=0;
for (i=0; i<zcode_backpatch_size; i=i+6) {
int data_len;
int32 v;
offset =
(zcode_backpatch_table[i+2] << 24)
| (zcode_backpatch_table[i+3] << 16)
| (zcode_backpatch_table[i+4] << 8)
| (zcode_backpatch_table[i+5]);
backpatch_error_flag = FALSE;
backpatch_marker =
zcode_backpatch_table[i];
data_len =
zcode_backpatch_table[i+1];
/* All code up until the next backpatch marker gets flushed out
as-is. (Unless we're in a stripped-out function.) */
while (j<offset) {
if (!use_function) {
while (j<offset && j<next_cons_check) {
j++;
}
}
else {
while (j<offset && j<next_cons_check) {
size++;
sf_put(zcode_area[j]);
j++;
}
}
if (j == next_cons_check)
next_cons_check = df_next_function_iterate(&use_function);
}
/* Write out the converted value of the backpatch marker.
(Unless we're in a stripped-out function.) */
switch (data_len) {
case 4:
v = (zcode_area[j]);
v = (v << 8) | (zcode_area[j+1]);
v = (v << 8) | (zcode_area[j+2]);
v = (v << 8) | (zcode_area[j+3]);
j += 4;
if (!use_function)
break;
v = backpatch_value(v);
sf_put((v >> 24) & 0xFF);
sf_put((v >> 16) & 0xFF);
sf_put((v >> 8) & 0xFF);
sf_put((v) & 0xFF);
size += 4;
break;
case 2:
v = (zcode_area[j]);
v = (v << 8) | (zcode_area[j+1]);
j += 2;
if (!use_function)
break;
v = backpatch_value(v);
if (v >= 0x10000) {
printf("*** backpatch value does not fit ***\n");
backpatch_error_flag = TRUE;
}
sf_put((v >> 8) & 0xFF);
sf_put((v) & 0xFF);
size += 2;
break;
case 1:
v = (zcode_area[j]);
j += 1;
if (!use_function)
break;
v = backpatch_value(v);
if (v >= 0x100) {
printf("*** backpatch value does not fit ***\n");
backpatch_error_flag = TRUE;
}
sf_put((v) & 0xFF);
size += 1;
break;
default:
printf("*** unknown backpatch data len = %d ***\n",
data_len);
backpatch_error_flag = TRUE;
}
if (j > next_cons_check)
compiler_error("Backpatch appears to straddle function break");
if (backpatch_error_flag) {
printf("*** %d bytes zcode offset=%08lx backpatch offset=%08lx ***\n",
data_len, (long int) j, (long int) i);
}
}
/* Flush out the last bit of zcode_area, after the last backpatch
marker. */
offset = zmachine_pc;
while (j<offset) {
if (!use_function) {
while (j<offset && j<next_cons_check) {
j++;
}
}
else {
while (j<offset && j<next_cons_check) {
size++;
sf_put(zcode_area[j]);
j++;
}
}
if (j == next_cons_check)
next_cons_check = df_next_function_iterate(&use_function);
}
if (size_before_code + code_length != size)
compiler_error("Code output length did not match");
/* (4) Output the static strings area. */
{
int32 ix, lx;
int ch, jx, curbyte, bx;
int depth, checkcount;
huffbitlist_t *bits;
int32 origsize;
origsize = size;
if (compression_switch) {
/* The 12-byte table header. */
lx = compression_table_size;
sf_put((lx >> 24) & 0xFF);
sf_put((lx >> 16) & 0xFF);
sf_put((lx >> 8) & 0xFF);
sf_put((lx) & 0xFF);
size += 4;
sf_put((no_huff_entities >> 24) & 0xFF);
sf_put((no_huff_entities >> 16) & 0xFF);
sf_put((no_huff_entities >> 8) & 0xFF);
sf_put((no_huff_entities) & 0xFF);
size += 4;
lx = Write_Strings_At + 12;
sf_put((lx >> 24) & 0xFF);
sf_put((lx >> 16) & 0xFF);
sf_put((lx >> 8) & 0xFF);
sf_put((lx) & 0xFF);
size += 4;
checkcount = 0;
output_compression(huff_entity_root, &size, &checkcount);
if (checkcount != no_huff_entities)
compiler_error("Compression table count mismatch.");
}
if ((int32)size - origsize != compression_table_size)
compiler_error("Compression table size mismatch.");
origsize = size;
for (lx=0, ix=0; lx<no_strings; lx++) {
int escapelen=0, escapetype=0;
int done=FALSE;
int32 escapeval=0;
if (compression_switch)
sf_put(0xE1); /* type byte -- compressed string */
else
sf_put(0xE0); /* type byte -- non-compressed string */
size++;
jx = 0;
curbyte = 0;
while (!done) {
ch = static_strings_area[ix];
ix++;
if (ix > static_strings_extent || ch < 0)
compiler_error("Read too much not-yet-compressed text.");
if (escapelen == -1) {
escapelen = 0;
if (ch == '@') {
ch = '@';
}
else if (ch == '0') {
ch = '\0';
}
else if (ch == 'A' || ch == 'D' || ch == 'U') {
escapelen = 4;
escapetype = ch;
escapeval = 0;
continue;
}
else {
compiler_error("Strange @ escape in processed text.");
}
}
else if (escapelen) {
escapeval = (escapeval << 4) | ((ch-'A') & 0x0F);
escapelen--;
if (escapelen == 0) {
if (escapetype == 'A') {
ch = huff_abbrev_start+escapeval;
}
else if (escapetype == 'D') {
ch = huff_dynam_start+escapeval;
}
else if (escapetype == 'U') {
ch = huff_unicode_start+escapeval;
}
else {
compiler_error("Strange @ escape in processed text.");
}
}
else
continue;
}