1 |
13 |
dgisselq |
/*******************************************************************************
|
2 |
|
|
**
|
3 |
|
|
** Filename: zasm.l
|
4 |
|
|
**
|
5 |
|
|
** Project: Zip CPU -- a small, lightweight, RISC CPU core
|
6 |
|
|
**
|
7 |
|
|
** Purpose: The lexical analyzer for the assembler. This converts assembler
|
8 |
|
|
** input into tokens, to feed the parser.
|
9 |
|
|
**
|
10 |
|
|
**
|
11 |
|
|
** Creator: Dan Gisselquist, Ph.D.
|
12 |
69 |
dgisselq |
** Gisselquist Technology, LLC
|
13 |
13 |
dgisselq |
**
|
14 |
|
|
********************************************************************************
|
15 |
|
|
**
|
16 |
|
|
** Copyright (C) 2015, Gisselquist Technology, LLC
|
17 |
|
|
**
|
18 |
|
|
** This program is free software (firmware): you can redistribute it and/or
|
19 |
|
|
** modify it under the terms of the GNU General Public License as published
|
20 |
|
|
** by the Free Software Foundation, either version 3 of the License, or (at
|
21 |
|
|
** your option) any later version.
|
22 |
|
|
**
|
23 |
|
|
** This program is distributed in the hope that it will be useful, but WITHOUT
|
24 |
|
|
** ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
|
25 |
|
|
** FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
26 |
|
|
** for more details.
|
27 |
|
|
**
|
28 |
|
|
** You should have received a copy of the GNU General Public License along
|
29 |
|
|
** with this program. (It's in the $(ROOT)/doc directory, run make with no
|
30 |
|
|
** target there if the PDF file isn't present.) If not, see
|
31 |
|
|
** for a copy.
|
32 |
|
|
**
|
33 |
|
|
** License: GPL, v3, as defined and found on www.gnu.org,
|
34 |
|
|
** http://www.gnu.org/licenses/gpl.html
|
35 |
|
|
**
|
36 |
|
|
**
|
37 |
|
|
*******************************************************************************/
|
38 |
|
|
|
39 |
|
|
%{
|
40 |
|
|
#include
|
41 |
|
|
#include "asmdata.h"
|
42 |
|
|
#include "zasm.tab.h"
|
43 |
|
|
|
44 |
|
|
extern "C" int yylex();
|
45 |
|
|
extern "C" int yywrap() { return 1;}
|
46 |
|
|
extern char *master_input_filename;
|
47 |
54 |
dgisselq |
extern int decode_char_int(const char *str);
|
48 |
13 |
dgisselq |
%}
|
49 |
|
|
|
50 |
|
|
%option yylineno
|
51 |
|
|
%option warn
|
52 |
|
|
|
53 |
|
|
%%
|
54 |
|
|
|
55 |
26 |
dgisselq |
"#line"[ \t]+[0-9]+[ \t]+[\"][^"]*[\"][ \t]*$ {
|
56 |
|
|
/* This should really be true *only* at the beginning of a line,
|
57 |
|
|
* however such line beginnings aren't matching. My guess is that
|
58 |
|
|
* the reason is my line copying code at the bottom that sucks up the
|
59 |
|
|
* newline for LEX. Hence, this is what we have.
|
60 |
|
|
*/
|
61 |
13 |
dgisselq |
yylineno = atoi(&yytext[6]);
|
62 |
|
|
char *bg, *en;
|
63 |
|
|
bg = strchr(yytext, '\"')+1;
|
64 |
|
|
en = strchr(bg, '\"'); *en = '\0';
|
65 |
|
|
if ((master_input_filename == NULL)||(strcmp(master_input_filename, bg)!=0)) {
|
66 |
|
|
if (!master_input_filename) delete[] master_input_filename;
|
67 |
|
|
master_input_filename = new char[en-bg+2];
|
68 |
|
|
strcpy(master_input_filename, bg);
|
69 |
|
|
}
|
70 |
26 |
dgisselq |
return '\n';
|
71 |
13 |
dgisselq |
}
|
72 |
26 |
dgisselq |
"#line"[ \t]+[0-9]+[ \t]*$ { yylineno = atoi(&yytext[6]); return '\n';}
|
73 |
|
|
^"#line".*$ { printf("WARNING: Unmatched #line: %s\n", yytext); return '\n'; }
|
74 |
|
|
"#line".*$ { printf("WARNING: Unmatched #line, not at beginnning: %s\n", yytext); return '\n'; }
|
75 |
46 |
dgisselq |
(?i:ur)1[0-5] { yylval.u_reg=(ZPARSER::ZIPREG)(26+yytext[3]-'0');return REG; }
|
76 |
|
|
(?i:sr)1[0-5] { yylval.u_reg=(ZPARSER::ZIPREG)(10+yytext[3]-'0');return REG; }
|
77 |
13 |
dgisselq |
[rR]1[0-5] { yylval.u_reg=(ZPARSER::ZIPREG)(10+yytext[2]-'0');return REG; }
|
78 |
46 |
dgisselq |
(?i:ur)[0-9] { yylval.u_reg=(ZPARSER::ZIPREG)(16+yytext[2]-'0');return REG; }
|
79 |
|
|
(?i:sr)[0-9] { yylval.u_reg=(ZPARSER::ZIPREG)( yytext[2]-'0');return REG; }
|
80 |
13 |
dgisselq |
[rR][0-9] { yylval.u_reg=(ZPARSER::ZIPREG)( yytext[1]-'0');return REG; }
|
81 |
26 |
dgisselq |
(?i:upc) { yylval.u_reg = ZPARSER::ZIP_uPC; return REG; }
|
82 |
|
|
(?i:spc) { yylval.u_reg = ZPARSER::ZIP_PC; return REG; }
|
83 |
|
|
(?i:pc) { yylval.u_reg = ZPARSER::ZIP_PC; return REG; }
|
84 |
|
|
(?i:ucc) { yylval.u_reg = ZPARSER::ZIP_uCC; return REG; }
|
85 |
|
|
(?i:scc) { yylval.u_reg = ZPARSER::ZIP_CC; return REG; }
|
86 |
|
|
(?i:cc) { yylval.u_reg = ZPARSER::ZIP_CC; return REG; }
|
87 |
|
|
(?i:usp) { yylval.u_reg = ZPARSER::ZIP_uSP; return REG; }
|
88 |
|
|
(?i:ssp) { yylval.u_reg = ZPARSER::ZIP_SP; return REG; }
|
89 |
|
|
(?i:sp) { yylval.u_reg = ZPARSER::ZIP_SP; return REG; }
|
90 |
46 |
dgisselq |
(?i:gbl) { yylval.u_reg = ZPARSER::ZIP_R12; return REG; }
|
91 |
|
|
(?i:bra) { yylval.u_op = OP_BRA; return BRANCHOP; }
|
92 |
|
|
(?i:brz) { yylval.u_op = OP_BZ; return BRANCHOP; }
|
93 |
|
|
(?i:bz) { yylval.u_op = OP_BZ; return BRANCHOP; }
|
94 |
|
|
(?i:bnz) { yylval.u_op = OP_BNZ; return BRANCHOP; }
|
95 |
|
|
(?i:bne) { yylval.u_op = OP_BNZ; return BRANCHOP; }
|
96 |
|
|
(?i:bge) { yylval.u_op = OP_BGE; return BRANCHOP; }
|
97 |
|
|
(?i:bgt) { yylval.u_op = OP_BGT; return BRANCHOP; }
|
98 |
|
|
(?i:bg) { yylval.u_op = OP_BGT; return BRANCHOP; }
|
99 |
|
|
(?i:blt) { yylval.u_op = OP_BLT; return BRANCHOP; }
|
100 |
|
|
(?i:bn) { yylval.u_op = OP_BLT; return BRANCHOP; }
|
101 |
|
|
(?i:brc) { yylval.u_op = OP_BRC; return BRANCHOP; }
|
102 |
|
|
(?i:bc) { yylval.u_op = OP_BRC; return BRANCHOP; }
|
103 |
|
|
(?i:brv) { yylval.u_op = OP_BRV; return BRANCHOP; }
|
104 |
|
|
(?i:bv) { yylval.u_op = OP_BRV; return BRANCHOP; }
|
105 |
26 |
dgisselq |
(?i:clr) { yylval.u_op = OP_CLR; return SINGLOP; }
|
106 |
46 |
dgisselq |
(?i:clrf) { yylval.u_op = OP_CLRF;return SINGLOP; }
|
107 |
26 |
dgisselq |
(?i:int) { yylval.u_op = OP_TRAP;return SINGLOP; }
|
108 |
46 |
dgisselq |
(?i:trap) { yylval.u_op = OP_TRAP;return SINGLOP; }
|
109 |
26 |
dgisselq |
(?i:jmp) { yylval.u_op = OP_JMP; return SINGLOP; }
|
110 |
|
|
(?i:neg) { yylval.u_op = OP_NEG; return SINGLOP; }
|
111 |
|
|
(?i:not) { yylval.u_op = OP_NOT; return SINGLOP; }
|
112 |
|
|
(?i:cmp) { yylval.u_op = OP_CMP; return DUALOP; }
|
113 |
|
|
(?i:tst) { yylval.u_op = OP_TST; return DUALOP; }
|
114 |
|
|
(?i:mov) { yylval.u_op = OP_MOV; return DUALOP; }
|
115 |
|
|
(?i:ldi) { yylval.u_op = OP_LDI; return LDIOP; }
|
116 |
46 |
dgisselq |
(?i:ldihi) { yylval.u_op =OP_LDIHI; return LDHLOP; }
|
117 |
|
|
(?i:lhi) { yylval.u_op =OP_LDIHI; return LDHLOP; }
|
118 |
|
|
(?i:ldilo) { yylval.u_op =OP_LDILO; return LDHLOP; }
|
119 |
|
|
(?i:llo) { yylval.u_op =OP_LDILO; return LDHLOP; }
|
120 |
26 |
dgisselq |
(?i:mpyu) { yylval.u_op = OP_MPYU; return DUALOP; }
|
121 |
|
|
(?i:mpys) { yylval.u_op = OP_MPYS; return DUALOP; }
|
122 |
|
|
(?i:rol) { yylval.u_op = OP_ROL; return DUALOP; }
|
123 |
|
|
(?i:sub) { yylval.u_op = OP_SUB; return DUALOP; }
|
124 |
|
|
(?i:and) { yylval.u_op = OP_AND; return DUALOP; }
|
125 |
|
|
(?i:add) { yylval.u_op = OP_ADD; return DUALOP; }
|
126 |
|
|
(?i:or) { yylval.u_op = OP_OR; return DUALOP;; }
|
127 |
|
|
(?i:xor) { yylval.u_op = OP_XOR; return DUALOP; }
|
128 |
|
|
(?i:lsl) { yylval.u_op = OP_LSL; return DUALOP; }
|
129 |
|
|
(?i:asr) { yylval.u_op = OP_ASR; return DUALOP; }
|
130 |
|
|
(?i:lsr) { yylval.u_op = OP_LSR; return DUALOP; }
|
131 |
98 |
dgisselq |
(?i:ljmp) { yylval.u_op = OP_LJMP; return BAREOP; }
|
132 |
26 |
dgisselq |
(?i:lod) { yylval.u_op = OP_LOD; return LOADOP; }
|
133 |
|
|
(?i:sto) { yylval.u_op = OP_STO; return STOROP; }
|
134 |
|
|
(?i:halt) { yylval.u_op = OP_HALT; return BAREOP; }
|
135 |
|
|
(?i:wait) { yylval.u_op = OP_HALT; return BAREOP; }
|
136 |
|
|
(?i:rtu) { yylval.u_op = OP_RTU; return BAREOP; }
|
137 |
46 |
dgisselq |
(?i:retn) { yylval.u_op = OP_RETN; return BAREOP; }
|
138 |
|
|
(?i:ret) { yylval.u_op = OP_RETN; return BAREOP; }
|
139 |
26 |
dgisselq |
(?i:nop) { yylval.u_op = OP_NOOP; return BAREOP; }
|
140 |
|
|
(?i:noop) { yylval.u_op = OP_NOOP; return BAREOP; }
|
141 |
|
|
(?i:break) { yylval.u_op = OP_BREAK; return BAREOP; }
|
142 |
|
|
(?i:brk) { yylval.u_op = OP_BREAK; return BAREOP; }
|
143 |
|
|
(?i:busy) { yylval.u_op = OP_BUSY; return BAREOP; }
|
144 |
69 |
dgisselq |
(?i:brev) { yylval.u_op = OP_BREV; return DUALOP; }
|
145 |
|
|
(?i:popc) { yylval.u_op = OP_POPC; return DUALOP; }
|
146 |
|
|
(?i:divu) { yylval.u_op = OP_DIVU; return DUALOP; }
|
147 |
|
|
(?i:divs) { yylval.u_op = OP_DIVS; return DUALOP; }
|
148 |
|
|
(?i:fpadd) { yylval.u_op = OP_FPADD; return DUALOP; }
|
149 |
|
|
(?i:fpsub) { yylval.u_op = OP_FPSUB; return DUALOP; }
|
150 |
|
|
(?i:fpmul) { yylval.u_op = OP_FPMUL; return DUALOP; }
|
151 |
|
|
(?i:fpdiv) { yylval.u_op = OP_FPDIV; return DUALOP; }
|
152 |
|
|
(?i:fpcvt) { yylval.u_op = OP_FPCVT; return DUALOP; }
|
153 |
|
|
(?i:fpint) { yylval.u_op = OP_FPINT; return DUALOP; }
|
154 |
26 |
dgisselq |
(?i:equ) { return EQU; }
|
155 |
|
|
(?i:fill) { return FILL; }
|
156 |
|
|
(?i:word) { return WORD; }
|
157 |
13 |
dgisselq |
"__"[hH][eE][rR][eE]"__" { return HERE; }
|
158 |
|
|
[\.][dD][aA][tT] { return WORD; }
|
159 |
69 |
dgisselq |
[\.](?i:z)/[ \t\n] { yylval.u_cond = ZPARSER::ZIPC_Z; return COND; }
|
160 |
|
|
[\.](?i:ne)/[ \t\n] { yylval.u_cond = ZPARSER::ZIPC_NZ; return COND; }
|
161 |
|
|
[\.](?i:nz)/[ \t\n] { yylval.u_cond = ZPARSER::ZIPC_NZ; return COND; }
|
162 |
|
|
[\.](?i:ge)/[ \t\n] { yylval.u_cond = ZPARSER::ZIPC_GE; return COND; }
|
163 |
|
|
[\.](?i:gt)/[ \t\n] { yylval.u_cond = ZPARSER::ZIPC_GT; return COND; }
|
164 |
|
|
[\.](?i:lt)/[ \t\n] { yylval.u_cond = ZPARSER::ZIPC_LT; return COND; }
|
165 |
|
|
[\.](?i:n)/[ \t\n] { yylval.u_cond = ZPARSER::ZIPC_LT; return COND; }
|
166 |
|
|
[\.](?i:c)/[ \t\n] { yylval.u_cond = ZPARSER::ZIPC_C; return COND; }
|
167 |
|
|
[\.](?i:v)/[ \t\n] { yylval.u_cond = ZPARSER::ZIPC_V; return COND; }
|
168 |
|
|
[_A-Za-z][_a-zA-Z0-9]* { yylval.u_id = strdup(yytext); return IDENTIFIER; }
|
169 |
13 |
dgisselq |
"$"?[-]?0[xX][0-9A-Fa-f]+ { yylval.u_ival = strtoul(yytext+(((*yytext)=='$')?1:0),NULL,16);return INT;}
|
170 |
|
|
"$"?[-]?0[0-7]+ { yylval.u_ival = strtoul(yytext+(((*yytext)=='$')?1:0),NULL, 8);return INT;}
|
171 |
|
|
"$"?[-]?[1-9][0-9]* { yylval.u_ival = strtoul(yytext+(((*yytext)=='$')?1:0),NULL,10);return INT;}
|
172 |
|
|
"$"?[-]?[1-9A-Fa-f][0-9A-Fa-f]*h {yylval.u_ival=strtoul(yytext+(((*yytext)=='$')?1:0),NULL,16); return INT;}
|
173 |
46 |
dgisselq |
"$"?[-]?[0-7]+o { yylval.u_ival = strtoul(yytext+(((*yytext)=='$')?1:0),NULL, 8);return INT;}
|
174 |
54 |
dgisselq |
"\'"(("\\\'")|([^'\\])|("\\"[0abfnrtv])|("\\\\")|("\\\"")){1,4}"\'" {
|
175 |
|
|
yylval.u_ival = decode_char_int(&yytext[1]); return INT; }
|
176 |
13 |
dgisselq |
"$"?"0" { yylval.u_ival = 0;return INT;}
|
177 |
|
|
"$" { return DOLLAR; }
|
178 |
|
|
"," { return COMMA; }
|
179 |
|
|
"+" { return PLUS; }
|
180 |
|
|
"-" { return MINUS; }
|
181 |
|
|
"*" { return TIMES; }
|
182 |
|
|
"||" { return BOOLEANOR; }
|
183 |
|
|
"|" { return BITWISEOR; }
|
184 |
|
|
"&&" { return BOOLEANAND; }
|
185 |
|
|
"&" { return BITWISEAND; }
|
186 |
|
|
"^" { return BITWISEXOR; }
|
187 |
|
|
":" { return COLON; }
|
188 |
|
|
"." { return DOT; }
|
189 |
34 |
dgisselq |
"%" { return '%'; }
|
190 |
13 |
dgisselq |
[ \t]+ { }
|
191 |
|
|
[(] { return '('; }
|
192 |
|
|
[)] { return ')'; }
|
193 |
|
|
\n.* { if (linecp) free(linecp); linecp = strdup(&yytext[1]); yyless(1); return '\n'; }
|
194 |
|
|
|
195 |
|
|
%%
|
196 |
|
|
|
197 |
54 |
dgisselq |
int decode_char_int(const char *str) {
|
198 |
|
|
unsigned r = 0;
|
199 |
|
|
// fprintf(stderr, "Decoding character string: \'%s\n", str);
|
200 |
|
|
while((*str)&&('\'' != (*str))) {
|
201 |
|
|
if ('\\' == (*str)) {
|
202 |
|
|
// Process escape characters
|
203 |
|
|
str++;
|
204 |
|
|
if ('\0' != (*str)) {
|
205 |
|
|
r<<=8;
|
206 |
|
|
switch(*str) {
|
207 |
|
|
case '0': break;
|
208 |
|
|
case 'a': r |= '\a'; break;
|
209 |
|
|
case 'b': r |= '\b'; break;
|
210 |
|
|
case 'n': r |= '\n'; break;
|
211 |
|
|
case 'f': r |= '\f'; break;
|
212 |
|
|
case 'r': r |= '\r'; break;
|
213 |
|
|
case 't': r |= '\t'; break;
|
214 |
|
|
case 'v': r |= '\v'; break;
|
215 |
|
|
case '\'': r |= '\''; break;
|
216 |
|
|
case '\"': r |= '\"'; break;
|
217 |
|
|
case '\\': r |= '\\'; break;
|
218 |
|
|
} str++;
|
219 |
|
|
}
|
220 |
|
|
} else {
|
221 |
|
|
r = (r<<8)|(*str);
|
222 |
|
|
str++;
|
223 |
|
|
}
|
224 |
|
|
} return r;
|
225 |
|
|
}
|