summaryrefslogtreecommitdiff
path: root/arm/arm_emit.h
blob: fe7cc3106f813b688659b02fddeb409b845b7a52 (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
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
/* gameplaySP
 *
 * Copyright (C) 2006 Exophase <exophase@gmail.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#ifndef ARM_EMIT_H
#define ARM_EMIT_H

#include "arm_codegen.h"

u32 arm_update_gba_arm(u32 pc);
u32 arm_update_gba_thumb(u32 pc);
u32 arm_update_gba_idle_arm(u32 pc);
u32 arm_update_gba_idle_thumb(u32 pc);

// Although these are defined as a function, don't call them as
// such (jump to it instead)
void arm_indirect_branch_arm(u32 address);
void arm_indirect_branch_thumb(u32 address);
void arm_indirect_branch_dual_arm(u32 address);
void arm_indirect_branch_dual_thumb(u32 address);

void execute_store_cpsr(u32 new_cpsr, u32 store_mask, u32 address);
u32 execute_store_cpsr_body(u32 _cpsr, u32 store_mask, u32 address);
void execute_store_spsr(u32 new_cpsr, u32 store_mask);
u32 execute_read_spsr();
u32 execute_spsr_restore(u32 address);

void execute_swi_arm(u32 pc);
void execute_swi_thumb(u32 pc);

void function_cc execute_store_u32_safe(u32 address, u32 source);

#define write32(value)                                                        \
  *((u32 *)translation_ptr) = value;                                          \
  translation_ptr += 4                                                        \

#define arm_relative_offset(source, offset)                                   \
  (((((u32)offset - (u32)source) - 8) >> 2) & 0xFFFFFF)                       \


// reg_base_offset is the amount of bytes after reg_base where the registers
// actually begin.

#define reg_base_offset 1024


#define reg_a0          ARMREG_R0
#define reg_a1          ARMREG_R1
#define reg_a2          ARMREG_R2

#define reg_s0          ARMREG_R9
#define reg_base        ARMREG_SP
#define reg_flags       ARMREG_R11

#define reg_cycles      ARMREG_R12

#define reg_rv          ARMREG_R0

#define reg_rm          ARMREG_R0
#define reg_rn          ARMREG_R1
#define reg_rs          ARMREG_R14
#define reg_rd          ARMREG_R0


// Register allocation layout for ARM and Thumb:
// Map from a GBA register to a host ARM register. -1 means load it
// from memory into one of the temp registers.

// The following registers are chosen based on statistical analysis
// of a few games (see below), but might not be the best ones. Results
// vary tremendously between ARM and Thumb (for obvious reasons), so
// two sets are used. Take care to not call any function which can
// overwrite any of these registers from the dynarec - only call
// trusted functions in arm_stub.S which know how to save/restore
// them and know how to transfer them to the C functions it calls
// if necessary.

// The following define the actual registers available for allocation.
// As registers are freed up add them to this list.

// Note that r15 is linked to the a0 temp reg - this register will
// be preloaded with a constant upon read, and used to link to
// indirect branch functions upon write.

#define reg_x0         ARMREG_R3
#define reg_x1         ARMREG_R4
#define reg_x2         ARMREG_R5
#define reg_x3         ARMREG_R6
#define reg_x4         ARMREG_R7
#define reg_x5         ARMREG_R8

#define mem_reg        -1

/*

ARM register usage (38.775138% ARM instructions):
r00: 18.263814% (-- 18.263814%)
r12: 11.531477% (-- 29.795291%)
r09: 11.500162% (-- 41.295453%)
r14: 9.063440% (-- 50.358893%)
r06: 7.837682% (-- 58.196574%)
r01: 7.401049% (-- 65.597623%)
r07: 6.778340% (-- 72.375963%)
r05: 5.445009% (-- 77.820973%)
r02: 5.427288% (-- 83.248260%)
r03: 5.293743% (-- 88.542003%)
r04: 3.601103% (-- 92.143106%)
r11: 3.207311% (-- 95.350417%)
r10: 2.334864% (-- 97.685281%)
r08: 1.708207% (-- 99.393488%)
r15: 0.311270% (-- 99.704757%)
r13: 0.295243% (-- 100.000000%)

Thumb register usage (61.224862% Thumb instructions):
r00: 34.788858% (-- 34.788858%)
r01: 26.564083% (-- 61.352941%)
r03: 10.983500% (-- 72.336441%)
r02: 8.303127% (-- 80.639567%)
r04: 4.900381% (-- 85.539948%)
r05: 3.941292% (-- 89.481240%)
r06: 3.257582% (-- 92.738822%)
r07: 2.644851% (-- 95.383673%)
r13: 1.408824% (-- 96.792497%)
r08: 0.906433% (-- 97.698930%)
r09: 0.679693% (-- 98.378623%)
r10: 0.656446% (-- 99.035069%)
r12: 0.453668% (-- 99.488737%)
r14: 0.248909% (-- 99.737646%)
r11: 0.171066% (-- 99.908713%)
r15: 0.091287% (-- 100.000000%)

*/

s32 arm_register_allocation[] =
{
  reg_x0,       // GBA r0
  reg_x1,       // GBA r1
  mem_reg,      // GBA r2
  mem_reg,      // GBA r3
  mem_reg,      // GBA r4
  mem_reg,      // GBA r5
  reg_x2,       // GBA r6
  mem_reg,      // GBA r7
  mem_reg,      // GBA r8
  reg_x3,       // GBA r9
  mem_reg,      // GBA r10
  mem_reg,      // GBA r11
  reg_x4,       // GBA r12
  mem_reg,      // GBA r13
  reg_x5,       // GBA r14
  reg_a0        // GBA r15

  mem_reg,
  mem_reg,
  mem_reg,
  mem_reg,
  mem_reg,
  mem_reg,
  mem_reg,
  mem_reg,
  mem_reg,
  mem_reg,
  mem_reg,
  mem_reg,
  mem_reg,
  mem_reg,
  mem_reg,
  mem_reg,
};

s32 thumb_register_allocation[] =
{
  reg_x0,       // GBA r0
  reg_x1,       // GBA r1
  reg_x2,       // GBA r2
  reg_x3,       // GBA r3
  reg_x4,       // GBA r4
  reg_x5,       // GBA r5
  mem_reg,      // GBA r6
  mem_reg,      // GBA r7
  mem_reg,      // GBA r8
  mem_reg,      // GBA r9
  mem_reg,      // GBA r10
  mem_reg,      // GBA r11
  mem_reg,      // GBA r12
  mem_reg,      // GBA r13
  mem_reg,      // GBA r14
  reg_a0        // GBA r15

  mem_reg,
  mem_reg,
  mem_reg,
  mem_reg,
  mem_reg,
  mem_reg,
  mem_reg,
  mem_reg,
  mem_reg,
  mem_reg,
  mem_reg,
  mem_reg,
  mem_reg,
  mem_reg,
  mem_reg,
  mem_reg,
};



#define arm_imm_lsl_to_rot(value)                                             \
  (32 - value)                                                                \


u32 arm_disect_imm_32bit(u32 imm, u32 *stores, u32 *rotations)
{
  u32 store_count = 0;
  u32 left_shift = 0;

  // Otherwise it'll return 0 things to store because it'll never
  // find anything.
  if(imm == 0)
  {
    rotations[0] = 0;
    stores[0] = 0;
    return 1;
  }

  // Find chunks of non-zero data at 2 bit alignments.
  while(1)
  {
    for(; left_shift < 32; left_shift += 2)
    {
      if((imm >> left_shift) & 0x03)
        break;
    }

    if(left_shift == 32)
    {
      // We've hit the end of the useful data.
      return store_count;
    }

    // Hit the end, it might wrap back around to the beginning.
    if(left_shift >= 24)
    {
      // Make a mask for the residual bits. IE, if we have
      // 5 bits of data at the end we can wrap around to 3
      // bits of data in the beginning. Thus the first
      // thing, after being shifted left, has to be less
      // than 111b, 0x7, or (1 << 3) - 1.
      u32 top_bits = 32 - left_shift;
      u32 residual_bits = 8 - top_bits;
      u32 residual_mask = (1 << residual_bits) - 1;

      if((store_count > 1) && (left_shift > 24) &&
       ((stores[0] << ((32 - rotations[0]) & 0x1F)) < residual_mask))
      {
        // Then we can throw out the last bit and tack it on
        // to the first bit.
        stores[0] =
         (stores[0] << ((top_bits + (32 - rotations[0])) & 0x1F)) |
         ((imm >> left_shift) & 0xFF);
        rotations[0] = top_bits;

        return store_count;
      }
      else
      {
        // There's nothing to wrap over to in the beginning
        stores[store_count] = (imm >> left_shift) & 0xFF;
        rotations[store_count] = (32 - left_shift) & 0x1F;
        return store_count + 1;
      }
      break;
    }

    stores[store_count] = (imm >> left_shift) & 0xFF;
    rotations[store_count] = (32 - left_shift) & 0x1F;

    store_count++;
    left_shift += 8;
  }
}

#define arm_load_imm_32bit(ireg, imm)                                         \
{                                                                             \
  u32 stores[4];                                                              \
  u32 rotations[4];                                                           \
  u32 store_count = arm_disect_imm_32bit(imm, stores, rotations);             \
  u32 i;                                                                      \
                                                                              \
  ARM_MOV_REG_IMM(0, ireg, stores[0], rotations[0]);                          \
                                                                              \
  for(i = 1; i < store_count; i++)                                            \
  {                                                                           \
    ARM_ORR_REG_IMM(0, ireg, ireg, stores[i], rotations[i]);                  \
  }                                                                           \
}                                                                             \


#define generate_load_pc(ireg, new_pc)                                        \
  arm_load_imm_32bit(ireg, new_pc)                                            \

#define generate_load_imm(ireg, imm, imm_ror)                                 \
  ARM_MOV_REG_IMM(0, ireg, imm, imm_ror)                                      \



#define generate_shift_left(ireg, imm)                                        \
  ARM_MOV_REG_IMMSHIFT(0, ireg, ireg, ARMSHIFT_LSL, imm)                      \

#define generate_shift_right(ireg, imm)                                       \
  ARM_MOV_REG_IMMSHIFT(0, ireg, ireg, ARMSHIFT_LSR, imm)                      \

#define generate_shift_right_arithmetic(ireg, imm)                            \
  ARM_MOV_REG_IMMSHIFT(0, ireg, ireg, ARMSHIFT_ASR, imm)                      \

#define generate_rotate_right(ireg, imm)                                      \
  ARM_MOV_REG_IMMSHIFT(0, ireg, ireg, ARMSHIFT_ROR, imm)                      \

#define generate_add(ireg_dest, ireg_src)                                     \
  ARM_ADD_REG_REG(0, ireg_dest, ireg_dest, ireg_src)                          \

#define generate_sub(ireg_dest, ireg_src)                                     \
  ARM_SUB_REG_REG(0, ireg_dest, ireg_dest, ireg_src)                          \

#define generate_or(ireg_dest, ireg_src)                                      \
  ARM_ORR_REG_REG(0, ireg_dest, ireg_dest, ireg_src)                          \

#define generate_xor(ireg_dest, ireg_src)                                     \
  ARM_EOR_REG_REG(0, ireg_dest, ireg_dest, ireg_src)                          \

#define generate_add_imm(ireg, imm, imm_ror)                                  \
  ARM_ADD_REG_IMM(0, ireg, ireg, imm, imm_ror)                                \

#define generate_sub_imm(ireg, imm, imm_ror)                                  \
  ARM_SUB_REG_IMM(0, ireg, ireg, imm, imm_ror)                                \

#define generate_xor_imm(ireg, imm, imm_ror)                                  \
  ARM_EOR_REG_IMM(0, ireg, ireg, imm, imm_ror)                                \

#define generate_add_reg_reg_imm(ireg_dest, ireg_src, imm, imm_ror)           \
  ARM_ADD_REG_IMM(0, ireg_dest, ireg_src, imm, imm_ror)                       \

#define generate_and_imm(ireg, imm, imm_ror)                                  \
  ARM_AND_REG_IMM(0, ireg, ireg, imm, imm_ror)                                \

#define generate_mov(ireg_dest, ireg_src)                                     \
  if(ireg_dest != ireg_src)                                                   \
  {                                                                           \
    ARM_MOV_REG_REG(0, ireg_dest, ireg_src);                                  \
  }                                                                           \

#define generate_function_call(function_location)                             \
  ARM_BL(0, arm_relative_offset(translation_ptr, function_location))          \

#define generate_exit_block()                                                 \
  ARM_BX(0, ARMREG_LR)                                                        \

// The branch target is to be filled in later (thus a 0 for now)

#define generate_branch_filler(condition_code, writeback_location)            \
  (writeback_location) = translation_ptr;                                     \
  ARM_B_COND(0, condition_code, 0)                                            \

#define generate_update_pc(new_pc)                                            \
  generate_load_pc(reg_a0, new_pc)                                            \

#define generate_cycle_update()                                               \
  if(cycle_count)                                                             \
  {                                                                           \
    if(cycle_count >> 8)                                                      \
    {                                                                         \
      ARM_ADD_REG_IMM(0, reg_cycles, reg_cycles, (cycle_count >> 8) & 0xFF,   \
       arm_imm_lsl_to_rot(8));                                                \
    }                                                                         \
    ARM_ADD_REG_IMM(0, reg_cycles, reg_cycles, (cycle_count & 0xFF), 0);      \
    cycle_count = 0;                                                          \
  }                                                                           \

#define generate_cycle_update_flag_set()                                      \
  if(cycle_count >> 8)                                                        \
  {                                                                           \
    ARM_ADD_REG_IMM(0, reg_cycles, reg_cycles, (cycle_count >> 8) & 0xFF,     \
     arm_imm_lsl_to_rot(8));                                                  \
  }                                                                           \
  generate_save_flags();                                                      \
  ARM_ADDS_REG_IMM(0, reg_cycles, reg_cycles, (cycle_count & 0xFF), 0);       \
  cycle_count = 0                                                             \

#define generate_branch_patch_conditional(dest, offset)                       \
  *((u32 *)(dest)) = (*((u32 *)dest) & 0xFF000000) |                          \
   arm_relative_offset(dest, offset)                                          \

#define generate_branch_patch_unconditional(dest, offset)                     \
  *((u32 *)(dest)) = (*((u32 *)dest) & 0xFF000000) |                          \
   arm_relative_offset(dest, offset)                                          \

// A different function is called for idle updates because of the relative
// location of the embedded PC. The idle version could be optimized to put
// the CPU into halt mode too, however.

#define generate_branch_idle_eliminate(writeback_location, new_pc, mode)      \
  generate_function_call(arm_update_gba_idle_##mode);                         \
  write32(new_pc);                                                            \
  generate_branch_filler(ARMCOND_AL, writeback_location)                      \

#define generate_branch_update(writeback_location, new_pc, mode)              \
  ARM_MOV_REG_IMMSHIFT(0, reg_a0, reg_cycles, ARMSHIFT_LSR, 31);              \
  ARM_ADD_REG_IMMSHIFT(0, ARMREG_PC, ARMREG_PC, reg_a0, ARMSHIFT_LSL, 2);     \
  write32(new_pc);                                                            \
  generate_function_call(arm_update_gba_##mode);                              \
  generate_branch_filler(ARMCOND_AL, writeback_location)                      \


#define generate_branch_no_cycle_update(writeback_location, new_pc, mode)     \
  if(pc == idle_loop_target_pc)                                               \
  {                                                                           \
    generate_branch_idle_eliminate(writeback_location, new_pc, mode);         \
  }                                                                           \
  else                                                                        \
  {                                                                           \
    generate_branch_update(writeback_location, new_pc, mode);                 \
  }                                                                           \

#define generate_branch_cycle_update(writeback_location, new_pc, mode)        \
  generate_cycle_update();                                                    \
  generate_branch_no_cycle_update(writeback_location, new_pc, mode)           \

// a0 holds the destination

#define generate_indirect_branch_no_cycle_update(type)                        \
  ARM_B(0, arm_relative_offset(translation_ptr, arm_indirect_branch_##type))  \

#define generate_indirect_branch_cycle_update(type)                           \
  generate_cycle_update();                                                    \
  generate_indirect_branch_no_cycle_update(type)                              \

#define generate_block_prologue()                                             \

#define generate_block_extra_vars_arm()                                       \
  void generate_indirect_branch_arm()                                         \
  {                                                                           \
    if(condition == 0x0E)                                                     \
    {                                                                         \
      generate_cycle_update();                                                \
    }                                                                         \
    generate_indirect_branch_no_cycle_update(arm);                            \
  }                                                                           \
                                                                              \
  void generate_indirect_branch_dual()                                        \
  {                                                                           \
    if(condition == 0x0E)                                                     \
    {                                                                         \
      generate_cycle_update();                                                \
    }                                                                         \
    generate_indirect_branch_no_cycle_update(dual_arm);                       \
  }                                                                           \
                                                                              \
  u32 prepare_load_reg(u32 scratch_reg, u32 reg_index)                        \
  {                                                                           \
    u32 reg_use = arm_register_allocation[reg_index];                         \
    if(reg_use == mem_reg)                                                    \
    {                                                                         \
      ARM_LDR_IMM(0, scratch_reg, reg_base,                                   \
       (reg_base_offset + (reg_index * 4)));                                  \
      return scratch_reg;                                                     \
    }                                                                         \
                                                                              \
    return reg_use;                                                           \
  }                                                                           \
                                                                              \
  u32 prepare_load_reg_pc(u32 scratch_reg, u32 reg_index, u32 pc_offset)      \
  {                                                                           \
    if(reg_index == 15)                                                       \
    {                                                                         \
      generate_load_pc(scratch_reg, pc + pc_offset);                          \
      return scratch_reg;                                                     \
    }                                                                         \
    return prepare_load_reg(scratch_reg, reg_index);                          \
  }                                                                           \
                                                                              \
  u32 prepare_store_reg(u32 scratch_reg, u32 reg_index)                       \
  {                                                                           \
    u32 reg_use = arm_register_allocation[reg_index];                         \
    if(reg_use == mem_reg)                                                    \
      return scratch_reg;                                                     \
                                                                              \
    return reg_use;                                                           \
  }                                                                           \
                                                                              \
  void complete_store_reg(u32 scratch_reg, u32 reg_index)                     \
  {                                                                           \
    if(arm_register_allocation[reg_index] == mem_reg)                         \
    {                                                                         \
      ARM_STR_IMM(0, scratch_reg, reg_base,                                   \
       (reg_base_offset + (reg_index * 4)));                                  \
    }                                                                         \
  }                                                                           \
                                                                              \
  void complete_store_reg_pc_no_flags(u32 scratch_reg, u32 reg_index)         \
  {                                                                           \
    if(reg_index == 15)                                                       \
    {                                                                         \
      generate_indirect_branch_arm();                                         \
    }                                                                         \
    else                                                                      \
    {                                                                         \
      complete_store_reg(scratch_reg, reg_index);                             \
    }                                                                         \
  }                                                                           \
                                                                              \
  void complete_store_reg_pc_flags(u32 scratch_reg, u32 reg_index)            \
  {                                                                           \
    if(reg_index == 15)                                                       \
    {                                                                         \
      if(condition == 0x0E)                                                   \
      {                                                                       \
        generate_cycle_update();                                              \
      }                                                                       \
      generate_function_call(execute_spsr_restore);                           \
    }                                                                         \
    else                                                                      \
    {                                                                         \
      complete_store_reg(scratch_reg, reg_index);                             \
    }                                                                         \
  }                                                                           \
                                                                              \
  void generate_load_reg(u32 ireg, u32 reg_index)                             \
  {                                                                           \
    s32 load_src = arm_register_allocation[reg_index];                        \
    if(load_src != mem_reg)                                                   \
    {                                                                         \
      ARM_MOV_REG_REG(0, ireg, load_src);                                     \
    }                                                                         \
    else                                                                      \
    {                                                                         \
      ARM_LDR_IMM(0, ireg, reg_base, (reg_base_offset + (reg_index * 4)));    \
    }                                                                         \
  }                                                                           \
                                                                              \
  void generate_store_reg(u32 ireg, u32 reg_index)                            \
  {                                                                           \
    s32 store_dest = arm_register_allocation[reg_index];                      \
    if(store_dest != mem_reg)                                                 \
    {                                                                         \
      ARM_MOV_REG_REG(0, store_dest, ireg);                                   \
    }                                                                         \
    else                                                                      \
    {                                                                         \
      ARM_STR_IMM(0, ireg, reg_base, (reg_base_offset + (reg_index * 4)));    \
    }                                                                         \
  }                                                                           \


#define generate_block_extra_vars_thumb()                                     \
  u32 prepare_load_reg(u32 scratch_reg, u32 reg_index)                        \
  {                                                                           \
    u32 reg_use = thumb_register_allocation[reg_index];                       \
    if(reg_use == mem_reg)                                                    \
    {                                                                         \
      ARM_LDR_IMM(0, scratch_reg, reg_base,                                   \
       (reg_base_offset + (reg_index * 4)));                                  \
      return scratch_reg;                                                     \
    }                                                                         \
                                                                              \
    return reg_use;                                                           \
  }                                                                           \
                                                                              \
  u32 prepare_load_reg_pc(u32 scratch_reg, u32 reg_index, u32 pc_offset)      \
  {                                                                           \
    if(reg_index == 15)                                                       \
    {                                                                         \
      generate_load_pc(scratch_reg, pc + pc_offset);                          \
      return scratch_reg;                                                     \
    }                                                                         \
    return prepare_load_reg(scratch_reg, reg_index);                          \
  }                                                                           \
                                                                              \
  u32 prepare_store_reg(u32 scratch_reg, u32 reg_index)                       \
  {                                                                           \
    u32 reg_use = thumb_register_allocation[reg_index];                       \
    if(reg_use == mem_reg)                                                    \
      return scratch_reg;                                                     \
                                                                              \
    return reg_use;                                                           \
  }                                                                           \
                                                                              \
  void complete_store_reg(u32 scratch_reg, u32 reg_index)                     \
  {                                                                           \
    if(thumb_register_allocation[reg_index] == mem_reg)                       \
    {                                                                         \
      ARM_STR_IMM(0, scratch_reg, reg_base,                                   \
       (reg_base_offset + (reg_index * 4)));                                  \
    }                                                                         \
  }                                                                           \
                                                                              \
  void generate_load_reg(u32 ireg, u32 reg_index)                             \
  {                                                                           \
    s32 load_src = thumb_register_allocation[reg_index];                      \
    if(load_src != mem_reg)                                                   \
    {                                                                         \
      ARM_MOV_REG_REG(0, ireg, load_src);                                     \
    }                                                                         \
    else                                                                      \
    {                                                                         \
      ARM_LDR_IMM(0, ireg, reg_base, (reg_base_offset + (reg_index * 4)));    \
    }                                                                         \
  }                                                                           \
                                                                              \
  void generate_store_reg(u32 ireg, u32 reg_index)                            \
  {                                                                           \
    s32 store_dest = thumb_register_allocation[reg_index];                    \
    if(store_dest != mem_reg)                                                 \
    {                                                                         \
      ARM_MOV_REG_REG(0, store_dest, ireg);                                   \
    }                                                                         \
    else                                                                      \
    {                                                                         \
      ARM_STR_IMM(0, ireg, reg_base, (reg_base_offset + (reg_index * 4)));    \
    }                                                                         \
  }                                                                           \

#define block_prologue_size 0

// It should be okay to still generate result flags, spsr will overwrite them.
// This is pretty infrequent (returning from interrupt handlers, et al) so
// probably not worth optimizing for.

#define check_for_interrupts()                                                \
  if((io_registers[REG_IE] & io_registers[REG_IF]) &&                         \
   io_registers[REG_IME] && ((reg[REG_CPSR] & 0x80) == 0))                    \
  {                                                                           \
    reg_mode[MODE_IRQ][6] = pc + 4;                                           \
    spsr[MODE_IRQ] = reg[REG_CPSR];                                           \
    reg[REG_CPSR] = 0xD2;                                                     \
    pc = 0x00000018;                                                          \
    set_cpu_mode(MODE_IRQ);                                                   \
  }                                                                           \

#define generate_load_reg_pc(ireg, reg_index, pc_offset)                      \
  if(reg_index == 15)                                                         \
  {                                                                           \
    generate_load_pc(ireg, pc + pc_offset);                                   \
  }                                                                           \
  else                                                                        \
  {                                                                           \
    generate_load_reg(ireg, reg_index);                                       \
  }                                                                           \

#define generate_store_reg_pc_no_flags(ireg, reg_index)                       \
  generate_store_reg(ireg, reg_index);                                        \
  if(reg_index == 15)                                                         \
  {                                                                           \
    generate_indirect_branch_arm();                                           \
  }                                                                           \


u32 function_cc execute_spsr_restore_body(u32 pc)
{
  set_cpu_mode(cpu_modes[reg[REG_CPSR] & 0x1F]);
  check_for_interrupts();

  return pc;
}


#define generate_store_reg_pc_flags(ireg, reg_index)                          \
  generate_store_reg(ireg, reg_index);                                        \
  if(reg_index == 15)                                                         \
  {                                                                           \
    if(condition == 0x0E)                                                     \
    {                                                                         \
      generate_cycle_update();                                                \
    }                                                                         \
    generate_function_call(execute_spsr_restore);                             \
  }                                                                           \


#define generate_load_flags()                                                 \
/*  ARM_MSR_REG(0, ARM_PSR_F, reg_flags, ARM_CPSR) */                         \

#define generate_store_flags()                                                \
/*  ARM_MRS_CPSR(0, reg_flags) */                                             \

#define generate_save_flags()                                                 \
  ARM_MRS_CPSR(0, reg_flags)                                                  \

#define generate_restore_flags()                                              \
  ARM_MSR_REG(0, ARM_PSR_F, reg_flags, ARM_CPSR)                              \


#define condition_opposite_eq ARMCOND_NE
#define condition_opposite_ne ARMCOND_EQ
#define condition_opposite_cs ARMCOND_CC
#define condition_opposite_cc ARMCOND_CS
#define condition_opposite_mi ARMCOND_PL
#define condition_opposite_pl ARMCOND_MI
#define condition_opposite_vs ARMCOND_VC
#define condition_opposite_vc ARMCOND_VS
#define condition_opposite_hi ARMCOND_LS
#define condition_opposite_ls ARMCOND_HI
#define condition_opposite_ge ARMCOND_LT
#define condition_opposite_lt ARMCOND_GE
#define condition_opposite_gt ARMCOND_LE
#define condition_opposite_le ARMCOND_GT
#define condition_opposite_al ARMCOND_NV
#define condition_opposite_nv ARMCOND_AL

#define generate_branch(mode)                                                 \
{                                                                             \
  generate_branch_cycle_update(                                               \
   block_exits[block_exit_position].branch_source,                            \
   block_exits[block_exit_position].branch_target, mode);                     \
  block_exit_position++;                                                      \
}                                                                             \


#define generate_op_and_reg_immshift(_rd, _rn, _rm, shift_type, shift)        \
  ARM_AND_REG_IMMSHIFT(0, _rd, _rn, _rm, shift_type, shift)                   \

#define generate_op_orr_reg_immshift(_rd, _rn, _rm, shift_type, shift)        \
  ARM_ORR_REG_IMMSHIFT(0, _rd, _rn, _rm, shift_type, shift)                   \

#define generate_op_eor_reg_immshift(_rd, _rn, _rm, shift_type, shift)        \
  ARM_EOR_REG_IMMSHIFT(0, _rd, _rn, _rm, shift_type, shift)                   \

#define generate_op_bic_reg_immshift(_rd, _rn, _rm, shift_type, shift)        \
  ARM_BIC_REG_IMMSHIFT(0, _rd, _rn, _rm, shift_type, shift)                   \

#define generate_op_sub_reg_immshift(_rd, _rn, _rm, shift_type, shift)        \
  ARM_SUB_REG_IMMSHIFT(0, _rd, _rn, _rm, shift_type, shift)                   \

#define generate_op_rsb_reg_immshift(_rd, _rn, _rm, shift_type, shift)        \
  ARM_RSB_REG_IMMSHIFT(0, _rd, _rn, _rm, shift_type, shift)                   \

#define generate_op_sbc_reg_immshift(_rd, _rn, _rm, shift_type, shift)        \
  ARM_SBC_REG_IMMSHIFT(0, _rd, _rn, _rm, shift_type, shift)                   \

#define generate_op_rsc_reg_immshift(_rd, _rn, _rm, shift_type, shift)        \
  ARM_RSC_REG_IMMSHIFT(0, _rd, _rn, _rm, shift_type, shift)                   \

#define generate_op_add_reg_immshift(_rd, _rn, _rm, shift_type, shift)        \
  ARM_ADD_REG_IMMSHIFT(0, _rd, _rn, _rm, shift_type, shift)                   \

#define generate_op_adc_reg_immshift(_rd, _rn, _rm, shift_type, shift)        \
  ARM_ADC_REG_IMMSHIFT(0, _rd, _rn, _rm, shift_type, shift)                   \

#define generate_op_mov_reg_immshift(_rd, _rn, _rm, shift_type, shift)        \
  ARM_MOV_REG_IMMSHIFT(0, _rd, _rm, shift_type, shift)                        \

#define generate_op_mvn_reg_immshift(_rd, _rn, _rm, shift_type, shift)        \
  ARM_MVN_REG_IMMSHIFT(0, _rd, _rm, shift_type, shift)                        \


#define generate_op_and_reg_regshift(_rd, _rn, _rm, shift_type, _rs)          \
  ARM_AND_REG_REGSHIFT(0, _rd, _rn, _rm, shift_type, _rs)                     \

#define generate_op_orr_reg_regshift(_rd, _rn, _rm, shift_type, _rs)          \
  ARM_ORR_REG_REGSHIFT(0, _rd, _rn, _rm, shift_type, _rs)                     \

#define generate_op_eor_reg_regshift(_rd, _rn, _rm, shift_type, _rs)          \
  ARM_EOR_REG_REGSHIFT(0, _rd, _rn, _rm, shift_type, _rs)                     \

#define generate_op_bic_reg_regshift(_rd, _rn, _rm, shift_type, _rs)          \
  ARM_BIC_REG_REGSHIFT(0, _rd, _rn, _rm, shift_type, _rs)                     \

#define generate_op_sub_reg_regshift(_rd, _rn, _rm, shift_type, _rs)          \
  ARM_SUB_REG_REGSHIFT(0, _rd, _rn, _rm, shift_type, _rs)                     \

#define generate_op_rsb_reg_regshift(_rd, _rn, _rm, shift_type, _rs)          \
  ARM_RSB_REG_REGSHIFT(0, _rd, _rn, _rm, shift_type, _rs)                     \

#define generate_op_sbc_reg_regshift(_rd, _rn, _rm, shift_type, _rs)          \
  ARM_SBC_REG_REGSHIFT(0, _rd, _rn, _rm, shift_type, _rs)                     \

#define generate_op_rsc_reg_regshift(_rd, _rn, _rm, shift_type, _rs)          \
  ARM_RSC_REG_REGSHIFT(0, _rd, _rn, _rm, shift_type, _rs)                     \

#define generate_op_add_reg_regshift(_rd, _rn, _rm, shift_type, _rs)          \
  ARM_ADD_REG_REGSHIFT(0, _rd, _rn, _rm, shift_type, _rs)                     \

#define generate_op_adc_reg_regshift(_rd, _rn, _rm, shift_type, _rs)          \
  ARM_ADC_REG_REGSHIFT(0, _rd, _rn, _rm, shift_type, _rs)                     \

#define generate_op_mov_reg_regshift(_rd, _rn, _rm, shift_type, _rs)          \
  ARM_MOV_REG_REGSHIFT(0, _rd, _rm, shift_type, _rs)                          \

#define generate_op_mvn_reg_regshift(_rd, _rn, _rm, shift_type, _rs)          \
  ARM_MVN_REG_REGSHIFT(0, _rd, _rm, shift_type, _rs)                          \


#define generate_op_and_imm(_rd, _rn)                                         \
  ARM_AND_REG_IMM(0, _rd, _rn, imm, imm_ror)                                  \

#define generate_op_orr_imm(_rd, _rn)                                         \
  ARM_ORR_REG_IMM(0, _rd, _rn, imm, imm_ror)                                  \

#define generate_op_eor_imm(_rd, _rn)                                         \
  ARM_EOR_REG_IMM(0, _rd, _rn, imm, imm_ror)                                  \

#define generate_op_bic_imm(_rd, _rn)                                         \
  ARM_BIC_REG_IMM(0, _rd, _rn, imm, imm_ror)                                  \

#define generate_op_sub_imm(_rd, _rn)                                         \
  ARM_SUB_REG_IMM(0, _rd, _rn, imm, imm_ror)                                  \

#define generate_op_rsb_imm(_rd, _rn)                                         \
  ARM_RSB_REG_IMM(0, _rd, _rn, imm, imm_ror)                                  \

#define generate_op_sbc_imm(_rd, _rn)                                         \
  ARM_SBC_REG_IMM(0, _rd, _rn, imm, imm_ror)                                  \

#define generate_op_rsc_imm(_rd, _rn)                                         \
  ARM_RSC_REG_IMM(0, _rd, _rn, imm, imm_ror)                                  \

#define generate_op_add_imm(_rd, _rn)                                         \
  ARM_ADD_REG_IMM(0, _rd, _rn, imm, imm_ror)                                  \

#define generate_op_adc_imm(_rd, _rn)                                         \
  ARM_ADC_REG_IMM(0, _rd, _rn, imm, imm_ror)                                  \

#define generate_op_mov_imm(_rd, _rn)                                         \
  ARM_MOV_REG_IMM(0, _rd, imm, imm_ror)                                       \

#define generate_op_mvn_imm(_rd, _rn)                                         \
  ARM_MVN_REG_IMM(0, _rd, imm, imm_ror)                                       \


#define generate_op_reg_immshift_lflags(name, _rd, _rn, _rm, st, shift)       \
  ARM_##name##_REG_IMMSHIFT(0, _rd, _rn, _rm, st, shift)                      \

#define generate_op_reg_immshift_aflags(name, _rd, _rn, _rm, st, shift)       \
  ARM_##name##_REG_IMMSHIFT(0, _rd, _rn, _rm, st, shift)                      \

#define generate_op_reg_immshift_aflags_load_c(name, _rd, _rn, _rm, st, sh)   \
  ARM_##name##_REG_IMMSHIFT(0, _rd, _rn, _rm, st, sh)                         \

#define generate_op_reg_immshift_uflags(name, _rd, _rm, shift_type, shift)    \
  ARM_##name##_REG_IMMSHIFT(0, _rd, _rm, shift_type, shift)                   \

#define generate_op_reg_immshift_tflags(name, _rn, _rm, shift_type, shift)    \
  ARM_##name##_REG_IMMSHIFT(0, _rn, _rm, shift_type, shift)                   \


#define generate_op_reg_regshift_lflags(name, _rd, _rn, _rm, shift_type, _rs) \
  ARM_##name##_REG_REGSHIFT(0, _rd, _rn, _rm, shift_type, _rs)                \

#define generate_op_reg_regshift_aflags(name, _rd, _rn, _rm, st, _rs)         \
  ARM_##name##_REG_REGSHIFT(0, _rd, _rn, _rm, st, _rs)                        \

#define generate_op_reg_regshift_aflags_load_c(name, _rd, _rn, _rm, st, _rs)  \
  ARM_##name##_REG_REGSHIFT(0, _rd, _rn, _rm, st, _rs)                        \

#define generate_op_reg_regshift_uflags(name, _rd, _rm, shift_type, _rs)      \
  ARM_##name##_REG_REGSHIFT(0, _rd, _rm, shift_type, _rs)                     \

#define generate_op_reg_regshift_tflags(name, _rn, _rm, shift_type, _rs)      \
  ARM_##name##_REG_REGSHIFT(0, _rn, _rm, shift_type, _rs)                     \


#define generate_op_imm_lflags(name, _rd, _rn)                                \
  ARM_##name##_REG_IMM(0, _rd, _rn, imm, imm_ror)                             \

#define generate_op_imm_aflags(name, _rd, _rn)                                \
  ARM_##name##_REG_IMM(0, _rd, _rn, imm, imm_ror)                             \

#define generate_op_imm_aflags_load_c(name, _rd, _rn)                         \
  ARM_##name##_REG_IMM(0, _rd, _rn, imm, imm_ror)                             \

#define generate_op_imm_uflags(name, _rd)                                     \
  ARM_##name##_REG_IMM(0, _rd, imm, imm_ror)                                  \

#define generate_op_imm_tflags(name, _rn)                                     \
  ARM_##name##_REG_IMM(0, _rn, imm, imm_ror)                                  \


#define generate_op_ands_reg_immshift(_rd, _rn, _rm, shift_type, shift)       \
  generate_op_reg_immshift_lflags(ANDS, _rd, _rn, _rm, shift_type, shift)     \

#define generate_op_orrs_reg_immshift(_rd, _rn, _rm, shift_type, shift)       \
  generate_op_reg_immshift_lflags(ORRS, _rd, _rn, _rm, shift_type, shift)     \

#define generate_op_eors_reg_immshift(_rd, _rn, _rm, shift_type, shift)       \
  generate_op_reg_immshift_lflags(EORS, _rd, _rn, _rm, shift_type, shift)     \

#define generate_op_bics_reg_immshift(_rd, _rn, _rm, shift_type, shift)       \
  generate_op_reg_immshift_lflags(BICS, _rd, _rn, _rm, shift_type, shift)     \

#define generate_op_subs_reg_immshift(_rd, _rn, _rm, shift_type, shift)       \
  generate_op_reg_immshift_aflags(SUBS, _rd, _rn, _rm, shift_type, shift)     \

#define generate_op_rsbs_reg_immshift(_rd, _rn, _rm, shift_type, shift)       \
  generate_op_reg_immshift_aflags(RSBS, _rd, _rn, _rm, shift_type, shift)     \

#define generate_op_sbcs_reg_immshift(_rd, _rn, _rm, st, shift)               \
  generate_op_reg_immshift_aflags_load_c(SBCS, _rd, _rn, _rm, st, shift)      \

#define generate_op_rscs_reg_immshift(_rd, _rn, _rm, st, shift)               \
  generate_op_reg_immshift_aflags_load_c(RSCS, _rd, _rn, _rm, st, shift)      \

#define generate_op_adds_reg_immshift(_rd, _rn, _rm, shift_type, shift)       \
  generate_op_reg_immshift_aflags(ADDS, _rd, _rn, _rm, shift_type, shift)     \

#define generate_op_adcs_reg_immshift(_rd, _rn, _rm, st, shift)               \
  generate_op_reg_immshift_aflags_load_c(ADCS, _rd, _rn, _rm, st, shift)      \

#define generate_op_movs_reg_immshift(_rd, _rn, _rm, shift_type, shift)       \
  generate_op_reg_immshift_uflags(MOVS, _rd, _rm, shift_type, shift)          \

#define generate_op_mvns_reg_immshift(_rd, _rn, _rm, shift_type, shift)       \
  generate_op_reg_immshift_uflags(MVNS, _rd, _rm, shift_type, shift)          \

// The reg operand is in reg_rm, not reg_rn like expected, so rsbs isn't
// being used here. When rsbs is fully inlined it can be used with the
// apropriate operands.

#define generate_op_neg_reg_immshift(_rd, _rn, _rm, shift_type, shift)        \
{                                                                             \
  generate_load_imm(reg_rn, 0, 0);                                            \
  generate_op_subs_reg_immshift(_rd, reg_rn, _rm, ARMSHIFT_LSL, 0);           \
}                                                                             \

#define generate_op_muls_reg_immshift(_rd, _rn, _rm, shift_type, shift)       \
  generate_load_flags();                                                      \
  ARM_MULS(0, _rd, _rn, _rm);                                                 \
  generate_store_flags()                                                      \

#define generate_op_cmp_reg_immshift(_rd, _rn, _rm, shift_type, shift)        \
  generate_op_reg_immshift_tflags(CMP, _rn, _rm, shift_type, shift)           \

#define generate_op_cmn_reg_immshift(_rd, _rn, _rm, shift_type, shift)        \
  generate_op_reg_immshift_tflags(CMN, _rn, _rm, shift_type, shift)           \

#define generate_op_tst_reg_immshift(_rd, _rn, _rm, shift_type, shift)        \
  generate_op_reg_immshift_tflags(TST, _rn, _rm, shift_type, shift)           \

#define generate_op_teq_reg_immshift(_rd, _rn, _rm, shift_type, shift)        \
  generate_op_reg_immshift_tflags(TEQ, _rn, _rm, shift_type, shift)           \


#define generate_op_ands_reg_regshift(_rd, _rn, _rm, shift_type, _rs)         \
  generate_op_reg_regshift_lflags(ANDS, _rd, _rn, _rm, shift_type, _rs)       \

#define generate_op_orrs_reg_regshift(_rd, _rn, _rm, shift_type, _rs)         \
  generate_op_reg_regshift_lflags(ORRS, _rd, _rn, _rm, shift_type, _rs)       \

#define generate_op_eors_reg_regshift(_rd, _rn, _rm, shift_type, _rs)         \
  generate_op_reg_regshift_lflags(EORS, _rd, _rn, _rm, shift_type, _rs)       \

#define generate_op_bics_reg_regshift(_rd, _rn, _rm, shift_type, _rs)         \
  generate_op_reg_regshift_lflags(BICS, _rd, _rn, _rm, shift_type, _rs)       \

#define generate_op_subs_reg_regshift(_rd, _rn, _rm, shift_type, _rs)         \
  generate_op_reg_regshift_aflags(SUBS, _rd, _rn, _rm, shift_type, _rs)       \

#define generate_op_rsbs_reg_regshift(_rd, _rn, _rm, shift_type, _rs)         \
  generate_op_reg_regshift_aflags(RSBS, _rd, _rn, _rm, shift_type, _rs)       \

#define generate_op_sbcs_reg_regshift(_rd, _rn, _rm, st, _rs)                 \
  generate_op_reg_regshift_aflags_load_c(SBCS, _rd, _rn, _rm, st, _rs)        \

#define generate_op_rscs_reg_regshift(_rd, _rn, _rm, st, _rs)                 \
  generate_op_reg_regshift_aflags_load_c(RSCS, _rd, _rn, _rm, st, _rs)        \

#define generate_op_adds_reg_regshift(_rd, _rn, _rm, shift_type, _rs)         \
  generate_op_reg_regshift_aflags(ADDS, _rd, _rn, _rm, shift_type, _rs)       \

#define generate_op_adcs_reg_regshift(_rd, _rn, _rm, st, _rs)                 \
  generate_op_reg_regshift_aflags_load_c(ADCS, _rd, _rn, _rm, st, _rs)        \

#define generate_op_movs_reg_regshift(_rd, _rn, _rm, shift_type, _rs)         \
  generate_op_reg_regshift_uflags(MOVS, _rd, _rm, shift_type, _rs)            \

#define generate_op_mvns_reg_regshift(_rd, _rn, _rm, shift_type, _rs)         \
  generate_op_reg_regshift_uflags(MVNS, _rd, _rm, shift_type, _rs)            \

#define generate_op_cmp_reg_regshift(_rd, _rn, _rm, shift_type, _rs)          \
  generate_op_reg_regshift_tflags(CMP, _rn, _rm, shift_type, _rs)             \

#define generate_op_cmn_reg_regshift(_rd, _rn, _rm, shift_type, _rs)          \
  generate_op_reg_regshift_tflags(CMN, _rn, _rm, shift_type, _rs)             \

#define generate_op_tst_reg_regshift(_rd, _rn, _rm, shift_type, _rs)          \
  generate_op_reg_regshift_tflags(TST, _rn, _rm, shift_type, _rs)             \

#define generate_op_teq_reg_regshift(_rd, _rn, _rm, shift_type, _rs)          \
  generate_op_reg_regshift_tflags(TEQ, _rn, _rm, shift_type, _rs)             \


#define generate_op_ands_imm(_rd, _rn)                                        \
  generate_op_imm_lflags(ANDS, _rd, _rn)                                      \

#define generate_op_orrs_imm(_rd, _rn)                                        \
  generate_op_imm_lflags(ORRS, _rd, _rn)                                      \

#define generate_op_eors_imm(_rd, _rn)                                        \
  generate_op_imm_lflags(EORS, _rd, _rn)                                      \

#define generate_op_bics_imm(_rd, _rn)                                        \
  generate_op_imm_lflags(BICS, _rd, _rn)                                      \

#define generate_op_subs_imm(_rd, _rn)                                        \
  generate_op_imm_aflags(SUBS, _rd, _rn)                                      \

#define generate_op_rsbs_imm(_rd, _rn)                                        \
  generate_op_imm_aflags(RSBS, _rd, _rn)                                      \

#define generate_op_sbcs_imm(_rd, _rn)                                        \
  generate_op_imm_aflags_load_c(SBCS, _rd, _rn)                               \

#define generate_op_rscs_imm(_rd, _rn)                                        \
  generate_op_imm_aflags_load_c(RSCS, _rd, _rn)                               \

#define generate_op_adds_imm(_rd, _rn)                                        \
  generate_op_imm_aflags(ADDS, _rd, _rn)                                      \

#define generate_op_adcs_imm(_rd, _rn)                                        \
  generate_op_imm_aflags_load_c(ADCS, _rd, _rn)                               \

#define generate_op_movs_imm(_rd, _rn)                                        \
  generate_op_imm_uflags(MOVS, _rd)                                           \

#define generate_op_mvns_imm(_rd, _rn)                                        \
  generate_op_imm_uflags(MVNS, _rd)                                           \

#define generate_op_cmp_imm(_rd, _rn)                                         \
  generate_op_imm_tflags(CMP, _rn)                                            \

#define generate_op_cmn_imm(_rd, _rn)                                         \
  generate_op_imm_tflags(CMN, _rn)                                            \

#define generate_op_tst_imm(_rd, _rn)                                         \
  generate_op_imm_tflags(TST, _rn)                                            \

#define generate_op_teq_imm(_rd, _rn)                                         \
  generate_op_imm_tflags(TEQ, _rn)                                            \


#define prepare_load_rn_yes()                                                 \
  u32 _rn = prepare_load_reg_pc(reg_rn, rn, 8)                                \

#define prepare_load_rn_no()                                                  \

#define prepare_store_rd_yes()                                                \
  u32 _rd = prepare_store_reg(reg_rd, rd)                                     \

#define prepare_store_rd_no()                                                 \

#define complete_store_rd_yes(flags_op)                                       \
  complete_store_reg_pc_##flags_op(_rd, rd)                                   \

#define complete_store_rd_no(flags_op)                                        \

#define arm_generate_op_reg(name, load_op, store_op, flags_op)                \
  u32 shift_type = (opcode >> 5) & 0x03;                                      \
  arm_decode_data_proc_reg(opcode);                                           \
  prepare_load_rn_##load_op();                                                \
  prepare_store_rd_##store_op();                                              \
                                                                              \
  if((opcode >> 4) & 0x01)                                                    \
  {                                                                           \
    u32 rs = ((opcode >> 8) & 0x0F);                                          \
    u32 _rs = prepare_load_reg(reg_rs, rs);                                   \
    u32 _rm = prepare_load_reg_pc(reg_rm, rm, 12);                            \
    generate_op_##name##_reg_regshift(_rd, _rn, _rm, shift_type, _rs);        \
  }                                                                           \
  else                                                                        \
  {                                                                           \
    u32 shift_imm = ((opcode >> 7) & 0x1F);                                   \
    u32 _rm = prepare_load_reg_pc(reg_rm, rm, 8);                             \
    generate_op_##name##_reg_immshift(_rd, _rn, _rm, shift_type, shift_imm);  \
  }                                                                           \
  complete_store_rd_##store_op(flags_op)                                      \

#define arm_generate_op_reg_flags(name, load_op, store_op, flags_op)          \
  arm_generate_op_reg(name, load_op, store_op, flags_op)                      \

// imm will be loaded by the called function if necessary.

#define arm_generate_op_imm(name, load_op, store_op, flags_op)                \
  arm_decode_data_proc_imm(opcode);                                           \
  prepare_load_rn_##load_op();                                                \
  prepare_store_rd_##store_op();                                              \
  generate_op_##name##_imm(_rd, _rn);                                         \
  complete_store_rd_##store_op(flags_op)                                      \

#define arm_generate_op_imm_flags(name, load_op, store_op, flags_op)          \
  arm_generate_op_imm(name, load_op, store_op, flags_op)                      \

#define arm_data_proc(name, type, flags_op)                                   \
{                                                                             \
  arm_generate_op_##type(name, yes, yes, flags_op);                           \
}                                                                             \

#define arm_data_proc_test(name, type)                                        \
{                                                                             \
  arm_generate_op_##type(name, yes, no, no);                                  \
}                                                                             \

#define arm_data_proc_unary(name, type, flags_op)                             \
{                                                                             \
  arm_generate_op_##type(name, no, yes, flags_op);                            \
}                                                                             \


#define arm_multiply_add_no_flags_no()                                        \
  ARM_MUL(0, _rd, _rm, _rs)                                                   \

#define arm_multiply_add_yes_flags_no()                                       \
  u32 _rn = prepare_load_reg(reg_a2, rn);                                     \
  ARM_MLA(0, _rd, _rm, _rs, _rn)                                              \

#define arm_multiply_add_no_flags_yes()                                       \
  generate_load_flags();                                                      \
  ARM_MULS(0, reg_a0, reg_a0, reg_a1)                                         \
  generate_store_flags()                                                      \

#define arm_multiply_add_yes_flags_yes()                                      \
  u32 _rn = prepare_load_reg(reg_a2, rn);                                     \
  generate_load_flags();                                                      \
  ARM_MLAS(0, _rd, _rm, _rs, _rn);                                            \
  generate_store_flags()


#define arm_multiply(add_op, flags)                                           \
{                                                                             \
  arm_decode_multiply();                                                      \
  u32 _rm = prepare_load_reg(reg_a0, rm);                                     \
  u32 _rs = prepare_load_reg(reg_a1, rs);                                     \
  u32 _rd = prepare_store_reg(reg_a0, rd);                                    \
  arm_multiply_add_##add_op##_flags_##flags();                                \
  complete_store_reg(_rd, rd);                                                \
}                                                                             \


#define arm_multiply_long_name_s64     SMULL
#define arm_multiply_long_name_u64     UMULL
#define arm_multiply_long_name_s64_add SMLAL
#define arm_multiply_long_name_u64_add UMLAL


#define arm_multiply_long_flags_no(name)                                      \
  ARM_##name(0, _rdlo, _rdhi, _rm, _rs)                                       \

#define arm_multiply_long_flags_yes(name)                                     \
  generate_load_flags();                                                      \
  ARM_##name##S(0, _rdlo, _rdhi, _rm, _rs);                                   \
  generate_store_flags()                                                      \


#define arm_multiply_long_add_no(name)                                        \

#define arm_multiply_long_add_yes(name)                                       \
  prepare_load_reg(reg_a0, rdlo);                                             \
  prepare_load_reg(reg_a1, rdhi)                                              \


#define arm_multiply_long_op(flags, name)                                     \
  arm_multiply_long_flags_##flags(name)                                       \

#define arm_multiply_long(name, add_op, flags)                                \
{                                                                             \
  arm_decode_multiply_long();                                                 \
  u32 _rm = prepare_load_reg(reg_a2, rm);                                     \
  u32 _rs = prepare_load_reg(reg_rs, rs);                                     \
  u32 _rdlo = prepare_store_reg(reg_a0, rdlo);                                \
  u32 _rdhi = prepare_store_reg(reg_a1, rdhi);                                \
  arm_multiply_long_add_##add_op(name);                                       \
  arm_multiply_long_op(flags, arm_multiply_long_name_##name);                 \
  complete_store_reg(_rdlo, rdlo);                                            \
  complete_store_reg(_rdhi, rdhi);                                            \
}                                                                             \

#define arm_psr_read_cpsr()                                                   \
  u32 _rd = prepare_store_reg(reg_a0, rd);                                    \
  generate_load_reg(_rd, REG_CPSR);                                           \
  ARM_BIC_REG_IMM(0, _rd, _rd, 0xF0, arm_imm_lsl_to_rot(24));                 \
  ARM_AND_REG_IMM(0, reg_flags, reg_flags, 0xF0, arm_imm_lsl_to_rot(24));     \
  ARM_ORR_REG_REG(0, _rd, _rd, reg_flags);                                    \
  complete_store_reg(_rd, rd)                                                 \

#define arm_psr_read_spsr()                                                   \
  generate_function_call(execute_read_spsr)                                   \
  generate_store_reg(reg_a0, rd)                                              \

#define arm_psr_read(op_type, psr_reg)                                        \
  arm_psr_read_##psr_reg()                                                    \

// This function's okay because it's called from an ASM function that can
// wrap it correctly.

u32 execute_store_cpsr_body(u32 _cpsr, u32 store_mask, u32 address)
{
  reg[REG_CPSR] = _cpsr;
  if(store_mask & 0xFF)
  {
    set_cpu_mode(cpu_modes[_cpsr & 0x1F]);
    if((io_registers[REG_IE] & io_registers[REG_IF]) &&
     io_registers[REG_IME] && ((_cpsr & 0x80) == 0))
    {
      reg_mode[MODE_IRQ][6] = address + 4;
      spsr[MODE_IRQ] = _cpsr;
      reg[REG_CPSR] = 0xD2;
      set_cpu_mode(MODE_IRQ);
      return 0x00000018;
    }
  }

  return 0;
}

#define arm_psr_load_new_reg()                                                \
  generate_load_reg(reg_a0, rm)                                               \

#define arm_psr_load_new_imm()                                                \
  generate_load_imm(reg_a0, imm, imm_ror)                                     \

#define arm_psr_store_cpsr()                                                  \
  arm_load_imm_32bit(reg_a1, psr_masks[psr_field]);                           \
  generate_function_call(execute_store_cpsr);                                 \
  write32(pc)                                                                 \

#define arm_psr_store_spsr()                                                  \
  generate_function_call(execute_store_spsr)                                  \

#define arm_psr_store(op_type, psr_reg)                                       \
  arm_psr_load_new_##op_type();                                               \
  arm_psr_store_##psr_reg()                                                   \


#define arm_psr(op_type, transfer_type, psr_reg)                              \
{                                                                             \
  arm_decode_psr_##op_type(opcode);                                           \
  arm_psr_##transfer_type(op_type, psr_reg);                                  \
}                                                                             \

// TODO: loads will need the PC passed as well for open address, however can
// eventually be rectified with a hash table on the memory accesses
// (same with the stores)

#define arm_access_memory_load(mem_type)                                      \
  cycle_count += 2;                                                           \
  generate_function_call(execute_load_##mem_type);                            \
  write32((pc + 8));                                                          \
  generate_store_reg_pc_no_flags(reg_rv, rd)                                  \

#define arm_access_memory_store(mem_type)                                     \
  cycle_count++;                                                              \
  generate_load_reg_pc(reg_a1, rd, 12);                                       \
  generate_function_call(execute_store_##mem_type);                           \
  write32((pc + 4))                                                           \

// Calculate the address into a0 from _rn, _rm

#define arm_access_memory_adjust_reg_sh_up(ireg)                              \
  ARM_ADD_REG_IMMSHIFT(0, ireg, _rn, _rm, ((opcode >> 5) & 0x03),             \
   ((opcode >> 7) & 0x1F))                                                    \

#define arm_access_memory_adjust_reg_sh_down(ireg)                            \
  ARM_SUB_REG_IMMSHIFT(0, ireg, _rn, _rm, ((opcode >> 5) & 0x03),             \
   ((opcode >> 7) & 0x1F))                                                    \

#define arm_access_memory_adjust_reg_up(ireg)                                 \
  ARM_ADD_REG_REG(0, ireg, _rn, _rm)                                          \

#define arm_access_memory_adjust_reg_down(ireg)                               \
  ARM_SUB_REG_REG(0, ireg, _rn, _rm)                                          \

#define arm_access_memory_adjust_imm(op, ireg)                                \
{                                                                             \
  u32 stores[4];                                                              \
  u32 rotations[4];                                                           \
  u32 store_count = arm_disect_imm_32bit(offset, stores, rotations);          \
                                                                              \
  if(store_count > 1)                                                         \
  {                                                                           \
    ARM_##op##_REG_IMM(0, ireg, _rn, stores[0], rotations[0]);                \
    ARM_##op##_REG_IMM(0, ireg, ireg, stores[1], rotations[1]);               \
  }                                                                           \
  else                                                                        \
  {                                                                           \
    ARM_##op##_REG_IMM(0, ireg, _rn, stores[0], rotations[0]);                \
  }                                                                           \
}                                                                             \

#define arm_access_memory_adjust_imm_up(ireg)                                 \
  arm_access_memory_adjust_imm(ADD, ireg)                                     \

#define arm_access_memory_adjust_imm_down(ireg)                               \
  arm_access_memory_adjust_imm(SUB, ireg)                                     \


#define arm_access_memory_pre(type, direction)                                \
  arm_access_memory_adjust_##type##_##direction(reg_a0)                       \

#define arm_access_memory_pre_wb(type, direction)                             \
  arm_access_memory_adjust_##type##_##direction(reg_a0);                      \
  generate_store_reg(reg_a0, rn)                                              \

#define arm_access_memory_post(type, direction)                               \
  u32 _rn_dest = prepare_store_reg(reg_a1, rn);                               \
  if(_rn != reg_a0)                                                           \
  {                                                                           \
    generate_load_reg(reg_a0, rn);                                            \
  }                                                                           \
  arm_access_memory_adjust_##type##_##direction(_rn_dest);                    \
  complete_store_reg(_rn_dest, rn)                                            \


#define arm_data_trans_reg(adjust_op, direction)                              \
  arm_decode_data_trans_reg();                                                \
  u32 _rn = prepare_load_reg_pc(reg_a0, rn, 8);                               \
  u32 _rm = prepare_load_reg(reg_a1, rm);                                     \
  arm_access_memory_##adjust_op(reg_sh, direction)                            \

#define arm_data_trans_imm(adjust_op, direction)                              \
  arm_decode_data_trans_imm();                                                \
  u32 _rn = prepare_load_reg_pc(reg_a0, rn, 8);                               \
  arm_access_memory_##adjust_op(imm, direction)                               \


#define arm_data_trans_half_reg(adjust_op, direction)                         \
  arm_decode_half_trans_r();                                                  \
  u32 _rn = prepare_load_reg_pc(reg_a0, rn, 8);                               \
  u32 _rm = prepare_load_reg(reg_a1, rm);                                     \
  arm_access_memory_##adjust_op(reg, direction)                               \

#define arm_data_trans_half_imm(adjust_op, direction)                         \
  arm_decode_half_trans_of();                                                 \
  u32 _rn = prepare_load_reg_pc(reg_a0, rn, 8);                               \
  arm_access_memory_##adjust_op(imm, direction)                               \


#define arm_access_memory(access_type, direction, adjust_op, mem_type,        \
 offset_type)                                                                 \
{                                                                             \
  arm_data_trans_##offset_type(adjust_op, direction);                         \
  arm_access_memory_##access_type(mem_type);                                  \
}                                                                             \


#define word_bit_count(word)                                                  \
  (bit_count[word >> 8] + bit_count[word & 0xFF])                             \

#define sprint_no(access_type, pre_op, post_op, wb)                           \

#define sprint_yes(access_type, pre_op, post_op, wb)                          \
  printf("sbit on %s %s %s %s\n", #access_type, #pre_op, #post_op, #wb)       \


// TODO: Make these use cached registers. Implement iwram_stack_optimize.

#define arm_block_memory_load()                                               \
  generate_function_call(execute_load_u32);                                   \
  write32((pc + 8));                                                          \
  generate_store_reg(reg_rv, i)                                               \

#define arm_block_memory_store()                                              \
  generate_load_reg_pc(reg_a1, i, 8);                                         \
  generate_function_call(execute_store_u32_safe)                              \

#define arm_block_memory_final_load()                                         \
  arm_block_memory_load()                                                     \

#define arm_block_memory_final_store()                                        \
  generate_load_reg_pc(reg_a1, i, 12);                                        \
  generate_function_call(execute_store_u32);                                  \
  write32((pc + 4))                                                           \

#define arm_block_memory_adjust_pc_store()                                    \

#define arm_block_memory_adjust_pc_load()                                     \
  if(reg_list & 0x8000)                                                       \
  {                                                                           \
    generate_mov(reg_a0, reg_rv);                                             \
    generate_indirect_branch_arm();                                           \
  }                                                                           \

#define arm_block_memory_offset_down_a()                                      \
  generate_sub_imm(reg_s0, ((word_bit_count(reg_list) * 4) - 4), 0)           \

#define arm_block_memory_offset_down_b()                                      \
  generate_sub_imm(reg_s0, (word_bit_count(reg_list) * 4), 0)                 \

#define arm_block_memory_offset_no()                                          \

#define arm_block_memory_offset_up()                                          \
  generate_add_imm(reg_s0, 4, 0)                                              \

#define arm_block_memory_writeback_down()                                     \
  generate_load_reg(reg_a0, rn);                                              \
  generate_sub_imm(reg_a0, (word_bit_count(reg_list) * 4), 0);                \
  generate_store_reg(reg_a0, rn)                                              \

#define arm_block_memory_writeback_up()                                       \
  generate_load_reg(reg_a0, rn);                                              \
  generate_add_imm(reg_a0, (word_bit_count(reg_list) * 4), 0);                \
  generate_store_reg(reg_a0, rn)                                              \

#define arm_block_memory_writeback_no()

// Only emit writeback if the register is not in the list

#define arm_block_memory_writeback_load(writeback_type)                       \
  if(!((reg_list >> rn) & 0x01))                                              \
  {                                                                           \
    arm_block_memory_writeback_##writeback_type();                            \
  }                                                                           \

#define arm_block_memory_writeback_store(writeback_type)                      \
  arm_block_memory_writeback_##writeback_type()                               \

#define arm_block_memory(access_type, offset_type, writeback_type, s_bit)     \
{                                                                             \
  arm_decode_block_trans();                                                   \
  u32 offset = 0;                                                             \
  u32 i;                                                                      \
                                                                              \
  generate_load_reg(reg_s0, rn);                                              \
  arm_block_memory_offset_##offset_type();                                    \
  arm_block_memory_writeback_##access_type(writeback_type);                   \
  ARM_BIC_REG_IMM(0, reg_s0, reg_s0, 0x03, 0);                                \
                                                                              \
  for(i = 0; i < 16; i++)                                                     \
  {                                                                           \
    if((reg_list >> i) & 0x01)                                                \
    {                                                                         \
      cycle_count++;                                                          \
      generate_add_reg_reg_imm(reg_a0, reg_s0, offset, 0);                    \
      if(reg_list & ~((2 << i) - 1))                                          \
      {                                                                       \
        arm_block_memory_##access_type();                                     \
        offset += 4;                                                          \
      }                                                                       \
      else                                                                    \
      {                                                                       \
        arm_block_memory_final_##access_type();                               \
        break;                                                                \
      }                                                                       \
    }                                                                         \
  }                                                                           \
                                                                              \
  arm_block_memory_adjust_pc_##access_type();                                 \
}                                                                             \

#define arm_swap(type)                                                        \
{                                                                             \
  arm_decode_swap();                                                          \
  cycle_count += 3;                                                           \
  generate_load_reg(reg_a0, rn);                                              \
  generate_function_call(execute_load_##type);                                \
  write32((pc + 8));                                                          \
  generate_mov(reg_s0, reg_rv);                                               \
  generate_load_reg(reg_a0, rn);                                              \
  generate_load_reg(reg_a1, rm);                                              \
  generate_function_call(execute_store_##type);                               \
  write32((pc + 4));                                                          \
  generate_store_reg(reg_s0, rd);                                             \
}                                                                             \


#define thumb_generate_op_reg(name, _rd, _rs, _rn)                            \
  u32 __rm = prepare_load_reg(reg_rm, _rn);                                   \
  generate_op_##name##_reg_immshift(__rd, __rn, __rm, ARMSHIFT_LSL, 0)        \

#define thumb_generate_op_imm(name, _rd, _rs, imm_)                           \
{                                                                             \
  u32 imm_ror = 0;                                                            \
  generate_op_##name##_imm(__rd, __rn);                                       \
}                                                                             \


#define thumb_data_proc(type, name, op_type, _rd, _rs, _rn)                   \
{                                                                             \
  thumb_decode_##type();                                                      \
  u32 __rn = prepare_load_reg(reg_rn, _rs);                                   \
  u32 __rd = prepare_store_reg(reg_rd, _rd);                                  \
  thumb_generate_op_##op_type(name, _rd, _rs, _rn);                           \
  complete_store_reg(__rd, _rd);                                              \
}                                                                             \

#define thumb_data_proc_test(type, name, op_type, _rd, _rs)                   \
{                                                                             \
  thumb_decode_##type();                                                      \
  u32 __rn = prepare_load_reg(reg_rn, _rd);                                   \
  thumb_generate_op_##op_type(name, 0, _rd, _rs);                             \
}                                                                             \

#define thumb_data_proc_unary(type, name, op_type, _rd, _rs)                  \
{                                                                             \
  thumb_decode_##type();                                                      \
  u32 __rd = prepare_store_reg(reg_rd, _rd);                                  \
  thumb_generate_op_##op_type(name, _rd, 0, _rs);                             \
  complete_store_reg(__rd, _rd);                                              \
}                                                                             \


#define complete_store_reg_pc_thumb()                                         \
  if(rd == 15)                                                                \
  {                                                                           \
    generate_indirect_branch_cycle_update(thumb);                             \
  }                                                                           \
  else                                                                        \
  {                                                                           \
    complete_store_reg(_rd, rd);                                              \
  }                                                                           \

#define thumb_data_proc_hi(name)                                              \
{                                                                             \
  thumb_decode_hireg_op();                                                    \
  u32 _rd = prepare_load_reg_pc(reg_rd, rd, 4);                               \
  u32 _rs = prepare_load_reg_pc(reg_rn, rs, 4);                               \
  generate_op_##name##_reg_immshift(_rd, _rd, _rs, ARMSHIFT_LSL, 0);          \
  complete_store_reg_pc_thumb();                                              \
}                                                                             \

#define thumb_data_proc_test_hi(name)                                         \
{                                                                             \
  thumb_decode_hireg_op();                                                    \
  u32 _rd = prepare_load_reg_pc(reg_rd, rd, 4);                               \
  u32 _rs = prepare_load_reg_pc(reg_rn, rs, 4);                               \
  generate_op_##name##_reg_immshift(0, _rd, _rs, ARMSHIFT_LSL, 0);            \
}                                                                             \

#define thumb_data_proc_mov_hi()                                              \
{                                                                             \
  thumb_decode_hireg_op();                                                    \
  u32 _rs = prepare_load_reg_pc(reg_rn, rs, 4);                               \
  u32 _rd = prepare_store_reg(reg_rd, rd);                                    \
  ARM_MOV_REG_REG(0, _rd, _rs);                                               \
  complete_store_reg_pc_thumb();                                              \
}                                                                             \



#define thumb_load_pc(_rd)                                                    \
{                                                                             \
  thumb_decode_imm();                                                         \
  u32 __rd = prepare_store_reg(reg_rd, _rd);                                  \
  generate_load_pc(__rd, (((pc & ~2) + 4) + (imm * 4)));                      \
  complete_store_reg(__rd, _rd);                                              \
}                                                                             \

#define thumb_load_sp(_rd)                                                    \
{                                                                             \
  thumb_decode_imm();                                                         \
  u32 __sp = prepare_load_reg(reg_a0, REG_SP);                                \
  u32 __rd = prepare_store_reg(reg_a0, _rd);                                  \
  ARM_ADD_REG_IMM(0, __rd, __sp, imm, arm_imm_lsl_to_rot(2));                 \
  complete_store_reg(__rd, _rd);                                              \
}                                                                             \

#define thumb_adjust_sp_up()                                                  \
  ARM_ADD_REG_IMM(0, _sp, _sp, imm, arm_imm_lsl_to_rot(2))                    \

#define thumb_adjust_sp_down()                                                \
  ARM_SUB_REG_IMM(0, _sp, _sp, imm, arm_imm_lsl_to_rot(2))                    \

#define thumb_adjust_sp(direction)                                            \
{                                                                             \
  thumb_decode_add_sp();                                                      \
  u32 _sp = prepare_load_reg(reg_a0, REG_SP);                                 \
  thumb_adjust_sp_##direction();                                              \
  complete_store_reg(_sp, REG_SP);                                            \
}                                                                             \

#define generate_op_lsl_reg(_rd, _rm, _rs)                                    \
  generate_op_movs_reg_regshift(_rd, 0, _rm, ARMSHIFT_LSL, _rs)               \

#define generate_op_lsr_reg(_rd, _rm, _rs)                                    \
  generate_op_movs_reg_regshift(_rd, 0, _rm, ARMSHIFT_LSR, _rs)               \

#define generate_op_asr_reg(_rd, _rm, _rs)                                    \
  generate_op_movs_reg_regshift(_rd, 0, _rm, ARMSHIFT_ASR, _rs)               \

#define generate_op_ror_reg(_rd, _rm, _rs)                                    \
  generate_op_movs_reg_regshift(_rd, 0, _rm, ARMSHIFT_ROR, _rs)               \


#define generate_op_lsl_imm(_rd, _rm)                                         \
  generate_op_movs_reg_immshift(_rd, 0, _rm, ARMSHIFT_LSL, imm)               \

#define generate_op_lsr_imm(_rd, _rm)                                         \
  generate_op_movs_reg_immshift(_rd, 0, _rm, ARMSHIFT_LSR, imm)               \

#define generate_op_asr_imm(_rd, _rm)                                         \
  generate_op_movs_reg_immshift(_rd, 0, _rm, ARMSHIFT_ASR, imm)               \

#define generate_op_ror_imm(_rd, _rm)                                         \
  generate_op_movs_reg_immshift(_rd, 0, _rm, ARMSHIFT_ROR, imm)               \


#define generate_shift_reg(op_type)                                           \
  u32 __rm = prepare_load_reg(reg_rd, rd);                                    \
  u32 __rs = prepare_load_reg(reg_rs, rs);                                    \
  generate_op_##op_type##_reg(__rd, __rm, __rs)                               \

#define generate_shift_imm(op_type)                                           \
  u32 __rs = prepare_load_reg(reg_rs, rs);                                    \
  generate_op_##op_type##_imm(__rd, __rs)                                     \


#define thumb_shift(decode_type, op_type, value_type)                         \
{                                                                             \
  thumb_decode_##decode_type();                                               \
  u32 __rd = prepare_store_reg(reg_rd, rd);                                   \
  generate_shift_##value_type(op_type);                                       \
  complete_store_reg(__rd, rd);                                               \
}                                                                             \

// Operation types: imm, mem_reg, mem_imm

#define thumb_access_memory_load(mem_type, _rd)                               \
  cycle_count += 2;                                                           \
  generate_function_call(execute_load_##mem_type);                            \
  write32((pc + 4));                                                          \
  generate_store_reg(reg_rv, _rd)                                             \

#define thumb_access_memory_store(mem_type, _rd)                              \
  cycle_count++;                                                              \
  generate_load_reg(reg_a1, _rd);                                             \
  generate_function_call(execute_store_##mem_type);                           \
  write32((pc + 2))                                                           \

#define thumb_access_memory_generate_address_pc_relative(offset, _rb, _ro)    \
  generate_load_pc(reg_a0, (offset))                                          \

#define thumb_access_memory_generate_address_reg_imm(offset, _rb, _ro)        \
  u32 __rb = prepare_load_reg(reg_a0, _rb);                                   \
  ARM_ADD_REG_IMM(0, reg_a0, __rb, offset, 0)                                 \

#define thumb_access_memory_generate_address_reg_imm_sp(offset, _rb, _ro)     \
  u32 __rb = prepare_load_reg(reg_a0, _rb);                                   \
  ARM_ADD_REG_IMM(0, reg_a0, __rb, offset, arm_imm_lsl_to_rot(2))             \

#define thumb_access_memory_generate_address_reg_reg(offset, _rb, _ro)        \
  u32 __rb = prepare_load_reg(reg_a0, _rb);                                   \
  u32 __ro = prepare_load_reg(reg_a1, _ro);                                   \
  ARM_ADD_REG_REG(0, reg_a0, __rb, __ro)                                      \

#define thumb_access_memory(access_type, op_type, _rd, _rb, _ro,              \
 address_type, offset, mem_type)                                              \
{                                                                             \
  thumb_decode_##op_type();                                                   \
  thumb_access_memory_generate_address_##address_type(offset, _rb, _ro);      \
  thumb_access_memory_##access_type(mem_type, _rd);                           \
}                                                                             \

// TODO: Make these use cached registers. Implement iwram_stack_optimize.

#define thumb_block_address_preadjust_up()                                    \
  generate_add_imm(reg_s0, (bit_count[reg_list] * 4), 0)                      \

#define thumb_block_address_preadjust_down()                                  \
  generate_sub_imm(reg_s0, (bit_count[reg_list] * 4), 0)                      \

#define thumb_block_address_preadjust_push_lr()                               \
  generate_sub_imm(reg_s0, ((bit_count[reg_list] + 1) * 4), 0)                \

#define thumb_block_address_preadjust_no()                                    \

#define thumb_block_address_postadjust_no(base_reg)                           \
  generate_store_reg(reg_s0, base_reg)                                        \

#define thumb_block_address_postadjust_up(base_reg)                           \
  generate_add_reg_reg_imm(reg_a0, reg_s0, (bit_count[reg_list] * 4), 0);     \
  generate_store_reg(reg_a0, base_reg)                                        \

#define thumb_block_address_postadjust_down(base_reg)                         \
  generate_mov(reg_a0, reg_s0);                                               \
  generate_sub_imm(reg_a0, (bit_count[reg_list] * 4), 0);                     \
  generate_store_reg(reg_a0, base_reg)                                        \

#define thumb_block_address_postadjust_pop_pc(base_reg)                       \
  generate_add_reg_reg_imm(reg_a0, reg_s0,                                    \
   ((bit_count[reg_list] + 1) * 4), 0);                                       \
  generate_store_reg(reg_a0, base_reg)                                        \

#define thumb_block_address_postadjust_push_lr(base_reg)                      \
  generate_store_reg(reg_s0, base_reg)                                        \

#define thumb_block_memory_extra_no()                                         \

#define thumb_block_memory_extra_up()                                         \

#define thumb_block_memory_extra_down()                                       \

#define thumb_block_memory_extra_pop_pc()                                     \
  generate_add_reg_reg_imm(reg_a0, reg_s0, (bit_count[reg_list] * 4), 0);     \
  generate_function_call(execute_load_u32);                                   \
  write32((pc + 4));                                                          \
  generate_mov(reg_a0, reg_rv);                                               \
  generate_indirect_branch_cycle_update(thumb)                                \

#define thumb_block_memory_extra_push_lr(base_reg)                            \
  generate_add_reg_reg_imm(reg_a0, reg_s0, (bit_count[reg_list] * 4), 0);     \
  generate_load_reg(reg_a1, REG_LR);                                          \
  generate_function_call(execute_store_u32_safe)                              \

#define thumb_block_memory_load()                                             \
  generate_function_call(execute_load_u32);                                   \
  write32((pc + 4));                                                          \
  generate_store_reg(reg_rv, i)                                               \

#define thumb_block_memory_store()                                            \
  generate_load_reg(reg_a1, i);                                               \
  generate_function_call(execute_store_u32_safe)                              \

#define thumb_block_memory_final_load()                                       \
  thumb_block_memory_load()                                                   \

#define thumb_block_memory_final_store()                                      \
  generate_load_reg(reg_a1, i);                                               \
  generate_function_call(execute_store_u32);                                  \
  write32((pc + 2))                                                           \

#define thumb_block_memory_final_no(access_type)                              \
  thumb_block_memory_final_##access_type()                                    \

#define thumb_block_memory_final_up(access_type)                              \
  thumb_block_memory_final_##access_type()                                    \

#define thumb_block_memory_final_down(access_type)                            \
  thumb_block_memory_final_##access_type()                                    \

#define thumb_block_memory_final_push_lr(access_type)                         \
  thumb_block_memory_##access_type()                                          \

#define thumb_block_memory_final_pop_pc(access_type)                          \
  thumb_block_memory_##access_type()                                          \

#define thumb_block_memory(access_type, pre_op, post_op, base_reg)            \
{                                                                             \
  thumb_decode_rlist();                                                       \
  u32 i;                                                                      \
  u32 offset = 0;                                                             \
                                                                              \
  generate_load_reg(reg_s0, base_reg);                                        \
  ARM_BIC_REG_IMM(0, reg_s0, reg_s0, 0x03, 0);                                \
  thumb_block_address_preadjust_##pre_op();                                   \
  thumb_block_address_postadjust_##post_op(base_reg);                         \
                                                                              \
  for(i = 0; i < 8; i++)                                                      \
  {                                                                           \
    if((reg_list >> i) & 0x01)                                                \
    {                                                                         \
      cycle_count++;                                                          \
      generate_add_reg_reg_imm(reg_a0, reg_s0, offset, 0);                    \
      if(reg_list & ~((2 << i) - 1))                                          \
      {                                                                       \
        thumb_block_memory_##access_type();                                   \
        offset += 4;                                                          \
      }                                                                       \
      else                                                                    \
      {                                                                       \
        thumb_block_memory_final_##post_op(access_type);                      \
        break;                                                                \
      }                                                                       \
    }                                                                         \
  }                                                                           \
                                                                              \
  thumb_block_memory_extra_##post_op();                                       \
}                                                                             \

#define thumb_conditional_branch(condition)                                   \
{                                                                             \
  generate_cycle_update();                                                    \
  generate_load_flags();                                                      \
  generate_branch_filler(condition_opposite_##condition, backpatch_address);  \
  generate_branch_no_cycle_update(                                            \
   block_exits[block_exit_position].branch_source,                            \
   block_exits[block_exit_position].branch_target, thumb);                    \
  generate_branch_patch_conditional(backpatch_address, translation_ptr);      \
  block_exit_position++;                                                      \
}                                                                             \


#define arm_conditional_block_header()                                        \
  generate_cycle_update();                                                    \
  generate_load_flags();                                                      \
  /* This will choose the opposite condition */                               \
  condition ^= 0x01;                                                          \
  generate_branch_filler(condition, backpatch_address)                        \

#define arm_b()                                                               \
  generate_branch(arm)                                                        \

#define arm_bl()                                                              \
  generate_update_pc((pc + 4));                                               \
  generate_store_reg(reg_a0, REG_LR);                                         \
  generate_branch(arm)                                                        \

#define arm_bx()                                                              \
  arm_decode_branchx(opcode);                                                 \
  generate_load_reg(reg_a0, rn);                                              \
  generate_indirect_branch_dual();                                            \

#define arm_swi()                                                             \
  generate_swi_hle_handler((opcode >> 16) & 0xFF, arm);                       \
  generate_function_call(execute_swi_arm);                                    \
  write32((pc + 4));                                                          \
  generate_branch(arm)                                                        \

#define thumb_b()                                                             \
  generate_branch(thumb)                                                      \

#define thumb_bl()                                                            \
  generate_update_pc(((pc + 2) | 0x01));                                      \
  generate_store_reg(reg_a0, REG_LR);                                         \
  generate_branch(thumb)                                                      \

#define thumb_blh()                                                           \
{                                                                             \
  thumb_decode_branch();                                                      \
  generate_update_pc(((pc + 2) | 0x01));                                      \
  generate_load_reg(reg_a1, REG_LR);                                          \
  generate_store_reg(reg_a0, REG_LR);                                         \
  generate_mov(reg_a0, reg_a1);                                               \
  generate_add_imm(reg_a0, (offset * 2), 0);                                  \
  generate_indirect_branch_cycle_update(thumb);                               \
}                                                                             \

#define thumb_bx()                                                            \
{                                                                             \
  thumb_decode_hireg_op();                                                    \
  generate_load_reg_pc(reg_a0, rs, 4);                                        \
  generate_indirect_branch_cycle_update(dual_thumb);                          \
}                                                                             \

#define thumb_swi()                                                           \
  generate_swi_hle_handler(opcode & 0xFF, thumb);                             \
  generate_function_call(execute_swi_thumb);                                  \
  write32((pc + 2));                                                          \
  /* We're in ARM mode now */                                                 \
  generate_branch(arm)                                                        \

u8 swi_hle_handle[256] =
{
  0x0,    // SWI 0:  SoftReset
  0x0,    // SWI 1:  RegisterRAMReset
  0x0,    // SWI 2:  Halt
  0x0,    // SWI 3:  Stop/Sleep
  0x0,    // SWI 4:  IntrWait
  0x0,    // SWI 5:  VBlankIntrWait
  0x1,    // SWI 6:  Div
  0x0,    // SWI 7:  DivArm
  0x0,    // SWI 8:  Sqrt
  0x0,    // SWI 9:  ArcTan
  0x0,    // SWI A:  ArcTan2
  0x0,    // SWI B:  CpuSet
  0x0,    // SWI C:  CpuFastSet
  0x0,    // SWI D:  GetBIOSCheckSum
  0x0,    // SWI E:  BgAffineSet
  0x0,    // SWI F:  ObjAffineSet
  0x0,    // SWI 10: BitUnpack
  0x0,    // SWI 11: LZ77UnCompWram
  0x0,    // SWI 12: LZ77UnCompVram
  0x0,    // SWI 13: HuffUnComp
  0x0,    // SWI 14: RLUnCompWram
  0x0,    // SWI 15: RLUnCompVram
  0x0,    // SWI 16: Diff8bitUnFilterWram
  0x0,    // SWI 17: Diff8bitUnFilterVram
  0x0,    // SWI 18: Diff16bitUnFilter
  0x0,    // SWI 19: SoundBias
  0x0,    // SWI 1A: SoundDriverInit
  0x0,    // SWI 1B: SoundDriverMode
  0x0,    // SWI 1C: SoundDriverMain
  0x0,    // SWI 1D: SoundDriverVSync
  0x0,    // SWI 1E: SoundChannelClear
  0x0,    // SWI 1F: MidiKey2Freq
  0x0,    // SWI 20: SoundWhatever0
  0x0,    // SWI 21: SoundWhatever1
  0x0,    // SWI 22: SoundWhatever2
  0x0,    // SWI 23: SoundWhatever3
  0x0,    // SWI 24: SoundWhatever4
  0x0,    // SWI 25: MultiBoot
  0x0,    // SWI 26: HardReset
  0x0,    // SWI 27: CustomHalt
  0x0,    // SWI 28: SoundDriverVSyncOff
  0x0,    // SWI 29: SoundDriverVSyncOn
  0x0     // SWI 2A: SoundGetJumpList
};

void execute_swi_hle_div_arm();
void execute_swi_hle_div_thumb();

void execute_swi_hle_div_c()
{
  if (reg[1] == 0)
    // real BIOS supposedly locks up, but game can recover on interrupt
    return;
  s32 result = (s32)reg[0] / (s32)reg[1];
  reg[1] = (s32)reg[0] % (s32)reg[1];
  reg[0] = result;

  reg[3] = (result ^ (result >> 31)) - (result >> 31);
}

#define generate_swi_hle_handler(_swi_number, mode)                           \
{                                                                             \
  u32 swi_number = _swi_number;                                               \
  if(swi_hle_handle[swi_number])                                              \
  {                                                                           \
    /* Div */                                                                 \
    if(swi_number == 0x06)                                                    \
    {                                                                         \
      generate_function_call(execute_swi_hle_div_##mode);                     \
    }                                                                         \
    break;                                                                    \
  }                                                                           \
}                                                                             \

#define generate_translation_gate(type)                                       \
  generate_update_pc(pc);                                                     \
  generate_indirect_branch_no_cycle_update(type)                              \

#endif