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 145 forks source link

Open binary files in binary mode #399

Closed notwa closed 1 month ago

notwa commented 1 month ago

This fixes newline-conversion issues on Windows. Otherwise, I'm not able to run compiled files. This does not change fopens that appear to be only used for text files.

notwa commented 1 month ago

hmm hold on, this should be adjusted to reopen stdin and stdout as binary as well, for the tools that don't take arguments (b2m, m2b, etc)

notwa commented 1 month ago

According to https://www.man7.org/linux/man-pages/man3/freopen.3p.html#APPLICATION_USAGE using freopen to set binary mode has quirks, so I opted to use win32 API functions instead. AFAIK this only affects Windows (mingw) so I think it's fair to only attempt this on the platform. Everything seems to be working now; I cascaded a C file through each process and didn't experience any corruption.

vnmakarov commented 1 month ago

Thank you for addressing Windows issues.

It is hard to make Windows port. I spent a lot of time on this but finally gave up. The biggest problem I saw is setjmp/longjmp implementation which requires to generate very specific function prologs/epilogs. Otherwise setjmp/longjmp will never work. Unfortunately documentation about prologs/epilogs on Windows is very brief and not clear.