rabbitizer 1.9.0

MIPS instruction decoder
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/* SPDX-FileCopyrightText: © 2022-2024 Decompollaborate */
/* SPDX-License-Identifier: MIT */

#include "instructions/RabbitizerInstruction.h"

#include <assert.h>

#include "common/RabbitizerConfig.h"
#include "instructions/RabbitizerRegister.h"
#include "instructions/RabbitizerInstructionRsp.h"
#include "instructions/RabbitizerInstructionR3000GTE.h"
#include "instructions/RabbitizerInstructionR5900.h"

void RabbitizerInstruction_init(RabbitizerInstruction *self, uint32_t word, uint32_t vram) {
    self->word = word;
    self->_mandatorybits = 0;

    self->uniqueId = RABBITIZER_INSTR_ID_cpu_INVALID;
    self->descriptor = &RabbitizerInstrDescriptor_Descriptors[self->uniqueId];
    self->instrIdType = RAB_INSTR_ID_TYPE_ALL_INVALID;

    self->vram = vram;
    self->_handwrittenCategory = false;
    self->inHandwrittenFunction = false;
    self->category = RABBITIZER_INSTRCAT_CPU;

    self->flags = 0;
    RAB_INSTR_FLAGS_SET_r5900DisasmAsData(self, RAB_TRINARY_VAL_NONE);
}

void RabbitizerInstruction_destroy(UNUSED RabbitizerInstruction *self) {
}

/* General getters */

/**
 * Get the word value encoding the instruction.
 *
 * The returned value may not be the same as the one to instance this Instruction
 * if a method that modifies the word has been used, like `RabbitizerInstruction_blankOut`.
 */
uint32_t RabbitizerInstruction_getRaw(const RabbitizerInstruction *self) {
    return self->word;
}

/**
 * Get the (possibly signed) immediate value for this instruction.

 * This only makes sense for an instruction with an immediate,
 * which can be checked with
 * `RabbitizerInstruction_hasOperandAlias(&instr, RAB_OPERAND_cpu_immediate)`
 */
int32_t RabbitizerInstruction_getProcessedImmediate(const RabbitizerInstruction *self) {
    if (RabbitizerInstrDescriptor_isUnsigned(self->descriptor)) {
        return RAB_INSTR_GET_immediate(self);
    }
    return RabbitizerUtils_From2Complement(RAB_INSTR_GET_immediate(self), 16);
}

/**
 * Get the target vram address this instruction jumps to.
 * This method is intended only for direct jump instructions.

 * This only makes sense if the instruction is a direct jump,
 * which can be checked with `RabbitizerInstrDescriptor_isJumpWithAddress`.
 */
uint32_t RabbitizerInstruction_getInstrIndexAsVram(const RabbitizerInstruction *self) {
    uint32_t vram = RAB_INSTR_GET_instr_index(self) << 2;

    if (self->vram == 0) {
        vram |= 0x80000000;
    } else {
        // Jumps are PC-region branches. The upper bits are filled with the address in the delay slot
        vram |= (self->vram + 4) & 0xF0000000;
    }
    return vram;
}

/**
 * Returns the offset (in bytes) that the branch instruction would branch,
 * relative to the instruction itself. This method is intended only for branch
 * instructions.
 *
 * The returned value can be negative, meaning the branch instructions does
 * a backwards branch.
 *
 * This only makes sense for an instruction is a branch,
 * which can be checked with `RabbitizerInstrDescriptor_isBranch`.
 *
 * To get the branch offset of either a branch instruction or a jump instruction
 * use `RabbitizerInstruction_getBranchOffsetGeneric` instead.
 */
int32_t RabbitizerInstruction_getBranchOffset(const RabbitizerInstruction *self) {
    int32_t diff = RabbitizerUtils_From2Complement(RAB_INSTR_GET_immediate(self), 16);

    return diff * 4 + 4;
}

//! @deprecated
int32_t RabbitizerInstruction_getGenericBranchOffset(const RabbitizerInstruction *self, uint32_t currentVram) {
    if (self->uniqueId == RABBITIZER_INSTR_ID_cpu_j) {
        return RabbitizerInstruction_getInstrIndexAsVram(self) - currentVram;
    }
    return RabbitizerInstruction_getBranchOffset(self);
}

