示例#1
0
inline void readTailItemAndInsert(MemoryReader& reader, MemoryWriter& writer, CFParser::DerivationQueue& queue, int offset)
{
   int nonterminal = reader.getDWord();
   int next = reader.getDWord();

   while (nonterminal == 0) {
      offset = writeDerivationItem(writer, 0, 0, offset);
      if (next != -1) {
         reader.seek(next);
         nonterminal = reader.getDWord();
         next = reader.getDWord();
      }
      else break;
   }

   if (next == -1) {
      queue.insert(CFParser::DerivationItem(-1, offset, 0));
   }
   else queue.insert(CFParser::DerivationItem(nonterminal, offset, next));
}
示例#2
0
void Instance :: configurate(MemoryReader& reader, int terminator)
{
   size_t pos = reader.Position();

   size_t command = reader.getDWord();
   while (command != terminator) {
      ident_t arg = NULL;
      size_t param = reader.getDWord();
      if (test(command, LITERAL_ARG_MASK)) {
         arg = (ident_t)reader.Address();

         reader.seek(reader.Position() + param);  // goes to the next record
      }

      switch (command) {
         case USE_VM_MESSAGE_ID:
            setPackagePath(arg);
            break;
         case MAP_VM_MESSAGE_ID:
            addForward(arg);
            break;
         case LOAD_VM_MESSAGE_ID:
            if(!loadTemplate(arg))
               throw EAbortException();

            break;
         case START_VM_MESSAGE_ID:
            if(!restart(_debugMode))
               throw EAbortException();

            break;
         default:
            reader.seek(pos);
            return;
      }

      pos = reader.Position();
      command = reader.getDWord();
   }
   reader.seek(pos);
}
示例#3
0
void Instance :: translate(MemoryReader& reader, ImageReferenceHelper& helper, MemoryDump& dump, int terminator)
{
   MemoryWriter ecodes(&dump);

   ecodes.writeDWord(0);            // write size place holder
   int procPtr = ecodes.Position();

   // open 0
   ecodes.writeByte(bcOpen);
   ecodes.writeDWord(0);

   // resolve tape
   size_t command = reader.getDWord();
   void*  extra_param;
   while (command != terminator) {
      ident_t arg = NULL;
      size_t param = reader.getDWord();
      if (test(command, LITERAL_ARG_MASK)) {
         arg = (ident_t)reader.Address();

         reader.seek(reader.Position() + param);  // goes to the next record
      }

      // in debug mode place a breakpoint excluding prefix command
      if (_debugMode)
         ecodes.writeByte(bcBreakpoint);

      switch(command) {
         case ARG_TAPE_MESSAGE_ID:
            extra_param = loadSymbol(arg, mskVMTRef);
            break;
         case CALL_TAPE_MESSAGE_ID: 
            //callr
            //pusha
            ecodes.writeByte(bcCallR);
            helper.writeTape(ecodes, loadSymbol(arg, mskSymbolRef), mskCodeRef);
            ecodes.writeByte(bcPushA);
            break;
         case PUSH_VAR_MESSAGE_ID:
            // pushfi param
            ecodes.writeByte(bcPushFI);
            ecodes.writeDWord(param);
            break;
         case ASSIGN_VAR_MESSAGE_ID:
            // popa
            // asavefi param
            ecodes.writeByte(bcPopA);
            ecodes.writeByte(bcASaveFI);
            ecodes.writeDWord(param);
            break;
         case PUSH_TAPE_MESSAGE_ID:
            //pushr r
            ecodes.writeByte(bcPushR);
            helper.writeTape(ecodes, loadSymbol(arg, mskConstantRef), mskRDataRef);
            //level++;

            break;
         case PUSHS_TAPE_MESSAGE_ID:
            //pushr constant
            ecodes.writeByte(bcPushR);
            helper.writeTape(ecodes, loadSymbol(arg, mskLiteralRef), mskRDataRef);
            break;
         case PUSHN_TAPE_MESSAGE_ID:
            //pushr r
            ecodes.writeByte(bcPushR);
            helper.writeTape(ecodes, loadSymbol(arg, mskInt32Ref), mskRDataRef);
            break;
         case PUSHR_TAPE_MESSAGE_ID:
            //pushr r
            ecodes.writeByte(bcPushR);
            helper.writeTape(ecodes, loadSymbol(arg, mskRealRef), mskRDataRef);
            break;
         case PUSHL_TAPE_MESSAGE_ID:
            //pushr r
            ecodes.writeByte(bcPushR);
            helper.writeTape(ecodes, loadSymbol(arg, mskInt64Ref), mskRDataRef);
            break;
         case PUSHM_TAPE_MESSAGE_ID:
            // pushr r
            ecodes.writeByte(bcPushR);
            helper.writeTape(ecodes, loadSymbol(arg, mskMessage), mskRDataRef);
            break;
         case PUSHG_TAPE_MESSAGE_ID:
            // pushr r
            ecodes.writeByte(bcPushR);
            helper.writeTape(ecodes, loadSymbol(arg, mskSignature), mskRDataRef);
            break;
         case POP_TAPE_MESSAGE_ID:
            //popi param
            ecodes.writeByte(bcPopI);
            ecodes.writeDWord(param);
            break;
         case REVERSE_TAPE_MESSAGE_ID:
            if (param == 2) {
               // popa
               // aswapsi 0
               // pusha
               ecodes.writeByte(bcPopA);
               ecodes.writeByte(bcASwapSI);
               ecodes.writeDWord(0);
               ecodes.writeByte(bcPushA);
            }
            else {
               int length = param >> 1;
               param--;

               // popa
               // aswapsi 0
               // pusha
               ecodes.writeByte(bcPopA);
               ecodes.writeByte(bcASwapSI);
               ecodes.writeDWord(param - 1);
               ecodes.writeByte(bcPushA);

               for (int i = 1 ; i < length ; i++) {
                  // aloadsi i
                  // aswapsi n - 1 - i
                  ecodes.writeByte(bcALoadSI);
                  ecodes.writeDWord(i);
                  ecodes.writeByte(bcASwapSI);
                  ecodes.writeDWord(param - i);
               }
            }
            break;
         case SEND_TAPE_MESSAGE_ID:
            //copym message
            //aloadsi 0
            //acallvi 0
            //pusha

            ecodes.writeByte(bcCopyM);
            ecodes.writeDWord(_linker->parseMessage(arg));
            ecodes.writeByte(bcALoadSI);
            ecodes.writeDWord(0);
            ecodes.writeByte(bcACallVI);
            ecodes.writeDWord(0);
            ecodes.writeByte(bcPushA);

            break;
         case NEW_TAPE_MESSAGE_ID:
         {
            int level = param;

            // new n, vmt
            ecodes.writeByte(bcNew);
            helper.writeTape(ecodes, extra_param, mskVMTRef);
            ecodes.writeDWord(param);

            // ; assign content
            // bcopya

            // ; repeat param-time
            // popa
            // axsavebi i

            // pushb
            ecodes.writeByte(bcBCopyA);
            while (level > 0) {
               ecodes.writeByte(bcPopA);
               ecodes.writeByte(bcAXSaveBI);
               level--;
               ecodes.writeDWord(level);
            }

            ecodes.writeByte(bcPushB);
            break;
         }
      }
      command = reader.getDWord();
   }
   // EOP breakpoint
   if (_debugMode)
      ecodes.writeByte(bcBreakpoint);

   // popa
   // close
   // quit
   ecodes.writeByte(bcPopA);
   ecodes.writeByte(bcClose);
   ecodes.writeByte(bcQuit);

   dump[procPtr - 4] = ecodes.Position() - procPtr;
}
示例#4
0
bool printCommand(_Module* module, MemoryReader& codeReader, int indent, List<int>& labels)
{
   // read bytecode + arguments
   int position = codeReader.Position();
   unsigned char code = codeReader.getByte();

   // ignore a breakpoint if required
   if (code == bcBreakpoint && _ignoreBreakpoints)
      return false;

   char opcode[0x30];
   ByteCodeCompiler::decode((ByteCode)code, opcode);

   IdentifierString command;
   while (indent > 0) {
      command.append(" ");

      indent--;
   }
   if (code < 0x10)
      command.append('0');

   command.appendHex((int)code);
   command.append(' ');

   int argument = 0;
   int argument2 = 0;
   if (code > MAX_DOUBLE_ECODE) {
      argument = codeReader.getDWord();
      argument2 = codeReader.getDWord();

      appendHex32(command, argument);
      command.append(' ');

      appendHex32(command, argument2);
      command.append(' ');
   }
   else if (code > MAX_SINGLE_ECODE) {
      argument = codeReader.getDWord();

      appendHex32(command, argument);
      command.append(' ');
   }

   size_t tabbing = code == bcNop ? 24 : 31;
   while (getlength(command) < tabbing) {
      command.append(' ');
   }

   switch(code)
   {
      case bcPushF:
      case bcSCopyF:
      case bcACopyF:
      case bcBCopyF:
         command.append(opcode);
         command.append(" fp:");
         command.appendInt(argument);
         break;
      case bcACopyS:
         command.append(opcode);
         command.append(" sp:");
         command.appendInt(argument);
         break;
      case bcJump:
      case bcHook:
      case bcIf:
      case bcIfB:
      case bcElse:
      case bcIfHeap:
      case bcNotLess:
//      case bcAddress:
         command.append(opcode);
         command.append(' ');
         printLabel(command, position + argument + 5, labels);
         break;
      case bcElseM:
      case bcIfM:
         command.append(opcode);
         command.append(' ');
         printMessage(command, module, argument);
         command.append(' ');
         printLabel(command, position + argument2 + 9, labels);
         break;
      case bcElseR:
      case bcIfR:
         command.append(opcode);
         command.append(' ');
         printReference(command, module, argument);
         command.append(' ');
         printLabel(command, position + argument2 + 9, labels);
         break;
      case bcIfN:
      case bcElseN:
      case bcLessN:
      case bcNotLessN:
      case bcGreaterN:
      case bcNotGreaterN:
         command.append(opcode);
         command.append(' ');
         command.appendHex(argument);
         command.append(' ');
         printLabel(command, position + argument2 + 9, labels);
         break;
      case bcNop:
         printLabel(command, position + argument, labels);
         command.append(':');
         command.append(' ');
         command.append(opcode);
         break;
      case bcPushR:
      case bcALoadR:
      case bcCallExtR:
      case bcCallR:
      case bcASaveR:
      case bcACopyR:
      case bcBCopyR:
         command.append(opcode);
         command.append(' ');
         printReference(command, module, argument);
         break;
      case bcReserve:
      case bcRestore:
      case bcPushN:
      case bcPopI:
      case bcOpen:
      case bcQuitN:
      case bcDCopy:
      case bcECopy:
      case bcAndN:
      case bcOrN:
      case bcInit:
      case bcNLoadI:
      case bcNSaveI:
      case bcMulN:
      case bcAddN:
         command.append(opcode);
         command.append(' ');
         command.appendHex(argument);
         break;
      case bcPushSI:
      case bcALoadSI:
      case bcASaveSI:
      case bcBLoadSI:
      case bcBSaveSI:
         command.append(opcode);
         command.append(" sp[");
         command.appendInt(argument);
         command.append(']');
         break;
      case bcBLoadFI:
      case bcPushFI:
      case bcALoadFI:
      case bcASaveFI:
      case bcDLoadFI:
         command.append(opcode);
         command.append(" fp[");
         command.appendInt(argument);
         command.append(']');
         break;
      case bcAJumpVI:
      case bcACallVI:
         command.append(opcode);
         command.append(" acc::vmt[");
         command.appendInt(argument);
         command.append(']');
         break;
      case bcPushAI:
      case bcALoadAI:
         command.append(opcode);
         command.append(" acc[");
         command.appendInt(argument);
         command.append(']');
         break;
      case bcASaveBI:
      case bcAXSaveBI:
      case bcALoadBI:
         command.append(opcode);
         command.append(" base[");
         command.appendInt(argument);
         command.append(']');
         break;
      case bcNew:
         command.append(opcode);
         command.append(' ');
         printReference(command, module, argument);
         command.append(", ");
         command.appendInt(argument2);
         break;
      case bcXCallRM:
      case bcXJumpRM:
      case bcXIndexRM:
      case bcXMTRedirect:
         command.append(opcode);
         command.append(' ');
         printReference(command, module, argument);
         command.append(", ");
         printMessage(command, module, argument2);
         break;
      case bcCopyM:
         command.append(opcode);
         command.append(' ');
         printMessage(command, module, argument);
         break;
      case bcSetVerb:
         command.append(opcode);
         command.append(' ');
         printMessage(command, module, encodeAction(argument));
         break;
      case bcSelectR:
      case bcXSelectR:
         command.append(opcode);
         command.append(' ');
         printReference(command, module, argument);
         command.append(", ");
         printReference(command, module, argument2);
         break;
      case bcNewN:
         command.append(opcode);
         command.append(' ');
         printReference(command, module, argument);
         command.append(", ");
         command.appendInt(argument2);
         break;
      case bcSaveFI:
      case bcAddFI:
      case bcSubFI:
         command.append(opcode);
         command.append(" fp[");
         command.appendInt(argument);
         command.append("], ");
         command.appendInt(argument2);
         break;
      default:
         command.append(opcode);
         break;
   }

   print(command);
   return true;
}
示例#5
0
void printCommand(_Module* module, MemoryReader& codeReader, int indent, List<int>& labels)
{
   // read bytecode + arguments
   int position = codeReader.Position();
   unsigned char code = codeReader.getByte();

   wchar16_t opcode[0x30];
   ByteCodeCompiler::decode((ByteCode)code, opcode);

   IdentifierString command;
   while (indent > 0) {
      command.append(_T(" "));

      indent--;
   }
   if (code < 0x10)
      command.append(_T('0'));

   command.appendHex((int)code);
   command.append(_T(' '));

   int argument = 0;
   int argument2 = 0;
   if (code >= 0xE0) {
      argument = codeReader.getDWord();
      argument2 = codeReader.getDWord();

      appendHex32(command, argument);
      command.append(_T(' '));

      appendHex32(command, argument2);
      command.append(_T(' '));
   }
   else if (code >= 0x20) {
      argument = codeReader.getDWord();

      appendHex32(command, argument);
      command.append(_T(' '));
   }

   int tabbing = code == bcNop ? 24 : 31;
   while (getlength(command) < tabbing) {
      command.append(_T(' '));
   }

   switch(code)
   {
      //case bcPushFPI:
      case bcXPushF:
      case bcSCopyF:
      case bcAXCopyF:
      //case bcAccCopyFPI:
         command.append(opcode);
         command.append(_T(" fp:"));
         command.appendHex(argument);
         break;
      case bcACopyS:
         command.append(opcode);
         command.append(_T(" sp:"));
         command.appendHex(argument);
         break;
      case bcAElse:
      case bcAThen:
      //case bcMccElseAcc:
      //case bcMccThenAcc:
      case bcJump:
      //case bcElseLocal:
         command.append(opcode);
         command.append(_T(' '));
         printLabel(command, position + argument + 5, labels);
         break;
      case bcMElse:
      case bcMThen:
         command.append(opcode);
         command.append(_T(' '));
         printMessage(command, module, argument);
         command.append(_T(' '));
         printLabel(command, position + argument2 + 9, labels);
         break;
      //case bcElseR:
      //case bcThenR:
      //   command.append(opcode);
      //   command.append(_T(' '));
      //   printReference(command, module, argument);
      //   command.append(_T(' '));
      //   printLabel(command, position + argument2 + 9, labels);
      //   break;
      case bcTestFlag:
      case bcElseFlag:
      ////case bcElseN:
      ////case bcThenN:
         command.append(opcode);
         command.append(_T(' '));
         command.appendHex(argument);
         command.append(_T(' '));
         printLabel(command, position + argument2 + 9, labels);
         break;
      //case bcMccElseAccI:
      //case bcMccThenAccI:
      //   command.append(opcode);
      //   command.append(_T(' acc['));
      //   command.appendHex(argument);
      //   command.append(_T('] '));
      //   printLabel(command, position + argument2 + 9, labels);
      //   break;
      case bcAElseR:
      case bcAThenR:
         command.append(opcode);
         command.append(_T(' '));
         printReference(command, module, argument);
         command.append(_T(' '));
         printLabel(command, position + argument2 + 9, labels);
         break;
         break;
      case bcAElseSI:
      case bcAThenSI:
      ////case bcMccElseSI:
      ////case bcMccThenSI:
      ////case bcMccVerbElseSI:
      ////case bcMccVerbThenSI:
         command.append(opcode);
         command.append(_T(" sp["));
         command.appendInt(argument);
         command.append(_T("] "));
         printLabel(command, position + argument2 + 9, labels);
         break;
      case bcNop:
         printLabel(command, position + argument, labels);
         command.append(_T(':'));
         command.append(_T(' '));
         command.append(opcode);
         break;
      case bcPushR:
      case bcALoadR:
      case bcCallExtR:
      case bcEvalR:
      //case bcCallR:
      ////case bcSendVMTR:
      case bcASaveR:
      case bcACopyR:
      case bcNBox:
      case bcBox:
      ////case bcAccTryR:
      ////case bcAccMergeR:
      ////case bcJumpR:
         command.append(opcode);
         command.append(_T(' '));
         printReference(command, module, argument);
         break;
      case bcReserve:
      case bcRestore:
      //case bcPushN:
      case bcPopI:
      case bcOpen:
      case bcQuitN:
      case bcDCopy:
      //case bcAccCreate:
      ////case bcAccTestFlagN:
      ////case bcAccCopyN:
      //case bcAccAddN:
      //case bcGetLen:
      ////case bcAccTryN:
      ////case bcSelfShiftI:
      ////case bcAccShiftI:
      ////case bcTryLock:
      ////case bcFreeLock:
      ////case bcSPTryLock:
      ////case bcAccFreeLock:
      //case bcJumpAccN:
         command.append(opcode);
         command.append(_T(' '));
         command.appendHex(argument);
         break;
      ////case bcAccInc:
      //case bcPushI:
      //   command.append(opcode);
      //   command.append(_T(' '));
      //   command.appendHex(argument);
      //   break;
      case bcPushSI:
      case bcALoadSI:
      case bcASaveSI:
      //case bcSwapSI:
      //case bcPopSI:
      case bcMLoadSI:
      //case bcAccSwapSI:
         command.append(opcode);
         command.append(_T(" sp["));
         command.appendInt(argument);
         command.append(_T(']'));
         break;
      //case bcAccGetSI:
      //   command.append(opcode);
      //   command.append(_T(" acc[sp["));
      //   command.appendInt(argument);
      //   command.append(_T("]]"));
      //   break;
      //case bcAccGetFI:
      //   command.append(opcode);
      //   command.append(_T(" acc[fp["));
      //   command.appendInt(argument);
      //   command.append(_T("]]"));
      //   break;
      case bcPushFI:
      case bcALoadFI:
      //case bcPopFI:
      //case bcIncFI:
      case bcASaveFI:
      case bcMLoadFI:
      case bcMSaveParams:
      //case bcXAccSaveFI:
      case bcDLoadFI:
      case bcDSaveFI:
         command.append(opcode);
         command.append(_T(" fp["));
         command.appendInt(argument);
         command.append(_T(']'));
         break;
      case bcACallVI:
         command.append(opcode);
         command.append(_T(" acc::vmt["));
         command.appendInt(argument);
         command.append(_T(']'));
         break;
      case bcPushAI:
      ////case bcPopAccI:
      case bcXPopAI:
      ////case bcPop2AccI:
      //case bcAccLoadAccI:
      case bcMLoadAI:
      //case bcMccAddAccI:
      case bcDSaveAI:
         command.append(opcode);
         command.append(_T(" acc["));
         command.appendInt(argument);
         command.append(_T(']'));
         break;
      //case bcPushSelfI:
      //case bcPopSelfI:
      case bcASaveBI:
      ////case bcAccLoadSelfI:
         command.append(opcode);
         command.append(_T(" self["));
         command.appendInt(argument);
         command.append(_T(']'));
         break;
      ////case bcIAccCopyN:
      ////   command.append(opcode);
      ////   command.append(_T(" acc["));
      ////   command.appendInt(argument);
      ////   command.append(_T("], "));
      ////   command.appendHex(argument2);
      ////   break;
      //case bcIAccCopyR:
      //   command.append(opcode);
      //   command.append(_T(" acc["));
      //   command.appendInt(argument);
      //   command.append(_T("], "));
      //   printReference(command, module, argument2);
      //   break;
      //case bcIAccFillR:
      //   command.append(opcode);
      //   command.append(_T(' '));
      //   command.appendInt(argument);
      //   command.append(_T(", "));
      //   printReference(command, module, argument2);
      //   break;
      //case bcCreate:
      //case bcCreateN:
      //case bcAccCreateN:
      //case bcAccBoxN:
      //   command.append(opcode);
      //   command.append(_T(' '));
      //   command.appendInt(argument);
      //   command.append(_T(", "));
      //   printReference(command, module, argument2);
      //   break;
      case bcXCallRM:
         command.append(opcode);
         command.append(_T(' '));
         printReference(command, module, argument);
         command.append(_T(", "));
         printMessage(command, module, argument2);
         break;
      //case bcRCallN:
      //   command.append(opcode);
      //   command.append(_T(' '));
      //   printReference(command, module, argument);
      //   command.append(_T(", "));
      //   command.appendInt(argument2);
      //   break;
      ////case bcAccCopyM:
      case bcMCopy:
      //case bcXMccCopyM:
      //case bcMccAddM:
         command.append(opcode);
         command.append(_T(' '));
         printMessage(command, module, argument);
         break;
      case bcSCallVI:
         command.append(opcode);
         command.append(_T(" sp["));
         command.appendInt(argument);
         command.append(_T("]::vmt["));
         command.appendInt(argument2);
         command.append(_T("]"));
         break;
      case bcNFunc:
      case bcLFunc:
      case bcRFunc:
      case bcWSFunc:
      case bcFunc:
         command.append(opcode);
         ByteCodeCompiler::decodeFunction((FunctionCode)argument, opcode);
         command.append(opcode);
         break;
      default:
         command.append(opcode);
         break;
   }

   print(command);
}