void dMaterialNodeInfo::Serialize (TiXmlElement* const rootNode) { SerialiseBase(dNodeInfo, rootNode); char buffer[4096]; // itoa64____ (m_id, id, 10); // rootNode->SetAttribute("id", id); rootNode->SetAttribute("id", m_id); // char id[256]; TiXmlElement* const ambient = new TiXmlElement ("ambient"); rootNode->LinkEndChild(ambient); dFloatArrayToString(&m_ambientColor[0], 4, buffer, sizeof (buffer)); //itoa64____ (m_ambientTexId, id, 10); dString id (m_ambientTexId); ambient->SetAttribute("textureId", id.GetStr()); ambient->SetAttribute("color", buffer); TiXmlElement* const diffuse = new TiXmlElement ("diffuse"); rootNode->LinkEndChild(diffuse); dFloatArrayToString(&m_diffuseColor[0], 4, buffer, sizeof (buffer)); //itoa64____ (m_diffuseTexId, id, 10); id = dString (m_diffuseTexId); diffuse->SetAttribute("textureId", id.GetStr()); diffuse->SetAttribute("color", buffer); TiXmlElement* const specular = new TiXmlElement ("specular"); rootNode->LinkEndChild(specular); dFloatArrayToString(&m_specularColor[0], 4, buffer, sizeof (buffer)); //itoa64____ (m_specularTexId, id, 10); id = dString (m_specularTexId); specular->SetAttribute("textureId", id.GetStr()); specular->SetAttribute("color", buffer); TiXmlElement* const emissive = new TiXmlElement ("emissive"); rootNode->LinkEndChild(emissive); dFloatArrayToString(&m_emissiveColor[0], 4, buffer, sizeof (buffer)); //itoa64____ (m_emissiveTexId, id, 10); id = dString (m_emissiveTexId); emissive->SetAttribute("textureId", id.GetStr()); emissive->SetAttribute("color", buffer); TiXmlElement* const shininess = new TiXmlElement ("shininess"); rootNode->LinkEndChild(shininess); shininess->SetDoubleAttribute ("float", m_shininess); TiXmlElement* const opacity = new TiXmlElement ("opacity"); rootNode->LinkEndChild(opacity); opacity->SetDoubleAttribute ("float", m_opacity); }
dString dCIL::NewLabel () { char tmp[256]; sprintf (tmp, "label_%d", m_labelIndex); m_labelIndex ++; return dString (tmp); }
bool dMaterialNodeInfo::Deserialize (const dScene* const scene, TiXmlElement* const rootNode) { DeserialiseBase(scene, dNodeInfo, rootNode); char text[1024]; rootNode->Attribute("id", &m_id); TiXmlElement* const ambient = (TiXmlElement*) rootNode->FirstChild ("ambient"); sprintf (text, "%s", ambient->Attribute("textureId")); m_ambientTexId = dString((const char*)text).ToInteger64(); dStringToFloatArray (ambient->Attribute("color"), &m_ambientColor[0], 4); TiXmlElement* const diffuse = (TiXmlElement*) rootNode->FirstChild ("diffuse"); sprintf (text, "%s", diffuse->Attribute("textureId")); m_diffuseTexId = dString((const char*)text).ToInteger64(); dStringToFloatArray (diffuse->Attribute("color"), &m_diffuseColor[0], 4); TiXmlElement* const specular = (TiXmlElement*) rootNode->FirstChild ("specular"); sprintf (text, "%s", specular->Attribute("textureId")); m_emissiveTexId = dString((const char*)text).ToInteger64(); dStringToFloatArray (specular->Attribute("color"), &m_specularColor[0], 4); TiXmlElement* const emissive = (TiXmlElement*) rootNode->FirstChild ("emissive"); sprintf (text, "%s", emissive->Attribute("textureId")); m_emissiveTexId = dString((const char*)text).ToInteger64(); dStringToFloatArray (emissive->Attribute("color"), &m_emissiveColor[0], 4); TiXmlElement* const shininess = (TiXmlElement*) rootNode->FirstChild ("shininess"); double value; shininess->Attribute("float", &value); m_shininess = dFloat (value); TiXmlElement* const opacity = (TiXmlElement*) rootNode->FirstChild ("opacity"); opacity->Attribute("float", &value); m_opacity = dFloat (value); return true; }
dString dParserCompiler::GetClassName(const char* const fileName) const { char className[256]; const char* ptr = strrchr (fileName, '/'); if (ptr) { ptr ++; } else { ptr = strrchr (fileName, '\\'); if (ptr) { ptr ++; } else { ptr = fileName; } } strcpy (className, ptr); strtok (className, "."); return dString (className); }
void dDAGExpressionNodeAssigment::CompileCIL(dCIL& cil) { m_expression->CompileCIL(cil); if (m_leftVariable->m_dimExpressions.GetCount()) { dDAGDimensionNode* const dim = m_leftVariable->m_dimExpressions.GetFirst()->GetInfo(); dim->CompileCIL(cil); dString result = dim->m_result.m_label; for (dList<dDAGDimensionNode*>::dListNode* node = m_leftVariable->m_dimExpressions.GetFirst()->GetNext(); node; node = node->GetNext()) { dAssert (0); dDAGDimensionNode* const dim = node->GetInfo(); dim->CompileCIL(cil); #if 0 dCILInstr& stmtMul = cil.NewStatement()->GetInfo(); stmtMul.m_instruction = dCILInstr::m_assigment; stmtMul.m_operator = dCILInstr::m_mul; stmtMul.m_arg0.m_label = cil.NewTemp(); stmtMul.m_arg1.m_label = result; stmtMul.m_arg2.m_label = dim->m_arraySize; DTRACE_INTRUCTION (&stmtMul); dCILInstr& stmtAdd = cil.NewStatement()->GetInfo(); stmtAdd.m_instruction = dCILInstr::m_assigment; stmtAdd.m_operator = dCILInstr::m_add; stmtAdd.m_arg0.m_label = cil.NewTemp(); stmtAdd.m_arg1.m_label = stmtMul.m_arg0.m_label; stmtAdd.m_arg2 = dim->m_result; result = stmtAdd.m_arg0.m_label; DTRACE_INTRUCTION (&stmtAdd); #endif } dAssert (m_leftVariable->m_parent); dTree<dCILInstr::dArg, dString>::dTreeNode* const variable = dDAG::FindLocalVariable(m_leftVariable->m_name); dAssert (variable); dCILInstr::dArg arg0 (LoadLocalVariable(cil, m_expression->m_result)); dCILInstr::dArg arg1 (LoadLocalVariable(cil, variable->GetInfo())); int size = arg0.GetSizeInByte(); dCILInstrIntergerLogical* const mulOffset = new dCILInstrIntergerLogical(cil, dCILThreeArgInstr::m_mul,cil.NewTemp(), dCILInstr::dArgType(dCILInstr::m_int), result, dCILInstr::dArgType(dCILInstr::m_int), dString(size), dCILInstr::m_constInt); mulOffset->Trace(); dCILInstrIntergerLogical* const address = new dCILInstrIntergerLogical(cil, dCILThreeArgInstr::m_add, cil.NewTemp(), arg1.GetType(), arg1.m_label, arg1.GetType(), mulOffset->GetArg0().m_label, mulOffset->GetArg0().GetType()); address->Trace(); dCILInstrStore* const store = new dCILInstrStore(cil, address->GetArg0().m_label, address->GetArg0().GetType(), arg0.m_label, arg0.GetType()); store->Trace(); m_result = arg0; } else { m_leftVariable->CompileCIL(cil); dCILInstr::dArg arg1 (LoadLocalVariable(cil, m_expression->m_result)); dCILInstrMove* const move = new dCILInstrMove (cil, m_leftVariable->m_result.m_label, m_leftVariable->m_result.GetType(), arg1.m_label, arg1.GetType()); move->Trace(); } }
void dParserCompiler::ScanGrammarFile( const dString& inputRules, dProductionRule& ruleList, dTree<dTokenInfo, dCRCTYPE>& symbolList, dOperatorsPrecedence& operatorPrecedence, dString& userCodeBlock, dString& userVariableClass, dString& endUserCode, int& lastTokenEnum) { dString startSymbol (""); int tokenEnumeration = 256; int operatorPrecedencePriority = 0; dParserLexical lexical (inputRules.GetStr()); LoadTemplateFile("dParserUserVariableTemplate_cpp.txt", userVariableClass); // scan the definition segment for (dToken token = dToken(lexical.NextToken()); token != GRAMMAR_SEGMENT; ) { switch (int (token)) { case START: { token = dToken(lexical.NextToken()); startSymbol = lexical.GetTokenString(); token = dToken(lexical.NextToken()); break; } case TOKEN: { for (token = dToken(lexical.NextToken()); token == LITERAL; token = dToken(lexical.NextToken())) { const char* const name = lexical.GetTokenString(); symbolList.Insert(dTokenInfo (tokenEnumeration, TERMINAL, name), dCRC64 (name)); tokenEnumeration ++; } break; } case LEFT: case RIGHT: { dOperatorsAssociation& association = operatorPrecedence.Append()->GetInfo(); association.m_prioprity = operatorPrecedencePriority; operatorPrecedencePriority ++; switch (int (token)) { case LEFT: association.m_associativity = dOperatorsAssociation::m_left; break; case RIGHT: association.m_associativity = dOperatorsAssociation::m_right; break; } for (token = dToken(lexical.NextToken()); (token == LITERAL) || ((token < 256) && !isalnum (token)); token = dToken(lexical.NextToken())) { association.Append(dCRC64 (lexical.GetTokenString())); } break; } case UNION: { token = dToken(lexical.NextToken()); dAssert (token == SEMANTIC_ACTION); userVariableClass = lexical.GetTokenString() + 1; userVariableClass.Replace(userVariableClass.Size() - 1, 1, ""); token = dToken(lexical.NextToken()); break; } case CODE_BLOCK: { userCodeBlock += lexical.GetTokenString(); token = dToken(lexical.NextToken()); break; } case EXPECT: { token = dToken(lexical.NextToken()); dAssert (token == INTEGER); m_shiftReduceExpectedWarnings = atoi (lexical.GetTokenString()); token = dToken(lexical.NextToken()); break; } default:; { dAssert (0); token = dToken(lexical.NextToken()); } } } int ruleNumber = 1; lastTokenEnum = tokenEnumeration; // scan the production rules segment dToken token1 = dToken(lexical.NextToken()); for (; (token1 != GRAMMAR_SEGMENT) && (token1 != -1); token1 = dToken(lexical.NextToken())) { //dTrace (("%s\n", lexical.GetTokenString())); switch (int (token1)) { case LITERAL: { // add the first Rule; dRuleInfo& rule = ruleList.Append()->GetInfo(); rule.m_token = token1; rule.m_type = NONTERMINAL; rule.m_name = lexical.GetTokenString(); rule.m_nameCRC = dCRC64 (lexical.GetTokenString()); dTree<dTokenInfo, dCRCTYPE>::dTreeNode* nonTerminalIdNode = symbolList.Find(rule.m_nameCRC); if (!nonTerminalIdNode) { nonTerminalIdNode = symbolList.Insert(dTokenInfo (tokenEnumeration, NONTERMINAL, rule.m_name), rule.m_nameCRC); tokenEnumeration ++; } rule.m_ruleId = nonTerminalIdNode->GetInfo().m_tokenId; rule.m_ruleNumber = ruleNumber; ruleNumber ++; token1 = ScanGrammarRule(lexical, ruleList, symbolList, ruleNumber, tokenEnumeration, operatorPrecedence); break; } default: dAssert (0); } } dProductionRule::dListNode* firtRuleNode = ruleList.GetFirst(); if (startSymbol != "") { firtRuleNode = ruleList.Find (dCRC64 (startSymbol.GetStr())); } //Expand the Grammar Rule by adding an empty start Rule; const dRuleInfo& firstRule = firtRuleNode->GetInfo(); dRuleInfo& rule = ruleList.Addtop()->GetInfo(); rule.m_ruleNumber = 0; rule.m_ruleId = tokenEnumeration; rule.m_token = firstRule.m_token; rule.m_type = NONTERMINAL; rule.m_name = firstRule.m_name + dString("__"); rule.m_nameCRC = dCRC64 (rule.m_name.GetStr()); symbolList.Insert(dTokenInfo (tokenEnumeration, rule.m_type, rule.m_name), rule.m_nameCRC); tokenEnumeration ++; dSymbol& symbol = rule.Append()->GetInfo(); symbol.m_token = firstRule.m_token; symbol.m_type = firstRule.m_type; symbol.m_name = firstRule.m_name; symbol.m_nameCRC = firstRule.m_nameCRC; // scan literal use code if (token1 == GRAMMAR_SEGMENT) { endUserCode = lexical.GetNextBuffer(); //endUserCode += "\n"; } }
void dParserLexical::GetLexString () { int length = m_index - m_startIndex; m_tokenString = dString (&m_data[m_startIndex], length); m_startIndex = m_index; }
bool dDataFlowGraph::ApplyConstantFolding() { bool ret = false; for (dCIL::dListNode* stmtNode = m_function; stmtNode; stmtNode = stmtNode->GetNext()) { dThreeAdressStmt& stmt = stmtNode->GetInfo(); switch (stmt.m_instruction) { case dThreeAdressStmt::m_if: { if (stmt.m_arg1.m_type == dThreeAdressStmt::m_intConst) { ret = true; int val = EvaluateBinaryExpression (stmt.m_arg0.m_label, stmt.m_operator, stmt.m_arg1.m_label); if (val) { stmt.m_instruction = dThreeAdressStmt::m_goto; stmt.m_arg0.m_label = stmt.m_arg2.m_label; } else { stmt.m_instruction = dThreeAdressStmt::m_nop; } } else if ((stmt.m_arg0.m_type == dThreeAdressStmt::m_intVar) && (stmt.m_arg1.m_type == dThreeAdressStmt::m_intVar)) { dList<dCIL::dListNode*>& argListNodeA = m_variableDefinitions.Find (stmt.m_arg0.m_label)->GetInfo(); dList<dCIL::dListNode*>& argListNodeB = m_variableDefinitions.Find (stmt.m_arg1.m_label)->GetInfo(); if ((argListNodeA.GetCount() == 1) && (argListNodeB.GetCount() == 1)) { dThreeAdressStmt& argStmtA = argListNodeA.GetFirst()->GetInfo()->GetInfo(); dThreeAdressStmt& argStmtB = argListNodeB.GetFirst()->GetInfo()->GetInfo(); if ((argStmtA.m_arg1.m_type == dThreeAdressStmt::m_intConst) && (argStmtB.m_arg1.m_type == dThreeAdressStmt::m_intConst)) { ret = true; stmt.m_arg0 = argStmtA.m_arg1; stmt.m_arg1 = argStmtB.m_arg1; int val = EvaluateBinaryExpression (stmt.m_arg0.m_label, stmt.m_operator, stmt.m_arg1.m_label); if (val) { stmt.m_instruction = dThreeAdressStmt::m_goto; stmt.m_arg0.m_label = stmt.m_arg2.m_label; } else { stmt.m_instruction = dThreeAdressStmt::m_nop; } UpdateReachingDefinitions(); //m_cil->Trace(); } } } break; } case dThreeAdressStmt::m_assigment: { if (stmt.m_operator != dThreeAdressStmt::m_nothing) { if (((stmt.m_arg1.m_type == dThreeAdressStmt::m_intConst) || (stmt.m_arg1.m_type == dThreeAdressStmt::m_floatConst)) && ((stmt.m_arg2.m_type == dThreeAdressStmt::m_intConst) || (stmt.m_arg2.m_type == dThreeAdressStmt::m_floatConst))) { ret = true; if ((stmt.m_arg1.m_type == dThreeAdressStmt::m_intConst) && (stmt.m_arg1.m_type == dThreeAdressStmt::m_intConst)) { int arg0 = EvaluateBinaryExpression (stmt.m_arg1.m_label, stmt.m_operator, stmt.m_arg2.m_label); stmt.m_operator = dThreeAdressStmt::m_nothing; stmt.m_arg1.m_label = dString(arg0); stmt.m_arg2.m_label = dString (""); } else if ((stmt.m_arg1.m_type == dThreeAdressStmt::m_floatConst) && (stmt.m_arg1.m_type == dThreeAdressStmt::m_floatConst)) { dAssert (0); } else { dAssert (0); } } } break; } } } return ret; }