source: dev/trunk/ab5.0/abdev/BasicCompiler_Common/include/CodeGenerator.h@ 760

Last change on this file since 760 was 641, checked in by dai_9181, 16 years ago

・デバッグトレース時、グローバル領域の終端行でステップインまたはステップアウトしたときにデバッグ情報の取得に失敗して強制終了してしまう不具合を修正。
・グローバル領域のデバッグ実行ができなくなっている不具合を修正。

File size: 19.5 KB
Line 
1#pragma once
2
3#include <LexicalScope.h>
4
5#ifdef _AMD64_
6#include "../../compiler_x64/MachineFixed.h"
7#else
8#include "../../compiler_x86/MachineFixed.h"
9#endif
10
11// コード生成時の部分的なスケジューリング
12class PertialSchedule
13{
14 int codePos; // バッファ位置
15 int typeSize; // 対象サイズ(一般的には8bit/32bit)
16
17public:
18 PertialSchedule( int codePos, int typeSize )
19 : codePos( codePos )
20 , typeSize( typeSize )
21 {
22 }
23 ~PertialSchedule()
24 {
25 }
26
27 int GetCodePos() const
28 {
29 return codePos;
30 }
31 int GetTypeSize() const
32 {
33 return typeSize;
34 }
35};
36typedef std::vector<const PertialSchedule *> PertialSchedules;
37
38//Goto未知ラベル
39class GotoLabelSchedule : public PertialSchedule
40{
41 std::string name;
42 int line;
43 int sourceCodePos;
44public:
45 GotoLabelSchedule( const std::string &name, int nativeCodePos, int sourceCodePos )
46 : PertialSchedule( nativeCodePos, sizeof(long) )
47 , name( name )
48 , line( -1 )
49 , sourceCodePos( sourceCodePos )
50 {
51 }
52 GotoLabelSchedule( int line, int nativeCodePos, int sourceCodePos )
53 : PertialSchedule( nativeCodePos, sizeof(long) )
54 , line( line )
55 , sourceCodePos( sourceCodePos )
56 {
57 }
58 const std::string &GetName() const
59 {
60 return name;
61 }
62 int GetLineNum() const
63 {
64 return line;
65 }
66 int GetSourceCodePos() const
67 {
68 return sourceCodePos;
69 }
70};
71typedef std::vector<const GotoLabelSchedule *> GotoLabelSchedules;
72
73//ラベルアドレス
74class GotoLabel
75{
76public:
77 std::string name;
78 int line;
79 DWORD address;
80
81 GotoLabel( const std::string &name, long nativeCodePos )
82 : name( name )
83 , line( -1 )
84 , address( nativeCodePos )
85 {
86 }
87 GotoLabel( int line, long nativeCodePos )
88 : name( "" )
89 , line( line )
90 , address( nativeCodePos )
91 {
92 }
93};
94typedef std::vector<GotoLabel> GotoLabels;
95
96class LexicalScope
97{
98public:
99 enum SCOPE_TYPE{
100 // ベース
101 SCOPE_TYPE_BASE,
102
103 // 分岐
104 SCOPE_TYPE_IF,
105
106 // ループ
107 SCOPE_TYPE_DO,
108 SCOPE_TYPE_FOR,
109 SCOPE_TYPE_WHILE,
110
111 // ケース分け
112 SCOPE_TYPE_SELECT,
113
114 // 例外処理
115 SCOPE_TRY,
116 SCOPE_CATCH,
117 SCOPE_FINALLY,
118 };
119
120private:
121 int level;
122 int StartAddress;
123 SCOPE_TYPE TypeOfStatement;
124
125 PertialSchedules breakPertialSchedules;
126
127public:
128 LexicalScope( int level, int addr, SCOPE_TYPE TypeOfStatement )
129 : level( level )
130 , StartAddress( addr )
131 , TypeOfStatement( TypeOfStatement )
132 {
133 }
134 ~LexicalScope()
135 {
136 }
137
138 int GetStartAddress()
139 {
140 return StartAddress;
141 }
142 SCOPE_TYPE GetTypeOfStatement()
143 {
144 return TypeOfStatement;
145 }
146
147 void Break();
148 void RunScheduleOfBreak();
149};
150
151class LexicalScopes
152{
153 LexicalScope **ppScopes;
154 int level;
155
156public:
157
158 LexicalScopes(){
159 ppScopes = (LexicalScope **)malloc( 1 );
160 level=0;
161 }
162 ~LexicalScopes(){
163 free( ppScopes );
164 }
165
166 //初期化(関数コンパイルの開始時に呼び出される)
167 void Init(int addr);
168
169 // スコープを開始
170 void Start( int addr, LexicalScope::SCOPE_TYPE TypeOfStatement );
171
172 // スコープを検索
173 LexicalScope *SearchScope( LexicalScope::SCOPE_TYPE TypeOfStatement );
174
175 int GetNowLevel(void);
176 void SetNowLevel( int level );
177 int GetStartAddress(void);
178
179 void End();
180
181 //スコープ終了時のデストラクタ呼び出し
182 void CallDestructorsOfScopeEnd();
183
184 //Returnステートメント用のデストラクタ呼び出し
185 void CallDestructorsOfReturn( int BaseLevel = 0 );
186};
187
188class CodeGenerator
189{
190 NativeCode *pNativeCode;
191
192private:
193 // 部分スケジュールの管理
194 PertialSchedules pertialSchedules;
195
196 // Continue用のコード位置情報の管理
197 std::vector<long> continueCodePositions;
198
199 // コンパイル中のステップにおいて、構造体の一時オブジェクトの解放が必要かどうか
200 bool isNeedFreeTempStructureInCurrentStep;
201
202public:
203
204 // ローカル変数用スケジュールの管理
205 PertialSchedules localVarPertialSchedules;
206
207 // Exit Subスケジュールの管理
208 std::vector<long> exitSubCodePositions;
209
210 // Gotoスケジュールの管理
211 GotoLabels gotoLabels;
212 GotoLabelSchedules gotoLabelSchedules;
213
214 // レキシカルスコープの管理
215 LexicalScopes lexicalScopes;
216
217 CodeGenerator()
218 : pNativeCode( 0 )
219 , isNeedFreeTempStructureInCurrentStep( false )
220 {
221 }
222 ~CodeGenerator()
223 {
224 if( pNativeCode )
225 {
226 CheckUnresolveSchedule();
227 }
228 }
229
230 void Select( NativeCode &nativeCode )
231 {
232 if( pNativeCode )
233 {
234 CheckUnresolveSchedule();
235 }
236 pNativeCode = &nativeCode;
237 }
238 long GetNativeCodeSize() const
239 {
240 return pNativeCode->GetSize();
241 }
242
243 void NextSourceLine( const SourceCodePosition &sourceCodePosition, bool isInSystemProc )
244 {
245 pNativeCode->NextSourceLine( sourceCodePosition, isInSystemProc );
246 }
247
248 long GetContinueCodePos() const
249 {
250 if( continueCodePositions.size() == 0 )
251 {
252 return -1;
253 }
254 return continueCodePositions[continueCodePositions.size()-1];
255 }
256 void ClearContinueArea()
257 {
258 continueCodePositions.clear();
259 }
260 void ContinueAreaBegin()
261 {
262 continueCodePositions.push_back( pNativeCode->GetSize() );
263 }
264 void ContinueAreaEnd()
265 {
266 continueCodePositions.pop_back();
267 }
268
269 void PutWithSchedule( long l, Schedule::Type scheduleType );
270
271 void ResolveExitSubSchedule();
272
273 void CheckUnresolveSchedule();
274
275 void opfix( const PertialSchedule *pPertialSchedule, _int64 newValue );
276 void opfix_offset( const PertialSchedule *pPertialSchedule, long offset );
277 void opfix_JmpPertialSchedule( const PertialSchedule *pPertialSchedule );
278
279
280 /////////////////////////////////////////////////////////////////
281 // 32bit/64bit共通 機械語生成
282 /////////////////////////////////////////////////////////////////
283
284private:
285 const PertialSchedule *__jmp_op_format( char opcode, long offset, int op_size, bool isPertialSchedule = false, bool isSelfOpcodeOffset = false );
286public:
287 const PertialSchedule *op_jle( long offset, int op_size = sizeof(char), bool isPertialSchedule = false, bool isSelfOpcodeOffset = false );
288 const PertialSchedule *op_jbe( long offset, int op_size = sizeof(char), bool isPertialSchedule = false, bool isSelfOpcodeOffset = false );
289 const PertialSchedule *op_jge( long offset, int op_size = sizeof(char), bool isPertialSchedule = false, bool isSelfOpcodeOffset = false );
290 const PertialSchedule *op_jae( long offset, int op_size = sizeof(char), bool isPertialSchedule = false, bool isSelfOpcodeOffset = false );
291 const PertialSchedule *op_jl( long offset, int op_size = sizeof(char), bool isPertialSchedule = false, bool isSelfOpcodeOffset = false );
292 const PertialSchedule *op_jb( long offset, int op_size = sizeof(char), bool isPertialSchedule = false, bool isSelfOpcodeOffset = false );
293 const PertialSchedule *op_jg( long offset, int op_size = sizeof(char), bool isPertialSchedule = false, bool isSelfOpcodeOffset = false );
294 const PertialSchedule *op_ja( long offset, int op_size = sizeof(char), bool isPertialSchedule = false, bool isSelfOpcodeOffset = false );
295 const PertialSchedule *op_jne( long offset, int op_size = sizeof(char), bool isPertialSchedule = false, bool isSelfOpcodeOffset = false );
296 const PertialSchedule *op_je( long offset, int op_size = sizeof(char), bool isPertialSchedule = false, bool isSelfOpcodeOffset = false );
297 const PertialSchedule *op_jmp( long offset, int op_size = sizeof(char), bool isPertialSchedule = false, bool isSelfOpcodeOffset = false );
298 void op_jmp_continue();
299 void op_jmp_exitsub();
300 void op_jmp_goto_schedule( const std::string &name, int lineNum, int sourceCodePos );
301 void op_AddNeedFreeTempStructure( int reg );
302 void op_FreeTempStructure();
303
304
305#ifdef _AMD64_
306 /////////////////////////////////////////////////////////////////
307 // 64ビット機械語生成
308 /////////////////////////////////////////////////////////////////
309private:
310 void set_rex(int op_size,int reg,int index_reg,int base_reg);
311 const PertialSchedule *set_mod_rm_sib_disp(char mod,int reg,int scale,int index_reg,int base_reg,long disp, Schedule::Type scheduleType = Schedule::None, bool isPertialSchedule = false );
312 const PertialSchedule *__op_format(int op_size,char op_prefix,char opcode1,char opcode2,int reg,int base_reg,long offset,char mod, Schedule::Type scheduleType = Schedule::None, bool isPertialSchedule = false );
313public:
314 const PertialSchedule *op_mov_RV (int op_size,int reg,long i32data, Schedule::Type scheduleType = Schedule::None, bool isPertialSchedule = false );
315 const PertialSchedule *op_mov_RV64 ( int reg, _int64 i64data, bool isPertialSchedule = false );
316 const PertialSchedule *op_mov_RM (int op_size,int reg,int base_reg,long offset,char mod, Schedule::Type scheduleType = Schedule::None, bool isPertialSchedule = false );
317 const PertialSchedule *op_mov_RM_ex (int op_size,int reg,int base_reg1,int base_reg2,long offset,BOOL bUseOffset, Schedule::Type scheduleType = Schedule::None, bool isPertialSchedule = false );
318 const PertialSchedule *op_mov_MR (int op_size,int reg,int base_reg,long offset,char mod, Schedule::Type scheduleType = Schedule::None, bool isPertialSchedule = false );
319 const PertialSchedule *op_mov_MR_ex (int op_size,int reg,int base_reg1,int base_reg2,long offset,BOOL bUseOffset, Schedule::Type scheduleType = Schedule::None, bool isPertialSchedule = false );
320 const PertialSchedule *op_mov_MV (int op_size,int base_reg,int offset, Schedule::Type offsetScheduleType, bool isPertialSchedule, BOOL bUseOffset,long i32data);
321 void op_mov_RR (int reg1,int reg2);
322 void op_movsxd (int reg64,int reg32);
323 void op_movsx64_FromReg16 (int reg64,int reg16);
324 void op_movsx64_FromReg8 (int reg64,int reg8);
325 void op_movsx32_FromReg16 (int reg32,int reg16);
326 void op_movsx32_FromReg8 (int reg32,int reg8);
327 void op_movsx16_FromReg8 (int reg32,int reg8);
328 void op_cqo ();
329 void op_inc (int reg);
330 void op_dec (int reg);
331 const PertialSchedule *op_add_RM (int op_size,int reg,int base_reg,int offset,char mod, Schedule::Type scheduleType = Schedule::None, bool isPertialSchedule = false );
332 const PertialSchedule *op_add_RV (int reg,long offset, Schedule::Type scheduleType = Schedule::None, bool isPertialSchedule = false );
333 void op_add_RR (int reg1,int reg2);
334 void op_add32_reg (int reg1,int reg2);
335 void op_sub_RV (int op_size,int reg,long i32data);
336 void op_sub64_reg (int reg1,int reg2);
337 void op_sub32_reg (int reg1,int reg2);
338 void op_sbb_RR ( int op_size, int reg1, int reg2 );
339 void op_imul_RR (int op_size,int reg1,int reg2);
340 void op_imul_RV (int op_size,int reg,long i32data);
341 void op_div64_reg (int reg);
342 void op_idiv64_reg (int reg);
343 void op_shl_reg (int op_size,int reg);
344 void op_sar_reg (int op_size,int reg);
345 void op_shr_reg (int op_size,int reg);
346 void op_and_reg (int op_size,int reg1,int reg2);
347 void op_and64_value (int reg,long offset);
348 void op_and32_value (int reg,long offset);
349 void op_or_reg (int op_size,int reg1,int reg2);
350 void op_xor_reg (int op_size,int reg1,int reg2);
351 void op_not_reg (int op_size,int reg);
352 void op_neg ( int reg );
353 void op_test (int reg1,int reg2);
354 void op_cmp_reg (int op_size,int reg1,int reg2);
355 void op_cmp_value (int op_size,int reg,char byte_data);
356 void op_setne (int reg);
357 const PertialSchedule *op_movlpd_MR (int xmm_reg,int base_reg,int offset,char mod, Schedule::Type scheduleType = Schedule::None, bool isPertialSchedule = false );
358 const PertialSchedule *op_movlpd_RM (int xmm_reg,int base_reg,int offset,char mod, Schedule::Type scheduleType = Schedule::None, bool isPertialSchedule = false );
359 void op_movsd_RR (int xmm_reg1,int xmm_reg2);
360 const PertialSchedule *op_movsd_MR (int xmm_reg,int base_reg,int offset,char mod, Schedule::Type scheduleType = Schedule::None, bool isPertialSchedule = false );
361 void op_movss_RR (int xmm_reg1,int xmm_reg2);
362 const PertialSchedule *op_movss_RM (int xmm_reg,int base_reg,int offset,char mod, Schedule::Type scheduleType = Schedule::None, bool isPertialSchedule = false );
363 const PertialSchedule *op_movss_MR (int xmm_reg,int base_reg,int offset,char mod, Schedule::Type scheduleType = Schedule::None, bool isPertialSchedule = false );
364 void op_movd_RX (int reg,int xmm_reg);
365 void op_cvtsd2ss (int xmm_reg1,int xmm_reg2);
366 void op_cvtss2sd (int xmm_reg1,int xmm_reg2);
367 void op_cvttsd2si_xmm (int op_size,int reg,int xmm_reg);
368 void op_cvttss2si_xmm (int op_size,int reg,int xmm_reg);
369 void op_cvtsi2sd_reg (int op_size,int xmm_reg,int reg);
370 void op_cvtsi2ss_reg (int op_size,int xmm_reg,int reg);
371 void op_comisd (int xmm_reg1,int xmm_reg2);
372 void op_comiss (int xmm_reg1,int xmm_reg2);
373 void op_rep_movs (int op_size);
374 void op_add_rsp(long num);
375 const PertialSchedule *op_sub_rsp( long num, bool isPertialSchedule = false );
376 void op_fld_ptr_esp(int type);
377 void op_zero_reg(int reg);
378
379 void op_call( const UserProc *pUserProc );
380 void op_call( const DllProc *pDllProc );
381 void op_ret();
382 void op_addressof( int reg, const UserProc *pUserProc );
383 void op_mov_RV_com_vtbl( int reg, const CClass *pClass );
384 void op_mov_RV_vtbl( int reg, const CClass *pClass );
385
386#else
387 /////////////////////////////////////////////////////////////////
388 // 32ビット機械語生成
389 /////////////////////////////////////////////////////////////////
390private:
391 const PertialSchedule *set_mod_rm_sib_disp(char mod,int reg,int scale,int index_reg,int base_reg,long disp, Schedule::Type scheduleType, bool isPertialSchedule );
392 void __op_format(char op_prefix,char opcode,int reg);
393 const PertialSchedule *__op_format(char op_prefix,char opcode1,char opcode2,int reg,int base_reg,int offset,char mod, Schedule::Type scheduleType = Schedule::None, bool isPertialSchedule = false );
394public:
395 const PertialSchedule *op_mov_MV ( int op_size, int base_reg, long offset, Schedule::Type offsetScheduleType, bool isPertialSchedule, long value, Schedule::Type valueScheduleType = Schedule::None );
396 const PertialSchedule *op_mov_RV ( int reg, long offset, Schedule::Type scheduleType = Schedule::None, bool isPertialSchedule = false );
397 void op_mov_RR ( int reg1,int reg2);
398 const PertialSchedule *op_mov_RM ( int op_size,int reg,int base_reg,int offset,char mod, Schedule::Type scheduleType = Schedule::None, bool isPertialSchedule = false );
399 const PertialSchedule *op_mov_RM_ex ( int op_size,int reg,int base_reg1,int base_reg2,long offset,BOOL bUseOffset, Schedule::Type scheduleType = Schedule::None, bool isPertialSchedule = false );
400 const PertialSchedule *op_mov_MR ( int op_size,int reg,int base_reg,int offset,char mod, Schedule::Type scheduleType = Schedule::None, bool isPertialSchedule = false );
401 const PertialSchedule *op_mov_MR_ex ( int op_size,int reg,int base_reg1,int base_reg2,long offset,BOOL bUseOffset, Schedule::Type scheduleType = Schedule::None, bool isPertialSchedule = false );
402 void op_movsx_R32R16 ( int reg32,int reg16 = REG_NON);
403 void op_movsx_R32R8 ( int reg32,int reg8 = REG_NON);
404 void op_movsx_R16R8 ( int reg16,int reg8 = REG_NON);
405 const PertialSchedule *op_lea_RM ( int reg, int base_reg, long offset, char mod, Schedule::Type scheduleType = Schedule::None, bool isPertialSchedule = false );
406 void op_inc (int reg);
407 void op_dec (int reg);
408 void op_add_RV8 (int reg,char cValue);
409 const PertialSchedule *op_add_RV ( int reg, long offset, Schedule::Type scheduleType = Schedule::None, bool isPertialSchedule = false );
410 void op_add_RR ( int reg1, int reg2 );
411 const PertialSchedule *op_add_RM (int op_size,int reg,int base_reg,int offset,char mod, Schedule::Type scheduleType = Schedule::None, bool isPertialSchedule = false );
412 void op_adc_RV8 (int reg,char cValue);
413 void op_adc_RR ( int reg1, int reg2 );
414 void op_sub_RV8 (int reg,char cValue);
415 void op_sub_RR ( int reg1, int reg2 );
416 void op_sbb_RV8 (int reg,char cValue);
417 void op_sbb_RR ( int reg1, int reg2 );
418 void op_imul_RR (int reg1,int reg2);
419 void op_imul_RV (int reg,long i32data);
420 void op_imul_RV8 (int reg,char cValue);
421 void op_div_R ( int reg );
422 void op_idiv_R ( int reg );
423 void op_and_RV (int reg,long value);
424 void op_and_RR ( int reg1, int reg2 );
425 void op_or_RR ( int op_size, int reg1, int reg2 );
426 void op_xor_RR ( int reg1, int reg2 = REG_NON );
427 void op_neg ( int reg );
428 void op_cdq ();
429
430 void op_rep_movs (int op_size);
431
432 void op_push(int reg);
433 void op_push_V( long data, Schedule::Type scheduleType = Schedule::None );
434 void op_push_M( int base_reg );
435 const PertialSchedule *op_push_M( int base_reg, long offset, Schedule::Type scheduleType = Schedule::None, bool isPertialSchedule = false );
436 void op_pop(int reg = REG_NON);
437 void op_add_esp(long num);
438 const PertialSchedule *op_sub_esp( long num, bool isPertialSchedule = false );
439 void op_cmp_RR( int reg1, int reg2 );
440 void op_cmp_value(int op_size,int reg,char byte_data);
441 void op_setne( int reg );
442 void op_test(int reg1,int reg2);
443 void op_test_ah( char cValue );
444 void op_fld_ptr_esp(int type);
445 void op_fld_basereg (int type,int base_reg);
446 const PertialSchedule *op_fld_base_offset (int type,int base_reg,long offset, Schedule::Type scheduleType = Schedule::None, bool isPertialSchedule = false );
447 const PertialSchedule *op_fld_base_offset_ex (int type,int base_reg1,int base_reg2,long offset,BOOL bUseOffset, Schedule::Type scheduleType = Schedule::None, bool isPertialSchedule = false );
448 void op_fstp_basereg (int type,int base_reg);
449 const PertialSchedule *op_fstp_base_offset (int type,int base_reg,long offset, Schedule::Type scheduleType = Schedule::None, bool isPertialSchedule = false );
450 const PertialSchedule *op_fstp_base_offset_ex (int type,int base_reg1,int base_reg2,long offset,BOOL bUseOffset, Schedule::Type scheduleType = Schedule::None, bool isPertialSchedule = false );
451 void op_fistp_ptr_esp ( int typeSize );
452 void op_fstp_push ( Type &type );
453 void op_fcompp();
454 void op_fnstsw_ax();
455 void op_zero_reg(int reg);
456 void fpu_cast();
457 void fpu_cast_end();
458
459 void op_call_R( int reg );
460 void op_call(const UserProc *pUserProc);
461 void op_call(const DllProc *pDllProc);
462 void op_ret();
463 void op_ret( short stackFrameSize );
464 void op_addressof( int reg, const UserProc *pUserProc );
465 void op_mov_RV_com_vtbl( int reg, const CClass *pClass );
466 void op_mov_RV_vtbl( int reg, const CClass *pClass );
467#endif
468
469
470
471 void PutOld( long l, Schedule::Type scheduleType )
472 {
473 this->PutWithSchedule( l, scheduleType );
474 }
475 const PertialSchedule *PutOld( long l, bool isPertialSchedule )
476 {
477 const PertialSchedule *pPertialSchedule;
478 if( isPertialSchedule )
479 {
480 pertialSchedules.push_back( new PertialSchedule( pNativeCode->GetSize(), sizeof(long) ) );
481 pPertialSchedule = pertialSchedules.back();
482 }
483 pNativeCode->Put( l );
484 return pPertialSchedule;
485 }
486 void PutOld( const NativeCode &nativeCode )
487 {
488 pNativeCode->PutEx( nativeCode );
489 }
490 void PutOld( char c )
491 {
492 pNativeCode->Put( c );
493 }
494 void PutOld( char c1, char c2 )
495 {
496 pNativeCode->Put( c1 );
497 pNativeCode->Put( c2 );
498 }
499 void PutOld( char c1, char c2, char c3 )
500 {
501 pNativeCode->Put( c1 );
502 pNativeCode->Put( c2 );
503 pNativeCode->Put( c3 );
504 }
505 void PutOld( char c1, char c2, char c3, char c4 )
506 {
507 pNativeCode->Put( c1 );
508 pNativeCode->Put( c2 );
509 pNativeCode->Put( c3 );
510 pNativeCode->Put( c4 );
511 }
512 void PutOld( char c1, char c2, char c3, long l )
513 {
514 pNativeCode->Put( c1 );
515 pNativeCode->Put( c2 );
516 pNativeCode->Put( c3 );
517 pNativeCode->Put( l );
518 }
519 void PutOld( char c1, char c2, char c3, char c4, char c5 )
520 {
521 pNativeCode->Put( c1 );
522 pNativeCode->Put( c2 );
523 pNativeCode->Put( c3 );
524 pNativeCode->Put( c4 );
525 pNativeCode->Put( c5 );
526 }
527 void PutOld( char c1, char c2, char c3, char c4, char c5, char c6 )
528 {
529 pNativeCode->Put( c1 );
530 pNativeCode->Put( c2 );
531 pNativeCode->Put( c3 );
532 pNativeCode->Put( c4 );
533 pNativeCode->Put( c5 );
534 pNativeCode->Put( c6 );
535 }
536};
Note: See TracBrowser for help on using the repository browser.