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

100% Statements 51/51
100% Branches 12/12
100% Functions 7/7
100% Lines 51/51

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                                            10x               8x           8x   7x   8x 8x   8x   8x 3x           3x     4x               6x           6x   5x   6x 6x   6x       6x 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 3x               3x           3x 1x     1x               1x                     15x                
/**
 * 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 BitUtils from "../../../../../utils/BitUtils";
import TAssignmentHandler from "./TAssignmentHandler";
import RegisterUtils from "./RegisterUtils";
import AssignmentHandlerUtils from "./AssignmentHandlerUtils";
import CodeGenState from "../../../../state/CodeGenState";
import type ICodeGenApi from "../../types/ICodeGenApi";
import QualifiedNameGenerator from "../../utils/QualifiedNameGenerator";
 
/** Get typed generator reference */
function gen(): ICodeGenApi {
  return CodeGenState.generator as ICodeGenApi;
}
 
/**
 * Handle register single bit: GPIO7.DR_SET[LED_BIT] <- true
 */
function handleRegisterBit(ctx: IAssignmentContext): string {
  // Issue #707: Use shared validation utility
  AssignmentHandlerUtils.validateNoCompoundForBitAccess(
    ctx.isCompound,
    ctx.cnextOp,
  );
 
  const { fullName } =
    AssignmentHandlerUtils.buildRegisterNameWithScopeDetection(
      ctx.identifiers,
      (name) => CodeGenState.isKnownScope(name),
    );
  const accessMod = CodeGenState.symbols!.registerMemberAccess.get(fullName);
  const isWriteOnly = RegisterUtils.isWriteOnlyRegister(accessMod);
 
  const bitIndex = gen().generateExpression(ctx.subscripts[0]);
 
  if (isWriteOnly) {
    AssignmentHandlerUtils.validateWriteOnlyValue(
      ctx.generatedValue,
      fullName,
      bitIndex,
      true,
    );
    return `${fullName} = (1U << ${bitIndex});`;
  }
 
  return `${fullName} = (${fullName} & ~(1U << ${bitIndex})) | (${BitUtils.boolToInt(ctx.generatedValue)} << ${bitIndex});`;
}
 
/**
 * Handle register bit range: GPIO7.DR_SET[0, 8] <- value
 */
function handleRegisterBitRange(ctx: IAssignmentContext): string {
  // Issue #707: Use shared validation utility
  AssignmentHandlerUtils.validateNoCompoundForBitAccess(
    ctx.isCompound,
    ctx.cnextOp,
  );
 
  const { fullName, regName } =
    AssignmentHandlerUtils.buildRegisterNameWithScopeDetection(
      ctx.identifiers,
      (name) => CodeGenState.isKnownScope(name),
    );
  const accessMod = CodeGenState.symbols!.registerMemberAccess.get(fullName);
  const isWriteOnly = RegisterUtils.isWriteOnlyRegister(accessMod);
 
  const { start, width, mask } = RegisterUtils.extractBitRangeParams(
    ctx.subscripts,
  );
 
  if (isWriteOnly) {
    AssignmentHandlerUtils.validateWriteOnlyValue(
      ctx.generatedValue,
      fullName,
      `${start}, ${width}`,
      false,
    );
 
    // Try MMIO optimization
    const mmio = RegisterUtils.tryGenerateMMIO(
      fullName,
      regName,
      ctx.subscripts,
      ctx.generatedValue,
    );
    if (mmio.success) {
      return mmio.statement!;
    }
 
    // 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): string {
  // Issue #707: Use shared validation utilities
  AssignmentHandlerUtils.validateScopeContext(CodeGenState.currentScope);
  AssignmentHandlerUtils.validateNoCompoundForBitAccess(
    ctx.isCompound,
    ctx.cnextOp,
  );
 
  // Build scoped name: Scope_Register_Member
  const regName = AssignmentHandlerUtils.buildScopedRegisterName(
    CodeGenState.currentScope!,
    ctx.identifiers,
  );
 
  const accessMod = CodeGenState.symbols!.registerMemberAccess.get(regName);
  const isWriteOnly = RegisterUtils.isWriteOnlyRegister(accessMod);
 
  const bitIndex = gen().generateExpression(ctx.subscripts[0]);
 
  if (isWriteOnly) {
    AssignmentHandlerUtils.validateWriteOnlyValue(
      ctx.generatedValue,
      regName,
      bitIndex,
      true,
    );
    return `${regName} = (1U << ${bitIndex});`;
  }
 
  return `${regName} = (${regName} & ~(1U << ${bitIndex})) | (${BitUtils.boolToInt(ctx.generatedValue)} << ${bitIndex});`;
}
 
/**
 * Handle scoped register bit range: this.GPIO7.ICR1[6, 2] <- value
 */
function handleScopedRegisterBitRange(ctx: IAssignmentContext): string {
  // Issue #707: Use shared validation utilities
  AssignmentHandlerUtils.validateScopeContext(CodeGenState.currentScope);
  AssignmentHandlerUtils.validateNoCompoundForBitAccess(
    ctx.isCompound,
    ctx.cnextOp,
  );
 
  const scopeName = CodeGenState.currentScope!;
  const parts = ctx.identifiers;
  const regName = AssignmentHandlerUtils.buildScopedRegisterName(
    scopeName,
    parts,
  );
  const scopedRegName = QualifiedNameGenerator.forMember(scopeName, parts[0]);
 
  const accessMod = CodeGenState.symbols!.registerMemberAccess.get(regName);
  const isWriteOnly = RegisterUtils.isWriteOnlyRegister(accessMod);
 
  const { start, width, mask } = RegisterUtils.extractBitRangeParams(
    ctx.subscripts,
  );
 
  if (isWriteOnly) {
    AssignmentHandlerUtils.validateWriteOnlyValue(
      ctx.generatedValue,
      regName,
      `${start}, ${width}`,
      false,
    );
 
    // Try MMIO optimization
    const mmio = RegisterUtils.tryGenerateMMIO(
      regName,
      scopedRegName,
      ctx.subscripts,
      ctx.generatedValue,
    );
    if (mmio.success) {
      return mmio.statement!;
    }
 
    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;