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

DRAFT: Fix issue #2670, support for extern(Objective-C) #4361

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

MrcSnm
Copy link

@MrcSnm MrcSnm commented Mar 26, 2023

Most here are obviously WIP, there is a bunch of forwarded things and as I'm only getting started with LLVM I would like you guys to review what I'm doing right now.

@kinke @thewilsonator @jacob-carlborg
#2670

I'm trying to replicate those structs defined by the following objc code:

#import <objc/runtime.h>
#import <Foundation/Foundation.h>

int main()
{
  [NSObject alloc];
  return 0;
}
; ModuleID = 'test.m'
source_filename = "test.m"
target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-apple-macosx12.0.0"

; Trying to create those strructs
%0 = type opaque
%struct._class_t = type { %struct._class_t*, %struct._class_t*, %struct._objc_cache*, i8* (i8*, i8*)**, %struct._class_ro_t* }
%struct._objc_cache = type opaque
%struct._class_ro_t = type { i32, i32, i32, i8*, i8*, %struct.__method_list_t*, %struct._objc_protocol_list*, %struct._ivar_list_t*, i8*, %struct._prop_list_t* }
%struct.__method_list_t = type { i32, i32, [0 x %struct._objc_method] }
%struct._objc_method = type { i8*, i8*, i8* }
%struct._objc_protocol_list = type { i64, [0 x %struct._protocol_t*] }
%struct._protocol_t = type { i8*, i8*, %struct._objc_protocol_list*, %struct.__method_list_t*, %struct.__method_list_t*, %struct.__method_list_t*, %struct.__method_list_t*, %struct._prop_list_t*, i32, i32, i8**, i8*, %struct._prop_list_t* }
%struct._ivar_list_t = type { i32, i32, [0 x %struct._ivar_t] }
%struct._ivar_t = type { i64*, i8*, i8*, i32, i32 }
%struct._prop_list_t = type { i32, i32, [0 x %struct._prop_t] }
%struct._prop_t = type { i8*, i8* }

@"OBJC_CLASS_$_NSObject" = external global %struct._class_t
@"OBJC_CLASSLIST_REFERENCES_$_" = internal global %struct._class_t* @"OBJC_CLASS_$_NSObject", section "__DATA,__objc_classrefs,regular,no_dead_strip", align 8
@llvm.compiler.used = appending global [1 x i8*] [i8* bitcast (%struct._class_t** @"OBJC_CLASSLIST_REFERENCES_$_" to i8*)], section "llvm.metadata"

; Function Attrs: noinline optnone ssp uwtable
define i32 @main() #0 {
  %1 = alloca i32, align 4
  store i32 0, i32* %1, align 4
  %2 = load %struct._class_t*, %struct._class_t** @"OBJC_CLASSLIST_REFERENCES_$_", align 8
  %3 = bitcast %struct._class_t* %2 to i8*
  %4 = call i8* @objc_alloc(i8* %3)
  %5 = bitcast i8* %4 to %0*
  ret i32 0
}

declare i8* @objc_alloc(i8*)

attributes #0 = { noinline optnone ssp uwtable "darwin-stkchk-strong-link" "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "probe-stack"="___chkstk_darwin" "stack-protector-buffer-size"="8" "target-cpu"="penryn" "target-features"="+cx16,+cx8,+fxsr,+mmx,+sahf,+sse,+sse2,+sse3,+sse4.1,+ssse3,+x87" "tune-cpu"="generic" }

!llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7, !8, !9, !10}
!llvm.ident = !{!11}

!0 = !{i32 2, !"SDK Version", [2 x i32] [i32 13, i32 1]}
!1 = !{i32 1, !"Objective-C Version", i32 2}
!2 = !{i32 1, !"Objective-C Image Info Version", i32 0}
!3 = !{i32 1, !"Objective-C Image Info Section", !"__DATA,__objc_imageinfo,regular,no_dead_strip"}
!4 = !{i32 1, !"Objective-C Garbage Collection", i8 0}
!5 = !{i32 1, !"Objective-C Class Properties", i32 64}
!6 = !{i32 1, !"Objective-C Enforce ClassRO Pointer Signing", i8 0}
!7 = !{i32 1, !"wchar_size", i32 4}
!8 = !{i32 7, !"PIC Level", i32 2}
!9 = !{i32 7, !"uwtable", i32 1}
!10 = !{i32 7, !"frame-pointer", i32 2}
!11 = !{!"Apple clang version 14.0.0 (clang-1400.0.29.202)"}

@MrcSnm MrcSnm changed the title WIP: Adding base structs WIP: Fix issue #2670, support for extern(Objective-C) Mar 26, 2023
@MrcSnm
Copy link
Author

MrcSnm commented Mar 26, 2023

Right now I would like to know what is the correct way to self reference types, function pointers, and create structs. Most of the handiwork is there but I'm still quite lost in the details

@jacob-carlborg
Copy link
Contributor

jacob-carlborg commented Mar 26, 2023

Unfortunately I'm not familiar with the LLVM or the LDC internals, but I can help you with the Objective-C specific parts.

As a suggestion, I would start with the disabled Objective-C tests and implement what's necessary to get them to pass. The most basic ones are the ones related to calling instance methods, which are https://github.com/ldc-developers/ldc/blob/master/tests/dmd/runnable/objc_call.d and https://github.com/ldc-developers/ldc/blob/master/tests/dmd/runnable/objc_objc_msgSend.d. As you can see, you can use the Objective-C runtime (objc_lookUpClass in this case) to help bootstrapping.

BTW, GitHub has support for draft pull requests.

@MrcSnm MrcSnm changed the title WIP: Fix issue #2670, support for extern(Objective-C) DRAFT: Fix issue #2670, support for extern(Objective-C) Mar 27, 2023
@MrcSnm MrcSnm marked this pull request as draft April 1, 2023 21:27
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

Successfully merging this pull request may close these issues.

None yet

2 participants