Go中func append的实现在哪里?

用户名

我对go非常感兴趣,并尝试阅读go函数的实现。我发现其中一些功能没有实现。

如追加或调用:

// The append built-in function appends elements to the end of a slice. If
// it has sufficient capacity, the destination is resliced to accommodate the
// new elements. If it does not, a new underlying array will be allocated.
// Append returns the updated slice. It is therefore necessary to store the
// result of append, often in the variable holding the slice itself:
//  slice = append(slice, elem1, elem2)
//  slice = append(slice, anotherSlice...)
// As a special case, it is legal to append a string to a byte slice, like this:
//  slice = append([]byte("hello "), "world"...)
func append(slice []Type, elems ...Type) []Type

// call calls fn with a copy of the n argument bytes pointed at by arg.
// After fn returns, reflectcall copies n-retoffset result bytes
// back into arg+retoffset before returning. If copying result bytes back,
// the caller must pass the argument frame type as argtype, so that
// call can execute appropriate write barriers during the copy.
func call(argtype *rtype, fn, arg unsafe.Pointer, n uint32, retoffset uint32)

似乎没有调用C代码,因为使用cgo需要一些特殊的注释。这些功能的实现在哪里?

尼莫

您正在阅读和引用的代码只是具有一致文档的伪代码。内置函数很好地内置在语言中,因此包含在代码处理步骤(编译器)中。

简化的过程是:lexer将检测' append(...)'为APPEND令牌,解析器将APPEND根据情况/参数/环境将其转换为代码,将代码编写为汇编和汇编。中间步骤-的实现append-可以在此处的编译器中找到

append当查看示例程序的汇编时,最好看到呼叫发生的情况考虑一下:

b := []byte{'a'}
b = append(b, 'b')
println(string(b), cap(b))

运行它会产生以下输出:

ab 2

append调用将转换为如下所示的程序集:

// create new slice object
MOVQ    BX, "".b+120(SP)       // BX contains data addr., write to b.addr
MOVQ    BX, CX                 // store addr. in CX
MOVQ    AX, "".b+128(SP)       // AX contains len(b) == 1, write to b.len
MOVQ    DI, "".b+136(SP)       // DI contains cap(b) == 1, write to b.cap
MOVQ    AX, BX                 // BX now contains len(b)
INCQ    BX                     // BX++
CMPQ    BX, DI                 // compare new length (2) with cap (1)
JHI $1, 225                    // jump to grow code if len > cap
...
LEAQ    (CX)(AX*1), BX         // load address of newly allocated slice entry
MOVB    $98, (BX)              // write 'b' to loaded address

// grow code, call runtime.growslice(t *slicetype, old slice, cap int)
LEAQ    type.[]uint8(SB), BP
MOVQ    BP, (SP)               // load parameters onto stack
MOVQ    CX, 8(SP)
MOVQ    AX, 16(SP)
MOVQ    SI, 24(SP)
MOVQ    BX, 32(SP)
PCDATA  $0, $0
CALL    runtime.growslice(SB)  // call
MOVQ    40(SP), DI
MOVQ    48(SP), R8
MOVQ    56(SP), SI
MOVQ    R8, AX
INCQ    R8
MOVQ    DI, CX
JMP 108                        // jump back, growing done

如您所见,看不到CALL对函数调用的语句append这是append示例代码中调用的完整实现用不同的参数另一呼叫看起来不同(其他寄存器,取决于切片类型等不同的参数)。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

cryptsetup中的dm_task_create的实现在哪里?

来自分类Dev

int.dart类中isEven的实现在哪里?

来自分类Dev

cryptsetup中的dm_task_create的实现在哪里?

来自分类Dev

在 angular4 中,@angular/core 中组件的实现在哪里?

来自分类Dev

<pe:exporter>展示柜中的#{exporterController.customExporter}的实现在哪里

来自分类Dev

<type_traits>头文件中的某些实现在哪里?

来自分类Dev

JDBC中使用的Java中的“连接接口”的“ prepareStatement”的实现在哪里?

