<commit_before>#include "iencoding.h"
#include "../vcompiler.h"
#include "../codegen.h"
#include "../dispatcher.h"
#include "../codemanager.h"
#include "../Python/frames.h"


DEFINEVAR reg_t RegistersLoop[REG_TOTAL] = {
    /* RAX > */ REG_NONE,
    /* RCX > */ REG_NONE,
    /* RDX > */ REG_NONE,
    /* RBX > */ RBP_IS_RESERVED ? REG_LOOP_START : REG_X64_RBP,
    /* RSP > */ REG_NONE,
    /* RBP > */ RBP_IS_RESERVED ? REG_NONE : REG_LOOP_START,
    /* RSI > */ REG_NONE,
    /* RDI > */ REG_NONE,
    /* R8  > */ REG_NONE,
    /* R9  > */ REG_NONE,
    /* R10 > */ REG_NONE,
    /* R11 > */ REG_NONE,
    /* R12 > */ REG_NONE,
    /* R13 > */ REG_X64_R14,
    /* R14 > */ REG_X64_R15,
    /* R15 > */ REG_X64_RBX
};

DEFINEFN void* psyco_call_code_builder(PsycoObject* po, void* fn, int restore, RunTimeSource extraarg, size_t block_size)
{
  char* block_start;
  BEGIN_CODE
  BEGIN_SHORT_JUMP(0);
  block_start = (char*)(((long)code + 7) & (~7)); /* alignment */
  code = block_start;
  code += block_size;
  END_SHORT_JUMP(0);
  BEGIN_CALL();
  if (extraarg != SOURCE_DUMMY) {
    CALL_SET_ARG_FROM_RT(extraarg, 1);
  }
  CALL_SET_ARG_IMMED(block_start, 0);
  END_CALL_I(fn);
  JMP_R(REG_FUNCTIONS_RETURN);
  END_CODE
  return (void*)block_start;
}

DEFINEFN
vinfo_t* psyco_call_psyco(PsycoObject* po, CodeBufferObject* codebuf,
			  Source argsources[], int argcount,
			  struct stack_frame_info_s* finfo)
{
	/* this is a simplified version of psyco_generic_call() which
	   assumes Psyco's calling convention instead of the C's. */
	int i;
	bool ccflags;
    int initial_stack_depth;
	BEGIN_CODE
	/* cannot use NEED_CC(): it might clobber one of the registers
	   mentioned in argsources */
	ccflags = HAS_CCREG(po);
	if (ccflags)
		PUSH_CC();
    
    /* PUSH RTs with reg set */
    for(i = 0; i < REG_TOTAL; i++) {
        vinfo_t* content = REG_NUMBER(po, i);
        if (content != NULL) {
            PUSH_R(i);
            psyco_inc_stackdepth(po);
        }
    }
#if CHECK_STACK_DEPTH
    MOV_R_R(REG_TRANSIENT_1, REG_X64_RSP);
    SUB_R_I8(REG_TRANSIENT_1, 8);
    PUSH_R(REG_TRANSIENT_1);
    psyco_inc_stackdepth(po);
#endif
    PUSH_I(-1); /* finfo */
    psyco_inc_stackdepth(po);

