xref: /inferno-os/utils/ic/gc.h (revision a93f6c888f6d530420fbb54e2f7fa4572cdc5208)
1 #include	"../cc/cc.h"
2 #include	"../ic/i.out.h"
3 
4 /*
5  * zc/riscv
6  * RISC-V 32-bit
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		4
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	Multab	Multab;
23 typedef	struct	Hintab	Hintab;
24 typedef	struct	Var	Var;
25 typedef	struct	Reg	Reg;
26 typedef	struct	Rgn	Rgn;
27 
28 struct	Adr
29 {
30 	long	offset;
31 	double	dval;
32 	char	sval[NSNAME];
33 	Ieee	ieee;
34 
35 	Sym*	sym;
36 	char	type;
37 	char	reg;
38 	char	name;
39 	char	etype;
40 };
41 #define	A	((Adr*)0)
42 
43 #define	INDEXED	9
44 struct	Prog
45 {
46 	Adr	from;
47 	Adr	to;
48 	Prog*	link;
49 	long	lineno;
50 	char	as;
51 	char	reg;
52 };
53 #define	P	((Prog*)0)
54 
55 struct	Case
56 {
57 	Case*	link;
58 	vlong	val;
59 	long	label;
60 	char	def;
61 	char isv;
62 };
63 #define	C	((Case*)0)
64 
65 struct	C1
66 {
67 	vlong	val;
68 	long	label;
69 };
70 
71 struct	Multab
72 {
73 	long	val;
74 	char	code[20];
75 };
76 
77 struct	Hintab
78 {
79 	ushort	val;
80 	char	hint[10];
81 };
82 
83 struct	Var
84 {
85 	long	offset;
86 	Sym*	sym;
87 	char	name;
88 	char	etype;
89 };
90 
91 struct	Reg
92 {
93 	long	pc;
94 	long	rpo;		/* reverse post ordering */
95 
96 	Bits	set;
97 	Bits	use1;
98 	Bits	use2;
99 
100 	Bits	refbehind;
101 	Bits	refahead;
102 	Bits	calbehind;
103 	Bits	calahead;
104 	Bits	regdiff;
105 	Bits	act;
106 
107 	long	regu;
108 	long	loop;		/* could be shorter */
109 
110 	Reg*	log5;
111 	long	active;
112 
113 	Reg*	p1;
114 	Reg*	p2;
115 	Reg*	p2link;
116 	Reg*	s1;
117 	Reg*	s2;
118 	Reg*	link;
119 	Prog*	prog;
120 };
121 #define	R	((Reg*)0)
122 
123 #define	NRGN	600
124 struct	Rgn
125 {
126 	Reg*	enter;
127 	short	cost;
128 	short	varno;
129 	short	regno;
130 };
131 
132 EXTERN	long	breakpc;
133 EXTERN	long	nbreak;
134 EXTERN	Case*	cases;
135 EXTERN	Node	constnode;
136 EXTERN	Node	fconstnode;
137 EXTERN	Node	vconstnode;
138 EXTERN	long	continpc;
139 EXTERN	long	curarg;
140 EXTERN	long	cursafe;
141 EXTERN	Prog*	firstp;
142 EXTERN	Prog*	lastp;
143 EXTERN	long	maxargsafe;
144 EXTERN	int	mnstring;
145 EXTERN	Multab	multab[20];
146 EXTERN	int	hintabsize;
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	regnode;
155 EXTERN	Node	vregnode;
156 EXTERN	char	string[NSNAME];
157 EXTERN	Sym*	symrathole;
158 EXTERN	Node	znode;
159 EXTERN	Prog	zprog;
160 EXTERN	int	reg[NREG+NREG];
161 EXTERN	long	exregoffset;
162 EXTERN	long	exfregoffset;
163 
164 #define	BLOAD(r)	band(bnot(r->refbehind), r->refahead)
165 #define	BSTORE(r)	band(bnot(r->calbehind), r->calahead)
166 #define	LOAD(r)		(~r->refbehind.b[z] & r->refahead.b[z])
167 #define	STORE(r)	(~r->calbehind.b[z] & r->calahead.b[z])
168 
169 #define	bset(a,n)	((a).b[(n)/32]&(1L<<(n)%32))
170 
171 #define	CLOAD	4
172 #define	CREF	5
173 #define	CINF	1000
174 #define	LOOP	3
175 
176 EXTERN	Rgn	region[NRGN];
177 EXTERN	Rgn*	rgp;
178 EXTERN	int	nregion;
179 EXTERN	int	nvar;
180 
181 EXTERN	Bits	externs;
182 EXTERN	Bits	params;
183 EXTERN	Bits	consts;
184 EXTERN	Bits	addrs;
185 
186 EXTERN	long	regbits;
187 EXTERN	long	exregbits;
188 
189 EXTERN	int	change;
190 EXTERN	int	suppress;
191 
192 EXTERN	Reg*	firstr;
193 EXTERN	Reg*	lastr;
194 EXTERN	Reg	zreg;
195 EXTERN	Reg*	freer;
196 EXTERN	Var	var[NVAR];
197 EXTERN	long*	idom;
198 EXTERN	Reg**	rpo2r;
199 EXTERN	long	maxnr;
200 
201 extern	char*	anames[];
202 extern	Hintab	hintab[];
203 
204 /*
205  * sgen.c
206  */
207 void	codgen(Node*, Node*);
208 void	gen(Node*);
209 void	noretval(int);
210 void	xcom(Node*);
211 int	bcomplex(Node*, Node*);
212 void	usedset(Node*, int);
213 
214 /*
215  * cgen.c
216  */
217 void	cgen(Node*, Node*);
218 void	reglcgen(Node*, Node*, Node*);
219 void	lcgen(Node*, Node*);
220 void	bcgen(Node*, int);
221 void	boolgen(Node*, int, Node*);
222 void	sugen(Node*, Node*, long);
223 void	layout(Node*, Node*, int, int, Node*);
224 
225 /*
226  * txt.c
227  */
228 void	ginit(void);
229 void	gclean(void);
230 void	nextpc(void);
231 void	gargs(Node*, Node*, Node*);
232 void	garg1(Node*, Node*, Node*, int, Node**);
233 Node*	nodconst(long);
234 Node*	nod32const(vlong);
235 Node*	nodfconst(double);
236 Node*	nodgconst(vlong, Type*);
237 void	nodreg(Node*, Node*, int);
238 void	regret(Node*, Node*);
239 void	regalloc(Node*, Node*, Node*);
240 void	regfree(Node*);
241 void	regialloc(Node*, Node*, Node*);
242 void	regsalloc(Node*, Node*);
243 void	regaalloc1(Node*, Node*);
244 void	regaalloc(Node*, Node*);
245 void	regind(Node*, Node*);
246 void	gprep(Node*, Node*);
247 void	raddr(Node*, Prog*);
248 void	naddr(Node*, Adr*);
249 void	gmove(Node*, Node*);
250 void	gins(int a, Node*, Node*);
251 void	gopcode(int, Node*, Node*, Node*);
252 int	samaddr(Node*, Node*);
253 void	gbranch(int);
254 void	patch(Prog*, long);
255 int	sconst(Node*);
256 int	sval(long);
257 void	gpseudo(int, Sym*, Node*);
258 
259 /*
260  * swt.c
261  */
262 int	swcmp(void*, void*);
263 void	doswit(Node*);
264 void	swit1(C1*, int, long, Node*);
265 void	swit2(C1*, int, long, Node*, Node*);
266 void	casf(void);
267 void	bitload(Node*, Node*, Node*, Node*, Node*);
268 void	bitstore(Node*, Node*, Node*, Node*, Node*);
269 long	outstring(char*, long);
270 int	mulcon(Node*, Node*);
271 Multab*	mulcon0(long);
272 void	nullwarn(Node*, Node*);
273 void	gextern(Sym*, Node*, long, long);
274 void	outcode(void);
275 void	ieeedtod(Ieee*, double);
276 
277 /*
278  * list
279  */
280 void	listinit(void);
281 int	Pconv(Fmt*);
282 int	Aconv(Fmt*);
283 int	Dconv(Fmt*);
284 int	Sconv(Fmt*);
285 int	Nconv(Fmt*);
286 int	Bconv(Fmt*);
287 
288 /*
289  * reg.c
290  */
291 Reg*	rega(void);
292 int	rcmp(void*, void*);
293 void	regopt(Prog*);
294 void	addmove(Reg*, int, int, int);
295 Bits	mkvar(Adr*, int);
296 void	prop(Reg*, Bits, Bits);
297 void	loopit(Reg*, long);
298 void	synch(Reg*, Bits);
299 ulong	allreg(ulong, Rgn*);
300 void	paint1(Reg*, int);
301 ulong	paint2(Reg*, int);
302 void	paint3(Reg*, int, long, int);
303 void	addreg(Adr*, int);
304 
305 /*
306  * peep.c
307  */
308 void	peep(void);
309 void	excise(Reg*);
310 Reg*	uniqp(Reg*);
311 Reg*	uniqs(Reg*);
312 int	regtyp(Adr*);
313 int	regzer(Adr*);
314 int	anyvar(Adr*);
315 int	subprop(Reg*);
316 int	copyprop(Reg*);
317 int	copy1(Adr*, Adr*, Reg*, int);
318 int	copyu(Prog*, Adr*, Adr*);
319 
320 int	copyas(Adr*, Adr*);
321 int	copyau(Adr*, Adr*);
322 int	copyau1(Prog*, Adr*);
323 int	copysub(Adr*, Adr*, Adr*, int);
324 int	copysub1(Prog*, Adr*, Adr*, int);
325 
326 long	RtoB(int);
327 long	FtoB(int);
328 int	BtoR(long);
329 int	BtoF(long);
330 
331 #pragma	varargck	type	"A"	int
332 #pragma	varargck	type	"B"	Bits
333 #pragma	varargck	type	"D"	Adr*
334 #pragma	varargck	type	"N"	Adr*
335 #pragma	varargck	type	"P"	Prog*
336 #pragma	varargck	type	"S"	char*
337