来自分类Dev

ASP.NET 5源代码中AuthenticationManager的具体实现在哪里?

来自分类Dev

<type_traits>头文件中的某些实现在哪里?

来自分类Dev

JDBC中使用的Java中的“连接接口”的“ prepareStatement”的实现在哪里?

来自分类Dev

TensorFlow 中 math_ops 三角函数的实现在哪里?

来自分类Dev

OpenMPI中的send()在哪里实现?

来自分类Dev

Windows中的stdlib在哪里实现?

来自分类Dev

现在,Microsoft Word中的矢量艺术字在哪里?

来自分类Dev

现在 IntelliJ 中的 Spring Roo 控制台在哪里?

来自分类Dev

glibc的套接字实现在哪里?

来自分类Dev

ConcurrentQueue TryDequeue实现在哪里

来自分类Dev

TensorFlow:RMSprop 的实际实现在哪里?

来自分类Dev

这些接口方法的实现在哪里?

来自分类Dev

NodaTime的.NET Core实现中的BCL DateTimeZoneProvider在哪里?

来自分类Dev

R中的Glicko-2实现,在哪里可以找到?

来自分类Dev

在XV6中在哪里实现FIFO和LIFO

来自分类Dev

在Rust中,在哪里可以找到结构的方法实现的引用?

来自分类Dev

在C ++中实现列表时在哪里声明Node结构

来自分类Dev

拆分反转的合并排序实现中的错误在哪里

来自分类Dev

MVP模式中的数据库查询在哪里实现?

来自分类Dev

Go的AppAssertionCredentials在哪里?

来自分类Dev

头文件如何知道函数原型的实现在哪里?

来自分类Dev

Stream <T>接口中的filter()方法的实现在哪里?

Related 相关文章

  1. 1

    cryptsetup中的dm_task_create的实现在哪里?

  2. 2

    int.dart类中isEven的实现在哪里?

  3. 3

    cryptsetup中的dm_task_create的实现在哪里?

  4. 4

    在 angular4 中,@angular/core 中组件的实现在哪里?

  5. 5

    <pe:exporter>展示柜中的#{exporterController.customExporter}的实现在哪里

  6. 6

    <type_traits>头文件中的某些实现在哪里?

  7. 7

    JDBC中使用的Java中的“连接接口”的“ prepareStatement”的实现在哪里?

  8. 8

    ASP.NET 5源代码中AuthenticationManager的具体实现在哪里?

  9. 9

    <type_traits>头文件中的某些实现在哪里?

  10. 10

    JDBC中使用的Java中的“连接接口”的“ prepareStatement”的实现在哪里?

  11. 11

    TensorFlow 中 math_ops 三角函数的实现在哪里?

  12. 12

    OpenMPI中的send()在哪里实现?

  13. 13

    Windows中的stdlib在哪里实现?

  14. 14

    现在,Microsoft Word中的矢量艺术字在哪里?

  15. 15

    现在 IntelliJ 中的 Spring Roo 控制台在哪里?

  16. 16

    glibc的套接字实现在哪里?

  17. 17

    ConcurrentQueue TryDequeue实现在哪里

  18. 18

    TensorFlow:RMSprop 的实际实现在哪里?

  19. 19

    这些接口方法的实现在哪里?

  20. 20

    NodaTime的.NET Core实现中的BCL DateTimeZoneProvider在哪里?

  21. 21

    R中的Glicko-2实现,在哪里可以找到?

  22. 22

    在XV6中在哪里实现FIFO和LIFO

  23. 23

    在Rust中,在哪里可以找到结构的方法实现的引用?

  24. 24

    在C ++中实现列表时在哪里声明Node结构

  25. 25

    拆分反转的合并排序实现中的错误在哪里

  26. 26

    MVP模式中的数据库查询在哪里实现?

  27. 27

    Go的AppAssertionCredentials在哪里?

  28. 28

    头文件如何知道函数原型的实现在哪里?

  29. 29

    Stream <T>接口中的filter()方法的实现在哪里?

热门标签

归档