[Pcre-svn] [1722] code/trunk: JIT compielr update.

Top Page
Delete this message
Author: Subversion repository
Date:  
To: pcre-svn
Subject: [Pcre-svn] [1722] code/trunk: JIT compielr update.
Revision: 1722
          http://vcs.pcre.org/viewvc?view=rev&revision=1722
Author:   zherczeg
Date:     2018-01-10 09:06:30 +0000 (Wed, 10 Jan 2018)
Log Message:
-----------
JIT compielr update.


Modified Paths:
--------------
    code/trunk/pcre_jit_compile.c
    code/trunk/sljit/sljitLir.h
    code/trunk/sljit/sljitNativeX86_64.c
    code/trunk/sljit/sljitUtils.c


Modified: code/trunk/pcre_jit_compile.c
===================================================================
--- code/trunk/pcre_jit_compile.c    2018-01-08 11:44:57 UTC (rev 1721)
+++ code/trunk/pcre_jit_compile.c    2018-01-10 09:06:30 UTC (rev 1722)
@@ -2473,7 +2473,7 @@
   OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, SLJIT_IMM, 0);
 OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(STACK_TOP), SLJIT_OFFSETOF(jit_arguments, stack));
 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->start_ptr);
-OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(STACK_TOP), SLJIT_OFFSETOF(struct sljit_stack, base));
+OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(STACK_TOP), SLJIT_OFFSETOF(struct sljit_stack, end));
 }


static sljit_sw SLJIT_FUNC do_search_mark(sljit_sw *current, const pcre_uchar *skip_arg)
@@ -2497,7 +2497,7 @@
SLJIT_ASSERT(current[0] == 0 || current < (sljit_sw*)current[0]);
current = (sljit_sw*)current[0];
}
-return -1;
+return 0;
}

static SLJIT_INLINE void copy_ovector(compiler_common *common, int topbracket)
@@ -6954,18 +6954,22 @@
OP1(SLJIT_MOV, SLJIT_R3, 0, STR_END, 0);
sljit_emit_icall(compiler, SLJIT_CALL, SLJIT_RET(SW) | SLJIT_ARG1(SW) | SLJIT_ARG2(SW) | SLJIT_ARG3(SW) | SLJIT_ARG4(SW), SLJIT_IMM, SLJIT_FUNC_OFFSET(do_utf_caselesscmp));
OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), LOCALS0);
+ OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_RETURN_REG, 0);

   if (common->mode == JIT_COMPILE)
     add_jump(compiler, backtracks, CMP(SLJIT_LESS_EQUAL, SLJIT_RETURN_REG, 0, SLJIT_IMM, 1));
   else
     {
-    add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, SLJIT_RETURN_REG, 0, SLJIT_IMM, 0));
-    nopartial = CMP(SLJIT_NOT_EQUAL, SLJIT_RETURN_REG, 0, SLJIT_IMM, 1);
+    OP2(SLJIT_SUB | SLJIT_SET_Z | SLJIT_SET_LESS, SLJIT_UNUSED, 0, SLJIT_RETURN_REG, 0, SLJIT_IMM, 1);
+
+    add_jump(compiler, backtracks, JUMP(SLJIT_LESS));
+
+    nopartial = JUMP(SLJIT_NOT_EQUAL);
+    OP1(SLJIT_MOV, STR_PTR, 0, STR_END, 0);
     check_partial(common, FALSE);
     add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));
     JUMPHERE(nopartial);
     }
-  OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_RETURN_REG, 0);
   }
 else
 #endif /* SUPPORT_UTF && SUPPORT_UCP */
@@ -7372,17 +7376,16 @@
 OP1(SLJIT_MOV, SLJIT_R1, 0, STACK_TOP, 0);
 GET_LOCAL_BASE(SLJIT_R2, 0, OVECTOR_START);
 sljit_emit_icall(compiler, SLJIT_CALL, SLJIT_RET(S32) | SLJIT_ARG1(SW) | SLJIT_ARG2(SW) | SLJIT_ARG3(SW), SLJIT_IMM, SLJIT_FUNC_OFFSET(do_callout));
-OP1(SLJIT_MOV_S32, SLJIT_RETURN_REG, 0, SLJIT_RETURN_REG, 0);
 OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), LOCALS0);
 free_stack(common, CALLOUT_ARG_SIZE / sizeof(sljit_sw));


/* Check return value. */
-OP2(SLJIT_SUB | SLJIT_SET_Z | SLJIT_SET_SIG_GREATER, SLJIT_UNUSED, 0, SLJIT_RETURN_REG, 0, SLJIT_IMM, 0);
-add_jump(compiler, &backtrack->topbacktracks, JUMP(SLJIT_SIG_GREATER));
+OP2(SLJIT_SUB32 | SLJIT_SET_Z | SLJIT_SET_SIG_GREATER, SLJIT_UNUSED, 0, SLJIT_RETURN_REG, 0, SLJIT_IMM, 0);
+add_jump(compiler, &backtrack->topbacktracks, JUMP(SLJIT_SIG_GREATER32));
if (common->forced_quit_label == NULL)
- add_jump(compiler, &common->forced_quit, JUMP(SLJIT_NOT_EQUAL) /* SIG_LESS */);
+ add_jump(compiler, &common->forced_quit, JUMP(SLJIT_NOT_EQUAL32) /* SIG_LESS */);
else
- JUMPTO(SLJIT_NOT_EQUAL /* SIG_LESS */, common->forced_quit_label);
+ JUMPTO(SLJIT_NOT_EQUAL32 /* SIG_LESS */, common->forced_quit_label);
return cc + 2 + 2 * LINK_SIZE;
}

@@ -10607,7 +10610,7 @@
OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), LOCALS0);

OP1(SLJIT_MOV, STR_PTR, 0, TMP1, 0);
- add_jump(compiler, &common->reset_match, CMP(SLJIT_NOT_EQUAL, STR_PTR, 0, SLJIT_IMM, -1));
+ add_jump(compiler, &common->reset_match, CMP(SLJIT_NOT_EQUAL, STR_PTR, 0, SLJIT_IMM, 0));
return;
}

@@ -11208,8 +11211,8 @@
OP1(SLJIT_MOV, STR_END, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, end));
OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, stack));
OP1(SLJIT_MOV_U32, TMP1, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, limit_match));
-OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(TMP2), SLJIT_OFFSETOF(struct sljit_stack, base));
-OP1(SLJIT_MOV, STACK_LIMIT, 0, SLJIT_MEM1(TMP2), SLJIT_OFFSETOF(struct sljit_stack, limit));
+OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(TMP2), SLJIT_OFFSETOF(struct sljit_stack, end));
+OP1(SLJIT_MOV, STACK_LIMIT, 0, SLJIT_MEM1(TMP2), SLJIT_OFFSETOF(struct sljit_stack, start));
OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LIMIT_MATCH, TMP1, 0);

@@ -11415,20 +11418,22 @@
set_jumps(common->stackalloc, LABEL());
/* RETURN_ADDR is not a saved register. */
sljit_emit_fast_enter(compiler, SLJIT_MEM1(SLJIT_SP), LOCALS0);
-OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS1, TMP2, 0);
-OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
-OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, stack));
-OP1(SLJIT_MOV, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(struct sljit_stack, top), STACK_TOP, 0);
-OP2(SLJIT_SUB, TMP2, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(struct sljit_stack, limit), SLJIT_IMM, STACK_GROWTH_RATE);

+SLJIT_ASSERT(TMP1 == SLJIT_R0 && STACK_TOP == SLJIT_R1);
+
+OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS1, STACK_TOP, 0);
+OP1(SLJIT_MOV, SLJIT_R0, 0, ARGUMENTS, 0);
+OP2(SLJIT_SUB, SLJIT_R1, 0, STACK_LIMIT, 0, SLJIT_IMM, STACK_GROWTH_RATE);
+OP1(SLJIT_MOV, SLJIT_R0, 0, SLJIT_MEM1(SLJIT_R0), SLJIT_OFFSETOF(jit_arguments, stack));
+OP1(SLJIT_MOV, STACK_LIMIT, 0, TMP2, 0);
+
sljit_emit_icall(compiler, SLJIT_CALL, SLJIT_RET(SW) | SLJIT_ARG1(SW) | SLJIT_ARG2(SW), SLJIT_IMM, SLJIT_FUNC_OFFSET(sljit_stack_resize));
-jump = CMP(SLJIT_NOT_EQUAL, SLJIT_RETURN_REG, 0, SLJIT_IMM, 0);
-OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
-OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, stack));
-OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(struct sljit_stack, top));
-OP1(SLJIT_MOV, STACK_LIMIT, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(struct sljit_stack, limit));
-OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), LOCALS1);
-sljit_emit_fast_return(compiler, SLJIT_MEM1(SLJIT_SP), LOCALS0);
+jump = CMP(SLJIT_EQUAL, SLJIT_RETURN_REG, 0, SLJIT_IMM, 0);
+OP1(SLJIT_MOV, TMP2, 0, STACK_LIMIT, 0);
+OP1(SLJIT_MOV, STACK_LIMIT, 0, SLJIT_RETURN_REG, 0);
+OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), LOCALS0);
+OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), LOCALS1);
+sljit_emit_fast_return(compiler, TMP1, 0);

/* Allocation failed. */
JUMPHERE(jump);
@@ -11573,9 +11578,9 @@
sljit_u8 local_space[MACHINE_STACK_SIZE];
struct sljit_stack local_stack;

-local_stack.max_limit = local_space;
-local_stack.limit = local_space;
-local_stack.base = local_space + MACHINE_STACK_SIZE;
+local_stack.min_start = local_space;
+local_stack.start = local_space;
+local_stack.end = local_space + MACHINE_STACK_SIZE;
local_stack.top = local_space + MACHINE_STACK_SIZE;
arguments->stack = &local_stack;
convert_executable_func.executable_func = executable_func;

Modified: code/trunk/sljit/sljitLir.h
===================================================================
--- code/trunk/sljit/sljitLir.h    2018-01-08 11:44:57 UTC (rev 1721)
+++ code/trunk/sljit/sljitLir.h    2018-01-10 09:06:30 UTC (rev 1722)
@@ -1353,56 +1353,52 @@


#if (defined SLJIT_UTIL_STACK && SLJIT_UTIL_STACK)

-/* The sljit_stack is a utility extension of sljit, which provides
- a top-down stack. The stack top is stored in base and the stack
- goes down to max_limit, so the memory region for this stack is
- between max_limit (inclusive) and base (exclusive). However the
- application can only use the region between limit (inclusive)
- and base (exclusive). The sljit_stack_resize can be used to
- extend this region up to max_limit.
+/* The sljit_stack structure and its manipulation functions provides
+ an implementation for a top-down stack. The stack top is stored
+ in the end field of the sljit_stack structure and the stack goes
+ down to the min_start field, so the memory region reserved for
+ this stack is between min_start (inclusive) and end (exclusive)
+ fields. However the application can only use the region between
+ start (inclusive) and end (exclusive) fields. The sljit_stack_resize
+ function can be used to extend this region up to min_start.

    This feature uses the "address space reserve" feature of modern
-   operating systems, so instead of allocating a huge memory block
-   applications can allocate a small region and extend it later
-   without moving the memory area. Hence the region is never moved
-   so pointers are valid after resize. */
+   operating systems. Instead of allocating a large memory block
+   applications can allocate a small memory region and extend it
+   later without moving the content of the memory area. Therefore
+   after a successful resize by sljit_stack_resize all pointers into
+   this region are still valid.


