1 /*  Ghdlsynth -*- C++ -*- interface
2 
3    This file is part of GHDL.
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
18    MA 02110-1301, USA.  */
19 
20 namespace GhdlSynth {
21   struct logic_32 {
22     unsigned int va;
23     unsigned int zx;
24   };
25 
26   //  Use struct wrappers for type safety.
27   //  Convention: W for wrapped, D for direct, B for boolean.
28 #define GHDLSYNTH_ADA_PREFIX(N) netlists__##N
29 #define GHDLSYNTH_ADA_WRAPPER_WW(NAME, RESTYPE, ARGTYPE) \
30   extern "C" unsigned int GHDLSYNTH_ADA_PREFIX(NAME) (unsigned int); \
31   inline RESTYPE NAME(ARGTYPE arg) { \
32     RESTYPE res; \
33     res.id = GHDLSYNTH_ADA_PREFIX(NAME) (arg.id);	\
34     return res; \
35   }
36 
37 #define GHDLSYNTH_ADA_WRAPPER_WWD(NAME, RESTYPE, ARGTYPE1, ARGTYPE2)	\
38   extern "C" unsigned int GHDLSYNTH_ADA_PREFIX(NAME) (unsigned int, ARGTYPE2);\
39   inline RESTYPE NAME(ARGTYPE1 arg1, ARGTYPE2 arg2) {	\
40     RESTYPE res; \
41     res.id = GHDLSYNTH_ADA_PREFIX(NAME) (arg1.id, arg2);	\
42     return res; \
43   }
44 
45 #define GHDLSYNTH_ADA_WRAPPER_DWD(NAME, RESTYPE, ARGTYPE1, ARGTYPE2)	\
46   extern "C" RESTYPE GHDLSYNTH_ADA_PREFIX(NAME) (unsigned int, ARGTYPE2);\
47   inline RESTYPE NAME(ARGTYPE1 arg1, ARGTYPE2 arg2) {	\
48     return GHDLSYNTH_ADA_PREFIX(NAME) (arg1.id, arg2);	\
49   }
50 
51 #define GHDLSYNTH_ADA_WRAPPER_DW(NAME, RESTYPE, ARGTYPE)	\
52   extern "C" RESTYPE GHDLSYNTH_ADA_PREFIX(NAME) (unsigned int); \
53   inline RESTYPE NAME(ARGTYPE arg) { \
54     return GHDLSYNTH_ADA_PREFIX(NAME) (arg.id); \
55   }
56 
57 #define GHDLSYNTH_ADA_WRAPPER_BW(NAME, ARGTYPE)	\
58   extern "C" unsigned int GHDLSYNTH_ADA_PREFIX(NAME) (unsigned int); \
59   inline bool NAME(ARGTYPE arg) { \
60     return (GHDLSYNTH_ADA_PREFIX(NAME) (arg.id) & 1);	\
61   }
62 
63 #define GHDLSYNTH_ADA_WRAPPER_BWD(NAME, ARGTYPE1, ARGTYPE2)		\
64   extern "C" unsigned int GHDLSYNTH_ADA_PREFIX(NAME) (unsigned, unsigned); \
65   inline bool NAME(ARGTYPE1 arg1, ARGTYPE2 arg2) {			\
66     return (GHDLSYNTH_ADA_PREFIX(NAME) (arg1.id, arg2) & 1);		\
67   }
68 
69   struct Name_Id { unsigned int id; };
70   extern "C" const char *name_table__get_address (unsigned int);
get_cstr(Name_Id n)71   inline const char *get_cstr(Name_Id n) {
72     return name_table__get_address (n.id);
73   }
74 
75   extern "C" unsigned name_table__get_identifier_with_len(const char *s, unsigned l);
get_identifier(const char * s)76   inline Name_Id get_identifier(const char *s) {
77     Name_Id n;
78     n.id = name_table__get_identifier_with_len(s, strlen(s));
79     return n;
80   }
81 
82   struct Sname { unsigned int id; };
83   const Sname No_Sname = {0 };
84 
85   enum Sname_Kind { Sname_User, Sname_Artificial, Sname_Version };
86   GHDLSYNTH_ADA_WRAPPER_DW(get_sname_kind, Sname_Kind, Sname);
is_valid(Sname l)87   inline bool is_valid(Sname l) { return l.id != 0; }
88 
89   GHDLSYNTH_ADA_WRAPPER_WW(get_sname_prefix, Sname, Sname);
90   GHDLSYNTH_ADA_WRAPPER_WW(get_sname_suffix, Name_Id, Sname);
91 
92   GHDLSYNTH_ADA_WRAPPER_DW(get_sname_version, unsigned int, Sname);
93 
94   typedef unsigned int Width;
95   typedef unsigned int Port_Idx;
96   typedef unsigned int Param_Idx;
97   struct Pval { unsigned int id; };
98 
99 #include "ghdlsynth_gates.h"
100 
101   struct Module { unsigned int id; };
is_valid(Module m)102   inline bool is_valid(Module m) { return m.id != 0; }
103   GHDLSYNTH_ADA_WRAPPER_WW(get_module_name, Sname, Module);
104   GHDLSYNTH_ADA_WRAPPER_WW(get_first_sub_module, Module, Module);
105   GHDLSYNTH_ADA_WRAPPER_WW(get_next_sub_module, Module, Module);
106   GHDLSYNTH_ADA_WRAPPER_DW(get_id, Module_Id, Module);
107   GHDLSYNTH_ADA_WRAPPER_DW(get_nbr_outputs, unsigned int, Module);
108   GHDLSYNTH_ADA_WRAPPER_DW(get_nbr_inputs, unsigned int, Module);
109   GHDLSYNTH_ADA_WRAPPER_DW(get_nbr_params, unsigned int, Module);
110 
111   struct Net { unsigned int id; };
112   GHDLSYNTH_ADA_WRAPPER_DW(get_width, Width, Net);
113 
114   struct Instance { unsigned int id; };
is_valid(Instance inst)115   inline bool is_valid(Instance inst) { return inst.id != 0; }
116   GHDLSYNTH_ADA_WRAPPER_WW(get_self_instance, Instance, Module);
117   GHDLSYNTH_ADA_WRAPPER_WW(get_first_instance, Instance, Module);
118   GHDLSYNTH_ADA_WRAPPER_WW(get_next_instance, Instance, Instance);
119   GHDLSYNTH_ADA_WRAPPER_WW(get_instance_name, Sname, Instance);
120   GHDLSYNTH_ADA_WRAPPER_WW(get_module, Module, Instance);
121   GHDLSYNTH_ADA_WRAPPER_WW(get_net_parent, Instance, Net);
122   GHDLSYNTH_ADA_WRAPPER_DWD(get_param_uns32, unsigned int, Instance, Param_Idx);
123   GHDLSYNTH_ADA_WRAPPER_WWD(get_param_pval, Pval, Instance, Param_Idx);
124   GHDLSYNTH_ADA_WRAPPER_DW(get_pval_length, unsigned int, Pval);
125   GHDLSYNTH_ADA_WRAPPER_DWD(read_pval, struct logic_32, Pval, unsigned int);
126 
127   struct Input { unsigned int id; };
128   GHDLSYNTH_ADA_WRAPPER_WWD(get_input, Input, Instance, Port_Idx);
129   GHDLSYNTH_ADA_WRAPPER_WWD(get_output, Net, Instance, Port_Idx);
130   GHDLSYNTH_ADA_WRAPPER_WW(get_driver, Net, Input);
131   GHDLSYNTH_ADA_WRAPPER_WW(get_input_parent, Instance, Input);
132 
133   GHDLSYNTH_ADA_WRAPPER_WW(get_first_sink, Input, Net);
134   GHDLSYNTH_ADA_WRAPPER_WW(get_next_sink, Input, Input);
135 
136   struct Attribute { unsigned int id; };
137   GHDLSYNTH_ADA_WRAPPER_WW(get_first_attribute, Attribute, Instance);
138   GHDLSYNTH_ADA_WRAPPER_WW(get_attribute_name, Name_Id, Attribute);
139   GHDLSYNTH_ADA_WRAPPER_DW(get_attribute_type, Param_Type, Attribute);
140   GHDLSYNTH_ADA_WRAPPER_WW(get_attribute_pval, Pval, Attribute);
141   GHDLSYNTH_ADA_WRAPPER_WW(get_attribute_next, Attribute, Attribute);
142 
143   //  Utils
144 #undef GHDLSYNTH_ADA_PREFIX
145 #define GHDLSYNTH_ADA_PREFIX(N) netlists__utils__##N
146   GHDLSYNTH_ADA_WRAPPER_DW(get_id, Module_Id, Instance);
147   GHDLSYNTH_ADA_WRAPPER_WWD(get_input_name, Sname, Module, Port_Idx);
148   GHDLSYNTH_ADA_WRAPPER_WWD(get_output_name, Sname, Module, Port_Idx);
149   GHDLSYNTH_ADA_WRAPPER_DWD(get_input_width, Width, Module, Port_Idx);
150   GHDLSYNTH_ADA_WRAPPER_DWD(get_output_width, Width, Module, Port_Idx);
151   GHDLSYNTH_ADA_WRAPPER_BWD(get_inout_flag, Module, Port_Idx);
152   GHDLSYNTH_ADA_WRAPPER_WWD(get_param_name, Sname, Module, Param_Idx);
153   GHDLSYNTH_ADA_WRAPPER_DWD(get_param_type, Param_Type, Module, Param_Idx);
154   GHDLSYNTH_ADA_WRAPPER_BW(has_one_connection, Net);
155 
156   GHDLSYNTH_ADA_WRAPPER_WWD(get_input_net, Net, Instance, Port_Idx);
157 
158 
159   extern "C" unsigned int ghdlsynth__ghdl_synth(int init,
160                                                 int argc, const char **argv);
ghdl_synth(int init,int argc,const char ** argv)161   inline Module ghdl_synth(int init, int argc, const char **argv) {
162     Module res;
163     res.id = ghdlsynth__ghdl_synth(init, argc, argv);
164     return res;
165   }
166 
167   //  Disp ghdl configuration.
168   extern "C" void ghdlcomp__disp_config (void);
169 
170   // Initialize the whole library.
171   extern "C" void libghdl_init (void);
172 
173   // More initialization for synthesis.
174   extern "C" void ghdlsynth__init_for_ghdl_synth (void);
175 };
176