Exemplo n.º 1
0
static int install_firmware_ctx (void)
{
	static const char *firmware_path 	= TVM_FIRMWARE_PATH "tvm-posix.tbc";
	static const char *posix_tbc		= "tvm-posix.tbc";
	char *firmware_file			= getenv ("TVM_FIRMWARE_FILE");

	if (firmware_file == NULL) {
		firmware_file = (char *) firmware_path;

		if (file_exists (firmware_file)) {
			/* do nothing */
		} else if (file_exists (posix_tbc)) {
			firmware_file = (char *) posix_tbc;
		}
	}

	if ((fw_bc = load_bytecode (firmware_file)) == NULL) {
		fprintf (stderr, 
			"Failed to load/decode %s\n",
			firmware_file
		);
		return -1;
	}

	if ((firmware = allocate_ectx (fw_bc, "!??", tlp_argv)) == NULL) {
		return -1;
	}

	return 0;
}
Exemplo n.º 2
0
 bool start_main(const char* hybFilename)
 {
     Bytecode* pBytecode = load_bytecode(hybFilename);
     if (pBytecode != NULL) {
         start_bytecode(pBytecode);
         return true;
     }
     return false;
 }
Exemplo n.º 3
0
Arquivo: vm.c Projeto: rjeli/vm
int main(int argc, char *argv[])
{
	printf("vm\n");
	if(argc < 2) {
		printf("usage: %s <vm-file>\n", argv[0]);
	} else {
		FILE *fp;
		fp = fopen(argv[1], "rb");
		if(fp == NULL) {
			printf("can't open file %s\n", argv[1]);
		} else {
			printf("opened file\n");
			struct fn_node fn_root;
			struct vm_env env;
			fn_root = load_bytecode(fp, &env);
			fclose(fp);
			printf("dump:\n\n");
			dump_bytecode(fn_root);
			eval_bc_fn(fn_root, &env, 0);
			free_bytecode(fn_root);
		}
	}
	return 0;
}
Exemplo n.º 4
0
/*-----------------------------------------------------------------------------
*
* ID: m_func 02.11.06 0.0.A.
*
* Summary: The func, method, operator, property, text processing
*
-----------------------------------------------------------------------------*/
plexem STDCALL m_func( plexem curlex, uint flgextern )
{
#ifdef DOUT
   uint     i;
#endif 


   uint     funckey;  //Вид функции
   uint     flgfunc;  //Флаги функции   

   pflabel  curlabel; // Текущий элемент в таблице меток
   pflabel  endlabel; // Конец таблицы меток
   uint     isreturn; //Есть return

   uint     thistype; //Тип переменной this для методов+
   pubyte   name;     //Имя функции
   plexem   lexname;  //Лексема с именем функции
   plexem   lexprev;

   s_desctype desctype;//Описание типа
   s_descid   descvar; //Описание переменной

   uint off_parcount; //Смещение в заголовке функции на кол. параметров
   uint off_blccount; //Смещение в заголовке функции на кол. блоков

   bcflag     bcf;    //Переменная для получение флагов функции
   pbuf       b;
   pfwith     pwith; 
   pvmobj     funcobj;   
   uint       thisid; //Номер переменной для this в текст функции

D( "Func start\n" );

// Инициализация
   desctype.idtype = 0;
   descvar.idtype = 0;
   descvar.flgdesc = 0;
   mem_zero( &fd, sizeof( fd ) );
   thistype = 0;
   funckey = curlex->key;
   hash_init( &fd.nvars, sizeof( uint ) );
   hash_init( &fd.nlabels, sizeof( uint ) );
   for ( b = &fd.bhead; b <= &fd.bvarsas; b++ )
   {
      buf_init( b );
      buf_reserve( b, 0x200 );
      b->step = 0x200;
   }
   fd.bvars.use = sizeof( fvar );
   fd.blabels.use = sizeof( flabel );
//   _compile->pout = &fd.bhead;   
//   fd.blcount = 0;
//   fd.varcount = 0;
//   fd.curcount = 0;
//   fd.lastcurcount = 0;
//   fd.bllevel = 0;
//   fd.blcycle = 0;
   fd.offlcbreak = -1;
   fd.offlccontinue = -1;   
//   fd.functype = 0;

   switch ( funckey )
   {
      case KEY_METHOD:
         flgfunc = GHBC_METHOD;
         break;
      case KEY_OPERATOR:
         flgfunc = GHBC_OPERATOR;
         break;
      case KEY_PROPERTY:
         flgfunc = GHBC_PROPERTY;
         break;
      case KEY_TEXT:
         flgfunc = GHBC_TEXT;
         break;
      default:
         flgfunc = 0;
   }
   curlex = lexem_next( curlex, LEXNEXT_IGNLINE );

// Получаем тип возвращаемого значения функции/метода если он есть
   if ( curlex->type == LEXEM_NAME )
      curlex = desc_idtype( curlex, &desctype );

   if ( desctype.idtype )
   {
      if ( ( funckey == KEY_METHOD || funckey == KEY_PROPERTY ) &&
           curlex->type == LEXEM_OPER &&
           curlex->oper.operid == OpWith )
      {
         //Возврат на лексему влево текущая лексема тип объекта
         desctype.idtype = 0;
         curlex--;
      }
      else
      {
         fd.functype = desctype.idtype;
         fd.funcoftype = desctype.oftype;
      }
   }
   lexprev = curlex;
   curlex = lexem_next( curlex, LEXNEXT_SKIPLINE );
// Получаем тип объекта для метода
   if ( funckey == KEY_METHOD || funckey == KEY_PROPERTY )
   {
      //Получаем тип объекта
	  if ( curlex->type > 32 )
		 msg( MExptype | MSG_LEXERR, lexprev );
	  if ( thistype = bc_type( curlex ) )
      {
         curlex = lexem_next( curlex, 0 );
         if ( curlex->type == LEXEM_OPER &&
              curlex->oper.operid == OpWith )
         {
            curlex = lexem_next( curlex, 0 );
         }
         else
            msg( MExppoint | MSG_LEXERR, curlex );
      }
      else
         msg( MExptype | MSG_LEXERR, curlex );
   }
// Получение имени функции, метода ...   
   if ( funckey == KEY_OPERATOR )
   {
      if ( curlex->type != LEXEM_OPER )
         msg( MExpoper | MSG_LEXERR, curlex );
      name = ( pubyte )&curlex->oper.name;
   }
   else
   {
      if ( curlex->type != LEXEM_NAME )
         msg( MExpname | MSG_LEXERR, curlex );
      name = lexem_getname( curlex );      
   }
   lexname = curlex;
   _vm.pos = curlex->pos;
   
// Получение списка директив
   curlex = lexem_next( curlex, flgextern ? 0 : LEXNEXT_IGNLINE );   
   curlex = bc_flag( curlex, BFLAG_FUNC, &bcf );   
   flgfunc |= GHCOM_NAME | bcf.value;
   _compile->pout = &fd.bhead;   
   out_head( OVM_BYTECODE, flgfunc, name );

   create_varmode( &fd.bhead, &desctype, 0 );//Возвращаемое значение

   off_parcount = fd.bhead.use;
   out_adduint( 0 );//Количество параметров

   if ( funckey == KEY_METHOD || funckey == KEY_PROPERTY )
   {   //Создание параметра this
      mem_zero( &descvar, sizeof( descvar ));
      descvar.idtype = thistype;
      descvar.name = "this";
      descvar.lex = curlex;
      descvar.flgdesc = DESCID_PARFUNC;

      pwith = ( pfwith )buf_appendtype( &fd.bwith, sizeof( fwith )) ;
      pwith->num = var_checkadd( &descvar );
      pwith->oftype = 0;
      pwith->type = thistype;
   }

//Получение списка параметров
   if ( curlex->type == LEXEM_OPER &&
        curlex->oper.operid == OpLbrack )//Открывающая скобка
   {
      curlex = lexem_next( curlex, LEXNEXT_IGNLINE );
      curlex = var_def( curlex, DESCID_PARFUNC );
      if ( curlex->type != LEXEM_OPER ||//Системная лексема
           curlex->oper.operid != OpRbrack )//Закрывающая скобка
         msg( MExpclosebr | MSG_LEXERR, curlex );

      curlex = lexem_next( curlex, flgextern ? 0 : LEXNEXT_IGNLINE );
   }
   else
   {
      if ( funckey == KEY_OPERATOR )
         msg( MExpopenbr | MSG_LEXERR, curlex );
   }

   fd.flgfunc = flgfunc;
   if ( flgfunc & GHBC_RESULT )
   {   //Создание параметра result
      if ( !fd.functype || fd.functype <= TUlong )
         msg( MResulttype | MSG_LEXERR, curlex );
      mem_zero( &descvar, sizeof( descvar ));
      descvar.idtype = desctype.idtype;
      descvar.oftype = desctype.oftype;
      descvar.flgdesc = DESCID_PARFUNC;
      descvar.name = "result";
      descvar.lex = curlex;
      fd.idresult = var_checkadd( &descvar );
      fd.functype = 0;
   }
   if ( fd.varcount )
   {
      *( puint )( fd.bhead.data + off_parcount ) = fd.varcount;//Кол-во параметров
      if ( flgfunc & ( GHBC_ENTRY | GHBC_MAIN ) )
         msg( MMainpar | MSG_LEXERR, curlex );
      fd.curcount = 0;
   }
   off_blccount = fd.bhead.use;
   out_adduint( 0 );//Количество блоков

   if ( funckey == KEY_PROPERTY )
   {      
      if ( ( fd.functype && fd.varcount > 1 ) ||
           (!fd.functype && fd.varcount != 2 ))
      {
         msg( MProppar | MSG_LEXERR, curlex );//Неверное количество параметров в описании свойства
      }
      if ( type_fieldname( thistype, name ) )
      {
         msg( MPropfield | MSG_LEXERR, curlex );//Свойство совпадает с именем поля
      }
   }
   
   funcobj = load_bytecode( &fd.bhead.data, flgextern ? VMLOAD_EXTERN : VMLOAD_FIRST );   
   if ( bcf.value & GHRT_ALIAS )
   {  
      alias_setid( bcf.alias, funcobj->id );     
   }
   if ( !( flgextern ) )
   {      
      if ( _compile->flag & CMPL_DEBUG )
      {  
         _compile->pout = fd.bout = &fd.bsubout;
         out_adduints( 3,  CDatasize, 
                           str_len( _compile->cur->filename ) + 5,
                           str_pos2line( _compile->cur->src, lexname->pos, 0 ) + 1 );
         out_addptr( str_ptr( _compile->cur->filename ), str_len( _compile->cur->filename ) + 1 );                     
         out_adduint( CDbgFunc );
         _compile->pout = fd.bout = &fd.bfuncout; 
      }
      _compile->pout = fd.bout = &fd.bfuncout;
      if ( funckey == KEY_TEXT )
      {   //Создание параметра this для Text функции
         mem_zero( &descvar, sizeof( descvar ));
         descvar.idtype = TUint;
         descvar.name = "this";
         descvar.lex = curlex;
         descvar.flgdesc = DESCID_VAR;///DESCID_PARFUNC;
         thisid = var_checkadd( &descvar );
         
         /*pwith = ( pfwith )buf_appendtype( &fd.bwith, sizeof( fwith )) ;
         pwith->num = var_checkadd( &descvar );
         //print( "ssssssssss %x %x %x %x", fd.bvars.data, fd.bvars.use, pwith->num, sizeof( fvar ) );
         pwith->oftype = 0;
         pwith->type = TStr;*/
         ((pfvar)(fd.bvars.data + fd.bvars.use - sizeof( fvar )))->type = TStr;
         out_adduints( 4, CVarptrload, thisid, CGetText, CSetI );
         /*buf_appenduint( &fd.bblinit, CVarptrload );
         buf_appenduint( &fd.bblinit, thisid );
         buf_appenduint( &fd.bblinit, CGetText );
         buf_appenduint( &fd.bblinit, CSetI );*/
      }
      curlex = f_body( curlex );
      
      *((puint)(fd.bhead.data+off_blccount)) = fd.blcount;

      curlabel = ( pflabel )( fd.blabels.data ) + 1;
      endlabel = ( pflabel )( fd.blabels.data + fd.blabels.use );
      //Контроль неразрешённых меток и проверка выходов из функции
      isreturn = 0;
      while( curlabel < endlabel )
      {
         if ( curlabel->type & LABT_GT )
         {
            if ( ( curlabel->type & LABT_GTUNDEF ) == LABT_GTUNDEF )
               msg( MUnklabel | MSG_LEXNAMEERR, curlabel->lex );
            *( puint )(fd.bfuncout.data + curlabel->offbout ) = 
                     ((( pflabel )(fd.blabels.data + curlabel->link ))->offbout + 
                      fd.bsubout.use )/sizeof(uint);
            if ( !isreturn )//Помечаем метку как отработавшую (на неё был переход)
               (( pflabel )(fd.blabels.data + curlabel->link ))->type |= LABT_LABELWORK;            
         }
         else
         if ( curlabel->type & LABT_RETURN )
         {
            isreturn = 1;//Устанавливаем флаг
         }
         else
         if ( curlabel->type & LABT_LABELWORK )
            isreturn = 0;//Если была отработавшая метка, то сбрасываем флаг
         curlabel++;
      }
      if ( fd.functype )
      {
         if ( !isreturn )
            msg( MMustret | MSG_LEXNAMEERR, lexname );
      }
      else
         if ( !isreturn )
         {
            if ( fd.flgfunc & GHBC_RESULT )
            {
               out_add2uint( CVarload, fd.idresult );
            }
            out_adduint( CReturn );
         }      
      buf_add( &fd.bhead, &fd.bvardef );
      
      if ( fd.bsubout.use )
      {
         if ( fd.offsubgoto )
         {
            //*((( puint)fd.bsubout.data ) + 1) = fd.bsubout.use/sizeof( uint );
            *( puint )( fd.bsubout.data + fd.offsubgoto ) = fd.bsubout.use / sizeof( uint );
         }
         buf_add( &fd.bhead, &fd.bsubout );
      }
      buf_add( &fd.bhead, &fd.bfuncout );
      _compile->pout = &fd.bhead;
      out_finish();
#ifdef DOUT
   //Тестируемый вывод 
   //if ( name[0] == 'c' && name[1] == 'r' ) getch();
   print( "FUNC OUT %x %s:\n", funcobj->id, name );
   for (i = 0; i < fd.bhead.use ; i++ )
   {
      print( "  %x", fd.bhead.data[i] );
   } 
   print( "\n" );
#endif            
      load_bytecode( &fd.bhead.data, VMLOAD_OK );
    //  print( "funcobjid2 =%x\n", funcobj->id );
   }
   //Очистка памяти
   for ( b = &fd.bhead;/*&fd.bblinit;*/ b <= &fd.bvarsas; b++ )
   {
      buf_delete( b );
   }
   hash_delete( &fd.nvars );
   hash_delete( &fd.nlabels );

D( "Func Stop\n" );
   return curlex;
}
Exemplo n.º 5
0
static int install_user_ctx (const char *fn)
{
	const char *const tlp_fmt = "?!!";
	int kyb_p = -1, scr_p = -1, err_p = -1, valid_tlp = 1;
	tbc_t *tbc;
	WORD *argv;
	char *tlp;
	int i, tlp_len;

	if ((us_bc = load_bytecode (fn)) == NULL) {
		fprintf (stderr, 
			"Failed to load/decode %s\n",
			fn
		);
		return -1;
	}

	tbc = us_bc->tbc;

	if (tbc->tlp != NULL) {
		tlp = tbc->tlp->fmt;
	} else {
		tlp = (char *) tlp_fmt;
	}

	tlp_argv[0] = (WORD) &kyb_channel;
	tlp_argv[1] = (WORD) &scr_channel;
	tlp_argv[2] = (WORD) &err_channel;

	for (tlp_len = 0; valid_tlp && (tlp[tlp_len] != '\0'); ++tlp_len) {
		char arg = tlp[tlp_len];
		if (arg == '.' || arg == 'F') {
			/* OK */
		} else if (kyb_p < 0 && (arg == '?' || arg == 'S')) {
			kyb_p = tlp_len;
			if (arg == 'S')
				kyb_p |= 0x100;
		} else if (scr_p < 0 && (arg == '!' || arg == 'C')) {
			scr_p = tlp_len;
			if (arg == 'C')
				scr_p |= 0x100;
		} else if (err_p < 0 && (arg == '!' || arg == 'C')) {
			err_p = tlp_len;
			if (arg == 'C')
				err_p |= 0x100;
		} else {
			valid_tlp = 0;
		}	
	}
	
	if (tlp_len > TVM_ECTX_TLP_ARGS)
		valid_tlp = 0;

	#if !defined(TVM_DYNAMIC_OCCAM_PI)
	if ((kyb_p | scr_p | err_p) & 0x100)
		valid_tlp = 0;
	#endif

	if (!valid_tlp) {
		error_out_no_errno ("unsupported top-level-process format: \"%s\"", tlp);
		return -1;
	}

	#if defined(TVM_DYNAMIC_OCCAM_PI)
	if (kyb_p & 0x100)
		tlp_argv[0] = (WORD) tvm_mt_alloc (NULL, TLP_MT_CB_TYPE, 1);
	if (scr_p & 0x100)
		tlp_argv[1] = (WORD) tvm_mt_alloc (NULL, TLP_MT_CB_TYPE, 1);
	if (err_p & 0x100)
		tlp_argv[2] = (WORD) tvm_mt_alloc (NULL, TLP_MT_CB_TYPE, 1);
	kyb_p &= 0xff;
	scr_p &= 0xff;
	err_p &= 0xff;
	#endif /* TVM_DYNAMIC_OCCAM_PI */

	argv = (WORD *) malloc (sizeof (WORD) * tlp_len);

	for (i = 0; i < tlp_len; ++i) {
		if (i == kyb_p) {
			argv[i] = tlp_argv[0];
		} else if (i == scr_p) {
			argv[i] = tlp_argv[1];
		} else if (i == err_p) {
			argv[i] = tlp_argv[2];
		} else {
			argv[i] = (WORD) MIN_INT;
		}
	}

	user = allocate_ectx (us_bc, tlp, argv);
	
	if (argv != NULL)
		free (argv);
	if (user == NULL)
		return -1;

	user->ext_chan_table		= ext_chans;
	user->ext_chan_table_length	= ext_chans_length;

	return 0;
}
Exemplo n.º 6
0
uint STDCALL ge_load( pbuf in )
{
   pubyte   cur, end, ptemp;
   uint     size;
   pgehead  phead = ( pgehead )buf_ptr( in );

   // Проверка заголовка и целостности
   // Сравниваем с 'GE' с двумя нулями на конце

   if ( *( puint )phead != GE_STRING )//0x00004547 )
      msg( MNotGE | MSG_EXIT );
   if ( phead->crc != crc( ( pubyte )phead + 12, phead->size - 12, 0xFFFFFFFF ))
      msg( MCrcGE | MSG_EXIT );
   if ( phead->vermajor != GEVER_MAJOR || phead->verminor > GEVER_MINOR )
      msg( MVerGE | MSG_EXIT );

   _vm.loadmode = VMLOAD_GE;
   _vm.icnv = arr_count( &_vm.objtbl ) - KERNEL_COUNT;
//   print("icnv=%i\n", _vm.icnv );
   cur = ( pubyte )phead + phead->headsize;
   end = ( pubyte )phead + phead->size;
   while ( cur < end )
   {
      ptemp = cur + 5; // type + flag
      _vm.ipack = ( *( puint )( cur + 1 )) & GHCOM_PACK ? 1 : 0;

      size = load_bwd( &ptemp );
      ptemp = cur;
//      print("size=%i type=%i flag = %x\n", size, *cur, *( puint )( cur + 1 ) );
      switch ( *cur )
      {
         case OVM_NONE:
            load_none();
            break;
         case OVM_BYTECODE:
            load_bytecode( &cur, VMLOAD_GE );
            break;
         case OVM_EXFUNC:
            load_exfunc( &cur, 0 );
            _vm.loadmode = VMLOAD_GE;
            break;
        case OVM_TYPE:
            load_type( &cur );
            break;
        case OVM_GLOBAL:
            load_global( &cur );
            break;
        case OVM_DEFINE:
            load_define( &cur );
            break;
        case OVM_IMPORT:
            load_import( &cur );
            break;
         case OVM_RESOURCE:
            load_resource( &cur );
            break;
         case OVM_ALIAS:
            load_alias( &cur );
            break;
         default: 
            msg( MUnkGE | MSG_DVAL, cur - ( pubyte )phead );
      }
      cur = ptemp + size;
   }
   _vm.loadmode = VMLOAD_G;
   _vm.icnv  = 0;
   return 1;
}