/* This file comes from the PyPA Setuptools repository, commit 16e452a: https://github.com/pypa/setuptools Modifications include this comment and inline inclusion of the LICENSE text. */ /* Copyright (C) 2016 Jason R Coombs Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* Setuptools Script Launcher for Windows This is a stub executable for Windows that functions somewhat like Effbot's "exemaker", in that it runs a script with the same name but a .py extension, using information from a #! line. It differs in that it spawns the actual Python executable, rather than attempting to hook into the Python DLL. This means that the script will run with sys.executable set to the Python executable, where exemaker ends up with sys.executable pointing to itself. (Which means it won't work if you try to run another Python process using sys.executable.) To build/rebuild with mingw32, do this in the setuptools project directory: gcc -DGUI=0 -mno-cygwin -O -s -o setuptools/cli.exe launcher.c gcc -DGUI=1 -mwindows -mno-cygwin -O -s -o setuptools/gui.exe launcher.c To build for Windows RT, install both Visual Studio Express for Windows 8 and for Windows Desktop (both freeware), create "win32" application using "Windows Desktop" version, create new "ARM" target via "Configuration Manager" menu and modify ".vcxproj" file by adding "true" tag as child of "PropertyGroup" tags that has "Debug|ARM" and "Release|ARM" properties. It links to msvcrt.dll, but this shouldn't be a problem since it doesn't actually run Python in the same process. Note that using 'exec' instead of 'spawn' doesn't work, because on Windows this leads to the Python executable running in the *background*, attached to the same console window, meaning you get a command prompt back *before* Python even finishes starting. So, we have to use spawnv() and wait for Python to exit before continuing. :( */ #include #include #include #include #include #include #include int child_pid=0; int fail(const char *format, const char *data) { /* Print error message to stderr and return 2 */ fprintf(stderr, format, data); return 2; } char *quoted(char *data) { int i, ln = strlen(data), nb; /* We allocate twice as much space as needed to deal with worse-case of having to escape everything. */ char *result = (char *)calloc(ln*2+3, sizeof(char)); char *presult = result; *presult++ = '"'; for (nb=0, i=0; i < ln; i++) { if (data[i] == '\\') nb += 1; else if (data[i] == '"') { for (; nb > 0; nb--) *presult++ = '\\'; *presult++ = '\\'; } else nb = 0; *presult++ = data[i]; } for (; nb > 0; nb--) /* Deal w trailing slashes */ *presult++ = '\\'; *presult++ = '"'; *presult++ = 0; return result; } char *loadable_exe(char *exename) { /* HINSTANCE hPython; DLL handle for python executable */ char *result; /* hPython = LoadLibraryEx(exename, NULL, LOAD_WITH_ALTERED_SEARCH_PATH); if (!hPython) return NULL; */ /* Return the absolute filename for spawnv */ result = (char *)calloc(MAX_PATH, sizeof(char)); strncpy(result, exename, MAX_PATH); /*if (result) GetModuleFileNameA(hPython, result, MAX_PATH); FreeLibrary(hPython); */ return result; } char *find_exe(char *exename, char *script) { char drive[_MAX_DRIVE], dir[_MAX_DIR], fname[_MAX_FNAME], ext[_MAX_EXT]; char path[_MAX_PATH], c, *result; /* convert slashes to backslashes for uniform search below */ result = exename; while (c = *result++) if (c=='/') result[-1] = '\\'; _splitpath(exename, drive, dir, fname, ext); if (drive[0] || dir[0]=='\\') { return loadable_exe(exename); /* absolute path, use directly */ } /* Use the script's parent directory, which should be the Python home (This should only be used for bdist_wininst-installed scripts, because easy_install-ed scripts use the absolute path to python[w].exe */ _splitpath(script, drive, dir, fname, ext); result = dir + strlen(dir) -1; if (*result == '\\') result--; while (*result != '\\' && result>=dir) *result-- = 0; _makepath(path, drive, dir, exename, NULL); return loadable_exe(path); } char **parse_argv(char *cmdline, int *argc) { /* Parse a command line in-place using MS C rules */ char **result = (char **)calloc(strlen(cmdline), sizeof(char *)); char *output = cmdline; char c; int nb = 0; int iq = 0; *argc = 0; result[0] = output; while (isspace(*cmdline)) cmdline++; /* skip leading spaces */ do { c = *cmdline++; if (!c || (isspace(c) && !iq)) { while (nb) {*output++ = '\\'; nb--; } *output++ = 0; result[++*argc] = output; if (!c) return result; while (isspace(*cmdline)) cmdline++; /* skip leading spaces */ if (!*cmdline) return result; /* avoid empty arg if trailing ws */ continue; } if (c == '\\') ++nb; /* count \'s */ else { if (c == '"') { if (!(nb & 1)) { iq = !iq; c = 0; } /* skip " unless odd # of \ */ nb = nb >> 1; /* cut \'s in half */ } while (nb) {*output++ = '\\'; nb--; } if (c) *output++ = c; } } while (1); } void pass_control_to_child(DWORD control_type) { /* * distribute-issue207 * passes the control event to child process (Python) */ if (!child_pid) { return; } GenerateConsoleCtrlEvent(child_pid,0); } BOOL control_handler(DWORD control_type) { /* * distribute-issue207 * control event handler callback function */ switch (control_type) { case CTRL_C_EVENT: pass_control_to_child(0); break; } return TRUE; } int create_and_wait_for_subprocess(char* command) { /* * distribute-issue207 * launches child process (Python) */ DWORD return_value = 0; LPSTR commandline = command; STARTUPINFOA s_info; PROCESS_INFORMATION p_info; ZeroMemory(&p_info, sizeof(p_info)); ZeroMemory(&s_info, sizeof(s_info)); s_info.cb = sizeof(STARTUPINFO); // set-up control handler callback funciotn SetConsoleCtrlHandler((PHANDLER_ROUTINE) control_handler, TRUE); if (!CreateProcessA(NULL, command
/*
 *  yosys -- Yosys Open SYnthesis Suite
 *
 *  Copyright (C) 2012  Clifford Wolf <clifford@clifford.at>
 *
 *  Permission to use, copy, modify, and/or distribute this software for any
 *  purpose with or without fee is hereby granted, provided that the above
 *  copyright notice and this permission notice appear in all copies.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 *  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 *  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 *  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 *  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 *  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 *  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */

#include "ast.h"

#ifdef YOSYS_ENABLE_PLUGINS

#include <dlfcn.h>
#include <ffi.h>

YOSYS_NAMESPACE_BEGIN

typedef void (*ffi_fptr) ();

static ffi_fptr resolve_fn (std::string symbol_name)
{
	if (symbol_name.find(':') != std::string::npos)
	{
		int pos = symbol_name.find(':');
		std::string plugin_name = symbol_name.substr(0, pos);
		std::string real_symbol_name = symbol_name.substr(pos+1);

		while (loaded_plugin_aliases.count(plugin_name))
			plugin_name = loaded_plugin_aliases.at(plugin_name);

		if (loaded_plugins.count(plugin_name) == 0)
			log_error("unable to resolve '%s': can't find plugin `%s'\n", symbol_name.c_str(), plugin_name.c_str());

		void *symbol = dlsym(loaded_plugins.at(plugin_name), real_symbol_name.c_str());

		if (symbol == nullptr)
			log_error("unable to resolve '%s': can't find symbol `%s' in plugin `%s'\n",
					symbol_name.c_str(), real_symbol_name.c_str(), plugin_name.c_str());

		return (ffi_fptr) symbol;
	}

	for (auto &it : loaded_plugins) {
		void *symbol = dlsym(it.second, symbol_name.c_str());
		if (symbol != nullptr)
			return (ffi_fptr) symbol;
	}

	void *symbol = dlsym(RTLD_DEFAULT, symbol_name.c_str());
	if (symbol != nullptr)
		return (ffi_fptr) symbol;

	log_error("unable to resolve '%s'.\n", symbol_name.c_str());
}

