VirtualBox

source: vbox/trunk/src/VBox/VMM/testcase/Instructions/InstructionTestGen.py@ 46875

Last change on this file since 46875 was 46875, checked in by vboxsync, 11 years ago

yeah, finally getting somewhere...

  • Property svn:eol-style set to LF
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 62.8 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# $Id: InstructionTestGen.py 46875 2013-06-29 02:15:38Z vboxsync $
4
5"""
6Instruction Test Generator.
7"""
8
9from __future__ import print_function;
10
11__copyright__ = \
12"""
13Copyright (C) 2012-2013 Oracle Corporation
14
15Oracle Corporation confidential
16All rights reserved
17"""
18__version__ = "$Revision: 46875 $";
19
20
21# Standard python imports.
22import io;
23import os;
24from optparse import OptionParser
25import random;
26import sys;
27
28
29## @name Exit codes
30## @{
31RTEXITCODE_SUCCESS = 0;
32RTEXITCODE_SYNTAX = 2;
33## @}
34
35## @name Various C macros we're used to.
36## @{
37UINT8_MAX = 0xff
38UINT16_MAX = 0xffff
39UINT32_MAX = 0xffffffff
40UINT64_MAX = 0xffffffffffffffff
41def RT_BIT_32(iBit): # pylint: disable=C0103
42 """ 32-bit one bit mask. """
43 return 1 << iBit;
44def RT_BIT_64(iBit): # pylint: disable=C0103
45 """ 64-bit one bit mask. """
46 return 1 << iBit;
47## @}
48
49
50## @name ModR/M
51## @{
52X86_MODRM_RM_MASK = 0x07;
53X86_MODRM_REG_MASK = 0x38;
54X86_MODRM_REG_SMASK = 0x07;
55X86_MODRM_REG_SHIFT = 3;
56X86_MODRM_MOD_MASK = 0xc0;
57X86_MODRM_MOD_SMASK = 0x03;
58X86_MODRM_MOD_SHIFT = 6;
59## @}
60
61## @name SIB
62## @{
63X86_SIB_BASE_MASK = 0x07;
64X86_SIB_INDEX_MASK = 0x38;
65X86_SIB_INDEX_SMASK = 0x07;
66X86_SIB_INDEX_SHIFT = 3;
67X86_SIB_SCALE_MASK = 0xc0;
68X86_SIB_SCALE_SMASK = 0x03;
69X86_SIB_SCALE_SHIFT = 6;
70## @}
71
72## @name Prefixes
73## @
74X86_OP_PRF_CS = 0x2e;
75X86_OP_PRF_SS = 0x36;
76X86_OP_PRF_DS = 0x3e;
77X86_OP_PRF_ES = 0x26;
78X86_OP_PRF_FS = 0x64;
79X86_OP_PRF_GS = 0x65;
80X86_OP_PRF_SIZE_OP = 0x66;
81X86_OP_PRF_SIZE_ADDR = 0x67;
82X86_OP_PRF_LOCK = 0xf0;
83X86_OP_PRF_REPZ = 0xf2;
84X86_OP_PRF_REPNZ = 0xf3;
85X86_OP_REX_B = 0x41;
86X86_OP_REX_X = 0x42;
87X86_OP_REX_R = 0x44;
88X86_OP_REX_W = 0x48;
89## @}
90
91
92## @name General registers
93## @
94X86_GREG_xAX = 0
95X86_GREG_xCX = 1
96X86_GREG_xDX = 2
97X86_GREG_xBX = 3
98X86_GREG_xSP = 4
99X86_GREG_xBP = 5
100X86_GREG_xSI = 6
101X86_GREG_xDI = 7
102X86_GREG_x8 = 8
103X86_GREG_x9 = 9
104X86_GREG_x10 = 10
105X86_GREG_x11 = 11
106X86_GREG_x12 = 12
107X86_GREG_x13 = 13
108X86_GREG_x14 = 14
109X86_GREG_x15 = 15
110## @}
111
112
113## @name Register names.
114## @{
115g_asGRegs64NoSp = ('rax', 'rcx', 'rdx', 'rbx', None, 'rbp', 'rsi', 'rdi', 'r8', 'r9', 'r10', 'r11', 'r12', 'r13', 'r14', 'r15');
116g_asGRegs64 = ('rax', 'rcx', 'rdx', 'rbx', 'rsp', 'rbp', 'rsi', 'rdi', 'r8', 'r9', 'r10', 'r11', 'r12', 'r13', 'r14', 'r15');
117g_asGRegs32NoSp = ('eax', 'ecx', 'edx', 'ebx', None, 'ebp', 'esi', 'edi',
118 'r8d', 'r9d', 'r10d', 'r11d', 'r12d', 'r13d', 'r14d', 'r15d');
119g_asGRegs32 = ('eax', 'ecx', 'edx', 'ebx', 'esp', 'ebp', 'esi', 'edi',
120 'r8d', 'r9d', 'r10d', 'r11d', 'r12d', 'r13d', 'r14d', 'r15d');
121g_asGRegs16NoSp = ('ax', 'cx', 'dx', 'bx', None, 'bp', 'si', 'di',
122 'r8w', 'r9w', 'r10w', 'r11w', 'r12w', 'r13w', 'r14w', 'r15w');
123g_asGRegs16 = ('ax', 'cx', 'dx', 'bx', 'sp', 'bp', 'si', 'di',
124 'r8w', 'r9w', 'r10w', 'r11w', 'r12w', 'r13w', 'r14w', 'r15w');
125g_asGRegs8 = ('al', 'cl', 'dl', 'bl', 'ah', 'ch', 'dh', 'bh');
126g_asGRegs8Rex = ('al', 'cl', 'dl', 'bl', 'spl', 'bpl', 'sil', 'dil',
127 'r8b', 'r9b', 'r10b', 'r11b', 'r12b', 'r13b', 'r14b', 'r15b',
128 'ah', 'ch', 'dh', 'bh');
129## @}
130
131
132## @name Random
133## @{
134g_oMyRand = random.SystemRandom()
135def randU16():
136 """ Unsigned 16-bit random number. """
137 return g_oMyRand.getrandbits(16);
138
139def randU32():
140 """ Unsigned 32-bit random number. """
141 return g_oMyRand.getrandbits(32);
142
143def randU64():
144 """ Unsigned 64-bit random number. """
145 return g_oMyRand.getrandbits(64);
146
147def randUxx(cBits):
148 """ Unsigned 8-, 16-, 32-, or 64-bit random number. """
149 return g_oMyRand.getrandbits(cBits);
150
151def randUxxList(cBits, cElements):
152 """ List of unsigned 8-, 16-, 32-, or 64-bit random numbers. """
153 return [randUxx(cBits) for _ in range(cElements)];
154## @}
155
156
157
158
159## @name Instruction Emitter Helpers
160## @{
161
162def calcRexPrefixForTwoModRmRegs(iReg, iRm, bOtherRexPrefixes = 0):
163 """
164 Calculates a rex prefix if neccessary given the two registers
165 and optional rex size prefixes.
166 Returns an empty array if not necessary.
167 """
168 bRex = bOtherRexPrefixes;
169 if iReg >= 8:
170 bRex |= X86_OP_REX_R;
171 if iRm >= 8:
172 bRex |= X86_OP_REX_B;
173 if bRex == 0:
174 return [];
175 return [bRex,];
176
177def calcModRmForTwoRegs(iReg, iRm):
178 """
179 Calculate the RM byte for two registers.
180 Returns an array with one byte in it.
181 """
182 bRm = (0x3 << X86_MODRM_MOD_SHIFT) \
183 | ((iReg << X86_MODRM_REG_SHIFT) & X86_MODRM_REG_MASK) \
184 | (iRm & X86_MODRM_RM_MASK);
185 return [bRm,];
186
187## @}
188
189
190## @name Misc
191## @{
192
193def convU32ToSigned(u32):
194 """ Converts a 32-bit unsigned value to 32-bit signed. """
195 if u32 < 0x80000000:
196 return u32;
197 return u32 - UINT32_MAX - 1;
198
199def rotateLeftUxx(cBits, uVal, cShift):
200 """ Rotate a xx-bit wide unsigned number to the left. """
201 assert cShift < cBits;
202
203 if cBits == 16:
204 uMask = UINT16_MAX;
205 elif cBits == 32:
206 uMask = UINT32_MAX;
207 elif cBits == 64:
208 uMask = UINT64_MAX;
209 else:
210 assert cBits == 8;
211 uMask = UINT8_MAX;
212
213 uVal &= uMask;
214 uRet = (uVal << cShift) & uMask;
215 uRet |= (uVal >> (cBits - cShift));
216 return uRet;
217
218def rotateRightUxx(cBits, uVal, cShift):
219 """ Rotate a xx-bit wide unsigned number to the right. """
220 assert cShift < cBits;
221
222 if cBits == 16:
223 uMask = UINT16_MAX;
224 elif cBits == 32:
225 uMask = UINT32_MAX;
226 elif cBits == 64:
227 uMask = UINT64_MAX;
228 else:
229 assert cBits == 8;
230 uMask = UINT8_MAX;
231
232 uVal &= uMask;
233 uRet = (uVal >> cShift);
234 uRet |= (uVal << (cBits - cShift)) & uMask;
235 return uRet;
236
237def gregName(iReg, cBits, fRexByteRegs = True):
238 """ Gets the name of a general register by index and width. """
239 if cBits == 64:
240 return g_asGRegs64[iReg];
241 if cBits == 32:
242 return g_asGRegs32[iReg];
243 if cBits == 16:
244 return g_asGRegs16[iReg];
245 assert cBits == 8;
246 if fRexByteRegs:
247 return g_asGRegs8Rex[iReg];
248 return g_asGRegs8[iReg];
249
250## @}
251
252
253class TargetEnv(object):
254 """
255 Target Runtime Environment.
256 """
257
258 ## @name CPU Modes
259 ## @{
260 ksCpuMode_Real = 'real';
261 ksCpuMode_Protect = 'prot';
262 ksCpuMode_Paged = 'paged';
263 ksCpuMode_Long = 'long';
264 ksCpuMode_V86 = 'v86';
265 ## @}
266
267 ## @name Instruction set.
268 ## @{
269 ksInstrSet_16 = '16';
270 ksInstrSet_32 = '32';
271 ksInstrSet_64 = '64';
272 ## @}
273
274 def __init__(self, sName,
275 sInstrSet = ksInstrSet_32,
276 sCpuMode = ksCpuMode_Paged,
277 iRing = 3,
278 ):
279 self.sName = sName;
280 self.sInstrSet = sInstrSet;
281 self.sCpuMode = sCpuMode;
282 self.iRing = iRing;
283 self.asGRegs = g_asGRegs64 if self.is64Bit() else g_asGRegs32;
284 self.asGRegsNoSp = g_asGRegs64NoSp if self.is64Bit() else g_asGRegs32NoSp;
285
286 def isUsingIprt(self):
287 """ Whether it's an IPRT environment or not. """
288 return self.sName.startswith('iprt');
289
290 def is64Bit(self):
291 """ Whether it's a 64-bit environment or not. """
292 return self.sInstrSet == self.ksInstrSet_64;
293
294 def getDefOpBits(self):
295 """ Get the default operand size as a bit count. """
296 if self.sInstrSet == self.ksInstrSet_16:
297 return 16;
298 return 32;
299
300 def getDefOpBytes(self):
301 """ Get the default operand size as a byte count. """
302 return self.getDefOpBits() / 8;
303
304 def getMaxOpBits(self):
305 """ Get the max operand size as a bit count. """
306 if self.sInstrSet == self.ksInstrSet_64:
307 return 64;
308 return 32;
309
310 def getMaxOpBytes(self):
311 """ Get the max operand size as a byte count. """
312 return self.getMaxOpBits() / 8;
313
314 def getDefAddrBits(self):
315 """ Get the default address size as a bit count. """
316 if self.sInstrSet == self.ksInstrSet_16:
317 return 16;
318 if self.sInstrSet == self.ksInstrSet_32:
319 return 32;
320 return 64;
321
322 def getDefAddrBytes(self):
323 """ Get the default address size as a byte count. """
324 return self.getDefAddrBits() / 8;
325
326 def getGRegCount(self, cbEffBytes = 4):
327 """ Get the number of general registers. """
328 if self.sInstrSet == self.ksInstrSet_64:
329 if cbEffBytes == 1:
330 return 16 + 4;
331 return 16;
332 return 8;
333
334 def randGRegNoSp(self):
335 """ Returns a random general register number, excluding the SP register. """
336 iReg = randU16();
337 return iReg % (16 if self.is64Bit() else 8);
338
339 def getAddrModes(self):
340 """ Gets a list of addressing mode (16, 32, or/and 64). """
341 if self.sInstrSet == self.ksInstrSet_16:
342 return [16, 32];
343 if self.sInstrSet == self.ksInstrSet_32:
344 return [32, 16];
345 return [64, 32];
346
347 def is8BitHighGReg(self, cbEffOp, iGReg):
348 """ Checks if the given register is a high 8-bit general register (AH, CH, DH or BH). """
349 assert cbEffOp in [1, 2, 4, 8];
350 if cbEffOp == 1:
351 if iGReg >= 16:
352 return True;
353 if iGReg >= 4 and not self.is64Bit():
354 return True;
355 return False;
356
357
358
359## Target environments.
360g_dTargetEnvs = {
361 'iprt-r3-32': TargetEnv('iprt-r3-32', TargetEnv.ksInstrSet_32, TargetEnv.ksCpuMode_Protect, 3),
362 'iprt-r3-64': TargetEnv('iprt-r3-64', TargetEnv.ksInstrSet_64, TargetEnv.ksCpuMode_Long, 3),
363};
364
365
366class InstrTestBase(object):
367 """
368 Base class for testing one instruction.
369 """
370
371 def __init__(self, sName, sInstr = None):
372 self.sName = sName;
373 self.sInstr = sInstr if sInstr else sName.split()[0];
374
375 def isApplicable(self, oGen):
376 """
377 Tests if the instruction test is applicable to the selected environment.
378 """
379 _ = oGen;
380 return True;
381
382 def generateTest(self, oGen, sTestFnName):
383 """
384 Emits the test assembly code.
385 """
386 oGen.write(';; @todo not implemented. This is for the linter: %s, %s\n' % (oGen, sTestFnName));
387 return True;
388
389 def generateInputs(self, cbEffOp, cbMaxOp, oGen, fLong = False):
390 """ Generate a list of inputs. """
391 if fLong:
392 #
393 # Try do extremes as well as different ranges of random numbers.
394 #
395 auRet = [0, 1, ];
396 if cbMaxOp >= 1:
397 auRet += [ UINT8_MAX / 2, UINT8_MAX / 2 + 1, UINT8_MAX ];
398 if cbMaxOp >= 2:
399 auRet += [ UINT16_MAX / 2, UINT16_MAX / 2 + 1, UINT16_MAX ];
400 if cbMaxOp >= 4:
401 auRet += [ UINT32_MAX / 2, UINT32_MAX / 2 + 1, UINT32_MAX ];
402 if cbMaxOp >= 8:
403 auRet += [ UINT64_MAX / 2, UINT64_MAX / 2 + 1, UINT64_MAX ];
404
405 if oGen.oOptions.sTestSize == InstructionTestGen.ksTestSize_Tiny:
406 for cBits, cValues in ( (8, 4), (16, 4), (32, 8), (64, 8) ):
407 if cBits < cbMaxOp * 8:
408 auRet += randUxxList(cBits, cValues);
409 cWanted = 16;
410 elif oGen.oOptions.sTestSize == InstructionTestGen.ksTestSize_Medium:
411 for cBits, cValues in ( (8, 8), (16, 8), (24, 2), (32, 16), (40, 1), (48, 1), (56, 1), (64, 16) ):
412 if cBits < cbMaxOp * 8:
413 auRet += randUxxList(cBits, cValues);
414 cWanted = 64;
415 else:
416 for cBits, cValues in ( (8, 16), (16, 16), (24, 4), (32, 64), (40, 4), (48, 4), (56, 4), (64, 64) ):
417 if cBits < cbMaxOp * 8:
418 auRet += randUxxList(cBits, cValues);
419 cWanted = 168;
420 if len(auRet) < cWanted:
421 auRet += randUxxList(cbEffOp * 8, cWanted - len(auRet));
422 else:
423 #
424 # Short list, just do some random numbers.
425 #
426 auRet = [];
427 if oGen.oOptions.sTestSize == InstructionTestGen.ksTestSize_Tiny:
428 auRet += randUxxList(cbMaxOp, 1);
429 elif oGen.oOptions.sTestSize == InstructionTestGen.ksTestSize_Medium:
430 auRet += randUxxList(cbMaxOp, 2);
431 else:
432 auRet = [];
433 for cBits in (8, 16, 32, 64):
434 if cBits < cbMaxOp * 8:
435 auRet += randUxxList(cBits, 1);
436 return auRet;
437
438
439class InstrTest_MemOrGreg_2_Greg(InstrTestBase):
440 """
441 Instruction reading memory or general register and writing the result to a
442 general register.
443 """
444
445 def __init__(self, sName, fnCalcResult, sInstr = None, acbOpVars = None):
446 InstrTestBase.__init__(self, sName, sInstr);
447 self.fnCalcResult = fnCalcResult;
448 self.acbOpVars = [ 1, 2, 4, 8 ] if not acbOpVars else list(acbOpVars);
449
450 ## @name Test Instruction Writers
451 ## @{
452
453 def writeInstrGregGreg(self, cbEffOp, iOp1, iOp2, oGen):
454 """ Writes the instruction with two general registers as operands. """
455 fRexByteRegs = oGen.oTarget.is64Bit();
456 oGen.write(' %s %s, %s\n'
457 % (self.sInstr, gregName(iOp1, cbEffOp * 8, fRexByteRegs), gregName(iOp2, cbEffOp * 8, fRexByteRegs),));
458 return True;
459
460 def writeInstrGregPureRM(self, cbEffOp, iOp1, cAddrBits, iOp2, iMod, offDisp, oGen):
461 """ Writes the instruction with two general registers as operands. """
462 oGen.write(' ');
463 if iOp2 == 13 and iMod == 0 and cAddrBits == 64:
464 oGen.write('altrexb '); # Alternative encoding for rip relative addressing.
465 if cbEffOp == 8:
466 oGen.write('%s %s, [' % (self.sInstr, g_asGRegs64[iOp1],));
467 elif cbEffOp == 4:
468 oGen.write('%s %s, [' % (self.sInstr, g_asGRegs32[iOp1],));
469 elif cbEffOp == 2:
470 oGen.write('%s %s, [' % (self.sInstr, g_asGRegs16[iOp1],));
471 elif cbEffOp == 1:
472 oGen.write('%s %s, [' % (self.sInstr, g_asGRegs8Rex[iOp1],));
473 else:
474 assert False;
475
476 if (iOp2 == 5 or iOp2 == 13) and iMod == 0:
477 oGen.write('VBINSTST_NAME(g_u%sData)' % (cbEffOp * 8,))
478 if oGen.oTarget.is64Bit():
479 oGen.write(' wrt rip');
480 else:
481 if iMod == 1:
482 oGen.write('byte %d + ' % (offDisp,));
483 elif iMod == 2:
484 oGen.write('dword %d + ' % (offDisp,));
485 else:
486 assert iMod == 0;
487
488 if cAddrBits == 64:
489 oGen.write(g_asGRegs64[iOp2]);
490 elif cAddrBits == 32:
491 oGen.write(g_asGRegs32[iOp2]);
492 elif cAddrBits == 16:
493 assert False; ## @todo implement 16-bit addressing.
494 else:
495 assert False, str(cAddrBits);
496
497 oGen.write(']\n');
498 return True;
499
500 def writeInstrGregSibLabel(self, cbEffOp, iOp1, cAddrBits, iBaseReg, iIndexReg, iScale, offDisp, oGen):
501 """ Writes the instruction taking a register and a label (base only w/o reg), SIB form. """
502 assert offDisp is None; assert iBaseReg in [5, 13]; assert iIndexReg == 4; assert cAddrBits != 16;
503 if cAddrBits == 64:
504 # Note! Cannot test this in 64-bit mode in any sensible way because the disp is 32-bit
505 # and we cannot (yet) make assumtions about where we're loaded.
506 ## @todo Enable testing this in environments where we can make assumptions (boot sector).
507 oGen.write(' %s %s, [VBINSTST_NAME(g_u%sData) xWrtRIP]\n'
508 % ( self.sInstr, gregName(iOp1, cbEffOp * 8), cbEffOp * 8,));
509 else:
510 oGen.write(' altsibx%u %s %s, [VBINSTST_NAME(g_u%sData) xWrtRIP]\n'
511 % ( iScale, self.sInstr, gregName(iOp1, cbEffOp * 8), cbEffOp * 8,));
512 return True;
513
514 def writeInstrGregSibScaledReg(self, cbEffOp, iOp1, cAddrBits, iBaseReg, iIndexReg, iScale, offDisp, oGen):
515 """ Writes the instruction taking a register and disp+scaled register (no base reg), SIB form. """
516 assert iBaseReg in [5, 13]; assert iIndexReg != 4; assert cAddrBits != 16;
517 # Note! Using altsibxN to force scaled encoding. This is only really a
518 # necessity for iScale=1, but doesn't hurt for the rest.
519 oGen.write(' altsibx%u %s %s, [%s * %#x'
520 % (iScale, self.sInstr, gregName(iOp1, cbEffOp * 8), gregName(iIndexReg, cAddrBits), iScale,));
521 if offDisp is not None:
522 oGen.write(' + %#x' % (offDisp,));
523 oGen.write(']\n');
524 _ = iBaseReg;
525 return True;
526
527 def writeInstrGregSibBase(self, cbEffOp, iOp1, cAddrBits, iBaseReg, iIndexReg, iScale, offDisp, oGen):
528 """ Writes the instruction taking a register and base only (with reg), SIB form. """
529 oGen.write(' altsibx%u %s %s, [%s'
530 % (iScale, self.sInstr, gregName(iOp1, cbEffOp * 8), gregName(iBaseReg, cAddrBits),));
531 if offDisp is not None:
532 oGen.write(' + %#x' % (offDisp,));
533 oGen.write(']\n');
534 _ = iIndexReg;
535 return True;
536
537 def writeInstrGregSibBaseAndScaledReg(self, cbEffOp, iOp1, cAddrBits, iBaseReg, iIndexReg, iScale, offDisp, oGen):
538 """ Writes tinstruction taking a register and full featured SIB form address. """
539 # Note! From the looks of things, yasm will encode the following instructions the same way:
540 # mov eax, [rsi*1 + rbx]
541 # mov eax, [rbx + rsi*1]
542 # So, when there are two registers involved, the '*1' selects
543 # which is index and which is base.
544 oGen.write(' %s %s, [%s + %s * %u'
545 % ( self.sInstr, gregName(iOp1, cbEffOp * 8),
546 gregName(iBaseReg, cAddrBits), gregName(iIndexReg, cAddrBits), iScale,));
547 if offDisp is not None:
548 oGen.write(' + %#x' % (offDisp,));
549 oGen.write(']\n');
550 return True;
551
552 ## @}
553
554
555 ## @name Memory setups
556 ## @{
557 def generateMemSetupReadByLabel(self, oGen, cbEffOp, uInput):
558 """ Sets up memory for a memory read. """
559 oGen.pushConst(uInput);
560 oGen.write(' call VBINSTST_NAME(Common_SetupMemReadU%u)\n' % (cbEffOp*8,));
561 return True;
562
563 def generateMemSetupReadByReg(self, oGen, cAddrBits, cbEffOp, iReg1, uInput, offDisp = None):
564 """ Sets up memory for a memory read indirectly addressed thru one register and optional displacement. """
565 oGen.pushConst(uInput);
566 oGen.write(' call VBINSTST_NAME(%s)\n'
567 % (oGen.needGRegMemSetup(cAddrBits, cbEffOp, iBaseReg = iReg1, offDisp = offDisp),));
568 oGen.write(' push %s\n' % (oGen.oTarget.asGRegs[iReg1],));
569 return True;
570
571 def generateMemSetupReadByScaledReg(self, oGen, cAddrBits, cbEffOp, iIndexReg, iScale, uInput, offDisp = None):
572 """ Sets up memory for a memory read indirectly addressed thru one register and optional displacement. """
573 oGen.pushConst(uInput);
574 oGen.write(' call VBINSTST_NAME(%s)\n'
575 % (oGen.needGRegMemSetup(cAddrBits, cbEffOp, offDisp = offDisp, iIndexReg = iIndexReg, iScale = iScale),));
576 oGen.write(' push %s\n' % (oGen.oTarget.asGRegs[iIndexReg],));
577 return True;
578
579 def generateMemSetupReadByBaseAndScaledReg(self, oGen, cAddrBits, cbEffOp, iBaseReg, iIndexReg, iScale, uInput, offDisp):
580 """ Sets up memory for a memory read indirectly addressed thru two registers with optional displacement. """
581 oGen.pushConst(uInput);
582 oGen.write(' call VBINSTST_NAME(%s)\n'
583 % (oGen.needGRegMemSetup(cAddrBits, cbEffOp, iBaseReg = iBaseReg, offDisp = offDisp,
584 iIndexReg = iIndexReg, iScale = iScale),));
585 oGen.write(' push %s\n' % (oGen.oTarget.asGRegs[iIndexReg],));
586 oGen.write(' push %s\n' % (oGen.oTarget.asGRegs[iBaseReg],));
587 return True;
588
589 def generateMemSetupPureRM(self, oGen, cAddrBits, cbEffOp, iOp2, iMod, uInput, offDisp = None):
590 """ Sets up memory for a pure R/M addressed read, iOp2 being the R/M value. """
591 oGen.pushConst(uInput);
592 assert offDisp is None or iMod != 0;
593 if (iOp2 != 5 and iOp2 != 13) or iMod != 0:
594 oGen.write(' call VBINSTST_NAME(%s)\n'
595 % (oGen.needGRegMemSetup(cAddrBits, cbEffOp, iOp2, offDisp),));
596 else:
597 oGen.write(' call VBINSTST_NAME(Common_SetupMemReadU%u)\n' % (cbEffOp*8,));
598 oGen.write(' push %s\n' % (oGen.oTarget.asGRegs[iOp2],));
599 return True;
600
601 ## @}
602
603 def generateOneStdTestGregGreg(self, oGen, cbEffOp, cbMaxOp, iOp1, iOp1X, iOp2, iOp2X, uInput, uResult):
604 """ Generate one standard instr greg,greg test. """
605 oGen.write(' call VBINSTST_NAME(Common_LoadKnownValues)\n');
606 oGen.write(' mov %s, 0x%x\n' % (oGen.oTarget.asGRegs[iOp2X], uInput,));
607 if iOp1X != iOp2X:
608 oGen.write(' push %s\n' % (oGen.oTarget.asGRegs[iOp2X],));
609 self.writeInstrGregGreg(cbEffOp, iOp1, iOp2, oGen);
610 oGen.pushConst(uResult);
611 oGen.write(' call VBINSTST_NAME(%s)\n' % (oGen.needGRegChecker(iOp1X, iOp2X if iOp1X != iOp2X else None),));
612 _ = cbMaxOp;
613 return True;
614
615 def generateOneStdTestGregGreg8BitHighPain(self, oGen, cbEffOp, cbMaxOp, iOp1, iOp2, uInput):
616 """ High 8-bit registers are a real pain! """
617 assert oGen.oTarget.is8BitHighGReg(cbEffOp, iOp1) or oGen.oTarget.is8BitHighGReg(cbEffOp, iOp2);
618 # Figure out the register indexes of the max op sized regs involved.
619 iOp1X = iOp1 & 3;
620 iOp2X = iOp2 & 3;
621 oGen.write(' ; iOp1=%u iOp1X=%u iOp2=%u iOp2X=%u\n' % (iOp1, iOp1X, iOp2, iOp2X,));
622
623 # Calculate unshifted result.
624 if iOp1X != iOp2X:
625 uCur = oGen.auRegValues[iOp1X];
626 if oGen.oTarget.is8BitHighGReg(cbEffOp, iOp1):
627 uCur = rotateRightUxx(cbMaxOp * 8, uCur, 8);
628 else:
629 uCur = uInput;
630 if oGen.oTarget.is8BitHighGReg(cbEffOp, iOp1) != oGen.oTarget.is8BitHighGReg(cbEffOp, iOp2):
631 if oGen.oTarget.is8BitHighGReg(cbEffOp, iOp1):
632 uCur = rotateRightUxx(cbMaxOp * 8, uCur, 8);
633 else:
634 uCur = rotateLeftUxx(cbMaxOp * 8, uCur, 8);
635 uResult = self.fnCalcResult(cbEffOp, uInput, uCur, oGen);
636
637
638 # Rotate the input and/or result to match their max-op-sized registers.
639 if oGen.oTarget.is8BitHighGReg(cbEffOp, iOp2):
640 uInput = rotateLeftUxx(cbMaxOp * 8, uInput, 8);
641 if oGen.oTarget.is8BitHighGReg(cbEffOp, iOp1):
642 uResult = rotateLeftUxx(cbMaxOp * 8, uResult, 8);
643
644 # Hand it over to an overridable worker method.
645 return self.generateOneStdTestGregGreg(oGen, cbEffOp, cbMaxOp, iOp1, iOp1X, iOp2, iOp2X, uInput, uResult);
646
647
648 def generateOneStdTestGregMemNoSib(self, oGen, cAddrBits, cbEffOp, cbMaxOp, iOp1, iOp2, uInput, uResult):
649 """ Generate mode 0, 1 and 2 test for the R/M=iOp2. """
650 if cAddrBits == 16:
651 _ = cbMaxOp;
652 else:
653 iMod = 0; # No disp, except for i=5.
654 oGen.write(' call VBINSTST_NAME(Common_LoadKnownValues)\n');
655 self.generateMemSetupPureRM(oGen, cAddrBits, cbEffOp, iOp2, iMod, uInput);
656 self.writeInstrGregPureRM(cbEffOp, iOp1, cAddrBits, iOp2, iMod, None, oGen);
657 oGen.pushConst(uResult);
658 oGen.write(' call VBINSTST_NAME(%s)\n' % (oGen.needGRegChecker(iOp1, iOp2),));
659
660 if iOp2 != 5 and iOp2 != 13:
661 iMod = 1;
662 for offDisp in oGen.getDispForMod(iMod):
663 oGen.write(' call VBINSTST_NAME(Common_LoadKnownValues)\n');
664 self.generateMemSetupPureRM(oGen, cAddrBits, cbEffOp, iOp2, iMod, uInput, offDisp);
665 self.writeInstrGregPureRM(cbEffOp, iOp1, cAddrBits, iOp2, iMod, offDisp, oGen);
666 oGen.pushConst(uResult);
667 oGen.write(' call VBINSTST_NAME(%s)\n' % (oGen.needGRegChecker(iOp1, iOp2),));
668
669 iMod = 2;
670 for offDisp in oGen.getDispForMod(iMod):
671 oGen.write(' call VBINSTST_NAME(Common_LoadKnownValues)\n');
672 self.generateMemSetupPureRM(oGen, cAddrBits, cbEffOp, iOp2, iMod, uInput, offDisp);
673 self.writeInstrGregPureRM(cbEffOp, iOp1, cAddrBits, iOp2, iMod, offDisp, oGen);
674 oGen.pushConst(uResult);
675 oGen.write(' call VBINSTST_NAME(%s)\n' % (oGen.needGRegChecker(iOp1, iOp2),));
676
677 return True;
678
679 def generateOneStdTestGregMemSib(self, oGen, cAddrBits, cbEffOp, cbMaxOp, iOp1, iMod, # pylint: disable=R0913
680 iBaseReg, iIndexReg, iScale, uInput, uResult):
681 """ Generate one SIB variations. """
682 for offDisp in oGen.getDispForMod(iMod, cbEffOp):
683 if ((iBaseReg == 5 or iBaseReg == 13) and iMod == 0):
684 if iIndexReg == 4:
685 if cAddrBits == 64:
686 continue; # skipping.
687 oGen.write(' call VBINSTST_NAME(Common_LoadKnownValues)\n');
688 self.generateMemSetupReadByLabel(oGen, cbEffOp, uInput);
689 self.writeInstrGregSibLabel(cbEffOp, iOp1, cAddrBits, iBaseReg, iIndexReg, iScale, offDisp, oGen);
690 sChecker = oGen.needGRegChecker(iOp1);
691 else:
692 oGen.write(' call VBINSTST_NAME(Common_LoadKnownValues)\n');
693 self.generateMemSetupReadByScaledReg(oGen, cAddrBits, cbEffOp, iIndexReg, iScale, uInput, offDisp);
694 self.writeInstrGregSibScaledReg(cbEffOp, iOp1, cAddrBits, iBaseReg, iIndexReg, iScale, offDisp, oGen);
695 sChecker = oGen.needGRegChecker(iOp1, iIndexReg);
696 else:
697 oGen.write(' call VBINSTST_NAME(Common_LoadKnownValues)\n');
698 if iIndexReg == 4:
699 self.generateMemSetupReadByReg(oGen, cAddrBits, cbEffOp, iBaseReg, uInput, offDisp);
700 self.writeInstrGregSibBase(cbEffOp, iOp1, cAddrBits, iBaseReg, iIndexReg, iScale, offDisp, oGen);
701 sChecker = oGen.needGRegChecker(iOp1, iBaseReg);
702 else:
703 if iIndexReg == iBaseReg and iScale == 1 and offDisp is not None and (offDisp & 1):
704 if offDisp < 0: offDisp += 1;
705 else: offDisp -= 1;
706 self.generateMemSetupReadByBaseAndScaledReg(oGen, cAddrBits, cbEffOp, iBaseReg,
707 iIndexReg, iScale, uInput, offDisp);
708 self.writeInstrGregSibBaseAndScaledReg(cbEffOp, iOp1, cAddrBits, iBaseReg, iIndexReg, iScale, offDisp, oGen);
709 sChecker = oGen.needGRegChecker(iOp1, iBaseReg, iIndexReg);
710 oGen.pushConst(uResult);
711 oGen.write(' call VBINSTST_NAME(%s)\n' % (sChecker,));
712 _ = cbMaxOp;
713 return True;
714
715 def generateStdTestGregMemSib(self, oGen, cAddrBits, cbEffOp, cbMaxOp, iOp1, auInputs):
716 """ Generate all SIB variations for the given iOp1 (reg) value. """
717 assert cAddrBits in [32, 64];
718 for iBaseReg in range(oGen.oTarget.getGRegCount(cAddrBits / 8)):
719 for iIndexReg in range(oGen.oTarget.getGRegCount(cAddrBits / 8)):
720 if iBaseReg == 4 or iIndexReg == 4: # no RSP testing atm.
721 continue;
722 for iMod in [0, 1, 2]:
723 if iBaseReg == iOp1 and ((iBaseReg != 5 and iBaseReg != 13) or iMod != 0) and cAddrBits != cbMaxOp:
724 continue; # Don't know the high bit of the address ending up the result - skip it for now.
725 if iIndexReg == iOp1 and iIndexReg != 4 and cAddrBits != cbMaxOp:
726 continue; # Don't know the high bit of the address ending up the result - skip it for now.
727 for iScale in (1, 2, 4, 8):
728 for uInput in auInputs:
729 oGen.newSubTest();
730 uResult = self.fnCalcResult(cbEffOp, uInput, oGen.auRegValues[iOp1], oGen);
731 self.generateOneStdTestGregMemSib(oGen, cAddrBits, cbEffOp, cbMaxOp, iOp1, iMod,
732 iBaseReg, iIndexReg, iScale, uInput, uResult);
733
734 return True;
735
736
737 def generateStandardTests(self, oGen):
738 """ Generate standard tests. """
739
740 # Parameters.
741 cbDefOp = oGen.oTarget.getDefOpBytes();
742 cbMaxOp = oGen.oTarget.getMaxOpBytes();
743 auShortInputs = self.generateInputs(cbDefOp, cbMaxOp, oGen);
744 auLongInputs = self.generateInputs(cbDefOp, cbMaxOp, oGen, fLong = True);
745 iLongOp1 = oGen.oTarget.randGRegNoSp();
746 iLongOp2 = oGen.oTarget.randGRegNoSp();
747 oOp1MemRange = range(oGen.oTarget.getGRegCount());
748 oOp2Range = None;
749 if oGen.oOptions.sTestSize == InstructionTestGen.ksTestSize_Tiny:
750 oOp2Range = [iLongOp2,];
751 oOp1MemRange = [iLongOp1,];
752
753 # Register tests
754 if False:
755 for cbEffOp in self.acbOpVars:
756 if cbEffOp > cbMaxOp:
757 continue;
758 oOp2Range = range(oGen.oTarget.getGRegCount(cbEffOp));
759 if oGen.oOptions.sTestSize == InstructionTestGen.ksTestSize_Tiny:
760 oOp2Range = [iLongOp2,];
761 oGen.write('; cbEffOp=%u\n' % (cbEffOp,));
762
763 for iOp1 in range(oGen.oTarget.getGRegCount(cbEffOp)):
764 if iOp1 == X86_GREG_xSP:
765 continue; # Cannot test xSP atm.
766 for iOp2 in oOp2Range:
767 if (iOp2 >= 16 and iOp1 in range(4, 16)) \
768 or (iOp1 >= 16 and iOp2 in range(4, 16)):
769 continue; # Any REX encoding turns AH,CH,DH,BH regs into SPL,BPL,SIL,DIL.
770 if iOp2 == X86_GREG_xSP:
771 continue; # Cannot test xSP atm.
772
773 oGen.write('; iOp2=%u cbEffOp=%u\n' % (iOp2, cbEffOp));
774 for uInput in (auLongInputs if iOp1 == iLongOp1 and iOp2 == iLongOp2 else auShortInputs):
775 oGen.newSubTest();
776 if not oGen.oTarget.is8BitHighGReg(cbEffOp, iOp1) and not oGen.oTarget.is8BitHighGReg(cbEffOp, iOp2):
777 uCur = oGen.auRegValues[iOp1 & 15] if iOp1 != iOp2 else uInput;
778 uResult = self.fnCalcResult(cbEffOp, uInput, uCur, oGen);
779 self.generateOneStdTestGregGreg(oGen, cbEffOp, cbMaxOp, iOp1, iOp1 & 15, iOp2, iOp2 & 15,
780 uInput, uResult);
781 else:
782 self.generateOneStdTestGregGreg8BitHighPain(oGen, cbEffOp, cbMaxOp, iOp1, iOp2, uInput);
783
784 # Memory test.
785 if True:
786 for cbEffOp in self.acbOpVars:
787 if cbEffOp > cbMaxOp:
788 continue;
789 for iOp1 in oOp1MemRange:
790 if iOp1 == X86_GREG_xSP:
791 continue; # Cannot test xSP atm.
792 for cAddrBits in oGen.oTarget.getAddrModes():
793 auInputs = auLongInputs if iOp1 == iLongOp1 and False else auShortInputs;
794 for iOp2 in range(oGen.oTarget.getGRegCount(cAddrBits / 8)):
795 if iOp2 != 4 or cAddrBits == 16:
796 for uInput in auInputs:
797 oGen.newSubTest();
798 if iOp1 == iOp2 and iOp2 != 5 and iOp2 != 13 and cbEffOp != cbMaxOp:
799 continue; # Don't know the high bit of the address ending up the result - skip it for now.
800 uResult = self.fnCalcResult(cbEffOp, uInput, oGen.auRegValues[iOp1], oGen);
801 self.generateOneStdTestGregMemNoSib(oGen, cAddrBits, cbEffOp, cbMaxOp,
802 iOp1, iOp2, uInput, uResult);
803 else:
804 # SIB.
805 self.generateStdTestGregMemSib(oGen, cAddrBits, cbEffOp, cbMaxOp, iOp1, auInputs);
806 break;
807 break;
808
809
810 return True;
811
812 def generateTest(self, oGen, sTestFnName):
813 oGen.write('VBINSTST_BEGINPROC %s\n' % (sTestFnName,));
814 #oGen.write(' int3\n');
815
816 self.generateStandardTests(oGen);
817
818 #oGen.write(' int3\n');
819 oGen.write(' ret\n');
820 oGen.write('VBINSTST_ENDPROC %s\n' % (sTestFnName,));
821 return True;
822
823
824
825class InstrTest_Mov_Gv_Ev(InstrTest_MemOrGreg_2_Greg):
826 """
827 Tests MOV Gv,Ev.
828 """
829 def __init__(self):
830 InstrTest_MemOrGreg_2_Greg.__init__(self, 'mov Gv,Ev', self.calc_mov);
831
832 @staticmethod
833 def calc_mov(cbEffOp, uInput, uCur, oGen):
834 """ Calculates the result of a mov instruction."""
835 if cbEffOp == 8:
836 return uInput & UINT64_MAX;
837 if cbEffOp == 4:
838 return uInput & UINT32_MAX;
839 if cbEffOp == 2:
840 return (uCur & 0xffffffffffff0000) | (uInput & UINT16_MAX);
841 assert cbEffOp == 1; _ = oGen;
842 return (uCur & 0xffffffffffffff00) | (uInput & UINT8_MAX);
843
844
845class InstrTest_MovSxD_Gv_Ev(InstrTest_MemOrGreg_2_Greg):
846 """
847 Tests MOVSXD Gv,Ev.
848 """
849 def __init__(self):
850 InstrTest_MemOrGreg_2_Greg.__init__(self, 'movsxd Gv,Ev', self.calc_movsxd, acbOpVars = [ 8, 4, 2, ]);
851
852 def writeInstrGregGreg(self, cbEffOp, iOp1, iOp2, oGen):
853 if cbEffOp == 8:
854 oGen.write(' %s %s, %s\n' % (self.sInstr, g_asGRegs64[iOp1], g_asGRegs32[iOp2]));
855 elif cbEffOp == 4 or cbEffOp == 2:
856 abInstr = [];
857 if cbEffOp != oGen.oTarget.getDefOpBytes():
858 abInstr.append(X86_OP_PRF_SIZE_OP);
859 abInstr += calcRexPrefixForTwoModRmRegs(iOp1, iOp2);
860 abInstr.append(0x63);
861 abInstr += calcModRmForTwoRegs(iOp1, iOp2);
862 oGen.writeInstrBytes(abInstr);
863 else:
864 assert False;
865 assert False;
866 return True;
867
868 def isApplicable(self, oGen):
869 return oGen.oTarget.is64Bit();
870
871 @staticmethod
872 def calc_movsxd(cbEffOp, uInput, uCur, oGen):
873 """
874 Calculates the result of a movxsd instruction.
875 Returns the result value (cbMaxOp sized).
876 """
877 _ = oGen;
878 if cbEffOp == 8 and (uInput & RT_BIT_32(31)):
879 return (UINT32_MAX << 32) | (uInput & UINT32_MAX);
880 if cbEffOp == 2:
881 return (uCur & 0xffffffffffff0000) | (uInput & 0xffff);
882 return uInput & UINT32_MAX;
883
884
885## Instruction Tests.
886g_aoInstructionTests = [
887 InstrTest_Mov_Gv_Ev(),
888 #InstrTest_MovSxD_Gv_Ev(),
889];
890
891
892class InstructionTestGen(object):
893 """
894 Instruction Test Generator.
895 """
896
897 ## @name Test size
898 ## @{
899 ksTestSize_Large = 'large';
900 ksTestSize_Medium = 'medium';
901 ksTestSize_Tiny = 'tiny';
902 ## @}
903 kasTestSizes = ( ksTestSize_Large, ksTestSize_Medium, ksTestSize_Tiny );
904
905
906
907 def __init__(self, oOptions):
908 self.oOptions = oOptions;
909 self.oTarget = g_dTargetEnvs[oOptions.sTargetEnv];
910
911 # Calculate the number of output files.
912 self.cFiles = 1;
913 if len(g_aoInstructionTests) > self.oOptions.cInstrPerFile:
914 self.cFiles = len(g_aoInstructionTests) / self.oOptions.cInstrPerFile;
915 if self.cFiles * self.oOptions.cInstrPerFile < len(g_aoInstructionTests):
916 self.cFiles += 1;
917
918 # Fix the known register values.
919 self.au64Regs = randUxxList(64, 16);
920 self.au32Regs = [(self.au64Regs[i] & UINT32_MAX) for i in range(8)];
921 self.au16Regs = [(self.au64Regs[i] & UINT16_MAX) for i in range(8)];
922 self.auRegValues = self.au64Regs if self.oTarget.is64Bit() else self.au32Regs;
923
924 # Declare state variables used while generating.
925 self.oFile = sys.stderr;
926 self.iFile = -1;
927 self.sFile = '';
928 self.dCheckFns = dict();
929 self.dMemSetupFns = dict();
930 self.d64BitConsts = dict();
931
932
933 #
934 # Methods used by instruction tests.
935 #
936
937 def write(self, sText):
938 """ Writes to the current output file. """
939 return self.oFile.write(unicode(sText));
940
941 def writeln(self, sText):
942 """ Writes a line to the current output file. """
943 self.write(sText);
944 return self.write('\n');
945
946 def writeInstrBytes(self, abInstr):
947 """
948 Emits an instruction given as a sequence of bytes values.
949 """
950 self.write(' db %#04x' % (abInstr[0],));
951 for i in range(1, len(abInstr)):
952 self.write(', %#04x' % (abInstr[i],));
953 return self.write('\n');
954
955 def newSubTest(self):
956 """
957 Indicates that a new subtest has started.
958 """
959 self.write(' mov dword [VBINSTST_NAME(g_uVBInsTstSubTestIndicator) xWrtRIP], __LINE__\n');
960 return True;
961
962 def needGRegChecker(self, iReg1, iReg2 = None, iReg3 = None):
963 """
964 Records the need for a given register checker function, returning its label.
965 """
966 if iReg2 is not None:
967 if iReg3 is not None:
968 sName = '%s_%s_%s' % (self.oTarget.asGRegs[iReg1], self.oTarget.asGRegs[iReg2], self.oTarget.asGRegs[iReg3],);
969 else:
970 sName = '%s_%s' % (self.oTarget.asGRegs[iReg1], self.oTarget.asGRegs[iReg2],);
971 else:
972 sName = '%s' % (self.oTarget.asGRegs[iReg1],);
973 assert iReg3 is None;
974
975 if sName in self.dCheckFns:
976 self.dCheckFns[sName] += 1;
977 else:
978 self.dCheckFns[sName] = 1;
979
980 return 'Common_Check_' + sName;
981
982 def needGRegMemSetup(self, cAddrBits, cbEffOp, iBaseReg = None, offDisp = None, iIndexReg = None, iScale = 1):
983 """
984 Records the need for a given register checker function, returning its label.
985 """
986 sName = '%ubit_U%u' % (cAddrBits, cbEffOp * 8,);
987 if iBaseReg is not None:
988 sName += '_%s' % (gregName(iBaseReg, cAddrBits),);
989 sName += '_x%u' % (iScale,);
990 if iIndexReg is not None:
991 sName += '_%s' % (gregName(iIndexReg, cAddrBits),);
992 if offDisp is not None:
993 sName += '_%#010x' % (offDisp & UINT32_MAX, );
994 if sName in self.dMemSetupFns:
995 self.dMemSetupFns[sName] += 1;
996 else:
997 self.dMemSetupFns[sName] = 1;
998 return 'Common_MemSetup_' + sName;
999
1000 def need64BitConstant(self, uVal):
1001 """
1002 Records the need for a 64-bit constant, returning its label.
1003 These constants are pooled to attempt reduce the size of the whole thing.
1004 """
1005 assert uVal >= 0 and uVal <= UINT64_MAX;
1006 if uVal in self.d64BitConsts:
1007 self.d64BitConsts[uVal] += 1;
1008 else:
1009 self.d64BitConsts[uVal] = 1;
1010 return 'g_u64Const_0x%016x' % (uVal, );
1011
1012 def pushConst(self, uResult):
1013 """
1014 Emits a push constant value, taking care of high values on 64-bit hosts.
1015 """
1016 if self.oTarget.is64Bit() and uResult >= 0x80000000:
1017 self.write(' push qword [%s wrt rip]\n' % (self.need64BitConstant(uResult),));
1018 else:
1019 self.write(' push dword 0x%x\n' % (uResult,));
1020 return True;
1021
1022 def getDispForMod(self, iMod, cbAlignment = 1):
1023 """
1024 Get a set of address dispositions for a given addressing mode.
1025 The alignment restriction is for SIB scaling.
1026 """
1027 assert cbAlignment in [1, 2, 4, 8];
1028 if iMod == 0:
1029 aoffDisp = [ None, ];
1030 elif iMod == 1:
1031 aoffDisp = [ 127 & ~(cbAlignment - 1), -128 ];
1032 elif iMod == 2:
1033 aoffDisp = [ 2147483647 & ~(cbAlignment - 1), -2147483648 ];
1034 else: assert False;
1035 return aoffDisp;
1036
1037
1038 #
1039 # Internal machinery.
1040 #
1041
1042 def _calcTestFunctionName(self, oInstrTest, iInstrTest):
1043 """
1044 Calc a test function name for the given instruction test.
1045 """
1046 sName = 'TestInstr%03u_%s' % (iInstrTest, oInstrTest.sName);
1047 return sName.replace(',', '_').replace(' ', '_').replace('%', '_');
1048
1049 def _generateFileHeader(self, ):
1050 """
1051 Writes the file header.
1052 Raises exception on trouble.
1053 """
1054 self.write('; $Id: InstructionTestGen.py 46875 2013-06-29 02:15:38Z vboxsync $\n'
1055 ';; @file %s\n'
1056 '; Autogenerate by %s %s. DO NOT EDIT\n'
1057 ';\n'
1058 '\n'
1059 ';\n'
1060 '; Headers\n'
1061 ';\n'
1062 '%%include "env-%s.mac"\n'
1063 % ( os.path.basename(self.sFile),
1064 os.path.basename(__file__), __version__[11:-1],
1065 self.oTarget.sName,
1066 ) );
1067 # Target environment specific init stuff.
1068
1069 #
1070 # Global variables.
1071 #
1072 self.write('\n\n'
1073 ';\n'
1074 '; Globals\n'
1075 ';\n');
1076 self.write('VBINSTST_BEGINDATA\n'
1077 'VBINSTST_GLOBALNAME_EX g_pvLow16Mem4K, data hidden\n'
1078 ' dq 0\n'
1079 'VBINSTST_GLOBALNAME_EX g_pvLow32Mem4K, data hidden\n'
1080 ' dq 0\n'
1081 'VBINSTST_GLOBALNAME_EX g_pvMem4K, data hidden\n'
1082 ' dq 0\n'
1083 'VBINSTST_GLOBALNAME_EX g_uVBInsTstSubTestIndicator, data hidden\n'
1084 ' dd 0\n'
1085 'VBINSTST_BEGINCODE\n'
1086 );
1087 self.write('%ifdef RT_ARCH_AMD64\n');
1088 for i in range(len(g_asGRegs64)):
1089 self.write('g_u64KnownValue_%s: dq 0x%x\n' % (g_asGRegs64[i], self.au64Regs[i]));
1090 self.write('%endif\n\n')
1091
1092 #
1093 # Common functions.
1094 #
1095
1096 # Loading common values.
1097 self.write('\n\n'
1098 'VBINSTST_BEGINPROC Common_LoadKnownValues\n'
1099 '%ifdef RT_ARCH_AMD64\n');
1100 for i in range(len(g_asGRegs64NoSp)):
1101 if g_asGRegs64NoSp[i]:
1102 self.write(' mov %s, 0x%x\n' % (g_asGRegs64NoSp[i], self.au64Regs[i],));
1103 self.write('%else\n');
1104 for i in range(8):
1105 if g_asGRegs32NoSp[i]:
1106 self.write(' mov %s, 0x%x\n' % (g_asGRegs32NoSp[i], self.au32Regs[i],));
1107 self.write('%endif\n'
1108 ' ret\n'
1109 'VBINSTST_ENDPROC Common_LoadKnownValues\n'
1110 '\n');
1111
1112 self.write('VBINSTST_BEGINPROC Common_CheckKnownValues\n'
1113 '%ifdef RT_ARCH_AMD64\n');
1114 for i in range(len(g_asGRegs64NoSp)):
1115 if g_asGRegs64NoSp[i]:
1116 self.write(' cmp %s, [g_u64KnownValue_%s wrt rip]\n'
1117 ' je .ok_%u\n'
1118 ' push %u ; register number\n'
1119 ' push %s ; actual\n'
1120 ' push qword [g_u64KnownValue_%s wrt rip] ; expected\n'
1121 ' call VBINSTST_NAME(Common_BadValue)\n'
1122 '.ok_%u:\n'
1123 % ( g_asGRegs64NoSp[i], g_asGRegs64NoSp[i], i, i, g_asGRegs64NoSp[i], g_asGRegs64NoSp[i], i,));
1124 self.write('%else\n');
1125 for i in range(8):
1126 if g_asGRegs32NoSp[i]:
1127 self.write(' cmp %s, 0x%x\n'
1128 ' je .ok_%u\n'
1129 ' push %u ; register number\n'
1130 ' push %s ; actual\n'
1131 ' push dword 0x%x ; expected\n'
1132 ' call VBINSTST_NAME(Common_BadValue)\n'
1133 '.ok_%u:\n'
1134 % ( g_asGRegs32NoSp[i], self.au32Regs[i], i, i, g_asGRegs32NoSp[i], self.au32Regs[i], i,));
1135 self.write('%endif\n'
1136 ' ret\n'
1137 'VBINSTST_ENDPROC Common_CheckKnownValues\n'
1138 '\n');
1139
1140 return True;
1141
1142 def _generateMemSetupFunctions(self):
1143 """
1144 Generates the memory setup functions.
1145 """
1146 cDefAddrBits = self.oTarget.getDefAddrBits();
1147 for sName in self.dMemSetupFns:
1148 # Unpack it.
1149 asParams = sName.split('_');
1150 cAddrBits = int(asParams[0][:-3]); assert asParams[0][-3:] == 'bit';
1151 cEffOpBits = int(asParams[1][1:]); assert asParams[1][0] == 'U';
1152 if cAddrBits == 64: asAddrGRegs = g_asGRegs64;
1153 elif cAddrBits == 32: asAddrGRegs = g_asGRegs32;
1154 else: asAddrGRegs = g_asGRegs16;
1155
1156 i = 2;
1157 iBaseReg = None;
1158 sBaseReg = None;
1159 if i < len(asParams) and asParams[i] in asAddrGRegs:
1160 sBaseReg = asParams[i];
1161 iBaseReg = asAddrGRegs.index(sBaseReg);
1162 i += 1
1163
1164 assert i < len(asParams); assert asParams[i][0] == 'x';
1165 iScale = iScale = int(asParams[i][1:]); assert iScale in [1, 2, 4, 8];
1166 i += 1;
1167
1168 sIndexReg = None;
1169 iIndexReg = None;
1170 if i < len(asParams) and asParams[i] in asAddrGRegs:
1171 sIndexReg = asParams[i];
1172 iIndexReg = asAddrGRegs.index(sIndexReg);
1173 i += 1;
1174
1175 u32Disp = None;
1176 if i < len(asParams) and len(asParams[i]) == 10:
1177 u32Disp = long(asParams[i], 16);
1178 i += 1;
1179
1180 assert i == len(asParams), 'i=%d len=%d len[i]=%d (%s)' % (i, len(asParams), len(asParams[i]), asParams[i],);
1181 assert iScale == 1 or iIndexReg is not None;
1182
1183 # Find a temporary register.
1184 iTmpReg1 = X86_GREG_xCX;
1185 while iTmpReg1 in [iBaseReg, iIndexReg]:
1186 iTmpReg1 += 1;
1187
1188 # Prologue.
1189 self.write('\n\n'
1190 '; cAddrBits=%s cEffOpBits=%s iBaseReg=%s u32Disp=%s iIndexReg=%s iScale=%s\n'
1191 'VBINSTST_BEGINPROC Common_MemSetup_%s\n'
1192 ' MY_PUSH_FLAGS\n'
1193 ' push %s\n'
1194 % ( cAddrBits, cEffOpBits, iBaseReg, u32Disp, iIndexReg, iScale,
1195 sName, self.oTarget.asGRegs[iTmpReg1], ));
1196
1197 # Figure out what to use.
1198 if cEffOpBits == 64:
1199 sTmpReg1 = g_asGRegs64[iTmpReg1];
1200 sDataVar = 'VBINSTST_NAME(g_u64Data)';
1201 elif cEffOpBits == 32:
1202 sTmpReg1 = g_asGRegs32[iTmpReg1];
1203 sDataVar = 'VBINSTST_NAME(g_u32Data)';
1204 elif cEffOpBits == 16:
1205 sTmpReg1 = g_asGRegs16[iTmpReg1];
1206 sDataVar = 'VBINSTST_NAME(g_u16Data)';
1207 else:
1208 assert cEffOpBits == 8; assert iTmpReg1 < 4;
1209 sTmpReg1 = g_asGRegs8Rex[iTmpReg1];
1210 sDataVar = 'VBINSTST_NAME(g_u8Data)';
1211
1212 # Special case: reg + reg * [2,4,8]
1213 if iBaseReg == iIndexReg and iBaseReg is not None and iScale != 1:
1214 iTmpReg2 = X86_GREG_xBP;
1215 while iTmpReg2 in [iBaseReg, iIndexReg, iTmpReg1]:
1216 iTmpReg2 += 1;
1217 sTmpReg2 = gregName(iTmpReg2, cAddrBits);
1218 self.write(' push sAX\n'
1219 ' push %s\n'
1220 ' push sDX\n'
1221 % (self.oTarget.asGRegs[iTmpReg2],));
1222 if cAddrBits == 16:
1223 self.write(' mov %s, [VBINSTST_NAME(g_pvLow16Mem4K) xWrtRIP]\n' % (sTmpReg2,));
1224 else:
1225 self.write(' mov %s, [VBINSTST_NAME(g_pvLow32Mem4K) xWrtRIP]\n' % (sTmpReg2,));
1226 self.write(' add %s, 0x200\n' % (sTmpReg2,));
1227 self.write(' mov %s, %s\n' % (gregName(X86_GREG_xAX, cAddrBits), sTmpReg2,));
1228 if u32Disp is not None:
1229 self.write(' sub %s, %d\n' % ( gregName(X86_GREG_xAX, cAddrBits), convU32ToSigned(u32Disp), ));
1230 self.write(' xor edx, edx\n'
1231 '%if xCB == 2\n'
1232 ' push 0\n'
1233 '%endif\n');
1234 self.write(' push %u\n' % (iScale + 1,));
1235 self.write(' div %s [xSP]\n' % ('qword' if cAddrBits == 64 else 'dword',));
1236 self.write(' sub %s, %s\n' % (sTmpReg2, gregName(X86_GREG_xDX, cAddrBits),));
1237 self.write(' pop sDX\n'
1238 ' pop sDX\n'); # sTmpReg2 is eff address; sAX is sIndexReg value.
1239 # Note! sTmpReg1 can be xDX and that's no problem now.
1240 self.write(' mov %s, [xSP + sCB*3 + MY_PUSH_FLAGS_SIZE + xCB]\n' % (sTmpReg1,));
1241 self.write(' mov [%s], %s\n' % (sTmpReg2, sTmpReg1,)); # Value in place.
1242 self.write(' pop %s\n' % (self.oTarget.asGRegs[iTmpReg2],));
1243 if iBaseReg == X86_GREG_xAX:
1244 self.write(' pop %s\n' % (self.oTarget.asGRegs[iTmpReg1],));
1245 else:
1246 self.write(' mov %s, %s\n' % (sBaseReg, gregName(X86_GREG_xAX, cAddrBits),));
1247 self.write(' pop sAX\n');
1248
1249 else:
1250 # Load the value and mem address, storing the value there.
1251 # Note! ASSUMES that the scale and disposition works fine together.
1252 sAddrReg = sBaseReg if sBaseReg is not None else sIndexReg;
1253 self.write(' mov %s, [xSP + sCB + MY_PUSH_FLAGS_SIZE + xCB]\n' % (sTmpReg1,));
1254 if cAddrBits >= cDefAddrBits:
1255 self.write(' mov [%s xWrtRIP], %s\n' % (sDataVar, sTmpReg1,));
1256 self.write(' lea %s, [%s xWrtRIP]\n' % (sAddrReg, sDataVar,));
1257 else:
1258 if cAddrBits == 16:
1259 self.write(' mov %s, [VBINSTST_NAME(g_pvLow16Mem4K) xWrtRIP]\n' % (sAddrReg,));
1260 else:
1261 self.write(' mov %s, [VBINSTST_NAME(g_pvLow32Mem4K) xWrtRIP]\n' % (sAddrReg,));
1262 self.write(' add %s, %s\n' % (sAddrReg, (randU16() << cEffOpBits) & 0xfff, ));
1263 self.write(' mov [%s], %s\n' % (sAddrReg, sTmpReg1, ));
1264
1265 # Adjust for disposition and scaling.
1266 if u32Disp is not None:
1267 self.write(' sub %s, %d\n' % ( sAddrReg, convU32ToSigned(u32Disp), ));
1268 if iIndexReg is not None:
1269 if iBaseReg == iIndexReg:
1270 assert iScale == 1;
1271 assert u32Disp is None or (u32Disp & 1) == 0;
1272 self.write(' shr %s, 1\n' % (sIndexReg,));
1273 elif sBaseReg is not None:
1274 uIdxRegVal = randUxx(cAddrBits);
1275 if cAddrBits == 64:
1276 self.write(' mov %s, %u\n'
1277 ' sub %s, %s\n'
1278 ' mov %s, %u\n'
1279 % ( sIndexReg, (uIdxRegVal * iScale) & UINT64_MAX,
1280 sBaseReg, sIndexReg,
1281 sIndexReg, uIdxRegVal, ));
1282 else:
1283 assert cAddrBits == 32;
1284 self.write(' mov %s, %u\n'
1285 ' sub %s, %#06x\n'
1286 % ( sIndexReg, uIdxRegVal, sBaseReg, (uIdxRegVal * iScale) & UINT32_MAX, ));
1287 elif iScale == 2:
1288 assert u32Disp is None or (u32Disp & 1) == 0;
1289 self.write(' shr %s, 1\n' % (sIndexReg,));
1290 elif iScale == 4:
1291 assert u32Disp is None or (u32Disp & 3) == 0;
1292 self.write(' shr %s, 2\n' % (sIndexReg,));
1293 elif iScale == 8:
1294 assert u32Disp is None or (u32Disp & 7) == 0;
1295 self.write(' shr %s, 3\n' % (sIndexReg,));
1296 else:
1297 assert iScale == 1;
1298
1299 # Set upper bits that's supposed to be unused.
1300 if cDefAddrBits > cAddrBits or cAddrBits == 16:
1301 if cDefAddrBits == 64:
1302 assert cAddrBits == 32;
1303 if iBaseReg is not None:
1304 self.write(' mov %s, %#018x\n'
1305 ' or %s, %s\n'
1306 % ( g_asGRegs64[iTmpReg1], randU64() & 0xffffffff00000000,
1307 g_asGRegs64[iBaseReg], g_asGRegs64[iTmpReg1],));
1308 if iIndexReg is not None and iIndexReg != iBaseReg:
1309 self.write(' mov %s, %#018x\n'
1310 ' or %s, %s\n'
1311 % ( g_asGRegs64[iTmpReg1], randU64() & 0xffffffff00000000,
1312 g_asGRegs64[iIndexReg], g_asGRegs64[iTmpReg1],));
1313 else:
1314 assert cDefAddrBits == 32; assert cAddrBits == 16; assert iIndexReg is None;
1315 if iBaseReg is not None:
1316 self.write(' or %s, %#010x\n'
1317 % ( g_asGRegs32[iBaseReg], randU32() & 0xffff0000, ));
1318
1319 # Epilogue.
1320 self.write(' pop %s\n'
1321 ' MY_POP_FLAGS\n'
1322 ' ret sCB\n'
1323 'VBINSTST_ENDPROC Common_MemSetup_%s\n'
1324 % ( self.oTarget.asGRegs[iTmpReg1], sName,));
1325
1326
1327 def _generateFileFooter(self):
1328 """
1329 Generates file footer.
1330 """
1331
1332 # Register checking functions.
1333 for sName in self.dCheckFns:
1334 asRegs = sName.split('_');
1335 sPushSize = 'dword';
1336
1337 # Prologue
1338 self.write('\n\n'
1339 '; Checks 1 or more register values, expected values pushed on the stack.\n'
1340 '; To save space, the callee cleans up the stack.'
1341 '; Ref count: %u\n'
1342 'VBINSTST_BEGINPROC Common_Check_%s\n'
1343 ' MY_PUSH_FLAGS\n'
1344 % ( self.dCheckFns[sName], sName, ) );
1345
1346 # Register checks.
1347 for i in range(len(asRegs)):
1348 sReg = asRegs[i];
1349 iReg = self.oTarget.asGRegs.index(sReg);
1350 if i == asRegs.index(sReg): # Only check once, i.e. input = output reg.
1351 self.write(' cmp %s, [xSP + MY_PUSH_FLAGS_SIZE + xCB + sCB * %u]\n'
1352 ' je .equal%u\n'
1353 ' push %s %u ; register number\n'
1354 ' push %s ; actual\n'
1355 ' mov %s, [xSP + sCB*2 + MY_PUSH_FLAGS_SIZE + xCB]\n'
1356 ' push %s ; expected\n'
1357 ' call VBINSTST_NAME(Common_BadValue)\n'
1358 ' pop %s\n'
1359 ' pop %s\n'
1360 ' pop %s\n'
1361 '.equal%u:\n'
1362 % ( sReg, i, i, sPushSize, iReg, sReg, sReg, sReg, sReg, sReg, sReg, i, ) );
1363
1364
1365 # Restore known register values and check the other registers.
1366 for sReg in asRegs:
1367 if self.oTarget.is64Bit():
1368 self.write(' mov %s, [g_u64KnownValue_%s wrt rip]\n' % (sReg, sReg,));
1369 else:
1370 iReg = self.oTarget.asGRegs.index(sReg)
1371 self.write(' mov %s, 0x%x\n' % (sReg, self.au32Regs[iReg],));
1372 self.write(' MY_POP_FLAGS\n'
1373 ' call VBINSTST_NAME(Common_CheckKnownValues)\n'
1374 ' ret sCB*%u\n'
1375 'VBINSTST_ENDPROC Common_Check_%s\n'
1376 % (len(asRegs), sName,));
1377
1378 # memory setup functions
1379 self._generateMemSetupFunctions();
1380
1381 # 64-bit constants.
1382 if len(self.d64BitConsts) > 0:
1383 self.write('\n\n'
1384 ';\n'
1385 '; 64-bit constants\n'
1386 ';\n');
1387 for uVal in self.d64BitConsts:
1388 self.write('g_u64Const_0x%016x: dq 0x%016x ; Ref count: %d\n' % (uVal, uVal, self.d64BitConsts[uVal], ) );
1389
1390 return True;
1391
1392 def _generateTests(self):
1393 """
1394 Generate the test cases.
1395 """
1396 for self.iFile in range(self.cFiles):
1397 if self.cFiles == 1:
1398 self.sFile = '%s.asm' % (self.oOptions.sOutputBase,)
1399 else:
1400 self.sFile = '%s-%u.asm' % (self.oOptions.sOutputBase, self.iFile)
1401 self.oFile = sys.stdout;
1402 if self.oOptions.sOutputBase != '-':
1403 self.oFile = io.open(self.sFile, 'w', buffering = 65536, encoding = 'utf-8');
1404
1405 self._generateFileHeader();
1406
1407 # Calc the range.
1408 iInstrTestStart = self.iFile * self.oOptions.cInstrPerFile;
1409 iInstrTestEnd = iInstrTestStart + self.oOptions.cInstrPerFile;
1410 if iInstrTestEnd > len(g_aoInstructionTests):
1411 iInstrTestEnd = len(g_aoInstructionTests);
1412
1413 # Generate the instruction tests.
1414 for iInstrTest in range(iInstrTestStart, iInstrTestEnd):
1415 oInstrTest = g_aoInstructionTests[iInstrTest];
1416 self.write('\n'
1417 '\n'
1418 ';\n'
1419 '; %s\n'
1420 ';\n'
1421 % (oInstrTest.sName,));
1422 oInstrTest.generateTest(self, self._calcTestFunctionName(oInstrTest, iInstrTest));
1423
1424 # Generate the main function.
1425 self.write('\n\n'
1426 'VBINSTST_BEGINPROC TestInstrMain\n'
1427 ' MY_PUSH_ALL\n'
1428 ' sub xSP, 40h\n'
1429 '\n');
1430
1431 for iInstrTest in range(iInstrTestStart, iInstrTestEnd):
1432 oInstrTest = g_aoInstructionTests[iInstrTest];
1433 if oInstrTest.isApplicable(self):
1434 self.write('%%ifdef ASM_CALL64_GCC\n'
1435 ' lea rdi, [.szInstr%03u wrt rip]\n'
1436 '%%elifdef ASM_CALL64_MSC\n'
1437 ' lea rcx, [.szInstr%03u wrt rip]\n'
1438 '%%else\n'
1439 ' mov xAX, .szInstr%03u\n'
1440 ' mov [xSP], xAX\n'
1441 '%%endif\n'
1442 ' VBINSTST_CALL_FN_SUB_TEST\n'
1443 ' call VBINSTST_NAME(%s)\n'
1444 % ( iInstrTest, iInstrTest, iInstrTest, self._calcTestFunctionName(oInstrTest, iInstrTest)));
1445
1446 self.write('\n'
1447 ' add xSP, 40h\n'
1448 ' MY_POP_ALL\n'
1449 ' ret\n\n');
1450 for iInstrTest in range(iInstrTestStart, iInstrTestEnd):
1451 self.write('.szInstr%03u: db \'%s\', 0\n' % (iInstrTest, g_aoInstructionTests[iInstrTest].sName,));
1452 self.write('VBINSTST_ENDPROC TestInstrMain\n\n');
1453
1454 self._generateFileFooter();
1455 if self.oOptions.sOutputBase != '-':
1456 self.oFile.close();
1457 self.oFile = None;
1458 self.sFile = '';
1459
1460 return RTEXITCODE_SUCCESS;
1461
1462 def _runMakefileMode(self):
1463 """
1464 Generate a list of output files on standard output.
1465 """
1466 if self.cFiles == 1:
1467 print('%s.asm' % (self.oOptions.sOutputBase,));
1468 else:
1469 print(' '.join('%s-%s.asm' % (self.oOptions.sOutputBase, i) for i in range(self.cFiles)));
1470 return RTEXITCODE_SUCCESS;
1471
1472 def run(self):
1473 """
1474 Generates the tests or whatever is required.
1475 """
1476 if self.oOptions.fMakefileMode:
1477 return self._runMakefileMode();
1478 return self._generateTests();
1479
1480 @staticmethod
1481 def main():
1482 """
1483 Main function a la C/C++. Returns exit code.
1484 """
1485
1486 #
1487 # Parse the command line.
1488 #
1489 oParser = OptionParser(version = __version__[11:-1].strip());
1490 oParser.add_option('--makefile-mode', dest = 'fMakefileMode', action = 'store_true', default = False,
1491 help = 'Special mode for use to output a list of output files for the benefit of '
1492 'the make program (kmk).');
1493 oParser.add_option('--split', dest = 'cInstrPerFile', metavar = '<instr-per-file>', type = 'int', default = 9999999,
1494 help = 'Number of instruction to test per output file.');
1495 oParser.add_option('--output-base', dest = 'sOutputBase', metavar = '<file>', default = None,
1496 help = 'The output file base name, no suffix please. Required.');
1497 oParser.add_option('--target', dest = 'sTargetEnv', metavar = '<target>',
1498 default = 'iprt-r3-32',
1499 choices = g_dTargetEnvs.keys(),
1500 help = 'The target environment. Choices: %s'
1501 % (', '.join(sorted(g_dTargetEnvs.keys())),));
1502 oParser.add_option('--test-size', dest = 'sTestSize', default = InstructionTestGen.ksTestSize_Medium,
1503 choices = InstructionTestGen.kasTestSizes,
1504 help = 'Selects the test size.');
1505
1506 (oOptions, asArgs) = oParser.parse_args();
1507 if len(asArgs) > 0:
1508 oParser.print_help();
1509 return RTEXITCODE_SYNTAX
1510 if oOptions.sOutputBase is None:
1511 print('syntax error: Missing required option --output-base.', file = sys.stderr);
1512 return RTEXITCODE_SYNTAX
1513
1514 #
1515 # Instantiate the program class and run it.
1516 #
1517 oProgram = InstructionTestGen(oOptions);
1518 return oProgram.run();
1519
1520
1521if __name__ == '__main__':
1522 sys.exit(InstructionTestGen.main());
1523
Note: See TracBrowser for help on using the repository browser.

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