// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package bpf

import (
	
	
)

// A VM is an emulated BPF virtual machine.
type VM struct {
	filter []Instruction
}

// NewVM returns a new VM using the input BPF program.
func ( []Instruction) (*VM, error) {
	if len() == 0 {
		return nil, errors.New("one or more Instructions must be specified")
	}

	for ,  := range  {
		 := len() - ( + 1)
		switch ins := .(type) {
		// Check for out-of-bounds jumps in instructions
		case Jump:
			if  <= int(.Skip) {
				return nil, fmt.Errorf("cannot jump %d instructions; jumping past program bounds", .Skip)
			}
		case JumpIf:
			if  <= int(.SkipTrue) {
				return nil, fmt.Errorf("cannot jump %d instructions in true case; jumping past program bounds", .SkipTrue)
			}
			if  <= int(.SkipFalse) {
				return nil, fmt.Errorf("cannot jump %d instructions in false case; jumping past program bounds", .SkipFalse)
			}
		case JumpIfX:
			if  <= int(.SkipTrue) {
				return nil, fmt.Errorf("cannot jump %d instructions in true case; jumping past program bounds", .SkipTrue)
			}
			if  <= int(.SkipFalse) {
				return nil, fmt.Errorf("cannot jump %d instructions in false case; jumping past program bounds", .SkipFalse)
			}
		// Check for division or modulus by zero
		case ALUOpConstant:
			if .Val != 0 {
				break
			}

			switch .Op {
			case ALUOpDiv, ALUOpMod:
				return nil, errors.New("cannot divide by zero using ALUOpConstant")
			}
		// Check for unknown extensions
		case LoadExtension:
			switch .Num {
			case ExtLen:
			default:
				return nil, fmt.Errorf("extension %d not implemented", .Num)
			}
		}
	}

	// Make sure last instruction is a return instruction
	switch [len()-1].(type) {
	case RetA, RetConstant:
	default:
		return nil, errors.New("BPF program must end with RetA or RetConstant")
	}

	// Though our VM works using disassembled instructions, we
	// attempt to assemble the input filter anyway to ensure it is compatible
	// with an operating system VM.
	,  := Assemble()

	return &VM{
		filter: ,
	}, 
}

// Run runs the VM's BPF program against the input bytes.
// Run returns the number of bytes accepted by the BPF program, and any errors
// which occurred while processing the program.
func ( *VM) ( []byte) (int, error) {
	var (
		// Registers of the virtual machine
		       uint32
		       uint32
		 [16]uint32

		// OK is true if the program should continue processing the next
		// instruction, or false if not, causing the loop to break
		 = true
	)

	// TODO(mdlayher): implement:
	// - NegateA:
	//   - would require a change from uint32 registers to int32
	//     registers

	// TODO(mdlayher): add interop tests that check signedness of ALU
	// operations against kernel implementation, and make sure Go
	// implementation matches behavior

	for  := 0;  < len(.filter) && ; ++ {
		 := .filter[]

		switch ins := .(type) {
		case ALUOpConstant:
			 = aluOpConstant(, )
		case ALUOpX:
			,  = aluOpX(, , )
		case Jump:
			 += int(.Skip)
		case JumpIf:
			 := jumpIf(, )
			 += 
		case JumpIfX:
			 := jumpIfX(, , )
			 += 
		case LoadAbsolute:
			,  = loadAbsolute(, )
		case LoadConstant:
			,  = loadConstant(, , )
		case LoadExtension:
			 = loadExtension(, )
		case LoadIndirect:
			,  = loadIndirect(, , )
		case LoadMemShift:
			,  = loadMemShift(, )
		case LoadScratch:
			,  = loadScratch(, , , )
		case RetA:
			return int(), nil
		case RetConstant:
			return int(.Val), nil
		case StoreScratch:
			 = storeScratch(, , , )
		case TAX:
			 = 
		case TXA:
			 = 
		default:
			return 0, fmt.Errorf("unknown Instruction at index %d: %T", , )
		}
	}

	return 0, nil
}