source: dev/BasicCompiler64/Opcode.h@ 100

Last change on this file since 100 was 97, checked in by dai_9181, 18 years ago

関数の戻り値オブジェクトのメンバ・メソッドを一時オブジェクトを介さずに参照できるようにした。

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