• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Saving compiled bytecode

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

27 replies to this topic

### #1Gyrbo  Members

Posted 15 July 2004 - 10:22 AM

This has ben brought up on a couple of occasions and I decided to try and do this. After browsing through the code I came to the conclusion that this wouldn't be very difficult. My current approach is to simply dump all the the variables to a file (traversing all arrays, strings and classes) after Build() has been called on a module. To load the bytecode, you would simply restore all the data right after you create the engine. I'm currently working on this but because of the amount of classes involved I haven't finished yet. I'm currently using function such as WriteProperty(), but I would preffer to add a function asCProperty::Write() (this is an example, it would need to be done for each class). The problem with this is that it becomes a part of AngelScript itself and this may not be the best approach. I have some questions about the state of certain variables: - What is asCModule::globalMem used for? Can I simply dump it to a file or is special pre/post proccessing needed? - asCScriptFunction::cleanCode only seems to be used during the Build() stage, can I safely discard its contents? Comments appreciated!

### #2Andreas Jonsson  Moderators

Posted 15 July 2004 - 03:20 PM

I welcome your effort, though I don't think it is quite so easy as you think. There are a few places where you would have to exchange pure address pointers for placeholders which would have to be resolved at load time. You should also verify that the engine was configured with the exact same functions as function ids would otherwise be wrong.

asCModule::globalMem is dynamic memory for holding global script variables. If you call @init after loading the bytecode I think you don't have to save the content of the array (just the size).

cleanCode is in fact used by the exception handler to clean up each function in case of exceptions. Thus you should definitely save this code as well.

I will give you all the support I can in the form of answering questions.

Regards,
Andreas

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

### #3Gyrbo  Members

Posted 16 July 2004 - 01:45 AM

I'm currently assuming that the system functions aren't being changed. In your code, you would have an if statement around your AddScriptSection() and Build() code. This code only has to be executed if bytecode is not available. There is actually no special compiler, the game/program acts as a compiler. After it first runs you have both a "compiled" and a source version and you can then remove the source version and the program uses the compiled.

I'm still writing the code so I can't test to see if it works or not. I'll report back when I have done some tests.

The union in asCProperty always seems to use index for script variables. Can I safely assume this to always be the case?

[edit2]I'm running into some problems with calling system functions. I'll debug it some more tomorrow and see if I can fix this up. System functions are registered in the engine and not the module, so I have no idea why it errors out...

[Edited by - Gyrbo on July 16, 2004 2:45:19 PM]

### #4Andreas Jonsson  Moderators

Posted 16 July 2004 - 03:52 PM

Script variables are referenced with indices. I have no plans to change this at the moment. Variables registered by the host application are references by address pointer though. I could change this to be by index, but that would affect the performance slightly, though I suppose that can be a small cost for the added benefit of precompiled byte code.

It could be that you are running into the same bug reported by Dentoid in the following thread:

http://www.gamedev.net/community/forums/topic.asp?topic_id=257373

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

### #5Gyrbo  Members

Posted 17 July 2004 - 12:42 AM

I'm only storing the script variables anyway, it's up to the host program to register the host variables.

I'll see if that's the same bug I'm having problems with. If this doesn't fix my problems I might try another approach.

Thanks to the wonderful debugger that comes with MSVC6.0, I found the problem. paramSize contained a bogus value and thus a crash in the ASM code. This was because this variable is being calculated by running asCScriptEngine::PrepareEngine(), which in turn is being called by (surpize surprize) asCModule::Build().

I still have to do some more tests and clean up the code a bit before I can show you guys anything. I'll probably place it all in a nice class and friend it in the needed classes so it has access to protected variables.

[edit2]My addition is almost done. Because I have no real-life code to work with, I'm not sure all functionality works. If someone can send me some example code I can test to see if it all works. I'd hate to send buggy code to WitchLord.

[Edited by - Gyrbo on July 17, 2004 10:42:21 AM]

### #6Andreas Jonsson  Moderators

Posted 17 July 2004 - 02:01 PM

You don't have to save the value of the registered global properties, but since the bytecode is using the absolute address directly you will have to somehow convert these values so that the correct addresses can be restored at load time. In this case I think the easiest solution is to simply create a new bytecode that references the properties by index instead, and change the compiler to output that bytecode instead.

Perhaps you can use the test framework available from the AngelScript site for your tests. You could even write your own tests as needed and send them to me.

I hope that I will be able to include your contribution in my library, but even if I can't do it at this moment I don't want your work to go to waste. It is too valuable for that and there are many developers asking for this feature. I promise you that I will upload your work to the AngelScript site as a stand alone improvement on AS, at least until I will have the time to include it in the official library.

I hope you don't feel bad about that. Your efforts are appreciated and I can assure you that they will be included in the library one day. I'm just not sure that this is the right moment to do it.

Regards,
Andreas
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

### #7Gyrbo  Members

Posted 18 July 2004 - 12:02 AM

I was using the test framework, but the problem is that very few of the tests use an actual script. Most use ExecuteString(), which makes it impossible to save the bytecode. I'll see if I can slap together some scripts for this.

I'll have to perform some tests with global variables to see how to solve this, but it shouldn't be much of a problem.

The problem with making it an external add-on is that the new class uses some methods in asCScriptEngine and it has to friend with asCModule to be able to access protected variables.

### #8Andreas Jonsson  Moderators

Posted 18 July 2004 - 01:58 AM

