further comments
[cacao.git] / jit / reg.inc
1 /* jit/reg.inc - register allocator
2
3    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
4    Institut f. Computersprachen, TU Wien
5    R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser, M. Probst,
6    S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich,
7    J. Wenninger
8
9    This file is part of CACAO.
10
11    This program is free software; you can redistribute it and/or
12    modify it under the terms of the GNU General Public License as
13    published by the Free Software Foundation; either version 2, or (at
14    your option) any later version.
15
16    This program is distributed in the hope that it will be useful, but
17    WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19    General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with this program; if not, write to the Free Software
23    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24    02111-1307, USA.
25
26    Contact: cacao@complang.tuwien.ac.at
27
28    Authors: Andreas Krall
29
30    Changes: Stefan Ring
31             Christian Thalinger
32
33    $Id: reg.inc 762 2003-12-13 22:42:03Z twisti $
34
35 */
36
37
38 #include "reg.h"
39 #include "toolbox/memory.h"
40
41
42 varinfo5 *locals;
43 varinfo5 *interfaces;
44
45 static int intregsnum;              /* absolute number of integer registers   */
46 static int floatregsnum;            /* absolute number of float registers     */
47
48 static int intreg_ret;              /* register to return integer values      */
49 int intreg_argnum;                  /* number of integer argument registers   */
50
51 static int floatreg_ret;            /* register for return float values       */
52 int fltreg_argnum;                  /* number of float argument registers     */
53
54
55 static int *argintregs;             /* scratch integer registers              */
56 static int *tmpintregs;             /* scratch integer registers              */
57 static int *savintregs;             /* saved integer registers                */
58 static int *argfltregs;             /* scratch float registers                */
59 static int *tmpfltregs;             /* scratch float registers                */
60 static int *savfltregs;             /* saved float registers                  */
61 static int *freeargintregs;         /* free argument integer registers        */
62 static int *freetmpintregs;         /* free scratch integer registers         */
63 static int *freesavintregs;         /* free saved integer registers           */
64 static int *freeargfltregs;         /* free argument float registers          */
65 static int *freetmpfltregs;         /* free scratch float registers           */
66 static int *freesavfltregs;         /* free saved float registers             */
67
68 #ifdef USETWOREGS
69 static int *secondregs;             /* used for longs in 2 32 bit registers   */
70 #endif
71
72 static int *freemem;                /* free scratch memory                    */
73 static int memuse;                  /* used memory count                      */
74 static int ifmemuse;                /* interface used memory count            */
75 static int maxmemuse;               /* maximal used memory count (spills)     */
76 static int freememtop;              /* free memory count                      */
77
78 static int tmpintregcnt;            /* scratch integer register count         */
79 static int savintregcnt;            /* saved integer register count           */
80 static int tmpfltregcnt;            /* scratch float register count           */
81 static int savfltregcnt;            /* saved float register count             */
82
83 static int iftmpintregcnt;          /* iface scratch integer register count   */
84 static int ifsavintregcnt;          /* iface saved integer register count     */
85 static int iftmpfltregcnt;          /* iface scratch float register count     */
86 static int ifsavfltregcnt;          /* iface saved float register count       */
87
88 static int argintreguse;            /* used argument integer register count   */
89 static int tmpintreguse;            /* used scratch integer register count    */
90 static int savintreguse;            /* used saved integer register count      */
91 static int argfltreguse;            /* used argument float register count     */
92 static int tmpfltreguse;            /* used scratch float register count      */
93 static int savfltreguse;            /* used saved float register count        */
94
95 static int maxargintreguse;         /* max used argument int register count   */
96 static int maxtmpintreguse;         /* max used scratch int register count    */
97 static int maxsavintreguse;         /* max used saved int register count      */
98 static int maxargfltreguse;         /* max used argument float register count */
99 static int maxtmpfltreguse;         /* max used scratch float register count  */
100 static int maxsavfltreguse;         /* max used saved float register count    */
101
102 static int freearginttop;           /* free argument integer register count   */
103 static int freetmpinttop;           /* free scratch integer register count    */
104 static int freesavinttop;           /* free saved integer register count      */
105 static int freeargflttop;           /* free argument float register count     */
106 static int freetmpflttop;           /* free scratch float register count      */
107 static int freesavflttop;           /* free saved float register count        */
108
109 static int savedregs_num;       /* total number of registers to be saved      */
110 int arguments_num;              /* size of parameter field in the stackframe  */
111
112
113
114 /* function reg_init ***********************************************************
115
116         initialises the register-allocator
117         
118 *******************************************************************************/
119
120 void reg_init()
121 {
122         int n;
123
124         if (TYPE_INT != 0 || TYPE_ADR != 4) 
125                 panic("JAVA-Basictypes have been changed");
126
127         /* setup the integer register table */
128         intreg_argnum = 0;
129         tmpintregcnt = 0;
130         savintregcnt = 0;
131
132         for (intregsnum = 0; nregdescint[intregsnum] != REG_END; intregsnum++) {
133                 switch (nregdescint[intregsnum]) {
134                 case REG_SAV: savintregcnt++;
135                         break;
136                 case REG_TMP: tmpintregcnt++;
137                         break;
138                 case REG_ARG: intreg_argnum++;
139                 }
140         }
141
142         argintregs = MNEW(int, intreg_argnum);
143         tmpintregs = MNEW(int, tmpintregcnt);
144         savintregs = MNEW(int, savintregcnt);
145         freeargintregs = MNEW(int, intreg_argnum);
146         freetmpintregs = MNEW(int, tmpintregcnt);
147         freesavintregs = MNEW(int, savintregcnt);
148 #ifdef USETWOREGS
149         secondregs = MNEW(int, intregsnum);
150 #endif
151
152         intreg_argnum = 0;
153         argintreguse = 0;
154         tmpintreguse = 0;
155         savintreguse = 0;
156
157         for (n = 0; n < intregsnum; n++) {
158                 switch (nregdescint[n]) {
159                 case REG_RET: intreg_ret = n; 
160                         break;
161                 case REG_SAV: savintregs[savintreguse++] = n;
162                         break;
163                 case REG_TMP: tmpintregs[tmpintreguse++] = n;
164                         break;
165                 case REG_ARG: argintregs[intreg_argnum++] = n;
166                         argintreguse++;
167                         break;
168                 }
169         }
170
171 #if defined(__I386__)
172         /* 
173            this assumes that we have 3 tmp regs (%ecx, %edx, %ebx) 
174            sort to [ %ebx, %edx, %ecx ]
175          */
176         n = tmpintregs[0];
177         tmpintregs[0] = tmpintregs[2];
178         tmpintregs[2] = n;
179 #endif
180
181 #if defined(__X86_64__)
182         /* 
183          * on x86_64 the argument registers are not in ascending order 
184          * a00 (%rdi) <-> a03 (%rcx) and a01 (%rsi) <-> a02 (%rdx)
185          */
186         n = argintregs[3];
187         argintregs[3] = argintregs[0];
188         argintregs[0] = n;
189
190         n = argintregs[2];
191         argintregs[2] = argintregs[1];
192         argintregs[1] = n;
193 #endif
194                 
195 #ifdef USETWOREGS
196         for (n = 1; n < intreg_argnum; n++)
197                 secondregs[argintregs[n - 1]] = argintregs[n];
198         for (n = 1; n < tmpintregcnt; n++)
199                 secondregs[tmpintregs[n - 1]] = tmpintregs[n];
200         for (n = 1; n < savintregcnt; n++)
201                 secondregs[savintregs[n - 1]] = savintregs[n];
202
203         secondregs[REG_ITMP1] = REG_ITMP2;
204         secondregs[REG_ITMP3] = REG_ITMP2;
205         secondregs[REG_RESULT] = REG_RESULT + 1;
206         secondregs[argintregs[intreg_argnum - 1]] = REG_ITMP3;
207 #endif
208
209         /* setup the float register table */
210         fltreg_argnum = 0;
211         tmpfltregcnt = 0;
212         savfltregcnt = 0;
213
214         for (floatregsnum = 0; nregdescfloat[floatregsnum] != REG_END; floatregsnum++) {
215                 switch (nregdescfloat[floatregsnum]) {
216                 case REG_SAV: savfltregcnt++;
217                         break;
218                 case REG_TMP: tmpfltregcnt++;
219                         break;
220                 case REG_ARG: fltreg_argnum++;
221                         break;
222                 }
223         }
224
225         argfltregs = MNEW(int, fltreg_argnum);
226         tmpfltregs = MNEW(int, tmpfltregcnt);
227         savfltregs = MNEW(int, savfltregcnt);
228         freeargfltregs = MNEW(int, fltreg_argnum);
229         freetmpfltregs = MNEW(int, tmpfltregcnt);
230         freesavfltregs = MNEW(int, savfltregcnt);
231
232         fltreg_argnum = 0;
233         argfltreguse = 0;
234         tmpfltreguse = 0;
235         savfltreguse = 0;
236
237         for (n = 0; n < floatregsnum; n++) {
238                 switch (nregdescfloat[n]) {
239                 case REG_RET:
240                         floatreg_ret = n; 
241                         break;
242                 case REG_SAV: savfltregs[savfltreguse++] = n;
243                         break;
244                 case REG_TMP: tmpfltregs[tmpfltreguse++] = n;
245                         break;
246                 case REG_ARG: argfltregs[fltreg_argnum++] = n;
247                         argfltreguse++;
248                         break;
249                 }
250         }
251 }
252
253
254 void reg_setup()
255 {
256         int i;
257         varinfo5 *v;
258
259         freemem    = DMNEW(int, maxstack);
260         locals     = DMNEW(varinfo5, maxlocals);
261         interfaces = DMNEW(varinfo5, maxstack);
262
263         for (v = locals, i = maxlocals; i > 0; v++, i--) {
264                 v[0][TYPE_INT].type = -1;
265                 v[0][TYPE_LNG].type = -1;
266                 v[0][TYPE_FLT].type = -1;
267                 v[0][TYPE_DBL].type = -1;
268                 v[0][TYPE_ADR].type = -1;
269         }
270
271         for (v = interfaces, i = maxstack; i > 0; v++, i--) {
272                 v[0][TYPE_INT].type = -1;
273                 v[0][TYPE_INT].flags = 0;
274                 v[0][TYPE_LNG].type = -1;
275                 v[0][TYPE_LNG].flags = 0;
276                 v[0][TYPE_FLT].type = -1;
277                 v[0][TYPE_FLT].flags = 0;
278                 v[0][TYPE_DBL].type = -1;
279                 v[0][TYPE_DBL].flags = 0;
280                 v[0][TYPE_ADR].type = -1;
281                 v[0][TYPE_ADR].flags = 0;
282         }
283 }
284
285
286
287 /* function reg_close **********************************************************
288
289         releases all allocated space for registers
290
291 *******************************************************************************/
292
293 void reg_close()
294 {
295         if (argintregs) MFREE(argintregs, int, intreg_argnum);
296         if (argfltregs) MFREE(argfltregs, int, fltreg_argnum);
297         if (tmpintregs) MFREE(tmpintregs, int, tmpintregcnt);
298         if (savintregs) MFREE(savintregs, int, savintregcnt);
299         if (tmpfltregs) MFREE(tmpfltregs, int, tmpfltregcnt);
300         if (savfltregs) MFREE(savfltregs, int, savfltregcnt);
301
302         if (freeargintregs) MFREE(freeargintregs, int, intreg_argnum);
303         if (freeargfltregs) MFREE(freeargfltregs, int, fltreg_argnum);
304         if (freetmpintregs) MFREE(freetmpintregs, int, tmpintregcnt);
305         if (freesavintregs) MFREE(freesavintregs, int, savintregcnt);
306         if (freetmpfltregs) MFREE(freetmpfltregs, int, tmpfltregcnt);
307         if (freesavfltregs) MFREE(freesavfltregs, int, savfltregcnt);
308
309 #ifdef USETWOREGS
310         if (secondregs) MFREE(secondregs, int, intregsnum);
311 #endif
312 }
313
314
315 /* function interface_regalloc *************************************************
316
317         allocates registers for all interface variables
318         
319 *******************************************************************************/
320         
321 void regalloc()
322 {
323 #if defined(__I386__)
324         /* remove %edx from tmpintregs */
325         int origtmpintregcnt = tmpintregcnt;
326         if (method_uses_ecx) tmpintregcnt--;
327         if (method_uses_edx) tmpintregcnt--;
328 #endif
329
330         interface_regalloc();
331         allocate_scratch_registers();
332         local_regalloc();
333
334 #if defined(__I386__)
335         tmpintregcnt = origtmpintregcnt;
336 #endif
337 }
338
339
340 /* function interface_regalloc *************************************************
341
342         allocates registers for all interface variables
343         
344 *******************************************************************************/
345         
346 static void interface_regalloc()
347 {
348         int     s, t, saved;
349         int     intalloc, fltalloc;
350         varinfo *v;
351         int             regsneeded = 0;
352         
353         /* allocate stack space for passing arguments to called methods */
354
355 #ifndef SPECIALMEMUSE
356 #if defined(__X86_64__)
357         /*
358          * XXX: we have a problem here, but allocating a little more stack space
359          *      is better than having a bug
360          */
361         /*      if (arguments_num > (intreg_argnum + fltreg_argnum)) */
362         /*              ifmemuse = arguments_num - (intreg_argnum + fltreg_argnum); */
363         if (arguments_num > fltreg_argnum)
364                 ifmemuse = arguments_num - fltreg_argnum;
365 #else
366         if (arguments_num > intreg_argnum)
367                 ifmemuse = arguments_num - intreg_argnum;
368 #endif
369         else
370                 ifmemuse = 0;
371 #endif
372
373         iftmpintregcnt = tmpintregcnt;
374         ifsavintregcnt = savintregcnt;
375         iftmpfltregcnt = tmpfltregcnt;
376         ifsavfltregcnt = savfltregcnt;
377
378         for (s = 0; s < maxstack; s++) {
379                 intalloc = -1; fltalloc = -1;
380                 saved = (interfaces[s][TYPE_INT].flags | interfaces[s][TYPE_LNG].flags |
381                          interfaces[s][TYPE_FLT].flags | interfaces[s][TYPE_DBL].flags |
382                          interfaces[s][TYPE_ADR].flags) & SAVEDVAR;
383  
384                 for (t = TYPE_INT; t <= TYPE_ADR; t++) {
385                         v = &interfaces[s][t];
386                         if (v->type >= 0) {
387 #ifdef USETWOREGS
388                                 regsneeded = (IS_2_WORD_TYPE(t)) ? 1 : 0;
389 #endif
390                                 if (!saved) {
391                                         if (IS_FLT_DBL_TYPE(t)) {
392                                                 if (fltalloc >= 0) {
393                                                         v->flags |= interfaces[s][fltalloc].flags & INMEMORY;
394                                                         v->regoff = interfaces[s][fltalloc].regoff;
395                                                 }
396                                                 else if (iftmpfltregcnt > 0) {
397                                                         iftmpfltregcnt--;
398                                                         v->regoff = tmpfltregs[iftmpfltregcnt];
399                                                 }
400                                                 else if (ifsavfltregcnt > 0) {
401                                                         ifsavfltregcnt--;
402                                                         v->regoff = savfltregs[ifsavfltregcnt];
403                                                 }
404                                                 else {
405                                                         v->flags |= INMEMORY;
406                                                         v->regoff = ifmemuse;
407                                                         ifmemuse += regsneeded + 1;
408                                                 }
409                                                 fltalloc = t;
410                                         }
411                                         else {
412 #if defined(__I386__)
413                                                 /*
414                                                  * for i386 put all longs in memory
415                                                  */
416                                                 if (IS_2_WORD_TYPE(t)) {
417                                                         v->flags |= INMEMORY;
418                                                         v->regoff = ifmemuse++;
419                                                 } else {
420 #endif
421                                                         if (intalloc >= 0) {
422                                                                 v->flags |= interfaces[s][intalloc].flags & INMEMORY;
423                                                                 v->regoff = interfaces[s][intalloc].regoff;
424                                                         }
425                                                         else if (iftmpintregcnt > regsneeded) {
426                                                                 iftmpintregcnt -= regsneeded + 1;
427                                                                 v->regoff = tmpintregs[iftmpintregcnt];
428                                                         }
429                                                         else if (ifsavintregcnt > regsneeded) {
430                                                                 ifsavintregcnt -= regsneeded + 1;
431                                                                 v->regoff = savintregs[ifsavintregcnt];
432                                                         }
433                                                         else {
434                                                                 v->flags |= INMEMORY;
435                                                                 v->regoff = ifmemuse;
436                                                                 ifmemuse += regsneeded + 1;
437                                                         }
438 #if defined(__I386__)
439                                                 }
440 #endif
441                                                 intalloc = t;
442                                         }
443                                 }
444                                 else {
445                                         if (IS_FLT_DBL_TYPE(t)) {
446                                                 if (fltalloc >= 0) {
447                                                         v->flags |= interfaces[s][fltalloc].flags & INMEMORY;
448                                                         v->regoff = interfaces[s][fltalloc].regoff;
449                                                 }
450                                                 else if (ifsavfltregcnt > 0) {
451                                                         ifsavfltregcnt--;
452                                                         v->regoff = savfltregs[ifsavfltregcnt];
453                                                 }
454                                                 else {
455                                                         v->flags |= INMEMORY;
456                                                         v->regoff = ifmemuse;
457                                                         ifmemuse += regsneeded + 1;
458                                                 }
459                                                 fltalloc = t;
460                                         }
461                                         else {
462 #if defined(__I386__)
463                                                 /*
464                                                  * for i386 put all longs in memory
465                                                  */
466                                                 if (IS_2_WORD_TYPE(t)) {
467                                                         v->flags |= INMEMORY;
468                                                         v->regoff = ifmemuse++;
469                                                 } else {
470 #endif
471                                                         if (intalloc >= 0) {
472                                                                 v->flags |= interfaces[s][intalloc].flags & INMEMORY;
473                                                                 v->regoff = interfaces[s][intalloc].regoff;
474                                                         }
475                                                         else if (ifsavintregcnt > regsneeded) {
476                                                                 ifsavintregcnt -= regsneeded + 1;
477                                                                 v->regoff = savintregs[ifsavintregcnt];
478                                                         }
479                                                         else {
480                                                                 v->flags |= INMEMORY;
481                                                                 v->regoff = ifmemuse;
482                                                                 ifmemuse += regsneeded + 1;
483                                                         }
484 #if defined(__I386__)
485                                                 }
486 #endif
487                                                 intalloc = t;
488                                         }
489                                 }
490                         } /* if (type >= 0) */
491                 } /* for t */
492         } /* for s */
493
494         maxmemuse = ifmemuse;
495         maxtmpintreguse = iftmpintregcnt;
496         maxsavintreguse = ifsavintregcnt;
497         maxtmpfltreguse = iftmpfltregcnt;
498         maxsavfltreguse = ifsavfltregcnt;
499 }
500
501
502
503 /* function local_regalloc *****************************************************
504
505         allocates registers for all local variables
506         
507 *******************************************************************************/
508         
509 static void local_regalloc()
510 {
511         int      s, t, tt;
512         int      intalloc, fltalloc;
513         varinfo *v;
514         int      regsneeded = 0;
515         int typeloop[] = { TYPE_LNG, TYPE_DBL, TYPE_INT, TYPE_FLT, TYPE_ADR };
516         
517         if (isleafmethod) {
518                 int arg, doublewordarg, iargcnt, fargcnt;
519
520                 arg = 0, iargcnt = 0, fargcnt = 0;
521                 doublewordarg = 0;
522                 for (s = 0; s < maxlocals; s++) {
523                         intalloc = -1; fltalloc = -1;
524                         for (tt = 0; tt <= 4; tt++) {
525                                 t = typeloop[tt];
526                                 v = &locals[s][t];
527
528                                 if (v->type >= 0) {
529 #ifdef USETWOREGS
530                                         regsneeded = (IS_2_WORD_TYPE(t)) ? 1 : 0;
531 #endif
532                                         if (IS_FLT_DBL_TYPE(t)) {
533 #if !defined(CONSECUTIVE_FLOATARGS)
534                                                 fargcnt = arg;
535 #endif
536                                                 if (fltalloc >= 0) {
537                                                         v->flags = locals[s][fltalloc].flags;
538                                                         v->regoff = locals[s][fltalloc].regoff;
539                                                 }
540                                                 else if (!doublewordarg && (arg < mparamcount)
541                                                                  && (fargcnt < fltreg_argnum)) {
542                                                         v->flags = 0;
543                                                         v->regoff = argfltregs[fargcnt];
544                                                 }
545                                                 else if (maxtmpfltreguse > 0) {
546                                                         maxtmpfltreguse--;
547                                                         v->flags = 0;
548                                                         v->regoff = tmpfltregs[maxtmpfltreguse];
549                                                 }
550                                                 else if (maxsavfltreguse > 0) {
551                                                         maxsavfltreguse--;
552                                                         v->flags = 0;
553                                                         v->regoff = savfltregs[maxsavfltreguse];
554                                                 }
555                                                 else {
556                                                         v->flags = INMEMORY;
557                                                         v->regoff = maxmemuse;
558                                                         maxmemuse += regsneeded + 1;
559                                                 }
560                                                 fltalloc = t;
561
562                                         } else {
563                                                 int regtouse;
564 #if defined(__I386__)
565                                                 /*
566                                                  * for i386 put all longs in memory
567                                                  */
568                                                 if (IS_2_WORD_TYPE(t)) {
569                                                         v->flags = INMEMORY;
570                                                         v->regoff = maxmemuse++;
571                                                 } else {
572 #endif
573 #if !defined(CONSECUTIVE_INTARGS)
574                                                         iargcnt = arg;
575 #endif
576                                                         if (intalloc >= 0) {
577                                                                 v->flags = locals[s][intalloc].flags;
578                                                                 v->regoff = locals[s][intalloc].regoff;
579                                                         }
580                                                         else if (!doublewordarg && (arg < mparamcount)
581 #ifndef USETWOREGS
582                                                                          && ((regtouse = iargcnt) < intreg_argnum)
583 #else
584                                                                          && ((regtouse = s) < intreg_argnum - regsneeded)
585 #endif
586                                                                          ) {
587                                                                 v->flags = 0;
588                                                                 v->regoff = argintregs[regtouse];
589                                                         }
590                                                         else if (maxtmpintreguse > regsneeded) {
591                                                                 maxtmpintreguse -= regsneeded + 1;
592                                                                 v->flags = 0;
593                                                                 v->regoff = tmpintregs[maxtmpintreguse];
594                                                         }
595                                                         else if (maxsavintreguse > regsneeded) {
596                                                                 maxsavintreguse -= regsneeded + 1;
597                                                                 v->flags = 0;
598                                                                 v->regoff = savintregs[maxsavintreguse];
599                                                         }
600                                                         /*
601                                                          * use unused argument registers as local registers
602                                                          */
603                                                         else if (!doublewordarg && (arg >= mparamcount)
604                                                                          && (iargcnt < intreg_argnum)) {
605                                                                 v->flags = 0;
606                                                                 v->regoff = argintregs[iargcnt];
607                                                                 iargcnt++;
608                                                                 arg++;
609                                                         }
610                                                         else {
611                                                                 v->flags = INMEMORY;
612                                                                 v->regoff = maxmemuse;
613                                                                 maxmemuse += regsneeded + 1;
614                                                         }
615 #if defined(__I386__)
616                                                 }
617 #endif
618                                                 intalloc = t;
619                                         }
620                                 }
621                         }
622                         if (arg < mparamcount) {
623                                 if (doublewordarg) {
624                                         doublewordarg = 0;
625                                         /* what type was the double arg? */
626                                         if (IS_FLT_DBL_TYPE(mparamtypes[arg])) {
627                                                 fargcnt++;
628
629                                         } else {
630                                                 iargcnt++;
631                                         }
632                                         arg++;
633
634                                 } else if (IS_2_WORD_TYPE(mparamtypes[arg])) {
635                                         doublewordarg = 1;
636
637                                 } else {
638                                         if (IS_FLT_DBL_TYPE(mparamtypes[arg])) {
639                                                 fargcnt++;
640
641                                         } else {
642                                                 iargcnt++;
643                                         }
644                                         arg++;
645                                 }
646                         }
647                 }
648                 return;
649         }
650
651         for (s = 0; s < maxlocals; s++) {
652                 intalloc = -1; fltalloc = -1;
653                 for (tt=0; tt<=4; tt++) {
654                         t = typeloop[tt];
655                         v = &locals[s][t];
656                         if (v->type >= 0) {
657 #ifdef USETWOREGS
658                                 regsneeded = (IS_2_WORD_TYPE(t)) ? 1 : 0;
659 #endif
660                                 if (IS_FLT_DBL_TYPE(t)) {
661                                         if (fltalloc >= 0) {
662                                                 v->flags = locals[s][fltalloc].flags;
663                                                 v->regoff = locals[s][fltalloc].regoff;
664                                         }
665                                         else if (maxsavfltreguse > 0) {
666                                                 maxsavfltreguse--;
667                                                 v->flags = 0;
668                                                 v->regoff = savfltregs[maxsavfltreguse];
669                                         }
670                                         else {
671                                                 v->flags = INMEMORY;
672                                                 v->regoff = maxmemuse;
673                                                 maxmemuse += regsneeded + 1;
674                                         }
675                                         fltalloc = t;
676                                 }
677                                 else {
678 #if defined(__I386__)
679                                         /*
680                                          * for i386 put all longs in memory
681                                          */
682                                         if (IS_2_WORD_TYPE(t)) {
683                                                 v->flags = INMEMORY;
684                                                 v->regoff = maxmemuse++;
685                                         } else {
686 #endif
687                                                 if (intalloc >= 0) {
688                                                         v->flags = locals[s][intalloc].flags;
689                                                         v->regoff = locals[s][intalloc].regoff;
690                                                 }
691                                                 else if (maxsavintreguse > regsneeded) {
692                                                         maxsavintreguse -= regsneeded+1;
693                                                         v->flags = 0;
694                                                         v->regoff = savintregs[maxsavintreguse];
695                                                 }
696                                                 else {
697                                                         v->flags = INMEMORY;
698                                                         v->regoff = maxmemuse;
699                                                         maxmemuse += regsneeded + 1;
700                                                 }
701 #if defined(__I386__)
702                                         }
703 #endif
704                                         intalloc = t;
705                                 }
706                         }
707                 }
708         }
709 }
710
711
712
713 static void reg_init_temp()
714 {
715         freememtop = 0;
716         memuse = ifmemuse;
717
718         freearginttop = 0;
719         freetmpinttop = 0;
720         freesavinttop = 0;
721         freeargflttop = 0;
722         freetmpflttop = 0;
723         freesavflttop = 0;
724
725         tmpintreguse = iftmpintregcnt;
726         savintreguse = ifsavintregcnt;
727         tmpfltreguse = iftmpfltregcnt;
728         savfltreguse = ifsavfltregcnt;
729
730         /*
731          * all argument registers are available
732          */
733         argintreguse = intreg_argnum;
734         argfltreguse = fltreg_argnum;
735 }
736
737
738
739 #define reg_new_temp(s) if (s->varkind == TEMPVAR) reg_new_temp_func(s)
740
741 static void reg_new_temp_func(stackptr s)
742 {
743         int regsneeded = 0;
744
745         /* Try to allocate a saved register if there is no temporary one available.   */
746         /* This is what happens during the second run.                                */
747         int tryagain = (s->flags & SAVEDVAR) ? 1 : 2;
748
749 #ifdef USETWOREGS
750         regsneeded = (IS_2_WORD_TYPE(s->type)) ? 1 : 0;
751 #endif
752
753         for(; tryagain; --tryagain) {
754                 if (tryagain == 1) {
755                         if (!(s->flags & SAVEDVAR))
756                                 s->flags |= SAVEDTMP;
757                         if (IS_FLT_DBL_TYPE(s->type)) {
758                                 if (freesavflttop > 0) {
759                                         freesavflttop--;
760                                         s->regoff = freesavfltregs[freesavflttop];
761                                         return;
762                                 }
763                                 else if (savfltreguse > 0) {
764                                         savfltreguse--;
765                                         if (savfltreguse < maxsavfltreguse)
766                                                 maxsavfltreguse = savfltreguse;
767                                         s->regoff = savfltregs[savfltreguse];
768                                         return;
769                                 }
770                         }
771                         else {
772 #if defined(__I386__)
773                                 /*
774                                  * for i386 put all longs in memory
775                                  */
776                                 if (!IS_2_WORD_TYPE(s->type)) {
777 #endif
778                                         if (freesavinttop > regsneeded) {
779                                                 freesavinttop -= regsneeded + 1;
780                                                 s->regoff = freesavintregs[freesavinttop];
781                                                 return;
782                                         }
783                                         else if (savintreguse > regsneeded) {
784                                                 savintreguse -= regsneeded + 1;
785                                                 if (savintreguse < maxsavintreguse)
786                                                         maxsavintreguse = savintreguse;
787                                                 s->regoff = savintregs[savintreguse];
788                                                 return;
789                                         }
790 #if defined(__I386__)
791                                 }
792 #endif
793                         }
794                 }
795                 else {
796                         if (IS_FLT_DBL_TYPE(s->type)) {
797                                 if (freetmpflttop > 0) {
798                                         freetmpflttop--;
799                                         s->regoff = freetmpfltregs[freetmpflttop];
800                                         return;
801                                 }
802                                 else if (tmpfltreguse > 0) {
803                                         tmpfltreguse--;
804                                         if (tmpfltreguse < maxtmpfltreguse)
805                                                 maxtmpfltreguse = tmpfltreguse;
806                                         s->regoff = tmpfltregs[tmpfltreguse];
807                                         return;
808                                 }
809                         }
810                         else {
811 #if defined(__I386__)
812                                 /*
813                                  * for i386 put all longs in memory
814                                  */
815                                 if (!IS_2_WORD_TYPE(s->type)) {
816 #endif
817                                         if (freetmpinttop > regsneeded) {
818                                                 freetmpinttop -= regsneeded + 1;
819                                                 s->regoff = freetmpintregs[freetmpinttop];
820                                                 return;
821                                         }
822                                         else if (tmpintreguse > regsneeded) {
823                                                 tmpintreguse -= regsneeded + 1;
824                                                 if (tmpintreguse < maxtmpintreguse)
825                                                         maxtmpintreguse = tmpintreguse;
826                                                 s->regoff = tmpintregs[tmpintreguse];
827                                                 return;
828                                         }
829 #if defined(__I386__)
830                                 }
831 #endif
832                         }
833                 }
834         }
835
836         if (freememtop > regsneeded) {
837                 freememtop -= regsneeded + 1;
838                 s->regoff = freemem[freememtop];
839         }
840         else {
841                 s->regoff = memuse;
842                 memuse += regsneeded + 1;
843                 if (memuse > maxmemuse)
844                         maxmemuse = memuse;
845         }
846         s->flags |= INMEMORY;
847 }
848
849
850
851 #define reg_free_temp(s) if (s->varkind == TEMPVAR) reg_free_temp_func(s)
852
853 static void reg_free_temp_func(stackptr s)
854 {
855         int regsneeded = 0;
856
857 #ifdef USETWOREGS
858         regsneeded = (IS_2_WORD_TYPE(s->type)) ? 1 : 0;
859 #endif
860
861         if (s->flags & INMEMORY) {
862                 freemem[freememtop] = s->regoff;
863                 if (regsneeded)
864                         freemem[freememtop + 1] = s->regoff + 1;
865                 freememtop += regsneeded + 1;
866         }
867         else if (IS_FLT_DBL_TYPE(s->type)) {
868                 if (s->flags & (SAVEDVAR | SAVEDTMP)) {
869                         s->flags &= ~SAVEDTMP;
870                         freesavfltregs[freesavflttop++] = s->regoff;
871                 } else
872                         freetmpfltregs[freetmpflttop++] = s->regoff;
873         }
874         else {
875                 if (s->flags & (SAVEDVAR | SAVEDTMP)) {
876                         s->flags &= ~SAVEDTMP;
877                         freesavintregs[freesavinttop] = s->regoff;
878 #ifdef USETWOREGS
879                         if (regsneeded)
880                                 freesavintregs[freesavinttop + 1] = secondregs[s->regoff];
881 #endif
882                         freesavinttop += regsneeded + 1;
883                 } else {
884                         freetmpintregs[freetmpinttop] = s->regoff;
885 #ifdef USETWOREGS
886                         if (regsneeded)
887                                 freetmpintregs[freetmpinttop + 1] = secondregs[s->regoff];
888 #endif
889                         freetmpinttop += regsneeded + 1;
890                 }
891         }
892 }
893
894
895
896 static void allocate_scratch_registers()
897 {
898         int opcode;
899         int i;
900         int len;
901         stackptr    src;
902         stackptr    dst;
903         instruction *iptr;
904         basicblock  *bptr;
905
906         /* b_count = block_count; */
907
908         bptr = block;
909         while (bptr != NULL) {
910
911                 if (bptr->flags >= BBREACHED) {
912                         dst = bptr->instack;
913                         reg_init_temp();
914                         iptr = bptr->iinstr;
915                         len = bptr->icount;
916   
917                         while (--len >= 0)  {
918                                 src = dst;
919                                 dst = iptr->dst;
920                                 opcode = iptr->opc;
921
922                                 switch (opcode) {
923
924                                         /* pop 0 push 0 */
925
926                                 case ICMD_NOP:
927                                 case ICMD_ELSE_ICONST:
928                                 case ICMD_CHECKASIZE:
929                                 case ICMD_IINC:
930                                 case ICMD_JSR:
931                                 case ICMD_RET:
932                                 case ICMD_RETURN:
933                                 case ICMD_GOTO:
934                                         break;
935
936                                         /* pop 0 push 1 const */
937                                         
938                                 case ICMD_ICONST:
939                                 case ICMD_LCONST:
940                                 case ICMD_FCONST:
941                                 case ICMD_DCONST:
942                                 case ICMD_ACONST:
943
944                                         /* pop 0 push 1 load */
945                                         
946                                 case ICMD_ILOAD:
947                                 case ICMD_LLOAD:
948                                 case ICMD_FLOAD:
949                                 case ICMD_DLOAD:
950                                 case ICMD_ALOAD:
951                                         reg_new_temp(dst);
952                                         break;
953
954                                         /* pop 2 push 1 */
955
956                                 case ICMD_IALOAD:
957                                 case ICMD_LALOAD:
958                                 case ICMD_FALOAD:
959                                 case ICMD_DALOAD:
960                                 case ICMD_AALOAD:
961
962                                 case ICMD_BALOAD:
963                                 case ICMD_CALOAD:
964                                 case ICMD_SALOAD:
965
966                                         reg_free_temp(src);
967                                         reg_free_temp(src->prev);
968                                         reg_new_temp(dst);
969                                         break;
970
971                                         /* pop 3 push 0 */
972
973                                 case ICMD_IASTORE:
974                                 case ICMD_LASTORE:
975                                 case ICMD_FASTORE:
976                                 case ICMD_DASTORE:
977                                 case ICMD_AASTORE:
978
979                                 case ICMD_BASTORE:
980                                 case ICMD_CASTORE:
981                                 case ICMD_SASTORE:
982
983                                         reg_free_temp(src);
984                                         reg_free_temp(src->prev);
985                                         reg_free_temp(src->prev->prev);
986                                         break;
987
988                                         /* pop 1 push 0 store */
989
990                                 case ICMD_ISTORE:
991                                 case ICMD_LSTORE:
992                                 case ICMD_FSTORE:
993                                 case ICMD_DSTORE:
994                                 case ICMD_ASTORE:
995
996                                         /* pop 1 push 0 */
997
998                                 case ICMD_POP:
999
1000                                 case ICMD_IRETURN:
1001                                 case ICMD_LRETURN:
1002                                 case ICMD_FRETURN:
1003                                 case ICMD_DRETURN:
1004                                 case ICMD_ARETURN:
1005
1006                                 case ICMD_ATHROW:
1007
1008                                 case ICMD_PUTSTATIC:
1009
1010                                         /* pop 1 push 0 branch */
1011
1012                                 case ICMD_IFNULL:
1013                                 case ICMD_IFNONNULL:
1014
1015                                 case ICMD_IFEQ:
1016                                 case ICMD_IFNE:
1017                                 case ICMD_IFLT:
1018                                 case ICMD_IFGE:
1019                                 case ICMD_IFGT:
1020                                 case ICMD_IFLE:
1021
1022                                 case ICMD_IF_LEQ:
1023                                 case ICMD_IF_LNE:
1024                                 case ICMD_IF_LLT:
1025                                 case ICMD_IF_LGE:
1026                                 case ICMD_IF_LGT:
1027                                 case ICMD_IF_LLE:
1028
1029                                         /* pop 1 push 0 table branch */
1030
1031                                 case ICMD_TABLESWITCH:
1032                                 case ICMD_LOOKUPSWITCH:
1033
1034                                 case ICMD_NULLCHECKPOP:
1035                                 case ICMD_MONITORENTER:
1036                                 case ICMD_MONITOREXIT:
1037                                         reg_free_temp(src);
1038                                         break;
1039
1040                                         /* pop 2 push 0 branch */
1041
1042                                 case ICMD_IF_ICMPEQ:
1043                                 case ICMD_IF_ICMPNE:
1044                                 case ICMD_IF_ICMPLT:
1045                                 case ICMD_IF_ICMPGE:
1046                                 case ICMD_IF_ICMPGT:
1047                                 case ICMD_IF_ICMPLE:
1048
1049                                 case ICMD_IF_LCMPEQ:
1050                                 case ICMD_IF_LCMPNE:
1051                                 case ICMD_IF_LCMPLT:
1052                                 case ICMD_IF_LCMPGE:
1053                                 case ICMD_IF_LCMPGT:
1054                                 case ICMD_IF_LCMPLE:
1055
1056                                 case ICMD_IF_ACMPEQ:
1057                                 case ICMD_IF_ACMPNE:
1058
1059                                         /* pop 2 push 0 */
1060
1061                                 case ICMD_POP2:
1062
1063                                 case ICMD_PUTFIELD:
1064                                         reg_free_temp(src);
1065                                         reg_free_temp(src->prev);
1066                                         break;
1067
1068                                         /* pop 0 push 1 dup */
1069                                         
1070                                 case ICMD_DUP:
1071                                         reg_new_temp(dst);
1072                                         break;
1073
1074                                         /* pop 0 push 2 dup */
1075                                         
1076                                 case ICMD_DUP2:
1077                                         reg_new_temp(dst->prev);
1078                                         reg_new_temp(dst);
1079                                         break;
1080
1081                                         /* pop 2 push 3 dup */
1082                                         
1083                                 case ICMD_DUP_X1:
1084                                         reg_new_temp(dst->prev->prev);
1085                                         reg_new_temp(dst->prev);
1086                                         reg_new_temp(dst);
1087                                         reg_free_temp(src);
1088                                         reg_free_temp(src->prev);
1089                                         break;
1090
1091                                         /* pop 3 push 4 dup */
1092                                         
1093                                 case ICMD_DUP_X2:
1094                                         reg_new_temp(dst->prev->prev->prev);
1095                                         reg_new_temp(dst->prev->prev);
1096                                         reg_new_temp(dst->prev);
1097                                         reg_new_temp(dst);
1098                                         reg_free_temp(src);
1099                                         reg_free_temp(src->prev);
1100                                         reg_free_temp(src->prev->prev);
1101                                         break;
1102
1103                                         /* pop 3 push 5 dup */
1104                                         
1105                                 case ICMD_DUP2_X1:
1106                                         reg_new_temp(dst->prev->prev->prev->prev);
1107                                         reg_new_temp(dst->prev->prev->prev);
1108                                         reg_new_temp(dst->prev->prev);
1109                                         reg_new_temp(dst->prev);
1110                                         reg_new_temp(dst);
1111                                         reg_free_temp(src);
1112                                         reg_free_temp(src->prev);
1113                                         reg_free_temp(src->prev->prev);
1114                                         break;
1115
1116                                         /* pop 4 push 6 dup */
1117                                         
1118                                 case ICMD_DUP2_X2:
1119                                         reg_new_temp(dst->prev->prev->prev->prev->prev);
1120                                         reg_new_temp(dst->prev->prev->prev->prev);
1121                                         reg_new_temp(dst->prev->prev->prev);
1122                                         reg_new_temp(dst->prev->prev);
1123                                         reg_new_temp(dst->prev);
1124                                         reg_new_temp(dst);
1125                                         reg_free_temp(src);
1126                                         reg_free_temp(src->prev);
1127                                         reg_free_temp(src->prev->prev);
1128                                         reg_free_temp(src->prev->prev->prev);
1129                                         break;
1130
1131                                         /* pop 2 push 2 swap */
1132                                         
1133                                 case ICMD_SWAP:
1134                                         reg_new_temp(dst->prev);
1135                                         reg_new_temp(dst);
1136                                         reg_free_temp(src);
1137                                         reg_free_temp(src->prev);
1138                                         break;
1139
1140                                         /* pop 2 push 1 */
1141                                         
1142                                 case ICMD_IADD:
1143                                 case ICMD_ISUB:
1144                                 case ICMD_IMUL:
1145                                 case ICMD_IDIV:
1146                                 case ICMD_IREM:
1147
1148                                 case ICMD_ISHL:
1149                                 case ICMD_ISHR:
1150                                 case ICMD_IUSHR:
1151                                 case ICMD_IAND:
1152                                 case ICMD_IOR:
1153                                 case ICMD_IXOR:
1154
1155                                 case ICMD_LADD:
1156                                 case ICMD_LSUB:
1157                                 case ICMD_LMUL:
1158                                 case ICMD_LDIV:
1159                                 case ICMD_LREM:
1160
1161                                 case ICMD_LOR:
1162                                 case ICMD_LAND:
1163                                 case ICMD_LXOR:
1164
1165                                 case ICMD_LSHL:
1166                                 case ICMD_LSHR:
1167                                 case ICMD_LUSHR:
1168
1169                                 case ICMD_FADD:
1170                                 case ICMD_FSUB:
1171                                 case ICMD_FMUL:
1172                                 case ICMD_FDIV:
1173                                 case ICMD_FREM:
1174
1175                                 case ICMD_DADD:
1176                                 case ICMD_DSUB:
1177                                 case ICMD_DMUL:
1178                                 case ICMD_DDIV:
1179                                 case ICMD_DREM:
1180
1181                                 case ICMD_LCMP:
1182                                 case ICMD_FCMPL:
1183                                 case ICMD_FCMPG:
1184                                 case ICMD_DCMPL:
1185                                 case ICMD_DCMPG:
1186                                         reg_free_temp(src);
1187                                         reg_free_temp(src->prev);
1188                                         reg_new_temp(dst);
1189                                         break;
1190
1191                                         /* pop 1 push 1 */
1192                                         
1193                                 case ICMD_IADDCONST:
1194                                 case ICMD_ISUBCONST:
1195                                 case ICMD_IMULCONST:
1196                                 case ICMD_IDIVPOW2:
1197                                 case ICMD_IREMPOW2:
1198                                 case ICMD_IREM0X10001:
1199                                 case ICMD_IANDCONST:
1200                                 case ICMD_IORCONST:
1201                                 case ICMD_IXORCONST:
1202                                 case ICMD_ISHLCONST:
1203                                 case ICMD_ISHRCONST:
1204                                 case ICMD_IUSHRCONST:
1205
1206                                 case ICMD_LADDCONST:
1207                                 case ICMD_LSUBCONST:
1208                                 case ICMD_LMULCONST:
1209                                 case ICMD_LDIVPOW2:
1210                                 case ICMD_LREMPOW2:
1211                                 case ICMD_LREM0X10001:
1212                                 case ICMD_LANDCONST:
1213                                 case ICMD_LORCONST:
1214                                 case ICMD_LXORCONST:
1215                                 case ICMD_LSHLCONST:
1216                                 case ICMD_LSHRCONST:
1217                                 case ICMD_LUSHRCONST:
1218
1219                                 case ICMD_IFEQ_ICONST:
1220                                 case ICMD_IFNE_ICONST:
1221                                 case ICMD_IFLT_ICONST:
1222                                 case ICMD_IFGE_ICONST:
1223                                 case ICMD_IFGT_ICONST:
1224                                 case ICMD_IFLE_ICONST:
1225
1226                                 case ICMD_INEG:
1227                                 case ICMD_INT2BYTE:
1228                                 case ICMD_INT2CHAR:
1229                                 case ICMD_INT2SHORT:
1230                                 case ICMD_LNEG:
1231                                 case ICMD_FNEG:
1232                                 case ICMD_DNEG:
1233
1234                                 case ICMD_I2L:
1235                                 case ICMD_I2F:
1236                                 case ICMD_I2D:
1237                                 case ICMD_L2I:
1238                                 case ICMD_L2F:
1239                                 case ICMD_L2D:
1240                                 case ICMD_F2I:
1241                                 case ICMD_F2L:
1242                                 case ICMD_F2D:
1243                                 case ICMD_D2I:
1244                                 case ICMD_D2L:
1245                                 case ICMD_D2F:
1246
1247                                 case ICMD_CHECKCAST:
1248
1249                                 case ICMD_ARRAYLENGTH:
1250                                 case ICMD_INSTANCEOF:
1251
1252                                 case ICMD_NEWARRAY:
1253                                 case ICMD_ANEWARRAY:
1254
1255                                 case ICMD_GETFIELD:
1256                                         reg_free_temp(src);
1257                                         reg_new_temp(dst);
1258                                         break;
1259
1260                                         /* pop 0 push 1 */
1261                                         
1262                                 case ICMD_GETSTATIC:
1263
1264                                 case ICMD_NEW:
1265
1266                                         reg_new_temp(dst);
1267                                         break;
1268
1269                                         /* pop many push any */
1270                                         
1271                                 case ICMD_INVOKEVIRTUAL:
1272                                 case ICMD_INVOKESPECIAL:
1273                                 case ICMD_INVOKESTATIC:
1274                                 case ICMD_INVOKEINTERFACE:
1275                                         {
1276                                                 i = iptr->op1;
1277                                                 while (--i >= 0) {
1278                                                         reg_free_temp(src);
1279                                                         src = src->prev;
1280                                                 }
1281                                                 if (((methodinfo*)iptr->val.a)->returntype != TYPE_VOID)
1282                                                         reg_new_temp(dst);
1283                                                 break;
1284                                         }
1285
1286                                 case ICMD_BUILTIN3:
1287                                         reg_free_temp(src);
1288                                         src = src->prev;
1289                                 case ICMD_BUILTIN2:
1290                                         reg_free_temp(src);
1291                                         src = src->prev;
1292                                 case ICMD_BUILTIN1:
1293                                         reg_free_temp(src);
1294                                         src = src->prev;
1295                                         if (iptr->op1 != TYPE_VOID)
1296                                                 reg_new_temp(dst);
1297                                         break;
1298
1299                                 case ICMD_MULTIANEWARRAY:
1300                                         i = iptr->op1;
1301                                         while (--i >= 0) {
1302                                                 reg_free_temp(src);
1303                                                 src = src->prev;
1304                                         }
1305                                         reg_new_temp(dst);
1306                                         break;
1307
1308                                 default:
1309                                         printf("ICMD %d at %d\n", iptr->opc, (int)(iptr-instr));
1310                                         panic("Missing ICMD code during register allocation");
1311                                 } /* switch */
1312                                 iptr++;
1313                         } /* while instructions */
1314                 } /* if */
1315                 bptr = bptr->next;
1316         } /* while blocks */
1317 }
1318
1319
1320 /*
1321  * These are local overrides for various environment variables in Emacs.
1322  * Please do not remove this and leave it at the end of the file, where
1323  * Emacs will automagically detect them.
1324  * ---------------------------------------------------------------------
1325  * Local variables:
1326  * mode: c
1327  * indent-tabs-mode: t
1328  * c-basic-offset: 4
1329  * tab-width: 4
1330  * End:
1331  */