Skip to content

Commit 5c83d14

Browse files
Upgrade to OCaml 5.x and LLVM 19 with opaque pointers (#14)
## Summary - Port codebase from OCaml 4.07 to OCaml 5.x (replace deprecated `Pervasives.compare`, fix warnings) - Adapt to LLVM 19 opaque pointer model (explicit type arguments for `build_load`, `build_gep`, `build_call`, etc.) - Update build configuration for OCaml 5 compatibility - Update README with modern installation instructions ## Changes ### OCaml 5.x Compatibility - Replace `Pervasives.compare` with `compare` in semant.ml, utilities.ml, codegenutils.ml - Fix unreachable code warning in coral.ml (`exit 0; ()` → `exit 0`) - Add `package(unix)` to `_tags` and remove redundant `-lib unix` from Makefile ### LLVM 19 Opaque Pointers - All pointer types now use single opaque `ptr` type via `L.pointer_type context` - Add explicit type arguments to `build_load`, `build_gep`, `build_struct_gep` - Add function type arguments to all `build_call` invocations - Fix optimized function calls to compute `ftype` explicitly instead of using `L.type_of` ### Documentation - Update README installation instructions for OCaml 5.x - Add Apple Silicon LLVM path (`/opt/homebrew/opt/llvm/bin`) - Simplify and modernize installation steps ## Test plan - [x] All 132 regression tests pass - [x] Build completes with no OCaml warnings - [x] Tested on macOS with OCaml 5.4.0 and LLVM 19 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 3a36be7 commit 5c83d14

File tree

10 files changed

+251
-320
lines changed

10 files changed

+251
-320
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.PHONY: all clean byte native fn
22

3-
OCB_FLAGS = -tag bin_annot -I src -use-ocamlfind -lib unix
3+
OCB_FLAGS = -tag bin_annot -I src -use-ocamlfind
44
OCB = ocamlbuild $(OCB_FLAGS)
55

66
all: clean native

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Coral supports Python 3.7 style type annotations, as seen in the add function he
4141

4242
The Coral GitHub page provides installers for MacOS and several Linux distributions. **These installers can be found on the [Releases page](https://github.com/jacobaustin123/Coral/releases)**. These require that clang be installed.
4343

44-
To build the language from the source, **you must have OCaml 4.07.\*, ocaml-llvm, and clang already installed**. To build Coral from the source with OCaml, ocaml-llvm, and gcc/clang already installed, run:
44+
To build the language from the source, **you must have OCaml (5.x recommended), ocaml-llvm, and clang already installed**. To build Coral from the source with OCaml, ocaml-llvm, and gcc/clang already installed, run:
4545

4646
```bash
4747
> git clone https://github.com/jacobaustin123/Coral.git
@@ -54,27 +54,27 @@ This will generate an executable called coral which acts as a compiler and inter
5454
```bash
5555
> brew install opam llvm
5656
> opam init
57-
> opam switch create 4.07.1
58-
> opam install llvm ocamlbuild ocamlfind core
57+
> eval $(opam env)
58+
> opam install llvm ocamlbuild ocamlfind
5959
```
6060

61-
If the above fails, you may need to run `eval $(opam env)` after opam init and opam switch. The following may also be useful instead:
61+
If the above fails, you may need to run `eval $(opam env)` after opam init. You may also need to ensure the LLVM binaries are in your PATH:
6262

6363
```bash
64-
> brew install opam llvm
65-
> opam depext conf-llvm.6.0.0
66-
> opam install llvm
67-
> export PATH=/usr/local/opt/llvm@6/bin:$PATH
64+
> export PATH=/opt/homebrew/opt/llvm/bin:$PATH # Apple Silicon
65+
> export PATH=/usr/local/opt/llvm/bin:$PATH # Intel Mac
6866
```
6967

7068
On Linux, follow the OCaml/Opam installation instructions [here](https://opam.ocaml.org/doc/Install.html) for your distribution, install llvm following instructions [here](https://apt.llvm.org/), and then run
7169

72-
```
73-
> sudo apt-get install cmake llvm-10 opam
70+
```bash
71+
> sudo apt-get install cmake llvm opam
72+
> opam init
73+
> eval $(opam env)
7474
> opam install llvm ocamlbuild ocamlfind
7575
```
7676

77-
You may need to add the llvm path (on Mac usually /usr/local/opt/llvm@6/bin:$PATH) to your PATH variable using bashrc.
77+
You may need to add the llvm bin directory to your PATH variable using bashrc or zshrc.
7878

7979
# Goals
8080

_tags

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# Include the llvm and llvm.analysis packages while compiling
2-
true: package(llvm), package(llvm.analysis)
1+
# Include the llvm, llvm.analysis, and unix packages while compiling
2+
true: package(llvm), package(llvm.analysis), package(unix)
33

44
# Enable almost all compiler warnings
55
true : warn(-8-26-11)

src/codegen.ml

Lines changed: 217 additions & 301 deletions
Large diffs are not rendered by default.

src/codegenutils.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ let pbind bind = tstp (string_of_sbind bind); ()
1111
let _ = L.enable_pretty_stacktrace()
1212
(* Maps *)
1313
module StringMap = Map.Make(String)
14-
module BindMap = Map.Make(struct type t = Ast.bind let compare = Pervasives.compare end)
15-
module SfdeclMap = Map.Make(struct type t = Sast.sfunc_decl let compare = Pervasives.compare end)
14+
module BindMap = Map.Make(struct type t = Ast.bind let compare = compare end)
15+
module SfdeclMap = Map.Make(struct type t = Sast.sfunc_decl let compare = compare end)
1616

1717
let explode s =
1818
let rec aux out n =

src/coral.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ let rec from_file map fname run = (* todo combine with loop *)
389389
| Not_found -> Printf.printf "NotFoundError: unknown error!\n"; flush stdout
390390
| Parsing.Parse_error -> Printf.printf "ParseError: invalid syntax!\n"; flush stdout
391391
| Failure explanation -> Printf.printf "%s\n" explanation; flush stdout
392-
| KeyboardInterrupt -> safe_remove !llvm_name; safe_remove !assembly_name; safe_remove !executable_name; exit 0; ()
392+
| KeyboardInterrupt -> safe_remove !llvm_name; safe_remove !assembly_name; safe_remove !executable_name; exit 0
393393

394394
(* Coral main interpreter loop. Parses command line arguments, including a single
395395
anonymous argument (file path) and runs either the interpreter or the from_file compiler *)

src/semant.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,5 +451,5 @@ and stmt the_state = function (* evaluates statements, can pass it a func *)
451451
statements and returning a list of sstmts, a list of globals, and the updated map *)
452452

453453
and check sast_out globals_out the_state = function
454-
| [] -> ((List.rev sast_out, List.sort_uniq Pervasives.compare (List.rev (globals_out @ !possible_globals))), the_state.locals)
454+
| [] -> ((List.rev sast_out, List.sort_uniq compare (List.rev (globals_out @ !possible_globals))), the_state.locals)
455455
| a :: t -> let (m', statement, data, binds) = stmt the_state a in check (statement :: sast_out) (binds @ globals_out) (change_state the_state (S_setmaps (m', m'))) t

src/utilities.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ let compare_decl a b = if a = b then a else false
208208
let compare_data a b = if a = b then a else None
209209

210210
(* map with SFunc and argument type list used to check recursive calls *)
211-
module TypeMap = Map.Make(struct type t = stmt * typ list let compare = Pervasives.compare end)
211+
module TypeMap = Map.Make(struct type t = stmt * typ list let compare = compare end)
212212

213213
(* map with string keys, used for variable lookup *)
214214
module StringMap = Map.Make(String)

tests/test-recurse-define.cl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
def fib(n):
2+
if n <= 1:
3+
return n
4+
else:
5+
return fib(n-1) + fib(n-2)
6+
7+
def factorial(n):
8+
if n <= 1:
9+
return 1
10+
else:
11+
return n * factorial(n-1)
12+
13+
x = 42
14+
print(x)

tests/test-recurse-define.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
42

0 commit comments

Comments
 (0)