summaryrefslogtreecommitdiff
path: root/arm/arm_codegen.h
blob: df42519b1adca6b390a654ad476b9f6f97cbdc70 (plain)
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
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
/*
 * arm-codegen.h
 *
 * Copyright (c) 2002 Wild West Software
 * Copyright (c) 2001, 2002 Sergey Chaban
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */


#ifndef ARM_CG_H
#define ARM_CG_H

typedef unsigned long arminstr_t;
typedef unsigned long armword_t;

/* Helper functions */
/*void arm_emit_std_prologue(cg_segment_t * segment, unsigned int local_size);
void arm_emit_std_epilogue(cg_segment_t * segment, unsigned int local_size, int pop_regs);
void arm_emit_lean_prologue(cg_segment_t * segment, unsigned int local_size, int push_regs);
int arm_is_power_of_2(armword_t val);
int calc_arm_mov_const_shift(armword_t val);
int is_arm_const(armword_t val);
int arm_bsf(armword_t val);
void arm_mov_reg_imm32_cond(cg_segment_t * segment, int reg, armword_t imm32, int cond);
void arm_mov_reg_imm32(cg_segment_t * segment, int reg, armword_t imm32);*/


//*** check for correctness ***
//extern u32* x86Ptr;

void write_to_file(u32 val);

//#define write32(val) { *(u32 *)translation_ptr = val; write_to_file(*(u32 *)translation_ptr); translation_ptr += 4; }

//#define write32(val) { if( g_PcWatch.IsReset == RECRESET_OFF ) { *(u32*)pCurPage->pCodeCurrent = val; pCurPage->pCodeCurrent +=4; if( (u32)pCurPage->pCodeCurrent >= (u32)pCurPage->pCodeEnd ) { g_PcWatch.IsReset = RECRESET_START; recResize(); g_PcWatch.IsReset = RECRESET_END; return; } }else{ if( g_PcWatch.IsReset == RECRESET_END ){ g_PcWatch.IsReset = RECRESET_OFF; return; } } }
//#define write32_ret(val) { if( g_PcWatch.IsReset == RECRESET_OFF ) { *(u32*)pCurPage->pCodeCurrent = val; pCurPage->pCodeCurrent +=4; if( (u32)pCurPage->pCodeCurrent >= (u32)pCurPage->pCodeEnd ) { g_PcWatch.IsReset = RECRESET_START; recResize(); g_PcWatch.IsReset = RECRESET_END; return 0; } }else{ if( g_PcWatch.IsReset == RECRESET_END ){ g_PcWatch.IsReset = RECRESET_OFF; return 0; } } }
//#define write32(val) { *(u32*)pCurPage->pCodeCurrent = val; pCurPage->pCodeCurrent +=4; }

#define ARM_EMIT(p, i) write32(i);
//write32(i);
/*{ *(u32*)translation_ptr = (i); translation_ptr += 4; } */

#if defined(_MSC_VER) && !defined(ARM_NOIASM)
# define ARM_IASM(_expr) __easfdmit (_expr)
#else
# define ARM_IASM(_expr)
#endif

/* even_scale = rot << 1 */
#define ARM_SCALE(imm8, even_scale) ( ((imm8) >> (even_scale)) | ((imm8) << (32 - even_scale)) )



typedef enum {
  ARMREG_R0 = 0,
  ARMREG_R1,
  ARMREG_R2,
  ARMREG_R3,
  ARMREG_R4,
  ARMREG_R5,
  ARMREG_R6,
  ARMREG_R7,
  ARMREG_R8,
  ARMREG_R9,
  ARMREG_R10,
  ARMREG_R11,
  ARMREG_R12,
  ARMREG_R13,
  ARMREG_R14,
  ARMREG_R15,


  /* aliases */
  /* args */
  ARMREG_A1 = ARMREG_R0,
  ARMREG_A2 = ARMREG_R1,
  ARMREG_A3 = ARMREG_R2,
  ARMREG_A4 = ARMREG_R3,

  /* local vars */
  ARMREG_V1 = ARMREG_R4,
  ARMREG_V2 = ARMREG_R5,
  ARMREG_V3 = ARMREG_R6,
  ARMREG_V4 = ARMREG_R7,
  ARMREG_V5 = ARMREG_R8,
  ARMREG_V6 = ARMREG_R9,
  ARMREG_V7 = ARMREG_R10,

  ARMREG_FP = ARMREG_R11,
  ARMREG_IP = ARMREG_R12,
  ARMREG_SP = ARMREG_R13,
  ARMREG_LR = ARMREG_R14,
  ARMREG_PC = ARMREG_R15,

  /* FPU */
  ARMREG_F0 = 0,
  ARMREG_F1,
  ARMREG_F2,
  ARMREG_F3,
  ARMREG_F4,
  ARMREG_F5,
  ARMREG_F6,
  ARMREG_F7,

  /* co-processor */
  ARMREG_CR0 = 0,
  ARMREG_CR1,
  ARMREG_CR2,
  ARMREG_CR3,
  ARMREG_CR4,
  ARMREG_CR5,
  ARMREG_CR6,
  ARMREG_CR7,
  ARMREG_CR8,
  ARMREG_CR9,
  ARMREG_CR10,
  ARMREG_CR11,
  ARMREG_CR12,
  ARMREG_CR13,
  ARMREG_CR14,
  ARMREG_CR15,

  /* XScale: acc0 on CP0 */
  ARMREG_ACC0 = ARMREG_CR0,

  ARMREG_MAX = ARMREG_R15,

  /* flags */
  ARMREG_CPSR = 0,
  ARMREG_SPSR = 1
} ARMReg;

typedef enum {
  ARM_FCONST_0_0 = 8,
  ARM_FCONST_1_0,
  ARM_FCONST_2_0,
  ARM_FCONST_3_0,
  ARM_FCONST_4_0,
  ARM_FCONST_5_0,
  ARM_FCONST_0_5,
  ARM_FCONST_10_0
} ARMFPUConst;

/* number of argument registers */
#define ARM_NUM_ARG_REGS 4

/* number of non-argument registers */
#define ARM_NUM_VARIABLE_REGS 7

/* number of global registers */
#define ARM_NUM_GLOBAL_REGS 5

/* bitvector for all argument regs (A1-A4) */
#define ARM_ALL_ARG_REGS \
  (1 << ARMREG_A1) | (1 << ARMREG_A2) | (1 << ARMREG_A3) | (1 << ARMREG_A4)


typedef enum {
  ARMCOND_EQ = 0x0,          /* Equal; Z = 1 */
  ARMCOND_NE = 0x1,          /* Not equal, or unordered; Z = 0 */
  ARMCOND_CS = 0x2,          /* Carry set; C = 1 */
  ARMCOND_HS = ARMCOND_CS,   /* Unsigned higher or same; */
  ARMCOND_CC = 0x3,          /* Carry clear; C = 0 */
  ARMCOND_LO = ARMCOND_CC,   /* Unsigned lower */
  ARMCOND_MI = 0x4,          /* Negative; N = 1 */
  ARMCOND_PL = 0x5,          /* Positive or zero; N = 0 */
  ARMCOND_VS = 0x6,          /* Overflow; V = 1 */
  ARMCOND_VC = 0x7,          /* No overflow; V = 0 */
  ARMCOND_HI = 0x8,          /* Unsigned higher; C = 1 && Z = 0 */
  ARMCOND_LS = 0x9,          /* Unsigned lower or same; C = 0 || Z = 1 */
  ARMCOND_GE = 0xA,          /* Signed greater than or equal; N = V */
  ARMCOND_LT = 0xB,          /* Signed less than; N != V */
  ARMCOND_GT = 0xC,          /* Signed greater than; Z = 0 && N = V */
  ARMCOND_LE = 0xD,          /* Signed less than or equal; Z = 1 && N != V */
  ARMCOND_AL = 0xE,          /* Always */
  ARMCOND_NV = 0xF,          /* Never */

  ARMCOND_SHIFT = 28
} ARMCond;

#define ARMCOND_MASK (ARMCOND_NV << ARMCOND_SHIFT)

#define ARM_DEF_COND(cond) (((cond) & 0xF) << ARMCOND_SHIFT)



typedef enum {
  ARMSHIFT_LSL = 0,
  ARMSHIFT_LSR = 1,
  ARMSHIFT_ASR = 2,
  ARMSHIFT_ROR = 3,

  ARMSHIFT_ASL = ARMSHIFT_LSL
  /* rrx = (ror, 1) */
} ARMShiftType;


typedef struct {
  armword_t PSR_c : 8;
  armword_t PSR_x : 8;
  armword_t PSR_s : 8;
  armword_t PSR_f : 8;
} ARMPSR;

typedef enum {
  ARMOP_AND = 0x0,
  ARMOP_EOR = 0x1,
  ARMOP_SUB = 0x2,
  ARMOP_RSB = 0x3,
  ARMOP_ADD = 0x4,
  ARMOP_ADC = 0x5,
  ARMOP_SBC = 0x6,
  ARMOP_RSC = 0x7,
  ARMOP_TST = 0x8,
  ARMOP_TEQ = 0x9,
  ARMOP_CMP = 0xa,
  ARMOP_CMN = 0xb,
  ARMOP_ORR = 0xc,
  ARMOP_MOV = 0xd,
  ARMOP_BIC = 0xe,
  ARMOP_MVN = 0xf,


  /* not really opcodes */

  ARMOP_STR = 0x0,
  ARMOP_LDR = 0x1,

  /* ARM2+ */
  ARMOP_MUL   = 0x0, /* Rd := Rm*Rs */
  ARMOP_MLA   = 0x1, /* Rd := (Rm*Rs)+Rn */

  /* ARM3M+ */
  ARMOP_UMULL = 0x4,
  ARMOP_UMLAL = 0x5,
  ARMOP_SMULL = 0x6,
  ARMOP_SMLAL = 0x7,

  /* for data transfers with register offset */
  ARM_UP   = 1,
  ARM_DOWN = 0
} ARMOpcode;

