RPA Toolkit
Work on the RJS engine - refined recursive compilation, added script execution.
authorMartin Stoilov <martin@rpasearch.com>
Fri, 29 Apr 2011 04:07:23 +0000 (21:07 -0700)
committerMartin Stoilov <martin@rpasearch.com>
Fri, 29 Apr 2011 04:07:23 +0000 (21:07 -0700)
rjs/ecma262.rpa
rjs/rjs.c
rjs/rjs.h
rjs/rjscompiler.c
rjs/rjscompiler.h
rjs/rjsexec.c

index 108e19a..ab6477b 100644 (file)
 #!emit CallExpression
 #!emit AssignmentExpressionOp
 
-#uid Program                                           RJS_PROGRAM                                             1
-#uid PostfixExpressionValOp            RJS_POSTFIXEXPRESSIONVALOP                      2
-#uid Expression                                        RJS_EXPRESSION                                          3
-#uid LeftHandSideExpression            RJS_LEFTHANDSIDEEXPRESSION                      4 
+#!uid Program                          RJS_PROGRAM                                     1
+#!uid PostfixExpressionValOp           RJS_POSTFIXEXPRESSIONVALOP                      2
+#!uid Expression                       RJS_EXPRESSION                                  3
+#!uid LeftHandSideExpression           RJS_LEFTHANDSIDEEXPRESSION                      4 
 
 
 # 6 Source Text
@@ -421,5 +421,5 @@ SourceElement                                               ::= <FunctionDeclaration> |
 Program                                                        ::= <SourceElements>
 # The root rule, it is anonymous
 <Program>
-       
+
 
index 7d43249..5dda861 100644 (file)
--- a/rjs/rjs.c
+++ b/rjs/rjs.c
@@ -105,6 +105,16 @@ rint rjs_engine_close(rjs_engine_t *jse)
 
 rint rjs_engine_run(rjs_engine_t *jse)
 {
+       rint res = 0;
 
-       return 0;
+       if (!jse->co) {
+
+               return -1;
+       }
+       if (jse->debugexec) {
+               res = rvm_cpu_exec_debug(jse->cpu, rvm_codegen_getcode(jse->co->cg, 0), 0);
+       } else {
+               res = rvm_cpu_exec(jse->cpu, rvm_codegen_getcode(jse->co->cg, 0), 0);
+       }
+       return res;
 }
index 007b14f..72a67a5 100644 (file)
--- a/rjs/rjs.h
+++ b/rjs/rjs.h
@@ -16,6 +16,7 @@ typedef struct rjs_engine_s {
        rjs_compiler_t *co;
        rvmcpu_t *cpu;
        rlong debugcompile:1;
+       rlong debugexec:1;
 } rjs_engine_t;
 
 
index 18a94dd..19c7f0b 100644 (file)
@@ -5,47 +5,76 @@ static rint rjs_compiler_playrecord(rjs_compiler_t *co, rarray_t *records, rlong
 static rint rjs_compiler_playchildrecords(rjs_compiler_t *co, rarray_t *records, rlong rec);
 
 
-void rjs_compiler_debughead(rjs_compiler_t *co, rarray_t *records, rlong rec, rparecord_t *prec)
+void rjs_compiler_debughead(rjs_compiler_t *co, rarray_t *records, rlong rec)
 {
        if (co->debug) {
                rpa_record_dump(records, rec);
-
+               co->headoff = rvm_codegen_getcodesize(co->cg);
        }
 }
 
 
-void rjs_compiler_debugtail(rjs_compiler_t *co, rarray_t *records, rlong rec, rparecord_t *prec)
+void rjs_compiler_debugtail(rjs_compiler_t *co, rarray_t *records, rlong rec)
 {
        if (co->debug) {
-
+               rvm_asm_dump(rvm_codegen_getcode(co->cg, co->headoff), rvm_codegen_getcodesize(co->cg) - co->headoff);
        }
 
 }
 
 
-rint rjs_compiler_rh_program(rjs_compiler_t *co, rarray_t *records, rlong rec, rparecord_t *prec)
+rint rjs_compiler_rh_program(rjs_compiler_t *co, rarray_t *records, rlong rec)
 {
-       rjs_compiler_debughead(co, records, rec, prec);
+       rparecord_t *prec;
+       prec = (rparecord_t *)r_array_slot(records, rec);
+       rjs_compiler_debughead(co, records, rec);
+       rvm_codegen_addins(co->cg, rvm_asm(RVM_NOP, XX, XX, XX, 0));
+       rjs_compiler_debugtail(co, records, rec);
 
-       rjs_compiler_debugtail(co, records, rec, prec);
+       if (rjs_compiler_playchildrecords(co, records, rec) < 0)
+               return -1;
+
+       rec = rpa_recordtree_get(records, rec, RPA_RECORD_END);
+       prec = (rparecord_t *)r_array_slot(records, rec);
+       rjs_compiler_debughead(co, records, rec);
+       rvm_codegen_addins(co->cg, rvm_asm(RVM_EXT, XX, XX, XX, 0));
+       rjs_compiler_debugtail(co, records, rec);
        return 0;
 }
 
 
-rint rjs_compiler_rh_expression(rjs_compiler_t *co, rarray_t *records, rlong rec, rparecord_t *prec)
+rint rjs_compiler_rh_expression(rjs_compiler_t *co, rarray_t *records, rlong rec)
 {
-       rjs_compiler_debughead(co, records, rec, prec);
+       rparecord_t *prec;
+       prec = (rparecord_t *)r_array_slot(records, rec);
+       rjs_compiler_debughead(co, records, rec);
+       rjs_compiler_debugtail(co, records, rec);
 
-       rjs_compiler_debugtail(co, records, rec, prec);
+       if (rjs_compiler_playchildrecords(co, records, rec) < 0)
+               return -1;
+
+       rec = rpa_recordtree_get(records, rec, RPA_RECORD_END);
+       prec = (rparecord_t *)r_array_slot(records, rec);
+       rjs_compiler_debughead(co, records, rec);
+       rjs_compiler_debugtail(co, records, rec);
        return 0;
 }
 
 
-rint rjs_compiler_rh_lefthandsideexpression(rjs_compiler_t *co, rarray_t *records, rlong rec, rparecord_t *prec)
+rint rjs_compiler_rh_lefthandsideexpression(rjs_compiler_t *co, rarray_t *records, rlong rec)
 {
-       rjs_compiler_debughead(co, records, rec, prec);
+       rparecord_t *prec;
+       prec = (rparecord_t *)r_array_slot(records, rec);
+       rjs_compiler_debughead(co, records, rec);
+       rjs_compiler_debugtail(co, records, rec);
+
+       if (rjs_compiler_playchildrecords(co, records, rec) < 0)
+               return -1;
 
-       rjs_compiler_debugtail(co, records, rec, prec);
+       rec = rpa_recordtree_get(records, rec, RPA_RECORD_END);
+       prec = (rparecord_t *)r_array_slot(records, rec);
+       rjs_compiler_debughead(co, records, rec);
+       rjs_compiler_debugtail(co, records, rec);
        return 0;
 }
 
@@ -78,18 +107,21 @@ void rjs_compiler_destroy(rjs_compiler_t *co)
 }
 
 
