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 end
742+ if t == "XStringNode"
743+ return "string"
744+ end
745+ if t == "InterpolatedXStringNode"
746+ return "string"
747+ end
742748 if t == "ArrayNode"
743749 return infer_array_elem_type(nid)
744750 end
@@ -1079,6 +1085,34 @@
10791085 end
10801086 end
10811087 end
1088+ # File operations
1089+ if recv >= 0
1090+ if @nd_type[recv] == "ConstantReadNode"
1091+ rcname = @nd_name[recv]
1092+ if rcname == "File"
1093+ if mname == "read"
1094+ return "string"
1095+ end
1096+ if mname == "exist?"
1097+ return "bool"
1098+ end
1099+ end
1100+ if rcname == "ENV"
1101+ if mname == "[]"
1102+ return "string"
1103+ end
1104+ end
1105+ if rcname == "Dir"
1106+ if mname == "home"
1107+ return "string"
1108+ end
1109+ end
1110+ end
1111+ end
1112+ # backtick
1113+ if mname == "`"
1114+ return "string"
1115+ end
10821116 if mname == "sqrt"
10831117 return "float"
10841118 end
@@ -2388,6 +2422,21 @@
23882422 return
23892423 end
23902424 t = @nd_type[nid]
2425+ if t == "BeginNode"
2426+ if @nd_rescue_clause[nid] >= 0
2427+ @needs_setjmp = 1
2428+ end
2429+ if @nd_ensure_clause[nid] >= 0
2430+ @needs_setjmp = 1
2431+ end
2432+ end
2433+ if t == "InterpolatedXStringNode"
2434+ @needs_file_io = 1
2435+ @needs_string_helpers = 1
2436+ end
2437+ if t == "XStringNode"
2438+ @needs_file_io = 1
2439+ end
23912440 if t == "ArrayNode"
23922441 et = infer_array_elem_type(nid)
23932442 if et == "str_array"
@@ -2490,6 +2539,26 @@
24902539 @needs_int_array = 1
24912540 @needs_gc = 1
24922541 end
2542+ if mname == "raise"
2543+ @needs_setjmp = 1
2544+ end
2545+ if mname == "catch"
2546+ @needs_setjmp = 1
2547+ end
2548+ if mname == "throw"
2549+ @needs_setjmp = 1
2550+ end
2551+ if mname == "system"
2552+ @needs_file_io = 1
2553+ end
2554+ if @nd_receiver[nid] >= 0
2555+ if @nd_type[@nd_receiver[nid]] == "ConstantReadNode"
2556+ rn = @nd_name[@nd_receiver[nid]]
2557+ if rn == "File"
2558+ @needs_file_io = 1
2559+ end
2560+ end
2561+ end
24932562 if mname == "keys"
24942563 @needs_str_array = 1
24952564 @needs_gc = 1
@@ -2668,6 +2737,12 @@
26682737 end
26692738 emit_string_helpers
26702739 end
2740+ if @needs_setjmp == 1
2741+ emit_setjmp_runtime
2742+ end
2743+ if @needs_file_io == 1
2744+ emit_file_io_runtime
2745+ end
26712746 emit_class_structs
26722747 emit_forward_decls
26732748 emit_class_methods
@@ -2806,6 +2881,33 @@
28062881 emit_raw("")
28072882 end
28082883
2884+ def emit_setjmp_runtime
2885+ 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 support
2893+ 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+ end
2901+
2902+ def emit_file_io_runtime
2903+ 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+ end
2910+
28092911 # ---- Struct emission ----
28102912 def emit_class_structs
28112913 # Forward declare typedefs
@@ -3632,6 +3734,19 @@
36323734 end
36333735 end
36343736 end
3737+ # Rescue reference (=> e) needs to be declared as a local
3738+ if @nd_type[nid] == "RescueNode"
3739+ ref = @nd_reference[nid]
3740+ if ref >= 0
3741+ lname = @nd_name[ref]
3742+ if not_in(lname, names) == 1
3743+ if not_in(lname, params) == 1
3744+ names.push(lname)
3745+ types.push("string")
3746+ end
3747+ end
3748+ end
3749+ end
36353750 # Block parameters need to be declared as locals
36363751 if @nd_type[nid] == "CallNode"
36373752 blk = @nd_block[nid]
@@ -3789,6 +3904,12 @@
37893904 if @nd_collection[nid] >= 0
37903905 scan_locals(@nd_collection[nid], names, types, params)
37913906 end
3907+ if @nd_rescue_clause[nid] >= 0
3908+ scan_locals(@nd_rescue_clause[nid], names, types, params)
3909+ end
3910+ if @nd_ensure_clause[nid] >= 0
3911+ scan_locals(@nd_ensure_clause[nid], names, types, params)
3912+ end
37923913 end
37933914
37943915 def not_in(name, arr)
@@ -3832,15 +3953,19 @@
38323953 i = i + 1
38333954 end
38343955
3956+ vol = ""
3957+ if @needs_setjmp == 1
3958+ vol = "volatile "
3959+ end
38353960 j = 0
38363961 while j < lnames.length
38373962 declare_var(lnames[j], ltypes[j])
38383963 ctp = c_type(ltypes[j])
38393964 if type_is_pointer(ltypes[j]) == 1
3840- emit(" " + ctp + "lv_" + lnames[j] + " = NULL;")
3965+ emit(" " + vol + ctp + "lv_" + lnames[j] + " = NULL;")
38413966 emit(" SP_GC_ROOT(lv_" + lnames[j] + ");")
38423967 else
3843- emit(" " + ctp + " lv_" + lnames[j] + " = " + c_default_val(ltypes[j]) + ";")
3968+ emit(" " + vol + ctp + " lv_" + lnames[j] + " = " + c_default_val(ltypes[j]) + ";")
38443969 end
38453970 j = j + 1
38463971 end
@@ -3966,6 +4091,29 @@
39664091 if t == "DefinedNode"
39674092 return "\"expression\""
39684093 end
4094+ if t == "XStringNode"
4095+ @needs_file_io = 1
4096+ return "sp_backtick(" + c_string_literal(@nd_content[nid]) + ")"
4097+ end
4098+ if t == "InterpolatedXStringNode"
4099+ @needs_file_io = 1
4100+ @needs_string_helpers = 1
4101+ interp = compile_interpolated(nid)
4102+ return "sp_backtick(" + interp + ")"
4103+ end
4104+ if t == "GlobalVariableReadNode"
4105+ gname = @nd_name[nid]
4106+ if gname == "$stderr"
4107+ return "0"
4108+ end
4109+ if gname == "$stdout"
4110+ return "0"
4111+ end
4112+ if gname == "$?"
4113+ return "sp_last_status"
4114+ end
4115+ return "0"
4116+ end
39694117 if t == "SourceLineNode"
39704118 return @nd_value[nid].to_s
39714119 end
@@ -4002,7 +4150,40 @@
40024150 while i < s.length
40034151 ch = s[i]
40044152 if ch == "\\"
4005- result = result + "\\\\"
4153+ # Check for Ruby escape sequences
4154+ if i + 1 < s.length
4155+ nch = s[i + 1]
4156+ if nch == "n"
4157+ result = result + "\\n"
4158+ i = i + 2
4159+ else
4160+ if nch == "t"
4161+ result = result + "\\t"
4162+ i = i + 2
4163+ else
4164+ if nch == "r"
4165+ result = result + "\\r"
4166+ i = i + 2
4167+ else
4168+ if nch == "\\"
4169+ result = result + "\\\\"
4170+ i = i + 2
4171+ else
4172+ if nch == "\""
4173+ result = result + "\\\""
4174+ i = i + 2
4175+ else
4176+ result = result + "\\\\"
4177+ i = i + 1
4178+ end
4179+ end
4180+ end
4181+ end
4182+ end
4183+ else
4184+ result = result + "\\\\"
4185+ i = i + 1
4186+ end
40064187 else
40074188 if ch == "\""
40084189 result = result + "\\\""
@@ -4021,8 +4202,8 @@
40214202 end
40224203 end
40234204 end
4205+ i = i + 1
40244206 end
4025- i = i + 1
40264207 end
40274208 result + "\""
40284209 end
@@ -4133,6 +4314,12 @@
41334314
41344315 # No receiver
41354316 if recv < 0
4317+ # catch as expression
4318+ if mname == "catch"
4319+ if @nd_block[nid] >= 0
4320+ return compile_catch_expr(nid)
4321+ end
4322+ end
41364323 if mname == "block_given?"
41374324 if @in_yield_method == 1
41384325 return "(_block != NULL)"
@@ -4457,11 +4644,39 @@
44574644
44584645 # Math
44594646 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 end
44644652 end
4653+ # File operations
4654+ if rcname == "File"
4655+ if mname == "read"
4656+ @needs_file_io = 1
4657+ return "sp_file_read(" + compile_arg0(nid) + ")"
4658+ end
4659+ if mname == "exist?"
4660+ @needs_file_io = 1
4661+ return "sp_file_exist(" + compile_arg0(nid) + ")"
4662+ end
4663+ if mname == "delete"
4664+ @needs_file_io = 1
4665+ return "(sp_file_delete(" + compile_arg0(nid) + "), 0)"
4666+ end
4667+ end
4668+ # ENV
4669+ if rcname == "ENV"
4670+ if mname == "[]"
4671+ return "getenv(" + compile_arg0(nid) + ")"
4672+ end
4673+ end
4674+ # Dir
4675+ if rcname == "Dir"
4676+ if mname == "home"
4677+ return "getenv(\"HOME\")"
4678+ end
4679+ end
44654680 end
44664681
44674682 # to_a on range (including parenthesized range)
@@ -4855,10 +5070,18 @@
48555070 emit(" continue;")
48565071 return
48575072 end
5073+ if t == "RetryNode"
5074+ emit(" continue;")
5075+ return
5076+ end
48585077 if t == "YieldNode"
48595078 compile_yield_stmt(nid)
48605079 return
48615080 end
5081+ if t == "BeginNode"
5082+ compile_begin_stmt(nid)
5083+ return
5084+ end
48625085 if t == "CallNode"
48635086 compile_call_stmt(nid)
48645087 return
@@ -5239,6 +5462,80 @@
52395462 end
52405463 end
52415464
5465+ # raise
5466+ if mname == "raise"
5467+ if recv < 0
5468+ @needs_setjmp = 1
5469+ msg = compile_arg0(nid)
5470+ emit(" sp_raise(" + msg + ");")
5471+ return
5472+ end
5473+ end
5474+
5475+ # system
5476+ if mname == "system"
5477+ if recv < 0
5478+ @needs_file_io = 1
5479+ emit(" sp_last_status = system(" + compile_arg0(nid) + ");")
5480+ return
5481+ end
5482+ end
5483+
5484+ # trap (just ignore)
5485+ if mname == "trap"
5486+ if recv < 0
5487+ return
5488+ end
5489+ end
5490+
5491+ # catch/throw
5492+ if mname == "catch"
5493+ if recv < 0
5494+ if @nd_block[nid] >= 0
5495+ compile_catch_stmt(nid)
5496+ return
5497+ end
5498+ end
5499+ end
5500+ if mname == "throw"
5501+ if recv < 0
5502+ compile_throw_stmt(nid)
5503+ return
5504+ end
5505+ end
5506+
5507+ # File operations
5508+ if recv >= 0
5509+ if @nd_type[recv] == "ConstantReadNode"
5510+ rcname = @nd_name[recv]
5511+ if rcname == "File"
5512+ if mname == "write"
5513+ @needs_file_io = 1
5514+ args_id = @nd_arguments[nid]
5515+ arg_ids = []
5516+ if args_id >= 0
5517+ arg_ids = get_args(args_id)
5518+ end
5519+ a0 = "0"
5520+ a1 = "0"
5521+ if arg_ids.length >= 1
5522+ a0 = compile_expr(arg_ids[0])
5523+ end
5524+ if arg_ids.length >= 2
5525+ a1 = compile_expr(arg_ids[1])
5526+ end
5527+ emit(" sp_file_write(" + a0 + ", " + a1 + ");")
5528+ return
5529+ end
5530+ if mname == "delete"
5531+ @needs_file_io = 1
5532+ emit(" sp_file_delete(" + compile_arg0(nid) + ");")
5533+ return
5534+ end
5535+ end
5536+ end
5537+ end
5538+
52425539 # exit
52435540 if mname == "exit"
52445541 if recv < 0
@@ -5770,6 +6067,186 @@
57706067 end
57716068 end
57726069
6070+ def compile_catch_expr(nid)
6071+ @needs_setjmp = 1
6072+ tag = compile_arg0(nid)
6073+ blk = @nd_block[nid]
6074+ tmp = new_temp
6075+
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 + 1
6081+ if blk >= 0
6082+ body = @nd_body[blk]
6083+ if body >= 0
6084+ stmts = get_stmts(body)
6085+ # Compile all but last as statements
6086+ k = 0
6087+ while k < stmts.length - 1
6088+ compile_stmt(stmts[k])
6089+ k = k + 1
6090+ end
6091+ if stmts.length > 0
6092+ last = stmts[stmts.length - 1]
6093+ emit(" " + tmp + " = " + compile_expr(last) + ";")
6094+ end
6095+ end
6096+ end
6097+ @indent = @indent - 1
6098+ emit(" sp_catch_top--;")
6099+ emit(" } else {")
6100+ emit(" sp_catch_top--;")
6101+ emit(" " + tmp + " = sp_catch_val[sp_catch_top];")
6102+ emit(" }")
6103+ tmp
6104+ end
6105+
6106+ def compile_catch_stmt(nid)
6107+ @needs_setjmp = 1
6108+ # catch(:tag) do ... end
6109+ 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 + 1
6117+ if blk >= 0
6118+ compile_stmts_body(@nd_body[blk])
6119+ end
6120+ @indent = @indent - 1
6121+ emit(" sp_catch_top--;")
6122+ emit(" } else {")
6123+ emit(" sp_catch_top--;")
6124+ emit(" }")
6125+ end
6126+
6127+ def compile_throw_stmt(nid)
6128+ @needs_setjmp = 1
6129+ args_id = @nd_arguments[nid]
6130+ arg_ids = []
6131+ if args_id >= 0
6132+ arg_ids = get_args(args_id)
6133+ end
6134+ tag = "\"\""
6135+ val = "0"
6136+ if arg_ids.length >= 1
6137+ tag = compile_expr(arg_ids[0])
6138+ end
6139+ if arg_ids.length >= 2
6140+ val = compile_expr(arg_ids[1])
6141+ end
6142+ emit(" sp_throw(" + tag + ", " + val + ");")
6143+ end
6144+
6145+ def compile_begin_stmt(nid)
6146+ @needs_setjmp = 1
6147+ has_rescue = @nd_rescue_clause[nid] >= 0
6148+ has_ensure = @nd_ensure_clause[nid] >= 0
6149+
6150+ # Check if rescue body has retry
6151+ has_retry = 0
6152+ rc = @nd_rescue_clause[nid]
6153+ if rc >= 0
6154+ if body_has_retry(@nd_body[rc]) == 1
6155+ has_retry = 1
6156+ end
6157+ end
6158+
6159+ if has_retry == 1
6160+ emit(" for (;;) {")
6161+ @indent = @indent + 1
6162+ end
6163+
6164+ if has_rescue
6165+ emit(" sp_exc_top++;")
6166+ emit(" if (setjmp(sp_exc_stack[sp_exc_top-1]) == 0) {")
6167+ @indent = @indent + 1
6168+ compile_stmts_body(@nd_body[nid])
6169+ @indent = @indent - 1
6170+ emit(" sp_exc_top--;")
6171+ if has_retry == 1
6172+ emit(" break;")
6173+ end
6174+
6175+ rc = @nd_rescue_clause[nid]
6176+ if rc >= 0
6177+ emit(" } else {")
6178+ emit(" sp_exc_top--;")
6179+ @indent = @indent + 1
6180+ ref = @nd_reference[rc]
6181+ if ref >= 0
6182+ rname = @nd_name[ref]
6183+ emit(" lv_" + rname + " = sp_exc_msg[sp_exc_top];")
6184+ end
6185+ compile_rescue_body(@nd_body[rc], has_retry)
6186+ @indent = @indent - 1
6187+ end
6188+ emit(" }")
6189+ else
6190+ compile_stmts_body(@nd_body[nid])
6191+ end
6192+
6193+ if has_retry == 1
6194+ @indent = @indent - 1
6195+ emit(" }")
6196+ end
6197+
6198+ if has_ensure
6199+ ec = @nd_ensure_clause[nid]
6200+ if ec >= 0
6201+ compile_stmts_body(@nd_body[ec])
6202+ end
6203+ end
6204+ end
6205+
6206+ def compile_rescue_body(nid, has_retry)
6207+ if nid < 0
6208+ return
6209+ end
6210+ stmts = get_stmts(nid)
6211+ k = 0
6212+ while k < stmts.length
6213+ compile_stmt(stmts[k])
6214+ k = k + 1
6215+ end
6216+ if has_retry == 1
6217+ # If we get here without a retry (continue), break out of the loop
6218+ emit(" break;")
6219+ end
6220+ end
6221+
6222+ def body_has_retry(nid)
6223+ if nid < 0
6224+ return 0
6225+ end
6226+ if @nd_type[nid] == "RetryNode"
6227+ return 1
6228+ end
6229+ stmts = parse_id_list(@nd_stmts[nid])
6230+ k = 0
6231+ while k < stmts.length
6232+ if body_has_retry(stmts[k]) == 1
6233+ return 1
6234+ end
6235+ k = k + 1
6236+ end
6237+ if @nd_body[nid] >= 0
6238+ if body_has_retry(@nd_body[nid]) == 1
6239+ return 1
6240+ end
6241+ end
6242+ if @nd_subsequent[nid] >= 0
6243+ if body_has_retry(@nd_subsequent[nid]) == 1
6244+ return 1
6245+ end
6246+ end
6247+ 0
6248+ end
6249+
57736250 def compile_yield_stmt(nid)
57746251 args_id = @nd_arguments[nid]
57756252 arg_exprs = ""
@@ -6518,6 +6995,13 @@
65186995 end
65196996 return
65206997 end
6998+ if @nd_type[last] == "BeginNode"
6999+ compile_begin_stmt(last)
7000+ if return_type != "void"
7001+ emit(" return " + c_default_val(return_type) + ";")
7002+ end
7003+ return
7004+ end
65217005 # If last statement is a CallNode with a block, handle map/select as expressions
65227006 if @nd_type[last] == "CallNode"
65237007 if @nd_block[last] >= 0