home *** CD-ROM | disk | FTP | other *** search
/ cs.rhul.ac.uk / www.cs.rhul.ac.uk.zip / www.cs.rhul.ac.uk / pub / rdp / rdp_cs3460.tar / ml_aux.c < prev    next >
C/C++ Source or Header  |  1998-05-07  |  2KB  |  96 lines

  1. /*******************************************************************************
  2. *
  3. * RDP release 1.50 by Adrian Johnstone (A.Johnstone@rhbnc.ac.uk) 20 December 1997
  4. *
  5. * ml_aux.c - miniloop one pass compiler semantic routines
  6. *
  7. * This file may be freely distributed. Please mail improvements to the author.
  8. *
  9. *******************************************************************************/
  10. #include <stdarg.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include "textio.h"
  14. #include "memalloc.h"
  15. #include "ml_aux.h"
  16.  
  17. FILE * outfile; 
  18.  
  19. static long unsigned temp_count = 0; 
  20.  
  21. int emitf(const char * fmt, ...)
  22. {
  23.   int i; 
  24.   va_list ap;                 /* argument list walker */
  25.   
  26.   va_start(ap, fmt);          /* pass parameters to vprintf */
  27.   i = vfprintf(outfile, fmt, ap);  /* remember count of characaters printed */
  28.   va_end(ap);                 /* end of var args block */
  29.   
  30.   return i;                   /* return number of characters printed */
  31. }
  32.  
  33. void emit_open(char * sourcefilename, char * outfilename)
  34. {
  35.   if ((outfile = fopen(outfilename, "w"))== NULL)
  36.     text_message(TEXT_FATAL, "unable to open output file \'%s\' for writing\n", outfilename); 
  37.   emitf("; %s - generated from \'%s\'\n\n", outfilename, sourcefilename); 
  38.   emitf(" DATA 0x8000\n__MPP_DATA:\n CODE 0x1000\n__MPP_CODE:\n"); 
  39. }
  40.  
  41. void emit_close(void)
  42. {
  43.   emitf("\n HALT\n\n DATA\n__temp: BLOCKW %lu  ;declare array of temporaries\n\n"
  44.   " END __MPP_CODE\n", temp_count); 
  45.   fclose(outfile); 
  46. }
  47.  
  48. void emit(char * asm_op, char * alg_op, char * dst, char * src1, char * src2)
  49. {
  50.   emitf(" %s  %s, %s", asm_op, dst, src1); 
  51.   if (src2 != NULL)
  52.     emitf(", %s", src2); 
  53.   
  54.   /* Now output algebraic style */
  55.   emitf(" \t;%s := %s %s", dst, src1, alg_op); 
  56.   if (src2 != NULL)
  57.     emitf(" %s", src2); 
  58.   emitf("\n"); 
  59. }
  60.  
  61. void emit_print(char kind, char * src)
  62. {
  63.   if (kind == 'S')
  64.   {
  65.     unsigned long label = new_label(); 
  66.     
  67.     emitf("\n DATA\n__STR_%lu: STRING \"", label); 
  68.     text_print_C_string_file(outfile, src); 
  69.     emitf("\"\n\n CODE\n PRTS __STR_%lu\n", label); 
  70.   }
  71.   else
  72.   {
  73.     emitf(" PRTI "); 
  74.     text_print_C_string_file(outfile, src); 
  75.     emitf("\t;print integer\n"); 
  76.   }
  77. }
  78.  
  79. char * new_temporary(void)
  80. {
  81.   char * ret =(char *) mem_malloc(30); 
  82.   
  83.   sprintf(ret, "__temp + %lu", temp_count++); 
  84.   
  85.   return ret; 
  86. }
  87.  
  88. unsigned long new_label(void)
  89. {
  90.   static long unsigned label = 0; 
  91.   
  92.   return label++; 
  93. }
  94.  
  95. /* End of ml_aux.c */
  96.