2 * Tiny Code Generator for QEMU
4 * Copyright (c) 2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 /* define it to suppress various consistency checks (faster) */
28 /* define it to use liveness analysis (better code) */
29 #define USE_LIVENESS_ANALYSIS
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
44 #define NO_CPU_IO_DEFS
52 static void patch_reloc(uint8_t *code_ptr, int type,
53 tcg_target_long value);
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 },
63 TCGRegSet tcg_target_available_regs[2];
64 TCGRegSet tcg_target_call_clobber_regs;
66 /* XXX: move that inside the context */
67 uint16_t *gen_opc_ptr;
68 TCGArg *gen_opparam_ptr;
70 static inline void tcg_out8(TCGContext *s, uint8_t v)
75 static inline void tcg_out16(TCGContext *s, uint16_t v)
77 *(uint16_t *)s->code_ptr = v;
81 static inline void tcg_out32(TCGContext *s, uint32_t v)
83 *(uint32_t *)s->code_ptr = v;
87 /* label relocation processing */
89 void tcg_out_reloc(TCGContext *s, uint8_t *code_ptr, int type,
90 int label_index, long addend)
95 l = &s->labels[label_index];
97 patch_reloc(code_ptr, type, l->u.value + addend);
99 /* add a new relocation entry */
100 r = tcg_malloc(sizeof(TCGRelocation));
104 r->next = l->u.first_reloc;
105 l->u.first_reloc = r;
109 static void tcg_out_label(TCGContext *s, int label_index,
110 tcg_target_long value)
115 l = &s->labels[label_index];
118 r = l->u.first_reloc;
120 patch_reloc(r->ptr, r->type, value + r->addend);
127 int gen_new_label(void)
129 TCGContext *s = &tcg_ctx;
133 if (s->nb_labels >= TCG_MAX_LABELS)
135 idx = s->nb_labels++;
138 l->u.first_reloc = NULL;
142 #include "tcg-target.c"
145 static void pstrcpy(char *buf, int buf_size, const char *str)
155 if (c == 0 || q >= buf + buf_size - 1)
162 #if TCG_TARGET_REG_BITS == 32
163 /* strcat and truncate. */
164 static char *pstrcat(char *buf, int buf_size, const char *s)
169 pstrcpy(buf + len, buf_size - len, s);
174 /* pool based memory allocation */
175 void *tcg_malloc_internal(TCGContext *s, int size)
180 if (size > TCG_POOL_CHUNK_SIZE) {
181 /* big malloc: insert a new pool (XXX: could optimize) */
182 p = qemu_malloc(sizeof(TCGPool) + size);
185 s->pool_current->next = p;
188 p->next = s->pool_current;
198 pool_size = TCG_POOL_CHUNK_SIZE;
199 p = qemu_malloc(sizeof(TCGPool) + pool_size);
203 s->pool_current->next = p;
212 s->pool_cur = p->data + size;
213 s->pool_end = p->data + p->size;
217 void tcg_pool_reset(TCGContext *s)
219 s->pool_cur = s->pool_end = NULL;
220 s->pool_current = NULL;
223 /* free all the pool */
224 void tcg_pool_free(TCGContext *s)
228 for(p = s->pool_first; p != NULL; p = p1) {
232 s->pool_first = NULL;
233 s->pool_cur = s->pool_end = NULL;
236 void tcg_context_init(TCGContext *s)
238 int op, total_args, n;
240 TCGArgConstraint *args_ct;
243 memset(s, 0, sizeof(*s));
244 s->temps = s->static_temps;
247 /* Count total number of arguments and allocate the corresponding
250 for(op = 0; op < NB_OPS; op++) {
251 def = &tcg_op_defs[op];
252 n = def->nb_iargs + def->nb_oargs;
256 args_ct = qemu_malloc(sizeof(TCGArgConstraint) * total_args);
257 sorted_args = qemu_malloc(sizeof(int) * total_args);
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;
271 void tcg_set_frame(TCGContext *s, int reg,
272 tcg_target_long start, tcg_target_long size)
274 s->frame_start = start;
275 s->frame_end = start + size;
279 void tcg_set_macro_func(TCGContext *s, TCGMacroFunc *func)
281 s->macro_func = func;
284 void tcg_func_start(TCGContext *s)
287 s->nb_temps = s->nb_globals;
288 s->labels = tcg_malloc(sizeof(TCGLabel) * TCG_MAX_LABELS);
290 s->current_frame_offset = s->frame_start;
292 gen_opc_ptr = gen_opc_buf;
293 gen_opparam_ptr = gen_opparam_buf;
296 static inline void tcg_temp_alloc(TCGContext *s, int n)
298 if (n > TCG_MAX_TEMPS)
302 TCGv tcg_global_reg_new(TCGType type, int reg, const char *name)
304 TCGContext *s = &tcg_ctx;
308 #if TCG_TARGET_REG_BITS == 32
309 if (type != TCG_TYPE_I32)
312 if (tcg_regset_test_reg(s->reserved_regs, reg))
315 tcg_temp_alloc(s, s->nb_globals + 1);
316 ts = &s->temps[s->nb_globals];
317 ts->base_type = type;
321 ts->val_type = TEMP_VAL_REG;
324 tcg_regset_set_reg(s->reserved_regs, reg);
325 return MAKE_TCGV(idx);
328 TCGv tcg_global_mem_new(TCGType type, int reg, tcg_target_long offset,
331 TCGContext *s = &tcg_ctx;
336 #if TCG_TARGET_REG_BITS == 32
337 if (type == TCG_TYPE_I64) {
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;
344 ts->mem_allocated = 1;
346 #ifdef TCG_TARGET_WORDS_BIGENDIAN
347 ts->mem_offset = offset + 4;
349 ts->mem_offset = offset;
351 ts->val_type = TEMP_VAL_MEM;
352 pstrcpy(buf, sizeof(buf), name);
353 pstrcat(buf, sizeof(buf), "_0");
354 ts->name = strdup(buf);
357 ts->base_type = type;
358 ts->type = TCG_TYPE_I32;
360 ts->mem_allocated = 1;
362 #ifdef TCG_TARGET_WORDS_BIGENDIAN
363 ts->mem_offset = offset;
365 ts->mem_offset = offset + 4;
367 ts->val_type = TEMP_VAL_MEM;
368 pstrcpy(buf, sizeof(buf), name);
369 pstrcat(buf, sizeof(buf), "_1");
370 ts->name = strdup(buf);
376 tcg_temp_alloc(s, s->nb_globals + 1);
377 ts = &s->temps[s->nb_globals];
378 ts->base_type = type;
381 ts->mem_allocated = 1;
383 ts->mem_offset = offset;
384 ts->val_type = TEMP_VAL_MEM;
388 return MAKE_TCGV(idx);
391 TCGv tcg_temp_new(TCGType type)
393 TCGContext *s = &tcg_ctx;
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;
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;
417 tcg_temp_alloc(s, s->nb_temps + 1);
418 ts = &s->temps[s->nb_temps];
419 ts->base_type = type;
421 ts->val_type = TEMP_VAL_DEAD;
422 ts->mem_allocated = 0;
426 return MAKE_TCGV(idx);
429 TCGv tcg_const_i32(int32_t val)
431 TCGContext *s = &tcg_ctx;
436 tcg_temp_alloc(s, idx + 1);
438 ts->base_type = ts->type = TCG_TYPE_I32;
439 ts->val_type = TEMP_VAL_CONST;
443 return MAKE_TCGV(idx);
446 TCGv tcg_const_i64(int64_t val)
448 TCGContext *s = &tcg_ctx;
453 #if TCG_TARGET_REG_BITS == 32
454 tcg_temp_alloc(s, idx + 2);
456 ts->base_type = TCG_TYPE_I64;
457 ts->type = TCG_TYPE_I32;
458 ts->val_type = TEMP_VAL_CONST;
462 ts->base_type = TCG_TYPE_I32;
463 ts->type = TCG_TYPE_I32;
464 ts->val_type = TEMP_VAL_CONST;
469 tcg_temp_alloc(s, idx + 1);
471 ts->base_type = ts->type = TCG_TYPE_I64;
472 ts->val_type = TEMP_VAL_CONST;
477 return MAKE_TCGV(idx);
480 void tcg_register_helper(void *func, const char *name)
482 TCGContext *s = &tcg_ctx;
484 if ((s->nb_helpers + 1) > s->allocated_helpers) {
485 n = s->allocated_helpers;
491 s->helpers = realloc(s->helpers, n * sizeof(TCGHelperInfo));
492 s->allocated_helpers = n;
494 s->helpers[s->nb_helpers].func = func;
495 s->helpers[s->nb_helpers].name = name;
499 const char *tcg_helper_get_name(TCGContext *s, void *func)
503 for(i = 0; i < s->nb_helpers; i++) {
504 if (s->helpers[i].func == func)
505 return s->helpers[i].name;
510 static inline TCGType tcg_get_base_type(TCGContext *s, TCGv arg)
512 return s->temps[GET_TCGV(arg)].base_type;
515 static void tcg_gen_call_internal(TCGContext *s, TCGv func,
517 unsigned int nb_rets, const TCGv *rets,
518 unsigned int nb_params, const TCGv *params)
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++ = GET_TCGV(rets[i]);
526 for(i = 0; i < nb_params; i++) {
527 *gen_opparam_ptr++ = GET_TCGV(params[i]);
529 *gen_opparam_ptr++ = GET_TCGV(func);
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;
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, TCGv func, unsigned int flags,
540 unsigned int nb_rets, const TCGv *rets,
541 unsigned int nb_params, const TCGv *args1)
543 TCGv ret, *args2, rets_2[2], arg;
548 if (tcg_get_base_type(s, ret) == TCG_TYPE_I64) {
551 rets_2[1] = TCGV_HIGH(ret);
555 args2 = alloca((nb_params * 2) * sizeof(TCGv));
557 call_type = (flags & TCG_CALL_TYPE_MASK);
558 for(i = 0; i < nb_params; 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;
569 args2[j++] = TCGV_HIGH(arg);
571 #ifdef TCG_TARGET_WORDS_BIGENDIAN
572 args2[j++] = TCGV_HOGH(arg);
576 args2[j++] = TCGV_HIGH(arg);
583 tcg_gen_call_internal(s, func, flags,
584 nb_rets, rets, j, args2);
587 void tcg_gen_call(TCGContext *s, TCGv func, unsigned int flags,
588 unsigned int nb_rets, const TCGv *rets,
589 unsigned int nb_params, const TCGv *args1)
591 tcg_gen_call_internal(s, func, flags,
592 nb_rets, rets, nb_params, args1);
596 #if TCG_TARGET_REG_BITS == 32
597 void tcg_gen_shifti_i64(TCGv ret, TCGv arg1,
598 int c, int right, int arith)
606 tcg_gen_sari_i32(ret, TCGV_HIGH(arg1), c);
607 tcg_gen_sari_i32(TCGV_HIGH(ret), TCGV_HIGH(arg1), 31);
609 tcg_gen_shri_i32(ret, TCGV_HIGH(arg1), c);
610 tcg_gen_movi_i32(TCGV_HIGH(ret), 0);
613 tcg_gen_shli_i32(TCGV_HIGH(ret), arg1, c);
614 tcg_gen_movi_i32(ret, 0);
619 t0 = tcg_temp_new(TCG_TYPE_I32);
620 t1 = tcg_temp_new(TCG_TYPE_I32);
622 tcg_gen_shli_i32(t0, TCGV_HIGH(arg1), 32 - c);
624 tcg_gen_sari_i32(t1, TCGV_HIGH(arg1), c);
626 tcg_gen_shri_i32(t1, TCGV_HIGH(arg1), c);
627 tcg_gen_shri_i32(ret, arg1, c);
628 tcg_gen_or_i32(ret, ret, t0);
629 tcg_gen_mov_i32(TCGV_HIGH(ret), t1);
631 tcg_gen_shri_i32(t0, arg1, 32 - c);
632 /* Note: ret can be the same as arg1, so we use t1 */
633 tcg_gen_shli_i32(t1, arg1, c);
634 tcg_gen_shli_i32(TCGV_HIGH(ret), TCGV_HIGH(arg1), c);
635 tcg_gen_or_i32(TCGV_HIGH(ret), TCGV_HIGH(ret), t0);
636 tcg_gen_mov_i32(ret, t1);
642 void tcg_reg_alloc_start(TCGContext *s)
646 for(i = 0; i < s->nb_globals; i++) {
649 ts->val_type = TEMP_VAL_REG;
651 ts->val_type = TEMP_VAL_MEM;
654 for(i = 0; i < TCG_TARGET_NB_REGS; i++) {
655 s->reg_to_temp[i] = -1;
659 static char *tcg_get_arg_str_idx(TCGContext *s, char *buf, int buf_size,
665 if (idx < s->nb_globals) {
666 pstrcpy(buf, buf_size, ts->name);
668 if (ts->val_type == TEMP_VAL_CONST) {
669 snprintf(buf, buf_size, "$0x%" TCG_PRIlx , ts->val);
671 snprintf(buf, buf_size, "tmp%d", idx - s->nb_globals);
677 char *tcg_get_arg_str(TCGContext *s, char *buf, int buf_size, TCGv arg)
679 return tcg_get_arg_str_idx(s, buf, buf_size, GET_TCGV(arg));
682 void tcg_dump_ops(TCGContext *s, FILE *outfile)
684 const uint16_t *opc_ptr;
687 int c, i, k, nb_oargs, nb_iargs, nb_cargs;
691 opc_ptr = gen_opc_buf;
692 args = gen_opparam_buf;
693 while (opc_ptr < gen_opc_ptr) {
695 def = &tcg_op_defs[c];
696 fprintf(outfile, " %s ", def->name);
697 if (c == INDEX_op_call) {
699 /* variable number of arguments */
701 nb_oargs = arg >> 16;
702 nb_iargs = arg & 0xffff;
703 nb_cargs = def->nb_cargs;
704 } else if (c == INDEX_op_nopn) {
705 /* variable number of arguments */
710 nb_oargs = def->nb_oargs;
711 nb_iargs = def->nb_iargs;
712 nb_cargs = def->nb_cargs;
716 for(i = 0; i < nb_oargs; i++) {
718 fprintf(outfile, ",");
719 fprintf(outfile, "%s",
720 tcg_get_arg_str_idx(s, buf, sizeof(buf), args[k++]));
722 for(i = 0; i < nb_iargs; i++) {
724 fprintf(outfile, ",");
725 /* XXX: dump helper name for call */
726 fprintf(outfile, "%s",
727 tcg_get_arg_str_idx(s, buf, sizeof(buf), args[k++]));
729 for(i = 0; i < nb_cargs; i++) {
731 fprintf(outfile, ",");
733 fprintf(outfile, "$0x%" TCG_PRIlx, arg);
735 fprintf(outfile, "\n");
736 args += nb_iargs + nb_oargs + nb_cargs;
740 /* we give more priority to constraints with less registers */
741 static int get_constraint_priority(const TCGOpDef *def, int k)
743 const TCGArgConstraint *arg_ct;
746 arg_ct = &def->args_ct[k];
747 if (arg_ct->ct & TCG_CT_ALIAS) {
748 /* an alias is equivalent to a single register */
751 if (!(arg_ct->ct & TCG_CT_REG))
754 for(i = 0; i < TCG_TARGET_NB_REGS; i++) {
755 if (tcg_regset_test_reg(arg_ct->u.regs, i))
759 return TCG_TARGET_NB_REGS - n + 1;
762 /* sort from highest priority to lowest */
763 static void sort_constraints(TCGOpDef *def, int start, int n)
765 int i, j, p1, p2, tmp;
767 for(i = 0; i < n; i++)
768 def->sorted_args[start + i] = start + i;
771 for(i = 0; i < n - 1; i++) {
772 for(j = i + 1; j < n; j++) {
773 p1 = get_constraint_priority(def, def->sorted_args[start + i]);
774 p2 = get_constraint_priority(def, def->sorted_args[start + j]);
776 tmp = def->sorted_args[start + i];
777 def->sorted_args[start + i] = def->sorted_args[start + j];
778 def->sorted_args[start + j] = tmp;
784 void tcg_add_target_add_op_defs(const TCGTargetOpDef *tdefs)
795 assert(op >= 0 && op < NB_OPS);
796 def = &tcg_op_defs[op];
797 nb_args = def->nb_iargs + def->nb_oargs;
798 for(i = 0; i < nb_args; i++) {
799 ct_str = tdefs->args_ct_str[i];
800 tcg_regset_clear(def->args_ct[i].u.regs);
801 def->args_ct[i].ct = 0;
802 if (ct_str[0] >= '0' && ct_str[0] <= '9') {
804 oarg = ct_str[0] - '0';
805 assert(oarg < def->nb_oargs);
806 assert(def->args_ct[oarg].ct & TCG_CT_REG);
807 /* TCG_CT_ALIAS is for the output arguments. The input
808 argument is tagged with TCG_CT_IALIAS for
809 informative purposes. */
810 def->args_ct[i] = def->args_ct[oarg];
811 def->args_ct[oarg].ct = i | TCG_CT_ALIAS;
812 def->args_ct[i].ct |= TCG_CT_IALIAS;
819 def->args_ct[i].ct |= TCG_CT_CONST;
823 if (target_parse_constraint(&def->args_ct[i], &ct_str) < 0) {
824 fprintf(stderr, "Invalid constraint '%s' for arg %d of operation '%s'\n",
825 ct_str, i, def->name);
833 /* sort the constraints (XXX: this is just an heuristic) */
834 sort_constraints(def, 0, def->nb_oargs);
835 sort_constraints(def, def->nb_oargs, def->nb_iargs);
841 printf("%s: sorted=", def->name);
842 for(i = 0; i < def->nb_oargs + def->nb_iargs; i++)
843 printf(" %d", def->sorted_args[i]);
852 #ifdef USE_LIVENESS_ANALYSIS
854 /* set a nop for an operation using 'nb_args' */
855 static inline void tcg_set_nop(TCGContext *s, uint16_t *opc_ptr,
856 TCGArg *args, int nb_args)
859 *opc_ptr = INDEX_op_nop;
861 *opc_ptr = INDEX_op_nopn;
863 args[nb_args - 1] = nb_args;
867 /* liveness analysis: end of basic block: globals are live, temps are dead */
868 static inline void tcg_la_bb_end(TCGContext *s, uint8_t *dead_temps)
870 memset(dead_temps, 0, s->nb_globals);
871 memset(dead_temps + s->nb_globals, 1, s->nb_temps - s->nb_globals);
874 /* Liveness analysis : update the opc_dead_iargs array to tell if a
875 given input arguments is dead. Instructions updating dead
876 temporaries are removed. */
877 void tcg_liveness_analysis(TCGContext *s)
879 int i, op_index, op, nb_args, nb_iargs, nb_oargs, arg, nb_ops;
883 unsigned int dead_iargs;
885 gen_opc_ptr++; /* skip end */
887 nb_ops = gen_opc_ptr - gen_opc_buf;
889 /* XXX: make it really dynamic */
890 s->op_dead_iargs = tcg_malloc(OPC_BUF_SIZE * sizeof(uint16_t));
892 dead_temps = tcg_malloc(s->nb_temps);
893 memset(dead_temps, 1, s->nb_temps);
895 args = gen_opparam_ptr;
896 op_index = nb_ops - 1;
897 while (op_index >= 0) {
898 op = gen_opc_buf[op_index];
899 def = &tcg_op_defs[op];
904 nb_iargs = args[0] & 0xffff;
905 nb_oargs = args[0] >> 16;
908 /* output args are dead */
909 for(i = 0; i < nb_oargs; i++) {
914 /* globals are live (they may be used by the call) */
915 memset(dead_temps, 0, s->nb_globals);
917 /* input args are live */
919 for(i = 0; i < nb_iargs; i++) {
920 arg = args[i + nb_oargs];
921 if (dead_temps[arg]) {
922 dead_iargs |= (1 << i);
926 s->op_dead_iargs[op_index] = dead_iargs;
929 case INDEX_op_set_label:
931 /* mark end of basic block */
932 tcg_la_bb_end(s, dead_temps);
938 case INDEX_op_macro_2:
940 int dead_args[2], macro_id;
941 int saved_op_index, saved_arg_index;
942 int macro_op_index, macro_arg_index;
943 int macro_end_op_index, macro_end_arg_index;
948 dead_args[0] = dead_temps[args[0]];
949 dead_args[1] = dead_temps[args[1]];
952 /* call the macro function which generate code
953 depending on the live outputs */
954 saved_op_index = op_index;
955 saved_arg_index = args - gen_opparam_buf;
957 /* add a macro start instruction */
958 *gen_opc_ptr++ = INDEX_op_macro_start;
959 *gen_opparam_ptr++ = saved_op_index;
960 *gen_opparam_ptr++ = saved_arg_index;
962 macro_op_index = gen_opc_ptr - gen_opc_buf;
963 macro_arg_index = gen_opparam_ptr - gen_opparam_buf;
965 last_nb_temps = s->nb_temps;
967 s->macro_func(s, macro_id, dead_args);
969 /* realloc temp info (XXX: make it faster) */
970 if (s->nb_temps > last_nb_temps) {
971 uint8_t *new_dead_temps;
973 new_dead_temps = tcg_malloc(s->nb_temps);
974 memcpy(new_dead_temps, dead_temps, last_nb_temps);
975 memset(new_dead_temps + last_nb_temps, 1,
976 s->nb_temps - last_nb_temps);
977 dead_temps = new_dead_temps;
980 macro_end_op_index = gen_opc_ptr - gen_opc_buf;
981 macro_end_arg_index = gen_opparam_ptr - gen_opparam_buf;
983 /* end of macro: add a goto to the next instruction */
984 *gen_opc_ptr++ = INDEX_op_macro_end;
985 *gen_opparam_ptr++ = op_index + 1;
986 *gen_opparam_ptr++ = saved_arg_index + nb_args;
988 /* modify the macro operation to be a macro_goto */
989 gen_opc_buf[op_index] = INDEX_op_macro_goto;
990 args[0] = macro_op_index;
991 args[1] = macro_arg_index;
992 args[2] = 0; /* dummy third arg to match the
995 /* set the next instruction to the end of the macro */
996 op_index = macro_end_op_index;
997 args = macro_end_arg_index + gen_opparam_buf;
1000 case INDEX_op_macro_start:
1003 args = gen_opparam_buf + args[1];
1005 case INDEX_op_macro_goto:
1006 case INDEX_op_macro_end:
1007 tcg_abort(); /* should never happen in liveness analysis */
1010 /* XXX: optimize by hardcoding common cases (e.g. triadic ops) */
1012 if (op > INDEX_op_end) {
1013 args -= def->nb_args;
1014 nb_iargs = def->nb_iargs;
1015 nb_oargs = def->nb_oargs;
1017 /* Test if the operation can be removed because all
1018 its outputs are dead. We may add a flag to
1019 explicitely tell if the op has side
1020 effects. Currently we assume that if nb_oargs == 0
1021 or OPF_BB_END is set, the operation has side
1022 effects and cannot be removed */
1023 if (nb_oargs != 0 && !(def->flags & TCG_OPF_BB_END)) {
1024 for(i = 0; i < nb_oargs; i++) {
1026 if (!dead_temps[arg])
1029 tcg_set_nop(s, gen_opc_buf + op_index, args, def->nb_args);
1030 #ifdef CONFIG_PROFILER
1032 extern int64_t dyngen_tcg_del_op_count;
1033 dyngen_tcg_del_op_count++;
1039 /* output args are dead */
1040 for(i = 0; i < nb_oargs; i++) {
1042 dead_temps[arg] = 1;
1045 /* if end of basic block, update */
1046 if (def->flags & TCG_OPF_BB_END) {
1047 tcg_la_bb_end(s, dead_temps);
1050 /* input args are live */
1052 for(i = 0; i < nb_iargs; i++) {
1053 arg = args[i + nb_oargs];
1054 if (dead_temps[arg]) {
1055 dead_iargs |= (1 << i);
1057 dead_temps[arg] = 0;
1059 s->op_dead_iargs[op_index] = dead_iargs;
1062 /* legacy dyngen operations */
1063 args -= def->nb_args;
1064 /* mark end of basic block */
1065 tcg_la_bb_end(s, dead_temps);
1072 if (args != gen_opparam_buf)
1076 /* dummy liveness analysis */
1077 void tcg_liveness_analysis(TCGContext *s)
1080 nb_ops = gen_opc_ptr - gen_opc_buf;
1082 s->op_dead_iargs = tcg_malloc(nb_ops * sizeof(uint16_t));
1083 memset(s->op_dead_iargs, 0, nb_ops * sizeof(uint16_t));
1088 static void dump_regs(TCGContext *s)
1094 for(i = 0; i < s->nb_temps; i++) {
1096 printf(" %10s: ", tcg_get_arg_str_idx(s, buf, sizeof(buf), i));
1097 switch(ts->val_type) {
1099 printf("%s", tcg_target_reg_names[ts->reg]);
1102 printf("%d(%s)", (int)ts->mem_offset, tcg_target_reg_names[ts->mem_reg]);
1104 case TEMP_VAL_CONST:
1105 printf("$0x%" TCG_PRIlx, ts->val);
1117 for(i = 0; i < TCG_TARGET_NB_REGS; i++) {
1118 if (s->reg_to_temp[i] >= 0) {
1120 tcg_target_reg_names[i],
1121 tcg_get_arg_str_idx(s, buf, sizeof(buf), s->reg_to_temp[i]));
1126 static void check_regs(TCGContext *s)
1132 for(reg = 0; reg < TCG_TARGET_NB_REGS; reg++) {
1133 k = s->reg_to_temp[reg];
1136 if (ts->val_type != TEMP_VAL_REG ||
1138 printf("Inconsistency for register %s:\n",
1139 tcg_target_reg_names[reg]);
1140 printf("reg state:\n");
1146 for(k = 0; k < s->nb_temps; k++) {
1148 if (ts->val_type == TEMP_VAL_REG &&
1150 s->reg_to_temp[ts->reg] != k) {
1151 printf("Inconsistency for temp %s:\n",
1152 tcg_get_arg_str_idx(s, buf, sizeof(buf), k));
1153 printf("reg state:\n");
1161 static void temp_allocate_frame(TCGContext *s, int temp)
1164 ts = &s->temps[temp];
1165 s->current_frame_offset = (s->current_frame_offset + sizeof(tcg_target_long) - 1) & ~(sizeof(tcg_target_long) - 1);
1166 if (s->current_frame_offset + sizeof(tcg_target_long) > s->frame_end)
1168 ts->mem_offset = s->current_frame_offset;
1169 ts->mem_reg = s->frame_reg;
1170 ts->mem_allocated = 1;
1171 s->current_frame_offset += sizeof(tcg_target_long);
1174 /* free register 'reg' by spilling the corresponding temporary if necessary */
1175 static void tcg_reg_free(TCGContext *s, int reg)
1180 temp = s->reg_to_temp[reg];
1182 ts = &s->temps[temp];
1183 assert(ts->val_type == TEMP_VAL_REG);
1184 if (!ts->mem_coherent) {
1185 if (!ts->mem_allocated)
1186 temp_allocate_frame(s, temp);
1187 tcg_out_st(s, reg, ts->mem_reg, ts->mem_offset);
1189 ts->val_type = TEMP_VAL_MEM;
1190 s->reg_to_temp[reg] = -1;
1194 /* Allocate a register belonging to reg1 & ~reg2 */
1195 static int tcg_reg_alloc(TCGContext *s, TCGRegSet reg1, TCGRegSet reg2)
1200 tcg_regset_andnot(reg_ct, reg1, reg2);
1202 /* first try free registers */
1203 for(i = 0; i < TCG_TARGET_NB_REGS; i++) {
1204 reg = tcg_target_reg_alloc_order[i];
1205 if (tcg_regset_test_reg(reg_ct, reg) && s->reg_to_temp[reg] == -1)
1209 /* XXX: do better spill choice */
1210 for(i = 0; i < TCG_TARGET_NB_REGS; i++) {
1211 reg = tcg_target_reg_alloc_order[i];
1212 if (tcg_regset_test_reg(reg_ct, reg)) {
1213 tcg_reg_free(s, reg);
1221 /* at the end of a basic block, we assume all temporaries are dead and
1222 all globals are stored at their canonical location */
1223 /* XXX: optimize by handling constants in another array ? */
1224 void tcg_reg_alloc_bb_end(TCGContext *s)
1229 for(i = 0; i < s->nb_globals; i++) {
1231 if (!ts->fixed_reg) {
1232 if (ts->val_type == TEMP_VAL_REG) {
1233 tcg_reg_free(s, ts->reg);
1238 for(i = s->nb_globals; i < s->nb_temps; i++) {
1240 if (ts->val_type != TEMP_VAL_CONST) {
1241 if (ts->val_type == TEMP_VAL_REG) {
1242 s->reg_to_temp[ts->reg] = -1;
1244 ts->val_type = TEMP_VAL_DEAD;
1249 #define IS_DEAD_IARG(n) ((dead_iargs >> (n)) & 1)
1251 static void tcg_reg_alloc_mov(TCGContext *s, const TCGOpDef *def,
1253 unsigned int dead_iargs)
1257 const TCGArgConstraint *arg_ct;
1259 ots = &s->temps[args[0]];
1260 ts = &s->temps[args[1]];
1261 arg_ct = &def->args_ct[0];
1263 if (ts->val_type == TEMP_VAL_REG) {
1264 if (IS_DEAD_IARG(0) && !ts->fixed_reg && !ots->fixed_reg) {
1265 /* the mov can be suppressed */
1266 if (ots->val_type == TEMP_VAL_REG)
1267 s->reg_to_temp[ots->reg] = -1;
1269 s->reg_to_temp[reg] = -1;
1270 ts->val_type = TEMP_VAL_DEAD;
1272 if (ots->val_type == TEMP_VAL_REG) {
1275 reg = tcg_reg_alloc(s, arg_ct->u.regs, s->reserved_regs);
1277 if (ts->reg != reg) {
1278 tcg_out_mov(s, reg, ts->reg);
1281 } else if (ts->val_type == TEMP_VAL_MEM) {
1282 if (ots->val_type == TEMP_VAL_REG) {
1285 reg = tcg_reg_alloc(s, arg_ct->u.regs, s->reserved_regs);
1287 tcg_out_ld(s, reg, ts->mem_reg, ts->mem_offset);
1288 } else if (ts->val_type == TEMP_VAL_CONST) {
1289 if (ots->val_type == TEMP_VAL_REG) {
1292 reg = tcg_reg_alloc(s, arg_ct->u.regs, s->reserved_regs);
1294 tcg_out_movi(s, ots->type, reg, ts->val);
1298 s->reg_to_temp[reg] = args[0];
1300 ots->val_type = TEMP_VAL_REG;
1301 ots->mem_coherent = 0;
1304 static void tcg_reg_alloc_op(TCGContext *s,
1305 const TCGOpDef *def, int opc,
1307 unsigned int dead_iargs)
1309 TCGRegSet allocated_regs;
1310 int i, k, nb_iargs, nb_oargs, reg;
1312 const TCGArgConstraint *arg_ct;
1314 TCGArg new_args[TCG_MAX_OP_ARGS];
1315 int const_args[TCG_MAX_OP_ARGS];
1317 nb_oargs = def->nb_oargs;
1318 nb_iargs = def->nb_iargs;
1320 /* copy constants */
1321 memcpy(new_args + nb_oargs + nb_iargs,
1322 args + nb_oargs + nb_iargs,
1323 sizeof(TCGArg) * def->nb_cargs);
1325 /* satisfy input constraints */
1326 tcg_regset_set(allocated_regs, s->reserved_regs);
1327 for(k = 0; k < nb_iargs; k++) {
1328 i = def->sorted_args[nb_oargs + k];
1330 arg_ct = &def->args_ct[i];
1331 ts = &s->temps[arg];
1332 if (ts->val_type == TEMP_VAL_MEM) {
1333 reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs);
1334 tcg_out_ld(s, reg, ts->mem_reg, ts->mem_offset);
1335 ts->val_type = TEMP_VAL_REG;
1337 ts->mem_coherent = 1;
1338 s->reg_to_temp[reg] = arg;
1339 } else if (ts->val_type == TEMP_VAL_CONST) {
1340 if (tcg_target_const_match(ts->val, arg_ct)) {
1341 /* constant is OK for instruction */
1343 new_args[i] = ts->val;
1346 /* need to move to a register*/
1347 reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs);
1348 tcg_out_movi(s, ts->type, reg, ts->val);
1352 assert(ts->val_type == TEMP_VAL_REG);
1353 if ((arg_ct->ct & TCG_CT_IALIAS) &&
1354 !IS_DEAD_IARG(i - nb_oargs)) {
1355 /* if the input is aliased to an output and if it is
1356 not dead after the instruction, we must allocate
1357 a new register and move it */
1358 goto allocate_in_reg;
1361 if (tcg_regset_test_reg(arg_ct->u.regs, reg)) {
1362 /* nothing to do : the constraint is satisfied */
1365 /* allocate a new register matching the constraint
1366 and move the temporary register into it */
1367 reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs);
1368 tcg_out_mov(s, reg, ts->reg);
1373 tcg_regset_set_reg(allocated_regs, reg);
1377 /* mark dead temporaries and free the associated registers */
1378 for(i = 0; i < nb_iargs; i++) {
1379 arg = args[nb_oargs + i];
1380 if (IS_DEAD_IARG(i)) {
1381 ts = &s->temps[arg];
1382 if (ts->val_type != TEMP_VAL_CONST && !ts->fixed_reg) {
1383 if (ts->val_type == TEMP_VAL_REG)
1384 s->reg_to_temp[ts->reg] = -1;
1385 ts->val_type = TEMP_VAL_DEAD;
1390 /* XXX: permit generic clobber register list ? */
1391 if (def->flags & TCG_OPF_CALL_CLOBBER) {
1392 for(reg = 0; reg < TCG_TARGET_NB_REGS; reg++) {
1393 if (tcg_regset_test_reg(tcg_target_call_clobber_regs, reg)) {
1394 tcg_reg_free(s, reg);
1399 /* satisfy the output constraints */
1400 tcg_regset_set(allocated_regs, s->reserved_regs);
1401 for(k = 0; k < nb_oargs; k++) {
1402 i = def->sorted_args[k];
1404 arg_ct = &def->args_ct[i];
1405 ts = &s->temps[arg];
1406 if (arg_ct->ct & TCG_CT_ALIAS) {
1407 reg = new_args[arg_ct->ct & ~TCG_CT_ALIAS];
1409 /* if fixed register, we try to use it */
1411 if (ts->fixed_reg &&
1412 tcg_regset_test_reg(arg_ct->u.regs, reg)) {
1415 reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs);
1417 tcg_regset_set_reg(allocated_regs, reg);
1418 /* if a fixed register is used, then a move will be done afterwards */
1419 if (!ts->fixed_reg) {
1420 if (ts->val_type == TEMP_VAL_REG)
1421 s->reg_to_temp[ts->reg] = -1;
1422 ts->val_type = TEMP_VAL_REG;
1424 /* temp value is modified, so the value kept in memory is
1425 potentially not the same */
1426 ts->mem_coherent = 0;
1427 s->reg_to_temp[reg] = arg;
1433 if (def->flags & TCG_OPF_BB_END)
1434 tcg_reg_alloc_bb_end(s);
1436 /* emit instruction */
1437 tcg_out_op(s, opc, new_args, const_args);
1439 /* move the outputs in the correct register if needed */
1440 for(i = 0; i < nb_oargs; i++) {
1441 ts = &s->temps[args[i]];
1443 if (ts->fixed_reg && ts->reg != reg) {
1444 tcg_out_mov(s, ts->reg, reg);
1449 static int tcg_reg_alloc_call(TCGContext *s, const TCGOpDef *def,
1450 int opc, const TCGArg *args,
1451 unsigned int dead_iargs)
1453 int nb_iargs, nb_oargs, flags, nb_regs, i, reg, nb_params;
1454 TCGArg arg, func_arg;
1456 tcg_target_long stack_offset, call_stack_size;
1458 TCGRegSet allocated_regs;
1459 const TCGArgConstraint *arg_ct;
1463 nb_oargs = arg >> 16;
1464 nb_iargs = arg & 0xffff;
1465 nb_params = nb_iargs - 1;
1467 flags = args[nb_oargs + nb_iargs];
1469 nb_regs = tcg_target_get_call_iarg_regs_count(flags);
1470 if (nb_regs > nb_params)
1471 nb_regs = nb_params;
1473 /* assign stack slots first */
1474 /* XXX: preallocate call stack */
1475 call_stack_size = (nb_params - nb_regs) * sizeof(tcg_target_long);
1476 call_stack_size = (call_stack_size + TCG_TARGET_STACK_ALIGN - 1) &
1477 ~(TCG_TARGET_STACK_ALIGN - 1);
1478 tcg_out_addi(s, TCG_REG_CALL_STACK, -call_stack_size);
1481 for(i = nb_regs; i < nb_params; i++) {
1482 arg = args[nb_oargs + i];
1483 ts = &s->temps[arg];
1484 if (ts->val_type == TEMP_VAL_REG) {
1485 tcg_out_st(s, ts->reg, TCG_REG_CALL_STACK, stack_offset);
1486 } else if (ts->val_type == TEMP_VAL_MEM) {
1487 reg = tcg_reg_alloc(s, tcg_target_available_regs[ts->type],
1489 /* XXX: not correct if reading values from the stack */
1490 tcg_out_ld(s, reg, ts->mem_reg, ts->mem_offset);
1491 tcg_out_st(s, reg, TCG_REG_CALL_STACK, stack_offset);
1492 } else if (ts->val_type == TEMP_VAL_CONST) {
1493 reg = tcg_reg_alloc(s, tcg_target_available_regs[ts->type],
1495 /* XXX: sign extend may be needed on some targets */
1496 tcg_out_movi(s, ts->type, reg, ts->val);
1497 tcg_out_st(s, reg, TCG_REG_CALL_STACK, stack_offset);
1501 stack_offset += sizeof(tcg_target_long);
1504 /* assign input registers */
1505 tcg_regset_set(allocated_regs, s->reserved_regs);
1506 for(i = 0; i < nb_regs; i++) {
1507 arg = args[nb_oargs + i];
1508 ts = &s->temps[arg];
1509 reg = tcg_target_call_iarg_regs[i];
1510 tcg_reg_free(s, reg);
1511 if (ts->val_type == TEMP_VAL_REG) {
1512 if (ts->reg != reg) {
1513 tcg_out_mov(s, reg, ts->reg);
1515 } else if (ts->val_type == TEMP_VAL_MEM) {
1516 tcg_out_ld(s, reg, ts->mem_reg, ts->mem_offset);
1517 } else if (ts->val_type == TEMP_VAL_CONST) {
1518 /* XXX: sign extend ? */
1519 tcg_out_movi(s, ts->type, reg, ts->val);
1523 tcg_regset_set_reg(allocated_regs, reg);
1526 /* assign function address */
1527 func_arg = args[nb_oargs + nb_iargs - 1];
1528 arg_ct = &def->args_ct[0];
1529 ts = &s->temps[func_arg];
1531 if (ts->val_type == TEMP_VAL_MEM) {
1532 reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs);
1533 tcg_out_ld(s, reg, ts->mem_reg, ts->mem_offset);
1535 } else if (ts->val_type == TEMP_VAL_REG) {
1537 if (!tcg_regset_test_reg(arg_ct->u.regs, reg)) {
1538 reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs);
1539 tcg_out_mov(s, reg, ts->reg);
1542 } else if (ts->val_type == TEMP_VAL_CONST) {
1543 if (tcg_target_const_match(ts->val, arg_ct)) {
1547 reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs);
1548 tcg_out_movi(s, ts->type, reg, ts->val);
1555 /* mark dead temporaries and free the associated registers */
1556 for(i = 0; i < nb_params; i++) {
1557 arg = args[nb_oargs + i];
1558 if (IS_DEAD_IARG(i)) {
1559 ts = &s->temps[arg];
1560 if (ts->val_type != TEMP_VAL_CONST && !ts->fixed_reg) {
1561 if (ts->val_type == TEMP_VAL_REG)
1562 s->reg_to_temp[ts->reg] = -1;
1563 ts->val_type = TEMP_VAL_DEAD;
1568 /* clobber call registers */
1569 for(reg = 0; reg < TCG_TARGET_NB_REGS; reg++) {
1570 if (tcg_regset_test_reg(tcg_target_call_clobber_regs, reg)) {
1571 tcg_reg_free(s, reg);
1575 /* store globals and free associated registers (we assume the call
1576 can modify any global. */
1577 for(i = 0; i < s->nb_globals; i++) {
1579 if (!ts->fixed_reg) {
1580 if (ts->val_type == TEMP_VAL_REG) {
1581 tcg_reg_free(s, ts->reg);
1586 tcg_out_op(s, opc, &func_arg, &const_func_arg);
1588 tcg_out_addi(s, TCG_REG_CALL_STACK, call_stack_size);
1590 /* assign output registers and emit moves if needed */
1591 for(i = 0; i < nb_oargs; i++) {
1593 ts = &s->temps[arg];
1594 reg = tcg_target_call_oarg_regs[i];
1595 tcg_reg_free(s, reg);
1596 if (ts->fixed_reg) {
1597 if (ts->reg != reg) {
1598 tcg_out_mov(s, ts->reg, reg);
1601 if (ts->val_type == TEMP_VAL_REG)
1602 s->reg_to_temp[ts->reg] = -1;
1603 ts->val_type = TEMP_VAL_REG;
1605 ts->mem_coherent = 0;
1606 s->reg_to_temp[reg] = arg;
1610 return nb_iargs + nb_oargs + def->nb_cargs + 1;
1613 #ifdef CONFIG_PROFILER
1615 static int64_t dyngen_table_op_count[NB_OPS];
1617 void dump_op_count(void)
1621 f = fopen("/tmp/op1.log", "w");
1622 for(i = 0; i < INDEX_op_end; i++) {
1623 fprintf(f, "%s %" PRId64 "\n", tcg_op_defs[i].name, dyngen_table_op_count[i]);
1626 f = fopen("/tmp/op2.log", "w");
1627 for(i = INDEX_op_end; i < NB_OPS; i++) {
1628 fprintf(f, "%s %" PRId64 "\n", tcg_op_defs[i].name, dyngen_table_op_count[i]);
1635 static inline int tcg_gen_code_common(TCGContext *s, uint8_t *gen_code_buf,
1637 const uint8_t *searched_pc)
1639 int opc, op_index, macro_op_index;
1640 const TCGOpDef *def;
1641 unsigned int dead_iargs;
1645 if (unlikely(loglevel & CPU_LOG_TB_OP)) {
1646 fprintf(logfile, "OP:\n");
1647 tcg_dump_ops(s, logfile);
1648 fprintf(logfile, "\n");
1652 tcg_liveness_analysis(s);
1655 if (unlikely(loglevel & CPU_LOG_TB_OP_OPT)) {
1656 fprintf(logfile, "OP after la:\n");
1657 tcg_dump_ops(s, logfile);
1658 fprintf(logfile, "\n");
1662 tcg_reg_alloc_start(s);
1664 s->code_buf = gen_code_buf;
1665 s->code_ptr = gen_code_buf;
1667 macro_op_index = -1;
1668 args = gen_opparam_buf;
1671 opc = gen_opc_buf[op_index];
1672 #ifdef CONFIG_PROFILER
1673 dyngen_table_op_count[opc]++;
1675 def = &tcg_op_defs[opc];
1677 printf("%s: %d %d %d\n", def->name,
1678 def->nb_oargs, def->nb_iargs, def->nb_cargs);
1682 case INDEX_op_mov_i32:
1683 #if TCG_TARGET_REG_BITS == 64
1684 case INDEX_op_mov_i64:
1686 dead_iargs = s->op_dead_iargs[op_index];
1687 tcg_reg_alloc_mov(s, def, args, dead_iargs);
1697 case INDEX_op_macro_goto:
1698 macro_op_index = op_index; /* only used for exceptions */
1699 op_index = args[0] - 1;
1700 args = gen_opparam_buf + args[1];
1702 case INDEX_op_macro_end:
1703 macro_op_index = -1; /* only used for exceptions */
1704 op_index = args[0] - 1;
1705 args = gen_opparam_buf + args[1];
1707 case INDEX_op_macro_start:
1708 /* must never happen here */
1710 case INDEX_op_set_label:
1711 tcg_reg_alloc_bb_end(s);
1712 tcg_out_label(s, args[0], (long)s->code_ptr);
1715 dead_iargs = s->op_dead_iargs[op_index];
1716 args += tcg_reg_alloc_call(s, def, opc, args, dead_iargs);
1720 case 0 ... INDEX_op_end - 1:
1721 /* legacy dyngen ops */
1722 #ifdef CONFIG_PROFILER
1724 extern int64_t dyngen_old_op_count;
1725 dyngen_old_op_count++;
1728 tcg_reg_alloc_bb_end(s);
1730 s->code_ptr += def->copy_size;
1731 args += def->nb_args;
1733 args = dyngen_op(s, opc, args);
1737 /* Note: in order to speed up the code, it would be much
1738 faster to have specialized register allocator functions for
1739 some common argument patterns */
1740 dead_iargs = s->op_dead_iargs[op_index];
1741 tcg_reg_alloc_op(s, def, opc, args, dead_iargs);
1744 args += def->nb_args;
1747 if (searched_pc < s->code_ptr) {
1748 if (macro_op_index >= 0)
1749 return macro_op_index;
1763 int dyngen_code(TCGContext *s, uint8_t *gen_code_buf)
1765 #ifdef CONFIG_PROFILER
1767 extern int64_t dyngen_op_count;
1768 extern int dyngen_op_count_max;
1770 n = (gen_opc_ptr - gen_opc_buf);
1771 dyngen_op_count += n;
1772 if (n > dyngen_op_count_max)
1773 dyngen_op_count_max = n;
1777 tcg_gen_code_common(s, gen_code_buf, 0, NULL);
1779 /* flush instruction cache */
1780 flush_icache_range((unsigned long)gen_code_buf,
1781 (unsigned long)s->code_ptr);
1782 return s->code_ptr - gen_code_buf;
1785 /* return the index of the micro operation such as the pc after is <
1786 search_pc. Note: gen_code_buf is accessed during the operation, but
1787 its content should not be modified. Return -1 if not found. */
1788 int dyngen_code_search_pc(TCGContext *s, uint8_t *gen_code_buf,
1789 const uint8_t *searched_pc)
1791 return tcg_gen_code_common(s, gen_code_buf, 1, searched_pc);