#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <errno.h>

#include "debug.h"
#include "box64context.h"
#include "box64cpu.h"
#include "emu/x64emu_private.h"
#include "la64_emitter.h"
#include "la64_mapping.h"
#include "x64emu.h"
#include "box64stack.h"
#include "callback.h"
#include "emu/x64run_private.h"
#include "x64trace.h"
#include "emu/x87emu_private.h"
#include "dynarec_native.h"

#include "la64_printer.h"
#include "dynarec_la64_private.h"
#include "../dynarec_helper.h"
#include "dynarec_la64_functions.h"

uintptr_t dynarec64_DF(dynarec_la64_t* dyn, uintptr_t addr, uintptr_t ip, int ninst, rex_t rex, int* ok, int* need_epilog)
{
    (void)ip;
    (void)need_epilog;

    uint8_t nextop = F8;
    uint8_t ed, wback, u8;
    int v1, v2;
    int s0;
    int64_t j64;
    int64_t fixedaddress;

    MAYUSE(s0);
    MAYUSE(v2);
    MAYUSE(v1);
    MAYUSE(j64);

    if (MODREG)
        switch (nextop) {
            case 0xC0 ... 0xC7:
                INST_NAME("FFREEP STx");
                // not handling Tag...
                X87_POP_OR_FAIL(dyn, ninst, x3);
                break;
            case 0xD0:
            case 0xD1:
            case 0xD2:
            case 0xD3:
            case 0xD4:
            case 0xD5:
            case 0xD6:
            case 0xD7:
                INST_NAME("FSTP STx, ST0");
                v1 = x87_get_st(dyn, ninst, x1, x2, 0, X87_COMBINE(0, nextop & 7));
                v2 = x87_get_st(dyn, ninst, x1, x2, nextop & 7, X87_COMBINE(0, nextop & 7));
                if (ST_IS_F(0)) {
                    FMOV_S(v2, v1);
                } else {
                    FMOV_D(v2, v1);
                }
                X87_POP_OR_FAIL(dyn, ninst, x3);
                break;
            case 0xE0:
                INST_NAME("FNSTSW AX");
                LD_WU(x2, xEmu, offsetof(x64emu_t, top));
                if (dyn->lsx.x87stack) {
                    ADDI_D(x2, x2, -dyn->lsx.x87stack);
                    ANDI(x2, x2, 0x7);
                }
                LD_HU(x1, xEmu, offsetof(x64emu_t, sw));
                MOV32w(x3, 0b1100011111111111); // mask
                AND(x1, x1, x3);
                SLLI_D(x2, x2, 11);
                OR(x1, x1, x2); // inject top
                ST_H(x1, xEmu, offsetof(x64emu_t, sw));
                SRLI_D(xRAX, xRAX, 16);
                SLLI_D(xRAX, xRAX, 16);
                OR(xRAX, xRAX, x1);
                break;
            case 0xE8 ... 0xF7:
                if (nextop < 0xF0) {
                    INST_NAME("FUCOMIP ST0, STx");
                } else {
                    INST_NAME("FCOMIP ST0, STx");
                }
                SETFLAGS(X_ALL, SF_SET_NODF, NAT_FLAGS_NOFUSION);
                SET_DFNONE();
                v1 = x87_get_st(dyn, ninst, x1, x2, 0, X87_COMBINE(0, nextop & 7));
                v2 = x87_get_st(dyn, ninst, x1, x2, nextop & 7, X87_COMBINE(0, nextop & 7));
                if (ST_IS_F(0)) {
                    FCOMIS(v1, v2, x1, x2);
                } else {
                    FCOMID(v1, v2, x1, x2);
                }
                X87_POP_OR_FAIL(dyn, ninst, x3);
                break;
            default:
                DEFAULT;
                break;
        }
    else
        switch ((nextop >> 3) & 7) {
            case 0:
                INST_NAME("FILD ST0, Ew");
                X87_PUSH_OR_FAIL(v1, dyn, ninst, x1, LSX_CACHE_ST_F);
                addr = geted(dyn, addr, ninst, nextop, &wback, x3, x4, &fixedaddress, rex, NULL, 1, 0);
                LD_H(x1, wback, fixedaddress);
                MOVGR2FR_D(v1, x1);
                if (ST_IS_F(0)) {
                    FFINT_S_L(v1, v1);
                } else {
                    FFINT_D_L(v1, v1);
                }
                break;
            case 1:
                INST_NAME("FISTTP Ew, ST0");
                v1 = x87_get_st(dyn, ninst, x1, x2, 0, LSX_CACHE_ST_F);
                v2 = fpu_get_scratch(dyn);
                addr = geted(dyn, addr, ninst, nextop, &wback, x3, x4, &fixedaddress, rex, NULL, 1, 0);
                if (!BOX64ENV(dynarec_fastround)) {
                    MOVGR2FCSR(FCSR2, xZR); // reset all bits
                }
                if (ST_IS_F(0)) {
                    FTINTRZ_W_S(v2, v1);
                    MOVFR2GR_S(x4, v2);
                } else {
                    FTINTRZ_W_D(v2, v1);
                    MOVFR2GR_S(x4, v2);
                }
                if (!BOX64ENV(dynarec_fastround)) {
                    MOVFCSR2GR(x5, FCSR2); // get back FPSR to check
                    BSTRPICK_D(x5, x5, FR_V, FR_V);
                    BNEZ_MARK(x5);
                    SLLI_W(x5, x4, 16);
                    SRAI_W(x5, x5, 16);
                    BEQ_MARK2(x5, x4);
                    MARK;
                    MOV32w(x4, 0x8000);
                }
                MARK2;
                ST_H(x4, wback, fixedaddress);
                X87_POP_OR_FAIL(dyn, ninst, x3);
                break;
            case 2:
                INST_NAME("FIST Ew, ST0");
                v1 = x87_get_st(dyn, ninst, x1, x2, 0, LSX_CACHE_ST_F);
                v2 = fpu_get_scratch(dyn);
                u8 = x87_setround(dyn, ninst, x1, x5);
                addr = geted(dyn, addr, ninst, nextop, &wback, x2, x3, &fixedaddress, rex, NULL, 1, 0);
                if (!BOX64ENV(dynarec_fastround)) {
                    MOVGR2FCSR(FCSR2, xZR); // reset all bits
                }
                if (ST_IS_F(0)) {
                    FTINT_W_S(v2, v1);
                    MOVFR2GR_S(x4, v2);
                } else {
                    FTINT_W_D(v2, v1);
                    MOVFR2GR_S(x4, v2);
                }
                x87_restoreround(dyn, ninst, u8);
                if (!BOX64ENV(dynarec_fastround)) {
                    MOVFCSR2GR(x5, FCSR2); // get back FPSR to check
                    BSTRPICK_D(x5, x5, FR_V, FR_V);
                    BNEZ_MARK(x5);
                    SLLI_W(x5, x4, 16);
                    SRAI_W(x5, x5, 16);
                    BEQ_MARK2(x5, x4);
                    MARK;
                    MOV32w(x4, 0x8000);
                }
                MARK2;
                ST_H(x4, wback, fixedaddress);
                break;
            case 3:
                INST_NAME("FISTP Ew, ST0");
                v1 = x87_get_st(dyn, ninst, x1, x2, 0, LSX_CACHE_ST_F);
                v2 = fpu_get_scratch(dyn);
                u8 = x87_setround(dyn, ninst, x1, x5);
                addr = geted(dyn, addr, ninst, nextop, &wback, x2, x3, &fixedaddress, rex, NULL, 1, 0);
                if (!BOX64ENV(dynarec_fastround)) {
                    MOVGR2FCSR(FCSR2, xZR); // reset all bits
                }
                if (ST_IS_F(0)) {
                    FTINT_W_S(v2, v1);
                    MOVFR2GR_S(x4, v2);
                } else {
                    FTINT_W_D(v2, v1);
                    MOVFR2GR_S(x4, v2);
                }
                x87_restoreround(dyn, ninst, u8);
                if (!BOX64ENV(dynarec_fastround)) {
                    MOVFCSR2GR(x5, FCSR2); // get back FPSR to check
                    BSTRPICK_D(x5, x5, FR_V, FR_V);
                    BNEZ_MARK(x5);
                    SLLI_W(x5, x4, 16);
                    SRAI_W(x5, x5, 16);
                    BEQ_MARK2(x5, x4);
                    MARK;
                    MOV32w(x4, 0x8000);
                }
                MARK2;
                ST_H(x4, wback, fixedaddress);
                X87_POP_OR_FAIL(dyn, ninst, x3);
                break;
            case 4:
                INST_NAME("FBLD ST0, tbytes");
                X87_PUSH_EMPTY_OR_FAIL(dyn, ninst, x1);
                addr = geted(dyn, addr, ninst, nextop, &ed, x1, x2, &fixedaddress, rex, NULL, 0, 0);
                s0 = x87_stackcount(dyn, ninst, x3);
                CALL(const_fpu_fbld, -1, ed, 0);
                x87_unstackcount(dyn, ninst, x3, s0);
                break;
            case 5:
                INST_NAME("FILD ST0, i64");
                X87_PUSH_OR_FAIL(v1, dyn, ninst, x1, LSX_CACHE_ST_I64);
                addr = geted(dyn, addr, ninst, nextop, &wback, x2, x3, &fixedaddress, rex, NULL, 1, 0);

                if (ST_IS_I64(0)) {
                    FLD_D(v1, wback, fixedaddress);
                } else {
                    LD_D(x1, wback, fixedaddress);
                    if (rex.is32bits) {
                        // need to also feed the STll stuff...
                        ADDI_D(x4, xEmu, offsetof(x64emu_t, fpu_ll));
                        LD_WU(x5, xEmu, offsetof(x64emu_t, top));
                        int a = 0 - dyn->lsx.x87stack;
                        if (a) {
                            ADDI_W(x5, x5, a);
                            ANDI(x5, x5, 0x7);
                        }
                        SLLI_D(x5, x5, 4); // fpu_ll is 2 i64
                        ADD_D(x5, x5, x4);
                        ST_D(x1, x5, 8); // ll
                    }
                    MOVGR2FR_D(v1, x1);
                    FFINT_D_L(v1, v1);
                    if (rex.is32bits) {
                        FST_D(v1, x5, 0); // ref
                    }
                }
                break;
            case 6:
                INST_NAME("FBSTP tbytes, ST0");
                x87_forget(dyn, ninst, x1, x2, 0);
                addr = geted(dyn, addr, ninst, nextop, &ed, x1, x2, &fixedaddress, rex, NULL, 0, 0);
                s0 = x87_stackcount(dyn, ninst, x3);
                CALL(const_fpu_fbst, -1, ed, 0);
                x87_unstackcount(dyn, ninst, x3, s0);
                X87_POP_OR_FAIL(dyn, ninst, x3);
                break;
            case 7:
                INST_NAME("FISTP i64, ST0");
                v1 = x87_get_st(dyn, ninst, x1, x2, 0, LSX_CACHE_ST_I64);
                v2 = fpu_get_scratch(dyn);
                if (!ST_IS_I64(0)) {
                    u8 = x87_setround(dyn, ninst, x1, x7);
                }
                addr = geted(dyn, addr, ninst, nextop, &wback, x2, x3, &fixedaddress, rex, NULL, 1, 0);

                if (ST_IS_I64(0)) {
                    FST_D(v1, wback, fixedaddress);
                } else {
                    if (rex.is32bits) {
                        // need to check STll first...
                        ADDI_D(x4, xEmu, offsetof(x64emu_t, fpu_ll));
                        LD_WU(x5, xEmu, offsetof(x64emu_t, top));
                        int a = 0 - dyn->lsx.x87stack;
                        if (a) {
                            ADDI_W(x5, x5, a);
                            ANDI(x5, x5, 0x7);
                        }
                        SLLI_D(x5, x5, 4); // fpu_ll is 2 i64
                        ADD_D(x5, x5, x4);
                        MOVFR2GR_D(x3, v1);
                        LD_D(x6, x5, 0); // ref
                        BNE_MARK(x6, x3);
                        LD_D(x6, x5, 8); // ll
                        ST_D(x6, wback, fixedaddress);
                        B_MARK3_nocond;
                        MARK;
                    }

                    if (!BOX64ENV(dynarec_fastround)) {
                        MOVGR2FCSR(FCSR2, xZR); // reset all bits
                    }
                    FTINT_L_D(v2, v1);
                    if (!BOX64ENV(dynarec_fastround)) {
                        MOVFCSR2GR(x5, FCSR2); // get back FPSR to check
                        BSTRPICK_D(x5, x5, FR_V, FR_V);
                        BEQ_MARK2(x5, xZR);
                        MOV64x(x4, 0x8000000000000000LL);
                        MOVGR2FR_D(v2, x4);
                    }
                    MARK2;
                    FST_D(v2, wback, fixedaddress);
                    MARK3;
                    x87_restoreround(dyn, ninst, u8);
                }
                X87_POP_OR_FAIL(dyn, ninst, x3);
                break;
            default:
                DEFAULT;
                break;
        }
    return addr;
}
