<commit_before>
#include "execute.h"

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

#include "vm.h"
#include "mps.h"
#include "mpsavm.h"
#include "loader.h"


int start(char *file) {
    struct sections sec = {0};

    mps_res_t res;

    res = loadfile(file, &sec);
    if (res != MPS_RES_OK) printf("Couldn't load file");

    VM vm = {0};

    size_t arenasize = 32ul * 1024 * 1024;

    vm_init(&vm, arenasize);

    printf("------------SLOT--------------\n");

    while (1) {

        uint32_t pc = vm.pc;
        instr inst = sec.instr[pc];
        //printf("------------INSTURCTION --------------\n");
        //printf("\n");

        struct OpABC abc = *((struct OpABC *) &inst);
        struct OpAD  ad  = *((struct OpAD *) &inst);

        uint8_t op = abc.op;

        switch (op) {
            //------------------Constant Table Value Operation------------------
            case CSTR: {
                uint16_t d = ntohs(ad.d);
                printf("CSTR: %d %d -> %s\n", ad.a, d, sec.cstr[d]);


                //TODO REAL STRING HANDLING
                //set(&vm, ad.a,   (uint64_t)(void*)sec.cstr[d] );

                set(&vm, ad.a,   d );

                vm.pc++;
                break;
            }
            case CKEY: {
                uint16_t d = ntohs(ad.d);

                printf("CKEY: %d %d\n", ad.a, d);
                set(&vm, ad.a, (uint64_t)(void*)sec.cstr[d] );


                vm.pc++;
                break;
            }
            //case CINT: {
            //    uint16_t d = ntohs(ad.d);
            //    printf("CINT: %d %d const: %d\n", ad.a, d, sec->cint[d]);

            //    slots.data[base + ad.a] = sec->cint[d];
            //    break;
            //}
            //case CFLOAT: {
            //    int target_slot = ad.a;
            //    printf("sec.cfloat[ad.d]: %f\n", swap(sec.cfloat[0]) );

            //    printf("CFLOAT: %d %d\n", ad.a, ad.d);
            //    break;
            //}
            case CTYPE: {
                uint16_t d = ntohs(ad.d);
                printf("CTYPE: %d %d\n", ad.a, d);

                set(&vm,ad.a,to_type(d));

                vm.pc++;
                break;
            }
            //------------------Constant Value Operation------------------
            case CBOOL: {
                uint16_t d = ntohs(ad.d);
                printf("CBOOL: %d %d\n", ad.a, d);
                set(&vm,ad.a,to_bool(d));
                vm.pc++;
                break;
            }
            case CNIL : {
                printf("CNIL: %d\n", ad.a);
                set(&vm,ad.a,get_nil());
                vm.pc++;
                break;
            }
            case CSHORT: {
                int target_slot = ad.a;
                uint16_t d16 = ntohs(ad.d);
                uint64_t d = (uint64_t) d16;

                printf("CSHORT: %d %d\n", ad.a, d16);
                set(&vm,target_slot,to_small_int(d));

                vm.pc++;
                break;
            }
            //------------------Global Table Ops------------------
            case NSSET: {
                uint16_t d = ntohs(ad.d);
                printf("NSSET: %d %d\n", ad.a, d);

                add_symbol_table_pair(&vm, sec.cstr[d], get(&vm,ad.a));

                vm.pc++;
                break;
            }
            case NSGET: {
                uint16_t d = ntohs(ad.d);
                printf("NSGET: %d %d -> %s\n ", ad.a, d, sec.cstr[d]);

                set(&vm,ad.a, get_symbol_table(&vm, sec.cstr[d]));

                vm.pc++;
                break;
            }
            //------------------Variable Slots------------------
            case ADDVV: {
                int target_slot = abc.a;
                uint64_t bslot = get(&vm,abc.b);
                uint64_t cslot = get(&vm,abc.c);

                printf("ADDVV: %d %d %d\n", abc.a, abc.b, abc.c);

                if (is_small_int(bslot) && is_small_int(cslot))
                    set(&vm, target_slot, to_small_int(get_small_int(bslot) + get_small_int(cslot)));

                if (is_double(bslot) && is_double(cslot))
                    set(&vm, target_slot,to_double(get_double(bslot) + get_double(cslot)));

                if (is_double(bslot) && is_small_int(cslot))
                    set(&vm, target_slot,to_double(get_double(bslot) + get_small_int(cslot)));

                if (is_small_int(bslot) && is_double(cslot))
                    set(&vm, target_slot, to_double(get_double(cslot) + get_small_int(bslot)));

                vm.pc++;
                break;
            }
            case SUBVV: {
                int target_slot = abc.a;
                uint64_t bslot = get(&vm,abc.b);
                uint64_t cslot = get(&vm,abc.c);

                printf("SUBVV: %d %d %d\n", abc.a, abc.b, abc.c);

                if (is_small_int(bslot) && is_small_int(cslot) )
                    set(&vm,target_slot, to_small_int(get_small_int(bslot) - get_small_int(cslot)));

                if (is_double(bslot) && is_double(cslot))
                    set(&vm,target_slot, to_double(get_double(bslot) - get_double(cslot)));

                if (is_double(bslot) && is_small_int(cslot))
                    set(&vm,target_slot, to_double(get_double(bslot) - get_small_int(cslot)));

                if (is_small_int(bslot) && is_double(cslot))
                    set(&vm,target_slot, to_double(get_double(cslot) - get_small_int(bslot)));

                vm.pc++;
                break;
            }
            case MULVV: {
                int target_slot = abc.a;
                uint64_t bslot = get(&vm,abc.b);
                uint64_t cslot = get(&vm,abc.c);

                printf("MULVV: %d %d %d\n", abc.a, abc.b, abc.c);

                if (is_small_int(bslot) && is_small_int(cslot))
                    set(&vm,target_slot,to_small_int(get_small_int(bslot) * get_small_int(cslot)));

                if (is_double(bslot) && is_double(cslot))
                    set(&vm,target_slot,to_double(get_double(bslot) * get_double(cslot)));

                if (is_double(bslot) && is_small_int(cslot))
                    set(&vm,target_slot,to_double(get_double(bslot) * get_small_int(cslot)));

                if (is_small_int(bslot) && is_double(cslot))
                    set(&vm,target_slot,to_double(get_double(cslot) * get_small_int(bslot)));

                vm.pc++;
                break;
            }
            case MODVV: {

                int target_slot = abc.a;
                uint64_t bslot = get(&vm,abc.b);
                uint64_t cslot = get(&vm,abc.c);

                printf("MODVV: %d %d %d\n", abc.a, abc.b, abc.c);

                if (is_small_int(bslot) && is_small_int(cslot)) {
                    set(&vm,target_slot, to_small_int(get_small_int(bslot) % get_small_int(cslot)));
                } else {
                    printf("Type Error. Called Modulo with Float\n");
                    exit(1);
                }

                vm.pc++;
                break;
            }
            case DIVVV: {
                printf("DIVVV: %d %d %d\n", abc.a, abc.b, abc.c);

                int target_slot = abc.a;
                uint64_t bslot = get(&vm,abc.b);
                uint64_t cslot = get(&vm,abc.c);

                if (is_small_int(bslot) && is_small_int(cslot))
                    set(&vm,target_slot,to_small_int(get_small_int(bslot) / get_small_int(cslot)));

                if (is_double(bslot) && is_double(cslot))
                    set(&vm,target_slot,to_double(get_double(bslot) / get_double(cslot)));

                if (is_double(bslot) && is_small_int(cslot))
                    set(&vm,target_slot,to_double(get_double(bslot) / get_small_int(cslot)));

                if (is_small_int(bslot) && is_double(cslot))
                    set(&vm,target_slot,to_double(get_double(cslot) / get_small_int(bslot)));

                vm.pc++;
                break;
            }
            case ISEQ: {
                printf("ISEQ: %d %d %d\n", abc.a, abc.b, abc.c);
                int target_slot = abc.a;
                uint64_t bslot = get(&vm,abc.b);
                uint64_t cslot = get(&vm,abc.c);

                vm.pc++;

                if (is_small_int(bslot) && is_small_int(cslot)) {
                    set(&vm,target_slot, to_bool(get_small_int(bslot) == get_small_int(cslot)));
                    break;
                }
                if (is_double(bslot) && is_double(cslot)) {
                    set(&vm,target_slot, to_bool(get_double(bslot) == get_double(cslot)));
                    break;
                }

                printf("Type Error ISEQ can only called with all Ints or all Double");
                return 1;

            }
            //------------------Unary Ops------------------
            case MOV: {
                uint8_t target_slot = ad.a;
                uint16_t d = ntohs(ad.d);

                printf("MOV: %d %d\n",target_slot, d);

                move(&vm, target_slot, d);

                vm.pc++;
                break;
            }
            case NOT: {
                printf("NOT // not implmented\n");
                vm.pc++;
                break;
            }
            //------------------Jumps------------------
            case JUMP: {
                uint16_t d = ntohs(ad.d);
                printf("JUMP: %d\n",d);
                int16_t offset = (int16_t) d;

                uint32_t new_pc = vm.pc + offset;

                vm.pc = new_pc;
                break;
            }
            case JUMPF: {
                uint16_t d = ntohs(ad.d);
                int16_t offset = (int16_t) d;
                printf("JUMPF: %d %d\n",ad.a,offset);

                if(is_falsy( get(&vm,ad.a) )) {
                    vm.pc = vm.pc + offset;
                } else {
                    vm.pc++;
                }
                break;
            }
            case JUMPT: {
                uint16_t d = ntohs(ad.d);
                int16_t offset = (int16_t) d;
                printf("JUMPT: %d %d\n",ad.a,offset);

                if(is_truthy(get(&vm,ad.a))) {
                    vm.pc = vm.pc + offset;
                } else {
                    vm.pc++;
                }
                break;
            }
            //------------------Function Calls------------------
            case CALL: {
                uint8_t localbase = ad.a;
                uint16_t lit = ntohs(ad.d);

                printf("CALL %d %d", localbase, lit);

                set(&vm,localbase, to_small_int(vm.pc));

                uint64_t fn_slot = get(&vm,localbase+1);

                uint16_t func = 0;
                if(is_fnew(fn_slot)) {
                    printf(" -> is func\n");
                    func = get_fnew(fn_slot);
                } else if(is_builtin(fn_slot)) {
                    printf(" -> is builtin\n");
                    func = -1;
                } else {
                    print_slots(&vm.slots, vm.base);
                    printf("\nCALL ERROR NO FUNC\n");
                    exit(0);
                }

                Context old = get_context(&vm);
                push(&vm.stack, old);

                uint32_t newbase = vm.base + localbase;

                Context newContext = { .base_slot = newbase, .ip = func };
                set_context(&vm, &newContext);

                if(func == -1) {

                    builtin_fn f =  get_builtin(fn_slot);
                    //gif((void *)vm); //TODO BIO

                    for(int i = 2; i != (localbase + 10); i++) {
                        set(&vm, i, get_nil());
                    }

                    Context caller = pop(&vm.stack);
                    set_context(&vm, &caller);

                    vm.pc++;

                } else {
                    vm.pc = func;
                }

                break;
            }
            case RET: {
                uint8_t a = ad.a;
                printf("RET %d\n", a);

                uint32_t ret_addr = get_small_int( get(&vm,0) );

                move(&vm,0,a);

                vm.slots.size = (vm.base+a);

                Context caller = pop(&vm.stack);
                set_context(&vm, &caller);

                vm.pc = ret_addr + 1;
                break;<commit_msg>no compile<commit_after>
#include "execute.h"

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

#include "vm.h"
#include "mps.h"
#include "mpsavm.h"
#include "loader.h"


int start(char *file) {
    struct sections sec = {0};

    mps_res_t res;

    res = loadfile(file, &sec);
    if (res != MPS_RES_OK) printf("Couldn't load file");

    VM vm = {0};

    size_t arenasize = 32ul * 1024 * 1024;

    vm_init(&vm, arenasize);

    printf("------------SLOT--------------\n");

    while (1) {

        uint32_t pc = vm.pc;
        instr inst = sec.instr[pc];
        //printf("------------INSTURCTION --------------\n");
        //printf("\n");

        struct OpABC abc = *((struct OpABC *) &inst);
        struct OpAD  ad  = *((struct OpAD *) &inst);

        uint8_t op = abc.op;

        switch (op) {
            //------------------Constant Table Value Operation------------------
            case CSTR: {
                uint16_t d = ntohs(ad.d);
                printf("CSTR: %d %d -> %s\n", ad.a, d, sec.cstr[d]);


                //TODO REAL STRING HANDLING
                //set(&vm, ad.a,   (uint64_t)(void*)sec.cstr[d] );

                set(&vm, ad.a,   d );

                vm.pc++;
                break;
            }
            case CKEY: {
                uint16_t d = ntohs(ad.d);

                printf("CKEY: %d %d\n", ad.a, d);
                set(&vm, ad.a, (uint64_t)(void*)sec.cstr[d] );


                vm.pc++;
                break;
            }
            //case CINT: {
            //    uint16_t d = ntohs(ad.d);
            //    printf("CINT: %d %d const: %d\n", ad.a, d, sec->cint[d]);

            //    slots.data[base + ad.a] = sec->cint[d];
            //    break;
            //}
            //case CFLOAT: {
            //    int target_slot = ad.a;
            //    printf("sec.cfloat[ad.d]: %f\n", swap(sec.cfloat[0]) );

            //    printf("CFLOAT: %d %d\n", ad.a, ad.d);
            //    break;
            //}
            case CTYPE: {
                uint16_t d = ntohs(ad.d);
                printf("CTYPE: %d %d\n", ad.a, d);

                set(&vm,ad.a,to_type(d));

                vm.pc++;
                break;
            }
            //------------------Constant Value Operation------------------
            case CBOOL: {
                uint16_t d = ntohs(ad.d);
                printf("CBOOL: %d %d\n", ad.a, d);
                set(&vm,ad.a,to_bool(d));
                vm.pc++;
                break;
            }
            case CNIL : {
                printf("CNIL: %d\n", ad.a);
                set(&vm,ad.a,get_nil());
                vm.pc++;
                break;
            }
            case CSHORT: {
                int target_slot = ad.a;
                uint16_t d16 = ntohs(ad.d);
                uint64_t d = (uint64_t) d16;

                printf("CSHORT: %d %d\n", ad.a, d16);
                set(&vm,target_slot,to_small_int(d));

                vm.pc++;
                break;
            }
            //------------------Global Table Ops------------------
            case NSSET: {
                uint16_t d = ntohs(ad.d);
                printf("NSSET: %d %d\n", ad.a, d);

                add_symbol_table_pair(&vm, sec.cstr[d], get(&vm,ad.a));

                vm.pc++;
                break;
            }
            case NSGET: {
                uint16_t d = ntohs(ad.d);
                printf("NSGET: %d %d -> %s\n ", ad.a, d, sec.cstr[d]);

                set(&vm,ad.a, get_symbol_table(&vm, sec.cstr[d]));

                vm.pc++;
                break;
            }
            //------------------Variable Slots------------------
            case ADDVV: {
                int target_slot = abc.a;
                uint64_t bslot = get(&vm,abc.b);
                uint64_t cslot = get(&vm,abc.c);

                printf("ADDVV: %d %d %d\n", abc.a, abc.b, abc.c);

                if (is_small_int(bslot) && is_small_int(cslot))
                    set(&vm, target_slot, to_small_int(get_small_int(bslot) + get_small_int(cslot)));

                if (is_double(bslot) && is_double(cslot))
                    set(&vm, target_slot,to_double(get_double(bslot) + get_double(cslot)));

                if (is_double(bslot) && is_small_int(cslot))
                    set(&vm, target_slot,to_double(get_double(bslot) + get_small_int(cslot)));

                if (is_small_int(bslot) && is_double(cslot))
                    set(&vm, target_slot, to_double(get_double(cslot) + get_small_int(bslot)));

                vm.pc++;
                break;
            }
            case SUBVV: {
                int target_slot = abc.a;
                uint64_t bslot = get(&vm,abc.b);
                uint64_t cslot = get(&vm,abc.c);

                printf("SUBVV: %d %d %d\n", abc.a, abc.b, abc.c);

                if (is_small_int(bslot) && is_small_int(cslot) )
                    set(&vm,target_slot, to_small_int(get_small_int(bslot) - get_small_int(cslot)));

                if (is_double(bslot) && is_double(cslot))
                    set(&vm,target_slot, to_double(get_double(bslot) - get_double(cslot)));

                if (is_double(bslot) && is_small_int(cslot))
                    set(&vm,target_slot, to_double(get_double(bslot) - get_small_int(cslot)));

                if (is_small_int(bslot) && is_double(cslot))
                    set(&vm,target_slot, to_double(get_double(cslot) - get_small_int(bslot)));

                vm.pc++;
                break;
            }
            case MULVV: {
                int target_slot = abc.a;
                uint64_t bslot = get(&vm,abc.b);
                uint64_t cslot = get(&vm,abc.c);

                printf("MULVV: %d %d %d\n", abc.a, abc.b, abc.c);

                if (is_small_int(bslot) && is_small_int(cslot))
                    set(&vm,target_slot,to_small_int(get_small_int(bslot) * get_small_int(cslot)));

                if (is_double(bslot) && is_double(cslot))
                    set(&vm,target_slot,to_double(get_double(bslot) * get_double(cslot)));

                if (is_double(bslot) && is_small_int(cslot))
                    set(&vm,target_slot,to_double(get_double(bslot) * get_small_int(cslot)));

                if (is_small_int(bslot) && is_double(cslot))
                    set(&vm,target_slot,to_double(get_double(cslot) * get_small_int(bslot)));

                vm.pc++;
                break;
            }
            case MODVV: {

                int target_slot = abc.a;
                uint64_t bslot = get(&vm,abc.b);
                uint64_t cslot = get(&vm,abc.c);

                printf("MODVV: %d %d %d\n", abc.a, abc.b, abc.c);

                if (is_small_int(bslot) && is_small_int(cslot)) {
                    set(&vm,target_slot, to_small_int(get_small_int(bslot) % get_small_int(cslot)));
                } else {
                    printf("Type Error. Called Modulo with Float\n");
                    exit(1);
                }

                vm.pc++;
                break;
            }
            case DIVVV: {
                printf("DIVVV: %d %d %d\n", abc.a, abc.b, abc.c);

                int target_slot = abc.a;
                uint64_t bslot = get(&vm,abc.b);
                uint64_t cslot = get(&vm,abc.c);

                if (is_small_int(bslot) && is_small_int(cslot))
                    set(&vm,target_slot,to_small_int(get_small_int(bslot) / get_small_int(cslot)));

                if (is_double(bslot) && is_double(cslot))
                    set(&vm,target_slot,to_double(get_double(bslot) / get_double(cslot)));

                if (is_double(bslot) && is_small_int(cslot))
                    set(&vm,target_slot,to_double(get_double(bslot) / get_small_int(cslot)));

                if (is_small_int(bslot) && is_double(cslot))
                    set(&vm,target_slot,to_double(get_double(cslot) / get_small_int(bslot)));

                vm.pc++;
                break;
            }
            case ISEQ: {
                printf("ISEQ: %d %d %d\n", abc.a, abc.b, abc.c);
                int target_slot = abc.a;
                uint64_t bslot = get(&vm,abc.b);
                uint64_t cslot = get(&vm,abc.c);

                vm.pc++;

                if (is_small_int(bslot) && is_small_int(cslot)) {
                    set(&vm,target_slot, to_bool(get_small_int(bslot) == get_small_int(cslot)));
                    break;
                }
                if (is_double(bslot) && is_double(cslot)) {
                    set(&vm,target_slot, to_bool(get_double(bslot) == get_double(cslot)));
                    break;
                }

                printf("Type Error ISEQ can only called with all Ints or all Double");
                return 1;

            }
            //------------------Unary Ops------------------
            case MOV: {
                uint8_t target_slot = ad.a;
                uint16_t d = ntohs(ad.d);

                printf("MOV: %d %d\n",target_slot, d);

                move(&vm, target_slot, d);

                vm.pc++;
                break;
            }
            case NOT: {
                printf("NOT // not implmented\n");
                vm.pc++;
                break;
            }
            //------------------Jumps------------------
            case JUMP: {
                uint16_t d = ntohs(ad.d);
                printf("JUMP: %d\n",d);
                int16_t offset = (int16_t) d;

                uint32_t new_pc = vm.pc + offset;

                vm.pc = new_pc;
                break;
            }
            case JUMPF: {
                uint16_t d = ntohs(ad.d);
                int16_t offset = (int16_t) d;
                printf("JUMPF: %d %d\n",ad.a,offset);

                if(is_falsy( get(&vm,ad.a) )) {
                    vm.pc = vm.pc + offset;
                } else {
                    vm.pc++;
                }
                break;
            }
            case JUMPT: {
                uint16_t d = ntohs(ad.d);
                int16_t offset = (int16_t) d;
                printf("JUMPT: %d %d\n",ad.a,offset);

                if(is_truthy(get(&vm,ad.a))) {
                    vm.pc = vm.pc + offset;
                } else {
                    vm.pc++;
                }
                break;
            }
            //------------------Function Calls------------------
            case CALL: {
                uint8_t localbase = ad.a;
                uint16_t lit = ntohs(ad.d);

                printf("CALL %d %d", localbase, lit);

                set(&vm,localbase, to_small_int(vm.pc));

                uint64_t fn_slot = get(&vm,localbase+1);

                uint16_t func = 0;
                if(is_fnew(fn_slot)) {
                    printf(" -> is func\n");
                    func = get_fnew(fn_slot);
                } else if(is_builtin(fn_slot)) {
                    printf(" -> is builtin\n");
                    func = -1;
                } else {
                    print_slots(&vm.slots, vm.base);
                    printf("\nCALL ERROR NO FUNC\n");
                    exit(0);
                }

                Context old = get_context(&vm);
                push(&vm.stack, old);

                uint32_t newbase = vm.base + localbase;

                Context newContext = { .base_slot = newbase, .ip = func };
                set_context(&vm, &newContext);

                if(func == -1) {

                    builtin_fn f =  get_builtin(fn_slot);
                    if((void *)vm); //TODO BIO

                    for(int i = 2; i != (localbase + 10); i++) {
                        set(&vm, i, get_nil());
                    }

                    Context caller = pop(&vm.stack);
                    set_context(&vm, &caller);

                    vm.pc++;

                } else {
                    vm.pc = func;
                }

                break;
            }
            case RET: {
                uint8_t a = ad.a;
                printf("RET %d\n", a);

                uint32_t ret_addr = get_small_int( get(&vm,0) );

                move(&vm,0,a);

                vm.slots.size = (vm.base+a);

                Context caller = pop(&vm.stack);
                set_context(&vm, &caller);

                vm.pc = ret_addr + 1;
                break;
{'fraction_non_alphanumeric': 0.15832656376929324, 'fraction_numerical': 0.00966693744922827, 'mean_word_length': 1.1152061855670103, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 21, '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': '4364367', 'n_tokens_mistral': 8319, 'n_tokens_neox': 7617, 'n_words': 1382}