libraries:machine_code:mcl

This is an old revision of the document!


MCL [v1][v2]

THIS IS ALPHA SOFTWARE

MCL is a new kind of machine code library for AutoHotkey, which takes a no-nonsense approach to converting compiled code to AutoHotkey embedded machine code. It requires a GCC-based cross-compiler installed under PATH (named as gcc.exe/g++.exe or x86_64-w64-mingw32-gcc.exe/x86_64-w64-mingw32-g++.exe) to run the tests, examples, or to compile any code.

It was designed to work with mingw-w64 installed via Cygwin, and mingw-w64 installed via MYSYS2, but has been tested to work with TDM-GCC as well.

Once your C or C++ code has been compiled to a standalone function or class, that standalone class will have no dependency back to MCL. Scripts using your compiled code will need to include just the compiled code, and nothing else. You can check the releases of cJson.ahk [v1][v2] for an example of what compiled code would look like in a script.

MCL has a few extra features compared to other machine code tools, the big ones being:

  • Support for both C and C++
  • Includes some basic hand-written replacements for some of the most common standard headers
  • Support for both 32 and 64 bit
  • Allows very large constants (and Doubles, which GCC tends to promote to being a global constant, breaking code under other tools)
  • Allows exporting one or more functions to AutoHotkey
  • Allows exporting one or more global variables to AutoHotkey
  • Generates a wrapper with all the DllCalls/NumGet/NumPut calls pre-bound with offsets and types
  • Allows importing functions from external DLLs into the C/C++ code, such as from the Windows API or any other DLL you have laying around
  • Generates standalone AutoHotkey code with the compiled machine code and wrappers, so you can include compiled code without including the MCL library.

Altogether, MCL has just enough features that a solid subset of standard C code can be compiled under it, and run (more or less) fine.

The primary goal of MCL is to enable much more complex machine code functions/libraries without resorting to a dll. For example cJson.ahk [v1][v2] which is compiled under MCL, and makes use of global variables, "exporting" multiple functions, and floating point numbers.

MCL also supports a tiny subset of C(no STL, exceptions, or RTTI). For example, see [[https://github.com/G33kDude/MCL.ahk/blob/main/Tests/CPP/Spooky.cpp implementation of the SpookyHash hashing algorithm.

MCL also adds the ability for code to "import" functions from DLLs in the same way you can with DllCall. This feature doesn't replace DLL files, and isn't the primary goal of the project. Just think of it as a cherry on top.

And a (low level) warning for this feature: There is a bug with mingw-w64 compilers which results in the stack being misaligned, which will cause a crash the next time a Windows API (or standard library) function is called from your C/C. This bug can be tracked [[https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54412

==== C as Library ====

==== C Return Struct to AHK ====

==== C Set Global from AHK ====

==== C with Floats ====

==== C Write to File ====

==== Import DLL with Headers (Lua) ====