Verify bytecode when parsing bytecode for the first time
Bytecode parsed by the VM should be verified for correctness before execution, allowing the removal of various checks that now happen every time bytecode gets executed.
Currently bytecode is not validated beyond just using the right binary format the parser requires. This means it's possible to compile bytecode that is invalid, such as containing too many arguments. This can lead to problems with the interpreter, because it does not use bounds checking for instruction arguments due to the overhead this imposes. We may also want to type check instruction arguments (if at all possible at parse time), allowing us to (potentially) get rid of code such as
Validation would likely be a separate module with some sort of
validate function. This function would get called after parsing a compiled code block, and would be used to validate the entire block. Any errors this function returns would result in a panic, as there is nothing sane that can be done with invalid bytecode.
The validation rules may be complex to implement, especially if we want to validate the types of instruction arguments. Assuming a correct program all these validations are essentially useless too, but I think disabling them is too unsafe to even consider offering as an option.