From 39edff85dacb63f9eeaa95390c5e8c113f7c6d68 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Sat, 10 Oct 2026 12:19:44 +0000 Subject: [PATCH] Step 17: README.md and doc/compiler.md README.md: what jed is, building, the checks, using the compiler, the language in brief, the library, the tools and editors, versions, and what it leaves out. doc/compiler.md: a guide to reading the compiler file by file, in the order to read it: the shape of it (one pass, items, a stack machine through rax, chunks and fixups, tree shaking), one program followed from the command line to the file, a procedure's code shown instruction by instruction, each unit's data and the routines to read first, and where to start changing it. --- diff --git a/CLAUDE.md b/CLAUDE.md index fe710d9..6bfee23 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -44,8 +44,10 @@ ## Where things stand -Stages 1 to 4 are done (steps 1-16), waiting for review. Next is step -17, `README.md` and `doc/compiler.md`, on the owner's go-ahead. Left +Steps 1-17 are done: the language, the tools, `README.md` and the +guide to the compiler, `doc/compiler.md`. Next, only on the owner's +word, is step 18, the formatter. Keep `doc/compiler.md`'s table of +lines and its routine names true when the compiler changes. Left for later at the owner's word: tagging versions, and a rewrite of var parameters. diff --git a/PLAN.md b/PLAN.md index 4a49d3c..a4f3918 100644 --- a/PLAN.md +++ b/PLAN.md @@ -158,7 +158,7 @@ Wrap up -- [ ] 17. `README.md` and `doc/compiler.md`, a guide to reading the +- [x] 17. `README.md` and `doc/compiler.md`, a guide to reading the compiler file by file. Later diff --git a/README.md b/README.md new file mode 100644 index 0000000..6f40265 --- /dev/null +++ b/README.md @@ -0,0 +1,132 @@ +# jed + +jed (Polish *jeden*, one) is a small systems language in Wirth's +tradition, for x86-64 Linux. A jed program can lay a record over a +device's registers, read and write memory exactly as written, take part +in lock-free algorithms, make system calls and call C libraries. It has +no garbage collector and no hidden run time: what the program does is +what its text says. + +``` +module Hello; +import Out; + +proc main*; +begin + Out.str("hello, world\n"); +end; + +end Hello. +``` + + $ make + $ ./jed examples/Hello.jed + $ examples/Hello + hello, world + +The compiler is under 5,000 lines of Free Pascal: one pass, no +intermediate form, no optimizer. It writes x86-64 machine code and the +ELF executable itself, with no assembler or linker, and compiles about +a million lines a second. + +## What's here + +| Path | What | +|---|---| +| `doc/jed.md` | The language report: the definition of jed | +| `doc/compiler.md` | A guide to reading the compiler, file by file | +| `src/` | The compiler, in Free Pascal | +| `lib/` | The library, in jed; the compiler finds it beside itself | +| `examples/` | Small programs, each with the output it should give | +| `tools/Doc.jed` | Prints what a module exports, with its comments | +| `editor/` | Support for vim and emacs | +| `test/` | Tests, and the scripts that run them | +| `PLAN.md` | How the compiler was built, step by step | + +## Building + +The compiler needs Free Pascal 3.2.2 (`apt-get install fp-compiler`) +and builds into the repository's root: + + make # ./jed + +The tests need a shell and Python 3; the editor checks run if vim or +emacs is installed. + + make check # every example and test, against what it should give + make bench # the compiler's speed on a program of 350,000 lines + make stress # 40,000 threads, started and joined + make gdb # gdb stops at a line of jed and names it + make loc # the compiler's lines + make tests # tests of the scanner, encoder and linker on their own + +## Using it + + jed [-I dir]... [-o file] Main.jed + jed --version + +`jed Main.jed` compiles module `Main` and every module it imports, and +writes the executable `Main`. A module `Name` lives in `Name.jed`, found +in the main module's directory, then in each `-I` directory, then in +`lib`. A module whose code depends on the system comes in one file per +target, `Name.linux-x86_64.jed` before `Name.jed`; the compiler checks +that all of them export the same things. + +A program that calls no C is a static executable of a few kilobytes, +holding only the code it reaches. One that declares an `extern` +procedure is linked dynamically against the C library, and starts +through it. Either way, gdb finds its procedures by name and its lines +by number. + +## The language, briefly + +- Integers of 8 to 64 bits, signed and unsigned, which wrap; `BOOL`, + `F32`, `F64`. No conversion happens unless it is written. +- Arrays, slices (`[]T`, checked), pointers (unchecked), records laid + out as C lays them out, packed records, variants with `match`, + enumerations and sets, procedure types. +- `^VOLATILE T`: every access through it is one instruction of its + size, in the order written. `CAS`, `XCHG`, `XADD`, `FENCE`. +- Several results, `ERROR` values compared by identity, `defer`. +- Modules with bodies; generic modules with type and procedure + arguments, `Map(K, V; hash, eq)`. +- Every variable starts at zero, and behaviour is defined everywhere + but through pointers. +- `extern` procedures and variables from C; code procedures written as + bytes of machine code; `SYSCALL`. + +The report, `doc/jed.md`, says all of it in about 900 lines. + +## The library + +`Rt` (ending the program, checks), `Sys` (system calls), `Out` +(buffered output), `C`, `Lock`, `Thread` (threads without C), `Mem`, +`Heap`, `Arena`, `Str`, `Buf`, `Fmt`, `List(T)` and `Map(K, V; hash, +eq)`. `tools/Doc.jed` prints any of them: + + $ ./jed -o Doc tools/Doc.jed && ./Doc lib/Str.jed + +Each library module's `main` is its test, which `make check` runs. + +## Editors + +`editor/vim` holds a syntax file, an indent file and file type +detection: add it to `runtimepath`. `editor/emacs/jed-mode.el` is a +major mode: put it on `load-path` and `(require 'jed-mode)`. Both +colour the language and indent it as the examples are indented. + +## Versions + +The compiler says which version of the report it follows: + + $ ./jed --version + jed 0.3.0, report 2026-10.0 + +A version of the report is named by its month and a count; the report's +last section says what each version changed. + +## Not here + +Other systems and processors (Mach-O, PE, aarch64), an optimizer, +separate compilation, records passed to C by value, thread-local +variables, and type information for debuggers. diff --git a/doc/compiler.md b/doc/compiler.md new file mode 100644 index 0000000..9a5642a --- /dev/null +++ b/doc/compiler.md @@ -0,0 +1,351 @@ +# Reading the jed compiler + +The compiler is eight files of Free Pascal, under 5,000 lines of code. +This guide walks through them in the order they are best read: from the +characters of a source file to the bytes of an executable. It assumes +you know the language from `doc/jed.md`, and enough x86-64 to read a +disassembly. + +| File | Lines | Code | What | +|---|---|---|---| +| `src/scan.pas` | 548 | 471 | Symbols, literals, comments, error messages | +| `src/sym.pas` | 508 | 422 | Types, objects, scopes, layout | +| `src/x64.pas` | 680 | 546 | Code buffer, chunks, fixups, line notes, instruction encoder | +| `src/gen.pas` | 1,414 | 1,240 | Items: loading, storing, copying, frames, calls | +| `src/parse.pas` | 1,816 | 1,685 | Declarations, statements, expressions, built-ins | +| `src/link.pas` | 131 | 95 | Tree shaking, layout, patching | +| `src/elf.pas` | 425 | 351 | Static and dynamic ELF, symbol table, line numbers | +| `src/jed.pas` | 191 | 151 | The command line, the entry points | + +Every file opens with a comment that says what it is, what it depends +on and what it leaves out. Read those first; they are the map. + +## The shape of it + +There is one pass and no tree. The parser reads a construct and, in +the same breath, has the code generator emit its machine code. Nothing +is kept to be looked at again, except the declarations, which live in +the symbol table for as long as the compiler runs. + +The code generator is a stack machine through `rax`. An expression's +value is computed into `rax`; when a second operand has to be computed, +the first is pushed, and popped into `rcx` when both are ready. No +register is ever allocated, so there is no register allocator, and +every access the program writes is one instruction, in the order +written. That is also why `VOLATILE` costs the compiler nothing: it +never does anything clever to begin with. + +The output is a set of *chunks*: one for each procedure, each module +variable, each string. Code refers to other chunks through *fixups*, +which aren't resolved until the end. When everything is compiled, the +linker marks the chunks the entry point can reach, gives each an +address, patches the fixups, and the ELF writer puts them in a file. +Whatever nothing reaches never makes it into the executable. + +All modules are compiled in one run, from source: there are no object +files, no symbol files, and nothing to keep up to date. + +## Following one program + +`jed Hello.jed` does this: + +1. `jed.pas` reads the command line and calls `CompileMain` + (`parse.pas`). +2. `CompileMain` imports `Rt` first, since every program needs it, and + then compiles `Hello.jed` with `CompileFile`. +3. `CompileFile` opens the file in the scanner and parses the module. + At `import Out;` it calls `ImportModule`, which finds `Out.jed` and + compiles it there and then, saving and restoring the scanner and the + scopes around it. So modules are finished in an order where each + comes after those it imports; that is the order their bodies run. +4. Each procedure becomes a chunk of code; `Out.str("hello, world\n")` + becomes a string chunk, a call, and a fixup to `Out.str`'s chunk. +5. Back in `jed.pas`, `Use` marks the modules the program uses; the + entry point, `_start`, is written as one more chunk: save `argc` and + `argv`, call every used module's body, call `main`, call `Rt.exit`. +6. `WriteElf` (`elf.pas`) has `link.pas` mark what `_start` reaches, + place it and patch it, then writes headers, code, data, symbols and + line numbers. + +## A procedure, compiled + +``` +proc scale(a, b: I64): I64; +begin + return a + b * 2; +end; +``` + +comes out as this (from `objdump -d -M intel`): + +``` +push rbp ; EnterBody: the frame +mov rbp,rsp +sub rsp,0x10 ; its size, patched in by EndProc +mov r10,0xfffffffffffffffe ; zero the frame, a quadword at a +xor eax,eax ; time, r10 counting up to zero +mov QWORD PTR [rbp+r10*8+0x0],rax +add r10,0x1 +jne +mov QWORD PTR [rbp-0x8],rdi ; the parameters, into their slots +mov QWORD PTR [rbp-0x10],rsi +mov rax,QWORD PTR [rbp-0x8] ; a +push rax ; kept while b * 2 is computed +mov rax,QWORD PTR [rbp-0x10] ; b +push rax ; kept while 2 is read +pop rax +mov ecx,0x2 ; the constant goes straight to rcx +imul rax,rcx +mov rcx,rax +pop rax ; a again +add rax,rcx +jmp ; return +xor eax,eax ; running off the end returns zero +movq xmm0,rax +leave +ret +``` + +Every line of it can be traced to a routine in `gen.pas`; the pushes +and pops are `Op1` and `Op2`, the frame `EnterBody` and `EndProc`. It +is not fast code. It is code whose every instruction you can account +for, which is the point. + +## scan.pas: the scanner + +The scanner turns bytes into symbols (`TSym`). The current symbol is in +`tok`, with its value in `ival` (integers and characters), `rval` +(reals), `id` (identifiers) or `sbuf` and `slen` (strings, already +decoded). + +Read `Next` first: it skips white space and comments (`Comment`, which +counts nesting), then dispatches on the first character. `Number` +handles decimal, `0FFH`-style hexadecimal and reals; `LitChar` handles +one character of a literal, escapes included. + +Identifiers are *interned*: `Intern` puts each distinct name once in a +pool and hands back a small integer, so every later comparison of names +is a comparison of integers. The reserved words are interned first, in +the order of `TSym`, so a name's number tells at once whether it is a +reserved word. + +Source is UTF-8, checked by `Utf8` wherever a byte above 127 appears: +in strings, characters and comments. Anywhere else it is an error. + +Errors go through `Mark`, which prints `file:line:column` (the column +counted in characters, by `Column`) and stops. There is no recovery: +the first error ends the compilation. `SaveScan` and `RestoreScan` keep +the scanner's state while an imported module is compiled. + +## sym.pas: types, objects and scopes + +Two records hold everything the compiler knows about a program. + +`TType` is a type. Its `form` says what kind (`fInt`, `fPtr`, `fArray`, +`fRecord`, `fVariant`, `fProc`, and so on), and the other fields mean +what that form needs: `base` is what a pointer points at, an array or +slice holds, or a procedure returns; `fields` lists a record's fields, +a variant's cases, a procedure's parameters; `len` counts an array's +elements. `size` and `align` are worked out when the type is built, by +the C rules. `obj` is the declaration that named the type, if any. + +`TObj` is anything a name can stand for: a constant, type, variable, +procedure, module, field, variant case or built-in (`cls`). Where it +lives is decided here too: a module variable's chunk, a local's frame +offset in `val`, a procedure's chunk. + +Scopes are numbers. The universe is 0; each module, procedure, record +and match arm gets a new one from `NewScope`. All objects of all scopes +sit in one open-addressed hash table, keyed by scope and name: `Find` +looks in one scope, `Lookup` in each open scope from the innermost out. +A lookup costs the same however big a program grows, which matters at +a million lines a second. + +`Same` and `Alike` decide whether two types are the same: one +declaration for named types, built alike otherwise. `Fingerprint` +hashes what a declaration exports, for the check that a module's files +for different targets agree. The universe, the basic types and built-in +procedures, is set up in the unit's `initialization`. + +## x64.pas: machine code + +Code is written into one growing buffer, `code`, with `pc` its length; +strings go to `rodata`. A chunk (`TChunk`) records its section, where +its bytes start, how many, and the fixups it makes. `BeginChunk` and +`EndChunk` bracket a procedure; `SetData` and `SetBss` make data +chunks. + +A *fixup* (`TFixup`) is a 32-bit field in the code that will hold the +distance to another chunk: a call's target, or a rip-relative address. +`AddFixup` records one; `link.pas` fills them in. + +Jumps within a procedure don't wait for the linker. A label not yet +placed is a chain of the jumps to it, each jump's 32-bit field holding +the position of the one before (`Jmp`, `Jcc`); `FixLink` walks the +chain and patches each once the label's position is known. This is the +trick from Wirth's compilers, and it means a label is one integer. + +The encoder is `OpM` and `OpR`: a prefix, the REX byte, the opcode, the +ModRM and SIB bytes and the displacement, for a register and a memory +operand (`TMem`) or two registers. Everything else (`MovRR`, `Load`, +`Store`, `Alu`, `Sse`, ...) is a line or two that calls them with the +right opcode. Only the instructions the code generator uses are here. +`test/enctest.pas` emits one of each for `objdump` to check. + +Two more things live here because they are tied to code positions: +`NoteLine` records which source line the code from `pc` on belongs to, +for gdb; `ExternChunk` makes, once per C symbol, the quadword the +dynamic loader will fill with that symbol's address. + +## gen.pas: the code generator + +This is the heart, and the file to read slowly. + +An operand is an *item* (`TItem`), as in Wirth's compilers. Its `mode` +says what it is now: + +- `mConst`: a value known to the compiler, in `val`; +- `mVar`: a variable at an address the code can name: relative to + `rbp` (`bFrame`), to a chunk (`bGlobal`), to a register (`bReg`), to + an address pushed on the stack (`bStack`), or kept in a frame slot + (`bSlot`); plus `off`; +- `mReg`: a value in `rax`; `mStack`: a value pushed on the stack; +- `mProc`, `mType`, `mStr`, and a few more the parser needs. + +Three routines move items towards the machine. `Value` brings a value +into `rax`, loading it with the one instruction its size needs. +`Address` brings a variable's address into a register. `Save` pushes +whatever an item has in a register, so that another expression can be +computed; the item remembers that it's on the stack. Read these three, +then `MemOf`, which turns an `mVar` into an x64 memory operand, popping +its address first if it was pushed. + +Expressions: `Op1` is called when the parser has seen an operator and +its left operand, `Op2` when it has the right one. `Op1` saves the left +operand (or, for `and` and `or`, emits the test that skips the right +one). `Op2` checks the types, folds constants (`FoldInt`, `FoldReal`), +and otherwise puts the left operand in `rax`, the right in `rcx`, and +emits the operation. Narrow integers are brought back to their width +after each operation (`Norm`), which is how `U8` arithmetic wraps. + +Variables: `Deref`, `Field`, `Index` and `Slice` turn an item for a +variable into an item for part of it, mostly by adding to `off`. +`Index` and `Slice` emit the bounds checks; `Check` jumps over a call +to `Rt.fail` with the file and line when the check holds. + +Assignment and conversion: `Coerce` makes a value fit the type it's +going to, or reports that it can't; it's where a string literal becomes +a slice, an array a slice, an untyped constant a typed one. `Assign` +stores, copying records and arrays with `rep movsb`. `Convert` is the +written conversion, `T(x)`. + +Calls: `BeginCall`, then `PassArg` for each argument, then `EndCall`. +Each argument is computed into a frame slot of its own, left to right; +only in `EndCall` are they loaded into the registers System V wants, +and the stack aligned for the call. A record passed by value goes as +the address of a copy; one returned comes back through a hidden pointer +in `rdi`, as C's structs do, and so do several results. + +Frames: `BeginProc` gives the parameters their slots; `EnterBody` +writes the prologue, including the loop that zeroes the frame; +`EndProc` writes the epilogue and patches the frame's size in. Within +a statement, scratch space comes from `AllocTemp` and is given back by +`ReleaseTemps`; `AllocPerm` reserves space for the whole procedure. + +## parse.pas: the parser + +A recursive descent parser, one routine for each rule of the grammar in +the report, calling `gen.pas` as it goes. + +Start with expressions, bottom up: `Factor`, `Term`, `SimpleExpr`, +`Expression`. Then `Designator`, which reads a name and what follows +it: `.field`, `^`, `[i]`, `[lo..hi]`, a call, a conversion. Each step +calls the matching routine in `gen.pas` on the item it has. The +`want` parameter carries the type an expression must have, where it is +known, so that `[1, 2, 3]`, `{x: 1}` and `{red}` know what to build. + +Then statements: `Statement` dispatches; `IfStatement`, +`WhileStatement`, `ForStatement` and `MatchStatement` emit their jumps +with `x64.pas`'s labels. `Defer` is worth reading: a deferred statement +is compiled where it stands, as a little subroutine of the procedure +that the code jumps over, and `RunDefers` emits a call to it at every +way out of its statement list: the end, `return` and `break`. + +Then declarations: `Declarations` reads `const`, `type`, `var`, `proc` +and `extern`. `ProcDecl` handles forward declarations, code procedures +(`CodeBody`, which just copies the bytes) and bodies (`Body`). +`ParseType` and its helpers (`RecordType`, `VariantType`, `Signature`, +`EnumType`) build `TType`s. + +Last, modules, at the end of the file. `CompileFile` reads one module. +`ImportModule` finds a module's file, trying the target's names first, +and compiles it if it isn't yet; `CheckSiblings` then reads the files +for other targets beside it, declarations only, and `CompareIface` +holds them to the same exports by their fingerprints. A generic module +is compiled once per distinct list of arguments: `ParseArgs` reads +them at the import, `Params` binds them to the module's parameters, and +`IsModule` decides whether an instance already exists. + +The built-in procedures are in `Builtin` (and `New_`, `Atomic`): each +parses its own arguments, since `SIZE` takes a type and `INC` a +variable. + +## link.pas: tree shaking and layout + +`MarkLive` starts from the entry chunk and follows fixups, marking each +chunk it reaches; nothing else goes into the executable. `LinkAll` +places the live chunks section by section (`Place`), each at its +alignment, then copies their bytes into the images of text and rodata, +patching every fixup with the distance from the end of its field to its +target. + +The layout is chosen so the file can be mapped as it lies: text just +after the headers, rodata on the next page, then a writable page that +begins with the dynamic loader's table (in a program that uses C) and +goes on as bss. + +## elf.pas: the executable + +`WriteElf` writes the ELF header, the program headers, the text and +rodata images, and then what isn't loaded: section headers, a symbol +table with every live procedure and variable by `Module.name`, and the +line table. + +A program that uses C is linked dynamically, in the least way the +loader accepts: an interpreter header naming the loader; a dynamic +section naming the libraries; and, for each C symbol the program uses, +a symbol and a `GLOB_DAT` relocation that has the loader write the +symbol's address into the quadword `ExternChunk` made. Code calls C +through that quadword. There is no PLT and no lazy binding. +`SizeTables` and `PutTables` lay these out in the room `link.pas` left +at the end of rodata. + +Line numbers are DWARF 4: `PutDebug` writes a line table with one +sequence per procedure, from the notes `NoteLine` made, and the single +compile unit gdb needs to find it. There are no types or variables in +it; gdb can stop at a line and show where it is, not print a variable. + +## jed.pas: the command line + +`jed.pas` parses the arguments, calls `CompileMain`, and works out what +the program uses: `Use` marks the modules imported from the main module +and `Rt`; `UsesC` decides whether the program is dynamic, refusing one +that both uses C and has a module saying `link static`. Then it writes +the entry point. A static program starts at `_start` itself. A dynamic +one starts at a `_start` that calls `__libc_start_main` the way C's own +start-up does, which sets C up and calls back a `main` that runs the +module bodies and the program's `main`. + +## Where to start changing it + +- **A built-in procedure:** an entry in `sym.pas`'s universe, a case in + `Builtin` in `parse.pas`, and the code, in `gen.pas` or right there. +- **An instruction:** a routine in `x64.pas` built on `OpM` or `OpR`, + and a line in `test/enctest.pas` to check its encoding. +- **A statement:** a reserved word in `scan.pas` (in `TSym` and `Words`, + in the same order), a case in `Statement`, and a routine beside the + others. +- **A check that fails:** `Check` in `gen.pas`, and the message in + `lib/Rt.jed`'s `fail`. + +After any change: `make check`, `make bench`, `make loc`.