fortrangoingonforty/fortsh / 095ce6e

Browse files

remove outdated 127-char limit from README and COMPILER_NOTES — C string library eliminated it

Authored by espadonne
SHA
095ce6e2278d8516c8ec6077bb5b1b229f25e684
Parents
a68e7cd
Tree
0d7fc3a

2 changed files

StatusFile+-
M COMPILER_NOTES.md 43 41
M README.md 0 1
COMPILER_NOTES.mdmodified
@@ -2,68 +2,70 @@
22
 
33
 ## TL;DR
44
 
5
-- **Linux**: Use `gfortran` (works great)
6
-- **macOS ARM64 (M1/M2/M3)**: Use **LLVM Flang (`flang-new`)** - gfortran has serious bugs
7
-- **macOS x86_64**: Use `gfortran` (should work fine, untested)
5
+- **Linux x86_64**: Use `gfortran` (works great)
6
+- **Linux aarch64**: Use `gfortran` (auto-enables C stat helpers for struct layout differences)
7
+- **macOS ARM64 (M1/M2/M3/M4)**: Use **LLVM Flang (`flang-new`)** — gfortran has serious bugs
8
+- **macOS x86_64**: Use `gfortran` with `-frecursive`
89
 
10
+The Makefile auto-detects your platform and selects the right compiler. Just run `make`.
911
 
10
-Lots of potential bugs uncovered in gfortran ARM64
12
+## macOS ARM64: Why flang-new?
1113
 
12
-1. **Stack corruption** - Large stack arrays (600KB+) corrupt memory
13
-2. **Deferred-length allocatable bug** - `character(len=:), allocatable` loses length descriptor
14
-3. **Intent(out) crashes** - Subroutine return epilogue segfaults
15
-4. **Allocatable string assignment corruption** - Assigning to allocatable strings in types corrupts heap
16
-5. **Automatic finalization crashes** - Crashes during automatic cleanup
17
-6. **Substring slice crashes** - `buffer(:length)` operations segfault
18
-7. **Empty string assignment corruption** - `buffer = ''` corrupts heap
19
-8. **flush() in loops corruption** - Frequent stderr flush in tight loops corrupts heap
14
+gfortran on Apple Silicon has at least 8 confirmed bugs that make it unusable:
2015
 
21
-So we switched to flang-new
16
+1. **Stack corruption** — Large stack arrays (600KB+) corrupt memory
17
+2. **Deferred-length allocatable bug** — `character(len=:), allocatable` loses length descriptor
18
+3. **Intent(out) crashes** — Subroutine return epilogue segfaults
19
+4. **Allocatable string assignment corruption** — Assigning to allocatable strings in types corrupts heap
20
+5. **Automatic finalization crashes** — Crashes during automatic cleanup
21
+6. **Substring slice crashes** — `buffer(:length)` operations segfault
22
+7. **Empty string assignment corruption** — `buffer = ''` corrupts heap
23
+8. **flush() in loops corruption** — Frequent stderr flush in tight loops corrupts heap
2224
 
25
+Install flang-new:
2326
 ```bash
24
-brew install llvm
27
+brew install flang
2528
 ```
2629
 
27
-This isntalls the full LLVM toolchain including `flang-new`.
30
+## flang-new String Buffer Issue (Resolved)
2831
 
29
-The Makefile auto-detects and uses Flang if available:
32
+flang-new has a known issue where Fortran string operations (substring slicing, direct assignment) on buffers larger than 128 bytes can cause heap corruption.
3033
 
31
-```bash
32
-make clean
33
-make
34
-```
34
+**This limitation has been fully worked around** via the C string library (`src/c_interop/fortsh_strings.c`), which routes all critical string operations through C code instead of flang-new's Fortran runtime. The C string library is auto-enabled for all flang-new builds (`USE_C_STRINGS`).
3535
 
36
-You'll see:
37
-```
38
-Using LLVM Flang (flang-new) - recommended for macOS ARM64
39
-```
36
+Additionally, a `safe_assign_alloc_str` routine performs char-by-char copies for allocatable strings >16 bytes, and the expansion pipeline uses C-backed growing buffers (`buffer_grow`, `buffer_append_chars`) for all variable and parameter expansion.
4037
 
