mipsy-scanner-gen.py 5.8 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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

import sys
import string, re

import mipsy
from mipsy import license, depth, define_id, class_id

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\"

#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;

56
57
using inst::Register;

58
59
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
%}

%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;

\"+\"                   return EXP_ADD;
\"-\"                   return EXP_SUB;
\"*\"                   return EXP_MUL;
\"/\"                   return EXP_DIV;

\".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\"			{
104
  yylval->reg = new Register(Register::zero);
105
  return REGISTER;
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
}"""
for i in range(2):
  print "\"$v" + str(i) + "\"			{"
  print "  yylval->reg = new Register(Register::v" + str(i) + ");"
  print "  return REGISTER;\n}"
for i in range(4):
  print "\"$a" + str(i) + "\"			{"
  print "  yylval->reg = new Register(Register::a" + str(i) + ");"
  print "  return REGISTER;\n}"
for i in range(10):
  print "\"$t" + str(i) + "\"			{"
  print "  yylval->reg = new Register(Register::t" + str(i) + ");"
  print "  return REGISTER;\n}"
for i in range(8):
  print "\"$s" + str(i) + "\"			{"
  print "  yylval->reg = new Register(Register::s" + str(i) + ");"
  print "  return REGISTER;\n}"
print """
124
\"$sp\"			{
125
  yylval->reg = new Register(Register::sp);
126
127
128
  return REGISTER;
}
\"$fp\"			{
129
  yylval->reg = new Register(Register::fp);
130
131
132
  return REGISTER;
}
\"$ra\"			{
133
  yylval->reg = new Register(Register::ra);
134
135
136
137
138
139
140
141
142
143
144
  return REGISTER;
}

\"$x\"{dec_int}		{
//   if (!unlimited_regs_p)
//     {
//       std::cerr << *yylloc
// 		<< \": unlimited registers not allowed \"
// 		<< \"(use --FIXME to enable it)\" << std::endl;
//       exit_set (exit_scan);
//     }
145
  yylval->reg = new Register(atoi(yytext +2));
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
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
  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;


\\\"			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);
  }
}
"""