Skip to main content
gamedev.net
🔒 Locked

tricky machine code question!

Started by paulbird 14 replies 850+ views
Original Post
paulbird
paulbird
Does anyone know a good way of doing this? What I want to do is have a function such as int f(int x,int y) which is used a million times. Then I want the user to be able to type into a text box a new definition for this function such as: int r=x*x+y*y; return r+2; or: int z=0; for(int n=0;n!=x;n++) z=z+7; return z; The program must then compile this text into machine code and place it in the RAM somewhere. Then the program can call this function a million times by running this piece of machine code in the RAM. (If it is not compiled it would run too slowly). So the question is: Is there any freeware compilers (not necesarrily C) that I could add to my program in order to compile the code into machine code (otherwise I would have to learn a bit of machine code myself to compile it). And secondly and most importantly, once the function is in the RAM say at a pointer of 12345. How would you run this bit of code from within your program and then return it to your program. Any ideas or examples from the internet doing this? Thanks. [edited by - paulbird on December 26, 2003 10:58:39 AM]
Puzzler183
Puzzler183
For opensource compilers, GCC is the only one I can think of. And it sounds like you need to rethink what you are oding here because if it''s a timing thing, it''s just better to paste your code into a file you have to test, and compile that.
Caesar
Caesar
well I also think you should think of an another solution. I guess you could do with making the compiler compile the source into a executable that would then be excuted and load the parameters from a file... Or maybe make it into a DLL and load it from the DLL then... otherwise IMHO you''ll have to look for some protected mode information, I don''t think it would be as simple as "jump here" under protected mode. But anyway, a compiler distriubted with a application, that doesn''t sound good. There are a few interpreters that can be embedded I believe. Like lua lua.

Nik02
Nik02
If .net is an option for your project:

You could use the built-in compilers programmatically (vb.net, c#, js.net) or even create your own compiler (sample of which is in the SDK).
The MS compilers produce very high-performance code, even though it is managed; that is, it is JIT compiled to machine code at runtime.

How I wish Linux development was that straightforward, Mono being only a beta as of now

-Nik
Niko Suni
Ilici
Ilici
You could use a virtual machine there are some good tutorials on this out there.
Melekor
Melekor
Try SoftWire, a runtime assembler for x86.

http://softwire.sourceforge.net/

Basically what it does is compiles assembler code from a file or a string into machine code, then gives you a function pointer to that code, which you call.

Of course if you want to compile some C code, you'd need to translate it to assembler first and then send it to SoftWire. A bit of work, but WAY, WAY easier than doing the machine code stuff yourself. Even a stack based method using push/pop in machine code is like 25 times faster than a virtual machine..

EDIT: Credit where credit is due: SoftWire is made by Nicolas Capens, and he certainly deserves credit for his awesome product.

[edited by - Melekor on December 26, 2003 6:00:23 PM]
aboeing
aboeing
quote:

Of course if you want to compile some C code, you''d need to translate it to assembler first and then send it to SoftWire. A bit of work, but WAY, WAY easier than doing the machine code stuff yourself


has anyone actually done this, and if so, are they willing to share the source, and where can i get it?

cheers!
Krylloan
Krylloan
I think using a compiler like GCC sounds a good idea. Just insert the user''s code into the middle of a function in a source file designed to produce a dynamically-linked library, then call GCC and be prepared to field compiler errors (preferably report these back to the user).

Make sure you tell the compiler exactly how you wish to optimise (I''d guess that''d be maximum optimisation for speed, none for size).

AFAIK cross-platform dynamically-linked-library-ing is not easy.

Without more information I don''t think it''s fair for Caesar or Puzzler183 to say you have the wrong idea... I can think of a number of practical applications for what you require, such as performing a custom transform (in a GUI-based 3d animation program) for every vertex of every frame of a given animation. (Here speed matters for complex models, and asking the user to quit and recompile does not make for an efficient GUI application).
paulbird
paulbird
Thanks guys, yes in fact the application I was thinking of was a customizable filter for use in a Photo-Paint type application. Since the function has to be repeated for each of maybe a million points, I decided that a virtual machine running a byte-code would be too slow.

I don't know, but I have a feeling that compiling it into a second executable file or DLL might be too slow as well - but I'm probably wrong about that(?)

I will check out GCC and Softwire sounds very interesting as it sounds just what I was looking for except that you need assembler code.


P.S. Has anyone got an example of some code (or even a short program) for creating a DLL on-the-fly and then using the function in it? I just want to check how fast this would actually be.


[edited by - paulbird on December 27, 2003 7:35:50 AM]
zackriggle
zackriggle
To get the assembler code you could just have your C code compiled, but not linked--no .exe or .dll (you can specify this as an option for almost every compiler.) This should either give you .o (object) or .asm (assembly) files. Since I don''t know or use assembly, I have no idea what use these are to you.
aboeing
aboeing
quote:
Original post by zackriggle
To get the assembler code you could just have your C code compiled, but not linked--no .exe or .dll (you can specify this as an option for almost every compiler.) This should either give you .o (object) or .asm (assembly) files. Since I don''t know or use assembly, I have no idea what use these are to you.


Yeah but that still requires an external compiler, in which case you might aswell do as has been suggested and compile a DLL to execture. So there is also no need for SoftWire, it would be nicer if it could all be internally done.
dunkel3d
dunkel3d
Softwire seems brilliant. Have you decided on what you are going to do?

Sarah

[edited by - dunkel3d on December 28, 2003 12:21:43 AM]
teamonkey
teamonkey
Maybe you could try a compiled scripting language? SMALL is C-like and has a JIT compiler available I believe: http://www.compuphase.com/small.htm

[teamonkey]
[teamonkey] [blog] [tinyminions]
paulbird
paulbird
I think I''m going to go for the DLL solution, since its easiest as I can use a C compiler - although, it does strike me as a slightly redundant to have to save a text file from the RAM onto the hard-disk and then compile it then load it BACK into the RAM again, but never mind.

I got it working and its working great since I can write custom filters such as setting each pixel at x,y to:

RED=(Red(x+2,y)+Green(x-2,y))/2;
GREEN=Blue(x/2,y+5);
BLUE=..etc

or even make it draw a fractal or so on.
(I''ve used a few #defines here to make it easier for the user)

My problem now is that I''m compiling it with BCC32 at the moment like this:

WinExec("bcc32 -tWD thecode.cpp")

But I need someway of knowing when bcc32 has finnished compiling so that I can then load the module thecode.dll. At the moment the user has to click on another button. Is there a way of knowing when a program that you ran has closed?

I am going to have a look at those compilers you mentioned to find a small one thats they''ll let me use and thats fast enough!


Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.