vnmakarov / mir

A lightweight JIT compiler based on MIR (Medium Internal Representation) and C11 JIT compiler and interpreter based on MIR
MIT License
2.24k stars 147 forks source link

Handle errors without terminating the whole program #328

Open 5E-324 opened 1 year ago

5E-324 commented 1 year ago

Currently, errors are handled by ctx->error_func, which is set by MIR_set_error_func. According to the way it's used (e.g. in MIR_link), it seems that the callers expect it to terminate the program, because otherwise they will continue as if there's no error. But sometimes, the program needs to record the error and continue.

In order to achieve that:

Alternatively, the errors can be saved in the context. If a function fails, the program can retrieve them by calling a function like GetLastError or glGetError and glGetShaderInfoLog

Error handling of OpenGL can be used as a reference since both OpenGL and MIR involve compilation and linking. See 1 2 3. Both of the aforementioned methods exist in OpenGL.

The method used in c2mir_compile is also usable.

Error handling methods which MIR uses also need to be unified. Currently, the three related functions c2mir_compile, MIR_scan_string, and MIR_link handle and report errors in three different ways!

vnmakarov commented 1 year ago

Thank you for your feedback. It is important to me.

I am agree the error handling could be improved. I did not pay much attention to this as I believe the development of MIR code is different from other programming languages with a typical cycle of getting errors and fixing them manually. I expect that the MIR code is generated and fixing one error requires more time than in the mentioned cycle.

Still I'll try to find time to work on this issue. Unfortunately I do not know when it happens as I have more high-priority projects these days. Also I am open to pull requests to improve the error handling.

5E-324 commented 1 year ago

Surely, MIR code is generated. However, sometimes it is generated from user provided C code, the errors in which need to be reported to the user. Syntax errors are handled by c2mir, so no problem. But linking errors are discovered by MIR_link, in this case, it needs to handle the errors in a different way. I can try to improve this. How would you like MIR to handle linking errors? Personally, I prefer using a method similar to what OpenGL uses: MIR_link will return false in case of failure, the error messages will be saved in ctx, and can be retrieved by a calling a function. Do you think this is OK?