void test_getLastElement_gives_the_address_of_last_element_in_the_list () { LinkedList list = createList(); int a = 5; int b = 10; addToList(&list, &a); addToList(&list, &b); Element *element = getLastElement(list); assert(*(int *)(element->value) == 10); }
int performLivenessIteration(t_cflow_Graph *graph) { int modified; t_list *current_element; t_basic_block *current_bblock; /* initialize the value of the local variable `modified' */ modified = 0; /* test the preconditions */ if (graph == NULL) { cflow_errorcode = CFLOW_GRAPH_UNDEFINED; return modified; } /* test if `graph->endingBlock' is valid */ if (graph->endingBlock == NULL) { cflow_errorcode = CFLOW_INVALID_BBLOCK; return modified; } /* retrieve the last basic block in the list */ current_element = getLastElement(graph->blocks); while(current_element != NULL) { t_list *live_out_vars; current_bblock = (t_basic_block *) LDATA(current_element); assert(current_bblock != NULL); /* retrieve the variables that will be live out from this block */ live_out_vars = computeLiveOutVars(graph, current_bblock); /* test if an error occurred */ if (cflow_errorcode != CFLOW_OK) return modified; /* retrieve the liveness informations for the current bblock */ if (performLivenessOnBlock(current_bblock, live_out_vars)) modified = 1; /* remove the list `out' */ freeList(live_out_vars); /* test if an error occurred */ if (cflow_errorcode != CFLOW_OK) { return modified; } /* retrieve the previous element in the list */ current_element = LPREV(current_element); } /* return 1 if another liveness iteration is required */ return modified; }
// --------------------------------------------------------------------------------- // 설명 : 순차모드로 파일을 open 한다. // 동작조건: 파일 경로가 입력되어 있어야 한다. (NEFileSystem의 setPath) // 메모 : // 히스토리: 2011-07-07 이태훈 개발 완료 // --------------------------------------------------------------------------------- void NE_DLL NESequencialFileLoader::open() { // pre: if(_sequencial_file_in_stream.is_open()) return; const NEFileSystem::TaskUnit* task = &getLastElement(); if( ! &task) return; if( ! task->isPathInputted()) return; if(! task->isTaskExecutedAtLeastOnce()) findFile(); // 여기에 도달했다는 건 최소한 1번은 findFile을 했다는 얘기 // 왜 TaskUnit을 두번 가져오는 가: // findFile() 을 하면서 처음 task가 pop(= delete) 될수도 있다. const NEFileSystem::TaskUnit& task_after_finding = getLastElement(); if( ! &task_after_finding) return; if(! task->isFileInformationFilledIn()) return; if(! task->isThereAnyTask()) return; // main: _sequencial_file_in_stream.open(task->getFilePath(), std::ios_base::in); }
t_list * getLastElement(t_list *list) { /* preconditions */ if (list == NULL) return NULL; /* test if the current element is the last element of the list */ if (LNEXT(list) == NULL) return list; /* recursively call the getLastElement on the next item of the list */ return getLastElement(LNEXT(list)); }
void test_forEach_goes_through_the_list_and_performs_the_given_action_on_each_element () { LinkedList list = createList(); int a = 5; int b = 10; addToList(&list, &a); addToList(&list, &b); forEach(list, add2); Element *element = getFirstElement(list); assert(*(int *)(element->value) == 7); element = getLastElement(list); assert(*(int *)(element->value) == 12); }
t_list * getLiveOUTVars(t_basic_block *bblock) { t_list *last_Element; t_cflow_Node *lastNode; if (bblock == NULL) return NULL; if (bblock->nodes == NULL) return NULL; last_Element = getLastElement(bblock->nodes); lastNode = (t_cflow_Node *) LDATA(last_Element); assert(lastNode != NULL); /* return a copy of the list of variables live in * input to the current basic block */ return cloneList(lastNode->out); }
int performLivenessOnBlock(t_basic_block *bblock, t_list *out) { t_list *current_element; t_list *cloned_list; t_cflow_Node *next_node; t_cflow_Node *current_node; int modified; /* initialize the local variables */ modified = 0; if (bblock == NULL || bblock->nodes == NULL) { cflow_errorcode = CFLOW_INVALID_BBLOCK; return modified; } current_element = getLastElement(bblock->nodes); current_node = (t_cflow_Node *) LDATA(current_element); assert(current_node != NULL); /* update the out set */ current_node->out = addListToSet (current_node->out, out, NULL, &modified); /* update the in list */ cloned_list = cloneList(current_node->out); #if CFLOW_ALWAYS_LIVEIN_R0 == (1) if ((current_node->uses)[0] != NULL && (current_node->uses)[0]->ID != REG_0) cloned_list = addVariableToSet (cloned_list, (current_node->uses)[0], NULL); if ((current_node->uses)[1] != NULL && (current_node->uses)[1]->ID != REG_0) cloned_list = addVariableToSet (cloned_list, (current_node->uses)[1], NULL); if ((current_node->uses)[2] != NULL && (current_node->uses)[2]->ID != REG_0) cloned_list = addVariableToSet (cloned_list, (current_node->uses)[2], NULL); #else if ((current_node->uses)[0] != NULL) cloned_list = addVariableToSet (cloned_list, (current_node->uses)[0], NULL); if ((current_node->uses)[1] != NULL) cloned_list = addVariableToSet (cloned_list, (current_node->uses)[1], NULL); if ((current_node->uses)[2] != NULL) cloned_list = addVariableToSet (cloned_list, (current_node->uses)[2], NULL); #endif #if CFLOW_ALWAYS_LIVEIN_R0 == (1) if (current_node->def != NULL && (current_node->def)->ID != REG_0) #else if (current_node->def != NULL) #endif { int found = 0; int current_use_idx = 0; do { if ((current_node->uses)[current_use_idx] != NULL) { if ((current_node->uses)[current_use_idx]->ID == current_node->def->ID) found = 1; } current_use_idx++; } while((found == 0) && (current_use_idx < 3)); if (!found) cloned_list = removeElement(cloned_list, current_node->def); } current_node->in = addListToSet (current_node->in, cloned_list, NULL, &modified); /* remove the cloned list */ freeList(cloned_list); /* set the new value of next_node */ next_node = current_node; current_element = LPREV(current_element); while (current_element != NULL) { /* take a new node */ current_node = (t_cflow_Node *) LDATA(current_element); assert(current_node != NULL); /* clone the `in' list of the next_node */ cloned_list = cloneList(next_node->in); /* update the out list */ current_node->out = addListToSet (current_node->out, cloned_list, NULL, &modified); /* remove the cloned list */ freeList(cloned_list); /* clone the `in' list of the next_node */ cloned_list = cloneList(current_node->out); /* update the in list */ #if CFLOW_ALWAYS_LIVEIN_R0 == (1) if ((current_node->uses)[0] != NULL && (current_node->uses)[0]->ID != REG_0) cloned_list = addVariableToSet (cloned_list, (current_node->uses)[0], NULL); if ((current_node->uses)[1] != NULL && (current_node->uses)[1]->ID != REG_0) cloned_list = addVariableToSet (cloned_list, (current_node->uses)[1], NULL); if ((current_node->uses)[2] != NULL && (current_node->uses)[2]->ID != REG_0) cloned_list = addVariableToSet (cloned_list, (current_node->uses)[2], NULL); #else if ((current_node->uses)[0] != NULL) cloned_list = addVariableToSet (cloned_list, (current_node->uses)[0], NULL); if ((current_node->uses)[1] != NULL) cloned_list = addVariableToSet (cloned_list, (current_node->uses)[1], NULL); if ((current_node->uses)[2] != NULL) cloned_list = addVariableToSet (cloned_list, (current_node->uses)[2], NULL); #endif #if CFLOW_ALWAYS_LIVEIN_R0 == (1) if (current_node->def != NULL && (current_node->def)->ID != REG_0) #else if (current_node->def != NULL) #endif { int found = 0; int current_use_idx = 0; do { if ((current_node->uses)[current_use_idx] != NULL) { if ((current_node->uses)[current_use_idx]->ID == current_node->def->ID) { found = 1; } } current_use_idx++; } while((found == 0) && (current_use_idx < 3)); if (!found) cloned_list = removeElement(cloned_list, current_node->def); } current_node->in = addListToSet (current_node->in, cloned_list, NULL, &modified); /* remove the cloned list */ freeList(cloned_list); /* update the loop control informations */ current_element = LPREV(current_element); next_node = current_node; } /* return the `modified' value */ return modified; }
void updateFlowGraph(t_cflow_Graph *graph) { t_list *current_element; t_basic_block *current_block; t_basic_block *successor; /* preconditions: graph should not be a NULL pointer */ if (graph == NULL){ cflow_errorcode = CFLOW_GRAPH_UNDEFINED; return; } successor = NULL; current_element = graph->blocks; while(current_element != NULL) { t_list *last_element; t_cflow_Node *last_node; t_axe_instruction *last_instruction; t_basic_block *jumpBlock; /* retrieve the current block */ current_block = (t_basic_block *) LDATA(current_element); assert(current_block != NULL); assert(current_block->nodes != NULL); /* get the last node of the basic block */ last_element = getLastElement(current_block->nodes); assert(last_element != NULL); last_node = (t_cflow_Node *) LDATA(last_element); assert(last_node != NULL); last_instruction = last_node->instr; assert(last_instruction != NULL); if (isHaltInstruction(last_instruction)) { setSucc(current_block, graph->endingBlock); setPred(graph->endingBlock, current_block); } else { if (isJumpInstruction(last_instruction)) { if ( (last_instruction->address == NULL) || ((last_instruction->address)->labelID == NULL) ) { cflow_errorcode = CFLOW_INVALID_LABEL_FOUND; return; } jumpBlock = searchLabel(graph , (last_instruction->address)->labelID); if (jumpBlock == NULL) { cflow_errorcode = CFLOW_INVALID_LABEL_FOUND; return; } /* add the jumpBlock to the list of successors of current_block */ /* add also current_block to the list of predecessors of jumpBlock */ setPred(jumpBlock, current_block); if (cflow_errorcode != CFLOW_OK) return; setSucc(current_block, jumpBlock); if (cflow_errorcode != CFLOW_OK) return; } if (!isUnconditionalJump(last_instruction)) { t_basic_block *nextBlock; t_list *next_element; next_element = LNEXT(current_element); if (next_element != NULL) { nextBlock = LDATA(next_element); assert(nextBlock != NULL); setSucc(current_block, nextBlock); setPred(nextBlock, current_block); } else { setSucc(current_block, graph->endingBlock); setPred(graph->endingBlock, current_block); } if (cflow_errorcode != CFLOW_OK) return; } } /* update the value of `current_element' */ current_element = LNEXT(current_element); } }
/* add an element `data' to the list `list' at position `pos'. If pos is negative * , or is larger than the number of elements in the list, the new element is * added on to the end of the list. Function `addElement' returns a pointer * to the new head of the list */ t_list * addElement(t_list *list, void * data, int pos) { t_list *result; t_list *current_elem; t_list *last_elem; int current_pos; /* initialize the value of `result' */ result = newElement(data); if (list == NULL) return result; if (pos == 0) { /* update the control informations */ SET_NEXT(result, list); SET_PREV(list, result); /* return the new head of the list */ return result; } /* retrieve the last element of the list */ last_elem = getLastElement(list); assert(last_elem != NULL); if (pos < 0) { /* update the control informations */ SET_NEXT(last_elem, result); SET_PREV(result, last_elem); /* update the value of result */ return list; } /* `pos' is a positive integer */ current_pos = 0; current_elem = list; while(current_pos < pos) { if (current_elem == last_elem) { /* update the control informations */ SET_NEXT(last_elem, result); SET_PREV(result, last_elem); /* update the value of result */ return list; } /* update the loop informations */ current_elem = LNEXT(current_elem); current_pos++; } /* assertions */ assert(current_elem != NULL); /* update the control informations */ SET_NEXT(result, current_elem); SET_PREV(result, LPREV(current_elem)); SET_NEXT(LPREV(current_elem), result); SET_PREV(current_elem, result); /* return the new head of the list */ return list; }
/** * This will loop through the file and extract XML Data. * @param _fileName the path to the file which will be read */ int XMLReader(string _fileName) { ifstream infile; // Input file stream for XML file to parse int intLineCount = 0; // Keep track of line number string strLine = ""; // Hold current line while parsing XML file vector<Element*> vecElementStack; // Will hold all tag names and tag lines string previousElementName = ""; // The tag name of the previous element on the vector (stack) ParserState currentState = STARTING_DOCUMENT; // Will hold state of previously read line Element* rootElement = NULL; infile.open(_fileName, ifstream::in); if ( infile.fail() ) { cerr << "Error opening '" << _fileName << "'" << endl ; return EXIT_FAILURE; } else { // Loop through the file do { // Increment line counter intLineCount++; // Read a new line from the file getline(infile, strLine); // Get tag name of previous open tag element (will return "" if none) previousElementName = getLastElement(vecElementStack); // Get state of newly read line currentState = GetXMLData(strLine, currentState); // Handle parser state specific actions if (currentState == ELEMENT_OPENING_TAG) //########################## HERE { Element* tempElement = new Element(intLineCount, strLine, vecElementStack.size(), currentState); tempElement->addAttribute(getAttributes(strLine)); if (rootElement == NULL) // First time adding to the tree { rootElement = tempElement; } else { (vecElementStack[ vecElementStack.size() - 1 ])->addChild( tempElement ); } vecElementStack.push_back(tempElement); previousElementName = getLastElement(vecElementStack); } else if (currentState == ELEMENT_CLOSING_TAG) { if (previousElementName != getClosingTagName(strLine)) { // if open tag name != closing tag name cout << endl << "*******************************************" << endl; cout << "ERROR: Invalid closing tag at Line " << intLineCount << endl; cout << "REASON: " << previousElementName << " != " << getClosingTagName(strLine) << endl; cout << "ABORTING..." << endl; cout << "*******************************************" << endl; return EXIT_FAILURE; } vecElementStack.pop_back(); } else if (currentState == ELEMENT_NAME_AND_CONTENT) { Element* tempElement = new Element(intLineCount, strLine, vecElementStack.size(), currentState); tempElement->addAttribute(getAttributes(strLine)); (vecElementStack[ vecElementStack.size() - 1 ])->addChild( tempElement ); } else if (currentState == ERROR) { removeWhiteSpace(strLine); cout << endl << "*******************************************" << endl; cout << "ERROR: Something went wrong!" << endl; cout << "~~ Line " << intLineCount << ": " << strLine << endl; cout << "ABORTING..." << endl; cout << "*******************************************" << endl; return EXIT_FAILURE; } printXMLData(vecElementStack, currentState, intLineCount, strLine, previousElementName); cout << endl; } while (infile.good()); cout << endl << endl << endl << "TREE OUTPUT" << endl; cout << "********************************************************" << endl; RecursivePrint(rootElement); deleteVectorData(vecElementStack); } }