Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is the underlying BOOL type a bool on Apple silicon #110

Closed
schell opened this issue Jan 3, 2022 · 6 comments
Closed

Is the underlying BOOL type a bool on Apple silicon #110

schell opened this issue Jan 3, 2022 · 6 comments

Comments

@schell
Copy link

schell commented Jan 3, 2022

I was confused about this library's definition of BOOL until finding this comment on SO, which explains that BOOL is a C signed char on Darwin and a C bool on iOS. I think we need to also check the target_os to distinguish between aarch64-apple-darwin and aarch64-apple-ios, because I'm seeing odd errors when compiling bluster dfrankland/bluster#44 on my M1 laptop.

I will submit a patch if it ends up working.

@madsmtm
Copy link

madsmtm commented Jan 3, 2022

I don't have an M1 myself, but cross-compiling the following code:

// test_bool.m
#import <objc/objc.h>

BOOL BOOL_VALUE = 256;
_Bool C99_BOOL_VALUE = 256;
signed char SIGNED_CHAR_VALUE = 256;

char* BOOL_ = @encode(BOOL);
char* C99_BOOL = @encode(_Bool);
char* SIGNED_CHAR = @encode(signed char);

With:

$ clang -emit-llvm -S -target arm64-apple-darwin test_bool.m

Yields:

// Snippet
@BOOL_VALUE = global i8 1, align 1
@C99_BOOL_VALUE = global i8 1, align 1
@SIGNED_CHAR_VALUE = global i8 0, align 1
@.str = private unnamed_addr constant [2 x i8] c"B\00", align 1
@BOOL_ = global i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i32 0, i32 0), align 8
@C99_BOOL = global i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i32 0, i32 0), align 8
@.str.1 = private unnamed_addr constant [2 x i8] c"c\00", align 1
@SIGNED_CHAR = global i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i32 0, i32 0), align 8

Which strongly suggests that BOOL acts the same way as C99 _Bool/bool.

@madsmtm
Copy link

madsmtm commented Jan 3, 2022

I think the problem is in your usage of get_ivar and set_ivar; you're trying to cast a boolean as a pointer type, while what you should be doing is just to use the correct type in your instance variable to begin with:

delegate.add_ivar::<BOOL>(POWERED_ON_IVAR);
let powered_on = delegate.get_ivar::<BOOL>(POWERED_ON_IVAR);
delegate.set_ivar::<BOOL>(POWERED_ON_IVAR, YES);

@schell
Copy link
Author

schell commented Jan 3, 2022

Thank you for your quick reply.

Is there another step you're taking to compile the above test? Maybe including some headers or something? When I try I get this error:

❯ clang -emit-llvm -S -target arm64-apple-darwin test_bool.m                                                                                                             (base) 
test_bool.m:2:1: error: unknown type name 'BOOL'
BOOL BOOL_VALUE = 256;
^
test_bool.m:4:33: warning: implicit conversion from 'int' to 'signed char' changes value from 256 to 0 [-Wconstant-conversion]
signed char SIGNED_CHAR_VALUE = 256;
            ~~~~~~~~~~~~~~~~~   ^~~
test_bool.m:6:23: error: unknown type name 'BOOL'
char* BOOL_ = @encode(BOOL);
                      ^
1 warning and 2 errors generated.

@madsmtm
Copy link

madsmtm commented Jan 3, 2022

Ah yes, sorry, I forgot to add an #import <objc/objc.h>; I've updated my comment to reflect this

@schell
Copy link
Author

schell commented Jan 3, 2022

I made your suggested changes to bluster and it seems to pass tests, so maybe that's all it was!

@schell
Copy link
Author

schell commented Jan 3, 2022

Also it looks like the llvm output is in fact the same on my machine as what you posted. Thank you for the investigation!

@schell schell closed this as completed Jan 3, 2022
@schell schell changed the title Underlying BOOL type is bool on Apple silicon Is the underlying BOOL type a bool on Apple silicon Jan 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants