[PRISM] Remove more dummy line usage

* or
* optional parameter
* parentheses
* pre execution
* post execution
* range
* rational
* redo
This commit is contained in:
Kevin Newton 2024-02-20 11:47:56 -05:00
parent e19fde52cc
commit 5c02d97780

View File

@ -6486,61 +6486,66 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
return; return;
} }
case PM_OR_NODE: { case PM_OR_NODE: {
pm_or_node_t *or_node = (pm_or_node_t *) node; // a or b
// ^^^^^^
const pm_or_node_t *cast = (const pm_or_node_t *) node;
LABEL *end_label = NEW_LABEL(lineno); LABEL *end_label = NEW_LABEL(location.line);
PM_COMPILE_NOT_POPPED(or_node->left); PM_COMPILE_NOT_POPPED(cast->left);
PM_DUP_UNLESS_POPPED; if (!popped) PUSH_INSN(ret, location, dup);
ADD_INSNL(ret, &dummy_line_node, branchif, end_label); PUSH_INSNL(ret, location, branchif, end_label);
PM_POP_UNLESS_POPPED; if (!popped) PUSH_INSN(ret, location, pop);
PM_COMPILE(or_node->right); PM_COMPILE(cast->right);
ADD_LABEL(ret, end_label); PUSH_LABEL(ret, end_label);
return; return;
} }
case PM_OPTIONAL_PARAMETER_NODE: { case PM_OPTIONAL_PARAMETER_NODE: {
pm_optional_parameter_node_t *optional_parameter_node = (pm_optional_parameter_node_t *)node; // def foo(bar = 1); end
PM_COMPILE_NOT_POPPED(optional_parameter_node->value); // ^^^^^^^
const pm_optional_parameter_node_t *cast = (const pm_optional_parameter_node_t *) node;
pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, optional_parameter_node->name, 0); PM_COMPILE_NOT_POPPED(cast->value);
pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, cast->name, 0);
ADD_SETLOCAL(ret, &dummy_line_node, index.index, index.level); ADD_SETLOCAL(ret, &dummy_line_node, index.index, index.level);
return; return;
} }
case PM_PARAMETERS_NODE: {
rb_bug("Cannot compile a ParametersNode directly\n");
return;
}
case PM_PARENTHESES_NODE: { case PM_PARENTHESES_NODE: {
pm_parentheses_node_t *parentheses_node = (pm_parentheses_node_t *) node; // ()
// ^^
//
// (1)
// ^^^
const pm_parentheses_node_t *cast = (const pm_parentheses_node_t *) node;
if (parentheses_node->body == NULL) { if (cast->body != NULL) {
PM_PUTNIL_UNLESS_POPPED; PM_COMPILE(cast->body);
} else { } else if (!popped) {
PM_COMPILE(parentheses_node->body); PUSH_INSN(ret, location, putnil);
} }
return; return;
} }
case PM_PRE_EXECUTION_NODE: { case PM_PRE_EXECUTION_NODE: {
pm_pre_execution_node_t *pre_execution_node = (pm_pre_execution_node_t *) node; // BEGIN {}
// ^^^^^^^^
const pm_pre_execution_node_t *cast = (const pm_pre_execution_node_t *) node;
DECL_ANCHOR(pre_ex); DECL_ANCHOR(pre_ex);
INIT_ANCHOR(pre_ex); INIT_ANCHOR(pre_ex);
if (pre_execution_node->statements) { if (cast->statements != NULL) {
pm_node_list_t node_list = pre_execution_node->statements->body; const pm_node_list_t *body = &cast->statements->body;
for (size_t index = 0; index < node_list.size; index++) { for (size_t index = 0; index < body->size; index++) {
pm_compile_node(iseq, node_list.nodes[index], pre_ex, true, scope_node); pm_compile_node(iseq, body->nodes[index], pre_ex, true, scope_node);
} }
} }
if (!popped) { if (!popped) {
ADD_INSN(pre_ex, &dummy_line_node, putnil); PUSH_INSN(pre_ex, location, putnil);
} }
pre_ex->last->next = ret->anchor.next; pre_ex->last->next = ret->anchor.next;
@ -6554,6 +6559,8 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
return; return;
} }
case PM_POST_EXECUTION_NODE: { case PM_POST_EXECUTION_NODE: {
// END {}
// ^^^^^^
const rb_iseq_t *child_iseq; const rb_iseq_t *child_iseq;
const rb_iseq_t *prevblock = ISEQ_COMPILE_DATA(iseq)->current_block; const rb_iseq_t *prevblock = ISEQ_COMPILE_DATA(iseq)->current_block;
@ -6565,119 +6572,118 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq; ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq;
int is_index = ISEQ_BODY(iseq)->ise_size++; int is_index = ISEQ_BODY(iseq)->ise_size++;
PUSH_INSN2(ret, location, once, child_iseq, INT2FIX(is_index));
ADD_INSN2(ret, &dummy_line_node, once, child_iseq, INT2FIX(is_index)); RB_OBJ_WRITTEN(iseq, Qundef, (VALUE) child_iseq);
RB_OBJ_WRITTEN(iseq, Qundef, (VALUE)child_iseq); if (popped) PUSH_INSN(ret, location, pop);
PM_POP_IF_POPPED;
ISEQ_COMPILE_DATA(iseq)->current_block = prevblock; ISEQ_COMPILE_DATA(iseq)->current_block = prevblock;
return; return;
} }
case PM_PROGRAM_NODE: {
rb_bug("Cannot compile a ProgramNode directly\n");
return;
}
case PM_RANGE_NODE: { case PM_RANGE_NODE: {
pm_range_node_t *range_node = (pm_range_node_t *) node; // 0..5
bool exclusive = (range_node->operator_loc.end - range_node->operator_loc.start) == 3; // ^^^^
const pm_range_node_t *cast = (const pm_range_node_t *) node;
bool exclude_end = PM_NODE_FLAG_P(cast, PM_RANGE_FLAGS_EXCLUDE_END);
if (pm_optimizable_range_item_p(range_node->left) && pm_optimizable_range_item_p(range_node->right)) { if (pm_optimizable_range_item_p(cast->left) && pm_optimizable_range_item_p(cast->right)) {
if (!popped) { if (!popped) {
pm_node_t *left = range_node->left; const pm_node_t *left = cast->left;
pm_node_t *right = range_node->right; const pm_node_t *right = cast->right;
VALUE val = rb_range_new( VALUE val = rb_range_new(
left && PM_NODE_TYPE_P(left, PM_INTEGER_NODE) ? parse_integer((pm_integer_node_t *) left) : Qnil, (left && PM_NODE_TYPE_P(left, PM_INTEGER_NODE)) ? parse_integer((const pm_integer_node_t *) left) : Qnil,
right && PM_NODE_TYPE_P(right, PM_INTEGER_NODE) ? parse_integer((pm_integer_node_t *) right) : Qnil, (right && PM_NODE_TYPE_P(right, PM_INTEGER_NODE)) ? parse_integer((const pm_integer_node_t *) right) : Qnil,
exclusive exclude_end
); );
ADD_INSN1(ret, &dummy_line_node, putobject, val);
RB_OBJ_WRITTEN(iseq, Qundef, val); PUSH_INSN1(ret, location, putobject, val);
} }
} }
else { else {
if (range_node->left == NULL) { if (cast->left == NULL) {
PM_PUTNIL; PUSH_INSN(ret, location, putnil);
} else { } else {
PM_COMPILE(range_node->left); PM_COMPILE(cast->left);
} }
if (range_node->right == NULL) { if (cast->right == NULL) {
PM_PUTNIL; PUSH_INSN(ret, location, putnil);
} else { } else {
PM_COMPILE(range_node->right); PM_COMPILE(cast->right);
} }
if (!popped) { if (!popped) {
ADD_INSN1(ret, &dummy_line_node, newrange, INT2FIX(exclusive)); PUSH_INSN1(ret, location, newrange, INT2FIX(exclude_end ? 1 : 0));
} }
} }
return; return;
} }
case PM_RATIONAL_NODE: { case PM_RATIONAL_NODE: {
// 1r
// ^^
if (!popped) { if (!popped) {
ADD_INSN1(ret, &dummy_line_node, putobject, parse_rational(node)); PUSH_INSN1(ret, location, putobject, parse_rational(node));
} }
return; return;
} }
case PM_REDO_NODE: { case PM_REDO_NODE: {
// redo
// ^^^^
if (ISEQ_COMPILE_DATA(iseq)->redo_label && can_add_ensure_iseq(iseq)) { if (ISEQ_COMPILE_DATA(iseq)->redo_label && can_add_ensure_iseq(iseq)) {
LABEL *splabel = NEW_LABEL(0); LABEL *splabel = NEW_LABEL(0);
ADD_LABEL(ret, splabel); PUSH_LABEL(ret, splabel);
PUSH_ADJUST(ret, location, ISEQ_COMPILE_DATA(iseq)->redo_label);
ADD_ADJUST(ret, &dummy_line_node, ISEQ_COMPILE_DATA(iseq)->redo_label);
pm_add_ensure_iseq(ret, iseq, 0, scope_node); pm_add_ensure_iseq(ret, iseq, 0, scope_node);
ADD_INSNL(ret, &dummy_line_node, jump, ISEQ_COMPILE_DATA(iseq)->redo_label);
ADD_ADJUST_RESTORE(ret, splabel); PUSH_INSNL(ret, location, jump, ISEQ_COMPILE_DATA(iseq)->redo_label);
PM_PUTNIL_UNLESS_POPPED; PUSH_ADJUST_RESTORE(ret, splabel);
if (!popped) PUSH_INSN(ret, location, putnil);
} }
else if (ISEQ_BODY(iseq)->type != ISEQ_TYPE_EVAL && ISEQ_COMPILE_DATA(iseq)->start_label && can_add_ensure_iseq(iseq)) { else if (ISEQ_BODY(iseq)->type != ISEQ_TYPE_EVAL && ISEQ_COMPILE_DATA(iseq)->start_label && can_add_ensure_iseq(iseq)) {
LABEL *splabel = NEW_LABEL(0); LABEL *splabel = NEW_LABEL(0);
ADD_LABEL(ret, splabel); PUSH_LABEL(ret, splabel);
pm_add_ensure_iseq(ret, iseq, 0, scope_node); pm_add_ensure_iseq(ret, iseq, 0, scope_node);
ADD_ADJUST(ret, &dummy_line_node, ISEQ_COMPILE_DATA(iseq)->start_label); PUSH_ADJUST(ret, location, ISEQ_COMPILE_DATA(iseq)->start_label);
ADD_INSNL(ret, &dummy_line_node, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
ADD_ADJUST_RESTORE(ret, splabel);
PM_PUTNIL_UNLESS_POPPED; PUSH_INSNL(ret, location, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
PUSH_ADJUST_RESTORE(ret, splabel);
if (!popped) PUSH_INSN(ret, location, putnil);
} }
else { else {
const rb_iseq_t *ip = iseq; const rb_iseq_t *ip = iseq;
while (ip) { while (ip) {
if (!ISEQ_COMPILE_DATA(ip)) { if (!ISEQ_COMPILE_DATA(ip)) {
ip = 0; ip = 0;
break; break;
} }
if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) { if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
break; break;
} }
else if (ISEQ_BODY(ip)->type == ISEQ_TYPE_BLOCK) { else if (ISEQ_BODY(ip)->type == ISEQ_TYPE_BLOCK) {
break; break;
} }
else if (ISEQ_BODY(ip)->type == ISEQ_TYPE_EVAL) { else if (ISEQ_BODY(ip)->type == ISEQ_TYPE_EVAL) {
COMPILE_ERROR(ERROR_ARGS "Can't escape from eval with redo"); COMPILE_ERROR(ERROR_ARGS "Can't escape from eval with redo");
return; return;
} }
ip = ISEQ_BODY(ip)->parent_iseq; ip = ISEQ_BODY(ip)->parent_iseq;
} }
if (ip != 0) {
PM_PUTNIL;
ADD_INSN1(ret, &dummy_line_node, throw, INT2FIX(VM_THROW_NO_ESCAPE_FLAG | TAG_REDO));
PM_POP_IF_POPPED; if (ip != 0) {
} PUSH_INSN(ret, location, putnil);
else { PUSH_INSN1(ret, location, throw, INT2FIX(VM_THROW_NO_ESCAPE_FLAG | TAG_REDO));
COMPILE_ERROR(ERROR_ARGS "Invalid redo"); if (popped) PUSH_INSN(ret, location, pop);
return; }
} else {
COMPILE_ERROR(ERROR_ARGS "Invalid redo");
return;
}
} }
return; return;
} }