aboutsummaryrefslogtreecommitdiffstats
path: root/decoder/source/i_dec/trc_idec_arminst.cpp
blob: 3652e84921f3c6a2a65badfb84983bd3dc7a4758 (plain) (blame)
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
/*
 * \file       trc_idec_arminst.cpp
 * \brief      OpenCSD : 
 * 
 * \copyright  Copyright (c) 2015, ARM Limited. All Rights Reserved.
 */

/* 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice, 
 * this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright notice, 
 * this list of conditions and the following disclaimer in the documentation 
 * and/or other materials provided with the distribution. 
 * 
 * 3. Neither the name of the copyright holder nor the names of its contributors 
 * may be used to endorse or promote products derived from this software without 
 * specific prior written permission. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */ 

/*
Basic ARM/Thumb/A64 instruction decode, suitable for e.g. basic
block identification and trace decode.
*/

#include "i_dec/trc_idec_arminst.h"

#include <stddef.h>  /* for NULL */
#include <assert.h>

int inst_ARM_is_direct_branch(uint32_t inst)
{
    int is_direct_branch = 1;
    if ((inst & 0xf0000000) == 0xf0000000) {
        /* NV space */
        if ((inst & 0xfe000000) == 0xfa000000){
            /* BLX (imm) */
        } else {
            is_direct_branch = 0;
        }
    } else if ((inst & 0x0e000000) == 0x0a000000) {
        /* B, BL */
    } else {
        is_direct_branch = 0;
    }
    return is_direct_branch;
}

int inst_ARM_wfiwfe(uint32_t inst)
{
    if ( ((inst & 0xf0000000) != 0xf0000000) &&
         ((inst & 0x0ffffffe) == 0x0320f002)
        )
        /* WFI & WFE may be traced as branches in etm4.3 ++ */
        return 1;
    return 0;
}

int inst_ARM_is_indirect_branch(uint32_t inst, struct decode_info *info)
{
    int is_indirect_branch = 1;
    if ((inst & 0xf0000000) == 0xf0000000) {
        /* NV space */
        if ((inst & 0xfe500000) == 0xf8100000) {
            /* RFE */
        } else {
            is_indirect_branch = 0;
        }
    } else if ((inst & 0x0ff000d0) == 0x01200010) {
        /* BLX (register), BX */
        if ((inst & 0xFF) == 0x1E)
            info->instr_sub_type = OCSD_S_INSTR_V7_IMPLIED_RET; /* BX LR */
    } else if ((inst & 0x0ff000f0) == 0x01200020) {
        /* BXJ: in v8 this behaves like BX */
    } else if ((inst & 0x0e108000) == 0x08108000) {
        /* POP {...,pc} or LDMxx {...,pc} */
        if ((inst & 0x0FFFA000) == 0x08BD8000) /* LDMIA SP!,{...,pc} */
            info->instr_sub_type = OCSD_S_INSTR_V7_IMPLIED_RET;
    } else if ((inst & 0x0e50f000) == 0x0410f000) {
        /* LDR PC,imm... inc. POP {PC} */
        if ( (inst & 0x01ff0000) == 0x009D0000)
            info->instr_sub_type = OCSD_S_INSTR_V7_IMPLIED_RET; /* LDR PC, [SP], #imm */
    } else if ((inst & 0x0e50f010) == 0x0610f000) {
        /* LDR PC,reg */
    } else if ((inst & 0x0fe0f000) == 0x01a0f000) {
        /* MOV PC,rx */
        if ((inst & 0x00100FFF) == 0x00E) /* ensure the S=0, LSL #0 variant - i.e plain MOV */
            info->instr_sub_type = OCSD_S_INSTR_V7_IMPLIED_RET; /* MOV PC, R14 */
    } else if ((inst & 0x0f900080) == 0x01000000) {
        /* "Miscellaneous instructions" - in DP space */
        is_indirect_branch = 0;
    } else if ((inst & 0x0f9000f0) == 0x01800090) {
        /* Some extended loads and stores */
        is_indirect_branch = 0;
    } else if ((inst & 0x0fb0f000) == 0x0320f000) {
        /* MSR #imm */
        is_indirect_branch = 0;
    } else if ((inst & 0x0e00f000) == 0x0200f000) {
        /* DP PC,imm shift */
        if ((inst & 0x0f90f000) == 0x0310f000) {
            /* TST/CMP */
           is_indirect_branch = 0;
        }
    } else if ((inst & 0x0e00f000) == 0x0000f000) {
        /* DP PC,reg */
    } else {
        is_indirect_branch = 0;
    }
    return is_indirect_branch;
}

