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