VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMAll/IEMAllThreadedPython.py@ 99819

最後變更 在這個檔案從99819是 99819,由 vboxsync 提交於 22 月 前

VMM/IEM: More recompiler work. bugref:10369

  • 屬性 svn:eol-style 設為 LF
  • 屬性 svn:executable 設為 *
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 61.8 KB
 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# $Id: IEMAllThreadedPython.py 99819 2023-05-16 23:24:30Z vboxsync $
4# pylint: disable=invalid-name
5
6"""
7Annotates and generates threaded functions from IEMAllInstructions*.cpp.h.
8"""
9
10from __future__ import print_function;
11
12__copyright__ = \
13"""
14Copyright (C) 2023 Oracle and/or its affiliates.
15
16This file is part of VirtualBox base platform packages, as
17available from https://www.alldomusa.eu.org.
18
19This program is free software; you can redistribute it and/or
20modify it under the terms of the GNU General Public License
21as published by the Free Software Foundation, in version 3 of the
22License.
23
24This program is distributed in the hope that it will be useful, but
25WITHOUT ANY WARRANTY; without even the implied warranty of
26MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27General Public License for more details.
28
29You should have received a copy of the GNU General Public License
30along with this program; if not, see <https://www.gnu.org/licenses>.
31
32SPDX-License-Identifier: GPL-3.0-only
33"""
34__version__ = "$Revision: 99819 $"
35
36# Standard python imports.
37import copy;
38import datetime;
39import os;
40import re;
41import sys;
42import argparse;
43
44import IEMAllInstructionsPython as iai;
45
46
47# Python 3 hacks:
48if sys.version_info[0] >= 3:
49 long = int; # pylint: disable=redefined-builtin,invalid-name
50
51## Number of generic parameters for the thread functions.
52g_kcThreadedParams = 3;
53
54g_kdTypeInfo = {
55 # type name: (cBits, fSigned, C-type )
56 'int8_t': ( 8, True, 'uint8_t', ),
57 'int16_t': ( 16, True, 'int16_t', ),
58 'int32_t': ( 32, True, 'int32_t', ),
59 'int64_t': ( 64, True, 'int64_t', ),
60 'uint4_t': ( 4, False, 'uint8_t', ),
61 'uint8_t': ( 8, False, 'uint8_t', ),
62 'uint16_t': ( 16, False, 'uint16_t', ),
63 'uint32_t': ( 32, False, 'uint32_t', ),
64 'uint64_t': ( 64, False, 'uint64_t', ),
65 'uintptr_t': ( 64, False, 'uintptr_t', ), # ASSUMES 64-bit host pointer size.
66 'bool': ( 1, False, 'bool', ),
67 'IEMMODE': ( 2, False, 'IEMMODE', ),
68};
69
70g_kdIemFieldToType = {
71 # Illegal ones:
72 'offInstrNextByte': ( None, ),
73 'cbInstrBuf': ( None, ),
74 'pbInstrBuf': ( None, ),
75 'uInstrBufPc': ( None, ),
76 'cbInstrBufTotal': ( None, ),
77 'offCurInstrStart': ( None, ),
78 'cbOpcode': ( None, ),
79 'offOpcode': ( None, ),
80 'offModRm': ( None, ),
81 # Okay ones.
82 'fPrefixes': ( 'uint32_t', ),
83 'uRexReg': ( 'uint8_t', ),
84 'uRexB': ( 'uint8_t', ),
85 'uRexIndex': ( 'uint8_t', ),
86 'iEffSeg': ( 'uint8_t', ),
87 'enmEffOpSize': ( 'IEMMODE', ),
88 'enmDefAddrMode': ( 'IEMMODE', ),
89 'enmEffAddrMode': ( 'IEMMODE', ),
90 'enmDefOpSize': ( 'IEMMODE', ),
91 'idxPrefix': ( 'uint8_t', ),
92 'uVex3rdReg': ( 'uint8_t', ),
93 'uVexLength': ( 'uint8_t', ),
94 'fEvexStuff': ( 'uint8_t', ),
95 'uFpuOpcode': ( 'uint16_t', ),
96};
97
98class ThreadedParamRef(object):
99 """
100 A parameter reference for a threaded function.
101 """
102
103 def __init__(self, sOrgRef, sType, oStmt, iParam = None, offParam = 0, sStdRef = None):
104 ## The name / reference in the original code.
105 self.sOrgRef = sOrgRef;
106 ## Normalized name to deal with spaces in macro invocations and such.
107 self.sStdRef = sStdRef if sStdRef else ''.join(sOrgRef.split());
108 ## Indicates that sOrgRef may not match the parameter.
109 self.fCustomRef = sStdRef is not None;
110 ## The type (typically derived).
111 self.sType = sType;
112 ## The statement making the reference.
113 self.oStmt = oStmt;
114 ## The parameter containing the references. None if implicit.
115 self.iParam = iParam;
116 ## The offset in the parameter of the reference.
117 self.offParam = offParam;
118
119 ## The variable name in the threaded function.
120 self.sNewName = 'x';
121 ## The this is packed into.
122 self.iNewParam = 99;
123 ## The bit offset in iNewParam.
124 self.offNewParam = 1024
125
126
127class ThreadedFunctionVariation(object):
128 """ Threaded function variation. """
129
130 ## @name Variations.
131 ## These variations will match translation block selection/distinctions as well.
132 ## @note Effective operand size is generally handled in the decoder, at present
133 ## we only do variations on addressing and memory accessing.
134 ## @todo Blocks without addressing should have 64-bit and 32-bit PC update
135 ## variations to reduce code size (see iemRegAddToRip).
136 ## @{
137 ksVariation_Default = ''; ##< No variations.
138 ksVariation_Addr16 = '_Addr16'; ##< 16-bit addressing mode.
139 ksVariation_Addr32 = '_Addr32'; ##< 32-bit addressing mode.
140 ksVariation_Addr32Flat = '_Addr32Flat'; ##< 32-bit addressing mode with CS, DS, ES and SS flat and 4GB wide.
141 ksVariation_Addr64 = '_Addr64'; ##< 64-bit addressing mode.
142 ksVariation_Addr64_32 = '_Addr6432'; ##< 32-bit addressing in 64-bit mode.
143 kasVariations_EffAddr = (
144 ksVariation_Addr16, ksVariation_Addr32, ksVariation_Addr32Flat, ksVariation_Addr64, ksVariation_Addr64_32
145 );
146 ## @}
147
148 def __init__(self, oThreadedFunction, sVariation = ksVariation_Default):
149 self.oParent = oThreadedFunction # type: ThreadedFunction
150 ##< ksVariation_Xxxx.
151 self.sVariation = sVariation
152
153 ## Threaded function parameter references.
154 self.aoParamRefs = [] # type: list(ThreadedParamRef)
155 ## Unique parameter references.
156 self.dParamRefs = {} # type: dict(str,list(ThreadedParamRef))
157 ## Minimum number of parameters to the threaded function.
158 self.cMinParams = 0;
159
160 ## List/tree of statements for the threaded function.
161 self.aoStmtsForThreadedFunction = [] # type: list(McStmt)
162
163 def getIndexName(self):
164 sName = self.oParent.oMcBlock.sFunction;
165 if sName.startswith('iemOp_'):
166 sName = sName[len('iemOp_'):];
167 if self.oParent.oMcBlock.iInFunction == 0:
168 return 'kIemThreadedFunc_%s%s' % ( sName, self.sVariation, );
169 return 'kIemThreadedFunc_%s_%s%s' % ( sName, self.oParent.oMcBlock.iInFunction, self.sVariation, );
170
171 def getFunctionName(self):
172 sName = self.oParent.oMcBlock.sFunction;
173 if sName.startswith('iemOp_'):
174 sName = sName[len('iemOp_'):];
175 if self.oParent.oMcBlock.iInFunction == 0:
176 return 'iemThreadedFunc_%s%s' % ( sName, self.sVariation, );
177 return 'iemThreadedFunc_%s_%s%s' % ( sName, self.oParent.oMcBlock.iInFunction, self.sVariation, );
178
179 #
180 # Analysis and code morphing.
181 #
182
183 def raiseProblem(self, sMessage):
184 """ Raises a problem. """
185 self.oParent.raiseProblem(sMessage);
186
187 def analyzeReferenceToType(self, sRef):
188 """
189 Translates a variable or structure reference to a type.
190 Returns type name.
191 Raises exception if unable to figure it out.
192 """
193 ch0 = sRef[0];
194 if ch0 == 'u':
195 if sRef.startswith('u32'):
196 return 'uint32_t';
197 if sRef.startswith('u8') or sRef == 'uReg':
198 return 'uint8_t';
199 if sRef.startswith('u64'):
200 return 'uint64_t';
201 if sRef.startswith('u16'):
202 return 'uint16_t';
203 elif ch0 == 'b':
204 return 'uint8_t';
205 elif ch0 == 'f':
206 return 'bool';
207 elif ch0 == 'i':
208 if sRef.startswith('i8'):
209 return 'int8_t';
210 if sRef.startswith('i16'):
211 return 'int32_t';
212 if sRef.startswith('i32'):
213 return 'int32_t';
214 if sRef.startswith('i64'):
215 return 'int64_t';
216 if sRef in ('iReg', 'iGReg', 'iSegReg', 'iSrcReg', 'iDstReg'):
217 return 'uint8_t';
218 elif ch0 == 'p':
219 if sRef.find('-') < 0:
220 return 'uintptr_t';
221 if sRef.startswith('pVCpu->iem.s.'):
222 sField = sRef[len('pVCpu->iem.s.') : ];
223 if sField in g_kdIemFieldToType:
224 if g_kdIemFieldToType[sField][0]:
225 return g_kdIemFieldToType[sField][0];
226 self.raiseProblem('Reference out-of-bounds decoder field: %s' % (sRef,));
227 elif ch0 == 'G' and sRef.startswith('GCPtr'):
228 return 'uint64_t';
229 elif ch0 == 'e':
230 if sRef == 'enmEffOpSize':
231 return 'IEMMODE';
232 elif sRef == 'cShift': ## @todo risky
233 return 'uint8_t';
234
235 self.raiseProblem('Unknown reference: %s' % (sRef,));
236 return None; # Shut up pylint 2.16.2.
237
238 def analyzeCallToType(self, sFnRef):
239 """
240 Determins the type of an indirect function call.
241 """
242 assert sFnRef[0] == 'p';
243
244 #
245 # Simple?
246 #
247 if sFnRef.find('-') < 0:
248 oDecoderFunction = self.oParent.oMcBlock.oFunction;
249
250 # Try the argument list of the function defintion macro invocation first.
251 iArg = 2;
252 while iArg < len(oDecoderFunction.asDefArgs):
253 if sFnRef == oDecoderFunction.asDefArgs[iArg]:
254 return oDecoderFunction.asDefArgs[iArg - 1];
255 iArg += 1;
256
257 # Then check out line that includes the word and looks like a variable declaration.
258 oRe = re.compile(' +(P[A-Z0-9_]+|const +IEMOP[A-Z0-9_]+ *[*]) +(const |) *' + sFnRef + ' *(;|=)');
259 for sLine in oDecoderFunction.asLines:
260 oMatch = oRe.match(sLine);
261 if oMatch:
262 if not oMatch.group(1).startswith('const'):
263 return oMatch.group(1);
264 return 'PC' + oMatch.group(1)[len('const ') : -1].strip();
265
266 #
267 # Deal with the pImpl->pfnXxx:
268 #
269 elif sFnRef.startswith('pImpl->pfn'):
270 sMember = sFnRef[len('pImpl->') : ];
271 sBaseType = self.analyzeCallToType('pImpl');
272 offBits = sMember.rfind('U') + 1;
273 if sBaseType == 'PCIEMOPBINSIZES': return 'PFNIEMAIMPLBINU' + sMember[offBits:];
274 if sBaseType == 'PCIEMOPUNARYSIZES': return 'PFNIEMAIMPLUNARYU' + sMember[offBits:];
275 if sBaseType == 'PCIEMOPSHIFTSIZES': return 'PFNIEMAIMPLSHIFTU' + sMember[offBits:];
276 if sBaseType == 'PCIEMOPSHIFTDBLSIZES': return 'PFNIEMAIMPLSHIFTDBLU' + sMember[offBits:];
277 if sBaseType == 'PCIEMOPMULDIVSIZES': return 'PFNIEMAIMPLMULDIVU' + sMember[offBits:];
278 if sBaseType == 'PCIEMOPMEDIAF3': return 'PFNIEMAIMPLMEDIAF3U' + sMember[offBits:];
279 if sBaseType == 'PCIEMOPMEDIAOPTF3': return 'PFNIEMAIMPLMEDIAOPTF3U' + sMember[offBits:];
280 if sBaseType == 'PCIEMOPMEDIAOPTF2': return 'PFNIEMAIMPLMEDIAOPTF2U' + sMember[offBits:];
281 if sBaseType == 'PCIEMOPMEDIAOPTF3IMM8': return 'PFNIEMAIMPLMEDIAOPTF3U' + sMember[offBits:] + 'IMM8';
282 if sBaseType == 'PCIEMOPBLENDOP': return 'PFNIEMAIMPLAVXBLENDU' + sMember[offBits:];
283
284 self.raiseProblem('Unknown call reference: %s::%s (%s)' % (sBaseType, sMember, sFnRef,));
285
286 self.raiseProblem('Unknown call reference: %s' % (sFnRef,));
287 return None; # Shut up pylint 2.16.2.
288
289 def analyze8BitGRegStmt(self, oStmt):
290 """
291 Gets the 8-bit general purpose register access details of the given statement.
292 ASSUMES the statement is one accessing an 8-bit GREG.
293 """
294 idxReg = 0;
295 if ( oStmt.sName.find('_FETCH_') > 0
296 or oStmt.sName.find('_REF_') > 0
297 or oStmt.sName.find('_TO_LOCAL') > 0):
298 idxReg = 1;
299
300 sRegRef = oStmt.asParams[idxReg];
301 sOrgExpr = '((%s) < 4 || (pVCpu->iem.s.fPrefixes & IEM_OP_PRF_REX) ? (%s) : (%s) | 16)' % (sRegRef, sRegRef, sRegRef);
302
303 if sRegRef.find('IEM_GET_MODRM_RM') > 0: sStdRef = 'bRmRm8Ex';
304 elif sRegRef.find('IEM_GET_MODRM_REG') > 0: sStdRef = 'bRmReg8Ex';
305 else: sStdRef = 'bOther8Ex';
306
307 return (idxReg, sOrgExpr, sStdRef);
308
309
310 def analyzeMorphStmtForThreaded(self, aoStmts, iParamRef = 0):
311 """
312 Transforms (copy) the statements into those for the threaded function.
313
314 Returns list/tree of statements (aoStmts is not modified) and the new
315 iParamRef value.
316 """
317 #
318 # We'll be traversing aoParamRefs in parallel to the statements, so we
319 # must match the traversal in analyzeFindThreadedParamRefs exactly.
320 #
321 #print('McBlock at %s:%s' % (os.path.split(self.oMcBlock.sSrcFile)[1], self.oMcBlock.iBeginLine,));
322 aoThreadedStmts = [];
323 for oStmt in aoStmts:
324 # Skip C++ statements that is purely related to decoding.
325 if not oStmt.isCppStmt() or not oStmt.fDecode:
326 # Copy the statement. Make a deep copy to make sure we've got our own
327 # copies of all instance variables, even if a bit overkill at the moment.
328 oNewStmt = copy.deepcopy(oStmt);
329 aoThreadedStmts.append(oNewStmt);
330 #print('oNewStmt %s %s' % (oNewStmt.sName, len(oNewStmt.asParams),));
331
332 # If the statement has parameter references, process the relevant parameters.
333 # We grab the references relevant to this statement and apply them in reserve order.
334 if iParamRef < len(self.aoParamRefs) and self.aoParamRefs[iParamRef].oStmt == oStmt:
335 iParamRefFirst = iParamRef;
336 while True:
337 iParamRef += 1;
338 if iParamRef >= len(self.aoParamRefs) or self.aoParamRefs[iParamRef].oStmt != oStmt:
339 break;
340
341 #print('iParamRefFirst=%s iParamRef=%s' % (iParamRefFirst, iParamRef));
342 for iCurRef in range(iParamRef - 1, iParamRefFirst - 1, -1):
343 oCurRef = self.aoParamRefs[iCurRef];
344 if oCurRef.iParam is not None:
345 assert oCurRef.oStmt == oStmt;
346 #print('iCurRef=%s iParam=%s sOrgRef=%s' % (iCurRef, oCurRef.iParam, oCurRef.sOrgRef));
347 sSrcParam = oNewStmt.asParams[oCurRef.iParam];
348 assert ( sSrcParam[oCurRef.offParam : oCurRef.offParam + len(oCurRef.sOrgRef)] == oCurRef.sOrgRef
349 or oCurRef.fCustomRef), \
350 'offParam=%s sOrgRef=%s iParam=%s oStmt.sName=%s sSrcParam=%s<eos>' \
351 % (oCurRef.offParam, oCurRef.sOrgRef, oCurRef.iParam, oStmt.sName, sSrcParam);
352 oNewStmt.asParams[oCurRef.iParam] = sSrcParam[0 : oCurRef.offParam] \
353 + oCurRef.sNewName \
354 + sSrcParam[oCurRef.offParam + len(oCurRef.sOrgRef) : ];
355
356 # Morph IEM_MC_CALC_RM_EFF_ADDR into IEM_MC_CALC_RM_EFF_ADDR_THREADED ...
357 if oNewStmt.sName == 'IEM_MC_CALC_RM_EFF_ADDR':
358 assert self.sVariation != self.ksVariation_Default;
359 oNewStmt.sName = 'IEM_MC_CALC_RM_EFF_ADDR_THREADED' + self.sVariation.upper();
360 assert len(oNewStmt.asParams) == 3;
361 if self.sVariation == self.ksVariation_Addr16:
362 oNewStmt.asParams = [
363 oNewStmt.asParams[0], oNewStmt.asParams[1], self.dParamRefs['u16Disp'][0].sNewName,
364 ];
365 elif self.sVariation in (self.ksVariation_Addr32, self.ksVariation_Addr32Flat):
366 oNewStmt.asParams = [
367 oNewStmt.asParams[0], oNewStmt.asParams[1], self.dParamRefs['bSib'][0].sNewName,
368 self.dParamRefs['u32Disp'][0].sNewName,
369 ];
370 else:
371 oNewStmt.asParams = [
372 oNewStmt.asParams[0], self.dParamRefs['bRmEx'][0].sNewName, self.dParamRefs['bSib'][0].sNewName,
373 self.dParamRefs['u32Disp'][0].sNewName, self.dParamRefs['cbInstr'][0].sNewName,
374 ];
375 # ... and IEM_MC_ADVANCE_RIP_AND_FINISH into *_THREADED and maybe *_LM64/_NOT64 ...
376 elif oNewStmt.sName in ('IEM_MC_ADVANCE_RIP_AND_FINISH', 'IEM_MC_REL_JMP_S8_AND_FINISH',
377 'IEM_MC_REL_JMP_S16_AND_FINISH', 'IEM_MC_REL_JMP_S32_AND_FINISH'):
378 oNewStmt.asParams.append(self.dParamRefs['cbInstr'][0].sNewName);
379 if oNewStmt.sName in ('IEM_MC_REL_JMP_S8_AND_FINISH', 'IEM_MC_REL_JMP_S32_AND_FINISH'):
380 oNewStmt.asParams.append(self.dParamRefs['pVCpu->iem.s.enmEffOpSize'][0].sNewName);
381 oNewStmt.sName += '_THREADED';
382 if self.sVariation in (self.ksVariation_Addr64, self.ksVariation_Addr64_32):
383 oNewStmt.sName += '_LM64';
384 elif self.sVariation != self.ksVariation_Default:
385 oNewStmt.sName += '_NOT64';
386
387 # ... and IEM_MC_*_GREG_U8 into *_THREADED w/ reworked index taking REX into account
388 elif oNewStmt.sName.startswith('IEM_MC_') and oNewStmt.sName.find('_GREG_U8') > 0:
389 (idxReg, _, sStdRef) = self.analyze8BitGRegStmt(oNewStmt);
390 oNewStmt.asParams[idxReg] = self.dParamRefs[sStdRef][0].sNewName;
391 oNewStmt.sName += '_THREADED';
392
393 # ... and IEM_MC_CALL_CIMPL_[0-5] into *_THREADED ...
394 elif oNewStmt.sName.startswith('IEM_MC_CALL_CIMPL_'):
395 oNewStmt.sName += '_THREADED';
396 oNewStmt.asParams.insert(0, self.dParamRefs['cbInstr'][0].sNewName);
397
398 # Process branches of conditionals recursively.
399 if isinstance(oStmt, iai.McStmtCond):
400 (oNewStmt.aoIfBranch, iParamRef) = self.analyzeMorphStmtForThreaded(oStmt.aoIfBranch, iParamRef);
401 if oStmt.aoElseBranch:
402 (oNewStmt.aoElseBranch, iParamRef) = self.analyzeMorphStmtForThreaded(oStmt.aoElseBranch, iParamRef);
403
404 return (aoThreadedStmts, iParamRef);
405
406 def analyzeConsolidateThreadedParamRefs(self):
407 """
408 Consolidate threaded function parameter references into a dictionary
409 with lists of the references to each variable/field.
410 """
411 # Gather unique parameters.
412 self.dParamRefs = {};
413 for oRef in self.aoParamRefs:
414 if oRef.sStdRef not in self.dParamRefs:
415 self.dParamRefs[oRef.sStdRef] = [oRef,];
416 else:
417 self.dParamRefs[oRef.sStdRef].append(oRef);
418
419 # Generate names for them for use in the threaded function.
420 dParamNames = {};
421 for sName, aoRefs in self.dParamRefs.items():
422 # Morph the reference expression into a name.
423 if sName.startswith('IEM_GET_MODRM_REG'): sName = 'bModRmRegP';
424 elif sName.startswith('IEM_GET_MODRM_RM'): sName = 'bModRmRmP';
425 elif sName.startswith('IEM_GET_MODRM_REG_8'): sName = 'bModRmReg8P';
426 elif sName.startswith('IEM_GET_MODRM_RM_8'): sName = 'bModRmRm8P';
427 elif sName.startswith('IEM_GET_EFFECTIVE_VVVV'): sName = 'bEffVvvvP';
428 elif sName.find('.') >= 0 or sName.find('->') >= 0:
429 sName = sName[max(sName.rfind('.'), sName.rfind('>')) + 1 : ] + 'P';
430 else:
431 sName += 'P';
432
433 # Ensure it's unique.
434 if sName in dParamNames:
435 for i in range(10):
436 if sName + str(i) not in dParamNames:
437 sName += str(i);
438 break;
439 dParamNames[sName] = True;
440
441 # Update all the references.
442 for oRef in aoRefs:
443 oRef.sNewName = sName;
444
445 # Organize them by size too for the purpose of optimize them.
446 dBySize = {} # type: dict(str,str)
447 for sStdRef, aoRefs in self.dParamRefs.items():
448 if aoRefs[0].sType[0] != 'P':
449 cBits = g_kdTypeInfo[aoRefs[0].sType][0];
450 assert(cBits <= 64);
451 else:
452 cBits = 64;
453
454 if cBits not in dBySize:
455 dBySize[cBits] = [sStdRef,]
456 else:
457 dBySize[cBits].append(sStdRef);
458
459 # Pack the parameters as best as we can, starting with the largest ones
460 # and ASSUMING a 64-bit parameter size.
461 self.cMinParams = 0;
462 offNewParam = 0;
463 for cBits in sorted(dBySize.keys(), reverse = True):
464 for sStdRef in dBySize[cBits]:
465 if offNewParam == 0 or offNewParam + cBits > 64:
466 self.cMinParams += 1;
467 offNewParam = cBits;
468 else:
469 offNewParam += cBits;
470 assert(offNewParam <= 64);
471
472 for oRef in self.dParamRefs[sStdRef]:
473 oRef.iNewParam = self.cMinParams - 1;
474 oRef.offNewParam = offNewParam - cBits;
475
476 # Currently there are a few that requires 4 parameters, list these so we can figure out why:
477 if self.cMinParams >= 4:
478 print('debug: cMinParams=%s cRawParams=%s - %s:%d'
479 % (self.cMinParams, len(self.dParamRefs), self.oParent.oMcBlock.sSrcFile, self.oParent.oMcBlock.iBeginLine,));
480
481 return True;
482
483 ksHexDigits = '0123456789abcdefABCDEF';
484
485 def analyzeFindThreadedParamRefs(self, aoStmts):
486 """
487 Scans the statements for things that have to passed on to the threaded
488 function (populates self.aoParamRefs).
489 """
490 for oStmt in aoStmts:
491 # Some statements we can skip alltogether.
492 if isinstance(oStmt, iai.McCppPreProc):
493 continue;
494 if oStmt.isCppStmt() and oStmt.fDecode:
495 continue;
496
497 if isinstance(oStmt, iai.McStmtVar):
498 if oStmt.sConstValue is None:
499 continue;
500 aiSkipParams = { 0: True, 1: True, 3: True };
501 else:
502 aiSkipParams = {};
503
504 # Several statements have implicit parameters and some have different parameters.
505 if oStmt.sName in ('IEM_MC_ADVANCE_RIP_AND_FINISH', 'IEM_MC_REL_JMP_S8_AND_FINISH', 'IEM_MC_REL_JMP_S16_AND_FINISH',
506 'IEM_MC_REL_JMP_S32_AND_FINISH', 'IEM_MC_CALL_CIMPL_0', 'IEM_MC_CALL_CIMPL_1',
507 'IEM_MC_CALL_CIMPL_2', 'IEM_MC_CALL_CIMPL_3', 'IEM_MC_CALL_CIMPL_4', 'IEM_MC_CALL_CIMPL_5', ):
508 self.aoParamRefs.append(ThreadedParamRef('IEM_GET_INSTR_LEN(pVCpu)', 'uint4_t', oStmt, sStdRef = 'cbInstr'));
509
510 if oStmt.sName in ('IEM_MC_REL_JMP_S8_AND_FINISH', 'IEM_MC_REL_JMP_S32_AND_FINISH'):
511 self.aoParamRefs.append(ThreadedParamRef('pVCpu->iem.s.enmEffOpSize', 'IEMMODE', oStmt));
512
513 if oStmt.sName == 'IEM_MC_CALC_RM_EFF_ADDR':
514 # This is being pretty presumptive about bRm always being the RM byte...
515 if self.sVariation == self.ksVariation_Addr16:
516 self.aoParamRefs.append(ThreadedParamRef('bRm', 'uint8_t', oStmt));
517 self.aoParamRefs.append(ThreadedParamRef('(uint16_t)uEffAddrInfo' ,
518 'uint16_t', oStmt, sStdRef = 'u16Disp'));
519 elif self.sVariation in (self.ksVariation_Addr32, self.ksVariation_Addr32Flat):
520 self.aoParamRefs.append(ThreadedParamRef('bRm', 'uint8_t', oStmt));
521 self.aoParamRefs.append(ThreadedParamRef('(uint8_t)(uEffAddrInfo >> 32)',
522 'uint8_t', oStmt, sStdRef = 'bSib'));
523 self.aoParamRefs.append(ThreadedParamRef('(uint32_t)uEffAddrInfo',
524 'uint32_t', oStmt, sStdRef = 'u32Disp'));
525 else:
526 assert self.sVariation in (self.ksVariation_Addr64, self.ksVariation_Addr64_32);
527 self.aoParamRefs.append(ThreadedParamRef('IEM_GET_MODRM_EX(pVCpu, bRm)',
528 'uint8_t', oStmt, sStdRef = 'bRmEx'));
529 self.aoParamRefs.append(ThreadedParamRef('(uint8_t)(uEffAddrInfo >> 32)',
530 'uint8_t', oStmt, sStdRef = 'bSib'));
531 self.aoParamRefs.append(ThreadedParamRef('(uint32_t)uEffAddrInfo',
532 'uint32_t', oStmt, sStdRef = 'u32Disp'));
533 self.aoParamRefs.append(ThreadedParamRef('IEM_GET_INSTR_LEN(pVCpu)',
534 'uint4_t', oStmt, sStdRef = 'cbInstr'));
535 assert len(oStmt.asParams) == 3;
536 assert oStmt.asParams[1].startswith('bRm');
537 aiSkipParams[1] = True; # Skip the bRm parameter
538
539 # 8-bit register accesses needs to have their index argument reworked to take REX into account.
540 if oStmt.sName.startswith('IEM_MC_') and oStmt.sName.find('_GREG_U8') > 0:
541 (idxReg, sOrgRef, sStdRef) = self.analyze8BitGRegStmt(oStmt);
542 self.aoParamRefs.append(ThreadedParamRef(sOrgRef, 'uint4_t', oStmt, idxReg, sStdRef = sStdRef));
543 aiSkipParams[idxReg] = True; # Skip the parameter below.
544
545 # Inspect the target of calls to see if we need to pass down a
546 # function pointer or function table pointer for it to work.
547 if isinstance(oStmt, iai.McStmtCall):
548 if oStmt.sFn[0] == 'p':
549 self.aoParamRefs.append(ThreadedParamRef(oStmt.sFn, self.analyzeCallToType(oStmt.sFn), oStmt, oStmt.idxFn));
550 elif ( oStmt.sFn[0] != 'i'
551 and not oStmt.sFn.startswith('IEMTARGETCPU_EFL_BEHAVIOR_SELECT')
552 and not oStmt.sFn.startswith('IEM_SELECT_HOST_OR_FALLBACK') ):
553 self.raiseProblem('Bogus function name in %s: %s' % (oStmt.sName, oStmt.sFn,));
554 aiSkipParams[oStmt.idxFn] = True;
555
556 # Check all the parameters for bogus references.
557 for iParam, sParam in enumerate(oStmt.asParams):
558 if iParam not in aiSkipParams and sParam not in self.oParent.dVariables:
559 # The parameter may contain a C expression, so we have to try
560 # extract the relevant bits, i.e. variables and fields while
561 # ignoring operators and parentheses.
562 offParam = 0;
563 while offParam < len(sParam):
564 # Is it the start of an C identifier? If so, find the end, but don't stop on field separators (->, .).
565 ch = sParam[offParam];
566 if ch.isalpha() or ch == '_':
567 offStart = offParam;
568 offParam += 1;
569 while offParam < len(sParam):
570 ch = sParam[offParam];
571 if not ch.isalnum() and ch != '_' and ch != '.':
572 if ch != '-' or sParam[offParam + 1] != '>':
573 # Special hack for the 'CTX_SUFF(pVM)' bit in pVCpu->CTX_SUFF(pVM)->xxxx:
574 if ( ch == '('
575 and sParam[offStart : offParam + len('(pVM)->')] == 'pVCpu->CTX_SUFF(pVM)->'):
576 offParam += len('(pVM)->') - 1;
577 else:
578 break;
579 offParam += 1;
580 offParam += 1;
581 sRef = sParam[offStart : offParam];
582
583 # For register references, we pass the full register indexes instead as macros
584 # like IEM_GET_MODRM_REG implicitly references pVCpu->iem.s.uRexReg and the
585 # threaded function will be more efficient if we just pass the register index
586 # as a 4-bit param.
587 if ( sRef.startswith('IEM_GET_MODRM')
588 or sRef.startswith('IEM_GET_EFFECTIVE_VVVV') ):
589 offParam = iai.McBlock.skipSpacesAt(sParam, offParam, len(sParam));
590 if sParam[offParam] != '(':
591 self.raiseProblem('Expected "(" following %s in "%s"' % (sRef, oStmt.renderCode(),));
592 (asMacroParams, offCloseParam) = iai.McBlock.extractParams(sParam, offParam);
593 if asMacroParams is None:
594 self.raiseProblem('Unable to find ")" for %s in "%s"' % (sRef, oStmt.renderCode(),));
595 offParam = offCloseParam + 1;
596 self.aoParamRefs.append(ThreadedParamRef(sParam[offStart : offParam], 'uint8_t',
597 oStmt, iParam, offStart));
598
599 # We can skip known variables.
600 elif sRef in self.oParent.dVariables:
601 pass;
602
603 # Skip certain macro invocations.
604 elif sRef in ('IEM_GET_HOST_CPU_FEATURES',
605 'IEM_GET_GUEST_CPU_FEATURES',
606 'IEM_IS_GUEST_CPU_AMD'):
607 offParam = iai.McBlock.skipSpacesAt(sParam, offParam, len(sParam));
608 if sParam[offParam] != '(':
609 self.raiseProblem('Expected "(" following %s in "%s"' % (sRef, oStmt.renderCode(),));
610 (asMacroParams, offCloseParam) = iai.McBlock.extractParams(sParam, offParam);
611 if asMacroParams is None:
612 self.raiseProblem('Unable to find ")" for %s in "%s"' % (sRef, oStmt.renderCode(),));
613 offParam = offCloseParam + 1;
614
615 # Skip any dereference following it, unless it's a predicate like IEM_IS_GUEST_CPU_AMD.
616 if sRef not in ('IEM_IS_GUEST_CPU_AMD', ):
617 offParam = iai.McBlock.skipSpacesAt(sParam, offParam, len(sParam));
618 if offParam + 2 <= len(sParam) and sParam[offParam : offParam + 2] == '->':
619 offParam = iai.McBlock.skipSpacesAt(sParam, offParam + 2, len(sParam));
620 while offParam < len(sParam) and (sParam[offParam].isalnum() or sParam[offParam] in '_.'):
621 offParam += 1;
622
623 # Skip constants, globals, types (casts), sizeof and macros.
624 elif ( sRef.startswith('IEM_OP_PRF_')
625 or sRef.startswith('IEM_ACCESS_')
626 or sRef.startswith('IEMINT_')
627 or sRef.startswith('X86_GREG_')
628 or sRef.startswith('X86_SREG_')
629 or sRef.startswith('X86_EFL_')
630 or sRef.startswith('X86_FSW_')
631 or sRef.startswith('X86_FCW_')
632 or sRef.startswith('X86_XCPT_')
633 or sRef.startswith('IEMMODE_')
634 or sRef.startswith('g_')
635 or sRef in ( 'int8_t', 'int16_t', 'int32_t',
636 'INT8_C', 'INT16_C', 'INT32_C', 'INT64_C',
637 'UINT8_C', 'UINT16_C', 'UINT32_C', 'UINT64_C',
638 'UINT8_MAX', 'UINT16_MAX', 'UINT32_MAX', 'UINT64_MAX',
639 'INT8_MAX', 'INT16_MAX', 'INT32_MAX', 'INT64_MAX',
640 'INT8_MIN', 'INT16_MIN', 'INT32_MIN', 'INT64_MIN',
641 'sizeof', 'NOREF', 'RT_NOREF', 'IEMMODE_64BIT',
642 'NIL_RTGCPTR' ) ):
643 pass;
644
645 # Skip certain macro invocations.
646 # Any variable (non-field) and decoder fields in IEMCPU will need to be parameterized.
647 elif ( ( '.' not in sRef
648 and '-' not in sRef
649 and sRef not in ('pVCpu', ) )
650 or iai.McBlock.koReIemDecoderVars.search(sRef) is not None):
651 self.aoParamRefs.append(ThreadedParamRef(sRef, self.analyzeReferenceToType(sRef),
652 oStmt, iParam, offStart));
653 # Number.
654 elif ch.isdigit():
655 if ( ch == '0'
656 and offParam + 2 <= len(sParam)
657 and sParam[offParam + 1] in 'xX'
658 and sParam[offParam + 2] in self.ksHexDigits ):
659 offParam += 2;
660 while offParam < len(sParam) and sParam[offParam] in self.ksHexDigits:
661 offParam += 1;
662 else:
663 while offParam < len(sParam) and sParam[offParam].isdigit():
664 offParam += 1;
665 # Whatever else.
666 else:
667 offParam += 1;
668
669 # Traverse the branches of conditionals.
670 if isinstance(oStmt, iai.McStmtCond):
671 self.analyzeFindThreadedParamRefs(oStmt.aoIfBranch);
672 self.analyzeFindThreadedParamRefs(oStmt.aoElseBranch);
673 return True;
674
675 def analyzeVariation(self, aoStmts):
676 """
677 2nd part of the analysis, done on each variation.
678
679 The variations may differ in parameter requirements and will end up with
680 slightly different MC sequences. Thus this is done on each individually.
681
682 Returns dummy True - raises exception on trouble.
683 """
684 # Now scan the code for variables and field references that needs to
685 # be passed to the threaded function because they are related to the
686 # instruction decoding.
687 self.analyzeFindThreadedParamRefs(aoStmts);
688 self.analyzeConsolidateThreadedParamRefs();
689
690 # Morph the statement stream for the block into what we'll be using in the threaded function.
691 (self.aoStmtsForThreadedFunction, iParamRef) = self.analyzeMorphStmtForThreaded(aoStmts);
692 if iParamRef != len(self.aoParamRefs):
693 raise Exception('iParamRef=%s, expected %s!' % (iParamRef, len(self.aoParamRefs),));
694
695 return True;
696
697 def emitThreadedCallStmt(self, cchIndent):
698 """
699 Produces a generic C++ statment that emits a call to the thread function variation.
700 """
701 sCode = ' ' * cchIndent;
702 sCode += 'IEM_MC2_EMIT_CALL_%s(%s' % (self.cMinParams, self.getIndexName(), );
703 for iParam in range(self.cMinParams):
704 asFrags = [];
705 for aoRefs in self.dParamRefs.values():
706 oRef = aoRefs[0];
707 if oRef.iNewParam == iParam:
708 if oRef.offNewParam == 0:
709 asFrags.append('(uint64_t)(' + oRef.sOrgRef + ')');
710 else:
711 asFrags.append('((uint64_t)(%s) << %s)' % (oRef.sOrgRef, oRef.offNewParam));
712 assert asFrags;
713 sCode += ', ' + ' | '.join(asFrags);
714 sCode += ');';
715 return iai.McCppGeneric(sCode);
716
717
718class ThreadedFunction(object):
719 """
720 A threaded function.
721 """
722
723 def __init__(self, oMcBlock):
724 self.oMcBlock = oMcBlock # type: IEMAllInstructionsPython.McBlock
725 ## Variations for this block. There is at least one.
726 self.aoVariations = [] # type: list(ThreadedFunctionVariation)
727 ## Dictionary of local variables (IEM_MC_LOCAL[_CONST]) and call arguments (IEM_MC_ARG*).
728 self.dVariables = {} # type: dict(str,McStmtVar)
729
730 @staticmethod
731 def dummyInstance():
732 """ Gets a dummy instance. """
733 return ThreadedFunction(iai.McBlock('null', 999999999, 999999999,
734 iai.DecoderFunction('null', 999999999, 'nil', ('','')), 999999999));
735
736 def raiseProblem(self, sMessage):
737 """ Raises a problem. """
738 raise Exception('%s:%s: error: %s' % (self.oMcBlock.sSrcFile, self.oMcBlock.iBeginLine, sMessage, ));
739
740 def analyzeFindVariablesAndCallArgs(self, aoStmts):
741 """ Scans the statements for MC variables and call arguments. """
742 for oStmt in aoStmts:
743 if isinstance(oStmt, iai.McStmtVar):
744 if oStmt.sVarName in self.dVariables:
745 raise Exception('Variable %s is defined more than once!' % (oStmt.sVarName,));
746 self.dVariables[oStmt.sVarName] = oStmt.sVarName;
747
748 # There shouldn't be any variables or arguments declared inside if/
749 # else blocks, but scan them too to be on the safe side.
750 if isinstance(oStmt, iai.McStmtCond):
751 cBefore = len(self.dVariables);
752 self.analyzeFindVariablesAndCallArgs(oStmt.aoIfBranch);
753 self.analyzeFindVariablesAndCallArgs(oStmt.aoElseBranch);
754 if len(self.dVariables) != cBefore:
755 raise Exception('Variables/arguments defined in conditional branches!');
756 return True;
757
758 def analyze(self):
759 """
760 Analyzes the code, identifying the number of parameters it requires and such.
761
762 Returns dummy True - raises exception on trouble.
763 """
764
765 # Decode the block into a list/tree of McStmt objects.
766 aoStmts = self.oMcBlock.decode();
767
768 # Scan the statements for local variables and call arguments (self.dVariables).
769 self.analyzeFindVariablesAndCallArgs(aoStmts);
770
771 # Create variations if needed.
772 if iai.McStmt.findStmtByNames(aoStmts, {'IEM_MC_CALC_RM_EFF_ADDR' : True,}):
773 self.aoVariations = [ThreadedFunctionVariation(self, sVar)
774 for sVar in ThreadedFunctionVariation.kasVariations_EffAddr];
775 else:
776 self.aoVariations = [ThreadedFunctionVariation(self),];
777
778 # Continue the analysis on each variation.
779 for oVariation in self.aoVariations:
780 oVariation.analyzeVariation(aoStmts);
781
782 return True;
783
784 def emitThreadedCallStmts(self):
785 """
786 Worker for morphInputCode that returns a list of statements that emits
787 the call to the threaded functions for the block.
788 """
789 # Special case for only default variation:
790 if len(self.aoVariations) == 1:
791 assert self.aoVariations[0].sVariation == ThreadedFunctionVariation.ksVariation_Default;
792 return [self.aoVariations[0].emitThreadedCallStmt(0),];
793
794 # Currently only have variations for address mode.
795 dByVariation = { oVar.sVariation: oVar for oVar in self.aoVariations };
796 aoStmts = [
797 iai.McCppGeneric('switch (pVCpu->iem.s.enmCpuMode | (pVCpu->iem.s.enmEffAddrMode << 2))'),
798 iai.McCppGeneric('{'),
799 ];
800 if ThreadedFunctionVariation.ksVariation_Addr64 in dByVariation:
801 aoStmts.extend([
802 iai.McCppGeneric(' case IEMMODE_64BIT | (IEMMODE_64BIT << 2):'),
803 dByVariation[ThreadedFunctionVariation.ksVariation_Addr64].emitThreadedCallStmt(8),
804 iai.McCppGeneric(' break;'),
805 ]);
806 if ( ThreadedFunctionVariation.ksVariation_Addr32 in dByVariation
807 or ThreadedFunctionVariation.ksVariation_Addr32Flat in dByVariation):
808 aoStmts.append(iai.McCppGeneric(' case IEMMODE_32BIT | (IEMMODE_32BIT << 2):'));
809 if ThreadedFunctionVariation.ksVariation_Addr32Flat in dByVariation:
810 aoStmts.extend([
811 iai.McCppGeneric(' if (false /** @todo */)'),
812 dByVariation[ThreadedFunctionVariation.ksVariation_Addr32Flat].emitThreadedCallStmt(12),
813 iai.McCppGeneric(' RT_FALL_THRU();'),
814 ]);
815 aoStmts.extend([
816 iai.McCppGeneric(' case IEMMODE_16BIT | (IEMMODE_32BIT << 2):'),
817 dByVariation[ThreadedFunctionVariation.ksVariation_Addr32].emitThreadedCallStmt(8),
818 iai.McCppGeneric(' break;'),
819 ]);
820 if ThreadedFunctionVariation.ksVariation_Addr16 in dByVariation:
821 aoStmts.extend([
822 iai.McCppGeneric(' case IEMMODE_16BIT | (IEMMODE_16BIT << 2):'),
823 iai.McCppGeneric(' case IEMMODE_32BIT | (IEMMODE_16BIT << 2):'),
824 dByVariation[ThreadedFunctionVariation.ksVariation_Addr16].emitThreadedCallStmt(8),
825 iai.McCppGeneric(' break;'),
826 ]);
827 if ThreadedFunctionVariation.ksVariation_Addr64_32 in dByVariation:
828 aoStmts.extend([
829 iai.McCppGeneric(' case IEMMODE_64BIT | (IEMMODE_32BIT << 2):'),
830 dByVariation[ThreadedFunctionVariation.ksVariation_Addr64_32].emitThreadedCallStmt(8),
831 iai.McCppGeneric(' break;'),
832 ]);
833 aoStmts.extend([
834 iai.McCppGeneric(' IEM_NOT_REACHED_DEFAULT_CASE_RET();'),
835 iai.McCppGeneric('}'),
836 ]);
837
838 return aoStmts;
839
840 def morphInputCode(self, aoStmts, fCallEmitted = False, cDepth = 0):
841 """
842 Adjusts (& copies) the statements for the input/decoder so it will emit
843 calls to the right threaded functions for each block.
844
845 Returns list/tree of statements (aoStmts is not modified) and updated
846 fCallEmitted status.
847 """
848 #print('McBlock at %s:%s' % (os.path.split(self.oMcBlock.sSrcFile)[1], self.oMcBlock.iBeginLine,));
849 aoDecoderStmts = [];
850 for oStmt in aoStmts:
851 # Copy the statement. Make a deep copy to make sure we've got our own
852 # copies of all instance variables, even if a bit overkill at the moment.
853 oNewStmt = copy.deepcopy(oStmt);
854 aoDecoderStmts.append(oNewStmt);
855 #print('oNewStmt %s %s' % (oNewStmt.sName, len(oNewStmt.asParams),));
856
857 # If we haven't emitted the threaded function call yet, look for
858 # statements which it would naturally follow or preceed.
859 if not fCallEmitted:
860 if not oStmt.isCppStmt():
861 if ( oStmt.sName.startswith('IEM_MC_MAYBE_RAISE_') \
862 or (oStmt.sName.endswith('_AND_FINISH') and oStmt.sName.startswith('IEM_MC_'))
863 or oStmt.sName.startswith('IEM_MC_CALL_CIMPL_')
864 or oStmt.sName in ('IEM_MC_RAISE_DIVIDE_ERROR',)):
865 aoDecoderStmts.pop();
866 aoDecoderStmts.extend(self.emitThreadedCallStmts());
867 aoDecoderStmts.append(oNewStmt);
868 fCallEmitted = True;
869 elif ( oStmt.fDecode
870 and ( oStmt.asParams[0].find('IEMOP_HLP_DONE_') >= 0
871 or oStmt.asParams[0].find('IEMOP_HLP_DECODED_') >= 0)):
872 aoDecoderStmts.extend(self.emitThreadedCallStmts());
873 fCallEmitted = True;
874
875 # Process branches of conditionals recursively.
876 if isinstance(oStmt, iai.McStmtCond):
877 (oNewStmt.aoIfBranch, fCallEmitted1) = self.morphInputCode(oStmt.aoIfBranch, fCallEmitted, cDepth + 1);
878 if oStmt.aoElseBranch:
879 (oNewStmt.aoElseBranch, fCallEmitted2) = self.morphInputCode(oStmt.aoElseBranch, fCallEmitted, cDepth + 1);
880 else:
881 fCallEmitted2 = False;
882 fCallEmitted = fCallEmitted or (fCallEmitted1 and fCallEmitted2);
883
884 if not fCallEmitted and cDepth == 0:
885 self.raiseProblem('Unable to insert call to threaded function.');
886
887 return (aoDecoderStmts, fCallEmitted);
888
889
890 def generateInputCode(self):
891 """
892 Modifies the input code.
893 """
894 assert len(self.oMcBlock.asLines) > 2, "asLines=%s" % (self.oMcBlock.asLines,);
895 cchIndent = (self.oMcBlock.cchIndent + 3) // 4 * 4;
896 return iai.McStmt.renderCodeForList(self.morphInputCode(self.oMcBlock.aoStmts)[0],
897 cchIndent = cchIndent).replace('\n', ' /* gen */\n', 1);
898
899
900class IEMThreadedGenerator(object):
901 """
902 The threaded code generator & annotator.
903 """
904
905 def __init__(self):
906 self.aoThreadedFuncs = [] # type: list(ThreadedFunction)
907 self.oOptions = None # type: argparse.Namespace
908 self.aoParsers = [] # type: list(IEMAllInstructionsPython.SimpleParser)
909
910 #
911 # Processing.
912 #
913
914 def processInputFiles(self):
915 """
916 Process the input files.
917 """
918
919 # Parse the files.
920 self.aoParsers = iai.parseFiles(self.oOptions.asInFiles);
921
922 # Create threaded functions for the MC blocks.
923 self.aoThreadedFuncs = [ThreadedFunction(oMcBlock) for oMcBlock in iai.g_aoMcBlocks];
924
925 # Analyze the threaded functions.
926 dRawParamCounts = {};
927 dMinParamCounts = {};
928 for oThreadedFunction in self.aoThreadedFuncs:
929 oThreadedFunction.analyze();
930 for oVariation in oThreadedFunction.aoVariations:
931 dRawParamCounts[len(oVariation.dParamRefs)] = dRawParamCounts.get(len(oVariation.dParamRefs), 0) + 1;
932 dMinParamCounts[oVariation.cMinParams] = dMinParamCounts.get(oVariation.cMinParams, 0) + 1;
933 print('debug: param count distribution, raw and optimized:', file = sys.stderr);
934 for cCount in sorted({cBits: True for cBits in list(dRawParamCounts.keys()) + list(dMinParamCounts.keys())}.keys()):
935 print('debug: %s params: %4s raw, %4s min'
936 % (cCount, dRawParamCounts.get(cCount, 0), dMinParamCounts.get(cCount, 0)),
937 file = sys.stderr);
938
939 return True;
940
941 #
942 # Output
943 #
944
945 def generateLicenseHeader(self):
946 """
947 Returns the lines for a license header.
948 """
949 return [
950 '/*',
951 ' * Autogenerated by $Id: IEMAllThreadedPython.py 99819 2023-05-16 23:24:30Z vboxsync $ ',
952 ' * Do not edit!',
953 ' */',
954 '',
955 '/*',
956 ' * Copyright (C) 2023-' + str(datetime.date.today().year) + ' Oracle and/or its affiliates.',
957 ' *',
958 ' * This file is part of VirtualBox base platform packages, as',
959 ' * available from https://www.alldomusa.eu.org.',
960 ' *',
961 ' * This program is free software; you can redistribute it and/or',
962 ' * modify it under the terms of the GNU General Public License',
963 ' * as published by the Free Software Foundation, in version 3 of the',
964 ' * License.',
965 ' *',
966 ' * This program is distributed in the hope that it will be useful, but',
967 ' * WITHOUT ANY WARRANTY; without even the implied warranty of',
968 ' * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU',
969 ' * General Public License for more details.',
970 ' *',
971 ' * You should have received a copy of the GNU General Public License',
972 ' * along with this program; if not, see <https://www.gnu.org/licenses>.',
973 ' *',
974 ' * The contents of this file may alternatively be used under the terms',
975 ' * of the Common Development and Distribution License Version 1.0',
976 ' * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included',
977 ' * in the VirtualBox distribution, in which case the provisions of the',
978 ' * CDDL are applicable instead of those of the GPL.',
979 ' *',
980 ' * You may elect to license modified versions of this file under the',
981 ' * terms and conditions of either the GPL or the CDDL or both.',
982 ' *',
983 ' * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0',
984 ' */',
985 '',
986 '',
987 '',
988 ];
989
990
991 def generateThreadedFunctionsHeader(self, oOut):
992 """
993 Generates the threaded functions header file.
994 Returns success indicator.
995 """
996
997 asLines = self.generateLicenseHeader();
998
999 # Generate the threaded function table indexes.
1000 asLines += [
1001 'typedef enum IEMTHREADEDFUNCS',
1002 '{',
1003 ' kIemThreadedFunc_Invalid = 0,',
1004 ];
1005 for oThreadedFunction in self.aoThreadedFuncs:
1006 for oVariation in oThreadedFunction.aoVariations:
1007 asLines.append(' ' + oVariation.getIndexName() + ',');
1008 asLines += [
1009 ' kIemThreadedFunc_End',
1010 '} IEMTHREADEDFUNCS;',
1011 '',
1012 ];
1013
1014 # Prototype the function table.
1015 sFnType = 'typedef IEM_DECL_IMPL_TYPE(VBOXSTRICTRC, FNIEMTHREADEDFUNC, (PVMCPU pVCpu';
1016 for iParam in range(g_kcThreadedParams):
1017 sFnType += ', uint64_t uParam' + str(iParam);
1018 sFnType += '));'
1019
1020 asLines += [
1021 sFnType,
1022 'typedef FNIEMTHREADEDFUNC *PFNIEMTHREADEDFUNC;',
1023 '',
1024 'extern const PFNIEMTHREADEDFUNC g_apfnIemThreadedFunctions[kIemThreadedFunc_End];',
1025 ];
1026
1027 oOut.write('\n'.join(asLines));
1028 return True;
1029
1030 ksBitsToIntMask = {
1031 1: "UINT64_C(0x1)",
1032 2: "UINT64_C(0x3)",
1033 4: "UINT64_C(0xf)",
1034 8: "UINT64_C(0xff)",
1035 16: "UINT64_C(0xffff)",
1036 32: "UINT64_C(0xffffffff)",
1037 };
1038 def generateThreadedFunctionsSource(self, oOut):
1039 """
1040 Generates the threaded functions source file.
1041 Returns success indicator.
1042 """
1043
1044 asLines = self.generateLicenseHeader();
1045 oOut.write('\n'.join(asLines));
1046
1047 # Prepare the fixed bits.
1048 sParamList = '(PVMCPU pVCpu';
1049 for iParam in range(g_kcThreadedParams):
1050 sParamList += ', uint64_t uParam' + str(iParam);
1051 sParamList += '))\n';
1052
1053 #
1054 # Emit the function definitions.
1055 #
1056 for oThreadedFunction in self.aoThreadedFuncs:
1057 oMcBlock = oThreadedFunction.oMcBlock;
1058 for oVariation in oThreadedFunction.aoVariations:
1059 # Function header
1060 oOut.write( '\n'
1061 + '\n'
1062 + '/**\n'
1063 + ' * %s at line %s offset %s in %s%s\n'
1064 % (oMcBlock.sFunction, oMcBlock.iBeginLine, oMcBlock.offBeginLine,
1065 os.path.split(oMcBlock.sSrcFile)[1],
1066 ' (macro expansion)' if oMcBlock.iBeginLine == oMcBlock.iEndLine else '')
1067 + ' */\n'
1068 + 'static IEM_DECL_IMPL_DEF(VBOXSTRICTRC, ' + oVariation.getFunctionName() + ',\n'
1069 + ' ' + sParamList
1070 + '{\n');
1071
1072 aasVars = [];
1073 for aoRefs in oVariation.dParamRefs.values():
1074 oRef = aoRefs[0];
1075 if oRef.sType[0] != 'P':
1076 cBits = g_kdTypeInfo[oRef.sType][0];
1077 sType = g_kdTypeInfo[oRef.sType][2];
1078 else:
1079 cBits = 64;
1080 sType = oRef.sType;
1081
1082 sTypeDecl = sType + ' const';
1083
1084 if cBits == 64:
1085 assert oRef.offNewParam == 0;
1086 if sType == 'uint64_t':
1087 sUnpack = 'uParam%s;' % (oRef.iNewParam,);
1088 else:
1089 sUnpack = '(%s)uParam%s;' % (sType, oRef.iNewParam,);
1090 elif oRef.offNewParam == 0:
1091 sUnpack = '(%s)(uParam%s & %s);' % (sType, oRef.iNewParam, self.ksBitsToIntMask[cBits]);
1092 else:
1093 sUnpack = '(%s)((uParam%s >> %s) & %s);' \
1094 % (sType, oRef.iNewParam, oRef.offNewParam, self.ksBitsToIntMask[cBits]);
1095
1096 sComment = '/* %s - %s ref%s */' % (oRef.sOrgRef, len(aoRefs), 's' if len(aoRefs) != 1 else '',);
1097
1098 aasVars.append([ '%s:%02u' % (oRef.iNewParam, oRef.offNewParam),
1099 sTypeDecl, oRef.sNewName, sUnpack, sComment ]);
1100 acchVars = [0, 0, 0, 0, 0];
1101 for asVar in aasVars:
1102 for iCol, sStr in enumerate(asVar):
1103 acchVars[iCol] = max(acchVars[iCol], len(sStr));
1104 sFmt = ' %%-%ss %%-%ss = %%-%ss %%s\n' % (acchVars[1], acchVars[2], acchVars[3]);
1105 for asVar in sorted(aasVars):
1106 oOut.write(sFmt % (asVar[1], asVar[2], asVar[3], asVar[4],));
1107
1108 # RT_NOREF for unused parameters.
1109 if oVariation.cMinParams < g_kcThreadedParams:
1110 oOut.write(' RT_NOREF('
1111 + ', '.join(['uParam%u' % (i,) for i in range(oVariation.cMinParams, g_kcThreadedParams)])
1112 + ');\n');
1113
1114 # Now for the actual statements.
1115 oOut.write(iai.McStmt.renderCodeForList(oVariation.aoStmtsForThreadedFunction, cchIndent = 4));
1116
1117 oOut.write('}\n');
1118
1119
1120 #
1121 # Emit the function table.
1122 #
1123 oOut.write( '\n'
1124 + '\n'
1125 + '/**\n'
1126 + ' * Function table.\n'
1127 + ' */\n'
1128 + 'const PFNIEMTHREADEDFUNC g_apfnIemThreadedFunctions[kIemThreadedFunc_End] =\n'
1129 + '{\n'
1130 + ' /*Invalid*/ NULL, \n');
1131 iThreadedFunction = 0;
1132 for oThreadedFunction in self.aoThreadedFuncs:
1133 for oVariation in oThreadedFunction.aoVariations:
1134 iThreadedFunction += 1;
1135 oOut.write(' /*%4u*/ %s,\n' % (iThreadedFunction, oVariation.getFunctionName(),));
1136 oOut.write('};\n');
1137
1138 return True;
1139
1140 def getThreadedFunctionByIndex(self, idx):
1141 """
1142 Returns a ThreadedFunction object for the given index. If the index is
1143 out of bounds, a dummy is returned.
1144 """
1145 if idx < len(self.aoThreadedFuncs):
1146 return self.aoThreadedFuncs[idx];
1147 return ThreadedFunction.dummyInstance();
1148
1149 def findEndOfMcEndStmt(self, sLine, offEndStmt):
1150 """
1151 Helper that returns the line offset following the 'IEM_MC_END();'.
1152 """
1153 assert sLine[offEndStmt:].startswith('IEM_MC_END');
1154 off = sLine.find(';', offEndStmt + len('IEM_MC_END'));
1155 assert off > 0, 'sLine="%s"' % (sLine, );
1156 return off + 1 if off > 0 else 99999998;
1157
1158 def generateModifiedInput(self, oOut):
1159 """
1160 Generates the combined modified input source/header file.
1161 Returns success indicator.
1162 """
1163 #
1164 # File header.
1165 #
1166 oOut.write('\n'.join(self.generateLicenseHeader()));
1167
1168 #
1169 # ASSUMING that g_aoMcBlocks/self.aoThreadedFuncs are in self.aoParsers
1170 # order, we iterate aoThreadedFuncs in parallel to the lines from the
1171 # parsers and apply modifications where required.
1172 #
1173 iThreadedFunction = 0;
1174 oThreadedFunction = self.getThreadedFunctionByIndex(0);
1175 for oParser in self.aoParsers: # type: IEMAllInstructionsPython.SimpleParser
1176 oOut.write("\n\n/* ****** BEGIN %s ******* */\n" % (oParser.sSrcFile,));
1177
1178 iLine = 0;
1179 while iLine < len(oParser.asLines):
1180 sLine = oParser.asLines[iLine];
1181 iLine += 1; # iBeginLine and iEndLine are 1-based.
1182
1183 # Can we pass it thru?
1184 if ( iLine not in [oThreadedFunction.oMcBlock.iBeginLine, oThreadedFunction.oMcBlock.iEndLine]
1185 or oThreadedFunction.oMcBlock.sSrcFile != oParser.sSrcFile):
1186 oOut.write(sLine);
1187 #
1188 # Single MC block. Just extract it and insert the replacement.
1189 #
1190 elif oThreadedFunction.oMcBlock.iBeginLine != oThreadedFunction.oMcBlock.iEndLine:
1191 assert sLine.count('IEM_MC_') == 1;
1192 oOut.write(sLine[:oThreadedFunction.oMcBlock.offBeginLine]);
1193 sModified = oThreadedFunction.generateInputCode().strip();
1194 oOut.write(sModified);
1195
1196 iLine = oThreadedFunction.oMcBlock.iEndLine;
1197 sLine = oParser.asLines[iLine - 1];
1198 assert sLine.count('IEM_MC_') == 1;
1199 oOut.write(sLine[self.findEndOfMcEndStmt(sLine, oThreadedFunction.oMcBlock.offEndLine) : ]);
1200
1201 # Advance
1202 iThreadedFunction += 1;
1203 oThreadedFunction = self.getThreadedFunctionByIndex(iThreadedFunction);
1204 #
1205 # Macro expansion line that have sublines and may contain multiple MC blocks.
1206 #
1207 else:
1208 offLine = 0;
1209 while iLine == oThreadedFunction.oMcBlock.iBeginLine:
1210 oOut.write(sLine[offLine : oThreadedFunction.oMcBlock.offBeginLine]);
1211
1212 sModified = oThreadedFunction.generateInputCode().strip();
1213 assert sModified.startswith('IEM_MC_BEGIN'), 'sModified="%s"' % (sModified,);
1214 oOut.write(sModified);
1215
1216 offLine = self.findEndOfMcEndStmt(sLine, oThreadedFunction.oMcBlock.offEndLine);
1217
1218 # Advance
1219 iThreadedFunction += 1;
1220 oThreadedFunction = self.getThreadedFunctionByIndex(iThreadedFunction);
1221
1222 # Last line segment.
1223 if offLine < len(sLine):
1224 oOut.write(sLine[offLine : ]);
1225
1226 oOut.write("/* ****** END %s ******* */\n" % (oParser.sSrcFile,));
1227
1228 return True;
1229
1230 #
1231 # Main
1232 #
1233
1234 def main(self, asArgs):
1235 """
1236 C-like main function.
1237 Returns exit code.
1238 """
1239
1240 #
1241 # Parse arguments
1242 #
1243 sScriptDir = os.path.dirname(__file__);
1244 oParser = argparse.ArgumentParser(add_help = False);
1245 oParser.add_argument('asInFiles', metavar = 'input.cpp.h', nargs = '*',
1246 default = [os.path.join(sScriptDir, asFM[0]) for asFM in iai.g_aasAllInstrFilesAndDefaultMap],
1247 help = "Selection of VMMAll/IEMAllInstructions*.cpp.h files to use as input.");
1248 oParser.add_argument('--out-funcs-hdr', metavar = 'file-funcs.h', dest = 'sOutFileFuncsHdr', action = 'store',
1249 default = '-', help = 'The output header file for the functions.');
1250 oParser.add_argument('--out-funcs-cpp', metavar = 'file-funcs.cpp', dest = 'sOutFileFuncsCpp', action = 'store',
1251 default = '-', help = 'The output C++ file for the functions.');
1252 oParser.add_argument('--out-mod-input', metavar = 'file-instr.cpp.h', dest = 'sOutFileModInput', action = 'store',
1253 default = '-', help = 'The output C++/header file for the modified input instruction files.');
1254 oParser.add_argument('--help', '-h', '-?', action = 'help', help = 'Display help and exit.');
1255 oParser.add_argument('--version', '-V', action = 'version',
1256 version = 'r%s (IEMAllThreadedPython.py), r%s (IEMAllInstructionsPython.py)'
1257 % (__version__.split()[1], iai.__version__.split()[1],),
1258 help = 'Displays the version/revision of the script and exit.');
1259 self.oOptions = oParser.parse_args(asArgs[1:]);
1260 print("oOptions=%s" % (self.oOptions,));
1261
1262 #
1263 # Process the instructions specified in the IEM sources.
1264 #
1265 if self.processInputFiles():
1266 #
1267 # Generate the output files.
1268 #
1269 aaoOutputFiles = (
1270 ( self.oOptions.sOutFileFuncsHdr, self.generateThreadedFunctionsHeader ),
1271 ( self.oOptions.sOutFileFuncsCpp, self.generateThreadedFunctionsSource ),
1272 ( self.oOptions.sOutFileModInput, self.generateModifiedInput ),
1273 );
1274 fRc = True;
1275 for sOutFile, fnGenMethod in aaoOutputFiles:
1276 if sOutFile == '-':
1277 fRc = fnGenMethod(sys.stdout) and fRc;
1278 else:
1279 try:
1280 oOut = open(sOutFile, 'w'); # pylint: disable=consider-using-with,unspecified-encoding
1281 except Exception as oXcpt:
1282 print('error! Failed open "%s" for writing: %s' % (sOutFile, oXcpt,), file = sys.stderr);
1283 return 1;
1284 fRc = fnGenMethod(oOut) and fRc;
1285 oOut.close();
1286 if fRc:
1287 return 0;
1288
1289 return 1;
1290
1291
1292if __name__ == '__main__':
1293 sys.exit(IEMThreadedGenerator().main(sys.argv));
1294
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette