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; }
55 ^{WS}*\.text{WS}*$ { update_lineinfo(); return SECTION_TEXT; }
56 ^{WS}*\.initvals/\({IDENTIFIER}\) { update_lineinfo(); return SECTION_IVALS; }
58 spr[0-9a-fA-F]{3,3} { 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 PLUS; }
71 \- { update_lineinfo(); return MINUS; }
72 \* { update_lineinfo(); return MULTIPLY; }
73 \/ { update_lineinfo(); return DIVIDE; }
74 \| { update_lineinfo(); return BITW_OR; }
75 \& { update_lineinfo(); return BITW_AND; }
76 \^ { update_lineinfo(); return BITW_XOR; }
77 \~ { update_lineinfo(); return BITW_NOT; }
78 \<\< { update_lineinfo(); return LEFTSHIFT; }
79 \>\> { update_lineinfo(); return RIGHTSHIFT; }
81 add { update_lineinfo(); return OP_ADD; }
82 add\. { update_lineinfo(); return OP_ADDSC; }
83 addc { update_lineinfo(); return OP_ADDC; }
84 addc\. { update_lineinfo(); return OP_ADDSCC; }
86 sub { update_lineinfo(); return OP_SUB; }
87 sub\. { update_lineinfo(); return OP_SUBSC; }
88 subc { update_lineinfo(); return OP_SUBC; }
89 subc\. { update_lineinfo(); return OP_SUBSCC; }
91 sra { update_lineinfo(); return OP_SRA; }
92 or { update_lineinfo(); return OP_OR; }
93 and { update_lineinfo(); return OP_AND; }
94 xor { update_lineinfo(); return OP_XOR; }
95 sr { update_lineinfo(); return OP_SR; }
96 srx { update_lineinfo(); return OP_SRX; }
97 sl { update_lineinfo(); return OP_SL; }
98 rl { update_lineinfo(); return OP_RL; }
99 rr { update_lineinfo(); return OP_RR; }
100 nand { update_lineinfo(); return OP_NAND; }
101 orx { update_lineinfo(); return OP_ORX; }
102 mov { update_lineinfo(); return OP_MOV; }
104 jmp { update_lineinfo(); return OP_JMP; }
105 jand { update_lineinfo(); return OP_JAND; }
106 jnand { update_lineinfo(); return OP_JNAND; }
107 js { update_lineinfo(); return OP_JS; }
108 jns { update_lineinfo(); return OP_JNS; }
109 je { update_lineinfo(); return OP_JE; }
110 jne { update_lineinfo(); return OP_JNE; }
111 jls { update_lineinfo(); return OP_JLS; }
112 jges { update_lineinfo(); return OP_JGES; }
113 jgs { update_lineinfo(); return OP_JGS; }
114 jles { update_lineinfo(); return OP_JLES; }
115 jl { update_lineinfo(); return OP_JL; }
116 jge { update_lineinfo(); return OP_JGE; }
117 jg { update_lineinfo(); return OP_JG; }
118 jle { update_lineinfo(); return OP_JLE; }
119 jzx { update_lineinfo(); return OP_JZX; }
120 jnzx { update_lineinfo(); return OP_JNZX; }
121 jext { update_lineinfo(); return OP_JEXT; }
122 jnext { update_lineinfo(); return OP_JNEXT; }
124 call { update_lineinfo(); return OP_CALL; }
125 ret { update_lineinfo(); return OP_RET; }
127 tkiph { update_lineinfo(); return OP_TKIPH; }
128 tkiphs { update_lineinfo(); return OP_TKIPHS; }
129 tkipl { update_lineinfo(); return OP_TKIPL; }
130 tkipls { update_lineinfo(); return OP_TKIPLS; }
132 nap { update_lineinfo(); return OP_NAP; }
134 mmio16 { update_lineinfo(); return IVAL_MMIO16; }
135 mmio32 { update_lineinfo(); return IVAL_MMIO32; }
136 phy { update_lineinfo(); return IVAL_PHY; }
137 radio { update_lineinfo(); return IVAL_RADIO; }
138 shm16 { update_lineinfo(); return IVAL_SHM16; }
139 shm32 { update_lineinfo(); return IVAL_SHM32; }
141 @[0-9a-fA-F]{3,3} { update_lineinfo(); return RAW_CODE; }
143 0x[0-9a-fA-F]+ { update_lineinfo(); return HEXNUM; }
144 -?[0-9]+ { update_lineinfo(); return DECNUM; }
146 {IDENTIFIER}: { update_lineinfo(); return LABEL; }
147 {IDENTIFIER} { update_lineinfo(); return IDENT; }
151 struct lineinfo cur_lineinfo;
152 //FIXME The linenumber sometimes is wrong.
154 static void interpret_cppinfo(const char *str)
156 const char * const orig = str;
160 /* This will interpret lines added by CPP.
167 str++; /* skip # character */
170 str++; /* skip whitespace */
173 found = strchr(str, ' ');
176 memset(tmp, 0, sizeof(tmp));
177 memcpy(tmp, str, min(sizeof(tmp) - 1,
178 (int)(found - str)));
179 cur_lineinfo.lineno = strtoul(tmp, NULL, 10) - 1;
189 found = strchr(str, '\"');
192 memset(cur_lineinfo.file, 0, sizeof(cur_lineinfo.file));
193 memcpy(cur_lineinfo.file, str,
194 min(sizeof(cur_lineinfo.file) - 1,
195 (int)(found - str)));
199 fprintf(stderr, "Invalid CPP line directive: %s\n", orig);
203 static void update_lineinfo(void)
207 while (yytext[i] != '\0') {
211 cur_lineinfo.column = 0;
214 cur_lineinfo.column += 8 - (cur_lineinfo.column % 8);
217 cur_lineinfo.column++;