typedef enum {
  THUMBOP_AND  = 0,
  THUMBOP_EOR  = 1,
  THUMBOP_LSL  = 2,
  THUMBOP_LSR  = 3,
  THUMBOP_ASR  = 4,
  THUMBOP_ADC  = 5,
  THUMBOP_SBC  = 6,
  THUMBOP_ROR  = 7,
  THUMBOP_TST  = 8,
  THUMBOP_NEG  = 9,
  THUMBOP_CMP  = 10,
  THUMBOP_CMN  = 11,
  THUMBOP_ORR  = 12,
  THUMBOP_MUL  = 13,
  THUMBOP_BIC  = 14,
  THUMBOP_MVN  = 15,
  THUMBOP_MOV  = 16,
  THUMBOP_CMPI = 17,
  THUMBOP_ADD  = 18,
  THUMBOP_SUB  = 19,
  THUMBOP_CMPH = 19,
  THUMBOP_MOVH = 20
} ThumbOpcode;


/* Generic form - all ARM instructions are conditional. */
typedef struct {
  arminstr_t icode : 28;
  arminstr_t cond  :  4;
} ARMInstrGeneric;



/* Branch or Branch with Link instructions. */
typedef struct {
  arminstr_t offset : 24;
  arminstr_t link   :  1;
  arminstr_t tag    :  3; /* 1 0 1 */
  arminstr_t cond   :  4;
} ARMInstrBR;

#define ARM_BR_ID 5
#define ARM_BR_MASK 7 << 25
#define ARM_BR_TAG ARM_BR_ID << 25

#define ARM_DEF_BR(offs, l, cond) ((offs) | ((l) << 24) | (ARM_BR_TAG) | (cond << ARMCOND_SHIFT))

/* branch */
#define ARM_B_COND(p, cond, offset) ARM_EMIT(p, ARM_DEF_BR(offset, 0, cond))
#define ARM_B(p, offs) ARM_B_COND((p), ARMCOND_AL, (offs))
/* branch with link */
#define ARM_BL_COND(p, cond, offset) ARM_EMIT(p, ARM_DEF_BR(offset, 1, cond))
#define ARM_BL(p, offs) ARM_BL_COND((p), ARMCOND_AL, (offs))

/* branch to register and exchange */
#define ARM_BX_COND(p, cond, reg) ARM_EMIT(p, ((cond << ARMCOND_SHIFT) | (reg) | 0x12FFF10))
#define ARM_BX(p, reg) ARM_BX_COND((p), ARMCOND_AL, (reg))

/* branch to register with link */
#define ARM_BLX_COND(p, cond, reg) ARM_EMIT(p, ((cond << ARMCOND_SHIFT) | (reg) | 0x12FFF30))
#define ARM_BLX(p, reg) ARM_BLX_COND((p), ARMCOND_AL, (reg))


/* Data Processing Instructions - there are 3 types. */

typedef struct {
  arminstr_t imm : 8;
  arminstr_t rot : 4;
} ARMDPI_op2_imm;

typedef struct {
  arminstr_t rm   : 4;
  arminstr_t tag  : 1; /* 0 - immediate shift, 1 - reg shift */
  arminstr_t type : 2; /* shift type - logical, arithmetic, rotate */
} ARMDPI_op2_reg_shift;


/* op2 is reg shift by imm */
typedef union {
  ARMDPI_op2_reg_shift r2;
  struct {
    arminstr_t _dummy_r2 : 7;
    arminstr_t shift : 5;
  } imm;
} ARMDPI_op2_reg_imm;

/* op2 is reg shift by reg */
typedef union {
  ARMDPI_op2_reg_shift r2;
  struct {
    arminstr_t _dummy_r2 : 7;
    arminstr_t pad       : 1; /* always 0, to differentiate from HXFER etc. */
    arminstr_t rs        : 4;
  } reg;
} ARMDPI_op2_reg_reg;

/* Data processing instrs */
typedef union {
  ARMDPI_op2_imm op2_imm;

  ARMDPI_op2_reg_shift op2_reg;
  ARMDPI_op2_reg_imm op2_reg_imm;
  ARMDPI_op2_reg_reg op2_reg_reg;

  struct {
    arminstr_t op2    : 12; /* raw operand 2 */
    arminstr_t rd     :  4; /* destination reg */
    arminstr_t rn     :  4; /* first operand reg */
    arminstr_t s      :  1; /* S-bit controls PSR update */
    arminstr_t opcode :  4; /* arithmetic/logic operation */
    arminstr_t type   :  1; /* type of op2, 0 = register, 1 = immediate */
    arminstr_t tag    :  2; /* 0 0 */
    arminstr_t cond   :  4;
  } all;
} ARMInstrDPI;

#define ARM_DPI_ID 0
#define ARM_DPI_MASK 3 << 26
#define ARM_DPI_TAG ARM_DPI_ID << 26

#define ARM_DEF_DPI_IMM_COND(imm8, rot, rd, rn, s, op, cond) \
  ((imm8) & 0xFF)      | \
  (((rot) & 0xF) << 8) | \
  ((rd) << 12)         | \
  ((rn) << 16)         | \
  ((s) << 20)          | \
  ((op) << 21)         | \
  (1 << 25)            | \
  (ARM_DPI_TAG)        | \
  ARM_DEF_COND(cond)


#define ARM_DEF_DPI_IMM(imm8, rot, rd, rn, s, op) \
  ARM_DEF_DPI_IMM_COND(imm8, rot, rd, rn, s, op, ARMCOND_AL)

/* codegen */
#define ARM_DPIOP_REG_IMM8ROT_COND(p, op, rd, rn, imm8, rot, cond) \
  ARM_EMIT(p, ARM_DEF_DPI_IMM_COND((imm8), ((rot) >> 1), (rd), (rn), 0, (op), cond))
#define ARM_DPIOP_S_REG_IMM8ROT_COND(p, op, rd, rn, imm8, rot, cond) \
  ARM_EMIT(p, ARM_DEF_DPI_IMM_COND((imm8), ((rot) >> 1), (rd), (rn), 1, (op), cond))

/* inline */
#define ARM_IASM_DPIOP_REG_IMM8ROT_COND(p, op, rd, rn, imm8, rot, cond) \
  ARM_IASM(ARM_DEF_DPI_IMM_COND((imm8), ((rot) >> 1), (rd), (rn), 0, (op), cond))
#define ARM_IASM_DPIOP_S_REG_IMM8ROT_COND(p, op, rd, rn, imm8, rot, cond) \
  ARM_IASM(ARM_DEF_DPI_IMM_COND((imm8), ((rot) >> 1), (rd), (rn), 1, (op), cond))



#define ARM_DEF_DPI_REG_IMMSHIFT_COND(rm, shift_type, imm_shift, rd, rn, s, op, cond) \
  (rm)                        | \
  ((shift_type & 3) << 5)     | \
  (((imm_shift) & 0x1F) << 7) | \
  ((rd) << 12)                | \
  ((rn) << 16)                | \
  ((s) << 20)                 | \
  ((op) << 21)                | \
  (ARM_DPI_TAG)               | \
  ARM_DEF_COND(cond)

/* codegen */
#define ARM_DPIOP_REG_IMMSHIFT_COND(p, op, rd, rn, rm, shift_type, imm_shift, cond) \
  ARM_EMIT(p, ARM_DEF_DPI_REG_IMMSHIFT_COND((rm), shift_type, imm_shift, (rd), (rn), 0, (op), cond))

#define ARM_DPIOP_S_REG_IMMSHIFT_COND(p, op, rd, rn, rm, shift_type, imm_shift, cond) \
  ARM_EMIT(p, ARM_DEF_DPI_REG_IMMSHIFT_COND((rm), shift_type, imm_shift, (rd), (rn), 1, (op), cond))

#define ARM_DPIOP_REG_REG_COND(p, op, rd, rn, rm, cond) \
  ARM_EMIT(p, ARM_DEF_DPI_REG_IMMSHIFT_COND((rm), ARMSHIFT_LSL, 0, (rd), (rn), 0, (op), cond))

#define ARM_DPIOP_S_REG_REG_COND(p, op, rd, rn, rm, cond) \
  ARM_EMIT(p, ARM_DEF_DPI_REG_IMMSHIFT_COND((rm), ARMSHIFT_LSL, 0, (rd), (rn), 1, (op), cond))

/* inline */
#define ARM_IASM_DPIOP_REG_IMMSHIFT_COND(p, op, rd, rn, rm, shift_type, imm_shift, cond) \
  ARM_IASM(ARM_DEF_DPI_REG_IMMSHIFT_COND((rm), shift_type, imm_shift, (rd), (rn), 0, (op), cond))

#define ARM_IASM_DPIOP_S_REG_IMMSHIFT_COND(p, op, rd, rn, rm, shift_type, imm_shift, cond) \
  ARM_IASM(ARM_DEF_DPI_REG_IMMSHIFT_COND((rm), shift_type, imm_shift, (rd), (rn), 1, (op), cond))

