OpenCores
URL https://opencores.org/ocsvn/zipcpu/zipcpu/trunk

Subversion Repositories zipcpu

[/] [zipcpu/] [trunk/] [sw/] [zasm/] [zasm.l] - Rev 42

Go to most recent revision | Compare with Previous | Blame | View Log

/*******************************************************************************
**
** Filename:    zasm.l
**
** Project:     Zip CPU -- a small, lightweight, RISC CPU core
**
** Purpose:     The lexical analyzer for the assembler.  This converts assembler
**              input into tokens, to feed the parser.
**
**
** Creator:     Dan Gisselquist, Ph.D.
**              Gisselquist Tecnology, LLC
**
********************************************************************************
**
** Copyright (C) 2015, Gisselquist Technology, LLC
**
** This program is free software (firmware): 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 3 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 MERCHANTIBILITY 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.  (It's in the $(ROOT)/doc directory, run make with no
** target there if the PDF file isn't present.)  If not, see
** <http://www.gnu.org/licenses/> for a copy.
**
** License:     GPL, v3, as defined and found on www.gnu.org,
**              http://www.gnu.org/licenses/gpl.html
**
**
*******************************************************************************/

%{
#include <stdio.h>
#include "asmdata.h"
#include "zasm.tab.h"

extern "C" int yylex();
extern "C" int  yywrap() { return 1;}
extern  char *master_input_filename;
%}

%option yylineno
%option warn

%%

"#line"[ \t]+[0-9]+[ \t]+[\"][^"]*[\"][ \t]*$ {
        /* This should really be true *only* at the beginning of a line,
        * however such line beginnings aren't matching.  My guess is that
        * the reason is my line copying code at the bottom that sucks up the
        * newline for LEX.  Hence, this is what we have.
        */
                yylineno = atoi(&yytext[6]);
                char    *bg, *en;
                bg = strchr(yytext, '\"')+1;
                en = strchr(bg, '\"'); *en = '\0';
                if ((master_input_filename == NULL)||(strcmp(master_input_filename, bg)!=0)) {
                        if (!master_input_filename) delete[] master_input_filename;
                        master_input_filename = new char[en-bg+2];
                        strcpy(master_input_filename, bg);
                }
                return '\n';
                }
