Statistics
| Branch: | Revision:

root / tcg / tcg.c @ c896fe29

History | View | Annotate | Download (51.8 kB)

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>
37

    
38
#include "config.h"
39
#include "osdep.h"
40

    
41
/* Note: the long term plan is to reduce the dependancies on the QEMU
42
   CPU definitions. Currently they are used for qemu_ld/st
43
   instructions */
44
#define NO_CPU_IO_DEFS
45
#include "cpu.h"
46
#include "exec-all.h"
47

    
48
#include "tcg-op.h"
49
#include "elf.h"
50

    
51

    
52
static void patch_reloc(uint8_t *code_ptr, int type, 
53
                        tcg_target_long value);
54

    
55
TCGOpDef tcg_op_defs[] = {
56
#define DEF(s, n, copy_size) { #s, 0, 0, n, n, 0, copy_size },
57
#define DEF2(s, iargs, oargs, cargs, flags) { #s, iargs, oargs, cargs, iargs + oargs + cargs, flags, 0 },
58
#include "tcg-opc.h"
59
#undef DEF
60
#undef DEF2
61
};
62

    
63
TCGRegSet tcg_target_available_regs[2];
64
TCGRegSet tcg_target_call_clobber_regs;
65

    
66
/* XXX: move that inside the context */
67
uint16_t *gen_opc_ptr;
68
TCGArg *gen_opparam_ptr;
69

    
70
static inline void tcg_out8(TCGContext *s, uint8_t v)
71
{
72
    *s->code_ptr++ = v;
73
}
74

    
75
static inline void tcg_out16(TCGContext *s, uint16_t v)
76
{
77
    *(uint16_t *)s->code_ptr = v;
78
    s->code_ptr += 2;
79
}
80

    
81
static inline void tcg_out32(TCGContext *s, uint32_t v)
82
{
83
    *(uint32_t *)s->code_ptr = v;
84
    s->code_ptr += 4;
85
}
86

    
87
/* label relocation processing */
88

    
89
void tcg_out_reloc(TCGContext *s, uint8_t *code_ptr, int type, 
90
                   int label_index, long addend)
91
{
92
    TCGLabel *l;
93
    TCGRelocation *r;
94

    
95
    l = &s->labels[label_index];
96
    if (l->has_value) {
97
        patch_reloc(code_ptr, type, l->u.value + addend);
98
    } else {
99
        /* add a new relocation entry */
100
        r = tcg_malloc(sizeof(TCGRelocation));
101
        r->type = type;
102
        r->ptr = code_ptr;
103
        r->addend = addend;
104
        r->next = l->u.first_reloc;
105
        l->u.first_reloc = r;
106
    }
107
}
108

    
109
static void tcg_out_label(TCGContext *s, int label_index, 
110
                          tcg_target_long value)
111
{
112
    TCGLabel *l;
113
    TCGRelocation *r;
114

    
115
    l = &s->labels[label_index];
116
    if (l->has_value)
117
        tcg_abort();
118
    r = l->u.first_reloc;
119
    while (r != NULL) {
120
        patch_reloc(r->ptr, r->type, value + r->addend);
121
        r = r->next;
122
    }
123
    l->has_value = 1;
124
    l->u.value = value;
125
}
126

    
127
int gen_new_label(void)
128
{
129
    TCGContext *s = &tcg_ctx;
130
    int idx;
131
    TCGLabel *l;
132

    
133
    if (s->nb_labels >= TCG_MAX_LABELS)
134
        tcg_abort();
135
    idx = s->nb_labels++;
136
    l = &s->labels[idx];
137
    l->has_value = 0;
138
    l->u.first_reloc = NULL;
139
    return idx;
140
}
141

    
142
#include "tcg-target.c"
143

    
144
/* XXX: factorize */
145
static void pstrcpy(char *buf, int buf_size, const char *str)
146
{
147
    int c;
148
    char *q = buf;
149

    
150
    if (buf_size <= 0)
151
        return;
152

    
153
    for(;;) {
154
        c = *str++;
155
        if (c == 0 || q >= buf + buf_size - 1)
156
            break;
157
        *q++ = c;
158
    }
159
    *q = '\0';
160
}
161

    
162
#if TCG_TARGET_REG_BITS == 32
163
/* strcat and truncate. */
164
static char *pstrcat(char *buf, int buf_size, const char *s)
165
{
166
    int len;
167
    len = strlen(buf);
168
    if (len < buf_size)
169
        pstrcpy(buf + len, buf_size - len, s);
170
    return buf;
171
}
172
#endif
173

    
174
/* pool based memory allocation */
175
void *tcg_malloc_internal(TCGContext *s, int size)
176
{
177
    TCGPool *p;
178
    int pool_size;
179
    
180
    if (size > TCG_POOL_CHUNK_SIZE) {
181
        /* big malloc: insert a new pool (XXX: could optimize) */
182
        p = qemu_malloc(sizeof(TCGPool) + size);
183
        p->size = size;
184
        if (s->pool_current)
185
            s->pool_current->next = p;
186
        else
187
            s->pool_first = p;
188
        p->next = s->pool_current;
189
    } else {
190
        p = s->pool_current;
191
        if (!p) {
192
            p = s->pool_first;
193
            if (!p)
194
                goto new_pool;
195
        } else {
196
            if (!p->next) {
197
            new_pool:
198
                pool_size = TCG_POOL_CHUNK_SIZE;
199
                p = qemu_malloc(sizeof(TCGPool) + pool_size);
200
                p->size = pool_size;
201
                p->next = NULL;
202
                if (s->pool_current) 
203
                    s->pool_current->next = p;
204
                else
205
                    s->pool_first = p;
206
            } else {
207
                p = p->next;
208
            }
209
        }
210
    }
211
    s->pool_current = p;
212
    s->pool_cur = p->data + size;
213
    s->pool_end = p->data + p->size;
214
    return p->data;
215
}
216

    
217
void tcg_pool_reset(TCGContext *s)
218
{
219
    s->pool_cur = s->pool_end = NULL;
220
    s->pool_current = NULL;
221
}
222

    
223
/* free all the pool */
224
void tcg_pool_free(TCGContext *s)
225
{
226
    TCGPool *p, *p1;
227

    
228
    for(p = s->pool_first; p != NULL; p = p1) {
229
        p1 = p->next;
230
        qemu_free(p);
231
    }
232
    s->pool_first = NULL;
233
    s->pool_cur = s->pool_end = NULL;
234
}
235

    
236
void tcg_context_init(TCGContext *s)
237
{
238
    int op, total_args, n;
239
    TCGOpDef *def;
240
    TCGArgConstraint *args_ct;
241
    int *sorted_args;
242

    
243
    memset(s, 0, sizeof(*s));
244
    s->temps = s->static_temps;
245
    s->nb_globals = 0;
246
    
247
    /* Count total number of arguments and allocate the corresponding
248
       space */
249
    total_args = 0;
250
    for(op = 0; op < NB_OPS; op++) {
251
        def = &tcg_op_defs[op];
252
        n = def->nb_iargs + def->nb_oargs;
253
        total_args += n;
254
    }
255

    
256
    args_ct = qemu_malloc(sizeof(TCGArgConstraint) * total_args);
257
    sorted_args = qemu_malloc(sizeof(int) * total_args);
258

    
259
    for(op = 0; op < NB_OPS; op++) {
260
        def = &tcg_op_defs[op];
261
        def->args_ct = args_ct;
262
        def->sorted_args = sorted_args;
263
        n = def->nb_iargs + def->nb_oargs;
264
        sorted_args += n;
265
        args_ct += n;
266
    }
267
    
268
    tcg_target_init(s);
269
}
270

    
271
void tcg_set_frame(TCGContext *s, int reg,
272
                   tcg_target_long start, tcg_target_long size)