#define ARM_IASM_DPIOP_REG_REG_COND(p, op, rd, rn, rm, cond) \
  ARM_IASM(ARM_DEF_DPI_REG_IMMSHIFT_COND((rm), ARMSHIFT_LSL, 0, (rd), (rn), 0, (op), cond))

#define ARM_IASM_DPIOP_S_REG_REG_COND(p, op, rd, rn, rm, cond) \
  ARM_IASM_EMIT(ARM_DEF_DPI_REG_IMMSHIFT_COND((rm), ARMSHIFT_LSL, 0, (rd), (rn), 1, (op), cond))


/* Rd := Rn op (Rm shift_type Rs) */
#define ARM_DEF_DPI_REG_REGSHIFT_COND(rm, shift_type, rs, rd, rn, s, op, cond) \
  (rm)                        | \
  (1 << 4)                    | \
  ((shift_type & 3) << 5)     | \
  ((rs) << 8)                 | \
  ((rd) << 12)                | \
  ((rn) << 16)                | \
  ((s) << 20)                 | \
  ((op) << 21)                | \
  (ARM_DPI_TAG)               | \
  ARM_DEF_COND(cond)

/* codegen */
#define ARM_DPIOP_REG_REGSHIFT_COND(p, op, rd, rn, rm, shift_type, rs, cond) \
  ARM_EMIT(p, ARM_DEF_DPI_REG_REGSHIFT_COND((rm), shift_type, (rs), (rd), (rn), 0, (op), cond))

#define ARM_DPIOP_S_REG_REGSHIFT_COND(p, op, rd, rn, rm, shift_type, rs, cond) \
  ARM_EMIT(p, ARM_DEF_DPI_REG_REGSHIFT_COND((rm), shift_type, (rs), (rd), (rn), 1, (op), cond))

/* inline */
#define ARM_IASM_DPIOP_REG_REGSHIFT_COND(p, op, rd, rn, rm, shift_type, rs, cond) \
  ARM_IASM(ARM_DEF_DPI_REG_REGSHIFT_COND((rm), shift_type, (rs), (rd), (rn), 0, (op), cond))

#define ARM_IASM_DPIOP_S_REG_REGSHIFT_COND(p, op, rd, rn, rm, shift_type, rs, cond) \
  ARM_IASM(ARM_DEF_DPI_REG_REGSHIFT_COND((rm), shift_type, (rs), (rd), (rn), 1, (op), cond))



/* Multiple register transfer. */
typedef struct {
  arminstr_t reg_list : 16; /* bitfield */
  arminstr_t rn       :  4; /* base reg */
  arminstr_t ls       :  1; /* load(1)/store(0) */
  arminstr_t wb       :  1; /* write-back "!" */
  arminstr_t s        :  1; /* restore PSR, force user bit */
  arminstr_t u        :  1; /* up/down */
  arminstr_t p        :  1; /* pre(1)/post(0) index */
  arminstr_t tag      :  3; /* 1 0 0 */
  arminstr_t cond     :  4;
} ARMInstrMRT;

#define ARM_MRT_ID 4
#define ARM_MRT_MASK 7 << 25
#define ARM_MRT_TAG ARM_MRT_ID << 25

#define ARM_DEF_MRT(regs, rn, l, w, s, u, p, cond) \
  (regs)        | \
  (rn << 16)    | \
  (l << 20)     | \
  (w << 21)     | \
  (s << 22)     | \
  (u << 23)     | \
  (p << 24)     | \
  (ARM_MRT_TAG) | \
  ARM_DEF_COND(cond)

#define ARM_STMDB(p, rbase, regs) ARM_EMIT(p, ARM_DEF_MRT(regs, rbase, 0, 0, 0, 0, 1, ARMCOND_AL))
#define ARM_LDMDB(p, rbase, regs) ARM_EMIT(p, ARM_DEF_MRT(regs, rbase, 1, 0, 0, 0, 1, ARMCOND_AL))
#define ARM_STMDB_WB(p, rbase, regs) ARM_EMIT(p, ARM_DEF_MRT(regs, rbase, 0, 1, 0, 0, 1, ARMCOND_AL))
#define ARM_LDMIA_WB(p, rbase, regs) ARM_EMIT(p, ARM_DEF_MRT(regs, rbase, 1, 1, 0, 1, 0, ARMCOND_AL))
#define ARM_LDMIA(p, rbase, regs) ARM_EMIT(p, ARM_DEF_MRT(regs, rbase, 1, 0, 0, 1, 0, ARMCOND_AL))
#define ARM_STMIA(p, rbase, regs) ARM_EMIT(p, ARM_DEF_MRT(regs, rbase, 0, 0, 0, 1, 0, ARMCOND_AL))
#define ARM_STMIA_WB(p, rbase, regs) ARM_EMIT(p, ARM_DEF_MRT(regs, rbase, 0, 1, 0, 1, 0, ARMCOND_AL))

#define ARM_LDMIA_WB_PC_S(p, rbase, regs) ARM_EMIT(p, ARM_DEF_MRT(regs, rbase, 1, 1, 1, 1, 0, ARMCOND_AL))

/* THUMB
#define ARM_POP_OP(p) ARM_EMIT(p, 0xFF01BD17)
#define ARM_PUSH_OP(p) ARM_EMIT(p, 0xFF02B497)
*/

/* stmdb sp!, {regs} */
#define ARM_PUSH(p, regs) ARM_EMIT(p, ARM_DEF_MRT(regs, ARMREG_SP, 0, 1, 0, 0, 1, ARMCOND_AL))
#define ARM_IASM_PUSH(regs) ARM_IASM(ARM_DEF_MRT(regs, ARMREG_SP, 0, 1, 0, 0, 1, ARMCOND_AL))

/* ldmia sp!, {regs} */
#define ARM_POP(p, regs) ARM_EMIT(p, ARM_DEF_MRT(regs, ARMREG_SP, 1, 1, 0, 1, 0, ARMCOND_AL))
#define ARM_IASM_POP(regs) ARM_IASM_EMIT(ARM_DEF_MRT(regs, ARMREG_SP, 1, 1, 0, 1, 0, ARMCOND_AL))

/* ldmia sp, {regs} ; (no write-back) */
#define ARM_POP_NWB(p, regs) ARM_EMIT(p, ARM_DEF_MRT(regs, ARMREG_SP, 1, 0, 0, 1, 0, ARMCOND_AL))
#define ARM_IASM_POP_NWB(regs) ARM_IASM_EMIT(ARM_DEF_MRT(regs, ARMREG_SP, 1, 0, 0, 1, 0, ARMCOND_AL))

#define ARM_PUSH1(p, r1) ARM_PUSH(p, (1 << r1))
#define ARM_PUSH2(p, r1, r2) ARM_PUSH(p, (1 << r1) | (1 << r2))
#define ARM_PUSH3(p, r1, r2, r3) ARM_PUSH(p, (1 << r1) | (1 << r2) | (1 << r3))
#define ARM_PUSH4(p, r1, r2, r3, r4) ARM_PUSH(p, (1 << r1) | (1 << r2) | (1 << r3) | (1 << r4))
#define ARM_PUSH5(p, r1, r2, r3, r4, r5) ARM_PUSH(p, (1 << r1) | (1 << r2) | (1 << r3) | (1 << r4) | (1 << r5))
#define ARM_PUSH6(p, r1, r2, r3, r4, r5, r6) ARM_PUSH(p, (1 << r1) | (1 << r2) | (1 << r3) | (1 << r4) | (1 << r5) | (1 << r6))
#define ARM_PUSH7(p, r1, r2, r3, r4, r5, r6, r7) ARM_PUSH(p, (1 << r1) | (1 << r2) | (1 << r3) | (1 << r4) | (1 << r5) | (1 << r6) | (1 << r7))
#define ARM_PUSH8(p, r1, r2, r3, r4, r5, r6, r7, r8) ARM_PUSH(p, (1 << r1) | (1 << r2) | (1 << r3) | (1 << r4) | (1 << r5) | (1 << r6) | (1 << r7) | (1 << r8))
#define ARM_PUSH9(p, r1, r2, r3, r4, r5, r6, r7, r8, r9) ARM_PUSH(p, (1 << r1) | (1 << r2) | (1 << r3) | (1 << r4) | (1 << r5) | (1 << r6) | (1 << r7) | (1 << r8) | (1 << r9))

#define ARM_POP9(p, r1, r2, r3, r4, r5, r6, r7, r8, r9) ARM_POP(p, (1 << r1) | (1 << r2) | (1 << r3) | (1 << r4) | (1 << r5) | (1 << r6) | (1 << r7) | (1 << r8) | (1 << r9))
#define ARM_POP8(p, r1, r2, r3, r4, r5, r6, r7, r8) ARM_POP(p, (1 << r1) | (1 << r2) | (1 << r3) | (1 << r4) | (1 << r5) | (1 << r6) | (1 << r7) | (1 << r8))
#define ARM_POP7(p, r1, r2, r3, r4, r5, r6, r7) ARM_POP(p, (1 << r1) | (1 << r2) | (1 << r3) | (1 << r4) | (1 << r5) | (1 << r6) | (1 << r7))
#define ARM_POP6(p, r1, r2, r3, r4, r5, r6) ARM_POP(p, (1 << r1) | (1 << r2) | (1 << r3) | (1 << r4) | (1 << r5) | (1 << r6))
#define ARM_POP5(p, r1, r2, r3, r4, r5) ARM_POP(p, (1 << r1) | (1 << r2) | (1 << r3) | (1 << r4) | (1 << r5))
#define ARM_POP4(p, r1, r2, r3, r4) ARM_POP(p, (1 << r1) | (1 << r2) | (1 << r3) | (1 << r4))
#define ARM_POP3(p, r1, r2, r3) ARM_POP(p, (1 << r1) | (1 << r2) | (1 << r3))
#define ARM_POP2(p, r1, r2) ARM_POP(p, (1 << r1) | (1 << r2))
#define ARM_POP1(p, r1) ARM_POP(p, (1 << r1))


