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