source: dev/trunk/abdev/BasicCompiler32/Opcode.h@ 210

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

コード全体のリファクタリングを実施

File size: 12.4 KB
Line 
1#pragma once
2
3#include <Type.h>
4#include <Procedure.h>
5
6
7//未定義の定数情報
8#define IMAGE_FILE_MACHINE_AMD64 0x8664
9
10
11//レジスタを示す定数
12#define REG_NON -1
13#define REG_EAX 0x00 //reg:000
14#define REG_ECX 0x01 //reg:001
15#define REG_EDX 0x02 //reg:010
16#define REG_EBX 0x03 //reg:011
17#define REG_ESP 0x04 //reg:100
18#define REG_EBP 0x05 //reg:101
19#define REG_ESI 0x06 //reg:110
20#define REG_EDI 0x07 //reg:111
21
22#define REG_RAX REG_EAX
23#define REG_RCX REG_ECX
24#define REG_RDX REG_EDX
25#define REG_RBX REG_EBX
26#define REG_RSP REG_ESP
27#define REG_RBP REG_EBP
28#define REG_RSI REG_ESI
29#define REG_RDI REG_EDI
30
31#define REGISTER_OPERAND(reg) (reg&0x07)
32
33//変数の種類
34#define NON_VAR 0
35#define VAR_GLOBAL 1 // Global Variable
36#define VAR_REFGLOBAL 2 // Global Refference Variable
37#define VAR_LOCAL 3 // Local Variable
38#define VAR_REFLOCAL 4 // Local Refference Variable
39#define VAR_DIRECTMEM 5 // Direct memory
40
41
42extern int cp;
43extern int obp;
44extern char *OpBuffer;
45
46#define breakpoint OpBuffer[obp++]=(char)0xCC;
47
48
49//ラベルアドレス
50struct LABEL{
51 char *pName;
52 int line;
53 DWORD address;
54};
55
56//Goto未知ラベル
57struct GOTOLABELSCHEDULE{
58 char *pName;
59 int line;
60 DWORD pos;
61 DWORD now_cp;
62};
63
64//プロシージャの種類
65#define PROC_DEFAULT 1 //ユーザー定義関数
66#define PROC_DLL 2 //DLL関数
67#define PROC_BUILTIN 3 //コンパイラ埋め込み型
68#define PROC_PTR 4 //関数ポインタ
69
70//プロシージャ
71struct PROCEDURE{
72 char name[255];
73 int address;
74 int types[MAX_PARMS];
75 _int8 ByVal[MAX_PARMS];
76 BOOL ReturnType;
77};
78
79//With情報
80struct WITHINFO{
81 char **ppName;
82 int *pWithCp;
83 int num;
84};
85
86
87//RSrcSection.cpp
88char *GetRSrcSectionBuffer(int *pLen);
89
90//Compile.cpp
91void ChangeOpcode(char *Command);
92void GetGlobalDataForDll(void);
93DWORD CompileBuffer(char Return_Sequence,WORD Return_Command);
94
95//Compile_Calc.cpp
96void ChangeTypeToDouble_ToFpuReg(int OldType);
97void ChangeTypeToDouble(int OldType);
98void ChangeTypeToSingle(int OldType);
99void ChangeTypeToInt64(int OldType);
100void ChangeTypeToLong(int OldType);
101void ChangeTypeToInteger(int OldType);
102void ChangeTypeToByte(int OldType);
103void SetVariableFromEax(int VarType,int CalcType,RELATIVE_VAR *pRelativeVar);
104void OpcodeCalc( const char *Command );
105
106//NumOpe.cpp
107void PushReturnValue(int type);
108bool TermOpe(
109 const char *term,
110 const Type &baseType,
111 Type &resultType,
112 bool &isLiteral,
113 BOOL *pbUseHeap,
114 bool isWantObject = false,
115 bool *pIsClassName = NULL,
116 bool isProcedureCallOnly = false );
117bool NumOpe( int reg,
118 const char *expression,
119 const Type &baseType,
120 Type &resultType,
121 BOOL *pbUseHeap = NULL );
122bool NumOpe( const char *Command,
123 const Type &baseType,
124 Type &resultType,
125 BOOL *pbUseHeap = NULL );
126
127//NumOpe_Arithmetic.cpp
128void GetStackData_ToRegister(int *type,int sp);
129BOOL CalcTwoTerm_Arithmetic(int idCalc,int *type,LONG_PTR *index_stack,int *pStackPointer);
130BOOL Calc_Mod(int *type,int *pStackPointer);
131BOOL Calc_Divide(int *type,int *pStackPointer,int BaseType);
132BOOL Calc_IntDivide(int *type,LONG_PTR *index_stack,int *pStackPointer);
133BOOL Calc_MinusMark(int *type,int sp);
134BOOL Calc_Power(int *type,int *pStackPointer);
135BOOL Calc_Cast(int *type,LONG_PTR *index_stack,int *pStackPointer);
136BOOL Calc_SHL(int *type,int *pStackPointer);
137BOOL Calc_SHR(int *type,int *pStackPointer);
138
139//NumOpe_Logical.cpp
140BOOL Calc_Xor(int *type,LONG_PTR *index_stack,int *pStackPointer);
141BOOL Calc_Or(int *type,LONG_PTR *index_stack,int *pStackPointer);
142BOOL Calc_And(int *type,LONG_PTR *index_stack,int *pStackPointer);
143BOOL Calc_Not(int *type,int sp);
144
145//NumOpe_Relation.cpp
146BOOL Calc_Relation_PE(int *type_stack,LONG_PTR *index_stack,int *pStackPointer);
147BOOL Calc_Relation_QE(int *type_stack,LONG_PTR *index_stack,int *pStackPointer);
148BOOL Calc_Relation_P(int *type_stack,LONG_PTR *index_stack,int *pStackPointer);
149BOOL Calc_Relation_Q(int *type_stack,LONG_PTR *index_stack,int *pStackPointer);
150BOOL Calc_Relation_NotEqual(int *type,int *pStackPointer);
151BOOL Calc_Relation_Equal(int *type,int *pStackPointer);
152
153//NumOpe_TypeOperation.cpp
154void ExtendStackTo64(int type);
155void ChangeTypeToWhole(int OldType,int NewType);
156
157//Compile_Set_Var.cpp
158BOOL IsUse_ecx(RELATIVE_VAR *pRelativeVar);
159void SetStructVariable( const Type &varType, const Type &calcType, BOOL bUseHeap);
160void SetRealVariable(int VarType, int CalcType, RELATIVE_VAR *pRelativeVar);
161void SetWholeVariable( int varSize,int calcType, RELATIVE_VAR *pRelative );
162
163void SetDoubleVariable(int type,DWORD VarKind,DWORD offset,BOOL bOffsetOffset);
164void SetSingleVariable(int type,DWORD VarKind,DWORD offset,BOOL bOffsetOffset);
165void SetInt64Variable(int type,RELATIVE_VAR *pRelative);
166void SetDWordVariable(int type,DWORD VarKind,DWORD offset,BOOL bOffsetOffset);
167void SetLongVariable(int type,DWORD VarKind,DWORD offset,BOOL bOffsetOffset);
168void Set16Variable(int type,DWORD VarKind,DWORD offset,BOOL bOffsetOffset);
169void Set8Variable(int type,DWORD VarKind,DWORD offset,BOOL bOffsetOffset);
170void SetBooleanVariable(int type,RELATIVE_VAR *pRelative);
171void ExtendTypeTo64(int type);
172void ExtendTypeTo32(int type,int reg);
173void ExtendTypeTo16(int type,int reg);
174
175//increment.cpp
176void IncDec(int idCalc, const char *lpszLeft, const char *lpszRight);
177
178//calc2.cpp
179#define EXP_TYPE_NUMBER 1
180#define EXP_TYPE_EAX 2
181#define EXP_TYPE_FPU 3
182#define EXP_TYPE_VAR 4
183int NumOpEx(char *Command,double *pDbl,DWORD *pdwType,RELATIVE_VAR *pRelativeVar);
184
185//SetVar.cpp
186BOOL SetVariable(DWORD dwVarType,RELATIVE_VAR *pVarRelativeVar,
187 DWORD dwExpType,DWORD dwType,void *data);
188
189//Compile_Calc_PushVar.cpp
190void SetReg_RealVariable(int type,RELATIVE_VAR *pRelativeVar);
191void SetReg_WholeVariable(int type,RELATIVE_VAR *pRelativeVar,int reg, bool is64Head = false);
192void PushDoubleVariable(RELATIVE_VAR *pRelativeVar);
193void PushLongVariable(RELATIVE_VAR *pRelativeVar);
194void PushIntegerVariable(RELATIVE_VAR *pRelativeVar);
195void PushWordVariable(RELATIVE_VAR *pRelativeVar);
196void PushCharVariable(RELATIVE_VAR *pRelativeVar);
197void PushByteVariable(RELATIVE_VAR *pRelativeVar);
198
199//Compile_Object.cpp
200void Operator_New( const CClass &classObj, const char *objectSizeStr, const char *parameter, const Type &baseType );
201void OpcodeDelete(const char *Parameter, bool isSweeping);
202
203//Compile_Var.cpp
204void SetRelativeOffset( RELATIVE_VAR &relativeVar );
205bool _member_offset(bool isErrorEnabled, bool isWriteAccess, const CClass &objClass, const char *member, RELATIVE_VAR *pRelativeVar, Type &resultType, BOOL bPrivateAccess);
206void SetThisPtrToReg(int reg);
207bool GetVarOffset(bool isErrorEnabled,bool isWriteAccess,const char *NameBuffer,RELATIVE_VAR *pRelativeVar,Type &resultType, Subscripts *pResultSubscripts = NULL );
208bool SetInitGlobalData(int offset,const Type &type,const Subscripts &subscripts,const char *InitBuf);
209#define DIMFLAG_INITDEBUGVAR 1
210#define DIMFLAG_NONCALL_CONSTRACTOR 2
211#define DIMFLAG_STATIC 4
212#define DIMFLAG_CONST 8
213void dim( char *VarName, const Subscripts &subscripts, Type &type, const char *InitBuf,const char *ConstractParameter,DWORD dwFlags);
214void SetVarPtrToEax(RELATIVE_VAR *pRelativeVar);
215void SetVarPtrToReg(int reg,RELATIVE_VAR *pRelativeVar);
216bool Compile_AddGlobalRootsForGc();
217
218//ParamImpl.cpp
219class ParamImpl{
220 char *Parms[255];
221 vector<Type> types;
222 int ParmsNum;
223
224 Type returnType;
225
226 //一時オブジェクト管理用
227 bool useTempObject;
228 bool useTempParameters[255];
229 int nCountOfTempObjects;
230
231public:
232 ParamImpl(const char *buffer);
233 ParamImpl(const Parameters &params);
234 ~ParamImpl();
235 void SetReturnType( const Type &returnType );
236
237private:
238 bool EvaluateOverloadScore( int level, const Parameters &targetParms, const Type &targetResultType );
239public:
240 const UserProc *_OverloadSolution( const char *name, std::vector<const UserProc *> &subs, bool isEnabledReturnType = false );
241 const UserProc *OverloadSolution( const char *name, std::vector<const UserProc *> &subs, bool isEnabledReturnType = false );
242
243 void ApplyDefaultParameters( const Parameters &params );
244 bool ErrorCheck( const string &procName, const Parameters &params, int SecondParmNum = -1 );
245 void MacroParameterSupport( const Parameters &params );
246 void SetStructParameter( const Type &baseType, const char *expression );
247 int SetParameter( const string &procName, const Parameters &params, int SecondParmNum = -1 );
248
249 //一時オブジェクトパラメータの生成と破棄
250 int NewTempParameters( const string &procName, const Parameters &params, int SecondParmNum = -1 );
251 void DeleteTempParameters();
252};
253
254//Compile_CallProc.cpp
255void AddLocalVarAddrSchedule();
256#define PROCFLAG_NEW 1
257bool Opcode_CallProcPtr( const char *variable, const char *lpszParms,ProcPointer *pProcPointer);
258bool Opcode_CallProc(const char *Parameter,const UserProc *pUserProc,DWORD dwFlags,const char *ObjectName,int RefType);
259bool Opcode_CallDllProc( const char *lpszParms, DllProc *pDllProc );
260
261//Compile_ProcOp.cpp
262void CompileLocal();
263
264//Compile_Func.cpp
265int GetFunctionFromName(char *FuncName);
266bool Opcode_CallFunc( const char *Parameter, const int FuncNum, Type &resultType, bool isCallOn = true );
267
268//OperatorProc.cpp
269void FreeTempObject(int reg,const CClass *pobj_c);
270int CallOperatorProc(int idCalc, const Type &baseType, int *type_stack,LONG_PTR *index_stack,BOOL *bUseHeap,int &sp);
271void CallCastOperatorProc(Type &calcType,BOOL bCalcUseHeap,const Type &toType);
272void CallIndexerGetterProc(const CClass *pobj_Class,char *ObjectName,char *Parameter,Type &resultType);
273
274//Compile_Statement.cpp
275void OpcodeOthers( const char *Command );
276void OpcodeIf(char *Parameter);
277void OpcodeGoto(char *Parameter);
278void OpcodeWhile(char *Parameter);
279void OpcodeFor(char *Parameter);
280void OpcodeDo(char *Parameter);
281void OpcodeContinue(void);
282void OpcodeExitSub(void);
283void OpcodeSelect(const char *lpszParms);
284void OpcodeCase(char *Parameter);
285void OpcodeGosub(char *Parameter);
286void OpcodeReturn(char *Parameter);
287void OpcodeSetPtrData(char *Parameter,int type);
288
289
290
291
292////////////////////////////////
293// IA-32機械語生成に利用する関数郡
294////////////////////////////////
295
296//Mod(モード)
297#define MOD_BASE (char)0x00
298#define MOD_DISP32 (char)0xFF
299#define MOD_BASE_DISP8 (char)0x40
300#define MOD_BASE_DISP32 (char)0x80
301#define MOD_REG (char)0xC0
302
303#define USE_OFFSET 1
304#define NON_OFFSET 0
305
306//op32_main.cpp
307BOOL IsSafeReg(int reg);
308void op_mov_RV (int reg,int offset);
309void op_mov_RV (int op_size,int reg,int offset);
310void op_mov_RR (int reg1,int reg2);
311void op_mov_RM (int op_size,int reg,int base_reg,int offset,char mod);
312void op_mov_RM_ex (int op_size,int reg,int base_reg1,int base_reg2,int offset,BOOL bUseOffset);
313void op_mov_MR (int op_size,int reg,int base_reg,int offset,char mod);
314void op_mov_MR_ex (int op_size,int reg,int base_reg1,int base_reg2,int offset,BOOL bUseOffset);
315void op_movsx_R32R16 (int reg32,int reg16);
316void op_movsx_R32R8 (int reg32,int reg8);
317void op_movsx_R16R8 (int reg16,int reg8);
318void op_inc (int reg);
319void op_dec (int reg);
320void op_add_RV8 (int reg,char cValue);
321void op_add_RM (int op_size,int reg,int base_reg,int offset,char mod);
322void op_adc_RV8 (int reg,char cValue);
323void op_sub_RV8 (int reg,char cValue);
324void op_sbb_RV8 (int reg,char cValue);
325void op_sbb_RR ( int reg1, int reg2 );
326void op_imul_RR (int reg1,int reg2);
327void op_imul_RV (int reg,int i32data);
328void op_and_RV (int reg,int value);
329void op_or_RR ( int op_size, int reg1, int reg2 );
330void op_neg ( int reg );
331void op_cdq ();
332
333void op_rep_movs (int op_size);
334
335void op_push(int reg);
336void op_push_V(long data);
337void op_pop(int reg = REG_NON);
338void op_add_esp(int num);
339void op_sub_esp(int num);
340void op_cmp_RR( int reg1, int reg2 );
341void op_cmp_value(int op_size,int reg,char byte_data);
342void op_setne( int reg );
343void op_test(int reg1,int reg2);
344void op_fld_ptr_esp(int type);
345void op_fld_basereg (int type,int base_reg);
346void op_fld_base_offset (int type,int base_reg,int offset);
347void op_fld_base_offset_ex (int type,int base_reg1,int base_reg2,int offset,BOOL bUseOffset);
348void op_fstp_basereg (int type,int base_reg);
349void op_fstp_base_offset (int type,int base_reg,int offset);
350void op_fstp_base_offset_ex (int type,int base_reg1,int base_reg2,int offset,BOOL bUseOffset);
351void op_fstp_push ( Type &type );
352void op_fistp_ptr_esp ( int typeSize );
353void op_zero_reg(int reg);
354void fpu_cast();
355void fpu_cast_end();
356
357void op_call(const UserProc *pUserProc);
358void op_ret();
Note: See TracBrowser for help on using the repository browser.