int inst_Thumb_is_direct_branch(uint32_t inst, struct decode_info *info)
{
    uint8_t link, cond;
    return inst_Thumb_is_direct_branch_link(inst, &link, &cond, info);
}

int inst_Thumb_is_direct_branch_link(uint32_t inst, uint8_t *is_link, uint8_t *is_cond, struct decode_info *info)
{
    int is_direct_branch = 1;

    if ((inst & 0xf0000000) == 0xd0000000 && (inst & 0x0e000000) != 0x0e000000) {
        /* B<c> (encoding T1) */
        *is_cond = 1;
    } else if ((inst & 0xf8000000) == 0xe0000000) {
        /* B (encoding T2) */
    } else if ((inst & 0xf800d000) == 0xf0008000 && (inst & 0x03800000) != 0x03800000) {
        /* B (encoding T3) */
        *is_cond = 1;
    } else if ((inst & 0xf8009000) == 0xf0009000) {
        /* B (encoding T4); BL (encoding T1) */
        if (inst & 0x00004000) {
            *is_link = 1;
            info->instr_sub_type = OCSD_S_INSTR_BR_LINK;
        }
    } else if ((inst & 0xf800d001) == 0xf000c000) {
        /* BLX (imm) (encoding T2) */
        *is_link = 1;
        info->instr_sub_type = OCSD_S_INSTR_BR_LINK;
    } else if ((inst & 0xf5000000) == 0xb1000000) {
        /* CB(NZ) */
        *is_cond = 1;
    } else {
        is_direct_branch = 0;
    }
    return is_direct_branch;
}

int inst_Thumb_wfiwfe(uint32_t inst)
{
    int is_wfiwfe = 1;
    /* WFI, WFE may be branches in etm4.3++ */
    if ((inst & 0xfffffffe) == 0xf3af8002) {
        /* WFI & WFE (encoding T2) */
    }
    else if ((inst & 0xffef0000) == 0xbf200000) {
        /* WFI & WFE (encoding T1) */
    }
    else {
        is_wfiwfe = 0;
    }
    return is_wfiwfe;
}

int inst_Thumb_is_indirect_branch(uint32_t inst, struct decode_info *info)
{
    uint8_t link;
    return inst_Thumb_is_indirect_branch_link(inst, &link, info);
}

int inst_Thumb_is_indirect_branch_link(uint32_t inst, uint8_t *is_link, struct decode_info *info)
{
    /* See e.g. PFT Table 2-3 and Table 2-5 */
    int is_branch = 1;
    
    if ((inst & 0xff000000) == 0x47000000) {
        /* BX, BLX (reg) [v8M includes BXNS, BLXNS] */
        if (inst & 0x00800000) {
            *is_link = 1;
            info->instr_sub_type = OCSD_S_INSTR_BR_LINK;
        }
        else if ((inst & 0x00780000) == 0x00700000) {
            info->instr_sub_type = OCSD_S_INSTR_V7_IMPLIED_RET;  /* BX LR */
        }
    } else if ((inst & 0xfff0d000) == 0xf3c08000) {
        /* BXJ: in v8 this behaves like BX */
    } else if ((inst & 0xff000000) == 0xbd000000) {
        /* POP {pc} */
        info->instr_sub_type = OCSD_S_INSTR_V7_IMPLIED_RET;
    } else if ((inst & 0xfd870000) == 0x44870000) {
        /* MOV PC,reg or ADD PC,reg */
        if ((inst & 0xffff0000) == 0x46f70000)
            info->instr_sub_type = OCSD_S_INSTR_V7_IMPLIED_RET; /* MOV PC,LR */
    } else if ((inst & 0xfff0ffe0) == 0xe8d0f000) {
        /* TBB/TBH */
    } else if ((inst & 0xffd00000) == 0xe8100000) {
        /* RFE (T1) */
    } else if ((inst & 0xffd00000) == 0xe9900000) {
        /* RFE (T2) */
    } else if ((inst & 0xfff0d000) == 0xf3d08000) {
        /* SUBS PC,LR,#imm inc.ERET */
    } else if ((inst & 0xfff0f000) == 0xf8d0f000) {
        /* LDR PC,imm (T3) */
    } else if ((inst & 0xff7ff000) == 0xf85ff000) {
        /* LDR PC,literal (T2) */
    } else if ((inst & 0xfff0f800) == 0xf850f800) {
        /* LDR PC,imm (T4) */
        if((inst & 0x000f0f00) == 0x000d0b00)
            info->instr_sub_type = OCSD_S_INSTR_V7_IMPLIED_RET; /* LDR PC, [SP], #imm*/
    } else if ((inst & 0xfff0ffc0) == 0xf850f000) {
        /* LDR PC,reg (T2) */
    } else if ((inst & 0xfe508000) == 0xe8108000) {
        /* LDM PC */
        if ((inst & 0x0FFF0000) == 0x08BD0000) /* LDMIA [SP]!, */
            info->instr_sub_type = OCSD_S_INSTR_V7_IMPLIED_RET; /* POP {...,pc} */
    } else {
        is_branch = 0;
    }
    return is_branch;
}

