MCode4GCC -- C/C++ to MCode Generator

Post your working scripts, libraries and tools for AHK v1.1 and older
sancarn
Posts: 224
Joined: 01 Mar 2016, 14:52

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

08 Aug 2017, 12:09

joedf wrote:which simplified gives this
ff25020c20006690554889e5897dfc8b45fc0faf45fc5dc3554889e5b8000000005dc3662e0f1f840000000000
Could not get it to work :/
That may be because you didn't get all the opcodes. Just looking at it now myself... I made this JS script:

Code: Select all

console.clear()
var s=""
var counter = 0
var hOpcodes = Array.from(new Set(document.getElementsByClassName("opcodes")))
hOpcodes.forEach(function(e){
    s+=e.innerText
    counter++
    if(counter==hOpcodes.length){
        console.log(s)
    }
})
And the opcodes are:

ff25020c20006690554889e5897dfc8b45fc0faf45fc5dc3554889e5b8000000005dc3662e0f1f8400000000000f1f440000

Edit: Nevermind it doesn't affect the final result:
554889e5b82a0000005dc3

Edit 2: Actually no... I can't get the OPCodes you got... xD

I got: 554889e5897dfc8b45fc0faf45fc5dc3

Or in base 64: VUiJ5Yl9/ItF/A+vRfxdww==
User avatar
joedf
Posts: 8940
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

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

08 Aug 2017, 15:40

Oh what haha XD
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
sancarn
Posts: 224
Joined: 01 Mar 2016, 14:52

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

08 Aug 2017, 18:18

I've made some code to generate the simplified opcodes via the site. The question is, does the generated code work? :P

Square function:
554889e5897dfc8b45fc0faf45fc5dc3

Return 42:
554889e5b82a0000005dc3

StrLen:
554889e548897de8c745fc000000008b45fc4863d0488b45e84801d00fb60084c074068345fc01ebe68b45fc5dc3

Edit: I think I made a mistake... Fixed.


The answer to which is, no :P

Unless 2^2 really does equal -123281152... I think I may need to grit my teeth and install GCC for now. It looks like the assembler code is correct but I guess the opcodes are just incorrect...
User avatar
joedf
Posts: 8940
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

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

10 Aug 2017, 07:26

The opcodes might be jumbled for another system.... :/
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
sancarn
Posts: 224
Joined: 01 Mar 2016, 14:52

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

13 Aug 2017, 07:33

joedf wrote:The opcodes might be jumbled for another system.... :/
So I contacted the creator and apparrently the solution is quite simple! Options: "-mabi=ms" forces the compiler to compile for windows OS!
Yup: you're compiling code with Linux GCC when you call Compiler Explorer's g71 compiler.

Windows and Linux use x86 instructions (naturally!) but different calling conventions. For Linux (AMD abi), when you call a function with a single parameter, the function expects the argument to be in rdi, and returns the result in rax. In Windows (Microsoft ABI) the function expects the argument to be in register rcx.

Luckily GCC supports targeting the Microsoft ABI. Add "-mabi=ms" to the options you pass to Compiler Explorer and you should be ok.
I've updated the original code on github. Give it a try!

I retried my 3 tests also:

Square number (Working):
msgbox, % DllCall(MCode("1,x64:0fafc989c8c3"),"int","2")

Return 42 (Working):
msgbox, % DllCall(MCode("1,x64:b82a000000c3"))

Length of string (Not working??):
msgbox, % DllCall(MCode("1,x64:803900741bba01000000660f1f44000089d04883c201807c11ff0075f3f3c39031c0c3"),"str","abcdefghi")

I'm not entirely sure why the length of the string isn't working... Maybe my code is faulty?

[code]
int stringlen(char *str){
int i=0;
for (; str!=0; i++);
return i;
}
[/code]

Edit: Also tried running the compiler without -Ofast, to see if that made a difference, msgbox, % DllCall(MCode("1,x64:554889e54883ec1048894d10c745fc000000008b45fc4863d0488b45104801d00fb60084c074068345fc01ebe68b45fcc9c3"),"str","abcdefghi") but it still returns 1 for me...


Edit2: Riiight. I didn't follow @nnnik's tutorial... xD

Length of string (Working):
msgbox, % DllCall(MCode("1,x64:803900741bba01000000660f1f44000089d04883c201807c11ff0075f3f3c39031c0c3"),"astr","abcdefghi")
Last edited by sancarn on 13 Aug 2017, 07:57, edited 2 times in total.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

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

13 Aug 2017, 07:54

Seems like a tool (Edit: nice) joedf :geek: I'll take a closer look at this.

Sancarn, interesting additions :thumbup:
You migth want short if the string is unicode.

I'm on the phone so typing is awkward.

