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

Last change on this file since 625 was 597, 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( int currentSourceIndex )
244 {
245 extern int cp;
246 pNativeCode->NextSourceLine( currentSourceIndex, cp );
247 }
248
249 long GetContinueCodePos() const
250 {
251 if( continueCodePositions.size() == 0 )
252 {
253 return -1;
254 }
255 return continueCodePositions[continueCodePositions.size()-1];
256 }
257 void ClearContinueArea()
258 {
259 continueCodePositions.clear();
260 }
261 void ContinueAreaBegin()
262 {
263 continueCodePositions.push_back( pNativeCode->GetSize() );
264 }
265 void ContinueAreaEnd()
266 {
267 continueCodePositions.pop_back();
268 }
269
270 void PutWithSchedule( long l, Schedule::Type scheduleType );
271
272 void ResolveExitSubSchedule();
273
274 void CheckUnresolveSchedule();
275
276 void opfix( const PertialSchedule *pPertialSchedule, _int64 newValue );
277 void opfix_offset( const PertialSchedule *pPertialSchedule, long offset );
278 void opfix_JmpPertialSchedule( const PertialSchedule *pPertialSchedule );
279
280
281 /////////////////////////////////////////////////////////////////
282 // 32bit/64bit共通 機械語生成
283 /////////////////////////////////////////////////////////////////
284
285private:
286 const PertialSchedule *__jmp_op_format( char opcode, long offset, int op_size, bool isPertialSchedule = false, bool isSelfOpcodeOffset = false );
287public:
288 const PertialSchedule *op_jle( long offset, int op_size = sizeof(char), bool isPertialSchedule = false, bool isSelfOpcodeOffset = false );
289 const PertialSchedule *op_jbe( long offset, int op_size = sizeof(char), bool isPertialSchedule = false, bool isSelfOpcodeOffset = false );
290 const PertialSchedule *op_jge( long offset, int op_size = sizeof(char), bool isPertialSchedule = false, bool isSelfOpcodeOffset = false );
291 const PertialSchedule *op_jae( long offset, int op_size = sizeof(char), bool isPertialSchedule = false, bool isSelfOpcodeOffset = false );
292 const PertialSchedule *op_jl( long offset, int op_size = sizeof(char), bool isPertialSchedule = false, bool isSelfOpcodeOffset = false );
293 const PertialSchedule *op_jb( long offset, int op_size = sizeof(char), bool isPertialSchedule = false, bool isSelfOpcodeOffset = false );
294 const PertialSchedule *op_jg( long offset, int op_size = sizeof(char), bool isPertialSchedule = false, bool isSelfOpcodeOffset = false );
295 const PertialSchedule *op_ja( long offset, int op_size = sizeof(char), bool isPertialSchedule = false, bool isSelfOpcodeOffset = false );
296 const PertialSchedule *op_jne( long offset, int op_size = sizeof(char), bool isPertialSchedule = false, bool isSelfOpcodeOffset = false );
297 const PertialSchedule *op_je( long offset, int op_size = sizeof(char), bool isPertialSchedule = false, bool isSelfOpcodeOffset = false );
298 const PertialSchedule *op_jmp( long offset, int op_size = sizeof(char), bool isPertialSchedule = false, bool isSelfOpcodeOffset = false );
299 void op_jmp_continue();
300 void op_jmp_exitsub();
301 void op_jmp_goto_schedule( const std::string &name, int lineNum, int sourceCodePos );
302 void op_AddNeedFreeTempStructure( int reg );
303 void op_FreeTempStructure();
304
305
306#ifdef _AMD64_
307 /////////////////////////////////////////////////////////////////
308 // 64ビット機械語生成
309 /////////////////////////////////////////////////////////////////
310private:
311 void set_rex(int op_size,int reg,int index_reg,int base_reg);
312 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 );
313 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 );
314public:
315 const PertialSchedule *op_mov_RV (int op_size,int reg,long i32data, Schedule::Type scheduleType = Schedule::None, bool isPertialSchedule = false );
316 const PertialSchedule *op_mov_RV64 ( int reg, _int64 i64data, bool isPertialSchedule = false );
317 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 );
318 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 );
319 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 );
320 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 );
321 const PertialSchedule *op_mov_MV (int op_size,int base_reg,int offset, Schedule::Type offsetScheduleType, bool isPertialSchedule, BOOL bUseOffset,long i32data);
322 void op_mov_RR (int reg1,int reg2);
323 void op_movsxd (int reg64,int reg32);
324 void op_movsx64_FromReg16 (int reg64,int reg16);
325 void op_movsx64_FromReg8 (int reg64,int reg8);
326 void op_movsx32_FromReg16 (int reg32,int reg16);
327 void op_movsx32_FromReg8 (int reg32,int reg8);
328 void op_movsx16_FromReg8 (int reg32,int reg8);
329 void op_cqo ();
330 void op_inc (int reg);
331 void op_dec (int reg);
332 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 );
333 const PertialSchedule *op_add_RV (int reg,long offset, Schedule::Type scheduleType = Schedule::None, bool isPertialSchedule = false );
334 void op_add_RR (int reg1,int reg2);
335 void op_add32_reg (int reg1,int reg2);
336 void op_sub_RV (int op_size,int reg,long i32data);
337 void op_sub64_reg (int reg1,int reg2);
338 void op_sub32_reg (int reg1,int reg2);
339 void op_sbb_RR ( int op_size, int reg1, int reg2 );
340 void op_imul_RR (int op_size,int reg1,int reg2);
341 void op_imul_RV (int op_size,int reg,long i32data);
342 void op_div64_reg (int reg);
343 void op_idiv64_reg (int reg);
344 void op_shl_reg (int op_size,int reg);
345 void op_sar_reg (int op_size,int reg);
346 void op_shr_reg (int op_size,int reg);
347 void op_and_reg (int op_size,int reg1,int reg2);
348 void op_and64_value (int reg,long offset);
349 void op_and32_value (int reg,long offset);
350 void op_or_reg (int op_size,int reg1,int reg2);
351 void op_xor_reg (int op_size,int reg1,int reg2);
352 void op_not_reg (int op_size,int reg);
353 void op_neg ( int reg );
354 void op_test (int reg1,int reg2);
355 void op_cmp_reg (int op_size,int reg1,int reg2);
356 void op_cmp_value (int op_size,int reg,char byte_data);
357 void op_setne (int reg);
358 const PertialSchedule *op_movlpd_MR (int xmm_reg,int base_reg,int offset,char mod, Schedule::Type scheduleType = Schedule::None, bool isPertialSchedule = false );
359 const PertialSchedule *op_movlpd_RM (int xmm_reg,int base_reg,int offset,char mod, Schedule::Type scheduleType = Schedule::None, bool isPertialSchedule = false );
360 void op_movsd_RR (int xmm_reg1,int xmm_reg2);
361 const PertialSchedule *op_movsd_MR (int xmm_reg,int base_reg,int offset,char mod, Schedule::Type scheduleType = Schedule::None, bool isPertialSchedule = false );
362 void op_movss_RR (int xmm_reg1,int xmm_reg2);
363 const PertialSchedule *op_movss_RM (int xmm_reg,int base_reg,int offset,char mod, Schedule::Type scheduleType = Schedule::None, bool isPertialSchedule = false );
364 const PertialSchedule *op_movss_MR (int xmm_reg,int base_reg,int offset,char mod, Schedule::Type scheduleType = Schedule::None, bool isPertialSchedule = false );
365 void op_movd_RX (int reg,int xmm_reg);
366 void op_cvtsd2ss (int xmm_reg1,int xmm_reg2);
367 void op_cvtss2sd (int xmm_reg1,int xmm_reg2);
368 void op_cvttsd2si_xmm (int op_size,int reg,int xmm_reg);
369 void op_cvttss2si_xmm (int op_size,int reg,int xmm_reg);
370 void op_cvtsi2sd_reg (int op_size,int xmm_reg,int reg);
371 void op_cvtsi2ss_reg (int op_size,int xmm_reg,int reg);
372 void op_comisd (int xmm_reg1,int xmm_reg2);
373 void op_comiss (int xmm_reg1,int xmm_reg2);
374 void op_rep_movs (int op_size);
375 void op_add_rsp(long num);
376 const PertialSchedule *op_sub_rsp( long num, bool isPertialSchedule = false );
377 void op_fld_ptr_esp(int type);
378 void op_zero_reg(int reg);
379
380 void op_call( const UserProc *pUserProc );
381 void op_call( const DllProc *pDllProc );
382 void op_ret();
383 void op_addressof( int reg, const UserProc *pUserProc );
384 void op_mov_RV_com_vtbl( int reg, const CClass *pClass );
385 void op_mov_RV_vtbl( int reg, const CClass *pClass );
386
387#else
388 /////////////////////////////////////////////////////////////////
389 // 32ビット機械語生成
390 /////////////////////////////////////////////////////////////////
391private:
392 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 );
393 void __op_format(char op_prefix,char opcode,int reg);
394 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 );
395public:
396 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 );
397 const PertialSchedule *op_mov_RV ( int reg, long offset, Schedule::Type scheduleType = Schedule::None, bool isPertialSchedule = false );
398 void op_mov_RR ( int reg1,int reg2);
399 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 );
400 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 );
401 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 );
402 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 );
403 void op_movsx_R32R16 ( int reg32,int reg16 = REG_NON);
404 void op_movsx_R32R8 ( int reg32,int reg8 = REG_NON);
405 void op_movsx_R16R8 ( int reg16,int reg8 = REG_NON);
406 const PertialSchedule *op_lea_RM ( int reg, int base_reg, long offset, char mod, Schedule::Type scheduleType = Schedule::None, bool isPertialSchedule = false );
407 void op_inc (int reg);
408 void op_dec (int reg);
409 void op_add_RV8 (int reg,char cValue);
410 const PertialSchedule *op_add_RV ( int reg, long offset, Schedule::Type scheduleType = Schedule::None, bool isPertialSchedule = false );
411 void op_add_RR ( int reg1, int reg2 );
412 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 );
413 void op_adc_RV8 (int reg,char cValue);
414 void op_adc_RR ( int reg1, int reg2 );
415 void op_sub_RV8 (int reg,char cValue);
416 void op_sub_RR ( int reg1, int reg2 );
417 void op_sbb_RV8 (int reg,char cValue);
418 void op_sbb_RR ( int reg1, int reg2 );
419 void op_imul_RR (int reg1,int reg2);
420 void op_imul_RV (int reg,long i32data);
421 void op_imul_RV8 (int reg,char cValue);
422 void op_div_R ( int reg );
423 void op_idiv_R ( int reg );
424 void op_and_RV (int reg,long value);
425 void op_and_RR ( int reg1, int reg2 );
426 void op_or_RR ( int op_size, int reg1, int reg2 );
427 void op_xor_RR ( int reg1, int reg2 = REG_NON );
428 void op_neg ( int reg );
429 void op_cdq ();
430
431 void op_rep_movs (int op_size);
432
433 void op_push(int reg);
434 void op_push_V( long data, Schedule::Type scheduleType = Schedule::None );
435 void op_push_M( int base_reg );
436 const PertialSchedule *op_push_M( int base_reg, long offset, Schedule::Type scheduleType = Schedule::None, bool isPertialSchedule = false );
437 void op_pop(int reg = REG_NON);
438 void op_add_esp(long num);
439 const PertialSchedule *op_sub_esp( long num, bool isPertialSchedule = false );
440 void op_cmp_RR( int reg1, int reg2 );
441 void op_cmp_value(int op_size,int reg,char byte_data);
442 void op_setne( int reg );
443 void op_test(int reg1,int reg2);
444 void op_test_ah( char cValue );
445 void op_fld_ptr_esp(int type);
446 void op_fld_basereg (int type,int base_reg);
447 const PertialSchedule *op_fld_base_offset (int type,int base_reg,long offset, Schedule::Type scheduleType = Schedule::None, bool isPertialSchedule = false );
448 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 );
449 void op_fstp_basereg (int type,int base_reg);
450 const PertialSchedule *op_fstp_base_offset (int type,int base_reg,long offset, Schedule::Type scheduleType = Schedule::None, bool isPertialSchedule = false );
451 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 );
452 void op_fistp_ptr_esp ( int typeSize );
453 void op_fstp_push ( Type &type );
454 void op_fcompp();
455 void op_fnstsw_ax();
456 void op_zero_reg(int reg);
457 void fpu_cast();
458 void fpu_cast_end();
459
460 void op_call_R( int reg );
461 void op_call(const UserProc *pUserProc);
462 void op_call(const DllProc *pDllProc);
463 void op_ret();
464 void op_ret( short stackFrameSize );
465 void op_addressof( int reg, const UserProc *pUserProc );
466 void op_mov_RV_com_vtbl( int reg, const CClass *pClass );
467 void op_mov_RV_vtbl( int reg, const CClass *pClass );
468#endif
469
470
471
472 void PutOld( long l, Schedule::Type scheduleType )
473 {
474 this->PutWithSchedule( l, scheduleType );
475 }
476 const PertialSchedule *PutOld( long l, bool isPertialSchedule )
477 {
478 const PertialSchedule *pPertialSchedule;
479 if( isPertialSchedule )
480 {
481 pertialSchedules.push_back( new PertialSchedule( pNativeCode->GetSize(), sizeof(long) ) );
482 pPertialSchedule = pertialSchedules.back();
483 }
484 pNativeCode->Put( l );
485 return pPertialSchedule;
486 }
487 void PutOld( const NativeCode &nativeCode )
488 {
489 pNativeCode->PutEx( nativeCode );
490 }
491 void PutOld( char c )
492 {
493 pNativeCode->Put( c );
494 }
495 void PutOld( char c1, char c2 )
496 {
497 pNativeCode->Put( c1 );
498 pNativeCode->Put( c2 );
499 }
500 void PutOld( char c1, char c2, char c3 )
501 {
502 pNativeCode->Put( c1 );
503 pNativeCode->Put( c2 );
504 pNativeCode->Put( c3 );
505 }
506 void PutOld( char c1, char c2, char c3, char c4 )
507 {
508 pNativeCode->Put( c1 );
509 pNativeCode->Put( c2 );
510 pNativeCode->Put( c3 );
511 pNativeCode->Put( c4 );
512 }
513 void PutOld( char c1, char c2, char c3, long l )
514 {
515 pNativeCode->Put( c1 );
516 pNativeCode->Put( c2 );
517 pNativeCode->Put( c3 );
518 pNativeCode->Put( l );
519 }
520 void PutOld( char c1, char c2, char c3, char c4, char c5 )
521 {
522 pNativeCode->Put( c1 );
523 pNativeCode->Put( c2 );
524 pNativeCode->Put( c3 );
525 pNativeCode->Put( c4 );
526 pNativeCode->Put( c5 );
527 }
528 void PutOld( char c1, char c2, char c3, char c4, char c5, char c6 )
529 {
530 pNativeCode->Put( c1 );
531 pNativeCode->Put( c2 );
532 pNativeCode->Put( c3 );
533 pNativeCode->Put( c4 );
534 pNativeCode->Put( c5 );
535 pNativeCode->Put( c6 );
536 }
537};
Note: See TracBrowser for help on using the repository browser.