RPA Toolkit
RJS: work on switch statement implementation.
authorMartin Stoilov <martin@rpasearch.com>
Thu, 4 Aug 2011 05:39:12 +0000 (22:39 -0700)
committerMartin Stoilov <martin@rpasearch.com>
Thu, 4 Aug 2011 05:39:12 +0000 (22:39 -0700)
rjs/ecma262.rpa
rjs/rjs.c
rjs/rjscompiler.c
rvm/rvmcpu.c
rvm/rvmreg.c
rvm/rvmreg.h
tests/testmisc/rpagen-test.c

index 420c96c..81607ca 100644 (file)
@@ -93,6 +93,9 @@
 #!emitid IterationForIn                                        UID_ITERATIONFORIN                                      63
 #!emitid ForInInit                                             UID_FORININIT                                           64
 #!emitid UnaryExpressionTypeof                 UID_UNARYEXPRESSIONTYPEOF                       65
+#!emitid SwitchStatement                               UID_SWITCHSTATEMENT                                     66
+#!emitid CaseClause                                            UID_CASECLAUSE                                          67
+#!emitid DefaultClause                                 UID_DEFAULTCLAUSE                                       68
 
 #!abort SC
 
@@ -351,7 +354,7 @@ Statement                                           ::= <FunctionDeclarationStatement> |
                                                                <BreakStatement> |
                                                                <ReturnStatement> |
                                                                <WithStatement> |
-                                                               <SwitchStatementOp>
+                                                               <SwitchStatement>
 
 # 12.1 Block
 Block                                                          ::= <S>? '{' <S>? <StatementList>? <S>? '}' <S>?
@@ -429,19 +432,18 @@ WithStatement                                             ::= 'with' <S>? '(' <S>? <Expression> <S>? ')' <S>? <Statemen
 
 
 # 12.11 The switch Statement
-SwitchExpressionOp                                     ::= ('switch' - 'switch' <IdentifierPart>) <S>? '(' <S>? <Expression> <S>? ')'
-SwitchStatementOp                                      ::= <SwitchExpressionOp> <S>? <CaseBlock>
+SwitchExpression                                       ::= ('switch' - 'switch' <IdentifierPart>) <S>? '(' <S>? <Expression> <S>? ')'
+SwitchStatement                                                ::= <SwitchExpression> <S>? <CaseBlock>
 
-CaseBlock                                                      ::= '{' <S>? <CaseClauses>? <S>? <DefaultClauseOp>? <S>? <CaseClauses>? <S>? '}' |
+CaseBlock                                                      ::= '{' <S>? <CaseClauses>? <S>? <DefaultClause>? <S>? <CaseClauses>? <S>? '}' |
                                                                        '{' <S>? <CaseClauses>? <S>? '}'
 
 
-
-CaseClauses                                            ::= (<S>? <CaseClauseOp>)+
-CaseExpressionOp                                       ::= ('case' - 'case' <IdentifierPart>) <S>? <Expression> <S>? ':'
-CaseClauseOp                                           ::= <CaseExpressionOp> <S>? <StatementList>?
-DefaultKeywordOp                                       ::= 'default' - 'default' <IdentifierPart>
-DefaultClauseOp                                                ::= <DefaultKeywordOp> <S>? ':' <S>? <StatementList>?
+CaseClauses                                            ::= (<S>? <CaseClause>)+
+CaseExpression                                         ::= ('case' - 'case' <IdentifierPart>) <S>? <Expression> <S>? ':'
+CaseClause                                                     ::= <CaseExpression> <S>? <StatementList>?
+DefaultKeyword                                         ::= 'default' - 'default' <IdentifierPart>
+DefaultClause                                          ::= <DefaultKeyword> <S>? ':' <S>? <StatementList>?
 
 
 # 13 Function Definition
