scanner.l 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. %{
  2. /*
  3. * Copyright (C) 2006-2010 Michael Buesch <m@bues.ch>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2
  7. * as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include "parser.h"
  15. #include "main.h"
  16. #include "util.h"
  17. #include <stdio.h>
  18. #include <ctype.h>
  19. static void interpret_cppinfo(const char *);
  20. static void update_lineinfo(void);
  21. static inline void log_current_line(void)
  22. {
  23. size_t len;
  24. len = min(sizeof(cur_lineinfo.linecopy) - 1, strlen(yytext));
  25. memcpy(cur_lineinfo.linecopy, yytext, len);
  26. cur_lineinfo.linecopy[len] = '\0';
  27. }
  28. %}
  29. IDENTIFIER ([a-zA-Z_][0-9a-zA-Z_]*)
  30. WS ([ \t])
  31. NEWLINE ((\r)|(\n)|(\r\n))
  32. %%
  33. ^.*$ { log_current_line(); REJECT; }
  34. #{WS}+[0-9]+{WS}+\".*\"[0-9 \t]*{NEWLINE} { interpret_cppinfo(yytext); }
  35. {WS}+ { update_lineinfo(); /* whitespace */ }
  36. {NEWLINE} { cur_lineinfo.lineno++; update_lineinfo(); }
  37. ^{WS}*"%"{WS}*arch { update_lineinfo(); return ASM_ARCH; }
  38. ^{WS}*"%"{WS}*start { update_lineinfo(); return ASM_START; }
  39. "%"{WS}*assert { update_lineinfo(); return ASM_ASSERT; }
  40. ^{WS}*\.text{WS}*$ { update_lineinfo(); return SECTION_TEXT; }
  41. ^{WS}*\.initvals/\({IDENTIFIER}\) { update_lineinfo(); return SECTION_IVALS; }
  42. spr[0-9a-fA-F]{1,4} { update_lineinfo(); return SPR; }
  43. r/([0-9]|([1-5][0-9])|(6[0-3])) { update_lineinfo(); return GPR; }
  44. off/[0-6] { update_lineinfo(); return OFFR; }
  45. lr/[0-3] { update_lineinfo(); return LR; }
  46. , { update_lineinfo(); return COMMA; }
  47. ; { update_lineinfo(); return SEMICOLON; }
  48. \[ { update_lineinfo(); return BRACK_OPEN; }
  49. \] { update_lineinfo(); return BRACK_CLOSE; }
  50. \( { update_lineinfo(); return PAREN_OPEN; }
  51. \) { update_lineinfo(); return PAREN_CLOSE; }
  52. == { update_lineinfo(); return EQUAL; }
  53. != { update_lineinfo(); return NOT_EQUAL; }
  54. \|\| { update_lineinfo(); return LOGICAL_OR; }
  55. \&\& { update_lineinfo(); return LOGICAL_AND; }
  56. \+ { update_lineinfo(); return PLUS; }
  57. \- { update_lineinfo(); return MINUS; }
  58. \* { update_lineinfo(); return MULTIPLY; }
  59. \/ { update_lineinfo(); return DIVIDE; }
  60. \| { update_lineinfo(); return BITW_OR; }
  61. \& { update_lineinfo(); return BITW_AND; }
  62. \^ { update_lineinfo(); return BITW_XOR; }
  63. \~ { update_lineinfo(); return BITW_NOT; }
  64. \<\< { update_lineinfo(); return LEFTSHIFT; }
  65. \>\> { update_lineinfo(); return RIGHTSHIFT; }
  66. mul { update_lineinfo(); return OP_MUL; }
  67. add { update_lineinfo(); return OP_ADD; }
  68. add\. { update_lineinfo(); return OP_ADDSC; }
  69. addc { update_lineinfo(); return OP_ADDC; }
  70. addc\. { update_lineinfo(); return OP_ADDSCC; }
  71. sub { update_lineinfo(); return OP_SUB; }
  72. sub\. { update_lineinfo(); return OP_SUBSC; }
  73. subc { update_lineinfo(); return OP_SUBC; }
  74. subc\. { update_lineinfo(); return OP_SUBSCC; }
  75. sra { update_lineinfo(); return OP_SRA; }
  76. or { update_lineinfo(); return OP_OR; }
  77. and { update_lineinfo(); return OP_AND; }
  78. xor { update_lineinfo(); return OP_XOR; }
  79. sr { update_lineinfo(); return OP_SR; }
  80. srx { update_lineinfo(); return OP_SRX; }
  81. sl { update_lineinfo(); return OP_SL; }
  82. rl { update_lineinfo(); return OP_RL; }
  83. rr { update_lineinfo(); return OP_RR; }
  84. nand { update_lineinfo(); return OP_NAND; }
  85. orx { update_lineinfo(); return OP_ORX; }
  86. mov { update_lineinfo(); return OP_MOV; }
  87. jmp { update_lineinfo(); return OP_JMP; }
  88. jand { update_lineinfo(); return OP_JAND; }
  89. jnand { update_lineinfo(); return OP_JNAND; }
  90. js { update_lineinfo(); return OP_JS; }
  91. jns { update_lineinfo(); return OP_JNS; }
  92. je { update_lineinfo(); return OP_JE; }
  93. jne { update_lineinfo(); return OP_JNE; }
  94. jls { update_lineinfo(); return OP_JLS; }
  95. jges { update_lineinfo(); return OP_JGES; }
  96. jgs { update_lineinfo(); return OP_JGS; }
  97. jles { update_lineinfo(); return OP_JLES; }
  98. jl { update_lineinfo(); return OP_JL; }
  99. jge { update_lineinfo(); return OP_JGE; }
  100. jg { update_lineinfo(); return OP_JG; }
  101. jle { update_lineinfo(); return OP_JLE; }
  102. jdn { update_lineinfo(); return OP_JDN; }
  103. jdpz { update_lineinfo(); return OP_JDPZ; }
  104. jdp { update_lineinfo(); return OP_JDP; }
  105. jdnz { update_lineinfo(); return OP_JDNZ; }
  106. jzx { update_lineinfo(); return OP_JZX; }
  107. jnzx { update_lineinfo(); return OP_JNZX; }
  108. jext { update_lineinfo(); return OP_JEXT; }
  109. jnext { update_lineinfo(); return OP_JNEXT; }
  110. call { update_lineinfo(); return OP_CALL; }
  111. calls { update_lineinfo(); return OP_CALLS; }
  112. ret { update_lineinfo(); return OP_RET; }
  113. rets { update_lineinfo(); return OP_RETS; }
  114. tkiph { update_lineinfo(); return OP_TKIPH; }
  115. tkiphs { update_lineinfo(); return OP_TKIPHS; }
  116. tkipl { update_lineinfo(); return OP_TKIPL; }
  117. tkipls { update_lineinfo(); return OP_TKIPLS; }
  118. nap { update_lineinfo(); return OP_NAP; }
  119. mmio16 { update_lineinfo(); return IVAL_MMIO16; }
  120. mmio32 { update_lineinfo(); return IVAL_MMIO32; }
  121. phy { update_lineinfo(); return IVAL_PHY; }
  122. radio { update_lineinfo(); return IVAL_RADIO; }
  123. shm16 { update_lineinfo(); return IVAL_SHM16; }
  124. shm32 { update_lineinfo(); return IVAL_SHM32; }
  125. tram { update_lineinfo(); return IVAL_TRAM; }
  126. @[0-9a-fA-F]{1,4} { update_lineinfo(); return RAW_CODE; }
  127. 0x[0-9a-fA-F]+ { update_lineinfo(); return HEXNUM; }
  128. -?[0-9]+ { update_lineinfo(); return DECNUM; }
  129. {IDENTIFIER}: { update_lineinfo(); return LABEL; }
  130. {IDENTIFIER} { update_lineinfo(); return IDENT; }
  131. %%
  132. struct lineinfo cur_lineinfo;
  133. static inline const char * strip_leading_ws(const char *str)
  134. {
  135. while (*str != '\0' && isspace(*str))
  136. str++;
  137. return str;
  138. }
  139. static void interpret_cppinfo(const char *str)
  140. {
  141. const char * const orig = str;
  142. char tmp[64];
  143. const char *found;
  144. char *tail;
  145. /* This will interpret lines added by CPP.
  146. * They look like:
  147. * # lineno "filename" flags...
  148. */
  149. str = strip_leading_ws(str);
  150. if (*str != '#')
  151. goto error;
  152. str++; /* skip # character */
  153. str = strip_leading_ws(str);
  154. if (*str == '\0')
  155. goto error;
  156. /* Line number */
  157. found = strchr(str, ' ');
  158. if (!found)
  159. goto error;
  160. memset(tmp, 0, sizeof(tmp));
  161. memcpy(tmp, str, min(sizeof(tmp) - 1, (size_t)(found - str)));
  162. cur_lineinfo.lineno = strtoul(tmp, &tail, 0);
  163. if (*tail != '\0')
  164. goto error;
  165. str = strip_leading_ws(found);
  166. /* File name */
  167. if (*str != '\"')
  168. goto error;
  169. str++;
  170. if (*str == '\0')
  171. goto error;
  172. found = strchr(str, '\"');
  173. if (!found)
  174. goto error;
  175. memset(cur_lineinfo.file, 0, sizeof(cur_lineinfo.file));
  176. memcpy(cur_lineinfo.file, str, min(sizeof(cur_lineinfo.file) - 1,
  177. (size_t)(found - str)));
  178. /* We ignore the flags. */
  179. return;
  180. error:
  181. fprintf(stderr, "Invalid CPP line directive: %s\n", orig);
  182. exit(1);
  183. }
  184. static void update_lineinfo(void)
  185. {
  186. int i = 0;
  187. while (yytext[i] != '\0') {
  188. switch (yytext[i]) {
  189. case '\r':
  190. case '\n':
  191. cur_lineinfo.column = 0;
  192. break;
  193. case '\t':
  194. cur_lineinfo.column += 8 - (cur_lineinfo.column % 8);
  195. break;
  196. default:
  197. cur_lineinfo.column++;
  198. }
  199. i++;
  200. }
  201. }