iverilog/vvp/lexor.lex

347 lines
10 KiB
Plaintext
Raw Permalink Normal View History

%option prefix="yy"
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
/*
2018-02-23 23:30:32 +01:00
* Copyright (c) 2001-2018 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
2012-08-29 03:41:23 +02:00
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
2001-03-20 03:48:40 +01:00
*/
2001-03-11 01:29:38 +01:00
# include "parse_misc.h"
# include "compile.h"
# include "parse.h"
# include <cstring>
# include <cassert>
# include "ivl_alloc.h"
# define YY_NO_INPUT
static char* strdupnew(char const *str)
{
return str ? strcpy(new char [strlen(str)+1], str) : 0;
}
inline uint64_t strtouint64(const char*str, char**endptr, int base)
{
if (sizeof(unsigned long) >= sizeof(uint64_t))
return strtoul(str, endptr, base);
else
return strtoull(str, endptr, base);
}
2001-03-11 01:29:38 +01:00
%}
%%
/* These are some special header/footer keywords. */
^":ivl_version" { return K_ivl_version; }
^":ivl_delay_selection" { return K_ivl_delay_selection; }
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 = strdupnew(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);
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. */
".abs" { return K_ARITH_ABS; }
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/mod.s" { return K_ARITH_MOD_S; }
".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/pow.s" { return K_ARITH_POW_S; }
".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/2s" { return K_ARRAY_2S; }
".array/2u" { return K_ARRAY_2U; }
".array/i" { return K_ARRAY_I; }
".array/obj" { return K_ARRAY_OBJ; }
".array/real" { return K_ARRAY_R; }
".array/s" { return K_ARRAY_S; }
".array/str" { return K_ARRAY_STR; }
".array/port" { return K_ARRAY_PORT; }
".cast/2" { return K_CAST_2; }
".cast/int" { return K_CAST_INT; }
".cast/real" { return K_CAST_REAL; }
".cast/real.s" { return K_CAST_REAL_S; }
".class" { return K_CLASS; }
2005-01-22 02:06:20 +01:00
".cmp/eeq" { return K_CMP_EEQ; }
".cmp/eqx" { return K_CMP_EQX; }
".cmp/eqz" { return K_CMP_EQZ; }
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; }
".cmp/weq" { return K_CMP_WEQ; }
".cmp/wne" { return K_CMP_WNE; }
".concat" { return K_CONCAT; }
".concat8" { return K_CONCAT8; }
".delay" { return K_DELAY; }
".dff/n" { return K_DFF_N; }
".dff/n/aclr" { return K_DFF_N_ACLR; }
".dff/n/aset" { return K_DFF_N_ASET; }
".dff/p" { return K_DFF_P; }
".dff/p/aclr" { return K_DFF_P_ACLR; }
".dff/p/aset" { return K_DFF_P_ASET; }
".enum2" { return K_ENUM2; }
".enum2/s" { return K_ENUM2_S; }
".enum4" { return K_ENUM4; }
".enum4/s" { return K_ENUM4_S; }
2001-04-14 07:10:56 +02:00
".event" { return K_EVENT; }
".event/or" { return K_EVENT_OR; }
".export" { return K_EXPORT; }
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; }
".import" { return K_IMPORT; }
".island" { return K_ISLAND; }
2016-03-12 10:04:51 +01:00
".latch" { return K_LATCH; }
".modpath" { return K_MODPATH; }
2001-04-14 07:10:56 +02:00
".net" { return K_NET; }
".net/2s" { return K_NET_2S; }
".net/2u" { return K_NET_2U; }
".net8" { return K_NET8; }
".net8/2s" { return K_NET8_2S; }
".net8/2u" { return K_NET8_2U; }
".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; }
".param/real" { return K_PARAM_REAL; }
".part" { return K_PART; }
".part/pv" { return K_PART_PV; }
2005-05-09 01:40:14 +02:00
".part/v" { return K_PART_V; }
".part/v.s" { return K_PART_V_S; }
".port" { return K_PORT; }
".port_info" { return K_PORT_INFO; }
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; }
2018-02-23 23:30:32 +01:00
".rtran" { return K_RTRAN; }
".rtranif0" { return K_RTRANIF0; }
".rtranif1" { return K_RTRANIF1; }
2001-04-14 07:10:56 +02:00
".scope" { return K_SCOPE; }
".sfunc" { return K_SFUNC; }
".sfunc/e" { return K_SFUNC_E; }
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; }
".substitute" { return K_SUBSTITUTE; }
2001-04-14 07:10:56 +02:00
".thread" { return K_THREAD; }
".timescale" { return K_TIMESCALE; }
".tran" { return K_TRAN; }
".tranif0" { return K_TRANIF0; }
".tranif1" { return K_TRANIF1; }
".tranvp" { return K_TRANVP; }
".ufunc/real" { return K_UFUNC_REAL; }
".ufunc/vec4" { return K_UFUNC_VEC4; }
".ufunc/e" { return K_UFUNC_E; }
2001-04-14 07:10:56 +02:00
".var" { return K_VAR; }
".var/cobj" { return K_VAR_COBJECT; }
".var/darray" { return K_VAR_DARRAY; }
".var/queue" { return K_VAR_QUEUE; }
".var/real" { return K_VAR_R; }
2001-04-14 07:10:56 +02:00
".var/s" { return K_VAR_S; }
".var/str" { return K_VAR_STR; }
".var/i" { return K_VAR_I; /* integer */ }
".var/2s" { return K_VAR_2S; /* byte/shortint/int/longint signed */ }
".var/2u" { return K_VAR_2U; /* byte/shortint/int/longint unsigned */ }
".udp" { return K_UDP; }
".udp/c"(omb)? { return K_UDP_C; }
".udp/s"(equ)? { return K_UDP_S; }
"-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. */
"%vpi_call" { return K_vpi_call; }
"%vpi_call/w" { return K_vpi_call_w; }
"%vpi_call/i" { return K_vpi_call_i; }
"%vpi_func" { return K_vpi_func; }
"%vpi_func/r" { return K_vpi_func_r; }
"%vpi_func/s" { return K_vpi_func_s; }
"%file_line" { return K_file_line; }
/* Handle the specialized variable access functions. */
"&A" { return K_A; }
"&APV" { return K_APV; }
"&PV" { return K_PV; }
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]* {
yylval.numb = strtouint64(yytext, 0, 0);
2001-03-11 01:29:38 +01:00
return T_NUMBER; }
"0x"[0-9a-fA-F]+ {
yylval.numb = strtouint64(yytext, 0, 0);
2001-03-11 01:29:38 +01:00
return T_NUMBER; }
/* 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; }
"S<"[0-9]*",str>" {
yylval.text = strdup(yytext);
assert(yylval.text);
return T_SYMBOL; }
"S<"[0-9]*",vec4,"[us][0-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; }
2012-08-16 20:06:20 +02:00
"/INPUT" { return K_PORT_INPUT; }
"/OUTPUT" { return K_PORT_OUTPUT; }
"/INOUT" { return K_PORT_INOUT; }
"/MIXED" { return K_PORT_MIXED; }
"/NODIR" { return K_PORT_NODIR; }
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/]|(\\.))* {
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;
}
/*
* Modern version of flex (>=2.5.9) can clean up the scanner data.
*/
void destroy_lexor()
{
# ifdef FLEX_SCANNER
# if YY_FLEX_MAJOR_VERSION >= 2 && YY_FLEX_MINOR_VERSION >= 5
# if YY_FLEX_MINOR_VERSION > 5 || defined(YY_FLEX_SUBMINOR_VERSION) && YY_FLEX_SUBMINOR_VERSION >= 9
yylex_destroy();
# endif
# endif
# endif
}