273
{
274
    s->frame_start = start;
275
    s->frame_end = start + size;
276
    s->frame_reg = reg;
277
}
278

    
279
void tcg_set_macro_func(TCGContext *s, TCGMacroFunc *func)
280
{
281
    s->macro_func = func;
282
}
283

    
284
void tcg_func_start(TCGContext *s)
285
{
286
    tcg_pool_reset(s);
287
    s->nb_temps = s->nb_globals;
288
    s->labels = tcg_malloc(sizeof(TCGLabel) * TCG_MAX_LABELS);
289
    s->nb_labels = 0;
290
    s->current_frame_offset = s->frame_start;
291

    
292
    gen_opc_ptr = gen_opc_buf;
293
    gen_opparam_ptr = gen_opparam_buf;
294
}
295

    
296
static inline void tcg_temp_alloc(TCGContext *s, int n)
297
{
298
    if (n > TCG_MAX_TEMPS)
299
        tcg_abort();
300
}
301

    
302
int tcg_global_reg_new(TCGType type, int reg, const char *name)
303
{
304
    TCGContext *s = &tcg_ctx;
305
    TCGTemp *ts;
306
    int idx;
307

    
308
#if TCG_TARGET_REG_BITS == 32
309
    if (type != TCG_TYPE_I32)
310
        tcg_abort();
311
#endif
312
    if (tcg_regset_test_reg(s->reserved_regs, reg))
313
        tcg_abort();
314
    idx = s->nb_globals;
315
    tcg_temp_alloc(s, s->nb_globals + 1);
316
    ts = &s->temps[s->nb_globals];
317
    ts->base_type = type;
318
    ts->type = type;
319
    ts->fixed_reg = 1;
320
    ts->reg = reg;
321
    ts->val_type = TEMP_VAL_REG;
322
    ts->name = name;
323
    s->nb_globals++;
324
    tcg_regset_set_reg(s->reserved_regs, reg);
325
    return idx;
326
}
327

    
328
int tcg_global_mem_new(TCGType type, int reg, tcg_target_long offset,
329
                       const char *name)
330
{
331
    TCGContext *s = &tcg_ctx;
332
    TCGTemp *ts;
333
    int idx;
334

    
335
    idx = s->nb_globals;
336
#if TCG_TARGET_REG_BITS == 32
337
    if (type == TCG_TYPE_I64) {
338
        char buf[64];
339
        tcg_temp_alloc(s, s->nb_globals + 1);
340
        ts = &s->temps[s->nb_globals];
341
        ts->base_type = type;
342
        ts->type = TCG_TYPE_I32;
343
        ts->fixed_reg = 0;
344
        ts->mem_allocated = 1;
345
        ts->mem_reg = reg;
346
#ifdef TCG_TARGET_WORDS_BIGENDIAN
347
        ts->mem_offset = offset + 4;
348
#else
349
        ts->mem_offset = offset;
350
#endif
351
        ts->val_type = TEMP_VAL_MEM;
352
        pstrcpy(buf, sizeof(buf), name);
353
        pstrcat(buf, sizeof(buf), "_0");
354
        ts->name = strdup(buf);
355
        ts++;
356

    
357
        ts->base_type = type;
358
        ts->type = TCG_TYPE_I32;
359
        ts->fixed_reg = 0;
360
        ts->mem_allocated = 1;
361
        ts->mem_reg = reg;
362
#ifdef TCG_TARGET_WORDS_BIGENDIAN
363
        ts->mem_offset = offset;
364
#else
365
        ts->mem_offset = offset + 4;
366
#endif
367
        ts->val_type = TEMP_VAL_MEM;
368
        pstrcpy(buf, sizeof(buf), name);
369
        pstrcat(buf, sizeof(buf), "_1");
370
        ts->name = strdup(buf);
371

    
372
        s->nb_globals += 2;
373
    } else
374
#endif
375
    {
376
        tcg_temp_alloc(s, s->nb_globals + 1);
377
        ts = &s->temps[s->nb_globals];
378
        ts->base_type = type;
379
        ts->type = type;
380
        ts->fixed_reg = 0;
381
        ts->mem_allocated = 1;
382
        ts->mem_reg = reg;
383
        ts->mem_offset = offset;
384
        ts->val_type = TEMP_VAL_MEM;
385
        ts->name = name;
386
        s->nb_globals++;
387
    }
388
    return idx;
389
}
390

    
391
int tcg_temp_new(TCGType type)
392
{
393
    TCGContext *s = &tcg_ctx;
394
    TCGTemp *ts;
395
    int idx;
396

    
397
    idx = s->nb_temps;
398
#if TCG_TARGET_REG_BITS == 32
399
    if (type == TCG_TYPE_I64) {
400
        tcg_temp_alloc(s, s->nb_temps + 1);
401
        ts = &s->temps[s->nb_temps];
402
        ts->base_type = type;
403
        ts->type = TCG_TYPE_I32;
404
        ts->val_type = TEMP_VAL_DEAD;
405
        ts->mem_allocated = 0;
406
        ts->name = NULL;
407
        ts++;
408
        ts->base_type = TCG_TYPE_I32;
409
        ts->type = TCG_TYPE_I32;
410
        ts->val_type = TEMP_VAL_DEAD;
411
        ts->mem_allocated = 0;
412
        ts->name = NULL;
413
        s->nb_temps += 2;
414
    } else
415
#endif
416
    {
417
        tcg_temp_alloc(s, s->nb_temps + 1);
418
        ts = &s->temps[s->nb_temps];
419
        ts->base_type = type;
420
        ts->type = type;
421
        ts->val_type = TEMP_VAL_DEAD;
422
        ts->mem_allocated = 0;
423
        ts->name = NULL;
424
        s->nb_temps++;
425
    }
426
    return idx;
427
}
428

    
429
int tcg_const_i32(int32_t val)
430
{
431
    TCGContext *s = &tcg_ctx;
432
    TCGTemp *ts;
433
    int idx;
434

    
435
    idx = s->nb_temps;
436
    tcg_temp_alloc(s, idx + 1);
437
    ts = &s->temps[idx];
438
    ts->base_type = ts->type = TCG_TYPE_I32;
439
    ts->val_type = TEMP_VAL_CONST;
440
    ts->name = NULL;
441
    ts->val = val;
442
    s->nb_temps++;
443
    return idx;
444
}
445

    
446
int tcg_const_i64(int64_t val)
447
{
448
    TCGContext *s = &tcg_ctx;
449
    TCGTemp *ts;
450
    int idx;
451

    
452
    idx = s->nb_temps;
453
#if TCG_TARGET_REG_BITS == 32
454
    tcg_temp_alloc(s, idx + 2);
455
    ts = &s->temps[idx];
456
    ts->base_type = TCG_TYPE_I64;
457
    ts->type = TCG_TYPE_I32;
458
    ts->val_type = TEMP_VAL_CONST;
459
    ts->name = NULL;
460
    ts->val = val;
461
    ts++;
462
    ts->base_type = TCG_TYPE_I32;
463
    ts->type = TCG_TYPE_I32;
464
    ts->val_type = TEMP_VAL_CONST;
465
    ts->name = NULL;
466
    ts->val = val >> 32;
467
    s->nb_temps += 2;
468
#else
469
    tcg_temp_alloc(s, idx + 1);
470
    ts = &s->temps[idx];
471
    ts->base_type = ts->type = TCG_TYPE_I64;
472
    ts->val_type = TEMP_VAL_CONST;
473
    ts->name = NULL;
474
    ts->val = val;
475
    s->nb_temps++;
476
#endif    
477
    return idx;
478
}
479

    
480
void tcg_register_helper(void *func, const char *name)
481
{
482
    TCGContext *s = &tcg_ctx;
483
    int n;
484
    if ((s->nb_helpers + 1) > s->allocated_helpers) {
485
        n = s->allocated_helpers;
486
        if (n == 0) {
487
            n = 4;
488
        } else {
489
            n *= 2;
490
        }
491
        s->helpers = realloc(s->helpers, n * sizeof(TCGHelperInfo));
492
        s->allocated_helpers = n;
493
    }
494
    s->helpers[s->nb_helpers].func = func;
495
    s->helpers[s->nb_helpers].name = name;
496
    s->nb_helpers++;
497
}
498

    
499
const char *tcg_helper_get_name(TCGContext *s, void *func)
500
{
501
    int i;
502

    
503
    for(i = 0; i < s->nb_helpers; i++) {
504
        if (s->helpers[i].func == func)
505
            return s->helpers[i].name;
506
    }
507
    return NULL;
508
}
509

    
510
static inline TCGType tcg_get_base_type(TCGContext *s, TCGArg arg)
511
{
512
    return s->temps[arg].base_type;
513
}
514

    
515
static void tcg_gen_call_internal(TCGContext *s, TCGArg func, 
516
                                  unsigned int flags,
517
                                  unsigned int nb_rets, const TCGArg *rets,
518
                                  unsigned int nb_params, const TCGArg *params)
519
{
520
    int i;
521
    *gen_opc_ptr++ = INDEX_op_call;
522
    *gen_opparam_ptr++ = (nb_rets << 16) | (nb_params + 1);
523
    for(i = 0; i < nb_rets; i++) {
524
        *gen_opparam_ptr++ = rets[i];
525
    }
526
    for(i = 0; i < nb_params; i++) {
527
        *gen_opparam_ptr++ = params[i];
528
    }
529
    *gen_opparam_ptr++ = func;
530

    
531
    *gen_opparam_ptr++ = flags;
532
    /* total parameters, needed to go backward in the instruction stream */
533
    *gen_opparam_ptr++ = 1 + nb_rets + nb_params + 3;
534
}
535

    
536

    
537
#if TCG_TARGET_REG_BITS < 64
538
/* Note: we convert the 64 bit args to 32 bit */
539
void tcg_gen_call(TCGContext *s, TCGArg func, unsigned int flags,
540
                  unsigned int nb_rets, const TCGArg *rets,
541
                  unsigned int nb_params, const TCGArg *args1)
