input-scrub.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /* input_scrub.c - Break up input buffers into whole numbers of lines.
  2. Copyright (C) 1987-2022 Free Software Foundation, Inc.
  3. This file is part of GAS, the GNU Assembler.
  4. GAS is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. GAS is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GAS; see the file COPYING. If not, write to the Free
  14. Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
  15. 02110-1301, USA. */
  16. #include "as.h"
  17. #include "filenames.h"
  18. #include "input-file.h"
  19. #include "sb.h"
  20. #include "listing.h"
  21. /*
  22. * O/S independent module to supply buffers of sanitised source code
  23. * to rest of assembler. We get sanitised input data of arbitrary length.
  24. * We break these buffers on line boundaries, recombine pieces that
  25. * were broken across buffers, and return a buffer of full lines to
  26. * the caller.
  27. * The last partial line begins the next buffer we build and return to caller.
  28. * The buffer returned to caller is preceded by BEFORE_STRING and followed
  29. * by AFTER_STRING, as sentinels. The last character before AFTER_STRING
  30. * is a newline.
  31. * Also looks after line numbers, for e.g. error messages.
  32. */
  33. /*
  34. * We don't care how filthy our buffers are, but our callers assume
  35. * that the following sanitation has already been done.
  36. *
  37. * No comments, reduce a comment to a space.
  38. * Reduce a tab to a space unless it is 1st char of line.
  39. * All multiple tabs and spaces collapsed into 1 char. Tab only
  40. * legal if 1st char of line.
  41. * # line file statements converted to .line x;.file y; statements.
  42. * Escaped newlines at end of line: remove them but add as many newlines
  43. * to end of statement as you removed in the middle, to synch line numbers.
  44. */
  45. #define BEFORE_STRING ("\n")
  46. #define AFTER_STRING ("\0") /* memcpy of 0 chars might choke. */
  47. #define BEFORE_SIZE (1)
  48. #define AFTER_SIZE (1)
  49. #ifndef TC_EOL_IN_INSN
  50. #define TC_EOL_IN_INSN(P) 0
  51. #endif
  52. static char *buffer_start; /*->1st char of full buffer area. */
  53. static char *partial_where; /*->after last full line in buffer. */
  54. static size_t partial_size; /* >=0. Number of chars in partial line in buffer. */
  55. /* Because we need AFTER_STRING just after last full line, it clobbers
  56. 1st part of partial line. So we preserve 1st part of partial line
  57. here. */
  58. static char save_source[AFTER_SIZE];
  59. /* The size of the input buffer we concatenate
  60. input_file_give_next_buffer chunks into. Excludes the BEFORE and
  61. AFTER counts. */
  62. static size_t buffer_length;
  63. /* The index into an sb structure we are reading from. -1 if none. */
  64. static size_t sb_index = -1;
  65. /* If we are reading from an sb structure, this is it. */
  66. static sb from_sb;
  67. /* Should we do a conditional check on from_sb? */
  68. static int from_sb_is_expansion = 1;
  69. /* The number of nested sb structures we have included. */
  70. int macro_nest;
  71. /* We can have more than one source file open at once, though the info for all
  72. but the latest one are saved off in a struct input_save. These files remain
  73. open, so we are limited by the number of open files allowed by the
  74. underlying OS. We may also sequentially read more than one source file in an
  75. assembly. */
  76. /* We must track the physical file and line number for error messages. We also
  77. track a "logical" file and line number corresponding to (C?) compiler
  78. source line numbers. Whenever we open a file we must fill in
  79. physical_input_file. So if it is NULL we have not opened any files yet. */
  80. static const char *physical_input_file;
  81. static const char *logical_input_file;
  82. /* 1-origin line number in a source file. */
  83. /* A line ends in '\n' or eof. */
  84. static unsigned int physical_input_line;
  85. static unsigned int logical_input_line;
  86. /* Struct used to save the state of the input handler during include files */
  87. struct input_save {
  88. char * buffer_start;
  89. char * partial_where;
  90. size_t partial_size;
  91. char save_source[AFTER_SIZE];
  92. size_t buffer_length;
  93. const char * physical_input_file;
  94. const char * logical_input_file;
  95. unsigned int physical_input_line;
  96. unsigned int logical_input_line;
  97. size_t sb_index;
  98. sb from_sb;
  99. int from_sb_is_expansion; /* Should we do a conditional check? */
  100. struct input_save * next_saved_file; /* Chain of input_saves. */
  101. char * input_file_save; /* Saved state of input routines. */
  102. char * saved_position; /* Caller's saved position in buf. */
  103. };
  104. static struct input_save *input_scrub_push (char *saved_position);
  105. static char *input_scrub_pop (struct input_save *arg);
  106. /* Saved information about the file that .include'd this one. When we hit EOF,
  107. we automatically pop to that file. */
  108. static struct input_save *next_saved_file;
  109. /* Initialize input buffering. */
  110. static void
  111. input_scrub_reinit (void)
  112. {
  113. input_file_begin (); /* Reinitialize! */
  114. logical_input_line = -1u;
  115. logical_input_file = NULL;
  116. buffer_length = input_file_buffer_size () * 2;
  117. buffer_start = XNEWVEC (char, BEFORE_SIZE + AFTER_SIZE + 1 + buffer_length);
  118. memcpy (buffer_start, BEFORE_STRING, (int) BEFORE_SIZE);
  119. }
  120. /* Push the state of input reading and scrubbing so that we can #include.
  121. The return value is a 'void *' (fudged for old compilers) to a save
  122. area, which can be restored by passing it to input_scrub_pop(). */
  123. static struct input_save *
  124. input_scrub_push (char *saved_position)
  125. {
  126. struct input_save *saved;
  127. saved = XNEW (struct input_save);
  128. saved->saved_position = saved_position;
  129. saved->buffer_start = buffer_start;
  130. saved->partial_where = partial_where;
  131. saved->partial_size = partial_size;
  132. saved->buffer_length = buffer_length;
  133. saved->physical_input_file = physical_input_file;
  134. saved->logical_input_file = logical_input_file;
  135. saved->physical_input_line = physical_input_line;
  136. saved->logical_input_line = logical_input_line;
  137. saved->sb_index = sb_index;
  138. saved->from_sb = from_sb;
  139. saved->from_sb_is_expansion = from_sb_is_expansion;
  140. memcpy (saved->save_source, save_source, sizeof (save_source));
  141. saved->next_saved_file = next_saved_file;
  142. saved->input_file_save = input_file_push ();
  143. sb_index = -1;
  144. input_scrub_reinit ();
  145. return saved;
  146. }
  147. static char *
  148. input_scrub_pop (struct input_save *saved)
  149. {
  150. char *saved_position;
  151. input_scrub_end (); /* Finish off old buffer */
  152. input_file_pop (saved->input_file_save);
  153. saved_position = saved->saved_position;
  154. buffer_start = saved->buffer_start;
  155. buffer_length = saved->buffer_length;
  156. physical_input_file = saved->physical_input_file;
  157. logical_input_file = saved->logical_input_file;
  158. physical_input_line = saved->physical_input_line;
  159. logical_input_line = saved->logical_input_line;
  160. sb_index = saved->sb_index;
  161. from_sb = saved->from_sb;
  162. from_sb_is_expansion = saved->from_sb_is_expansion;
  163. partial_where = saved->partial_where;
  164. partial_size = saved->partial_size;
  165. next_saved_file = saved->next_saved_file;
  166. memcpy (save_source, saved->save_source, sizeof (save_source));
  167. free (saved);
  168. return saved_position;
  169. }
  170. void
  171. input_scrub_begin (void)
  172. {
  173. know (strlen (BEFORE_STRING) == BEFORE_SIZE);
  174. know (strlen (AFTER_STRING) == AFTER_SIZE
  175. || (AFTER_STRING[0] == '\0' && AFTER_SIZE == 1));
  176. physical_input_file = NULL; /* No file read yet. */
  177. next_saved_file = NULL; /* At EOF, don't pop to any other file */
  178. input_scrub_reinit ();
  179. do_scrub_begin (flag_m68k_mri);
  180. }
  181. void
  182. input_scrub_end (void)
  183. {
  184. if (buffer_start)
  185. {
  186. free (buffer_start);
  187. buffer_start = 0;
  188. input_file_end ();
  189. }
  190. }
  191. /* Start reading input from a new file.
  192. Return start of caller's part of buffer. */
  193. char *
  194. input_scrub_new_file (const char *filename)
  195. {
  196. input_file_open (filename, !flag_no_comments);
  197. physical_input_file = filename[0] ? filename : _("{standard input}");
  198. physical_input_line = 0;
  199. partial_size = 0;
  200. return (buffer_start + BEFORE_SIZE);
  201. }
  202. /* Include a file from the current file. Save our state, cause it to
  203. be restored on EOF, and begin handling a new file. Same result as
  204. input_scrub_new_file. */
  205. char *
  206. input_scrub_include_file (const char *filename, char *position)
  207. {
  208. next_saved_file = input_scrub_push (position);
  209. return input_scrub_new_file (filename);
  210. }
  211. /* Start getting input from an sb structure. This is used when
  212. expanding a macro. */
  213. void
  214. input_scrub_include_sb (sb *from, char *position, int is_expansion)
  215. {
  216. int newline;
  217. if (macro_nest > max_macro_nest)
  218. as_fatal (_("macros nested too deeply"));
  219. ++macro_nest;
  220. #ifdef md_macro_start
  221. if (is_expansion)
  222. {
  223. md_macro_start ();
  224. }
  225. #endif
  226. next_saved_file = input_scrub_push (position);
  227. /* Allocate sufficient space: from->len + optional newline. */
  228. newline = from->len >= 1 && from->ptr[0] != '\n';
  229. sb_build (&from_sb, from->len + newline);
  230. from_sb_is_expansion = is_expansion;
  231. if (newline)
  232. {
  233. /* Add the sentinel required by read.c. */
  234. sb_add_char (&from_sb, '\n');
  235. }
  236. sb_scrub_and_add_sb (&from_sb, from);
  237. /* Make sure the parser looks at defined contents when it scans for
  238. e.g. end-of-line at the end of a macro. */
  239. sb_terminate (&from_sb);
  240. sb_index = 1;
  241. /* These variables are reset by input_scrub_push. Restore them
  242. since we are, after all, still at the same point in the file. */
  243. logical_input_line = next_saved_file->logical_input_line;
  244. logical_input_file = next_saved_file->logical_input_file;
  245. }
  246. void
  247. input_scrub_close (void)
  248. {
  249. input_file_close ();
  250. physical_input_line = 0;
  251. logical_input_line = -1u;
  252. }
  253. char *
  254. input_scrub_next_buffer (char **bufp)
  255. {
  256. char *limit; /*->just after last char of buffer. */
  257. if (sb_index != (size_t) -1)
  258. {
  259. if (sb_index >= from_sb.len)
  260. {
  261. sb_kill (&from_sb);
  262. if (from_sb_is_expansion)
  263. {
  264. cond_finish_check (macro_nest);
  265. #ifdef md_macro_end
  266. /* Allow the target to clean up per-macro expansion
  267. data. */
  268. md_macro_end ();
  269. #endif
  270. }
  271. --macro_nest;
  272. partial_where = NULL;
  273. partial_size = 0;
  274. if (next_saved_file != NULL)
  275. *bufp = input_scrub_pop (next_saved_file);
  276. return partial_where;
  277. }
  278. partial_where = from_sb.ptr + from_sb.len;
  279. partial_size = 0;
  280. *bufp = from_sb.ptr + sb_index;
  281. sb_index = from_sb.len;
  282. return partial_where;
  283. }
  284. if (partial_size)
  285. {
  286. memmove (buffer_start + BEFORE_SIZE, partial_where, partial_size);
  287. memcpy (buffer_start + BEFORE_SIZE, save_source, AFTER_SIZE);
  288. }
  289. while (1)
  290. {
  291. char *p;
  292. char *start = buffer_start + BEFORE_SIZE + partial_size;
  293. *bufp = buffer_start + BEFORE_SIZE;
  294. limit = input_file_give_next_buffer (start);
  295. if (!limit)
  296. {
  297. if (!partial_size)
  298. /* End of this file. */
  299. break;
  300. as_warn (_("end of file not at end of a line; newline inserted"));
  301. p = buffer_start + BEFORE_SIZE + partial_size;
  302. *p++ = '\n';
  303. limit = p;
  304. }
  305. else
  306. {
  307. /* Terminate the buffer to avoid confusing TC_EOL_IN_INSN. */
  308. *limit = '\0';
  309. /* Find last newline. */
  310. for (p = limit - 1; *p != '\n' || TC_EOL_IN_INSN (p); --p)
  311. if (p < start)
  312. goto read_more;
  313. ++p;
  314. }
  315. if (multibyte_handling == multibyte_warn)
  316. (void) scan_for_multibyte_characters ((const unsigned char *) p,
  317. (const unsigned char *) limit,
  318. true /* Generate warnings */);
  319. /* We found a newline in the newly read chars. */
  320. partial_where = p;
  321. partial_size = limit - p;
  322. /* Save the fragment after that last newline. */
  323. memcpy (save_source, partial_where, (int) AFTER_SIZE);
  324. memcpy (partial_where, AFTER_STRING, (int) AFTER_SIZE);
  325. return partial_where;
  326. read_more:
  327. /* Didn't find a newline. Read more text. */
  328. partial_size = limit - (buffer_start + BEFORE_SIZE);
  329. if (buffer_length - input_file_buffer_size () < partial_size)
  330. {
  331. /* Increase the buffer when it doesn't have room for the
  332. next block of input. */
  333. buffer_length *= 2;
  334. buffer_start = XRESIZEVEC (char, buffer_start,
  335. (buffer_length
  336. + BEFORE_SIZE + AFTER_SIZE + 1));
  337. }
  338. }
  339. /* Tell the listing we've finished the file. */
  340. LISTING_EOF ();
  341. /* If we should pop to another file at EOF, do it. */
  342. partial_where = NULL;
  343. if (next_saved_file)
  344. *bufp = input_scrub_pop (next_saved_file);
  345. return partial_where;
  346. }
  347. /* The remaining part of this file deals with line numbers, error
  348. messages and so on. Return TRUE if we opened any file. */
  349. int
  350. seen_at_least_1_file (void)
  351. {
  352. return (physical_input_file != NULL);
  353. }
  354. void
  355. bump_line_counters (void)
  356. {
  357. if (sb_index == (size_t) -1)
  358. ++physical_input_line;
  359. if (logical_input_line != -1u)
  360. ++logical_input_line;
  361. }
  362. /* Tells us what the new logical line number and file are.
  363. If the line_number is -1, we don't change the current logical line
  364. number. If it is -2, we decrement the logical line number (this is
  365. to support the .appfile pseudo-op inserted into the stream by
  366. do_scrub_chars).
  367. If the fname is NULL, we don't change the current logical file name.
  368. Returns nonzero if the filename actually changes. */
  369. int
  370. new_logical_line_flags (const char *fname, /* DON'T destroy it! We point to it! */
  371. int line_number,
  372. int flags)
  373. {
  374. switch (flags)
  375. {
  376. case 0:
  377. break;
  378. case 1:
  379. if (line_number != -1)
  380. abort ();
  381. break;
  382. case 1 << 1:
  383. case 1 << 2:
  384. /* FIXME: we could check that include nesting is correct. */
  385. break;
  386. default:
  387. abort ();
  388. }
  389. if (line_number >= 0)
  390. logical_input_line = line_number;
  391. else if (line_number == -1 && fname && !*fname && (flags & (1 << 2)))
  392. {
  393. logical_input_file = physical_input_file;
  394. logical_input_line = physical_input_line;
  395. fname = NULL;
  396. }
  397. if (fname
  398. && (logical_input_file == NULL
  399. || filename_cmp (logical_input_file, fname)))
  400. {
  401. logical_input_file = fname;
  402. return 1;
  403. }
  404. else
  405. return 0;
  406. }
  407. int
  408. new_logical_line (const char *fname, int line_number)
  409. {
  410. return new_logical_line_flags (fname, line_number, 0);
  411. }
  412. /* Return the current physical input file name and line number, if known */
  413. const char *
  414. as_where_physical (unsigned int *linep)
  415. {
  416. if (physical_input_file != NULL)
  417. {
  418. if (linep != NULL)
  419. *linep = physical_input_line;
  420. return physical_input_file;
  421. }
  422. if (linep != NULL)
  423. *linep = 0;
  424. return NULL;
  425. }
  426. /* Return the current file name and line number. */
  427. const char *
  428. as_where (unsigned int *linep)
  429. {
  430. if (logical_input_file != NULL
  431. && (linep == NULL || logical_input_line != -1u))
  432. {
  433. if (linep != NULL)
  434. *linep = logical_input_line;
  435. return logical_input_file;
  436. }
  437. return as_where_physical (linep);
  438. }