de04e125d34ecf179e3c63563b0bb09ae32b1b49
[openpower-isa.git] / src / libopid / opid-check.c
1 #include <opid.h>
2
3 #include <errno.h>
4 #include <inttypes.h>
5 #include <stddef.h>
6 #include <stdio.h>
7 #include <stdint.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <unistd.h>
11
12 #define error(FORMAT, ...) \
13 do { \
14 (void)fprintf(stderr, FORMAT, ##__VA_ARGS__); \
15 } while (0)
16
17 #define OPID_OPERAND_SIGNED (UINT32_C(1) << UINT32_C(0))
18 #define OPID_OPERAND_GPR (UINT32_C(1) << UINT32_C(1))
19 #define OPID_OPERAND_FPR (UINT32_C(1) << UINT32_C(2))
20 #define OPID_OPERAND_PAIR (UINT32_C(1) << UINT32_C(3))
21 #define OPID_OPERAND_CR3 (UINT32_C(1) << UINT32_C(4))
22 #define OPID_OPERAND_CR5 (UINT32_C(1) << UINT32_C(5))
23 #define OPID_OPERAND_NONZERO (UINT32_C(1) << UINT32_C(6))
24 #define OPID_OPERAND_ADDRESS (UINT32_C(1) << UINT32_C(7))
25
26 int
27 main(void) {
28 ssize_t size;
29 uint32_t insn;
30 struct opid_ctx ctx;
31 enum opid_state state;
32 struct opid_operand const *operand;
33
34 do {
35 size = read(STDIN_FILENO, &insn, sizeof(insn));
36 if (size < 0) {
37 error("cannot fetch instruction: %s\n", strerror(errno));
38 return EXIT_FAILURE;
39 } else if (size == sizeof(uint32_t)) {
40 state = opid_disassemble(&ctx, insn);
41 if (state != OPID_SUCCESS) {
42 error("invalid instruction: %08" PRIx32 "\n", insn);
43 return EXIT_FAILURE;
44 }
45
46 printf("%s [", ctx.record->name);
47 opid_foreach_operand(&ctx, operand) {
48 printf("(%" PRIi32 " ",
49 (int32_t)operand->value);
50 if (operand->flags & OPID_OPERAND_SIGNED)
51 printf("s");
52 if (operand->flags & OPID_OPERAND_GPR)
53 printf("g");
54 if (operand->flags & OPID_OPERAND_FPR)
55 printf("f");
56 if (operand->flags & OPID_OPERAND_PAIR)
57 printf("p");
58 if (operand->flags & OPID_OPERAND_CR3)
59 printf("3");
60 if (operand->flags & OPID_OPERAND_CR5)
61 printf("5");
62 if (operand->flags & OPID_OPERAND_NONZERO)
63 printf("z");
64 if (operand->flags & OPID_OPERAND_ADDRESS)
65 printf("a");
66 printf(")");
67 }
68 printf("]\n");
69 } else if (size != 0) {
70 error("truncated instruction: %zu\n", (size_t)size);
71 return EXIT_FAILURE;
72 }
73 } while (size != 0);
74
75 return EXIT_SUCCESS;
76 }