int main () { carp_value val = 127; carp_value code[] = {CARP_I(LOADR), CARP_R0, val, CARP_I(MOV), CARP_R1, CARP_R0, CARP_I(MOV), CARP_R2, CARP_R0, CARP_I(INCR), CARP_R2, CARP_I(MOV), CARP_R3, CARP_R0, CARP_I(DECR), CARP_R3, CARP_I(HALT), EXIT_SUCCESS}; carp_vm_init(&m, 1, 0); carp_vm_load(&m, code, sizeof(code)/sizeof(*code)); carp_vm_run(&m); carp_vm_cleanup(&m); plan(NO_PLAN); ok(m.regs[CARP_R0] == val, "Set register successfully."); ok(m.regs[CARP_R1] == val, "Moved register successfully."); ok(m.regs[CARP_R2] == val + 1, "Incremented register successfully."); ok(m.regs[CARP_R3] == val - 1, "Decremented register successfully."); done_testing(); }
int main (int argc, char **argv) { carp_value code[] = { CARP_INSTR_PUSH, 5, CARP_INSTR_PUSH, 6, CARP_INSTR_PREG, CARP_SP, CARP_INSTR_DECR, CARP_SP, CARP_INSTR_POP, CARP_GBG, CARP_INSTR_PREG, CARP_GBG, CARP_INSTR_HALT, 1 }; carp_machine_state m; carp_vm_init(&m, 10, 0); carp_vm_load(&m, code, sizeof(code)/sizeof(*code)); carp_value ecode = carp_vm_run(&m); carp_vm_cleanup(&m); return ecode; }
int main (int argc, char **argv) { long long code[] = { CARP_INSTR_GLOAD, -5, // 1st arg CARP_INSTR_GLOAD, -4, // 2nd arg CARP_INSTR_ADD, CARP_INSTR_RET, CARP_INSTR_PUSH, 7, // 1st arg CARP_INSTR_PUSH, 9, // 2nd arg CARP_INSTR_CALL, 0, 2, CARP_INSTR_PTOP, CARP_INSTR_HALT, 0 }; carp_machine_state m; carp_vm_init(&m, 1, 6); carp_vm_load(&m, code, sizeof(code)); carp_vm_run(&m); }
/* Assigns values to a series of tokens. NUM is obvious. REG is obvious. LBL is NOP instr. FUNC is label lookup value. INSTR is obvious. */ void carp_lex_lex (carp_machine_state *m, carp_tok *tokens) { assert(m != NULL); assert(tokens != NULL); long long length = -1; carp_tok *tmp = tokens; carp_ht_init(&m->labels, 10); while (tmp != NULL) { switch (tmp->type) { case CARP_TOK_UNDEF: { fprintf(stderr, "Unknown token <%s>\n", tmp->lexeme); carp_lex_exit(tokens, &m->labels, EXIT_FAILURE); break; } case CARP_TOK_NUM: { long long num = atoi(tmp->lexeme); tmp->value = num; break; } case CARP_TOK_REG: { carp_reg reg = carp_reg_lookup(tmp->lexeme); tmp->value = reg; break; } case CARP_TOK_LBL: { carp_bool status = carp_ht_set(&m->labels, tmp->lexeme, tmp->pos); if (status != 0) { fprintf(stderr, "Could not make label <%s>\n", tmp->lexeme); carp_lex_exit(tokens, &m->labels, 1); } carp_value instr = CARP_INSTR_NOP; tmp->value = instr; break; } case CARP_TOK_FUNC: { carp_ht_entry *res = carp_ht_get(&m->labels, tmp->lexeme); if (res == NULL) { fprintf(stderr, "Unknown label <%s>\n", tmp->lexeme); carp_lex_exit(tokens, &m->labels, EXIT_FAILURE); } tmp->value = res->value; break; } case CARP_TOK_VAR: { break; } case CARP_TOK_INSTR: { carp_value instr = carp_instr_lookup(tmp->lexeme); tmp->value = instr; break; } } #ifdef CDEBUG printf("[%04ld] %5s (%5s) = %4ld\n", tmp->pos, tmp->lexeme, carp_reverse_type[tmp->type], tmp->value); #endif tmp = tmp->next; length++; } carp_value code[length]; tmp = tokens; while (tmp != NULL) { code[tmp->pos] = tmp->value; tmp = tmp->next; } carp_lex_cleanup(tokens); carp_vm_make(m); carp_vm_load(m, code, length); }