/* The function appends the given list of vectors to the variable set of the current dataset. */ void dataset::appendVariables (vector * v) { vector * next; for (vector * t = v; t != NULL; t = next) { next = (vector *) t->getNext (); appendVariable (t); } }
/** * Creates an expression wrapping an identifier */ struct memorycontainer* createIdentifierExpression(char * identifier) { struct memorycontainer* memoryContainer = (struct memorycontainer*) malloc(sizeof(struct memorycontainer)); if (doesVariableExist(identifier)) { memoryContainer->length=sizeof(unsigned char)+sizeof(unsigned short); memoryContainer->data=(char*) malloc(memoryContainer->length); memoryContainer->lineDefns=NULL; int location=appendStatement(memoryContainer, IDENTIFIER_TOKEN, 0); appendVariable(memoryContainer, getVariableId(identifier, 0), location); } else { memoryContainer->length=sizeof(unsigned char)+sizeof(unsigned short); memoryContainer->data=(char*) malloc(memoryContainer->length); appendStatement(memoryContainer, FN_ADDR_TOKEN, 0); struct lineDefinition * defn = (struct lineDefinition*) malloc(sizeof(struct lineDefinition)); defn->next=NULL; defn->type=4; defn->linenumber=line_num; defn->name=(char*) malloc(strlen(identifier)+1); strcpy(defn->name, identifier); defn->currentpoint=sizeof(unsigned char); memoryContainer->lineDefns=defn; if (currentCall==NULL) { mainCodeCallTree.calledFunctions[mainCodeCallTree.number_of_calls]=(char*)malloc(strlen(identifier)+1); strcpy(mainCodeCallTree.calledFunctions[mainCodeCallTree.number_of_calls++], identifier); } else { currentCall->calledFunctions[currentCall->number_of_calls]=(char*)malloc(strlen(identifier)+1); strcpy(currentCall->calledFunctions[currentCall->number_of_calls++], identifier); } } return memoryContainer; }
/** * Appends and returns a for iteration. This puts in the assignment of the initial value to the variable, the normal stuff and then * termination check at each iteration along with jumping to next iteration if applicable */ struct memorycontainer* appendForStatement(char * identifier, struct memorycontainer* exp, struct memorycontainer* block) { struct memorycontainer* initialLet=appendLetStatement("epy_i_ctr", createIntegerExpression(0)); struct memorycontainer* variantLet=appendLetStatement(identifier, createIntegerExpression(0)); struct memorycontainer* incrementLet=appendLetStatement("epy_i_ctr", createAddExpression(createIdentifierExpression("epy_i_ctr"), createIntegerExpression(1))); struct memorycontainer* memoryContainer = (struct memorycontainer*) malloc(sizeof(struct memorycontainer)); memoryContainer->length=sizeof(unsigned char)*2+sizeof(unsigned short) * 4 + exp->length + (block != NULL ? block->length : 0) + initialLet->length + variantLet->length + incrementLet->length; memoryContainer->data=(char*) malloc(memoryContainer->length); memoryContainer->lineDefns=NULL; struct memorycontainer* combinedBlock=block != NULL ? concatenateMemory(block, incrementLet) : incrementLet; unsigned int position=0; struct lineDefinition * defn = (struct lineDefinition*) malloc(sizeof(struct lineDefinition)); defn->next=memoryContainer->lineDefns; defn->type=0; defn->linenumber=currentForLine; defn->currentpoint=initialLet->length; memoryContainer->lineDefns=defn; position=appendMemory(memoryContainer, initialLet, position); position=appendMemory(memoryContainer, variantLet, position); position=appendStatement(memoryContainer, FOR_TOKEN, position); position=appendVariable(memoryContainer, getVariableId("epy_i_ctr", 0), position); position=appendVariable(memoryContainer, getVariableId(identifier, 0), position); position=appendMemory(memoryContainer, exp, position); unsigned short length=(unsigned short) combinedBlock->length; memcpy(&memoryContainer->data[position], &length, sizeof(unsigned short)); position+=sizeof(unsigned short); position=appendMemory(memoryContainer, combinedBlock, position); position=appendStatement(memoryContainer, GOTO_TOKEN, position); defn = (struct lineDefinition*) malloc(sizeof(struct lineDefinition)); defn->next=memoryContainer->lineDefns; defn->type=1; defn->linenumber=currentForLine; defn->currentpoint=position; memoryContainer->lineDefns=defn; currentForLine--; return memoryContainer; }
static struct memorycontainer* appendLetIfNoAliasStatement(char * identifier, struct memorycontainer* expressionContainer) { struct memorycontainer* memoryContainer = (struct memorycontainer*) malloc(sizeof(struct memorycontainer)); memoryContainer->length=sizeof(unsigned char)+sizeof(unsigned short) + expressionContainer->length; memoryContainer->data=(char*) malloc(memoryContainer->length); memoryContainer->lineDefns=NULL; unsigned int position=0; position=appendStatement(memoryContainer, LETNOALIAS_TOKEN, position); position=appendVariable(memoryContainer, getVariableId(identifier, 1), position); appendMemory(memoryContainer, expressionContainer, position); return memoryContainer; }
/** * Creates an expression wrapping an identifier array access */ struct memorycontainer* createIdentifierArrayAccessExpression(char* identifier, struct stack_t* index_expressions) { int lenOfIndexes=getStackSize(index_expressions); struct memorycontainer* memoryContainer = (struct memorycontainer*) malloc(sizeof(struct memorycontainer)); memoryContainer->length=sizeof(unsigned short)+(sizeof(unsigned char)*2); memoryContainer->data=(char*) malloc(memoryContainer->length); memoryContainer->lineDefns=NULL; unsigned int position=0; position=appendStatement(memoryContainer, ARRAYACCESS_TOKEN, position); position=appendVariable(memoryContainer, getVariableId(identifier, 1), position); unsigned char packageNumDims=(unsigned char) lenOfIndexes; memcpy(&memoryContainer->data[position], &packageNumDims, sizeof(unsigned char)); position+=sizeof(unsigned char); int i; for (i=0;i<lenOfIndexes;i++) { memoryContainer=concatenateMemory(memoryContainer, getExpressionAt(index_expressions, i)); } return memoryContainer; }
/** * Appends and returns the setting of an array element (assignment) statement */ struct memorycontainer* appendArraySetStatement( char* identifier, struct stack_t* indexContainer, struct memorycontainer* expressionContainer) { struct memorycontainer* memoryContainer = (struct memorycontainer*) malloc(sizeof(struct memorycontainer)); memoryContainer->length=(sizeof(unsigned char)*2)+sizeof(unsigned short); memoryContainer->data=(char*) malloc(memoryContainer->length); memoryContainer->lineDefns=NULL; unsigned int position=0; position=appendStatement(memoryContainer, ARRAYSET_TOKEN, position); position=appendVariable(memoryContainer, getVariableId(identifier, 1), position); unsigned char numIndexes=(unsigned char) getStackSize(indexContainer); memcpy(&memoryContainer->data[position], &numIndexes, sizeof(unsigned char)); position+=sizeof(unsigned char); int i; for (i=0;i<numIndexes;i++) { memoryContainer=concatenateMemory(memoryContainer, getExpressionAt(indexContainer, i)); } memoryContainer=concatenateMemory(memoryContainer, expressionContainer); return memoryContainer; }
/** * Appends and returns a call function, this is added as a placeholder and then resolved at the end to point to the absolute byte code location * which is needed as the function might appear at any point */ struct memorycontainer* appendCallFunctionStatement(char* functionName, struct stack_t* args) { char * last_dot=strrchr(functionName,'.'); if (last_dot != NULL) functionName=last_dot+1; if (currentFunctionName != NULL && strcmp(currentFunctionName, functionName) == 0) isFnRecursive=1; struct memorycontainer* assignmentContainer=NULL; unsigned short numArgs=(unsigned short) getStackSize(args); char *isArgIdentifier=(char*) malloc(numArgs); unsigned short *varIds=(unsigned short*) malloc(sizeof(unsigned short) * numArgs); char * varname=(char*) malloc(strlen(functionName)+5); int i; for (i=0;i<numArgs;i++) { struct memorycontainer* expression=getExpressionAt(args, i); unsigned char command=((unsigned char*) expression->data)[0]; if (command != IDENTIFIER_TOKEN) { isArgIdentifier[i]=0; sprintf(varname,"%s#%d", functionName, i); if (assignmentContainer == NULL) { assignmentContainer=appendLetStatement(varname, getExpressionAt(args, i)); } else { assignmentContainer=concatenateMemory(assignmentContainer, appendLetStatement(varname, getExpressionAt(args, i))); } } else { isArgIdentifier[i]=1; varIds[i]=*((unsigned short*) (&((char*) expression->data)[1])); free(expression->data); } } struct memorycontainer* memoryContainer = (struct memorycontainer*) malloc(sizeof(struct memorycontainer)); memoryContainer->length=sizeof(unsigned short)*(2+numArgs)+sizeof(unsigned char); memoryContainer->data=(char*) malloc(memoryContainer->length); unsigned int position=0; if (doesVariableExist(functionName)) { memoryContainer->lineDefns=NULL; position=appendStatement(memoryContainer, FNCALL_BY_VAR_TOKEN, position); position=appendVariable(memoryContainer, getVariableId(functionName, 0), position); } else { struct lineDefinition * defn = (struct lineDefinition*) malloc(sizeof(struct lineDefinition)); defn->next=NULL; defn->type=3; defn->linenumber=line_num; defn->name=(char*) malloc(strlen(functionName)+1); strcpy(defn->name, functionName); defn->currentpoint=sizeof(unsigned char); memoryContainer->lineDefns=defn; position=appendStatement(memoryContainer, FNCALL_TOKEN, position); position+=sizeof(unsigned short); } position=appendVariable(memoryContainer, numArgs, position); for (i=0;i<numArgs;i++) { if (isArgIdentifier[i]) { position=appendVariable(memoryContainer, varIds[i], position); } else { sprintf(varname,"%s#%d", functionName, i); position=appendVariable(memoryContainer, getVariableId(varname, 0), position); } } clearStack(args); free(varname); free(isArgIdentifier); free(varIds); if (currentCall==NULL) { mainCodeCallTree.calledFunctions[mainCodeCallTree.number_of_calls]=(char*)malloc(strlen(functionName)+1); strcpy(mainCodeCallTree.calledFunctions[mainCodeCallTree.number_of_calls++], functionName); } else { currentCall->calledFunctions[currentCall->number_of_calls]=(char*)malloc(strlen(functionName)+1); strcpy(currentCall->calledFunctions[currentCall->number_of_calls++], functionName); } if (assignmentContainer != NULL) { return concatenateMemory(assignmentContainer, memoryContainer); } else { return memoryContainer; } }
struct memorycontainer* appendNativeCallFunctionStatement(char* functionName, struct stack_t* args, struct memorycontainer* singleArg) { struct memorycontainer* memoryContainer = (struct memorycontainer*) malloc(sizeof(struct memorycontainer)); memoryContainer->length=(sizeof(unsigned char)*2) + sizeof(unsigned short); memoryContainer->data=(char*) malloc(memoryContainer->length); memoryContainer->lineDefns=NULL; unsigned int position=0; position=appendStatement(memoryContainer, NATIVE_TOKEN, position); if (strcmp(functionName, NATIVE_RTL_ISHOST_STR)==0) { position=appendStatement(memoryContainer, NATIVE_FN_RTL_ISHOST, position); } else if (strcmp(functionName, NATIVE_RTL_ISDEVICE_STR)==0) { position=appendStatement(memoryContainer, NATIVE_FN_RTL_ISDEVICE, position); } else if (strcmp(functionName, NATIVE_RTL_PRINT_STR)==0) { position=appendStatement(memoryContainer, NATIVE_FN_RTL_PRINT, position); } else if (strcmp(functionName, NATIVE_RTL_NUMDIMS_STR)==0) { position=appendStatement(memoryContainer, NATIVE_FN_RTL_NUMDIMS, position); } else if (strcmp(functionName, NATIVE_RTL_DSIZE_STR)==0) { position=appendStatement(memoryContainer, NATIVE_FN_RTL_DSIZE, position); } else if (strcmp(functionName, NATIVE_RTL_INPUT_STR)==0) { position=appendStatement(memoryContainer, NATIVE_FN_RTL_INPUT, position); } else if (strcmp(functionName, NATIVE_RTL_INPUTPRINT_STR)==0) { position=appendStatement(memoryContainer, NATIVE_FN_RTL_INPUTPRINT, position); } else if (strcmp(functionName, NATIVE_RTL_SYNC_STR)==0) { position=appendStatement(memoryContainer, NATIVE_FN_RTL_SYNC, position); } else if (strcmp(functionName, NATIVE_RTL_GC_STR)==0) { position=appendStatement(memoryContainer, NATIVE_FN_RTL_GC, position); } else if (strcmp(functionName, NATIVE_RTL_FREE_STR)==0) { position=appendStatement(memoryContainer, NATIVE_FN_RTL_FREE, position); } else if (strcmp(functionName, NATIVE_RTL_SEND_STR)==0) { position=appendStatement(memoryContainer, NATIVE_FN_RTL_SEND, position); } else if (strcmp(functionName, NATIVE_RTL_RECV_STR)==0) { position=appendStatement(memoryContainer, NATIVE_FN_RTL_RECV, position); } else if (strcmp(functionName, NATIVE_RTL_SENDRECV_STR)==0) { position=appendStatement(memoryContainer, NATIVE_FN_RTL_SENDRECV, position); } else if (strcmp(functionName, NATIVE_RTL_BCAST_STR)==0) { position=appendStatement(memoryContainer, NATIVE_FN_RTL_BCAST, position); } else if (strcmp(functionName, NATIVE_RTL_NUMCORES_STR)==0) { position=appendStatement(memoryContainer, NATIVE_FN_RTL_NUMCORES, position); } else if (strcmp(functionName, NATIVE_RTL_COREID_STR)==0) { position=appendStatement(memoryContainer, NATIVE_FN_RTL_COREID, position); } else if (strcmp(functionName, NATIVE_RTL_REDUCE_STR)==0) { position=appendStatement(memoryContainer, NATIVE_FN_RTL_REDUCE, position); } else if (strcmp(functionName, NATIVE_RTL_ALLOCATEARRAY_STR)==0) { position=appendStatement(memoryContainer, NATIVE_FN_RTL_ALLOCARRAY, position); } else if (strcmp(functionName, NATIVE_RTL_ALLOCATESHAREDARRAY_STR)==0) { position=appendStatement(memoryContainer, NATIVE_FN_RTL_ALLOCSHAREDARRAY, position); } else if (strcmp(functionName, NATIVE_RTL_MATH_STR)==0) { position=appendStatement(memoryContainer, NATIVE_FN_RTL_MATH, position); } else if (strcmp(functionName, NATIVE_RTL_PROBE_FOR_MESSAGE_STR)==0) { position=appendStatement(memoryContainer, NATIVE_FN_RTL_PROBE_FOR_MESSAGE, position); } else if (strcmp(functionName, NATIVE_RTL_TEST_FOR_SEND_STR)==0) { position=appendStatement(memoryContainer, NATIVE_FN_RTL_TEST_FOR_SEND, position); } else if (strcmp(functionName, NATIVE_RTL_WAIT_FOR_SEND_STR)==0) { position=appendStatement(memoryContainer, NATIVE_FN_RTL_WAIT_FOR_SEND, position); } else if (strcmp(functionName, NATIVE_RTL_SEND_NB_STR)==0) { position=appendStatement(memoryContainer, NATIVE_FN_RTL_SEND_NB, position); } else { fprintf(stderr, "Native function call of '%s' is not found\n", functionName); exit(EXIT_FAILURE); } unsigned short numArgs=args !=NULL ? (unsigned short) getStackSize(args) : singleArg != NULL ? 1 : 0; position=appendVariable(memoryContainer, numArgs, position); if (args != NULL) { int i; for (i=0;i<numArgs;i++) { struct memorycontainer* expression=getExpressionAt(args, i); memoryContainer=concatenateMemory(memoryContainer, expression); } } if (singleArg != NULL) memoryContainer=concatenateMemory(memoryContainer, singleArg); return memoryContainer; }