MCode4GCC -- C/C++ to MCode Generator

Post your working scripts, libraries and tools
User avatar
joedf
Posts: 5819
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada, Quebec
Contact:

MCode4GCC -- C/C++ to MCode Generator

20 Sep 2014, 21:09

MCode4GCC

MCode4GCC is an MCode generator using the GCC Compiler.
More about MCode here : viewtopic.php?f=7&t=32
Info & downloads here: https://github.com/joedf/MCode4GCC

Image

Questions?
Don't be shy to post your questions. Just ask away!

Credits
Special thanks to IsNull, fincs, Laszlo, SKAN, Bentschi and kon
Written by joedf (joedf@ahkscript.org)
Released under the [MIT License](http://opensource.org/licenses/MIT)

Information and Downloads here : https://github.com/joedf/MCode4GCC
Elduke

Re: MCode4GCC -- C/C++ to MCode Generator

24 Oct 2014, 13:49

dumb question... is visual c++ needed?
kon
Posts: 1760
Joined: 29 Sep 2013, 17:11

Re: MCode4GCC -- C/C++ to MCode Generator

24 Oct 2014, 14:07

Elduke wrote:dumb question... is visual c++ needed?

Nope! :D
GCC is an open source compiler. You just need GCC and an editor of your choosing.
Setting Up GCC on this script's GitHub page lists some of the recommended distributions.
User avatar
fincs
Posts: 500
Joined: 30 Sep 2013, 14:17
GitHub: fincs
Location: Seville, Spain
Contact:

Re: MCode4GCC -- C/C++ to MCode Generator

24 Oct 2014, 15:36

I use TDM-GCC. The 'normal' one just loves to link in dependencies to all sorts of hefty runtime DLLs.
fincs
Windows 10 x64 Build 14393 / AutoHotkey v1.1.24.04
Get SciTE4AutoHotkey v3.0.06.01 - [My project list]
User avatar
Relayer
Posts: 101
Joined: 30 Sep 2013, 13:09
Location: Delaware, USA

Re: MCode4GCC -- C/C++ to MCode Generator

24 Oct 2014, 15:42

Is there a repository of MCode functions with docs? I'd be interested in previous work since I'm not that proficient at writing my own C code.

Relayer
Sam_
Posts: 40
Joined: 20 Mar 2014, 20:24

Re: MCode4GCC -- C/C++ to MCode Generator

14 Nov 2014, 20:24

I'm not a c++ programmer, but there are a couple MCode functions from the old forum that I need x64 version for, so I decided to give this a try. I think I got everything set up correctly as I successfully got the test TheAnswerToEverything() function to compile to MCode. I added it as a function in my script and it works. Now I try compiling the Bin2Hex and Hex2Bin functions, and I am getting errors like:
SOURCEFILE:1:14: error: unknown type name 'UInt8'
void Bin2Hex(UInt8 *hex, UInt8 *bin, Int32 len) { // in hex room for 2*len+1 bytes
^
SOURCEFILE:1:26: error: unknown type name 'UInt8'
void Bin2Hex(UInt8 *hex, UInt8 *bin, Int32 len) { // in hex room for 2*len+1 bytes
^
SOURCEFILE:1:38: error: unknown type name 'Int32'
void Bin2Hex(UInt8 *hex, UInt8 *bin, Int32 len) { // in hex room for 2*len+1 bytes
^

What is happening here? Do I need to adjust the jargon of the data types or something?

For convenience, these are the two functions I need MCode for:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

User avatar
joedf
Posts: 5819
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada, Quebec
Contact:

Re: MCode4GCC -- C/C++ to MCode Generator

15 Nov 2014, 11:28

Yup
Sam_
Posts: 40
Joined: 20 Mar 2014, 20:24

Re: MCode4GCC -- C/C++ to MCode Generator

18 Nov 2014, 04:37

Thank you both! I have now gotten both functions to compile nicely, and everything works great in the ANSI build of AHK_L, less so in the Unicode builds. Here is my original test script:

Code: [Select all] [Expand] [Download] (Script.ahk)GeSHi © Codebox Plus


Notice that it works as expected in the ANSI build of AHK_L, but the hex data is not displayed properly in Unicode builds. This can be remedied by:

Code: [Select all] [Download] (Script.ahk)GeSHi © Codebox Plus

Bin2Hex(ByRef hex,addr,len){
VarSetCapacity(hex,(2*len+1)*(A_IsUnicode?2:1))
Global _Bin2Hex
DllCall(_Bin2Hex,"UInt",&hex,"UInt",addr,"UInt",len,"CDECL")
If (A_IsUnicode=1)
hex:=StrGet(&hex,len*(A_IsUnicode?2:1),"cp0") ; Without this, it only works properly in the AHK_L ANSI build
Return VarSetCapacity(hex,-1)//2
}

But now the hex data is not properly converted back to binary (in Unicode builds of AHK_L) and so is not written back to the file properly. My problem is I can't figure out how to undo the codepage manipulation:

Code: [Select all] [Download] (Script.ahk)GeSHi © Codebox Plus

Hex2Bin(ByRef bin,ByRef hex){
VarSetCapacity(bin,0)
;~ VarSetCapacity(bin,((StrLen(hex)+1)//2)/(A_IsUnicode?2:1))
VarSetCapacity(bin,(StrLen(hex)+1)//2)
If (A_IsUnicode=1){
; Some way to undo the Unicode compatability stuff
}
Global _Hex2Bin
DllCall(_Hex2Bin,"UInt",&bin,"UInt",&hex,"CDECL")
Return (StrLen(hex)+1)//2
}

I apologize if this is straying too far off topic, but if anyone can help me out, I'd really appreciate it!
User avatar
nnnik
Posts: 2187
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: MCode4GCC -- C/C++ to MCode Generator

21 Nov 2014, 02:43

TDM-GCC wrote:>>>>> USAGE NOTES

*** 32-bit or 64-bit compilation ***

In this edition of TDM-GCC, you can use "-m32" and "-m64" to control whether
32-bit or 64-bit binaries are generated. By default (if neither -m32 nor -m64 is
specified), 64-bit binaries will be generated. In a 64-bit binary, all pointer
math is 64-bit by default, and the built-in "size_t" and "ptrdiff_t" types are
64 bits in size (some other types are larger also). Additionally, the following
preprocessor definitions will be in effect:
* #define _WIN64 1 (also WIN64, __WIN64, and __WIN64__)
* #define __MINGW64__ 1
* #define __x86_64 1 (also __x86_64__)
* #define __amd64 1 (also __amd64__)
Be sure to use "-m32" or "-m64" at both the compile stage and the link stage.

Just saying :)
Recommends AHK Studio
sancarn
Posts: 189
Joined: 01 Mar 2016, 14:52

Re: MCode4GCC -- C/C++ to MCode Generator

08 Aug 2017, 04:05

I'm not certain how this works but it might be nice if it were to optionally use an online compiler like e.g. I have a compile-and-execute script here for C code.

This means you wouldn't have to download and install a compiler. Which is nice because in some cases (like the office) people cannot easily install software.

...Some time later...

Actually, after some investigating I made another AHK Script which compiles the code and downloads the file (because it seems you use the file in your code). See Github
User avatar
joedf
Posts: 5819
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada, Quebec
Contact:

Re: MCode4GCC -- C/C++ to MCode Generator

08 Aug 2017, 09:24

I guess you could but you don't really want to "compile" it per se. You want the assembly w/ opcodes which is done with "-Wa,-aln"... You would need something like objdump or another decompiler/disassembler.
I saw this https://github.com/yegord/snowman
but opcodes are not given :/
sancarn
Posts: 189
Joined: 01 Mar 2016, 14:52

Re: MCode4GCC -- C/C++ to MCode Generator

08 Aug 2017, 10:13

joedf wrote:I guess you could but you don't really want to "compile" it per se. You want the assembly w/ opcodes which is done with "-Wa,-aln"... You would need something like objdump or another decompiler/disassembler.
I saw this https://github.com/yegord/snowman
but opcodes are not given :/


Ah... Would this do the trick?
User avatar
joedf
Posts: 5819
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada, Quebec
Contact:

Re: MCode4GCC -- C/C++ to MCode Generator

08 Aug 2017, 10:52

I saw this also... it crases sometimes.. :/ http://www.backerstreet.com/rec/rec.htm
The website you gave could work, I saw there's a button to show the opcodes.
It seems it can be extracted as something like this <div class="opcodes" title="ff 25 02 0c 20 00">
For the example you gave me, I got this:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


which simplified gives this
ff25020c20006690554889e5897dfc8b45fc0faf45fc5dc3554889e5b8000000005dc3662e0f1f840000000000
Could not get it to work :/
even with the junk removed 554889e5897dfc8b45fc0faf45fc5dc3 to VUiJ5Yl9/ItF/A+vRfxdww==...

So I tried the compiler with a return 42 function (from the tutorial https://autohotkey.com/boards/viewtopic.php?f=7&t=32):

Code: [Select all] [Download] GeSHi © Codebox Plus

int MyFunction()
{
return 42;
}

554889e5b82a0000005dc3554889e5b8000000005dc30f1f00 becomes VUiJ5bgqAAAAXcNVSInluAAAAABdww8fAA==
junk removed
554889e5b82a0000005dc3 becomes VUiJ5bgqAAAAXcM=
works well...
User avatar
joedf
Posts: 5819
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada, Quebec
Contact:

Re: MCode4GCC -- C/C++ to MCode Generator

08 Aug 2017, 10:57

it could work, but this is wayyy out there...
If you're doing mcode, I strongly recommend doing it on a pc where you have more control.
sancarn
Posts: 189
Joined: 01 Mar 2016, 14:52

Re: MCode4GCC -- C/C++ to MCode Generator

08 Aug 2017, 11:12

joedf wrote:it could work, but this is wayyy out there...
If you're doing mcode, I strongly recommend doing it on a pc where you have more control.


Out there? Out where? :P

But quite it is a 'round-about' way to do it. And I do agree that using a web compiler does lose control, but it'd be a nice option, imo (in the sense that it's better than nothing). I will try to integrate this into the compiler. If I can't, alas. :P

Edit, Via HTTP requests we get the following data:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



which seems very manageable... It also seems the payload is JSON also.

Code: [Select all] [Download] GeSHi © Codebox Plus

{"source":"// Type your code here, or load an example.\nint square(int num) {\n    num = num + 1;\n    return num * num;\n}","compiler":"g71","options":{"userArguments":"","compilerOptions":{},"filters":{"binary":true,"labels":true,"directives":true,"commentOnly":true,"trim":true,"intel":true}}}


And for future reference the Request headers:

Code: [Select all] [Download] GeSHi © Codebox Plus

Content-Type:application/json
Request URL:https://godbolt.org/api/compiler/g71/compile
Request Method:POST
Last edited by sancarn on 08 Aug 2017, 11:38, edited 1 time in total.
User avatar
joedf
Posts: 5819
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada, Quebec
Contact:

Re: MCode4GCC -- C/C++ to MCode Generator

08 Aug 2017, 11:34

Sure no problemo! Would be very cool, I meant simply that the output is not "guaranteed" working. :(
If you do get it working that would be great! :+1:
sancarn
Posts: 189
Joined: 01 Mar 2016, 14:52

Re: MCode4GCC -- C/C++ to MCode Generator

08 Aug 2017, 11:45

Btw what do you mean by 'with the junk removed' ? As in, how do you know what is junk and what isn't?
User avatar
joedf
Posts: 5819
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada, Quebec
Contact:

Re: MCode4GCC -- C/C++ to MCode Generator

08 Aug 2017, 12:05

Oh very cool! Didn't know that had an API. This is much more promising hhaha
Junk I meant is the other parts that are not part of the function for the default example of square(), it's everything before it and everything after the ret.

Return to “Scripts and Functions”

Who is online

Users browsing this forum: No registered users and 13 guests