📄 an386.c
字号:
/*
Copyright 1994-2003 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
This program is derived from the cc68k complier by
Matthew Brandt (mattb@walkingdog.net)
You may contact the author of this derivative at:
mailto::camille@bluegrass.net
or by snail mail at:
David Lindauer
850 Washburn Ave Apt 99
Louisville, KY 40222
*/
/*
* Register analysis
*/
#include <stdio.h>
#include <string.h>
#include "lists.h"
#include "expr.h"
#include "c.h"
#include "gen386.h"
#include "diag.h"
#include "rtti.h"
/* pc-relative expressions not optimized */
extern int nextlabel;
extern AMODE push[], pop[];
extern OCODE *peep_tail;
extern SYM *currentfunc;
extern int prm_stackcheck, prm_farkeyword;
extern int floatregs, dataregs, addrregs;
extern int cf_maxfloat, cf_maxaddress, cf_maxdata, cf_freeaddress, cf_freedata,
cf_freefloat;
extern CSE *olist; /* list of optimizable expressions */
extern int funcfloat;
extern SYM *declclass;
extern int conscount;
extern TRYBLOCK *try_block_list;
extern int prm_cplusplus, prm_xcept;
long lc_maxauto;
int fsave_mask, save_mask;
OCODE *frame_ins;
int floatstack_mode;
extern int startlab;
extern int prm_smartframes;
extern int prm_xcept, conscount, xceptoffs;
void reserveregs(int *datareg, int *addreg, int *floatreg)
/*
* Reserve regs goes through and reserves a register for variables with
* the REGISTER keyword. Note that it currently does register allocation
* backwards... NOT the standard I suspect.
*/
{
CSE *csp = olist;
while (csp)
{
switch (csp->exp->nodetype)
{
case en_fcomplexref:
case en_rcomplexref:
case en_lrcomplexref:
case en_fimaginaryref:
case en_rimaginaryref:
case en_lrimaginaryref:
case en_floatref:
case en_doubleref:
case en_longdoubleref:
case en_ll_ref:
case en_ull_ref:
case en_fp_ref:
break;
case en_bool_ref:
case en_b_ref:
case en_w_ref:
case en_ub_ref:
case en_uw_ref:
case en_l_ref:
case en_ul_ref:
case en_i_ref:
case en_ui_ref:
case en_a_ref: case en_ua_ref:
if (csp->exp->v.p[0]->nodetype != en_autoreg)
break;
if ((csp->duses <= csp->uses / 4) && (*datareg < cf_maxdata) &&
dataregs)
csp->reg = (*datareg)++;
else if (!(csp->size == - 1 || csp->size == 1 || csp->size ==
5) && (*addreg < cf_maxaddress) && addrregs)
{
csp->reg = (*addreg)++;
}
else if ((*datareg < cf_maxdata) && dataregs)
csp->reg = (*datareg)++;
if (csp->reg != - 1)
{
int size = (csp->size < 0 ? - csp->size: csp->size);
((SYM*)csp->exp->v.p[0]->v.p[0])->mainsym->inreg = TRUE;
((SYM*)csp->exp->v.p[0]->v.p[0])->mainsym->value.i = - csp
->reg - size * 256;
}
break;
}
csp = csp->next;
}
}
/* Assums the dseg is the next higher seg value from the cseg */
void loadds(void)
{
gen_code(op_push, makesegreg(DS), 0);
gen_code(op_push, makesegreg(CS), 0);
gen_codes(op_add, 2, make_stack(0), make_immed(8));
gen_code(op_pop, makesegreg(DS), 0);
}
//-------------------------------------------------------------------------
void allocate(int datareg, int addreg, int floatreg, SNODE *block)
/*
* allocate will allocate registers for the expressions that have
* a high enough desirability. It also puts the function
* header, consisting of saved registers and stack decrments for local
* variables
*/
{
CSE *csp;
ENODE *exptr;
unsigned mask, rmask, i, fmask, frmask, size;
AMODE *ap, *ap2;
mask = 0;
rmask = 0;
fmask = frmask = 0;
for (i = cf_freedata; i < datareg; i++)
{
rmask = rmask | (1 << (15-i));
mask = mask | (1 << i);
}
for (i = cf_freeaddress + 16; i < addreg; i++)
{
rmask = rmask | (1 << (23-i));
mask = mask | (1 << (i - 8));
}
while (bsort(&olist))
;
/* sort the expression list */
csp = olist;
while (csp != 0)
{
if (csp->reg == - 1 && !(csp->exp->cflags &DF_VOL) && !csp->voidf)
{
if (desire(csp) < 3)
csp->reg = - 1;
else
{
if (csp->exp->nodetype == en_rcon || csp->exp->nodetype ==
en_fcon || csp->exp->nodetype == en_lrcon || csp->exp
->nodetype == en_floatref || csp->exp->nodetype ==
en_doubleref || csp->exp->nodetype == en_longdoubleref ||
csp->exp->nodetype == en_fimaginarycon ||
csp->exp->nodetype == en_rimaginarycon ||
csp->exp->nodetype == en_lrimaginarycon ||
csp->exp->nodetype == en_fimaginaryref ||
csp->exp->nodetype == en_rimaginaryref ||
csp->exp->nodetype == en_lrimaginaryref) {}
else if ((csp->duses <= csp->uses / 4) && (datareg < cf_maxdata)
&& dataregs)
csp->reg = (datareg)++;
else if (!(csp->size == 1 || csp->size == - 1 || csp->size ==
5) && (addreg < cf_maxaddress) && addrregs)
csp->reg = (addreg)++;
else if ((datareg < cf_maxdata) && dataregs)
csp->reg = (datareg)++;
if (csp->reg != - 1)
// if (lvalue(csp->exp))
// csp->seg = defseg(csp->exp->v.p[0]) ;
// else
csp->seg = defseg(csp->exp);
}
}
if (csp->reg != - 1)
{
if (lvalue(csp->exp) && !((SYM*)csp->exp->v.p[0]->v.p[0])->funcparm)
{
((SYM*)csp->exp->v.p[0]->v.p[0])->mainsym->inreg = TRUE;
((SYM*)csp->exp->v.p[0]->v.p[0])->mainsym->value.i = - csp
->reg - (csp->size < 0 ? - csp->size: csp->size) *256;
}
if (csp->reg < 16)
{
rmask = rmask | (1 << (15-csp->reg));
mask = mask | (1 << csp->reg);
}
if (csp->reg < 32)
{
rmask = rmask | (1 << (23-csp->reg));
mask = mask | (1 << (csp->reg - 8));
}
else
{
frmask = frmask | (1 << (39-csp->reg));
fmask = fmask | (1 << (csp->reg - 32));
}
}
csp = csp->next;
}
allocstack(); /* Allocate stack space for the local vars */
floatstack_mode = 0; /* no space for floating point temps */
if (currentfunc->intflag || currentfunc->faultflag)
{
mask = 0;
rmask = 0;
if (currentfunc->loadds)
loadds();
if (prm_farkeyword)
{
gen_push(ES + 24, am_dreg, 0);
gen_push(FS + 24, am_dreg, 0);
gen_push(GS + 24, am_dreg, 0);
}
gen_code(op_pushad, 0, 0);
}
if ((conscount || try_block_list || currentfunc->value.classdata.throwlist
&& currentfunc->value.classdata.throwlist->data) && prm_xcept)
{
xceptoffs = lc_maxauto += sizeof(XCEPTDATA);
}
if (prm_cplusplus && prm_xcept || (funcfloat || lc_maxauto || currentfunc
->tp->lst.head && currentfunc->tp->lst.head != (SYM*) - 1)
|| (currentfunc->value.classdata.cppflags &PF_MEMBER) && !(currentfunc
->value.classdata.cppflags &PF_STATIC)
|| !prm_smartframes)
{
/* enter is *really* inefficient so we will not use it */
if (!currentfunc->intflag)
gen_codes(op_push, 4, makedreg(EBP), 0);
gen_codes(op_mov, 4, makedreg(EBP), makedreg(ESP));
if (lc_maxauto)
gen_code(op_sub, makedreg(ESP), make_immed(lc_maxauto));
// FIXME ... could auto-alloc an FP value when no frame!
frame_ins = peep_tail;
}
else
frame_ins = 0;
if (mask != 0)
pushregs(rmask);
save_mask = mask;
if (fmask != 0)
fsave_mask = fmask;
if (currentfunc->loadds && !currentfunc->intflag)
{
loadds();
}
if (prm_stackcheck && lc_maxauto)
{
AMODE *ap1;
ap = set_symbol("__stackerror", 1);
ap1 = set_symbol("__stackbottom", 0);
ap1->mode = am_direct;
gen_codes(op_cmp, 4, makedreg(ESP), ap1);
gen_codes(op_jb, 0, ap, 0);
}
AddProfilerData();
if ((conscount || try_block_list || currentfunc->value.classdata.throwlist
&& currentfunc->value.classdata.throwlist->data) && prm_xcept)
{
currentfunc->value.classdata.conslabel = nextlabel++;
currentfunc->value.classdata.destlabel = nextlabel++;
gen_codes(op_mov, 4, makedreg(EAX), make_label(nextlabel - 2));
call_library("__InitExceptBlock");
gen_label(nextlabel - 1);
}
}
//-------------------------------------------------------------------------
void loadregs(void)
/*
* initialize allocated registers
*/
{
CSE *csp;
ENODE *exptr;
unsigned mask, rmask, i, fmask, frmask, size;
AMODE *ap, *ap2;
csp = olist;
while (csp != 0)
{
int sz;
if (csp->reg != - 1)
{
/* see if preload needed */
exptr = csp->exp;
if (!lvalue(exptr) || ((SYM*)exptr->v.p[0]->v.p[0])->funcparm)
{
exptr = csp->exp;
initstack();
sz = csp->size;
ap = gen_expr(exptr, FALSE, FALSE, sz);
if (csp->reg < 16)
{
if (ap->mode == am_dreg)
peep_tail->oper1->preg = csp->reg;
else
{
if (csp->reg > 3 && chksize(BESZ_WORD, sz))
DIAG("byte sized non-register in analyze");
ap2 = makedreg(csp->reg);
ap2->length = BESZ_DWORD;
if (ap->mode == am_immed || ap->length == BESZ_DWORD || ap
->length == - BESZ_DWORD)
gen_codes(op_mov, BESZ_DWORD, ap2, ap);
else
if (sz < 0)
gen_code(op_movsx, ap2, ap);
else
gen_code(op_movzx, ap2, ap);
}
}
else
if (csp->reg < 32)
{
if (ap->mode == am_dreg)
peep_tail->oper1->preg = csp->reg - 12;
else
{
if (csp->reg - 12 > 3 && chksize(BESZ_WORD, sz))
DIAG("byte sized non-register in analyze");
ap2 = makedreg(csp->reg - 12);
ap2->length = BESZ_DWORD;
if (ap->mode == am_immed || ap->length == BESZ_DWORD || ap
->length == - BESZ_DWORD)
gen_codes(op_mov, BESZ_DWORD, ap2, ap);
else
if (sz < 0)
gen_code(op_movsx, ap2, ap);
else
gen_code(op_movzx, ap2, ap);
}
}
else
{
/* Should never get here */
DIAG("float reg assigned in analyze");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -