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