AspirinJunkie Posted March 6 Posted March 6 (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 March 10 by AspirinJunkie changed Release-Link SOLVE-SMART and WildByDesign 1 1
SOLVE-SMART Posted March 6 Posted March 6 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)
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now