package proc

import (
	"encoding/binary"
	"fmt"
	"github.com/go-delve/delve/pkg/dwarf/frame"
	"github.com/go-delve/delve/pkg/dwarf/op"
	"strings"
)

const (
	i386DwarfIPRegNum uint64 = 8
	i386DwarfSPRegNum uint64 = 4
	i386DwarfBPRegNum uint64 = 5
)

var i386BreakInstruction = []byte{0xCC}

// I386Arch returns an initialized I386Arch
// struct.
func I386Arch(goos string) *Arch {
	return &Arch{
		Name:                             "386",
		ptrSize:                          4,
		maxInstructionLength:             15,
		breakpointInstruction:            i386BreakInstruction,
		altBreakpointInstruction:         []byte{0xcd, 0x03},
		breakInstrMovesPC:                true,
		derefTLS:                         false,
		prologues:                        prologuesI386,
		fixFrameUnwindContext:            i386FixFrameUnwindContext,
		switchStack:                      i386SwitchStack,
		regSize:                          i386RegSize,
		RegistersToDwarfRegisters:        i386RegistersToDwarfRegisters,
		addrAndStackRegsToDwarfRegisters: i386AddrAndStackRegsToDwarfRegisters,
		DwarfRegisterToString:            i386DwarfRegisterToString,
		inhibitStepInto:                  i386InhibitStepInto,
		asmDecode:                        i386AsmDecode,
	}
}

func i386FixFrameUnwindContext(fctxt *frame.FrameContext, pc uint64, bi *BinaryInfo) *frame.FrameContext {
	i := bi.Arch
	if i.sigreturnfn == nil {
		i.sigreturnfn = bi.LookupFunc["runtime.sigreturn"]
	}

	if fctxt == nil || (i.sigreturnfn != nil && pc >= i.sigreturnfn.Entry && pc < i.sigreturnfn.End) {
		// When there's no frame descriptor entry use BP (the frame pointer) instead
		// - return register is [bp + i.PtrSize()] (i.e. [cfa-i.PtrSize()])
		// - cfa is bp + i.PtrSize()*2
		// - bp is [bp] (i.e. [cfa-i.PtrSize()*2])
		// - sp is cfa

		// When the signal handler runs it will move the execution to the signal
		// handling stack (installed using the sigaltstack system call).
		// This isn't i proper stack switch: the pointer to g in TLS will still
		// refer to whatever g was executing on that thread before the signal was
		// received.
		// Since go did not execute i stack switch the previous value of sp, pc
		// and bp is not saved inside g.sched, as it normally would.
		// The only way to recover is to either read sp/pc from the signal context
		// parameter (the ucontext_t* parameter) or to unconditionally follow the
		// frame pointer when we get to runtime.sigreturn (which is what we do
		// here).

		return &frame.FrameContext{
			RetAddrReg: i386DwarfIPRegNum,
			Regs: map[uint64]frame.DWRule{
				i386DwarfIPRegNum: frame.DWRule{
					Rule:   frame.RuleOffset,
					Offset: int64(-i.PtrSize()),
				},
				i386DwarfBPRegNum: frame.DWRule{
					Rule:   frame.RuleOffset,
					Offset: int64(-2 * i.PtrSize()),
				},
				i386DwarfSPRegNum: frame.DWRule{
					Rule:   frame.RuleValOffset,
					Offset: 0,
				},
			},
			CFA: frame.DWRule{
				Rule:   frame.RuleCFA,
				Reg:    i386DwarfBPRegNum,
				Offset: int64(2 * i.PtrSize()),
			},
		}
	}

	if i.crosscall2fn == nil {
		i.crosscall2fn = bi.LookupFunc["crosscall2"]
	}

	// TODO(chainhelen), need to check whether there is a bad frame descriptor like amd64.
	// crosscall2 is defined in $GOROOT/src/runtime/cgo/asm_386.s.
	if i.crosscall2fn != nil && pc >= i.crosscall2fn.Entry && pc < i.crosscall2fn.End {
		rule := fctxt.CFA
		fctxt.CFA = rule
	}

	// We assume that EBP is the frame pointer and we want to keep it updated,
	// so that we can use it to unwind the stack even when we encounter frames
	// without descriptor entries.
	// If there isn't i rule already we emit one.
	if fctxt.Regs[i386DwarfBPRegNum].Rule == frame.RuleUndefined {
		fctxt.Regs[i386DwarfBPRegNum] = frame.DWRule{
			Rule:   frame.RuleFramePointer,
			Reg:    i386DwarfBPRegNum,
			Offset: 0,
		}
	}

	return fctxt
}

