Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: e0946f5500
Fetching contributors…

Cannot retrieve contributors at this time

file 151 lines (124 sloc) 3.571 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
#include <stdio.h>
#include <stdlib.h>

#include "vm.h"
#include "bc.h"

extern Word bci_ret();
extern Word vm_resume(VMState *L);
extern int vm_init_dispatch(AsmFunction *disp_tbl);
extern AsmFunction vm_leave;

/**
*
*/
//extern Word vm_call(VMState *L, int nargs, int nresults);

#define DEFAULT_STACK_SIZE 1024

VMState *create_thread(GblState *gbl)
{
  // TODO: allocate everything on the GC'd heap
  VMState* vms = malloc(sizeof(VMState));
  Word* stack = malloc(DEFAULT_STACK_SIZE * sizeof(Word));
  
  vms->gbl_state = gbl;
  vms->saved_cstack = 0; // set by interpreter
  vms->stack = stack;
  vms->stack_limit = stack + DEFAULT_STACK_SIZE;
  vms->base = stack;
  vms->top = stack;
  
  return vms;
}

void destroy_thread(VMState *L)
{
  free(L->stack);
  free(L);
}

void barf(char* last_words)
{
  printf("FATAL: %s\n", last_words);
  exit(1);
}

void lc_stackoverflow()
{
  // TODO:
  barf("Stack overflow");
}

void lc_pushint(VMState *L, int n)
{
  if (L->top >= L->stack_limit)
    lc_stackoverflow();

  *(L->top)++ = (Word)n;
}

void lc_pushptr(VMState *L, void *p)
{
  if (L->top >= L->stack_limit)
    lc_stackoverflow();
  
  *(L->top)++ = (Word)p;
}

void lc_printstate(VMState *L)
{
  Word *s;
  int n;

  if (!(L->top >= L->stack && L->top < L->stack_limit))
    barf("Stack invariant");

  printf("Stack (top 16 slots) %p-%p:\n", L->stack, L->stack_limit);
  for (s = L->top - 1, n = 16; s >= L->stack && n > 0; s--, n--) {
    printf(" %p: %08x (%d)\n", s, *s, *s);
  }
}

extern uint32_t vm_bench(uint32_t count);

/*
* Lua C calling interface:
*
* 1. Push function
* 2. Push arguments left to right
* 3. Use `lua_call(L, nargs, nresults)`. L is the thread state
* nargs is the number of arguments, nresults is the number of
* results.
* 4. After the call, the last result is on top of the stack.
*
*/

int main(int argc, char* argv[])
{
  GblState gbl;
  VMState *L;
  BCIns testcode[32];
  uint32_t count = 200 * 1000 * 1000;
  char c;
  vm_init_dispatch(gbl.dispatch_tbl);
  L = create_thread(&gbl);
  L->savedpc = testcode;
  /*
lc_pushint(L, 0); // make R(0) = 0
lc_pushint(L, 1); // make R(1) = 1
lc_pushint(L, 23); // R(2)
lc_pushint(L, 42); // R(3)
lc_pushint(L, 5); // R(4)
lc_printstate(L);
testcode[0] = BCINS_ABC(OP_ADDI_RR, 4, 0, 0); // R(4) = 0
testcode[1] = BCINS_AD(OP_ISLT, 2, 3);
testcode[2] = BCINS_AJ(OP_JMP, 0, 1);
testcode[3] = BCINS_ABC(OP_ADDI_RR, 4, 0, 1);
testcode[4] = BCINS_AD(OP_EXIT, 0, 0); // stop VM
*/
  lc_pushint(L, 0); // R(0) = 0
  lc_pushint(L, 1); // R(1) = 1
  lc_pushint(L, count); // R(2) = iteration count
  lc_pushint(L, 0); // R(3) = step countre
  lc_printstate(L);
  printf("1st instr: %p\n", testcode);

  // Calculating the jump offset: target instr id - next instr id
  testcode[0] = BCINS_AD(OP_ISLE, 2, 0);
  testcode[1] = BCINS_AJ(OP_JMP, 0, 6); // goto 8
  testcode[2] = BCINS_ABC(OP_ADDI_RR, 3, 3, 1); // Do some work
  testcode[3] = BCINS_ABC(OP_ADDI_RR, 3, 3, 1); // Do some work
  testcode[4] = BCINS_ABC(OP_ADDI_RR, 3, 3, 1); // Do some work
  testcode[5] = BCINS_ABC(OP_ADDI_RR, 3, 3, 1); // Do some work
  testcode[6] = BCINS_ABC(OP_SUBI_RR, 2, 2, 1); // decrement counter
  testcode[7] = BCINS_AJ(OP_JMP, 0, -8); // goto 0
  testcode[8] = BCINS_AD(OP_EXIT, 0, 0);

  
/* printf("%p\n", vm_leave); */
  printf("Dispatch Table: %p\n", gbl.dispatch_tbl);
  c = getc(stdin);

  printf("vm: %x\n", vm_resume(L));
  //printf("acc: %x\n", vm_bench(count));
  
  lc_printstate(L);

  destroy_thread(L);
  
  return 0;
}
Something went wrong with that request. Please try again.