-static rint rjs_compiler_rh_default(rjs_compiler_t *co, rarray_t *records, rlong rec, rparecord_t *prec)
+static rint rjs_compiler_rh_default(rjs_compiler_t *co, rarray_t *records, rlong rec)
 {
-       rjs_compiler_debughead(co, records, rec, prec);
-       rjs_compiler_debugtail(co, records, rec, prec);
+       rparecord_t *prec;
+
+       prec = (rparecord_t *)r_array_slot(records, rec);
+       rjs_compiler_debughead(co, records, rec);
+       rjs_compiler_debugtail(co, records, rec);
 
        if (rjs_compiler_playchildrecords(co, records, rec) < 0)
                return -1;
 
        rec = rpa_recordtree_get(records, rec, RPA_RECORD_END);
        prec = (rparecord_t *)r_array_slot(records, rec);
-       rjs_compiler_debughead(co, records, rec, prec);
-       rjs_compiler_debugtail(co, records, rec, prec);
+       rjs_compiler_debughead(co, records, rec);
+       rjs_compiler_debugtail(co, records, rec);
 
        return 0;
 }
@@ -113,21 +145,24 @@ static rint rjs_compiler_playrecord(rjs_compiler_t *co, rarray_t *records, rlong
        rparecord_t *prec;
        prec = (rparecord_t *)r_array_slot(records, rec);
        if (prec->ruleuid >= 0 && prec->ruleuid < RJS_COMPILER_NHANDLERS && co->handlers[prec->ruleuid]) {
-               return co->handlers[prec->ruleuid](co, records, rec, prec);
+               return co->handlers[prec->ruleuid](co, records, rec);
        }
-       return rjs_compiler_rh_default(co, records, rec, prec);
+       return rjs_compiler_rh_default(co, records, rec);
 }
 
 
 rint rjs_compiler_compile(rjs_compiler_t *co, rarray_t *records)
 {
        rlong i;
-       rparecord_t *prec;
 
        if (!co || !records || r_array_empty(records)) {
 
                return -1;
        }
 
-       return rjs_compiler_playrecord(co, records, 0);
+       for (i = 0; i >= 0; i = rpa_recordtree_next(records, i, RPA_RECORD_START)) {
+               if (rjs_compiler_playrecord(co, records, i) < 0)
+                       return -1;
+       }
+       return 0;
 }
index f184629..4c3e29e 100644 (file)
@@ -21,12 +21,13 @@ typedef struct rjs_coexp_s {
 
 
 typedef struct rjs_compiler_s rjs_compiler_t;
-typedef rint (*RJS_COMPILER_RH)(rjs_compiler_t *co, rarray_t *records, rlong rec, rparecord_t *prec);
+typedef rint (*RJS_COMPILER_RH)(rjs_compiler_t *co, rarray_t *records, rlong rec);
 
 struct rjs_compiler_s {
        rvmcpu_t *cpu;
        rvm_codegen_t *cg;
        rarray_t *coexp;
+       rlong headoff;
        rulong debug:1;
        RJS_COMPILER_RH handlers[RJS_COMPILER_NHANDLERS];
 };
index cec85c8..87d820d 100644 (file)
@@ -124,6 +124,9 @@ exec:
                rjs_engine_compile(jse, script->str, script->size);
        } else {
                rjs_engine_compile(jse, script->str, script->size);
+               if (debuginfo)
+                       jse->debugexec = 1;
+               rjs_engine_run(jse);
        }
 
 end: