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 #define TMP_REG(t) ((t)->loc == LOC_REG ? (t)->addr : reg_get(~0))
28 #define TMP_REG2(t, r) ((t)->loc == LOC_REG && (t)->addr != (r) ? \
29 (t)->addr : reg_get(~(1 << (r))))
30 #define TMP_REG3(t, r1, r2) ((t)->loc == LOC_REG && (t)->addr != (r1) && (t)->addr != (r2) ? \
31 (t)->addr : reg_get(~((1 << (r1)) | (1 << (r2)))))
33 static char cs
[SECSIZE
]; /* code segment */
35 static char ds
[SECSIZE
]; /* data segment */
37 static long bsslen
; /* bss segment size */
44 #define TMP(i) (((i) < ntmp) ? &tmps[ntmp - 1 - (i)] : NULL)
49 long off
; /* offset from a symbol or a local */
50 unsigned loc
; /* variable location */
51 unsigned bt
; /* type of address; zero when not a pointer */
57 static struct tmp
*regs
[NREGS
];
58 static int tmpregs
[] = {4, 5, 6, 7, 8, 9, 0, 1, 2, 3};
59 static int argregs
[] = {0, 1, 2, 3};
72 #define MAXRET (1 << 8)
74 static long ret
[MAXRET
];
77 /* output div/mod functions */
78 static int putdiv
= 0;
80 /* for optimizing cmp + bcc */
81 static long last_cmp
= -1;
82 static long last_set
= -1;
83 static long last_cond
;
85 static void os(void *s
, int n
)
87 memcpy(cs
+ cslen
, s
, n
);
91 static void oi(long n
)
95 *(int *) (cs
+ cslen
) = n
;
102 static long num_offs
[NDATS
]; /* data immediate value */
103 static char num_names
[NDATS
][NAMELEN
]; /* relocation data symbol name */
106 static int pool_num(long num
)
110 num_names
[idx
][0] = '\0';
114 static int pool_reloc(char *name
, long off
)
118 strcpy(num_names
[idx
], name
);
122 static void pool_write(void)
125 for (i
= 0; i
< ndats
; i
++) {
127 out_rel(num_names
[i
], OUT_CS
, cslen
);
134 * +---------------------------------------+
135 * |COND|00|I| op |S| Rn | Rd | operand2 |
136 * +---------------------------------------+
138 * S: set condition code
140 * Rd: destination operand
142 * I=0 operand2=| shift | Rm |
143 * I=1 operand2=|rota| imm |
145 #define ADD(op, rd, rn, s, i, cond) \
146 (((cond) << 28) | ((i) << 25) | ((s) << 20) | \
147 ((op) << 21) | ((rn) << 16) | ((rd) << 12))
149 static void i_add(int op
, int rd
, int rn
, int rm
)
151 oi(ADD(op
, rd
, rn
, 0, 0, 14) | rm
);
154 static int add_encimm(unsigned n
)
157 while (i
< 12 && (n
>> ((4 + i
) << 1)))
159 return (n
>> (i
<< 1)) | (((16 - i
) & 0x0f) << 8);
162 static int add_decimm(unsigned n
)
164 int rot
= (16 - ((n
>> 8) & 0x0f)) & 0x0f;
165 return (n
& 0xff) << (rot
<< 1);
168 static int add_rndimm(unsigned n
)
170 int rot
= (n
>> 8) & 0x0f;
176 rot
= (rot
+ 12) & 0x0f;
178 return ((num
+ 1) & 0xff) | (rot
<< 8);
181 static void i_ldr(int l
, int rd
, int rn
, int off
, int bt
);
183 static void i_num(int rd
, long n
)
186 int p
= neg
? -n
- 1 : n
;
187 int enc
= add_encimm(p
);
188 if (p
== add_decimm(enc
)) {
189 oi(ADD(neg
? I_MVN
: I_MOV
, rd
, 0, 0, 1, 14) | enc
);
192 int off
= pool_num(n
);
193 i_ldr(1, rd
, REG_DP
, off
, LONGSZ
);
198 static void i_add_imm(int op
, int rd
, int rn
, int n
)
201 int imm
= add_encimm(neg
? -n
: n
);
202 if (imm
== add_decimm(neg
? -n
: n
)) {
203 oi(ADD(neg
? I_SUB
: I_ADD
, rd
, rn
, 0, 1, 14) | imm
);
206 i_add(I_ADD
, rd
, rd
, rn
);
212 * +----------------------------------------+
213 * |COND|000000|A|S| Rd | Rn | Rs |1001| Rm |
214 * +----------------------------------------+
218 * C: set condition codes
220 * I=0 operand2=| shift | Rm |
221 * I=1 operand2=|rota| imm |
223 #define MUL(rd, rn, rs) \
224 ((14 << 28) | ((rd) << 16) | ((0) << 12) | ((rn) << 8) | ((9) << 4) | (rm))
226 static void i_mul(int rd
, int rn
, int rm
)
231 static void i_cmp(int op
, int rn
, int rm
)
233 oi(ADD(op
, 0, rn
, 1, 0, 14) | rm
);
237 static void i_set(int cond
, int rd
)
241 oi(ADD(I_MOV
, rd
, 0, 0, 1, 14));
242 oi(ADD(I_MOV
, rd
, 0, 0, 1, cond
) | 1);
249 static void i_shl(int sm
, int rd
, int rm
, int rs
)
251 oi(ADD(I_MOV
, rd
, 0, 0, 0, 14) | (rs
<< 8) | (sm
<< 5) | (1 << 4) | rm
);
254 static void i_shl_imm(int sm
, int rd
, int n
)
256 oi(ADD(I_MOV
, rd
, 0, 0, 0, 14) | (n
<< 7) | (sm
<< 5) | rd
);
259 static void i_mov(int op
, int rd
, int rn
)
261 oi(ADD(op
, rd
, 0, 0, 0, 14) | rn
);
265 * single data transfer:
266 * +------------------------------------------+
267 * |COND|01|I|P|U|B|W|L| Rn | Rd | offset |
268 * +------------------------------------------+
270 * I: immediate/offset
271 * P: post/pre indexing
277 * Rd: source/destination register
279 * I=0 offset=| immediate |
280 * I=1 offset=| shift | Rm |
282 * halfword and signed data transfer
283 * +----------------------------------------------+
284 * |COND|000|P|U|0|W|L| Rn | Rd |0000|1|S|H|1| Rm |
285 * +----------------------------------------------+
287 * +----------------------------------------------+
288 * |COND|000|P|U|1|W|L| Rn | Rd |off1|1|S|H|1|off2|
289 * +----------------------------------------------+
294 #define LDR(l, rd, rn, b, u, p, w) \
295 ((14 << 28) | (1 << 26) | ((p) << 24) | ((b) << 22) | ((u) << 23) | \
296 ((w) << 21) | ((l) << 20) | ((rn) << 16) | ((rd) << 12))
297 #define LDRH(l, rd, rn, s, h, u, i) \
298 ((14 << 28) | (1 << 24) | ((u) << 23) | ((i) << 22) | ((l) << 20) | \
299 ((rn) << 16) | ((rd) << 12) | ((s) << 6) | ((h) << 5) | (9 << 4))
301 static void i_ldr(int l
, int rd
, int rn
, int off
, int bt
)
303 int b
= BT_SZ(bt
) == 1;
304 int h
= BT_SZ(bt
) == 2;
305 int s
= l
&& (bt
& BT_SIGNED
);
306 int half
= h
|| (b
&& s
);
307 int maximm
= half
? 0x100 : 0x1000;
311 while (off
>= maximm
) {
312 int imm
= add_encimm(off
);
313 oi(ADD(neg
? I_SUB
: I_ADD
, rd
, rn
, 0, 1, 14) | imm
);
315 off
-= add_decimm(imm
);
318 oi(LDR(l
, rd
, rn
, b
, !neg
, 1, 0) | off
);
320 oi(LDRH(l
, rd
, rn
, s
, h
, !neg
, 1) |
321 ((off
& 0xf0) << 4) | (off
& 0x0f));
324 static void i_sym(int rd
, char *sym
, int off
)
327 int doff
= pool_reloc(sym
, off
);
328 i_ldr(1, rd
, REG_DP
, doff
, LONGSZ
);
332 static void i_neg(int rd
)
334 oi(ADD(I_RSB
, rd
, rd
, 0, 1, 14));
337 static void i_not(int rd
)
339 oi(ADD(I_MVN
, rd
, 0, rd
, 1, 14));
342 static void i_lnot(int rd
)
344 i_cmp(I_TST
, rd
, rd
);
345 oi(ADD(I_MOV
, rd
, 0, 0, 1, 14));
346 oi(ADD(I_MOV
, rd
, 0, 0, 1, 0) | 1);
349 /* rd = rd & ((1 << bits) - 1) */
350 static void i_zx(int rd
, int bits
)
353 oi(ADD(I_AND
, rd
, rd
, 0, 1, 14) | add_encimm((1 << bits
) - 1));
355 i_shl_imm(SM_LSL
, rd
, 32 - bits
);
356 i_shl_imm(SM_LSR
, rd
, 32 - bits
);
360 static void i_sx(int rd
, int bits
)
362 i_shl_imm(SM_LSL
, rd
, 32 - bits
);
363 i_shl_imm(SM_ASR
, rd
, 32 - bits
);
368 * +-----------------------------------+
369 * |COND|101|L| offset |
370 * +-----------------------------------+
374 #define BL(cond, l, o) (((cond) << 28) | (5 << 25) | ((l) << 24) | \
375 ((((o) - 8) >> 2) & 0x00ffffff))
377 static void i_b(long addr
)
379 oi(BL(14, 0, addr
- cslen
));
382 static void i_b_if(long addr
, int rn
, int z
)
384 static int nots
[] = {1, 0, 3, 2, -1, -1, -1, -1, 9, 8, 11, 10, 13, 12, -1};
385 if (last_cmp
+ 8 != cslen
|| last_set
!= last_cmp
) {
386 i_cmp(I_TST
, rn
, rn
);
387 oi(BL(z
? 0 : 1, 0, addr
- cslen
));
391 oi(BL(z
? nots
[last_cond
] : last_cond
, 0, addr
- cslen
));
394 static void i_b_fill(long *dst
, int diff
)
396 *dst
= (*dst
& 0xff000000) | (((diff
- 8) >> 2) & 0x00ffffff);
399 static void i_memcpy(int rd
, int rs
, int rn
)
401 oi(ADD(I_TST
, 0, rn
, 1, 0, 14) | rn
);
403 oi(LDR(1, REG_TMP
, rs
, 1, 1, 0, 0) | 1);
404 oi(LDR(0, REG_TMP
, rd
, 1, 1, 0, 0) | 1);
405 oi(ADD(I_SUB
, rn
, rn
, 0, 1, 14) | 1);
409 static void i_memset(int rd
, int rs
, int rn
)
411 oi(ADD(I_TST
, 0, rn
, 1, 0, 14) | rn
);
413 oi(LDR(0, rs
, rd
, 1, 1, 0, 0) | 1);
414 oi(ADD(I_SUB
, rn
, rn
, 0, 1, 14) | 1);
418 static void i_call_reg(int rd
)
420 i_mov(I_MOV
, REG_LR
, REG_PC
);
421 i_mov(I_MOV
, REG_PC
, rd
);
424 static void i_call(char *sym
)
427 out_rel(sym
, OUT_CS
| OUT_REL24
, cslen
);
431 static void i_prolog(void)
435 oi(0xe1a0c00d); /* mov r12, sp */
436 oi(0xe92d000f); /* stmfd sp!, {r0-r3} */
437 oi(0xe92d5ff0); /* stmfd sp!, {r0-r11, r12, lr} */
438 oi(0xe1a0b00d); /* mov fp, sp */
439 oi(0xe24dd000); /* sub sp, sp, xx */
440 oi(0xe28fa000); /* add dp, pc, xx */
443 static void i_epilog(void)
446 oi(0xe89baff0); /* ldmfd fp, {r4-r11, sp, pc} */
447 dpoff
= add_decimm(add_rndimm(add_encimm(cslen
- func_beg
- 28)));
448 cslen
= func_beg
+ dpoff
+ 28;
449 maxsp
= ALIGN(maxsp
, 8);
450 maxsp
= add_decimm(add_rndimm(add_encimm(maxsp
)));
451 /* fill stack sub: sp = sp - xx */
452 *(long *) (cs
+ func_beg
+ 16) |= add_encimm(maxsp
);
453 /* fill data ptr addition: dp = pc + xx */
454 *(long *) (cs
+ func_beg
+ 20) |= add_encimm(dpoff
);
458 static long sp_push(int size
)
466 static void tmp_mem(struct tmp
*tmp
)
469 if (!(tmp
->loc
== LOC_REG
))
473 tmp
->addr
= -sp_push(LONGSZ
);
474 i_ldr(0, src
, REG_FP
, tmp
->addr
, LONGSZ
);
479 static void num_cast(struct tmp
*t
, unsigned bt
)
481 if (!(bt
& BT_SIGNED
) && BT_SZ(bt
) != LONGSZ
)
482 t
->addr
&= ((1l << (long) (BT_SZ(bt
) * 8)) - 1);
483 if (bt
& BT_SIGNED
&& BT_SZ(bt
) != LONGSZ
&&
484 t
->addr
> (1l << (BT_SZ(bt
) * 8 - 1)))
485 t
->addr
= -((1l << (BT_SZ(bt
) * 8)) - t
->addr
);
488 static void tmp_reg(struct tmp
*tmp
, int dst
, int deref
)
495 if (tmp
->loc
== LOC_NUM
) {
496 i_num(dst
, tmp
->addr
);
501 if (tmp
->loc
== LOC_SYM
) {
502 i_sym(dst
, tmp
->sym
, tmp
->off
);
507 if (tmp
->loc
== LOC_REG
) {
509 i_ldr(1, dst
, tmp
->addr
, 0, bt
);
510 else if (dst
!= tmp
->addr
)
511 i_mov(I_MOV
, dst
, tmp
->addr
);
512 regs
[tmp
->addr
] = NULL
;
514 if (tmp
->loc
== LOC_LOCAL
) {
516 i_ldr(1, dst
, REG_FP
, tmp
->addr
+ tmp
->off
, bt
);
518 i_add_imm(I_ADD
, dst
, REG_FP
, tmp
->addr
+ tmp
->off
);
520 if (tmp
->loc
== LOC_MEM
) {
521 i_ldr(1, dst
, REG_FP
, tmp
->addr
, LONGSZ
);
523 i_ldr(1, dst
, dst
, 0, bt
);
530 static void reg_free(int reg
)
535 for (i
= 0; i
< ARRAY_SIZE(tmpregs
); i
++)
536 if (!regs
[tmpregs
[i
]]) {
537 tmp_reg(regs
[reg
], tmpregs
[i
], 0);
543 static void reg_for(int reg
, struct tmp
*t
)
545 if (regs
[reg
] && regs
[reg
] != t
)
549 static void tmp_mv(struct tmp
*t
, int reg
)
555 static void tmp_to(struct tmp
*t
, int reg
)
561 static void tmp_drop(int n
)
564 for (i
= ntmp
- n
; i
< ntmp
; i
++)
565 if (tmps
[i
].loc
== LOC_REG
)
566 regs
[tmps
[i
].addr
] = NULL
;
570 static void tmp_pop(int reg
)
572 struct tmp
*t
= TMP(0);
577 static struct tmp
*tmp_new(void)
579 return &tmps
[ntmp
++];
582 static void tmp_push(int reg
)
584 struct tmp
*t
= tmp_new();
591 void o_local(long addr
)
593 struct tmp
*t
= tmp_new();
602 struct tmp
*t
= tmp_new();
608 void o_sym(char *name
)
610 struct tmp
*t
= tmp_new();
611 strcpy(t
->sym
, name
);
617 void o_tmpdrop(int n
)
619 if (n
== -1 || n
> ntmp
)
629 #define FORK_REG 0x00
631 /* make sure tmps remain intact after a conditional expression */
635 for (i
= 0; i
< ntmp
- 1; i
++)
639 void o_forkpush(void)
644 void o_forkjoin(void)
651 struct tmp
*t1
= TMP(0);
652 struct tmp
*t2
= TMP(1);
654 memcpy(&t
, t1
, sizeof(t
));
655 memcpy(t1
, t2
, sizeof(t
));
656 memcpy(t2
, &t
, sizeof(t
));
657 if (t1
->loc
== LOC_REG
)
659 if (t2
->loc
== LOC_REG
)
663 static int reg_get(int mask
)
666 for (i
= 0; i
< ARRAY_SIZE(tmpregs
); i
++)
667 if ((1 << tmpregs
[i
]) & mask
&& !regs
[tmpregs
[i
]])
669 for (i
= 0; i
< ARRAY_SIZE(tmpregs
); i
++)
670 if ((1 << tmpregs
[i
]) & mask
) {
671 reg_free(tmpregs
[i
]);
677 static void tmp_copy(struct tmp
*t1
)
679 struct tmp
*t2
= tmp_new();
680 memcpy(t2
, t1
, sizeof(*t1
));
681 if (!(t1
->loc
& (LOC_REG
| LOC_MEM
)))
683 if (t1
->loc
== LOC_MEM
) {
684 tmp_reg(t2
, reg_get(~0), 0);
685 } else if (t1
->loc
== LOC_REG
) {
686 t2
->addr
= TMP_REG2(t2
, t1
->addr
);
687 i_mov(I_MOV
, t2
->addr
, t1
->addr
);
697 void o_cast(unsigned bt
)
699 struct tmp
*t
= TMP(0);
704 if (t
->loc
== LOC_NUM
) {
708 if (BT_SZ(bt
) != LONGSZ
) {
709 int reg
= TMP_REG(t
);
712 i_sx(reg
, BT_SZ(bt
) * 8);
714 i_zx(reg
, BT_SZ(bt
) * 8);
718 void o_func_beg(char *name
, int global
)
720 out_sym(name
, (global
? OUT_GLOB
: 0) | OUT_CS
, cslen
, 0);
727 memset(regs
, 0, sizeof(regs
));
730 void o_deref(unsigned bt
)
732 struct tmp
*t
= TMP(0);
734 tmp_to(t
, TMP_REG(t
));
740 struct tmp
*t
= TMP(0);
741 tmp_to(t
, TMP_REG(t
));
744 #define TMP_NUM(t) ((t)->loc == LOC_NUM && !(t)->bt)
745 #define LOCAL_PTR(t) ((t)->loc == LOC_LOCAL && !(t)->bt)
746 #define SYM_PTR(t) ((t)->loc == LOC_SYM && !(t)->bt)
748 int o_popnum(long *c
)
750 struct tmp
*t
= TMP(0);
764 ret
[nret
++] = o_jmp(0);
767 void o_func_end(void)
770 for (i
= 0; i
< nret
; i
++)
775 long o_mklocal(int size
)
777 return sp_push(ALIGN(size
, LONGSZ
));
780 void o_rmlocal(long addr
, int sz
)
787 return -(10 + i
) << 2;
790 void o_assign(unsigned bt
)
792 struct tmp
*t1
= TMP(0);
793 struct tmp
*t2
= TMP(1);
794 int r1
= TMP_REG(t1
);
795 int r2
= TMP_REG2(t2
, r1
);
800 if (t2
->loc
== LOC_LOCAL
) {
802 off
= t2
->addr
+ t2
->off
;
807 i_ldr(0, r1
, r2
, off
, bt
);
811 static long cu(int op
, long i
)
824 static int c_uop(int op
)
826 struct tmp
*t1
= TMP(0);
830 o_num(cu(op
, t1
->addr
));
834 static long cb(int op
, long a
, long b
)
859 return (unsigned long) a
>> b
;
876 static int c_bop(int op
)
878 struct tmp
*t1
= TMP(0);
879 struct tmp
*t2
= TMP(1);
880 int locals
= LOCAL_PTR(t1
) + LOCAL_PTR(t2
);
881 int syms
= SYM_PTR(t1
) + SYM_PTR(t2
);
882 int nums
= TMP_NUM(t1
) + TMP_NUM(t2
);
883 if (syms
+ locals
== 2 || syms
+ nums
+ locals
!= 2)
886 if ((op
!= O_ADD
&& op
!= O_SUB
) || (op
== O_SUB
&& TMP_NUM(t2
)))
889 long o1
= TMP_NUM(t1
) ? t1
->addr
: t1
->off
;
890 long o2
= TMP_NUM(t2
) ? t2
->addr
: t2
->off
;
891 long ret
= cb(op
, o2
, o1
);
897 long ret
= cb(op
, t2
->addr
, t1
->addr
);
906 int r1
= TMP_REG(TMP(0));
923 static void bin_regs(int *r1
, int *r2
)
925 struct tmp
*t2
= TMP(0);
926 struct tmp
*t1
= TMP(1);
929 *r1
= TMP_REG2(t1
, *r2
);
934 static void bin_add(int op
)
936 /* opcode for O_ADD, O_SUB, O_AND, O_OR, O_XOR */
937 static int rx
[] = {I_ADD
, I_SUB
, I_AND
, I_ORR
, I_EOR
};
940 i_add(rx
[op
& 0xff], r1
, r1
, r2
);
944 static void bin_shx(int op
)
949 if ((op
& 0x0f) == 1)
950 sm
= op
& O_SIGNED
? SM_ASR
: SM_LSR
;
951 i_shl(sm
, r1
, r1
, r2
);
955 static int log2a(unsigned long n
)
958 for (i
= 0; i
< LONGSZ
* 8; i
++)
961 if (i
== LONGSZ
* 8 || !(n
>> (i
+ 1)))
966 /* optimized version of mul/div/mod for powers of two */
967 static int mul_2(int op
)
969 struct tmp
*t1
= TMP(0);
970 struct tmp
*t2
= TMP(1);
974 if (op
== O_MUL
&& t2
->loc
== LOC_NUM
&& !t2
->bt
)
976 if (t1
->loc
!= LOC_NUM
|| t1
->bt
)
993 i_shl_imm(SM_LSL
, r2
, p
);
1002 i_shl_imm(SM_LSR
, r2
, p
);
1020 static void bin_div(int op
)
1022 struct tmp
*t2
= TMP(0);
1023 struct tmp
*t1
= TMP(1);
1027 if ((op
& 0xff) == O_DIV
)
1028 func
= op
& O_SIGNED
? "__divdi3" : "__udivdi3";
1030 func
= op
& O_SIGNED
? "__moddi3" : "__umoddi3";
1031 for (i
= 0; i
< ARRAY_SIZE(argregs
); i
++)
1032 if (regs
[argregs
[i
]] && regs
[argregs
[i
]] - tmps
< ntmp
- 2)
1033 tmp_mem(regs
[argregs
[i
]]);
1034 tmp_to(t1
, argregs
[0]);
1035 tmp_to(t2
, argregs
[1]);
1041 static void bin_mul(int op
)
1044 if (!mul_2(op
& 0xff))
1046 if ((op
& 0xff) == O_DIV
|| (op
& 0xff) == O_MOD
) {
1055 static void bin_cmp(int op
)
1057 /* lt, gt, le, ge, eq, neq */
1058 static int ucond
[] = {3, 8, 9, 2, 0, 1};
1059 static int scond
[] = {11, 12, 13, 10, 0, 1};
1062 i_cmp(I_CMP
, r1
, r2
);
1063 i_set(op
& O_SIGNED
? scond
[op
& 0x0f] : ucond
[op
& 0x0f], r1
);
1071 if ((op
& 0xf0) == 0x00)
1073 if ((op
& 0xf0) == 0x10)
1075 if ((op
& 0xf0) == 0x20)
1077 if ((op
& 0xf0) == 0x30)
1081 static void load_regs2(int *r0
, int *r1
, int *r2
)
1083 struct tmp
*t0
= TMP(0);
1084 struct tmp
*t1
= TMP(1);
1085 struct tmp
*t2
= TMP(2);
1087 *r1
= TMP_REG2(t1
, *r0
);
1088 *r2
= TMP_REG3(t2
, *r0
, *r1
);
1097 load_regs2(&rn
, &rs
, &rd
);
1098 i_memcpy(rd
, rs
, rn
);
1105 load_regs2(&rn
, &rs
, &rd
);
1106 i_memset(rd
, rs
, rn
);
1110 long o_mklabel(void)
1115 static long jxz(long addr
, int z
)
1117 int r
= TMP_REG(TMP(0));
1123 long o_jz(long addr
)
1125 return jxz(addr
, 1);
1128 long o_jnz(long addr
)
1130 return jxz(addr
, 0);
1133 long o_jmp(long addr
)
1139 void o_filljmp2(long addr
, long jmpdst
)
1141 i_b_fill((void *) cs
+ addr
, jmpdst
- addr
);
1144 void o_filljmp(long addr
)
1146 o_filljmp2(addr
, cslen
);
1149 void o_call(int argc
, int rets
)
1153 int aregs
= MIN(ARRAY_SIZE(argregs
), argc
);
1154 for (i
= 0; i
< ARRAY_SIZE(tmpregs
); i
++)
1155 if (regs
[tmpregs
[i
]] && regs
[tmpregs
[i
]] - tmps
< ntmp
- argc
)
1156 tmp_mem(regs
[tmpregs
[i
]]);
1158 sp_push(LONGSZ
* (argc
- aregs
));
1159 for (i
= argc
- 1; i
>= aregs
; --i
) {
1160 int reg
= TMP_REG(TMP(0));
1162 i_ldr(0, reg
, REG_SP
, (i
- aregs
) * LONGSZ
, LONGSZ
);
1165 for (i
= aregs
- 1; i
>= 0; --i
)
1166 tmp_to(TMP(aregs
- i
- 1), argregs
[i
]);
1169 if (t
->loc
== LOC_SYM
&& !t
->bt
) {
1173 int reg
= t
->loc
== LOC_REG
? t
->addr
: REG_TMP
;
1191 void dat_bss(char *name
, int size
, int global
)
1193 out_sym(name
, OUT_BSS
| (global
? OUT_GLOB
: 0), bsslen
, size
);
1194 bsslen
+= ALIGN(size
, LONGSZ
);
1197 #define MAXDATS (1 << 10)
1198 static char dat_names
[MAXDATS
][NAMELEN
];
1199 static int dat_offs
[MAXDATS
];
1202 void err(char *msg
);
1203 void *dat_dat(char *name
, int size
, int global
)
1205 void *addr
= ds
+ dslen
;
1208 err("nomem: MAXDATS reached!\n");
1209 strcpy(dat_names
[idx
], name
);
1210 dat_offs
[idx
] = dslen
;
1211 out_sym(name
, OUT_DS
| (global
? OUT_GLOB
: 0), dslen
, size
);
1212 dslen
+= ALIGN(size
, LONGSZ
);
1216 static int dat_off(char *name
)
1219 for (i
= 0; i
< ndats
; i
++)
1220 if (!strcmp(name
, dat_names
[i
]))
1225 void o_datset(char *name
, int off
, unsigned bt
)
1227 struct tmp
*t
= TMP(0);
1228 int sym_off
= dat_off(name
) + off
;
1229 if (t
->loc
== LOC_NUM
&& !t
->bt
) {
1231 memcpy(ds
+ sym_off
, &t
->addr
, BT_SZ(bt
));
1233 if (t
->loc
== LOC_SYM
&& !t
->bt
) {
1234 out_rel(t
->sym
, OUT_DS
, sym_off
);
1235 memcpy(ds
+ sym_off
, &t
->off
, BT_SZ(bt
));
1240 /* compiled division functions; div.s contains the source */
1241 static int udivdi3
[] = {
1242 0xe3a02000, 0xe3a03000, 0xe1110001, 0x0a00000a,
1243 0xe1b0c211, 0xe2822001, 0x5afffffc, 0xe3a0c001,
1244 0xe2522001, 0x4a000004, 0xe1500211, 0x3afffffb,
1245 0xe0400211, 0xe083321c, 0xeafffff8, 0xe1a01000,
1246 0xe1a00003, 0xe1a0f00e,
1248 static int umoddi3
[] = {
1249 0xe92d4000, 0xebffffeb, 0xe1a00001, 0xe8bd8000,
1251 static int divdi3
[] = {
1252 0xe92d4010, 0xe1100000, 0x42600000, 0x43a04001,
1253 0xe1110001, 0x42600000, 0x42444001, 0xebffffe1,
1254 0xe1140004, 0x12400000, 0xe8bd8010,
1256 static int moddi3
[] = {
1257 0xe92d4000, 0xebfffff2, 0xe1a00001, 0xe8bd8000,
1260 void o_write(int fd
)
1263 out_sym("__udivdi3", OUT_CS
, cslen
, 0);
1264 os(udivdi3
, sizeof(udivdi3
));
1265 out_sym("__umoddi3", OUT_CS
, cslen
, 0);
1266 os(umoddi3
, sizeof(umoddi3
));
1267 out_sym("__divdi3", OUT_CS
, cslen
, 0);
1268 os(divdi3
, sizeof(divdi3
));
1269 out_sym("__moddi3", OUT_CS
, cslen
, 0);
1270 os(moddi3
, sizeof(moddi3
));
1272 out_write(fd
, cs
, cslen
, ds
, dslen
);