iverilog/vvp/lexor.lex

243 lines
7.1 KiB
Plaintext
Raw Normal View History

2001-03-11 01:29:38 +01:00
2006-05-17 06:15:12 +02:00
%option never-interactive
%option nounput
2006-05-17 06:15:12 +02:00
2001-03-11 01:29:38 +01:00
%{
2001-03-20 03:48:40 +01:00
/*
* Copyright (c) 2001-2008 Stephen Williams (steve@icarus.com)
2001-03-20 03:48:40 +01:00
*
* This source code is free software; you can redistribute it
* and/or modify it in source code form 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.
*
* This program 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
*/
2001-03-11 01:29:38 +01:00
# include "parse_misc.h"
# include "compile.h"
# include "parse.h"
# include <string.h>
2002-03-01 06:42:50 +01:00
# include <assert.h>
2001-03-11 01:29:38 +01:00
%}
%%
/* These are some special header/footer keywords. */
2001-03-23 03:40:22 +01:00
^":vpi_module" { return K_vpi_module; }
^":vpi_time_precision" { return K_vpi_time_precision; }
^":file_names" { return K_file_names; }
2001-03-23 03:40:22 +01:00
2001-03-11 01:29:38 +01:00
/* A label is any non-blank text that appears left justified. */
^[.$_a-zA-Z\\][.$_a-zA-Z\\0-9<>/]* {
2001-03-11 01:29:38 +01:00
yylval.text = strdup(yytext);
2002-03-01 06:42:50 +01:00
assert(yylval.text);
2001-03-11 01:29:38 +01:00
return T_LABEL; }
/* String tokens are parsed here. Return as the token value the
contents of the string without the enclosing quotes. */
\"([^\"\\]|\\.)*\" {
yytext[strlen(yytext)-1] = 0;
yylval.text = strdup(yytext+1);
2002-03-01 06:42:50 +01:00
assert(yylval.text);
return T_STRING; }
2003-02-10 00:33:26 +01:00
/* Binary vector tokens are parsed here. The result of this is a
string of binary 4-values in the yylval.vect.text string. This is
2003-02-10 00:33:26 +01:00
preceded by an 's' if the vector is signed. */
[1-9][0-9]*("'b"|"'sb")[01xz]+ {
yylval.vect.idx = strtoul(yytext, 0, 10);
yylval.vect.text = (char*)malloc(yylval.vect.idx + 2);
2002-03-01 06:42:50 +01:00
assert(yylval.vect.text);
char*dest = yylval.vect.text;
const char*bits = strchr(yytext, '\'');
assert(bits);
bits += 1;
if (*bits == 's') {
*dest++ = 's';
bits += 1;
}
assert(*bits == 'b');
bits += 1;
unsigned pad = 0;
if (strlen(bits) < yylval.vect.idx)
pad = yylval.vect.idx - strlen(bits);
memset(dest, '0', pad);
for (unsigned idx = pad ; idx < yylval.vect.idx ; idx += 1)
dest[idx] = bits[idx-pad];
dest[yylval.vect.idx] = 0;
return T_VECTOR; }
2001-03-11 01:29:38 +01:00
/* These are some keywords that are recognized. */
2005-10-12 19:23:15 +02:00
".alias" { return K_ALIAS; }
".alias/real" { return K_ALIAS_R; }
".alias/s" { return K_ALIAS_S; }
2004-06-30 04:15:57 +02:00
".arith/div" { return K_ARITH_DIV; }
".arith/div.r" { return K_ARITH_DIV_R; }
2004-06-30 04:15:57 +02:00
".arith/div.s" { return K_ARITH_DIV_S; }
".arith/mod" { return K_ARITH_MOD; }
".arith/mod.r" { return K_ARITH_MOD_R; }
".arith/mult" { return K_ARITH_MULT; }
".arith/mult.r" { return K_ARITH_MULT_R; }
".arith/pow" { return K_ARITH_POW; }
".arith/pow.r" { return K_ARITH_POW_R; }
".arith/sub" { return K_ARITH_SUB; }
".arith/sub.r" { return K_ARITH_SUB_R; }
".arith/sum" { return K_ARITH_SUM; }
".arith/sum.r" { return K_ARITH_SUM_R; }
".array" { return K_ARRAY; }
".array/i" { return K_ARRAY_I; }
".array/real" { return K_ARRAY_R; }
".array/s" { return K_ARRAY_S; }
".array/port" { return K_ARRAY_PORT; }
2005-01-22 02:06:20 +01:00
".cmp/eeq" { return K_CMP_EEQ; }
2004-06-16 18:33:25 +02:00
".cmp/eq" { return K_CMP_EQ; }
".cmp/eq.r" { return K_CMP_EQ_R; }
2005-03-09 06:52:03 +01:00
".cmp/nee" { return K_CMP_NEE; }
2004-06-16 18:33:25 +02:00
".cmp/ne" { return K_CMP_NE; }
".cmp/ne.r" { return K_CMP_NE_R; }
".cmp/ge" { return K_CMP_GE; }
".cmp/ge.r" { return K_CMP_GE_R; }
2003-04-11 07:15:38 +02:00
".cmp/ge.s" { return K_CMP_GE_S; }
".cmp/gt" { return K_CMP_GT; }
".cmp/gt.r" { return K_CMP_GT_R; }
2003-04-11 07:15:38 +02:00
".cmp/gt.s" { return K_CMP_GT_S; }
".concat" { return K_CONCAT; }
".delay" { return K_DELAY; }
2005-04-24 22:07:25 +02:00
".dff" { return K_DFF; }
2001-04-14 07:10:56 +02:00
".event" { return K_EVENT; }
".event/or" { return K_EVENT_OR; }
2005-05-24 03:43:27 +02:00
".extend/s" { return K_EXTEND_S; }
2001-04-14 07:10:56 +02:00
".functor" { return K_FUNCTOR; }
2006-09-23 06:57:19 +02:00
".modpath" { return K_MODPATH; }
2001-04-14 07:10:56 +02:00
".net" { return K_NET; }
".net8" { return K_NET8; }
".net8/s" { return K_NET8_S; }
".net/real" { return K_NET_R; }
2001-04-14 07:10:56 +02:00
".net/s" { return K_NET_S; }
2006-03-08 06:29:42 +01:00
".param/l" { return K_PARAM_L; }
".param/str" { return K_PARAM_STR; }
".part" { return K_PART; }
".part/pv" { return K_PART_PV; }
2005-05-09 01:40:14 +02:00
".part/v" { return K_PART_V; }
2005-02-03 05:55:13 +01:00
".reduce/and" { return K_REDUCE_AND; }
".reduce/or" { return K_REDUCE_OR; }
".reduce/xor" { return K_REDUCE_XOR; }
".reduce/nand" { return K_REDUCE_NAND; }
".reduce/nor" { return K_REDUCE_NOR; }
".reduce/xnor" { return K_REDUCE_XNOR; }
".repeat" { return K_REPEAT; }
2001-05-09 04:53:25 +02:00
".resolv" { return K_RESOLV; }
2001-04-14 07:10:56 +02:00
".scope" { return K_SCOPE; }
".sfunc" { return K_SFUNC; }
2001-07-06 06:46:44 +02:00
".shift/l" { return K_SHIFTL; }
2001-07-07 04:57:33 +02:00
".shift/r" { return K_SHIFTR; }
2006-07-30 04:51:35 +02:00
".shift/rs" { return K_SHIFTRS; }
2001-04-14 07:10:56 +02:00
".thread" { return K_THREAD; }
".timescale" { return K_TIMESCALE; }
2002-03-18 01:19:34 +01:00
".ufunc" { return K_UFUNC; }
2001-04-14 07:10:56 +02:00
".var" { return K_VAR; }
".var/real" { return K_VAR_R; }
2001-04-14 07:10:56 +02:00
".var/s" { return K_VAR_S; }
".var/i" { return K_VAR_I; /* integer */ }
".udp" { return K_UDP; }
".udp/c"(omb)? { return K_UDP_C; }
".udp/s"(equ)? { return K_UDP_S; }
".mem" { return K_MEM; }
".mem/p"(ort)? { return K_MEM_P; }
".mem/i"(nit)? { return K_MEM_I; }
"-debug" { return K_DEBUG; }
2001-03-11 01:29:38 +01:00
/* instructions start with a % character. The compiler decides what
kind of instruction this really is. The few exceptions (that have
exceptional parameter requirements) are listed first. */
2001-04-18 06:21:23 +02:00
"%vpi_call" { return K_vpi_call; }
2001-05-20 02:46:12 +02:00
"%vpi_func" { return K_vpi_func; }
"%vpi_func/r" { return K_vpi_func_r; }
2001-04-18 06:21:23 +02:00
"%disable" { return K_disable; }
"%fork" { return K_fork; }
2001-03-11 01:29:38 +01:00
"%"[.$_/a-zA-Z0-9]+ {
yylval.text = strdup(yytext);
2002-03-01 06:42:50 +01:00
assert(yylval.text);
2001-03-11 01:29:38 +01:00
return T_INSTR; }
[0-9][0-9]* {
2001-03-20 03:45:25 +01:00
yylval.numb = strtol(yytext, 0, 0);
2001-03-11 01:29:38 +01:00
return T_NUMBER; }
"0x"[0-9a-fA-F]+ {
2001-03-20 03:45:25 +01:00
yylval.numb = strtol(yytext, 0, 0);
2001-03-11 01:29:38 +01:00
return T_NUMBER; }
2001-03-11 01:29:38 +01:00
/* Symbols are pretty much what is left. They are used to refer to
labels so the rule must match a string that a label would match. */
[.$_a-zA-Z\\][.$_a-zA-Z\\0-9/]* {
2001-03-11 01:29:38 +01:00
yylval.text = strdup(yytext);
2002-03-01 06:42:50 +01:00
assert(yylval.text);
2001-03-11 01:29:38 +01:00
return T_SYMBOL; }
/* Handle some specialized constant/literals as symbols. */
"C4<"[01xz]*">" {
yylval.text = strdup(yytext);
2002-03-01 06:42:50 +01:00
assert(yylval.text);
return T_SYMBOL; }
"C8<"[01234567xz]*">" {
yylval.text = strdup(yytext);
assert(yylval.text);
return T_SYMBOL; }
"Cr<m"[a-f0-9]*"g"[a-f0-9]*">" {
yylval.text = strdup(yytext);
assert(yylval.text);
return T_SYMBOL; }
"T<"[0-9]*","[0-9]*","[us]">" {
yylval.text = strdup(yytext);
assert(yylval.text);
return T_SYMBOL; }
"W<"[0-9]*","[r]">" {
yylval.text = strdup(yytext);
assert(yylval.text);
return T_SYMBOL; }
2001-03-11 01:29:38 +01:00
/* Accept the common assembler style comments, treat them as white
space. Of course, also skip white space. The semi-colon is
special, though, in that it is also a statement terminator. */
";".* { return ';'; }
"#".* { ; }
2002-02-27 06:46:33 +01:00
[ \t\b\r] { ; }
2001-03-11 01:29:38 +01:00
\n { yyline += 1; }
. { return yytext[0]; }
%%
int yywrap()
{
return -1;
}