2008-06-04 15:37:38 -06:00
|
|
|
// Inferno utils/6c/gc.h
|
|
|
|
// http://code.google.com/p/inferno-os/source/browse/utils/6c/gc.h
|
|
|
|
//
|
|
|
|
// Copyright © 1994-1999 Lucent Technologies Inc. All rights reserved.
|
|
|
|
// Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net)
|
|
|
|
// Portions Copyright © 1997-1999 Vita Nuova Limited
|
|
|
|
// Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
|
|
|
|
// Portions Copyright © 2004,2006 Bruce Ellis
|
|
|
|
// Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net)
|
|
|
|
// Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
|
|
|
|
// Portions Copyright © 2009 The Go Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
// THE SOFTWARE.
|
|
|
|
|
2011-07-01 09:13:38 -06:00
|
|
|
#include <u.h>
|
2008-06-04 15:37:38 -06:00
|
|
|
#include "../cc/cc.h"
|
|
|
|
#include "../6l/6.out.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 6c/amd64
|
|
|
|
* Intel 386 with AMD64 extensions
|
|
|
|
*/
|
|
|
|
#define SZ_CHAR 1
|
|
|
|
#define SZ_SHORT 2
|
|
|
|
#define SZ_INT 4
|
|
|
|
#define SZ_LONG 4
|
|
|
|
#define SZ_IND 8
|
|
|
|
#define SZ_FLOAT 4
|
|
|
|
#define SZ_VLONG 8
|
|
|
|
#define SZ_DOUBLE 8
|
|
|
|
#define FNX 100
|
|
|
|
|
|
|
|
typedef struct Adr Adr;
|
|
|
|
typedef struct Prog Prog;
|
|
|
|
typedef struct Case Case;
|
|
|
|
typedef struct C1 C1;
|
|
|
|
typedef struct Var Var;
|
|
|
|
typedef struct Reg Reg;
|
|
|
|
typedef struct Rgn Rgn;
|
|
|
|
typedef struct Renv Renv;
|
|
|
|
|
|
|
|
EXTERN struct
|
|
|
|
{
|
|
|
|
Node* regtree;
|
|
|
|
Node* basetree;
|
|
|
|
short scale;
|
|
|
|
short reg;
|
|
|
|
short ptr;
|
|
|
|
} idx;
|
|
|
|
|
|
|
|
struct Adr
|
|
|
|
{
|
|
|
|
vlong offset;
|
|
|
|
double dval;
|
|
|
|
char sval[NSNAME];
|
|
|
|
|
|
|
|
Sym* sym;
|
|
|
|
uchar type;
|
|
|
|
uchar index;
|
|
|
|
uchar etype;
|
|
|
|
uchar scale; /* doubles as width in DATA op */
|
|
|
|
};
|
|
|
|
#define A ((Adr*)0)
|
|
|
|
|
|
|
|
#define INDEXED 9
|
|
|
|
struct Prog
|
|
|
|
{
|
|
|
|
Adr from;
|
|
|
|
Adr to;
|
|
|
|
Prog* link;
|
2008-08-03 18:25:15 -06:00
|
|
|
int32 lineno;
|
2008-06-04 15:37:38 -06:00
|
|
|
short as;
|
|
|
|
};
|
|
|
|
#define P ((Prog*)0)
|
|
|
|
|
|
|
|
struct Case
|
|
|
|
{
|
|
|
|
Case* link;
|
|
|
|
vlong val;
|
2008-08-03 18:25:15 -06:00
|
|
|
int32 label;
|
2008-06-04 15:37:38 -06:00
|
|
|
char def;
|
|
|
|
char isv;
|
|
|
|
};
|
|
|
|
#define C ((Case*)0)
|
|
|
|
|
|
|
|
struct C1
|
|
|
|
{
|
|
|
|
vlong val;
|
2008-08-03 18:25:15 -06:00
|
|
|
int32 label;
|
2008-06-04 15:37:38 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Var
|
|
|
|
{
|
|
|
|
vlong offset;
|
|
|
|
Sym* sym;
|
|
|
|
char name;
|
|
|
|
char etype;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Reg
|
|
|
|
{
|
2008-08-03 18:25:15 -06:00
|
|
|
int32 pc;
|
|
|
|
int32 rpo; /* reverse post ordering */
|
2008-06-04 15:37:38 -06:00
|
|
|
|
|
|
|
Bits set;
|
|
|
|
Bits use1;
|
|
|
|
Bits use2;
|
|
|
|
|
|
|
|
Bits refbehind;
|
|
|
|
Bits refahead;
|
|
|
|
Bits calbehind;
|
|
|
|
Bits calahead;
|
|
|
|
Bits regdiff;
|
|
|
|
Bits act;
|
|
|
|
|
2008-08-03 18:25:15 -06:00
|
|
|
int32 regu;
|
|
|
|
int32 loop; /* could be shorter */
|
2008-06-04 15:37:38 -06:00
|
|
|
|
|
|
|
Reg* log5;
|
2008-08-03 18:25:15 -06:00
|
|
|
int32 active;
|
2008-06-04 15:37:38 -06:00
|
|
|
|
|
|
|
Reg* p1;
|
|
|
|
Reg* p2;
|
|
|
|
Reg* p2link;
|
|
|
|
Reg* s1;
|
|
|
|
Reg* s2;
|
|
|
|
Reg* link;
|
|
|
|
Prog* prog;
|
|
|
|
};
|
|
|
|
#define R ((Reg*)0)
|
|
|
|
|
|
|
|
struct Renv
|
|
|
|
{
|
|
|
|
int safe;
|
|
|
|
Node base;
|
|
|
|
Node* saved;
|
|
|
|
Node* scope;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define NRGN 600
|
|
|
|
struct Rgn
|
|
|
|
{
|
|
|
|
Reg* enter;
|
|
|
|
short cost;
|
|
|
|
short varno;
|
|
|
|
short regno;
|
|
|
|
};
|
|
|
|
|
2008-08-03 18:25:15 -06:00
|
|
|
EXTERN int32 breakpc;
|
|
|
|
EXTERN int32 nbreak;
|
2008-06-04 15:37:38 -06:00
|
|
|
EXTERN Case* cases;
|
|
|
|
EXTERN Node constnode;
|
|
|
|
EXTERN Node fconstnode;
|
|
|
|
EXTERN Node vconstnode;
|
2008-08-03 18:25:15 -06:00
|
|
|
EXTERN int32 continpc;
|
|
|
|
EXTERN int32 curarg;
|
|
|
|
EXTERN int32 cursafe;
|
2008-06-04 15:37:38 -06:00
|
|
|
EXTERN Prog* firstp;
|
|
|
|
EXTERN Prog* lastp;
|
2008-08-03 18:25:15 -06:00
|
|
|
EXTERN int32 maxargsafe;
|
2008-06-04 15:37:38 -06:00
|
|
|
EXTERN int mnstring;
|
2009-07-07 11:07:15 -06:00
|
|
|
EXTERN int retok;
|
2008-06-04 15:37:38 -06:00
|
|
|
EXTERN Node* nodrat;
|
|
|
|
EXTERN Node* nodret;
|
|
|
|
EXTERN Node* nodsafe;
|
2008-08-03 18:25:15 -06:00
|
|
|
EXTERN int32 nrathole;
|
|
|
|
EXTERN int32 nstring;
|
2008-06-04 15:37:38 -06:00
|
|
|
EXTERN Prog* p;
|
2008-08-03 18:25:15 -06:00
|
|
|
EXTERN int32 pc;
|
2008-06-04 15:37:38 -06:00
|
|
|
EXTERN Node lregnode;
|
|
|
|
EXTERN Node qregnode;
|
|
|
|
EXTERN char string[NSNAME];
|
|
|
|
EXTERN Sym* symrathole;
|
|
|
|
EXTERN Node znode;
|
|
|
|
EXTERN Prog zprog;
|
|
|
|
EXTERN int reg[D_NONE];
|
2008-08-03 18:25:15 -06:00
|
|
|
EXTERN int32 exregoffset;
|
|
|
|
EXTERN int32 exfregoffset;
|
2008-06-04 15:37:38 -06:00
|
|
|
EXTERN uchar typechlpv[NTYPE];
|
|
|
|
|
|
|
|
#define BLOAD(r) band(bnot(r->refbehind), r->refahead)
|
|
|
|
#define BSTORE(r) band(bnot(r->calbehind), r->calahead)
|
|
|
|
#define LOAD(r) (~r->refbehind.b[z] & r->refahead.b[z])
|
|
|
|
#define STORE(r) (~r->calbehind.b[z] & r->calahead.b[z])
|
|
|
|
|
|
|
|
#define bset(a,n) ((a).b[(n)/32]&(1L<<(n)%32))
|
|
|
|
|
|
|
|
#define CLOAD 5
|
|
|
|
#define CREF 5
|
|
|
|
#define CINF 1000
|
|
|
|
#define LOOP 3
|
|
|
|
|
|
|
|
EXTERN Rgn region[NRGN];
|
|
|
|
EXTERN Rgn* rgp;
|
|
|
|
EXTERN int nregion;
|
|
|
|
EXTERN int nvar;
|
|
|
|
|
|
|
|
EXTERN Bits externs;
|
|
|
|
EXTERN Bits params;
|
|
|
|
EXTERN Bits consts;
|
|
|
|
EXTERN Bits addrs;
|
|
|
|
|
2008-08-03 18:25:15 -06:00
|
|
|
EXTERN int32 regbits;
|
|
|
|
EXTERN int32 exregbits;
|
2008-06-04 15:37:38 -06:00
|
|
|
|
|
|
|
EXTERN int change;
|
|
|
|
EXTERN int suppress;
|
|
|
|
|
|
|
|
EXTERN Reg* firstr;
|
|
|
|
EXTERN Reg* lastr;
|
|
|
|
EXTERN Reg zreg;
|
|
|
|
EXTERN Reg* freer;
|
|
|
|
EXTERN Var var[NVAR];
|
2008-08-03 18:25:15 -06:00
|
|
|
EXTERN int32* idom;
|
2008-06-04 15:37:38 -06:00
|
|
|
EXTERN Reg** rpo2r;
|
2008-08-03 18:25:15 -06:00
|
|
|
EXTERN int32 maxnr;
|
2008-06-04 15:37:38 -06:00
|
|
|
|
|
|
|
extern char* anames[];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sgen.c
|
|
|
|
*/
|
|
|
|
void codgen(Node*, Node*);
|
|
|
|
void gen(Node*);
|
|
|
|
void noretval(int);
|
|
|
|
void usedset(Node*, int);
|
|
|
|
void xcom(Node*);
|
|
|
|
void indx(Node*);
|
|
|
|
int bcomplex(Node*, Node*);
|
2009-07-07 11:07:15 -06:00
|
|
|
Prog* gtext(Sym*, int32);
|
|
|
|
vlong argsize(void);
|
2008-06-04 15:37:38 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
* cgen.c
|
|
|
|
*/
|
|
|
|
void zeroregm(Node*);
|
|
|
|
void cgen(Node*, Node*);
|
|
|
|
void reglcgen(Node*, Node*, Node*);
|
|
|
|
void lcgen(Node*, Node*);
|
|
|
|
void bcgen(Node*, int);
|
|
|
|
void boolgen(Node*, int, Node*);
|
2008-08-03 18:25:15 -06:00
|
|
|
void sugen(Node*, Node*, int32);
|
2008-06-04 15:37:38 -06:00
|
|
|
int needreg(Node*, int);
|
|
|
|
int hardconst(Node*);
|
|
|
|
int immconst(Node*);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* txt.c
|
|
|
|
*/
|
|
|
|
void ginit(void);
|
|
|
|
void gclean(void);
|
|
|
|
void nextpc(void);
|
|
|
|
void gargs(Node*, Node*, Node*);
|
|
|
|
void garg1(Node*, Node*, Node*, int, Node**);
|
2008-08-03 18:25:15 -06:00
|
|
|
Node* nodconst(int32);
|
2008-06-04 15:37:38 -06:00
|
|
|
Node* nodfconst(double);
|
|
|
|
Node* nodgconst(vlong, Type*);
|
|
|
|
int nodreg(Node*, Node*, int);
|
|
|
|
int isreg(Node*, int);
|
|
|
|
void regret(Node*, Node*);
|
|
|
|
void regalloc(Node*, Node*, Node*);
|
|
|
|
void regfree(Node*);
|
|
|
|
void regialloc(Node*, Node*, Node*);
|
|
|
|
void regsalloc(Node*, Node*);
|
|
|
|
void regaalloc1(Node*, Node*);
|
|
|
|
void regaalloc(Node*, Node*);
|
|
|
|
void regind(Node*, Node*);
|
|
|
|
void gprep(Node*, Node*);
|
|
|
|
void naddr(Node*, Adr*);
|
|
|
|
void gcmp(int, Node*, vlong);
|
|
|
|
void gmove(Node*, Node*);
|
|
|
|
void gins(int a, Node*, Node*);
|
|
|
|
void gopcode(int, Type*, Node*, Node*);
|
|
|
|
int samaddr(Node*, Node*);
|
|
|
|
void gbranch(int);
|
2008-08-03 18:25:15 -06:00
|
|
|
void patch(Prog*, int32);
|
2008-06-04 15:37:38 -06:00
|
|
|
int sconst(Node*);
|
|
|
|
void gpseudo(int, Sym*, Node*);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* swt.c
|
|
|
|
*/
|
|
|
|
int swcmp(const void*, const void*);
|
|
|
|
void doswit(Node*);
|
2008-08-03 18:25:15 -06:00
|
|
|
void swit1(C1*, int, int32, Node*);
|
2011-10-26 16:27:59 -06:00
|
|
|
void newcase(void);
|
2008-06-04 15:37:38 -06:00
|
|
|
void bitload(Node*, Node*, Node*, Node*, Node*);
|
|
|
|
void bitstore(Node*, Node*, Node*, Node*, Node*);
|
2008-08-03 18:25:15 -06:00
|
|
|
int32 outstring(char*, int32);
|
2008-06-04 15:37:38 -06:00
|
|
|
void nullwarn(Node*, Node*);
|
2008-08-03 18:25:15 -06:00
|
|
|
void sextern(Sym*, Node*, int32, int32);
|
|
|
|
void gextern(Sym*, Node*, int32, int32);
|
2008-06-04 15:37:38 -06:00
|
|
|
void outcode(void);
|
|
|
|
void ieeedtod(Ieee*, double);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* list
|
|
|
|
*/
|
|
|
|
void listinit(void);
|
|
|
|
int Pconv(Fmt*);
|
|
|
|
int Aconv(Fmt*);
|
|
|
|
int Dconv(Fmt*);
|
|
|
|
int Sconv(Fmt*);
|
|
|
|
int Rconv(Fmt*);
|
|
|
|
int Xconv(Fmt*);
|
|
|
|
int Bconv(Fmt*);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* reg.c
|
|
|
|
*/
|
|
|
|
Reg* rega(void);
|
|
|
|
int rcmp(const void*, const void*);
|
|
|
|
void regopt(Prog*);
|
|
|
|
void addmove(Reg*, int, int, int);
|
|
|
|
Bits mkvar(Reg*, Adr*);
|
|
|
|
void prop(Reg*, Bits, Bits);
|
2008-08-03 18:25:15 -06:00
|
|
|
void loopit(Reg*, int32);
|
2008-06-04 15:37:38 -06:00
|
|
|
void synch(Reg*, Bits);
|
2008-08-03 18:25:15 -06:00
|
|
|
uint32 allreg(uint32, Rgn*);
|
2008-06-04 15:37:38 -06:00
|
|
|
void paint1(Reg*, int);
|
2008-08-03 18:25:15 -06:00
|
|
|
uint32 paint2(Reg*, int);
|
|
|
|
void paint3(Reg*, int, int32, int);
|
2008-06-04 15:37:38 -06:00
|
|
|
void addreg(Adr*, int);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* peep.c
|
|
|
|
*/
|
|
|
|
void peep(void);
|
|
|
|
void excise(Reg*);
|
|
|
|
Reg* uniqp(Reg*);
|
|
|
|
Reg* uniqs(Reg*);
|
|
|
|
int regtyp(Adr*);
|
|
|
|
int anyvar(Adr*);
|
|
|
|
int subprop(Reg*);
|
|
|
|
int copyprop(Reg*);
|
|
|
|
int copy1(Adr*, Adr*, Reg*, int);
|
|
|
|
int copyu(Prog*, Adr*, Adr*);
|
|
|
|
|
|
|
|
int copyas(Adr*, Adr*);
|
|
|
|
int copyau(Adr*, Adr*);
|
|
|
|
int copysub(Adr*, Adr*, Adr*, int);
|
|
|
|
int copysub1(Prog*, Adr*, Adr*, int);
|
|
|
|
|
2008-08-03 18:25:15 -06:00
|
|
|
int32 RtoB(int);
|
|
|
|
int32 FtoB(int);
|
|
|
|
int BtoR(int32);
|
|
|
|
int BtoF(int32);
|
2008-06-04 15:37:38 -06:00
|
|
|
|
|
|
|
#define D_HI D_NONE
|
|
|
|
#define D_LO D_NONE
|
|
|
|
|
|
|
|
#define isregtype(t) ((t)>= D_AX && (t)<=D_R15)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* bound
|
|
|
|
*/
|
|
|
|
void comtarg(void);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* com64
|
|
|
|
*/
|
|
|
|
int cond(int);
|
|
|
|
int com64(Node*);
|
|
|
|
void com64init(void);
|
|
|
|
void bool64(Node*);
|
2008-08-03 18:25:15 -06:00
|
|
|
int32 lo64v(Node*);
|
|
|
|
int32 hi64v(Node*);
|
2008-06-04 15:37:38 -06:00
|
|
|
Node* lo64(Node*);
|
|
|
|
Node* hi64(Node*);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* div/mul
|
|
|
|
*/
|
|
|
|
void sdivgen(Node*, Node*, Node*, Node*);
|
|
|
|
void udivgen(Node*, Node*, Node*, Node*);
|
2008-08-03 18:25:15 -06:00
|
|
|
void sdiv2(int32, int, Node*, Node*);
|
|
|
|
void smod2(int32, int, Node*, Node*);
|
2008-06-04 15:37:38 -06:00
|
|
|
void mulgen(Type*, Node*, Node*);
|
|
|
|
void genmuladd(Node*, Node*, int, Node*);
|
|
|
|
void shiftit(Type*, Node*, Node*);
|
|
|
|
|
|
|
|
#pragma varargck type "A" int
|
|
|
|
#pragma varargck type "B" Bits
|
|
|
|
#pragma varargck type "D" Adr*
|
2011-07-15 09:58:39 -06:00
|
|
|
#pragma varargck type "lD" Adr*
|
2008-06-04 15:37:38 -06:00
|
|
|
#pragma varargck type "P" Prog*
|
|
|
|
#pragma varargck type "R" int
|
|
|
|
#pragma varargck type "S" char*
|
|
|
|
|
|
|
|
#define D_X7 (D_X0+7)
|
|
|
|
|
|
|
|
void fgopcode(int, Node*, Node*, int, int);
|