Sign in Join
Wiki copytemporary

ObjC Bool: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: ObjC Bool

OldNewLine
1+## Objective-C
2+
3+### Source code
4+
5+main.m
6+
7+```objc
8+#import <Foundation/Foundation.h>
9+
10+@interface BoolTest: NSObject
11+ -(void) testcbool: (_Bool) avalue;
12+ -(void) testobjcbool: (BOOL) avalue;
13+@end
14+
15+@implementation BoolTest
16+-(void) testcbool: (_Bool) avalue
17+{
18+ NSLog(@" cbool %s", avalue ? "true" : "false");
19+}
20+
21+-(void) testobjcbool: (BOOL) avalue
22+{
23+ NSLog(@" objcbool %s", avalue ? "true" : "false");
24+}
25+@end
26+
27+void simpleC(_Bool avalue)
28+{
29+ NSLog(@" plainc %s", avalue ? "true" : "false");
30+ return;
31+}
32+
33+int main(int argc, const char * argv[]) {
34+ @autoreleasepool {
35+ // insert code here...
36+ int a = 10;
37+ int b = 10;
38+ BoolTest* tt = [[BoolTest alloc] init];
39+ [tt testcbool:(a==b)];
40+ [tt testobjcbool:(a==b)];
41+ simpleC(a==b);
42+ }
43+ return 0;
44+}
45+```
46+
47+### Compile
48+
49+You need to have Xcode command-line tools installed
50+
51+`clang  -framework Foundation -arch x86_64 main.m -o main`
52+
53+produces the executable. (The executable can be debugged and lldb can show the disassebly)
54+
55+In order to get assembly file from the compiler the following command-line could be used
56+
57+`clang  -framework Foundation -arch x86_64 main.m --assemble -o main.as`
58+
59+## Disassembly x86_64
60+
61+Boolean parameters, no matter BOOL (signed char) or _Bool are always passed in the entire register
62+
63+### No optimization
64+
65+``` asm
66+ 0x100000e5d <+93>: movl -0x14(%rbp), %edx
67+ 0x100000e60 <+96>: cmpl -0x18(%rbp), %edx
68+ 0x100000e63 <+99>: sete %r8b
69+ 0x100000e67 <+103>: movq 0x31a(%rip), %rsi ; "testcbool:"
70+ 0x100000e6e <+110>: andb $0x1, %r8b
71+
72+ 0x100000e72 <+114>: movzbl %r8b, %edx
73+
74+ 0x100000e76 <+118>: movq 0x183(%rip), %rcx ; (void *)0x00007fff7e936e80: objc_msgSend
75+ 0x100000e7d <+125>: movq %rax, %rdi
76+ 0x100000e80 <+128>: callq *%rcx
77+ 0x100000e82 <+130>: movq -0x20(%rbp), %rax
78+
79+ 0x100000e86 <+134>: movl -0x14(%rbp), %edx
80+ 0x100000e89 <+137>: cmpl -0x18(%rbp), %edx
81+ 0x100000e8c <+140>: sete %r8b
82+ 0x100000e90 <+144>: andb $0x1, %r8b
83+
84+ 0x100000e94 <+148>: movzbl %r8b, %edx
85+
86+ 0x100000e98 <+152>: movb %dl, %r8b
87+ 0x100000e9b <+155>: movq 0x2ee(%rip), %rsi ; "testobjcbool:"
88+ 0x100000ea2 <+162>: movq %rax, %rdi
89+ 0x100000ea5 <+165>: movsbl %r8b, %edx
90+ 0x100000ea9 <+169>: callq 0x100000ef8 ; symbol stub for: objc_msgSend
91+
92+ 0x100000eae <+174>: movl -0x14(%rbp), %edx
93+ 0x100000eb1 <+177>: cmpl -0x18(%rbp), %edx
94+ 0x100000eb4 <+180>: sete %r8b
95+ 0x100000eb8 <+184>: andb $0x1, %r8b
96+
97+ 0x100000ebc <+188>: movzbl %r8b, %edi
98+
99+ 0x100000ec0 <+192>: callq 0x100000db0 ; simpleC at main.m:29
100+```
101+
102+### O3
103+
104+As expected, O3 evaluates constant expressions and puts the end result into the code. Still boolean parameters are being written as long values into registers.
105+
106+``` asm
107+ 0x100000e97 <+55>: movq %rax, %rbx
108+ 0x100000e9a <+58>: movq 0x2df(%rip), %rsi ; "testcbool:"
109+ 0x100000ea1 <+65>: movl $0x1, %edx
110+ 0x100000ea6 <+70>: movq %rbx, %rdi
111+ 0x100000ea9 <+73>: callq *%r15
112+ 0x100000eac <+76>: movq 0x2d5(%rip), %rsi ; "testobjcbool:"
113+ 0x100000eb3 <+83>: movl $0x1, %edx
114+ 0x100000eb8 <+88>: movq %rbx, %rdi
115+ 0x100000ebb <+91>: callq *%r15
116+ 0x100000ebe <+94>: leaq 0x1b3(%rip), %rdi ; @" plainc %s"
117+ 0x100000ec5 <+101>: leaq 0x70(%rip), %rsi ; "true"
118+ 0x100000ecc <+108>: xorl %eax, %eax
119+ 0x100000ece <+110>: callq 0x100000ef2 ; symbol stub for: NSLog
120+ 0x100000ed3 <+115>: movq %rbx, %rdi
121+```
122+
123+---
124+*From the Free Pascal wiki, page "ObjC Bool", as it stood on 2022-05-04 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
125+Category: Objective-C
126+Category: macOS

Back to history