]> Witch of Git - jade-rose/blob - toolchain/src/inst/test.rs
Add support for binary, hex, and octal literals
[jade-rose] / toolchain / src / inst / test.rs
1 use super::{decode::Decode, encode::Encode, parse, Inst};
2 use proptest::prelude::*;
3 use std::io::Cursor;
4
5 proptest! {
6 #[test]
7 fn roundtrip(inst: Inst) {
8 let mut buf = [0, 0];
9 let mut cursor = Cursor::new(&mut buf[..]);
10 inst.encode(&mut cursor).expect("to encode");
11 cursor.set_position(0);
12 let decoded = Inst::decode(&mut cursor).expect("to decode");
13 prop_assert_eq!(inst, decoded);
14 }
15 }
16
17 fn display(buf: &[u8]) -> String {
18 match buf.len() {
19 1 => format!("{:04b}_{:04b}", buf[0] >> 4, buf[0] & 0xf),
20 2 => format!(
21 "{:04b}_{:04b} {:04b}_{:04b}",
22 buf[0] >> 4,
23 buf[0] & 0xf,
24 buf[1] >> 4,
25 buf[1] & 0xf
26 ),
27 n => unreachable!("Shouldn't be instructions of length {}", n),
28 }
29 }
30
31 macro_rules! case {
32 ($txt:expr => !) => {
33 assert!(
34 parse::parse_inst($txt).is_none(),
35 "expect to not parse {:?}",
36 $txt,
37 );
38 };
39 ($txt:expr => $res:expr) => {
40 let mut buf = [0, 0];
41 let mut cursor = Cursor::new(&mut buf[..]);
42 let parsed = parse::parse_inst($txt).expect(&format!("to parse {:?}", $txt));
43 parsed
44 .encode(&mut cursor)
45 .expect(&format!("to encode {:?}", parsed));
46 let pos = cursor.position() as usize;
47 assert_eq!($res, &display(&buf[..pos]), "While parsing {:?}", $txt);
48 };
49 }
50
51 macro_rules! test_parse {
52 ($($txt:expr => $res:tt),* $(,)?) => {
53 $(
54 case!($txt => $res);
55 )*
56 }
57 }
58
59 #[test]
60 fn trap() {
61 test_parse! {
62 "TRAP" => "0000_0000",
63 "trap" => "0000_0000",
64 }
65 }
66
67 #[test]
68 fn jump_call() {
69 test_parse! {
70 "JABS" => "0000_0100",
71 "CABS" => "0000_0101",
72 "JOFF" => "0000_0110",
73 "COFF" => "0000_0111",
74 }
75 }
76
77 #[test]
78 fn swap() {
79 test_parse! {
80 "swap r0" => "0000_1000",
81 "SWAP r1" => "0000_1001",
82 "Swap\tr2" => "0000_1010",
83 "SWAP\t\tr3" => "0000_1011",
84 "SWAP r4" => "0000_1100",
85 "SWAP r5" => "0000_1101",
86 "SWAP r6" => "0000_1110",
87 "SWAP r7" => "0000_1111",
88 "SWAP r8" => !,
89 "swapr0" => !,
90 "swap 0" => !,
91 "swap g0" => !,
92 "swap" => !,
93 }
94 }
95
96 #[test]
97 fn getr_setr() {
98 test_parse! {
99 "getr r3" => "0001_0011",
100 "GETR r7" => "0001_0111",
101 "GETR r9" => !,
102
103 "SETR r0" => "0001_1000",
104 "SETR r7" => "0001_1111",
105 }
106 }
107
108 #[test]
109 fn get_set_special() {
110 test_parse! {
111 "GET1" => "0010_0000",
112 "GET2" => "0010_0001",
113 "GETC" => "0010_0010",
114
115 "SET1" => "0010_1000",
116 "SET2" => "0010_1001",
117 "SETC" => "0010_1010",
118 }
119 }
120
121 #[test]
122 fn alu1() {
123 test_parse! {
124 "zero" => "0011_0000",
125 "lsl1" => "0011_0001",
126 "lsr1" => "0011_0010",
127 "asr1" => "0011_0011",
128 "incr" => "0011_0100",
129 "decr" => "0011_0101",
130 "comp" => "0011_0110",
131 "negt" => "0011_0111",
132 }
133 }
134
135 #[test]
136 fn islt() {
137 test_parse! {
138 "islt r0" => "0011_1000",
139 "islt r7" => "0011_1111",
140 "islt r8" => !,
141 "islt" => !,
142 }
143 }
144
145 #[test]
146 fn alu_r() {
147 test_parse! {
148 "addr r0" => "0100_0000",
149 "addr r4" => "0100_0100",
150 "subr r1" => "0100_1001",
151 "subr r5" => "0100_1101",
152 "andr r2" => "0101_0010",
153 "andr r6" => "0101_0110",
154 "iorr r3" => "0101_1011",
155 "iorr r7" => "0101_1111",
156 "xorr r0" => "0110_0000",
157 "xorr r4" => "0110_0100",
158 "lslr r1" => "0110_1001",
159 "lslr r5" => "0110_1101",
160 "lsrr r2" => "0111_0010",
161 "lsrr r6" => "0111_0110",
162 "asrr r3" => "0111_1011",
163 "asrr r7" => "0111_1111",
164 }
165 }
166
167 #[test]
168 fn ldst() {
169 test_parse! {
170 "ld1u r0" => "1000_0000",
171 "ld1u r3!" => "1001_0011",
172 "ld2u r2!" => "1011_0010",
173 "ld2u r4" => "1010_0100",
174
175 "st1u r1" => "1000_1001",
176 "st1u r7!" => "1001_1111",
177 "st2u r7!" => "1011_1111",
178 "st2u r7" => "1010_1111",
179 }
180 }
181
182 #[test]
183 fn alu_i() {
184 test_parse! {
185 "andi #00" => "1111_0000 0000_0000",
186 "andi #0000" => "1111_0000 0000_0000",
187 "andi #10" => "1111_0000 0000_1010",
188 "andi #255" => "1111_0000 1111_1111",
189 "andi #-1" => !,
190 "andi #-23" => !,
191 "andi #256" => !,
192
193 "andi #d00" => "1111_0000 0000_0000",
194 "andi #D0000" => "1111_0000 0000_0000",
195 "andi #d10" => "1111_0000 0000_1010",
196 "andi #D255" => "1111_0000 1111_1111",
197 "andi #d-1" => !,
198 "andi #d-23" => !,
199 "andi #d256" => !,
200
201 "andi #x00" => "1111_0000 0000_0000",
202 "andi #XFF" => "1111_0000 1111_1111",
203 "andi #xAA" => "1111_0000 1010_1010",
204 "andi #o177" => "1111_0000 0111_1111",
205 "andi #O177" => "1111_0000 0111_1111",
206 "andi #b11010000" => "1111_0000 1101_0000",
207
208 "iori #00" => "1111_0001 0000_0000",
209 "iori #32" => "1111_0001 0010_0000",
210 "iori #01" => "1111_0001 0000_0001",
211 "iori #-1" => !,
212
213 "xori #00" => "1111_0010 0000_0000",
214 "xori #07" => "1111_0010 0000_0111",
215 "xori #255" => "1111_0010 1111_1111",
216 "xori #300" => !,
217
218 "addi #0" => "1111_0011 0000_0000",
219 "addi #1" => "1111_0011 0000_0001",
220 "addi #127" => "1111_0011 0111_1111",
221 "addi #128" => !,
222 "addi #250" => !,
223 "addi #-1" => "1111_0011 1111_1111",
224 "addi #-10" => "1111_0011 1111_0110",
225 "addi #-64" => "1111_0011 1100_0000",
226 "addi #-65" => !,
227 "addi #-100" => !,
228
229 "addi #x7F" => "1111_0011 0111_1111",
230 "addi #xFF" => !,
231 }
232 }
233
234 #[test]
235 fn alu_compact() {
236 test_parse! {
237 "roli #0" => "1111_0011 1000_0000",
238 "roli #7" => "1111_0011 1000_0111",
239 "roli #-1" => !,
240 "roli #8" => !,
241
242 "lsli #0" => "1111_0011 1000_1000",
243 "lsli #7" => "1111_0011 1000_1111",
244 "lsri #0" => "1111_0011 1001_0000",
245 "lsri #7" => "1111_0011 1001_0111",
246 "asri #0" => "1111_0011 1001_1000",
247 "asri #7" => "1111_0011 1001_1111",
248
249 "clri #0" => "1111_0011 1010_0000",
250 "clri #7" => "1111_0011 1010_0111",
251 "seti #0" => "1111_0011 1010_1000",
252 "seti #7" => "1111_0011 1010_1111",
253 "togi #0" => "1111_0011 1011_0000",
254 "togi #7" => "1111_0011 1011_0111",
255 "exti #0" => "1111_0011 1011_1000",
256 "exti #7" => "1111_0011 1011_1111",
257 }
258 }
259
260 #[test]
261 fn branch() {
262 test_parse! {
263 "bezi #0" => "1111_0100 0000_0000",
264 "bezi #1" => "1111_0100 0000_0001",
265 "bezi #-1" => "1111_0100 1111_1111",
266 "bezi #-128" => "1111_0100 1000_0000",
267 "bezi #127" => "1111_0100 0111_1111",
268 "bezi #-129" => !,
269 "bezi #128" => !,
270
271 "jofi #0" => "1111_0110 0000_0000",
272 "jofi #1" => "1111_0110 0000_0001",
273 "jofi #-1" => "1111_0110 1111_1111",
274
275 "cofi #0" => "1111_0111 0000_0000",
276 "cofi #1" => "1111_0111 0000_0001",
277 "cofi #-1" => "1111_0111 1111_1111",
278 }
279 }