/* ILEngineer - Crummy .NET Decompiler
 * 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 "ilengineer/Block.h"

#include "ilengineer/Blocks/ForEach.h"
#include "ilengineer/Blocks/Method.h"

#include "ilengineer/Ops/MSIL/Branch.h"
#include "ilengineer/Ops/MSIL/LoadConstant.h"
#include "ilengineer/Ops/MSIL/LoadElement.h"
#include "ilengineer/Ops/MSIL/LoadLocal.h"
#include "ilengineer/Ops/MSIL/StoreLocal.h"

void ILEngineer::Block::OptimizeForEach() {
	for (StatementVector::iterator stI(m_Statements.begin()); stI != m_Statements.end(); stI++)
		if ((*stI)->isBlock())
			dynamic_cast<Block *>(*stI)->OptimizeForEach();

	for (int i(0); i < ((int) m_Statements.size()) - 4; i++) {
		Statement *st1 = m_Statements[i+0], *st2 = m_Statements[i+1], *st3 = m_Statements[i+2], *st4 = m_Statements[i+3], *st5 = m_Statements[i+4];
		if (!st1->isBlock() && !st2->isBlock() && !st3->isBlock() && !st4->isBlock() && !st5->isBlock()) {
			Operation *op1 = st1->getOperations()[0];
			Operation *op2 = st2->getOperations()[0];
			Operation *op3 = st3->getOperations()[0];
			Operation *op4 = st4->getOperations()[0];
			Operation *op5 = st5->getOperations()[0];

			if (typeid(*op1) == typeid(Ops::MSIL::StoreLocal) &&
				typeid(*op2) == typeid(Ops::MSIL::StoreLocal) &&
				typeid(*op3) == typeid(Ops::MSIL::StoreLocal) &&
				typeid(*op4) == typeid(Ops::MSIL::Branch) &&
				typeid(*op5) == typeid(Ops::MSIL::StoreLocal))
			{
				Ops::MSIL::StoreLocal	*store1 = dynamic_cast<Ops::MSIL::StoreLocal *>(st1->getOperations()[0]);
				Ops::MSIL::StoreLocal	*store2 = dynamic_cast<Ops::MSIL::StoreLocal *>(st2->getOperations()[0]);
				Ops::MSIL::StoreLocal	*store3 = dynamic_cast<Ops::MSIL::StoreLocal *>(st3->getOperations()[0]);
				Ops::MSIL::Branch		*branch = dynamic_cast<Ops::MSIL::Branch *>(st4->getOperations()[0]);
				Ops::MSIL::StoreLocal	*store5 = dynamic_cast<Ops::MSIL::StoreLocal *>(st5->getOperations()[0]);

				Statement::Reference wrap;
				Find(branch->getTarget(), wrap);

				if (typeid(*store3->getValue()) != typeid(Ops::MSIL::LoadI4Constant) ||
					typeid(*store5->getValue()) != typeid(Ops::MSIL::LoadElement) ||
					typeid(*store5->getValue()->getOperations()[0]) != typeid(Ops::MSIL::LoadLocal) ||
					dynamic_cast<Ops::MSIL::LoadI4Constant *>(store3->getValue())->getConstant() != 0 ||
					&m_Statements != wrap.stV)
					continue;

				Blocks::ForEach *foreach = new Blocks::ForEach(m_Method, st1->getOffset(), store1, store5);
				size_t diff = wrap.stI - m_Statements.begin() - (i + 6);
				for (size_t st(0); st < diff; st++) {
					foreach->getStatements().push_back(m_Statements[i+5]);
					m_Statements.erase(m_Statements.begin() + i + 5);
				}

				m_Statements.erase(m_Statements.begin() + i); delete st1;
				m_Statements.erase(m_Statements.begin() + i); delete st2;
				m_Statements.erase(m_Statements.begin() + i); delete st3;
				m_Statements.erase(m_Statements.begin() + i); delete st4;
				m_Statements.erase(m_Statements.begin() + i); delete st5;

				foreach->setEnd(m_Statements[i]->getOffset());
				delete m_Statements[i];
				m_Statements.erase(m_Statements.begin() + i);

				delete m_Statements[i];
				m_Statements[i] = foreach;

				foreach->OptimizeForEach();
			}
		}
	}
}