/src/capstonenext/arch/Alpha/AlphaInstPrinter.c
Line | Count | Source |
1 | | /* Capstone Disassembly Engine */ |
2 | | /* By Dmitry Sibirtsev <sibirtsevdl@gmail.com>, 2023 */ |
3 | | |
4 | | #ifdef CAPSTONE_HAS_ALPHA |
5 | | |
6 | | #include <platform.h> |
7 | | #include <stdio.h> |
8 | | #include <stdlib.h> |
9 | | #include <string.h> |
10 | | |
11 | | #include "../../utils.h" |
12 | | #include "../../Mapping.h" |
13 | | #include "../../MCInstPrinter.h" |
14 | | |
15 | | #include "AlphaLinkage.h" |
16 | | #include "AlphaMapping.h" |
17 | | |
18 | | static const char *getRegisterName(unsigned RegNo); |
19 | | |
20 | | static void printInstruction(MCInst *, uint64_t, SStream *); |
21 | | static void printOperand(MCInst *MI, int OpNum, SStream *O); |
22 | | static void printOperandAddr(MCInst *MI, uint64_t Address, unsigned OpNum, |
23 | | SStream *O); |
24 | | |
25 | | #define GET_INSTRINFO_ENUM |
26 | | |
27 | | #include "AlphaGenInstrInfo.inc" |
28 | | |
29 | | #define GET_REGINFO_ENUM |
30 | | |
31 | | #include "AlphaGenRegisterInfo.inc" |
32 | | |
33 | | static void printOperand(MCInst *MI, int OpNum, SStream *O) |
34 | 0 | { |
35 | 0 | if (OpNum >= MI->size) |
36 | 0 | return; |
37 | | |
38 | 0 | Alpha_add_cs_detail(MI, OpNum); |
39 | |
|
40 | 0 | MCOperand *Op; |
41 | 0 | Op = MCInst_getOperand(MI, OpNum); |
42 | 0 | if (MCOperand_isReg(Op)) { |
43 | 0 | unsigned reg = MCOperand_getReg(Op); |
44 | 0 | SStream_concat(O, "%s", getRegisterName(reg)); |
45 | 0 | } else if (MCOperand_isImm(Op)) { |
46 | 0 | int64_t Imm = MCOperand_getImm(Op); |
47 | 0 | if (Imm >= 0) { |
48 | 0 | if (Imm > HEX_THRESHOLD) |
49 | 0 | SStream_concat(O, "0x%" PRIx64, Imm); |
50 | 0 | else |
51 | 0 | SStream_concat(O, "%" PRIu64, Imm); |
52 | 0 | } else { |
53 | 0 | if (Imm < -HEX_THRESHOLD) |
54 | 0 | SStream_concat(O, "-0x%" PRIx64, -Imm); |
55 | 0 | else |
56 | 0 | SStream_concat(O, "-%" PRIu64, -Imm); |
57 | 0 | } |
58 | 0 | } |
59 | 0 | } |
60 | | |
61 | | static void printOperandAddr(MCInst *MI, uint64_t Address, unsigned OpNum, |
62 | | SStream *O) |
63 | 0 | { |
64 | 0 | MCOperand *Op = MCInst_getOperand(MI, (OpNum)); |
65 | |
|
66 | 0 | uint64_t Imm = MCOperand_getImm(Op); |
67 | 0 | uint64_t Target = Address + 4 + (int16_t)(Imm << 2); |
68 | |
|
69 | 0 | Alpha_set_detail_op_imm(MI, OpNum, ALPHA_OP_IMM, Target); |
70 | 0 | printUInt64(O, Target); |
71 | 0 | } |
72 | | |
73 | | #define PRINT_ALIAS_INSTR |
74 | | |
75 | | #include "AlphaGenAsmWriter.inc" |
76 | | |
77 | | const char *ALPHA_LLVM_getRegisterName(csh handle, unsigned int id) |
78 | 0 | { |
79 | 0 | #ifndef CAPSTONE_DIET |
80 | 0 | return getRegisterName(id); |
81 | | #else |
82 | | return NULL; |
83 | | #endif |
84 | 0 | } |
85 | | |
86 | | void ALPHA_LLVM_printInstruction(MCInst *MI, SStream *O, void *Info) |
87 | 0 | { |
88 | 0 | unsigned Opcode = MCInst_getOpcode(MI); |
89 | | /* |
90 | | * The generated AsmWriter hardcodes "$31" for BR and restricts BSR to |
91 | | * Ra=26. Format 27 now decodes Ra as operand[0] and disp21 as |
92 | | * operand[1], so we must print them explicitly here. |
93 | | */ |
94 | 0 | if (Opcode == ALPHA_COND_BRANCH_I) { |
95 | 0 | SStream_concat0(O, "call_pal "); |
96 | 0 | printOperand(MI, 0, O); |
97 | 0 | return; |
98 | 0 | } |
99 | | /* |
100 | | * JMP/JSR/JSRs now decode with format 18 (Ra + Rb + hint14). |
101 | | * The generated AsmWriter hardcodes operands for specific canonical |
102 | | * forms, so print all three operands explicitly here. |
103 | | */ |
104 | 0 | if (Opcode == ALPHA_JMP || Opcode == ALPHA_JSR || |
105 | 0 | Opcode == ALPHA_JSRs || Opcode == ALPHA_RETDAG) { |
106 | 0 | const char *name = (Opcode == ALPHA_JMP) ? "jmp " : |
107 | 0 | (Opcode == ALPHA_RETDAG) ? "ret " : |
108 | 0 | "jsr "; |
109 | 0 | SStream_concat0(O, name); |
110 | 0 | printOperand(MI, 0, O); |
111 | 0 | SStream_concat1(O, ','); |
112 | 0 | SStream_concat1(O, '('); |
113 | 0 | printOperand(MI, 1, O); |
114 | 0 | SStream_concat1(O, ')'); |
115 | 0 | SStream_concat1(O, ','); |
116 | 0 | printOperand(MI, 2, O); |
117 | 0 | return; |
118 | 0 | } |
119 | 0 | if (Opcode == ALPHA_BR || Opcode == ALPHA_BSR) { |
120 | 0 | SStream_concat0(O, Opcode == ALPHA_BR ? "br " : "bsr "); |
121 | 0 | printOperand(MI, 0, O); |
122 | 0 | SStream_concat1(O, ','); |
123 | 0 | printOperandAddr(MI, MI->address, 1, O); |
124 | 0 | return; |
125 | 0 | } |
126 | 0 | printAliasInstr(MI, MI->address, O); |
127 | 0 | printInstruction(MI, MI->address, O); |
128 | 0 | } |
129 | | |
130 | | #endif |