当前位置:网站首页>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 .
边栏推荐
- There are four ways for Nacos to configure hot updates and multiple ways to read project configuration files, @value, @refreshscope, @nacosconfigurationproperties
- 软件测试工具fiddler postman jmeter charlse核心功能总结
- Thesis reading (0) - alexnet of classification
- Is 1E3 a floating point number?
- 6 open source tutorials of super conscience!
- Target detection notes -yolo
- Stm32f4 serial port burning [flymcu]
- Improvement 18 of yolov5: the loss function is improved to alpha IOU loss function
- Pgbench benchmark PostgreSQL
- 6 个超级良心的开源教程!
猜你喜欢

A new MPLS note from quigo, which must be read when taking the IE exam ---- quigo of Shangwen network

Reading of "robust and communication efficient federated learning from non-i.i.d. data"

Target detection notes SSD

Record a question about the order of trigonometric function exchange integrals

Nacos配置热更新的4种方式、读取项目配置文件的多种方式,@value,@RefreshScope,@NacosConfigurationProperties

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

MySQL foundation - data query

It's settled! All products of Nezha s will be launched on July 31

Advanced C language: pointer (2)

以价换量却遭遇销量“六连跌”,不再安全的沃尔沃还能翻身吗?
随机推荐
Cnpm installation steps
Learning experience sharing 5: yolov5 dataset division and Yolo format conversion
RouterOS 有限dns劫持及check
[filter tracking] target tracking based on EKF, TDOA and frequency difference positioning with matlab code
Introduction to address book export without code development platform
Xinhuazhang announced the completion of more than 200million a-round financing and the comprehensive layout of eda2.0 R & D
即将获售高通、联发科芯片,荣耀要超越华为做国内第一?
WebView optimization
The safety dog has been selected into many details of cloud security panorama 2.0
Target detection notes -yolo
二叉搜索树
Thesis reading (3) - googlenet of classification
Anr questions often asked in Android interviews
Reading of "robust and communication efficient federated learning from non-i.i.d. data"
6 个超级良心的开源教程!
DirectX修复工具下载(exagear模拟器数据包在哪里)
[C language] implementation of three piece chess games
这个胶水有多强呢?
pg_ Installation and use of RMAN "PostgreSQL"
Target detection notes fast r-cnn