source: dev/BasicCompiler64/Opcode.h@ 52

Last change on this file since 52 was 50, checked in by dai_9181, 18 years ago

オーバーロード解決用の関数保持リストを "SUBINFO " ではなく、"vector<SUBINFO *>" に変更した。

File size: 16.2 KB
Line 
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//変数の種類
54#define NON_VAR 0
55#define VAR_GLOBAL 1 //Global Variable
56#define VAR_LOCAL 2 //Local Variable
57#define VAR_REFLOCAL 3 //Local Refference Variable
58#define VAR_DIRECTMEM 5 //Direct memory
59
60
61extern int cp;
62extern int obp;
63extern char *OpBuffer;
64
65#define breakpoint OpBuffer[obp++]=(char)0xCC;
66
67
68//ラベルアドレス
69struct LABEL{
70 char *pName;
71 int line;
72 DWORD address;
73};
74
75//Goto未知ラベル
76struct GOTOLABELSCHEDULE{
77 char *pName;
78 int line;
79 DWORD pos;
80 DWORD now_cp;
81};
82
83
84//プロシージャの種類
85#define PROC_DEFAULT 1 //ユーザー定義関数
86#define PROC_DLL 2 //DLL関数
87#define PROC_BUILTIN 3 //コンパイラ埋め込み型
88#define PROC_PTR 4 //関数ポインタ
89
90//プロシージャ
91struct PROCEDURE{
92 char name[255];
93 int address;
94 int types[MAX_PARMS];
95 _int8 ByVal[MAX_PARMS];
96 BOOL ReturnType;
97};
98
99//With情報
100struct WITHINFO{
101 char **ppName;
102 int *pWithCp;
103 int num;
104};
105
106
107class CStackFrame:public CSchedule{
108 ///////////////////////////
109 // スタックフレーム管理
110 ///////////////////////////
111
112 int lowest_sp; //スタックポインタの最下位位置
113 int now_sp; //スタックポインタ
114 int max_parm_size; //パラメータの最大サイズ
115 int local_parm_size; //ローカル領域のパラメータサイズ
116
117public:
118 //コンストラクタ
119 CStackFrame();
120
121 //デストラクタ
122 ~CStackFrame();
123
124 void SetLocalParmSize(int size);
125 int GetFrameSize();
126 int push(int reg);
127 void push(int xmm_reg,int var_size);
128 void ref_offset_data( int reg, int sp_offset );
129 void ref(int reg);
130 void ref(int xmm_reg,int var_size);
131 void pop(int reg);
132 void pop(int xmm_reg,int var_size);
133 void parameter_allocate(int size);
134 void RunningSchedule(void);
135
136 void error_check(void);
137};
138extern CStackFrame *pobj_sf;
139
140
141class CBlockReg{
142 int array_BlockReg[256];
143 int num;
144
145public:
146 CBlockReg();
147 void lock(int reg);
148 void unlock(int reg);
149 BOOL check(int reg);
150 void clear(void);
151
152 //レジスタのバックアップと復旧
153 void backup();
154 void restore();
155};
156extern CBlockReg *pobj_BlockReg;
157class CRegister{
158 ////////////////////
159 // レジスタ管理
160 ////////////////////
161
162 //利用可能なレジスタを列挙する関数
163 void EnumRegister(int *pRegList,int nMaxList,int *array_reg,int *sp,int AnswerReg);
164
165 int array_UseReg[16],sp_UseReg;
166
167 int array_XmmReg[16];
168 int sp_XmmReg;
169
170 int init_sp_reg,init_sp_xmm_reg;
171
172public:
173 CRegister(){};
174 CRegister(int AnswerReg);
175 ~CRegister(){};
176
177 //コンパイラにバグがないかをチェックする機構
178 void bug_check();
179
180 //汎用レジスタ
181 int GetNextReg();
182 int GetLockingReg();
183 int LockReg();
184 int UnlockReg();
185
186 //XMMレジスタ
187 int GetNextXmmReg();
188 int GetLockingXmmReg();
189 int LockXmmReg();
190 int UnlockXmmReg();
191
192 //レジスタが利用中かどうかを調べる
193 bool IsUsing( int reg );
194
195 //レジスタのバックアップと復旧
196 void backup();
197 void restore();
198};
199extern CRegister *pobj_reg;
200
201
202#define BACKUP_REGISTER_RESOURCE \
203 /* レジスタをスタックフレームにバックアップ */ \
204 pobj_BlockReg->backup(); \
205 if(pobj_reg) pobj_reg->backup(); \
206 \
207 /* レジスタブロッキングオブジェクトを退避して再生成 */ \
208 CBlockReg *pobj_BlockReg_back; \
209 pobj_BlockReg_back=pobj_BlockReg; \
210 pobj_BlockReg=new CBlockReg; \
211 \
212 /* レジスタ管理オブジェクトポインタを退避して0をセット */ \
213 CRegister *pobj_reg_back; \
214 pobj_reg_back=pobj_reg; \
215 pobj_reg=0;
216
217#define RESTORE_REGISTER_RESOURCE \
218 /* レジスタブロッキングオブジェクトポインタを復元 */ \
219 delete pobj_BlockReg; \
220 pobj_BlockReg=pobj_BlockReg_back; \
221 \
222 /* レジスタ管理オブジェクトポインタを復元 */ \
223 delete pobj_reg; \
224 pobj_reg=pobj_reg_back; \
225 \
226 /* レジスタをスタックフレームから復元 */ \
227 if(pobj_reg) pobj_reg->restore(); \
228 pobj_BlockReg->restore();
229
230
231
232//MakePeHdr.cpp
233int AddDataTable(char *buffer,int length);
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);
251void SetRefVariable( const char *varname, const char *expression );
252
253//NumOpe.cpp
254int NumOpe(int *pReg,const char *Command,int BaseType,LONG_PTR lpBaseIndex,LONG_PTR *plpIndex,BOOL *pbUseHeap=0);
255
256//NumOpe_Arithmetic.cpp
257BOOL CalcTwoTerm_Arithmetic(int idCalc,int *type,LONG_PTR *index_stack,int *pStackPointer);
258BOOL Calc_Mod(int *type,LONG_PTR *index_stack,int *pStackPointer);
259BOOL Calc_Divide(int *type,int *pStackPointer,int BaseType);
260BOOL Calc_IntDivide(int *type,LONG_PTR *index_stack,int *pStackPointer);
261BOOL Calc_MinusMark(int *type,int sp);
262BOOL Calc_Power(int *type,int *pStackPointer);
263BOOL Calc_Shift(int idCalc,int *type,int *pStackPointer);
264
265//NumOpe_Logical.cpp
266BOOL CalcTwoTerm_Logical(int idCalc,int *type,LONG_PTR *index_stack,int *pStackPointer);
267BOOL Calc_Not(int *type,int sp);
268
269//NumOpe_Relation.cpp
270BOOL CalcTwoTerm_Relational(int idCalc,int *type,LONG_PTR *index_stack,int *pStackPointer);
271
272//NumOpe_TypeOperation.cpp
273void ExtendTypeTo64(int type,int reg);
274void ExtendTypeTo32(int type,int reg);
275void ExtendTypeTo16(int type,int reg);
276void ChangeTypeToXmm_Double(int type,int xmm_reg,int general_reg);
277void ChangeTypeToXmm_Single(int type,int xmm_reg,int general_reg);
278void ChangeTypeToWhole(int OldType,int NewType,int reg,int xmm_reg);
279void SetOneTermToReg_RealCalc(int TermType,int *pXmmReg);
280void SetOneTermToReg_Whole64Calc(int TermType,int *pReg);
281void SetOneTermToReg_Whole32Calc(int TermType,int *pReg);
282void SetTowTermToReg_RealCalc(int AnswerType,int *type,int sp,int *pXmmReg1,int *pXmmReg2);
283void SetTowTermToReg_Whole64Calc(int *type,int sp,int *pReg1,int *pReg2);
284void SetTowTermToReg_Whole32Calc(int *type,int sp,int *pReg1,int *pReg2);
285BOOL Calc_Cast(int *type,LONG_PTR *index_stack,int *pStackPointer);
286
287//Compile_Set_Var.cpp
288BOOL IsUse_r11(RELATIVE_VAR *pRelativeVar);
289void SetObjectVariableFromRax(LONG_PTR lpVarIndex,int CalcType,LONG_PTR lpCalcIndex,RELATIVE_VAR *pRelativeVar,BOOL bUseHeap);
290void SetDoubleVariable(int type,RELATIVE_VAR *pRelative);
291void SetSingleVariable(int type,RELATIVE_VAR *pRelative);
292void SetBooleanVariable(int type,RELATIVE_VAR *pRelative);
293void SetWholeVariable(int var_size,int type,RELATIVE_VAR *pRelative);
294
295//increment.cpp
296void IncDec(int idCalc, char *lpszLeft, char *lpszRight);
297
298//calc2.cpp
299#define EXP_TYPE_NUMBER 1
300#define EXP_TYPE_EAX 2
301#define EXP_TYPE_FPU 3
302#define EXP_TYPE_VAR 4
303int NumOpEx(char *Command,double *pDbl,DWORD *pdwType,RELATIVE_VAR *pRelativeVar);
304
305//Compile_Calc_PushVar.cpp
306void SetXmmReg_DoubleVariable(RELATIVE_VAR *pRelativeVar,int xmm_reg);
307void SetXmmReg_SingleVariable(RELATIVE_VAR *pRelativeVar,int xmm_reg);
308void SetReg_WholeVariable(int type,RELATIVE_VAR *pRelativeVar,int reg);
309
310//Compile_Object.cpp
311int Operator_New(const char *Parameter,LONG_PTR *plpIndex);
312void OpcodeDelete(const char *Parameter);
313
314//Compile_Var.cpp
315void GetWithName(char *buffer);
316void SetThisPtrToReg(int reg);
317BOOL GetVarOffset(bool isErrorEnabled,bool isWriteAccess,const char *NameBuffer,int *pType,RELATIVE_VAR *pRelativeVar,LONG_PTR *plpIndex,int *pss=0);
318BOOL SetInitGlobalData(int offset,int type,LONG_PTR lpIndex,int *SubScripts,char *InitBuf);
319#define DIMFLAG_INITDEBUGVAR 0x01
320#define DIMFLAG_NONCALL_CONSTRACTOR 0x02
321#define DIMFLAG_STATIC 0x04
322#define DIMFLAG_CONST 0x08
323void OpcodeDim(char *Parameter,DWORD dwFlag);
324void SetVarPtrToReg(int reg,RELATIVE_VAR *pRelativeVar);
325
326//CParameter.cpp
327#define OVERLOAD_LEVEL1 1
328#define OVERLOAD_LEVEL2 2
329#define OVERLOAD_LEVEL3 3
330class CParameter{
331 char *Parms[255];
332 TYPEINFO types[255];
333 int ParmsNum;
334
335 TYPEINFO ReturnTypeInfo;
336
337 //一時オブジェクト管理用
338 bool useTempObject;
339 bool useTempParameters[255];
340 int StackOffsetOfTempObject[255];
341
342public:
343 CParameter(const char *buffer);
344 CParameter(const PARAMETER_INFO *pParamInfo, const int ParmNum);
345 ~CParameter();
346 void SetReturnType(TYPEINFO *pTypeInfo);
347
348private:
349 BOOL _overload_check(PARAMETER_INFO *ppi,int pi_num,TYPEINFO *pReturnTypeInfo,int overload_level);
350 SUBINFO *OverloadSolutionWithReturnType( const char *name, std::vector<SUBINFO *> &subs );
351public:
352 SUBINFO *OverloadSolution( const char *name, std::vector<SUBINFO *> &subs );
353
354 BOOL ErrorCheck(const char *FuncName,PARAMETER_INFO *ppi,int pi_num,int SecondParmNum);
355 void MacroParameterSupport(PARAMETER_INFO *ppi);
356 void SetObjectParameter(int reg,CClass *pobj_Class,LPSTR Parameter);
357
358 //一時オブジェクトパラメータの生成と破棄
359 void NewTempParameters( const char *FuncName,PARAMETER_INFO *ppi,int pi_num,int SecondParmNum );
360 void DeleteTempParameters();
361
362 void SetParameter(const char *FuncName,PARAMETER_INFO *ppi,int pi_num,int SecondParmNum);
363
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
383int Opcode_CallProcPtr(char *variable,char *Parameter,PROCPTRINFO *pi,LONG_PTR *plpIndex);
384void Opcode_CallProc(char *Parameter,SUBINFO *psi,DWORD dwFlags,char *ObjectName,int RefType);
385int Opcode_CallDllProc(char *Parameter,DECLAREINFO *pdi,LONG_PTR *plpIndex);
386
387//Compile_ProcOp.cpp
388void CompileLocal();
389
390//Compile_Func.cpp
391int GetFunctionType(int FuncNum);
392int GetFunctionFromName(char *FuncName);
393void Opcode_CallFunc( const char *Parameter, const int FuncNum, TYPEINFO &ReturnTypeInfo );
394
395//OperatorProc.cpp
396void FreeTempObject(int reg,CClass *pobj_c);
397int CallOperatorProc(int idCalc,TYPEINFO *pBaseTypeInfo,int *type,LONG_PTR *index_stack,BOOL *bUseHeap,int &sp);
398void CallCastOperatorProc(int reg,int &CalcType,LONG_PTR &lpCalcIndex,BOOL bCalcUseHeap,int ToType,LONG_PTR lpToIndex);
399void CallIndexerGetterProc(int reg,CClass *pobj_Class,char *ObjectName,char *Parameter,TYPEINFO &RetTypeInfo);
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);
410void OpcodeSelect(char *Parameter);
411void OpcodeCase(char *Parameter);
412void OpcodeGosub(char *Parameter);
413void OpcodeReturn(char *Parameter);
414void Opcode_Input(char *Parameter);
415void Opcode_Print(char *Parameter,BOOL bWrite);
416void OpcodeCallPtr(char *Parameter);
417void OpcodeSetPtrData(char *Parameter,int type);
418
419
420//InsertOpcode.cpp
421void InsertDimStatement_ToProcHead(char *lpszCommand);
422
423
424
425////////////////////////////////
426// AMD64機械語生成に利用する関数郡
427////////////////////////////////
428
429//Mod(モード)
430#define MOD_BASE (char)0x00
431#define MOD_DISP32 (char)0xFF
432#define MOD_BASE_DISP8 (char)0x40
433#define MOD_BASE_DISP32 (char)0x80
434#define MOD_REG (char)0xC0
435
436#define USE_OFFSET 1
437#define NON_OFFSET 0
438
439//amd64_main.cpp
440BOOL IsSafeReg(int reg);
441void op_push(int reg);
442void op_push_value(long data);
443void op_pop(int reg);
444void op_mov_RV (int op_size,int reg,int i32data);
445void op_mov_RV64 (int reg,_int64 i64data);
446void op_mov_RM (int op_size,int reg,int base_reg,int offset,char mod);
447void op_mov_RM_ex (int op_size,int reg,int base_reg1,int base_reg2,int offset,BOOL bUseOffset);
448void op_mov_MR (int op_size,int reg,int base_reg,int offset,char mod);
449void op_mov_MR_ex (int op_size,int reg,int base_reg1,int base_reg2,int offset,BOOL bUseOffset);
450void op_mov_MV (int op_size,int base_reg,int offset,BOOL bUseOffset,int i32data);
451void op_mov_RR (int reg1,int reg2);
452void op_mov64_ToReg (int reg,_int64 i64data);
453void op_mov64_ToReg (int reg,int i32data);
454void op_mov64_ToReg_FromReg (int reg1,int reg2);
455void op_movsxd (int reg64,int reg32);
456void op_movsx64_FromReg16 (int reg64,int reg16);
457void op_movsx64_FromReg8 (int reg64,int reg8);
458void op_movsx32_FromReg16 (int reg32,int reg16);
459void op_movsx32_FromReg8 (int reg32,int reg8);
460void op_movsx16_FromReg8 (int reg32,int reg8);
461void op_inc (int reg);
462void op_dec (int reg);
463void op_add_RM (int op_size,int reg,int base_reg,int offset,char mod);
464void op_add64_value (int reg,int offset);
465void op_add64_reg (int reg1,int reg2);
466void op_add32_reg (int reg1,int reg2);
467void op_sub_RV (int op_size,int reg,int i32data);
468void op_sub64_reg (int reg1,int reg2);
469void op_sub32_reg (int reg1,int reg2);
470void op_sbb_RR ( int op_size, int reg1, int reg2 );
471void op_imul_reg (int op_size,int reg1,int reg2);
472void op_imul_value (int op_size,int reg,int i32data);
473void op_div64_reg (int reg);
474void op_idiv64_reg (int reg);
475void op_shl_reg (int op_size,int reg);
476void op_sar_reg (int op_size,int reg);
477void op_shr_reg (int op_size,int reg);
478void op_and_reg (int op_size,int reg1,int reg2);
479void op_and64_value (int reg,int offset);
480void op_and32_value (int reg,int offset);
481void op_or_reg (int op_size,int reg1,int reg2);
482void op_xor_reg (int op_size,int reg1,int reg2);
483void op_not_reg (int op_size,int reg);
484void op_neg ( int reg );
485void op_test (int reg1,int reg2);
486void op_cmp_reg (int op_size,int reg1,int reg2);
487void op_cmp_value (int op_size,int reg,char byte_data);
488void op_setne (int reg);
489void op_movlpd_MR (int xmm_reg,int base_reg,int offset,char mod);
490void op_movlpd_RM (int xmm_reg,int base_reg,int offset,char mod);
491void op_movsd_RR (int xmm_reg1,int xmm_reg2);
492void op_movsd_MR (int xmm_reg,int base_reg,int offset,char mod);
493void op_movss_RR (int xmm_reg1,int xmm_reg2);
494void op_movss_RM (int xmm_reg,int base_reg,int offset,char mod);
495void op_movss_MR (int xmm_reg,int base_reg,int offset,char mod);
496void op_movd_RX (int reg,int xmm_reg);
497void op_cvtsd2ss (int xmm_reg1,int xmm_reg2);
498void op_cvtss2sd (int xmm_reg1,int xmm_reg2);
499void op_cvttsd2si_xmm (int op_size,int reg,int xmm_reg);
500void op_cvttss2si_xmm (int op_size,int reg,int xmm_reg);
501void op_cvtsi2sd_reg (int op_size,int xmm_reg,int reg);
502void op_cvtsi2ss_reg (int op_size,int xmm_reg,int reg);
503void op_comisd (int xmm_reg1,int xmm_reg2);
504void op_comiss (int xmm_reg1,int xmm_reg2);
505void op_rep_movs (int op_size);
506void op_add_rsp(int num);
507void op_sub_rsp(int num);
508void op_add_esp(int num);
509void op_sub_esp(int num);
510void op_fld_ptr_esp(int type);
511void op_zero_reg(int reg);
512void op_call( SUBINFO *psi );
513void op_call( DECLAREINFO *pdi );
Note: See TracBrowser for help on using the repository browser.