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_SUB
, rn
, rn
, 1, 1, 14) | 1);
403 oi(LDR(1, REG_TMP
, rs
, 1, 1, 0, 0) | 1);
404 oi(LDR(0, REG_TMP
, rd
, 1, 1, 0, 0) | 1);
408 static void i_memset(int rd
, int rs
, int rn
)
410 oi(ADD(I_SUB
, rn
, rn
, 1, 1, 14) | 1);
412 oi(LDR(0, rs
, rd
, 1, 1, 0, 0) | 1);
416 static void i_call_reg(int rd
)
418 i_mov(I_MOV
, REG_LR
, REG_PC
);
419 i_mov(I_MOV
, REG_PC
, rd
);
422 static void i_call(char *sym
)
425 out_rel(sym
, OUT_CS
| OUT_REL24
, cslen
);
429 static void i_prolog(void)
433 oi(0xe1a0c00d); /* mov r12, sp */
434 oi(0xe92d000f); /* stmfd sp!, {r0-r3} */
435 oi(0xe92d5ff0); /* stmfd sp!, {r0-r11, r12, lr} */
436 oi(0xe1a0b00d); /* mov fp, sp */
437 oi(0xe24dd000); /* sub sp, sp, xx */
438 oi(0xe28fa000); /* add dp, pc, xx */
441 static void i_epilog(void)
444 oi(0xe89baff0); /* ldmfd fp, {r4-r11, sp, pc} */
445 dpoff
= add_decimm(add_rndimm(add_encimm(cslen
- func_beg
- 28)));
446 cslen
= func_beg
+ dpoff
+ 28;
447 maxsp
= ALIGN(maxsp
, 8);
448 maxsp
= add_decimm(add_rndimm(add_encimm(maxsp
)));
449 /* fill stack sub: sp = sp - xx */
450 *(long *) (cs
+ func_beg
+ 16) |= add_encimm(maxsp
);
451 /* fill data ptr addition: dp = pc + xx */
452 *(long *) (cs
+ func_beg
+ 20) |= add_encimm(dpoff
);
456 static long sp_push(int size
)
464 static void tmp_mem(struct tmp
*tmp
)
467 if (!(tmp
->loc
== LOC_REG
))
471 tmp
->addr
= -sp_push(LONGSZ
);
472 i_ldr(0, src
, REG_FP
, tmp
->addr
, LONGSZ
);
477 static void num_cast(struct tmp
*t
, unsigned bt
)
479 if (!(bt
& BT_SIGNED
) && BT_SZ(bt
) != LONGSZ
)
480 t
->addr
&= ((1l << (long) (BT_SZ(bt
) * 8)) - 1);
481 if (bt
& BT_SIGNED
&& BT_SZ(bt
) != LONGSZ
&&
482 t
->addr
> (1l << (BT_SZ(bt
) * 8 - 1)))
483 t
->addr
= -((1l << (BT_SZ(bt
) * 8)) - t
->addr
);
486 static void tmp_reg(struct tmp
*tmp
, int dst
, int deref
)
493 if (tmp
->loc
== LOC_NUM
) {
494 i_num(dst
, tmp
->addr
);
499 if (tmp
->loc
== LOC_SYM
) {
500 i_sym(dst
, tmp
->sym
, tmp
->off
);
505 if (tmp
->loc
== LOC_REG
) {
507 i_ldr(1, dst
, tmp
->addr
, 0, bt
);
508 else if (dst
!= tmp
->addr
)
509 i_mov(I_MOV
, dst
, tmp
->addr
);
510 regs
[tmp
->addr
] = NULL
;
512 if (tmp
->loc
== LOC_LOCAL
) {
514 i_ldr(1, dst
, REG_FP
, tmp
->addr
+ tmp
->off
, bt
);
516 i_add_imm(I_ADD
, dst
, REG_FP
, tmp
->addr
+ tmp
->off
);
518 if (tmp
->loc
== LOC_MEM
) {
519 i_ldr(1, dst
, REG_FP
, tmp
->addr
, LONGSZ
);
521 i_ldr(1, dst
, dst
, 0, bt
);
528 static void reg_free(int reg
)
533 for (i
= 0; i
< ARRAY_SIZE(tmpregs
); i
++)
534 if (!regs
[tmpregs
[i
]]) {
535 tmp_reg(regs
[reg
], tmpregs
[i
], 0);
541 static void reg_for(int reg
, struct tmp
*t
)
543 if (regs
[reg
] && regs
[reg
] != t
)
547 static void tmp_mv(struct tmp
*t
, int reg
)
553 static void tmp_to(struct tmp
*t
, int reg
)
559 static void tmp_drop(int n
)
562 for (i
= ntmp
- n
; i
< ntmp
; i
++)
563 if (tmps
[i
].loc
== LOC_REG
)
564 regs
[tmps
[i
].addr
] = NULL
;
568 static void tmp_pop(int reg
)
570 struct tmp
*t
= TMP(0);
575 static struct tmp
*tmp_new(void)
577 return &tmps
[ntmp
++];
580 static void tmp_push(int reg
)
582 struct tmp
*t
= tmp_new();
589 void o_local(long addr
)
591 struct tmp
*t
= tmp_new();
600 struct tmp
*t
= tmp_new();
606 void o_sym(char *name
)
608 struct tmp
*t
= tmp_new();
609 strcpy(t
->sym
, name
);
615 void o_tmpdrop(int n
)
617 if (n
== -1 || n
> ntmp
)
627 #define FORK_REG 0x00
629 /* make sure tmps remain intact after a conditional expression */
633 for (i
= 0; i
< ntmp
- 1; i
++)
637 void o_forkpush(void)
642 void o_forkjoin(void)
649 struct tmp
*t1
= TMP(0);
650 struct tmp
*t2
= TMP(1);
652 memcpy(&t
, t1
, sizeof(t
));
653 memcpy(t1
, t2
, sizeof(t
));
654 memcpy(t2
, &t
, sizeof(t
));
655 if (t1
->loc
== LOC_REG
)
657 if (t2
->loc
== LOC_REG
)
661 static int reg_get(int mask
)
664 for (i
= 0; i
< ARRAY_SIZE(tmpregs
); i
++)
665 if ((1 << tmpregs
[i
]) & mask
&& !regs
[tmpregs
[i
]])
667 for (i
= 0; i
< ARRAY_SIZE(tmpregs
); i
++)
668 if ((1 << tmpregs
[i
]) & mask
) {
669 reg_free(tmpregs
[i
]);
675 static void tmp_copy(struct tmp
*t1
)
677 struct tmp
*t2
= tmp_new();
678 memcpy(t2
, t1
, sizeof(*t1
));
679 if (!(t1
->loc
& (LOC_REG
| LOC_MEM
)))
681 if (t1
->loc
== LOC_MEM
) {
682 tmp_reg(t2
, reg_get(~0), 0);
683 } else if (t1
->loc
== LOC_REG
) {
684 t2
->addr
= TMP_REG2(t2
, t1
->addr
);
685 i_mov(I_MOV
, t2
->addr
, t1
->addr
);
695 void o_cast(unsigned bt
)
697 struct tmp
*t
= TMP(0);
702 if (t
->loc
== LOC_NUM
) {
706 if (BT_SZ(bt
) != LONGSZ
) {
707 int reg
= TMP_REG(t
);
710 i_sx(reg
, BT_SZ(bt
) * 8);
712 i_zx(reg
, BT_SZ(bt
) * 8);
716 void o_func_beg(char *name
, int global
)
718 out_sym(name
, (global
? OUT_GLOB
: 0) | OUT_CS
, cslen
, 0);
725 memset(regs
, 0, sizeof(regs
));
728 void o_deref(unsigned bt
)
730 struct tmp
*t
= TMP(0);
732 tmp_to(t
, TMP_REG(t
));
738 struct tmp
*t
= TMP(0);
739 tmp_to(t
, TMP_REG(t
));
742 #define TMP_NUM(t) ((t)->loc == LOC_NUM && !(t)->bt)
743 #define LOCAL_PTR(t) ((t)->loc == LOC_LOCAL && !(t)->bt)
744 #define SYM_PTR(t) ((t)->loc == LOC_SYM && !(t)->bt)
746 int o_popnum(long *c
)
748 struct tmp
*t
= TMP(0);
762 ret
[nret
++] = o_jmp(0);
765 void o_func_end(void)
768 for (i
= 0; i
< nret
; i
++)
773 long o_mklocal(int size
)
775 return sp_push(ALIGN(size
, LONGSZ
));
778 void o_rmlocal(long addr
, int sz
)
785 return -(10 + i
) << 2;
788 void o_assign(unsigned bt
)
790 struct tmp
*t1
= TMP(0);
791 struct tmp
*t2
= TMP(1);
792 int r1
= TMP_REG(t1
);
793 int r2
= TMP_REG2(t2
, r1
);
798 if (t2
->loc
== LOC_LOCAL
) {
800 off
= t2
->addr
+ t2
->off
;
805 i_ldr(0, r1
, r2
, off
, bt
);
809 static long cu(int op
, long i
)
822 static int c_uop(int op
)
824 struct tmp
*t1
= TMP(0);
828 o_num(cu(op
, t1
->addr
));
832 static long cb(int op
, long a
, long b
)
857 return (unsigned long) a
>> b
;
874 static int c_bop(int op
)
876 struct tmp
*t1
= TMP(0);
877 struct tmp
*t2
= TMP(1);
878 int locals
= LOCAL_PTR(t1
) + LOCAL_PTR(t2
);
879 int syms
= SYM_PTR(t1
) + SYM_PTR(t2
);
880 int nums
= TMP_NUM(t1
) + TMP_NUM(t2
);
881 if (syms
+ locals
== 2 || syms
+ nums
+ locals
!= 2)
884 if ((op
& 0xff) != O_ADD
&& ((op
& 0xff) != O_SUB
|| TMP_NUM(t2
)))
887 long o1
= TMP_NUM(t1
) ? t1
->addr
: t1
->off
;
888 long o2
= TMP_NUM(t2
) ? t2
->addr
: t2
->off
;
889 long ret
= cb(op
, o2
, o1
);
895 long ret
= cb(op
, t2
->addr
, t1
->addr
);
904 int r1
= TMP_REG(TMP(0));
921 static void bin_regs(int *r1
, int *r2
)
923 struct tmp
*t2
= TMP(0);
924 struct tmp
*t1
= TMP(1);
927 *r1
= TMP_REG2(t1
, *r2
);
932 static void bin_add(int op
)
934 /* opcode for O_ADD, O_SUB, O_AND, O_OR, O_XOR */
935 static int rx
[] = {I_ADD
, I_SUB
, I_AND
, I_ORR
, I_EOR
};
938 i_add(rx
[op
& 0x0f], r1
, r1
, r2
);
942 static void bin_shx(int op
)
947 if ((op
& 0x0f) == 1)
948 sm
= op
& O_SIGNED
? SM_ASR
: SM_LSR
;
949 i_shl(sm
, r1
, r1
, r2
);
953 static int log2a(unsigned long n
)
956 for (i
= 0; i
< LONGSZ
* 8; i
++)
959 if (i
== LONGSZ
* 8 || !(n
>> (i
+ 1)))
964 /* optimized version of mul/div/mod for powers of two */
965 static int mul_2(int op
)
967 struct tmp
*t1
= TMP(0);
968 struct tmp
*t2
= TMP(1);
972 if ((op
& 0xff) == O_MUL
&& t2
->loc
== LOC_NUM
&& !t2
->bt
)
974 if (t1
->loc
!= LOC_NUM
|| t1
->bt
)
980 if ((op
& 0xff) == O_MUL
) {
991 i_shl_imm(SM_LSL
, r2
, p
);
1000 i_shl_imm(op
& O_SIGNED
? SM_ASR
: SM_LSR
, r2
, p
);
1018 static void bin_div(int op
)
1020 struct tmp
*t2
= TMP(0);
1021 struct tmp
*t1
= TMP(1);
1025 if ((op
& 0xff) == O_DIV
)
1026 func
= op
& O_SIGNED
? "__divdi3" : "__udivdi3";
1028 func
= op
& O_SIGNED
? "__moddi3" : "__umoddi3";
1029 for (i
= 0; i
< ARRAY_SIZE(argregs
); i
++)
1030 if (regs
[argregs
[i
]] && regs
[argregs
[i
]] - tmps
< ntmp
- 2)
1031 tmp_mem(regs
[argregs
[i
]]);
1032 tmp_to(t1
, argregs
[0]);
1033 tmp_to(t2
, argregs
[1]);
1039 static void bin_mul(int op
)
1044 if ((op
& 0xff) == O_DIV
|| (op
& 0xff) == O_MOD
) {
1053 static void bin_cmp(int op
)
1055 /* lt, gt, le, ge, eq, neq */
1056 static int ucond
[] = {3, 8, 9, 2, 0, 1};
1057 static int scond
[] = {11, 12, 13, 10, 0, 1};
1060 i_cmp(I_CMP
, r1
, r2
);
1061 i_set(op
& O_SIGNED
? scond
[op
& 0x0f] : ucond
[op
& 0x0f], r1
);
1069 if ((op
& 0xf0) == 0x00)
1071 if ((op
& 0xf0) == 0x10)
1073 if ((op
& 0xf0) == 0x20)
1075 if ((op
& 0xf0) == 0x30)
1079 static void load_regs2(int *r0
, int *r1
, int *r2
)
1081 struct tmp
*t0
= TMP(0);
1082 struct tmp
*t1
= TMP(1);
1083 struct tmp
*t2
= TMP(2);
1085 *r1
= TMP_REG2(t1
, *r0
);
1086 *r2
= TMP_REG3(t2
, *r0
, *r1
);
1095 load_regs2(&rn
, &rs
, &rd
);
1096 i_memcpy(rd
, rs
, rn
);
1103 load_regs2(&rn
, &rs
, &rd
);
1104 i_memset(rd
, rs
, rn
);
1108 long o_mklabel(void)
1113 static long jxz(long addr
, int z
)
1115 int r
= TMP_REG(TMP(0));
1121 long o_jz(long addr
)
1123 return jxz(addr
, 1);
1126 long o_jnz(long addr
)
1128 return jxz(addr
, 0);
1131 long o_jmp(long addr
)
1137 void o_filljmp2(long addr
, long jmpdst
)
1139 i_b_fill((void *) cs
+ addr
, jmpdst
- addr
);
1142 void o_filljmp(long addr
)
1144 o_filljmp2(addr
, cslen
);
1147 void o_call(int argc
, int rets
)
1151 int aregs
= MIN(ARRAY_SIZE(argregs
), argc
);
1152 for (i
= 0; i
< ARRAY_SIZE(argregs
); i
++)
1153 if (regs
[argregs
[i
]] && regs
[argregs
[i
]] - tmps
< ntmp
- argc
)
1154 tmp_mem(regs
[argregs
[i
]]);
1156 sp_push(LONGSZ
* (argc
- aregs
));
1157 for (i
= argc
- 1; i
>= aregs
; --i
) {
1158 int reg
= TMP_REG(TMP(0));
1160 i_ldr(0, reg
, REG_SP
, (i
- aregs
) * LONGSZ
, LONGSZ
);
1163 for (i
= aregs
- 1; i
>= 0; --i
)
1164 tmp_to(TMP(aregs
- i
- 1), argregs
[i
]);
1167 if (t
->loc
== LOC_SYM
&& !t
->bt
) {
1171 int reg
= t
->loc
== LOC_REG
? t
->addr
: REG_TMP
;
1189 void dat_bss(char *name
, int size
, int global
)
1191 out_sym(name
, OUT_BSS
| (global
? OUT_GLOB
: 0), bsslen
, size
);
1192 bsslen
+= ALIGN(size
, LONGSZ
);
1195 #define MAXDATS (1 << 10)
1196 static char dat_names
[MAXDATS
][NAMELEN
];
1197 static int dat_offs
[MAXDATS
];
1200 void err(char *msg
);
1201 void *dat_dat(char *name
, int size
, int global
)
1203 void *addr
= ds
+ dslen
;
1206 err("nomem: MAXDATS reached!\n");
1207 strcpy(dat_names
[idx
], name
);
1208 dat_offs
[idx
] = dslen
;
1209 out_sym(name
, OUT_DS
| (global
? OUT_GLOB
: 0), dslen
, size
);
1210 dslen
+= ALIGN(size
, LONGSZ
);
1214 static int dat_off(char *name
)
1217 for (i
= 0; i
< ndats
; i
++)
1218 if (!strcmp(name
, dat_names
[i
]))
1223 void o_datset(char *name
, int off
, unsigned bt
)
1225 struct tmp
*t
= TMP(0);
1226 int sym_off
= dat_off(name
) + off
;
1227 if (t
->loc
== LOC_NUM
&& !t
->bt
) {
1229 memcpy(ds
+ sym_off
, &t
->addr
, BT_SZ(bt
));
1231 if (t
->loc
== LOC_SYM
&& !t
->bt
) {
1232 out_rel(t
->sym
, OUT_DS
, sym_off
);
1233 memcpy(ds
+ sym_off
, &t
->off
, BT_SZ(bt
));
1238 /* compiled division functions; div.s contains the source */
1239 static int udivdi3
[] = {
1240 0xe3a02000, 0xe3a03000, 0xe1110001, 0x0a00000a,
1241 0xe1b0c211, 0xe2822001, 0x5afffffc, 0xe3a0c001,
1242 0xe2522001, 0x4a000004, 0xe1500211, 0x3afffffb,
1243 0xe0400211, 0xe083321c, 0xeafffff8, 0xe1a01000,
1244 0xe1a00003, 0xe1a0f00e,
1246 static int umoddi3
[] = {
1247 0xe92d4000, 0xebffffeb, 0xe1a00001, 0xe8bd8000,
1249 static int divdi3
[] = {
1250 0xe92d4030, 0xe1a04000, 0xe1a05001, 0xe1100000,
1251 0x42600000, 0xe1110001, 0x42611000, 0xebffffe1,
1252 0xe1340005, 0x42600000, 0xe1140004, 0x42611000,
1255 static int moddi3
[] = {
1256 0xe92d4000, 0xebfffff0, 0xe1a00001, 0xe8bd8000,
1259 void o_write(int fd
)
1262 out_sym("__udivdi3", OUT_CS
, cslen
, 0);
1263 os(udivdi3
, sizeof(udivdi3
));
1264 out_sym("__umoddi3", OUT_CS
, cslen
, 0);
1265 os(umoddi3
, sizeof(umoddi3
));
1266 out_sym("__divdi3", OUT_CS
, cslen
, 0);
1267 os(divdi3
, sizeof(divdi3
));
1268 out_sym("__moddi3", OUT_CS
, cslen
, 0);
1269 os(moddi3
, sizeof(moddi3
));
1271 out_write(fd
, cs
, cslen
, ds
, dslen
);