void ClipLine( POINT &Start, POINT &End ) { INT32 ThisSector = GetSector( End ) ; switch ( CaseTable[(INT32)LastSector][(INT32)ThisSector] ) { case 01 : GenLineToBelowLeft() ; break ; case 02 : GenLineToBelowRight() ; break ; case 03 : GenLineToAboveLeft() ; break ; case 04 : GenLineToAboveRight() ; break ; case 05 : GenLine(Start) ; GenLine(End) ; break ; case 06 : if ( OFirstPoint ) GenLine(Start) ; GenLine(End) ; break ; case 07 : POINT Middle ; Middle.x = (Start.x+End.x) >> 1 ; Middle.y = (Start.y+End.y) >> 1 ; ClipLine( Start,Middle ) ; ClipLine( Middle,End ) ; return ; case 10 : GenLineToBelowLeft() ; GenLineToBelowRight() ; break ; case 11 : GenLineToBelowLeft() ; GenLineToAboveLeft() ; break ; case 12 : GenLineToBelowRight() ; GenLineToAboveRight() ; break ; case 13 : GenLineToBelowRight() ; GenLineToBelowLeft() ; break ; case 14 : GenLineToAboveLeft() ; GenLineToAboveRight() ; break ; case 15 : GenLineToAboveLeft() ; GenLineToBelowLeft() ; break ; case 16 : GenLineToAboveRight() ; GenLineToBelowRight() ; break ; case 17 : GenLineToAboveRight() ; GenLineToAboveLeft() ; break ; } LastSector = ThisSector ; }
void CodeGenerator::GenProgramStart() { GenLine( ".386P" ); GenLine( ".MODEL FLAT" ); GenLine( "INCLUDELIB LIBCMT" ); GenLine( "EXTRN _printf : NEAR" ); GenLine( ".code\n" ); }
void CodeGenerator::GenFuncCall( std::string funcName, int paramCountCall ) { GenReverseStack( paramCountCall ); GenBlockHeader( "Call " + funcName + " Function" ); GenLine( "CALL _" + funcName ); GenLine( "add esp, " + toString( 4 * paramCountCall ) + "d" ); GenPush( "eax" ); }
int CodeGenerator::GenWhileMiddle() { int whileFinishLabel = CreateLabel(); GenPop( "eax" ); GenLine( "cmp eax, 0" ); GenLine( "JE L" + toString( whileFinishLabel ) ); return whileFinishLabel; }
void CodeGenerator::GenStringPush( std::string str ) { std::string strVar = "S" + toString( mStrCount++ ); GenLine( ".data" ); GenLine( strVar + " BYTE \"" + str + "\", 00h" ); GenLine( ".code" ); GenPush( "OFFSET " + strVar ); }
void CodeGenerator::GenDivision() { GenBlockHeader( "Divide" ); GenPop( "ecx" ); GenPop( "eax" ); GenLine( "CDQ" ); GenLine( "idiv ecx" ); GenPush( "eax" ); }
int CodeGenerator::GenIfStart() { GenBlockHeader( "If statement" ); GenPop( "eax" ); GenLine( "cmp eax, 0" ); int endIflabel = CreateLabel(); GenLine( "JE L" + toString( endIflabel ) ); return endIflabel; }
void CodeGenerator::GenFuncEnd( std::string funcName, int retLabel ) { funcName = '_' + funcName; GenLine(); GenLabel( retLabel ); GenMov( "esp", "ebp" ); GenPop( "ebp" ); GenLine( "RET" ); GenLine( funcName + " ENDP" ); }
int CodeGenerator::GenFuncStart( std::string funcName ) { GenBlockHeader( "Function " + funcName + " Declaration" ); funcName = '_' + funcName; GenLine( "PUBLIC " + funcName ); GenLine( funcName + " PROC" ); GenPush( "ebp" ); GenMov( "ebp","esp" ); GenLine(); return CreateLabel(); }
void CodeGenerator::GenNot() { GenBlockHeader( "Not" ); GenPop( "eax" ); GenMov( "edx", "0" ); GenLine( "cmp eax, 0" ); GenLine( "JNE L" + toString( ++mLabelCount ) ); GenLine( "inc edx" ); GenLabel( mLabelCount ); GenPush( "edx" ); }
void CodeGenerator::GenOr() { GenBlockHeader( "Or" ); GenPop( "ecx" ); GenPop( "eax" ); GenMov( "edx", "0" ); GenLine( "or eax, ecx" ); GenLine( "cmp eax, 0" ); GenLine( "JE L" + toString( ++mLabelCount ) ); GenLine( "inc edx" ); GenLabel( mLabelCount ); GenPush( "edx" ); }
void CodeGenerator::GenAnd() { GenBlockHeader( "And" ); GenPop( "ecx" ); GenPop( "eax" ); GenLine( "imul ecx" ); GenMov( "edx", "0" ); GenLine( "cmp eax, 0" ); mAsmCode += "JE"; mAsmCode += " L" + toString( ++mLabelCount ) + "\n"; GenLine( "inc edx" ); GenLabel( mLabelCount ); GenPush( "edx" ); }
void CodeGenerator::GenNegate() { GenBlockHeader( "Negate" ); GenPop( "eax" ); GenLine( "neg eax" ); GenPush( "eax" ); }
int _tmain(int argc, _TCHAR* argv[]) { GenLine(); // char utf8Buf[256]; // CommonLib::CString str = "string"; // str.exportToUTF8(utf8Buf, 255); // CompressShape(); // TestTableWks(); //testLoadXML1(); //return 1; // TestShapeIndex(); // /*CommonLib::CString s1(L"vfdgggf"); CommonLib::CString s2(s1); ITest *pTest = new ITest(s1); delete pTest;*/ //TestShapeIndex(); // TestSQLiteWks(); //TestEmbDBWks(); //TestXML(); /*for(int i =0; i < 100000000; ++i) { CommonLib::CString s1 = L"vfdgggf"; CommonLib::CString s2 = s1; }*/ return 0; }
// GenBinaryOperation void CodeGenerator::GenBinaryOperation( std::string operationName ) { GenBlockHeader( operationName ); GenPop( "ecx" ); GenPop( "eax" ); GenLine( operationName + " eax, ecx" ); GenPush( "eax" ); }
void CodeGenerator::GenMultiplication() { GenBlockHeader( "Multiply" ); GenPop( "ecx" ); GenPop( "eax" ); GenLine( "imul ecx" ); GenPush( "eax" ); }
void CodeGenerator::GenCompare( std::string sign ) { GenBlockHeader( "Compare" ); GenPop( "ecx" ); GenPop( "eax" ); GenMov( "edx", "0" ); GenLine( "cmp eax, ecx" ); if( sign == "==" ) mAsmCode += "JNE "; if( sign == "!=" ) mAsmCode += "JE"; if( sign == ">" ) mAsmCode += "JLE"; if( sign == "<" ) mAsmCode += "JGE"; if( sign == ">=" ) mAsmCode += "JL"; if( sign == "<=" ) mAsmCode += "JG"; mAsmCode += " L" + toString( ++mLabelCount ) + "\n"; GenLine( "inc edx" ); GenLabel( mLabelCount ); GenPush( "edx" ); }
void CodeGenerator::GenReverseStack( int depth ) { if( depth < 2 ) return; GenBlockHeader( "Reversing last " + toString( depth ) + " stack items" ); GenLine( ".data" ); // allocate enough memory for( int i = 1; i <= depth; ++i ) { GenLine( "stackReveseVar" + toString( mStackReverseCount++ ) + " DWORD ?" ); } GenLine( ".code" ); // pop into memory for( int i = depth; i >= 1; --i ) { GenPop( "stackReveseVar" + toString( mStackReverseCount - i ) ); } for( int i = depth; i >= 1; --i ) { GenPush( "stackReveseVar" + toString( mStackReverseCount - i ) ); } }
void ClipCurve( POINT &C0, POINT &C1, POINT &C2, POINT &C3 ) { INT32 C1Sector = GetSector( C1 ) ; INT32 C2Sector = GetSector( C2 ) ; INT32 ThisSector = GetSector( C3 ) ; // if ( ( ( C1Sector==LastSector || C1Sector==ThisSector ) && // ( C2Sector==LastSector || C2Sector==ThisSector ) ) || // ( Region[LastSector] & Region[C1Sector] & Region[C2Sector] & Region[ThisSector] ) ) INT32 RegionUnion = Region[LastSector] & Region[C1Sector] & Region[C2Sector] & Region[ThisSector]; if ( ( RegionUnion & 0x0ff ) || ( (RegionUnion & 0x300) && (LastSector!=ThisSector || LastSector==12) ) || ( (C1Sector==LastSector || C1Sector==ThisSector) && (C2Sector==LastSector || C2Sector==ThisSector) ) ) { switch ( CaseTable[(INT32)LastSector][(INT32)ThisSector] ) { case 01 : GenLineToBelowLeft() ; break ; case 02 : GenLineToBelowRight() ; break ; case 03 : GenLineToAboveLeft() ; break ; case 04 : GenLineToAboveRight() ; break ; case 05 : GenLine(C0,FALSE) ; GenCurve(C1,C2,C3) ; break ; case 06 : if ( OFirstPoint ) GenLine(C0,FALSE) ; GenCurve(C1,C2,C3) ; break ; case 07 : SplitCurve(C0,C1,C2,C3) ; return ; case 10 : GenLineToBelowLeft() ; GenLineToBelowRight() ; break ; case 11 : GenLineToBelowLeft() ; GenLineToAboveLeft() ; break ; case 12 : GenLineToBelowRight() ; GenLineToAboveRight() ; break ; case 13 : GenLineToBelowRight() ; GenLineToBelowLeft() ; break ; case 14 : GenLineToAboveLeft() ; GenLineToAboveRight() ; break ; case 15 : GenLineToAboveLeft() ; GenLineToBelowLeft() ; break ; case 16 : GenLineToAboveRight() ; GenLineToBelowRight() ; break ; case 17 : GenLineToAboveRight() ; GenLineToAboveLeft() ; break ; } LastSector = ThisSector ; } else SplitCurve(C0,C1,C2,C3) ; }
void CodeGenerator::GenLabel( int labelNum ) { GenLine( "L" + toString( labelNum ) + ":" ); }
void CodeGenerator::GenPop( std::string reg ) { GenLine( "pop " + reg ); }
void CodeGenerator::GenMov( std::string toStr, std::string fromStr ) { GenLine( "mov " + toStr + ", " + fromStr ); }
void CodeGenerator::GenBlockHeader( std::string blockName ) { GenLine( "\n;--------------- " + blockName + " ---------------" ); }
void CodeGenerator::GenProgramEnd() { GenLine( "\nEND" ); }
void CodeGenerator::GenGlobalVarDef( std::string varName ) { GenLine( ".data" ); GenLine( '_' + varName + " DWORD ?" ); GenLine( ".code" ); }
void CodeGenerator::GenLocalVarDef() { GenLine( "sub esp, 4" ); }
void CodeGenerator::GenDoMiddle( int doStartLabel ) { GenPop( "eax" ); GenLine( "cmp eax, 0" ); GenLine( "JNE L" + toString( doStartLabel ) ); }
void CodeGenerator::GenPush( std::string reg ) { GenLine( "push " + reg ); }
void CodeGenerator::GenPush( int n ) { GenLine( "push " + toString( n ) + "d" ); }
void CodeGenerator::GenJmp( int label ) { GenLine( "JMP L" + toString( label ) ); }