    ABOUT_TO_CALL_SUBFUNCTION(finfo);
    finfo_last(finfo)->link_stack_depth = po->stack_depth;
    initial_stack_depth = po->stack_depth;
    /* We assume that compiler register state is not changed by the call macros */
    /* note that we are not using C calling convention here */
    for(i = 0; i < argcount; i++) {
        Source source = argsources[i];
        if(RSOURCE_REG_IS_NONE(source)) {
            assert(getstack(source) != RUNTIME_STACK_NONE);
            PUSH_O(REG_X64_RSP, STACK_POS_OFFSET(RSOURCE_STACK(source)));
        } else {
            PUSH_R(getreg(source));<commit_msg>Fixed stack unwinding<commit_after>#include "iencoding.h"
#include "../vcompiler.h"
#include "../codegen.h"
#include "../dispatcher.h"
#include "../codemanager.h"
#include "../Python/frames.h"


DEFINEVAR reg_t RegistersLoop[REG_TOTAL] = {
    /* RAX > */ REG_NONE,
    /* RCX > */ REG_NONE,
    /* RDX > */ REG_NONE,
    /* RBX > */ RBP_IS_RESERVED ? REG_LOOP_START : REG_X64_RBP,
    /* RSP > */ REG_NONE,
    /* RBP > */ RBP_IS_RESERVED ? REG_NONE : REG_LOOP_START,
    /* RSI > */ REG_NONE,
    /* RDI > */ REG_NONE,
    /* R8  > */ REG_NONE,
    /* R9  > */ REG_NONE,
    /* R10 > */ REG_NONE,
    /* R11 > */ REG_NONE,
    /* R12 > */ REG_NONE,
    /* R13 > */ REG_X64_R14,
    /* R14 > */ REG_X64_R15,
    /* R15 > */ REG_X64_RBX
};

DEFINEFN void* psyco_call_code_builder(PsycoObject* po, void* fn, int restore, RunTimeSource extraarg, size_t block_size)
{
  char* block_start;
  BEGIN_CODE
  BEGIN_SHORT_JUMP(0);
  block_start = (char*)(((long)code + 7) & (~7)); /* alignment */
  code = block_start;
  code += block_size;
  END_SHORT_JUMP(0);
  BEGIN_CALL();
  if (extraarg != SOURCE_DUMMY) {
    CALL_SET_ARG_FROM_RT(extraarg, 1);
  }
  CALL_SET_ARG_IMMED(block_start, 0);
  END_CALL_I(fn);
  JMP_R(REG_FUNCTIONS_RETURN);
  END_CODE
  return (void*)block_start;
}

DEFINEFN
vinfo_t* psyco_call_psyco(PsycoObject* po, CodeBufferObject* codebuf,
			  Source argsources[], int argcount,
			  struct stack_frame_info_s* finfo)
{
	/* this is a simplified version of psyco_generic_call() which
	   assumes Psyco's calling convention instead of the C's. */
	int i;
	bool ccflags;
    int initial_stack_depth;
	BEGIN_CODE
	/* cannot use NEED_CC(): it might clobber one of the registers
	   mentioned in argsources */
	ccflags = HAS_CCREG(po);
	if (ccflags)
		PUSH_CC();
    
    /* PUSH RTs with reg set */
    for(i = 0; i < REG_TOTAL; i++) {
        vinfo_t* content = REG_NUMBER(po, i);
        if (content != NULL) {
            PUSH_R(i);
            psyco_inc_stackdepth(po);
        }
    }
#if CHECK_STACK_DEPTH
    MOV_R_R(REG_TRANSIENT_1, REG_X64_RSP);
    SUB_R_I8(REG_TRANSIENT_1, 8);
    PUSH_R(REG_TRANSIENT_1);
    psyco_inc_stackdepth(po);
#endif
    PUSH_I(-1); /* finfo */
    psyco_inc_stackdepth(po);

    ABOUT_TO_CALL_SUBFUNCTION(finfo);
    /* offset in stack to this this finfo */
    finfo_last(finfo)->link_stack_depth = po->stack_depth - INITIAL_STACK_DEPTH; 
    initial_stack_depth = po->stack_depth;
    /* We assume that compiler register state is not changed by the call macros */
    /* note that we are not using C calling convention here */
    for(i = 0; i < argcount; i++) {
        Source source = argsources[i];
        if(RSOURCE_REG_IS_NONE(source)) {
            assert(getstack(source) != RUNTIME_STACK_NONE);
            PUSH_O(REG_X64_RSP, STACK_POS_OFFSET(RSOURCE_STACK(source)));
        } else {
            PUSH_R(getreg(source));
{'fraction_non_alphanumeric': 0.13519690576652602, 'fraction_numerical': 0.015471167369901548, 'mean_word_length': 3.0119887165021155, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3170454', 'n_tokens_mistral': 2290, 'n_tokens_neox': 2181, 'n_words': 447}