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

Last change on this file since 154 was 142, checked in by dai_9181, 17 years ago

op_ret関数を用意した

File size: 16.5 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_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
60
61
62extern int cp;
63extern int obp;
64extern char *OpBuffer;
65
66#define breakpoint OpBuffer[obp++]=(char)0xCC;
67
68
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();
127 int GetNowSp();
128 void mov_sp( int reg );
129 int push(int reg);
130 void push(int xmm_reg,int varSize);
131 void ref_offset_data( int reg, int sp_offset );
132 void ref(int reg);
133 void ref(int xmm_reg,int varSize);
134 void pop(int reg = REG_NON);
135 void pop(int xmm_reg,int varSize);
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
195 //レジスタが利用中かどうかを調べる
196 bool IsUsing( int reg );
197
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);
247void IfR14Push( int reg );
248
249//Compile_Calc.cpp
250void SetVariableFromRax(int VarType,int CalcType,RELATIVE_VAR *pRelativeVar);
251void OpcodeCalc(const char *Command);
252
253//NumOpe.cpp
254bool TermOpe(
255 const char *term,
256 const Type &baseType,
257 Type &resultType,
258 bool &isLiteral,
259 BOOL *pbUseHeap,
260 bool isWantObject = false,
261 bool *pIsClassName = NULL,
262 bool isProcedureCallOnly = false );
263bool NumOpe( int *pReg,
264 const char *Command,
265 const Type &baseType,
266 Type &resultType,
267 BOOL *pbUseHeap = NULL );
268
269//NumOpe_Arithmetic.cpp
270BOOL CalcTwoTerm_Arithmetic(int idCalc,int *type,LONG_PTR *index_stack,int *pStackPointer);
271BOOL Calc_Mod(int *type,LONG_PTR *index_stack,int *pStackPointer);
272BOOL Calc_Divide(int *type,int *pStackPointer,int BaseType);
273BOOL Calc_IntDivide(int *type,LONG_PTR *index_stack,int *pStackPointer);
274BOOL Calc_MinusMark(int *type,int sp);
275BOOL Calc_Power(int *type,int *pStackPointer);
276BOOL Calc_Shift(int idCalc,int *type,int *pStackPointer);
277
278//NumOpe_Logical.cpp
279BOOL CalcTwoTerm_Logical(int idCalc,int *type,LONG_PTR *index_stack,int *pStackPointer);
280BOOL Calc_Not(int *type,int sp);
281
282//NumOpe_Relation.cpp
283BOOL CalcTwoTerm_Relational(int idCalc,int *type,LONG_PTR *index_stack,int *pStackPointer);
284
285//NumOpe_TypeOperation.cpp
286void ExtendTypeTo64(int type,int reg);
287void ExtendTypeTo32(int type,int reg);
288void ExtendTypeTo16(int type,int reg);
289void ChangeTypeToXmm_Double(int type,int xmm_reg,int general_reg);
290void ChangeTypeToXmm_Single(int type,int xmm_reg,int general_reg);
291void ChangeTypeToWhole(int OldType,int NewType,int reg,int xmm_reg);
292void SetOneTermToReg_RealCalc(int TermType,int *pXmmReg);
293void SetOneTermToReg_Whole64Calc(int TermType,int *pReg);
294void SetOneTermToReg_Whole32Calc(int TermType,int *pReg);
295void SetTowTermToReg_RealCalc(int AnswerType,int *type,int sp,int *pXmmReg1,int *pXmmReg2);
296void SetTowTermToReg_Whole64Calc(int *type,int sp,int *pReg1,int *pReg2);
297void SetTowTermToReg_Whole32Calc(int *type,int sp,int *pReg1,int *pReg2);
298BOOL Calc_Cast(int *type,LONG_PTR *index_stack,int *pStackPointer);
299
300//Compile_Set_Var.cpp
301BOOL IsUse_r11(RELATIVE_VAR *pRelativeVar);
302void SetStructVariableFromRax( const Type &varType, const Type &calcType, RELATIVE_VAR *pRelativeVar,BOOL bUseHeap);
303void SetRealVariable(int VarType, int CalcType, RELATIVE_VAR *pRelativeVar);
304void SetBooleanVariable(int type,RELATIVE_VAR *pRelative);
305void SetWholeVariable(int varSize,int type,RELATIVE_VAR *pRelative);
306
307//increment.cpp
308void IncDec(int idCalc, const char *lpszLeft, const char *lpszRight);
309
310//Compile_Calc_PushVar.cpp
311void SetXmmReg_DoubleVariable(RELATIVE_VAR *pRelativeVar,int xmm_reg);
312void SetXmmReg_SingleVariable(RELATIVE_VAR *pRelativeVar,int xmm_reg);
313void SetReg_WholeVariable(int type,RELATIVE_VAR *pRelativeVar,int reg);
314
315//Compile_Interface.cpp
316bool CastToInterface( int reg, int vtblReg, const CClass &baseClass, const CClass &interfaceClass );
317
318//Compile_Object.cpp
319void Operator_New( const CClass &classObj, const char *objectSizeStr, const char *parameter,const Type &baseTypeInfo );
320void OpcodeDelete(const char *Parameter, bool isSweeping);
321
322//Compile_Var.cpp
323bool _member_offset(bool isErrorEnabled, bool isWriteAccess, const CClass &objClass, const char *member, RELATIVE_VAR *pRelativeVar, Type &resultType, BOOL bPrivateAccess);
324void SetThisPtrToReg(int reg);
325bool GetVarOffset(bool isErrorEnabled,bool isWriteAccess,const char *NameBuffer,RELATIVE_VAR *pRelativeVar,Type &resultType,int *pss = NULL);
326bool SetInitGlobalData(int offset,const Type &type,const int *SubScripts,const char *InitBuf);
327#define DIMFLAG_INITDEBUGVAR 0x01
328#define DIMFLAG_NONCALL_CONSTRACTOR 0x02
329#define DIMFLAG_STATIC 0x04
330#define DIMFLAG_CONST 0x08
331void dim( char *VarName,int *SubScripts,Type &type, const char *InitBuf,const char *ConstractParameter,DWORD dwFlags);
332void SetVarPtrToReg(int reg,RELATIVE_VAR *pRelativeVar);
333bool Compile_AddGlobalRootsForGc();
334
335//ParamImpl.cpp
336class ParamImpl{
337 char *Parms[255];
338 vector<Type> types;
339 int ParmsNum;
340
341 Type returnType;
342
343 //一時オブジェクト管理用
344 bool useTempObject;
345 bool useTempParameters[255];
346 int StackOffsetOfTempObject[255];
347
348public:
349 ParamImpl(const char *buffer);
350 ParamImpl(const Parameters &params);
351 ~ParamImpl();
352 void SetReturnType( const Type &returnType );
353
354private:
355 bool EvaluateOverloadScore( int level, const Parameters &targetParms, const Type &targetResultType );
356
357public:
358 UserProc *_OverloadSolution( const char *name, std::vector<UserProc *> &subs, bool isEnabledReturnType = false );
359 UserProc *OverloadSolution( const char *name, std::vector<UserProc *> &subs, bool isEnabledReturnType = false );
360
361 void ApplyDefaultParameters( const Parameters &params );
362 bool ErrorCheck( const string &procName, const Parameters &params, int SecondParmNum = -1 );
363 void MacroParameterSupport( const Parameters &params );
364 void SetStructParameter( int reg, const Type &baseType, const char *expression );
365 void SetParameter( const string &procName, const Parameters &params, int SecondParmNum = -1 );
366
367 //一時オブジェクトパラメータの生成と破棄
368 int NewTempParameters( const string &procName, const Parameters &params, int SecondParmNum = -1 );
369 void DeleteTempParameters();
370
371 void BackupParameter(int pi_num);
372 void RestoreParameter(int pi_num);
373};
374
375//CLockParameter.cpp
376#define MAX_LOCKPARMS 255
377class CDBLockParms{
378public:
379 int array_LevelCount[MAX_LOCKPARMS];
380 CDBLockParms();
381 ~CDBLockParms();
382
383 void lock(int level);
384 void unlock(int level);
385};
386
387//Compile_CallProc.cpp
388void AddLocalVarAddrSchedule();
389#define PROCFLAG_NEW 1
390bool Opcode_CallProcPtr(const char *variable, const char *lpszParms,ProcPointer *pProcPointer);
391bool Opcode_CallProc(const char *Parameter,UserProc *pUserProc,DWORD dwFlags,const char *ObjectName,int RefType);
392bool Opcode_CallDllProc( const char *lpszParms,DllProc *pDllProc);
393
394//Compile_ProcOp.cpp
395void CompileLocal();
396
397//Compile_Func.cpp
398int GetFunctionFromName(char *FuncName);
399bool Opcode_CallFunc( const char *Parameter, const int FuncNum, Type &ReturnTypeInfo, bool isCallOn = true );
400
401//OperatorProc.cpp
402void FreeTempObject(int reg,const CClass *pobj_c);
403int CallOperatorProc(int idCalc, const Type &baseType, int *type_stack,LONG_PTR *index_stack,BOOL *bUseHeap,int &sp);
404void CallCastOperatorProc(int reg,Type &calcType,BOOL bCalcUseHeap,const Type &toType);
405void CallIndexerGetterProc(int reg,const CClass *pobj_Class,char *ObjectName,char *Parameter,Type &resultType);
406
407//Compile_Statement.cpp
408void OpcodeOthers(const char *Command);
409void OpcodeIf(char *Parameter);
410void OpcodeGoto(char *Parameter);
411void OpcodeWhile(char *Parameter);
412void OpcodeFor(char *Parameter);
413void OpcodeDo(char *Parameter);
414void OpcodeContinue(void);
415void OpcodeExitSub(void);
416void OpcodeSelect( const char *Parameter );
417void OpcodeCase(char *Parameter);
418void OpcodeGosub(char *Parameter);
419void OpcodeReturn(char *Parameter);
420void OpcodeSetPtrData(char *Parameter,int type);
421
422
423//InsertOpcode.cpp
424void InsertDimStatement_ToProcHead(char *lpszCommand);
425
426
427
428////////////////////////////////
429// AMD64機械語生成に利用する関数郡
430////////////////////////////////
431
432//Mod(モード)
433#define MOD_BASE (char)0x00
434#define MOD_DISP32 (char)0xFF
435#define MOD_BASE_DISP8 (char)0x40
436#define MOD_BASE_DISP32 (char)0x80
437#define MOD_REG (char)0xC0
438
439#define USE_OFFSET 1
440#define NON_OFFSET 0
441
442//amd64_main.cpp
443BOOL IsSafeReg(int reg);
444void op_push(int reg);
445void op_push_value(long data);
446void op_pop(int reg);
447void op_mov_RV (int op_size,int reg,int i32data);
448void op_mov_RV64 (int reg,_int64 i64data);
449void op_mov_RM (int op_size,int reg,int base_reg,int offset,char mod);
450void op_mov_RM_ex (int op_size,int reg,int base_reg1,int base_reg2,int offset,BOOL bUseOffset);
451void op_mov_MR (int op_size,int reg,int base_reg,int offset,char mod);
452void op_mov_MR_ex (int op_size,int reg,int base_reg1,int base_reg2,int offset,BOOL bUseOffset);
453void op_mov_MV (int op_size,int base_reg,int offset,BOOL bUseOffset,int i32data);
454void op_mov_RR (int reg1,int reg2);
455void op_mov64_ToReg (int reg,_int64 i64data);
456void op_mov64_ToReg (int reg,int i32data);
457void op_movsxd (int reg64,int reg32);
458void op_movsx64_FromReg16 (int reg64,int reg16);
459void op_movsx64_FromReg8 (int reg64,int reg8);
460void op_movsx32_FromReg16 (int reg32,int reg16);
461void op_movsx32_FromReg8 (int reg32,int reg8);
462void op_movsx16_FromReg8 (int reg32,int reg8);
463void op_inc (int reg);
464void op_dec (int reg);
465void op_add_RM (int op_size,int reg,int base_reg,int offset,char mod);
466void op_add_RV (int reg,int offset);
467void op_add64_reg (int reg1,int reg2);
468void op_add32_reg (int reg1,int reg2);
469void op_sub_RV (int op_size,int reg,int i32data);
470void op_sub64_reg (int reg1,int reg2);
471void op_sub32_reg (int reg1,int reg2);
472void op_sbb_RR ( int op_size, int reg1, int reg2 );
473void op_imul_RR (int op_size,int reg1,int reg2);
474void op_imul_RV (int op_size,int reg,int i32data);
475void op_div64_reg (int reg);
476void op_idiv64_reg (int reg);
477void op_shl_reg (int op_size,int reg);
478void op_sar_reg (int op_size,int reg);
479void op_shr_reg (int op_size,int reg);
480void op_and_reg (int op_size,int reg1,int reg2);
481void op_and64_value (int reg,int offset);
482void op_and32_value (int reg,int offset);
483void op_or_reg (int op_size,int reg1,int reg2);
484void op_xor_reg (int op_size,int reg1,int reg2);
485void op_not_reg (int op_size,int reg);
486void op_neg ( int reg );
487void op_test (int reg1,int reg2);
488void op_cmp_reg (int op_size,int reg1,int reg2);
489void op_cmp_value (int op_size,int reg,char byte_data);
490void op_setne (int reg);
491void op_movlpd_MR (int xmm_reg,int base_reg,int offset,char mod);
492void op_movlpd_RM (int xmm_reg,int base_reg,int offset,char mod);
493void op_movsd_RR (int xmm_reg1,int xmm_reg2);
494void op_movsd_MR (int xmm_reg,int base_reg,int offset,char mod);
495void op_movss_RR (int xmm_reg1,int xmm_reg2);
496void op_movss_RM (int xmm_reg,int base_reg,int offset,char mod);
497void op_movss_MR (int xmm_reg,int base_reg,int offset,char mod);
498void op_movd_RX (int reg,int xmm_reg);
499void op_cvtsd2ss (int xmm_reg1,int xmm_reg2);
500void op_cvtss2sd (int xmm_reg1,int xmm_reg2);
501void op_cvttsd2si_xmm (int op_size,int reg,int xmm_reg);
502void op_cvttss2si_xmm (int op_size,int reg,int xmm_reg);
503void op_cvtsi2sd_reg (int op_size,int xmm_reg,int reg);
504void op_cvtsi2ss_reg (int op_size,int xmm_reg,int reg);
505void op_comisd (int xmm_reg1,int xmm_reg2);
506void op_comiss (int xmm_reg1,int xmm_reg2);
507void op_rep_movs (int op_size);
508void op_add_rsp(int num);
509void op_sub_rsp(int num);
510void op_add_esp(int num);
511void op_sub_esp(int num);
512void op_fld_ptr_esp(int type);
513void op_zero_reg(int reg);
514void op_call( UserProc *pUserProc );
515void op_call( DllProc *pDllProc );
516void op_ret();
Note: See TracBrowser for help on using the repository browser.