void ring_vm_loadfuncp ( VM *pVM ) { List *pList ; pVM->nFuncExecute++ ; pVM->nFuncExecute2++ ; pList = ring_list_newlist(pVM->pFuncCallList); ring_list_addint(pList,RING_VM_IR_READIVALUE(3)); ring_list_addstring(pList,RING_VM_IR_READC); ring_list_addint(pList,RING_VM_IR_READIVALUE(2)); ring_list_addint(pList,pVM->nSP); ring_list_newlist(pList); ring_list_addpointer(pList,pVM->cFileName); pVM->cFileName = (char *) RING_VM_IR_READPVALUE(4) ; ring_list_addint(pList,RING_VM_IR_READIVALUE(5)); ring_vm_saveloadaddressscope(pVM); }
void ring_vm_try ( VM *pVM ) { List *pList ; pList = ring_list_newlist(pVM->pTry); ring_list_addint(pList,RING_VM_IR_READI); ring_vm_savestate(pVM,pList); pVM->nActiveCatch = 0 ; }
void ring_vm_newscope ( VM *pVM ) { pVM->pActiveMem = ring_list_newlist(pVM->pMem); /* Save Local Scope Information */ pVM->nScopeID++ ; ring_list_addint(pVM->aScopeID,pVM->nScopeID); pVM->nActiveScopeID = pVM->nScopeID ; }
void ring_parser_icg_newoperandint ( Parser *pParser , int nValue ) { assert(pParser != NULL); assert(pParser->ActiveGenCodeList); ring_list_addint(pParser->ActiveGenCodeList,nValue); #if RING_SHOWIC printf( " Operand : %d ",nValue ) ; #endif }
void ring_parser_icg_insertoperation ( Parser *pParser , int nPos , IC_OPERATIONS opcode ) { assert(pParser != NULL); pParser->ActiveGenCodeList = ring_list_insertlist(pParser->GenCode,nPos); ring_list_addint(pParser->ActiveGenCodeList,opcode); #if RING_SHOWIC printf( "\n %6d [ %s ] ",nPos, RING_IC_OP[opcode] ) ; #endif }
void ring_murmurhash3_x64_128(void *pPointer) { char *key = NULL; int keylen; int seed = 0; uint64_t out[2]; int ret_type = 0; List *tmp_list, *ret_val; if (RING_API_PARACOUNT < 2 || RING_API_PARACOUNT > 3) { RING_API_ERROR(RING_API_MISS2PARA); return ; } if (!RING_API_ISSTRING(1)) { RING_API_ERROR("murmurhash3_x64_128 expects the first parameter to be a string"); return; } if (!RING_API_ISNUMBER(2)) { RING_API_ERROR("murmurhash3_x64_128 expects the first parameter to be an integer"); return; } key = RING_API_GETSTRING(1); keylen = strlen(key); seed = RING_API_GETNUMBER(2); if (RING_API_PARACOUNT == 3) { if (RING_API_ISNUMBER(3)) { ret_type = RING_API_GETNUMBER(3); if (!is_bool(ret_type)) { RING_API_ERROR("Third parameter should be boolean value\n"); } } else { RING_API_ERROR("murmurhash3_x64_128 expects the third parameter to be an integer\n"); } } MurmurHash3_x64_128(key, keylen, seed, out); ret_val = RING_API_NEWLIST; tmp_list = ring_list_newlist_gc(((VM *)pPointer)->pRingState, ret_val); for (int i = 0; i < 2; i++) { if (ret_type) { char tmp[50]; LONG2HEX(tmp, out[i]); ring_list_addstring2(tmp_list, (char*) tmp, strlen((char *) tmp)); } else { ring_list_addint(tmp_list, out[i]); } } RING_API_RETLIST(ret_val); }
List * ring_vm_newvar2 ( const char *cStr,List *pParent ) { List *pList ; /* This function is called by all of the other functions that create new varaibles */ pList = ring_list_newlist(pParent); ring_list_addstring(pList,cStr); ring_list_addint(pList,RING_VM_NULL); ring_list_addstring(pList,"NULL"); /* Pointer Type */ ring_list_addint(pList,0); /* Private Flag */ ring_list_addint(pList,0); /* Add Pointer to the HashTable */ if ( pParent->pHashTable == NULL ) { pParent->pHashTable = ring_hashtable_new(); } ring_hashtable_newpointer(pParent->pHashTable,cStr,pList); return pList ; }
void ring_vm_jumpvarlenum ( VM *pVM ) { List *pVar ; double nNum1,nNum2 ; if ( ring_vm_findvar(pVM, RING_VM_IR_READC ) == 0 ) { ring_vm_newvar(pVM, RING_VM_IR_READC); } nNum1 = ring_list_getdouble(pVM->aForStep,ring_list_getsize(pVM->aForStep)); /* Change Instruction for Performance */ if ( pVM->nVarScope == RING_VARSCOPE_GLOBAL ) { /* Replace JumpVarLENum with JumpVarPLENum for better performance */ if ( nNum1 == 1.0 ) { RING_VM_IR_OPCODE = ICO_JUMPVARPLENUMSTEP1 ; } else { RING_VM_IR_OPCODE = ICO_JUMPVARPLENUM ; } ring_item_setpointer(RING_VM_IR_ITEM(1),RING_VM_STACK_READP); } else if ( pVM->nVarScope == RING_VARSCOPE_LOCAL ) { /* Replace JumpVarLENum with JumpVarLPLENum for better performance */ RING_VM_IR_OPCODE = ICO_JUMPVARLPLENUM ; ring_item_setpointer(RING_VM_IR_ITEM(4),RING_VM_STACK_READP); ring_item_setint(RING_VM_IR_ITEM(5),ring_list_getint(pVM->aScopeID,ring_list_getsize(pVM->aScopeID))); #if RING_SHOWICFINAL RING_VM_IR_PARACOUNT = RING_VM_IR_PARACOUNT + 2 ; ring_list_addpointer(RING_VM_IR_LIST,RING_VM_STACK_READP); ring_list_addint(RING_VM_IR_LIST,ring_list_getint(pVM->aScopeID,ring_list_getsize(pVM->aScopeID))); #endif } pVar = (List *) RING_VM_STACK_READP ; RING_VM_STACK_POP ; /* Check Data */ if ( ring_list_isstring(pVar,RING_VAR_VALUE) ) { nNum2 = ring_vm_stringtonum(pVM,ring_list_getstring(pVar,RING_VAR_VALUE)); ring_list_setdouble(pVar,RING_VAR_VALUE,nNum2); } else if ( ring_list_isnumber(pVar,RING_VAR_VALUE) ) { nNum2 = ring_list_getdouble(pVar,RING_VAR_VALUE) ; } else { ring_vm_error(pVM,RING_VM_ERROR_FORLOOPDATATYPE); return ; } if ( nNum1 < 0 ) { if ( ! (nNum2 >= RING_VM_IR_READDVALUE(2)) ) { /* Jump */ pVM->nPC = RING_VM_IR_READIVALUE(3) ; } } else { if ( ! (nNum2 <= RING_VM_IR_READDVALUE(2)) ) { /* Jump */ pVM->nPC = RING_VM_IR_READIVALUE(3) ; } } }
void ring_vm_addnewcpointervar ( VM *pVM,const char *cStr,void *pPointer,const char *cStr2 ) { List *pList, *pList2 ; pList = ring_vm_newvar2(cStr,pVM->pActiveMem); ring_list_setint(pList,RING_VAR_TYPE,RING_VM_LIST); ring_list_setlist(pList,RING_VAR_VALUE); pList2 = ring_list_getlist(pList,RING_VAR_VALUE); /* Add Pointer */ ring_list_addpointer(pList2,pPointer); /* Add Type */ ring_list_addstring(pList2,cStr2); /* Add Status Number */ ring_list_addint(pList2,RING_CPOINTERSTATUS_NOTCOPIED); }
void ring_parser_icg_newoperation ( Parser *pParser , IC_OPERATIONS opcode ) { assert(pParser != NULL); if ( pParser->nInsertFlag == 1 ) { ring_parser_icg_insertoperation(pParser,pParser->nInsertCounter,opcode); pParser->nInsertCounter++ ; return ; } pParser->ActiveGenCodeList = ring_list_newlist(pParser->GenCode); ring_list_addint(pParser->ActiveGenCodeList,opcode); #if RING_SHOWIC printf( "\n %6d [ %s ] ",ring_list_getsize(pParser->GenCode) , RING_IC_OP[opcode] ) ; #endif }
void ring_vm_newvar ( VM *pVM,const char *cStr ) { List *pList ; assert(pVM->pActiveMem); pList = ring_vm_newvar2(cStr,pVM->pActiveMem); pVM->nSP++ ; RING_VM_STACK_SETPVALUE(pList); RING_VM_STACK_OBJTYPE = RING_OBJTYPE_VARIABLE ; /* Set the scope of the new variable */ if ( (ring_list_getsize(pVM->pMem) == 1) && (pVM->pActiveMem == ring_list_getlist(pVM->pMem,RING_MEMORY_GLOBALSCOPE)) ) { pVM->nVarScope = RING_VARSCOPE_GLOBAL ; } else if ( pVM->pActiveMem == ring_list_getlist(pVM->pMem,ring_list_getsize(pVM->pMem)) ) { pVM->nVarScope = RING_VARSCOPE_LOCAL ; } else { pVM->nVarScope = RING_VARSCOPE_NOTHING ; } /* Add Scope to aLoadAddressScope */ ring_list_addint(pVM->aLoadAddressScope,pVM->nVarScope); }
int ring_objfile_readfile ( const char *cFileName,RingState *pRingState ) { FILE *fObj; signed char c ; List *pListFunctions, *pListClasses, *pListPackages, *pListCode, *pList, *pListStack ; int nActiveList,nValue,nBraceEnd ; double dValue ; char *cString ; char cKey[11] ; char cFileType[100] ; strcpy(cKey,"ringstring"); /* Create Lists */ pListFunctions = ring_list_new(0); pListClasses = ring_list_new(0); pListPackages = ring_list_new(0); pListCode = ring_list_new(0); pListStack = ring_list_new(0); pList = NULL ; /* Set Active List (1=functions 2=classes 3=packages 4=code) */ nActiveList = 0 ; nBraceEnd = 0 ; /* Open File */ fObj = fopen(cFileName , "rb" ); if ( fObj==NULL ) { printf( "Can't open file %s \n ",cFileName ) ; return 0 ; } fread( cFileType , 1 , 18 , fObj ); cFileType[18] = '\0' ; if ( strcmp(cFileType,"# Ring Object File") != 0 ) { printf( "The file type is not correct - the VM expect a ring object file\n" ) ; return 0 ; } c = getc(fObj); fread( cFileType , 1 , 13 , fObj ); cFileType[13] = '\0' ; if ( strcmp(cFileType,"# Version 1.1") != 0 ) { printf( "The file version is not correct - the VM expect a ring object file version 1.1\n" ) ; return 0 ; } /* Process File */ c = getc(fObj); while ( c != EOF ) { /* Check Char */ switch ( c ) { case '#' : /* Read Line */ while ( c != '\n' ) { c = getc(fObj); #ifdef DEBUG_OBJFILE printf( "%c ",c ) ; #endif } #ifdef DEBUG_OBJFILE puts("Read Comment ! "); #endif break ; case '{' : nActiveList++ ; switch ( nActiveList ) { case 1 : pList = pListFunctions ; break ; case 2 : pList = pListClasses ; break ; case 3 : pList = pListPackages ; break ; case 4 : pList = pListCode ; break ; } break ; case '[' : c = getc(fObj); switch ( c ) { case 'S' : c = getc(fObj); fscanf( fObj , "[%d]" , &nValue ) ; cString = (char *) malloc(nValue+1) ; fread( cString , 1 , nValue , fObj ); cString[nValue] = '\0' ; /* Decrypt String */ ring_objfile_xorstring(cString,nValue,cKey,10); ring_list_addstring2(pList,cString,nValue); free( cString ) ; #ifdef DEBUG_OBJFILE printf( "Read String %s Size %d \n",cString,nValue ) ; #endif break ; case 'I' : c = getc(fObj); fscanf( fObj , "%d" , &nValue ) ; ring_list_addint(pList,nValue); #ifdef DEBUG_OBJFILE printf( "Read Number %d \n ",nValue ) ; #endif break ; case 'D' : c = getc(fObj); fscanf( fObj , "%lf" , &dValue ) ; ring_list_adddouble(pList,dValue); #ifdef DEBUG_OBJFILE printf( "Read Double %d \n",dValue ) ; #endif break ; case 'P' : ring_list_addpointer(pList,NULL); /* Read Line */ while ( c != '\n' ) { c = getc(fObj); } #ifdef DEBUG_OBJFILE puts("Read Pointer "); #endif break ; case 'T' : ring_list_addpointer(pListStack,pList); pList = ring_list_newlist(pList); /* Read Line */ while ( c != '\n' ) { c = getc(fObj); } #ifdef DEBUG_OBJFILE puts("Read T "); #endif break ; case 'E' : pList = (List *) ring_list_getpointer(pListStack,ring_list_getsize(pListStack)) ; ring_list_deletelastitem(pListStack); /* Read Line */ while ( c != '\n' ) { c = getc(fObj); } #ifdef DEBUG_OBJFILE puts("Read E "); #endif break ; case 'L' : /* Read Until { */ while ( c != '{' ) { c = getc(fObj); } ring_list_addpointer(pListStack,pList); pList = ring_list_newlist(pList); nBraceEnd = 1 ; #ifdef DEBUG_OBJFILE puts("Read L "); #endif break ; } break ; case '}' : if ( nBraceEnd ) { pList = (List *) ring_list_getpointer(pListStack,ring_list_getsize(pListStack)) ; ring_list_deletelastitem(pListStack); nBraceEnd = 0 ; #ifdef DEBUG_OBJFILE puts("Read } "); #endif } break ; } c = getc(fObj); } /* Close File */ fclose( fObj ) ; ring_list_delete(pListStack); /* Update Ring State */ #ifdef DEBUG_OBJFILE puts("Old Code List "); ring_list_print(pRingState->pRingGenCode); #endif /* Update Lists */ pRingState->pRingFunctionsMap = pListFunctions ; pRingState->pRingClassesMap = pListClasses ; pRingState->pRingPackagesMap = pListPackages ; pRingState->pRingGenCode = pListCode ; #ifdef DEBUG_OBJFILE puts("Update Done! "); puts("New Code List "); ring_list_print(pRingState->pRingGenCode); #endif /* Update Classes Pointers */ ring_objfile_updateclassespointers(pRingState); return 1 ; }
void ring_parser_icg_addoperandint ( List *pList , int nValue ) { assert(pList != NULL); ring_list_addint(pList,nValue); }
int ring_parser_class ( Parser *pParser ) { List *pList,*pList2,*pList3 ; int x ; String *pString ; /* Statement --> Class Identifier [ From Identifier ] */ if ( ring_parser_iskeyword(pParser,K_CLASS) ) { ring_parser_nexttoken(pParser); if ( ring_parser_isidentifier(pParser) ) { /* ** Generate Code ** Return NULL */ ring_parser_icg_newoperation(pParser,ICO_RETNULL); ring_parser_icg_newoperation(pParser,ICO_NEWCLASS); ring_parser_icg_newoperand(pParser,pParser->TokenText); /* Add Class to Classes Table */ pList = pParser->ClassesMap ; pList = ring_list_newlist(pList); ring_list_addstring(pList,pParser->TokenText); ring_list_addint(pList,ring_list_getsize(pParser->GenCode)); /* Add class pointer to generated code */ ring_parser_icg_newoperandpointer(pParser,pList); ring_parser_nexttoken(pParser); /* [From Identifer] */ if ( ring_parser_iskeyword(pParser,K_FROM) ) { ring_parser_nexttoken(pParser); if ( ring_parser_namedotname(pParser) ) { /* Generate Code */ pList3 = ring_parser_icg_getactiveoperation(pParser); /* Check if parent class name = subclass name */ if ( strcmp(ring_list_getstring(pList,1),ring_list_getstring(pList3,4)) == 0 ) { ring_parser_error(pParser,RING_PARSER_ERROR_PARENTLIKESUBCLASS); return 0 ; } /* Set Parent Class Name in Classes Map */ ring_list_addstring(pList,ring_list_getstring(pList3,4)); #if RING_PARSERTRACE RING_STATE_CHECKPRINTRULES puts("Rule : Statement --> 'Class' Identifier 'From' [PackageName'.']Identifier"); #endif } else { ring_parser_error(pParser,RING_PARSER_ERROR_PRENTCLASSNAME); return 0 ; } } else { /* Set Parent Class Name In Classes Map */ ring_list_addstring(pList,""); #if RING_PARSERTRACE RING_STATE_CHECKPRINTRULES puts("Rule : Statement --> 'Class' Identifier "); #endif } /* Add Method/Functions List to Class in Class Table */ pList2 = ring_list_newlist(pList); /* Add Flag ( IS Parent Class information collected ) */ ring_list_addint(pList,0); /* Set Active Functions List to be Class Methods */ pParser->FunctionsMap = pList2 ; /* Make class visible using PackageName.ClassName if we have package */ if ( pParser->ClassesMap != pParser->pRingState->pRingClassesMap ) { /* Get Package Name */ pList3 = ring_list_getlist(pParser->pRingState->pRingPackagesMap,ring_list_getsize(pParser->pRingState->pRingPackagesMap)); pString = ring_string_new(ring_list_getstring(pList3,1)); /* Add pointer to the Package in the Class List */ ring_list_addpointer(pList,pList3); /* Add List point to General Classes point to the class in the package */ pList2 = ring_list_newlist(pParser->pRingState->pRingClassesMap); ring_list_addstring(pList2,""); ring_list_addpointer(pList2,pList); /* Ignore Adding Pointer to File Name */ ring_list_addpointer(pList2,NULL); /* Add Class Name to Package Name */ ring_string_add(pString,"."); ring_string_add(pString,ring_list_getstring(pList,1)); ring_list_setstring(pList2,1,ring_string_get(pString)); ring_string_delete(pString); } else { /* Add pointer to the Package in the Class List */ ring_list_addpointer(pList,NULL); } pParser->nClassStart = 1 ; /* Create label to be used by Private */ pParser->nClassMark = ring_parser_icg_newlabel2(pParser); pParser->nPrivateFlag = 0 ; return 1 ; } else { ring_parser_error(pParser,RING_PARSER_ERROR_CLASSNAME); return 0 ; } } /* Statement --> Func Identifier [PARALIST] */ if ( ring_parser_iskeyword(pParser,K_FUNC) ) { ring_parser_nexttoken(pParser); if ( ring_parser_isidentifier(pParser) ) { /* ** Generate Code ** Return NULL */ ring_parser_icg_newoperation(pParser,ICO_RETNULL); ring_parser_icg_newoperation(pParser,ICO_NEWFUNC); ring_parser_icg_newoperand(pParser,pParser->TokenText); /* Add function to Functions Table */ pList2 = pParser->FunctionsMap ; /* Check Function Redefinition */ if ( ring_list_getsize(pList2) > 0 ) { for ( x = 1 ; x <= ring_list_getsize(pList2) ; x++ ) { if ( strcmp(ring_list_getstring(ring_list_getlist(pList2,x),1),pParser->TokenText) == 0 ) { ring_parser_error(pParser,RING_PARSER_ERROR_FUNCREDEFINE); return 0 ; } } } pList2 = ring_list_newlist(pList2); ring_list_addstring(pList2,pParser->TokenText); ring_list_addint(pList2,ring_list_getsize(pParser->GenCode)); ring_list_addstring(pList2,ring_list_getstring(pParser->pRingState->pRingFilesStack,ring_list_getsize(pParser->pRingState->pRingFilesStack))); if ( pParser->nClassStart == 1 ) { ring_list_addint(pList2,pParser->nPrivateFlag); } else { ring_list_addint(pList2,0); } ring_parser_nexttoken(pParser); if ( ring_parser_isidentifier(pParser) ) { x = ring_parser_paralist(pParser); } else { x = 1 ; } #if RING_PARSERTRACE if ( x == 1 ) { RING_STATE_CHECKPRINTRULES puts("Rule : Statement --> 'Func' Identifier [ParaList]"); } #endif return x ; } else { ring_parser_error(pParser,RING_PARSER_ERROR_FUNCNAME); return 0 ; } } /* Statement --> Package Identifier { '.' Identifier } */ if ( ring_parser_iskeyword(pParser,K_PACKAGE) ) { ring_parser_nexttoken(pParser); /* Generate Code */ ring_parser_icg_newoperation(pParser,ICO_PACKAGE); #if RING_PARSERTRACE RING_STATE_CHECKPRINTRULES puts("Rule : Statement --> 'Package' Identifier{'.'identifier}"); #endif if ( ring_parser_namedotname(pParser) ) { /* Add Package to Packages List */ pList = ring_parser_icg_getactiveoperation(pParser); /* Check early definition of the package */ for ( x = 1 ; x <= ring_list_getsize(pParser->pRingState->pRingPackagesMap) ; x++ ) { pList3 = ring_list_getlist(pParser->pRingState->pRingPackagesMap,x); if ( strcmp(ring_list_getstring(pList3,1),ring_list_getstring(pList,2)) == 0 ) { pParser->ClassesMap = ring_list_getlist(pList3,2); return 1 ; } } pList2 = ring_list_newlist(pParser->pRingState->pRingPackagesMap); /* Add Package Name */ ring_list_addstring(pList2,ring_list_getstring(pList,2)); /* Add Package Classes List */ pParser->ClassesMap = ring_list_newlist(pList2); return 1 ; } else { return 0 ; } } /* Statement --> Import Identifier { '.' Identifier } */ if ( ring_parser_iskeyword(pParser,K_IMPORT) ) { ring_parser_nexttoken(pParser); /* Generate Code */ ring_parser_icg_newoperation(pParser,ICO_IMPORT); #if RING_PARSERTRACE RING_STATE_CHECKPRINTRULES puts("Rule : Statement --> 'Import' Identifier{'.'identifier}"); #endif return ring_parser_namedotname(pParser) ; } /* Statement --> Private */ if ( ring_parser_iskeyword(pParser,K_PRIVATE) ) { ring_parser_nexttoken(pParser); if ( pParser->nClassStart == 1 ) { /* Generate Code */ ring_parser_icg_newoperation(pParser,ICO_RETNULL); /* Change Label After Class to BlockFlag to Jump to Private */ pList = ring_parser_icg_getoperationlist(pParser,pParser->nClassMark); ring_list_setint(pList,1,ICO_BLOCKFLAG); ring_list_addint(pList,ring_parser_icg_newlabel(pParser)); ring_parser_icg_newoperation(pParser,ICO_PRIVATE); #if RING_PARSERTRACE RING_STATE_CHECKPRINTRULES puts("Rule : Statement --> 'Private'"); #endif pParser->nPrivateFlag = 1 ; return 1 ; } else { ring_parser_error(pParser,RING_PARSER_ERROR_NOCLASSDEFINED); return 0 ; } } return ring_parser_stmt(pParser) ; }