🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Saving compiled bytecode

Started by
26 comments, last by Gyrbo 19 years, 11 months ago
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!
Advertisement
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

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.

[edit]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]
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

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.

Thank you for your advice.

[edit]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]
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

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.
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

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.
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

This topic is closed to new replies.

Advertisement