index 6fb45f7..9e59f2a 100644 (file)
--- a/rjs/rjs.c
+++ b/rjs/rjs.c
@@ -73,6 +73,7 @@ error:
 void rjs_engine_destroy(rjs_engine_t *jse)
 {
        unsigned long i;
+
        if (jse) {
                if (jse->cgs) {
                        for (i = 0; i < r_array_length(jse->cgs); i++) {
@@ -88,41 +89,42 @@ void rjs_engine_destroy(rjs_engine_t *jse)
        }
 }
 
+
+static void rjs_engine_addtypename(rjs_engine_t *jse, rmap_t *types, unsigned long type, const char *typename)
+{
+       rvmreg_t rs;
+       rstring_t *s;
+
+       s = r_string_create_from_ansistr(typename);
+       r_gc_add(jse->cpu->gc, (robject_t*)s);
+       rvm_reg_setstring(&rs, s);
+       r_map_add_l(types, type, &rs);
+}
+
+
 static void rjs_engine_inittypes(rjs_engine_t *jse)
 {
        rmap_t *gmap = (rmap_t *)RVM_CPUREG_GETP(jse->cpu, GP);
        rmap_t *types;
-       rvmreg_t rt, rs;
-       rstring_t *s;
+       rvmreg_t rt;
 
        types = r_map_create(sizeof(rvmreg_t), 3);
        r_gc_add(jse->cpu->gc, (robject_t*)types);
        rvm_reg_setjsobject(&rt, (robject_t *)types);
        r_map_add_l(gmap, RJS_GPKEY_TYPES, &rt);
-
-       s = r_string_create_from_ansistr("undefined");
-       r_gc_add(jse->cpu->gc, (robject_t*)s);
-       rvm_reg_setstring(&rs, s);
-       r_map_add_l(types, RVM_DTYPE_UNDEF, &rs);
-
-       s = r_string_create_from_ansistr("boolean");
-       r_gc_add(jse->cpu->gc, (robject_t*)s);
-       rvm_reg_setstring(&rs, s);
-       r_map_add_l(types, RVM_DTYPE_BOOLEAN, &rs);
-
-       s = r_string_create_from_ansistr("number");
-       r_gc_add(jse->cpu->gc, (robject_t*)s);
-       rvm_reg_setstring(&rs, s);
-       r_map_add_l(types, RVM_DTYPE_DOUBLE, &rs);
-       r_map_add_l(types, RVM_DTYPE_UNSIGNED, &rs);
-       r_map_add_l(types, RVM_DTYPE_SIGNED, &rs);
-
-       s = r_string_create_from_ansistr("string");
-       rvm_reg_setstring(&rs, s);
-       r_gc_add(jse->cpu->gc, (robject_t*)s);
-       r_map_add_l(types, RVM_DTYPE_STRING, &rs);
+       rjs_engine_addtypename(jse, types, RVM_DTYPE_UNDEF, "undefined");
+       rjs_engine_addtypename(jse, types, RVM_DTYPE_BOOLEAN, "boolean");
+       rjs_engine_addtypename(jse, types, RVM_DTYPE_DOUBLE, "number");
+       rjs_engine_addtypename(jse, types, RVM_DTYPE_UNSIGNED, "number");
+       rjs_engine_addtypename(jse, types, RVM_DTYPE_SIGNED, "number");
+       rjs_engine_addtypename(jse, types, RVM_DTYPE_STRING, "string");
+       rjs_engine_addtypename(jse, types, RVM_DTYPE_FUNCTION, "object");
+       rjs_engine_addtypename(jse, types, RVM_DTYPE_NAN, "object");
+       rjs_engine_addtypename(jse, types, RVM_DTYPE_MAP, "object");
+       rjs_engine_addtypename(jse, types, RVM_DTYPE_POINTER, "object");
 }
 
+
 static void rjs_engine_initgp(rjs_engine_t *jse)
 {
        rvmreg_t gp;
@@ -131,7 +133,6 @@ static void rjs_engine_initgp(rjs_engine_t *jse)
        rvm_reg_init(&gp);
        gmap = r_map_create(sizeof(rvmreg_t), 7);
        r_gc_add(jse->cpu->gc, (robject_t*)gmap);
-
        rvm_reg_setjsobject(RVM_CPUREG_PTR(jse->cpu, GP), (robject_t *)gmap);
        rjs_engine_inittypes(jse);
 }
@@ -378,7 +379,7 @@ static void rjs_engine_print(rvmcpu_t *cpu, rvm_asmins_t *ins)
                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)
+       else if (rvm_reg_gettype(r) == RVM_DTYPE_MAP)
                r_printf("(object) %p\n",RVM_REG_GETP(r));
        else if (rvm_reg_gettype(r) == RVM_DTYPE_HARRAY)
                r_printf("(hashed array) %p\n",RVM_REG_GETP(r));
@@ -411,7 +412,7 @@ static void rjs_engine_dbgprint(rvmcpu_t *cpu, rvm_asmins_t *ins)
                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)
