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 void i_add(int op
, int rd
, int rn
, int rm
)
155 oi(ADD(op
, rd
, rn
, 0, 0, 14) | rm
);
158 static int add_encimm(unsigned n
)
161 while (i
< 12 && (n
>> ((4 + i
) << 1)))
163 return (n
>> (i
<< 1)) | (((16 - i
) & 0x0f) << 8);
166 static int add_decimm(unsigned n
)
168 int rot
= (16 - ((n
>> 8) & 0x0f)) & 0x0f;
169 return (n
& 0xff) << (rot
<< 1);
172 static int add_rndimm(unsigned n
)
174 int rot
= (n
>> 8) & 0x0f;
180 rot
= (rot
+ 12) & 0x0f;
182 return ((num
+ 1) & 0xff) | (rot
<< 8);
185 static void i_ldr(int l
, int rd
, int rn
, int off
, int bt
);
187 static void i_num(int rd
, long n
)
190 int p
= neg
? -n
- 1 : n
;
191 int enc
= add_encimm(p
);
192 if (p
== add_decimm(enc
)) {
193 oi(ADD(neg
? I_MVN
: I_MOV
, rd
, 0, 0, 1, 14) | enc
);
196 int off
= pool_num(n
);
197 i_ldr(1, rd
, REG_DP
, off
, LONGSZ
);
202 static void i_add_imm(int op
, int rd
, int rn
, int n
)
205 int imm
= add_encimm(neg
? -n
: n
);
206 if (imm
== add_decimm(neg
? -n
: n
)) {
207 oi(ADD(neg
? I_SUB
: I_ADD
, rd
, rn
, 0, 1, 14) | imm
);
210 i_add(I_ADD
, rd
, rd
, rn
);
216 * +----------------------------------------+
217 * |COND|000000|A|S| Rd | Rn | Rs |1001| Rm |
218 * +----------------------------------------+
222 * C: set condition codes
224 * I=0 operand2=| shift | Rm |
225 * I=1 operand2=|rota| imm |
227 #define MUL(rd, rn, rs) \
228 ((14 << 28) | ((rd) << 16) | ((0) << 12) | ((rn) << 8) | ((9) << 4) | (rm))
230 static void i_mul(int rd
, int rn
, int rm
)
235 static void i_cmp(int op
, int rn
, int rm
)
237 oi(ADD(op
, 0, rn
, 1, 0, 14) | rm
);
240 static void i_set(int cond
, int rd
)
242 oi(ADD(I_MOV
, rd
, 0, 0, 1, 14));
243 oi(ADD(I_MOV
, rd
, 0, 0, 1, cond
) | 1);
250 static void i_shl(int sm
, int rd
, int rm
, int rs
)
252 oi(ADD(I_MOV
, rd
, 0, 0, 0, 14) | (rs
<< 8) | (sm
<< 5) | (1 << 4) | rm
);
255 static void i_shl_imm(int sm
, int rd
, int n
)
257 oi(ADD(I_MOV
, rd
, 0, 0, 0, 14) | (n
<< 7) | (sm
<< 5) | rd
);
260 static void i_mov(int op
, int rd
, int rn
)
262 oi(ADD(op
, rd
, 0, 0, 0, 14) | rn
);
266 * single data transfer:
267 * +------------------------------------------+
268 * |COND|01|I|P|U|B|W|L| Rn | Rd | offset |
269 * +------------------------------------------+
271 * I: immediate/offset
272 * P: post/pre indexing
278 * Rd: source/destination register
280 * I=0 offset=| immediate |
281 * I=1 offset=| shift | Rm |
283 * halfword and signed data transfer
284 * +----------------------------------------------+
285 * |COND|000|P|U|0|W|L| Rn | Rd |0000|1|S|H|1| Rm |
286 * +----------------------------------------------+
288 * +----------------------------------------------+
289 * |COND|000|P|U|1|W|L| Rn | Rd |off1|1|S|H|1|off2|
290 * +----------------------------------------------+
295 #define LDR(l, rd, rn, b, u, p, w) \
296 ((14 << 28) | (1 << 26) | ((p) << 24) | ((b) << 22) | ((u) << 23) | \
297 ((w) << 21) | ((l) << 20) | ((rn) << 16) | ((rd) << 12))
298 #define LDRH(l, rd, rn, s, h, u, i) \
299 ((14 << 28) | (1 << 24) | ((u) << 23) | ((i) << 22) | ((l) << 20) | \
300 ((rn) << 16) | ((rd) << 12) | ((s) << 6) | ((h) << 5) | (9 << 4))
302 static void i_ldr(int l
, int rd
, int rn
, int off
, int bt
)
304 int b
= BT_SZ(bt
) == 1;
305 int h
= BT_SZ(bt
) == 2;
306 int s
= l
&& (bt
& BT_SIGNED
);
307 int half
= h
|| (b
&& s
);
308 int maximm
= half
? 0x100 : 0x1000;
312 while (off
>= maximm
) {
313 int imm
= add_encimm(off
);
314 oi(ADD(neg
? I_SUB
: I_ADD
, REG_TMP
, rn
, 0, 1, 14) | imm
);
316 off
-= add_decimm(imm
);
319 oi(LDR(l
, rd
, rn
, b
, !neg
, 1, 0) | off
);
321 oi(LDRH(l
, rd
, rn
, s
, h
, !neg
, 1) |
322 ((off
& 0xf0) << 4) | (off
& 0x0f));
325 static void i_sym(int rd
, char *sym
, int off
)
328 int doff
= pool_reloc(sym
, off
);
329 i_ldr(1, rd
, REG_DP
, doff
, LONGSZ
);
333 static void i_neg(int rd
)
335 oi(ADD(I_RSB
, rd
, rd
, 0, 1, 14));
338 static void i_not(int rd
)
340 oi(ADD(I_MVN
, rd
, 0, 0, 0, 14) | rd
);
343 static void i_lnot(int rd
)
345 i_cmp(I_TST
, rd
, rd
);
346 oi(ADD(I_MOV
, rd
, 0, 0, 1, 14));
347 oi(ADD(I_MOV
, rd
, 0, 0, 1, 0) | 1);
350 /* rd = rd & ((1 << bits) - 1) */
351 static void i_zx(int rd
, int bits
)
354 oi(ADD(I_AND
, rd
, rd
, 0, 1, 14) | add_encimm((1 << bits
) - 1));
356 i_shl_imm(SM_LSL
, rd
, 32 - bits
);
357 i_shl_imm(SM_LSR
, rd
, 32 - bits
);
361 static void i_sx(int rd
, int bits
)
363 i_shl_imm(SM_LSL
, rd
, 32 - bits
);
364 i_shl_imm(SM_ASR
, rd
, 32 - bits
);
369 * +-----------------------------------+
370 * |COND|101|L| offset |
371 * +-----------------------------------+
375 #define BL(cond, l, o) (((cond) << 28) | (5 << 25) | ((l) << 24) | \
376 ((((o) - 8) >> 2) & 0x00ffffff))
378 static void i_b(long addr
)
380 oi(BL(14, 0, addr
- cslen
));
383 static void i_b_if(long addr
, int rn
, int z
)
385 i_cmp(I_TST
, rn
, rn
);
386 oi(BL(z
? 0 : 1, 0, addr
- cslen
));
389 static void i_b_fill(long *dst
, int diff
)
391 *dst
= (*dst
& 0xff000000) | (((diff
- 8) >> 2) & 0x00ffffff);
394 static void i_memcpy(int rd
, int rs
, int rn
)
396 oi(ADD(I_SUB
, rn
, rn
, 1, 1, 14) | 1);
398 oi(LDR(1, REG_TMP
, rs
, 1, 1, 0, 0) | 1);
399 oi(LDR(0, REG_TMP
, rd
, 1, 1, 0, 0) | 1);
403 static void i_memset(int rd
, int rs
, int rn
)
405 oi(ADD(I_SUB
, rn
, rn
, 1, 1, 14) | 1);
407 oi(LDR(0, rs
, rd
, 1, 1, 0, 0) | 1);
411 static void i_call_reg(int rd
)
413 i_mov(I_MOV
, REG_LR
, REG_PC
);
414 i_mov(I_MOV
, REG_PC
, rd
);
417 static void i_call(char *sym
)
420 out_rel(sym
, OUT_CS
| OUT_REL24
, cslen
);
424 static void i_prolog(void)
428 oi(0xe1a0c00d); /* mov r12, sp */
429 oi(0xe92d000f); /* stmfd sp!, {r0-r3} */
430 oi(0xe92d5ff0); /* stmfd sp!, {r0-r11, r12, lr} */
431 oi(0xe1a0b00d); /* mov fp, sp */
432 oi(0xe24dd000); /* sub sp, sp, xx */
433 oi(0xe28fa000); /* add dp, pc, xx */
436 static void i_epilog(void)
439 oi(0xe89baff0); /* ldmfd fp, {r4-r11, sp, pc} */
440 dpoff
= add_decimm(add_rndimm(add_encimm(cslen
- func_beg
- 28)));
441 cslen
= func_beg
+ dpoff
+ 28;
442 maxsp
= ALIGN(maxsp
, 8);
443 maxsp
= add_decimm(add_rndimm(add_encimm(maxsp
)));
444 /* fill stack sub: sp = sp - xx */
445 *(long *) (cs
+ func_beg
+ 16) |= add_encimm(maxsp
);
446 /* fill data ptr addition: dp = pc + xx */
447 *(long *) (cs
+ func_beg
+ 20) |= add_encimm(dpoff
);
451 static long sp_push(int size
)
459 static void tmp_mem(struct tmp
*tmp
)
462 if (!(tmp
->loc
== LOC_REG
))
466 tmp
->addr
= -sp_push(LONGSZ
);
467 i_ldr(0, src
, REG_FP
, tmp
->addr
, LONGSZ
);
472 static void num_cast(struct tmp
*t
, unsigned bt
)
474 if (!(bt
& BT_SIGNED
) && BT_SZ(bt
) != LONGSZ
)
475 t
->addr
&= ((1l << (long) (BT_SZ(bt
) * 8)) - 1);
476 if (bt
& BT_SIGNED
&& BT_SZ(bt
) != LONGSZ
&&
477 t
->addr
> (1l << (BT_SZ(bt
) * 8 - 1)))
478 t
->addr
= -((1l << (BT_SZ(bt
) * 8)) - t
->addr
);
481 static void tmp_reg(struct tmp
*tmp
, int dst
, int deref
)
488 if (tmp
->loc
== LOC_NUM
) {
489 i_num(dst
, tmp
->addr
);
494 if (tmp
->loc
== LOC_SYM
) {
495 i_sym(dst
, tmp
->sym
, tmp
->off
);
500 if (tmp
->loc
== LOC_REG
) {
502 i_ldr(1, dst
, tmp
->addr
, 0, bt
);
503 else if (dst
!= tmp
->addr
)
504 i_mov(I_MOV
, dst
, tmp
->addr
);
505 regs
[tmp
->addr
] = NULL
;
507 if (tmp
->loc
== LOC_LOCAL
) {
509 i_ldr(1, dst
, REG_FP
, tmp
->addr
+ tmp
->off
, bt
);
511 i_add_imm(I_ADD
, dst
, REG_FP
, tmp
->addr
+ tmp
->off
);
513 if (tmp
->loc
== LOC_MEM
) {
514 i_ldr(1, dst
, REG_FP
, tmp
->addr
, LONGSZ
);
516 i_ldr(1, dst
, dst
, 0, bt
);
523 static void reg_free(int reg
)
528 for (i
= 0; i
< ARRAY_SIZE(tmpregs
); i
++)
529 if (!regs
[tmpregs
[i
]]) {
530 tmp_reg(regs
[reg
], tmpregs
[i
], 0);
536 static void reg_for(int reg
, struct tmp
*t
)
538 if (regs
[reg
] && regs
[reg
] != t
)
542 static void tmp_mv(struct tmp
*t
, int reg
)
548 static void tmp_to(struct tmp
*t
, int reg
)
554 static void tmp_drop(int n
)
557 for (i
= ntmp
- n
; i
< ntmp
; i
++)
558 if (tmps
[i
].loc
== LOC_REG
)
559 regs
[tmps
[i
].addr
] = NULL
;
563 static void tmp_pop(int reg
)
565 struct tmp
*t
= TMP(0);
570 static struct tmp
*tmp_new(void)
572 return &tmps
[ntmp
++];
575 static void tmp_push(int reg
)
577 struct tmp
*t
= tmp_new();
584 void o_local(long addr
)
586 struct tmp
*t
= tmp_new();
595 struct tmp
*t
= tmp_new();
601 void o_sym(char *name
)
603 struct tmp
*t
= tmp_new();
604 strcpy(t
->sym
, name
);
610 void o_tmpdrop(int n
)
612 if (n
== -1 || n
> ntmp
)
622 #define FORK_REG 0x00
624 /* make sure tmps remain intact after a conditional expression */
628 for (i
= 0; i
< ntmp
- 1; i
++)
632 void o_forkpush(void)
637 void o_forkjoin(void)
644 struct tmp
*t1
= TMP(0);
645 struct tmp
*t2
= TMP(1);
647 memcpy(&t
, t1
, sizeof(t
));
648 memcpy(t1
, t2
, sizeof(t
));
649 memcpy(t2
, &t
, sizeof(t
));
650 if (t1
->loc
== LOC_REG
)
652 if (t2
->loc
== LOC_REG
)
656 static int reg_get(int mask
)
659 for (i
= 0; i
< ARRAY_SIZE(tmpregs
); i
++)
660 if ((1 << tmpregs
[i
]) & mask
&& !regs
[tmpregs
[i
]])
662 for (i
= 0; i
< ARRAY_SIZE(tmpregs
); i
++)
663 if ((1 << tmpregs
[i
]) & mask
) {
664 reg_free(tmpregs
[i
]);
670 static int reg_fortmp(struct tmp
*t
, int notmask
)
672 if (t
->loc
== LOC_REG
&& !(notmask
& (1 << t
->addr
)))
674 return reg_get(~notmask
);
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_mv(t2
, reg_get(~0));
685 } else if (t1
->loc
== LOC_REG
) {
686 t2
->addr
= reg_fortmp(t2
, 1 << t1
->addr
);
687 i_mov(I_MOV
, t2
->addr
, t1
->addr
);
697 void o_cast(unsigned bt
)
699 struct tmp
*t
= TMP(0);
700 if (!t
->bt
&& t
->loc
== LOC_NUM
) {
704 if (BT_SZ(bt
) != LONGSZ
) {
705 int reg
= reg_fortmp(t
, 0);
708 i_sx(reg
, BT_SZ(bt
) * 8);
710 i_zx(reg
, BT_SZ(bt
) * 8);
714 void o_func_beg(char *name
, int global
)
716 out_sym(name
, (global
? OUT_GLOB
: 0) | OUT_CS
, cslen
, 0);
723 memset(regs
, 0, sizeof(regs
));
726 void o_deref(unsigned bt
)
728 struct tmp
*t
= TMP(0);
730 tmp_to(t
, reg_fortmp(t
, 0));
736 struct tmp
*t
= TMP(0);
737 tmp_to(t
, reg_fortmp(t
, 0));
740 #define TMP_NUM(t) ((t)->loc == LOC_NUM && !(t)->bt)
741 #define LOCAL_PTR(t) ((t)->loc == LOC_LOCAL && !(t)->bt)
742 #define SYM_PTR(t) ((t)->loc == LOC_SYM && !(t)->bt)
744 int o_popnum(long *c
)
746 struct tmp
*t
= TMP(0);
760 ret
[nret
++] = o_jmp(0);
763 void o_func_end(void)
766 for (i
= 0; i
< nret
; i
++)
771 long o_mklocal(int size
)
773 return sp_push(ALIGN(size
, LONGSZ
));
776 void o_rmlocal(long addr
, int sz
)
781 long o_arg2loc(int i
)
783 return -(10 + i
) << 2;
786 void o_assign(unsigned bt
)
788 struct tmp
*t1
= TMP(0);
789 struct tmp
*t2
= TMP(1);
790 int r1
= reg_fortmp(t1
, 0);
791 int r2
= reg_fortmp(t2
, 1 << r1
);
796 if (t2
->loc
== LOC_LOCAL
) {
798 off
= t2
->addr
+ t2
->off
;
803 i_ldr(0, r1
, r2
, off
, bt
);
807 static long cu(int op
, long i
)
820 static int c_uop(int op
)
822 struct tmp
*t1
= TMP(0);
826 o_num(cu(op
, t1
->addr
));
830 static long cb(int op
, long a
, long b
)
855 return (unsigned long) a
>> b
;
872 static int c_bop(int op
)
874 struct tmp
*t1
= TMP(0);
875 struct tmp
*t2
= TMP(1);
876 int locals
= LOCAL_PTR(t1
) + LOCAL_PTR(t2
);
877 int syms
= SYM_PTR(t1
) + SYM_PTR(t2
);
878 int nums
= TMP_NUM(t1
) + TMP_NUM(t2
);
879 if (syms
+ locals
== 2 || syms
+ nums
+ locals
!= 2)
882 if ((op
& 0xff) != O_ADD
&& ((op
& 0xff) != O_SUB
|| TMP_NUM(t2
)))
885 long o1
= TMP_NUM(t1
) ? t1
->addr
: t1
->off
;
886 long o2
= TMP_NUM(t2
) ? t2
->addr
: t2
->off
;
887 long ret
= cb(op
, o2
, o1
);
893 long ret
= cb(op
, t2
->addr
, t1
->addr
);
902 int r1
= reg_fortmp(TMP(0), 0);
919 static void bin_regs(int *r1
, int *r2
)
921 struct tmp
*t2
= TMP(0);
922 struct tmp
*t1
= TMP(1);
923 *r2
= reg_fortmp(t2
, 0);
925 *r1
= reg_fortmp(t1
, 1 << *r2
);
930 static void bin_add(int op
)
932 /* opcode for O_ADD, O_SUB, O_AND, O_OR, O_XOR */
933 static int rx
[] = {I_ADD
, I_SUB
, I_AND
, I_ORR
, I_EOR
};
936 i_add(rx
[op
& 0x0f], r1
, r1
, r2
);
940 static void bin_shx(int op
)
945 if ((op
& 0x0f) == 1)
946 sm
= op
& O_SIGNED
? SM_ASR
: SM_LSR
;
947 i_shl(sm
, r1
, r1
, r2
);
951 static int log2a(unsigned long n
)
954 for (i
= 0; i
< LONGSZ
* 8; i
++)
957 if (i
== LONGSZ
* 8 || !(n
>> (i
+ 1)))
962 /* optimized version of mul/div/mod for powers of two */
963 static int mul_2(int op
)
965 struct tmp
*t1
= TMP(0);
966 struct tmp
*t2
= TMP(1);
970 if ((op
& 0xff) == O_MUL
&& t2
->loc
== LOC_NUM
&& !t2
->bt
)
972 if (t1
->loc
!= LOC_NUM
|| t1
->bt
)
978 if ((op
& 0xff) == O_MUL
) {
987 r2
= reg_fortmp(t2
, 0);
989 i_shl_imm(SM_LSL
, r2
, p
);
996 r2
= reg_fortmp(t2
, 0);
998 i_shl_imm(op
& O_SIGNED
? SM_ASR
: SM_LSR
, r2
, p
);
1008 r2
= reg_fortmp(t2
, 0);
1016 static void bin_div(int op
)
1018 struct tmp
*t2
= TMP(0);
1019 struct tmp
*t1
= TMP(1);
1023 if ((op
& 0xff) == O_DIV
)
1024 func
= op
& O_SIGNED
? "__divdi3" : "__udivdi3";
1026 func
= op
& O_SIGNED
? "__moddi3" : "__umoddi3";
1027 for (i
= 0; i
< ARRAY_SIZE(argregs
); i
++)
1028 if (regs
[argregs
[i
]] && regs
[argregs
[i
]] - tmps
< ntmp
- 2)
1029 tmp_mem(regs
[argregs
[i
]]);
1030 tmp_to(t1
, argregs
[0]);
1031 tmp_to(t2
, argregs
[1]);
1037 static void bin_mul(int op
)
1042 if ((op
& 0xff) == O_DIV
|| (op
& 0xff) == O_MOD
) {
1051 static void bin_cmp(int op
)
1053 /* lt, gt, le, ge, eq, neq */
1054 static int ucond
[] = {3, 8, 9, 2, 0, 1};
1055 static int scond
[] = {11, 12, 13, 10, 0, 1};
1058 i_cmp(I_CMP
, r1
, r2
);
1059 i_set(op
& O_SIGNED
? scond
[op
& 0x0f] : ucond
[op
& 0x0f], r1
);
1067 if ((op
& 0xf0) == 0x00)
1069 if ((op
& 0xf0) == 0x10)
1071 if ((op
& 0xf0) == 0x20)
1073 if ((op
& 0xf0) == 0x30)
1077 static void load_regs2(int *r0
, int *r1
, int *r2
)
1079 struct tmp
*t0
= TMP(0);
1080 struct tmp
*t1
= TMP(1);
1081 struct tmp
*t2
= TMP(2);
1082 *r0
= reg_fortmp(t0
, 0);
1083 *r1
= reg_fortmp(t1
, 1 << *r0
);
1084 *r2
= reg_fortmp(t2
, (1 << *r0
) | (1 << *r1
));
1093 load_regs2(&rn
, &rs
, &rd
);
1094 i_memcpy(rd
, rs
, rn
);
1101 load_regs2(&rn
, &rs
, &rd
);
1102 i_memset(rd
, rs
, rn
);
1106 long o_mklabel(void)
1111 static long jxz(long addr
, int z
)
1113 int r
= reg_fortmp(TMP(0), 0);
1119 long o_jz(long addr
)
1121 return jxz(addr
, 1);
1124 long o_jnz(long addr
)
1126 return jxz(addr
, 0);
1129 long o_jmp(long addr
)
1135 void o_filljmp2(long addr
, long jmpdst
)
1137 i_b_fill((void *) cs
+ addr
, jmpdst
- addr
);
1140 void o_filljmp(long addr
)
1142 o_filljmp2(addr
, cslen
);
1145 void o_call(int argc
, int rets
)
1149 int aregs
= MIN(ARRAY_SIZE(argregs
), argc
);
1150 for (i
= 0; i
< ARRAY_SIZE(argregs
); i
++)
1151 if (regs
[argregs
[i
]] && regs
[argregs
[i
]] - tmps
< ntmp
- argc
)
1152 tmp_mem(regs
[argregs
[i
]]);
1154 sp_push(LONGSZ
* (argc
- aregs
));
1155 for (i
= argc
- 1; i
>= aregs
; --i
) {
1156 int reg
= reg_fortmp(TMP(0), 0);
1158 i_ldr(0, reg
, REG_SP
, (i
- aregs
) * LONGSZ
, LONGSZ
);
1161 for (i
= aregs
- 1; i
>= 0; --i
)
1162 tmp_to(TMP(aregs
- i
- 1), argregs
[i
]);
1165 if (t
->loc
== LOC_SYM
&& !t
->bt
) {
1169 int reg
= t
->loc
== LOC_REG
? t
->addr
: REG_TMP
;
1187 void o_mkbss(char *name
, int size
, int global
)
1189 out_sym(name
, OUT_BSS
| (global
? OUT_GLOB
: 0), bsslen
, size
);
1190 bsslen
+= ALIGN(size
, LONGSZ
);
1193 #define MAXDATS (1 << 10)
1194 static char dat_names
[MAXDATS
][NAMELEN
];
1195 static int dat_offs
[MAXDATS
];
1198 void err(char *msg
);
1199 void *o_mkdat(char *name
, int size
, int global
)
1201 void *addr
= ds
+ dslen
;
1204 err("nomem: MAXDATS reached!\n");
1205 strcpy(dat_names
[idx
], name
);
1206 dat_offs
[idx
] = dslen
;
1207 out_sym(name
, OUT_DS
| (global
? OUT_GLOB
: 0), dslen
, size
);
1208 dslen
+= ALIGN(size
, LONGSZ
);
1212 static int dat_off(char *name
)
1215 for (i
= 0; i
< ndats
; i
++)
1216 if (!strcmp(name
, dat_names
[i
]))
1221 void o_datset(char *name
, int off
, unsigned bt
)
1223 struct tmp
*t
= TMP(0);
1224 int sym_off
= dat_off(name
) + off
;
1225 if (t
->loc
== LOC_NUM
&& !t
->bt
) {
1227 memcpy(ds
+ sym_off
, &t
->addr
, BT_SZ(bt
));
1229 if (t
->loc
== LOC_SYM
&& !t
->bt
) {
1230 out_rel(t
->sym
, OUT_DS
, sym_off
);
1231 memcpy(ds
+ sym_off
, &t
->off
, BT_SZ(bt
));
1236 /* compiled division functions; div.s contains the source */
1237 static int udivdi3
[] = {
1238 0xe3a02000, 0xe3a03000, 0xe1110001, 0x0a00000a,
1239 0xe1b0c211, 0xe2822001, 0x5afffffc, 0xe3a0c001,
1240 0xe2522001, 0x4a000004, 0xe1500211, 0x3afffffb,
1241 0xe0400211, 0xe083321c, 0xeafffff8, 0xe1a01000,
1242 0xe1a00003, 0xe1a0f00e,
1244 static int umoddi3
[] = {
1245 0xe92d4000, 0xebffffeb, 0xe1a00001, 0xe8bd8000,
1247 static int divdi3
[] = {
1248 0xe92d4030, 0xe1a04000, 0xe1a05001, 0xe1100000,
1249 0x42600000, 0xe1110001, 0x42611000, 0xebffffe1,
1250 0xe1340005, 0x42600000, 0xe1140004, 0x42611000,
1253 static int moddi3
[] = {
1254 0xe92d4000, 0xebfffff0, 0xe1a00001, 0xe8bd8000,
1257 void o_write(int fd
)
1260 out_sym("__udivdi3", OUT_CS
, cslen
, 0);
1261 os(udivdi3
, sizeof(udivdi3
));
1262 out_sym("__umoddi3", OUT_CS
, cslen
, 0);
1263 os(umoddi3
, sizeof(umoddi3
));
1264 out_sym("__divdi3", OUT_CS
, cslen
, 0);
1265 os(divdi3
, sizeof(divdi3
));
1266 out_sym("__moddi3", OUT_CS
, cslen
, 0);
1267 os(moddi3
, sizeof(moddi3
));
1269 out_write(fd
, cs
, cslen
, ds
, dslen
);