Jump to content

Recommended Posts

Posted (edited)

JIT.au3 - Compile and Run C Code Directly from AutoIt

Sometimes you wish critical parts of your AutoIt script could run at native speed - tight loops, numerical algorithms, or bit-level operations that AutoIt simply wasn't designed for. JIT.au3 lets you write C code as a string, compile it at runtime, and call it directly from AutoIt.

No compiler installation required. All you need is this UDF and an internet connection.
The compilation is handled by the Compiler Explorer (Godbolt) API - the compiled machine code is loaded into executable memory and made callable via DllCallAddress.

GitHub: https://github.com/Sylvan86/autoit-jit-udf
Download: Releases

Quick Start

#include "JIT.au3"

; Write C code - prefix exported functions with CALLCONV
Global $sCode = _
    'CALLCONV double doubleIt(double x) {' & @LF & _
    '    return x * 2.0;                 ' & @LF & _
    '}'

; Compile via Godbolt API
Global $mCode = _JIT_Compile($sCode)

; Call the compiled function
Global $aResult = DllCallAddress("double", $mCode.ptr + $mCode.Funcs["doubleIt"], "DOUBLE", 21.0)
ConsoleWrite("Result: " & $aResult[0] & @CRLF)  ; โ†’ 42.0

; Free memory when done
_JIT_Free($mCode)


Compile Once, Reuse Forever

Quote

Please treat the Godbolt API with respect.


The Compiler Explorer is a free community project. Every call to _JIT_Compile sends a request to their servers. (unless you have explicitly chosen a different instance - what`s possible)

After the first successful compilation, save the binary string and reuse it - no further API calls needed and much faster startup:

; First run: compile and note the ReusableString
Global $mCode = _JIT_Compile($sCode)
ConsoleWrite($mCode.ReusableString & @CRLF)  ; โ†’ save this string
_JIT_Free($mCode)

; All subsequent runs: load from saved ReusableString (no internet needed!)
Global $mCode = _JIT_LoadBinary('{"b":"8g9YwMM","f":{"doubleIt":0}}')
Global $aResult = DllCallAddress("double", $mCode.ptr + $mCode.Funcs["doubleIt"], "DOUBLE", 21.0)

See examples/example_reuse.au3 in the repository for a complete walkthrough.

API Overview

  • _JIT_Compile($sCode) - Compile C code and return executable memory
  • _JIT_LoadBinary($sBinary) - Load a previously compiled binary (no API call)
  • _JIT_Free($mCode) - Release allocated executable memory
  • _JIT_SetServer(...) - Configure server, compiler, language, proxy
  • _JIT_GetCompilers() - List available compilers
  • _JIT_GetLanguages() - List available languages
  • _JIT_DescribeOpcode($sOp) - Get description of an assembler opcode

Limitations

This UDF is designed for small, self-contained code snippets - not for full C projects.

  • No #include support - the C standard library is not available. There is no linker; the code must be fully self-contained.
  • No math.h functions - sin(), cos(), pow() etc. require libm which is not linked. Some GCC __builtin_* functions work as inline alternatives (see examples/example_builtins.au3).
  • Internet required for compilation - use _JIT_LoadBinary to work offline after the first compile.
  • Single translation unit - all code must be in one string, no multi-file compilation.

Examples

The repository includes five examples:

  • example_scalar.au3 - Basic principle: compile and call a simple function
  • example_array.au3 - Pass an array to Kahan summation vs. naive summation
  • example_string.au3 - String processing: alternating case transformation
  • example_builtins.au3 - GCC __builtin_* functions: what works, what doesn't
  • example_reuse.au3 - Save and reload compiled binaries for offline use

Dependencies

  • JSON.au3 - JSON UDF for API communication (included in release download)

Acknowledgements

This project would not be possible without the Compiler Explorer (Godbolt) by Matt Godbolt and contributors. It is an incredible open-source tool for the programming community. Please use their API responsibly.

ย 

Edited by AspirinJunkie
changed Release-Link
Posted

I know, I already comment in the German forum about this, but I really like the idea - so once again:

This is really impressive ๐Ÿ˜ฎ @AspirinJunkie, thank you!

Also, Compiler Explorer (Godbolt)ย by Matt Godbolt is awesome.
I also really appreciate that you mentioned the Godbolt API (here and in the GitHub project) โ€” very far-sighted โ€” thanks๐Ÿ‘Œ.

I gave the project a star right away and Iโ€™ll get familiar with it.
Pull requests may follow โ€” the usual GitHub stuff. Whether I can help with the code will depend on how quickly I can get up to speed.

Either way, thanks and best regards
Sven

==> AutoIt related: ๐Ÿ”—ย Organization AutoIt Community,ย ๐Ÿ”— GitHub, ๐Ÿ”— Discord Server, ๐Ÿ”—ย Cheat Sheet,ย ๐Ÿ”—ย autoit-webdriver-boilerplate

Spoiler

๐ŸŒย Au3Forums

๐ŸŽฒ AutoIt (en) Cheat Sheet

๐Ÿ“Š AutoIt limits/defaults

๐Ÿ’Ž Code Katas: [...] (comming soon)

๐ŸŽญ Collection of GitHub users with AutoIt projects

๐Ÿžย False-Positives

๐Ÿ”ฎย Me on GitHub

๐Ÿ’ฌย Opinion about new forum sub category

๐Ÿ“‘ย UDF wiki list

โœ‚ย VSCode-AutoItSnippets

๐Ÿ“‘ย WebDriver FAQs

๐Ÿ‘จโ€๐Ÿซย WebDriver Tutorial (coming soon)

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...