I didn't mean to make it an external add-on. I just meant that I won't have the time to include it in the library for a while (I don't have time to do much at all on Angelscript right now). That is why I will upload your library as is so that others can take advantage of your work as an unofficial version. When I get the time I will make an official release that include your work.

It's good that you've made it a separate class, that should be much easier for me to import and maintain for future releases.

How have you designed the interface for this functionality?

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

### #9Gyrbo  Members

Posted 18 July 2004 - 03:16 AM

asCScriptEngine has two extra methods SaveByteCode() and RestoreByteCode(). Both take the module to save/restore and a special interface class as arguments.
This class is much like the asIOutputStream, but instead it's fully binary and supports reading and writing.
It's up to the user to implement code for checking if it should use the bytecode or the sourcecode. You can't pass both to the engine.

I'm downloading the latest version and I'll use that to do my further testing. Once everything work to my liking, I'll send the changes to you.

### #10Andreas Jonsson  Moderators

Posted 18 July 2004 - 08:00 AM

Excellent, that is the way I had imagined implementing this as well.

From what you tell me, I should be able to include your implementation already in version 1.9.0 (the next interface change).

Regards,
Andreas
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

### #11Gyrbo  Members

Posted 18 July 2004 - 09:50 AM

I tested the code and it seems to work properly. I also think I understand why the global variables could be a problem. The following code won't work:
int* num = new num;engine->RegisterGlobalProperty("int num", num);

The only way I see this to be currently possible is the following:
int ptoi(int* num) return *num;...int* num = new num;engine->RegisterObjectType("intp", sizeof(int*), 0);engine->RegisterGlobalProperty("intp num", &num);engine->RegisterGlobalFunction("int valueof(intp)", asFUNCTION(ptoi), asCALL_CDECL);

This isn't the cleanest approach, but it should work. There's probably some way to automate this, but I can't currently think of any. Maybe using a special function to register the pointer of a pointer?

Here's the code for my modifications, simply extract to the angelscript root dir: http://users.pandora.be/gyrbo-be/AS/AngelScript-bytecode.tar.bz2

### #12Andreas Jonsson  Moderators

Posted 18 July 2004 - 10:08 AM

You don't have to register a new type for int *, you can use it directly.

int *num = new int;
engine->RegisterGlobalProperty("int *num", &num);

When I get the time I will make the necessary changes to make global properties to work with saved byte code. It should only be necessary to create a new bytecode with similar functionality as BC_PGA, and changed the compiler to output this one instead of BC_SET4 with the absolute address. This change will most likely work directly with your code changes.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

### #13Andreas Jonsson  Moderators

Posted 19 July 2004 - 02:26 PM

I downloaded your file, but for some reason I couldn't unpack it.

Would it be possible for you to pack it as a normal .zip?

By the way, what's your full name? I would like to put your name as the author of modifications.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

### #14Gyrbo  Members

Posted 19 July 2004 - 10:29 PM

I also had some problems unpacking the file using powerarchiver, but 7-zip worked OK. I'm not sure what the problem was, but here's the zip: http://users.pandora.be/gyrbo-be/AS/AngelScript-bytecode.zip

### #15Andreas Jonsson  Moderators

Posted 20 July 2004 - 01:40 AM

Now I can also open it. Thanks. I took a quick look at the code and it looks good. It shouldn't be hard to maintain this code, so I'll probably include it with version 1.9.0. Until then I will upload it to the site for those that are interested in this functionality already.

Thanks for a great contribution to the library.

You didn't tell me your name. How would you like me to refer to you in the credits? Just Gyrbo?

### #16Gyrbo  Members

Posted 20 July 2004 - 10:17 AM

Sorry forgot that (you can look up my name in the copyright of the files I added though ;)). My full name is on Dennis Bollyn.

### #17Andreas Jonsson  Moderators

Posted 21 July 2004 - 05:42 AM

Thanks, Dennis.

I've sent an e-mail to you about the copyright. There's something we need to clear up before I can put up the files on my site.
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Posted 21 July 2004 - 09:37 PM

Yay!! to both of you, let's hope i can get it running on my code, Gybro thanks a lot for your additions to Angelscript.

I wonder if my earlier doubts are appropriate here, i was trying to create a script compiler which can run along side VC and compile the scripts before the host App has to actually run, just probably as a syntax checker. we can scout through the whole App project for RegisterGlobalFunction and get stuff out of them to see if the syntaxes are alriught before we can start the Host APP. could we build this up to a discussion?

Cheers!!!
Jayanth.KRaptor Entertainment Pvt. Ltd.http://www.raptorentertainment.com---------------------------------------------------------Why Mr. Anderson? Why? ...Why keep fighting? Do you think you're fighting for something - for more than your survival? Can you tell me what it is? Do you even know? Is it freedom, or truth, perhaps peace, could it be for love? Illusions Mr. Anderson, vagaries of perception. Temporary constructs of a feeble human intellect trying desperately to justify an existence without meaning or purpose.

### #19Gyrbo  Members

Posted 22 July 2004 - 01:42 AM

A seperate compiler is a lot harder to make than a simply bytecode saver.

The only way I see this to be possible is if you have a sort of header file which the script compiler reads and uses to known which system functions and variables are available.
For functions, this is fairly trivial because an index is used. Variables are a bit harder. You would have to also use an index for them because I don't see any way how you could know the address beforehand.

### #20Andreas Jonsson  Moderators

Posted 22 July 2004 - 02:11 AM

I will change the way global properties are handled so that they also use indices. This should make it easier to use the feature Dennis added, and also to write a separate compiler. AngelScriptBind that Thomas Suter wrote might help you in writing the compiler.

There might be a problem in the way function IDs are handled as well. The function ID holds two components, the module index and function index in the module. When loading the bytecode into a module the function ID (for each of the BC_CALL) must be updated to reflect the true index of the module. I'll have to think about how to solve this in a good way. Of course if you always load the modules in the same order the indices will always be the same.
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.