xref: /openbsd-src/gnu/llvm/clang/docs/ClangRepl.rst (revision 12c855180aad702bbcca06e0398d774beeafb155)
1===========
2Clang-Repl
3===========
4
5**Clang-Repl** is an interactive C++ interpreter that allows for incremental
6compilation. It supports interactive programming for C++ in a
7read-evaluate-print-loop (REPL) style. It uses Clang as a library to compile the
8high level programming language into LLVM IR. Then the LLVM IR is executed by
9the LLVM just-in-time (JIT) infrastructure.
10
11Clang-Repl is suitable for exploratory programming and in places where time
12to insight is important. Clang-Repl is a project inspired by the work in
13`Cling <https://github.com/root-project/cling>`_, a LLVM-based C/C++ interpreter
14developed by the field of high energy physics and used by the scientific data
15analysis framework `ROOT <https://root.cern/>`_. Clang-Repl allows to move parts
16of Cling upstream, making them useful and available to a broader audience.
17
18
19
20Clang-Repl Usage
21================
22
23
24.. code-block:: text
25
26  clang-repl> #include <iostream>
27  clang-repl> int f() { std::cout << "Hello Interpreted World!\n"; return 0; }
28  clang-repl> auto r = f();
29  // Prints Hello Interpreted World!
30
31Note that the implementation is not complete and highly experimental. We do
32not yet support statements on the global scope, for example.
33
34
35Clang-Repl Basic Data Flow
36==========================
37
38.. image:: ClangRepl_design.png
39   :align: center
40   :alt: ClangRepl design
41
42Clang-Repl data flow can be divided into roughly 8 phases:
43
441. Clang-Repl controls the input infrastructure by an interactive prompt or by
45   an interface allowing the incremental processing of input.
46
472. Then it sends the input to the underlying incremental facilities in Clang
48   infrastructure.
49
503. Clang compiles the input into an AST representation.
51
524. When required the AST can be further transformed in order to attach specific
53   behavior.
54
555. The AST representation is then lowered to LLVM IR.
56
576. The LLVM IR is the input format for LLVM’s JIT compilation infrastructure.
58   The tool will instruct the JIT to run specified functions, translating them
59   into machine code targeting the underlying device architecture (eg. Intel
60   x86 or NVPTX).
61
627. The LLVM JIT lowers the LLVM IR to machine code.
63
648. The machine code is then executed.
65
66
67Just like Clang, Clang-Repl can be integrated in existing applications as a
68library (via using the clangInterpreter library). This turning your C++ compiler
69into a service which incrementally can consume and execute code. The
70**Compiler as A Service** (**CaaS**) concept helps supporting move advanced use
71cases such as template instantiations on demand and automatic language
72interoperability. It also helps static languages such as C/C++ become apt for
73data science.
74
75
76Related Reading
77===============
78`Cling Transitions to LLVM's Clang-Repl <https://root.cern/blog/cling-in-llvm/>`_
79
80`Moving (parts of) the Cling REPL in Clang <https://lists.llvm.org/pipermail/llvm-dev/2020-July/143257.html>`_
81
82`GPU Accelerated Automatic Differentiation With Clad <https://arxiv.org/pdf/2203.06139.pdf>`_
83