int inst_A64_is_direct_branch(uint32_t inst, struct decode_info *info)
{
    uint8_t link = 0;
    return inst_A64_is_direct_branch_link(inst, &link, info);
}

int inst_A64_is_direct_branch_link(uint32_t inst, uint8_t *is_link, struct decode_info *info)
{
    int is_direct_branch = 1;
    if ((inst & 0x7c000000) == 0x34000000) {
        /* CB, TB */
    } else if ((inst & 0xff000010) == 0x54000000) {
        /* B<cond> */
    } else if ((inst & 0x7c000000) == 0x14000000) {
        /* B, BL imm */
        if (inst & 0x80000000) {
            *is_link = 1;
            info->instr_sub_type = OCSD_S_INSTR_BR_LINK;
        }
    } else {
        is_direct_branch = 0;
    }
    return is_direct_branch;
}

int inst_A64_wfiwfe(uint32_t inst)
{
    /* WFI, WFE may be traced as branches in etm 4.3++ */
    if ((inst & 0xffffffdf) == 0xd503205f)
        return 1;
    return 0;
}

int inst_A64_is_indirect_branch(uint32_t inst, struct decode_info *info)
{
    uint8_t link = 0;
    return inst_A64_is_indirect_branch_link(inst, &link, info);
}

int inst_A64_is_indirect_branch_link(uint32_t inst, uint8_t *is_link, struct decode_info *info)
{
    int is_indirect_branch = 1;

    if ((inst & 0xffdffc1f) == 0xd61f0000) {
        /* BR, BLR */
        if (inst & 0x00200000) {
            *is_link = 1;
            info->instr_sub_type = OCSD_S_INSTR_BR_LINK;
        }
    } else if ((inst & 0xfffffc1f) == 0xd65f0000) {
        info->instr_sub_type = OCSD_S_INSTR_V8_RET;
        /* RET */
    } else if ((inst & 0xffffffff) == 0xd69f03e0) {
        /* ERET */
        info->instr_sub_type = OCSD_S_INSTR_V8_ERET;
    } else if (info->arch_version >= 0x0803) {
        /* new pointer auth instr for v8.3 arch */   
        if ((inst & 0xffdff800) == 0xd71f0800) {
            /* BRAA, BRAB, BLRAA, BLRBB */
            if (inst & 0x00200000) {
                *is_link = 1;
                info->instr_sub_type = OCSD_S_INSTR_BR_LINK;
            }
        } else if ((inst & 0xffdff81F) == 0xd61f081F) {
            /* BRAAZ, BRABZ, BLRAAZ, BLRBBZ */
            if (inst & 0x00200000) {
                *is_link = 1;
                info->instr_sub_type = OCSD_S_INSTR_BR_LINK;
            }
        } else if ((inst & 0xfffffbff) == 0xd69f0bff) {
            /* ERETAA, ERETAB */
            info->instr_sub_type = OCSD_S_INSTR_V8_ERET;
        } else if ((inst & 0xfffffbff) == 0xd65f0bff) {
            /* RETAA, RETAB */
            info->instr_sub_type = OCSD_S_INSTR_V8_RET;
        } else {
            is_indirect_branch = 0;
        }
    } else {
        is_indirect_branch = 0;
    }
    return is_indirect_branch;
}