542
{
543
    TCGArg ret, *args2, rets_2[2], arg;
544
    int j, i, call_type;
545

    
546
    if (nb_rets == 1) {
547
        ret = rets[0];
548
        if (tcg_get_base_type(s, ret) == TCG_TYPE_I64) {
549
            nb_rets = 2;
550
            rets_2[0] = ret;
551
            rets_2[1] = ret + 1;
552
            rets = rets_2;
553
        }
554
    }
555
    args2 = alloca((nb_params * 2) * sizeof(TCGArg));
556
    j = 0;
557
    call_type = (flags & TCG_CALL_TYPE_MASK);
558
    for(i = 0; i < nb_params; i++) {
559
        arg = args1[i];
560
        if (tcg_get_base_type(s, arg) == TCG_TYPE_I64) {
561
#ifdef TCG_TARGET_I386
562
            /* REGPARM case: if the third parameter is 64 bit, it is
563
               allocated on the stack */
564
            if (j == 2 && call_type == TCG_CALL_TYPE_REGPARM) {
565
                call_type = TCG_CALL_TYPE_REGPARM_2;
566
                flags = (flags & ~TCG_CALL_TYPE_MASK) | call_type;
567
            }
568
            args2[j++] = arg;
569
            args2[j++] = arg + 1;
570
#else
571
#ifdef TCG_TARGET_WORDS_BIGENDIAN
572
            args2[j++] = arg + 1;
573
            args2[j++] = arg;
574
#else
575
            args2[j++] = arg;
576
            args2[j++] = arg + 1;
577
#endif
578
#endif
579
        } else {
580
            args2[j++] = arg;
581
        }
582
    }
583
    tcg_gen_call_internal(s, func, flags, 
584
                          nb_rets, rets, j, args2);
585
}
586
#else
587
void tcg_gen_call(TCGContext *s, TCGArg func, unsigned int flags,
588
                  unsigned int nb_rets, const TCGArg *rets,
589
                  unsigned int nb_params, const TCGArg *args1)
590
{
591
    tcg_gen_call_internal(s, func, flags, 
592
                          nb_rets, rets, nb_params, args1);
593
}
594
#endif
595

    
596
void tcg_gen_shifti_i64(TCGArg ret, TCGArg arg1, 
597
                        int c, int right, int arith)
598
{
599
    if (c == 0)
600
        return;
601
    if (c >= 32) {
602
        c -= 32;
603
        if (right) {
604
            if (arith) {
605
                tcg_gen_sari_i32(ret, arg1 + 1, c);
606
                tcg_gen_sari_i32(ret + 1, arg1 + 1, 31);
607
            } else {
608
                tcg_gen_shri_i32(ret, arg1 + 1, c);
609
                tcg_gen_movi_i32(ret + 1, 0);
610
            }
611
        } else {
612
            tcg_gen_shli_i32(ret + 1, arg1, c);
613
            tcg_gen_movi_i32(ret, 0);
614
        }
615
    } else {
616
        int t0, t1;
617

    
618
        t0 = tcg_temp_new(TCG_TYPE_I32);
619
        t1 = tcg_temp_new(TCG_TYPE_I32);
620
        if (right) {
621
            tcg_gen_shli_i32(t0, arg1 + 1, 32 - c);
622
            if (arith)
623
                tcg_gen_sari_i32(t1, arg1 + 1, c);
624
            else 
625
                tcg_gen_shri_i32(t1, arg1 + 1, c);
626
            tcg_gen_shri_i32(ret, arg1, c); 
627
            tcg_gen_or_i32(ret, ret, t0);
628
            tcg_gen_mov_i32(ret + 1, t1);
629
        } else {
630
            tcg_gen_shri_i32(t0, arg1, 32 - c);
631
            /* Note: ret can be the same as arg1, so we use t1 */
632
            tcg_gen_shli_i32(t1, arg1, c); 
633
            tcg_gen_shli_i32(ret + 1, arg1 + 1, c);
634
            tcg_gen_or_i32(ret + 1, ret + 1, t0);
635
            tcg_gen_mov_i32(ret, t1);
636
        }
637
    }
638
}
639

    
640
void tcg_reg_alloc_start(TCGContext *s)
641
{
642
    int i;
643
    TCGTemp *ts;
644
    for(i = 0; i < s->nb_globals; i++) {
645
        ts = &s->temps[i];
646
        if (ts->fixed_reg) {
647
            ts->val_type = TEMP_VAL_REG;
648
        } else {
649
            ts->val_type = TEMP_VAL_MEM;
650
        }
651
    }
652
    for(i = 0; i < TCG_TARGET_NB_REGS; i++) {
653
        s->reg_to_temp[i] = -1;
654
    }
655
}
656

    
657
char *tcg_get_arg_str(TCGContext *s, char *buf, int buf_size, TCGArg arg)
658
{
659
    TCGTemp *ts;
660
    if (arg < s->nb_globals) {
661
        pstrcpy(buf, buf_size, s->temps[arg].name);
662
    } else {
663
        ts = &s->temps[arg];
664
        if (ts->val_type == TEMP_VAL_CONST) {
665
            snprintf(buf, buf_size, "$0x%" TCG_PRIlx , ts->val);
666
        } else {
667
            snprintf(buf, buf_size, "tmp%d", (int)arg - s->nb_globals);
668
        }
669
    }
670
    return buf;
671
}
672

    
673
void tcg_dump_ops(TCGContext *s, FILE *outfile)
674
{
675
    const uint16_t *opc_ptr;
676
    const TCGArg *args;
677
    TCGArg arg;
678
    int c, i, k, nb_oargs, nb_iargs, nb_cargs;
679
    const TCGOpDef *def;
680
    char buf[128];
681

    
682
    opc_ptr = gen_opc_buf;
683
    args = gen_opparam_buf;
684
    while (opc_ptr < gen_opc_ptr) {
685
        c = *opc_ptr++;
686
        def = &tcg_op_defs[c];
687
        fprintf(outfile, " %s ", def->name);
688
        if (c == INDEX_op_call) {
689
            TCGArg arg;
690
            /* variable number of arguments */
691
            arg = *args++;
692
            nb_oargs = arg >> 16;
693
            nb_iargs = arg & 0xffff;
694
            nb_cargs = def->nb_cargs;
695
        } else if (c == INDEX_op_nopn) {
696
            /* variable number of arguments */
697
            nb_cargs = *args;
698
            nb_oargs = 0;
699
            nb_iargs = 0;
700
        } else {
701
            nb_oargs = def->nb_oargs;
702
            nb_iargs = def->nb_iargs;
703
            nb_cargs = def->nb_cargs;
704
        }
705

    
706
        k = 0;
707
        for(i = 0; i < nb_oargs; i++) {
708
            if (k != 0)
709
                fprintf(outfile, ",");
710
            fprintf(outfile, "%s", tcg_get_arg_str(s, buf, sizeof(buf), args[k++]));
711
        }
712
        for(i = 0; i < nb_iargs; i++) {
713
            if (k != 0)
714
                fprintf(outfile, ",");
715
            /* XXX: dump helper name for call */
716
            fprintf(outfile, "%s", tcg_get_arg_str(s, buf, sizeof(buf), args[k++]));
717
        }
718
        for(i = 0; i < nb_cargs; i++) {
719
            if (k != 0)
720
                fprintf(outfile, ",");
721
            arg = args[k++];
722
            fprintf(outfile, "$0x%" TCG_PRIlx, arg);
723
        }
724
        fprintf(outfile, "\n");
725
        args += nb_iargs + nb_oargs + nb_cargs;
726
    }
727
}
728

    
729
/* we give more priority to constraints with less registers */
730
static int get_constraint_priority(const TCGOpDef *def, int k)
731
{
732
    const TCGArgConstraint *arg_ct;
733

    
734
    int i, n;
735
    arg_ct = &def->args_ct[k];
736
    if (arg_ct->ct & TCG_CT_ALIAS) {
737
        /* an alias is equivalent to a single register */
738
        n = 1;
739
    } else {
740
        if (!(arg_ct->ct & TCG_CT_REG))
741
            return 0;
742
        n = 0;
743
        for(i = 0; i < TCG_TARGET_NB_REGS; i++) {
744
            if (tcg_regset_test_reg(arg_ct->u.regs, i))
745
                n++;
746
        }
747
    }
748
    return TCG_TARGET_NB_REGS - n + 1;
749
}
750

    
751
/* sort from highest priority to lowest */
752
static void sort_constraints(TCGOpDef *def, int start, int n)
753
{
754
    int i, j, p1, p2, tmp;
755

    
756
    for(i = 0; i < n; i++)
757
        def->sorted_args[start + i] = start + i;
758
    if (n <= 1)
759
        return;
760
    for(i = 0; i < n - 1; i++) {
761
        for(j = i + 1; j < n; j++) {
762
            p1 = get_constraint_priority(def, def->sorted_args[start + i]);
763
            p2 = get_constraint_priority(def, def->sorted_args[start + j]);
764
            if (p1 < p2) {
765
                tmp = def->sorted_args[start + i];
766
                def->sorted_args[start + i] = def->sorted_args[start + j];
767
                def->sorted_args[start + j] = tmp;
768
            }
769
        }
770
    }
771
}
772

    
773
void tcg_add_target_add_op_defs(const TCGTargetOpDef *tdefs)
774
{
775
    int op;
776
    TCGOpDef *def;
777
    const char *ct_str;
778
    int i, nb_args;
779

    
780
    for(;;) {
781
        if (tdefs->op < 0)
782
            break;
783
        op = tdefs->op;
784
        assert(op >= 0 && op < NB_OPS);
785
        def = &tcg_op_defs[op];
786
        nb_args = def->nb_iargs + def->nb_oargs;
787
        for(i = 0; i < nb_args; i++) {
788
            ct_str = tdefs->args_ct_str[i];
789
            tcg_regset_clear(def->args_ct[i].u.regs);
790
            def->args_ct[i].ct = 0;
791
            if (ct_str[0] >= '0' && ct_str[0] <= '9') {
792
                int oarg;
793
                oarg = ct_str[0] - '0';
794
                assert(oarg < def->nb_oargs);
795
                assert(def->args_ct[oarg].ct & TCG_CT_REG);
796
                /* TCG_CT_ALIAS is for the output arguments. The input
797
                   argument is tagged with TCG_CT_IALIAS for
798
                   informative purposes. */
799
                def->args_ct[i] = def->args_ct[oarg];
800
                def->args_ct[oarg].ct = i | TCG_CT_ALIAS;
801
                def->args_ct[i].ct |= TCG_CT_IALIAS;
802
            } else {
803
                for(;;) {
804
                    if (*ct_str == '\0')
805
                        break;
806
                    switch(*ct_str) {
807
                    case 'i':
808
                        def->args_ct[i].ct |= TCG_CT_CONST;
809
                        ct_str++;
810
                        break;
811
                    default:
812
                        if (target_parse_constraint(&def->args_ct[i], &ct_str) < 0) {
813
                            fprintf(stderr, "Invalid constraint '%s' for arg %d of operation '%s'\n",
814
                                    ct_str, i, def->name);
815
                            exit(1);
816
                        }
817
                    }
818
                }
819
            }
820
        }
821

    
822
        /* sort the constraints (XXX: this is just an heuristic) */
823
        sort_constraints(def, 0, def->nb_oargs);
824
        sort_constraints(def, def->nb_oargs, def->nb_iargs);
825

    
826
#if 0
827
        {
828
            int i;
829

830
            printf("%s: sorted=", def->name);
831
            for(i = 0; i < def->nb_oargs + def->nb_iargs; i++)
832
                printf(" %d", def->sorted_args[i]);
833
            printf("\n");
834
        }
835
#endif
836
        tdefs++;
837
    }
838

    
839
}
840

    
841
#ifdef USE_LIVENESS_ANALYSIS
842

    
843
/* set a nop for an operation using 'nb_args' */
844
static inline void tcg_set_nop(TCGContext *s, uint16_t *opc_ptr, 
845
                               TCGArg *args, int nb_args)
