[njs] Setting exit index for try_return instruction is simplified.

Dmitry Volyntsev xeioex at nginx.com
Thu Jan 17 14:03:23 UTC 2019


details:   https://hg.nginx.org/njs/rev/3b1239c2ee12
branches:  
changeset: 732:3b1239c2ee12
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Thu Jan 17 17:01:22 2019 +0300
description:
Setting exit index for try_return instruction is simplified.

diffstat:

 njs/njs_generator.c |  57 ++++++++++++----------------------------------------
 1 files changed, 13 insertions(+), 44 deletions(-)

diffs (175 lines):

diff -r 2e9bdb42b3e6 -r 3b1239c2ee12 njs/njs_generator.c
--- a/njs/njs_generator.c	Thu Jan 17 16:12:46 2019 +0300
+++ b/njs/njs_generator.c	Thu Jan 17 17:01:22 2019 +0300
@@ -20,11 +20,6 @@ struct njs_generator_patch_s {
      */
     njs_ret_t                       jump_offset;
     njs_generator_patch_t           *next;
-    /*
-     * index_offset is used for patching vmcode_try_return instruction
-     * inside try blocks.
-     */
-    njs_index_t                     index_offset;
 };
 
 
@@ -47,6 +42,8 @@ struct njs_generator_block_s {
     njs_generator_patch_t           *continuation;
     njs_generator_patch_t           *exit;
     njs_generator_block_t           *next;
+
+    njs_index_t                     index;
 };
 
 
@@ -87,8 +84,6 @@ static nxt_int_t njs_generate_make_conti
     njs_generator_t *generator, njs_generator_block_t *block, njs_ret_t offset);
 static nxt_noinline void njs_generate_patch_block(njs_vm_t *vm,
     njs_generator_t *generator, njs_generator_patch_t *list);
