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 /* for optimizing cmp + bcc */
75 static long last_cmp
= -1;
76 static long last_set
= -1;
77 static long last_cond
;
79 static void os(void *s
, int n
)
81 memcpy(cs
+ cslen
, s
, n
);
85 static void oi(long n
)
89 *(int *) (cs
+ cslen
) = n
;
96 static long num_offs
[MAXNUMS
]; /* data immediate value */
97 static char num_names
[MAXNUMS
][NAMELEN
]; /* relocation data symbol name */
100 static int pool_find(char *name
, int off
)
103 for (i
= 0; i
< nums
; i
++)
104 if (!strcmp(name
, num_names
[i
]) && off
== num_offs
[i
])
109 static int pool_num(long num
)
111 int idx
= pool_find("", num
);
115 num_names
[idx
][0] = '\0';
120 static int pool_reloc(char *name
, long off
)
122 int idx
= pool_find(name
, off
);
126 strcpy(num_names
[idx
], name
);
131 static void pool_write(void)
134 for (i
= 0; i
< nums
; i
++) {
136 out_rel(num_names
[i
], OUT_CS
, cslen
);
143 * +---------------------------------------+
144 * |COND|00|I| op |S| Rn | Rd | operand2 |
145 * +---------------------------------------+
147 * S: set condition code
149 * Rd: destination operand
151 * I=0 operand2=| shift | Rm |
152 * I=1 operand2=|rota| imm |
154 #define ADD(op, rd, rn, s, i, cond) \
155 (((cond) << 28) | ((i) << 25) | ((s) << 20) | \
156 ((op) << 21) | ((rn) << 16) | ((rd) << 12))
158 static void i_add(int op
, int rd
, int rn
, int rm
)
160 oi(ADD(op
, rd
, rn
, 0, 0, 14) | rm
);
163 static int add_encimm(unsigned n
)
166 while (i
< 12 && (n
>> ((4 + i
) << 1)))
168 return (n
>> (i
<< 1)) | (((16 - i
) & 0x0f) << 8);
171 static int add_decimm(unsigned n
)
173 int rot
= (16 - ((n
>> 8) & 0x0f)) & 0x0f;
174 return (n
& 0xff) << (rot
<< 1);
177 static int add_rndimm(unsigned n
)
179 int rot
= (n
>> 8) & 0x0f;
185 rot
= (rot
+ 12) & 0x0f;
187 return ((num
+ 1) & 0xff) | (rot
<< 8);
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_imm(int op
, int rd
, int rn
, int 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
);
246 static void i_set(int cond
, int rd
)
250 oi(ADD(I_MOV
, rd
, 0, 0, 1, 14));
251 oi(ADD(I_MOV
, rd
, 0, 0, 1, cond
) | 1);
258 static void i_shl(int sm
, int rd
, int rm
, int rs
)
260 oi(ADD(I_MOV
, rd
, 0, 0, 0, 14) | (rs
<< 8) | (sm
<< 5) | (1 << 4) | rm
);
263 static void i_shl_imm(int sm
, int rd
, int n
)
265 oi(ADD(I_MOV
, rd
, 0, 0, 0, 14) | (n
<< 7) | (sm
<< 5) | rd
);
268 static void i_mov(int op
, int rd
, int rn
)
270 oi(ADD(op
, rd
, 0, 0, 0, 14) | rn
);
274 * single data transfer:
275 * +------------------------------------------+
276 * |COND|01|I|P|U|B|W|L| Rn | Rd | offset |
277 * +------------------------------------------+
279 * I: immediate/offset
280 * P: post/pre indexing
286 * Rd: source/destination register
288 * I=0 offset=| immediate |
289 * I=1 offset=| shift | Rm |
291 * halfword and signed data transfer
292 * +----------------------------------------------+
293 * |COND|000|P|U|0|W|L| Rn | Rd |0000|1|S|H|1| Rm |
294 * +----------------------------------------------+
296 * +----------------------------------------------+
297 * |COND|000|P|U|1|W|L| Rn | Rd |off1|1|S|H|1|off2|
298 * +----------------------------------------------+
303 #define LDR(l, rd, rn, b, u, p, w) \
304 ((14 << 28) | (1 << 26) | ((p) << 24) | ((b) << 22) | ((u) << 23) | \
305 ((w) << 21) | ((l) << 20) | ((rn) << 16) | ((rd) << 12))
306 #define LDRH(l, rd, rn, s, h, u, i) \
307 ((14 << 28) | (1 << 24) | ((u) << 23) | ((i) << 22) | ((l) << 20) | \
308 ((rn) << 16) | ((rd) << 12) | ((s) << 6) | ((h) << 5) | (9 << 4))
310 static void i_ldr(int l
, int rd
, int rn
, int off
, int bt
)
312 int b
= BT_SZ(bt
) == 1;
313 int h
= BT_SZ(bt
) == 2;
314 int s
= l
&& (bt
& BT_SIGNED
);
315 int half
= h
|| (b
&& s
);
316 int maximm
= half
? 0x100 : 0x1000;
320 while (off
>= maximm
) {
321 int imm
= add_encimm(off
);
322 oi(ADD(neg
? I_SUB
: I_ADD
, REG_TMP
, rn
, 0, 1, 14) | imm
);
324 off
-= add_decimm(imm
);
327 oi(LDR(l
, rd
, rn
, b
, !neg
, 1, 0) | off
);
329 oi(LDRH(l
, rd
, rn
, s
, h
, !neg
, 1) |
330 ((off
& 0xf0) << 4) | (off
& 0x0f));
333 static void i_sym(int rd
, char *sym
, int off
)
336 int doff
= pool_reloc(sym
, off
);
337 i_ldr(1, rd
, REG_DP
, doff
, LONGSZ
);
341 static void i_neg(int rd
)
343 oi(ADD(I_RSB
, rd
, rd
, 0, 1, 14));
346 static void i_not(int rd
)
348 oi(ADD(I_MVN
, rd
, 0, 0, 0, 14) | rd
);
351 static void i_lnot(int rd
)
353 i_cmp(I_TST
, rd
, rd
);
354 oi(ADD(I_MOV
, rd
, 0, 0, 1, 14));
355 oi(ADD(I_MOV
, rd
, 0, 0, 1, 0) | 1);
358 /* rd = rd & ((1 << bits) - 1) */
359 static void i_zx(int rd
, int bits
)
362 oi(ADD(I_AND
, rd
, rd
, 0, 1, 14) | add_encimm((1 << bits
) - 1));
364 i_shl_imm(SM_LSL
, rd
, 32 - bits
);
365 i_shl_imm(SM_LSR
, rd
, 32 - bits
);
369 static void i_sx(int rd
, int bits
)
371 i_shl_imm(SM_LSL
, rd
, 32 - bits
);
372 i_shl_imm(SM_ASR
, rd
, 32 - bits
);
377 * +-----------------------------------+
378 * |COND|101|L| offset |
379 * +-----------------------------------+
383 #define BL(cond, l, o) (((cond) << 28) | (5 << 25) | ((l) << 24) | \
384 ((((o) - 8) >> 2) & 0x00ffffff))
386 static void i_b(long addr
)
388 oi(BL(14, 0, addr
- cslen
));
391 static void i_b_if(long addr
, int rn
, int z
)
393 static int nots
[] = {1, 0, 3, 2, -1, -1, -1, -1, 9, 8, 11, 10, 13, 12, -1};
394 if (last_cmp
+ 8 != cslen
|| last_set
!= last_cmp
) {
395 i_cmp(I_TST
, rn
, rn
);
396 oi(BL(z
? 0 : 1, 0, addr
- cslen
));
401 oi(BL(z
? nots
[last_cond
] : last_cond
, 0, addr
- cslen
));
404 static void i_b_fill(long *dst
, int diff
)
406 *dst
= (*dst
& 0xff000000) | (((diff
- 8) >> 2) & 0x00ffffff);
409 static void i_memcpy(int rd
, int rs
, int rn
)
411 oi(ADD(I_SUB
, rn
, rn
, 1, 1, 14) | 1);
413 oi(LDR(1, REG_TMP
, rs
, 1, 1, 0, 0) | 1);
414 oi(LDR(0, REG_TMP
, rd
, 1, 1, 0, 0) | 1);
418 static void i_memset(int rd
, int rs
, int rn
)
420 oi(ADD(I_SUB
, rn
, rn
, 1, 1, 14) | 1);
422 oi(LDR(0, rs
, rd
, 1, 1, 0, 0) | 1);
426 static void i_call_reg(int rd
)
428 i_mov(I_MOV
, REG_LR
, REG_PC
);
429 i_mov(I_MOV
, REG_PC
, rd
);
432 static void i_call(char *sym
)
435 out_rel(sym
, OUT_CS
| OUT_REL24
, cslen
);
439 static void i_prolog(void)
443 oi(0xe1a0c00d); /* mov r12, sp */
444 oi(0xe92d000f); /* stmfd sp!, {r0-r3} */
445 oi(0xe92d5ff0); /* stmfd sp!, {r0-r11, r12, lr} */
446 oi(0xe1a0b00d); /* mov fp, sp */
447 oi(0xe24dd000); /* sub sp, sp, xx */
448 oi(0xe28fa000); /* add dp, pc, xx */
451 static void i_epilog(void)
454 oi(0xe89baff0); /* ldmfd fp, {r4-r11, sp, pc} */
455 dpoff
= add_decimm(add_rndimm(add_encimm(cslen
- func_beg
- 28)));
456 cslen
= func_beg
+ dpoff
+ 28;
457 maxsp
= ALIGN(maxsp
, 8);
458 maxsp
= add_decimm(add_rndimm(add_encimm(maxsp
)));
459 /* fill stack sub: sp = sp - xx */
460 *(long *) (cs
+ func_beg
+ 16) |= add_encimm(maxsp
);
461 /* fill data ptr addition: dp = pc + xx */
462 *(long *) (cs
+ func_beg
+ 20) |= add_encimm(dpoff
);
466 static long sp_push(int size
)
474 static void tmp_mem(struct tmp
*tmp
)
477 if (!(tmp
->loc
== LOC_REG
))
481 tmp
->addr
= -sp_push(LONGSZ
);
482 i_ldr(0, src
, REG_FP
, tmp
->addr
, LONGSZ
);
487 static void num_cast(struct tmp
*t
, unsigned bt
)
489 if (!(bt
& BT_SIGNED
) && BT_SZ(bt
) != LONGSZ
)
490 t
->addr
&= ((1l << (long) (BT_SZ(bt
) * 8)) - 1);
491 if (bt
& BT_SIGNED
&& BT_SZ(bt
) != LONGSZ
&&
492 t
->addr
> (1l << (BT_SZ(bt
) * 8 - 1)))
493 t
->addr
= -((1l << (BT_SZ(bt
) * 8)) - t
->addr
);
496 static void tmp_reg(struct tmp
*tmp
, int dst
, int deref
)
503 if (tmp
->loc
== LOC_NUM
) {
504 i_num(dst
, tmp
->addr
);
509 if (tmp
->loc
== LOC_SYM
) {
510 i_sym(dst
, tmp
->sym
, tmp
->off
);
515 if (tmp
->loc
== LOC_REG
) {
517 i_ldr(1, dst
, tmp
->addr
, 0, bt
);
518 else if (dst
!= tmp
->addr
)
519 i_mov(I_MOV
, dst
, tmp
->addr
);
520 regs
[tmp
->addr
] = NULL
;
522 if (tmp
->loc
== LOC_LOCAL
) {
524 i_ldr(1, dst
, REG_FP
, tmp
->addr
+ tmp
->off
, bt
);
526 i_add_imm(I_ADD
, dst
, REG_FP
, tmp
->addr
+ tmp
->off
);
528 if (tmp
->loc
== LOC_MEM
) {
529 i_ldr(1, dst
, REG_FP
, tmp
->addr
, LONGSZ
);
531 i_ldr(1, dst
, dst
, 0, bt
);
538 static void reg_free(int reg
)
543 for (i
= 0; i
< ARRAY_SIZE(tmpregs
); i
++)
544 if (!regs
[tmpregs
[i
]]) {
545 tmp_reg(regs
[reg
], tmpregs
[i
], 0);
551 static void reg_for(int reg
, struct tmp
*t
)
553 if (regs
[reg
] && regs
[reg
] != t
)
557 static void tmp_mv(struct tmp
*t
, int reg
)
563 static void tmp_to(struct tmp
*t
, int reg
)
569 static void tmp_drop(int n
)
572 for (i
= ntmp
- n
; i
< ntmp
; i
++)
573 if (tmps
[i
].loc
== LOC_REG
)
574 regs
[tmps
[i
].addr
] = NULL
;
578 static void tmp_pop(int reg
)
580 struct tmp
*t
= TMP(0);
585 static struct tmp
*tmp_new(void)
587 return &tmps
[ntmp
++];
590 static void tmp_push(int reg
)
592 struct tmp
*t
= tmp_new();
599 void o_local(long addr
)
601 struct tmp
*t
= tmp_new();
610 struct tmp
*t
= tmp_new();
616 void o_sym(char *name
)
618 struct tmp
*t
= tmp_new();
619 strcpy(t
->sym
, name
);
625 void o_tmpdrop(int n
)
627 if (n
== -1 || n
> ntmp
)
637 #define FORK_REG 0x00
639 /* make sure tmps remain intact after a conditional expression */
643 for (i
= 0; i
< ntmp
- 1; i
++)
647 void o_forkpush(void)
652 void o_forkjoin(void)
659 struct tmp
*t1
= TMP(0);
660 struct tmp
*t2
= TMP(1);
662 memcpy(&t
, t1
, sizeof(t
));
663 memcpy(t1
, t2
, sizeof(t
));
664 memcpy(t2
, &t
, sizeof(t
));
665 if (t1
->loc
== LOC_REG
)
667 if (t2
->loc
== LOC_REG
)
671 static int reg_get(int mask
)
674 for (i
= 0; i
< ARRAY_SIZE(tmpregs
); i
++)
675 if ((1 << tmpregs
[i
]) & mask
&& !regs
[tmpregs
[i
]])
677 for (i
= 0; i
< ARRAY_SIZE(tmpregs
); i
++)
678 if ((1 << tmpregs
[i
]) & mask
) {
679 reg_free(tmpregs
[i
]);
685 static int reg_fortmp(struct tmp
*t
, int notmask
)
687 if (t
->loc
== LOC_REG
&& !(notmask
& (1 << t
->addr
)))
689 return reg_get(~notmask
);
692 static void tmp_copy(struct tmp
*t1
)
694 struct tmp
*t2
= tmp_new();
695 memcpy(t2
, t1
, sizeof(*t1
));
696 if (!(t1
->loc
& (LOC_REG
| LOC_MEM
)))
698 if (t1
->loc
== LOC_MEM
) {
699 tmp_mv(t2
, reg_get(~0));
700 } else if (t1
->loc
== LOC_REG
) {
701 t2
->addr
= reg_fortmp(t2
, 1 << t1
->addr
);
702 i_mov(I_MOV
, t2
->addr
, t1
->addr
);
712 void o_cast(unsigned bt
)
714 struct tmp
*t
= TMP(0);
715 if (!t
->bt
&& t
->loc
== LOC_NUM
) {
719 if (BT_SZ(bt
) != LONGSZ
) {
720 int reg
= reg_fortmp(t
, 0);
723 i_sx(reg
, BT_SZ(bt
) * 8);
725 i_zx(reg
, BT_SZ(bt
) * 8);
729 void o_func_beg(char *name
, int global
)
731 out_sym(name
, (global
? OUT_GLOB
: 0) | OUT_CS
, cslen
, 0);
738 memset(regs
, 0, sizeof(regs
));
741 void o_deref(unsigned bt
)
743 struct tmp
*t
= TMP(0);
745 tmp_to(t
, reg_fortmp(t
, 0));
751 struct tmp
*t
= TMP(0);
752 tmp_to(t
, reg_fortmp(t
, 0));
755 #define TMP_NUM(t) ((t)->loc == LOC_NUM && !(t)->bt)
756 #define LOCAL_PTR(t) ((t)->loc == LOC_LOCAL && !(t)->bt)
757 #define SYM_PTR(t) ((t)->loc == LOC_SYM && !(t)->bt)
759 int o_popnum(long *c
)
761 struct tmp
*t
= TMP(0);
775 ret
[nret
++] = o_jmp(0);
778 void o_func_end(void)
781 for (i
= 0; i
< nret
; i
++)
786 long o_mklocal(int size
)
788 return sp_push(ALIGN(size
, LONGSZ
));
791 void o_rmlocal(long addr
, int sz
)
796 long o_arg2loc(int i
)
798 return -(10 + i
) << 2;
801 void o_assign(unsigned bt
)
803 struct tmp
*t1
= TMP(0);
804 struct tmp
*t2
= TMP(1);
805 int r1
= reg_fortmp(t1
, 0);
806 int r2
= reg_fortmp(t2
, 1 << r1
);
811 if (t2
->loc
== LOC_LOCAL
) {
813 off
= t2
->addr
+ t2
->off
;
818 i_ldr(0, r1
, r2
, off
, bt
);
822 static long cu(int op
, long i
)
835 static int c_uop(int op
)
837 struct tmp
*t1
= TMP(0);
841 o_num(cu(op
, t1
->addr
));
845 static long cb(int op
, long a
, long b
)
870 return (unsigned long) a
>> b
;
887 static int c_bop(int op
)
889 struct tmp
*t1
= TMP(0);
890 struct tmp
*t2
= TMP(1);
891 int locals
= LOCAL_PTR(t1
) + LOCAL_PTR(t2
);
892 int syms
= SYM_PTR(t1
) + SYM_PTR(t2
);
893 int nums
= TMP_NUM(t1
) + TMP_NUM(t2
);
894 if (syms
+ locals
== 2 || syms
+ nums
+ locals
!= 2)
897 if ((op
& 0xff) != O_ADD
&& ((op
& 0xff) != O_SUB
|| TMP_NUM(t2
)))
900 long o1
= TMP_NUM(t1
) ? t1
->addr
: t1
->off
;
901 long o2
= TMP_NUM(t2
) ? t2
->addr
: t2
->off
;
902 long ret
= cb(op
, o2
, o1
);
908 long ret
= cb(op
, t2
->addr
, t1
->addr
);
917 int r1
= reg_fortmp(TMP(0), 0);
934 static void bin_regs(int *r1
, int *r2
)
936 struct tmp
*t2
= TMP(0);
937 struct tmp
*t1
= TMP(1);
938 *r2
= reg_fortmp(t2
, 0);
940 *r1
= reg_fortmp(t1
, 1 << *r2
);
945 static void bin_add(int op
)
947 /* opcode for O_ADD, O_SUB, O_AND, O_OR, O_XOR */
948 static int rx
[] = {I_ADD
, I_SUB
, I_AND
, I_ORR
, I_EOR
};
951 i_add(rx
[op
& 0x0f], r1
, r1
, r2
);
955 static void bin_shx(int op
)
960 if ((op
& 0x0f) == 1)
961 sm
= op
& O_SIGNED
? SM_ASR
: SM_LSR
;
962 i_shl(sm
, r1
, r1
, r2
);
966 static int log2a(unsigned long n
)
969 for (i
= 0; i
< LONGSZ
* 8; i
++)
972 if (i
== LONGSZ
* 8 || !(n
>> (i
+ 1)))
977 /* optimized version of mul/div/mod for powers of two */
978 static int mul_2(int op
)
980 struct tmp
*t1
= TMP(0);
981 struct tmp
*t2
= TMP(1);
985 if ((op
& 0xff) == O_MUL
&& t2
->loc
== LOC_NUM
&& !t2
->bt
)
987 if (t1
->loc
!= LOC_NUM
|| t1
->bt
)
993 if ((op
& 0xff) == O_MUL
) {
1002 r2
= reg_fortmp(t2
, 0);
1004 i_shl_imm(SM_LSL
, r2
, p
);
1011 r2
= reg_fortmp(t2
, 0);
1013 i_shl_imm(op
& O_SIGNED
? SM_ASR
: SM_LSR
, r2
, p
);
1023 r2
= reg_fortmp(t2
, 0);
1031 static void bin_div(int op
)
1033 struct tmp
*t2
= TMP(0);
1034 struct tmp
*t1
= TMP(1);
1038 if ((op
& 0xff) == O_DIV
)
1039 func
= op
& O_SIGNED
? "__divdi3" : "__udivdi3";
1041 func
= op
& O_SIGNED
? "__moddi3" : "__umoddi3";
1042 for (i
= 0; i
< ARRAY_SIZE(argregs
); i
++)
1043 if (regs
[argregs
[i
]] && regs
[argregs
[i
]] - tmps
< ntmp
- 2)
1044 tmp_mem(regs
[argregs
[i
]]);
1045 tmp_to(t1
, argregs
[0]);
1046 tmp_to(t2
, argregs
[1]);
1052 static void bin_mul(int op
)
1057 if ((op
& 0xff) == O_DIV
|| (op
& 0xff) == O_MOD
) {
1066 static void bin_cmp(int op
)
1068 /* lt, gt, le, ge, eq, neq */
1069 static int ucond
[] = {3, 8, 9, 2, 0, 1};
1070 static int scond
[] = {11, 12, 13, 10, 0, 1};
1073 i_cmp(I_CMP
, r1
, r2
);
1074 i_set(op
& O_SIGNED
? scond
[op
& 0x0f] : ucond
[op
& 0x0f], r1
);
1082 if ((op
& 0xf0) == 0x00)
1084 if ((op
& 0xf0) == 0x10)
1086 if ((op
& 0xf0) == 0x20)
1088 if ((op
& 0xf0) == 0x30)
1092 static void load_regs2(int *r0
, int *r1
, int *r2
)
1094 struct tmp
*t0
= TMP(0);
1095 struct tmp
*t1
= TMP(1);
1096 struct tmp
*t2
= TMP(2);
1097 *r0
= reg_fortmp(t0
, 0);
1098 *r1
= reg_fortmp(t1
, 1 << *r0
);
1099 *r2
= reg_fortmp(t2
, (1 << *r0
) | (1 << *r1
));
1108 load_regs2(&rn
, &rs
, &rd
);
1109 i_memcpy(rd
, rs
, rn
);
1116 load_regs2(&rn
, &rs
, &rd
);
1117 i_memset(rd
, rs
, rn
);
1121 long o_mklabel(void)
1126 static long jxz(long addr
, int z
)
1128 int r
= reg_fortmp(TMP(0), 0);
1134 long o_jz(long addr
)
1136 return jxz(addr
, 1);
1139 long o_jnz(long addr
)
1141 return jxz(addr
, 0);
1144 long o_jmp(long addr
)
1150 void o_filljmp2(long addr
, long jmpdst
)
1152 i_b_fill((void *) cs
+ addr
, jmpdst
- addr
);
1155 void o_filljmp(long addr
)
1157 o_filljmp2(addr
, cslen
);
1160 void o_call(int argc
, int rets
)
1164 int aregs
= MIN(ARRAY_SIZE(argregs
), argc
);
1165 for (i
= 0; i
< ARRAY_SIZE(argregs
); i
++)
1166 if (regs
[argregs
[i
]] && regs
[argregs
[i
]] - tmps
< ntmp
- argc
)
1167 tmp_mem(regs
[argregs
[i
]]);
1169 sp_push(LONGSZ
* (argc
- aregs
));
1170 for (i
= argc
- 1; i
>= aregs
; --i
) {
1171 int reg
= reg_fortmp(TMP(0), 0);
1173 i_ldr(0, reg
, REG_SP
, (i
- aregs
) * LONGSZ
, LONGSZ
);
1176 for (i
= aregs
- 1; i
>= 0; --i
)
1177 tmp_to(TMP(aregs
- i
- 1), argregs
[i
]);
1180 if (t
->loc
== LOC_SYM
&& !t
->bt
) {
1184 int reg
= t
->loc
== LOC_REG
? t
->addr
: REG_TMP
;
1202 void dat_bss(char *name
, int size
, int global
)
1204 out_sym(name
, OUT_BSS
| (global
? OUT_GLOB
: 0), bsslen
, size
);
1205 bsslen
+= ALIGN(size
, LONGSZ
);
1208 #define MAXDATS (1 << 10)
1209 static char dat_names
[MAXDATS
][NAMELEN
];
1210 static int dat_offs
[MAXDATS
];
1213 void err(char *msg
);
1214 void *dat_dat(char *name
, int size
, int global
)
1216 void *addr
= ds
+ dslen
;
1219 err("nomem: MAXDATS reached!\n");
1220 strcpy(dat_names
[idx
], name
);
1221 dat_offs
[idx
] = dslen
;
1222 out_sym(name
, OUT_DS
| (global
? OUT_GLOB
: 0), dslen
, size
);
1223 dslen
+= ALIGN(size
, LONGSZ
);
1227 static int dat_off(char *name
)
1230 for (i
= 0; i
< ndats
; i
++)
1231 if (!strcmp(name
, dat_names
[i
]))
1236 void o_datset(char *name
, int off
, unsigned bt
)
1238 struct tmp
*t
= TMP(0);
1239 int sym_off
= dat_off(name
) + off
;
1240 if (t
->loc
== LOC_NUM
&& !t
->bt
) {
1242 memcpy(ds
+ sym_off
, &t
->addr
, BT_SZ(bt
));
1244 if (t
->loc
== LOC_SYM
&& !t
->bt
) {
1245 out_rel(t
->sym
, OUT_DS
, sym_off
);
1246 memcpy(ds
+ sym_off
, &t
->off
, BT_SZ(bt
));
1251 /* compiled division functions; div.s contains the source */
1252 static int udivdi3
[] = {
1253 0xe3a02000, 0xe3a03000, 0xe1110001, 0x0a00000a,
1254 0xe1b0c211, 0xe2822001, 0x5afffffc, 0xe3a0c001,
1255 0xe2522001, 0x4a000004, 0xe1500211, 0x3afffffb,
1256 0xe0400211, 0xe083321c, 0xeafffff8, 0xe1a01000,
1257 0xe1a00003, 0xe1a0f00e,
1259 static int umoddi3
[] = {
1260 0xe92d4000, 0xebffffeb, 0xe1a00001, 0xe8bd8000,
1262 static int divdi3
[] = {
1263 0xe92d4030, 0xe1a04000, 0xe1a05001, 0xe1100000,
1264 0x42600000, 0xe1110001, 0x42611000, 0xebffffe1,
1265 0xe1340005, 0x42600000, 0xe1140004, 0x42611000,
1268 static int moddi3
[] = {
1269 0xe92d4000, 0xebfffff0, 0xe1a00001, 0xe8bd8000,
1272 void o_write(int fd
)
1275 out_sym("__udivdi3", OUT_CS
, cslen
, 0);
1276 os(udivdi3
, sizeof(udivdi3
));
1277 out_sym("__umoddi3", OUT_CS
, cslen
, 0);
1278 os(umoddi3
, sizeof(umoddi3
));
1279 out_sym("__divdi3", OUT_CS
, cslen
, 0);
1280 os(divdi3
, sizeof(divdi3
));
1281 out_sym("__moddi3", OUT_CS
, cslen
, 0);
1282 os(moddi3
, sizeof(moddi3
));
1284 out_write(fd
, cs
, cslen
, ds
, dslen
);