1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright (c) 2017 Rob Clark <robdclark@gmail.com> 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci * 11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 13bf215546Sopenharmony_ci * Software. 14bf215546Sopenharmony_ci * 15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21bf215546Sopenharmony_ci * SOFTWARE. 22bf215546Sopenharmony_ci */ 23bf215546Sopenharmony_ci 24bf215546Sopenharmony_ci%{ 25bf215546Sopenharmony_ci#include <stdlib.h> 26bf215546Sopenharmony_ci#include "parser.h" 27bf215546Sopenharmony_ci#include "asm.h" 28bf215546Sopenharmony_ci 29bf215546Sopenharmony_ci#define YY_NO_INPUT 30bf215546Sopenharmony_ci#define YY_NO_UNPUT 31bf215546Sopenharmony_ci 32bf215546Sopenharmony_ci#define TOKEN(t) (yylval.tok = t) 33bf215546Sopenharmony_ciextern YYSTYPE yylval; 34bf215546Sopenharmony_ci 35bf215546Sopenharmony_ci%} 36bf215546Sopenharmony_ci 37bf215546Sopenharmony_ci%option noyywrap 38bf215546Sopenharmony_ci 39bf215546Sopenharmony_ci%% 40bf215546Sopenharmony_ci"\n" yylineno++; 41bf215546Sopenharmony_ci[ \t] ; /* ignore whitespace */ 42bf215546Sopenharmony_ci";"[^\n]*"\n" yylineno++; /* ignore comments */ 43bf215546Sopenharmony_ci0|[1-9][0-9]* yylval.num = strtoul(yytext, NULL, 0); return T_INT; 44bf215546Sopenharmony_ci"0x"[0-9a-fA-F]* yylval.num = strtoul(yytext, NULL, 0); return T_HEX; 45bf215546Sopenharmony_ci 46bf215546Sopenharmony_ci"$"[0-9a-fA-F][0-9a-fA-F] yylval.num = parse_reg(yytext); return T_REGISTER; 47bf215546Sopenharmony_ci"$"[a-zA-Z][a-zA-Z0-9]* yylval.num = parse_reg(yytext); return T_REGISTER; 48bf215546Sopenharmony_ci"b"[0-9][0-9]* yylval.num = parse_bit(yytext); return T_BIT; 49bf215546Sopenharmony_ci"@"[a-zA-Z_][a-zA-Z0-9_]* yylval.num = parse_control_reg(yytext); return T_CONTROL_REG; 50bf215546Sopenharmony_ci"#"[a-zA-Z_][a-zA-Z0-9_]* yylval.str = strdup(yytext+1); return T_LABEL_REF; /* label reference */ 51bf215546Sopenharmony_ci[a-zA-Z_][a-zA-Z0-9_]*":" yylval.str = parse_label_decl(yytext); return T_LABEL_DECL; /* label declaration */ 52bf215546Sopenharmony_ci"["[0-9a-fA-F][0-9a-fA-F]*"]" yylval.num = parse_literal(yytext); return T_LITERAL; 53bf215546Sopenharmony_ci 54bf215546Sopenharmony_ci /* instructions: */ 55bf215546Sopenharmony_ci"nop" return TOKEN(T_OP_NOP); 56bf215546Sopenharmony_ci"add" return TOKEN(T_OP_ADD); 57bf215546Sopenharmony_ci"addhi" return TOKEN(T_OP_ADDHI); 58bf215546Sopenharmony_ci"sub" return TOKEN(T_OP_SUB); 59bf215546Sopenharmony_ci"subhi" return TOKEN(T_OP_SUBHI); 60bf215546Sopenharmony_ci"and" return TOKEN(T_OP_AND); 61bf215546Sopenharmony_ci"or" return TOKEN(T_OP_OR); 62bf215546Sopenharmony_ci"xor" return TOKEN(T_OP_XOR); 63bf215546Sopenharmony_ci"not" return TOKEN(T_OP_NOT); 64bf215546Sopenharmony_ci"shl" return TOKEN(T_OP_SHL); 65bf215546Sopenharmony_ci"ushr" return TOKEN(T_OP_USHR); 66bf215546Sopenharmony_ci"ishr" return TOKEN(T_OP_ISHR); 67bf215546Sopenharmony_ci"rot" return TOKEN(T_OP_ROT); 68bf215546Sopenharmony_ci"mul8" return TOKEN(T_OP_MUL8); 69bf215546Sopenharmony_ci"min" return TOKEN(T_OP_MIN); 70bf215546Sopenharmony_ci"max" return TOKEN(T_OP_MAX); 71bf215546Sopenharmony_ci"cmp" return TOKEN(T_OP_CMP); 72bf215546Sopenharmony_ci"msb" return TOKEN(T_OP_MSB); 73bf215546Sopenharmony_ci"mov" return TOKEN(T_OP_MOV); 74bf215546Sopenharmony_ci"cwrite" return TOKEN(T_OP_CWRITE); 75bf215546Sopenharmony_ci"cread" return TOKEN(T_OP_CREAD); 76bf215546Sopenharmony_ci"store" return TOKEN(T_OP_STORE); 77bf215546Sopenharmony_ci"load" return TOKEN(T_OP_LOAD); 78bf215546Sopenharmony_ci"brne" return TOKEN(T_OP_BRNE); 79bf215546Sopenharmony_ci"breq" return TOKEN(T_OP_BREQ); 80bf215546Sopenharmony_ci"ret" return TOKEN(T_OP_RET); 81bf215546Sopenharmony_ci"iret" return TOKEN(T_OP_IRET); 82bf215546Sopenharmony_ci"call" return TOKEN(T_OP_CALL); 83bf215546Sopenharmony_ci"jump" return TOKEN(T_OP_JUMP); 84bf215546Sopenharmony_ci"waitin" return TOKEN(T_OP_WAITIN); 85bf215546Sopenharmony_ci"preemptleave" return TOKEN(T_OP_PREEMPTLEAVE); 86bf215546Sopenharmony_ci"setsecure" return TOKEN(T_OP_SETSECURE); 87bf215546Sopenharmony_ci"<<" return TOKEN(T_LSHIFT); 88bf215546Sopenharmony_ci"(rep)" return TOKEN(T_REP); 89bf215546Sopenharmony_ci"(xmov"[1-3]")" yylval.num = yytext[5] - '\0'; return T_XMOV; 90bf215546Sopenharmony_ci 91bf215546Sopenharmony_ci"," return ','; 92bf215546Sopenharmony_ci"[" return '['; 93bf215546Sopenharmony_ci"]" return ']'; 94bf215546Sopenharmony_ci"+" return '+'; 95bf215546Sopenharmony_ci 96bf215546Sopenharmony_ci. fprintf(stderr, "error at line %d: Unknown token: %s\n", yyget_lineno(), yytext); yyterminate(); 97bf215546Sopenharmony_ci 98bf215546Sopenharmony_ci%% 99