846
{
847
    if (nb_args == 0) {
848
        *opc_ptr = INDEX_op_nop;
849
    } else {
850
        *opc_ptr = INDEX_op_nopn;
851
        args[0] = nb_args;
852
        args[nb_args - 1] = nb_args;
853
    }
854
}
855

    
856
/* liveness analysis: end of basic block: globals are live, temps are dead */
857
static inline void tcg_la_bb_end(TCGContext *s, uint8_t *dead_temps)
858
{
859
    memset(dead_temps, 0, s->nb_globals);
860
    memset(dead_temps + s->nb_globals, 1, s->nb_temps - s->nb_globals);
861
}
862

    
863
/* Liveness analysis : update the opc_dead_iargs array to tell if a
864
   given input arguments is dead. Instructions updating dead
865
   temporaries are removed. */
866
void tcg_liveness_analysis(TCGContext *s)
867
{
868
    int i, op_index, op, nb_args, nb_iargs, nb_oargs, arg, nb_ops;
869
    TCGArg *args;
870
    const TCGOpDef *def;
871
    uint8_t *dead_temps;
872
    unsigned int dead_iargs;
873
    
874
    gen_opc_ptr++; /* skip end */
875

    
876
    nb_ops = gen_opc_ptr - gen_opc_buf;
877

    
878
    /* XXX: make it really dynamic */
879
    s->op_dead_iargs = tcg_malloc(OPC_BUF_SIZE * sizeof(uint16_t));
880
    
881
    dead_temps = tcg_malloc(s->nb_temps);
882
    memset(dead_temps, 1, s->nb_temps);
883

    
884
    args = gen_opparam_ptr;
885
    op_index = nb_ops - 1;
886
    while (op_index >= 0) {
887
        op = gen_opc_buf[op_index];
888
        def = &tcg_op_defs[op];
889
        switch(op) {
890
        case INDEX_op_call:
891
            nb_args = args[-1];
892
            args -= nb_args;
893
            nb_iargs = args[0] & 0xffff;
894
            nb_oargs = args[0] >> 16;
895
            args++;
896

    
897
            /* output args are dead */
898
            for(i = 0; i < nb_oargs; i++) {
899
                arg = args[i];
900
                dead_temps[arg] = 1;
901
            }
902
            
903
            /* globals are live (they may be used by the call) */
904
            memset(dead_temps, 0, s->nb_globals);
905

    
906
            /* input args are live */
907
            dead_iargs = 0;
908
            for(i = 0; i < nb_iargs; i++) {
909
                arg = args[i + nb_oargs];
910
                if (dead_temps[arg]) {
911
                    dead_iargs |= (1 << i);
912
                }
913
                dead_temps[arg] = 0;
914
            }
915
            s->op_dead_iargs[op_index] = dead_iargs;
916
            args--;
917
            break;
918
        case INDEX_op_set_label:
919
            args--;
920
            /* mark end of basic block */
921
            tcg_la_bb_end(s, dead_temps);
922
            break;
923
        case INDEX_op_nopn:
924
            nb_args = args[-1];
925
            args -= nb_args;
926
            break;
927
        case INDEX_op_macro_2:
928
            {
929
                int dead_args[2], macro_id;
930
                int saved_op_index, saved_arg_index;
931
                int macro_op_index, macro_arg_index;
932
                int macro_end_op_index, macro_end_arg_index;
933
                int last_nb_temps;
934
                
935
                nb_args = 3;
936
                args -= nb_args;
937
                dead_args[0] = dead_temps[args[0]];
938
                dead_args[1] = dead_temps[args[1]];
939
                macro_id = args[2];
940

    
941
                /* call the macro function which generate code
942
                   depending on the live outputs */
943
                saved_op_index = op_index;
944
                saved_arg_index = args - gen_opparam_buf;
945

    
946
                /* add a macro start instruction */
947
                *gen_opc_ptr++ = INDEX_op_macro_start;
948
                *gen_opparam_ptr++ = saved_op_index;
949
                *gen_opparam_ptr++ = saved_arg_index;
950

    
951
                macro_op_index = gen_opc_ptr - gen_opc_buf;
952
                macro_arg_index = gen_opparam_ptr -  gen_opparam_buf;
953

    
954
                last_nb_temps = s->nb_temps;
955

    
956
                s->macro_func(s, macro_id, dead_args);
957

    
958
                /* realloc temp info (XXX: make it faster) */
959
                if (s->nb_temps > last_nb_temps) {
960
                    uint8_t *new_dead_temps;
961

    
962
                    new_dead_temps = tcg_malloc(s->nb_temps);
963
                    memcpy(new_dead_temps, dead_temps, last_nb_temps);
964
                    memset(new_dead_temps + last_nb_temps, 1, 
965
                           s->nb_temps - last_nb_temps);
966
                    dead_temps = new_dead_temps;
967
                }
968

    
969
                macro_end_op_index = gen_opc_ptr - gen_opc_buf;
970
                macro_end_arg_index = gen_opparam_ptr - gen_opparam_buf;
971

    
972
                /* end of macro: add a goto to the next instruction */
973
                *gen_opc_ptr++ = INDEX_op_macro_end;
974
                *gen_opparam_ptr++ = op_index + 1;
975
                *gen_opparam_ptr++ = saved_arg_index + nb_args;
976

    
977
                /* modify the macro operation to be a macro_goto */
978
                gen_opc_buf[op_index] = INDEX_op_macro_goto;
979
                args[0] = macro_op_index;
980
                args[1] = macro_arg_index;
981
                args[2] = 0; /* dummy third arg to match the 
982
                                macro parameters */
983

    
984
                /* set the next instruction to the end of the macro */
985
                op_index = macro_end_op_index;
986
                args = macro_end_arg_index + gen_opparam_buf;
987
            }
988
            break;
989
        case INDEX_op_macro_start:
990
            args -= 2;
991
            op_index = args[0];
992
            args = gen_opparam_buf + args[1];
993
            break;
994
        case INDEX_op_macro_goto:
995
        case INDEX_op_macro_end:
996
            tcg_abort(); /* should never happen in liveness analysis */
997
        case INDEX_op_end:
998
            break;
999
            /* XXX: optimize by hardcoding common cases (e.g. triadic ops) */
1000
        default:
1001
            if (op > INDEX_op_end) {
1002
                args -= def->nb_args;
1003
                nb_iargs = def->nb_iargs;
1004
                nb_oargs = def->nb_oargs;
1005

    
1006
                /* Test if the operation can be removed because all
1007
                   its outputs are dead. We may add a flag to
1008
                   explicitely tell if the op has side
1009
                   effects. Currently we assume that if nb_oargs == 0
1010
                   or OPF_BB_END is set, the operation has side
1011
                   effects and cannot be removed */
1012
                if (nb_oargs != 0 && !(def->flags & TCG_OPF_BB_END)) {
1013
                    for(i = 0; i < nb_oargs; i++) {
1014
                        arg = args[i];
1015
                        if (!dead_temps[arg])
1016
                            goto do_not_remove;
1017
                    }
1018
                    tcg_set_nop(s, gen_opc_buf + op_index, args, def->nb_args);
1019
#ifdef CONFIG_PROFILER
1020
                    {
1021
                        extern int64_t dyngen_tcg_del_op_count;
1022
                        dyngen_tcg_del_op_count++;
1023
                    }
1024
#endif
1025
                } else {
1026
                do_not_remove:
1027

    
1028
                    /* output args are dead */
1029
                    for(i = 0; i < nb_oargs; i++) {
1030
                        arg = args[i];
1031
                        dead_temps[arg] = 1;
1032
                    }
1033
                    
1034
                    /* if end of basic block, update */
1035
                    if (def->flags & TCG_OPF_BB_END) {
1036
                        tcg_la_bb_end(s, dead_temps);
1037
                    }
1038
                    
1039
                    /* input args are live */
1040
                    dead_iargs = 0;
1041
                    for(i = 0; i < nb_iargs; i++) {
1042
                        arg = args[i + nb_oargs];
1043
                        if (dead_temps[arg]) {
1044
                            dead_iargs |= (1 << i);
1045
                        }
1046
                        dead_temps[arg] = 0;
1047
                    }
1048
                    s->op_dead_iargs[op_index] = dead_iargs;
1049
                }
1050
            } else {
1051
                /* legacy dyngen operations */
1052
                args -= def->nb_args;
1053
                /* mark end of basic block */
1054
                tcg_la_bb_end(s, dead_temps);
1055
            }
1056
            break;
1057
        }
1058
        op_index--;
1059
    }
1060

    
1061
    if (args != gen_opparam_buf)
1062
        tcg_abort();
1063
}
1064
#else
1065
/* dummy liveness analysis */
1066
void tcg_liveness_analysis(TCGContext *s)
1067
{
1068
    int nb_ops;
1069
    nb_ops = gen_opc_ptr - gen_opc_buf;
1070

    
1071
    s->op_dead_iargs = tcg_malloc(nb_ops * sizeof(uint16_t));
1072
    memset(s->op_dead_iargs, 0, nb_ops * sizeof(uint16_t));
1073
}
1074
#endif
1075

    
1076
#ifndef NDEBUG
1077
static void dump_regs(TCGContext *s)
1078
{
1079
    TCGTemp *ts;
1080
    int i;
1081
    char buf[64];
1082

    
1083
    for(i = 0; i < s->nb_temps; i++) {
1084
        ts = &s->temps[i];
1085
        printf("  %10s: ", tcg_get_arg_str(s, buf, sizeof(buf), i));
1086
        switch(ts->val_type) {
1087
        case TEMP_VAL_REG:
1088
            printf("%s", tcg_target_reg_names[ts->reg]);
1089
            break;
1090
        case TEMP_VAL_MEM:
1091
            printf("%d(%s)", (int)ts->mem_offset, tcg_target_reg_names[ts->mem_reg]);
1092
            break;
1093
        case TEMP_VAL_CONST:
1094
            printf("$0x%" TCG_PRIlx, ts->val);
1095
            break;
1096
        case TEMP_VAL_DEAD:
1097
            printf("D");
1098
            break;
1099
        default:
1100
            printf("???");
1101
            break;
1102
        }
1103
        printf("\n");
1104
    }
1105

    
1106
    for(i = 0; i < TCG_TARGET_NB_REGS; i++) {
1107
        if (s->reg_to_temp[i] >= 0) {
1108
            printf("%s: %s\n", 
1109
                   tcg_target_reg_names[i], 
1110
                   tcg_get_arg_str(s, buf, sizeof(buf), s->reg_to_temp[i]));
1111
        }
1112
    }
1113
}
1114

    
1115
static void check_regs(TCGContext *s)
1116
{
1117
    int reg, k;
1118
    TCGTemp *ts;
1119
    char buf[64];
1120

    
1121
    for(reg = 0; reg < TCG_TARGET_NB_REGS; reg++) {
1122
        k = s->reg_to_temp[reg];
1123
        if (k >= 0) {
1124
            ts = &s->temps[k];
1125
            if (ts->val_type != TEMP_VAL_REG ||
1126
                ts->reg != reg) {
1127
                printf("Inconsistency for register %s:\n", 
1128
                       tcg_target_reg_names[reg]);
1129
                printf("reg state:\n");
1130
                dump_regs(s);
1131
                tcg_abort();
1132
            }
1133
        }
1134
    }
1135
    for(k = 0; k < s->nb_temps; k++) {
1136
        ts = &s->temps[k];
1137
        if (ts->val_type == TEMP_VAL_REG &&
1138
            !ts->fixed_reg &&
1139
            s->reg_to_temp[ts->reg] != k) {
1140
                printf("Inconsistency for temp %s:\n", 
1141
                       tcg_get_arg_str(s, buf, sizeof(buf), k));
1142
                printf("reg state:\n");
1143
                dump_regs(s);
1144
                tcg_abort();
1145
        }
1146
    }
1147
}
1148
#endif
1149

    
1150
static void temp_allocate_frame(TCGContext *s, int temp)
1151
{
1152
    TCGTemp *ts;
1153
    ts = &s->temps[temp];
1154
    s->current_frame_offset = (s->current_frame_offset + sizeof(tcg_target_long) - 1) & ~(sizeof(tcg_target_long) - 1);
1155
    if (s->current_frame_offset + sizeof(tcg_target_long) > s->frame_end)
1156
        abort();
1157
    ts->mem_offset = s->current_frame_offset;
1158
    ts->mem_reg = s->frame_reg;
1159
    ts->mem_allocated = 1;
1160
    s->current_frame_offset += sizeof(tcg_target_long);
1161
}
1162

    
1163
/* free register 'reg' by spilling the corresponding temporary if necessary */
1164
static void tcg_reg_free(TCGContext *s, int reg)
1165
{
1166
    TCGTemp *ts;
1167
    int temp;
1168

    
1169
    temp = s->reg_to_temp[reg];
1170
    if (temp != -1) {
1171
        ts = &s->temps[temp];
1172
        assert(ts->val_type == TEMP_VAL_REG);
1173
        if (!ts->mem_coherent) {
1174
            if (!ts->mem_allocated) 
1175
                temp_allocate_frame(s, temp);
1176
            tcg_out_st(s, reg, ts->mem_reg, ts->mem_offset);
1177
        }
1178
        ts->val_type = TEMP_VAL_MEM;
1179
        s->reg_to_temp[reg] = -1;
1180
    }
1181
}
1182

    
1183
/* Allocate a register belonging to reg1 & ~reg2 */
1184
static int tcg_reg_alloc(TCGContext *s, TCGRegSet reg1, TCGRegSet reg2)
1185
{
1186
    int i, reg;
1187
    TCGRegSet reg_ct;
1188

    
1189
    tcg_regset_andnot(reg_ct, reg1, reg2);
1190

    
1191
    /* first try free registers */
1192
    for(i = 0; i < TCG_TARGET_NB_REGS; i++) {
1193
        reg = tcg_target_reg_alloc_order[i];
1194
        if (tcg_regset_test_reg(reg_ct, reg) && s->reg_to_temp[reg] == -1)
1195
            return reg;
1196
    }
1197

    
1198
    /* XXX: do better spill choice */
1199
    for(i = 0; i < TCG_TARGET_NB_REGS; i++) {
1200
        reg = tcg_target_reg_alloc_order[i];
1201
        if (tcg_regset_test_reg(reg_ct, reg)) {
1202
            tcg_reg_free(s, reg);
1203
            return reg;
1204
        }
1205
    }
1206

    
1207
    tcg_abort();
1208
}
1209

    
1210
/* at the end of a basic block, we assume all temporaries are dead and
1211
   all globals are stored at their canonical location */
