Commit 0b18fb80

0b18fb801892Merged

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

Description

Merge pull request #290 from ryanseys/rs-node-class-var-read

feat(parse,codegen): support ClassVariableReadNode (@@var) + body init folding

Details

Commit
0b18fb801892
Parent
820ef0287db6
Merge parent
b8de8866c5f4

spinel_codegen.rbmodifiedView file

@@ -188,6 +188,12 @@
188188 # storage model better. Documented in the test fixtures.
189189 @cvar_names = "".split(",")
190190 @cvar_types = "".split(",")
191+ # Compile-time literal initializer per cvar, if the class-body
192+ # write was `@@x = <literal>`. "" means "use type-default". This
193+ # is necessary because Spinel doesn't run class-body statements
194+ # at startup, so any initializer that's not a fold-able literal
195+ # leaves the cvar at its type-default until first write.
196+ @cvar_init_values = "".split(",")
191197 @const_expr_ids = []
192198 @const_scope_names = "".split(",")
193199
@@ -1582,7 +1588,7 @@
15821588 end
15831589
15841590 # Register or update a cvar's inferred type. Called from
1585- # scan_class_vars during the pre-pass and (defensively) again from
1591+ # collect_cvars during the pre-pass and (defensively) again from
15861592 # compile_stmt when the write fires.
15871593 def register_cvar(qname, t)
15881594 ci = find_cvar_idx(qname)
@@ -1596,9 +1602,53 @@
15961602 end
15971603 @cvar_names.push(qname)
15981604 @cvar_types.push(t)
1605+ @cvar_init_values.push("")
15991606 @cvar_names.length - 1
16001607 end
16011608
1609+ # Try to compile-time fold a class-body cvar initializer. If the
1610+ # value is a simple literal (Integer/Float/String/Symbol/True/
1611+ # False/Nil), capture it as the static decl's initializer so the
1612+ # cvar enters the program with the source's value rather than the
1613+ # type default. Spinel doesn't run class-body statements at
1614+ # startup, so without this fold a `class C; @@x = 42; end` leaves
1615+ # cvar_C_x at 0 until the first write fires.
1616+ def try_fold_cvar_init(qname, value_id)
1617+ if value_id < 0
1618+ return
1619+ end
1620+ vt = @nd_type[value_id]
1621+ lit = ""
1622+ if vt == "IntegerNode"
1623+ lit = @nd_value[value_id].to_s
1624+ end
1625+ if vt == "FloatNode"
1626+ lit = @nd_content[value_id]
1627+ end
1628+ if vt == "StringNode"
1629+ lit = c_string_literal(@nd_content[value_id])
1630+ end
1631+ if vt == "SymbolNode"
1632+ lit = compile_symbol_literal(@nd_content[value_id])
1633+ end
1634+ if vt == "TrueNode"
1635+ lit = "TRUE"
1636+ end
1637+ if vt == "FalseNode"
1638+ lit = "FALSE"
1639+ end
1640+ if vt == "NilNode"
1641+ lit = "0"
1642+ end
1643+ if lit == ""
1644+ return
1645+ end
1646+ ci = find_cvar_idx(qname)
1647+ if ci >= 0
1648+ @cvar_init_values[ci] = lit
1649+ end
1650+ end
1651+
16021652 # If the constant's initializer is a simple literal, return the
16031653 # corresponding C expression. Otherwise return "" so callers fall
16041654 # back to cst_<name> lookup. Enables propagation of:
@@ -1877,6 +1927,14 @@
18771927 end
18781928 return "int"
18791929 end
1930+ if t == "ClassVariableReadNode"
1931+ qname = cvar_qname(@current_class_idx, @nd_name[nid])
1932+ ci = find_cvar_idx(qname)
1933+ if ci >= 0
1934+ return @cvar_types[ci]
1935+ end
1936+ return "int"
1937+ end
18801938 if t == "ConstantReadNode"
18811939 if @nd_name[nid] == "ARGV"
18821940 return "argv"
@@ -5833,6 +5891,7 @@
58335891 qname = cvar_qname(class_idx, @nd_name[nid])
58345892 val_t = infer_type(@nd_expression[nid])
58355893 register_cvar(qname, val_t)
5894+ try_fold_cvar_init(qname, @nd_expression[nid])
58365895 end
58375896 cs = []
58385897 push_child_ids(nid, cs)
@@ -13596,11 +13655,18 @@
1359613655 end
1359713656 # Emit file-scope class-variable declarations. One C global per
1359813657 # (class, name) pair, named `cvar_<ClassName>_<var>`; populated
13599- # by the ClassVariable*WriteNode codegen arms.
13658+ # by the ClassVariable*WriteNode codegen arms. If the class-body
13659+ # initializer was a simple literal, collect_cvars folded it into
13660+ # @cvar_init_values; otherwise we fall back to the type default
13661+ # and the cvar is undefined-acting until the first runtime write.
1360013662 i = 0
1360113663 while i < @cvar_names.length
1360213664 ctp = c_type(@cvar_types[i])
13603- emit_raw("static " + ctp + " cvar_" + @cvar_names[i] + " = " + c_default_val(@cvar_types[i]) + ";")
13665+ init = @cvar_init_values[i]
13666+ if init == ""
13667+ init = c_default_val(@cvar_types[i])
13668+ end
13669+ emit_raw("static " + ctp + " cvar_" + @cvar_names[i] + " = " + init + ";")
1360413670 i = i + 1
1360513671 end
1360613672 if @cvar_names.length > 0
@@ -17662,6 +17728,26 @@
1766217728 end
1766317729 return ivar_lhs(@nd_name[nid])
1766417730 end
17731+ if t == "ClassVariableReadNode"
17732+ # `@@var` -- reads the per-(class,name) C global registered
17733+ # by collect_cvars. @current_class_idx is set during class
17734+ # body and class/instance method compilation, so the lookup
17735+ # resolves to whichever class's body or method we're in.
17736+ return "cvar_" + cvar_qname(@current_class_idx, @nd_name[nid])
17737+ end
17738+ if t == "ClassVariableWriteNode"
17739+ # Expression-form write: `def tick; @@x = @@x + 1; end` --
17740+ # the method body's last expression is the write, and
17741+ # CRuby returns the assigned value. Emits the C-comma
17742+ # form so the assignment fires AND the value is the
17743+ # result, matching the InstanceVariableWriteNode pattern
17744+ # at line 14780.
17745+ qname = cvar_qname(@current_class_idx, @nd_name[nid])
17746+ val = compile_expr(@nd_expression[nid])
17747+ val_t = infer_type(@nd_expression[nid])
17748+ register_cvar(qname, val_t)
17749+ return "(cvar_" + qname + " = " + val + ")"
17750+ end
1766517751 if t == "InstanceVariableWriteNode"
1766617752 # Issue #130: same poly-slot boxing as the statement-form emit.
1766717753 # Expression form (`x = (@y = expr)`) is rarer but reaches the