1
2
3
4
5 package abi
6
7 import (
8 "cmd/compile/internal/base"
9 "cmd/compile/internal/ir"
10 "cmd/compile/internal/types"
11 "cmd/internal/obj"
12 "cmd/internal/src"
13 "fmt"
14 "math"
15 "sync"
16 )
17
18
19
20
21
22
23
24
25
26
27
28
29 type ABIParamResultInfo struct {
30 inparams []ABIParamAssignment
31 outparams []ABIParamAssignment
32 offsetToSpillArea int64
33 spillAreaSize int64
34 inRegistersUsed int
35 outRegistersUsed int
36 config *ABIConfig
37 }
38
39 func (a *ABIParamResultInfo) Config() *ABIConfig {
40 return a.config
41 }
42
43 func (a *ABIParamResultInfo) InParams() []ABIParamAssignment {
44 return a.inparams
45 }
46
47 func (a *ABIParamResultInfo) OutParams() []ABIParamAssignment {
48 return a.outparams
49 }
50
51 func (a *ABIParamResultInfo) InRegistersUsed() int {
52 return a.inRegistersUsed
53 }
54
55 func (a *ABIParamResultInfo) OutRegistersUsed() int {
56 return a.outRegistersUsed
57 }
58
59 func (a *ABIParamResultInfo) InParam(i int) *ABIParamAssignment {
60 return &a.inparams[i]
61 }
62
63 func (a *ABIParamResultInfo) OutParam(i int) *ABIParamAssignment {
64 return &a.outparams[i]
65 }
66
67 func (a *ABIParamResultInfo) SpillAreaOffset() int64 {
68 return a.offsetToSpillArea
69 }
70
71 func (a *ABIParamResultInfo) SpillAreaSize() int64 {
72 return a.spillAreaSize
73 }
74
75
76
77
78
79 func (a *ABIParamResultInfo) ArgWidth() int64 {
80 return a.spillAreaSize + a.offsetToSpillArea - a.config.LocalsOffset()
81 }
82
83
84
85
86
87
88
89
90
91
92 type RegIndex uint8
93
94
95
96
97
98
99 type ABIParamAssignment struct {
100 Type *types.Type
101 Name *ir.Name
102 Registers []RegIndex
103 offset int32
104 }
105
106
107
108 func (a *ABIParamAssignment) Offset() int32 {
109 if len(a.Registers) > 0 {
110 base.Fatalf("register allocated parameters have no offset")
111 }
112 return a.offset
113 }
114
115
116
117
118 func RegisterTypes(apa []ABIParamAssignment) []*types.Type {
119 rcount := 0
120 for _, pa := range apa {
121 rcount += len(pa.Registers)
122 }
123 if rcount == 0 {
124
125 return make([]*types.Type, 0, 1)
126 }
127 rts := make([]*types.Type, 0, rcount+1)
128 for _, pa := range apa {
129 if len(pa.Registers) == 0 {
130 continue
131 }
132 rts = appendParamTypes(rts, pa.Type)
133 }
134 return rts
135 }
136
137 func (pa *ABIParamAssignment) RegisterTypesAndOffsets() ([]*types.Type, []int64) {
138 l := len(pa.Registers)
139 if l == 0 {
140 return nil, nil
141 }
142 typs := make([]*types.Type, 0, l)
143 offs := make([]int64, 0, l)
144 offs, _ = appendParamOffsets(offs, 0, pa.Type)
145 return appendParamTypes(typs, pa.Type), offs
146 }
147
148 func appendParamTypes(rts []*types.Type, t *types.Type) []*types.Type {
149 w := t.Size()
150 if w == 0 {
151 return rts
152 }
153 if t.IsScalar() || t.IsPtrShaped() {
154 if t.IsComplex() {
155 c := types.FloatForComplex(t)
156 return append(rts, c, c)
157 } else {
158 if int(t.Size()) <= types.RegSize {
159 return append(rts, t)
160 }
161
162
163 if t.IsSigned() {
164 rts = append(rts, types.Types[types.TINT32])
165 } else {
166 rts = append(rts, types.Types[types.TUINT32])
167 }
168 return append(rts, types.Types[types.TUINT32])
169 }
170 } else {
171 typ := t.Kind()
172 switch typ {
173 case types.TARRAY:
174 for i := int64(0); i < t.NumElem(); i++ {
175 rts = appendParamTypes(rts, t.Elem())
176 }
177 case types.TSTRUCT:
178 for _, f := range t.Fields() {
179 if f.Type.Size() > 0 {
180 rts = appendParamTypes(rts, f.Type)
181 }
182 }
183 case types.TSLICE:
184 return appendParamTypes(rts, synthSlice)
185 case types.TSTRING:
186 return appendParamTypes(rts, synthString)
187 case types.TINTER:
188 return appendParamTypes(rts, synthIface)
189 }
190 }
191 return rts
192 }
193
194
195
196 func appendParamOffsets(offsets []int64, at int64, t *types.Type) ([]int64, int64) {
197 at = align(at, t)
198 w := t.Size()
199 if w == 0 {
200 return offsets, at
201 }
202 if t.IsScalar() || t.IsPtrShaped() {
203 if t.IsComplex() || int(t.Size()) > types.RegSize {
204 s := w / 2
205 return append(offsets, at, at+s), at + w
206 } else {
207 return append(offsets, at), at + w
208 }
209 } else {
210 typ := t.Kind()
211 switch typ {
212 case types.TARRAY:
213 for i := int64(0); i < t.NumElem(); i++ {
214 offsets, at = appendParamOffsets(offsets, at, t.Elem())
215 }
216 case types.TSTRUCT:
217 for i, f := range t.Fields() {
218 offsets, at = appendParamOffsets(offsets, at, f.Type)
219 if f.Type.Size() == 0 && i == t.NumFields()-1 {
220 at++
221 }
222 }
223 at = align(at, t)
224 case types.TSLICE:
225 return appendParamOffsets(offsets, at, synthSlice)
226 case types.TSTRING:
227 return appendParamOffsets(offsets, at, synthString)
228 case types.TINTER:
229 return appendParamOffsets(offsets, at, synthIface)
230 }
231 }
232 return offsets, at
233 }
234
235
236
237
238
239
240
241
242 func (a *ABIParamAssignment) FrameOffset(i *ABIParamResultInfo) int64 {
243 if a.offset == -1 {
244 base.Fatalf("function parameter has no ABI-defined frame-pointer offset")
245 }
246 if len(a.Registers) == 0 {
247 return int64(a.offset) - i.config.LocalsOffset()
248 }
249
250 return int64(a.offset) + i.SpillAreaOffset() - i.config.LocalsOffset()
251 }
252
253
254 type RegAmounts struct {
255 intRegs int
256 floatRegs int
257 }
258
259
260
261 type ABIConfig struct {
262
263 offsetForLocals int64
264 regAmounts RegAmounts
265 which obj.ABI
266 }
267
268
269
270 func NewABIConfig(iRegsCount, fRegsCount int, offsetForLocals int64, which uint8) *ABIConfig {
271 return &ABIConfig{offsetForLocals: offsetForLocals, regAmounts: RegAmounts{iRegsCount, fRegsCount}, which: obj.ABI(which)}
272 }
273
274
275
276
277 func (config *ABIConfig) Copy() *ABIConfig {
278 return config
279 }
280
281
282 func (config *ABIConfig) Which() obj.ABI {
283 return config.which
284 }
285
286
287
288
289 func (config *ABIConfig) LocalsOffset() int64 {
290 return config.offsetForLocals
291 }
292
293
294
295
296 func (config *ABIConfig) FloatIndexFor(r RegIndex) int64 {
297 return int64(r) - int64(config.regAmounts.intRegs)
298 }
299
300
301
302
303 func (config *ABIConfig) NumParamRegs(typ *types.Type) int {
304 intRegs, floatRegs := typ.Registers()
305 if intRegs == math.MaxUint8 && floatRegs == math.MaxUint8 {
306 base.Fatalf("cannot represent parameters of type %v in registers", typ)
307 }
308 return int(intRegs) + int(floatRegs)
309 }
310
311
312
313
314
315 func (config *ABIConfig) ABIAnalyzeTypes(params, results []*types.Type) *ABIParamResultInfo {
316 setup()
317 s := assignState{
318 stackOffset: config.offsetForLocals,
319 rTotal: config.regAmounts,
320 }
321
322 assignParams := func(params []*types.Type, isResult bool) []ABIParamAssignment {
323 res := make([]ABIParamAssignment, len(params))
324 for i, param := range params {
325 res[i] = s.assignParam(param, nil, isResult)
326 }
327 return res
328 }
329
330 info := &ABIParamResultInfo{config: config}
331
332
333 info.inparams = assignParams(params, false)
334 s.stackOffset = types.RoundUp(s.stackOffset, int64(types.RegSize))
335 info.inRegistersUsed = s.rUsed.intRegs + s.rUsed.floatRegs
336
337
338 s.rUsed = RegAmounts{}
339 info.outparams = assignParams(results, true)
340
341
342 info.offsetToSpillArea = alignTo(s.stackOffset, types.RegSize)
343 info.spillAreaSize = alignTo(s.spillOffset, types.RegSize)
344 info.outRegistersUsed = s.rUsed.intRegs + s.rUsed.floatRegs
345
346 return info
347 }
348
349
350
351
352
353 func (config *ABIConfig) ABIAnalyzeFuncType(ft *types.Type) *ABIParamResultInfo {
354 setup()
355 s := assignState{
356 stackOffset: config.offsetForLocals,
357 rTotal: config.regAmounts,
358 }
359
360 assignParams := func(params []*types.Field, isResult bool) []ABIParamAssignment {
361 res := make([]ABIParamAssignment, len(params))
362 for i, param := range params {
363 var name *ir.Name
364 if param.Nname != nil {
365 name = param.Nname.(*ir.Name)
366 }
367 res[i] = s.assignParam(param.Type, name, isResult)
368 }
369 return res
370 }
371
372 info := &ABIParamResultInfo{config: config}
373
374
375 info.inparams = assignParams(ft.RecvParams(), false)
376 s.stackOffset = types.RoundUp(s.stackOffset, int64(types.RegSize))
377 info.inRegistersUsed = s.rUsed.intRegs + s.rUsed.floatRegs
378
379
380 s.rUsed = RegAmounts{}
381 info.outparams = assignParams(ft.Results(), true)
382
383
384 info.offsetToSpillArea = alignTo(s.stackOffset, types.RegSize)
385 info.spillAreaSize = alignTo(s.spillOffset, types.RegSize)
386 info.outRegistersUsed = s.rUsed.intRegs + s.rUsed.floatRegs
387 return info
388 }
389
390
391
392
393
394
395
396
397 func (config *ABIConfig) ABIAnalyze(t *types.Type, setNname bool) *ABIParamResultInfo {
398 result := config.ABIAnalyzeFuncType(t)
399
400
401 for i, f := range t.RecvParams() {
402 config.updateOffset(result, f, result.inparams[i], false, setNname)
403 }
404 for i, f := range t.Results() {
405 config.updateOffset(result, f, result.outparams[i], true, setNname)
406 }
407 return result
408 }
409
410 func (config *ABIConfig) updateOffset(result *ABIParamResultInfo, f *types.Field, a ABIParamAssignment, isResult, setNname bool) {
411 if f.Offset != types.BADWIDTH {
412 base.Fatalf("field offset for %s at %s has been set to %d", f.Sym, base.FmtPos(f.Pos), f.Offset)
413 }
414
415
416 if !isResult || len(a.Registers) == 0 {
417
418
419 off := a.FrameOffset(result)
420 if setNname && f.Nname != nil {
421 f.Nname.(*ir.Name).SetFrameOffset(off)
422 f.Nname.(*ir.Name).SetIsOutputParamInRegisters(false)
423 }
424 } else {
425 if setNname && f.Nname != nil {
426 fname := f.Nname.(*ir.Name)
427 fname.SetIsOutputParamInRegisters(true)
428 fname.SetFrameOffset(0)
429 }
430 }
431 }
432
433
434
435
436
437
438 func (c *RegAmounts) regString(r RegIndex) string {
439 if int(r) < c.intRegs {
440 return fmt.Sprintf("I%d", int(r))
441 } else if int(r) < c.intRegs+c.floatRegs {
442 return fmt.Sprintf("F%d", int(r)-c.intRegs)
443 }
444 return fmt.Sprintf("<?>%d", r)
445 }
446
447
448
449 func (ri *ABIParamAssignment) ToString(config *ABIConfig, extra bool) string {
450 regs := "R{"
451 offname := "spilloffset"
452 if len(ri.Registers) == 0 {
453 offname = "offset"
454 }
455 for _, r := range ri.Registers {
456 regs += " " + config.regAmounts.regString(r)
457 if extra {
458 regs += fmt.Sprintf("(%d)", r)
459 }
460 }
461 if extra {
462 regs += fmt.Sprintf(" | #I=%d, #F=%d", config.regAmounts.intRegs, config.regAmounts.floatRegs)
463 }
464 return fmt.Sprintf("%s } %s: %d typ: %v", regs, offname, ri.offset, ri.Type)
465 }
466
467
468
469 func (ri *ABIParamResultInfo) String() string {
470 res := ""
471 for k, p := range ri.inparams {
472 res += fmt.Sprintf("IN %d: %s\n", k, p.ToString(ri.config, false))
473 }
474 for k, r := range ri.outparams {
475 res += fmt.Sprintf("OUT %d: %s\n", k, r.ToString(ri.config, false))
476 }
477 res += fmt.Sprintf("offsetToSpillArea: %d spillAreaSize: %d",
478 ri.offsetToSpillArea, ri.spillAreaSize)
479 return res
480 }
481
482
483
484 type assignState struct {
485 rTotal RegAmounts
486 rUsed RegAmounts
487 stackOffset int64
488 spillOffset int64
489 }
490
491
492 func align(a int64, t *types.Type) int64 {
493 return alignTo(a, int(uint8(t.Alignment())))
494 }
495
496
497 func alignTo(a int64, t int) int64 {
498 if t == 0 {
499 return a
500 }
501 return types.RoundUp(a, int64(t))
502 }
503
504
505 func nextSlot(offsetp *int64, typ *types.Type) int64 {
506 offset := align(*offsetp, typ)
507 *offsetp = offset + typ.Size()
508 return offset
509 }
510
511
512
513
514 func (state *assignState) allocateRegs(regs []RegIndex, t *types.Type) []RegIndex {
515 if t.Size() == 0 {
516 return regs
517 }
518 ri := state.rUsed.intRegs
519 rf := state.rUsed.floatRegs
520 if t.IsScalar() || t.IsPtrShaped() {
521 if t.IsComplex() {
522 regs = append(regs, RegIndex(rf+state.rTotal.intRegs), RegIndex(rf+1+state.rTotal.intRegs))
523 rf += 2
524 } else if t.IsFloat() {
525 regs = append(regs, RegIndex(rf+state.rTotal.intRegs))
526 rf += 1
527 } else {
528 n := (int(t.Size()) + types.RegSize - 1) / types.RegSize
529 for i := 0; i < n; i++ {
530 regs = append(regs, RegIndex(ri))
531 ri += 1
532 }
533 }
534 state.rUsed.intRegs = ri
535 state.rUsed.floatRegs = rf
536 return regs
537 } else {
538 typ := t.Kind()
539 switch typ {
540 case types.TARRAY:
541 for i := int64(0); i < t.NumElem(); i++ {
542 regs = state.allocateRegs(regs, t.Elem())
543 }
544 return regs
545 case types.TSTRUCT:
546 for _, f := range t.Fields() {
547 regs = state.allocateRegs(regs, f.Type)
548 }
549 return regs
550 case types.TSLICE:
551 return state.allocateRegs(regs, synthSlice)
552 case types.TSTRING:
553 return state.allocateRegs(regs, synthString)
554 case types.TINTER:
555 return state.allocateRegs(regs, synthIface)
556 }
557 }
558 base.Fatalf("was not expecting type %s", t)
559 panic("unreachable")
560 }
561
562
563 var synthOnce sync.Once
564
565
566
567 var synthSlice *types.Type
568 var synthString *types.Type
569 var synthIface *types.Type
570
571
572
573 func setup() {
574 synthOnce.Do(func() {
575 fname := types.BuiltinPkg.Lookup
576 nxp := src.NoXPos
577 bp := types.NewPtr(types.Types[types.TUINT8])
578 it := types.Types[types.TINT]
579 synthSlice = types.NewStruct([]*types.Field{
580 types.NewField(nxp, fname("ptr"), bp),
581 types.NewField(nxp, fname("len"), it),
582 types.NewField(nxp, fname("cap"), it),
583 })
584 types.CalcStructSize(synthSlice)
585 synthString = types.NewStruct([]*types.Field{
586 types.NewField(nxp, fname("data"), bp),
587 types.NewField(nxp, fname("len"), it),
588 })
589 types.CalcStructSize(synthString)
590 unsp := types.Types[types.TUNSAFEPTR]
591 synthIface = types.NewStruct([]*types.Field{
592 types.NewField(nxp, fname("f1"), unsp),
593 types.NewField(nxp, fname("f2"), unsp),
594 })
595 types.CalcStructSize(synthIface)
596 })
597 }
598
599
600
601
602
603 func (state *assignState) assignParam(typ *types.Type, name *ir.Name, isResult bool) ABIParamAssignment {
604 registers := state.tryAllocRegs(typ)
605
606 var offset int64 = -1
607 if registers == nil {
608 offset = nextSlot(&state.stackOffset, typ)
609 } else if !isResult {
610 offset = nextSlot(&state.spillOffset, typ)
611 }
612
613 return ABIParamAssignment{
614 Type: typ,
615 Name: name,
616 Registers: registers,
617 offset: int32(offset),
618 }
619 }
620
621
622
623 func (state *assignState) tryAllocRegs(typ *types.Type) []RegIndex {
624 if typ.Size() == 0 {
625 return nil
626 }
627
628 intRegs, floatRegs := typ.Registers()
629 if int(intRegs) > state.rTotal.intRegs-state.rUsed.intRegs || int(floatRegs) > state.rTotal.floatRegs-state.rUsed.floatRegs {
630 return nil
631 }
632
633 regs := make([]RegIndex, 0, int(intRegs)+int(floatRegs))
634 return state.allocateRegs(regs, typ)
635 }
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657 func (pa *ABIParamAssignment) ComputePadding(storage []uint64) []uint64 {
658 nr := len(pa.Registers)
659 padding := storage[:nr]
660 for i := 0; i < nr; i++ {
661 padding[i] = 0
662 }
663 if pa.Type.Kind() != types.TSTRUCT || nr == 0 {
664 return padding
665 }
666 types := make([]*types.Type, 0, nr)
667 types = appendParamTypes(types, pa.Type)
668 if len(types) != nr {
669 panic("internal error")
670 }
671 off := int64(0)
672 for idx, t := range types {
673 ts := t.Size()
674 off += int64(ts)
675 if idx < len(types)-1 {
676 noff := align(off, types[idx+1])
677 if noff != off {
678 padding[idx] = uint64(noff - off)
679 }
680 }
681 }
682 return padding
683 }
684
View as plain text