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 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 | 14x 14x 68x 68x 68x 68x 68x 2x 66x 66x 68x 61x 61x 13x 14x 35x 35x 35x 5x 30x 8x 8x 8x 8x 2x 6x 3x 3x 3x 3x 4x 4x 4x 1x 3x 3x 3x 3x 3x 2x 2x 2x 1x 1x 2x 2x 2x 2x 1x 1x 1x 1x 3x 3x 3x 1x 2x 64x 64x 1x 63x 48x 35x 8x 3x 2x 15x 9x 4x 3x 2x 6x 2x 1x 3x 54x 10x 4x | /**
* Overflow Helper Templates
*
* Template-based generation for overflow-safe arithmetic helpers.
* Reduces duplication between clamp (release) and panic (debug) modes
* by extracting common type resolution and function structure.
*
* Issue #707: Extracted from HelperGenerator.ts to reduce code duplication.
*/
import TYPE_MAP from "../../types/TYPE_MAP";
import WIDER_TYPE_MAP from "../../types/WIDER_TYPE_MAP";
import TYPE_LIMITS from "../../types/TYPE_LIMITS";
const { TYPE_MAX, TYPE_MIN } = TYPE_LIMITS;
// C newline escape for generated fprintf statements (S7780: use raw string)
const C_NEWLINE = String.raw`\n`;
/**
* Type information needed for helper generation
*/
interface ITypeInfo {
cType: string;
widerType: string;
maxValue: string;
minValue: string;
cnxType: string;
isUnsigned: boolean;
useWiderArithmetic: boolean;
}
/**
* Resolve type information for a C-Next type
*/
function resolveTypeInfo(cnxType: string): ITypeInfo | null {
const cType = TYPE_MAP[cnxType];
const widerType = WIDER_TYPE_MAP[cnxType] || cType;
const maxValue = TYPE_MAX[cnxType];
const minValue = TYPE_MIN[cnxType];
if (!cType || !maxValue) {
return null;
}
const isUnsigned = cnxType.startsWith("u");
// For signed types narrower than i64, use wider arithmetic to avoid UB (Issue #94)
const useWiderArithmetic =
!isUnsigned && widerType !== cType && cnxType !== "i64";
return {
cType,
widerType,
maxValue,
minValue,
cnxType,
isUnsigned,
useWiderArithmetic,
};
}
/**
* Generate function signature
*/
function generateSignature(
operation: string,
info: ITypeInfo,
bParamType?: string,
): string {
const paramB = bParamType ?? info.cType;
return `static inline ${info.cType} cnx_clamp_${operation}_${info.cnxType}(${info.cType} a, ${paramB} b)`;
}
/**
* Generate panic block for debug mode
*/
function generatePanicBlock(cnxType: string, opName: string): string {
return ` fprintf(stderr, "PANIC: Integer overflow in ${cnxType} ${opName}${C_NEWLINE}");
abort();`;
}
// Operation names for panic messages
const OPERATION_NAMES: Record<string, string> = {
add: "addition",
sub: "subtraction",
mul: "multiplication",
};
/**
* Templates for unsigned clamp operations using builtin hybrid approach
* Issue #231: Check wide operand first, then use builtin
* Issue #94: Wide operand check prevents truncation issues
*/
class UnsignedClampTemplates {
static add(info: ITypeInfo, debugMode: boolean): string {
const sig = generateSignature("add", info, info.widerType);
const opName = OPERATION_NAMES.add;
if (debugMode) {
return `${sig} {
if (b > (${info.widerType})(${info.maxValue} - a)) {
${generatePanicBlock(info.cnxType, opName)}
}
${info.cType} result;
if (__builtin_add_overflow(a, (${info.cType})b, &result)) {
${generatePanicBlock(info.cnxType, opName)}
}
return result;
}`;
}
return `${sig} {
if (b > (${info.widerType})(${info.maxValue} - a)) return ${info.maxValue};
${info.cType} result;
if (__builtin_add_overflow(a, (${info.cType})b, &result)) return ${info.maxValue};
return result;
}`;
}
static sub(info: ITypeInfo, debugMode: boolean): string {
const sig = generateSignature("sub", info, info.widerType);
const opName = OPERATION_NAMES.sub;
// Use "underflow" message for unsigned subtraction
const panicMsg = `Integer underflow in ${info.cnxType} ${opName}`;
if (debugMode) {
return `${sig} {
if (b > (${info.widerType})a) {
fprintf(stderr, "PANIC: ${panicMsg}${C_NEWLINE}");
abort();
}
${info.cType} result;
if (__builtin_sub_overflow(a, (${info.cType})b, &result)) {
fprintf(stderr, "PANIC: ${panicMsg}${C_NEWLINE}");
abort();
}
return result;
}`;
}
// Use > (not >=) since b == a produces valid result 0 via the builtin path
return `${sig} {
if (b > (${info.widerType})a) return 0;
${info.cType} result;
if (__builtin_sub_overflow(a, (${info.cType})b, &result)) return 0;
return result;
}`;
}
static mul(info: ITypeInfo, debugMode: boolean): string {
const sig = generateSignature("mul", info, info.widerType);
const opName = OPERATION_NAMES.mul;
Iif (debugMode) {
return `${sig} {
if (b != 0 && a > ${info.maxValue} / b) {
${generatePanicBlock(info.cnxType, opName)}
}
${info.cType} result;
if (__builtin_mul_overflow(a, (${info.cType})b, &result)) {
${generatePanicBlock(info.cnxType, opName)}
}
return result;
}`;
}
return `${sig} {
if (b != 0 && a > ${info.maxValue} / b) return ${info.maxValue};
${info.cType} result;
if (__builtin_mul_overflow(a, (${info.cType})b, &result)) return ${info.maxValue};
return result;
}`;
}
}
/**
* Templates for signed narrow types using wider arithmetic
* Issue #94: Compute in wider type, then clamp to avoid UB
*/
class SignedWiderTemplates {
static add(info: ITypeInfo, debugMode: boolean): string {
const sig = generateSignature("add", info, info.widerType);
const opName = OPERATION_NAMES.add;
if (debugMode) {
return `${sig} {
${info.widerType} result = (${info.widerType})a + b;
if (result > ${info.maxValue} || result < ${info.minValue}) {
${generatePanicBlock(info.cnxType, opName)}
}
return (${info.cType})result;
}`;
}
return `${sig} {
${info.widerType} result = (${info.widerType})a + b;
if (result > ${info.maxValue}) return ${info.maxValue};
if (result < ${info.minValue}) return ${info.minValue};
return (${info.cType})result;
}`;
}
static sub(info: ITypeInfo, debugMode: boolean): string {
const sig = generateSignature("sub", info, info.widerType);
const opName = OPERATION_NAMES.sub;
Iif (debugMode) {
return `${sig} {
${info.widerType} result = (${info.widerType})a - b;
if (result > ${info.maxValue} || result < ${info.minValue}) {
${generatePanicBlock(info.cnxType, opName)}
}
return (${info.cType})result;
}`;
}
return `${sig} {
${info.widerType} result = (${info.widerType})a - b;
if (result > ${info.maxValue}) return ${info.maxValue};
if (result < ${info.minValue}) return ${info.minValue};
return (${info.cType})result;
}`;
}
static mul(info: ITypeInfo, debugMode: boolean): string {
const sig = generateSignature("mul", info, info.widerType);
const opName = OPERATION_NAMES.mul;
if (debugMode) {
return `${sig} {
${info.widerType} result = (${info.widerType})a * b;
if (result > ${info.maxValue} || result < ${info.minValue}) {
${generatePanicBlock(info.cnxType, opName)}
}
return (${info.cType})result;
}`;
}
return `${sig} {
${info.widerType} result = (${info.widerType})a * b;
if (result > ${info.maxValue}) return ${info.maxValue};
if (result < ${info.minValue}) return ${info.minValue};
return (${info.cType})result;
}`;
}
}
/**
* Templates for i64 (widest type) using manual overflow checks
*/
class SignedWidestTemplates {
static add(info: ITypeInfo, debugMode: boolean): string {
const sig = generateSignature("add", info);
const opName = OPERATION_NAMES.add;
Iif (debugMode) {
return `${sig} {
if ((b > 0 && a > ${info.maxValue} - b) || (b < 0 && a < ${info.minValue} - b)) {
${generatePanicBlock(info.cnxType, opName)}
}
return a + b;
}`;
}
return `${sig} {
if (b > 0 && a > ${info.maxValue} - b) return ${info.maxValue};
if (b < 0 && a < ${info.minValue} - b) return ${info.minValue};
return a + b;
}`;
}
static sub(info: ITypeInfo, debugMode: boolean): string {
const sig = generateSignature("sub", info);
const opName = OPERATION_NAMES.sub;
Iif (debugMode) {
return `${sig} {
if ((b < 0 && a > ${info.maxValue} + b) || (b > 0 && a < ${info.minValue} + b)) {
${generatePanicBlock(info.cnxType, opName)}
}
return a - b;
}`;
}
return `${sig} {
if (b < 0 && a > ${info.maxValue} + b) return ${info.maxValue};
if (b > 0 && a < ${info.minValue} + b) return ${info.minValue};
return a - b;
}`;
}
static mul(info: ITypeInfo, debugMode: boolean): string {
const sig = generateSignature("mul", info);
const opName = OPERATION_NAMES.mul;
if (debugMode) {
return `${sig} {
if (a != 0 && b != 0) {
if ((a > 0 && b > 0 && a > ${info.maxValue} / b) ||
(a < 0 && b < 0 && a < ${info.maxValue} / b) ||
(a > 0 && b < 0 && b < ${info.minValue} / a) ||
(a < 0 && b > 0 && a < ${info.minValue} / b)) {
${generatePanicBlock(info.cnxType, opName)}
}
}
return a * b;
}`;
}
return `${sig} {
if (a == 0 || b == 0) return 0;
if (a > 0 && b > 0 && a > ${info.maxValue} / b) return ${info.maxValue};
if (a < 0 && b < 0 && a < ${info.maxValue} / b) return ${info.maxValue};
if (a > 0 && b < 0 && b < ${info.minValue} / a) return ${info.minValue};
if (a < 0 && b > 0 && a < ${info.minValue} / b) return ${info.minValue};
return a * b;
}`;
}
}
/**
* Generate an overflow helper function for the given operation and type
*
* @param operation - The arithmetic operation (add, sub, mul)
* @param cnxType - The C-Next type (u8, i32, etc.)
* @param debugMode - If true, generate panic helpers; otherwise, clamp helpers
* @returns The generated helper function or null if type is invalid
*/
function generateHelper(
operation: string,
cnxType: string,
debugMode: boolean,
): string | null {
const info = resolveTypeInfo(cnxType);
if (!info) {
return null;
}
// Select template based on type characteristics
if (info.isUnsigned) {
switch (operation) {
case "add":
return UnsignedClampTemplates.add(info, debugMode);
case "sub":
return UnsignedClampTemplates.sub(info, debugMode);
case "mul":
return UnsignedClampTemplates.mul(info, debugMode);
default:
return null;
}
} else if (info.useWiderArithmetic) {
switch (operation) {
case "add":
return SignedWiderTemplates.add(info, debugMode);
case "sub":
return SignedWiderTemplates.sub(info, debugMode);
case "mul":
return SignedWiderTemplates.mul(info, debugMode);
default:
return null;
}
} else {
// i64: widest signed type
switch (operation) {
case "add":
return SignedWidestTemplates.add(info, debugMode);
case "sub":
return SignedWidestTemplates.sub(info, debugMode);
case "mul":
return SignedWidestTemplates.mul(info, debugMode);
default:
return null;
}
}
}
/**
* Overflow Helper Templates API
*/
class OverflowHelperTemplates {
/**
* Generate a clamp helper (returns boundary value on overflow)
*/
static generateClampHelper(
operation: string,
cnxType: string,
): string | null {
return generateHelper(operation, cnxType, false);
}
/**
* Generate a panic helper (calls abort on overflow)
*/
static generatePanicHelper(
operation: string,
cnxType: string,
): string | null {
return generateHelper(operation, cnxType, true);
}
/**
* Resolve type information (exposed for testing)
*/
static resolveTypeInfo(cnxType: string): ITypeInfo | null {
return resolveTypeInfo(cnxType);
}
}
export default OverflowHelperTemplates;
|