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