Cheers.
Last edited by Helgef on 13 Aug 2017, 11:50, edited 1 time in total.
sancarn
Posts: 224
Joined: 01 Mar 2016, 14:52

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

13 Aug 2017, 07:58

Helgef wrote:Seems like a tool joedf :geek: I'll take a closer look at this.

Sancarn, interesting additions :thumbup:
You migth want short if the string is unicode.

I'm on the phone so typing is awkward.

Cheers.
Yeah cheers Helpgef, turns out I should really not try to make up code I don't fully understand ;) Should have read nnnik's tutorial in more detail...
User avatar
joedf
Posts: 8940
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

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

13 Aug 2017, 11:57

@sancarn It seems my theory was right... :think: great job! Nice idea to contact them! :dance: I'll look at it later today. Again, great work! :+1:
@Helgef dont worry about it, I use my phone quite a bit to post when im on the go haha ;)
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

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

13 Aug 2017, 12:05

It is very nice joedf, thanks for sharing :thumbup: I have my own setup for this sort of stuff, it is not as nice but it has a checkbox for the -m32 flag which is very convenient. TDM GCC can compile for both 64 and 32 bit.
@sancarn, for your online stuff, you might want to consider 64/32 bit options, if possible. And, if you want wstr for your strlen function you need either

Code: Select all

int stringlen(unsigned short* str){
	int i=0;
	for (; str[i]!=0; i++);
	return i;
}
or search for two consecutive char 0, and then divide the result by 2, and welcome to the fun :D
Cheers
sancarn
Posts: 224
Joined: 01 Mar 2016, 14:52

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

13 Aug 2017, 12:21

Helgef wrote:@sancarn, for your online stuff, you might want to consider 64/32 bit options, if possible.
In general I need to add an optional compiler args argument to the function. Will add that now :)
And, if you want wstr for your strlen function you need either

Code: Select all

int stringlen(unsigned short* str){
	int i=0;
	for (; str[i]!=0; i++);
	return i;
}
or search for two consecutive char 0, and then divide the result by 2, and welcome to the fun :D
Cheers


wstr? What exactly is that variable type? It's somewhat odd. I assumed unicode would be fine in general... Would just have to multiply the result by 2, but apparently that's incorrect. My guess is unicode is passed in as a pointer or something? So the length of the pointer is actually assessed instead of the length of the string... Not entirely sure, I'm still a C++ noob. But this will definitely greatly help me improve :)
User avatar
joedf
Posts: 8940
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

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

13 Aug 2017, 12:41

Yes, would be very nice to put the online option and a 64/32bit switcher option. :+1:
Once the online version is perfected, ill look into it at some point :b
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
sancarn
Posts: 224
Joined: 01 Mar 2016, 14:52

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

13 Aug 2017, 13:11

Added optional compiler argument and also a way to define the compiler one needs to use:

Code: Select all

a := CppToOpcodesOnline(source)
b := CppToOpcodesOnline(source,"-Ofast")
c := CppToOpcodesOnline(source,"-Ofast -m32")	;Compile error (GCC 7.2 doesn't support 32 bit apparently)
c := CppToOpcodesOnline(source,"-Ofast -m32","g62")	;This returns errors for some other reason...
The Errors I'm getting are as follows:

Code: Select all

/opt/compiler-explorer/gcc-7.1.0/bin/../lib/gcc/x86_64-linux-gnu/7.1.0/../../../../x86_64-linux-gnu/bin/ld: cannot find -lstdc++
/opt/compiler-explorer/gcc-7.1.0/bin/../lib/gcc/x86_64-linux-gnu/7.1.0/../../../../x86_64-linux-gnu/bin/ld: cannot find -lgcc_s
/opt/compiler-explorer/gcc-7.1.0/bin/../lib/gcc/x86_64-linux-gnu/7.1.0/../../../../x86_64-linux-gnu/bin/ld: skipping incompatible /opt/compiler-explorer/gcc-7.1.0/bin/../lib/gcc/x86_64-linux-gnu/7.1.0/libgcc.a when searching for -lgcc
/opt/compiler-explorer/gcc-7.1.0/bin/../lib/gcc/x86_64-linux-gnu/7.1.0/../../../../x86_64-linux-gnu/bin/ld: cannot find -lgcc
collect2: error: ld returned 1 exit status
Still need to add some system to actually notify of any errors... However, can't appear to set ErrorLevel inside a function... xD
64/32bit switcher option


Might actually be better to just return both since mcode can deal with both x64 and x32... As in 2,x32:...,x64:...
ill look into it at some point :b


No rush! :)
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

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

13 Aug 2017, 13:34