/* Multiply instructions */
typedef struct {
  arminstr_t rm     : 4;
  arminstr_t tag2   : 4;   /* 9 */
  arminstr_t rs     : 4;
  arminstr_t rn     : 4;
  arminstr_t rd     : 4;
  arminstr_t s      : 1;
  arminstr_t opcode : 3;
  arminstr_t tag    : 4;
  arminstr_t cond   : 4;
} ARMInstrMul;

#define ARM_MUL_ID 0
#define ARM_MUL_ID2 9
#define ARM_MUL_MASK ((0xF << 24) | (0xF << 4))
#define ARM_MUL_TAG ((ARM_MUL_ID << 24) | (ARM_MUL_ID2 << 4))

#define ARM_DEF_MUL_COND(op, rd, rm, rs, rn, s, cond) \
  (rm)               | \
  ((rs) << 8)        | \
  ((rn) << 12)       | \
  ((rd) << 16)       | \
  (((s) & 1) << 20)  | \
  (((op) & 7) << 21) | \
  ARM_MUL_TAG        | \
  ARM_DEF_COND(cond)

/* Rd := (Rm * Rs)[31:0]; 32 x 32 -> 32 */
#define ARM_MUL_COND(p, rd, rm, rs, cond) \
  ARM_EMIT(p, ARM_DEF_MUL_COND(ARMOP_MUL, rd, rm, rs, 0, 0, cond))
#define ARM_MUL(p, rd, rm, rs) \
  ARM_MUL_COND(p, rd, rm, rs, ARMCOND_AL)
#define ARM_MULS_COND(p, rd, rm, rs, cond) \
  ARM_EMIT(p, ARM_DEF_MUL_COND(ARMOP_MUL, rd, rm, rs, 0, 1, cond))
#define ARM_MULS(p, rd, rm, rs) \
  ARM_MULS_COND(p, rd, rm, rs, ARMCOND_AL)
#define ARM_MUL_REG_REG(p, rd, rm, rs) ARM_MUL(p, rd, rm, rs)
#define ARM_MULS_REG_REG(p, rd, rm, rs) ARM_MULS(p, rd, rm, rs)

/* inline */
#define ARM_IASM_MUL_COND(rd, rm, rs, cond) \
  ARM_IASM_EMIT(ARM_DEF_MUL_COND(ARMOP_MUL, rd, rm, rs, 0, 0, cond))
#define ARM_IASM_MUL(rd, rm, rs) \
  ARM_IASM_MUL_COND(rd, rm, rs, ARMCOND_AL)
#define ARM_IASM_MULS_COND(rd, rm, rs, cond) \
  ARM_IASM_EMIT(ARM_DEF_MUL_COND(ARMOP_MUL, rd, rm, rs, 0, 1, cond))
#define ARM_IASM_MULS(rd, rm, rs) \
  ARM_IASM_MULS_COND(rd, rm, rs, ARMCOND_AL)


/* Rd := (Rm * Rs) + Rn; 32x32+32->32 */
#define ARM_MLA_COND(p, rd, rm, rs, rn, cond) \
  ARM_EMIT(p, ARM_DEF_MUL_COND(ARMOP_MLA, rd, rm, rs, rn, 0, cond))
#define ARM_MLA(p, rd, rm, rs, rn) \
  ARM_MLA_COND(p, rd, rm, rs, rn, ARMCOND_AL)
#define ARM_MLAS_COND(p, rd, rm, rs, rn, cond) \
  ARM_EMIT(p, ARM_DEF_MUL_COND(ARMOP_MLA, rd, rm, rs, rn, 1, cond))
#define ARM_MLAS(p, rd, rm, rs, rn) \
  ARM_MLAS_COND(p, rd, rm, rs, rn, ARMCOND_AL)

/* inline */
#define ARM_IASM_MLA_COND(rd, rm, rs, rn, cond) \
  ARM_IASM_EMIT(ARM_DEF_MUL_COND(ARMOP_MLA, rd, rm, rs, rn, 0, cond))
#define ARM_IASM_MLA(rd, rm, rs, rn) \
  ARM_IASM_MLA_COND(rd, rm, rs, rn, ARMCOND_AL)
#define ARM_IASM_MLAS_COND(rd, rm, rs, rn, cond) \
  ARM_IASM_EMIT(ARM_DEF_MUL_COND(ARMOP_MLA, rd, rm, rs, rn, 1, cond))
#define ARM_IASM_MLAS(rd, rm, rs, rn) \
  ARM_IASM_MLAS_COND(rd, rm, rs, rn, ARMCOND_AL)


#define ARM_SMULL_COND(p, rn, rd, rm, rs, cond) \
  ARM_EMIT(p, ARM_DEF_MUL_COND(ARMOP_SMULL, rd, rm, rs, rn, 0, cond))
#define ARM_SMULL(p, rn, rd, rm, rs) \
  ARM_SMULL_COND(p, rn, rd, rm, rs, ARMCOND_AL)

#define ARM_SMLAL_COND(p, rn, rd, rm, rs, cond) \
  ARM_EMIT(p, ARM_DEF_MUL_COND(ARMOP_SMLAL, rd, rm, rs, rn, 0, cond))
#define ARM_SMLAL(p, rn, rd, rm, rs) \
  ARM_SMLAL_COND(p, rn, rd, rm, rs, ARMCOND_AL)

#define ARM_UMULL_COND(p, rn, rd, rm, rs, cond) \
  ARM_EMIT(p, ARM_DEF_MUL_COND(ARMOP_UMULL, rd, rm, rs, rn, 0, cond))
#define ARM_UMULL(p, rn, rd, rm, rs) \
  ARM_UMULL_COND(p, rn, rd, rm, rs, ARMCOND_AL)

#define ARM_UMLAL_COND(p, rn, rd, rm, rs, cond) \
  ARM_EMIT(p, ARM_DEF_MUL_COND(ARMOP_UMLAL, rd, rm, rs, rn, 0, cond))
#define ARM_UMLAL(p, rn, rd, rm, rs) \
  ARM_UMLAL_COND(p, rn, rd, rm, rs, ARMCOND_AL)


#define ARM_SMULLS_COND(p, rn, rd, rm, rs, cond) \
  ARM_EMIT(p, ARM_DEF_MUL_COND(ARMOP_SMULL, rd, rm, rs, rn, 1, cond))
#define ARM_SMULLS(p, rn, rd, rm, rs) \
  ARM_SMULLS_COND(p, rn, rd, rm, rs, ARMCOND_AL)

#define ARM_SMLALS_COND(p, rn, rd, rm, rs, cond) \
  ARM_EMIT(p, ARM_DEF_MUL_COND(ARMOP_SMLAL, rd, rm, rs, rn, 1, cond))
#define ARM_SMLALS(p, rn, rd, rm, rs) \
  ARM_SMLALS_COND(p, rn, rd, rm, rs, ARMCOND_AL)

#define ARM_UMULLS_COND(p, rn, rd, rm, rs, cond) \
  ARM_EMIT(p, ARM_DEF_MUL_COND(ARMOP_UMULL, rd, rm, rs, rn, 1, cond))
#define ARM_UMULLS(p, rn, rd, rm, rs) \
  ARM_UMULLS_COND(p, rn, rd, rm, rs, ARMCOND_AL)

#define ARM_UMLALS_COND(p, rn, rd, rm, rs, cond) \
  ARM_EMIT(p, ARM_DEF_MUL_COND(ARMOP_UMLAL, rd, rm, rs, rn, 1, cond))
#define ARM_UMLALS(p, rn, rd, rm, rs) \
  ARM_UMLALS_COND(p, rn, rd, rm, rs, ARMCOND_AL)



/*  Word/byte transfer */
typedef union {
  ARMDPI_op2_reg_imm op2_reg_imm;
  struct {
    arminstr_t op2_imm : 12;
    arminstr_t rd      :  4;
    arminstr_t rn      :  4;
    arminstr_t ls      :  1;
    arminstr_t wb      :  1;
    arminstr_t b       :  1;
    arminstr_t u       :  1; /* down(0) / up(1) */
    arminstr_t p       :  1; /* post-index(0) / pre-index(1) */
    arminstr_t type    :  1; /* imm(0) / register(1) */
    arminstr_t tag     :  2; /* 0 1 */
    arminstr_t cond    :  4;
  } all;
} ARMInstrWXfer;

#define ARM_WXFER_ID 1
#define ARM_WXFER_MASK 3 << 26
#define ARM_WXFER_TAG ARM_WXFER_ID << 26


/*
 * ls    :  opcode, ARMOP_STR(0)/ARMOP_LDR(1)
 * imm12 :  immediate offset
 * wb    :  write-back
 * p     :  index mode, post-index (0, automatic write-back)
 *        or pre-index (1, calc effective address before memory access)
 */