/**
 * Returns the offset (in bytes) that the instruction would branch,
 * relative to the instruction itself. This method is intended for both branch
 * and jump instructions.
 *
 * The returned value can be either positive or negative.
 *
 * This only makes sense for an instruction is a branch or a direct jump,
 * which can be checked with `RabbitizerInstruction_getBranchOffsetGeneric`
 * or `RabbitizerInstrDescriptor_isJumpWithAddress`.
 */
int32_t RabbitizerInstruction_getBranchOffsetGeneric(const RabbitizerInstruction *self) {
    if (RabbitizerInstruction_hasOperandAlias(self, RAB_OPERAND_cpu_label)) {
        return RabbitizerInstruction_getInstrIndexAsVram(self) - self->vram;
    }
    return RabbitizerInstruction_getBranchOffset(self);
}

/**
 * Get the target vram address this instruction jumps to.
 * This method is intended only for branch or direct jump instructions.
 *
 * This only makes sense for an instruction is a branch or a direct jump,
 * which can be checked with `RabbitizerInstruction_getBranchOffsetGeneric`
 * or `RabbitizerInstrDescriptor_isJumpWithAddress`.
 */
uint32_t RabbitizerInstruction_getBranchVramGeneric(const RabbitizerInstruction *self) {
    if (RabbitizerInstruction_hasOperandAlias(self, RAB_OPERAND_cpu_label)) {
        return RabbitizerInstruction_getInstrIndexAsVram(self);
    }
    return RabbitizerInstruction_getBranchOffset(self) + self->vram;
}

/**
 * Returns the general purpose register (GPR) which this instruction modifies,
 * or a negative value if the instruction does not modify the state of any GPR
 */
int8_t RabbitizerInstruction_getDestinationGpr(const RabbitizerInstruction *self) {
    if (RabbitizerInstrDescriptor_modifiesRd(self->descriptor)) {
        return RAB_INSTR_GET_rd(self);
    }
    if (RabbitizerInstrDescriptor_modifiesRt(self->descriptor)) {
        return RAB_INSTR_GET_rt(self);
    }
    return -1;
}

/**
 * Returns `true` if the GPR which is modified by this register is $zero, `false` otherwise.
 * Returns `false` if this instruction does not modify a GPR.
 */
bool RabbitizerInstruction_outputsToGprZero(const RabbitizerInstruction *self) {
    return RabbitizerInstruction_getDestinationGpr(self) == 0;
}

/* General getters */

