关于 ARM 上的 Objective C 调用约定和参数传递的问题 [英] Question about Objective C calling convention and argument passing on ARM
问题描述
当我调用像这样的目标 C 方法时,我想知道目标 C 运行时如何处理参数
I want to know how objective C runtime handle arguments when I call a objective C method like
[NSString stringWithFomat:@"%@, %@", @"Hello", @"World"]
这个目标 C 调用有三个参数,与 ARM 系统上的典型方式相比,它是如何工作的.我知道寄存器 r0, r1, r2, r3 将保存前 4 个参数,还有其他参数如何?它如何将它们放在堆栈上并稍后弹出它们?
There are three arguments for this objective C call, how does it work compared to typical way on a ARM system. I have known register r0, r1, r2, r3 will hold first 4 arguments, how about there are additional arguments? How does it put them on a stack and pop them later?
推荐答案
对于返回简单类型的函数:
For functions that returns a simple type:
r0 = self (NSString)
r1 = _cmd (@selector(stringWithFormat:))
r2 = 1st argument (@"%@, %@")
r3 = 2nd argument (@"Hello")
然后剩下的放在栈上:
[sp,#0] = 3rd argument (@"World")
[sp,#4] = 4th argument (does not exist in your example)
...
当然,这里的参数"是指一个 4 字节的对象.如果参数有 >4 个字节,那么它将被拆分,例如
Of course, "argument" here means a 4-byte object. If the argument has >4 bytes then it will be split out, e.g.
-[UIView initWithFrame:rect];
r0 = self
r1 = _cmd
r2 = rect.origin.x
r3 = rect.origin.y
[sp,#0] = rect.size.width
[sp,#4] = rect.size.height
返回的值(最多 16 个字节)会放在 r0、r1、r2、r3 中.
The returned value (up to 16 bytes) will be placed in r0, r1, r2, r3.
对于返回结构体的函数:r0
用于存储返回值的指针.
For functions that returns a struct: r0
is used to store the pointer of the return value.
NSRange retval = [self rangeOfString:string options:options range:range]
r0 = &retval (of type NSRange*)
r1 = self
r2 = _cmd (@selector(rangeOfString:options:range:))
r3 = string
[sp,#0] = options
[sp,#4] = range.location
[sp,#8] = range.length
这篇关于关于 ARM 上的 Objective C 调用约定和参数传递的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!