4 * Copyright (C) 2006-2007 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.
22 #define min(x,y) ((x) < (y) ? (x) : (y))
25 static void interpret_cppinfo(const char *);
26 static void update_lineinfo(void);
28 static inline void log_current_line(void)
32 len = min(sizeof(cur_lineinfo.linecopy) - 1, strlen(yytext));
33 strncpy(cur_lineinfo.linecopy, yytext, len);
34 cur_lineinfo.linecopy[len] = '\0';
39 IDENTIFIER ([a-zA-Z_][0-9a-zA-Z_]*)
41 NEWLINE ((\r)|(\n)|(\r\n))
45 ^.*$ { log_current_line(); REJECT; }
46 #\ [0-9]+\ \".*\"[ ]*[0-9]*{NEWLINE} { interpret_cppinfo(yytext); }
49 {WS}+ { update_lineinfo(); /* whitespace */ }
50 {NEWLINE} { cur_lineinfo.lineno++; update_lineinfo(); }
52 ^{WS}*"%"{WS}*arch { update_lineinfo(); return ASM_ARCH; }
53 ^{WS}*"%"{WS}*start { update_lineinfo(); return ASM_START; }
54 "%"{WS}*assert { update_lineinfo(); return ASM_ASSERT; }
56 ^{WS}*\.text{WS}*$ { update_lineinfo(); return SECTION_TEXT; }
57 ^{WS}*\.initvals/\({IDENTIFIER}\) { update_lineinfo(); return SECTION_IVALS; }
59 spr[0-9a-fA-F]{1,4} { update_lineinfo(); return SPR; }
60 r/([0-9]|([1-5][0-9])|(6[0-3])) { update_lineinfo(); return GPR; }
61 off/[0-6] { update_lineinfo(); return OFFR; }
62 lr/[0-3] { update_lineinfo(); return LR; }
64 , { update_lineinfo(); return COMMA; }
65 ; { update_lineinfo(); return SEMICOLON; }
66 \[ { update_lineinfo(); return BRACK_OPEN; }
67 \] { update_lineinfo(); return BRACK_CLOSE; }
68 \( { update_lineinfo(); return PAREN_OPEN; }
69 \) { update_lineinfo(); return PAREN_CLOSE; }
71 == { update_lineinfo(); return EQUAL; }
72 != { update_lineinfo(); return NOT_EQUAL; }
73 \|\| { update_lineinfo(); return LOGICAL_OR; }
74 \&\& { update_lineinfo(); return LOGICAL_AND; }
75 \+ { update_lineinfo(); return PLUS; }
76 \- { update_lineinfo(); return MINUS; }
77 \* { update_lineinfo(); return MULTIPLY; }
78 \/ { update_lineinfo(); return DIVIDE; }
79 \| { update_lineinfo(); return BITW_OR; }
80 \& { update_lineinfo(); return BITW_AND; }
81 \^ { update_lineinfo(); return BITW_XOR; }
82 \~ { update_lineinfo(); return BITW_NOT; }
83 \<\< { update_lineinfo(); return LEFTSHIFT; }
84 \>\> { update_lineinfo(); return RIGHTSHIFT; }
86 add { update_lineinfo(); return OP_ADD; }
87 add\. { update_lineinfo(); return OP_ADDSC; }
88 addc { update_lineinfo(); return OP_ADDC; }
89 addc\. { update_lineinfo(); return OP_ADDSCC; }
91 sub { update_lineinfo(); return OP_SUB; }
92 sub\. { update_lineinfo(); return OP_SUBSC; }
93 subc { update_lineinfo(); return OP_SUBC; }
94 subc\. { update_lineinfo(); return OP_SUBSCC; }
96 sra { update_lineinfo(); return OP_SRA; }
97 or { update_lineinfo(); return OP_OR; }
98 and { update_lineinfo(); return OP_AND; }
99 xor { update_lineinfo(); return OP_XOR; }
100 sr { update_lineinfo(); return OP_SR; }
101 srx { update_lineinfo(); return OP_SRX; }
102 sl { update_lineinfo(); return OP_SL; }
103 rl { update_lineinfo(); return OP_RL; }
104 rr { update_lineinfo(); return OP_RR; }
105 nand { update_lineinfo(); return OP_NAND; }
106 orx { update_lineinfo(); return OP_ORX; }
107 mov { update_lineinfo(); return OP_MOV; }
109 jmp { update_lineinfo(); return OP_JMP; }
110 jand { update_lineinfo(); return OP_JAND; }
111 jnand { update_lineinfo(); return OP_JNAND; }
112 js { update_lineinfo(); return OP_JS; }
113 jns { update_lineinfo(); return OP_JNS; }
114 je { update_lineinfo(); return OP_JE; }
115 jne { update_lineinfo(); return OP_JNE; }
116 jls { update_lineinfo(); return OP_JLS; }
117 jges { update_lineinfo(); return OP_JGES; }
118 jgs { update_lineinfo(); return OP_JGS; }
119 jles { update_lineinfo(); return OP_JLES; }
120 jl { update_lineinfo(); return OP_JL; }
121 jge { update_lineinfo(); return OP_JGE; }
122 jg { update_lineinfo(); return OP_JG; }
123 jle { update_lineinfo(); return OP_JLE; }
124 jzx { update_lineinfo(); return OP_JZX; }
125 jnzx { update_lineinfo(); return OP_JNZX; }
126 jext { update_lineinfo(); return OP_JEXT; }
127 jnext { update_lineinfo(); return OP_JNEXT; }
129 call { update_lineinfo(); return OP_CALL; }
130 ret { update_lineinfo(); return OP_RET; }
132 tkiph { update_lineinfo(); return OP_TKIPH; }
133 tkiphs { update_lineinfo(); return OP_TKIPHS; }
134 tkipl { update_lineinfo(); return OP_TKIPL; }
135 tkipls { update_lineinfo(); return OP_TKIPLS; }
137 nap { update_lineinfo(); return OP_NAP; }
139 mmio16 { update_lineinfo(); return IVAL_MMIO16; }
140 mmio32 { update_lineinfo(); return IVAL_MMIO32; }
141 phy { update_lineinfo(); return IVAL_PHY; }
142 radio { update_lineinfo(); return IVAL_RADIO; }
143 shm16 { update_lineinfo(); return IVAL_SHM16; }
144 shm32 { update_lineinfo(); return IVAL_SHM32; }
145 tram { update_lineinfo(); return IVAL_TRAM; }
147 @[0-9a-fA-F]{1,4} { update_lineinfo(); return RAW_CODE; }
149 0x[0-9a-fA-F]+ { update_lineinfo(); return HEXNUM; }
150 -?[0-9]+ { update_lineinfo(); return DECNUM; }
152 {IDENTIFIER}: { update_lineinfo(); return LABEL; }
153 {IDENTIFIER} { update_lineinfo(); return IDENT; }
157 struct lineinfo cur_lineinfo;
158 //FIXME The linenumber sometimes is wrong.
160 static void interpret_cppinfo(const char *str)
162 const char * const orig = str;
166 /* This will interpret lines added by CPP.
173 str++; /* skip # character */
176 str++; /* skip whitespace */
179 found = strchr(str, ' ');
182 memset(tmp, 0, sizeof(tmp));
183 memcpy(tmp, str, min(sizeof(tmp) - 1,
184 (int)(found - str)));
185 cur_lineinfo.lineno = strtoul(tmp, NULL, 10) - 1;
195 found = strchr(str, '\"');
198 memset(cur_lineinfo.file, 0, sizeof(cur_lineinfo.file));
199 memcpy(cur_lineinfo.file, str,
200 min(sizeof(cur_lineinfo.file) - 1,
201 (int)(found - str)));
205 fprintf(stderr, "Invalid CPP line directive: %s\n", orig);
209 static void update_lineinfo(void)
213 while (yytext[i] != '\0') {
217 cur_lineinfo.column = 0;
220 cur_lineinfo.column += 8 - (cur_lineinfo.column % 8);
223 cur_lineinfo.column++;