/* 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/Method.h"

#include "ilengineer/Ops/MSIL/Branch.h"
#include "ilengineer/Ops/MSIL/BranchFalse.h"
#include "ilengineer/Ops/MSIL/BranchTrue.h"

#include "ilengineer/Ops/ILE/LogicAnd.h"
#include "ilengineer/Ops/ILE/LogicOr.h"

// Yes, I know this code is redundant.
// Yes, I know redudant is bad.

void ILEngineer::Block::OptimizeBranch() {
	m_Method->UpdateTargets();
	for (StatementVector::iterator stI(m_Statements.begin()); stI != m_Statements.end(); stI++)
		if ((*stI)->isBlock())
			dynamic_cast<Block *>(*stI)->OptimizeBranch();

	for (int i(0); i < ((int) m_Statements.size() - 1); i++) {
		Statement *st1 = m_Statements[i], *st2 = m_Statements[i + 1];
		if (!st1->isBlock() && !st2->isBlock()) {
			Operation *op1 = st1->getOperations()[0];
			Operation *op2 = st2->getOperations()[0];

			if (typeid(*op1) == typeid(Ops::MSIL::Branch)) {
				Ops::MSIL::Branch *branch = dynamic_cast<Ops::MSIL::Branch *>(op1);
				if (!st1->isTarget() && branch->getTarget() == st2->getOffset()) {
					m_Statements.erase(m_Statements.begin() + i--);
					delete st1;
				}
			} else if (typeid(*op2) != typeid(Ops::MSIL::Branch)) {
				Ops::MSIL::CheckedBranch *branch1 = dynamic_cast<Ops::MSIL::CheckedBranch *>(op1);
				Ops::MSIL::CheckedBranch *branch2 = dynamic_cast<Ops::MSIL::CheckedBranch *>(op2);

				if (branch1 != NULL && branch2 != NULL) {
                    //   These warnings will not go away.  I fail to understand
                    // why an == operator on a C++-centric object like a typeinfo
                    // would return an integer and not a boolean.  The only thing
                    // I can think of is reinterpret_cast<>ing their addresses to
                    // bool*'s and indirecting those... but that's just silly :).
					bool not1 = (bool) (typeid(*op1) == typeid(Ops::MSIL::BranchFalse));
					bool not2 = (bool) (typeid(*op2) == typeid(Ops::MSIL::BranchFalse));

					if (i != ((int) m_Statements.size() - 2) && branch1->getTarget() == m_Statements[i + 2]->getOffset()) {
						Operation *value1 = new Ops::ILE::LogicNot(m_Method, branch1->getValue());
						if (not1) value1 = new Ops::ILE::LogicNot(m_Method, value1);

						Operation *value2 = branch2->getValue();
						if (not2) value2 = new Ops::ILE::LogicNot(m_Method, value2);

						m_Statements[i] = new Statement(m_Method, st1->getOffset(), new Ops::MSIL::BranchTrue(m_Method, branch1->getOffset(), new Ops::ILE::LogicAnd(m_Method, value1, value2), branch2->getTarget()));
						m_Statements.erase(m_Statements.begin() + i + 1);

						branch1->toTrash(); delete st1;
						branch2->toTrash(); delete st2;
						i = 0;
					} else if (!st2->isTarget() && branch1->getTarget() == branch2->getTarget()) {
						Operation *value1 = branch1->getValue();
						if (not1) value1 = new Ops::ILE::LogicNot(m_Method, value1);

						Operation *value2 = branch2->getValue();
						if (not2) value2 = new Ops::ILE::LogicNot(m_Method, value2);

						m_Statements[i] = new Statement(m_Method, st1->getOffset(), new Ops::MSIL::BranchTrue(m_Method, branch1->getOffset(), new Ops::ILE::LogicOr(m_Method, value1, value2), branch2->getTarget()));
						m_Statements.erase(m_Statements.begin() + i + 1);

						branch1->toTrash(); delete st1;
						branch2->toTrash(); delete st2;
						i = 0;
					}
				}
			}

			/*if (typeid(*op1) == typeid(Ops::MSIL::BranchTrue)) {
				Ops::MSIL::BranchTrue *branch1 = dynamic_cast<Ops::MSIL::BranchTrue *>(op1);
				if (typeid(*op2) == typeid(Ops::MSIL::BranchTrue)) {
					Ops::MSIL::BranchTrue *branch2 = dynamic_cast<Ops::MSIL::BranchTrue *>(op2);
					if (i != m_Statements.size() - 2 && branch1->getTarget() == m_Statements[i + 2]->getOffset()) {
						m_Statements[i] = new Statement(m_Method, st1->getOffset(), new Ops::MSIL::BranchTrue(m_Method, branch1->getOffset(), new Ops::ILE::LogicAnd(m_Method, new Ops::ILE::LogicNot(m_Method, branch1->getValue()), branch2->getValue()), branch2->getTarget()));
						m_Statements.erase(m_Statements.begin() + i + 1);

						branch1->toTrash(); delete st1;
						branch2->toTrash(); delete st2;
						i = 0;
					} else if (!st2->isTarget() && branch1->getTarget() == branch2->getTarget()) {
						m_Statements[i] = new Statement(m_Method, st1->getOffset(), new Ops::MSIL::BranchTrue(m_Method, branch1->getOffset(), new Ops::ILE::LogicOr(m_Method, branch1->getValue(), branch2->getValue()), branch2->getTarget()));
						m_Statements.erase(m_Statements.begin() + i + 1);

						branch1->toTrash(); delete st1;
						branch2->toTrash(); delete st2;
						i = 0;
					}
				} else if (typeid(*op2) == typeid(Ops::MSIL::BranchFalse)) {
					Ops::MSIL::BranchFalse *branch2 = dynamic_cast<Ops::MSIL::BranchFalse *>(op2);
					if (i != m_Statements.size() - 2 && branch1->getTarget() == m_Statements[i + 2]->getOffset()) {
						m_Statements[i] = new Statement(m_Method, st1->getOffset(), new Ops::MSIL::BranchFalse(m_Method, branch1->getOffset(), new Ops::ILE::LogicOr(m_Method, branch1->getValue(), branch2->getValue()), branch2->getTarget()));
						m_Statements.erase(m_Statements.begin() + i + 1);

						branch1->toTrash(); delete st1;
						branch2->toTrash(); delete st2;
						i = 0;
					} else if (!st2->isTarget() && branch1->getTarget() == branch2->getTarget()) {
						m_Statements[i] = new Statement(m_Method, st1->getOffset(), new Ops::MSIL::BranchTrue(m_Method, branch1->getOffset(), new Ops::ILE::LogicOr(m_Method, branch1->getValue(), new Ops::ILE::LogicNot(m_Method, branch2->getValue())), branch2->getTarget()));
						m_Statements.erase(m_Statements.begin() + i + 1);

						branch1->toTrash(); delete st1;
						branch2->toTrash(); delete st2;
						i = 0;
					}
				}
			} else if (typeid(*op1) == typeid(Ops::MSIL::BranchFalse)) {
				Ops::MSIL::BranchFalse *branch1 = dynamic_cast<Ops::MSIL::BranchFalse *>(op1);
				if (typeid(*op2) == typeid(Ops::MSIL::BranchTrue)) {
					Ops::MSIL::BranchTrue *branch2 = dynamic_cast<Ops::MSIL::BranchTrue *>(op2);
					if (i != m_Statements.size() - 2 && branch1->getTarget() == m_Statements[i + 2]->getOffset()) {
						m_Statements[i] = new Statement(m_Method, st1->getOffset(), new Ops::MSIL::BranchTrue(m_Method, branch1->getOffset(), new Ops::ILE::LogicAnd(m_Method, branch1->getValue(), branch2->getValue()), branch2->getTarget()));
						m_Statements.erase(m_Statements.begin() + i + 1);

						branch1->toTrash(); delete st1;
						branch2->toTrash(); delete st2;
						i = 0;
					} else if (!st2->isTarget() && branch1->getTarget() == branch2->getTarget()) {
						m_Statements[i] = new Statement(m_Method, st1->getOffset(), new Ops::MSIL::BranchTrue(m_Method, branch1->getOffset(), new Ops::ILE::LogicOr(m_Method, new Ops::ILE::LogicNot(m_Method, branch1->getValue()), branch2->getValue()), branch2->getTarget()));
						m_Statements.erase(m_Statements.begin() + i + 1);

						branch1->toTrash(); delete st1;
						branch2->toTrash(); delete st2;
						i = 0;
					}
				} else if (typeid(*op2) == typeid(Ops::MSIL::BranchFalse)) {
					Ops::MSIL::BranchFalse *branch2 = dynamic_cast<Ops::MSIL::BranchFalse *>(op2);
					if (i != m_Statements.size() - 2 && branch1->getTarget() == m_Statements[i + 2]->getOffset()) {
						m_Statements[i] = new Statement(m_Method, st1->getOffset(), new Ops::MSIL::BranchFalse(m_Method, branch1->getOffset(), new Ops::ILE::LogicOr(m_Method, new Ops::ILE::LogicNot(m_Method, branch1->getValue()), branch2->getValue()), branch2->getTarget()));
						m_Statements.erase(m_Statements.begin() + i + 1);

						branch1->toTrash(); delete st1;
						branch2->toTrash(); delete st2;
						i = 0;
					} else if (!st2->isTarget() && branch1->getTarget() == branch2->getTarget()) {
						m_Statements[i] = new Statement(m_Method, st1->getOffset(), new Ops::MSIL::BranchFalse(m_Method, branch1->getOffset(), new Ops::ILE::LogicAnd(m_Method, branch1->getValue(), branch2->getValue()), branch2->getTarget()));
						m_Statements.erase(m_Statements.begin() + i + 1);

						branch1->toTrash(); delete st1;
						branch2->toTrash(); delete st2;
						i = 0;
					}
				}
			} else if (typeid(*op1) == typeid(Ops::MSIL::Branch)) {
				Ops::MSIL::Branch *branch = dynamic_cast<Ops::MSIL::Branch *>(op1);
				if (!st1->isTarget() && branch->getTarget() == st2->getOffset()) {
					m_Statements.erase(m_Statements.begin() + i--);
					delete st1;
				}
			}*/
		}
	}
}