Exception vectors
the_yonggrand
Posts: 13
in Propeller 2
I have just browsed the P2 docs a bit more to see its features.
However, I'm a bit concerned about the lack of exception vectors compared to other architectures such as ARM, RISC-V and even PIC.
What happened if I divide by zero? Or what ever happened when I try to access an invalid location? Does this P2 tell me about it, or it goes into undefined operation?
We know that we need to keep an eye on these when we write code. However, even if we code the best that we could, these things can slip out. Catching these automatically during runtime helps us to debug or write a program better.
However, I'm a bit concerned about the lack of exception vectors compared to other architectures such as ARM, RISC-V and even PIC.
What happened if I divide by zero? Or what ever happened when I try to access an invalid location? Does this P2 tell me about it, or it goes into undefined operation?
We know that we need to keep an eye on these when we write code. However, even if we code the best that we could, these things can slip out. Catching these automatically during runtime helps us to debug or write a program better.
Comments
If you read a hub location that has no RAM, you'll get zero.
These kinds of problems amount to a small percentage of the software bugs a programmer will likely deal with.
For what it's worth, you can set up an interrupt to fire on the QMT event (GETQ* executed while CORDIC is idle) though.
Returning a 32-bit maximum for a div-0 without stopping the system could lead to the program continuing its faulty ways.
I could imagine this scenario:
On P2 as mentioned, if b = 0, it'll return c = 0xffffffff, and could lead to very destructive situations. If "anotherFunction" was handling some moving parts outside, this could be harmful. Or, if "anotherFunction" was written to display gibberish on a screen instead of a temperature. Tracking faults like that could take a lot of time if nothing get stopped during function(int a, int b) especially when the code becomes very complex.
Meanwhile, this goes the same to reading invalid location - the program may run okay and didn't stop in P2, but to track down of this could take a lot of time too. It's better to stop the program than to accidentally read or write values elsewhere if a wrong pointer is passed.
I would have to write some handler to deal with this if it doesn't exist in the P2 hardware. If the P2 GCC has it, it'll be a good thing to hear.
This is useful for big systems and if you have big libraries of "foreign" (written by somebody else) code where you never know what happens inside.
However, for small systems I prefer the simple method: handle the errors where you expect them and not somewhere else.
I hate those ARM CPUs where you have to write 50 kB of code just to initialize all exception vectors, watchdogs, protection registers etc. before you can do something useful. The potential risk of doing something wrong with all those "obstacles" is much higher than their actual benefit, at least for small systems.
When controlling a machine, vehicle or similar things, halting the whole program is often more harmful than to continue with a slighly inaccurate value. If your software is safety critical you have to make it bullet proof by testing every possible case and branch, anyway. And Murphy's law says: There is always one more bug and the most expensive ones are surely not one of those catched by an exception handler. For examples watch this.