Advertisement
Guest User

tophat02

a guest
Jan 3rd, 2010
1,792
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.75 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. /*
  4.  * A little playground for VM and JIT testing.  The basic idea is to have an instruction set that is
  5.  * EXTREMELY EASY to decode: we prefer more instructions versus complex decoding logic.  For example,
  6.  * most opcodes have a W version that lets the last argument (usually a word) take up an entire
  7.  * wordsize.  Also, we don't have operand selector bits.  So OP_LOADI would load the address from
  8.  * the register given by the second arg and OP_LOADM would use INS+1 to load the contents of a full
  9.  * memory address.
  10.  */
  11.  
  12. typedef enum {
  13.     OP_HALT = 0,
  14.     OP_ADD,         // Arg1 = REG, Arg2 = word
  15.     OP_LOAD,        // Arg1 = REG, Arg2 = word
  16.     OP_LOADW,       // Arg1 = REG, Arg2 = <unused>, INS+1 = word
  17.     OP_COPY,        // Arg1 = REG[dst], Arg2 = REG[src]
  18.     OP_CNDPC,       // Arg1 = word
  19.     OP_JMP,         // Arg1 = word
  20.     OP_JMPEQ,       // Arg1 = REG, Arg2 = word
  21.     OP_JMPEQW,      // Arg1 = REG, Arg2 = <unused>, INS+1 = word
  22.     OP_PRINT        // Arg1 = REG, Arg2 = <unused>
  23. } opcode;
  24.  
  25. typedef enum { REG_0 = 0, REG_1, REG_2, REG_3} reg;
  26.  
  27. #define INS(op, arg1, arg2) (((op) << 8) | ((arg1) << 4) | (arg2))
  28. #define OP(ins)             (((ins) & 0xF00) >> 8)
  29. #define ARG1(ins)           (((ins) & 0xF0) >> 4)
  30. #define ARG2(ins)           ((ins) & 0xF)
  31.  
  32. void native()   {
  33.     int i = 0;
  34.    
  35.     for (i = 0; i < 1000000; i++)   {
  36.         i = i + i;
  37.     }
  38.    
  39.     fprintf(stdout, "i=%d\n", i);
  40.    
  41. }
  42.  
  43. void print_ins(int ins) {
  44.     fprintf(stdout, "INS=0x%x, OP=0x%x, ARG1=0x%x, ARG2=0x%x\n",
  45.         ins, OP(ins), ARG1(ins), ARG2(ins));
  46. }
  47.  
  48. void vm()   {
  49.     int regs[REG_3] = {};
  50.     int code[1024] = {};
  51.    
  52.     int pc, condpc;
  53.     int ins;
  54.    
  55.     code[0] = INS(OP_CNDPC, 7, 0);
  56.     code[1] = INS(OP_LOADW, REG_0, 0);
  57.     code[2] = 0;
  58.     code[3] = INS(OP_ADD, REG_0, 1);
  59.     code[4] = INS(OP_JMPEQW, REG_0, 0);
  60.     code[5] = 1000000;
  61.     code[6] = INS(OP_JMP, 3, 0);
  62.     code[7] = INS(OP_PRINT, REG_0, 0);
  63.     code[8] = INS(OP_HALT, 0, 0);
  64.      
  65.     pc = condpc = 0;
  66.    
  67.     while(1)    {
  68.         ins = code[pc];
  69.        
  70. //      fprintf(stdout, "PC = 0x%x, ", pc);
  71. //      print_ins(ins);
  72.        
  73.         switch (OP(ins))    {
  74.         case OP_HALT:   return;
  75.         case OP_LOAD:
  76.             regs[ARG1(ins)] = ARG2(ins);
  77.             break;
  78.         case OP_LOADW:
  79.             regs[ARG1(ins)] = code[++pc];
  80.             break;
  81.         case OP_ADD:
  82.             regs[ARG1(ins)] += ARG2(ins);
  83.             break;
  84.         case OP_CNDPC:
  85.             condpc = ARG1(ins);
  86.             break;
  87.         case OP_JMP:
  88.             pc = ARG1(ins);
  89.             continue;
  90.         case OP_JMPEQ:
  91.             if (regs[ARG1(ins)] == ARG2(ins))   {
  92.                 pc = condpc;
  93.                 continue;
  94.             }
  95.             break;
  96.         case OP_JMPEQW:
  97.             if (regs[ARG1(ins)] == code[++pc])  {
  98.                 pc = condpc;
  99.                 continue;
  100.             }
  101.             break;
  102.         case OP_PRINT:
  103.             fprintf(stdout, "%d\n", regs[ARG1(ins)]);
  104.             break;
  105.         default:
  106.             fprintf(stderr, "Unrecognized opcode at 0x%x\n", pc);
  107.             return;
  108.         }
  109.        
  110.         pc++;
  111.     }
  112.    
  113. }
  114.  
  115. int main(int argc, char** argv) {
  116.     int ins;
  117.     //native();
  118.     vm();
  119.     return 0;
  120. }
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement