RPA Toolkit
Work on static strings.
authorMartin Stoilov <martin@rpasearch.com>
Fri, 27 May 2011 04:44:14 +0000 (21:44 -0700)
committerMartin Stoilov <martin@rpasearch.com>
Fri, 27 May 2011 04:44:14 +0000 (21:44 -0700)
rjs/rjs.c
rjs/rjscompiler.c
rjs/rjscompiler.h
rvm/rvmcodegen.c
rvm/rvmcodemap.c
rvm/rvmcodemap.h
rvm/rvmoperator.c

index 8aa5e30..1c3e44a 100644 (file)
--- a/rjs/rjs.c
+++ b/rjs/rjs.c
@@ -220,6 +220,8 @@ static void rjs_engine_print(rvmcpu_t *cpu, rvm_asmins_t *ins)
                r_printf("%f\n", RVM_REG_GETD(r));
        else if (rvm_reg_gettype(r) == RVM_DTYPE_STRING)
                r_printf("%s\n", ((rstring_t*) RVM_REG_GETP(r))->s.str);
+       else if (rvm_reg_gettype(r) == RVM_DTYPE_STRPTR)
+               r_printf("(STRPTR) %s\n", (RVM_REG_GETSTR(r)));
        else if (rvm_reg_gettype(r) == RVM_DTYPE_JSOBJECT)
                r_printf("(object) %p\n",RVM_REG_GETP(r));
        else if (rvm_reg_gettype(r) == RVM_DTYPE_HARRAY)
@@ -251,6 +253,8 @@ static void rjs_engine_dbgprint(rvmcpu_t *cpu, rvm_asmins_t *ins)
                r_printf("(DOUBLE) %f\n", RVM_REG_GETD(r));
        else if (rvm_reg_gettype(r) == RVM_DTYPE_STRING)
                r_printf("(STRING) %s\n", ((rstring_t*) RVM_REG_GETP(r))->s.str);
+       else if (rvm_reg_gettype(r) == RVM_DTYPE_STRPTR)
+               r_printf("(STRPTR) %s\n", (RVM_REG_GETSTR(r)));
        else if (rvm_reg_gettype(r) == RVM_DTYPE_JSOBJECT)
                r_printf("(object) %p\n",RVM_REG_GETP(r));
        else if (rvm_reg_gettype(r) == RVM_DTYPE_HARRAY)
index 4cd456d..8e5d65b 100644 (file)
@@ -111,18 +111,21 @@ static rlong rjs_compiler_record2identifer(rjs_compiler_t *co, rarray_t *records
                return -1;
        }
 
