mipsy-scanner-gen.py 6.39 KB
Newer Older
1
#!/usr/bin/env python
Benoit Perrot's avatar
Benoit Perrot committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
##
## This file is part of Mipsy, a tiny MIPS simulator
## Copyright (C) 2003 Benoit Perrot <benoit@lrde.epita.fr>
##
## Mipsy is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
## 
## Mipsy is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
## 
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##
20
21
22
23
24

import sys
import string, re

import mipsy
Benoit Perrot's avatar
Benoit Perrot committed
25
from mipsy import license, depth, define_id
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

parser = mipsy.InstructionSetParser()
instructions = parser.parse(sys.stdin)

#### Mipsy Flex scanner generator --------------------------

## Prologue ----------------------------
print "%{                                             /* -*- C++ -*- */"
print license
print """
#include <string>

#include \"common.hh\"
#include \"misc/escape.hh\"

41
42
#include \"vm/cpu.hh\"

43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include \"parse/asm-scan.hh\"

#define YY_NEVER_INTERACTIVE 1

#define YY_USER_INIT				\\
  do {						\\
    yylloc->begin.filename = current_file;	\\
    yylloc->end.filename = current_file;	\\
  } while (0)

#define YY_USER_ACTION				\\
    yylloc->columns (yyleng);

static std::string	current_file;

58
using inst::Register;
59
using vm::Cpu;
60

61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
%}

%option noyywrap
%option nounput
%option debug

%x STATE_STRING
eol			\\n|\\r|\\n\\r|\\r\\n

dec_int			[0-9]+
hex_int			0x[0-9A-Fa-f]+

label			[a-zA-Z_][a-zA-Z_0-9]*
%%

%{
  yylloc->step ();
%}

[ \\t]+			yylloc->step ();
[#;][^\\n\\r]*		yylloc->step ();
{eol}			yylloc->lines (yyleng); yylloc->step ();

\"(\"			return LPAREN;
\")\"			return RPAREN;
\",\"			return COMMA;

88
89
90
91
\"+\"			return EXP_ADD;
\"-\"			return EXP_SUB;
\"*\"			return EXP_MUL;
\"/\"			return EXP_DIV;
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106

\".text\"			return DIR_TEXT;
\".data\"			return DIR_DATA;
\".word\"			return DIR_WORD;
\".asciiz\"		return DIR_ASCIIZ;
\".space\"		return DIR_SPACE;
"""

## Body --------------------------------
for inst in instructions:
  print '"' + inst.opcode + '"\t\t\treturn ' + define_id(inst.opcode) + ";"

## Epilogue ----------------------------
print """
\"$zero\"			{
107
  yylval->reg = new Register(Register::general, Cpu::zero);
108
  return REGISTER;
109
110
111
}"""
for i in range(2):
  print "\"$v" + str(i) + "\"			{"
112
  print "  yylval->reg = new Register(Register::general, Cpu::v" + str(i)+ ");"
113
114
115
  print "  return REGISTER;\n}"
for i in range(4):
  print "\"$a" + str(i) + "\"			{"
116
  print "  yylval->reg = new Register(Register::general, Cpu::a" + str(i)+ ");"
117
118
119
  print "  return REGISTER;\n}"
for i in range(10):
  print "\"$t" + str(i) + "\"			{"
120
  print "  yylval->reg = new Register(Register::general, Cpu::t" + str(i)+ ");"
121
122
123
  print "  return REGISTER;\n}"
for i in range(8):
  print "\"$s" + str(i) + "\"			{"
124
  print "  yylval->reg = new Register(Register::general, Cpu::s" + str(i)+ ");"
125
126
  print "  return REGISTER;\n}"
