12 #define LOC_LOCAL 0x10
16 #define REG_PC 15 /* program counter */
17 #define REG_LR 14 /* link register */
18 #define REG_SP 13 /* stack pointer */
19 #define REG_TMP 12 /* temporary register */
20 #define REG_FP 11 /* frame pointer register */
21 #define REG_DP 10 /* data pointer register */
22 #define REG_RET 0 /* returned value register */
23 #define FORK_REG 0 /* result of conditional branches */
25 #define MIN(a, b) ((a) < (b) ? (a) : (b))
26 #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
28 static char cs
[SECSIZE
]; /* code segment */
30 static char ds
[SECSIZE
]; /* data segment */
32 static long bsslen
; /* bss segment size */
34 static int nogen
; /* don't generate code */
39 #define TMP(i) (((i) < ntmp) ? &tmps[ntmp - 1 - (i)] : NULL)
44 long off
; /* offset from a symbol or a local */
45 unsigned loc
; /* variable location */
46 unsigned bt
; /* type of address; zero when not a pointer */
57 static void i_ldr(int l
, int rd
, int rn
, int off
, int bt
);
58 static void i_add(int op
, int rd
, int rn
, int rm
);
59 static void i_add_imm(int op
, int rd
, int rn
, long n
);
60 static void i_num(int rd
, long n
);
61 static void i_add_anyimm(int rd
, int rn
, long n
);
62 static void i_mul(int rd
, int rn
, int rm
);
63 static void i_cmp(int op
, int rn
, int rm
);
64 static void i_cmp_imm(int op
, int rn
, long n
);
65 static int i_decodeable(long imm
);
66 static void i_set(int cond
, int rd
);
67 static void i_shl(int sm
, int rd
, int rm
, int rs
);
68 static void i_shl_imm(int sm
, int rd
, int rn
, long n
);
69 static void i_mov(int rd
, int rn
);
70 static void i_ldr(int l
, int rd
, int rn
, int off
, int bt
);
71 static void i_sym(int rd
, char *sym
, int off
);
72 static void i_neg(int rd
);
73 static void i_not(int rd
);
74 static void i_lnot(int rd
);
75 static void i_zx(int rd
, int bits
);
76 static void i_sx(int rd
, int bits
);
77 static void i_b(long addr
);
78 static void i_b_if(long addr
, int rn
, int z
);
79 static void i_b_fill(long *dst
, int diff
);
80 static void i_memcpy(int rd
, int rs
, int rn
);
81 static void i_memset(int rd
, int rs
, int rn
);
82 static void i_call_reg(int rd
);
83 static void i_call(char *sym
, int off
);
84 static void i_prolog(void);
85 static void i_epilog(void);
87 static struct tmp
*regs
[NREGS
];
88 static int tmpregs
[] = {4, 5, 6, 7, 8, 9, 0, 1, 2, 3};
89 static int argregs
[] = {0, 1, 2, 3};
102 #define MAXRET (1 << 8)
104 static long ret
[MAXRET
];
107 /* output div/mod functions */
108 static int putdiv
= 0;
110 static void os(void *s
, int n
)
112 memcpy(cs
+ cslen
, s
, n
);
116 static void oi(long n
)
120 *(int *) (cs
+ cslen
) = n
;
124 static long sp_push(int size
)
132 static void tmp_mem(struct tmp
*tmp
)
135 if (!(tmp
->loc
== LOC_REG
))
139 tmp
->addr
= -sp_push(LONGSZ
);
140 i_ldr(0, src
, REG_FP
, tmp
->addr
, LONGSZ
);
145 static void num_cast(struct tmp
*t
, unsigned bt
)
147 if (!(bt
& BT_SIGNED
) && BT_SZ(bt
) != LONGSZ
)
148 t
->addr
&= ((1l << (long) (BT_SZ(bt
) * 8)) - 1);
149 if (bt
& BT_SIGNED
&& BT_SZ(bt
) != LONGSZ
&&
150 t
->addr
> (1l << (BT_SZ(bt
) * 8 - 1)))
151 t
->addr
= -((1l << (BT_SZ(bt
) * 8)) - t
->addr
);
154 static void tmp_reg(struct tmp
*tmp
, int dst
, int deref
)
161 if (tmp
->loc
== LOC_NUM
) {
162 i_num(dst
, tmp
->addr
);
167 if (tmp
->loc
== LOC_SYM
) {
168 i_sym(dst
, tmp
->sym
, tmp
->off
);
173 if (tmp
->loc
== LOC_REG
) {
175 i_ldr(1, dst
, tmp
->addr
, 0, bt
);
176 else if (dst
!= tmp
->addr
)
177 i_mov(dst
, tmp
->addr
);
178 regs
[tmp
->addr
] = NULL
;
180 if (tmp
->loc
== LOC_LOCAL
) {
182 i_ldr(1, dst
, REG_FP
, tmp
->addr
+ tmp
->off
, bt
);
184 i_add_anyimm(dst
, REG_FP
, tmp
->addr
+ tmp
->off
);
186 if (tmp
->loc
== LOC_MEM
) {
187 i_ldr(1, dst
, REG_FP
, tmp
->addr
, LONGSZ
);
189 i_ldr(1, dst
, dst
, 0, bt
);
196 static void reg_free(int reg
)
201 for (i
= 0; i
< ARRAY_SIZE(tmpregs
); i
++)
202 if (!regs
[tmpregs
[i
]]) {
203 tmp_reg(regs
[reg
], tmpregs
[i
], 0);
209 static void reg_for(int reg
, struct tmp
*t
)
211 if (regs
[reg
] && regs
[reg
] != t
)
215 static void tmp_mv(struct tmp
*t
, int reg
)
221 static void tmp_to(struct tmp
*t
, int reg
)
227 static void tmp_drop(int n
)
230 for (i
= ntmp
- n
; i
< ntmp
; i
++)
231 if (tmps
[i
].loc
== LOC_REG
)
232 regs
[tmps
[i
].addr
] = NULL
;
236 static void tmp_pop(int reg
)
238 struct tmp
*t
= TMP(0);
243 static struct tmp
*tmp_new(void)
245 return &tmps
[ntmp
++];
248 static void tmp_push(int reg
)
250 struct tmp
*t
= tmp_new();
257 void o_local(long addr
)
259 struct tmp
*t
= tmp_new();
268 struct tmp
*t
= tmp_new();
274 void o_sym(char *name
)
276 struct tmp
*t
= tmp_new();
277 strcpy(t
->sym
, name
);
283 void o_tmpdrop(int n
)
285 if (n
== -1 || n
> ntmp
)
295 /* make sure tmps remain intact after a conditional expression */
299 for (i
= 0; i
< ntmp
- 1; i
++)
303 void o_forkpush(void)
308 void o_forkjoin(void)
315 struct tmp
*t1
= TMP(0);
316 struct tmp
*t2
= TMP(1);
318 memcpy(&t
, t1
, sizeof(t
));
319 memcpy(t1
, t2
, sizeof(t
));
320 memcpy(t2
, &t
, sizeof(t
));
321 if (t1
->loc
== LOC_REG
)
323 if (t2
->loc
== LOC_REG
)
327 static int reg_get(int mask
)
330 for (i
= 0; i
< ARRAY_SIZE(tmpregs
); i
++)
331 if ((1 << tmpregs
[i
]) & mask
&& !regs
[tmpregs
[i
]])
333 for (i
= 0; i
< ARRAY_SIZE(tmpregs
); i
++)
334 if ((1 << tmpregs
[i
]) & mask
) {
335 reg_free(tmpregs
[i
]);
341 static int reg_fortmp(struct tmp
*t
, int notmask
)
343 if (t
->loc
== LOC_REG
&& !(notmask
& (1 << t
->addr
)))
345 return reg_get(~notmask
);
348 static void tmp_copy(struct tmp
*t1
)
350 struct tmp
*t2
= tmp_new();
351 memcpy(t2
, t1
, sizeof(*t1
));
352 if (!(t1
->loc
& (LOC_REG
| LOC_MEM
)))
354 if (t1
->loc
== LOC_MEM
) {
355 tmp_mv(t2
, reg_get(~0));
356 } else if (t1
->loc
== LOC_REG
) {
357 t2
->addr
= reg_fortmp(t2
, 1 << t1
->addr
);
358 i_mov(t2
->addr
, t1
->addr
);
368 void o_cast(unsigned bt
)
370 struct tmp
*t
= TMP(0);
371 if (!t
->bt
&& t
->loc
== LOC_NUM
) {
375 if (BT_SZ(bt
) != LONGSZ
) {
376 int reg
= reg_fortmp(t
, 0);
379 i_sx(reg
, BT_SZ(bt
) * 8);
381 i_zx(reg
, BT_SZ(bt
) * 8);
385 void o_func_beg(char *name
, int argc
, int global
, int vararg
)
387 out_sym(name
, (global
? OUT_GLOB
: 0) | OUT_CS
, cslen
, 0);
394 memset(regs
, 0, sizeof(regs
));
397 void o_deref(unsigned bt
)
399 struct tmp
*t
= TMP(0);
401 tmp_to(t
, reg_fortmp(t
, 0));
407 struct tmp
*t
= TMP(0);
408 tmp_to(t
, reg_fortmp(t
, 0));
411 #define TMP_NUM(t) ((t)->loc == LOC_NUM && !(t)->bt)
412 #define LOCAL_PTR(t) ((t)->loc == LOC_LOCAL && !(t)->bt)
413 #define SYM_PTR(t) ((t)->loc == LOC_SYM && !(t)->bt)
415 int o_popnum(long *c
)
417 struct tmp
*t
= TMP(0);
431 ret
[nret
++] = o_jmp(0);
434 void o_func_end(void)
437 for (i
= 0; i
< nret
; i
++)
442 long o_mklocal(int size
)
444 return sp_push(ALIGN(size
, LONGSZ
));
447 void o_rmlocal(long addr
, int sz
)
452 long o_arg2loc(int i
)
454 return -(10 + i
) << 2;
457 void o_assign(unsigned bt
)
459 struct tmp
*t1
= TMP(0);
460 struct tmp
*t2
= TMP(1);
461 int r1
= reg_fortmp(t1
, 0);
462 int r2
= reg_fortmp(t2
, 1 << r1
);
467 if (t2
->loc
== LOC_LOCAL
) {
469 off
= t2
->addr
+ t2
->off
;
474 i_ldr(0, r1
, r2
, off
, bt
);
478 static long cu(int op
, long i
)
491 static int c_uop(int op
)
493 struct tmp
*t1
= TMP(0);
497 o_num(cu(op
, t1
->addr
));
501 static long cb(int op
, long a
, long b
)
526 return (unsigned long) a
>> b
;
543 static int c_bop(int op
)
545 struct tmp
*t1
= TMP(0);
546 struct tmp
*t2
= TMP(1);
547 int locals
= LOCAL_PTR(t1
) + LOCAL_PTR(t2
);
548 int syms
= SYM_PTR(t1
) + SYM_PTR(t2
);
549 int nums
= TMP_NUM(t1
) + TMP_NUM(t2
);
550 if (syms
+ locals
== 2 || syms
+ nums
+ locals
!= 2)
553 if ((op
& 0xff) != O_ADD
&& ((op
& 0xff) != O_SUB
|| TMP_NUM(t2
)))
556 long o1
= TMP_NUM(t1
) ? t1
->addr
: t1
->off
;
557 long o2
= TMP_NUM(t2
) ? t2
->addr
: t2
->off
;
558 long ret
= cb(op
, o2
, o1
);
564 long ret
= cb(op
, t2
->addr
, t1
->addr
);
573 int r1
= reg_fortmp(TMP(0), 0);
590 static void bin_regs(int *r1
, int *r2
)
592 struct tmp
*t2
= TMP(0);
593 struct tmp
*t1
= TMP(1);
594 *r2
= reg_fortmp(t2
, 0);
596 *r1
= reg_fortmp(t1
, 1 << *r2
);
601 static int bop_imm(int *r1
, long *n
, int swap
)
603 struct tmp
*t1
= TMP(0);
604 struct tmp
*t2
= TMP(1);
605 if (!TMP_NUM(t1
) && (!swap
|| !TMP_NUM(t2
)))
607 *n
= TMP_NUM(t1
) ? t1
->addr
: t2
->addr
;
608 if (!i_decodeable(*n
))
612 *r1
= reg_fortmp(t2
, 0);
618 static void bin_add(int op
)
620 /* opcode for O_ADD, O_SUB, O_AND, O_OR, O_XOR */
621 static int rx
[] = {I_ADD
, I_SUB
, I_AND
, I_ORR
, I_EOR
};
624 if (!bop_imm(&r1
, &n
, (op
& 0xff) != O_SUB
)) {
625 i_add_imm(rx
[op
& 0x0f], r1
, r1
, n
);
628 i_add(rx
[op
& 0x0f], r1
, r1
, r2
);
633 static void bin_shx(int op
)
638 if ((op
& 0x0f) == 1)
639 sm
= op
& O_SIGNED
? SM_ASR
: SM_LSR
;
640 if (!bop_imm(&r1
, &n
, 0)) {
641 i_shl_imm(sm
, r1
, r1
, n
);
644 i_shl(sm
, r1
, r1
, r2
);
649 static int log2a(unsigned long n
)
652 for (i
= 0; i
< LONGSZ
* 8; i
++)
655 if (i
== LONGSZ
* 8 || !(n
>> (i
+ 1)))
660 /* optimized version of mul/div/mod for powers of two */
661 static int mul_2(int op
)
663 struct tmp
*t1
= TMP(0);
664 struct tmp
*t2
= TMP(1);
668 if ((op
& 0xff) == O_MUL
&& t2
->loc
== LOC_NUM
&& !t2
->bt
)
670 if (t1
->loc
!= LOC_NUM
|| t1
->bt
)
676 if ((op
& 0xff) == O_MUL
) {
685 r2
= reg_fortmp(t2
, 0);
687 i_shl_imm(SM_LSL
, r2
, r2
, p
);
694 r2
= reg_fortmp(t2
, 0);
696 i_shl_imm(op
& O_SIGNED
? SM_ASR
: SM_LSR
, r2
, r2
, p
);
706 r2
= reg_fortmp(t2
, 0);
714 static void bin_div(int op
)
716 struct tmp
*t2
= TMP(0);
717 struct tmp
*t1
= TMP(1);
721 if ((op
& 0xff) == O_DIV
)
722 func
= op
& O_SIGNED
? "__divdi3" : "__udivdi3";
724 func
= op
& O_SIGNED
? "__moddi3" : "__umoddi3";
725 for (i
= 0; i
< ARRAY_SIZE(argregs
); i
++)
726 if (regs
[argregs
[i
]] && regs
[argregs
[i
]] - tmps
< ntmp
- 2)
727 tmp_mem(regs
[argregs
[i
]]);
728 tmp_to(t1
, argregs
[0]);
729 tmp_to(t2
, argregs
[1]);
735 static void bin_mul(int op
)
740 if ((op
& 0xff) == O_DIV
|| (op
& 0xff) == O_MOD
) {
749 static void bin_cmp(int op
)
751 /* lt, gt, le, ge, eq, neq */
752 static int ucond
[] = {3, 8, 9, 2, 0, 1};
753 static int scond
[] = {11, 12, 13, 10, 0, 1};
756 if (!bop_imm(&r1
, &n
, (op
& 0xff) == O_EQ
|| (op
& 0xff) == O_NEQ
)) {
757 i_cmp_imm(I_CMP
, r1
, n
);
760 i_cmp(I_CMP
, r1
, r2
);
762 i_set(op
& O_SIGNED
? scond
[op
& 0x0f] : ucond
[op
& 0x0f], r1
);
770 if ((op
& 0xf0) == 0x00)
772 if ((op
& 0xf0) == 0x10)
774 if ((op
& 0xf0) == 0x20)
776 if ((op
& 0xf0) == 0x30)
780 static void load_regs2(int *r0
, int *r1
, int *r2
)
782 struct tmp
*t0
= TMP(0);
783 struct tmp
*t1
= TMP(1);
784 struct tmp
*t2
= TMP(2);
785 *r0
= reg_fortmp(t0
, 0);
786 *r1
= reg_fortmp(t1
, 1 << *r0
);
787 *r2
= reg_fortmp(t2
, (1 << *r0
) | (1 << *r1
));
796 load_regs2(&rn
, &rs
, &rd
);
797 i_memcpy(rd
, rs
, rn
);
804 load_regs2(&rn
, &rs
, &rd
);
805 i_memset(rd
, rs
, rn
);
814 static long jxz(long addr
, int z
)
816 int r
= reg_fortmp(TMP(0), 0);
827 long o_jnz(long addr
)
832 long o_jmp(long addr
)
838 void o_filljmp2(long addr
, long jmpdst
)
840 i_b_fill((void *) cs
+ addr
, jmpdst
- addr
);
843 void o_filljmp(long addr
)
845 o_filljmp2(addr
, cslen
);
848 void o_call(int argc
, int rets
)
852 int aregs
= MIN(ARRAY_SIZE(argregs
), argc
);
853 for (i
= 0; i
< ARRAY_SIZE(argregs
); i
++)
854 if (regs
[argregs
[i
]] && regs
[argregs
[i
]] - tmps
< ntmp
- argc
)
855 tmp_mem(regs
[argregs
[i
]]);
857 sp_push(LONGSZ
* (argc
- aregs
));
858 for (i
= argc
- 1; i
>= aregs
; --i
) {
859 int reg
= reg_fortmp(TMP(0), 0);
861 i_ldr(0, reg
, REG_SP
, (i
- aregs
) * LONGSZ
, LONGSZ
);
864 for (i
= aregs
- 1; i
>= 0; --i
)
865 tmp_to(TMP(aregs
- i
- 1), argregs
[i
]);
868 if (t
->loc
== LOC_SYM
&& !t
->bt
) {
869 i_call(t
->sym
, t
->off
);
872 int reg
= t
->loc
== LOC_REG
? t
->addr
: REG_TMP
;
890 void o_mkbss(char *name
, int size
, int global
)
892 out_sym(name
, OUT_BSS
| (global
? OUT_GLOB
: 0), bsslen
, size
);
893 bsslen
+= ALIGN(size
, LONGSZ
);
896 #define MAXDATS (1 << 10)
897 static char dat_names
[MAXDATS
][NAMELEN
];
898 static int dat_offs
[MAXDATS
];
902 void *o_mkdat(char *name
, int size
, int global
)
904 void *addr
= ds
+ dslen
;
907 err("nomem: MAXDATS reached!\n");
908 strcpy(dat_names
[idx
], name
);
909 dat_offs
[idx
] = dslen
;
910 out_sym(name
, OUT_DS
| (global
? OUT_GLOB
: 0), dslen
, size
);
911 dslen
+= ALIGN(size
, LONGSZ
);
915 static int dat_off(char *name
)
918 for (i
= 0; i
< ndats
; i
++)
919 if (!strcmp(name
, dat_names
[i
]))
924 void o_datset(char *name
, int off
, unsigned bt
)
926 struct tmp
*t
= TMP(0);
927 int sym_off
= dat_off(name
) + off
;
928 if (t
->loc
== LOC_NUM
&& !t
->bt
) {
930 memcpy(ds
+ sym_off
, &t
->addr
, BT_SZ(bt
));
932 if (t
->loc
== LOC_SYM
&& !t
->bt
) {
933 out_rel(t
->sym
, OUT_DS
, sym_off
);
934 memcpy(ds
+ sym_off
, &t
->off
, BT_SZ(bt
));
939 /* compiled division functions; div.s contains the source */
940 static int udivdi3
[] = {
941 0xe3a02000, 0xe3a03000, 0xe1110001, 0x0a00000a,
942 0xe1b0c211, 0xe2822001, 0x5afffffc, 0xe3a0c001,
943 0xe2522001, 0x4a000004, 0xe1500211, 0x3afffffb,
944 0xe0400211, 0xe083321c, 0xeafffff8, 0xe1a01000,
945 0xe1a00003, 0xe1a0f00e,
947 static int umoddi3
[] = {
948 0xe92d4000, 0xebffffeb, 0xe1a00001, 0xe8bd8000,
950 static int divdi3
[] = {
951 0xe92d4030, 0xe1a04000, 0xe1a05001, 0xe1100000,
952 0x42600000, 0xe1110001, 0x42611000, 0xebffffe1,
953 0xe1340005, 0x42600000, 0xe1140004, 0x42611000,
956 static int moddi3
[] = {
957 0xe92d4000, 0xebfffff0, 0xe1a00001, 0xe8bd8000,
963 out_sym("__udivdi3", OUT_CS
, cslen
, 0);
964 os(udivdi3
, sizeof(udivdi3
));
965 out_sym("__umoddi3", OUT_CS
, cslen
, 0);
966 os(umoddi3
, sizeof(umoddi3
));
967 out_sym("__divdi3", OUT_CS
, cslen
, 0);
968 os(divdi3
, sizeof(divdi3
));
969 out_sym("__moddi3", OUT_CS
, cslen
, 0);
970 os(moddi3
, sizeof(moddi3
));
972 out_write(fd
, cs
, cslen
, ds
, dslen
);
978 static long num_offs
[MAXNUMS
]; /* data immediate value */
979 static char num_names
[MAXNUMS
][NAMELEN
]; /* relocation data symbol name */
982 static int pool_find(char *name
, int off
)
985 for (i
= 0; i
< nums
; i
++)
986 if (!strcmp(name
, num_names
[i
]) && off
== num_offs
[i
])
991 static int pool_num(long num
)
993 int idx
= pool_find("", num
);
997 num_names
[idx
][0] = '\0';
1002 static int pool_reloc(char *name
, long off
)
1004 int idx
= pool_find(name
, off
);
1007 num_offs
[idx
] = off
;
1008 strcpy(num_names
[idx
], name
);
1013 static void pool_write(void)
1016 for (i
= 0; i
< nums
; i
++) {
1018 out_rel(num_names
[i
], OUT_CS
, cslen
);
1025 * +---------------------------------------+
1026 * |COND|00|I| op |S| Rn | Rd | operand2 |
1027 * +---------------------------------------+
1029 * S: set condition code
1031 * Rd: destination operand
1033 * I=0 operand2=| shift | Rm |
1034 * I=1 operand2=|rota| imm |
1036 #define ADD(op, rd, rn, s, i, cond) \
1037 (((cond) << 28) | ((i) << 25) | ((s) << 20) | \
1038 ((op) << 21) | ((rn) << 16) | ((rd) << 12))
1040 static int add_encimm(unsigned n
)
1043 while (i
< 12 && (n
>> ((4 + i
) << 1)))
1045 return (n
>> (i
<< 1)) | (((16 - i
) & 0x0f) << 8);
1048 static unsigned add_decimm(int n
)
1050 int rot
= (16 - ((n
>> 8) & 0x0f)) & 0x0f;
1051 return (n
& 0xff) << (rot
<< 1);
1054 static int add_rndimm(unsigned n
)
1056 int rot
= (n
>> 8) & 0x0f;
1062 rot
= (rot
+ 12) & 0x0f;
1064 return ((num
+ 1) & 0xff) | (rot
<< 8);
1067 static void i_add(int op
, int rd
, int rn
, int rm
)
1069 oi(ADD(op
, rd
, rn
, 0, 0, 14) | rm
);
1072 static int i_decodeable(long imm
)
1074 return add_decimm(add_encimm(imm
)) == imm
;
1077 static void i_add_imm(int op
, int rd
, int rn
, long n
)
1079 oi(ADD(op
, rd
, rn
, 0, 1, 14) | add_encimm(n
));
1082 static void i_num(int rd
, long n
)
1084 int enc
= add_encimm(n
);
1085 if (n
== add_decimm(enc
)) {
1086 oi(ADD(I_MOV
, rd
, 0, 0, 1, 14) | enc
);
1089 enc
= add_encimm(-n
- 1);
1090 if (~n
== add_decimm(enc
)) {
1091 oi(ADD(I_MVN
, rd
, 0, 0, 1, 14) | enc
);
1095 int off
= pool_num(n
);
1096 i_ldr(1, rd
, REG_DP
, off
, LONGSZ
);
1100 static void i_add_anyimm(int rd
, int rn
, long n
)
1103 int imm
= add_encimm(neg
? -n
: n
);
1104 if (imm
== add_decimm(neg
? -n
: n
)) {
1105 oi(ADD(neg
? I_SUB
: I_ADD
, rd
, rn
, 0, 1, 14) | imm
);
1108 i_add(I_ADD
, rd
, rd
, rn
);
1114 * +----------------------------------------+
1115 * |COND|000000|A|S| Rd | Rn | Rs |1001| Rm |
1116 * +----------------------------------------+
1120 * C: set condition codes
1122 * I=0 operand2=| shift | Rm |
1123 * I=1 operand2=|rota| imm |
1125 #define MUL(rd, rn, rs) \
1126 ((14 << 28) | ((rd) << 16) | ((0) << 12) | ((rn) << 8) | ((9) << 4) | (rm))
1128 static void i_mul(int rd
, int rn
, int rm
)
1130 oi(MUL(rd
, rn
, rm
));
1133 static void i_cmp(int op
, int rn
, int rm
)
1135 oi(ADD(op
, 0, rn
, 1, 0, 14) | rm
);
1138 static void i_cmp_imm(int op
, int rn
, long n
)
1140 oi(ADD(op
, 0, rn
, 1, 1, 14) | add_encimm(n
));
1143 static void i_set(int cond
, int rd
)
1145 oi(ADD(I_MOV
, rd
, 0, 0, 1, 14));
1146 oi(ADD(I_MOV
, rd
, 0, 0, 1, cond
) | 1);
1149 static void i_shl(int sm
, int rd
, int rm
, int rs
)
1151 oi(ADD(I_MOV
, rd
, 0, 0, 0, 14) | (rs
<< 8) | (sm
<< 5) | (1 << 4) | rm
);
1154 static void i_shl_imm(int sm
, int rd
, int rn
, long n
)
1156 oi(ADD(I_MOV
, rd
, 0, 0, 0, 14) | (n
<< 7) | (sm
<< 5) | rn
);
1159 static void i_mov(int rd
, int rn
)
1161 oi(ADD(I_MOV
, rd
, 0, 0, 0, 14) | rn
);
1165 * single data transfer:
1166 * +------------------------------------------+
1167 * |COND|01|I|P|U|B|W|L| Rn | Rd | offset |
1168 * +------------------------------------------+
1170 * I: immediate/offset
1171 * P: post/pre indexing
1177 * Rd: source/destination register
1179 * I=0 offset=| immediate |
1180 * I=1 offset=| shift | Rm |
1182 * halfword and signed data transfer
1183 * +----------------------------------------------+
1184 * |COND|000|P|U|0|W|L| Rn | Rd |0000|1|S|H|1| Rm |
1185 * +----------------------------------------------+
1187 * +----------------------------------------------+
1188 * |COND|000|P|U|1|W|L| Rn | Rd |off1|1|S|H|1|off2|
1189 * +----------------------------------------------+
1194 #define LDR(l, rd, rn, b, u, p, w) \
1195 ((14 << 28) | (1 << 26) | ((p) << 24) | ((b) << 22) | ((u) << 23) | \
1196 ((w) << 21) | ((l) << 20) | ((rn) << 16) | ((rd) << 12))
1197 #define LDRH(l, rd, rn, s, h, u, i) \
1198 ((14 << 28) | (1 << 24) | ((u) << 23) | ((i) << 22) | ((l) << 20) | \
1199 ((rn) << 16) | ((rd) << 12) | ((s) << 6) | ((h) << 5) | (9 << 4))
1201 static void i_ldr(int l
, int rd
, int rn
, int off
, int bt
)
1203 int b
= BT_SZ(bt
) == 1;
1204 int h
= BT_SZ(bt
) == 2;
1205 int s
= l
&& (bt
& BT_SIGNED
);
1206 int half
= h
|| (b
&& s
);
1207 int maximm
= half
? 0x100 : 0x1000;
1211 while (off
>= maximm
) {
1212 int imm
= add_encimm(off
);
1213 oi(ADD(neg
? I_SUB
: I_ADD
, REG_TMP
, rn
, 0, 1, 14) | imm
);
1215 off
-= add_decimm(imm
);
1218 oi(LDR(l
, rd
, rn
, b
, !neg
, 1, 0) | off
);
1220 oi(LDRH(l
, rd
, rn
, s
, h
, !neg
, 1) |
1221 ((off
& 0xf0) << 4) | (off
& 0x0f));
1224 static void i_sym(int rd
, char *sym
, int off
)
1227 int doff
= pool_reloc(sym
, off
);
1228 i_ldr(1, rd
, REG_DP
, doff
, LONGSZ
);
1232 static void i_neg(int rd
)
1234 oi(ADD(I_RSB
, rd
, rd
, 0, 1, 14));
1237 static void i_not(int rd
)
1239 oi(ADD(I_MVN
, rd
, 0, 0, 0, 14) | rd
);
1242 static void i_lnot(int rd
)
1244 i_cmp(I_TST
, rd
, rd
);
1248 /* rd = rd & ((1 << bits) - 1) */
1249 static void i_zx(int rd
, int bits
)
1252 oi(ADD(I_AND
, rd
, rd
, 0, 1, 14) | add_encimm((1 << bits
) - 1));
1254 i_shl_imm(SM_LSL
, rd
, rd
, 32 - bits
);
1255 i_shl_imm(SM_LSR
, rd
, rd
, 32 - bits
);
1259 static void i_sx(int rd
, int bits
)
1261 i_shl_imm(SM_LSL
, rd
, rd
, 32 - bits
);
1262 i_shl_imm(SM_ASR
, rd
, rd
, 32 - bits
);
1267 * +-----------------------------------+
1268 * |COND|101|L| offset |
1269 * +-----------------------------------+
1273 #define BL(cond, l, o) (((cond) << 28) | (5 << 25) | ((l) << 24) | \
1274 ((((o) - 8) >> 2) & 0x00ffffff))
1276 static void i_b(long addr
)
1278 oi(BL(14, 0, addr
- cslen
));
1281 static void i_b_if(long addr
, int rn
, int z
)
1283 i_cmp(I_TST
, rn
, rn
);
1284 oi(BL(z
? 0 : 1, 0, addr
- cslen
));
1287 static void i_b_fill(long *dst
, int diff
)
1289 *dst
= (*dst
& 0xff000000) | (((diff
- 8) >> 2) & 0x00ffffff);
1292 static void i_memcpy(int rd
, int rs
, int rn
)
1294 oi(ADD(I_SUB
, rn
, rn
, 1, 1, 14) | 1);
1296 oi(LDR(1, REG_TMP
, rs
, 1, 1, 0, 0) | 1);
1297 oi(LDR(0, REG_TMP
, rd
, 1, 1, 0, 0) | 1);
1301 static void i_memset(int rd
, int rs
, int rn
)
1303 oi(ADD(I_SUB
, rn
, rn
, 1, 1, 14) | 1);
1305 oi(LDR(0, rs
, rd
, 1, 1, 0, 0) | 1);
1309 static void i_call_reg(int rd
)
1311 i_mov(REG_LR
, REG_PC
);
1315 static void i_call(char *sym
, int off
)
1318 out_rel(sym
, OUT_CS
| OUT_REL24
, cslen
);
1322 static void i_prolog(void)
1326 oi(0xe1a0c00d); /* mov r12, sp */
1327 oi(0xe92d000f); /* stmfd sp!, {r0-r3} */
1328 oi(0xe92d5ff0); /* stmfd sp!, {r0-r11, r12, lr} */
1329 oi(0xe1a0b00d); /* mov fp, sp */
1330 oi(0xe24dd000); /* sub sp, sp, xx */
1331 oi(0xe28fa000); /* add dp, pc, xx */
1334 static void i_epilog(void)
1337 oi(0xe89baff0); /* ldmfd fp, {r4-r11, sp, pc} */
1338 dpoff
= add_decimm(add_rndimm(add_encimm(cslen
- func_beg
- 28)));
1339 cslen
= func_beg
+ dpoff
+ 28;
1340 maxsp
= ALIGN(maxsp
, 8);
1341 maxsp
= add_decimm(add_rndimm(add_encimm(maxsp
)));
1342 /* fill stack sub: sp = sp - xx */
1343 *(long *) (cs
+ func_beg
+ 16) |= add_encimm(maxsp
);
1344 /* fill data ptr addition: dp = pc + xx */
1345 *(long *) (cs
+ func_beg
+ 20) |= add_encimm(dpoff
);