LIST.C
上传用户:hlzzc88
上传日期:2007-01-06
资源大小:220k
文件大小:6k
源码类别:

编译器/解释器

开发平台:

Others

  1. /*
  2.  * 68K/386 32-bit C compiler.
  3.  *
  4.  * copyright (c) 1997, David Lindauer
  5.  * 
  6.  * This compiler is intended for educational use.  It may not be used
  7.  * for profit without the express written consent of the author.
  8.  *
  9.  * It may be freely redistributed, as long as this notice remains intact
  10.  * and either the original sources or derived sources 
  11.  * are distributed along with any executables derived from the originals.
  12.  *
  13.  * The author is not responsible for any damages that may arise from use
  14.  * of this software, either idirect or consequential.
  15.  *
  16.  * v1.35 March 1997
  17.  * David Lindauer, gclind01@starbase.spd.louisville.edu
  18.  *
  19.  * Credits to Mathew Brandt for original K&R C compiler
  20.  *
  21.  */
  22. /* 
  23.  * Listing file
  24.  */
  25. #include        <stdio.h>
  26. #include        "expr.h"
  27. #include        "c.h"
  28. extern int prm_listfile;
  29. extern HASHREC **globalhash;
  30. extern FILE *listFile;
  31. extern TABLE gsyms;
  32. extern char *registers[];
  33. /* Unnamed structure tags */
  34. char *tn_unnamed = "<no name> ";
  35. static char * unmangledname(char *str)
  36. {
  37. static char name[40];
  38. unmangle(name,str);
  39. return name;
  40. }
  41. /* Put the storage class */
  42. void put_sc(int scl)
  43. {
  44.   if (!prm_listfile)
  45.     return;
  46.        switch(scl) {
  47.                 case sc_static:
  48.                         fprintf(listFile,"Static      ");
  49.                         break;
  50.                 case sc_auto:
  51.                         fprintf(listFile,"Auto        ");
  52.                         break;
  53.                 case sc_autoreg:
  54.                 case sc_memberreg:
  55.                         fprintf(listFile,"Register    ");
  56.                         break;
  57.                 case sc_global:
  58.                         fprintf(listFile,"Global      ");
  59.                         break;
  60.                 case sc_abs:
  61.                         fprintf(listFile,"Absolute    ");
  62.                         break;
  63.                 case sc_external:
  64.                 case sc_externalfunc:
  65.                         fprintf(listFile,"External    ");
  66.                         break;
  67.                 case sc_type:
  68.                         fprintf(listFile,"Type        ");
  69.                         break;
  70.                 case sc_const:
  71.                         fprintf(listFile,"Constant    ");
  72.                         break;
  73.                 case sc_member:
  74.                         fprintf(listFile,"Member      ");
  75.                         break;
  76.                 case sc_label:
  77.                         fprintf(listFile,"Label");
  78.                         break;
  79.                 case sc_ulabel:
  80.                         fprintf(listFile,"Undefined label");
  81.                         break;
  82.                 }
  83. }
  84. /* Put the type */
  85. void put_ty(TYP *tp)
  86. {       if((tp == 0) || (!prm_listfile))
  87.                 return;
  88.         switch(tp->type) {
  89. case bt_matchall:
  90. fprintf(listFile,"Undefined");
  91. break;
  92.                 case bt_char:
  93.                         fprintf(listFile,"Char");
  94.                         break;
  95.                 case bt_short:
  96.                         fprintf(listFile,"Short");
  97.                         break;
  98.                 case bt_enum:
  99.                         fprintf(listFile,"enum ");
  100.                         goto ucont;
  101.                 case bt_long:
  102.                         fprintf(listFile,"Long");
  103.                         break;
  104.                 case bt_unsigned:
  105.                         fprintf(listFile,"Unsigned Long");
  106.                         break;
  107.                 case bt_float:
  108.                         fprintf(listFile,"Float");
  109.                         break;
  110.                 case bt_double:
  111.                         fprintf(listFile,"Double");
  112.                         break;
  113.                 case bt_longdouble:
  114.                         fprintf(listFile,"Long Double");
  115.                         break;
  116.                 case bt_pointer:
  117.                         if( tp->val_flag == 0)
  118.                                 fprintf(listFile,"Pointer to ");
  119.                         else
  120.                                 fprintf(listFile,"Array of ");
  121.                         put_ty(tp->btp);
  122.                         break;
  123.                 case bt_union:
  124.                         fprintf(listFile,"union ");
  125.                         goto ucont;
  126.                 case bt_struct:
  127.                         fprintf(listFile,"struct ");
  128. ucont:                  if(tp->sname == 0)
  129.                                 fprintf(listFile,tn_unnamed);
  130.                         else
  131.                                 fprintf(listFile,"%s ",unmangledname(tp->sname));
  132.                         break;
  133. case bt_void:
  134. fprintf(listFile,"Void");
  135. break;
  136. case bt_ptrfunc:
  137.                         fprintf(listFile,"Pointer to ");
  138.                 case bt_ifunc:
  139.                 case bt_func:
  140.                         fprintf(listFile,"Function returning ");
  141.                         put_ty(tp->btp);
  142.                         break;
  143.                 }
  144. if (tp->startbit != -1)
  145. fprintf(listFile,"  Bits %d to %d",tp->startbit,tp->startbit+tp->bits-1);
  146. }
  147. /* List a variable */
  148. void list_var(SYM *sp, int i)
  149. {       int     j;
  150. long val;
  151. if (!prm_listfile)
  152. return;
  153.         for(j = i; j; --j)
  154.                 fprintf(listFile,"    ");
  155. if ((sp->storage_class == sc_auto || sp->storage_class == sc_autoreg) && !sp->inreg)
  156. val = (long)getautoval(sp->value.i);
  157. else
  158. val = sp->value.u;
  159. if (sp->inreg)
  160.          fprintf(listFile,"%-10s = Register %-3s ",unmangledname(sp->name),registers[-val]);
  161. else
  162.          fprintf(listFile,"%-10s = %06x       ",unmangledname(sp->name),val);
  163.         put_sc(sp->storage_class);
  164.         put_ty(sp->tp);
  165.         fprintf(listFile,"n");
  166.         if(sp->tp == 0)
  167.                 return;
  168.         if((sp->tp->type == bt_struct || sp->tp->type == bt_union) &&
  169.                 sp->storage_class == sc_type)
  170.                 list_table(&(sp->tp->lst),i+1);
  171. }
  172. /* List an entire table */
  173. void list_table(TABLE *t,int j)
  174. {       SYM     *sp;
  175. int i;
  176. if (!prm_listfile)
  177. return;
  178. if (t == &gsyms) {
  179. for (i=0; i < HASHTABLESIZE; i++) {
  180. if ((sp=(SYM *) globalhash[i]) != 0) {
  181. while (sp) {
  182. list_var(sp,j);
  183.            sp = sp->next;
  184. }
  185. }
  186. }
  187. }
  188. else {
  189.          sp = t->head;
  190.          while(sp != NULL) {
  191.                 list_var(sp,j);
  192.                 sp = sp->next;
  193.                 }
  194. }
  195. }