testing
[gnucap-felix.git] / src / e_ivl_compile.h
blobba3ded19bacef20574b193a9d6a1576d14e4e1ce
1 // add another interface to vvp compiler
2 #ifndef IVLC__H
3 #define IVLC__H
5 #include "vvp/compile.h"
6 #include <string>
7 #include <sstream>
8 #include <iostream>
9 #include "io_trace.h"
10 #include "vvp/codes.h"
11 // #include <iostream>
12 #define sd(a) strdup(a)
13 #define none (const char*)0
14 class COMPONENT;
16 class ARG_BASE{
17 public:
18 virtual operator string() const=0;
19 // needed to circumvent temporary address warning
20 ARG_BASE* operator &(){ return(this); }
22 /*-----------------------------------------------------*/
23 class ARG_I : public ARG_BASE{
24 private:
25 intptr_t i;
26 public:
27 ARG_I(intptr_t x): ARG_BASE(),i(x){}
28 operator string() const{
29 stringstream x;
30 x << hex << i;
31 return x.str();
34 /*-----------------------------------------------------*/
35 class ARG_E : public ARG_BASE{
36 private:
37 intptr_t i;
38 public:
39 static intptr_t offset;
40 static intptr_t top;
41 ARG_E(intptr_t x): ARG_BASE(),i(x)
42 { assert(x>=0);}
43 operator string() const{
44 stringstream x;
45 x << "E_0x" << hex << (i-offset);
46 return x.str();
49 /*-----------------------------------------------------*/
50 class ARG_O : public ARG_BASE{
51 private:
52 intptr_t i;
53 public:
54 static intptr_t offset;
55 static intptr_t top;
56 ARG_O(intptr_t x): ARG_BASE(),i(x){}
57 operator string() const{
58 stringstream x;
59 x << "oo" << hex << (void*)(i-offset);
60 return x.str();
62 /*-----------------------------------------------*/
64 /*-----------------------------------------------------*/
65 class ARG_RAW : public ARG_BASE{
66 private:
67 string s;
68 public:
69 ARG_RAW(string x): ARG_BASE(),s(x){}
70 ARG_RAW(const char* x): ARG_BASE(),s(x){}
71 operator string() const{
72 stringstream x;
73 x << s;
74 trace0(x.str());
75 return x.str();
78 /*-----------------------------------------------------*/
79 class ARG_S : public ARG_BASE{
80 private:
81 intptr_t i;
82 public:
83 static intptr_t offset;
84 static intptr_t top;
85 ARG_S(intptr_t x): ARG_BASE(),i(x){}
86 operator string() const;
88 /*-----------------------------------------------------*/
89 class ARG_T : public ARG_BASE{
90 private:
91 intptr_t i;
92 unsigned app;
93 bool apped;
94 public:
95 static intptr_t offset;
96 static intptr_t top;
97 ARG_T(intptr_t x): ARG_BASE(),i(x),apped(false){}
98 ARG_T(intptr_t x, unsigned y): ARG_BASE(),i(x),app(y),apped(true){}
99 operator string() const{
100 stringstream x;
101 x << "T_" << i - offset;
102 if (apped)
103 x<<"." << app;
104 return x.str();
107 /*-----------------------------------------------------*/
108 class ARG_C4 : public ARG_BASE{
109 private:
110 unsigned i;
111 string s;
112 char c;
113 int type;
114 public:
115 // just reproduce the string...
116 // obsolete at the end??
117 ARG_C4(const char* x): ARG_BASE(),s(strdup(x)),type(0){}
119 ARG_C4(unsigned x): ARG_BASE(),i(x),type(1){}
120 ARG_C4(char x, unsigned wid): ARG_BASE(),i(wid),c(x),type(2){}
122 operator string() const;
124 /*------------------------------------------------*/
125 class ARG_P : public ARG_BASE{
126 private:
127 intptr_t i;
128 public:
129 static intptr_t offset;
130 static intptr_t top;
131 ARG_P(intptr_t x): ARG_BASE(),i(x){}
132 operator string() const{
133 stringstream x;
134 x << "P_" << hex << i - offset;
135 return x.str();
138 /*------------------------------------------------*/
139 class ARG_V : public ARG_BASE{
140 private:
141 intptr_t i;
142 int app;
143 public:
144 static intptr_t offset;
145 static intptr_t top;
146 ARG_V(intptr_t x, int y): ARG_BASE(),i(x), app(y){}
147 operator string() const{
148 stringstream x;
149 x << "v0x" << hex << i - offset << "_" << app;
150 return x.str();
153 /*------------------------------------------------*/
155 // compile iverilogish argument list from ARG_BASE va_list...
156 symb_s* arg_symbols( unsigned argc, va_list argv );
158 inline symb_s* arg_symbols( unsigned argc, ... ){
159 trace0("arg_sybbols ... ");
160 va_list arguments;
161 va_start ( arguments, argc );
162 return arg_symbols(argc,arguments);
166 class COMPILE_WRAP : public COMPILE {
167 private:
168 intptr_t param_offset;
169 intptr_t param_top;
170 intptr_t signal_offset;
171 intptr_t signal_top;
174 public:
175 COMPILE_WRAP();
176 void flush();
178 void param_logic(ARG_BASE* label, char* name, char*value, bool signed_flag,
179 long file_idx, long lineno);
181 // this is a READ ONLY pointer. so it is ok.
182 void param_real(ARG_BASE*label, char*name, const double* value,
183 long file_idx, long lineno);
185 void variable(intptr_t, int app, char*name,
186 int msb, int lsb, int vpi_type_code,
187 bool signed_flag, bool local_flag);
188 void variable(ARG_BASE* label, char*name,
189 int msb, int lsb, int vpi_type_code,
190 bool signed_flag, bool local_flag);
194 void code( intptr_t label, const char *mnem );
195 void code( intptr_t label, const char *mnem,
196 unsigned long l);
197 void code( intptr_t label, const char *mnem,
198 const char* cc);
199 void code( intptr_t label, const char *mnem,
200 unsigned long l, unsigned long m);
201 void code( intptr_t label, const char *mnem,
202 unsigned long l, const char* m, unsigned long n);
203 void code( intptr_t label, const char *mnem,
204 unsigned long l, unsigned long m, unsigned long n);
205 void code( intptr_t label, const char *mnem,
206 const char * l, unsigned long m, unsigned long n);
209 void vpi_call( const char* label, const char* cmd, bool b0, bool
210 b1, long l0, long l1, ... );
212 //deprecated.
213 void net( intptr_t label, int app, char*name, int msb, int lsb,
214 int vpi_type_code, bool signed_flag, bool local_flag,
215 unsigned argc, ... );
216 void net( ARG_BASE* label, char*name, int msb, int lsb,
217 int vpi_type_code, bool signed_flag, bool local_flag,
218 unsigned argc, ... );
220 void functor(const char*label, const char*type, unsigned width,
221 unsigned ostr0, unsigned ostr1,
222 unsigned argc, ...) {
223 va_list arguments,argv;
224 va_start ( arguments, argc );
225 va_copy(argv,arguments);
227 // if (argc > 0 ){
228 // ARG_BASE* w = va_arg ( arguments, ARG_BASE* );
229 // trace0("passing arguments 1, "+ string(*w) );
230 // }
232 COMPILE::functor(label, type, width,
233 ostr0, ostr1,
234 argc, arg_symbols( argc, argv));
236 // compile->functor( &ARG_O(0), s("BUFZ"), 1, 6, 6, 1, &ARG_RAW("C4<z>") ); // o0x2b9ed4fce018
237 void functor(const ARG_BASE* label, const char*type, unsigned width,
238 unsigned ostr0, unsigned ostr1,
239 unsigned argc, va_list argv) {
241 const char* l = strdup(string(*label).c_str());
242 trace0("calling arg_symbols");
243 struct symb_s*args = arg_symbols(argc, argv );
244 COMPILE::functor(l, type, width,
245 ostr0, ostr1,
246 argc, args);
248 void functor(const ARG_BASE* label, const char*type, unsigned width,
249 unsigned ostr0, unsigned ostr1,
250 unsigned argc, ...) {
251 va_list argv;
252 va_start( argv, argc );
254 functor(label, type, width,
255 ostr0, ostr1,
256 argc, argv);
258 void event(int l, char* t, int argc, ... ){
259 va_list argv;
260 va_start(argv,argc);
261 COMPILE::event(strdup(string(ARG_E(l)).c_str()), t, argc, arg_symbols(argc, argv));
263 void event(ARG_BASE* l, char* t, int argc, ... ){
264 va_list argv;
265 va_start(argv,argc);
266 COMPILE::event(strdup(string(*l).c_str()), t, argc, arg_symbols(argc, argv));
269 /*---------------------------*/
270 inline void code( const char *label, const char *mnem ){
271 comp_operands_s *opa = 0;
272 COMPILE::code(label?sd(label):0, sd(mnem), opa);
274 inline void code( const char *label, const char *mnem, unsigned long l){
275 comp_operands_s *opa = (comp_operands_t) calloc(1, sizeof(comp_operands_s));
276 opa->argc = 1;
278 opa->argv[0].ltype = L_NUMB;
279 opa->argv[0].numb = l;
281 COMPILE::code(label?sd(label):0, sd(mnem), opa);
283 inline void code( const char *label, const char *mnem, const char* cc){
284 comp_operands_s *opa = (comp_operands_t) calloc(1, sizeof(comp_operands_s));
285 //= new comp_operands_s();
286 opa->argc = 1;
288 symb_s s;// = new symb_s();
289 s.text = strdup(cc);
290 s.idx = 0;
291 opa->argv[0].ltype = L_SYMB;
292 opa->argv[0].symb = s;
294 COMPILE::code(label?sd(label):0, sd(mnem), opa);
298 inline void code( const char *label, const char *mnem,
299 unsigned long l,
300 unsigned long m){
301 comp_operands_s *opa = (comp_operands_t) calloc(1, sizeof(comp_operands_s));
302 opa->argc = 2;
304 opa->argv[0].ltype = L_NUMB;
305 opa->argv[0].numb = l;
306 opa->argv[1].ltype = L_NUMB;
307 opa->argv[1].numb = m;
310 COMPILE::code(label?sd(label):0, sd(mnem), opa);
313 /*inline void code( const char *label, const char *mnem,
314 unsigned long l,
315 const char* m,
316 unsigned long n){
317 comp_operands_s *opa = (comp_operands_t) calloc(1, sizeof(comp_operands_s));
318 opa->argc = 3;
319 symb_s M;
320 M.text = strdup(m);
321 M.idx = 0;
323 opa->argv[0].ltype = L_NUMB;
324 opa->argv[0].numb = l;
325 opa->argv[1].ltype = L_SYMB;
326 opa->argv[1].symb = M;
327 opa->argv[2].ltype = L_NUMB;
328 opa->argv[2].numb = n;
330 COMPILE::code(label?sd(label):0, sd(mnem), opa);
333 inline void code( const char *label, const char *mnem,
334 unsigned long l,
335 unsigned long m,
336 unsigned long n){
337 comp_operands_s *opa = (comp_operands_t) calloc(1, sizeof(comp_operands_s));
338 opa->argc = 3;
340 opa->argv[0].ltype = L_NUMB;
341 opa->argv[0].numb = l;
342 opa->argv[1].ltype = L_NUMB;
343 opa->argv[1].numb = m;
344 opa->argv[2].ltype = L_NUMB;
345 opa->argv[2].numb = n;
347 COMPILE::code(label?sd(label):0, sd(mnem), opa);
350 // compile->code( 0, "\%set/v", &ARG_V(0,0), 0, 32);
351 inline void code( const char *label, const char *mnem,
352 const ARG_BASE* l,
353 unsigned long m,
354 unsigned long n){
355 assert(l);
356 trace0("COMPILE_WRAP::code " + string(*l) );
357 char *ll = strdup(string(*l).c_str());
358 assert(ll);
359 comp_operands_s *opa = (comp_operands_t) calloc(1, sizeof(comp_operands_s));
360 symb_s L;
361 opa->argc = 3;
362 L.text = ll;
363 L.idx = 0;
365 opa->argv[0].ltype = L_SYMB;
366 opa->argv[0].symb = L;
367 opa->argv[1].ltype = L_NUMB;
368 opa->argv[1].numb = m;
369 opa->argv[2].ltype = L_NUMB;
370 opa->argv[2].numb = n;
372 COMPILE::code(label?sd(label):0, sd(mnem), opa);
375 void thread( const ARG_BASE* l, char* c ){
376 return COMPILE::thread ( strdup(string(*l).c_str()), c);
378 void scope_recall( const ARG_BASE* l ){
379 return COMPILE::scope_recall ( strdup(string(*l).c_str()));
381 void code( const char*label, const char *mnem,
382 const ARG_BASE* l ){
383 return code (label, mnem, strdup(string(*l).c_str()));
386 void code( const char*label, const char *mnem,
387 unsigned long l,
388 const ARG_BASE* m,
389 unsigned long n){
390 char *mm = strdup(string(*m).c_str());
392 comp_operands_s *opa = (comp_operands_t) calloc(1, sizeof(comp_operands_s));
393 opa->argc = 3;
394 symb_s M;
395 M.text = mm;
396 M.idx = 0;
398 opa->argv[0].ltype = L_NUMB;
399 opa->argv[0].numb = l;
400 opa->argv[1].ltype = L_SYMB;
401 opa->argv[1].symb = M;
402 opa->argv[2].ltype = L_NUMB;
403 opa->argv[2].numb = n;
405 COMPILE::code(label?sd(label):0, sd(mnem), opa);
408 void scope_decl(ARG_BASE*typ, char*lab, char*nam, char*tnam,
409 ARG_BASE* par, long file_idx, long lineno,
410 long def_file_idx, long def_lineno,
411 long is_cell){
412 char * p = (par)? strdup(string(*par).c_str()):0;
413 return
414 COMPILE::scope_decl(strdup(string(*typ).c_str()), lab, nam, tnam,
415 p, file_idx, lineno,
416 def_file_idx, def_lineno,
417 is_cell);
420 void notify_i( const ARG_BASE* l,
421 uint32_t m,
422 uint32_t n, COMPONENT* daport);
423 void notify( const ARG_BASE* l,
424 uint32_t m,
425 uint32_t n, COMPONENT* daport);
428 void notify_i ( uint32_t delay, uint32_t bit, COMPONENT* daport);
429 void notify ( uint32_t delay, uint32_t bit, COMPONENT* daport);
430 void load_real_parameter( const double*, unsigned reg );
431 void load_number_parameter( const int64_t*, unsigned reg );
432 void load_number_parameter( const int32_t*, unsigned reg, unsigned wid=32 );
433 void codelabel( ARG_BASE* label);
436 }; // COMPILE_WRAP
443 //#define OPERAND_MAX 3
444 //enum ltype_e { L_NUMB, L_SYMB };
446 //struct comp_operands_s {
447 // unsigned argc;
448 // struct {
449 // enum ltype_e ltype;
450 // union {
451 // unsigned long numb;
452 // struct symb_s symb;
453 // };
454 // } argv[OPERAND_MAX];
455 //};
457 // void compile_code(char*label, char*mnem, va_list a);
459 class COMPILE;
462 symb_s* operands_wrap( unsigned argc, ... ){
463 va_list arguments;
464 va_start ( arguments, argc );
465 assert (argc>0);
467 struct symbv_s obj;
468 symbv_init(&obj);
470 for(unsigned i=0; i<argc; i++){
471 const char* w = va_arg ( arguments, const char* );
473 comp_operands_s* opa = (comp_operands_s*)
474 calloc(1, sizeof(struct comp_operands_s));
476 char* ww=strdup(w);
482 void compile_vpi_call(COMPILE* c, const char* label, const char* cmd, bool b0, bool b1, long l0, long l1, ... );
485 inline symb_s* symbols_wrap( unsigned argc, ... ){
486 trace0("symbols_wrap");
487 va_list arguments;
488 va_start ( arguments, argc );
489 assert (argc>0);
492 struct symbv_s obj;
493 symbv_init(&obj);
494 for(unsigned i=0; i<argc; i++){
495 const char* w = va_arg ( arguments, const char* );
496 symb_s s;
497 char* ww=strdup(w);
498 trace0("wrapping" + string(w));
499 s.text=ww;
500 s.idx=0;
501 symbv_add(&obj, s);
502 // delete[]ww;
506 trace0("wrapped " + string(obj.vect[0].text));
507 return (obj.vect);
512 #endif