int inst_ARM_branch_destination(uint32_t addr, uint32_t inst, uint32_t *pnpc)
{
    uint32_t npc;
    int is_direct_branch = 1;
    if ((inst & 0x0e000000) == 0x0a000000) {
        /*
          B:   cccc:1010:imm24
          BL:  cccc:1011:imm24
          BLX: 1111:101H:imm24
        */
        npc = addr + 8 + ((int32_t)((inst & 0xffffff) << 8) >> 6);
        if ((inst & 0xf0000000) == 0xf0000000) {
            npc |= 1;  /* indicate ISA is now Thumb */
            npc |= ((inst >> 23) & 2);   /* apply the H bit */
        }
    } else {
        is_direct_branch = 0;
    }
    if (is_direct_branch && pnpc != NULL) {
        *pnpc = npc;
    }
    return is_direct_branch;
}

int inst_Thumb_branch_destination(uint32_t addr, uint32_t inst, uint32_t *pnpc)
{
    uint32_t npc;
    int is_direct_branch = 1;
    if ((inst & 0xf0000000) == 0xd0000000 && (inst & 0x0e000000) != 0x0e000000) {
        /* B<c> (encoding T1) */
        npc = addr + 4 + ((int32_t)((inst & 0x00ff0000) << 8) >> 23);
        npc |= 1;
    } else if ((inst & 0xf8000000) == 0xe0000000) {
        /* B (encoding T2) */
        npc = addr + 4 + ((int32_t)((inst & 0x07ff0000) << 5) >> 20);
        npc |= 1;
    } else if ((inst & 0xf800d000) == 0xf0008000 && (inst & 0x03800000) != 0x03800000) {
        /* B (encoding T3) */
        npc = addr + 4 + ((int32_t)(((inst & 0x04000000) << 5) |
                                    ((inst & 0x0800) << 19) |
                                    ((inst & 0x2000) << 16) |
                                    ((inst & 0x003f0000) << 7) |
                                    ((inst & 0x000007ff) << 12)) >> 11);
        npc |= 1;
    } else if ((inst & 0xf8009000) == 0xf0009000) {
        /* B (encoding T4); BL (encoding T1) */
        uint32_t S = ((inst & 0x04000000) >> 26)-1;  /* ffffffff or 0 according to S bit */
        npc = addr + 4 + ((int32_t)(((inst & 0x04000000) << 5) |
                                    (((inst^S) & 0x2000) << 17) |
                                    (((inst^S) & 0x0800) << 18) |
                                    ((inst & 0x03ff0000) << 3) |
                                    ((inst & 0x000007ff) << 8)) >> 7);
        npc |= 1;
    } else if ((inst & 0xf800d001) == 0xf000c000) {
        /* BLX (encoding T2) */
        uint32_t S = ((inst & 0x04000000) >> 26)-1;  /* ffffffff or 0 according to S bit */
        addr &= 0xfffffffc;   /* Align(PC,4) */
        npc = addr + 4 + ((int32_t)(((inst & 0x04000000) << 5) |
                                    (((inst^S) & 0x2000) << 17) |
                                    (((inst^S) & 0x0800) << 18) |
                                    ((inst & 0x03ff0000) << 3) |
                                    ((inst & 0x000007fe) << 8)) >> 7);
        /* don't set the Thumb bit, as we're transferring to ARM */
    } else if ((inst & 0xf5000000) == 0xb1000000) {
        /* CB(NZ) */
        /* Note that it's zero-extended - always a forward branch */
        npc = addr + 4 + ((((inst & 0x02000000) << 6) |
                           ((inst & 0x00f80000) << 7)) >> 25);
        npc |= 1;
    } else {
        is_direct_branch = 0;
    }
    if (is_direct_branch && pnpc != NULL) {
        *pnpc = npc;
    }
    return is_direct_branch;
}

int inst_A64_branch_destination(uint64_t addr, uint32_t inst, uint64_t *pnpc)
{
    uint64_t npc;
    int is_direct_branch = 1;
    if ((inst & 0xff000010) == 0x54000000) {
        /* B<cond> */
        npc = addr + ((int32_t)((inst & 0x00ffffe0) << 8) >> 11);
    } else if ((inst & 0x7c000000) == 0x14000000) {
        /* B, BL imm */
        npc = addr + ((int32_t)((inst & 0x03ffffff) << 6) >> 4);
    } else if ((inst & 0x7e000000) == 0x34000000) {
        /* CB */
        npc = addr + ((int32_t)((inst & 0x00ffffe0) << 8) >> 11);
    } else if ((inst & 0x7e000000) == 0x36000000) {
        /* TB */
        npc = addr + ((int32_t)((inst & 0x0007ffe0) << 13) >> 16);
    } else {
        is_direct_branch = 0;
    }
    if (is_direct_branch && pnpc != NULL) {
        *pnpc = npc;
    }
    return is_direct_branch;
}

