void bbGCStartup(){

#ifdef _WIN32
/*
	printf( "_bss_start__=%p\n",&_bss_start__ );
	printf( "_bss_end__=%p\n",&_bss_end__ );
	printf( "_data_start__=%p\n",&_data_start__ );
	printf( "_data_end__=%p\n",&_data_end__ );
	printf( "_end__=%p\n",&_end__ );
	fflush( stdout );
*/
	DATA_START=&_data_start__;
	DATA_END=&_bss_end__;
#endif

#ifdef __APPLE__
	int *seg=getsegbyname( "__DATA" );
	DATA_START=(void**)seg[6];
	DATA_END=(void**)(seg[6]+seg[7]);
#endif

#ifdef __linux
	DATA_START=&__data_start;
	DATA_END=&_end;
#endif	
	
#ifdef DEBUG_GC
	printf( "DATA_START=%p, DATA_END=%p\n",DATA_START,DATA_END );fflush( stdout );
#endif
	
	void **r;
	n_global_vars=0;
	for( r=DATA_START;r!=DATA_END;++r ){
		void *p=*r;
		if( isGlobalVar( p ) ){
			++n_global_vars;
		}
	}
	
#ifdef DEBUG_GC
	printf( "Found %i global vars\n",n_global_vars );fflush( stdout );
#endif
	
	global_vars=(void***)malloc( n_global_vars*4 );
	int i=0;
	for( r=DATA_START;r!=DATA_END;++r ){
		void *p=*r;
		if( isGlobalVar( p ) ){
			global_vars[i++]=r;
		}
	}

}
Example #2
0
File: DCL.C Project: 8l/rose
/**
 * Const-qualify immutable objects
 *
 * \todo count assignments, if only one, report violation
 */
bool DCL00_C( const SgNode *node ) {
	const SgInitializedName *varName = isSgInitializedName(node);
	if (!varName)
		return false;

	/**
	 * Ignore variables generated by macros
	 */
	if ((varName->get_name().getString().substr(0,2) == "__")
	||  isCompilerGeneratedNode(node))
		return false;

	/**
	 * Ignore global variables
	 */
	if (isGlobalVar(varName))
		return false;

	/**
	 * Ignore variables that are already const, are function pointers, or are
	 * declared inside of a struct, enum, or as an argument to a function
	 */
	SgType *varType = varName->get_type();
	if (isConstType(varType)
	|| isConstType(varType->dereference())
	|| isConstType(varType->dereference()->dereference())
	|| isSgFunctionType(varType)
	|| isSgClassType(varType)
	|| findParentOfType(varName, SgCtorInitializerList)
	|| findParentOfType(varName, SgEnumDeclaration)
	|| findParentOfType(varName, SgClassDeclaration))
		return false;

	/**
	 * DCL13-C is a subset of this rule, figure out which rule we are dealing
	 * with here
	 */
	std::string ruleStr;
	std::string errStr;
	if (findParentOfType(varName, SgFunctionParameterList)) {
		/** ignore function prototypes, just worry about the definitions */
		const SgFunctionDeclaration *fnDecl = findParentOfType(varName, SgFunctionDeclaration);
		/**
		 * Disabling assertion due to C++ code
		 */
		if (!fnDecl)
			return false;
//		assert(fnDecl);
		if (!fnDecl->get_definition())
			return false;
		if (isSgPointerType(varName->get_type())
		||  isSgArrayType(varName->get_type())) {
			ruleStr = "DCL13-C";
			errStr = "Declare function parameters that are pointers to values not changed by the function as const: ";
		} else {
			return false;
		}
	} else {
		ruleStr = "DCL00-C";
		errStr = "Const-qualify immutable objects: ";
	}

	/**
	 * Ignore global variables or variables declared as extern
	 */
	const SgScopeStatement *varScope = varName->get_scope();
	if (isSgGlobal(varScope) || isExternVar(varName))
		return false;

	FOREACH_SUBNODE(varScope, nodes, i, V_SgVarRefExp) {
		const SgVarRefExp *iVar = isSgVarRefExp(*i);
		assert(iVar);
		if (getRefDecl(iVar) != varName)
			continue;

		const SgNode *parent = iVar->get_parent();
		while(isSgCastExp(parent)) {
			parent = parent->get_parent();
		}
		assert(parent);

		/**
		 * If the variable is written to or it's address is taken, we can no
		 * longer be sure it should be const, if it's a struct and gets
		 * dereferenced, who knows what's getting written there :/
		 */
		if (varWrittenTo(iVar)
		||  isSgArrowExp(parent)
		||  findParentOfType(iVar, SgAddressOfOp))
			return false;

		/**
		 * If the variable is a pointer or array, and we pass it to a function
		 * or as an argument to pointer arithmetic, or assign it's value
		 * somewhere, we can longer be sure it should be const
		 */
		if ((isSgPointerType(varType) || isSgArrayType(varType))
		&& (findParentOfType(iVar, SgFunctionCallExp)
			|| isSgAddOp(parent)
			|| isSgSubtractOp(parent)
			|| isSgAssignOp(parent)
			|| isSgPntrArrRefExp(parent)
			|| isSgPointerDerefExp(parent)
			|| isSgAssignInitializer(parent)))
			return false;
	}

	const std::string msg =  errStr + varName->get_name().getString();
	print_error(node, ruleStr.c_str(), msg.c_str(), true);
	return true;
}