Skip to content

Commit

Permalink
[OCaml] Drop support for 3.12.1 and earlier.
Browse files Browse the repository at this point in the history
In practice this means:
  * Always using -g flag.
  * Embedding -cclib -lstdc++ into the corresponding cma/cmxa file.
    This also moves -lstdc++ in a single place.
  * Using caml_named_value instead of a homegrown mechanism.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@220843 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
whitequark committed Oct 29, 2014
1 parent 360c7ed commit e3227ca
Show file tree
Hide file tree
Showing 29 changed files with 167 additions and 263 deletions.
5 changes: 3 additions & 2 deletions bindings/ocaml/Makefile.ocaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ ifneq ($(ObjectsO),)
OCAMLAFLAGS += $(patsubst %,-cclib %, \
$(filter-out -L$(LibDir),-l$(LIBRARYNAME) \
$(shell $(LLVM_CONFIG) --ldflags)) \
$(UsedLibs))
$(UsedLibs) $(ExtraLibs))
else
OCAMLAFLAGS += $(patsubst %,-cclib %, \
$(filter-out -L$(LibDir),$(shell $(LLVM_CONFIG) --ldflags)) \
$(UsedLibs))
$(UsedLibs) $(ExtraLibs))
endif
endif

Expand Down Expand Up @@ -481,6 +481,7 @@ printcamlvars::
$(Echo) "DestSharedLib: " '$(DestSharedLib)'
$(Echo) "UsedLibs : " '$(UsedLibs)'
$(Echo) "UsedLibNames : " '$(UsedLibNames)'
$(Echo) "ExtraLibs : " '$(ExtraLibs)'

.PHONY: printcamlvars build-cmis \
clean-a clean-cmis clean-cma clean-cmxa \
Expand Down
4 changes: 2 additions & 2 deletions bindings/ocaml/all_backends/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
##===- bindings/ocaml/all_backends/Makefile ----------------------*- Makefile -*-===##
##===- bindings/ocaml/all_backends/Makefile ----------------*- Makefile -*-===##
#
# The LLVM Compiler Infrastructure
#
Expand All @@ -7,7 +7,7 @@
#
##===----------------------------------------------------------------------===##
#
# This is the makefile for the Objective Caml Llvm_backends interface.
# This is the makefile for the Objective Caml Llvm_all_backends interface.
#
##===----------------------------------------------------------------------===##

Expand Down
1 change: 0 additions & 1 deletion bindings/ocaml/analysis/analysis_ocaml.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include "caml/mlvalues.h"
#include "caml/memory.h"


