package binary

import (
	
	
	

	
	
	
	
)

func decodeConstantExpression( *bytes.Reader,  api.CoreFeatures,  *wasm.ConstantExpression) error {
	,  := .ReadByte()
	if  != nil {
		return fmt.Errorf("read opcode: %v", )
	}

	 := int64(.Len())
	 := .Size() - 

	 := 
	switch  {
	case wasm.OpcodeI32Const:
		// Treat constants as signed as their interpretation is not yet known per /RATIONALE.md
		_, _,  = leb128.DecodeInt32()
	case wasm.OpcodeI64Const:
		// Treat constants as signed as their interpretation is not yet known per /RATIONALE.md
		_, _,  = leb128.DecodeInt64()
	case wasm.OpcodeF32Const:
		 := make([]byte, 4)
		if ,  := io.ReadFull(, );  != nil {
			return fmt.Errorf("read f32 constant: %v", )
		}
		_,  = ieee754.DecodeFloat32()
	case wasm.OpcodeF64Const:
		 := make([]byte, 8)
		if ,  := io.ReadFull(, );  != nil {
			return fmt.Errorf("read f64 constant: %v", )
		}
		_,  = ieee754.DecodeFloat64()
	case wasm.OpcodeGlobalGet:
		_, _,  = leb128.DecodeUint32()
	case wasm.OpcodeRefNull:
		if  := .RequireEnabled(api.CoreFeatureBulkMemoryOperations);  != nil {
			return fmt.Errorf("ref.null is not supported as %w", )
		}
		,  := .ReadByte()
		if  != nil {
			return fmt.Errorf("read reference type for ref.null: %w", )
		} else if  != wasm.RefTypeFuncref &&  != wasm.RefTypeExternref {
			return fmt.Errorf("invalid type for ref.null: 0x%x", )
		}
	case wasm.OpcodeRefFunc:
		if  := .RequireEnabled(api.CoreFeatureBulkMemoryOperations);  != nil {
			return fmt.Errorf("ref.func is not supported as %w", )
		}
		// Parsing index.
		_, _,  = leb128.DecodeUint32()
	case wasm.OpcodeVecPrefix:
		if  := .RequireEnabled(api.CoreFeatureSIMD);  != nil {
			return fmt.Errorf("vector instructions are not supported as %w", )
		}
		,  = .ReadByte()
		if  != nil {
			return fmt.Errorf("read vector instruction opcode suffix: %w", )
		}

		if  != wasm.OpcodeVecV128Const {
			return fmt.Errorf("invalid vector opcode for const expression: %#x", )
		}

		 = int64(.Len())
		 = .Size() - 

		,  := .Read(make([]byte, 16))
		if  != nil {
			return fmt.Errorf("read vector const instruction immediates: %w", )
		} else if  != 16 {
			return fmt.Errorf("read vector const instruction immediates: needs 16 bytes but was %d bytes", )
		}
	default:
		return fmt.Errorf("%v for const expression opt code: %#x", ErrInvalidByte, )
	}

	if  != nil {
		return fmt.Errorf("read value: %v", )
	}

	if ,  = .ReadByte();  != nil {
		return fmt.Errorf("look for end opcode: %v", )
	}

	if  != wasm.OpcodeEnd {
		return fmt.Errorf("constant expression has been not terminated")
	}

	.Data = make([]byte, -int64(.Len())-1)
	if _,  = .ReadAt(.Data, );  != nil {
		return fmt.Errorf("error re-buffering ConstantExpression.Data")
	}
	.Opcode = 
	return nil
}