Example #1
0
void ExpressionGraph::save(std::vector<io::Item>& ioItems) {
  for(auto p : params()->getMap()) {
    std::string pName = p.first;

    if(!namespace_.empty()) {
      if(pName.substr(0, namespace_.size() + 2) == namespace_ + "::")
        pName = pName.substr(namespace_.size() + 2);
    }

    ABORT_IF(p.second->val()->type() != Type::float32,
             "Only float32 supported at the moment");

    Tensor val = p.second->val();

    io::Item item;
    item.name = pName;
    item.shape = val->shape();
    item.type = val->type();

    // Use the actual memory as this will be aligned and padded.
    // When memory mapping this is required. Shape keeps track of
    // tensor size. Saving to *.npz will cut to size.
    auto mem = val->memory();
    item.bytes.resize(mem->size());
    copy(backend_,
         mem->data<char>(),
         mem->data<char>() + mem->size(),
         item.bytes.data());

    ioItems.emplace_back(std::move(item));
  }
}
Example #2
0
/*! Read expressions from file or memory and Evaluate 
*/
int EvaluateExpressions(const char *buffer, int buf_length, int is_filename)
{
	pANTLR3_INPUT_STREAM input_stream = NULL;
	pEvaLexer lxr = NULL;
	pANTLR3_COMMON_TOKEN_STREAM token_stream = NULL;
	pEvaParser psr = NULL;
	pEvaTree treeParser = NULL;
	pANTLR3_COMMON_TREE_NODE_STREAM	nodes = NULL;
	EvaParser_program_return eva_ast;
	int error = 0;

	ResetErrorString();
	
	/*Is it a file or memory*/
	if (is_filename) {
		input_stream = antlr3AsciiFileStreamNew((pANTLR3_UINT8)buffer);
	} else {
		input_stream = antlr3NewAsciiStringCopyStream((pANTLR3_UINT8)buffer, (ANTLR3_UINT32)buf_length, NULL);
	}
	ABORT_IF(input_stream == NULL);
	
	/*Invoke lexer and tokenzie*/
	lxr	= EvaLexerNew(input_stream);
	ABORT_IF(lxr == NULL);
	token_stream = antlr3CommonTokenStreamSourceNew(ANTLR3_SIZE_HINT, TOKENSOURCE(lxr));
	ABORT_IF(token_stream == NULL);

	/*Parse the expression*/
	psr	= EvaParserNew(token_stream);
	ABORT_IF(psr == NULL);
	//psr->pParser->rec->recoverFromMismatchedElement = recoverFromMismatchedElement;
	psr->pParser->rec->displayRecognitionError = DisplayRecognitionError;
	
	/*create ast from the parser*/
	eva_ast = psr->program(psr);
	/*check if there is parsing error*/
	ABORT_IF(psr->pParser->rec->state->errorCount > 0);
	
	nodes = antlr3CommonTreeNodeStreamNewTree(eva_ast.tree, ANTLR3_SIZE_HINT);
	ABORT_IF(nodes == NULL);
		
	/*Walk the tree and evaluate the expression*/
	treeParser = EvaTreeNew(nodes);
	ABORT_IF(treeParser == NULL);
	/*Take action*/
	treeParser->program(treeParser);

	/*All done lets cleanup*/
clean_up:
	FREE(treeParser);
	FREE(nodes);
	FREE(psr);
	FREE(token_stream);
	FREE(lxr);
	FREE(input_stream);
	
	return error;
}
void SingletonGraph::execute(Ptr<data::Batch> batch) {
  auto costNode = builder_->build(graph_, batch);

  graph_->forward();
  float cost = costNode->scalar();
  graph_->backward();

  // Get batch stats
  size_t batch_words = batch->wordsTrg();

  if(scaleLearningRate_) {
    opt_->update(graph_, batch_words / avgBatchWords_);
  } else {
    opt_->update(graph_);
  }

  if(mvAvg_) {
    ABORT_IF(!scheduler_, "Scheduler is required for exponential smoothing");

    if(!graphAvg_) {
      graphAvg_ = New<ExpressionGraph>();
      graphAvg_->setDevice(graph_->getDeviceId());
      graphAvg_->copyParams(graph_);
    } else {
      updateAvgParams(graphAvg_->params()->vals(),
                      graph_->params()->vals(),
                      scheduler_->numberOfBatches());
    }
  }

  if(scheduler_) {
    scheduler_->update(cost, batch);

    if(scheduler_->validating()) {
      if(mvAvg_) {
        graphAvg_->reuseWorkspace(graph_);
        scheduler_->validate({graphAvg_});
      } else {
        scheduler_->validate({graph_});
      }
    }

    if(scheduler_->saving())
      this->save();
  }
}
/*
 * Make a value type.
 */
