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 */
24 #define MIN(a, b) ((a) < (b) ? (a) : (b))
25 #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
27 static char cs
[SECSIZE
]; /* code segment */
29 static char ds
[SECSIZE
]; /* data segment */
31 static long bsslen
; /* bss segment size */
33 static int nogen
; /* don't generate code */
38 #define TMP(i) (((i) < ntmp) ? &tmps[ntmp - 1 - (i)] : NULL)
43 long off
; /* offset from a symbol or a local */
44 unsigned loc
; /* variable location */
45 unsigned bt
; /* type of address; zero when not a pointer */
51 static struct tmp
*regs
[NREGS
];
52 static int tmpregs
[] = {4, 5, 6, 7, 8, 9, 0, 1, 2, 3};
53 static int argregs
[] = {0, 1, 2, 3};
66 #define MAXRET (1 << 8)
68 static long ret
[MAXRET
];
71 /* output div/mod functions */
72 static int putdiv
= 0;
74 static void os(void *s
, int n
)
76 memcpy(cs
+ cslen
, s
, n
);
80 static void oi(long n
)
84 *(int *) (cs
+ cslen
) = n
;
91 static long num_offs
[MAXNUMS
]; /* data immediate value */
92 static char num_names
[MAXNUMS
][NAMELEN
]; /* relocation data symbol name */
95 static int pool_find(char *name
, int off
)
98 for (i
= 0; i
< nums
; i
++)
99 if (!strcmp(name
, num_names
[i
]) && off
== num_offs
[i
])
104 static int pool_num(long num
)
106 int idx
= pool_find("", num
);
110 num_names
[idx
][0] = '\0';
115 static int pool_reloc(char *name
, long off
)
117 int idx
= pool_find(name
, off
);
121 strcpy(num_names
[idx
], name
);
126 static void pool_write(void)
129 for (i
= 0; i
< nums
; i
++) {
131 out_rel(num_names
[i
], OUT_CS
, cslen
);
138 * +---------------------------------------+
139 * |COND|00|I| op |S| Rn | Rd | operand2 |
140 * +---------------------------------------+
142 * S: set condition code
144 * Rd: destination operand
146 * I=0 operand2=| shift | Rm |
147 * I=1 operand2=|rota| imm |
149 #define ADD(op, rd, rn, s, i, cond) \
150 (((cond) << 28) | ((i) << 25) | ((s) << 20) | \
151 ((op) << 21) | ((rn) << 16) | ((rd) << 12))
153 static int add_encimm(unsigned n
)
156 while (i
< 12 && (n
>> ((4 + i
) << 1)))
158 return (n
>> (i
<< 1)) | (((16 - i
) & 0x0f) << 8);
161 static int add_decimm(unsigned n
)
163 int rot
= (16 - ((n
>> 8) & 0x0f)) & 0x0f;
164 return (n
& 0xff) << (rot
<< 1);
167 static int add_rndimm(unsigned n
)
169 int rot
= (n
>> 8) & 0x0f;
175 rot
= (rot
+ 12) & 0x0f;
177 return ((num
+ 1) & 0xff) | (rot
<< 8);
180 static void i_add(int op
, int rd
, int rn
, int rm
)
182 oi(ADD(op
, rd
, rn
, 0, 0, 14) | rm
);
185 static void i_add_imm(int op
, int rd
, int rn
, long n
)
187 oi(ADD(op
, rd
, rn
, 0, 1, 14) | add_encimm(n
));
190 static void i_ldr(int l
, int rd
, int rn
, int off
, int bt
);
192 static void i_num(int rd
, long n
)
195 int p
= neg
? -n
- 1 : n
;
196 int enc
= add_encimm(p
);
197 if (p
== add_decimm(enc
)) {
198 oi(ADD(neg
? I_MVN
: I_MOV
, rd
, 0, 0, 1, 14) | enc
);
201 int off
= pool_num(n
);
202 i_ldr(1, rd
, REG_DP
, off
, LONGSZ
);
207 static void i_add_anyimm(int rd
, int rn
, long n
)
210 int imm
= add_encimm(neg
? -n
: n
);
211 if (imm
== add_decimm(neg
? -n
: n
)) {
212 oi(ADD(neg
? I_SUB
: I_ADD
, rd
, rn
, 0, 1, 14) | imm
);
215 i_add(I_ADD
, rd
, rd
, rn
);
221 * +----------------------------------------+
222 * |COND|000000|A|S| Rd | Rn | Rs |1001| Rm |
223 * +----------------------------------------+
227 * C: set condition codes
229 * I=0 operand2=| shift | Rm |
230 * I=1 operand2=|rota| imm |
232 #define MUL(rd, rn, rs) \
233 ((14 << 28) | ((rd) << 16) | ((0) << 12) | ((rn) << 8) | ((9) << 4) | (rm))
235 static void i_mul(int rd
, int rn
, int rm
)
240 static void i_cmp(int op
, int rn
, int rm
)
242 oi(ADD(op
, 0, rn
, 1, 0, 14) | rm
);
245 static void i_cmp_imm(int op
, int rn
, long n
)
247 oi(ADD(op
, 0, rn
, 1, 1, 14) | add_encimm(n
));
250 static void i_set(int cond
, int rd
)
252 oi(ADD(I_MOV
, rd
, 0, 0, 1, 14));
253 oi(ADD(I_MOV
, rd
, 0, 0, 1, cond
) | 1);
260 static void i_shl(int sm
, int rd
, int rm
, int rs
)
262 oi(ADD(I_MOV
, rd
, 0, 0, 0, 14) | (rs
<< 8) | (sm
<< 5) | (1 << 4) | rm
);
265 static void i_shl_imm(int sm
, int rd
, int rn
, long n
)
267 oi(ADD(I_MOV
, rd
, 0, 0, 0, 14) | (n
<< 7) | (sm
<< 5) | rn
);
270 static void i_mov(int op
, int rd
, int rn
)
272 oi(ADD(op
, rd
, 0, 0, 0, 14) | rn
);
276 * single data transfer:
277 * +------------------------------------------+
278 * |COND|01|I|P|U|B|W|L| Rn | Rd | offset |
279 * +------------------------------------------+
281 * I: immediate/offset
282 * P: post/pre indexing
288 * Rd: source/destination register
290 * I=0 offset=| immediate |
291 * I=1 offset=| shift | Rm |
293 * halfword and signed data transfer
294 * +----------------------------------------------+
295 * |COND|000|P|U|0|W|L| Rn | Rd |0000|1|S|H|1| Rm |
296 * +----------------------------------------------+
298 * +----------------------------------------------+
299 * |COND|000|P|U|1|W|L| Rn | Rd |off1|1|S|H|1|off2|
300 * +----------------------------------------------+
305 #define LDR(l, rd, rn, b, u, p, w) \
306 ((14 << 28) | (1 << 26) | ((p) << 24) | ((b) << 22) | ((u) << 23) | \
307 ((w) << 21) | ((l) << 20) | ((rn) << 16) | ((rd) << 12))
308 #define LDRH(l, rd, rn, s, h, u, i) \
309 ((14 << 28) | (1 << 24) | ((u) << 23) | ((i) << 22) | ((l) << 20) | \
310 ((rn) << 16) | ((rd) << 12) | ((s) << 6) | ((h) << 5) | (9 << 4))
312 static void i_ldr(int l
, int rd
, int rn
, int off
, int bt
)
314 int b
= BT_SZ(bt
) == 1;
315 int h
= BT_SZ(bt
) == 2;
316 int s
= l
&& (bt
& BT_SIGNED
);
317 int half
= h
|| (b
&& s
);
318 int maximm
= half
? 0x100 : 0x1000;
322 while (off
>= maximm
) {
323 int imm
= add_encimm(off
);
324 oi(ADD(neg
? I_SUB
: I_ADD
, REG_TMP
, rn
, 0, 1, 14) | imm
);
326 off
-= add_decimm(imm
);
329 oi(LDR(l
, rd
, rn
, b
, !neg
, 1, 0) | off
);
331 oi(LDRH(l
, rd
, rn
, s
, h
, !neg
, 1) |
332 ((off
& 0xf0) << 4) | (off
& 0x0f));
335 static void i_sym(int rd
, char *sym
, int off
)
338 int doff
= pool_reloc(sym
, off
);
339 i_ldr(1, rd
, REG_DP
, doff
, LONGSZ
);
343 static void i_neg(int rd
)
345 oi(ADD(I_RSB
, rd
, rd
, 0, 1, 14));
348 static void i_not(int rd
)
350 oi(ADD(I_MVN
, rd
, 0, 0, 0, 14) | rd
);
353 static void i_lnot(int rd
)
355 i_cmp(I_TST
, rd
, rd
);
356 oi(ADD(I_MOV
, rd
, 0, 0, 1, 14));
357 oi(ADD(I_MOV
, rd
, 0, 0, 1, 0) | 1);
360 /* rd = rd & ((1 << bits) - 1) */
361 static void i_zx(int rd
, int bits
)
364 oi(ADD(I_AND
, rd
, rd
, 0, 1, 14) | add_encimm((1 << bits
) - 1));
366 i_shl_imm(SM_LSL
, rd
, rd
, 32 - bits
);
367 i_shl_imm(SM_LSR
, rd
, rd
, 32 - bits
);
371 static void i_sx(int rd
, int bits
)
373 i_shl_imm(SM_LSL
, rd
, rd
, 32 - bits
);
374 i_shl_imm(SM_ASR
, rd
, rd
, 32 - bits
);
379 * +-----------------------------------+
380 * |COND|101|L| offset |
381 * +-----------------------------------+
385 #define BL(cond, l, o) (((cond) << 28) | (5 << 25) | ((l) << 24) | \
386 ((((o) - 8) >> 2) & 0x00ffffff))
388 static void i_b(long addr
)
390 oi(BL(14, 0, addr
- cslen
));
393 static void i_b_if(long addr
, int rn
, int z
)
395 i_cmp(I_TST
, rn
, rn
);
396 oi(BL(z
? 0 : 1, 0, addr
- cslen
));
399 static void i_b_fill(long *dst
, int diff
)
401 *dst
= (*dst
& 0xff000000) | (((diff
- 8) >> 2) & 0x00ffffff);
404 static void i_memcpy(int rd
, int rs
, int rn
)
406 oi(ADD(I_SUB
, rn
, rn
, 1, 1, 14) | 1);
408 oi(LDR(1, REG_TMP
, rs
, 1, 1, 0, 0) | 1);
409 oi(LDR(0, REG_TMP
, rd
, 1, 1, 0, 0) | 1);
413 static void i_memset(int rd
, int rs
, int rn
)
415 oi(ADD(I_SUB
, rn
, rn
, 1, 1, 14) | 1);
417 oi(LDR(0, rs
, rd
, 1, 1, 0, 0) | 1);
421 static void i_call_reg(int rd
)
423 i_mov(I_MOV
, REG_LR
, REG_PC
);
424 i_mov(I_MOV
, REG_PC
, rd
);
427 static void i_call(char *sym
)
430 out_rel(sym
, OUT_CS
| OUT_REL24
, cslen
);
434 static void i_prolog(void)
438 oi(0xe1a0c00d); /* mov r12, sp */
439 oi(0xe92d000f); /* stmfd sp!, {r0-r3} */
440 oi(0xe92d5ff0); /* stmfd sp!, {r0-r11, r12, lr} */
441 oi(0xe1a0b00d); /* mov fp, sp */
442 oi(0xe24dd000); /* sub sp, sp, xx */
443 oi(0xe28fa000); /* add dp, pc, xx */
446 static void i_epilog(void)
449 oi(0xe89baff0); /* ldmfd fp, {r4-r11, sp, pc} */
450 dpoff
= add_decimm(add_rndimm(add_encimm(cslen
- func_beg
- 28)));
451 cslen
= func_beg
+ dpoff
+ 28;
452 maxsp
= ALIGN(maxsp
, 8);
453 maxsp
= add_decimm(add_rndimm(add_encimm(maxsp
)));
454 /* fill stack sub: sp = sp - xx */
455 *(long *) (cs
+ func_beg
+ 16) |= add_encimm(maxsp
);
456 /* fill data ptr addition: dp = pc + xx */
457 *(long *) (cs
+ func_beg
+ 20) |= add_encimm(dpoff
);
461 static long sp_push(int size
)
469 static void tmp_mem(struct tmp
*tmp
)
472 if (!(tmp
->loc
== LOC_REG
))
476 tmp
->addr
= -sp_push(LONGSZ
);
477 i_ldr(0, src
, REG_FP
, tmp
->addr
, LONGSZ
);
482 static void num_cast(struct tmp
*t
, unsigned bt
)
484 if (!(bt
& BT_SIGNED
) && BT_SZ(bt
) != LONGSZ
)
485 t
->addr
&= ((1l << (long) (BT_SZ(bt
) * 8)) - 1);
486 if (bt
& BT_SIGNED
&& BT_SZ(bt
) != LONGSZ
&&
487 t
->addr
> (1l << (BT_SZ(bt
) * 8 - 1)))
488 t
->addr
= -((1l << (BT_SZ(bt
) * 8)) - t
->addr
);
491 static void tmp_reg(struct tmp
*tmp
, int dst
, int deref
)
498 if (tmp
->loc
== LOC_NUM
) {
499 i_num(dst
, tmp
->addr
);
504 if (tmp
->loc
== LOC_SYM
) {
505 i_sym(dst
, tmp
->sym
, tmp
->off
);
510 if (tmp
->loc
== LOC_REG
) {
512 i_ldr(1, dst
, tmp
->addr
, 0, bt
);
513 else if (dst
!= tmp
->addr
)
514 i_mov(I_MOV
, dst
, tmp
->addr
);
515 regs
[tmp
->addr
] = NULL
;
517 if (tmp
->loc
== LOC_LOCAL
) {
519 i_ldr(1, dst
, REG_FP
, tmp
->addr
+ tmp
->off
, bt
);
521 i_add_anyimm(dst
, REG_FP
, tmp
->addr
+ tmp
->off
);
523 if (tmp
->loc
== LOC_MEM
) {
524 i_ldr(1, dst
, REG_FP
, tmp
->addr
, LONGSZ
);
526 i_ldr(1, dst
, dst
, 0, bt
);
533 static void reg_free(int reg
)
538 for (i
= 0; i
< ARRAY_SIZE(tmpregs
); i
++)
539 if (!regs
[tmpregs
[i
]]) {
540 tmp_reg(regs
[reg
], tmpregs
[i
], 0);
546 static void reg_for(int reg
, struct tmp
*t
)
548 if (regs
[reg
] && regs
[reg
] != t
)
552 static void tmp_mv(struct tmp
*t
, int reg
)
558 static void tmp_to(struct tmp
*t
, int reg
)
564 static void tmp_drop(int n
)
567 for (i
= ntmp
- n
; i
< ntmp
; i
++)
568 if (tmps
[i
].loc
== LOC_REG
)
569 regs
[tmps
[i
].addr
] = NULL
;
573 static void tmp_pop(int reg
)
575 struct tmp
*t
= TMP(0);
580 static struct tmp
*tmp_new(void)
582 return &tmps
[ntmp
++];
585 static void tmp_push(int reg
)
587 struct tmp
*t
= tmp_new();
594 void o_local(long addr
)
596 struct tmp
*t
= tmp_new();
605 struct tmp
*t
= tmp_new();
611 void o_sym(char *name
)
613 struct tmp
*t
= tmp_new();
614 strcpy(t
->sym
, name
);
620 void o_tmpdrop(int n
)
622 if (n
== -1 || n
> ntmp
)
632 #define FORK_REG 0x00
634 /* make sure tmps remain intact after a conditional expression */
638 for (i
= 0; i
< ntmp
- 1; i
++)
642 void o_forkpush(void)
647 void o_forkjoin(void)
654 struct tmp
*t1
= TMP(0);
655 struct tmp
*t2
= TMP(1);
657 memcpy(&t
, t1
, sizeof(t
));
658 memcpy(t1
, t2
, sizeof(t
));
659 memcpy(t2
, &t
, sizeof(t
));
660 if (t1
->loc
== LOC_REG
)
662 if (t2
->loc
== LOC_REG
)
666 static int reg_get(int mask
)
669 for (i
= 0; i
< ARRAY_SIZE(tmpregs
); i
++)
670 if ((1 << tmpregs
[i
]) & mask
&& !regs
[tmpregs
[i
]])
672 for (i
= 0; i
< ARRAY_SIZE(tmpregs
); i
++)
673 if ((1 << tmpregs
[i
]) & mask
) {
674 reg_free(tmpregs
[i
]);
680 static int reg_fortmp(struct tmp
*t
, int notmask
)
682 if (t
->loc
== LOC_REG
&& !(notmask
& (1 << t
->addr
)))
684 return reg_get(~notmask
);
687 static void tmp_copy(struct tmp
*t1
)
689 struct tmp
*t2
= tmp_new();
690 memcpy(t2
, t1
, sizeof(*t1
));
691 if (!(t1
->loc
& (LOC_REG
| LOC_MEM
)))
693 if (t1
->loc
== LOC_MEM
) {
694 tmp_mv(t2
, reg_get(~0));
695 } else if (t1
->loc
== LOC_REG
) {
696 t2
->addr
= reg_fortmp(t2
, 1 << t1
->addr
);
697 i_mov(I_MOV
, t2
->addr
, t1
->addr
);
707 void o_cast(unsigned bt
)
709 struct tmp
*t
= TMP(0);
710 if (!t
->bt
&& t
->loc
== LOC_NUM
) {
714 if (BT_SZ(bt
) != LONGSZ
) {
715 int reg
= reg_fortmp(t
, 0);
718 i_sx(reg
, BT_SZ(bt
) * 8);
720 i_zx(reg
, BT_SZ(bt
) * 8);
724 void o_func_beg(char *name
, int global
)
726 out_sym(name
, (global
? OUT_GLOB
: 0) | OUT_CS
, cslen
, 0);
733 memset(regs
, 0, sizeof(regs
));
736 void o_deref(unsigned bt
)
738 struct tmp
*t
= TMP(0);
740 tmp_to(t
, reg_fortmp(t
, 0));
746 struct tmp
*t
= TMP(0);
747 tmp_to(t
, reg_fortmp(t
, 0));
750 #define TMP_NUM(t) ((t)->loc == LOC_NUM && !(t)->bt)
751 #define LOCAL_PTR(t) ((t)->loc == LOC_LOCAL && !(t)->bt)
752 #define SYM_PTR(t) ((t)->loc == LOC_SYM && !(t)->bt)
754 int o_popnum(long *c
)
756 struct tmp
*t
= TMP(0);
770 ret
[nret
++] = o_jmp(0);
773 void o_func_end(void)
776 for (i
= 0; i
< nret
; i
++)
781 long o_mklocal(int size
)
783 return sp_push(ALIGN(size
, LONGSZ
));
786 void o_rmlocal(long addr
, int sz
)
791 long o_arg2loc(int i
)
793 return -(10 + i
) << 2;
796 void o_assign(unsigned bt
)
798 struct tmp
*t1
= TMP(0);
799 struct tmp
*t2
= TMP(1);
800 int r1
= reg_fortmp(t1
, 0);
801 int r2
= reg_fortmp(t2
, 1 << r1
);
806 if (t2
->loc
== LOC_LOCAL
) {
808 off
= t2
->addr
+ t2
->off
;
813 i_ldr(0, r1
, r2
, off
, bt
);
817 static long cu(int op
, long i
)
830 static int c_uop(int op
)
832 struct tmp
*t1
= TMP(0);
836 o_num(cu(op
, t1
->addr
));
840 static long cb(int op
, long a
, long b
)
865 return (unsigned long) a
>> b
;
882 static int c_bop(int op
)
884 struct tmp
*t1
= TMP(0);
885 struct tmp
*t2
= TMP(1);
886 int locals
= LOCAL_PTR(t1
) + LOCAL_PTR(t2
);
887 int syms
= SYM_PTR(t1
) + SYM_PTR(t2
);
888 int nums
= TMP_NUM(t1
) + TMP_NUM(t2
);
889 if (syms
+ locals
== 2 || syms
+ nums
+ locals
!= 2)
892 if ((op
& 0xff) != O_ADD
&& ((op
& 0xff) != O_SUB
|| TMP_NUM(t2
)))
895 long o1
= TMP_NUM(t1
) ? t1
->addr
: t1
->off
;
896 long o2
= TMP_NUM(t2
) ? t2
->addr
: t2
->off
;
897 long ret
= cb(op
, o2
, o1
);
903 long ret
= cb(op
, t2
->addr
, t1
->addr
);
912 int r1
= reg_fortmp(TMP(0), 0);
929 static void bin_regs(int *r1
, int *r2
)
931 struct tmp
*t2
= TMP(0);
932 struct tmp
*t1
= TMP(1);
933 *r2
= reg_fortmp(t2
, 0);
935 *r1
= reg_fortmp(t1
, 1 << *r2
);
940 static int bop_imm(int *r1
, long *n
, int swap
)
942 struct tmp
*t1
= TMP(0);
943 struct tmp
*t2
= TMP(1);
944 if (!TMP_NUM(t1
) && (!swap
|| !TMP_NUM(t2
)))
946 *n
= TMP_NUM(t1
) ? t1
->addr
: t2
->addr
;
947 if (*n
< 0 || add_decimm(add_encimm(*n
)) != *n
)
951 *r1
= reg_fortmp(t2
, 0);
957 static void bin_add(int op
)
959 /* opcode for O_ADD, O_SUB, O_AND, O_OR, O_XOR */
960 static int rx
[] = {I_ADD
, I_SUB
, I_AND
, I_ORR
, I_EOR
};
963 if (!bop_imm(&r1
, &n
, (op
& 0xff) != O_SUB
)) {
964 i_add_imm(rx
[op
& 0x0f], r1
, r1
, n
);
967 i_add(rx
[op
& 0x0f], r1
, r1
, r2
);
972 static void bin_shx(int op
)
977 if ((op
& 0x0f) == 1)
978 sm
= op
& O_SIGNED
? SM_ASR
: SM_LSR
;
979 if (!bop_imm(&r1
, &n
, 0)) {
980 i_shl_imm(sm
, r1
, r1
, n
);
983 i_shl(sm
, r1
, r1
, r2
);
988 static int log2a(unsigned long n
)
991 for (i
= 0; i
< LONGSZ
* 8; i
++)
994 if (i
== LONGSZ
* 8 || !(n
>> (i
+ 1)))
999 /* optimized version of mul/div/mod for powers of two */
1000 static int mul_2(int op
)
1002 struct tmp
*t1
= TMP(0);
1003 struct tmp
*t2
= TMP(1);
1007 if ((op
& 0xff) == O_MUL
&& t2
->loc
== LOC_NUM
&& !t2
->bt
)
1009 if (t1
->loc
!= LOC_NUM
|| t1
->bt
)
1015 if ((op
& 0xff) == O_MUL
) {
1024 r2
= reg_fortmp(t2
, 0);
1026 i_shl_imm(SM_LSL
, r2
, r2
, p
);
1033 r2
= reg_fortmp(t2
, 0);
1035 i_shl_imm(op
& O_SIGNED
? SM_ASR
: SM_LSR
, r2
, r2
, p
);
1045 r2
= reg_fortmp(t2
, 0);
1053 static void bin_div(int op
)
1055 struct tmp
*t2
= TMP(0);
1056 struct tmp
*t1
= TMP(1);
1060 if ((op
& 0xff) == O_DIV
)
1061 func
= op
& O_SIGNED
? "__divdi3" : "__udivdi3";
1063 func
= op
& O_SIGNED
? "__moddi3" : "__umoddi3";
1064 for (i
= 0; i
< ARRAY_SIZE(argregs
); i
++)
1065 if (regs
[argregs
[i
]] && regs
[argregs
[i
]] - tmps
< ntmp
- 2)
1066 tmp_mem(regs
[argregs
[i
]]);
1067 tmp_to(t1
, argregs
[0]);
1068 tmp_to(t2
, argregs
[1]);
1074 static void bin_mul(int op
)
1079 if ((op
& 0xff) == O_DIV
|| (op
& 0xff) == O_MOD
) {
1088 static void bin_cmp(int op
)
1090 /* lt, gt, le, ge, eq, neq */
1091 static int ucond
[] = {3, 8, 9, 2, 0, 1};
1092 static int scond
[] = {11, 12, 13, 10, 0, 1};
1095 if (!bop_imm(&r1
, &n
, 0)) {
1096 i_cmp_imm(I_CMP
, r1
, n
);
1099 i_cmp(I_CMP
, r1
, r2
);
1101 i_set(op
& O_SIGNED
? scond
[op
& 0x0f] : ucond
[op
& 0x0f], r1
);
1109 if ((op
& 0xf0) == 0x00)
1111 if ((op
& 0xf0) == 0x10)
1113 if ((op
& 0xf0) == 0x20)
1115 if ((op
& 0xf0) == 0x30)
1119 static void load_regs2(int *r0
, int *r1
, int *r2
)
1121 struct tmp
*t0
= TMP(0);
1122 struct tmp
*t1
= TMP(1);
1123 struct tmp
*t2
= TMP(2);
1124 *r0
= reg_fortmp(t0
, 0);
1125 *r1
= reg_fortmp(t1
, 1 << *r0
);
1126 *r2
= reg_fortmp(t2
, (1 << *r0
) | (1 << *r1
));
1135 load_regs2(&rn
, &rs
, &rd
);
1136 i_memcpy(rd
, rs
, rn
);
1143 load_regs2(&rn
, &rs
, &rd
);
1144 i_memset(rd
, rs
, rn
);
1148 long o_mklabel(void)
1153 static long jxz(long addr
, int z
)
1155 int r
= reg_fortmp(TMP(0), 0);
1161 long o_jz(long addr
)
1163 return jxz(addr
, 1);
1166 long o_jnz(long addr
)
1168 return jxz(addr
, 0);
1171 long o_jmp(long addr
)
1177 void o_filljmp2(long addr
, long jmpdst
)
1179 i_b_fill((void *) cs
+ addr
, jmpdst
- addr
);
1182 void o_filljmp(long addr
)
1184 o_filljmp2(addr
, cslen
);
1187 void o_call(int argc
, int rets
)
1191 int aregs
= MIN(ARRAY_SIZE(argregs
), argc
);
1192 for (i
= 0; i
< ARRAY_SIZE(argregs
); i
++)
1193 if (regs
[argregs
[i
]] && regs
[argregs
[i
]] - tmps
< ntmp
- argc
)
1194 tmp_mem(regs
[argregs
[i
]]);
1196 sp_push(LONGSZ
* (argc
- aregs
));
1197 for (i
= argc
- 1; i
>= aregs
; --i
) {
1198 int reg
= reg_fortmp(TMP(0), 0);
1200 i_ldr(0, reg
, REG_SP
, (i
- aregs
) * LONGSZ
, LONGSZ
);
1203 for (i
= aregs
- 1; i
>= 0; --i
)
1204 tmp_to(TMP(aregs
- i
- 1), argregs
[i
]);
1207 if (t
->loc
== LOC_SYM
&& !t
->bt
) {
1211 int reg
= t
->loc
== LOC_REG
? t
->addr
: REG_TMP
;
1229 void o_mkbss(char *name
, int size
, int global
)
1231 out_sym(name
, OUT_BSS
| (global
? OUT_GLOB
: 0), bsslen
, size
);
1232 bsslen
+= ALIGN(size
, LONGSZ
);
1235 #define MAXDATS (1 << 10)
1236 static char dat_names
[MAXDATS
][NAMELEN
];
1237 static int dat_offs
[MAXDATS
];
1240 void err(char *msg
);
1241 void *o_mkdat(char *name
, int size
, int global
)
1243 void *addr
= ds
+ dslen
;
1246 err("nomem: MAXDATS reached!\n");
1247 strcpy(dat_names
[idx
], name
);
1248 dat_offs
[idx
] = dslen
;
1249 out_sym(name
, OUT_DS
| (global
? OUT_GLOB
: 0), dslen
, size
);
1250 dslen
+= ALIGN(size
, LONGSZ
);
1254 static int dat_off(char *name
)
1257 for (i
= 0; i
< ndats
; i
++)
1258 if (!strcmp(name
, dat_names
[i
]))
1263 void o_datset(char *name
, int off
, unsigned bt
)
1265 struct tmp
*t
= TMP(0);
1266 int sym_off
= dat_off(name
) + off
;
1267 if (t
->loc
== LOC_NUM
&& !t
->bt
) {
1269 memcpy(ds
+ sym_off
, &t
->addr
, BT_SZ(bt
));
1271 if (t
->loc
== LOC_SYM
&& !t
->bt
) {
1272 out_rel(t
->sym
, OUT_DS
, sym_off
);
1273 memcpy(ds
+ sym_off
, &t
->off
, BT_SZ(bt
));
1278 /* compiled division functions; div.s contains the source */
1279 static int udivdi3
[] = {
1280 0xe3a02000, 0xe3a03000, 0xe1110001, 0x0a00000a,
1281 0xe1b0c211, 0xe2822001, 0x5afffffc, 0xe3a0c001,
1282 0xe2522001, 0x4a000004, 0xe1500211, 0x3afffffb,
1283 0xe0400211, 0xe083321c, 0xeafffff8, 0xe1a01000,
1284 0xe1a00003, 0xe1a0f00e,
1286 static int umoddi3
[] = {
1287 0xe92d4000, 0xebffffeb, 0xe1a00001, 0xe8bd8000,
1289 static int divdi3
[] = {
1290 0xe92d4030, 0xe1a04000, 0xe1a05001, 0xe1100000,
1291 0x42600000, 0xe1110001, 0x42611000, 0xebffffe1,
1292 0xe1340005, 0x42600000, 0xe1140004, 0x42611000,
1295 static int moddi3
[] = {
1296 0xe92d4000, 0xebfffff0, 0xe1a00001, 0xe8bd8000,
1299 void o_write(int fd
)
1302 out_sym("__udivdi3", OUT_CS
, cslen
, 0);
1303 os(udivdi3
, sizeof(udivdi3
));
1304 out_sym("__umoddi3", OUT_CS
, cslen
, 0);
1305 os(umoddi3
, sizeof(umoddi3
));
1306 out_sym("__divdi3", OUT_CS
, cslen
, 0);
1307 os(divdi3
, sizeof(divdi3
));
1308 out_sym("__moddi3", OUT_CS
, cslen
, 0);
1309 os(moddi3
, sizeof(moddi3
));
1311 out_write(fd
, cs
, cslen
, ds
, dslen
);