Writing my own compiler for a Java-like language, I am having trouble compiling synchronized blocks
. I come up with the following idea to simplify them to try-finally
blocks:
synchonized (obj) {
statements...
}
Can be replaced with
Object _lock = obj
_monitorEnter(lock)
try {
statements...
}
finally {
_monitorExit(lock)
}
Where _monitorEnter
and _monitorExit
represent the MONITORENTER
and MONITOREXIT
instructions.
Am I correct with this assumption of how synchronized
is compiled, or am I missing something?
EDIT
My implementation previously had some special handling for return
and throw
statements within the body. Basically, it would manually load all lock
variables and MONITOREXIT
them before each *RETURN
or THROW
instruction. Is this handled by the finally
block, or do I still need these checks?