source: dev/BasicCompiler64/Opcode.h@ 60

Last change on this file since 60 was 56, checked in by dai_9181, 18 years ago

・[Unicode]リテラル文字列のスイッチング
・[Unicode]Char型を文字型として扱うようにする
・[Unicode]SByte型を追加する
に対応。

/unicodeコマンドオプションに対応。

File size: 16.1 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//RSrcSection.cpp
233char *GetRSrcSectionBuffer(int *pLen);
234
235//Compile.cpp
236void ChangeOpcode(char *Command);
237void GetGlobalDataForDll(void);
238DWORD CompileBuffer(char Return_Sequence,WORD Return_Command);
239
240//Register.cpp
241BOOL IsGeneralReg(int reg);
242BOOL IsXmmReg(int reg);
243BOOL IsVolatileReg(int reg);
244
245//Compile_Calc.cpp
246void SetVariableFromRax(int VarType,int CalcType,RELATIVE_VAR *pRelativeVar);
247void OpcodeCalc(char *Command);
248void SetRefVariable( const char *varname, const char *expression );
249
250//NumOpe.cpp
251int NumOpe(int *pReg,const char *Command,int BaseType,LONG_PTR lpBaseIndex,LONG_PTR *plpIndex,BOOL *pbUseHeap=0);
252
253//NumOpe_Arithmetic.cpp
254BOOL CalcTwoTerm_Arithmetic(int idCalc,int *type,LONG_PTR *index_stack,int *pStackPointer);
255BOOL Calc_Mod(int *type,LONG_PTR *index_stack,int *pStackPointer);
256BOOL Calc_Divide(int *type,int *pStackPointer,int BaseType);
257BOOL Calc_IntDivide(int *type,LONG_PTR *index_stack,int *pStackPointer);
258BOOL Calc_MinusMark(int *type,int sp);
259BOOL Calc_Power(int *type,int *pStackPointer);
260BOOL Calc_Shift(int idCalc,int *type,int *pStackPointer);
261
262//NumOpe_Logical.cpp
263BOOL CalcTwoTerm_Logical(int idCalc,int *type,LONG_PTR *index_stack,int *pStackPointer);
264BOOL Calc_Not(int *type,int sp);
265
266//NumOpe_Relation.cpp
267BOOL CalcTwoTerm_Relational(int idCalc,int *type,LONG_PTR *index_stack,int *pStackPointer);
268
269//NumOpe_TypeOperation.cpp
270void ExtendTypeTo64(int type,int reg);
271void ExtendTypeTo32(int type,int reg);
272void ExtendTypeTo16(int type,int reg);
273void ChangeTypeToXmm_Double(int type,int xmm_reg,int general_reg);
274void ChangeTypeToXmm_Single(int type,int xmm_reg,int general_reg);
275void ChangeTypeToWhole(int OldType,int NewType,int reg,int xmm_reg);
276void SetOneTermToReg_RealCalc(int TermType,int *pXmmReg);
277void SetOneTermToReg_Whole64Calc(int TermType,int *pReg);
278void SetOneTermToReg_Whole32Calc(int TermType,int *pReg);
279void SetTowTermToReg_RealCalc(int AnswerType,int *type,int sp,int *pXmmReg1,int *pXmmReg2);
280void SetTowTermToReg_Whole64Calc(int *type,int sp,int *pReg1,int *pReg2);
281void SetTowTermToReg_Whole32Calc(int *type,int sp,int *pReg1,int *pReg2);
282BOOL Calc_Cast(int *type,LONG_PTR *index_stack,int *pStackPointer);
283
284//Compile_Set_Var.cpp
285BOOL IsUse_r11(RELATIVE_VAR *pRelativeVar);
286void SetObjectVariableFromRax(LONG_PTR lpVarIndex,int CalcType,LONG_PTR lpCalcIndex,RELATIVE_VAR *pRelativeVar,BOOL bUseHeap);
287void SetDoubleVariable(int type,RELATIVE_VAR *pRelative);
288void SetSingleVariable(int type,RELATIVE_VAR *pRelative);
289void SetBooleanVariable(int type,RELATIVE_VAR *pRelative);
290void SetWholeVariable(int var_size,int type,RELATIVE_VAR *pRelative);
291
292//increment.cpp
293void IncDec(int idCalc, char *lpszLeft, char *lpszRight);
294
295//calc2.cpp
296#define EXP_TYPE_NUMBER 1
297#define EXP_TYPE_EAX 2
298#define EXP_TYPE_FPU 3
299#define EXP_TYPE_VAR 4
300int NumOpEx(char *Command,double *pDbl,DWORD *pdwType,RELATIVE_VAR *pRelativeVar);
301
302//Compile_Calc_PushVar.cpp
303void SetXmmReg_DoubleVariable(RELATIVE_VAR *pRelativeVar,int xmm_reg);
304void SetXmmReg_SingleVariable(RELATIVE_VAR *pRelativeVar,int xmm_reg);
305void SetReg_WholeVariable(int type,RELATIVE_VAR *pRelativeVar,int reg);
306
307//Compile_Object.cpp
308int Operator_New(const char *Parameter,LONG_PTR *plpIndex);
309void OpcodeDelete(const char *Parameter);
310
311//Compile_Var.cpp
312void GetWithName(char *buffer);
313void SetThisPtrToReg(int reg);
314BOOL GetVarOffset(bool isErrorEnabled,bool isWriteAccess,const char *NameBuffer,int *pType,RELATIVE_VAR *pRelativeVar,LONG_PTR *plpIndex,int *pss=0);
315BOOL SetInitGlobalData(int offset,int type,LONG_PTR lpIndex,int *SubScripts,char *InitBuf);
316#define DIMFLAG_INITDEBUGVAR 0x01
317#define DIMFLAG_NONCALL_CONSTRACTOR 0x02
318#define DIMFLAG_STATIC 0x04
319#define DIMFLAG_CONST 0x08
320void OpcodeDim(char *Parameter,DWORD dwFlag);
321void SetVarPtrToReg(int reg,RELATIVE_VAR *pRelativeVar);
322
323//CParameter.cpp
324#define OVERLOAD_LEVEL1 1
325#define OVERLOAD_LEVEL2 2
326#define OVERLOAD_LEVEL3 3
327class CParameter{
328 char *Parms[255];
329 TYPEINFO types[255];
330 int ParmsNum;
331
332 TYPEINFO ReturnTypeInfo;
333
334 //一時オブジェクト管理用
335 bool useTempObject;
336 bool useTempParameters[255];
337 int StackOffsetOfTempObject[255];
338
339public:
340 CParameter(const char *buffer);
341 CParameter(const PARAMETER_INFO *pParamInfo, const int ParmNum);
342 ~CParameter();
343 void SetReturnType(TYPEINFO *pTypeInfo);
344
345private:
346 BOOL _overload_check(PARAMETER_INFO *ppi,int pi_num,TYPEINFO *pReturnTypeInfo,int overload_level);
347 SUBINFO *OverloadSolutionWithReturnType( const char *name, std::vector<SUBINFO *> &subs );
348public:
349 SUBINFO *OverloadSolution( const char *name, std::vector<SUBINFO *> &subs );
350
351 BOOL ErrorCheck(const char *FuncName,PARAMETER_INFO *ppi,int pi_num,int SecondParmNum);
352 void MacroParameterSupport(PARAMETER_INFO *ppi);
353 void SetObjectParameter(int reg,CClass *pobj_Class,LPSTR Parameter);
354
355 //一時オブジェクトパラメータの生成と破棄
356 void NewTempParameters( const char *FuncName,PARAMETER_INFO *ppi,int pi_num,int SecondParmNum );
357 void DeleteTempParameters();
358
359 void SetParameter(const char *FuncName,PARAMETER_INFO *ppi,int pi_num,int SecondParmNum);
360
361 void BackupParameter(int pi_num);
362 void RestoreParameter(int pi_num);
363};
364
365//CLockParameter.cpp
366#define MAX_LOCKPARMS 255
367class CDBLockParms{
368public:
369 int array_LevelCount[MAX_LOCKPARMS];
370 CDBLockParms();
371 ~CDBLockParms();
372
373 void lock(int level);
374 void unlock(int level);
375};
376
377//Compile_CallProc.cpp
378void AddLocalVarAddrSchedule();
379#define PROCFLAG_NEW 1
380int Opcode_CallProcPtr(char *variable,char *Parameter,PROCPTRINFO *pi,LONG_PTR *plpIndex);
381void Opcode_CallProc(char *Parameter,SUBINFO *psi,DWORD dwFlags,char *ObjectName,int RefType);
382int Opcode_CallDllProc(char *Parameter,DECLAREINFO *pdi,LONG_PTR *plpIndex);
383
384//Compile_ProcOp.cpp
385void CompileLocal();
386
387//Compile_Func.cpp
388int GetFunctionType(int FuncNum);
389int GetFunctionFromName(char *FuncName);
390void Opcode_CallFunc( const char *Parameter, const int FuncNum, TYPEINFO &ReturnTypeInfo );
391
392//OperatorProc.cpp
393void FreeTempObject(int reg,CClass *pobj_c);
394int CallOperatorProc(int idCalc,TYPEINFO *pBaseTypeInfo,int *type,LONG_PTR *index_stack,BOOL *bUseHeap,int &sp);
395void CallCastOperatorProc(int reg,int &CalcType,LONG_PTR &lpCalcIndex,BOOL bCalcUseHeap,int ToType,LONG_PTR lpToIndex);
396void CallIndexerGetterProc(int reg,CClass *pobj_Class,char *ObjectName,char *Parameter,TYPEINFO &RetTypeInfo);
397
398//Compile_Statement.cpp
399void OpcodeOthers(char *Command);
400void OpcodeIf(char *Parameter);
401void OpcodeGoto(char *Parameter);
402void OpcodeWhile(char *Parameter);
403void OpcodeFor(char *Parameter);
404void OpcodeDo(char *Parameter);
405void OpcodeContinue(void);
406void OpcodeExitSub(void);
407void OpcodeSelect(char *Parameter);
408void OpcodeCase(char *Parameter);
409void OpcodeGosub(char *Parameter);
410void OpcodeReturn(char *Parameter);
411void Opcode_Input(char *Parameter);
412void Opcode_Print(char *Parameter,BOOL bWrite);
413void OpcodeCallPtr(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_mov64_ToReg_FromReg (int reg1,int reg2);
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_add64_value (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_reg (int op_size,int reg1,int reg2);
469void op_imul_value (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( SUBINFO *psi );
510void op_call( DECLAREINFO *pdi );
Note: See TracBrowser for help on using the repository browser.