Commit 45329cca

45329ccadc94Merged

NHH <[email protected]> · 6 months ago · 4 files · ~4 modified

Description

fix: defined?() returns nil for never-written globals

CRuby returns nil for defined?() when has never been assigned, but Spinel always returned "global-variable" since the analyzer registered every gvar read (including bare references in defined? checks) into @gvar_names with no write tracking.

Add @gvar_written flag array that records whether each global variable has a write node in the program. The analyzer sets the flag on write and exports it through the IR; the codegen's defined? handler checks it before returning "global-variable".

Details

Commit
45329ccadc94
Parent
b4f6709777a4

spinel_analyze.rbmodifiedView file

@@ -581,6 +581,7 @@
581581 # Global variables ($x)
582582 @gvar_names = "".split(",", -1)
583583 @gvar_types = "".split(",", -1)
584+ @gvar_written = []
584585
585586 # Poly tracking: functions with params called with different types
586587 @poly_funcs = "".split(",", -1)
@@ -20829,8 +20830,9 @@
2082920830 if not_in(gname, @gvar_names) == 1
2083020831 @gvar_names.push(gname)
2083120832 @gvar_types.push(gt)
20833+ @gvar_written.push(1)
2083220834 else
20833- # Check type consistency
20835+ # Check type consistency + mark written
2083420836 gi = 0
2083520837 while gi < @gvar_names.length
2083620838 if @gvar_names[gi] == gname
@@ -20838,6 +20840,7 @@
2083820840 $stderr.puts "Error: global variable " + gname + " type mismatch: " + @gvar_types[gi] + " vs " + gt
2083920841 exit(1)
2084020842 end
20843+ @gvar_written[gi] = 1
2084120844 end
2084220845 gi = gi + 1
2084320846 end
@@ -20858,8 +20861,10 @@
2085820861 # against an mrb_int and fail to compile.
2085920862 if gname == "$PROGRAM_NAME" || gname == "$0"
2086020863 @gvar_types.push("string")
20864+ @gvar_written.push(1)
2086120865 else
2086220866 @gvar_types.push("int")
20867+ @gvar_written.push(0)
2086320868 end
2086420869 end
2086520870 end
@@ -29915,6 +29920,7 @@
2991529920 buf = ir_emit_sa(buf, "@cvar_init_values", @cvar_init_values)
2991629921 buf = ir_emit_sa(buf, "@gvar_names", @gvar_names)
2991729922 buf = ir_emit_sa(buf, "@gvar_types", @gvar_types)
29923+ buf = ir_emit_ia(buf, "@gvar_written", @gvar_written)
2991829924
2991929925 # Modules
2992029926 buf = ir_emit_sa(buf, "@module_names", @module_names)