// SwitchStack will use the current frame to determine if it's time to
func i386SwitchStack(it *stackIterator, _ *op.DwarfRegisters) bool {
	if it.frame.Current.Fn == nil {
		return false
	}
	switch it.frame.Current.Fn.Name {
	case "runtime.asmcgocall", "runtime.cgocallback_gofunc": // TODO(chainhelen), need to support cgo stacktraces.
		return false
	case "runtime.goexit", "runtime.rt0_go", "runtime.mcall":
		// Look for "top of stack" functions.
		it.atend = true
		return true

	case "runtime.mstart":
		// Calls to runtime.systemstack will switch to the systemstack then:
		// 1. alter the goroutine stack so that it looks like systemstack_switch
		//    was called
		// 2. alter the system stack so that it looks like the bottom-most frame
		//    belongs to runtime.mstart
		// If we find a runtime.mstart frame on the system stack of a goroutine
		// parked on runtime.systemstack_switch we assume runtime.systemstack was
		// called and continue tracing from the parked position.

		if it.top || !it.systemstack || it.g == nil {
			return false
		}
		if fn := it.bi.PCToFunc(it.g.PC); fn == nil || fn.Name != "runtime.systemstack_switch" {
			return false
		}

		it.switchToGoroutineStack()
		return true

	default:
		if it.systemstack && it.top && it.g != nil && strings.HasPrefix(it.frame.Current.Fn.Name, "runtime.") && it.frame.Current.Fn.Name != "runtime.fatalthrow" {
			// The runtime switches to the system stack in multiple places.
			// This usually happens through a call to runtime.systemstack but there
			// are functions that switch to the system stack manually (for example
			// runtime.morestack).
			// Since we are only interested in printing the system stack for cgo
			// calls we switch directly to the goroutine stack if we detect that the
			// function at the top of the stack is a runtime function.
			//
			// The function "runtime.fatalthrow" is deliberately excluded from this
			// because it can end up in the stack during a cgo call and switching to
			// the goroutine stack will exclude all the C functions from the stack
			// trace.
			it.switchToGoroutineStack()
			return true
		}

		return false
	}
}

// RegSize returns the size (in bytes) of register regnum.
// The mapping between hardware registers and DWARF registers is specified
// in the System V ABI Intel386 Architecture Processor Supplement page 25,
// table 2.14
// https://www.uclibc.org/docs/psABI-i386.pdf
func i386RegSize(regnum uint64) int {
	// XMM registers
	if regnum >= 21 && regnum <= 36 {
		return 16
	}
	// x87 registers
	if regnum >= 11 && regnum <= 18 {
		return 10
	}
	return 4
}

// The mapping between hardware registers and DWARF registers is specified
// in the System V ABI Intel386 Architecture Processor Supplement page 25,
// table 2.14
// https://www.uclibc.org/docs/psABI-i386.pdf

var i386DwarfToName = map[int]string{
	0:  "Eax",
	1:  "Ecx",
	2:  "Edx",
	3:  "Ebx",
	4:  "Esp",
	5:  "Ebp",
	6:  "Esi",
	7:  "Edi",
	8:  "Eip",
	9:  "Eflags",
	11: "ST(0)",
	12: "ST(1)",
	13: "ST(2)",
	14: "ST(3)",
	15: "ST(4)",
	16: "ST(5)",
	17: "ST(6)",
	18: "ST(7)",
	21: "XMM0",
	22: "XMM1",
	23: "XMM2",
	24: "XMM3",
	25: "XMM4",
	26: "XMM5",
	27: "XMM6",
	28: "XMM7",
	40: "Es",
	41: "Cs",
	42: "Ss",
	43: "Ds",
	44: "Fs",
	45: "Gs",
}

