github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/cmd/compile/objw/objw.go (about)

     1  // Copyright 2009 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 objw
     6  
     7  import (
     8  	"encoding/binary"
     9  
    10  	"github.com/go-asm/go/cmd/compile/base"
    11  	"github.com/go-asm/go/cmd/compile/bitvec"
    12  	"github.com/go-asm/go/cmd/compile/types"
    13  	"github.com/go-asm/go/cmd/obj"
    14  )
    15  
    16  // Uint8 writes an unsigned byte v into s at offset off,
    17  // and returns the next unused offset (i.e., off+1).
    18  func Uint8(s *obj.LSym, off int, v uint8) int {
    19  	return UintN(s, off, uint64(v), 1)
    20  }
    21  
    22  func Uint16(s *obj.LSym, off int, v uint16) int {
    23  	return UintN(s, off, uint64(v), 2)
    24  }
    25  
    26  func Uint32(s *obj.LSym, off int, v uint32) int {
    27  	return UintN(s, off, uint64(v), 4)
    28  }
    29  
    30  func Uintptr(s *obj.LSym, off int, v uint64) int {
    31  	return UintN(s, off, v, types.PtrSize)
    32  }
    33  
    34  // Uvarint writes a varint v into s at offset off,
    35  // and returns the next unused offset.
    36  func Uvarint(s *obj.LSym, off int, v uint64) int {
    37  	var buf [binary.MaxVarintLen64]byte
    38  	n := binary.PutUvarint(buf[:], v)
    39  	return int(s.WriteBytes(base.Ctxt, int64(off), buf[:n]))
    40  }
    41  
    42  func Bool(s *obj.LSym, off int, v bool) int {
    43  	w := 0
    44  	if v {
    45  		w = 1
    46  	}
    47  	return UintN(s, off, uint64(w), 1)
    48  }
    49  
    50  // UintN writes an unsigned integer v of size wid bytes into s at offset off,
    51  // and returns the next unused offset.
    52  func UintN(s *obj.LSym, off int, v uint64, wid int) int {
    53  	if off&(wid-1) != 0 {
    54  		base.Fatalf("duintxxLSym: misaligned: v=%d wid=%d off=%d", v, wid, off)
    55  	}
    56  	s.WriteInt(base.Ctxt, int64(off), wid, int64(v))
    57  	return off + wid
    58  }
    59  
    60  func SymPtr(s *obj.LSym, off int, x *obj.LSym, xoff int) int {
    61  	off = int(types.RoundUp(int64(off), int64(types.PtrSize)))
    62  	s.WriteAddr(base.Ctxt, int64(off), types.PtrSize, x, int64(xoff))
    63  	off += types.PtrSize
    64  	return off
    65  }
    66  
    67  func SymPtrWeak(s *obj.LSym, off int, x *obj.LSym, xoff int) int {
    68  	off = int(types.RoundUp(int64(off), int64(types.PtrSize)))
    69  	s.WriteWeakAddr(base.Ctxt, int64(off), types.PtrSize, x, int64(xoff))
    70  	off += types.PtrSize
    71  	return off
    72  }
    73  
    74  func SymPtrOff(s *obj.LSym, off int, x *obj.LSym) int {
    75  	s.WriteOff(base.Ctxt, int64(off), x, 0)
    76  	off += 4
    77  	return off
    78  }
    79  
    80  func SymPtrWeakOff(s *obj.LSym, off int, x *obj.LSym) int {
    81  	s.WriteWeakOff(base.Ctxt, int64(off), x, 0)
    82  	off += 4
    83  	return off
    84  }
    85  
    86  func Global(s *obj.LSym, width int32, flags int16) {
    87  	if flags&obj.LOCAL != 0 {
    88  		s.Set(obj.AttrLocal, true)
    89  		flags &^= obj.LOCAL
    90  	}
    91  	base.Ctxt.Globl(s, int64(width), int(flags))
    92  }
    93  
    94  // BitVec writes the contents of bv into s as sequence of bytes
    95  // in little-endian order, and returns the next unused offset.
    96  func BitVec(s *obj.LSym, off int, bv bitvec.BitVec) int {
    97  	// Runtime reads the bitmaps as byte arrays. Oblige.
    98  	for j := 0; int32(j) < bv.N; j += 8 {
    99  		word := bv.B[j/32]
   100  		off = Uint8(s, off, uint8(word>>(uint(j)%32)))
   101  	}
   102  	return off
   103  }