static void MakeValueType(ILGenInfo *info, ILImage *image,
						  const char *name, ILClass *parent,
						  ILClass *stringClass)
{
	ILClass *newClass;
	ABORT_IF(newClass, ILClassCreate(ILClassGlobalScope(image),
									 0, name, "System", parent));
	ILClassSetAttrs(newClass, ~0,
					IL_META_TYPEDEF_PUBLIC |
				    IL_META_TYPEDEF_VALUE_TYPE |
				    IL_META_TYPEDEF_LAYOUT_SEQUENTIAL |
				    IL_META_TYPEDEF_SERIALIZABLE |
				    IL_META_TYPEDEF_BEFORE_FIELD_INIT |
				    IL_META_TYPEDEF_SEALED);
	if(!AddMethod(newClass, "ToString",
				  ILType_FromClass(stringClass), ILType_Void, ILType_Void,
				  IL_META_METHODDEF_VIRTUAL))
	{
		ILGenOutOfMemory(info);
	}
}
Example #5
0
void SingletonGraph::execute(Ptr<data::Batch> batch) {
  auto lossNode = builder_->build(graph_, batch);
  graph_->forward();
  graph_->backward();

  // Get batch stats
  opt_->update(graph_);

  if(mvAvg_) {
    ABORT_IF(!scheduler_, "Scheduler is required for exponential smoothing");

    if(!graphAvg_) {
      graphAvg_ = New<ExpressionGraph>();
      graphAvg_->setDevice(graph_->getDeviceId());
      graphAvg_->copyParams(graph_);
    } else {
      updateAvgParams(graphAvg_->params()->vals(),
                      graph_->params()->vals(),
                      scheduler_->numberOfBatches());
    }
  }

  if(scheduler_) {
    scheduler_->update(*lossNode, batch);

    if(scheduler_->validating()) {
      if(mvAvg_) {
        graphAvg_->reuseWorkspace(graph_);
        scheduler_->validate({graphAvg_});
      } else {
        scheduler_->validate({graph_});
      }
    }

    if(scheduler_->saving())
      this->save();
  }
}
Example #6
0
static void
start_server (void) {
    int                 client_sd;
    int                 daemon_sd;
    int                 connected_sd;
    struct sockaddr_in  client_sa;
    struct sockaddr_in  daemon_sa;
    struct sockaddr_in  connected_sa;
    socklen_t           size;
    fd_set              socket_set;
    int                 nfds;
    struct ifreq        if_info;
    struct sockaddr_in  *if_addr;
    char                addr[INET_ADDRSTRLEN];
    struct client       *c;
    struct daemon       *d;
    struct parsed_cmd   *pcmd = NULL;
    char                *ident_msg;
    int                 port;
    char                *colon;


    /* Prepare all the threads */
    slow_pool = NULL;
    fast_pool = NULL;
    clients_pool = NULL;
    daemons_pool = NULL;

    ABORT_IF (!(slow_pool = pool_create (prefs->nb_proc)),
        "Unable to create slow_pool")

    ABORT_IF (!(fast_pool = pool_create (prefs->nb_proc)),
        "Unable to create fast_pool")

    ABORT_IF (!(clients_pool = pool_create (prefs->max_clients)),
        "Unable to create clients_pool")

    ABORT_IF (!(daemons_pool = pool_create (prefs->max_daemons)),
        "Unable to create daemons_pool")

    /* Create the shared directory if it does not exist already */
    ABORT_IF (create_dir (prefs->shared_folder, (mode_t)0755) < 0,
                "Unable to create shared directory")

    /* Initialize global pointers and their semaphores */
    clients = NULL;
    ABORT_IF (sem_init (&clients_lock, 0, 1) < 0,
        "Unable to sem_init clients_lock")

    daemons = NULL;
    ABORT_IF (sem_init (&daemons_lock, 0, 1) < 0,
        "Unable to sem_init daemons_lock")

    file_cache = NULL;
    ABORT_IF (sem_init (&file_cache_lock, 0, 1) < 0,
        "Unable to sem_init file_cache_lock")

    list_client = NULL;
    ABORT_IF (sem_init (&list_lock, 0, 1) < 0,
        "Unable to sem_init list_lock")

    downloads = NULL;
    ABORT_IF (sem_init (&downloads_lock, 0, 1) < 0,
        "Unable to sem_init download_queue_lock")

    client_sa.sin_family        = AF_INET;
    client_sa.sin_addr.s_addr   = INADDR_ANY;
    client_sa.sin_port          = htons (prefs->client_port);
    client_sd = socket_init (&client_sa);
    ABORT_IF (client_sd < 0,
        "Unable to socket_init client_sd")

    daemon_sa.sin_family        = AF_INET;
    daemon_sa.sin_addr.s_addr   = INADDR_ANY;
    daemon_sa.sin_port          = htons (prefs->daemon_port);
    daemon_sd = socket_init (&daemon_sa);
    ABORT_IF (daemon_sd < 0,
        "Unable to socket_init daemon_sd")

#if 1
    /* We get our ip */
    memcpy (if_info.ifr_name, prefs->interface, strlen (prefs->interface) + 1);
    if (ioctl (daemon_sd, SIOCGIFADDR, &if_info) == -1) {
        log_failure (log_file, "Can't get my ip from interface");
        log_failure (log_file, "LOL ERRNO : %s\n", strerror (errno));
        goto abort;
    }
    if_addr = (struct sockaddr_in *)&if_info.ifr_addr;
    inet_ntop (AF_INET, &if_addr->sin_addr, my_ip, INET_ADDRSTRLEN);
    log_success (log_file, "Found my IP : %s", my_ip);
#endif

    /* socket_set contains both client_sd and daemon_sd */
    FD_ZERO (&socket_set);
    size = sizeof (connected_sa);
    nfds = NFDS (client_sd, daemon_sd);
    for (;;) {
        /*
         * It is VERY important to FD_SET at each loop, because select
         * will FD_UNSET the socket descriptors
         */
        FD_SET (client_sd, &socket_set);
        FD_SET (daemon_sd, &socket_set);

        /* Block until a socket is ready to accept */
        if (select (nfds, &socket_set, NULL, NULL, NULL) < 0) {
            log_failure (log_file, "main () : select failed");
        }

        if (FD_ISSET (client_sd, &socket_set)) {
            if ((connected_sd = (accept (client_sd,
                                        (struct sockaddr *) &connected_sa,
                                        &size))) < 0) {
                log_failure (log_file, "Failed to accept incoming connection.");
                break;
            }

            /* Can we handle this client? */
            if (client_count () > prefs->max_clients) {
                socket_sendline (connected_sd, " < Too many clients\n");
                goto close_socket;
            }

            /* Then, let's handle him */
            if (!inet_ntop (AF_INET, &connected_sa.sin_addr,
                            addr, INET_ADDRSTRLEN)) {
                socket_sendline (connected_sd, " < Oops\n");
                goto close_socket;
            }
            if (!(c = client_new (connected_sd, addr))) {
                socket_sendline (connected_sd, " < Sorry pal :(\n");
            }

            pool_queue (clients_pool, handle_client, c);
        }
        else if (FD_ISSET (daemon_sd, &socket_set)) {
            if ((connected_sd = (accept (daemon_sd,
                                        (struct sockaddr *) &connected_sa,
                                        &size))) < 0) {
                log_failure (log_file, "Failed to accept incoming connection.");
                break;
            }

            /* Can we handle this daemon? */
            if (daemon_count () > prefs->max_daemons) {
                socket_sendline (connected_sd, " < Too many daemons\n");
                goto close_socket;
            }

            /* Let's identify him first */
            ident_msg = socket_try_getline (connected_sd,
                                            IDENTIFICATION_TIMEOUT);
            if (!ident_msg) {
                socket_sendline (connected_sd,
                                "error: identification timed out\n");
                goto close_socket;
            }
            if (cmd_parse_failed ((pcmd = cmd_parse (ident_msg, NULL)))) {
                pcmd = NULL;
                goto close_socket;
            }
            if (pcmd->argc < 2)
                goto close_socket;
            if (strcmp (pcmd->argv[0], "neighbour") != 0)
                goto close_socket;
            if (!(colon = strchr (pcmd->argv[1], ':')))
                goto close_socket;
            port = atoi (colon + 1);

            free (ident_msg);
            cmd_parse_free (pcmd);
            pcmd = NULL;

            if (!inet_ntop (AF_INET, &connected_sa.sin_addr,
                            addr, INET_ADDRSTRLEN)) {
                socket_sendline (connected_sd, " < Oops\n");
                goto close_socket;
            }

            /* Now we've got his port, let him go in */
            if (!(d = daemon_new (connected_sd, addr, port))) {
                socket_sendline (connected_sd, " < Sorry pal :(\n");
                goto close_socket;
            }

            pool_queue (daemons_pool, handle_daemon, d);
        }
        else {
            /* This should never happen : neither client nor daemon!? */
            log_failure (log_file, "Unknown connection");
        }

        continue;

close_socket:
        if (pcmd) {
            cmd_parse_free (pcmd);
            pcmd = NULL;
        }
        close (connected_sd);
    }

abort:
    if (slow_pool)
        pool_destroy (slow_pool);
    if (fast_pool)
        pool_destroy (fast_pool);
    if (clients_pool)
        pool_destroy (clients_pool);
    if (daemons_pool)
        pool_destroy (daemons_pool);
    conf_free (prefs);
    exit (EXIT_FAILURE);
}
Example #7
0
void ExpressionGraph::checkNan(Tensor t) {
  ABORT_IF(throwNaN_, "Not implemented"); t;
  // ABORT_IF(throwNaN_ && IsNan(t), "Tensor has NaN");
}
void ILGenMakeLibrary(ILGenInfo *info)
{
	ILImage *image = info->libImage;
	ILProgramItem *scope = ILClassGlobalScope(image);
	ILClass *objectClass;
	ILClass *stringClass;
	ILClass *typeClass;
	ILClass *valueTypeClass;
	ILClass *enumClass;
	ILClass *voidClass;
	ILClass *intPtrClass;
	ILClass *uintPtrClass;
	ILClass *typedRefClass;
	ILClass *argIterClass;
	ILClass *argHandleClass;
	ILClass *attributeClass;
	ILClass *paramAttributeClass;
	ILClass *defMemberAttributeClass;
	ILClass *decimalConstantClass;
	ILClass *exceptionClass;
	ILClass *disposableInterface;
	ILClass *asyncResultInterface;
	ILClass *collectionInterface;
	ILClass *enumeratorInterface;
	ILClass *isVolatileClass;
	ILClass *delegateClass;
	ILClass *multicastDelegateClass;
	ILClass *asyncCallbackClass;
	int constructorOK;
	ILMethod *method;
	ILProperty *property;
	ILType *signature;

	/* Create the "System.Object" class */
	ABORT_IF(objectClass, ILClassCreate(scope, 0, "Object", "System", 0));
	ILClassSetAttrs(objectClass, ~0,
				    IL_META_TYPEDEF_PUBLIC |
				    IL_META_TYPEDEF_BEFORE_FIELD_INIT |
					IL_META_TYPEDEF_SERIALIZABLE);
	ABORT_IF(constructorOK, AddDefaultConstructor(objectClass));

	/* Create the "System.String" class */
	ABORT_IF(stringClass,
			 ILClassCreate(scope, 0, "String", "System", objectClass));
	ILClassSetAttrs(stringClass, ~0,
					IL_META_TYPEDEF_PUBLIC |
				    IL_META_TYPEDEF_SERIALIZABLE |
					IL_META_TYPEDEF_BEFORE_FIELD_INIT |
				    IL_META_TYPEDEF_SEALED);

	/* Create the "System.Type" class */
	ABORT_IF(typeClass,
			 ILClassCreate(scope, 0, "Type", "System", objectClass));
	ILClassSetAttrs(typeClass, ~0,
					IL_META_TYPEDEF_PUBLIC |
				    IL_META_TYPEDEF_SERIALIZABLE |
					IL_META_TYPEDEF_BEFORE_FIELD_INIT |
				    IL_META_TYPEDEF_ABSTRACT);
	ABORT_IF(constructorOK, AddDefaultConstructor(typeClass));

	/* Add the "ToString" and "GetType" methods to the "System.Object" class */
	if(!AddMethod(objectClass, "ToString",
				  ILType_FromClass(stringClass), ILType_Void, ILType_Void,
				  IL_META_METHODDEF_VIRTUAL | IL_META_METHODDEF_NEW_SLOT))
	{
		ILGenOutOfMemory(info);
	}
	if(!AddMethod(objectClass, "GetType",
				  ILType_FromClass(typeClass),
				  ILType_Void, ILType_Void, 0))
	{
		ILGenOutOfMemory(info);
	}

	/* Add the "==" and "!=" operators to the "System.String" class */
	if(!AddMethod(stringClass, "op_Equality",
				  ILType_Boolean, ILType_FromClass(stringClass),
				  ILType_FromClass(stringClass),
				  IL_META_METHODDEF_STATIC | IL_META_METHODDEF_SPECIAL_NAME))
	{
		ILGenOutOfMemory(info);
	}
	if(!AddMethod(stringClass, "op_Inequality",
				  ILType_Boolean, ILType_FromClass(stringClass),
				  ILType_FromClass(stringClass),
				  IL_META_METHODDEF_STATIC | IL_META_METHODDEF_SPECIAL_NAME))
	{
		ILGenOutOfMemory(info);
	}

	/* Create the "System.ValueType" class */
	ABORT_IF(valueTypeClass,
			 ILClassCreate(scope, 0, "ValueType", "System", objectClass));
	ILClassSetAttrs(valueTypeClass, ~0,
					IL_META_TYPEDEF_PUBLIC |
					IL_META_TYPEDEF_BEFORE_FIELD_INIT |
				    IL_META_TYPEDEF_SERIALIZABLE);
	ABORT_IF(constructorOK, AddDefaultConstructor(valueTypeClass));

	/* Create the "System.Enum" class */
	ABORT_IF(enumClass,
			 ILClassCreate(scope, 0, "Enum", "System", valueTypeClass));
	ILClassSetAttrs(enumClass, ~0,
					IL_META_TYPEDEF_PUBLIC |
					IL_META_TYPEDEF_ABSTRACT |
					IL_META_TYPEDEF_BEFORE_FIELD_INIT |
				    IL_META_TYPEDEF_SERIALIZABLE);
	ABORT_IF(constructorOK, AddDefaultConstructor(enumClass));

	/* Create the "System.Void" class */
	ABORT_IF(voidClass,
			 ILClassCreate(scope, 0, "Void", "System", valueTypeClass));
	ILClassSetAttrs(voidClass, ~0,
					IL_META_TYPEDEF_PUBLIC |
				    IL_META_TYPEDEF_VALUE_TYPE |
					IL_META_TYPEDEF_BEFORE_FIELD_INIT |
					IL_META_TYPEDEF_SEALED);

	/* Create the numeric value types */
	MakeValueType(info, image, "SByte", valueTypeClass, stringClass);
	MakeValueType(info, image, "Byte", valueTypeClass, stringClass);
	MakeValueType(info, image, "Int16", valueTypeClass, stringClass);
	MakeValueType(info, image, "UInt16", valueTypeClass, stringClass);
	MakeValueType(info, image, "Int32", valueTypeClass, stringClass);
	MakeValueType(info, image, "UInt32", valueTypeClass, stringClass);
	MakeValueType(info, image, "Int64", valueTypeClass, stringClass);
	MakeValueType(info, image, "UInt64", valueTypeClass, stringClass);
	MakeValueType(info, image, "Single", valueTypeClass, stringClass);
	MakeValueType(info, image, "Double", valueTypeClass, stringClass);
	MakeValueType(info, image, "Decimal", valueTypeClass, stringClass);

	/* Create the "System.IntPtr" class */
	ABORT_IF(intPtrClass,
			 ILClassCreate(scope, 0, "IntPtr", "System", valueTypeClass));
	ILClassSetAttrs(intPtrClass, ~0,
					IL_META_TYPEDEF_PUBLIC |
				    IL_META_TYPEDEF_VALUE_TYPE |
				    IL_META_TYPEDEF_SERIALIZABLE |
					IL_META_TYPEDEF_BEFORE_FIELD_INIT |
				    IL_META_TYPEDEF_SEALED);

	/* Create the "System.UIntPtr" class */
	ABORT_IF(uintPtrClass,
			 ILClassCreate(scope, 0, "UIntPtr", "System", valueTypeClass));
	ILClassSetAttrs(uintPtrClass, ~0,
					IL_META_TYPEDEF_PUBLIC |
				    IL_META_TYPEDEF_VALUE_TYPE |
				    IL_META_TYPEDEF_SERIALIZABLE |
					IL_META_TYPEDEF_BEFORE_FIELD_INIT |
				    IL_META_TYPEDEF_SEALED);

	/* Create the "System.TypedReference" class */
	ABORT_IF(typedRefClass,
			 ILClassCreate(scope, 0, "TypedReference", "System",
			 			   valueTypeClass));
	ILClassSetAttrs(typedRefClass, ~0,
					IL_META_TYPEDEF_PUBLIC |
				    IL_META_TYPEDEF_VALUE_TYPE |
				    IL_META_TYPEDEF_SERIALIZABLE |
					IL_META_TYPEDEF_BEFORE_FIELD_INIT |
				    IL_META_TYPEDEF_SEALED);

	/* Create the "System.ArgIterator" class */
	ABORT_IF(argIterClass,
			 ILClassCreate(scope, 0, "ArgIterator", "System",
			 			   valueTypeClass));
	ILClassSetAttrs(argIterClass, ~0,
					IL_META_TYPEDEF_PUBLIC |
				    IL_META_TYPEDEF_VALUE_TYPE |
				    IL_META_TYPEDEF_SERIALIZABLE |
					IL_META_TYPEDEF_BEFORE_FIELD_INIT |
				    IL_META_TYPEDEF_SEALED);

	/* Create the "System.RuntimeArgumentHandle" class */
	ABORT_IF(argHandleClass,
			 ILClassCreate(scope, 0, "RuntimeArgumentHandle", "System",
			 			   valueTypeClass));
	ILClassSetAttrs(argHandleClass, ~0,
					IL_META_TYPEDEF_PUBLIC |
				    IL_META_TYPEDEF_VALUE_TYPE |
				    IL_META_TYPEDEF_SERIALIZABLE |
					IL_META_TYPEDEF_BEFORE_FIELD_INIT |
				    IL_META_TYPEDEF_SEALED);

	/* Create the "System.Attribute" class */
	ABORT_IF(attributeClass,
			 ILClassCreate(scope, 0, "Attribute", "System",
			 			   objectClass));
	ILClassSetAttrs(attributeClass, ~0,
					IL_META_TYPEDEF_PUBLIC |
				    IL_META_TYPEDEF_SERIALIZABLE |
					IL_META_TYPEDEF_BEFORE_FIELD_INIT |
				    IL_META_TYPEDEF_ABSTRACT);

	/* Create the "System.ParamArrayAttribute" class */
	ABORT_IF(paramAttributeClass,
			 ILClassCreate(scope, 0, "ParamArrayAttribute", "System",
			 			   attributeClass));
	ILClassSetAttrs(paramAttributeClass, ~0,
					IL_META_TYPEDEF_PUBLIC |
				    IL_META_TYPEDEF_SERIALIZABLE |
					IL_META_TYPEDEF_BEFORE_FIELD_INIT |
				    IL_META_TYPEDEF_SEALED);
	ABORT_IF(constructorOK, AddDefaultConstructor(paramAttributeClass));

	/* Create the "System.Reflection.DefaultMemberAttribute" class */
	ABORT_IF(defMemberAttributeClass,
			 ILClassCreate(scope, 0, "DefaultMemberAttribute",
			 			   "System.Reflection", attributeClass));
	ILClassSetAttrs(defMemberAttributeClass, ~0,
					IL_META_TYPEDEF_PUBLIC |
				    IL_META_TYPEDEF_SERIALIZABLE |
					IL_META_TYPEDEF_BEFORE_FIELD_INIT |
				    IL_META_TYPEDEF_SEALED);
	ABORT_IF(constructorOK,
			 AddParamConstructor(defMemberAttributeClass,
			 					 ILType_FromClass(stringClass), ILType_Void));

	/* Create "System.Runtime.CompilerServices.DecimalConstantAttribute" */
	ABORT_IF(decimalConstantClass,
			 ILClassCreate(scope, 0, "DecimalConstantAttribute",
			 			   "System.Runtime.CompilerServices",
			 			   attributeClass));
	ILClassSetAttrs(decimalConstantClass, ~0,
					IL_META_TYPEDEF_PUBLIC |
				    IL_META_TYPEDEF_SERIALIZABLE |
					IL_META_TYPEDEF_BEFORE_FIELD_INIT |
				    IL_META_TYPEDEF_SEALED);
	ABORT_IF(constructorOK, AddDecimalConstructor(decimalConstantClass));

	/* Create the "System.Exception" class */
	ABORT_IF(exceptionClass,
			 ILClassCreate(scope, 0, "Exception", "System",
			 			   objectClass));
	ILClassSetAttrs(exceptionClass, ~0,
					IL_META_TYPEDEF_PUBLIC |
				    IL_META_TYPEDEF_SERIALIZABLE |
					IL_META_TYPEDEF_BEFORE_FIELD_INIT);
	ABORT_IF(constructorOK, AddDefaultConstructor(exceptionClass));

	/* Create the "System.IDisposable" interface */
	ABORT_IF(disposableInterface,
			 ILClassCreate(scope, 0, "IDisposable", "System", 0));
	ILClassSetAttrs(disposableInterface, ~0,
					IL_META_TYPEDEF_PUBLIC |
				    IL_META_TYPEDEF_INTERFACE |
					IL_META_TYPEDEF_ABSTRACT);

	/* Create the "System.IAsyncResult" interface */
	ABORT_IF(asyncResultInterface,
			 ILClassCreate(scope, 0, "IAsyncResult", "System", 0));
	ILClassSetAttrs(asyncResultInterface, ~0,
					IL_META_TYPEDEF_PUBLIC |
				    IL_META_TYPEDEF_INTERFACE |
					IL_META_TYPEDEF_ABSTRACT);

	/* Create the "System.Collections.ICollection" interface */
	ABORT_IF(collectionInterface,
			 ILClassCreate(scope, 0, "ICollection", "System.Collections", 0));
	ILClassSetAttrs(collectionInterface, ~0,
					IL_META_TYPEDEF_PUBLIC |
				    IL_META_TYPEDEF_INTERFACE |
					IL_META_TYPEDEF_ABSTRACT);

	/* Create the "System.Collections.IEnumerator" interface */
	ABORT_IF(enumeratorInterface,
			 ILClassCreate(scope, 0, "IEnumerator", "System.Collections", 0));
	ILClassSetAttrs(enumeratorInterface, ~0,
					IL_META_TYPEDEF_PUBLIC |
				    IL_META_TYPEDEF_INTERFACE |
					IL_META_TYPEDEF_ABSTRACT);

	/* Add the "GetEnumerator" method to the "ICollection" interface */
	ABORT_IF(method, AddAbstractMethod(collectionInterface, "GetEnumerator",
									   ILType_FromClass(enumeratorInterface),
									   ILType_Void));

	/* Add the "MoveNext" and "Reset" methods to "IEnumerator" */
	ABORT_IF(method, AddAbstractMethod(enumeratorInterface, "MoveNext",
									   ILType_Boolean, ILType_Void));
	ABORT_IF(method, AddAbstractMethod(enumeratorInterface, "Reset",
									   ILType_Void, ILType_Void));

	/* Add the "Current" property to "IEnumerator" */
	ABORT_IF(method, AddAbstractMethod(enumeratorInterface, "get_Current",
									   ILType_FromClass(objectClass),
									   ILType_Void));
	ABORT_IF(signature, ILTypeCreateProperty(info->context,
									 		 ILType_FromClass(objectClass)));
	ILTypeSetCallConv(signature, IL_META_CALLCONV_HASTHIS);
	ABORT_IF(property, ILPropertyCreate(enumeratorInterface, 0, "Current",
										0, signature));
	if(!ILMethodSemCreate((ILProgramItem *)property, 0,
				  		  IL_META_METHODSEM_GETTER, method))
	{
		ILGenOutOfMemory(info);
	}

	/* Create the "System.Runtime.CompilerServices.IsVolatile" class */
	ABORT_IF(isVolatileClass,
			 ILClassCreate(scope, 0, "IsVolatile",
			 			   "System.Runtime.CompilerServices",
			 			   objectClass));
	ILClassSetAttrs(isVolatileClass, ~0,
					IL_META_TYPEDEF_PUBLIC |
					IL_META_TYPEDEF_SEALED |
				    IL_META_TYPEDEF_SERIALIZABLE |
					IL_META_TYPEDEF_BEFORE_FIELD_INIT);
	ABORT_IF(constructorOK, AddDefaultConstructor(isVolatileClass));

	/* Create the "System.Delegate" class */
	ABORT_IF(delegateClass,
			 ILClassCreate(scope, 0, "Delegate", "System", objectClass));
	ILClassSetAttrs(delegateClass, ~0,
					IL_META_TYPEDEF_PUBLIC |
				    IL_META_TYPEDEF_SERIALIZABLE |
					IL_META_TYPEDEF_BEFORE_FIELD_INIT |
				    IL_META_TYPEDEF_ABSTRACT);
	if(!AddMethod(delegateClass, "op_Equality", ILType_Boolean,
				  ILType_FromClass(delegateClass),
				  ILType_FromClass(delegateClass),
				  IL_META_METHODDEF_SPECIAL_NAME | IL_META_METHODDEF_STATIC))
	{
		ILGenOutOfMemory(info);
	}
	if(!AddMethod(delegateClass, "op_Inequality", ILType_Boolean,
				  ILType_FromClass(delegateClass),
				  ILType_FromClass(delegateClass),
				  IL_META_METHODDEF_SPECIAL_NAME | IL_META_METHODDEF_STATIC))
	{
		ILGenOutOfMemory(info);
	}

	/* Create the "System.MulticastDelegate" class */
	ABORT_IF(multicastDelegateClass,
			 ILClassCreate(scope, 0, "MulticastDelegate", "System",
			 			   delegateClass));
	ILClassSetAttrs(multicastDelegateClass, ~0,
					IL_META_TYPEDEF_PUBLIC |
				    IL_META_TYPEDEF_SERIALIZABLE |
					IL_META_TYPEDEF_BEFORE_FIELD_INIT |
				    IL_META_TYPEDEF_ABSTRACT);
	if(!AddMethod(multicastDelegateClass, "op_Equality", ILType_Boolean,
				  ILType_FromClass(multicastDelegateClass),
				  ILType_FromClass(multicastDelegateClass),
				  IL_META_METHODDEF_SPECIAL_NAME | IL_META_METHODDEF_STATIC))
	{
		ILGenOutOfMemory(info);
	}
	if(!AddMethod(multicastDelegateClass, "op_Inequality", ILType_Boolean,
				  ILType_FromClass(multicastDelegateClass),
				  ILType_FromClass(multicastDelegateClass),
				  IL_META_METHODDEF_SPECIAL_NAME | IL_META_METHODDEF_STATIC))
	{
		ILGenOutOfMemory(info);
	}

	/* Create the "AsyncCallback" delegate class */
	ABORT_IF(asyncCallbackClass,
			 ILClassCreate(scope, 0, "AsyncCallback", "System",
			 			   multicastDelegateClass));
	ILClassSetAttrs(asyncCallbackClass, ~0,
					IL_META_TYPEDEF_PUBLIC |
				    IL_META_TYPEDEF_SERIALIZABLE |
					IL_META_TYPEDEF_BEFORE_FIELD_INIT |
				    IL_META_TYPEDEF_ABSTRACT);
}
Example #9
0
static void regen_target_code(CPUState *env, TranslationBlock *tb)
{
    decode_t ds1, *ds = &ds1;
    int num_insns;
    bool cont_trans;

    ///fprintf(stderr, "regen: tb:0x%x\n", (uint32_t)tb->pc);

    cgc->pc_start = tb->pc;
    cgc->pc_ptr = cgc->pc_start;
    env->code_ptr = tb->tc_ptr;
    cont_trans = true;
    env->patch_count = 0;
    cur_tb = tb;

    SuperTransBlock *cur_stb;
    cur_stb = &(cgc->stbs[cgc->stb_count++]);
    ABORT_IF((cgc->stb_count >= STB_MAX), "stb count, overflow\n");

    cur_stb->pc_start = tb->pc;
    cur_stb->tc_ptr = (uint32_t)(tb->tc_ptr);

    ///printf("start tb_gen_code cgc->pc_ptr = 0x%x\n", cgc->pc_ptr);
    for(num_insns = 0; ;num_insns++) {
        simple_disas_insn(ds, cgc->pc_ptr);
        ///fprintf(stderr, "regen pc_ptr = 0x%x\n", cgc->pc_ptr);
        ABORT_IF((ds->opstate & OPSTATE_ADDR16), "error: OPSTATE_ADDR16\n");

        cgc->insn_len = ds->decode_eip - cgc->pc_ptr;
        cgc->pc_ptr = ds->decode_eip;

        /* emit the insn */
        cont_trans = (ds->emitfn)(env, ds);
        tb->insn_count++;

        /* stop translation if indicated */
        if (cont_trans == false) break;

        if ((env->patch_count >= PATCH_ARRAY_SIZE - 2) ||
            (num_insns >= MAX_INSNS)) {
            if(tb->tc_ptr == env->code_ptr) {
                tb->tc_ptr = (uint8_t *)NOT_TRANS_YET;
            }
            code_emit8(env->code_ptr, 0xe9);
            code_emit32(env->code_ptr, NEED_PATCH_32);
            note_patch(env, env->code_ptr - 4, (uint8_t *)cgc->pc_ptr, 
                       (uint8_t *)tb, tb->func_addr, tb->tb_tag);
            break;
        }
    } /* end for */
    cur_stb->pc_end = cgc->pc_ptr - 1;
    ///qemu_log("end for cgc->pc_ptr = 0x%x\n", cgc->pc_ptr);

    lazy_patch(env);

    cgc->s_code_size += (cgc->pc_ptr - cgc->pc_start);

    code_gen_ptr = env->code_ptr;
    ABORT_IF((code_gen_ptr - code_gen_buffer > DEFAULT_CODE_GEN_BUFFER_SIZE),
           "code_buffer overflow\n");
    tb->hcode_size = code_gen_ptr - tb->tc_ptr;
}
Example #10
0
void cemit_retrans_ind(CPUX86State *env, uint32_t ind_type)
{
    uint8_t *patch_addr;
    uint32_t addr, jmp_offset;

    cur_tb->ind_miss_count = 0;
    addr = (uint32_t)&cur_tb->ind_miss_count;

    /* pushf */
    code_emit8(env->code_ptr, 0x9c);
    /* incl tb->ind_miss_count */
    code_emit8(env->code_ptr, 0xff);
    code_emit8(env->code_ptr, 0x05); /* ModRM = 00 000 101b */
    code_emit32(env->code_ptr, addr);
    /* cmpl ind_miss_count, RETRANS_THRESHOLD */
    code_emit8(env->code_ptr, 0x81);
    code_emit8(env->code_ptr, 0x3d); /* ModRM = 00 111 101b */
    code_emit32(env->code_ptr, addr);
    code_emit32(env->code_ptr, RETRANS_THRESHOLD);
    /* je equal */
    /* fall-through to the code_ptr */
    code_emit8(env->code_ptr, 0x0f);
    code_emit8(env->code_ptr, 0x84);
    patch_addr = env->code_ptr;
    code_emit32(env->code_ptr, NEED_PATCH_32);
    /* popf */
    code_emit8(env->code_ptr, 0x9d);

    /* equal: NOTE: following code is placed in sieve_code_ptr */
    *(uint32_t *)patch_addr = env->sieve_code_ptr - patch_addr - 4;
    /* pusha */
    code_emit8(env->sieve_code_ptr, 0x60);
    /* push cur_tb */
    code_emit8(env->sieve_code_ptr, 0x68);
    code_emit32(env->sieve_code_ptr, (uint32_t)cur_tb);
    /* push env */
    code_emit8(env->sieve_code_ptr, 0x68);
    code_emit32(env->sieve_code_ptr, (uint32_t)env);
    /* call retrans_ind */
    code_emit8(env->sieve_code_ptr, 0xe8);
    jmp_offset = (uint32_t)retrans_ind - (uint32_t)(env->sieve_code_ptr) - 4;
    code_emit32(env->sieve_code_ptr, jmp_offset);
    /* leal %esp, 8(%esp) */
    code_emit8(env->sieve_code_ptr, 0x8d);
    code_emit8(env->sieve_code_ptr, 0x64); /*01 100 100 */
    code_emit8(env->sieve_code_ptr, 0x24); /*10 100 100 */
    code_emit8(env->sieve_code_ptr, 8);
    /* popa */
    code_emit8(env->sieve_code_ptr, 0x61);
    /* popf */
    code_emit8(env->sieve_code_ptr, 0x9d);
#if 0
    /* pop %ecx */
    code_emit8(env->sieve_code_ptr, 0x59);
    /* mov $cur_tb, (ret_tb) */
    addr = (uint32_t)&(env->ret_tb);
    code_emit8(env->sieve_code_ptr, 0xc7);
    code_emit8(env->sieve_code_ptr, 0x05); /* ModRM = 00 000 101b */
    code_emit32(env->sieve_code_ptr, addr);
    code_emit32(env->sieve_code_ptr, (uint32_t)cur_tb);
    /* mov m_ind_type, (&env->ind_type) */
    addr = (uint32_t)&(env->ind_type);
    code_emit8(env->sieve_code_ptr, 0xc7);
    code_emit8(env->sieve_code_ptr, 0x05); /* ModRM = 00 000 101b */
    code_emit32(env->sieve_code_ptr, addr);
    code_emit32(env->sieve_code_ptr, IND_TYPE_JMP);
    /* pop (env->eip) */
    addr = (uint32_t)&(env->eip);
    code_emit8(env->sieve_code_ptr, 0x8fu);
    code_emit8(env->sieve_code_ptr, 0x05u); /* ModRM = 00 000 101b */
    code_emit32(env->sieve_code_ptr, addr);
    /* jmp tb_epilogue */
    code_emit8(env->sieve_code_ptr, 0xe9u);
    jmp_offset = cgc->tb_ret_addr - env->sieve_code_ptr - 4;
    code_emit32(env->sieve_code_ptr, jmp_offset);
#else
    /* jmp back to code_ptr */
    code_emit8(env->sieve_code_ptr, 0xe9);
    code_emit32(env->sieve_code_ptr,
               (uint32_t)env->code_ptr - (uint32_t)env->sieve_code_ptr - 4);
#endif

    ABORT_IF((env->sieve_code_ptr - sieve_buffer > DEFAULT_SIEVE_BUFFER_SIZE),
           "out of sieve buffer\n");
}
Example #11
0
uint32_t get_retrans_tb(CPUState *env, TranslationBlock *start_tb)
{
    int i;
    retr_node nodes[PATH_DEPTH * LEAF_MAX];
    retr_node *leaves[LEAF_MAX];
    retr_node *node;
    int node_idx, leaf_idx;
    retr_node *travel_queue[PATH_DEPTH * LEAF_MAX];
    int queue_head, queue_tail;
    TranslationBlock *tb;
    int depth;
    int path_count;

    //fprintf(stderr, "enter get_retrans_tb, tb = 0x%x\n", start_tb->pc);
    path_count = 0;
    node_idx = 0;
    leaf_idx = 0;
    depth = 0; 
    queue_head = 0;
    queue_tail = 0;

    node = &nodes[node_idx++];
    node->tb = start_tb;
    node->depth = 0;
    node->patch_num = CONT_TRANS_TAG;
    node->next = NULL;

#ifdef VAR_PATH
    path_depth = 3;
#else
    path_depth = PATH_DEPTH;
#endif

    /* Broaden First Travel */
    travel_queue[queue_tail++] = node;

    while(queue_head != queue_tail) {
        node = travel_queue[queue_head++];
        tb = node->tb;

#ifdef VAR_PATH
        /* check the node num */
        if(node->depth == path_depth && path_depth < PATH_DEPTH_MAX) {
            if(node_idx < NODE_TH) {
                path_depth++;
            }
	}
#endif

	if(node->depth == path_depth ||
           tb->jmp_from_index == 0) {
    	    //fprintf(stderr, "node pc: %p d=%d\n", node->tb->pc, node->depth);
            leaves[leaf_idx++] = node;
        } else {
            for(i = 0; i < tb->jmp_from_index; i++) {
                if(tb->jmp_from[i] != NULL) {
                    retr_node *child_node;
                    child_node = &nodes[node_idx++];
                    child_node->tb = tb->jmp_from[i];
                    child_node->next = node;
                    child_node->depth = node->depth + 1;
                    child_node->patch_num = tb->jmp_from_num[i];
                    travel_queue[queue_tail++] = child_node;
                } else {
                    //fprintf(stderr, "jmp_from err\n");
                    abort();
                }
            }
        }
    }
    //fprintf(stderr, "%d\n", node_idx);
    ABORT_IF((node_idx > PATH_DEPTH * LEAF_MAX), "retrans node overflow\n");

    /* put the travel result into retrans_stack */
    for(i = 0; i < leaf_idx; i++) {
        int pos;

        node = leaves[i];
        /* remove the unuse leaf */
        while(node != NULL && node->patch_num == CONT_TRANS_TAG)
            node = node->next;
        if(node == NULL) continue;

        path_count++;
	//fprintf(stderr, "depth=%d, index=%d\n", node->depth, retrans_stack_index);
        pos = retrans_stack_index + node->depth;
        retrans_stack_index = pos + 2;
        retrans_stack[pos + 1] = NULL;

	/* push TBs from the leaf */
        while(node->next != NULL) {
            retrans_stack[pos] = node->tb;
            retrans_stack_patch[pos]  = node->patch_num;
	    node = node->next;
            pos--;
        }
        retrans_stack[pos] = node->tb;
    }
    //fprintf(stderr, "exit get_retrans_tb\n");
    return (retrans_stack_index - path_count);
}