예제 #1
0
        static bool HandleCallOperations(MidRepresentationVariables vars, MethodInterpreter interpreter,
                                         ClosureEntities crRuntime, LocalOperation operation, CodeOutput bodySb)
        {
            switch (operation.Kind)
            {
            case OperationKind.Call:
                CppHandleCalls.HandleCall(operation, bodySb, vars, interpreter, crRuntime);
                break;

            case OperationKind.CallInterface:
                CppHandleCalls.HandleCallInterface(operation, bodySb, vars, interpreter, crRuntime);
                break;

            case OperationKind.CallVirtual:
                CppHandleCalls.HandleCallVirtual(operation, bodySb, interpreter, crRuntime);
                break;

            case OperationKind.CallRuntime:
                CppHandleCalls.HandleCallRuntime(operation, bodySb, crRuntime);
                break;

            default:
                return(false);
            }
            return(true);
        }
        private static StringBuilder ComputeBodySb(List <LocalOperation> operations, MidRepresentationVariables vars,
                                                   TypeDescriptionTable typeTable, MethodInterpreter interpreter, ClosureEntities crRuntime)
        {
            var bodySb = new StringBuilder();

            foreach (var operation in operations)
            {
                if (CppHandleOperators.HandleAssignmentOperations(bodySb, operation, operation.Kind, typeTable,
                                                                  interpreter, crRuntime))
                {
                    bodySb.AppendLine();
                    continue;
                }
                switch (operation.Kind)
                {
                case OperationKind.Label:
                    WriteLabel(bodySb, ((Label)operation).JumpTo);
                    break;

                case OperationKind.AlwaysBranch:
                    HandleAlwaysBranchOperator(operation, bodySb);
                    break;

                case OperationKind.BranchOperator:
                    CppHandleBranches.HandleBranchOperator(operation, bodySb);
                    break;

                case OperationKind.Call:
                    CppHandleCalls.HandleCall(operation, bodySb, vars, interpreter, crRuntime);
                    break;

                case OperationKind.CallInterface:
                    CppHandleCalls.HandleCallInterface(operation, bodySb, vars, interpreter, crRuntime);
                    break;

                case OperationKind.CallVirtual:
                    CppHandleCalls.HandleCallVirtual(operation, bodySb, interpreter, crRuntime);
                    break;

                case OperationKind.CallRuntime:
                    CppHandleCalls.HandleCallRuntime(operation, bodySb, crRuntime);
                    break;

                case OperationKind.Return:
                    CppHandleCalls.HandleReturn(operation, bodySb, interpreter);
                    break;

                case OperationKind.CopyArrayInitializer:
                    HandleCopyArrayInitializer(operation, bodySb);
                    break;

                case OperationKind.Switch:
                    HandleSwitch(operation, bodySb);
                    break;

                case OperationKind.Comment:
                    HandleComment(operation.ToString(), bodySb);
                    break;

                case OperationKind.Box:
                    var boxing = crRuntime.FindFeature("Boxing");
                    if (!boxing.IsUsed)
                    {
                        boxing.IsUsed = true;
                        boxing.Declarations.Add(@"template<class T>
struct BoxedT : public System_Object
{
	T Data;
};

template<class T>
std::shared_ptr<System_Object> box_value(T value, int typeId){
	auto result = std::make_shared<BoxedT< T > >();
	result->_typeId = typeId;
	result->Data = value;
	return result;
}

template<class T>
T unbox_value(std::shared_ptr<System_Object> value){
	auto resultObject = value.get();
	auto castedUnboxing = (BoxedT<T>*)resultObject;
	return castedUnboxing->Data;
}");
                    }
                    HandleBox((Boxing)operation, bodySb, typeTable, crRuntime);
                    break;

                case OperationKind.CastClass:
                    HandleCastClass((ClassCasting)operation, bodySb, crRuntime);
                    break;

                case OperationKind.Unbox:
                    HandleUnbox((Unboxing)operation, bodySb, crRuntime);
                    break;

                case OperationKind.IsInstance:

                    HandleIsInstance((IsInstance)operation, bodySb, crRuntime);
                    break;

                default:
                    throw new InvalidOperationException(
                              string.Format(
                                  "Invalid operation '{0}' is introduced in intermediary representation\nValue: {1}",
                                  operation.Kind,
                                  operation));
                }
                bodySb.AppendLine();
            }
            bodySb.AppendLine("}");
            return(bodySb);
        }