1 |
18 |
umairsiddi |
/*
|
2 |
|
|
#--------------------------------------------------------------
|
3 |
|
|
#-- HPC-16 Assembler
|
4 |
|
|
#--------------------------------------------------------------
|
5 |
|
|
#-- project: HPC-16 Microprocessor
|
6 |
|
|
#--
|
7 |
|
|
#-- ANTLR4 grammar on HPC-16 Assembler
|
8 |
|
|
#--
|
9 |
|
|
#--
|
10 |
|
|
#--
|
11 |
|
|
#-- Author: M. Umair Siddiqui (umairsiddiqui@opencores.org)
|
12 |
|
|
#---------------------------------------------------------------
|
13 |
|
|
#------------------------------------------------------------------------------------
|
14 |
|
|
#-- --
|
15 |
|
|
#-- Copyright (c) 2015, M. Umair Siddiqui all rights reserved --
|
16 |
|
|
#-- --
|
17 |
|
|
#-- This file is part of HPC-16. --
|
18 |
|
|
#-- --
|
19 |
|
|
#-- HPC-16 is free software; you can redistribute it and/or modify --
|
20 |
|
|
#-- it under the terms of the GNU Lesser General Public License as published by --
|
21 |
|
|
#-- the Free Software Foundation; either version 2.1 of the License, or --
|
22 |
|
|
#-- (at your option) any later version. --
|
23 |
|
|
#-- --
|
24 |
|
|
#-- HPC-16 is distributed in the hope that it will be useful, --
|
25 |
|
|
#-- but WITHOUT ANY WARRANTY; without even the implied warranty of --
|
26 |
|
|
#-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the --
|
27 |
|
|
#-- GNU Lesser General Public License for more details. --
|
28 |
|
|
#-- --
|
29 |
|
|
#-- You should have received a copy of the GNU Lesser General Public License --
|
30 |
|
|
#-- along with HPC-16; if not, write to the Free Software --
|
31 |
|
|
#-- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA --
|
32 |
|
|
#-- --
|
33 |
|
|
#------------------------------------------------------------------------------------
|
34 |
|
|
*/
|
35 |
|
|
|
36 |
|
|
grammar HPC16;
|
37 |
|
|
|
38 |
|
|
prog : stat+;
|
39 |
|
|
|
40 |
|
|
stat : asm_inst? NEWLINE;
|
41 |
|
|
|
42 |
|
|
asm_inst : 'mov' REG ',' REG # mov_reg_reg
|
43 |
|
|
| 'mov' STK_PTR ',' REG # mov_sp_reg
|
44 |
|
|
| 'mov' REG ',' STK_PTR # mov_reg_sp
|
45 |
|
|
| 'ld' REG ',' '(' REG ')' # ld_reg_reg
|
46 |
|
|
| 'ld' REG ',' '(' REG '+' IMM ')' # ld_reg_reg_imm16
|
47 |
|
|
| 'ld' REG ',' '(' STK_PTR ')' # ld_reg_sp
|
48 |
|
|
| 'ld' REG ',' '(' STK_PTR '+' IMM ')' # ld_reg_sp_imm16
|
49 |
|
|
| 'st' REG ',' '(' REG ')' # st_reg_reg
|
50 |
|
|
| 'st' REG ',' '(' REG '+' IMM ')' # st_reg_reg_imm16
|
51 |
|
|
| 'st' REG ',' '(' STK_PTR ')' # st_reg_sp
|
52 |
|
|
| 'st' REG ',' '(' STK_PTR '+' IMM ')' # st_reg_sp_imm16
|
53 |
|
|
| 'lbzx' REG ',' '(' REG ')' # lbzx_reg_reg
|
54 |
|
|
| 'lbzx' REG ',' '(' REG '+' IMM ')' # lbzx_reg_reg_imm16
|
55 |
|
|
| 'lbsx' REG ',' '(' REG ')' # lbsx_reg_reg
|
56 |
|
|
| 'lbsx' REG ',' '(' REG '+' IMM ')' # lbsx_reg_reg_imm16
|
57 |
|
|
| 'sb' REG ',' '(' REG ')' # sb_reg_reg
|
58 |
|
|
| 'sb' REG ',' '(' REG '+' IMM ')' # sb_reg_reg_imm16
|
59 |
|
|
| 'li' REG ',' IMM # li_reg_imm16
|
60 |
|
|
| 'li' STK_PTR ',' IMM # li_sp_imm16
|
61 |
|
|
| 'inc' REG # inc_reg
|
62 |
|
|
| 'dec' REG # dec_reg
|
63 |
|
|
| 'sub' REG ',' REG # sub_reg_reg
|
64 |
|
|
| 'and' REG ',' REG # and_reg_reg
|
65 |
|
|
| 'add' REG ',' REG # add_reg_reg
|
66 |
|
|
| 'adc' REG ',' REG # adc_reg_reg
|
67 |
|
|
| 'sbb' REG ',' REG # sbb_reg_reg
|
68 |
|
|
| 'or' REG ',' REG # or_reg_reg
|
69 |
|
|
| 'xor' REG ',' REG # xor_reg_reg
|
70 |
|
|
| 'not' REG # not_reg
|
71 |
|
|
| 'sub' REG ',' IMM # sub_reg_imm16
|
72 |
|
|
| 'and' REG ',' IMM # and_reg_imm16
|
73 |
|
|
| 'add' REG ',' IMM # add_reg_imm16
|
74 |
|
|
| 'adc' REG ',' IMM # adc_reg_imm16
|
75 |
|
|
| 'sbb' REG ',' IMM # sbb_reg_imm16
|
76 |
|
|
| 'or' REG ',' IMM # or_reg_imm16
|
77 |
|
|
| 'xor' REG ',' IMM # xor_reg_imm16
|
78 |
|
|
| 'add' STK_PTR ',' IMM # add_sp_imm16
|
79 |
|
|
| 'sub' STK_PTR ',' IMM # sub_sp_imm16
|
80 |
|
|
| 'cmp' REG ',' REG # cmp_reg_reg
|
81 |
|
|
| 'test' REG ',' REG # test_reg_reg
|
82 |
|
|
| 'cmp' REG ',' IMM # cmp_reg_imm16
|
83 |
|
|
| 'test' REG ',' IMM # test_reg_imm16
|
84 |
|
|
| 'sll' REG ',' REG # sll_reg_reg
|
85 |
|
|
| 'slr' REG ',' REG # slr_reg_reg
|
86 |
|
|
| 'sal' REG ',' REG # sal_reg_reg
|
87 |
|
|
| 'sar' REG ',' REG # sar_reg_reg
|
88 |
|
|
| 'rol' REG ',' REG # rol_reg_reg
|
89 |
|
|
| 'ror' REG ',' REG # ror_reg_reg
|
90 |
|
|
| 'rcl' REG ',' REG # rcl_reg_reg
|
91 |
|
|
| 'rcr' REG ',' REG # rcr_reg_reg
|
92 |
|
|
| 'sll' REG ',' IMM # sll_reg_imm4
|
93 |
|
|
| 'slr' REG ',' IMM # slr_reg_imm4
|
94 |
|
|
| 'sal' REG ',' IMM # sal_reg_imm4
|
95 |
|
|
| 'sar' REG ',' IMM # sar_reg_imm4
|
96 |
|
|
| 'rol' REG ',' IMM # rol_reg_imm4
|
97 |
|
|
| 'ror' REG ',' IMM # ror_reg_imm4
|
98 |
|
|
| 'rcl' REG ',' IMM # rcl_reg_imm4
|
99 |
|
|
| 'rcr' REG ',' IMM # rcr_reg_imm4
|
100 |
|
|
| 'push' REG # push_reg
|
101 |
|
|
| 'pushf' # pushf
|
102 |
|
|
| 'pop' REG # pop_reg
|
103 |
|
|
| 'popf' # popf
|
104 |
|
|
| 'acall' '(' REG ')' # acall_reg
|
105 |
|
|
| 'call' '(' REG ')' # call_reg
|
106 |
|
|
| 'call' '(' IMM ')' # call_imm11
|
107 |
|
|
| 'ret' # ret
|
108 |
|
|
| 'iret' # iret
|
109 |
|
|
| 'clc' # clc
|
110 |
|
|
| 'stc' # stc
|
111 |
|
|
| 'cmc' # cmc
|
112 |
|
|
| 'cli' # cli
|
113 |
|
|
| 'sti' # sti
|
114 |
|
|
| 'int' IMM # int_imm4
|
115 |
|
|
| 'into' # into
|
116 |
|
|
| 'ajmp' '(' REG ')' # ajmp
|
117 |
|
|
| 'jmp' '(' REG ')' # jmp_reg
|
118 |
|
|
| 'jmp' '(' IMM ')' # jmp_imm11
|
119 |
|
|
| 'nop' # nop
|
120 |
|
|
| 'hlt' # halt
|
121 |
|
|
| 'jo' IMM # jo_imm7
|
122 |
|
|
| 'jno' IMM # jno_imm7
|
123 |
|
|
| 'jb' IMM # jb_imm7
|
124 |
|
|
| 'jnae' IMM # jnae_imm7
|
125 |
|
|
| 'jnb' IMM # jnb_imm7
|
126 |
|
|
| 'jae' IMM # jae_imm7
|
127 |
|
|
| 'je' IMM # je_imm7
|
128 |
|
|
| 'jz' IMM # jz_imm7
|
129 |
|
|
| 'jne' IMM # jne_imm7
|
130 |
|
|
| 'jnz' IMM # jnz_imm7
|
131 |
|
|
| 'jbe' IMM # jbe_imm7
|
132 |
|
|
| 'jna' IMM # jna_imm7
|
133 |
|
|
| 'jnbe' IMM # jnbe_imm7
|
134 |
|
|
| 'ja' IMM # ja_imm7
|
135 |
|
|
| 'js' IMM # js_imm7
|
136 |
|
|
| 'jns' IMM # jns_imm7
|
137 |
|
|
| 'jl' IMM # jl_imm7
|
138 |
|
|
| 'jnge' IMM # jnge_imm7
|
139 |
|
|
| 'jnl' IMM # jnl_imm7
|
140 |
|
|
| 'jge' IMM # jge_imm7
|
141 |
|
|
| 'jle' IMM # jle_imm7
|
142 |
|
|
| 'jng' IMM # jng_imm7
|
143 |
|
|
| 'jnle' IMM # jnle_imm7
|
144 |
|
|
| 'jg' IMM # jg_imm7
|
145 |
|
|
;
|
146 |
|
|
|
147 |
|
|
REG : [Rr][0-9]+;
|
148 |
|
|
|
149 |
|
|
IMM : [0-9]+
|
150 |
|
|
| '0x'[0-9a-fA-F]+
|
151 |
|
|
;
|
152 |
|
|
|
153 |
|
|
STK_PTR : 'SP'
|
154 |
|
|
| 'sp'
|
155 |
|
|
;
|
156 |
|
|
|
157 |
|
|
WS : [ \t]+ -> skip ;
|
158 |
|
|
LINE_COMMENT : '#' .*? '\r'? '\n' -> skip ; // Match "#" stuff '\n'
|
159 |
|
|
NEWLINE : '\r'?'\n';
|
160 |
|
|
|
161 |
|
|
|
162 |
|
|
|