#define ARM_DEF_WXFER_IMM(imm12, rd, rn, ls, wb, b, p, cond) \
  ((((int)(imm12)) < 0) ? -((int)(imm12)) : (imm12)) | \
  ((rd) << 12)                                   | \
  ((rn) << 16)                                   | \
  ((ls) << 20)                                   | \
  ((wb) << 21)                                   | \
  ((b)  << 22)                                   | \
  (((int)(imm12) >= 0) << 23)                    | \
  ((p) << 24)                                    | \
  ARM_WXFER_TAG                                  | \
  ARM_DEF_COND(cond)

#define ARM_WXFER_MAX_OFFS 0xFFF

/* this macro checks for imm12 bounds */
#define ARM_EMIT_WXFER_IMM(ptr, imm12, rd, rn, ls, wb, b, p, cond) \
  do { \
    int _imm12 = (int)(imm12) < -ARM_WXFER_MAX_OFFS  \
                 ? -ARM_WXFER_MAX_OFFS               \
                 : (int)(imm12) > ARM_WXFER_MAX_OFFS \
                 ? ARM_WXFER_MAX_OFFS                \
                 : (int)(imm12);                     \
    ARM_EMIT((ptr), \
    ARM_DEF_WXFER_IMM(_imm12, (rd), (rn), (ls), (wb), (b), (p), (cond))); \
  } while (0)


/* LDRx */
/* immediate offset, post-index */
#define ARM_LDR_IMM_POST_COND(p, rd, rn, imm, cond) \
  ARM_EMIT(p, ARM_DEF_WXFER_IMM(imm, rd, rn, ARMOP_LDR, 0, 0, 0, cond))

#define ARM_LDR_IMM_POST(p, rd, rn, imm) ARM_LDR_IMM_POST_COND(p, rd, rn, imm, ARMCOND_AL)

#define ARM_LDRB_IMM_POST_COND(p, rd, rn, imm, cond) \
  ARM_EMIT(p, ARM_DEF_WXFER_IMM(imm, rd, rn, ARMOP_LDR, 0, 1, 0, cond))

#define ARM_LDRB_IMM_POST(p, rd, rn, imm) ARM_LDRB_IMM_POST_COND(p, rd, rn, imm, ARMCOND_AL)

/* immediate offset, pre-index */
#define ARM_LDR_IMM_COND(p, rd, rn, imm, cond) \
  ARM_EMIT(p, ARM_DEF_WXFER_IMM(imm, rd, rn, ARMOP_LDR, 0, 0, 1, cond))

#define ARM_LDR_IMM(p, rd, rn, imm) ARM_LDR_IMM_COND(p, rd, rn, imm, ARMCOND_AL)

#define ARM_LDRB_IMM_COND(p, rd, rn, imm, cond) \
  ARM_EMIT(p, ARM_DEF_WXFER_IMM(imm, rd, rn, ARMOP_LDR, 0, 1, 1, cond))

#define ARM_LDRB_IMM(p, rd, rn, imm) ARM_LDRB_IMM_COND(p, rd, rn, imm, ARMCOND_AL)


/* STRx */
/* immediate offset, post-index */
#define ARM_STR_IMM_POST_COND(p, rd, rn, imm, cond) \
  ARM_EMIT(p, ARM_DEF_WXFER_IMM(imm, rd, rn, ARMOP_STR, 0, 0, 0, cond))

#define ARM_STR_IMM_POST(p, rd, rn, imm) ARM_STR_IMM_POST_COND(p, rd, rn, imm, ARMCOND_AL)

#define ARM_STRB_IMM_POST_COND(p, rd, rn, imm, cond) \
  ARM_EMIT(p, ARM_DEF_WXFER_IMM(imm, rd, rn, ARMOP_STR, 0, 1, 0, cond))

#define ARM_STRB_IMM_POST(p, rd, rn, imm) ARM_STRB_IMM_POST_COND(p, rd, rn, imm, ARMCOND_AL)

/* immediate offset, pre-index */
#define ARM_STR_IMM_COND(p, rd, rn, imm, cond) \
  ARM_EMIT_WXFER_IMM(p, imm, rd, rn, ARMOP_STR, 0, 0, 1, cond)
/*  ARM_EMIT(p, ARM_DEF_WXFER_IMM(imm, rd, rn, ARMOP_STR, 0, 0, 1, cond))*/
/*  ARM_EMIT_WXFER_IMM(p, imm, rd, rn, ARMOP_STR, 0, 0, 1, cond) */
/*  ARM_EMIT(p, ARM_DEF_WXFER_IMM(imm, rd, rn, ARMOP_STR, 0, 0, 1, cond)) */

#define ARM_STR_IMM(p, rd, rn, imm) ARM_STR_IMM_COND(p, rd, rn, imm, ARMCOND_AL)

#define ARM_STRB_IMM_COND(p, rd, rn, imm, cond) \
  ARM_EMIT(p, ARM_DEF_WXFER_IMM(imm, rd, rn, ARMOP_STR, 0, 1, 1, cond))

#define ARM_STRB_IMM(p, rd, rn, imm) ARM_STRB_IMM_COND(p, rd, rn, imm, ARMCOND_AL)

/* write-back */
#define ARM_STR_IMM_WB_COND(p, rd, rn, imm, cond) \
  ARM_EMIT_WXFER_IMM(p, imm, rd, rn, ARMOP_STR, 1, 0, 1, cond)
#define ARM_STR_IMM_WB(p, rd, rn, imm) ARM_STR_IMM_WB_COND(p, rd, rn, imm, ARMCOND_AL)


/*
 * wb    :  write-back
 * u     :  down(0) / up(1)
 * p     :  index mode, post-index (0, automatic write-back) or pre-index (1)
 */
#define ARM_DEF_WXFER_REG_REG_UPDOWN_COND(rm, shift_type, shift, rd, rn, ls, wb, b, u, p, cond) \
  (rm)                | \
  ((shift_type) << 5) | \
  ((shift) << 7)      | \
  ((rd) << 12)        | \
  ((rn) << 16)        | \
  ((ls) << 20)        | \
  ((wb) << 21)        | \
  ((b)  << 22)        | \
  ((u)  << 23)        | \
  ((p)  << 24)        | \
  (1    << 25)        | \
  ARM_WXFER_TAG       | \
  ARM_DEF_COND(cond)

#define ARM_DEF_WXFER_REG_REG_COND(rm, shift_type, shift, rd, rn, ls, wb, b, p, cond) \
  ARM_DEF_WXFER_REG_REG_UPDOWN_COND(rm, shift_type, shift, rd, rn, ls, wb, b, ARM_UP, p, cond)
#define ARM_DEF_WXFER_REG_MINUS_REG_COND(rm, shift_type, shift, rd, rn, ls, wb, b, p, cond) \
  ARM_DEF_WXFER_REG_REG_UPDOWN_COND(rm, shift_type, shift, rd, rn, ls, wb, b, ARM_DOWN, p, cond)


#define ARM_LDR_REG_REG_SHIFT_COND(p, rd, rn, rm, shift_type, shift, cond) \
  ARM_EMIT(p, ARM_DEF_WXFER_REG_REG_COND(rm, shift_type, shift, rd, rn, ARMOP_LDR, 0, 0, 1, cond))
#define ARM_LDR_REG_REG_SHIFT(p, rd, rn, rm, shift_type, shift) \
  ARM_LDR_REG_REG_SHIFT_COND(p, rd, rn, rm, shift_type, shift, ARMCOND_AL)
#define ARM_LDR_REG_REG(p, rd, rn, rm) \
  ARM_LDR_REG_REG_SHIFT(p, rd, rn, rm, ARMSHIFT_LSL, 0)

#define ARM_LDRB_REG_REG_SHIFT_COND(p, rd, rn, rm, shift_type, shift, cond) \
  ARM_EMIT(p, ARM_DEF_WXFER_REG_REG_COND(rm, shift_type, shift, rd, rn, ARMOP_LDR, 0, 1, 1, cond))
#define ARM_LDRB_REG_REG_SHIFT(p, rd, rn, rm, shift_type, shift) \
  ARM_LDRB_REG_REG_SHIFT_COND(p, rd, rn, rm, shift_type, shift, ARMCOND_AL)
#define ARM_LDRB_REG_REG(p, rd, rn, rm) \
  ARM_LDRB_REG_REG_SHIFT(p, rd, rn, rm, ARMSHIFT_LSL, 0)

#define ARM_STR_REG_REG_SHIFT_COND(p, rd, rn, rm, shift_type, shift, cond) \
  ARM_EMIT(p, ARM_DEF_WXFER_REG_REG_COND(rm, shift_type, shift, rd, rn, ARMOP_STR, 0, 0, 1, cond))
#define ARM_STR_REG_REG_SHIFT(p, rd, rn, rm, shift_type, shift) \
  ARM_STR_REG_REG_SHIFT_COND(p, rd, rn, rm, shift_type, shift, ARMCOND_AL)
#define ARM_STR_REG_REG(p, rd, rn, rm) \
  ARM_STR_REG_REG_SHIFT(p, rd, rn, rm, ARMSHIFT_LSL, 0)

/* post-index */
#define ARM_STR_REG_REG_SHIFT_POST_COND(p, rd, rn, rm, shift_type, shift, cond) \
  ARM_EMIT(p, ARM_DEF_WXFER_REG_REG_COND(rm, shift_type, shift, rd, rn, ARMOP_STR, 0, 0, 0, cond))
