Thread: Copying WHOLE Functions

  1. #31
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Speedy5
    That works great, no doubt about it. But almost 60% of the time is spent calling up the function. It also runs 7x slower than C++ code. So when I saw the cloning functions code, I thought, instead of calling the functions up, lets take their code, paste them together removing the return statements (trimming off the last X bytes), and voila. One function in a char* array I can cast into a function pointer and one function call instead of many. That would definatly boost speed to, lets say, 3.5x slower, which is much better. I know it's not efficient the big function that is made but that will be up to the programmer using the 'fastrun' attribute he can place to methods telling us to do this way instead of the normal way.
    ...haha

    You're telling me that all this time that all you wanted to do was inline the function call...

    Write the word "inline" before the function definitions that you want the compiler to insert.

  2. #32
    Speedy5
    Guest
    The inline assembler is added as a nice bonus. And, as I said, its not encouraged or used in BL's libraries.

    Do you know many multimedia applications code for SSE2? and MMX? Take games or codecs. Those tehnologies aren't guaranteed to be on the user's proccessor but they use a if or something to determine which version to run. Same thing with BL, asm, native, normal code.

    BTW, It's nice talking to another intellectual programmer and not a newb. Also that from your profile, you're young too (well partly, lol).

  3. #33
    Speedy5
    Guest
    Uh no. Not inline. I'm not stupid. I wanted to copy its code, paste it into an array, remove the returns, repeat, and run. Inline code implies that you have some sort of pipeline to determine which intruction to run. That's what I have right now, a pipeline with function pointers. But, that's not what i'm trying to do right now, hence primitive JIT compiler.

  4. #34
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    No, using the word inline actually will copy the body of the function in there.

  5. #35
    Speedy5
    Guest
    I'd like to see you do that at runtime, when you don't know which order you have to place those functions. And, without a for loop when you run it. OK:

    functions:
    add, mov, sub, mul
    0 1 2 3

    in comes the bytecode: 0 3 2 1 2

    before running (JIT'ing, hehe):
    make new char array
    while removing the 'ret' s:
    insert the ACTUAL CODE of add
    insert the ACTUAL CODE of mul
    insert the ACTUAL CODE of sub
    insert the ACTUAL CODE of mov
    insert the ACTUAL CODE of sub
    insert one 'ret'
    store this char array
    make function pointer to that char array

    when function is called: call up the function pointer

    This saves time, one function is called instead of 5. What you mean by inlining is:

    // the functions are inline
    for <as many instructions as there are>
    {
    switch (instructionID)
    {
    case 0:
    add(); break;
    case 1:
    mov(); break;
    case 2:
    sub(); break;
    case 3:
    mul(); break;
    }
    }

    I tested it, this type of pipeline is slower than function pointers stored in an array, as I described how I used in a previous post. But cloning the functions is even faster, if it can be done, and safely if done right. I'm pretty sure you can, but unsure of how.

  6. #36
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    "inline" is only a hint to the compiler to generate an inline function, not a command. What happens when you take the address of an inline function via a function pointer? Two copies of the function are most likely generated.

  7. #37
    Speedy5
    Guest
    If you take an address of an inline function, it acts just like ANY other function. Its ONLY when you explicitly call it when it inlines. BTW, in visual studio, __forceinline forces the inline unless its recursive.

  8. #38
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Write that part in assembly. You can't do it directly in C++.

  9. #39
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by Speedy5
    If you take an address of an inline function, it acts just like ANY other function. Its ONLY when you explicitly call it when it inlines. BTW, in visual studio, __forceinline forces the inline unless its recursive.
    No, it's implementation defined what functions don't get inlined; and the rules governing what becomes of a function which doesn't get inlined were added very late in the standardization process.
    Last edited by Eibro; 01-19-2003 at 10:15 PM.

  10. #40
    Speedy5
    Guest
    OK, just tested, I was able to clone the function. But it's unsafe, crashes for some reason when I use Debug build and not really stable. I also can't figure out how to remove the return statement so that the next function can execute. How many bytes is it? 1? 4? That would be a major obstacle making it all safe which is sure possible. How could you do it in ASM? If you could provide any assistance in ANY way to the project, please do. This goes out to all of you!

    [email protected]
    www.blue-lightning.tk

    (Everything is being redone, most of the info on the site is old except, well the news, .)

  11. #41
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    This would be difficult enough to do in 1987 under dos. Expecting to do this under Windows Uberprofits from .NET to feed a P4 is begging for grief. Forget trying to copy and modify the compilers emitted opcodes, those were generated with a large number of assumptions that you are brutally violating. If you really want to do this you are going to have to become intimately familiar with how to generate and align the opcodes you need for your JIT compiled functions yourself. You will then need to either create a new segment, write, properly formatted, the codes you need, then mark that segment as text and do your context switch into the new read-only segment, then return and restore the state to whatever the assumptions .NET expected when you yanked the world out from under it. You can also mark your existing code segment as r/w and tell the linker to give you some room to party on. The latter will slow down your entire program, possibly quite painfully. The former takes quite a bit of time to get the new segment, but afterwards runs just as fast as dynamically loaded functions. You will also probably trip most anti virus software while doing any of this.

    Finally have a look at pfe a forth environment that needs to do a lot of the things you are doing.

  12. #42
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Heh, wow.

    I think what speedy is trying to do is take his byte code, and use his bl_add or wahtever functions to create another function and then call that instead of calling 1 function, then another, and then another. This way he has to just call 1 functions rather than X many.

    Two things, first of all. Depending on the size of your code, creating your 1 function and calling it might take longer than just calling each one.

    Secondly, this is probably not the way to do this. You should probably read some literature on building and executing bytecoe first.

    Now, relating to your language/VM here.
    From the website, it seems like you lack some fundamental knowledge in virtual machines. In your list of featurse you say 'possibly platform independent'. What is the point of making your VM if the bytecode isn't platform independent? This also makes your current problem obsolete, if you are making a VM, then you DO care if the code is portable.

    The problem with having inline ASM isn't like your argument using SSE/MMX. Different operating systems have different system calls, and ways of calling them. What speed-gain will you aquire by having to assemble the inline ASM during runtime. There is a very good reason Java does not have inline ASM, you should look into it.
    Last edited by orbitz; 01-20-2003 at 12:08 AM.

  13. #43
    Speedy5
    Guest
    I can't stress enough that the information on the site is old. It was made when the BL project was first shown to the public, around August.

    BL is platform independent, or else there would be no sense in making a VM. And the inline ASM is compiled during the compiling phase. It's stored in binary format inside the bytecode. It's called using a function pointer. It works. But, it's not platform independent. You can ALWAYS test for the target operating system at runtime to determine whether to execute it or not. Or maybe, the VM will test for that.

    As for the JIT, it's probably not going to happen, it would be cool though but it's too much work for now. Maybe later unless I can get some help...

    Since this board is awesome, I think, I will repost on similar subject once plan #5 is out to check out what you guys think of it. Everything will be updated at that time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM