All files / transpiler/output/codegen/generators/declarationGenerators ScopedRegisterGenerator.ts

100% Statements 9/9
100% Branches 2/2
100% Functions 2/2
100% Lines 9/9

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                                        14x             10x 10x 10x     10x 12x 12x         10x                       10x              
/**
 * ScopedRegisterGenerator - ADR-004 Scoped Register Generation
 *
 * Generates C #define macros from C-Next register declarations within scopes.
 *
 * Example:
 *   scope Teensy4 { register GPIO7 @ 0x42004000 { ... } }
 *   ->
 *   #define Teensy4_GPIO7_DR (*(volatile uint32_t*)(0x42004000 + 0x00))
 */
import * as Parser from "../../../../logic/parser/grammar/CNextParser";
import IGeneratorInput from "../IGeneratorInput";
import IGeneratorState from "../IGeneratorState";
import IGeneratorOutput from "../IGeneratorOutput";
import IOrchestrator from "../IOrchestrator";
import generateRegisterMacros from "./RegisterMacroGenerator";
 
/**
 * Generate register macros with scope prefix.
 */
const generateScopedRegister = (
  node: Parser.RegisterDeclarationContext,
  scopeName: string,
  input: IGeneratorInput,
  _state: IGeneratorState,
  orchestrator: IOrchestrator,
): IGeneratorOutput => {
  const name = node.IDENTIFIER().getText();
  const fullName = `${scopeName}_${name}`; // Teensy4_GPIO7
  const baseAddress = orchestrator.generateExpression(node.expression());
 
  // Type resolver for scoped bitmaps (e.g., GPIO7Pins -> Teensy4_GPIO7Pins)
  const resolveType = (regType: string): string | undefined => {
    const scopedTypeName = `${scopeName}_${regType}`;
    return input.symbols?.knownBitmaps.has(scopedTypeName)
      ? scopedTypeName
      : undefined;
  };
 
  const lines: string[] = [
    `/* Register: ${fullName} @ ${baseAddress} */`,
    ...generateRegisterMacros(
      node.registerMember(),
      fullName,
      baseAddress,
      orchestrator,
      resolveType,
    ),
    "",
  ];
 
  return {
    code: lines.join("\n"),
    effects: [],
  };
};
 
export default generateScopedRegister;