RPA Toolkit
work on the rpagen test
authorMartin Stoilov <martin@rpasearch.com>
Wed, 17 Nov 2010 04:58:23 +0000 (20:58 -0800)
committerMartin Stoilov <martin@rpasearch.com>
Wed, 17 Nov 2010 04:58:23 +0000 (20:58 -0800)
rlib/rarray.c
rvm/rvmcodegen.c
rvm/rvmcodegen.h
rvm/rvmcpu.c
rvm/rvmscope.c
rvm/rvmscope.h
tests/rpagen-test.c
tests/scope-test.c

index 4139436..1e50ea4 100644 (file)
@@ -118,8 +118,9 @@ rint r_array_insert(rarray_t *array, ruint index, rconstpointer data)
 {
        r_array_checkexpand(array, index + 1);
        if (index < array->len) {
-               r_memmove(r_array_slot(array, index + 1), r_array_slot(array, index), (array->len - index) * array->elt_size);
-               array->len += 1;
+               ruint curlen = r_array_size(array);
+               r_array_setsize(array, array->len + 1);
+               r_memmove(r_array_slot(array, index + 1), r_array_slot(array, index), (curlen - index) * array->elt_size);
        } else {
                r_array_setsize(array, index + 1);
        }
index e45fb2d..a265320 100644 (file)
@@ -43,6 +43,12 @@ ruint rvm_codegen_addins(rvm_codegen_t *cg, rvm_asmins_t ins)
 }
 
 
