source: dev/BasicCompiler64/Opcode.h@ 127

Last change on this file since 127 was 122, checked in by dai_9181, 18 years ago

(呼び出し単体コードも対応→)関数の戻り値オブジェクトのメンバ・メソッドを一時オブジェクトを介さずに参照できるようにした。

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