Commit 3f8973a7

3f8973a7e8a5Merged

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

Description

Merge pull request #298 from ryanseys/rs-node-alias-global-var

feat(parse,codegen): support AliasGlobalVariableNode (alias $copy $orig)

Details

Commit
3f8973a7e8a5
Parent
f5fc728473ba
Merge parent
b7c6eaa4e0f5

spinel_codegen.rbmodifiedView file

@@ -97,6 +97,11 @@
9797 # (post-rest parameters aren't observed in test/), but the parser
9898 # emits the field so future tests get a proper AST.
9999 @nd_posts = "".split(",")
100+ # AliasMethodNode / AliasGlobalVariableNode -- parallel ref slots
101+ # for the new and old names (SymbolNode for methods, GlobalVariableReadNode
102+ # for globals).
103+ @nd_new_name = []
104+ @nd_old_name = []
100105
101106 @nd_count = 0
102107 @root_id = 0
@@ -203,6 +208,13 @@
203208 @redo_label_stack = "".split(",")
204209 @redo_label_counter = 0
205210
211+ # `alias $copy $orig` -- maps new gvar name to its target.
212+ # Populated by collect_all from AliasGlobalVariableNode
213+ # statements; consulted by sanitize_gvar / scan_features /
214+ # infer_type so $copy and $orig share storage.
215+ @galias_new = "".split(",")
216+ @galias_old = "".split(",")
217+
206218 # ---- Scope stack for local variables ----
207219 @scope_names = "".split(",")
208220 @scope_types = "".split(",")
@@ -485,6 +497,8 @@
485497 @nd_targets.push("")
486498 @nd_rights.push("")
487499 @nd_posts.push("")
500+ @nd_new_name.push(-1)
501+ @nd_old_name.push(-1)
488502 @nd_count = @nd_count + 1
489503 nid
490504 end
@@ -753,6 +767,14 @@
753767 if field == "call"
754768 @nd_receiver[nid] = ref_id
755769 end
770+ if field == "new_name"
771+ # AliasMethodNode / AliasGlobalVariableNode -- the new-name slot
772+ # (a SymbolNode for methods, GlobalVariableReadNode for globals).
773+ @nd_new_name[nid] = ref_id
774+ end
775+ if field == "old_name"
776+ @nd_old_name[nid] = ref_id
777+ end
756778 end
757779
758780 def set_array_field(nid, field, ids_str)
@@ -1912,7 +1934,10 @@
19121934 return "int"
19131935 end
19141936 if t == "GlobalVariableReadNode"
1915- gname = @nd_name[nid]
1937+ # `alias $copy $orig` -- a $copy read must look up $orig's
1938+ # registered type so the C codegen sees the correct format
1939+ # specifier when interpolating or printing.
1940+ gname = resolve_gvar_alias(@nd_name[nid])
19161941 gi = 0
19171942 while gi < @gvar_names.length
19181943 if @gvar_names[gi] == gname
@@ -5010,6 +5035,12 @@
50105035 end
50115036
50125037 def sanitize_gvar(name)
5038+ # `alias $copy $orig` -- transparently rewrite `$copy` to `$orig`
5039+ # everywhere it appears so the C output uses one storage slot
5040+ # for both names. Compile-time only; matches CRuby's
5041+ # `$copy and $orig share storage` semantics for the static-alias
5042+ # case.
5043+ name = resolve_gvar_alias(name)
50135044 # $last → gv_last, $1 → gv_1
50145045 if name.length > 0 && name[0] == "$"
50155046 return "gv_" + name[1, name.length - 1]
@@ -5017,6 +5048,17 @@
50175048 "gv_" + name
50185049 end
50195050
5051+ def resolve_gvar_alias(name)
5052+ i = 0
5053+ while i < @galias_new.length
5054+ if @galias_new[i] == name
5055+ return @galias_old[i]
5056+ end
5057+ i = i + 1
5058+ end
5059+ name
5060+ end
5061+
50205062 # ---- Array type helpers ----
50215063 def array_c_prefix(t)
50225064 if t == "str_array"
@@ -5440,6 +5482,20 @@
54405482 end
54415483 }
54425484
5485+ # Top-level `alias $copy $orig`. Records the rewrite in
5486+ # @galias_* so sanitize_gvar / scan_features / infer_type
5487+ # treat $copy and $orig as the same storage slot.
5488+ stmts.each { |sid|
5489+ if @nd_type[sid] == "AliasGlobalVariableNode"
5490+ nn = @nd_name[@nd_new_name[sid]]
5491+ on = @nd_name[@nd_old_name[sid]]
5492+ if nn != "" && on != ""
5493+ @galias_new.push(nn)
5494+ @galias_old.push(on)
5495+ end
5496+ end
5497+ }
5498+
54435499 # Pass 2.6: hoist `recv.instance_eval do ... end` blocks into
54445500 # file-scope static functions. Receiver-class flow analysis picks the
54455501 # receiver's class, the block body is later compiled as a function
@@ -11693,7 +11749,12 @@
1169311749 end
1169411750
1169511751 if t == "GlobalVariableWriteNode"
11696- gname = @nd_name[nid]
11752+ # `alias $copy $orig` -- a $copy = ... write must register
11753+ # the type under $orig's slot, not a separate $copy slot,
11754+ # otherwise the type-consistency check below would later see
11755+ # $orig and $copy as two slots that resolve to the same C
11756+ # global and fire a spurious type-mismatch error.
11757+ gname = resolve_gvar_alias(@nd_name[nid])
1169711758 if gname != "$stderr" && gname != "$stdout" && gname != "$?"
1169811759 gt = infer_type(@nd_expression[nid])
1169911760 if not_in(gname, @gvar_names) == 1
@@ -11715,7 +11776,9 @@
1171511776 end
1171611777 end
1171711778 if t == "GlobalVariableReadNode"
11718- gname = @nd_name[nid]
11779+ # Same alias resolution as the write side -- $copy reads must
11780+ # land on $orig's slot.
11781+ gname = resolve_gvar_alias(@nd_name[nid])
1171911782 if gname != "$stderr" && gname != "$stdout" && gname != "$?"
1172011783 if not_in(gname, @gvar_names) == 1
1172111784 @gvar_names.push(gname)
@@ -25398,6 +25461,13 @@
2539825461 emit(" goto " + lbl + ";")
2539925462 return
2540025463 end
25464+ if t == "AliasGlobalVariableNode"
25465+ # `alias $copy $orig` -- compile-time only. collect_all
25466+ # already populated @galias_*; references through
25467+ # sanitize_gvar / scan_features / infer_type pick up the
25468+ # rewrite. Nothing to emit at runtime.
25469+ return
25470+ end
2540125471 if t == "LocalVariableWriteNode"
2540225472 lname = @nd_name[nid]
2540325473 # Check for method(:name) assignment