<commit_before>#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "yarn.h"

static void yarn_sys_gettime(yarn_State *Y) {
  yarn_int t = time(NULL);
  yarn_setRegister(Y, YARN_REG_RETURN, &t);
}
static void yarn_sys_getvmmemory(yarn_State *Y) {
  yarn_setRegister(Y, YARN_REG_RETURN, &Y->memsize);
}

yarn_State *yarn_init(size_t memsize) {
  yarn_uint stackaddr;
  yarn_uint instaddr;
  yarn_State *Y = malloc(sizeof(yarn_State));
  Y->code = NULL;
  Y->memsize = memsize;
  Y->memory = calloc(memsize,1);

  //Init our instruction pointer to 0
  instaddr = 0;
  yarn_setRegister(Y, YARN_REG_INSTRUCTION, &instaddr);

  // Init our stack&base pointer to the top of the memory, stack will grow down.
  //   Leave space for the registers, flags, and other stuff at the top.
  stackaddr =  memsize-(YARN_REG_NUM+2)*sizeof(yarn_uint);
  yarn_setRegister(Y, YARN_REG_STACK, &stackaddr);
  yarn_setRegister(Y, YARN_REG_BASE, &stackaddr);

  struct { yarn_uint id; yarn_CFunc fn; } syscalls[] = {
    { 0x00,  yarn_sys_getvmmemory   },
    { 0x01,  yarn_sys_gettime       },
    { 0x00,  NULL                   }
  };
  for (int i = 0; syscalls[i].fn; i++) {
    yarn_registerSysCall(Y, syscalls[i].id, syscalls[i].fn);
  }

  return Y;
}

void yarn_destroy(yarn_State *Y) {
  if (Y->code != NULL) {
    free(Y->code);
  }
  free(Y->memory);
  free(Y);
}

// Will copy given code to an internal buffer.
void yarn_loadCode(yarn_State *Y, char *code, size_t codesize) {
  Y->code = malloc(codesize);
  memcpy(Y->code, code, codesize);
  Y->codesize = codesize;
}


#define enumcase(r,val) case r: result = val; break;<commit_msg>Add syscall to get cycle count<commit_after>#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "yarn.h"

static void yarn_sys_gettime(yarn_State *Y) {
  yarn_int t = time(NULL);
  yarn_setRegister(Y, YARN_REG_RETURN, &t);
}
static void yarn_sys_getinstructioncount(yarn_State *Y) {
  yarn_setRegister(Y, YARN_REG_RETURN, &Y->instructioncount);
}
static void yarn_sys_getvmmemory(yarn_State *Y) {
  yarn_setRegister(Y, YARN_REG_RETURN, &Y->memsize);
}

yarn_State *yarn_init(size_t memsize) {
  yarn_uint stackaddr;
  yarn_uint instaddr;
  yarn_State *Y = malloc(sizeof(yarn_State));
  Y->code = NULL;
  Y->memsize = memsize;
  Y->memory = calloc(memsize,1);

  //Init our instruction pointer to 0
  instaddr = 0;
  yarn_setRegister(Y, YARN_REG_INSTRUCTION, &instaddr);

  // Init our stack&base pointer to the top of the memory, stack will grow down.
  //   Leave space for the registers, flags, and other stuff at the top.
  stackaddr =  memsize-(YARN_REG_NUM+2)*sizeof(yarn_uint);
  yarn_setRegister(Y, YARN_REG_STACK, &stackaddr);
  yarn_setRegister(Y, YARN_REG_BASE, &stackaddr);

  struct { yarn_uint id; yarn_CFunc fn; } syscalls[] = {
    { 0x00,  yarn_sys_getvmmemory               },
    { 0x01,  yarn_sys_getinstructioncount       },
    { 0x02,  yarn_sys_gettime                   },
    { 0x00,  NULL                               }
  };
  for (int i = 0; syscalls[i].fn; i++) {
    yarn_registerSysCall(Y, syscalls[i].id, syscalls[i].fn);
  }

  return Y;
}

void yarn_destroy(yarn_State *Y) {
  if (Y->code != NULL) {
    free(Y->code);
  }
  free(Y->memory);
  free(Y);
}

// Will copy given code to an internal buffer.
void yarn_loadCode(yarn_State *Y, char *code, size_t codesize) {
  Y->code = malloc(codesize);
  memcpy(Y->code, code, codesize);
  Y->codesize = codesize;
}


#define enumcase(r,val) case r: result = val; break;
{'fraction_non_alphanumeric': 0.13361581920903956, 'fraction_numerical': 0.008757062146892656, 'mean_word_length': 3.7402945113788486, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 32, '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': '2671252', 'n_tokens_mistral': 1456, 'n_tokens_neox': 1383, 'n_words': 311}