With respect to compiler calling the assembler, there _was_ work done to allow the compiler to generate object code directly, see: https://github.com/bmeurer/ocamljit2, as well as https://arxiv.org/pdf/1110.1029.pdf and https://arxiv.org/pdf/1011.6223.pdf.  Unfortunately, it looks like that effort stopped tracking the OCaml mainline as of version 3.12.  I don't have a good idea of how much effort it would be to resurrect that work for version 4.0.x of the OCaml compiler.  IIRC, Benedikt Meurer reads this list, so perhaps he can comment. :-)

--
Best,
Evgeny

On Wed, Jan 10, 2018 at 12:41 PM, Gabriel Scherer <gabriel.scherer@gmail.com> wrote:
# 1

Loading compiler-libs module from the toplevel requires some black
magic because the toplevel is already built by linking the same
libraries. (see MPR#6704,
https://caml.inria.fr/mantis/view.php?id=6704). The "ocaml" toplevel
does more black magic than "utop" and it works better there, but in
general I would recommend experimenting with non-toplevel programs for
this specific purpose.

# 2

The various x86_* modules seem to internally define a

  print_line : Buffer.t -> X86_ast.asm_line -> unit

which does not involve filesystem i/o. I think that the simplest
solution to your question would be to expose this function in addition
to the global one. (If I were you I would feel free to send a Pull
Request performing this change for all x86 printer implementations,
although I don't know whether the maintainers of this part of the
codebase would find it desirable.)

That said, what you would get is an in-memory representation of the
assembler *text*; the compiler calls an assembler and linker as
external programs. If for some reason you wanted to avoid this, you
would have to link to an assembler as a library, and then it would
possibly be easier to directly feed the (X86_ast.asm_line list)
abstract representation -- with some conversion into the format this
assembler library expects. For this you don't need to modify the
interface of the printer modules.

On Wed, Jan 10, 2018 at 11:20 AM, Christoph Höger
<christoph.hoeger@celeraone.com> wrote:
> Dear all,
>
> I want to trigger OCaml compilation programmatically from a C++ application
> and avoid the usage of subprocesses. It is fairly straightforward to
> generate an OCaml AST, but I cannot compile it.
>
> 1. The relevant modules/libraries do not load
>
>   utop # #require "compiler-libs.optcomp";;
>   Error: Reference to undefined global `Location'
>
>   X86_masm.generate_asm stdout [] ;;
>
>
> 2. The relevant modules seem to insist on file i/o for communication (i.e.,
> they use out_channel objects)
>
>
> Does anyone know a simple way around these issues?
>
> a) Can I load the optcomp library without modifying the compiler (or with
> some simple modifications)
>
> b) Is there a way to create an out_channel  from a memory buffer?
>
> thanks,
>
> Christoph
>
> ps: If the answer to both questions is "no", I would be willing to work with
> the community and enhance the compiler if I could get some advice on how to
> do it best and if such a modification is wanted.

--
Caml-list mailing list.  Subscription management and archives:
https://sympa.inria.fr/sympa/arc/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs