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

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