-static nxt_noinline void njs_generate_patch_try_exit_block(njs_vm_t *vm,
-    njs_generator_t *generator, njs_generator_patch_t *list, njs_index_t dest);
 static nxt_int_t njs_generate_make_exit_patch(njs_vm_t *vm,
     njs_generator_t *generator, njs_generator_block_t *block, njs_ret_t offset);
 static nxt_noinline void njs_generate_patch_block_exit(njs_vm_t *vm,
@@ -891,7 +886,6 @@ njs_generate_switch_statement(njs_vm_t *
                 return NXT_ERROR;
             }
 
-            patch->index_offset = 0;
             patch->jump_offset = njs_code_offset(generator, equal)
                                  + offsetof(njs_vmcode_equal_jump_t, offset);
 
@@ -1277,6 +1271,8 @@ njs_generate_start_block(njs_vm_t *vm, n
         block->continuation = NULL;
         block->exit = NULL;
 
+        block->index = 0;
+
         return NXT_OK;
     }
 
@@ -1315,7 +1311,6 @@ njs_generate_make_continuation_patch(njs
     patch->next = block->continuation;
     block->continuation = patch;
 
-    patch->index_offset = 0;
     patch->jump_offset = offset;
 
     return NXT_OK;
@@ -1337,25 +1332,6 @@ njs_generate_patch_block(njs_vm_t *vm, n
 }
 
 
-static nxt_noinline void
-njs_generate_patch_try_exit_block(njs_vm_t *vm, njs_generator_t *generator,
-    njs_generator_patch_t *list, njs_index_t dest)
-{
-    njs_generator_patch_t  *patch, *next;
-
-    for (patch = list; patch != NULL; patch = next) {
-        njs_code_update_offset(generator, patch);
-        next = patch->next;
-
-        if (patch->index_offset != 0) {
-            *(njs_code_ptr(generator, njs_index_t, patch->index_offset)) = dest;
-        }
-
-        nxt_mem_cache_free(vm->mem_cache_pool, patch);
-    }
-}
-
-
 static nxt_int_t
 njs_generate_make_exit_patch(njs_vm_t *vm, njs_generator_t *generator,
     njs_generator_block_t *block, njs_ret_t offset)
@@ -1372,7 +1348,6 @@ njs_generate_make_exit_patch(njs_vm_t *v
     patch->next = block->exit;
     block->exit = patch;
 
-    patch->index_offset = 0;
     patch->jump_offset = offset;
 
     return NXT_OK;
@@ -1429,7 +1404,6 @@ njs_generate_continue_statement(njs_vm_t
         jump->code.retval = NJS_VMCODE_NO_RETVAL;
         jump->offset = offsetof(njs_vmcode_jump_t, offset);
 
-        patch->index_offset = 0;
         patch->jump_offset = njs_code_offset(generator, jump)
                              + offsetof(njs_vmcode_jump_t, offset);
     }
@@ -1480,7 +1454,6 @@ njs_generate_break_statement(njs_vm_t *v
         jump->code.retval = NJS_VMCODE_NO_RETVAL;
         jump->offset = offsetof(njs_vmcode_jump_t, offset);
 
-        patch->index_offset = 0;
         patch->jump_offset = njs_code_offset(generator, jump)
                              + offsetof(njs_vmcode_jump_t, offset);
     }
@@ -2533,10 +2506,7 @@ njs_generate_return_statement(njs_vm_t *
     try_return->code.operands = NJS_VMCODE_2OPERANDS;
     try_return->code.retval = NJS_VMCODE_RETVAL;
     try_return->retval = index;
-
-    try_return->save = index;
-    patch->index_offset = njs_code_offset(generator, try_return)
-                          + offsetof(njs_vmcode_try_return_t, save);
+    try_return->save = block->index;
 
     try_return->offset = offsetof(njs_vmcode_try_return_t, offset);
     patch->jump_offset = njs_code_offset(generator, try_return)
@@ -2739,13 +2709,14 @@ njs_generate_try_statement(njs_vm_t *vm,
         return ret;
     }
 
+    try_block = generator->block;
+    try_block->index = exit_index;
+
     ret = njs_generator(vm, generator, node->left);
     if (nxt_slow_path(ret != NXT_OK)) {
         return ret;
     }
 
-    try_block = generator->block;
-
     njs_generate_code(generator, njs_vmcode_try_end_t, try_end);
     try_end_offset = njs_code_offset(generator, try_end);
     try_end->code.operation = njs_vmcode_try_end;
@@ -2753,8 +2724,7 @@ njs_generate_try_statement(njs_vm_t *vm,
     try_end->code.retval = NJS_VMCODE_NO_RETVAL;
 
     if (try_block->exit != NULL) {
-        njs_generate_patch_try_exit_block(vm, generator, try_block->exit,
-                                          exit_index);
+        njs_generate_patch_block(vm, generator, try_block->exit);
 
         njs_generate_code(generator, njs_vmcode_try_trampoline_t, try_break);
         try_break->code.operation = njs_vmcode_try_break;
@@ -2876,13 +2846,14 @@ njs_generate_try_statement(njs_vm_t *vm,
                 return ret;
             }
 
+            catch_block = generator->block;
+            catch_block->index = exit_index;
+
             ret = njs_generator(vm, generator, node->left->right);
             if (nxt_slow_path(ret != NXT_OK)) {
                 return ret;
             }
 
-            catch_block = generator->block;
-
             njs_generate_code(generator, njs_vmcode_try_end_t, catch_end);
             catch_end_offset = njs_code_offset(generator, catch_end);
             catch_end->code.operation = njs_vmcode_try_end;
@@ -2890,9 +2861,7 @@ njs_generate_try_statement(njs_vm_t *vm,
             catch_end->code.retval = NJS_VMCODE_NO_RETVAL;
 
             if (catch_block->exit != NULL) {
-                njs_generate_patch_try_exit_block(vm, generator,
-                                                  catch_block->exit,
-                                                  exit_index);
+                njs_generate_patch_block(vm, generator, catch_block->exit);
 
                 njs_generate_code(generator, njs_vmcode_try_trampoline_t,
                                   try_break);


More information about the nginx-devel mailing list