Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / legion / src / support / reggen.l
/*
* ========== Copyright Header Begin ==========================================
*
* OpenSPARC T2 Processor File: reggen.l
* Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES.
*
* The above named program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License version 2 as published by the Free Software Foundation.
*
* The above named 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 work; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*
* ========== Copyright Header End ============================================
*/
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* ident "@(#)reggen.l 1.5 05/01/12 SMI"
*/
%{
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>
#include <errno.h>
#include <sys/param.h> /* for MAXPATHLEN */
#include <string.h> /* for strchr */
#include <ctype.h> /* for islower / toupper */
#define MAXBUF 1024
void fatal(char *, ...);
void strcasecpy(char * top, char * fromp);
int yywrap();
FILE * dotc, * doth;
char * basenamep;
char * structnamep;
char * specfilep;
int enumidx;
int alias;
char aliasname[MAXBUF];
%}
%%
<<EOF>> return 0;
[ \t]+ { }
\n {
if (alias > 0) {
alias = 0;
enumidx++;
}
}
[A-Za-z][A-Za-z0-9]* {
if (!alias) {
sprintf(aliasname, "Reg_%s_%s", structnamep, yytext);
fprintf(doth,"\t%s = %d, \n", aliasname, enumidx);
fprintf(dotc,"\t{ \"%s\", Reg_%s_%s },\n", yytext, structnamep, yytext);
} else {
fprintf(doth,"\t\tReg_%s_%s = %s,\n",
structnamep, yytext, aliasname);
fprintf(dotc,"\t{ \"%s\", %s },\n", yytext, aliasname);
}
alias ++;
}
#.* { /* swallow comments */ }
. {
fatal("Illegal spec text %s", yytext);
}
%%
int main(int argc, char ** argv)
{
char bufp[MAXPATHLEN];
char caps[MAXPATHLEN];
char * bnamep;
if (argc != 3) fatal("usage: %s <basefilename> <basestructname>\n",
argv[0]);
basenamep = argv[1];
structnamep = argv[2];
if (strlen(structnamep)>MAXBUF-100) fatal("basestructname is too long!");
/* Initialise the .h and .c files */
sprintf(bufp, "%s.c", basenamep);
dotc = fopen(bufp, "w");
if (dotc == NULL) fatal("creating %s", bufp);
sprintf(bufp, "%s.h", basenamep);
doth = fopen(bufp, "w");
if (doth == NULL) fatal("creating %s", bufp);
/* remove any earlier path ... */
bnamep = strrchr(basenamep, '/');
if (bnamep == (char*)0)
bnamep = basenamep;
else
bnamep ++;
strcasecpy(caps, bnamep);
fprintf(dotc,"\
/* Autogenerated file - DO NOT EDIT */\n\
#include \"basics.h\"\n\
#include \"simcore.h\"\n\
#include \"config.h\"\n\
#include \"regmap.h\"\n\
#include \"%s.h\"\n\
\n\
reg_map_t %s_reg_map[]={\n",
bnamep, structnamep);
fprintf(doth,"\
#ifndef _%s_H_\n\
#define _%s_H_\n\
\n\
typedef enum {\n", caps, caps);
alias = 0;
enumidx = 0;
yylex();
fprintf(dotc,"\n\
\t{ (char *)0, -1 }\n\
};\n" );
fclose(dotc);
fprintf(doth,"\
\tReg_%s_TOTAL = %d\n\
} %s_reg_t;\n\
\n\
#endif\n", structnamep, enumidx, structnamep);
fclose(doth);
exit(0);
return 0; /* compiler joy */
}
void fatal(char* fmt, ...)
{
va_list args;
if (errno!=0) perror("FATAL: "); else fprintf(stderr,"FATAL: ");
if (fmt) {
va_start(args, fmt);
(void)vfprintf(stderr, fmt, args);
va_end(args);
}
fprintf(stderr,"\n");
fflush(stderr);
fflush(stdout);
exit(1);
}
void strcasecpy(char * top, char * fromp)
{
do {
*top++ = islower((unsigned)*fromp) ? toupper((unsigned)*fromp) : *fromp;
} while (*fromp++ != '\0');
}
int yywrap()
{
return 1;
}