-       if (ctx->type == RJS_COCTX_FUNCTION) {
+       if ( ctx->type == RJS_COCTX_FUNCTION ) {
                rjs_coctx_function_t *functx = (rjs_coctx_function_t *)ctx;
-               functx->allocs += 1;
-               rvm_scope_addoffset(co->scope, prec->input, prec->inputsiz, functx->allocs);
+               functx->stackallocs += 1;
+               rvm_scope_addoffset(co->scope, prec->input, prec->inputsiz, functx->stackallocs);
        } else {
-//             rjs_coctx_global_t *globalctx = (rjs_coctx_global_t *)ctx;
-//             r_carray_setlength(co->cpu->data, globalctx->allocs + 1);
-//             rvm_scope_addpointer(co->scope, prec->input, prec->inputsiz, r_carray_slot(co->cpu->data, globalctx->allocs));
-
-//             rvm_scope_addpointer(co->scope, prec->input, prec->inputsiz, r_carray_slot_expand(co->cpu->data, r_carray_length(co->cpu->data)));
-//             r_carray_setlength(co->cpu->data, r_carray_length(co->cpu->data) + 1);
-               rvm_scope_addpointer(co->scope, prec->input, prec->inputsiz, rvm_cpu_alloc_global(co->cpu));
+               if (rvm_scope_count(co->scope)) {
+                       rjs_coctx_global_t *globalctx = (rjs_coctx_global_t *)ctx;
+                       globalctx->stackallocs += 1;
+                       rvm_scope_addoffset(co->scope, prec->input, prec->inputsiz, globalctx->stackallocs);
+               } else {
+                       /*
+                        * Global Data
+                        */
+                       rvm_scope_addpointer(co->scope, prec->input, prec->inputsiz, rvm_cpu_alloc_global(co->cpu));
+               }
        }
        v = rvm_scope_tiplookup(co->scope, prec->input, prec->inputsiz);
        if (v->datatype == VARMAP_DATATYPE_OFFSET) {
@@ -266,6 +269,7 @@ rint rjs_compiler_rh_program(rjs_compiler_t *co, rarray_t *records, rlong rec)
        rjs_coctx_global_t ctx;
        rparecord_t *prec;
        rlong mainidx = rvm_codegen_invalid_add_numlabel_s(co->cg, "__main", 0);
+       rlong allocsidx = rvm_codegen_invalid_add_numlabel_s(co->cg, "__allocs", 0);
        rlong start;
 
        r_memset(&ctx, 0, sizeof(ctx));
@@ -276,6 +280,7 @@ rint rjs_compiler_rh_program(rjs_compiler_t *co, rarray_t *records, rlong rec)
        rjs_compiler_debughead(co, records, rec);
        rvm_codegen_index_addrelocins(co->cg, RVM_RELOC_BRANCH, mainidx, rvm_asm(RVM_B, DA, XX, XX, 0));
        start = rvm_codegen_addins(co->cg, rvm_asm(RVM_NOP, XX, XX, XX, 0));
+       rvm_codegen_index_addrelocins(co->cg, RVM_RELOC_DEFAULT, allocsidx, rvm_asm(RVM_ADD, SP, FP, DA, 0));
        rjs_compiler_debugtail(co, records, rec);
 
        if (rjs_compiler_playchildrecords(co, records, rec) < 0)
@@ -283,6 +288,7 @@ rint rjs_compiler_rh_program(rjs_compiler_t *co, rarray_t *records, rlong rec)
 
        rec = rpa_recordtree_get(records, rec, RPA_RECORD_END);
        prec = (rparecord_t *)r_array_slot(records, rec);
+       rvm_codegen_redefinepointer(co->cg, allocsidx, (rpointer)ctx.stackallocs);
        rjs_compiler_debughead(co, records, rec);
        rvm_codegen_addins(co->cg, rvm_asm(RVM_BX, LR, XX, XX, 0));
        rvm_codegen_redefinelabel_default(co->cg, mainidx);
@@ -532,6 +538,8 @@ rint rjs_compiler_rh_stringliteral(rjs_compiler_t *co, rarray_t *records, rlong
        prec = (rparecord_t *)r_array_slot(records, rec);
        rvmreg_t *strreg;
        rstring_t *s;
+       rlong stringidx;
+       rchar temp[256];
 
        rjs_compiler_debughead(co, records, rec);
        co->stringcharacters.str = NULL;
@@ -544,11 +552,19 @@ rint rjs_compiler_rh_stringliteral(rjs_compiler_t *co, rarray_t *records, rlong
        rec = rpa_recordtree_get(records, rec, RPA_RECORD_END);
        prec = (rparecord_t *)r_array_slot(records, rec);
        rjs_compiler_debughead(co, records, rec);
+
+/*
+       memset(temp, 0, sizeof(temp));
+       r_snprintf(temp, sizeof(temp), "__%ld__stringliteral", rvm_codegen_getcodesize(co->cg));
+       stringidx = rvm_codegen_adddata_s(co->cg, temp, "hello", 4);
+       rvm_codegen_index_addrelocins(co->cg, RVM_RELOC_STRING, stringidx, rvm_asma(RVM_MOV, R0, DA, XX, 0, co->stringcharacters.size));
+*/
        strreg = rvm_cpu_alloc_global(co->cpu);
        s = r_string_create_from_rstr(&co->stringcharacters);
        rvm_gc_add(co->cpu->gc, (robject_t*)s);
        rvm_reg_setstring(strreg, s);
        rvm_codegen_addins(co->cg, rvm_asmp(RVM_LDRR, R0, DA, XX, strreg));
+
        rjs_compiler_debugtail(co, records, rec);
        return 0;
 }
@@ -760,7 +776,7 @@ rint rjs_compiler_rh_functiondeclaration(rjs_compiler_t *co, rarray_t *records,
        rvm_codegen_addins(co->cg, rvm_asm(RVM_BX, LR, XX, XX, 0));
        rvm_codegen_redefinelabel_default(co->cg, endidx);
        rvm_codegen_addins(co->cg, rvm_asm(RVM_NOP, XX, XX, XX, 0xffffffff));
-       rvm_codegen_redefinepointer(co->cg, allocsidx, (rpointer)ctx.allocs);
+       rvm_codegen_redefinepointer(co->cg, allocsidx, (rpointer)ctx.stackallocs);
        rjs_compiler_debugtail(co, records, rec);
 
        co->cg->userdata = RJS_COMPILER_CODEGENKEEP;
@@ -812,8 +828,8 @@ rint rjs_compiler_rh_functionparameter(rjs_compiler_t *co, rarray_t *records, rl
                return -1;
        }
 
-       functx->allocs += 1;
-       rvm_scope_addoffset(co->scope, prec->input, prec->inputsiz, functx->allocs);
+       functx->stackallocs += 1;
+       rvm_scope_addoffset(co->scope, prec->input, prec->inputsiz, functx->stackallocs);
 
        rjs_compiler_debugtail(co, records, rec);
        return 0;
index ae12dae..49252cd 100644 (file)
@@ -34,6 +34,7 @@ typedef struct rjs_coctx_s {
 
 typedef struct rjs_coctx_global_s {
        rjs_coctx_t base;
+       rsize_t stackallocs;
 } rjs_coctx_global_t;
 
 
@@ -44,7 +45,7 @@ typedef struct rjs_coctx_function_s {
        rlong endidx;
        rlong allocsidx;
        rlong execoff;
-       rsize_t allocs;
+       rsize_t stackallocs;
 } rjs_coctx_function_t;
 
 
index 7db07d2..b8e3e71 100644 (file)
@@ -88,6 +88,10 @@ rlong rvm_codegen_redefinelabel(rvm_codegen_t *cg, rlong index, rulong offset)
        if (!label)
                return -1;
        return rvm_codemap_addoffset(cg->codemap, label->name->str, label->name->size, rvm_codemap_lookupadd_s(cg->codemap, ".code"), RVM_CODE2BYTE_OFFSET(offset));
+
+//  This doesn't work
+//     rvm_codelabel_setoffset(label, rvm_codemap_lookupadd_s(cg->codemap, ".code"), RVM_CODE2BYTE_OFFSET(offset));
+//     return index;
 }
 
 
@@ -110,6 +114,10 @@ rlong rvm_codegen_redefinepointer(rvm_codegen_t *cg, rlong index, rpointer data)
        if (!label)
                return -1;
        return rvm_codemap_addpointer(cg->codemap, label->name->str, label->name->size, data);
+
+// This doesn't work
+//     rvm_codelabel_setpointer(label, data);
+//     return index;
 }
 
 
@@ -250,7 +258,7 @@ rlong rvm_codegen_adddata(rvm_codegen_t *cg, const rchar *name, ruint namesize,
        rpointer buffer;
        rulong cursize = R_SIZE_ALIGN(r_array_length(cg->data), sizeof(rword));
 
-       r_array_setlength(cg->data, cursize + size + sizeof(rword));
+       r_array_setlength(cg->data, cursize + size + sizeof(rword) + 1);
        buffer = r_array_slot(cg->data, cursize);
        r_memset(buffer, 0, size + sizeof(rword));
        r_memmove(buffer, data, size);
index a6c391f..feff70d 100644 (file)
@@ -69,7 +69,6 @@ static rlong rvm_codemap_add(rvm_codemap_t *codemap, const rchar *name, ruint na
 
        labelidx = rvm_codemap_dolookup(codemap, name, namesize);
        if (labelidx < 0) {
-               r_memset(&label, 0, sizeof(label));
                labelidx = r_array_add(codemap->labels, NULL);
                label = rvm_codemap_label(codemap, labelidx);
                if (name) {
@@ -81,17 +80,36 @@ static rlong rvm_codemap_add(rvm_codemap_t *codemap, const rchar *name, ruint na
 }
 
 
+void rvm_codelabel_setoffset(rvm_codelabel_t *label, rulong base, rulong offset)
+{
+       label->base = base;
+       label->value = offset;
+       label->type = RVM_CODELABEL_OFFSET;
+}
+
+
+void rvm_codelabel_setpointer(rvm_codelabel_t *label, rpointer ptr)
+{
+       label->base = 0;
+       label->value = (rword)ptr;
+       label->type = RVM_CODELABEL_POINTER;
+}
+
+
+void rvm_codelabel_setinvalid(rvm_codelabel_t *label)
+{
+       label->base = 0;
+       label->value = 0;
+       label->type = RVM_CODELABEL_INVALID;
+}
+
 
 rlong rvm_codemap_addoffset(rvm_codemap_t *codemap, const rchar *name, ruint namesize, rulong base, rulong offset)
 {
        rlong labelidx = rvm_codemap_add(codemap, name, namesize);
        rvm_codelabel_t *label = rvm_codemap_label(codemap, labelidx);
-
-       if (label) {
-               label->base = base;
-               label->value = offset;
-               label->type = RVM_CODELABEL_OFFSET;
-       }
+       if (label)
+               rvm_codelabel_setoffset(label, base, offset);
        return labelidx;
 }
 
@@ -108,8 +126,7 @@ rlong rvm_codemap_addpointer(rvm_codemap_t *codemap, const rchar *name, ruint na
        rvm_codelabel_t *label = rvm_codemap_label(codemap, labelidx);
 
        if (label) {
-               label->value = (rword)ptr;
-               label->type = RVM_CODELABEL_POINTER;
+               rvm_codelabel_setpointer(label, ptr);
        }
        return labelidx;
 }
@@ -127,7 +144,7 @@ rlong rvm_codemap_invalid_add(rvm_codemap_t *codemap, const rchar *name, ruint n
        rvm_codelabel_t *label = rvm_codemap_label(codemap, labelidx);
 
        if (label) {
-               label->type = RVM_CODELABEL_INVALID;
+               rvm_codelabel_setinvalid(label);
        }
        return labelidx;
 }
index 9e4463d..d54c4ed 100644 (file)
@@ -49,6 +49,9 @@ rvm_codelabel_t *rvm_codemap_label(rvm_codemap_t *codemap, rlong index);
 rlong rvm_codemap_validindex(rvm_codemap_t *codemap, rlong labelidx);
 rword rvm_codemap_resolve(rvm_codemap_t *codemap, rlong index, rvm_codelabel_t **err);
 void rvm_codemap_dump(rvm_codemap_t *codemap);
+void rvm_codelabel_setoffset(rvm_codelabel_t *label, rulong base, rulong offset);
+void rvm_codelabel_setpointer(rvm_codelabel_t *label, rpointer ptr);
+void rvm_codelabel_setinvalid(rvm_codelabel_t *label);
 
 #ifdef __cplusplus
 }
index 42972ea..b0174dd 100644 (file)
@@ -85,8 +85,34 @@ void rvm_opmap_invoke_binary_handler(rvm_opmap_t *opmap, rushort opid, rvmcpu_t
 {
        rvm_ophandler_t *h;
        rvm_opinfo_t *opinfo;
-       ruint index = RVM_OP_HANDLER(RVM_REG_GETTYPE(arg1), RVM_REG_GETTYPE(arg2));
+       ruint index;
+       rstring_t tempstr1, tempstr2;
+       rvmreg_t temparg1, temparg2;
+
+       /*
+        * if any of the arg1 or arg2 are of type RVM_DTYPE_STRPTR (static string) We need to convert them to
+        * rstring_t;
+        */
+       if (RVM_REG_GETTYPE(arg1) == RVM_DTYPE_STRPTR) {
+               r_memset(&tempstr1, 0, sizeof(tempstr1));
+               tempstr1.s.str = RVM_REG_GETSTR(arg1);
+               tempstr1.s.size = RVM_REG_GETSIZE(arg1);
+               RVM_REG_SETP(&temparg1, &tempstr1);
+               RVM_REG_SETTYPE(&temparg1, RVM_DTYPE_STRING);
+               arg1 = &temparg1;
+       }
+
+       if (RVM_REG_GETTYPE(arg2) == RVM_DTYPE_STRPTR) {
+               r_memset(&tempstr2, 0, sizeof(tempstr2));
+               tempstr2.s.str = RVM_REG_GETSTR(arg2);
+               tempstr2.s.size = RVM_REG_GETSIZE(arg2);
+               RVM_REG_SETP(&temparg2, &tempstr2);
+               RVM_REG_SETTYPE(&temparg2, RVM_DTYPE_STRING);
+               arg2 = &temparg2;
+       }
 
+
+       index = RVM_OP_HANDLER(RVM_REG_GETTYPE(arg1), RVM_REG_GETTYPE(arg2));
        if (opid >= r_array_length(opmap->operators))
                goto error;
        opinfo = ((rvm_opinfo_t*)r_array_slot(opmap->operators, opid));
@@ -105,8 +131,20 @@ void rvm_opmap_invoke_unary_handler(rvm_opmap_t *opmap, rushort opid, rvmcpu_t *
 {
        rvm_ophandler_t *h;
        rvm_opinfo_t *opinfo;
-       ruint index = RVM_UNARY_HANDLER(RVM_REG_GETTYPE(arg));
+       ruint index;
+       rstring_t tempstr;
+       rvmreg_t temparg;
+
+       if (RVM_REG_GETTYPE(arg) == RVM_DTYPE_STRPTR) {
+               r_memset(&tempstr, 0, sizeof(tempstr));
+               tempstr.s.str = RVM_REG_GETSTR(arg);
+               tempstr.s.size = RVM_REG_GETSIZE(arg);
+               RVM_REG_SETP(&temparg, &tempstr);
+               RVM_REG_SETTYPE(&temparg, RVM_DTYPE_STRING);
+               arg = &temparg;
+       }
 
+       index = RVM_UNARY_HANDLER(RVM_REG_GETTYPE(arg));
        if (opid >= r_array_length(opmap->operators))
                goto error;
        opinfo = ((rvm_opinfo_t*)r_array_slot(opmap->operators, opid));