print """
127
\"$sp\"			{
128
  yylval->reg = new Register(Register::general, Cpu::sp);
129
130
131
  return REGISTER;
}
\"$fp\"			{
132
  yylval->reg = new Register(Register::general, Cpu::fp);
133
134
135
  return REGISTER;
}
\"$ra\"			{
136
  yylval->reg = new Register(Register::general, Cpu::ra);
137
138
139
140
  return REGISTER;
}

\"$x\"{dec_int}		{
141
142
143
144
145
146
  if (!parse::unlimited_regs_p)
    {
      std::cerr << *yylloc
		<< \": unlimited registers prohibited\" << std::endl;
      exit_set (exit_scan);
    }
147
  yylval->reg = new Register(Register::unlimited, atoi(yytext +2));
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
  return REGISTER;
}


{label}			{
  yylval->label = new inst::Label (yytext);
  return LABEL;
}
{label}\":\"		{
  std::string label = yytext;
  label.resize(yyleng - 1);
  yylval->label = new inst::Label (label);
  return LABEL_DEF;
}


-?{dec_int}		yylval->i = atoi (yytext); return INTEGER;
-?{hex_int}		yylval->i = strtoll (yytext, 0, 16); return INTEGER;

Benoit Perrot's avatar
Benoit Perrot committed
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
'\\\\a'			yylval->i = '\\a'; return INTEGER;
'\\\\b'			yylval->i = '\\b'; return INTEGER;
'\\\\f'			yylval->i = '\\f'; return INTEGER;
'\\\\n'			yylval->i = '\\n'; return INTEGER;
'\\\\r'			yylval->i = '\\r'; return INTEGER;
'\\\\t'			yylval->i = '\\t'; return INTEGER;
'\\\\v'			yylval->i = '\\v'; return INTEGER;
'\\\\[\\\\\\']'		yylval->i = yytext[1]; return INTEGER;
'\\\\.'			{
  std::cerr << *yylloc
	    << \": unrecognized escape: \" << escape (yytext) << std::endl;
  exit_set (exit_scan);
}
'[^\\'\\n\\r\\\\]'	yylval->i = yytext[1]; return INTEGER;

182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279

\\\"			yylval->s = new std::string; BEGIN STATE_STRING;
<STATE_STRING>{
  \\\"			{
    BEGIN INITIAL;
    return STRING;
  }

  \\\\[0-7]{3}		{
    long c = strtol (yytext + 1, 0, 8);
    if (c > 255)
      {
	std::cerr << *yylloc
		  << \": invalid escape: \" << escape (yytext) << std::endl;
	exit_set (exit_scan);
      }
    else
      *(yylval->s) += c;
  }
  
  \\\\x[0-9a-fA-F]{2}	{
    *(yylval->s) += strtol (yytext + 2, 0, 16);
  }
  
  \\\\a			*(yylval->s) += '\\a';
  \\\\b			*(yylval->s) += '\\b';
  \\\\f			*(yylval->s) += '\\f';
  \\\\n			*(yylval->s) += '\\n';
  \\\\r			*(yylval->s) += '\\r';
  \\\\t			*(yylval->s) += '\\t';
  \\\\v			*(yylval->s) += '\\v';
  \\\\[\\\\\"\"]		*(yylval->s) += yytext[1];
  \\\\.			{
    std::cerr << *yylloc
	      << \": unrecognized escape: \" << escape (yytext) << std::endl;
    exit_set (exit_scan);
  }
  
  [^\\\"\\n\\r\\\\]+		yylval->s->append (yytext, yyleng);
  
  {eol}+		{
    yylloc->lines (yyleng);
    yylval->s->append (yyleng, '\\n');
  }
  
  <<EOF>>		{
    std::cerr
      << *yylloc << \": unexpected end of file in a string\" << std::endl;
    exit_set (exit_scan);
    
    BEGIN INITIAL;
    return STRING;
  }
}

.                       {
  std::cerr << *yylloc
	    << \": invalid character: `\"
	    << escape (yytext) << \"'\" << std::endl;
  exit_set (exit_scan);
}

%%
namespace parse
{

  void
  scan_open (const std::string &name)
  {
    yy_flex_debug = trace_scan_p;

    if (name == \"-\")
      {
	yyin = stdin;
	current_file = \"standard input\";
      }
    else
      {
	yyin = fopen (name.c_str (), \"r\");
	current_file = name;
	if (!yyin)
	  {
	    std::cerr
	      << program_name
	      << \": cannot open `\" << name << \"': \"
	      << strerror (errno) << std::endl;
	    exit (1);
	  }
      }
  }

  void
  scan_close (void)
  {
    fclose (yyin);
  }
}
"""