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 / mvm_aux.c < prev    next >
C/C++ Source or Header  |  1998-05-07  |  3KB  |  146 lines

  1. /*******************************************************************************
  2. *
  3. * RDP release 1.50 by Adrian Johnstone (A.Johnstone@rhbnc.ac.uk) 20 December 1997
  4. *
  5. * mvm_aux.c - Mini Virtual Machine assembler 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 <stdlib.h>
  13. #include "scan.h"
  14. #include "memalloc.h"
  15. #include "textio.h"
  16. #include "mvmasm.h"
  17. #include "mvm_aux.h"
  18.  
  19. int emit_code = 0; 
  20. int execute_sim = 0; 
  21.  
  22. static FILE * objfile = NULL; 
  23.  
  24. unsigned long * location; 
  25. unsigned long data_location; 
  26. unsigned long code_location; 
  27. unsigned long transfer = 0; 
  28.  
  29. void * last_label = NULL;     /* pointer to most recently seen label: NULLed at start of each line */
  30. void * dummy_label = NULL;    /* dummy symbol returned by current label on error */
  31.  
  32. static int emitted = 0;       /* Count of bytes emitted this line */
  33.  
  34. static int emitf(char * fmt, ...) /* conditional print to object file */
  35. {
  36.   int i; 
  37.   va_list ap;                 /* argument list walker */
  38.   
  39.   va_start(ap, fmt); 
  40.   
  41.   if (emit_code)              /* no-op if not emitting... */
  42.   {
  43.     if (emitted < 16 && text_get_echo())
  44.       i = vprintf(fmt, ap); 
  45.     vfprintf(objfile, fmt, ap);  /* ... otherwise pass to fprintf() */
  46.   }
  47.   
  48.   va_end(ap); 
  49.   
  50.   return(i);                  /* for completeness, although not used here */
  51. }
  52.  
  53. void emit_eoln(void)
  54. {
  55.   if (emit_code)
  56.     fprintf(objfile, "\n"); 
  57. }
  58.  
  59. void emit_transfer(void)
  60. {
  61.   if (emit_code)
  62.     emitted += emitf("*%.4lX", transfer); 
  63. }
  64.  
  65. void emit_loc(void)
  66. {
  67.   emitted = 0; 
  68.   emitf("%.4lX ", * location); 
  69. }
  70.  
  71. void emit_fill(void)
  72. {
  73.   if (text_get_echo())
  74.   {
  75.     while (emitted++ < 16) printf(" "); 
  76.       printf(" "); 
  77.   }
  78. }
  79.  
  80. void emit_op(int op, unsigned long oper1, unsigned long oper2, unsigned long oper3, int mode1, int mode2, int opers)
  81. {
  82.   emit1((unsigned long) op);  /* output opcode */
  83.   emit1((unsigned long)((mode1 << 4)| mode2));  /* output addressing modes */
  84.   if (opers > 0)
  85.     emit2(oper1); 
  86.   if (opers > 1)
  87.     emit2(oper2); 
  88.   if (opers > 2)
  89.     emit2(oper3); 
  90. }
  91.  
  92. void emit1(unsigned long val)
  93. {
  94.   emitted += emitf("%.2lX", val); 
  95.   (* location)++; 
  96. }
  97.  
  98. void emit2(unsigned long val)
  99. {
  100.   emitted += emitf("%.4lX", val); 
  101.   (* location)+= 2; 
  102. }
  103.  
  104. void * current_label(void)    /* check that there is a valid label on this line */
  105. {
  106.   if (last_label == NULL)
  107.   {
  108.     text_message(TEXT_ERROR_ECHO, "Missing label on directive\n"); 
  109.     return & dummy_label; 
  110.   }
  111.   else
  112.     return last_label; 
  113. }
  114.  
  115. void init(char * outputfilename)
  116. {
  117.   if (* outputfilename == '-')
  118.     objfile = stdout; 
  119.   else if ((objfile = fopen(outputfilename, "w"))== NULL)
  120.     text_message(TEXT_FATAL, "Unable to open object file"); 
  121. }
  122.  
  123. int quit(char * outputfilename)
  124. {
  125.   fclose(objfile); 
  126.   
  127.   text_message(TEXT_INFO, "Transfer address %.8lX\n", transfer); 
  128.   
  129.   if (execute_sim && * outputfilename != '-')
  130.   {
  131.     #define COMMAND "mvmsim -t -v "
  132.     char * command =(char *) mem_calloc(1, strlen(outputfilename)+ strlen(COMMAND)+ 1); 
  133.     
  134.     command = strcat(command, COMMAND); 
  135.     command = strcat(command, outputfilename); 
  136.     
  137.     text_message(TEXT_INFO, "Calling simulator: %s \n", command); 
  138.     
  139.     if (system(command)!= 0)
  140.       text_message(TEXT_FATAL, "Not enough memory or simulator not found\n"); 
  141.   }
  142.   
  143.   return 0; 
  144. }
  145.  
  146.