From 8631927dbea7531a7062b5359c1e11714e88d9d5 Mon Sep 17 00:00:00 2001 From: "K.Kosako" Date: Wed, 28 Jun 2017 11:23:33 +0900 Subject: [PATCH] define compile_enclosure_memory_node() --- src/regcomp.c | 113 ++++++++++++++++++++++++++++---------------------- 1 file changed, 63 insertions(+), 50 deletions(-) diff --git a/src/regcomp.c b/src/regcomp.c index 1560a30..3808999 100644 --- a/src/regcomp.c +++ b/src/regcomp.c @@ -1350,66 +1350,79 @@ compile_length_enclosure_node(EnclosureNode* node, regex_t* reg) static int get_char_length_tree(Node* node, regex_t* reg, int* len); static int -compile_enclosure_node(EnclosureNode* node, regex_t* reg, ScanEnv* env) +compile_enclosure_memory_node(EnclosureNode* node, regex_t* reg, ScanEnv* env) { - int r, len; - - if (node->type == ENCLOSURE_OPTION) - return compile_option_node(node, reg, env); + int r; + int len; - switch (node->type) { - case ENCLOSURE_MEMORY: #ifdef USE_SUBEXP_CALL - if (NODE_IS_CALLED(node)) { - r = add_opcode(reg, OP_CALL); - if (r != 0) return r; - node->m.call_addr = BBUF_GET_OFFSET_POS(reg) + SIZE_ABSADDR + SIZE_OP_JUMP; - NODE_STATUS_ADD(node, NST_ADDR_FIXED); - r = add_abs_addr(reg, (int )node->m.call_addr); - if (r != 0) return r; - len = compile_length_tree(NODE_ENCLOSURE_BODY(node), reg); - len += (SIZE_OP_MEMORY_START_PUSH + SIZE_OP_RETURN); - if (BIT_STATUS_AT(reg->bt_mem_end, node->m.regnum)) - len += (NODE_IS_RECURSION(node) - ? SIZE_OP_MEMORY_END_PUSH_REC : SIZE_OP_MEMORY_END_PUSH); - else - len += (NODE_IS_RECURSION(node) - ? SIZE_OP_MEMORY_END_REC : SIZE_OP_MEMORY_END); - - r = add_opcode_rel_addr(reg, OP_JUMP, len); - if (r != 0) return r; - } -#endif - if (BIT_STATUS_AT(reg->bt_mem_start, node->m.regnum)) - r = add_opcode(reg, OP_MEMORY_START_PUSH); - else - r = add_opcode(reg, OP_MEMORY_START); - if (r != 0) return r; - r = add_mem_num(reg, node->m.regnum); + if (NODE_IS_CALLED(node)) { + r = add_opcode(reg, OP_CALL); if (r != 0) return r; - r = compile_tree(NODE_ENCLOSURE_BODY(node), reg, env); + node->m.call_addr = BBUF_GET_OFFSET_POS(reg) + SIZE_ABSADDR + SIZE_OP_JUMP; + NODE_STATUS_ADD(node, NST_ADDR_FIXED); + r = add_abs_addr(reg, (int )node->m.call_addr); if (r != 0) return r; -#ifdef USE_SUBEXP_CALL + len = compile_length_tree(NODE_ENCLOSURE_BODY(node), reg); + len += (SIZE_OP_MEMORY_START_PUSH + SIZE_OP_RETURN); if (BIT_STATUS_AT(reg->bt_mem_end, node->m.regnum)) - r = add_opcode(reg, (NODE_IS_RECURSION(node) - ? OP_MEMORY_END_PUSH_REC : OP_MEMORY_END_PUSH)); + len += (NODE_IS_RECURSION(node) + ? SIZE_OP_MEMORY_END_PUSH_REC : SIZE_OP_MEMORY_END_PUSH); else - r = add_opcode(reg, (NODE_IS_RECURSION(node) - ? OP_MEMORY_END_REC : OP_MEMORY_END)); + len += (NODE_IS_RECURSION(node) + ? SIZE_OP_MEMORY_END_REC : SIZE_OP_MEMORY_END); + + r = add_opcode_rel_addr(reg, OP_JUMP, len); if (r != 0) return r; - r = add_mem_num(reg, node->m.regnum); - if (NODE_IS_CALLED(node)) { - if (r != 0) return r; - r = add_opcode(reg, OP_RETURN); - } -#else - if (BIT_STATUS_AT(reg->bt_mem_end, node->m.regnum)) - r = add_opcode(reg, OP_MEMORY_END_PUSH); - else - r = add_opcode(reg, OP_MEMORY_END); + } +#endif + + if (BIT_STATUS_AT(reg->bt_mem_start, node->m.regnum)) + r = add_opcode(reg, OP_MEMORY_START_PUSH); + else + r = add_opcode(reg, OP_MEMORY_START); + if (r != 0) return r; + r = add_mem_num(reg, node->m.regnum); + if (r != 0) return r; + r = compile_tree(NODE_ENCLOSURE_BODY(node), reg, env); + if (r != 0) return r; + +#ifdef USE_SUBEXP_CALL + if (BIT_STATUS_AT(reg->bt_mem_end, node->m.regnum)) + r = add_opcode(reg, (NODE_IS_RECURSION(node) + ? OP_MEMORY_END_PUSH_REC : OP_MEMORY_END_PUSH)); + else + r = add_opcode(reg, (NODE_IS_RECURSION(node) + ? OP_MEMORY_END_REC : OP_MEMORY_END)); + if (r != 0) return r; + r = add_mem_num(reg, node->m.regnum); + if (NODE_IS_CALLED(node)) { if (r != 0) return r; - r = add_mem_num(reg, node->m.regnum); + r = add_opcode(reg, OP_RETURN); + } +#else + if (BIT_STATUS_AT(reg->bt_mem_end, node->m.regnum)) + r = add_opcode(reg, OP_MEMORY_END_PUSH); + else + r = add_opcode(reg, OP_MEMORY_END); + if (r != 0) return r; + r = add_mem_num(reg, node->m.regnum); #endif + + return r; +} + +static int +compile_enclosure_node(EnclosureNode* node, regex_t* reg, ScanEnv* env) +{ + int r, len; + + if (node->type == ENCLOSURE_OPTION) + return compile_option_node(node, reg, env); + + switch (node->type) { + case ENCLOSURE_MEMORY: + r = compile_enclosure_memory_node(node, reg, env); break; case ENCLOSURE_STOP_BACKTRACK: -- 2.40.0