I started the work to a compiler... a compiler for my own object oriented language. My language will suport exceptions like java and c++, and others.
There is a problem: I don't know how to implement the throw exception mechanism at the low level... I mean assembler.
Has anyone some docs, or some links?
Thanks!
compiler development question
RE:compiler development question
I've thought about this a lot myself when developing my compiler, but haven't yet gotten to the production phaze yet, to worry about it (so all I can offer is suggestions...)
First off, though, I'd check GCC's output with -S. That's how I've been checking up a lot of my assembly output.
One idea I had would be to have a special register allocated to exceptions. So, if a method is defined as throwing an exception, then after the method has returned, a check upon that register is made. If it's 0, continue on; no exception existed.
Otherwise, that register contains the exception (ie, a pointer to a class derived from Exception).
This special register, of course, only need be checked on methods defined as throwing an exception. Any other method calls can continue normally. And, of course, there must be a provision to set that register to 0 upon normal exit of the method.
Jeff
First off, though, I'd check GCC's output with -S. That's how I've been checking up a lot of my assembly output.
One idea I had would be to have a special register allocated to exceptions. So, if a method is defined as throwing an exception, then after the method has returned, a check upon that register is made. If it's 0, continue on; no exception existed.
Otherwise, that register contains the exception (ie, a pointer to a class derived from Exception).
This special register, of course, only need be checked on methods defined as throwing an exception. Any other method calls can continue normally. And, of course, there must be a provision to set that register to 0 upon normal exit of the method.
Jeff
RE:compiler development question
Your solution is not good... an exception can be caught after many function calls. But this problem is already solved... I developed an algorithm witch will help me...
Thanks...
Thanks...
RE:compiler development question
It's realy simple and you don't need low level stuff to explain it
You use a exception stack. So when you have:
try{
...
}
catch(){}
}
you should push the program pointer(the only low level stuff) like EIP on intel to point after the try block(the catch block will be rebuild, see below).
And when you have a exception, guese what you do? You pop this pointer and jump to it, finding the right catch(you should rebuild the catchs to a switch:
catch(AException e){
...A...
}
catch(BException e){
...B...
}
=>
switch( exeption_type )//exeption_type - an exeption thrown.
case type(AException): ...A...
break;
case type(BException): ...B...
break;
default:
...final code... for try-catch block
It's that simple.
Anton
You use a exception stack. So when you have:
try{
...
}
catch(){}
}
you should push the program pointer(the only low level stuff) like EIP on intel to point after the try block(the catch block will be rebuild, see below).
And when you have a exception, guese what you do? You pop this pointer and jump to it, finding the right catch(you should rebuild the catchs to a switch:
catch(AException e){
...A...
}
catch(BException e){
...B...
}
=>
switch( exeption_type )//exeption_type - an exeption thrown.
case type(AException): ...A...
break;
case type(BException): ...B...
break;
default:
...final code... for try-catch block
It's that simple.
Anton
RE:compiler development question
That's what I was thinking too. But before the jump, I must restore the stack for that value... because of local variables.
This method is easy to implement, and works realy fast. But it has a weak point. I can't know from where the exception was thrown.
This method is easy to implement, and works realy fast. But it has a weak point. I can't know from where the exception was thrown.
RE:compiler development question
"I can't know from where the exception was thrown" =>
When you throw an expression you should have a special argument(later passed to the catch) witch mite containt stuff like:
line number(generated by the compiler), str message, ..
Anton
When you throw an expression you should have a special argument(later passed to the catch) witch mite containt stuff like:
line number(generated by the compiler), str message, ..
Anton
RE:compiler development question
That's true, but you don't get informations like java gives you. When you catch an exception in java, you can "see" the whole list of methods witch throwed again that exception as a stack.
RE:compiler development question
To "see" the whole list of methids witch throwed the exception, you need to write the default catch(if no one else catches this exception) like the following:
remember the arg of the last catch
generate a new info arg and trow the same exception with this info
You should also have a try/catch block which catches every exception at the begining of the program:
try{
main();
}catch(...){
...
f = getExceptionThrowsInfoList();
for( int i = 0; i < f.getSize(); i++){
...
}
...
}
Anton
remember the arg of the last catch
generate a new info arg and trow the same exception with this info
You should also have a try/catch block which catches every exception at the begining of the program:
try{
main();
}catch(...){
...
f = getExceptionThrowsInfoList();
for( int i = 0; i < f.getSize(); i++){
...
}
...
}
Anton