github.com/bir3/gocompiler@v0.3.205/src/cmd/internal/obj/plist.go (about) 1 // Copyright 2013 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 obj 6 7 import ( 8 "github.com/bir3/gocompiler/src/cmd/internal/objabi" 9 "github.com/bir3/gocompiler/src/cmd/internal/src" 10 "fmt" 11 "strings" 12 ) 13 14 type Plist struct { 15 Firstpc *Prog 16 Curfn interface{} // holds a *gc.Node, if non-nil 17 } 18 19 // ProgAlloc is a function that allocates Progs. 20 // It is used to provide access to cached/bulk-allocated Progs to the assemblers. 21 type ProgAlloc func() *Prog 22 23 func Flushplist(ctxt *Link, plist *Plist, newprog ProgAlloc, myimportpath string) { 24 // Build list of symbols, and assign instructions to lists. 25 var curtext *LSym 26 var etext *Prog 27 var text []*LSym 28 29 var plink *Prog 30 for p := plist.Firstpc; p != nil; p = plink { 31 if ctxt.Debugasm > 0 && ctxt.Debugvlog { 32 fmt.Printf("obj: %v\n", p) 33 } 34 plink = p.Link 35 p.Link = nil 36 37 switch p.As { 38 case AEND: 39 continue 40 41 case ATEXT: 42 s := p.From.Sym 43 if s == nil { 44 // func _() { } 45 curtext = nil 46 continue 47 } 48 text = append(text, s) 49 etext = p 50 curtext = s 51 continue 52 53 case AFUNCDATA: 54 // Rewrite reference to go_args_stackmap(SB) to the Go-provided declaration information. 55 if curtext == nil { // func _() {} 56 continue 57 } 58 switch p.To.Sym.Name { 59 case "go_args_stackmap": 60 if p.From.Type != TYPE_CONST || p.From.Offset != objabi.FUNCDATA_ArgsPointerMaps { 61 ctxt.Diag("%s: FUNCDATA use of go_args_stackmap(SB) without FUNCDATA_ArgsPointerMaps", p.Pos) 62 } 63 p.To.Sym = ctxt.LookupDerived(curtext, curtext.Name+".args_stackmap") 64 case "no_pointers_stackmap": 65 if p.From.Type != TYPE_CONST || p.From.Offset != objabi.FUNCDATA_LocalsPointerMaps { 66 ctxt.Diag("%s: FUNCDATA use of no_pointers_stackmap(SB) without FUNCDATA_LocalsPointerMaps", p.Pos) 67 } 68 // funcdata for functions with no local variables in frame. 69 // Define two zero-length bitmaps, because the same index is used 70 // for the local variables as for the argument frame, and assembly 71 // frames have two argument bitmaps, one without results and one with results. 72 // Write []uint32{2, 0}. 73 b := make([]byte, 8) 74 ctxt.Arch.ByteOrder.PutUint32(b, 2) 75 s := ctxt.GCLocalsSym(b) 76 if !s.OnList() { 77 ctxt.Globl(s, int64(len(s.P)), int(RODATA|DUPOK)) 78 } 79 p.To.Sym = s 80 } 81 82 } 83 84 if curtext == nil { 85 etext = nil 86 continue 87 } 88 etext.Link = p 89 etext = p 90 } 91 92 if newprog == nil { 93 newprog = ctxt.NewProg 94 } 95 96 // Add reference to Go arguments for assembly functions without them. 97 if ctxt.IsAsm { 98 for _, s := range text { 99 if !strings.HasPrefix(s.Name, "\"\".") { 100 continue 101 } 102 // The current args_stackmap generation in the compiler assumes 103 // that the function in question is ABI0, so avoid introducing 104 // an args_stackmap reference if the func is not ABI0 (better to 105 // have no stackmap than an incorrect/lying stackmap). 106 if s.ABI() != ABI0 { 107 continue 108 } 109 foundArgMap, foundArgInfo := false, false 110 for p := s.Func().Text; p != nil; p = p.Link { 111 if p.As == AFUNCDATA && p.From.Type == TYPE_CONST { 112 if p.From.Offset == objabi.FUNCDATA_ArgsPointerMaps { 113 foundArgMap = true 114 } 115 if p.From.Offset == objabi.FUNCDATA_ArgInfo { 116 foundArgInfo = true 117 } 118 if foundArgMap && foundArgInfo { 119 break 120 } 121 } 122 } 123 if !foundArgMap { 124 p := Appendp(s.Func().Text, newprog) 125 p.As = AFUNCDATA 126 p.From.Type = TYPE_CONST 127 p.From.Offset = objabi.FUNCDATA_ArgsPointerMaps 128 p.To.Type = TYPE_MEM 129 p.To.Name = NAME_EXTERN 130 p.To.Sym = ctxt.LookupDerived(s, s.Name+".args_stackmap") 131 } 132 if !foundArgInfo { 133 p := Appendp(s.Func().Text, newprog) 134 p.As = AFUNCDATA 135 p.From.Type = TYPE_CONST 136 p.From.Offset = objabi.FUNCDATA_ArgInfo 137 p.To.Type = TYPE_MEM 138 p.To.Name = NAME_EXTERN 139 p.To.Sym = ctxt.LookupDerived(s, fmt.Sprintf("%s.arginfo%d", s.Name, s.ABI())) 140 } 141 } 142 } 143 144 // Turn functions into machine code images. 145 for _, s := range text { 146 mkfwd(s) 147 if ctxt.Arch.ErrorCheck != nil { 148 ctxt.Arch.ErrorCheck(ctxt, s) 149 } 150 linkpatch(ctxt, s, newprog) 151 ctxt.Arch.Preprocess(ctxt, s, newprog) 152 ctxt.Arch.Assemble(ctxt, s, newprog) 153 if ctxt.Errors > 0 { 154 continue 155 } 156 linkpcln(ctxt, s) 157 if myimportpath != "" { 158 ctxt.populateDWARF(plist.Curfn, s, myimportpath) 159 } 160 } 161 } 162 163 func (ctxt *Link) InitTextSym(s *LSym, flag int, start src.XPos) { 164 if s == nil { 165 // func _() { } 166 return 167 } 168 if s.Func() != nil { 169 ctxt.Diag("%s: symbol %s redeclared\n\t%s: other declaration of symbol %s", ctxt.PosTable.Pos(start), s.Name, ctxt.PosTable.Pos(s.Func().Text.Pos), s.Name) 170 return 171 } 172 s.NewFuncInfo() 173 if s.OnList() { 174 ctxt.Diag("%s: symbol %s redeclared", ctxt.PosTable.Pos(start), s.Name) 175 return 176 } 177 178 // startLine should be the same line number that would be displayed via 179 // pcln, etc for the declaration (i.e., relative line number, as 180 // adjusted by //line). 181 _, startLine := ctxt.getFileSymbolAndLine(start) 182 183 // TODO(mdempsky): Remove once cmd/asm stops writing "" symbols. 184 name := strings.Replace(s.Name, "\"\"", ctxt.Pkgpath, -1) 185 s.Func().FuncID = objabi.GetFuncID(name, flag&WRAPPER != 0 || flag&ABIWRAPPER != 0) 186 s.Func().FuncFlag = ctxt.toFuncFlag(flag) 187 s.Func().StartLine = startLine 188 s.Set(AttrOnList, true) 189 s.Set(AttrDuplicateOK, flag&DUPOK != 0) 190 s.Set(AttrNoSplit, flag&NOSPLIT != 0) 191 s.Set(AttrReflectMethod, flag&REFLECTMETHOD != 0) 192 s.Set(AttrWrapper, flag&WRAPPER != 0) 193 s.Set(AttrABIWrapper, flag&ABIWRAPPER != 0) 194 s.Set(AttrNeedCtxt, flag&NEEDCTXT != 0) 195 s.Set(AttrNoFrame, flag&NOFRAME != 0) 196 s.Type = objabi.STEXT 197 ctxt.Text = append(ctxt.Text, s) 198 199 // Set up DWARF entries for s 200 ctxt.dwarfSym(s) 201 } 202 203 func (ctxt *Link) toFuncFlag(flag int) objabi.FuncFlag { 204 var out objabi.FuncFlag 205 if flag&TOPFRAME != 0 { 206 out |= objabi.FuncFlag_TOPFRAME 207 } 208 if ctxt.IsAsm { 209 out |= objabi.FuncFlag_ASM 210 } 211 return out 212 } 213 214 func (ctxt *Link) Globl(s *LSym, size int64, flag int) { 215 ctxt.GloblPos(s, size, flag, src.NoXPos) 216 } 217 func (ctxt *Link) GloblPos(s *LSym, size int64, flag int, pos src.XPos) { 218 if s.OnList() { 219 // TODO: print where the first declaration was. 220 ctxt.Diag("%s: symbol %s redeclared", ctxt.PosTable.Pos(pos), s.Name) 221 } 222 s.Set(AttrOnList, true) 223 ctxt.Data = append(ctxt.Data, s) 224 s.Size = size 225 if s.Type == 0 { 226 s.Type = objabi.SBSS 227 } 228 if flag&DUPOK != 0 { 229 s.Set(AttrDuplicateOK, true) 230 } 231 if flag&RODATA != 0 { 232 s.Type = objabi.SRODATA 233 } else if flag&NOPTR != 0 { 234 if s.Type == objabi.SDATA { 235 s.Type = objabi.SNOPTRDATA 236 } else { 237 s.Type = objabi.SNOPTRBSS 238 } 239 } else if flag&TLSBSS != 0 { 240 s.Type = objabi.STLSBSS 241 } 242 } 243 244 // EmitEntryLiveness generates PCDATA Progs after p to switch to the 245 // liveness map active at the entry of function s. It returns the last 246 // Prog generated. 247 func (ctxt *Link) EmitEntryLiveness(s *LSym, p *Prog, newprog ProgAlloc) *Prog { 248 pcdata := ctxt.EmitEntryStackMap(s, p, newprog) 249 pcdata = ctxt.EmitEntryUnsafePoint(s, pcdata, newprog) 250 return pcdata 251 } 252 253 // Similar to EmitEntryLiveness, but just emit stack map. 254 func (ctxt *Link) EmitEntryStackMap(s *LSym, p *Prog, newprog ProgAlloc) *Prog { 255 pcdata := Appendp(p, newprog) 256 pcdata.Pos = s.Func().Text.Pos 257 pcdata.As = APCDATA 258 pcdata.From.Type = TYPE_CONST 259 pcdata.From.Offset = objabi.PCDATA_StackMapIndex 260 pcdata.To.Type = TYPE_CONST 261 pcdata.To.Offset = -1 // pcdata starts at -1 at function entry 262 263 return pcdata 264 } 265 266 // Similar to EmitEntryLiveness, but just emit unsafe point map. 267 func (ctxt *Link) EmitEntryUnsafePoint(s *LSym, p *Prog, newprog ProgAlloc) *Prog { 268 pcdata := Appendp(p, newprog) 269 pcdata.Pos = s.Func().Text.Pos 270 pcdata.As = APCDATA 271 pcdata.From.Type = TYPE_CONST 272 pcdata.From.Offset = objabi.PCDATA_UnsafePoint 273 pcdata.To.Type = TYPE_CONST 274 pcdata.To.Offset = -1 275 276 return pcdata 277 } 278 279 // StartUnsafePoint generates PCDATA Progs after p to mark the 280 // beginning of an unsafe point. The unsafe point starts immediately 281 // after p. 282 // It returns the last Prog generated. 283 func (ctxt *Link) StartUnsafePoint(p *Prog, newprog ProgAlloc) *Prog { 284 pcdata := Appendp(p, newprog) 285 pcdata.As = APCDATA 286 pcdata.From.Type = TYPE_CONST 287 pcdata.From.Offset = objabi.PCDATA_UnsafePoint 288 pcdata.To.Type = TYPE_CONST 289 pcdata.To.Offset = objabi.PCDATA_UnsafePointUnsafe 290 291 return pcdata 292 } 293 294 // EndUnsafePoint generates PCDATA Progs after p to mark the end of an 295 // unsafe point, restoring the register map index to oldval. 296 // The unsafe point ends right after p. 297 // It returns the last Prog generated. 298 func (ctxt *Link) EndUnsafePoint(p *Prog, newprog ProgAlloc, oldval int64) *Prog { 299 pcdata := Appendp(p, newprog) 300 pcdata.As = APCDATA 301 pcdata.From.Type = TYPE_CONST 302 pcdata.From.Offset = objabi.PCDATA_UnsafePoint 303 pcdata.To.Type = TYPE_CONST 304 pcdata.To.Offset = oldval 305 306 return pcdata 307 } 308 309 // MarkUnsafePoints inserts PCDATAs to mark nonpreemptible and restartable 310 // instruction sequences, based on isUnsafePoint and isRestartable predicate. 311 // p0 is the start of the instruction stream. 312 // isUnsafePoint(p) returns true if p is not safe for async preemption. 313 // isRestartable(p) returns true if we can restart at the start of p (this Prog) 314 // upon async preemption. (Currently multi-Prog restartable sequence is not 315 // supported.) 316 // isRestartable can be nil. In this case it is treated as always returning false. 317 // If isUnsafePoint(p) and isRestartable(p) are both true, it is treated as 318 // an unsafe point. 319 func MarkUnsafePoints(ctxt *Link, p0 *Prog, newprog ProgAlloc, isUnsafePoint, isRestartable func(*Prog) bool) { 320 if isRestartable == nil { 321 // Default implementation: nothing is restartable. 322 isRestartable = func(*Prog) bool { return false } 323 } 324 prev := p0 325 prevPcdata := int64(-1) // entry PC data value 326 prevRestart := int64(0) 327 for p := prev.Link; p != nil; p, prev = p.Link, p { 328 if p.As == APCDATA && p.From.Offset == objabi.PCDATA_UnsafePoint { 329 prevPcdata = p.To.Offset 330 continue 331 } 332 if prevPcdata == objabi.PCDATA_UnsafePointUnsafe { 333 continue // already unsafe 334 } 335 if isUnsafePoint(p) { 336 q := ctxt.StartUnsafePoint(prev, newprog) 337 q.Pc = p.Pc 338 q.Link = p 339 // Advance to the end of unsafe point. 340 for p.Link != nil && isUnsafePoint(p.Link) { 341 p = p.Link 342 } 343 if p.Link == nil { 344 break // Reached the end, don't bother marking the end 345 } 346 p = ctxt.EndUnsafePoint(p, newprog, prevPcdata) 347 p.Pc = p.Link.Pc 348 continue 349 } 350 if isRestartable(p) { 351 val := int64(objabi.PCDATA_Restart1) 352 if val == prevRestart { 353 val = objabi.PCDATA_Restart2 354 } 355 prevRestart = val 356 q := Appendp(prev, newprog) 357 q.As = APCDATA 358 q.From.Type = TYPE_CONST 359 q.From.Offset = objabi.PCDATA_UnsafePoint 360 q.To.Type = TYPE_CONST 361 q.To.Offset = val 362 q.Pc = p.Pc 363 q.Link = p 364 365 if p.Link == nil { 366 break // Reached the end, don't bother marking the end 367 } 368 if isRestartable(p.Link) { 369 // Next Prog is also restartable. No need to mark the end 370 // of this sequence. We'll just go ahead mark the next one. 371 continue 372 } 373 p = Appendp(p, newprog) 374 p.As = APCDATA 375 p.From.Type = TYPE_CONST 376 p.From.Offset = objabi.PCDATA_UnsafePoint 377 p.To.Type = TYPE_CONST 378 p.To.Offset = prevPcdata 379 p.Pc = p.Link.Pc 380 } 381 } 382 }