/* Theoretic - Graph Theoretic Byte Code Engineering
 * Copyright (C) 2001-2002  Jay Freeman (saurik)
*/

/*
 *        Redistribution and use in source and binary
 * forms, with or without modification, are permitted
 * provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the
 *    above copyright notice, this list of conditions
 *    and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the
 *    above copyright notice, this list of conditions
 *    and the following disclaimer in the documentation
 *    and/or other materials provided with the
 *    distribution.
 * 3. The name of the author may not be used to endorse
 *    or promote products derived from this software
 *    without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "stdafx.h"

#include "theoretic/Instruction.h"

namespace Theoretic {

Instruction::Control::Control(uint32_t offset, std::wstring name, bool adjust) :
	offset(offset), name(name), adjust(adjust)
{}

Instruction::~Instruction() {
}

/*void Instruction::Resolve(const OffsetMap &offsets) {
	for (ControlList::iterator i_control = control.begin(); i_control != control.end(); ++i_control) {
		uint32_t target = i_control->offset;
		if (i_control->adjust) target += width;
		OffsetMap::const_iterator off = offsets.find(target);
		links_.insert(LinkMap::value_type(i_control->name, off->second));
	}

	control.clear();
}*/

int32_t Instruction::GetInteger(const XalanDOMString &value) {
	return value.compare(0, 1, L"$") ? _wtoi(value.c_str()) : args_[value.substr(1).c_str()]->num();
}

/*void Instruction::Read(XalanNode *where, uint8_t * &data) {
	for (XalanNode *cmd = where->getFirstChild(); cmd != NULL; cmd = cmd->getNextSibling()) {
		std::wstring cname = cmd->getNodeName().c_str();
		const XalanNamedNodeMap *attrs = cmd->getAttributes();

		if (cname == L"arg") {
			std::wstring key = attrs->getNamedItem(XalanDOMString(L"name"))->getNodeValue().c_str();

			uint32_t value = engine.ParseValue(attrs->getNamedItem(XalanDOMString(L"type"))->getNodeValue().c_str(), data);
			args.insert(ArgPair(key, value));

			XalanNode *branch = attrs->getNamedItem(XalanDOMString(L"branch"));
			if (branch != NULL)
				control.push_back(Control(start + value, key, branch->getNodeValue() == L"post"));
		} else if (cname == L"multi") {
			int first = GetInteger(attrs->getNamedItem(XalanDOMString(L"start"))->getNodeValue());
			int last = GetInteger(attrs->getNamedItem(XalanDOMString(L"end"))->getNodeValue());

			for (int i(first); i < last; i++) {
				Read(cmd, data); // XXX: This needs a prefix or something...
			}
		}
	}
}*/

Instruction::Instruction(XalanNode *op, uint32_t start, uint8_t * &data, IEngine &engine) :
	start(start),
	engine(engine)
{
	const XalanNamedNodeMap *attrs = op->getAttributes();
	name = attrs->getNamedItem(XalanDOMString(L"name"))->getNodeValue().c_str();

	uint8_t *begin = data;
	// XXX! Read(op, data);

	width = data - begin;
	uint32_t end(start + width);

	if (attrs->getNamedItem(XalanDOMString(L"stop")) == NULL)
		control.push_back(Control(end, L"next", false));

	/*for (int i(start); i < end; ++i)
		bytes.insert(i);*/
}

}