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
197 func appendParamOffsets(offsets []int64, at int64, t *types.Type) ([]int64, int64) {
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 te := t.Elem()
214 for i := int64(0); i < t.NumElem(); i++ {
215 at = align(at, te)
216 offsets, at = appendParamOffsets(offsets, at, te)
217 }
218 case types.TSTRUCT:
219 at0 := at
220 for i, f := range t.Fields() {
221 at = at0 + f.Offset
222 offsets, at = appendParamOffsets(offsets, at, f.Type)
223 if f.Type.Size() == 0 && i == t.NumFields()-1 {
224 at++
225 }
226 }
227 at = align(at, t)
228 case types.TSLICE:
229 return appendParamOffsets(offsets, at, synthSlice)
230 case types.TSTRING:
231 return appendParamOffsets(offsets, at, synthString)
232 case types.TINTER:
233 return appendParamOffsets(offsets, at, synthIface)
234 }
235 }
236 return offsets, at
237 }
238
239
240
241
242
243
244
245
246 func (a *ABIParamAssignment) FrameOffset(i *ABIParamResultInfo) int64 {
247 if a.offset == -1 {
248 base.Fatalf("function parameter has no ABI-defined frame-pointer offset")
249 }
250 if len(a.Registers) == 0 {
251 return int64(a.offset) - i.config.LocalsOffset()
252 }
253
254 return int64(a.offset) + i.SpillAreaOffset() - i.config.LocalsOffset()
255 }
256
257
258 type RegAmounts struct {
259 intRegs int
260 floatRegs int
261 }
262
263
264
265 type ABIConfig struct {
266
267 offsetForLocals int64
268 regAmounts RegAmounts
269 which obj.ABI
270 }
271
272
273
274 func NewABIConfig(iRegsCount, fRegsCount int, offsetForLocals int64, which uint8) *ABIConfig {
275 return &ABIConfig{offsetForLocals: offsetForLocals, regAmounts: RegAmounts{iRegsCount, fRegsCount}, which: obj.ABI(which)}
276 }
277
278
279
280
281 func (config *ABIConfig) Copy() *ABIConfig {
282 return config
283 }
284
285
286 func (config *ABIConfig) Which() obj.ABI {
287 return config.which
288 }
289
290
291
292
293 func (config *ABIConfig) LocalsOffset() int64 {
294 return config.offsetForLocals
295 }
296
297
298
299
300 func (config *ABIConfig) FloatIndexFor(r RegIndex) int64 {
301 return int64(r) - int64(config.regAmounts.intRegs)
302 }
303
304
305
306
307 func (config *ABIConfig) NumParamRegs(typ *types.Type) int {
308 intRegs, floatRegs := typ.Registers()
309 if intRegs == math.MaxUint8 && floatRegs == math.MaxUint8 {
310 base.Fatalf("cannot represent parameters of type %v in registers", typ)
311 }
312 return int(intRegs) + int(floatRegs)
313 }
314
315
316
317
318
319 func (config *ABIConfig) ABIAnalyzeTypes(params, results []*types.Type) *ABIParamResultInfo {
320 setup()
321 s := assignState{
322 stackOffset: config.offsetForLocals,
323 rTotal: config.regAmounts,
324 }
325
326 assignParams := func(params []*types.Type, isResult bool) []ABIParamAssignment {
327 res := make([]ABIParamAssignment, len(params))
328 for i, param := range params {
329 res[i] = s.assignParam(param, nil, isResult)
330 }
331 return res
332 }
333
334 info := &ABIParamResultInfo{config: config}
335
336
337 info.inparams = assignParams(params, false)
338 s.stackOffset = types.RoundUp(s.stackOffset, int64(types.RegSize))
339 info.inRegistersUsed = s.rUsed.intRegs + s.rUsed.floatRegs
340
341
342 s.rUsed = RegAmounts{}
343 info.outparams = assignParams(results, true)
344
345
346 info.offsetToSpillArea = alignTo(s.stackOffset, types.RegSize)
347 info.spillAreaSize = alignTo(s.spillOffset, types.RegSize)
348 info.outRegistersUsed = s.rUsed.intRegs + s.rUsed.floatRegs
349
350 return info
351 }
352
353
354
355
356
357 func (config *ABIConfig) ABIAnalyzeFuncType(ft *types.Type) *ABIParamResultInfo {
358 setup()
359 s := assignState{
360 stackOffset: config.offsetForLocals,
361 rTotal: config.regAmounts,
362 }
363
364 assignParams := func(params []*types.Field, isResult bool) []ABIParamAssignment {
365 res := make([]ABIParamAssignment, len(params))
366 for i, param := range params {
367 var name *ir.Name
368 if param.Nname != nil {
369 name = param.Nname.(*ir.Name)
370 }
371 res[i] = s.assignParam(param.Type, name, isResult)
372 }
373 return res
374 }
375
376 info := &ABIParamResultInfo{config: config}
377
378
379 info.inparams = assignParams(ft.RecvParams(), false)
380 s.stackOffset = types.RoundUp(s.stackOffset, int64(types.RegSize))
381 info.inRegistersUsed = s.rUsed.intRegs + s.rUsed.floatRegs
382
383
384 s.rUsed = RegAmounts{}
385 info.outparams = assignParams(ft.Results(), true)
386
387
388 info.offsetToSpillArea = alignTo(s.stackOffset, types.RegSize)
389 info.spillAreaSize = alignTo(s.spillOffset, types.RegSize)
390 info.outRegistersUsed = s.rUsed.intRegs + s.rUsed.floatRegs
391 return info
392 }
393
394
395
396
397
398
399
400
401 func (config *ABIConfig) ABIAnalyze(t *types.Type, setNname bool) *ABIParamResultInfo {
402 result := config.ABIAnalyzeFuncType(t)
403
404
405 for i, f := range t.RecvParams() {
406 config.updateOffset(result, f, result.inparams[i], false, setNname)
407 }
408 for i, f := range t.Results() {
409 config.updateOffset(result, f, result.outparams[i], true, setNname)
410 }
411 return result
412 }
413
414 func (config *ABIConfig) updateOffset(result *ABIParamResultInfo, f *types.Field, a ABIParamAssignment, isResult, setNname bool) {
415 if f.Offset != types.BADWIDTH {
416 base.Fatalf("field offset for %s at %s has been set to %d", f.Sym, base.FmtPos(f.Pos), f.Offset)
417 }
418
419
420 if !isResult || len(a.Registers) == 0 {
421
422
423 off := a.FrameOffset(result)
424 if setNname && f.Nname != nil {
425 f.Nname.(*ir.Name).SetFrameOffset(off)
426 f.Nname.(*ir.Name).SetIsOutputParamInRegisters(false)
427 }
428 } else {
429 if setNname && f.Nname != nil {
430 fname := f.Nname.(*ir.Name)
431 fname.SetIsOutputParamInRegisters(true)
432 fname.SetFrameOffset(0)
433 }
434 }
435 }
436
437
438
439
440
441
442 func (c *RegAmounts) regString(r RegIndex) string {
443 if int(r) < c.intRegs {
444 return fmt.Sprintf("I%d", int(r))
445 } else if int(r) < c.intRegs+c.floatRegs {
446 return fmt.Sprintf("F%d", int(r)-c.intRegs)
447 }
448 return fmt.Sprintf("<?>%d", r)
449 }
450
451
452
453 func (ri *ABIParamAssignment) ToString(config *ABIConfig, extra bool) string {
454 regs := "R{"
455 offname := "spilloffset"
456 if len(ri.Registers) == 0 {
457 offname = "offset"
458 }
459 for _, r := range ri.Registers {
460 regs += " " + config.regAmounts.regString(r)
461 if extra {
462 regs += fmt.Sprintf("(%d)", r)
463 }
464 }
465 if extra {
466 regs += fmt.Sprintf(" | #I=%d, #F=%d", config.regAmounts.intRegs, config.regAmounts.floatRegs)
467 }
468 return fmt.Sprintf("%s } %s: %d typ: %v", regs, offname, ri.offset, ri.Type)
469 }
470
471
472
473 func (ri *ABIParamResultInfo) String() string {
474 res := ""
475 for k, p := range ri.inparams {
476 res += fmt.Sprintf("IN %d: %s\n", k, p.ToString(ri.config, false))
477 }
478 for k, r := range ri.outparams {
479 res += fmt.Sprintf("OUT %d: %s\n", k, r.ToString(ri.config, false))
480 }
481 res += fmt.Sprintf("offsetToSpillArea: %d spillAreaSize: %d",
482 ri.offsetToSpillArea, ri.spillAreaSize)
483 return res
484 }
485
486
487
488 type assignState struct {
489 rTotal RegAmounts
490 rUsed RegAmounts
491 stackOffset int64
492 spillOffset int64
493 }
494
495
496 func align(a int64, t *types.Type) int64 {
497 return alignTo(a, int(uint8(t.Alignment())))
498 }
499
500
501 func alignTo(a int64, t int) int64 {
502 if t == 0 {
503 return a
504 }
505 return types.RoundUp(a, int64(t))
506 }
507
508
509 func nextSlot(offsetp *int64, typ *types.Type) int64 {
510 offset := align(*offsetp, typ)
511 *offsetp = offset + typ.Size()
512 return offset
513 }
514
515
516
517
518 func (state *assignState) allocateRegs(regs []RegIndex, t *types.Type) []RegIndex {
519 if t.Size() == 0 {
520 return regs
521 }
522 ri := state.rUsed.intRegs
523 rf := state.rUsed.floatRegs
524 if t.IsScalar() || t.IsPtrShaped() {
525 if t.IsComplex() {
526 regs = append(regs, RegIndex(rf+state.rTotal.intRegs), RegIndex(rf+1+state.rTotal.intRegs))
527 rf += 2
528 } else if t.IsFloat() {
529 regs = append(regs, RegIndex(rf+state.rTotal.intRegs))
530 rf += 1
531 } else {
532 n := (int(t.Size()) + types.RegSize - 1) / types.RegSize
533 for i := 0; i < n; i++ {
534 regs = append(regs, RegIndex(ri))
535 ri += 1
536 }
537 }
538 state.rUsed.intRegs = ri
539 state.rUsed.floatRegs = rf
540 return regs
541 } else {
542 typ := t.Kind()
543 switch typ {
544 case types.TARRAY:
545 for i := int64(0); i < t.NumElem(); i++ {
546 regs = state.allocateRegs(regs, t.Elem())
547 }
548 return regs
549 case types.TSTRUCT:
550 for _, f := range t.Fields() {
551 regs = state.allocateRegs(regs, f.Type)
552 }
553 return regs
554 case types.TSLICE:
555 return state.allocateRegs(regs, synthSlice)
556 case types.TSTRING:
557 return state.allocateRegs(regs, synthString)
558 case types.TINTER:
559 return state.allocateRegs(regs, synthIface)
560 }
561 }
562 base.Fatalf("was not expecting type %s", t)
563 panic("unreachable")
564 }
565
566
567 var synthOnce sync.Once
568
569
570
571 var synthSlice *types.Type
572 var synthString *types.Type
573 var synthIface *types.Type
574
575
576
577 func setup() {
578 synthOnce.Do(func() {
579 fname := types.BuiltinPkg.Lookup
580 nxp := src.NoXPos
581 bp := types.NewPtr(types.Types[types.TUINT8])
582 it := types.Types[types.TINT]
583 synthSlice = types.NewStruct([]*types.Field{
584 types.NewField(nxp, fname("ptr"), bp),
585 types.NewField(nxp, fname("len"), it),
586 types.NewField(nxp, fname("cap"), it),
587 })
588 types.CalcStructSize(synthSlice)
589 synthString = types.NewStruct([]*types.Field{
590 types.NewField(nxp, fname("data"), bp),
591 types.NewField(nxp, fname("len"), it),
592 })
593 types.CalcStructSize(synthString)
594 unsp := types.Types[types.TUNSAFEPTR]
595 synthIface = types.NewStruct([]*types.Field{
596 types.NewField(nxp, fname("f1"), unsp),
597 types.NewField(nxp, fname("f2"), unsp),
598 })
599 types.CalcStructSize(synthIface)
600 })
601 }
602
603
604
605
606
607 func (state *assignState) assignParam(typ *types.Type, name *ir.Name, isResult bool) ABIParamAssignment {
608 registers := state.tryAllocRegs(typ)
609
610 var offset int64 = -1
611 if registers == nil {
612 offset = nextSlot(&state.stackOffset, typ)
613 } else if !isResult {
614 offset = nextSlot(&state.spillOffset, typ)
615 }
616
617 return ABIParamAssignment{
618 Type: typ,
619 Name: name,
620 Registers: registers,
621 offset: int32(offset),
622 }
623 }
624
625
626
627 func (state *assignState) tryAllocRegs(typ *types.Type) []RegIndex {
628 if typ.Size() == 0 {
629 return nil
630 }
631
632 intRegs, floatRegs := typ.Registers()
633 if int(intRegs) > state.rTotal.intRegs-state.rUsed.intRegs || int(floatRegs) > state.rTotal.floatRegs-state.rUsed.floatRegs {
634 return nil
635 }
636
637 regs := make([]RegIndex, 0, int(intRegs)+int(floatRegs))
638 return state.allocateRegs(regs, typ)
639 }
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661 func (pa *ABIParamAssignment) ComputePadding(storage []uint64) []uint64 {
662 nr := len(pa.Registers)
663 padding := storage[:nr]
664 for i := 0; i < nr; i++ {
665 padding[i] = 0
666 }
667 if pa.Type.Kind() != types.TSTRUCT || nr == 0 {
668 return padding
669 }
670 types := make([]*types.Type, 0, nr)
671 types = appendParamTypes(types, pa.Type)
672 if len(types) != nr {
673 panic("internal error")
674 }
675 offsets, _ := appendParamOffsets([]int64{}, 0, pa.Type)
676 off := int64(0)
677 for idx, t := range types {
678 ts := t.Size()
679 off += int64(ts)
680 if idx < len(types)-1 {
681 noff := offsets[idx+1]
682 if noff != off {
683 padding[idx] = uint64(noff - off)
684 }
685 }
686 }
687 return padding
688 }
689
View as plain text