#define ARM_STR_REG_REG_SHIFT_POST(p, rd, rn, rm, shift_type, shift) \
  ARM_STR_REG_REG_SHIFT_POST_COND(p, rd, rn, rm, shift_type, shift, ARMCOND_AL)
#define ARM_STR_REG_REG_POST(p, rd, rn, rm) \
  ARM_STR_REG_REG_SHIFT_POST(p, rd, rn, rm, ARMSHIFT_LSL, 0)

/* zero-extend */
#define ARM_STRB_REG_REG_SHIFT_COND(p, rd, rn, rm, shift_type, shift, cond) \
  ARM_EMIT(p, ARM_DEF_WXFER_REG_REG_COND(rm, shift_type, shift, rd, rn, ARMOP_STR, 0, 1, 1, cond))
#define ARM_STRB_REG_REG_SHIFT(p, rd, rn, rm, shift_type, shift) \
  ARM_STRB_REG_REG_SHIFT_COND(p, rd, rn, rm, shift_type, shift, ARMCOND_AL)
#define ARM_STRB_REG_REG(p, rd, rn, rm) \
  ARM_STRB_REG_REG_SHIFT(p, rd, rn, rm, ARMSHIFT_LSL, 0)


/* ARMv4+ */
/* Half-word or byte (signed) transfer. */
typedef struct {
  arminstr_t rm     : 4; /* imm_lo */
  arminstr_t tag3   : 1; /* 1 */
  arminstr_t h      : 1; /* half-word or byte */
  arminstr_t s      : 1; /* sign-extend or zero-extend */
  arminstr_t tag2   : 1; /* 1 */
  arminstr_t imm_hi : 4;
  arminstr_t rd     : 4;
  arminstr_t rn     : 4;
  arminstr_t ls     : 1;
  arminstr_t wb     : 1;
  arminstr_t type   : 1; /* imm(1) / reg(0) */
  arminstr_t u      : 1; /* +- */
  arminstr_t p      : 1; /* pre/post-index */
  arminstr_t tag    : 3;
  arminstr_t cond   : 4;
} ARMInstrHXfer;

#define ARM_HXFER_ID 0
#define ARM_HXFER_ID2 1
#define ARM_HXFER_ID3 1
#define ARM_HXFER_MASK ((0x7 << 25) | (0x9 << 4))
#define ARM_HXFER_TAG ((ARM_HXFER_ID << 25) | (ARM_HXFER_ID2 << 7) | (ARM_HXFER_ID3 << 4))

#define ARM_DEF_HXFER_IMM_COND(imm, h, s, rd, rn, ls, wb, p, cond) \
  (((int)(imm) >= 0 ? (imm) : -(int)(imm)) & 0xF)               | \
  ((h) << 5)                                                    | \
  ((s) << 6)                                                    | \
  ((((int)(imm) >= 0 ? (imm) : -(int)(imm)) << 4) & (0xF << 8)) | \
  ((rd) << 12)                                                  | \
  ((rn) << 16)                                                  | \
  ((ls) << 20)                                                  | \
  ((wb) << 21)                                                  | \
  (1 << 22)                                                     | \
  (((int)(imm) >= 0) << 23)                                     | \
  ((p) << 24)                                                   | \
  ARM_HXFER_TAG                                                 | \
  ARM_DEF_COND(cond)

#define ARM_LDRH_IMM_COND(p, rd, rn, imm, cond) \
  ARM_EMIT(p, ARM_DEF_HXFER_IMM_COND(imm, 1, 0, rd, rn, ARMOP_LDR, 0, 1, cond))
#define ARM_LDRH_IMM(p, rd, rn, imm) \
  ARM_LDRH_IMM_COND(p, rd, rn, imm, ARMCOND_AL)
#define ARM_LDRSH_IMM_COND(p, rd, rn, imm, cond) \
  ARM_EMIT(p, ARM_DEF_HXFER_IMM_COND(imm, 1, 1, rd, rn, ARMOP_LDR, 0, 1, cond))
#define ARM_LDRSH_IMM(p, rd, rn, imm) \
  ARM_LDRSH_IMM_COND(p, rd, rn, imm, ARMCOND_AL)
#define ARM_LDRSB_IMM_COND(p, rd, rn, imm, cond) \
  ARM_EMIT(p, ARM_DEF_HXFER_IMM_COND(imm, 0, 1, rd, rn, ARMOP_LDR, 0, 1, cond))
#define ARM_LDRSB_IMM(p, rd, rn, imm) \
  ARM_LDRSB_IMM_COND(p, rd, rn, imm, ARMCOND_AL)


#define ARM_STRH_IMM_COND(p, rd, rn, imm, cond) \
  ARM_EMIT(p, ARM_DEF_HXFER_IMM_COND(imm, 1, 0, rd, rn, ARMOP_STR, 0, 1, cond))
#define ARM_STRH_IMM(p, rd, rn, imm) \
  ARM_STRH_IMM_COND(p, rd, rn, imm, ARMCOND_AL)

#define ARM_STRH_IMM_POST_COND(p, rd, rn, imm, cond) \
  ARM_EMIT(p, ARM_DEF_HXFER_IMM_COND(imm, 1, 0, rd, rn, ARMOP_STR, 0, 0, cond))
#define ARM_STRH_IMM_POST(p, rd, rn, imm) \
  ARM_STRH_IMM_POST_COND(p, rd, rn, imm, ARMCOND_AL)


#define ARM_DEF_HXFER_REG_REG_UPDOWN_COND(rm, h, s, rd, rn, ls, wb, u, p, cond) \
  ((rm) & 0xF)                | \
  ((h) << 5)                  | \
  ((s) << 6)                  | \
  ((rd) << 12)                | \
  ((rn) << 16)                | \
  ((ls) << 20)                | \
  ((wb) << 21)                | \
  (0 << 22)                   | \
  ((u) << 23)                 | \
  ((p) << 24)                 | \
  ARM_HXFER_TAG               | \
  ARM_DEF_COND(cond)

#define ARM_DEF_HXFER_REG_REG_COND(rm, h, s, rd, rn, ls, wb, p, cond) \
  ARM_DEF_HXFER_REG_REG_UPDOWN_COND(rm, h, s, rd, rn, ls, wb, ARM_UP, p, cond)
#define ARM_DEF_HXFER_REG_MINUS_REG_COND(rm, h, s, rd, rn, ls, wb, p, cond) \
  ARM_DEF_HXFER_REG_REG_UPDOWN_COND(rm, h, s, rd, rn, ls, wb, ARM_DOWN, p, cond)

#define ARM_LDRH_REG_REG_COND(p, rd, rn, rm, cond) \
  ARM_EMIT(p, ARM_DEF_HXFER_REG_REG_COND(rm, 1, 0, rd, rn, ARMOP_LDR, 0, 1, cond))
#define ARM_LDRH_REG_REG(p, rd, rn, rm) \
  ARM_LDRH_REG_REG_COND(p, rd, rn, rm, ARMCOND_AL)
#define ARM_LDRSH_REG_REG_COND(p, rd, rn, rm, cond) \
  ARM_EMIT(p, ARM_DEF_HXFER_REG_REG_COND(rm, 1, 1, rd, rn, ARMOP_LDR, 0, 1, cond))
#define ARM_LDRSH_REG_REG(p, rd, rn, rm) \
  ARM_LDRSH_REG_REG_COND(p, rd, rn, rm, ARMCOND_AL)
#define ARM_LDRSB_REG_REG_COND(p, rd, rn, rm, cond) \
  ARM_EMIT(p, ARM_DEF_HXFER_REG_REG_COND(rm, 0, 1, rd, rn, ARMOP_LDR, 0, 1, cond))
#define ARM_LDRSB_REG_REG(p, rd, rn, rm) ARM_LDRSB_REG_REG_COND(p, rd, rn, rm, ARMCOND_AL)

#define ARM_STRH_REG_REG_COND(p, rd, rn, rm, cond) \
  ARM_EMIT(p, ARM_DEF_HXFER_REG_REG_COND(rm, 1, 0, rd, rn, ARMOP_STR, 0, 1, cond))
#define ARM_STRH_REG_REG(p, rd, rn, rm) \
  ARM_STRH_REG_REG_COND(p, rd, rn, rm, ARMCOND_AL)

#define ARM_STRH_REG_REG_POST_COND(p, rd, rn, rm, cond) \
  ARM_EMIT(p, ARM_DEF_HXFER_REG_REG_COND(rm, 1, 0, rd, rn, ARMOP_STR, 0, 0, cond))
#define ARM_STRH_REG_REG_POST(p, rd, rn, rm) \
  ARM_STRH_REG_REG_POST_COND(p, rd, rn, rm, ARMCOND_AL)



/* Swap */
typedef struct {
  arminstr_t rm   : 4;
  arminstr_t tag3 : 8; /* 0x9 */
  arminstr_t rd   : 4;
  arminstr_t rn   : 4;
  arminstr_t tag2 : 2;
  arminstr_t b    : 1;
  arminstr_t tag  : 5; /* 0x2 */
  arminstr_t cond : 4;
} ARMInstrSwap;

#define ARM_SWP_ID 2
#define ARM_SWP_ID2 9
#define ARM_SWP_MASK ((0x1F << 23) | (3 << 20) | (0xFF << 4))
#define ARM_SWP_TAG ((ARM_SWP_ID << 23) | (ARM_SWP_ID2 << 4))



/* Software interrupt */
typedef struct {
  arminstr_t num  : 24;
  arminstr_t tag  :  4;
  arminstr_t cond :  4;
} ARMInstrSWI;

