Commit 90bf02b0

90bf02b07f53Merged

sacckey <[email protected]> · 6 months ago · 2 files · ~2 modified

Description

fix(codegen): track lexical scope for constant initialization and remove redundant root-builtin check

Details

Commit
90bf02b07f53
Parent
5f3707d3fb46

spinel_codegen.rbmodifiedView file

@@ -129,6 +129,7 @@
129129 @const_names = "".split(",")
130130 @const_types = "".split(",")
131131 @const_expr_ids = []
132+ @const_scope_names = "".split(",")
132133
133134 # ---- Scope stack for local variables ----
134135 @scope_names = "".split(",")
@@ -136,6 +137,7 @@
136137
137138 @current_class_idx = -1
138139 @current_method_name = ""
140+ @current_lexical_scope = ""
139141 @current_method_return = ""
140142 @in_main = 0
141143 @in_loop = 0
@@ -759,16 +761,6 @@
759761 0
760762 end
761763
762- def builtin_const_root_name(name)
763- if name == "Float" || name == "Integer" || name == "Math"
764- return 1
765- end
766- if name == "Array" || name == "Hash" || name == "Proc" || name == "StringIO" || name == "Fiber"
767- return 1
768- end
769- 0
770- end
771-
772764 def const_namespace_exists(name)
773765 if name == ""
774766 return 0
@@ -782,13 +774,13 @@
782774 if module_name_exists(name) == 1
783775 return 1
784776 end
785- if builtin_const_root_name(name) == 1
786- return 1
787- end
788777 0
789778 end
790779
791780 def current_lexical_scope_name
781+ if @current_lexical_scope != ""
782+ return @current_lexical_scope
783+ end
792784 if @current_class_idx >= 0
793785 if @current_class_idx < @cls_names.length
794786 return @cls_names[@current_class_idx]
@@ -1165,6 +1157,10 @@
11651157 if ci >= 0
11661158 return @const_types[ci]
11671159 end
1160+ cx = find_class_idx(cpname)
1161+ if cx >= 0
1162+ return "class_" + cpname
1163+ end
11681164 end
11691165 parent = @nd_receiver[nid]
11701166 if parent >= 0
@@ -3577,17 +3573,22 @@
35773573 end
35783574 ct = "int"
35793575 if expr_id >= 0
3576+ old_scope = @current_lexical_scope
3577+ @current_lexical_scope = scope_name
35803578 ct = infer_type(expr_id)
3579+ @current_lexical_scope = old_scope
35813580 end
35823581 ci = find_const_idx(cname)
35833582 if ci >= 0
35843583 @const_types[ci] = ct
35853584 @const_expr_ids[ci] = expr_id
3585+ @const_scope_names[ci] = scope_name
35863586 return
35873587 end
35883588 @const_names.push(cname)
35893589 @const_types.push(ct)
35903590 @const_expr_ids.push(expr_id)
3591+ @const_scope_names.push(scope_name)
35913592 end
35923593
35933594 def collect_class_with_prefix(nid, module_prefix)
@@ -4598,6 +4599,7 @@
45984599 @const_names.push(cname2)
45994600 @const_types.push(ct)
46004601 @const_expr_ids.push(expr_id)
4602+ @const_scope_names.push(mname)
46014603 end
46024604 }
46034605 end
@@ -11289,7 +11291,14 @@
1128911291 # Constants (initialize global declarations)
1129011292 i = 0
1129111293 while i < @const_names.length
11294+ old_scope = @current_lexical_scope
11295+ if i < @const_scope_names.length
11296+ @current_lexical_scope = @const_scope_names[i]
11297+ else
11298+ @current_lexical_scope = ""
11299+ end
1129211300 val = compile_expr(@const_expr_ids[i])
11301+ @current_lexical_scope = old_scope
1129311302 emit(" cst_" + @const_names[i] + " = " + val + ";")
1129411303 if type_is_pointer(@const_types[i]) == 1
1129511304 emit(" SP_GC_ROOT(cst_" + @const_names[i] + ");")