blob: 5bd2d3711a2696967f8ed930e4a9cbb756e75c9e [file] [log] [blame]
bellardd19893d2003-06-15 19:58:51 +00001/*
2 * Host code generation
ths5fafdf22007-09-16 21:08:06 +00003 *
bellardd19893d2003-06-15 19:58:51 +00004 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
Blue Swirl8167ee82009-07-16 20:47:01 +000017 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
bellardd19893d2003-06-15 19:58:51 +000018 */
19#include <stdarg.h>
20#include <stdlib.h>
21#include <stdio.h>
22#include <string.h>
23#include <inttypes.h>
24
25#include "config.h"
bellard20543962003-06-15 23:28:43 +000026
bellardaf5ad102004-01-04 23:28:12 +000027#define NO_CPU_IO_DEFS
bellardd3eead22003-09-30 20:59:51 +000028#include "cpu.h"
bellardd19893d2003-06-15 19:58:51 +000029#include "disas.h"
bellard57fec1f2008-02-01 10:50:11 +000030#include "tcg.h"
Blue Swirl29e922b2010-03-29 19:24:00 +000031#include "qemu-timer.h"
bellardd19893d2003-06-15 19:58:51 +000032
bellard57fec1f2008-02-01 10:50:11 +000033/* code generation context */
34TCGContext tcg_ctx;
bellardd19893d2003-06-15 19:58:51 +000035
bellardd19893d2003-06-15 19:58:51 +000036uint16_t gen_opc_buf[OPC_BUF_SIZE];
bellard57fec1f2008-02-01 10:50:11 +000037TCGArg gen_opparam_buf[OPPARAM_BUF_SIZE];
bellardc4687872005-01-03 23:44:44 +000038
39target_ulong gen_opc_pc[OPC_BUF_SIZE];
pbrook2e70f6e2008-06-29 01:03:05 +000040uint16_t gen_opc_icount[OPC_BUF_SIZE];
bellardd19893d2003-06-15 19:58:51 +000041uint8_t gen_opc_instr_start[OPC_BUF_SIZE];
bellardd19893d2003-06-15 19:58:51 +000042
bellard57fec1f2008-02-01 10:50:11 +000043void cpu_gen_init(void)
44{
45 tcg_context_init(&tcg_ctx);
bellard57fec1f2008-02-01 10:50:11 +000046}
47
bellardd19893d2003-06-15 19:58:51 +000048/* return non zero if the very first instruction is invalid so that
ths5fafdf22007-09-16 21:08:06 +000049 the virtual CPU can trigger an exception.
bellardd19893d2003-06-15 19:58:51 +000050
51 '*gen_code_size_ptr' contains the size of the generated code (host
52 code).
53*/
Andreas Färber9349b4f2012-03-14 01:38:32 +010054int cpu_gen_code(CPUArchState *env, TranslationBlock *tb, int *gen_code_size_ptr)
bellardd19893d2003-06-15 19:58:51 +000055{
bellard57fec1f2008-02-01 10:50:11 +000056 TCGContext *s = &tcg_ctx;
bellardd19893d2003-06-15 19:58:51 +000057 uint8_t *gen_code_buf;
58 int gen_code_size;
bellard57fec1f2008-02-01 10:50:11 +000059#ifdef CONFIG_PROFILER
60 int64_t ti;
61#endif
62
63#ifdef CONFIG_PROFILER
bellardb67d9a52008-05-23 09:57:34 +000064 s->tb_count1++; /* includes aborted translations because of
65 exceptions */
bellard57fec1f2008-02-01 10:50:11 +000066 ti = profile_getclock();
67#endif
68 tcg_func_start(s);
bellardd19893d2003-06-15 19:58:51 +000069
ths2cfc5f12008-07-18 18:01:29 +000070 gen_intermediate_code(env, tb);
71
bellardec6338b2007-11-08 14:25:03 +000072 /* generate machine code */
bellard57fec1f2008-02-01 10:50:11 +000073 gen_code_buf = tb->tc_ptr;
bellardec6338b2007-11-08 14:25:03 +000074 tb->tb_next_offset[0] = 0xffff;
75 tb->tb_next_offset[1] = 0xffff;
bellard57fec1f2008-02-01 10:50:11 +000076 s->tb_next_offset = tb->tb_next_offset;
bellard4cbb86e2003-09-17 22:53:29 +000077#ifdef USE_DIRECT_JUMP
bellard57fec1f2008-02-01 10:50:11 +000078 s->tb_jmp_offset = tb->tb_jmp_offset;
79 s->tb_next = NULL;
bellardd19893d2003-06-15 19:58:51 +000080#else
bellard57fec1f2008-02-01 10:50:11 +000081 s->tb_jmp_offset = NULL;
82 s->tb_next = tb->tb_next;
bellardd19893d2003-06-15 19:58:51 +000083#endif
bellard57fec1f2008-02-01 10:50:11 +000084
85#ifdef CONFIG_PROFILER
bellardb67d9a52008-05-23 09:57:34 +000086 s->tb_count++;
87 s->interm_time += profile_getclock() - ti;
88 s->code_time -= profile_getclock();
bellard57fec1f2008-02-01 10:50:11 +000089#endif
aurel3254604f72008-12-07 20:35:00 +000090 gen_code_size = tcg_gen_code(s, gen_code_buf);
bellardd19893d2003-06-15 19:58:51 +000091 *gen_code_size_ptr = gen_code_size;
bellard57fec1f2008-02-01 10:50:11 +000092#ifdef CONFIG_PROFILER
bellardb67d9a52008-05-23 09:57:34 +000093 s->code_time += profile_getclock();
94 s->code_in_len += tb->size;
95 s->code_out_len += gen_code_size;
bellard57fec1f2008-02-01 10:50:11 +000096#endif
97
bellardd19893d2003-06-15 19:58:51 +000098#ifdef DEBUG_DISAS
aliguori8fec2b82009-01-15 22:36:53 +000099 if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM)) {
aliguori93fcfe32009-01-15 22:34:14 +0000100 qemu_log("OUT: [size=%d]\n", *gen_code_size_ptr);
101 log_disas(tb->tc_ptr, *gen_code_size_ptr);
102 qemu_log("\n");
aliguori31b1a7b2009-01-15 22:35:09 +0000103 qemu_log_flush();
bellardd19893d2003-06-15 19:58:51 +0000104 }
105#endif
106 return 0;
107}
108
ths5fafdf22007-09-16 21:08:06 +0000109/* The cpu state corresponding to 'searched_pc' is restored.
bellardd19893d2003-06-15 19:58:51 +0000110 */
ths5fafdf22007-09-16 21:08:06 +0000111int cpu_restore_state(TranslationBlock *tb,
Stefan Weil6375e092012-04-06 22:26:15 +0200112 CPUArchState *env, uintptr_t searched_pc)
bellardd19893d2003-06-15 19:58:51 +0000113{
bellard57fec1f2008-02-01 10:50:11 +0000114 TCGContext *s = &tcg_ctx;
115 int j;
Stefan Weil6375e092012-04-06 22:26:15 +0200116 uintptr_t tc_ptr;
bellard57fec1f2008-02-01 10:50:11 +0000117#ifdef CONFIG_PROFILER
118 int64_t ti;
119#endif
120
121#ifdef CONFIG_PROFILER
122 ti = profile_getclock();
123#endif
124 tcg_func_start(s);
bellardd19893d2003-06-15 19:58:51 +0000125
ths2cfc5f12008-07-18 18:01:29 +0000126 gen_intermediate_code_pc(env, tb);
ths3b46e622007-09-17 08:09:54 +0000127
pbrook2e70f6e2008-06-29 01:03:05 +0000128 if (use_icount) {
129 /* Reset the cycle counter to the start of the block. */
130 env->icount_decr.u16.low += tb->icount;
131 /* Clear the IO flag. */
132 env->can_do_io = 0;
133 }
134
bellardd19893d2003-06-15 19:58:51 +0000135 /* find opc index corresponding to search_pc */
Stefan Weil6375e092012-04-06 22:26:15 +0200136 tc_ptr = (uintptr_t)tb->tc_ptr;
bellardd19893d2003-06-15 19:58:51 +0000137 if (searched_pc < tc_ptr)
138 return -1;
bellard57fec1f2008-02-01 10:50:11 +0000139
140 s->tb_next_offset = tb->tb_next_offset;
141#ifdef USE_DIRECT_JUMP
142 s->tb_jmp_offset = tb->tb_jmp_offset;
143 s->tb_next = NULL;
144#else
145 s->tb_jmp_offset = NULL;
146 s->tb_next = tb->tb_next;
147#endif
aurel3254604f72008-12-07 20:35:00 +0000148 j = tcg_gen_code_search_pc(s, (uint8_t *)tc_ptr, searched_pc - tc_ptr);
bellard57fec1f2008-02-01 10:50:11 +0000149 if (j < 0)
150 return -1;
bellardd19893d2003-06-15 19:58:51 +0000151 /* now find start of instruction before */
152 while (gen_opc_instr_start[j] == 0)
153 j--;
pbrook2e70f6e2008-06-29 01:03:05 +0000154 env->icount_decr.u16.low -= gen_opc_icount[j];
ths3b46e622007-09-17 08:09:54 +0000155
Stefan Weile87b7cb2011-04-18 06:39:52 +0000156 restore_state_to_opc(env, tb, j);
bellard57fec1f2008-02-01 10:50:11 +0000157
158#ifdef CONFIG_PROFILER
bellardb67d9a52008-05-23 09:57:34 +0000159 s->restore_time += profile_getclock() - ti;
160 s->restore_count++;
bellard57fec1f2008-02-01 10:50:11 +0000161#endif
bellardd19893d2003-06-15 19:58:51 +0000162 return 0;
163}