/*
 * The contents of this file are subject to the Netscape Public
 * License Version 1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of
 * the License at http://www.mozilla.org/NPL/

 * Software distributed under the License is distributed on an "AS
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.

 * The Original Code is Rhino code, released
 * May 6, 1999.

 * The Initial Developer of the Original Code is Netscape
 * Communications Corporation.  Portions created by Netscape are
 * Copyright (C) 1997-1999 Netscape Communications Corporation. All
 * Rights Reserved.

 * Contributor(s): 

 * Alternatively, the contents of this file may be used under the
 * terms of the GNU Public License (the "GPL"), in which case the
 * provisions of the GPL are applicable instead of those above.
 * If you wish to allow use of your version of this file only
 * under the terms of the GPL and not to allow others to use your
 * version of this file under the NPL, indicate your decision by
 * deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL.  If you do not delete
 * the provisions above, a recipient may use your version of this
 * file under either the NPL or the GPL.
 */

#ifndef _JS_INTERPRETER_H_
#define _JS_INTERPRETER_H_

#include "js-labeltable.h"
#include "js-interpreterdata.h"
#include "js-interpretedfunction.h"
#include "js-interpretedscript.h"
#include "js-functionnode.h"
#include "js-irfactory.h"

/* Typedefs */
/* Classes */
class JSInterpreter: public JSLabelTable {
    protected:
        jsBool_t        itsInFunctionFlag;
        JSNode::Vector  *itsFunctionList;

        JSInterpreterData
                        *itsData;
        int             itsTryDepth;
        int             itsStackDepth;
        int             itsEpilogLabel;
        jsString_t      itsSourceFile;
        int             itsLineNumber;
        JSInterpretedFunction
                        **itsNestedFunctions;
        int             itsNestedFunctionsLength;
        int             version;
#ifdef MEASURMENT
        static  int     countiCode[148];
#endif

   public:
        static  const jsBool_t  printICode = FALSE;
        static  const jsVal_t   zero = JS_INT_TO_VAL(0);
        static  const jsVal_t   one = JS_INT_TO_VAL(1);


    protected:
        static jsString_t
                        getString(jsString_t *theStringTable, int8_t *iCode,
                                int pc);
        static double   getNumber(double *theNumberTable, int8_t *iCode,
                                int pc);
        static int      getTarget(int8_t *iCode, int pc);
        static void     dumpICode(JSContext *cx, JSInterpreterData *theData);
        static void     createFunctionObject(JSContext *cx,
                                JSInterpretedFunction *fn,
                                JSScriptable *scope);
#ifdef TEMPVAL
        static jsVal_t  normalizeVal(JSContext *cx, JSFrame *frame,
                                jsVal_t val);
        static void     storeToStack(JSFrame *frame, int index, double d);
        static void     storeToStack(JSFrame *frame, int index, jsVal_t val);
        static void     storeToLocal(JSFrame *frame, int index, jsVal_t val);
        static void     storeToVars(JSFrame *frame, int index, double d);
        static void     storeToVars(JSFrame *frame, int index, jsVal_t val);
        static void     loadFromLocal(JSFrame *frame, int stackIndex,
                                int index);
        static void     loadFromVars(JSFrame *frame, int stackIndex,
                                int index);
#endif
#ifdef BRANCH
        static  void    checkBranch(JSContext *cx, int displacement);
#endif
        void    generateICodeFromTree(JSContext *cx, JSNode *tree,
                        JSVariableTable *varTable, jsBool_t needsActivation);
        jsVal_t *
                generateRegExpLiterals(JSContext *cx, JSScriptable *scope,
                        JSNode::Vector *regexps, int *regExpsLength);
        JSInterpretedScript *
                generateScriptICode(JSContext *cx, JSScriptable *scope,
                        JSNode *tree);
        void    generateNestedFunctions(JSScriptable *scope, JSContext *cx);
        JSInterpretedFunction *
                generateFunctionICode(JSContext *cx, JSScriptable *scope,
                        JSFunctionNode *theFunction);
        int     updateLineNumber(JSContext *cx, JSNode *node, int iCodeTop);
        void    badTree(JSContext *cx, JSNode *node);
        int     generateICode(JSContext *cx, JSNode *node, int iCodeTop);
        int     addLocalRef(JSContext *cx, JSNode *node, int iCodeTop);
#ifdef REUSE_LOCAL
        void    delLocalRef(JSContext *cx, JSNode *node);
#endif
        int     addEnumRef(JSContext *cx, JSNode *node, int iCodeTop);
        int     addGoto(JSContext *cx, JSNode *node, int gotoOp, int iCodeTop);
        int     addGoto(JSContext *cx, int targetLabel, int gotoOp,
                        int iCodeTop);
        int     addByte(JSContext *cx, int8_t b, int iCodeTop);
        int     addString(JSContext *cx, jsString_t str, int iCodeTop);
        int     addNumber(JSContext *cx, double num, int iCodeTop);

        void    adjustGCMemory(JSContext *cx);


    public:
        static  jsVal_t interpret(JSContext *cx, JSScriptable *scope,
                                JSScriptable *thisObj, int numArgs,
                                jsVal_t *args, JSInterpreterData *theData);

        JSInterpreter();
        virtual ~JSInterpreter() {
        }

        JSIRFactory *
                createIRFactory(JSContext *cx, JSTokenStream *ts,
                        JSScriptable *scope);
        JSNode *
                transform(JSContext *cx, JSNode *tree, JSTokenStream *ts,
                        JSScriptable *scope);
        JSNativeFunction *
                compile(JSContext *cx, JSScriptable *scope, JSNode *tree);

        /* gc methods */
        void    mark(JSContext *cx);
#ifdef MEASURENT
        static void    bytecodeCountShow();
        static void    bytecodeCountReset();
#endif
        int getIcodeMemSize();
} ;

#endif /* _JS_INTERPRETER_H_ */

