Anonymous avatar Anonymous committed 597ba11

Added code for stack size calculation.

Comments (0)

Files changed (5)

 ^arana/lexnparse.c
-^arana/grammar.[ch]
-^arana/grammar.out
+^arana/grammar.([ch]|out)
+^arana/lexer.c
+^arana/parser.([ch]|out)
 ^tools/lemon
 \.o$
 \.so$
 static Ar_size_t
 arana_compiler_get_stack_size(AR_SIG, ArCodeUnit *unit)
 {
-    return 0;
+    ArOp op;
+    Ar_size_t depth = 0, max_depth = 0;
+
+#define ON_OPCODE(x) case AR_OP_##x
+#define RECORD(x) \
+    depth += x; \
+    if (depth > max_depth) max_depth = depth; \
+    break
+
+    for (Ar_size_t i = 0; i < unit->op_count; i++)
+        switch ((op = unit->ops[i]).op) {
+        ON_OPCODE(LOAD_CONST):
+        ON_OPCODE(LOAD_FAST):
+        ON_OPCODE(ITER_NEXT):
+            RECORD(+1);
+        ON_OPCODE(MAKE_STRING):
+        ON_OPCODE(MAKE_TUPLE):
+        ON_OPCODE(MAKE_LIST):
+            RECORD(1 - op.arg);
+        ON_OPCODE(CALL_METHOD):
+            RECORD(-2);
+        ON_OPCODE(STORE_FAST):
+        ON_OPCODE(POS):
+        ON_OPCODE(NEG):
+        ON_OPCODE(NOT):
+        ON_OPCODE(GET_ITERATOR):
+            RECORD(0);
+        ON_OPCODE(ADD):
+        ON_OPCODE(SUB):
+        ON_OPCODE(MUL):
+        ON_OPCODE(DIV):
+        ON_OPCODE(MOD):
+        ON_OPCODE(POW):
+        ON_OPCODE(JUMP_IF_FALSE):
+        ON_OPCODE(JUMP_ALWAYS):
+        ON_OPCODE(POP_TOP):
+        ON_OPCODE(CALL):
+        ON_OPCODE(GET_ATTRIBUTE):
+        ON_OPCODE(GET_SUBSCRIPT):
+            RECORD(-1);
+        }
+
+    return max_depth;
 }
 
 static ArCode *
     code->filename = compiler->unit->filename;
     code->vars = arana_compiler_make_localtuple(AR_ISIG, compiler->unit->vars);
     code->ops = compiler->unit->ops;
-    code->stack_size = arana_compiler_get_stack_size(AR_SIG, compiler->unit);
+    code->stack_size = arana_compiler_get_stack_size(AR_ISIG, compiler->unit);
     return code;
 }
 
     AR consts;
     AR vars;
     struct _ArOp *ops;
-    AR_size_t stack_size;
+    Ar_size_t stack_size;
 } ArCode;
 
 typedef struct _ArCodeUnit {
     register AR arg2;
     register AR arg3;
 
-    /* XXX make it running for the moment.  Figure out the real
-       offset for that stack somehow. */
-    register AR *stack_ptr = frame->stack + 64;
+    register AR *stack_ptr = frame->stack;
 
+    /* if you change the effect an opcode has on the stack size here
+       make sure to also update the arana_compiler_get_stack_size
+       function in the compiler (compile.c) */
     while (1) {
         switch (op->op) {
         /* loading */

arana/interpreter.c

     if (!frm)
         AR_RAISE_OOM();
     frm->code = code;
-    frm->stack_size = 256;
+    frm->stack_size = code->stack_size;
     frm->num_locals = AR_TUPLE_SIZE(code->vars);
     frm->stack = AR_ALLOCTN(AR, frm->stack_size);
     frm->fastlocals = AR_ALLOCTN(AR, frm->num_locals);
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.