Merged with tip.
[cacao.git] / src / vm / jit / allocator / simplereg.c
1 /* src/vm/jit/allocator/simplereg.c - register allocator
2
3    Copyright (C) 1996-2005, 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21    02111-1307, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include <assert.h>
29 #include <stdint.h>
30
31 #include "vm/types.h"
32
33 #include "arch.h"
34 #include "md-abi.h"
35
36 #include "mm/memory.h"
37
38 #include "vm/builtin.h"
39 #include "vm/exceptions.h"
40 #include "vm/resolve.h"
41 #include "vm/stringlocal.h"
42
43 #include "vm/jit/abi.h"
44 #include "vm/jit/reg.h"
45 #include "vm/jit/show.h"
46 #include "vm/jit/allocator/simplereg.h"
47
48 #include "vmcore/method.h"
49 #include "vmcore/options.h"
50
51
52 #if 0
53 #    define LOG(args) printf args
54 #else
55 #    define LOG(args)
56 #endif
57
58
59 /* function prototypes for this file ******************************************/
60
61 static void simplereg_allocate_interfaces(jitdata *jd);
62 static void simplereg_allocate_locals(jitdata *jd);
63 static void simplereg_allocate_temporaries(jitdata *jd);
64
65
66 /* size of a stackslot used by the internal ABI */
67
68 #if defined(HAS_4BYTE_STACKSLOT)
69 # define SIZE_OF_STACKSLOT 4
70 #else 
71 # define SIZE_OF_STACKSLOT 8
72 #endif
73
74
75 /* total number of registers */
76
77 #if defined(HAS_ADDRESS_REGISTER_FILE)
78 #define TOTAL_REG_CNT  (INT_REG_CNT + FLT_REG_CNT + ADR_REG_CNT)
79 #else
80 #define TOTAL_REG_CNT  (INT_REG_CNT + FLT_REG_CNT)
81 #endif
82
83
84 /* macros for handling register stacks ****************************************/
85
86 #define AVAIL_FRONT(cnt, limit)   ((cnt) < (limit))
87 #define AVAIL_BACK(cnt)           ((cnt) > 0)
88
89 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
90 #define AVAIL_FRONT_INT(cnt, limit)   ((cnt) < (limit) - intregsneeded)
91 #define AVAIL_BACK_INT(cnt)           ((cnt) > intregsneeded)
92 #else
93 #define AVAIL_FRONT_INT(cnt, limit)   AVAIL_FRONT(cnt, limit)
94 #define AVAIL_BACK_INT(cnt)           AVAIL_BACK(cnt)
95 #endif
96
97 #define POP_FRONT(stk, cnt, reg)   do {  reg = stk[cnt++];    } while (0)
98 #define POP_BACK(stk, cnt, reg)    do {  reg = stk[--cnt];    } while (0)
99 #define PUSH_FRONT(stk, cnt, reg)  do {  stk[--cnt] = (reg);  } while (0)
100 #define PUSH_BACK(stk, cnt, reg)   do {  stk[cnt++] = (reg);  } while (0)
101
102 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
103 #define POP_FRONT_INT(stk, cnt, reg)                                 \
104     do {                                                             \
105         if (intregsneeded) {                                         \
106             reg = PACK_REGS(stk[cnt], stk[cnt+1]);                   \
107             cnt += 2;                                                \
108         }                                                            \
109         else                                                         \
110             POP_FRONT(stk, cnt, reg);                                \
111     } while (0)
112 #else
113 #define POP_FRONT_INT(stk, cnt, reg)  POP_FRONT(stk, cnt, reg)
114 #endif
115
116 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
117 #define POP_BACK_INT(stk, cnt, reg)                                  \
118     do {                                                             \
119         if (intregsneeded) {                                         \
120             cnt -= 2;                                                \
121             reg = PACK_REGS(stk[cnt], stk[cnt+1]);                   \
122         }                                                            \
123         else                                                         \
124             POP_BACK(stk, cnt, reg);                                 \
125     } while (0)
126 #else
127 #define POP_BACK_INT(stk, cnt, reg)  POP_BACK(stk, cnt, reg)
128 #endif
129
130 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
131 #define PUSH_BACK_INT(stk, cnt, reg)                                 \
132     do {                                                             \
133         if (intregsneeded) {                                         \
134             stk[cnt] = GET_LOW_REG(reg);                             \
135             stk[cnt + 1] = GET_HIGH_REG(reg);                        \
136             cnt += 2;                                                \
137         }                                                            \
138         else                                                         \
139             PUSH_BACK(stk, cnt, reg);                                \
140     } while (0)
141 #else
142 #define PUSH_BACK_INT(stk, cnt, reg)  PUSH_BACK(stk, cnt, reg)
143 #endif
144
145 #define AVAIL_ARG_FLT  AVAIL_FRONT(rd->argfltreguse, FLT_ARG_CNT)
146 #define AVAIL_TMP_FLT  AVAIL_BACK(rd->tmpfltreguse)
147 #define AVAIL_SAV_FLT  AVAIL_BACK(rd->savfltreguse)
148
149 #define AVAIL_ARG_ADR  AVAIL_FRONT(rd->argadrreguse, ADR_ARG_CNT)
150 #define AVAIL_TMP_ADR  AVAIL_BACK(rd->tmpadrreguse)
151 #define AVAIL_SAV_ADR  AVAIL_BACK(rd->savadrreguse)
152
153 #define AVAIL_ARG_INT  AVAIL_FRONT_INT(rd->argintreguse, INT_ARG_CNT)
154 #define AVAIL_TMP_INT  AVAIL_BACK_INT(rd->tmpintreguse)
155 #define AVAIL_SAV_INT  AVAIL_BACK_INT(rd->savintreguse)
156
157 #define AVAIL_FREE_ARG_FLT  AVAIL_BACK(rd->freeargflttop)
158 #define AVAIL_FREE_TMP_FLT  AVAIL_BACK(rd->freetmpflttop)
159 #define AVAIL_FREE_SAV_FLT  AVAIL_BACK(rd->freesavflttop)
160
161 #define AVAIL_FREE_ARG_ADR  AVAIL_BACK(rd->freeargadrtop)
162 #define AVAIL_FREE_TMP_ADR  AVAIL_BACK(rd->freetmpadrtop)
163 #define AVAIL_FREE_SAV_ADR  AVAIL_BACK(rd->freesavadrtop)
164
165 #define AVAIL_FREE_ARG_INT  AVAIL_BACK_INT(rd->freearginttop)
166 #define AVAIL_FREE_TMP_INT  AVAIL_BACK_INT(rd->freetmpinttop)
167 #define AVAIL_FREE_SAV_INT  AVAIL_BACK_INT(rd->freesavinttop)
168
169 #define TAKE_ARG_FLT(r)  POP_FRONT(abi_registers_float_argument, rd->argfltreguse, r)
170 #define TAKE_TMP_FLT(r)  POP_BACK(rd->tmpfltregs, rd->tmpfltreguse, r)
171 #define TAKE_SAV_FLT(r)  POP_BACK(rd->savfltregs, rd->savfltreguse, r)
172
173 #define TAKE_ARG_ADR(r)  POP_FRONT(rd->argadrregs, rd->argadrreguse, r)
174 #define TAKE_TMP_ADR(r)  POP_BACK(rd->tmpadrregs, rd->tmpadrreguse, r)
175 #define TAKE_SAV_ADR(r)  POP_BACK(rd->savadrregs, rd->savadrreguse, r)
176
177 #define TAKE_ARG_INT(r)  POP_FRONT_INT(abi_registers_integer_argument, rd->argintreguse, r)
178 #define TAKE_TMP_INT(r)  POP_BACK_INT(rd->tmpintregs, rd->tmpintreguse, r)
179 #define TAKE_SAV_INT(r)  POP_BACK_INT(rd->savintregs, rd->savintreguse, r)
180
181 #define TAKE_FREE_ARG_FLT(r)  POP_BACK(rd->freeargfltregs, rd->freeargflttop, r)
182 #define TAKE_FREE_TMP_FLT(r)  POP_BACK(rd->freetmpfltregs, rd->freetmpflttop, r)
183 #define TAKE_FREE_SAV_FLT(r)  POP_BACK(rd->freesavfltregs, rd->freesavflttop, r)
184
185 #define TAKE_FREE_ARG_ADR(r)  POP_BACK(rd->freeargadrregs, rd->freeargadrtop, r)
186 #define TAKE_FREE_TMP_ADR(r)  POP_BACK(rd->freetmpadrregs, rd->freetmpadrtop, r)
187 #define TAKE_FREE_SAV_ADR(r)  POP_BACK(rd->freesavadrregs, rd->freesavadrtop, r)
188
189 #define TAKE_FREE_ARG_INT(r)  POP_BACK_INT(rd->freeargintregs, rd->freearginttop, r)
190 #define TAKE_FREE_TMP_INT(r)  POP_BACK_INT(rd->freetmpintregs, rd->freetmpinttop, r)
191 #define TAKE_FREE_SAV_INT(r)  POP_BACK_INT(rd->freesavintregs, rd->freesavinttop, r)
192
193 #define PUSH_FREE_ARG_FLT(r)  PUSH_BACK(rd->freeargfltregs, rd->freeargflttop, r)
194 #define PUSH_FREE_TMP_FLT(r)  PUSH_BACK(rd->freetmpfltregs, rd->freetmpflttop, r)
195 #define PUSH_FREE_SAV_FLT(r)  PUSH_BACK(rd->freesavfltregs, rd->freesavflttop, r)
196
197 #define PUSH_FREE_ARG_ADR(r)  PUSH_BACK(rd->freeargadrregs, rd->freeargadrtop, r)
198 #define PUSH_FREE_TMP_ADR(r)  PUSH_BACK(rd->freetmpadrregs, rd->freetmpadrtop, r)
199 #define PUSH_FREE_SAV_ADR(r)  PUSH_BACK(rd->freesavadrregs, rd->freesavadrtop, r)
200
201 #define PUSH_FREE_ARG_INT(r)  PUSH_BACK_INT(rd->freeargintregs, rd->freearginttop, r)
202 #define PUSH_FREE_TMP_INT(r)  PUSH_BACK_INT(rd->freetmpintregs, rd->freetmpinttop, r)
203 #define PUSH_FREE_SAV_INT(r)  PUSH_BACK_INT(rd->freesavintregs, rd->freesavinttop, r)
204
205
206 /* macros for allocating memory slots ****************************************/
207
208 #define NEW_MEM_SLOT(r)                                              \
209     do {                                                             \
210         (r) = rd->memuse * SIZE_OF_STACKSLOT;                        \
211         rd->memuse += memneeded + 1;                                 \
212     } while (0)
213
214 #define NEW_MEM_SLOT_ALIGNED(r)                                      \
215     do {                                                             \
216         if ( (memneeded) && (rd->memuse & 1))                        \
217             rd->memuse++;                                            \
218         (r) = rd->memuse * SIZE_OF_STACKSLOT;                        \
219         rd->memuse += memneeded + 1;                                 \
220     } while (0)
221
222 #define NEW_MEM_SLOT_ALIGNED_REUSE_PADDING(r)                        \
223     do {                                                             \
224         if ( (memneeded) && (rd->memuse & 1)) {                      \
225                         PUSH_BACK(rd->freemem, rd->freememtop, rd->memuse);      \
226             rd->memuse++;                                            \
227                 }                                                            \
228         (r) = rd->memuse * SIZE_OF_STACKSLOT;                        \
229         rd->memuse += memneeded + 1;                                 \
230     } while (0)
231
232 #if defined(ALIGN_LONGS_IN_MEMORY)
233 #define NEW_MEM_SLOT_INT_LNG(r)  NEW_MEM_SLOT_ALIGNED(r)
234 #else
235 #define NEW_MEM_SLOT_INT_LNG(r)  NEW_MEM_SLOT(r)
236 #endif
237
238 #if defined(ALIGN_DOUBLES_IN_MEMORY)
239 #define NEW_MEM_SLOT_FLT_DBL(r)  NEW_MEM_SLOT_ALIGNED(r)
240 #else
241 #define NEW_MEM_SLOT_FLT_DBL(r)  NEW_MEM_SLOT(r)
242 #endif
243
244 #if defined(ALIGN_LONGS_IN_MEMORY) || defined(ALIGN_DOUBLES_IN_MEMORY)
245 #define NEW_MEM_SLOT_REUSE_PADDING(r)  NEW_MEM_SLOT_ALIGNED_REUSE_PADDING(r)
246 #else
247 #define NEW_MEM_SLOT_REUSE_PADDING(r)  NEW_MEM_SLOT(r)
248 #endif
249
250
251 /* macros for creating/freeing temporary variables ***************************/
252
253 #define NEW_TEMP_REG(index)                                          \
254     if ( ((index) >= jd->localcount)                                 \
255          && (!(VAR(index)->flags & (INOUT | PREALLOC))) )            \
256         simplereg_new_temp(jd, (index))
257
258
259 #define FREE_TEMP_REG(index)                                         \
260     if (((index) > jd->localcount)                                   \
261         && (!(VAR(index)->flags & (PREALLOC))))                      \
262         simplereg_free_temp(jd, (index))
263
264
265 /* macro for getting a unique register index *********************************/
266
267 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
268 #define REG_INDEX_NON_ADR(regoff, type)                              \
269     (IS_FLT_DBL_TYPE(type) ? (INT_REG_CNT + (regoff)) : (GET_LOW_REG(regoff)))
270 #else
271 #define REG_INDEX_NON_ADR(regoff, type)                              \
272     (IS_FLT_DBL_TYPE(type) ? (INT_REG_CNT + (regoff)) : (regoff))
273 #endif
274
275 #if defined(HAS_ADDRESS_REGISTER_FILE)
276 #define REG_INDEX(regoff, type)                                      \
277     (IS_ADR_TYPE(type) ? (regoff) : (ADR_REG_CNT + REG_INDEX_NON_ADR(regoff, type)))
278 #else
279 #define REG_INDEX(regoff, type)  REG_INDEX_NON_ADR(regoff, type)
280 #endif
281
282
283 /* regalloc ********************************************************************
284
285    Does a simple register allocation.
286         
287 *******************************************************************************/
288         
289 bool regalloc(jitdata *jd)
290 {
291         /* There is a problem with the use of unused float argument
292            registers in leafmethods for stackslots on c7 (2 * Dual Core
293            AMD Opteron(tm) Processor 270) - runtime for the jvm98 _mtrt
294            benchmark is heaviliy increased. This could be prevented by
295            setting rd->argfltreguse to FLT_ARG_CNT before calling
296            simplereg_allocate_temporaries and setting it back to the original
297            value before calling simplereg_allocate_locals.  */
298
299         simplereg_allocate_interfaces(jd);
300         simplereg_allocate_temporaries(jd);
301         simplereg_allocate_locals(jd);
302
303         /* everthing's ok */
304
305         return true;
306 }
307
308
309 /* simplereg_allocate_interfaces ***********************************************
310
311    Allocates registers for all interface variables.
312         
313 *******************************************************************************/
314         
315 static void simplereg_allocate_interfaces(jitdata *jd)
316 {
317         methodinfo   *m;
318         codeinfo     *code;
319         codegendata  *cd;
320         registerdata *rd;
321
322         int     s, t, tt, saved;
323         int     intalloc, fltalloc; /* Remember allocated Register/Memory offset */
324                         /* in case more vars are packed into this interface slot */
325         int             memneeded = 0;
326         /* Allocate LNG and DBL types first to ensure 2 memory slots or          */
327         /* registers on HAS_4BYTE_STACKSLOT architectures.                       */
328         int     typeloop[] = { TYPE_LNG, TYPE_DBL, TYPE_INT, TYPE_FLT, TYPE_ADR };
329         int     flags, regoff;
330 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
331         int             intregsneeded;
332 #endif
333
334         /* get required compiler data */
335
336         m    = jd->m;
337         code = jd->code;
338         cd   = jd->cd;
339         rd   = jd->rd;
340
341         /* rd->memuse was already set in stack.c to allocate stack space
342            for passing arguments to called methods. */
343
344 #if defined(__I386__)
345         if (checksync && code_is_synchronized(code)) {
346                 /* reserve 0(%esp) for Monitorenter/exit Argument on i386 */
347                 if (rd->memuse < 1)
348                         rd->memuse = 1;
349         }
350 #endif
351
352         if (code_is_leafmethod(code)) {
353                 /* Reserve argument register, which will be used for Locals acting */
354                 /* as Parameters */
355                 if (rd->argintreguse < m->parseddesc->argintreguse)
356                         rd->argintreguse = m->parseddesc->argintreguse;
357                 if (rd->argfltreguse < m->parseddesc->argfltreguse)
358                         rd->argfltreguse = m->parseddesc->argfltreguse;
359 #ifdef HAS_ADDRESS_REGISTER_FILE
360                 if (rd->argadrreguse < m->parseddesc->argadrreguse)
361                         rd->argadrreguse = m->parseddesc->argadrreguse;
362 #endif
363         }
364
365         for (s = 0; s < jd->maxinterfaces; s++) {
366                 intalloc = -1; fltalloc = -1;
367
368                 /* check if the interface at this stack depth must be a SAVEDVAR */
369
370                 saved = 0;
371
372                 for (tt = 0; tt <=4; tt++) {
373                         if ((t = jd->interface_map[s * 5 + tt].flags) != UNUSED) {
374                                 saved |= t & SAVEDVAR;
375                         }
376                 }
377
378                 /* allocate reg/mem for each type the interface is used as */
379
380                 for (tt = 0; tt <= 4; tt++) {
381                         t = typeloop[tt];
382                         if (jd->interface_map[s * 5 + t].flags == UNUSED)
383                                 continue;
384
385                         flags = saved;
386                         regoff = -1; /* initialize to invalid value */
387
388 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
389                         intregsneeded = (IS_2_WORD_TYPE(t)) ? 1 : 0;
390 #endif
391
392 #if defined(HAS_4BYTE_STACKSLOT)
393                         memneeded = (IS_2_WORD_TYPE(t)) ? 1 : 0;
394 #endif
395
396                         if (!saved) {
397 #if defined(HAS_ADDRESS_REGISTER_FILE)
398                                 if (IS_ADR_TYPE(t)) {
399                                         if (!code_is_leafmethod(code) && AVAIL_ARG_ADR) {
400                                                 flags |= ARGREG;
401                                                 TAKE_ARG_ADR(regoff);
402                                         } 
403                                         else if (AVAIL_TMP_ADR) {
404                                                 TAKE_TMP_ADR(regoff);
405                                         } 
406                                         else if (AVAIL_SAV_ADR) {
407                                                 flags |= SAVREG;
408                                                 TAKE_SAV_ADR(regoff);
409                                         } 
410                                         else {
411                                                 flags |= INMEMORY;
412                                                 regoff = rd->memuse++ * SIZE_OF_STACKSLOT;
413                                         }                                               
414                                 } 
415                                 else /* !IS_ADR_TYPE */
416 #endif /* defined(HAS_ADDRESS_REGISTER_FILE) */
417                                 {
418                                         if (IS_FLT_DBL_TYPE(t)) {
419                                                 if (fltalloc >= 0) {
420                                                         /* Reuse memory slot(s)/register(s) for shared interface slots */
421                                                         flags |= jd->interface_map[fltalloc].flags & ~SAVEDVAR;
422                                                         regoff = jd->interface_map[fltalloc].regoff;
423                                                 } 
424                                                 else if (AVAIL_ARG_FLT) {
425                                                         flags |= ARGREG;
426                                                         TAKE_ARG_FLT(regoff);
427                                                 } 
428                                                 else if (AVAIL_TMP_FLT) {
429                                                         TAKE_TMP_FLT(regoff);
430                                                 } 
431                                                 else if (AVAIL_SAV_FLT) {
432                                                         flags |= SAVREG;
433                                                         TAKE_SAV_FLT(regoff);
434                                                 } 
435                                                 else {
436                                                         flags |= INMEMORY;
437                                                         NEW_MEM_SLOT_FLT_DBL(regoff);
438                                                 }
439                                                 fltalloc = s * 5 + t;
440                                         }
441                                         else { /* !IS_FLT_DBL_TYPE(t) */
442 #if (SIZEOF_VOID_P == 4) && !defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
443                                                 /*
444                                                  * for i386 put all longs in memory
445                                                  */
446                                                 if (IS_2_WORD_TYPE(t)) {
447                                                         flags |= INMEMORY;
448                                                         NEW_MEM_SLOT_INT_LNG(regoff);
449                                                 } 
450                                                 else
451 #endif
452                                                         if (intalloc >= 0) {
453                                                                 /* Reuse memory slot(s)/register(s) for shared interface slots */
454                                                                 flags |= jd->interface_map[intalloc].flags & ~SAVEDVAR;
455                                                                 regoff = jd->interface_map[intalloc].regoff;
456 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
457                                                                 /* reuse lower half */
458                                                                 if (!(flags & INMEMORY) 
459                                                                                 && IS_2_WORD_TYPE(intalloc % 5))
460                                                                         regoff = GET_LOW_REG(regoff);
461 #endif
462                                                         } 
463                                                         else {
464                                                                 if (AVAIL_ARG_INT) {
465                                                                         flags |= ARGREG;
466                                                                         TAKE_ARG_INT(regoff);
467                                                                 }
468                                                                 else if (AVAIL_TMP_INT) {
469                                                                         TAKE_TMP_INT(regoff);
470                                                                 }
471                                                                 else if (AVAIL_SAV_INT) {
472                                                                         flags |= SAVREG;
473                                                                         TAKE_SAV_INT(regoff);
474                                                                 }
475                                                                 else {
476                                                                         flags |= INMEMORY;
477                                                                         NEW_MEM_SLOT_INT_LNG(regoff);
478                                                                 }
479                                                         }
480
481                                                 intalloc = s * 5 + t;
482                                         } /* if (IS_FLT_DBL_TYPE(t)) */
483                                 } 
484                         } 
485                         else { /* (saved) */
486                                 /* now the same like above, but without a chance to take a temporary register */
487 #ifdef HAS_ADDRESS_REGISTER_FILE
488                                 if (IS_ADR_TYPE(t)) {
489                                         if (AVAIL_SAV_ADR) {
490                                                 TAKE_SAV_ADR(regoff);
491                                         }
492                                         else {
493                                                 flags |= INMEMORY;
494                                                 regoff = rd->memuse++ * SIZE_OF_STACKSLOT;
495                                         }                                               
496                                 } 
497                                 else
498 #endif
499                                 {
500                                         if (IS_FLT_DBL_TYPE(t)) {
501                                                 if (fltalloc >= 0) {
502                                                         flags |= jd->interface_map[fltalloc].flags & ~SAVEDVAR;
503                                                         regoff = jd->interface_map[fltalloc].regoff;
504                                                 } 
505                                                 else {
506                                                         if (AVAIL_SAV_FLT) {
507                                                                 TAKE_SAV_FLT(regoff);
508                                                         }
509                                                         else {
510                                                                 flags |= INMEMORY;
511                                                                 NEW_MEM_SLOT_FLT_DBL(regoff);
512                                                         }
513                                                 }
514                                                 fltalloc = s * 5 + t;
515                                         }
516                                         else { /* IS_INT_LNG */
517 #if (SIZEOF_VOID_P == 4) && !defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
518                                                 /*
519                                                  * for i386 put all longs in memory
520                                                  */
521                                                 if (IS_2_WORD_TYPE(t)) {
522                                                         flags |= INMEMORY;
523                                                         NEW_MEM_SLOT_INT_LNG(regoff);
524                                                 } 
525                                                 else
526 #endif
527                                                 {
528                                                         if (intalloc >= 0) {
529                                                                 flags |= jd->interface_map[intalloc].flags & ~SAVEDVAR;
530                                                                 regoff = jd->interface_map[intalloc].regoff;
531 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
532                                                                 /*  reuse lower half */
533                                                                 if (!(flags & INMEMORY)
534                                                                                 && IS_2_WORD_TYPE(intalloc % 5))
535                                                                         regoff = GET_LOW_REG(regoff);
536 #endif
537                                                         } 
538                                                         else {
539                                                                 if (AVAIL_SAV_INT) {
540                                                                         TAKE_SAV_INT(regoff);
541                                                                 } 
542                                                                 else {
543                                                                         flags |= INMEMORY;
544                                                                         NEW_MEM_SLOT_INT_LNG(regoff);
545                                                                 }
546                                                         }
547                                                         intalloc = s*5 + t;
548                                                 }
549                                         } /* if (IS_FLT_DBL_TYPE(t) else */
550                                 } /* if (IS_ADR_TYPE(t)) else */
551                         } /* if (saved) else */
552                         /* if (type >= 0) */
553
554                         assert(regoff >= 0);
555                         jd->interface_map[5*s + t].flags = flags | INOUT;
556                         jd->interface_map[5*s + t].regoff = regoff;
557                 } /* for t */
558         } /* for s */
559 }
560
561
562 /* simplereg_allocate_locals_leafmethod ****************************************
563
564    Allocates registers for all local variables of a leafmethod.
565         
566 *******************************************************************************/
567         
568 static void simplereg_allocate_locals_leafmethod(jitdata *jd)
569 {
570         methodinfo   *m;
571         codegendata  *cd;
572         registerdata *rd;
573         methoddesc *md;
574
575         int     p, s, t, tt, varindex;
576         int     intalloc, fltalloc;
577         varinfo *v;
578         int     intregsneeded = 0;
579         int     memneeded = 0;
580         int     typeloop[] = { TYPE_LNG, TYPE_DBL, TYPE_INT, TYPE_FLT, TYPE_ADR };
581         int     fargcnt, iargcnt;
582 #ifdef HAS_ADDRESS_REGISTER_FILE
583         int     aargcnt;
584 #endif
585
586         /* get required compiler data */
587
588         m  = jd->m;
589         cd = jd->cd;
590         rd = jd->rd;
591
592         md = m->parseddesc;
593
594         iargcnt = rd->argintreguse;
595         fargcnt = rd->argfltreguse;
596 #ifdef HAS_ADDRESS_REGISTER_FILE
597         aargcnt = rd->argadrreguse;
598 #endif
599         for (p = 0, s = 0; s < jd->maxlocals; s++, p++) {
600                 intalloc = -1; fltalloc = -1;
601                 for (tt = 0; tt <= 4; tt++) {
602                         t = typeloop[tt];
603                         varindex = jd->local_map[s * 5 + t];
604                         if (varindex == UNUSED)
605                                 continue;
606
607                         v = VAR(varindex);
608
609 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
610                         intregsneeded = (IS_2_WORD_TYPE(t)) ? 1 : 0;
611 #endif
612 #if defined(HAS_4BYTE_STACKSLOT)
613                         memneeded = (IS_2_WORD_TYPE(t)) ? 1 : 0;
614 #endif
615
616                         /*
617                          *  The order of
618                          *
619                          *  #ifdef HAS_ADDRESS_REGISTER_FILE
620                          *  if (IS_ADR_TYPE) { 
621                          *  ...
622                          *  } else 
623                          *  #endif
624                          *  if (IS_FLT_DBL) {
625                          *  ...
626                          *  } else { / int & lng
627                          *  ...
628                          *  }
629                          *
630                          *  must not to be changed!
631                          */
632
633 #ifdef HAS_ADDRESS_REGISTER_FILE
634                         if (IS_ADR_TYPE(t)) {
635                                 if ((p < md->paramcount) && !md->params[p].inmemory) {
636                                         v->flags = 0;
637                                         v->vv.regoff = rd->argadrregs[md->params[p].regoff];
638                                 }
639                                 else if (AVAIL_TMP_ADR) {
640                                         v->flags = 0;
641                                         TAKE_TMP_ADR(v->vv.regoff);
642                                 }
643                                 /* use unused argument registers as local registers */
644                                 else if ((p >= md->paramcount) &&
645                                                  (aargcnt < ADR_ARG_CNT)) 
646                                 {
647                                         v->flags = 0;
648                                         POP_FRONT(rd->argadrregs, aargcnt, v->vv.regoff);
649                                 }
650                                 else if (AVAIL_SAV_ADR) {
651                                         v->flags = 0;
652                                         TAKE_SAV_ADR(v->vv.regoff);
653                                 }
654                                 else {
655                                         v->flags |= INMEMORY;
656                                         v->vv.regoff = rd->memuse++ * SIZE_OF_STACKSLOT;
657                                 }                                               
658                         } 
659                         else {
660 #endif
661                                 if (IS_FLT_DBL_TYPE(t)) {
662                                         if (fltalloc >= 0) {
663                                                 v->flags = VAR(fltalloc)->flags;
664                                                 v->vv.regoff = VAR(fltalloc)->vv.regoff;
665                                         }
666 #if !defined(SUPPORT_PASS_FLOATARGS_IN_INTREGS)
667                                         /* We can only use float arguments as local variables,
668                                          * if we do not pass them in integer registers. */
669                                         else if ((p < md->paramcount) && !md->params[p].inmemory) {
670                                                 v->flags = 0;
671                                                 v->vv.regoff = md->params[p].regoff;
672                                         }
673 #endif
674                                         else if (AVAIL_TMP_FLT) {
675                                                 v->flags = 0;
676                                                 TAKE_TMP_FLT(v->vv.regoff);
677                                         }
678                                         /* use unused argument registers as local registers */
679                                         else if ((p >= md->paramcount) && (fargcnt < FLT_ARG_CNT)) {
680                                                 v->flags = 0;
681                                                 POP_FRONT(abi_registers_float_argument,
682                                                                   fargcnt, v->vv.regoff);
683                                         }
684                                         else if (AVAIL_SAV_FLT) {
685                                                 v->flags = 0;
686                                                 TAKE_SAV_FLT(v->vv.regoff);
687                                         }
688                                         else {
689                                                 v->flags = INMEMORY;
690                                                 NEW_MEM_SLOT_FLT_DBL(v->vv.regoff);
691                                         }
692                                         fltalloc = jd->local_map[s * 5 + t];
693
694                                 } 
695                                 else {
696 #if (SIZEOF_VOID_P == 4) && !defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
697                                         /*
698                                          * for i386 put all longs in memory
699                                          */
700                                         if (IS_2_WORD_TYPE(t)) {
701                                                 v->flags = INMEMORY;
702                                                 NEW_MEM_SLOT_INT_LNG(v->vv.regoff);
703                                         } 
704                                         else 
705 #endif
706                                         {
707                                                 if (intalloc >= 0) {
708                                                         v->flags = VAR(intalloc)->flags;
709 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
710                                                         if (!(v->flags & INMEMORY)
711                                                                 && IS_2_WORD_TYPE(VAR(intalloc)->type))
712                                                                 v->vv.regoff = GET_LOW_REG(
713                                                                                                 VAR(intalloc)->vv.regoff);
714                                                         else
715 #endif
716                                                                 v->vv.regoff = VAR(intalloc)->vv.regoff;
717                                                 }
718                                                 else if ((p < md->paramcount) && 
719                                                                  !md->params[p].inmemory) 
720                                                 {
721                                                         v->flags = 0;
722 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
723                                                         if (IS_2_WORD_TYPE(t))
724                                                                 v->vv.regoff =
725                                                                         PACK_REGS(GET_LOW_REG(md->params[p].regoff),
726                                                                                           GET_HIGH_REG(md->params[p].regoff));
727                                                                 else
728 #endif
729                                                                         v->vv.regoff = md->params[p].regoff;
730                                                 }
731                                                 else if (AVAIL_TMP_INT) {
732                                                         v->flags = 0;
733                                                         TAKE_TMP_INT(v->vv.regoff);
734                                                 }
735                                                 /*
736                                                  * use unused argument registers as local registers
737                                                  */
738                                                 else if ((p >= m->parseddesc->paramcount) &&
739                                                                  (iargcnt + intregsneeded < INT_ARG_CNT)) 
740                                                 {
741                                                         v->flags = 0;
742                                                         POP_FRONT_INT(abi_registers_integer_argument,
743                                                                                   iargcnt, v->vv.regoff);
744                                                 }
745                                                 else if (AVAIL_SAV_INT) {
746                                                         v->flags = 0;
747                                                         TAKE_SAV_INT(v->vv.regoff);
748                                                 }
749                                                 else {
750                                                         v->flags = INMEMORY;
751                                                         NEW_MEM_SLOT_INT_LNG(v->vv.regoff);
752                                                 }
753                                         }
754                                         intalloc = jd->local_map[s * 5 + t];
755                                 }
756 #ifdef HAS_ADDRESS_REGISTER_FILE
757                         }
758 #endif
759                 } /* for (tt=0;...) */
760
761                 /* If the current parameter is a 2-word type, the next local slot */
762                 /* is skipped.                                                    */
763
764                 if (p < md->paramcount)
765                         if (IS_2_WORD_TYPE(md->paramtypes[p].type))
766                                 s++;
767         }
768 }
769
770 /* simplereg_allocate_locals ***************************************************
771
772    Allocates registers for all local variables.
773         
774 *******************************************************************************/
775         
776 static void simplereg_allocate_locals(jitdata *jd)
777 {
778         codeinfo     *code;
779         codegendata  *cd;
780         registerdata *rd;
781
782         int     s, t, tt, varindex;
783         int     intalloc, fltalloc;
784         varinfo *v;
785         int     memneeded = 0;
786         int     typeloop[] = { TYPE_LNG, TYPE_DBL, TYPE_INT, TYPE_FLT, TYPE_ADR };
787 #ifdef SUPPORT_COMBINE_INTEGER_REGISTERS
788         s4 intregsneeded;
789 #endif
790
791         /* get required compiler data */
792
793         code = jd->code;
794         cd   = jd->cd;
795         rd   = jd->rd;
796
797         if (code_is_leafmethod(code)) {
798                 simplereg_allocate_locals_leafmethod(jd);
799                 return;
800         }
801
802         for (s = 0; s < jd->maxlocals; s++) {
803                 intalloc = -1; fltalloc = -1;
804                 for (tt=0; tt<=4; tt++) {
805                         t = typeloop[tt];
806
807                         varindex = jd->local_map[s * 5 + t];
808                         if (varindex == UNUSED)
809                                 continue;
810
811                         v = VAR(varindex);
812
813 #ifdef SUPPORT_COMBINE_INTEGER_REGISTERS
814                                 intregsneeded = (IS_2_WORD_TYPE(t)) ? 1 : 0;
815 #endif
816
817 #if defined(HAS_4BYTE_STACKSLOT)
818                                 memneeded = (IS_2_WORD_TYPE(t)) ? 1 : 0;
819 #endif
820
821 #ifdef HAS_ADDRESS_REGISTER_FILE
822                                 if (IS_ADR_TYPE(t)) {
823                                         if (AVAIL_SAV_ADR) {
824                                                 v->flags = 0;
825                                                 TAKE_SAV_ADR(v->vv.regoff);
826                                         }
827                                         else {
828                                                 v->flags = INMEMORY;
829                                                 v->vv.regoff = rd->memuse++ * SIZE_OF_STACKSLOT;
830                                         }
831                                 } 
832                                 else {
833 #endif
834                                 if (IS_FLT_DBL_TYPE(t)) {
835                                         if (fltalloc >= 0) {
836                                                 v->flags = VAR(fltalloc)->flags;
837                                                 v->vv.regoff = VAR(fltalloc)->vv.regoff;
838                                         }
839                                         else if (AVAIL_SAV_FLT) {
840                                                 v->flags = 0;
841                                                 TAKE_SAV_FLT(v->vv.regoff);
842                                         }
843                                         else {
844                                                 v->flags = INMEMORY;
845                                                 NEW_MEM_SLOT_FLT_DBL(v->vv.regoff);
846                                         }
847                                         fltalloc = jd->local_map[s * 5 + t];
848                                 }
849                                 else {
850 #if (SIZEOF_VOID_P == 4) && !defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
851                                         /*
852                                          * for i386 put all longs in memory
853                                          */
854                                         if (IS_2_WORD_TYPE(t)) {
855                                                 v->flags = INMEMORY;
856                                                 NEW_MEM_SLOT_INT_LNG(v->vv.regoff);
857                                         } 
858                                         else {
859 #endif
860                                                 if (intalloc >= 0) {
861                                                         v->flags = VAR(intalloc)->flags;
862 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
863                                                         if (!(v->flags & INMEMORY)
864                                                                 && IS_2_WORD_TYPE(VAR(intalloc)->type))
865                                                                 v->vv.regoff = GET_LOW_REG(
866                                                                                             VAR(intalloc)->vv.regoff);
867                                                         else
868 #endif
869                                                                 v->vv.regoff = VAR(intalloc)->vv.regoff;
870                                                 }
871                                                 else if (AVAIL_SAV_INT) {
872                                                         v->flags = 0;
873                                                         TAKE_SAV_INT(v->vv.regoff);
874                                                 }
875                                                 else {
876                                                         v->flags = INMEMORY;
877                                                         NEW_MEM_SLOT_INT_LNG(v->vv.regoff);
878                                                 }
879 #if (SIZEOF_VOID_P == 4) && !defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
880                                         }
881 #endif
882                                         intalloc = jd->local_map[s * 5 + t];
883                                 }
884 #ifdef HAS_ADDRESS_REGISTER_FILE
885                                 }
886 #endif
887                 }
888         }
889 }
890
891
892 static void simplereg_init(jitdata *jd, registerdata *rd)
893 {
894         int i;
895
896         rd->freememtop = 0;
897 #if defined(HAS_4BYTE_STACKSLOT)
898         rd->freememtop_2 = 0;
899 #endif
900
901         rd->freetmpinttop = 0;
902         rd->freesavinttop = 0;
903         rd->freetmpflttop = 0;
904         rd->freesavflttop = 0;
905 #ifdef HAS_ADDRESS_REGISTER_FILE
906         rd->freetmpadrtop = 0;
907         rd->freesavadrtop = 0;
908 #endif
909
910         rd->freearginttop = 0;
911         rd->freeargflttop = 0;
912 #ifdef HAS_ADDRESS_REGISTER_FILE
913         rd->freeargadrtop = 0;
914 #endif
915
916         rd->regisoutvar = DMNEW(int, TOTAL_REG_CNT);
917         rd->regcopycount = DMNEW(int, TOTAL_REG_CNT);
918         MZERO(rd->regcopycount, int, TOTAL_REG_CNT);
919
920         /* memcopycount is dynamically allocated when needed */
921
922         rd->memcopycount = NULL;
923         rd->memcopycountsize = 0;
924
925         rd->intusedinout = DMNEW(int, INT_REG_CNT);
926         MZERO(rd->intusedinout, int, INT_REG_CNT);
927         rd->fltusedinout = DMNEW(int, FLT_REG_CNT);
928         MZERO(rd->fltusedinout, int, FLT_REG_CNT);
929
930         /* record the interface registers as used */
931
932         for (i=0; i<rd->argintreguse; ++i)
933                 rd->intusedinout[abi_registers_integer_argument[i]] = 1;
934         for (i=rd->tmpintreguse; i<INT_TMP_CNT; ++i)
935                 rd->intusedinout[rd->tmpintregs[i]] = 1;
936         for (i=rd->savintreguse; i<INT_SAV_CNT; ++i)
937                 rd->intusedinout[rd->savintregs[i]] = 1;
938
939         for (i=0; i<rd->argfltreguse; ++i)
940                 rd->fltusedinout[abi_registers_float_argument[i]] = 1;
941         for (i=rd->tmpfltreguse; i<FLT_TMP_CNT; ++i)
942                 rd->fltusedinout[rd->tmpfltregs[i]] = 1;
943         for (i=rd->savfltreguse; i<FLT_SAV_CNT; ++i)
944                 rd->fltusedinout[rd->savfltregs[i]] = 1;
945
946 #ifdef HAS_ADDRESS_REGISTER_FILE
947         rd->adrusedinout = DMNEW(int, ADR_REG_CNT);
948         MZERO(rd->adrusedinout, int, ADR_REG_CNT);
949
950         for (i=0; i<rd->argadrreguse; ++i)
951                 rd->adrusedinout[rd->argadrregs[i]] = 1;
952         for (i=rd->tmpadrreguse; i<ADR_TMP_CNT; ++i)
953                 rd->adrusedinout[rd->tmpadrregs[i]] = 1;
954         for (i=rd->savadrreguse; i<ADR_SAV_CNT; ++i)
955                 rd->adrusedinout[rd->savadrregs[i]] = 1;
956 #endif
957 }
958
959
960 static void simplereg_init_block(registerdata *rd)
961 {
962         int i;
963
964         /* remove all interface registers from the free lists */
965
966         for (i=0; i<rd->freearginttop; ++i)
967                 if (rd->intusedinout[rd->freeargintregs[i]]) {
968                         rd->freeargintregs[i--] = rd->freeargintregs[--rd->freearginttop];
969                 }
970         for (i=0; i<rd->freetmpinttop; ++i)
971                 if (rd->intusedinout[rd->freetmpintregs[i]]) {
972                         rd->freetmpintregs[i--] = rd->freetmpintregs[--rd->freetmpinttop];
973                 }
974         for (i=0; i<rd->freesavinttop; ++i)
975                 if (rd->intusedinout[rd->freesavintregs[i]]) {
976                         rd->freesavintregs[i--] = rd->freesavintregs[--rd->freesavinttop];
977                 }
978
979         for (i=0; i<rd->freeargflttop; ++i)
980                 if (rd->fltusedinout[rd->freeargfltregs[i]]) {
981                         rd->freeargfltregs[i--] = rd->freeargfltregs[--rd->freeargflttop];
982                 }
983         for (i=0; i<rd->freetmpflttop; ++i)
984                 if (rd->fltusedinout[rd->freetmpfltregs[i]]) {
985                         rd->freetmpfltregs[i--] = rd->freetmpfltregs[--rd->freetmpflttop];
986                 }
987         for (i=0; i<rd->freesavflttop; ++i)
988                 if (rd->fltusedinout[rd->freesavfltregs[i]]) {
989                         rd->freesavfltregs[i--] = rd->freesavfltregs[--rd->freesavflttop];
990                 }
991
992 #ifdef HAS_ADDRESS_REGISTER_FILE
993         for (i=0; i<rd->freeargadrtop; ++i)
994                 if (rd->adrusedinout[rd->freeargadrregs[i]]) {
995                         rd->freeargadrregs[i--] = rd->freeargadrregs[--rd->freeargadrtop];
996                 }
997         for (i=0; i<rd->freetmpadrtop; ++i)
998                 if (rd->adrusedinout[rd->freetmpadrregs[i]]) {
999                         rd->freetmpadrregs[i--] = rd->freetmpadrregs[--rd->freetmpadrtop];
1000                 }
1001         for (i=0; i<rd->freesavadrtop; ++i)
1002                 if (rd->adrusedinout[rd->freesavadrregs[i]]) {
1003                         rd->freesavadrregs[i--] = rd->freesavadrregs[--rd->freesavadrtop];
1004                 }
1005 #endif
1006 }
1007
1008
1009 static void simplereg_new_temp(jitdata *jd, s4 index)
1010 {
1011 #ifdef SUPPORT_COMBINE_INTEGER_REGISTERS
1012         s4 intregsneeded;
1013 #endif
1014         s4 memneeded;
1015         s4 tryagain;
1016         registerdata *rd;
1017         varinfo      *v;
1018
1019         rd = jd->rd;
1020         v = VAR(index);
1021
1022         /* assert that constants are not allocated */
1023
1024         assert(v->type != TYPE_RET);
1025
1026         /* Try to allocate a saved register if there is no temporary one          */
1027         /* available. This is what happens during the second run.                 */
1028         tryagain = (v->flags & SAVEDVAR) ? 1 : 2;
1029
1030 #ifdef SUPPORT_COMBINE_INTEGER_REGISTERS
1031         intregsneeded = (IS_2_WORD_TYPE(v->type)) ? 1 : 0;
1032 #endif
1033
1034 #if defined(HAS_4BYTE_STACKSLOT)
1035         memneeded = (IS_2_WORD_TYPE(v->type)) ? 1 : 0;
1036 #else
1037         memneeded = 0;
1038 #endif
1039
1040         for(; tryagain; --tryagain) {
1041                 if (tryagain == 1) {
1042                         if (!(v->flags & SAVEDVAR))
1043                                 v->flags |= SAVREG;
1044 #ifdef HAS_ADDRESS_REGISTER_FILE
1045                         if (IS_ADR_TYPE(v->type)) {
1046                                 if (AVAIL_FREE_SAV_ADR) {
1047                                         TAKE_FREE_SAV_ADR(v->vv.regoff);
1048                                         return;
1049                                 } 
1050                                 else if (AVAIL_SAV_ADR) {
1051                                         TAKE_SAV_ADR(v->vv.regoff);
1052                                         return;
1053                                 }
1054                         } 
1055                         else
1056 #endif
1057                         {
1058                                 if (IS_FLT_DBL_TYPE(v->type)) {
1059                                         if (AVAIL_FREE_SAV_FLT) {
1060                                                 TAKE_FREE_SAV_FLT(v->vv.regoff);
1061                                                 return;
1062                                         } 
1063                                         else if (AVAIL_SAV_FLT) {
1064                                                 TAKE_SAV_FLT(v->vv.regoff);
1065                                                 return;
1066                                         }
1067                                 } 
1068                                 else {
1069 #if (SIZEOF_VOID_P == 4) && !defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
1070                                         /*
1071                                          * for i386 put all longs in memory
1072                                          */
1073                                         if (!IS_2_WORD_TYPE(v->type))
1074 #endif
1075                                         {
1076                                                 if (AVAIL_FREE_SAV_INT) {
1077                                                         TAKE_FREE_SAV_INT(v->vv.regoff);
1078                                                         return;
1079                                                 } 
1080                                                 else if (AVAIL_SAV_INT) {
1081                                                         TAKE_SAV_INT(v->vv.regoff);
1082                                                         return;
1083                                                 }
1084                                         }
1085                                 }
1086                         }
1087                 } 
1088                 else { /* tryagain == 2 */
1089 #ifdef HAS_ADDRESS_REGISTER_FILE
1090                         if (IS_ADR_TYPE(v->type)) {
1091                                 if (AVAIL_FREE_TMP_ADR) {
1092                                         TAKE_FREE_TMP_ADR(v->vv.regoff);
1093                                         return;
1094                                 } 
1095                                 else if (AVAIL_TMP_ADR) {
1096                                         TAKE_TMP_ADR(v->vv.regoff);
1097                                         return;
1098                                 }
1099                         } 
1100                         else
1101 #endif
1102                         {
1103                                 if (IS_FLT_DBL_TYPE(v->type)) {
1104                                         if (AVAIL_FREE_ARG_FLT) {
1105                                                 v->flags |= ARGREG;
1106                                                 TAKE_FREE_ARG_FLT(v->vv.regoff);
1107                                                 return;
1108                                         } 
1109                                         else if (AVAIL_ARG_FLT) {
1110                                                 v->flags |= ARGREG;
1111                                                 TAKE_ARG_FLT(v->vv.regoff);
1112                                                 return;
1113                                         } 
1114                                         else if (AVAIL_FREE_TMP_FLT) {
1115                                                 TAKE_FREE_TMP_FLT(v->vv.regoff);
1116                                                 return;
1117                                         } 
1118                                         else if (AVAIL_TMP_FLT) {
1119                                                 TAKE_TMP_FLT(v->vv.regoff);
1120                                                 return;
1121                                         }
1122
1123                                 } 
1124                                 else {
1125 #if (SIZEOF_VOID_P == 4) && !defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
1126                                         /*
1127                                          * for i386 put all longs in memory
1128                                          */
1129                                         if (!IS_2_WORD_TYPE(v->type))
1130 #endif
1131                                         {
1132                                                 if (AVAIL_FREE_ARG_INT) {
1133                                                         v->flags |= ARGREG;
1134                                                         TAKE_FREE_ARG_INT(v->vv.regoff);
1135                                                         return;
1136                                                 } 
1137                                                 else if (AVAIL_ARG_INT) {
1138                                                         v->flags |= ARGREG;
1139                                                         TAKE_ARG_INT(v->vv.regoff);
1140                                                         return;
1141                                                 } 
1142                                                 else if (AVAIL_FREE_TMP_INT) {
1143                                                         TAKE_FREE_TMP_INT(v->vv.regoff);
1144                                                         return;
1145                                                 } 
1146                                                 else if (AVAIL_TMP_INT) {
1147                                                         TAKE_TMP_INT(v->vv.regoff);
1148                                                         return;
1149                                                 }
1150                                         } /* if (!IS_2_WORD_TYPE(s->type)) */
1151                                 } /* if (IS_FLT_DBL_TYPE(s->type)) */
1152                         } /* if (IS_ADR_TYPE(s->type)) */
1153                 } /* if (tryagain == 1) else */
1154         } /* for(; tryagain; --tryagain) */
1155
1156         /* spill to memory */
1157
1158         v->flags |= INMEMORY;
1159
1160 #if defined(HAS_4BYTE_STACKSLOT)
1161         if ((memneeded == 1) && (rd->freememtop_2 > 0))
1162                 POP_BACK(rd->freemem_2, rd->freememtop_2, v->vv.regoff);
1163         else
1164 #endif /*defined(HAS_4BYTE_STACKSLOT) */
1165                 if ((memneeded == 0) && (rd->freememtop > 0))
1166                         POP_BACK(rd->freemem, rd->freememtop, v->vv.regoff);
1167                 else
1168                         NEW_MEM_SLOT_REUSE_PADDING(v->vv.regoff);
1169 }
1170
1171
1172 static void simplereg_free(registerdata *rd, s4 flags, s4 regoff, s4 type)
1173 {
1174         /* assert that constants are not freed */
1175
1176         assert(type != TYPE_RET);
1177
1178         /* if this is a copy of another variable, just decrement the copy counter */
1179
1180         if (flags & INMEMORY) {
1181                 int32_t memindex;
1182
1183                 if (flags & INOUT)
1184                         return;
1185
1186                 memindex = regoff / SIZE_OF_STACKSLOT;
1187
1188                 if (memindex < rd->memcopycountsize && rd->memcopycount[memindex]) {
1189                         rd->memcopycount[memindex]--;
1190                         return;
1191                 }
1192         }
1193         else {
1194                 s4 regindex;
1195
1196                 regindex = REG_INDEX(regoff, type);
1197
1198                 /* do not free interface registers that are needed as outvars */
1199
1200                 if (flags & INOUT) {
1201                         if (rd->regisoutvar[regindex]) {
1202                                 LOG(("DONT FREE f=%02x r=%d t=%d\n", flags, regoff, type));
1203                                 return;
1204                         }
1205
1206                         LOG(("FREEING INVAR f=%02x r=%d t=%d\n", flags, regoff, type));
1207                 }
1208
1209                 if (rd->regcopycount[regindex]) {
1210                         rd->regcopycount[regindex]--;
1211                         return;
1212                 }
1213         }
1214
1215         if (flags & INMEMORY) {
1216 #if defined(HAS_4BYTE_STACKSLOT)
1217                 if (IS_2_WORD_TYPE(type))
1218                         PUSH_BACK(rd->freemem_2, rd->freememtop_2, regoff);
1219                 else 
1220 #endif
1221                         PUSH_BACK(rd->freemem, rd->freememtop, regoff);
1222
1223                 return;
1224         } 
1225
1226         /* freeing a register */
1227
1228 #ifdef HAS_ADDRESS_REGISTER_FILE
1229         if (IS_ADR_TYPE(type)) {
1230                 if (flags & (SAVEDVAR | SAVREG))
1231                         PUSH_FREE_SAV_ADR(regoff);
1232                 else
1233                         PUSH_FREE_TMP_ADR(regoff);
1234         } 
1235 #endif
1236         else if (IS_FLT_DBL_TYPE(type)) {
1237                 if (flags & (SAVEDVAR | SAVREG))
1238                         PUSH_FREE_SAV_FLT(regoff);
1239                 else if (flags & ARGREG)
1240                         PUSH_FREE_ARG_FLT(regoff);
1241                 else
1242                         PUSH_FREE_TMP_FLT(regoff);
1243         } 
1244         else { /* IS_INT_LNG_TYPE */
1245 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
1246                 s4 intregsneeded = (IS_2_WORD_TYPE(type)) ? 1 : 0;
1247 #endif
1248
1249                 if (flags & (SAVEDVAR | SAVREG))
1250                         PUSH_FREE_SAV_INT(regoff);
1251                 else if (flags & ARGREG)
1252                         PUSH_FREE_ARG_INT(regoff);
1253                 else
1254                         PUSH_FREE_TMP_INT(regoff);
1255         }
1256 }
1257
1258
1259 static inline void simplereg_free_temp(jitdata *jd, s4 index)
1260 {
1261         varinfo *v;
1262
1263         v = VAR(index);
1264
1265         simplereg_free(jd->rd, v->flags, v->vv.regoff, v->type);
1266 }
1267
1268
1269 static bool simplereg_alloc_dup(jitdata *jd, s4 srcindex, s4 dstindex)
1270 {
1271         varinfo *sv;
1272         varinfo *dv;
1273
1274         /* do not coalesce local variables here */
1275
1276         if (srcindex <= jd->localcount || dstindex <= jd->localcount)
1277                 return false;
1278
1279         sv = VAR(srcindex);
1280         dv = VAR(dstindex);
1281
1282         /* do not coalesce in/out vars or preallocated variables here */
1283
1284         if ((sv->flags | dv->flags) & (INOUT | PREALLOC))
1285                 return false;
1286
1287         /* if the source is in memory, we can coalesce in any case */
1288
1289         if (sv->flags & INMEMORY) {
1290                 dv->flags |= INMEMORY;
1291                 dv->vv.regoff = sv->vv.regoff;
1292                 return true;
1293         }
1294
1295         /* we do not allocate a REG_TMP to a REG_SAV variable */
1296
1297         if ((sv->flags & SAVEDVAR) != (dv->flags & SAVEDVAR))
1298                 return false;
1299
1300         /* coalesce */
1301         dv->vv.regoff = sv->vv.regoff;
1302         dv->flags |= sv->flags & (SAVREG | ARGREG);
1303
1304         return true;
1305 }
1306
1307
1308 /* simplereg_allocate_temporaries **********************************************
1309
1310    Allocate temporary (non-interface, non-local) registers.
1311
1312 *******************************************************************************/
1313
1314 static void simplereg_allocate_temporaries(jitdata *jd)
1315 {
1316         methodinfo         *m;
1317         registerdata       *rd;
1318         s4                  i;
1319         s4                  len;
1320         instruction        *iptr;
1321         basicblock         *bptr;
1322         builtintable_entry *bte;
1323         methoddesc         *md;
1324         s4                 *argp;
1325         varinfo            *v;
1326         s4                  flags;
1327         s4                  regoff;
1328         s4                  type;
1329         s4                  regindex;
1330
1331         /* get required compiler data */
1332
1333         m  = jd->m;
1334         rd = jd->rd;
1335
1336         /* initialize temp registers */
1337
1338         simplereg_init(jd, rd);
1339
1340         bptr = jd->basicblocks;
1341
1342         while (bptr != NULL) {
1343                 if (bptr->flags >= BBREACHED) {
1344
1345                         LOG(("\nallocating block L%03d\n", bptr->nr));
1346
1347                         simplereg_init_block(rd);
1348
1349                         /* assert that all copy counts are zero */
1350
1351 #if !defined(NDEBUG) && !defined(ENABLE_SSA)
1352                         for (i=0; i < TOTAL_REG_CNT; ++i)
1353                                 assert(rd->regcopycount[i] == 0);
1354 #endif
1355
1356                         /* reset outvar flags */
1357
1358                         MZERO(rd->regisoutvar, int, TOTAL_REG_CNT);
1359
1360                         /* set allocation of invars */
1361
1362                         for (i=0; i<bptr->indepth; ++i) 
1363                         {
1364                                 v = VAR(bptr->invars[i]);
1365                                 if (v->type == TYPE_RET)
1366                                         continue;
1367
1368                                 v->vv.regoff = jd->interface_map[5*i + v->type].regoff;
1369                                 v->flags  = jd->interface_map[5*i + v->type].flags;
1370
1371                                 if (!(v->flags & INMEMORY))
1372                                         rd->regcopycount[REG_INDEX(v->vv.regoff, v->type)] = 1;
1373                         }
1374
1375                         /* set allocation of outvars */
1376
1377                         for (i=0; i<bptr->outdepth; ++i) 
1378                         {
1379                                 v = VAR(bptr->outvars[i]);
1380                                 if (v->type == TYPE_RET)
1381                                         continue;
1382
1383                                 v->vv.regoff = jd->interface_map[5*i + v->type].regoff;
1384                                 v->flags  = jd->interface_map[5*i + v->type].flags;
1385
1386                                 if (!(v->flags & INMEMORY)) {
1387                                         regindex = REG_INDEX(v->vv.regoff, v->type);
1388                                         rd->regcopycount[regindex] = 1;
1389                                         rd->regisoutvar[regindex] = 1;
1390                                 }
1391                         }
1392
1393                         /* free interface registers not used in this block */
1394
1395                         for (i=0; i < 5 * jd->maxinterfaces; ++i) {
1396                                 type = i%5;
1397                                 regoff = jd->interface_map[i].regoff;
1398                                 flags = jd->interface_map[i].flags;
1399
1400                                 if (!(flags & INMEMORY)) {
1401                                         if (!rd->regcopycount[REG_INDEX(regoff, type)]) {
1402                                                 LOG(("MAY REUSE interface register f=%02x r=%d t=%d\n", 
1403                                                                         flags, regoff, type));
1404                                                 simplereg_free(rd, flags, regoff, type);
1405
1406                                                 /* mark it, so it is not freed again */
1407                                                 rd->regcopycount[REG_INDEX(regoff, type)] = -1;
1408                                         }
1409                                 }
1410                         }
1411
1412                         /* reset copy counts */
1413
1414                         MZERO(rd->regcopycount, int, TOTAL_REG_CNT);
1415
1416                         /* iterate over ICMDS to allocate temporary variables */
1417
1418                         iptr = bptr->iinstr;
1419                         len = bptr->icount;
1420
1421                         while (--len >= 0)  {
1422
1423                                 switch (iptr->opc) {
1424
1425                                         /* pop 0 push 0 */
1426
1427                                 case ICMD_JSR:
1428                                 case ICMD_NOP:
1429                                 case ICMD_CHECKNULL:
1430                                 case ICMD_IINC:
1431                                 case ICMD_RET:
1432                                 case ICMD_RETURN:
1433                                 case ICMD_GOTO:
1434                                 case ICMD_PUTSTATICCONST:
1435                                 case ICMD_INLINE_START:
1436                                 case ICMD_INLINE_END:
1437                                 case ICMD_INLINE_BODY:
1438                                         break;
1439
1440                                         /* pop 0 push 1 const */
1441                                         
1442                                 case ICMD_ICONST:
1443                                 case ICMD_LCONST:
1444                                 case ICMD_FCONST:
1445                                 case ICMD_DCONST:
1446                                 case ICMD_ACONST:
1447
1448                                         /* pop 0 push 1 load */
1449                                         
1450                                 case ICMD_ILOAD:
1451                                 case ICMD_LLOAD:
1452                                 case ICMD_FLOAD:
1453                                 case ICMD_DLOAD:
1454                                 case ICMD_ALOAD:
1455                                         NEW_TEMP_REG(iptr->dst.varindex);
1456                                         break;
1457
1458                                         /* pop 2 push 1 */
1459
1460                                 case ICMD_IALOAD:
1461                                 case ICMD_LALOAD:
1462                                 case ICMD_FALOAD:
1463                                 case ICMD_DALOAD:
1464                                 case ICMD_AALOAD:
1465
1466                                 case ICMD_BALOAD:
1467                                 case ICMD_CALOAD:
1468                                 case ICMD_SALOAD:
1469                                         FREE_TEMP_REG(iptr->sx.s23.s2.varindex);
1470                                         FREE_TEMP_REG(iptr->s1.varindex);
1471                                         NEW_TEMP_REG(iptr->dst.varindex);
1472                                         break;
1473
1474                                         /* pop 3 push 0 */
1475
1476                                 case ICMD_IASTORE:
1477                                 case ICMD_LASTORE:
1478                                 case ICMD_FASTORE:
1479                                 case ICMD_DASTORE:
1480                                 case ICMD_AASTORE:
1481
1482                                 case ICMD_BASTORE:
1483                                 case ICMD_CASTORE:
1484                                 case ICMD_SASTORE:
1485                                         FREE_TEMP_REG(iptr->sx.s23.s3.varindex);
1486                                         FREE_TEMP_REG(iptr->sx.s23.s2.varindex);
1487                                         FREE_TEMP_REG(iptr->s1.varindex);
1488                                         break;
1489
1490                                         /* pop 1 push 0 store */
1491
1492                                 case ICMD_ISTORE:
1493                                 case ICMD_LSTORE:
1494                                 case ICMD_FSTORE:
1495                                 case ICMD_DSTORE:
1496                                 case ICMD_ASTORE:
1497
1498                                         /* pop 1 push 0 */
1499
1500                                 case ICMD_POP:
1501
1502                                 case ICMD_IRETURN:
1503                                 case ICMD_LRETURN:
1504                                 case ICMD_FRETURN:
1505                                 case ICMD_DRETURN:
1506                                 case ICMD_ARETURN:
1507
1508                                 case ICMD_ATHROW:
1509
1510                                 case ICMD_PUTSTATIC:
1511                                 case ICMD_PUTFIELDCONST:
1512
1513                                         /* pop 1 push 0 branch */
1514
1515                                 case ICMD_IFNULL:
1516                                 case ICMD_IFNONNULL:
1517
1518                                 case ICMD_IFEQ:
1519                                 case ICMD_IFNE:
1520                                 case ICMD_IFLT:
1521                                 case ICMD_IFGE:
1522                                 case ICMD_IFGT:
1523                                 case ICMD_IFLE:
1524
1525                                 case ICMD_IF_LEQ:
1526                                 case ICMD_IF_LNE:
1527                                 case ICMD_IF_LLT:
1528                                 case ICMD_IF_LGE:
1529                                 case ICMD_IF_LGT:
1530                                 case ICMD_IF_LLE:
1531
1532                                         /* pop 1 push 0 table branch */
1533
1534                                 case ICMD_TABLESWITCH:
1535                                 case ICMD_LOOKUPSWITCH:
1536
1537                                 case ICMD_MONITORENTER:
1538                                 case ICMD_MONITOREXIT:
1539                                         FREE_TEMP_REG(iptr->s1.varindex);
1540                                         break;
1541
1542                                         /* pop 2 push 0 branch */
1543
1544                                 case ICMD_IF_ICMPEQ:
1545                                 case ICMD_IF_ICMPNE:
1546                                 case ICMD_IF_ICMPLT:
1547                                 case ICMD_IF_ICMPGE:
1548                                 case ICMD_IF_ICMPGT:
1549                                 case ICMD_IF_ICMPLE:
1550
1551                                 case ICMD_IF_LCMPEQ:
1552                                 case ICMD_IF_LCMPNE:
1553                                 case ICMD_IF_LCMPLT:
1554                                 case ICMD_IF_LCMPGE:
1555                                 case ICMD_IF_LCMPGT:
1556                                 case ICMD_IF_LCMPLE:
1557
1558                                 case ICMD_IF_ACMPEQ:
1559                                 case ICMD_IF_ACMPNE:
1560
1561                                         /* pop 2 push 0 */
1562
1563                                 case ICMD_POP2:
1564
1565                                 case ICMD_PUTFIELD:
1566
1567                                 case ICMD_IASTORECONST:
1568                                 case ICMD_LASTORECONST:
1569                                 case ICMD_AASTORECONST:
1570                                 case ICMD_BASTORECONST:
1571                                 case ICMD_CASTORECONST:
1572                                 case ICMD_SASTORECONST:
1573                                         FREE_TEMP_REG(iptr->sx.s23.s2.varindex);
1574                                         FREE_TEMP_REG(iptr->s1.varindex);
1575                                         break;
1576
1577                                         /* pop 0 push 1 copy */
1578                                         
1579                                 case ICMD_COPY:
1580                                         /* src === dst->prev (identical Stackslot Element)     */
1581                                         /* src --> dst       (copied value, take same reg/mem) */
1582
1583                                         if (!simplereg_alloc_dup(jd, iptr->s1.varindex, iptr->dst.varindex)) {
1584                                                 NEW_TEMP_REG(iptr->dst.varindex);
1585                                         } 
1586                                         else {
1587                                                 v = VAROP(iptr->dst);
1588
1589                                                 if (v->flags & INMEMORY) {
1590                                                         int32_t memindex = v->vv.regoff / SIZE_OF_STACKSLOT;
1591                                                         if (memindex >= rd->memcopycountsize) {
1592                                                                 int newsize = (memindex + 1) * 2;
1593                                                                 i = rd->memcopycountsize;
1594                                                                 rd->memcopycount = DMREALLOC(rd->memcopycount, int, i, newsize);
1595                                                                 MZERO(rd->memcopycount + i, int, newsize - i);
1596                                                                 rd->memcopycountsize = newsize;
1597                                                         }
1598                                                         rd->memcopycount[memindex]++;
1599                                                 }
1600                                                 else {
1601                                                         /* XXX split reg/mem variables on arm may need special handling here */
1602
1603                                                         s4 regindex = REG_INDEX(v->vv.regoff, v->type);
1604
1605                                                         rd->regcopycount[regindex]++;
1606                                                 }
1607                                         }
1608                                         break;
1609
1610                                         /* pop 1 push 1 move */
1611
1612                                 case ICMD_MOVE:
1613                                         if (!simplereg_alloc_dup(jd, iptr->s1.varindex, iptr->dst.varindex)) {
1614                                                 NEW_TEMP_REG(iptr->dst.varindex);
1615                                                 FREE_TEMP_REG(iptr->s1.varindex);
1616                                         }
1617                                         break;
1618
1619                                         /* pop 2 push 1 */
1620                                         
1621                                 case ICMD_IADD:
1622                                 case ICMD_ISUB:
1623                                 case ICMD_IMUL:
1624                                 case ICMD_IDIV:
1625                                 case ICMD_IREM:
1626
1627                                 case ICMD_ISHL:
1628                                 case ICMD_ISHR:
1629                                 case ICMD_IUSHR:
1630                                 case ICMD_IAND:
1631                                 case ICMD_IOR:
1632                                 case ICMD_IXOR:
1633
1634                                 case ICMD_LADD:
1635                                 case ICMD_LSUB:
1636                                 case ICMD_LMUL:
1637                                 case ICMD_LDIV:
1638                                 case ICMD_LREM:
1639
1640                                 case ICMD_LOR:
1641                                 case ICMD_LAND:
1642                                 case ICMD_LXOR:
1643
1644                                 case ICMD_LSHL:
1645                                 case ICMD_LSHR:
1646                                 case ICMD_LUSHR:
1647
1648                                 case ICMD_FADD:
1649                                 case ICMD_FSUB:
1650                                 case ICMD_FMUL:
1651                                 case ICMD_FDIV:
1652                                 case ICMD_FREM:
1653
1654                                 case ICMD_DADD:
1655                                 case ICMD_DSUB:
1656                                 case ICMD_DMUL:
1657                                 case ICMD_DDIV:
1658                                 case ICMD_DREM:
1659
1660                                 case ICMD_LCMP:
1661                                 case ICMD_FCMPL:
1662                                 case ICMD_FCMPG:
1663                                 case ICMD_DCMPL:
1664                                 case ICMD_DCMPG:
1665                                         FREE_TEMP_REG(iptr->sx.s23.s2.varindex);
1666                                         FREE_TEMP_REG(iptr->s1.varindex);
1667                                         NEW_TEMP_REG(iptr->dst.varindex);
1668                                         break;
1669
1670                                         /* pop 1 push 1 */
1671                                         
1672                                 case ICMD_IADDCONST:
1673                                 case ICMD_ISUBCONST:
1674                                 case ICMD_IMULCONST:
1675                                 case ICMD_IMULPOW2:
1676                                 case ICMD_IDIVPOW2:
1677                                 case ICMD_IREMPOW2:
1678                                 case ICMD_IANDCONST:
1679                                 case ICMD_IORCONST:
1680                                 case ICMD_IXORCONST:
1681                                 case ICMD_ISHLCONST:
1682                                 case ICMD_ISHRCONST:
1683                                 case ICMD_IUSHRCONST:
1684
1685                                 case ICMD_LADDCONST:
1686                                 case ICMD_LSUBCONST:
1687                                 case ICMD_LMULCONST:
1688                                 case ICMD_LMULPOW2:
1689                                 case ICMD_LDIVPOW2:
1690                                 case ICMD_LREMPOW2:
1691                                 case ICMD_LANDCONST:
1692                                 case ICMD_LORCONST:
1693                                 case ICMD_LXORCONST:
1694                                 case ICMD_LSHLCONST:
1695                                 case ICMD_LSHRCONST:
1696                                 case ICMD_LUSHRCONST:
1697
1698                                 case ICMD_INEG:
1699                                 case ICMD_INT2BYTE:
1700                                 case ICMD_INT2CHAR:
1701                                 case ICMD_INT2SHORT:
1702                                 case ICMD_LNEG:
1703                                 case ICMD_FNEG:
1704                                 case ICMD_DNEG:
1705
1706                                 case ICMD_I2L:
1707                                 case ICMD_I2F:
1708                                 case ICMD_I2D:
1709                                 case ICMD_L2I:
1710                                 case ICMD_L2F:
1711                                 case ICMD_L2D:
1712                                 case ICMD_F2I:
1713                                 case ICMD_F2L:
1714                                 case ICMD_F2D:
1715                                 case ICMD_D2I:
1716                                 case ICMD_D2L:
1717                                 case ICMD_D2F:
1718
1719                                 case ICMD_CHECKCAST:
1720
1721                                 case ICMD_ARRAYLENGTH:
1722                                 case ICMD_INSTANCEOF:
1723
1724                                 case ICMD_NEWARRAY:
1725                                 case ICMD_ANEWARRAY:
1726
1727                                 case ICMD_GETFIELD:
1728                                         FREE_TEMP_REG(iptr->s1.varindex);
1729                                         NEW_TEMP_REG(iptr->dst.varindex);
1730                                         break;
1731
1732                                         /* pop 0 push 1 */
1733                                         
1734                                 case ICMD_GETSTATIC:
1735
1736                                 case ICMD_NEW:
1737                                         NEW_TEMP_REG(iptr->dst.varindex);
1738                                         break;
1739
1740                                         /* pop many push any */
1741                                         
1742                                 case ICMD_INVOKESTATIC:
1743                                 case ICMD_INVOKESPECIAL:
1744                                 case ICMD_INVOKEVIRTUAL:
1745                                 case ICMD_INVOKEINTERFACE:
1746                                         INSTRUCTION_GET_METHODDESC(iptr,md);
1747                                         i = md->paramcount;
1748                                         argp = iptr->sx.s23.s2.args;
1749                                         while (--i >= 0) {
1750                                                 FREE_TEMP_REG(*argp);
1751                                                 argp++;
1752                                         }
1753                                         if (md->returntype.type != TYPE_VOID)
1754                                                 NEW_TEMP_REG(iptr->dst.varindex);
1755                                         break;
1756
1757                                 case ICMD_BUILTIN:
1758                                         bte = iptr->sx.s23.s3.bte;
1759                                         md = bte->md;
1760                                         i = md->paramcount;
1761                                         argp = iptr->sx.s23.s2.args;
1762                                         while (--i >= 0) {
1763                                                 FREE_TEMP_REG(*argp);
1764                                                 argp++;
1765                                         }
1766                                         if (md->returntype.type != TYPE_VOID)
1767                                                 NEW_TEMP_REG(iptr->dst.varindex);
1768                                         break;
1769
1770                                 case ICMD_MULTIANEWARRAY:
1771                                         i = iptr->s1.argcount;
1772                                         argp = iptr->sx.s23.s2.args;
1773                                         while (--i >= 0) {
1774                                                 FREE_TEMP_REG(*argp);
1775                                                 argp++;
1776                                         }
1777                                         NEW_TEMP_REG(iptr->dst.varindex);
1778                                         break;
1779
1780                                 default:
1781                                         exceptions_throw_internalerror("Unknown ICMD %d during register allocation",
1782                                                                                                    iptr->opc);
1783                                         return;
1784                                 } /* switch */
1785                                 iptr++;
1786                         } /* while instructions */
1787                 } /* if */
1788                 bptr = bptr->next;
1789         } /* while blocks */
1790 }
1791
1792
1793 #if defined(ENABLE_STATISTICS)
1794 void simplereg_make_statistics(jitdata *jd)
1795 {
1796         methodinfo   *m;
1797         codegendata  *cd;
1798         registerdata *rd;
1799         int i;
1800         s4 len;
1801 #if 0
1802         stackelement_t*    src, src_old;
1803         stackelement_t*    dst;
1804         instruction *iptr;
1805 #endif
1806         basicblock  *bptr;
1807         int size_interface; /* == maximum size of in/out stack at basic block boundaries */
1808         bool in_register;
1809         varinfo *var;
1810
1811         /* get required compiler data */
1812
1813         m  = jd->m;
1814         cd = jd->cd;
1815         rd = jd->rd;
1816
1817         in_register = true;
1818
1819         size_interface = 0;
1820
1821                 /* count how many local variables are held in memory or register */
1822                 for(i=0; i < jd->localcount; i++) {
1823                         if (VAR(i)->flags & INMEMORY) {
1824                                 count_locals_spilled++;
1825                                 in_register=false;
1826                         }
1827                         else {
1828                                 count_locals_register++;
1829                         }
1830                 }
1831
1832                 /* count how many stack slots are held in memory or register */
1833
1834                 bptr = jd->basicblocks;
1835
1836                 while (bptr != NULL) {
1837                         if (bptr->flags >= BBREACHED) {
1838
1839 #if defined(ENABLE_LSRA) || defined(ENABLE_SSA)
1840                         if (!opt_lsra) {
1841 #endif  
1842                                 /* check for memory moves from interface to BB instack */
1843                                 len = bptr->indepth;
1844                                 
1845                                 if (len > size_interface) size_interface = len;
1846
1847                                 while (len) {
1848                                         len--;
1849                                         var = VAR(bptr->invars[len]);
1850
1851                                         /* invars statistics (currently none) */
1852                                 }
1853
1854                                 /* check for memory moves from BB outstack to interface */
1855                                 len = bptr->outdepth;
1856                                 if (len > size_interface) size_interface = len;
1857
1858                                 while (len) {
1859                                         len--;
1860                                         var = VAR(bptr->outvars[len]);
1861
1862                                         /* outvars statistics (currently none) */
1863                                 }
1864 #if defined(ENABLE_LSRA) || defined(ENABLE_SSA)
1865                         }
1866 #endif  
1867
1868
1869 #if 0
1870                                 dst = bptr->instack;
1871                                 iptr = bptr->iinstr;
1872                                 len = bptr->icount;
1873                                 src_old = NULL;
1874
1875                                 while (--len >= 0)  {
1876                                         src = dst;
1877                                         dst = iptr->dst.var;
1878
1879                                         if ((src!= NULL) && (src != src_old)) { /* new stackslot */
1880                                                 switch (src->varkind) {
1881                                                 case TEMPVAR:
1882                                                 case STACKVAR:
1883                                                         if (!(src->flags & INMEMORY)) 
1884                                                                 count_ss_register++;
1885                                                         else {
1886                                                                 count_ss_spilled++;
1887                                                                 in_register=false;
1888                                                         }                               
1889                                                         break;
1890                                                         /*                                      case LOCALVAR: */
1891                                                         /*                                              if (!(rd->locals[src->varnum][src->type].flags & INMEMORY)) */
1892                                                         /*                                                      count_ss_register++; */
1893                                                         /*                                              else */
1894                                                         /*                                                      count_ss_spilled++; */
1895                                                         /*                                              break; */
1896                                                 case ARGVAR:
1897                                                         if (!(src->flags & INMEMORY)) 
1898                                                                 count_argument_mem_ss++;
1899                                                         else
1900                                                                 count_argument_reg_ss++;
1901                                                         break;
1902
1903
1904                                                         /*                                              if (IS_FLT_DBL_TYPE(src->type)) { */
1905                                                         /*                                                      if (src->varnum < FLT_ARG_CNT) { */
1906                                                         /*                                                              count_ss_register++; */
1907                                                         /*                                                              break; */
1908                                                         /*                                                      } */
1909                                                         /*                                              } else { */
1910                                                         /* #if defined(__POWERPC__) */
1911                                                         /*                                                      if (src->varnum < INT_ARG_CNT - (IS_2_WORD_TYPE(src->type) != 0)) { */
1912                                                         /* #else */
1913                                                         /*                                                      if (src->varnum < INT_ARG_CNT) { */
1914                                                         /* #endif */
1915                                                         /*                                                              count_ss_register++; */
1916                                                         /*                                                              break; */
1917                                                         /*                                                      } */
1918                                                         /*                                              } */
1919                                                         /*                                              count_ss_spilled++; */
1920                                                         /*                                              break; */
1921                                                 }
1922                                         }
1923                                         src_old = src;
1924                                         
1925                                         iptr++;
1926                                 } /* while instructions */
1927 #endif
1928                         } /* if */
1929
1930                         bptr = bptr->next;
1931                 } /* while blocks */
1932
1933                 count_interface_size += size_interface; /* accummulate the size of the interface (between bb boundaries) */
1934                 if (in_register) count_method_in_register++;
1935                 if (in_register) {
1936 /*                      printf("INREGISTER: %s%s%s\n",m->class->name->text, m->name->text, m->descriptor->text); */
1937                 }
1938 }
1939 #endif /* defined(ENABLE_STATISTICS) */
1940
1941
1942 /*
1943  * These are local overrides for various environment variables in Emacs.
1944  * Please do not remove this and leave it at the end of the file, where
1945  * Emacs will automagically detect them.
1946  * ---------------------------------------------------------------------
1947  * Local variables:
1948  * mode: c
1949  * indent-tabs-mode: t
1950  * c-basic-offset: 4
1951  * tab-width: 4
1952  * End:
1953  * vim:noexpandtab:sw=4:ts=4:
1954  */