예제 #1
0
파일: main.c 프로젝트: coderodde/ToyVM
int main(int argc, const char * argv[]) {
    if (argc != 2)
    {
        puts("Usage: toy FILE.brick\n");
        return 0;
    }
    
    FILE* file = fopen(argv[1], "r");
    
    if (!file)
    {
        printf("ERROR: cannot read file \"%s\".", argv[1]);
        return (EXIT_FAILURE);
    }
    
    size_t file_size = getFileSize(file);
    
    TOYVM vm;
    InitializeVM(&vm, 2 * file_size, file_size);
    
    fread(vm.memory, 1, file_size, file);
    fclose(file);

    RunVM(&vm);
    
    if (vm.cpu.status.BAD_ACCESS
        || vm.cpu.status.BAD_INSTRUCTION
        || vm.cpu.status.INVALID_REGISTER_INDEX
        || vm.cpu.status.STACK_OVERFLOW
        || vm.cpu.status.STACK_UNDERFLOW)
    {
        PrintStatus(&vm);
    }
}
예제 #2
0
    bool start_launcher(int argc, TCHAR* argv[]) {
        bool result = false;
        bool parentProcess = true;

        // Platform must be initialize first.
        Platform& platform = Platform::GetInstance();

        try {
            for (int index = 0; index < argc; index++) {
                TString argument = argv[index];

                if (argument == _T("-Xappcds:generatecache")) {
                    platform.SetAppCDSState(cdsGenCache);
                }
                else if (argument == _T("-Xappcds:off")) {
                    platform.SetAppCDSState(cdsDisabled);
                }
                else if (argument == _T("-Xapp:child")) {
                    parentProcess = false;
                }
#ifdef DEBUG
//TODO There appears to be a compiler bug on Mac overloading ShowResponseMessage. Investigate.
                else if (argument == _T("-nativedebug")) {
                    if (platform.ShowResponseMessage(_T("Test"),
                                                     TString(_T("Would you like to debug?\n\nProcessID: ")) +
                                                     PlatformString(platform.GetProcessID()).toString()) == mrOK) {
                        while (platform.IsNativeDebuggerPresent() == false) {
                        }
                    }
                }
#endif //DEBUG
            }

            // Package must be initialized after Platform is fully initialized.
            Package& package = Package::GetInstance();
            Macros::Initialize();
            package.SetCommandLineArguments(argc, argv);
            platform.SetCurrentDirectory(package.GetPackageAppDirectory());

            switch (platform.GetAppCDSState()) {
                case cdsDisabled:
                case cdsUninitialized:
                case cdsEnabled: {
                    break;
                }

                case cdsGenCache: {
                        TString cacheDirectory = package.GetAppCDSCacheDirectory();

                        if (FilePath::DirectoryExists(cacheDirectory) == false) {
                            FilePath::CreateDirectory(cacheDirectory, true);
                        }
                        else {
                            TString cacheFileName = package.GetAppCDSCacheFileName();

                            if (FilePath::FileExists(cacheFileName) == true) {
                                FilePath::DeleteFile(cacheFileName);
                            }
                        }

                        break;
                    }

                case cdsAuto: {
                    TString cacheFileName = package.GetAppCDSCacheFileName();

                    if (parentProcess == true && FilePath::FileExists(cacheFileName) == false) {
                        AutoFreePtr<Process> process = platform.CreateProcess();
                        std::vector<TString> args;
                        args.push_back(_T("-Xappcds:generatecache"));
                        args.push_back(_T("-Xapp:child"));
                        process->Execute(platform.GetModuleFileName(), args, true);

                        if (FilePath::FileExists(cacheFileName) == false) {
                            // Cache does not exist after trying to generate it,
                            // so run without cache.
                            platform.SetAppCDSState(cdsDisabled);
                            package.Clear();
                            package.Initialize();
                        }
                    }

                    break;
                }
            }

            // Validation
            {
                switch (platform.GetAppCDSState()) {
                    case cdsDisabled:
                    case cdsGenCache: {
                        // Do nothing.
                        break;
                    }

                    case cdsEnabled:
                    case cdsAuto: {
                            TString cacheFileName = package.GetAppCDSCacheFileName();

                            if (FilePath::FileExists(cacheFileName) == false) {
                                Messages& messages = Messages::GetInstance();
                                TString message = PlatformString::Format(messages.GetMessage(APPCDS_CACHE_FILE_NOT_FOUND), cacheFileName.data());
                                throw FileNotFoundException(message);
                            }
                            break;
                        }

                    case cdsUninitialized: {
                        throw Exception(_T("Internal Error"));
                }
            }
            }

            // Run App
            result = RunVM();
        }
        catch (FileNotFoundException &e) {
            platform.ShowMessage(e.GetMessage());
        }

        return result;
    }