All files / transpiler/output/codegen/assignment/handlers RegisterHandlers.ts

98.59% Statements 70/71
91.17% Branches 31/34
100% Functions 5/5
98.59% Lines 70/71

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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282                                                              3x           1x     2x 2x   2x       2x 2x   2x   3x                     8x           8x       8x 8x   8x   8x 3x           3x     4x                     6x           6x       6x 6x   6x 6x 6x   6x 4x               4x 4x 4x               4x 2x       1x                 1x                               5x 5x           5x         5x 5x   5x   5x 2x           2x     1x                     6x 6x         6x 6x 6x       6x   6x 6x   6x 6x 6x   6x 3x               3x 3x   3x           1x 1x   1x 1x 1x   1x 1x       1x               1x                     14x                
/**
 * Register bit assignment handlers (ADR-109).
 *
 * Handles assignments to register bits:
 * - REGISTER_BIT: GPIO7.DR_SET[LED_BIT] <- true
 * - REGISTER_BIT_RANGE: GPIO7.DR_SET[0, 8] <- value
 * - REGISTER_MEMORY_MAPPED: Width-appropriate MMIO access
 * - SCOPED_REGISTER_BIT: this.GPIO7.DR_SET[bit] <- true
 * - SCOPED_REGISTER_BIT_RANGE: this.GPIO7.ICR1[6, 2] <- value
 */
import AssignmentKind from "../AssignmentKind";
import IAssignmentContext from "../IAssignmentContext";
import IHandlerDeps from "./IHandlerDeps";
import BitUtils from "../../../../../utils/BitUtils";
import TypeCheckUtils from "../../../../../utils/TypeCheckUtils";
import TAssignmentHandler from "./TAssignmentHandler";
import RegisterUtils from "./RegisterUtils";
import AssignmentHandlerUtils from "./AssignmentHandlerUtils";
 
/**
 * Try to generate MMIO-optimized memory access for byte-aligned writes.
 * Returns null if optimization not applicable.
 */
function tryGenerateMMIO(
  fullName: string,
  regName: string,
  startExpr: ReturnType<IHandlerDeps["tryEvaluateConstant"]>,
  widthExpr: ReturnType<IHandlerDeps["tryEvaluateConstant"]>,
  value: string,
  deps: IHandlerDeps,
): string | null {
  if (
    startExpr === undefined ||
    widthExpr === undefined ||
    startExpr % 8 !== 0 ||
    !TypeCheckUtils.isStandardWidth(widthExpr)
  ) {
    return null;
  }
 
  const baseAddr = deps.symbols.registerBaseAddresses.get(regName);
  const memberOffset = deps.symbols.registerMemberOffsets.get(fullName);
 
  Iif (baseAddr === undefined || memberOffset === undefined) {
    return null;
  }
 
  const byteOffset = startExpr / 8;
  const accessType = `uint${widthExpr}_t`;
  const totalOffset =
    byteOffset === 0 ? memberOffset : `${memberOffset} + ${byteOffset}`;
 
  return `*((volatile ${accessType}*)(${baseAddr} + ${totalOffset})) = (${value});`;
}
 
/**
 * Handle register single bit: GPIO7.DR_SET[LED_BIT] <- true
 */
function handleRegisterBit(
  ctx: IAssignmentContext,
  deps: IHandlerDeps,
): string {
  // Issue #707: Use shared validation utility
  AssignmentHandlerUtils.validateNoCompoundForBitAccess(
    ctx.isCompound,
    ctx.cnextOp,
  );
 
  const { fullName } =
    AssignmentHandlerUtils.buildRegisterNameWithScopeDetection(
      ctx.identifiers,
      deps.isKnownScope,
    );
  const accessMod = deps.symbols.registerMemberAccess.get(fullName);
  const isWriteOnly = RegisterUtils.isWriteOnlyRegister(accessMod);
 
  const bitIndex = deps.generateExpression(ctx.subscripts[0]);
 
  if (isWriteOnly) {
    AssignmentHandlerUtils.validateWriteOnlyValue(
      ctx.generatedValue,
      fullName,
      bitIndex,
      true,
    );
    return `${fullName} = (1 << ${bitIndex});`;
  }
 
  return `${fullName} = (${fullName} & ~(1 << ${bitIndex})) | (${BitUtils.boolToInt(ctx.generatedValue)} << ${bitIndex});`;
}
 
/**
 * Handle register bit range: GPIO7.DR_SET[0, 8] <- value
 */
function handleRegisterBitRange(
  ctx: IAssignmentContext,
  deps: IHandlerDeps,
): string {
  // Issue #707: Use shared validation utility
  AssignmentHandlerUtils.validateNoCompoundForBitAccess(
    ctx.isCompound,
    ctx.cnextOp,
  );
 
  const { fullName, regName } =
    AssignmentHandlerUtils.buildRegisterNameWithScopeDetection(
      ctx.identifiers,
      deps.isKnownScope,
    );
  const accessMod = deps.symbols.registerMemberAccess.get(fullName);
  const isWriteOnly = RegisterUtils.isWriteOnlyRegister(accessMod);
 
  const start = deps.generateExpression(ctx.subscripts[0]);
  const width = deps.generateExpression(ctx.subscripts[1]);
  const mask = BitUtils.generateMask(width);
 
  if (isWriteOnly) {
    AssignmentHandlerUtils.validateWriteOnlyValue(
      ctx.generatedValue,
      fullName,
      `${start}, ${width}`,
      false,
    );
 
    // Try MMIO optimization
    const startConst = deps.tryEvaluateConstant(ctx.subscripts[0]);
    const widthConst = deps.tryEvaluateConstant(ctx.subscripts[1]);
    const mmio = tryGenerateMMIO(
      fullName,
      regName,
      startConst,
      widthConst,
      ctx.generatedValue,
      deps,
    );
    if (mmio) {
      return mmio;
    }
 
    // Fallback: write shifted value
    return RegisterUtils.generateWriteOnlyBitRange(
      fullName,
      ctx.generatedValue,
      mask,
      start,
    );
  }
 
  // Read-write: read-modify-write
  return RegisterUtils.generateRmwBitRange(
    fullName,
    ctx.generatedValue,
    mask,
    start,
  );
}
 
