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