41
-## Note: Flang limitations
38
+The workaround is transparent — no command length limits, no feature restrictions. macOS ARM64 passes the full test suite (3,600+ POSIX tests, 850+ builtin tests, 200+ stress tests) identically to Linux.
4239
 
43
-flang is far more stable it seems, but it has **one pretty glaring limitation**: string buffers larger than 128 bytes cause heap corruption when performing certain operations (substring slicing, direct assignment from allocatable strings) on them.
40
+## flang-new Fortran I/O Caveat
4441
 
45
-**Impact for fortsh:**
46
-- Command lines are limited to **127 characters** on apple silicon.
47
-- All other features work normally (history, tab completion, syntax highlighting, etc.)
48
-- This is a fundamental limitation we cannot work around without risking heap corruption
42
+flang-new's `write(output_unit, ...)` and `write(error_unit, ...)` cache file descriptors at process startup and don't follow `dup2` redirections. This means builtin output written via Fortran I/O bypasses shell redirections like `> /dev/null`.
4943
 
50
-**Why? because that seems odd:**
51
-- Allocating strings >128 bytes works fine, obviously
52
-- BUT operating on them (substring ops, assignments) triggers heap corruption
53
-- We attempted a "shadow buffer" pattern (1024-byte storage, 128-byte working buffer)
54
-- Even this approach still limits effective command length to 128 bytes
44
+**Fix**: Key builtins use `write_stdout`/`write_stderr` from `io_helpers.f90`, which call C `write()` directly to fd 1/2. This respects all `dup2` redirections. Files affected: `builtins.f90`, `aliases.f90`, `shell_options.f90`, `better_errors.f90`, `grammar_parser.f90`, `fd_redirection.f90`, `variables.f90`, `ast_executor.f90`.
5545
 
56
-## Alternative: x86_64 gfortran via Rosetta
46
+When adding new builtin output that users might redirect, use `write_stdout`/`write_stderr` instead of `write(output_unit/error_unit, ...)`.
5747
 
58
-If you prefer not to use Flang, you can use x86_64 gfortran through Rosetta, and tell us how it goes:
48
+## Linux aarch64: struct stat Layout
5949
 
60
-### other
50
+glibc on aarch64 uses a different `struct stat` layout than x86_64:
51
+- `st_mode` and `st_nlink` are **swapped** (mode at offset 16 on aarch64, offset 24 on x86_64)
52
+- `st_nlink` is 4 bytes (`unsigned int`) on aarch64, 8 bytes (`unsigned long`) on x86_64
53
+- `st_blksize` is 4 bytes on aarch64, 8 bytes on x86_64
54
+- Total struct size: 128 bytes (aarch64) vs 144 bytes (x86_64)
6155
 
62
-Force a specific compiler:
56
+**Fix**: C stat helper functions in `fd_wrapper.c` (`fortsh_stat_mode`, `fortsh_stat_size`, etc.) use system headers for the correct layout. Enabled via `-DUSE_C_STAT` (auto-set by Makefile when `uname -m` is `aarch64`). x86_64 uses the Fortran `stat_t` struct directly.
6357
 
58
+## Compiler Selection
59
+
60
+Force a specific compiler:
6461
 ```bash
6562
 make FC=gfortran clean all    # Force gfortran
6663
 make FC=flang-new clean all   # Force LLVM Flang
6764
 ```
68
-- **gfortran**: Frequent crashes, unusable
69
-- **flang-new**: Works great, but 127-character command limit
65
+
66
+Build flags:
67
+```bash
68
+make NO_C_STRINGS=1     # Disable C string library (will crash on flang-new)
69
+make NO_MEMPOOL=1       # Disable memory pooling
70
+make MEMPOOL_DEBUG=1    # Enable memory pool debug output
71
+```
README.mdmodified
@@ -569,7 +569,6 @@ Key differences from Linux builds:
569569
 
570570
 ## Known Issues
571571
 
572
-- macOS ARM64 has a 127-character command line limit (flang-new string buffer constraint). All features work, but long one-liners need to be broken up or put in scripts. See `COMPILER_NOTES.md`.
573572
 - Slower than bash for large scripts (it's Fortran, not a miracle worker)
574573
 - Unicode support varies by system locale
575574
 - Will not make you coffee