Commit 6ee8a675

6ee8a675b277Merged

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

Description

Merge pull request #90 from mame/pr/array-mul

feat(codegen): support Array#* (repeat) for typed arrays

Details

Commit
6ee8a675b277
Parent
667be99671bf
Merge parent
d161a0dbbf31

spinel_codegen.rbmodifiedView file

@@ -1726,6 +1726,10 @@
17261726 if lt == "poly"
17271727 return "poly"
17281728 end
1729+ if is_array_type(lt) == 1
1730+ # Array#* (repeat) yields another array of the same element type.
1731+ return lt
1732+ end
17291733 # Check RHS for float promotion
17301734 args_id = @nd_arguments[nid]
17311735 if args_id >= 0
@@ -3559,6 +3563,19 @@
35593563 0
35603564 end
35613565
3566+ # Set the right @needs_<runtime> flag for the given array type.
3567+ def mark_array_runtime_needs(t)
3568+ if t == "float_array"
3569+ @needs_float_array = 1
3570+ elsif t == "str_array"
3571+ @needs_str_array = 1
3572+ elsif t == "poly_array"
3573+ @needs_rb_value = 1
3574+ else
3575+ @needs_int_array = 1
3576+ end
3577+ end
3578+
35623579 # ---- Collection pass ----
35633580 def collect_all
35643581 root = @root_id
@@ -13908,6 +13925,22 @@
1390813925 @needs_rb_value = 1
1390913926 return "sp_poly_mul(" + compile_expr(recv) + ", " + box_expr_to_poly(get_args(@nd_arguments[nid])[0]) + ")"
1391013927 end
13928+ if is_array_type(lt) == 1
13929+ # All array kinds expose `_new` / `_length` / `_get` / `_push` with
13930+ # the same shape, so the repeat loop is a single template
13931+ # parameterized by the C prefix.
13932+ mark_array_runtime_needs(lt)
13933+ @needs_gc = 1
13934+ pfx = array_c_prefix(lt)
13935+ tmp = new_temp
13936+ src = new_temp
13937+ cnt = new_temp
13938+ emit(" sp_" + pfx + " *" + src + " = " + compile_expr(recv) + ";")
13939+ emit(" mrb_int " + cnt + " = " + compile_arg0(nid) + ";")
13940+ emit(" sp_" + pfx + " *" + tmp + " = sp_" + pfx + "_new();")
13941+ emit(" { mrb_int _mi; mrb_int _sl = sp_" + pfx + "_length(" + src + "); for (_mi = 0; _mi < " + cnt + "; _mi++) { mrb_int _mj; for (_mj = 0; _mj < _sl; _mj++) sp_" + pfx + "_push(" + tmp + ", sp_" + pfx + "_get(" + src + ", _mj)); } }")
13942+ return tmp
13943+ end
1391113944 return "(" + compile_expr(recv) + " * " + compile_arg0(nid) + ")"
1391213945 end
1391313946 if mname == "/"