Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6602149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:50:34+00:00 2026-05-25T18:50:34+00:00

OCaml allows C functions to be called from OCaml programs, as long as the

  • 0

OCaml allows C functions to be called from OCaml programs, as long as the programmer follows the instructions in the “Interfacing C with OCaml” chapter of the manual.

When following these instructions, a call to a C function is translated by the native compiler to:

    movq    ml_as_z_sub@GOTPCREL(%rip), %rax
    call    caml_c_call@PLT

(amd64 instruction set here, but looking at other architectures, the scheme seems to be rather uniform).

The function caml_c_call eventually does a computed jump call *%rax, but it does a lot of things before and after. From asmrun/amd64.S:

/* Call a C function from Caml */

FUNCTION(G(caml_c_call))
.Lcaml_c_call:
    /* Record lowest stack address and return address */
        popq    %r12
        STORE_VAR(%r12, caml_last_return_address)
        STORE_VAR(%rsp, caml_bottom_of_stack)
    /* Make the exception handler and alloc ptr available to the C code */
        STORE_VAR(%r15, caml_young_ptr)
        STORE_VAR(%r14, caml_exception_pointer)
    /* Call the function (address in %rax) */
        call    *%rax
    /* Reload alloc ptr */
        LOAD_VAR(caml_young_ptr, %r15)
    /* Return to caller */
        pushq   %r12
        ret

When one wants to frequently execute a couple of instructions that neither allocate nor raise exceptions, the above is a little bit overkill.

Does anyone have any experience in calling a small assembly routine directly from OCaml, without going through the caml_c_call stub? This probably involves tricking the native compiler into thinking that it is calling an ML function, or modifying the compiler.

The question is in the context of the library Zarith, where small assembly bits of code could compute and return most results directly, without having to go through caml_c_call, and only jump to caml_c_code for the difficult arguments that require allocation or exceptions. See this file for examples of assembly bits that could be executed directly.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:50:34+00:00Added an answer on May 25, 2026 at 6:50 pm

    Maybe “noalloc” and “float” could be of some use?

    PS some more related links.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Coming to OCaml from Lisp, I find myself very confused about when functions return
The OCaml GC imposes a global lock that prevents mutators (threads) from running in
F# is derived from OCaml, but what major items are missing or added? Specifically
I'm a very novice OCaml programmer so please forgive me if this is a
I'm writing a simple little ocaml program that reads an algebraic statement in from
My configuration is as follows: OCaml is installed by Homebrew, according to its default
In my program in OCaml, I need to randomly select a string from a
(The following question concerns the OCaml language and has examples in OCaml, but the
How to read contents from file in ocaml? Specifically how to parse them? Example
I'm switching from Haskell to OCaml but I'm having some problems. For instance, I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.