-/* Note: base and max_limit fields are aligned to PAGE_SIZE bytes
-     (usually 4 Kbyte or more).
-   Note: stack should grow in larger steps, e.g. 4Kbyte, 16Kbyte or more.
-   Note: this structure may not be supported by all operating systems.
-     Some kind of fallback mechanism is suggested when SLJIT_UTIL_STACK
-     is not defined. */
+   Note:
+     this structure may not be supported by all operating systems.
+     end and max_limit fields are aligned to PAGE_SIZE bytes (usually
+         4 Kbyte or more).
+     stack should grow in larger steps, e.g. 4Kbyte, 16Kbyte or more. */


 struct sljit_stack {
     /* User data, anything can be stored here.
-       Starting with the same value as base. */
+       Initialized to the same value as the end field. */
     sljit_u8 *top;
-    /* These members are read only. */
-    sljit_u8 *base;
-    sljit_u8 *limit;
-    sljit_u8 *max_limit;
+/* These members are read only. */
+    /* End address of the stack */
+    sljit_u8 *end;
+    /* Current start address of the stack. */
+    sljit_u8 *start;
+    /* Lowest start address of the stack. */
+    sljit_u8 *min_start;
 };


-/* Returns NULL if unsuccessful.
-
-   Note:
-     max_limit field contains the lower bound adress of the stack.
-     limit field contains the current starting address of the stack.
-     base field contains the end address of the stack.
-     top field is initialized to base.
-
+/* Allocates a new stack. Returns NULL if unsuccessful.
    Note: see sljit_create_compiler for the explanation of allocator_data. */
-SLJIT_API_FUNC_ATTRIBUTE struct sljit_stack* SLJIT_FUNC sljit_allocate_stack(sljit_uw limit, sljit_uw max_limit, void *allocator_data);
+SLJIT_API_FUNC_ATTRIBUTE struct sljit_stack* SLJIT_FUNC sljit_allocate_stack(sljit_uw start_size, sljit_uw max_size, void *allocator_data);
 SLJIT_API_FUNC_ATTRIBUTE void SLJIT_FUNC sljit_free_stack(struct sljit_stack *stack, void *allocator_data);


-/* Can be used to increase (allocate) or decrease (free) the memory area.
- Returns with a non-zero value if unsuccessful. If new_limit is greater than
- max_limit, it will fail. It is very easy to implement a stack data structure,
- since the growth ratio can be added to the current limit, and sljit_stack_resize
- will do all the necessary checks. The fields of the stack are not changed if
- sljit_stack_resize fails. */
-SLJIT_API_FUNC_ATTRIBUTE sljit_sw SLJIT_FUNC sljit_stack_resize(struct sljit_stack *stack, sljit_u8 *new_limit);
+/* Can be used to increase (extend) or decrease (shrink) the stack
+ memory area. Returns with new_start if successful and NULL otherwise.
+ It always fails if new_start is less than min_start or greater or equal
+ than end fields. The fields of the stack are not changed if the returned
+ value is NULL (the current memory content is never lost). */
+SLJIT_API_FUNC_ATTRIBUTE sljit_u8 *SLJIT_FUNC sljit_stack_resize(struct sljit_stack *stack, sljit_u8 *new_start);

#endif /* (defined SLJIT_UTIL_STACK && SLJIT_UTIL_STACK) */