"#line"[ \t]+[0-9]+[ \t]*$      { yylineno = atoi(&yytext[6]); return '\n';}
^"#line".*$             { printf("WARNING: Unmatched #line: %s\n", yytext); return '\n'; }
"#line".*$              { printf("WARNING: Unmatched #line, not at beginnning: %s\n", yytext); return '\n'; }
[uU][rR]1[0-5]  { yylval.u_reg=(ZPARSER::ZIPREG)(26+yytext[3]-'0');return REG; }
[sS][rR]1[0-5]  { yylval.u_reg=(ZPARSER::ZIPREG)(10+yytext[3]-'0');return REG; }
[rR]1[0-5]      { yylval.u_reg=(ZPARSER::ZIPREG)(10+yytext[2]-'0');return REG; }
[uU][rR][0-9]   { yylval.u_reg=(ZPARSER::ZIPREG)(16+yytext[2]-'0');return REG; }
[sS][rR][0-9]   { yylval.u_reg=(ZPARSER::ZIPREG)(   yytext[2]-'0');return REG; }
[rR][0-9]       { yylval.u_reg=(ZPARSER::ZIPREG)(   yytext[1]-'0');return REG; }
(?i:upc)        { yylval.u_reg = ZPARSER::ZIP_uPC; return REG; }
(?i:spc)        { yylval.u_reg = ZPARSER::ZIP_PC; return REG; }
(?i:pc)         { yylval.u_reg = ZPARSER::ZIP_PC; return REG; }
(?i:ucc)        { yylval.u_reg = ZPARSER::ZIP_uCC; return REG; }
(?i:scc)        { yylval.u_reg = ZPARSER::ZIP_CC; return REG; }
(?i:cc)         { yylval.u_reg = ZPARSER::ZIP_CC; return REG; }
(?i:usp)        { yylval.u_reg = ZPARSER::ZIP_uSP; return REG; }
(?i:ssp)        { yylval.u_reg = ZPARSER::ZIP_SP; return REG; }
(?i:sp)         { yylval.u_reg = ZPARSER::ZIP_SP; return REG; }
[bB][rR][aA]    { yylval.u_op  = OP_BRA; return BRANCHOP; }
[bB][rR][zZ]    { yylval.u_op  = OP_BZ;  return BRANCHOP; }
[bB][zZ]        { yylval.u_op  = OP_BZ;  return BRANCHOP; }
[bB][nN][zZ]    { yylval.u_op  = OP_BNZ; return BRANCHOP; }
[bB][nN][eE]    { yylval.u_op  = OP_BNZ; return BRANCHOP; }
[bB][gG][eE]    { yylval.u_op  = OP_BGE; return BRANCHOP; }
[bB][gG][tT]    { yylval.u_op  = OP_BGT; return BRANCHOP; }
[bB][gG]        { yylval.u_op  = OP_BGT; return BRANCHOP; }
[bB][lL][tT]    { yylval.u_op  = OP_BLT; return BRANCHOP; }
[bB][nN]        { yylval.u_op  = OP_BLT; return BRANCHOP; }
[bB][rR][cC]    { yylval.u_op  = OP_BRC; return BRANCHOP; }
[bB][cC]        { yylval.u_op  = OP_BRC; return BRANCHOP; }
[bB][rR][vV]    { yylval.u_op  = OP_BRV; return BRANCHOP; }
[bB][vV]        { yylval.u_op  = OP_BRV; return BRANCHOP; }
(?i:clr)        { yylval.u_op  = OP_CLR; return SINGLOP; }
(?i:clrf)       {yylval.u_op  = OP_CLRF;return SINGLOP; }
(?i:int)        { yylval.u_op  = OP_TRAP;return SINGLOP; }
(?i:trap)       {yylval.u_op  = OP_TRAP;return SINGLOP; }
(?i:jmp)        { yylval.u_op  = OP_JMP; return SINGLOP; }
[lL][jJ][mM][pP] {yylval.u_op  = OP_LJMP;return SINGLOP; }
(?i:neg)        { yylval.u_op  = OP_NEG; return SINGLOP; }
(?i:not)        { yylval.u_op  = OP_NOT; return SINGLOP; }
(?i:cmp)        { yylval.u_op  = OP_CMP; return DUALOP; }
(?i:tst)        { yylval.u_op  = OP_TST; return DUALOP; }
(?i:mov)        { yylval.u_op  = OP_MOV; return DUALOP; }
(?i:ldi)        { yylval.u_op  = OP_LDI; return LDIOP; }
[lL][dD][iI][hH][iI]    { yylval.u_op =OP_LDIHI; return LDHLOP; }
[lL][dD][iI][lL][oO]    { yylval.u_op =OP_LDILO; return LDHLOP; }
(?i:mpyu)       { yylval.u_op = OP_MPYU; return DUALOP; }
(?i:mpys)       { yylval.u_op = OP_MPYS; return DUALOP; }
(?i:rol)        { yylval.u_op  = OP_ROL; return DUALOP; }
(?i:sub)        { yylval.u_op  = OP_SUB; return DUALOP; }
(?i:and)        { yylval.u_op  = OP_AND; return DUALOP; }
(?i:add)        { yylval.u_op  = OP_ADD; return DUALOP; }
(?i:or)         { yylval.u_op  = OP_OR; return DUALOP;; }
(?i:xor)        { yylval.u_op  = OP_XOR; return DUALOP; }
(?i:lsl)        { yylval.u_op  = OP_LSL; return DUALOP; }
(?i:asr)        { yylval.u_op  = OP_ASR; return DUALOP; }
(?i:lsr)        { yylval.u_op  = OP_LSR; return DUALOP; }
(?i:lod)        { yylval.u_op = OP_LOD;   return LOADOP; }
(?i:sto)        { yylval.u_op = OP_STO;   return STOROP; }
(?i:halt)       { yylval.u_op = OP_HALT;  return BAREOP;  }
(?i:wait)       { yylval.u_op = OP_HALT;  return BAREOP;  }
(?i:rtu)        { yylval.u_op = OP_RTU;   return BAREOP;  }
(?i:nop)        { yylval.u_op = OP_NOOP;  return BAREOP; }
(?i:noop)       { yylval.u_op = OP_NOOP;  return BAREOP; }
(?i:break)      { yylval.u_op = OP_BREAK; return BAREOP; }
(?i:brk)        { yylval.u_op = OP_BREAK; return BAREOP; }
(?i:busy)       { yylval.u_op = OP_BUSY;  return BAREOP; }
(?i:equ)        { return EQU; }
(?i:fill)       { return FILL; }
(?i:word)       { return WORD; }
"__"[hH][eE][rR][eE]"__" { return HERE; }
[\.][dD][aA][tT] { return WORD; }
[\.][zZ]/[ \t\n]        { yylval.u_cond = ZPARSER::ZIPC_Z; return COND; }
[\.][nN][eE]/[ \t\n]    { yylval.u_cond = ZPARSER::ZIPC_NZ; return COND; }
[\.][nN][zZ]/[ \t\n]    { yylval.u_cond = ZPARSER::ZIPC_NZ; return COND; }
[\.][gG][eE]/[ \t\n]    { yylval.u_cond = ZPARSER::ZIPC_GE; return COND; }
[\.][gG][tT]/[ \t\n]    { yylval.u_cond = ZPARSER::ZIPC_GT; return COND; }
[\.][lL][tT]/[ \t\n]    { yylval.u_cond = ZPARSER::ZIPC_LT; return COND; }
[\.][nN]/[ \t\n]        { yylval.u_cond = ZPARSER::ZIPC_LT; return COND; }
[\.][cC]/[ \t\n]        { yylval.u_cond = ZPARSER::ZIPC_C; return COND; }
[\.][vV]/[ \t\n]        { yylval.u_cond = ZPARSER::ZIPC_V; return COND; }
[_A-Za-z][_a-zA-Z0-9]* { yylval.u_id  = strdup(yytext); return IDENTIFIER; }
"$"?[-]?0[xX][0-9A-Fa-f]+ { yylval.u_ival = strtoul(yytext+(((*yytext)=='$')?1:0),NULL,16);return INT;}
"$"?[-]?0[0-7]+           { yylval.u_ival = strtoul(yytext+(((*yytext)=='$')?1:0),NULL, 8);return INT;}
"$"?[-]?[1-9][0-9]*       { yylval.u_ival = strtoul(yytext+(((*yytext)=='$')?1:0),NULL,10);return INT;}
"$"?[-]?[1-9A-Fa-f][0-9A-Fa-f]*h {yylval.u_ival=strtoul(yytext+(((*yytext)=='$')?1:0),NULL,16); return INT;}
"$"?[-]?[0-7]+o           { yylval.u_ival = strtoul(yytext+(((*yytext)=='$')?1:0),NULL, 8);return INT;}
"\'"[^\\\']"\'"         { printf("Match char, %s\n", yytext); yylval.u_ival = yytext[1]&0x0ff; return INT; }
"\'\\"[abnr\\]"\'"              {
                yylval.u_ival = yytext[2];
                if (yytext[2] == 'a') yylval.u_ival = '\a';
                else if (yytext[2] == 'b') yylval.u_ival = '\b';
                else if (yytext[2] == 'f') yylval.u_ival = '\f';
                else if (yytext[2] == 'n') yylval.u_ival = '\n';
                else if (yytext[2] == 'r') yylval.u_ival = '\r';
                else if (yytext[2] == 't') yylval.u_ival = '\t';
                else if (yytext[2] == 'v') yylval.u_ival = '\v';
                else if (yytext[2] == '0') yylval.u_ival = '\0';
                else if (yytext[2] == '\\') yylval.u_ival = '\\';
                return INT; }
"\'"[^\\\'][^\\\']"\'"          { yylval.u_ival = ((yytext[1]&0x0ff)<<8)+(yytext[2]&0x0ff); return INT; }
"\'"[^\\\'][^\\\'][^\\\']"\'"           { yylval.u_ival = ((yytext[1]&0x0ff)<<16)
                                        +((yytext[2]&0x0ff)<<8)
                                        +(yytext[3]&0x0ff); return INT; }
"\'"[^\\\'][^\\\'][^\\\'][^\\\']"\'"    { yylval.u_ival = ((yytext[1]&0x0ff)<<24)
                                        +((yytext[2]&0x0ff)<<16)
                                        +((yytext[3]&0x0ff)<<8)
                                        +(yytext[4]&0x0ff); return INT; }
"$"?"0"            { yylval.u_ival = 0;return INT;}
"$"             { return DOLLAR; }
","             { return COMMA; }
"+"             { return PLUS; }
"-"             { return MINUS; }
"*"             { return TIMES; }
"||"            { return BOOLEANOR; }
"|"             { return BITWISEOR; }
"&&"            { return BOOLEANAND; }
"&"             { return BITWISEAND; }
"^"             { return BITWISEXOR; }
":"             { return COLON; }
"."             { return DOT; }
"%"             { return '%'; }
[ \t]+          { }
[(]             { return '('; }
[)]             { return ')'; }
\n.*            { if (linecp) free(linecp); linecp = strdup(&yytext[1]); yyless(1); return '\n'; }

%%

Go to most recent revision | Compare with Previous | Blame | View Log

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.