Commit 5168e358
5168e358a9e1Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified
Description
Merge pull request #15 from khasinski/feat/instance-eval-block-form
Support instance_eval { block } in DSL receivers
Details
- Commit
- 5168e358a9e1
- Parent
- 3e89023db62e
- Merge parent
- a978f22747b6
spinel_codegen.rbmodifiedView file
@@ -271,6 +271,13 @@
271271
272272
# Symbol type Phase 2 Step 1: intern table (infrastructure only; unused yet).273273
@sym_names = "".split(",")274+
275+
# instance_eval block hoisting: parallel arrays indexed by synthetic276+
# function id N. Each lifted block becomes a file-scope static277+
# function `sp_ieval_<N>` that takes a typed `self` parameter.278+
@ieval_counter = 0279+
@ieval_class_idxs = []280+
@ieval_body_ids = []274281
end275282
276283
# Backslash-n for C string literals - bootstrap-safe (avoids escape level issues)@@ -4010,6 +4017,14 @@
40104017
end40114018
}40124019
4020+
# Pass 2.6: hoist `recv.instance_eval do ... end` blocks into4021+
# file-scope static functions. Receiver-class flow analysis picks the4022+
# receiver's class, the block body is later compiled as a function4023+
# with a typed `self` parameter, and the call site is rewritten to4024+
# invoke that function directly. v1: top-level locals previously4025+
# assigned `ClassName.new`; no block params; no closures; no yield.4026+
rewrite_instance_eval_calls4027+
40134028
# Pass 2.5: infer lambda parameter types from call sites40144029
infer_lambda_param_types40154030
@@ -4017,6 +4032,287 @@
40174032
infer_all_returns40184033
end40194034
4035+
def rewrite_instance_eval_calls4036+
@ieval_counter = 04037+
local_class = {}4038+
# Walk the AST recursively from the root, respecting scope boundaries.4039+
# `local_class` maps `name -> class_idx` for the current scope only.4040+
# Method/lambda/class/module/block bodies are NOT entered: their4041+
# locals belong to a different scope, so the top-level map must not4042+
# apply to them. A reassignment to a non-`Class.new` RHS poisons the4043+
# mapping for that name.4044+
ieval_walk(@root_id, local_class)4045+
end4046+
4047+
def ieval_walk(nid, local_class)4048+
if nid < 04049+
return4050+
end4051+
t = @nd_type[nid]4052+
if t == "ProgramNode"4053+
ieval_walk(@nd_body[nid], local_class)4054+
return4055+
end4056+
if t == "StatementsNode"4057+
stmts = parse_id_list(@nd_stmts[nid])4058+
k = 04059+
while k < stmts.length4060+
ieval_walk(stmts[k], local_class)4061+
k = k + 14062+
end4063+
return4064+
end4065+
if t == "LocalVariableWriteNode"4066+
val_nid = @nd_expression[nid]4067+
vname = @nd_name[nid]4068+
if val_nid >= 04069+
ieval_walk(val_nid, local_class)4070+
ci = ieval_expr_class_idx(val_nid)4071+
if ci >= 04072+
local_class[vname] = ci4073+
else4074+
if local_class.key?(vname)4075+
local_class.delete(vname)4076+
end4077+
end4078+
end4079+
return4080+
end4081+
if t == "CallNode"4082+
if @nd_name[nid] == "instance_eval"4083+
ieval_rewrite_call(nid, local_class)4084+
# Don't descend into the lifted block body.4085+
return4086+
end4087+
r = @nd_receiver[nid]4088+
if r >= 04089+
ieval_walk(r, local_class)4090+
end4091+
a = @nd_arguments[nid]4092+
if a >= 04093+
ieval_walk(a, local_class)4094+
end4095+
# Block bodies are a separate scope; don't recurse.4096+
return4097+
end4098+
if t == "ArgumentsNode"4099+
args = parse_id_list(@nd_args[nid])4100+
k = 04101+
while k < args.length4102+
ieval_walk(args[k], local_class)4103+
k = k + 14104+
end4105+
return4106+
end4107+
if t == "IfNode"4108+
ieval_walk(@nd_predicate[nid], local_class)4109+
ieval_walk(@nd_body[nid], local_class)4110+
ieval_walk(@nd_subsequent[nid], local_class)4111+
ieval_walk(@nd_else_clause[nid], local_class)4112+
return4113+
end4114+
if t == "UnlessNode"4115+
ieval_walk(@nd_predicate[nid], local_class)4116+
ieval_walk(@nd_body[nid], local_class)4117+
ieval_walk(@nd_else_clause[nid], local_class)4118+
return4119+
end4120+
if t == "ElseNode"4121+
ieval_walk(@nd_body[nid], local_class)4122+
return4123+
end4124+
if t == "WhileNode"4125+
ieval_walk(@nd_predicate[nid], local_class)4126+
ieval_walk(@nd_body[nid], local_class)4127+
return4128+
end4129+
if t == "UntilNode"4130+
ieval_walk(@nd_predicate[nid], local_class)4131+
ieval_walk(@nd_body[nid], local_class)4132+
return4133+
end4134+
if t == "CaseNode"4135+
ieval_walk(@nd_predicate[nid], local_class)4136+
conds = parse_id_list(@nd_conditions[nid])4137+
k = 04138+
while k < conds.length4139+
ieval_walk(conds[k], local_class)4140+
k = k + 14141+
end4142+
ieval_walk(@nd_else_clause[nid], local_class)4143+
return4144+
end4145+
if t == "WhenNode"4146+
ieval_walk(@nd_body[nid], local_class)4147+
return4148+
end4149+
if t == "BeginNode"4150+
ieval_walk(@nd_body[nid], local_class)4151+
ieval_walk(@nd_rescue_clause[nid], local_class)4152+
ieval_walk(@nd_ensure_clause[nid], local_class)4153+
return4154+
end4155+
# DefNode, LambdaNode, ClassNode, ModuleNode, BlockNode: not entered.4156+
# Their bodies introduce new scopes; the top-level map must not leak4157+
# in. Anything else: stop. Conservative — we won't rewrite.4158+
end4159+
4160+
def ieval_expr_class_idx(nid)4161+
if @nd_type[nid] == "CallNode"4162+
if @nd_name[nid] == "new"4163+
recv = @nd_receiver[nid]4164+
if recv >= 04165+
if @nd_type[recv] == "ConstantReadNode"4166+
return find_class_idx(@nd_name[recv])4167+
end4168+
# `Foo::Bar.new`: Spinel's class registry is keyed by the leaf4169+
# name, matching how `collect_class` records nested classes.4170+
if @nd_type[recv] == "ConstantPathNode"4171+
return find_class_idx(@nd_name[recv])4172+
end4173+
end4174+
end4175+
end4176+
-14177+
end4178+
4179+
def ieval_rewrite_call(nid, local_class)4180+
if @nd_name[nid] != "instance_eval"4181+
return4182+
end4183+
recv = @nd_receiver[nid]4184+
blk = @nd_block[nid]4185+
if recv < 04186+
return4187+
end4188+
if blk < 04189+
return4190+
end4191+
# Skip blocks with parameters: lifted function takes only `self`.4192+
if @nd_parameters[blk] >= 04193+
return4194+
end4195+
if @nd_type[recv] != "LocalVariableReadNode"4196+
return4197+
end4198+
vname = @nd_name[recv]4199+
if local_class.key?(vname) == false4200+
return4201+
end4202+
ci = local_class[vname]4203+
body_id = @nd_body[blk]4204+
# v1: bail if the block uses yield/block_given?. Lifting it as a4205+
# plain function would lose the enclosing method's block plumbing.4206+
# Spinel rejected such code before — leaving it rejected is no4207+
# regression, and the support belongs in a follow-up.4208+
if body_id >= 0 && body_has_yield(body_id) == 14209+
return4210+
end4211+
n = @ieval_counter4212+
@ieval_counter = @ieval_counter + 14213+
@ieval_class_idxs.push(ci)4214+
@ieval_body_ids.push(body_id)4215+
# Mark the call site: the function name doubles as the synthetic id.4216+
# compile_call_expr / compile_call_stmt recognise the prefix and4217+
# emit a direct C call to `sp_ieval_<N>`.4218+
@nd_name[nid] = "__sp_ieval_" + n.to_s4219+
@nd_block[nid] = -14220+
end4221+
4222+
def emit_ieval_funcs4223+
n = 04224+
while n < @ieval_class_idxs.length4225+
emit_ieval_func(n, @ieval_class_idxs[n], @ieval_body_ids[n])4226+
n = n + 14227+
end4228+
end4229+
4230+
# Type inference: walk each lifted block body with `@current_class_idx`4231+
# set to the receiver's class so bare self-calls inside the block4232+
# propagate arg types to the class's methods. Without this pass, a4233+
# block like `app.instance_eval { get("/") }` would fail to teach4234+
# `Routes#get(path)` that `path` is a string. Sibling pass to4235+
# `infer_class_body_call_types` for hoisted blocks.4236+
def infer_ieval_body_call_types4237+
n = 04238+
while n < @ieval_class_idxs.length4239+
ci = @ieval_class_idxs[n]4240+
bid = @ieval_body_ids[n]4241+
if bid >= 04242+
@current_class_idx = ci4243+
push_scope4244+
scan_cls_method_calls(ci, bid)4245+
scan_new_calls(bid)4246+
pop_scope4247+
@current_class_idx = -14248+
end4249+
n = n + 14250+
end4251+
end4252+
4253+
def is_ieval_call_name(mname)4254+
if mname.length <= 114255+
return 04256+
end4257+
if mname[0, 11] == "__sp_ieval_"4258+
return 14259+
end4260+
04261+
end4262+
4263+
def compile_ieval_call(nid)4264+
mname = @nd_name[nid]4265+
# Synthetic id is the suffix after the 11-char "__sp_ieval_" prefix.4266+
suffix = mname[11, mname.length - 11]4267+
"sp_ieval_" + suffix + "(" + compile_expr(@nd_receiver[nid]) + ")"4268+
end4269+
4270+
# v1 lifts blocks into void-returning functions (Ruby's4271+
# instance_eval-as-expression value isn't supported yet). When a4272+
# call appears in expression position, return the recv pointer as a4273+
# truthy default via a comma expression so callers like4274+
# `if obj.instance_eval { ... }` still type-check. Real expression4275+
# support — return the block's last expression — is a v2 follow-up.4276+
def compile_ieval_call_expr(nid)4277+
"(" + compile_ieval_call(nid) + ", " + compile_expr(@nd_receiver[nid]) + ")"4278+
end4279+
4280+
def emit_ieval_func(n, ci, bid)4281+
cname = @cls_names[ci]4282+
@current_class_idx = ci4283+
@current_method_name = "__sp_ieval_" + n.to_s4284+
@current_method_return = "void"4285+
@indent = 14286+
@in_gc_scope = 04287+
@in_yield_method = 04288+
4289+
if @cls_is_value_type[ci] == 14290+
emit_raw("static void sp_ieval_" + n.to_s + "(sp_" + cname + " self) {")4291+
else4292+
emit_raw("static void sp_ieval_" + n.to_s + "(sp_" + cname + " *self) {")4293+
end4294+
4295+
push_scope4296+
if bid >= 04297+
declare_method_locals(bid, "".split(","))4298+
if @needs_gc == 14299+
emit(" SP_GC_SAVE();")4300+
@in_gc_scope = 14301+
if @cls_is_value_type[ci] == 04302+
emit(" SP_GC_ROOT(self);")4303+
end4304+
end4305+
compile_body_return(bid, "void")4306+
end4307+
pop_scope4308+
4309+
@current_class_idx = -14310+
@current_method_name = ""4311+
@indent = 04312+
emit_raw("}")4313+
emit_raw("")4314+
end4315+
40204316
def is_builtin_type_name(name)40214317
if name == "Integer"40224318
return 1@@ -8716,6 +9012,7 @@
87169012
infer_main_call_types87179013
infer_function_body_call_types87189014
infer_class_body_call_types9015+
infer_ieval_body_call_types87199016
detect_poly_locals87209017
# Iterative type inference: converge param types, return types, ivar types.87219018
# Stop early when the signature of these three arrays stops changing.@@ -8790,6 +9087,7 @@
87909087
emit_global_constants87919088
emit_raw("/*LAMBDA_INSERT_POINT*/")87929089
emit_class_methods9090+
emit_ieval_funcs87939091
emit_toplevel_methods87949092
# Emit lambda functions before main (they are generated during compilation)87959093
# We emit them in emit_main after forward declarations@@ -13761,6 +14059,14 @@
1376114059
mname = @nd_name[nid]1376214060
recv = @nd_receiver[nid]1376314061
14062+
# Hoisted instance_eval block: emit a direct C call to the synthetic14063+
# file-scope function. The receiver is a local variable known to14064+
# carry a class instance (the rewriter checked this); pass it as the14065+
# typed `self` argument.14066+
if is_ieval_call_name(mname) == 114067+
return compile_ieval_call_expr(nid)14068+
end14069+
1376414070
# Fiber.new { block }1376514071
if mname == "new" && recv >= 01376614072
if constructor_class_name(recv) == "Fiber"@@ -20043,6 +20349,13 @@
2004320349
return2004420350
end2004520351
20352+
# Hoisted instance_eval block (statement context): the lifted20353+
# function returns void, so emit it as a plain statement.20354+
if is_ieval_call_name(mname) == 120355+
emit(" " + compile_ieval_call(nid) + ";")20356+
return20357+
end20358+
2004620359
# IO: puts, print, printf2004720360
if compile_io_call_stmt(nid, mname, recv) == 12004820361
returntest/bm_instance_eval.rbaddedView file
Loading diff…