#define ARM_SWI_ID 0xF
#define ARM_SWI_MASK (0xF << 24)
#define ARM_SWI_TAG (ARM_SWI_ID << 24)



/* Co-processor Data Processing */
typedef struct {
  arminstr_t crm  : 4;
  arminstr_t tag2 : 1; /* 0 */
  arminstr_t op2  : 3;
  arminstr_t cpn  : 4; /* CP number */
  arminstr_t crd  : 4;
  arminstr_t crn  : 4;
  arminstr_t op   : 4;
  arminstr_t tag  : 4; /* 0xE */
  arminstr_t cond : 4;
} ARMInstrCDP;

#define ARM_CDP_ID 0xE
#define ARM_CDP_ID2 0
#define ARM_CDP_MASK ((0xF << 24) | (1 << 4))
#define ARM_CDP_TAG ((ARM_CDP_ID << 24) | (ARM_CDP_ID2 << 4))


/* Co-processor Data Transfer (ldc/stc) */
typedef struct {
  arminstr_t offs : 8;
  arminstr_t cpn  : 4;
  arminstr_t crd  : 4;
  arminstr_t rn   : 4;
  arminstr_t ls   : 1;
  arminstr_t wb   : 1;
  arminstr_t n    : 1;
  arminstr_t u    : 1;
  arminstr_t p    : 1;
  arminstr_t tag  : 3;
  arminstr_t cond : 4;
} ARMInstrCDT;

#define ARM_CDT_ID 6
#define ARM_CDT_MASK (7 << 25)
#define ARM_CDT_TAG (ARM_CDT_ID << 25)


/* Co-processor Register Transfer (mcr/mrc) */
typedef struct {
  arminstr_t crm  : 4;
  arminstr_t tag2 : 1;
  arminstr_t op2  : 3;
  arminstr_t cpn  : 4;
  arminstr_t rd   : 4;
  arminstr_t crn  : 4;
  arminstr_t ls   : 1;
  arminstr_t op1  : 3;
  arminstr_t tag  : 4;
  arminstr_t cond : 4;
} ARMInstrCRT;

#define ARM_CRT_ID 0xE
#define ARM_CRT_ID2 0x1
#define ARM_CRT_MASK ((0xF << 24) | (1 << 4))
#define ARM_CRT_TAG ((ARM_CRT_ID << 24) | (ARM_CRT_ID2 << 4))

/*
 * Move from co-processor register to CPU register
 * Rd := cRn {<op>cRm}
 * op{condition} CP#,CPOp,Rd,CRn,CRm{,CPOp2}
 */
#define ARM_DEF_MRC_COND(cpn, cpop, rd, crn, crm, cpop2, cond) \
  ((crm) & 0xF)       |\
  ((cpop2) << 5)      |\
  ((cpn) << 8)        |\
  ((rd) << 12)        |\
  ((crn) << 16)       |\
  ((ARMOP_LDR) << 20) |\
  ((cpop) << 21)      |\
  ARM_CRT_TAG         |\
  ARM_DEF_COND(cond)

#define ARM_MRC_COND(p, cpn, cpop, rd, crn, crm, cpop2, cond) \
  ARM_EMIT(p, ARM_DEF_MRC_COND(cpn, cpop, rd, crn, crm, cpop2, cond))
#define ARM_MRC(p, cpn, cpop, rd, crn, crm, cpop2) \
  ARM_MRC_COND(p, cpn, cpop, rd, crn, crm, cpop2, ARMCOND_AL)



/* Move register to PSR. */
typedef union {
  ARMDPI_op2_imm op2_imm;
  struct {
    arminstr_t rm   : 4;
    arminstr_t pad  : 8; /* 0 */
    arminstr_t tag4 : 4; /* 0xF */
    arminstr_t fld  : 4;
    arminstr_t tag3 : 2; /* 0x2 */
    arminstr_t sel  : 1;
    arminstr_t tag2 : 2; /* 0x2 */
    arminstr_t type : 1;
    arminstr_t tag  : 2; /* 0 */
    arminstr_t cond : 4;
  } all;
} ARMInstrMSR;

#define ARM_MSR_ID 0
#define ARM_MSR_ID2 2
#define ARM_MSR_ID3 2
#define ARM_MSR_ID4 0xF
#define ARM_MSR_MASK ((3 << 26) | \
                      (3 << 23) | \
                      (3 << 20) | \
                      (0xF << 12))
#define ARM_MSR_TAG ((ARM_MSR_ID << 26)  | \
                     (ARM_MSR_ID2 << 23) | \
                     (ARM_MSR_ID3 << 20) | \
                     (ARM_MSR_ID4 << 12))

#define ARM_DEF_MSR_REG_COND(mask, rm, r, cond) \
    ARM_MSR_TAG       | \
    ARM_DEF_COND(cond)    | \
    ((rm) & 0xf)      | \
    (((r) & 1) << 22)   | \
    (((mask) & 0xf) << 16)

#define ARM_MSR_REG_COND(p, mask, rm, r, cond) \
  ARM_EMIT(p, ARM_DEF_MSR_REG_COND(mask, rm, r, cond))

#define ARM_MSR_REG(p, mask, rm, r) \
  ARM_MSR_REG_COND(p, mask, rm, r, ARMCOND_AL)

#define ARM_PSR_C 1
#define ARM_PSR_X 2
#define ARM_PSR_S 4
#define ARM_PSR_F 8

#define ARM_CPSR 0
#define ARM_SPSR 1

/* Move PSR to register. */
typedef struct {
  arminstr_t tag3 : 12;
  arminstr_t rd   :  4;
  arminstr_t tag2 :  6;
  arminstr_t sel  :  1; /* CPSR | SPSR */
  arminstr_t tag  :  5;
  arminstr_t cond :  4;
} ARMInstrMRS;

#define ARM_MRS_ID 2
#define ARM_MRS_ID2 0xF
#define ARM_MRS_ID3 0
#define ARM_MRS_MASK ((0x1F << 23) | (0x3F << 16) | 0xFFF)
#define ARM_MRS_TAG ((ARM_MRS_ID << 23) | (ARM_MRS_ID2 << 16) | ARM_MRS_ID3)

#define ARM_DEF_MRS_COND(rd, r, cond) \
  ARM_MRS_TAG       | \
  ARM_DEF_COND(cond)    | \
  (((r) & 1) << 22)   | \
  ((rd)& 0xf) << 12

#define ARM_MRS_COND(p, rd, r, cond) \
  ARM_EMIT(p, ARM_DEF_MRS_COND(rd, r, cond))

#define ARM_MRS_CPSR_COND(p, rd, cond) \
  ARM_MRS_COND(p, rd, ARM_CPSR, cond)

#define ARM_MRS_CPSR(p, rd) \
  ARM_MRS_CPSR_COND(p, rd, ARMCOND_AL)

#define ARM_MRS_SPSR_COND(p, rd, cond) \
  ARM_MRS_COND(p, rd, ARM_SPSR, cond)

#define ARM_MRS_SPSR(p, rd) \
  ARM_MRS_SPSR_COND(p, rd, ARMCOND_AL)


#include "arm_dpimacros.h"

#define ARM_NOP(p) ARM_MOV_REG_REG(p, ARMREG_R0, ARMREG_R0)


#define ARM_SHL_IMM_COND(p, rd, rm, imm, cond) \
  ARM_MOV_REG_IMMSHIFT_COND(p, rd, rm, ARMSHIFT_LSL, imm, cond)
#define ARM_SHL_IMM(p, rd, rm, imm) \
  ARM_SHL_IMM_COND(p, rd, rm, imm, ARMCOND_AL)
#define ARM_SHLS_IMM_COND(p, rd, rm, imm, cond) \
  ARM_MOVS_REG_IMMSHIFT_COND(p, rd, rm, ARMSHIFT_LSL, imm, cond)
#define ARM_SHLS_IMM(p, rd, rm, imm) \
  ARM_SHLS_IMM_COND(p, rd, rm, imm, ARMCOND_AL)

#define ARM_SHR_IMM_COND(p, rd, rm, imm, cond) \
  ARM_MOV_REG_IMMSHIFT_COND(p, rd, rm, ARMSHIFT_LSR, imm, cond)
#define ARM_SHR_IMM(p, rd, rm, imm) \
  ARM_SHR_IMM_COND(p, rd, rm, imm, ARMCOND_AL)
#define ARM_SHRS_IMM_COND(p, rd, rm, imm, cond) \
  ARM_MOVS_REG_IMMSHIFT_COND(p, rd, rm, ARMSHIFT_LSR, imm, cond)
#define ARM_SHRS_IMM(p, rd, rm, imm) \
  ARM_SHRS_IMM_COND(p, rd, rm, imm, ARMCOND_AL)

#define ARM_SAR_IMM_COND(p, rd, rm, imm, cond) \
  ARM_MOV_REG_IMMSHIFT_COND(p, rd, rm, ARMSHIFT_ASR, imm, cond)
#define ARM_SAR_IMM(p, rd, rm, imm) \
  ARM_SAR_IMM_COND(p, rd, rm, imm, ARMCOND_AL)
#define ARM_SARS_IMM_COND(p, rd, rm, imm, cond) \
  ARM_MOVS_REG_IMMSHIFT_COND(p, rd, rm, ARMSHIFT_ASR, imm, cond)
#define ARM_SARS_IMM(p, rd, rm, imm) \
  ARM_SARS_IMM_COND(p, rd, rm, imm, ARMCOND_AL)

