/* * nextpnr -- Next Generation Place and Route * * Copyright (C) 2018 Clifford Wolf * Copyright (C) 2018 David Shah * * 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. * */ #ifndef PYWRAPPERS_H #define PYWRAPPERS_H #include #include #include "nextpnr.h" NEXTPNR_NAMESPACE_BEGIN namespace py = pybind11; namespace PythonConversion { template struct ContextualWrapper { Context *ctx; T base; inline ContextualWrapper(Context *c, T x) : ctx(c), base(x){}; inline operator T() { return base; }; typedef T base_type; }; template struct WrapIfNotContext { typedef ContextualWrapper maybe_wrapped_t; }; template <> struct WrapIfNotContext { typedef Context maybe_wrapped_t; }; template inline Context *get_ctx(typename WrapIfNotContext::maybe_wrapped_t &wrp_ctx) { return wrp_ctx.ctx; } template <> inline Context *get_ctx(WrapIfNotContext::maybe_wrapped_t &unwrp_ctx) { return &unwrp_ctx; } template inline T &get_base(typename WrapIfNotContext::maybe_wrapped_t &wrp_ctx) { return wrp_ctx.base; } template <> inline Context &get_base(WrapIfNotContext::maybe_wrapped_t &unwrp_ctx) { return unwrp_ctx; } template ContextualWrapper wrap_ctx(Context *ctx, T x) { return ContextualWrapper(ctx, x); } // Dummy class, to be implemented by users template struct string_converter; class bad_wrap { }; // Action options template struct pass_through { inline T operator()(Context *ctx, T x) { return x; } using ret_type = T; using arg_type = T; }; template struct wrap_context { inline ContextualWrapper operator()(Context *ctx, T x) { return ContextualWrapper(ctx, x); } using arg_type = T; using ret_type = ContextualWrapper; }; template struct unwrap_context { inline T operator()(Context *ctx, ContextualWrapper x) { return x.base; } using ret_type = T; using arg_type = ContextualWrapper; }; template struct conv_from_str { inline T operator()(Context *ctx, std::string x) { return string_converter().from_str(ctx, x); } using ret_type = T; using arg_type = std::string; }; template struct conv_to_str { inline std::string operator()(Context *ctx, T x) { return string_converter().to_str(ctx, x); } using ret_type = std::string; using arg_type = T; }; template struct deref_and_wrap { inline ContextualWrapper operator()(Context *ctx, T *x) { if (x == nullptr) throw bad_wrap(); return ContextualWrapper(ctx, *x); } using arg_type = T *; using ret_type = ContextualWrapper; }; template struct addr_and_unwrap { inline T *operator()(Context *ctx, ContextualWrapper x) { return &(x.base); } using arg_type = ContextualWrapper; using ret_type = T *; }; // Function wrapper // Zero parameters, one return template struct fn_wrapper_0a { using class_type = typename WrapIfNotContext::maybe_wrapped_t; using conv_result_type = typename rv_conv::ret_type; static py::object wrapped_fn(class_type &cls) { Context *ctx = get_ctx(cls); Class &base = get_base(cls); try { return py::cast(rv_conv()(ctx, (base.*fn)())); } catch (bad_wrap &) { return py::none(); } } template static void def_wrap(WrapCls cls_, const char *name) { cls_.def(name, wrapped_fn); } }; // One parameter, one return template struct fn_wrapper_1a { using class_type = typename WrapIfNotContext::maybe_wrapped_t; using conv_result_type = typename rv_conv::ret_type; using conv_arg1_type = typename arg1_conv::arg_type; static py::object wrapped_fn(class_type &cls, conv_arg1_type arg1) { Context *ctx = get_ctx(cls); Class &base = get_base(cls); try { return py::cast(rv_conv()(ctx, (base.*fn)(arg1_conv()(ctx, arg1)))); } catch (bad_wrap &) { return py::none(); }
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

int line = 0;
char buffer1[8192];
char buffer2[8192];

void check(bool ok)
{
	if (ok)
		return;
	fprintf(stderr, "Error in testbench output compare (line=%d):\n-%s\n+%s\n", line, buffer1, buffer2);
	exit(1);
}

int main(int argc, char **argv)
{
	FILE *f1, *f2;
	bool eof1, eof2;
	int i;

	check(argc == 3);

	f1 = fopen(argv[1], "r");
	f2 = fopen(argv[2], "r");

	check(f1 && f2);

	while (!feof(f1) && !feof(f2))
	{
		line++;
		buffer1[0] = 0;
		buffer2[0] = 0;

		eof1 = fgets(buffer1, 1024, f1) == NULL;
		eof2 = fgets(buffer2, 1024, f2) == NULL;

		if (*buffer1 && buffer1[strlen(buffer1)-1] == '\n')
			buffer1[strlen(buffer1)-1] = 0;

		if (*buffer2 && buffer2[strlen(buffer2)-1] == '\n')
			buffer2[strlen(buffer2)-1] = 0;

		check(eof1 == eof2);

		for (i = 0; buffer1[i] || buffer2[i]; i++)
		{
			check(buffer1[i] != 0 && buffer2[i] != 0);

			// first argument is the reference. An 'z' or 'x'
			// here means we don't care about the result.
			if (buffer1[i] == 'z' || buffer1[i] == 'x')
				continue;
			if (buffer1[i] == 'Z' || buffer1[i] == 'X')
				continue;

			check(buffer1[i] == buffer2[i]);
		}
	}

	check(feof(f1) && feof(f2));

	fclose(f1);
	fclose(f2);
	return 0;
}