int inst_ARM_is_branch(uint32_t inst, struct decode_info *info)
{
    return inst_ARM_is_indirect_branch(inst, info) ||
           inst_ARM_is_direct_branch(inst);
}

int inst_Thumb_is_branch(uint32_t inst, struct decode_info *info)
{
    return inst_Thumb_is_indirect_branch(inst, info) ||
           inst_Thumb_is_direct_branch(inst, info);
}

int inst_A64_is_branch(uint32_t inst, struct decode_info *info)
{
    return inst_A64_is_indirect_branch(inst, info) ||
           inst_A64_is_direct_branch(inst, info);
}

int inst_ARM_is_branch_and_link(uint32_t inst, struct decode_info *info)
{
    int is_branch = 1;
    if ((inst & 0xf0000000) == 0xf0000000) {
        if ((inst & 0xfe000000) == 0xfa000000){
            info->instr_sub_type = OCSD_S_INSTR_BR_LINK;
            /* BLX (imm) */
        } else {
            is_branch = 0;
        }
    } else if ((inst & 0x0f000000) == 0x0b000000) {
        info->instr_sub_type = OCSD_S_INSTR_BR_LINK;
        /* BL */
    } else if ((inst & 0x0ff000f0) == 0x01200030) {
        info->instr_sub_type = OCSD_S_INSTR_BR_LINK;
        /* BLX (reg) */
    } else {
        is_branch = 0;
    }
    return is_branch;
}

int inst_Thumb_is_branch_and_link(uint32_t inst, struct decode_info *info)
{
    int is_branch = 1;
    if ((inst & 0xff800000) == 0x47800000) {
        info->instr_sub_type = OCSD_S_INSTR_BR_LINK;
        /* BLX (reg) */
    } else if ((inst & 0xf800c000) == 0xf000c000) {
        info->instr_sub_type = OCSD_S_INSTR_BR_LINK;
        /* BL, BLX (imm) */
    } else {
        is_branch = 0;
    }
    return is_branch;
}

int inst_A64_is_branch_and_link(uint32_t inst, struct decode_info *info)
{
    int is_branch = 1;
    if ((inst & 0xfffffc1f) == 0xd63f0000) {
        /* BLR */
        info->instr_sub_type = OCSD_S_INSTR_BR_LINK;
    } else if ((inst & 0xfc000000) == 0x94000000) {
        /* BL */
        info->instr_sub_type = OCSD_S_INSTR_BR_LINK;
    }  else if (info->arch_version >= 0x0803) {
        /* new pointer auth instr for v8.3 arch */
        if ((inst & 0xfffff800) == 0xd73f0800) {
            /* BLRAA, BLRBB */
            info->instr_sub_type = OCSD_S_INSTR_BR_LINK;
        } else if ((inst & 0xfffff81F) == 0xd63f081F) {
            /* BLRAAZ, BLRBBZ */
            info->instr_sub_type = OCSD_S_INSTR_BR_LINK;
        } else {
            is_branch = 0;
        }
    } else {
        is_branch = 0;
    }
    return is_branch;
}

int inst_ARM_is_conditional(uint32_t inst)
{
    return (inst & 0xe0000000) != 0xe0000000;
}

int inst_Thumb_is_conditional(uint32_t inst)
{
    if ((inst & 0xf0000000) == 0xd0000000 && (inst & 0x0e000000) != 0x0e000000) {
        /* B<c> (encoding T1) */
        return 1;
    } else if ((inst & 0xf800d000) == 0xf0008000 && (inst & 0x03800000) != 0x03800000) {
        /* B<c> (encoding T3) */
        return 1;
    } else if ((inst & 0xf5000000) == 0xb1000000) {
        /* CB(N)Z */
        return 1;
    }
    return 0;
}

