1 /*
2  * Copyright (c) 2006 - 2010, Nils R. Weller
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  *
27  * SPARC backend
28  */
29 #include "backend.h"
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <stdarg.h>
33 #include <string.h>
34 #include <ctype.h>
35 #include <limits.h>
36 #include "scope.h"
37 #include "decl.h"
38 #include "type.h"
39 #include "decl.h"
40 #include "icode.h"
41 #include "functions.h"
42 #include "control.h"
43 #include "debug.h"
44 #include "token.h"
45 #include "error.h"
46 #include "functions.h"
47 #include "symlist.h"
48 #include "icode.h"
49 #include "stack.h"
50 #include "reg.h"
51 #include "subexpr.h"
52 #include "expr.h"
53 /* #include "x86_emit_gas.h" */
54 #include "inlineasm.h"
55 #include "sparc_emit_as.h"
56 #include "cc1_main.h"
57 #include "features.h"
58 #include "n_libc.h"
59 
60 static FILE		*out;
61 struct emitter_sparc	*emit_sparc;
62 
63 #define N_GPRS 32
64 #define N_FPRS 64
65 
66 struct reg			sparc_gprs[N_GPRS];
67 struct reg			*g_regs;
68 struct reg			*l_regs;
69 struct reg			*o_regs;
70 struct reg			*i_regs;
71 
72 static struct reg		sparc_fprs[N_FPRS];
73 static struct vreg		saved_gprs[N_GPRS];
74 static struct stack_block	*saved_gprs_sb[N_GPRS];
75 
76 /* XXX This SUCKS!! it is actually the save area start...
77  * stack arguments are really at STACK_ARG_START+6*sparc_gprs[0].size
78  * 01/04/08: Renamed
79  */
80 #define ARG_SAVE_AREA_START ( \
81 	16 * sparc_gprs[0].size)
82 
83 static int	callee_save_map[] = {
84 /* 0-3 */   0, 0, 0, 0,
85 /* 4-7 */   0, 0, 0, 0,
86 /* 8-11 */  0, 0, 0, 0,
87 /* 12-15 */ 0, 0, 0, 0,
88 /* 16-19 */ 1, 1, 1, 1, /* 16 - 23 = callee save temp */
89 /* 20-23 */ 1, 1, 1, 1,
90 /* 24-27 */ 0, 0, 0, 0,
91 /* 28-31 */ 0, 0, 1<<8, 0  /* 30 = callee save temp */
92 };
93 
94 static int	floating_callee_save_map[] = {
95 	0, 0, 0, 0,
96 	0, 0, 0, 0,
97 	0, 0, 0, 0,
98 	0, 0, 1, 1,
99 	1, 1, 1, 1,
100 	1, 1, 1, 1,
101 	1, 1, 1, 1,
102 	1, 1, 1, 1
103 };
104 
105 static void
init_regs(void)106 init_regs(void) {
107 	int		i;
108 	char		*p;
109 	static char	fpr_names[1024];
110 	static char	*names[] = {
111 		"g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7",
112 		"o0", "o1", "o2", "o3", "o4", "o5", "o6", "o7",
113 		"l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7",
114 		"i0", "i1", "i2", "i3", "i4", "i5", "i6", "i7",
115 	};
116 
117 	g_regs = &sparc_gprs[0];
118 	o_regs = &sparc_gprs[8];
119 	l_regs = &sparc_gprs[16];
120 	i_regs = &sparc_gprs[24];
121 
122 	for (i = 0; i < N_GPRS; ++i) {
123 		sparc_gprs[i].type = REG_GPR;
124 		sparc_gprs[i].allocatable = 1;
125 		if (backend->abi == ABI_SPARC64) {
126 			sparc_gprs[i].size = 8;
127 		} else {
128 			sparc_gprs[i].size = 4;
129 		}
130 		sparc_gprs[i].name = names[i];
131 	}
132 	p = fpr_names;
133 	for (i = 0; i < N_FPRS; ++i) {
134 		sparc_fprs[i].type = REG_FPR;
135 		sparc_fprs[i].allocatable = 1;
136 		sparc_fprs[i].size = 8;
137 		sparc_fprs[i].name = p;
138 		p += sprintf(p, "f%d", i)+1;
139 	}
140 
141 	/* Some registers with predefined meaning should not be allocated */
142 	/*
143 	 * 07/17/09: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
144 	 * XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX!!!!!!!!!!!!!
145 	 * This should probably use reg_set_dedicated() to avoid errors
146 	 * like free_preg() setting allocatability! The matter should be
147 	 * investigated during the next SPARC debugging session
148 	 */
149 	for (i = 0; i < 8; ++i) {
150 		g_regs[i].allocatable = 0; /* Global registers */
151 	}
152 	o_regs[6].allocatable = 0; /* stack pointer     o6 */
153 	i_regs[6].allocatable = 0; /* frame pointer     i6 */
154 	i_regs[7].allocatable = 0; /* 01/06/08: woah this was missing.. return address */
155 
156 	tmpgpr = &g_regs[1];
157 	tmpgpr->allocatable = 0;
158 
159 	/*
160 	 * tmpgpr2 is only used for 64bit address calculations
161 	 */
162 	if (backend->abi == ABI_SPARC64) {
163 		tmpgpr2 = &l_regs[7];
164 		tmpgpr2->allocatable = 0;
165 	}
166 	tmpfpr = &sparc_fprs[13];
167 	tmpfpr->allocatable = 0;
168 	if (picflag) {
169 		pic_reg = &l_regs[6];
170 		pic_reg->allocatable = 0;
171 	}
172 }
173 
174 
175 static void
do_invalidate(struct reg * r,struct icode_list * il,int save)176 do_invalidate(struct reg *r, struct icode_list *il, int save) {
177 	/* Neither allocatable nor used means dedicated register */
178 	if (!r->allocatable && !r->used) {
179 		return;
180 	}
181 	free_preg(r, il, 1, save);
182 }
183 
184 /*
185  * XXX Hm should distinguish between function calls and other
186  * invalidations
187  * That would e.g. allow us not to save %i0-%i7 when doing a
188  * function call (since a new register window without those
189  * is created)
190  */
191 static void
invalidate_gprs(struct icode_list * il,int saveregs,int for_fcall)192 invalidate_gprs(struct icode_list *il, int saveregs, int for_fcall) {
193 	int	i;
194 
195 	/* Save all gprs except g0-g7 */
196 	/*do_invalidate(&g_regs[1], il, saveregs) */
197 	for (i = 8; i < N_GPRS; ++i) {
198 		if (&sparc_gprs[i] == tmpgpr2
199 			|| &sparc_gprs[i] == pic_reg) {
200 			if (for_fcall && curfunc->pic_initialized) {
201 				/*
202 				 * Careful - we are using a PIC reg
203 				 * which is caller-save, so we have
204 				 * to save it here
205 				 */
206 				free_preg(pic_reg, il, 1, 1);
207 
208 				/* Restore unallocatability */
209 				pic_reg->allocatable = 0;
210 				pic_reg->used = 1;
211 			}
212 			continue;
213 		}
214 		do_invalidate(&sparc_gprs[i], il, saveregs);
215 	}
216 	for (i = 0; i < N_FPRS; ++i) {
217 		/*
218 		 * XXX this belongs into invalidate_fprs() or
219 		 * else rename this to invalidate_regs() ;-)
220 		 */
221 		do_invalidate(&sparc_fprs[i], il, saveregs);
222 	}
223 	(void) floating_callee_save_map;
224 }
225 
226 
227 static void
invalidate_except(struct icode_list * il,int save,int for_fcall,...)228 invalidate_except(struct icode_list *il, int save, int for_fcall,  ...) {
229 	int		i;
230 	struct reg	*except[8];
231 	struct reg	*arg;
232 	va_list		va;
233 
234 	va_start(va, for_fcall);
235 	for (i = 0; (arg = va_arg(va, struct reg *)) != NULL; ++i) {
236 		except[i] = arg;
237 	}
238 	va_end(va);
239 	except[i] = NULL;
240 
241 	for (i = 0; i < N_GPRS; ++i) {
242 		int	j;
243 
244 		for (j = 0; except[j] != NULL; ++j) {
245 			if (&sparc_gprs[i] == except[j]) {
246 				break;
247 			}
248 		}
249 		if (except[j] != NULL) {
250 			continue;
251 		}
252 		do_invalidate(&sparc_gprs[i], il, save);
253 	}
254 }
255 
256 
257 static struct reg *
alloc_gpr(struct function * f,int size,struct icode_list * il,struct reg * dontwipe,int line)258 alloc_gpr(
259 	struct function *f,
260 	int size,
261 	struct icode_list *il,
262 	struct reg *dontwipe,
263 
264 	int line) {
265 
266 	if (backend->abi != ABI_SPARC64
267 		&& size == 8) {
268 		if (backend->multi_gpr_object) {
269 			backend->multi_gpr_object = 0;
270 		} else {
271 			backend->multi_gpr_object = 1;
272 		}
273 	}
274 	return generic_alloc_gpr(f,size,il,dontwipe,sparc_gprs,N_GPRS,
275 		callee_save_map, line);
276 }
277 
278 /*
279  * SPARC floating point register allocator. This is a little tricky
280  * because we have 64 32bit FPRs which can be combined into pairs and
281  * quadruples to form 64bit and 128bit double and long double sets.
282  *
283  * Register numbers have to be aligned, such that double may only
284  * occupy a first register whose number is divisible by 2, and long
285  * double one that is divisible by 4:
286  *
287  * [   f0   ][   f1   ][   f2   ][   f3   ][   f4   ]
288  * |_________|_________|_________|_________|____ float slots
289  * |___________________|___________________|____ double slots
290  * |                                       |
291  * |_______________________________________|____ long double slots
292  *
293  * We just always mark the corresponding slot allocated. This means
294  * that if we're e.g. looking at f3 because we want to allocate a 32bit
295  * float there, we also have to consider whether f0 is allocated to a
296  * 4-fpr long double, in which case f3 is implicitly already in use!
297  * Additionally we also have to look at f2 to check for the same
298  * condition with double!
299  *
300  * XXX hmm this might have been a job for composed_of in struct reg..
301  * but maybe not.
302  */
303 
304 #define GET_DOUBLE_BASE(regno) \
305 	(regno & ~1u)
306 
307 #define GET_LDOUBLE_BASE(regno) \
308 	(regno & ~3u)
309 
310 #define IS_DOUBLE_SLOT(base) \
311 	(sparc_fprs[base].vreg != NULL \
312 		&& sparc_fprs[base].vreg->type->code == TY_DOUBLE)
313 
314 #define IS_LDOUBLE_SLOT(base) \
315 	(sparc_fprs[base].vreg != NULL \
316 		&& sparc_fprs[base].vreg->type->code == TY_LDOUBLE)
317 
318 static struct reg *
alloc_fpr(struct function * f,int size,struct icode_list * il,struct reg * dontwipe)319 alloc_fpr(struct function *f, int size, struct icode_list *il,
320 struct reg *dontwipe) {
321 	int			i;
322 	struct reg	*ret = NULL;
323 	int		limit;
324 	int		stepsize;
325 	int		base;
326 	static int	lastalloc = 0;
327 
328 	if (size == 4) {
329 		/* Only lower 32 FPRs can be used for floats */
330 		limit = 32;
331 		stepsize = 1; /* Can use any fpr */
332 	} else {
333 		limit = 64;
334 		if (size == 8) {
335 			stepsize = 2; /* 2-reg-alignment */;
336 		} else { /* long double */
337 			stepsize = 4; /* 4-reg-alignment */
338 		}
339 	}
340 	(void) f; (void) size; (void) il; (void) dontwipe;
341 
342 	for (i = 0; i < limit; i += stepsize) {
343 #define REGFREE(r) (!r.used && r.allocatable)
344 		if (REGFREE(sparc_fprs[i])) {
345 			/*
346 			 * For double and long double, we actually need
347 			 * multiple free 32bit FPRs
348 			 */
349 			if (stepsize > 1) {
350 				/* Need 2 or 4 regs */
351 				if (!REGFREE(sparc_fprs[i+1])) {
352 					continue;
353 				}
354 				if (stepsize == 4) {
355 					/* Need 4 regs */
356 					if (!REGFREE(sparc_fprs[i+2])
357 						|| !REGFREE(sparc_fprs[i+3])) {
358 						continue;
359 					}
360 				} else {
361 					/*
362 					 * Must be double - check if we are
363 					 * in an allocated long double set
364 				 	 */
365 					base = GET_LDOUBLE_BASE(i);
366 					if (base != i
367 						&& IS_DOUBLE_SLOT(base)) {
368 						if (!REGFREE(sparc_fprs[base])) {
369 							continue;
370 						}
371 					}
372 				}
373 			} else {
374 				/*
375 				 * This is a float - check if we are
376 				 * in an allocated double or long double
377 				 * register set
378 				 */
379 				base = GET_DOUBLE_BASE(i);
380 				if (base != i
381 					&& IS_DOUBLE_SLOT(base)) {
382 					if (!REGFREE(sparc_fprs[base])) {
383 						/* Already taken! */
384 						continue;
385 					}
386 				}
387 				base = GET_LDOUBLE_BASE(i);
388 				if (base != i
389 					&& IS_LDOUBLE_SLOT(base)) {
390 					if (!REGFREE(sparc_fprs[base])) {
391 						/* Already taken! */
392 						continue;
393 					}
394 				}
395 			}
396 
397 			ret = &sparc_fprs[i];
398 			lastalloc = i;
399 			break;
400 		}
401 	}
402 	if (ret == NULL) {
403 		/*
404 		 * We have to free a register that is already in use.
405 		 * In case of double and long double, we need an entire
406 		 * set of 32bit registers. We need to inspect the vreg
407 		 * corresponding to our registers to determine whether
408 		 * we need to save multiple small items or jus a big
409 		 * one.
410 		 *
411 		 * Note that lastalloc may have been aligned for a
412 		 * different type!
413 		 */
414 		lastalloc &= ~(unsigned)(stepsize - 1); /* Align */
415 		lastalloc += stepsize;
416 		lastalloc %= limit;
417 		ret = &sparc_fprs[lastalloc];
418 
419 		if (stepsize == 1) {
420 			if (ret->vreg && ret->vreg->size == 4 && ret->used) {
421 				/* Free a float for new one! */
422 				free_preg(ret, il, 1, 1);
423 			} else {
424 				base = GET_DOUBLE_BASE(lastalloc);
425 				if (IS_DOUBLE_SLOT(base)
426 					&& !REGFREE(sparc_fprs[base])) {
427 					/* Using double slot */
428 					free_preg(&sparc_fprs[base], il, 1, 1);
429 				} else {
430 					/* Using long double slot */
431 					base = GET_LDOUBLE_BASE(lastalloc);
432 					free_preg(&sparc_fprs[base], il, 1, 1);
433 				}
434 			}
435 		} else if (stepsize == 2) {
436 			if (ret->vreg && ret->vreg->size == 8 && ret->used) {
437 				/* Free a double for a new one! */
438 				free_preg(ret, il, 1, 1);
439 			} else {
440 				base = GET_LDOUBLE_BASE(lastalloc);
441 				if (IS_LDOUBLE_SLOT(base)
442 					&& !REGFREE(sparc_fprs[base])) {
443 					free_preg(&sparc_fprs[base], il, 1, 1);
444 				} else {
445 					/*
446 					 * There must be one or two floats
447 					 * occupying our desired double slot
448 					 */
449 					if (!REGFREE(sparc_fprs[lastalloc])) {
450 						free_preg(&sparc_fprs[lastalloc],
451 							il, 1, 1);
452 					}
453 					if (!REGFREE(sparc_fprs[lastalloc+1])) {
454 						free_preg(
455 							&sparc_fprs[lastalloc+1],
456 							il, 1, 1);
457 					}
458 				}
459 			}
460 		} else /* if (stepsize == 4) */ {
461 			if (ret->vreg && ret->vreg->size == 16 && ret->used) {
462 				/* Free a long double for a new one! */
463 				free_preg(ret, il, 1, 1);
464 			} else {
465 				/*
466 				 * There may be up to 2 doubles and or up to
467 				 * 4 floats occupying our slot
468 				 */
469 				for (i = 0; i < 4; ++i) {
470 					if (!REGFREE(sparc_fprs[lastalloc+i])) {
471 						free_preg(&sparc_fprs[
472 							lastalloc+i], il, 1, 1);
473 					}
474 				}
475 			}
476 		}
477 	}
478 	ret->used = 1;
479 	return ret;
480 }
481 
482 static int
init(FILE * fd,struct scope * s)483 init(FILE *fd, struct scope *s) {
484 	out = fd;
485 
486 	init_regs();
487 
488 	if (asmflag && strcmp(asmname, "as") != 0) {
489 		(void) fprintf(stderr, "Unknown SPARC assembler `%s'\n",
490 			asmflag);
491 		exit(EXIT_FAILURE);
492 	}
493 	emit = &sparc_emit_as;
494 	emit_sparc = &sparc_emit_sparc_as;
495 
496 	backend->emit = emit;
497 	return emit->init(out, s);
498 }
499 
500 static int
get_ptr_size(void)501 get_ptr_size(void) {
502 	if (backend->abi != ABI_SPARC64) {
503 		return 4;
504 	} else {
505 		return 8;
506 	}
507 }
508 
509 static struct type *
get_size_t(void)510 get_size_t(void) {
511 	if (backend->abi != ABI_SPARC64) {
512 		return make_basic_type(TY_UINT);
513 	} else {
514 		return make_basic_type(TY_ULONG);
515 	}
516 }
517 
518 static struct type *
get_uintptr_t(void)519 get_uintptr_t(void) {
520 	return make_basic_type(TY_ULONG);
521 }
522 
523 static struct type *
get_wchar_t(void)524 get_wchar_t(void) {
525 	return make_basic_type(TY_INT);
526 }
527 
528 
529 
530 static size_t
get_sizeof_basic(int type)531 get_sizeof_basic(int type) {
532 	switch (type) {
533 	case TY_ENUM:
534 		return 4; /* XXX */
535 
536 	case TY_INT:
537 	case TY_UINT:
538 	case TY_LONG:
539 	case TY_ULONG:
540 		if (backend->abi == ABI_SPARC64) {
541 			if (IS_LONG(type)) {
542 				return 8;
543 			}
544 		}
545 		return 4;
546 
547 	case TY_LLONG:
548 	case TY_ULLONG:
549 		return 8;
550 
551 	case TY_CHAR:
552 	case TY_UCHAR:
553 	case TY_SCHAR:
554 	case TY_BOOL:
555 		return 1;
556 
557 	case TY_SHORT:
558 	case TY_USHORT:
559 		return 2;
560 
561 	case TY_FLOAT:
562 		return 4;
563 
564 	case TY_DOUBLE:
565 		return 8;
566 	case TY_LDOUBLE:
567 		return 16;
568 	default:
569 	printf("err sizeof cannot cope w/ it, wuz %d\n", type);
570 	abort();
571 		return 1; /* XXX */
572 	}
573 }
574 
575 static void
do_ret(struct function * f,struct icode_instr * ip)576 do_ret(struct function *f, struct icode_instr *ip) {
577 	int	i;
578 
579 	if (f->alloca_head != NULL) {
580 		struct stack_block	*sb;
581 		static struct vreg	rvr;
582 
583 		rvr.stack_addr = f->alloca_regs;
584 		rvr.size = sparc_gprs[0].size;
585 		backend_vreg_map_preg(&rvr, &i_regs[0]);
586 		emit->store(&rvr, &rvr);
587 		backend_vreg_unmap_preg(&i_regs[0]);
588 		if (ip && ip->src_vreg && ip->src_vreg->is_multi_reg_obj) {
589 			rvr.stack_addr = f->alloca_regs->next;
590 			backend_vreg_map_preg(&rvr, &i_regs[1]);
591 			emit->store(&rvr, &rvr);
592 			backend_vreg_unmap_preg(&i_regs[1]);
593 		}
594 
595 		for (sb = f->alloca_head; sb != NULL; sb = sb->next) {
596 			emit->dealloca(sb, NULL);
597 		}
598 
599 		rvr.stack_addr = f->alloca_regs;
600 		backend_vreg_map_preg(&rvr, &i_regs[0]);
601 		emit->load(&i_regs[0], &rvr);
602 		backend_vreg_unmap_preg(&i_regs[0]);
603 		if (ip && ip->src_vreg && ip->src_vreg->is_multi_reg_obj) {
604 			rvr.stack_addr = f->alloca_regs->next;
605 			backend_vreg_map_preg(&rvr, &i_regs[1]);
606 			emit->load(&i_regs[1], &rvr);
607 			backend_vreg_unmap_preg(&i_regs[1]);
608 		}
609 	}
610 	if (f->vla_head != NULL) {
611 		struct stack_block	*sb;
612 		static struct vreg	rvr;
613 
614 		rvr.stack_addr = f->alloca_regs;
615 		rvr.size = sparc_gprs[0].size;
616 		backend_vreg_map_preg(&rvr, &i_regs[0]);
617 		emit->store(&rvr, &rvr);
618 		backend_vreg_unmap_preg(&i_regs[0]);
619 		if (ip && ip->src_vreg && ip->src_vreg->is_multi_reg_obj) {
620 			rvr.stack_addr = f->alloca_regs->next;
621 			backend_vreg_map_preg(&rvr, &i_regs[1]);
622 			emit->store(&rvr, &rvr);
623 			backend_vreg_unmap_preg(&i_regs[1]);
624 		}
625 
626 		for (sb = f->vla_head; sb != NULL; sb = sb->next) {
627 			emit->dealloc_vla(sb, NULL);
628 		}
629 
630 		rvr.stack_addr = f->alloca_regs;
631 		backend_vreg_map_preg(&rvr, &i_regs[0]);
632 		emit->load(&i_regs[0], &rvr);
633 		backend_vreg_unmap_preg(&i_regs[0]);
634 		if (ip && ip->src_vreg && ip->src_vreg->is_multi_reg_obj) {
635 			rvr.stack_addr = f->alloca_regs->next;
636 			backend_vreg_map_preg(&rvr, &i_regs[1]);
637 			emit->load(&i_regs[1], &rvr);
638 			backend_vreg_unmap_preg(&i_regs[1]);
639 		}
640 	}
641 
642 	for (i = 8; i < N_GPRS; ++i) {
643 		if (saved_gprs[i].stack_addr != NULL) {
644 			emit->load(&sparc_gprs[i], &saved_gprs[i]);
645 		}
646 	}
647 	emit->freestack(f, NULL);
648 	emit->ret(ip);
649 }
650 
651 /* XXX platform-independent?!?! used by amd64 */
652 void
653 store_preg_to_var(struct decl *d, size_t size, struct reg *r);
654 
655 #define IS_UNION_OR_ARRAY(ty) \
656 	((ty->code == TY_UNION && ty->tlist == NULL) \
657 	 || (ty->tlist && ty->tlist->type == TN_ARRAY_OF))
658 
659 /* Get total size for struct field se, including alignment (except last) */
660 #define TOTAL_BYTES(se) \
661 	(se->next? se->next->dec->offset - se->dec->offset: \
662 	 backend->get_sizeof_type(se->dec->dtype, NULL))
663 
664 
665 #define CUR_SAVE_AREA_OFFSET (ARG_SAVE_AREA_START + \
666 	*slots_used * 8 \
667 )
668 
669 
670 
671 static void
do_map_parameter(struct function * f,int * slots_used,size_t * stack_bytes_used,struct sym_entry * se)672 do_map_parameter(
673 	struct function *f,
674 	int *slots_used,
675 	size_t *stack_bytes_used,
676 	struct sym_entry *se) {
677 
678 	size_t			size;
679 	int			is_func_arg = 1;
680 	int			is_struct = 0;
681 
682 	/*
683 	 * 01/05/07: Heavy changes... Now we think more in terms of ``slots''
684 	 * instead of gprs and fprs, and also assign fp arguments to stack
685 	 * save area slots. Hope this is correct now, needs more testing
686 	 */
687 	(void) f;
688 
689 	size = backend->get_sizeof_type(se->dec->dtype,0);
690 
691 	if ((se->dec->dtype->code == TY_STRUCT
692 		|| se->dec->dtype->code == TY_UNION)
693 		&& se->dec->dtype->tlist == NULL) {
694 		is_struct = 1;
695 	}
696 
697 	if (is_integral_type(se->dec->dtype) || is_struct || se->dec->dtype->tlist) {
698 		if (*slots_used < 6) {
699 			if (is_struct) {
700 				/*
701 				 * Allocate storage for saving the struct -
702 				 * remember the caller only passed a
703 				 * pointer
704 				 */
705 				stack_align(curfunc, 16);
706 
707 				/*
708 				 * 12/29/07: ATTENTION! We use the struct storage
709 				 * area to save the pointer to the source struct
710 				 * itself before copying is carried out! This is
711 				 * to avoid memcpy() for another struct trashing
712 				 * those pointers. So the allocated space must be
713 				 * at least 8 bytes
714 				 */
715 
716 				se->dec->stack_addr =
717 					stack_malloc(curfunc, size >= 8? size: 8);
718 				is_func_arg = 0;
719 				stack_align(curfunc, 16);
720 			} else {
721 				/*
722 				 * The variable can be saved in the
723 				 * caller provided register save area
724 				 * (right-adjusted if below xword size)
725 				 */
726 				se->dec->stack_addr = make_stack_block(0, size);
727 				se->dec->stack_addr->offset = CUR_SAVE_AREA_OFFSET
728 					+ (sparc_gprs[0].size - size);
729 			}
730 			se->dec->stack_addr->from_reg =
731 				&i_regs[*slots_used];
732 		} else {
733 			/* Passed on stack */
734 			if (is_struct) {
735 				se->dec->stack_addr = make_stack_block(
736 					CUR_SAVE_AREA_OFFSET
737 					/* + (sparc_gprs[0].size - size)*/, size);
738 
739 				/* XXX kludge... incompletec means from_reg points to stack block!!!!! */
740 				se->dec->dtype->incomplete = 1;
741 				se->dec->stack_addr->from_reg =
742 					(struct reg *)stack_malloc(curfunc, size);
743 				((struct stack_block *)se->dec->stack_addr->from_reg)->is_func_arg = 0;
744 				stack_align(curfunc, 16);
745 			} else {
746 				se->dec->stack_addr = make_stack_block(
747 					CUR_SAVE_AREA_OFFSET + (sparc_gprs[0].size - size), size);
748 			}
749 			is_func_arg = 1;
750 
751 			*stack_bytes_used += sparc_gprs[0].size;
752 		}
753 		++*slots_used;
754 	} else if (IS_FLOATING(se->dec->dtype->code)) {
755 		if (*slots_used < 16) {
756 			int	regs_needed = 1;
757 			int	right_adjusted = 0;
758 
759 			/*
760 			 * 12/28/07: Align register number to multiple of two or
761 			 * four for double and long double
762 			 */
763 			switch (se->dec->dtype->code) {
764 			case TY_FLOAT:
765 				regs_needed = 1;
766 				right_adjusted = 1;
767 				break;
768 			case TY_DOUBLE:
769 				regs_needed = 2;
770 				break;
771 			case TY_LDOUBLE:
772 				if (*slots_used & 1) {
773 					/* Not 4-reg-aligned! */
774 					++*slots_used;
775 				}
776 				regs_needed = 4;
777 				break;
778 			}
779 
780 			/*
781 			 * Having now aligned the register number for the type, and
782 			 * having determined the number of needed registers, we can
783 			 * reevaluate whether enough regs are available
784 			 */
785 
786 			/*
787 			 * 12/25/07: Changed to stack_malloc from make_stack_block
788 			 * since FP values are not stored in the caller save area!
789 			 */
790 			se->dec->stack_addr = make_stack_block(0, size);
791 			se->dec->stack_addr->offset = CUR_SAVE_AREA_OFFSET;
792 
793 			se->dec->stack_addr->from_reg =
794 				&sparc_fprs[*slots_used * 2 + right_adjusted];
795 
796 			++*slots_used;
797 			if (regs_needed == 4) {
798 				++*slots_used;
799 			}
800 		} else {
801 			int	pad = 0;
802 
803 			/* 01/05/08: Changed this... working now? */
804 			if (se->dec->dtype->code == TY_FLOAT) {
805 				*stack_bytes_used += 4;
806 				pad = 4;
807 			}
808 			se->dec->stack_addr = make_stack_block(
809 				CUR_SAVE_AREA_OFFSET + pad, size);
810 			*stack_bytes_used += size;
811 			++*slots_used;
812 			if (size == 16) {
813 				++*slots_used;
814 			}
815 		}
816 	} else {
817 		unimpl();
818 	}
819 
820 	/*
821 	 * 12/29/07: This used to UNCONDITIONALLY set is_func_arg to 1! This is
822 	 * wrong for structs and unions passed by value. Those require LOCAL
823 	 * frame allocation (i.e. not in parent's register/stack save area), and
824 	 * are then copied using memcpy().
825 	 */
826 	se->dec->stack_addr->is_func_arg = is_func_arg;    /*1; */
827 }
828 
829 static void
map_parameters(struct function * f,struct ty_func * proto)830 map_parameters(struct function *f, struct ty_func *proto) {
831 	int			i;
832 	int			slots_used = 0;
833 	size_t			stack_bytes_used = 0;
834 	struct sym_entry	*se;
835 
836 	if (f->fty->variadic) {
837 		/*
838 		 * Allocate enough storage for all registers. If none
839 		 * of the unprototyped variadic arguments are passed
840 		 * in registers (quite unlikely), then the allocation
841 		 * below is redundant
842 		 */
843 		f->fty->lastarg = alloc_decl();
844 
845 		/* Register save area starts at 24 on 32bit ppc */
846 		/* XXX is 24 for 32bit and 48 for 64bit right?? */
847 #define REG_SAVE_AREA_OFFSET (6 * sparc_gprs[0].size)
848 		f->fty->lastarg->stack_addr =
849 			make_stack_block(/*REG_SAVE_AREA_OFFSET*/
850 				ARG_SAVE_AREA_START, 0);
851 		f->fty->lastarg->stack_addr->is_func_arg = 1;
852 	}
853 
854 	se = proto->scope->slist;
855 	if (f->proto->dtype->tlist->next == NULL
856 		&& (f->proto->dtype->code == TY_STRUCT
857 		|| f->proto->dtype->code == TY_UNION)) {
858 		/*
859 		 * Function returns struct/union - accomodate for
860 		 * hidden pointer (passed as first argument)
861 		 */
862 		size_t	ptrsize = backend->get_ptr_size();
863 
864 		f->hidden_pointer = vreg_alloc(NULL, NULL, NULL, NULL);
865 		f->hidden_pointer->size = ptrsize;
866 		f->hidden_pointer->var_backed = alloc_decl();
867 		f->hidden_pointer->var_backed->dtype =
868 			n_xmemdup(f->proto->dtype, sizeof *f->proto->dtype);
869 		f->hidden_pointer->var_backed->dtype->tlist =
870 			alloc_type_node();
871 		f->hidden_pointer->type = f->hidden_pointer->var_backed->dtype;
872 		f->hidden_pointer->var_backed->dtype->tlist->type =
873 			TN_POINTER_TO;
874 
875 		/* Pointer goes to register save area */
876 		f->hidden_pointer->var_backed->stack_addr =
877 			make_stack_block(0, ptrsize);
878 		f->hidden_pointer->var_backed->stack_addr->offset =
879 			ARG_SAVE_AREA_START;
880 		f->hidden_pointer->var_backed->stack_addr->from_reg =
881 			&i_regs[0];
882 		f->hidden_pointer->var_backed->stack_addr->is_func_arg = 1;
883 		++slots_used;
884 	}
885 
886 	/*
887 	 * Allocate stack space for those arguments that were
888 	 * passed in registers, set addresses to existing
889 	 * space for those that were passed on the stack
890 	 */
891 	for (i = 0; i < proto->nargs; ++i, se = se->next) {
892 		do_map_parameter(f /*&gprs_used, &fprs_used*/  ,
893 			&slots_used,
894 			&stack_bytes_used, se);
895 	}
896 	if (f->fty->variadic) {
897 		if (slots_used >= 6) {
898 			/* Wow, all variadic stuff passed on stack */
899 			;
900 		} else {
901 			f->fty->lastarg->stack_addr->from_reg =
902 				&i_regs[slots_used];
903 		}
904 		f->fty->lastarg->stack_addr->offset +=
905 			slots_used * sparc_gprs[0].size + stack_bytes_used;
906 	}
907 }
908 
909 static void
make_local_variables(struct function * f)910 make_local_variables(struct function *f) {
911 	struct scope	*scope;
912 	int		i;
913 	size_t		size;
914 
915 	for (scope = f->scope; scope != NULL; scope = scope->next) {
916 		struct stack_block	*sb;
917 		struct scope		*tmp;
918 		struct decl		**dec;
919 		size_t			align;
920 
921 		for (tmp = scope; tmp != NULL; tmp = tmp->parent) {
922 			if (tmp == f->scope) {
923 				break;
924 			}
925 		}
926 
927 		if (tmp == NULL) {
928 			/* End of function reached */
929 			break;
930 		}
931 		if (scope->type != SCOPE_CODE) continue;
932 
933 		dec = scope->automatic_decls.data;
934 		for (i = 0; i < scope->automatic_decls.ndecls; ++i) {
935 			if (dec[i]->stack_addr != NULL) { /* XXX sucks */
936 				continue;
937 			} else if (IS_VLA(dec[i]->dtype->flags)) {
938                                 /*
939                                  * 05/22/11: Handle pointers to VLAs properly;
940                                  * We have to create a metadata block to
941                                  * record dimension sizes, but we allocate
942                                  * the pointers themselves on the stack
943                                  *
944                                  *   char (*p)[N];
945                                  *
946                                  * ... "p" on stack, N in metadata block
947                                  */
948                                 if (dec[i]->dtype->tlist->type == TN_POINTER_TO) {
949                                         ;
950                                 } else {
951                                         continue;
952                                 }
953 			}
954 
955 			size = backend->
956 				get_sizeof_decl(dec[i], NULL);
957 
958 			align = align_for_cur_auto_var(dec[i]->dtype, f->total_allocated);
959 			if (align) {
960 				(void)stack_malloc(f, align);
961 			}
962 			sb = stack_malloc(f, size);
963 
964 			sb->nbytes = size;
965 			dec[i]->stack_addr = sb;
966 		}
967 	}
968 }
969 
970 static int
gen_function(struct function * f)971 gen_function(struct function *f) {
972 	unsigned int		mask;
973 	int			i;
974 	int			nsaved;
975 	int			min_bytes_pushed;
976 	int			need_reiteration = 0;
977 	struct ty_func		*proto;
978 	struct icode_instr	*lastret = NULL;
979 	struct stack_block	*sb;
980 	struct sym_entry	*se;
981 	size_t			alloca_bytes = 0;
982 	size_t			vla_bytes = 0;
983 
984 
985 	/* XXX use emit->intro() ??? */
986 #if 0
987 	x_fprintf(out, "\t.section \".text\"\n");
988 #endif
989 	emit->setsection(SECTION_TEXT);
990 
991 	/* 4 = 4 bytes, not bits?! */
992 	x_fprintf(out, "\t.align 4\n");
993 	if (f->proto->dtype->storage != TOK_KEY_STATIC) {
994 		x_fprintf(out, "\t.global %s\n", f->proto->dtype->name);
995 	}
996 
997 	x_fprintf(out, "\t.type %s, #function\n", f->proto->dtype->name);
998 	emit->label(f->proto->dtype->name, 1);
999 
1000 	proto = f->proto->dtype->tlist->tfunc;
1001 
1002 	/* Create space for saving frame pointer */
1003 	f->total_allocated += sparc_gprs[0].size;
1004 
1005 	map_parameters(f, proto);
1006 
1007 	stack_align(f, 16);
1008 	make_local_variables(f);
1009 	stack_align(f, 16);
1010 
1011 	/*
1012 	 * Allocate storage for saving callee-saved registers
1013 	 * (but defer saving them until sp has been updated)
1014 	 */
1015 	nsaved = 0;
1016 	for (mask = 1, i = 0; i < N_GPRS; ++i, mask <<= 1) {
1017 		if (f->callee_save_used & mask) {
1018 			if (saved_gprs_sb[i] == NULL) {
1019 				saved_gprs_sb[i] =
1020 					make_stack_block(0, sparc_gprs[0].size);
1021 
1022 				/*
1023 				 * The frame pointer cannot be used yet
1024 				 * because it needs to be saved as well
1025 				 */
1026 			}
1027 			f->total_allocated += sparc_gprs[0].size;
1028 			saved_gprs[i].stack_addr = saved_gprs_sb[i];
1029 			saved_gprs[i].size = sparc_gprs[0].size;
1030 			saved_gprs[i].stack_addr->offset =
1031 				f->total_allocated;
1032 			++nsaved;
1033 		} else {
1034 			saved_gprs[i].stack_addr = NULL;
1035 		}
1036 	}
1037 	f->callee_save_offset = f->total_allocated;
1038 
1039 	/*
1040 	 * Allocate storage for temporarily saving GPRs. Offsets need to
1041 	 * be patched once more when the size of the entire stack frame
1042 	 * is known :-(
1043 	 */
1044 	for (sb = f->regs_head; sb != NULL; sb = sb->next) {
1045 		stack_align(f, sb->nbytes);
1046 		f->total_allocated += sb->nbytes;
1047 		sb->offset = f->total_allocated;
1048 	}
1049 	/*
1050 	 * Allocate storage for saving alloca() pointers, and initialize
1051 	 * it to zero (must be patched like temporary register storage)
1052 	 */
1053 	stack_align(f, sparc_gprs[0].size);
1054 	for (sb = f->alloca_head; sb != NULL; sb = sb->next) {
1055 		f->total_allocated += sb->nbytes;
1056 		alloca_bytes += sb->nbytes;
1057 		sb->offset = f->total_allocated;
1058 	}
1059 
1060 	/*
1061 	 * Allocate storage for saving VLA data, and initialize
1062 	 * it to zero (must be patched like temporary register storage)
1063 	 */
1064 	for (sb = f->vla_head; sb != NULL; sb = sb->next) {
1065 		f->total_allocated += sb->nbytes;
1066 		vla_bytes += sb->nbytes;
1067 		sb->offset = f->total_allocated;
1068 	}
1069 	if (f->alloca_head != NULL || f->vla_head != NULL) {
1070 		/*
1071 		 * Get stack for saving return value registers before
1072 		 * performing free() on alloca()ted blocks
1073 		 */
1074 		f->alloca_regs = make_stack_block(0, sparc_gprs[0].size);
1075 		f->total_allocated += sparc_gprs[0].size;
1076 		f->alloca_regs->offset = f->total_allocated;
1077 		f->alloca_regs->next = make_stack_block(0, sparc_gprs[0].size);
1078 		f->total_allocated += sparc_gprs[0].size;
1079 		f->alloca_regs->next->offset = f->total_allocated;
1080 	}
1081 
1082 	stack_align(f, 16);
1083 
1084 	/*
1085 	 * The SPARC ABI requires the caller to allocate storage
1086 	 * for saving argument registers and passing stack arguments,
1087 	 * the latter of which is recorded by max_bytes_pushed.
1088 	 * Unfortunately, it is possible that unrequested function
1089 	 * calls must be generated, e.g. to perform software arithmetic
1090 	 * with the __nwcc*() functions.
1091 	 * Therefore, we always allocate a minimum save area to ensure
1092 	 * that no surprises with hidden calls can happen.
1093 	 * XXX obviously it may be beneficial to omit this if no
1094 	 * calls actually happen
1095 	 */
1096 	min_bytes_pushed = 6 * sparc_gprs[0].size + 20 * sparc_fprs[0].size; /* reg save area */
1097 
1098 	if (f->max_bytes_pushed < min_bytes_pushed) {
1099 		f->max_bytes_pushed = min_bytes_pushed;
1100 		while (f->max_bytes_pushed % 8) ++f->max_bytes_pushed;
1101 	}
1102 
1103 	f->total_allocated += f->max_bytes_pushed; /* Parameter area */
1104 /*	f->total_allocated += sparc_gprs[1].size; *//* saved sp */
1105 	/* register window save area and hidden parameter  - 16+1 */
1106 	f->total_allocated += 17 * sparc_gprs[0].size;
1107 
1108 	/* 16 byte alignment */
1109 	while (f->total_allocated % 16) ++f->total_allocated;
1110 
1111 	if (f->total_allocated > 0) {
1112 		emit->allocstack(f, f->total_allocated);
1113 	}
1114 
1115 	for (i = 1; i < N_GPRS; ++i) {
1116 		if (saved_gprs[i].stack_addr != NULL) {
1117 			/* Register is callee-save! */
1118 			backend_vreg_map_preg(&saved_gprs[i], &sparc_gprs[i]);
1119 			emit->store(&saved_gprs[i], &saved_gprs[i]);
1120 			backend_vreg_unmap_preg(&sparc_gprs[i]);
1121 			sparc_gprs[i].used = 0;
1122 		}
1123 	}
1124 
1125 	/*
1126 	 * Patch parameter offsets and save corresponding argument
1127 	 * registers to stack, if necessary
1128 	 */
1129 	se = proto->scope->slist;
1130 	for (i = 0; i < proto->nargs; ++i, se = se->next) {
1131 		/*
1132 		 * XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1133 		 * 01/05/08: This comment seems completely bogus since
1134 		 * no patching at all is done on SPARC!!! Only thing
1135 		 * done here is saving arg registers to stack areas
1136 		 * Local variable offsets may be wrong - do those
1137 		 * need patching????
1138 		 *
1139 		 *
1140 		 * There are two cases where the stack address must
1141 		 * be patched:
1142 		 *
1143 		 *      - The argument is passed on the stack
1144 		 *      - The argument is passed in a register, but
1145 		 *        backed by a register save area slot
1146 		 *
1147 		 * In either case, we need an offset into the stack
1148 		 * frame of the caller, which can only now be
1149 		 * computed.
1150 		 *
1151 		 * (this also applies to hidden_pointer for struct
1152 		 * returns, which is passed in gpr3.)
1153 		 *
1154 		 * fp . . . . . . [frame start] [arguments ...]
1155 		 * ^ lowest address           highest address ^
1156 		 * So the offset (with fp) is the size of the entire
1157 		 * stack frame plus the offset in the stack arg area
1158 		 */
1159 		if (se->dec->stack_addr->from_reg != NULL
1160 			&& !se->dec->dtype->incomplete) { /* XXX kludge */
1161 			/* Write argument register contents to stack */
1162 			if ((se->dec->dtype->code == TY_STRUCT
1163 				|| se->dec->dtype->code == TY_UNION)
1164 				&& se->dec->dtype->tlist == NULL) {
1165 
1166 				/*
1167 				 * 12/29/07: Save register with address
1168 				 * of struct/union passed by value. We
1169 				 * can't memcpy() the struct/union yet
1170 				 * because that could trash other
1171 				 * argument registers!
1172 				 */
1173 
1174 				save_struct_ptr(se->dec);
1175 				need_reiteration = 1;
1176 			} else {
1177 				/*
1178 				 * Passed in register, but backed by
1179 				 * register save area
1180 				 */
1181 				store_preg_to_var(se->dec,
1182 					se->dec->stack_addr->nbytes,
1183 					se->dec->stack_addr->from_reg);
1184 			}
1185 		} else {
1186 			if ((se->dec->dtype->code == TY_STRUCT
1187 				|| se->dec->dtype->code == TY_UNION)
1188 				&& se->dec->dtype->tlist == NULL) {
1189 				need_reiteration = 1;
1190 			}
1191 		}
1192 	}
1193 
1194 	if (need_reiteration) {
1195 		/*
1196 		 * 12/29/07; Now memcpy() structs/unions passed by value
1197 		 * the struct pointers are currently stored at the stack
1198 		 * location where the struct will be saved! (A minimum
1199 		 * size of 8 bytes is guaranteed)
1200 		 */
1201 		se = proto->scope->slist;
1202 		for (i = 0; i < proto->nargs; ++i, se = se->next) {
1203 			if ( 1 /*se->dec->stack_addr->from_reg != NULL */) {
1204 				if ((se->dec->dtype->code == TY_STRUCT
1205 					|| se->dec->dtype->code == TY_UNION)
1206 					&& se->dec->dtype->tlist == NULL) {
1207 					if (se->dec->dtype->incomplete) {  /* XXX */
1208 						/*
1209 						 * 01/12/08: The struct pointer was
1210 						 * passed on the stack. We can use
1211 						 * %i0 as a temporary register because
1212 						 * all argument registers have already
1213 						 * been saved to the stack at this
1214 						 * point
1215 						 *
1216 						 * XXX We abuse from_reg to hold the
1217 						 * stack address to which the struct
1218 						 * will be copied, so it must be saved
1219 						 * first
1220 						 */
1221 						struct stack_block	*target_address;
1222 
1223 						target_address = (struct stack_block *)
1224 							se->dec->stack_addr->from_reg;
1225 						se->dec->stack_addr->from_reg = &i_regs[0];
1226 						target_address->from_reg =
1227 							se->dec->stack_addr->from_reg;
1228 
1229 						reload_struct_ptr(se->dec);
1230 
1231 						/*
1232 						 * Now we have the struct pointer in the
1233 						 * register and can replace the pointer
1234 						 * save area slot with the target address
1235 					 	 */
1236 						se->dec->stack_addr = target_address;
1237 
1238 						/* Restore kludged flag */
1239 						se->dec->dtype->incomplete = 0;
1240 					} else {
1241 						reload_struct_ptr(se->dec);
1242 					}
1243 					copy_struct_regstack(se->dec);
1244 				}
1245 			}
1246 		}
1247 	}
1248 
1249 	if (f->hidden_pointer) {
1250 		struct decl	*d = f->hidden_pointer->var_backed;
1251 
1252 #if 0
1253 		d->stack_addr->offset = f->total_allocated + d->stack_addr->offset;
1254 #endif
1255 		store_preg_to_var(d,
1256 			d->stack_addr->nbytes,
1257 			d->stack_addr->from_reg);
1258 	}
1259 	if (f->fty->variadic) {
1260 		size_t	saved_offset;
1261 
1262 		if (f->fty->lastarg->stack_addr->from_reg == NULL) {
1263 			/* Entirely on stack */
1264 #if 0
1265 			f->fty->lastarg->stack_addr->offset =
1266 				f->total_allocated +
1267 				f->fty->lastarg->stack_addr->offset;
1268 			unimpl();
1269 #endif
1270 			/*
1271 			 * 12/25/07: This had the offset patchery above
1272 			 * commented out, with just the unimpl in place.
1273 			 * It turns out that there is apparently nothing
1274 			 * to be done here
1275 			 */
1276 		} else {
1277 			struct reg	*r =
1278 				f->fty->lastarg->stack_addr->from_reg;
1279 
1280 			saved_offset = f->fty->lastarg->stack_addr->offset;
1281 			for (; r != &i_regs[6]; ++r) {
1282 				store_preg_to_var(f->fty->lastarg,
1283 					sparc_gprs[0].size, r);
1284 				f->fty->lastarg->stack_addr->offset +=
1285 					sparc_gprs[0].size;
1286 			}
1287 			f->fty->lastarg->stack_addr->offset = saved_offset;
1288 		}
1289 	}
1290 	if (f->alloca_head != NULL) {
1291 		emit->zerostack(f->alloca_tail, alloca_bytes);
1292 	}
1293 	if (f->vla_head != NULL) {
1294 		emit->zerostack(f->vla_tail, vla_bytes);
1295 	}
1296 
1297 	if (xlate_icode(f, f->icode, &lastret) != 0) {
1298 		return -1;
1299 	}
1300 	emit->outro(f);
1301 
1302 	return 0;
1303 }
1304 
1305 #if XLATE_IMMEDIATELY
1306 
1307 static int
gen_prepare_output(void)1308 gen_prepare_output(void) {
1309 	if (picflag) {
1310 		emit->pic_support();
1311 	}
1312 	return 0;
1313 }
1314 
1315 static int
gen_finish_output(void)1316 gen_finish_output(void) {
1317 	emit->static_init_vars(static_init_vars);
1318 	emit->static_init_thread_vars(static_init_thread_vars);
1319 	emit->static_uninit_vars(static_uninit_vars);
1320 	emit->static_uninit_thread_vars(static_uninit_thread_vars);
1321 
1322 	emit->global_extern_decls(global_scope.extern_decls.data,
1323 		global_scope.extern_decls.ndecls);
1324 	if (emit->extern_decls) {
1325 		emit->extern_decls();
1326 	}
1327 
1328 	if (emit->support_buffers) {
1329 		emit->support_buffers();
1330 	}
1331 	x_fflush(out);
1332 	return 0;
1333 }
1334 
1335 #else
1336 
1337 static int
gen_program(void)1338 gen_program(void) {
1339 	struct function		*func;
1340 
1341 	if (picflag) {
1342 		emit->pic_support();
1343 	}
1344 
1345 #if 0
1346 	emit->global_decls();
1347 #endif
1348 	emit->global_extern_decls(global_scope.extern_decls.data,
1349 			global_scope.extern_decls.ndecls);
1350 	emit->global_static_decls(global_scope.static_decls.data,
1351 			global_scope.static_decls.ndecls);
1352 #if 0
1353 	emit->static_decls();
1354 #endif
1355 	emit->static_init_vars(static_init_vars);
1356 	emit->static_uninit_vars(static_uninit_vars);
1357 	emit->static_init_thread_vars(static_init_thread_vars);
1358 	emit->static_uninit_thread_vars(static_uninit_thread_vars);
1359 
1360 	emit->struct_inits(init_list_head);
1361 
1362 	emit->empty();
1363 	emit->strings(str_const); /* XXX bad */
1364 	emit->fp_constants(float_const);
1365 
1366 	for (func = funclist; func != NULL; func = func->next) {
1367 		curfunc = func;
1368 		if (gen_function(func) != 0) {
1369 			return -1;
1370 		}
1371 		emit->empty();
1372 		emit->empty();
1373 	}
1374 	x_fflush(out);
1375 
1376 	return 0;
1377 }
1378 
1379 #endif
1380 
1381 static void
pass_arg_stack(struct vreg * vr,size_t bytes_left,int * slots_used,size_t * stack_bytes_used,struct icode_list * il)1382 pass_arg_stack(
1383 	struct vreg *vr,
1384 	size_t bytes_left,
1385 	int *slots_used,
1386 	size_t *stack_bytes_used,
1387 	struct icode_list *il) {
1388 
1389 	struct vreg	*dest;
1390 
1391 	/*
1392 	 * All types are passed as double words
1393 	 * (right-justified)
1394 	 */
1395 	size_t	size;
1396 	int	is_struct = 0;
1397 
1398 
1399 	(void) bytes_left;
1400 
1401 	size = backend->get_sizeof_type(vr->type, NULL);
1402 	if (vr->type->tlist
1403 		&& vr->type->tlist->type ==
1404 		TN_ARRAY_OF) {
1405 		size = /*4*/sparc_gprs[0].size;
1406 	} else if (vr->type->tlist == NULL
1407 		&& (vr->type->code == TY_STRUCT
1408 		|| vr->type->code == TY_UNION)) {
1409 		/* Struct/union-by-value is passed by address */
1410 		size = sparc_gprs[0].size;
1411 		is_struct = 1;
1412 	}
1413 
1414 	if (/*4*/sparc_gprs[0].size - size > 0) {
1415 		/* Need to right-adjust */
1416 		*stack_bytes_used += /*4*/sparc_gprs[0].size - size;
1417 	}
1418 
1419 	dest = vreg_alloc(NULL, NULL, NULL, NULL);
1420 	dest->type = vr->type;
1421 	dest->size = vr->size;
1422 
1423 	{
1424 		int pad;
1425 		if (size < sparc_gprs[0].size) {
1426 			pad = sparc_gprs[0].size - size;
1427 		} else {
1428 			pad = 0;
1429 		}
1430 		dest->stack_addr = make_stack_block(CUR_SAVE_AREA_OFFSET + pad, dest->size);
1431 	}
1432 
1433 
1434 	dest->stack_addr->use_frame_pointer = 0;
1435 	*stack_bytes_used += size;
1436 	++*slots_used;
1437 	if (dest->size > 8) {
1438 		++*slots_used;
1439 	}
1440 
1441 	/*
1442 	 * XXXXXXXXXXXXXXXXXXXXXXXXXXXXX 12/12/07
1443 	 * Should we really use tmpgr here????
1444 	 */
1445 {
1446 	struct reg	*temp;
1447 	int		i;
1448 
1449 	if (!is_floating_type(vr->type)) {
1450 		for (i = 0; i < 6; ++i) {
1451 			reg_set_unallocatable(&o_regs[i]);
1452 		}
1453 		if (is_struct) {
1454 			reg_set_unallocatable(vr->pregs[0]);
1455 		}
1456 
1457 		temp = ALLOC_GPR(curfunc, backend->get_sizeof_type(vr->type, 0), il, NULL);
1458 
1459 		for (i = 0; i < 6; ++i) {
1460 			reg_set_allocatable(&o_regs[i]);
1461 		}
1462 		if (is_struct) {
1463 			reg_set_allocatable(vr->pregs[0]);
1464 		}
1465 	} else {
1466 		for (i = 0; i < 13; ++i) {
1467 			reg_set_unallocatable(&sparc_fprs[i]);
1468 		}
1469 
1470 		temp = backend->alloc_fpr(curfunc, backend->get_sizeof_type(vr->type, 0), il, NULL);
1471 
1472 		for (i = 0; i < 13; ++i) {
1473 			reg_set_allocatable(&sparc_fprs[i]);
1474 		}
1475 	}
1476 
1477 	if (is_struct) {
1478 		vr = n_xmemdup(vr, sizeof *vr);
1479 		vr->type = n_xmemdup(vr->type, sizeof *vr->type);
1480 		append_typelist(vr->type, TN_POINTER_TO, NULL, NULL, 0);
1481 		icode_make_copyreg( /*tmpgpr*/  temp, vr->pregs[0], NULL, NULL, il);
1482 	} else {
1483 		vreg_faultin(  /*tmpgpr*/  temp, NULL, vr, il, 0);
1484 	}
1485 	vreg_map_preg(dest, /* tmpgpr */ temp);
1486 	icode_make_store(curfunc, dest, dest, il);
1487 free_preg(temp, il, 1, 0);
1488 
1489 	/*
1490 	 * We have to ensure that the frontend never permanently uses
1491 	 * tmpgpr
1492 	 */
1493 #if 0
1494 	tmpgpr->vreg = NULL;
1495 	tmpgpr->used = 0;
1496 #endif
1497   }
1498 }
1499 
1500 
1501 void
1502 put_arg_into_reg(
1503 	struct reg *regset,
1504 	int *index, int startat,
1505 	struct vreg *vr,
1506 	struct icode_list *il);
1507 
1508 extern unsigned long	reg_offset; /* XXX mips ... */
1509 
1510 
1511 /*
1512  * This function handles potential fpr register clashes. For example:
1513  *
1514  *    func(expr, expr2);
1515  *
1516  *    - expr2 may have been evaluated and loaded into fpr N/N+1 BEFORE the
1517  * function call is performed
1518  *    - expr is loaded into fpr N+1
1519  *    - expr2 is moved to N+2 where it is expected by func
1520  *
1521  * Here, if expr2 spans two registers, and expr just one (right-adjuted),
1522  * the load for expr will trash part of expr2 if we do not explicitly
1523  * check for this scenario
1524  */
1525 static void
put_sparc_fp_arg_into_reg(struct reg * regset,int * index,int startat,struct vreg * vr,struct icode_list * il)1526 put_sparc_fp_arg_into_reg(
1527 	struct reg *regset,
1528 	int *index, int startat,
1529 	struct vreg *vr,
1530 	struct icode_list *il) {
1531 
1532 	if (vr->type->code == TY_FLOAT) {
1533 		/*
1534 		 * Must be right-adjusted. Is this slot used for a double?
1535 		 */
1536 		if (regset[*index - 1].used) {
1537 			if (!regset[*index - 1].allocatable) {
1538 				puts("BUG?!?!: prev fpr not allocatable");
1539 				abort();
1540 			} else {
1541 				free_preg(&regset[*index - 1], il, 1, 1);
1542 			}
1543 		} else {
1544 			/*
1545 			 * Is this slot the second half of a loaded long
1546 			 * double? (odd-numbered)
1547 			 */
1548 			if (((*index - 1) & 1) == 0
1549 				&& *index >= 3) {
1550 				struct reg	*r = &regset[*index - 3];
1551 
1552 				if (r->vreg
1553 					&& r->vreg->pregs[0]
1554 					&& r->vreg->pregs[0] == r) {
1555 					if (r->vreg->type->code == TY_LDOUBLE) {
1556 						if (!r->allocatable) {
1557 							puts("BUG?!?!? prev fpr not allocatable (long double)");
1558 							abort();
1559 						} else {
1560 							free_preg(r, il, 1, 1);
1561 						}
1562 					}
1563 				}
1564 			}
1565 		}
1566 	} else if (vr->type->code == TY_DOUBLE) {
1567 		/*
1568 		 * Preceding slot may be long double
1569 		 */
1570 		if ((*index & 1) != 0) {
1571 			struct reg	*r = &regset[*index - 1];
1572 
1573 			if (r->vreg
1574 				&& r->vreg->pregs[0]
1575 				&& r->vreg->pregs[0] == r) {
1576 				if (r->vreg->type->code == TY_LDOUBLE) {
1577 					if (!r->allocatable) {
1578 						puts("BUG?!?!? prev fpr not allocatable (long double)");
1579 						abort();
1580 					} else {
1581 						free_preg(r, il, 1, 1);
1582 					}
1583 				}
1584 			}
1585 		}
1586 	}
1587 	put_arg_into_reg(regset, index, startat, vr, il);
1588 }
1589 
1590 
1591 
1592 
1593 static size_t
1594 sparc_calc_stack_bytes(struct vreg **vrs, int nvrs, int *takes_struct);
1595 
1596 
1597 static struct vreg *
icode_make_fcall(struct fcall_data * fcall,struct vreg ** vrs,int nvrs,struct icode_list * il)1598 icode_make_fcall(struct fcall_data *fcall, struct vreg **vrs, int nvrs,
1599 struct icode_list *il)
1600 {
1601 	unsigned long		allpushed = 0;
1602 	struct vreg		*tmpvr;
1603 	struct vreg		*ret = NULL;
1604 	struct vreg		*vr2;
1605 	struct type		*ty;
1606 	struct icode_instr	*ii;
1607 	struct type_node	*tn;
1608 	struct vreg		*struct_lvalue;
1609 	size_t			stack_bytes_used = 0;
1610 	int			i;
1611 	int			takes_struct = 0;
1612 	int			need_dap = 0;
1613 	int			struct_return = 0;
1614 	int			gprs_used = 0;
1615 	int			fprs_used = 0;
1616 	int			slots_used = 0;
1617 	int			is_multi_reg_obj = 0;
1618 	int			ret_is_anon_struct = 0;
1619 	unsigned char		gprs_used_map[8];
1620 
1621         /*
1622 	 * 23/12/08: Record numbers of actually used GPRS to avoid them
1623 	 * from being mistaken for GPRS that were passed as a result of
1624 	 * e.g. an FP argument incrementing the slot counter
1625 	 */
1626 	memset(gprs_used_map, 0, sizeof gprs_used_map);
1627 
1628 	ty = fcall->calltovr->type;
1629 	tmpvr = fcall->calltovr;
1630 
1631 	tn = ty->tlist;
1632 	if (tn->type == TN_POINTER_TO) {
1633 		/* Called thru function pointer */
1634 		tn = tn->next;
1635 	}
1636 
1637 	struct_lvalue = fcall->lvalue;
1638 
1639 	if ((ty->code == TY_STRUCT
1640 		|| ty->code == TY_UNION)
1641 		&& tn->next == NULL) {
1642 		struct_return = 1;
1643 
1644 		/*
1645 		 * This is a big struct that doesn't fit into
1646 		 * regsiters, so it is necessary to pass a pointer
1647 		 * to some space to store the result into in r4
1648 		 */
1649 		if (struct_lvalue == NULL || fcall->need_anon) {
1650 			struct type_node	*tnsav;
1651 			/*
1652 			 * Result of function is not assigned so we need
1653 			 * to allocate storage for the callee to store
1654 			 * its result into
1655 			 */
1656 
1657 /* XXX ARGH This is bogus on ppc..need to preallocate */
1658 #if 1 /* 06/17/08: Should go! Use rettype instead */
1659 			tnsav = ty->tlist;
1660 			ty->tlist = NULL;
1661 #endif
1662 
1663 			/*
1664 			 * 12/29/07: Allocate anon return struct on stack frame
1665 			 * instead of current temporary argument save area.
1666 			 * Otherwise stuff is getting trashed.
1667 			 */
1668 			struct_lvalue = vreg_stack_alloc(ty, il, /*1*/ 1, NULL);
1669 
1670 #if 1
1671 			ty->tlist = tnsav;
1672 #endif
1673 			/*
1674 			 * 08/05/08: Don't add to allpushed since struct is
1675 			 * created on frame. This bug occurred on AMD64 and
1676 			 * hasn't been verified on SPARC yet
1677 			 */
1678 	/*		allpushed += struct_lvalue->size;*/
1679 			/*while (allpushed % 4*/ /* XXX *//*) ++allpushed;*/
1680 			while (allpushed % sparc_gprs[0].size) ++allpushed;
1681 			ret_is_anon_struct = 1;
1682 		}
1683 
1684 		/* Hidden pointer is passed in first GPR! */
1685 		{
1686 			struct reg 	*r;
1687 			/*ii*/ r = make_addrof_structret(struct_lvalue, il);
1688 			free_preg(&o_regs[0], il, 1, 1);
1689 
1690 			icode_make_copyreg(&o_regs[0], r /*ii->dat*/,
1691 				NULL, NULL, il);
1692 			reg_set_unallocatable(&o_regs[0]);
1693 		}
1694 		gprs_used_map[slots_used] = 1;
1695 		++slots_used;
1696 	}
1697 
1698 	/*
1699 	 * 07/20/08: This wrongly took an implicit return type into account
1700 	 * to determine whether default argument promotions are needed!
1701 	 */
1702 	if (fcall->functype->nargs == -1
1703 		/*|| ty->implicit*/) {
1704 		/* Need default argument promotions */
1705 		need_dap = 1;
1706 	}
1707 
1708 	allpushed += sparc_calc_stack_bytes(vrs, nvrs, &takes_struct);
1709 	allpushed *= 2;
1710 	if (takes_struct) {
1711 		/* memcpy() will be called to pass argument(s) */
1712 		backend->invalidate_gprs(il, 1, INV_FOR_FCALL);
1713 	}
1714 
1715 	if (/*allpushed > 0*/ 1) {
1716 		if ((int)allpushed > curfunc->max_bytes_pushed) {
1717 			curfunc->max_bytes_pushed = allpushed;
1718 		}
1719 	}
1720 
1721 
1722 
1723 	for (i = 0; i < nvrs; ++i) {
1724 		/*
1725 		 * 12/23/08: Don't check type->implicit (which may indicate
1726 		 * implicit int instead of implicit delaration through call)
1727 		 */
1728 		if ((fcall->functype->variadic
1729 			&& i >= fcall->functype->nargs)
1730 			|| /*fcall->calltovr->type->implicit*/ fcall->functype->nargs == -1) {
1731 			need_dap = 1;
1732 		}
1733 
1734 		if (vrs[i]->parent) {
1735 			vr2 = get_parent_struct(vrs[i]);
1736 		} else {
1737 			vr2 = NULL;
1738 		}
1739 		if (is_integral_type(vrs[i]->type)
1740 			|| vrs[i]->type->tlist
1741 			/*|| vrs[i]->from_const*/ ) {
1742 			if (vrs[i]->type->tlist == NULL
1743 				&& (IS_CHAR(vrs[i]->type->code)
1744 					|| IS_SHORT(vrs[i]->type->code))) {
1745 				vrs[i] = backend->
1746 					icode_make_cast(vrs[i],
1747 						make_basic_type(TY_INT), il);
1748 			}
1749 			if (slots_used < 6) {
1750 				gprs_used_map[slots_used] = 1;
1751 				put_arg_into_reg(o_regs,
1752 					/* &gprs_used*/ &slots_used, 0, vrs[i], il);
1753 			} else {
1754 				pass_arg_stack(vrs[i], 0,
1755 					&slots_used, &stack_bytes_used, il);
1756 			}
1757 		} else if (vrs[i]->type->code == TY_STRUCT
1758 			|| vrs[i]->type->code == TY_UNION) {
1759 			int	regidx = -1;
1760 
1761 			/*
1762 			 * Structs/unions passed by value are really
1763 			 * passed by address in the SPARC ABI
1764 			 */
1765 			struct reg	*addrreg;
1766 
1767 			/*ii =*/addrreg = icode_make_addrof(NULL, vrs[i], il);
1768 /*			append_icode_list(il, ii);*/
1769 			vreg_map_preg(vrs[i], addrreg /*ii->dat*/);
1770 			if (slots_used < 6) {
1771 				if (addrreg /*ii->dat*/ != &o_regs[slots_used]) {
1772 					free_preg(&o_regs[slots_used], il, 1, 1);
1773 					icode_make_copyreg(&o_regs[slots_used],
1774 						addrreg /*ii->dat*/, NULL, NULL, il);
1775 				}
1776 
1777 				regidx = slots_used;
1778 #if 0
1779 				++gprs_used;
1780 #endif
1781 				gprs_used_map[slots_used] = 1;
1782 				++slots_used;
1783 			} else {
1784 				struct vreg	*vr;
1785 
1786 				vr = n_xmemdup(vrs[i], sizeof *vrs[i]);
1787 				vr->type = addrofify_type(vrs[i]->type);
1788 				vr->size = backend->get_sizeof_type(vr->type, NULL);
1789 				vreg_map_preg(vr, addrreg /*ii->dat*/);
1790 				pass_arg_stack(/*vrs[i]*/vr, 0,
1791 					&slots_used, &stack_bytes_used, il);
1792 			}
1793 
1794 			if (regidx == -1 || addrreg /*ii->dat*/ != &o_regs[regidx]) {
1795 				free_preg(addrreg /*ii->dat*/, il, 1, 0);
1796 			}
1797 
1798 			/* 12/17/07: This (unallocatable) was missing! */
1799 			if (regidx != -1) {
1800 				reg_set_unallocatable(&o_regs[regidx]);
1801 			}
1802 		} else if (IS_FLOATING(vrs[i]->type->code)) {
1803 			/*
1804 			 * For variadic functions, floating point values
1805 			 * go into gprs (floats are promoted to double)
1806 			 */
1807 			if (need_dap) {
1808 				if (vrs[i]->type->code == TY_FLOAT) {
1809 					vrs[i] = backend->
1810 						icode_make_cast(vrs[i],
1811 							make_basic_type(TY_DOUBLE), il);
1812 				} else if (vrs[i]->type->code == TY_LDOUBLE) {
1813 					/*
1814 					 * Long double is tricky because
1815 					 * it 1) is passed in two gprs, and
1816 					 * 2) those gprs need to be aligned
1817 					 */
1818 					if (slots_used < 6) {
1819 						if (slots_used & 1) {
1820 							/* Not aligned! */
1821 							++slots_used;
1822 						}
1823 					}
1824 				}
1825 				vreg_faultin(NULL, NULL, vrs[i], il, 0);
1826 
1827 				if (slots_used < 6) {
1828 					struct type	*oty = vrs[i]->type;
1829 
1830 					vreg_anonymify(&vrs[i], NULL, NULL, il);
1831 					free_preg(vrs[i]->pregs[0], il, 1, 1);
1832 					vrs[i] = vreg_disconnect(vrs[i]);
1833 
1834 					vrs[i]->type = make_basic_type(TY_ULONG);
1835 					vrs[i]->size = 8;
1836 					free_preg(&o_regs[slots_used], il, 1, 1);
1837 					vreg_faultin(&o_regs[slots_used],
1838 						NULL,
1839 						vrs[i], il, 0);
1840 
1841 					reg_set_unallocatable(&o_regs[slots_used]);
1842 
1843 					++slots_used;
1844 					if (oty->code == TY_LDOUBLE) {
1845 						if (slots_used < 6) {
1846 							struct vreg	*tmp;
1847 
1848 							tmp = n_xmemdup(vrs[i],
1849 								sizeof *vrs[i]);
1850 							tmp->pregs[0] = NULL;
1851 							free_preg(&o_regs[slots_used], il, 1, 1);
1852 							vreg_faultin(
1853 							&o_regs[slots_used],
1854 							NULL, tmp, il, 0);
1855 
1856 							/* 12/17/07: Unallocable missing */
1857 							reg_set_unallocatable(
1858 								&o_regs[slots_used]);
1859 							++slots_used;
1860 						} else {
1861 							unimpl();
1862 						}
1863 					}
1864 				} else {
1865 					if (vrs[i]->type->code == TY_LDOUBLE) {
1866 						/*
1867 						 * 01/12/08: Align for long double if we're at an
1868 						 * odd-numbered slot
1869 						 */
1870 						if (slots_used & 1) {
1871 							++slots_used;
1872 							stack_bytes_used += 8;
1873 						}
1874 					}
1875 					pass_arg_stack(vrs[i], 0,
1876 						&slots_used, &stack_bytes_used, il);
1877 				}
1878 			} else {
1879 				if (slots_used < 16) {
1880 					/*
1881 					 * put_arg_into_reg() only increments by one; However
1882 					 * for double and long double we need two or four
1883 					 * FPRs, respectively
1884 					 */
1885 					fprs_used = slots_used * 2;
1886 					if (vrs[i]->type->code == TY_FLOAT) {
1887 						/*
1888 						 * 12/28/07: As it turns out, floats are passed
1889 						 * with double-FPRs too, and they are right-
1890 						 * adjusted, if the current register number is
1891 						 * not odd-numberedd
1892 						 */
1893 						if ((fprs_used & 1) == 0) {
1894 							++fprs_used;
1895 						}
1896 					} else if (vrs[i]->type->code == TY_LDOUBLE) {
1897 						if (fprs_used & 3) {
1898 							/* Not 4-reg-aligned */
1899 							++slots_used;
1900 							fprs_used += 2;
1901 						}
1902 					}
1903 
1904 					/* 01/11/08: New function to save fprs if needed */
1905 					put_sparc_fp_arg_into_reg(sparc_fprs,
1906 						&fprs_used, 0, vrs[i], il);
1907 
1908 					++slots_used;
1909 					if (vrs[i]->type->code == TY_DOUBLE) {
1910 						reg_set_unallocatable(&sparc_fprs[fprs_used]);
1911 						++fprs_used;
1912 					} else if (vrs[i]->type->code == TY_LDOUBLE) {
1913 						reg_set_unallocatable(&sparc_fprs[fprs_used++]);
1914 						reg_set_unallocatable(&sparc_fprs[fprs_used++]);
1915 						reg_set_unallocatable(&sparc_fprs[fprs_used++]);
1916 						++slots_used;
1917 					}
1918 				} else {
1919 					if (vrs[i]->type->code == TY_LDOUBLE) {
1920 						/*
1921 						 * 01/12/08: Align for long double if we're at
1922 						 * an odd-numbered slot
1923 						 */
1924 						if (slots_used & 1) {
1925 							++slots_used;
1926 							stack_bytes_used += 8;
1927 						}
1928 					}
1929 					pass_arg_stack(vrs[i], 0,
1930 						&slots_used, &stack_bytes_used, il);
1931 				}
1932 			}
1933 		} else {
1934 			unimpl();
1935 		}
1936 
1937 		/* We can free the register(s) - marked unallocatable */
1938 		/*free_pregs_vreg(vrs[i], il, 0, 0);*/
1939 		if (vr2 && vr2->from_ptr && vr2->from_ptr->pregs[0]
1940 			&& vr2->from_ptr->pregs[0]->vreg == vr2->from_ptr) {
1941 			free_preg(vr2->from_ptr->pregs[0], il, 0, 0);
1942 		}
1943 	}
1944 
1945 	if (struct_return) {
1946         /* XXX uh-huh what's going on here?!!?! why this AGAIN??? */
1947 		struct reg *r;
1948 		/*ii*/r = make_addrof_structret(struct_lvalue, il);
1949 
1950 		icode_make_copyreg(&o_regs[0], r /*ii->dat*/, NULL, NULL, il);
1951 
1952 		free_preg(r /*ii->dat*/, il, 0, 0);
1953 	}
1954 
1955 	gprs_used = slots_used >= 6? 6: slots_used;
1956 	for (i = 0; i < gprs_used; ++i) {
1957 		if (!gprs_used_map[i]) {
1958 			/*
1959 			 * 12/23/08: The GPR isn't actually used for an
1960 			 * argument (presumably an FP arg caused it to
1961 			 * be skipped), so it has to be freed during
1962 			 * invalidation
1963 			 */
1964 			continue;
1965 		}
1966 
1967 		/* Don't save argument registers */
1968 		reg_set_unused(&o_regs[i]);
1969 
1970 		/*
1971 		 * 12/29/07: This was restoring allocatability too early!
1972 		 * If we mark the argument registers allocatable, they may
1973 		 * be used to build addresses for any stores performed by
1974 		 * the invalidate_gprs() below, thus trashing their
1975 		 * contents. Note that we explicitly have to disassociate
1976 		 * the vreg from the preg, since invalidate_gprs() won't
1977 		 * do this because they are already marked unused
1978 		 */
1979 		o_regs[i].vreg = NULL;
1980 		o_regs[i].allocatable = 0;
1981 	}
1982 	fprs_used = slots_used * 2;
1983 	if (fprs_used > 32) {
1984 		fprs_used = 32;
1985 	}
1986 	for (i = 0; i < fprs_used; ++i) {
1987 		/* 12/29/07: Don't save fp argument registers */
1988 		reg_set_unused(&sparc_fprs[i]);
1989 		sparc_fprs[i].vreg = NULL;
1990 		sparc_fprs[i].allocatable = 0;
1991 	}
1992 
1993 	if (!takes_struct) {
1994 		backend->invalidate_gprs(il, 1, INV_FOR_FCALL);
1995 	}
1996 
1997 	for (i = 0; i < gprs_used; ++i) {
1998 		o_regs[i].allocatable = 1;
1999 	}
2000 	for (i = 0; i < fprs_used; ++i) {
2001 		sparc_fprs[i].allocatable = 1;
2002 	}
2003 
2004 	if (ty->tlist->type == TN_POINTER_TO) {
2005 		/*
2006 		 * Need to indirect through function pointer. It is
2007 		 * necessary to mark argument registers unallocatable
2008 		 * because the function pointer itself may be loaded
2009 		 * indirectly, e.g. through a struct pointer, and
2010 		 * that pointer then too needs to be faulted in
2011 		 */
2012 		for (i = 0; i < gprs_used; ++i) {
2013 			o_regs[i].allocatable = 0;
2014 		}
2015 
2016 		/* 12/13/07 Don't use tmpgpr!!! */
2017 		vreg_faultin(/*tmpgpr*/ NULL, NULL, tmpvr, il, 0);
2018 
2019 		for (i = 0; i < gprs_used; ++i) {
2020 			o_regs[i].allocatable = 1;
2021 		}
2022 		ii = icode_make_call_indir(tmpvr->pregs[0]);
2023 		free_preg(tmpvr->pregs[0], il, 1, 0);
2024 	} else {
2025 		ii = icode_make_call(ty->name);
2026 	}
2027 	append_icode_list(il, ii);
2028 
2029 	ret = vreg_alloc(NULL, NULL, NULL, NULL);
2030 	ret->type = ty;
2031 
2032 	if ((ty->tlist->type == TN_POINTER_TO
2033 		&& ty->tlist->next->next != NULL)
2034 		|| (ty->tlist->type == TN_FUNCTION
2035 		&& ty->tlist->next != NULL)) {
2036 		/* Must be pointer */
2037 		ret->pregs[0] = &o_regs[0];
2038 	} else {
2039 #if 1 /* XXX Should go! Use rettype instead */
2040 		struct type_node	*tnsav = ty->tlist;
2041 
2042 		ty->tlist = NULL;
2043 #endif
2044 		if (backend->abi != ABI_SPARC64
2045 			&& IS_LLONG(ty->code)) {
2046 			is_multi_reg_obj = 2;
2047 		}
2048 		if (is_integral_type(ty)) {
2049 			ret->pregs[0] = &o_regs[0];
2050 			if (is_multi_reg_obj) {
2051 				ret->pregs[1] = &o_regs[1];
2052 			}
2053 		} else if (ty->code == TY_FLOAT
2054 			|| ty->code == TY_DOUBLE
2055 			|| ty->code == TY_LDOUBLE) {
2056 			ret->pregs[0] = &sparc_fprs[0];
2057 		} else if (ty->code == TY_STRUCT
2058 			|| ty->code == TY_UNION) {
2059 			if (ret_is_anon_struct) {
2060 				/*
2061 				 * 08/16/07: Added this
2062 				 */
2063 				ret = struct_lvalue;
2064 			}
2065 			ret->struct_ret = 1;
2066 			ret->pregs[0] = NULL;
2067 		} else if (ty->code == TY_VOID) {
2068 			; /* Nothing! */
2069 		}
2070 #if 1
2071 		ty->tlist = tnsav;
2072 #endif
2073 	}
2074 	for (i = 0; i < 6; ++i) {
2075 		reg_set_allocatable(&o_regs[i]);
2076 	}
2077 
2078 	if (ret->pregs[0] != NULL) {
2079 		vreg_map_preg(ret, ret->pregs[0]);
2080 	}
2081 	if (is_multi_reg_obj) {
2082 		vreg_map_preg2(ret, ret->pregs[1]);
2083 		ret->is_multi_reg_obj = 2;
2084 	}
2085 
2086 	ret->type = n_xmemdup(ret->type, sizeof *ret->type);
2087 	if (ret->type->tlist->type == TN_POINTER_TO) {
2088 		copy_tlist(&ret->type->tlist, ret->type->tlist->next->next);
2089 	} else {
2090 		copy_tlist(&ret->type->tlist, ret->type->tlist->next);
2091 	}
2092 	if (ret->type->code != TY_VOID || ret->type->tlist) {
2093 		ret->size = backend->get_sizeof_type(ret->type, NULL);
2094 	}
2095 
2096 	return ret;
2097 }
2098 
2099 static int
icode_make_return(struct vreg * vr,struct icode_list * il)2100 icode_make_return(struct vreg *vr, struct icode_list *il) {
2101 	struct icode_instr	*ii;
2102 #if 0
2103 	struct type_node	*oldtn;
2104 #endif
2105 	struct type		*rtype = curfunc->rettype; /*curfunc->proto->dtype;*/
2106 
2107 	/* 06/17/08: Use rettype instead of type kludgery */
2108 #if 0
2109 	oldtn = rtype->tlist;
2110 	rtype->tlist = rtype->tlist->next;
2111 #endif
2112 
2113 	if (vr != NULL) {
2114 		if (is_integral_type(rtype)
2115 			|| rtype->code == TY_ENUM /* 06/15/09: Was missing?!? */
2116 			|| rtype->tlist != NULL) {
2117 			/* XXX long long ?!?!!?!??!?? */
2118 			if (vr->is_multi_reg_obj) {
2119 				vreg_faultin(&i_regs[0],
2120 					&i_regs[1], vr, il, 0);
2121 			} else {
2122 				vreg_faultin(&i_regs[0], NULL,
2123 					vr, il, 0);
2124 			}
2125 		} else if (rtype->code == TY_FLOAT
2126 			|| rtype->code == TY_DOUBLE
2127 			|| rtype->code == TY_LDOUBLE) {
2128 			/* XXX ldouble ... */
2129 			vreg_faultin(&sparc_fprs[0], NULL, vr, il, 0);
2130 		} else if (rtype->code == TY_STRUCT
2131 			|| rtype->code == TY_UNION) {
2132 			/* vr may come from pointer */
2133 			vreg_faultin_ptr(vr, il);
2134 			icode_make_copystruct(NULL, vr, il);
2135 		}
2136 	}
2137 	ii = icode_make_ret(vr);
2138 	append_icode_list(il, ii);
2139 
2140 #if 0
2141 	rtype->tlist = oldtn;
2142 #endif
2143 	return 0;
2144 }
2145 
2146 static void
icode_prepare_op(struct vreg ** dest,struct vreg ** src,int op,struct icode_list * il)2147 icode_prepare_op(
2148 	struct vreg **dest,
2149 	struct vreg **src,
2150 	int op,
2151 	struct icode_list *il) {
2152 
2153 	vreg_faultin(NULL, NULL, *dest, il, 0);
2154 	vreg_faultin(NULL, NULL, *src, il, 0);
2155 	(void) op;
2156 }
2157 
2158 
2159 void
change_preg_size(struct vreg * ret,struct type * to,struct type * from,struct icode_list * il)2160 change_preg_size(struct vreg *ret, struct type *to, struct type *from,
2161 	struct icode_list *il) {
2162 	struct icode_instr	*ii;
2163 
2164 	int	to_is_64bit = 0;
2165 	int	from_is_64bit = 0;
2166 
2167 	if (to->tlist == NULL) {
2168 		if (IS_LLONG(to->code)
2169 			|| (backend->abi == ABI_SPARC64
2170 				&& IS_LONG(to->code))) {
2171 			to_is_64bit = 1;
2172 		}
2173 	}
2174 	if (from->tlist == NULL) {
2175 		if (IS_LLONG(from->code)
2176 			|| (backend->abi == ABI_SPARC64
2177 				&& IS_LONG(from->code))) {
2178 			from_is_64bit = 1;
2179 		}
2180 	}
2181 
2182 	if (IS_CHAR(from->code) && IS_CHAR(to->code)) {
2183 		if (from->sign == TOK_KEY_UNSIGNED
2184 			&& to->code == TY_SCHAR) {
2185 			unimpl();
2186 		}
2187 	} else if (to_is_64bit) {
2188 		if (!from_is_64bit && !from->tlist) {
2189 			if (backend->abi != ABI_SPARC64) {
2190 				ret->pregs[1] = ret->pregs[0];
2191 				ret->pregs[0] = ALLOC_GPR(curfunc
2192 					, 0, il, NULL);
2193 				if (from->sign != TOK_KEY_UNSIGNED) {
2194 					unimpl();
2195 				} else {
2196 					ii = icode_make_setreg(
2197 						ret->pregs[0], 0);
2198 					append_icode_list(il, ii);
2199 				}
2200 			} else {
2201 				if (from->sign == TOK_KEY_UNSIGNED
2202 					|| from->code == TY_CHAR) {
2203 					int	bits = 0;
2204 
2205 					if (IS_CHAR(from->code)) {
2206 						bits = 56;
2207 					} else if (from->code == TY_USHORT) {
2208 						bits = 48;
2209 					} else if (from->code == TY_UINT) {
2210 						bits = 32;
2211 					} else {
2212 						unimpl();
2213 					}
2214 
2215 					(void) bits;
2216 					unimpl();
2217 				} else {
2218 					/* from signed */
2219 					if (from->code == TY_SCHAR) {
2220 					} else if (from->code == TY_SHORT) {
2221 					} else if (from->code == TY_INT) {
2222 					} else {
2223 					}
2224 					unimpl();
2225 				}
2226 			}
2227 		}
2228 	} else if (from_is_64bit) {
2229 		struct reg	*r = ret->pregs[0];
2230 
2231 		if (backend->abi != ABI_SPARC64) {
2232 			ret->pregs[0] = ret->pregs[1];
2233 			free_preg(r, il, 1, 0);
2234 		}
2235 
2236 		/*
2237 		 * Seems r can become gpr0. I do not know why
2238 		 * this is happening but it is terrible
2239 		 */
2240 		sparc_gprs[0].allocatable = sparc_gprs[0].used = 0;
2241 
2242 		goto truncate_further;
2243 	} else {
2244 		/* Neither is long long, but they are different */
2245 		int	needand;
2246 		int	needextsh;
2247 		int	needlwi;
2248 
2249 truncate_further:
2250 		needand = needextsh = needlwi = 0;
2251 
2252 		if (to->code == TY_SCHAR) {
2253 			needand = 0xff;
2254 			needlwi = 24;
2255 		} else if (to->code == TY_CHAR || to->code == TY_UCHAR) {
2256 			needand = 0xff;
2257 		} else if (to->code == TY_SHORT) {
2258 			needand = 0xffff;
2259 			needextsh = 1;
2260 		} else if (to->code == TY_USHORT) {
2261 			needand = 0xffff;
2262 		} else if (to->code == TY_INT) {
2263 		} else if (to->code == TY_UINT) {
2264 			/*needand = 0xffffffff;*/
2265 		} else if (to->code == TY_LONG) {
2266 		} else if (to->code == TY_ULONG) {
2267 			/*needand = 0xffffffff;*/
2268 		}
2269 
2270 		if (needand) {
2271 			ii = icode_make_setreg(tmpgpr, needand);
2272 			append_icode_list(il, ii);
2273 			ii = icode_make_and(ret, NULL);
2274 			append_icode_list(il, ii);
2275 		}
2276 		if (needlwi) {
2277 			unimpl();
2278 		}
2279 		if (needextsh) {
2280 			unimpl();
2281 		}
2282 	}
2283 	sparc_gprs[0].allocatable = sparc_gprs[0].used = 0;
2284 }
2285 
2286 
2287 /*
2288  * Most of the time, instructions give meaning to data. This function
2289  * generates code required to convert virtual register ``src'' to type
2290  * ``to'' where necessary
2291  */
2292 static struct vreg *
icode_make_cast(struct vreg * src,struct type * to,struct icode_list * il)2293 icode_make_cast(struct vreg *src, struct type *to, struct icode_list *il) {
2294 	struct vreg		*ret;
2295 	struct type		*from = src->type;
2296 	struct type		*orig_to = to;
2297 	struct icode_instr	*ii;
2298 
2299 	ret = src;
2300 
2301 	if (ret->type->tlist != NULL
2302 		|| (ret->type->code != TY_STRUCT
2303 		&& ret->type->code != TY_UNION)) {
2304 		vreg_anonymify(&ret, NULL, NULL /*r*/, il);
2305 	}
2306 
2307 	/* XXX anonymify.. */
2308 	if (ret == src) {
2309 		ret = vreg_disconnect(src);
2310 	}
2311 	ret->type = to;
2312 
2313 	if (to->code == TY_VOID) {
2314 		if (to->tlist == NULL) {
2315 			ret->size = 0;
2316 			free_pregs_vreg(ret, il, 0, 0);
2317 			return ret;
2318 		}
2319 	} else {
2320 		ret->is_nullptr_const = 0;
2321 	}
2322 	ret->size = backend->get_sizeof_type(to, NULL);
2323 
2324 	if (from->tlist != NULL && to->tlist != NULL) {
2325 		/*
2326 		 * Pointers are always of same size
2327 		 * and use same registers
2328 		 */
2329 		return ret;
2330 	} else if (to->tlist != NULL) {
2331 		/*
2332 		 * Integral type to pointer type - cast to
2333 		 * uintptr_t to get it to the same size
2334 		 */
2335 		to = backend->get_uintptr_t();
2336 	}
2337 
2338 	if (to->code != from->code) {
2339 		/*
2340 		 * XXX source type may be trashed. This is perhaps a
2341 		 * bug in vreg_anonymify()!!? this kludge fixes it
2342 		 * for now ...
2343 		 */
2344 		src = n_xmemdup(src, sizeof *src);
2345 		src->type = from;
2346 		src->size = backend->get_sizeof_type(from, 0);
2347 	}
2348 
2349 	/*
2350 	 * We may have to move the item to a different
2351 	 * register as a result of the conversion
2352 	 */
2353 	if (to->code == from->code) {
2354 		return ret; /* Nothing to do */
2355 	} else if (to->tlist || from->tlist) {
2356 		; /* XXX hmm */
2357 	} else if (IS_FLOATING(to->code)) {
2358 		if (backend->abi == ABI_SPARC64
2359 			&& (IS_LONG(from->code) ||
2360 				IS_LLONG(from->code))) {
2361 
2362 			/*
2363 			 * Load value as integer, then conv_fp it. That's
2364 			 * it!
2365 			 */
2366 			struct vreg	*double_vreg;
2367 			struct vreg	*tmpret;
2368 			struct reg	*fpreg;
2369 			int		to_size;
2370 
2371 			to_size = backend->get_sizeof_type(to, NULL);
2372 
2373 			/* Save integer to stack */
2374 			tmpret = n_xmemdup(ret, sizeof *ret); /* XXX :-( */
2375 			tmpret->type = from;
2376 			tmpret->size = backend->get_sizeof_type(from, NULL);
2377 			vreg_map_preg(tmpret, tmpret->pregs[0]);
2378 			free_preg(tmpret->pregs[0], il, 1, 1);
2379 
2380 			/* Now load as double */
2381 			double_vreg = n_xmemdup(tmpret, sizeof *tmpret);
2382 			double_vreg->type = make_basic_type(TY_DOUBLE);
2383 
2384 			if (to_size == 4) {
2385 				to_size = 8;
2386 			}
2387 			fpreg = backend->alloc_fpr(curfunc, to_size, il, NULL);
2388 			vreg_faultin(fpreg, NULL, double_vreg,
2389 				il, 0);
2390 			vreg_map_preg(ret, fpreg);
2391 			icode_make_conv_fp(ret->pregs[0], ret->pregs[0],
2392 				to, from, il);
2393 		} else if (!IS_FLOATING(from->code)) {
2394 			struct vreg	*fp_vreg;
2395 
2396 			/*
2397 			 * We have to load the integer value into a
2398 			 * floating point register and then perform
2399 			 * a fitos/fitod on it
2400 		 	 */
2401 			fp_vreg = ret;
2402 
2403 			if (IS_CHAR(from->code) || IS_SHORT(from->code)) {
2404 				struct type	*ity = make_basic_type(TY_INT);
2405 
2406 				if (from->code == TY_SCHAR
2407 					|| from->code == TY_SHORT) {
2408 					/* Have to sign-extend */
2409 
2410 					icode_make_extend_sign(ret, ity,
2411 						from, il);
2412 				}
2413 				from = ity;
2414 			}
2415 			vreg_reinterpret_as(&fp_vreg, from, to, il);
2416 			icode_make_conv_fp(fp_vreg->pregs[0],
2417 				fp_vreg->pregs[0], to, from, il);
2418 
2419 			vreg_map_preg(ret, fp_vreg->pregs[0]);
2420 		} else if (to->code != from->code) {
2421 			/*
2422 			 * From fp to fp.
2423 			 * Double to float means we can cut the 2-reg set
2424 			 * Float to double means we may have to allocate a
2425 			 * new 2-reg set (currently always done - the
2426 			 * icode_make_conv_fp interface sucks)
2427 			 */
2428 			/* XXXXXXXXX need to aligned alloc reg !!!! */
2429 			struct reg	*r;
2430 
2431 			reg_set_unallocatable(ret->pregs[0]);
2432 			r = backend->alloc_fpr(curfunc,
2433 				backend->get_sizeof_type(to, NULL), il, 0);
2434 			icode_make_conv_fp(r, ret->pregs[0], to, from, il);
2435 			ret->pregs[0] = r;
2436 		}
2437 	} else if (IS_FLOATING(from->code)) {
2438 		struct reg	*r;
2439 		struct vreg	*double_vreg;
2440 		struct vreg	*int_vreg;
2441 
2442 		/*
2443 		 * ``to'' has already been found to be non-fp.
2444 		 * We have to load as float/double (depending on
2445 		 * whether the source integer is 32bit or 64bit)
2446 		 * and then fitos/fitod it
2447 		 */
2448 
2449 		r = ALLOC_GPR(curfunc, 0, il, NULL);
2450 		double_vreg = vreg_alloc(NULL, NULL, NULL, NULL);
2451 		vreg_map_preg(double_vreg, ret->pregs[0]);
2452 		double_vreg->type = from;
2453 		double_vreg->size = backend->get_sizeof_type(from, NULL);
2454 		icode_make_conv_fp(ret->pregs[0], ret->pregs[0], to, from, il);
2455 		icode_make_store(curfunc, double_vreg, double_vreg, il);
2456 		free_preg(ret->pregs[0], il, 1, 0);
2457 
2458 		int_vreg = vreg_alloc(NULL, NULL, NULL, NULL);
2459 		int_vreg->type = to;
2460 		int_vreg->size = backend->get_sizeof_type(to, NULL);
2461 		int_vreg->stack_addr = double_vreg->stack_addr;
2462 
2463 		/*
2464 		 * 05/22/08: Workaround for long double load kludge.
2465 		 * XXX fix this once and for all!
2466 		 */
2467 
2468 		icode_make_sparc_load_int_from_ldouble(r, int_vreg, il);
2469 		ret->pregs[0] = r;
2470 	} else {
2471 		int	to_size = backend->get_sizeof_type(to, NULL);
2472 		int	from_size = backend->get_sizeof_type(from, NULL);
2473 		unsigned needand = 0;
2474 
2475 		if (to_size == from_size) {
2476 			;
2477 		} else if (to->tlist != NULL) {
2478 			; /* XXX What 2 do */
2479 		} else if (to_size < from_size) {
2480 			/* Truncate */
2481 			if (to->tlist != NULL) {
2482 				/* XXX hmm */
2483 			} else if (IS_CHAR(to->code)) {
2484 				needand = 0xff;
2485 			} else if (IS_SHORT(to->code)) {
2486 				needand = 0xffff;
2487 			} else if (IS_INT(to->code)
2488 				|| IS_LONG(to->code)) {
2489 				/* Must be from 64bit long or long long */
2490 				needand = 0xffffffff;
2491 			} else {
2492 				unimpl();
2493 			}
2494 		} else {
2495 			/* to_size > from_size - sign- or zero-extend */
2496 			if (from->sign == TOK_KEY_UNSIGNED) {
2497 				needand = 0xffffffff;
2498 			} else {
2499 				/* sign-extend */
2500 				icode_make_extend_sign(ret, to, from, il);
2501 			}
2502 		}
2503 		if (needand) {
2504 			ii = icode_make_setreg(tmpgpr, needand);
2505 			append_icode_list(il, ii);
2506 			ii = icode_make_and(ret, NULL);
2507 			append_icode_list(il, ii);
2508 		}
2509 	}
2510 
2511 	vreg_set_new_type(ret, orig_to); /* because of uintptr_t stuff */
2512 	vreg_map_preg(ret, ret->pregs[0]);
2513 	if (backend->abi != ABI_SPARC64
2514 		&& IS_LLONG(to->code)
2515 		&& to->tlist == NULL) {
2516 		ret->is_multi_reg_obj = 2;
2517 		vreg_map_preg2(ret, ret->pregs[1]);
2518 	} else {
2519 		ret->is_multi_reg_obj = 0;
2520 		ret->pregs[1] = NULL;
2521 	}
2522 	if (ret->type->code == TY_BOOL && ret->type->tlist == NULL) {
2523 		boolify_result(ret, il);
2524 	}
2525 
2526 	return ret;
2527 }
2528 
2529 static void
icode_make_structreloc(struct copystruct * cs,struct icode_list * il)2530 icode_make_structreloc(struct copystruct *cs, struct icode_list *il) {
2531 	relocate_struct_regs(cs, &o_regs[0], &o_regs[1],
2532 		&o_regs[2], il);
2533 }
2534 
2535 void
generic_icode_initialize_pic(struct function * f,struct icode_list * il)2536 generic_icode_initialize_pic(struct function *f, struct icode_list *il) {
2537 	if (!f->pic_initialized) {
2538 		if (!pic_reg->dedicated) { /* 07/17/09: For MIPS until SPARC
2539 					    * handles dedicated regs
2540 					    * properly!*/
2541 			free_preg(pic_reg, il, 1, 1);
2542 		}
2543 		f->pic_vreg = vreg_alloc(NULL,NULL,NULL,NULL);
2544 		vreg_set_new_type(f->pic_vreg, make_void_ptr_type());
2545 		vreg_map_preg_dedicated(f->pic_vreg, pic_reg);
2546 		/*
2547 		 * 07/17/09: The line below was discovered while
2548 		 * porting to MIPS. It cannot possibly be right
2549 		 * because we don't want to trash the PIC reg!
2550 		 * Probably should set the flag to 0, or better
2551 		 * yet use reg_set_dedicated()
2552 		 */
2553 /*		pic_reg->allocatable = 1;*/
2554 		reg_set_dedicated(pic_reg);
2555 
2556 		/*
2557 		 * 07/17/09: For MIPS, it is always done in the
2558 		 * function prologue
2559 		 */
2560 		if (backend->arch != ARCH_MIPS) {
2561 			icode_make_initialize_pic(f, il);
2562 		}
2563 	} else {
2564 		vreg_faultin_dedicated(pic_reg, NULL, f->pic_vreg, il, 0);
2565 	}
2566 }
2567 
2568 
2569 static void
do_print_gpr(struct reg * r)2570 do_print_gpr(struct reg *r) {
2571 	printf("%s=%d ", r->name, r->used);
2572 	if (r->vreg && r->vreg->pregs[0] == r) {
2573 		printf("<-> %p", r->vreg);
2574 	}
2575 }
2576 
2577 static void
debug_print_gprs(void)2578 debug_print_gprs(void) {
2579 	int	i;
2580 
2581 	for (i = 0; i < N_GPRS; ++i) {
2582 		if ((i % 3) == 0) {
2583 			printf("\t\t");
2584 		} else {
2585 			putchar('\t');
2586 		}
2587 		do_print_gpr(&sparc_gprs[i]);
2588 		if (((i+1) % 3) == 0) {
2589 			putchar('\n');
2590 		}
2591 	}
2592 }
2593 
2594 static int
is_multi_reg_obj(struct type * t)2595 is_multi_reg_obj(struct type *t) {
2596 	(void) t;
2597 	if (backend->abi != ABI_SPARC64
2598 		&& IS_LLONG(t->code)
2599 		&& t->tlist == NULL) {
2600 		return 2;
2601 	}
2602 	return 0;
2603 }
2604 
2605 static struct reg *
name_to_reg(const char * name)2606 name_to_reg(const char *name) {
2607 	(void) name;
2608 	return NULL;
2609 }
2610 
2611 static struct reg *
asmvreg_to_reg(struct vreg ** vr0,int ch,struct inline_asm_io * io,struct icode_list * il,int faultin)2612 asmvreg_to_reg(
2613 	struct vreg **vr0,
2614 	int ch,
2615 	struct inline_asm_io *io,
2616 	struct icode_list *il,
2617 	int faultin) {
2618 
2619 	struct vreg	*vr = *vr0;
2620 
2621 	(void) ch; (void) io; (void) il; (void)faultin;
2622 
2623 	if ((vr->type->code == TY_STRUCT || vr->type->code == TY_UNION)
2624 		&& vr->type->tlist == NULL) {
2625 		errorfl(io->expr->tok,
2626 			"Cannot load struct/union into register");
2627 	}
2628 	return NULL;
2629 }
2630 
2631 static char *
get_inlineasm_label(const char * tmpl)2632 get_inlineasm_label(const char *tmpl) {
2633 	char	*ret = n_xmalloc(strlen(tmpl) + sizeof "inlasm");
2634 	sprintf(ret, "inlasm%s", tmpl);
2635 	return ret;
2636 }
2637 
2638 static struct reg *
get_abi_reg(int index,struct type * ty)2639 get_abi_reg(int index, struct type *ty) {
2640 	if (index == 0
2641 		&& (is_integral_type(ty)
2642 		|| ty->tlist != NULL)) {
2643 		return &o_regs[0];
2644 	} else {
2645 		unimpl();
2646 	}
2647 	return NULL;
2648 }
2649 
2650 static struct reg *
get_abi_ret_reg(struct type * ty)2651 get_abi_ret_reg(struct type *ty) {
2652 	if (is_integral_type(ty) || ty->tlist != NULL) {
2653 		return &o_regs[0];
2654 	} else {
2655 		unimpl();
2656 	}
2657 	/* NOTREACHED */
2658 	return NULL;
2659 }
2660 
2661 /*
2662  * Calculates the amount of stack bytes needed to pass vrs to
2663  * a function, if any. This is because it's more convenient
2664  * to pre-allocated any needed space in advance, rather than
2665  * doing it when needed. Special care must be taken to ensure
2666  * that this stuff is always in sync with the code that
2667  * actually pushes the arguments onto the stack.
2668  *
2669  * It is assumed that the stack starts out being word aligned.
2670  *
2671  */
2672 static size_t
sparc_calc_stack_bytes(struct vreg ** vrs,int nvrs,int * takes_struct)2673 sparc_calc_stack_bytes(struct vreg **vrs, int nvrs, int *takes_struct) {
2674 	size_t	nbytes = 0;
2675 	int	i;
2676 	int	stackstruct = 0;
2677 
2678 	for (i = 0; i < nvrs; ++i) {
2679 		nbytes += sparc_gprs[0].size * 2;
2680 		if (vrs[i]->type->code == TY_STRUCT
2681 			|| vrs[i]->type->code == TY_UNION) {
2682 			nbytes += sparc_gprs[0].size;
2683 			stackstruct = 1;
2684 		}
2685 	}
2686 	while (nbytes % 16) {
2687 		++nbytes;
2688 	}
2689 
2690 	/* Allocate space for saving registers */
2691 	if (stackstruct) {
2692 		/* XXXXXXXX wow reg_offset seems broken!??! */
2693 		reg_offset = nbytes;
2694 		nbytes += 8 * 16;
2695 		*takes_struct = 1;
2696 	} else {
2697 		*takes_struct = 0;
2698 	}
2699 
2700 	return nbytes;
2701 }
2702 
2703 static int
have_immediate_op(struct type * ty,int op)2704 have_immediate_op(struct type *ty, int op) {
2705 	(void) ty; (void) op;
2706 	if (Oflag == -1) { /* XXX */
2707 		return 0;
2708 	}
2709 	return 0;
2710 }
2711 
2712 struct backend sparc_backend = {
2713 	ARCH_SPARC,
2714 	0, /* ABI */
2715 	0, /* multi_gpr_object */
2716 	8, /* structure alignment (set by init()) */
2717 	1, /* need pic initialization? */
2718 	0, /* emulate long double? */
2719 	0, /* relax alloc gpr order? */
2720 	4095, /* max displacement */
2721 	-4096, /* min displacement */
2722 	have_immediate_op,
2723 	init,
2724 	is_multi_reg_obj,
2725 	get_ptr_size,
2726 	get_size_t,
2727 	get_uintptr_t,
2728 	get_wchar_t,
2729 	get_sizeof_basic,
2730 	get_sizeof_type,
2731 	get_sizeof_elem_type,
2732 	get_sizeof_decl,
2733 	get_sizeof_const,
2734 	get_sizeof_vla_type,
2735 	get_align_type,
2736 	gen_function,
2737 #if XLATE_IMMEDIATELY
2738 	gen_prepare_output,
2739 	gen_finish_output,
2740 #else
2741 	gen_program,
2742 #endif
2743 	NULL,
2744 	NULL,
2745 	invalidate_gprs,
2746 	invalidate_except,
2747 	/*generic_*/ alloc_gpr,
2748 	NULL,
2749 	alloc_fpr,
2750 	NULL,
2751 	icode_make_fcall,
2752 	icode_make_return,
2753 	NULL,
2754 	icode_prepare_op,
2755 	NULL, /* prepare_load_addrlabel */
2756 	icode_make_cast,
2757 	icode_make_structreloc,
2758 	generic_icode_initialize_pic, /* icode_initialize_pic */
2759 	NULL, /* icode_complete_func */
2760 	make_null_block,
2761 	make_init_name,
2762 	debug_print_gprs,
2763 	name_to_reg,
2764 	asmvreg_to_reg,
2765 	get_inlineasm_label,
2766 	do_ret,
2767 	get_abi_reg,
2768 	get_abi_ret_reg,
2769 	generic_same_representation
2770 };
2771 
2772 
2773