/* Llvm.llmodule -> string option */
CAMLprim value llvm_verify_module(LLVMModuleRef M) {
CAMLparam0();
Expand Down
2 changes: 1 addition & 1 deletion bindings/ocaml/analysis/llvm_analysis.ml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(*===-- llvm_analysis.ml - LLVM OCaml Interface -----------------*- C++ -*-===*
(*===-- llvm_analysis.ml - LLVM OCaml Interface ---------------*- OCaml -*-===*
*
* The LLVM Compiler Infrastructure
*
Expand Down
2 changes: 1 addition & 1 deletion bindings/ocaml/analysis/llvm_analysis.mli
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(*===-- llvm_analysis.mli - LLVM OCaml Interface ----------------*- C++ -*-===*
(*===-- llvm_analysis.mli - LLVM OCaml Interface --------------*- OCaml -*-===*
*
* The LLVM Compiler Infrastructure
*
Expand Down
1 change: 0 additions & 1 deletion bindings/ocaml/backends/META.llvm_backend.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ requires = "llvm"
archive(byte) = "llvm_@[email protected]"
archive(native) = "llvm_@[email protected]"
directory = "."
linkopts = "-ccopt -lstdc++"
9 changes: 5 additions & 4 deletions bindings/ocaml/backends/backend_ocaml.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
#include "caml/alloc.h"
#include "caml/memory.h"

// TODO: Figure out how to call these only for targets which support them.
// LLVMInitialize ## target ## AsmPrinter();
// LLVMInitialize ## target ## AsmParser();
// LLVMInitialize ## target ## Disassembler();
/* TODO: Figure out how to call these only for targets which support them.
* LLVMInitialize ## target ## AsmPrinter();
* LLVMInitialize ## target ## AsmParser();
* LLVMInitialize ## target ## Disassembler();
*/

#define INITIALIZER1(target) \
CAMLprim value llvm_initialize_ ## target(value Unit) { \
Expand Down
39 changes: 10 additions & 29 deletions bindings/ocaml/bitreader/bitreader_ocaml.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,7 @@
#include "caml/alloc.h"
#include "caml/fail.h"
#include "caml/memory.h"


/* Can't use the recommended caml_named_value mechanism for backwards
compatibility reasons. This is largely equivalent. */
static value llvm_bitreader_error_exn;

CAMLprim value llvm_register_bitreader_exns(value Error) {
llvm_bitreader_error_exn = Field(Error, 0);
register_global_root(&llvm_bitreader_error_exn);
return Val_unit;
}
#include "caml/callback.h"

static void llvm_raise(value Prototype, char *Message) {
CAMLparam1(Prototype);
Expand All @@ -36,38 +26,29 @@ static void llvm_raise(value Prototype, char *Message) {
LLVMDisposeMessage(Message);

raise_with_arg(Prototype, CamlMessage);
abort(); /* NOTREACHED */
#ifdef CAMLnoreturn
CAMLnoreturn; /* Silences warnings, but is missing in some versions. */
#endif
CAMLnoreturn;
}


/*===-- Modules -----------------------------------------------------------===*/
/*===-- BitReader --------------------------------------------------------===*/

/* Llvm.llcontext -> Llvm.llmemorybuffer -> Llvm.llmodule */
CAMLprim value llvm_get_module(LLVMContextRef C, LLVMMemoryBufferRef MemBuf) {
CAMLparam0();
CAMLlocal2(Variant, MessageVal);
CAMLprim LLVMModuleRef llvm_get_module(LLVMContextRef C, LLVMMemoryBufferRef MemBuf) {
LLVMModuleRef M;
char *Message;

LLVMModuleRef M;
if (LLVMGetBitcodeModuleInContext(C, MemBuf, &M, &Message))
llvm_raise(llvm_bitreader_error_exn, Message);
llvm_raise(*caml_named_value("Llvm_bitreader.Error"), Message);

CAMLreturn((value) M);
return M;
}

/* Llvm.llcontext -> Llvm.llmemorybuffer -> Llvm.llmodule */
CAMLprim value llvm_parse_bitcode(LLVMContextRef C,
LLVMMemoryBufferRef MemBuf) {
CAMLparam0();
CAMLlocal2(Variant, MessageVal);
CAMLprim LLVMModuleRef llvm_parse_bitcode(LLVMContextRef C, LLVMMemoryBufferRef MemBuf) {
LLVMModuleRef M;
char *Message;

if (LLVMParseBitcodeInContext(C, MemBuf, &M, &Message))
llvm_raise(llvm_bitreader_error_exn, Message);
llvm_raise(*caml_named_value("Llvm_bitreader.Error"), Message);

CAMLreturn((value) M);
return M;
}
17 changes: 8 additions & 9 deletions bindings/ocaml/bitreader/llvm_bitreader.ml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(*===-- llvm_bitreader.ml - LLVM OCaml Interface ----------------*- C++ -*-===*
(*===-- llvm_bitreader.ml - LLVM OCaml Interface --------------*- OCaml -*-===*
*
* The LLVM Compiler Infrastructure
*
Expand All @@ -7,14 +7,13 @@
*
*===----------------------------------------------------------------------===*)


exception Error of string

external register_exns : exn -> unit = "llvm_register_bitreader_exns"
let _ = register_exns (Error "")

external get_module : Llvm.llcontext -> Llvm.llmemorybuffer -> Llvm.llmodule
= "llvm_get_module"
let () = Callback.register_exception "Llvm_bitreader.Error" (Error "")

external parse_bitcode : Llvm.llcontext -> Llvm.llmemorybuffer -> Llvm.llmodule
= "llvm_parse_bitcode"
external get_module
: Llvm.llcontext -> Llvm.llmemorybuffer -> Llvm.llmodule
= "llvm_get_module"
external parse_bitcode
: Llvm.llcontext -> Llvm.llmemorybuffer -> Llvm.llmodule
= "llvm_parse_bitcode"
3 changes: 1 addition & 2 deletions bindings/ocaml/bitreader/llvm_bitreader.mli
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(*===-- llvm_bitreader.mli - LLVM OCaml Interface ---------------*- C++ -*-===*
(*===-- llvm_bitreader.mli - LLVM OCaml Interface -------------*- OCaml -*-===*
*
* The LLVM Compiler Infrastructure
*
Expand All @@ -20,7 +20,6 @@ exception Error of string
encountered. See the function [llvm::getBitcodeModule]. *)
val get_module : Llvm.llcontext -> Llvm.llmemorybuffer -> Llvm.llmodule


(** [parse_bitcode context mb] parses the bitcode for a new module [m] from the
memory buffer [mb] in the context [context]. Returns [m] if successful, or
raises [Error msg] otherwise, where [msg] is a description of the error
Expand Down
18 changes: 9 additions & 9 deletions bindings/ocaml/bitwriter/bitwriter_ocaml.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,25 @@
#include "caml/mlvalues.h"
#include "caml/memory.h"

/*===-- Modules -----------------------------------------------------------===*/
/*===-- BitWriter ---------------------------------------------------------===*/

/* Llvm.llmodule -> string -> bool */
CAMLprim value llvm_write_bitcode_file(value M, value Path) {
int res = LLVMWriteBitcodeToFile((LLVMModuleRef) M, String_val(Path));
return Val_bool(res == 0);
CAMLprim value llvm_write_bitcode_file(LLVMModuleRef M, value Path) {
int Result = LLVMWriteBitcodeToFile(M, String_val(Path));
return Val_bool(Result == 0);
}

/* ?unbuffered:bool -> Llvm.llmodule -> Unix.file_descr -> bool */
CAMLprim value llvm_write_bitcode_to_fd(value U, value M, value FD) {
CAMLprim value llvm_write_bitcode_to_fd(value U, LLVMModuleRef M, value FD) {
int Unbuffered;
int res;
int Result;

if (U == Val_int(0)) {
Unbuffered = 0;
} else {
Unbuffered = Bool_val(Field(U,0));
Unbuffered = Bool_val(Field(U, 0));
}

res = LLVMWriteBitcodeToFD((LLVMModuleRef) M, Int_val(FD), 0, Unbuffered);
return Val_bool(res == 0);
Result = LLVMWriteBitcodeToFD(M, Int_val(FD), 0, Unbuffered);
return Val_bool(Result == 0);
}
2 changes: 1 addition & 1 deletion bindings/ocaml/executionengine/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
##===- bindings/ocaml/executionengine/Makefile --------------*- Makefile -*-===##
##===- bindings/ocaml/executionengine/Makefile -------------*- Makefile -*-===##
#
# The LLVM Compiler Infrastructure
#
Expand Down
82 changes: 33 additions & 49 deletions bindings/ocaml/executionengine/executionengine_ocaml.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,15 @@
|* *|
\*===----------------------------------------------------------------------===*/

#include <string.h>
#include <assert.h>
#include "llvm-c/ExecutionEngine.h"
#include "llvm-c/Target.h"
#include "caml/alloc.h"
#include "caml/custom.h"
#include "caml/fail.h"
#include "caml/memory.h"
#include <string.h>
#include <assert.h>

/* Force the LLVM interpreter and JIT to be linked in. */
void llvm_initialize(void) {
LLVMLinkInInterpreter();
LLVMLinkInMCJIT();
}

/* unit -> bool */
CAMLprim value llvm_initialize_native_target(value Unit) {
return Val_bool(!LLVMInitializeNativeTarget() &&
!LLVMInitializeNativeAsmParser() &&
!LLVMInitializeNativeAsmPrinter());
}

/* Can't use the recommended caml_named_value mechanism for backwards
compatibility reasons. This is largely equivalent. */
static value llvm_ee_error_exn;

CAMLprim value llvm_register_ee_exns(value Error) {
llvm_ee_error_exn = Field(Error, 0);
register_global_root(&llvm_ee_error_exn);
return Val_unit;
}
#include "caml/callback.h"

static void llvm_raise(value Prototype, char *Message) {
CAMLparam1(Prototype);
Expand All @@ -55,13 +33,9 @@ static void llvm_raise(value Prototype, char *Message) {
LLVMDisposeMessage(Message);

raise_with_arg(Prototype, CamlMessage);
abort(); /* NOTREACHED */
#ifdef CAMLnoreturn
CAMLnoreturn; /* Silences warnings, but is missing in some versions. */
#endif
CAMLnoreturn;
}


/*--... Operations on generic values .......................................--*/

#define Genericvalue_val(v) (*(LLVMGenericValueRef *)(Data_custom_val(v)))
Expand All @@ -71,15 +45,13 @@ static void llvm_finalize_generic_value(value GenVal) {
}

static struct custom_operations generic_value_ops = {
(char *) "LLVMGenericValue",
(char *) "Llvm_executionengine.GenericValue.t",
llvm_finalize_generic_value,
custom_compare_default,
custom_hash_default,
custom_serialize_default,
custom_deserialize_default
#ifdef custom_compare_ext_default
, custom_compare_ext_default
#endif
custom_deserialize_default,
custom_compare_ext_default
};

static value alloc_generic_value(LLVMGenericValueRef Ref) {
Expand Down Expand Up @@ -173,12 +145,22 @@ CAMLprim value llvm_genericvalue_as_nativeint(value GenVal) {

/*--... Operations on execution engines ....................................--*/

/* unit -> bool */
CAMLprim value llvm_initialize_native_target(value Unit) {
LLVMLinkInInterpreter();
LLVMLinkInMCJIT();

return Val_bool(!LLVMInitializeNativeTarget() &&
!LLVMInitializeNativeAsmParser() &&
!LLVMInitializeNativeAsmPrinter());
}

/* llmodule -> ExecutionEngine.t */
CAMLprim LLVMExecutionEngineRef llvm_ee_create(LLVMModuleRef M) {
LLVMExecutionEngineRef Interp;
char *Error;
if (LLVMCreateExecutionEngineForModule(&Interp, M, &Error))
llvm_raise(llvm_ee_error_exn, Error);
llvm_raise(*caml_named_value("Llvm_executionengine.Error"), Error);
return Interp;
}

Expand All @@ -188,7 +170,7 @@ llvm_ee_create_interpreter(LLVMModuleRef M) {
LLVMExecutionEngineRef Interp;
char *Error;
if (LLVMCreateInterpreterForModule(&Interp, M, &Error))
llvm_raise(llvm_ee_error_exn, Error);
llvm_raise(*caml_named_value("Llvm_executionengine.Error"), Error);
return Interp;
}

Expand All @@ -198,7 +180,7 @@ llvm_ee_create_jit(LLVMModuleRef M, value OptLevel) {
LLVMExecutionEngineRef JIT;
char *Error;
if (LLVMCreateJITCompilerForModule(&JIT, M, Int_val(OptLevel), &Error))
llvm_raise(llvm_ee_error_exn, Error);
llvm_raise(*caml_named_value("Llvm_executionengine.Error"), Error);
return JIT;
}

Expand All @@ -207,16 +189,18 @@ CAMLprim LLVMExecutionEngineRef
llvm_ee_create_mcjit(LLVMModuleRef M, value OptRecord) {
LLVMExecutionEngineRef MCJIT;
char *Error;
struct LLVMMCJITCompilerOptions Options = {
.OptLevel = Int_val(Field(OptRecord, 0)),
.CodeModel = Int_val(Field(OptRecord, 1)),
.NoFramePointerElim = Int_val(Field(OptRecord, 2)),
.EnableFastISel = Int_val(Field(OptRecord, 3)),
.MCJMM = NULL
};
struct LLVMMCJITCompilerOptions Options;

LLVMInitializeMCJITCompilerOptions(&Options, sizeof(Options));
Options.OptLevel = Int_val(Field(OptRecord, 0));
Options.CodeModel = Int_val(Field(OptRecord, 1));
Options.NoFramePointerElim = Int_val(Field(OptRecord, 2));
Options.EnableFastISel = Int_val(Field(OptRecord, 3));
Options.MCJMM = NULL;

if (LLVMCreateMCJITCompilerForModule(&MCJIT, M, &Options,
sizeof(Options), &Error))
llvm_raise(llvm_ee_error_exn, Error);
llvm_raise(*caml_named_value("Llvm_executionengine.Error"), Error);
return MCJIT;
}

Expand All @@ -238,7 +222,7 @@ CAMLprim LLVMModuleRef llvm_ee_remove_module(LLVMModuleRef M,
LLVMModuleRef RemovedModule;
char *Error;
if (LLVMRemoveModule(EE, M, &RemovedModule, &Error))
llvm_raise(llvm_ee_error_exn, Error);
llvm_raise(*caml_named_value("Llvm_executionengine.Error"), Error);
return RemovedModule;
}

Expand Down Expand Up @@ -350,9 +334,9 @@ extern value llvm_alloc_data_layout(LLVMTargetDataRef TargetData);
CAMLprim value llvm_ee_get_data_layout(LLVMExecutionEngineRef EE) {
value DataLayout;
LLVMTargetDataRef OrigDataLayout;
OrigDataLayout = LLVMGetExecutionEngineTargetData(EE);

char* TargetDataCStr;

OrigDataLayout = LLVMGetExecutionEngineTargetData(EE);
TargetDataCStr = LLVMCopyStringRepOfTargetData(OrigDataLayout);
DataLayout = llvm_alloc_data_layout(LLVMCreateTargetData(TargetDataCStr));
LLVMDisposeMessage(TargetDataCStr);
Expand Down
Loading

0 comments on commit e3227ca

Please sign in to comment.