如何在汇编语言中为结构指针定义一个函数?

huangapple go评论83阅读模式
英文:

How to define a function on a struct pointer in assembly?

问题

例如:

// dummy.go
type dummy struct {
    p uintptr
}

func (d dummy) Get(i int) uint64
//func (d *dummy) Get(i int) uint64 //无法在汇编中定义*dummy

func (d dummy) Get 可以定义为:

// dummy_amd64.s
#include "textflag.h"
TEXT ·dummy·Get(SB),NOSPLIT,$0
    MOVQ $42, 24(SP)
    RET

我尝试了以下几种方式:

TEXT "".(*dummy).Get+0(SB),4,$0-24 //output from 6g -S
TEXT ·(*dummy)·Get+0(SB),4,$0
TEXT ·*dummy·Get(SB),NOSPLIT,$0
//and
TEXT ·(*dummy)·Get(SB),NOSPLIT,$0

它们都给我返回了相同的错误:

语法错误,最后一个名称:""

我确定我漏掉了一些明显的东西,但我似乎无法弄清楚。

英文:

For example:

// dummy.go
type dummy struct {
	p uintptr
}

func (d dummy) Get(i int) uint64
//func (d *dummy) Get(i int) uint64 //no way to define *dummy in assembly

func (d dummy) Get can be defined as:

// dummy_amd64.s
#include "textflag.h"
TEXT ·dummy·Get(SB),NOSPLIT,$0
	MOVQ $42, 24(SP)
	RET

I tried

TEXT "".(*dummy).Get+0(SB),4,$0-24 //output from 6g -S
TEXT ""·(*dummy)·Get+0(SB),4,$0
TEXT ·*dummy·Get(SB),NOSPLIT,$0
//and
TEXT ·(*dummy)·Get(SB),NOSPLIT,$0

All of them gives me the same error:
>syntax error, last name: "".

I'm sure I'm missing something obvious but I can't seem to figure it out.

答案1

得分: 3

这在当前的工具链中实际上是不可能的。该上下文在问题4978中有解释。

请注意,有一个简单的补丁可以启用此功能,但只有少数人在使用它。

您可以编写一个普通的汇编函数(即非方法),并在Go方法中实现对该汇编函数的调用。但是,编译器不会优化掉额外的调用。

解决这个问题的一个可能的方法是实现一些支持,允许将汇编函数内联到Go代码中-这将带来更多的好处。据我了解,过去曾经讨论过这个问题,但目前还没有计划。

英文:

This is actually not possible with the current toolchain. The context is explained in issue 4978

Note that there is a simple patch to enable this feature - but only a few people are using it.

You could write a normal assembly function (i.e. not method), and implement a call in the Go method, to this assembly function. But the extra call will not be optimized away by the compiler.

A possible workaround to this problem would be to implement some support to allow assembly functions to be inlined in Go code - which would bring more benefits. My understanding is it has been discussed in the past, but it is not planned yet.

huangapple
  • 本文由 发表于 2014年9月22日 11:40:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/25966384.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定