+       else if (rvm_reg_gettype(r) == RVM_DTYPE_MAP)
                r_printf("(object) %p\n",RVM_REG_GETP(r));
        else if (rvm_reg_gettype(r) == RVM_DTYPE_HARRAY)
                r_printf("(hashed array) %p\n",RVM_REG_GETP(r));
index 5793080..8e3940c 100644 (file)
@@ -1263,7 +1263,7 @@ int rjs_compiler_rh_forininit(rjs_compiler_t *co, rarray_t *records, long rec)
        rjs_compiler_debughead(co, records, rec);
        rvm_codegen_addins(co->cg, rvm_asm(RVM_PUSH, R0, XX, XX, 0));
        rvm_codegen_addins(co->cg, rvm_asm(RVM_TYPE, R0, R0, XX, 0));
-       rvm_codegen_addins(co->cg, rvm_asml(RVM_CMP, R0, DA, XX, RVM_DTYPE_JSOBJECT));
+       rvm_codegen_addins(co->cg, rvm_asml(RVM_CMP, R0, DA, XX, RVM_DTYPE_MAP));
        rvm_codegen_index_addrelocins(co->cg, RVM_RELOC_BRANCH, ctx->endidx, rvm_asm(RVM_BNEQ, DA, XX, XX, 0));
        rvm_codegen_redefinelabel_default(co->cg, ctx->iterationidx);
        rvm_codegen_redefinelabel_default(co->cg, ctx->continueidx);
index a6db1dd..fdaeef2 100644 (file)
@@ -1127,7 +1127,7 @@ static void rvm_op_prn(rvmcpu_t *cpu, rvm_asmins_t *ins)
                rvm_printf("(STRING) R%d = %s\n", ins->op1, ((rstring_t*) RVM_REG_GETP(r))->s.str);
        else if (rvm_reg_gettype(r) == RVM_DTYPE_ARRAY)
                rvm_printf("(ARRAY) R%d = %p\n", ins->op1, RVM_REG_GETP(r));
-       else if (rvm_reg_gettype(r) == RVM_DTYPE_JSOBJECT)
+       else if (rvm_reg_gettype(r) == RVM_DTYPE_MAP)
                rvm_printf("(Object) R%d = %p\n", ins->op1, RVM_REG_GETP(r));
        else if (rvm_reg_gettype(r) == RVM_DTYPE_SWIID)
                rvm_printf("(SWI) R%d = %p\n", ins->op1, RVM_REG_GETP(r));
@@ -1549,7 +1549,7 @@ static void rvm_op_maplookup(rvmcpu_t *cpu, rvm_asmins_t *ins)
        rvmreg_t *arg3 = RVM_CPUREG_PTR(cpu, ins->op3);
        rmap_t *a = (rmap_t*)RVM_REG_GETP(arg2);
 
