Commit 531e5676

531e56766672Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

fix(codegen): cast away volatile when passing main() locals to typed params

Closes #487. Top-level begin/rescue sets @needs_setjmp = 1, which makes main()'s pointer locals get declared as volatile T *. When those locals are then passed to a function whose param is plain T *, gcc's -Wdiscarded-qualifiers (clang's -Wincompatible-pointer-types-discards-qualifiers) flags the call. The program still ran correctly because volatile is strictly stronger than the callee asks for, but clean -Wall builds tripped the warning.

Generalises the existing bigint-side cast-away-volatile pattern. A new helper cast_away_volatile_arg(arg_id, val) returns (T *)val when the enclosing scope uses setjmp AND the arg is a LocalVariableReadNode whose declared type is a pointer; otherwise returns val untouched. The helper is wrapped around compile_expr results in compile_call_args, compile_gc_safe_args, and the positional-arg paths of compile_call_args_with_defaults.

The cast is cosmetic: same C semantics, just discards the stronger qualifier the callee doesn't need.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
531e56766672
Parent
ab2c59d09229

spinel_codegen.rbmodifiedView file

@@ -20075,9 +20075,9 @@
2007520075 # the caller is more general than the callee) is unboxed via
2007620076 # the matching union field. Mirrors the #438 IntStrHash-key
2007720077 # unbox and extends it to user-method args.
20078- result = result + compile_expr_for_expected_type(positional_ids[k], ptypes[k])
20078+ result = result + cast_away_volatile_arg(positional_ids[k], compile_expr_for_expected_type(positional_ids[k], ptypes[k]))
2007920079 else
20080- result = result + compile_expr(positional_ids[k])
20080+ result = result + cast_away_volatile_arg(positional_ids[k], compile_expr(positional_ids[k]))
2008120081 end
2008220082 else
2008320083 # Use default value
@@ -20232,12 +20232,37 @@
2023220232 if k > 0
2023320233 result = result + ", "
2023420234 end
20235- result = result + compile_expr(arg_ids[k])
20235+ result = result + cast_away_volatile_arg(arg_ids[k], compile_expr(arg_ids[k]))
2023620236 k = k + 1
2023720237 end
2023820238 result
2023920239 end
2024020240
20241+ # When the enclosing function uses setjmp (top-level begin/rescue),
20242+ # every pointer LV in main() is declared `volatile T *`. Passing
20243+ # such a local to a function that takes plain `T *` discards the
20244+ # volatile qualifier — gcc -Wdiscarded-qualifiers and clang's
20245+ # -Wincompatible-pointer-types-discards-qualifiers flag it. Casting
20246+ # away volatile is safe because the qualifier is strictly stronger
20247+ # than what the callee asks for. Mirrors the bigint-side cast at
20248+ # the operator dispatch (search for `(sp_Bigint *)`).
20249+ def cast_away_volatile_arg(arg_id, val)
20250+ if @needs_setjmp != 1
20251+ return val
20252+ end
20253+ if arg_id < 0
20254+ return val
20255+ end
20256+ if @nd_type[arg_id] != "LocalVariableReadNode"
20257+ return val
20258+ end
20259+ vt = find_var_declared_type(@nd_name[arg_id])
20260+ if vt == "" || type_is_pointer(vt) != 1
20261+ return val
20262+ end
20263+ "(" + c_type(vt) + ")" + val
20264+ end
20265+
2024120266 def compile_proc_call_args(nid)
2024220267 args_id = @nd_arguments[nid]
2024320268 if args_id < 0
@@ -20273,13 +20298,13 @@
2027320298 tmp = new_temp
2027420299 at = infer_type(arg_ids[k])
2027520300 ct = c_type(at)
20276- emit(" " + ct + " " + tmp + " = " + compile_expr(arg_ids[k]) + ";")
20301+ emit(" " + ct + " " + tmp + " = " + cast_away_volatile_arg(arg_ids[k], compile_expr(arg_ids[k])) + ";")
2027720302 if type_is_pointer(at) == 1
2027820303 emit(" SP_GC_ROOT(" + tmp + ");")
2027920304 end
2028020305 temps.push(tmp)
2028120306 else
20282- temps.push(compile_expr(arg_ids[k]))
20307+ temps.push(cast_away_volatile_arg(arg_ids[k], compile_expr(arg_ids[k])))
2028320308 end
2028420309 k = k + 1
2028520310 end