Modified: code/trunk/sljit/sljitNativeX86_64.c
===================================================================
--- code/trunk/sljit/sljitNativeX86_64.c    2018-01-08 11:44:57 UTC (rev 1721)
+++ code/trunk/sljit/sljitNativeX86_64.c    2018-01-10 09:06:30 UTC (rev 1722)
@@ -41,11 +41,11 @@


 static sljit_u8* generate_far_jump_code(struct sljit_jump *jump, sljit_u8 *code_ptr, sljit_s32 type)
 {
+    int short_addr = !(jump->flags & SLJIT_REWRITABLE_JUMP) && !(jump->flags & JUMP_LABEL) && (jump->u.target <= 0xffffffff);
+
     /* The relative jump below specialized for this case. */
     SLJIT_ASSERT(reg_map[TMP_REG2] >= 8);


-    int short_addr = !(jump->flags & SLJIT_REWRITABLE_JUMP) && !(jump->flags & JUMP_LABEL) && (jump->u.target <= 0xffffffff);
-
     if (type < SLJIT_JUMP) {
         /* Invert type. */
         *code_ptr++ = get_jump_code(type ^ 0x1) - 0x10;


Modified: code/trunk/sljit/sljitUtils.c
===================================================================
--- code/trunk/sljit/sljitUtils.c    2018-01-08 11:44:57 UTC (rev 1721)
+++ code/trunk/sljit/sljitUtils.c    2018-01-10 09:06:30 UTC (rev 1722)
@@ -203,7 +203,7 @@
 /* Planning to make it even more clever in the future. */
 static sljit_sw sljit_page_align = 0;


-SLJIT_API_FUNC_ATTRIBUTE struct sljit_stack* SLJIT_FUNC sljit_allocate_stack(sljit_uw limit, sljit_uw max_limit, void *allocator_data)
+SLJIT_API_FUNC_ATTRIBUTE struct sljit_stack* SLJIT_FUNC sljit_allocate_stack(sljit_uw start_size, sljit_uw max_size, void *allocator_data)
 {
     struct sljit_stack *stack;
     void *ptr;
@@ -212,7 +212,7 @@
 #endif


     SLJIT_UNUSED_ARG(allocator_data);
-    if (limit > max_limit || limit < 1)
+    if (start_size > max_size || start_size < 1)
         return NULL;


 #ifdef _WIN32
@@ -234,25 +234,27 @@
     if (!stack)
         return NULL;


-    /* Align max_limit. */
-    max_limit = (max_limit + sljit_page_align) & ~sljit_page_align;
+    /* Align max_size. */
+    max_size = (max_size + sljit_page_align) & ~sljit_page_align;


 #ifdef _WIN32
-    ptr = VirtualAlloc(NULL, max_limit, MEM_RESERVE, PAGE_READWRITE);
+    ptr = VirtualAlloc(NULL, max_size, MEM_RESERVE, PAGE_READWRITE);
     if (!ptr) {
         SLJIT_FREE(stack, allocator_data);
         return NULL;
     }
-    stack->max_limit = (sljit_u8 *)ptr;
-    stack->base = stack->max_limit + max_limit;
-    stack->limit = stack->base;
-    if (sljit_stack_resize(stack, stack->base - limit)) {
+
+    stack->min_start = (sljit_u8 *)ptr;
+    stack->end = stack->min_start + max_size;
+    stack->start = stack->end;
+
+    if (sljit_stack_resize(stack, stack->end - start_size) == NULL) {
         sljit_free_stack(stack, allocator_data);
         return NULL;
     }
 #else
 #ifdef MAP_ANON
-    ptr = mmap(NULL, max_limit, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
+    ptr = mmap(NULL, max_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
 #else
     if (dev_zero < 0) {
         if (open_dev_zero()) {
@@ -260,17 +262,17 @@
             return NULL;
         }
     }
-    ptr = mmap(NULL, max_limit, PROT_READ | PROT_WRITE, MAP_PRIVATE, dev_zero, 0);
+    ptr = mmap(NULL, max_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, dev_zero, 0);
 #endif
     if (ptr == MAP_FAILED) {
         SLJIT_FREE(stack, allocator_data);
         return NULL;
     }
-    stack->max_limit = (sljit_u8 *)ptr;
-    stack->base = stack->max_limit + max_limit;
-    stack->limit = stack->base - limit;
+    stack->min_start = (sljit_u8 *)ptr;
+    stack->end = stack->min_start + max_size;
+    stack->start = stack->end - start_size;
 #endif
-    stack->top = stack->base;
+    stack->top = stack->end;
     return stack;
 }


@@ -280,53 +282,50 @@
 {
     SLJIT_UNUSED_ARG(allocator_data);
 #ifdef _WIN32
-    VirtualFree((void*)stack->max_limit, 0, MEM_RELEASE);
+    VirtualFree((void*)stack->min_start, 0, MEM_RELEASE);
 #else
-    munmap((void*)stack->max_limit, stack->base - stack->max_limit);
+    munmap((void*)stack->min_start, stack->end - stack->min_start);
 #endif
     SLJIT_FREE(stack, allocator_data);
 }


-SLJIT_API_FUNC_ATTRIBUTE sljit_sw SLJIT_FUNC sljit_stack_resize(struct sljit_stack *stack, sljit_u8 *new_limit)
+SLJIT_API_FUNC_ATTRIBUTE sljit_u8 *SLJIT_FUNC sljit_stack_resize(struct sljit_stack *stack, sljit_u8 *new_start)
 {
-    sljit_uw aligned_old_limit;
-    sljit_uw aligned_new_limit;
+    sljit_uw aligned_old_start;
+    sljit_uw aligned_new_start;


-    if ((new_limit < stack->max_limit) || (new_limit >= stack->base))
-        return -1;
+    if ((new_start < stack->min_start) || (new_start >= stack->end))
+        return NULL;
+
 #ifdef _WIN32
-    aligned_new_limit = (sljit_uw)new_limit & ~sljit_page_align;
-    aligned_old_limit = ((sljit_uw)stack->limit) & ~sljit_page_align;
-    if (aligned_new_limit != aligned_old_limit) {
-        if (aligned_new_limit < aligned_old_limit) {
-            if (!VirtualAlloc((void*)aligned_new_limit, aligned_old_limit - aligned_new_limit, MEM_COMMIT, PAGE_READWRITE))
-                return -1;
+    aligned_new_start = (sljit_uw)new_start & ~sljit_page_align;
+    aligned_old_start = ((sljit_uw)stack->start) & ~sljit_page_align;
+    if (aligned_new_start != aligned_old_start) {
+        if (aligned_new_start < aligned_old_start) {
+            if (!VirtualAlloc((void*)aligned_new_start, aligned_old_start - aligned_new_start, MEM_COMMIT, PAGE_READWRITE))
+                return NULL;
         }
         else {
-            if (!VirtualFree((void*)aligned_old_limit, aligned_new_limit - aligned_old_limit, MEM_DECOMMIT))
-                return -1;
+            if (!VirtualFree((void*)aligned_old_start, aligned_new_start - aligned_old_start, MEM_DECOMMIT))
+                return NULL;
         }
     }
-    stack->limit = new_limit;
-    return 0;
 #else
-    if (new_limit <= stack->limit) {
-        stack->limit = new_limit;
-        return 0;
-    }
-    aligned_new_limit = (sljit_uw)new_limit & ~sljit_page_align;
-    aligned_old_limit = ((sljit_uw)stack->limit) & ~sljit_page_align;
-    /* If madvise is available, we release the unnecessary space. */
+    if (stack->start < new_start) {
+        aligned_new_start = (sljit_uw)new_start & ~sljit_page_align;
+        aligned_old_start = ((sljit_uw)stack->start) & ~sljit_page_align;
+        /* If madvise is available, we release the unnecessary space. */
 #if defined(MADV_DONTNEED)
-    if (aligned_new_limit > aligned_old_limit)
-        madvise((void*)aligned_old_limit, aligned_new_limit - aligned_old_limit, MADV_DONTNEED);
+        if (aligned_new_start > aligned_old_start)
+            madvise((void*)aligned_old_start, aligned_new_start - aligned_old_start, MADV_DONTNEED);
 #elif defined(POSIX_MADV_DONTNEED)
-    if (aligned_new_limit > aligned_old_limit)
-        posix_madvise((void*)aligned_old_limit, aligned_new_limit - aligned_old_limit, POSIX_MADV_DONTNEED);
+        if (aligned_new_start > aligned_old_start)
+            posix_madvise((void*)aligned_old_start, aligned_new_start - aligned_old_start, POSIX_MADV_DONTNEED);
 #endif
-    stack->limit = new_limit;
-    return 0;
+    }
 #endif
+    stack->start = new_start;
+    return new_start;
 }


#endif /* SLJIT_UTIL_STACK */