summaryrefslogtreecommitdiffstats
path: root/libpixelflinger/codeflinger/Arm64Assembler.cpp
blob: f37072aa5acc34e682f40eda3c438d83b24bc3c0 (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
/*
 * Copyright (C) 2013 The Android Open Source Project
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#define LOG_TAG "ArmToArm64Assembler"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <cutils/log.h>
#include <cutils/properties.h>
#include <private/pixelflinger/ggl_context.h>

#include "codeflinger/Arm64Assembler.h"
#include "codeflinger/CodeCache.h"
#include "codeflinger/Arm64Disassembler.h"


/*
** --------------------------------------------
** Support for Arm64 in GGLAssembler JIT
** --------------------------------------------
**
** Approach
** - GGLAssembler and associated files are largely un-changed.
** - A translator class maps ArmAssemblerInterface calls to
**   generate Arm64 instructions.
**
** ----------------------
** ArmToArm64Assembler
** ----------------------
**
** - Subclassed from ArmAssemblerInterface
**
** - Translates each ArmAssemblerInterface call to generate
**   one or more Arm64 instructions  as necessary.
**
** - Does not implement ArmAssemblerInterface portions unused by GGLAssembler
**   It calls NOT_IMPLEMENTED() for such cases, which in turn logs
**    a fatal message.
**
** - Uses A64_.. series of functions to generate instruction machine code
**   for Arm64 instructions. These functions also log the instruction
**   to LOG, if ARM64_ASM_DEBUG define is set to 1
**
** - Dumps machine code and eqvt assembly if "debug.pf.disasm" option is set
**   It uses arm64_disassemble to perform disassembly
**
** - Uses register 13 (SP in ARM), 15 (PC in ARM), 16, 17 for storing
**   intermediate results. GGLAssembler does not use SP and PC as these
**   registers are marked as reserved. The temporary registers are not
**   saved/restored on stack as these are caller-saved registers in Arm64
**
** - Uses CSEL instruction to support conditional execution. The result is
**   stored in a temporary register and then copied to the target register
**   if the condition is true.
**
** - In the case of conditional data transfer instructions, conditional
**   branch is used to skip over instruction, if the condition is false
**
** - Wherever possible, immediate values are transferred to temporary
**   register prior to processing. This simplifies overall implementation
**   as instructions requiring immediate values are converted to
**   move immediate instructions followed by register-register instruction.
**
** --------------------------------------------
** ArmToArm64Assembler unit test bench
** --------------------------------------------
**
** - Tests ArmToArm64Assembler interface for all the possible
**   ways in which GGLAssembler uses ArmAssemblerInterface interface.
**
** - Uses test jacket (written in assembly) to set the registers,
**   condition flags prior to calling generated instruction. It also
**   copies registers and flags at the end of execution. Caller then
**   checks if generated code performed correct operation based on
**   output registers and flags.
**
** - Broadly contains three type of tests, (i) data operation tests
**   (ii) data transfer tests and (iii) LDM/STM tests.
**
** ----------------------
** Arm64 disassembler
** ----------------------
** - This disassembler disassembles only those machine codes which can be
**   generated by ArmToArm64Assembler. It has a unit testbench which
**   tests all the instructions supported by the disassembler.
**
** ------------------------------------------------------------------
** ARMAssembler/ARMAssemblerInterface/ARMAssemblerProxy changes
** ------------------------------------------------------------------
**
** - In existing code, addresses were being handled as 32 bit values at
**   certain places.
**
** - Added a new set of functions for address load/store/manipulation.
**   These are ADDR_LDR, ADDR_STR, ADDR_ADD, ADDR_SUB and they map to
**   default 32 bit implementations in ARMAssemblerInterface.
**
** - ArmToArm64Assembler maps these functions to appropriate 64 bit
**   functions.
**
** ----------------------
** GGLAssembler changes
** ----------------------
** - Since ArmToArm64Assembler can generate 4 Arm64 instructions for
**   each call in worst case, the memory required is set to 4 times
**   ARM memory
**
** - Address load/store/manipulation were changed to use new functions
**   added in the ARMAssemblerInterface.
**
*/


#define NOT_IMPLEMENTED()  LOG_FATAL("Arm instruction %s not yet implemented\n", __func__)

#define ARM64_ASM_DEBUG 0

#if ARM64_ASM_DEBUG
    #define LOG_INSTR(...) ALOGD("\t" __VA_ARGS__)
    #define LOG_LABEL(...) ALOGD(__VA_ARGS__)
#else
    #define LOG_INSTR(...) ((void)0)
    #define LOG_LABEL(...) ((void)0)
#endif

namespace android {

static const char* shift_codes[] =
{
    "LSL", "LSR", "ASR", "ROR"
};
static const char *cc_codes[] =
{
    "EQ", "NE", "CS", "CC", "MI",
    "PL", "VS", "VC", "HI", "LS",
    "GE", "LT", "GT", "LE", "AL", "NV"
};

ArmToArm64Assembler::ArmToArm64Assembler(const sp<Assembly>& assembly)
    :   ARMAssemblerInterface(),
        mAssembly(assembly)
{
    mBase = mPC = (uint32_t *)assembly->base();
    mDuration = ggl_system_time();
    mZeroReg = 13;
    mTmpReg1 = 15;
    mTmpReg2 = 16;
    mTmpReg3 = 17;
}

ArmToArm64Assembler::ArmToArm64Assembler(void *base)
    :   ARMAssemblerInterface(), mAssembly(NULL)
{
    mBase = mPC = (uint32_t *)base;
    mDuration = ggl_system_time();
    // Regs 13, 15, 16, 17 are used as temporary registers
    mZeroReg = 13;
    mTmpReg1 = 15;
    mTmpReg2 = 16;
    mTmpReg3 = 17;
}

ArmToArm64Assembler::~ArmToArm64Assembler()
{
}

uint32_t* ArmToArm64Assembler::pc() const
{
    return mPC;
}

uint32_t* ArmToArm64Assembler::base() const
{
    return mBase;
}

void ArmToArm64Assembler::reset()
{
    if(mAssembly == NULL)
        mPC = mBase;
    else
        mBase = mPC = (uint32_t *)mAssembly->base();
    mBranchTargets.clear();
    mLabels.clear();
    mLabelsInverseMapping.clear();
    mComments.clear();
#if ARM64_ASM_DEBUG
    ALOGI("RESET\n");
#endif
}

int ArmToArm64Assembler::getCodegenArch()
{
    return CODEGEN_ARCH_ARM64;
}

// ----------------------------------------------------------------------------

void ArmToArm64Assembler::disassemble(const char* name)
{
    if(name)
    {
        printf("%s:\n", name);
    }
    size_t count = pc()-base();
    uint32_t* i = base();
    while (count--)
    {
        ssize_t label = mLabelsInverseMapping.indexOfKey(i);
        if (label >= 0)
        {
            printf("%s:\n", mLabelsInverseMapping.valueAt(label));
        }
        ssize_t comment = mComments.indexOfKey(i);
        if (comment >= 0)
        {
            printf("; %s\n", mComments.valueAt(comment));
        }
        printf("%p:    %08x    ", i, uint32_t(i[0]));
        {
            char instr[256];
            ::arm64_disassemble(*i, instr);
            printf("%s\n", instr);
        }
        i++;
    }
}

void ArmToArm64Assembler::comment(const char* string)
{
    mComments.add(mPC, string);
    LOG_INSTR("//%s\n", string);
}

void ArmToArm64Assembler::label(const char* theLabel)
{
    mLabels.add(theLabel, mPC);
    mLabelsInverseMapping.add(mPC, theLabel);
    LOG_LABEL("%s:\n", theLabel);
}

void ArmToArm64Assembler::B(int cc, const char* label)
{
    mBranchTargets.add(branch_target_t(label, mPC));
    LOG_INSTR("B%s %s\n", cc_codes[cc], label );
    *mPC++ = (0x54 << 24) | cc;
}

void ArmToArm64Assembler::BL(int cc, const char* label)
{
    NOT_IMPLEMENTED(); //Not Required
}

// ----------------------------------------------------------------------------
//Prolog/Epilog & Generate...
// ----------------------------------------------------------------------------

void ArmToArm64Assembler::prolog()
{
    // write prolog code
    mPrologPC = mPC;
    *mPC++ = A64_MOVZ_X(mZeroReg,0,0);
}

void ArmToArm64Assembler::epilog(uint32_t touched)
{
    // write epilog code
    static const int XLR = 30;
    *mPC++ = A64_RET(XLR);
}

int ArmToArm64Assembler::generate(const char* name)
{
    // fixup all the branches
    size_t count = mBranchTargets.size();
    while (count--)
    {
        const branch_target_t& bt = mBranchTargets[count];
        uint32_t* target_pc = mLabels.valueFor(bt.label);
        LOG_ALWAYS_FATAL_IF(!target_pc,
                "error resolving branch targets, target_pc is null");
        int32_t offset = int32_t(target_pc - bt.pc);
        *bt.pc |= (offset & 0x7FFFF) << 5;
    }

    if(mAssembly != NULL)
        mAssembly->resize( int(pc()-base())*4 );

    // the instruction cache is flushed by CodeCache
    const int64_t duration = ggl_system_time() - mDuration;
    const char * const format = "generated %s (%d ins) at [%p:%p] in %ld ns\n";
    ALOGI(format, name, int(pc()-base()), base(), pc(), duration);


    char value[PROPERTY_VALUE_MAX];
    property_get("debug.pf.disasm", value, "0");
    if (atoi(value) != 0)
    {
        printf(format, name, int(pc()-base()), base(), pc(), duration);
        disassemble(name);
    }
    return NO_ERROR;
}

uint32_t* ArmToArm64Assembler::pcForLabel(const char* label)
{
    return mLabels.valueFor(label);
}

// ----------------------------------------------------------------------------
// Data Processing...
// ----------------------------------------------------------------------------
void ArmToArm64Assembler::dataProcessingCommon(int opcode,
        int s, int Rd, int Rn, uint32_t Op2)
{
    if(opcode != opSUB && s == 1)
    {
        NOT_IMPLEMENTED(); //Not required
        return;
    }

    if(opcode != opSUB && opcode != opADD && opcode != opAND &&
       opcode != opORR && opcode != opMVN)
    {
        NOT_IMPLEMENTED(); //Not required
        return;
    }

    if(Op2 == OPERAND_REG_IMM && mAddrMode.reg_imm_shift > 31)
        {
        NOT_IMPLEMENTED();
        return;
    }

    //Store immediate in temporary register and convert
    //immediate operation into register operation
    if(Op2 == OPERAND_IMM)
    {
        int imm = mAddrMode.immediate;
        *mPC++ = A64_MOVZ_W(mTmpReg2, imm & 0x0000FFFF, 0);
        *mPC++ = A64_MOVK_W(mTmpReg2, (imm >> 16) & 0x0000FFFF, 16);
        Op2 = mTmpReg2;
    }


    {
        uint32_t shift;
        uint32_t amount;
        uint32_t Rm;

        if(Op2 == OPERAND_REG_IMM)
        {
            shift   = mAddrMode.reg_imm_type;
            amount  = mAddrMode.reg_imm_shift;
            Rm      = mAddrMode.reg_imm_Rm;
        }
        else if(Op2 < OPERAND_REG)
        {
            shift   = 0;
            amount  = 0;
            Rm      = Op2;
        }
        else
        {
            NOT_IMPLEMENTED(); //Not required
            return;
        }

        switch(opcode)
        {
            case opADD: *mPC++ = A64_ADD_W(Rd, Rn, Rm, shift, amount); break;
            case opAND: *mPC++ = A64_AND_W(Rd, Rn, Rm, shift, amount); break;
            case opORR: *mPC++ = A64_ORR_W(Rd, Rn, Rm, shift, amount); break;
            case opMVN: *mPC++ = A64_ORN_W(Rd, Rn, Rm, shift, amount); break;
            case opSUB: *mPC++ = A64_SUB_W(Rd, Rn, Rm, shift, amount, s);break;
        };

    }
}

void ArmToArm64Assembler::dataProcessing(int opcode, int cc,
        int s, int Rd, int Rn, uint32_t Op2)
{
    uint32_t Wd;

    if(cc != AL)
        Wd = mTmpReg1;
    else
        Wd = Rd;

    if(opcode == opADD || opcode == opAND || opcode == opORR ||opcode == opSUB)
    {
        dataProcessingCommon(opcode, s, Wd, Rn, Op2);
    }
    else if(opcode == opCMP)
    {
        dataProcessingCommon(opSUB, 1, mTmpReg3, Rn, Op2);
    }
    else if(opcode == opRSB)
    {
        dataProcessingCommon(opSUB, s, Wd, Rn, Op2);
        dataProcessingCommon(opSUB, s, Wd, mZeroReg, Wd);
    }
    else if(opcode == opMOV)
    {
        dataProcessingCommon(opORR, 0, Wd, mZeroReg, Op2);
        if(s == 1)
        {
            dataProcessingCommon(opSUB, 1, mTmpReg3, Wd, mZeroReg);
        }
    }
    else if(opcode == opMVN)
    {
        dataProcessingCommon(opMVN, s, Wd, mZeroReg, Op2);
    }
    else if(opcode == opBIC)
    {
        dataProcessingCommon(opMVN, s, mTmpReg3, mZeroReg, Op2);
        dataProcessingCommon(opAND, s, Wd, Rn, mTmpReg3);
    }
    else
    {
        NOT_IMPLEMENTED();
        return;
    }

    if(cc != AL)
    {
        *mPC++ = A64_CSEL_W(Rd, mTmpReg1, Rd, cc);
    }
}
// ----------------------------------------------------------------------------
// Address Processing...
// ----------------------------------------------------------------------------

void ArmToArm64Assembler::ADDR_ADD(int cc,
        int s, int Rd, int Rn, uint32_t Op2)
{
    if(cc != AL){ NOT_IMPLEMENTED(); return;} //Not required
    if(s  != 0) { NOT_IMPLEMENTED(); return;} //Not required


    if(Op2 == OPERAND_REG_IMM && mAddrMode.reg_imm_type == LSL)
    {
        int Rm = mAddrMode.reg_imm_Rm;
        int amount = mAddrMode.reg_imm_shift;
        *mPC++ = A64_ADD_X_Wm_SXTW(Rd, Rn, Rm, amount);
    }
    else if(Op2 < OPERAND_REG)
    {
        int Rm = Op2;
        int amount = 0;
        *mPC++ = A64_ADD_X_Wm_SXTW(Rd, Rn, Rm, amount);
    }
    else if(Op2 == OPERAND_IMM)
    {
        int imm = mAddrMode.immediate;
        *mPC++ = A64_MOVZ_W(mTmpReg1, imm & 0x0000FFFF, 0);
        *mPC++ = A64_MOVK_W(mTmpReg1, (imm >> 16) & 0x0000FFFF, 16);

        int Rm = mTmpReg1;
        int amount = 0;
        *mPC++ = A64_ADD_X_Wm_SXTW(Rd, Rn, Rm, amount);
    }
    else
    {
        NOT_IMPLEMENTED(); //Not required
    }
}

void ArmToArm64Assembler::ADDR_SUB(int cc,
        int s, int Rd, int Rn, uint32_t Op2)
{
    if(cc != AL){ NOT_IMPLEMENTED(); return;} //Not required
    if(s  != 0) { NOT_IMPLEMENTED(); return;} //Not required

    if(Op2 == OPERAND_REG_IMM && mAddrMode.reg_imm_type == LSR)
    {
        *mPC++ = A64_ADD_W(mTmpReg1, mZeroReg, mAddrMode.reg_imm_Rm,
                           LSR, mAddrMode.reg_imm_shift);
        *mPC++ = A64_SUB_X_Wm_SXTW(Rd, Rn, mTmpReg1, 0);
    }
    else
    {
        NOT_IMPLEMENTED(); //Not required
    }
}

// ----------------------------------------------------------------------------
// multiply...
// ----------------------------------------------------------------------------
void ArmToArm64Assembler::MLA(int cc, int s,int Rd, int Rm, int Rs, int Rn)
{
    if(cc != AL){ NOT_IMPLEMENTED(); return;} //Not required

    *mPC++ = A64_MADD_W(Rd, Rm, Rs, Rn);
    if(s == 1)
        dataProcessingCommon(opSUB, 1, mTmpReg1, Rd, mZeroReg);
}
void ArmToArm64Assembler::MUL(int cc, int s, int Rd, int Rm, int Rs)
{
    if(cc != AL){ NOT_IMPLEMENTED(); return;} //Not required
    if(s  != 0) { NOT_IMPLEMENTED(); return;} //Not required
    *mPC++ = A64_MADD_W(Rd, Rm, Rs, mZeroReg);
}
void ArmToArm64Assembler::UMULL(int cc, int s,
        int RdLo, int RdHi, int Rm, int Rs)
{
    NOT_IMPLEMENTED(); //Not required
}
void ArmToArm64Assembler::UMUAL(int cc, int s,
        int RdLo, int RdHi, int Rm, int Rs)
{
    NOT_IMPLEMENTED(); //Not required
}
void ArmToArm64Assembler::SMULL(int cc, int s,
        int RdLo, int RdHi, int Rm, int Rs)
{
    NOT_IMPLEMENTED(); //Not required
}
void ArmToArm64Assembler::SMUAL(int cc, int s,
        int RdLo, int RdHi, int Rm, int Rs)
{
    NOT_IMPLEMENTED(); //Not required
}

// ----------------------------------------------------------------------------
// branches relative to PC...
// ----------------------------------------------------------------------------
void ArmToArm64Assembler::B(int cc, uint32_t* pc){
    NOT_IMPLEMENTED(); //Not required
}

void ArmToArm64Assembler::BL(int cc, uint32_t* pc){
    NOT_IMPLEMENTED(); //Not required
}

void ArmToArm64Assembler::BX(int cc, int Rn){
    NOT_IMPLEMENTED(); //Not required
}

// ----------------------------------------------------------------------------
// data transfer...
// ----------------------------------------------------------------------------
enum dataTransferOp
{
    opLDR,opLDRB,opLDRH,opSTR,opSTRB,opSTRH
};

void ArmToArm64Assembler::dataTransfer(int op, int cc,
                            int Rd, int Rn, uint32_t op_type, uint32_t size)
{
    const int XSP = 31;
    if(Rn == SP)
        Rn = XSP;

    if(op_type == OPERAND_IMM)
    {
        int addrReg;
        int imm = mAddrMode.immediate;
        if(imm >= 0 && imm < (1<<12))
            *mPC++ = A64_ADD_IMM_X(mTmpReg1, mZeroReg, imm, 0);
        else if(imm < 0 && -imm < (1<<12))
            *mPC++ = A64_SUB_IMM_X(mTmpReg1, mZeroReg, -imm, 0);
        else
        {
            NOT_IMPLEMENTED();
            return;
        }

        addrReg = Rn;
        if(mAddrMode.preindex == true || mAddrMode.postindex == true)
        {
            *mPC++ = A64_ADD_X(mTmpReg2, addrReg, mTmpReg1);
            if(mAddrMode.preindex == true)
                addrReg = mTmpReg2;
        }

        if(cc != AL)
            *mPC++ = A64_B_COND(cc^1, 8);

        *mPC++ = A64_LDRSTR_Wm_SXTW_0(op, size, Rd, addrReg, mZeroReg);

        if(mAddrMode.writeback == true)
            *mPC++ = A64_CSEL_X(Rn, mTmpReg2, Rn, cc);
    }
    else if(op_type == OPERAND_REG_OFFSET)
    {
        if(cc != AL)
            *mPC++ = A64_B_COND(cc^1, 8);
        *mPC++ = A64_LDRSTR_Wm_SXTW_0(op, size, Rd, Rn, mAddrMode.reg_offset);

    }
    else if(op_type > OPERAND_UNSUPPORTED)
    {
        if(cc != AL)
            *mPC++ = A64_B_COND(cc^1, 8);
        *mPC++ = A64_LDRSTR_Wm_SXTW_0(op, size, Rd, Rn, mZeroReg);
    }
    else
    {
        NOT_IMPLEMENTED(); // Not required
    }
    return;

}
void ArmToArm64Assembler::ADDR_LDR(int cc, int Rd, int Rn, uint32_t op_type)
{
    return dataTransfer(opLDR, cc, Rd, Rn, op_type, 64);
}
void ArmToArm64Assembler::ADDR_STR(int cc, int Rd, int Rn, uint32_t op_type)
{
    return dataTransfer(opSTR, cc, Rd, Rn, op_type, 64);
}
void ArmToArm64Assembler::LDR(int cc, int Rd, int Rn, uint32_t op_type)
{
    return dataTransfer(opLDR, cc, Rd, Rn, op_type);
}
void ArmToArm64Assembler::LDRB(int cc, int Rd, int Rn, uint32_t op_type)
{
    return dataTransfer(opLDRB, cc, Rd, Rn, op_type);
}
void ArmToArm64Assembler::STR(int cc, int Rd, int Rn, uint32_t op_type)
{
    return dataTransfer(opSTR, cc, Rd, Rn, op_type);
}

void ArmToArm64Assembler::STRB(int cc, int Rd, int Rn, uint32_t op_type)
{
    return dataTransfer(opSTRB, cc, Rd, Rn, op_type);
}

void ArmToArm64Assembler::LDRH(int cc, int Rd, int Rn, uint32_t op_type)
{
    return dataTransfer(opLDRH, cc, Rd, Rn, op_type);
}
void ArmToArm64Assembler::LDRSB(int cc, int Rd, int Rn, uint32_t offset)
{
    NOT_IMPLEMENTED(); //Not required
}
void ArmToArm64Assembler::LDRSH(int cc, int Rd, int Rn, uint32_t offset)
{
    NOT_IMPLEMENTED(); //Not required
}

void ArmToArm64Assembler::STRH(int cc, int Rd, int Rn, uint32_t op_type)
{
    return dataTransfer(opSTRH, cc, Rd, Rn, op_type);
}

// ----------------------------------------------------------------------------
// block data transfer...
// ----------------------------------------------------------------------------
void ArmToArm64Assembler::LDM(int cc, int dir,
        int Rn, int W, uint32_t reg_list)
{
    const int XSP = 31;
    if(cc != AL || dir != IA || W == 0 || Rn != SP)
    {
        NOT_IMPLEMENTED();
        return;
    }

    for(int i = 0; i < 32; ++i)
    {
        if((reg_list & (1 << i)))
        {
            int reg = i;
            int size = 16;
            *mPC++ = A64_LDR_IMM_PostIndex(reg, XSP, size);
        }
    }
}

void ArmToArm64Assembler::STM(int cc, int dir,
        int Rn, int W, uint32_t reg_list)
{
    const int XSP = 31;
    if(cc != AL || dir != DB || W == 0 || Rn != SP)
    {
        NOT_IMPLEMENTED();
        return;
    }

    for(int i = 31; i >= 0; --i)
    {
        if((reg_list & (1 << i)))
        {
            int size = -16;
            int reg  = i;
            *mPC++ = A64_STR_IMM_PreIndex(reg, XSP, size);
        }
    }
}

// ----------------------------------------------------------------------------
// special...
// ----------------------------------------------------------------------------
void ArmToArm64Assembler::SWP(int cc, int Rn, int Rd, int Rm)
{
    NOT_IMPLEMENTED(); //Not required
}
void ArmToArm64Assembler::SWPB(int cc, int Rn, int Rd, int Rm)
{
    NOT_IMPLEMENTED(); //Not required
}
void ArmToArm64Assembler::SWI(int cc, uint32_t comment)
{
    NOT_IMPLEMENTED(); //Not required
}

// ----------------------------------------------------------------------------
// DSP instructions...
// ----------------------------------------------------------------------------
void ArmToArm64Assembler::PLD(int Rn, uint32_t offset) {
    NOT_IMPLEMENTED(); //Not required
}

void ArmToArm64Assembler::CLZ(int cc, int Rd, int Rm)
{
    NOT_IMPLEMENTED(); //Not required
}

void ArmToArm64Assembler::QADD(int cc,  int Rd, int Rm, int Rn)
{
    NOT_IMPLEMENTED(); //Not required
}

void ArmToArm64Assembler::QDADD(int cc,  int Rd, int Rm, int Rn)
{
    NOT_IMPLEMENTED(); //Not required
}

void ArmToArm64Assembler::QSUB(int cc,  int Rd, int Rm, int Rn)
{
    NOT_IMPLEMENTED(); //Not required
}

void ArmToArm64Assembler::QDSUB(int cc,  int Rd, int Rm, int Rn)
{
    NOT_IMPLEMENTED(); //Not required
}

// ----------------------------------------------------------------------------
// 16 x 16 multiplication
// ----------------------------------------------------------------------------
void ArmToArm64Assembler::SMUL(int cc, int xy,
                int Rd, int Rm, int Rs)
{
    if(cc != AL){ NOT_IMPLEMENTED(); return;} //Not required

    if (xy & xyTB)
        *mPC++ = A64_SBFM_W(mTmpReg1, Rm, 16, 31);
    else
        *mPC++ = A64_SBFM_W(mTmpReg1, Rm, 0, 15);

    if (xy & xyBT)
        *mPC++ = A64_SBFM_W(mTmpReg2, Rs, 16, 31);
    else
        *mPC++ = A64_SBFM_W(mTmpReg2, Rs, 0, 15);

    *mPC++ = A64_MADD_W(Rd,mTmpReg1,mTmpReg2, mZeroReg);
}
// ----------------------------------------------------------------------------
// 32 x 16 multiplication
// ----------------------------------------------------------------------------
void ArmToArm64Assembler::SMULW(int cc, int y, int Rd, int Rm, int Rs)
{
    if(cc != AL){ NOT_IMPLEMENTED(); return;} //Not required

    if (y & yT)
        *mPC++ = A64_SBFM_W(mTmpReg1, Rs, 16, 31);
    else
        *mPC++ = A64_SBFM_W(mTmpReg1, Rs, 0, 15);

    *mPC++ = A64_SBFM_W(mTmpReg2, Rm, 0, 31);
    *mPC++ = A64_SMADDL(mTmpReg3,mTmpReg1,mTmpReg2, mZeroReg);
    *mPC++ = A64_UBFM_X(Rd,mTmpReg3, 16, 47);
}
// ----------------------------------------------------------------------------
// 16 x 16 multiplication and accumulate
// ----------------------------------------------------------------------------
void ArmToArm64Assembler::SMLA(int cc, int xy, int Rd, int Rm, int Rs, int Rn)
{
    if(cc != AL){ NOT_IMPLEMENTED(); return;} //Not required
    if(xy != xyBB) { NOT_IMPLEMENTED(); return;} //Not required

    *mPC++ = A64_SBFM_W(mTmpReg1, Rm, 0, 15);
    *mPC++ = A64_SBFM_W(mTmpReg2, Rs, 0, 15);
    *mPC++ = A64_MADD_W(Rd, mTmpReg1, mTmpReg2, Rn);
}

void ArmToArm64Assembler::SMLAL(int cc, int xy,
                int RdHi, int RdLo, int Rs, int Rm)
{
    NOT_IMPLEMENTED(); //Not required
    return;
}

void ArmToArm64Assembler::SMLAW(int cc, int y,
                int Rd, int Rm, int Rs, int Rn)
{
    NOT_IMPLEMENTED(); //Not required
    return;
}

// ----------------------------------------------------------------------------
// Byte/half word extract and extend
// ----------------------------------------------------------------------------
void ArmToArm64Assembler::UXTB16(int cc, int Rd, int Rm, int rotate)
{
    if(cc != AL){ NOT_IMPLEMENTED(); return;} //Not required

    *mPC++ = A64_EXTR_W(mTmpReg1, Rm, Rm, rotate * 8);

    uint32_t imm = 0x00FF00FF;
    *mPC++ = A64_MOVZ_W(mTmpReg2, imm & 0xFFFF, 0);
    *mPC++ = A64_MOVK_W(mTmpReg2, (imm >> 16) & 0x0000FFFF, 16);
    *mPC++ = A64_AND_W(Rd,mTmpReg1, mTmpReg2);
}

// ----------------------------------------------------------------------------
// Bit manipulation
// ----------------------------------------------------------------------------
void ArmToArm64Assembler::UBFX(int cc, int Rd, int Rn, int lsb, int width)
{
    if(cc != AL){ NOT_IMPLEMENTED(); return;} //Not required
    *mPC++ = A64_UBFM_W(Rd, Rn, lsb, lsb + width - 1);
}
// ----------------------------------------------------------------------------
// Shifters...
// ----------------------------------------------------------------------------
int ArmToArm64Assembler::buildImmediate(
        uint32_t immediate, uint32_t& rot, uint32_t& imm)
{
    rot = 0;
    imm = immediate;
    return 0; // Always true
}


bool ArmToArm64Assembler::isValidImmediate(uint32_t immediate)
{
    uint32_t rot, imm;
    return buildImmediate(immediate, rot, imm) == 0;
}

uint32_t ArmToArm64Assembler::imm(uint32_t immediate)
{
    mAddrMode.immediate = immediate;
    mAddrMode.writeback = false;
    mAddrMode.preindex  = false;
    mAddrMode.postindex = false;
    return OPERAND_IMM;

}

uint32_t ArmToArm64Assembler::reg_imm(int Rm, int type, uint32_t shift)
{
    mAddrMode.reg_imm_Rm = Rm;
    mAddrMode.reg_imm_type = type;
    mAddrMode.reg_imm_shift = shift;
    return OPERAND_REG_IMM;
}

uint32_t ArmToArm64Assembler::reg_rrx(int Rm)
{
    NOT_IMPLEMENTED();
    return OPERAND_UNSUPPORTED;
}

uint32_t ArmToArm64Assembler::reg_reg(int Rm, int type, int Rs)
{
    NOT_IMPLEMENTED(); //Not required
    return OPERAND_UNSUPPORTED;
}
// ----------------------------------------------------------------------------
// Addressing modes...
// ----------------------------------------------------------------------------
uint32_t ArmToArm64Assembler::immed12_pre(int32_t immed12, int W)
{
    mAddrMode.immediate = immed12;
    mAddrMode.writeback = W;
    mAddrMode.preindex  = true;
    mAddrMode.postindex = false;
    return OPERAND_IMM;
}

uint32_t ArmToArm64Assembler::immed12_post(int32_t immed12)
{
    mAddrMode.immediate = immed12;
    mAddrMode.writeback = true;
    mAddrMode.preindex  = false;
    mAddrMode.postindex = true;
    return OPERAND_IMM;
}

uint32_t ArmToArm64Assembler::reg_scale_pre(int Rm, int type,
        uint32_t shift, int W)
{
    if(type != 0 || shift != 0 || W != 0)
    {
        NOT_IMPLEMENTED(); //Not required
        return OPERAND_UNSUPPORTED;
    }
    else
    {
        mAddrMode.reg_offset = Rm;
        return OPERAND_REG_OFFSET;
    }
}

uint32_t ArmToArm64Assembler::reg_scale_post(int Rm, int type, uint32_t shift)
{
    NOT_IMPLEMENTED(); //Not required
    return OPERAND_UNSUPPORTED;
}

uint32_t ArmToArm64Assembler::immed8_pre(int32_t immed8, int W)
{
    mAddrMode.immediate = immed8;
    mAddrMode.writeback = W;
    mAddrMode.preindex  = true;
    mAddrMode.postindex = false;
    return OPERAND_IMM;
}

uint32_t ArmToArm64Assembler::immed8_post(int32_t immed8)
{
    mAddrMode.immediate = immed8;
    mAddrMode.writeback = true;
    mAddrMode.preindex  = false;
    mAddrMode.postindex = true;
    return OPERAND_IMM;
}

uint32_t ArmToArm64Assembler::reg_pre(int Rm, int W)
{
    if(W != 0)
    {
        NOT_IMPLEMENTED(); //Not required
        return OPERAND_UNSUPPORTED;
    }
    else
    {
        mAddrMode.reg_offset = Rm;
        return OPERAND_REG_OFFSET;
    }
}

uint32_t ArmToArm64Assembler::reg_post(int Rm)
{
    NOT_IMPLEMENTED(); //Not required
    return OPERAND_UNSUPPORTED;
}

// ----------------------------------------------------------------------------
// A64 instructions
// ----------------------------------------------------------------------------

static const char * dataTransferOpName[] =
{
    "LDR","LDRB","LDRH","STR","STRB","STRH"
};

static const uint32_t dataTransferOpCode [] =
{
    ((0xB8u << 24) | (0x3 << 21) | (0x6 << 13) | (0x0 << 12) |(0x1 << 11)),
    ((0x38u << 24) | (0x3 << 21) | (0x6 << 13) | (0x1 << 12) |(0x1 << 11)),
    ((0x78u << 24) | (0x3 << 21) | (0x6 << 13) | (0x0 << 12) |(0x1 << 11)),
    ((0xB8u << 24) | (0x1 << 21) | (0x6 << 13) | (0x0 << 12) |(0x1 << 11)),
    ((0x38u << 24) | (0x1 << 21) | (0x6 << 13) | (0x1 << 12) |(0x1 << 11)),
    ((0x78u << 24) | (0x1 << 21) | (0x6 << 13) | (0x0 << 12) |(0x1 << 11))
};
uint32_t ArmToArm64Assembler::A64_LDRSTR_Wm_SXTW_0(uint32_t op,
                            uint32_t size, uint32_t Rt,
                            uint32_t Rn, uint32_t Rm)
{
    if(size == 32)
    {
        LOG_INSTR("%s W%d, [X%d, W%d, SXTW #0]\n",
                   dataTransferOpName[op], Rt, Rn, Rm);
        return(dataTransferOpCode[op] | (Rm << 16) | (Rn << 5) | Rt);
    }
    else
    {
        LOG_INSTR("%s X%d, [X%d, W%d, SXTW #0]\n",
                  dataTransferOpName[op], Rt, Rn, Rm);
        return(dataTransferOpCode[op] | (0x1<<30) | (Rm<<16) | (Rn<<5)|Rt);
    }
}

uint32_t ArmToArm64Assembler::A64_STR_IMM_PreIndex(uint32_t Rt,
                            uint32_t Rn, int32_t simm)
{
    if(Rn == 31)
        LOG_INSTR("STR W%d, [SP, #%d]!\n", Rt, simm);
    else
        LOG_INSTR("STR W%d, [X%d, #%d]!\n", Rt, Rn, simm);

    uint32_t imm9 = (unsigned)(simm) & 0x01FF;
    return (0xB8 << 24) | (imm9 << 12) | (0x3 << 10) | (Rn << 5) | Rt;
}

uint32_t ArmToArm64Assembler::A64_LDR_IMM_PostIndex(uint32_t Rt,
                            uint32_t Rn, int32_t simm)
{
    if(Rn == 31)
        LOG_INSTR("LDR W%d, [SP], #%d\n",Rt,simm);
    else
        LOG_INSTR("LDR W%d, [X%d], #%d\n",Rt, Rn, simm);

    uint32_t imm9 = (unsigned)(simm) & 0x01FF;
    return (0xB8 << 24) | (0x1 << 22) |
             (imm9 << 12) | (0x1 << 10) | (Rn << 5) | Rt;

}
uint32_t ArmToArm64Assembler::A64_ADD_X_Wm_SXTW(uint32_t Rd,
                               uint32_t Rn,
                               uint32_t Rm,
                               uint32_t amount)
{
    LOG_INSTR("ADD X%d, X%d, W%d, SXTW #%d\n", Rd, Rn, Rm, amount);
    return ((0x8B << 24) | (0x1 << 21) |(Rm << 16) |
              (0x6 << 13) | (amount << 10) | (Rn << 5) | Rd);

}

uint32_t ArmToArm64Assembler::A64_SUB_X_Wm_SXTW(uint32_t Rd,
                               uint32_t Rn,
                               uint32_t Rm,
                               uint32_t amount)
{
    LOG_INSTR("SUB X%d, X%d, W%d, SXTW #%d\n", Rd, Rn, Rm, amount);
    return ((0xCB << 24) | (0x1 << 21) |(Rm << 16) |
            (0x6 << 13) | (amount << 10) | (Rn << 5) | Rd);

}

uint32_t ArmToArm64Assembler::A64_B_COND(uint32_t cc, uint32_t offset)
{
    LOG_INSTR("B.%s #.+%d\n", cc_codes[cc], offset);
    return (0x54 << 24) | ((offset/4) << 5) | (cc);

}
uint32_t ArmToArm64Assembler::A64_ADD_X(uint32_t Rd, uint32_t Rn,
                                          uint32_t Rm, uint32_t shift,
                                          uint32_t amount)
{
    LOG_INSTR("ADD X%d, X%d, X%d, %s #%d\n",
               Rd, Rn, Rm, shift_codes[shift], amount);
    return ((0x8B << 24) | (shift << 22) | ( Rm << 16) |
            (amount << 10) |(Rn << 5) | Rd);
}
uint32_t ArmToArm64Assembler::A64_ADD_IMM_X(uint32_t Rd, uint32_t Rn,
                                          uint32_t imm, uint32_t shift)
{
    LOG_INSTR("ADD X%d, X%d, #%d, LSL #%d\n", Rd, Rn, imm, shift);
    return (0x91 << 24) | ((shift/12) << 22) | (imm << 10) | (Rn << 5) | Rd;
}

uint32_t ArmToArm64Assembler::A64_SUB_IMM_X(uint32_t Rd, uint32_t Rn,
                                          uint32_t imm, uint32_t shift)
{
    LOG_INSTR("SUB X%d, X%d, #%d, LSL #%d\n", Rd, Rn, imm, shift);
    return (0xD1 << 24) | ((shift/12) << 22) | (imm << 10) | (Rn << 5) | Rd;
}

uint32_t ArmToArm64Assembler::A64_ADD_W(uint32_t Rd, uint32_t Rn,
                                          uint32_t Rm, uint32_t shift,
                                          uint32_t amount)
{
    LOG_INSTR("ADD W%d, W%d, W%d, %s #%d\n",
               Rd, Rn, Rm, shift_codes[shift], amount);
    return ((0x0B << 24) | (shift << 22) | ( Rm << 16) |
            (amount << 10) |(Rn << 5) | Rd);
}

uint32_t ArmToArm64Assembler::A64_SUB_W(uint32_t Rd, uint32_t Rn,
                                          uint32_t Rm, uint32_t shift,
                                          uint32_t amount,
                                          uint32_t setflag)
{
    if(setflag == 0)
    {
        LOG_INSTR("SUB W%d, W%d, W%d, %s #%d\n",
               Rd, Rn, Rm, shift_codes[shift], amount);
        return ((0x4B << 24) | (shift << 22) | ( Rm << 16) |
                (amount << 10) |(Rn << 5) | Rd);
    }
    else
    {
        LOG_INSTR("SUBS W%d, W%d, W%d, %s #%d\n",
                   Rd, Rn, Rm, shift_codes[shift], amount);
        return ((0x6B << 24) | (shift << 22) | ( Rm << 16) |
                (amount << 10) |(Rn << 5) | Rd);
    }
}

uint32_t ArmToArm64Assembler::A64_AND_W(uint32_t Rd, uint32_t Rn,
                                          uint32_t Rm, uint32_t shift,
                                          uint32_t amount)
{
    LOG_INSTR("AND W%d, W%d, W%d, %s #%d\n",
               Rd, Rn, Rm, shift_codes[shift], amount);
    return ((0x0A << 24) | (shift << 22) | ( Rm << 16) |
            (amount << 10) |(Rn << 5) | Rd);
}

uint32_t ArmToArm64Assembler::A64_ORR_W(uint32_t Rd, uint32_t Rn,
                                          uint32_t Rm, uint32_t shift,
                                          uint32_t amount)
{
    LOG_INSTR("ORR W%d, W%d, W%d, %s #%d\n",
               Rd, Rn, Rm, shift_codes[shift], amount);
    return ((0x2A << 24) | (shift << 22) | ( Rm << 16) |
            (amount << 10) |(Rn << 5) | Rd);
}

uint32_t ArmToArm64Assembler::A64_ORN_W(uint32_t Rd, uint32_t Rn,
                                          uint32_t Rm, uint32_t shift,
                                          uint32_t amount)
{
    LOG_INSTR("ORN W%d, W%d, W%d, %s #%d\n",
               Rd, Rn, Rm, shift_codes[shift], amount);
    return ((0x2A << 24) | (shift << 22) | (0x1 << 21) | ( Rm << 16) |
            (amount << 10) |(Rn << 5) | Rd);
}

uint32_t ArmToArm64Assembler::A64_CSEL_X(uint32_t Rd, uint32_t Rn,
                                           uint32_t Rm, uint32_t cond)
{
    LOG_INSTR("CSEL X%d, X%d, X%d, %s\n", Rd, Rn, Rm, cc_codes[cond]);
    return ((0x9A << 24)|(0x1 << 23)|(Rm << 16) |(cond << 12)| (Rn << 5) | Rd);
}

uint32_t ArmToArm64Assembler::A64_CSEL_W(uint32_t Rd, uint32_t Rn,
                                           uint32_t Rm, uint32_t cond)
{
    LOG_INSTR("CSEL W%d, W%d, W%d, %s\n", Rd, Rn, Rm, cc_codes[cond]);
    return ((0x1A << 24)|(0x1 << 23)|(Rm << 16) |(cond << 12)| (Rn << 5) | Rd);
}

uint32_t ArmToArm64Assembler::A64_RET(uint32_t Rn)
{
    LOG_INSTR("RET X%d\n", Rn);
    return ((0xD6 << 24) | (0x1 << 22) | (0x1F << 16) | (Rn << 5));
}

uint32_t ArmToArm64Assembler::A64_MOVZ_X(uint32_t Rd, uint32_t imm,
                                         uint32_t shift)
{
    LOG_INSTR("MOVZ X%d, #0x%x, LSL #%d\n", Rd, imm, shift);
    return(0xD2 << 24) | (0x1 << 23) | ((shift/16) << 21) |  (imm << 5) | Rd;
}

uint32_t ArmToArm64Assembler::A64_MOVK_W(uint32_t Rd, uint32_t imm,
                                         uint32_t shift)
{
    LOG_INSTR("MOVK W%d, #0x%x, LSL #%d\n", Rd, imm, shift);
    return (0x72 << 24) | (0x1 << 23) | ((shift/16) << 21) | (imm << 5) | Rd;
}

uint32_t ArmToArm64Assembler::A64_MOVZ_W(uint32_t Rd, uint32_t imm,
                                         uint32_t shift)
{
    LOG_INSTR("MOVZ W%d, #0x%x, LSL #%d\n", Rd, imm, shift);
    return(0x52 << 24) | (0x1 << 23) | ((shift/16) << 21) |  (imm << 5) | Rd;
}

uint32_t ArmToArm64Assembler::A64_SMADDL(uint32_t Rd, uint32_t Rn,
                                           uint32_t Rm, uint32_t Ra)
{
    LOG_INSTR("SMADDL X%d, W%d, W%d, X%d\n",Rd, Rn, Rm, Ra);
    return ((0x9B << 24) | (0x1 << 21) | (Rm << 16)|(Ra << 10)|(Rn << 5) | Rd);
}

uint32_t ArmToArm64Assembler::A64_MADD_W(uint32_t Rd, uint32_t Rn,
                                           uint32_t Rm, uint32_t Ra)
{
    LOG_INSTR("MADD W%d, W%d, W%d, W%d\n",Rd, Rn, Rm, Ra);
    return ((0x1B << 24) | (Rm << 16) | (Ra << 10) |(Rn << 5) | Rd);
}

uint32_t ArmToArm64Assembler::A64_SBFM_W(uint32_t Rd, uint32_t Rn,
                                           uint32_t immr, uint32_t imms)
{
    LOG_INSTR("SBFM W%d, W%d, #%d, #%d\n", Rd, Rn, immr, imms);
    return ((0x13 << 24) | (immr << 16) | (imms << 10) | (Rn << 5) | Rd);

}
uint32_t ArmToArm64Assembler::A64_UBFM_W(uint32_t Rd, uint32_t Rn,
                                           uint32_t immr, uint32_t imms)
{
    LOG_INSTR("UBFM W%d, W%d, #%d, #%d\n", Rd, Rn, immr, imms);
    return ((0x53 << 24) | (immr << 16) | (imms << 10) | (Rn << 5) | Rd);

}
uint32_t ArmToArm64Assembler::A64_UBFM_X(uint32_t Rd, uint32_t Rn,
                                           uint32_t immr, uint32_t imms)
{
    LOG_INSTR("UBFM X%d, X%d, #%d, #%d\n", Rd, Rn, immr, imms);
    return ((0xD3 << 24) | (0x1 << 22) |
            (immr << 16) | (imms << 10) | (Rn << 5) | Rd);

}
uint32_t ArmToArm64Assembler::A64_EXTR_W(uint32_t Rd, uint32_t Rn,
                                           uint32_t Rm, uint32_t lsb)
{
    LOG_INSTR("EXTR W%d, W%d, W%d, #%d\n", Rd, Rn, Rm, lsb);
    return (0x13 << 24)|(0x1 << 23) | (Rm << 16) | (lsb << 10)|(Rn << 5) | Rd;
}

}; // namespace android