1 /* 2 * Copyright (c) 2017 Rob Clark <[email protected]> 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; 52 "["[0-9a-fA-F][0-9a-fA-F]*"]" yylval.num = parse_literal(yytext); return T_LITERAL; 53 54 /* instructions: */ 55 "nop" return TOKEN(T_OP_NOP); 56 "add" return TOKEN(T_OP_ADD); 57 "addhi" return TOKEN(T_OP_ADDHI); 58 "sub" return TOKEN(T_OP_SUB); 59 "subhi" return TOKEN(T_OP_SUBHI); 60 "and" return TOKEN(T_OP_AND); 61 "or" return TOKEN(T_OP_OR); 62 "xor" return TOKEN(T_OP_XOR); 63 "not" return TOKEN(T_OP_NOT); 64 "shl" return TOKEN(T_OP_SHL); 65 "ushr" return TOKEN(T_OP_USHR); 66 "ishr" return TOKEN(T_OP_ISHR); 67 "rot" return TOKEN(T_OP_ROT); 68 "mul8" return TOKEN(T_OP_MUL8); 69 "min" return TOKEN(T_OP_MIN); 70 "max" return TOKEN(T_OP_MAX); 71 "cmp" return TOKEN(T_OP_CMP); 72 "bic" return TOKEN(T_OP_BIC); 73 "msb" return TOKEN(T_OP_MSB); 74 "setbit" return TOKEN(T_OP_SETBIT); 75 "clrbit" return TOKEN(T_OP_CLRBIT); 76 "ubfx" return TOKEN(T_OP_UBFX); 77 "bfi" return TOKEN(T_OP_BFI); 78 "mov" return TOKEN(T_OP_MOV); 79 "cwrite" return TOKEN(T_OP_CWRITE); 80 "cread" return TOKEN(T_OP_CREAD); 81 "swrite" return TOKEN(T_OP_SWRITE); 82 "sread" return TOKEN(T_OP_SREAD); 83 "store" return TOKEN(T_OP_STORE); 84 "load" return TOKEN(T_OP_LOAD); 85 "brne" return TOKEN(T_OP_BRNE); 86 "breq" return TOKEN(T_OP_BREQ); 87 "ret" return TOKEN(T_OP_RET); 88 "iret" return TOKEN(T_OP_IRET); 89 "call" return TOKEN(T_OP_CALL); 90 "jump" return TOKEN(T_OP_JUMP); 91 "jumpa" return TOKEN(T_OP_JUMPA); 92 "sret" return TOKEN(T_OP_SRET); 93 "waitin" return TOKEN(T_OP_WAITIN); 94 "bl" return TOKEN(T_OP_BL); 95 "setsecure" return TOKEN(T_OP_SETSECURE); 96 "<<" return TOKEN(T_LSHIFT); 97 "(rep)" return TOKEN(T_REP); 98 "(xmov"[1-3]")" yylval.num = yytext[5] - '0'; return T_XMOV; 99 "(sds"[1-3]")" yylval.num = yytext[4] - '0'; return T_SDS; 100 "(peek)" return TOKEN(T_PEEK); 101 102 ".align" return TOKEN(T_ALIGN); 103 ".jumptbl" return TOKEN(T_JUMPTBL); 104 ".section" return TOKEN(T_SECTION); 105 106 "," return ','; 107 "[" return '['; 108 "]" return ']'; 109 "+" return '+'; 110 "!" return '!'; 111 ":" return ':'; 112 113 [a-zA-Z_][a-zA-Z0-9_]* yylval.str = strdup(yytext); return T_IDENTIFIER; 114 115 . fprintf(stderr, "error at line %d: Unknown token: %s\n", yyget_lineno(), yytext); yyterminate(); 116 117 %% 118