1212
/* XXX: optimize by handling constants in another array ? */
1213
void tcg_reg_alloc_bb_end(TCGContext *s)
1214
{
1215
    TCGTemp *ts;
1216
    int i;
1217

    
1218
    for(i = 0; i < s->nb_globals; i++) {
1219
        ts = &s->temps[i];
1220
        if (!ts->fixed_reg) {
1221
            if (ts->val_type == TEMP_VAL_REG) {
1222
                tcg_reg_free(s, ts->reg);
1223
            }
1224
        }
1225
    }
1226

    
1227
    for(i = s->nb_globals; i < s->nb_temps; i++) {
1228
        ts = &s->temps[i];
1229
        if (ts->val_type != TEMP_VAL_CONST) {
1230
            if (ts->val_type == TEMP_VAL_REG) {
1231
                s->reg_to_temp[ts->reg] = -1;
1232
            }
1233
            ts->val_type = TEMP_VAL_DEAD;
1234
        }
1235
    }
1236
}
1237

    
1238
#define IS_DEAD_IARG(n) ((dead_iargs >> (n)) & 1)
1239

    
1240
static void tcg_reg_alloc_mov(TCGContext *s, const TCGOpDef *def,
1241
                              const TCGArg *args,
1242
                              unsigned int dead_iargs)