void RabbitizerInstruction_blankOut(RabbitizerInstruction *self) {
    size_t i;

    for (i = 0; i < ARRAY_COUNT(self->descriptor->operands) && self->descriptor->operands[i] != RAB_OPERAND_ALL_INVALID;
         i++) {
        switch (self->descriptor->operands[i]) {
            case RAB_OPERAND_cpu_rs:
                self->word = RAB_INSTR_PACK_rs(self->word, 0);
                break;

            case RAB_OPERAND_cpu_rt:
                self->word = RAB_INSTR_PACK_rt(self->word, 0);
                break;

            case RAB_OPERAND_cpu_rd:
                self->word = RAB_INSTR_PACK_rd(self->word, 0);
                break;

            case RAB_OPERAND_cpu_sa:
                self->word = RAB_INSTR_PACK_sa(self->word, 0);
                break;

            case RAB_OPERAND_cpu_zero:
                break;

            case RAB_OPERAND_cpu_cop0d:
                self->word = RAB_INSTR_PACK_cop0d(self->word, 0);
                break;

            case RAB_OPERAND_cpu_fs:
                self->word = RAB_INSTR_PACK_fs(self->word, 0);
                break;

            case RAB_OPERAND_cpu_ft:
                self->word = RAB_INSTR_PACK_ft(self->word, 0);
                break;

            case RAB_OPERAND_cpu_fd:
                self->word = RAB_INSTR_PACK_fd(self->word, 0);
                break;

            case RAB_OPERAND_cpu_cop1cs:
                self->word = RAB_INSTR_PACK_cop1cs(self->word, 0);
                break;

            case RAB_OPERAND_cpu_cop2t:
                self->word = RAB_INSTR_PACK_cop2t(self->word, 0);
                break;

            case RAB_OPERAND_cpu_cop2cd:
                self->word = RAB_INSTR_PACK_cop2cd(self->word, 0);
                break;

            case RAB_OPERAND_cpu_op:
                self->word = RAB_INSTR_PACK_op(self->word, 0);
                break;

            case RAB_OPERAND_cpu_code:
                self->word = RAB_INSTR_PACK_code(self->word, 0);
                break;

            case RAB_OPERAND_cpu_code_lower:
                self->word = RAB_INSTR_PACK_code_lower(self->word, 0);
                break;

            case RAB_OPERAND_cpu_copraw:
                self->word = RAB_INSTR_PACK_copraw(self->word, 0);
                break;

            case RAB_OPERAND_cpu_label:
                self->word = RAB_INSTR_PACK_instr_index(self->word, 0);
                break;

            case RAB_OPERAND_cpu_immediate:
                self->word = RAB_INSTR_PACK_immediate(self->word, 0);
                break;

            case RAB_OPERAND_cpu_branch_target_label:
                self->word = RAB_INSTR_PACK_immediate(self->word, 0);
                break;

            case RAB_OPERAND_cpu_immediate_base:
                self->word = RAB_INSTR_PACK_rs(self->word, 0);
                self->word = RAB_INSTR_PACK_immediate(self->word, 0);
                break;

            case RAB_OPERAND_cpu_maybe_rd_rs:
                self->word = RAB_INSTR_PACK_rd(self->word, 0);
                self->word = RAB_INSTR_PACK_rs(self->word, 0);
                break;

            /* rsp */
            case RAB_OPERAND_rsp_rs:
                self->word = RAB_INSTR_PACK_rs(self->word, 0);
                break;

            case RAB_OPERAND_rsp_rt:
                self->word = RAB_INSTR_PACK_rt(self->word, 0);
                break;

            case RAB_OPERAND_rsp_rd:
                self->word = RAB_INSTR_PACK_rd(self->word, 0);
                break;

            case RAB_OPERAND_rsp_cop0d:
                self->word = RAB_INSTR_PACK_cop0d(self->word, 0);
                break;

            case RAB_OPERAND_rsp_cop2t:
                self->word = RAB_INSTR_RSP_PACK_cop2t(self->word, 0);
                break;

            case RAB_OPERAND_rsp_cop2cd:
                self->word = RAB_INSTR_RSP_PACK_cop2cd(self->word, 0);
                break;

            case RAB_OPERAND_rsp_vs:
                self->word = RAB_INSTR_RSP_PACK_vs(self->word, 0);
                break;

            case RAB_OPERAND_rsp_vt:
                self->word = RAB_INSTR_RSP_PACK_vt(self->word, 0);
                break;

            case RAB_OPERAND_rsp_vd:
                self->word = RAB_INSTR_RSP_PACK_vd(self->word, 0);
                break;

            case RAB_OPERAND_rsp_vt_elementhigh:
                self->word = RAB_INSTR_RSP_PACK_vt(self->word, 0);
                self->word = RAB_INSTR_RSP_PACK_elementhigh(self->word, 0);
                break;

            case RAB_OPERAND_rsp_vt_elementlow:
                self->word = RAB_INSTR_RSP_PACK_vt(self->word, 0);
                self->word = RAB_INSTR_RSP_PACK_elementlow(self->word, 0);
                break;

            case RAB_OPERAND_rsp_vd_de:
                self->word = RAB_INSTR_RSP_PACK_vd(self->word, 0);
                self->word = RAB_INSTR_RSP_PACK_de(self->word, 0);
                break;

            case RAB_OPERAND_rsp_vs_index:
                self->word = RAB_INSTR_RSP_PACK_vs(self->word, 0);
                self->word = RAB_INSTR_RSP_PACK_index(self->word, 0);
                break;

            case RAB_OPERAND_rsp_offset_rs:
                self->word = RAB_INSTR_RSP_PACK_offset(self->word, 0);
                self->word = RAB_INSTR_PACK_rs(self->word, 0);
                break;

            case RAB_OPERAND_rsp_immediate_base:
                self->word = RAB_INSTR_PACK_rs(self->word, 0);
                self->word = RAB_INSTR_PACK_immediate(self->word, 0);
                break;

            case RAB_OPERAND_rsp_maybe_rd_rs:
                self->word = RAB_INSTR_PACK_rd(self->word, 0);
                self->word = RAB_INSTR_PACK_rs(self->word, 0);
                break;
            /* rsp */

            /* r3000gte */
            case RAB_OPERAND_r3000gte_sf:
                self->word = RAB_INSTR_R3000GTE_PACK_sf(self->word, 0);
                break;

            case RAB_OPERAND_r3000gte_mx:
                self->word = RAB_INSTR_R3000GTE_PACK_mx(self->word, 0);
                break;

            case RAB_OPERAND_r3000gte_v:
                self->word = RAB_INSTR_R3000GTE_PACK_v(self->word, 0);
                break;

            case RAB_OPERAND_r3000gte_cv:
                self->word = RAB_INSTR_R3000GTE_PACK_cv(self->word, 0);
                break;

            case RAB_OPERAND_r3000gte_lm:
                self->word = RAB_INSTR_R3000GTE_PACK_lm(self->word, 0);
                break;
            /* r3000gte */

            /* r5900 */
            case RAB_OPERAND_r5900_I:
            case RAB_OPERAND_r5900_Q:
            case RAB_OPERAND_r5900_R:
            case RAB_OPERAND_r5900_ACC:
                // Not real registers encoded on the instruction itself
                break;

            case RAB_OPERAND_r5900_ACCxyzw:
                self->word = RAB_INSTR_R5900_PACK_xyzw_x(self->word, 0);
                self->word = RAB_INSTR_R5900_PACK_xyzw_y(self->word, 0);
                self->word = RAB_INSTR_R5900_PACK_xyzw_z(self->word, 0);
                self->word = RAB_INSTR_R5900_PACK_xyzw_w(self->word, 0);
                break;

            case RAB_OPERAND_r5900_vfs:
                self->word = RAB_INSTR_R5900_PACK_vfs(self->word, 0);
                break;

            case RAB_OPERAND_r5900_vft:
                self->word = RAB_INSTR_R5900_PACK_vft(self->word, 0);
                break;

            case RAB_OPERAND_r5900_vfd:
                self->word = RAB_INSTR_R5900_PACK_vfd(self->word, 0);
                break;

            case RAB_OPERAND_r5900_vfsxyzw:
                self->word = RAB_INSTR_R5900_PACK_vfs(self->word, 0);
                self->word = RAB_INSTR_R5900_PACK_xyzw_x(self->word, 0);
                self->word = RAB_INSTR_R5900_PACK_xyzw_y(self->word, 0);
                self->word = RAB_INSTR_R5900_PACK_xyzw_z(self->word, 0);
                self->word = RAB_INSTR_R5900_PACK_xyzw_w(self->word, 0);
                break;

            case RAB_OPERAND_r5900_vftxyzw:
                self->word = RAB_INSTR_R5900_PACK_vft(self->word, 0);
                self->word = RAB_INSTR_R5900_PACK_xyzw_x(self->word, 0);
                self->word = RAB_INSTR_R5900_PACK_xyzw_y(self->word, 0);
                self->word = RAB_INSTR_R5900_PACK_xyzw_z(self->word, 0);
                self->word = RAB_INSTR_R5900_PACK_xyzw_w(self->word, 0);
                break;

            case RAB_OPERAND_r5900_vfdxyzw:
                self->word = RAB_INSTR_R5900_PACK_vfd(self->word, 0);
                self->word = RAB_INSTR_R5900_PACK_xyzw_x(self->word, 0);
                self->word = RAB_INSTR_R5900_PACK_xyzw_y(self->word, 0);
                self->word = RAB_INSTR_R5900_PACK_xyzw_z(self->word, 0);
                self->word = RAB_INSTR_R5900_PACK_xyzw_w(self->word, 0);
                break;

            case RAB_OPERAND_r5900_vfsn:
                self->word = RAB_INSTR_R5900_PACK_vfs(self->word, 0);
                self->word = RAB_INSTR_R5900_PACK_n(self->word, 0);
                break;

            case RAB_OPERAND_r5900_vftn:
                self->word = RAB_INSTR_R5900_PACK_vft(self->word, 0);
                self->word = RAB_INSTR_R5900_PACK_n(self->word, 0);
                break;

            case RAB_OPERAND_r5900_vfdn:
                self->word = RAB_INSTR_R5900_PACK_vfd(self->word, 0);
                self->word = RAB_INSTR_R5900_PACK_n(self->word, 0);
                break;

            case RAB_OPERAND_r5900_vfsl:
                self->word = RAB_INSTR_R5900_PACK_vfs(self->word, 0);
                self->word = RAB_INSTR_R5900_PACK_l(self->word, 0);
                break;

            case RAB_OPERAND_r5900_vftl:
                self->word = RAB_INSTR_R5900_PACK_vft(self->word, 0);
                self->word = RAB_INSTR_R5900_PACK_l(self->word, 0);
                break;

            case RAB_OPERAND_r5900_vfdl:
                self->word = RAB_INSTR_R5900_PACK_vfd(self->word, 0);
                self->word = RAB_INSTR_R5900_PACK_l(self->word, 0);
                break;

            case RAB_OPERAND_r5900_vfsm:
                self->word = RAB_INSTR_R5900_PACK_vfs(self->word, 0);
                self->word = RAB_INSTR_R5900_PACK_m(self->word, 0);
                break;

            case RAB_OPERAND_r5900_vftm:
                self->word = RAB_INSTR_R5900_PACK_vft(self->word, 0);
                self->word = RAB_INSTR_R5900_PACK_m(self->word, 0);
                break;

            case RAB_OPERAND_r5900_vfdm:
                self->word = RAB_INSTR_R5900_PACK_vfd(self->word, 0);
                self->word = RAB_INSTR_R5900_PACK_m(self->word, 0);
                break;

            case RAB_OPERAND_r5900_vis:
                self->word = RAB_INSTR_R5900_PACK_vis(self->word, 0);
                break;

            case RAB_OPERAND_r5900_vit:
                self->word = RAB_INSTR_R5900_PACK_vit(self->word, 0);
                break;

            case RAB_OPERAND_r5900_vid:
                self->word = RAB_INSTR_R5900_PACK_vid(self->word, 0);
                break;

            case RAB_OPERAND_r5900_vis_predecr:
                self->word = RAB_INSTR_R5900_PACK_vis(self->word, 0);
                break;

            case RAB_OPERAND_r5900_vit_predecr:
                self->word = RAB_INSTR_R5900_PACK_vit(self->word, 0);
                break;

            case RAB_OPERAND_r5900_vid_predecr:
                self->word = RAB_INSTR_R5900_PACK_vid(self->word, 0);
                break;

            case RAB_OPERAND_r5900_vis_postincr:
                self->word = RAB_INSTR_R5900_PACK_vis(self->word, 0);
                break;

            case RAB_OPERAND_r5900_vit_postincr:
                self->word = RAB_INSTR_R5900_PACK_vit(self->word, 0);
                break;

            case RAB_OPERAND_r5900_vid_postincr:
                self->word = RAB_INSTR_R5900_PACK_vid(self->word, 0);
                break;

            case RAB_OPERAND_r5900_immediate5:
                self->word = RAB_INSTR_R5900_PACK_imm5(self->word, 0);
                break;

            case RAB_OPERAND_r5900_immediate15:
                self->word = RAB_INSTR_R5900_PACK_imm15(self->word, 0);
                break;
                /* r5900 */

            case RAB_OPERAND_ALL_INVALID:
            case RAB_OPERAND_ALL_MAX:
                assert(self->descriptor->operands[i] != RAB_OPERAND_ALL_INVALID &&
                       self->descriptor->operands[i] != RAB_OPERAND_ALL_MAX);
                break;
        }
    }
}