Source file src/cmd/compile/internal/objw/objw.go

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

View as plain text