1243
{
1244
    TCGTemp *ts, *ots;
1245
    int reg;
1246
    const TCGArgConstraint *arg_ct;
1247

    
1248
    ots = &s->temps[args[0]];
1249
    ts = &s->temps[args[1]];
1250
    arg_ct = &def->args_ct[0];
1251

    
1252
    if (ts->val_type == TEMP_VAL_REG) {
1253
        if (IS_DEAD_IARG(0) && !ts->fixed_reg && !ots->fixed_reg) {
1254
            /* the mov can be suppressed */
1255
            if (ots->val_type == TEMP_VAL_REG)
1256
                s->reg_to_temp[ots->reg] = -1;
1257
            reg = ts->reg;
1258
            s->reg_to_temp[reg] = -1;
1259
            ts->val_type = TEMP_VAL_DEAD;
1260
        } else {
1261
            if (ots->val_type == TEMP_VAL_REG) {
1262
                reg = ots->reg;
1263
            } else {
1264
                reg = tcg_reg_alloc(s, arg_ct->u.regs, s->reserved_regs);
1265
            }
1266
            if (ts->reg != reg) {
1267
                tcg_out_mov(s, reg, ts->reg);
1268
            }
1269
        }
1270
    } else if (ts->val_type == TEMP_VAL_MEM) {
1271
        if (ots->val_type == TEMP_VAL_REG) {
1272
            reg = ots->reg;
1273
        } else {
1274
            reg = tcg_reg_alloc(s, arg_ct->u.regs, s->reserved_regs);
1275
        }
1276
        tcg_out_ld(s, reg, ts->mem_reg, ts->mem_offset);
1277
    } else if (ts->val_type == TEMP_VAL_CONST) {
1278
        if (ots->val_type == TEMP_VAL_REG) {
1279
            reg = ots->reg;
1280
        } else {
1281
            reg = tcg_reg_alloc(s, arg_ct->u.regs, s->reserved_regs);
1282
        }
1283
        tcg_out_movi(s, ots->type, reg, ts->val);
1284
    } else {
1285
        tcg_abort();
1286
    }
1287
    s->reg_to_temp[reg] = args[0];
1288
    ots->reg = reg;
1289
    ots->val_type = TEMP_VAL_REG;
1290
    ots->mem_coherent = 0;
1291
}
1292

    
1293
static void tcg_reg_alloc_op(TCGContext *s, 
1294
                             const TCGOpDef *def, int opc,
1295
                             const TCGArg *args,
1296
                             unsigned int dead_iargs)
1297
{
1298
    TCGRegSet allocated_regs;
1299
    int i, k, nb_iargs, nb_oargs, reg;
1300
    TCGArg arg;
1301
    const TCGArgConstraint *arg_ct;
1302
    TCGTemp *ts;
1303
    TCGArg new_args[TCG_MAX_OP_ARGS];
1304
    int const_args[TCG_MAX_OP_ARGS];
1305

    
1306
    nb_oargs = def->nb_oargs;
1307
    nb_iargs = def->nb_iargs;
1308

    
1309
    /* copy constants */
1310
    memcpy(new_args + nb_oargs + nb_iargs, 
1311
           args + nb_oargs + nb_iargs, 
1312
           sizeof(TCGArg) * def->nb_cargs);
1313

    
1314
    /* satisfy input constraints */ 
1315
    tcg_regset_set(allocated_regs, s->reserved_regs);
1316
    for(k = 0; k < nb_iargs; k++) {
1317
        i = def->sorted_args[nb_oargs + k];
1318
        arg = args[i];
1319
        arg_ct = &def->args_ct[i];
1320
        ts = &s->temps[arg];
1321
        if (ts->val_type == TEMP_VAL_MEM) {
1322
            reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs);
1323
            tcg_out_ld(s, reg, ts->mem_reg, ts->mem_offset);
1324
            ts->val_type = TEMP_VAL_REG;
1325
            ts->reg = reg;
1326
            ts->mem_coherent = 1;
1327
            s->reg_to_temp[reg] = arg;
1328
        } else if (ts->val_type == TEMP_VAL_CONST) {
1329
            if (tcg_target_const_match(ts->val, arg_ct)) {
1330
                /* constant is OK for instruction */
1331
                const_args[i] = 1;
1332
                new_args[i] = ts->val;
1333
                goto iarg_end;
1334
            } else {
1335
                /* need to move to a register*/
1336
                reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs);
1337
                tcg_out_movi(s, ts->type, reg, ts->val);
1338
                goto iarg_end1;
1339
            }
1340
        }
1341
        assert(ts->val_type == TEMP_VAL_REG);
1342
        if ((arg_ct->ct & TCG_CT_IALIAS) &&
1343
            !IS_DEAD_IARG(i - nb_oargs)) {
1344
            /* if the input is aliased to an output and if it is
1345
               not dead after the instruction, we must allocate
1346
               a new register and move it */
1347
            goto allocate_in_reg;
1348
        }
1349
        reg = ts->reg;
1350
        if (tcg_regset_test_reg(arg_ct->u.regs, reg)) {
1351
            /* nothing to do : the constraint is satisfied */
1352
        } else {
1353
        allocate_in_reg:
1354
            /* allocate a new register matching the constraint 
1355
               and move the temporary register into it */
1356
            reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs);
1357
            tcg_out_mov(s, reg, ts->reg);
1358
        }
1359
    iarg_end1:
1360
        new_args[i] = reg;
1361
        const_args[i] = 0;
1362
        tcg_regset_set_reg(allocated_regs, reg);
1363
    iarg_end: ;
1364
    }
1365
    
1366
    /* mark dead temporaries and free the associated registers */
1367
    for(i = 0; i < nb_iargs; i++) {
1368
        arg = args[nb_oargs + i];
1369
        if (IS_DEAD_IARG(i)) {
1370
            ts = &s->temps[arg];
1371
            if (ts->val_type != TEMP_VAL_CONST && !ts->fixed_reg) {
1372
                if (ts->val_type == TEMP_VAL_REG)
1373
                    s->reg_to_temp[ts->reg] = -1;
1374
                ts->val_type = TEMP_VAL_DEAD;
1375
            }
1376
        }
1377
    }
1378

    
1379
    /* XXX: permit generic clobber register list ? */ 