@sancarn,
After typing this, I see it looks horrible, please feel free to pm me if you have questions, I do not want to derail this thread further.
wstr is unicode string. Unicode strings are two bytes per character, so the letter a is 0097 in unicode, and 97 in ANSI. Or in c, short str = 97; vs char str = 97;. When you dllcall(..., "str", str:="a") on unicode, you pass two shorts 0097 and 0000 (trailing 0000 is the null terminator) to the function, but the function expects char* so it sees four char, 00, 97,00 and 00, so when you look for (char) str == 0 you will stop on the first 00 in 00970000 that is why your first strlen attempt didn't work, but when you specify astr for the dllcall the input string is copied and converted (if on unicode build) to an ANSI string, and then the pointer to that copy is passed to your strlen function, and it points to two chars: 97 followed by 00. wstr, astr and str all pass pointers, you can also specify "Ptr", &str for the dllcall.
You can try different combinations of input to these functions for testing,

Code: Select all

msgbox(dllcall("MSVCRT.dll\strlen","wstr","☕","cdecl")) ; strlen is for ansi strings.
msgbox(dllcall("MSVCRT.dll\wcslen","str","☕","cdecl"))	; wcslen is for unicode strings
sancarn
Posts: 224
Joined: 01 Mar 2016, 14:52

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

13 Aug 2017, 13:53

Helgef wrote:so it sees four char, 00, 97,00 and 00
Riiight of course. I knew about null termination but hadn't quite thought of splitting up the unicode string into bytes why it was seeing unicode as 1 char every time :P. But now you spell it out, makes a lot more sense!
User avatar
joedf
Posts: 8940
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

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

13 Aug 2017, 18:11

It looks like full 32/64 bit is not supported?
And yes, generally a good estimate for unicode is just go with the 2*size buffer
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
sancarn
Posts: 224
Joined: 01 Mar 2016, 14:52

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

15 Aug 2017, 07:24

joedf wrote:It looks like full 32/64 bit is not supported?
Well 32 bit is supported for GCC 6.2. Of course GCC 7.2 probably does support 32 bit, but godbolt does not. The dev says it is low on the priority list, so we'll have to make do with GCC6.2 for now.
User avatar
joedf
Posts: 8940
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

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

15 Aug 2017, 07:27

wow.. i was still using 4.8.5 on my pc. Honestly, 6.2 is just fine for our uses.
cool, so 6.2 on godbolt can build to 32bit and 64bit?
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
sancarn
Posts: 224
Joined: 01 Mar 2016, 14:52

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

15 Aug 2017, 15:52

joedf wrote:wow.. i was still using 4.8.5 on my pc. Honestly, 6.2 is just fine for our uses.
cool, so 6.2 on godbolt can build to 32bit and 64bit?
Yeah 6.2 can build to both 32 and 64 bit:

Edit: See this
Last edited by sancarn on 16 Aug 2017, 01:36, edited 2 times in total.
User avatar
joedf
Posts: 8940
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

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

15 Aug 2017, 17:33

Cool, but those 2 lines are exactly the same. I am guess 64bit is without the "-m32"?
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
sancarn
Posts: 224
Joined: 01 Mar 2016, 14:52

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

16 Aug 2017, 01:34

joedf wrote:Cool, but those 2 lines are exactly the same. I am guess 64bit is without the "-m32"?
Wahaha didn't realise that xD. Yes you are correct:

Code: Select all

//32 bit:
{"source":"// Type your code here, or load an example.\nint square(int num) {\n    return num * num;\n    \n}","compiler":"g62","options":{"userArguments":"-Ofast -mabi=ms -m32","compilerOptions":{},"filters":{"0":{},"binary":true,"labels":true,"directives":true,"commentOnly":true,"trim":true,"intel":true,"length":1,"prevObject":{"0":{"sizzle1502865089194":{"undefined":{"legend":[5,0,false]}}},"length":1,"prevObject":{"0":{"sizzle1502865089194":{"undefined":{"legend":[5,0,false]}},"jQuery3210470881754013861451":{"display":""}},"length":1}},"jquery":"3.2.1"}}}

//64 bit:
{"source":"// Type your code here, or load an example.\nint square(int num) {\n    return num * num;\n    \n}","compiler":"g62","options":{"userArguments":"-Ofast -mabi=ms ","compilerOptions":{},"filters":{"0":{},"binary":true,"labels":true,"directives":true,"commentOnly":true,"trim":true,"intel":true,"length":1,"prevObject":{"0":{"sizzle1502865089194":{"undefined":{"legend":[5,0,false]}}},"length":1,"prevObject":{"0":{"sizzle1502865089194":{"undefined":{"legend":[5,0,false]}},"jQuery3210470881754013861451":{"display":""}},"length":1}},"jquery":"3.2.1"}}}

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 118 guests