Commit eae72ec8
eae72ec81822Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
feat: Phase 6 - exceptions, File I/O, system, catch/throw
Add begin/rescue/ensure via setjmp/longjmp with volatile locals. Add raise, retry support. Add File.read/write/exist?/delete. Add system(), backtick (popen), ENV[], Dir.home. Add catch/throw with nested catch stack. Fix c_string_literal to handle Ruby escape sequences.
Tests passing: bm_rescue, bm_fileio, bm_system, bm_catch
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- eae72ec81822
- Parent
- 167cac045044
spinel_codegen.rbmodifiedView file
@@ -739,6 +739,12 @@
739739
if t == "NilNode"740740
return "nil"741741
end742+
if t == "XStringNode"743+
return "string"744+
end745+
if t == "InterpolatedXStringNode"746+
return "string"747+
end742748
if t == "ArrayNode"743749
return infer_array_elem_type(nid)744750
end@@ -1079,6 +1085,34 @@
10791085
end10801086
end10811087
end1088+
# File operations1089+
if recv >= 01090+
if @nd_type[recv] == "ConstantReadNode"1091+
rcname = @nd_name[recv]1092+
if rcname == "File"1093+
if mname == "read"1094+
return "string"1095+
end1096+
if mname == "exist?"1097+
return "bool"1098+
end1099+
end1100+
if rcname == "ENV"1101+
if mname == "[]"1102+
return "string"1103+
end1104+
end1105+
if rcname == "Dir"1106+
if mname == "home"1107+
return "string"1108+
end1109+
end1110+
end1111+
end1112+
# backtick1113+
if mname == "`"1114+
return "string"1115+
end10821116
if mname == "sqrt"10831117
return "float"10841118
end@@ -2388,6 +2422,21 @@
23882422
return23892423
end23902424
t = @nd_type[nid]2425+
if t == "BeginNode"2426+
if @nd_rescue_clause[nid] >= 02427+
@needs_setjmp = 12428+
end2429+
if @nd_ensure_clause[nid] >= 02430+
@needs_setjmp = 12431+
end2432+
end2433+
if t == "InterpolatedXStringNode"2434+
@needs_file_io = 12435+
@needs_string_helpers = 12436+
end2437+
if t == "XStringNode"2438+
@needs_file_io = 12439+
end23912440
if t == "ArrayNode"23922441
et = infer_array_elem_type(nid)23932442
if et == "str_array"@@ -2490,6 +2539,26 @@
24902539
@needs_int_array = 124912540
@needs_gc = 124922541
end2542+
if mname == "raise"2543+
@needs_setjmp = 12544+
end2545+
if mname == "catch"2546+
@needs_setjmp = 12547+
end2548+
if mname == "throw"2549+
@needs_setjmp = 12550+
end2551+
if mname == "system"2552+
@needs_file_io = 12553+
end2554+
if @nd_receiver[nid] >= 02555+
if @nd_type[@nd_receiver[nid]] == "ConstantReadNode"2556+
rn = @nd_name[@nd_receiver[nid]]2557+
if rn == "File"2558+
@needs_file_io = 12559+
end2560+
end2561+
end24932562
if mname == "keys"24942563
@needs_str_array = 124952564
@needs_gc = 1@@ -2668,6 +2737,12 @@
26682737
end26692738
emit_string_helpers26702739
end2740+
if @needs_setjmp == 12741+
emit_setjmp_runtime2742+
end2743+
if @needs_file_io == 12744+
emit_file_io_runtime2745+
end26712746
emit_class_structs26722747
emit_forward_decls26732748
emit_class_methods@@ -2806,6 +2881,33 @@
28062881
emit_raw("")28072882
end28082883
2884+
def emit_setjmp_runtime2885+
emit_raw("#include <setjmp.h>")2886+
emit_raw("#define SP_EXC_STACK_MAX 64")2887+
emit_raw("static jmp_buf sp_exc_stack[SP_EXC_STACK_MAX];")2888+
emit_raw("static const char *sp_exc_msg[SP_EXC_STACK_MAX];")2889+
emit_raw("static volatile int sp_exc_top = 0;")2890+
emit_raw("static void sp_raise(const char *msg) { if (sp_exc_top > 0) { sp_exc_msg[sp_exc_top-1] = msg; longjmp(sp_exc_stack[sp_exc_top-1], 1); } fprintf(stderr, \"unhandled exception: %s\\n\", msg); exit(1); }")2891+
emit_raw("")2892+
# catch/throw support2893+
emit_raw("#define SP_CATCH_STACK_MAX 64")2894+
emit_raw("static jmp_buf sp_catch_stack[SP_CATCH_STACK_MAX];")2895+
emit_raw("static const char *sp_catch_tag[SP_CATCH_STACK_MAX];")2896+
emit_raw("static mrb_int sp_catch_val[SP_CATCH_STACK_MAX];")2897+
emit_raw("static volatile int sp_catch_top = 0;")2898+
emit_raw("static void sp_throw(const char *tag, mrb_int val) { int i = sp_catch_top - 1; while (i >= 0) { if (strcmp(sp_catch_tag[i], tag) == 0) { sp_catch_val[i] = val; sp_catch_top = i + 1; longjmp(sp_catch_stack[i], 1); } i--; } fprintf(stderr, \"uncaught throw: %s\\n\", tag); exit(1); }")2899+
emit_raw("")2900+
end2901+
2902+
def emit_file_io_runtime2903+
emit_raw("static const char *sp_file_read(const char *path) { FILE *f = fopen(path, \"rb\"); if (!f) return \"\"; fseek(f, 0, SEEK_END); long sz = ftell(f); fseek(f, 0, SEEK_SET); char *buf = (char *)malloc(sz + 1); if (sz > 0) fread(buf, 1, sz, f); buf[sz] = 0; fclose(f); return buf; }")2904+
emit_raw("static void sp_file_write(const char *path, const char *data) { FILE *f = fopen(path, \"w\"); if (f) { fputs(data, f); fclose(f); } }")2905+
emit_raw("static mrb_bool sp_file_exist(const char *path) { FILE *f = fopen(path, \"r\"); if (f) { fclose(f); return TRUE; } return FALSE; }")2906+
emit_raw("static void sp_file_delete(const char *path) { remove(path); }")2907+
emit_raw("static const char *sp_backtick(const char *cmd) { FILE *p = popen(cmd, \"r\"); if (!p) return \"\"; char *buf = (char *)malloc(4096); size_t n = fread(buf, 1, 4095, p); buf[n] = 0; pclose(p); return buf; }")2908+
emit_raw("")2909+
end2910+
28092911
# ---- Struct emission ----28102912
def emit_class_structs28112913
# Forward declare typedefs@@ -3632,6 +3734,19 @@
36323734
end36333735
end36343736
end3737+
# Rescue reference (=> e) needs to be declared as a local3738+
if @nd_type[nid] == "RescueNode"3739+
ref = @nd_reference[nid]3740+
if ref >= 03741+
lname = @nd_name[ref]3742+
if not_in(lname, names) == 13743+
if not_in(lname, params) == 13744+
names.push(lname)3745+
types.push("string")3746+
end3747+
end3748+
end3749+
end36353750
# Block parameters need to be declared as locals36363751
if @nd_type[nid] == "CallNode"36373752
blk = @nd_block[nid]@@ -3789,6 +3904,12 @@
37893904
if @nd_collection[nid] >= 037903905
scan_locals(@nd_collection[nid], names, types, params)37913906
end3907+
if @nd_rescue_clause[nid] >= 03908+
scan_locals(@nd_rescue_clause[nid], names, types, params)3909+
end3910+
if @nd_ensure_clause[nid] >= 03911+
scan_locals(@nd_ensure_clause[nid], names, types, params)3912+
end37923913
end37933914
37943915
def not_in(name, arr)@@ -3832,15 +3953,19 @@
38323953
i = i + 138333954
end38343955
3956+
vol = ""3957+
if @needs_setjmp == 13958+
vol = "volatile "3959+
end38353960
j = 038363961
while j < lnames.length38373962
declare_var(lnames[j], ltypes[j])38383963
ctp = c_type(ltypes[j])38393964
if type_is_pointer(ltypes[j]) == 13840-
emit(" " + ctp + "lv_" + lnames[j] + " = NULL;")3965+
emit(" " + vol + ctp + "lv_" + lnames[j] + " = NULL;")38413966
emit(" SP_GC_ROOT(lv_" + lnames[j] + ");")38423967
else3843-
emit(" " + ctp + " lv_" + lnames[j] + " = " + c_default_val(ltypes[j]) + ";")3968+
emit(" " + vol + ctp + " lv_" + lnames[j] + " = " + c_default_val(ltypes[j]) + ";")38443969
end38453970
j = j + 138463971
end@@ -3966,6 +4091,29 @@
39664091
if t == "DefinedNode"39674092
return "\"expression\""39684093
end4094+
if t == "XStringNode"4095+
@needs_file_io = 14096+
return "sp_backtick(" + c_string_literal(@nd_content[nid]) + ")"4097+
end4098+
if t == "InterpolatedXStringNode"4099+
@needs_file_io = 14100+
@needs_string_helpers = 14101+
interp = compile_interpolated(nid)4102+
return "sp_backtick(" + interp + ")"4103+
end4104+
if t == "GlobalVariableReadNode"4105+
gname = @nd_name[nid]4106+
if gname == "$stderr"4107+
return "0"4108+
end4109+
if gname == "$stdout"4110+
return "0"4111+
end4112+
if gname == "$?"4113+
return "sp_last_status"4114+
end4115+
return "0"4116+
end39694117
if t == "SourceLineNode"39704118
return @nd_value[nid].to_s39714119
end@@ -4002,7 +4150,40 @@
40024150
while i < s.length40034151
ch = s[i]40044152
if ch == "\\"4005-
result = result + "\\\\"4153+
# Check for Ruby escape sequences4154+
if i + 1 < s.length4155+
nch = s[i + 1]4156+
if nch == "n"4157+
result = result + "\\n"4158+
i = i + 24159+
else4160+
if nch == "t"4161+
result = result + "\\t"4162+
i = i + 24163+
else4164+
if nch == "r"4165+
result = result + "\\r"4166+
i = i + 24167+
else4168+
if nch == "\\"4169+
result = result + "\\\\"4170+
i = i + 24171+
else4172+
if nch == "\""4173+
result = result + "\\\""4174+
i = i + 24175+
else4176+
result = result + "\\\\"4177+
i = i + 14178+
end4179+
end4180+
end4181+
end4182+
end4183+
else4184+
result = result + "\\\\"4185+
i = i + 14186+
end40064187
else40074188
if ch == "\""40084189
result = result + "\\\""@@ -4021,8 +4202,8 @@
40214202
end40224203
end40234204
end4205+
i = i + 140244206
end4025-
i = i + 140264207
end40274208
result + "\""40284209
end@@ -4133,6 +4314,12 @@
41334314
41344315
# No receiver41354316
if recv < 04317+
# catch as expression4318+
if mname == "catch"4319+
if @nd_block[nid] >= 04320+
return compile_catch_expr(nid)4321+
end4322+
end41364323
if mname == "block_given?"41374324
if @in_yield_method == 141384325
return "(_block != NULL)"@@ -4457,11 +4644,39 @@
44574644
44584645
# Math44594646
if @nd_type[recv] == "ConstantReadNode"4460-
if @nd_name[recv] == "Math"4647+
rcname = @nd_name[recv]4648+
if rcname == "Math"44614649
if mname == "sqrt"44624650
return "sqrt(" + compile_arg0(nid) + ")"44634651
end44644652
end4653+
# File operations4654+
if rcname == "File"4655+
if mname == "read"4656+
@needs_file_io = 14657+
return "sp_file_read(" + compile_arg0(nid) + ")"4658+
end4659+
if mname == "exist?"4660+
@needs_file_io = 14661+
return "sp_file_exist(" + compile_arg0(nid) + ")"4662+
end4663+
if mname == "delete"4664+
@needs_file_io = 14665+
return "(sp_file_delete(" + compile_arg0(nid) + "), 0)"4666+
end4667+
end4668+
# ENV4669+
if rcname == "ENV"4670+
if mname == "[]"4671+
return "getenv(" + compile_arg0(nid) + ")"4672+
end4673+
end4674+
# Dir4675+
if rcname == "Dir"4676+
if mname == "home"4677+
return "getenv(\"HOME\")"4678+
end4679+
end44654680
end44664681
44674682
# to_a on range (including parenthesized range)@@ -4855,10 +5070,18 @@
48555070
emit(" continue;")48565071
return48575072
end5073+
if t == "RetryNode"5074+
emit(" continue;")5075+
return5076+
end48585077
if t == "YieldNode"48595078
compile_yield_stmt(nid)48605079
return48615080
end5081+
if t == "BeginNode"5082+
compile_begin_stmt(nid)5083+
return5084+
end48625085
if t == "CallNode"48635086
compile_call_stmt(nid)48645087
return@@ -5239,6 +5462,80 @@
52395462
end52405463
end52415464
5465+
# raise5466+
if mname == "raise"5467+
if recv < 05468+
@needs_setjmp = 15469+
msg = compile_arg0(nid)5470+
emit(" sp_raise(" + msg + ");")5471+
return5472+
end5473+
end5474+
5475+
# system5476+
if mname == "system"5477+
if recv < 05478+
@needs_file_io = 15479+
emit(" sp_last_status = system(" + compile_arg0(nid) + ");")5480+
return5481+
end5482+
end5483+
5484+
# trap (just ignore)5485+
if mname == "trap"5486+
if recv < 05487+
return5488+
end5489+
end5490+
5491+
# catch/throw5492+
if mname == "catch"5493+
if recv < 05494+
if @nd_block[nid] >= 05495+
compile_catch_stmt(nid)5496+
return5497+
end5498+
end5499+
end5500+
if mname == "throw"5501+
if recv < 05502+
compile_throw_stmt(nid)5503+
return5504+
end5505+
end5506+
5507+
# File operations5508+
if recv >= 05509+
if @nd_type[recv] == "ConstantReadNode"5510+
rcname = @nd_name[recv]5511+
if rcname == "File"5512+
if mname == "write"5513+
@needs_file_io = 15514+
args_id = @nd_arguments[nid]5515+
arg_ids = []5516+
if args_id >= 05517+
arg_ids = get_args(args_id)5518+
end5519+
a0 = "0"5520+
a1 = "0"5521+
if arg_ids.length >= 15522+
a0 = compile_expr(arg_ids[0])5523+
end5524+
if arg_ids.length >= 25525+
a1 = compile_expr(arg_ids[1])5526+
end5527+
emit(" sp_file_write(" + a0 + ", " + a1 + ");")5528+
return5529+
end5530+
if mname == "delete"5531+
@needs_file_io = 15532+
emit(" sp_file_delete(" + compile_arg0(nid) + ");")5533+
return5534+
end5535+
end5536+
end5537+
end5538+
52425539
# exit52435540
if mname == "exit"52445541
if recv < 0@@ -5770,6 +6067,186 @@
57706067
end57716068
end57726069
6070+
def compile_catch_expr(nid)6071+
@needs_setjmp = 16072+
tag = compile_arg0(nid)6073+
blk = @nd_block[nid]6074+
tmp = new_temp6075+
6076+
emit(" mrb_int " + tmp + " = 0;")6077+
emit(" sp_catch_tag[sp_catch_top] = " + tag + ";")6078+
emit(" sp_catch_top++;")6079+
emit(" if (setjmp(sp_catch_stack[sp_catch_top-1]) == 0) {")6080+
@indent = @indent + 16081+
if blk >= 06082+
body = @nd_body[blk]6083+
if body >= 06084+
stmts = get_stmts(body)6085+
# Compile all but last as statements6086+
k = 06087+
while k < stmts.length - 16088+
compile_stmt(stmts[k])6089+
k = k + 16090+
end6091+
if stmts.length > 06092+
last = stmts[stmts.length - 1]6093+
emit(" " + tmp + " = " + compile_expr(last) + ";")6094+
end6095+
end6096+
end6097+
@indent = @indent - 16098+
emit(" sp_catch_top--;")6099+
emit(" } else {")6100+
emit(" sp_catch_top--;")6101+
emit(" " + tmp + " = sp_catch_val[sp_catch_top];")6102+
emit(" }")6103+
tmp6104+
end6105+
6106+
def compile_catch_stmt(nid)6107+
@needs_setjmp = 16108+
# catch(:tag) do ... end6109+
tag = compile_arg0(nid)6110+
blk = @nd_block[nid]6111+
bp1 = get_block_param(nid, 0)6112+
6113+
emit(" sp_catch_tag[sp_catch_top] = " + tag + ";")6114+
emit(" sp_catch_top++;")6115+
emit(" if (setjmp(sp_catch_stack[sp_catch_top-1]) == 0) {")6116+
@indent = @indent + 16117+
if blk >= 06118+
compile_stmts_body(@nd_body[blk])6119+
end6120+
@indent = @indent - 16121+
emit(" sp_catch_top--;")6122+
emit(" } else {")6123+
emit(" sp_catch_top--;")6124+
emit(" }")6125+
end6126+
6127+
def compile_throw_stmt(nid)6128+
@needs_setjmp = 16129+
args_id = @nd_arguments[nid]6130+
arg_ids = []6131+
if args_id >= 06132+
arg_ids = get_args(args_id)6133+
end6134+
tag = "\"\""6135+
val = "0"6136+
if arg_ids.length >= 16137+
tag = compile_expr(arg_ids[0])6138+
end6139+
if arg_ids.length >= 26140+
val = compile_expr(arg_ids[1])6141+
end6142+
emit(" sp_throw(" + tag + ", " + val + ");")6143+
end6144+
6145+
def compile_begin_stmt(nid)6146+
@needs_setjmp = 16147+
has_rescue = @nd_rescue_clause[nid] >= 06148+
has_ensure = @nd_ensure_clause[nid] >= 06149+
6150+
# Check if rescue body has retry6151+
has_retry = 06152+
rc = @nd_rescue_clause[nid]6153+
if rc >= 06154+
if body_has_retry(@nd_body[rc]) == 16155+
has_retry = 16156+
end6157+
end6158+
6159+
if has_retry == 16160+
emit(" for (;;) {")6161+
@indent = @indent + 16162+
end6163+
6164+
if has_rescue6165+
emit(" sp_exc_top++;")6166+
emit(" if (setjmp(sp_exc_stack[sp_exc_top-1]) == 0) {")6167+
@indent = @indent + 16168+
compile_stmts_body(@nd_body[nid])6169+
@indent = @indent - 16170+
emit(" sp_exc_top--;")6171+
if has_retry == 16172+
emit(" break;")6173+
end6174+
6175+
rc = @nd_rescue_clause[nid]6176+
if rc >= 06177+
emit(" } else {")6178+
emit(" sp_exc_top--;")6179+
@indent = @indent + 16180+
ref = @nd_reference[rc]6181+
if ref >= 06182+
rname = @nd_name[ref]6183+
emit(" lv_" + rname + " = sp_exc_msg[sp_exc_top];")6184+
end6185+
compile_rescue_body(@nd_body[rc], has_retry)6186+
@indent = @indent - 16187+
end6188+
emit(" }")6189+
else6190+
compile_stmts_body(@nd_body[nid])6191+
end6192+
6193+
if has_retry == 16194+
@indent = @indent - 16195+
emit(" }")6196+
end6197+
6198+
if has_ensure6199+
ec = @nd_ensure_clause[nid]6200+
if ec >= 06201+
compile_stmts_body(@nd_body[ec])6202+
end6203+
end6204+
end6205+
6206+
def compile_rescue_body(nid, has_retry)6207+
if nid < 06208+
return6209+
end6210+
stmts = get_stmts(nid)6211+
k = 06212+
while k < stmts.length6213+
compile_stmt(stmts[k])6214+
k = k + 16215+
end6216+
if has_retry == 16217+
# If we get here without a retry (continue), break out of the loop6218+
emit(" break;")6219+
end6220+
end6221+
6222+
def body_has_retry(nid)6223+
if nid < 06224+
return 06225+
end6226+
if @nd_type[nid] == "RetryNode"6227+
return 16228+
end6229+
stmts = parse_id_list(@nd_stmts[nid])6230+
k = 06231+
while k < stmts.length6232+
if body_has_retry(stmts[k]) == 16233+
return 16234+
end6235+
k = k + 16236+
end6237+
if @nd_body[nid] >= 06238+
if body_has_retry(@nd_body[nid]) == 16239+
return 16240+
end6241+
end6242+
if @nd_subsequent[nid] >= 06243+
if body_has_retry(@nd_subsequent[nid]) == 16244+
return 16245+
end6246+
end6247+
06248+
end6249+
57736250
def compile_yield_stmt(nid)57746251
args_id = @nd_arguments[nid]57756252
arg_exprs = ""@@ -6518,6 +6995,13 @@
65186995
end65196996
return65206997
end6998+
if @nd_type[last] == "BeginNode"6999+
compile_begin_stmt(last)7000+
if return_type != "void"7001+
emit(" return " + c_default_val(return_type) + ";")7002+
end7003+
return7004+
end65217005
# If last statement is a CallNode with a block, handle map/select as expressions65227006
if @nd_type[last] == "CallNode"65237007
if @nd_block[last] >= 0