8449ce8a973e323334b0b90e34add1ff83f77e63
[cacao.git] / src / vm / jit / allocator / simplereg.c
1 /* src/vm/jit/allocator/simplereg.c - register allocator
2
3    Copyright (C) 1996-2005, 2007 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4    R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5    C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6    Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23    02111-1307, USA.
24
25    $Id: simplereg.c 8320 2007-08-16 11:35:14Z michi $
26
27 */
28
29
30 #include "config.h"
31
32 #include <assert.h>
33 #include <stdint.h>
34
35 #include "vm/types.h"
36
37 #include "arch.h"
38 #include "md-abi.h"
39
40 #include "mm/memory.h"
41
42 #include "vm/builtin.h"
43 #include "vm/exceptions.h"
44 #include "vm/resolve.h"
45 #include "vm/stringlocal.h"
46
47 #include "vm/jit/abi.h"
48 #include "vm/jit/reg.h"
49 #include "vm/jit/show.h"
50 #include "vm/jit/allocator/simplereg.h"
51
52 #include "vmcore/method.h"
53 #include "vmcore/options.h"
54
55
56 #if 0
57 #    define LOG(args) printf args
58 #else
59 #    define LOG(args)
60 #endif
61
62
63 /* function prototypes for this file ******************************************/
64
65 static void simplereg_allocate_interfaces(jitdata *jd);
66 static void simplereg_allocate_locals(jitdata *jd);
67 static void simplereg_allocate_temporaries(jitdata *jd);
68
69
70 /* size of a stackslot used by the internal ABI */
71
72 #if defined(HAS_4BYTE_STACKSLOT)
73 # define SIZE_OF_STACKSLOT 4
74 #else 
75 # define SIZE_OF_STACKSLOT 8
76 #endif
77
78
79 /* total number of registers */
80
81 #if defined(HAS_ADDRESS_REGISTER_FILE)
82 #define TOTAL_REG_CNT  (INT_REG_CNT + FLT_REG_CNT + ADR_REG_CNT)
83 #else
84 #define TOTAL_REG_CNT  (INT_REG_CNT + FLT_REG_CNT)
85 #endif
86
87
88 /* macros for handling register stacks ****************************************/
89
90 #define AVAIL_FRONT(cnt, limit)   ((cnt) < (limit))
91 #define AVAIL_BACK(cnt)           ((cnt) > 0)
92
93 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
94 #define AVAIL_FRONT_INT(cnt, limit)   ((cnt) < (limit) - intregsneeded)
95 #define AVAIL_BACK_INT(cnt)           ((cnt) > intregsneeded)
96 #else
97 #define AVAIL_FRONT_INT(cnt, limit)   AVAIL_FRONT(cnt, limit)
98 #define AVAIL_BACK_INT(cnt)           AVAIL_BACK(cnt)
99 #endif
100
101 #define POP_FRONT(stk, cnt, reg)   do {  reg = stk[cnt++];    } while (0)
102 #define POP_BACK(stk, cnt, reg)    do {  reg = stk[--cnt];    } while (0)
103 #define PUSH_FRONT(stk, cnt, reg)  do {  stk[--cnt] = (reg);  } while (0)
104 #define PUSH_BACK(stk, cnt, reg)   do {  stk[cnt++] = (reg);  } while (0)
105
106 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
107 #define POP_FRONT_INT(stk, cnt, reg)                                 \
108     do {                                                             \
109         if (intregsneeded) {                                         \
110             reg = PACK_REGS(stk[cnt], stk[cnt+1]);                   \
111             cnt += 2;                                                \
112         }                                                            \
113         else                                                         \
114             POP_FRONT(stk, cnt, reg);                                \
115     } while (0)
116 #else
117 #define POP_FRONT_INT(stk, cnt, reg)  POP_FRONT(stk, cnt, reg)
118 #endif
119
120 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
121 #define POP_BACK_INT(stk, cnt, reg)                                  \
122     do {                                                             \
123         if (intregsneeded) {                                         \
124             cnt -= 2;                                                \
125             reg = PACK_REGS(stk[cnt], stk[cnt+1]);                   \
126         }                                                            \
127         else                                                         \
128             POP_BACK(stk, cnt, reg);                                 \
129     } while (0)
130 #else
131 #define POP_BACK_INT(stk, cnt, reg)  POP_BACK(stk, cnt, reg)
132 #endif
133
134 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
135 #define PUSH_BACK_INT(stk, cnt, reg)                                 \
136     do {                                                             \
137         if (intregsneeded) {                                         \
138             stk[cnt] = GET_LOW_REG(reg);                             \
139             stk[cnt + 1] = GET_HIGH_REG(reg);                        \
140             cnt += 2;                                                \
141         }                                                            \
142         else                                                         \
143             PUSH_BACK(stk, cnt, reg);                                \
144     } while (0)
145 #else
146 #define PUSH_BACK_INT(stk, cnt, reg)  PUSH_BACK(stk, cnt, reg)
147 #endif
148
149 #define AVAIL_ARG_FLT  AVAIL_FRONT(rd->argfltreguse, FLT_ARG_CNT)
150 #define AVAIL_TMP_FLT  AVAIL_BACK(rd->tmpfltreguse)
151 #define AVAIL_SAV_FLT  AVAIL_BACK(rd->savfltreguse)
152
153 #define AVAIL_ARG_ADR  AVAIL_FRONT(rd->argadrreguse, ADR_ARG_CNT)
154 #define AVAIL_TMP_ADR  AVAIL_BACK(rd->tmpadrreguse)
155 #define AVAIL_SAV_ADR  AVAIL_BACK(rd->savadrreguse)
156
157 #define AVAIL_ARG_INT  AVAIL_FRONT_INT(rd->argintreguse, INT_ARG_CNT)
158 #define AVAIL_TMP_INT  AVAIL_BACK_INT(rd->tmpintreguse)
159 #define AVAIL_SAV_INT  AVAIL_BACK_INT(rd->savintreguse)
160
161 #define AVAIL_FREE_ARG_FLT  AVAIL_BACK(rd->freeargflttop)
162 #define AVAIL_FREE_TMP_FLT  AVAIL_BACK(rd->freetmpflttop)
163 #define AVAIL_FREE_SAV_FLT  AVAIL_BACK(rd->freesavflttop)
164
165 #define AVAIL_FREE_ARG_ADR  AVAIL_BACK(rd->freeargadrtop)
166 #define AVAIL_FREE_TMP_ADR  AVAIL_BACK(rd->freetmpadrtop)
167 #define AVAIL_FREE_SAV_ADR  AVAIL_BACK(rd->freesavadrtop)
168
169 #define AVAIL_FREE_ARG_INT  AVAIL_BACK_INT(rd->freearginttop)
170 #define AVAIL_FREE_TMP_INT  AVAIL_BACK_INT(rd->freetmpinttop)
171 #define AVAIL_FREE_SAV_INT  AVAIL_BACK_INT(rd->freesavinttop)
172
173 #define TAKE_ARG_FLT(r)  POP_FRONT(abi_registers_float_argument, rd->argfltreguse, r)
174 #define TAKE_TMP_FLT(r)  POP_BACK(rd->tmpfltregs, rd->tmpfltreguse, r)
175 #define TAKE_SAV_FLT(r)  POP_BACK(rd->savfltregs, rd->savfltreguse, r)
176
177 #define TAKE_ARG_ADR(r)  POP_FRONT(rd->argadrregs, rd->argadrreguse, r)
178 #define TAKE_TMP_ADR(r)  POP_BACK(rd->tmpadrregs, rd->tmpadrreguse, r)
179 #define TAKE_SAV_ADR(r)  POP_BACK(rd->savadrregs, rd->savadrreguse, r)
180
181 #define TAKE_ARG_INT(r)  POP_FRONT_INT(abi_registers_integer_argument, rd->argintreguse, r)
182 #define TAKE_TMP_INT(r)  POP_BACK_INT(rd->tmpintregs, rd->tmpintreguse, r)
183 #define TAKE_SAV_INT(r)  POP_BACK_INT(rd->savintregs, rd->savintreguse, r)
184
185 #define TAKE_FREE_ARG_FLT(r)  POP_BACK(rd->freeargfltregs, rd->freeargflttop, r)
186 #define TAKE_FREE_TMP_FLT(r)  POP_BACK(rd->freetmpfltregs, rd->freetmpflttop, r)
187 #define TAKE_FREE_SAV_FLT(r)  POP_BACK(rd->freesavfltregs, rd->freesavflttop, r)
188
189 #define TAKE_FREE_ARG_ADR(r)  POP_BACK(rd->freeargadrregs, rd->freeargadrtop, r)
190 #define TAKE_FREE_TMP_ADR(r)  POP_BACK(rd->freetmpadrregs, rd->freetmpadrtop, r)
191 #define TAKE_FREE_SAV_ADR(r)  POP_BACK(rd->freesavadrregs, rd->freesavadrtop, r)
192
193 #define TAKE_FREE_ARG_INT(r)  POP_BACK_INT(rd->freeargintregs, rd->freearginttop, r)
194 #define TAKE_FREE_TMP_INT(r)  POP_BACK_INT(rd->freetmpintregs, rd->freetmpinttop, r)
195 #define TAKE_FREE_SAV_INT(r)  POP_BACK_INT(rd->freesavintregs, rd->freesavinttop, r)
196
197 #define PUSH_FREE_ARG_FLT(r)  PUSH_BACK(rd->freeargfltregs, rd->freeargflttop, r)
198 #define PUSH_FREE_TMP_FLT(r)  PUSH_BACK(rd->freetmpfltregs, rd->freetmpflttop, r)
199 #define PUSH_FREE_SAV_FLT(r)  PUSH_BACK(rd->freesavfltregs, rd->freesavflttop, r)
200
201 #define PUSH_FREE_ARG_ADR(r)  PUSH_BACK(rd->freeargadrregs, rd->freeargadrtop, r)
202 #define PUSH_FREE_TMP_ADR(r)  PUSH_BACK(rd->freetmpadrregs, rd->freetmpadrtop, r)
203 #define PUSH_FREE_SAV_ADR(r)  PUSH_BACK(rd->freesavadrregs, rd->freesavadrtop, r)
204
205 #define PUSH_FREE_ARG_INT(r)  PUSH_BACK_INT(rd->freeargintregs, rd->freearginttop, r)
206 #define PUSH_FREE_TMP_INT(r)  PUSH_BACK_INT(rd->freetmpintregs, rd->freetmpinttop, r)
207 #define PUSH_FREE_SAV_INT(r)  PUSH_BACK_INT(rd->freesavintregs, rd->freesavinttop, r)
208
209
210 /* macros for allocating memory slots ****************************************/
211
212 #define NEW_MEM_SLOT(r)                                              \
213     do {                                                             \
214         (r) = rd->memuse * SIZE_OF_STACKSLOT;                        \
215         rd->memuse += memneeded + 1;                                 \
216     } while (0)
217
218 #define NEW_MEM_SLOT_ALIGNED(r)                                      \
219     do {                                                             \
220         if ( (memneeded) && (rd->memuse & 1))                        \
221             rd->memuse++;                                            \
222         (r) = rd->memuse * SIZE_OF_STACKSLOT;                        \
223         rd->memuse += memneeded + 1;                                 \
224     } while (0)
225
226 #define NEW_MEM_SLOT_ALIGNED_REUSE_PADDING(r)                        \
227     do {                                                             \
228         if ( (memneeded) && (rd->memuse & 1)) {                      \
229                         PUSH_BACK(rd->freemem, rd->freememtop, rd->memuse);      \
230             rd->memuse++;                                            \
231                 }                                                            \
232         (r) = rd->memuse * SIZE_OF_STACKSLOT;                        \
233         rd->memuse += memneeded + 1;                                 \
234     } while (0)
235
236 #if defined(ALIGN_LONGS_IN_MEMORY)
237 #define NEW_MEM_SLOT_INT_LNG(r)  NEW_MEM_SLOT_ALIGNED(r)
238 #else
239 #define NEW_MEM_SLOT_INT_LNG(r)  NEW_MEM_SLOT(r)
240 #endif
241
242 #if defined(ALIGN_DOUBLES_IN_MEMORY)
243 #define NEW_MEM_SLOT_FLT_DBL(r)  NEW_MEM_SLOT_ALIGNED(r)
244 #else
245 #define NEW_MEM_SLOT_FLT_DBL(r)  NEW_MEM_SLOT(r)
246 #endif
247
248 #if defined(ALIGN_LONGS_IN_MEMORY) || defined(ALIGN_DOUBLES_IN_MEMORY)
249 #define NEW_MEM_SLOT_REUSE_PADDING(r)  NEW_MEM_SLOT_ALIGNED_REUSE_PADDING(r)
250 #else
251 #define NEW_MEM_SLOT_REUSE_PADDING(r)  NEW_MEM_SLOT(r)
252 #endif
253
254
255 /* macros for creating/freeing temporary variables ***************************/
256
257 #define NEW_TEMP_REG(index)                                          \
258     if ( ((index) >= jd->localcount)                                 \
259          && (!(VAR(index)->flags & (INOUT | PREALLOC))) )            \
260         simplereg_new_temp(jd, (index))
261
262
263 #define FREE_TEMP_REG(index)                                         \
264     if (((index) > jd->localcount)                                   \
265         && (!(VAR(index)->flags & (PREALLOC))))                      \
266         simplereg_free_temp(jd, (index))
267
268
269 /* macro for getting a unique register index *********************************/
270
271 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
272 #define REG_INDEX_NON_ADR(regoff, type)                              \
273     (IS_FLT_DBL_TYPE(type) ? (INT_REG_CNT + (regoff)) : (GET_LOW_REG(regoff)))
274 #else
275 #define REG_INDEX_NON_ADR(regoff, type)                              \
276     (IS_FLT_DBL_TYPE(type) ? (INT_REG_CNT + (regoff)) : (regoff))
277 #endif
278
279 #if defined(HAS_ADDRESS_REGISTER_FILE)
280 #define REG_INDEX(regoff, type)                                      \
281     (IS_ADR_TYPE(type) ? (regoff) : (ADR_REG_CNT + REG_INDEX_NON_ADR(regoff, type)))
282 #else
283 #define REG_INDEX(regoff, type)  REG_INDEX_NON_ADR(regoff, type)
284 #endif
285
286
287 /* regalloc ********************************************************************
288
289    Does a simple register allocation.
290         
291 *******************************************************************************/
292         
293 bool regalloc(jitdata *jd)
294 {
295         /* There is a problem with the use of unused float argument
296            registers in leafmethods for stackslots on c7 (2 * Dual Core
297            AMD Opteron(tm) Processor 270) - runtime for the jvm98 _mtrt
298            benchmark is heaviliy increased. This could be prevented by
299            setting rd->argfltreguse to FLT_ARG_CNT before calling
300            simplereg_allocate_temporaries and setting it back to the original
301            value before calling simplereg_allocate_locals.  */
302
303         simplereg_allocate_interfaces(jd);
304         simplereg_allocate_temporaries(jd);
305         simplereg_allocate_locals(jd);
306
307         /* everthing's ok */
308
309         return true;
310 }
311
312
313 /* simplereg_allocate_interfaces ***********************************************
314
315    Allocates registers for all interface variables.
316         
317 *******************************************************************************/
318         
319 static void simplereg_allocate_interfaces(jitdata *jd)
320 {
321         methodinfo   *m;
322         codegendata  *cd;
323         registerdata *rd;
324
325         int     s, t, tt, saved;
326         int     intalloc, fltalloc; /* Remember allocated Register/Memory offset */
327                         /* in case more vars are packed into this interface slot */
328         int             memneeded = 0;
329         /* Allocate LNG and DBL types first to ensure 2 memory slots or          */
330         /* registers on HAS_4BYTE_STACKSLOT architectures.                       */
331         int     typeloop[] = { TYPE_LNG, TYPE_DBL, TYPE_INT, TYPE_FLT, TYPE_ADR };
332         int     flags, regoff;
333 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
334         int             intregsneeded;
335 #endif
336
337         /* get required compiler data */
338
339         m  = jd->m;
340         cd = jd->cd;
341         rd = jd->rd;
342
343         /* rd->memuse was already set in stack.c to allocate stack space
344            for passing arguments to called methods. */
345
346 #if defined(__I386__)
347         if (checksync && (m->flags & ACC_SYNCHRONIZED)) {
348                 /* reserve 0(%esp) for Monitorenter/exit Argument on i386 */
349                 if (rd->memuse < 1)
350                         rd->memuse = 1;
351         }
352 #endif
353
354         if (jd->isleafmethod) {
355                 /* Reserve argument register, which will be used for Locals acting */
356                 /* as Parameters */
357                 if (rd->argintreguse < m->parseddesc->argintreguse)
358                         rd->argintreguse = m->parseddesc->argintreguse;
359                 if (rd->argfltreguse < m->parseddesc->argfltreguse)
360                         rd->argfltreguse = m->parseddesc->argfltreguse;
361 #ifdef HAS_ADDRESS_REGISTER_FILE
362                 if (rd->argadrreguse < m->parseddesc->argadrreguse)
363                         rd->argadrreguse = m->parseddesc->argadrreguse;
364 #endif
365         }
366
367         for (s = 0; s < jd->maxinterfaces; s++) {
368                 intalloc = -1; fltalloc = -1;
369
370                 /* check if the interface at this stack depth must be a SAVEDVAR */
371
372                 saved = 0;
373
374                 for (tt = 0; tt <=4; tt++) {
375                         if ((t = jd->interface_map[s * 5 + tt].flags) != UNUSED) {
376                                 saved |= t & SAVEDVAR;
377                         }
378                 }
379
380                 /* allocate reg/mem for each type the interface is used as */
381
382                 for (tt = 0; tt <= 4; tt++) {
383                         t = typeloop[tt];
384                         if (jd->interface_map[s * 5 + t].flags == UNUSED)
385                                 continue;
386
387                         flags = saved;
388                         regoff = -1; /* initialize to invalid value */
389
390 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
391                         intregsneeded = (IS_2_WORD_TYPE(t)) ? 1 : 0;
392 #endif
393
394 #if defined(HAS_4BYTE_STACKSLOT)
395                         memneeded = (IS_2_WORD_TYPE(t)) ? 1 : 0;
396 #endif
397
398                         if (!saved) {
399 #if defined(HAS_ADDRESS_REGISTER_FILE)
400                                 if (IS_ADR_TYPE(t)) {
401                                         if (!jd->isleafmethod && AVAIL_ARG_ADR) {
402                                                 flags |= ARGREG;
403                                                 TAKE_ARG_ADR(regoff);
404                                         } 
405                                         else if (AVAIL_TMP_ADR) {
406                                                 TAKE_TMP_ADR(regoff);
407                                         } 
408                                         else if (AVAIL_SAV_ADR) {
409                                                 flags |= SAVREG;
410                                                 TAKE_SAV_ADR(regoff);
411                                         } 
412                                         else {
413                                                 flags |= INMEMORY;
414                                                 regoff = rd->memuse++ * SIZE_OF_STACKSLOT;
415                                         }                                               
416                                 } 
417                                 else /* !IS_ADR_TYPE */
418 #endif /* defined(HAS_ADDRESS_REGISTER_FILE) */
419                                 {
420                                         if (IS_FLT_DBL_TYPE(t)) {
421                                                 if (fltalloc >= 0) {
422                                                         /* Reuse memory slot(s)/register(s) for shared interface slots */
423                                                         flags |= jd->interface_map[fltalloc].flags & ~SAVEDVAR;
424                                                         regoff = jd->interface_map[fltalloc].regoff;
425                                                 } 
426                                                 else if (AVAIL_ARG_FLT) {
427                                                         flags |= ARGREG;
428                                                         TAKE_ARG_FLT(regoff);
429                                                 } 
430                                                 else if (AVAIL_TMP_FLT) {
431                                                         TAKE_TMP_FLT(regoff);
432                                                 } 
433                                                 else if (AVAIL_SAV_FLT) {
434                                                         flags |= SAVREG;
435                                                         TAKE_SAV_FLT(regoff);
436                                                 } 
437                                                 else {
438                                                         flags |= INMEMORY;
439                                                         NEW_MEM_SLOT_FLT_DBL(regoff);
440                                                 }
441                                                 fltalloc = s * 5 + t;
442                                         }
443                                         else { /* !IS_FLT_DBL_TYPE(t) */
444 #if (SIZEOF_VOID_P == 4) && !defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
445                                                 /*
446                                                  * for i386 put all longs in memory
447                                                  */
448                                                 if (IS_2_WORD_TYPE(t)) {
449                                                         flags |= INMEMORY;
450                                                         NEW_MEM_SLOT_INT_LNG(regoff);
451                                                 } 
452                                                 else
453 #endif
454                                                         if (intalloc >= 0) {
455                                                                 /* Reuse memory slot(s)/register(s) for shared interface slots */
456                                                                 flags |= jd->interface_map[intalloc].flags & ~SAVEDVAR;
457                                                                 regoff = jd->interface_map[intalloc].regoff;
458 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
459                                                                 /* reuse lower half */
460                                                                 if (!(flags & INMEMORY) 
461                                                                                 && IS_2_WORD_TYPE(intalloc % 5))
462                                                                         regoff = GET_LOW_REG(regoff);
463 #endif
464                                                         } 
465                                                         else {
466                                                                 if (AVAIL_ARG_INT) {
467                                                                         flags |= ARGREG;
468                                                                         TAKE_ARG_INT(regoff);
469                                                                 }
470                                                                 else if (AVAIL_TMP_INT) {
471                                                                         TAKE_TMP_INT(regoff);
472                                                                 }
473                                                                 else if (AVAIL_SAV_INT) {
474                                                                         flags |= SAVREG;
475                                                                         TAKE_SAV_INT(regoff);
476                                                                 }
477                                                                 else {
478                                                                         flags |= INMEMORY;
479                                                                         NEW_MEM_SLOT_INT_LNG(regoff);
480                                                                 }
481                                                         }
482
483                                                 intalloc = s * 5 + t;
484                                         } /* if (IS_FLT_DBL_TYPE(t)) */
485                                 } 
486                         } 
487                         else { /* (saved) */
488                                 /* now the same like above, but without a chance to take a temporary register */
489 #ifdef HAS_ADDRESS_REGISTER_FILE
490                                 if (IS_ADR_TYPE(t)) {
491                                         if (AVAIL_SAV_ADR) {
492                                                 TAKE_SAV_ADR(regoff);
493                                         }
494                                         else {
495                                                 flags |= INMEMORY;
496                                                 regoff = rd->memuse++ * SIZE_OF_STACKSLOT;
497                                         }                                               
498                                 } 
499                                 else
500 #endif
501                                 {
502                                         if (IS_FLT_DBL_TYPE(t)) {
503                                                 if (fltalloc >= 0) {
504                                                         flags |= jd->interface_map[fltalloc].flags & ~SAVEDVAR;
505                                                         regoff = jd->interface_map[fltalloc].regoff;
506                                                 } 
507                                                 else {
508                                                         if (AVAIL_SAV_FLT) {
509                                                                 TAKE_SAV_FLT(regoff);
510                                                         }
511                                                         else {
512                                                                 flags |= INMEMORY;
513                                                                 NEW_MEM_SLOT_FLT_DBL(regoff);
514                                                         }
515                                                 }
516                                                 fltalloc = s * 5 + t;
517                                         }
518                                         else { /* IS_INT_LNG */
519 #if (SIZEOF_VOID_P == 4) && !defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
520                                                 /*
521                                                  * for i386 put all longs in memory
522                                                  */
523                                                 if (IS_2_WORD_TYPE(t)) {
524                                                         flags |= INMEMORY;
525                                                         NEW_MEM_SLOT_INT_LNG(regoff);
526                                                 } 
527                                                 else
528 #endif
529                                                 {
530                                                         if (intalloc >= 0) {
531                                                                 flags |= jd->interface_map[intalloc].flags & ~SAVEDVAR;
532                                                                 regoff = jd->interface_map[intalloc].regoff;
533 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
534                                                                 /*  reuse lower half */
535                                                                 if (!(flags & INMEMORY)
536                                                                                 && IS_2_WORD_TYPE(intalloc % 5))
537                                                                         regoff = GET_LOW_REG(regoff);
538 #endif
539                                                         } 
540                                                         else {
541                                                                 if (AVAIL_SAV_INT) {
542                                                                         TAKE_SAV_INT(regoff);
543                                                                 } 
544                                                                 else {
545                                                                         flags |= INMEMORY;
546                                                                         NEW_MEM_SLOT_INT_LNG(regoff);
547                                                                 }
548                                                         }
549                                                         intalloc = s*5 + t;
550                                                 }
551                                         } /* if (IS_FLT_DBL_TYPE(t) else */
552                                 } /* if (IS_ADR_TYPE(t)) else */
553                         } /* if (saved) else */
554                         /* if (type >= 0) */
555
556                         assert(regoff >= 0);
557                         jd->interface_map[5*s + t].flags = flags | INOUT;
558                         jd->interface_map[5*s + t].regoff = regoff;
559                 } /* for t */
560         } /* for s */
561 }
562
563
564 /* simplereg_allocate_locals_leafmethod ****************************************
565
566    Allocates registers for all local variables of a leafmethod.
567         
568 *******************************************************************************/
569         
570 static void simplereg_allocate_locals_leafmethod(jitdata *jd)
571 {
572         methodinfo   *m;
573         codegendata  *cd;
574         registerdata *rd;
575         methoddesc *md;
576
577         int     p, s, t, tt, varindex;
578         int     intalloc, fltalloc;
579         varinfo *v;
580         int     intregsneeded = 0;
581         int     memneeded = 0;
582         int     typeloop[] = { TYPE_LNG, TYPE_DBL, TYPE_INT, TYPE_FLT, TYPE_ADR };
583         int     fargcnt, iargcnt;
584 #ifdef HAS_ADDRESS_REGISTER_FILE
585         int     aargcnt;
586 #endif
587
588         /* get required compiler data */
589
590         m  = jd->m;
591         cd = jd->cd;
592         rd = jd->rd;
593
594         md = m->parseddesc;
595
596         iargcnt = rd->argintreguse;
597         fargcnt = rd->argfltreguse;
598 #ifdef HAS_ADDRESS_REGISTER_FILE
599         aargcnt = rd->argadrreguse;
600 #endif
601         for (p = 0, s = 0; s < jd->maxlocals; s++, p++) {
602                 intalloc = -1; fltalloc = -1;
603                 for (tt = 0; tt <= 4; tt++) {
604                         t = typeloop[tt];
605                         varindex = jd->local_map[s * 5 + t];
606                         if (varindex == UNUSED)
607                                 continue;
608
609                         v = VAR(varindex);
610
611 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
612                         intregsneeded = (IS_2_WORD_TYPE(t)) ? 1 : 0;
613 #endif
614 #if defined(HAS_4BYTE_STACKSLOT)
615                         memneeded = (IS_2_WORD_TYPE(t)) ? 1 : 0;
616 #endif
617
618                         /*
619                          *  The order of
620                          *
621                          *  #ifdef HAS_ADDRESS_REGISTER_FILE
622                          *  if (IS_ADR_TYPE) { 
623                          *  ...
624                          *  } else 
625                          *  #endif
626                          *  if (IS_FLT_DBL) {
627                          *  ...
628                          *  } else { / int & lng
629                          *  ...
630                          *  }
631                          *
632                          *  must not to be changed!
633                          */
634
635 #ifdef HAS_ADDRESS_REGISTER_FILE
636                         if (IS_ADR_TYPE(t)) {
637                                 if ((p < md->paramcount) && !md->params[p].inmemory) {
638                                         v->flags = 0;
639                                         v->vv.regoff = rd->argadrregs[md->params[p].regoff];
640                                 }
641                                 else if (AVAIL_TMP_ADR) {
642                                         v->flags = 0;
643                                         TAKE_TMP_ADR(v->vv.regoff);
644                                 }
645                                 /* use unused argument registers as local registers */
646                                 else if ((p >= md->paramcount) &&
647                                                  (aargcnt < ADR_ARG_CNT)) 
648                                 {
649                                         v->flags = 0;
650                                         POP_FRONT(rd->argadrregs, aargcnt, v->vv.regoff);
651                                 }
652                                 else if (AVAIL_SAV_ADR) {
653                                         v->flags = 0;
654                                         TAKE_SAV_ADR(v->vv.regoff);
655                                 }
656                                 else {
657                                         v->flags |= INMEMORY;
658                                         v->vv.regoff = rd->memuse++ * SIZE_OF_STACKSLOT;
659                                 }                                               
660                         } 
661                         else {
662 #endif
663                                 if (IS_FLT_DBL_TYPE(t)) {
664                                         if (fltalloc >= 0) {
665                                                 v->flags = VAR(fltalloc)->flags;
666                                                 v->vv.regoff = VAR(fltalloc)->vv.regoff;
667                                         }
668 #if !defined(SUPPORT_PASS_FLOATARGS_IN_INTREGS)
669                                         /* We can only use float arguments as local variables,
670                                          * if we do not pass them in integer registers. */
671                                         else if ((p < md->paramcount) && !md->params[p].inmemory) {
672                                                 v->flags = 0;
673                                                 v->vv.regoff = md->params[p].regoff;
674                                         }
675 #endif
676                                         else if (AVAIL_TMP_FLT) {
677                                                 v->flags = 0;
678                                                 TAKE_TMP_FLT(v->vv.regoff);
679                                         }
680                                         /* use unused argument registers as local registers */
681                                         else if ((p >= md->paramcount) && (fargcnt < FLT_ARG_CNT)) {
682                                                 v->flags = 0;
683                                                 POP_FRONT(abi_registers_float_argument,
684                                                                   fargcnt, v->vv.regoff);
685                                         }
686                                         else if (AVAIL_SAV_FLT) {
687                                                 v->flags = 0;
688                                                 TAKE_SAV_FLT(v->vv.regoff);
689                                         }
690                                         else {
691                                                 v->flags = INMEMORY;
692                                                 NEW_MEM_SLOT_FLT_DBL(v->vv.regoff);
693                                         }
694                                         fltalloc = jd->local_map[s * 5 + t];
695
696                                 } 
697                                 else {
698 #if (SIZEOF_VOID_P == 4) && !defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
699                                         /*
700                                          * for i386 put all longs in memory
701                                          */
702                                         if (IS_2_WORD_TYPE(t)) {
703                                                 v->flags = INMEMORY;
704                                                 NEW_MEM_SLOT_INT_LNG(v->vv.regoff);
705                                         } 
706                                         else 
707 #endif
708                                         {
709                                                 if (intalloc >= 0) {
710                                                         v->flags = VAR(intalloc)->flags;
711 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
712                                                         if (!(v->flags & INMEMORY)
713                                                                 && IS_2_WORD_TYPE(VAR(intalloc)->type))
714                                                                 v->vv.regoff = GET_LOW_REG(
715                                                                                                 VAR(intalloc)->vv.regoff);
716                                                         else
717 #endif
718                                                                 v->vv.regoff = VAR(intalloc)->vv.regoff;
719                                                 }
720                                                 else if ((p < md->paramcount) && 
721                                                                  !md->params[p].inmemory) 
722                                                 {
723                                                         v->flags = 0;
724 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
725                                                         if (IS_2_WORD_TYPE(t))
726                                                                 v->vv.regoff =
727                                                                         PACK_REGS(GET_LOW_REG(md->params[p].regoff),
728                                                                                           GET_HIGH_REG(md->params[p].regoff));
729                                                                 else
730 #endif
731                                                                         v->vv.regoff = md->params[p].regoff;
732                                                 }
733                                                 else if (AVAIL_TMP_INT) {
734                                                         v->flags = 0;
735                                                         TAKE_TMP_INT(v->vv.regoff);
736                                                 }
737                                                 /*
738                                                  * use unused argument registers as local registers
739                                                  */
740                                                 else if ((p >= m->parseddesc->paramcount) &&
741                                                                  (iargcnt + intregsneeded < INT_ARG_CNT)) 
742                                                 {
743                                                         v->flags = 0;
744                                                         POP_FRONT_INT(abi_registers_integer_argument,
745                                                                                   iargcnt, v->vv.regoff);
746                                                 }
747                                                 else if (AVAIL_SAV_INT) {
748                                                         v->flags = 0;
749                                                         TAKE_SAV_INT(v->vv.regoff);
750                                                 }
751                                                 else {
752                                                         v->flags = INMEMORY;
753                                                         NEW_MEM_SLOT_INT_LNG(v->vv.regoff);
754                                                 }
755                                         }
756                                         intalloc = jd->local_map[s * 5 + t];
757                                 }
758 #ifdef HAS_ADDRESS_REGISTER_FILE
759                         }
760 #endif
761                 } /* for (tt=0;...) */
762
763                 /* If the current parameter is a 2-word type, the next local slot */
764                 /* is skipped.                                                    */
765
766                 if (p < md->paramcount)
767                         if (IS_2_WORD_TYPE(md->paramtypes[p].type))
768                                 s++;
769         }
770 }
771
772 /* simplereg_allocate_locals ***************************************************
773
774    Allocates registers for all local variables.
775         
776 *******************************************************************************/
777         
778 static void simplereg_allocate_locals(jitdata *jd)
779 {
780         codegendata  *cd;
781         registerdata *rd;
782
783         int     s, t, tt, varindex;
784         int     intalloc, fltalloc;
785         varinfo *v;
786         int     memneeded = 0;
787         int     typeloop[] = { TYPE_LNG, TYPE_DBL, TYPE_INT, TYPE_FLT, TYPE_ADR };
788 #ifdef SUPPORT_COMBINE_INTEGER_REGISTERS
789         s4 intregsneeded;
790 #endif
791
792         /* get required compiler data */
793
794         cd = jd->cd;
795         rd = jd->rd;
796
797         if (jd->isleafmethod) {
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)
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_FCMPEQ:
1559                                 case ICMD_IF_FCMPNE:
1560
1561                                 case ICMD_IF_FCMPL_LT:
1562                                 case ICMD_IF_FCMPL_GE:
1563                                 case ICMD_IF_FCMPL_GT:
1564                                 case ICMD_IF_FCMPL_LE:
1565
1566                                 case ICMD_IF_FCMPG_LT:
1567                                 case ICMD_IF_FCMPG_GE:
1568                                 case ICMD_IF_FCMPG_GT:
1569                                 case ICMD_IF_FCMPG_LE:
1570
1571                                 case ICMD_IF_DCMPEQ:
1572                                 case ICMD_IF_DCMPNE:
1573
1574                                 case ICMD_IF_DCMPL_LT:
1575                                 case ICMD_IF_DCMPL_GE:
1576                                 case ICMD_IF_DCMPL_GT:
1577                                 case ICMD_IF_DCMPL_LE:
1578
1579                                 case ICMD_IF_DCMPG_LT:
1580                                 case ICMD_IF_DCMPG_GE:
1581                                 case ICMD_IF_DCMPG_GT:
1582                                 case ICMD_IF_DCMPG_LE:
1583
1584                                 case ICMD_IF_ACMPEQ:
1585                                 case ICMD_IF_ACMPNE:
1586
1587                                         /* pop 2 push 0 */
1588
1589                                 case ICMD_POP2:
1590
1591                                 case ICMD_PUTFIELD:
1592
1593                                 case ICMD_IASTORECONST:
1594                                 case ICMD_LASTORECONST:
1595                                 case ICMD_AASTORECONST:
1596                                 case ICMD_BASTORECONST:
1597                                 case ICMD_CASTORECONST:
1598                                 case ICMD_SASTORECONST:
1599                                         FREE_TEMP_REG(iptr->sx.s23.s2.varindex);
1600                                         FREE_TEMP_REG(iptr->s1.varindex);
1601                                         break;
1602
1603                                         /* pop 0 push 1 copy */
1604                                         
1605                                 case ICMD_COPY:
1606                                         /* src === dst->prev (identical Stackslot Element)     */
1607                                         /* src --> dst       (copied value, take same reg/mem) */
1608
1609                                         if (!simplereg_alloc_dup(jd, iptr->s1.varindex, iptr->dst.varindex)) {
1610                                                 NEW_TEMP_REG(iptr->dst.varindex);
1611                                         } 
1612                                         else {
1613                                                 v = VAROP(iptr->dst);
1614
1615                                                 if (v->flags & INMEMORY) {
1616                                                         int32_t memindex = v->vv.regoff / SIZE_OF_STACKSLOT;
1617                                                         if (memindex >= rd->memcopycountsize) {
1618                                                                 int newsize = (memindex + 1) * 2;
1619                                                                 i = rd->memcopycountsize;
1620                                                                 rd->memcopycount = DMREALLOC(rd->memcopycount, int, i, newsize);
1621                                                                 MZERO(rd->memcopycount + i, int, newsize - i);
1622                                                                 rd->memcopycountsize = newsize;
1623                                                         }
1624                                                         rd->memcopycount[memindex]++;
1625                                                 }
1626                                                 else {
1627                                                         /* XXX split reg/mem variables on arm may need special handling here */
1628
1629                                                         s4 regindex = REG_INDEX(v->vv.regoff, v->type);
1630
1631                                                         rd->regcopycount[regindex]++;
1632                                                 }
1633                                         }
1634                                         break;
1635
1636                                         /* pop 1 push 1 move */
1637
1638                                 case ICMD_MOVE:
1639                                         if (!simplereg_alloc_dup(jd, iptr->s1.varindex, iptr->dst.varindex)) {
1640                                                 NEW_TEMP_REG(iptr->dst.varindex);
1641                                                 FREE_TEMP_REG(iptr->s1.varindex);
1642                                         }
1643                                         break;
1644
1645                                         /* pop 2 push 1 */
1646                                         
1647                                 case ICMD_IADD:
1648                                 case ICMD_ISUB:
1649                                 case ICMD_IMUL:
1650                                 case ICMD_IDIV:
1651                                 case ICMD_IREM:
1652
1653                                 case ICMD_ISHL:
1654                                 case ICMD_ISHR:
1655                                 case ICMD_IUSHR:
1656                                 case ICMD_IAND:
1657                                 case ICMD_IOR:
1658                                 case ICMD_IXOR:
1659
1660                                 case ICMD_LADD:
1661                                 case ICMD_LSUB:
1662                                 case ICMD_LMUL:
1663                                 case ICMD_LDIV:
1664                                 case ICMD_LREM:
1665
1666                                 case ICMD_LOR:
1667                                 case ICMD_LAND:
1668                                 case ICMD_LXOR:
1669
1670                                 case ICMD_LSHL:
1671                                 case ICMD_LSHR:
1672                                 case ICMD_LUSHR:
1673
1674                                 case ICMD_FADD:
1675                                 case ICMD_FSUB:
1676                                 case ICMD_FMUL:
1677                                 case ICMD_FDIV:
1678                                 case ICMD_FREM:
1679
1680                                 case ICMD_DADD:
1681                                 case ICMD_DSUB:
1682                                 case ICMD_DMUL:
1683                                 case ICMD_DDIV:
1684                                 case ICMD_DREM:
1685
1686                                 case ICMD_LCMP:
1687                                 case ICMD_FCMPL:
1688                                 case ICMD_FCMPG:
1689                                 case ICMD_DCMPL:
1690                                 case ICMD_DCMPG:
1691                                         FREE_TEMP_REG(iptr->sx.s23.s2.varindex);
1692                                         FREE_TEMP_REG(iptr->s1.varindex);
1693                                         NEW_TEMP_REG(iptr->dst.varindex);
1694                                         break;
1695
1696                                         /* pop 1 push 1 */
1697                                         
1698                                 case ICMD_IADDCONST:
1699                                 case ICMD_ISUBCONST:
1700                                 case ICMD_IMULCONST:
1701                                 case ICMD_IMULPOW2:
1702                                 case ICMD_IDIVPOW2:
1703                                 case ICMD_IREMPOW2:
1704                                 case ICMD_IANDCONST:
1705                                 case ICMD_IORCONST:
1706                                 case ICMD_IXORCONST:
1707                                 case ICMD_ISHLCONST:
1708                                 case ICMD_ISHRCONST:
1709                                 case ICMD_IUSHRCONST:
1710
1711                                 case ICMD_LADDCONST:
1712                                 case ICMD_LSUBCONST:
1713                                 case ICMD_LMULCONST:
1714                                 case ICMD_LMULPOW2:
1715                                 case ICMD_LDIVPOW2:
1716                                 case ICMD_LREMPOW2:
1717                                 case ICMD_LANDCONST:
1718                                 case ICMD_LORCONST:
1719                                 case ICMD_LXORCONST:
1720                                 case ICMD_LSHLCONST:
1721                                 case ICMD_LSHRCONST:
1722                                 case ICMD_LUSHRCONST:
1723
1724                                 case ICMD_INEG:
1725                                 case ICMD_INT2BYTE:
1726                                 case ICMD_INT2CHAR:
1727                                 case ICMD_INT2SHORT:
1728                                 case ICMD_LNEG:
1729                                 case ICMD_FNEG:
1730                                 case ICMD_DNEG:
1731
1732                                 case ICMD_I2L:
1733                                 case ICMD_I2F:
1734                                 case ICMD_I2D:
1735                                 case ICMD_L2I:
1736                                 case ICMD_L2F:
1737                                 case ICMD_L2D:
1738                                 case ICMD_F2I:
1739                                 case ICMD_F2L:
1740                                 case ICMD_F2D:
1741                                 case ICMD_D2I:
1742                                 case ICMD_D2L:
1743                                 case ICMD_D2F:
1744
1745                                 case ICMD_CHECKCAST:
1746
1747                                 case ICMD_ARRAYLENGTH:
1748                                 case ICMD_INSTANCEOF:
1749
1750                                 case ICMD_NEWARRAY:
1751                                 case ICMD_ANEWARRAY:
1752
1753                                 case ICMD_GETFIELD:
1754                                         FREE_TEMP_REG(iptr->s1.varindex);
1755                                         NEW_TEMP_REG(iptr->dst.varindex);
1756                                         break;
1757
1758                                         /* pop 0 push 1 */
1759                                         
1760                                 case ICMD_GETSTATIC:
1761
1762                                 case ICMD_NEW:
1763                                         NEW_TEMP_REG(iptr->dst.varindex);
1764                                         break;
1765
1766                                         /* pop many push any */
1767                                         
1768                                 case ICMD_INVOKESTATIC:
1769                                 case ICMD_INVOKESPECIAL:
1770                                 case ICMD_INVOKEVIRTUAL:
1771                                 case ICMD_INVOKEINTERFACE:
1772                                         INSTRUCTION_GET_METHODDESC(iptr,md);
1773                                         i = md->paramcount;
1774                                         argp = iptr->sx.s23.s2.args;
1775                                         while (--i >= 0) {
1776                                                 FREE_TEMP_REG(*argp);
1777                                                 argp++;
1778                                         }
1779                                         if (md->returntype.type != TYPE_VOID)
1780                                                 NEW_TEMP_REG(iptr->dst.varindex);
1781                                         break;
1782
1783                                 case ICMD_BUILTIN:
1784                                         bte = iptr->sx.s23.s3.bte;
1785                                         md = bte->md;
1786                                         i = md->paramcount;
1787                                         argp = iptr->sx.s23.s2.args;
1788                                         while (--i >= 0) {
1789                                                 FREE_TEMP_REG(*argp);
1790                                                 argp++;
1791                                         }
1792                                         if (md->returntype.type != TYPE_VOID)
1793                                                 NEW_TEMP_REG(iptr->dst.varindex);
1794                                         break;
1795
1796                                 case ICMD_MULTIANEWARRAY:
1797                                         i = iptr->s1.argcount;
1798                                         argp = iptr->sx.s23.s2.args;
1799                                         while (--i >= 0) {
1800                                                 FREE_TEMP_REG(*argp);
1801                                                 argp++;
1802                                         }
1803                                         NEW_TEMP_REG(iptr->dst.varindex);
1804                                         break;
1805
1806                                 default:
1807                                         exceptions_throw_internalerror("Unknown ICMD %d during register allocation",
1808                                                                                                    iptr->opc);
1809                                         return;
1810                                 } /* switch */
1811                                 iptr++;
1812                         } /* while instructions */
1813                 } /* if */
1814                 bptr = bptr->next;
1815         } /* while blocks */
1816 }
1817
1818
1819 #if defined(ENABLE_STATISTICS)
1820 void simplereg_make_statistics(jitdata *jd)
1821 {
1822         methodinfo   *m;
1823         codegendata  *cd;
1824         registerdata *rd;
1825         int i;
1826         s4 len;
1827 #if 0
1828         stackptr    src, src_old;
1829         stackptr    dst;
1830         instruction *iptr;
1831 #endif
1832         basicblock  *bptr;
1833         int size_interface; /* == maximum size of in/out stack at basic block boundaries */
1834         bool in_register;
1835         varinfo *var;
1836
1837         /* get required compiler data */
1838
1839         m  = jd->m;
1840         cd = jd->cd;
1841         rd = jd->rd;
1842
1843         in_register = true;
1844
1845         size_interface = 0;
1846
1847                 /* count how many local variables are held in memory or register */
1848                 for(i=0; i < jd->localcount; i++) {
1849                         if (VAR(i)->flags & INMEMORY) {
1850                                 count_locals_spilled++;
1851                                 in_register=false;
1852                         }
1853                         else {
1854                                 count_locals_register++;
1855                         }
1856                 }
1857
1858                 /* count how many stack slots are held in memory or register */
1859
1860                 bptr = jd->basicblocks;
1861
1862                 while (bptr != NULL) {
1863                         if (bptr->flags >= BBREACHED) {
1864
1865 #if defined(ENABLE_LSRA) || defined(ENABLE_SSA)
1866                         if (!opt_lsra) {
1867 #endif  
1868                                 /* check for memory moves from interface to BB instack */
1869                                 len = bptr->indepth;
1870                                 
1871                                 if (len > size_interface) size_interface = len;
1872
1873                                 while (len) {
1874                                         len--;
1875                                         var = VAR(bptr->invars[len]);
1876
1877                                         /* invars statistics (currently none) */
1878                                 }
1879
1880                                 /* check for memory moves from BB outstack to interface */
1881                                 len = bptr->outdepth;
1882                                 if (len > size_interface) size_interface = len;
1883
1884                                 while (len) {
1885                                         len--;
1886                                         var = VAR(bptr->outvars[len]);
1887
1888                                         /* outvars statistics (currently none) */
1889                                 }
1890 #if defined(ENABLE_LSRA) || defined(ENABLE_SSA)
1891                         }
1892 #endif  
1893
1894
1895 #if 0
1896                                 dst = bptr->instack;
1897                                 iptr = bptr->iinstr;
1898                                 len = bptr->icount;
1899                                 src_old = NULL;
1900
1901                                 while (--len >= 0)  {
1902                                         src = dst;
1903                                         dst = iptr->dst.var;
1904
1905                                         if ((src!= NULL) && (src != src_old)) { /* new stackslot */
1906                                                 switch (src->varkind) {
1907                                                 case TEMPVAR:
1908                                                 case STACKVAR:
1909                                                         if (!(src->flags & INMEMORY)) 
1910                                                                 count_ss_register++;
1911                                                         else {
1912                                                                 count_ss_spilled++;
1913                                                                 in_register=false;
1914                                                         }                               
1915                                                         break;
1916                                                         /*                                      case LOCALVAR: */
1917                                                         /*                                              if (!(rd->locals[src->varnum][src->type].flags & INMEMORY)) */
1918                                                         /*                                                      count_ss_register++; */
1919                                                         /*                                              else */
1920                                                         /*                                                      count_ss_spilled++; */
1921                                                         /*                                              break; */
1922                                                 case ARGVAR:
1923                                                         if (!(src->flags & INMEMORY)) 
1924                                                                 count_argument_mem_ss++;
1925                                                         else
1926                                                                 count_argument_reg_ss++;
1927                                                         break;
1928
1929
1930                                                         /*                                              if (IS_FLT_DBL_TYPE(src->type)) { */
1931                                                         /*                                                      if (src->varnum < FLT_ARG_CNT) { */
1932                                                         /*                                                              count_ss_register++; */
1933                                                         /*                                                              break; */
1934                                                         /*                                                      } */
1935                                                         /*                                              } else { */
1936                                                         /* #if defined(__POWERPC__) */
1937                                                         /*                                                      if (src->varnum < INT_ARG_CNT - (IS_2_WORD_TYPE(src->type) != 0)) { */
1938                                                         /* #else */
1939                                                         /*                                                      if (src->varnum < INT_ARG_CNT) { */
1940                                                         /* #endif */
1941                                                         /*                                                              count_ss_register++; */
1942                                                         /*                                                              break; */
1943                                                         /*                                                      } */
1944                                                         /*                                              } */
1945                                                         /*                                              count_ss_spilled++; */
1946                                                         /*                                              break; */
1947                                                 }
1948                                         }
1949                                         src_old = src;
1950                                         
1951                                         iptr++;
1952                                 } /* while instructions */
1953 #endif
1954                         } /* if */
1955
1956                         bptr = bptr->next;
1957                 } /* while blocks */
1958
1959                 count_interface_size += size_interface; /* accummulate the size of the interface (between bb boundaries) */
1960                 if (in_register) count_method_in_register++;
1961                 if (in_register) {
1962 /*                      printf("INREGISTER: %s%s%s\n",m->class->name->text, m->name->text, m->descriptor->text); */
1963                 }
1964 }
1965 #endif /* defined(ENABLE_STATISTICS) */
1966
1967
1968 /*
1969  * These are local overrides for various environment variables in Emacs.
1970  * Please do not remove this and leave it at the end of the file, where
1971  * Emacs will automagically detect them.
1972  * ---------------------------------------------------------------------
1973  * Local variables:
1974  * mode: c
1975  * indent-tabs-mode: t
1976  * c-basic-offset: 4
1977  * tab-width: 4
1978  * End:
1979  * vim:noexpandtab:sw=4:ts=4:
1980  */