aicasm_scan.l 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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#20 $
  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. else { return T_ELSE; }
  128. VERSION { return T_VERSION; }
  129. PREFIX { return T_PREFIX; }
  130. PATCH_ARG_LIST { return T_PATCH_ARG_LIST; }
  131. \" {
  132. string_buf_ptr = string_buf;
  133. BEGIN STRING;
  134. }
  135. <STRING>[^"]+ {
  136. char *yptr;
  137. yptr = yytext;
  138. while (*yptr)
  139. *string_buf_ptr++ = *yptr++;
  140. }
  141. <STRING>\" {
  142. /* All done */
  143. BEGIN INITIAL;
  144. *string_buf_ptr = '\0';
  145. yylval.str = string_buf;
  146. return T_STRING;
  147. }
  148. {SPACE} ;
  149. /* Register/SCB/SRAM definition keywords */
  150. export { return T_EXPORT; }
  151. register { return T_REGISTER; }
  152. const { yylval.value = FALSE; return T_CONST; }
  153. download { return T_DOWNLOAD; }
  154. address { return T_ADDRESS; }
  155. count { return T_COUNT; }
  156. access_mode { return T_ACCESS_MODE; }
  157. modes { return T_MODES; }
  158. RW|RO|WO {
  159. if (strcmp(yytext, "RW") == 0)
  160. yylval.value = RW;
  161. else if (strcmp(yytext, "RO") == 0)
  162. yylval.value = RO;
  163. else
  164. yylval.value = WO;
  165. return T_MODE;
  166. }
  167. field { return T_FIELD; }
  168. enum { return T_ENUM; }
  169. mask { return T_MASK; }
  170. alias { return T_ALIAS; }
  171. size { return T_SIZE; }
  172. scb { return T_SCB; }
  173. scratch_ram { return T_SRAM; }
  174. accumulator { return T_ACCUM; }
  175. mode_pointer { return T_MODE_PTR; }
  176. allones { return T_ALLONES; }
  177. allzeros { return T_ALLZEROS; }
  178. none { return T_NONE; }
  179. sindex { return T_SINDEX; }
  180. A { return T_A; }
  181. /* Instruction Formatting */
  182. PAD_PAGE { return T_PAD_PAGE; }
  183. BEGIN_CRITICAL { return T_BEGIN_CS; }
  184. END_CRITICAL { return T_END_CS; }
  185. SET_SRC_MODE { return T_SET_SRC_MODE; }
  186. SET_DST_MODE { return T_SET_DST_MODE; }
  187. /* Opcodes */
  188. shl { return T_SHL; }
  189. shr { return T_SHR; }
  190. ror { return T_ROR; }
  191. rol { return T_ROL; }
  192. mvi { return T_MVI; }
  193. mov { return T_MOV; }
  194. clr { return T_CLR; }
  195. jmp { return T_JMP; }
  196. jc { return T_JC; }
  197. jnc { return T_JNC; }
  198. je { return T_JE; }
  199. jne { return T_JNE; }
  200. jz { return T_JZ; }
  201. jnz { return T_JNZ; }
  202. call { return T_CALL; }
  203. add { return T_ADD; }
  204. adc { return T_ADC; }
  205. bmov { return T_BMOV; }
  206. inc { return T_INC; }
  207. dec { return T_DEC; }
  208. stc { return T_STC; }
  209. clc { return T_CLC; }
  210. cmp { return T_CMP; }
  211. not { return T_NOT; }
  212. xor { return T_XOR; }
  213. test { return T_TEST;}
  214. and { return T_AND; }
  215. or { return T_OR; }
  216. ret { return T_RET; }
  217. nop { return T_NOP; }
  218. /* ARP2 16bit extensions */
  219. /* or16 { return T_OR16; } */
  220. /* and16 { return T_AND16; }*/
  221. /* xor16 { return T_XOR16; }*/
  222. /* add16 { return T_ADD16; }*/
  223. /* adc16 { return T_ADC16; }*/
  224. /* mvi16 { return T_MVI16; }*/
  225. /* test16 { return T_TEST16; }*/
  226. /* cmp16 { return T_CMP16; }*/
  227. /* cmpxchg { return T_CMPXCHG; }*/
  228. /* Allowed Symbols */
  229. \<\< { return T_EXPR_LSHIFT; }
  230. \>\> { return T_EXPR_RSHIFT; }
  231. [-+,:()~|&."{};<>[\]/*!=] { return yytext[0]; }
  232. /* Number processing */
  233. 0[0-7]* {
  234. yylval.value = strtol(yytext, NULL, 8);
  235. return T_NUMBER;
  236. }
  237. 0[xX][0-9a-fA-F]+ {
  238. yylval.value = strtoul(yytext + 2, NULL, 16);
  239. return T_NUMBER;
  240. }
  241. [1-9][0-9]* {
  242. yylval.value = strtol(yytext, NULL, 10);
  243. return T_NUMBER;
  244. }
  245. /* Include Files */
  246. #include{SPACE} {
  247. BEGIN INCLUDE;
  248. quote_count = 0;
  249. return T_INCLUDE;
  250. }
  251. <INCLUDE>[<] { return yytext[0]; }
  252. <INCLUDE>[>] { BEGIN INITIAL; return yytext[0]; }
  253. <INCLUDE>[\"] {
  254. if (quote_count != 0)
  255. BEGIN INITIAL;
  256. quote_count++;
  257. return yytext[0];
  258. }
  259. <INCLUDE>{PATH} {
  260. char *yptr;
  261. yptr = yytext;
  262. string_buf_ptr = string_buf;
  263. while (*yptr)
  264. *string_buf_ptr++ = *yptr++;
  265. yylval.str = string_buf;
  266. *string_buf_ptr = '\0';
  267. return T_PATH;
  268. }
  269. <INCLUDE>. { stop("Invalid include line", EX_DATAERR); }
  270. #define{SPACE} {
  271. BEGIN MACRODEF;
  272. return T_DEFINE;
  273. }
  274. <MACRODEF>{WORD}{SPACE} {
  275. char *yptr;
  276. /* Strip space and return as a normal symbol */
  277. yptr = yytext;
  278. while (*yptr != ' ' && *yptr != '\t')
  279. yptr++;
  280. *yptr = '\0';
  281. yylval.sym = symtable_get(yytext);
  282. string_buf_ptr = string_buf;
  283. BEGIN MACROBODY;
  284. return T_SYMBOL;
  285. }
  286. <MACRODEF>{WORD}\( {
  287. /*
  288. * We store the symbol with its opening
  289. * parren so we can differentiate macros
  290. * that take args from macros with the
  291. * same name that do not take args as
  292. * is allowed in C.
  293. */
  294. BEGIN MACROARGLIST;
  295. yylval.sym = symtable_get(yytext);
  296. unput('(');
  297. return T_SYMBOL;
  298. }
  299. <MACROARGLIST>{WORD} {
  300. yylval.str = yytext;
  301. return T_ARG;
  302. }
  303. <MACROARGLIST>{SPACE} ;
  304. <MACROARGLIST>[(,] {
  305. return yytext[0];
  306. }
  307. <MACROARGLIST>[)] {
  308. string_buf_ptr = string_buf;
  309. BEGIN MACROBODY;
  310. return ')';
  311. }
  312. <MACROARGLIST>. {
  313. snprintf(buf, sizeof(buf), "Invalid character "
  314. "'%c' in macro argument list",
  315. yytext[0]);
  316. stop(buf, EX_DATAERR);
  317. }
  318. <MACROCALLARGS>{SPACE} ;
  319. <MACROCALLARGS>\( {
  320. parren_count++;
  321. if (parren_count == 1)
  322. return ('(');
  323. *string_buf_ptr++ = '(';
  324. }
  325. <MACROCALLARGS>\) {
  326. parren_count--;
  327. if (parren_count == 0) {
  328. BEGIN INITIAL;
  329. return (')');
  330. }
  331. *string_buf_ptr++ = ')';
  332. }
  333. <MACROCALLARGS>{MCARG} {
  334. char *yptr;
  335. yptr = yytext;
  336. while (*yptr)
  337. *string_buf_ptr++ = *yptr++;
  338. }
  339. <MACROCALLARGS>\, {
  340. if (string_buf_ptr != string_buf) {
  341. /*
  342. * Return an argument and
  343. * rescan this comma so we
  344. * can return it as well.
  345. */
  346. *string_buf_ptr = '\0';
  347. yylval.str = string_buf;
  348. string_buf_ptr = string_buf;
  349. unput(',');
  350. return T_ARG;
  351. }
  352. return ',';
  353. }
  354. <MACROBODY>\\\n {
  355. /* Eat escaped newlines. */
  356. ++yylineno;
  357. }
  358. <MACROBODY>\r ;
  359. <MACROBODY>\n {
  360. /* Macros end on the first unescaped newline. */
  361. BEGIN INITIAL;
  362. *string_buf_ptr = '\0';
  363. yylval.str = string_buf;
  364. ++yylineno;
  365. return T_MACROBODY;
  366. }
  367. <MACROBODY>{MBODY} {
  368. char *yptr;
  369. char c;
  370. yptr = yytext;
  371. while (c = *yptr++) {
  372. /*
  373. * Strip carriage returns.
  374. */
  375. if (c == '\r')
  376. continue;
  377. *string_buf_ptr++ = c;
  378. }
  379. }
  380. {WORD}\( {
  381. char *yptr;
  382. char *ycopy;
  383. /* May be a symbol or a macro invocation. */
  384. yylval.sym = symtable_get(yytext);
  385. if (yylval.sym->type == MACRO) {
  386. YY_BUFFER_STATE old_state;
  387. YY_BUFFER_STATE temp_state;
  388. ycopy = strdup(yytext);
  389. yptr = ycopy + yyleng;
  390. while (yptr > ycopy)
  391. unput(*--yptr);
  392. old_state = YY_CURRENT_BUFFER;
  393. temp_state =
  394. yy_create_buffer(stdin,
  395. YY_BUF_SIZE);
  396. yy_switch_to_buffer(temp_state);
  397. mm_switch_to_buffer(old_state);
  398. mmparse();
  399. mm_switch_to_buffer(temp_state);
  400. yy_switch_to_buffer(old_state);
  401. mm_delete_buffer(temp_state);
  402. expand_macro(yylval.sym);
  403. } else {
  404. if (yylval.sym->type == UNINITIALIZED) {
  405. /* Try without the '(' */
  406. symbol_delete(yylval.sym);
  407. yytext[yyleng-1] = '\0';
  408. yylval.sym =
  409. symtable_get(yytext);
  410. }
  411. unput('(');
  412. return T_SYMBOL;
  413. }
  414. }
  415. {WORD} {
  416. yylval.sym = symtable_get(yytext);
  417. if (yylval.sym->type == MACRO) {
  418. expand_macro(yylval.sym);
  419. } else {
  420. return T_SYMBOL;
  421. }
  422. }
  423. . {
  424. snprintf(buf, sizeof(buf), "Invalid character "
  425. "'%c'", yytext[0]);
  426. stop(buf, EX_DATAERR);
  427. }
  428. %%
  429. typedef struct include {
  430. YY_BUFFER_STATE buffer;
  431. int lineno;
  432. char *filename;
  433. SLIST_ENTRY(include) links;
  434. }include_t;
  435. SLIST_HEAD(, include) include_stack;
  436. void
  437. include_file(char *file_name, include_type type)
  438. {
  439. FILE *newfile;
  440. include_t *include;
  441. newfile = NULL;
  442. /* Try the current directory first */
  443. if (includes_search_curdir != 0 || type == SOURCE_FILE)
  444. newfile = fopen(file_name, "r");
  445. if (newfile == NULL && type != SOURCE_FILE) {
  446. path_entry_t include_dir;
  447. for (include_dir = search_path.slh_first;
  448. include_dir != NULL;
  449. include_dir = include_dir->links.sle_next) {
  450. char fullname[PATH_MAX];
  451. if ((include_dir->quoted_includes_only == TRUE)
  452. && (type != QUOTED_INCLUDE))
  453. continue;
  454. snprintf(fullname, sizeof(fullname),
  455. "%s/%s", include_dir->directory, file_name);
  456. if ((newfile = fopen(fullname, "r")) != NULL)
  457. break;
  458. }
  459. }
  460. if (newfile == NULL) {
  461. perror(file_name);
  462. stop("Unable to open input file", EX_SOFTWARE);
  463. /* NOTREACHED */
  464. }
  465. if (type != SOURCE_FILE) {
  466. include = (include_t *)malloc(sizeof(include_t));
  467. if (include == NULL) {
  468. stop("Unable to allocate include stack entry",
  469. EX_SOFTWARE);
  470. /* NOTREACHED */
  471. }
  472. include->buffer = YY_CURRENT_BUFFER;
  473. include->lineno = yylineno;
  474. include->filename = yyfilename;
  475. SLIST_INSERT_HEAD(&include_stack, include, links);
  476. }
  477. yy_switch_to_buffer(yy_create_buffer(newfile, YY_BUF_SIZE));
  478. yylineno = 1;
  479. yyfilename = strdup(file_name);
  480. }
  481. static void next_substitution(struct symbol *mac_symbol, const char *body_pos,
  482. const char **next_match,
  483. struct macro_arg **match_marg, regmatch_t *match);
  484. void
  485. expand_macro(struct symbol *macro_symbol)
  486. {
  487. struct macro_arg *marg;
  488. struct macro_arg *match_marg;
  489. const char *body_head;
  490. const char *body_pos;
  491. const char *next_match;
  492. /*
  493. * Due to the nature of unput, we must work
  494. * backwards through the macro body performing
  495. * any expansions.
  496. */
  497. body_head = macro_symbol->info.macroinfo->body;
  498. body_pos = body_head + strlen(body_head);
  499. while (body_pos > body_head) {
  500. regmatch_t match;
  501. next_match = body_head;
  502. match_marg = NULL;
  503. next_substitution(macro_symbol, body_pos, &next_match,
  504. &match_marg, &match);
  505. /* Put back everything up until the replacement. */
  506. while (body_pos > next_match)
  507. unput(*--body_pos);
  508. /* Perform the replacement. */
  509. if (match_marg != NULL) {
  510. const char *strp;
  511. next_match = match_marg->replacement_text;
  512. strp = next_match + strlen(next_match);
  513. while (strp > next_match)
  514. unput(*--strp);
  515. /* Skip past the unexpanded macro arg. */
  516. body_pos -= match.rm_eo - match.rm_so;
  517. }
  518. }
  519. /* Cleanup replacement text. */
  520. STAILQ_FOREACH(marg, &macro_symbol->info.macroinfo->args, links) {
  521. free(marg->replacement_text);
  522. }
  523. }
  524. /*
  525. * Find the next substitution in the macro working backwards from
  526. * body_pos until the beginning of the macro buffer. next_match
  527. * should be initialized to the beginning of the macro buffer prior
  528. * to calling this routine.
  529. */
  530. static void
  531. next_substitution(struct symbol *mac_symbol, const char *body_pos,
  532. const char **next_match, struct macro_arg **match_marg,
  533. regmatch_t *match)
  534. {
  535. regmatch_t matches[2];
  536. struct macro_arg *marg;
  537. const char *search_pos;
  538. int retval;
  539. do {
  540. search_pos = *next_match;
  541. STAILQ_FOREACH(marg, &mac_symbol->info.macroinfo->args, links) {
  542. retval = regexec(&marg->arg_regex, search_pos, 2,
  543. matches, 0);
  544. if (retval == 0
  545. && (matches[1].rm_eo + search_pos) <= body_pos
  546. && (matches[1].rm_eo + search_pos) > *next_match) {
  547. *match = matches[1];
  548. *next_match = match->rm_eo + search_pos;
  549. *match_marg = marg;
  550. }
  551. }
  552. } while (search_pos != *next_match);
  553. }
  554. int
  555. yywrap()
  556. {
  557. include_t *include;
  558. yy_delete_buffer(YY_CURRENT_BUFFER);
  559. (void)fclose(yyin);
  560. if (yyfilename != NULL)
  561. free(yyfilename);
  562. yyfilename = NULL;
  563. include = include_stack.slh_first;
  564. if (include != NULL) {
  565. yy_switch_to_buffer(include->buffer);
  566. yylineno = include->lineno;
  567. yyfilename = include->filename;
  568. SLIST_REMOVE_HEAD(&include_stack, links);
  569. free(include);
  570. return (0);
  571. }
  572. return (1);
  573. }