Page 1 of 1

[AHK v2 64-bit] Compile C to memory using TCC

Posted: 17 Dec 2018, 02:54
by oif2003
Hi,

Here's a script for writing C functions inside AHK scripts. It uses TCC's libtcc.dll to compile C code to memory and returns a function pointer without saving temporary files to disk. Unlike MCode, code is compiled when an object of class tcc is instantiated. Code can be added either as a string literal or as a comment block. Requires TCC and AHK v2 64 bit.

https://bellard.org/tcc/
https://download.savannah.gnu.org/relea ... 64-bin.zip

Usage:

Code: Select all

;define a C function string
string1 := new tcc('
	(
		char* hello() {
			return "hello world";
		}
	)')
msgbox(DllCall(string1["hello"], "AStr"))

Code: Select all

;explicitly include User32.dll so MessageBox will work
comment2 := new tcc(parseC("Block 2"),,, "C:\Windows\System32\User32.dll") 
/*_C Block 2
	#include <stdio.h>
	#include <windows.h>
	
	int hello(char name[]) {
		char buf[256];
		snprintf(buf, sizeof buf, "Hello %s!", name);
		MessageBox(0, buf, "Greeting", 0);
	}
*/
DllCall(comment2["hello"], "AStr", A_UserName)
Full script:
Spoiler
Edit: Minor fixes

Re: [AHK v2 64-bit] Compile C to memory using TCC

Posted: 17 Dec 2018, 03:15
by nnnik
Nice - I can already see this replacing MCode.

Re: [AHK v2 64-bit] Compile C to memory using TCC

Posted: 17 Dec 2018, 03:16
by Helgef
Great job, works great :clap:.

Cheers.

Re: [AHK v2 64-bit] Compile C to memory using TCC

Posted: 17 Dec 2018, 08:08
by nnnik
How does TCC compare to compiled code by VSC++ or gcc performance wise?

Re: [AHK v2 64-bit] Compile C to memory using TCC

Posted: 17 Dec 2018, 08:23
by Helgef
As far as I remember TCC does almost no optimisations, unlike gcc which has great optimisation capabilities. However, this makes TCC fast, so in that regard it performs superior to both gcc and VS.

Cheers.

Re: [AHK v2 64-bit] Compile C to memory using TCC

Posted: 17 Dec 2018, 11:52
by nnnik
As far as I can tell the compilation time is irrelevant when compared to gcc and VS.
gcc and VS are roughly similar with gcc being slightly ahead in most cases.

No optimization is kind of bad - so I guess MCode still has its uses.

Re: [AHK v2 64-bit] Compile C to memory using TCC

Posted: 17 Dec 2018, 12:20
by Helgef
Yes ofc, compilation time doesn't matter once compiled :D.

Cheers.

Re: [AHK v2 64-bit] Compile C to memory using TCC

Posted: 17 Dec 2018, 14:29
by oif2003
nnnik wrote:
17 Dec 2018, 11:52
Helgef wrote:
17 Dec 2018, 12:20
Thanks! Indeed, this was not intended to be a replacement for MCode, instead it's more like a scripting solution in C. :D

At the moment I see these potential uses:
1) for testing out C snippets as potential candidates for MCode/Dll,
2) to offer maximum transparency (as no compiled code is provided),
3) for JIT solutions that require C code to be generated at runtime

For computationally intensive tasks where portability is not required, Julia offers a viable alternative. It is fast enough to be used as a scripting language for some tasks, and it's exceptionally good at handling math. Here's a very primitive example using Julia's libjulia.dll to evaluate a string from AHK v2:
(Requires installation of Julia Language: https://julialang.org/downloads/)
Spoiler
Edit: Fixed typo
Edit2: Fixed code example

Re: [AHK v2 64-bit] Compile C to memory using TCC

Posted: 17 Dec 2018, 15:04
by CyL0N
Dope stuff, a good enough reason to finally get on with v2. I've been putting it off for too long. Thanks dude.