home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / src / !runtime / fix_code.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-18  |  2.6 KB  |  84 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(MOSML_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: case CLOSURE: case CLOSREC:
  29.     case PUSH_GETGLOBAL_APPTERM1: case PUSH_GETGLOBAL_APPTERM2: 
  30.     case PUSH_GETGLOBAL_APPTERM3: case PUSH_GETGLOBAL_APPTERM4: 
  31.       p++; /* fall through */
  32.       /* Instructions with a two-byte immediate argument */
  33.     case GETGLOBAL: case SETGLOBAL: case CONSTSHORT:
  34.     case PUSH_GETGLOBAL: 
  35.     case PUSH_GETGLOBAL_APPLY1: case PUSH_GETGLOBAL_APPLY2:
  36.     case PUSH_GETGLOBAL_APPLY3: case PUSH_GETGLOBAL_APPLY4:
  37.     case PUSH_RETADDR: case PUSHTRAP:
  38.     case BRANCH: case BRANCHIF: case BRANCHIFNOT: case POPBRANCHIFNOT:
  39.     case BRANCHIFEQ: case BRANCHIFNEQ: case BRANCHIFLT:
  40.     case BRANCHIFGT: case BRANCHIFLE: case BRANCHIFGE:
  41.     case C_CALL1: case C_CALL2: case C_CALL3: case C_CALL4:
  42.     case C_CALL5: case GETFIELD: case SETFIELD:
  43.       Reverse_short(p);
  44.       p += 2;
  45.       break;
  46.       /* Instructions with a four-byte immediate argument */
  47.     case MAKEBLOCK: case CONSTINT:  case PUSHCONSTINT: 
  48.       Reverse_word(p);
  49.       p += 4;
  50.       break;
  51.       /* Instructions with two two-byte immediate arguments */
  52.     case BRANCHINTERVAL: 
  53.       Reverse_short(p);
  54.       Reverse_short(p+2);
  55.       p += 4;
  56.       break;
  57.       /* Special case for switch */
  58.     case SWITCH:
  59.       n = *p++;
  60.       while (n > 0) {
  61.         Reverse_short(p);
  62.         p += 2;
  63.         n--;
  64.       }
  65.       break;
  66.       /* Instructions with two one-byte immediate arguments */
  67.     case APPTERM:
  68.       p++; /* fall into... */
  69.       /* Instructions with a one-byte immediate argument */
  70.     case CONSTBYTE: case ATOM: case PUSHATOM: case ASSIGN:
  71.     case MAKEBLOCK1: case MAKEBLOCK2: case MAKEBLOCK3: case MAKEBLOCK4:
  72.     case DUMMY: case POP: case GRAB: case RETURN: case ACCESS:
  73.     case APPTERM1: case APPTERM2: case APPTERM3: case APPTERM4:
  74.     case PUSH_ENV1_APPTERM1: case PUSH_ENV1_APPTERM2: 
  75.     case PUSH_ENV1_APPTERM3: case PUSH_ENV1_APPTERM4: 
  76.     case ENVACC: case PUSHACC: case PUSHENVACC: case APPLY: 
  77.       p++;
  78.       break;
  79.     }
  80.   }
  81. }
  82.  
  83. #endif
  84.