/**
 * Handle scoped register single bit: this.GPIO7.DR_SET[bit] <- true
 */
function handleScopedRegisterBit(
  ctx: IAssignmentContext,
  deps: IHandlerDeps,
): string {
  // Issue #707: Use shared validation utilities
  AssignmentHandlerUtils.validateScopeContext(deps.currentScope);
  AssignmentHandlerUtils.validateNoCompoundForBitAccess(
    ctx.isCompound,
    ctx.cnextOp,
  );
 
  // Build scoped name: Scope_Register_Member
  const regName = AssignmentHandlerUtils.buildScopedRegisterName(
    deps.currentScope!,
    ctx.identifiers,
  );
 
  const accessMod = deps.symbols.registerMemberAccess.get(regName);
  const isWriteOnly = RegisterUtils.isWriteOnlyRegister(accessMod);
 
  const bitIndex = deps.generateExpression(ctx.subscripts[0]);
 
  if (isWriteOnly) {
    AssignmentHandlerUtils.validateWriteOnlyValue(
      ctx.generatedValue,
      regName,
      bitIndex,
      true,
    );
    return `${regName} = (1 << ${bitIndex});`;
  }
 
  return `${regName} = (${regName} & ~(1 << ${bitIndex})) | (${BitUtils.boolToInt(ctx.generatedValue)} << ${bitIndex});`;
}
 
/**
 * Handle scoped register bit range: this.GPIO7.ICR1[6, 2] <- value
 */
function handleScopedRegisterBitRange(
  ctx: IAssignmentContext,
  deps: IHandlerDeps,
): string {
  // Issue #707: Use shared validation utilities
  AssignmentHandlerUtils.validateScopeContext(deps.currentScope);
  AssignmentHandlerUtils.validateNoCompoundForBitAccess(
    ctx.isCompound,
    ctx.cnextOp,
  );
 
  const scopeName = deps.currentScope!;
  const parts = ctx.identifiers;
  const regName = AssignmentHandlerUtils.buildScopedRegisterName(
    scopeName,
    parts,
  );
  const scopedRegName = `${scopeName}_${parts[0]}`;
 
  const accessMod = deps.symbols.registerMemberAccess.get(regName);
  const isWriteOnly = RegisterUtils.isWriteOnlyRegister(accessMod);
 
  const start = deps.generateExpression(ctx.subscripts[0]);
  const width = deps.generateExpression(ctx.subscripts[1]);
  const mask = `((1U << ${width}) - 1)`;
 
  if (isWriteOnly) {
    AssignmentHandlerUtils.validateWriteOnlyValue(
      ctx.generatedValue,
      regName,
      `${start}, ${width}`,
      false,
    );
 
    // Try MMIO optimization
    const startConst = deps.tryEvaluateConstant(ctx.subscripts[0]);
    const widthConst = deps.tryEvaluateConstant(ctx.subscripts[1]);
 
    if (
      startConst !== undefined &&
      widthConst !== undefined &&
      startConst % 8 === 0 &&
      TypeCheckUtils.isStandardWidth(widthConst)
    ) {
      const baseAddr = deps.symbols.registerBaseAddresses.get(scopedRegName);
      const memberOffset = deps.symbols.registerMemberOffsets.get(regName);
 
      Eif (baseAddr !== undefined && memberOffset !== undefined) {
        const byteOffset = startConst / 8;
        const accessType = `uint${widthConst}_t`;
        const totalOffset =
          byteOffset === 0 ? memberOffset : `${memberOffset} + ${byteOffset}`;
        return `*((volatile ${accessType}*)(${baseAddr} + ${totalOffset})) = (${ctx.generatedValue});`;
      }
    }
 
    return RegisterUtils.generateWriteOnlyBitRange(
      regName,
      ctx.generatedValue,
      mask,
      start,
    );
  }
 
  return RegisterUtils.generateRmwBitRange(
    regName,
    ctx.generatedValue,
    mask,
    start,
  );
}
 
/**
 * All register handlers for registration.
 */
const registerHandlers: ReadonlyArray<[AssignmentKind, TAssignmentHandler]> = [
  [AssignmentKind.REGISTER_BIT, handleRegisterBit],
  [AssignmentKind.REGISTER_BIT_RANGE, handleRegisterBitRange],
  [AssignmentKind.SCOPED_REGISTER_BIT, handleScopedRegisterBit],
  [AssignmentKind.SCOPED_REGISTER_BIT_RANGE, handleScopedRegisterBitRange],
];
 
export default registerHandlers;