PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/scsi/aic7xxx/aicasm/aicasm_scan.l

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