Source file test/asmhdr.dir/main.go

     1  // Copyright 2021 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 main
     6  
     7  import "unsafe"
     8  
     9  const (
    10  	smallInt = 42
    11  
    12  	// For bigInt, we use a value that's too big for an int64, but still
    13  	// fits in uint64. go/constant uses a different representation for
    14  	// values larger than int64, but the cmd/asm parser can't parse
    15  	// anything bigger than a uint64.
    16  	bigInt = 0xffffffffffffffff
    17  
    18  	stringVal = "test"
    19  
    20  	longStringVal = "this_is_a_string_constant_longer_than_seventy_characters_which_used_to_fail_see_issue_50253"
    21  )
    22  
    23  var (
    24  	smallIntAsm   int64
    25  	bigIntAsm     uint64
    26  	stringAsm     [len(stringVal)]byte
    27  	longStringAsm [len(longStringVal)]byte
    28  )
    29  
    30  type typ struct {
    31  	a uint64
    32  	b [100]uint8
    33  	c uint8
    34  }
    35  
    36  var (
    37  	typSize uint64
    38  
    39  	typA, typB, typC uint64
    40  )
    41  
    42  func main() {
    43  	if smallInt != smallIntAsm {
    44  		println("smallInt", smallInt, "!=", smallIntAsm)
    45  	}
    46  	if bigInt != bigIntAsm {
    47  		println("bigInt", uint64(bigInt), "!=", bigIntAsm)
    48  	}
    49  	if stringVal != string(stringAsm[:]) {
    50  		println("stringVal", stringVal, "!=", string(stringAsm[:]))
    51  	}
    52  	if longStringVal != string(longStringAsm[:]) {
    53  		println("longStringVal", longStringVal, "!=", string(longStringAsm[:]))
    54  	}
    55  
    56  	// We also include boolean consts in go_asm.h, but they're
    57  	// defined to be "true" or "false", and it's not clear how to
    58  	// use that in assembly.
    59  
    60  	if want := unsafe.Sizeof(typ{}); want != uintptr(typSize) {
    61  		println("typSize", want, "!=", typSize)
    62  	}
    63  	if want := unsafe.Offsetof(typ{}.a); want != uintptr(typA) {
    64  		println("typA", want, "!=", typA)
    65  	}
    66  	if want := unsafe.Offsetof(typ{}.b); want != uintptr(typB) {
    67  		println("typB", want, "!=", typB)
    68  	}
    69  	if want := unsafe.Offsetof(typ{}.c); want != uintptr(typC) {
    70  		println("typC", want, "!=", typC)
    71  	}
    72  }
    73  

View as plain text