symbols.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382
  1. /* ------------------------------------------------------------------------- */
  2. /* "symbols" : The symbols table; creating stock of reserved words */
  3. /* */
  4. /* Part of Inform 6.33 */
  5. /* copyright (c) Graham Nelson 1993 - 2014 */
  6. /* */
  7. /* ------------------------------------------------------------------------- */
  8. #include "header.h"
  9. /* ------------------------------------------------------------------------- */
  10. /* This section of Inform is a service detached from the rest. */
  11. /* Only two variables are accessible from the outside: */
  12. /* ------------------------------------------------------------------------- */
  13. int no_symbols; /* Total number of symbols defined */
  14. int no_named_constants; /* Copied into story file */
  15. /* ------------------------------------------------------------------------- */
  16. /* Plus six arrays. Each symbol has its own index n (an int32) and */
  17. /* */
  18. /* svals[n] is its value (must be 32 bits wide, i.e. an int32, tho' */
  19. /* it is used to hold an unsigned 16 bit Z-machine value) */
  20. /* sflags[n] holds flags (see "header.h" for a list) */
  21. /* stypes[n] is the "type", distinguishing between the data type of */
  22. /* different kinds of constants/variables. */
  23. /* (See the "typename()" below.) */
  24. /* symbs[n] (needs to be cast to (char *) to be used) is the name */
  25. /* of the symbol, in the same case form as when created. */
  26. /* slines[n] is the source line on which the symbol value was first */
  27. /* assigned */
  28. /* symbol_debug_backpatch_positions[n] */
  29. /* is a file position in the debug information file where */
  30. /* the symbol's value should be written after backpatching, */
  31. /* or else the null position if the value was known and */
  32. /* written beforehand */
  33. /* replacement_debug_backpatch_positions[n] */
  34. /* is a file position in the debug information file where */
  35. /* the symbol's name can be erased if it is replaced, or */
  36. /* else null if the name will never need to be replaced */
  37. /* */
  38. /* Comparison is case insensitive. */
  39. /* Note that local variable names are not entered into the symbols table, */
  40. /* as their numbers and scope are too limited for this to be efficient. */
  41. /* ------------------------------------------------------------------------- */
  42. /* Caveat editor: some array types are set up to work even on machines */
  43. /* where sizeof(int32 *) differs from, e.g., sizeof(char *): so do not */
  44. /* alter the types unless you understand what is going on! */
  45. /* ------------------------------------------------------------------------- */
  46. int32 **symbs;
  47. int32 *svals;
  48. int *smarks; /* Glulx-only */
  49. int32 *slines;
  50. int *sflags;
  51. #ifdef VAX
  52. char *stypes; /* In VAX C, insanely, "signed char" is illegal */
  53. #else
  54. signed char *stypes;
  55. #endif
  56. maybe_file_position *symbol_debug_backpatch_positions;
  57. maybe_file_position *replacement_debug_backpatch_positions;
  58. /* ------------------------------------------------------------------------- */
  59. /* Memory to hold the text of symbol names: note that this memory is */
  60. /* allocated as needed in chunks of size SYMBOLS_CHUNK_SIZE. */
  61. /* ------------------------------------------------------------------------- */
  62. #define MAX_SYMBOL_CHUNKS (100)
  63. static uchar *symbols_free_space, /* Next byte free to hold new names */
  64. *symbols_ceiling; /* Pointer to the end of the current
  65. allocation of memory for names */
  66. static char** symbol_name_space_chunks; /* For chunks of memory used to hold
  67. the name strings of symbols */
  68. static int no_symbol_name_space_chunks;
  69. typedef struct value_pair_struct {
  70. int original_symbol;
  71. int renamed_symbol;
  72. } value_pair_t;
  73. static value_pair_t *symbol_replacements;
  74. static int symbol_replacements_count;
  75. static int symbol_replacements_size; /* calloced size */
  76. /* ------------------------------------------------------------------------- */
  77. /* The symbols table is "hash-coded" into a disjoint union of linked */
  78. /* lists, so that for any symbol i, next_entry[i] is either -1 (meaning */
  79. /* that it's the last in its list) or the next in the list. */
  80. /* */
  81. /* Each list contains, in alphabetical order, all the symbols which share */
  82. /* the same "hash code" (a numerical function of the text of the symbol */
  83. /* name, designed with the aim that roughly equal numbers of symbols are */
  84. /* given each possible hash code). The hash codes are 0 to HASH_TAB_SIZE */
  85. /* (which is a memory setting) minus 1: start_of_list[h] gives the first */
  86. /* symbol with hash code h, or -1 if no symbol exists with hash code h. */
  87. /* */
  88. /* Note that the running time of the symbol search algorithm is about */
  89. /* */
  90. /* O ( n^2 / HASH_TAB_SIZE ) */
  91. /* */
  92. /* (where n is the number of symbols in the program) so that it is a good */
  93. /* idea to choose HASH_TAB_SIZE as large as conveniently possible. */
  94. /* ------------------------------------------------------------------------- */
  95. static int *next_entry;
  96. static int32 *start_of_list;
  97. /* ------------------------------------------------------------------------- */
  98. /* Initialisation. */
  99. /* ------------------------------------------------------------------------- */
  100. static void init_symbol_banks(void)
  101. { int i;
  102. for (i=0; i<HASH_TAB_SIZE; i++) start_of_list[i] = -1;
  103. }
  104. /* ------------------------------------------------------------------------- */
  105. /* The hash coding we use is quite standard; the variable hashcode is */
  106. /* expected to overflow a good deal. (The aim is to produce a number */
  107. /* so that similar names do not produce the same number.) Note that */
  108. /* 30011 is prime. It doesn't matter if the unsigned int to int cast */
  109. /* behaves differently on different ports. */
  110. /* ------------------------------------------------------------------------- */
  111. int case_conversion_grid[128];
  112. static void make_case_conversion_grid(void)
  113. {
  114. /* Assumes that A to Z are contiguous in the host OS character set:
  115. true for ASCII but not for EBCDIC, for instance. */
  116. int i;
  117. for (i=0; i<128; i++) case_conversion_grid[i] = i;
  118. for (i=0; i<26; i++) case_conversion_grid['A'+i]='a'+i;
  119. }
  120. extern int hash_code_from_string(char *p)
  121. { uint32 hashcode=0;
  122. for (; *p; p++) hashcode=hashcode*30011 + case_conversion_grid[(uchar)*p];
  123. return (int) (hashcode % HASH_TAB_SIZE);
  124. }
  125. extern int strcmpcis(char *p, char *q)
  126. {
  127. /* Case insensitive strcmp */
  128. int i, j, pc, qc;
  129. for (i=0;p[i] != 0;i++)
  130. { pc = p[i]; if (isupper(pc)) pc = tolower(pc);
  131. qc = q[i]; if (isupper(qc)) qc = tolower(qc);
  132. j = pc - qc;
  133. if (j!=0) return j;
  134. }
  135. qc = q[i]; if (isupper(qc)) qc = tolower(qc);
  136. return -qc;
  137. }
  138. /* ------------------------------------------------------------------------- */
  139. /* Symbol finding, creating, and removing. */
  140. /* ------------------------------------------------------------------------- */
  141. extern int symbol_index(char *p, int hashcode)
  142. {
  143. /* Return the index in the symbs/svals/sflags/stypes/... arrays of symbol
  144. "p", creating a new symbol with that name if it isn't already there.
  145. New symbols are created with flag UNKNOWN_SFLAG, value 0x100
  146. (a 2-byte quantity in Z-machine terms) and type CONSTANT_T.
  147. The string "p" is undamaged. */
  148. int32 new_entry, this, last; char *r;
  149. if (hashcode == -1) hashcode = hash_code_from_string(p);
  150. this = start_of_list[hashcode]; last = -1;
  151. do
  152. { if (this == -1) break;
  153. r = (char *)symbs[this];
  154. new_entry = strcmpcis(r, p);
  155. if (new_entry == 0)
  156. {
  157. if (track_unused_routines)
  158. df_note_function_symbol(this);
  159. return this;
  160. }
  161. if (new_entry > 0) break;
  162. last = this;
  163. this = next_entry[this];
  164. } while (this != -1);
  165. if (no_symbols >= MAX_SYMBOLS)
  166. memoryerror("MAX_SYMBOLS", MAX_SYMBOLS);
  167. if (last == -1)
  168. { next_entry[no_symbols]=start_of_list[hashcode];
  169. start_of_list[hashcode]=no_symbols;
  170. }
  171. else
  172. { next_entry[no_symbols]=this;
  173. next_entry[last]=no_symbols;
  174. }
  175. if (symbols_free_space+strlen(p)+1 >= symbols_ceiling)
  176. { symbols_free_space
  177. = my_malloc(SYMBOLS_CHUNK_SIZE, "symbol names chunk");
  178. symbols_ceiling = symbols_free_space + SYMBOLS_CHUNK_SIZE;
  179. /* If we've passed MAX_SYMBOL_CHUNKS chunks, we print an error
  180. message telling the user to increase SYMBOLS_CHUNK_SIZE.
  181. That is the correct cure, even though the error comes out
  182. worded inaccurately. */
  183. if (no_symbol_name_space_chunks >= MAX_SYMBOL_CHUNKS)
  184. memoryerror("SYMBOLS_CHUNK_SIZE", SYMBOLS_CHUNK_SIZE);
  185. symbol_name_space_chunks[no_symbol_name_space_chunks++]
  186. = (char *) symbols_free_space;
  187. if (symbols_free_space+strlen(p)+1 >= symbols_ceiling)
  188. memoryerror("SYMBOLS_CHUNK_SIZE", SYMBOLS_CHUNK_SIZE);
  189. }
  190. strcpy((char *) symbols_free_space, p);
  191. symbs[no_symbols] = (int32 *) symbols_free_space;
  192. symbols_free_space += strlen((char *)symbols_free_space) + 1;
  193. svals[no_symbols] = 0x100; /* ###-wrong? Would this fix the
  194. unbound-symbol-causes-asm-error? */
  195. sflags[no_symbols] = UNKNOWN_SFLAG;
  196. stypes[no_symbols] = CONSTANT_T;
  197. slines[no_symbols] = ErrorReport.line_number
  198. + FILE_LINE_SCALE_FACTOR*ErrorReport.file_number;
  199. if (debugfile_switch)
  200. { nullify_debug_file_position
  201. (&symbol_debug_backpatch_positions[no_symbols]);
  202. nullify_debug_file_position
  203. (&replacement_debug_backpatch_positions[no_symbols]);
  204. }
  205. if (track_unused_routines)
  206. df_note_function_symbol(no_symbols);
  207. return(no_symbols++);
  208. }
  209. extern void end_symbol_scope(int k)
  210. {
  211. /* Remove the given symbol from the hash table, making it
  212. invisible to symbol_index. This is used by the Undef directive.
  213. If the symbol is not found, this silently does nothing.
  214. */
  215. int j;
  216. j = hash_code_from_string((char *) symbs[k]);
  217. if (start_of_list[j] == k)
  218. { start_of_list[j] = next_entry[k];
  219. return;
  220. }
  221. j = start_of_list[j];
  222. while (j != -1)
  223. {
  224. if (next_entry[j] == k)
  225. { next_entry[j] = next_entry[k];
  226. return;
  227. }
  228. j = next_entry[j];
  229. }
  230. }
  231. /* ------------------------------------------------------------------------- */
  232. /* Printing diagnostics */
  233. /* ------------------------------------------------------------------------- */
  234. extern char *typename(int type)
  235. { switch(type)
  236. {
  237. /* These are the possible symbol types. Note that local variables
  238. do not reside in the symbol table (for scope and efficiency
  239. reasons) and actions have their own name-space (via routine
  240. names with "Sub" appended). */
  241. case ROUTINE_T: return("Routine");
  242. case LABEL_T: return("Label");
  243. case GLOBAL_VARIABLE_T: return("Global variable");
  244. case ARRAY_T: return("Array");
  245. case CONSTANT_T: return("Defined constant");
  246. case ATTRIBUTE_T: return("Attribute");
  247. case PROPERTY_T: return("Property");
  248. case INDIVIDUAL_PROPERTY_T: return("Individual property");
  249. case OBJECT_T: return("Object");
  250. case CLASS_T: return("Class");
  251. case FAKE_ACTION_T: return("Fake action");
  252. default: return("(Unknown type)");
  253. }
  254. }
  255. static void describe_flags(int flags)
  256. { if (flags & UNKNOWN_SFLAG) printf("(?) ");
  257. if (flags & USED_SFLAG) printf("(used) ");
  258. if (flags & REPLACE_SFLAG) printf("(Replaced) ");
  259. if (flags & DEFCON_SFLAG) printf("(Defaulted) ");
  260. if (flags & STUB_SFLAG) printf("(Stubbed) ");
  261. if (flags & CHANGE_SFLAG) printf("(value will change) ");
  262. if (flags & IMPORT_SFLAG) printf("(Imported) ");
  263. if (flags & EXPORT_SFLAG) printf("(Exported) ");
  264. if (flags & SYSTEM_SFLAG) printf("(System) ");
  265. if (flags & INSF_SFLAG) printf("(created in sys file) ");
  266. if (flags & UERROR_SFLAG) printf("('Unknown' error issued) ");
  267. if (flags & ALIASED_SFLAG) printf("(aliased) ");
  268. if (flags & ACTION_SFLAG) printf("(Action name) ");
  269. if (flags & REDEFINABLE_SFLAG) printf("(Redefinable) ");
  270. }
  271. extern void describe_symbol(int k)
  272. { printf("%4d %-16s %2d:%04d %04x %s ",
  273. k, (char *) (symbs[k]),
  274. (int)(slines[k]/FILE_LINE_SCALE_FACTOR),
  275. (int)(slines[k]%FILE_LINE_SCALE_FACTOR),
  276. svals[k], typename(stypes[k]));
  277. describe_flags(sflags[k]);
  278. }
  279. extern void list_symbols(int level)
  280. { int k;
  281. for (k=0; k<no_symbols; k++)
  282. { if ((level==2) ||
  283. ((sflags[k] & (SYSTEM_SFLAG + UNKNOWN_SFLAG + INSF_SFLAG)) == 0))
  284. { describe_symbol(k); printf("\n");
  285. }
  286. }
  287. }
  288. extern void issue_unused_warnings(void)
  289. { int32 i;
  290. if (module_switch) return;
  291. /* Update any ad-hoc variables that might help the library */
  292. if (glulx_mode)
  293. { global_initial_value[10]=statusline_flag;
  294. }
  295. /* Now back to mark anything necessary as used */
  296. i = symbol_index("Main", -1);
  297. if (!(sflags[i] & UNKNOWN_SFLAG)) sflags[i] |= USED_SFLAG;
  298. for (i=0;i<no_symbols;i++)
  299. { if (((sflags[i]
  300. & (SYSTEM_SFLAG + UNKNOWN_SFLAG + EXPORT_SFLAG
  301. + INSF_SFLAG + USED_SFLAG + REPLACE_SFLAG)) == 0)
  302. && (stypes[i] != OBJECT_T))
  303. dbnu_warning(typename(stypes[i]), (char *) symbs[i], slines[i]);
  304. }
  305. }
  306. /* ------------------------------------------------------------------------- */
  307. /* These are arrays used only during story file (never module) creation, */
  308. /* and not allocated until then. */
  309. int32 *individual_name_strings; /* Packed addresses of Z-encoded
  310. strings of the names of the
  311. properties: this is an array
  312. indexed by the property ID */
  313. int32 *action_name_strings; /* Ditto for actions */
  314. int32 *attribute_name_strings; /* Ditto for attributes */
  315. int32 *array_name_strings; /* Ditto for arrays */
  316. extern void write_the_identifier_names(void)
  317. { int i, j, k, t, null_value; char idname_string[256];
  318. static char unknown_attribute[20] = "<unknown attribute>";
  319. for (i=0; i<no_individual_properties; i++)
  320. individual_name_strings[i] = 0;
  321. if (module_switch) return;
  322. veneer_mode = TRUE;
  323. null_value = compile_string(unknown_attribute, FALSE, FALSE);
  324. for (i=0; i<NUM_ATTR_BYTES*8; i++) attribute_name_strings[i] = null_value;
  325. for (i=0; i<no_symbols; i++)
  326. { t=stypes[i];
  327. if ((t == INDIVIDUAL_PROPERTY_T) || (t == PROPERTY_T))
  328. { if (sflags[i] & ALIASED_SFLAG)
  329. { if (individual_name_strings[svals[i]] == 0)
  330. { sprintf(idname_string, "%s", (char *) symbs[i]);
  331. for (j=i+1, k=0; (j<no_symbols && k<3); j++)
  332. { if ((stypes[j] == stypes[i])
  333. && (svals[j] == svals[i]))
  334. { sprintf(idname_string+strlen(idname_string),
  335. "/%s", (char *) symbs[j]);
  336. k++;
  337. }
  338. }
  339. individual_name_strings[svals[i]]
  340. = compile_string(idname_string, FALSE, FALSE);
  341. }
  342. }
  343. else
  344. { sprintf(idname_string, "%s", (char *) symbs[i]);
  345. individual_name_strings[svals[i]]
  346. = compile_string(idname_string, FALSE, FALSE);
  347. }
  348. }
  349. if (t == ATTRIBUTE_T)
  350. { if (sflags[i] & ALIASED_SFLAG)
  351. { if (attribute_name_strings[svals[i]] == null_value)
  352. { sprintf(idname_string, "%s", (char *) symbs[i]);
  353. for (j=i+1, k=0; (j<no_symbols && k<3); j++)
  354. { if ((stypes[j] == stypes[i])
  355. && (svals[j] == svals[i]))
  356. { sprintf(idname_string+strlen(idname_string),
  357. "/%s", (char *) symbs[j]);
  358. k++;
  359. }
  360. }
  361. attribute_name_strings[svals[i]]
  362. = compile_string(idname_string, FALSE, FALSE);
  363. }
  364. }
  365. else
  366. { sprintf(idname_string, "%s", (char *) symbs[i]);
  367. attribute_name_strings[svals[i]]
  368. = compile_string(idname_string, FALSE, FALSE);
  369. }
  370. }
  371. if (sflags[i] & ACTION_SFLAG)
  372. { sprintf(idname_string, "%s", (char *) symbs[i]);
  373. idname_string[strlen(idname_string)-3] = 0;
  374. if (debugfile_switch)
  375. { debug_file_printf("<action>");
  376. debug_file_printf
  377. ("<identifier>##%s</identifier>", idname_string);
  378. debug_file_printf("<value>%d</value>", svals[i]);
  379. debug_file_printf("</action>");
  380. }
  381. action_name_strings[svals[i]]
  382. = compile_string(idname_string, FALSE, FALSE);
  383. }
  384. }
  385. for (i=0; i<no_symbols; i++)
  386. { if (stypes[i] == FAKE_ACTION_T)
  387. { sprintf(idname_string, "%s", (char *) symbs[i]);
  388. idname_string[strlen(idname_string)-3] = 0;
  389. action_name_strings[svals[i]
  390. - ((grammar_version_number==1)?256:4096) + no_actions]
  391. = compile_string(idname_string, FALSE, FALSE);
  392. }
  393. }
  394. for (j=0; j<no_arrays; j++)
  395. { i = array_symbols[j];
  396. sprintf(idname_string, "%s", (char *) symbs[i]);
  397. array_name_strings[j]
  398. = compile_string(idname_string, FALSE, FALSE);
  399. }
  400. if (define_INFIX_switch)
  401. { for (i=0; i<no_symbols; i++)
  402. { if (stypes[i] == GLOBAL_VARIABLE_T)
  403. { sprintf(idname_string, "%s", (char *) symbs[i]);
  404. array_name_strings[no_arrays + svals[i] -16]
  405. = compile_string(idname_string, FALSE, FALSE);
  406. }
  407. }
  408. for (i=0; i<no_named_routines; i++)
  409. { sprintf(idname_string, "%s", (char *) symbs[named_routine_symbols[i]]);
  410. array_name_strings[no_arrays + no_globals + i]
  411. = compile_string(idname_string, FALSE, FALSE);
  412. }
  413. for (i=0, no_named_constants=0; i<no_symbols; i++)
  414. { if (((stypes[i] == OBJECT_T) || (stypes[i] == CLASS_T)
  415. || (stypes[i] == CONSTANT_T))
  416. && ((sflags[i] & (UNKNOWN_SFLAG+ACTION_SFLAG))==0))
  417. { sprintf(idname_string, "%s", (char *) symbs[i]);
  418. array_name_strings[no_arrays + no_globals + no_named_routines
  419. + no_named_constants++]
  420. = compile_string(idname_string, FALSE, FALSE);
  421. }
  422. }
  423. }
  424. veneer_mode = FALSE;
  425. }
  426. /* ------------------------------------------------------------------------- */
  427. /* Creating symbols */
  428. /* ------------------------------------------------------------------------- */
  429. static void assign_symbol_base(int index, int32 value, int type)
  430. { svals[index] = value;
  431. stypes[index] = type;
  432. if (sflags[index] & UNKNOWN_SFLAG)
  433. { sflags[index] &= (~UNKNOWN_SFLAG);
  434. if (is_systemfile()) sflags[index] |= INSF_SFLAG;
  435. slines[index] = ErrorReport.line_number
  436. + FILE_LINE_SCALE_FACTOR*ErrorReport.file_number;
  437. }
  438. }
  439. extern void assign_symbol(int index, int32 value, int type)
  440. {
  441. if (!glulx_mode) {
  442. assign_symbol_base(index, value, type);
  443. }
  444. else {
  445. smarks[index] = 0;
  446. assign_symbol_base(index, value, type);
  447. }
  448. }
  449. extern void assign_marked_symbol(int index, int marker, int32 value, int type)
  450. {
  451. if (!glulx_mode) {
  452. assign_symbol_base(index, (int32)marker*0x10000 + (value % 0x10000),
  453. type);
  454. }
  455. else {
  456. smarks[index] = marker;
  457. assign_symbol_base(index, value, type);
  458. }
  459. }
  460. static void emit_debug_information_for_predefined_symbol
  461. (char *name, int32 symbol, int32 value, int type)
  462. { if (debugfile_switch)
  463. { switch (type)
  464. { case CONSTANT_T:
  465. debug_file_printf("<constant>");
  466. debug_file_printf("<identifier>%s</identifier>", name);
  467. write_debug_symbol_optional_backpatch(symbol);
  468. debug_file_printf("</constant>");
  469. break;
  470. case GLOBAL_VARIABLE_T:
  471. debug_file_printf("<global-variable>");
  472. debug_file_printf("<identifier>%s</identifier>", name);
  473. debug_file_printf("<address>");
  474. write_debug_global_backpatch(value);
  475. debug_file_printf("</address>");
  476. debug_file_printf("</global-variable>");
  477. break;
  478. case OBJECT_T:
  479. if (value)
  480. { compiler_error("Non-nothing object predefined");
  481. }
  482. debug_file_printf("<object>");
  483. debug_file_printf("<identifier>%s</identifier>", name);
  484. debug_file_printf("<value>0</value>");
  485. debug_file_printf("</object>");
  486. break;
  487. case ATTRIBUTE_T:
  488. debug_file_printf("<attribute>");
  489. debug_file_printf("<identifier>%s</identifier>", name);
  490. debug_file_printf("<value>%d</value>", value);
  491. debug_file_printf("</attribute>");
  492. break;
  493. case PROPERTY_T:
  494. case INDIVIDUAL_PROPERTY_T:
  495. debug_file_printf("<property>");
  496. debug_file_printf("<identifier>%s</identifier>", name);
  497. debug_file_printf("<value>%d</value>", value);
  498. debug_file_printf("</property>");
  499. break;
  500. default:
  501. compiler_error
  502. ("Unable to emit debug information for predefined symbol");
  503. break;
  504. }
  505. }
  506. }
  507. static void create_symbol(char *p, int32 value, int type)
  508. { int i = symbol_index(p, -1);
  509. svals[i] = value; stypes[i] = type; slines[i] = 0;
  510. sflags[i] = USED_SFLAG + SYSTEM_SFLAG;
  511. emit_debug_information_for_predefined_symbol(p, i, value, type);
  512. }
  513. static void create_rsymbol(char *p, int value, int type)
  514. { int i = symbol_index(p, -1);
  515. svals[i] = value; stypes[i] = type; slines[i] = 0;
  516. sflags[i] = USED_SFLAG + SYSTEM_SFLAG + REDEFINABLE_SFLAG;
  517. emit_debug_information_for_predefined_symbol(p, i, value, type);
  518. }
  519. static void stockup_symbols(void)
  520. {
  521. if (!glulx_mode)
  522. create_symbol("TARGET_ZCODE", 0, CONSTANT_T);
  523. else
  524. create_symbol("TARGET_GLULX", 0, CONSTANT_T);
  525. create_symbol("nothing", 0, OBJECT_T);
  526. create_symbol("name", 1, PROPERTY_T);
  527. create_symbol("true", 1, CONSTANT_T);
  528. create_symbol("false", 0, CONSTANT_T);
  529. /* Glulx defaults to GV2; Z-code to GV1 */
  530. if (!glulx_mode)
  531. create_rsymbol("Grammar__Version", 1, CONSTANT_T);
  532. else
  533. create_rsymbol("Grammar__Version", 2, CONSTANT_T);
  534. grammar_version_symbol = symbol_index("Grammar__Version", -1);
  535. if (module_switch)
  536. create_rsymbol("MODULE_MODE",0, CONSTANT_T);
  537. if (runtime_error_checking_switch)
  538. create_rsymbol("STRICT_MODE",0, CONSTANT_T);
  539. if (define_DEBUG_switch)
  540. create_rsymbol("DEBUG", 0, CONSTANT_T);
  541. if (define_USE_MODULES_switch)
  542. create_rsymbol("USE_MODULES",0, CONSTANT_T);
  543. if (define_INFIX_switch)
  544. { create_rsymbol("INFIX", 0, CONSTANT_T);
  545. create_symbol("infix__watching", 0, ATTRIBUTE_T);
  546. }
  547. create_symbol("WORDSIZE", WORDSIZE, CONSTANT_T);
  548. create_symbol("DICT_ENTRY_BYTES", DICT_ENTRY_BYTE_LENGTH, CONSTANT_T);
  549. if (!glulx_mode) {
  550. create_symbol("DICT_WORD_SIZE", ((version_number==3)?4:6), CONSTANT_T);
  551. create_symbol("NUM_ATTR_BYTES", ((version_number==3)?4:6), CONSTANT_T);
  552. }
  553. else {
  554. create_symbol("DICT_WORD_SIZE", DICT_WORD_SIZE, CONSTANT_T);
  555. create_symbol("DICT_CHAR_SIZE", DICT_CHAR_SIZE, CONSTANT_T);
  556. if (DICT_CHAR_SIZE != 1)
  557. create_symbol("DICT_IS_UNICODE", 1, CONSTANT_T);
  558. create_symbol("NUM_ATTR_BYTES", NUM_ATTR_BYTES, CONSTANT_T);
  559. create_symbol("GOBJFIELD_CHAIN", GOBJFIELD_CHAIN(), CONSTANT_T);
  560. create_symbol("GOBJFIELD_NAME", GOBJFIELD_NAME(), CONSTANT_T);
  561. create_symbol("GOBJFIELD_PROPTAB", GOBJFIELD_PROPTAB(), CONSTANT_T);
  562. create_symbol("GOBJFIELD_PARENT", GOBJFIELD_PARENT(), CONSTANT_T);
  563. create_symbol("GOBJFIELD_SIBLING", GOBJFIELD_SIBLING(), CONSTANT_T);
  564. create_symbol("GOBJFIELD_CHILD", GOBJFIELD_CHILD(), CONSTANT_T);
  565. create_symbol("GOBJ_EXT_START", 1+NUM_ATTR_BYTES+6*WORDSIZE, CONSTANT_T);
  566. create_symbol("GOBJ_TOTAL_LENGTH", 1+NUM_ATTR_BYTES+6*WORDSIZE+GLULX_OBJECT_EXT_BYTES, CONSTANT_T);
  567. create_symbol("INDIV_PROP_START", INDIV_PROP_START, CONSTANT_T);
  568. }
  569. if (!glulx_mode) {
  570. create_symbol("temp_global", 255, GLOBAL_VARIABLE_T);
  571. create_symbol("temp__global2", 254, GLOBAL_VARIABLE_T);
  572. create_symbol("temp__global3", 253, GLOBAL_VARIABLE_T);
  573. create_symbol("temp__global4", 252, GLOBAL_VARIABLE_T);
  574. create_symbol("self", 251, GLOBAL_VARIABLE_T);
  575. create_symbol("sender", 250, GLOBAL_VARIABLE_T);
  576. create_symbol("sw__var", 249, GLOBAL_VARIABLE_T);
  577. create_symbol("sys__glob0", 16, GLOBAL_VARIABLE_T);
  578. create_symbol("sys__glob1", 17, GLOBAL_VARIABLE_T);
  579. create_symbol("sys__glob2", 18, GLOBAL_VARIABLE_T);
  580. create_symbol("create", 64, INDIVIDUAL_PROPERTY_T);
  581. create_symbol("recreate", 65, INDIVIDUAL_PROPERTY_T);
  582. create_symbol("destroy", 66, INDIVIDUAL_PROPERTY_T);
  583. create_symbol("remaining", 67, INDIVIDUAL_PROPERTY_T);
  584. create_symbol("copy", 68, INDIVIDUAL_PROPERTY_T);
  585. create_symbol("call", 69, INDIVIDUAL_PROPERTY_T);
  586. create_symbol("print", 70, INDIVIDUAL_PROPERTY_T);
  587. create_symbol("print_to_array",71, INDIVIDUAL_PROPERTY_T);
  588. }
  589. else {
  590. /* In Glulx, these system globals are entered in order, not down
  591. from 255. */
  592. create_symbol("temp_global", MAX_LOCAL_VARIABLES+0,
  593. GLOBAL_VARIABLE_T);
  594. create_symbol("temp__global2", MAX_LOCAL_VARIABLES+1,
  595. GLOBAL_VARIABLE_T);
  596. create_symbol("temp__global3", MAX_LOCAL_VARIABLES+2,
  597. GLOBAL_VARIABLE_T);
  598. create_symbol("temp__global4", MAX_LOCAL_VARIABLES+3,
  599. GLOBAL_VARIABLE_T);
  600. create_symbol("self", MAX_LOCAL_VARIABLES+4,
  601. GLOBAL_VARIABLE_T);
  602. create_symbol("sender", MAX_LOCAL_VARIABLES+5,
  603. GLOBAL_VARIABLE_T);
  604. create_symbol("sw__var", MAX_LOCAL_VARIABLES+6,
  605. GLOBAL_VARIABLE_T);
  606. /* These are almost certainly meaningless, and can be removed. */
  607. create_symbol("sys__glob0", MAX_LOCAL_VARIABLES+7,
  608. GLOBAL_VARIABLE_T);
  609. create_symbol("sys__glob1", MAX_LOCAL_VARIABLES+8,
  610. GLOBAL_VARIABLE_T);
  611. create_symbol("sys__glob2", MAX_LOCAL_VARIABLES+9,
  612. GLOBAL_VARIABLE_T);
  613. /* value of statusline_flag to be written later */
  614. create_symbol("sys_statusline_flag", MAX_LOCAL_VARIABLES+10,
  615. GLOBAL_VARIABLE_T);
  616. /* These are created in order, but not necessarily at a fixed
  617. value. */
  618. create_symbol("create", INDIV_PROP_START+0,
  619. INDIVIDUAL_PROPERTY_T);
  620. create_symbol("recreate", INDIV_PROP_START+1,
  621. INDIVIDUAL_PROPERTY_T);
  622. create_symbol("destroy", INDIV_PROP_START+2,
  623. INDIVIDUAL_PROPERTY_T);
  624. create_symbol("remaining", INDIV_PROP_START+3,
  625. INDIVIDUAL_PROPERTY_T);
  626. create_symbol("copy", INDIV_PROP_START+4,
  627. INDIVIDUAL_PROPERTY_T);
  628. create_symbol("call", INDIV_PROP_START+5,
  629. INDIVIDUAL_PROPERTY_T);
  630. create_symbol("print", INDIV_PROP_START+6,
  631. INDIVIDUAL_PROPERTY_T);
  632. create_symbol("print_to_array",INDIV_PROP_START+7,
  633. INDIVIDUAL_PROPERTY_T);
  634. /* Floating-point constants. Note that FLOAT_NINFINITY is not
  635. -FLOAT_INFINITY, because float negation doesn't work that
  636. way. Also note that FLOAT_NAN is just one of many possible
  637. "not-a-number" values. */
  638. create_symbol("FLOAT_INFINITY", 0x7F800000, CONSTANT_T);
  639. create_symbol("FLOAT_NINFINITY", 0xFF800000, CONSTANT_T);
  640. create_symbol("FLOAT_NAN", 0x7FC00000, CONSTANT_T);
  641. }
  642. }
  643. /* ------------------------------------------------------------------------- */
  644. /* The symbol replacement table. This is needed only for the */
  645. /* "Replace X Y" directive. */
  646. /* ------------------------------------------------------------------------- */
  647. extern void add_symbol_replacement_mapping(int original, int renamed)
  648. {
  649. int ix;
  650. if (original == renamed) {
  651. error_named("A routine cannot be 'Replace'd to itself:", (char *)symbs[original]);
  652. return;
  653. }
  654. if (symbol_replacements_count == symbol_replacements_size) {
  655. int oldsize = symbol_replacements_size;
  656. if (symbol_replacements_size == 0)
  657. symbol_replacements_size = 4;
  658. else
  659. symbol_replacements_size *= 2;
  660. my_recalloc(&symbol_replacements, sizeof(value_pair_t), oldsize,
  661. symbol_replacements_size, "symbol replacement table");
  662. }
  663. /* If the original form is already in our table, report an error.
  664. Same goes if the replaced form is already in the table as an
  665. original. (Other collision cases have already been
  666. detected.) */
  667. for (ix=0; ix<symbol_replacements_count; ix++) {
  668. if (original == symbol_replacements[ix].original_symbol) {
  669. error_named("A routine cannot be 'Replace'd to more than one new name:", (char *)symbs[original]);
  670. }
  671. if (renamed == symbol_replacements[ix].original_symbol) {
  672. error_named("A routine cannot be 'Replace'd to a 'Replace'd name:", (char *)symbs[original]);
  673. }
  674. }
  675. symbol_replacements[symbol_replacements_count].original_symbol = original;
  676. symbol_replacements[symbol_replacements_count].renamed_symbol = renamed;
  677. symbol_replacements_count++;
  678. }
  679. extern int find_symbol_replacement(int *value)
  680. {
  681. int changed = FALSE;
  682. int ix;
  683. if (!symbol_replacements)
  684. return FALSE;
  685. for (ix=0; ix<symbol_replacements_count; ix++) {
  686. if (*value == symbol_replacements[ix].original_symbol) {
  687. *value = symbol_replacements[ix].renamed_symbol;
  688. changed = TRUE;
  689. }
  690. }
  691. return changed;
  692. }
  693. /* ------------------------------------------------------------------------- */
  694. /* The dead-function removal optimization. */
  695. /* ------------------------------------------------------------------------- */
  696. int track_unused_routines; /* set if either WARN_UNUSED_ROUTINES or
  697. OMIT_UNUSED_ROUTINES is nonzero */
  698. int df_dont_note_global_symbols; /* temporarily set at times in parsing */
  699. static int df_tables_closed; /* set at end of compiler pass */
  700. typedef struct df_function_struct df_function_t;
  701. typedef struct df_reference_struct df_reference_t;
  702. struct df_function_struct {
  703. char *name; /* borrowed reference, generally to the symbs[] table */
  704. int32 source_line; /* copied from routine_starts_line */
  705. int sysfile; /* does this occur in a system file? */
  706. uint32 address; /* function offset in zcode_area (not the final address) */
  707. uint32 newaddress; /* function offset after stripping */
  708. uint32 length;
  709. int usage;
  710. df_reference_t *refs; /* chain of references made *from* this function */
  711. int processed;
  712. df_function_t *funcnext; /* in forward functions order */
  713. df_function_t *todonext; /* in the todo chain */
  714. df_function_t *next; /* in the hash table */
  715. };
  716. struct df_reference_struct {
  717. uint32 address; /* function offset in zcode_area (not the final address) */
  718. int symbol; /* index in symbols array */
  719. df_reference_t *refsnext; /* in the function's refs chain */
  720. df_reference_t *next; /* in the hash table */
  721. };
  722. /* Bitmask flags for how functions are used: */
  723. #define DF_USAGE_GLOBAL (1<<0) /* In a global variable, array, etc */
  724. #define DF_USAGE_EMBEDDED (1<<1) /* An anonymous function in a property */
  725. #define DF_USAGE_MAIN (1<<2) /* Main() or Main__() */
  726. #define DF_USAGE_FUNCTION (1<<3) /* Used from another used function */
  727. #define DF_FUNCTION_HASH_BUCKETS (1023)
  728. /* Table of all compiled functions. (Only created if track_unused_routines
  729. is set.) */
  730. static df_function_t **df_functions;
  731. /* List of all compiled functions, in address order. The first entry
  732. has address DF_NOT_IN_FUNCTION, and stands in for the global namespace. */
  733. static df_function_t *df_functions_head;
  734. static df_function_t *df_functions_tail;
  735. /* Used during output_file(), to track how far the code-area output has
  736. gotten. */
  737. static df_function_t *df_iterator;
  738. #define DF_NOT_IN_FUNCTION ((uint32)0xFFFFFFFF)
  739. #define DF_SYMBOL_HASH_BUCKETS (4095)
  740. /* Map of what functions reference what other functions. (Only created if
  741. track_unused_routines is set.) */
  742. static df_reference_t **df_symbol_map;
  743. /* Globals used while a function is being compiled. When a function
  744. *isn't* being compiled, df_current_function_addr will be DF_NOT_IN_FUNCTION
  745. and df_current_function will refer to the global namespace record. */
  746. static df_function_t *df_current_function;
  747. static char *df_current_function_name;
  748. static uint32 df_current_function_addr;
  749. /* Size totals for compiled code. These are only meaningful if
  750. track_unused_routines is true. (If we're only doing WARN_UNUSED_ROUTINES,
  751. these values will be set, but the "after" value will not affect the
  752. final game file.) */
  753. uint32 df_total_size_before_stripping;
  754. uint32 df_total_size_after_stripping;
  755. /* When we begin compiling a function, call this to note that fact.
  756. Any symbol referenced from now on will be associated with the function.
  757. */
  758. extern void df_note_function_start(char *name, uint32 address,
  759. int embedded_flag, int32 source_line)
  760. {
  761. df_function_t *func;
  762. int bucket;
  763. if (df_tables_closed)
  764. error("Internal error in stripping: Tried to start a new function after tables were closed.");
  765. /* We retain the name only for debugging output. Note that embedded
  766. functions all show up as "<embedded>" -- their "obj.prop" name
  767. never gets stored in permanent memory. */
  768. df_current_function_name = name;
  769. df_current_function_addr = address;
  770. func = my_malloc(sizeof(df_function_t), "df function entry");
  771. memset(func, 0, sizeof(df_function_t));
  772. func->name = name;
  773. func->address = address;
  774. func->source_line = source_line;
  775. func->sysfile = (address == DF_NOT_IN_FUNCTION || is_systemfile());
  776. /* An embedded function is stored in an object property, so we
  777. consider it to be used a priori. */
  778. if (embedded_flag)
  779. func->usage |= DF_USAGE_EMBEDDED;
  780. if (!df_functions_head) {
  781. df_functions_head = func;
  782. df_functions_tail = func;
  783. }
  784. else {
  785. df_functions_tail->funcnext = func;
  786. df_functions_tail = func;
  787. }
  788. bucket = address % DF_FUNCTION_HASH_BUCKETS;
  789. func->next = df_functions[bucket];
  790. df_functions[bucket] = func;
  791. df_current_function = func;
  792. }
  793. /* When we're done compiling a function, call this. Any symbol referenced
  794. from now on will be associated with the global namespace.
  795. */
  796. extern void df_note_function_end(uint32 endaddress)
  797. {
  798. df_current_function->length = endaddress - df_current_function->address;
  799. df_current_function_name = NULL;
  800. df_current_function_addr = DF_NOT_IN_FUNCTION;
  801. df_current_function = df_functions_head; /* the global namespace */
  802. }
  803. /* Find the function record for a given address. (Addresses are offsets
  804. in zcode_area.)
  805. */
  806. static df_function_t *df_function_for_address(uint32 address)
  807. {
  808. int bucket = address % DF_FUNCTION_HASH_BUCKETS;
  809. df_function_t *func;
  810. for (func = df_functions[bucket]; func; func = func->next) {
  811. if (func->address == address)
  812. return func;
  813. }
  814. return NULL;
  815. }
  816. /* Whenever a function is referenced, we call this to note who called it.
  817. */
  818. extern void df_note_function_symbol(int symbol)
  819. {
  820. int bucket, symtype;
  821. df_reference_t *ent;
  822. /* If the compiler pass is over, looking up symbols does not create
  823. a global reference. */
  824. if (df_tables_closed)
  825. return;
  826. /* In certain cases during parsing, looking up symbols does not
  827. create a global reference. (For example, when reading the name
  828. of a function being defined.) */
  829. if (df_dont_note_global_symbols)
  830. return;
  831. /* We are only interested in functions, or forward-declared symbols
  832. that might turn out to be functions. */
  833. symtype = stypes[symbol];
  834. if (symtype != ROUTINE_T && symtype != CONSTANT_T)
  835. return;
  836. if (symtype == CONSTANT_T && !(sflags[symbol] & UNKNOWN_SFLAG))
  837. return;
  838. bucket = (df_current_function_addr ^ (uint32)symbol) % DF_SYMBOL_HASH_BUCKETS;
  839. for (ent = df_symbol_map[bucket]; ent; ent = ent->next) {
  840. if (ent->address == df_current_function_addr && ent->symbol == symbol)
  841. return;
  842. }
  843. /* Create a new reference entry in df_symbol_map. */
  844. ent = my_malloc(sizeof(df_reference_t), "df symbol map entry");
  845. ent->address = df_current_function_addr;
  846. ent->symbol = symbol;
  847. ent->next = df_symbol_map[bucket];
  848. df_symbol_map[bucket] = ent;
  849. /* Add the reference to the function's entry as well. */
  850. /* The current function is the most recently added, so it will be
  851. at the top of its bucket. That makes this call fast. Unless
  852. we're in global scope, in which case it might be slower.
  853. (I suppose we could cache the df_function_t pointer of the
  854. current function, to speed things up.) */
  855. if (!df_current_function || df_current_function_addr != df_current_function->address)
  856. compiler_error("DF: df_current_function does not match current address.");
  857. ent->refsnext = df_current_function->refs;
  858. df_current_function->refs = ent;
  859. }
  860. /* This does the hard work of figuring out what functions are truly dead.
  861. It's called near the end of run_pass() in inform.c.
  862. */
  863. extern void locate_dead_functions(void)
  864. {
  865. df_function_t *func, *tofunc;
  866. df_reference_t *ent;
  867. int ix;
  868. if (!track_unused_routines)
  869. compiler_error("DF: locate_dead_functions called, but function references have not been mapped");
  870. df_tables_closed = TRUE;
  871. df_current_function = NULL;
  872. /* Note that Main__ was tagged as global implicitly during
  873. compile_initial_routine(). Main was tagged during
  874. issue_unused_warnings(). But for the sake of thoroughness,
  875. we'll mark them specially. */
  876. ix = symbol_index("Main__", -1);
  877. if (stypes[ix] == ROUTINE_T) {
  878. uint32 addr = svals[ix] * (glulx_mode ? 1 : scale_factor);
  879. tofunc = df_function_for_address(addr);
  880. if (tofunc)
  881. tofunc->usage |= DF_USAGE_MAIN;
  882. }
  883. ix = symbol_index("Main", -1);
  884. if (stypes[ix] == ROUTINE_T) {
  885. uint32 addr = svals[ix] * (glulx_mode ? 1 : scale_factor);
  886. tofunc = df_function_for_address(addr);
  887. if (tofunc)
  888. tofunc->usage |= DF_USAGE_MAIN;
  889. }
  890. /* Go through all the functions referenced at the global level;
  891. mark them as used. */
  892. func = df_functions_head;
  893. if (!func || func->address != DF_NOT_IN_FUNCTION)
  894. compiler_error("DF: Global namespace entry is not at the head of the chain.");
  895. for (ent = func->refs; ent; ent=ent->refsnext) {
  896. uint32 addr;
  897. int symbol = ent->symbol;
  898. if (stypes[symbol] != ROUTINE_T)
  899. continue;
  900. addr = svals[symbol] * (glulx_mode ? 1 : scale_factor);
  901. tofunc = df_function_for_address(addr);
  902. if (!tofunc) {
  903. error_named("Internal error in stripping: global ROUTINE_T symbol is not found in df_function map:", (char *)symbs[symbol]);
  904. continue;
  905. }
  906. /* A function may be marked here more than once. That's fine. */
  907. tofunc->usage |= DF_USAGE_GLOBAL;
  908. }
  909. /* Perform a breadth-first search through functions, starting with
  910. the ones that are known to be used at the top level. */
  911. {
  912. df_function_t *todo, *todotail;
  913. df_function_t *func;
  914. todo = NULL;
  915. todotail = NULL;
  916. for (func = df_functions_head; func; func = func->funcnext) {
  917. if (func->address == DF_NOT_IN_FUNCTION)
  918. continue;
  919. if (func->usage == 0)
  920. continue;
  921. if (!todo) {
  922. todo = func;
  923. todotail = func;
  924. }
  925. else {
  926. todotail->todonext = func;
  927. todotail = func;
  928. }
  929. }
  930. /* todo is a linked list of functions which are known to be
  931. used. If a function's usage field is nonzero, it must be
  932. either be on the todo list or have come off already (in
  933. which case processed will be set). */
  934. while (todo) {
  935. /* Pop the next function. */
  936. func = todo;
  937. todo = todo->todonext;
  938. if (!todo)
  939. todotail = NULL;
  940. if (func->processed)
  941. error_named("Internal error in stripping: function has been processed twice:", func->name);
  942. /* Go through the function's symbol references. Any
  943. reference to a routine, push it into the todo list (if
  944. it isn't there already). */
  945. for (ent = func->refs; ent; ent=ent->refsnext) {
  946. uint32 addr;
  947. int symbol = ent->symbol;
  948. if (stypes[symbol] != ROUTINE_T)
  949. continue;
  950. addr = svals[symbol] * (glulx_mode ? 1 : scale_factor);
  951. tofunc = df_function_for_address(addr);
  952. if (!tofunc) {
  953. error_named("Internal error in stripping: function ROUTINE_T symbol is not found in df_function map:", (char *)symbs[symbol]);
  954. continue;
  955. }
  956. if (tofunc->usage)
  957. continue;
  958. /* Not yet known to be used. Add it to the todo list. */
  959. tofunc->usage |= DF_USAGE_FUNCTION;
  960. if (!todo) {
  961. todo = tofunc;
  962. todotail = tofunc;
  963. }
  964. else {
  965. todotail->todonext = tofunc;
  966. todotail = tofunc;
  967. }
  968. }
  969. func->processed = TRUE;
  970. }
  971. }
  972. /* Go through all functions; figure out how much space is consumed,
  973. with and without useless functions. */
  974. {
  975. df_function_t *func;
  976. df_total_size_before_stripping = 0;
  977. df_total_size_after_stripping = 0;
  978. for (func = df_functions_head; func; func = func->funcnext) {
  979. if (func->address == DF_NOT_IN_FUNCTION)
  980. continue;
  981. if (func->address != df_total_size_before_stripping)
  982. compiler_error("DF: Address gap in function list");
  983. df_total_size_before_stripping += func->length;
  984. if (func->usage) {
  985. func->newaddress = df_total_size_after_stripping;
  986. df_total_size_after_stripping += func->length;
  987. }
  988. if (!glulx_mode && (df_total_size_after_stripping % scale_factor != 0))
  989. compiler_error("DF: New function address is not aligned");
  990. if (WARN_UNUSED_ROUTINES && !func->usage) {
  991. if (!func->sysfile || WARN_UNUSED_ROUTINES >= 2)
  992. uncalled_routine_warning("Routine", func->name, func->source_line);
  993. }
  994. }
  995. }
  996. /* df_measure_hash_table_usage(); */
  997. }
  998. extern uint32 df_stripped_address_for_address(uint32 addr)
  999. {
  1000. df_function_t *func;
  1001. if (!track_unused_routines)
  1002. compiler_error("DF: df_stripped_address_for_address called, but function references have not been mapped");
  1003. if (!glulx_mode)
  1004. func = df_function_for_address(addr*scale_factor);
  1005. else
  1006. func = df_function_for_address(addr);
  1007. if (!func) {
  1008. compiler_error("DF: Unable to find function while backpatching");
  1009. return 0;
  1010. }
  1011. if (!func->usage)
  1012. compiler_error("DF: Tried to backpatch a function address which should be stripped");
  1013. if (!glulx_mode)
  1014. return func->newaddress / scale_factor;
  1015. else
  1016. return func->newaddress;
  1017. }
  1018. /* The output_file() routines in files.c have to run down the list of
  1019. functions, deciding who is in and who is out. But I don't want to
  1020. export the df_function_t list structure. Instead, I provide this
  1021. silly iterator pair. Set it up with df_prepare_function_iterate();
  1022. then repeatedly call df_next_function_iterate().
  1023. */
  1024. extern void df_prepare_function_iterate(void)
  1025. {
  1026. df_iterator = df_functions_head;
  1027. if (!df_iterator || df_iterator->address != DF_NOT_IN_FUNCTION)
  1028. compiler_error("DF: Global namespace entry is not at the head of the chain.");
  1029. if (!df_iterator->funcnext || df_iterator->funcnext->address != 0)
  1030. compiler_error("DF: First function entry is not second in the chain.");
  1031. }
  1032. /* This returns the end of the next function, and whether the next function
  1033. is used (live).
  1034. */
  1035. extern uint32 df_next_function_iterate(int *funcused)
  1036. {
  1037. if (df_iterator)
  1038. df_iterator = df_iterator->funcnext;
  1039. if (!df_iterator) {
  1040. *funcused = TRUE;
  1041. return df_total_size_before_stripping+1;
  1042. }
  1043. *funcused = (df_iterator->usage != 0);
  1044. return df_iterator->address + df_iterator->length;
  1045. }
  1046. /* ========================================================================= */
  1047. /* Data structure management routines */
  1048. /* ------------------------------------------------------------------------- */
  1049. extern void init_symbols_vars(void)
  1050. {
  1051. symbs = NULL;
  1052. svals = NULL;
  1053. smarks = NULL;
  1054. stypes = NULL;
  1055. sflags = NULL;
  1056. next_entry = NULL;
  1057. start_of_list = NULL;
  1058. symbol_name_space_chunks = NULL;
  1059. no_symbol_name_space_chunks = 0;
  1060. symbols_free_space=NULL;
  1061. symbols_ceiling=symbols_free_space;
  1062. no_symbols = 0;
  1063. symbol_replacements = NULL;
  1064. symbol_replacements_count = 0;
  1065. symbol_replacements_size = 0;
  1066. make_case_conversion_grid();
  1067. track_unused_routines = (WARN_UNUSED_ROUTINES || OMIT_UNUSED_ROUTINES);
  1068. df_tables_closed = FALSE;
  1069. df_symbol_map = NULL;
  1070. df_functions = NULL;
  1071. df_functions_head = NULL;
  1072. df_functions_tail = NULL;
  1073. df_current_function = NULL;
  1074. }
  1075. extern void symbols_begin_pass(void)
  1076. {
  1077. df_total_size_before_stripping = 0;
  1078. df_total_size_after_stripping = 0;
  1079. df_dont_note_global_symbols = FALSE;
  1080. df_iterator = NULL;
  1081. }
  1082. extern void symbols_allocate_arrays(void)
  1083. {
  1084. symbs = my_calloc(sizeof(char *), MAX_SYMBOLS, "symbols");
  1085. svals = my_calloc(sizeof(int32), MAX_SYMBOLS, "symbol values");
  1086. if (glulx_mode)
  1087. smarks = my_calloc(sizeof(int), MAX_SYMBOLS, "symbol markers");
  1088. slines = my_calloc(sizeof(int32), MAX_SYMBOLS, "symbol lines");
  1089. stypes = my_calloc(sizeof(char), MAX_SYMBOLS, "symbol types");
  1090. sflags = my_calloc(sizeof(int), MAX_SYMBOLS, "symbol flags");
  1091. if (debugfile_switch)
  1092. { symbol_debug_backpatch_positions =
  1093. my_calloc(sizeof(maybe_file_position), MAX_SYMBOLS,
  1094. "symbol debug information backpatch positions");
  1095. replacement_debug_backpatch_positions =
  1096. my_calloc(sizeof(maybe_file_position), MAX_SYMBOLS,
  1097. "replacement debug information backpatch positions");
  1098. }
  1099. next_entry = my_calloc(sizeof(int), MAX_SYMBOLS,
  1100. "symbol linked-list forward links");
  1101. start_of_list = my_calloc(sizeof(int32), HASH_TAB_SIZE,
  1102. "hash code list beginnings");
  1103. symbol_name_space_chunks
  1104. = my_calloc(sizeof(char *), MAX_SYMBOL_CHUNKS, "symbol names chunk addresses");
  1105. if (track_unused_routines) {
  1106. df_tables_closed = FALSE;
  1107. df_symbol_map = my_calloc(sizeof(df_reference_t *), DF_SYMBOL_HASH_BUCKETS, "df symbol-map hash table");
  1108. memset(df_symbol_map, 0, sizeof(df_reference_t *) * DF_SYMBOL_HASH_BUCKETS);
  1109. df_functions = my_calloc(sizeof(df_function_t *), DF_FUNCTION_HASH_BUCKETS, "df function hash table");
  1110. memset(df_functions, 0, sizeof(df_function_t *) * DF_FUNCTION_HASH_BUCKETS);
  1111. df_functions_head = NULL;
  1112. df_functions_tail = NULL;
  1113. df_note_function_start("<global namespace>", DF_NOT_IN_FUNCTION, FALSE, -1);
  1114. df_note_function_end(DF_NOT_IN_FUNCTION);
  1115. /* Now df_current_function is df_functions_head. */
  1116. }
  1117. init_symbol_banks();
  1118. stockup_symbols();
  1119. /* Allocated as needed */
  1120. symbol_replacements = NULL;
  1121. /* Allocated during story file construction, not now */
  1122. individual_name_strings = NULL;
  1123. attribute_name_strings = NULL;
  1124. action_name_strings = NULL;
  1125. array_name_strings = NULL;
  1126. }
  1127. extern void symbols_free_arrays(void)
  1128. { int i;
  1129. for (i=0; i<no_symbol_name_space_chunks; i++)
  1130. my_free(&(symbol_name_space_chunks[i]),
  1131. "symbol names chunk");
  1132. my_free(&symbol_name_space_chunks, "symbol names chunk addresses");
  1133. my_free(&symbs, "symbols");
  1134. my_free(&svals, "symbol values");
  1135. my_free(&smarks, "symbol markers");
  1136. my_free(&slines, "symbol lines");
  1137. my_free(&stypes, "symbol types");
  1138. my_free(&sflags, "symbol flags");
  1139. if (debugfile_switch)
  1140. { my_free
  1141. (&symbol_debug_backpatch_positions,
  1142. "symbol debug information backpatch positions");
  1143. my_free
  1144. (&replacement_debug_backpatch_positions,
  1145. "replacement debug information backpatch positions");
  1146. }
  1147. my_free(&next_entry, "symbol linked-list forward links");
  1148. my_free(&start_of_list, "hash code list beginnings");
  1149. if (symbol_replacements)
  1150. my_free(&symbol_replacements, "symbol replacement table");
  1151. if (df_symbol_map) {
  1152. for (i=0; i<DF_SYMBOL_HASH_BUCKETS; i++) {
  1153. df_reference_t *ent = df_symbol_map[i];
  1154. while (ent) {
  1155. df_reference_t *next = ent->next;
  1156. my_free(&ent, "df symbol map entry");
  1157. ent = next;
  1158. }
  1159. }
  1160. my_free(&df_symbol_map, "df symbol-map hash table");
  1161. }
  1162. if (df_functions) {
  1163. for (i=0; i<DF_FUNCTION_HASH_BUCKETS; i++) {
  1164. df_function_t *func = df_functions[i];
  1165. while (func) {
  1166. df_function_t *next = func->next;
  1167. my_free(&func, "df function entry");
  1168. func = next;
  1169. }
  1170. }
  1171. my_free(&df_functions, "df function hash table");
  1172. df_functions_head = NULL;
  1173. df_functions_tail = NULL;
  1174. }
  1175. if (individual_name_strings != NULL)
  1176. my_free(&individual_name_strings, "property name strings");
  1177. if (action_name_strings != NULL)
  1178. my_free(&action_name_strings, "action name strings");
  1179. if (attribute_name_strings != NULL)
  1180. my_free(&attribute_name_strings, "attribute name strings");
  1181. if (array_name_strings != NULL)
  1182. my_free(&array_name_strings, "array name strings");
  1183. }
  1184. /* ========================================================================= */