home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Moscow ML 1.31 / source code / mosml / src / runtime / fix_code.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-03  |  2.0 KB  |  74 lines  |  [TEXT/R*ch]

  1. /* Switch immediate operands from little endian to big endian. */
  2.  
  3. #include "config.h"
  4. #include "misc.h"
  5. #include "mlvalues.h"
  6. #include "instruct.h"
  7. #include "reverse.h"
  8.  
  9. /* We don't need this code if 1- the machine is little-endian, or 2- the
  10.    machine has alignment constraints, in which case the interpreter does word
  11.    accesses byte per byte, in little-endian mode, regardless of the
  12.    native endianness.
  13. */
  14.  
  15. #if defined(BIG_ENDIAN) && !defined(ALIGNMENT)
  16.  
  17. void fixup_endianness(p, len)
  18.      register code_t p;
  19.      asize_t len;
  20. {
  21.   register code_t q;
  22.   int n;
  23.  
  24.   q = p + len;
  25.   while(p < q) {
  26.     switch(*p++) {
  27.       /* Instructions with one one-byte + one two-byte argument */
  28.     case BRANCHIFNEQTAG: case C_CALLN:
  29.       p++; /* fall through */
  30.       /* Instructions with a two-byte immediate argument */
  31.     case GETGLOBAL: case SETGLOBAL: 
  32.     case PUSH_GETGLOBAL_APPLY: case PUSH_GETGLOBAL_APPTERM:
  33.     case CONSTSHORT:
  34.     case CUR: case LETREC1: case PUSHTRAP:
  35.     case BRANCH: case BRANCHIF: case BRANCHIFNOT: case POPBRANCHIFNOT:
  36.     case BRANCHIFEQ: case BRANCHIFNEQ: case BRANCHIFLT:
  37.     case BRANCHIFGT: case BRANCHIFLE: case BRANCHIFGE:
  38.     case C_CALL1: case C_CALL2: case C_CALL3: case C_CALL4:
  39.     case C_CALL5:
  40.       Reverse_short(p);
  41.       p += 2;
  42.       break;
  43.       /* Instructions with a four-byte immediate argument */
  44.     case MAKEBLOCK:
  45.       Reverse_word(p);
  46.       p += 4;
  47.       break;
  48.       /* Instructions with two two-byte immediate arguments */
  49.     case BRANCHINTERVAL:
  50.       Reverse_short(p);
  51.       Reverse_short(p+2);
  52.       p += 4;
  53.       break;
  54.       /* Special case for switch */
  55.     case SWITCH:
  56.       n = *p++;
  57.       while (n > 0) {
  58.         Reverse_short(p);
  59.         p += 2;
  60.         n--;
  61.       }
  62.       break;
  63.       /* Instructions with a one-byte immediate argument */
  64.     case CONSTBYTE: case MAKEBLOCK1: case MAKEBLOCK2: case MAKEBLOCK3:
  65.     case MAKEBLOCK4: case ACCESS: case ATOM: case GETFIELD: case SETFIELD:
  66.     case DUMMY: case ENDLET: case FLOATOP:
  67.       p++;
  68.       break;
  69.     }
  70.   }
  71. }
  72.  
  73. #endif
  74.