raz / commit 6949db09

commit
6949db09bbc9f4c6ff60e4ba0540d4d8f3bf9ab8
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
3a4fb62b
Report 2026-10.12: code procedures in assembly

For step 9b, before any code: a code procedure's body is the target's
assembly language, instructions to each ;, labels, up to end, with db
for bytes the assembler doesn't know (section 8, grammar). Appendix
A: the language is the target's; x86-64's is Intel's syntax as NASM
writes it. Version entry 2026-10.12.

patch · log

1 file changed, +34 -15

doc/raz.md (history) modified +34 -15

@@ -9,7 +9,7 @@
99 This report defines the language. It is short on purpose. Whatever it
1010 doesn't allow, raz doesn't have.
1111
12-This is version 2026-10.11 of the report; section 12 says what each
12+This is version 2026-10.12 of the report; section 12 says what each
1313 version changed. It is a thaw the owner chose, for a language anyone
1414 can build again, and it breaks programs written for 2026-10.10.
1515 The compiler follows version 2026-10.10 until it is brought to this
@@ -964,7 +964,7 @@
964964 ```
965965 ProcDeclaration = "proc" IdentDef Signature ";"
966966 ( "forward" | { LocalDeclaration } "begin" Statements "end" [ ident ]
967- | "code" Expr { "," Expr } ) .
967+ | "code" { [ ident ":" ] [ Instruction ] ";" } "end" [ ident ] ) .
968968 LocalDeclaration = "const" { ident [ ":" Type ] "=" Expr ";" }
969969 | "type" { ident "=" Type ";" }
970970 | "var" { ident { "," ident } ":" Type ";" } .
@@ -1017,25 +1017,34 @@
10171017
10181018 Procedures don't nest.
10191019
1020-**Code procedures.** A procedure whose body is `code` and a list of
1021-constant bytes is machine code, written by hand:
1020+**Code procedures.** A procedure whose body is `code` is machine code,
1021+written by hand in the target's assembly language (appendix A): one
1022+instruction to each `;`, and labels for jumps, up to `end`:
10221023
10231024 ```
10241025 proc rdtsc*: U64;
1025-code 0FH, 31H, (* rdtsc *)
1026- 48H, 0C1H, 0E2H, 20H, (* shl rdx, 32 *)
1027- 48H, 09H, 0D0H, (* or rax, rdx *)
1028- 0C3H; (* ret *)
1026+code
1027+ rdtsc;
1028+ shl rdx, 32;
1029+ or rax, rdx;
1030+ ret;
1031+end rdtsc;
10291032 ```
10301033
1034+A label, `name:`, names the place of the instruction after it, within
1035+the procedure. A constant of the module may stand wherever a number
1036+does. Any bytes may be written with `db`, as in `db 0FH, 31H;`, for an
1037+instruction the assembler doesn't know.
1038+
10311039 It is called like any procedure, with the System V convention: integer
10321040 and pointer arguments arrive in `rdi`, `rsi`, `rdx`, `rcx`, `r8` and
10331041 `r9`, `F32` and `F64` ones in `xmm0` to `xmm7`, and the result goes in
10341042 `rax` (and `rdx` for a second) or `xmm0`. Its parameters and results
10351043 are therefore of the types C can take (section 9), at most six integer
1036-and eight real parameters, and at most two results. The bytes are the whole
1037-procedure, `ret` included; they must keep `rbx`, `rbp`, `rsp` and `r12`
1038-to `r15` as they found them. The compiler doesn't look inside them.
1044+and eight real parameters, and at most two results. The instructions
1045+are the whole procedure, `ret` included; they must keep `rbx`, `rbp`,
1046+`rsp` and `r12` to `r15` as they found them. The compiler doesn't look
1047+past what they say.
10391048
10401049 ## 9. Calling C
10411050
@@ -1725,6 +1734,8 @@
17251734 11). An array constant has a type and is used as a variable that
17261735 can't be changed; `+` joins string constants (section 4). `match`
17271736 takes an integer, with constants and ranges as labels (7.4).
1737+- **2026-10.12.** A code procedure is written in the target's assembly
1738+ language, not as bytes (section 8; appendix A).
17281739
17291740 ## Appendix A. What an implementation chooses
17301741
@@ -1753,10 +1764,17 @@
17531764 several results, and how its frame is laid out, are the
17541765 implementation's.
17551766 - **What is the target's.** Code procedures (section 8) hold the
1756- target's machine code, and exist only where the implementation says
1757- so; `SYSCALL` exists only on Linux targets (section 6.8). A record's
1767+ target's machine code, written in its assembly language, and exist
1768+ only where the implementation says so; `SYSCALL` exists only on Linux targets (section 6.8). A record's
17581769 layout is the target's C layout (section 5.5), and the order of a
17591770 value's bytes is the target's (little-endian on x86-64).
1771+- **The assembly language.** How a code procedure's instructions are
1772+ written (section 8). On x86-64 it is Intel's syntax, as NASM writes
1773+ it: the destination first; registers by name (`rax`, `eax`, `al`,
1774+ `xmm0`); memory as `[base + index * scale + disp]`, with `byte`,
1775+ `word`, `dword` or `qword` before it where no register says the
1776+ size; `lock` before an instruction. Which instructions it knows is
1777+ the implementation's; `db` gives any others.
17601778 - **Sizes in the library.** How many bytes `Out` keeps before it
17611779 writes them, `Sys.outSize` (section 11): 4 KB on Linux, fewer on a
17621780 small machine.
@@ -1866,7 +1884,7 @@
18661884
18671885 ProcDeclaration = "proc" IdentDef Signature ";"
18681886 ( "forward" | { LocalDeclaration } "begin" Statements "end" [ ident ]
1869- | "code" Expr { "," Expr } ) .
1887+ | "code" { [ ident ":" ] [ Instruction ] ";" } "end" [ ident ] ) .
18701888 LocalDeclaration = "const" { ident [ ":" Type ] "=" Expr ";" }
18711889 | "type" { ident "=" Type ";" }
18721890 | "var" { ident { "," ident } ":" Type ";" } .
@@ -1882,4 +1900,5 @@
18821900 but `"` and `\`, in a character any but `'` and `\`. A procedure
18831901 argument of a generic module (section 3) is written as a `QualIdent`,
18841902 which is a `Type` here. `...` comes last in a `Signature`, and only in
1885-an `extern`.
1903+an `extern`. An `Instruction` is written in the target's assembly
1904+language (appendix A).