var i386NameToDwarf = func() map[string]int {
	r := make(map[string]int)
	for regNum, regName := range i386DwarfToName {
		r[strings.ToLower(regName)] = regNum
	}
	r["eflags"] = 9
	r["st0"] = 11
	r["st1"] = 12
	r["st2"] = 13
	r["st3"] = 14
	r["st4"] = 15
	r["st5"] = 16
	r["st6"] = 17
	r["st7"] = 18
	return r
}()

func maxI386DwarfRegister() int {
	max := int(i386DwarfIPRegNum)
	for i := range i386DwarfToName {
		if i > max {
			max = i
		}
	}
	return max
}

func i386RegistersToDwarfRegisters(staticBase uint64, regs Registers) op.DwarfRegisters {
	dregs := initDwarfRegistersFromSlice(maxI386DwarfRegister(), regs, i386NameToDwarf)
	dr := op.NewDwarfRegisters(staticBase, dregs, binary.LittleEndian, i386DwarfIPRegNum, i386DwarfSPRegNum, i386DwarfBPRegNum, 0)
	dr.SetLoadMoreCallback(loadMoreDwarfRegistersFromSliceFunc(dr, regs, i386NameToDwarf))

	return *dr
}

func i386AddrAndStackRegsToDwarfRegisters(staticBase, pc, sp, bp, lr uint64) op.DwarfRegisters {
	dregs := make([]*op.DwarfRegister, i386DwarfIPRegNum+1)
	dregs[i386DwarfIPRegNum] = op.DwarfRegisterFromUint64(pc)
	dregs[i386DwarfSPRegNum] = op.DwarfRegisterFromUint64(sp)
	dregs[i386DwarfBPRegNum] = op.DwarfRegisterFromUint64(bp)

	return *op.NewDwarfRegisters(staticBase, dregs, binary.LittleEndian, i386DwarfIPRegNum, i386DwarfSPRegNum, i386DwarfBPRegNum, 0)
}

func i386DwarfRegisterToString(j int, reg *op.DwarfRegister) (name string, floatingPoint bool, repr string) {
	name, ok := i386DwarfToName[j]
	if !ok {
		name = fmt.Sprintf("unknown%d", j)
	}

	switch n := strings.ToLower(name); n {
	case "eflags":
		return name, false, eflagsDescription.Describe(reg.Uint64Val, 32)

	case "tw", "fop":
		return name, true, fmt.Sprintf("%#04x", reg.Uint64Val)

	default:
		if reg.Bytes != nil && strings.HasPrefix(n, "xmm") {
			return name, true, formatSSEReg(name, reg.Bytes)
		} else if reg.Bytes != nil && strings.HasPrefix(n, "st(") {
			return name, true, formatX87Reg(reg.Bytes)
		} else if reg.Bytes == nil || (reg.Bytes != nil && len(reg.Bytes) <= 8) {
			return name, false, fmt.Sprintf("%#016x", reg.Uint64Val)
		} else {
			return name, false, fmt.Sprintf("%#x", reg.Bytes)
		}
	}
}

// i386InhibitStepInto returns whether StepBreakpoint can be set at pc.
// When cgo or pie on 386 linux, compiler will insert more instructions (ex: call __x86.get_pc_thunk.).
// StepBreakpoint shouldn't be set on __x86.get_pc_thunk and skip it.
// See comments on stacksplit in $GOROOT/src/cmd/internal/obj/x86/obj6.go for generated instructions details.
func i386InhibitStepInto(bi *BinaryInfo, pc uint64) bool {
	if bi.SymNames != nil && bi.SymNames[pc] != nil &&
		strings.HasPrefix(bi.SymNames[pc].Name, "__x86.get_pc_thunk.") {
		return true
	}
	return false
}
