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 |
|
|
** Gisselquist Tecnology, LLC
|
13 |
|
|
**
|
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 |
46 |
dgisselq |
(?i:ljmp) { yylval.u_op = OP_LJMP;return SINGLOP; }
|
111 |
26 |
dgisselq |
(?i:neg) { yylval.u_op = OP_NEG; return SINGLOP; }
|
112 |
|
|
(?i:not) { yylval.u_op = OP_NOT; return SINGLOP; }
|
113 |
|
|
(?i:cmp) { yylval.u_op = OP_CMP; return DUALOP; }
|
114 |
|
|
(?i:tst) { yylval.u_op = OP_TST; return DUALOP; }
|
115 |
|
|
(?i:mov) { yylval.u_op = OP_MOV; return DUALOP; }
|
116 |
|
|
(?i:ldi) { yylval.u_op = OP_LDI; return LDIOP; }
|
117 |
46 |
dgisselq |
(?i:ldihi) { yylval.u_op =OP_LDIHI; return LDHLOP; }
|
118 |
|
|
(?i:lhi) { yylval.u_op =OP_LDIHI; return LDHLOP; }
|
119 |
|
|
(?i:ldilo) { yylval.u_op =OP_LDILO; return LDHLOP; }
|
120 |
|
|
(?i:llo) { yylval.u_op =OP_LDILO; return LDHLOP; }
|
121 |
26 |
dgisselq |
(?i:mpyu) { yylval.u_op = OP_MPYU; return DUALOP; }
|
122 |
|
|
(?i:mpys) { yylval.u_op = OP_MPYS; return DUALOP; }
|
123 |
|
|
(?i:rol) { yylval.u_op = OP_ROL; return DUALOP; }
|
124 |
|
|
(?i:sub) { yylval.u_op = OP_SUB; return DUALOP; }
|
125 |
|
|
(?i:and) { yylval.u_op = OP_AND; return DUALOP; }
|
126 |
|
|
(?i:add) { yylval.u_op = OP_ADD; return DUALOP; }
|
127 |
|
|
(?i:or) { yylval.u_op = OP_OR; return DUALOP;; }
|
128 |
|
|
(?i:xor) { yylval.u_op = OP_XOR; return DUALOP; }
|
129 |
|
|
(?i:lsl) { yylval.u_op = OP_LSL; return DUALOP; }
|
130 |
|
|
(?i:asr) { yylval.u_op = OP_ASR; return DUALOP; }
|
131 |
|
|
(?i:lsr) { yylval.u_op = OP_LSR; return DUALOP; }
|
132 |
|
|
(?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 |
|
|
(?i:equ) { return EQU; }
|
145 |
|
|
(?i:fill) { return FILL; }
|
146 |
|
|
(?i:word) { return WORD; }
|
147 |
13 |
dgisselq |
"__"[hH][eE][rR][eE]"__" { return HERE; }
|
148 |
|
|
[\.][dD][aA][tT] { return WORD; }
|
149 |
|
|
[\.][zZ]/[ \t\n] { yylval.u_cond = ZPARSER::ZIPC_Z; return COND; }
|
150 |
|
|
[\.][nN][eE]/[ \t\n] { yylval.u_cond = ZPARSER::ZIPC_NZ; return COND; }
|
151 |
|
|
[\.][nN][zZ]/[ \t\n] { yylval.u_cond = ZPARSER::ZIPC_NZ; return COND; }
|
152 |
|
|
[\.][gG][eE]/[ \t\n] { yylval.u_cond = ZPARSER::ZIPC_GE; return COND; }
|
153 |
|
|
[\.][gG][tT]/[ \t\n] { yylval.u_cond = ZPARSER::ZIPC_GT; return COND; }
|
154 |
|
|
[\.][lL][tT]/[ \t\n] { yylval.u_cond = ZPARSER::ZIPC_LT; return COND; }
|
155 |
|
|
[\.][nN]/[ \t\n] { yylval.u_cond = ZPARSER::ZIPC_LT; return COND; }
|
156 |
|
|
[\.][cC]/[ \t\n] { yylval.u_cond = ZPARSER::ZIPC_C; return COND; }
|
157 |
|
|
[\.][vV]/[ \t\n] { yylval.u_cond = ZPARSER::ZIPC_V; return COND; }
|
158 |
|
|
[_A-Za-z][_a-zA-Z0-9]* { yylval.u_id = strdup(yytext); return IDENTIFIER; }
|
159 |
|
|
"$"?[-]?0[xX][0-9A-Fa-f]+ { yylval.u_ival = strtoul(yytext+(((*yytext)=='$')?1:0),NULL,16);return INT;}
|
160 |
|
|
"$"?[-]?0[0-7]+ { yylval.u_ival = strtoul(yytext+(((*yytext)=='$')?1:0),NULL, 8);return INT;}
|
161 |
|
|
"$"?[-]?[1-9][0-9]* { yylval.u_ival = strtoul(yytext+(((*yytext)=='$')?1:0),NULL,10);return INT;}
|
162 |
|
|
"$"?[-]?[1-9A-Fa-f][0-9A-Fa-f]*h {yylval.u_ival=strtoul(yytext+(((*yytext)=='$')?1:0),NULL,16); return INT;}
|
163 |
46 |
dgisselq |
"$"?[-]?[0-7]+o { yylval.u_ival = strtoul(yytext+(((*yytext)=='$')?1:0),NULL, 8);return INT;}
|
164 |
54 |
dgisselq |
"\'"(("\\\'")|([^'\\])|("\\"[0abfnrtv])|("\\\\")|("\\\"")){1,4}"\'" {
|
165 |
|
|
yylval.u_ival = decode_char_int(&yytext[1]); return INT; }
|
166 |
13 |
dgisselq |
"$"?"0" { yylval.u_ival = 0;return INT;}
|
167 |
|
|
"$" { return DOLLAR; }
|
168 |
|
|
"," { return COMMA; }
|
169 |
|
|
"+" { return PLUS; }
|
170 |
|
|
"-" { return MINUS; }
|
171 |
|
|
"*" { return TIMES; }
|
172 |
|
|
"||" { return BOOLEANOR; }
|
173 |
|
|
"|" { return BITWISEOR; }
|
174 |
|
|
"&&" { return BOOLEANAND; }
|
175 |
|
|
"&" { return BITWISEAND; }
|
176 |
|
|
"^" { return BITWISEXOR; }
|
177 |
|
|
":" { return COLON; }
|
178 |
|
|
"." { return DOT; }
|
179 |
34 |
dgisselq |
"%" { return '%'; }
|
180 |
13 |
dgisselq |
[ \t]+ { }
|
181 |
|
|
[(] { return '('; }
|
182 |
|
|
[)] { return ')'; }
|
183 |
|
|
\n.* { if (linecp) free(linecp); linecp = strdup(&yytext[1]); yyless(1); return '\n'; }
|
184 |
|
|
|
185 |
|
|
%%
|
186 |
|
|
|
187 |
54 |
dgisselq |
int decode_char_int(const char *str) {
|
188 |
|
|
unsigned r = 0;
|
189 |
|
|
// fprintf(stderr, "Decoding character string: \'%s\n", str);
|
190 |
|
|
while((*str)&&('\'' != (*str))) {
|
191 |
|
|
if ('\\' == (*str)) {
|
192 |
|
|
// Process escape characters
|
193 |
|
|
str++;
|
194 |
|
|
if ('\0' != (*str)) {
|
195 |
|
|
r<<=8;
|
196 |
|
|
switch(*str) {
|
197 |
|
|
case '0': break;
|
198 |
|
|
case 'a': r |= '\a'; break;
|
199 |
|
|
case 'b': r |= '\b'; break;
|
200 |
|
|
case 'n': r |= '\n'; break;
|
201 |
|
|
case 'f': r |= '\f'; break;
|
202 |
|
|
case 'r': r |= '\r'; break;
|
203 |
|
|
case 't': r |= '\t'; break;
|
204 |
|
|
case 'v': r |= '\v'; break;
|
205 |
|
|
case '\'': r |= '\''; break;
|
206 |
|
|
case '\"': r |= '\"'; break;
|
207 |
|
|
case '\\': r |= '\\'; break;
|
208 |
|
|
} str++;
|
209 |
|
|
}
|
210 |
|
|
} else {
|
211 |
|
|
r = (r<<8)|(*str);
|
212 |
|
|
str++;
|
213 |
|
|
}
|
214 |
|
|
} return r;
|
215 |
|
|
}
|