[239] | 1 | #include "stdafx.h"
|
---|
| 2 |
|
---|
| 3 | #include <CodeGenerator.h>
|
---|
| 4 |
|
---|
| 5 |
|
---|
[247] | 6 | void CodeGenerator::ResolveExitSubSchedule()
|
---|
| 7 | {
|
---|
| 8 | BOOST_FOREACH( long exitSubCodePosition, exitSubCodePositions )
|
---|
| 9 | {
|
---|
| 10 | pNativeCode->Overwrite( exitSubCodePosition, (long)( pNativeCode->GetSize()-(exitSubCodePosition+sizeof(long)) ) );
|
---|
| 11 | }
|
---|
| 12 |
|
---|
| 13 | // TODO: 未完成
|
---|
| 14 | BOOST_FOREACH( long exitSubCodePositionOld, _exitSubCodePositions_ObpOld )
|
---|
| 15 | {
|
---|
| 16 | extern int obp;
|
---|
| 17 | pNativeCode->OverwriteOld( exitSubCodePositionOld, (long)( obp-(exitSubCodePositionOld+sizeof(long)) ) );
|
---|
| 18 | }
|
---|
| 19 | }
|
---|
| 20 |
|
---|
[239] | 21 | void CodeGenerator::CheckUnresolveSchedule()
|
---|
| 22 | {
|
---|
| 23 | if( pertialSchedules.size() > 0 )
|
---|
| 24 | {
|
---|
| 25 | SetError();
|
---|
| 26 | }
|
---|
| 27 | }
|
---|
| 28 |
|
---|
[251] | 29 | void CodeGenerator::opfix( const PertialSchedule *pPertialSchedule, long newValue )
|
---|
| 30 | {
|
---|
| 31 | bool isSuccessful = false;
|
---|
[239] | 32 |
|
---|
[251] | 33 | PertialSchedules::iterator it = pertialSchedules.begin();
|
---|
| 34 | while( it != pertialSchedules.end() )
|
---|
| 35 | {
|
---|
| 36 | if( (*it) == pPertialSchedule )
|
---|
| 37 | {
|
---|
| 38 | if( pPertialSchedule->GetTypeSize() == sizeof(char) )
|
---|
| 39 | {
|
---|
| 40 | if( newValue < -128 || 127 < newValue )
|
---|
| 41 | {
|
---|
| 42 | // 範囲外
|
---|
| 43 | SetError();
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | pNativeCode->Overwrite( pPertialSchedule->GetCodePos(), (char)newValue );
|
---|
| 47 |
|
---|
| 48 | // TODO: 未完成(用が無くなったら消す)
|
---|
| 49 | pNativeCode->OverwriteOld( pPertialSchedule->GetObpOld(), (char)newValue );
|
---|
| 50 | }
|
---|
| 51 | else if( pPertialSchedule->GetTypeSize() == sizeof(long) )
|
---|
| 52 | {
|
---|
| 53 | pNativeCode->Overwrite( pPertialSchedule->GetCodePos(), newValue );
|
---|
| 54 |
|
---|
| 55 | // TODO: 未完成(用が無くなったら消す)
|
---|
| 56 | pNativeCode->OverwriteOld( pPertialSchedule->GetObpOld(), newValue );
|
---|
| 57 | }
|
---|
| 58 | else
|
---|
| 59 | {
|
---|
| 60 | SetError();
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | it = pertialSchedules.erase( it );
|
---|
| 64 | delete pPertialSchedule;
|
---|
| 65 |
|
---|
| 66 | isSuccessful = true;
|
---|
| 67 | }
|
---|
| 68 | else
|
---|
| 69 | {
|
---|
| 70 | it++;
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | if( isSuccessful == false )
|
---|
| 75 | {
|
---|
| 76 | SetError();
|
---|
| 77 | }
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 |
|
---|
[239] | 81 | // 分岐関連
|
---|
| 82 | void CodeGenerator::opfix_JmpPertialSchedule( const PertialSchedule *pPertialSchedule )
|
---|
| 83 | {
|
---|
| 84 | bool isSuccessful = false;
|
---|
| 85 |
|
---|
[248] | 86 | PertialSchedules::iterator it = pertialSchedules.begin();
|
---|
[239] | 87 | while( it != pertialSchedules.end() )
|
---|
| 88 | {
|
---|
| 89 | if( (*it) == pPertialSchedule )
|
---|
| 90 | {
|
---|
[241] | 91 | long newValue = pNativeCode->GetSize() - (pPertialSchedule->GetCodePos()+pPertialSchedule->GetTypeSize());
|
---|
| 92 |
|
---|
| 93 | extern int obp;
|
---|
| 94 | long newValueOld = obp - (pPertialSchedule->GetObpOld()+pPertialSchedule->GetTypeSize());
|
---|
| 95 |
|
---|
[239] | 96 | if( pPertialSchedule->GetTypeSize() == sizeof(char) )
|
---|
| 97 | {
|
---|
[241] | 98 | if( newValue < -128 || 127 < newValue )
|
---|
| 99 | {
|
---|
| 100 | // 範囲外
|
---|
| 101 | SetError();
|
---|
| 102 | }
|
---|
[239] | 103 |
|
---|
[241] | 104 | pNativeCode->Overwrite( pPertialSchedule->GetCodePos(), (char)newValue );
|
---|
| 105 |
|
---|
[239] | 106 | // TODO: 未完成(用が無くなったら消す)
|
---|
[241] | 107 | pNativeCode->OverwriteOld( pPertialSchedule->GetObpOld(), (char)newValueOld );
|
---|
[239] | 108 | }
|
---|
[241] | 109 | else if( pPertialSchedule->GetTypeSize() == sizeof(long) )
|
---|
| 110 | {
|
---|
| 111 | pNativeCode->Overwrite( pPertialSchedule->GetCodePos(), newValue );
|
---|
| 112 |
|
---|
| 113 | // TODO: 未完成(用が無くなったら消す)
|
---|
| 114 | pNativeCode->OverwriteOld( pPertialSchedule->GetObpOld(), newValueOld );
|
---|
| 115 | }
|
---|
[239] | 116 | else
|
---|
| 117 | {
|
---|
| 118 | SetError();
|
---|
| 119 | }
|
---|
| 120 |
|
---|
[245] | 121 | it = pertialSchedules.erase( it );
|
---|
[239] | 122 | delete pPertialSchedule;
|
---|
| 123 |
|
---|
| 124 | isSuccessful = true;
|
---|
| 125 | }
|
---|
| 126 | else
|
---|
| 127 | {
|
---|
| 128 | it++;
|
---|
| 129 | }
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | if( isSuccessful == false )
|
---|
| 133 | {
|
---|
| 134 | SetError();
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
[248] | 137 | const PertialSchedule *CodeGenerator::__jmp_op_format( char opcode, long offset, int op_size, bool isPertialSchedule, bool isSelfOpcodeOffset )
|
---|
[239] | 138 | {
|
---|
[241] | 139 | long beginCodePos = pNativeCode->GetSize();
|
---|
| 140 | {
|
---|
| 141 | // TODO: 未完成
|
---|
| 142 | extern int obp;
|
---|
| 143 | beginCodePos = obp;
|
---|
| 144 | }
|
---|
[239] | 145 |
|
---|
[241] | 146 | if( opcode == (char)0xEB )
|
---|
| 147 | {
|
---|
| 148 | // jmp命令のとき
|
---|
| 149 | if( op_size == sizeof(char) )
|
---|
| 150 | {
|
---|
| 151 | pNativeCode->Put( (char)opcode );
|
---|
| 152 | }
|
---|
| 153 | else if( op_size == sizeof(long) )
|
---|
| 154 | {
|
---|
| 155 | pNativeCode->Put( (char)0xE9 );
|
---|
| 156 | }
|
---|
| 157 | else
|
---|
| 158 | {
|
---|
| 159 | SetError();
|
---|
| 160 | }
|
---|
| 161 | }
|
---|
| 162 | else
|
---|
| 163 | {
|
---|
| 164 | if( op_size == sizeof(char) )
|
---|
| 165 | {
|
---|
| 166 | pNativeCode->Put( (char)((char)0x70 | opcode) );
|
---|
| 167 | }
|
---|
| 168 | else if( op_size == sizeof(long) )
|
---|
| 169 | {
|
---|
| 170 | pNativeCode->Put( (char)0x0F );
|
---|
| 171 | pNativeCode->Put( (char)((char)0x80 | opcode) );
|
---|
| 172 | }
|
---|
| 173 | else
|
---|
| 174 | {
|
---|
| 175 | SetError();
|
---|
| 176 | }
|
---|
| 177 | }
|
---|
| 178 |
|
---|
[245] | 179 | const PertialSchedule *pPertialSchedule = NULL;
|
---|
[239] | 180 | if( isPertialSchedule )
|
---|
| 181 | {
|
---|
| 182 | pertialSchedules.push_back( new PertialSchedule( pNativeCode->GetSize(), op_size ) );
|
---|
[251] | 183 | pPertialSchedule = pertialSchedules.back();
|
---|
[239] | 184 | }
|
---|
| 185 |
|
---|
[241] | 186 | if( isSelfOpcodeOffset )
|
---|
| 187 | {
|
---|
| 188 | // 自分自身の命令サイズを考慮する場合
|
---|
| 189 | //offset += ( pNativeCode->GetSize() - beginCodePos ) + op_size;
|
---|
| 190 |
|
---|
| 191 | // TODO: 未完成
|
---|
| 192 | extern int obp;
|
---|
| 193 | offset -= ( obp - beginCodePos ) + op_size;
|
---|
| 194 | }
|
---|
| 195 |
|
---|
[239] | 196 | if( op_size == sizeof(char) )
|
---|
| 197 | {
|
---|
| 198 | pNativeCode->Put( (char)offset );
|
---|
| 199 | }
|
---|
| 200 | else if( op_size == sizeof(long) )
|
---|
| 201 | {
|
---|
| 202 | pNativeCode->Put( offset );
|
---|
| 203 | }
|
---|
| 204 | else
|
---|
| 205 | {
|
---|
| 206 | SetError();
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | return pPertialSchedule;
|
---|
| 210 | }
|
---|
[250] | 211 | const PertialSchedule *CodeGenerator::op_jle( long offset, int op_size, bool isPertialSchedule, bool isSelfOpcodeOffset )
|
---|
[239] | 212 | {
|
---|
[250] | 213 | return __jmp_op_format( (char)0x0E, offset, op_size, isPertialSchedule, isSelfOpcodeOffset );
|
---|
[239] | 214 | }
|
---|
[250] | 215 | const PertialSchedule *CodeGenerator::op_jbe( long offset, int op_size, bool isPertialSchedule, bool isSelfOpcodeOffset )
|
---|
[239] | 216 | {
|
---|
[250] | 217 | return __jmp_op_format( (char)0x06, offset, op_size, isPertialSchedule, isSelfOpcodeOffset );
|
---|
[239] | 218 | }
|
---|
[250] | 219 | const PertialSchedule *CodeGenerator::op_jge( long offset, int op_size, bool isPertialSchedule, bool isSelfOpcodeOffset )
|
---|
[239] | 220 | {
|
---|
[250] | 221 | return __jmp_op_format( (char)0x0D, offset, op_size, isPertialSchedule, isSelfOpcodeOffset );
|
---|
[239] | 222 | }
|
---|
[250] | 223 | const PertialSchedule *CodeGenerator::op_jae( long offset, int op_size, bool isPertialSchedule, bool isSelfOpcodeOffset )
|
---|
[239] | 224 | {
|
---|
[250] | 225 | return __jmp_op_format( (char)0x03, offset, op_size, isPertialSchedule, isSelfOpcodeOffset );
|
---|
[239] | 226 | }
|
---|
[250] | 227 | const PertialSchedule *CodeGenerator::op_jl( long offset, int op_size, bool isPertialSchedule, bool isSelfOpcodeOffset )
|
---|
[239] | 228 | {
|
---|
[250] | 229 | return __jmp_op_format( (char)0x0C, offset, op_size, isPertialSchedule, isSelfOpcodeOffset );
|
---|
[239] | 230 | }
|
---|
[250] | 231 | const PertialSchedule *CodeGenerator::op_jb( long offset, int op_size, bool isPertialSchedule, bool isSelfOpcodeOffset )
|
---|
[239] | 232 | {
|
---|
[250] | 233 | return __jmp_op_format( (char)0x02, offset, op_size, isPertialSchedule, isSelfOpcodeOffset );
|
---|
[239] | 234 | }
|
---|
[250] | 235 | const PertialSchedule *CodeGenerator::op_jg( long offset, int op_size, bool isPertialSchedule, bool isSelfOpcodeOffset )
|
---|
[239] | 236 | {
|
---|
[250] | 237 | return __jmp_op_format( (char)0x0F, offset, op_size, isPertialSchedule, isSelfOpcodeOffset );
|
---|
[239] | 238 | }
|
---|
[250] | 239 | const PertialSchedule *CodeGenerator::op_ja( long offset, int op_size, bool isPertialSchedule, bool isSelfOpcodeOffset )
|
---|
[239] | 240 | {
|
---|
[250] | 241 | return __jmp_op_format( (char)0x07, offset, op_size, isPertialSchedule, isSelfOpcodeOffset );
|
---|
[239] | 242 | }
|
---|
[250] | 243 | const PertialSchedule *CodeGenerator::op_jne( long offset, int op_size, bool isPertialSchedule, bool isSelfOpcodeOffset )
|
---|
[239] | 244 | {
|
---|
[250] | 245 | return __jmp_op_format( (char)0x05, offset, op_size, isPertialSchedule, isSelfOpcodeOffset );
|
---|
[239] | 246 | }
|
---|
[250] | 247 | const PertialSchedule *CodeGenerator::op_je( long offset, int op_size, bool isPertialSchedule, bool isSelfOpcodeOffset )
|
---|
[239] | 248 | {
|
---|
[250] | 249 | return __jmp_op_format( (char)0x04, offset, op_size, isPertialSchedule, isSelfOpcodeOffset );
|
---|
[239] | 250 | }
|
---|
[248] | 251 | const PertialSchedule *CodeGenerator::op_jmp( long offset, int op_size, bool isPertialSchedule, bool isSelfOpcodeOffset )
|
---|
[239] | 252 | {
|
---|
[241] | 253 | return __jmp_op_format( (char)0xEB, offset, op_size, isPertialSchedule, isSelfOpcodeOffset );
|
---|
[239] | 254 | }
|
---|
[241] | 255 | void CodeGenerator::op_jmp_continue()
|
---|
| 256 | {
|
---|
| 257 | //op_jmp( GetContinueCodePos()-(pNativeCode->GetSize()+sizeof(long)), sizeof(long) );
|
---|
| 258 |
|
---|
| 259 | // TODO: 未完成(OpBuffer/obp廃止が整ったら上記のコードを有効にすべし)
|
---|
| 260 |
|
---|
| 261 | if( GetContinueCodePosOld() == -1 )
|
---|
| 262 | {
|
---|
| 263 | SetError(12,"Continue",cp);
|
---|
| 264 | return;
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 | extern int obp;
|
---|
| 268 | op_jmp( GetContinueCodePosOld()-obp, sizeof(long), false, true );
|
---|
| 269 | }
|
---|
[247] | 270 | void CodeGenerator::op_jmp_exitsub()
|
---|
| 271 | {
|
---|
| 272 | // オペコード
|
---|
| 273 | pNativeCode->Put( (char)0xE9 );
|
---|
| 274 |
|
---|
| 275 | exitSubCodePositions.push_back( pNativeCode->GetSize() );
|
---|
| 276 |
|
---|
| 277 | extern int obp;
|
---|
| 278 | _exitSubCodePositions_ObpOld.push_back( obp );
|
---|
| 279 |
|
---|
| 280 | pNativeCode->Put( (long)0 );
|
---|
| 281 | }
|
---|
[246] | 282 | void CodeGenerator::op_jmp_goto_schedule( const GotoLabelSchedule &gotoLabelSchedule )
|
---|
| 283 | {
|
---|
| 284 | // オペコード
|
---|
| 285 | pNativeCode->Put( (char)0xE9 );
|
---|
| 286 |
|
---|
| 287 | gotoLabelSchedules.push_back( gotoLabelSchedule );
|
---|
| 288 |
|
---|
| 289 | pNativeCode->Put( (long)0 );
|
---|
| 290 | }
|
---|