Sign in Join
Wiki copytemporary

ObjC Bool: what changed

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

OldNewLine
11## Objective-C
22
33### Source code
44
55main.m
66
77```objc
88#import <Foundation/Foundation.h>
99
1010@interface BoolTest: NSObject
1111 -(void) testcbool: (_Bool) avalue;
1212 -(void) testobjcbool: (BOOL) avalue;
1313@end
1414
1515@implementation BoolTest
1616-(void) testcbool: (_Bool) avalue
1717{
1818 NSLog(@" cbool %s", avalue ? "true" : "false");
1919}
2020
2121-(void) testobjcbool: (BOOL) avalue
2222{
2323 NSLog(@" objcbool %s", avalue ? "true" : "false");
2424}
2525@end
2626
2727void simpleC(_Bool avalue)
2828{
2929 NSLog(@" plainc %s", avalue ? "true" : "false");
3030 return;
3131}
3232
3333int main(int argc, const char * argv[]) {
3434 @autoreleasepool {
3535 // insert code here...
3636 int a = 10;
3737 int b = 10;
3838 BoolTest* tt = [[BoolTest alloc] init];
3939 [tt testcbool:(a==b)];
4040 [tt testobjcbool:(a==b)];
4141 simpleC(a==b);
4242 }
4343 return 0;
4444}
4545```
4646
4747### Compile
4848
4949You need to have Xcode command-line tools installed
5050
51-`clang  -framework Foundation -arch x86_64 main.m -o main`
51+```text
52+clang -framework Foundation -arch x86_64 main.m -o main
53+```
5254
5355produces the executable. (The executable can be debugged and lldb can show the disassebly)
5456
5557In order to get assembly file from the compiler the following command-line could be used
5658
57-`clang  -framework Foundation -arch x86_64 main.m --assemble -o main.as`
59+```text
60+clang -framework Foundation -arch x86_64 main.m --assemble -o main.as
61+```
5862
5963## Disassembly x86_64
6064
6165Boolean parameters, no matter BOOL (signed char) or _Bool are always passed in the entire register
6266
6367### No optimization
6468
6569``` asm
6670 0x100000e5d <+93>: movl -0x14(%rbp), %edx
6771 0x100000e60 <+96>: cmpl -0x18(%rbp), %edx
6872 0x100000e63 <+99>: sete %r8b
6973 0x100000e67 <+103>: movq 0x31a(%rip), %rsi ; "testcbool:"
7074 0x100000e6e <+110>: andb $0x1, %r8b
7175
7276 0x100000e72 <+114>: movzbl %r8b, %edx
7377
7478 0x100000e76 <+118>: movq 0x183(%rip), %rcx ; (void *)0x00007fff7e936e80: objc_msgSend
7579 0x100000e7d <+125>: movq %rax, %rdi
7680 0x100000e80 <+128>: callq *%rcx
7781 0x100000e82 <+130>: movq -0x20(%rbp), %rax
7882
7983 0x100000e86 <+134>: movl -0x14(%rbp), %edx
8084 0x100000e89 <+137>: cmpl -0x18(%rbp), %edx
8185 0x100000e8c <+140>: sete %r8b
8286 0x100000e90 <+144>: andb $0x1, %r8b
8387
8488 0x100000e94 <+148>: movzbl %r8b, %edx
8589
8690 0x100000e98 <+152>: movb %dl, %r8b
8791 0x100000e9b <+155>: movq 0x2ee(%rip), %rsi ; "testobjcbool:"
8892 0x100000ea2 <+162>: movq %rax, %rdi
8993 0x100000ea5 <+165>: movsbl %r8b, %edx
9094 0x100000ea9 <+169>: callq 0x100000ef8 ; symbol stub for: objc_msgSend
9195
9296 0x100000eae <+174>: movl -0x14(%rbp), %edx
9397 0x100000eb1 <+177>: cmpl -0x18(%rbp), %edx
9498 0x100000eb4 <+180>: sete %r8b
9599 0x100000eb8 <+184>: andb $0x1, %r8b
96100
97101 0x100000ebc <+188>: movzbl %r8b, %edi
98102
99103 0x100000ec0 <+192>: callq 0x100000db0 ; simpleC at main.m:29
100104```
101105
102106### O3
103107
104108As expected, O3 evaluates constant expressions and puts the end result into the code. Still boolean parameters are being written as long values into registers.
105109
106110``` asm
107111 0x100000e97 <+55>: movq %rax, %rbx
108112 0x100000e9a <+58>: movq 0x2df(%rip), %rsi ; "testcbool:"
109113 0x100000ea1 <+65>: movl $0x1, %edx
110114 0x100000ea6 <+70>: movq %rbx, %rdi
111115 0x100000ea9 <+73>: callq *%r15
112116 0x100000eac <+76>: movq 0x2d5(%rip), %rsi ; "testobjcbool:"
113117 0x100000eb3 <+83>: movl $0x1, %edx
114118 0x100000eb8 <+88>: movq %rbx, %rdi
115119 0x100000ebb <+91>: callq *%r15
116120 0x100000ebe <+94>: leaq 0x1b3(%rip), %rdi ; @" plainc %s"
117121 0x100000ec5 <+101>: leaq 0x70(%rip), %rsi ; "true"
118122 0x100000ecc <+108>: xorl %eax, %eax
119123 0x100000ece <+110>: callq 0x100000ef2 ; symbol stub for: NSLog
120124 0x100000ed3 <+115>: movq %rbx, %rdi
121125```
122126
123127---
124128*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).*
125129Category: Objective-C
126130Category: macOS

Back to history