source: dev/trunk/abdev/BasicCompiler32/Compile_Func.cpp@ 154

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

CP4を刻印

File size: 12.2 KB
Line 
1#include "../BasicCompiler_Common/common.h"
2#include "Opcode.h"
3
4int GetFunctionFromName(char *FuncName){
5 if(lstrcmpi(FuncName,"CUDbl")==0) return FUNC_CUDBL;
6 if(lstrcmpi(FuncName,"Fix")==0) return FUNC_FIX;
7 if(lstrcmpi(FuncName,"Len")==0) return FUNC_LEN;
8 if(lstrcmpi(FuncName,"AddressOf")==0) return FUNC_ADDRESSOF;
9 if(lstrcmpi(FuncName,"SizeOf")==0) return FUNC_SIZEOF;
10 if(lstrcmpi(FuncName,"VarPtr")==0) return FUNC_VARPTR;
11 if(lstrcmpi(FuncName,"ObjPtr")==0) return FUNC_OBJPTR;
12 if(lstrcmpi(FuncName,"GetDouble")==0) return FUNC_GETDOUBLE;
13 if(lstrcmpi(FuncName,"GetSingle")==0) return FUNC_GETSINGLE;
14 if(lstrcmpi(FuncName,"GetQWord")==0) return FUNC_GETQWORD;
15 if(lstrcmpi(FuncName,"GetDWord")==0) return FUNC_GETDWORD;
16 if(lstrcmpi(FuncName,"GetWord")==0) return FUNC_GETWORD;
17 if(lstrcmpi(FuncName,"GetByte")==0) return FUNC_GETBYTE;
18 return 0;
19}
20
21void Opcode_Func_Fix(const char *lpszParms){
22 Type resultType;
23 if( !NumOpe( lpszParms, Type(), resultType ) ){
24 return;
25 }
26
27 if( resultType.IsDouble() ){
28 //fld qword ptr[esp]
29 op_fld_ptr_esp(DEF_DOUBLE);
30
31 //fnstcw word ptr[esp]
32 OpBuffer[obp++]=(char)0xD9;
33 OpBuffer[obp++]=(char)0x3C;
34 OpBuffer[obp++]=(char)0x24;
35
36 //mov ax,word ptr[esp]
37 OpBuffer[obp++]=(char)0x66;
38 OpBuffer[obp++]=(char)0x8B;
39 OpBuffer[obp++]=(char)0x04;
40 OpBuffer[obp++]=(char)0x24;
41
42 //or ah,0Ch
43 OpBuffer[obp++]=(char)0x80;
44 OpBuffer[obp++]=(char)0xCC;
45 OpBuffer[obp++]=(char)0x0C;
46
47 //mov word ptr[esp-2],ax
48 OpBuffer[obp++]=(char)0x66;
49 OpBuffer[obp++]=(char)0x89;
50 OpBuffer[obp++]=(char)0x44;
51 OpBuffer[obp++]=(char)0x24;
52 OpBuffer[obp++]=(char)0xFE;
53
54 //fldcw word ptr[esp-2]
55 OpBuffer[obp++]=(char)0xD9;
56 OpBuffer[obp++]=(char)0x6C;
57 OpBuffer[obp++]=(char)0x24;
58 OpBuffer[obp++]=(char)0xFE;
59
60 //fistp dword ptr[esp+4]
61 OpBuffer[obp++]=(char)0xDB;
62 OpBuffer[obp++]=(char)0x5C;
63 OpBuffer[obp++]=(char)0x24;
64 OpBuffer[obp++]=(char)0x04;
65
66 //fldcw word ptr[esp]
67 OpBuffer[obp++]=(char)0xD9;
68 OpBuffer[obp++]=(char)0x2C;
69 OpBuffer[obp++]=(char)0x24;
70
71 //add esp,4
72 op_add_esp(4);
73 }
74 else if( resultType.IsSingle() ){
75 //fld dword ptr[esp]
76 op_fld_ptr_esp(DEF_SINGLE);
77
78 //sub esp,4
79 op_sub_esp(4);
80
81 //fnstcw word ptr[esp]
82 OpBuffer[obp++]=(char)0xD9;
83 OpBuffer[obp++]=(char)0x3C;
84 OpBuffer[obp++]=(char)0x24;
85
86 //mov ax,word ptr[esp]
87 OpBuffer[obp++]=(char)0x66;
88 OpBuffer[obp++]=(char)0x8B;
89 OpBuffer[obp++]=(char)0x04;
90 OpBuffer[obp++]=(char)0x24;
91
92 //or ah,0Ch
93 OpBuffer[obp++]=(char)0x80;
94 OpBuffer[obp++]=(char)0xCC;
95 OpBuffer[obp++]=(char)0x0C;
96
97 //mov word ptr[esp-2],ax
98 OpBuffer[obp++]=(char)0x66;
99 OpBuffer[obp++]=(char)0x89;
100 OpBuffer[obp++]=(char)0x44;
101 OpBuffer[obp++]=(char)0x24;
102 OpBuffer[obp++]=(char)0xFE;
103
104 //fldcw word ptr[esp-2]
105 OpBuffer[obp++]=(char)0xD9;
106 OpBuffer[obp++]=(char)0x6C;
107 OpBuffer[obp++]=(char)0x24;
108 OpBuffer[obp++]=(char)0xFE;
109
110 //fistp dword ptr[esp+4]
111 OpBuffer[obp++]=(char)0xDB;
112 OpBuffer[obp++]=(char)0x5C;
113 OpBuffer[obp++]=(char)0x24;
114 OpBuffer[obp++]=(char)0x04;
115
116 //fldcw word ptr[esp]
117 OpBuffer[obp++]=(char)0xD9;
118 OpBuffer[obp++]=(char)0x2C;
119 OpBuffer[obp++]=(char)0x24;
120
121 //add esp,4
122 op_add_esp(4);
123 }
124 else if( resultType.Is64() ){
125 //pop eax
126 op_pop(REG_EAX);
127
128 //add esp,4
129 op_add_esp(4);
130
131 //push eax
132 op_push(REG_EAX);
133 }
134
135 //pop eax
136 op_pop(REG_EAX);
137}
138
139void Opcode_Func_CUDbl(const char *Parameter){
140 Type resultType;
141 if( !NumOpe(Parameter,Type(),resultType) ){
142 return;
143 }
144 ChangeTypeToLong(resultType.GetBasicType());
145
146 //pop eax
147 op_pop(REG_EAX);
148
149 //push 0
150 OpBuffer[obp++]=(char)0x6A;
151 OpBuffer[obp++]=(char)0x00;
152
153 //push eax
154 op_push(REG_EAX);
155
156 //fild qword ptr[esp]
157 OpBuffer[obp++]=(char)0xDF;
158 OpBuffer[obp++]=(char)0x2C;
159 OpBuffer[obp++]=(char)0x24;
160
161 //add esp,8
162 op_add_esp(8);
163}
164void Opcode_Func_Len(const char *Parameter){
165 BOOL bArrayHead;
166
167 const char *tempParm=Parameter;
168 char temporary[VN_SIZE];
169 char temp2[32];
170 Type type;
171 if( !GetVarType(Parameter,type,0) ){
172 sprintf(temporary,"_System_DummyStr2=%s",Parameter);
173 OpcodeCalc(temporary);
174
175 lstrcpy(temp2,"_System_DummyStr2");
176 tempParm=temp2;
177
178 type.SetType( DEF_OBJECT, pobj_DBClass->GetStringClassPtr() );
179 }
180
181 if( type.IsStringClass() ){
182 //Stringオブジェクトの場合
183 sprintf(temporary,"%s.Length",tempParm);
184
185 int reg=REG_RAX;
186 NumOpe(temporary,Type(),Type());
187
188 //pop eax
189 op_pop(REG_EAX);
190
191 return;
192 }
193
194 int SubScripts[MAX_ARRAYDIM];
195 RELATIVE_VAR RelativeVar;
196 if(!GetVarOffsetReadOnly(tempParm,&RelativeVar,type,SubScripts)) return;
197
198 if(type.GetBasicType()&FLAG_PTR){
199 type.SetBasicType( type.GetBasicType() & ( ~FLAG_PTR ) );
200
201 bArrayHead=1;
202 }
203 else bArrayHead=0;
204
205 int typeSize = type.GetSize();
206
207 if(bArrayHead) typeSize*=JumpSubScripts(SubScripts);
208
209 //mov eax,TypeSize
210 OpBuffer[obp++]=(char)0xB8;
211 *((long *)(OpBuffer+obp))=typeSize;
212 obp+=sizeof(long);
213}
214void Opcode_Func_AddressOf( const char *name ){
215 extern int cp;
216 UserProc *pUserProc;
217
218 extern LONG_PTR ProcPtr_BaseIndex;
219 if(ProcPtr_BaseIndex!=-1){
220 //左辺の型にのっとり、オーバーロードを解決
221
222 std::vector<UserProc *> subs;
223 GetOverloadSubHash( name, subs );
224 if( subs.size() == 0 ){
225 SetError(27,name,cp);
226 return;
227 }
228
229 //オーバーロードを解決
230 pUserProc=OverloadSolution(name,subs,Smoothie::Meta::procPointers[ProcPtr_BaseIndex]->Params(), Type() );
231
232 if(!pUserProc){
233 SetError(27,name,cp);
234 return;
235 }
236 }
237 else{
238 pUserProc=GetSubHash(name);
239 if(!pUserProc){
240 SetError(27,name,cp);
241 return;
242 }
243 }
244
245 if( pUserProc->IsVirtual() ){
246 ///////////////////////////////
247 // 仮想関数の場合
248 // thisポインタをrcxにコピー
249 ///////////////////////////////
250
251 const CClass *pobj_c;
252
253 char ObjectName[VN_SIZE];
254 int RefType;
255 SplitObjectName(name,ObjectName,&RefType);
256
257 if(ObjectName[0]){
258 if(lstrcmpi(ObjectName,"Super")==0) goto InClassMember;
259 else{
260 RELATIVE_VAR RelativeVar;
261 Type type;
262 if(!GetVarOffsetReadOnly(ObjectName,&RelativeVar,type)) return;
263 SetVarPtrToEax(&RelativeVar);
264
265 //mov ecx,eax
266 op_mov_RR(REG_ECX,REG_EAX);
267
268 //参照タイプが整合しているかをチェック
269 if(type.GetBasicType()!=RefType) SetError(104,ObjectName,cp);
270
271 if(type.IsObjectPtr()){
272 //mov ecx,dword ptr[ecx]
273 op_mov_RM(sizeof(long),REG_ECX,REG_ECX,0,MOD_BASE);
274 }
275 }
276 }
277 else{
278InClassMember:
279 //自身のオブジェクトのThisポインタをrcxにコピー
280 SetThisPtrToReg(REG_RCX);
281
282 pobj_c=pobj_CompilingClass;
283 }
284
285
286 //仮想関数(オブジェクトメソッド)
287 //pObj->func_table->func1
288 // ->func2
289 // ->func3
290
291 //mov edx,dword ptr[ecx]
292 OpBuffer[obp++]=(char)0x8B;
293 OpBuffer[obp++]=(char)0x11;
294
295 int i2 = pobj_c->GetFuncNumInVtbl( pUserProc );
296
297 //mov eax,dword ptr[edx+func_index]
298 if(i2*PTR_SIZE<=0x7F){
299 op_mov_RM(sizeof(long),REG_EAX,REG_EDX,i2*PTR_SIZE,MOD_BASE_DISP8);
300 }
301 else{
302 op_mov_RM(sizeof(long),REG_EAX,REG_EDX,i2*PTR_SIZE,MOD_BASE_DISP32);
303 }
304 }
305 else{
306 //一般の関数
307
308 //mov eax,ProcAddr
309 OpBuffer[obp++]=(char)0xB8;
310 pobj_SubAddrSchedule->add(pUserProc,0);
311 obp+=sizeof(long);
312 }
313
314 pUserProc->Using();
315}
316void Opcode_Func_SizeOf( const string &typeName ){
317 Type tempType;
318 if( !Type::StringToType( typeName, tempType ) ){
319 SetError(3,typeName,cp);
320 return;
321 }
322
323 int typeSize = ( tempType.IsObject() ) ?
324 tempType.GetClass().GetSize() : tempType.GetSize();
325
326 //mov eax,size
327 op_mov_RV( REG_EAX, typeSize );
328}
329void Opcode_Func_VarPtr( const char *Parameter, Type &resultType, bool isCallOn ){
330 if( isCallOn == false ){
331 // 戻り値の型を取得するだけ
332
333 //変数のアドレスを取得
334 if(!GetVarType( Parameter, resultType, true )) return;
335
336 resultType.PtrLevelUp();
337
338 return;
339 }
340
341 RELATIVE_VAR RelativeVar;
342
343 //変数のアドレスを取得
344 if(!GetVarOffsetReadOnly( Parameter, &RelativeVar, resultType )) return;
345
346 int beforeType = resultType.GetBasicType();
347
348 resultType.PtrLevelUp();
349
350 SetVarPtrToEax(&RelativeVar);
351
352 // TODO: 取り除く(この動きはObjPtrに託す)
353 /*
354 if( beforeType == DEF_OBJECT && lstrcmpi( Parameter, "This" ) != 0 ){
355 //参照をオブジェクトポインタに変更
356
357 //mov eax,dword ptr[eax]
358 op_mov_RM( sizeof(long), REG_EAX, REG_EAX, 0, MOD_BASE );
359
360 SetError(-120,NULL,cp);
361 }*/
362}
363void Opcode_Func_ObjPtr( const char *Parameter, Type &resultType, bool isCallOn ){
364 if( isCallOn == false ){
365 // 戻り値の型を取得するだけ
366
367 //変数のアドレスを取得
368 if(!GetVarType( Parameter, resultType, true )) return;
369
370 resultType.PtrLevelUp();
371
372 return;
373 }
374
375 RELATIVE_VAR RelativeVar;
376
377 //変数のアドレスを取得
378 if(!GetVarOffsetReadOnly( Parameter, &RelativeVar, resultType )) return;
379
380 int beforeType = resultType.GetBasicType();
381
382 resultType.PtrLevelUp();
383
384 SetVarPtrToEax(&RelativeVar);
385
386 if( lstrcmpi( Parameter, "This" )==0 ){
387 // Thisの場合は特別にオブジェクトポインタが返ってくるので、何もせずに抜ける
388 }
389 else if( beforeType == DEF_OBJECT ){
390 //参照をオブジェクトポインタに変更
391
392 //mov eax,dword ptr[eax]
393 op_mov_RM( sizeof(long), REG_EAX, REG_EAX, 0, MOD_BASE );
394 }
395 else{
396 SetError(134,NULL,cp );
397 }
398}
399void Opcode_Func_GetPtrData(const char *Parameter,const int type){
400 Type tempType;
401 if( !NumOpe(Parameter,Type(),tempType) ){
402 return;
403 }
404 if(!tempType.IsWhole()){
405 SetError(11,Parameter,cp);
406 return;
407 }
408 ChangeTypeToLong(tempType.GetBasicType());
409
410 if(type==DEF_DOUBLE){
411 //pop eax
412 op_pop(REG_EAX);
413
414 //fld qword ptr[eax]
415 OpBuffer[obp++]=(char)0xDD;
416 OpBuffer[obp++]=(char)0x00;
417 }
418 else if(type==DEF_SINGLE||type==DEF_DWORD){
419 //pop eax
420 op_pop(REG_EAX);
421
422 //mov eax,dword ptr[eax]
423 OpBuffer[obp++]=(char)0x8B;
424 OpBuffer[obp++]=(char)0x00;
425 }
426 else if(type==DEF_QWORD){
427 //pop ecx
428 op_pop(REG_ECX);
429
430 //mov eax,dword ptr[ecx]
431 op_mov_RM(sizeof(long),REG_EAX,REG_ECX,0,MOD_BASE);
432
433 //mov edx,dword ptr[ecx+sizeof(long)]
434 op_mov_RM(sizeof(long),REG_EDX,REG_ECX,sizeof(long),MOD_BASE_DISP8);
435 }
436 else if(type==DEF_WORD){
437 //pop ebx
438 op_pop(REG_EBX);
439
440 //xor eax,eax
441 OpBuffer[obp++]=(char)0x33;
442 OpBuffer[obp++]=(char)0xC0;
443
444 //mov ax,word ptr[ebx]
445 OpBuffer[obp++]=(char)0x66;
446 OpBuffer[obp++]=(char)0x8B;
447 OpBuffer[obp++]=(char)0x03;
448 }
449 else if(type==DEF_BYTE){
450 //pop ebx
451 op_pop(REG_EBX);
452
453 //xor eax,eax
454 OpBuffer[obp++]=(char)0x33;
455 OpBuffer[obp++]=(char)0xC0;
456
457 //mov al,byte ptr[ebx]
458 OpBuffer[obp++]=(char)0x8A;
459 OpBuffer[obp++]=(char)0x03;
460 }
461}
462
463bool Opcode_CallFunc( const char *Parameter, const int FuncNum, Type &resultType, bool isCallOn ){
464 switch(FuncNum){
465 case FUNC_FIX:
466 if( isCallOn ) Opcode_Func_Fix(Parameter);
467 resultType.SetBasicType( DEF_LONG );
468 break;
469 case FUNC_CUDBL:
470 if( isCallOn ) Opcode_Func_CUDbl(Parameter);
471 resultType.SetBasicType( DEF_DOUBLE );
472 break;
473 case FUNC_LEN:
474 if( isCallOn ) Opcode_Func_Len(Parameter);
475 resultType.SetBasicType( DEF_LONG );
476 break;
477 case FUNC_ADDRESSOF:
478 if( isCallOn ) Opcode_Func_AddressOf(Parameter);
479 resultType.SetBasicType( DEF_PTR_VOID );
480 break;
481 case FUNC_SIZEOF:
482 if( isCallOn ) Opcode_Func_SizeOf(Parameter);
483 resultType.SetBasicType( DEF_LONG );
484 break;
485 case FUNC_VARPTR:
486 Opcode_Func_VarPtr( Parameter, resultType, isCallOn );
487 break;
488 case FUNC_OBJPTR:
489 Opcode_Func_ObjPtr( Parameter, resultType, isCallOn );
490 break;
491
492 case FUNC_GETDOUBLE:
493 if( isCallOn ) Opcode_Func_GetPtrData(Parameter,DEF_DOUBLE);
494 resultType.SetBasicType( DEF_DOUBLE );
495 break;
496 case FUNC_GETSINGLE:
497 if( isCallOn ) Opcode_Func_GetPtrData(Parameter,DEF_SINGLE);
498 resultType.SetBasicType( DEF_SINGLE );
499 break;
500 case FUNC_GETQWORD:
501 if( isCallOn ) Opcode_Func_GetPtrData(Parameter,DEF_QWORD);
502 resultType.SetBasicType( DEF_QWORD );
503 break;
504 case FUNC_GETDWORD:
505 if( isCallOn ) Opcode_Func_GetPtrData(Parameter,DEF_DWORD);
506 resultType.SetBasicType( DEF_DWORD );
507 break;
508 case FUNC_GETWORD:
509 if( isCallOn ) Opcode_Func_GetPtrData(Parameter,DEF_WORD);
510 resultType.SetBasicType( DEF_WORD );
511 break;
512 case FUNC_GETBYTE:
513 if( isCallOn ) Opcode_Func_GetPtrData(Parameter,DEF_BYTE);
514 resultType.SetBasicType( DEF_BYTE );
515 break;
516 default:
517 return false;
518 }
519 return true;
520}
Note: See TracBrowser for help on using the repository browser.