Archibald Rust and Clifford Wolf: ffi-based dpi_call()
authorClifford Wolf <clifford@clifford.at>
Fri, 22 Aug 2014 12:22:09 +0000 (14:22 +0200)
committerClifford Wolf <clifford@clifford.at>
Fri, 22 Aug 2014 12:22:09 +0000 (14:22 +0200)
Makefile
README
frontends/ast/dpicall.cc

index e7417539480ea31e8948c5863068225515055da9..8d7f2f8dd6fca116997b5c8d092163a4a38d5a03 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -27,7 +27,7 @@ all: top-all
 
 CXXFLAGS = -Wall -Wextra -ggdb -I"$(shell pwd)" -MD -DYOSYS_SRC='"$(shell pwd)"' -D_YOSYS_ -fPIC -I${DESTDIR}/include
 LDFLAGS = -L${DESTDIR}/lib
-LDLIBS = -lstdc++ -lreadline -lm -ldl
+LDLIBS = -lstdc++ -lreadline -lm -lffi -ldl
 QMAKE = qmake-qt4
 SED = sed
 
diff --git a/README b/README
index a0e67e8a46edbde6a709102ebf5784f9ded0f91c..5c3286c20e3ea62cab8a66d119407e5db25ac278 100644 (file)
--- a/README
+++ b/README
@@ -57,8 +57,8 @@ Icarus Verilog. For example on Ubuntu Linux 12.04 LTS the following commands
 will install all prerequisites for building yosys:
 
        $ yosys_deps="git g++ clang make bison flex libreadline-dev
-                     tcl8.5-dev zlib1g-dev libqt4-dev mercurial
-                     iverilog graphviz"
+                     tcl8.5-dev zlib1g-dev libqt4-dev libffi-dev
+                     mercurial iverilog graphviz"
        $ sudo apt-get install $yosys_deps
 
 There are also pre-compiled packages for Yosys on Ubuntu. Visit the Yosys
index 965645cde221623598d777b4a59b482241f62b9e..b79bd59ebb89cbf96a4d4d27274a0098f99b0700 100644 (file)
  *
  */
 
+#include <dlfcn.h>
+#include <ffi.h>
 #include "ast.h"
 
+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(SIZE(args) == SIZE(argtypes));
-       for (int i = 0; i < SIZE(args); i++)
-               if (argtypes[i] == "real" || argtypes[i] == "shortreal")
+       for (int i = 0; i < SIZE(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));
-               else
+                       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);
 
-       if (rtype == "real" || rtype == "shortreal") {
+        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 = 1234;
+               newNode->realvalue = value_store[args.size()].f32;
+               log("  return realvalue: %g\n", newNode->asReal(true));
        } else {
-               newNode = AstNode::mkconst_int(1234, false);
+               newNode = AstNode::mkconst_int(value_store[args.size()].i32, false);
+               log("  return integer: %lld\n", (long long)newNode->asInt(true));
        }
 
        return newNode;