如何计算ObjC方法类型编码中的数字? [英] How are the digits in ObjC method type encoding calculated?

查看:85
本文介绍了如何计算ObjC方法类型编码中的数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是我上一个问题的跟进: ObjC方法类型编码中的数字是什么字符串?

Is is a follow-up to my previous question: What are the digits in an ObjC method type encoding string?

说有一个编码:

v24@0:4:8@12B16@20

这些数字如何计算? B是一个字符,因此它应该仅占据1个字节(而不是4个字节).它与对齐"有关吗? void的大小是多少?

How are those numbers calculated? B is a char so it should occupy just 1 byte (not 4 bytes). Does it have something to do with "alignment"? What is the size of void?

按以下方式计算数字是否正确?在每个项目上询问sizeof并将结果四舍五入到4的倍数?而第一个数字成为所有其他数字的总和吗?

Is it correct to calculate the numbers as follows? Ask sizeof on every item and round up the result to multiple of 4? And the first number becomes the sum of all the other ones?

推荐答案

在m68K天中,数字用于表示堆栈布局.也就是说,您可以从字面上解码方法签名,并且对于几乎所有类型,您都可以准确地知道可以在堆栈帧内哪个偏移量的哪个字节,以获取/设置参数.

The numbers were used in the m68K days to denote stack layout. That is, you could literally decode the the method signature and, for just about all types, know exactly which bytes at what offset within the stack frame you could diddle to get/set arguments.

之所以可行,是因为m68K的ABI完全是[IIRC-很长一段时间]基于堆栈的参数/返回传递.跨呼叫边界的寄存器中没有任何东西.

This worked because the m68K's ABI was entirely [IIRC -- been a long long time] stack based argument/return passing. There wasn't anything shoved into registers across call boundaries.

但是,由于将Objective-C移植到其他平台上,因此始终在堆栈上已不再是调用约定.参数和返回值通常在寄存器中传递.

However, as Objective-C was ported to other platforms, always-on-the-stack was no longer the calling convention. Arguments and return values are often passed in registers.

因此,这些偏移量现在无用了.同样,编译器使用的类型编码不再完整(因为它从来没有非常有用),并且会有一些类型不会被编码.不太提及对某些C ++模板化类型进行编码会产生方法类型编码字符串,其大小可能为千字节(我认为我遇到的记录大约是类型信息的30K).

Thus, those offsets are now useless. As well, the type encoding used by the compiler is no longer complete (because it never was terribly useful) and there will be types that won't be encoded. Not too mention that encoding some C++ templatized types yields method type encoding strings that can be many Kilobytes in size (I think the record I ran into was around 30K of type information).

因此,不,使用sizeof()生成数字是不正确的,因为它们实际上对所有内容都毫无意义.它们仍然存在的唯一原因是为了二进制兼容性.到处都有一些深奥的代码,仍在解析类型编码字符串,并期望到处都是随机数.

So, no, it isn't correct to use sizeof() to generate the numbers because they are effectively meaningless to everything. The only reason why they still exist is for binary compatibility; there are bits of esoteric code here and there that still parse the type encoding string with the expectation that there will be random numbers sprinkled here and there.

请注意,ObjC运行时中有一些API的痕迹,仍然使人们相信,可能可以动态地对堆栈帧进行编码/解码.确实不是这样,因为C ABI不能保证面对优化时,参数寄存器将在调用边界之间保留.您必须放弃组装,事情真的真的很快就变得丑陋(> shudder<).

Note that there are vestiges of API in the ObjC runtime that still lead one to believe that it might be possible to encode/decode stack frames on the fly. It really isn't as the C ABI doesn't guarantee that argument registers will be preserved across call boundaries in the face of optimization. You'd have to drop to assembly and things get ugly really really fast (>shudder<).

这篇关于如何计算ObjC方法类型编码中的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