xref: /freebsd-src/contrib/llvm-project/lldb/source/Plugins/Process/Utility/RegisterContext_x86.cpp (revision e8d8bef961a50d4dc22501cde4fb9fb0be1b2532)
1*e8d8bef9SDimitry Andric //===-- RegisterContext_x86.cpp ---------------------------------*- C++ -*-===//
2*e8d8bef9SDimitry Andric //
3*e8d8bef9SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*e8d8bef9SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*e8d8bef9SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*e8d8bef9SDimitry Andric //
7*e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
8*e8d8bef9SDimitry Andric 
9*e8d8bef9SDimitry Andric #include "RegisterContext_x86.h"
10*e8d8bef9SDimitry Andric 
11*e8d8bef9SDimitry Andric using namespace lldb_private;
12*e8d8bef9SDimitry Andric 
13*e8d8bef9SDimitry Andric // Convert the 8-bit abridged FPU Tag Word (as found in FXSAVE) to the full
14*e8d8bef9SDimitry Andric // 16-bit FPU Tag Word (as found in FSAVE, and used by gdb protocol).  This
15*e8d8bef9SDimitry Andric // requires knowing the values of the ST(i) registers and the FPU Status Word.
AbridgedToFullTagWord(uint8_t abridged_tw,uint16_t sw,llvm::ArrayRef<MMSReg> st_regs)16*e8d8bef9SDimitry Andric uint16_t lldb_private::AbridgedToFullTagWord(uint8_t abridged_tw, uint16_t sw,
17*e8d8bef9SDimitry Andric                                              llvm::ArrayRef<MMSReg> st_regs) {
18*e8d8bef9SDimitry Andric   // Tag word is using internal FPU register numbering rather than ST(i).
19*e8d8bef9SDimitry Andric   // Mapping to ST(i): i = FPU regno - TOP (Status Word, bits 11:13).
20*e8d8bef9SDimitry Andric   // Here we start with FPU reg 7 and go down.
21*e8d8bef9SDimitry Andric   int st = 7 - ((sw >> 11) & 7);
22*e8d8bef9SDimitry Andric   uint16_t tw = 0;
23*e8d8bef9SDimitry Andric   for (uint8_t mask = 0x80; mask != 0; mask >>= 1) {
24*e8d8bef9SDimitry Andric     tw <<= 2;
25*e8d8bef9SDimitry Andric     if (abridged_tw & mask) {
26*e8d8bef9SDimitry Andric       // The register is non-empty, so we need to check the value of ST(i).
27*e8d8bef9SDimitry Andric       uint16_t exp =
28*e8d8bef9SDimitry Andric           st_regs[st].comp.sign_exp & 0x7fff; // Discard the sign bit.
29*e8d8bef9SDimitry Andric       if (exp == 0) {
30*e8d8bef9SDimitry Andric         if (st_regs[st].comp.mantissa == 0)
31*e8d8bef9SDimitry Andric           tw |= 1; // Zero
32*e8d8bef9SDimitry Andric         else
33*e8d8bef9SDimitry Andric           tw |= 2; // Denormal
34*e8d8bef9SDimitry Andric       } else if (exp == 0x7fff)
35*e8d8bef9SDimitry Andric         tw |= 2; // Infinity or NaN
36*e8d8bef9SDimitry Andric       // 0 if normal number
37*e8d8bef9SDimitry Andric     } else
38*e8d8bef9SDimitry Andric       tw |= 3; // Empty register
39*e8d8bef9SDimitry Andric 
40*e8d8bef9SDimitry Andric     // Rotate ST down.
41*e8d8bef9SDimitry Andric     st = (st - 1) & 7;
42*e8d8bef9SDimitry Andric   }
43*e8d8bef9SDimitry Andric 
44*e8d8bef9SDimitry Andric   return tw;
45*e8d8bef9SDimitry Andric }
46*e8d8bef9SDimitry Andric 
47*e8d8bef9SDimitry Andric // Convert the 16-bit FPU Tag Word to the abridged 8-bit value, to be written
48*e8d8bef9SDimitry Andric // into FXSAVE.
FullToAbridgedTagWord(uint16_t tw)49*e8d8bef9SDimitry Andric uint8_t lldb_private::FullToAbridgedTagWord(uint16_t tw) {
50*e8d8bef9SDimitry Andric   uint8_t abridged_tw = 0;
51*e8d8bef9SDimitry Andric   for (uint16_t mask = 0xc000; mask != 0; mask >>= 2) {
52*e8d8bef9SDimitry Andric     abridged_tw <<= 1;
53*e8d8bef9SDimitry Andric     // full TW uses 11 for empty registers, aTW uses 0
54*e8d8bef9SDimitry Andric     if ((tw & mask) != mask)
55*e8d8bef9SDimitry Andric       abridged_tw |= 1;
56*e8d8bef9SDimitry Andric   }
57*e8d8bef9SDimitry Andric   return abridged_tw;
58*e8d8bef9SDimitry Andric }
59