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 /* Part of Inform 6.40 */
7 /* copyright (c) Graham Nelson 1993 - 2022 */
9 /* Inform is free software: you can redistribute it and/or modify */
10 /* it under the terms of the GNU General Public License as published by */
11 /* the Free Software Foundation, either version 3 of the License, or */
12 /* (at your option) any later version. */
14 /* Inform is distributed in the hope that it will be useful, */
15 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
16 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
17 /* GNU General Public License for more details. */
19 /* You should have received a copy of the GNU General Public License */
20 /* along with Inform. If not, see https://gnu.org/licenses/ */
22 /* ------------------------------------------------------------------------- */
26 /* ------------------------------------------------------------------------- */
27 /* Arrays defined below: */
29 /* uchar dynamic_array_area[] Initial values for the bytes of */
30 /* the dynamic array area */
31 /* uchar static_array_area[] Initial values for the bytes of */
32 /* the static array area */
33 /* int32 global_initial_value[n] The initialised value of the nth */
34 /* global variable (counting 0 - 239, */
35 /* or higher for Glulx) */
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. */
41 /* In Glulx, we don't keep the global variables in dynamic_array_area. */
42 /* Array data starts at the start. */
44 /* We can also store arrays (but not globals) into static memory (ROM). */
45 /* The storage for these goes, unsurprisingly, into static_array_area. */
46 /* ------------------------------------------------------------------------- */
47 uchar *dynamic_array_area; /* See above */
48 memory_list dynamic_array_area_memlist;
49 int dynamic_array_area_size; /* Size in bytes */
51 int32 *global_initial_value; /* Allocated to no_globals */
52 static memory_list global_initial_value_memlist;
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 uchar *static_array_area;
62 memory_list static_array_area_memlist;
63 int static_array_area_size;
67 static memory_list arrays_memlist;
69 static int array_entry_size, /* 1 for byte array, 2 for word array */
70 array_base; /* Offset in dynamic array area of the
71 array being constructed. During the
72 same time, dynamic_array_area_size
73 is the offset of the initial entry
74 in the array: so for "table" and
75 "string" arrays, these numbers are
76 different (by 2 and 1 bytes resp) */
78 /* In Glulx, of course, that will be
81 static memory_list current_array_name; /* The name of the global or array
82 currently being compiled. */
84 /* Complete the array. Fill in the size field (if it has one) and
85 advance foo_array_area_size.
87 extern void finish_array(int32 i, int is_static)
93 ensure_memory_list_available(&dynamic_array_area_memlist, dynamic_array_area_size+array_base+1*array_entry_size);
94 area = dynamic_array_area;
95 area_size = dynamic_array_area_size;
98 ensure_memory_list_available(&static_array_area_memlist, static_array_area_size+array_base+1*array_entry_size);
99 area = static_array_area;
100 area_size = static_array_area_size;
104 error("An array must have at least one entry");
107 /* Write the array size into the 0th byte/word of the array, if it's
108 a "table" or "string" array */
111 if (array_base != area_size)
112 { if (area_size-array_base==2)
113 { area[array_base] = i/256;
114 area[array_base+1] = i%256;
118 error("A 'string' array can have at most 256 entries");
119 area[array_base] = i;
125 if (array_base != area_size)
126 { if (area_size-array_base==4)
128 area[array_base] = (i >> 24) & 0xFF;
129 area[array_base+1] = (i >> 16) & 0xFF;
130 area[array_base+2] = (i >> 8) & 0xFF;
131 area[array_base+3] = (i) & 0xFF;
135 error("A 'string' array can have at most 256 entries");
136 area[array_base] = i;
142 /* Move on the static/dynamic array size so that it now points to the
143 next available free space */
146 dynamic_array_area_size += i*array_entry_size;
149 static_array_area_size += i*array_entry_size;
154 /* Fill in array entry i (in either the static or dynamic area).
155 When this is called, foo_array_area_size is the end of the previous
156 array; we're writing after that.
158 extern void array_entry(int32 i, int is_static, assembly_operand VAL)
164 ensure_memory_list_available(&dynamic_array_area_memlist, dynamic_array_area_size+(i+1)*array_entry_size);
165 area = dynamic_array_area;
166 area_size = dynamic_array_area_size;
169 ensure_memory_list_available(&static_array_area_memlist, static_array_area_size+(i+1)*array_entry_size);
170 area = static_array_area;
171 area_size = static_array_area_size;
175 /* Array entry i (initial entry has i=0) is set to Z-machine value j */
177 if (array_entry_size==1)
178 { area[area_size+i] = (VAL.value)%256;
181 error("Entries in byte arrays and strings must be known constants");
183 /* If the entry is too large for a byte array, issue a warning
184 and truncate the value */
186 if (VAL.value >= 256)
187 warning("Entry in '->', 'string' or 'buffer' array not in range 0 to 255");
191 int32 addr = area_size + 2*i;
192 area[addr] = (VAL.value)/256;
193 area[addr+1] = (VAL.value)%256;
194 if (VAL.marker != 0) {
196 backpatch_zmachine(VAL.marker, DYNAMIC_ARRAY_ZA,
200 backpatch_zmachine(VAL.marker, STATIC_ARRAY_ZA,
207 /* Array entry i (initial entry has i=0) is set to value j */
209 if (array_entry_size==1)
210 { area[area_size+i] = (VAL.value) & 0xFF;
213 error("Entries in byte arrays and strings must be known constants");
215 /* If the entry is too large for a byte array, issue a warning
216 and truncate the value */
218 if (VAL.value >= 256)
219 warning("Entry in '->', 'string' or 'buffer' array not in range 0 to 255");
221 else if (array_entry_size==4)
223 int32 addr = area_size + 4*i;
224 area[addr] = (VAL.value >> 24) & 0xFF;
225 area[addr+1] = (VAL.value >> 16) & 0xFF;
226 area[addr+2] = (VAL.value >> 8) & 0xFF;
227 area[addr+3] = (VAL.value) & 0xFF;
228 if (VAL.marker != 0) {
230 backpatch_zmachine(VAL.marker, DYNAMIC_ARRAY_ZA,
234 /* We can't use backpatch_zmachine() because that only applies to RAM. Instead we add an entry to staticarray_backpatch_table.
235 A backpatch entry is five bytes: *_MV followed by the array offset (in static array area). */
236 if (bpatch_trace_setting >= 2)
237 printf("BP added: MV %d staticarray %04x\n", VAL.marker, addr);
238 ensure_memory_list_available(&staticarray_backpatch_table_memlist, staticarray_backpatch_size+5);
239 staticarray_backpatch_table[staticarray_backpatch_size++] = VAL.marker;
240 staticarray_backpatch_table[staticarray_backpatch_size++] = ((addr >> 24) & 0xFF);
241 staticarray_backpatch_table[staticarray_backpatch_size++] = ((addr >> 16) & 0xFF);
242 staticarray_backpatch_table[staticarray_backpatch_size++] = ((addr >> 8) & 0xFF);
243 staticarray_backpatch_table[staticarray_backpatch_size++] = (addr & 0xFF);
249 error("Somehow created an array of shorts");
254 /* ------------------------------------------------------------------------- */
255 /* Global and Array directives. */
257 /* Global <variablename> | */
259 /* | <array specification> */
261 /* Array <arrayname> [static] <array specification> */
263 /* where an array specification is: */
265 /* | -> | <number-of-entries> */
266 /* | --> | <entry-1> ... <entry-n> */
267 /* | string | [ <entry-1> [;] <entry-2> ... <entry-n> ]; */
271 /* The "static" keyword (arrays only) places the array in static memory. */
273 /* ------------------------------------------------------------------------- */
275 extern void set_variable_value(int i, int32 v)
277 /* This can be called during module-load to create a new global,
278 so we call ensure. */
279 ensure_memory_list_available(&global_initial_value_memlist, i+1);
280 global_initial_value[i]=v;
283 /* There are four ways to initialise arrays: */
285 #define UNSPECIFIED_AI -1
291 extern void make_global(int array_flag, int name_only)
293 /* array_flag is TRUE for an Array directive, FALSE for a Global;
294 name_only is only TRUE for parsing an imported variable name, so
295 array_flag is always FALSE in that case. */
299 int array_type, data_type;
300 int is_static = FALSE;
306 debug_location_beginning beginning_debug_location =
307 get_token_location_beginning();
309 directive_keywords.enabled = FALSE;
314 name_length = strlen(token_text) + 1;
315 ensure_memory_list_available(¤t_array_name, name_length);
316 strncpy(current_array_name.data, token_text, name_length);
319 if ((token_type==SYMBOL_TT) && (symbols[i].type==GLOBAL_VARIABLE_T)
320 && (symbols[i].value >= LOWEST_SYSTEM_VAR_NUMBER))
321 goto RedefinitionOfSystemVar;
324 if ((token_type==SYMBOL_TT) && (symbols[i].type==GLOBAL_VARIABLE_T))
325 goto RedefinitionOfSystemVar;
328 if (token_type != SYMBOL_TT)
329 { discard_token_location(beginning_debug_location);
331 ebf_error("new array name", token_text);
332 else ebf_error("new global variable name", token_text);
333 panic_mode_error_recovery(); return;
336 if (!(symbols[i].flags & UNKNOWN_SFLAG))
337 { discard_token_location(beginning_debug_location);
339 ebf_symbol_error("new array name", token_text, typename(symbols[i].type), symbols[i].line);
340 else ebf_symbol_error("new global variable name", token_text, typename(symbols[i].type), symbols[i].line);
341 panic_mode_error_recovery(); return;
344 if ((!array_flag) && (symbols[i].flags & USED_SFLAG))
345 error_named("Variable must be defined before use:", token_text);
347 directive_keywords.enabled = TRUE;
349 directive_keywords.enabled = FALSE;
350 if ((token_type==DIR_KEYWORD_TT)&&(token_value==STATIC_DK)) {
355 error("Global variables cannot be static");
364 assign_symbol(i, dynamic_array_area_size, ARRAY_T);
367 assign_symbol(i, static_array_area_size, STATIC_ARRAY_T);
369 ensure_memory_list_available(&arrays_memlist, no_arrays+1);
370 arrays[no_arrays].symbol = i;
373 { if (!glulx_mode && no_globals==233)
374 { discard_token_location(beginning_debug_location);
375 error("All 233 global variables already declared");
376 panic_mode_error_recovery();
380 ensure_memory_list_available(&variables_memlist, MAX_LOCAL_VARIABLES+no_globals+1);
381 variables[MAX_LOCAL_VARIABLES+no_globals].token = i;
382 variables[MAX_LOCAL_VARIABLES+no_globals].usage = FALSE;
383 assign_symbol(i, MAX_LOCAL_VARIABLES+no_globals, GLOBAL_VARIABLE_T);
389 ensure_memory_list_available(&global_initial_value_memlist, no_globals+1);
390 global_initial_value[no_globals++]=0;
394 directive_keywords.enabled = TRUE;
396 RedefinitionOfSystemVar:
399 { discard_token_location(beginning_debug_location);
405 if ((token_type == SEP_TT) && (token_value == SEMICOLON_SEP))
407 { discard_token_location(beginning_debug_location);
408 ebf_error("array definition", token_text);
411 if (debugfile_switch && !array_flag)
413 char *global_name = current_array_name.data;
414 debug_file_printf("<global-variable>");
415 debug_file_printf("<identifier>%s</identifier>", global_name);
416 debug_file_printf("<address>");
417 write_debug_global_backpatch(symbols[global_symbol].value);
418 debug_file_printf("</address>");
419 write_debug_locations
420 (get_token_location_end(beginning_debug_location));
421 debug_file_printf("</global-variable>");
428 /* is_static is always false in this case */
429 if ((token_type == SEP_TT) && (token_value == SETEQUALS_SEP))
430 { AO = parse_expression(CONSTANT_CONTEXT);
433 backpatch_zmachine(AO.marker, DYNAMIC_ARRAY_ZA,
438 backpatch_zmachine(AO.marker, GLOBALVAR_ZA,
441 global_initial_value[no_globals-1] = AO.value;
442 if (debugfile_switch)
444 char *global_name = current_array_name.data;
445 debug_file_printf("<global-variable>");
446 debug_file_printf("<identifier>%s</identifier>", global_name);
447 debug_file_printf("<address>");
448 write_debug_global_backpatch(symbols[global_symbol].value);
449 debug_file_printf("</address>");
450 write_debug_locations
451 (get_token_location_end(beginning_debug_location));
452 debug_file_printf("</global-variable>");
457 obsolete_warning("more modern to use 'Array', not 'Global'");
460 backpatch_zmachine(ARRAY_MV, DYNAMIC_ARRAY_ZA, 2*(no_globals-1));
461 global_initial_value[no_globals-1]
462 = dynamic_array_area_size+variables_offset;
465 backpatch_zmachine(ARRAY_MV, GLOBALVAR_ZA, 4*(no_globals-1));
466 global_initial_value[no_globals-1]
467 = dynamic_array_area_size;
471 array_type = BYTE_ARRAY; data_type = UNSPECIFIED_AI;
474 ((token_type==DIR_KEYWORD_TT)&&(token_value==DATA_DK)))
476 else if ((!array_flag) &&
477 ((token_type==DIR_KEYWORD_TT)&&(token_value==INITIAL_DK)))
479 else if ((!array_flag) &&
480 ((token_type==DIR_KEYWORD_TT)&&(token_value==INITSTR_DK)))
483 else if ((token_type==SEP_TT)&&(token_value==ARROW_SEP))
484 array_type = BYTE_ARRAY;
485 else if ((token_type==SEP_TT)&&(token_value==DARROW_SEP))
486 array_type = WORD_ARRAY;
487 else if ((token_type==DIR_KEYWORD_TT)&&(token_value==STRING_DK))
488 array_type = STRING_ARRAY;
489 else if ((token_type==DIR_KEYWORD_TT)&&(token_value==TABLE_DK))
490 array_type = TABLE_ARRAY;
491 else if ((token_type==DIR_KEYWORD_TT)&&(token_value==BUFFER_DK))
492 array_type = BUFFER_ARRAY;
494 { discard_token_location(beginning_debug_location);
497 ("'->', '-->', 'string', 'table' or 'buffer'", token_text);
500 ("'=', '->', '-->', 'string', 'table' or 'buffer'", token_text);
501 panic_mode_error_recovery();
506 if ((array_type==WORD_ARRAY) || (array_type==TABLE_ARRAY))
507 array_entry_size=WORDSIZE;
510 if ((token_type == SEP_TT) && (token_value == SEMICOLON_SEP))
511 { discard_token_location(beginning_debug_location);
512 error("No array size or initial values given");
518 { case UNSPECIFIED_AI:
519 if ((token_type == SEP_TT) && (token_value == OPEN_SQUARE_SEP))
520 data_type = BRACKET_AI;
522 { data_type = NULLS_AI;
523 if (token_type == DQ_TT) data_type = ASCII_AI;
525 if (!((token_type == SEP_TT) && (token_value == SEMICOLON_SEP)))
531 case NULLS_AI: obsolete_warning("use '->' instead of 'data'"); break;
532 case DATA_AI: obsolete_warning("use '->' instead of 'initial'"); break;
533 case ASCII_AI: obsolete_warning("use '->' instead of 'initstr'"); break;
536 /* Leave room to write the array size in later, if string/table array */
539 if ((array_type==STRING_ARRAY) || (array_type==TABLE_ARRAY))
540 extraspace += array_entry_size;
541 if (array_type==BUFFER_ARRAY)
542 extraspace += WORDSIZE;
545 array_base = dynamic_array_area_size;
546 dynamic_array_area_size += extraspace;
549 array_base = static_array_area_size;
550 static_array_area_size += extraspace;
553 arrays[no_arrays].type = array_type;
554 arrays[no_arrays].loc = is_static;
556 /* Note that, from this point, we must continue through finish_array().
557 Exiting this routine on error causes problems. */
563 AO = parse_expression(CONSTANT_CONTEXT);
568 { error("Array sizes must be known now, not defined later");
573 if ((AO.value <= 0) || (AO.value >= 32768))
574 { error("An array must have between 1 and 32767 entries");
579 if (AO.value <= 0 || (AO.value & 0x80000000))
580 { error("An array may not have 0 or fewer entries");
585 { for (i=0; i<AO.value; i++) array_entry(i, is_static, zero_operand);
591 /* In this case the array is initialised to the sequence of
592 constant values supplied on the same line */
597 /* This isn't the start of a statement, but it's safe to
598 release token texts anyway. Expressions in an array
599 list are independent of each other. */
600 release_token_texts();
602 if ((token_type == SEP_TT) && (token_value == SEMICOLON_SEP))
605 if ((token_type == SEP_TT)
606 && ((token_value == OPEN_SQUARE_SEP)
607 || (token_value == CLOSE_SQUARE_SEP)))
608 { discard_token_location(beginning_debug_location);
609 error("Missing ';' to end the initial array values "
610 "before \"[\" or \"]\"");
614 AO = parse_expression(ARRAY_CONTEXT);
619 if ((token_type == SEP_TT)
620 && (token_value == SEMICOLON_SEP))
621 { data_type = NULLS_AI;
622 goto CalculatedArraySize;
626 array_entry(i, is_static, AO);
634 /* In this case the array is initialised to the ASCII values of
635 the characters of a given "quoted string" */
638 if (token_type != DQ_TT)
639 { ebf_error("literal text in double-quotes", token_text);
640 token_text = "error";
643 { assembly_operand chars;
647 for (i=0,j=0; token_text[j]!=0; i++,j+=textual_form_length)
649 int32 unicode; int zscii;
650 unicode = text_to_unicode(token_text+j);
653 if (array_entry_size == 1 && (unicode < 0 || unicode >= 256))
655 error("Unicode characters beyond Latin-1 cannot be used in a byte array");
659 chars.value = unicode;
664 zscii = unicode_to_zscii(unicode);
665 if ((zscii != 5) && (zscii < 0x100)) chars.value = zscii;
667 { unicode_char_error("Character can only be used if declared in \
668 advance as part of 'Zcharacter table':", unicode);
673 set_constant_ot(&chars);
674 array_entry(i, is_static, chars);
681 /* In this case the array is initialised to the sequence of
682 constant values given over a whole range of compiler-lines,
683 between square brackets [ and ] */
688 /* This isn't the start of a statement, but it's safe to
689 release token texts anyway. Expressions in an array
690 list are independent of each other. */
691 release_token_texts();
693 if ((token_type == SEP_TT) && (token_value == SEMICOLON_SEP))
695 if ((token_type == SEP_TT) && (token_value == CLOSE_SQUARE_SEP))
697 if ((token_type == SEP_TT) && (token_value == OPEN_SQUARE_SEP))
698 { /* Minimal error recovery: we assume that a ] has
699 been missed, and the programmer is now starting
702 ebf_error("']'", token_text);
703 put_token_back(); break;
706 array_entry(i, is_static, parse_expression(ARRAY_CONTEXT));
711 finish_array(i, is_static);
713 if (debugfile_switch)
716 char *global_name = current_array_name.data;
717 debug_file_printf("<array>");
718 debug_file_printf("<identifier>%s</identifier>", global_name);
719 debug_file_printf("<value>");
720 write_debug_array_backpatch(symbols[global_symbol].value);
721 debug_file_printf("</value>");
722 new_area_size = (!is_static ? dynamic_array_area_size : static_array_area_size);
724 ("<byte-count>%d</byte-count>",
725 new_area_size - array_base);
727 ("<bytes-per-element>%d</bytes-per-element>",
730 ("<zeroth-element-holds-length>%s</zeroth-element-holds-length>",
731 (array_type == STRING_ARRAY || array_type == TABLE_ARRAY) ?
734 write_debug_locations(get_token_location_end(beginning_debug_location));
736 debug_file_printf("</array>");
739 if ((array_type==BYTE_ARRAY) || (array_type==WORD_ARRAY)) i--;
740 if (array_type==BUFFER_ARRAY) i+=WORDSIZE-1;
741 arrays[no_arrays++].size = i;
744 extern int32 begin_table_array(void)
746 /* The "box" statement needs to be able to construct table
747 arrays of strings like this. (Static data, but we create a dynamic
748 array for maximum backwards compatibility.) */
750 array_base = dynamic_array_area_size;
751 array_entry_size = WORDSIZE;
753 /* Leave room to write the array size in later */
755 dynamic_array_area_size += array_entry_size;
760 extern int32 begin_word_array(void)
762 /* The "random(a, b, ...)" function needs to be able to construct
763 word arrays like this. (Static data, but we create a dynamic
764 array for maximum backwards compatibility.) */
766 array_base = dynamic_array_area_size;
767 array_entry_size = WORDSIZE;
772 /* ========================================================================= */
773 /* Data structure management routines */
774 /* ------------------------------------------------------------------------- */
776 extern void init_arrays_vars(void)
777 { dynamic_array_area = NULL;
778 static_array_area = NULL;
780 global_initial_value = NULL;
784 extern void arrays_begin_pass(void)
791 /* The compiler-defined globals start at 239 and go down, so
792 we need to initialize the entire list from the start. */
793 totalvar = MAX_ZCODE_GLOBAL_VARS;
796 /* The compiler-defined globals run from 0 to 10. */
798 totalvar = no_globals;
801 ensure_memory_list_available(&global_initial_value_memlist, totalvar);
802 for (ix=0; ix<totalvar; ix++) {
803 global_initial_value[ix] = 0;
806 ensure_memory_list_available(&variables_memlist, MAX_LOCAL_VARIABLES+totalvar);
807 for (ix=0; ix<MAX_LOCAL_VARIABLES+totalvar; ix++) {
808 variables[ix].token = 0;
809 variables[ix].usage = FALSE;
812 dynamic_array_area_size = 0;
816 /* This initial segment of dynamic_array_area is never used. It's
817 notionally space for the global variables, but that data is
818 kept in the global_initial_value array. Nonetheless, all the
819 Z-compiler math is set up with the idea that arrays start at
820 WORDSIZE * MAX_ZCODE_GLOBAL_VARS, so we need the blank segment.
822 dynamic_array_area_size = WORDSIZE * MAX_ZCODE_GLOBAL_VARS;
823 ensure_memory_list_available(&dynamic_array_area_memlist, dynamic_array_area_size);
824 for (ix=0; ix<WORDSIZE * MAX_ZCODE_GLOBAL_VARS; ix++)
825 dynamic_array_area[ix] = 0;
828 static_array_area_size = 0;
831 extern void arrays_allocate_arrays(void)
833 initialise_memory_list(&dynamic_array_area_memlist,
834 sizeof(uchar), 10000, (void**)&dynamic_array_area,
835 "dynamic array data");
836 initialise_memory_list(&static_array_area_memlist,
837 sizeof(uchar), 0, (void**)&static_array_area,
838 "static array data");
839 initialise_memory_list(&arrays_memlist,
840 sizeof(arrayinfo), 64, (void**)&arrays,
842 initialise_memory_list(&global_initial_value_memlist,
843 sizeof(int32), 200, (void**)&global_initial_value,
844 "global variable values");
846 initialise_memory_list(¤t_array_name,
847 sizeof(char), MAX_IDENTIFIER_LENGTH+1, NULL,
848 "array name currently being defined");
851 extern void arrays_free_arrays(void)
853 deallocate_memory_list(&dynamic_array_area_memlist);
854 deallocate_memory_list(&static_array_area_memlist);
855 deallocate_memory_list(&arrays_memlist);
856 deallocate_memory_list(&global_initial_value_memlist);
857 deallocate_memory_list(¤t_array_name);
860 /* ========================================================================= */