11 extern char *strcpy();
14 #define free_reg_num(i) if (1) { assert(reg[i].inuse > 0); reg[i].inuse--; if (debug) fprintf(stderr,"free_reg(%s)\n", regnam[i]); } else
16 #define POP1 cache_need(1);
17 #define POP2 { --tos; assert(c_count); --c_count; }
19 static int indent_count = 0;
21 #define enter(x) indent_count++;
22 #define indent() { int i = indent_count; while (i--) putc('\t', stderr); }
23 #define leave(x) indent_count--;
26 const13(int) boolean proc
27 init_cache() mandatory
29 forced_alloc_reg(reg_t)
31 change_reg(reg_t) used when reg on stack has changed
32 type_of_tos() bit-field representation of ext/reg/cst
33 inc_tos(int) tos->cst += n
37 flush_cache() after branches and labels
38 cache_read(int) read-ahead. optimization only
39 dump_cache(File *) debug info: show current stack
40 pop_nop() remove element from cache
44 reg_t pop_reg_c13(char*)
46 arith pop_const(char *)
49 alloc_float, alloc_double, free_double,
50 pop_float, pop_double, push_float, push_double
54 typedef struct regdat_t {
55 int inuse; /* free == 0, otherwise #owners */
58 typedef struct cache_elt {
65 #define REG_NUM(r) (((char(*)[8])(r))-regnam)
67 static char regnam[][8] = {
68 /*x*/ "%g0", "%g1", "%g2", "%g3", "%g4", "%g5", "%g6", "%g7",
69 /*x*/ "%i0", "%i1", "%i2", "%i3", "%i4", "%i5", "%i6", "%i7",
70 /*x*/ "%l0", "%l1", "%l2", "%l3", "%l4", "%l5", "%l6", "%l7",
71 /*x*/ "%o0", "%o1", "%o2", "%o3", "%o4", "%o5", "%o6", "%o7",
72 /*x*/ "%f0", "%f1", "%f2", "%f3", "%f4", "%f5", "%f6", "%f7",
73 /*x*/ "%f8", "%f9", "%f10", "%f11", "%f12", "%f13", "%f14", "%f15",
74 /*x*/ "%f16", "%f17", "%f18", "%f19", "%f20", "%f21", "%f22", "%f23",
75 /*x*/ "%f24", "%f25", "%f26", "%f27", "%f28", "%f29", "%f30", "%f31"
78 reg_t reg_g0, reg_g1, reg_g2, reg_g3, reg_g4, reg_g5, reg_g6, reg_g7;
79 reg_t reg_i0, reg_i1, reg_i2, reg_i3, reg_i4, reg_i5, reg_i6, reg_i7;
80 reg_t reg_l0, reg_l1, reg_l2, reg_l3, reg_l4, reg_l5, reg_l6, reg_l7;
81 reg_t reg_o0, reg_o1, reg_o2, reg_o3, reg_o4, reg_o5, reg_o6, reg_o7;
83 reg_t reg_sp, reg_lb, reg_gap;
84 reg_t reg_tmp, reg_lin, reg_fil;
86 static struct regdat_t reg[NR_REGS];
88 #define POP_SIZE 1 /* maybe >1 (read-ahead cache) or explicit?! */
89 #define CACHE_SIZE 32 /* ? */
103 return (x < 4096 && x >= -4096);
106 static struct cache_elt cache[CACHE_SIZE], *tos = 0;
107 static int c_count = 0;
108 static const_str_t s;
110 _PROTOTYPE(static void panic, (char*));
111 _PROTOTYPE(static void dump_cache, (File *stream));
112 _PROTOTYPE(static int cache_read, (int n, int i));
113 _PROTOTYPE(static void flush_part_cache, (int c, int r, int f, int d));
114 _PROTOTYPE(static void subst_reg, (reg_t, reg_t));
119 printf("PANIC: %s\n", s);
127 for (i = 0; i < NR_REGS; i++)
130 reg_g0 = regnam[000];
131 reg_g1 = regnam[001];
132 reg_g2 = regnam[002];
133 reg_g3 = regnam[003];
134 reg_g4 = regnam[004];
135 reg_g5 = regnam[005];
136 reg_g6 = regnam[006];
137 reg_g7 = regnam[007];
139 reg_i0 = regnam[010];
140 reg_i1 = regnam[011];
141 reg_i2 = regnam[012];
142 reg_i3 = regnam[013];
143 reg_i4 = regnam[014];
144 reg_i5 = regnam[015];
145 reg_i6 = regnam[016];
146 reg_i7 = regnam[017];
148 reg_l0 = regnam[020];
149 reg_l1 = regnam[021];
150 reg_l2 = regnam[022];
151 reg_l3 = regnam[023];
152 reg_l4 = regnam[024];
153 reg_l5 = regnam[025];
154 reg_l6 = regnam[026];
155 reg_l7 = regnam[027];
157 reg_o0 = regnam[030];
158 reg_o1 = regnam[031];
159 reg_o2 = regnam[032];
160 reg_o3 = regnam[033];
161 reg_o4 = regnam[034];
162 reg_o5 = regnam[035];
163 reg_o6 = regnam[036];
164 reg_o7 = regnam[037];
166 reg_f0 = regnam[040];
172 forced_alloc_reg(reg_g0); /* can not be used as a reg */
173 forced_alloc_reg(reg_o6);
174 forced_alloc_reg(reg_o7);
175 forced_alloc_reg(reg_i6);
176 forced_alloc_reg(reg_i7);
179 static void flush_part_cache(n,r,f,d)
182 /* free at least n entries, r integer registers, f float regs and d double regs
186 const_str_t i_str, e_str, n_str;
190 enter("flush_part_cache");
191 for (i = 0; i < c_count; i++)
193 if (i >= n && !r && !f && !d)
195 if (cache[i].reg != reg_g0)
197 rn= REG_NUM(cache[i].reg);
207 if (d && (((rn & 1) &&
209 (!(rn & 1) && !reg[rn+1].inuse)))
215 if (cache[i].reg2 != reg_g0)
217 rn= REG_NUM(cache[i].reg2);
227 if (d && (((rn & 1) &&
229 (!(rn & 1) && !reg[rn+1].inuse)))
237 panic ("can't free enough registers");
241 sprint (i_str, "%d", 4*i);
242 "dec $i_str, $reg_sp";
245 sprint(i_str, "%d", 4*(j-1-i));
249 sprint (e_str, "%d", cache[i].cst);
250 "set $ext+$e_str, $reg_tmp";
251 "st $reg_tmp, [$reg_sp+$i_str]";
256 if (cache[i].reg2 != reg_g0)
260 "add $rt, $rh, $reg_tmp";
261 cache[i].reg = reg_tmp;
262 cache[i].reg2 = reg_g0;
264 if (!const13(cache[i].cst))
268 "sethi %hi($n_str), $reg_tmp";
269 if (cache[i].reg != reg_g0)
272 "add $reg_tmp, $rt, $reg_tmp";
275 cache[i].cst &= 0x3ff;
282 sprint(n_str, "%d", cache[i].cst);
283 "add $rh, $n_str, $reg_tmp";
286 "st $rh, [$reg_sp+$i_str]";
289 for (i= j; i < c_count; i++)
290 cache[i-j]= cache[i];
292 tos= &cache[c_count-1];
294 leave("flush_part_cache");
303 for (i = LO_GLOB+1 /* SPEED-HACK */; i <= HI_GLOB; i++) {
306 reg[i].inuse = 1; /* allocate */
311 for (i = LO_IN; i <= HI_IN; i++) {
314 reg[i].inuse = 1; /* allocate */
319 for (i = LO_LOC; i <= HI_LOC; i++) {
322 reg[i].inuse = 1; /* allocate */
327 for (i = LO_OUT; i <= HI_OUT; i++) {
330 reg[i].inuse = 1; /* allocate */
335 flush_part_cache(c_count/2,1,0,0);
338 if (debug) { indent(); fprintf(stderr,"alloc_reg() = %s\n", res ? regnam[i] : "NULL"); }
348 enter("alloc_float");
349 for (i = LO_FLOAT+15; i >= LO_FLOAT; i--) {
352 reg[i].inuse = 1; /* allocate */
357 flush_part_cache(c_count/2,0,1,0);
360 if (debug) { indent(); fprintf(stderr,"alloc_float() = %s\n", res ? regnam[i] : "NULL"); }
361 leave("alloc_float");
365 reg_t alloc_double(sub_reg)
371 enter("alloc_double");
372 for (i = LO_FLOAT+14; i >= LO_FLOAT; i -= 2) {
373 if (reg[i].inuse || reg[i+1].inuse)
375 reg[i].inuse = 1; /* allocate */
376 reg[i+1].inuse = 1; /* allocate */
377 if (debug) { indent(); fprintf(stderr,"alloc_double() = %s\n", regnam[i]); }
379 *sub_reg= regnam[i+1];
384 flush_part_cache(c_count/2,0,0,1);
385 res = alloc_double(sub_reg);
387 if (debug) { indent(); fprintf(stderr,"alloc_double() = %s\n", res ? regnam[i] : "NULL"); }
388 leave("alloc_double");
392 reg_t alloc_reg_var() /* ins and locals only */
397 enter("alloc_reg_var");
398 for (i = LO_LOC +2 /* SPEED-HACK */; i <= HI_LOC; i++) {
401 reg[i].inuse = 1; /* allocate */
406 for (i = LO_IN; i <= HI_IN; i++) {
409 reg[i].inuse = 1; /* allocate */
413 if (debug) { indent(); fprintf(stderr,"alloc_reg_var() = %s\n", res ? regnam[i] : "NULL"); }
414 leave("alloc_reg_var");
418 reg_t alloc_float_var()
423 enter("alloc_float_var");
424 for (i= LO_FLOAT+16; i<= HI_FLOAT; i++)
428 reg[i].inuse = 1; /* allocate */
432 if (debug) { indent(); fprintf(stderr,"alloc_reg_var() = %s\n", res ? regnam[i] : "NULL"); }
433 leave("alloc_float_var");
437 reg_t alloc_double_var(sub_reg)
443 enter("alloc_double_var");
444 for (i = LO_FLOAT+16; i < HI_FLOAT; i += 2) {
445 if (reg[i].inuse || reg[i+1].inuse)
447 reg[i].inuse = 1; /* allocate */
448 reg[i+1].inuse = 1; /* allocate */
449 if (debug) { indent(); fprintf(stderr,"alloc_double_var() = %s\n", regnam[i]); }
451 *sub_reg= regnam[i+1];
455 if (debug) { indent(); fprintf(stderr,"alloc_double_var() = %s\n", res? regnam[i] : "NULL"); }
456 leave("alloc_double_var");
466 if (i != 0 && i != reg_g0) {
468 assert(0 <= r && r <= NR_REGS);
469 assert(reg[r].inuse > 0); /* "freeing unused register" */
472 if (debug) { indent(); fprintf(stderr,"free_reg(%s)\n", i); }
476 void free_double_reg(i)
481 enter("free_double_reg");
485 free_reg(regnam[rn+1]);
486 leave("free_double_reg");
490 void force_alloc_output() /* TMP HACK */
494 enter("force_alloc_output");
495 if (debug) { indent(); fprintf(stderr,"force_alloc_output\n"); }
496 for (i = REG_NUM(reg_o0); i <= REG_NUM(reg_o5); i++)
497 forced_alloc_reg(regnam[i]);
498 leave("force_alloc_output");
505 enter("free_output");
506 leave("free_output");
507 if (debug) { indent(); fprintf(stderr,"free_output\n"); }
508 for (i = REG_NUM(reg_o0); i <= REG_NUM(reg_o5); i++) {
509 assert(reg[i].inuse > 0);
514 void soft_alloc_reg(i)
517 enter("soft_alloc_reg");
518 if (debug) { indent(); fprintf(stderr,"soft_alloc_reg(%s)\n", i); }
519 /* assert(reg[REG_NUM(i)].inuse); */
520 reg[REG_NUM(i)].inuse++;
521 leave("soft_alloc_reg");
524 void forced_alloc_reg(i)
529 enter("forced_alloc_reg");
530 if (debug) { indent(); fprintf(stderr,"forced_alloc_reg(%s)\n", i); }
539 if (debug) { indent(); fprintf(stderr,"---> inuse: moving to %s\n", S1); }
544 panic("forced_alloc_reg: external owners left!");
547 leave("forced_alloc_reg");
550 void change_reg(r) /* to be called when register r changes */
556 if (debug) { indent(); fprintf(stderr, "change_reg(%s)\n", r); }
558 for (i = 0; i < c_count; i++)
559 if (cache[i].reg == r || cache[i].reg2 == r) {
576 static void subst_reg(old, new)
582 if (debug) { indent(); fprintf(stderr,"subst_reg(%s, %s)\n", old, new); }
583 for (i = 0; i < c_count; i++) {
584 if (cache[i].reg == old) {
589 if (cache[i].reg2 == old) {
606 if (tos->reg != reg_g0)
607 if (tos->reg >= reg_f0)
611 if (tos->reg2 != reg_g0)
612 if (tos->reg2 >= reg_f0)
624 assert(type_of_tos() == T_cst);
625 if (debug) { indent(); fprintf(stderr,"top_const()=%d\n", tos->cst); }
635 enter("pop_reg_reg");
637 if (!(type_of_tos() & T_reg2))
643 if (debug) { indent(); fprintf(stderr,"pop_reg_reg()=%s\n", s); fprint(codefile,"\t\t! "); dump_cache(codefile); }
644 leave("pop_reg_reg");
648 reg_t pop_reg_c13(n) /* returns reg_t + optional n (as string) */
654 enter("pop_reg_c13");
655 if (debug) { indent(); fprintf(stderr,"pop_reg_c13()=...\n"); }
660 if (tos->reg >= reg_f0) { /* convert float to int */
668 else if (tos->reg2 != reg_g0) { /* add integers */
672 assert(tos->reg == reg_g0);
675 sprint(V2, "%d", tos->cst);
676 "sethi %hi($V1+$V2), $S1";
677 sprint(n, "%%lo(%s+%d)", tos->ext, tos->cst);
682 if (!(const13(tos->cst))) {
684 sprint(V2, "%d", tos->cst);
685 "sethi %hi($V2), $S3";
686 if (tos->reg != reg_g0) {
699 sprint(n, "%d", tos->cst);
702 if (debug) { indent(); fprint(codefile, "\t\t! %s+%s cache:", S1, n); dump_cache(codefile);}
703 leave("pop_reg_c13");
715 if (!(type_of_tos() & T_float)) {
723 else if (tos->reg2 >= reg_f0) {
726 "fadds $S1, $R1, $R2";
733 if (debug) { indent(); fprint(codefile, "\t\t! %s cache:", S1); dump_cache(codefile); }
741 enter("inc_tos_reg");
742 if (debug) { indent(); fprintf(stderr, "inc_tos_reg(%s)\n", r); }
743 if (type_of_tos() != T_reg)
746 if (debug) { indent(); fprint(codefile, "\t\t! "); dump_cache(codefile); }
747 leave("inc_tos_reg");
757 if (debug) { indent(); fprintf(stderr,"inc_tos(%d)\n", n); }
759 if (tos->reg >= reg_f0)
764 else if (tos->reg2 != reg_g0) {
769 if (debug) { indent(); fprint(codefile, "\t\t! "); dump_cache(codefile); }
773 #define INC_TOS if (c_count >= CACHE_SIZE) flush_part_cache(c_count/2,0,0,0); \
774 tos = &cache[c_count++];
785 if (debug) { indent(); fprint(codefile, "\t\t! "); dump_cache(codefile); }
793 assert(0 <= REG_NUM(i) && REG_NUM(i) < NR_REGS);
799 if (debug) { indent(); fprint(codefile, "\t\t! "); dump_cache(codefile); }
804 void push_double_reg(i)
809 enter("push_double_reg");
815 tos->reg = regnam[rn+1];
822 if (debug) { indent(); fprint(codefile, "\t\t! "); dump_cache(codefile); }
823 leave("push_double_reg");
832 p = Malloc(strlen(s)+1);
837 tos->ext = strcpy(p, s);
839 if (debug) { indent(); fprint(codefile, "\t\t! "); dump_cache(codefile); }
854 if (debug) { indent(); fprint(codefile, "\t\t! %d cache:", x); dump_cache(codefile); }
863 register char *V1, *V3;
864 const_str_t V2, c_str;
866 reg_t tos_reg, tos_reg2;
870 if (debug) { indent(); fprintf(stderr,"pop_reg_as(%s)=...\n", r); }
871 if (c_count == 0) { /* special case */
874 } else if (r >= reg_f0) {
875 if (tos->reg < reg_f0)
885 if (tos->reg2 == reg_g0) {
896 "fadds $V1, $V3, $r";
901 } else if (tos->reg >= reg_f0) {
907 } else if (tos->ext) {
908 assert(tos->reg == reg_g0);
910 sprint(V2, "%d", tos->cst);
921 if (!const13(tos_cst))
923 assert (tos_reg2 == reg_g0);
924 if (tos_reg != reg_g0 || (tos_cst & 0x3ff))
925 tos_reg2= alloc_reg();
931 sprint(c_str, "%d", tos_cst);
932 "sethi %hi($c_str), $tos_reg2";
934 if (tos_reg == reg_g0)
940 if (tos_reg2 != reg_g0)
942 assert (tos_reg != reg_g0);
950 "add $tos_reg, $tos_reg2, $S1";
958 sprint(c_str, "%d", tos_cst);
960 "add $tos_reg, $c_str, $r";
971 void pop_double_reg_as(rdl)
979 enter("pop_double_reg_as");
990 if (type_of_tos() & T_float)
994 if (type_of_tos() & T_float)
1000 if (rl < reg_f0 && rh < reg_f0)
1004 "ldd [%fp+64], $rdl";
1008 else if (rl < reg_f0)
1013 "ld [%fp+64], $rdl";
1017 else if (rh < reg_f0)
1022 "ld [%fp+64], $rdh";
1030 if (rdl == rh && rdh == rl)
1037 if (rdl != rl && rdl != rh)
1043 if (rdh != rh && rdh != rl)
1049 if (rdl != rl && rdl != rh)
1055 assert (rdl == rl && rdh == rh);
1064 leave("pop_double_reg_as");
1075 if (type_of_tos() == T_reg) {
1082 if (debug) { indent(); fprint(codefile, "\t\t! %s cache:", S1); dump_cache(codefile); }
1088 reg_t pop_double(sub_reg) /* pop_double_float actually */
1091 reg_t R1, R2, R3, R4;
1094 enter("pop_double");
1096 R1 = alloc_double(&R2);
1104 if (tos->reg >= reg_f0 && tos[-1].reg >= reg_f0 &&
1105 REG_NUM(tos->reg) == REG_NUM(tos[-1].reg)-1 &&
1106 tos->reg2 == reg_g0 && tos[-1].reg2 == reg_g0)
1110 *sub_reg= tos[-1].reg;
1114 R1= alloc_double(&R2);
1117 if (tos->reg >= reg_f0 || tos[-1].reg >= reg_f0)
1122 /* two normal registers */
1127 "ldd [%fp+64], $R1";
1133 leave("pop_double");
1144 while (c_count && i) {
1148 free_reg(tos->reg2);
1154 sprint(V1, "%d", 4*i);
1158 "set $V1, $reg_tmp";
1159 "add %l0, $reg_tmp, %l0";
1162 if (debug) { indent(); fprint(codefile, "\t\t! %dw cache:",j); dump_cache(codefile); }
1170 for (i = 0; i < NR_REGS; i++)
1171 fprintf(stderr, "%c", reg[i].inuse ? (reg[i].inuse)+48 : '.');
1172 fprintf(stderr, "\n");
1175 void flush_cache() /* flush the cache onto the stack */
1177 enter("flush_cache");
1179 if (debug) { indent(); fprintf(stderr,"flush_cache\n"); }
1181 flush_part_cache(c_count, 0, 0, 0);
1186 leave("flush_cache");
1192 enter("cache_need");
1194 (void) cache_read(n, 0);
1195 assert(c_count >= n);
1196 leave("cache_need");
1199 static int cache_read(n, i)
1208 enter("cache_read");
1209 if (debug) { indent(); fprintf(stderr,"cache_read(%d, %d)\n", n,i); }
1213 old_c_count = cache_read(n, i+1);
1215 sprint(V1, "%d", (old_c_count-1-i) * 4);
1216 "ld [%l0+$V1], $S1";
1218 cache[i].reg2= reg_g0;
1223 sprint(V1, "%d", (old_c_count)*4);
1224 "add $reg_sp, $V1, $reg_sp";
1230 for (j= c_count-1; j>=0; j--)
1231 cache[j+i]= cache[j];
1233 tos= &cache[c_count-1];
1236 leave("cache_read");
1240 static void dump_cache(stream) /* to codefile! */
1245 assert (c_count >= 0);
1246 for (i = c_count -1; i >= 0; i--) {
1248 fprint(stream, "%s", cache[i].ext);
1249 if (cache[i].reg != reg_g0) {
1251 fprint(stream, "+");
1252 fprint(stream, "%s", cache[i].reg);
1253 if (cache[i].reg2 != reg_g0) {
1254 fprint(stream, "+");
1255 fprint(stream, "%s", cache[i].reg2);
1258 if (cache[i].cst || (!cache[i].ext && cache[i].reg == reg_g0)) {
1259 if (cache[i].ext || cache[i].reg != reg_g0)
1260 fprint(stream, "+");
1261 fprint(stream, "%d", cache[i].cst);
1263 fprint(stream, " ");
1265 fprint(stream, "\n");
1266 if (debug) check_cache();
1277 for (i = 0;i < n; i++) {
1289 ext= Malloc(strlen(tos->ext)+1);
1290 strcpy(ext, tos->ext);
1293 soft_alloc_reg(tos->reg);
1294 soft_alloc_reg(tos->reg2);
1297 sprint(i_str, "%d", (n-c_count)*4);
1298 "ld [$reg_sp+$i_str], $a";
1302 if (debug) { indent(); fprint(codefile, "\t\t! "); dump_cache(codefile); }