1 | #!/usr/bin/env python
|
---|
2 | # -*- coding: utf-8 -*-
|
---|
3 | # $Id: IEMAllN8vePython.py 101484 2023-10-18 01:32:17Z vboxsync $
|
---|
4 | # pylint: disable=invalid-name
|
---|
5 |
|
---|
6 | """
|
---|
7 | Native recompiler side-kick for IEMAllThrdPython.py.
|
---|
8 |
|
---|
9 | Analyzes the each threaded function variant to see if we can we're able to
|
---|
10 | recompile it, then provides modifies MC block code for doing so.
|
---|
11 | """
|
---|
12 |
|
---|
13 | from __future__ import print_function;
|
---|
14 |
|
---|
15 | __copyright__ = \
|
---|
16 | """
|
---|
17 | Copyright (C) 2023 Oracle and/or its affiliates.
|
---|
18 |
|
---|
19 | This file is part of VirtualBox base platform packages, as
|
---|
20 | available from https://www.virtualbox.org.
|
---|
21 |
|
---|
22 | This program is free software; you can redistribute it and/or
|
---|
23 | modify it under the terms of the GNU General Public License
|
---|
24 | as published by the Free Software Foundation, in version 3 of the
|
---|
25 | License.
|
---|
26 |
|
---|
27 | This program is distributed in the hope that it will be useful, but
|
---|
28 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
29 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
30 | General Public License for more details.
|
---|
31 |
|
---|
32 | You should have received a copy of the GNU General Public License
|
---|
33 | along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
34 |
|
---|
35 | SPDX-License-Identifier: GPL-3.0-only
|
---|
36 | """
|
---|
37 | __version__ = "$Revision: 101484 $"
|
---|
38 |
|
---|
39 | # Standard python imports:
|
---|
40 | #import sys;
|
---|
41 |
|
---|
42 | # Out python imports:
|
---|
43 | import IEMAllInstPython as iai;
|
---|
44 |
|
---|
45 |
|
---|
46 | ## Supplememnts g_dMcStmtParsers.
|
---|
47 | g_dMcStmtThreaded = {
|
---|
48 | 'IEM_MC_DEFER_TO_CIMPL_0_RET_THREADED': (None, True, True, ),
|
---|
49 | 'IEM_MC_DEFER_TO_CIMPL_1_RET_THREADED': (None, True, True, ),
|
---|
50 | 'IEM_MC_DEFER_TO_CIMPL_2_RET_THREADED': (None, True, True, ),
|
---|
51 | 'IEM_MC_DEFER_TO_CIMPL_3_RET_THREADED': (None, True, True, ),
|
---|
52 |
|
---|
53 | 'IEM_MC_ADVANCE_RIP_AND_FINISH_THREADED_PC16': (None, True, False, ),
|
---|
54 | 'IEM_MC_ADVANCE_RIP_AND_FINISH_THREADED_PC32': (None, True, True, ),
|
---|
55 | 'IEM_MC_ADVANCE_RIP_AND_FINISH_THREADED_PC64': (None, True, False, ),
|
---|
56 |
|
---|
57 | 'IEM_MC_ADVANCE_RIP_AND_FINISH_THREADED_PC16_WITH_FLAGS': (None, True, False, ),
|
---|
58 | 'IEM_MC_ADVANCE_RIP_AND_FINISH_THREADED_PC32_WITH_FLAGS': (None, True, False, ),
|
---|
59 | 'IEM_MC_ADVANCE_RIP_AND_FINISH_THREADED_PC64_WITH_FLAGS': (None, True, False, ),
|
---|
60 |
|
---|
61 | 'IEM_MC_CALC_RM_EFF_ADDR_THREADED_16_ADDR32': (None, False, False, ),
|
---|
62 | 'IEM_MC_CALC_RM_EFF_ADDR_THREADED_16_PRE386': (None, False, False, ),
|
---|
63 | 'IEM_MC_CALC_RM_EFF_ADDR_THREADED_16': (None, False, False, ),
|
---|
64 | 'IEM_MC_CALC_RM_EFF_ADDR_THREADED_32_ADDR16': (None, False, False, ),
|
---|
65 | 'IEM_MC_CALC_RM_EFF_ADDR_THREADED_32_FLAT': (None, False, False, ),
|
---|
66 | 'IEM_MC_CALC_RM_EFF_ADDR_THREADED_32': (None, False, False, ),
|
---|
67 | 'IEM_MC_CALC_RM_EFF_ADDR_THREADED_64_ADDR32': (None, False, False, ),
|
---|
68 | 'IEM_MC_CALC_RM_EFF_ADDR_THREADED_64_FSGS': (None, False, False, ),
|
---|
69 | 'IEM_MC_CALC_RM_EFF_ADDR_THREADED_64': (None, False, False, ),
|
---|
70 |
|
---|
71 | 'IEM_MC_CALL_CIMPL_1_THREADED': (None, True, False, ),
|
---|
72 | 'IEM_MC_CALL_CIMPL_2_THREADED': (None, True, False, ),
|
---|
73 | 'IEM_MC_CALL_CIMPL_3_THREADED': (None, True, False, ),
|
---|
74 | 'IEM_MC_CALL_CIMPL_4_THREADED': (None, True, False, ),
|
---|
75 | 'IEM_MC_CALL_CIMPL_5_THREADED': (None, True, False, ),
|
---|
76 |
|
---|
77 | 'IEM_MC_REL_JMP_S8_AND_FINISH_THREADED_PC16': (None, True, False, ),
|
---|
78 | 'IEM_MC_REL_JMP_S8_AND_FINISH_THREADED_PC32': (None, True, False, ),
|
---|
79 | 'IEM_MC_REL_JMP_S8_AND_FINISH_THREADED_PC64': (None, True, False, ),
|
---|
80 | 'IEM_MC_REL_JMP_S16_AND_FINISH_THREADED_PC16': (None, True, False, ),
|
---|
81 | 'IEM_MC_REL_JMP_S16_AND_FINISH_THREADED_PC32': (None, True, False, ),
|
---|
82 | 'IEM_MC_REL_JMP_S16_AND_FINISH_THREADED_PC64': (None, True, False, ),
|
---|
83 | 'IEM_MC_REL_JMP_S32_AND_FINISH_THREADED_PC32': (None, True, False, ),
|
---|
84 | 'IEM_MC_REL_JMP_S32_AND_FINISH_THREADED_PC64': (None, True, False, ),
|
---|
85 |
|
---|
86 | 'IEM_MC_REL_JMP_S8_AND_FINISH_THREADED_PC16_WITH_FLAGS': (None, True, False, ),
|
---|
87 | 'IEM_MC_REL_JMP_S8_AND_FINISH_THREADED_PC32_WITH_FLAGS': (None, True, False, ),
|
---|
88 | 'IEM_MC_REL_JMP_S8_AND_FINISH_THREADED_PC64_WITH_FLAGS': (None, True, False, ),
|
---|
89 | 'IEM_MC_REL_JMP_S16_AND_FINISH_THREADED_PC16_WITH_FLAGS': (None, True, False, ),
|
---|
90 | 'IEM_MC_REL_JMP_S16_AND_FINISH_THREADED_PC32_WITH_FLAGS': (None, True, False, ),
|
---|
91 | 'IEM_MC_REL_JMP_S16_AND_FINISH_THREADED_PC64_WITH_FLAGS': (None, True, False, ),
|
---|
92 | 'IEM_MC_REL_JMP_S32_AND_FINISH_THREADED_PC32_WITH_FLAGS': (None, True, False, ),
|
---|
93 | 'IEM_MC_REL_JMP_S32_AND_FINISH_THREADED_PC64_WITH_FLAGS': (None, True, False, ),
|
---|
94 |
|
---|
95 | 'IEM_MC_STORE_GREG_U8_THREADED': (None, True, False, ),
|
---|
96 | 'IEM_MC_STORE_GREG_U8_CONST_THREADED': (None, True, False, ),
|
---|
97 | 'IEM_MC_FETCH_GREG_U8_THREADED': (None, False, False, ),
|
---|
98 | 'IEM_MC_FETCH_GREG_U8_SX_U16_THREADED': (None, False, False, ),
|
---|
99 | 'IEM_MC_FETCH_GREG_U8_SX_U32_THREADED': (None, False, False, ),
|
---|
100 | 'IEM_MC_FETCH_GREG_U8_SX_U64_THREADED': (None, False, False, ),
|
---|
101 | 'IEM_MC_FETCH_GREG_U8_ZX_U16_THREADED': (None, False, False, ),
|
---|
102 | 'IEM_MC_FETCH_GREG_U8_ZX_U32_THREADED': (None, False, False, ),
|
---|
103 | 'IEM_MC_FETCH_GREG_U8_ZX_U64_THREADED': (None, False, False, ),
|
---|
104 | 'IEM_MC_REF_GREG_U8_THREADED': (None, True, False, ),
|
---|
105 |
|
---|
106 | # Flat Mem:
|
---|
107 | 'IEM_MC_FETCH_MEM16_FLAT_U8': (None, True, False, ),
|
---|
108 | 'IEM_MC_FETCH_MEM32_FLAT_U8': (None, True, False, ),
|
---|
109 | 'IEM_MC_FETCH_MEM_FLAT_D80': (None, True, False, ),
|
---|
110 | 'IEM_MC_FETCH_MEM_FLAT_I16': (None, True, False, ),
|
---|
111 | 'IEM_MC_FETCH_MEM_FLAT_I32': (None, True, False, ),
|
---|
112 | 'IEM_MC_FETCH_MEM_FLAT_I64': (None, True, False, ),
|
---|
113 | 'IEM_MC_FETCH_MEM_FLAT_R32': (None, True, False, ),
|
---|
114 | 'IEM_MC_FETCH_MEM_FLAT_R64': (None, True, False, ),
|
---|
115 | 'IEM_MC_FETCH_MEM_FLAT_R80': (None, True, False, ),
|
---|
116 | 'IEM_MC_FETCH_MEM_FLAT_U128_ALIGN_SSE': (None, True, False, ),
|
---|
117 | 'IEM_MC_FETCH_MEM_FLAT_U128_NO_AC': (None, True, False, ),
|
---|
118 | 'IEM_MC_FETCH_MEM_FLAT_U128': (None, True, False, ),
|
---|
119 | 'IEM_MC_FETCH_MEM_FLAT_U16_DISP': (None, True, False, ),
|
---|
120 | 'IEM_MC_FETCH_MEM_FLAT_U16_SX_U32': (None, True, False, ),
|
---|
121 | 'IEM_MC_FETCH_MEM_FLAT_U16_SX_U64': (None, True, False, ),
|
---|
122 | 'IEM_MC_FETCH_MEM_FLAT_U16': (None, True, False, ),
|
---|
123 | 'IEM_MC_FETCH_MEM_FLAT_U16_ZX_U32': (None, True, False, ),
|
---|
124 | 'IEM_MC_FETCH_MEM_FLAT_U16_ZX_U64': (None, True, False, ),
|
---|
125 | 'IEM_MC_FETCH_MEM_FLAT_U256_ALIGN_AVX': (None, True, False, ),
|
---|
126 | 'IEM_MC_FETCH_MEM_FLAT_U256_NO_AC': (None, True, False, ),
|
---|
127 | 'IEM_MC_FETCH_MEM_FLAT_U256': (None, True, False, ),
|
---|
128 | 'IEM_MC_FETCH_MEM_FLAT_U32_DISP': (None, True, False, ),
|
---|
129 | 'IEM_MC_FETCH_MEM_FLAT_U32_SX_U64': (None, True, False, ),
|
---|
130 | 'IEM_MC_FETCH_MEM_FLAT_U32': (None, True, False, ),
|
---|
131 | 'IEM_MC_FETCH_MEM_FLAT_U32_ZX_U64': (None, True, False, ),
|
---|
132 | 'IEM_MC_FETCH_MEM_FLAT_U64': (None, True, False, ),
|
---|
133 | 'IEM_MC_FETCH_MEM_FLAT_U8_SX_U16': (None, True, False, ),
|
---|
134 | 'IEM_MC_FETCH_MEM_FLAT_U8_SX_U32': (None, True, False, ),
|
---|
135 | 'IEM_MC_FETCH_MEM_FLAT_U8_SX_U64': (None, True, False, ),
|
---|
136 | 'IEM_MC_FETCH_MEM_FLAT_U8': (None, True, False, ),
|
---|
137 | 'IEM_MC_FETCH_MEM_FLAT_U8_ZX_U16': (None, True, False, ),
|
---|
138 | 'IEM_MC_FETCH_MEM_FLAT_U8_ZX_U32': (None, True, False, ),
|
---|
139 | 'IEM_MC_FETCH_MEM_FLAT_U8_ZX_U64': (None, True, False, ),
|
---|
140 | 'IEM_MC_FETCH_MEM_FLAT_XMM_ALIGN_SSE': (None, True, False, ),
|
---|
141 | 'IEM_MC_FETCH_MEM_FLAT_XMM_U32': (None, True, False, ),
|
---|
142 | 'IEM_MC_FETCH_MEM_FLAT_XMM_U64': (None, True, False, ),
|
---|
143 | 'IEM_MC_MEM_FLAT_MAP_EX': (None, True, False, ),
|
---|
144 | 'IEM_MC_MEM_FLAT_MAP': (None, True, False, ),
|
---|
145 | 'IEM_MC_MEM_FLAT_MAP_U16_RO': (None, True, False, ),
|
---|
146 | 'IEM_MC_MEM_FLAT_MAP_U16_RW': (None, True, False, ),
|
---|
147 | 'IEM_MC_MEM_FLAT_MAP_U32_RO': (None, True, False, ),
|
---|
148 | 'IEM_MC_MEM_FLAT_MAP_U32_RW': (None, True, False, ),
|
---|
149 | 'IEM_MC_MEM_FLAT_MAP_U64_RO': (None, True, False, ),
|
---|
150 | 'IEM_MC_MEM_FLAT_MAP_U64_RW': (None, True, False, ),
|
---|
151 | 'IEM_MC_MEM_FLAT_MAP_U8_RO': (None, True, False, ),
|
---|
152 | 'IEM_MC_MEM_FLAT_MAP_U8_RW': (None, True, False, ),
|
---|
153 | 'IEM_MC_STORE_MEM_FLAT_U128_ALIGN_SSE': (None, True, False, ),
|
---|
154 | 'IEM_MC_STORE_MEM_FLAT_U128': (None, True, False, ),
|
---|
155 | 'IEM_MC_STORE_MEM_FLAT_U16': (None, True, False, ),
|
---|
156 | 'IEM_MC_STORE_MEM_FLAT_U256_ALIGN_AVX': (None, True, False, ),
|
---|
157 | 'IEM_MC_STORE_MEM_FLAT_U256': (None, True, False, ),
|
---|
158 | 'IEM_MC_STORE_MEM_FLAT_U32': (None, True, False, ),
|
---|
159 | 'IEM_MC_STORE_MEM_FLAT_U64': (None, True, False, ),
|
---|
160 | 'IEM_MC_STORE_MEM_FLAT_U8_CONST': (None, True, False, ),
|
---|
161 | 'IEM_MC_STORE_MEM_FLAT_U8': (None, True, False, ),
|
---|
162 |
|
---|
163 | # Flat Stack:
|
---|
164 | 'IEM_MC_FLAT64_PUSH_U16': (None, True, False, ),
|
---|
165 | 'IEM_MC_FLAT64_PUSH_U64': (None, True, False, ),
|
---|
166 | 'IEM_MC_FLAT64_PUSH_U16': (None, True, False, ),
|
---|
167 | 'IEM_MC_FLAT64_PUSH_U64': (None, True, False, ),
|
---|
168 | 'IEM_MC_FLAT64_POP_U16': (None, True, False, ),
|
---|
169 | 'IEM_MC_FLAT64_POP_U64': (None, True, False, ),
|
---|
170 | 'IEM_MC_FLAT64_POP_U16': (None, True, False, ),
|
---|
171 | 'IEM_MC_FLAT64_POP_U64': (None, True, False, ),
|
---|
172 | 'IEM_MC_FLAT64_PUSH_U16': (None, True, False, ),
|
---|
173 | 'IEM_MC_FLAT64_PUSH_U64': (None, True, False, ),
|
---|
174 | 'IEM_MC_FLAT64_PUSH_U16': (None, True, False, ),
|
---|
175 | 'IEM_MC_FLAT64_PUSH_U64': (None, True, False, ),
|
---|
176 | 'IEM_MC_FLAT32_PUSH_U16': (None, True, False, ),
|
---|
177 | 'IEM_MC_FLAT64_PUSH_U16': (None, True, False, ),
|
---|
178 | 'IEM_MC_FLAT32_PUSH_U32': (None, True, False, ),
|
---|
179 | 'IEM_MC_FLAT64_PUSH_U64': (None, True, False, ),
|
---|
180 | };
|
---|
181 |
|
---|
182 | class NativeRecompFunctionVariation(object):
|
---|
183 | """
|
---|
184 | Class that deals with transforming a threaded function variation into a
|
---|
185 | native recompiler function.
|
---|
186 |
|
---|
187 | This base class doesn't do any transforming and just renders the same
|
---|
188 | code as for the threaded function.
|
---|
189 | """
|
---|
190 |
|
---|
191 | def __init__(self, oVariation, sHostArch):
|
---|
192 | self.oVariation = oVariation # type: ThreadedFunctionVariation
|
---|
193 | self.sHostArch = sHostArch;
|
---|
194 |
|
---|
195 | def isRecompilable(self):
|
---|
196 | """
|
---|
197 | Predicate that returns whether the variant can be recompiled natively
|
---|
198 | (for the selected host architecture).
|
---|
199 | """
|
---|
200 | return True;
|
---|
201 |
|
---|
202 | def renderCode(self, cchIndent):
|
---|
203 | """
|
---|
204 | Returns the native recompiler function body for this threaded variant.
|
---|
205 | """
|
---|
206 | aoStmts = self.oVariation.aoStmtsForThreadedFunction # type: list(McStmt)
|
---|
207 | return iai.McStmt.renderCodeForList(aoStmts, cchIndent);
|
---|
208 |
|
---|
209 | @staticmethod
|
---|
210 | def checkStatements(aoStmts, sHostArch):
|
---|
211 | """
|
---|
212 | Checks that all the given statements are supported by the native recompiler.
|
---|
213 | """
|
---|
214 | _ = sHostArch;
|
---|
215 | for oStmt in aoStmts: # type: McStmt
|
---|
216 | if not oStmt.isCppStmt():
|
---|
217 | aInfo = iai.g_dMcStmtParsers.get(oStmt.sName);
|
---|
218 | if not aInfo:
|
---|
219 | aInfo = g_dMcStmtThreaded.get(oStmt.sName);
|
---|
220 | if not aInfo:
|
---|
221 | raise Exception('Unknown statement: %s' % (oStmt.sName, ));
|
---|
222 | if aInfo[2] is False:
|
---|
223 | return False;
|
---|
224 | if aInfo[2] is not True:
|
---|
225 | if isinstance(aInfo[2], str):
|
---|
226 | if aInfo[2] != sHostArch:
|
---|
227 | return False;
|
---|
228 | elif sHostArch not in aInfo[2]:
|
---|
229 | return False;
|
---|
230 | #elif not self.fDecode:
|
---|
231 |
|
---|
232 | if isinstance(oStmt, iai.McStmtCond):
|
---|
233 | if not NativeRecompFunctionVariation.checkStatements(oStmt.aoIfBranch, sHostArch):
|
---|
234 | return False;
|
---|
235 | if not NativeRecompFunctionVariation.checkStatements(oStmt.aoElseBranch, sHostArch):
|
---|
236 | return False;
|
---|
237 | return True;
|
---|
238 |
|
---|
239 |
|
---|
240 |
|
---|
241 | def analyzeVariantForNativeRecomp(oVariation,
|
---|
242 | sHostArch): # type: (ThreadedFunctionVariation, str) -> NativeRecompFunctionVariation
|
---|
243 | """
|
---|
244 | This function analyzes the threaded function variant and returns an
|
---|
245 | NativeRecompFunctionVariation instance for it, unless it's not
|
---|
246 | possible to recompile at present.
|
---|
247 |
|
---|
248 | Returns NativeRecompFunctionVariation or None.
|
---|
249 | """
|
---|
250 |
|
---|
251 | #
|
---|
252 | # Analyze the statements.
|
---|
253 | #
|
---|
254 | aoStmts = oVariation.aoStmtsForThreadedFunction # type: list(McStmt)
|
---|
255 | if NativeRecompFunctionVariation.checkStatements(aoStmts, sHostArch):
|
---|
256 | return NativeRecompFunctionVariation(oVariation, sHostArch);
|
---|
257 |
|
---|
258 | # The simplest case are the IEM_MC_DEFER_TO_CIMPL_*_RET_THREADED ones, just pass them thru:
|
---|
259 | #if ( len(aoStmts) == 1
|
---|
260 | # and aoStmts[0].sName.startswith('IEM_MC_DEFER_TO_CIMPL_')
|
---|
261 | # and aoStmts[0].sName.endswith('_RET_THREADED')):
|
---|
262 | # return NativeRecompFunctionVariation(oVariation, sHostArch);
|
---|
263 |
|
---|
264 |
|
---|
265 | # g_dMcStmtParsers
|
---|
266 |
|
---|
267 |
|
---|
268 | return None;
|
---|