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-body192+
# write was `@@x = <literal>`. "" means "use type-default". This193+
# is necessary because Spinel doesn't run class-body statements194+
# at startup, so any initializer that's not a fold-able literal195+
# 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
end15831589
15841590
# Register or update a cvar's inferred type. Called from1585-
# scan_class_vars during the pre-pass and (defensively) again from1591+
# collect_cvars during the pre-pass and (defensively) again from15861592
# compile_stmt when the write fires.15871593
def register_cvar(qname, t)15881594
ci = find_cvar_idx(qname)@@ -1596,9 +1602,53 @@
15961602
end15971603
@cvar_names.push(qname)15981604
@cvar_types.push(t)1605+
@cvar_init_values.push("")15991606
@cvar_names.length - 116001607
end16011608
1609+
# Try to compile-time fold a class-body cvar initializer. If the1610+
# value is a simple literal (Integer/Float/String/Symbol/True/1611+
# False/Nil), capture it as the static decl's initializer so the1612+
# cvar enters the program with the source's value rather than the1613+
# type default. Spinel doesn't run class-body statements at1614+
# startup, so without this fold a `class C; @@x = 42; end` leaves1615+
# cvar_C_x at 0 until the first write fires.1616+
def try_fold_cvar_init(qname, value_id)1617+
if value_id < 01618+
return1619+
end1620+
vt = @nd_type[value_id]1621+
lit = ""1622+
if vt == "IntegerNode"1623+
lit = @nd_value[value_id].to_s1624+
end1625+
if vt == "FloatNode"1626+
lit = @nd_content[value_id]1627+
end1628+
if vt == "StringNode"1629+
lit = c_string_literal(@nd_content[value_id])1630+
end1631+
if vt == "SymbolNode"1632+
lit = compile_symbol_literal(@nd_content[value_id])1633+
end1634+
if vt == "TrueNode"1635+
lit = "TRUE"1636+
end1637+
if vt == "FalseNode"1638+
lit = "FALSE"1639+
end1640+
if vt == "NilNode"1641+
lit = "0"1642+
end1643+
if lit == ""1644+
return1645+
end1646+
ci = find_cvar_idx(qname)1647+
if ci >= 01648+
@cvar_init_values[ci] = lit1649+
end1650+
end1651+
16021652
# If the constant's initializer is a simple literal, return the16031653
# corresponding C expression. Otherwise return "" so callers fall16041654
# back to cst_<name> lookup. Enables propagation of:@@ -1877,6 +1927,14 @@
18771927
end18781928
return "int"18791929
end1930+
if t == "ClassVariableReadNode"1931+
qname = cvar_qname(@current_class_idx, @nd_name[nid])1932+
ci = find_cvar_idx(qname)1933+
if ci >= 01934+
return @cvar_types[ci]1935+
end1936+
return "int"1937+
end18801938
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
end58375896
cs = []58385897
push_child_ids(nid, cs)@@ -13596,11 +13655,18 @@
1359613655
end1359713656
# Emit file-scope class-variable declarations. One C global per1359813657
# (class, name) pair, named `cvar_<ClassName>_<var>`; populated13599-
# by the ClassVariable*WriteNode codegen arms.13658+
# by the ClassVariable*WriteNode codegen arms. If the class-body13659+
# initializer was a simple literal, collect_cvars folded it into13660+
# @cvar_init_values; otherwise we fall back to the type default13661+
# and the cvar is undefined-acting until the first runtime write.1360013662
i = 01360113663
while i < @cvar_names.length1360213664
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+
end13669+
emit_raw("static " + ctp + " cvar_" + @cvar_names[i] + " = " + init + ";")1360413670
i = i + 11360513671
end1360613672
if @cvar_names.length > 0@@ -17662,6 +17728,26 @@
1766217728
end1766317729
return ivar_lhs(@nd_name[nid])1766417730
end17731+
if t == "ClassVariableReadNode"17732+
# `@@var` -- reads the per-(class,name) C global registered17733+
# by collect_cvars. @current_class_idx is set during class17734+
# body and class/instance method compilation, so the lookup17735+
# resolves to whichever class's body or method we're in.17736+
return "cvar_" + cvar_qname(@current_class_idx, @nd_name[nid])17737+
end17738+
if t == "ClassVariableWriteNode"17739+
# Expression-form write: `def tick; @@x = @@x + 1; end` --17740+
# the method body's last expression is the write, and17741+
# CRuby returns the assigned value. Emits the C-comma17742+
# form so the assignment fires AND the value is the17743+
# result, matching the InstanceVariableWriteNode pattern17744+
# 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+
end1766517751
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 thespinel_parse.cmodifiedView file
Loading diff…
test/class_var_read.rbaddedView file
Loading diff…
test/class_var_read.rb.expectedaddedView file
Loading diff…