1 /* s12z-dis.h -- Header file for s12z-dis.c and s12z-decode.c 2 Copyright (C) 2019 Free Software Foundation, Inc. 3 4 This file is part of the GNU opcodes library. 5 6 This library is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 3, or (at your option) 9 any later version. 10 11 It is distributed in the hope that it will be useful, but WITHOUT 12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 14 License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with this program; see the file COPYING3. If not, 18 see <http://www.gnu.org/licenses/>. */ 19 20 #ifndef S12Z_OPC_H 21 #define S12Z_OPC_H 22 23 #include <stdbool.h> 24 25 /* An abstraction used to read machine code from a source. */ 26 struct mem_read_abstraction_base 27 { 28 int (*read) (struct mem_read_abstraction_base *, int, size_t, bfd_byte *); 29 void (*advance) (struct mem_read_abstraction_base *); 30 bfd_vma (*posn) (struct mem_read_abstraction_base *); 31 }; 32 33 34 /* Machine code operators. 35 These *roughly* correspond to opcodes. 36 But describe their purpose rather than their form. */ 37 enum operator 38 { 39 OP_INVALID = 0, 40 41 OP_push, 42 OP_pull, 43 /* Test and branch. */ 44 OP_tbNE, OP_tbEQ, OP_tbPL, OP_tbMI, OP_tbGT, OP_tbLE, 45 /* Decrement and branch. */ 46 OP_dbNE, OP_dbEQ, OP_dbPL, OP_dbMI, OP_dbGT, OP_dbLE, 47 48 /* Note: sex and exg are the same opcode. 49 They are mnemonic changes according to the operands. */ 50 OP_sex, 51 OP_exg, 52 53 /* Shifters. */ 54 OP_lsl, OP_lsr, 55 OP_asl, OP_asr, 56 OP_rol, OP_ror, 57 /* Bit field operations. */ 58 OP_bfins, OP_bfext, 59 OP_trap, 60 61 OP_ld, 62 OP_st, 63 OP_cmp, 64 65 OP_stop, 66 OP_wai, 67 OP_sys, 68 69 OP_minu, 70 OP_mins, 71 OP_maxu, 72 OP_maxs, 73 74 OP_abs, 75 OP_adc, 76 OP_bit, 77 OP_sbc, 78 OP_rti, 79 OP_clb, 80 OP_eor, 81 82 OP_sat, 83 84 OP_nop, 85 OP_bgnd, 86 OP_brclr, 87 OP_brset, 88 OP_rts, 89 OP_lea, 90 OP_mov, 91 92 OP_bra, 93 OP_bsr, 94 OP_bhi, 95 OP_bls, 96 OP_bcc, 97 OP_bcs, 98 OP_bne, 99 OP_beq, 100 OP_bvc, 101 OP_bvs, 102 OP_bpl, 103 OP_bmi, 104 OP_bge, 105 OP_blt, 106 OP_bgt, 107 OP_ble, 108 OP_inc, 109 OP_clr, 110 OP_dec, 111 112 OP_add, 113 OP_sub, 114 OP_and, 115 OP_or, 116 117 OP_tfr, 118 OP_jmp, 119 OP_jsr, 120 OP_com, 121 OP_andcc, 122 OP_neg, 123 OP_orcc, 124 OP_bclr, 125 OP_bset, 126 OP_btgl, 127 OP_swi, 128 129 OP_mulu, 130 OP_divu, 131 OP_modu, 132 OP_macu, 133 OP_qmulu, 134 135 OP_muls, 136 OP_divs, 137 OP_mods, 138 OP_macs, 139 OP_qmuls, 140 141 OPBASE_mul = 0x4000, 142 OPBASE_div, 143 OPBASE_mod, 144 OPBASE_mac, 145 OPBASE_qmul, 146 147 n_OPS 148 }; 149 150 151 /* Used for operands which mutate their index/base registers. 152 Eg ld d0, (s+). */ 153 enum op_reg_mutation 154 { 155 OPND_RM_NONE, 156 OPND_RM_PRE_DEC, 157 OPND_RM_PRE_INC, 158 OPND_RM_POST_DEC, 159 OPND_RM_POST_INC 160 }; 161 162 /* The class of an operand. */ 163 enum opnd_class 164 { 165 OPND_CL_IMMEDIATE, 166 OPND_CL_MEMORY, 167 OPND_CL_REGISTER, 168 OPND_CL_REGISTER_ALL, /* Used only for psh/pul. */ 169 OPND_CL_REGISTER_ALL16, /* Used only for psh/pul. */ 170 OPND_CL_SIMPLE_MEMORY, 171 OPND_CL_BIT_FIELD 172 }; 173 174 175 /* Base structure of all operands. */ 176 struct operand 177 { 178 enum opnd_class cl; 179 180 /* OSIZE determines the size of memory access for 181 the operation in which the operand participates. 182 It may be -1 which indicates either unknown 183 (must be determined by other operands) or if 184 it is not applicable for this operation. */ 185 int osize; 186 }; 187 188 /* Immediate operands. Eg: #23 */ 189 struct immediate_operand 190 { 191 struct operand parent; 192 int value; 193 }; 194 195 /* Bitfield operands. Used only in bfext and bfins 196 instructions. */ 197 struct bitfield_operand 198 { 199 struct operand parent; 200 int width; 201 int offset; 202 }; 203 204 /* Register operands. */ 205 struct register_operand 206 { 207 struct operand parent; 208 int reg; 209 }; 210 211 212 /* Simple memory operands. ie, direct memory, 213 no index, no pre/post inc/dec. May be either relative or absolute. 214 Eg st d0, 0x123456 */ 215 struct simple_memory_operand 216 { 217 struct operand parent; 218 219 bfd_vma addr; 220 bfd_vma base; 221 bool relative; 222 }; 223 224 225 /* Memory operands. Should be able to represent all memory 226 operands in the S12Z instruction set which are not simple 227 memory operands. */ 228 struct memory_operand 229 { 230 struct operand parent; 231 232 /* True for indirect operands: eg [0x123456] */ 233 bool indirect; 234 235 /* The value of any offset. eg 45 in (45,d7) */ 236 int base_offset; 237 238 /* Does this operand increment or decrement 239 its participating registers. Eg (-s) */ 240 enum op_reg_mutation mutation; 241 242 /* The number of registers participating in this operand. 243 For S12Z this is always in the range [0, 6] (but for most 244 instructions it's <= 2). */ 245 int n_regs; 246 247 /* The participating registers. */ 248 int regs[6]; 249 }; 250 251 252 /* Decode a single instruction. 253 OPERATOR, OSIZE, N_OPERANDS and OPERANDS are pointers to 254 variables which must be provided by the caller. 255 N_OPERANDS will be incremented by the number of operands read, so 256 you should assign it to something before calling this function. 257 OPERANDS must be large enough to contain all operands read 258 (which may be up to 6). 259 It is the responsibility of the caller to free all operands 260 when they are no longer needed. 261 Returns the number of bytes read. */ 262 int decode_s12z (enum operator *myoperator, short *osize, 263 int *n_operands, struct operand **operands, 264 struct mem_read_abstraction_base *); 265 266 267 #endif /* S12Z_OPC_H */ 268