Copying WHOLE Functions

This is a discussion on Copying WHOLE Functions within the C++ Programming forums, part of the General Programming Boards category; Originally posted by Speedy5 You are right, I can't be sure. BUT, as I said, on the Retail build, it ...

  1. #16
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Speedy5
    You are right, I can't be sure. BUT, as I said, on the Retail build, it works, (the function size). I am SURE of this.
    You can't be sure of that at all. There is no way you can be certain that's the actual size from what you are doing.

  2. #17
    Speedy5
    Guest
    I know that, the author of that code is wrong. But can they clone the functions like that? Or else how?

  3. #18
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    It's possible but not portable. What may work on one compiler won't work on another. You should never ever have to "clone" a function.

  4. #19
    Speedy5
    Guest
    I really don't care if it's not portable. 99% of ppl use windows. Plus, since this is a VM (what I'm making), I can just think up another alternative, maybe not JIT. So it'll work for what I'm doing, using the C++.NET compiler (unmanaged). Now how do you clone the function? That's my question.

  5. #20
    Speedy5
    Guest
    (No offence, really, to those w/o windows who use Linux, Unix, etc) There will be VM for u guys :-D

    The project (site being remodled):
    www.blue-lightning.tk

  6. #21
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Speedy5
    I really don't care if it's not portable. 99% of ppl use windows. Plus, since this is a VM (what I'm making), I can just think up another alternative, maybe not JIT. So it'll work for what I'm doing, using the C++.NET compiler (unmanaged). Now how do you clone the function? That's my question.
    No, you don't understand. Portable meaning portable to other compilers, not platforms.

    If it's a virtual machine then why are you copying compiled code?

    You still shouldn't be cloning a function. You should understand your project and how to accomplish it more before you decide that you have to do something as "copy and paste" a function. Copying a function will accomplish nothing and there are more elegant and portable solutions.

  7. #22
    Registered User WebSnozz's Avatar
    Join Date
    Oct 2001
    Posts
    102
    If you are wanting to basically queue up a bunch of functions to be processed then you can use funciton pointers and an array and iterate through the array. Function pointer arrays are also useful for flow control.

    Learn about funciton pointer arrays here.

    At the bottom of the funciton pointer array function where he calls the funcitons uysing the array you could easily place this in a for loop. Personally I like to use an STL queue, list, or matrix, depending on what the purpose is.


    However, if you do want to edit your own program you have a problem, because once you're program is compiled and you run the EXE, it is no longer in C++, it is in machine code....
    Code:
    004010CC >/$ 55             PUSH EBP
    004010CD  |. 8BEC           MOV EBP,ESP
    004010CF  |. 83EC 44        SUB ESP,44
    004010D2  |. 56             PUSH ESI
    004010D3  |. FF15 74634000  CALL DWORD PTR DS:[<&KERNEL32.GetCommand>; [GetCommandLineA
    004010D9  |. 8BF0           MOV ESI,EAX
    004010DB  |. 8A00           MOV AL,BYTE PTR DS:[EAX]
    004010DD  |. 3C 22          CMP AL,22
    004010DF  |. 75 1B          JNZ SHORT NOTEPAD.004010FC
    004010E1  |> 56             PUSH ESI                                 ; /pCurrentChar
    004010E2  |. FF15 FC644000  CALL DWORD PTR DS:[<&USER32.CharNextA>]  ; \CharNextA
    004010E8  |. 8BF0           MOV ESI,EAX
    004010EA  |. 8A00           MOV AL,BYTE PTR DS:[EAX]
    004010EC  |. 84C0           TEST AL,AL
    004010EE  |. 74 04          JE SHORT NOTEPAD.004010F4
    004010F0  |. 3C 22          CMP AL,22
    004010F2  |.^75 ED          JNZ SHORT NOTEPAD.004010E1
    004010F4  |> 803E 22        CMP BYTE PTR DS:[ESI],22
    004010F7  |. 75 15          JNZ SHORT NOTEPAD.0040110E
    004010F9  |. 46             INC ESI
    004010FA  |. EB 12          JMP SHORT NOTEPAD.0040110E
    004010FC  |> 3C 20          CMP AL,20
    004010FE  |. 76 0E          JBE SHORT NOTEPAD.0040110E
    00401100  |> 56             PUSH ESI                                 ; /pCurrentChar
    That's not all machine code, there's some extra junk there, but you should see that this is nothing like any high level programming language(which is what C++ is). The organization of seperate funcitons disapears, which is what a compiler basically does, what you are wanting to do, but you can't compile you're own program, because it's already compiled!!!! Plus even if you could udnestand the machine code you'd still have to deal with the problem that if you try to edit the file it's going to give you an access violation because the program is running, can't edit the exe file of a running program.

    What you could do however, is write a program that opens a cpp file as just a text file, and does as you say, combines functions, and then closes the file. You would then manually compile the cpp with a standard compiler such as Visual Studio C++ and see if it will compile and run. You mentioned trying to make a compiler, that's what a compiler does, is opens something as a text file, interprets the words and symbols, and then saves it in some other language. It does not edit itself though, or try to combine it's own functions.

    ... Which came first? The compiler, or the source code for the compiler?
    WebSnozz-
    Cats have no butt cheeks.
    If one farted, then it would make a flute noise.

  8. #23
    Registered User WebSnozz's Avatar
    Join Date
    Oct 2001
    Posts
    102
    Ok, there's been about 5 post since I started writing my post, lol, got a little off of what I thought he was asking about, oh well.
    WebSnozz-
    Cats have no butt cheeks.
    If one farted, then it would make a flute noise.

  9. #24
    Speedy5
    Guest
    OK, let me elaborate a bit more. I've found a way to run the BL 7x slower than c++. I tested ifs, switches, function pointers and a few other techniques. Function pointers run fastest for running a single instruction. Then, I tested that 60% of the time is being spent calling up the functions for the instructions. So when I saw that code for cloning functions, I thought, maybe I can cut and paste the bodies of the instruction-handling functions together to form a primitive type of JIT. This would boost speed at the expense of memory (which no one really cares when you have 512mb+...lol). Portable to other compilers? I don't care. I'm using the managed extensions (.NET) anyways. So I have to use C++.NET's compiler. This part, however, and test, is in unmanaged, normal code. How do you clone?

    Thanks, WebSnozz, I'll be reading your long, "looks great at first sight" post after I post this.

  10. #25
    Speedy5
    Guest
    Well, function pointer arrays is what I had to execute a single instruction. They work great, they're fastest of the techniques I tested. (ifs, switches, func. pointers, etc) But <look at my last post>...

    Plus, using another compiler inside your interpreter is kinda gay, lol

  11. #26
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Speedy5
    OK, let me elaborate a bit more. I've found a way to run the BL 7x slower than c++. I tested ifs, switches, function pointers and a few other techniques. Function pointers run fastest for running a single instruction. Then, I tested that 60% of the time is being spent calling up the functions for the instructions. So when I saw that code for cloning functions, I thought, maybe I can cut and paste the bodies of the instruction-handling functions together to form a primitive type of JIT. This would boost speed at the expense of memory (which no one really cares when you have 512mb+...lol). Portable to other compilers? I don't care. I'm using the managed extensions (.NET) anyways. So I have to use C++.NET's compiler. This part, however, and test, is in unmanaged, normal code. How do you clone?
    You need to understand programming more before you continue with your project. You still are making absolutely no sense what-so-ever as to why you want to clone a function. You are stubborn at thinking this is what you should do, when you couldn't be more wrong.

    Post a detailed description of exactly what your problem is and why you'd think you need to do such an obscure thing, and we'll explain to you what you should be doing instead. Until then, we can't answer your question, as we've said many times, because C++ does not directly allow you to do such a thing (and for good reason, because it would never make sense to do it).

  12. #27
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Also, you're saying your language is interpretted and that you are building a virtual machine, yet you allow inline assembly. Do you understand how contradictory that is? Rethink the language as well as the way you are coding your virtual machine. You don't understand what you want.

  13. #28
    Speedy5
    Guest
    I'm not a newbie programmer just for your information...

    OK, BL is compiled into bytecode. This is portable just like Java's bytecode and .NET's bytecode. A sample would be (and it's not really like this but this is just for demonstration):


    //a+=b*c-d;
    mov reg1, b
    mul reg1, c
    sub reg1, d
    add a, reg1


    This might be compiled into byte values (figure it out, it's simple):

    0, 0, 2
    1, 0, 3
    2, 0, 4
    3, 1, 0


    To interpret this byte code, I could use a gigantic if-else statement to test the instruction id, I could use a switch, or an array of function pointers. The last one is fastest, as I tested. So:

    void bl_mov(int var1, int var2) { ... }
    void bl_mul(int var1, int var2) { ... }
    void bl_sub(int var1, int var2) { ... }
    void bl_add(int var1, int var2) { ... }

    void (*Instructions[])(int,int);
    .....
    Instructions[0]=&bl_mov;
    Instructions[1]=&bl_mul;
    .....
    struct Instruction { int ID, var1, var2; }
    ......
    Instruction i;
    (Instructions[i.ID])(i.var1, i.var2);


    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.

    All I need to know, is how to do it. I can access the code inside the functions (readonly). I know their sizes. Which way do I copy it, how many bytes (or how) to trim off the return statements, and how do I call it? Yea, its a bit advanced.

    Just a sidenote: This doesn't really prove BlueLightning's power nor extensibility. Plan #5, if you visited the site, will come shortly with amazing features.

  14. #29
    Speedy5
    Guest
    LOL, inline assembly! Yup, it has it. There is something called an if statement you can use. Using (imagining the future libraries), BL.System.GetProperty("/OS/name") and placing it into variable a, you can:

    if (a=="Windows")
    {
    asm
    {
    //.......
    }
    }
    else
    {
    // normal bl code
    }


    But we are thinking of dropping inline assembly in favor of assembly methods like:

    asm void FastStuff()
    {
    // all assembler (easy syntax with access to all variables in scope).
    }

    Plus, its not like we'll encourage it. All the libaries will be free from inline assembler. They will have native methods, true, but since they are DLLs like Photoshop, we can load/unload/replace, blah blah blah, you guys know this stuff, you're just as advanced.... :-D

  15. #30
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Speedy5
    LOL, inline assembly! Yup, it has it.
    And I again point out that you can't allow for inline assembly in an interpretted language because that would make the language not interpretted. You don't understand what you want let alone how to come to a solution. Rethink what you want to do.

    I'm reading your other post right now, I'll get back to you in a minute.

Page 2 of 3 FirstFirst 123 LastLast
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, 02: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21