1 /* ------------------------------------------------------------------------- */
2 /* "arrays" : Parses array declarations and constructs arrays from them; */
3 /* likewise global variables, which are in some ways a */
4 /* simpler form of the same thing. */
6 /* Copyright (c) Graham Nelson 1993 - 2020 */
8 /* This file is part of Inform. */
10 /* Inform is free software: you can redistribute it and/or modify */
11 /* it under the terms of the GNU General Public License as published by */
12 /* the Free Software Foundation, either version 3 of the License, or */
13 /* (at your option) any later version. */
15 /* Inform is distributed in the hope that it will be useful, */
16 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
17 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
18 /* GNU General Public License for more details. */
20 /* You should have received a copy of the GNU General Public License */
21 /* along with Inform. If not, see https://gnu.org/licenses/ */
23 /* ------------------------------------------------------------------------- */
27 /* ------------------------------------------------------------------------- */
28 /* Arrays defined below: */
30 /* int dynamic_array_area[] Initial values for the bytes of */
31 /* the dynamic array area */
32 /* int static_array_area[] Initial values for the bytes of */
33 /* the static array area */
34 /* int32 global_initial_value[n] The initialised value of the nth */
35 /* global variable (counting 0 - 239) */
37 /* The "dynamic array area" is the Z-machine area holding the current */
38 /* values of the global variables (in 240x2 = 480 bytes) followed by any */
39 /* (dynamic) arrays which may be defined. Owing to a poor choice of name */
40 /* some years ago, this is also called the "static data area", which is */
41 /* why the memory setting for its maximum extent is "MAX_STATIC_DATA". */
43 /* In Glulx, that 240 is changed to MAX_GLOBAL_VAR_NUMBER, and we take */
44 /* correspondingly more space for the globals. This *really* ought to be */
45 /* split into two segments. */
47 /* We can also store arrays (but not globals) into static memory (ROM). */
48 /* The storage for these goes, unsurprisingly, into static_array_area -- */
49 /* a separate allocation of MAX_STATIC_DATA bytes. */
50 /* ------------------------------------------------------------------------- */
51 int *dynamic_array_area; /* See above */
52 int32 *global_initial_value;
54 int no_globals; /* Number of global variables used
55 by the programmer (Inform itself
56 uses the top seven -- but these do
58 /* In Glulx, Inform uses the bottom
61 int dynamic_array_area_size; /* Size in bytes */
63 int *static_array_area;
64 int static_array_area_size;
68 int *array_sizes, *array_types, *array_locs;
69 /* array_sizes[N] gives the length of array N; array_types[N] is one of
70 the constants BYTE_ARRAY, WORD_ARRAY, etc; array_locs[N] is true for
71 static arrays, false for dynamic arrays. */
73 static int array_entry_size, /* 1 for byte array, 2 for word array */
74 array_base; /* Offset in dynamic array area of the
75 array being constructed. During the
76 same time, dynamic_array_area_size
77 is the offset of the initial entry
78 in the array: so for "table" and
79 "string" arrays, these numbers are
80 different (by 2 and 1 bytes resp) */
82 /* In Glulx, of course, that will be
85 extern void finish_array(int32 i, int is_static)
91 area = dynamic_array_area;
92 area_size = dynamic_array_area_size;
95 area = static_array_area;
96 area_size = static_array_area_size;
99 /* Write the array size into the 0th byte/word of the array, if it's
100 a "table" or "string" array */
103 if (array_base != area_size)
104 { if (area_size-array_base==2)
105 { area[array_base] = i/256;
106 area[array_base+1] = i%256;
110 error("A 'string' array can have at most 256 entries");
111 area[array_base] = i;
117 if (array_base != area_size)
118 { if (area_size-array_base==4)
120 area[array_base] = (i >> 24) & 0xFF;
121 area[array_base+1] = (i >> 16) & 0xFF;
122 area[array_base+2] = (i >> 8) & 0xFF;
123 area[array_base+3] = (i) & 0xFF;
127 error("A 'string' array can have at most 256 entries");
128 area[array_base] = i;
134 /* Move on the static/dynamic array size so that it now points to the
135 next available free space */
138 dynamic_array_area_size += i*array_entry_size;
141 static_array_area_size += i*array_entry_size;
146 extern void array_entry(int32 i, int is_static, assembly_operand VAL)
152 area = dynamic_array_area;
153 area_size = dynamic_array_area_size;
156 area = static_array_area;
157 area_size = static_array_area_size;
161 /* Array entry i (initial entry has i=0) is set to Z-machine value j */
163 if (area_size+(i+1)*array_entry_size > MAX_STATIC_DATA)
164 memoryerror("MAX_STATIC_DATA", MAX_STATIC_DATA);
166 if (array_entry_size==1)
167 { area[area_size+i] = (VAL.value)%256;
170 error("Entries in byte arrays and strings must be known constants");
172 /* If the entry is too large for a byte array, issue a warning
173 and truncate the value */
175 if (VAL.value >= 256)
176 warning("Entry in '->', 'string' or 'buffer' array not in range 0 to 255");
180 int32 addr = area_size + 2*i;
181 area[addr] = (VAL.value)/256;
182 area[addr+1] = (VAL.value)%256;
183 if (VAL.marker != 0) {
185 backpatch_zmachine(VAL.marker, DYNAMIC_ARRAY_ZA,
189 backpatch_zmachine(VAL.marker, STATIC_ARRAY_ZA,
196 /* Array entry i (initial entry has i=0) is set to value j */
198 if (area_size+(i+1)*array_entry_size > MAX_STATIC_DATA)
199 memoryerror("MAX_STATIC_DATA", MAX_STATIC_DATA);
201 if (array_entry_size==1)
202 { area[area_size+i] = (VAL.value) & 0xFF;
205 error("Entries in byte arrays and strings must be known constants");
207 /* If the entry is too large for a byte array, issue a warning
208 and truncate the value */
210 if (VAL.value >= 256)
211 warning("Entry in '->', 'string' or 'buffer' array not in range 0 to 255");
213 else if (array_entry_size==4)
215 int32 addr = area_size + 4*i;
216 area[addr] = (VAL.value >> 24) & 0xFF;
217 area[addr+1] = (VAL.value >> 16) & 0xFF;
218 area[addr+2] = (VAL.value >> 8) & 0xFF;
219 area[addr+3] = (VAL.value) & 0xFF;
220 if (VAL.marker != 0) {
222 backpatch_zmachine(VAL.marker, DYNAMIC_ARRAY_ZA,
223 addr - 4*MAX_GLOBAL_VARIABLES);
226 /* We can't use backpatch_zmachine() because that only applies to RAM. Instead we add an entry to staticarray_backpatch_table.
227 A backpatch entry is five bytes: *_MV followed by the array offset (in static array area). */
228 write_byte_to_memory_block(&staticarray_backpatch_table,
229 staticarray_backpatch_size++,
231 write_byte_to_memory_block(&staticarray_backpatch_table,
232 staticarray_backpatch_size++, ((addr >> 24) & 0xFF));
233 write_byte_to_memory_block(&staticarray_backpatch_table,
234 staticarray_backpatch_size++, ((addr >> 16) & 0xFF));
235 write_byte_to_memory_block(&staticarray_backpatch_table,
236 staticarray_backpatch_size++, ((addr >> 8) & 0xFF));
237 write_byte_to_memory_block(&staticarray_backpatch_table,
238 staticarray_backpatch_size++, (addr & 0xFF));
244 error("Somehow created an array of shorts");
249 /* ------------------------------------------------------------------------- */
250 /* Global and Array directives. */
252 /* Global <variablename> | */
254 /* | <array specification> */
256 /* Array <arrayname> [static] <array specification> */
258 /* where an array specification is: */
260 /* | -> | <number-of-entries> */
261 /* | --> | <entry-1> ... <entry-n> */
262 /* | string | [ <entry-1> [,] [;] <entry-2> ... <entry-n> ]; */
266 /* The "static" keyword (arrays only) places the array in static memory. */
268 /* ------------------------------------------------------------------------- */
270 extern void set_variable_value(int i, int32 v)
271 { global_initial_value[i]=v;
274 /* There are four ways to initialise arrays: */
276 #define UNSPECIFIED_AI -1
282 extern void make_global(int array_flag, int name_only)
284 /* array_flag is TRUE for an Array directive, FALSE for a Global;
285 name_only is only TRUE for parsing an imported variable name, so
286 array_flag is always FALSE in that case. */
289 int array_type, data_type;
290 int is_static = FALSE;
294 const char *global_name;
295 debug_location_beginning beginning_debug_location =
296 get_token_location_beginning();
298 directive_keywords.enabled = FALSE;
302 global_name = token_text;
305 if ((token_type==SYMBOL_TT) && (stypes[i]==GLOBAL_VARIABLE_T)
306 && (svals[i] >= LOWEST_SYSTEM_VAR_NUMBER))
307 goto RedefinitionOfSystemVar;
310 if ((token_type==SYMBOL_TT) && (stypes[i]==GLOBAL_VARIABLE_T))
311 goto RedefinitionOfSystemVar;
314 if ((token_type != SYMBOL_TT) || (!(sflags[i] & UNKNOWN_SFLAG)))
315 { discard_token_location(beginning_debug_location);
317 ebf_error("new array name", token_text);
318 else ebf_error("new global variable name", token_text);
319 panic_mode_error_recovery(); return;
322 if ((!array_flag) && (sflags[i] & USED_SFLAG))
323 error_named("Variable must be defined before use:", token_text);
325 directive_keywords.enabled = TRUE;
327 directive_keywords.enabled = FALSE;
328 if ((token_type==DIR_KEYWORD_TT)&&(token_value==STATIC_DK)) {
333 error("Global variables cannot be static");
343 assign_symbol(i, dynamic_array_area_size, ARRAY_T);
346 dynamic_array_area_size - 4*MAX_GLOBAL_VARIABLES, ARRAY_T);
349 assign_symbol(i, static_array_area_size, STATIC_ARRAY_T);
351 if (no_arrays == MAX_ARRAYS)
352 memoryerror("MAX_ARRAYS", MAX_ARRAYS);
353 array_symbols[no_arrays] = i;
356 { if (!glulx_mode && no_globals==233)
357 { discard_token_location(beginning_debug_location);
358 error("All 233 global variables already declared");
359 panic_mode_error_recovery();
362 if (glulx_mode && no_globals==MAX_GLOBAL_VARIABLES)
363 { discard_token_location(beginning_debug_location);
364 memoryerror("MAX_GLOBAL_VARIABLES", MAX_GLOBAL_VARIABLES);
365 panic_mode_error_recovery();
369 variable_tokens[MAX_LOCAL_VARIABLES+no_globals] = i;
370 assign_symbol(i, MAX_LOCAL_VARIABLES+no_globals, GLOBAL_VARIABLE_T);
371 variable_tokens[svals[i]] = i;
373 if (name_only) import_symbol(i);
374 else global_initial_value[no_globals++]=0;
377 directive_keywords.enabled = TRUE;
379 RedefinitionOfSystemVar:
382 { discard_token_location(beginning_debug_location);
388 if ((token_type == SEP_TT) && (token_value == SEMICOLON_SEP))
390 { discard_token_location(beginning_debug_location);
391 ebf_error("array definition", token_text);
394 if (debugfile_switch && !array_flag)
395 { debug_file_printf("<global-variable>");
396 debug_file_printf("<identifier>%s</identifier>", global_name);
397 debug_file_printf("<address>");
398 write_debug_global_backpatch(svals[global_symbol]);
399 debug_file_printf("</address>");
400 write_debug_locations
401 (get_token_location_end(beginning_debug_location));
402 debug_file_printf("</global-variable>");
409 /* is_static is always false in this case */
410 if ((token_type == SEP_TT) && (token_value == SETEQUALS_SEP))
411 { AO = parse_expression(CONSTANT_CONTEXT);
414 backpatch_zmachine(AO.marker, DYNAMIC_ARRAY_ZA,
419 backpatch_zmachine(AO.marker, GLOBALVAR_ZA,
422 global_initial_value[no_globals-1] = AO.value;
423 if (debugfile_switch)
424 { debug_file_printf("<global-variable>");
425 debug_file_printf("<identifier>%s</identifier>", global_name);
426 debug_file_printf("<address>");
427 write_debug_global_backpatch(svals[global_symbol]);
428 debug_file_printf("</address>");
429 write_debug_locations
430 (get_token_location_end(beginning_debug_location));
431 debug_file_printf("</global-variable>");
436 obsolete_warning("more modern to use 'Array', not 'Global'");
439 backpatch_zmachine(ARRAY_MV, DYNAMIC_ARRAY_ZA, 2*(no_globals-1));
440 global_initial_value[no_globals-1]
441 = dynamic_array_area_size+variables_offset;
444 backpatch_zmachine(ARRAY_MV, GLOBALVAR_ZA, 4*(no_globals-1));
445 global_initial_value[no_globals-1]
446 = dynamic_array_area_size - 4*MAX_GLOBAL_VARIABLES;
450 array_type = BYTE_ARRAY; data_type = UNSPECIFIED_AI;
453 ((token_type==DIR_KEYWORD_TT)&&(token_value==DATA_DK)))
455 else if ((!array_flag) &&
456 ((token_type==DIR_KEYWORD_TT)&&(token_value==INITIAL_DK)))
458 else if ((!array_flag) &&
459 ((token_type==DIR_KEYWORD_TT)&&(token_value==INITSTR_DK)))
462 else if ((token_type==SEP_TT)&&(token_value==ARROW_SEP))
463 array_type = BYTE_ARRAY;
464 else if ((token_type==SEP_TT)&&(token_value==DARROW_SEP))
465 array_type = WORD_ARRAY;
466 else if ((token_type==DIR_KEYWORD_TT)&&(token_value==STRING_DK))
467 array_type = STRING_ARRAY;
468 else if ((token_type==DIR_KEYWORD_TT)&&(token_value==TABLE_DK))
469 array_type = TABLE_ARRAY;
470 else if ((token_type==DIR_KEYWORD_TT)&&(token_value==BUFFER_DK))
471 array_type = BUFFER_ARRAY;
473 { discard_token_location(beginning_debug_location);
476 ("'->', '-->', 'string', 'table' or 'buffer'", token_text);
479 ("'=', '->', '-->', 'string', 'table' or 'buffer'", token_text);
480 panic_mode_error_recovery();
485 if ((array_type==WORD_ARRAY) || (array_type==TABLE_ARRAY))
486 array_entry_size=WORDSIZE;
489 if ((token_type == SEP_TT) && (token_value == SEMICOLON_SEP))
490 { discard_token_location(beginning_debug_location);
491 error("No array size or initial values given");
497 { case UNSPECIFIED_AI:
498 if ((token_type == SEP_TT) && (token_value == OPEN_SQUARE_SEP))
499 data_type = BRACKET_AI;
501 { data_type = NULLS_AI;
502 if (token_type == DQ_TT) data_type = ASCII_AI;
504 if (!((token_type == SEP_TT) && (token_value == SEMICOLON_SEP)))
510 case NULLS_AI: obsolete_warning("use '->' instead of 'data'"); break;
511 case DATA_AI: obsolete_warning("use '->' instead of 'initial'"); break;
512 case ASCII_AI: obsolete_warning("use '->' instead of 'initstr'"); break;
515 /* Leave room to write the array size in later, if string/table array */
518 if ((array_type==STRING_ARRAY) || (array_type==TABLE_ARRAY))
519 extraspace += array_entry_size;
520 if (array_type==BUFFER_ARRAY)
521 extraspace += WORDSIZE;
526 orig_area_size = dynamic_array_area_size;
527 array_base = dynamic_array_area_size;
528 dynamic_array_area_size += extraspace;
531 orig_area_size = static_array_area_size;
532 array_base = static_array_area_size;
533 static_array_area_size += extraspace;
536 array_types[no_arrays] = array_type;
537 array_locs[no_arrays] = is_static;
543 AO = parse_expression(CONSTANT_CONTEXT);
547 if (module_switch && (AO.marker != 0))
548 { error("Array sizes must be known now, not externally defined");
553 if ((AO.value <= 0) || (AO.value >= 32768))
554 { error("An array must have between 1 and 32767 entries");
559 if (AO.value <= 0 || (AO.value & 0x80000000))
560 { error("An array may not have 0 or fewer entries");
565 { for (i=0; i<AO.value; i++) array_entry(i, is_static, zero_operand);
571 /* In this case the array is initialised to the sequence of
572 constant values supplied on the same line */
577 if ((token_type == SEP_TT) && (token_value == SEMICOLON_SEP))
580 if ((token_type == SEP_TT)
581 && ((token_value == OPEN_SQUARE_SEP)
582 || (token_value == CLOSE_SQUARE_SEP)))
583 { discard_token_location(beginning_debug_location);
584 error("Missing ';' to end the initial array values "
585 "before \"[\" or \"]\"");
590 AO = parse_expression(ARRAY_CONTEXT);
595 if ((token_type == SEP_TT)
596 && (token_value == SEMICOLON_SEP))
597 { data_type = NULLS_AI;
598 goto CalculatedArraySize;
602 array_entry(i, is_static, AO);
610 /* In this case the array is initialised to the ASCII values of
611 the characters of a given "quoted string" */
614 if (token_type != DQ_TT)
615 { ebf_error("literal text in double-quotes", token_text);
616 token_text = "error";
619 { assembly_operand chars;
623 for (i=0,j=0; token_text[j]!=0; i++,j+=textual_form_length)
625 int32 unicode; int zscii;
626 unicode = text_to_unicode(token_text+j);
629 if (array_entry_size == 1 && (unicode < 0 || unicode >= 256))
631 error("Unicode characters beyond Latin-1 cannot be used in a byte array");
635 chars.value = unicode;
640 zscii = unicode_to_zscii(unicode);
641 if ((zscii != 5) && (zscii < 0x100)) chars.value = zscii;
643 { unicode_char_error("Character can only be used if declared in \
644 advance as part of 'Zcharacter table':", unicode);
649 set_constant_ot(&chars);
650 array_entry(i, is_static, chars);
657 /* In this case the array is initialised to the sequence of
658 constant values given over a whole range of compiler-lines,
659 between square brackets [ and ] */
664 if ((token_type == SEP_TT) && (token_value == SEMICOLON_SEP))
666 if ((token_type == SEP_TT) && (token_value == CLOSE_SQUARE_SEP))
668 if ((token_type == SEP_TT) && (token_value == OPEN_SQUARE_SEP))
669 { /* Minimal error recovery: we assume that a ] has
670 been missed, and the programmer is now starting
673 ebf_error("']'", token_text);
674 put_token_back(); break;
677 array_entry(i, is_static, parse_expression(ARRAY_CONTEXT));
682 finish_array(i, is_static);
684 if (debugfile_switch)
685 { debug_file_printf("<array>");
686 debug_file_printf("<identifier>%s</identifier>", global_name);
687 debug_file_printf("<value>");
688 write_debug_array_backpatch(svals[global_symbol]);
689 debug_file_printf("</value>");
690 int32 new_area_size = (!is_static ? dynamic_array_area_size : static_array_area_size);
692 ("<byte-count>%d</byte-count>",
693 new_area_size - array_base);
695 ("<bytes-per-element>%d</bytes-per-element>",
698 ("<zeroth-element-holds-length>%s</zeroth-element-holds-length>",
699 (array_type == STRING_ARRAY || array_type == TABLE_ARRAY) ?
702 write_debug_locations(get_token_location_end(beginning_debug_location));
704 debug_file_printf("</array>");
707 if ((array_type==BYTE_ARRAY) || (array_type==WORD_ARRAY)) i--;
708 if (array_type==BUFFER_ARRAY) i+=WORDSIZE-1;
709 array_sizes[no_arrays++] = i;
712 extern int32 begin_table_array(void)
714 /* The "box" statement needs to be able to construct table
715 arrays of strings like this. (Static data, but we create a dynamic
716 array for maximum backwards compatibility.) */
718 array_base = dynamic_array_area_size;
719 array_entry_size = WORDSIZE;
721 /* Leave room to write the array size in later */
723 dynamic_array_area_size += array_entry_size;
728 return array_base - WORDSIZE * MAX_GLOBAL_VARIABLES;
731 extern int32 begin_word_array(void)
733 /* The "random(a, b, ...)" function needs to be able to construct
734 word arrays like this. (Static data, but we create a dynamic
735 array for maximum backwards compatibility.) */
737 array_base = dynamic_array_area_size;
738 array_entry_size = WORDSIZE;
743 return array_base - WORDSIZE * MAX_GLOBAL_VARIABLES;
746 /* ========================================================================= */
747 /* Data structure management routines */
748 /* ------------------------------------------------------------------------- */
750 extern void init_arrays_vars(void)
751 { dynamic_array_area = NULL;
752 static_array_area = NULL;
753 global_initial_value = NULL;
754 array_sizes = NULL; array_symbols = NULL; array_types = NULL;
757 extern void arrays_begin_pass(void)
763 dynamic_array_area_size = WORDSIZE * MAX_GLOBAL_VARIABLES;
764 static_array_area_size = 0;
767 extern void arrays_allocate_arrays(void)
768 { dynamic_array_area = my_calloc(sizeof(int), MAX_STATIC_DATA,
769 "dynamic array data");
770 static_array_area = my_calloc(sizeof(int), MAX_STATIC_DATA,
771 "static array data");
772 array_sizes = my_calloc(sizeof(int), MAX_ARRAYS, "array sizes");
773 array_types = my_calloc(sizeof(int), MAX_ARRAYS, "array types");
774 array_locs = my_calloc(sizeof(int), MAX_ARRAYS, "array locations");
775 array_symbols = my_calloc(sizeof(int32), MAX_ARRAYS, "array symbols");
776 global_initial_value = my_calloc(sizeof(int32), MAX_GLOBAL_VARIABLES,
780 extern void arrays_free_arrays(void)
781 { my_free(&dynamic_array_area, "dynamic array data");
782 my_free(&static_array_area, "static array data");
783 my_free(&global_initial_value, "global values");
784 my_free(&array_sizes, "array sizes");
785 my_free(&array_types, "array types");
786 my_free(&array_locs, "array locations");
787 my_free(&array_symbols, "array sizes");
790 /* ========================================================================= */