Commit 6567fefb

6567fefbf7fbMerged

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

Description

Merge pull request #283 from ryanseys/rs-node-redo

feat(parse,codegen): support RedoNode (redo)

Details

Commit
6567fefbf7fb
Parent
9fe821c2c615
Merge parent
31206f7ac549

spinel_codegen.rbmodifiedView file

@@ -191,6 +191,12 @@
191191 @const_expr_ids = []
192192 @const_scope_names = "".split(",")
193193
194+ # `redo` -- labeled-goto target stack. Each loop emitter pushes
195+ # a fresh label name when entering an iteration body and pops on
196+ # exit; a `redo` jumps to the top of the innermost label.
197+ @redo_label_stack = "".split(",")
198+ @redo_label_counter = 0
199+
194200 # ---- Scope stack for local variables ----
195201 @scope_names = "".split(",")
196202 @scope_types = "".split(",")
@@ -20469,7 +20475,10 @@
2046920475 @indent = @indent + 1
2047020476 push_scope
2047120477 declare_var(bp, "int")
20478+ redo_label = push_redo_label
20479+ emit_redo_label(redo_label)
2047220480 compile_stmts_body(@nd_body[@nd_block[nid]])
20481+ pop_redo_label
2047320482 pop_scope
2047420483 @indent = @indent - 1
2047520484 emit(" }")
@@ -25070,6 +25079,19 @@
2507025079 emit(" cvar_" + qname + " = " + val + ";")
2507125080 return
2507225081 end
25082+ if t == "RedoNode"
25083+ # `redo` -- jump to the top of the current iteration without
25084+ # advancing the iterator or re-evaluating the loop guard. The
25085+ # loop emitter installs a label via push_redo_label /
25086+ # emit_redo_label; we goto the innermost.
25087+ if @redo_label_stack.length == 0
25088+ $stderr.puts "Spinel: `redo` outside of a loop"
25089+ exit(1)
25090+ end
25091+ lbl = @redo_label_stack.last
25092+ emit(" goto " + lbl + ";")
25093+ return
25094+ end
2507325095 if t == "LocalVariableWriteNode"
2507425096 lname = @nd_name[nid]
2507525097 # Check for method(:name) assignment
@@ -26187,7 +26209,10 @@
2618726209 cond = compile_cond_expr(@nd_predicate[nid])
2618826210 emit(" while (" + cond + ") {")
2618926211 @indent = @indent + 1
26212+ redo_label = push_redo_label
26213+ emit_redo_label(redo_label)
2619026214 compile_stmts_body(@nd_body[nid])
26215+ pop_redo_label
2619126216 @indent = @indent - 1
2619226217 emit(" }")
2619326218 @hoisted_strlen_var = saved_var
@@ -26201,7 +26226,10 @@
2620126226 cond = compile_cond_expr(@nd_predicate[nid])
2620226227 emit(" while (!(" + cond + ")) {")
2620326228 @indent = @indent + 1
26229+ redo_label = push_redo_label
26230+ emit_redo_label(redo_label)
2620426231 compile_stmts_body(@nd_body[nid])
26232+ pop_redo_label
2620526233 @indent = @indent - 1
2620626234 emit(" }")
2620726235 @in_loop = old
@@ -26225,7 +26253,10 @@
2622526253 cmp = range_excl_end(coll) == 1 ? "<" : "<="
2622626254 emit(" for (lv_" + vname + " = " + left + "; lv_" + vname + " " + cmp + " " + right + "; lv_" + vname + "++) {")
2622726255 @indent = @indent + 1
26256+ redo_label = push_redo_label
26257+ emit_redo_label(redo_label)
2622826258 compile_stmts_body(@nd_body[nid])
26259+ pop_redo_label
2622926260 @indent = @indent - 1
2623026261 emit(" }")
2623126262 else
@@ -26235,10 +26266,15 @@
2623526266 tmp = new_temp
2623626267 pfx = array_c_prefix(ct)
2623726268 emit(" for (mrb_int " + tmp + " = 0; " + tmp + " < sp_" + pfx + "_length(" + rc + "); " + tmp + "++) {")
26269+ # Loop-var assignment stays OUTSIDE the redo label so `redo` re-runs
26270+ # the body with the same value (matches MRI: `redo` does not advance).
2623826271 emit(" lv_" + vname + " = sp_" + pfx + "_get(" + rc + ", " + tmp + ");")
2623926272
2624026273 @indent = @indent + 1
26274+ redo_label = push_redo_label
26275+ emit_redo_label(redo_label)
2624126276 compile_stmts_body(@nd_body[nid])
26277+ pop_redo_label
2624226278 @indent = @indent - 1
2624326279 emit(" }")
2624426280 end
@@ -27381,7 +27417,10 @@
2738127417 push_scope
2738227418 declare_var(bp1, elem_t)
2738327419 declare_var(bp2, elem_t)
27420+ redo_label = push_redo_label
27421+ emit_redo_label(redo_label)
2738427422 compile_stmts_body(@nd_body[@nd_block[nid]])
27423+ pop_redo_label
2738527424 pop_scope
2738627425 @indent = @indent - 1
2738727426 emit(" }")
@@ -27425,7 +27464,10 @@
2742527464 @indent = @indent + 1
2742627465 push_scope
2742727466 declare_var(bp1, "int")
27467+ redo_label = push_redo_label
27468+ emit_redo_label(redo_label)
2742827469 compile_stmts_body(@nd_body[@nd_block[nid]])
27470+ pop_redo_label
2742927471 pop_scope
2743027472 @indent = @indent - 1
2743127473 emit(" }")
@@ -27458,7 +27500,10 @@
2745827500 @indent = @indent + 1
2745927501 push_scope
2746027502 declare_var(bp1, et)
27503+ redo_label = push_redo_label
27504+ emit_redo_label(redo_label)
2746127505 compile_stmts_body(@nd_body[@nd_block[nid]])
27506+ pop_redo_label
2746227507 pop_scope
2746327508 @indent = @indent - 1
2746427509 emit(" }")
@@ -27554,7 +27599,10 @@
2755427599 @in_loop = 1
2755527600 emit(" while (1) {")
2755627601 @indent = @indent + 1
27602+ redo_label = push_redo_label
27603+ emit_redo_label(redo_label)
2755727604 compile_stmts_body(@nd_body[@nd_block[nid]])
27605+ pop_redo_label
2755827606 @indent = @indent - 1
2755927607 emit(" }")
2756027608 @in_loop = old
@@ -29536,7 +29584,10 @@
2953629584 @indent = @indent + 1
2953729585 push_scope
2953829586 declare_var(bp1, rt)
29587+ redo_label = push_redo_label
29588+ emit_redo_label(redo_label)
2953929589 compile_stmts_body(@nd_body[@nd_block[nid]])
29590+ pop_redo_label
2954029591 pop_scope
2954129592 @indent = @indent - 1
2954229593 emit(" }")
@@ -29575,7 +29626,10 @@
2957529626 @indent = @indent + 1
2957629627 push_scope
2957729628 declare_var(bp1, rt)
29629+ redo_label = push_redo_label
29630+ emit_redo_label(redo_label)
2957829631 compile_stmts_body(@nd_body[@nd_block[nid]])
29632+ pop_redo_label
2957929633 pop_scope
2958029634 @indent = @indent - 1
2958129635 emit(" }")
@@ -29628,7 +29682,10 @@
2962829682 push_scope
2962929683 declare_var(bp1, elem_type_of_array(rt))
2963029684 declare_var(bp2, obj_t)
29685+ redo_label = push_redo_label
29686+ emit_redo_label(redo_label)
2963129687 compile_stmts_body(@nd_body[@nd_block[nid]])
29688+ pop_redo_label
2963229689 pop_scope
2963329690 @indent = @indent - 1
2963429691 emit(" }")
@@ -29662,7 +29719,10 @@
2966229719 push_scope
2966329720 declare_var(bp1, elem_type_of_array(rt))
2966429721 declare_var(bp2, "int")
29722+ redo_label = push_redo_label
29723+ emit_redo_label(redo_label)
2966529724 compile_stmts_body(@nd_body[@nd_block[nid]])
29725+ pop_redo_label
2966629726 pop_scope
2966729727 @indent = @indent - 1
2966829728 emit(" }")
@@ -29720,7 +29780,10 @@
2972029780 if has_bp == 1
2972129781 declare_var(bp1, key_type)
2972229782 end
29783+ redo_label = push_redo_label
29784+ emit_redo_label(redo_label)
2972329785 compile_stmts_body(@nd_body[@nd_block[nid]])
29786+ pop_redo_label
2972429787 pop_scope
2972529788 @indent = @indent - 1
2972629789 emit(" }")
@@ -29751,7 +29814,10 @@
2975129814 if has_bp == 1
2975229815 declare_var(bp1, elem_type_of_array(rt))
2975329816 end
29817+ redo_label = push_redo_label
29818+ emit_redo_label(redo_label)
2975429819 compile_stmts_body(@nd_body[@nd_block[nid]])
29820+ pop_redo_label
2975529821 pop_scope
2975629822 @indent = @indent - 1
2975729823 emit(" }")
@@ -29767,7 +29833,10 @@
2976729833 if has_bp == 1
2976829834 declare_var(bp1, "symbol")
2976929835 end
29836+ redo_label = push_redo_label
29837+ emit_redo_label(redo_label)
2977029838 compile_stmts_body(@nd_body[@nd_block[nid]])
29839+ pop_redo_label
2977129840 pop_scope
2977229841 @indent = @indent - 1
2977329842 emit(" }")
@@ -29823,7 +29892,10 @@
2982329892 declare_var(bp1, elem_type)
2982429893 end
2982529894 end
29895+ redo_label = push_redo_label
29896+ emit_redo_label(redo_label)
2982629897 compile_stmts_body(@nd_body[@nd_block[nid]])
29898+ pop_redo_label
2982729899 pop_scope
2982829900 @indent = @indent - 1
2982929901 emit(" }")
@@ -29841,7 +29913,10 @@
2984129913 if bp2 != ""
2984229914 declare_var(bp2, "int")
2984329915 end
29916+ redo_label = push_redo_label
29917+ emit_redo_label(redo_label)
2984429918 compile_stmts_body(@nd_body[@nd_block[nid]])
29919+ pop_redo_label
2984529920 pop_scope
2984629921 @indent = @indent - 1
2984729922 emit(" }")
@@ -29859,7 +29934,10 @@
2985929934 if bp2 != ""
2986029935 declare_var(bp2, "string")
2986129936 end
29937+ redo_label = push_redo_label
29938+ emit_redo_label(redo_label)
2986229939 compile_stmts_body(@nd_body[@nd_block[nid]])
29940+ pop_redo_label
2986329941 pop_scope
2986429942 @indent = @indent - 1
2986529943 emit(" }")
@@ -29877,7 +29955,10 @@
2987729955 if bp2 != ""
2987829956 declare_var(bp2, "string")
2987929957 end
29958+ redo_label = push_redo_label
29959+ emit_redo_label(redo_label)
2988029960 compile_stmts_body(@nd_body[@nd_block[nid]])
29961+ pop_redo_label
2988129962 pop_scope
2988229963 @indent = @indent - 1
2988329964 emit(" }")
@@ -29895,7 +29976,10 @@
2989529976 if bp2 != ""
2989629977 declare_var(bp2, "int")
2989729978 end
29979+ redo_label = push_redo_label
29980+ emit_redo_label(redo_label)
2989829981 compile_stmts_body(@nd_body[@nd_block[nid]])
29982+ pop_redo_label
2989929983 pop_scope
2990029984 @indent = @indent - 1
2990129985 emit(" }")
@@ -29913,7 +29997,10 @@
2991329997 if bp2 != ""
2991429998 declare_var(bp2, "string")
2991529999 end
30000+ redo_label = push_redo_label
30001+ emit_redo_label(redo_label)
2991630002 compile_stmts_body(@nd_body[@nd_block[nid]])
30003+ pop_redo_label
2991730004 pop_scope
2991830005 @indent = @indent - 1
2991930006 emit(" }")
@@ -29931,7 +30018,10 @@
2993130018 if bp2 != ""
2993230019 declare_var(bp2, "poly")
2993330020 end
30021+ redo_label = push_redo_label
30022+ emit_redo_label(redo_label)
2993430023 compile_stmts_body(@nd_body[@nd_block[nid]])
30024+ pop_redo_label
2993530025 pop_scope
2993630026 @indent = @indent - 1
2993730027 emit(" }")
@@ -29949,7 +30039,10 @@
2994930039 if bp2 != ""
2995030040 declare_var(bp2, "poly")
2995130041 end
30042+ redo_label = push_redo_label
30043+ emit_redo_label(redo_label)
2995230044 compile_stmts_body(@nd_body[@nd_block[nid]])
30045+ pop_redo_label
2995330046 pop_scope
2995430047 @indent = @indent - 1
2995530048 emit(" }")
@@ -29964,7 +30057,10 @@
2996430057 if has_bp == 1
2996530058 declare_var(bp1, "int")
2996630059 end
30060+ redo_label = push_redo_label
30061+ emit_redo_label(redo_label)
2996730062 compile_stmts_body(@nd_body[@nd_block[nid]])
30063+ pop_redo_label
2996830064 pop_scope
2996930065 @indent = @indent - 1
2997030066 emit(" }")
@@ -29978,7 +30074,10 @@
2997830074 if has_bp == 1
2997930075 declare_var(bp1, "poly")
2998030076 end
30077+ redo_label = push_redo_label
30078+ emit_redo_label(redo_label)
2998130079 compile_stmts_body(@nd_body[@nd_block[nid]])
30080+ pop_redo_label
2998230081 pop_scope
2998330082 @indent = @indent - 1
2998430083 emit(" }")
@@ -30001,13 +30100,40 @@
3000130100 if bp1 != ""
3000230101 declare_var(bp1, "int")
3000330102 end
30103+ redo_label = push_redo_label
30104+ emit_redo_label(redo_label)
3000430105 compile_stmts_body(@nd_body[@nd_block[nid]])
30106+ pop_redo_label
3000530107 pop_scope
3000630108 @indent = @indent - 1
3000730109 emit(" }")
3000830110 @in_loop = old
3000930111 end
3001030112
30113+ # `redo` label-stack helpers. Each loop emitter wraps its body
30114+ # between push_redo_label / emit_redo_label / pop_redo_label so
30115+ # `redo` knows which label to jump to. Labels are unique per loop
30116+ # body via @redo_label_counter.
30117+ def push_redo_label
30118+ @redo_label_counter = @redo_label_counter + 1
30119+ lbl = "sp_redo_" + @redo_label_counter.to_s
30120+ @redo_label_stack.push(lbl)
30121+ lbl
30122+ end
30123+
30124+ def pop_redo_label
30125+ if @redo_label_stack.length > 0
30126+ @redo_label_stack.pop
30127+ end
30128+ end
30129+
30130+ # Emit the C label that `redo` jumps to. C requires labels to be
30131+ # followed by a statement; `;` lets a subsequent `}` stay valid
30132+ # even if the body is empty.
30133+ def emit_redo_label(lbl)
30134+ emit(" " + lbl + ": ;")
30135+ end
30136+
3001130137 def compile_upto_block(nid)
3001230138 old = @in_loop
3001330139 @in_loop = 1
@@ -30024,7 +30150,10 @@
3002430150 if bp1 != ""
3002530151 declare_var(bp1, "int")
3002630152 end
30153+ redo_label = push_redo_label
30154+ emit_redo_label(redo_label)
3002730155 compile_stmts_body(@nd_body[@nd_block[nid]])
30156+ pop_redo_label
3002830157 pop_scope
3002930158 @indent = @indent - 1
3003030159 emit(" }")
@@ -30047,7 +30176,10 @@
3004730176 if bp1 != ""
3004830177 declare_var(bp1, "int")
3004930178 end
30179+ redo_label = push_redo_label
30180+ emit_redo_label(redo_label)
3005030181 compile_stmts_body(@nd_body[@nd_block[nid]])
30182+ pop_redo_label
3005130183 pop_scope
3005230184 @indent = @indent - 1
3005330185 emit(" }")
@@ -31855,6 +31987,8 @@
3185531987 cond = compile_expr_remap(@nd_predicate[nid], map_from, map_to)
3185631988 emit(" while (" + cond + ") {")
3185731989 @indent = @indent + 1
31990+ redo_label = push_redo_label
31991+ emit_redo_label(redo_label)
3185831992 body = @nd_body[nid]
3185931993 if body >= 0
3186031994 stmts = get_stmts(body)
@@ -31864,6 +31998,7 @@
3186431998 sk = sk + 1
3186531999 end
3186632000 end
32001+ pop_redo_label
3186732002 @indent = @indent - 1
3186832003 emit(" }")
3186932004 @in_loop = old
@@ -32250,6 +32385,8 @@
3225032385 emit(" for (mrb_int " + tmp + " = 0; " + tmp + " < sp_IntArray_length(" + recv_expr + "); " + tmp + "++) {")
3225132386 emit(" lv_" + inner_bp_remapped + " = sp_IntArray_get(" + recv_expr + ", " + tmp + ");")
3225232387 @indent = @indent + 1
32388+ redo_label = push_redo_label
32389+ emit_redo_label(redo_label)
3225332390 # Compile inner block body, replacing yield with outer block body
3225432391 inner_blk = @nd_block[nid]
3225532392 if inner_blk >= 0
@@ -32289,6 +32426,7 @@
3228932426 end
3229032427 end
3229132428 end
32429+ pop_redo_label
3229232430 @indent = @indent - 1
3229332431 emit(" }")
3229432432 end