1380
    if (def->flags & TCG_OPF_CALL_CLOBBER) {
1381
        for(reg = 0; reg < TCG_TARGET_NB_REGS; reg++) {
1382
            if (tcg_regset_test_reg(tcg_target_call_clobber_regs, reg)) {
1383
                tcg_reg_free(s, reg);
1384
            }
1385
        }
1386
    }
1387

    
1388
    /* satisfy the output constraints */
1389
    tcg_regset_set(allocated_regs, s->reserved_regs);
1390
    for(k = 0; k < nb_oargs; k++) {
1391
        i = def->sorted_args[k];
1392
        arg = args[i];
1393
        arg_ct = &def->args_ct[i];
1394
        ts = &s->temps[arg];
1395
        if (arg_ct->ct & TCG_CT_ALIAS) {
1396
            reg = new_args[arg_ct->ct & ~TCG_CT_ALIAS];
1397
        } else {
1398
            /* if fixed register, we try to use it */
1399
            reg = ts->reg;
1400
            if (ts->fixed_reg &&
1401
                tcg_regset_test_reg(arg_ct->u.regs, reg)) {
1402
                goto oarg_end;
1403
            }
1404
            reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs);
1405
        }
1406
        tcg_regset_set_reg(allocated_regs, reg);
1407
        /* if a fixed register is used, then a move will be done afterwards */
1408
        if (!ts->fixed_reg) {
1409
            if (ts->val_type == TEMP_VAL_REG)
1410
                s->reg_to_temp[ts->reg] = -1;
1411
            ts->val_type = TEMP_VAL_REG;
1412
            ts->reg = reg;
1413
            /* temp value is modified, so the value kept in memory is
1414
               potentially not the same */
1415
            ts->mem_coherent = 0; 
1416
            s->reg_to_temp[reg] = arg;
1417
        }
1418
    oarg_end:
1419
        new_args[i] = reg;
1420
    }
1421

    
1422
    if (def->flags & TCG_OPF_BB_END)
1423
        tcg_reg_alloc_bb_end(s);
1424

    
1425
    /* emit instruction */
1426
    tcg_out_op(s, opc, new_args, const_args);
1427
    
1428
    /* move the outputs in the correct register if needed */
1429
    for(i = 0; i < nb_oargs; i++) {
1430
        ts = &s->temps[args[i]];
1431
        reg = new_args[i];
1432
        if (ts->fixed_reg && ts->reg != reg) {
1433
            tcg_out_mov(s, ts->reg, reg);
1434
        }
1435
    }
1436
}
1437

    
1438
static int tcg_reg_alloc_call(TCGContext *s, const TCGOpDef *def,
1439
                              int opc, const TCGArg *args,
1440
                              unsigned int dead_iargs)
1441
{
1442
    int nb_iargs, nb_oargs, flags, nb_regs, i, reg, nb_params;
1443
    TCGArg arg, func_arg;
1444
    TCGTemp *ts;
1445
    tcg_target_long stack_offset, call_stack_size;
1446
    int const_func_arg;
1447
    TCGRegSet allocated_regs;
1448
    const TCGArgConstraint *arg_ct;
1449

    
1450
    arg = *args++;
1451

    
1452
    nb_oargs = arg >> 16;
1453
    nb_iargs = arg & 0xffff;
1454
    nb_params = nb_iargs - 1;
1455

    
1456
    flags = args[nb_oargs + nb_iargs];
1457

    
1458
    nb_regs = tcg_target_get_call_iarg_regs_count(flags);
1459
    if (nb_regs > nb_params)
1460
        nb_regs = nb_params;
1461

    
1462
    /* assign stack slots first */
1463
    /* XXX: preallocate call stack */
1464
    call_stack_size = (nb_params - nb_regs) * sizeof(tcg_target_long);
1465
    call_stack_size = (call_stack_size + TCG_TARGET_STACK_ALIGN - 1) & 
1466
        ~(TCG_TARGET_STACK_ALIGN - 1);
1467
    tcg_out_addi(s, TCG_REG_CALL_STACK, -call_stack_size);
1468

    
1469
    stack_offset = 0;
1470
    for(i = nb_regs; i < nb_params; i++) {
1471
        arg = args[nb_oargs + i];
1472
        ts = &s->temps[arg];
1473
        if (ts->val_type == TEMP_VAL_REG) {
1474
            tcg_out_st(s, ts->reg, TCG_REG_CALL_STACK, stack_offset);
1475
        } else if (ts->val_type == TEMP_VAL_MEM) {
1476
            reg = tcg_reg_alloc(s, tcg_target_available_regs[ts->type], 
1477
                                s->reserved_regs);
1478
            /* XXX: not correct if reading values from the stack */
1479
            tcg_out_ld(s, reg, ts->mem_reg, ts->mem_offset);
1480
            tcg_out_st(s, reg, TCG_REG_CALL_STACK, stack_offset);
1481
        } else if (ts->val_type == TEMP_VAL_CONST) {
1482
            reg = tcg_reg_alloc(s, tcg_target_available_regs[ts->type], 
1483
                                s->reserved_regs);
1484
            /* XXX: sign extend may be needed on some targets */
1485
            tcg_out_movi(s, ts->type, reg, ts->val);
1486
            tcg_out_st(s, reg, TCG_REG_CALL_STACK, stack_offset);
1487
        } else {
1488
            tcg_abort();
1489
        }
1490
        stack_offset += sizeof(tcg_target_long);
1491
    }
1492
    
1493
    /* assign input registers */
1494
    tcg_regset_set(allocated_regs, s->reserved_regs);
1495
    for(i = 0; i < nb_regs; i++) {
1496
        arg = args[nb_oargs + i];
1497
        ts = &s->temps[arg];
1498
        reg = tcg_target_call_iarg_regs[i];
1499
        tcg_reg_free(s, reg);
1500
        if (ts->val_type == TEMP_VAL_REG) {
1501
            if (ts->reg != reg) {
1502
                tcg_out_mov(s, reg, ts->reg);
1503
            }
1504
        } else if (ts->val_type == TEMP_VAL_MEM) {
1505
            tcg_out_ld(s, reg, ts->mem_reg, ts->mem_offset);
1506
        } else if (ts->val_type == TEMP_VAL_CONST) {
1507
            /* XXX: sign extend ? */
1508
            tcg_out_movi(s, ts->type, reg, ts->val);
1509
        } else {
1510
            tcg_abort();
1511
        }
1512
        tcg_regset_set_reg(allocated_regs, reg);
1513
    }
1514
    
1515
    /* assign function address */
1516
    func_arg = args[nb_oargs + nb_iargs - 1];
1517
    arg_ct = &def->args_ct[0];
1518
    ts = &s->temps[func_arg];
1519
    const_func_arg = 0;
1520
    if (ts->val_type == TEMP_VAL_MEM) {
1521
        reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs);
1522
        tcg_out_ld(s, reg, ts->mem_reg, ts->mem_offset);
1523
        func_arg = reg;
1524
    } else if (ts->val_type == TEMP_VAL_REG) {
1525
        reg = ts->reg;
1526
        if (!tcg_regset_test_reg(arg_ct->u.regs, reg)) {
1527
            reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs);
1528
            tcg_out_mov(s, reg, ts->reg);
1529
        }
1530
        func_arg = reg;
1531
    } else if (ts->val_type == TEMP_VAL_CONST) {
1532
        if (tcg_target_const_match(ts->val, arg_ct)) {
1533
            const_func_arg = 1;
1534
            func_arg = ts->val;
1535
        } else {
1536
            reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs);
1537
            tcg_out_movi(s, ts->type, reg, ts->val);
1538
            func_arg = reg;
1539
        }
1540
    } else {
1541
        tcg_abort();
1542
    }
1543
    
1544
    /* mark dead temporaries and free the associated registers */
1545
    for(i = 0; i < nb_params; i++) {
1546
        arg = args[nb_oargs + i];
1547
        if (IS_DEAD_IARG(i)) {
1548
            ts = &s->temps[arg];
1549
            if (ts->val_type != TEMP_VAL_CONST && !ts->fixed_reg) {
1550
                if (ts->val_type == TEMP_VAL_REG)
1551
                    s->reg_to_temp[ts->reg] = -1;
1552
                ts->val_type = TEMP_VAL_DEAD;
1553
            }
1554
        }
1555
    }
1556
    
1557
    /* clobber call registers */
1558
    for(reg = 0; reg < TCG_TARGET_NB_REGS; reg++) {
1559
        if (tcg_regset_test_reg(tcg_target_call_clobber_regs, reg)) {
1560
            tcg_reg_free(s, reg);
1561
        }
1562
    }
1563
    