unsigned int inst_Thumb_is_IT(uint32_t inst)
{
    if ((inst & 0xff000000) == 0xbf000000 &&
        (inst & 0x000f0000) != 0x00000000) {
        if (inst & 0x00010000) {
            return 4;
        } else if (inst & 0x00020000) {
            return 3;
        } else if (inst & 0x00040000) {
            return 2;
        } else {
            assert(inst & 0x00080000);
            return 1;
        }
    } else {
        return 0;
    }
}

/*
Test whether an A64 instruction is conditional.

Instructions like CSEL, CSINV, CCMP are not classed as conditional.
They use the condition code but do one of two things with it,
neither a NOP.  The "intruction categories" section of ETMv4
lists no (non branch) conditional instructions for A64.
*/
int inst_A64_is_conditional(uint32_t inst)
{
    if ((inst & 0x7c000000) == 0x34000000) {
        /* CB, TB */
        return 1;
    } else if ((inst & 0xff000010) == 0x54000000) {
        /* B.cond */
        return 1;
    }
    return 0;
}

arm_barrier_t inst_ARM_barrier(uint32_t inst)
{
    if ((inst & 0xfff00000) == 0xf5700000) {
        switch (inst & 0xf0) {
        case 0x40:
            return ARM_BARRIER_DSB;
        case 0x50:
            return ARM_BARRIER_DMB;
        case 0x60:
            return ARM_BARRIER_ISB;
        default:
            return ARM_BARRIER_NONE;
        }
    } else if ((inst & 0x0fff0f00) == 0x0e070f00) {
        switch (inst & 0xff) {
        case 0x9a:
            return ARM_BARRIER_DSB;   /* mcr p15,0,Rt,c7,c10,4 */
        case 0xba:
            return ARM_BARRIER_DMB;   /* mcr p15,0,Rt,c7,c10,5 */
        case 0x95:
            return ARM_BARRIER_ISB;   /* mcr p15,0,Rt,c7,c5,4 */
        default:
            return ARM_BARRIER_NONE;
        }
    } else {
        return ARM_BARRIER_NONE;
    }
}

arm_barrier_t inst_Thumb_barrier(uint32_t inst)
{
    if ((inst & 0xffffff00) == 0xf3bf8f00) {
        switch (inst & 0xf0) {
        case 0x40:
            return ARM_BARRIER_DSB;
        case 0x50:
            return ARM_BARRIER_DMB;
        case 0x60:
            return ARM_BARRIER_ISB;
        default:
            return ARM_BARRIER_NONE;
        }
    } else if ((inst & 0xffff0f00) == 0xee070f00) {
        /* Thumb2 CP15 barriers are unlikely... 1156T2 only? */
        switch (inst & 0xff) {
        case 0x9a:
            return ARM_BARRIER_DSB;   /* mcr p15,0,Rt,c7,c10,4 */
        case 0xba:
            return ARM_BARRIER_DMB;   /* mcr p15,0,Rt,c7,c10,5 */
        case 0x95:
            return ARM_BARRIER_ISB;   /* mcr p15,0,Rt,c7,c5,4 */
        default:
            return ARM_BARRIER_NONE;
        }
        return ARM_BARRIER_NONE;
    } else {
        return ARM_BARRIER_NONE;
    }
}

arm_barrier_t inst_A64_barrier(uint32_t inst)
{
    if ((inst & 0xfffff09f) == 0xd503309f) {
        switch (inst & 0x60) {
        case 0x0:
            return ARM_BARRIER_DSB;
        case 0x20:
            return ARM_BARRIER_DMB;
        case 0x40:
            return ARM_BARRIER_ISB;
        default:
            return ARM_BARRIER_NONE;
        }
    } else {
        return ARM_BARRIER_NONE;
    }
}

int inst_ARM_is_UDF(uint32_t inst)
{
    return (inst & 0xfff000f0) == 0xe7f000f0;
}

int inst_Thumb_is_UDF(uint32_t inst)
{
    return (inst & 0xff000000) == 0xde000000 ||   /* T1 */
           (inst & 0xfff0f000) == 0xf7f0a000;     /* T2 */
}

int inst_A64_is_UDF(uint32_t inst)
{
    /* No A64 encodings are formally allocated as permanently undefined,
       but it is intended not to allocate any instructions in the 21-bit
       regions at the bottom or top of the range. */
    return (inst & 0xffe00000) == 0x00000000 ||
           (inst & 0xffe00000) == 0xffe00000;
}

/* End of File trc_idec_arminst.cpp */