Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | 3890x 3890x 1339x 846x 3890x 19x 19x 19x 603x 133x 603x 603x 31x 603x 51x 51x 1x 603x 82x 82x 49x 603x 504x 622x 213x 236x 213x 2825x 2825x 622x 622x 2825x 2825x 2825x 100x 100x 100x 100x 100x 100x 522x | /**
* Which register member a chain names (ADR-004).
*
* #1322. Two rules ask this and they are not the same rule: ADR-004's access
* modifiers (E0870-E0872) and ADR-034's bitmap access (E0882/E0883), because a
* register member may be TYPED by a bitmap and a bitmap rule then has to reach
* it through the register.
*
* The resolution is the thing that must not be written twice. It tries the
* spellings codegen accepted, in a specific order: `R.M` for a global
* register; a scoped register's bare name inside its own scope; `S.R.M`
* across a scope. `this.` binds to the enclosing scope and `global.` to the
* file scope, so each root reads the chain from its own offset. A second copy
* would be free to disagree about any of those, and a rule enforced on a
* different register than the one written is worse than no rule.
*
* WHICH root a chain is written with is not a register question, so it is
* asked of `ChainRoot`. This file held two copies of it and `BitAccessAnalyzer`
* held two more.
*/
import { ParserRuleContext } from "antlr4ng";
import * as Parser from "../../../PARSE/2-Parse/grammar/CNextParser";
import ChainRoot from "../../../utils/ChainRoot";
import QualifiedCName from "../../../utils/QualifiedCName";
import ScopeUtils from "../../../utils/ScopeUtils";
import IRegisterMember from "../types/IRegisterMember";
import TChainRoot from "../../../types/TChainRoot";
import OperandTyper from "../../../utils/OperandTyper";
import ParserUtils from "../../../utils/ParserUtils";
import type IAnalysisContext from "../types/IAnalysisContext";
import type ICodeGenSymbols from "../../../types/ICodeGenSymbols";
class RegisterMemberReference {
/**
* The chain's leading member names: the head, then every `.name` op up to
* the first subscript or call.
*/
static leadingNames(
head: string | undefined,
names: (string | null)[],
): string[] {
const chain = head === undefined ? [] : [head];
for (const name of names) {
if (name === null) break;
chain.push(name);
}
return chain;
}
/**
* The register spellings a chain's leading names may denote, and how many
* names each consumed. In the order codegen accepted them.
*
* Separated from the member lookup below because they are two questions --
* "which register could this be?" and "does that register declare this
* member?" -- and reading them as one was what made this the densest
* function in the pass.
*/
/** The one register `this.R` can name: R in the enclosing scope. */
private static thisCandidate(
symbols: ICodeGenSymbols,
chain: string[],
here: string,
): { reg: string; at: number }[] {
Iif (here === "") return [];
const inScope = ScopeUtils.getTranspiledCName({
scopePath: here,
name: chain[0],
});
return RegisterMemberReference.isRegister(symbols, inScope)
? [{ reg: inScope, at: 1 }]
: [];
}
/**
* The registers a bare or `global.`-rooted chain may name, in the order
* codegen accepted them: the file-scope register, then the enclosing scope's
* own, then another scope's through `S.R.M`.
*/
private static searchCandidates(
symbols: ICodeGenSymbols,
root: TChainRoot,
chain: string[],
here: string,
isShadowed: boolean,
): { reg: string; at: number }[] {
const scoped = (scope: string, name: string): string =>
ScopeUtils.getTranspiledCName({ scopePath: scope, name });
const candidates: { reg: string; at: number }[] = [];
// `global.` bypasses shadowing by construction; a bare name does not.
if (!isShadowed && RegisterMemberReference.isRegister(symbols, chain[0])) {
candidates.push({ reg: chain[0], at: 1 });
}
if (root === null && here !== "") {
const inScope = scoped(here, chain[0]);
if (RegisterMemberReference.isRegister(symbols, inScope)) {
candidates.push({ reg: inScope, at: 1 });
}
}
if (chain.length >= 3) {
const crossScope = scoped(chain[0], chain[1]);
if (RegisterMemberReference.isRegister(symbols, crossScope)) {
candidates.push({ reg: crossScope, at: 2 });
}
}
return candidates;
}
/** Whether the program declares a register under this C name. */
private static isRegister(symbols: ICodeGenSymbols, cName: string): boolean {
return symbols.knownRegisters.has(cName);
}
/**
* The register spellings a chain's leading names may denote, and how many
* names each consumed.
*
* `this.R` STATES where to look and admits exactly one answer; everything
* else SEARCHES. Splitting on that -- rather than on line count -- is why
* these are two functions.
*/
private static registerCandidates(
symbols: ICodeGenSymbols,
root: TChainRoot,
chain: string[],
here: string,
isShadowed: boolean,
): { reg: string; at: number }[] {
return root === "this"
? RegisterMemberReference.thisCandidate(symbols, chain, here)
: RegisterMemberReference.searchCandidates(
symbols,
root,
chain,
here,
isShadowed,
);
}
/**
* The register member an assignment target names, or null: its leading
* names up to the first subscript, read from the target's own root
*/
static ofTarget(
target: Parser.AssignmentTargetContext,
context: IAnalysisContext,
): IRegisterMember | null {
const names = target
.postfixTargetOp()
.map((op) => (op.DOT() === null ? null : op.IDENTIFIER()!.getText()));
return RegisterMemberReference.resolve(
ChainRoot.ofTarget(target),
RegisterMemberReference.leadingNames(
target.IDENTIFIER().getText(),
names,
),
target,
context,
);
}
/** The register member a chain names, or null when it names none. */
static resolve(
root: TChainRoot,
chain: string[],
node: ParserRuleContext,
context: IAnalysisContext,
): IRegisterMember | null {
const symbols = context.symbols;
if (chain.length < 2) return null;
// A bare root that names a declared value here -- a local, a member, a
// global, this file's or an included one's -- shadows a register of that
// spelling; Program's one binder decides what the name means (#1668)
const scopePath = OperandTyper.scopePathAt(node, context);
const binding =
root === null
? context.program.bindValue(
context.sourceFile,
null,
chain[0],
ParserUtils.getPosition(node),
)
: null;
const isShadowed =
binding?.kind === "local" || binding?.kind === "variable";
const prefix = root === null ? "" : `${root}.`;
for (const { reg, at } of RegisterMemberReference.registerCandidates(
symbols,
root,
chain,
scopePath,
isShadowed,
)) {
const member = chain[at];
Iif (member === undefined) continue;
const key = QualifiedCName.fromParts([reg, member]);
const access = symbols.registerMemberAccess.get(key);
Iif (access === undefined) continue;
return {
key,
access,
member,
spelling: prefix + chain.slice(0, at + 1).join("."),
consumed: at + 1,
};
}
return null;
}
}
export default RegisterMemberReference;
|