@@ -30,19 +30,17 @@ var templateArchTrampolines = template.Must(template.New("arch_trampolines").Fun
3030
3131// these trampolines map the gcc ABI to Go ABI and then calls into the Go equivalent functions.
3232{{ range .Symbols }}
33- TEXT {{.Name}}_trampoline(SB), NOSPLIT, ${{argCount .Args | mul $.Arch.WordSize}}
34- {{- range $i, $arg := .Args }}
35- {{- if $arg.Name }}
33+ TEXT _x_cgo_{{.Name}}_trampoline(SB), NOSPLIT, ${{mul .ArgsCount $.Arch.WordSize}}
34+ {{- range $i := .ArgsCount }}
3635 {{- $src := index $.Arch.C.IntRegArgs $i }}
3736 {{- $dst := index $.Arch.GoABI0.IntRegArgs $i }}
3837 {{- if ne $src $dst }}
3938 {{$.Arch.MOV}} {{$src}}, {{$dst}}
4039 {{- end }}
4140{{- end }}
42- {{- end }}
43- {{$.Arch.MOV}} ·{{.Name}}_call(SB), {{$.Arch.VolatileReg}}
41+ {{$.Arch.MOV}} ·_cgo_{{.Name}}_call(SB), {{$.Arch.VolatileReg}}
4442 {{$.Arch.MOV}} ({{$.Arch.VolatileReg}}), {{$.Arch.VolatileReg}}
45- {{- if eq $.Arch.Name "loong64" }}
43+ {{- if $.Arch.CallNeedsParens }}
4644 CALL ({{$.Arch.VolatileReg}})
4745{{- else }}
4846 CALL {{$.Arch.VolatileReg}}
@@ -147,19 +145,16 @@ import (
147145)
148146
149147{{ range .Symbols }}
150- {{- $cgoName := getCgoName .Name }}
151- {{- if $cgoName }}
152- //go:linkname {{.Name}}_trampoline {{.Name}}_trampoline
153- //go:linkname {{$cgoName}} {{$cgoName}}
154- var {{.Name}}_trampoline byte
155- var {{$cgoName}} = &{{.Name}}_trampoline
156- {{- end }}
148+ //go:linkname _x_cgo_{{.Name}}_trampoline _x_cgo_{{.Name}}_trampoline
149+ //go:linkname _cgo_{{.Name}} {{if .Package}}{{.Package}}.{{end}}_cgo_{{.Name}}
150+ var _x_cgo_{{.Name}}_trampoline byte
151+ var _cgo_{{.Name}} = &_x_cgo_{{.Name}}_trampoline
157152{{- end }}
158153
159154var (
160155 threadentry_call = threadentry
161156{{- range .Symbols }}
162- {{.Name}}_call = {{.Name}}
157+ _cgo_ {{.Name}}_call = x_cgo_ {{.Name}}
163158{{- end }}
164159)
165160` ))
@@ -205,35 +200,9 @@ var (
205200)
206201
207202var funcs = map [string ]any {
208- "hasPrefix" : strings .HasPrefix ,
209- "imports" : imports ,
210- "argCount" : argCount ,
211- "mul" : func (a , b int ) int { return a * b },
212- "getCgoName" : getCgoName ,
213- }
214-
215- func getCgoName (name string ) string {
216- switch name {
217- case "x_cgo_init" :
218- return "_cgo_init"
219- case "x_cgo_thread_start" :
220- return "_cgo_thread_start"
221- case "x_cgo_notify_runtime_init_done" :
222- return "_cgo_notify_runtime_init_done"
223- case "x_cgo_bindm" :
224- return "_cgo_bindm"
225- }
226- return ""
227- }
228-
229- func argCount (args [5 ]Arg ) int {
230- count := 0
231- for _ , arg := range args {
232- if arg .Name != "" {
233- count ++
234- }
235- }
236- return count
203+ "hasPrefix" : strings .HasPrefix ,
204+ "imports" : imports ,
205+ "mul" : func (a , b int ) int { return a * b },
237206}
238207
239208var GOOSes = []string {"darwin" , "freebsd" , "linux" , "netbsd" }
@@ -382,73 +351,77 @@ func run() error {
382351 return err
383352 }
384353 }
385- if err := execute (templateCallbacks , "zcallbacks.go" , struct { Symbols []Symbol }{Symbols : archTrampolines }); err != nil {
354+ if err := execute (templateCallbacks , "zcallbacks.go" , struct { Symbols []AsmGoSymbol }{Symbols : asmGoSymbols }); err != nil {
386355 return err
387356 }
388357 return nil
389358}
390359
391360type Arch struct {
392- Name string // as in runtime.GOARCH
393- GoABIInternal ABI // if empty, same as GoABI0
394- GoABI0 ABI
395- C ABI
396- WordSize int // 4 on 32-bit systems, 8 on 64-bit systems
397- MOV string // MOV instruction, e.g., "MOVL" or "MOVQ"
398- VolatileReg string // Scratch register for intermediate values
361+ Name string // as in runtime.GOARCH
362+ GoABI0 ABI
363+ C ABI
364+ WordSize int // 4 on 32-bit systems, 8 on 64-bit systems
365+ MOV string // MOV instruction, e.g., "MOVL" or "MOVQ"
366+ VolatileReg string // Scratch register for intermediate values
367+ CallNeedsParens bool
399368}
400369
401370type ABI struct {
402371 IntRegArgs [5 ]string // Name of integer argument registers in order. If empty, stack-based calling convention is used.
403372 OutRegArg string // Name of the register for the single return value. If empty, stack-based calling convention is used.
404373}
405374
375+ // AsmGoSymbols are symbols that called from Go Assembly.
376+ type AsmGoSymbol struct {
377+ Name string
378+ ArgsCount int
379+ Package string
380+ }
381+
406382var (
407383 archs = []Arch {
408384 {
409- Name : "amd64" ,
410- WordSize : 8 ,
411- GoABIInternal : ABI {IntRegArgs : [5 ]string {"AX" , "BX" , "CX" , "DX" , "SI" }, OutRegArg : "AX" },
412- GoABI0 : ABI {IntRegArgs : [5 ]string {"AX" , "BX" , "CX" , "DX" , "SI" }, OutRegArg : "AX" },
413- C : ABI {IntRegArgs : [5 ]string {"DI" , "SI" , "DX" , "CX" , "R8" }, OutRegArg : "AX" },
414- MOV : "MOVQ" ,
415- VolatileReg : "R11" ,
385+ Name : "amd64" ,
386+ WordSize : 8 ,
387+ GoABI0 : ABI {IntRegArgs : [5 ]string {"AX" , "BX" , "CX" , "DX" , "SI" }, OutRegArg : "AX" },
388+ C : ABI {IntRegArgs : [5 ]string {"DI" , "SI" , "DX" , "CX" , "R8" }, OutRegArg : "AX" },
389+ MOV : "MOVQ" ,
390+ VolatileReg : "R11" ,
416391 },
417392 {
418- Name : "arm64" ,
419- WordSize : 8 ,
420- GoABIInternal : ABI {IntRegArgs : [5 ]string {"R0" , "R1" , "R2" , "R3" , "R4" }, OutRegArg : "R0" },
421- GoABI0 : ABI {IntRegArgs : [5 ]string {"R0" , "R1" , "R2" , "R3" , "R4" }, OutRegArg : "R0" },
422- C : ABI {IntRegArgs : [5 ]string {"R0" , "R1" , "R2" , "R3" , "R4" }, OutRegArg : "R0" },
423- MOV : "MOVD" ,
424- VolatileReg : "R9" ,
393+ Name : "arm64" ,
394+ WordSize : 8 ,
395+ GoABI0 : ABI {IntRegArgs : [5 ]string {"R0" , "R1" , "R2" , "R3" , "R4" }, OutRegArg : "R0" },
396+ C : ABI {IntRegArgs : [5 ]string {"R0" , "R1" , "R2" , "R3" , "R4" }, OutRegArg : "R0" },
397+ MOV : "MOVD" ,
398+ VolatileReg : "R9" ,
425399 },
426400 {
427- Name : "loong64" ,
428- WordSize : 8 ,
429- GoABIInternal : ABI {IntRegArgs : [5 ]string {"R4" , "R5" , "R6" , "R7" , "R8" }, OutRegArg : "R4" },
430- GoABI0 : ABI {IntRegArgs : [5 ]string {"R4" , "R5" , "R6" , "R7" , "R8" }, OutRegArg : "R4" },
431- C : ABI { IntRegArgs : [ 5 ] string { "R4" , "R5" , "R6" , "R7" , "R8" }, OutRegArg : "R4" } ,
432- MOV : "MOVV " ,
433- VolatileReg : "R23" ,
401+ Name : "loong64" ,
402+ WordSize : 8 ,
403+ GoABI0 : ABI {IntRegArgs : [5 ]string {"R4" , "R5" , "R6" , "R7" , "R8" }, OutRegArg : "R4" },
404+ C : ABI {IntRegArgs : [5 ]string {"R4" , "R5" , "R6" , "R7" , "R8" }, OutRegArg : "R4" },
405+ MOV : "MOVV" ,
406+ VolatileReg : "R23 " ,
407+ CallNeedsParens : true ,
434408 },
435409 {
436- Name : "riscv64" ,
437- WordSize : 8 ,
438- GoABIInternal : ABI {IntRegArgs : [5 ]string {"X10" , "X11" , "X12" , "X13" , "X14" }, OutRegArg : "X10" },
439- GoABI0 : ABI {IntRegArgs : [5 ]string {"X10" , "X11" , "X12" , "X13" , "X14" }, OutRegArg : "X10" },
440- C : ABI {IntRegArgs : [5 ]string {"X10" , "X11" , "X12" , "X13" , "X14" }, OutRegArg : "X10" },
441- MOV : "MOV" ,
442- VolatileReg : "X5" ,
410+ Name : "riscv64" ,
411+ WordSize : 8 ,
412+ GoABI0 : ABI {IntRegArgs : [5 ]string {"X10" , "X11" , "X12" , "X13" , "X14" }, OutRegArg : "X10" },
413+ C : ABI {IntRegArgs : [5 ]string {"X10" , "X11" , "X12" , "X13" , "X14" }, OutRegArg : "X10" },
414+ MOV : "MOV" ,
415+ VolatileReg : "X5" ,
443416 },
444417 }
445- archTrampolines = []Symbol {
446- {"x_cgo_init " , [ 5 ] Arg {{ "G" , "*g" }, { "setg" , "uintptr" }}, "" , nil },
447- {"x_cgo_thread_start " , [ 5 ] Arg {{ "ts" , "*ThreadStart" }}, "" , nil },
448- {"x_cgo_setenv " , [ 5 ] Arg {{ "arg" , "*uintptr" }}, "" , nil },
449- {"x_cgo_unsetenv " , [ 5 ] Arg {{ "arg" , "*uintptr" }}, "" , nil },
450- {"x_cgo_notify_runtime_init_done " , [ 5 ] Arg {} , "" , nil },
451- {"x_cgo_bindm " , [ 5 ] Arg {{ "g" , "unsafe.Pointer" }}, "" , nil },
418+ asmGoSymbols = []AsmGoSymbol {
419+ {"init " , 2 , "" },
420+ {"thread_start " , 1 , "" },
421+ {"setenv " , 1 , "runtime" },
422+ {"unsetenv " , 1 , "runtime" },
423+ {"notify_runtime_init_done " , 0 , "" },
424+ {"bindm " , 1 , "" },
452425 }
453426)
454427
@@ -468,11 +441,11 @@ func writeArchTrampolines(arch Arch) error {
468441 }
469442 data := struct {
470443 Tag string
471- Symbols []Symbol
444+ Symbols []AsmGoSymbol
472445 Arch Arch
473446 }{
474447 Tag : tag ,
475- Symbols : archTrampolines ,
448+ Symbols : asmGoSymbols ,
476449 Arch : arch ,
477450 }
478451 return execute (templateArchTrampolines , fmt .Sprintf ("ztrampolines_%s.s" , arch .Name ), data )
0 commit comments