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 | 14x 14x 14x 16x 16x 1x 15x 15x 1x 14x 14x 14x 11x 11x 7x 4x 3x 3x 3x 8x 8x 8x 8x 8x 8x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 5x 1x 4x 4x 4x 4x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 6x 3x 2x 14x | /**
* Bitmap field assignment handlers (ADR-109).
*
* Handles assignments to bitmap fields:
* - BITMAP_FIELD_SINGLE_BIT: flags.Running <- true
* - BITMAP_FIELD_MULTI_BIT: flags.Mode <- 3
* - BITMAP_ARRAY_ELEMENT_FIELD: bitmapArr[i].Field <- value
* - STRUCT_MEMBER_BITMAP_FIELD: device.flags.Active <- true
* - REGISTER_MEMBER_BITMAP_FIELD: MOTOR.CTRL.Running <- true
* - SCOPED_REGISTER_MEMBER_BITMAP_FIELD: Scope.GPIO7.ICR1.LED <- value
*/
import AssignmentKind from "../AssignmentKind";
import IAssignmentContext from "../IAssignmentContext";
import IHandlerDeps from "./IHandlerDeps";
import BitUtils from "../../../../../utils/BitUtils";
import TAssignmentHandler from "./TAssignmentHandler";
/**
* Calculate mask value and hex string for bitmap field.
*/
function calculateMask(width: number): { mask: number; maskHex: string } {
const mask = (1 << width) - 1;
const maskHex = BitUtils.formatHex(mask);
return { mask, maskHex };
}
/**
* Validate and get bitmap field info, throwing appropriate errors.
*/
function getBitmapFieldInfo(
bitmapType: string,
fieldName: string,
ctx: IAssignmentContext,
deps: IHandlerDeps,
): { offset: number; width: number } {
const fields = deps.symbols.bitmapFields.get(bitmapType);
if (!fields?.has(fieldName)) {
throw new Error(
`Error: Unknown bitmap field '${fieldName}' on type '${bitmapType}'`,
);
}
const fieldInfo = fields.get(fieldName)!;
// Validate compound operators not allowed
if (ctx.isCompound) {
throw new Error(
`Compound assignment operators not supported for bitmap field access: ${ctx.cnextOp}`,
);
}
// Validate compile-time literal overflow
Eif (ctx.valueCtx) {
deps.validateBitmapFieldLiteral(ctx.valueCtx, fieldInfo.width, fieldName);
}
return fieldInfo;
}
/**
* Generate bitmap field write using read-modify-write pattern.
*/
function generateBitmapWrite(
target: string,
fieldInfo: { offset: number; width: number },
value: string,
): string {
const { maskHex } = calculateMask(fieldInfo.width);
if (fieldInfo.width === 1) {
// Single bit write: target = (target & ~(1 << offset)) | ((value ? 1 : 0) << offset)
return `${target} = (${target} & ~(1 << ${fieldInfo.offset})) | (${BitUtils.boolToInt(value)} << ${fieldInfo.offset});`;
} else {
// Multi-bit write: target = (target & ~(mask << offset)) | ((value & mask) << offset)
return `${target} = (${target} & ~(${maskHex} << ${fieldInfo.offset})) | ((${value} & ${maskHex}) << ${fieldInfo.offset});`;
}
}
/**
* Generate write-only bitmap field write (no RMW).
*/
function generateWriteOnlyBitmapWrite(
target: string,
fieldInfo: { offset: number; width: number },
value: string,
): string {
const { maskHex } = calculateMask(fieldInfo.width);
if (fieldInfo.width === 1) {
return `${target} = (${BitUtils.boolToInt(value)} << ${fieldInfo.offset});`;
} else E{
return `${target} = ((${value} & ${maskHex}) << ${fieldInfo.offset});`;
}
}
/**
* Handle simple bitmap field: flags.Running <- true
*/
function handleBitmapFieldSingleBit(
ctx: IAssignmentContext,
deps: IHandlerDeps,
): string {
const varName = ctx.identifiers[0];
const fieldName = ctx.identifiers[1];
const typeInfo = deps.typeRegistry.get(varName);
const bitmapType = typeInfo!.bitmapTypeName!;
const fieldInfo = getBitmapFieldInfo(bitmapType, fieldName, ctx, deps);
return generateBitmapWrite(varName, fieldInfo, ctx.generatedValue);
}
/**
* Handle multi-bit bitmap field: flags.Mode <- 3
*/
function handleBitmapFieldMultiBit(
ctx: IAssignmentContext,
deps: IHandlerDeps,
): string {
// Same logic as single bit, generateBitmapWrite handles width
return handleBitmapFieldSingleBit(ctx, deps);
}
/**
* Handle bitmap array element field: bitmapArr[i].Field <- value
*/
function handleBitmapArrayElementField(
ctx: IAssignmentContext,
deps: IHandlerDeps,
): string {
const arrayName = ctx.identifiers[0];
const fieldName = ctx.identifiers[1];
const typeInfo = deps.typeRegistry.get(arrayName);
const bitmapType = typeInfo!.bitmapTypeName!;
const fieldInfo = getBitmapFieldInfo(bitmapType, fieldName, ctx, deps);
const index = deps.generateExpression(ctx.subscripts[0]);
const arrayElement = `${arrayName}[${index}]`;
return generateBitmapWrite(arrayElement, fieldInfo, ctx.generatedValue);
}
/**
* Handle struct member bitmap field: device.flags.Active <- true
*/
function handleStructMemberBitmapField(
ctx: IAssignmentContext,
deps: IHandlerDeps,
): string {
const structName = ctx.identifiers[0];
const memberName = ctx.identifiers[1];
const fieldName = ctx.identifiers[2];
const structTypeInfo = deps.typeRegistry.get(structName);
const memberInfo = deps.getMemberTypeInfo(
structTypeInfo!.baseType,
memberName,
);
const bitmapType = memberInfo!.baseType;
const fieldInfo = getBitmapFieldInfo(bitmapType, fieldName, ctx, deps);
const memberPath = `${structName}.${memberName}`;
return generateBitmapWrite(memberPath, fieldInfo, ctx.generatedValue);
}
/**
* Handle register member bitmap field: MOTOR.CTRL.Running <- true
*/
function handleRegisterMemberBitmapField(
ctx: IAssignmentContext,
deps: IHandlerDeps,
): string {
const regName = ctx.identifiers[0];
const memberName = ctx.identifiers[1];
const fieldName = ctx.identifiers[2];
const fullRegMember = `${regName}_${memberName}`;
const bitmapType = deps.symbols.registerMemberTypes.get(fullRegMember)!;
const fieldInfo = getBitmapFieldInfo(bitmapType, fieldName, ctx, deps);
return generateBitmapWrite(fullRegMember, fieldInfo, ctx.generatedValue);
}
/**
* Handle scoped register member bitmap field.
* Two patterns:
* - this.REG.MEMBER.field (hasThis=true, 3 identifiers) - scope from currentScope
* - Scope.REG.MEMBER.field (hasThis=false, 4 identifiers) - scope from identifiers[0]
*/
function handleScopedRegisterMemberBitmapField(
ctx: IAssignmentContext,
deps: IHandlerDeps,
): string {
let scopeName: string;
let regName: string;
let memberName: string;
let fieldName: string;
if (ctx.hasThis) {
// this.REG.MEMBER.field - 3 identifiers
if (!deps.currentScope) {
throw new Error("Error: 'this' can only be used inside a scope");
}
scopeName = deps.currentScope;
regName = ctx.identifiers[0];
memberName = ctx.identifiers[1];
fieldName = ctx.identifiers[2];
} else {
// Scope.REG.MEMBER.field - 4 identifiers
scopeName = ctx.identifiers[0];
regName = ctx.identifiers[1];
memberName = ctx.identifiers[2];
fieldName = ctx.identifiers[3];
// Validate cross-scope access
deps.validateCrossScopeVisibility(scopeName, regName);
}
const fullRegName = `${scopeName}_${regName}`;
const fullRegMember = `${fullRegName}_${memberName}`;
const bitmapType = deps.symbols.registerMemberTypes.get(fullRegMember)!;
const fieldInfo = getBitmapFieldInfo(bitmapType, fieldName, ctx, deps);
// Check for write-only register (includes w1s, w1c)
const accessMod = deps.symbols.registerMemberAccess.get(fullRegMember);
const isWriteOnly =
accessMod === "wo" || accessMod === "w1s" || accessMod === "w1c";
if (isWriteOnly) {
return generateWriteOnlyBitmapWrite(
fullRegMember,
fieldInfo,
ctx.generatedValue,
);
}
return generateBitmapWrite(fullRegMember, fieldInfo, ctx.generatedValue);
}
/**
* All bitmap handlers for registration.
*/
const bitmapHandlers: ReadonlyArray<[AssignmentKind, TAssignmentHandler]> = [
[AssignmentKind.BITMAP_FIELD_SINGLE_BIT, handleBitmapFieldSingleBit],
[AssignmentKind.BITMAP_FIELD_MULTI_BIT, handleBitmapFieldMultiBit],
[AssignmentKind.BITMAP_ARRAY_ELEMENT_FIELD, handleBitmapArrayElementField],
[AssignmentKind.STRUCT_MEMBER_BITMAP_FIELD, handleStructMemberBitmapField],
[
AssignmentKind.REGISTER_MEMBER_BITMAP_FIELD,
handleRegisterMemberBitmapField,
],
[
AssignmentKind.SCOPED_REGISTER_MEMBER_BITMAP_FIELD,
handleScopedRegisterMemberBitmapField,
],
];
export default bitmapHandlers;
|