Skip to content

Commit

Permalink
Add minimal load-time DLL support on Windows, support dllimport sto…
Browse files Browse the repository at this point in the history
…rage class (#11573)
  • Loading branch information
HertzDevil authored Dec 13, 2021
1 parent 4aabf4e commit eee1a02
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/compiler/crystal/codegen/codegen.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1450,6 +1450,9 @@ module Crystal
unless var
var = llvm_mod.globals.add(llvm_c_return_type(type), name)
var.linkage = LLVM::Linkage::External
if @program.has_flag?("win32") && @program.has_flag?("preview_dll")
var.dll_storage_class = LLVM::DLLStorageClass::DLLImport
end
var.thread_local = thread_local
end
var
Expand Down
2 changes: 1 addition & 1 deletion src/empty.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "primitives"

{% if flag?(:win32) %}
@[Link("libcmt")] # For `mainCRTStartup`
@[Link({{ flag?(:preview_dll) ? "msvcrt" : "libcmt" }})] # For `mainCRTStartup`
{% end %}
lib LibCrystalMain
@[Raises]
Expand Down
2 changes: 1 addition & 1 deletion src/lib_c.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% if flag?(:win32) %}
@[Link("libcmt")]
@[Link({{ flag?(:preview_dll) ? "msvcrt" : "libcmt" }})]
{% end %}
lib LibC
alias Char = UInt8
Expand Down
14 changes: 12 additions & 2 deletions src/llvm/enums.cr
Original file line number Diff line number Diff line change
Expand Up @@ -220,15 +220,25 @@ module LLVM
Appending
Internal
Private
DLLImport
DLLExport
DLLImport # obsolete
DLLExport # obsolete
ExternalWeak
Ghost
Common
LinkerPrivate
LinkerPrivateWeak
end

enum DLLStorageClass
Default

# Function to be imported from DLL.
DLLImport

# Function to be accessible from DLL.
DLLExport
end

enum IntPredicate
EQ = 32
NE
Expand Down
1 change: 1 addition & 0 deletions src/llvm/lib_llvm.cr
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ lib LibLLVM
fun get_initializer = LLVMGetInitializer(global_var : ValueRef) : ValueRef
fun set_linkage = LLVMSetLinkage(global : ValueRef, linkage : LLVM::Linkage)
fun get_linkage = LLVMGetLinkage(global : ValueRef) : LLVM::Linkage
fun set_dll_storage_class = LLVMSetDLLStorageClass(global : ValueRef, storage_class : LLVM::DLLStorageClass)
fun set_metadata = LLVMSetMetadata(value : ValueRef, kind_id : UInt32, node : ValueRef)
fun set_target = LLVMSetTarget(mod : ModuleRef, triple : UInt8*)
fun set_thread_local = LLVMSetThreadLocal(global_var : ValueRef, is_thread_local : Int32)
Expand Down
4 changes: 4 additions & 0 deletions src/llvm/value_methods.cr
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ module LLVM::ValueMethods
LibLLVM.get_linkage(self)
end

def dll_storage_class=(storage_class)
LibLLVM.set_dll_storage_class(self, storage_class)
end

def call_convention=(call_convention)
LibLLVM.set_instruction_call_convention(self, call_convention)
end
Expand Down

0 comments on commit eee1a02

Please sign in to comment.