Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Geek Culture / Pulling code from the compiler process... would it work?

Author
Message
Da_Rhyno
13
Years of Service
User Offline
Joined: 25th May 2011
Location:
Posted: 3rd Oct 2011 04:55
Okay guys, here's the concept... Let's say we made this in C to make it a bit easier.

Suppose I have a full compiler, with a parser and whatnot.

We already have our PE header set up for our program we're making, but now it gets down to the nitty-gritty of the sections. Suppose we have two sections for our executable we are making, a CODE section and a DATA section. The CODE section lies at RVA 4096 and the DATA section at RVA 8192.

We then have some code in our compiler:



What we then do in our compiler code where it tells us to add the two numbers is set the addresses of the global variables we have there to our EXE's base address + their RVAs. Then we get the address of addnumbers, and copy that into our EXE at the desired place in code. What that should do then is copy the necessary code to run that portion.

What do you all think? Will it work?
mm0zct
21
Years of Service
User Offline
Joined: 18th Nov 2003
Location: scotland-uk
Posted: 6th Oct 2011 03:14
I don't think anyone really understands what you are trying to describe. Are you talking about function inlining? runtime optimisation? (like dynamo).

I have a feeling you should do a little more reading into the full compiler toolchain. Compile->assemble->link. This link stage is probably the bit you are either interested in or forgetting about, most compilers don't go straight to machine code.

AMD AthlonX2 5000 black edition @2.8ghz, 4gb pc5400, AMD/ATi hd3850, creative xfi music, 24" hp widescreen 1920x1200 + 22" zalman trimon 3D 1680x1050, ECS KA3 MVP mobo
Da_Rhyno
13
Years of Service
User Offline
Joined: 25th May 2011
Location:
Posted: 6th Oct 2011 04:58
True, they don't... but this one would actually take care of both compiling and linking.

And I'm not talking about either... Pretty much you have code from your compiler EXE that isn't run at all. Instead, it's a reference for your compiler to pull from when it's making your program EXE (ie - opcodes, data, etc).
Da_Rhyno
13
Years of Service
User Offline
Joined: 25th May 2011
Location:
Posted: 6th Oct 2011 07:10
I tried it... it worked! However, you have the bit of overhead from the function being called (the previous stack base pointer being stored on the stack, the top of the stack being moved to the current base pointer), but apart from that it looks like it would work like a charm. =D

BTW: Are functions in DarkBasic Pro created in machine code similarly to C functions?
mm0zct
21
Years of Service
User Offline
Joined: 18th Nov 2003
Location: scotland-uk
Posted: 6th Oct 2011 21:38
I believe so, it sounds like you are inlining machine code for library functions at compile time, in this case the library is statically linked into your compiler's own binary.

Any decent compiler will throw away the stack pointer stuff during optimisation passes.

May I ask how old/what level of education you are at?
(I'm a PhD student dealing with low level embedded stuff, many core stuff, and simulation so I'm immersed in the sort of thing you are talking about, even if I'm not so hot on my actual compiler theory (that's for other people in my office))

AMD AthlonX2 5000 black edition @2.8ghz, 4gb pc5400, AMD/ATi hd3850, creative xfi music, 24" hp widescreen 1920x1200 + 22" zalman trimon 3D 1680x1050, ECS KA3 MVP mobo
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 6th Oct 2011 22:34
It actually sounds like he wants to pre-compile code using C, and then used the pre-compiled snippets within his own code.

The limitation of his current method is that he's using globals to do everything, which rules out any recursive code he may want to use. Better would be to use the stack to pass values in, and EAX/EDX/ST(0) to return values, or an area in memory to store results if returning a struct (as explained here).

Quote: "Are functions in DarkBasic Pro created in machine code similarly to C functions? "

Yes. DBPro uses the same calling convention as C does, with the exception that it can't return structs/types, and I mustn't forget to mention the funny DBPro floats either.

Da_Rhyno
13
Years of Service
User Offline
Joined: 25th May 2011
Location:
Posted: 7th Oct 2011 00:43
mm0zct - I am 24 years old with only a few computer science classes under my belt.

IanM - That was just a simple case to see if it would work in the first place. Also, your first sentence was right on the mark.

One reason I'm thinking about this is I'm not sure if any of you have used RPG Maker in the past. What could be done is to create a program that would make a game of sorts, then put everything into an actual executable when you're done with your project.

Login to post a reply

Server time is: 2025-05-20 11:58:48
Your offset time is: 2025-05-20 11:58:48