Debian Codegraph @

src/asahi/isa/test/test-disassembler.c

raw
1 /*
2 * Copyright (C) 2021 Collabora, Ltd.
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include <inttypes.h>
7 #include <stdbool.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include "asahi/isa/disasm.h"
12 #include "util/macros.h"
13
14 static inline uint8_t
15 parse_nibble(const char c)
16 {
17 return (c >= 'a') ? 10 + (c - 'a') : (c - '0');
18 }
19
20 /* Given a little endian 8 byte hexdump, parse out the 64-bit value */
21 static uint64_t
22 parse_hex(const char *in)
23 {
24 uint64_t v = 0;
25
26 for (unsigned i = 0; i < 8; ++i) {
27 uint8_t byte = (parse_nibble(in[0]) << 4) | parse_nibble(in[1]);
28 v |= ((uint64_t)byte) << (8 * i);
29
30 /* Skip the space after the byte */
31 in += 3;
32 }
33
34 return v;
35 }
36
37 int
38 main(int argc, const char **argv)
39 {
40 if (argc < 2) {
41 fprintf(stderr, "Expected case list\n");
42 return 1;
43 }
44
45 FILE *fp = fopen(argv[1], "r");
46
47 if (fp == NULL) {
48 fprintf(stderr, "Could not open the case list");
49 return 1;
50 }
51
52 char line[128];
53 unsigned nr_fail = 0, nr_pass = 0;
54
55 while (fgets(line, sizeof(line), fp) != NULL) {
56 char *output = NULL;
57 size_t sz = 0;
58 size_t len = strlen(line);
59
60 /* Skip empty lines */
61 if (len <= 1)
62 continue;
63
64 /* Parse the hex */
65 uint8_t code[16];
66 const char *cursor = line;
67 const char *end = line + len;
68 uint32_t i = 0;
69 while ((cursor + 2) <= end && cursor[0] != ' ') {
70 assert(i < ARRAY_SIZE(code));
71 code[i] = (parse_nibble(cursor[0]) << 4) | parse_nibble(cursor[1]);
72 cursor += 2;
73 ++i;
74 }
75
76 /* Skip spacing */
77 while (cursor < end && (*cursor) == ' ')
78 ++cursor;
79
80 FILE *outputp = open_memstream(&output, &sz);
81 signed ret = agx2_disassemble_instr((void *)code, outputp, 0, false);
82 unsigned instr_len = abs(ret);
83 fclose(outputp);
84
85 /* Rest of the line is the reference assembly */
86 bool fail = strcmp(cursor, output);
87 fail |= (instr_len != i);
88 fail |= (ret < 0);
89
90 if (fail) {
91 /* Extra spaces after Got to align with Expected */
92 printf("Got %sExpected %s\n", output, cursor);
93
94 if (instr_len != i) {
95 printf("Got length %d, expected length %u\n", instr_len, i);
96 }
97
98 if (ret < 0) {
99 printf("Got an error.\n");
100 }
101
102 nr_fail++;
103 } else {
104 nr_pass++;
105 }
106
107 free(output);
108 }
109
110 printf("Passed %u/%u tests.\n", nr_pass, nr_pass + nr_fail);
111 fclose(fp);
112
113 return nr_fail ? 1 : 0;
114 }