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

Post your working scripts, libraries and tools.
oif2003
Posts: 214
Joined: 17 Oct 2018, 11:43
Contact:

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

17 Dec 2018, 02:54

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
Last edited by oif2003 on 17 Dec 2018, 22:48, edited 1 time in total.
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

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

17 Dec 2018, 03:15

Nice - I can already see this replacing MCode.
Recommends AHK Studio
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

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

17 Dec 2018, 03:16

Great job, works great :clap:.

Cheers.
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

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

17 Dec 2018, 08:08

How does TCC compare to compiled code by VSC++ or gcc performance wise?
Recommends AHK Studio
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

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

17 Dec 2018, 08:23

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.
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

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

17 Dec 2018, 11:52

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.
Recommends AHK Studio
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

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

17 Dec 2018, 12:20

Yes ofc, compilation time doesn't matter once compiled :D.

Cheers.
oif2003
Posts: 214
Joined: 17 Oct 2018, 11:43
Contact:

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

17 Dec 2018, 14:29

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
Last edited by oif2003 on 19 Dec 2018, 15:37, edited 2 times in total.
CyL0N
Posts: 211
Joined: 27 Sep 2018, 09:58

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

17 Dec 2018, 15:04

Dope stuff, a good enough reason to finally get on with v2. I've been putting it off for too long. Thanks dude.
live ? long & prosper : regards

Return to “Scripts and Functions (v2)”

Who is online

Users browsing this forum: No registered users and 20 guests