History

x86-64: use r8,r9 as load/store registers

grischka committed 16 years ago

Commit 0e5c0ee1 parent cda8c411 file · +74 −43Browse files

Unified · Split

x86_64-gen.c+74−43
@@ -23,7 +23,7 @@ · 22 unmodified lines
2323 #ifdef TARGET_DEFS_ONLY
2424
2525 /* number of available registers */
26−#define NB_REGS 5
26+#define NB_REGS 10
2727 #define NB_ASM_REGS 8
2828
2929 /* a register can belong to several classes. The classes must be
@@ -34,6 +34,8 @@ · 4 unmodified lines
3434 #define RC_RAX 0x0004
3535 #define RC_RCX 0x0008
3636 #define RC_RDX 0x0010
37+#define RC_R8 0x0100
38+#define RC_R9 0x0200
3739 #define RC_XMM0 0x0020
3840 #define RC_ST0 0x0040 /* only for long double */
3941 #define RC_IRET RC_RAX /* function return: integer register */
@@ -45,16 +47,17 @@ enum { · 5 unmodified lines
4547 TREG_RAX = 0,
4648 TREG_RCX = 1,
4749 TREG_RDX = 2,
50+ TREG_XMM0 = 3,
51+ TREG_ST0 = 4,
52+
4853 TREG_RSI = 6,
4954 TREG_RDI = 7,
5055 TREG_R8 = 8,
5156 TREG_R9 = 9,
57+
5258 TREG_R10 = 10,
5359 TREG_R11 = 11,
5460
55− TREG_XMM0 = 3,
56− TREG_ST0 = 4,
57−
5861 TREG_MEM = 0x10,
5962 };
6063
@@ -107,6 +110,13 @@ ST_DATA const int reg_classes[NB_REGS] = { · 46 unmodified lines
107110 /* edx */ RC_INT | RC_RDX,
108111 /* xmm0 */ RC_FLOAT | RC_XMM0,
109112 /* st0 */ RC_ST0,
113+#if NB_REGS == 10
114+ 0,
115+ 0,
116+ 0,
117+ RC_R8,
118+ RC_R9,
119+#endif
110120 };
111121
112122 static unsigned long func_sub_sp_offset;
@@ -286,7 +296,7 @@ static void gen_modrm_impl(int op_reg, int r, Sym *sym, int c, int is_got) · 173 unmodified lines
286296 g(0x00 | op_reg | REG_VALUE(r));
287297 }
288298 } else {
289− g(0x00 | op_reg | (r & VT_VALMASK));
299+ g(0x00 | op_reg | REG_VALUE(r));
290300 }
291301 }
292302
@@ -364,62 +374,78 @@ void load(int r, SValue *sv) · 71 unmodified lines
364374 } else if ((ft & VT_BTYPE) == VT_LDOUBLE) {
365375 o(0xdb); /* fldt */
366376 r = 5;
367− } else if ((ft & VT_TYPE) == VT_BYTE) {
368− o(0xbe0f); /* movsbl */
369− } else if ((ft & VT_TYPE) == (VT_BYTE | VT_UNSIGNED)) {
370− o(0xb60f); /* movzbl */
371− } else if ((ft & VT_TYPE) == VT_SHORT) {
372− o(0xbf0f); /* movswl */
373− } else if ((ft & VT_TYPE) == (VT_SHORT | VT_UNSIGNED)) {
374− o(0xb70f); /* movzwl */
375− } else if (is64_type(ft)) {
376− gen_modrm64(0x8b, r, fr, sv->sym, fc);
377− return;
378377 } else {
379− o(0x8b); /* movl */
378+ int x;
379+ if ((ft & VT_TYPE) == VT_BYTE) {
380+ x = 0xbe0f; /* movsbl */
381+ } else if ((ft & VT_TYPE) == (VT_BYTE | VT_UNSIGNED)) {
382+ x = 0xb60f; /* movzbl */
383+ } else if ((ft & VT_TYPE) == VT_SHORT) {
384+ x = 0xbf0f; /* movswl */
385+ } else if ((ft & VT_TYPE) == (VT_SHORT | VT_UNSIGNED)) {
386+ x = 0xb70f; /* movzwl */
387+ } else if (is64_type(ft)) {
388+ gen_modrm64(0x8b, r, fr, sv->sym, fc);
389+ return;
390+ } else {
391+ x = 0x8b; /* movl */
392+ }
393+ if (REX_BASE(r) || (!(fr & ~0x0f) && REX_BASE(fr)))
394+ o(0x40 + REX_BASE(fr) + (REX_BASE(r) << 2));
395+ o(x);
380396 }
381397 gen_modrm(r, fr, sv->sym, fc);
382398 } else {
383399 if (v == VT_CONST) {
384400 if (fr & VT_SYM) {
385401 #ifdef TCC_TARGET_PE
386− o(0x8d48);
402+ o(0x8d48 + (REX_BASE(r) << 2));
387403 o(0x05 + REG_VALUE(r) * 8); /* lea xx(%rip), r */
388404 gen_addrpc32(fr, sv->sym, fc);
389405 #else
390406 if (sv->sym->type.t & VT_STATIC) {
391− o(0x8d48);
407+ o(0x8d48 + REX_BASE(r));
392408 o(0x05 + REG_VALUE(r) * 8); /* lea xx(%rip), r */
393409 gen_addrpc32(fr, sv->sym, fc);
394410 } else {
395− o(0x8b48);
411+ o(0x8b48 + REX_BASE(r));
396412 o(0x05 + REG_VALUE(r) * 8); /* mov xx(%rip), r */
397413 gen_gotpcrel(r, sv->sym, fc);
398414 }
399415 #endif
400416 } else if (is64_type(ft)) {
401− o(0x48);
417+ o(0x48 + REX_BASE(r));
402418 o(0xb8 + REG_VALUE(r)); /* mov $xx, r */
403419 gen_le64(sv->c.ull);
404420 } else {
421+ if (REX_BASE(r))
422+ o(0x41);
405423 o(0xb8 + REG_VALUE(r)); /* mov $xx, r */
406424 gen_le32(fc);
407425 }
408426 } else if (v == VT_LOCAL) {
409− o(0x48 | REX_BASE(r));
427+ o(0x48 | (REX_BASE(r) << 2));
410428 o(0x8d); /* lea xxx(%ebp), r */
411429 gen_modrm(r, VT_LOCAL, sv->sym, fc);
412430 } else if (v == VT_CMP) {
413− oad(0xb8 + r, 0); /* mov $0, r */
431+ if (REX_BASE(r))
432+ o(0x41);
433+ oad(0xb8 + REG_VALUE(r), 0); /* mov $0, r */
434+ if (REX_BASE(r))
435+ o(0x41);
414436 o(0x0f); /* setxx %br */
415437 o(fc);
416− o(0xc0 + r);
438+ o(0xc0 + REG_VALUE(r));
417439 } else if (v == VT_JMP || v == VT_JMPI) {
418440 t = v & 1;
419− oad(0xb8 + r, t); /* mov $1, r */
420− o(0x05eb); /* jmp after */
441+ if (REX_BASE(r))
442+ o(0x41);
443+ oad(0xb8 + REG_VALUE(r), t); /* mov $1, r */
444+ o(0x05eb + (REX_BASE(r) << 8)); /* jmp after */
421445 gsym(fc);
422− oad(0xb8 + r, t ^ 1); /* mov $0, r */
446+ if (REX_BASE(r))
447+ o(0x41);
448+ oad(0xb8 + REG_VALUE(r), t ^ 1); /* mov $0, r */
423449 } else if (v != r) {
424450 if (r == TREG_XMM0) {
425451 assert(v == TREG_ST0);
@@ -438,7 +464,7 @@ void load(int r, SValue *sv) · 12 unmodified lines
438464 } else {
439465 o(0x48 | REX_BASE(r) | (REX_BASE(v) << 2));
440466 o(0x89);
441− o(0xc0 + r + v * 8); /* mov v, r */
467+ o(0xc0 + REG_VALUE(r) + REG_VALUE(v) * 8); /* mov v, r */
442468 }
443469 }
444470 }
@@ -505,9 +531,7 @@ void store(int r, SValue *v) · 60 unmodified lines
505531 o(op64);
506532 o(3 + (r << 3));
507533 } else if (op64) {
508− if (fr == VT_CONST ||
509− fr == VT_LOCAL ||
510− (v->r & VT_LVAL)) {
534+ if (fr == VT_CONST || fr == VT_LOCAL || (v->r & VT_LVAL)) {
511535 gen_modrm64(op64, r, v->r, v->sym, fc);
512536 } else if (fr != r) {
513537 /* XXX: don't we really come here? */
@@ -515,9 +539,7 @@ void store(int r, SValue *v) · 1 unmodified line
515539 o(0xc0 + fr + r * 8); /* mov r, fr */
516540 }
517541 } else {
518− if (fr == VT_CONST ||
519− fr == VT_LOCAL ||
520− (v->r & VT_LVAL)) {
542+ if (fr == VT_CONST || fr == VT_LOCAL || (v->r & VT_LVAL)) {
521543 gen_modrm(r, v->r, v->sym, fc);
522544 } else if (fr != r) {
523545 /* XXX: don't we really come here? */
@@ -591,7 +613,6 @@ void gfunc_call(int nb_args) · 67 unmodified lines
591613 }
592614
593615 args_size = (nb_reg_args < REGN ? REGN : nb_reg_args) * PTR_SIZE;
594− save_regs(0); /* save used temporary registers */
595616
596617 /* for struct arguments, we need to call memcpy and the function
597618 call breaks register passing arguments we are preparing.
@@ -627,12 +648,14 @@ void gfunc_call(int nb_args) · 29 unmodified lines
627648 if (func_scratch < args_size)
628649 func_scratch = args_size;
629650
651+ for (i = 0; i < REGN; ++i)
652+ save_reg(arg_regs[i]);
653+
630654 gen_reg = nb_reg_args;
631655 for(i = 0; i < nb_args; i++) {
632656 bt = (vtop->type.t & VT_BTYPE);
633657 if (bt == VT_STRUCT || bt == VT_LDOUBLE) {
634658 ; /* done */
635−
636659 } else if (is_sse_float(vtop->type.t)) {
637660 gv(RC_FLOAT); /* only one float register */
638661 j = --gen_reg;
@@ -641,31 +664,39 @@ void gfunc_call(int nb_args) · 2 unmodified lines
641664 /* movq %xmm0, j*8(%rsp) */
642665 gen_offs_sp(0x2444d6, 0, j*8);
643666 } else {
644− d = arg_regs[j];
645667 /* movaps %xmm0, %xmmN */
646668 o(0x280f);
647669 o(0xc0 + (j << 3));
670+ d = arg_regs[j];
648671 /* mov %xmm0, %rxx */
649672 o(0x66);
650− o(0x7e0f48 + (d >= 8));
651− o(0xc0 + (d & 7));
673+ o(0x7e0f48 + REX_BASE(d));
674+ o(0xc0 + REG_VALUE(d));
652675 }
653676 } else {
654− r = gv(RC_INT);
655677 j = --gen_reg;
656678 if (j >= REGN) {
679+ r = gv(RC_INT);
657680 o(0x48);
658681 gen_offs_sp(0x244489, r, j*8);
659682 } else {
660683 d = arg_regs[j];
661− if (d != r) {
662− o(0x8948 + (d >= 8));
663− o(0xc0 + r*8 + (d & 7));
684+ if (d < NB_REGS) {
685+ gv(reg_classes[d] & ~RC_INT);
686+ } else {
687+ r = gv(RC_INT);
688+ if (d != r) {
689+ o(0x8948 + REX_BASE(d));
690+ o(0xc0 + r*8 + REG_VALUE(d));
691+ }
664692 }
693+
665694 }
666695 }
667696 vtop--;
668697 }
698+
699+ save_regs(0);
669700 gcall_or_jmp(0);
670701 vtop--;
671702 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t