#define ARM_ROR_IMM_COND(p, rd, rm, imm, cond) \
  ARM_MOV_REG_IMMSHIFT_COND(p, rd, rm, ARMSHIFT_ROR, imm, cond)
#define ARM_ROR_IMM(p, rd, rm, imm) \
  ARM_ROR_IMM_COND(p, rd, rm, imm, ARMCOND_AL)
#define ARM_RORS_IMM_COND(p, rd, rm, imm, cond) \
  ARM_MOVS_REG_IMMSHIFT_COND(p, rd, rm, ARMSHIFT_ROR, imm, cond)
#define ARM_RORS_IMM(p, rd, rm, imm) \
  ARM_RORS_IMM_COND(p, rd, rm, imm, ARMCOND_AL)

#define ARM_SHL_REG_COND(p, rd, rm, rs, cond) \
  ARM_MOV_REG_REGSHIFT_COND(p, rd, rm, ARMSHIFT_LSL, rs, cond)
#define ARM_SHL_REG(p, rd, rm, rs) \
  ARM_SHL_REG_COND(p, rd, rm, rs, ARMCOND_AL)
#define ARM_SHLS_REG_COND(p, rd, rm, rs, cond) \
  ARM_MOVS_REG_REGSHIFT_COND(p, rd, rm, ARMSHIFT_LSL, rs, cond)
#define ARM_SHLS_REG(p, rd, rm, rs) \
  ARM_SHLS_REG_COND(p, rd, rm, rs, ARMCOND_AL)
#define ARM_SHLS_REG_REG(p, rd, rm, rs) ARM_SHLS_REG(p, rd, rm, rs)

#define ARM_SHR_REG_COND(p, rd, rm, rs, cond) \
  ARM_MOV_REG_REGSHIFT_COND(p, rd, rm, ARMSHIFT_LSR, rs, cond)
#define ARM_SHR_REG(p, rd, rm, rs) \
  ARM_SHR_REG_COND(p, rd, rm, rs, ARMCOND_AL)
#define ARM_SHRS_REG_COND(p, rd, rm, rs, cond) \
  ARM_MOVS_REG_REGSHIFT_COND(p, rd, rm, ARMSHIFT_LSR, rs, cond)
#define ARM_SHRS_REG(p, rd, rm, rs) \
  ARM_SHRS_REG_COND(p, rd, rm, rs, ARMCOND_AL)
#define ARM_SHRS_REG_REG(p, rd, rm, rs) ARM_SHRS_REG(p, rd, rm, rs)

#define ARM_SAR_REG_COND(p, rd, rm, rs, cond) \
  ARM_MOV_REG_REGSHIFT_COND(p, rd, rm, ARMSHIFT_ASR, rs, cond)
#define ARM_SAR_REG(p, rd, rm, rs) \
  ARM_SAR_REG_COND(p, rd, rm, rs, ARMCOND_AL)
#define ARM_SARS_REG_COND(p, rd, rm, rs, cond) \
  ARM_MOVS_REG_REGSHIFT_COND(p, rd, rm, ARMSHIFT_ASR, rs, cond)
#define ARM_SARS_REG(p, rd, rm, rs) \
  ARM_SARS_REG_COND(p, rd, rm, rs, ARMCOND_AL)
#define ARM_SARS_REG_REG(p, rd, rm, rs) ARM_SARS_REG(p, rd, rm, rs)

#define ARM_ROR_REG_COND(p, rd, rm, rs, cond) \
  ARM_MOV_REG_REGSHIFT_COND(p, rd, rm, ARMSHIFT_ROR, rs, cond)
#define ARM_ROR_REG(p, rd, rm, rs) \
  ARM_ROR_REG_COND(p, rd, rm, rs, ARMCOND_AL)
#define ARM_RORS_REG_COND(p, rd, rm, rs, cond) \
  ARM_MOVS_REG_REGSHIFT_COND(p, rd, rm, ARMSHIFT_ROR, rs, cond)
#define ARM_RORS_REG(p, rd, rm, rs) \
  ARM_RORS_REG_COND(p, rd, rm, rs, ARMCOND_AL)
#define ARM_RORS_REG_REG(p, rd, rm, rs) ARM_RORS_REG(p, rd, rm, rs)

#define ARM_DBRK(p) ARM_EMIT(p, 0xE6000010)
#define ARM_IASM_DBRK() ARM_IASM_EMIT(0xE6000010)

#define ARM_INC(p, reg) ARM_ADD_REG_IMM8(p, reg, reg, 1)
#define ARM_DEC(p, reg) ARM_SUB_REG_IMM8(p, reg, reg, 1)


/* ARM V5 */

/* Count leading zeros, CLZ{cond} Rd, Rm */
typedef struct {
  arminstr_t rm   :  4;
  arminstr_t tag2 :  8;
  arminstr_t rd   :  4;
  arminstr_t tag  :  12;
  arminstr_t cond :  4;
} ARMInstrCLZ;

#define ARM_CLZ_ID 0x16F
#define ARM_CLZ_ID2 0xF1
#define ARM_CLZ_MASK ((0xFFF << 16) | (0xFF < 4))
#define ARM_CLZ_TAG ((ARM_CLZ_ID << 16) | (ARM_CLZ_ID2 << 4))

#define ARM_DEF_CLZ_COND(rd, rm, cond) \
  ARM_CLZ_TAG       | \
  ARM_DEF_COND(cond)    | \
  (((rm) & 0xf))      | \
  ((rd) & 0xf) << 12

#define ARM_CLZ_COND(p, rd, rm, cond) \
  ARM_EMIT(p, ARM_DEF_CLZ_COND(rd, rm, cond))

#define ARM_CLZ(p, rd, rm) \
  ARM_EMIT(p, ARM_DEF_CLZ_COND(rd, rm, ARMCOND_AL))

/*
 *              TAG     p         b   wb  ls
 * ARMCOND_NV | 0-1-0 | 0 | +/- | 1 | 0 | 1 | rn -|- 0xF | imm12
 */
#define ARM_PLD_ID 0xF45
#define ARM_PLD_ID2 0xF /* rd */
#define ARM_PLD_MASK ((0xFC7 << 20) | (0xF << 12))
#define ARM_PLD_TAG ((ARM_PLD_ID << 20) | (ARM_PLD_ID2 << 12))
#define ARM_DEF_PLD_IMM(imm12, rn) \
  ((((int)imm12) < 0) ? -(int)(imm12) : (imm12)) | \
  ((0xF) << 12)                                  | \
  ((rn) << 16)                                   | \
  ((1) << 20)  /* ls = load(1) */                | \
  ((0) << 21)  /* wb = 0 */                      | \
  ((1)  << 22) /* b = 1 */                       | \
  (((int)(imm12) >= 0) << 23)                    | \
  ((1) << 24)  /* pre/post = pre(1) */           | \
  ((2) << 25)  /* tag */                         | \
  ARM_DEF_COND(ARMCOND_NV)

#define ARM_PLD_IMM(p, rn, imm12) ARM_EMIT(p, ARM_DEF_PLD_IMM(imm12, rn))

#define ARM_DEF_PLD_REG_REG_UPDOWN_SHIFT(rn, shift_type, shift, rm, u) \
  (rm)                            | \
  ((shift_type) << 5)             | \
  ((shift) << 7)                  | \
  (0xF << 12) /* rd = 0xF */      | \
  ((rn) << 16)                    | \
  (1    << 20) /* ls = load(1) */ | \
  (0    << 21) /* wb = 0 */       | \
  (1    << 22) /* b = 1 */        | \
  ((u)  << 23)                    | \
  (1  << 24)   /* pre(1) */       | \
  (3    << 25)                    | \
  ARM_DEF_COND(ARMCOND_NV)

#define ARM_PLD_REG_REG_UPDOWN_SHIFT(p, rm, rn, u, shift_type, shift) \
  ARM_EMIT(p,  ARM_DEF_PLD_REG_REG_UPDOWN_SHIFT(rm, shift_type, shift, rn, u))

#define ARM_PLD_REG_PLUS_REG(p, rm, rn) \
  ARM_PLD_REG_REG_UPDOWN_SHIFT(p, rm, rn, ARM_UP, ARMSHIFT_LSL, 0)

#define ARM_PLD_REG_MINUS_REG(p, rm, rn) \
  ARM_PLD_REG_REG_UPDOWN_SHIFT(p, rm, rn, ARM_DOWN, ARMSHIFT_LSL, 0)


#define ARM_DEF_STF_IMM_COND(p, prec, freg_const, rd, imm8, rot, cond) \
  ((imm8) & 0xFF)      | \
  (((rot) & 0xF) << 8) | \
  ((freg_const) << 12) | \
  (1 << 25)            | \
  ARM_DEF_COND(cond)


typedef union {
  ARMInstrBR    br;
  ARMInstrDPI   dpi;
  ARMInstrMRT   mrt;
  ARMInstrMul   mul;
  ARMInstrWXfer wxfer;
  ARMInstrHXfer hxfer;
  ARMInstrSwap  swp;
  ARMInstrCDP   cdp;
  ARMInstrCDT   cdt;
  ARMInstrCRT   crt;
  ARMInstrSWI   swi;
  ARMInstrMSR   msr;
  ARMInstrMRS   mrs;
  ARMInstrCLZ   clz;

  ARMInstrGeneric generic;
  arminstr_t      raw;
} ARMInstr;

#endif /* ARM_CG_H */