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

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

ヘッダファイルを整理中

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