source: dev/trunk/abdev/BasicCompiler64/Opcode.h@ 225

Last change on this file since 225 was 220, checked in by dai_9181, 17 years ago

[64bitコンパイラ] スタックフレームが128ビット境界ラインにならないバグを修正

File size: 16.6 KB
Line 
1#pragma once
2
3#include <Type.h>
4#include <Procedure.h>
5
6
7//レジスタを示す定数
8#define REG_EAX 1
9#define REG_EBX 2
10#define REG_ECX 3
11#define REG_EDX 4
12#define REG_ESP 5
13#define REG_EBP 6
14
15#define REGISTER_OPERAND(reg) (reg&0x07)
16
17#define REG_NON -1
18#define REG_RAX 0x00 //reg:000
19#define REG_RCX 0x01 //reg:001
20#define REG_RDX 0x02 //reg:010
21#define REG_RBX 0x03 //reg:011
22#define REG_RSP 0x04 //reg:100
23#define REG_RBP 0x05 //reg:101
24#define REG_RSI 0x06 //reg:110
25#define REG_RDI 0x07 //reg:111
26#define REG_R8 0x08 //reg:000(REXプリフィックス)
27#define REG_R9 0x09 //reg:001(REXプリフィックス)
28#define REG_R10 0x0A //reg:010(REXプリフィックス)
29#define REG_R11 0x0B //reg:011(REXプリフィックス)
30#define REG_R12 0x0C //reg:100(REXプリフィックス)
31#define REG_R13 0x0D //reg:101(REXプリフィックス)
32#define REG_R14 0x0E //reg:110(REXプリフィックス)
33#define REG_R15 0x0F //reg:111(REXプリフィックス)
34
35#define REG_XMM0 0x10 //reg:000
36#define REG_XMM1 0x11 //reg:001
37#define REG_XMM2 0x12 //reg:010
38#define REG_XMM3 0x13 //reg:011
39#define REG_XMM4 0x14 //reg:100
40#define REG_XMM5 0x15 //reg:101
41#define REG_XMM6 0x16 //reg:110
42#define REG_XMM7 0x17 //reg:111
43#define REG_XMM8 0x18 //reg:000
44#define REG_XMM9 0x19 //reg:001
45#define REG_XMM10 0x1A //reg:010
46#define REG_XMM11 0x1B //reg:011
47#define REG_XMM12 0x1C //reg:100
48#define REG_XMM13 0x1D //reg:101
49#define REG_XMM14 0x1E //reg:110
50#define REG_XMM15 0x1F //reg:111
51
52#define IS_XMM_REG(reg) (reg&0x10)
53
54
55
56//変数の種類
57#define NON_VAR 0
58#define VAR_GLOBAL 1 // Global Variable
59#define VAR_REFGLOBAL 2 // Global Refference Variable
60#define VAR_LOCAL 3 // Local Variable
61#define VAR_REFLOCAL 4 // Local Refference Variable
62#define VAR_DIRECTMEM 5 // Direct memory
63
64
65extern int cp;
66extern int obp;
67extern char *OpBuffer;
68
69#define breakpoint OpBuffer[obp++]=(char)0xCC;
70
71
72//ラベルアドレス
73struct LABEL{
74 char *pName;
75 int line;
76 DWORD address;
77};
78
79//Goto未知ラベル
80struct GOTOLABELSCHEDULE{
81 char *pName;
82 int line;
83 DWORD pos;
84 DWORD now_cp;
85};
86
87
88//プロシージャの種類
89#define PROC_DEFAULT 1 //ユーザー定義関数
90#define PROC_DLL 2 //DLL関数
91#define PROC_BUILTIN 3 //コンパイラ埋め込み型
92#define PROC_PTR 4 //関数ポインタ
93
94//プロシージャ
95struct PROCEDURE{
96 char name[255];
97 int address;
98 int types[MAX_PARMS];
99 _int8 ByVal[MAX_PARMS];
100 BOOL ReturnType;
101};
102
103//With情報
104struct WITHINFO{
105 char **ppName;
106 int *pWithCp;
107 int num;
108};
109
110
111class CStackFrame:public CSchedule{
112 ///////////////////////////
113 // スタックフレーム管理
114 ///////////////////////////
115
116 int lowest_sp; //スタックポインタの最下位位置
117 int now_sp; //スタックポインタ
118 int max_parm_size; //パラメータの最大サイズ
119
120public:
121 //コンストラクタ
122 CStackFrame();
123
124 //デストラクタ
125 ~CStackFrame();
126
127 int GetFrameSize( int localParamSize );
128 int GetNowSp();
129 void mov_sp( int reg );
130 int push(int reg);
131 void push(int xmm_reg,int varSize);
132 void ref_offset_data( int reg, int sp_offset );
133 void ref(int reg);
134 void ref(int xmm_reg,int varSize);
135 void pop(int reg = REG_NON);
136 void pop(int xmm_reg,int varSize);
137 void parameter_allocate(int size);
138 void RunningSchedule( int stackFrameSize );
139
140 void error_check(void);
141};
142extern CStackFrame *pobj_sf;
143
144
145class CBlockReg{
146 int array_BlockReg[256];
147 int num;
148
149public:
150 CBlockReg();
151 void lock(int reg);
152 void unlock(int reg);
153 BOOL check(int reg);
154 void clear(void);
155
156 //レジスタのバックアップと復旧
157 void backup();
158 void restore();
159};
160extern CBlockReg *pobj_BlockReg;
161class CRegister{
162 ////////////////////
163 // レジスタ管理
164 ////////////////////
165
166 //利用可能なレジスタを列挙する関数
167 void EnumRegister(int *pRegList,int nMaxList,int *array_reg,int *sp,int AnswerReg);
168
169 int array_UseReg[16],sp_UseReg;
170
171 int array_XmmReg[16];
172 int sp_XmmReg;
173
174 int init_sp_reg,init_sp_xmm_reg;
175
176public:
177 CRegister(){};
178 CRegister(int AnswerReg);
179 ~CRegister(){};
180
181 //コンパイラにバグがないかをチェックする機構
182 void bug_check();
183
184 //汎用レジスタ
185 int GetNextReg();
186 int GetLockingReg();
187 int LockReg();
188 int UnlockReg();
189
190 //XMMレジスタ
191 int GetNextXmmReg();
192 int GetLockingXmmReg();
193 int LockXmmReg();
194 int UnlockXmmReg();
195
196 //レジスタが利用中かどうかを調べる
197 bool IsUsing( int reg );
198
199 //レジスタのバックアップと復旧
200 void backup();
201 void restore();
202};
203extern CRegister *pobj_reg;
204
205
206#define BACKUP_REGISTER_RESOURCE \
207 /* レジスタをスタックフレームにバックアップ */ \
208 pobj_BlockReg->backup(); \
209 if(pobj_reg) pobj_reg->backup(); \
210 \
211 /* レジスタブロッキングオブジェクトを退避して再生成 */ \
212 CBlockReg *pobj_BlockReg_back; \
213 pobj_BlockReg_back=pobj_BlockReg; \
214 pobj_BlockReg=new CBlockReg; \
215 \
216 /* レジスタ管理オブジェクトポインタを退避して0をセット */ \
217 CRegister *pobj_reg_back; \
218 pobj_reg_back=pobj_reg; \
219 pobj_reg=0;
220
221#define RESTORE_REGISTER_RESOURCE \
222 /* レジスタブロッキングオブジェクトポインタを復元 */ \
223 delete pobj_BlockReg; \
224 pobj_BlockReg=pobj_BlockReg_back; \
225 \
226 /* レジスタ管理オブジェクトポインタを復元 */ \
227 delete pobj_reg; \
228 pobj_reg=pobj_reg_back; \
229 \
230 /* レジスタをスタックフレームから復元 */ \
231 if(pobj_reg) pobj_reg->restore(); \
232 pobj_BlockReg->restore();
233
234
235
236//RSrcSection.cpp
237char *GetRSrcSectionBuffer(int *pLen);
238
239//Compile.cpp
240void ChangeOpcode(char *Command);
241void GetGlobalDataForDll(void);
242DWORD CompileBuffer(char Return_Sequence,WORD Return_Command);
243
244//Register.cpp
245BOOL IsGeneralReg(int reg);
246BOOL IsXmmReg(int reg);
247BOOL IsVolatileReg(int reg);
248void IfR14Push( int reg );
249
250//Compile_Calc.cpp
251void SetVariableFromRax(int VarType,int CalcType,RELATIVE_VAR *pRelativeVar);
252void OpcodeCalc(const char *Command);
253
254//NumOpe.cpp
255bool TermOpe(
256 const char *term,
257 const Type &baseType,
258 Type &resultType,
259 bool &isLiteral,
260 BOOL *pbUseHeap,
261 bool isWantObject = false,
262 bool *pIsClassName = NULL,
263 bool isProcedureCallOnly = false );
264bool NumOpe( int *pReg,
265 const char *Command,
266 const Type &baseType,
267 Type &resultType,
268 BOOL *pbUseHeap = NULL );
269
270//NumOpe_Arithmetic.cpp
271BOOL CalcTwoTerm_Arithmetic(int idCalc,int *type,LONG_PTR *index_stack,int *pStackPointer);
272BOOL Calc_Mod(int *type,LONG_PTR *index_stack,int *pStackPointer);
273BOOL Calc_Divide(int *type,int *pStackPointer,int BaseType);
274BOOL Calc_IntDivide(int *type,LONG_PTR *index_stack,int *pStackPointer);
275BOOL Calc_MinusMark(int *type,int sp);
276BOOL Calc_Power(int *type,int *pStackPointer);
277BOOL Calc_Shift(int idCalc,int *type,int *pStackPointer);
278
279//NumOpe_Logical.cpp
280BOOL CalcTwoTerm_Logical(int idCalc,int *type,LONG_PTR *index_stack,int *pStackPointer);
281BOOL Calc_Not(int *type,int sp);
282
283//NumOpe_Relation.cpp
284BOOL CalcTwoTerm_Relational(int idCalc,int *type,LONG_PTR *index_stack,int *pStackPointer);
285
286//NumOpe_TypeOperation.cpp
287void ExtendTypeTo64(int type,int reg);
288void ExtendTypeTo32(int type,int reg);
289void ExtendTypeTo16(int type,int reg);
290void ChangeTypeToXmm_Double(int type,int xmm_reg,int general_reg);
291void ChangeTypeToXmm_Single(int type,int xmm_reg,int general_reg);
292void ChangeTypeToWhole(int OldType,int NewType,int reg,int xmm_reg);
293void SetOneTermToReg_RealCalc(int TermType,int *pXmmReg);
294void SetOneTermToReg_Whole64Calc(int TermType,int *pReg);
295void SetOneTermToReg_Whole32Calc(int TermType,int *pReg);
296void SetTowTermToReg_RealCalc(int AnswerType,int *type,int sp,int *pXmmReg1,int *pXmmReg2);
297void SetTowTermToReg_Whole64Calc(int *type,int sp,int *pReg1,int *pReg2);
298void SetTowTermToReg_Whole32Calc(int *type,int sp,int *pReg1,int *pReg2);
299BOOL Calc_Cast(int *type,LONG_PTR *index_stack,int *pStackPointer);
300
301//Compile_Set_Var.cpp
302BOOL IsUse_r11(RELATIVE_VAR *pRelativeVar);
303void SetStructVariableFromRax( const Type &varType, const Type &calcType, RELATIVE_VAR *pRelativeVar,BOOL bUseHeap);
304void SetRealVariable(int VarType, int CalcType, RELATIVE_VAR *pRelativeVar);
305void SetBooleanVariable(int type,RELATIVE_VAR *pRelative);
306void SetWholeVariable(int varSize,int type,RELATIVE_VAR *pRelative);
307
308//increment.cpp
309void IncDec(int idCalc, const char *lpszLeft, const char *lpszRight);
310
311//Compile_Calc_PushVar.cpp
312void SetXmmReg_DoubleVariable(RELATIVE_VAR *pRelativeVar,int xmm_reg);
313void SetXmmReg_SingleVariable(RELATIVE_VAR *pRelativeVar,int xmm_reg);
314void SetReg_WholeVariable(int type,RELATIVE_VAR *pRelativeVar,int reg);
315
316//Compile_Interface.cpp
317bool CastToInterface( int reg, int vtblReg, const CClass &baseClass, const CClass &interfaceClass );
318
319//Compile_Object.cpp
320void Operator_New( const CClass &classObj, const char *objectSizeStr, const char *parameter,const Type &baseTypeInfo );
321void OpcodeDelete(const char *Parameter, bool isSweeping);
322
323//Compile_Var.cpp
324bool _member_offset(bool isErrorEnabled, bool isWriteAccess, const CClass &objClass, const char *member, RELATIVE_VAR *pRelativeVar, Type &resultType, BOOL bPrivateAccess);
325void SetThisPtrToReg(int reg);
326bool GetVarOffset(bool isErrorEnabled,bool isWriteAccess,const char *NameBuffer,RELATIVE_VAR *pRelativeVar,Type &resultType, Subscripts *pResultSubscripts = NULL );
327bool SetInitGlobalData(int offset,const Type &type,const Subscripts &subscripts,const char *InitBuf);
328#define DIMFLAG_INITDEBUGVAR 0x01
329#define DIMFLAG_NONCALL_CONSTRACTOR 0x02
330#define DIMFLAG_STATIC 0x04
331#define DIMFLAG_CONST 0x08
332void dim( char *VarName, const Subscripts &subscripts, Type &type, const char *InitBuf,const char *ConstractParameter,DWORD dwFlags);
333void SetVarPtrToReg(int reg,RELATIVE_VAR *pRelativeVar);
334bool Compile_AddGlobalRootsForGc();
335
336//ParamImpl.cpp
337class ParamImpl{
338 char *Parms[255];
339 vector<Type> types;
340 int ParmsNum;
341
342 Type returnType;
343
344 //一時オブジェクト管理用
345 bool useTempObject;
346 bool useTempParameters[255];
347 int StackOffsetOfTempObject[255];
348
349public:
350 ParamImpl(const char *buffer);
351 ParamImpl(const Parameters &params);
352 ~ParamImpl();
353 void SetReturnType( const Type &returnType );
354
355private:
356 bool EvaluateOverloadScore( int level, const Parameters &targetParms, const Type &targetResultType );
357
358public:
359 const UserProc *_OverloadSolution( const char *name, std::vector<const UserProc *> &subs, bool isEnabledReturnType = false );
360 const UserProc *OverloadSolution( const char *name, std::vector<const UserProc *> &subs, bool isEnabledReturnType = false );
361
362 void ApplyDefaultParameters( const Parameters &params );
363 bool ErrorCheck( const string &procName, const Parameters &params, int SecondParmNum = -1 );
364 void MacroParameterSupport( const Parameters &params );
365 void SetStructParameter( int reg, const Type &baseType, const char *expression );
366 void SetParameter( const string &procName, const Parameters &params, int SecondParmNum = -1 );
367
368 //一時オブジェクトパラメータの生成と破棄
369 int NewTempParameters( const string &procName, const Parameters &params, int SecondParmNum = -1 );
370 void DeleteTempParameters();
371
372 void BackupParameter(int pi_num);
373 void RestoreParameter(int pi_num);
374};
375
376//CLockParameter.cpp
377#define MAX_LOCKPARMS 255
378class CDBLockParms{
379public:
380 int array_LevelCount[MAX_LOCKPARMS];
381 CDBLockParms();
382 ~CDBLockParms();
383
384 void lock(int level);
385 void unlock(int level);
386};
387
388//Compile_CallProc.cpp
389void AddLocalVarAddrSchedule();
390#define PROCFLAG_NEW 1
391bool Opcode_CallProcPtr(const char *variable, const char *lpszParms,ProcPointer *pProcPointer);
392bool Opcode_CallProc(const char *Parameter,const UserProc *pUserProc,DWORD dwFlags,const char *ObjectName,int RefType);
393bool Opcode_CallDllProc( const char *lpszParms,DllProc *pDllProc);
394
395//Compile_ProcOp.cpp
396void CompileLocal();
397
398//Compile_Func.cpp
399int GetFunctionFromName(char *FuncName);
400bool Opcode_CallFunc( const char *Parameter, const int FuncNum, Type &ReturnTypeInfo, bool isCallOn = true );
401
402//OperatorProc.cpp
403void FreeTempObject(int reg,const CClass *pobj_c);
404int CallOperatorProc(BYTE idCalc, const Type &baseType, int *type_stack,LONG_PTR *index_stack,BOOL *bUseHeap,int &sp);
405void CallCastOperatorProc(int reg,Type &calcType,BOOL bCalcUseHeap,const Type &toType);
406void CallIndexerGetterProc(int reg,const CClass *pobj_Class,char *ObjectName,char *Parameter,Type &resultType);
407
408//Compile_Statement.cpp
409void OpcodeOthers(const char *Command);
410void OpcodeIf(char *Parameter);
411void OpcodeGoto(char *Parameter);
412void OpcodeWhile(char *Parameter);
413void OpcodeFor(char *Parameter);
414void OpcodeDo(char *Parameter);
415void OpcodeContinue(void);
416void OpcodeExitSub(void);
417void OpcodeSelect( const char *Parameter );
418void OpcodeCase(char *Parameter);
419void OpcodeGosub(char *Parameter);
420void OpcodeReturn(char *Parameter);
421void OpcodeSetPtrData(char *Parameter,int type);
422
423
424//InsertOpcode.cpp
425void InsertDimStatement_ToProcHead(char *lpszCommand);
426
427
428
429////////////////////////////////
430// AMD64機械語生成に利用する関数郡
431////////////////////////////////
432
433//Mod(モード)
434#define MOD_BASE (char)0x00
435#define MOD_DISP32 (char)0xFF
436#define MOD_BASE_DISP8 (char)0x40
437#define MOD_BASE_DISP32 (char)0x80
438#define MOD_REG (char)0xC0
439
440#define USE_OFFSET 1
441#define NON_OFFSET 0
442
443//amd64_main.cpp
444BOOL IsSafeReg(int reg);
445void op_push(int reg);
446void op_push_value(long data);
447void op_pop(int reg);
448void op_mov_RV (int op_size,int reg,int i32data);
449void op_mov_RV64 (int reg,_int64 i64data);
450void op_mov_RM (int op_size,int reg,int base_reg,int offset,char mod);
451void op_mov_RM_ex (int op_size,int reg,int base_reg1,int base_reg2,int offset,BOOL bUseOffset);
452void op_mov_MR (int op_size,int reg,int base_reg,int offset,char mod);
453void op_mov_MR_ex (int op_size,int reg,int base_reg1,int base_reg2,int offset,BOOL bUseOffset);
454void op_mov_MV (int op_size,int base_reg,int offset,BOOL bUseOffset,int i32data);
455void op_mov_RR (int reg1,int reg2);
456void op_mov64_ToReg (int reg,_int64 i64data);
457void op_mov64_ToReg (int reg,int i32data);
458void op_movsxd (int reg64,int reg32);
459void op_movsx64_FromReg16 (int reg64,int reg16);
460void op_movsx64_FromReg8 (int reg64,int reg8);
461void op_movsx32_FromReg16 (int reg32,int reg16);
462void op_movsx32_FromReg8 (int reg32,int reg8);
463void op_movsx16_FromReg8 (int reg32,int reg8);
464void op_inc (int reg);
465void op_dec (int reg);
466void op_add_RM (int op_size,int reg,int base_reg,int offset,char mod);
467void op_add_RV (int reg,int offset);
468void op_add64_reg (int reg1,int reg2);
469void op_add32_reg (int reg1,int reg2);
470void op_sub_RV (int op_size,int reg,int i32data);
471void op_sub64_reg (int reg1,int reg2);
472void op_sub32_reg (int reg1,int reg2);
473void op_sbb_RR ( int op_size, int reg1, int reg2 );
474void op_imul_RR (int op_size,int reg1,int reg2);
475void op_imul_RV (int op_size,int reg,int i32data);
476void op_div64_reg (int reg);
477void op_idiv64_reg (int reg);
478void op_shl_reg (int op_size,int reg);
479void op_sar_reg (int op_size,int reg);
480void op_shr_reg (int op_size,int reg);
481void op_and_reg (int op_size,int reg1,int reg2);
482void op_and64_value (int reg,int offset);
483void op_and32_value (int reg,int offset);
484void op_or_reg (int op_size,int reg1,int reg2);
485void op_xor_reg (int op_size,int reg1,int reg2);
486void op_not_reg (int op_size,int reg);
487void op_neg ( int reg );
488void op_test (int reg1,int reg2);
489void op_cmp_reg (int op_size,int reg1,int reg2);
490void op_cmp_value (int op_size,int reg,char byte_data);
491void op_setne (int reg);
492void op_movlpd_MR (int xmm_reg,int base_reg,int offset,char mod);
493void op_movlpd_RM (int xmm_reg,int base_reg,int offset,char mod);
494void op_movsd_RR (int xmm_reg1,int xmm_reg2);
495void op_movsd_MR (int xmm_reg,int base_reg,int offset,char mod);
496void op_movss_RR (int xmm_reg1,int xmm_reg2);
497void op_movss_RM (int xmm_reg,int base_reg,int offset,char mod);
498void op_movss_MR (int xmm_reg,int base_reg,int offset,char mod);
499void op_movd_RX (int reg,int xmm_reg);
500void op_cvtsd2ss (int xmm_reg1,int xmm_reg2);
501void op_cvtss2sd (int xmm_reg1,int xmm_reg2);
502void op_cvttsd2si_xmm (int op_size,int reg,int xmm_reg);
503void op_cvttss2si_xmm (int op_size,int reg,int xmm_reg);
504void op_cvtsi2sd_reg (int op_size,int xmm_reg,int reg);
505void op_cvtsi2ss_reg (int op_size,int xmm_reg,int reg);
506void op_comisd (int xmm_reg1,int xmm_reg2);
507void op_comiss (int xmm_reg1,int xmm_reg2);
508void op_rep_movs (int op_size);
509void op_add_rsp(int num);
510void op_sub_rsp(int num);
511void op_add_esp(int num);
512void op_sub_esp(int num);
513void op_fld_ptr_esp(int type);
514void op_zero_reg(int reg);
515void op_call( const UserProc *pUserProc );
516void op_call( const DllProc *pDllProc );
517void op_ret();
Note: See TracBrowser for help on using the repository browser.