]>
Commit | Line | Data |
---|---|---|
c896fe29 FB |
1 | /* |
2 | * Tiny Code Generator for QEMU | |
3 | * | |
4 | * Copyright (c) 2008 Fabrice Bellard | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
24 | ||
25 | /* define it to suppress various consistency checks (faster) */ | |
26 | #define NDEBUG | |
27 | ||
28 | /* define it to use liveness analysis (better code) */ | |
29 | #define USE_LIVENESS_ANALYSIS | |
30 | ||
31 | #include <assert.h> | |
32 | #include <stdarg.h> | |
33 | #include <stdlib.h> | |
34 | #include <stdio.h> | |
35 | #include <string.h> | |
36 | #include <inttypes.h> | |
3fe43da7 FB |
37 | #ifdef _WIN32 |
38 | #include <malloc.h> | |
39 | #endif | |
c896fe29 FB |
40 | |
41 | #include "config.h" | |
ca10f867 | 42 | #include "qemu-common.h" |
c896fe29 FB |
43 | |
44 | /* Note: the long term plan is to reduce the dependancies on the QEMU | |
45 | CPU definitions. Currently they are used for qemu_ld/st | |
46 | instructions */ | |
47 | #define NO_CPU_IO_DEFS | |
48 | #include "cpu.h" | |
49 | #include "exec-all.h" | |
50 | ||
51 | #include "tcg-op.h" | |
52 | #include "elf.h" | |
53 | ||
54 | ||
55 | static void patch_reloc(uint8_t *code_ptr, int type, | |
f54b3f92 | 56 | tcg_target_long value, tcg_target_long addend); |
c896fe29 FB |
57 | |
58 | TCGOpDef tcg_op_defs[] = { | |
59 | #define DEF(s, n, copy_size) { #s, 0, 0, n, n, 0, copy_size }, | |
60 | #define DEF2(s, iargs, oargs, cargs, flags) { #s, iargs, oargs, cargs, iargs + oargs + cargs, flags, 0 }, | |
61 | #include "tcg-opc.h" | |
62 | #undef DEF | |
63 | #undef DEF2 | |
64 | }; | |
65 | ||
66 | TCGRegSet tcg_target_available_regs[2]; | |
67 | TCGRegSet tcg_target_call_clobber_regs; | |
68 | ||
69 | /* XXX: move that inside the context */ | |
70 | uint16_t *gen_opc_ptr; | |
71 | TCGArg *gen_opparam_ptr; | |
72 | ||
73 | static inline void tcg_out8(TCGContext *s, uint8_t v) | |
74 | { | |
75 | *s->code_ptr++ = v; | |
76 | } | |
77 | ||
78 | static inline void tcg_out16(TCGContext *s, uint16_t v) | |
79 | { | |
80 | *(uint16_t *)s->code_ptr = v; | |
81 | s->code_ptr += 2; | |
82 | } | |
83 | ||
84 | static inline void tcg_out32(TCGContext *s, uint32_t v) | |
85 | { | |
86 | *(uint32_t *)s->code_ptr = v; | |
87 | s->code_ptr += 4; | |
88 | } | |
89 | ||
90 | /* label relocation processing */ | |
91 | ||
92 | void tcg_out_reloc(TCGContext *s, uint8_t *code_ptr, int type, | |
93 | int label_index, long addend) | |
94 | { | |
95 | TCGLabel *l; | |
96 | TCGRelocation *r; | |
97 | ||
98 | l = &s->labels[label_index]; | |
99 | if (l->has_value) { | |
623e265c PB |
100 | /* FIXME: This may break relocations on RISC targets that |
101 | modify instruction fields in place. The caller may not have | |
102 | written the initial value. */ | |
f54b3f92 | 103 | patch_reloc(code_ptr, type, l->u.value, addend); |
c896fe29 FB |
104 | } else { |
105 | /* add a new relocation entry */ | |
106 | r = tcg_malloc(sizeof(TCGRelocation)); | |
107 | r->type = type; | |
108 | r->ptr = code_ptr; | |
109 | r->addend = addend; | |
110 | r->next = l->u.first_reloc; | |
111 | l->u.first_reloc = r; | |
112 | } | |
113 | } | |
114 | ||
115 | static void tcg_out_label(TCGContext *s, int label_index, | |
116 | tcg_target_long value) | |
117 | { | |
118 | TCGLabel *l; | |
119 | TCGRelocation *r; | |
120 | ||
121 | l = &s->labels[label_index]; | |
122 | if (l->has_value) | |
123 | tcg_abort(); | |
124 | r = l->u.first_reloc; | |
125 | while (r != NULL) { | |
f54b3f92 | 126 | patch_reloc(r->ptr, r->type, value, r->addend); |
c896fe29 FB |
127 | r = r->next; |
128 | } | |
129 | l->has_value = 1; | |
130 | l->u.value = value; | |
131 | } | |
132 | ||
133 | int gen_new_label(void) | |
134 | { | |
135 | TCGContext *s = &tcg_ctx; | |
136 | int idx; | |
137 | TCGLabel *l; | |
138 | ||
139 | if (s->nb_labels >= TCG_MAX_LABELS) | |
140 | tcg_abort(); | |
141 | idx = s->nb_labels++; | |
142 | l = &s->labels[idx]; | |
143 | l->has_value = 0; | |
144 | l->u.first_reloc = NULL; | |
145 | return idx; | |
146 | } | |
147 | ||
148 | #include "tcg-target.c" | |
149 | ||
c896fe29 FB |
150 | /* pool based memory allocation */ |
151 | void *tcg_malloc_internal(TCGContext *s, int size) | |
152 | { | |
153 | TCGPool *p; | |
154 | int pool_size; | |
155 | ||
156 | if (size > TCG_POOL_CHUNK_SIZE) { | |
157 | /* big malloc: insert a new pool (XXX: could optimize) */ | |
158 | p = qemu_malloc(sizeof(TCGPool) + size); | |
159 | p->size = size; | |
160 | if (s->pool_current) | |
161 | s->pool_current->next = p; | |
162 | else | |
163 | s->pool_first = p; | |
164 | p->next = s->pool_current; | |
165 | } else { | |
166 | p = s->pool_current; | |
167 | if (!p) { | |
168 | p = s->pool_first; | |
169 | if (!p) | |
170 | goto new_pool; | |
171 | } else { | |
172 | if (!p->next) { | |
173 | new_pool: | |
174 | pool_size = TCG_POOL_CHUNK_SIZE; | |
175 | p = qemu_malloc(sizeof(TCGPool) + pool_size); | |
176 | p->size = pool_size; | |
177 | p->next = NULL; | |
178 | if (s->pool_current) | |
179 | s->pool_current->next = p; | |
180 | else | |
181 | s->pool_first = p; | |
182 | } else { | |
183 | p = p->next; | |
184 | } | |
185 | } | |
186 | } | |
187 | s->pool_current = p; | |
188 | s->pool_cur = p->data + size; | |
189 | s->pool_end = p->data + p->size; | |
190 | return p->data; | |
191 | } | |
192 | ||
193 | void tcg_pool_reset(TCGContext *s) | |
194 | { | |
195 | s->pool_cur = s->pool_end = NULL; | |
196 | s->pool_current = NULL; | |
197 | } | |
198 | ||
199 | /* free all the pool */ | |
200 | void tcg_pool_free(TCGContext *s) | |
201 | { | |
202 | TCGPool *p, *p1; | |
203 | ||
204 | for(p = s->pool_first; p != NULL; p = p1) { | |
205 | p1 = p->next; | |
206 | qemu_free(p); | |
207 | } | |
208 | s->pool_first = NULL; | |
209 | s->pool_cur = s->pool_end = NULL; | |
210 | } | |
211 | ||
212 | void tcg_context_init(TCGContext *s) | |
213 | { | |
214 | int op, total_args, n; | |
215 | TCGOpDef *def; | |
216 | TCGArgConstraint *args_ct; | |
217 | int *sorted_args; | |
218 | ||
219 | memset(s, 0, sizeof(*s)); | |
220 | s->temps = s->static_temps; | |
221 | s->nb_globals = 0; | |
222 | ||
223 | /* Count total number of arguments and allocate the corresponding | |
224 | space */ | |
225 | total_args = 0; | |
226 | for(op = 0; op < NB_OPS; op++) { | |
227 | def = &tcg_op_defs[op]; | |
228 | n = def->nb_iargs + def->nb_oargs; | |
229 | total_args += n; | |
230 | } | |
231 | ||
232 | args_ct = qemu_malloc(sizeof(TCGArgConstraint) * total_args); | |
233 | sorted_args = qemu_malloc(sizeof(int) * total_args); | |
234 | ||
235 | for(op = 0; op < NB_OPS; op++) { | |
236 | def = &tcg_op_defs[op]; | |
237 | def->args_ct = args_ct; | |
238 | def->sorted_args = sorted_args; | |
239 | n = def->nb_iargs + def->nb_oargs; | |
240 | sorted_args += n; | |
241 | args_ct += n; | |
242 | } | |
243 | ||
244 | tcg_target_init(s); | |
b03cce8e FB |
245 | |
246 | /* init global prologue and epilogue */ | |
247 | s->code_buf = code_gen_prologue; | |
248 | s->code_ptr = s->code_buf; | |
249 | tcg_target_qemu_prologue(s); | |
250 | flush_icache_range((unsigned long)s->code_buf, | |
251 | (unsigned long)s->code_ptr); | |
c896fe29 FB |
252 | } |
253 | ||
254 | void tcg_set_frame(TCGContext *s, int reg, | |
255 | tcg_target_long start, tcg_target_long size) | |
256 | { | |
257 | s->frame_start = start; | |
258 | s->frame_end = start + size; | |
259 | s->frame_reg = reg; | |
260 | } | |
261 | ||
262 | void tcg_set_macro_func(TCGContext *s, TCGMacroFunc *func) | |
263 | { | |
264 | s->macro_func = func; | |
265 | } | |
266 | ||
267 | void tcg_func_start(TCGContext *s) | |
268 | { | |
269 | tcg_pool_reset(s); | |
270 | s->nb_temps = s->nb_globals; | |
271 | s->labels = tcg_malloc(sizeof(TCGLabel) * TCG_MAX_LABELS); | |
272 | s->nb_labels = 0; | |
273 | s->current_frame_offset = s->frame_start; | |
274 | ||
275 | gen_opc_ptr = gen_opc_buf; | |
276 | gen_opparam_ptr = gen_opparam_buf; | |
277 | } | |
278 | ||
279 | static inline void tcg_temp_alloc(TCGContext *s, int n) | |
280 | { | |
281 | if (n > TCG_MAX_TEMPS) | |
282 | tcg_abort(); | |
283 | } | |
284 | ||
ac56dd48 | 285 | TCGv tcg_global_reg_new(TCGType type, int reg, const char *name) |
c896fe29 FB |
286 | { |
287 | TCGContext *s = &tcg_ctx; | |
288 | TCGTemp *ts; | |
289 | int idx; | |
290 | ||
291 | #if TCG_TARGET_REG_BITS == 32 | |
292 | if (type != TCG_TYPE_I32) | |
293 | tcg_abort(); | |
294 | #endif | |
295 | if (tcg_regset_test_reg(s->reserved_regs, reg)) | |
296 | tcg_abort(); | |
297 | idx = s->nb_globals; | |
298 | tcg_temp_alloc(s, s->nb_globals + 1); | |
299 | ts = &s->temps[s->nb_globals]; | |
300 | ts->base_type = type; | |
301 | ts->type = type; | |
302 | ts->fixed_reg = 1; | |
303 | ts->reg = reg; | |
304 | ts->val_type = TEMP_VAL_REG; | |
305 | ts->name = name; | |
306 | s->nb_globals++; | |
307 | tcg_regset_set_reg(s->reserved_regs, reg); | |
ac56dd48 | 308 | return MAKE_TCGV(idx); |
c896fe29 FB |
309 | } |
310 | ||
6a8d7b76 FB |
311 | #if TCG_TARGET_REG_BITS == 32 |
312 | /* temporary hack to avoid register shortage for tcg_qemu_st64() */ | |
313 | TCGv tcg_global_reg2_new_hack(TCGType type, int reg1, int reg2, | |
314 | const char *name) | |
315 | { | |
316 | TCGContext *s = &tcg_ctx; | |
317 | TCGTemp *ts; | |
318 | int idx; | |
319 | char buf[64]; | |
320 | ||
321 | if (type != TCG_TYPE_I64) | |
322 | tcg_abort(); | |
323 | idx = s->nb_globals; | |
324 | tcg_temp_alloc(s, s->nb_globals + 2); | |
325 | ts = &s->temps[s->nb_globals]; | |
326 | ts->base_type = type; | |
327 | ts->type = TCG_TYPE_I32; | |
328 | ts->fixed_reg = 1; | |
329 | ts->reg = reg1; | |
330 | ts->val_type = TEMP_VAL_REG; | |
331 | pstrcpy(buf, sizeof(buf), name); | |
332 | pstrcat(buf, sizeof(buf), "_0"); | |
333 | ts->name = strdup(buf); | |
334 | ||
335 | ts++; | |
336 | ts->base_type = type; | |
337 | ts->type = TCG_TYPE_I32; | |
338 | ts->fixed_reg = 1; | |
339 | ts->reg = reg2; | |
340 | ts->val_type = TEMP_VAL_REG; | |
341 | pstrcpy(buf, sizeof(buf), name); | |
342 | pstrcat(buf, sizeof(buf), "_1"); | |
343 | ts->name = strdup(buf); | |
344 | ||
345 | s->nb_globals += 2; | |
346 | return MAKE_TCGV(idx); | |
347 | } | |
348 | #endif | |
349 | ||
ac56dd48 PB |
350 | TCGv tcg_global_mem_new(TCGType type, int reg, tcg_target_long offset, |
351 | const char *name) | |
c896fe29 FB |
352 | { |
353 | TCGContext *s = &tcg_ctx; | |
354 | TCGTemp *ts; | |
355 | int idx; | |
356 | ||
357 | idx = s->nb_globals; | |
358 | #if TCG_TARGET_REG_BITS == 32 | |
359 | if (type == TCG_TYPE_I64) { | |
360 | char buf[64]; | |
361 | tcg_temp_alloc(s, s->nb_globals + 1); | |
362 | ts = &s->temps[s->nb_globals]; | |
363 | ts->base_type = type; | |
364 | ts->type = TCG_TYPE_I32; | |
365 | ts->fixed_reg = 0; | |
366 | ts->mem_allocated = 1; | |
367 | ts->mem_reg = reg; | |
368 | #ifdef TCG_TARGET_WORDS_BIGENDIAN | |
369 | ts->mem_offset = offset + 4; | |
370 | #else | |
371 | ts->mem_offset = offset; | |
372 | #endif | |
373 | ts->val_type = TEMP_VAL_MEM; | |
374 | pstrcpy(buf, sizeof(buf), name); | |
375 | pstrcat(buf, sizeof(buf), "_0"); | |
376 | ts->name = strdup(buf); | |
377 | ts++; | |
378 | ||
379 | ts->base_type = type; | |
380 | ts->type = TCG_TYPE_I32; | |
381 | ts->fixed_reg = 0; | |
382 | ts->mem_allocated = 1; | |
383 | ts->mem_reg = reg; | |
384 | #ifdef TCG_TARGET_WORDS_BIGENDIAN | |
385 | ts->mem_offset = offset; | |
386 | #else | |
387 | ts->mem_offset = offset + 4; | |
388 | #endif | |
389 | ts->val_type = TEMP_VAL_MEM; | |
390 | pstrcpy(buf, sizeof(buf), name); | |
391 | pstrcat(buf, sizeof(buf), "_1"); | |
392 | ts->name = strdup(buf); | |
393 | ||
394 | s->nb_globals += 2; | |
395 | } else | |
396 | #endif | |
397 | { | |
398 | tcg_temp_alloc(s, s->nb_globals + 1); | |
399 | ts = &s->temps[s->nb_globals]; | |
400 | ts->base_type = type; | |
401 | ts->type = type; | |
402 | ts->fixed_reg = 0; | |
403 | ts->mem_allocated = 1; | |
404 | ts->mem_reg = reg; | |
405 | ts->mem_offset = offset; | |
406 | ts->val_type = TEMP_VAL_MEM; | |
407 | ts->name = name; | |
408 | s->nb_globals++; | |
409 | } | |
ac56dd48 | 410 | return MAKE_TCGV(idx); |
c896fe29 FB |
411 | } |
412 | ||
ac56dd48 | 413 | TCGv tcg_temp_new(TCGType type) |
c896fe29 FB |
414 | { |
415 | TCGContext *s = &tcg_ctx; | |
416 | TCGTemp *ts; | |
417 | int idx; | |
418 | ||
419 | idx = s->nb_temps; | |
420 | #if TCG_TARGET_REG_BITS == 32 | |
421 | if (type == TCG_TYPE_I64) { | |
422 | tcg_temp_alloc(s, s->nb_temps + 1); | |
423 | ts = &s->temps[s->nb_temps]; | |
424 | ts->base_type = type; | |
425 | ts->type = TCG_TYPE_I32; | |
5ff9d6a4 | 426 | ts->fixed_reg = 0; |
c896fe29 FB |
427 | ts->val_type = TEMP_VAL_DEAD; |
428 | ts->mem_allocated = 0; | |
429 | ts->name = NULL; | |
430 | ts++; | |
431 | ts->base_type = TCG_TYPE_I32; | |
432 | ts->type = TCG_TYPE_I32; | |
433 | ts->val_type = TEMP_VAL_DEAD; | |
5ff9d6a4 | 434 | ts->fixed_reg = 0; |
c896fe29 FB |
435 | ts->mem_allocated = 0; |
436 | ts->name = NULL; | |
437 | s->nb_temps += 2; | |
438 | } else | |
439 | #endif | |
440 | { | |
441 | tcg_temp_alloc(s, s->nb_temps + 1); | |
442 | ts = &s->temps[s->nb_temps]; | |
443 | ts->base_type = type; | |
444 | ts->type = type; | |
5ff9d6a4 | 445 | ts->fixed_reg = 0; |
c896fe29 FB |
446 | ts->val_type = TEMP_VAL_DEAD; |
447 | ts->mem_allocated = 0; | |
448 | ts->name = NULL; | |
449 | s->nb_temps++; | |
450 | } | |
ac56dd48 | 451 | return MAKE_TCGV(idx); |
c896fe29 FB |
452 | } |
453 | ||
ac56dd48 | 454 | TCGv tcg_const_i32(int32_t val) |
c896fe29 FB |
455 | { |
456 | TCGContext *s = &tcg_ctx; | |
457 | TCGTemp *ts; | |
458 | int idx; | |
459 | ||
460 | idx = s->nb_temps; | |
461 | tcg_temp_alloc(s, idx + 1); | |
462 | ts = &s->temps[idx]; | |
463 | ts->base_type = ts->type = TCG_TYPE_I32; | |
464 | ts->val_type = TEMP_VAL_CONST; | |
465 | ts->name = NULL; | |
466 | ts->val = val; | |
467 | s->nb_temps++; | |
ac56dd48 | 468 | return MAKE_TCGV(idx); |
c896fe29 FB |
469 | } |
470 | ||
ac56dd48 | 471 | TCGv tcg_const_i64(int64_t val) |
c896fe29 FB |
472 | { |
473 | TCGContext *s = &tcg_ctx; | |
474 | TCGTemp *ts; | |
475 | int idx; | |
476 | ||
477 | idx = s->nb_temps; | |
478 | #if TCG_TARGET_REG_BITS == 32 | |
479 | tcg_temp_alloc(s, idx + 2); | |
480 | ts = &s->temps[idx]; | |
481 | ts->base_type = TCG_TYPE_I64; | |
482 | ts->type = TCG_TYPE_I32; | |
483 | ts->val_type = TEMP_VAL_CONST; | |
484 | ts->name = NULL; | |
485 | ts->val = val; | |
486 | ts++; | |
487 | ts->base_type = TCG_TYPE_I32; | |
488 | ts->type = TCG_TYPE_I32; | |
489 | ts->val_type = TEMP_VAL_CONST; | |
490 | ts->name = NULL; | |
491 | ts->val = val >> 32; | |
492 | s->nb_temps += 2; | |
493 | #else | |
494 | tcg_temp_alloc(s, idx + 1); | |
495 | ts = &s->temps[idx]; | |
496 | ts->base_type = ts->type = TCG_TYPE_I64; | |
497 | ts->val_type = TEMP_VAL_CONST; | |
498 | ts->name = NULL; | |
499 | ts->val = val; | |
500 | s->nb_temps++; | |
501 | #endif | |
ac56dd48 | 502 | return MAKE_TCGV(idx); |
c896fe29 FB |
503 | } |
504 | ||
505 | void tcg_register_helper(void *func, const char *name) | |
506 | { | |
507 | TCGContext *s = &tcg_ctx; | |
508 | int n; | |
509 | if ((s->nb_helpers + 1) > s->allocated_helpers) { | |
510 | n = s->allocated_helpers; | |
511 | if (n == 0) { | |
512 | n = 4; | |
513 | } else { | |
514 | n *= 2; | |
515 | } | |
516 | s->helpers = realloc(s->helpers, n * sizeof(TCGHelperInfo)); | |
517 | s->allocated_helpers = n; | |
518 | } | |
519 | s->helpers[s->nb_helpers].func = func; | |
520 | s->helpers[s->nb_helpers].name = name; | |
521 | s->nb_helpers++; | |
522 | } | |
523 | ||
524 | const char *tcg_helper_get_name(TCGContext *s, void *func) | |
525 | { | |
526 | int i; | |
527 | ||
528 | for(i = 0; i < s->nb_helpers; i++) { | |
529 | if (s->helpers[i].func == func) | |
530 | return s->helpers[i].name; | |
531 | } | |
532 | return NULL; | |
533 | } | |
534 | ||
ac56dd48 | 535 | static inline TCGType tcg_get_base_type(TCGContext *s, TCGv arg) |
c896fe29 | 536 | { |
ac56dd48 | 537 | return s->temps[GET_TCGV(arg)].base_type; |
c896fe29 FB |
538 | } |
539 | ||
ac56dd48 | 540 | static void tcg_gen_call_internal(TCGContext *s, TCGv func, |
c896fe29 | 541 | unsigned int flags, |
ac56dd48 PB |
542 | unsigned int nb_rets, const TCGv *rets, |
543 | unsigned int nb_params, const TCGv *params) | |
c896fe29 FB |
544 | { |
545 | int i; | |
546 | *gen_opc_ptr++ = INDEX_op_call; | |
547 | *gen_opparam_ptr++ = (nb_rets << 16) | (nb_params + 1); | |
548 | for(i = 0; i < nb_rets; i++) { | |
ac56dd48 | 549 | *gen_opparam_ptr++ = GET_TCGV(rets[i]); |
c896fe29 FB |
550 | } |
551 | for(i = 0; i < nb_params; i++) { | |
ac56dd48 | 552 | *gen_opparam_ptr++ = GET_TCGV(params[i]); |
c896fe29 | 553 | } |
ac56dd48 | 554 | *gen_opparam_ptr++ = GET_TCGV(func); |
c896fe29 FB |
555 | |
556 | *gen_opparam_ptr++ = flags; | |
557 | /* total parameters, needed to go backward in the instruction stream */ | |
558 | *gen_opparam_ptr++ = 1 + nb_rets + nb_params + 3; | |
559 | } | |
560 | ||
561 | ||
562 | #if TCG_TARGET_REG_BITS < 64 | |
563 | /* Note: we convert the 64 bit args to 32 bit */ | |
ac56dd48 PB |
564 | void tcg_gen_call(TCGContext *s, TCGv func, unsigned int flags, |
565 | unsigned int nb_rets, const TCGv *rets, | |
566 | unsigned int nb_params, const TCGv *args1) | |
c896fe29 | 567 | { |
ac56dd48 | 568 | TCGv ret, *args2, rets_2[2], arg; |
c896fe29 FB |
569 | int j, i, call_type; |
570 | ||
571 | if (nb_rets == 1) { | |
572 | ret = rets[0]; | |
573 | if (tcg_get_base_type(s, ret) == TCG_TYPE_I64) { | |
574 | nb_rets = 2; | |
575 | rets_2[0] = ret; | |
ac56dd48 | 576 | rets_2[1] = TCGV_HIGH(ret); |
c896fe29 FB |
577 | rets = rets_2; |
578 | } | |
579 | } | |
ac56dd48 | 580 | args2 = alloca((nb_params * 2) * sizeof(TCGv)); |
c896fe29 FB |
581 | j = 0; |
582 | call_type = (flags & TCG_CALL_TYPE_MASK); | |
583 | for(i = 0; i < nb_params; i++) { | |
584 | arg = args1[i]; | |
585 | if (tcg_get_base_type(s, arg) == TCG_TYPE_I64) { | |
586 | #ifdef TCG_TARGET_I386 | |
587 | /* REGPARM case: if the third parameter is 64 bit, it is | |
588 | allocated on the stack */ | |
589 | if (j == 2 && call_type == TCG_CALL_TYPE_REGPARM) { | |
590 | call_type = TCG_CALL_TYPE_REGPARM_2; | |
591 | flags = (flags & ~TCG_CALL_TYPE_MASK) | call_type; | |
592 | } | |
593 | args2[j++] = arg; | |
ac56dd48 | 594 | args2[j++] = TCGV_HIGH(arg); |
c896fe29 FB |
595 | #else |
596 | #ifdef TCG_TARGET_WORDS_BIGENDIAN | |
a0d69e00 | 597 | args2[j++] = TCGV_HIGH(arg); |
c896fe29 FB |
598 | args2[j++] = arg; |
599 | #else | |
600 | args2[j++] = arg; | |
ac56dd48 | 601 | args2[j++] = TCGV_HIGH(arg); |
c896fe29 FB |
602 | #endif |
603 | #endif | |
604 | } else { | |
605 | args2[j++] = arg; | |
606 | } | |
607 | } | |
608 | tcg_gen_call_internal(s, func, flags, | |
609 | nb_rets, rets, j, args2); | |
610 | } | |
611 | #else | |
ac56dd48 PB |
612 | void tcg_gen_call(TCGContext *s, TCGv func, unsigned int flags, |
613 | unsigned int nb_rets, const TCGv *rets, | |
614 | unsigned int nb_params, const TCGv *args1) | |
c896fe29 FB |
615 | { |
616 | tcg_gen_call_internal(s, func, flags, | |
617 | nb_rets, rets, nb_params, args1); | |
618 | } | |
619 | #endif | |
620 | ||
ac56dd48 PB |
621 | #if TCG_TARGET_REG_BITS == 32 |
622 | void tcg_gen_shifti_i64(TCGv ret, TCGv arg1, | |
c896fe29 FB |
623 | int c, int right, int arith) |
624 | { | |
cf60bce4 FB |
625 | if (c == 0) { |
626 | tcg_gen_mov_i32(ret, arg1); | |
627 | tcg_gen_mov_i32(TCGV_HIGH(ret), TCGV_HIGH(arg1)); | |
628 | } else if (c >= 32) { | |
c896fe29 FB |
629 | c -= 32; |
630 | if (right) { | |
631 | if (arith) { | |
ac56dd48 PB |
632 | tcg_gen_sari_i32(ret, TCGV_HIGH(arg1), c); |
633 | tcg_gen_sari_i32(TCGV_HIGH(ret), TCGV_HIGH(arg1), 31); | |
c896fe29 | 634 | } else { |
ac56dd48 PB |
635 | tcg_gen_shri_i32(ret, TCGV_HIGH(arg1), c); |
636 | tcg_gen_movi_i32(TCGV_HIGH(ret), 0); | |
c896fe29 FB |
637 | } |
638 | } else { | |
ac56dd48 | 639 | tcg_gen_shli_i32(TCGV_HIGH(ret), arg1, c); |
c896fe29 FB |
640 | tcg_gen_movi_i32(ret, 0); |
641 | } | |
642 | } else { | |
ac56dd48 | 643 | TCGv t0, t1; |
c896fe29 FB |
644 | |
645 | t0 = tcg_temp_new(TCG_TYPE_I32); | |
646 | t1 = tcg_temp_new(TCG_TYPE_I32); | |
647 | if (right) { | |
ac56dd48 | 648 | tcg_gen_shli_i32(t0, TCGV_HIGH(arg1), 32 - c); |
c896fe29 | 649 | if (arith) |
ac56dd48 | 650 | tcg_gen_sari_i32(t1, TCGV_HIGH(arg1), c); |
c896fe29 | 651 | else |
ac56dd48 | 652 | tcg_gen_shri_i32(t1, TCGV_HIGH(arg1), c); |
c896fe29 FB |
653 | tcg_gen_shri_i32(ret, arg1, c); |
654 | tcg_gen_or_i32(ret, ret, t0); | |
ac56dd48 | 655 | tcg_gen_mov_i32(TCGV_HIGH(ret), t1); |
c896fe29 FB |
656 | } else { |
657 | tcg_gen_shri_i32(t0, arg1, 32 - c); | |
658 | /* Note: ret can be the same as arg1, so we use t1 */ | |
659 | tcg_gen_shli_i32(t1, arg1, c); | |
ac56dd48 PB |
660 | tcg_gen_shli_i32(TCGV_HIGH(ret), TCGV_HIGH(arg1), c); |
661 | tcg_gen_or_i32(TCGV_HIGH(ret), TCGV_HIGH(ret), t0); | |
c896fe29 FB |
662 | tcg_gen_mov_i32(ret, t1); |
663 | } | |
664 | } | |
665 | } | |
ac56dd48 | 666 | #endif |
c896fe29 FB |
667 | |
668 | void tcg_reg_alloc_start(TCGContext *s) | |
669 | { | |
670 | int i; | |
671 | TCGTemp *ts; | |
672 | for(i = 0; i < s->nb_globals; i++) { | |
673 | ts = &s->temps[i]; | |
674 | if (ts->fixed_reg) { | |
675 | ts->val_type = TEMP_VAL_REG; | |
676 | } else { | |
677 | ts->val_type = TEMP_VAL_MEM; | |
678 | } | |
679 | } | |
680 | for(i = 0; i < TCG_TARGET_NB_REGS; i++) { | |
681 | s->reg_to_temp[i] = -1; | |
682 | } | |
683 | } | |
684 | ||
ac56dd48 PB |
685 | static char *tcg_get_arg_str_idx(TCGContext *s, char *buf, int buf_size, |
686 | int idx) | |
c896fe29 FB |
687 | { |
688 | TCGTemp *ts; | |
ac56dd48 PB |
689 | |
690 | ts = &s->temps[idx]; | |
691 | if (idx < s->nb_globals) { | |
692 | pstrcpy(buf, buf_size, ts->name); | |
c896fe29 | 693 | } else { |
c896fe29 FB |
694 | if (ts->val_type == TEMP_VAL_CONST) { |
695 | snprintf(buf, buf_size, "$0x%" TCG_PRIlx , ts->val); | |
696 | } else { | |
ac56dd48 | 697 | snprintf(buf, buf_size, "tmp%d", idx - s->nb_globals); |
c896fe29 FB |
698 | } |
699 | } | |
700 | return buf; | |
701 | } | |
702 | ||
ac56dd48 PB |
703 | char *tcg_get_arg_str(TCGContext *s, char *buf, int buf_size, TCGv arg) |
704 | { | |
705 | return tcg_get_arg_str_idx(s, buf, buf_size, GET_TCGV(arg)); | |
706 | } | |
707 | ||
c896fe29 FB |
708 | void tcg_dump_ops(TCGContext *s, FILE *outfile) |
709 | { | |
710 | const uint16_t *opc_ptr; | |
711 | const TCGArg *args; | |
712 | TCGArg arg; | |
713 | int c, i, k, nb_oargs, nb_iargs, nb_cargs; | |
714 | const TCGOpDef *def; | |
715 | char buf[128]; | |
716 | ||
717 | opc_ptr = gen_opc_buf; | |
718 | args = gen_opparam_buf; | |
719 | while (opc_ptr < gen_opc_ptr) { | |
720 | c = *opc_ptr++; | |
721 | def = &tcg_op_defs[c]; | |
722 | fprintf(outfile, " %s ", def->name); | |
723 | if (c == INDEX_op_call) { | |
724 | TCGArg arg; | |
725 | /* variable number of arguments */ | |
726 | arg = *args++; | |
727 | nb_oargs = arg >> 16; | |
728 | nb_iargs = arg & 0xffff; | |
729 | nb_cargs = def->nb_cargs; | |
c896fe29 | 730 | |
b03cce8e | 731 | /* function name */ |
c896fe29 | 732 | /* XXX: dump helper name for call */ |
ac56dd48 | 733 | fprintf(outfile, "%s", |
b03cce8e FB |
734 | tcg_get_arg_str_idx(s, buf, sizeof(buf), args[nb_oargs + nb_iargs - 1])); |
735 | /* flags */ | |
736 | fprintf(outfile, ",$0x%" TCG_PRIlx, | |
737 | args[nb_oargs + nb_iargs]); | |
738 | /* nb out args */ | |
739 | fprintf(outfile, ",$%d", nb_oargs); | |
740 | for(i = 0; i < nb_oargs; i++) { | |
741 | fprintf(outfile, ","); | |
742 | fprintf(outfile, "%s", | |
743 | tcg_get_arg_str_idx(s, buf, sizeof(buf), args[i])); | |
744 | } | |
745 | for(i = 0; i < (nb_iargs - 1); i++) { | |
c896fe29 | 746 | fprintf(outfile, ","); |
b03cce8e FB |
747 | fprintf(outfile, "%s", |
748 | tcg_get_arg_str_idx(s, buf, sizeof(buf), args[nb_oargs + i])); | |
749 | } | |
750 | } else { | |
751 | if (c == INDEX_op_nopn) { | |
752 | /* variable number of arguments */ | |
753 | nb_cargs = *args; | |
754 | nb_oargs = 0; | |
755 | nb_iargs = 0; | |
756 | } else { | |
757 | nb_oargs = def->nb_oargs; | |
758 | nb_iargs = def->nb_iargs; | |
759 | nb_cargs = def->nb_cargs; | |
760 | } | |
761 | ||
762 | k = 0; | |
763 | for(i = 0; i < nb_oargs; i++) { | |
764 | if (k != 0) | |
765 | fprintf(outfile, ","); | |
766 | fprintf(outfile, "%s", | |
767 | tcg_get_arg_str_idx(s, buf, sizeof(buf), args[k++])); | |
768 | } | |
769 | for(i = 0; i < nb_iargs; i++) { | |
770 | if (k != 0) | |
771 | fprintf(outfile, ","); | |
772 | fprintf(outfile, "%s", | |
773 | tcg_get_arg_str_idx(s, buf, sizeof(buf), args[k++])); | |
774 | } | |
775 | for(i = 0; i < nb_cargs; i++) { | |
776 | if (k != 0) | |
777 | fprintf(outfile, ","); | |
778 | arg = args[k++]; | |
779 | fprintf(outfile, "$0x%" TCG_PRIlx, arg); | |
780 | } | |
c896fe29 FB |
781 | } |
782 | fprintf(outfile, "\n"); | |
783 | args += nb_iargs + nb_oargs + nb_cargs; | |
784 | } | |
785 | } | |
786 | ||
787 | /* we give more priority to constraints with less registers */ | |
788 | static int get_constraint_priority(const TCGOpDef *def, int k) | |
789 | { | |
790 | const TCGArgConstraint *arg_ct; | |
791 | ||
792 | int i, n; | |
793 | arg_ct = &def->args_ct[k]; | |
794 | if (arg_ct->ct & TCG_CT_ALIAS) { | |
795 | /* an alias is equivalent to a single register */ | |
796 | n = 1; | |
797 | } else { | |
798 | if (!(arg_ct->ct & TCG_CT_REG)) | |
799 | return 0; | |
800 | n = 0; | |
801 | for(i = 0; i < TCG_TARGET_NB_REGS; i++) { | |
802 | if (tcg_regset_test_reg(arg_ct->u.regs, i)) | |
803 | n++; | |
804 | } | |
805 | } | |
806 | return TCG_TARGET_NB_REGS - n + 1; | |
807 | } | |
808 | ||
809 | /* sort from highest priority to lowest */ | |
810 | static void sort_constraints(TCGOpDef *def, int start, int n) | |
811 | { | |
812 | int i, j, p1, p2, tmp; | |
813 | ||
814 | for(i = 0; i < n; i++) | |
815 | def->sorted_args[start + i] = start + i; | |
816 | if (n <= 1) | |
817 | return; | |
818 | for(i = 0; i < n - 1; i++) { | |
819 | for(j = i + 1; j < n; j++) { | |
820 | p1 = get_constraint_priority(def, def->sorted_args[start + i]); | |
821 | p2 = get_constraint_priority(def, def->sorted_args[start + j]); | |
822 | if (p1 < p2) { | |
823 | tmp = def->sorted_args[start + i]; | |
824 | def->sorted_args[start + i] = def->sorted_args[start + j]; | |
825 | def->sorted_args[start + j] = tmp; | |
826 | } | |
827 | } | |
828 | } | |
829 | } | |
830 | ||
831 | void tcg_add_target_add_op_defs(const TCGTargetOpDef *tdefs) | |
832 | { | |
833 | int op; | |
834 | TCGOpDef *def; | |
835 | const char *ct_str; | |
836 | int i, nb_args; | |
837 | ||
838 | for(;;) { | |
839 | if (tdefs->op < 0) | |
840 | break; | |
841 | op = tdefs->op; | |
842 | assert(op >= 0 && op < NB_OPS); | |
843 | def = &tcg_op_defs[op]; | |
844 | nb_args = def->nb_iargs + def->nb_oargs; | |
845 | for(i = 0; i < nb_args; i++) { | |
846 | ct_str = tdefs->args_ct_str[i]; | |
847 | tcg_regset_clear(def->args_ct[i].u.regs); | |
848 | def->args_ct[i].ct = 0; | |
849 | if (ct_str[0] >= '0' && ct_str[0] <= '9') { | |
850 | int oarg; | |
851 | oarg = ct_str[0] - '0'; | |
852 | assert(oarg < def->nb_oargs); | |
853 | assert(def->args_ct[oarg].ct & TCG_CT_REG); | |
854 | /* TCG_CT_ALIAS is for the output arguments. The input | |
5ff9d6a4 | 855 | argument is tagged with TCG_CT_IALIAS. */ |
c896fe29 | 856 | def->args_ct[i] = def->args_ct[oarg]; |
5ff9d6a4 FB |
857 | def->args_ct[oarg].ct = TCG_CT_ALIAS; |
858 | def->args_ct[oarg].alias_index = i; | |
c896fe29 | 859 | def->args_ct[i].ct |= TCG_CT_IALIAS; |
5ff9d6a4 | 860 | def->args_ct[i].alias_index = oarg; |
c896fe29 FB |
861 | } else { |
862 | for(;;) { | |
863 | if (*ct_str == '\0') | |
864 | break; | |
865 | switch(*ct_str) { | |
866 | case 'i': | |
867 | def->args_ct[i].ct |= TCG_CT_CONST; | |
868 | ct_str++; | |
869 | break; | |
870 | default: | |
871 | if (target_parse_constraint(&def->args_ct[i], &ct_str) < 0) { | |
872 | fprintf(stderr, "Invalid constraint '%s' for arg %d of operation '%s'\n", | |
873 | ct_str, i, def->name); | |
874 | exit(1); | |
875 | } | |
876 | } | |
877 | } | |
878 | } | |
879 | } | |
880 | ||
881 | /* sort the constraints (XXX: this is just an heuristic) */ | |
882 | sort_constraints(def, 0, def->nb_oargs); | |
883 | sort_constraints(def, def->nb_oargs, def->nb_iargs); | |
884 | ||
885 | #if 0 | |
886 | { | |
887 | int i; | |
888 | ||
889 | printf("%s: sorted=", def->name); | |
890 | for(i = 0; i < def->nb_oargs + def->nb_iargs; i++) | |
891 | printf(" %d", def->sorted_args[i]); | |
892 | printf("\n"); | |
893 | } | |
894 | #endif | |
895 | tdefs++; | |
896 | } | |
897 | ||
898 | } | |
899 | ||
900 | #ifdef USE_LIVENESS_ANALYSIS | |
901 | ||
902 | /* set a nop for an operation using 'nb_args' */ | |
903 | static inline void tcg_set_nop(TCGContext *s, uint16_t *opc_ptr, | |
904 | TCGArg *args, int nb_args) | |
905 | { | |
906 | if (nb_args == 0) { | |
907 | *opc_ptr = INDEX_op_nop; | |
908 | } else { | |
909 | *opc_ptr = INDEX_op_nopn; | |
910 | args[0] = nb_args; | |
911 | args[nb_args - 1] = nb_args; | |
912 | } | |
913 | } | |
914 | ||
915 | /* liveness analysis: end of basic block: globals are live, temps are dead */ | |
916 | static inline void tcg_la_bb_end(TCGContext *s, uint8_t *dead_temps) | |
917 | { | |
918 | memset(dead_temps, 0, s->nb_globals); | |
919 | memset(dead_temps + s->nb_globals, 1, s->nb_temps - s->nb_globals); | |
920 | } | |
921 | ||
922 | /* Liveness analysis : update the opc_dead_iargs array to tell if a | |
923 | given input arguments is dead. Instructions updating dead | |
924 | temporaries are removed. */ | |
925 | void tcg_liveness_analysis(TCGContext *s) | |
926 | { | |
927 | int i, op_index, op, nb_args, nb_iargs, nb_oargs, arg, nb_ops; | |
928 | TCGArg *args; | |
929 | const TCGOpDef *def; | |
930 | uint8_t *dead_temps; | |
931 | unsigned int dead_iargs; | |
932 | ||
933 | gen_opc_ptr++; /* skip end */ | |
934 | ||
935 | nb_ops = gen_opc_ptr - gen_opc_buf; | |
936 | ||
937 | /* XXX: make it really dynamic */ | |
938 | s->op_dead_iargs = tcg_malloc(OPC_BUF_SIZE * sizeof(uint16_t)); | |
939 | ||
940 | dead_temps = tcg_malloc(s->nb_temps); | |
941 | memset(dead_temps, 1, s->nb_temps); | |
942 | ||
943 | args = gen_opparam_ptr; | |
944 | op_index = nb_ops - 1; | |
945 | while (op_index >= 0) { | |
946 | op = gen_opc_buf[op_index]; | |
947 | def = &tcg_op_defs[op]; | |
948 | switch(op) { | |
949 | case INDEX_op_call: | |
c6e113f5 FB |
950 | { |
951 | int call_flags; | |
c896fe29 | 952 | |
c6e113f5 FB |
953 | nb_args = args[-1]; |
954 | args -= nb_args; | |
955 | nb_iargs = args[0] & 0xffff; | |
956 | nb_oargs = args[0] >> 16; | |
957 | args++; | |
958 | call_flags = args[nb_oargs + nb_iargs]; | |
959 | ||
960 | /* pure functions can be removed if their result is not | |
961 | used */ | |
962 | if (call_flags & TCG_CALL_PURE) { | |
963 | for(i = 0; i < nb_oargs; i++) { | |
964 | arg = args[i]; | |
965 | if (!dead_temps[arg]) | |
966 | goto do_not_remove_call; | |
967 | } | |
968 | tcg_set_nop(s, gen_opc_buf + op_index, | |
969 | args - 1, nb_args); | |
970 | } else { | |
971 | do_not_remove_call: | |
c896fe29 | 972 | |
c6e113f5 FB |
973 | /* output args are dead */ |
974 | for(i = 0; i < nb_oargs; i++) { | |
975 | arg = args[i]; | |
976 | dead_temps[arg] = 1; | |
977 | } | |
978 | ||
979 | /* globals are live (they may be used by the call) */ | |
980 | memset(dead_temps, 0, s->nb_globals); | |
981 | ||
982 | /* input args are live */ | |
983 | dead_iargs = 0; | |
984 | for(i = 0; i < nb_iargs; i++) { | |
985 | arg = args[i + nb_oargs]; | |
986 | if (dead_temps[arg]) { | |
987 | dead_iargs |= (1 << i); | |
988 | } | |
989 | dead_temps[arg] = 0; | |
990 | } | |
991 | s->op_dead_iargs[op_index] = dead_iargs; | |
c896fe29 | 992 | } |
c6e113f5 | 993 | args--; |
c896fe29 | 994 | } |
c896fe29 FB |
995 | break; |
996 | case INDEX_op_set_label: | |
997 | args--; | |
998 | /* mark end of basic block */ | |
999 | tcg_la_bb_end(s, dead_temps); | |
1000 | break; | |
1001 | case INDEX_op_nopn: | |
1002 | nb_args = args[-1]; | |
1003 | args -= nb_args; | |
1004 | break; | |
5ff9d6a4 FB |
1005 | case INDEX_op_discard: |
1006 | args--; | |
1007 | /* mark the temporary as dead */ | |
1008 | dead_temps[args[0]] = 1; | |
1009 | break; | |
c896fe29 FB |
1010 | case INDEX_op_macro_2: |
1011 | { | |
1012 | int dead_args[2], macro_id; | |
1013 | int saved_op_index, saved_arg_index; | |
1014 | int macro_op_index, macro_arg_index; | |
1015 | int macro_end_op_index, macro_end_arg_index; | |
1016 | int last_nb_temps; | |
1017 | ||
1018 | nb_args = 3; | |
1019 | args -= nb_args; | |
1020 | dead_args[0] = dead_temps[args[0]]; | |
1021 | dead_args[1] = dead_temps[args[1]]; | |
1022 | macro_id = args[2]; | |
1023 | ||
1024 | /* call the macro function which generate code | |
1025 | depending on the live outputs */ | |
1026 | saved_op_index = op_index; | |
1027 | saved_arg_index = args - gen_opparam_buf; | |
1028 | ||
1029 | /* add a macro start instruction */ | |
1030 | *gen_opc_ptr++ = INDEX_op_macro_start; | |
1031 | *gen_opparam_ptr++ = saved_op_index; | |
1032 | *gen_opparam_ptr++ = saved_arg_index; | |
1033 | ||
1034 | macro_op_index = gen_opc_ptr - gen_opc_buf; | |
1035 | macro_arg_index = gen_opparam_ptr - gen_opparam_buf; | |
1036 | ||
1037 | last_nb_temps = s->nb_temps; | |
1038 | ||
1039 | s->macro_func(s, macro_id, dead_args); | |
1040 | ||
1041 | /* realloc temp info (XXX: make it faster) */ | |
1042 | if (s->nb_temps > last_nb_temps) { | |
1043 | uint8_t *new_dead_temps; | |
1044 | ||
1045 | new_dead_temps = tcg_malloc(s->nb_temps); | |
1046 | memcpy(new_dead_temps, dead_temps, last_nb_temps); | |
1047 | memset(new_dead_temps + last_nb_temps, 1, | |
1048 | s->nb_temps - last_nb_temps); | |
1049 | dead_temps = new_dead_temps; | |
1050 | } | |
1051 | ||
1052 | macro_end_op_index = gen_opc_ptr - gen_opc_buf; | |
1053 | macro_end_arg_index = gen_opparam_ptr - gen_opparam_buf; | |
1054 | ||
1055 | /* end of macro: add a goto to the next instruction */ | |
1056 | *gen_opc_ptr++ = INDEX_op_macro_end; | |
1057 | *gen_opparam_ptr++ = op_index + 1; | |
1058 | *gen_opparam_ptr++ = saved_arg_index + nb_args; | |
1059 | ||
1060 | /* modify the macro operation to be a macro_goto */ | |
1061 | gen_opc_buf[op_index] = INDEX_op_macro_goto; | |
1062 | args[0] = macro_op_index; | |
1063 | args[1] = macro_arg_index; | |
1064 | args[2] = 0; /* dummy third arg to match the | |
1065 | macro parameters */ | |
1066 | ||
1067 | /* set the next instruction to the end of the macro */ | |
1068 | op_index = macro_end_op_index; | |
1069 | args = macro_end_arg_index + gen_opparam_buf; | |
1070 | } | |
1071 | break; | |
1072 | case INDEX_op_macro_start: | |
1073 | args -= 2; | |
1074 | op_index = args[0]; | |
1075 | args = gen_opparam_buf + args[1]; | |
1076 | break; | |
1077 | case INDEX_op_macro_goto: | |
1078 | case INDEX_op_macro_end: | |
1079 | tcg_abort(); /* should never happen in liveness analysis */ | |
1080 | case INDEX_op_end: | |
1081 | break; | |
1082 | /* XXX: optimize by hardcoding common cases (e.g. triadic ops) */ | |
1083 | default: | |
1084 | if (op > INDEX_op_end) { | |
1085 | args -= def->nb_args; | |
1086 | nb_iargs = def->nb_iargs; | |
1087 | nb_oargs = def->nb_oargs; | |
1088 | ||
1089 | /* Test if the operation can be removed because all | |
5ff9d6a4 FB |
1090 | its outputs are dead. We assume that nb_oargs == 0 |
1091 | implies side effects */ | |
1092 | if (!(def->flags & TCG_OPF_SIDE_EFFECTS) && nb_oargs != 0) { | |
c896fe29 FB |
1093 | for(i = 0; i < nb_oargs; i++) { |
1094 | arg = args[i]; | |
1095 | if (!dead_temps[arg]) | |
1096 | goto do_not_remove; | |
1097 | } | |
1098 | tcg_set_nop(s, gen_opc_buf + op_index, args, def->nb_args); | |
1099 | #ifdef CONFIG_PROFILER | |
1100 | { | |
1101 | extern int64_t dyngen_tcg_del_op_count; | |
1102 | dyngen_tcg_del_op_count++; | |
1103 | } | |
1104 | #endif | |
1105 | } else { | |
1106 | do_not_remove: | |
1107 | ||
1108 | /* output args are dead */ | |
1109 | for(i = 0; i < nb_oargs; i++) { | |
1110 | arg = args[i]; | |
1111 | dead_temps[arg] = 1; | |
1112 | } | |
1113 | ||
1114 | /* if end of basic block, update */ | |
1115 | if (def->flags & TCG_OPF_BB_END) { | |
1116 | tcg_la_bb_end(s, dead_temps); | |
b03cce8e FB |
1117 | } else if (def->flags & TCG_OPF_CALL_CLOBBER) { |
1118 | /* globals are live */ | |
1119 | memset(dead_temps, 0, s->nb_globals); | |
c896fe29 FB |
1120 | } |
1121 | ||
1122 | /* input args are live */ | |
1123 | dead_iargs = 0; | |
1124 | for(i = 0; i < nb_iargs; i++) { | |
1125 | arg = args[i + nb_oargs]; | |
1126 | if (dead_temps[arg]) { | |
1127 | dead_iargs |= (1 << i); | |
1128 | } | |
1129 | dead_temps[arg] = 0; | |
1130 | } | |
1131 | s->op_dead_iargs[op_index] = dead_iargs; | |
1132 | } | |
1133 | } else { | |
1134 | /* legacy dyngen operations */ | |
1135 | args -= def->nb_args; | |
1136 | /* mark end of basic block */ | |
1137 | tcg_la_bb_end(s, dead_temps); | |
1138 | } | |
1139 | break; | |
1140 | } | |
1141 | op_index--; | |
1142 | } | |
1143 | ||
1144 | if (args != gen_opparam_buf) | |
1145 | tcg_abort(); | |
1146 | } | |
1147 | #else | |
1148 | /* dummy liveness analysis */ | |
1149 | void tcg_liveness_analysis(TCGContext *s) | |
1150 | { | |
1151 | int nb_ops; | |
1152 | nb_ops = gen_opc_ptr - gen_opc_buf; | |
1153 | ||
1154 | s->op_dead_iargs = tcg_malloc(nb_ops * sizeof(uint16_t)); | |
1155 | memset(s->op_dead_iargs, 0, nb_ops * sizeof(uint16_t)); | |
1156 | } | |
1157 | #endif | |
1158 | ||
1159 | #ifndef NDEBUG | |
1160 | static void dump_regs(TCGContext *s) | |
1161 | { | |
1162 | TCGTemp *ts; | |
1163 | int i; | |
1164 | char buf[64]; | |
1165 | ||
1166 | for(i = 0; i < s->nb_temps; i++) { | |
1167 | ts = &s->temps[i]; | |
ac56dd48 | 1168 | printf(" %10s: ", tcg_get_arg_str_idx(s, buf, sizeof(buf), i)); |
c896fe29 FB |
1169 | switch(ts->val_type) { |
1170 | case TEMP_VAL_REG: | |
1171 | printf("%s", tcg_target_reg_names[ts->reg]); | |
1172 | break; | |
1173 | case TEMP_VAL_MEM: | |
1174 | printf("%d(%s)", (int)ts->mem_offset, tcg_target_reg_names[ts->mem_reg]); | |
1175 | break; | |
1176 | case TEMP_VAL_CONST: | |
1177 | printf("$0x%" TCG_PRIlx, ts->val); | |
1178 | break; | |
1179 | case TEMP_VAL_DEAD: | |
1180 | printf("D"); | |
1181 | break; | |
1182 | default: | |
1183 | printf("???"); | |
1184 | break; | |
1185 | } | |
1186 | printf("\n"); | |
1187 | } | |
1188 | ||
1189 | for(i = 0; i < TCG_TARGET_NB_REGS; i++) { | |
1190 | if (s->reg_to_temp[i] >= 0) { | |
1191 | printf("%s: %s\n", | |
1192 | tcg_target_reg_names[i], | |
ac56dd48 | 1193 | tcg_get_arg_str_idx(s, buf, sizeof(buf), s->reg_to_temp[i])); |
c896fe29 FB |
1194 | } |
1195 | } | |
1196 | } | |
1197 | ||
1198 | static void check_regs(TCGContext *s) | |
1199 | { | |
1200 | int reg, k; | |
1201 | TCGTemp *ts; | |
1202 | char buf[64]; | |
1203 | ||
1204 | for(reg = 0; reg < TCG_TARGET_NB_REGS; reg++) { | |
1205 | k = s->reg_to_temp[reg]; | |
1206 | if (k >= 0) { | |
1207 | ts = &s->temps[k]; | |
1208 | if (ts->val_type != TEMP_VAL_REG || | |
1209 | ts->reg != reg) { | |
1210 | printf("Inconsistency for register %s:\n", | |
1211 | tcg_target_reg_names[reg]); | |
b03cce8e | 1212 | goto fail; |
c896fe29 FB |
1213 | } |
1214 | } | |
1215 | } | |
1216 | for(k = 0; k < s->nb_temps; k++) { | |
1217 | ts = &s->temps[k]; | |
1218 | if (ts->val_type == TEMP_VAL_REG && | |
1219 | !ts->fixed_reg && | |
1220 | s->reg_to_temp[ts->reg] != k) { | |
1221 | printf("Inconsistency for temp %s:\n", | |
ac56dd48 | 1222 | tcg_get_arg_str_idx(s, buf, sizeof(buf), k)); |
b03cce8e | 1223 | fail: |
c896fe29 FB |
1224 | printf("reg state:\n"); |
1225 | dump_regs(s); | |
1226 | tcg_abort(); | |
1227 | } | |
b03cce8e FB |
1228 | if (ts->val_type == TEMP_VAL_CONST && k < s->nb_globals) { |
1229 | printf("constant forbidden in global %s\n", | |
1230 | tcg_get_arg_str_idx(s, buf, sizeof(buf), k)); | |
1231 | goto fail; | |
1232 | } | |
c896fe29 FB |
1233 | } |
1234 | } | |
1235 | #endif | |
1236 | ||
1237 | static void temp_allocate_frame(TCGContext *s, int temp) | |
1238 | { | |
1239 | TCGTemp *ts; | |
1240 | ts = &s->temps[temp]; | |
1241 | s->current_frame_offset = (s->current_frame_offset + sizeof(tcg_target_long) - 1) & ~(sizeof(tcg_target_long) - 1); | |
1242 | if (s->current_frame_offset + sizeof(tcg_target_long) > s->frame_end) | |
5ff9d6a4 | 1243 | tcg_abort(); |
c896fe29 FB |
1244 | ts->mem_offset = s->current_frame_offset; |
1245 | ts->mem_reg = s->frame_reg; | |
1246 | ts->mem_allocated = 1; | |
1247 | s->current_frame_offset += sizeof(tcg_target_long); | |
1248 | } | |
1249 | ||
1250 | /* free register 'reg' by spilling the corresponding temporary if necessary */ | |
1251 | static void tcg_reg_free(TCGContext *s, int reg) | |
1252 | { | |
1253 | TCGTemp *ts; | |
1254 | int temp; | |
1255 | ||
1256 | temp = s->reg_to_temp[reg]; | |
1257 | if (temp != -1) { | |
1258 | ts = &s->temps[temp]; | |
1259 | assert(ts->val_type == TEMP_VAL_REG); | |
1260 | if (!ts->mem_coherent) { | |
1261 | if (!ts->mem_allocated) | |
1262 | temp_allocate_frame(s, temp); | |
e4d5434c | 1263 | tcg_out_st(s, ts->type, reg, ts->mem_reg, ts->mem_offset); |
c896fe29 FB |
1264 | } |
1265 | ts->val_type = TEMP_VAL_MEM; | |
1266 | s->reg_to_temp[reg] = -1; | |
1267 | } | |
1268 | } | |
1269 | ||
1270 | /* Allocate a register belonging to reg1 & ~reg2 */ | |
1271 | static int tcg_reg_alloc(TCGContext *s, TCGRegSet reg1, TCGRegSet reg2) | |
1272 | { | |
1273 | int i, reg; | |
1274 | TCGRegSet reg_ct; | |
1275 | ||
1276 | tcg_regset_andnot(reg_ct, reg1, reg2); | |
1277 | ||
1278 | /* first try free registers */ | |
0954d0d9 | 1279 | for(i = 0; i < ARRAY_SIZE(tcg_target_reg_alloc_order); i++) { |
c896fe29 FB |
1280 | reg = tcg_target_reg_alloc_order[i]; |
1281 | if (tcg_regset_test_reg(reg_ct, reg) && s->reg_to_temp[reg] == -1) | |
1282 | return reg; | |
1283 | } | |
1284 | ||
1285 | /* XXX: do better spill choice */ | |
0954d0d9 | 1286 | for(i = 0; i < ARRAY_SIZE(tcg_target_reg_alloc_order); i++) { |
c896fe29 FB |
1287 | reg = tcg_target_reg_alloc_order[i]; |
1288 | if (tcg_regset_test_reg(reg_ct, reg)) { | |
1289 | tcg_reg_free(s, reg); | |
1290 | return reg; | |
1291 | } | |
1292 | } | |
1293 | ||
1294 | tcg_abort(); | |
1295 | } | |
1296 | ||
e5097dc8 FB |
1297 | /* save globals to their cannonical location and assume they can be |
1298 | modified be the following code. */ | |
1299 | static void save_globals(TCGContext *s) | |
c896fe29 FB |
1300 | { |
1301 | TCGTemp *ts; | |
1302 | int i; | |
1303 | ||
1304 | for(i = 0; i < s->nb_globals; i++) { | |
1305 | ts = &s->temps[i]; | |
1306 | if (!ts->fixed_reg) { | |
1307 | if (ts->val_type == TEMP_VAL_REG) { | |
1308 | tcg_reg_free(s, ts->reg); | |
e5097dc8 FB |
1309 | } else if (ts->val_type == TEMP_VAL_DEAD) { |
1310 | ts->val_type = TEMP_VAL_MEM; | |
c896fe29 FB |
1311 | } |
1312 | } | |
1313 | } | |
e5097dc8 FB |
1314 | } |
1315 | ||
1316 | /* at the end of a basic block, we assume all temporaries are dead and | |
1317 | all globals are stored at their canonical location */ | |
1318 | /* XXX: optimize by handling constants in another array ? */ | |
1319 | void tcg_reg_alloc_bb_end(TCGContext *s) | |
1320 | { | |
1321 | TCGTemp *ts; | |
1322 | int i; | |
1323 | ||
1324 | save_globals(s); | |
c896fe29 FB |
1325 | |
1326 | for(i = s->nb_globals; i < s->nb_temps; i++) { | |
1327 | ts = &s->temps[i]; | |
1328 | if (ts->val_type != TEMP_VAL_CONST) { | |
1329 | if (ts->val_type == TEMP_VAL_REG) { | |
1330 | s->reg_to_temp[ts->reg] = -1; | |
1331 | } | |
1332 | ts->val_type = TEMP_VAL_DEAD; | |
1333 | } | |
1334 | } | |
1335 | } | |
1336 | ||
1337 | #define IS_DEAD_IARG(n) ((dead_iargs >> (n)) & 1) | |
1338 | ||
1339 | static void tcg_reg_alloc_mov(TCGContext *s, const TCGOpDef *def, | |
1340 | const TCGArg *args, | |
1341 | unsigned int dead_iargs) | |
1342 | { | |
1343 | TCGTemp *ts, *ots; | |
1344 | int reg; | |
1345 | const TCGArgConstraint *arg_ct; | |
1346 | ||
1347 | ots = &s->temps[args[0]]; | |
1348 | ts = &s->temps[args[1]]; | |
1349 | arg_ct = &def->args_ct[0]; | |
1350 | ||
1351 | if (ts->val_type == TEMP_VAL_REG) { | |
1352 | if (IS_DEAD_IARG(0) && !ts->fixed_reg && !ots->fixed_reg) { | |
1353 | /* the mov can be suppressed */ | |
1354 | if (ots->val_type == TEMP_VAL_REG) | |
1355 | s->reg_to_temp[ots->reg] = -1; | |
1356 | reg = ts->reg; | |
1357 | s->reg_to_temp[reg] = -1; | |
1358 | ts->val_type = TEMP_VAL_DEAD; | |
1359 | } else { | |
1360 | if (ots->val_type == TEMP_VAL_REG) { | |
1361 | reg = ots->reg; | |
1362 | } else { | |
1363 | reg = tcg_reg_alloc(s, arg_ct->u.regs, s->reserved_regs); | |
1364 | } | |
1365 | if (ts->reg != reg) { | |
1366 | tcg_out_mov(s, reg, ts->reg); | |
1367 | } | |
1368 | } | |
1369 | } else if (ts->val_type == TEMP_VAL_MEM) { | |
1370 | if (ots->val_type == TEMP_VAL_REG) { | |
1371 | reg = ots->reg; | |
1372 | } else { | |
1373 | reg = tcg_reg_alloc(s, arg_ct->u.regs, s->reserved_regs); | |
1374 | } | |
e4d5434c | 1375 | tcg_out_ld(s, ts->type, reg, ts->mem_reg, ts->mem_offset); |
c896fe29 FB |
1376 | } else if (ts->val_type == TEMP_VAL_CONST) { |
1377 | if (ots->val_type == TEMP_VAL_REG) { | |
1378 | reg = ots->reg; | |
1379 | } else { | |
1380 | reg = tcg_reg_alloc(s, arg_ct->u.regs, s->reserved_regs); | |
1381 | } | |
1382 | tcg_out_movi(s, ots->type, reg, ts->val); | |
1383 | } else { | |
1384 | tcg_abort(); | |
1385 | } | |
1386 | s->reg_to_temp[reg] = args[0]; | |
1387 | ots->reg = reg; | |
1388 | ots->val_type = TEMP_VAL_REG; | |
1389 | ots->mem_coherent = 0; | |
1390 | } | |
1391 | ||
1392 | static void tcg_reg_alloc_op(TCGContext *s, | |
1393 | const TCGOpDef *def, int opc, | |
1394 | const TCGArg *args, | |
1395 | unsigned int dead_iargs) | |
1396 | { | |
1397 | TCGRegSet allocated_regs; | |
1398 | int i, k, nb_iargs, nb_oargs, reg; | |
1399 | TCGArg arg; | |
1400 | const TCGArgConstraint *arg_ct; | |
1401 | TCGTemp *ts; | |
1402 | TCGArg new_args[TCG_MAX_OP_ARGS]; | |
1403 | int const_args[TCG_MAX_OP_ARGS]; | |
1404 | ||
1405 | nb_oargs = def->nb_oargs; | |
1406 | nb_iargs = def->nb_iargs; | |
1407 | ||
1408 | /* copy constants */ | |
1409 | memcpy(new_args + nb_oargs + nb_iargs, | |
1410 | args + nb_oargs + nb_iargs, | |
1411 | sizeof(TCGArg) * def->nb_cargs); | |
1412 | ||
1413 | /* satisfy input constraints */ | |
1414 | tcg_regset_set(allocated_regs, s->reserved_regs); | |
1415 | for(k = 0; k < nb_iargs; k++) { | |
1416 | i = def->sorted_args[nb_oargs + k]; | |
1417 | arg = args[i]; | |
1418 | arg_ct = &def->args_ct[i]; | |
1419 | ts = &s->temps[arg]; | |
1420 | if (ts->val_type == TEMP_VAL_MEM) { | |
1421 | reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs); | |
e4d5434c | 1422 | tcg_out_ld(s, ts->type, reg, ts->mem_reg, ts->mem_offset); |
c896fe29 FB |
1423 | ts->val_type = TEMP_VAL_REG; |
1424 | ts->reg = reg; | |
1425 | ts->mem_coherent = 1; | |
1426 | s->reg_to_temp[reg] = arg; | |
1427 | } else if (ts->val_type == TEMP_VAL_CONST) { | |
1428 | if (tcg_target_const_match(ts->val, arg_ct)) { | |
1429 | /* constant is OK for instruction */ | |
1430 | const_args[i] = 1; | |
1431 | new_args[i] = ts->val; | |
1432 | goto iarg_end; | |
1433 | } else { | |
1434 | /* need to move to a register*/ | |
1435 | reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs); | |
1436 | tcg_out_movi(s, ts->type, reg, ts->val); | |
1437 | goto iarg_end1; | |
1438 | } | |
1439 | } | |
1440 | assert(ts->val_type == TEMP_VAL_REG); | |
5ff9d6a4 FB |
1441 | if (arg_ct->ct & TCG_CT_IALIAS) { |
1442 | if (ts->fixed_reg) { | |
1443 | /* if fixed register, we must allocate a new register | |
1444 | if the alias is not the same register */ | |
1445 | if (arg != args[arg_ct->alias_index]) | |
1446 | goto allocate_in_reg; | |
1447 | } else { | |
1448 | /* if the input is aliased to an output and if it is | |
1449 | not dead after the instruction, we must allocate | |
1450 | a new register and move it */ | |
1451 | if (!IS_DEAD_IARG(i - nb_oargs)) | |
1452 | goto allocate_in_reg; | |
1453 | } | |
c896fe29 FB |
1454 | } |
1455 | reg = ts->reg; | |
1456 | if (tcg_regset_test_reg(arg_ct->u.regs, reg)) { | |
1457 | /* nothing to do : the constraint is satisfied */ | |
1458 | } else { | |
1459 | allocate_in_reg: | |
1460 | /* allocate a new register matching the constraint | |
1461 | and move the temporary register into it */ | |
1462 | reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs); | |
1463 | tcg_out_mov(s, reg, ts->reg); | |
1464 | } | |
1465 | iarg_end1: | |
1466 | new_args[i] = reg; | |
1467 | const_args[i] = 0; | |
1468 | tcg_regset_set_reg(allocated_regs, reg); | |
1469 | iarg_end: ; | |
1470 | } | |
1471 | ||
1472 | /* mark dead temporaries and free the associated registers */ | |
1473 | for(i = 0; i < nb_iargs; i++) { | |
1474 | arg = args[nb_oargs + i]; | |
1475 | if (IS_DEAD_IARG(i)) { | |
1476 | ts = &s->temps[arg]; | |
1477 | if (ts->val_type != TEMP_VAL_CONST && !ts->fixed_reg) { | |
1478 | if (ts->val_type == TEMP_VAL_REG) | |
1479 | s->reg_to_temp[ts->reg] = -1; | |
1480 | ts->val_type = TEMP_VAL_DEAD; | |
1481 | } | |
1482 | } | |
1483 | } | |
1484 | ||
c896fe29 | 1485 | if (def->flags & TCG_OPF_CALL_CLOBBER) { |
b03cce8e | 1486 | /* XXX: permit generic clobber register list ? */ |
c896fe29 FB |
1487 | for(reg = 0; reg < TCG_TARGET_NB_REGS; reg++) { |
1488 | if (tcg_regset_test_reg(tcg_target_call_clobber_regs, reg)) { | |
1489 | tcg_reg_free(s, reg); | |
1490 | } | |
1491 | } | |
b03cce8e FB |
1492 | /* XXX: for load/store we could do that only for the slow path |
1493 | (i.e. when a memory callback is called) */ | |
1494 | ||
1495 | /* store globals and free associated registers (we assume the insn | |
1496 | can modify any global. */ | |
e5097dc8 | 1497 | save_globals(s); |
c896fe29 FB |
1498 | } |
1499 | ||
1500 | /* satisfy the output constraints */ | |
1501 | tcg_regset_set(allocated_regs, s->reserved_regs); | |
1502 | for(k = 0; k < nb_oargs; k++) { | |
1503 | i = def->sorted_args[k]; | |
1504 | arg = args[i]; | |
1505 | arg_ct = &def->args_ct[i]; | |
1506 | ts = &s->temps[arg]; | |
1507 | if (arg_ct->ct & TCG_CT_ALIAS) { | |
5ff9d6a4 | 1508 | reg = new_args[arg_ct->alias_index]; |
c896fe29 FB |
1509 | } else { |
1510 | /* if fixed register, we try to use it */ | |
1511 | reg = ts->reg; | |
1512 | if (ts->fixed_reg && | |
1513 | tcg_regset_test_reg(arg_ct->u.regs, reg)) { | |
1514 | goto oarg_end; | |
1515 | } | |
1516 | reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs); | |
1517 | } | |
1518 | tcg_regset_set_reg(allocated_regs, reg); | |
1519 | /* if a fixed register is used, then a move will be done afterwards */ | |
1520 | if (!ts->fixed_reg) { | |
1521 | if (ts->val_type == TEMP_VAL_REG) | |
1522 | s->reg_to_temp[ts->reg] = -1; | |
1523 | ts->val_type = TEMP_VAL_REG; | |
1524 | ts->reg = reg; | |
1525 | /* temp value is modified, so the value kept in memory is | |
1526 | potentially not the same */ | |
1527 | ts->mem_coherent = 0; | |
1528 | s->reg_to_temp[reg] = arg; | |
1529 | } | |
1530 | oarg_end: | |
1531 | new_args[i] = reg; | |
1532 | } | |
1533 | ||
1534 | if (def->flags & TCG_OPF_BB_END) | |
1535 | tcg_reg_alloc_bb_end(s); | |
1536 | ||
1537 | /* emit instruction */ | |
1538 | tcg_out_op(s, opc, new_args, const_args); | |
1539 | ||
1540 | /* move the outputs in the correct register if needed */ | |
1541 | for(i = 0; i < nb_oargs; i++) { | |
1542 | ts = &s->temps[args[i]]; | |
1543 | reg = new_args[i]; | |
1544 | if (ts->fixed_reg && ts->reg != reg) { | |
1545 | tcg_out_mov(s, ts->reg, reg); | |
1546 | } | |
1547 | } | |
1548 | } | |
1549 | ||
b03cce8e FB |
1550 | #ifdef TCG_TARGET_STACK_GROWSUP |
1551 | #define STACK_DIR(x) (-(x)) | |
1552 | #else | |
1553 | #define STACK_DIR(x) (x) | |
1554 | #endif | |
1555 | ||
c896fe29 FB |
1556 | static int tcg_reg_alloc_call(TCGContext *s, const TCGOpDef *def, |
1557 | int opc, const TCGArg *args, | |
1558 | unsigned int dead_iargs) | |
1559 | { | |
1560 | int nb_iargs, nb_oargs, flags, nb_regs, i, reg, nb_params; | |
1561 | TCGArg arg, func_arg; | |
1562 | TCGTemp *ts; | |
f54b3f92 | 1563 | tcg_target_long stack_offset, call_stack_size, func_addr; |
b03cce8e | 1564 | int const_func_arg, allocate_args; |
c896fe29 FB |
1565 | TCGRegSet allocated_regs; |
1566 | const TCGArgConstraint *arg_ct; | |
1567 | ||
1568 | arg = *args++; | |
1569 | ||
1570 | nb_oargs = arg >> 16; | |
1571 | nb_iargs = arg & 0xffff; | |
1572 | nb_params = nb_iargs - 1; | |
1573 | ||
1574 | flags = args[nb_oargs + nb_iargs]; | |
1575 | ||
1576 | nb_regs = tcg_target_get_call_iarg_regs_count(flags); | |
1577 | if (nb_regs > nb_params) | |
1578 | nb_regs = nb_params; | |
1579 | ||
1580 | /* assign stack slots first */ | |
1581 | /* XXX: preallocate call stack */ | |
1582 | call_stack_size = (nb_params - nb_regs) * sizeof(tcg_target_long); | |
1583 | call_stack_size = (call_stack_size + TCG_TARGET_STACK_ALIGN - 1) & | |
1584 | ~(TCG_TARGET_STACK_ALIGN - 1); | |
b03cce8e FB |
1585 | allocate_args = (call_stack_size > TCG_STATIC_CALL_ARGS_SIZE); |
1586 | if (allocate_args) { | |
1587 | tcg_out_addi(s, TCG_REG_CALL_STACK, -STACK_DIR(call_stack_size)); | |
1588 | } | |
1589 | /* XXX: on some architectures it does not start at zero */ | |
c896fe29 FB |
1590 | stack_offset = 0; |
1591 | for(i = nb_regs; i < nb_params; i++) { | |
1592 | arg = args[nb_oargs + i]; | |
1593 | ts = &s->temps[arg]; | |
1594 | if (ts->val_type == TEMP_VAL_REG) { | |
e4d5434c | 1595 | tcg_out_st(s, ts->type, ts->reg, TCG_REG_CALL_STACK, stack_offset); |
c896fe29 FB |
1596 | } else if (ts->val_type == TEMP_VAL_MEM) { |
1597 | reg = tcg_reg_alloc(s, tcg_target_available_regs[ts->type], | |
1598 | s->reserved_regs); | |
1599 | /* XXX: not correct if reading values from the stack */ | |
e4d5434c BS |
1600 | tcg_out_ld(s, ts->type, reg, ts->mem_reg, ts->mem_offset); |
1601 | tcg_out_st(s, ts->type, reg, TCG_REG_CALL_STACK, stack_offset); | |
c896fe29 FB |
1602 | } else if (ts->val_type == TEMP_VAL_CONST) { |
1603 | reg = tcg_reg_alloc(s, tcg_target_available_regs[ts->type], | |
1604 | s->reserved_regs); | |
1605 | /* XXX: sign extend may be needed on some targets */ | |
1606 | tcg_out_movi(s, ts->type, reg, ts->val); | |
e4d5434c | 1607 | tcg_out_st(s, ts->type, reg, TCG_REG_CALL_STACK, stack_offset); |
c896fe29 FB |
1608 | } else { |
1609 | tcg_abort(); | |
1610 | } | |
b03cce8e FB |
1611 | /* XXX: not necessarily in the same order */ |
1612 | stack_offset += STACK_DIR(sizeof(tcg_target_long)); | |
c896fe29 FB |
1613 | } |
1614 | ||
1615 | /* assign input registers */ | |
1616 | tcg_regset_set(allocated_regs, s->reserved_regs); | |
1617 | for(i = 0; i < nb_regs; i++) { | |
1618 | arg = args[nb_oargs + i]; | |
1619 | ts = &s->temps[arg]; | |
1620 | reg = tcg_target_call_iarg_regs[i]; | |
1621 | tcg_reg_free(s, reg); | |
1622 | if (ts->val_type == TEMP_VAL_REG) { | |
1623 | if (ts->reg != reg) { | |
1624 | tcg_out_mov(s, reg, ts->reg); | |
1625 | } | |
1626 | } else if (ts->val_type == TEMP_VAL_MEM) { | |
e4d5434c | 1627 | tcg_out_ld(s, ts->type, reg, ts->mem_reg, ts->mem_offset); |
c896fe29 FB |
1628 | } else if (ts->val_type == TEMP_VAL_CONST) { |
1629 | /* XXX: sign extend ? */ | |
1630 | tcg_out_movi(s, ts->type, reg, ts->val); | |
1631 | } else { | |
1632 | tcg_abort(); | |
1633 | } | |
1634 | tcg_regset_set_reg(allocated_regs, reg); | |
1635 | } | |
1636 | ||
1637 | /* assign function address */ | |
1638 | func_arg = args[nb_oargs + nb_iargs - 1]; | |
1639 | arg_ct = &def->args_ct[0]; | |
1640 | ts = &s->temps[func_arg]; | |
f54b3f92 | 1641 | func_addr = ts->val; |
c896fe29 FB |
1642 | const_func_arg = 0; |
1643 | if (ts->val_type == TEMP_VAL_MEM) { | |
1644 | reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs); | |
e4d5434c | 1645 | tcg_out_ld(s, ts->type, reg, ts->mem_reg, ts->mem_offset); |
c896fe29 FB |
1646 | func_arg = reg; |
1647 | } else if (ts->val_type == TEMP_VAL_REG) { | |
1648 | reg = ts->reg; | |
1649 | if (!tcg_regset_test_reg(arg_ct->u.regs, reg)) { | |
1650 | reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs); | |
1651 | tcg_out_mov(s, reg, ts->reg); | |
1652 | } | |
1653 | func_arg = reg; | |
1654 | } else if (ts->val_type == TEMP_VAL_CONST) { | |
f54b3f92 | 1655 | if (tcg_target_const_match(func_addr, arg_ct)) { |
c896fe29 | 1656 | const_func_arg = 1; |
f54b3f92 | 1657 | func_arg = func_addr; |
c896fe29 FB |
1658 | } else { |
1659 | reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs); | |
f54b3f92 | 1660 | tcg_out_movi(s, ts->type, reg, func_addr); |
c896fe29 FB |
1661 | func_arg = reg; |
1662 | } | |
1663 | } else { | |
1664 | tcg_abort(); | |
1665 | } | |
1666 | ||
1667 | /* mark dead temporaries and free the associated registers */ | |
c6e113f5 | 1668 | for(i = 0; i < nb_iargs; i++) { |
c896fe29 FB |
1669 | arg = args[nb_oargs + i]; |
1670 | if (IS_DEAD_IARG(i)) { | |
1671 | ts = &s->temps[arg]; | |
1672 | if (ts->val_type != TEMP_VAL_CONST && !ts->fixed_reg) { | |
1673 | if (ts->val_type == TEMP_VAL_REG) | |
1674 | s->reg_to_temp[ts->reg] = -1; | |
1675 | ts->val_type = TEMP_VAL_DEAD; | |
1676 | } | |
1677 | } | |
1678 | } | |
1679 | ||
1680 | /* clobber call registers */ | |
1681 | for(reg = 0; reg < TCG_TARGET_NB_REGS; reg++) { | |
1682 | if (tcg_regset_test_reg(tcg_target_call_clobber_regs, reg)) { | |
1683 | tcg_reg_free(s, reg); | |
1684 | } | |
1685 | } | |
1686 | ||
1687 | /* store globals and free associated registers (we assume the call | |
1688 | can modify any global. */ | |
e5097dc8 | 1689 | save_globals(s); |
c896fe29 FB |
1690 | |
1691 | tcg_out_op(s, opc, &func_arg, &const_func_arg); | |
1692 | ||
b03cce8e FB |
1693 | if (allocate_args) { |
1694 | tcg_out_addi(s, TCG_REG_CALL_STACK, STACK_DIR(call_stack_size)); | |
1695 | } | |
c896fe29 FB |
1696 | |
1697 | /* assign output registers and emit moves if needed */ | |
1698 | for(i = 0; i < nb_oargs; i++) { | |
1699 | arg = args[i]; | |
1700 | ts = &s->temps[arg]; | |
1701 | reg = tcg_target_call_oarg_regs[i]; | |
1702 | tcg_reg_free(s, reg); | |
1703 | if (ts->fixed_reg) { | |
1704 | if (ts->reg != reg) { | |
1705 | tcg_out_mov(s, ts->reg, reg); | |
1706 | } | |
1707 | } else { | |
1708 | if (ts->val_type == TEMP_VAL_REG) | |
1709 | s->reg_to_temp[ts->reg] = -1; | |
1710 | ts->val_type = TEMP_VAL_REG; | |
1711 | ts->reg = reg; | |
1712 | ts->mem_coherent = 0; | |
1713 | s->reg_to_temp[reg] = arg; | |
1714 | } | |
1715 | } | |
1716 | ||
1717 | return nb_iargs + nb_oargs + def->nb_cargs + 1; | |
1718 | } | |
1719 | ||
1720 | #ifdef CONFIG_PROFILER | |
1721 | ||
1722 | static int64_t dyngen_table_op_count[NB_OPS]; | |
1723 | ||
1724 | void dump_op_count(void) | |
1725 | { | |
1726 | int i; | |
1727 | FILE *f; | |
1728 | f = fopen("/tmp/op1.log", "w"); | |
1729 | for(i = 0; i < INDEX_op_end; i++) { | |
1730 | fprintf(f, "%s %" PRId64 "\n", tcg_op_defs[i].name, dyngen_table_op_count[i]); | |
1731 | } | |
1732 | fclose(f); | |
1733 | f = fopen("/tmp/op2.log", "w"); | |
1734 | for(i = INDEX_op_end; i < NB_OPS; i++) { | |
1735 | fprintf(f, "%s %" PRId64 "\n", tcg_op_defs[i].name, dyngen_table_op_count[i]); | |
1736 | } | |
1737 | fclose(f); | |
1738 | } | |
1739 | #endif | |
1740 | ||
1741 | ||
1742 | static inline int tcg_gen_code_common(TCGContext *s, uint8_t *gen_code_buf, | |
2ba1eeb6 | 1743 | long search_pc) |
c896fe29 FB |
1744 | { |
1745 | int opc, op_index, macro_op_index; | |
1746 | const TCGOpDef *def; | |
1747 | unsigned int dead_iargs; | |
1748 | const TCGArg *args; | |
1749 | ||
1750 | #ifdef DEBUG_DISAS | |
1751 | if (unlikely(loglevel & CPU_LOG_TB_OP)) { | |
1752 | fprintf(logfile, "OP:\n"); | |
1753 | tcg_dump_ops(s, logfile); | |
1754 | fprintf(logfile, "\n"); | |
1755 | } | |
1756 | #endif | |
1757 | ||
1758 | tcg_liveness_analysis(s); | |
1759 | ||
1760 | #ifdef DEBUG_DISAS | |
1761 | if (unlikely(loglevel & CPU_LOG_TB_OP_OPT)) { | |
1762 | fprintf(logfile, "OP after la:\n"); | |
1763 | tcg_dump_ops(s, logfile); | |
1764 | fprintf(logfile, "\n"); | |
1765 | } | |
1766 | #endif | |
1767 | ||
1768 | tcg_reg_alloc_start(s); | |
1769 | ||
1770 | s->code_buf = gen_code_buf; | |
1771 | s->code_ptr = gen_code_buf; | |
1772 | ||
1773 | macro_op_index = -1; | |
1774 | args = gen_opparam_buf; | |
1775 | op_index = 0; | |
b3db8758 | 1776 | |
c896fe29 FB |
1777 | for(;;) { |
1778 | opc = gen_opc_buf[op_index]; | |
1779 | #ifdef CONFIG_PROFILER | |
1780 | dyngen_table_op_count[opc]++; | |
1781 | #endif | |
1782 | def = &tcg_op_defs[opc]; | |
1783 | #if 0 | |
1784 | printf("%s: %d %d %d\n", def->name, | |
1785 | def->nb_oargs, def->nb_iargs, def->nb_cargs); | |
1786 | // dump_regs(s); | |
1787 | #endif | |
1788 | switch(opc) { | |
1789 | case INDEX_op_mov_i32: | |
1790 | #if TCG_TARGET_REG_BITS == 64 | |
1791 | case INDEX_op_mov_i64: | |
1792 | #endif | |
1793 | dead_iargs = s->op_dead_iargs[op_index]; | |
1794 | tcg_reg_alloc_mov(s, def, args, dead_iargs); | |
1795 | break; | |
1796 | case INDEX_op_nop: | |
1797 | case INDEX_op_nop1: | |
1798 | case INDEX_op_nop2: | |
1799 | case INDEX_op_nop3: | |
1800 | break; | |
1801 | case INDEX_op_nopn: | |
1802 | args += args[0]; | |
1803 | goto next; | |
5ff9d6a4 FB |
1804 | case INDEX_op_discard: |
1805 | { | |
1806 | TCGTemp *ts; | |
1807 | ts = &s->temps[args[0]]; | |
1808 | /* mark the temporary as dead */ | |
1809 | if (ts->val_type != TEMP_VAL_CONST && !ts->fixed_reg) { | |
1810 | if (ts->val_type == TEMP_VAL_REG) | |
1811 | s->reg_to_temp[ts->reg] = -1; | |
1812 | ts->val_type = TEMP_VAL_DEAD; | |
1813 | } | |
1814 | } | |
1815 | break; | |
c896fe29 FB |
1816 | case INDEX_op_macro_goto: |
1817 | macro_op_index = op_index; /* only used for exceptions */ | |
1818 | op_index = args[0] - 1; | |
1819 | args = gen_opparam_buf + args[1]; | |
1820 | goto next; | |
1821 | case INDEX_op_macro_end: | |
1822 | macro_op_index = -1; /* only used for exceptions */ | |
1823 | op_index = args[0] - 1; | |
1824 | args = gen_opparam_buf + args[1]; | |
1825 | goto next; | |
1826 | case INDEX_op_macro_start: | |
1827 | /* must never happen here */ | |
1828 | tcg_abort(); | |
1829 | case INDEX_op_set_label: | |
1830 | tcg_reg_alloc_bb_end(s); | |
1831 | tcg_out_label(s, args[0], (long)s->code_ptr); | |
1832 | break; | |
1833 | case INDEX_op_call: | |
1834 | dead_iargs = s->op_dead_iargs[op_index]; | |
1835 | args += tcg_reg_alloc_call(s, def, opc, args, dead_iargs); | |
1836 | goto next; | |
1837 | case INDEX_op_end: | |
1838 | goto the_end; | |
cf2be984 | 1839 | |
bf6247fb | 1840 | #ifdef CONFIG_DYNGEN_OP |
c896fe29 FB |
1841 | case 0 ... INDEX_op_end - 1: |
1842 | /* legacy dyngen ops */ | |
1843 | #ifdef CONFIG_PROFILER | |
1844 | { | |
1845 | extern int64_t dyngen_old_op_count; | |
1846 | dyngen_old_op_count++; | |
1847 | } | |
1848 | #endif | |
1849 | tcg_reg_alloc_bb_end(s); | |
2ba1eeb6 | 1850 | if (search_pc >= 0) { |
c896fe29 FB |
1851 | s->code_ptr += def->copy_size; |
1852 | args += def->nb_args; | |
1853 | } else { | |
1854 | args = dyngen_op(s, opc, args); | |
1855 | } | |
1856 | goto next; | |
cf2be984 | 1857 | #endif |
c896fe29 FB |
1858 | default: |
1859 | /* Note: in order to speed up the code, it would be much | |
1860 | faster to have specialized register allocator functions for | |
1861 | some common argument patterns */ | |
1862 | dead_iargs = s->op_dead_iargs[op_index]; | |
1863 | tcg_reg_alloc_op(s, def, opc, args, dead_iargs); | |
1864 | break; | |
1865 | } | |
1866 | args += def->nb_args; | |
1867 | next: ; | |
2ba1eeb6 PB |
1868 | if (search_pc >= 0 && search_pc < s->code_ptr - gen_code_buf) { |
1869 | if (macro_op_index >= 0) | |
1870 | return macro_op_index; | |
1871 | else | |
1872 | return op_index; | |
c896fe29 FB |
1873 | } |
1874 | op_index++; | |
1875 | #ifndef NDEBUG | |
1876 | check_regs(s); | |
1877 | #endif | |
1878 | } | |
1879 | the_end: | |
1880 | return -1; | |
1881 | } | |
1882 | ||
1883 | int dyngen_code(TCGContext *s, uint8_t *gen_code_buf) | |
1884 | { | |
1885 | #ifdef CONFIG_PROFILER | |
1886 | { | |
1887 | extern int64_t dyngen_op_count; | |
1888 | extern int dyngen_op_count_max; | |
1889 | int n; | |
1890 | n = (gen_opc_ptr - gen_opc_buf); | |
1891 | dyngen_op_count += n; | |
1892 | if (n > dyngen_op_count_max) | |
1893 | dyngen_op_count_max = n; | |
1894 | } | |
1895 | #endif | |
1896 | ||
2ba1eeb6 | 1897 | tcg_gen_code_common(s, gen_code_buf, -1); |
c896fe29 FB |
1898 | |
1899 | /* flush instruction cache */ | |
1900 | flush_icache_range((unsigned long)gen_code_buf, | |
1901 | (unsigned long)s->code_ptr); | |
1902 | return s->code_ptr - gen_code_buf; | |
1903 | } | |
1904 | ||
2ba1eeb6 | 1905 | /* Return the index of the micro operation such as the pc after is < |
623e265c PB |
1906 | offset bytes from the start of the TB. The contents of gen_code_buf must |
1907 | not be changed, though writing the same values is ok. | |
1908 | Return -1 if not found. */ | |
1909 | int dyngen_code_search_pc(TCGContext *s, uint8_t *gen_code_buf, long offset) | |
c896fe29 | 1910 | { |
623e265c | 1911 | return tcg_gen_code_common(s, gen_code_buf, offset); |
c896fe29 | 1912 | } |