4 * Copyright (C) 2006-2010 Michael Buesch <mb@bu3sch.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
24 static void interpret_cppinfo(const char *);
25 static void update_lineinfo(void);
27 static inline void log_current_line(void)
31 len = min(sizeof(cur_lineinfo.linecopy) - 1, strlen(yytext));
32 memcpy(cur_lineinfo.linecopy, yytext, len);
33 cur_lineinfo.linecopy[len] = '\0';
38 IDENTIFIER ([a-zA-Z_][0-9a-zA-Z_]*)
40 NEWLINE ((\r)|(\n)|(\r\n))
44 ^.*$ { log_current_line(); REJECT; }
45 #{WS}+[0-9]+{WS}+\".*\"[0-9 \t]*{NEWLINE} { interpret_cppinfo(yytext); }
48 {WS}+ { update_lineinfo(); /* whitespace */ }
49 {NEWLINE} { cur_lineinfo.lineno++; update_lineinfo(); }
51 ^{WS}*"%"{WS}*arch { update_lineinfo(); return ASM_ARCH; }
52 ^{WS}*"%"{WS}*start { update_lineinfo(); return ASM_START; }
53 "%"{WS}*assert { update_lineinfo(); return ASM_ASSERT; }
55 ^{WS}*\.text{WS}*$ { update_lineinfo(); return SECTION_TEXT; }
56 ^{WS}*\.initvals/\({IDENTIFIER}\) { update_lineinfo(); return SECTION_IVALS; }
58 spr[0-9a-fA-F]{1,4} { update_lineinfo(); return SPR; }
59 r/([0-9]|([1-5][0-9])|(6[0-3])) { update_lineinfo(); return GPR; }
60 off/[0-6] { update_lineinfo(); return OFFR; }
61 lr/[0-3] { update_lineinfo(); return LR; }
63 , { update_lineinfo(); return COMMA; }
64 ; { update_lineinfo(); return SEMICOLON; }
65 \[ { update_lineinfo(); return BRACK_OPEN; }
66 \] { update_lineinfo(); return BRACK_CLOSE; }
67 \( { update_lineinfo(); return PAREN_OPEN; }
68 \) { update_lineinfo(); return PAREN_CLOSE; }
70 == { update_lineinfo(); return EQUAL; }
71 != { update_lineinfo(); return NOT_EQUAL; }
72 \|\| { update_lineinfo(); return LOGICAL_OR; }
73 \&\& { update_lineinfo(); return LOGICAL_AND; }
74 \+ { update_lineinfo(); return PLUS; }
75 \- { update_lineinfo(); return MINUS; }
76 \* { update_lineinfo(); return MULTIPLY; }
77 \/ { update_lineinfo(); return DIVIDE; }
78 \| { update_lineinfo(); return BITW_OR; }
79 \& { update_lineinfo(); return BITW_AND; }
80 \^ { update_lineinfo(); return BITW_XOR; }
81 \~ { update_lineinfo(); return BITW_NOT; }
82 \<\< { update_lineinfo(); return LEFTSHIFT; }
83 \>\> { update_lineinfo(); return RIGHTSHIFT; }
85 add { update_lineinfo(); return OP_ADD; }
86 add\. { update_lineinfo(); return OP_ADDSC; }
87 addc { update_lineinfo(); return OP_ADDC; }
88 addc\. { update_lineinfo(); return OP_ADDSCC; }
90 sub { update_lineinfo(); return OP_SUB; }
91 sub\. { update_lineinfo(); return OP_SUBSC; }
92 subc { update_lineinfo(); return OP_SUBC; }
93 subc\. { update_lineinfo(); return OP_SUBSCC; }
95 sra { update_lineinfo(); return OP_SRA; }
96 or { update_lineinfo(); return OP_OR; }
97 and { update_lineinfo(); return OP_AND; }
98 xor { update_lineinfo(); return OP_XOR; }
99 sr { update_lineinfo(); return OP_SR; }
100 srx { update_lineinfo(); return OP_SRX; }
101 sl { update_lineinfo(); return OP_SL; }
102 rl { update_lineinfo(); return OP_RL; }
103 rr { update_lineinfo(); return OP_RR; }
104 nand { update_lineinfo(); return OP_NAND; }
105 orx { update_lineinfo(); return OP_ORX; }
106 mov { update_lineinfo(); return OP_MOV; }
108 jmp { update_lineinfo(); return OP_JMP; }
109 jand { update_lineinfo(); return OP_JAND; }
110 jnand { update_lineinfo(); return OP_JNAND; }
111 js { update_lineinfo(); return OP_JS; }
112 jns { update_lineinfo(); return OP_JNS; }
113 je { update_lineinfo(); return OP_JE; }
114 jne { update_lineinfo(); return OP_JNE; }
115 jls { update_lineinfo(); return OP_JLS; }
116 jges { update_lineinfo(); return OP_JGES; }
117 jgs { update_lineinfo(); return OP_JGS; }
118 jles { update_lineinfo(); return OP_JLES; }
119 jl { update_lineinfo(); return OP_JL; }
120 jge { update_lineinfo(); return OP_JGE; }
121 jg { update_lineinfo(); return OP_JG; }
122 jle { update_lineinfo(); return OP_JLE; }
123 jzx { update_lineinfo(); return OP_JZX; }
124 jnzx { update_lineinfo(); return OP_JNZX; }
125 jext { update_lineinfo(); return OP_JEXT; }
126 jnext { update_lineinfo(); return OP_JNEXT; }
128 call { update_lineinfo(); return OP_CALL; }
129 calls { update_lineinfo(); return OP_CALLS; }
130 ret { update_lineinfo(); return OP_RET; }
131 rets { update_lineinfo(); return OP_RETS; }
133 tkiph { update_lineinfo(); return OP_TKIPH; }
134 tkiphs { update_lineinfo(); return OP_TKIPHS; }
135 tkipl { update_lineinfo(); return OP_TKIPL; }
136 tkipls { update_lineinfo(); return OP_TKIPLS; }
138 nap { update_lineinfo(); return OP_NAP; }
140 mmio16 { update_lineinfo(); return IVAL_MMIO16; }
141 mmio32 { update_lineinfo(); return IVAL_MMIO32; }
142 phy { update_lineinfo(); return IVAL_PHY; }
143 radio { update_lineinfo(); return IVAL_RADIO; }
144 shm16 { update_lineinfo(); return IVAL_SHM16; }
145 shm32 { update_lineinfo(); return IVAL_SHM32; }
146 tram { update_lineinfo(); return IVAL_TRAM; }
148 @[0-9a-fA-F]{1,4} { update_lineinfo(); return RAW_CODE; }
150 0x[0-9a-fA-F]+ { update_lineinfo(); return HEXNUM; }
151 -?[0-9]+ { update_lineinfo(); return DECNUM; }
153 {IDENTIFIER}: { update_lineinfo(); return LABEL; }
154 {IDENTIFIER} { update_lineinfo(); return IDENT; }
158 struct lineinfo cur_lineinfo;
160 static inline const char * strip_leading_ws(const char *str)
162 while (*str != '\0' && isspace(*str))
167 static void interpret_cppinfo(const char *str)
169 const char * const orig = str;
174 /* This will interpret lines added by CPP.
176 * # lineno "filename" flags...
179 str = strip_leading_ws(str);
182 str++; /* skip # character */
183 str = strip_leading_ws(str);
188 found = strchr(str, ' ');
191 memset(tmp, 0, sizeof(tmp));
192 memcpy(tmp, str, min(sizeof(tmp) - 1, (size_t)(found - str)));
193 cur_lineinfo.lineno = strtoul(tmp, &tail, 0);
196 str = strip_leading_ws(found);
204 found = strchr(str, '\"');
207 memset(cur_lineinfo.file, 0, sizeof(cur_lineinfo.file));
208 memcpy(cur_lineinfo.file, str, min(sizeof(cur_lineinfo.file) - 1,
209 (size_t)(found - str)));
211 /* We ignore the flags. */
215 fprintf(stderr, "Invalid CPP line directive: %s\n", orig);
219 static void update_lineinfo(void)
223 while (yytext[i] != '\0') {
227 cur_lineinfo.column = 0;
230 cur_lineinfo.column += 8 - (cur_lineinfo.column % 8);
233 cur_lineinfo.column++;