AST::AstNode *AST::dpi_call(const std::string &rtype, const std::string &fname, const std::vector<std::string> &argtypes, const std::vector<AstNode*> &args)
{
	AST::AstNode *newNode = nullptr;
	union { double f64; float f32; int32_t i32; } value_store [args.size() + 1];
	ffi_type *types [args.size() + 1];
	void *values [args.size() + 1];
	ffi_cif cif;
	int status;

	log("Calling DPI function `%s' and returning `%s':\n", fname.c_str(), rtype.c_str());

	log_assert(GetSize(args) == GetSize(argtypes));
	for (int i = 0; i < GetSize(args); i++) {
		if (argtypes[i] == "real") {
			log("  arg %d (%s): %f\n", i, argtypes[i].c_str(), args[i]->asReal(args[i]->is_signed));
			value_store[i].f64 = args[i]->asReal(args[i]->is_signed);
			values[i] = &value_store[i].f64;
			types[i] = &ffi_type_double;
		} else if (argtypes[i] == "shortreal") {
			log("  arg %d (%s): %f\n", i, argtypes[i].c_str(), args[i]->asReal(args[i]->is_signed));
			value_store[i].f32 = args[i]->asReal(args[i]->is_signed);
			values[i] = &value_store[i].f32;
			types[i] = &ffi_type_double;
		} else if (argtypes[i] == "integer") {
			log("  arg %d (%s): %lld\n", i, argtypes[i].c_str(), (long long)args[i]->asInt(args[i]->is_signed));
			value_store[i].i32 = args[i]->asInt(args[i]->is_signed);
			values[i] = &value_store[i].i32;
			types[i] = &ffi_type_sint32;
		} else {
			log_error("invalid argtype '%s' for argument %d.\n", argtypes[i].c_str(), i);
		}
	}

        if (rtype == "integer") {
                types[args.size()] = &ffi_type_slong;
                values[args.size()] = &value_store[args.size()].i32;
        } else if (rtype == "shortreal") {
                types[args.size()] = &ffi_type_float;
                values[args.size()] = &value_store[args.size()].f32;
        } else if (rtype == "real") {
                types[args.size()] = &ffi_type_double;
                values[args.size()] = &value_store[args.size()].f64;
        } else {
                log_error("invalid rtype '%s'.\n", rtype.c_str());
        }

        if ((status = ffi_prep_cif(&cif, FFI_DEFAULT_ABI, args.size(), types[args.size()], types)) != FFI_OK)
                log_error("ffi_prep_cif failed: status %d.\n", status);

        ffi_call(&cif, resolve_fn(fname.c_str()), values[args.size()], values);

	if (rtype == "real") {
		newNode = new AstNode(AST_REALVALUE);
		newNode->realvalue = value_store[args.size()].f64;
		log("  return realvalue: %g\n", newNode->asReal(true));
	} else if (rtype == "shortreal") {
		newNode = new AstNode(AST_REALVALUE);
		newNode->realvalue = value_store[args.size()].f32;
		log("  return realvalue: %g\n", newNode->asReal(true));
	} else {
		newNode = AstNode::mkconst_int(value_store[args.size()].i32, false);
		log("  return integer: %lld\n", (long long)newNode->asInt(true));
	}

	return newNode;
}

YOSYS_NAMESPACE_END

#else /* YOSYS_ENABLE_PLUGINS */

YOSYS_NAMESPACE_BEGIN

AST::AstNode *AST::dpi_call(const std::string&, const std::string &fname, const std::vector<std::string>&, const std::vector<AstNode*>&)
{
	log_error("Can't call DPI function `%s': this version of yosys is built without plugin support\n", fname.c_str());
}

YOSYS_NAMESPACE_END

#endif /* YOSYS_ENABLE_PLUGINS */