Пример #1
0
/*
0:  DUM 1
1:  LDF factorial=5
2:  LDF main=20
3:  RAP 1
4:  RTN
factorial=5: ; (x)
5:  LD 0 0 ; push x
6:  LDC 1
7:  CEQ
8:  SEL one=10 notOne=12
9:  RTN
one=10:
10:  LDC 1 ; return 1
11: JOIN
notOne=12:
12: LD 0 0 ; push x
13: LD 0 0
14: LDC 1
15: SUB ; push x-1
16: LD 1 0 ; push factorial
17: AP 1 ; push factorial(x-1)
18: MUL ; push x*factorial(x-1)
19: JOIN
main=20:
20: LDC 7
21: LD 0 0 ; push factorial
22: AP 1
23: RTN
*/
int main() {
  std::vector<Instruction> prog;
  prog.push_back(Instruction(DUM, 1));
  prog.push_back(Instruction(LDF, 5));
  prog.push_back(Instruction(LDF, 20));
  prog.push_back(Instruction(RAP, 1));
  prog.push_back(Instruction(RTN));
// factorial=5: ; (x)
  prog.push_back(Instruction(LD, 0, 0));
  prog.push_back(Instruction(LDC, 1));
  prog.push_back(Instruction(CEQ));
  prog.push_back(Instruction(SEL, 10, 12));
  prog.push_back(Instruction(RTN));
// one=10:
  prog.push_back(Instruction(LDC, 1));
  prog.push_back(Instruction(JOIN));
// notOne=12:
  prog.push_back(Instruction(LD, 0, 0));
  prog.push_back(Instruction(LD, 0, 0));
  prog.push_back(Instruction(LDC, 1));
  prog.push_back(Instruction(SUB));
  prog.push_back(Instruction(LD, 1, 0));
  prog.push_back(Instruction(AP, 1));
  prog.push_back(Instruction(MUL));
  prog.push_back(Instruction(JOIN));
// main=20:
  prog.push_back(Instruction(LDC, 7));
  prog.push_back(Instruction(LD, 0, 0));
  prog.push_back(Instruction(AP, 1));
  prog.push_back(Instruction(RTN));
  GccInterpreter gcc(prog);
  std::cout << gcc.runMain() << std::endl;
  gcc.gc.collect();
}
Пример #2
0
main()
{
	borland();
	gcc();
	msc();
	sunpro();
	unknown();
	exit(0);
}
Пример #3
0
CompilerPtr CompilerLocatorMinGW::Locate(const wxString& folder)
{
    m_compilers.clear();
    wxFileName gcc(folder, "gcc");
#ifdef __WXMSW__
    gcc.SetExt("exe");
#endif

    bool found = gcc.FileExists();
    if ( ! found ) {
        // try to see if we have a bin folder here
        gcc.AppendDir("bin");
        found = gcc.FileExists();
    }
    
    if ( found ) {
        AddTools(gcc.GetPath(), GetGCCVersion(gcc.GetFullPath() ));
        return *m_compilers.begin();
    }
    return NULL;
}
CompilerPtr CompilerLocatorGCC::Locate(const wxString& folder)
{
    m_compilers.clear();
    wxFileName gcc(folder, "gcc");
    wxFileName tmpfn(folder, "");
    
    wxString name;
    if( tmpfn.GetDirCount() > 1 && tmpfn.GetDirs().Last() == "bin" ) {
        tmpfn.RemoveLastDir();
        name = tmpfn.GetDirs().Last();
    }
    
#ifdef __WXMSW__
    gcc.SetExt("exe");
#endif

    bool found = gcc.FileExists();
    if ( ! found ) {
        // try to see if we have a bin folder here
        gcc.AppendDir("bin");
        found = gcc.FileExists();
    }
    
    if ( found ) {
        CompilerPtr compiler( new Compiler(NULL) );
        compiler->SetCompilerFamily(COMPILER_FAMILY_GCC);
        
        // get the compiler version
        compiler->SetName( name.IsEmpty() ? "GCC" : name );
        compiler->SetGenerateDependeciesFile(true);
        m_compilers.push_back( compiler );
        
        // we path the bin folder
        AddTools(compiler, gcc.GetPath());
        return compiler;
    }
    return NULL;
}
Пример #5
0
CompilerPtr CompilerLocatorCygwin::Locate(const wxString& folder)
{
    m_compilers.clear();
    
    wxString binFolder;
    wxFileName gcc(folder, "gcc.exe");
    if ( gcc.FileExists() ) {
        binFolder = gcc.GetPath();
    } else {
        gcc.AppendDir("bin");
        if ( gcc.FileExists() ) {
            binFolder = gcc.GetPath();
        }
    }
    
    if ( binFolder.IsEmpty() )
        return NULL;
    
    wxArrayString suffixes = GetSuffixes(binFolder);
    if ( suffixes.IsEmpty() )
        return NULL;
    
    for(size_t i=0; i<suffixes.GetCount(); ++i) {
        gcc.SetFullName( "gcc-" + suffixes.Item(i) + ".exe" );
        wxString gccVer = GetGCCVersion( gcc.GetFullPath() );

        wxString compilerName;
        compilerName << "Cygwin";
        if ( !gccVer.IsEmpty() ) {
            compilerName <<  " - " << gccVer;
        }
        // Add the tools (use the bin folder)
        AddTools(gcc.GetPath(), compilerName, suffixes.Item(i));
    }
    return m_compilers.at(0);
}
Пример #6
0
bool CompilerLocatorMinGW::Locate()
{   
    m_compilers.clear();
    m_locatedFolders.clear();
    
    // for wxRegKey
#ifdef __WXMSW__ 
    
    {
        // HKEY_LOCAL_MACHINE\SOFTWARE\codelite\settings
        wxRegKey regClMinGW(wxRegKey::HKLM, "SOFTWARE\\codelite\\settings");
        wxString clInstallFolder;
        if ( regClMinGW.QueryValue("MinGW", clInstallFolder) && wxDirExists(clInstallFolder)) {
            wxFileName gccExe(clInstallFolder, "gcc.exe");
            wxString ver;
            regClMinGW.QueryValue("MinGW_Version", ver);
            gccExe.AppendDir("bin");
            if ( gccExe.FileExists() ) {
                AddTools(gccExe.GetPath(), "CodeLite-" + ver);
            }
        }
    }
    
    {
        // HKEY_LOCAL_MACHINE\SOFTWARE\codelite\settings
        wxRegKey regClMinGW(wxRegKey::HKLM, "SOFTWARE\\Wow6432Node\\codelite\\settings");
        wxString clInstallFolder;
        if ( regClMinGW.QueryValue("MinGW", clInstallFolder) && wxDirExists(clInstallFolder)) {
            wxFileName gccExe(clInstallFolder, "gcc.exe");
            wxString ver;
            regClMinGW.QueryValue("MinGW_Version", ver);
            gccExe.AppendDir("bin");
            if ( gccExe.FileExists() ) {
                AddTools(gccExe.GetPath(), "CodeLite-" + ver);
            }
        }
    }
    // Check registry for TDM-GCC-64 
    wxRegKey regTDM(wxRegKey::HKCU, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\TDM-GCC");
    wxString tdmInstallFolder;
    tdmInstallFolder.Clear();
    if ( regTDM.QueryValue("InstallLocation", tdmInstallFolder) && wxFileName::DirExists(tdmInstallFolder)) {
        wxFileName fnTDMBinFolder( tdmInstallFolder, "" );
        fnTDMBinFolder.AppendDir("bin");
        AddTools(fnTDMBinFolder.GetPath(), "TDM-GCC-64");
    }
    
    // Check for 32 bit
    wxRegKey regTDM_32(wxRegKey::HKLM, "SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\TDM-GCC");
    tdmInstallFolder.Clear();
    if ( regTDM_32.QueryValue("InstallLocation", tdmInstallFolder) && wxFileName::DirExists(tdmInstallFolder)) {
        wxFileName fnTDMBinFolder( tdmInstallFolder, "" );
        fnTDMBinFolder.AppendDir("bin");
        AddTools(fnTDMBinFolder.GetPath(), "TDM-GCC-32");
    }
    
    // locate codeblock's MinGW
    wxRegKey regCB(wxRegKey::HKCU, "SOFTWARE\\CodeBlocks");
    wxString cbInstallPath;
    if ( regCB.QueryValue("Path", cbInstallPath) ) {
        wxFileName mingwBinFolder( cbInstallPath, "" );
        mingwBinFolder.AppendDir("MinGW");
        mingwBinFolder.AppendDir("bin");
        if ( mingwBinFolder.DirExists() && wxFileName(mingwBinFolder.GetFullPath(), "gcc.exe").FileExists() ) {
            AddTools(mingwBinFolder.GetPath(), "Code::Blocks");
        }
    }
    
    // Last: many people install MinGW by simply extracting it into the 
    // root folder:
    // C:\MinGW-X.Y.Z
    wxArrayString volumes = wxFSVolume::GetVolumes();
    wxArrayString mingwFolderArr;
    // Get list of folders for the volume only
    for(size_t i=0; i<volumes.GetCount(); ++i) {
        wxDir dir( volumes.Item(i) );
        if ( dir.IsOpened() ) {
            wxString path;
            bool cont = dir.GetFirst(&path, "*mingw*", wxDIR_DIRS);
            while (cont ) {
                wxString fullpath;
                fullpath << volumes.Item(i) << path;
                CL_DEBUG("Found folder containing MinGW: %s", fullpath);
                mingwFolderArr.Add( fullpath );
                cont = dir.GetNext( &path );
            }
        }
    }
    
    for(size_t i=0; i<mingwFolderArr.GetCount(); ++i) {
        wxString binFolder = FindBinFolder( mingwFolderArr.Item(i) );
        if ( binFolder.IsEmpty() )
            continue;
        
        wxFileName gcc(binFolder, "gcc.exe");
        if( gcc.FileExists() ) {
            AddTools(gcc.GetPath());
        }
    }
#endif
    
    // try to find MinGW in environment variable PATH (last)
    wxString pathValues;
    wxGetEnv("PATH", &pathValues);

    if ( !pathValues.IsEmpty() ) {
        wxArrayString pathArray = ::wxStringTokenize(pathValues, wxPATH_SEP, wxTOKEN_STRTOK);
        for (size_t i = 0; i < pathArray.GetCount(); ++i) {
            wxFileName gccComp( pathArray.Item(i), "gcc.exe" );
            if ( gccComp.GetDirs().Last() == "bin" && gccComp.Exists() ) {
                // We found gcc.exe
                wxString pathToGcc = gccComp.GetPath();
                pathToGcc.MakeLower();
                
                // Don't mix cygwin and mingw
                if ( !pathToGcc.Contains("cygwin") ) {
                    AddTools( gccComp.GetPath() );
                }
            }
        }
    }

    return !m_compilers.empty();
}
Пример #7
0
static int linkObjToBinaryGcc(bool sharedLib)
{
    Logger::println("*** Linking executable ***");

    // find gcc for linking
    std::string gcc(getGcc());

    // build arguments
    std::vector<std::string> args;

    // object files
    for (unsigned i = 0; i < global.params.objfiles->dim; i++)
    {
        const char *p = static_cast<const char *>(global.params.objfiles->data[i]);
        args.push_back(p);
    }

    // user libs
    for (unsigned i = 0; i < global.params.libfiles->dim; i++)
    {
        const char *p = static_cast<const char *>(global.params.libfiles->data[i]);
        args.push_back(p);
    }

    // output filename
    std::string output = getOutputName(sharedLib);

    if (sharedLib)
        args.push_back("-shared");

    args.push_back("-o");
    args.push_back(output);

    // set the global gExePath
    gExePath = output;
    //assert(gExePath.isValid());

    // create path to exe
    CreateDirectoryOnDisk(gExePath);

#if LDC_LLVM_VER >= 303
    // Pass sanitizer arguments to linker. Requires clang.
    if (opts::sanitize == opts::AddressSanitizer) {
        args.push_back("-fsanitize=address");
    }

    if (opts::sanitize == opts::MemorySanitizer) {
        args.push_back("-fsanitize=memory");
    }

    if (opts::sanitize == opts::ThreadSanitizer) {
        args.push_back("-fsanitize=thread");
    }
#endif

    // additional linker switches
    for (unsigned i = 0; i < global.params.linkswitches->dim; i++)
    {
        const char *p = static_cast<const char *>(global.params.linkswitches->data[i]);
        // Don't push -l and -L switches using -Xlinker, but pass them indirectly
        // via GCC. This makes sure user-defined paths take precedence over
        // GCC's builtin LIBRARY_PATHs.
        if (!p[0] || !(p[0] == '-' && (p[1] == 'l' || p[1] == 'L')))
            args.push_back("-Xlinker");
        args.push_back(p);
    }

    // default libs
    bool addSoname = false;
    switch (global.params.targetTriple.getOS()) {
    case llvm::Triple::Linux:
        addSoname = true;
        args.push_back("-lrt");
        if (!opts::disableLinkerStripDead) {
            args.push_back("-Wl,--gc-sections");
        }
        // fallthrough
    case llvm::Triple::Darwin:
    case llvm::Triple::MacOSX:
        args.push_back("-ldl");
        // fallthrough
    case llvm::Triple::FreeBSD:
        addSoname = true;
        args.push_back("-lpthread");
        args.push_back("-lm");
        break;

    case llvm::Triple::Solaris:
        args.push_back("-lm");
        args.push_back("-lumem");
        // solaris TODO
        break;

#if LDC_LLVM_VER < 305
    case llvm::Triple::MinGW32:
        // This is really more of a kludge, as linking in the Winsock functions
        // should be handled by the pragma(lib, ...) in std.socket, but it
        // makes LDC behave as expected for now.
        args.push_back("-lws2_32");
        break;
#endif

    default:
        // OS not yet handled, will probably lead to linker errors.
        // FIXME: Win32.
        break;
    }

#if LDC_LLVM_VER >= 305
    if (global.params.targetTriple.isWindowsGNUEnvironment())
    {
        // This is really more of a kludge, as linking in the Winsock functions
        // should be handled by the pragma(lib, ...) in std.socket, but it
        // makes LDC behave as expected for now.
        args.push_back("-lws2_32");
    }
#endif

    // Only specify -m32/-m64 for architectures where the two variants actually
    // exist (as e.g. the GCC ARM toolchain doesn't recognize the switches).
    // MIPS does not have -m32/-m64 but requires -mabi=.
    if (global.params.targetTriple.get64BitArchVariant().getArch() !=
        llvm::Triple::UnknownArch &&
        global.params.targetTriple.get32BitArchVariant().getArch() !=
        llvm::Triple::UnknownArch) {
        if (global.params.targetTriple.get64BitArchVariant().getArch() ==
            llvm::Triple::mips64 ||
            global.params.targetTriple.get64BitArchVariant().getArch() ==
            llvm::Triple::mips64el) {
            switch (getMipsABI())
            {
                case MipsABI::EABI:
                    args.push_back("-mabi=eabi");
                    break;
                case MipsABI::O32:
                    args.push_back("-mabi=32");
                    break;
                case MipsABI::N32:
                    args.push_back("-mabi=n32");
                    break;
                case MipsABI::N64:
                    args.push_back("-mabi=64");
                    break;
                case MipsABI::Unknown:
                    break;
            }
        }
        else {
            if (global.params.is64bit)
                args.push_back("-m64");
            else
                args.push_back("-m32");
        }
    }

    if (opts::createSharedLib && addSoname) {
        std::string soname = opts::soname;
        if (!soname.empty()) {
            args.push_back("-Wl,-soname," + soname);
        }
    }

    Logger::println("Linking with: ");
    std::vector<std::string>::const_iterator I = args.begin(), E = args.end();
    Stream logstr = Logger::cout();
    for (; I != E; ++I)
        if (!(*I).empty())
            logstr << "'" << *I << "'" << " ";
    logstr << "\n"; // FIXME where's flush ?

    // try to call linker
    return executeToolAndWait(gcc, args, global.params.verbose);
}
Пример #8
0
bool CompilerLocatorMinGW::Locate()
{
    // try to find MinGW in environment variable PATH first
    wxString pathValues;
    wxGetEnv("PATH", &pathValues);

    if ( !pathValues.IsEmpty() ) {
        wxArrayString pathArray = ::wxStringTokenize(pathValues, wxPATH_SEP, wxTOKEN_STRTOK);
        for (size_t i = 0; i < pathArray.GetCount(); ++i) {
            wxFileName gccComp( pathArray.Item(i), "gcc.exe" );
            if ( gccComp.GetDirs().Last() == "bin" && gccComp.Exists() ) {
                // We found gcc.exe
                CompilerPtr compiler( new Compiler(NULL) );
                m_compilers.push_back( compiler );
                AddTools( compiler, gccComp.GetPath() );
            }
        }
    }

#ifdef __WXMSW__ // for wxRegKey
    // Check registry for TDM-GCC
    // HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\TDM-GCC
    wxRegKey regTDM(wxRegKey::HKLM, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\TDM-GCC");
    wxString tdmInstallFolder;
    if ( regTDM.QueryValue("InstallLocation", tdmInstallFolder) ) {
        wxFileName fnTDMBinFolder( tdmInstallFolder, "" );
        fnTDMBinFolder.AppendDir("bin");
        CompilerPtr compiler( new Compiler(NULL) );
        m_compilers.push_back( compiler );
        AddTools(compiler, fnTDMBinFolder.GetPath(), "TDM-GCC");
    }
    
    // 64 bit
    // HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\TDM-GCC
    wxRegKey regTDM_64(wxRegKey::HKLM, "SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall");
    if ( regTDM_64.QueryValue("InstallLocation", tdmInstallFolder) ) {
        wxFileName fnTDMBinFolder( tdmInstallFolder, "" );
        fnTDMBinFolder.AppendDir("bin");
        CompilerPtr compiler( new Compiler(NULL) );
        m_compilers.push_back( compiler );
        AddTools(compiler, fnTDMBinFolder.GetPath(), "TDM-GCC-64");
    }
    
    // locate codeblock's MinGW
    wxRegKey regCB(wxRegKey::HKCU, "SOFTWARE\\CodeBlocks");
    wxString cbInstallPath;
    if ( regCB.QueryValue("Path", cbInstallPath) ) {
        wxFileName mingwBinFolder( cbInstallPath, "" );
        mingwBinFolder.AppendDir("MinGW");
        mingwBinFolder.AppendDir("bin");
        if ( mingwBinFolder.DirExists() && wxFileName(mingwBinFolder.GetFullPath(), "gcc.exe").FileExists() ) {
            CompilerPtr compiler( new Compiler(NULL) );
            m_compilers.push_back( compiler );
            AddTools(compiler, mingwBinFolder.GetPath(), "Code::Blocks MinGW");
        }
    }
    
    // Last: many people install MinGW by simply extracting it into the 
    // root folder:
    // C:\MinGW-X.Y.Z
    wxArrayString volumes = wxFSVolume::GetVolumes();
    wxArrayString mingwFolderArr;
    // Get list of folders for the volume only
    for(size_t i=0; i<volumes.GetCount(); ++i) {
        wxDir dir( volumes.Item(i) );
        if ( dir.IsOpened() ) {
            wxString path;
            bool cont = dir.GetFirst(&path, "*mingw*", wxDIR_DIRS);
            while (cont ) {
                wxString fullpath;
                fullpath << volumes.Item(i) << path;
                CL_DEBUG("Found folder containing MinGW: %s", fullpath);
                mingwFolderArr.Add( fullpath );
                cont = dir.GetNext( &path );
            }
        }
    }
    
    for(size_t i=0; i<mingwFolderArr.GetCount(); ++i) {
        wxString binFolder = FindBinFolder( mingwFolderArr.Item(i) );
        if ( binFolder.IsEmpty() )
            continue;
        
        wxFileName gcc(binFolder, "gcc.exe");
        if( gcc.FileExists() ) {
            CompilerPtr compiler( new Compiler(NULL) );
            m_compilers.push_back( compiler );
            AddTools(compiler, gcc.GetPath());
        }
    }
#endif
    return !m_compilers.empty();
}