home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / me34src.zip / me3 / mc / supp.c < prev    next >
C/C++ Source or Header  |  1995-01-14  |  7KB  |  289 lines

  1. /*
  2.  * supp.c : support routines for the Mutt compiler
  3.  *     label routines
  4.  */
  5.  
  6. /* Copyright 1990, 1991, 1992 Craig Durland
  7.  *   Distributed under the terms of the GNU General Public License.
  8.  *   Distributed "as is", without warranties of any kind, but comments,
  9.  *     suggestions and bug reports are welcome.
  10.  */
  11.  
  12. #include <stdio.h>
  13. #include <os.h>
  14. #include <dtable.h>
  15. #include "mm.h"
  16. #include "mc.h"
  17.  
  18. extern address pcaddr();
  19. extern char
  20.   ebuf[],
  21.   *save_string(), *spoof();
  22. extern FILE *lstfile;    /* in mc.c */
  23.  
  24. /* ******************************************************************** */
  25. /* ************************** Error Routines ************************** */
  26. /* ******************************************************************** */
  27.  
  28. extern char *muttfile;            /* in mc.c */
  29. extern int srcline, errors, warnings;    /* in mc.c */
  30.  
  31. void errmsg(errtype,msg) char *errtype, *msg;
  32. {
  33.   char buf[300];
  34.  
  35.   spoof(buf,"%s %d %s: %s\n",muttfile,srcline,errtype,msg);
  36.   if (lstfile) fputs(buf,lstfile);
  37.   fputs(buf,stdout);
  38. }
  39.  
  40. int no_warn = FALSE, no_gripe = FALSE;
  41.  
  42. void gripe(msg) char *msg; { if (!no_gripe) errmsg("Note",msg); }
  43. void groan(msg) char *msg;
  44. {
  45.   warnings++; if (!no_warn) errmsg("Warning",msg);
  46. }
  47. void moan(msg)  char *msg; { errors++;   errmsg("Error",msg); }
  48. void bitch(msg) char *msg; { moan(msg);  exit(1); }
  49.  
  50.     /* Internal error : print message and bail out */
  51. bail(msg) char *msg;
  52. {
  53.   fprintf(stderr,"Internal error!  %s\n",msg);
  54.   exit(1);
  55. }
  56.  
  57. /* ******************************************************************** */
  58. /* ******************************* Misc ******************************* */
  59. /* ******************************************************************** */
  60.  
  61. is_object_type(type)
  62. {
  63.   return (type == LIST || type == STRING);
  64. }
  65.  
  66. /* ******************************************************************** */
  67. /* ************************** Label Routines ************************** */
  68. /* ******************************************************************** */
  69.  
  70. static declare_and_init_dTable(label_table, address);
  71.  
  72. genlabel()
  73. {
  74.   static int labels = 0;
  75.  
  76.   if (!xpand_dTable(&label_table, 1, 100,100))
  77.     bail("Out of memory! Can't expand label table.");
  78.   
  79.   label_table.table[labels] = NIL;
  80.   return labels++;
  81. }
  82.  
  83. address getlabel(label) { return label_table.table[label]; }
  84.  
  85. void stufflabel(label)
  86. {
  87.   if (lstfile) fprintf(lstfile,"%4u: L%d:\n",pcaddr(),label);
  88.   label_table.table[label] = pcaddr();
  89. }
  90.  
  91. /* ******************************************************************** */
  92. /* **************************** Type info ***************************** */
  93. /* ******************************************************************** */
  94.  
  95. typesize(type) unsigned int type;
  96. {
  97.   if ((type & POINTER) || type == BLOB) return sizeof(int32);
  98.   switch(type)
  99.   {
  100.     case INT8: case BOOLEAN: case STRING: return sizeof(uint8);
  101.     case INT16: return sizeof(int16);
  102.     case INT32: return sizeof(int32);
  103.     case FCNPTR: moan("typesize(FCNPTR)"); return 4;
  104.   }
  105.   /* NOTREACHED */
  106. }
  107.  
  108. unsigned int mmtype(type) unsigned int type;
  109. {
  110. /*if (type & MCTYPE) bitch(spoof(ebuf,"mmtype(%d): not a MMable type.",type));*/
  111.   if (type & POINTER) return BLOB;
  112.   switch (type)
  113.   {
  114.     case INT8: case INT16: case INT32: return NUMBER;
  115.   }
  116.   return type;
  117. }
  118.  
  119. char *keyword_type_name(type)
  120. {
  121.   switch (type)
  122.   {
  123.     case KWMutt:    return "Mutt keyword";
  124.     case KWoMutt:    return "oMutt keyword";
  125.     case KWXToken:    return "external token";
  126.     case KWGlobalVar:    return "global variable";
  127.     case KWLocalVar:    return "local variable";
  128.     case KWConst:    return "constant";
  129.     case KWProgram:    return "program";
  130.     case KWProto:    return "arg prototype";
  131.   }
  132.   return "Unknown";
  133. }
  134.  
  135. /* ******************************************************************** */
  136. /* ************************ Consts ************************************ */
  137. /* ******************************************************************** */
  138.  
  139. extern KeyWord *global_look_up(), *global_look_for(), *global_check();
  140.  
  141. /* Here is where I save (const foo <value>) */
  142.  
  143. typedef struct { char *name; MMDatum rv; } Const;
  144.  
  145. static int nconsts = 0;
  146.  
  147. static declare_and_init_dTable(consts, Const);
  148.  
  149. #define CID_TO_CONST(n) (&consts.table[n].rv)
  150. MMDatum *cid_to_const(n) { return CID_TO_CONST(n); }
  151.  
  152. MMDatum *getconst(name) char *name;
  153. {
  154.   KeyWord *kw;
  155.  
  156.   if (kw = global_look_for(name, KWConst)) return CID_TO_CONST(kw->token);
  157.   return NULL;
  158. }
  159.  
  160. void add_const(name,rv) char *name; MMDatum *rv;
  161. {
  162.   char *ptr;
  163.  
  164.   if (global_check(name)) return;
  165.  
  166.   if (!xpand_dTable(&consts, 1, 10,10))
  167.     bail("Out of memory! Can't expand constant table.");
  168.  
  169.   ptr = save_string(name);
  170.   consts.table[nconsts].name = ptr;
  171.   consts.table[nconsts].rv = *rv;
  172.  
  173.   add_keyword(ptr, KWConst, nconsts, 3);
  174.  
  175.   nconsts++;
  176. }
  177.  
  178. number_of_consts() { return nconsts; }
  179.  
  180. /* ******************************************************************** */
  181. /* ***************************** Programs ***************************** */
  182. /* ******************************************************************** */
  183.  
  184. typedef struct { char *name; address addr; uint8 flags; } PgmTable;
  185.  
  186. #define pid_to_Pgm(n) (&pgm_table.table[n])
  187.  
  188. static int num_pgms = 0;
  189. static declare_and_init_dTable(pgm_table, PgmTable);
  190.  
  191. void pgm_init()
  192. {
  193. }
  194.  
  195. address get_pgm(name) char *name;
  196. {
  197.   KeyWord *kw;
  198.   PgmTable *pgm;
  199.  
  200.   if ((kw = global_look_up(name)) && kw->type == KWProgram)
  201.   {
  202.     pgm = pid_to_Pgm(kw->token);
  203.     if (pgm->flags & LEAR)    /* !!!??? */
  204.       moan(spoof(ebuf,"%s is special and can't be used.",pgm->name));
  205.     return pgm->addr;
  206.   }
  207.  
  208.   return NIL;
  209. }
  210.  
  211. int add_pgm(name) char *name;
  212. {
  213.   char *ptr;
  214.   int i, flags, hash_it;
  215.   KeyWord *kw;
  216.   PgmTable *the_pgm;
  217.  
  218.   flags = 0;
  219.   hash_it = TRUE;
  220.  
  221.   if (0 == strcmp(name,"MAIN"))
  222.   {
  223.     static int nth_main = 000;
  224.     char main_name[20];
  225.  
  226.     sprintf(main_name,"MAIN-%04d",++nth_main);        /* MAIN-0001 */
  227.     name = main_name;
  228.     flags = (MAIN | HIDDEN | LEAR);
  229.     hash_it = FALSE;    /* Don't put MAINs in the look up table */
  230.   }
  231.  
  232.   if (find_local_var(name) != -1)
  233.     moan(spoof(ebuf,"\"%s\" is a local variable or arg.",name));
  234.   else
  235.     if (kw = global_check(name))
  236.     {
  237.       if (kw->type == KWProgram) return kw->token;
  238.       /* else go ahead and add it */
  239.     }
  240.  
  241.   if (!xpand_dTable(&pgm_table, 1, 400,100))
  242.     bail("Out of memory! Can't expand pgm table.");
  243.  
  244.   i = num_pgms++;
  245.   the_pgm = pid_to_Pgm(i);
  246.  
  247.   ptr = save_string(name, TRUE);
  248.   the_pgm->name = ptr;
  249.  
  250.   the_pgm->addr = pcaddr(); the_pgm->flags = flags;
  251.  
  252.   if (hash_it) add_keyword(ptr, KWProgram, i, 3);
  253.  
  254.   return i;
  255. }
  256.  
  257. pgms() { return num_pgms; }
  258. void modpgm(n,f)   { pid_to_Pgm(n)->flags |= f; }
  259.  
  260. address pgmaddr(n) { return      pid_to_Pgm(n)->addr;  }
  261. char *pgmname(n)   { return      pid_to_Pgm(n)->name;  }
  262. pgmflags(n)       { return (int)pid_to_Pgm(n)->flags; }
  263.  
  264.     /* Return the id of the next pgm with id >= n, -1 if none */
  265. get_main(n)
  266. {
  267.   for (; n < num_pgms; n++) if (pid_to_Pgm(n)->flags & MAIN) return n;
  268.   return -1;
  269. }
  270.  
  271. static int compare2pgms(a,b) PgmTable *a, *b;
  272. { return strcmp(a->name,b->name); }
  273.  
  274. void sort_pgms()
  275. {
  276.   ssort(pgm_table.table, num_pgms, sizeof(PgmTable), compare2pgms);
  277. }
  278.  
  279. void print_entry_points(lstfile) FILE *lstfile;
  280. {
  281.   extern address entrypt;    /* in code.c */
  282.   int j;
  283.  
  284.   fputs("\n   Program Table\nAddress    Name\n",lstfile);
  285.   fprintf(lstfile," %5d     entry point\n",entrypt);
  286.   for (j = 0; j < num_pgms; j++)
  287.     fprintf(lstfile," %5d     %s\n",pgmaddr(j),pgmname(j));
  288. }
  289.