Example #1
0
static inline cpu_sub(Cpu *cpu){
        sign_reg a = s_stack_pop(&cpu->mem);
        sign_reg b = s_stack_pop(&cpu->mem);
        if (s_errno == EMPTY){
                p_errno = STACK;
        }
        else {
                s_stack_push(&cpu->mem, b - a);
	}
}
Example #2
0
static inline cpu_mul(Cpu *cpu){
        int a = s_stack_pop(&cpu->mem);
        int b = s_stack_pop(&cpu->mem);
	int result = a * b;
	sign_reg higher_order = result >> 16;
	sign_reg lower_order = result & 0xffff;
	printf("Result %d, higher %d, lower %d\n", result, higher_order, lower_order);
        if (s_errno == EMPTY){
                p_errno = STACK;
        }
        else {
                s_stack_push(&cpu->mem, higher_order);
		s_stack_push(&cpu->mem, lower_order);
	}
 }
Example #3
0
static inline cpu_div(Cpu *cpu){
        sign_reg a = s_stack_pop(&cpu->mem);
        sign_reg b = s_stack_pop(&cpu->mem);
        if (s_errno == EMPTY){
                p_errno = STACK;
        }
        else if (a != 0){
		sign_reg quotient = b / a;
		sign_reg remainder = b % a;
		s_stack_push(&cpu->mem, remainder);
		s_stack_push(&cpu->mem, quotient);
	}
	else {
		p_errno = DIV_BY_0;		
	}
}
Example #4
0
static inline cpu_pop(Cpu *cpu, int reg_num){
	reg tmp = s_stack_pop(&(cpu->mem));
	if (s_errno == EMPTY){
		p_errno = STACK;
	}
	else {
		cpu->r[reg_num] = tmp;
	}
}
Example #5
0
static inline cpu_triz(Cpu *cpu, int destination){
        sign_reg a = s_stack_pop(&cpu->mem);
        if (s_errno == EMPTY){
                p_errno = STACK;
        }
        else if (a == 0){
                NPC = destination;
        }
	else {
		NPC_INC(2);
	}
}
Example #6
0
int main() {
    sTableInit();
    sStackInit();
    // generate entries
    SymbolTableEntry* entry, *entry2, *result, *result2;
    Lexeme ll = "abc";
    int tt = 1;
    printf("%d\n", s_stack->top);
    fflush(stdout);
    entry = sNewEntry ( ll, tt, sStackLevel, sStackTopId );
    entry2 = sNewEntry ( "xyz", 2, 1, 1 );
    printf("key=%s\nbind=%s\nSlevel=%d\nScopeId=%d\n", entry->key, entry->bind,entry->scope[0],entry->scope[1]);
    printf("key=%s\nbind=%s\n", entry2->key, entry2->bind);
    // insert to s_table
    sTableInsert( entry);
    // lookup
    SymbolTableKey key;
    sNewKey(entry->lex, entry->scope[1], key);
    result = s_table_lookup(s_table, key);
    // check
    printf("key=%s\nbind=%s\n", result->key, result->bind);
    // remove
    bool istat = s_table_remove(s_table, entry);
    if (istat) printf("remove done\n");
    else printf("remove false\n");
    // lookup 2 , should fail
    result2 = s_table_lookup(s_table, key);
    printf("Should be NULL %p\n", result2);
    // cleanup
    sTableDestory();

    // test stack
    ScopeId ttt;
    s_stack_push(s_stack, 1);
    s_stack_push(s_stack, 2);
    s_stack_push(s_stack, 3);
    s_stack_push(s_stack, 4);
    printf("top=%d, present=%d\n", s_stack->top, s_stack->present);
    ttt = s_stack_pop(s_stack);
    printf("pop:%d\n",ttt);
    //
    printf("top=%d, present=%d\n", s_stack->top, s_stack->present);
    //
    s_stack_reset(s_stack);
    while(s_stack->present>=0) {
        printf("down:%d\n",s_stack_down(s_stack));
    }
    s_stack_reset(s_stack);
    printf("top=%d, present=%d\n", s_stack->top, s_stack->present);
    //
    sStackDestroy();
}
Example #7
0
static inline cpu_out(Cpu *cpu){
	sign_reg tmp = s_stack_pop(&cpu->mem);
	printf("\n%d", tmp);
}