当前位置:网站首页>Assembly analysis swift polymorphism principle
Assembly analysis swift polymorphism principle
2022-07-28 23:17:00 【Fluttering moth】
polymorphic
First, let's take a look at the instance method call of the structure

Take a look at the compilation
After compiling, you can determine who will be called finally , Because the function address is written dead .
Switch to class Then let's take a look

Take another look at the compilation
You can find a lot more assembly code , So when inheritance is not needed , Use struct It will be more efficient .
adopt si Go inside the method

It is certain that it is entering speak Method , Similarly, other methods are called in the same way .
Now let's formally enter the link of polymorphism principle , Let's first look at what polymorphism is

The parent class pointer points to the child class .
-0x48(%rbp) Is the address value of the global object ,%r13 Is the address value of the heap space object , The second sentence is to get the address of the object in the heap space 8 Bytes of data to %rax, therefore %rax It's in there dog Before the object memory 8 Bytes , That is, type information , The memory address of the method will be placed in the type information , That is, you know whether to call the methods of the parent class or the methods of the subclass .
dog When an object calls a method , First, I will get the type information , Then add the offset , Here is 0x50, It will go directly to the memory address of the method , Remove 8 A byte comes out , Namely speak Method .eat And other methods are called in the same way .
There is a function table in the type information , This function table is in Dog Class, for example
0x50 High memory
Dog.speak() //8 Bytes
Dog.eat()
Animal.sleep()
Dog.run()
From these three method calls, we can see that the offset is 0x50.
No matter how many are created dog object , Before that 8 Each byte is the same , Are the same type information .
There are so many spaces in our code memory , Heap space 、 Global area 、 Stack space 、 Code section . So where does the type information exist , First, eliminate stack space , Because it always exists, and it doesn't come out of the stack after calling , You need to stay in memory all the time , Let's see which area it is through compilation .


0x102cce879 + 0x9867 = 0x102CD80E0, That is to say dog object ( Global variables ) Address value of , You can contrast Dog Type information address , It's very much like .

Heap space Dog Object address value , The address value of the code area is the leftmost address of the assembly , Like this 0x100c36800, In terms of address size, it is : Code section 、 Global area 、 Heap space 、 Stack space . We can roughly see from the address , The type information is in the global area .
It can also be used MachOView Prying into mach-o file , Look at the type information in TEXT Paragraph or DATA paragraph .
summary :
Swift The realization principle of polymorphism in , Is similar to the C++ The virtual table of , Directly put the method address of the object to be called in the future into the type information , And it can be determined after compilation .
边栏推荐
- 投资500亿元!中芯京城正式注册成立!
- 一种分布式深度学习编程新范式:Global Tensor
- WebView whitelist
- This year, MediaTek 5g chip shipments are expected to reach 50million sets!
- 软件测试工具fiddler postman jmeter charlse核心功能总结
- (important) first knowledge of C language -- function
- 这个胶水有多强呢?
- Bullet frame mask layer "recommended collection"
- Reading of "robust and communication efficient federated learning from non-i.i.d. data"
- Sqlilabs-2 (breakthrough record)
猜你喜欢

Target detection notes SSD

sql优化常用的几种方法

6 个超级良心的开源教程!

Basic concept of MySQL database and deployment of MySQL version 8.0 (I)

Shell script foundation - shell operation principle + variable and array definitions
![[physical application] atmospheric absorption loss with matlab code](/img/72/e6ac23012a59ac48a37bcbb068890b.png)
[physical application] atmospheric absorption loss with matlab code

【物理应用】大气吸收损耗附matlab代码

The safety dog has been selected into many details of cloud security panorama 2.0
![[image segmentation] vein segmentation based on directional valley detection with matlab code](/img/82/7b7b761c975cd5c2f5b8f3e43592d2.png)
[image segmentation] vein segmentation based on directional valley detection with matlab code

Cglib create proxy
随机推荐
Empowering Chinese core entrepreneurs! See how Moore elite solves the development problems of small and medium-sized chip Enterprises
Source code analysis of kotlin collaborative process startup
Thesis reading (2) - vggnet of classification
MySQL foundation - data query
Cnpm installation steps
GCD summary
投资1450亿欧元!欧盟17国宣布联合发展半导体技术
【雷达】基于核聚类实现雷达信号在线分选附matlab代码
GCD implementation and arc, blocks, GCD usage examples
终端输出g_debug()信息
解决控制文件全部损坏的异常
Advanced C language: pointer (3)
6 个超级良心的开源教程!
Thesis reading (0) - alexnet of classification
【C语言】三子棋小游戏实现
【图像分割】基于方向谷形检测实现静脉纹路分割附MATLAB代码
Summary of core functions of software testing tool Fiddler postman JMeter charlse
recursion and iteration
[image segmentation] vein segmentation based on directional valley detection with matlab code
程序员成长第三十篇:识别真伪需求的神器