raz / commit e514b239

commit
e514b2392778dfb478f3ed2c2031a2a999ac04c8
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
6949db09
Step 9b: assembly procedures in place of byte procedures

x64.pas's Assemble reads a code procedure's instructions, Intel syntax
as NASM writes it (report 2026-10.12, section 8 and appendix A), up to
end, and encodes them with OpM/OpR: two-operand ALU ops, mov, test,
xchg, cmpxchg, xadd (with lock), lea, movzx/movsx/movsxd, the F7/FF
group, imul, shifts, push/pop, call/jmp, setcc, cmovcc, jumps to
labels (chains as the compiler's own), SSE sd/ss arithmetic, movsd,
movss, movq/movd, ucomis, the conversions, and a table of plain ones
(ret, syscall, cpuid, rdtsc, pause, fences, ...); db for the rest.
Registers by name in four sizes and xmm0-15; [base + index * scale +
disp] with byte/word/dword/qword; constants through parse's ConstExpr,
which gen's CodeProc hands in. OpR takes byte registers spl-dil.
Every code procedure in lib, examples and tests is assembly now;
Pretty indents a code block as begin's, Doc skips it, vim and emacs
indent it. test/run/Asm checks what each kind computes; test/err/Asm*
the assembler's errors.

patch · log

34 files changed, +667 -93

PLAN.md (history) modified +1 -1

@@ -147,7 +147,7 @@
147147 before a division; chosen per target. A `nil` test is left out
148148 only where the offset falls in the unmapped page at address 0.
149149 On x86-64 that means a field past 4,095 bytes still gets one.
150-- [ ] 9b. Assembly procedures in place of byte procedures: the
150+- [x] 9b. Assembly procedures in place of byte procedures: the
151151 target's own syntax (appendix A), for the instructions the
152152 encoder knows and a few of the system's, bytes for the rest.
153153 - [ ] 9c. `raz -S`: each line of source with its instructions, named

doc/compiler.md (history) modified +15 -5

@@ -12,10 +12,10 @@
1212 | `src/scan.pas` | 851 | 722 | Symbols, literals, comments, positions |
1313 | `src/sym.pas` | 596 | 497 | Types, objects, scopes, layout |
1414 | `src/chunk.pas` | 244 | 192 | The compiled program: chunks, fixups, line notes |
15-| `src/x64.pas` | 476 | 377 | The instruction encoder, and jumps within a procedure |
16-| `src/gen.pas` | 1,290 | 1,020 | The stack machine, for x86-64: values, operations, checks, calls, frames |
17-| `src/item.pas` | 1,303 | 1,090 | Items: types, constants, variables, operators, conversions, calls |
18-| `src/parse.pas` | 2,538 | 2,276 | Declarations, statements, expressions, built-ins, recovery, warnings |
15+| `src/x64.pas` | 834 | 694 | The instruction encoder, jumps within a procedure, the assembler |
16+| `src/gen.pas` | 1,319 | 1,037 | The stack machine, for x86-64: values, operations, checks, calls, frames |
17+| `src/item.pas` | 1,318 | 1,100 | Items: types, constants, variables, operators, conversions, calls |
18+| `src/parse.pas` | 2,529 | 2,267 | Declarations, statements, expressions, built-ins, recovery, warnings |
1919 | `src/link.pas` | 180 | 139 | Tree shaking, layout, patching, the places |
2020 | `src/elf.pas` | 424 | 351 | Static and dynamic ELF, symbol table, line numbers |
2121 | `src/raz.pas` | 188 | 141 | The command line |
@@ -234,6 +234,15 @@
234234 `test/enctest.pas` emits one of each for `objdump` to check. `Here` is
235235 the address of the code being written, as an operand.
236236
237+The assembler, `Assemble`, reads a code procedure's instructions with
238+the scanner, Intel's syntax as NASM writes it, and encodes them with
239+`OpM` and `OpR`: `Operand` reads a register, a memory operand or a
240+constant (by the parser's `ConstExpr`, handed in), and a few tables
241+give the operations of a kind their opcodes (`Alus`, `Unaries`,
242+`Shifts`, `SseOps`, `Plain`). Labels are chains of jumps, as below.
243+`db` gives bytes it doesn't know. `RegNames` and `CondNames` are the
244+names of the registers and conditions.
245+
237246 Jumps within a procedure don't wait for the linker. A label not yet
238247 placed is a chain of the jumps to it, each jump's 32-bit field holding
239248 the position of the one before (`Jmp`, `Jcc`); `FixLink` walks the
@@ -414,7 +423,8 @@
414423 Then declarations: `Declarations` reads `const`, `type`, `var`, `proc`
415424 and `extern`, each one through `Declaration`, a recovery point, which
416425 calls `ConstDecl`, `TypeDecl`, `VarDecl`, `ProcDecl` or `ExternDecl`. `ProcDecl` handles forward declarations, code procedures
417-(`CodeBody`, which just copies the bytes) and bodies (`Body`).
426+(`CodeBody`, which checks the signature and has gen's `CodeProc` run
427+the target's assembler) and bodies (`Body`).
418428 `ParseType` and its helpers (`RecordType`, `VariantType`, `Signature`,
419429 `EnumType`) build `TType`s.
420430

editor/emacs/raz-mode.el (history) modified +2 -2

@@ -62,10 +62,10 @@
6262 ;;; Indenting
6363
6464 (defconst raz--decl
65- "^[ \t]*\\(?:module\\|import\\|link\\|extern\\|proc\\|const\\|type\\|var\\|begin\\)\\_>")
65+ "^[ \t]*\\(?:module\\|import\\|link\\|extern\\|proc\\|const\\|type\\|var\\|begin\\|code\\)\\_>")
6666 (defconst raz--leading "^[ \t]*\\(?:or\\|and\\)\\_>")
6767 (defconst raz--block-word
68- "\\_<\\(if\\|while\\|for\\|match\\|record\\|variant\\|begin\\|repeat\\|end\\|until\\)\\_>")
68+ "\\_<\\(if\\|while\\|for\\|match\\|record\\|variant\\|begin\\|repeat\\|code\\|end\\|until\\)\\_>")
6969
7070 (defun raz--line (n)
7171 "The text of line N."

editor/vim/indent/raz.vim (history) modified +3 -3

@@ -19,10 +19,10 @@
1919 endif
2020
2121 " searchpair() needs groups that don't capture
22-let s:opener = '\C\<\%(if\|while\|for\|match\|record\|variant\|begin\|repeat\)\>'
22+let s:opener = '\C\<\%(if\|while\|for\|match\|record\|variant\|begin\|repeat\|code\)\>'
2323 let s:closer = '\C\<\%(end\|until\)\>'
2424 let s:skip = 'synIDattr(synID(line("."), col("."), 1), "name") =~? "string\\|char\\|comment"'
25-let s:decl = '^\s*\%(module\|import\|link\|extern\|proc\|const\|type\|var\|begin\)\>'
25+let s:decl = '^\s*\%(module\|import\|link\|extern\|proc\|const\|type\|var\|begin\|code\)\>'
2626
2727 " The line's code, without strings, characters and comments.
2828 function! s:Code(lnum)
@@ -81,7 +81,7 @@
8181 let n = 0
8282 let i = 0
8383 while 1
84- let m = matchstrpos(a:code, '\C\<\%(if\|while\|for\|record\|variant\|begin\|repeat\|end\|until\)\>', i)
84+ let m = matchstrpos(a:code, '\C\<\%(if\|while\|for\|record\|variant\|begin\|repeat\|code\|end\|until\)\>', i)
8585 if m[1] < 0
8686 return n
8787 endif

examples/Cpu.raz (history) modified +11 -9

@@ -5,15 +5,17 @@
55
66 (* cpuid leaf 0: puts the 12 bytes of the vendor in buf, rdi. *)
77 proc vendor(buf: ^U8);
8-code 53H, (* push rbx *)
9- 49H, 89H, 0F8H, (* mov r8, rdi *)
10- 31H, 0C0H, (* xor eax, eax *)
11- 0FH, 0A2H, (* cpuid *)
12- 41H, 89H, 18H, (* mov [r8], ebx *)
13- 41H, 89H, 50H, 04H, (* mov [r8+4], edx *)
14- 41H, 89H, 48H, 08H, (* mov [r8+8], ecx *)
15- 5BH, (* pop rbx *)
16- 0C3H; (* ret *)
8+code
9+ push rbx;
10+ mov r8, rdi;
11+ xor eax, eax;
12+ cpuid;
13+ mov [r8], ebx;
14+ mov [r8 + 4], edx;
15+ mov [r8 + 8], ecx;
16+ pop rbx;
17+ ret;
18+end vendor;
1719
1820 proc main*;
1921 var buf: [12]U8; i: INT; ok: BOOL;

lib/Math.raz (history) modified +9 -5

@@ -36,8 +36,10 @@
3636 end;
3737
3838 proc sqrt*(x: F64): F64;
39-code 0F2H, 0FH, 51H, 0C0H, (* sqrtsd xmm0, xmm0 *)
40- 0C3H; (* ret *)
39+code
40+ sqrtsd xmm0, xmm0;
41+ ret;
42+end sqrt;
4143
4244 (* The largest whole number not above x. One of 2^52 or more is whole
4345 already, and so are an infinity and NaN. *)
@@ -109,9 +111,11 @@
109111
110112 (* a times b: the low 64 bits of the product, and the high. *)
111113 proc mul(a, b: U64): (U64, U64);
112-code 48H, 89H, 0F8H, (* mov rax, rdi *)
113- 48H, 0F7H, 0E6H, (* mul rsi *)
114- 0C3H; (* ret *)
114+code
115+ mov rax, rdi;
116+ mul rsi;
117+ ret;
118+end mul;
115119
116120 (* 64 bits of t from bit s on, the first bit of t[0] being 0. *)
117121 proc window(t: []U64; s: INT): U64;

lib/Rt.raz (history) modified +4 -2

@@ -194,8 +194,10 @@
194194
195195 (* Returns from a signal handler: rt_sigreturn *)
196196 proc sigReturn;
197-code 0B8H, 0FH, 0, 0, 0, (* mov eax, 15 *)
198- 0FH, 05H; (* syscall *)
197+code
198+ mov eax, 15;
199+ syscall;
200+end sigReturn;
199201
200202 (* A signal stops the program: says where it was and why on standard
201203 error, and returns to the instruction that faulted. The handler was

lib/Thread.linux-x86_64.raz (history) modified +19 -16

@@ -41,22 +41,25 @@
4141 registers but rsp of its own to find them by. Returns the new
4242 thread's id, or minus an errno. *)
4343 proc start(flags: INT; stack: ^U8; ptid, ctid: ^INT; fn: proc(arg: ^U8); arg: ^U8): INT;
44-code 48H, 83H, 0EEH, 10H, (* sub rsi, 16 *)
45- 4CH, 89H, 06H, (* mov [rsi], r8 *)
46- 4CH, 89H, 4EH, 08H, (* mov [rsi + 8], r9 *)
47- 49H, 89H, 0CAH, (* mov r10, rcx *)
48- 45H, 31H, 0C0H, (* xor r8d, r8d: no tls *)
49- 0B8H, 38H, 0, 0, 0, (* mov eax, 56: clone *)
50- 0FH, 05H, (* syscall *)
51- 48H, 85H, 0C0H, (* test rax, rax *)
52- 75H, 0DH, (* jnz parent *)
53- 58H, (* pop rax: fn *)
54- 5FH, (* pop rdi: arg *)
55- 0FFH, 0D0H, (* call rax *)
56- 31H, 0FFH, (* xor edi, edi *)
57- 0B8H, 3CH, 0, 0, 0, (* mov eax, 60: exit, this thread only *)
58- 0FH, 05H, (* syscall *)
59- 0C3H; (* parent: ret *)
44+code
45+ sub rsi, 16;
46+ mov [rsi], r8;
47+ mov [rsi + 8], r9;
48+ mov r10, rcx;
49+ xor r8d, r8d; (* no tls *)
50+ mov eax, 56; (* clone *)
51+ syscall;
52+ test rax, rax;
53+ jnz parent;
54+ pop rax; (* fn *)
55+ pop rdi; (* arg *)
56+ call rax;
57+ xor edi, edi;
58+ mov eax, 60; (* exit, this thread only *)
59+ syscall;
60+ parent:
61+ ret;
62+end start;
6063
6164 proc futex(word: ^INT; op, val: INT): INT;
6265 begin

src/gen.pas modified +10 -0

@@ -147,6 +147,7 @@
147147 procedure RunDefer(at: longint);
148148
149149 function EntryPoint(dynamic: boolean; first: PModule; main: PObj; cExit, places, placesVar, exitProc: longint): longint;
150+procedure CodeProc(c: longint; constExpr: TConstFn);
150151
151152 implementation
152153
@@ -1297,6 +1298,15 @@
12971298 Ud2;
12981299 EndChunk(start);
12991300 EntryPoint := entry;
1301+end;
1302+
1303+{ A code procedure: its instructions, read by the assembler (x64.pas),
1304+ are the whole of chunk c }
1305+procedure CodeProc(c: longint; constExpr: TConstFn);
1306+begin
1307+ BeginChunk(c);
1308+ Assemble(constExpr);
1309+ EndChunk(c);
13001310 end;
13011311
13021312 initialization

src/parse.pas modified +4 -14

@@ -1671,7 +1671,7 @@
16711671 end;
16721672
16731673 procedure CodeBody(p: PObj; sig: PType; const at: TPos);
1674-var q: PObj; ni, nf: longint; v: int64; start: TPos;
1674+var q: PObj; ni, nf: longint;
16751675 begin
16761676 ni := 0; nf := 0;
16771677 q := sig^.fields;
@@ -1690,16 +1690,8 @@
16901690 or (q^.typ^.form = fReal) or (q^.next^.typ^.form = fReal) then
16911691 MarkAt(at, 'a code procedure returns two results only in rax and rdx');
16921692 end else if not IsScalar(sig^.base) then MarkAt(at, 'a code procedure returns a single value');
1693- BeginChunk(p^.chunk);
1694- repeat
1695- start := TokPos;
1696- v := ConstExpr;
1697- if (v < 0) or (v > 255) then MarkAt(SpanFrom(start), 'expected a byte from 0 to 255, found ' + IntStr(v));
1698- Put1(v);
1699- if tok <> sComma then break;
1700- Next;
1701- until false;
1702- EndChunk(p^.chunk);
1693+ CodeProc(p^.chunk, @ConstExpr);
1694+ Expect(sEnd);
17031695 end;
17041696
17051697 procedure ProcDecl;
@@ -1738,9 +1730,7 @@
17381730 Next;
17391731 p^.isCode := true;
17401732 CodeBody(p, sig, at);
1741- exit;
1742- end;
1743- if checkMode then SkipBody else Body(p, sig, line);
1733+ end else if checkMode then SkipBody else Body(p, sig, line);
17441734 if tok = sIdent then begin
17451735 if id <> n then Mark('expected ' + NameOf(n) + ', found ' + NameOf(id));
17461736 Next;

src/x64.pas modified +363 -5

@@ -103,6 +103,24 @@
103103 procedure FixLink(L: longint);
104104 procedure FixLinkTo(L, target: longint);
105105
106+type
107+ TConstFn = function: int64;
108+
109+const
110+ RegNames: array[0..3, 0..15] of string[5] = (
111+ ('rax', 'rcx', 'rdx', 'rbx', 'rsp', 'rbp', 'rsi', 'rdi',
112+ 'r8', 'r9', 'r10', 'r11', 'r12', 'r13', 'r14', 'r15'),
113+ ('eax', 'ecx', 'edx', 'ebx', 'esp', 'ebp', 'esi', 'edi',
114+ 'r8d', 'r9d', 'r10d', 'r11d', 'r12d', 'r13d', 'r14d', 'r15d'),
115+ ('ax', 'cx', 'dx', 'bx', 'sp', 'bp', 'si', 'di',
116+ 'r8w', 'r9w', 'r10w', 'r11w', 'r12w', 'r13w', 'r14w', 'r15w'),
117+ ('al', 'cl', 'dl', 'bl', 'spl', 'bpl', 'sil', 'dil',
118+ 'r8b', 'r9b', 'r10b', 'r11b', 'r12b', 'r13b', 'r14b', 'r15b'));
119+ CondNames: array[0..15] of string[3] = ('o', 'no', 'b', 'ae', 'e', 'ne',
120+ 'be', 'a', 's', 'ns', 'p', 'np', 'l', 'ge', 'le', 'g');
121+
122+procedure Assemble(constExpr: TConstFn);
123+
106124 implementation
107125
108126 { Operands }
@@ -145,7 +163,10 @@
145163 until op = 0;
146164 end;
147165
148-procedure Prefix(pfx: longint; w: boolean; reg, index, base: longint; byteReg: boolean);
166+{ The legacy prefix pfx, if any, and REX: wide is REX.W; byte4 says
167+ a byte register of 4 to 7 is used, spl to dil, which exist only with
168+ a REX prefix. }
169+procedure Prefix(pfx: longint; w: boolean; reg, index, base: longint; byte4: boolean);
149170 var rex: longint;
150171 begin
151172 if pfx <> 0 then Put1(pfx);
@@ -154,14 +175,14 @@
154175 if reg >= 8 then rex := rex or 4;
155176 if index >= 8 then rex := rex or 2;
156177 if base >= 8 then rex := rex or 1;
157- { sil and dil exist only with a REX prefix }
158- if (rex <> 0) or (byteReg and (reg >= 4) and (reg <= 7)) then Put1($40 or rex);
178+ if (rex <> 0) or byte4 then Put1($40 or rex);
159179 end;
160180
161181 { op reg, [mem] }
162182 procedure OpM(pfx: longint; w: boolean; op, reg: longint; const m: TMem; byteReg: boolean = false);
163183 var md, b, sc: longint;
164184 begin
185+ byteReg := byteReg and (reg >= 4) and (reg <= 7);
165186 if m.base = RIP then Prefix(pfx, w, reg, NoIndex, 0, byteReg)
166187 else Prefix(pfx, w, reg, m.index, m.base, byteReg);
167188 Opcode(op);
@@ -190,9 +211,9 @@
190211 end;
191212
192213 { op reg, rm (both registers) }
193-procedure OpR(pfx: longint; w: boolean; op, reg, rm: longint);
214+procedure OpR(pfx: longint; w: boolean; op, reg, rm: longint; byteReg: boolean = false);
194215 begin
195- Prefix(pfx, w, reg, NoIndex, rm, false);
216+ Prefix(pfx, w, reg, NoIndex, rm, byteReg and ((reg in [4..7]) or (rm in [4..7])));
196217 Opcode(op);
197218 Put1($C0 or (reg and 7) shl 3 or (rm and 7));
198219 end;
@@ -471,6 +492,343 @@
471492 procedure FixLink(L: longint);
472493 begin
473494 FixLinkTo(L, pc);
495+end;
496+
497+
498+{ The assembler, for code procedures: x86-64 in Intel's syntax as NASM
499+ writes it (report, appendix A). It reads instructions with the
500+ scanner up to end, and encodes them with OpM and OpR; constants are
501+ read by the parser's constExpr. }
502+
503+const
504+ NoBase = -3; { a memory operand's base, not yet read }
505+
506+type
507+ TOpKind = (oReg, oXmm, oMem, oImm);
508+ TOpd = record
509+ kind: TOpKind;
510+ r, size: longint; { oReg, oXmm: the register and its size; oMem: its size, or 0 }
511+ m: TMem;
512+ v: int64; { oImm }
513+ end;
514+
515+{ The current symbol as a word: an identifier, or a reserved word that
516+ is also an instruction }
517+function AsmWord: shortstring;
518+begin
519+ case tok of
520+ sIdent: AsmWord := NameOf(id);
521+ sAnd: AsmWord := 'and';
522+ sOr: AsmWord := 'or';
523+ sXor: AsmWord := 'xor';
524+ sNot: AsmWord := 'not';
525+ sDiv: AsmWord := 'div';
526+ sShl: AsmWord := 'shl';
527+ sShr: AsmWord := 'shr';
528+ else AsmWord := '';
529+ end;
530+end;
531+
532+{ Whether w names a register: rax, eax, ax, al and so on, or xmm0 to
533+ xmm15 (size 16) }
534+function IsReg(const w: shortstring; var r, size: longint): boolean;
535+const Sizes: array[0..3] of longint = (8, 4, 2, 1);
536+var i, j, e: longint;
537+begin
538+ IsReg := true;
539+ for i := 0 to 3 do
540+ for j := 0 to 15 do
541+ if RegNames[i, j] = w then begin r := j; size := Sizes[i]; exit end;
542+ size := 16;
543+ Val(copy(w, 4, 2), r, e);
544+ IsReg := (copy(w, 1, 3) = 'xmm') and (length(w) > 3) and (e = 0) and (r in [0..15]);
545+end;
546+
547+{ Which word of list, a list of words each with a space either side,
548+ w is, from 0; -1 if none }
549+function WordIndex(const w, list: shortstring): longint;
550+var i, k: longint;
551+begin
552+ k := pos(' ' + w + ' ', list);
553+ WordIndex := -1;
554+ if k = 0 then exit;
555+ WordIndex := 0;
556+ for i := 2 to k do if list[i] = ' ' then inc(WordIndex);
557+end;
558+
559+{ The condition a name ends in: e of je, nz of jnz }
560+function Cond(const w: shortstring): longint;
561+const
562+ Alias: array[0..11] of string[3] = ('c', 'nae', 'nc', 'nb', 'z', 'nz', 'na', 'nbe', 'nge', 'nl', 'ng', 'nle');
563+ AliasCc: array[0..11] of longint = (2, 2, 3, 3, 4, 5, 6, 7, 12, 13, 14, 15);
564+var i: longint;
565+begin
566+ for i := 0 to 15 do if CondNames[i] = w then exit(i);
567+ for i := 0 to 11 do if Alias[i] = w then exit(AliasCc[i]);
568+ Cond := -1;
569+end;
570+
571+procedure Operand(var o: TOpd; constExpr: TConstFn);
572+var w: shortstring; r, size, sz: longint; st: TScanState;
573+begin
574+ FillChar(o, SizeOf(o), 0);
575+ w := AsmWord;
576+ sz := 0;
577+ if w = 'byte' then sz := 1 else if w = 'word' then sz := 2
578+ else if w = 'dword' then sz := 4 else if w = 'qword' then sz := 8;
579+ if sz <> 0 then begin Next; w := AsmWord end;
580+ if tok = sLbrak then begin
581+ { [base + index * scale + disp], the displacement last }
582+ Next;
583+ o.kind := oMem; o.size := sz;
584+ o.m := Mem(NoBase, 0);
585+ repeat
586+ if IsReg(AsmWord, r, size) and (size = 8) then begin
587+ Next;
588+ if tok = sTimes then begin Next; o.m.index := r; o.m.scale := constExpr() end
589+ else if o.m.base = NoBase then o.m.base := r
590+ else begin o.m.index := r; o.m.scale := 1 end;
591+ end else o.m.disp := o.m.disp + constExpr();
592+ if not (tok in [sPlus, sMinus]) then break;
593+ SaveScan(st);
594+ Next;
595+ if not IsReg(AsmWord, r, size) and (st.tok = sMinus) then RestoreScan(st);
596+ until false;
597+ if (tok <> sRbrak) or (o.m.base = NoBase) or not (o.m.scale in [1, 2, 4, 8]) then
598+ Mark('expected a memory operand, [base + index * scale + disp]');
599+ Next;
600+ end else if IsReg(w, o.r, o.size) then begin
601+ if o.size = 16 then o.kind := oXmm else o.kind := oReg;
602+ Next;
603+ end else begin
604+ o.kind := oImm;
605+ o.v := constExpr();
606+ end;
607+end;
608+
609+{ op with register reg and the register or memory operand o }
610+procedure RM(pfx: longint; w: boolean; op, reg: longint; const o: TOpd; byteReg: boolean = false);
611+begin
612+ if o.kind = oMem then OpM(pfx, w, op, reg, o.m, byteReg)
613+ else OpR(pfx, w, op, reg, o.r, byteReg);
614+end;
615+
616+procedure PutImm(v: int64; size: longint);
617+begin
618+ case size of
619+ 1: Put1(v);
620+ 2: begin Put1(v); Put1(v shr 8) end;
621+ else Put4(v);
622+ end;
623+end;
624+
625+procedure Assemble(constExpr: TConstFn);
626+const
627+ Alus = ' add or adc sbb and sub xor cmp ';
628+ Unaries = ' not neg mul imul div idiv ';
629+ Shifts = ' rol ror rcl rcr shl shr sal sar ';
630+ SseOps = ' sqrt add mul sub min div max ';
631+ SseCodes: array[0..6] of longint = ($51, $58, $59, $5C, $5D, $5E, $5F);
632+ Plain: array[0..14] of string[7] = ('ret', 'leave', 'syscall', 'cpuid', 'rdtsc',
633+ 'pause', 'nop', 'ud2', 'int3', 'hlt', 'cqo', 'cdq', 'mfence', 'lfence', 'sfence');
634+ PlainOp: array[0..14] of longint = ($C3, $C9, $050F, $A20F, $310F,
635+ $90F3, $90, $0B0F, $CC, $F4, $9948, $99, $F0AE0F, $E8AE0F, $F8AE0F);
636+var
637+ w: shortstring; o: array[0..2] of TOpd; n, i, k, size, pfx, cc: longint;
638+ wide, byteOp: boolean; at, start: TPos; v: int64;
639+ names, places, chains: array[0..63] of longint; numLabels: longint;
640+ usedAt: array[0..63] of TPos;
641+
642+ { the label called n, entered if new }
643+ function Lab(n: longint): longint;
644+ var i: longint;
645+ begin
646+ for i := 0 to numLabels - 1 do if names[i] = n then exit(i);
647+ if numLabels > High(names) then Mark('more than 64 labels');
648+ names[numLabels] := n; places[numLabels] := -1; chains[numLabels] := NoLabel;
649+ usedAt[numLabels] := TokPos;
650+ Lab := numLabels;
651+ inc(numLabels);
652+ end;
653+
654+ procedure Bad;
655+ begin
656+ MarkAt(at, 'can''t assemble ' + w + ' with these operands');
657+ end;
658+
659+ { an instruction of two operands: a register or memory, and a
660+ register (opcode toRM), the other way (fromRM), or an immediate
661+ (opImm /digit, or none if opImm < 0) }
662+ procedure Two(toRM, fromRM, opImm, digit: longint);
663+ begin
664+ if n <> 2 then Bad;
665+ if o[1].kind = oImm then begin
666+ if (o[0].kind = oImm) or (opImm < 0) then Bad;
667+ if (opImm = $81) and not byteOp and (o[1].v >= -128) and (o[1].v <= 127) then begin
668+ RM(pfx, wide, $83, digit, o[0]); { a byte, sign-extended }
669+ Put1(o[1].v);
670+ end else begin
671+ RM(pfx, wide, opImm - ord(byteOp), digit, o[0]);
672+ PutImm(o[1].v, size);
673+ end;
674+ end else if (o[1].kind = oReg) and (o[0].kind in [oReg, oMem]) then
675+ RM(pfx, wide, toRM - ord(byteOp), o[1].r, o[0], byteOp)
676+ else if (o[0].kind = oReg) and (o[1].kind = oMem) then
677+ RM(pfx, wide, fromRM - ord(byteOp), o[0].r, o[1], byteOp)
678+ else Bad;
679+ end;
680+
681+begin
682+ numLabels := 0;
683+ while not (tok in [sEnd, sEof]) do begin
684+ at := TokPos;
685+ w := AsmWord;
686+ if tok = sSemicolon then begin Next; continue end;
687+ if w = '' then Mark('expected an instruction');
688+ Next;
689+ if tok = sColon then begin
690+ k := Lab(Intern(@w[1], length(w)));
691+ if places[k] >= 0 then MarkAt(at, 'label ' + w + ' is defined twice');
692+ places[k] := pc;
693+ FixLink(chains[k]);
694+ Next;
695+ continue;
696+ end;
697+ if w = 'lock' then begin
698+ Put1($F0);
699+ w := AsmWord;
700+ Next;
701+ end;
702+ cc := -1;
703+ if w = 'db' then
704+ repeat
705+ start := TokPos;
706+ v := constExpr();
707+ if (v < -128) or (v > 255) then MarkAt(SpanFrom(start), 'expected a byte from 0 to 255, found ' + IntStr(v));
708+ Put1(v);
709+ if tok <> sComma then break;
710+ Next;
711+ until false
712+ else if (w[1] = 'j') and ((w = 'jmp') or (Cond(copy(w, 2, 3)) >= 0)) and (tok = sIdent) then begin
713+ { a jump to a label }
714+ k := Lab(id);
715+ Next;
716+ if w <> 'jmp' then cc := Cond(copy(w, 2, 3));
717+ if places[k] >= 0 then begin
718+ if cc < 0 then JmpBack(places[k]) else JccBack(cc, places[k])
719+ end else if cc < 0 then Jmp(chains[k]) else Jcc(cc, chains[k]);
720+ end else begin
721+ n := 0;
722+ if tok <> sSemicolon then
723+ repeat
724+ if n > High(o) then Mark('too many operands');
725+ Operand(o[n], constExpr);
726+ inc(n);
727+ if tok <> sComma then break;
728+ Next;
729+ until false;
730+ { the size: a register's, or what the memory operand says }
731+ size := 8;
732+ for i := n - 1 downto 0 do
733+ if (o[i].kind = oReg) or (o[i].kind = oMem) and (o[i].size <> 0) then size := o[i].size;
734+ if n > 0 then
735+ if (o[0].kind = oReg) or (o[0].kind = oMem) and (o[0].size <> 0) then size := o[0].size;
736+ wide := size = 8;
737+ byteOp := size = 1;
738+ if size = 2 then pfx := $66 else pfx := 0;
739+ if WordIndex(w, Alus) >= 0 then begin
740+ k := WordIndex(w, Alus);
741+ Two(k * 8 + 1, k * 8 + 3, $81, k)
742+ end
743+ else if w = 'mov' then begin
744+ if (n = 2) and (o[0].kind = oReg) and (o[1].kind = oImm) and (size >= 4) then begin
745+ if wide then MovRI(o[0].r, o[1].v)
746+ else begin Prefix(0, false, 0, NoIndex, o[0].r, false); Put1($B8 + o[0].r and 7); Put4(o[1].v) end;
747+ end else Two($89, $8B, $C7, 0);
748+ end else if w = 'test' then Two($85, $85, $F7, 0)
749+ else if w = 'xchg' then Two($87, $87, -1, 0)
750+ else if (w = 'cmpxchg') or (w = 'xadd') then begin
751+ if (n <> 2) or (o[1].kind <> oReg) then Bad;
752+ if w = 'xadd' then k := $C10F else k := $B10F;
753+ RM(pfx, wide, k - ord(byteOp) shl 8, o[1].r, o[0], byteOp);
754+ end else if w = 'lea' then begin
755+ if (n <> 2) or (o[0].kind <> oReg) or (o[1].kind <> oMem) then Bad;
756+ RM(pfx, wide, $8D, o[0].r, o[1]);
757+ end else if (w = 'movzx') or (w = 'movsx') or (w = 'movsxd') then begin
758+ if (n <> 2) or (o[0].kind <> oReg) or (o[1].kind = oImm) then Bad;
759+ if w = 'movsxd' then k := $63
760+ else if o[1].size = 1 then k := $B60F
761+ else if o[1].size = 2 then k := $B70F
762+ else Bad;
763+ if w = 'movsx' then k := k + $0800;
764+ RM(pfx, wide, k, o[0].r, o[1], o[1].size = 1);
765+ end else if ((WordIndex(w, Unaries) >= 0) and (n = 1)) or (w = 'inc') or (w = 'dec') then begin
766+ if o[0].kind = oImm then Bad;
767+ if w = 'inc' then RM(pfx, wide, $FF - ord(byteOp), 0, o[0], byteOp)
768+ else if w = 'dec' then RM(pfx, wide, $FF - ord(byteOp), 1, o[0], byteOp)
769+ else RM(pfx, wide, $F7 - ord(byteOp), WordIndex(w, Unaries) + 2, o[0], byteOp);
770+ end else if w = 'imul' then begin
771+ if (n < 2) or (o[0].kind <> oReg) or (o[1].kind = oImm) then Bad;
772+ if n = 2 then RM(pfx, wide, $AF0F, o[0].r, o[1])
773+ else begin RM(pfx, wide, $69, o[0].r, o[1]); PutImm(o[2].v, size) end;
774+ end else if WordIndex(w, Shifts) >= 0 then begin
775+ k := WordIndex(w, Shifts);
776+ if k = 6 then k := 4; { sal is shl }
777+ if (n <> 2) or (o[0].kind = oImm) then Bad;
778+ if o[1].kind = oImm then begin RM(pfx, wide, $C1 - ord(byteOp), k, o[0], byteOp); Put1(o[1].v) end
779+ else if (o[1].kind = oReg) and (o[1].r = RCX) and (o[1].size = 1) then RM(pfx, wide, $D3 - ord(byteOp), k, o[0], byteOp)
780+ else Bad;
781+ end else if (w = 'push') or (w = 'pop') then begin
782+ if (n <> 1) or (o[0].kind <> oReg) or not wide then Bad;
783+ if w = 'push' then Push(o[0].r) else Pop(o[0].r);
784+ end else if (w = 'call') or (w = 'jmp') then begin
785+ if (n <> 1) or (o[0].kind = oImm) then Bad;
786+ if w = 'call' then RM(0, false, $FF, 2, o[0]) else RM(0, false, $FF, 4, o[0]);
787+ end else if (copy(w, 1, 3) = 'set') and (Cond(copy(w, 4, 3)) >= 0) then begin
788+ if (n <> 1) or (o[0].kind = oImm) or (size <> 1) then Bad;
789+ RM(0, false, $900F + Cond(copy(w, 4, 3)) shl 8, 0, o[0], true);
790+ end else if (copy(w, 1, 4) = 'cmov') and (Cond(copy(w, 5, 3)) >= 0) then begin
791+ if (n <> 2) or (o[0].kind <> oReg) or (o[1].kind = oImm) then Bad;
792+ RM(pfx, wide, $400F + Cond(copy(w, 5, 3)) shl 8, o[0].r, o[1]);
793+ end else if (length(w) > 2) and (WordIndex(copy(w, 1, length(w) - 2), SseOps) >= 0)
794+ and ((copy(w, length(w) - 1, 2) = 'sd') or (copy(w, length(w) - 1, 2) = 'ss')) then begin
795+ { sqrtsd, addss and the like: xmm, xmm or memory }
796+ if (n <> 2) or (o[0].kind <> oXmm) or not (o[1].kind in [oXmm, oMem]) then Bad;
797+ k := $0F or SseCodes[WordIndex(copy(w, 1, length(w) - 2), SseOps)] shl 8;
798+ RM(RealPfx[w[length(w)] = 's'], false, k, o[0].r, o[1]);
799+ end else if (w = 'movsd') or (w = 'movss') then begin
800+ if w = 'movsd' then k := $F2 else k := $F3;
801+ if (n = 2) and (o[0].kind = oXmm) and (o[1].kind in [oXmm, oMem]) then RM(k, false, $100F, o[0].r, o[1])
802+ else if (n = 2) and (o[0].kind = oMem) and (o[1].kind = oXmm) then RM(k, false, $110F, o[1].r, o[0])
803+ else Bad;
804+ end else if (w = 'movq') or (w = 'movd') then begin
805+ if (n = 2) and (o[0].kind = oXmm) and (o[1].kind in [oReg, oMem]) then RM($66, w = 'movq', $6E0F, o[0].r, o[1])
806+ else if (n = 2) and (o[1].kind = oXmm) and (o[0].kind in [oReg, oMem]) then RM($66, w = 'movq', $7E0F, o[1].r, o[0])
807+ else Bad;
808+ end else if (w = 'ucomisd') or (w = 'ucomiss') then begin
809+ if (n <> 2) or (o[0].kind <> oXmm) or not (o[1].kind in [oXmm, oMem]) then Bad;
810+ if w = 'ucomisd' then RM($66, false, $2E0F, o[0].r, o[1]) else RM(0, false, $2E0F, o[0].r, o[1]);
811+ end else if (w = 'cvtsi2sd') or (w = 'cvtsi2ss') then begin
812+ if (n <> 2) or (o[0].kind <> oXmm) or not (o[1].kind in [oReg, oMem]) then Bad;
813+ RM(RealPfx[w = 'cvtsi2ss'], o[1].size <> 4, $2A0F, o[0].r, o[1]);
814+ end else if (w = 'cvttsd2si') or (w = 'cvttss2si') then begin
815+ if (n <> 2) or (o[0].kind <> oReg) or not (o[1].kind in [oXmm, oMem]) then Bad;
816+ RM(RealPfx[w = 'cvttss2si'], wide, $2C0F, o[0].r, o[1]);
817+ end else if (w = 'cvtsd2ss') or (w = 'cvtss2sd') then begin
818+ if (n <> 2) or (o[0].kind <> oXmm) or not (o[1].kind in [oXmm, oMem]) then Bad;
819+ RM(RealPfx[w = 'cvtss2sd'], false, $5A0F, o[0].r, o[1]);
820+ end else begin
821+ k := 0;
822+ while (k <= High(Plain)) and (Plain[k] <> w) do inc(k);
823+ if (k > High(Plain)) or (n <> 0) then MarkAt(at, 'unknown instruction ' + w);
824+ Opcode(PlainOp[k]);
825+ end;
826+ end;
827+ if tok <> sSemicolon then Mark('expected ";"');
828+ Next;
829+ end;
830+ for k := 0 to numLabels - 1 do
831+ if places[k] < 0 then MarkAt(usedAt[k], 'label ' + NameOf(names[k]) + ' isn''t defined');
474832 end;
475833
476834 end.

test/err/AsmLabel.err (history) added +4 -0

@@ -0,0 +1,4 @@
1+test/err/AsmLabel.raz:4:9: error: label nowhere isn't defined
2+ 4 | jmp nowhere;
3+ | ^^^^^^^
4+raz: 1 error

test/err/AsmLabel.raz (history) added +6 -0

@@ -0,0 +1,6 @@
1+module AsmLabel;
2+proc f;
3+code
4+ jmp nowhere;
5+end f;
6+end AsmLabel.

test/err/AsmMemory.err (history) added +4 -0

@@ -0,0 +1,4 @@
1+test/err/AsmMemory.raz:4:16: error: expected a memory operand, [base + index * scale + disp]
2+ 4 | mov rax, [8];
3+ | ^
4+raz: 1 error

test/err/AsmMemory.raz (history) added +6 -0

@@ -0,0 +1,6 @@
1+module AsmMemory;
2+proc f;
3+code
4+ mov rax, [8];
5+end f;
6+end AsmMemory.

test/err/AsmOperands.err (history) added +4 -0

@@ -0,0 +1,4 @@
1+test/err/AsmOperands.raz:4:5: error: can't assemble lea with these operands
2+ 4 | lea rax, rbx;
3+ | ^^^
4+raz: 1 error

test/err/AsmOperands.raz (history) added +7 -0

@@ -0,0 +1,7 @@
1+module AsmOperands;
2+proc f;
3+code
4+ lea rax, rbx;
5+ ret;
6+end f;
7+end AsmOperands.

test/err/AsmUnknown.err (history) added +4 -0

@@ -0,0 +1,4 @@
1+test/err/AsmUnknown.raz:4:5: error: unknown instruction frob
2+ 4 | frob rax;
3+ | ^^^^
4+raz: 1 error

test/err/AsmUnknown.raz (history) added +7 -0

@@ -0,0 +1,7 @@
1+module AsmUnknown;
2+proc f;
3+code
4+ frob rax;
5+ ret;
6+end f;
7+end AsmUnknown.

test/err/AtPByte.err (history) modified +3 -3

@@ -1,4 +1,4 @@
1-test/err/AtPByte.raz:3:6: error: expected a byte from 0 to 255, found 300
2- 3 | code 300;
3- | ^^^
1+test/err/AtPByte.raz:4:11: error: expected a byte from 0 to 255, found 300
2+ 4 | db 1, 300;
3+ | ^^^
44 raz: 1 error

test/err/AtPByte.raz (history) modified +3 -1

@@ -1,4 +1,6 @@
11 module AtPByte;
22 proc f(a: I64);
3-code 300;
3+code
4+ db 1, 300;
5+end;
46 end AtPByte.

test/err/GenCodeValue.raz (history) modified +1 -1

@@ -1,5 +1,5 @@
11 module GenCodeValue;
2-type F = proc(): (I64, I64); proc Two(): (I64, I64); code 0C3H;
2+type F = proc(): (I64, I64); proc Two(): (I64, I64); code ret; end;
33 proc main*;
44 var f: F;
55 begin

test/err/StmtCodeParams.raz (history) modified +1 -1

@@ -1,4 +1,4 @@
11 module StmtCodeParams;
22 proc f(a, b, c, d, e, f, g: I64);
3-code 0C3H;
3+code ret; end;
44 end StmtCodeParams.

test/err/StmtCodeResult.raz (history) modified +1 -1

@@ -1,5 +1,5 @@
11 module StmtCodeResult;
22 type R = record a: I64; end;
33 proc f: R;
4-code 0C3H;
4+code ret; end;
55 end StmtCodeResult.

test/err/StmtCodeTakes.raz (history) modified +1 -1

@@ -1,5 +1,5 @@
11 module StmtCodeTakes;
22 type R = record a: I64; end;
33 proc f(r: R);
4-code 0C3H;
4+code ret; end;
55 end StmtCodeTakes.

test/err/StmtCodeTwo.raz (history) modified +1 -1

@@ -1,4 +1,4 @@
11 module StmtCodeTwo;
22 proc f: (F64, I64);
3-code 0C3H;
3+code ret; end;
44 end StmtCodeTwo.

test/run/Asm.expect (history) added +9 -0

@@ -0,0 +1,9 @@
1+35
2+4321 0
3+98 azc
4+142 6
5+3 9 below
6+2
7+13
8+40 42
9+42 42

test/run/Asm.raz (history) added +132 -0

@@ -0,0 +1,132 @@
1+module Asm;
2+(* Code procedures in assembly: the instructions the assembler knows,
3+ each checked by what it computes *)
4+import Out;
5+
6+const Seven = 7;
7+
8+(* a * 10 + b - 7, by lea, shifts and adds *)
9+proc tens(a, b: I64): I64;
10+code
11+ lea rax, [rdi + rdi * 4];
12+ shl rax, 1;
13+ add rax, rsi;
14+ sub rax, Seven;
15+ ret;
16+end tens;
17+
18+(* the sum of n quadwords at p, by a loop back to a label *)
19+proc sum(p: ^I64; n: INT): I64;
20+code
21+ xor eax, eax;
22+ test rsi, rsi;
23+ jz done;
24+ again:
25+ add rax, [rdi];
26+ add rdi, 8;
27+ dec rsi;
28+ jnz again;
29+ done:
30+ ret;
31+end sum;
32+
33+(* the byte at p + i, zero-extended, and the byte there set to v *)
34+proc swapByte(p: ^U8; i: INT; v: U8): I64;
35+code
36+ movzx eax, byte [rdi + rsi];
37+ mov [rdi + rsi * 1], dl;
38+ ret;
39+end swapByte;
40+
41+(* a div b and a mod b, unsigned; two results in rax and rdx *)
42+proc udivmod(a, b: U64): (U64, U64);
43+code
44+ mov rax, rdi;
45+ xor edx, edx;
46+ div rsi;
47+ ret;
48+end udivmod;
49+
50+(* the larger of a and b, signed, by cmov; and whether a < b by setl *)
51+proc max(a, b: I64): I64;
52+code
53+ cmp rdi, rsi;
54+ mov rax, rdi;
55+ cmovl rax, rsi;
56+ ret;
57+end max;
58+
59+proc below(a, b: I64): BOOL;
60+code
61+ xor eax, eax;
62+ cmp rdi, rsi;
63+ setl al;
64+ ret;
65+end below;
66+
67+(* (x + y) * x, then its square root *)
68+proc real(x, y: F64): F64;
69+code
70+ movsd xmm2, xmm0;
71+ addsd xmm0, xmm1;
72+ mulsd xmm0, xmm2;
73+ sqrtsd xmm0, xmm0;
74+ ret;
75+end real;
76+
77+(* -a, ~a and a * 3 + 1 for an I32, through eax *)
78+proc mix(a: I32): I64;
79+code
80+ mov eax, edi;
81+ neg eax;
82+ not eax;
83+ imul eax, eax, 3;
84+ inc eax;
85+ movsxd rax, eax;
86+ ret;
87+end mix;
88+
89+(* XADD by hand: the old value of p^, which goes up by v *)
90+proc fetchAdd(p: ^INT; v: INT): INT;
91+code
92+ mov rax, rsi;
93+ lock xadd [rdi], rax;
94+ ret;
95+end fetchAdd;
96+
97+(* the real 2.0, from its bits, through a general register *)
98+proc two: F64;
99+code
100+ mov rax, 4000000000000000H;
101+ movq xmm0, rax;
102+ ret;
103+end two;
104+
105+(* bytes the assembler is given as they are: mov eax, 42; ret *)
106+proc answer: I64;
107+code
108+ db 0B8H, 42, 0, 0, 0;
109+ db 0C3H;
110+end answer;
111+
112+proc main*;
113+var a: [4]I64; s: [3]U8; q, r: U64; c: INT;
114+begin
115+ Out.int(tens(4, 2)); Out.ln;
116+ a := [1, 20, 300, 4000];
117+ Out.int(sum(ADR(a[0]), 4)); Out.str(" "); Out.int(sum(ADR(a[0]), 0)); Out.ln;
118+ s := "abc";
119+ Out.int(swapByte(ADR(s[0]), 1, 'z')); Out.str(" "); Out.str(s[0..3]); Out.ln;
120+ q, r := udivmod(1000, 7);
121+ Out.int(I64(q)); Out.str(" "); Out.int(I64(r)); Out.ln;
122+ Out.int(max(-5, 3)); Out.str(" "); Out.int(max(9, 3)); Out.str(" ");
123+ if below(-1, 1) and not below(1, -1) then Out.str("below"); end;
124+ Out.ln;
125+ Out.int(I64(real(1.0, 3.0))); Out.ln;
126+ Out.int(mix(5)); Out.ln;
127+ c := 40;
128+ Out.int(fetchAdd(ADR(c), 2)); Out.str(" "); Out.int(c); Out.ln;
129+ Out.int(I64(two() * 21.0)); Out.str(" "); Out.int(answer()); Out.ln;
130+end;
131+
132+end Asm.

test/run/Atomic.raz (history) modified +5 -3

@@ -4,9 +4,11 @@
44 var counter: INT; flag: UINT; p: ^INT;
55
66 proc add3*(a, b, c: I64): I64;
7-code 48H, 8DH, 04H, 37H, (* lea rax, [rdi+rsi] *)
8- 48H, 01H, 0D0H, (* add rax, rdx *)
9- 0C3H; (* ret *)
7+code
8+ lea rax, [rdi + rsi];
9+ add rax, rdx;
10+ ret;
11+end add3;
1012
1113 proc main*;
1214 var old: INT; ok: BOOL; v: ^VOLATILE INT; x: INT;

test/run/Edge.raz (history) modified +5 -3

@@ -10,9 +10,11 @@
1010 (* rsp mod 16 at the call, as the callee sees it after its return
1111 address: always 8 when the caller keeps the stack aligned *)
1212 proc rspmod: I64;
13-code 48H, 89H, 0E0H, (* mov rax, rsp *)
14- 48H, 83H, 0E0H, 0FH, (* and rax, 15 *)
15- 0C3H;
13+code
14+ mov rax, rsp;
15+ and rax, 15;
16+ ret;
17+end rspmod;
1618
1719 proc next: I64;
1820 begin

test/run/Results.raz (history) modified +10 -7

@@ -32,13 +32,16 @@
3232 end;
3333
3434 proc minmax(a, b: I64): (I64, I64);
35-code 48H, 89H, 0F8H, (* mov rax, rdi *)
36- 48H, 89H, 0F2H, (* mov rdx, rsi *)
37- 48H, 39H, 0F7H, (* cmp rdi, rsi *)
38- 7EH, 06H, (* jle +6 *)
39- 48H, 89H, 0F0H, (* mov rax, rsi *)
40- 48H, 89H, 0FAH, (* mov rdx, rdi *)
41- 0C3H;
35+code
36+ mov rax, rdi;
37+ mov rdx, rsi;
38+ cmp rdi, rsi;
39+ jle done;
40+ mov rax, rsi;
41+ mov rdx, rdi;
42+ done:
43+ ret;
44+end minmax;
4245
4346 proc nothing: (I64, ERROR);
4447 begin

test/run/Single.raz (history) modified +5 -2

@@ -15,9 +15,12 @@
1515 extern proc fabsf(x: F32): F32;
1616 extern proc sqrt(x: F64): F64;
1717
18-(* addss xmm0, xmm0; ret *)
18+(* x + x, by hand *)
1919 proc twice(x: F32): F32;
20-code 0F3H, 0FH, 58H, 0C0H, 0C3H;
20+code
21+ addss xmm0, xmm0;
22+ ret;
23+end twice;
2124
2225 proc dot(a, b: Vec): F32;
2326 begin

tools/Doc.raz (history) modified +4 -4

@@ -146,15 +146,15 @@
146146 return false;
147147 end;
148148
149-(* A procedure's body: to the end that closes its begin, the procedure
150- name after it and its ;. *)
149+(* A procedure's body: to the end that closes its begin or code, the
150+ procedure name after it and its ;. *)
151151 proc skipBody;
152152 var depth: INT; sawBegin: BOOL;
153153 begin
154154 depth := 0;
155155 sawBegin := false;
156156 while sc.kind # Scan.eof do
157- if is("begin") then
157+ if is("begin") or is("code") then
158158 if depth = 0 then sawBegin := true; end;
159159 INC(depth);
160160 elsif is("if") or is("while") or is("for") or is("match") or is("record") or is("variant") then
@@ -178,7 +178,7 @@
178178 exp := name();
179179 stop := toSemicolon();
180180 next;
181- if is("forward") or is("code") then toSemicolon();
181+ if is("forward") then toSemicolon();
182182 else skipBody;
183183 end;
184184 declaration("", exp, start, ds, de, stop);

tools/Pretty.raz (history) modified +3 -3

@@ -8,8 +8,8 @@
88
99 Every line break and comment stays where it is. A line is indented
1010 4 spaces for each block it stands in: the statements of if, while,
11- for, repeat and begin, the fields of a record or variant, the
12- declarations of const, type and var. end, until, else, elsif and a
11+ for, repeat and begin, the instructions of code, the fields of a
12+ record or variant, the declarations of const, type and var. end, until, else, elsif and a
1313 match's arms stand at the level of the statement that opened them;
1414 module, import, proc, the sections and begin at the margin, with the
1515 comment just before them. A line that goes on with the expression of
@@ -25,7 +25,7 @@
2525
2626 const
2727 width = 4; (* the spaces of a level *)
28- openers = "if while for match record variant begin repeat";
28+ openers = "if while for match record variant begin repeat code";
2929 declarations = "module import link extern proc const type var begin code";
3030 binaryWords = "div mod and or xor shl shr in";
3131 binarySymbols = "+ - * / & | = # < <= > >= :=";