Source file src/cmd/internal/obj/wasm/wasmobj_test.go

     1  // Copyright 2026 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package wasm
     6  
     7  import (
     8  	"bytes"
     9  	"cmd/internal/obj"
    10  	"testing"
    11  )
    12  
    13  // TestWriteOpcode checks one opcode from each encoding range. Vector
    14  // instructions are the 0xFD prefix followed by a ULEB128-encoded opcode
    15  // index, so the cases below bracket both points where that index grows a
    16  // byte: 0x07F to 0x080 and 0x0FF to 0x100.
    17  func TestWriteOpcode(t *testing.T) {
    18  	tests := []struct {
    19  		as   obj.As
    20  		want []byte
    21  	}{
    22  		{AUnreachable, []byte{0x00}},
    23  		{AEnd, []byte{0x0B}},
    24  		{ADrop, []byte{0x1A}},
    25  		{ALocalGet, []byte{0x20}},
    26  		{AI32Load, []byte{0x28}},
    27  		{ARefNull, []byte{0xD0}},
    28  		{AContNew, []byte{0xE0}},
    29  		{AI32TruncSatF32S, []byte{0xFC, 0x00}},
    30  
    31  		{AV128Load, []byte{0xFD, 0x00}},                            // index 0x000
    32  		{AI32x4ExtaddPairwiseI16x8U, []byte{0xFD, 0x7F}},           // index 0x07F
    33  		{AI16x8Abs, []byte{0xFD, 0x80, 0x01}},                      // index 0x080
    34  		{AF64x2ConvertLowI32x4U, []byte{0xFD, 0xFF, 0x01}},         // index 0x0FF
    35  		{AI8x16RelaxedSwizzle, []byte{0xFD, 0x80, 0x02}},           // index 0x100
    36  		{AI32x4RelaxedDotI8x16I7x16AddS, []byte{0xFD, 0x93, 0x02}}, // index 0x113
    37  	}
    38  	var buf bytes.Buffer
    39  	for _, test := range tests {
    40  		buf.Reset()
    41  		writeOpcode(&buf, test.as)
    42  		if got := buf.Bytes(); !bytes.Equal(got, test.want) {
    43  			t.Errorf("writeOpcode(%v) = % x, want % x", test.as, got, test.want)
    44  		}
    45  	}
    46  }
    47  

View as plain text