home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / src / !runtime / debugger.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-18  |  4.4 KB  |  212 lines  |  [TEXT/R*ch]

  1. #ifdef DEBUG
  2.  
  3. #include <stdio.h>
  4. #include "debugger.h"
  5. #include "instruct.h"
  6. #include "memory.h"
  7. #include "mlvalues.h"
  8. #include "opnames.h"
  9. #include "stacks.h"
  10. #include "unalignd.h"
  11.  
  12. code_t log_buffer[LOG_BUFFER_SIZE];
  13. code_t * log_ptr;
  14. int trace_flag;
  15.  
  16. /* Displaying a heap object */
  17.  
  18. long max_print = 100;
  19. long max_print_depth = 10;
  20.  
  21. long print_cnt;
  22.  
  23. static void print_val(v, d)
  24.      value v;
  25.      long d;
  26. {
  27.   long n;
  28.   value * p;
  29.  
  30.   if (d <= 0) {
  31.     printf(".");
  32.     return;
  33.   }
  34.   print_cnt--;
  35.   if (print_cnt <= 0) {
  36.     if (print_cnt == 0) printf("...");
  37.     return;
  38.   }
  39.   if (Is_long(v))
  40.     printf("%ld", Long_val(v));
  41.   else if (!Is_in_heap (v) && !Is_young (v))
  42.     printf("0x%lx", v);
  43.   else switch(Tag_val(v)) {
  44.     case String_tag:
  45.       printf("\"%s\"", String_val(v));
  46.       break;
  47.     case Double_tag:
  48.       printf("%g", Double_val(v));
  49.       break;
  50.     case Abstract_tag:
  51.       printf("<abstract>");
  52.       break;
  53.     case Final_tag:
  54.       printf("<finalized>");
  55.       break;
  56.     default:
  57.       n = Tag_val(v);
  58.       if (n < 26){
  59.     printf ("%c", n + 'A');
  60.       }else{
  61.         printf("tag%ld", n);
  62.       }
  63.       n = Wosize_val(v);
  64.       if (n > 0) {
  65.         printf("(");
  66.         p = &Field(v, 0);
  67.         while (n > 1) {
  68.           print_val(*p, d-1);
  69.           printf(", ");
  70.           p++;
  71.           n--;
  72.         }
  73.         print_val(*p, d-1);
  74.         printf(")");
  75.       }
  76.       break;
  77.   }
  78. }
  79.  
  80. void print_value(v)
  81.     value v;
  82. {
  83.   print_cnt = max_print;
  84.   print_val(v, max_print_depth);
  85.   printf("\n");
  86. }
  87.  
  88. extern code_t start_code;
  89.  
  90. void print_pc(pc)
  91.      code_t pc;
  92. {
  93.   printf("%6d  ", pc - start_code);
  94. }
  95.  
  96. /* Disassembling one instruction */
  97.  
  98. code_t disasm_instr(pc)
  99.     code_t pc;
  100. {
  101.   int i;
  102.  
  103.   print_pc(pc);
  104.   i = *pc++;
  105.   if (i < 0 || i >= sizeof(names_of_instructions) / sizeof(char *)) {
  106.     printf("???\n");
  107.     return pc;
  108.   }
  109.   printf("%s ", names_of_instructions[i]);
  110.   switch(i) {
  111. /* instructions with a 1-byte immediate operand */
  112.     case ACCESS: case DUMMY: case UPDATE: case ENDLET: case CONSTBYTE:
  113.     case ATOM: case GETFIELD: case SETFIELD:
  114.     case MAKEBLOCK1: case MAKEBLOCK2: case MAKEBLOCK3: case MAKEBLOCK4: 
  115.     case C_CALL1: case C_CALL2: case C_CALL3: case C_CALL4: case C_CALL5:
  116.       printf("%d", *pc++);
  117.       break;
  118. /* instructions with an unsigned 2-byte immediate operand */
  119.     case GETGLOBAL: case SETGLOBAL:
  120.     case PUSH_GETGLOBAL_APPLY: case PUSH_GETGLOBAL_APPTERM:
  121.       printf("%d", u16(pc)); pc += 2; break;
  122. /* instruction with a signed 2-byte immediate operand */
  123.     case CONSTSHORT: 
  124.       printf("%d", s16(pc)); pc += 2; break;
  125. /* instructions with a 4-byte immediate operand */
  126.     case MAKEBLOCK:
  127.       printf("%ld", s32(pc)); pc += 4; break;
  128. /* instructions with a displacement */
  129.     case CUR: case LETREC1: case PUSHTRAP:
  130.     case BRANCH: case BRANCHIF: case BRANCHIFNOT: case POPBRANCHIFNOT:
  131.     case BRANCHIFEQ: case BRANCHIFNEQ: case BRANCHIFLT:
  132.     case BRANCHIFGT: case BRANCHIFLE: case BRANCHIFGE:
  133.     depl:
  134.       { int orig = pc - start_code;
  135.     printf("%d", orig + s16(pc));
  136.     pc += 2;
  137.         break;
  138.       }
  139. /* instruction with 2 displacements */
  140.     case BRANCHINTERVAL:
  141.       printf("%d, ", pc - start_code + s16(pc));
  142.       pc += 2;
  143.       printf("%d", pc - start_code + s16(pc));
  144.       pc += 2;
  145.       break;
  146. /* instruction with tag and displacement */
  147.     case BRANCHIFNEQTAG:
  148.       printf("tag %d, ", (unsigned) *pc++);
  149.       goto depl;
  150. /* miscellaneous */
  151.     case FLOATOP:
  152.       printf("%s", names_of_float_instructions[*pc++]);
  153.       break;
  154.     case SWITCH:
  155.       { int n;
  156.         code_t orig;
  157.     for (n = *pc++, orig = pc; n > 0; n--) {
  158.       printf("%d, ", orig + s16(pc) - start_code);
  159.           pc += 2;
  160.     }
  161.       }
  162.       break;
  163.     }
  164.   printf("\n");
  165.   return pc;
  166. }
  167.  
  168. void disasm(pc)
  169.      code_t pc;
  170. {
  171.   int i;
  172.  
  173.   for (i = 0; i < 20; i++)
  174.     pc = disasm_instr(pc);
  175. }
  176.  
  177. void post_mortem(n)
  178.     int n;
  179. {
  180.   code_t * p;
  181.  
  182.   if (n > LOG_BUFFER_SIZE) n = LOG_BUFFER_SIZE;
  183.   for (p = log_buffer +
  184.              (unsigned) (log_ptr - log_buffer - n) % LOG_BUFFER_SIZE;
  185.        n > 0;
  186.        n--) {
  187.     disasm_instr(*p);
  188.     p++;
  189.     if (p >= log_buffer + LOG_BUFFER_SIZE) p = log_buffer;
  190.   }
  191. }
  192.  
  193. void failed_assert (expr, file, line)
  194.      char *expr, *file;
  195.      int line;
  196. {
  197.   fprintf (stderr, "Assertion failed: %s; file %s; line %d\n",
  198.        expr, file, line);
  199.   exit (100);
  200. }
  201.  
  202. static unsigned long seed = 0x12345;
  203.  
  204. unsigned long not_random ()
  205. {
  206.   seed = seed * 65537 + 12345;
  207.   return seed;
  208. }
  209.  
  210.  
  211. #endif /* DEBUG */
  212.