Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib/dfa/jvm: Support for effect.finally #3784

Merged
merged 25 commits into from
Sep 17, 2024
Merged

lib/dfa/jvm: Support for effect.finally #3784

merged 25 commits into from
Sep 17, 2024

Conversation

fridis
Copy link
Member

@fridis fridis commented Sep 12, 2024

Semantic additions to effect are

  • new feature effect.finally will be called when an effect is removed from the environment, either because the code passed to instate returns normally, or because the effect was aborted, or a surrounding effect was aborted. This can be used as a resource cleanup mechanism
  • the def function passed to instate now receives the final instance of the effect as an argument, so it can use the data form the last effect instance to produce a result
  • effect.finally is called after the execution of the code has returned or was aborted and after the effect was removed from the environment, but before the function def is run, which determines the result of instate in case of an abort.
  • DFA now can distinguish effects that might be aborted from those that will never be aborted.

Implementation changes

  • for the JVM backend, effect.instate0 is now implemented in Instrinsix with less help from jvm.Runtime
  • the JVM backend now supports simple exception tables in the code.
  • JVM backend now uses effect ids (0,1,2,..) instead of effect clazz ids so the effect lists stored in FuzionThread is much shorter
  • improved comments for effect related features
  • a test was added that checks nested resource tracking effects and that finally is called when expected.

This is still work-in-progress and missing interpreter and C backend support
completely.

Two main new features:

- effect.instate0 is now implemented in Instrinsix with less help from
  jvm.Runtime

- the `def` function passed to `instate` now receives the final instance of the
  effect as an argument, so it can use the data form the last effect instance to
  produce a result

- new feature `effect.finally` will be called when an effect is removed from the
  environment, either because the code passed to `instate` returns normally, or
  because the effect was aborted, or a surrounding effect was aborted.

- `effect.finally` is called after the execution of the code has returned or was
  aborted and after the effect was removed from tne environment, but before the
  function `def` is run, which t determines the result of `instate` in case of
  an abort.

- the JVM backend now supported exception simple exception tables in the code.

- a test was added that checks nested resource tracking effects and that
  `finally` is called when expected.
Managed to remove `if (3%%4) e.static_finally` by adding the feature dependance
in `MiddleEnd.martInternallyUsed`.

Remove unused code.
Support for `finally` still incomplete, no cleanup if surrounding effect is
aborted.
…instate`

Renamed `Runtime.effect_pop_and_get` as `Runtime.effect_pop`.
…orts

For this, there is no longer a thread local jump buffer for each effect clazz,
but only one jump buffer for any effect abort.  The value passed to longjump and
returned by setjmp is the effect id.

On an instate, the id returned from setjump is then used to decide if this is a
local abort, such that instate woudl return the result of call_def, or we have
an abort of a surrounding effect, i.e., we have to longjump further to the next
instate.
This was ingored by the C backend, we can handle the effect replacing in Fuzion
code instead.
Also restructured the code not to use `finally`, which would otherwise be
executed also in case of, e.g., an `OutOfMemoryError`.
Added and improved comments.

Use `instate` instead of `install` for effects.

Remove debug code, unused code, unused method arguments.

Add constants fields for inline Strings like
`"dev/flang/be/jvm/runtime/Runtime$Abort"`.

Optimization: Added `JVM.effectId()` to create a small id 0,1,2,... to use
instead of the clazz id for effects.  This reduces the size of the list
`FuzionThread._installedEffects`.
The effect environment now has a flag `isAborted` that tracks if a given effect is ever aborted.
…er called

This happens if an effect is found never to be aborted, so no code will be
generated for call_def.
@fridis fridis marked this pull request as ready for review September 14, 2024 20:34
@fridis fridis merged commit 363e8f0 into main Sep 17, 2024
5 checks passed
@fridis fridis deleted the effect_finally branch September 17, 2024 08:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants