xref: /inferno-os/utils/6c/gc.h (revision 2b69dba5038ffd0b59cf30a4c44bce549e5097f8)
1 #include	"../cc/cc.h"
2 #include	"../6c/6.out.h"
3 
4 /*
5  * 6c/amd64
6  * Intel 386 with AMD64 extensions
7  */
8 #define	SZ_CHAR		1
9 #define	SZ_SHORT	2
10 #define	SZ_INT		4
11 #define	SZ_LONG		4
12 #define	SZ_IND		8
13 #define	SZ_FLOAT	4
14 #define	SZ_VLONG	8
15 #define	SZ_DOUBLE	8
16 #define	FNX		100
17 
18 typedef	struct	Adr	Adr;
19 typedef	struct	Prog	Prog;
20 typedef	struct	Case	Case;
21 typedef	struct	C1	C1;
22 typedef	struct	Var	Var;
23 typedef	struct	Reg	Reg;
24 typedef	struct	Rgn	Rgn;
25 typedef	struct	Renv	Renv;
26 
27 EXTERN	struct
28 {
29 	Node*	regtree;
30 	Node*	basetree;
31 	short	scale;
32 	short	reg;
33 	short	ptr;
34 } idx;
35 
36 struct	Adr
37 {
38 	vlong	offset;
39 	double	dval;
40 	char	sval[NSNAME];
41 
42 	Sym*	sym;
43 	uchar	type;
44 	uchar	index;
45 	uchar	etype;
46 	uchar	scale;	/* doubles as width in DATA op */
47 };
48 #define	A	((Adr*)0)
49 
50 #define	INDEXED	9
51 struct	Prog
52 {
53 	Adr	from;
54 	Adr	to;
55 	Prog*	link;
56 	long	lineno;
57 	short	as;
58 };
59 #define	P	((Prog*)0)
60 
61 struct	Case
62 {
63 	Case*	link;
64 	vlong	val;
65 	long	label;
66 	char	def;
67 	char	isv;
68 };
69 #define	C	((Case*)0)
70 
71 struct	C1
72 {
73 	vlong	val;
74 	long	label;
75 };
76 
77 struct	Var
78 {
79 	vlong	offset;
80 	Sym*	sym;
81 	char	name;
82 	char	etype;
83 };
84 
85 struct	Reg
86 {
87 	long	pc;
88 	long	rpo;		/* reverse post ordering */
89 
90 	Bits	set;
91 	Bits	use1;
92 	Bits	use2;
93 
94 	Bits	refbehind;
95 	Bits	refahead;
96 	Bits	calbehind;
97 	Bits	calahead;
98 	Bits	regdiff;
99 	Bits	act;
100 
101 	long	regu;
102 	long	loop;		/* could be shorter */
103 
104 	Reg*	log5;
105 	long	active;
106 
107 	Reg*	p1;
108 	Reg*	p2;
109 	Reg*	p2link;
110 	Reg*	s1;
111 	Reg*	s2;
112 	Reg*	link;
113 	Prog*	prog;
114 };
115 #define	R	((Reg*)0)
116 
117 struct	Renv
118 {
119 	int	safe;
120 	Node	base;
121 	Node*	saved;
122 	Node*	scope;
123 };
124 
125 #define	NRGN	600
126 struct	Rgn
127 {
128 	Reg*	enter;
129 	short	cost;
130 	short	varno;
131 	short	regno;
132 };
133 
134 EXTERN	long	breakpc;
135 EXTERN	long	nbreak;
136 EXTERN	Case*	cases;
137 EXTERN	Node	constnode;
138 EXTERN	Node	fconstnode;
139 EXTERN	Node	vconstnode;
140 EXTERN	long	continpc;
141 EXTERN	long	curarg;
142 EXTERN	long	cursafe;
143 EXTERN	Prog*	firstp;
144 EXTERN	Prog*	lastp;
145 EXTERN	long	maxargsafe;
146 EXTERN	int	mnstring;
147 EXTERN	Node*	nodrat;
148 EXTERN	Node*	nodret;
149 EXTERN	Node*	nodsafe;
150 EXTERN	long	nrathole;
151 EXTERN	long	nstring;
152 EXTERN	Prog*	p;
153 EXTERN	long	pc;
154 EXTERN	Node	lregnode;
155 EXTERN	Node	qregnode;
156 EXTERN	char	string[NSNAME];
157 EXTERN	Sym*	symrathole;
158 EXTERN	Node	znode;
159 EXTERN	Prog	zprog;
160 EXTERN	int	reg[D_NONE];
161 EXTERN	long	exregoffset;
162 EXTERN	long	exfregoffset;
163 EXTERN	uchar	typechlpv[NTYPE];
164 
165 #define	BLOAD(r)	band(bnot(r->refbehind), r->refahead)
166 #define	BSTORE(r)	band(bnot(r->calbehind), r->calahead)
167 #define	LOAD(r)		(~r->refbehind.b[z] & r->refahead.b[z])
168 #define	STORE(r)	(~r->calbehind.b[z] & r->calahead.b[z])
169 
170 #define	bset(a,n)	((a).b[(n)/32]&(1L<<(n)%32))
171 
172 #define	CLOAD	5
173 #define	CREF	5
174 #define	CINF	1000
175 #define	LOOP	3
176 
177 EXTERN	Rgn	region[NRGN];
178 EXTERN	Rgn*	rgp;
179 EXTERN	int	nregion;
180 EXTERN	int	nvar;
181 
182 EXTERN	Bits	externs;
183 EXTERN	Bits	params;
184 EXTERN	Bits	consts;
185 EXTERN	Bits	addrs;
186 
187 EXTERN	long	regbits;
188 EXTERN	long	exregbits;
189 
190 EXTERN	int	change;
191 EXTERN	int	suppress;
192 
193 EXTERN	Reg*	firstr;
194 EXTERN	Reg*	lastr;
195 EXTERN	Reg	zreg;
196 EXTERN	Reg*	freer;
197 EXTERN	Var	var[NVAR];
198 EXTERN	long*	idom;
199 EXTERN	Reg**	rpo2r;
200 EXTERN	long	maxnr;
201 
202 extern	char*	anames[];
203 
204 /*
205  * sgen.c
206  */
207 void	codgen(Node*, Node*);
208 void	gen(Node*);
209 void	noretval(int);
210 void	usedset(Node*, int);
211 void	xcom(Node*);
212 void	indx(Node*);
213 int	bcomplex(Node*, Node*);
214 
215 /*
216  * cgen.c
217  */
218 void	zeroregm(Node*);
219 void	cgen(Node*, Node*);
220 void	reglcgen(Node*, Node*, Node*);
221 void	lcgen(Node*, Node*);
222 void	bcgen(Node*, int);
223 void	boolgen(Node*, int, Node*);
224 void	sugen(Node*, Node*, long);
225 int	needreg(Node*, int);
226 int	hardconst(Node*);
227 int	immconst(Node*);
228 
229 /*
230  * cgen64.c
231  */
232 int	vaddr(Node*, int);
233 void	loadpair(Node*, Node*);
234 int	cgen64(Node*, Node*);
235 void	testv(Node*, int);
236 
237 /*
238  * txt.c
239  */
240 void	ginit(void);
241 void	gclean(void);
242 void	nextpc(void);
243 void	gargs(Node*, Node*, Node*);
244 void	garg1(Node*, Node*, Node*, int, Node**);
245 Node*	nodconst(long);
246 Node*	nodfconst(double);
247 Node*	nodgconst(vlong, Type*);
248 int	nodreg(Node*, Node*, int);
249 int	isreg(Node*, int);
250 void	regret(Node*, Node*);
251 void	regalloc(Node*, Node*, Node*);
252 void	regfree(Node*);
253 void	regialloc(Node*, Node*, Node*);
254 void	regsalloc(Node*, Node*);
255 void	regaalloc1(Node*, Node*);
256 void	regaalloc(Node*, Node*);
257 void	regind(Node*, Node*);
258 void	gprep(Node*, Node*);
259 void	naddr(Node*, Adr*);
260 void	gcmp(int, Node*, vlong);
261 void	gmove(Node*, Node*);
262 void	gins(int a, Node*, Node*);
263 void	gopcode(int, Type*, Node*, Node*);
264 int	samaddr(Node*, Node*);
265 void	gbranch(int);
266 void	patch(Prog*, long);
267 int	sconst(Node*);
268 void	gpseudo(int, Sym*, Node*);
269 
270 /*
271  * swt.c
272  */
273 int	swcmp(const void*, const void*);
274 void	doswit(Node*);
275 void	swit1(C1*, int, long, Node*);
276 void	casf(void);
277 void	bitload(Node*, Node*, Node*, Node*, Node*);
278 void	bitstore(Node*, Node*, Node*, Node*, Node*);
279 long	outstring(char*, long);
280 void	nullwarn(Node*, Node*);
281 void	sextern(Sym*, Node*, long, long);
282 void	gextern(Sym*, Node*, long, long);
283 void	outcode(void);
284 void	ieeedtod(Ieee*, double);
285 
286 /*
287  * list
288  */
289 void	listinit(void);
290 int	Pconv(Fmt*);
291 int	Aconv(Fmt*);
292 int	Dconv(Fmt*);
293 int	Sconv(Fmt*);
294 int	Rconv(Fmt*);
295 int	Xconv(Fmt*);
296 int	Bconv(Fmt*);
297 
298 /*
299  * reg.c
300  */
301 Reg*	rega(void);
302 int	rcmp(const void*, const void*);
303 void	regopt(Prog*);
304 void	addmove(Reg*, int, int, int);
305 Bits	mkvar(Reg*, Adr*);
306 void	prop(Reg*, Bits, Bits);
307 void	loopit(Reg*, long);
308 void	synch(Reg*, Bits);
309 ulong	allreg(ulong, Rgn*);
310 void	paint1(Reg*, int);
311 ulong	paint2(Reg*, int);
312 void	paint3(Reg*, int, long, int);
313 void	addreg(Adr*, int);
314 
315 /*
316  * peep.c
317  */
318 void	peep(void);
319 void	excise(Reg*);
320 Reg*	uniqp(Reg*);
321 Reg*	uniqs(Reg*);
322 int	regtyp(Adr*);
323 int	anyvar(Adr*);
324 int	subprop(Reg*);
325 int	copyprop(Reg*);
326 int	copy1(Adr*, Adr*, Reg*, int);
327 int	copyu(Prog*, Adr*, Adr*);
328 
329 int	copyas(Adr*, Adr*);
330 int	copyau(Adr*, Adr*);
331 int	copysub(Adr*, Adr*, Adr*, int);
332 int	copysub1(Prog*, Adr*, Adr*, int);
333 
334 long	RtoB(int);
335 long	FtoB(int);
336 int	BtoR(long);
337 int	BtoF(long);
338 
339 #define	D_HI	D_NONE
340 #define	D_LO	D_NONE
341 
342 #define	isregtype(t)	((t)>= D_AX && (t)<=D_R15)
343 
344 /*
345  * bound
346  */
347 void	comtarg(void);
348 
349 /*
350  * com64
351  */
352 int	cond(int);
353 int	com64(Node*);
354 void	com64init(void);
355 void	bool64(Node*);
356 long	lo64v(Node*);
357 long	hi64v(Node*);
358 Node*	lo64(Node*);
359 Node*	hi64(Node*);
360 
361 /*
362  * div/mul
363  */
364 void	sdivgen(Node*, Node*, Node*, Node*);
365 void	udivgen(Node*, Node*, Node*, Node*);
366 void	sdiv2(long, int, Node*, Node*);
367 void	smod2(long, int, Node*, Node*);
368 void	mulgen(Type*, Node*, Node*);
369 void	genmuladd(Node*, Node*, int, Node*);
370 void	shiftit(Type*, Node*, Node*);
371 
372 #pragma	varargck	type	"A"	int
373 #pragma	varargck	type	"B"	Bits
374 #pragma	varargck	type	"D"	Adr*
375 #pragma	varargck	type	"P"	Prog*
376 #pragma	varargck	type	"R"	int
377 #pragma	varargck	type	"S"	char*
378 
379 #define	D_X7	(D_X0+7)
380 
381 void	fgopcode(int, Node*, Node*, int, int);
382