int32 Compile_ReConfigureLogicInBlock ( BlockInfo * bi, int32 overwriteFlag ) { if ( CfrTil_GetState ( _CfrTil_, OPTIMIZE_ON | INLINE_ON ) ) { byte * saveHere = Here ; if ( bi->LogicCode ) // && ( bi->LogicCodeWord->Symbol->Category & CATEGORY_LOGIC ) ) { //if ( bi->LogicCodeWord->StackPushRegisterCode ) SetHere ( bi->LogicCodeWord->StackPushRegisterCode ) ; //else SetHere ( bi->LogicCode ) ; // compile RET within the block bi at the SpecialCode // standard compile of logic is overwritten for optimize and inline if ( overwriteFlag ) { _Compile_Return ( ) ; bi->bp_Last = Here ; _Compile_Noop ( ) ; // adjust so Disassemble doesn't get an "invalid" insn; we overwrite a 3 byte insn ( 0fb6c0 : movzx eax, al ) with RET NOP NOP _Compile_Noop ( ) ; SetHere ( saveHere ) ; } return true ; } } return false ; }
int32 CfrTil_DoWhileCombinator ( ) { block testBlock = ( block ) TOS, doBlock = ( block ) Dsp [ - 1 ] ; _DataStack_DropN ( 2 ) ; if ( CompileMode ) { CfrTil_BeginCombinator ( 2 ) ; byte * start = Here ; _Context_->Compiler0->ContinuePoint = Here ; _Compile_Block ( ( byte* ) doBlock, 1, 0 ) ; //_Compile_Block ( ( byte* ) testBlock, 0, 1 ) ; if ( ! _Compile_Block ( ( byte* ) testBlock, 0, 1 ) ) { SetHere ( start ) ; return 0 ; } _Compile_JumpToAddress ( start ) ; _Context_->Compiler0->BreakPoint = Here ; CfrTil_CalculateAndSetPreviousJmpOffset_ToHere ( ) ; CfrTil_EndCombinator ( 2, 1 ) ; } else { do { _Block_Eval ( doBlock ) ; _Block_Eval ( testBlock ) ; if ( ! _DataStack_Pop ( ) ) break ; } while ( 1 ) ; } return 1 ; }
void Compile_X_Group5 ( Compiler * compiler, int32 op ) { //_Set_SCA ( 0 ) ; int optFlag = CheckOptimize ( compiler, 3 ) ; //Word *one = Compiler_WordStack ( - 1 ) ; // assumes two values ( n m ) on the DSP stack Word *one = Compiler_WordList ( 1 ) ; // assumes two values ( n m ) on the DSP stack if ( optFlag & OPTIMIZE_DONE ) return ; else if ( optFlag ) { if ( compiler->optInfo->OptimizeFlag & OPTIMIZE_IMM ) { _Compile_MoveImm_To_Reg ( EAX, compiler->optInfo->Optimize_Imm, CELL ) ; compiler->optInfo->Optimize_Mod = REG ; compiler->optInfo->Optimize_Rm = EAX ; } _Compile_Group5 ( op, compiler->optInfo->Optimize_Mod, compiler->optInfo->Optimize_Rm, 0, compiler->optInfo->Optimize_Disp, 0 ) ; } else if ( one && one->CProperty & ( PARAMETER_VARIABLE | LOCAL_VARIABLE | NAMESPACE_VARIABLE ) ) // *( ( cell* ) ( TOS ) ) += 1 ; { SetHere ( one->Coding ) ; _Compile_GetVarLitObj_RValue_To_Reg ( one, EAX, 1 ) ; //_Compile_Group5 ( int32 code, int32 mod, int32 rm, int32 sib, int32 disp, int32 size ) _Compile_Group5 ( op, REG, EAX, 0, 0, 0 ) ; // ++ == += :: -- == -= so : _Compile_SetVarLitObj_With_Reg ( one, EAX, ECX ) ; } else { // assume rvalue on stack _Compile_Group5 ( op, MEM, DSP, 0, 0, 0 ) ; } _Compiler_Setup_BI_tttn ( _Context_->Compiler0, ZERO_CC, NZ, 3 ) ; // ?? // not less than 0 == greater than 0 }
void _Block_Copy ( byte * srcAddress, int32 qsize ) { byte * saveHere = Here, * saveAddress = srcAddress ; ud_t * ud = Debugger_UdisInit ( _CfrTil_->Debugger0 ) ; int32 isize, left ; for ( left = qsize ; left > 0 ; srcAddress += isize ) { isize = _Udis_GetInstructionSize ( ud, srcAddress ) ; left -= isize ; if ( * srcAddress == _RET ) { if ( left && ( ( * srcAddress + 1 ) != NOOP ) ) // noop from our logic overwrite { // ?? unable at present to compile inline with more than one return in the block SetHere ( saveHere ) ; _Compile_Call ( saveAddress ) ; } break ; // don't include RET } else if ( * srcAddress == CALLI32 ) { int32 offset = * ( int32* ) ( srcAddress + 1 ) ; // 1 : 1 byte opCode if ( ! offset ) { _CfrTil_WordName_Run ( ( byte* ) "recurse" ) ; continue ; } else { byte * jcAddress = JumpCallInstructionAddress ( srcAddress ) ; Word * word = Word_GetFromCodeAddress ( jcAddress ) ; if ( word ) { _CompileWord ( word ) ; continue ; } //else (drop to) _CompileN ( srcAddress, isize ) } } else if ( * srcAddress == JMPI32 ) { int32 offset = * ( int32* ) ( srcAddress + 1 ) ; // 1 : 1 byte opCode if ( offset == 0 ) // signature of a goto point { _CfrTil_MoveGotoPoint ( ( int32 ) srcAddress + 1, 0, ( int32 ) Here + 1 ) ; _CompileN ( srcAddress, isize ) ; // memcpy ( dstAddress, address, size ) ; continue ; } //else (drop to) _CompileN ( srcAddress, isize ) } _CompileN ( srcAddress, isize ) ; // memcpy ( dstAddress, address, size ) ; } }
void Block_Copy ( byte * dst, byte * src, int32 qsize ) { if ( dst > src ) { Error_Abort ( ( byte* ) "\nBlock_Copy :: Error : dst > src.\n" ) ; //return ; // ?? what is going on here ?? } SetHere ( dst ) ; _Block_Copy ( src, qsize ) ; }
void Compile_LogicalNot ( Compiler * compiler ) { Word *one = Compiler_WordStack ( compiler, - 1 ) ; int optFlag = CheckOptimizeOperands ( compiler, 2 ) ; if ( optFlag == OPTIMIZE_DONE ) return ; else if ( optFlag ) { if ( compiler->Optimizer->OptimizeFlag & OPTIMIZE_IMM ) { _Compile_MoveImm_To_Reg ( EAX, compiler->Optimizer->Optimize_Imm, CELL ) ; } else if ( compiler->Optimizer->Optimize_Rm == DSP ) { _Compile_Move_StackN_To_Reg ( EAX, DSP, 0 ) ; } else if ( compiler->Optimizer->Optimize_Rm != EAX ) { _Compile_VarConstOrLit_RValue_To_Reg ( one, EAX ) ; } _Compile_TEST_Reg_To_Reg ( EAX, EAX ) ; _Compile_SET_tttn_REG ( ZERO, 0, EAX ) ; // SET : this only sets one byte of reg _Compile_MOVZX_REG ( EAX ) ; // so Zero eXtend reg if ( compiler->Optimizer->Optimize_Rm != DSP ) // optimize sets this { Word *zero = Compiler_WordStack ( compiler, 0 ) ; _Word_CompileAndRecord_PushEAX ( zero ) ; } else Compile_Move_EAX_To_TOS ( DSP ) ; } else { if ( one->StackPushRegisterCode ) SetHere ( one->StackPushRegisterCode ) ; else Compile_Move_TOS_To_EAX ( DSP ) ; //_Compile_Move_StackN_To_Reg ( EAX, DSP, 0 ) ; _Compile_TEST_Reg_To_Reg ( EAX, EAX ) ; //logical and eax eax => if ( eax > 0 ) 1 else 0 _Compile_SET_tttn_REG ( ZERO, 0, EAX ) ; // if (eax == zero) => zero flag is 1 if zero flag is true (1) set EAX to 1 else set eax to 0 :: SET : this only sets one byte of reg _Compile_MOVZX_REG ( EAX ) ; // so Zero eXtend reg Word *zero = Compiler_WordStack ( compiler, 0 ) ; zero->StackPushRegisterCode = Here ; Compile_Move_EAX_To_TOS ( DSP ) ; //int a, b, c= 0, d ; a = 1; b = !a, d= !c ; Printf ( "a = %d b = %d c =%d ~d = %d", a, b, c, d ) ; } }
void _Compiler_RemoveLocalFrame ( Compiler * compiler ) { int32 parameterVarsSubAmount, returnValueFlag ; Compiler_SetLocalsFrameSize_AtItsCellOffset ( compiler ) ; parameterVarsSubAmount = ( compiler->NumberOfParameterVariables * CELL ) ; if ( GetState ( _Context_, C_SYNTAX ) && ( ! String_Equal ( compiler->CurrentWord->S_ContainingNamespace->Name, "void" ) ) ) { SetState ( compiler, RETURN_TOS, true ) ; } //returnValueFlag = rvf = ( _Context_->CurrentRunWord->CProperty & C_RETURN ) || ( GetState ( compiler, RETURN_TOS | RETURN_EAX ) ) || IsWordRecursive || compiler->ReturnVariableWord ; returnValueFlag = ( _Context_->CurrentlyRunningWord->CProperty & C_RETURN ) || ( GetState ( compiler, RETURN_TOS | RETURN_EAX ) ) || IsWordRecursive || compiler->ReturnVariableWord ; Word * word = compiler->ReturnVariableWord ; #if 0 if ( GetState ( _Context_->Compiler0, SAVE_ESP ) ) { _Compile_ESP_Restore ( ) ; } #endif if ( word ) { _Compile_GetVarLitObj_RValue_To_Reg ( word, EAX, 0 ) ; // nb. these variables have no lasting lvalue - they exist on the stack - therefore we can only return there rvalue } //else if ( compiler->NumberOfParameterVariables && returnValueFlag && ( ! compiler->NumberOfRegisterVariables ) && ( ! GetState ( compiler, RETURN_EAX ) ) ) else if ( compiler->NumberOfParameterVariables && returnValueFlag && ( ! GetState ( compiler, RETURN_EAX ) ) ) { #if 0 // for some unknown reason this code will overwrite the function call address also byte add_esi_eax [ ] = { 0x01, 0x06 } ; //0106 add [esi], eax byte * here = _Q_CodeByteArray->EndIndex ; if ( memcmp ( add_esi_eax, here - 2, 2 ) ) { //_ByteArray_UnAppendSpace ( _Q_CodeByteArray, 2 ) ; SetHere ( Here - 2 ) ; _Compile_X_Group1 ( ADD, REG, MEM, EAX, DSP, 0, 0, CELL ) ; // result is on TOS } //{returnValueFlag ++ ; rvf = 0 ;} //else #else Compile_Move_TOS_To_EAX ( DSP ) ; // save TOS to EAX so we can set return it as TOS below #endif //else { parameterVarsSubAmount += CELL ; rvf = 0 ; } } else if ( GetState ( compiler, RETURN_TOS ) ) { Compile_Move_TOS_To_EAX ( DSP ) ; } _Compile_LEA ( DSP, FP, 0, - LocalVarIndex_Disp ( 1 ) ) ; // restore sp - release locals stack frame _Compile_Move_StackN_To_Reg ( FP, DSP, 1 ) ; // restore the saved pre fp - cf AddLocalsFrame // remove the incoming parameters -- like in C parameterVarsSubAmount -= returnValueFlag * CELL ; // reduce the subtract amount to make room for the return value if ( parameterVarsSubAmount > 0 ) { Compile_SUBI ( REG, DSP, 0, parameterVarsSubAmount, CELL ) ; // remove stack variables } else if ( parameterVarsSubAmount < 0 ) { Compile_ADDI ( REG, DSP, 0, - parameterVarsSubAmount, CELL ) ; // add a place on the stack for return value } //if ( rvf && returnValueFlag && ( ! GetState ( compiler, RETURN_EAX ) ) ) if ( returnValueFlag && ( ! GetState ( compiler, RETURN_EAX ) ) ) { // nb : stack was already adjusted accordingly for this above by reducing the SUBI subAmount or adding if there weren't any parameter variables Compile_Move_EAX_To_TOS ( DSP ) ; } }