-       if (rvm_reg_gettype(arg2) != RVM_DTYPE_JSOBJECT) {
+       if (rvm_reg_gettype(arg2) != RVM_DTYPE_MAP) {
                RVM_ABORT(cpu, RVM_E_NOTOBJECT);
        }
        if (RVM_REG_GETTYPE(arg3) == RVM_DTYPE_SIGNED || RVM_REG_GETTYPE(arg3) == RVM_DTYPE_UNSIGNED) {
@@ -1578,7 +1578,7 @@ static void rvm_op_mapadd(rvmcpu_t *cpu, rvm_asmins_t *ins)
        rvmreg_t *arg3 = RVM_CPUREG_PTR(cpu, ins->op3);
        rmap_t *a = (rmap_t*)RVM_REG_GETP(arg2);
 
-       if (rvm_reg_gettype(arg2) != RVM_DTYPE_JSOBJECT) {
+       if (rvm_reg_gettype(arg2) != RVM_DTYPE_MAP) {
                RVM_ABORT(cpu, RVM_E_NOTOBJECT);
        }
        if (RVM_REG_GETTYPE(arg3) == RVM_DTYPE_SIGNED || RVM_REG_GETTYPE(arg3) == RVM_DTYPE_UNSIGNED) {
@@ -1607,7 +1607,7 @@ static void rvm_op_maplookupadd(rvmcpu_t *cpu, rvm_asmins_t *ins)
        rvmreg_t *arg3 = RVM_CPUREG_PTR(cpu, ins->op3);
        rmap_t *a = (rmap_t*)RVM_REG_GETP(arg2);
 
-       if (rvm_reg_gettype(arg2) != RVM_DTYPE_JSOBJECT) {
+       if (rvm_reg_gettype(arg2) != RVM_DTYPE_MAP) {
                RVM_ABORT(cpu, RVM_E_NOTOBJECT);
        }
        if (RVM_REG_GETTYPE(arg3) == RVM_DTYPE_SIGNED || RVM_REG_GETTYPE(arg3) == RVM_DTYPE_UNSIGNED) {
@@ -1646,7 +1646,7 @@ static void rvm_op_mapdel(rvmcpu_t *cpu, rvm_asmins_t *ins)
 
        rvm_opmap_invoke_binary_handler(cpu->opmap, RVM_OPID_CAST, cpu, &tmp, RVM_CPUREG_PTR(cpu, ins->op3), &tmp);
        index = (long)RVM_REG_GETL(&tmp);
-       if (rvm_reg_gettype(arg2) != RVM_DTYPE_JSOBJECT)
+       if (rvm_reg_gettype(arg2) != RVM_DTYPE_MAP)
                RVM_ABORT(cpu, RVM_E_NOTOBJECT);
        a = (rmap_t*)RVM_REG_GETP(arg2);
        ret = r_map_delete(a, index);
@@ -1665,7 +1665,7 @@ static void rvm_op_mapaddr(rvmcpu_t *cpu, rvm_asmins_t *ins)
 
        rvm_opmap_invoke_binary_handler(cpu->opmap, RVM_OPID_CAST, cpu, &tmp, RVM_CPUREG_PTR(cpu, ins->op3), &tmp);
        index = (long)RVM_REG_GETL(&tmp);
-       if (rvm_reg_gettype(arg2) != RVM_DTYPE_JSOBJECT)
+       if (rvm_reg_gettype(arg2) != RVM_DTYPE_MAP)
                RVM_ABORT(cpu, RVM_E_NOTOBJECT);
        a = (rmap_t*)RVM_REG_GETP(arg2);
        value = r_map_value(a, index);
@@ -1688,7 +1688,7 @@ static void rvm_op_mapldr(rvmcpu_t *cpu, rvm_asmins_t *ins)
 
        rvm_opmap_invoke_binary_handler(cpu->opmap, RVM_OPID_CAST, cpu, &tmp, RVM_CPUREG_PTR(cpu, ins->op3), &tmp);
        index = (long)RVM_REG_GETL(&tmp);
-       if (rvm_reg_gettype(arg2) != RVM_DTYPE_JSOBJECT)
+       if (rvm_reg_gettype(arg2) != RVM_DTYPE_MAP)
                RVM_ABORT(cpu, RVM_E_NOTOBJECT);
        a = (rmap_t*)RVM_REG_GETP(arg2);
        value = r_map_value(a, index);
@@ -1712,7 +1712,7 @@ static void rvm_op_mapkeyldr(rvmcpu_t *cpu, rvm_asmins_t *ins)
 
        rvm_opmap_invoke_binary_handler(cpu->opmap, RVM_OPID_CAST, cpu, &tmp, RVM_CPUREG_PTR(cpu, ins->op3), &tmp);
        index = (long)RVM_REG_GETL(&tmp);
-       if (rvm_reg_gettype(arg2) != RVM_DTYPE_JSOBJECT)
+       if (rvm_reg_gettype(arg2) != RVM_DTYPE_MAP)
                RVM_ABORT(cpu, RVM_E_NOTOBJECT);
        a = (rmap_t*)RVM_REG_GETP(arg2);
        key = r_map_key(a, index);
@@ -1735,7 +1735,7 @@ static void rvm_op_mapnext(rvmcpu_t *cpu, rvm_asmins_t *ins)
 
        rvm_opmap_invoke_binary_handler(cpu->opmap, RVM_OPID_CAST, cpu, &tmp, RVM_CPUREG_PTR(cpu, ins->op3), &tmp);
        index = (long)RVM_REG_GETL(&tmp);
-       if (rvm_reg_gettype(arg2) != RVM_DTYPE_JSOBJECT)
+       if (rvm_reg_gettype(arg2) != RVM_DTYPE_MAP)
                RVM_ABORT(cpu, RVM_E_NOTOBJECT);
        a = (rmap_t*)RVM_REG_GETP(arg2);
        if (index < 0)
@@ -1758,7 +1758,7 @@ static void rvm_op_mapprev(rvmcpu_t *cpu, rvm_asmins_t *ins)
 
        rvm_opmap_invoke_binary_handler(cpu->opmap, RVM_OPID_CAST, cpu, &tmp, RVM_CPUREG_PTR(cpu, ins->op3), &tmp);
        index = (long)RVM_REG_GETL(&tmp);
-       if (rvm_reg_gettype(arg2) != RVM_DTYPE_JSOBJECT)
+       if (rvm_reg_gettype(arg2) != RVM_DTYPE_MAP)
                RVM_ABORT(cpu, RVM_E_NOTOBJECT);
        a = (rmap_t*)RVM_REG_GETP(arg2);
        if (index < 0)
@@ -1782,7 +1782,7 @@ static void rvm_op_mapstr(rvmcpu_t *cpu, rvm_asmins_t *ins)
 
        rvm_opmap_invoke_binary_handler(cpu->opmap, RVM_OPID_CAST, cpu, &tmp, RVM_CPUREG_PTR(cpu, ins->op3), &tmp);
        index = (long)RVM_REG_GETL(&tmp);
-       if (rvm_reg_gettype(arg2) != RVM_DTYPE_JSOBJECT)
+       if (rvm_reg_gettype(arg2) != RVM_DTYPE_MAP)
                RVM_ABORT(cpu, RVM_E_NOTOBJECT);
        a = (rmap_t*)RVM_REG_GETP(arg2);
        value = r_map_value(a, index);
index 719259a..ee63770 100644 (file)
@@ -261,7 +261,7 @@ void rvm_reg_setarray(rvmreg_t *r, robject_t *ptr)
 void rvm_reg_setjsobject(rvmreg_t *r, robject_t *ptr)
 {
        RVM_REG_SETP(r, ptr);
-       RVM_REG_SETTYPE(r, RVM_DTYPE_JSOBJECT);
+       RVM_REG_SETTYPE(r, RVM_DTYPE_MAP);
        RVM_REG_SETFLAG(r, RVM_INFOBIT_ROBJECT);
 }
 
@@ -504,6 +504,6 @@ rstring_t *rvm_reg_string(const rvmreg_t *src)
 
 rmap_t *rvm_reg_jsobject(const rvmreg_t *src)
 {
-       R_ASSERT(src && rvm_reg_gettype(src) == RVM_DTYPE_JSOBJECT);
+       R_ASSERT(src && rvm_reg_gettype(src) == RVM_DTYPE_MAP);
        return (rmap_t*)RVM_REG_GETP(src);
 }
index 0e0652f..c4aedd2 100644 (file)
@@ -48,7 +48,8 @@ extern "C" {
 #define RVM_DTYPE_ARRAY 9
 #define RVM_DTYPE_HARRAY 10
 #define RVM_DTYPE_NAN 11
-#define RVM_DTYPE_JSOBJECT 12
+#define RVM_DTYPE_NULL RVM_DTYPE_NAN
+#define RVM_DTYPE_MAP 12
 #define RVM_DTYPE_FUNCTION 13
 #define RVM_DTYPE_SWIID 14
 #define RVM_DTYPE_USER 16
index 305ca23..edd6835 100644 (file)
@@ -302,7 +302,7 @@ static void rvm_js_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_JSOBJECT)
+       else if (rvm_reg_gettype(r) == RVM_DTYPE_MAP)
                r_printf("(object) %p\n",RVM_REG_GETP(r));
        else if (rvm_reg_gettype(r) == RVM_DTYPE_HARRAY)
                r_printf("(hashed array) %p\n",RVM_REG_GETP(r));