From a072a400a959492392f5b26274c208e7b386717d Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Mon, 26 Oct 2026 17:26:29 +0000 Subject: [PATCH] Assembler: a procedure or a module variable stands for its address in call p, jmp p and lea r, [v] --- diff --git a/doc/compiler.md b/doc/compiler.md index d5cb2ce..700e038 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -1194,6 +1194,42 @@ shown, and the compile goes on to its end. `RegNames` and `CondNames` are the names of the registers and conditions. +*Names in the assembler.* A procedure or a module variable, of the +module or exported by an import, stands for its address in `call p`, +`jmp p` and `lea r, [v]` (report, 8). The parser hands in a second +function beside the `ConstFn`, a `NameFn`, `asmName`: it looks the +name up, bare or qualified by `qualIdent` (which refuses another +module's name that isn't exported, as anywhere), and for a procedure or +a module variable gives its chunk. For anything else it reads nothing +and gives -1, and the assembler reads a constant there as it always +did, so a constant still stands where a number does, and a type or an +unknown name has the error it had. C's names are refused: an `extern`'s +chunk holds C's address, not the thing. With the chunk, nothing new is +made: `call p` is `callChunk`, as Gen calls, `jmp p` its twin +`jmpChunk`, and `[v]` the operand `memRip(chunk, 0)`, rip-relative, as +Gen reaches a global; each a 32-bit field with a fixup that `Link.raz` +patches with the distance from the field's end, which is the +instruction's end for all three: + + call triple E8 rel32 fixup to triple + jmp AsmNamed.twice E9 rel32 fixup to AsmNamed.twice + lea r9, [total] 4C 8D 0D rel32 fixup to total + +So `markLive` follows them as it follows a call: what only a code +procedure names is kept. Every module variable is a bss chunk of its +own (nothing in raz gives one a value before the program runs, so there +is no data section to tell apart), reached the same way. A procedure +made in place (`Parse.inlined`) keeps its own code too, made before it +was known to only return, which the call reaches; `test/run/AsmName.raz` +calls one and checks each field's distance against the address lea and +a procedure value give. A label is found first, even one placed after +the jump, so `assemble` first reads the procedure's instructions +quietly (`findLabels`) and enters every label; `jmp p` is then a jump +to the label `p` if there is one. A label anywhere but after a jump, or +a name anywhere but in those three places, is an error (`NameOnly`): +an operand that is a name is marked `named`, with its place, and +`statement` checks where it stands once the operands are read. + *When an instruction joins the assembler.* The report leaves the instructions to the implementation, and `db` gives any other, which stays the answer for an instruction no program uses twice. An diff --git a/src/Gen.raz b/src/Gen.raz index b0a1711..72a1d1d 100644 --- a/src/Gen.raz +++ b/src/Gen.raz @@ -2793,10 +2793,10 @@ (* A code procedure: its instructions, read by the assembler (X64), are the whole of chunk c *) -proc codeProc*(c: INT; constExpr: X.ConstFn): ERROR; +proc codeProc*(c: INT; constExpr: X.ConstFn; nameFn: X.NameFn): ERROR; begin Chunk.beginChunk(c); - try X.assemble(constExpr); + try X.assemble(constExpr, nameFn); Chunk.endChunk(c); return nil; end; diff --git a/src/Parse.raz b/src/Parse.raz index 0064052..8187b71 100644 --- a/src/Parse.raz +++ b/src/Parse.raz @@ -3489,6 +3489,32 @@ return constValue(x); end; +(* A procedure or a module variable named in a code procedure, for the + assembler: it stands for its address (report section 8). Its chunk, + the name read, bare or qualified as any other; -1, with nothing read, + if the name at the scan is anything else, for the assembler to read as + a constant. Being named, it is used, and the linker keeps it, as it + keeps what a call reaches: by the fixup to its chunk. *) +proc asmName: (INT, ERROR); +var o: PObj; st: Scan.State; at: Diag.Pos; +begin + o := Sym.lookup(Scan.id); + if (o = nil) or ((o^.cls # Sym.oModule) and (o^.cls # Sym.oProc) and (o^.cls # Sym.oVar)) then + return -1, nil; + end; + Scan.saveScan(st); + at := Scan.tokPos(); + o := try qualIdent(); + if (o^.cls = Sym.oProc) or ((o^.cls = Sym.oVar) and (o^.lev = 0)) then + if o^.ext then + return -1, Scan.markAt(Scan.spanFrom(at), say1("{} is C's: a code procedure names only raz's procedures and variables", nameOf(o^.name))); + end; + return o^.chunk, nil; + end; + Scan.restoreScan(st); + return -1, nil; +end; + proc codeBody(p: PObj; sig: PType; at: Diag.Pos): ERROR; var q: PObj; ni, nf: INT; begin @@ -3520,7 +3546,7 @@ return Scan.markAt(at, "a code procedure returns a single value"); end; end; - try Gen.codeProc(p^.chunk, asmConst); + try Gen.codeProc(p^.chunk, asmConst, asmName); (* a square root of one real, of its own kind, is made in place *) q := sig^.fields; if (q # nil) and (q^.next = nil) and not q^.isVar and (q^.typ^.form = Sym.fReal) diff --git a/src/X64.raz b/src/X64.raz index aa57d73..00c3722 100644 --- a/src/X64.raz +++ b/src/X64.raz @@ -48,6 +48,12 @@ whole expression, or, when whole is false, a single factor *) ConstFn* = proc(whole: BOOL): (I64, ERROR); + (* The parser's reader of a name in a code procedure that stands for an + address: a procedure or a module variable, bare or qualified. Its + chunk, the name read; or -1, nothing read, if the name at the scan + is anything else, which is then read as a constant *) + NameFn* = proc: (INT, ERROR); + (* The text of the instruction that starts at at, for the listing *) Said = record at: INT; @@ -175,7 +181,7 @@ from the end of the instruction, and the fixup takes it to end with these four bytes: an instruction with an immediate after them, aluMI's or storeI's, takes the immediate's size off disp - first (the assembler can't name a chunk). *) + first (the assembler names a chunk only in lea, which has none). *) Chunk.addFixup(Chunk.pc, m.chunk, m.disp); Chunk.put4(0); return; @@ -626,6 +632,15 @@ Chunk.put4(0); end; +(* jmp to the start of chunk c, as a tail call: E9 and the distance *) +proc jmpChunk*(c: INT); +begin + if listing then say(text1("jmp", Chunk.chunkName(c))); end; + Chunk.put1(0E9H); + Chunk.addFixup(Chunk.pc, c, 0); + Chunk.put4(0); +end; + proc callBack*(target: INT); begin if listing then say("call"); end; @@ -953,18 +968,26 @@ (* The assembler, for code procedures: x86-64 in Intel's syntax as NASM writes it (report, appendix A). It reads instructions with the scanner up to end, and encodes them with opM and opR; constants are - read by the parser's constExpr. *) + read by the parser's constExpr, and the names of procedures and + module variables by its nameFn, which gives their chunks: call p and + jmp p reach one by a fixup, as Gen's calls do, and lea r, [v] by a + rip-relative fixup, as Gen's globals are reached. *) const NoBase = -3; (* a memory operand's base, not yet read *) + (* the places a procedure's or variable's name may stand (report section 8) *) + NameOnly = "a procedure or a variable stands for its address only in call p, jmp p and lea r, [v]"; type - OpKind = (oReg, oXmm, oCr, oMem, oImm); + (* oName is a procedure or module variable, its chunk in m, for call *) + OpKind = (oReg, oXmm, oCr, oMem, oImm, oName); Operand = record kind: OpKind; r, size: INT; (* oReg, oXmm, oCr: the register and its size; oMem: its size, or 0 *) m: Mem; v: I64; (* oImm *) + named: BOOL; (* oName, or oMem as [v]: a procedure's or variable's address *) + at: Diag.Pos; (* where its name is, if named *) end; (* The current symbol as a word: an identifier, or a reserved word that @@ -1053,9 +1076,79 @@ return -1; end; -proc operand(var o: Operand; constExpr: ConstFn): ERROR; -var w: []const U8; r, size, sz: INT; st: Scan.State; v: I64; isR, wasMinus: BOOL; +var + (* the labels of the procedure *) + lnames, lplaces, lchains: [64]INT; + lusedAt: [64]Diag.Pos; + numLabels: INT; + +(* Which label is called n, or -1 if none is *) +proc labelIndex(n: INT): INT; +var i: INT; +begin + for i := 0 to numLabels - 1 do + if lnames[i] = n then return i; end; + end; + return -1; +end; + +(* The label called n, entered if new *) +proc lab(n: INT): (INT, ERROR); +var i: INT; begin + i := labelIndex(n); + if i >= 0 then return i, nil; end; + if numLabels > 63 then return 0, Scan.mark("more than 64 labels"); end; + lnames[numLabels] := n; lplaces[numLabels] := -1; lchains[numLabels] := NoLabel; + lusedAt[numLabels] := Scan.tokPos(); + numLabels := numLabels + 1; + return numLabels - 1, nil; +end; + +(* Every label of the procedure, entered before an instruction is read, + so that a name that is a label is the label wherever it is written, + before its place or after (report section 8: a label is found first). + A label is a word, at the start of an instruction, before a colon. The + instructions are read quietly here, as they are read again after. *) +proc findLabels; +var st: Scan.State; wasQuiet, start: BOOL; w: []const U8; +begin + Scan.saveScan(st); + wasQuiet := Diag.quiet; + Diag.setQuiet(true); + start := true; + while (Scan.tok # Scan.sEnd) and (Scan.tok # Scan.sEof) do + w := ""; + if start then w := asmWord(); end; + start := Scan.tok = Scan.sSemicolon; + Scan.next; + if (LEN(w) > 0) and (Scan.tok = Scan.sColon) and (numLabels < 64) then + _, _ := lab(Scan.intern(SYSTEM.PTR(w), LEN(w))); + start := true; + Scan.next; + end; + end; + Diag.setQuiet(wasQuiet); + Scan.restoreScan(st); +end; + +(* The address a name at the scan stands for: a procedure's or a module + variable's chunk, read by nameFn, its place noted in o.at; -1 if it is + none, with nothing read. A label is found first, and only a jump takes + one. *) +proc nameAt(var o: Operand; nameFn: NameFn): (INT, ERROR); +begin + if Scan.tok # Scan.sIdent then return -1, nil; end; + o.at := Scan.tokPos(); + if labelIndex(Scan.id) >= 0 then + return -1, Scan.mark(Diag.say("label {} is only for a jump", [Fmt.str(Scan.nameOf(Scan.id))])); + end; + return nameFn(); +end; + +proc operand(var o: Operand; constExpr: ConstFn; nameFn: NameFn): ERROR; +var w: []const U8; r, size, sz, c: INT; st: Scan.State; v: I64; isR, wasMinus: BOOL; +begin o := {}; w := asmWord(); sz := 0; @@ -1087,8 +1180,16 @@ o.m.index := r; o.m.scale := 1; end; else - v := try constExpr(true); - o.m.disp := o.m.disp + INT(v); + c := try nameAt(o, nameFn); + if c < 0 then + v := try constExpr(true); + o.m.disp := o.m.disp + INT(v); + elsif o.named then + return Scan.markAt(o.at, NameOnly); + else + o.named := true; + o.m.chunk := c; + end; end; if (Scan.tok # Scan.sPlus) and (Scan.tok # Scan.sMinus) then break; end; wasMinus := Scan.tok = Scan.sMinus; @@ -1097,6 +1198,13 @@ isR := isReg(asmWord(), r, size); if not isR and wasMinus then Scan.restoreScan(st); end; until false; + if o.named then + (* [v], whose address lea takes, alone, as statement checks *) + if (o.m.base # NoBase) or (o.m.index # NoIndex) or (o.m.disp # 0) then + return Scan.markAt(o.at, NameOnly); + end; + o.m := memRip(o.m.chunk, 0); + end; if (Scan.tok # Scan.sRbrak) or (o.m.base = NoBase) or not ((o.m.scale = 1) or (o.m.scale = 2) or (o.m.scale = 4) or (o.m.scale = 8)) then return Scan.mark("expected a memory operand, [base + index * scale + disp]"); @@ -1110,8 +1218,14 @@ if o.size = 16 then o.kind := oXmm; else o.kind := oReg; end; Scan.next; else - o.kind := oImm; - o.v := try constExpr(true); + c := try nameAt(o, nameFn); + if c >= 0 then + o.kind := oName; o.named := true; + o.m := memRip(c, 0); + else + o.kind := oImm; + o.v := try constExpr(true); + end; end; return nil; end; @@ -1174,25 +1288,6 @@ asize, apfx: INT; awide, abyte: BOOL; aat: Diag.Pos; (* where it starts *) - (* the labels of the procedure *) - lnames, lplaces, lchains: [64]INT; - lusedAt: [64]Diag.Pos; - numLabels: INT; - -(* The label called n, entered if new *) -proc lab(n: INT): (INT, ERROR); -var i: INT; -begin - for i := 0 to numLabels - 1 do - if lnames[i] = n then return i, nil; end; - end; - if numLabels > 63 then return 0, Scan.mark("more than 64 labels"); end; - lnames[numLabels] := n; lplaces[numLabels] := -1; lchains[numLabels] := NoLabel; - lusedAt[numLabels] := Scan.tokPos(); - numLabels := numLabels + 1; - return numLabels - 1, nil; -end; - proc bad: ERROR; begin return Scan.markAt(aat, Diag.say("can't assemble {} with these operands", [Fmt.str(aw)])); @@ -1344,7 +1439,13 @@ if aw = "push" then push(ao[0].r); else pop(ao[0].r); end; elsif (aw = "call") or (aw = "jmp") then if (an # 1) or (ao[0].kind = oImm) then return bad(); end; - if aw = "call" then rm(0, false, 0FFH, 2, ao[0], false); else rm(0, false, 0FFH, 4, ao[0], false); end; + if ao[0].kind = oName then + callChunk(ao[0].m.chunk); (* call p, as Gen's calls; jmp p is read as a jump *) + elsif aw = "call" then + rm(0, false, 0FFH, 2, ao[0], false); + else + rm(0, false, 0FFH, 4, ao[0], false); + end; elsif Str.hasPrefix(aw, "set") and (cond(part(aw, 3, 3)) >= 0) then if (an # 1) or (ao[0].kind = oImm) or (asize # 1) then return bad(); end; rm(0, false, 900FH + (cond(part(aw, 3, 3)) shl 8), 0, ao[0], true); @@ -1475,9 +1576,9 @@ end; (* One instruction or label, as assemble reads it *) -proc statement(constExpr: ConstFn): ERROR; +proc statement(constExpr: ConstFn; nameFn: NameFn): ERROR; var - i, k, cc, first: INT; wasListing: BOOL; start: Diag.Pos; v: I64; + i, k, c, cc, first: INT; wasListing: BOOL; start, at: Diag.Pos; v: I64; begin aat := Scan.tokPos(); aw := asmWord(); @@ -1515,28 +1616,46 @@ Scan.next; until false; elsif (aw[0] = 'j') and ((aw = "jmp") or (cond(part(aw, 1, 3)) >= 0)) and (Scan.tok = Scan.sIdent) and not atRegister() then - (* a jump to a label *) - k := try lab(Scan.id); - Scan.next; + (* a jump to a label, which is found first; else jmp p, to a + procedure (a tail call) or a variable, by nameFn; else a label + still, which isn't defined *) if aw # "jmp" then cc := cond(part(aw, 1, 3)); end; - if lplaces[k] >= 0 then - if cc < 0 then jmpBack(lplaces[k]); else jccBack(cc, lplaces[k]); end; - elsif cc < 0 then - jmp(lchains[k]); + c := -1; + if labelIndex(Scan.id) < 0 then + at := Scan.tokPos(); + c := try nameFn(); + end; + if c >= 0 then + if cc >= 0 then return Scan.markAt(at, NameOnly); end; + jmpChunk(c); else - jcc(cc, lchains[k]); + k := try lab(Scan.id); + Scan.next; + if lplaces[k] >= 0 then + if cc < 0 then jmpBack(lplaces[k]); else jccBack(cc, lplaces[k]); end; + elsif cc < 0 then + jmp(lchains[k]); + else + jcc(cc, lchains[k]); + end; end; else an := 0; if Scan.tok # Scan.sSemicolon then repeat if an > 2 then return Scan.mark("too many operands"); end; - try operand(ao[an], constExpr); + try operand(ao[an], constExpr, nameFn); INC(an); if Scan.tok # Scan.sComma then break; end; Scan.next; until false; end; + (* a name stands only in call p and lea r, [v]; jmp p is read above *) + for i := 0 to an - 1 do + if ao[i].named and not (((aw = "call") and (ao[i].kind = oName)) or ((aw = "lea") and (i = 1))) then + return Scan.markAt(ao[i].at, NameOnly); + end; + end; (* the size: a register's, or what the memory operand says *) asize := 8; for i := an - 1 to 0 by -1 do @@ -1557,15 +1676,16 @@ return nil; end; -proc assemble*(constExpr: ConstFn): ERROR; +proc assemble*(constExpr: ConstFn; nameFn: NameFn): ERROR; var k: INT; begin numLabels := 0; + findLabels(); while (Scan.tok # Scan.sEnd) and (Scan.tok # Scan.sEof) do if Scan.tok = Scan.sSemicolon then Scan.next; else - try statement(constExpr); + try statement(constExpr, nameFn); end; end; for k := 0 to numLabels - 1 do diff --git a/test/err/AsmNameHidden.err b/test/err/AsmNameHidden.err new file mode 100644 index 0000000..e00abb2 --- /dev/null +++ b/test/err/AsmNameHidden.err @@ -0,0 +1,7 @@ +test/err/AsmNameHidden.raz:9:18: error: hidden isn't exported by AsmNamed + 9 | jmp AsmNamed.hidden; + | ^^^^^^ +test/run/mods/AsmNamed.raz:22:6: note: hidden is declared here, without a * + 22 | proc hidden(x: I64): I64; + | ^^^^^^ +raz: 1 error diff --git a/test/err/AsmNameHidden.raz b/test/err/AsmNameHidden.raz new file mode 100644 index 0000000..4e3aea7 --- /dev/null +++ b/test/err/AsmNameHidden.raz @@ -0,0 +1,12 @@ +module AsmNameHidden; +(* Another module's procedure that isn't exported can't be named, in a + code procedure as anywhere *) +import AsmNamed; +import SYSTEM; + +proc f(x: I64): I64; +code + jmp AsmNamed.hidden; +end f; + +end AsmNameHidden. diff --git a/test/err/AsmNameWhere.err b/test/err/AsmNameWhere.err new file mode 100644 index 0000000..923facb --- /dev/null +++ b/test/err/AsmNameWhere.err @@ -0,0 +1,28 @@ +test/err/AsmNameWhere.raz:18:14: error: a procedure or a variable stands for its address only in call p, jmp p and lea r, [v] + 18 | mov rax, p; + | ^ +test/err/AsmNameWhere.raz:24:15: error: a procedure or a variable stands for its address only in call p, jmp p and lea r, [v] + 24 | mov rax, [g]; + | ^ +test/err/AsmNameWhere.raz:30:8: error: a procedure or a variable stands for its address only in call p, jmp p and lea r, [v] + 30 | jz p; + | ^ +test/err/AsmNameWhere.raz:36:15: error: a procedure or a variable stands for its address only in call p, jmp p and lea r, [v] + 36 | lea rax, [g + 8]; + | ^ +test/err/AsmNameWhere.raz:42:10: error: expected a constant + 42 | call T; + | ^ +test/err/AsmNameWhere.raz:48:5: error: can't assemble call with these operands + 48 | call K; + | ^^^^ +test/err/AsmNameWhere.raz:54:10: error: nowhere isn't declared + 54 | call nowhere; + | ^^^^^^^ +test/err/AsmNameWhere.raz:60:10: error: puts is C's: a code procedure names only raz's procedures and variables + 60 | call puts; + | ^^^^ +test/err/AsmNameWhere.raz:67:15: error: label here is only for a jump + 67 | lea rax, [here]; + | ^^^^ +raz: 9 errors diff --git a/test/err/AsmNameWhere.raz b/test/err/AsmNameWhere.raz new file mode 100644 index 0000000..38966cb --- /dev/null +++ b/test/err/AsmNameWhere.raz @@ -0,0 +1,83 @@ +module AsmNameWhere; +(* A procedure or a variable stands for its address only in call p, + jmp p and lea r, [v]; a type, a constant or an unknown name is no + procedure, nor is C's, and a label is only for a jump *) +import SYSTEM; + +type T = I64; +const K = 64; +var g: I64; +extern proc puts(s: ^U8): I32; + +proc p; +begin +end; + +proc movName; +code + mov rax, p; + ret; +end movName; + +proc load; +code + mov rax, [g]; + ret; +end load; + +proc condJump; +code + jz p; + ret; +end condJump; + +proc offset; +code + lea rax, [g + 8]; + ret; +end offset; + +proc callType; +code + call T; + ret; +end callType; + +proc callConst; +code + call K; + ret; +end callConst; + +proc callUnknown; +code + call nowhere; + ret; +end callUnknown; + +proc callC; +code + call puts; + ret; +end callC; + +proc leaLabel; +code + here: + lea rax, [here]; + ret; +end leaLabel; + +(* a constant still stands where a number does *) +proc constant: I64; +code + mov rax, K; + ret; +end constant; + +proc main*; +begin + g := constant(); +end; + +end AsmNameWhere. diff --git a/test/run/AsmName.expect b/test/run/AsmName.expect new file mode 100644 index 0000000..b377eb1 --- /dev/null +++ b/test/run/AsmName.expect @@ -0,0 +1,16 @@ +17 +16 +42 +55 +2 +12 12 112 +lea of a procedure is its address: yes +and of another module's: yes +call is E8: yes +call reaches triple: yes +jmp is E9: yes +jmp reaches AsmNamed.twice: yes +lea is 48 8D 05: yes +lea reaches total: yes +lea rcx is 48 8D 0D: yes +lea reaches AsmNamed.count: yes diff --git a/test/run/AsmName.raz b/test/run/AsmName.raz new file mode 100644 index 0000000..fca7ff5 --- /dev/null +++ b/test/run/AsmName.raz @@ -0,0 +1,139 @@ +module AsmName; +(* Procedures and module variables named in code procedures, where + they stand for their addresses (report section 8): call p, jmp p and + lea r, [v], of this module and of another. Each is run, and its + bytes are read back: the distance in call's, jmp's and lea's 32-bit + field must reach what lea and a procedure value say is there. *) +import Out, AsmNamed; +import SYSTEM; + +var total: I64; (* added to through its address, by lea *) + +(* x * 3 + 1: only returns, so a call of it in raz is made in place, + but its own code is still there for a code procedure to call *) +proc triple(x: I64): I64; +begin + return x * 3 + 1; +end; + +(* 1 + 2 + ... + n, reached only from sumTo's code: kept all the same *) +proc onlyFromCode(n: I64): I64; +var i, s: I64; +begin + s := 0; + for i := 1 to n do s := s + i; end; + return s; +end; + +(* triple(x) + 1: x is in rdi already, as System V passes it, and rsp + is kept a multiple of 16 at the call *) +proc callTriple(x: I64): I64; +code + sub rsp, 8; + call triple; + add rsp, 8; + add rax, 1; + ret; +end callTriple; + +(* AsmNamed.twice(x), as a tail call: its ret returns to our caller *) +proc tailTwice(x: I64): I64; +code + jmp AsmNamed.twice; +end tailTwice; + +proc sumTo(n: I64): I64; +code + sub rsp, 8; + call onlyFromCode; + add rsp, 8; + ret; +end sumTo; + +(* adds x to total and to AsmNamed.count, through their addresses *) +proc addBoth(x: I64); +code + lea rax, [total]; + add [rax], rdi; + lea rcx, [AsmNamed.count]; + add [rcx], rdi; + ret; +end addBoth; + +(* total, read through its address *) +proc readTotal: I64; +code + lea r9, [total]; + mov rax, [r9]; + ret; +end readTotal; + +(* a label is found before a procedure of the same name *) +proc labelFirst: I64; +code + jmp triple; + mov eax, 1; + ret; + triple: + mov eax, 2; + ret; +end labelFirst; + +(* the address of triple, and of AsmNamed.twice *) +proc addresses: (^U8, ^U8); +code + lea rax, [triple]; + lea rdx, [AsmNamed.twice]; + ret; +end addresses; + +(* Whether the 32-bit field at b[at], counted from b[at + 4], reaches target *) +proc reaches(b: ^U8; at: INT; target: ^U8): BOOL; +var d: I32; t: ^U8; +begin + d := I32(INT(b[at]) | (INT(b[at + 1]) shl 8) | (INT(b[at + 2]) shl 16) | (INT(b[at + 3]) shl 24)); + t := ^U8(INT(b) + at + 4 + INT(d)); + return t = target; +end; + +(* yes or no *) +proc say(what: []const U8; ok: BOOL); +begin + Out.str(what); + if ok then Out.str(": yes"); else Out.str(": no"); end; + Out.ln; +end; + +proc main*; +var p: proc(x: I64): I64; q: proc(x: I64); b, t, w: ^U8; +begin + Out.int(callTriple(5)); Out.ln; + Out.int(triple(5)); Out.ln; + Out.int(tailTwice(21)); Out.ln; + Out.int(sumTo(10)); Out.ln; + Out.int(labelFirst()); Out.ln; + addBoth(7); + addBoth(5); + Out.int(readTotal()); Out.str(" "); Out.int(total); Out.str(" "); Out.int(AsmNamed.count); Out.ln; + t, w := addresses(); + p := triple; + say("lea of a procedure is its address", t = ^U8(p)); + p := AsmNamed.twice; + say("and of another module's", w = ^U8(p)); + p := callTriple; + b := ^U8(p); + say("call is E8", b[4] = 0E8H); + say("call reaches triple", reaches(b, 5, t)); + p := tailTwice; + b := ^U8(p); + say("jmp is E9", b[0] = 0E9H); + say("jmp reaches AsmNamed.twice", reaches(b, 1, w)); + q := addBoth; + b := ^U8(q); + say("lea is 48 8D 05", (b[0] = 48H) and (b[1] = 8DH) and (b[2] = 05H)); + say("lea reaches total", reaches(b, 3, ^U8(SYSTEM.ADR(total)))); + say("lea rcx is 48 8D 0D", (b[10] = 48H) and (b[11] = 8DH) and (b[12] = 0DH)); + say("lea reaches AsmNamed.count", reaches(b, 13, ^U8(AsmNamed.countAt()))); +end; + +end AsmName. diff --git a/test/run/mods/AsmNamed.raz b/test/run/mods/AsmNamed.raz new file mode 100644 index 0000000..975d175 --- /dev/null +++ b/test/run/mods/AsmNamed.raz @@ -0,0 +1,29 @@ +(* What AsmName's code procedures name in another module: an exported + procedure and variable, and a procedure that isn't exported, which + test/err/AsmNameHidden may not name. *) +module AsmNamed; +import SYSTEM; + +var count*: I64; (* added to through its address, by lea *) + +(* x * 2, the target of a tail call *) +proc twice*(x: I64): I64; +begin + return x * 2; +end; + +(* count's address, as raz takes it *) +proc countAt*: ^I64; +begin + return SYSTEM.ADR(count); +end; + +(* not exported: another module's code may not name it *) +proc hidden(x: I64): I64; +begin + return x; +end; + +begin + count := hidden(100); +end AsmNamed.