source: dev/trunk/abdev/BasicCompiler_Common/include/CodeGenerator.h@ 261

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