+ruint rvm_codegen_insertins(rvm_codegen_t *cg, ruint index, rvm_asmins_t ins)
+{
+       return r_array_insert(cg->code, index, &ins);
+}
+
+
 ruint rvm_codegen_funcstart(rvm_codegen_t *cg, const rchar* name, ruint namesize, ruint args)
 {
        ruint start = rvm_codegen_addins(cg, rvm_asm(RVM_PUSHM, DA, XX, XX, BIT(R0)|BIT(FP)|BIT(SP)|BIT(LR)));
index d041172..2d03d13 100644 (file)
@@ -31,6 +31,7 @@ ruint rvm_codegen_vargs_funcstart(rvm_codegen_t *cg, const rchar* name, ruint na
 ruint rvm_codegen_vargs_funcstart_s(rvm_codegen_t *cg, const rchar* name);
 void rvm_codegen_funcend(rvm_codegen_t *cg);
 ruint rvm_codegen_addins(rvm_codegen_t *cg, rvm_asmins_t ins);
+ruint rvm_codegen_insertins(rvm_codegen_t *cg, ruint index, rvm_asmins_t ins);
 rvm_asmins_t *rvm_codegen_getcode(rvm_codegen_t *cg, ruint index);
 rulong rvm_codegen_getcodesize(rvm_codegen_t *cg);
 
index 3167724..07f617b 100644 (file)
@@ -783,7 +783,7 @@ static void rvm_op_prn(rvmcpu_t *cpu, rvm_asmins_t *ins)
        rvmreg_t *r = rvm_reg_unshadow(RVM_CPUREG_PTR(cpu, ins->op1));
 
        if (rvm_reg_gettype(r) == RVM_DTYPE_UNSIGNED)
-               rvm_printf("(UNSIGNED) R%d = %lu(%lx)\n", ins->op1, RVM_REG_GETU(r), RVM_REG_GETU(r));
+               rvm_printf("(UNSIGNED) R%d = %lu(0x%lx)\n", ins->op1, RVM_REG_GETU(r), RVM_REG_GETU(r));
        else if (rvm_reg_gettype(r) == RVM_DTYPE_LONG)
                rvm_printf("(LONG) R%d = %ld\n", ins->op1, RVM_REG_GETL(r));
        else if (rvm_reg_gettype(r) == RVM_DTYPE_DOUBLE)
index 0b79a7b..4431742 100644 (file)
@@ -111,7 +111,7 @@ rvm_varmap_t *rvm_scope_lookup(rvm_scope_t *scope, const rchar *name, ruint name
 }
 
 
-rvm_varmap_t *rvm_scope_lookup_tip(rvm_scope_t *scope, const rchar *name, ruint namesize)
+rvm_varmap_t *rvm_scope_tiplookup(rvm_scope_t *scope, const rchar *name, ruint namesize)
 {
        ruint scopesize = scope->varstack->len;
        ruint tipstart = r_array_empty(scope->scopestack) ? 0 : r_array_last(scope->scopestack, ruint);
@@ -133,7 +133,7 @@ rvm_varmap_t *rvm_scope_lookup_s(rvm_scope_t *scope, const rchar *name)
 }
 
 
-rvm_varmap_t *rvm_scope_lookuptip_s(rvm_scope_t *scope, const rchar *name)
+rvm_varmap_t *rvm_scope_tiplookup_s(rvm_scope_t *scope, const rchar *name)
 {
-       return rvm_scope_lookup_tip(scope, name, r_strlen(name));
+       return rvm_scope_tiplookup(scope, name, r_strlen(name));
 }
index 715ad2d..6107d12 100644 (file)
@@ -42,9 +42,9 @@ void rvm_scope_push(rvm_scope_t* scope);
 void rvm_scope_pop(rvm_scope_t* scope);
 ruint rvm_scope_numentries(rvm_scope_t *scope);
 rvm_varmap_t *rvm_scope_lookup(rvm_scope_t *scope, const rchar *name, ruint namesize);
-rvm_varmap_t *rvm_scope_lookup_tip(rvm_scope_t *scope, const rchar *name, ruint namesize);
 rvm_varmap_t *rvm_scope_lookup_s(rvm_scope_t *scope, const rchar *name);
-rvm_varmap_t *rvm_scope_lookuptip_s(rvm_scope_t *scope, const rchar *name);
+rvm_varmap_t *rvm_scope_tiplookup(rvm_scope_t *scope, const rchar *name, ruint namesize);
+rvm_varmap_t *rvm_scope_tiplookup_s(rvm_scope_t *scope, const rchar *name);
 
 
 #ifdef __cplusplus
index 644f7e8..28c02b2 100644 (file)
@@ -12,15 +12,42 @@ typedef struct rvm_compiler_s {
        rvm_codegen_t *cg;
        rvm_scope_t *scope;
        ruint fpoff;
+       ruint dectip;
 } rvm_compiler_t;
 
 
+void rvm_compiler_createvar(rvm_compiler_t *co, const rchar *name, ruint size)
+{
+       rvm_codegen_insertins(co->cg, co->dectip, rvm_asm(RVM_PUSH, R0, XX, XX, 0));
+       rvm_scope_addoffset(co->scope, name, size, co->fpoff);
+       co->dectip += 1;
+       co->fpoff += 1;
+}
+
+
+int rvm_compiler_pushvar(rvm_compiler_t *co, const rchar *name, ruint size)
+{
+       rvm_varmap_t *v = rvm_scope_tiplookup(co->scope,name, size);
+       if (!v) {
+               fprintf(stdout, "undefined variable: ");
+               fwrite(name, sizeof(char), size, stdout);
+               fprintf(stdout, "\n");
+               return -1;
+       }
+
+       rvm_codegen_addins(co->cg, rvm_asm(RVM_LDS, R0, FP, DA, v->data.offset));
+       rvm_codegen_addins(co->cg, rvm_asm(RVM_PUSH, R0, XX, XX, 0));
+       return 0;
+}
+
+
 rvm_compiler_t *rvm_compiler_create()
 {
        rvm_compiler_t *co;
 
        co = r_malloc(sizeof(*co));
        r_memset(co, 0, sizeof(*co));
+       co->fpoff = 1;
        co->cg = rvm_codegen_create();
        co->scope = rvm_scope_create();
        return co;
@@ -155,17 +182,39 @@ int codegen_double_callback(const char *name, void *userdata, const char *input,
 }
 
 
+int codegen_movi_r0_callback(const char *name, void *userdata, const char *input, unsigned int size, unsigned int reason, const char *start, const char *end)
+{
+       rvm_compiler_t *co = (rvm_compiler_t *)userdata;
+
+       rvm_codegen_insertins(co->cg, co->dectip++, rvm_asm(RVM_MOV, R0, DA, XX, r_strtol(input, NULL, 10)));
+       codegen_print_callback(name, userdata, input, size, reason, start, end);
+       return size;
+}
+
+
+int codegen_movd_r0_callback(const char *name, void *userdata, const char *input, unsigned int size, unsigned int reason, const char *start, const char *end)
+{
+       rvm_compiler_t *co = (rvm_compiler_t *)userdata;
+
+       rvm_codegen_insertins(co->cg, co->dectip++, rvm_asmd(RVM_MOV, R0, DA, XX, r_strtod(input, NULL)));
+       codegen_print_callback(name, userdata, input, size, reason, start, end);
+       return size;
+}
+
+
 int codegen_numexpr_callback(const char *name, void *userdata, const char *input, unsigned int size, unsigned int reason, const char *start, const char *end)
 {
        rvm_compiler_t *co = (rvm_compiler_t *)userdata;
        ruint off = rvm_codegen_getcodesize(co->cg);
 
+       rvm_codegen_insertins(co->cg, 0, rvm_asm(RVM_MOV, FP, SP, XX, 0));
        rvm_codegen_addins(co->cg, rvm_asm(RVM_POP, R0, XX, XX, 0));
        rvm_codegen_addins(co->cg, rvm_asm(RVM_PRN, R0, XX, XX, 0));
        rvm_codegen_addins(co->cg, rvm_asm(RVM_EXT, XX, XX, XX, 0));
 
        codegen_print_callback(name, userdata, input, size, reason, start, end);
        rvm_asm_dump(rvm_codegen_getcode(co->cg, off), rvm_codegen_getcodesize(co->cg) - off);
+
        return size;
 }
 
@@ -182,6 +231,27 @@ int codegen_assignment_callback(const char *name, void *userdata, const char *in
 }
 
 
+int codegen_declaration_callback(const char *name, void *userdata, const char *input, unsigned int size, unsigned int reason, const char *start, const char *end)
+{
+       rvm_compiler_t *co = (rvm_compiler_t *)userdata;
+
+       codegen_print_callback(name, userdata, input, size, reason, start, end);
+
+       return size;
+}
+
+
+int codegen_decvar_callback(const char *name, void *userdata, const char *input, unsigned int size, unsigned int reason, const char *start, const char *end)
+{
+       rvm_compiler_t *co = (rvm_compiler_t *)userdata;
+
+       rvm_compiler_createvar(co, input, size);
+       codegen_print_callback(name, userdata, input, size, reason, start, end);
+
+       return size;
+}
+
+
 int codegen_varname_callback(const char *name, void *userdata, const char *input, unsigned int size, unsigned int reason, const char *start, const char *end)
 {
        rvm_compiler_t *co = (rvm_compiler_t *)userdata;
@@ -193,6 +263,21 @@ int codegen_varname_callback(const char *name, void *userdata, const char *input
        return size;
 }
 
+
+int codegen_usevar_callback(const char *name, void *userdata, const char *input, unsigned int size, unsigned int reason, const char *start, const char *end)
+{
+       rvm_compiler_t *co = (rvm_compiler_t *)userdata;
+       ruint off = rvm_codegen_getcodesize(co->cg);
+
+       if (rvm_compiler_pushvar(co, input, size) < 0)
+               return 0;
+       codegen_print_callback(name, userdata, input, size, reason, start, end);
+       rvm_asm_dump(rvm_codegen_getcode(co->cg, off), rvm_codegen_getcodesize(co->cg) - off);
+
+       return size;
+}
+
+
 int main(int argc, char *argv[])
 {
        int res;
@@ -208,20 +293,29 @@ int main(int argc, char *argv[])
        rpa_dbex_add_callback(dbex, "subop", RPA_REASON_MATCHED, codegen_subop_callback, co);
        rpa_dbex_add_callback(dbex, "integer", RPA_REASON_MATCHED, codegen_integer_callback, co);
        rpa_dbex_add_callback(dbex, "double", RPA_REASON_MATCHED, codegen_double_callback, co);
+       rpa_dbex_add_callback(dbex, "movi_r0", RPA_REASON_MATCHED, codegen_movi_r0_callback, co);
+       rpa_dbex_add_callback(dbex, "movd_r0", RPA_REASON_MATCHED, codegen_movd_r0_callback, co);
        rpa_dbex_add_callback(dbex, "numexpr", RPA_REASON_MATCHED, codegen_numexpr_callback, co);
        rpa_dbex_add_callback(dbex, "assignment", RPA_REASON_MATCHED, codegen_assignment_callback, co);
+       rpa_dbex_add_callback(dbex, "declaration", RPA_REASON_MATCHED, codegen_declaration_callback, co);
        rpa_dbex_add_callback(dbex, "varname", RPA_REASON_MATCHED, codegen_varname_callback, co);
-
+       rpa_dbex_add_callback(dbex, "usevar", RPA_REASON_MATCHED, codegen_usevar_callback, co);
+       rpa_dbex_add_callback(dbex, "decvar", RPA_REASON_MATCHED, codegen_decvar_callback, co);
 
        rpa_dbex_open(dbex);
        rpa_dbex_load_string(dbex, "S                   ::= [#x20]+");
        rpa_dbex_load_string(dbex, "digit               ::= [0-9]");
        rpa_dbex_load_string(dbex, "eoe                 ::= (';' | [#xD] | [#xA])+");
        rpa_dbex_load_string(dbex, "varname             ::= [a-zA-z][a-zA-z0-9]*");
-       rpa_dbex_load_string(dbex, "assignment  ::= <:S:>? <:varname:> <:S:>? '=' <:S:>? (<:double:> | <:integer:>) <:S:>? <:eoe:>");
+       rpa_dbex_load_string(dbex, "usevar              ::= <varname>");
+       rpa_dbex_load_string(dbex, "decvar              ::= <varname>");
+       rpa_dbex_load_string(dbex, "assignment  ::= <:S:>? <:varname:> <:S:>? '=' <:S:>? (<double> | <integer>) <:S:>? <:eoe:>");
+       rpa_dbex_load_string(dbex, "declaration ::= <:S:>? 'var' <:S:> <:decvar:> <:S:>? '=' <:S:>? (<:movd_r0:> | <:movi_r0:>) <:S:>? <:eoe:>");
        rpa_dbex_load_string(dbex, "integer             ::= <:digit:>+");
-       rpa_dbex_load_string(dbex, "double              ::= <:digit:>+ '.' <:digit:>+ | '.' <:digit:>+");
-       rpa_dbex_load_string(dbex, "term                ::= <:double:> | <:integer:> | '('<:S:>? <:expr:> <:S:>? ')'");
+       rpa_dbex_load_string(dbex, "double              ::= <integer> '.' <integer> | '.' <integer>");
+       rpa_dbex_load_string(dbex, "movi_r0             ::= <integer>");
+       rpa_dbex_load_string(dbex, "movd_r0             ::= <integer> '.' <integer> | '.' <integer>");
+       rpa_dbex_load_string(dbex, "term                ::= <:usevar:> | <:double:> | <:integer:> | '('<:S:>? <:expr:> <:S:>? ')'");
        rpa_dbex_load_string(dbex, "mulop               ::= <:mulex:> <:S:>? '*' <:S:>? <:term:>");
        rpa_dbex_load_string(dbex, "divop               ::= <:mulex:> <:S:>? '/' <:S:>? <:term:>");
        rpa_dbex_load_string(dbex, "mulex               ::= <:mulop:> | <:divop:> | <:term:>");
@@ -229,7 +323,7 @@ int main(int argc, char *argv[])
        rpa_dbex_load_string(dbex, "subop               ::= <:expr:> <:S:>? '-' <:S:>? <:mulex:>");
        rpa_dbex_load_string(dbex, "expr        ::= <:addop:> | <:subop:> | <:mulex:>");
        rpa_dbex_load_string(dbex, "numexpr             ::= <:expr:>");
-       rpa_dbex_load_string(dbex, "exec                ::= <:assignment:>* <:S:>? <:numexpr:>");
+       rpa_dbex_load_string(dbex, "exec                ::= (<:declaration:>|<:assignment:>)* <:S:>? <:numexpr:>");
        rpa_dbex_close(dbex);
        if (argc > 1) {
                res = rpa_dbex_parse(dbex, rpa_dbex_default_pattern(dbex), argv[1], argv[1], argv[1] + r_strlen(argv[1]));
index e971542..af0a285 100644 (file)
@@ -10,7 +10,7 @@ void print_var_info(rvm_scope_t *scope, rchar* varname)
 {
        rvm_varmap_t *vmap;
 
-       vmap = rvm_scope_lookuptip_s(scope, varname);
+       vmap = rvm_scope_tiplookup_s(scope, varname);
        if (vmap) {
                if (vmap && vmap->datatype == VARMAP_DATATYPE_OFFSET)
                        fprintf(stdout, "tip: %s, offset: %d\n", vmap->name, vmap->data.offset);