aboutsummaryrefslogtreecommitdiffstats
path: root/compile.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-04-11 08:43:06 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-04-11 08:43:06 +0000
commitf0226f0a3c836e9262c5cd2671860140878d6b75 (patch)
tree80f7c0c865eeb38453c781d4d2ecbf8505a4d83e /compile.c
parent0ebf3849e4dee70b65dec91997400a235411dc15 (diff)
downloadruby-f0226f0a3c836e9262c5cd2671860140878d6b75.tar.gz
compile.c: disable tco with rescue
* compile.c (iseq_optimize): disable tail call optimization in rescued, rescue, and ensure blocks. [ruby-core:73871] [Bug #12082] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54542 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'compile.c')
-rw-r--r--compile.c47
1 files changed, 45 insertions, 2 deletions
diff --git a/compile.c b/compile.c
index 860de31f9f..cd9426c230 100644
--- a/compile.c
+++ b/compile.c
@@ -47,6 +47,13 @@ typedef struct iseq_link_anchor {
LINK_ELEMENT *last;
} LINK_ANCHOR;
+typedef enum {
+ LABEL_RESCUE_NONE,
+ LABEL_RESCUE_BEG,
+ LABEL_RESCUE_END,
+ LABEL_RESCUE_TYPE_MAX
+} LABEL_RESCUE_TYPE;
+
typedef struct iseq_label_data {
LINK_ELEMENT link;
int label_no;
@@ -55,6 +62,7 @@ typedef struct iseq_label_data {
int sp;
int refcnt;
unsigned int set: 1;
+ unsigned int rescued: 2;
} LABEL;
typedef struct iseq_insn_data {
@@ -561,6 +569,9 @@ rb_iseq_compile_node(rb_iseq_t *iseq, NODE *node)
LABEL *start = ISEQ_COMPILE_DATA(iseq)->start_label = NEW_LABEL(0);
LABEL *end = ISEQ_COMPILE_DATA(iseq)->end_label = NEW_LABEL(0);
+ start->rescued = LABEL_RESCUE_BEG;
+ end->rescued = LABEL_RESCUE_END;
+
ADD_TRACE(ret, FIX2INT(iseq->body->location.first_lineno), RUBY_EVENT_B_CALL);
ADD_LABEL(ret, start);
COMPILE(ret, "block body", node->nd_body);
@@ -2357,20 +2368,37 @@ iseq_specialized_instruction(rb_iseq_t *iseq, INSN *iobj)
return COMPILE_OK;
}
+static inline int
+tailcallable_p(rb_iseq_t *iseq)
+{
+ switch (iseq->body->type) {
+ case ISEQ_TYPE_RESCUE:
+ case ISEQ_TYPE_ENSURE:
+ /* rescue block can't tail call because of errinfo */
+ return FALSE;
+ default:
+ return TRUE;
+ }
+}
+
static int
iseq_optimize(rb_iseq_t *iseq, LINK_ANCHOR *anchor)
{
LINK_ELEMENT *list;
const int do_peepholeopt = ISEQ_COMPILE_DATA(iseq)->option->peephole_optimization;
- const int do_tailcallopt = ISEQ_COMPILE_DATA(iseq)->option->tailcall_optimization;
+ const int do_tailcallopt = tailcallable_p(iseq) &&
+ ISEQ_COMPILE_DATA(iseq)->option->tailcall_optimization;
const int do_si = ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction;
const int do_ou = ISEQ_COMPILE_DATA(iseq)->option->operands_unification;
+ int rescue_level = 0;
+ int tailcallopt = do_tailcallopt;
+
list = FIRST_ELEMENT(anchor);
while (list) {
if (list->type == ISEQ_ELEMENT_INSN) {
if (do_peepholeopt) {
- iseq_peephole_optimize(iseq, list, do_tailcallopt);
+ iseq_peephole_optimize(iseq, list, tailcallopt);
}
if (do_si) {
iseq_specialized_instruction(iseq, (INSN *)list);
@@ -2379,6 +2407,17 @@ iseq_optimize(rb_iseq_t *iseq, LINK_ANCHOR *anchor)
insn_operands_unification((INSN *)list);
}
}
+ if (list->type == ISEQ_ELEMENT_LABEL) {
+ switch (((LABEL *)list)->rescued) {
+ case LABEL_RESCUE_BEG:
+ rescue_level++;
+ tailcallopt = FALSE;
+ break;
+ case LABEL_RESCUE_END:
+ if (!--rescue_level) tailcallopt = do_tailcallopt;
+ break;
+ }
+ }
list = list->next;
}
return COMPILE_OK;
@@ -3483,6 +3522,8 @@ defined_expr(rb_iseq_t *iseq, LINK_ANCHOR *ret,
("defined guard in "),
iseq->body->location.label),
ISEQ_TYPE_DEFINED_GUARD, 0);
+ lstart->rescued = LABEL_RESCUE_BEG;
+ lend->rescued = LABEL_RESCUE_END;
APPEND_LABEL(ret, lcur, lstart);
ADD_LABEL(ret, lend);
ADD_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue, lfinish[1]);
@@ -4339,6 +4380,8 @@ iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node, int poped)
rb_str_concat(rb_str_new2("rescue in "), iseq->body->location.label),
ISEQ_TYPE_RESCUE, line);
+ lstart->rescued = LABEL_RESCUE_BEG;
+ lend->rescued = LABEL_RESCUE_END;
ADD_LABEL(ret, lstart);
COMPILE(ret, "rescue head", node->nd_head);
ADD_LABEL(ret, lend);