aicasm_scan.l 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. %{
  2. /*
  3. * Lexical Analyzer for the Aic7xxx SCSI Host adapter sequencer assembler.
  4. *
  5. * Copyright (c) 1997, 1998, 2000 Justin T. Gibbs.
  6. * Copyright (c) 2001, 2002 Adaptec Inc.
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions, and the following disclaimer,
  14. * without modification.
  15. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  16. * substantially similar to the "NO WARRANTY" disclaimer below
  17. * ("Disclaimer") and any redistribution must be conditioned upon
  18. * including a substantially similar Disclaimer requirement for further
  19. * binary redistribution.
  20. * 3. Neither the names of the above-listed copyright holders nor the names
  21. * of any contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * Alternatively, this software may be distributed under the terms of the
  25. * GNU General Public License ("GPL") version 2 as published by the Free
  26. * Software Foundation.
  27. *
  28. * NO WARRANTY
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  30. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  31. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  32. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  33. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  34. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  35. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  36. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  37. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  38. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  39. * POSSIBILITY OF SUCH DAMAGES.
  40. *
  41. * $Id: //depot/aic7xxx/aic7xxx/aicasm/aicasm_scan.l#19 $
  42. *
  43. * $FreeBSD$
  44. */
  45. #include <sys/types.h>
  46. #include <inttypes.h>
  47. #include <limits.h>
  48. #include <regex.h>
  49. #include <stdio.h>
  50. #include <string.h>
  51. #include <sysexits.h>
  52. #ifdef __linux__
  53. #include "../queue.h"
  54. #else
  55. #include <sys/queue.h>
  56. #endif
  57. #include "aicasm.h"
  58. #include "aicasm_symbol.h"
  59. #include "aicasm_gram.h"
  60. /* This is used for macro body capture too, so err on the large size. */
  61. #define MAX_STR_CONST 4096
  62. static char string_buf[MAX_STR_CONST];
  63. static char *string_buf_ptr;
  64. static int parren_count;
  65. static int quote_count;
  66. static char buf[255];
  67. %}
  68. PATH ([/]*[-A-Za-z0-9_.])+
  69. WORD [A-Za-z_][-A-Za-z_0-9]*
  70. SPACE [ \t]+
  71. MCARG [^(), \t]+
  72. MBODY ((\\[^\n])*[^\n\\]*)+
  73. %x COMMENT
  74. %x CEXPR
  75. %x INCLUDE
  76. %x STRING
  77. %x MACRODEF
  78. %x MACROARGLIST
  79. %x MACROCALLARGS
  80. %x MACROBODY
  81. %%
  82. \n { ++yylineno; }
  83. \r ;
  84. "/*" { BEGIN COMMENT; /* Enter comment eating state */ }
  85. <COMMENT>"/*" { fprintf(stderr, "Warning! Comment within comment."); }
  86. <COMMENT>\n { ++yylineno; }
  87. <COMMENT>[^*/\n]* ;
  88. <COMMENT>"*"+[^*/\n]* ;
  89. <COMMENT>"/"+[^*/\n]* ;
  90. <COMMENT>"*"+"/" { BEGIN INITIAL; }
  91. if[ \t]*\( {
  92. string_buf_ptr = string_buf;
  93. parren_count = 1;
  94. BEGIN CEXPR;
  95. return T_IF;
  96. }
  97. <CEXPR>\( { *string_buf_ptr++ = '('; parren_count++; }
  98. <CEXPR>\) {
  99. parren_count--;
  100. if (parren_count == 0) {
  101. /* All done */
  102. BEGIN INITIAL;
  103. *string_buf_ptr = '\0';
  104. yylval.sym = symtable_get(string_buf);
  105. return T_CEXPR;
  106. } else {
  107. *string_buf_ptr++ = ')';
  108. }
  109. }
  110. <CEXPR>\n { ++yylineno; }
  111. <CEXPR>\r ;
  112. <CEXPR>[^()\n]+ {
  113. char *yptr;
  114. yptr = yytext;
  115. while (*yptr != '\0') {
  116. /* Remove duplicate spaces */
  117. if (*yptr == '\t')
  118. *yptr = ' ';
  119. if (*yptr == ' '
  120. && string_buf_ptr != string_buf
  121. && string_buf_ptr[-1] == ' ')
  122. yptr++;
  123. else
  124. *string_buf_ptr++ = *yptr++;
  125. }
  126. }
  127. VERSION { return T_VERSION; }
  128. PREFIX { return T_PREFIX; }
  129. PATCH_ARG_LIST { return T_PATCH_ARG_LIST; }
  130. \" {
  131. string_buf_ptr = string_buf;
  132. BEGIN STRING;
  133. }
  134. <STRING>[^"]+ {
  135. char *yptr;
  136. yptr = yytext;
  137. while (*yptr)
  138. *string_buf_ptr++ = *yptr++;
  139. }
  140. <STRING>\" {
  141. /* All done */
  142. BEGIN INITIAL;
  143. *string_buf_ptr = '\0';
  144. yylval.str = string_buf;
  145. return T_STRING;
  146. }
  147. {SPACE} ;
  148. /* Register/SCB/SRAM definition keywords */
  149. export { return T_EXPORT; }
  150. register { return T_REGISTER; }
  151. const { yylval.value = FALSE; return T_CONST; }
  152. download { return T_DOWNLOAD; }
  153. address { return T_ADDRESS; }
  154. access_mode { return T_ACCESS_MODE; }
  155. modes { return T_MODES; }
  156. RW|RO|WO {
  157. if (strcmp(yytext, "RW") == 0)
  158. yylval.value = RW;
  159. else if (strcmp(yytext, "RO") == 0)
  160. yylval.value = RO;
  161. else
  162. yylval.value = WO;
  163. return T_MODE;
  164. }
  165. BEGIN_CRITICAL { return T_BEGIN_CS; }
  166. END_CRITICAL { return T_END_CS; }
  167. SET_SRC_MODE { return T_SET_SRC_MODE; }
  168. SET_DST_MODE { return T_SET_DST_MODE; }
  169. field { return T_FIELD; }
  170. enum { return T_ENUM; }
  171. mask { return T_MASK; }
  172. alias { return T_ALIAS; }
  173. size { return T_SIZE; }
  174. scb { return T_SCB; }
  175. scratch_ram { return T_SRAM; }
  176. accumulator { return T_ACCUM; }
  177. mode_pointer { return T_MODE_PTR; }
  178. allones { return T_ALLONES; }
  179. allzeros { return T_ALLZEROS; }
  180. none { return T_NONE; }
  181. sindex { return T_SINDEX; }
  182. A { return T_A; }
  183. /* Opcodes */
  184. shl { return T_SHL; }
  185. shr { return T_SHR; }
  186. ror { return T_ROR; }
  187. rol { return T_ROL; }
  188. mvi { return T_MVI; }
  189. mov { return T_MOV; }
  190. clr { return T_CLR; }
  191. jmp { return T_JMP; }
  192. jc { return T_JC; }
  193. jnc { return T_JNC; }
  194. je { return T_JE; }
  195. jne { return T_JNE; }
  196. jz { return T_JZ; }
  197. jnz { return T_JNZ; }
  198. call { return T_CALL; }
  199. add { return T_ADD; }
  200. adc { return T_ADC; }
  201. bmov { return T_BMOV; }
  202. inc { return T_INC; }
  203. dec { return T_DEC; }
  204. stc { return T_STC; }
  205. clc { return T_CLC; }
  206. cmp { return T_CMP; }
  207. not { return T_NOT; }
  208. xor { return T_XOR; }
  209. test { return T_TEST;}
  210. and { return T_AND; }
  211. or { return T_OR; }
  212. ret { return T_RET; }
  213. nop { return T_NOP; }
  214. else { return T_ELSE; }
  215. /* Allowed Symbols */
  216. \<\< { return T_EXPR_LSHIFT; }
  217. \>\> { return T_EXPR_RSHIFT; }
  218. [-+,:()~|&."{};<>[\]/*!=] { return yytext[0]; }
  219. /* Number processing */
  220. 0[0-7]* {
  221. yylval.value = strtol(yytext, NULL, 8);
  222. return T_NUMBER;
  223. }
  224. 0[xX][0-9a-fA-F]+ {
  225. yylval.value = strtoul(yytext + 2, NULL, 16);
  226. return T_NUMBER;
  227. }
  228. [1-9][0-9]* {
  229. yylval.value = strtol(yytext, NULL, 10);
  230. return T_NUMBER;
  231. }
  232. /* Include Files */
  233. #include{SPACE} {
  234. BEGIN INCLUDE;
  235. quote_count = 0;
  236. return T_INCLUDE;
  237. }
  238. <INCLUDE>[<] { return yytext[0]; }
  239. <INCLUDE>[>] { BEGIN INITIAL; return yytext[0]; }
  240. <INCLUDE>[\"] {
  241. if (quote_count != 0)
  242. BEGIN INITIAL;
  243. quote_count++;
  244. return yytext[0];
  245. }
  246. <INCLUDE>{PATH} {
  247. char *yptr;
  248. yptr = yytext;
  249. string_buf_ptr = string_buf;
  250. while (*yptr)
  251. *string_buf_ptr++ = *yptr++;
  252. yylval.str = string_buf;
  253. *string_buf_ptr = '\0';
  254. return T_PATH;
  255. }
  256. <INCLUDE>. { stop("Invalid include line", EX_DATAERR); }
  257. #define{SPACE} {
  258. BEGIN MACRODEF;
  259. return T_DEFINE;
  260. }
  261. <MACRODEF>{WORD}{SPACE} {
  262. char *yptr;
  263. /* Strip space and return as a normal symbol */
  264. yptr = yytext;
  265. while (*yptr != ' ' && *yptr != '\t')
  266. yptr++;
  267. *yptr = '\0';
  268. yylval.sym = symtable_get(yytext);
  269. string_buf_ptr = string_buf;
  270. BEGIN MACROBODY;
  271. return T_SYMBOL;
  272. }
  273. <MACRODEF>{WORD}\( {
  274. /*
  275. * We store the symbol with its opening
  276. * parren so we can differentiate macros
  277. * that take args from macros with the
  278. * same name that do not take args as
  279. * is allowed in C.
  280. */
  281. BEGIN MACROARGLIST;
  282. yylval.sym = symtable_get(yytext);
  283. unput('(');
  284. return T_SYMBOL;
  285. }
  286. <MACROARGLIST>{WORD} {
  287. yylval.str = yytext;
  288. return T_ARG;
  289. }
  290. <MACROARGLIST>{SPACE} ;
  291. <MACROARGLIST>[(,] {
  292. return yytext[0];
  293. }
  294. <MACROARGLIST>[)] {
  295. string_buf_ptr = string_buf;
  296. BEGIN MACROBODY;
  297. return ')';
  298. }
  299. <MACROARGLIST>. {
  300. snprintf(buf, sizeof(buf), "Invalid character "
  301. "'%c' in macro argument list",
  302. yytext[0]);
  303. stop(buf, EX_DATAERR);
  304. }
  305. <MACROCALLARGS>{SPACE} ;
  306. <MACROCALLARGS>\( {
  307. parren_count++;
  308. if (parren_count == 1)
  309. return ('(');
  310. *string_buf_ptr++ = '(';
  311. }
  312. <MACROCALLARGS>\) {
  313. parren_count--;
  314. if (parren_count == 0) {
  315. BEGIN INITIAL;
  316. return (')');
  317. }
  318. *string_buf_ptr++ = ')';
  319. }
  320. <MACROCALLARGS>{MCARG} {
  321. char *yptr;
  322. yptr = yytext;
  323. while (*yptr)
  324. *string_buf_ptr++ = *yptr++;
  325. }
  326. <MACROCALLARGS>\, {
  327. if (string_buf_ptr != string_buf) {
  328. /*
  329. * Return an argument and
  330. * rescan this comma so we
  331. * can return it as well.
  332. */
  333. *string_buf_ptr = '\0';
  334. yylval.str = string_buf;
  335. string_buf_ptr = string_buf;
  336. unput(',');
  337. return T_ARG;
  338. }
  339. return ',';
  340. }
  341. <MACROBODY>\\\n {
  342. /* Eat escaped newlines. */
  343. ++yylineno;
  344. }
  345. <MACROBODY>\r ;
  346. <MACROBODY>\n {
  347. /* Macros end on the first unescaped newline. */
  348. BEGIN INITIAL;
  349. *string_buf_ptr = '\0';
  350. yylval.str = string_buf;
  351. ++yylineno;
  352. return T_MACROBODY;
  353. }
  354. <MACROBODY>{MBODY} {
  355. char *yptr;
  356. char c;
  357. yptr = yytext;
  358. while (c = *yptr++) {
  359. /*
  360. * Strip carriage returns.
  361. */
  362. if (c == '\r')
  363. continue;
  364. *string_buf_ptr++ = c;
  365. }
  366. }
  367. {WORD}\( {
  368. char *yptr;
  369. char *ycopy;
  370. /* May be a symbol or a macro invocation. */
  371. yylval.sym = symtable_get(yytext);
  372. if (yylval.sym->type == MACRO) {
  373. YY_BUFFER_STATE old_state;
  374. YY_BUFFER_STATE temp_state;
  375. ycopy = strdup(yytext);
  376. yptr = ycopy + yyleng;
  377. while (yptr > ycopy)
  378. unput(*--yptr);
  379. old_state = YY_CURRENT_BUFFER;
  380. temp_state =
  381. yy_create_buffer(stdin,
  382. YY_BUF_SIZE);
  383. yy_switch_to_buffer(temp_state);
  384. mm_switch_to_buffer(old_state);
  385. mmparse();
  386. mm_switch_to_buffer(temp_state);
  387. yy_switch_to_buffer(old_state);
  388. mm_delete_buffer(temp_state);
  389. expand_macro(yylval.sym);
  390. } else {
  391. if (yylval.sym->type == UNINITIALIZED) {
  392. /* Try without the '(' */
  393. symbol_delete(yylval.sym);
  394. yytext[yyleng-1] = '\0';
  395. yylval.sym =
  396. symtable_get(yytext);
  397. }
  398. unput('(');
  399. return T_SYMBOL;
  400. }
  401. }
  402. {WORD} {
  403. yylval.sym = symtable_get(yytext);
  404. if (yylval.sym->type == MACRO) {
  405. expand_macro(yylval.sym);
  406. } else {
  407. return T_SYMBOL;
  408. }
  409. }
  410. . {
  411. snprintf(buf, sizeof(buf), "Invalid character "
  412. "'%c'", yytext[0]);
  413. stop(buf, EX_DATAERR);
  414. }
  415. %%
  416. typedef struct include {
  417. YY_BUFFER_STATE buffer;
  418. int lineno;
  419. char *filename;
  420. SLIST_ENTRY(include) links;
  421. }include_t;
  422. SLIST_HEAD(, include) include_stack;
  423. void
  424. include_file(char *file_name, include_type type)
  425. {
  426. FILE *newfile;
  427. include_t *include;
  428. newfile = NULL;
  429. /* Try the current directory first */
  430. if (includes_search_curdir != 0 || type == SOURCE_FILE)
  431. newfile = fopen(file_name, "r");
  432. if (newfile == NULL && type != SOURCE_FILE) {
  433. path_entry_t include_dir;
  434. for (include_dir = search_path.slh_first;
  435. include_dir != NULL;
  436. include_dir = include_dir->links.sle_next) {
  437. char fullname[PATH_MAX];
  438. if ((include_dir->quoted_includes_only == TRUE)
  439. && (type != QUOTED_INCLUDE))
  440. continue;
  441. snprintf(fullname, sizeof(fullname),
  442. "%s/%s", include_dir->directory, file_name);
  443. if ((newfile = fopen(fullname, "r")) != NULL)
  444. break;
  445. }
  446. }
  447. if (newfile == NULL) {
  448. perror(file_name);
  449. stop("Unable to open input file", EX_SOFTWARE);
  450. /* NOTREACHED */
  451. }
  452. if (type != SOURCE_FILE) {
  453. include = (include_t *)malloc(sizeof(include_t));
  454. if (include == NULL) {
  455. stop("Unable to allocate include stack entry",
  456. EX_SOFTWARE);
  457. /* NOTREACHED */
  458. }
  459. include->buffer = YY_CURRENT_BUFFER;
  460. include->lineno = yylineno;
  461. include->filename = yyfilename;
  462. SLIST_INSERT_HEAD(&include_stack, include, links);
  463. }
  464. yy_switch_to_buffer(yy_create_buffer(newfile, YY_BUF_SIZE));
  465. yylineno = 1;
  466. yyfilename = strdup(file_name);
  467. }
  468. static void next_substitution(struct symbol *mac_symbol, const char *body_pos,
  469. const char **next_match,
  470. struct macro_arg **match_marg, regmatch_t *match);
  471. void
  472. expand_macro(struct symbol *macro_symbol)
  473. {
  474. struct macro_arg *marg;
  475. struct macro_arg *match_marg;
  476. const char *body_head;
  477. const char *body_pos;
  478. const char *next_match;
  479. /*
  480. * Due to the nature of unput, we must work
  481. * backwards through the macro body performing
  482. * any expansions.
  483. */
  484. body_head = macro_symbol->info.macroinfo->body;
  485. body_pos = body_head + strlen(body_head);
  486. while (body_pos > body_head) {
  487. regmatch_t match;
  488. next_match = body_head;
  489. match_marg = NULL;
  490. next_substitution(macro_symbol, body_pos, &next_match,
  491. &match_marg, &match);
  492. /* Put back everything up until the replacement. */
  493. while (body_pos > next_match)
  494. unput(*--body_pos);
  495. /* Perform the replacement. */
  496. if (match_marg != NULL) {
  497. const char *strp;
  498. next_match = match_marg->replacement_text;
  499. strp = next_match + strlen(next_match);
  500. while (strp > next_match)
  501. unput(*--strp);
  502. /* Skip past the unexpanded macro arg. */
  503. body_pos -= match.rm_eo - match.rm_so;
  504. }
  505. }
  506. /* Cleanup replacement text. */
  507. STAILQ_FOREACH(marg, &macro_symbol->info.macroinfo->args, links) {
  508. free(marg->replacement_text);
  509. }
  510. }
  511. /*
  512. * Find the next substitution in the macro working backwards from
  513. * body_pos until the beginning of the macro buffer. next_match
  514. * should be initialized to the beginning of the macro buffer prior
  515. * to calling this routine.
  516. */
  517. static void
  518. next_substitution(struct symbol *mac_symbol, const char *body_pos,
  519. const char **next_match, struct macro_arg **match_marg,
  520. regmatch_t *match)
  521. {
  522. regmatch_t matches[2];
  523. struct macro_arg *marg;
  524. const char *search_pos;
  525. int retval;
  526. do {
  527. search_pos = *next_match;
  528. STAILQ_FOREACH(marg, &mac_symbol->info.macroinfo->args, links) {
  529. retval = regexec(&marg->arg_regex, search_pos, 2,
  530. matches, 0);
  531. if (retval == 0
  532. && (matches[1].rm_eo + search_pos) <= body_pos
  533. && (matches[1].rm_eo + search_pos) > *next_match) {
  534. *match = matches[1];
  535. *next_match = match->rm_eo + search_pos;
  536. *match_marg = marg;
  537. }
  538. }
  539. } while (search_pos != *next_match);
  540. }
  541. int
  542. yywrap()
  543. {
  544. include_t *include;
  545. yy_delete_buffer(YY_CURRENT_BUFFER);
  546. (void)fclose(yyin);
  547. if (yyfilename != NULL)
  548. free(yyfilename);
  549. yyfilename = NULL;
  550. include = include_stack.slh_first;
  551. if (include != NULL) {
  552. yy_switch_to_buffer(include->buffer);
  553. yylineno = include->lineno;
  554. yyfilename = include->filename;
  555. SLIST_REMOVE_HEAD(&include_stack, links);
  556. free(include);
  557. return (0);
  558. }
  559. return (1);
  560. }