Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pcwalton/435124 to your computer and use it in GitHub Desktop.
Save pcwalton/435124 to your computer and use it in GitHub Desktop.
Index: local/bindings/ocaml/llvm/llvm.ml
===================================================================
--- local.orig/bindings/ocaml/llvm/llvm.ml 2010-06-11 12:26:55.000000000 -0700
+++ local/bindings/ocaml/llvm/llvm.ml 2010-06-11 12:31:32.000000000 -0700
@@ -134,9 +134,10 @@
end
exception IoError of string
+exception Assertion of string
-external register_exns : exn -> unit = "llvm_register_core_exns"
-let _ = register_exns (IoError "")
+external register_exns : exn -> exn -> unit = "llvm_register_core_exns"
+let _ = register_exns (IoError "") (Assertion "")
type ('a, 'b) llpos =
| At_end of 'a
Index: local/bindings/ocaml/llvm/llvm.mli
===================================================================
--- local.orig/bindings/ocaml/llvm/llvm.mli 2010-06-11 12:26:55.000000000 -0700
+++ local/bindings/ocaml/llvm/llvm.mli 2010-06-11 12:31:32.000000000 -0700
@@ -207,6 +207,7 @@
(** {6 Exceptions} *)
exception IoError of string
+exception Assertion of string
(** {6 Contexts} *)
Index: local/bindings/ocaml/llvm/llvm_ocaml.c
===================================================================
--- local.orig/bindings/ocaml/llvm/llvm_ocaml.c 2010-06-11 12:26:55.000000000 -0700
+++ local/bindings/ocaml/llvm/llvm_ocaml.c 2010-06-11 14:47:11.000000000 -0700
@@ -22,19 +22,15 @@
#include "caml/fail.h"
#include "caml/callback.h"
#include "llvm/Config/config.h"
-#include <assert.h>
+#include <stdio.h>
#include <stdlib.h>
+#define assert(e) ((void)((e) ? 0 : LLVMDie(#e, __FILE__, __LINE__)))
/* Can't use the recommended caml_named_value mechanism for backwards
compatibility reasons. This is largely equivalent. */
static value llvm_ioerror_exn;
-
-CAMLprim value llvm_register_core_exns(value IoError) {
- llvm_ioerror_exn = Field(IoError, 0);
- register_global_root(&llvm_ioerror_exn);
- return Val_unit;
-}
+static value llvm_assertion_exn;
static void llvm_raise(value Prototype, char *Message) {
CAMLparam1(Prototype);
@@ -50,6 +46,27 @@
#endif
}
+static int llvm_assertion_failed(const char *Message, const char *File,
+ int Line)
+{
+ char *Msg = malloc(1024);
+ snprintf(Msg, 1024, "Assertion failed: %s, file %s, line %d.", Message, File,
+ Line);
+ llvm_raise(llvm_assertion_exn, Msg);
+ return 1;
+}
+
+CAMLprim value llvm_register_core_exns(value IoError, value Assertion) {
+ llvm_ioerror_exn = Field(IoError, 0);
+ register_global_root(&llvm_ioerror_exn);
+ llvm_assertion_exn = Field(Assertion, 0);
+ register_global_root(&llvm_assertion_exn);
+
+ LLVMDie = llvm_assertion_failed;
+
+ return Val_unit;
+}
+
static value alloc_variant(int tag, void *Value) {
value Iter = alloc_small(1, tag);
Field(Iter, 0) = Val_op(Value);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment