raz / commit b6a29ae3

commit
b6a29ae30955277b8fa46685bf5c20e6d8e047c0
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
e99cab9b
Exe.darwin: line numbers for lldb, other C libraries; Host.darwin

PLAN.md 5, step 6. A __DWARF segment holds the line table and the
compile unit, which Link now makes for both Exe files; make lldb stops
at a line on the Mac. link "name" loads @rpath/libname.dylib, its
symbols bound by flat lookup, as extern doesn't say whose they are.
test/mac/NarrowArgs checks a C procedure's narrow arguments past x7 and
v7 against cc's own. Host.darwin is the compiler's host on the Mac: a
compiler staged with it runs there, compiles and runs a program.
x86-64's output is unchanged for 202 programs; the fixpoint holds.

patch · log

16 files changed, +567 -84

.claude/worktrees/agent-a2fc7142f6816bb63 added (submodule) +1 -0

@@ -0,0 +1 @@
1+Subproject commit e99cab9b8685d604c3338e9f31339d8b2af63d18

CLAUDE.md modified +2 -0

@@ -17,6 +17,8 @@
1717 make loc # lines of the compiler, by rloc; the last, code, is the budget
1818 make fixpoint # BOOT's release builds src, that builds src, that again: the last two agree
1919 make gdb # gdb stops at a line of a test and names it
20+ make lldb # the same for lldb, on a Mac (a program made for darwin-arm64, in the container)
21+ make mac-check # on an Apple Silicon Mac: test/mac and conform/run cross-compiled in the container, run natively, and lldb
2022 make stress # 40,000 native threads; make check runs a small version
2123 make alpine # test/libc in an Alpine container, over musl (docker)
2224 make fmt # lib/, examples/ and tools/ are formatted

Makefile (history) modified +9 -2

@@ -50,6 +50,11 @@
5050 gdb: raz
5151 test/gdb.sh
5252
53+# the same for lldb, on a Mac: a program made for darwin-arm64 by the
54+# container's cross compiler (mac-build), stopped at a line
55+lldb: mac-build
56+ test/lldb.sh
57+
5358 # the compiler's speed, on a generated program of about 350,000 lines,
5459 # the best of three runs; fails below 400,000 lines a second
5560 bench: raz | build
@@ -207,7 +212,7 @@
207212 site-check: build/site/Verify
208213 build/site/Verify
209214
210-.PHONY: docker-image docker-check mac-check docker-shell prev-bench fixpoint check gdb bench kernels stress alpine fmt loc man pdf dist site site-source site-check clean install uninstall
215+.PHONY: docker-image docker-check mac-build mac-check lldb docker-shell prev-bench fixpoint check gdb bench kernels stress alpine fmt loc man pdf dist site site-source site-check clean install uninstall
211216
212217 # `make check` in a linux/amd64 container, for a machine that isn't one
213218 # (the repository is mounted at its own path, so git and the tests see
@@ -222,8 +227,10 @@
222227
223228 # test/mac's programs (test/mac.sh), on an Apple Silicon Mac: compiled
224229 # for darwin-arm64 by the cross compiler in the container, then run here
225-mac-check: docker-image
230+mac-build: docker-image
226231 $(DOCKER) $(DOCKERIMAGE) sh -c "git config --global --add safe.directory '*' && timeout 1200 test/mac.sh build"
232+
233+mac-check: mac-build
227234 test/mac.sh run
228235
229236 docker-shell: docker-image

PLAN.md (history) modified +9 -2

@@ -283,8 +283,15 @@
283283 twin, so that `Math.sqrt` is made in place (step 7). A rotation of a
284284 negative `I8` or `I16` leaves the bits above its width zero, where
285285 x86-64's `rol al, cl` leaves them as they were.
286-6. `Exe.darwin.raz`'s line numbers for lldb (its rebases came with
287- 5a); `Host.darwin.raz`.
286+6. Done. `Exe.darwin.raz`'s line numbers for lldb, in a `__DWARF`
287+ segment (mapped, read only: dyld refuses a segment bigger in the file
288+ than in memory), the compile unit and its abbreviation made by `Link`
289+ for both formats, `make lldb`; other C libraries (`link "X11";` is
290+ `@rpath/libX11.dylib`, four rpaths, binds by flat lookup);
291+ `test/mac/NarrowArgs`, a C library `cc` builds, narrow arguments past
292+ x7, a variadic call and a callback; `Host.darwin.raz`, and `HOST=darwin
293+ src/cross.sh darwin-arm64`, which stages the first Mac compiler: it
294+ runs on the Mac, compiles and runs programs, and builds `Procs`.
288295 7. The library: `CAbi`, `Sys`, `Rt`, `Thread`, `Bytes`, `Math`, `Str`.
289296 8. `test/` and `conform/` passing on the Mac; what differs by target
290297 (signals' words, errors' numbers) in files of the target's.

doc/compiler.md (history) modified +90 -17

@@ -1,8 +1,8 @@
11 # Reading the raz compiler
22
3-The compiler is fifteen files of raz, about 16,500 lines of code (the
3+The compiler is fifteen files of raz, about 16,700 lines of code (the
44 budget is 18,000); three of them, `A64.raz`, `Gen.arm64.raz` and
5-`Exe.darwin.raz`, are the Mac's back end, begun (`PLAN.md` 5), built
5+`Exe.darwin.raz`, with `Host.darwin.raz` for a compiler that runs on a Mac, are the Mac's back end, begun (`PLAN.md` 5), built
66 only into a compiler for `darwin-arm64` (`src/cross.sh`). It is its own compiler: a release of it, the one
77 `BOOT` names, builds `src/`, and `make fixpoint` checks that what it
88 makes makes itself again, byte for byte. Each release was built by the
@@ -28,10 +28,11 @@
2828 | `src/Gen.arm64.raz` | 2,295 | 1,573 | The same for arm64: values, operations, reals, checks, calls, frames |
2929 | `src/Item.raz` | 2,553 | 2,051 | Items: types, constants, variables, operators, conversions, calls |
3030 | `src/Parse.raz` | 4,947 | 4,197 | Declarations, statements, expressions, built-ins, recovery, warnings |
31-| `src/Link.raz` | 481 | 365 | Tree shaking, layout, patching, the places |
32-| `src/Exe.linux.raz` | 455 | 358 | The executable, ELF: static and dynamic, symbol table, line numbers |
33-| `src/Exe.darwin.raz` | 458 | 332 | The executable for macOS, Mach-O: dyld's binds and rebases, the signature |
31+| `src/Link.raz` | 531 | 400 | Tree shaking, layout, patching, the places |
32+| `src/Exe.linux.raz` | 430 | 336 | The executable, ELF: static and dynamic, symbol table, line numbers |
33+| `src/Exe.darwin.raz` | 559 | 394 | The executable for macOS, Mach-O: dyld's binds and rebases, libraries, line numbers, the signature |
3434 | `src/Host.linux.raz` | 103 | 52 | The system running the compiler |
35+| `src/Host.darwin.raz` | 105 | 60 | The same, for a compiler that runs on a Mac |
3536 | `src/Raz.raz` | 499 | 390 | The command line, and `-q` for an editor |
3637
3738 Every file opens with a comment that says what it is, what it depends
@@ -2601,8 +2602,11 @@
26012602
26022603 Line numbers are DWARF 4: the line table `makeLines` made from the
26032604 notes `Chunk.noteLine` made, and the single compile unit gdb needs to find
2604-it, which `putDebug` writes. There are no types or variables in it;
2605-gdb can stop at a line and show where it is, not print a variable.
2605+it, which `Link.debugInfo` and `Link.debugAbbrev` make, shared with
2606+`Exe.darwin.raz`, and `putDebug` places. There are no types or variables
2607+in it; gdb can stop at a line and show where it is, not print a variable.
2608+The table counts addresses in bytes (`minimum_instruction_length` 1),
2609+which suits arm64 too, where every row is a multiple of 4.
26062610
26072611 ## Exe.darwin.raz: the Mac's executable
26082612
@@ -2610,8 +2614,7 @@
26102614 takes it and the kernel runs it, with the same calls as Linux's: the
26112615 driver's `needLib`, `setStripped` and `write` mean what they mean
26122616 there, and `setMusl` refuses musl. `-base` is refused, since macOS
2613-loads a program where it chooses, and a library besides libSystem
2614-isn't linked yet. The file:
2617+loads a program where it chooses. The file:
26152618
26162619 | Part | What |
26172620 |---|---|
@@ -2620,6 +2623,7 @@
26202623 | `__TEXT` | the header again, at 0, and the code, read and run |
26212624 | `__DATA_CONST` | rodata, which dyld makes read-only once it has rebased it (`SG_READ_ONLY`) |
26222625 | `__DATA` | the variables and C's symbols' quadwords, zeros, not in the file |
2626+| `__DWARF` | the line numbers for lldb, three sections (below); not for `-s` |
26232627 | `__LINKEDIT` | dyld's opcodes, the symbol table, and the code signature |
26242628
26252629 and its load commands: `LC_DYLD_INFO_ONLY` for the rebases and binds;
@@ -2627,21 +2631,58 @@
26272631 for lldb, and `LC_DYSYMTAB`, which says all are local;
26282632 `LC_LOAD_DYLINKER` naming `/usr/lib/dyld`; `LC_UUID`, the first 16
26292633 bytes of the SHA-256 of what follows the header; `LC_BUILD_VERSION`,
2630-macOS 11.0 and up; `LC_MAIN`, the entry point's offset; `LC_LOAD_DYLIB`,
2631-`/usr/lib/libSystem.B.dylib`; and `LC_CODE_SIGNATURE`. The header has
2634+macOS 11.0 and up; `LC_MAIN`, the entry point's offset; an
2635+`LC_LOAD_DYLIB` for `/usr/lib/libSystem.B.dylib` and one for each
2636+other library (below); four `LC_RPATH`s; and `LC_CODE_SIGNATURE`. The header has
26322637 the room of every command, `HeaderRoom`, as the code's place is fixed
26332638 before the commands' sizes are known.
26342639
26352640 Rodata holds the addresses of other chunks (a constant's data fixups),
26362641 and macOS loads a program anywhere, so dyld must move each: a rebase.
2637-C's symbols are bound into their quadwords from libSystem by name,
2638-with Mach-O's `_` before it. Both are dyld's opcodes, the plainest form
2642+C's symbols are bound into their quadwords by name, with Mach-O's `_`
2643+before it, by flat lookup (below). Both are dyld's opcodes, the plainest form
26392644 it still takes: a rebase is `SET_SEGMENT_AND_OFFSET_ULEB` and
26402645 `DO_REBASE_IMM_TIMES`, two bytes and the offset; a bind the symbol's
26412646 name, its segment and offset, and `DO_BIND`. The other form, chained
26422647 fixups, which Apple's linker writes now, threads them through the data
26432648 itself with tables of where each chain starts on each page.
26442649
2650+**Line numbers.** lldb reads DWARF from a `__DWARF` segment, as it does
2651+Go's. `Link.makePlaces` has made the line table in the places, in
2652+rodata, where `Rt` reads it; `Exe.darwin.raz` copies it into
2653+`__debug_line`, and puts `Link.debugInfo`'s compile unit in
2654+`__debug_info` and `Link.debugAbbrev`'s in `__debug_abbrev`, the same
2655+bytes Linux's writes. The segment sits after `__DATA` and before
2656+`__LINKEDIT`, which must stay last, for the signature, and is page
2657+aligned like the others. dyld refuses a segment bigger in the file than
2658+in memory (`filesize exceeds vmsize`), so it is mapped, read only,
2659+with its sections flagged `S_ATTR_DEBUG`; nothing touches its pages
2660+unless lldb reads them from the file. `make lldb` (`test/lldb.sh`) and
2661+`make mac-check` run `lldb --batch -o 'b Procs.raz:13' -o run -o bt`
2662+on conform's `Procs` and check that it stops at the line and names each
2663+caller's. `dwarfdump --debug-line` reads the table too.
2664+
2665+**Libraries.** Every program loads libSystem, first. `link "name";`
2666+loads another, by the path `libPath` makes of the name. A name with a
2667+`/` is that path, as it is (`@executable_path/libx.dylib` works). A name
2668+that ends in `.dylib` is `@rpath/` that name. Any other is a library
2669+written as on Linux or bare: it is cut at its first dot and given
2670+`lib` before it if it has none, so `X11`, `libX11` and `libX11.so.6`
2671+are all `@rpath/libX11.dylib`, and so are `m` and `libm.so.6` for
2672+`libm.dylib`, which dyld finds in its cache. Two names of one library
2673+load it once. The program has four `LC_RPATH`s: `@executable_path`, so
2674+that a program can ship its libraries beside it, `/usr/local/lib`,
2675+`/opt/homebrew/lib`, where Homebrew keeps them, and `/usr/lib`. The
2676+compiler looks for nothing when it compiles: a program can be made
2677+where its libraries aren't, as the cross compiler does, and dyld says,
2678+when the program starts, which one it didn't find. Each C symbol is
2679+bound by `BIND_SPECIAL_DYLIB_FLAT_LOOKUP` (ordinal -2), and dyld looks
2680+for it in all the libraries the program loaded, in order, as Linux's
2681+loader does. A two-level namespace wants the library of each symbol,
2682+which `extern` doesn't say (it is found "in the C libraries the program
2683+links", section 9 of the report); flat lookup says exactly that.
2684+`test/mac/NarrowArgs` links a library `cc -dynamiclib` makes.
2685+
26452686 arm64 macOS runs nothing unsigned, so the file ends with an ad-hoc
26462687 code signature, which the kernel checks as it maps each page: a
26472688 SuperBlob holding one CodeDirectory, big-endian, of version 20400H
@@ -2702,13 +2743,41 @@
27022743 compiler for the Mac this way, in the linux/amd64 container, and `make
27032744 mac-check` (`test/mac.sh`), on an Apple Silicon Mac, has it compile
27042745 `test/mac`'s programs there and runs each here, natively, against its
2705-`X.expect` and `X.status`; then conform/run's, which are counted, those
2746+`X.expect` and `X.status` (a test with a C file, `X.c`, has it built into
2747+`libX.dylib` here by `cc`, and as a program, with `-DMAIN`, whose output
2748+must be `X.expect`, so C itself says what is right); then conform/run's, which are counted, those
27062749 that fail listed in `build/mac/conform.fail`, and those not for the Mac,
27072750 named in the script with why (`SystemImport`, as `SYSCALL` is Linux's),
2708-skipped. The host changes nothing of the target:
2751+skipped; and last `test/lldb.sh`, if lldb is there (`make lldb` is that
2752+alone, after `make mac-build`, the container's half). The host changes nothing of the target:
27092753 a program for another system still needs that system's `Sys` and `Rt`,
27102754 and for a kernel the options `-bare`, `-s` and `-base`.
27112755
2756+## Host.darwin.raz: a compiler that runs on a Mac
2757+
2758+`Host.darwin.raz` is `Host.linux.raz` for the compiler running on macOS
2759+(step 9 of `PLAN.md` 5 builds the first); it has the same five
2760+procedures and `CLib`. Four things differ. There is no `/proc`:
2761+`libraryDir` asks `_NSGetExecutablePath` for the compiler's path and
2762+`realpath` follows its links, then takes the `lib` beside it (argument
2763+0 if either fails). There is one C library, so `cLibrary` says `glibc`,
2764+meaning the system's, and there is no musl. There is no `memfd_create`:
2765+`prepareRun` is a file in `$TMPDIR` (`/tmp` if it isn't set), named by
2766+the compiler's process number, an error if that directory isn't
2767+writable; `execute` runs it with `Sys.execve`, and the file stays,
2768+for macOS to clear. And a file's code signature belongs to the file, so
2769+a program written in place over one that has run is killed:
2770+`writeExecutable` removes what is there first, then writes and sets
2771+mode 755. It uses `Sys` and `Files`, the C ones, and declares two
2772+externs of its own, `_NSGetExecutablePath` and `realpath`.
2773+
2774+`HOST=darwin src/cross.sh darwin-arm64` stages it: the Mac's `Host`, `Gen` and
2775+`Exe` in a directory of their own, `build/cross/darwin-arm64-on-darwin`,
2776+built by the cross compiler `src/cross.sh darwin-arm64` made, since
2777+BOOT's makes programs for Linux. The result is a Mac executable: on a
2778+Mac, `raz --version`, `raz -o hello Hello.raz` and `raz run` work, and
2779+it builds conform's `Procs`, which lldb stops in.
2780+
27122781 ## Raz.raz: the command line
27132782
27142783 `Raz.raz` parses the arguments, calls `Parse.compileMain`, and works out what
@@ -3144,15 +3213,19 @@
31443213 | `Sys.chmod` | `Host.writeExecutable`: makes the program's file executable (mode 755) |
31453214 | `Sys.close` | `Scan` and `Parse`: closes a source or `RAZ-VERSION` file after reading |
31463215 | `Sys.closeDir`, `Sys.openDir`, `Sys.nextEntry`, `Sys.Dir` | `Parse`: the files of a directory, to find a module's other files for the one-module-of-each-name check |
3147-| `Sys.env`, `Sys.envAt` | `Diag` (`NO_COLOR`) and `Raz.userLibraries` (`RAZ_PATH`, `HOME`; `envAt` tells unset from empty) |
3216+| `Sys.env`, `Sys.envAt` | `Diag` (`NO_COLOR`), `Raz.userLibraries` (`RAZ_PATH`, `HOME`; `envAt` tells unset from empty) and `Host.darwin.raz` (`TMPDIR`, and the environment `execute` hands on) |
31483217 | `Sys.errDenied`, `Sys.errNoEntry`, `Sys.errIntr` | `Scan`: "permission denied" and "there is no such file" in a message; a read of standard input a signal broke into, tried again |
3218+| `Sys.execve` | `Host.darwin.raz`'s `execute`: runs the program `raz run` wrote, in the compiler's place |
31493219 | `Sys.info`, `Sys.Info` | `Scan` and `Parse`: whether a path is a file (`file`) and its length (`size`) |
31503220 | `Sys.isTerminal` | `Diag`: colour only when standard error is a terminal |
3221+| `Sys.pid` | `Host.darwin.raz`'s `prepareRun`: the name of the file a program is written to |
3222+| `Sys.remove` | `Host.darwin.raz`'s `writeExecutable`: a program's file is never written over in place |
31513223 | `Sys.openRead`, `Sys.read` | `Scan`: reads a source file whole; `Parse`: the library's `RAZ-VERSION` |
31523224 | `Sys.stdin` | `Scan`: `raz -q`'s source, read from standard input |
31533225 | `Sys.readLink` | `Host.libraryDir`: `/proc/self/exe`, to find the library beside the compiler |
31543226 | `Sys.stderr`, `Sys.stdout`, `Sys.write` | `Diag`: its messages, or `raz -q check`'s rows on standard output |
3155-| `Sys.workDir` | `Diag`: shows a path relative to the working directory; `Exe`: the compilation directory of the line table |
3227+| `Sys.workDir` | `Diag`: shows a path relative to the working directory; `Link`: the compilation directory of the line table |
3228+| `Sys.writing` | `Host.darwin.raz`'s `prepareRun`: whether `$TMPDIR` is a directory the compiler may write to |
31563229
31573230 `Rt`, the run time, besides `fail`, `exit` and the places (above):
31583231

src/Exe.darwin.raz (history) modified +119 -18

@@ -13,6 +13,7 @@
1313 addresses in it (rebases)
1414 __DATA the variables, zeros, and C's symbols' quadwords, which
1515 dyld fills in (binds)
16+ __DWARF the line numbers lldb reads (below), in the file only
1617 __LINKEDIT what dyld reads, unmapped by the program: the rebases
1718 and binds, the symbol table, and the code signature
1819
@@ -32,10 +33,27 @@
3233 its pages of 4 KB, in a CodeDirectory, which the kernel checks as it
3334 maps them.
3435
36+ Line numbers are DWARF 4, the same as Linux's: Link makes the line
37+ table (in the places, in rodata, where Rt reads it too) and the one
38+ compile unit; here they are copied into __DWARF, a segment of three
39+ sections, __debug_line, __debug_info and __debug_abbrev, which lldb
40+ finds there as it does Go's. dyld refuses a segment bigger in the file
41+ than in memory, so it is mapped, read only, and never touched: its
42+ pages are read only if the program is stopped in lldb. It lies before __LINKEDIT, which must stay last, for the signature.
43+ -s leaves it out, and the symbols.
44+
45+ Libraries. Every program loads libSystem, first. A program that says
46+ `link "name";` loads that library too, by the path libPath makes of
47+ the name (below), and every C symbol is bound by flat lookup, as the
48+ ordinal -2 (BIND_SPECIAL_DYLIB_FLAT_LOOKUP): dyld looks for it in all
49+ the libraries the program loaded, in order, as Linux's loader does.
50+ The compiler doesn't know which library defines a symbol, as `extern`
51+ doesn't say, and the two-level namespace wants it to say; flat lookup
52+ is how a Mac's program says "wherever it is".
53+
3554 Depends on: Link for the images and addresses, Chunk for the chunks,
3655 Scan for names and memory, Diag for errors, Host to write the file.
37- Leaves out: line numbers (DWARF, in __DWARF, PLAN.md 5), libraries
38- besides libSystem, an export trie (the program exports nothing).
56+ Leaves out: an export trie (the program exports nothing).
3957
4058 Exported variables can't be set from outside, so needLib, setMusl,
4159 setMainPath and setStripped do it. *)
@@ -49,14 +67,20 @@
4967 Page = 4000H; (* arm64 macOS's page, 16 KB, on which each segment starts *)
5068 HashPage = 1000H; (* the signature hashes pages of 4 KB *)
5169 Dyld = "/usr/lib/dyld";
52- (* room for the header and the most load commands: see write *)
53- HeaderRoom = 32 + 72 + 3 * 152 + 72 + 48 + 24 + 80 + 32 + 24 + 24 + 24 + 56 + 16;
70+ Rpaths: [4][]const U8 = ["@executable_path", "/usr/local/lib", "/opt/homebrew/lib", "/usr/lib"];
71+ (* room for the header and the load commands but the libraries': see
72+ write. The most segments: __PAGEZERO, __TEXT, __DATA_CONST, __DATA,
73+ __DWARF (three sections), __LINKEDIT; then dyld info, symbols and
74+ the rest; the four rpaths of libPath. *)
75+ HeaderRoom = 32 + 72 + 3 * 152 + (72 + 3 * 80) + 72 + 48 + 24 + 80 + 32 + 24 + 24 + 24 + 16
76+ + 32 + 32 + 32 + 24;
5477
5578 var
5679 needed: [32]INT; (* the libraries to load, names interned *)
5780 numNeeded*: INT;
5881 stripped*: BOOL; (* -s: no symbols *)
5982 libc*: []const U8; (* the C library, libSystem *)
83+ mainPath: []const U8; (* the main module's file, which lldb will name *)
6084
6185 buf: ^U8;
6286 len, cap: INT;
@@ -89,7 +113,34 @@
89113 (* The main module's file, which line numbers will name *)
90114 proc setMainPath*(path: []const U8);
91115 begin
92- _ := path;
116+ mainPath := path;
117+end;
118+
119+(* The path a `link` name loads from. A name with a / is a path, used as
120+ it is (it may start with @executable_path/). Otherwise the name is a
121+ library's, written as on Linux or without its decoration: "X11",
122+ "libX11" and "libX11.so.6" all mean libX11.dylib, the name cut at its
123+ first dot, "lib" put before it if it isn't there; a name that ends in
124+ .dylib is kept whole. The file is then looked for, at run time, by
125+ @rpath, which the program gives four places: its own directory, so
126+ that a program can ship its libraries, /usr/local/lib and
127+ /opt/homebrew/lib, where Homebrew keeps them, and /usr/lib. Nothing
128+ is looked for when compiling, so a program can be compiled where its
129+ libraries are not, as for another machine. *)
130+proc libPath(name: []const U8): []const U8;
131+var i, stem: INT;
132+begin
133+ stem := 0;
134+ for i := LEN(name) - 1 to 0 by -1 do
135+ if name[i] = '/' then return name; end;
136+ if name[i] = '.' then stem := i; end;
137+ end;
138+ if (LEN(name) > 6) and (name[LEN(name) - 6..LEN(name)] = ".dylib") then
139+ return Diag.say("@rpath/{}", [Fmt.str(name)]);
140+ end;
141+ if stem = 0 then stem := LEN(name); end;
142+ if (stem > 3) and (name[0..3] = "lib") then return Diag.say("@rpath/{}.dylib", [Fmt.str(name[0..stem])]); end;
143+ return Diag.say("@rpath/lib{}.dylib", [Fmt.str(name[0..stem])]);
93144 end;
94145
95146 proc setStripped*(yes: BOOL);
@@ -230,7 +281,7 @@
230281 const
231282 LcSegment = 19H; LcSymtab = 2H; LcDysymtab = 0BH; LcLoadDylinker = 0EH; LcLoadDylib = 0CH;
232283 LcUuid = 1BH; LcBuildVersion = 32H; LcMain = 80000028H; LcDyldInfoOnly = 80000022H;
233- LcCodeSignature = 1DH;
284+ LcCodeSignature = 1DH; LcRpath = 8000001CH;
234285
235286 (* A segment of 64 bits, followed by its nsects sections; prot is the
236287 access, 1 read, 2 write, 4 run *)
@@ -275,20 +326,36 @@
275326 rebaseOff, rebaseSize, bindOff, bindSize, symOff, nsyms, strOff, strSize, sigOff, sigSize, slots: INT;
276327 ident: []const U8;
277328 uuidAt, end0, sigRoom: INT;
329+ dwarfOff, dwarfSize, dwarfAdr, libsRoom: INT;
330+ dbgOff, dbgSize: [3]INT;
331+ libs: [33][]const U8; (* the paths to load, libSystem first *)
332+ nlibs: INT;
333+ debug: BOOL;
278334 begin
279335 if Link.base # 400000H then Diag.fatal("-base: macOS loads a program where it chooses"); end;
280- for i := 1 to numNeeded - 1 do
281- Diag.fatal(Diag.say("not yet on darwin-arm64: a library besides libSystem, {}", [Fmt.str(Scan.nameOf(needed[i]))]));
336+ libs[0] := libc;
337+ nlibs := 1;
338+ libsRoom := roundUp(24 + LEN(libc) + 1, 8);
339+ for i := 0 to numNeeded - 1 do
340+ libs[nlibs] := libPath(Scan.nameOf(needed[i]));
341+ n := 0; (* several names may mean one library: "m" and "libm.so.6" *)
342+ while libs[n] # libs[nlibs] do INC(n); end;
343+ if n = nlibs then (* new *)
344+ libsRoom := libsRoom + roundUp(24 + LEN(libs[nlibs]) + 1, 8);
345+ INC(nlibs);
346+ end;
282347 end;
348+ debug := not stripped and (Link.placesChunk >= 0);
283349 cap := 1 shl 16;
284350 buf := Scan.grab(cap);
285351 len := 0;
286352 Link.setBase(Base);
287353 Link.setPaged(Page);
288- Link.linkAll(HeaderRoom, 0, 0);
354+ Link.linkAll(HeaderRoom + libsRoom, 0, 0);
289355 textSize := roundUp(Link.textOff + Link.textSize, Page); (* __TEXT's, the header in it *)
290- leOff := Link.bssOff; (* the next page in the file: __DATA has no bytes there *)
356+ dwarfOff := Link.bssOff; (* the next page in the file: __DATA has no bytes there *)
291357 leAdr := Link.bssAdr + roundUp(Link.bssSize, Page);
358+ dwarfAdr := leAdr;
292359 (* the segments' numbers, as the opcodes name them, and the
293360 sections', as the symbols do: __PAGEZERO 0, __TEXT 1 (__text, 1) *)
294361 segRo := 2;
@@ -301,6 +368,24 @@
301368 putBytes(Link.textImg, Link.textSize);
302369 padTo(Link.roOff);
303370 putBytes(Link.roImg, Link.roSize);
371+ padTo(dwarfOff);
372+
373+ (* the line numbers: the line table, copied from the places, then the
374+ compile unit and its abbreviation; each section's file offset and size *)
375+ if debug then
376+ dbgOff[0] := len;
377+ dbgSize[0] := Link.linesSize;
378+ putBytes(Link.roImg + (Chunk.chunks[Link.placesChunk].adr - Link.roAdr + Link.linesOff), Link.linesSize);
379+ ident := Link.debugInfo(mainPath);
380+ dbgOff[1] := len; dbgSize[1] := LEN(ident);
381+ putBytes(SYSTEM.PTR(ident), LEN(ident));
382+ ident := Link.debugAbbrev();
383+ dbgOff[2] := len; dbgSize[2] := LEN(ident);
384+ putBytes(SYSTEM.PTR(ident), LEN(ident));
385+ end;
386+ dwarfSize := len - dwarfOff;
387+ leOff := roundUp(len, Page); (* __LINKEDIT, a page of its own *)
388+ leAdr := leAdr + roundUp(dwarfSize, Page); (* in memory, after __DWARF *)
304389 padTo(leOff);
305390
306391 (* rebases: each word in rodata that holds an address, a pointer
@@ -315,10 +400,10 @@
315400 padTo(roundUp(len, 8));
316401 rebaseSize := len - rebaseOff;
317402
318- (* binds: each C symbol's quadword, from library 1, libSystem, by
319- its name, which in Mach-O begins with _ *)
403+ (* binds: each C symbol's quadword, by flat lookup (above), by its
404+ name, which in Mach-O begins with _ *)
320405 bindOff := len;
321- putN(11H, 1); (* BIND_OPCODE_SET_DYLIB_ORDINAL_IMM, 1 *)
406+ putN(3EH, 1); (* BIND_OPCODE_SET_DYLIB_SPECIAL_IMM, -2 *)
322407 putN(51H, 1); (* SET_TYPE_IMM, pointer *)
323408 for c := 0 to Chunk.numChunks - 1 do
324409 if isExt(c) then
@@ -397,6 +482,13 @@
397482 segment("__DATA", Link.bssAdr, roundUp(Link.bssSize, Page), 0, 0, 3, 1, 0); INC(ncmds);
398483 section("__bss", "__DATA", Link.bssAdr, Link.bssSize, 0, 3, 1); (* S_ZEROFILL *)
399484 end;
485+ if debug then
486+ (* read only, flags 0 *)
487+ segment("__DWARF", dwarfAdr, roundUp(dwarfSize, Page), dwarfOff, dwarfSize, 1, 3, 0); INC(ncmds);
488+ section("__debug_line", "__DWARF", dwarfAdr + dbgOff[0] - dwarfOff, dbgSize[0], dbgOff[0], 0, 02000000H); (* S_ATTR_DEBUG *)
489+ section("__debug_info", "__DWARF", dwarfAdr + dbgOff[1] - dwarfOff, dbgSize[1], dbgOff[1], 0, 02000000H);
490+ section("__debug_abbrev", "__DWARF", dwarfAdr + dbgOff[2] - dwarfOff, dbgSize[2], dbgOff[2], 0, 02000000H);
491+ end;
400492 segment("__LINKEDIT", leAdr, roundUp(sigOff + sigRoom - leOff, Page), leOff, sigOff + sigRoom - leOff, 1, 0, 0); INC(ncmds);
401493 putN(LcDyldInfoOnly, 4); putN(48, 4);
402494 putN(rebaseOff, 4); putN(rebaseSize, 4); putN(bindOff, 4); putN(bindSize, 4);
@@ -412,12 +504,20 @@
412504 putN(1, 4); putN(0B0000H, 4); putN(0B0000H, 4); putN(0, 4); (* macOS, 11.0 and up, SDK 11.0, no tools *)
413505 INC(ncmds);
414506 putN(LcMain, 4); putN(24, 4); putN(Chunk.chunks[entry].adr - Base, 8); putN(0, 8); INC(ncmds);
415- (* libSystem, version 1.0.0 and compatible with 1.0.0, which any is *)
416- n := roundUp(24 + LEN(libc) + 1, 8);
417- putN(LcLoadDylib, 4); putN(n, 4); putN(24, 4); putN(2, 4); putN(10000H, 4); putN(10000H, 4);
418- putName(libc, n - 24); INC(ncmds);
507+ (* each library, version 1.0.0 and compatible with 1.0.0, which any is
508+ (dyld checks those of the library's own; the timestamp is 2) *)
509+ for i := 0 to nlibs - 1 do
510+ n := roundUp(24 + LEN(libs[i]) + 1, 8);
511+ putN(LcLoadDylib, 4); putN(n, 4); putN(24, 4); putN(2, 4); putN(10000H, 4); putN(10000H, 4);
512+ putName(libs[i], n - 24); INC(ncmds);
513+ end;
514+ (* where @rpath looks, in order (libPath) *)
515+ for i := 0 to 3 do
516+ n := roundUp(12 + LEN(Rpaths[i]) + 1, 8);
517+ putN(LcRpath, 4); putN(n, 4); putN(12, 4); putName(Rpaths[i], n - 12); INC(ncmds);
518+ end;
419519 putN(LcCodeSignature, 4); putN(16, 4); putN(sigOff, 4); putN(sigRoom, 4); INC(ncmds);
420- if len > HeaderRoom then Diag.fatal("internal error: the Mach-O header outgrew its room"); end;
520+ if len > HeaderRoom + libsRoom then Diag.fatal("internal error: the Mach-O header outgrew its room"); end;
421521 i := len;
422522 len := 16; putN(ncmds, 4); putN(i - 32, 4);
423523 len := end0;
@@ -454,5 +554,6 @@
454554 end;
455555
456556 begin
557+ mainPath := "";
457558 libc := "/usr/lib/libSystem.B.dylib";
458559 end Exe.

src/Exe.linux.raz (history) modified +13 -38

@@ -16,8 +16,8 @@
1616 There is no PLT and no lazy binding, and the program exports nothing.
1717
1818 Depends on: Link for the images and addresses, Chunk for the chunks,
19- Scan for names and memory, Diag for errors, Sys for the working
20- directory, and Host to write the file.
19+ Scan for names and memory, Diag for errors, and Host to write the
20+ file; Link also makes the DWARF's compile unit.
2121 Leaves out: position independent executables, symbol versions, the
2222 rest of DWARF.
2323
@@ -27,7 +27,7 @@
2727 Exported variables can't be set from outside, so needLib, setMusl,
2828 setMainPath and setStripped do it. *)
2929 module Exe;
30-import Sys, Host, Fmt, Diag, Scan, Chunk, Link;
30+import Host, Fmt, Diag, Scan, Chunk, Link;
3131 import SYSTEM;
3232
3333 const
@@ -176,47 +176,22 @@
176176 putN(0, 1);
177177 end;
178178
179-(* LEB128, unsigned *)
180-proc putU(v: INT);
181-begin
182- while v >= 80H do
183- putN(v & 7FH | 80H, 1);
184- v := v shr 7;
185- end;
186- putN(v, 1);
187-end;
188-
189-(* The compile unit, which names the line table, and its abbreviation;
190- the table itself is in the places, in rodata (Link). Section
191- offsets go in off, sizes in size: line, info, abbrev. *)
179+(* The compile unit, which names the line table, and its abbreviation
180+ (Link makes them); the table itself is in the places, in rodata.
181+ Section offsets go in off, sizes in size: line, info, abbrev. *)
192182 proc putDebug(off, size: []INT);
193-var cwd: []const U8; err: INT;
183+var info, abbrev: []const U8;
194184 begin
195185 off[0] := Chunk.chunks[Link.placesChunk].adr - Link.roAdr + Link.roOff + Link.linesOff;
196186 size[0] := Link.linesSize;
187+ info := Link.debugInfo(mainPath);
197188 off[1] := len;
198- putN(0, 4); putN(4, 2); putN(0, 4); putN(8, 1); (* length, version, abbrev, address size *)
199- putU(1);
200- putStr(mainPath);
201- cwd, err := Sys.workDir();
202- if err # 0 then cwd := ""; end;
203- putStr(cwd);
204- putStr("raz");
205- putN(0, 4); (* the line table, at 0 *)
206- putN(Link.base + Link.textOff, 8); putN(Link.textSize, 8); (* the code, all of it *)
207- patch(off[1], len - off[1] - 4, 4);
208- size[1] := len - off[1];
209-
189+ putBytes(SYSTEM.PTR(info), LEN(info));
190+ size[1] := LEN(info);
191+ abbrev := Link.debugAbbrev();
210192 off[2] := len;
211- putU(1); putU(11H); putN(0, 1); (* 1: a compile unit, no children *)
212- putU(03H); putU(08H); (* name, a string *)
213- putU(1BH); putU(08H); (* comp_dir, a string *)
214- putU(25H); putU(08H); (* producer, a string *)
215- putU(10H); putU(17H); (* stmt_list, a section offset *)
216- putU(11H); putU(01H); (* low_pc, an address *)
217- putU(12H); putU(07H); (* high_pc, a length in 8 bytes *)
218- putN(0, 2); putN(0, 1);
219- size[2] := len - off[2];
193+ putBytes(SYSTEM.PTR(abbrev), LEN(abbrev));
194+ size[2] := LEN(abbrev);
220195 end;
221196
222197 const

src/Host.darwin.raz (history) added +105 -0

@@ -0,0 +1,105 @@
1+(* Host: what the compiler asks of the system it runs on, beyond the
2+ library. The host is the system running raz; the target, the one its
3+ program will run on. This file is macOS's, for the compiler running
4+ on a Mac (a cross compiler built for one is staged with it, as
5+ src/cross.sh does); Host.linux.raz says what each procedure is for
6+ and what a host must give, and this one gives the same five.
7+
8+ macOS differs from Linux in four things the compiler asks:
9+ - there is no /proc: the compiler's own path is _NSGetExecutablePath,
10+ which may name it through a link, so realpath follows them;
11+ - there is one C library, libSystem, so cLibrary says glibc, which
12+ here means "the C library the system has", and there is no musl;
13+ - there is no memfd_create: a program raz run starts is written to a
14+ file, in $TMPDIR (/tmp if it is not set), named by the compiler's
15+ process number, and the file stays after the program replaces the
16+ compiler, which macOS clears out of $TMPDIR in time;
17+ - a file's code signature belongs to the file: a program written
18+ in place over one that has run is killed. prepareRun's name is new
19+ each time, and writeExecutable removes a file that is there first.
20+
21+ Depends on: Sys and Files for the system's services, Scan for memory
22+ that lasts until exit, Diag and Fmt for paths.
23+ Leaves out: compiling and the executable format, which are Parse and
24+ Exe's. *)
25+module Host;
26+import Sys, Files, Fmt, Diag, Scan;
27+import SYSTEM;
28+
29+type
30+ CLib* = (none, glibc, musl); (* the C library to link against *)
31+
32+const
33+ Mode755 = 1EDH; (* rwxr-xr-x *)
34+ PathMax = 4096; (* more than macOS's 1024, which realpath needs *)
35+ NoRoom: ERROR = "no writable directory for the program";
36+
37+extern proc _NSGetExecutablePath(buf: ^U8; size: ^U32): I32;
38+extern proc realpath(path, resolved: ^U8): ^U8;
39+
40+(* The directory of the library, ending in what the name of raz leaves,
41+ "lib": the one beside raz as the system says it, links followed, or,
42+ if it won't say, beside argument 0 as it was typed *)
43+proc libraryDir*: []const U8;
44+var raw, real: ^U8; size: U32; path: []const U8; n, i: INT;
45+begin
46+ raw := Scan.grab(PathMax);
47+ real := Scan.grab(PathMax);
48+ size := U32(PathMax);
49+ path := Files.arg(0);
50+ if (_NSGetExecutablePath(raw, SYSTEM.ADR(size)) = 0) and (realpath(raw, real) # nil) then
51+ n := 0;
52+ while real[n] # 0 do INC(n); end;
53+ path := real[0..n];
54+ end;
55+ i := LEN(path);
56+ while (i > 0) and (path[i - 1] # '/') do DEC(i); end;
57+ if i = 0 then return "lib"; end;
58+ return Diag.say("{}lib", [Fmt.str(path[0..i])]);
59+end;
60+
61+(* The C library a program is linked against: the Mac's one, libSystem *)
62+proc cLibrary*: CLib;
63+begin
64+ return glibc;
65+end;
66+
67+(* The file at path made to hold data, an executable: any file there
68+ removed first (see above), then written and given mode 755. False if
69+ it failed. *)
70+proc writeExecutable*(path, data: []const U8): BOOL;
71+begin
72+ _ := Sys.remove(path);
73+ return Files.write(path, data) and (Sys.chmod(path, Mode755) >= 0);
74+end;
75+
76+(* The path raz run writes its program to: a file in $TMPDIR, if that
77+ is a directory the compiler may write to *)
78+proc prepareRun*: ([]const U8, ERROR);
79+var dir: []const U8;
80+begin
81+ dir := Sys.env("TMPDIR");
82+ if LEN(dir) = 0 then dir := "/tmp"; end;
83+ if dir[LEN(dir) - 1] = '/' then dir := dir[0..LEN(dir) - 1]; end;
84+ if Sys.access(dir, {Sys.writing}) # 0 then return "", NoRoom; end;
85+ return Diag.say("{}/raz-run-{}", [Fmt.str(dir), Fmt.int(Sys.pid())]), nil;
86+end;
87+
88+(* The program at path runs in the compiler's place, with the compiler's
89+ arguments from number first on as its own, first as its argument 0,
90+ and the environment. It returns only if that failed: minus the errno. *)
91+proc execute*(path: []const U8; first: INT): INT;
92+var args, env: ^[]const U8; n, m, i: INT; more: BOOL;
93+begin
94+ n := Files.argCount() - first;
95+ args := ^[]const U8(Scan.grab((n + 1) * SIZE([]const U8)));
96+ for i := 0 to n - 1 do args[i] := Files.arg(first + i); end;
97+ m := 0;
98+ _, more := Sys.envAt(0);
99+ while more do INC(m); _, more := Sys.envAt(m); end;
100+ env := ^[]const U8(Scan.grab((m + 1) * SIZE([]const U8)));
101+ for i := 0 to m - 1 do env[i], _ := Sys.envAt(i); end;
102+ return Sys.execve(path, args[0..n], env[0..m]);
103+end;
104+
105+end Host.

src/Link.raz (history) modified +52 -2

@@ -19,11 +19,14 @@
1919 loader to move with it. arm64's fixups, fields of an instruction,
2020 are patched here too.
2121
22+ Also here, shared by both executable formats, the DWARF the line table
23+ needs (debugInfo, debugAbbrev): the one compile unit that points to it.
24+
2225 Depends on: Chunk for chunks and fixups, Scan for names and memory,
23- Diag for errors and paths.
26+ Diag for errors and paths, Sys for the working directory.
2427 Leaves out: the file format; that is Exe. *)
2528 module Link;
26-import Fmt, Diag, Scan, Chunk;
29+import Sys, Fmt, Diag, Scan, Chunk;
2730 import SYSTEM;
2831
2932 const
@@ -339,6 +342,53 @@
339342 patch8(buf, 24, np);
340343 patch8(buf, 32, nv);
341344 Chunk.setData(placesChunk, buf, names + lpLen, 8);
345+end;
346+
347+(* The compile unit that names the line table, and its abbreviation, DWARF
348+ 4's .debug_info and .debug_abbrev: the main module's file and the
349+ directory it was compiled in, the producer, the line table (at 0 of its
350+ section), and the code, all of it. No types and no variables. Each is
351+ made in lp, which the line table has left, and returned: copy it
352+ before the next call. The address unit of the line table is a byte
353+ (minimum_instruction_length 1, in makeLines), so its rows hold, and
354+ its advances count, bytes, on arm64 too, where every row is a multiple
355+ of 4: DWARF takes that, and no row needs dividing. *)
356+proc putStr(s: []const U8);
357+var i: INT;
358+begin
359+ for i := 0 to LEN(s) - 1 do putB(INT(s[i])); end;
360+ putB(0);
361+end;
362+
363+proc debugInfo*(name: []const U8): []const U8;
364+var cwd: []const U8; err: INT;
365+begin
366+ lpLen := 0;
367+ putN(0, 4); putN(4, 2); putN(0, 4); putN(8, 1); (* length, version, abbrev, address size *)
368+ putU(1); (* abbreviation 1 *)
369+ putStr(name);
370+ cwd, err := Sys.workDir();
371+ if err # 0 then cwd := ""; end;
372+ putStr(cwd);
373+ putStr("raz");
374+ putN(0, 4); (* the line table, at 0 *)
375+ putN(base + textOff, 8); putN(textSize, 8); (* the code, all of it *)
376+ patch4(lp, 0, lpLen - 4);
377+ return lp[0..lpLen];
378+end;
379+
380+proc debugAbbrev*: []const U8;
381+begin
382+ lpLen := 0;
383+ putU(1); putU(11H); putB(0); (* 1: a compile unit, no children *)
384+ putU(03H); putU(08H); (* name, a string *)
385+ putU(1BH); putU(08H); (* comp_dir, a string *)
386+ putU(25H); putU(08H); (* producer, a string *)
387+ putU(10H); putU(17H); (* stmt_list, a section offset *)
388+ putU(11H); putU(01H); (* low_pc, an address *)
389+ putU(12H); putU(07H); (* high_pc, a length in 8 bytes *)
390+ putN(0, 2); putB(0);
391+ return lp[0..lpLen];
342392 end;
343393
344394 (* The address a as raz writes it, in hexadecimal: 0FFFFFFFF80000000H *)

src/cross.sh (history) modified +14 -4

@@ -4,7 +4,14 @@
44 # is whatever files the compiler was built from (doc/raz.md, 3).
55 #
66 # src/cross.sh TARGET [OUT] run from the repository root
7+# HOST=darwin src/cross.sh darwin-arm64 [OUT]
78 #
9+# The second makes the compiler that runs on a Mac and compiles for it,
10+# the first Mac compiler (PLAN.md 5, step 9): the stage has the host's
11+# own Host.darwin.raz, and it is built by the cross compiler of the
12+# first form, which must be there, since BOOT's makes programs for this
13+# system, and a stage for another host can't be run here to be built.
14+#
815 # Stage build/cross/TARGET/src/ holds src/'s generic files and, under
916 # the plain names, the target's own: Gen.ARCH.raz as Gen.raz and
1017 # Exe.OS.raz as Exe.raz; and the host's, Host.OS.raz as Host.raz, the
@@ -12,18 +19,18 @@
1219 # With only plain names there, module lookup has one choice each. BOOT's
1320 # release builds the stage as the Makefile builds raz, from a directory
1421 # where src/Raz.raz is again at src/, so the paths a program holds are
15-# the same. The compiler is OUT, build/cross/TARGET/raz if not said;
22+# the same. The compiler is OUT, build/cross/TARGET/raz (or TARGET-on-HOST) if not said;
1623 # its library is lib/, beside it.
1724 set -e
1825 target=$1
19-out=${2:-build/cross/$target/raz}
2026 case $target in
2127 *-*) ;;
2228 *) echo "usage: src/cross.sh OS-ARCH [OUT]" >&2; exit 2;;
2329 esac
2430 os=${target%%-*}
2531 arch=${target#*-}
26-host=$(uname -s | tr A-Z a-z) # the OS that runs the compiler
32+here=$(uname -s | tr A-Z a-z)
33+host=${HOST:-$here} # the OS that runs the compiler
2734
2835 # Each unit with files by target, and the file this build takes of it
2936 missing=
@@ -38,6 +45,8 @@
3845 [ -x build/boot/raz ] || make build/boot/raz
3946
4047 d=build/cross/$target
48+[ $host != $here ] && d=$d-on-$host
49+out=${2:-$d/raz}
4150 rm -rf $d
4251 mkdir -p $d/src $d/v
4352 for f in src/*.raz; do
@@ -52,8 +61,9 @@
5261 cp build/src/Version.raz $d/v/
5362 ln -s ../../../lib $d/lib
5463 boot=$PWD/build/boot/raz
64+[ $host != $here ] && boot=$PWD/build/cross/$target/raz # the cross compiler, run here
5565 (cd $d && $boot -Werror -I src -I v -o raz src/Raz.raz)
5666 case $out in
57-build/cross/$target/raz) ;;
67+$d/raz) ;;
5868 *) cp $d/raz "$out";;
5969 esac

test/compiler-host.sh (history) modified +1 -1

@@ -31,7 +31,7 @@
3131 mkdir "$top/src" "$top/work"
3232 cp src/*.raz "$top/src"
3333 # staged as src/cross.sh does: the plain name takes the place of Linux's
34-rm "$top/src/Host.linux.raz"
34+rm "$top"/src/Host.*.raz
3535 cp test/host/Host.raz "$top/src/Host.raz"
3636 "$raz" -Werror -I "$top/src" -I build/src -o "$top/compiler" "$top/src/Raz.raz" 2>"$top/build.err" || {
3737 cat "$top/build.err"

test/lldb.sh (history) added (executable) +17 -0

@@ -0,0 +1,17 @@
1+#!/bin/sh
2+# Line numbers for lldb, as gdb.sh's for gdb, on a Mac: stops a test
3+# program at a line of its source, by file and line, and checks that
4+# lldb names it, and the line of each caller. The program is conform's
5+# Procs, which test/mac.sh build made for darwin-arm64 in build/mac.
6+prog=build/mac/conform/Procs
7+out=build/mac
8+if [ ! -x $prog ]; then echo "FAIL lldb: no $prog; make mac-build first"; exit 1; fi
9+lldb --batch -o 'b Procs.raz:13' -o run -o bt $prog > $out/lldb.out 2>&1
10+for want in 'Procs.odd at Procs.raz:13' \
11+ 'Procs.even at Procs.raz:20' \
12+ 'Procs.main at Procs.raz:'; do
13+ if ! grep -qF "$want" $out/lldb.out; then
14+ echo "FAIL lldb: no \"$want\""; cat $out/lldb.out; exit 1
15+ fi
16+done
17+echo "lldb finds the lines"

test/mac.sh (history) modified +15 -0

@@ -15,6 +15,12 @@
1515 # test/mac.sh build in the container: the cross compiler, then the programs
1616 # test/mac.sh run on the Mac: each program, run with a timeout
1717 #
18+# A test with a C file, test/mac/X.c, links the library that file makes:
19+# run builds it by cc into build/mac/libX.dylib, where the program finds
20+# it by @executable_path (Exe.darwin's libPath), and, built as a program
21+# with -DMAIN, it must print what X.expect holds, as C itself says it.
22+# Then lldb, if there is one, must find a line (test/lldb.sh).
23+#
1824 # build/mac is made anew each time: macOS keeps a file's code signature
1925 # while the file is open or cached, and kills a program whose file was
2026 # written again in place with another one.
@@ -41,6 +47,14 @@
4147 run)
4248 pass=0
4349 fail=0
50+ for f in test/mac/*.c; do
51+ name=$(basename $f .c)
52+ cc -dynamiclib -o $out/lib$name.dylib $f
53+ cc -DMAIN -o $out/$name.cmain $f
54+ if ! $out/$name.cmain | cmp -s - test/mac/$name.expect; then
55+ echo "FAIL $f: C's own output differs from $name.expect"; fail=$((fail + 1))
56+ fi
57+ done
4458 for f in test/mac/*.raz; do
4559 name=$(basename $f .raz)
4660 want=0
@@ -93,6 +107,7 @@
93107 fi
94108 done
95109 echo "conform/run: $cpass of $((cpass + cfail)) passed, $cskip not for this target; the others in $c.fail"
110+ if command -v lldb > /dev/null; then test/lldb.sh || fail=$((fail + 1)); fi
96111 [ $fail = 0 ]
97112 ;;
98113 *)

test/mac/NarrowArgs.c (history) added +69 -0

@@ -0,0 +1,69 @@
1+/* NarrowArgs's C side (test/mac.sh): built by cc into a library, which
2+ the raz program links; with -DMAIN, built as a program that prints
3+ what NarrowArgs.expect holds, so that C itself says what is right. */
4+#include <stdarg.h>
5+#include <stdint.h>
6+#include <stdio.h>
7+
8+/* A running hash: each value changes every digit after it */
9+static int64_t mix(int64_t h, int64_t v) { return (int64_t)((uint64_t)h * 1000003u + (uint64_t)v); }
10+
11+/* Eight integers in x0 to x7; then, on the stack, packed by size as
12+ Apple's convention has it, i at 0, j at 1, k at 2, l at 4, m at 8, n
13+ at 16, y at 24, z at 32 and last at 40; the reals p to w in v0 to v7 */
14+int64_t narrow(int8_t a, uint8_t b, int16_t c, int32_t d, int64_t e, uint8_t f, int16_t g, int32_t h,
15+ int8_t i, uint8_t j, int16_t k, int32_t l, int8_t m, int64_t n,
16+ float p, double q, float r, double s, float t, double u, float v, double w,
17+ float y, double z, int8_t last) {
18+ int64_t x = 7;
19+ x = mix(x, a); x = mix(x, b); x = mix(x, c); x = mix(x, d);
20+ x = mix(x, e); x = mix(x, f); x = mix(x, g); x = mix(x, h);
21+ x = mix(x, i); x = mix(x, j); x = mix(x, k); x = mix(x, l);
22+ x = mix(x, m); x = mix(x, n);
23+ x = mix(x, (int64_t)(p * 4)); x = mix(x, (int64_t)(q * 4));
24+ x = mix(x, (int64_t)(r * 4)); x = mix(x, (int64_t)(s * 4));
25+ x = mix(x, (int64_t)(t * 4)); x = mix(x, (int64_t)(u * 4));
26+ x = mix(x, (int64_t)(v * 4)); x = mix(x, (int64_t)(w * 4));
27+ x = mix(x, (int64_t)(y * 4)); x = mix(x, (int64_t)(z * 4));
28+ x = mix(x, last);
29+ return x;
30+}
31+
32+/* A variadic procedure: its arguments past n all go on the stack, each 8 bytes */
33+int64_t varsum(int n, ...) {
34+ va_list ap;
35+ int64_t x = 7;
36+ int k;
37+ va_start(ap, n);
38+ for (k = 0; k < n; k++) {
39+ if (k % 2 == 0) x = mix(x, va_arg(ap, int)); else x = mix(x, (int64_t)(va_arg(ap, double) * 4));
40+ }
41+ va_end(ap);
42+ return x;
43+}
44+
45+/* C calls back a procedure of ten integers, the last two on the stack */
46+int64_t callback(int64_t (*cb)(int64_t, int64_t, int64_t, int64_t, int64_t,
47+ int64_t, int64_t, int64_t, int64_t, int64_t)) {
48+ return cb(1, -2, 3, -4, 5, -6, 7, -8, 9, -10);
49+}
50+
51+#ifdef MAIN
52+static int64_t mine(int64_t a, int64_t b, int64_t c, int64_t d, int64_t e,
53+ int64_t f, int64_t g, int64_t h, int64_t i, int64_t j) {
54+ int64_t x = 7;
55+ x = mix(x, a); x = mix(x, b); x = mix(x, c); x = mix(x, d); x = mix(x, e);
56+ x = mix(x, f); x = mix(x, g); x = mix(x, h); x = mix(x, i); x = mix(x, j);
57+ return x;
58+}
59+
60+int main(void) {
61+ printf("%lld\n", (long long)narrow(-1, 200, -300, -70000, -5000000000LL, 255, -32768, 2147483647,
62+ -128, 129, -2, -100000, 127, 9000000000LL,
63+ 1.5f, -2.25, 3.5f, 4.75, -5.5f, 6.25, 7.5f, -8.75,
64+ 9.5f, 10.25, -100));
65+ printf("%lld\n", (long long)varsum(5, 1, 1.5, -3, -2.25, 5));
66+ printf("%lld\n", (long long)callback(mine));
67+ return 0;
68+}
69+#endif

test/mac/NarrowArgs.expect (history) added +3 -0

@@ -0,0 +1,3 @@
1+-4734827987094619837
2+-7950848964622138137
3+9213667167917808028

test/mac/NarrowArgs.raz (history) added +48 -0

@@ -0,0 +1,48 @@
1+(* Calling C with arguments of every width past the registers (PLAN.md 5,
2+ step 6): Apple's convention packs a C procedure's own arguments on the
3+ stack by their natural size, where a raz procedure's take 8 bytes
4+ each. narrow has eight integers in x0 to x7, six more on the stack
5+ (an I8, a U8, an I16, an I32, an I8, an I64), eight reals in v0 to v7
6+ and an F32, an F64 and an I8 past them. varsum is variadic, whose
7+ arguments past its own go on the stack, 8 bytes each. callback has C
8+ call a raz procedure of ten integers. Each answer is a hash C makes of
9+ what it received; NarrowArgs.c, built as a program, prints the same. *)
10+module NarrowArgs;
11+import Out;
12+import SYSTEM;
13+link "NarrowArgs";
14+
15+extern proc narrow(a: I8; b: U8; c: I16; d: I32; e: I64; f: U8; g: I16; h: I32;
16+ i: I8; j: U8; k: I16; l: I32; m: I8; n: I64;
17+ p: F32; q: F64; r: F32; s: F64; t: F32; u: F64; v: F32; w: F64;
18+ y: F32; z: F64; last: I8): I64;
19+extern proc varsum(n: I32; ...): I64;
20+extern proc callback(cb: proc(a, b, c, d, e, f, g, h, i, j: I64): I64): I64;
21+
22+(* the same hash as C's mix *)
23+proc mix(h, v: I64): I64;
24+begin
25+ return I64(U64(h) * 1000003 + U64(v));
26+end;
27+
28+proc mine(a, b, c, d, e, f, g, h, i, j: I64): I64;
29+var x: I64;
30+begin
31+ x := 7;
32+ x := mix(x, a); x := mix(x, b); x := mix(x, c); x := mix(x, d); x := mix(x, e);
33+ x := mix(x, f); x := mix(x, g); x := mix(x, h); x := mix(x, i); x := mix(x, j);
34+ return x;
35+end;
36+
37+proc main*;
38+begin
39+ Out.int(narrow(-1, 200, -300, -70000, -5000000000, 255, -32768, 2147483647,
40+ -128, 129, -2, -100000, 127, 9000000000,
41+ 1.5, -2.25, 3.5, 4.75, -5.5, 6.25, 7.5, -8.75,
42+ 9.5, 10.25, -100)); Out.ln;
43+ Out.int(varsum(5, 1, 1.5, -3, -2.25, 5)); Out.ln;
44+ Out.int(callback(mine)); Out.ln;
45+ Out.flush;
46+end;
47+
48+end NarrowArgs.