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

Last change on this file since 217 was 206, checked in by dai_9181, 17 years ago

コード全体のリファクタリングを実施

File size: 16.7 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 int local_parm_size; //ローカル領域のパラメータサイズ
120
121public:
122 //コンストラクタ
123 CStackFrame();
124
125 //デストラクタ
126 ~CStackFrame();
127
128 void SetLocalParmSize(int size);
129 int GetFrameSize();
130 int GetNowSp();
131 void mov_sp( int reg );
132 int push(int reg);
133 void push(int xmm_reg,int varSize);
134 void ref_offset_data( int reg, int sp_offset );
135 void ref(int reg);
136 void ref(int xmm_reg,int varSize);
137 void pop(int reg = REG_NON);
138 void pop(int xmm_reg,int varSize);
139 void parameter_allocate(int size);
140 void RunningSchedule(void);
141
142 void error_check(void);
143};
144extern CStackFrame *pobj_sf;
145
146
147class CBlockReg{
148 int array_BlockReg[256];
149 int num;
150
151public:
152 CBlockReg();
153 void lock(int reg);
154 void unlock(int reg);
155 BOOL check(int reg);
156 void clear(void);
157
158 //レジスタのバックアップと復旧
159 void backup();
160 void restore();
161};
162extern CBlockReg *pobj_BlockReg;
163class CRegister{
164 ////////////////////
165 // レジスタ管理
166 ////////////////////
167
168 //利用可能なレジスタを列挙する関数
169 void EnumRegister(int *pRegList,int nMaxList,int *array_reg,int *sp,int AnswerReg);
170
171 int array_UseReg[16],sp_UseReg;
172
173 int array_XmmReg[16];
174 int sp_XmmReg;
175
176 int init_sp_reg,init_sp_xmm_reg;
177
178public:
179 CRegister(){};
180 CRegister(int AnswerReg);
181 ~CRegister(){};
182
183 //コンパイラにバグがないかをチェックする機構
184 void bug_check();
185
186 //汎用レジスタ
187 int GetNextReg();
188 int GetLockingReg();
189 int LockReg();
190 int UnlockReg();
191
192 //XMMレジスタ
193 int GetNextXmmReg();
194 int GetLockingXmmReg();
195 int LockXmmReg();
196 int UnlockXmmReg();
197
198 //レジスタが利用中かどうかを調べる
199 bool IsUsing( int reg );
200
201 //レジスタのバックアップと復旧
202 void backup();
203 void restore();
204};
205extern CRegister *pobj_reg;
206
207
208#define BACKUP_REGISTER_RESOURCE \
209 /* レジスタをスタックフレームにバックアップ */ \
210 pobj_BlockReg->backup(); \
211 if(pobj_reg) pobj_reg->backup(); \
212 \
213 /* レジスタブロッキングオブジェクトを退避して再生成 */ \
214 CBlockReg *pobj_BlockReg_back; \
215 pobj_BlockReg_back=pobj_BlockReg; \
216 pobj_BlockReg=new CBlockReg; \
217 \
218 /* レジスタ管理オブジェクトポインタを退避して0をセット */ \
219 CRegister *pobj_reg_back; \
220 pobj_reg_back=pobj_reg; \
221 pobj_reg=0;
222
223#define RESTORE_REGISTER_RESOURCE \
224 /* レジスタブロッキングオブジェクトポインタを復元 */ \
225 delete pobj_BlockReg; \
226 pobj_BlockReg=pobj_BlockReg_back; \
227 \
228 /* レジスタ管理オブジェクトポインタを復元 */ \
229 delete pobj_reg; \
230 pobj_reg=pobj_reg_back; \
231 \
232 /* レジスタをスタックフレームから復元 */ \
233 if(pobj_reg) pobj_reg->restore(); \
234 pobj_BlockReg->restore();
235
236
237
238//RSrcSection.cpp
239char *GetRSrcSectionBuffer(int *pLen);
240
241//Compile.cpp
242void ChangeOpcode(char *Command);
243void GetGlobalDataForDll(void);
244DWORD CompileBuffer(char Return_Sequence,WORD Return_Command);
245
246//Register.cpp
247BOOL IsGeneralReg(int reg);
248BOOL IsXmmReg(int reg);
249BOOL IsVolatileReg(int reg);
250void IfR14Push( int reg );
251
252//Compile_Calc.cpp
253void SetVariableFromRax(int VarType,int CalcType,RELATIVE_VAR *pRelativeVar);
254void OpcodeCalc(const char *Command);
255
256//NumOpe.cpp
257bool TermOpe(
258 const char *term,
259 const Type &baseType,
260 Type &resultType,
261 bool &isLiteral,
262 BOOL *pbUseHeap,
263 bool isWantObject = false,
264 bool *pIsClassName = NULL,
265 bool isProcedureCallOnly = false );
266bool NumOpe( int *pReg,
267 const char *Command,
268 const Type &baseType,
269 Type &resultType,
270 BOOL *pbUseHeap = NULL );
271
272//NumOpe_Arithmetic.cpp
273BOOL CalcTwoTerm_Arithmetic(int idCalc,int *type,LONG_PTR *index_stack,int *pStackPointer);
274BOOL Calc_Mod(int *type,LONG_PTR *index_stack,int *pStackPointer);
275BOOL Calc_Divide(int *type,int *pStackPointer,int BaseType);
276BOOL Calc_IntDivide(int *type,LONG_PTR *index_stack,int *pStackPointer);
277BOOL Calc_MinusMark(int *type,int sp);
278BOOL Calc_Power(int *type,int *pStackPointer);
279BOOL Calc_Shift(int idCalc,int *type,int *pStackPointer);
280
281//NumOpe_Logical.cpp
282BOOL CalcTwoTerm_Logical(int idCalc,int *type,LONG_PTR *index_stack,int *pStackPointer);
283BOOL Calc_Not(int *type,int sp);
284
285//NumOpe_Relation.cpp
286BOOL CalcTwoTerm_Relational(int idCalc,int *type,LONG_PTR *index_stack,int *pStackPointer);
287
288//NumOpe_TypeOperation.cpp
289void ExtendTypeTo64(int type,int reg);
290void ExtendTypeTo32(int type,int reg);
291void ExtendTypeTo16(int type,int reg);
292void ChangeTypeToXmm_Double(int type,int xmm_reg,int general_reg);
293void ChangeTypeToXmm_Single(int type,int xmm_reg,int general_reg);
294void ChangeTypeToWhole(int OldType,int NewType,int reg,int xmm_reg);
295void SetOneTermToReg_RealCalc(int TermType,int *pXmmReg);
296void SetOneTermToReg_Whole64Calc(int TermType,int *pReg);
297void SetOneTermToReg_Whole32Calc(int TermType,int *pReg);
298void SetTowTermToReg_RealCalc(int AnswerType,int *type,int sp,int *pXmmReg1,int *pXmmReg2);
299void SetTowTermToReg_Whole64Calc(int *type,int sp,int *pReg1,int *pReg2);
300void SetTowTermToReg_Whole32Calc(int *type,int sp,int *pReg1,int *pReg2);
301BOOL Calc_Cast(int *type,LONG_PTR *index_stack,int *pStackPointer);
302
303//Compile_Set_Var.cpp
304BOOL IsUse_r11(RELATIVE_VAR *pRelativeVar);
305void SetStructVariableFromRax( const Type &varType, const Type &calcType, RELATIVE_VAR *pRelativeVar,BOOL bUseHeap);
306void SetRealVariable(int VarType, int CalcType, RELATIVE_VAR *pRelativeVar);
307void SetBooleanVariable(int type,RELATIVE_VAR *pRelative);
308void SetWholeVariable(int varSize,int type,RELATIVE_VAR *pRelative);
309
310//increment.cpp
311void IncDec(int idCalc, const char *lpszLeft, const char *lpszRight);
312
313//Compile_Calc_PushVar.cpp
314void SetXmmReg_DoubleVariable(RELATIVE_VAR *pRelativeVar,int xmm_reg);
315void SetXmmReg_SingleVariable(RELATIVE_VAR *pRelativeVar,int xmm_reg);
316void SetReg_WholeVariable(int type,RELATIVE_VAR *pRelativeVar,int reg);
317
318//Compile_Interface.cpp
319bool CastToInterface( int reg, int vtblReg, const CClass &baseClass, const CClass &interfaceClass );
320
321//Compile_Object.cpp
322void Operator_New( const CClass &classObj, const char *objectSizeStr, const char *parameter,const Type &baseTypeInfo );
323void OpcodeDelete(const char *Parameter, bool isSweeping);
324
325//Compile_Var.cpp
326bool _member_offset(bool isErrorEnabled, bool isWriteAccess, const CClass &objClass, const char *member, RELATIVE_VAR *pRelativeVar, Type &resultType, BOOL bPrivateAccess);
327void SetThisPtrToReg(int reg);
328bool GetVarOffset(bool isErrorEnabled,bool isWriteAccess,const char *NameBuffer,RELATIVE_VAR *pRelativeVar,Type &resultType, Subscripts *pResultSubscripts = NULL );
329bool SetInitGlobalData(int offset,const Type &type,const Subscripts &subscripts,const char *InitBuf);
330#define DIMFLAG_INITDEBUGVAR 0x01
331#define DIMFLAG_NONCALL_CONSTRACTOR 0x02
332#define DIMFLAG_STATIC 0x04
333#define DIMFLAG_CONST 0x08
334void dim( char *VarName, const Subscripts &subscripts, Type &type, const char *InitBuf,const char *ConstractParameter,DWORD dwFlags);
335void SetVarPtrToReg(int reg,RELATIVE_VAR *pRelativeVar);
336bool Compile_AddGlobalRootsForGc();
337
338//ParamImpl.cpp
339class ParamImpl{
340 char *Parms[255];
341 vector<Type> types;
342 int ParmsNum;
343
344 Type returnType;
345
346 //一時オブジェクト管理用
347 bool useTempObject;
348 bool useTempParameters[255];
349 int StackOffsetOfTempObject[255];
350
351public:
352 ParamImpl(const char *buffer);
353 ParamImpl(const Parameters &params);
354 ~ParamImpl();
355 void SetReturnType( const Type &returnType );
356
357private:
358 bool EvaluateOverloadScore( int level, const Parameters &targetParms, const Type &targetResultType );
359
360public:
361 const UserProc *_OverloadSolution( const char *name, std::vector<const UserProc *> &subs, bool isEnabledReturnType = false );
362 const UserProc *OverloadSolution( const char *name, std::vector<const UserProc *> &subs, bool isEnabledReturnType = false );
363
364 void ApplyDefaultParameters( const Parameters &params );
365 bool ErrorCheck( const string &procName, const Parameters &params, int SecondParmNum = -1 );
366 void MacroParameterSupport( const Parameters &params );
367 void SetStructParameter( int reg, const Type &baseType, const char *expression );
368 void SetParameter( const string &procName, const Parameters &params, int SecondParmNum = -1 );
369
370 //一時オブジェクトパラメータの生成と破棄
371 int NewTempParameters( const string &procName, const Parameters &params, int SecondParmNum = -1 );
372 void DeleteTempParameters();
373
374 void BackupParameter(int pi_num);
375 void RestoreParameter(int pi_num);
376};
377
378//CLockParameter.cpp
379#define MAX_LOCKPARMS 255
380class CDBLockParms{
381public:
382 int array_LevelCount[MAX_LOCKPARMS];
383 CDBLockParms();
384 ~CDBLockParms();
385
386 void lock(int level);
387 void unlock(int level);
388};
389
390//Compile_CallProc.cpp
391void AddLocalVarAddrSchedule();
392#define PROCFLAG_NEW 1
393bool Opcode_CallProcPtr(const char *variable, const char *lpszParms,ProcPointer *pProcPointer);
394bool Opcode_CallProc(const char *Parameter,const UserProc *pUserProc,DWORD dwFlags,const char *ObjectName,int RefType);
395bool Opcode_CallDllProc( const char *lpszParms,DllProc *pDllProc);
396
397//Compile_ProcOp.cpp
398void CompileLocal();
399
400//Compile_Func.cpp
401int GetFunctionFromName(char *FuncName);
402bool Opcode_CallFunc( const char *Parameter, const int FuncNum, Type &ReturnTypeInfo, bool isCallOn = true );
403
404//OperatorProc.cpp
405void FreeTempObject(int reg,const CClass *pobj_c);
406int CallOperatorProc(BYTE idCalc, const Type &baseType, int *type_stack,LONG_PTR *index_stack,BOOL *bUseHeap,int &sp);
407void CallCastOperatorProc(int reg,Type &calcType,BOOL bCalcUseHeap,const Type &toType);
408void CallIndexerGetterProc(int reg,const CClass *pobj_Class,char *ObjectName,char *Parameter,Type &resultType);
409
410//Compile_Statement.cpp
411void OpcodeOthers(const char *Command);
412void OpcodeIf(char *Parameter);
413void OpcodeGoto(char *Parameter);
414void OpcodeWhile(char *Parameter);
415void OpcodeFor(char *Parameter);
416void OpcodeDo(char *Parameter);
417void OpcodeContinue(void);
418void OpcodeExitSub(void);
419void OpcodeSelect( const char *Parameter );
420void OpcodeCase(char *Parameter);
421void OpcodeGosub(char *Parameter);
422void OpcodeReturn(char *Parameter);
423void OpcodeSetPtrData(char *Parameter,int type);
424
425
426//InsertOpcode.cpp
427void InsertDimStatement_ToProcHead(char *lpszCommand);
428
429
430
431////////////////////////////////
432// AMD64機械語生成に利用する関数郡
433////////////////////////////////
434
435//Mod(モード)
436#define MOD_BASE (char)0x00
437#define MOD_DISP32 (char)0xFF
438#define MOD_BASE_DISP8 (char)0x40
439#define MOD_BASE_DISP32 (char)0x80
440#define MOD_REG (char)0xC0
441
442#define USE_OFFSET 1
443#define NON_OFFSET 0
444
445//amd64_main.cpp
446BOOL IsSafeReg(int reg);
447void op_push(int reg);
448void op_push_value(long data);
449void op_pop(int reg);
450void op_mov_RV (int op_size,int reg,int i32data);
451void op_mov_RV64 (int reg,_int64 i64data);
452void op_mov_RM (int op_size,int reg,int base_reg,int offset,char mod);
453void op_mov_RM_ex (int op_size,int reg,int base_reg1,int base_reg2,int offset,BOOL bUseOffset);
454void op_mov_MR (int op_size,int reg,int base_reg,int offset,char mod);
455void op_mov_MR_ex (int op_size,int reg,int base_reg1,int base_reg2,int offset,BOOL bUseOffset);
456void op_mov_MV (int op_size,int base_reg,int offset,BOOL bUseOffset,int i32data);
457void op_mov_RR (int reg1,int reg2);
458void op_mov64_ToReg (int reg,_int64 i64data);
459void op_mov64_ToReg (int reg,int i32data);
460void op_movsxd (int reg64,int reg32);
461void op_movsx64_FromReg16 (int reg64,int reg16);
462void op_movsx64_FromReg8 (int reg64,int reg8);
463void op_movsx32_FromReg16 (int reg32,int reg16);
464void op_movsx32_FromReg8 (int reg32,int reg8);
465void op_movsx16_FromReg8 (int reg32,int reg8);
466void op_inc (int reg);
467void op_dec (int reg);
468void op_add_RM (int op_size,int reg,int base_reg,int offset,char mod);
469void op_add_RV (int reg,int offset);
470void op_add64_reg (int reg1,int reg2);
471void op_add32_reg (int reg1,int reg2);
472void op_sub_RV (int op_size,int reg,int i32data);
473void op_sub64_reg (int reg1,int reg2);
474void op_sub32_reg (int reg1,int reg2);
475void op_sbb_RR ( int op_size, int reg1, int reg2 );
476void op_imul_RR (int op_size,int reg1,int reg2);
477void op_imul_RV (int op_size,int reg,int i32data);
478void op_div64_reg (int reg);
479void op_idiv64_reg (int reg);
480void op_shl_reg (int op_size,int reg);
481void op_sar_reg (int op_size,int reg);
482void op_shr_reg (int op_size,int reg);
483void op_and_reg (int op_size,int reg1,int reg2);
484void op_and64_value (int reg,int offset);
485void op_and32_value (int reg,int offset);
486void op_or_reg (int op_size,int reg1,int reg2);
487void op_xor_reg (int op_size,int reg1,int reg2);
488void op_not_reg (int op_size,int reg);
489void op_neg ( int reg );
490void op_test (int reg1,int reg2);
491void op_cmp_reg (int op_size,int reg1,int reg2);
492void op_cmp_value (int op_size,int reg,char byte_data);
493void op_setne (int reg);
494void op_movlpd_MR (int xmm_reg,int base_reg,int offset,char mod);
495void op_movlpd_RM (int xmm_reg,int base_reg,int offset,char mod);
496void op_movsd_RR (int xmm_reg1,int xmm_reg2);
497void op_movsd_MR (int xmm_reg,int base_reg,int offset,char mod);
498void op_movss_RR (int xmm_reg1,int xmm_reg2);
499void op_movss_RM (int xmm_reg,int base_reg,int offset,char mod);
500void op_movss_MR (int xmm_reg,int base_reg,int offset,char mod);
501void op_movd_RX (int reg,int xmm_reg);
502void op_cvtsd2ss (int xmm_reg1,int xmm_reg2);
503void op_cvtss2sd (int xmm_reg1,int xmm_reg2);
504void op_cvttsd2si_xmm (int op_size,int reg,int xmm_reg);
505void op_cvttss2si_xmm (int op_size,int reg,int xmm_reg);
506void op_cvtsi2sd_reg (int op_size,int xmm_reg,int reg);
507void op_cvtsi2ss_reg (int op_size,int xmm_reg,int reg);
508void op_comisd (int xmm_reg1,int xmm_reg2);
509void op_comiss (int xmm_reg1,int xmm_reg2);
510void op_rep_movs (int op_size);
511void op_add_rsp(int num);
512void op_sub_rsp(int num);
513void op_add_esp(int num);
514void op_sub_esp(int num);
515void op_fld_ptr_esp(int type);
516void op_zero_reg(int reg);
517void op_call( const UserProc *pUserProc );
518void op_call( const DllProc *pDllProc );
519void op_ret();
Note: See TracBrowser for help on using the repository browser.