Branch data Line data Source code
1 : : /* do not edit automatically generated by mc from M2Swig. */
2 : : /* M2Swig.mod generates a swig interface file for the main module.
3 : :
4 : : Copyright (C) 2008-2024 Free Software Foundation, Inc.
5 : : Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
6 : :
7 : : This file is part of GNU Modula-2.
8 : :
9 : : GNU Modula-2 is free software; you can redistribute it and/or modify
10 : : it under the terms of the GNU General Public License as published by
11 : : the Free Software Foundation; either version 3, or (at your option)
12 : : any later version.
13 : :
14 : : GNU Modula-2 is distributed in the hope that it will be useful, but
15 : : WITHOUT ANY WARRANTY; without even the implied warranty of
16 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 : : General Public License for more details.
18 : :
19 : : You should have received a copy of the GNU General Public License
20 : : along with GNU Modula-2; see the file COPYING3. If not see
21 : : <http://www.gnu.org/licenses/>. */
22 : :
23 : : #include "config.h"
24 : : #include "system.h"
25 : : #include <stdbool.h>
26 : : # if !defined (PROC_D)
27 : : # define PROC_D
28 : : typedef void (*PROC_t) (void);
29 : : typedef struct { PROC_t proc; } PROC;
30 : : # endif
31 : :
32 : : # if !defined (TRUE)
33 : : # define TRUE (1==1)
34 : : # endif
35 : :
36 : : # if !defined (FALSE)
37 : : # define FALSE (1==0)
38 : : # endif
39 : :
40 : : # include "GStorage.h"
41 : : # include "Gmcrts.h"
42 : : #define _M2Swig_H
43 : : #define _M2Swig_C
44 : :
45 : : # include "GStorage.h"
46 : : # include "GM2Options.h"
47 : : # include "GSFIO.h"
48 : : # include "GFIO.h"
49 : : # include "GNameKey.h"
50 : : # include "GM2Error.h"
51 : : # include "GM2Printf.h"
52 : : # include "GM2AsmUtil.h"
53 : : # include "GSYSTEM.h"
54 : : # include "GDynamicStrings.h"
55 : : # include "GLists.h"
56 : : # include "GM2Quads.h"
57 : : # include "GM2System.h"
58 : : # include "GM2Bitset.h"
59 : : # include "GIndexing.h"
60 : : # include "GM2Scope.h"
61 : : # include "GM2Base.h"
62 : : # include "GSymbolTable.h"
63 : : # include "GM2BasicBlock.h"
64 : :
65 : : typedef struct M2Swig__T1_r M2Swig__T1;
66 : :
67 : : typedef M2Swig__T1 *M2Swig_UnboundedSig;
68 : :
69 : : struct M2Swig__T1_r {
70 : : unsigned int type;
71 : : NameKey_Name name;
72 : : };
73 : :
74 : : static bool includedArray;
75 : : static Indexing_Index uKey;
76 : : static unsigned int mainModule;
77 : : static Lists_List Done;
78 : : static Lists_List ToDo;
79 : : static FIO_File f;
80 : : static DynamicStrings_String name;
81 : : static bool FirstBasicBlock;
82 : : static bool Input;
83 : : static bool Output;
84 : : static bool InOut;
85 : : static bool CanGuess;
86 : : static bool IsKnown;
87 : : static unsigned int rs;
88 : : static unsigned int ws;
89 : :
90 : : /*
91 : : GenerateSwigFile - if the -fswig option was specified then generate
92 : : a swig interface file for the main module.
93 : : */
94 : :
95 : : extern "C" void M2Swig_GenerateSwigFile (unsigned int sym);
96 : :
97 : : /*
98 : : DoExported - includes, sym, into the, ToDo, list.
99 : : */
100 : :
101 : : static void DoExported (unsigned int sym);
102 : :
103 : : /*
104 : : MoveToDone - moves a sym to the, Done, list,
105 : : providing that it is not already on it.
106 : : It returns TRUE if the lists were modified.
107 : : */
108 : :
109 : : static bool MoveToDone (unsigned int sym);
110 : :
111 : : /*
112 : : MoveToToDo - moves a sym to the, ToDo, list,
113 : : providing that it is not already on it.
114 : : It returns TRUE if the lists were modified.
115 : : */
116 : :
117 : : static bool MoveToToDo (unsigned int sym);
118 : : static bool TryBase (unsigned int sym);
119 : :
120 : : /*
121 : : TrySystem - returns TRUE if sym can be moved to the done list.
122 : : */
123 : :
124 : : static bool TrySystem (unsigned int sym);
125 : :
126 : : /*
127 : : TryMove - tries to move sym to the done queue as long
128 : : as type is known.
129 : : */
130 : :
131 : : static bool TryMove (unsigned int sym, unsigned int type);
132 : :
133 : : /*
134 : : TryType -
135 : : */
136 : :
137 : : static bool TryType (unsigned int sym);
138 : :
139 : : /*
140 : : TryVar -
141 : : */
142 : :
143 : : static bool TryVar (unsigned int sym);
144 : :
145 : : /*
146 : : TryProcedure -
147 : : */
148 : :
149 : : static bool TryProcedure (unsigned int sym);
150 : :
151 : : /*
152 : : TryUnbounded -
153 : : */
154 : :
155 : : static bool TryUnbounded (unsigned int sym);
156 : :
157 : : /*
158 : : TryParameter -
159 : : */
160 : :
161 : : static bool TryParameter (unsigned int sym);
162 : :
163 : : /*
164 : : TryDependents - returns TRUE if any alteration occurred to any
165 : : of the lists.
166 : : */
167 : :
168 : : static bool TryDependents (unsigned int sym);
169 : :
170 : : /*
171 : : DoResolveOrder - resolves the declaration order for swig (C).
172 : : */
173 : :
174 : : static void DoResolveOrder (void);
175 : :
176 : : /*
177 : : DoName -
178 : : */
179 : :
180 : : static void DoName (unsigned int sym);
181 : :
182 : : /*
183 : : DoParamName -
184 : : */
185 : :
186 : : static void DoParamName (unsigned int sym);
187 : :
188 : : /*
189 : : DoVar -
190 : : */
191 : :
192 : : static void DoVar (unsigned int sym);
193 : :
194 : : /*
195 : : DoType -
196 : : */
197 : :
198 : : static void DoType (unsigned int sym);
199 : :
200 : : /*
201 : : DoUnbounded -
202 : : */
203 : :
204 : : static void DoUnbounded (unsigned int sym);
205 : :
206 : : /*
207 : : DoBasicBlock -
208 : : */
209 : :
210 : : static void DoBasicBlock (unsigned int start, unsigned int end);
211 : :
212 : : /*
213 : : DetermineParameter -
214 : : */
215 : :
216 : : static void DetermineParameter (unsigned int procedure, unsigned int param);
217 : :
218 : : /*
219 : : PrintDirection -
220 : : */
221 : :
222 : : static void PrintDirection (void);
223 : :
224 : : /*
225 : : CalculateVarDirective -
226 : : */
227 : :
228 : : static void CalculateVarDirective (unsigned int procedure, unsigned int param, bool annotate);
229 : :
230 : : /*
231 : : AnnotateProcedure -
232 : : */
233 : :
234 : : static void AnnotateProcedure (unsigned int sym);
235 : :
236 : : /*
237 : : DoProcedure -
238 : : */
239 : :
240 : : static bool DoProcedure (unsigned int sym);
241 : :
242 : : /*
243 : : DoWriteSymbol -
244 : : */
245 : :
246 : : static void DoWriteSymbol (unsigned int sym);
247 : :
248 : : /*
249 : : DoCheckExported -
250 : : */
251 : :
252 : : static void DoCheckExported (unsigned int sym);
253 : :
254 : : /*
255 : : IsUnique - returns TRUE if the combination of, n, and, t,
256 : : is unique.
257 : : */
258 : :
259 : : static bool IsUnique (NameKey_Name n, unsigned int t);
260 : :
261 : : /*
262 : : IsTypeUnique - returns TRUE if type, t, has not been entered yet.
263 : : */
264 : :
265 : : static bool IsTypeUnique (unsigned int t);
266 : :
267 : : /*
268 : : DoCheckUnbounded -
269 : : */
270 : :
271 : : static void DoCheckUnbounded (unsigned int sym);
272 : :
273 : : /*
274 : : DoWriteFile -
275 : : */
276 : :
277 : : static void DoWriteFile (unsigned int sym);
278 : :
279 : : /*
280 : : DoGenerateSwig -
281 : : */
282 : :
283 : : static void DoGenerateSwig (unsigned int sym);
284 : :
285 : : /*
286 : : Init -
287 : : */
288 : :
289 : : static void Init (void);
290 : :
291 : : /*
292 : : Kill -
293 : : */
294 : :
295 : : static void Kill (void);
296 : :
297 : :
298 : : /*
299 : : DoExported - includes, sym, into the, ToDo, list.
300 : : */
301 : :
302 : 0 : static void DoExported (unsigned int sym)
303 : : {
304 : 0 : Lists_IncludeItemIntoList (ToDo, sym);
305 : 0 : }
306 : :
307 : :
308 : : /*
309 : : MoveToDone - moves a sym to the, Done, list,
310 : : providing that it is not already on it.
311 : : It returns TRUE if the lists were modified.
312 : : */
313 : :
314 : 0 : static bool MoveToDone (unsigned int sym)
315 : : {
316 : 0 : if (Lists_IsItemInList (Done, sym))
317 : : {
318 : : return false;
319 : : }
320 : 0 : else if (Lists_IsItemInList (ToDo, sym))
321 : : {
322 : : /* avoid dangling else. */
323 : 0 : Lists_RemoveItemFromList (ToDo, sym);
324 : 0 : Lists_IncludeItemIntoList (Done, sym);
325 : 0 : return true;
326 : : }
327 : 0 : Lists_IncludeItemIntoList (Done, sym);
328 : 0 : return true;
329 : : /* static analysis guarentees a RETURN statement will be used before here. */
330 : : __builtin_unreachable ();
331 : : }
332 : :
333 : :
334 : : /*
335 : : MoveToToDo - moves a sym to the, ToDo, list,
336 : : providing that it is not already on it.
337 : : It returns TRUE if the lists were modified.
338 : : */
339 : :
340 : 0 : static bool MoveToToDo (unsigned int sym)
341 : : {
342 : 0 : if (Lists_IsItemInList (Done, sym))
343 : : {
344 : 0 : M2Error_InternalError ((const char *) "not expecting to get here", 25);
345 : : }
346 : 0 : else if (Lists_IsItemInList (ToDo, sym))
347 : : {
348 : : /* avoid dangling else. */
349 : : return false;
350 : : }
351 : : else
352 : : {
353 : : /* avoid dangling else. */
354 : 0 : Lists_IncludeItemIntoList (ToDo, sym);
355 : 0 : return true;
356 : : }
357 : : ReturnException ("/home/worker/buildworker/tiber-lcov/build/gcc/m2/gm2-compiler/M2Swig.def", 20, 1);
358 : : __builtin_unreachable ();
359 : : }
360 : :
361 : 0 : static bool TryBase (unsigned int sym)
362 : : {
363 : : /*
364 : : Trybase - returns TRUE
365 : : */
366 : 0 : if (((((((((((sym == M2Base_Cardinal) || (sym == M2Base_Integer)) || (sym == M2Base_LongInt)) || (sym == M2Base_LongCard)) || (sym == M2Base_Char)) || (sym == M2Base_ShortCard)) || (sym == M2Base_ShortInt)) || (sym == M2Base_Real)) || (sym == M2Base_LongReal)) || (sym == M2Base_ShortReal)) || (sym == M2Base_Boolean))
367 : : {
368 : 0 : return MoveToDone (sym);
369 : : }
370 : : else
371 : : {
372 : : return false;
373 : : }
374 : : /* static analysis guarentees a RETURN statement will be used before here. */
375 : : __builtin_unreachable ();
376 : : }
377 : :
378 : :
379 : : /*
380 : : TrySystem - returns TRUE if sym can be moved to the done list.
381 : : */
382 : :
383 : 0 : static bool TrySystem (unsigned int sym)
384 : : {
385 : 0 : if (((((sym == M2Bitset_Bitset) || (sym == M2System_Address)) || (sym == M2System_Byte)) || (sym == M2System_Loc)) || (sym == M2System_Word))
386 : : {
387 : 0 : return MoveToDone (sym);
388 : : }
389 : : else
390 : : {
391 : : return false;
392 : : }
393 : : /* static analysis guarentees a RETURN statement will be used before here. */
394 : : __builtin_unreachable ();
395 : : }
396 : :
397 : :
398 : : /*
399 : : TryMove - tries to move sym to the done queue as long
400 : : as type is known.
401 : : */
402 : :
403 : 0 : static bool TryMove (unsigned int sym, unsigned int type)
404 : : {
405 : 0 : if (Lists_IsItemInList (Done, type))
406 : : {
407 : : /* avoid dangling else. */
408 : 0 : if (MoveToDone (sym))
409 : : {
410 : : return true;
411 : : }
412 : : }
413 : : else
414 : : {
415 : 0 : if (MoveToToDo (sym))
416 : : {
417 : : return true;
418 : : }
419 : : }
420 : : return false;
421 : : /* static analysis guarentees a RETURN statement will be used before here. */
422 : : __builtin_unreachable ();
423 : : }
424 : :
425 : :
426 : : /*
427 : : TryType -
428 : : */
429 : :
430 : 0 : static bool TryType (unsigned int sym)
431 : : {
432 : 0 : unsigned int type;
433 : 0 : bool result;
434 : :
435 : 0 : type = SymbolTable_GetType (sym);
436 : 0 : result = TryDependents (type);
437 : 0 : if (TryMove (sym, type))
438 : : {
439 : : return true;
440 : : }
441 : : else
442 : : {
443 : 0 : return result;
444 : : }
445 : : /* static analysis guarentees a RETURN statement will be used before here. */
446 : : __builtin_unreachable ();
447 : : }
448 : :
449 : :
450 : : /*
451 : : TryVar -
452 : : */
453 : :
454 : 0 : static bool TryVar (unsigned int sym)
455 : : {
456 : 0 : unsigned int type;
457 : 0 : bool result;
458 : :
459 : 0 : type = SymbolTable_GetType (sym);
460 : 0 : result = TryDependents (type);
461 : 0 : if (TryMove (sym, type))
462 : : {
463 : : return true;
464 : : }
465 : : else
466 : : {
467 : 0 : return result;
468 : : }
469 : : /* static analysis guarentees a RETURN statement will be used before here. */
470 : : __builtin_unreachable ();
471 : : }
472 : :
473 : :
474 : : /*
475 : : TryProcedure -
476 : : */
477 : :
478 : 0 : static bool TryProcedure (unsigned int sym)
479 : : {
480 : 0 : unsigned int son;
481 : 0 : unsigned int p;
482 : 0 : unsigned int i;
483 : 0 : unsigned int type;
484 : 0 : bool solved;
485 : 0 : bool result;
486 : :
487 : 0 : type = SymbolTable_GetType (sym);
488 : 0 : result = false;
489 : 0 : solved = true;
490 : 0 : if (type != SymbolTable_NulSym)
491 : : {
492 : 0 : if (TryDependents (type))
493 : : {
494 : : result = true;
495 : : }
496 : 0 : if (! (Lists_IsItemInList (Done, type)))
497 : : {
498 : : solved = false;
499 : : }
500 : : }
501 : 0 : p = SymbolTable_NoOfParam (sym);
502 : 0 : i = 1;
503 : 0 : while (i <= p)
504 : : {
505 : 0 : son = SymbolTable_GetNthParam (sym, i);
506 : 0 : if (TryDependents (son))
507 : : {
508 : 0 : result = true;
509 : : }
510 : 0 : if (! (Lists_IsItemInList (Done, son)))
511 : : {
512 : 0 : solved = false;
513 : : }
514 : 0 : i += 1;
515 : : }
516 : 0 : if (solved)
517 : : {
518 : : /* avoid dangling else. */
519 : 0 : if (MoveToDone (sym))
520 : : {
521 : : return true;
522 : : }
523 : : }
524 : : else
525 : : {
526 : 0 : if (MoveToToDo (sym))
527 : : {
528 : : return true;
529 : : }
530 : : }
531 : : return result;
532 : : /* static analysis guarentees a RETURN statement will be used before here. */
533 : : __builtin_unreachable ();
534 : : }
535 : :
536 : :
537 : : /*
538 : : TryUnbounded -
539 : : */
540 : :
541 : 0 : static bool TryUnbounded (unsigned int sym)
542 : : {
543 : 0 : unsigned int type;
544 : 0 : bool result;
545 : :
546 : 0 : type = SymbolTable_GetType (sym);
547 : 0 : result = TryDependents (type);
548 : 0 : if (TryMove (sym, type))
549 : : {
550 : : return true;
551 : : }
552 : : else
553 : : {
554 : 0 : return result;
555 : : }
556 : : /* static analysis guarentees a RETURN statement will be used before here. */
557 : : __builtin_unreachable ();
558 : : }
559 : :
560 : :
561 : : /*
562 : : TryParameter -
563 : : */
564 : :
565 : 0 : static bool TryParameter (unsigned int sym)
566 : : {
567 : 0 : unsigned int type;
568 : 0 : bool result;
569 : :
570 : 0 : type = SymbolTable_GetType (sym);
571 : 0 : result = TryDependents (type);
572 : 0 : if (TryMove (sym, type))
573 : : {
574 : : return true;
575 : : }
576 : : else
577 : : {
578 : 0 : return result;
579 : : }
580 : : /* static analysis guarentees a RETURN statement will be used before here. */
581 : : __builtin_unreachable ();
582 : : }
583 : :
584 : :
585 : : /*
586 : : TryDependents - returns TRUE if any alteration occurred to any
587 : : of the lists.
588 : : */
589 : :
590 : 0 : static bool TryDependents (unsigned int sym)
591 : : {
592 : 0 : if (M2Base_IsBaseType (sym))
593 : : {
594 : 0 : return TryBase (sym);
595 : : }
596 : 0 : else if (M2System_IsSystemType (sym))
597 : : {
598 : : /* avoid dangling else. */
599 : 0 : return TrySystem (sym);
600 : : }
601 : 0 : else if (SymbolTable_IsType (sym))
602 : : {
603 : : /* avoid dangling else. */
604 : 0 : return TryType (sym);
605 : : }
606 : 0 : else if (SymbolTable_IsParameter (sym))
607 : : {
608 : : /* avoid dangling else. */
609 : 0 : return TryParameter (sym);
610 : : }
611 : 0 : else if (SymbolTable_IsProcedure (sym))
612 : : {
613 : : /* avoid dangling else. */
614 : 0 : return TryProcedure (sym);
615 : : }
616 : 0 : else if (SymbolTable_IsConstString (sym))
617 : : {
618 : : /* avoid dangling else. */
619 : 0 : return MoveToDone (sym);
620 : : }
621 : 0 : else if (SymbolTable_IsConstLit (sym))
622 : : {
623 : : /* avoid dangling else. */
624 : 0 : return MoveToDone (sym);
625 : : }
626 : 0 : else if ((SymbolTable_IsVar (sym)) && ((SymbolTable_GetMode (sym)) == SymbolTable_ImmediateValue))
627 : : {
628 : : /* avoid dangling else. */
629 : 0 : return MoveToDone (sym);
630 : : }
631 : 0 : else if (SymbolTable_IsVar (sym))
632 : : {
633 : : /* avoid dangling else. */
634 : 0 : return TryVar (sym);
635 : : }
636 : 0 : else if (SymbolTable_IsUnbounded (sym))
637 : : {
638 : : /* avoid dangling else. */
639 : 0 : return TryUnbounded (sym);
640 : : }
641 : : else
642 : : {
643 : : /* avoid dangling else. */
644 : : return false;
645 : : }
646 : : /* static analysis guarentees a RETURN statement will be used before here. */
647 : : __builtin_unreachable ();
648 : : }
649 : :
650 : :
651 : : /*
652 : : DoResolveOrder - resolves the declaration order for swig (C).
653 : : */
654 : :
655 : 0 : static void DoResolveOrder (void)
656 : : {
657 : 0 : unsigned int sym;
658 : 0 : unsigned int i;
659 : 0 : unsigned int n;
660 : 0 : bool movement;
661 : :
662 : 0 : do {
663 : 0 : n = Lists_NoOfItemsInList (ToDo);
664 : 0 : movement = false;
665 : 0 : i = 1;
666 : 0 : while ((i <= n) && ! movement)
667 : : {
668 : 0 : sym = static_cast<unsigned int> (Lists_GetItemFromList (ToDo, i));
669 : 0 : movement = TryDependents (sym);
670 : 0 : i += 1;
671 : : }
672 : 0 : } while (! (! movement));
673 : 0 : }
674 : :
675 : :
676 : : /*
677 : : DoName -
678 : : */
679 : :
680 : 0 : static void DoName (unsigned int sym)
681 : : {
682 : 0 : NameKey_Name n;
683 : :
684 : 0 : n = M2AsmUtil_GetFullScopeAsmName (sym);
685 : 0 : M2Printf_fprintf1 (f, (const char *) "%a", 2, (const unsigned char *) &n, (sizeof (n)-1));
686 : 0 : }
687 : :
688 : :
689 : : /*
690 : : DoParamName -
691 : : */
692 : :
693 : 0 : static void DoParamName (unsigned int sym)
694 : : {
695 : 0 : NameKey_Name n;
696 : :
697 : 0 : n = SymbolTable_GetSymName (sym);
698 : 0 : M2Printf_fprintf1 (f, (const char *) "%a", 2, (const unsigned char *) &n, (sizeof (n)-1));
699 : 0 : }
700 : :
701 : :
702 : : /*
703 : : DoVar -
704 : : */
705 : :
706 : 0 : static void DoVar (unsigned int sym)
707 : : {
708 : 0 : M2Printf_fprintf0 (f, (const char *) "extern \"C\" ", 11);
709 : 0 : DoType (SymbolTable_GetType (sym));
710 : 0 : M2Printf_fprintf0 (f, (const char *) " ", 1);
711 : 0 : DoName (sym);
712 : 0 : M2Printf_fprintf0 (f, (const char *) ";\\n", 3);
713 : 0 : }
714 : :
715 : :
716 : : /*
717 : : DoType -
718 : : */
719 : :
720 : 0 : static void DoType (unsigned int sym)
721 : : {
722 : 0 : if (SymbolTable_IsPointer (sym))
723 : : {
724 : 0 : DoType (SymbolTable_GetType (sym));
725 : 0 : M2Printf_fprintf0 (f, (const char *) " *", 2);
726 : : }
727 : 0 : else if (sym == M2Base_Cardinal)
728 : : {
729 : : /* avoid dangling else. */
730 : 0 : M2Printf_fprintf0 (f, (const char *) "unsigned int", 12);
731 : : }
732 : 0 : else if (sym == M2Base_Integer)
733 : : {
734 : : /* avoid dangling else. */
735 : 0 : M2Printf_fprintf0 (f, (const char *) "int", 3);
736 : : }
737 : 0 : else if (sym == M2Base_Boolean)
738 : : {
739 : : /* avoid dangling else. */
740 : 0 : M2Printf_fprintf0 (f, (const char *) "unsigned int", 12);
741 : : }
742 : 0 : else if (sym == M2Base_LongInt)
743 : : {
744 : : /* avoid dangling else. */
745 : 0 : M2Printf_fprintf0 (f, (const char *) "long long int", 13);
746 : : }
747 : 0 : else if (sym == M2Base_LongCard)
748 : : {
749 : : /* avoid dangling else. */
750 : 0 : M2Printf_fprintf0 (f, (const char *) "long long unsigned int", 22);
751 : : }
752 : 0 : else if (sym == M2Base_Char)
753 : : {
754 : : /* avoid dangling else. */
755 : 0 : M2Printf_fprintf0 (f, (const char *) "char", 4);
756 : : }
757 : 0 : else if (sym == M2Base_ShortCard)
758 : : {
759 : : /* avoid dangling else. */
760 : 0 : M2Printf_fprintf0 (f, (const char *) "short unsigned int", 18);
761 : : }
762 : 0 : else if (sym == M2Base_ShortInt)
763 : : {
764 : : /* avoid dangling else. */
765 : 0 : M2Printf_fprintf0 (f, (const char *) "short int", 9);
766 : : }
767 : 0 : else if (sym == M2Base_Real)
768 : : {
769 : : /* avoid dangling else. */
770 : 0 : M2Printf_fprintf0 (f, (const char *) "double", 6);
771 : : }
772 : 0 : else if (sym == M2Base_LongReal)
773 : : {
774 : : /* avoid dangling else. */
775 : 0 : M2Printf_fprintf0 (f, (const char *) "long double", 11);
776 : : }
777 : 0 : else if (sym == M2Base_ShortReal)
778 : : {
779 : : /* avoid dangling else. */
780 : 0 : M2Printf_fprintf0 (f, (const char *) "float", 5);
781 : : }
782 : 0 : else if (sym == M2Bitset_Bitset)
783 : : {
784 : : /* avoid dangling else. */
785 : 0 : M2Printf_fprintf0 (f, (const char *) "unsigned int", 12);
786 : : }
787 : 0 : else if (sym == M2System_Address)
788 : : {
789 : : /* avoid dangling else. */
790 : 0 : M2Printf_fprintf0 (f, (const char *) "void *", 6);
791 : : }
792 : 0 : else if (sym == M2System_Byte)
793 : : {
794 : : /* avoid dangling else. */
795 : 0 : M2Printf_fprintf0 (f, (const char *) "unsigned char", 13);
796 : : }
797 : 0 : else if (sym == M2System_Loc)
798 : : {
799 : : /* avoid dangling else. */
800 : 0 : M2Printf_fprintf0 (f, (const char *) "unsigned char", 13);
801 : : }
802 : 0 : else if (sym == M2System_Word)
803 : : {
804 : : /* avoid dangling else. */
805 : 0 : M2Printf_fprintf0 (f, (const char *) "unsigned int", 12);
806 : : }
807 : 0 : }
808 : :
809 : :
810 : : /*
811 : : DoUnbounded -
812 : : */
813 : :
814 : 0 : static void DoUnbounded (unsigned int sym)
815 : : {
816 : 0 : NameKey_Name n;
817 : 0 : unsigned int type;
818 : :
819 : 0 : type = SymbolTable_GetType (sym);
820 : 0 : DoType (SymbolTable_GetType (type));
821 : 0 : n = SymbolTable_GetSymName (sym);
822 : 0 : M2Printf_fprintf2 (f, (const char *) " *_m2_address_%a, int _m2_high_%a", 33, (const unsigned char *) &n, (sizeof (n)-1), (const unsigned char *) &n, (sizeof (n)-1));
823 : 0 : }
824 : :
825 : :
826 : : /*
827 : : DoBasicBlock -
828 : : */
829 : :
830 : 0 : static void DoBasicBlock (unsigned int start, unsigned int end)
831 : : {
832 : 0 : if (M2Quads_IsProcedureScope (start))
833 : : {
834 : : /* skip this basic block, as this will not modify the parameter */
835 : : return ;
836 : : }
837 : 0 : else if (IsKnown || CanGuess)
838 : : {
839 : : /* avoid dangling else. */
840 : : /* already resolved */
841 : : return ;
842 : : }
843 : : else
844 : : {
845 : : /* avoid dangling else. */
846 : 0 : if ((ws == 0) && (rs == 0))
847 : : {
848 : 0 : FirstBasicBlock = false;
849 : : }
850 : 0 : else if (rs == 0)
851 : : {
852 : : /* avoid dangling else. */
853 : : /* only written */
854 : 0 : if (ws <= end)
855 : : {
856 : 0 : Output = true;
857 : 0 : if (FirstBasicBlock)
858 : : {
859 : 0 : IsKnown = true;
860 : : }
861 : : else
862 : : {
863 : 0 : CanGuess = true;
864 : : }
865 : 0 : FirstBasicBlock = false;
866 : : }
867 : : }
868 : 0 : else if (ws == 0)
869 : : {
870 : : /* avoid dangling else. */
871 : : /* only read */
872 : 0 : Input = true;
873 : 0 : if ((rs <= end) && FirstBasicBlock)
874 : : {
875 : 0 : IsKnown = true;
876 : : }
877 : : else
878 : : {
879 : 0 : CanGuess = true;
880 : : }
881 : 0 : FirstBasicBlock = false;
882 : : }
883 : 0 : else if (rs <= ws)
884 : : {
885 : : /* avoid dangling else. */
886 : : /* read before write */
887 : 0 : InOut = true;
888 : 0 : if (((rs <= end) && (ws <= end)) && FirstBasicBlock)
889 : : {
890 : 0 : IsKnown = true;
891 : : }
892 : : else
893 : : {
894 : 0 : CanGuess = true;
895 : : }
896 : 0 : FirstBasicBlock = false;
897 : : }
898 : : else
899 : : {
900 : : /* avoid dangling else. */
901 : : /* must be written before read */
902 : 0 : Output = true;
903 : 0 : if (((rs <= end) && (ws <= end)) && FirstBasicBlock)
904 : : {
905 : 0 : IsKnown = true;
906 : : }
907 : : else
908 : : {
909 : 0 : CanGuess = true;
910 : : }
911 : 0 : FirstBasicBlock = false;
912 : : }
913 : : }
914 : : }
915 : :
916 : :
917 : : /*
918 : : DetermineParameter -
919 : : */
920 : :
921 : 0 : static void DetermineParameter (unsigned int procedure, unsigned int param)
922 : : {
923 : 0 : M2Scope_ScopeBlock sb;
924 : 0 : M2BasicBlock_BasicBlock bb;
925 : 0 : unsigned int we;
926 : 0 : unsigned int re;
927 : :
928 : 0 : sb = M2Scope_InitScopeBlock (procedure);
929 : 0 : bb = M2BasicBlock_InitBasicBlocks (sb);
930 : 0 : Input = false;
931 : 0 : Output = false;
932 : 0 : InOut = false;
933 : 0 : CanGuess = false;
934 : 0 : IsKnown = false;
935 : 0 : FirstBasicBlock = true;
936 : 0 : SymbolTable_GetReadQuads (param, SymbolTable_RightValue, &rs, &re);
937 : 0 : SymbolTable_GetWriteQuads (param, SymbolTable_RightValue, &ws, &we);
938 : 0 : M2BasicBlock_ForeachBasicBlockDo (bb, (M2BasicBlock_BasicBlockProc) {(M2BasicBlock_BasicBlockProc_t) DoBasicBlock});
939 : 0 : M2BasicBlock_KillBasicBlocks (&bb);
940 : 0 : M2Scope_KillScopeBlock (&sb);
941 : 0 : }
942 : :
943 : :
944 : : /*
945 : : PrintDirection -
946 : : */
947 : :
948 : 0 : static void PrintDirection (void)
949 : : {
950 : 0 : if (Input)
951 : : {
952 : 0 : M2Printf_fprintf0 (f, (const char *) "INPUT", 5);
953 : : }
954 : 0 : else if (Output)
955 : : {
956 : : /* avoid dangling else. */
957 : 0 : M2Printf_fprintf0 (f, (const char *) "OUTPUT", 6);
958 : : }
959 : : else
960 : : {
961 : : /* avoid dangling else. */
962 : 0 : M2Printf_fprintf0 (f, (const char *) "INOUT", 5);
963 : : }
964 : 0 : }
965 : :
966 : :
967 : : /*
968 : : CalculateVarDirective -
969 : : */
970 : :
971 : 0 : static void CalculateVarDirective (unsigned int procedure, unsigned int param, bool annotate)
972 : : {
973 : 0 : unsigned int sym;
974 : :
975 : 0 : sym = SymbolTable_GetParameterShadowVar (param);
976 : 0 : if (sym == SymbolTable_NulSym)
977 : : {
978 : 0 : M2Error_InternalError ((const char *) "why did we get here", 19);
979 : : }
980 : : else
981 : : {
982 : 0 : DetermineParameter (procedure, sym);
983 : 0 : if (annotate)
984 : : {
985 : 0 : DoParamName (sym);
986 : 0 : if (IsKnown)
987 : : {
988 : 0 : M2Printf_fprintf0 (f, (const char *) " is known to be an ", 19);
989 : 0 : PrintDirection ();
990 : : }
991 : 0 : else if (CanGuess)
992 : : {
993 : : /* avoid dangling else. */
994 : 0 : M2Printf_fprintf0 (f, (const char *) " is guessed to be an ", 21);
995 : 0 : PrintDirection ();
996 : : }
997 : : else
998 : : {
999 : : /* avoid dangling else. */
1000 : 0 : M2Printf_fprintf0 (f, (const char *) " is unknown", 11);
1001 : : }
1002 : : }
1003 : : else
1004 : : {
1005 : 0 : M2Printf_fprintf0 (f, (const char *) "*", 1);
1006 : 0 : if (IsKnown || CanGuess)
1007 : : {
1008 : 0 : PrintDirection ();
1009 : : }
1010 : : else
1011 : : {
1012 : 0 : DoParamName (sym);
1013 : : }
1014 : : }
1015 : : }
1016 : 0 : }
1017 : :
1018 : :
1019 : : /*
1020 : : AnnotateProcedure -
1021 : : */
1022 : :
1023 : 0 : static void AnnotateProcedure (unsigned int sym)
1024 : : {
1025 : 0 : unsigned int son;
1026 : 0 : unsigned int p;
1027 : 0 : unsigned int i;
1028 : 0 : bool needComma;
1029 : :
1030 : 0 : M2Printf_fprintf0 (f, (const char *) "/* parameter: ", 15);
1031 : 0 : p = SymbolTable_NoOfParam (sym);
1032 : 0 : i = 1;
1033 : 0 : needComma = false;
1034 : 0 : while (i <= p)
1035 : : {
1036 : 0 : son = SymbolTable_GetNthParam (sym, i);
1037 : 0 : if (SymbolTable_IsParameterVar (son))
1038 : : {
1039 : 0 : if (needComma)
1040 : : {
1041 : 0 : M2Printf_fprintf0 (f, (const char *) ", ", 2);
1042 : : }
1043 : 0 : CalculateVarDirective (sym, son, true);
1044 : 0 : needComma = true;
1045 : : }
1046 : 0 : i += 1;
1047 : : }
1048 : 0 : M2Printf_fprintf0 (f, (const char *) " */\\n\\n", 7);
1049 : 0 : }
1050 : :
1051 : :
1052 : : /*
1053 : : DoProcedure -
1054 : : */
1055 : :
1056 : 0 : static bool DoProcedure (unsigned int sym)
1057 : : {
1058 : 0 : unsigned int son;
1059 : 0 : unsigned int p;
1060 : 0 : unsigned int i;
1061 : 0 : bool found;
1062 : :
1063 : 0 : found = false;
1064 : 0 : M2Printf_fprintf0 (f, (const char *) "extern \"C\" ", 11);
1065 : 0 : if ((SymbolTable_GetType (sym)) == SymbolTable_NulSym)
1066 : : {
1067 : 0 : M2Printf_fprintf0 (f, (const char *) "void", 4);
1068 : : }
1069 : : else
1070 : : {
1071 : 0 : DoType (SymbolTable_GetType (sym));
1072 : : }
1073 : 0 : M2Printf_fprintf0 (f, (const char *) " ", 1);
1074 : 0 : DoName (sym);
1075 : 0 : M2Printf_fprintf0 (f, (const char *) " (", 2);
1076 : 0 : p = SymbolTable_NoOfParam (sym);
1077 : 0 : if (p == 0)
1078 : : {
1079 : 0 : M2Printf_fprintf0 (f, (const char *) "void", 4);
1080 : : }
1081 : : else
1082 : : {
1083 : : i = 1;
1084 : 0 : while (i <= p)
1085 : : {
1086 : 0 : son = SymbolTable_GetNthParam (sym, i);
1087 : 0 : if (SymbolTable_IsUnboundedParam (sym, i))
1088 : : {
1089 : 0 : DoUnbounded (son);
1090 : : }
1091 : : else
1092 : : {
1093 : 0 : DoType (SymbolTable_GetType (son));
1094 : 0 : M2Printf_fprintf0 (f, (const char *) " ", 1);
1095 : 0 : if (SymbolTable_IsParameterVar (son))
1096 : : {
1097 : 0 : found = true;
1098 : 0 : CalculateVarDirective (sym, son, false);
1099 : : }
1100 : : else
1101 : : {
1102 : 0 : DoParamName (son);
1103 : : }
1104 : : }
1105 : 0 : if (i < p)
1106 : : {
1107 : 0 : M2Printf_fprintf0 (f, (const char *) ", ", 2);
1108 : : }
1109 : 0 : i += 1;
1110 : : }
1111 : : }
1112 : 0 : M2Printf_fprintf0 (f, (const char *) ");\\n", 4);
1113 : 0 : return found;
1114 : : /* static analysis guarentees a RETURN statement will be used before here. */
1115 : : __builtin_unreachable ();
1116 : : }
1117 : :
1118 : :
1119 : : /*
1120 : : DoWriteSymbol -
1121 : : */
1122 : :
1123 : 0 : static void DoWriteSymbol (unsigned int sym)
1124 : : {
1125 : 0 : if (M2Base_IsBaseType (sym))
1126 : : {} /* empty. */
1127 : 0 : else if (M2System_IsSystemType (sym))
1128 : : {
1129 : : /* avoid dangling else. */
1130 : : }
1131 : 0 : else if (SymbolTable_IsType (sym))
1132 : : {
1133 : : /* avoid dangling else. */
1134 : : }
1135 : 0 : else if (SymbolTable_IsProcedure (sym))
1136 : : {
1137 : : /* avoid dangling else. */
1138 : 0 : if (DoProcedure (sym))
1139 : : {
1140 : 0 : AnnotateProcedure (sym);
1141 : : }
1142 : : }
1143 : 0 : else if (SymbolTable_IsConstString (sym))
1144 : : {
1145 : : /* avoid dangling else. */
1146 : : }
1147 : 0 : else if (SymbolTable_IsConstLit (sym))
1148 : : {
1149 : : /* avoid dangling else. */
1150 : : }
1151 : 0 : else if ((SymbolTable_IsVar (sym)) && ((SymbolTable_GetMode (sym)) == SymbolTable_ImmediateValue))
1152 : : {
1153 : : /* avoid dangling else. */
1154 : : }
1155 : 0 : else if (SymbolTable_IsVar (sym))
1156 : : {
1157 : : /* avoid dangling else. */
1158 : 0 : DoVar (sym);
1159 : : }
1160 : 0 : }
1161 : :
1162 : :
1163 : : /*
1164 : : DoCheckExported -
1165 : : */
1166 : :
1167 : 0 : static void DoCheckExported (unsigned int sym)
1168 : : {
1169 : 0 : if (SymbolTable_IsExported (mainModule, sym))
1170 : : {
1171 : 0 : DoWriteSymbol (sym);
1172 : : }
1173 : 0 : }
1174 : :
1175 : :
1176 : : /*
1177 : : IsUnique - returns TRUE if the combination of, n, and, t,
1178 : : is unique.
1179 : : */
1180 : :
1181 : 0 : static bool IsUnique (NameKey_Name n, unsigned int t)
1182 : : {
1183 : 0 : M2Swig_UnboundedSig p;
1184 : 0 : unsigned int h;
1185 : 0 : unsigned int i;
1186 : :
1187 : 0 : i = 1;
1188 : 0 : h = Indexing_HighIndice (uKey);
1189 : 0 : while (i <= h)
1190 : : {
1191 : 0 : p = static_cast<M2Swig_UnboundedSig> (Indexing_GetIndice (uKey, i));
1192 : 0 : if ((p->type == t) && (p->name == n))
1193 : : {
1194 : : return false;
1195 : : }
1196 : 0 : i += 1;
1197 : : }
1198 : 0 : h += 1;
1199 : 0 : Storage_ALLOCATE ((void **) &p, sizeof (M2Swig__T1));
1200 : 0 : p->type = t;
1201 : 0 : p->name = n;
1202 : 0 : Indexing_PutIndice (uKey, h, reinterpret_cast<void *> (p));
1203 : 0 : return true;
1204 : : /* static analysis guarentees a RETURN statement will be used before here. */
1205 : : __builtin_unreachable ();
1206 : : }
1207 : :
1208 : :
1209 : : /*
1210 : : IsTypeUnique - returns TRUE if type, t, has not been entered yet.
1211 : : */
1212 : :
1213 : 0 : static bool IsTypeUnique (unsigned int t)
1214 : : {
1215 : 0 : M2Swig_UnboundedSig p;
1216 : 0 : unsigned int h;
1217 : 0 : unsigned int i;
1218 : :
1219 : 0 : i = 1;
1220 : 0 : h = Indexing_HighIndice (uKey);
1221 : 0 : while (i <= h)
1222 : : {
1223 : 0 : p = static_cast<M2Swig_UnboundedSig> (Indexing_GetIndice (uKey, i));
1224 : 0 : if (p->type == t)
1225 : : {
1226 : : return false;
1227 : : }
1228 : 0 : i += 1;
1229 : : }
1230 : : return true;
1231 : : /* static analysis guarentees a RETURN statement will be used before here. */
1232 : : __builtin_unreachable ();
1233 : : }
1234 : :
1235 : :
1236 : : /*
1237 : : DoCheckUnbounded -
1238 : : */
1239 : :
1240 : 0 : static void DoCheckUnbounded (unsigned int sym)
1241 : : {
1242 : 0 : NameKey_Name name;
1243 : 0 : unsigned int type;
1244 : 0 : bool typeUnique;
1245 : :
1246 : 0 : if ((SymbolTable_IsParameter (sym)) && (SymbolTable_IsParameterUnbounded (sym)))
1247 : : {
1248 : 0 : name = SymbolTable_GetSymName (sym);
1249 : 0 : type = SymbolTable_GetType (SymbolTable_GetType (sym));
1250 : 0 : typeUnique = IsTypeUnique (type);
1251 : 0 : if (IsUnique (name, type))
1252 : : {
1253 : 0 : if (! includedArray)
1254 : : {
1255 : 0 : includedArray = true;
1256 : 0 : M2Printf_fprintf0 (f, (const char *) "%include \"carrays.i\"\\n", 22);
1257 : : }
1258 : 0 : M2Printf_fprintf0 (f, (const char *) "%", 1);
1259 : 0 : M2Printf_fprintf0 (f, (const char *) "apply (char *STRING, int LENGTH) { (", 36);
1260 : 0 : DoUnbounded (sym);
1261 : 0 : M2Printf_fprintf0 (f, (const char *) ") };\\n", 6);
1262 : 0 : if (typeUnique)
1263 : : {
1264 : 0 : M2Printf_fprintf0 (f, (const char *) "%array_functions(", 17);
1265 : 0 : DoType (type);
1266 : 0 : M2Printf_fprintf0 (f, (const char *) ", ", 2);
1267 : 0 : DoType (type);
1268 : 0 : M2Printf_fprintf0 (f, (const char *) "Array);\\n", 9);
1269 : : }
1270 : : }
1271 : : }
1272 : 0 : }
1273 : :
1274 : :
1275 : : /*
1276 : : DoWriteFile -
1277 : : */
1278 : :
1279 : 0 : static void DoWriteFile (unsigned int sym)
1280 : : {
1281 : 0 : NameKey_Name n;
1282 : :
1283 : 0 : mainModule = sym;
1284 : 0 : n = SymbolTable_GetSymName (sym);
1285 : 0 : M2Printf_fprintf0 (f, (const char *) "/* automatically generated by gm2 -fswig */\\n", 45);
1286 : 0 : M2Printf_fprintf0 (f, (const char *) "%", 1);
1287 : 0 : M2Printf_fprintf1 (f, (const char *) "module %a\\n\\n", 13, (const unsigned char *) &n, (sizeof (n)-1));
1288 : 0 : M2Printf_fprintf0 (f, (const char *) "%", 1);
1289 : 0 : M2Printf_fprintf1 (f, (const char *) "include exception.i\\n\\n", 23, (const unsigned char *) &n, (sizeof (n)-1));
1290 : 0 : M2Printf_fprintf0 (f, (const char *) "%", 1);
1291 : 0 : M2Printf_fprintf0 (f, (const char *) "exception {\\n", 13);
1292 : 0 : M2Printf_fprintf0 (f, (const char *) " try {\\n", 9);
1293 : 0 : M2Printf_fprintf0 (f, (const char *) " $action\\n", 14);
1294 : 0 : M2Printf_fprintf0 (f, (const char *) " } catch (int i) {\\n", 21);
1295 : 0 : M2Printf_fprintf0 (f, (const char *) " return NULL;\\n", 19);
1296 : 0 : M2Printf_fprintf0 (f, (const char *) " }\\n", 5);
1297 : 0 : M2Printf_fprintf0 (f, (const char *) "}\\n\\n", 5);
1298 : 0 : Lists_ForeachItemInListDo (Done, (SymbolKey_PerformOperation) {(SymbolKey_PerformOperation_t) DoCheckUnbounded});
1299 : 0 : M2Printf_fprintf0 (f, (const char *) "\\n%{\\n", 6);
1300 : 0 : Lists_ForeachItemInListDo (Done, (SymbolKey_PerformOperation) {(SymbolKey_PerformOperation_t) DoCheckExported});
1301 : 0 : M2Printf_fprintf0 (f, (const char *) "%}\\n\\n", 6);
1302 : 0 : Lists_ForeachItemInListDo (Done, (SymbolKey_PerformOperation) {(SymbolKey_PerformOperation_t) DoCheckExported});
1303 : 0 : }
1304 : :
1305 : :
1306 : : /*
1307 : : DoGenerateSwig -
1308 : : */
1309 : :
1310 : 0 : static void DoGenerateSwig (unsigned int sym)
1311 : : {
1312 : 0 : Init ();
1313 : 0 : name = DynamicStrings_ConCat (DynamicStrings_InitStringCharStar (NameKey_KeyToCharStar (SymbolTable_GetSymName (sym))), DynamicStrings_Mark (DynamicStrings_InitString ((const char *) ".i", 2)));
1314 : 0 : f = SFIO_OpenToWrite (name);
1315 : 0 : SymbolTable_ForeachExportedDo (sym, (SymbolKey_PerformOperation) {(SymbolKey_PerformOperation_t) DoExported});
1316 : 0 : DoResolveOrder ();
1317 : 0 : DoWriteFile (sym);
1318 : 0 : FIO_Close (f);
1319 : 0 : name = DynamicStrings_KillString (name);
1320 : 0 : Kill ();
1321 : 0 : }
1322 : :
1323 : :
1324 : : /*
1325 : : Init -
1326 : : */
1327 : :
1328 : 0 : static void Init (void)
1329 : : {
1330 : 0 : Lists_InitList (&Done);
1331 : 0 : Lists_InitList (&ToDo);
1332 : 0 : uKey = Indexing_InitIndex (1);
1333 : 0 : includedArray = false;
1334 : 0 : }
1335 : :
1336 : :
1337 : : /*
1338 : : Kill -
1339 : : */
1340 : :
1341 : 0 : static void Kill (void)
1342 : : {
1343 : 0 : Lists_KillList (&Done);
1344 : 0 : Lists_KillList (&ToDo);
1345 : 0 : uKey = Indexing_KillIndex (uKey);
1346 : 0 : }
1347 : :
1348 : :
1349 : : /*
1350 : : GenerateSwigFile - if the -fswig option was specified then generate
1351 : : a swig interface file for the main module.
1352 : : */
1353 : :
1354 : 12328 : extern "C" void M2Swig_GenerateSwigFile (unsigned int sym)
1355 : : {
1356 : 12328 : if (M2Options_GenerateSwig)
1357 : : {
1358 : 0 : DoGenerateSwig (sym);
1359 : : }
1360 : 12328 : }
1361 : :
1362 : 12963 : extern "C" void _M2_M2Swig_init (__attribute__((unused)) int argc,__attribute__((unused)) char *argv[],__attribute__((unused)) char *envp[])
1363 : : {
1364 : 12963 : }
1365 : :
1366 : 0 : extern "C" void _M2_M2Swig_fini (__attribute__((unused)) int argc,__attribute__((unused)) char *argv[],__attribute__((unused)) char *envp[])
1367 : : {
1368 : 0 : }
|