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