source: dev/BasicCompiler64/Opcode.h@ 129

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

_System_StartupProgramの呼び出し順序を変更。

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