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