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

Last change on this file since 332 was 332, checked in by dai_9181, 17 years ago
File size: 16.1 KB
Line 
1#include "stdafx.h"
2
3#include <jenga/include/smoothie/Smoothie.h>
4
5#include <Compiler.h>
6
7#include "../BasicCompiler_Common/common.h"
8#include "Opcode.h"
9
10int GetFunctionFromName(char *FuncName){
11 if( lstrcmpi( FuncName, "CUDbl" ) == 0 ) return FUNC_CUDBL;
12 if( lstrcmpi( FuncName, "Fix" ) == 0 ) return FUNC_FIX;
13 if( lstrcmpi( FuncName, "Len" ) == 0 ) return FUNC_LEN;
14 if( lstrcmpi( FuncName, "AddressOf" ) == 0 ) return FUNC_ADDRESSOF;
15 if( lstrcmpi( FuncName, "SizeOf" ) == 0 ) return FUNC_SIZEOF;
16 if( lstrcmpi( FuncName, "VarPtr" ) == 0 ) return FUNC_VARPTR;
17 if( lstrcmpi( FuncName, "ObjPtr" ) == 0 ) return FUNC_OBJPTR;
18 if( lstrcmpi( FuncName, "__delegate_dynamicmethod_call" ) == 0 ) return FUNC_DELEGATE_DYNAMICMETHOD_CALL;
19 if( lstrcmpi( FuncName, "__delegate_staticmethod_call" ) == 0 ) return FUNC_DELEGATE_STATICMETHOD_CALL;
20 if( lstrcmpi( FuncName, "GetDouble" ) == 0 ) return FUNC_GETDOUBLE;
21 if( lstrcmpi( FuncName, "GetSingle" ) == 0 ) return FUNC_GETSINGLE;
22 if( lstrcmpi( FuncName, "GetQWord" ) == 0 ) return FUNC_GETQWORD;
23 if( lstrcmpi( FuncName, "GetDWord" ) == 0 ) return FUNC_GETDWORD;
24 if( lstrcmpi( FuncName, "GetWord" ) == 0 ) return FUNC_GETWORD;
25 if( lstrcmpi( FuncName, "GetByte" ) == 0 ) return FUNC_GETBYTE;
26 return 0;
27}
28
29void Opcode_Func_Fix(const char *lpszParms){
30 Type resultType;
31 if( !NumOpe( lpszParms, Type(), resultType ) ){
32 return;
33 }
34
35 if( resultType.IsDouble() ){
36 //fld qword ptr[esp]
37 compiler.codeGenerator.op_fld_ptr_esp(DEF_DOUBLE);
38
39 //fnstcw word ptr[esp]
40 compiler.codeGenerator.PutOld(
41 (char)0xD9,
42 (char)0x3C,
43 (char)0x24
44 );
45
46 //mov ax,word ptr[esp]
47 compiler.codeGenerator.op_mov_RM( sizeof(short), REG_EAX, REG_ESP, 0, MOD_BASE );
48
49 //or ah,0Ch
50 compiler.codeGenerator.PutOld(
51 (char)0x80,
52 (char)0xCC,
53 (char)0x0C
54 );
55
56 //mov word ptr[esp-2],ax
57 compiler.codeGenerator.op_mov_MR( sizeof(short), REG_EAX, REG_ESP, -2, MOD_BASE_DISP8 );
58
59 //fldcw word ptr[esp-2]
60 compiler.codeGenerator.PutOld(
61 (char)0xD9,
62 (char)0x6C,
63 (char)0x24,
64 (char)0xFE
65 );
66
67 //fistp dword ptr[esp+4]
68 compiler.codeGenerator.PutOld(
69 (char)0xDB,
70 (char)0x5C,
71 (char)0x24,
72 (char)0x04
73 );
74
75 //fldcw word ptr[esp]
76 compiler.codeGenerator.PutOld(
77 (char)0xD9,
78 (char)0x2C,
79 (char)0x24
80 );
81
82 //add esp,4
83 compiler.codeGenerator.op_add_esp(4);
84 }
85 else if( resultType.IsSingle() ){
86 //fld dword ptr[esp]
87 compiler.codeGenerator.op_fld_ptr_esp(DEF_SINGLE);
88
89 //sub esp,4
90 compiler.codeGenerator.op_sub_esp(4);
91
92 //fnstcw word ptr[esp]
93 compiler.codeGenerator.PutOld(
94 (char)0xD9,
95 (char)0x3C,
96 (char)0x24
97 );
98
99 //mov ax,word ptr[esp]
100 compiler.codeGenerator.op_mov_RM( sizeof(short), REG_EAX, REG_ESP, 0, MOD_BASE );
101
102 //or ah,0Ch
103 compiler.codeGenerator.PutOld(
104 (char)0x80,
105 (char)0xCC,
106 (char)0x0C
107 );
108
109 //mov word ptr[esp-2],ax
110 compiler.codeGenerator.op_mov_MR( sizeof(short), REG_EAX, REG_ESP, -2, MOD_BASE_DISP8 );
111
112 //fldcw word ptr[esp-2]
113 compiler.codeGenerator.PutOld(
114 (char)0xD9,
115 (char)0x6C,
116 (char)0x24,
117 (char)0xFE
118 );
119
120 //fistp dword ptr[esp+4]
121 compiler.codeGenerator.PutOld(
122 (char)0xDB,
123 (char)0x5C,
124 (char)0x24,
125 (char)0x04
126 );
127
128 //fldcw word ptr[esp]
129 compiler.codeGenerator.PutOld(
130 (char)0xD9,
131 (char)0x2C,
132 (char)0x24
133 );
134
135 //add esp,4
136 compiler.codeGenerator.op_add_esp(4);
137 }
138 else if( resultType.Is64() ){
139 //pop eax
140 compiler.codeGenerator.op_pop(REG_EAX);
141
142 //add esp,4
143 compiler.codeGenerator.op_add_esp(4);
144
145 //push eax
146 compiler.codeGenerator.op_push(REG_EAX);
147 }
148
149 //pop eax
150 compiler.codeGenerator.op_pop(REG_EAX);
151}
152
153void Opcode_Func_CUDbl(const char *Parameter){
154 Type resultType;
155 if( !NumOpe(Parameter,Type(),resultType) ){
156 return;
157 }
158 ChangeTypeToLong(resultType.GetBasicType());
159
160 //pop eax
161 compiler.codeGenerator.op_pop(REG_EAX);
162
163 //push 0
164 compiler.codeGenerator.op_push_V( 0 );
165
166 //push eax
167 compiler.codeGenerator.op_push(REG_EAX);
168
169 //fild qword ptr[esp]
170 compiler.codeGenerator.op_fld_ptr_esp(DEF_INT64);
171
172 //add esp,8
173 compiler.codeGenerator.op_add_esp(8);
174}
175void Opcode_Func_Len(const char *Parameter){
176 BOOL bArrayHead;
177
178 const char *tempParm=Parameter;
179 char temporary[VN_SIZE];
180 char temp2[32];
181 Type type;
182 if( !GetVarType(Parameter,type,0) ){
183 sprintf(temporary,"_System_DummyStr2=%s",Parameter);
184 OpcodeCalc(temporary);
185
186 lstrcpy(temp2,"_System_DummyStr2");
187 tempParm=temp2;
188
189 type.SetType( DEF_OBJECT, compiler.GetObjectModule().meta.GetClasses().GetStringClassPtr() );
190 }
191
192 if( type.IsStringClass() ){
193 //Stringオブジェクトの場合
194 sprintf(temporary,"%s.Length",tempParm);
195
196 int reg=REG_RAX;
197 NumOpe(temporary,Type(),Type());
198
199 //pop eax
200 compiler.codeGenerator.op_pop(REG_EAX);
201
202 return;
203 }
204
205 Subscripts subscripts;
206 RELATIVE_VAR RelativeVar;
207 if(!GetVarOffsetReadOnly(tempParm,&RelativeVar,type,&subscripts)) return;
208
209 if(type.GetBasicType()&FLAG_PTR){
210 type.SetBasicType( type.GetBasicType() & ( ~FLAG_PTR ) );
211
212 bArrayHead=1;
213 }
214 else bArrayHead=0;
215
216 int typeSize = type.GetSize();
217
218 if(bArrayHead) typeSize*=JumpSubScripts(subscripts);
219
220 //mov eax,typeSize
221 compiler.codeGenerator.op_mov_RV( REG_EAX, typeSize );
222}
223
224void _Opcode_Func_AddressOf( const char *methodInstanceName, const UserProc &userProc )
225{
226 if( userProc.IsVirtual() ){
227 ///////////////////////////////
228 // 仮想関数の場合
229 // thisポインタをrcxにコピー
230 ///////////////////////////////
231
232 const CClass *pobj_c;
233
234 char ObjectName[VN_SIZE];
235 ReferenceKind referenceKind;
236 SplitObjectName( methodInstanceName, ObjectName, referenceKind );
237
238 if(ObjectName[0]){
239 if(lstrcmpi(ObjectName,"Super")==0) goto InClassMember;
240 else{
241 RELATIVE_VAR RelativeVar;
242 Type type;
243 if(!GetVarOffsetReadOnly(ObjectName,&RelativeVar,type)) return;
244 SetVarPtrToEax(&RelativeVar);
245
246 //mov ecx,eax
247 compiler.codeGenerator.op_mov_RR(REG_ECX,REG_EAX);
248
249 //参照タイプが整合しているかをチェック
250 if( !( type.IsObject() && referenceKind == RefDot
251 || type.IsObjectPtr() && referenceKind == RefPointer ) )
252 {
253 SetError(104,ObjectName,cp);
254 }
255
256 if(type.IsObjectPtr()){
257 //mov ecx,dword ptr[ecx]
258 compiler.codeGenerator.op_mov_RM(sizeof(long),REG_ECX,REG_ECX,0,MOD_BASE);
259 }
260 }
261 }
262 else{
263InClassMember:
264 //自身のオブジェクトのThisポインタをrcxにコピー
265 SetThisPtrToReg(REG_RCX);
266
267 pobj_c=compiler.pCompilingClass;
268 }
269
270
271 //仮想関数(オブジェクトメソッド)
272 //pObj->func_table->func1
273 // ->func2
274 // ->func3
275
276 //mov edx,dword ptr[ecx]
277 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EDX, REG_ECX, 0, MOD_BASE );
278
279 int i2 = pobj_c->GetFuncNumInVtbl( &userProc );
280
281 //mov eax,dword ptr[edx+func_index]
282 if(i2*PTR_SIZE<=0x7F){
283 compiler.codeGenerator.op_mov_RM(sizeof(long),REG_EAX,REG_EDX,i2*PTR_SIZE,MOD_BASE_DISP8);
284 }
285 else{
286 compiler.codeGenerator.op_mov_RM(sizeof(long),REG_EAX,REG_EDX,i2*PTR_SIZE,MOD_BASE_DISP32);
287 }
288 }
289 else{
290 //一般の関数
291
292 //mov eax,ProcAddr
293 compiler.codeGenerator.op_addressof( REG_EAX, &userProc );
294 }
295
296 userProc.Using();
297}
298void Opcode_CreateSimpleDelegate( const char *methodInstanceName, const UserProc &userProc )
299{
300 /////////////////////////////////////////////////////////////////
301 // 関数ポインタをpush
302 /////////////////////////////////////////////////////////////////
303
304 //push AddressOf(userProc)
305 _Opcode_Func_AddressOf( methodInstanceName, userProc );
306 compiler.codeGenerator.op_push( REG_EAX );
307
308
309 /////////////////////////////////////////////////////////////////
310 // オブジェクト ポインタをpush
311 /////////////////////////////////////////////////////////////////
312
313 // オブジェクト名を取得
314 char objectName[VN_SIZE];
315 char memberName[VN_SIZE];
316 char *thisPtrName = "This";
317 Type type;
318 if( SplitMemberName( methodInstanceName, objectName, memberName ) )
319 {
320 if( GetVarType( objectName, type, false ) )
321 {
322 thisPtrName = objectName;
323 }
324 }
325
326 // オブジェクト ポインタを取得
327 RELATIVE_VAR relativeVar;
328 GetVarOffsetReadOnly( thisPtrName, &relativeVar, type );
329 if( !type.IsObject() )
330 {
331 extern int cp;
332 SetError(1,NULL,cp);
333 return;
334 }
335
336 SetVarPtrToEax( &relativeVar );
337
338 //mov eax,dword ptr[eax]
339 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EAX, REG_EAX, 0, MOD_BASE );
340
341 //push this
342 compiler.codeGenerator.op_push( REG_EAX );
343
344
345 /////////////////////////////////////////////////////////////////
346 // call _System_CreateSimpleDynamicDelegate
347 /////////////////////////////////////////////////////////////////
348
349 // call _System_CreateSimpleDynamicDelegate
350 extern const UserProc *pSub_System_CreateSimpleDynamicDelegate;
351 compiler.codeGenerator.op_call( pSub_System_CreateSimpleDynamicDelegate );
352}
353void Opcode_Func_AddressOf( const char *name, const Type &baseType, bool isCallOn, Type &resultType ){
354 extern int cp;
355 const UserProc *pUserProc;
356
357 const Parameters *pBaseParams = NULL;
358 bool isDelegate = false;
359 if( baseType.IsProcPtr() )
360 {
361 // 左辺で関数ポインタを要求されているとき
362 pBaseParams = &compiler.GetObjectModule().meta.GetProcPointers()[baseType.GetIndex()]->Params();
363 }
364 else if( baseType.IsObject() && baseType.GetClass().GetName() == "_SimpleDelegate" )
365 {
366 extern const Delegate *pConstructingDelegate;
367 if( !pConstructingDelegate )
368 {
369 SetError();
370 }
371 // 左辺でデリゲートを要求されているとき
372 pBaseParams = &pConstructingDelegate->Params();
373
374 isDelegate = true;
375 }
376
377 if( pBaseParams )
378 {
379 //左辺の型にのっとり、オーバーロードを解決
380
381 std::vector<const UserProc *> subs;
382 GetOverloadSubHash( name, subs );
383 if( subs.size() == 0 ){
384 SetError(27,name,cp);
385 return;
386 }
387
388 //オーバーロードを解決
389 pUserProc=OverloadSolution( name, subs, *pBaseParams, Type() );
390
391 if(!pUserProc){
392 SetError(27,name,cp);
393 return;
394 }
395 }
396 else{
397 pUserProc=GetSubHash(name);
398 if(!pUserProc){
399 SetError(27,name,cp);
400 return;
401 }
402 }
403
404 if( isDelegate )
405 {
406 if( isCallOn )
407 {
408 // デリゲートのとき
409 Opcode_CreateSimpleDelegate( name, *pUserProc );
410 }
411 resultType = baseType;
412 }
413 else
414 {
415 if( isCallOn )
416 {
417 // 関数ポインタのとき
418 _Opcode_Func_AddressOf( name, *pUserProc );
419 }
420 resultType.SetBasicType( DEF_PTR_VOID );
421 }
422}
423void Opcode_Func_SizeOf( const string &typeName ){
424 Type tempType;
425 if( !compiler.StringToType( typeName, tempType ) ){
426 SetError(3,typeName,cp);
427 return;
428 }
429
430 int typeSize = ( tempType.IsObject() ) ?
431 tempType.GetClass().GetSize() : tempType.GetSize();
432
433 //mov eax,size
434 compiler.codeGenerator.op_mov_RV( REG_EAX, typeSize );
435}
436void Opcode_Func_VarPtr( const char *Parameter, Type &resultType, bool isCallOn ){
437 if( isCallOn == false ){
438 // 戻り値の型を取得するだけ
439
440 //変数のアドレスを取得
441 if(!GetVarType( Parameter, resultType, true )) return;
442
443 resultType.PtrLevelUp();
444
445 return;
446 }
447
448 RELATIVE_VAR RelativeVar;
449
450 //変数のアドレスを取得
451 if(!GetVarOffsetReadOnly( Parameter, &RelativeVar, resultType )) return;
452
453 int beforeType = resultType.GetBasicType();
454
455 resultType.PtrLevelUp();
456
457 SetVarPtrToEax(&RelativeVar);
458}
459void Opcode_Func_ObjPtr( const char *Parameter, Type &resultType, bool isCallOn ){
460 if( isCallOn == false ){
461 // 戻り値の型を取得するだけ
462
463 //変数のアドレスを取得
464 if(!GetVarType( Parameter, resultType, true )) return;
465
466 resultType.PtrLevelUp();
467
468 return;
469 }
470
471 RELATIVE_VAR RelativeVar;
472
473 //変数のアドレスを取得
474 if(!GetVarOffsetReadOnly( Parameter, &RelativeVar, resultType )) return;
475
476 int beforeType = resultType.GetBasicType();
477
478 resultType.PtrLevelUp();
479
480 SetVarPtrToEax(&RelativeVar);
481
482 if( lstrcmpi( Parameter, "This" )==0 ){
483 // Thisの場合は特別にオブジェクトポインタが返ってくるので、何もせずに抜ける
484 }
485 else if( beforeType == DEF_OBJECT ){
486 //参照をオブジェクトポインタに変更
487
488 //mov eax,dword ptr[eax]
489 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EAX, REG_EAX, 0, MOD_BASE );
490 }
491 else{
492 SetError(134,NULL,cp );
493 }
494}
495
496void Opcode_Func_delegate_call( const char *paramsStr, Type &resultType, bool isDynamicCall, bool isCallOn )
497{
498 if( isCallOn )
499 {
500 int i = 0;
501 char methodPtrParamStr[VN_SIZE];
502 i = GetOneParameter( paramsStr, i, methodPtrParamStr );
503
504 char objPtrValueStr[VN_SIZE];
505 if( isDynamicCall )
506 {
507 i = GetOneParameter( paramsStr, i, objPtrValueStr );
508 }
509
510 Opcode_CallDelegate( compiler.pCompilingClass->GetDelegate(), methodPtrParamStr, objPtrValueStr, paramsStr + i );
511 }
512
513 resultType = UserProc::CompilingUserProc().ReturnType();
514}
515
516void Opcode_Func_GetPtrData(const char *Parameter,const int type){
517 Type tempType;
518 if( !NumOpe(Parameter,Type(),tempType) ){
519 return;
520 }
521 if(!tempType.IsWhole()){
522 SetError(11,Parameter,cp);
523 return;
524 }
525 ChangeTypeToLong(tempType.GetBasicType());
526
527 if(type==DEF_DOUBLE){
528 //pop eax
529 compiler.codeGenerator.op_pop(REG_EAX);
530
531 //fld qword ptr[eax]
532 compiler.codeGenerator.PutOld(
533 (char)0xDD,
534 (char)0x00
535 );
536 }
537 else if(type==DEF_SINGLE||type==DEF_DWORD){
538 //pop eax
539 compiler.codeGenerator.op_pop(REG_EAX);
540
541 //mov eax,dword ptr[eax]
542 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EAX, REG_EAX, 0, MOD_BASE );
543 }
544 else if(type==DEF_QWORD){
545 //pop ecx
546 compiler.codeGenerator.op_pop(REG_ECX);
547
548 //mov eax,dword ptr[ecx]
549 compiler.codeGenerator.op_mov_RM(sizeof(long),REG_EAX,REG_ECX,0,MOD_BASE);
550
551 //mov edx,dword ptr[ecx+sizeof(long)]
552 compiler.codeGenerator.op_mov_RM(sizeof(long),REG_EDX,REG_ECX,sizeof(long),MOD_BASE_DISP8);
553 }
554 else if(type==DEF_WORD){
555 //pop ebx
556 compiler.codeGenerator.op_pop(REG_EBX);
557
558 //xor eax,eax
559 compiler.codeGenerator.op_xor_RR(REG_EAX);
560
561 //mov ax,word ptr[ebx]
562 compiler.codeGenerator.op_mov_RM( sizeof(short), REG_EAX, REG_EBX, 0, MOD_BASE );
563 }
564 else if(type==DEF_BYTE){
565 //pop ebx
566 compiler.codeGenerator.op_pop(REG_EBX);
567
568 //xor eax,eax
569 compiler.codeGenerator.op_xor_RR(REG_EAX);
570
571 //mov al,byte ptr[ebx]
572 compiler.codeGenerator.op_mov_RM( sizeof(char), REG_EAX, REG_EBX, 0, MOD_BASE );
573 }
574}
575
576bool Opcode_CallFunc( const char *Parameter, const int FuncNum, const Type &baseType, Type &resultType, bool isCallOn )
577{
578 switch(FuncNum){
579 case FUNC_FIX:
580 if( isCallOn ) Opcode_Func_Fix(Parameter);
581 resultType.SetBasicType( DEF_LONG );
582 break;
583 case FUNC_CUDBL:
584 if( isCallOn ) Opcode_Func_CUDbl(Parameter);
585 resultType.SetBasicType( DEF_DOUBLE );
586 break;
587 case FUNC_LEN:
588 if( isCallOn ) Opcode_Func_Len(Parameter);
589 resultType.SetBasicType( DEF_LONG );
590 break;
591 case FUNC_ADDRESSOF:
592 Opcode_Func_AddressOf( Parameter, baseType, isCallOn, resultType );
593 break;
594 case FUNC_SIZEOF:
595 if( isCallOn ) Opcode_Func_SizeOf(Parameter);
596 resultType.SetBasicType( DEF_LONG );
597 break;
598 case FUNC_VARPTR:
599 Opcode_Func_VarPtr( Parameter, resultType, isCallOn );
600 break;
601 case FUNC_OBJPTR:
602 Opcode_Func_ObjPtr( Parameter, resultType, isCallOn );
603 break;
604 case FUNC_DELEGATE_DYNAMICMETHOD_CALL:
605 Opcode_Func_delegate_call( Parameter, resultType, true, isCallOn );
606 break;
607 case FUNC_DELEGATE_STATICMETHOD_CALL:
608 Opcode_Func_delegate_call( Parameter, resultType, false, isCallOn );
609 break;
610
611 case FUNC_GETDOUBLE:
612 if( isCallOn ) Opcode_Func_GetPtrData(Parameter,DEF_DOUBLE);
613 resultType.SetBasicType( DEF_DOUBLE );
614 break;
615 case FUNC_GETSINGLE:
616 if( isCallOn ) Opcode_Func_GetPtrData(Parameter,DEF_SINGLE);
617 resultType.SetBasicType( DEF_SINGLE );
618 break;
619 case FUNC_GETQWORD:
620 if( isCallOn ) Opcode_Func_GetPtrData(Parameter,DEF_QWORD);
621 resultType.SetBasicType( DEF_QWORD );
622 break;
623 case FUNC_GETDWORD:
624 if( isCallOn ) Opcode_Func_GetPtrData(Parameter,DEF_DWORD);
625 resultType.SetBasicType( DEF_DWORD );
626 break;
627 case FUNC_GETWORD:
628 if( isCallOn ) Opcode_Func_GetPtrData(Parameter,DEF_WORD);
629 resultType.SetBasicType( DEF_WORD );
630 break;
631 case FUNC_GETBYTE:
632 if( isCallOn ) Opcode_Func_GetPtrData(Parameter,DEF_BYTE);
633 resultType.SetBasicType( DEF_BYTE );
634 break;
635 default:
636 return false;
637 }
638 return true;
639}
Note: See TracBrowser for help on using the repository browser.