/*
 * 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.
 */
/* Header file */
#include "js-runtimeexception.h"
#include "js-exceptionhandler.h"
#include "js-label.h"

/* Procedures */
short
 JSLabel::getPC()
{
    return itsPC;
}

void JSLabel::fixGotos(JSContext * cx, int8_t * theCodeBuffer)
{
#ifdef VX
    if (_DEBUG) {
#else
    if (DEBUG) {
#endif
        if ((itsPC == -1) && (itsFixupTable != NULL)) {
            cx->exceptionHandler->throwEx(new(cx) JSRuntimeException("Unlocated label"));
        }
    }

    if (itsFixupTable != NULL) {
        for (int i = 0; i < itsFixupTableTop; i++) {
            int fixupSite = itsFixupTable[i];
            // -1 to get delta from instruction start
            short offset = (short)(itsPC - (fixupSite - 1));

            theCodeBuffer[fixupSite++] = (int8_t) (offset >> 8);
            theCodeBuffer[fixupSite] = (int8_t) offset;
        }
        JS_DeleteZoneObj(cx, itsFixupTable);
    }
    itsFixupTable = NULL;
}
void JSLabel::setPC(JSContext * cx, short thePC)
{
#ifdef VX
    if (_DEBUG) {
#else
    if (DEBUG) {
#endif
        if ((itsPC != -1) && (itsPC != thePC)) {
            cx->exceptionHandler->throwEx(new(cx) JSRuntimeException("Duplicate label"));
        }
    }
    itsPC = thePC;
}
void JSLabel::addFixup(JSContext * cx, int fixupSite)
{
    if (itsFixupTable == NULL) {
        itsFixupTableTop = 1;
        itsFixupTableLength = FIXUPTABLE_SIZE;
        itsFixupTable = (int *)JS_NewZoneObj(cx, sizeof(*itsFixupTable)
                * FIXUPTABLE_SIZE);
        itsFixupTable[0] = fixupSite;
    }
    else {
        if (itsFixupTableTop == itsFixupTableLength) {
            int oldLength = itsFixupTableLength;
            int *newTable = (int *)JS_NewZoneObj(cx, sizeof(*newTable)
                    * (oldLength + FIXUPTABLE_SIZE));
            memcpy(newTable, itsFixupTable, sizeof(*newTable) * oldLength);
            JS_DeleteZoneObj(cx, itsFixupTable);
            itsFixupTable = newTable;
            itsFixupTableLength = oldLength + FIXUPTABLE_SIZE;
        }
        itsFixupTable[itsFixupTableTop++] = fixupSite;
    }
}
void JSLabel::destroy(JSContext * cx)
{
    if (itsFixupTable != NULL) {
        JS_DeleteZoneObj(cx, itsFixupTable);
    }
    JSZoneNew::destroy(cx);
}

JSLabel::JSLabel()
{
    itsFixupTableLength = 0;
    itsFixupTable = NULL;
    itsFixupTableTop = 0;
    itsPC = -1;
}
