Assembler: a procedure or a module variable stands for its address in call p, jmp p and lea r, [v]
| @@ -1194,6 +1194,42 @@ | ||
| 1194 | 1194 | shown, and the compile goes on to its end. `RegNames` and `CondNames` |
| 1195 | 1195 | are the names of the registers and conditions. |
| 1196 | 1196 | |
| 1197 | +*Names in the assembler.* A procedure or a module variable, of the | |
| 1198 | +module or exported by an import, stands for its address in `call p`, | |
| 1199 | +`jmp p` and `lea r, [v]` (report, 8). The parser hands in a second | |
| 1200 | +function beside the `ConstFn`, a `NameFn`, `asmName`: it looks the | |
| 1201 | +name up, bare or qualified by `qualIdent` (which refuses another | |
| 1202 | +module's name that isn't exported, as anywhere), and for a procedure or | |
| 1203 | +a module variable gives its chunk. For anything else it reads nothing | |
| 1204 | +and gives -1, and the assembler reads a constant there as it always | |
| 1205 | +did, so a constant still stands where a number does, and a type or an | |
| 1206 | +unknown name has the error it had. C's names are refused: an `extern`'s | |
| 1207 | +chunk holds C's address, not the thing. With the chunk, nothing new is | |
| 1208 | +made: `call p` is `callChunk`, as Gen calls, `jmp p` its twin | |
| 1209 | +`jmpChunk`, and `[v]` the operand `memRip(chunk, 0)`, rip-relative, as | |
| 1210 | +Gen reaches a global; each a 32-bit field with a fixup that `Link.raz` | |
| 1211 | +patches with the distance from the field's end, which is the | |
| 1212 | +instruction's end for all three: | |
| 1213 | + | |
| 1214 | + call triple E8 rel32 fixup to triple | |
| 1215 | + jmp AsmNamed.twice E9 rel32 fixup to AsmNamed.twice | |
| 1216 | + lea r9, [total] 4C 8D 0D rel32 fixup to total | |
| 1217 | + | |
| 1218 | +So `markLive` follows them as it follows a call: what only a code | |
| 1219 | +procedure names is kept. Every module variable is a bss chunk of its | |
| 1220 | +own (nothing in raz gives one a value before the program runs, so there | |
| 1221 | +is no data section to tell apart), reached the same way. A procedure | |
| 1222 | +made in place (`Parse.inlined`) keeps its own code too, made before it | |
| 1223 | +was known to only return, which the call reaches; `test/run/AsmName.raz` | |
| 1224 | +calls one and checks each field's distance against the address lea and | |
| 1225 | +a procedure value give. A label is found first, even one placed after | |
| 1226 | +the jump, so `assemble` first reads the procedure's instructions | |
| 1227 | +quietly (`findLabels`) and enters every label; `jmp p` is then a jump | |
| 1228 | +to the label `p` if there is one. A label anywhere but after a jump, or | |
| 1229 | +a name anywhere but in those three places, is an error (`NameOnly`): | |
| 1230 | +an operand that is a name is marked `named`, with its place, and | |
| 1231 | +`statement` checks where it stands once the operands are read. | |
| 1232 | + | |
| 1197 | 1233 | *When an instruction joins the assembler.* The report leaves the |
| 1198 | 1234 | instructions to the implementation, and `db` gives any other, which |
| 1199 | 1235 | stays the answer for an instruction no program uses twice. An |
| @@ -2793,10 +2793,10 @@ | ||
| 2793 | 2793 | |
| 2794 | 2794 | (* A code procedure: its instructions, read by the assembler (X64), |
| 2795 | 2795 | are the whole of chunk c *) |
| 2796 | -proc codeProc*(c: INT; constExpr: X.ConstFn): ERROR; | |
| 2796 | +proc codeProc*(c: INT; constExpr: X.ConstFn; nameFn: X.NameFn): ERROR; | |
| 2797 | 2797 | begin |
| 2798 | 2798 | Chunk.beginChunk(c); |
| 2799 | - try X.assemble(constExpr); | |
| 2799 | + try X.assemble(constExpr, nameFn); | |
| 2800 | 2800 | Chunk.endChunk(c); |
| 2801 | 2801 | return nil; |
| 2802 | 2802 | end; |
| @@ -3489,6 +3489,32 @@ | ||
| 3489 | 3489 | return constValue(x); |
| 3490 | 3490 | end; |
| 3491 | 3491 | |
| 3492 | +(* A procedure or a module variable named in a code procedure, for the | |
| 3493 | + assembler: it stands for its address (report section 8). Its chunk, | |
| 3494 | + the name read, bare or qualified as any other; -1, with nothing read, | |
| 3495 | + if the name at the scan is anything else, for the assembler to read as | |
| 3496 | + a constant. Being named, it is used, and the linker keeps it, as it | |
| 3497 | + keeps what a call reaches: by the fixup to its chunk. *) | |
| 3498 | +proc asmName: (INT, ERROR); | |
| 3499 | +var o: PObj; st: Scan.State; at: Diag.Pos; | |
| 3500 | +begin | |
| 3501 | + o := Sym.lookup(Scan.id); | |
| 3502 | + if (o = nil) or ((o^.cls # Sym.oModule) and (o^.cls # Sym.oProc) and (o^.cls # Sym.oVar)) then | |
| 3503 | + return -1, nil; | |
| 3504 | + end; | |
| 3505 | + Scan.saveScan(st); | |
| 3506 | + at := Scan.tokPos(); | |
| 3507 | + o := try qualIdent(); | |
| 3508 | + if (o^.cls = Sym.oProc) or ((o^.cls = Sym.oVar) and (o^.lev = 0)) then | |
| 3509 | + if o^.ext then | |
| 3510 | + return -1, Scan.markAt(Scan.spanFrom(at), say1("{} is C's: a code procedure names only raz's procedures and variables", nameOf(o^.name))); | |
| 3511 | + end; | |
| 3512 | + return o^.chunk, nil; | |
| 3513 | + end; | |
| 3514 | + Scan.restoreScan(st); | |
| 3515 | + return -1, nil; | |
| 3516 | +end; | |
| 3517 | + | |
| 3492 | 3518 | proc codeBody(p: PObj; sig: PType; at: Diag.Pos): ERROR; |
| 3493 | 3519 | var q: PObj; ni, nf: INT; |
| 3494 | 3520 | begin |
| @@ -3520,7 +3546,7 @@ | ||
| 3520 | 3546 | return Scan.markAt(at, "a code procedure returns a single value"); |
| 3521 | 3547 | end; |
| 3522 | 3548 | end; |
| 3523 | - try Gen.codeProc(p^.chunk, asmConst); | |
| 3549 | + try Gen.codeProc(p^.chunk, asmConst, asmName); | |
| 3524 | 3550 | (* a square root of one real, of its own kind, is made in place *) |
| 3525 | 3551 | q := sig^.fields; |
| 3526 | 3552 | if (q # nil) and (q^.next = nil) and not q^.isVar and (q^.typ^.form = Sym.fReal) |
| @@ -48,6 +48,12 @@ | ||
| 48 | 48 | whole expression, or, when whole is false, a single factor *) |
| 49 | 49 | ConstFn* = proc(whole: BOOL): (I64, ERROR); |
| 50 | 50 | |
| 51 | + (* The parser's reader of a name in a code procedure that stands for an | |
| 52 | + address: a procedure or a module variable, bare or qualified. Its | |
| 53 | + chunk, the name read; or -1, nothing read, if the name at the scan | |
| 54 | + is anything else, which is then read as a constant *) | |
| 55 | + NameFn* = proc: (INT, ERROR); | |
| 56 | + | |
| 51 | 57 | (* The text of the instruction that starts at at, for the listing *) |
| 52 | 58 | Said = record |
| 53 | 59 | at: INT; |
| @@ -175,7 +181,7 @@ | ||
| 175 | 181 | from the end of the instruction, and the fixup takes it to end |
| 176 | 182 | with these four bytes: an instruction with an immediate after |
| 177 | 183 | them, aluMI's or storeI's, takes the immediate's size off disp |
| 178 | - first (the assembler can't name a chunk). *) | |
| 184 | + first (the assembler names a chunk only in lea, which has none). *) | |
| 179 | 185 | Chunk.addFixup(Chunk.pc, m.chunk, m.disp); |
| 180 | 186 | Chunk.put4(0); |
| 181 | 187 | return; |
| @@ -626,6 +632,15 @@ | ||
| 626 | 632 | Chunk.put4(0); |
| 627 | 633 | end; |
| 628 | 634 | |
| 635 | +(* jmp to the start of chunk c, as a tail call: E9 and the distance *) | |
| 636 | +proc jmpChunk*(c: INT); | |
| 637 | +begin | |
| 638 | + if listing then say(text1("jmp", Chunk.chunkName(c))); end; | |
| 639 | + Chunk.put1(0E9H); | |
| 640 | + Chunk.addFixup(Chunk.pc, c, 0); | |
| 641 | + Chunk.put4(0); | |
| 642 | +end; | |
| 643 | + | |
| 629 | 644 | proc callBack*(target: INT); |
| 630 | 645 | begin |
| 631 | 646 | if listing then say("call"); end; |
| @@ -953,18 +968,26 @@ | ||
| 953 | 968 | (* The assembler, for code procedures: x86-64 in Intel's syntax as NASM |
| 954 | 969 | writes it (report, appendix A). It reads instructions with the |
| 955 | 970 | scanner up to end, and encodes them with opM and opR; constants are |
| 956 | - read by the parser's constExpr. *) | |
| 971 | + read by the parser's constExpr, and the names of procedures and | |
| 972 | + module variables by its nameFn, which gives their chunks: call p and | |
| 973 | + jmp p reach one by a fixup, as Gen's calls do, and lea r, [v] by a | |
| 974 | + rip-relative fixup, as Gen's globals are reached. *) | |
| 957 | 975 | |
| 958 | 976 | const |
| 959 | 977 | NoBase = -3; (* a memory operand's base, not yet read *) |
| 978 | + (* the places a procedure's or variable's name may stand (report section 8) *) | |
| 979 | + NameOnly = "a procedure or a variable stands for its address only in call p, jmp p and lea r, [v]"; | |
| 960 | 980 | |
| 961 | 981 | type |
| 962 | - OpKind = (oReg, oXmm, oCr, oMem, oImm); | |
| 982 | + (* oName is a procedure or module variable, its chunk in m, for call *) | |
| 983 | + OpKind = (oReg, oXmm, oCr, oMem, oImm, oName); | |
| 963 | 984 | Operand = record |
| 964 | 985 | kind: OpKind; |
| 965 | 986 | r, size: INT; (* oReg, oXmm, oCr: the register and its size; oMem: its size, or 0 *) |
| 966 | 987 | m: Mem; |
| 967 | 988 | v: I64; (* oImm *) |
| 989 | + named: BOOL; (* oName, or oMem as [v]: a procedure's or variable's address *) | |
| 990 | + at: Diag.Pos; (* where its name is, if named *) | |
| 968 | 991 | end; |
| 969 | 992 | |
| 970 | 993 | (* The current symbol as a word: an identifier, or a reserved word that |
| @@ -1053,9 +1076,79 @@ | ||
| 1053 | 1076 | return -1; |
| 1054 | 1077 | end; |
| 1055 | 1078 | |
| 1056 | -proc operand(var o: Operand; constExpr: ConstFn): ERROR; | |
| 1057 | -var w: []const U8; r, size, sz: INT; st: Scan.State; v: I64; isR, wasMinus: BOOL; | |
| 1079 | +var | |
| 1080 | + (* the labels of the procedure *) | |
| 1081 | + lnames, lplaces, lchains: [64]INT; | |
| 1082 | + lusedAt: [64]Diag.Pos; | |
| 1083 | + numLabels: INT; | |
| 1084 | + | |
| 1085 | +(* Which label is called n, or -1 if none is *) | |
| 1086 | +proc labelIndex(n: INT): INT; | |
| 1087 | +var i: INT; | |
| 1088 | +begin | |
| 1089 | + for i := 0 to numLabels - 1 do | |
| 1090 | + if lnames[i] = n then return i; end; | |
| 1091 | + end; | |
| 1092 | + return -1; | |
| 1093 | +end; | |
| 1094 | + | |
| 1095 | +(* The label called n, entered if new *) | |
| 1096 | +proc lab(n: INT): (INT, ERROR); | |
| 1097 | +var i: INT; | |
| 1058 | 1098 | begin |
| 1099 | + i := labelIndex(n); | |
| 1100 | + if i >= 0 then return i, nil; end; | |
| 1101 | + if numLabels > 63 then return 0, Scan.mark("more than 64 labels"); end; | |
| 1102 | + lnames[numLabels] := n; lplaces[numLabels] := -1; lchains[numLabels] := NoLabel; | |
| 1103 | + lusedAt[numLabels] := Scan.tokPos(); | |
| 1104 | + numLabels := numLabels + 1; | |
| 1105 | + return numLabels - 1, nil; | |
| 1106 | +end; | |
| 1107 | + | |
| 1108 | +(* Every label of the procedure, entered before an instruction is read, | |
| 1109 | + so that a name that is a label is the label wherever it is written, | |
| 1110 | + before its place or after (report section 8: a label is found first). | |
| 1111 | + A label is a word, at the start of an instruction, before a colon. The | |
| 1112 | + instructions are read quietly here, as they are read again after. *) | |
| 1113 | +proc findLabels; | |
| 1114 | +var st: Scan.State; wasQuiet, start: BOOL; w: []const U8; | |
| 1115 | +begin | |
| 1116 | + Scan.saveScan(st); | |
| 1117 | + wasQuiet := Diag.quiet; | |
| 1118 | + Diag.setQuiet(true); | |
| 1119 | + start := true; | |
| 1120 | + while (Scan.tok # Scan.sEnd) and (Scan.tok # Scan.sEof) do | |
| 1121 | + w := ""; | |
| 1122 | + if start then w := asmWord(); end; | |
| 1123 | + start := Scan.tok = Scan.sSemicolon; | |
| 1124 | + Scan.next; | |
| 1125 | + if (LEN(w) > 0) and (Scan.tok = Scan.sColon) and (numLabels < 64) then | |
| 1126 | + _, _ := lab(Scan.intern(SYSTEM.PTR(w), LEN(w))); | |
| 1127 | + start := true; | |
| 1128 | + Scan.next; | |
| 1129 | + end; | |
| 1130 | + end; | |
| 1131 | + Diag.setQuiet(wasQuiet); | |
| 1132 | + Scan.restoreScan(st); | |
| 1133 | +end; | |
| 1134 | + | |
| 1135 | +(* The address a name at the scan stands for: a procedure's or a module | |
| 1136 | + variable's chunk, read by nameFn, its place noted in o.at; -1 if it is | |
| 1137 | + none, with nothing read. A label is found first, and only a jump takes | |
| 1138 | + one. *) | |
| 1139 | +proc nameAt(var o: Operand; nameFn: NameFn): (INT, ERROR); | |
| 1140 | +begin | |
| 1141 | + if Scan.tok # Scan.sIdent then return -1, nil; end; | |
| 1142 | + o.at := Scan.tokPos(); | |
| 1143 | + if labelIndex(Scan.id) >= 0 then | |
| 1144 | + return -1, Scan.mark(Diag.say("label {} is only for a jump", [Fmt.str(Scan.nameOf(Scan.id))])); | |
| 1145 | + end; | |
| 1146 | + return nameFn(); | |
| 1147 | +end; | |
| 1148 | + | |
| 1149 | +proc operand(var o: Operand; constExpr: ConstFn; nameFn: NameFn): ERROR; | |
| 1150 | +var w: []const U8; r, size, sz, c: INT; st: Scan.State; v: I64; isR, wasMinus: BOOL; | |
| 1151 | +begin | |
| 1059 | 1152 | o := {}; |
| 1060 | 1153 | w := asmWord(); |
| 1061 | 1154 | sz := 0; |
| @@ -1087,8 +1180,16 @@ | ||
| 1087 | 1180 | o.m.index := r; o.m.scale := 1; |
| 1088 | 1181 | end; |
| 1089 | 1182 | else |
| 1090 | - v := try constExpr(true); | |
| 1091 | - o.m.disp := o.m.disp + INT(v); | |
| 1183 | + c := try nameAt(o, nameFn); | |
| 1184 | + if c < 0 then | |
| 1185 | + v := try constExpr(true); | |
| 1186 | + o.m.disp := o.m.disp + INT(v); | |
| 1187 | + elsif o.named then | |
| 1188 | + return Scan.markAt(o.at, NameOnly); | |
| 1189 | + else | |
| 1190 | + o.named := true; | |
| 1191 | + o.m.chunk := c; | |
| 1192 | + end; | |
| 1092 | 1193 | end; |
| 1093 | 1194 | if (Scan.tok # Scan.sPlus) and (Scan.tok # Scan.sMinus) then break; end; |
| 1094 | 1195 | wasMinus := Scan.tok = Scan.sMinus; |
| @@ -1097,6 +1198,13 @@ | ||
| 1097 | 1198 | isR := isReg(asmWord(), r, size); |
| 1098 | 1199 | if not isR and wasMinus then Scan.restoreScan(st); end; |
| 1099 | 1200 | until false; |
| 1201 | + if o.named then | |
| 1202 | + (* [v], whose address lea takes, alone, as statement checks *) | |
| 1203 | + if (o.m.base # NoBase) or (o.m.index # NoIndex) or (o.m.disp # 0) then | |
| 1204 | + return Scan.markAt(o.at, NameOnly); | |
| 1205 | + end; | |
| 1206 | + o.m := memRip(o.m.chunk, 0); | |
| 1207 | + end; | |
| 1100 | 1208 | if (Scan.tok # Scan.sRbrak) or (o.m.base = NoBase) |
| 1101 | 1209 | or not ((o.m.scale = 1) or (o.m.scale = 2) or (o.m.scale = 4) or (o.m.scale = 8)) then |
| 1102 | 1210 | return Scan.mark("expected a memory operand, [base + index * scale + disp]"); |
| @@ -1110,8 +1218,14 @@ | ||
| 1110 | 1218 | if o.size = 16 then o.kind := oXmm; else o.kind := oReg; end; |
| 1111 | 1219 | Scan.next; |
| 1112 | 1220 | else |
| 1113 | - o.kind := oImm; | |
| 1114 | - o.v := try constExpr(true); | |
| 1221 | + c := try nameAt(o, nameFn); | |
| 1222 | + if c >= 0 then | |
| 1223 | + o.kind := oName; o.named := true; | |
| 1224 | + o.m := memRip(c, 0); | |
| 1225 | + else | |
| 1226 | + o.kind := oImm; | |
| 1227 | + o.v := try constExpr(true); | |
| 1228 | + end; | |
| 1115 | 1229 | end; |
| 1116 | 1230 | return nil; |
| 1117 | 1231 | end; |
| @@ -1174,25 +1288,6 @@ | ||
| 1174 | 1288 | asize, apfx: INT; |
| 1175 | 1289 | awide, abyte: BOOL; |
| 1176 | 1290 | aat: Diag.Pos; (* where it starts *) |
| 1177 | - (* the labels of the procedure *) | |
| 1178 | - lnames, lplaces, lchains: [64]INT; | |
| 1179 | - lusedAt: [64]Diag.Pos; | |
| 1180 | - numLabels: INT; | |
| 1181 | - | |
| 1182 | -(* The label called n, entered if new *) | |
| 1183 | -proc lab(n: INT): (INT, ERROR); | |
| 1184 | -var i: INT; | |
| 1185 | -begin | |
| 1186 | - for i := 0 to numLabels - 1 do | |
| 1187 | - if lnames[i] = n then return i, nil; end; | |
| 1188 | - end; | |
| 1189 | - if numLabels > 63 then return 0, Scan.mark("more than 64 labels"); end; | |
| 1190 | - lnames[numLabels] := n; lplaces[numLabels] := -1; lchains[numLabels] := NoLabel; | |
| 1191 | - lusedAt[numLabels] := Scan.tokPos(); | |
| 1192 | - numLabels := numLabels + 1; | |
| 1193 | - return numLabels - 1, nil; | |
| 1194 | -end; | |
| 1195 | - | |
| 1196 | 1291 | proc bad: ERROR; |
| 1197 | 1292 | begin |
| 1198 | 1293 | return Scan.markAt(aat, Diag.say("can't assemble {} with these operands", [Fmt.str(aw)])); |
| @@ -1344,7 +1439,13 @@ | ||
| 1344 | 1439 | if aw = "push" then push(ao[0].r); else pop(ao[0].r); end; |
| 1345 | 1440 | elsif (aw = "call") or (aw = "jmp") then |
| 1346 | 1441 | if (an # 1) or (ao[0].kind = oImm) then return bad(); end; |
| 1347 | - if aw = "call" then rm(0, false, 0FFH, 2, ao[0], false); else rm(0, false, 0FFH, 4, ao[0], false); end; | |
| 1442 | + if ao[0].kind = oName then | |
| 1443 | + callChunk(ao[0].m.chunk); (* call p, as Gen's calls; jmp p is read as a jump *) | |
| 1444 | + elsif aw = "call" then | |
| 1445 | + rm(0, false, 0FFH, 2, ao[0], false); | |
| 1446 | + else | |
| 1447 | + rm(0, false, 0FFH, 4, ao[0], false); | |
| 1448 | + end; | |
| 1348 | 1449 | elsif Str.hasPrefix(aw, "set") and (cond(part(aw, 3, 3)) >= 0) then |
| 1349 | 1450 | if (an # 1) or (ao[0].kind = oImm) or (asize # 1) then return bad(); end; |
| 1350 | 1451 | rm(0, false, 900FH + (cond(part(aw, 3, 3)) shl 8), 0, ao[0], true); |
| @@ -1475,9 +1576,9 @@ | ||
| 1475 | 1576 | end; |
| 1476 | 1577 | |
| 1477 | 1578 | (* One instruction or label, as assemble reads it *) |
| 1478 | -proc statement(constExpr: ConstFn): ERROR; | |
| 1579 | +proc statement(constExpr: ConstFn; nameFn: NameFn): ERROR; | |
| 1479 | 1580 | var |
| 1480 | - i, k, cc, first: INT; wasListing: BOOL; start: Diag.Pos; v: I64; | |
| 1581 | + i, k, c, cc, first: INT; wasListing: BOOL; start, at: Diag.Pos; v: I64; | |
| 1481 | 1582 | begin |
| 1482 | 1583 | aat := Scan.tokPos(); |
| 1483 | 1584 | aw := asmWord(); |
| @@ -1515,28 +1616,46 @@ | ||
| 1515 | 1616 | Scan.next; |
| 1516 | 1617 | until false; |
| 1517 | 1618 | elsif (aw[0] = 'j') and ((aw = "jmp") or (cond(part(aw, 1, 3)) >= 0)) and (Scan.tok = Scan.sIdent) and not atRegister() then |
| 1518 | - (* a jump to a label *) | |
| 1519 | - k := try lab(Scan.id); | |
| 1520 | - Scan.next; | |
| 1619 | + (* a jump to a label, which is found first; else jmp p, to a | |
| 1620 | + procedure (a tail call) or a variable, by nameFn; else a label | |
| 1621 | + still, which isn't defined *) | |
| 1521 | 1622 | if aw # "jmp" then cc := cond(part(aw, 1, 3)); end; |
| 1522 | - if lplaces[k] >= 0 then | |
| 1523 | - if cc < 0 then jmpBack(lplaces[k]); else jccBack(cc, lplaces[k]); end; | |
| 1524 | - elsif cc < 0 then | |
| 1525 | - jmp(lchains[k]); | |
| 1623 | + c := -1; | |
| 1624 | + if labelIndex(Scan.id) < 0 then | |
| 1625 | + at := Scan.tokPos(); | |
| 1626 | + c := try nameFn(); | |
| 1627 | + end; | |
| 1628 | + if c >= 0 then | |
| 1629 | + if cc >= 0 then return Scan.markAt(at, NameOnly); end; | |
| 1630 | + jmpChunk(c); | |
| 1526 | 1631 | else |
| 1527 | - jcc(cc, lchains[k]); | |
| 1632 | + k := try lab(Scan.id); | |
| 1633 | + Scan.next; | |
| 1634 | + if lplaces[k] >= 0 then | |
| 1635 | + if cc < 0 then jmpBack(lplaces[k]); else jccBack(cc, lplaces[k]); end; | |
| 1636 | + elsif cc < 0 then | |
| 1637 | + jmp(lchains[k]); | |
| 1638 | + else | |
| 1639 | + jcc(cc, lchains[k]); | |
| 1640 | + end; | |
| 1528 | 1641 | end; |
| 1529 | 1642 | else |
| 1530 | 1643 | an := 0; |
| 1531 | 1644 | if Scan.tok # Scan.sSemicolon then |
| 1532 | 1645 | repeat |
| 1533 | 1646 | if an > 2 then return Scan.mark("too many operands"); end; |
| 1534 | - try operand(ao[an], constExpr); | |
| 1647 | + try operand(ao[an], constExpr, nameFn); | |
| 1535 | 1648 | INC(an); |
| 1536 | 1649 | if Scan.tok # Scan.sComma then break; end; |
| 1537 | 1650 | Scan.next; |
| 1538 | 1651 | until false; |
| 1539 | 1652 | end; |
| 1653 | + (* a name stands only in call p and lea r, [v]; jmp p is read above *) | |
| 1654 | + for i := 0 to an - 1 do | |
| 1655 | + if ao[i].named and not (((aw = "call") and (ao[i].kind = oName)) or ((aw = "lea") and (i = 1))) then | |
| 1656 | + return Scan.markAt(ao[i].at, NameOnly); | |
| 1657 | + end; | |
| 1658 | + end; | |
| 1540 | 1659 | (* the size: a register's, or what the memory operand says *) |
| 1541 | 1660 | asize := 8; |
| 1542 | 1661 | for i := an - 1 to 0 by -1 do |
| @@ -1557,15 +1676,16 @@ | ||
| 1557 | 1676 | return nil; |
| 1558 | 1677 | end; |
| 1559 | 1678 | |
| 1560 | -proc assemble*(constExpr: ConstFn): ERROR; | |
| 1679 | +proc assemble*(constExpr: ConstFn; nameFn: NameFn): ERROR; | |
| 1561 | 1680 | var k: INT; |
| 1562 | 1681 | begin |
| 1563 | 1682 | numLabels := 0; |
| 1683 | + findLabels(); | |
| 1564 | 1684 | while (Scan.tok # Scan.sEnd) and (Scan.tok # Scan.sEof) do |
| 1565 | 1685 | if Scan.tok = Scan.sSemicolon then |
| 1566 | 1686 | Scan.next; |
| 1567 | 1687 | else |
| 1568 | - try statement(constExpr); | |
| 1688 | + try statement(constExpr, nameFn); | |
| 1569 | 1689 | end; |
| 1570 | 1690 | end; |
| 1571 | 1691 | for k := 0 to numLabels - 1 do |
| @@ -0,0 +1,7 @@ | ||
| 1 | +test/err/AsmNameHidden.raz:9:18: error: hidden isn't exported by AsmNamed | |
| 2 | + 9 | jmp AsmNamed.hidden; | |
| 3 | + | ^^^^^^ | |
| 4 | +test/run/mods/AsmNamed.raz:22:6: note: hidden is declared here, without a * | |
| 5 | + 22 | proc hidden(x: I64): I64; | |
| 6 | + | ^^^^^^ | |
| 7 | +raz: 1 error |
| @@ -0,0 +1,12 @@ | ||
| 1 | +module AsmNameHidden; | |
| 2 | +(* Another module's procedure that isn't exported can't be named, in a | |
| 3 | + code procedure as anywhere *) | |
| 4 | +import AsmNamed; | |
| 5 | +import SYSTEM; | |
| 6 | + | |
| 7 | +proc f(x: I64): I64; | |
| 8 | +code | |
| 9 | + jmp AsmNamed.hidden; | |
| 10 | +end f; | |
| 11 | + | |
| 12 | +end AsmNameHidden. |
| @@ -0,0 +1,28 @@ | ||
| 1 | +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] | |
| 2 | + 18 | mov rax, p; | |
| 3 | + | ^ | |
| 4 | +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] | |
| 5 | + 24 | mov rax, [g]; | |
| 6 | + | ^ | |
| 7 | +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] | |
| 8 | + 30 | jz p; | |
| 9 | + | ^ | |
| 10 | +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] | |
| 11 | + 36 | lea rax, [g + 8]; | |
| 12 | + | ^ | |
| 13 | +test/err/AsmNameWhere.raz:42:10: error: expected a constant | |
| 14 | + 42 | call T; | |
| 15 | + | ^ | |
| 16 | +test/err/AsmNameWhere.raz:48:5: error: can't assemble call with these operands | |
| 17 | + 48 | call K; | |
| 18 | + | ^^^^ | |
| 19 | +test/err/AsmNameWhere.raz:54:10: error: nowhere isn't declared | |
| 20 | + 54 | call nowhere; | |
| 21 | + | ^^^^^^^ | |
| 22 | +test/err/AsmNameWhere.raz:60:10: error: puts is C's: a code procedure names only raz's procedures and variables | |
| 23 | + 60 | call puts; | |
| 24 | + | ^^^^ | |
| 25 | +test/err/AsmNameWhere.raz:67:15: error: label here is only for a jump | |
| 26 | + 67 | lea rax, [here]; | |
| 27 | + | ^^^^ | |
| 28 | +raz: 9 errors |
| @@ -0,0 +1,83 @@ | ||
| 1 | +module AsmNameWhere; | |
| 2 | +(* A procedure or a variable stands for its address only in call p, | |
| 3 | + jmp p and lea r, [v]; a type, a constant or an unknown name is no | |
| 4 | + procedure, nor is C's, and a label is only for a jump *) | |
| 5 | +import SYSTEM; | |
| 6 | + | |
| 7 | +type T = I64; | |
| 8 | +const K = 64; | |
| 9 | +var g: I64; | |
| 10 | +extern proc puts(s: ^U8): I32; | |
| 11 | + | |
| 12 | +proc p; | |
| 13 | +begin | |
| 14 | +end; | |
| 15 | + | |
| 16 | +proc movName; | |
| 17 | +code | |
| 18 | + mov rax, p; | |
| 19 | + ret; | |
| 20 | +end movName; | |
| 21 | + | |
| 22 | +proc load; | |
| 23 | +code | |
| 24 | + mov rax, [g]; | |
| 25 | + ret; | |
| 26 | +end load; | |
| 27 | + | |
| 28 | +proc condJump; | |
| 29 | +code | |
| 30 | + jz p; | |
| 31 | + ret; | |
| 32 | +end condJump; | |
| 33 | + | |
| 34 | +proc offset; | |
| 35 | +code | |
| 36 | + lea rax, [g + 8]; | |
| 37 | + ret; | |
| 38 | +end offset; | |
| 39 | + | |
| 40 | +proc callType; | |
| 41 | +code | |
| 42 | + call T; | |
| 43 | + ret; | |
| 44 | +end callType; | |
| 45 | + | |
| 46 | +proc callConst; | |
| 47 | +code | |
| 48 | + call K; | |
| 49 | + ret; | |
| 50 | +end callConst; | |
| 51 | + | |
| 52 | +proc callUnknown; | |
| 53 | +code | |
| 54 | + call nowhere; | |
| 55 | + ret; | |
| 56 | +end callUnknown; | |
| 57 | + | |
| 58 | +proc callC; | |
| 59 | +code | |
| 60 | + call puts; | |
| 61 | + ret; | |
| 62 | +end callC; | |
| 63 | + | |
| 64 | +proc leaLabel; | |
| 65 | +code | |
| 66 | + here: | |
| 67 | + lea rax, [here]; | |
| 68 | + ret; | |
| 69 | +end leaLabel; | |
| 70 | + | |
| 71 | +(* a constant still stands where a number does *) | |
| 72 | +proc constant: I64; | |
| 73 | +code | |
| 74 | + mov rax, K; | |
| 75 | + ret; | |
| 76 | +end constant; | |
| 77 | + | |
| 78 | +proc main*; | |
| 79 | +begin | |
| 80 | + g := constant(); | |
| 81 | +end; | |
| 82 | + | |
| 83 | +end AsmNameWhere. |
| @@ -0,0 +1,16 @@ | ||
| 1 | +17 | |
| 2 | +16 | |
| 3 | +42 | |
| 4 | +55 | |
| 5 | +2 | |
| 6 | +12 12 112 | |
| 7 | +lea of a procedure is its address: yes | |
| 8 | +and of another module's: yes | |
| 9 | +call is E8: yes | |
| 10 | +call reaches triple: yes | |
| 11 | +jmp is E9: yes | |
| 12 | +jmp reaches AsmNamed.twice: yes | |
| 13 | +lea is 48 8D 05: yes | |
| 14 | +lea reaches total: yes | |
| 15 | +lea rcx is 48 8D 0D: yes | |
| 16 | +lea reaches AsmNamed.count: yes |
| @@ -0,0 +1,139 @@ | ||
| 1 | +module AsmName; | |
| 2 | +(* Procedures and module variables named in code procedures, where | |
| 3 | + they stand for their addresses (report section 8): call p, jmp p and | |
| 4 | + lea r, [v], of this module and of another. Each is run, and its | |
| 5 | + bytes are read back: the distance in call's, jmp's and lea's 32-bit | |
| 6 | + field must reach what lea and a procedure value say is there. *) | |
| 7 | +import Out, AsmNamed; | |
| 8 | +import SYSTEM; | |
| 9 | + | |
| 10 | +var total: I64; (* added to through its address, by lea *) | |
| 11 | + | |
| 12 | +(* x * 3 + 1: only returns, so a call of it in raz is made in place, | |
| 13 | + but its own code is still there for a code procedure to call *) | |
| 14 | +proc triple(x: I64): I64; | |
| 15 | +begin | |
| 16 | + return x * 3 + 1; | |
| 17 | +end; | |
| 18 | + | |
| 19 | +(* 1 + 2 + ... + n, reached only from sumTo's code: kept all the same *) | |
| 20 | +proc onlyFromCode(n: I64): I64; | |
| 21 | +var i, s: I64; | |
| 22 | +begin | |
| 23 | + s := 0; | |
| 24 | + for i := 1 to n do s := s + i; end; | |
| 25 | + return s; | |
| 26 | +end; | |
| 27 | + | |
| 28 | +(* triple(x) + 1: x is in rdi already, as System V passes it, and rsp | |
| 29 | + is kept a multiple of 16 at the call *) | |
| 30 | +proc callTriple(x: I64): I64; | |
| 31 | +code | |
| 32 | + sub rsp, 8; | |
| 33 | + call triple; | |
| 34 | + add rsp, 8; | |
| 35 | + add rax, 1; | |
| 36 | + ret; | |
| 37 | +end callTriple; | |
| 38 | + | |
| 39 | +(* AsmNamed.twice(x), as a tail call: its ret returns to our caller *) | |
| 40 | +proc tailTwice(x: I64): I64; | |
| 41 | +code | |
| 42 | + jmp AsmNamed.twice; | |
| 43 | +end tailTwice; | |
| 44 | + | |
| 45 | +proc sumTo(n: I64): I64; | |
| 46 | +code | |
| 47 | + sub rsp, 8; | |
| 48 | + call onlyFromCode; | |
| 49 | + add rsp, 8; | |
| 50 | + ret; | |
| 51 | +end sumTo; | |
| 52 | + | |
| 53 | +(* adds x to total and to AsmNamed.count, through their addresses *) | |
| 54 | +proc addBoth(x: I64); | |
| 55 | +code | |
| 56 | + lea rax, [total]; | |
| 57 | + add [rax], rdi; | |
| 58 | + lea rcx, [AsmNamed.count]; | |
| 59 | + add [rcx], rdi; | |
| 60 | + ret; | |
| 61 | +end addBoth; | |
| 62 | + | |
| 63 | +(* total, read through its address *) | |
| 64 | +proc readTotal: I64; | |
| 65 | +code | |
| 66 | + lea r9, [total]; | |
| 67 | + mov rax, [r9]; | |
| 68 | + ret; | |
| 69 | +end readTotal; | |
| 70 | + | |
| 71 | +(* a label is found before a procedure of the same name *) | |
| 72 | +proc labelFirst: I64; | |
| 73 | +code | |
| 74 | + jmp triple; | |
| 75 | + mov eax, 1; | |
| 76 | + ret; | |
| 77 | + triple: | |
| 78 | + mov eax, 2; | |
| 79 | + ret; | |
| 80 | +end labelFirst; | |
| 81 | + | |
| 82 | +(* the address of triple, and of AsmNamed.twice *) | |
| 83 | +proc addresses: (^U8, ^U8); | |
| 84 | +code | |
| 85 | + lea rax, [triple]; | |
| 86 | + lea rdx, [AsmNamed.twice]; | |
| 87 | + ret; | |
| 88 | +end addresses; | |
| 89 | + | |
| 90 | +(* Whether the 32-bit field at b[at], counted from b[at + 4], reaches target *) | |
| 91 | +proc reaches(b: ^U8; at: INT; target: ^U8): BOOL; | |
| 92 | +var d: I32; t: ^U8; | |
| 93 | +begin | |
| 94 | + d := I32(INT(b[at]) | (INT(b[at + 1]) shl 8) | (INT(b[at + 2]) shl 16) | (INT(b[at + 3]) shl 24)); | |
| 95 | + t := ^U8(INT(b) + at + 4 + INT(d)); | |
| 96 | + return t = target; | |
| 97 | +end; | |
| 98 | + | |
| 99 | +(* yes or no *) | |
| 100 | +proc say(what: []const U8; ok: BOOL); | |
| 101 | +begin | |
| 102 | + Out.str(what); | |
| 103 | + if ok then Out.str(": yes"); else Out.str(": no"); end; | |
| 104 | + Out.ln; | |
| 105 | +end; | |
| 106 | + | |
| 107 | +proc main*; | |
| 108 | +var p: proc(x: I64): I64; q: proc(x: I64); b, t, w: ^U8; | |
| 109 | +begin | |
| 110 | + Out.int(callTriple(5)); Out.ln; | |
| 111 | + Out.int(triple(5)); Out.ln; | |
| 112 | + Out.int(tailTwice(21)); Out.ln; | |
| 113 | + Out.int(sumTo(10)); Out.ln; | |
| 114 | + Out.int(labelFirst()); Out.ln; | |
| 115 | + addBoth(7); | |
| 116 | + addBoth(5); | |
| 117 | + Out.int(readTotal()); Out.str(" "); Out.int(total); Out.str(" "); Out.int(AsmNamed.count); Out.ln; | |
| 118 | + t, w := addresses(); | |
| 119 | + p := triple; | |
| 120 | + say("lea of a procedure is its address", t = ^U8(p)); | |
| 121 | + p := AsmNamed.twice; | |
| 122 | + say("and of another module's", w = ^U8(p)); | |
| 123 | + p := callTriple; | |
| 124 | + b := ^U8(p); | |
| 125 | + say("call is E8", b[4] = 0E8H); | |
| 126 | + say("call reaches triple", reaches(b, 5, t)); | |
| 127 | + p := tailTwice; | |
| 128 | + b := ^U8(p); | |
| 129 | + say("jmp is E9", b[0] = 0E9H); | |
| 130 | + say("jmp reaches AsmNamed.twice", reaches(b, 1, w)); | |
| 131 | + q := addBoth; | |
| 132 | + b := ^U8(q); | |
| 133 | + say("lea is 48 8D 05", (b[0] = 48H) and (b[1] = 8DH) and (b[2] = 05H)); | |
| 134 | + say("lea reaches total", reaches(b, 3, ^U8(SYSTEM.ADR(total)))); | |
| 135 | + say("lea rcx is 48 8D 0D", (b[10] = 48H) and (b[11] = 8DH) and (b[12] = 0DH)); | |
| 136 | + say("lea reaches AsmNamed.count", reaches(b, 13, ^U8(AsmNamed.countAt()))); | |
| 137 | +end; | |
| 138 | + | |
| 139 | +end AsmName. |
| @@ -0,0 +1,29 @@ | ||
| 1 | +(* What AsmName's code procedures name in another module: an exported | |
| 2 | + procedure and variable, and a procedure that isn't exported, which | |
| 3 | + test/err/AsmNameHidden may not name. *) | |
| 4 | +module AsmNamed; | |
| 5 | +import SYSTEM; | |
| 6 | + | |
| 7 | +var count*: I64; (* added to through its address, by lea *) | |
| 8 | + | |
| 9 | +(* x * 2, the target of a tail call *) | |
| 10 | +proc twice*(x: I64): I64; | |
| 11 | +begin | |
| 12 | + return x * 2; | |
| 13 | +end; | |
| 14 | + | |
| 15 | +(* count's address, as raz takes it *) | |
| 16 | +proc countAt*: ^I64; | |
| 17 | +begin | |
| 18 | + return SYSTEM.ADR(count); | |
| 19 | +end; | |
| 20 | + | |
| 21 | +(* not exported: another module's code may not name it *) | |
| 22 | +proc hidden(x: I64): I64; | |
| 23 | +begin | |
| 24 | + return x; | |
| 25 | +end; | |
| 26 | + | |
| 27 | +begin | |
| 28 | + count := hidden(100); | |
| 29 | +end AsmNamed. |