<commit_before>#include "shell.h"

#include "map.h"
#include "orb.h"
#include "config.h"

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

#include <readline/readline.h>
#include <readline/history.h>

#define FAIL(cond, fmt, ...) if ((cond)) { printf(fmt, ##__VA_ARGS__); return -1; }

#define ARG_CNT_FAIL(idx) FAIL(args[idx] == NULL, "not enough arguments\n")
#define ORB_SEL_FAIL      FAIL(current_orb == NULL, "no orb selected\n")
#define GET_ORB_FAIL(orb, str) \
  orb = str2orb(str); FAIL(orb == NULL, "no such orb: %s\n", str)

extern map_t* map;

static char* orbs_shell_getline(char* pref) {
  char* line = readline(pref);
  add_history(line);
  return line;
}

static char** orbs_shell_split_args(char* line) {
  int bufsize = 64, pos = 0;
  char* arg;
  char** args = malloc(sizeof(char*) * bufsize);

  arg = strtok(line, " \n\r\a\t");
  while (arg != NULL) {

    args[pos++] = arg;

    if (pos >= bufsize) {
      bufsize <<= 2;
      args = realloc(args, sizeof(char*) * bufsize);
    }

    arg = strtok(NULL, " \n\r\a\t");
  }

  args[pos] = NULL;

  return args;
}

static orb_t* get_orb(int id) {
  node_t* node = map->orbs.head;
  while (node) {
    orb_t* orb = node->data;

    if (orb->id == id)
      return orb;

    node = node->next;
  }
  return NULL;
}

static orb_t* str2orb(char* str) {
  if (strlen(str) > 3) {
    int id = atoi(str + 3);
    return get_orb(id);
  }

  return NULL;
}

static float calc_orb_instr_usage(orb_t* orb) {
  int idx = 0, count = 0;

  while (idx < ORB_GENE_SIZE) {
    count += ((float)orb->trace[idx]/orb->trace_count < .0001 ? 0 : 1);
    idx++;
  }

  return 100.0*count/ORB_GENE_SIZE;
}

static void print_orb_status(orb_t* orb) {
  printf("%8p  ->  map/orb%d/ @ [%2d, %2d]\n", orb, orb->id, orb->x, orb->y);
  printf("  [Score]  : %d\n", orb->score);
  printf("  [TTL]    : %d\n", orb->ttl);
  printf("  [Status] : 0x%02x\n", orb->status);
  printf("  [Regs]   : r0: 0x%02x, r1: 0x%02x, r2: 0x%02x, r3: 0x%02x\n",
      orb->regs[0], orb->regs[1], orb->regs[2], orb->regs[3]);
  printf("  [LR]     : 0x%02x\n", orb->lr);
  printf("  [Idx]    : 0x%02x\n", orb->idx);
  printf("  [IU]     : %.2f %%\n", calc_orb_instr_usage(orb));
}

static void print_map_status(map_t* map) {
  printf("%8p  ->  map/\n", &map->orbs);
  printf("  [Iter]   : 0x%lx\n", map->iteration);
  printf("  [Orbs]   : %ld\n", map->orbs.size);
}

static orb_t * current_orb = NULL;

static int print_orb_disas_instr(orb_t* orb, int idx) {
  int n;
  char buffer[64];

  // disassemble instruction
  n = orb_disas(orb, idx, buffer);

  if (strcmp(buffer, "nop") == 0)
    printf("\e[37m");

  printf("%*s [ %6.2f%% ]\r", 60, " ", 100.0*orb->trace[idx]/orb->trace_count);
  printf("[0x%02x]: 0x%02x %s\n\e[39m", idx, orb->genes[idx], buffer);

  return n;
}

static int orbs_cmd_disas(char** args) {
  ORB_SEL_FAIL;
  printf("%p -> genes @ map/orb%d/\n", current_orb, current_orb->id);

  int nodeadcode = 0;
  if (args[1] != NULL && strcmp(args[1], "ndc") == 0)
    nodeadcode = 1;

  int idx = 0;
  while (idx < ORB_GENE_SIZE) {

    int dead = (float)current_orb->trace[idx]/current_orb->trace_count < .0001;
    if(nodeadcode && dead) {
      idx++;
      continue;
    }

    if (idx == current_orb->idx)
      printf(" -> ");
    else
      printf("    ");
    idx += print_orb_disas_instr(current_orb, idx);
  }

  return 1;
}

static int orbs_cmd_p(char** args) {

  orb_t* orb = NULL;

  if (args[1] == NULL && current_orb == NULL) {
    print_map_status(map);
    return 1;
  } else if (args[1] != NULL) {
    GET_ORB_FAIL(orb, args[1]);
  } else {
    ORB_SEL_FAIL;
    orb = current_orb;
  }

  print_orb_status(orb);

  return 1;
}