1564
    /* store globals and free associated registers (we assume the call
1565
       can modify any global. */
1566
    for(i = 0; i < s->nb_globals; i++) {
1567
        ts = &s->temps[i];
1568
        if (!ts->fixed_reg) {
1569
            if (ts->val_type == TEMP_VAL_REG) {
1570
                tcg_reg_free(s, ts->reg);
1571
            }
1572
        }
1573
    }
1574

    
1575
    tcg_out_op(s, opc, &func_arg, &const_func_arg);
1576
    
1577
    tcg_out_addi(s, TCG_REG_CALL_STACK, call_stack_size);
1578

    
1579
    /* assign output registers and emit moves if needed */
1580
    for(i = 0; i < nb_oargs; i++) {
1581
        arg = args[i];
1582
        ts = &s->temps[arg];
1583
        reg = tcg_target_call_oarg_regs[i];
1584
        tcg_reg_free(s, reg);
1585
        if (ts->fixed_reg) {
1586
            if (ts->reg != reg) {
1587
                tcg_out_mov(s, ts->reg, reg);
1588
            }
1589
        } else {
1590
            if (ts->val_type == TEMP_VAL_REG)
1591
                s->reg_to_temp[ts->reg] = -1;
1592
            ts->val_type = TEMP_VAL_REG;
1593
            ts->reg = reg;
1594
            ts->mem_coherent = 0; 
1595
            s->reg_to_temp[reg] = arg;
1596
        }
1597
    }
1598
    
1599
    return nb_iargs + nb_oargs + def->nb_cargs + 1;
1600
}
1601

    
1602
#ifdef CONFIG_PROFILER
1603

    
1604
static int64_t dyngen_table_op_count[NB_OPS];
1605

    
1606
void dump_op_count(void)
1607
{
1608
    int i;
1609
    FILE *f;
1610
    f = fopen("/tmp/op1.log", "w");
1611
    for(i = 0; i < INDEX_op_end; i++) {
1612
        fprintf(f, "%s %" PRId64 "\n", tcg_op_defs[i].name, dyngen_table_op_count[i]);
1613
    }
1614
    fclose(f);
1615
    f = fopen("/tmp/op2.log", "w");
1616
    for(i = INDEX_op_end; i < NB_OPS; i++) {
1617
        fprintf(f, "%s %" PRId64 "\n", tcg_op_defs[i].name, dyngen_table_op_count[i]);
1618
    }
1619
    fclose(f);
1620
}
1621
#endif
1622

    
1623

    
1624
static inline int tcg_gen_code_common(TCGContext *s, uint8_t *gen_code_buf,
1625
                                      int do_search_pc,
1626
                                      const uint8_t *searched_pc)
1627
{
1628
    int opc, op_index, macro_op_index;
1629
    const TCGOpDef *def;
1630
    unsigned int dead_iargs;
1631
    const TCGArg *args;
1632

    
1633
#ifdef DEBUG_DISAS
1634
    if (unlikely(loglevel & CPU_LOG_TB_OP)) {
1635
        fprintf(logfile, "OP:\n");
1636
        tcg_dump_ops(s, logfile);
1637
        fprintf(logfile, "\n");
1638
    }
1639
#endif
1640

    
1641
    tcg_liveness_analysis(s);
1642

    
1643
#ifdef DEBUG_DISAS
1644
    if (unlikely(loglevel & CPU_LOG_TB_OP_OPT)) {
1645
        fprintf(logfile, "OP after la:\n");
1646
        tcg_dump_ops(s, logfile);
1647
        fprintf(logfile, "\n");
1648
    }
1649
#endif
1650

    
1651
    tcg_reg_alloc_start(s);
1652

    
1653
    s->code_buf = gen_code_buf;
1654
    s->code_ptr = gen_code_buf;
1655

    
1656
    macro_op_index = -1;
1657
    args = gen_opparam_buf;
1658
    op_index = 0;
1659
    for(;;) {
1660
        opc = gen_opc_buf[op_index];
1661
#ifdef CONFIG_PROFILER
1662
        dyngen_table_op_count[opc]++;
1663
#endif
1664
        def = &tcg_op_defs[opc];
1665
#if 0
1666
        printf("%s: %d %d %d\n", def->name,
1667
               def->nb_oargs, def->nb_iargs, def->nb_cargs);
1668
        //        dump_regs(s);
1669
#endif
1670
        switch(opc) {
1671
        case INDEX_op_mov_i32:
1672
#if TCG_TARGET_REG_BITS == 64
1673
        case INDEX_op_mov_i64:
1674
#endif
1675
            dead_iargs = s->op_dead_iargs[op_index];
1676
            tcg_reg_alloc_mov(s, def, args, dead_iargs);
1677
            break;
1678
        case INDEX_op_nop:
1679
        case INDEX_op_nop1:
1680
        case INDEX_op_nop2:
1681
        case INDEX_op_nop3:
1682
            break;
1683
        case INDEX_op_nopn:
1684
            args += args[0];
1685
            goto next;
1686
        case INDEX_op_macro_goto:
1687
            macro_op_index = op_index; /* only used for exceptions */
1688
            op_index = args[0] - 1;
1689
            args = gen_opparam_buf + args[1];
1690
            goto next;
1691
        case INDEX_op_macro_end:
1692
            macro_op_index = -1; /* only used for exceptions */
1693
            op_index = args[0] - 1;
1694
            args = gen_opparam_buf + args[1];
1695
            goto next;
1696
        case INDEX_op_macro_start:
1697
            /* must never happen here */
1698
            tcg_abort();
1699
        case INDEX_op_set_label:
1700
            tcg_reg_alloc_bb_end(s);
1701
            tcg_out_label(s, args[0], (long)s->code_ptr);
1702
            break;
1703
        case INDEX_op_call:
1704
            dead_iargs = s->op_dead_iargs[op_index];
1705
            args += tcg_reg_alloc_call(s, def, opc, args, dead_iargs);
1706
            goto next;
1707
        case INDEX_op_end:
1708
            goto the_end;
1709
        case 0 ... INDEX_op_end - 1:
1710
            /* legacy dyngen ops */
1711
#ifdef CONFIG_PROFILER
1712
            {
1713
                extern int64_t dyngen_old_op_count;
1714
                dyngen_old_op_count++;
1715
            }
1716
#endif
1717
            tcg_reg_alloc_bb_end(s);
1718
            if (do_search_pc) {
1719
                s->code_ptr += def->copy_size;
1720
                args += def->nb_args;
1721
            } else {
1722
                args = dyngen_op(s, opc, args);
1723
            }
1724
            goto next;
1725
        default:
1726
            /* Note: in order to speed up the code, it would be much
1727
               faster to have specialized register allocator functions for
1728
               some common argument patterns */
1729
            dead_iargs = s->op_dead_iargs[op_index];
1730
            tcg_reg_alloc_op(s, def, opc, args, dead_iargs);
1731
            break;
1732
        }
1733
        args += def->nb_args;
1734
    next: ;
1735
        if (do_search_pc) {
1736
            if (searched_pc < s->code_ptr) {
1737
                if (macro_op_index >= 0)
1738
                    return macro_op_index;
1739
                else
1740
                    return op_index;
1741
            }
1742
        }
1743
        op_index++;
1744
#ifndef NDEBUG
1745
        check_regs(s);
1746
#endif
1747
    }
1748
 the_end:
1749
    return -1;
1750
}
1751

    
1752
int dyngen_code(TCGContext *s, uint8_t *gen_code_buf)
1753
{
1754
#ifdef CONFIG_PROFILER
1755
    {
1756
        extern int64_t dyngen_op_count;
1757
        extern int dyngen_op_count_max;
1758
        int n;
1759
        n = (gen_opc_ptr - gen_opc_buf);
1760
        dyngen_op_count += n;
1761
        if (n > dyngen_op_count_max)
1762
            dyngen_op_count_max = n;
1763
    }
1764
#endif
1765

    
1766
    tcg_gen_code_common(s, gen_code_buf, 0, NULL);
1767

    
1768
    /* flush instruction cache */
1769
    flush_icache_range((unsigned long)gen_code_buf, 
1770
                       (unsigned long)s->code_ptr);
1771
    return s->code_ptr -  gen_code_buf;
1772
}
1773

    
1774
/* return the index of the micro operation such as the pc after is <
1775
   search_pc. Note: gen_code_buf is accessed during the operation, but
1776
   its content should not be modified. Return -1 if not found. */
1777
int dyngen_code_search_pc(TCGContext *s, uint8_t *gen_code_buf,
1778
                          const uint8_t *searched_pc)
1779
{
1780
    return tcg_gen_code_common(s, gen_code_buf, 1, searched_pc);
1781
}