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