17ec681f3Smrg/*
27ec681f3Smrg * Copyright (c) 2017 Rob Clark <robdclark@gmail.com>
37ec681f3Smrg *
47ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
57ec681f3Smrg * copy of this software and associated documentation files (the "Software"),
67ec681f3Smrg * to deal in the Software without restriction, including without limitation
77ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
87ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the
97ec681f3Smrg * Software is furnished to do so, subject to the following conditions:
107ec681f3Smrg *
117ec681f3Smrg * The above copyright notice and this permission notice (including the next
127ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the
137ec681f3Smrg * Software.
147ec681f3Smrg *
157ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
167ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
177ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
187ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
197ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
207ec681f3Smrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
217ec681f3Smrg * SOFTWARE.
227ec681f3Smrg */
237ec681f3Smrg
247ec681f3Smrg%{
257ec681f3Smrg#include <stdlib.h>
267ec681f3Smrg#include "parser.h"
277ec681f3Smrg#include "asm.h"
287ec681f3Smrg
297ec681f3Smrg#define YY_NO_INPUT
307ec681f3Smrg#define YY_NO_UNPUT
317ec681f3Smrg
327ec681f3Smrg#define TOKEN(t) (yylval.tok = t)
337ec681f3Smrgextern YYSTYPE yylval;
347ec681f3Smrg
357ec681f3Smrg%}
367ec681f3Smrg
377ec681f3Smrg%option noyywrap
387ec681f3Smrg
397ec681f3Smrg%%
407ec681f3Smrg"\n"                              yylineno++;
417ec681f3Smrg[ \t]                             ; /* ignore whitespace */
427ec681f3Smrg";"[^\n]*"\n"                     yylineno++; /* ignore comments */
437ec681f3Smrg0|[1-9][0-9]*                     yylval.num = strtoul(yytext, NULL, 0);    return T_INT;
447ec681f3Smrg"0x"[0-9a-fA-F]*                  yylval.num = strtoul(yytext, NULL, 0);    return T_HEX;
457ec681f3Smrg
467ec681f3Smrg"$"[0-9a-fA-F][0-9a-fA-F]         yylval.num = parse_reg(yytext); return T_REGISTER;
477ec681f3Smrg"$"[a-zA-Z][a-zA-Z0-9]*           yylval.num = parse_reg(yytext); return T_REGISTER;
487ec681f3Smrg"b"[0-9][0-9]*                    yylval.num = parse_bit(yytext); return T_BIT;
497ec681f3Smrg"@"[a-zA-Z_][a-zA-Z0-9_]*         yylval.num = parse_control_reg(yytext); return T_CONTROL_REG;
507ec681f3Smrg"#"[a-zA-Z_][a-zA-Z0-9_]*         yylval.str = strdup(yytext+1);  return T_LABEL_REF; /* label reference */
517ec681f3Smrg[a-zA-Z_][a-zA-Z0-9_]*":"         yylval.str = parse_label_decl(yytext); return T_LABEL_DECL; /* label declaration */
527ec681f3Smrg"["[0-9a-fA-F][0-9a-fA-F]*"]"     yylval.num = parse_literal(yytext); return T_LITERAL;
537ec681f3Smrg
547ec681f3Smrg                                  /* instructions: */
557ec681f3Smrg"nop"                             return TOKEN(T_OP_NOP);
567ec681f3Smrg"add"                             return TOKEN(T_OP_ADD);
577ec681f3Smrg"addhi"                           return TOKEN(T_OP_ADDHI);
587ec681f3Smrg"sub"                             return TOKEN(T_OP_SUB);
597ec681f3Smrg"subhi"                           return TOKEN(T_OP_SUBHI);
607ec681f3Smrg"and"                             return TOKEN(T_OP_AND);
617ec681f3Smrg"or"                              return TOKEN(T_OP_OR);
627ec681f3Smrg"xor"                             return TOKEN(T_OP_XOR);
637ec681f3Smrg"not"                             return TOKEN(T_OP_NOT);
647ec681f3Smrg"shl"                             return TOKEN(T_OP_SHL);
657ec681f3Smrg"ushr"                            return TOKEN(T_OP_USHR);
667ec681f3Smrg"ishr"                            return TOKEN(T_OP_ISHR);
677ec681f3Smrg"rot"                             return TOKEN(T_OP_ROT);
687ec681f3Smrg"mul8"                            return TOKEN(T_OP_MUL8);
697ec681f3Smrg"min"                             return TOKEN(T_OP_MIN);
707ec681f3Smrg"max"                             return TOKEN(T_OP_MAX);
717ec681f3Smrg"cmp"                             return TOKEN(T_OP_CMP);
727ec681f3Smrg"msb"                             return TOKEN(T_OP_MSB);
737ec681f3Smrg"mov"                             return TOKEN(T_OP_MOV);
747ec681f3Smrg"cwrite"                          return TOKEN(T_OP_CWRITE);
757ec681f3Smrg"cread"                           return TOKEN(T_OP_CREAD);
767ec681f3Smrg"store"                           return TOKEN(T_OP_STORE);
777ec681f3Smrg"load"                            return TOKEN(T_OP_LOAD);
787ec681f3Smrg"brne"                            return TOKEN(T_OP_BRNE);
797ec681f3Smrg"breq"                            return TOKEN(T_OP_BREQ);
807ec681f3Smrg"ret"                             return TOKEN(T_OP_RET);
817ec681f3Smrg"iret"                            return TOKEN(T_OP_IRET);
827ec681f3Smrg"call"                            return TOKEN(T_OP_CALL);
837ec681f3Smrg"jump"                            return TOKEN(T_OP_JUMP);
847ec681f3Smrg"waitin"                          return TOKEN(T_OP_WAITIN);
857ec681f3Smrg"preemptleave"			  return TOKEN(T_OP_PREEMPTLEAVE);
867ec681f3Smrg"setsecure"			  return TOKEN(T_OP_SETSECURE);
877ec681f3Smrg"<<"                              return TOKEN(T_LSHIFT);
887ec681f3Smrg"(rep)"                           return TOKEN(T_REP);
897ec681f3Smrg"(xmov"[1-3]")"	                  yylval.num = yytext[5] - '\0'; return T_XMOV;
907ec681f3Smrg
917ec681f3Smrg","                               return ',';
927ec681f3Smrg"["                               return '[';
937ec681f3Smrg"]"                               return ']';
947ec681f3Smrg"+"                               return '+';
957ec681f3Smrg
967ec681f3Smrg.                                 fprintf(stderr, "error at line %d: Unknown token: %s\n", yyget_lineno(), yytext); yyterminate();
977ec681f3Smrg
987ec681f3Smrg%%
99