OSDN Git Service

Initial Update.
[bcpl-language/BCPL.git] / compiler / debug.c
1 /*
2  *
3  *
4  */
5
6
7 #include "bcpl.h"
8 #include "parser.tab.h"
9
10
11 extern void     print_node ();
12 static void     print_symbol ();
13 static void     print_string ();
14 static void     print_number ();
15 static void     print_const ();
16 static void     print_declare ();
17 static void     print_func ();
18 static void     print_proc ();
19 static void     print_call ();
20 static void     print_expr ();
21 static void     print_args ();
22 static void     print_arglist ();
23 static void     print_statement ();
24 static void     print_return ();
25 static void     print_variable ();
26 static void     print_initialized_variable ();
27 static void     print_initial_list ();
28 static void     print_assign (struct node *nodep, int indent);
29 static void     print_if ();
30 static void     print_while ();
31 static void     print_switchon (struct node *nodep, int indent);
32 static void     print_case (struct node *nodep, int indent);
33 static void     print_repeatuntil (struct node *nodep, int indent);
34 static void     print_get (struct node *nodep, int indent);
35 static void     print_block (struct node *nodep, int indent);
36
37 static struct debug_entry       *get_entry ();
38
39
40 struct debug_entry
41 {
42   int   type;
43   char  *nicname;
44   void (*f)();
45 };
46
47
48 struct debug_entry debug_table[] =
49 {
50   { OP_DECLARE,         "declare",      print_declare   },
51   { OP_MANIFEST,        "manifest",     print_node      },
52   { OP_CONST,           "const",        print_const     },
53   { OP_GLOBAL,          "global",       print_node      },
54   { OP_STATIC,          "static",       print_node      },
55   { OP_FUNCTION,        "func",         print_func      },
56   { OP_PROCEDURE,       "proc",         print_proc      },
57   { OP_ARGS,            "args",         print_arglist   },
58   { OP_PLUSOP,          "+",            print_expr      },
59   { OP_SUBOP,           "-",            print_expr      },
60   { OP_MULTOP,          "*",            print_expr      },
61   { OP_DIVOP,           "/",            print_expr      },
62   { OP_GT,              "<",            print_expr      },
63   { OP_LT,              ">",            print_expr      },
64   { OP_GE,              "<=",           print_expr      },
65   { OP_LE,              ">=",           print_expr      },
66   { OP_AND,             "&",            print_expr      },
67   { OP_OR,              "|",            print_expr      },
68   { OP_EQUAL,           "=",            print_expr      },
69   { OP_VECIS,           "!",            print_expr      },
70
71   { OP_STATEMENT,       "-  ",          print_statement },
72   { OP_CALL,            "Call",         print_call      },
73   { OP_RETURN,          "return",       print_return    },
74   { OP_VALOF,           "valof",        print_node      },
75   { OP_VARIABLE,        "var",          print_variable  },
76   { OP_INITIALVAR,      "initvar",      print_initialized_variable },
77   { OP_SET,             "assign",       print_assign },
78   { OP_IF,              "if",           print_if },
79   { OP_WHILE,           "whlie",        print_while },
80   { OP_SWITCHON,        "switchon",     print_switchon },
81   { OP_CASE,            "case",         print_case },
82   { OP_CASECONT,        "case",         print_case },
83   { OP_REPEATUNTIL,     "repeatuntil",  print_repeatuntil },
84   { OP_GET,             "GET",          print_get },
85   { OP_BLOCK,           "BLOCK",        print_block },
86
87   { TK_SYMBOL,          "symbol",       print_symbol    },
88   { TK_STRING,          "string",       print_string    },
89   { TK_NUMBER,          "number",       print_number    },
90   { 0,                  NULL,           NULL            },
91 };
92
93
94 static struct debug_entry *
95 get_entry (nodep)
96      struct node *nodep;
97 {
98   int   i;
99
100   if (nodep == NULL)
101     return (NULL);
102
103   for (i = 0; debug_table[i].type != 0; i++)
104     {
105       if (nodep->type == debug_table[i].type)
106         {
107           return (&debug_table[i]);
108         }
109     }
110   return (NULL);
111 }
112
113
114 static
115 print_space (int n)
116 {
117   int   i;
118
119   for (i = 0; i < n; i++)
120     {
121       printf (" ");
122     }
123 }
124
125
126 void
127 debug_print (struct node *nodep, int indent)
128 {
129   struct debug_entry    *entp, *p;
130
131   if (nodep == NULL)
132     return;
133
134   entp = get_entry (nodep);
135   if (entp == NULL)
136     return;
137
138   (entp->f)(nodep, indent);
139 }
140
141
142 void
143 print_node (struct node *nodep, int indent)
144 {
145   struct debug_entry    *entp, *p;
146
147   if (nodep == NULL)
148     return;
149
150   entp = get_entry (nodep);
151   print_space (indent);
152   printf ("[%s]\n", entp->nicname);
153   if (p = get_entry (nodep->head))
154     {
155       (p->f)(nodep->head, indent + 2);
156     }
157
158   if (p = get_entry (nodep->body))
159     {
160       (p->f)(nodep->body, indent + 2);
161     }
162
163   if (p = get_entry (nodep->next))
164     {
165       (p->f)(nodep->next, indent + 2);
166     }
167 }
168
169
170 static void
171 print_symbol (struct symbol *symbolp, int indent)
172 {
173   print_space (indent);
174   printf ("<%s>", symbolp->value);
175 }
176
177
178 static void
179 print_string (struct string *strp, int indent)
180 {
181   print_space (indent);
182   printf ("<string>\"%s\"", strp->value);
183 }
184
185 static void
186 print_number (struct number *nump, int indent)
187 {
188   print_space (indent);
189   printf ("%d", nump->value);
190 }
191
192
193 static void
194 print_declare (struct node *nodep, int indent)
195 {
196   struct symbol *sp;
197   struct number *np;
198
199   sp = nodep->head;
200   print_space (indent);
201   printf ("[declare]\n");
202   debug_print (nodep->head, indent + 10);
203   if (nodep->next)
204     {
205       print_declare (nodep->next, indent);
206     }
207 }
208
209 static void
210 print_const (struct node *nodep, int indent)
211 {
212   struct symbol *sp;
213   struct number *np;
214
215   sp = nodep->head;
216   np = nodep->body;
217   print_space (indent);
218   printf ("[const]\t%s,%d\n", sp->value, np->value);
219   if (nodep->next)
220     {
221       print_const (nodep->next, indent);
222     }
223 }
224
225 static void
226 print_func (struct node *nodep, int indent)
227 {
228   struct symbol *sp;
229   struct nodep  *argp;
230   struct nodep  *exp;
231
232   sp = nodep->head;
233   argp = nodep->body;
234   exp = nodep->next;
235
236   print_space (indent);
237   printf ("[function] %s\n", sp->value);
238   debug_print ((void *)argp, indent + 2);
239   debug_print ((void *)exp, indent + 2);
240 /*  printf ("\n"); ** */
241 }
242
243 static void
244 print_proc (struct node *nodep, int indent)
245 {
246   struct symbol *sp;
247   struct nodep  *argp;
248   struct nodep  *body;
249
250   sp = nodep->head;
251   argp = nodep->body;
252   body = nodep->next;
253
254   print_space (indent);
255   printf ("[procedure] %s\n", sp->value);
256   debug_print ((void *)argp, indent + 2);
257   debug_print ((void *)body, indent + 2);
258 }
259
260
261 static void
262 print_call (struct node *nodep, int indent)
263 {
264   struct symbol *sp;
265   struct node   *argp;
266
267   sp = nodep->head;
268   argp = nodep->body;
269
270   print_space (indent);
271   printf ("[call] %s(", sp->value);
272   print_args ((void *)argp, indent + 2);
273   printf (")");
274 }
275
276
277 static void
278 print_arglist (struct node *nodep, int indent)
279 {
280   print_space (indent);
281   printf ("[args] ");
282   print_args (nodep, indent);
283   printf ("\n");
284 }
285
286
287 static void
288 print_args (struct node *nodep, int indent)
289 {
290   struct symbol *sp;
291   struct node   *argp;
292
293   if (nodep == NULL)
294     return;
295
296   sp = nodep->head;
297   argp = nodep->next;
298   if (sp->type == TK_SYMBOL)
299     {
300       printf ("%s", sp->value);
301     }
302   else
303     {
304       debug_print ((struct node *)sp, 0);
305     }
306
307   if (argp)
308     {
309       printf (", ");
310       print_args (argp, indent);
311     }
312 }
313
314 static void
315 print_expr (struct node *nodep, int indent)
316 {
317   struct debug_entry    *entp;
318   struct node           *ex1, *ex2;
319
320   ex1 = nodep->head;
321   ex2 = nodep->body;
322   entp = get_entry (nodep);
323
324   print_space (indent);
325   printf ("(%s", entp->nicname);
326   if (ex1)
327     {
328       printf (" ");
329       debug_print (ex1, 0);
330     }
331   if (ex2)
332     {
333       printf (" ");
334       debug_print (ex2, 0);
335     }
336   printf (")");
337 }
338
339
340 static void
341 print_statement (struct node *nodep, int indent)
342 {
343   struct debug_entry    *entp;
344   struct node           *body, *next;
345
346   body = nodep->head;
347   next = nodep->next;
348   entp = get_entry (nodep);
349
350   print_space (indent);
351   printf ("[S] ");
352   if (body)
353     {
354       switch (body->type)
355         {
356         case OP_IF:
357         case OP_WHILE:
358         case OP_SWITCHON:
359         case OP_REPEATUNTIL:
360         case OP_BLOCK:
361           debug_print (body, indent);
362           break;
363
364         default:
365           debug_print (body, 0);
366           printf ("\n");
367           break;
368         }
369     }
370   if (next)
371     {
372       debug_print (next, indent);
373     }
374 }
375
376
377 static void
378 print_variable (struct node *nodep, int indent)
379 {
380   struct debug_entry    *entp;
381   struct symbol         *body;
382   struct node           *next;
383
384   body = nodep->head;
385   next = nodep->next;
386   entp = get_entry (nodep);
387
388   print_space (indent);
389   printf ("[Var] ");
390   if (body)
391     {
392       if (body->type == TK_SYMBOL)
393         {
394           printf ("%s", body->value);
395         }
396       else
397         {
398           debug_print ((struct node *)body, 0);
399         }
400     }
401   if (next)
402     {
403       printf (", ");
404       print_args (next, 0);
405     }
406 }
407
408
409 static void
410 print_assign (struct node *nodep, int indent)
411 {
412   struct node           *body, *head;
413
414   head = nodep->head;
415   body = nodep->body;
416   print_space (indent);
417   printf ("[assign] ");
418   if (head)
419     {
420       debug_print (head, 0);
421     }
422
423   printf (" := ");
424
425   if (body)
426     {
427       debug_print (body, 0);
428     }
429 }
430
431
432 static void
433 print_return (struct node *nodep, int indent)
434 {
435   struct node           *body;
436
437   body = nodep->head;
438   print_space (indent);
439   printf ("[return] ");
440   if (body)
441     {
442       debug_print (body, 0);
443     }
444 }
445
446
447 static void
448 print_initialized_variable (struct node *nodep, int indent)
449 {
450   struct node           *body, *next;
451
452   body = nodep->head;
453   next = nodep->body;
454
455   print_space (indent);
456   if (body)
457     {
458       print_variable (body, 0);
459     }
460
461   if (next)
462     {
463       printf (" = ");
464       print_initial_list (next);
465     }
466 }
467
468
469 static void
470 print_initial_list (struct node *nodep)
471 {
472   struct number *nump;
473
474   if (nodep == NULL)
475     return;
476
477   nump = nodep->head;
478   if (nump->type == TK_NUMBER)
479     {
480       printf ("%d", nump->value);
481     }
482   else
483     {
484       print_node ((struct node *)nump, 0);
485     }
486
487   if (nodep->next)
488     {
489       printf (", ");
490       print_initial_list (nodep->next);
491     }
492 }
493
494 static void
495 print_if (struct node *nodep, int indent)
496 {
497   struct node   *head, *body;
498   
499   printf ("[IF]  ( ");
500   head = nodep->head;
501   body = nodep->body;
502   debug_print ((struct node *)head, 0);
503   printf (" ) \n");
504   debug_print ((struct node *)body, indent + 4);
505 }
506
507
508 static void
509 print_while (struct node *nodep, int indent)
510 {
511   struct node   *head, *body;
512   
513   printf ("[WHILE]  ( ");
514   head = nodep->head;
515   body = nodep->body;
516   debug_print ((struct node *)head, 0);
517   printf (" )\n");
518   debug_print ((struct node *)body, indent + 4);
519 }
520
521
522 static void
523 print_switchon (struct node *nodep, int indent)
524 {
525   struct node   *expr, *caselist;
526   
527   printf ("[SWITCHON] ( ");
528   expr = nodep->head;
529   caselist = nodep->body;
530
531   debug_print (expr, 0);
532   printf (" )\n");
533   debug_print (caselist, indent + 4);
534 }
535
536
537 static void
538 print_case (struct node *nodep, int indent)
539 {
540   print_space (indent);
541   printf ("[CASE]  ( ");
542   debug_print (nodep->head, 0);
543   printf (" )\n");
544   debug_print (nodep->body, indent + 8);
545   if (nodep->next)
546     {
547       print_case (nodep->next, indent);
548     }
549 }
550
551 static void
552 print_repeatuntil (struct node *nodep, int indent)
553 {
554   struct node   *head, *body;
555   
556   printf ("[REEATUNTIL]  ( ");
557   head = nodep->head;
558   body = nodep->body;
559   debug_print ((struct node *)body, 0);
560   printf (" )\n");
561   debug_print ((struct node *)head, indent + 4);
562 }
563
564 static void
565 print_get (struct node *nodep, int indent)
566 {
567   print_space (indent);
568   printf ("[GET] ");
569   debug_print ((struct node *)nodep->body, 0);
570   printf ("\n\n");
571 }
572
573 static void
574 print_block (struct node *nodep, int indent)
575 {
576   printf ("[BLOCK]\n");
577   debug_print ((struct node *)nodep->head, indent + 4);
578   printf ("\n");
579 }
580
581