test_client.c 3.72 KB
Newer Older
Kunshan Wang's avatar
Kunshan Wang committed
1 2 3 4 5 6 7 8 9
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <unistd.h>     // write

#include <refimpl2-start.h>
#include <muapi.h>

10
const char *hw_string = "Hello world!\n";
Kunshan Wang's avatar
Kunshan Wang committed
11
const char *hw2_string = "Goodbye world!\n";
12 13

const char *gc_conf =
14
"vmLog=DEBUG\n"
15 16 17 18 19
"sosSize=524288\n"
"losSize=524288\n"
"globalSize=1048576\n"
"stackSize=32768\n"
;
Kunshan Wang's avatar
Kunshan Wang committed
20 21

int main() {
22
    printf("Creating micro VM...\n");
23
    MuVM *mvm = mu_refimpl2_new_ex(gc_conf);
Kunshan Wang's avatar
Kunshan Wang committed
24

25
    printf("Creating client context...\n");
Kunshan Wang's avatar
Kunshan Wang committed
26 27 28 29 30 31 32 33 34 35
    MuCtx *ctx = mvm->new_context(mvm);

    char *bundle1 = 
        ".typedef @cint = int<32>\n"
        ".typedef @void = void\n"
        ".typedef @cvoidptr = uptr<@void>\n"
        ".typedef @csize_t = int<64>\n"
        ".funcsig @write.sig = (@cint @cvoidptr @csize_t) -> (@csize_t)\n"
        ".typedef @write.fp  = ufuncptr<@write.sig>\n"
        ".const @the_fd <@cint> = 1\n"
36 37
        ".typedef @i64 = int<64>\n"
        ".const @print_stat_point <@i64> = 92\n"
Kunshan Wang's avatar
Kunshan Wang committed
38 39 40 41 42 43 44
        ;

    char bundle2[256];
    sprintf(bundle2,
            ".const @the_write  <@write.fp> = 0x%lx\n"
            ".const @the_string <@cvoidptr> = 0x%lx\n"
            ".const @the_length <@csize_t>  = 0x%lx\n"
Kunshan Wang's avatar
Kunshan Wang committed
45 46 47
            ".const @the_length2 <@csize_t>  = 0x%lx\n"
            , (uintptr_t)write, (uintptr_t)hw_string,
            (unsigned long)strlen(hw_string), (unsigned long)strlen(hw2_string));
Kunshan Wang's avatar
Kunshan Wang committed
48 49 50 51 52 53
        
    char *bundle3 = 
        ".funcsig @v_v = ()->()\n"
        ".funcdef @hw VERSION %1 <@v_v> {\n"
        "  %entry():\n"
        "    %rv = CCALL #DEFAULT <@write.fp @write.sig> @the_write (@the_fd @the_string @the_length)\n"
54
        "    COMMINST @uvm.ext.print_stats (@print_stat_point)\n"
Kunshan Wang's avatar
Kunshan Wang committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
        "    COMMINST @uvm.thread_exit\n"
        "}\n"
        ;

    printf("Loading bundles...\n");

    printf("Bundle1:\n%s\n", bundle1);
    ctx->load_bundle(ctx, bundle1, strlen(bundle1));
    printf("Bundle2:\n%s\n", bundle2);
    ctx->load_bundle(ctx, bundle2, strlen(bundle2));
    printf("Bundle3:\n%s\n", bundle3);
    ctx->load_bundle(ctx, bundle3, strlen(bundle3));

    printf("Bundles loaded. Execute...\n");

    MuFuncRefValue   func   = ctx->handle_from_func(ctx, ctx->id_of(ctx, "@hw"));
    MuStackRefValue  stack  = ctx->new_stack(ctx, func);
Kunshan Wang's avatar
Kunshan Wang committed
72
    MuThreadRefValue thread = ctx->new_thread_nor(ctx, stack, NULL, NULL, 0);
Kunshan Wang's avatar
Kunshan Wang committed
73 74 75

    mvm->execute(mvm);

Kunshan Wang's avatar
Kunshan Wang committed
76 77 78 79 80 81
    char *bundle4 = 
        ".typedef @cchar = int<8>\n"
        ".typedef @charhybrid = hybrid<@cchar>\n"
        ".typedef @refvoid = ref<@void>\n"
        ".funcdef @hw2 VERSION %1 <@v_v> {\n"
        "  %entry():\n"
82
        "    COMMINST @uvm.ext.clear_stats\n"
Kunshan Wang's avatar
Kunshan Wang committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
        "    %tl = COMMINST @uvm.get_threadlocal\n"
        "    %p  = COMMINST @uvm.native.pin <@refvoid> (%tl)\n"
        "    %rv = CCALL #DEFAULT <@write.fp @write.sig> @the_write (@the_fd %p @the_length2)\n"
        "    COMMINST @uvm.native.unpin <@refvoid> (%tl)\n"
        "    COMMINST @uvm.thread_exit\n"
        "}\n"
        ;

    printf("Loading additional bundle...\n");
    ctx->load_bundle(ctx, bundle4, strlen(bundle4));

    printf("Bundle loaded. Create a thread-local string object...\n");
    MuIntValue hlen = ctx->handle_from_sint32(ctx, 256, 32);
    MuRefValue hobj = ctx->new_hybrid(ctx, ctx->id_of(ctx, "@charhybrid"), hlen);
    MuUPtrValue hpobj = ctx->pin(ctx, hobj);
    char *mustrbuf = (char*)ctx->handle_to_ptr(ctx, hpobj);
    strcpy(mustrbuf, hw2_string);
    ctx->unpin(ctx, hobj);

    printf("Object populated. Create thread with threadlocal and execute...\n");
    MuFuncRefValue   func2   = ctx->handle_from_func(ctx, ctx->id_of(ctx, "@hw2"));
    MuStackRefValue  stack2  = ctx->new_stack(ctx, func2);
Kunshan Wang's avatar
Kunshan Wang committed
105
    MuThreadRefValue thread2 = ctx->new_thread_nor(ctx, stack2, hobj, NULL, 0);
Kunshan Wang's avatar
Kunshan Wang committed
106 107 108 109

    mvm->execute(mvm);


Kunshan Wang's avatar
Kunshan Wang committed
110 111 112 113
    mu_refimpl2_close(mvm);

    return 0;
}