예제 #1
0
        static CodeOutput ComputeBodySb(List <LocalOperation> operations, MidRepresentationVariables vars,
                                        TypeDescriptionTable typeTable, MethodInterpreter interpreter, ClosureEntities crRuntime)
        {
            var bodySb = new CodeOutput();

            foreach (var operation in operations)
            {
                bodySb.Append("\n");
                if (CppHandleOperators.HandleAssignmentOperations(bodySb, operation, operation.Kind, typeTable,
                                                                  interpreter, crRuntime))
                {
                    continue;
                }
                if (CppCastRelatedOperations.HandleCastRelatedOperations(typeTable, crRuntime, operation, bodySb,
                                                                         operation.Kind))
                {
                    continue;
                }
                if (HandleCallOperations(vars, interpreter, crRuntime, operation, bodySb))
                {
                    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.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;


                default:
                    throw new InvalidOperationException(
                              $"Invalid operation '{operation.Kind}' is introduced in intermediary representation\nValue: {operation}");
                }
            }

            return(bodySb);
        }
        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);
        }