Commit bc391d05

bc391d056802Merged

Yusuke Endoh <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

fix(codegen): lower int-keyed Hash literal {0=>v0,1=>v1,...} to Array

A Hash literal whose keys are the consecutive non-negative integers 0..N-1 is semantically equivalent to the Array [v0, v1, ..., vN-1] for the common H[k] lookup pattern. Spinel's runtime has no int__hash slot, so any such literal previously fell through to the str_int_hash default — H[0] miscompiled to a str-keyed lookup that returned 0, and the subsequent .length / chained [] crashed at runtime.

Detect the pattern at the AST level (IntegerNode keys with literal values 0,1,...,N-1 in source order) and route both type inference and emission through the existing Array paths via a small refactor that extracts the body of infer_array_elem_type and compile_array_literal into id-list-parameterised helpers.

Detection is shape-only — any deviation (gap key, duplicate, non-integer key, splat) opts back into the regular hash codegen, so existing hash literals are unchanged.

Unblocks the optcarrot SP_PIXEL_POSITIONS constant (the pixel-shuffle table indexed by 0/1).

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
bc391d056802
Parent
01f36bc8ec7a

spinel_codegen.rbmodifiedView file

@@ -2288,6 +2288,9 @@
22882288 return infer_array_elem_type(nid)
22892289 end
22902290 if t == "HashNode"
2291+ if is_int_array_lowered_hash(nid) == 1
2292+ return infer_int_keyed_hash_as_array_type(nid)
2293+ end
22912294 return infer_hash_val_type(nid)
22922295 end
22932296 if t == "RangeNode"
@@ -2558,7 +2561,14 @@
25582561 end
25592562
25602563 def infer_array_elem_type(nid)
2561- elems = parse_id_list(@nd_elements[nid])
2564+ infer_array_elem_type_from_ids(parse_id_list(@nd_elements[nid]))
2565+ end
2566+
2567+ # Body of infer_array_elem_type, parameterised on the list of value
2568+ # node ids. Lets a {0=>v0,1=>v1,...} HashNode lowered to an Array
2569+ # share the same type-inference logic by feeding in [v0, v1, ...]
2570+ # without needing an actual ArrayNode to host them.
2571+ def infer_array_elem_type_from_ids(elems)
25622572 if elems.length > 0
25632573 et = infer_type(elems[0])
25642574 if et == "symbol"
@@ -2662,6 +2672,53 @@
26622672 "int_array"
26632673 end
26642674
2675+ # Detects a Hash literal whose keys are the consecutive non-negative
2676+ # integers 0, 1, ..., N-1 in source order. Such a literal is
2677+ # semantically equivalent to the Array `[v0, v1, ..., vN-1]` for the
2678+ # common `H[k]` lookup pattern, so we lower it to an Array
2679+ # internally — no `int_<X>_hash` runtime type needed. Detection is
2680+ # AST-shape only (IntegerNode key with literal value `k` at index
2681+ # `k`); any deviation (gap, duplicate, non-integer key, splat) opts
2682+ # back into the regular hash codegen.
2683+ def is_int_array_lowered_hash(nid)
2684+ if @nd_type[nid] != "HashNode"
2685+ return 0
2686+ end
2687+ elems = parse_id_list(@nd_elements[nid])
2688+ if elems.length == 0
2689+ return 0
2690+ end
2691+ k = 0
2692+ while k < elems.length
2693+ eid = elems[k]
2694+ if @nd_type[eid] != "AssocNode"
2695+ return 0
2696+ end
2697+ kid = @nd_key[eid]
2698+ if kid < 0 || @nd_type[kid] != "IntegerNode"
2699+ return 0
2700+ end
2701+ if @nd_value[kid].to_i != k
2702+ return 0
2703+ end
2704+ k = k + 1
2705+ end
2706+ 1
2707+ end
2708+
2709+ # Returns the array type the lowered HashNode evaluates to. Same
2710+ # logic as infer_array_elem_type but reads each AssocNode's value.
2711+ def infer_int_keyed_hash_as_array_type(nid)
2712+ elems = parse_id_list(@nd_elements[nid])
2713+ vids = []
2714+ k = 0
2715+ while k < elems.length
2716+ vids.push(@nd_expression[elems[k]])
2717+ k = k + 1
2718+ end
2719+ infer_array_elem_type_from_ids(vids)
2720+ end
2721+
26652722 def infer_hash_val_type(nid)
26662723 elems = parse_id_list(@nd_elements[nid])
26672724 if elems.length > 0
@@ -8095,6 +8152,9 @@
80958152 return infer_array_elem_type(nid)
80968153 end
80978154 if t == "HashNode"
8155+ if is_int_array_lowered_hash(nid) == 1
8156+ return infer_int_keyed_hash_as_array_type(nid)
8157+ end
80988158 return infer_hash_val_type(nid)
80998159 end
81008160 if t == "CallNode"
@@ -13267,21 +13327,37 @@
1326713327 @needs_gc = 1
1326813328 end
1326913329 if t == "HashNode"
13270- ht = infer_hash_val_type(nid)
13271- if ht == "str_str_hash"
13272- @needs_str_str_hash = 1
13273- elsif ht == "int_str_hash"
13274- @needs_int_str_hash = 1
13275- @needs_int_array = 1
13276- elsif ht == "sym_int_hash"
13277- @needs_sym_int_hash = 1
13278- elsif ht == "sym_str_hash"
13279- @needs_sym_str_hash = 1
13330+ if is_int_array_lowered_hash(nid) == 1
13331+ # Lowered to Array — same @needs_* flag set as the
13332+ # ArrayNode arm above derives from `infer_array_elem_type`.
13333+ et = infer_int_keyed_hash_as_array_type(nid)
13334+ if et == "str_array"
13335+ @needs_str_array = 1
13336+ elsif et == "poly_array"
13337+ @needs_rb_value = 1
13338+ elsif et == "float_array"
13339+ @needs_float_array = 1
13340+ else
13341+ @needs_int_array = 1
13342+ end
13343+ @needs_gc = 1
1328013344 else
13281- @needs_str_int_hash = 1
13345+ ht = infer_hash_val_type(nid)
13346+ if ht == "str_str_hash"
13347+ @needs_str_str_hash = 1
13348+ elsif ht == "int_str_hash"
13349+ @needs_int_str_hash = 1
13350+ @needs_int_array = 1
13351+ elsif ht == "sym_int_hash"
13352+ @needs_sym_int_hash = 1
13353+ elsif ht == "sym_str_hash"
13354+ @needs_sym_str_hash = 1
13355+ else
13356+ @needs_str_int_hash = 1
13357+ end
13358+ @needs_gc = 1
13359+ @needs_str_array = 1
1328213360 end
13283- @needs_gc = 1
13284- @needs_str_array = 1
1328513361 end
1328613362
1328713363 if t == "GlobalVariableWriteNode"
@@ -28459,7 +28535,16 @@
2845928535 @needs_int_array = 1
2846028536 return "sp_IntArray_new()"
2846128537 end
28462- arr_type = infer_array_elem_type(nid)
28538+ compile_array_literal_from_ids(elems, infer_array_elem_type(nid))
28539+ end
28540+
28541+ # Body of compile_array_literal, parameterised on the list of value
28542+ # node ids and the resolved array element type. Lets a HashNode
28543+ # whose keys are 0..N-1 (lowered to Array) reuse the same emission
28544+ # paths by feeding in the AssocNode rhs ids without needing an
28545+ # ArrayNode wrapper.
28546+ def compile_array_literal_from_ids(elems, arr_type)
28547+ @needs_gc = 1
2846328548 if is_tuple_type(arr_type) == 1
2846428549 name = tuple_c_name(arr_type)
2846528550 tmp = new_temp
@@ -28594,8 +28679,26 @@
2859428679 tmp
2859528680 end
2859628681
28682+ # HashNode literal whose keys are 0..N-1 lowered to an Array
28683+ # literal. Reuses compile_array_literal_from_ids by mapping each
28684+ # AssocNode to its rhs value id.
28685+ def compile_int_keyed_hash_as_array(nid)
28686+ @needs_gc = 1
28687+ elems = parse_id_list(@nd_elements[nid])
28688+ vids = []
28689+ k = 0
28690+ while k < elems.length
28691+ vids.push(@nd_expression[elems[k]])
28692+ k = k + 1
28693+ end
28694+ compile_array_literal_from_ids(vids, infer_array_elem_type_from_ids(vids))
28695+ end
28696+
2859728697 def compile_hash_literal(nid)
2859828698 @needs_gc = 1
28699+ if is_int_array_lowered_hash(nid) == 1
28700+ return compile_int_keyed_hash_as_array(nid)
28701+ end
2859928702 elems = parse_id_list(@nd_elements[nid])
2860028703 if elems.length == 0
2860128704 @needs_str_int_hash = 1