static int orbs_cmd_s(char** args) {<commit_msg>added option to print status of all orbs in map, minor fixes<commit_after>#include "shell.h"

#include "map.h"
#include "orb.h"
#include "config.h"

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

#include <readline/readline.h>
#include <readline/history.h>

#define FAIL(cond, fmt, ...) if ((cond)) { printf(fmt, ##__VA_ARGS__); return -1; }

#define ARG_CNT_FAIL(idx) FAIL(args[idx] == NULL, "not enough arguments\n")
#define ORB_SEL_FAIL      FAIL(current_orb == NULL, "no orb selected\n")
#define GET_ORB_FAIL(orb, str) \
  orb = str2orb(str); FAIL(orb == NULL, "no such orb: %s\n", str)

extern map_t* map;

static char* orbs_shell_getline(char* pref) {
  char* line = readline(pref);
  add_history(line);
  return line;
}

static char** orbs_shell_split_args(char* line) {
  int bufsize = 64, pos = 0;
  char* arg;
  char** args = malloc(sizeof(char*) * bufsize);

  arg = strtok(line, " \n\r\a\t");
  while (arg != NULL) {

    args[pos++] = arg;

    if (pos >= bufsize) {
      bufsize <<= 2;
      args = realloc(args, sizeof(char*) * bufsize);
    }

    arg = strtok(NULL, " \n\r\a\t");
  }

  args[pos] = NULL;

  return args;
}

static orb_t* get_orb(int id) {
  node_t* node = map->orbs.head;
  while (node) {
    orb_t* orb = node->data;

    if (orb->id == id)
      return orb;

    node = node->next;
  }
  return NULL;
}

static orb_t* str2orb(char* str) {
  if (strlen(str) > 3) {
    int id = atoi(str + 3);
    return get_orb(id);
  }

  return NULL;
}

static float calc_orb_instr_usage(orb_t* orb) {
  int idx = 0, count = 0;

  while (idx < ORB_GENE_SIZE) {
    count += ((float)orb->trace[idx]/orb->trace_count < .0001 ? 0 : 1);
    idx++;
  }

  return 100.0*count/ORB_GENE_SIZE;
}

static void print_orb_status(orb_t* orb) {
  printf("%8p  ->  map/orb%d/ @ [%2d, %2d]\n", orb, orb->id, orb->x, orb->y);
  printf("  [Score]  : %d\n", orb->score);
  printf("  [TTL]    : %d\n", orb->ttl);
  printf("  [Status] : 0x%02x\n", orb->status);
  printf("  [Regs]   : r0: 0x%02x, r1: 0x%02x, r2: 0x%02x, r3: 0x%02x\n",
      orb->regs[0], orb->regs[1], orb->regs[2], orb->regs[3]);
  printf("  [LR]     : 0x%02x\n", orb->lr);
  printf("  [Idx]    : 0x%02x\n", orb->idx);
  printf("  [IMU]    : %.2f %%\n", calc_orb_instr_usage(orb));
}

static void print_map_status(map_t* map) {
  printf("%8p  ->  map/\n", &map->orbs);
  printf("  [Iter]   : 0x%lx\n", map->iteration);
  printf("  [Orbs]   : %ld\n", map->orbs.size);
}

static orb_t * current_orb = NULL;

static int print_orb_disas_instr(orb_t* orb, int idx) {
  int n;
  char buffer[64];

  // disassemble instruction
  n = orb_disas(orb, idx, buffer);

  if (strcmp(buffer, "nop") == 0)
    printf("\e[37m");

  printf("%*s [ %6.2f%% ]\r", 60, " ", 100.0*orb->trace[idx]/orb->trace_count);
  printf("%s[0x%02x]: 0x%02x %s\n\e[39m", orb->idx == idx ? " -> " : "    ",
      idx, orb->genes[idx], buffer);

  return n;
}

static int orbs_cmd_disas(char** args) {
  ORB_SEL_FAIL;
  printf("%p -> genes @ map/orb%d/\n", current_orb, current_orb->id);

  int nodeadcode = 0;
  if (args[1] != NULL && strcmp(args[1], "ndc") == 0)
    nodeadcode = 1;

  int idx = 0;
  while (idx < ORB_GENE_SIZE) {

    int dead = (float)current_orb->trace[idx]/current_orb->trace_count < .0001;
    if(nodeadcode && dead) {
      idx++;
      continue;
    }

    idx += print_orb_disas_instr(current_orb, idx);
  }

  return 1;
}

static int orbs_cmd_p(char** args) {

  orb_t* orb = NULL;

  if (args[1] == NULL && current_orb == NULL) {
    print_map_status(map);
    return 1;
  } else if (args[1] != NULL && strcmp(args[1], "*") == 0) {
    FAIL(current_orb != NULL, "option '*' not allowed for orb\n");
    node_t* node = map->orbs.head;
    while (node) {
      orb_t* orb = node->data;
      print_orb_status(orb);
      node = node->next;
    }
    return 1;
  } else if (args[1] != NULL) {
    GET_ORB_FAIL(orb, args[1]);
  } else {
    ORB_SEL_FAIL;
    orb = current_orb;
  }

  print_orb_status(orb);

  return 1;
}

static int orbs_cmd_s(char** args) {
{'fraction_non_alphanumeric': 0.19616424250225253, 'fraction_numerical': 0.024456171965503926, 'mean_word_length': 3.130781499202552, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 84, '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': '7002959', 'n_tokens_mistral': 3428, 'n_tokens_neox': 3246, 'n_words': 719}